*** 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
36 (eval-when-compile (require 'cl))
37
38 ;; Site dependent variables.  These variables should be defined in
39 ;; paths.el.
40
41 (defvar gnus-default-nntp-server nil
42   "Specify a default NNTP server.
43 This variable should be defined in paths.el, and should never be set
44 by the user.
45 If you want to change servers, you should use `gnus-select-method'.
46 See the documentation to that variable.")
47
48 (defvar gnus-backup-default-subscribed-newsgroups
49   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
50   "Default default new newsgroups the first time Gnus is run.
51 Should be set in paths.el, and shouldn't be touched by the user.")
52
53 (defvar gnus-local-domain nil
54   "Local domain name without a host name.
55 The DOMAINNAME environment variable is used instead if it is defined.
56 If the `system-name' function returns the full Internet name, there is
57 no need to set this variable.")
58
59 (defvar gnus-local-organization nil
60   "String with a description of what organization (if any) the user belongs to.
61 The ORGANIZATION environment variable is used instead if it is defined.
62 If this variable contains a function, this function will be called
63 with the current newsgroup name as the argument.  The function should
64 return a string.
65
66 In any case, if the string (either in the variable, in the environment
67 variable, or returned by the function) is a file name, the contents of
68 this file will be used as the organization.")
69
70 ;; Customization variables
71
72 ;; Don't touch this variable.
73 (defvar gnus-nntp-service "nntp"
74   "*NNTP service name (\"nntp\" or 119).
75 This is an obsolete variable, which is scarcely used.  If you use an
76 nntp server for your newsgroup and want to change the port number
77 used to 899, you would say something along these lines:
78
79  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
80
81 (defvar gnus-nntpserver-file "/etc/nntpserver"
82   "*A file with only the name of the nntp server in it.")
83
84 ;; This function is used to check both the environment variable
85 ;; NNTPSERVER and the /etc/nntpserver file to see whether one can find
86 ;; an nntp server name default.
87 (defun gnus-getenv-nntpserver ()
88   (or (getenv "NNTPSERVER")
89       (and (file-readable-p gnus-nntpserver-file)
90            (save-excursion
91              (set-buffer (get-buffer-create " *gnus nntp*"))
92              (buffer-disable-undo (current-buffer))
93              (insert-file-contents gnus-nntpserver-file)
94              (let ((name (buffer-string)))
95                (prog1
96                    (if (string-match "^[ \t\n]*$" name)
97                        nil
98                      name)
99                  (kill-buffer (current-buffer))))))))
100
101 (defvar gnus-select-method
102   (nconc
103    (list 'nntp (or (condition-case ()
104                        (gnus-getenv-nntpserver)
105                      (error nil))
106                    (if (and gnus-default-nntp-server
107                             (not (string= gnus-default-nntp-server "")))
108                        gnus-default-nntp-server)
109                    (system-name)))
110    (if (or (null gnus-nntp-service)
111            (equal gnus-nntp-service "nntp"))
112        nil
113      (list gnus-nntp-service)))
114   "*Default method for selecting a newsgroup.
115 This variable should be a list, where the first element is how the
116 news is to be fetched, the second is the address.
117
118 For instance, if you want to get your news via NNTP from
119 \"flab.flab.edu\", you could say:
120
121 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
122
123 If you want to use your local spool, say:
124
125 (setq gnus-select-method (list 'nnspool (system-name)))
126
127 If you use this variable, you must set `gnus-nntp-server' to nil.
128
129 There is a lot more to know about select methods and virtual servers -
130 see the manual for details.")
131
132 (defvar gnus-message-archive-method 
133   '(nnfolder "archive" (nnfolder-directory "~/Mail/archive/")
134              (nnfolder-active-file "~/Mail/archive/active")
135              (nnfolder-get-new-mail nil)
136              (nnfolder-inhibit-expiry t))
137   "*Method used for archiving messages you've sent.
138 This should be a mail method.")
139
140 (defvar gnus-refer-article-method nil
141   "*Preferred method for fetching an article by Message-ID.
142 If you are reading news from the local spool (with nnspool), fetching
143 articles by Message-ID is painfully slow.  By setting this method to an
144 nntp method, you might get acceptable results.
145
146 The value of this variable must be a valid select method as discussed
147 in the documentation of `gnus-select-method'.")
148
149 (defvar gnus-secondary-select-methods nil
150   "*A list of secondary methods that will be used for reading news.
151 This is a list where each element is a complete select method (see
152 `gnus-select-method').
153
154 If, for instance, you want to read your mail with the nnml backend,
155 you could set this variable:
156
157 (setq gnus-secondary-select-methods '((nnml \"\")))")
158
159 (defvar gnus-secondary-servers nil
160   "*List of NNTP servers that the user can choose between interactively.
161 To make Gnus query you for a server, you have to give `gnus' a
162 non-numeric prefix - `C-u M-x gnus', in short.")
163
164 (defvar gnus-nntp-server nil
165   "*The name of the host running the NNTP server.
166 This variable is semi-obsolete.  Use the `gnus-select-method'
167 variable instead.")
168
169 (defvar gnus-startup-file "~/.newsrc"
170   "*Your `.newsrc' file.
171 `.newsrc-SERVER' will be used instead if that exists.")
172
173 (defvar gnus-init-file "~/.gnus"
174   "*Your Gnus elisp startup file.
175 If a file with the .el or .elc suffixes exist, it will be read
176 instead.")
177
178 (defvar gnus-group-faq-directory
179   '("/ftp@mirrors.aol.com:/pub/rtfm/usenet/"
180     "/ftp@sunsite.auc.dk:/pub/usenet/"
181     "/ftp@src.doc.ic.ac.uk:/usenet/news-FAQS/"
182     "/ftp@ftp.seas.gwu.edu:/pub/rtfm/"
183     "/ftp@rtfm.mit.edu:/pub/usenet/"
184     "/ftp@ftp.uni-paderborn.de:/pub/FAQ/"
185     "/ftp@ftp.sunet.se:/pub/usenet/"
186     "/ftp@nctuccca.edu.tw:/USENET/FAQ/"
187     "/ftp@hwarang.postech.ac.kr:/pub/usenet/"
188     "/ftp@ftp.hk.super.net:/mirror/faqs/")
189   "*Directory where the group FAQs are stored.
190 This will most commonly be on a remote machine, and the file will be
191 fetched by ange-ftp.
192
193 This variable can also be a list of directories.  In that case, the
194 first element in the list will be used by default, and the others will
195 be used as backup sites.
196
197 Note that Gnus uses an aol machine as the default directory.  If this
198 feels fundamentally unclean, just think of it as a way to finally get
199 something of value back from them.
200
201 If the default site is too slow, try one of these:
202
203    North America: mirrors.aol.com                /pub/rtfm/usenet
204                   ftp.seas.gwu.edu               /pub/rtfm
205                   rtfm.mit.edu                   /pub/usenet
206    Europe:        ftp.uni-paderborn.de           /pub/FAQ
207                   src.doc.ic.ac.uk               /usenet/news-FAQS
208                   ftp.sunet.se                   /pub/usenet
209                   sunsite.auc.dk                 /pub/usenet
210    Asia:          nctuccca.edu.tw                /USENET/FAQ
211                   hwarang.postech.ac.kr          /pub/usenet
212                   ftp.hk.super.net               /mirror/faqs")
213
214 (defvar gnus-group-archive-directory
215   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/"
216   "*The address of the (ding) archives.")
217
218 (defvar gnus-group-recent-archive-directory
219   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
220   "*The address of the most recent (ding) articles.")
221
222 (defvar gnus-default-subscribed-newsgroups nil
223   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
224 It should be a list of strings.
225 If it is `t', Gnus will not do anything special the first time it is
226 started; it'll just use the normal newsgroups subscription methods.")
227
228 (defvar gnus-use-cross-reference t
229   "*Non-nil means that cross referenced articles will be marked as read.
230 If nil, ignore cross references.  If t, mark articles as read in
231 subscribed newsgroups.  If neither t nor nil, mark as read in all
232 newsgroups.")
233
234 (defvar gnus-single-article-buffer t
235   "*If non-nil, display all articles in the same buffer.
236 If nil, each group will get its own article buffer.")
237
238 (defvar gnus-use-dribble-file t
239   "*Non-nil means that Gnus will use a dribble file to store user updates.
240 If Emacs should crash without saving the .newsrc files, complete
241 information can be restored from the dribble file.")
242
243 (defvar gnus-dribble-directory nil
244   "*The directory where dribble files will be saved.
245 If this variable is nil, the directory where the .newsrc files are
246 saved will be used.")
247
248 (defvar gnus-asynchronous nil
249   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
250
251 (defvar gnus-kill-summary-on-exit t
252   "*If non-nil, kill the summary buffer when you exit from it.
253 If nil, the summary will become a \"*Dead Summary*\" buffer, and
254 it will be killed sometime later.")
255
256 (defvar gnus-large-newsgroup 200
257   "*The number of articles which indicates a large newsgroup.
258 If the number of articles in a newsgroup is greater than this value,
259 confirmation is required for selecting the newsgroup.")
260
261 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
262 (defvar gnus-no-groups-message "No news is horrible news"
263   "*Message displayed by Gnus when no groups are available.")
264
265 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
266   "*Non-nil means that the default name of a file to save articles in is the group name.
267 If it's nil, the directory form of the group name is used instead.
268
269 If this variable is a list, and the list contains the element
270 `not-score', long file names will not be used for score files; if it
271 contains the element `not-save', long file names will not be used for
272 saving; and if it contains the element `not-kill', long file names
273 will not be used for kill files.")
274
275 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
276   "*Name of the directory articles will be saved in (default \"~/News\").
277 Initialized from the SAVEDIR environment variable.")
278
279 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
280   "*Name of the directory where kill files will be stored (default \"~/News\").
281 Initialized from the SAVEDIR environment variable.")
282
283 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
284   "*A function to save articles in your favorite format.
285 The function must be interactively callable (in other words, it must
286 be an Emacs command).
287
288 Gnus provides the following functions:
289
290 * gnus-summary-save-in-rmail (Rmail format)
291 * gnus-summary-save-in-mail (Unix mail format)
292 * gnus-summary-save-in-folder (MH folder)
293 * gnus-summary-save-in-file (article format).
294 * gnus-summary-save-in-vm (use VM's folder format).")
295
296 (defvar gnus-prompt-before-saving 'always
297   "*This variable says how much prompting is to be done when saving articles.
298 If it is nil, no prompting will be done, and the articles will be
299 saved to the default files.  If this variable is `always', each and
300 every article that is saved will be preceded by a prompt, even when
301 saving large batches of articles.  If this variable is neither nil not
302 `always', there the user will be prompted once for a file name for
303 each invocation of the saving commands.")
304
305 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
306   "*A function generating a file name to save articles in Rmail format.
307 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
308
309 (defvar gnus-mail-save-name (function gnus-plain-save-name)
310   "*A function generating a file name to save articles in Unix mail format.
311 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
312
313 (defvar gnus-folder-save-name (function gnus-folder-save-name)
314   "*A function generating a file name to save articles in MH folder.
315 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
316
317 (defvar gnus-file-save-name (function gnus-numeric-save-name)
318   "*A function generating a file name to save articles in article format.
319 The function is called with NEWSGROUP, HEADERS, and optional
320 LAST-FILE.")
321
322 (defvar gnus-split-methods
323   '((gnus-article-archive-name))
324   "*Variable used to suggest where articles are to be saved.
325 For instance, if you would like to save articles related to Gnus in
326 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
327 you could set this variable to something like:
328
329  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
330    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
331
332 This variable is an alist where the where the key is the match and the
333 value is a list of possible files to save in if the match is non-nil.
334
335 If the match is a string, it is used as a regexp match on the
336 article.  If the match is a symbol, that symbol will be funcalled
337 from the buffer of the article to be saved with the newsgroup as the
338 parameter.  If it is a list, it will be evaled in the same buffer.
339
340 If this form or function returns a string, this string will be used as
341 a possible file name; and if it returns a non-nil list, that list will
342 be used as possible file names.")
343
344 (defvar gnus-move-split-methods nil
345   "*Variable used to suggest where articles are to be moved to.
346 It uses the same syntax as the `gnus-split-methods' variable.")
347
348 (defvar gnus-save-score nil
349   "*If non-nil, save group scoring info.")
350
351 (defvar gnus-use-adaptive-scoring nil
352   "*If non-nil, use some adaptive scoring scheme.")
353
354 (defvar gnus-use-cache nil
355   "*If nil, Gnus will ignore the article cache.
356 If `passive', it will allow entering (and reading) articles
357 explicitly entered into the cache.  If anything else, use the
358 cache to the full extent of the law.")
359
360 (defvar gnus-use-trees nil
361   "*If non-nil, display a thread tree buffer.")
362
363 (defvar gnus-use-grouplens nil
364   "*If non-nil, use GroupLens ratings.")
365
366 (defvar gnus-keep-backlog nil
367   "*If non-nil, Gnus will keep read articles for later re-retrieval.
368 If it is a number N, then Gnus will only keep the last N articles
369 read.  If it is neither nil nor a number, Gnus will keep all read
370 articles.  This is not a good idea.")
371
372 (defvar gnus-use-nocem nil
373   "*If non-nil, Gnus will read NoCeM cancel messages.")
374
375 (defvar gnus-use-demon nil
376   "If non-nil, Gnus might use some demons.")
377
378 (defvar gnus-use-scoring t
379   "*If non-nil, enable scoring.")
380
381 (defvar gnus-use-picons nil
382   "*If non-nil, display picons.")
383
384 (defvar gnus-fetch-old-headers nil
385   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
386 If an unread article in the group refers to an older, already read (or
387 just marked as read) article, the old article will not normally be
388 displayed in the Summary buffer.  If this variable is non-nil, Gnus
389 will attempt to grab the headers to the old articles, and thereby
390 build complete threads.  If it has the value `some', only enough
391 headers to connect otherwise loose threads will be displayed.
392 This variable can also be a number.  In that case, no more than that
393 number of old headers will be fetched.
394
395 The server has to support NOV for any of this to work.")
396
397 ;see gnus-cus.el
398 ;(defvar gnus-visual t
399 ;  "*If non-nil, will do various highlighting.
400 ;If nil, no mouse highlights (or any other highlights) will be
401 ;performed.  This might speed up Gnus some when generating large group
402 ;and summary buffers.")
403
404 (defvar gnus-novice-user t
405   "*Non-nil means that you are a usenet novice.
406 If non-nil, verbose messages may be displayed and confirmations may be
407 required.")
408
409 (defvar gnus-expert-user nil
410   "*Non-nil means that you will never be asked for confirmation about anything.
411 And that means *anything*.")
412
413 (defvar gnus-verbose 7
414   "*Integer that says how verbose Gnus should be.
415 The higher the number, the more messages Gnus will flash to say what
416 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
417 display most important messages; and at ten, Gnus will keep on
418 jabbering all the time.")
419
420 (defvar gnus-keep-same-level nil
421   "*Non-nil means that the next newsgroup after the current will be on the same level.
422 When you type, for instance, `n' after reading the last article in the
423 current newsgroup, you will go to the next newsgroup.  If this variable
424 is nil, the next newsgroup will be the next from the group
425 buffer.
426 If this variable is non-nil, Gnus will either put you in the
427 next newsgroup with the same level, or, if no such newsgroup is
428 available, the next newsgroup with the lowest possible level higher
429 than the current level.
430 If this variable is `best', Gnus will make the next newsgroup the one
431 with the best level.")
432
433 (defvar gnus-summary-make-false-root 'adopt
434   "*nil means that Gnus won't gather loose threads.
435 If the root of a thread has expired or been read in a previous
436 session, the information necessary to build a complete thread has been
437 lost.  Instead of having many small sub-threads from this original thread
438 scattered all over the summary buffer, Gnus can gather them.
439
440 If non-nil, Gnus will try to gather all loose sub-threads from an
441 original thread into one large thread.
442
443 If this variable is non-nil, it should be one of `none', `adopt',
444 `dummy' or `empty'.
445
446 If this variable is `none', Gnus will not make a false root, but just
447 present the sub-threads after another.
448 If this variable is `dummy', Gnus will create a dummy root that will
449 have all the sub-threads as children.
450 If this variable is `adopt', Gnus will make one of the \"children\"
451 the parent and mark all the step-children as such.
452 If this variable is `empty', the \"children\" are printed with empty
453 subject fields.  (Or rather, they will be printed with a string
454 given by the `gnus-summary-same-subject' variable.)")
455
456 (defvar gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
457   "*A regexp to match subjects to be excluded from loose thread gathering.
458 As loose thread gathering is done on subjects only, that means that
459 there can be many false gatherings performed.  By rooting out certain
460 common subjects, gathering might become saner.")
461
462 (defvar gnus-summary-gather-subject-limit nil
463   "*Maximum length of subject comparisons when gathering loose threads.
464 Use nil to compare full subjects.  Setting this variable to a low
465 number will help gather threads that have been corrupted by
466 newsreaders chopping off subject lines, but it might also mean that
467 unrelated articles that have subject that happen to begin with the
468 same few characters will be incorrectly gathered.
469
470 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
471 comparing subjects.")
472
473 (defvar gnus-simplify-ignored-prefixes nil
474   "*Regexp, matches for which are removed from subject lines when simplifying.")
475
476 (defvar gnus-build-sparse-threads nil
477   "*If non-nil, fill in the gaps in threads.
478 If `some', only fill in the gaps that are needed to tie loose threads
479 together.  If `more', fill in all leaf nodes that Gnus can find.  If
480 non-nil and non-`some', fill in all gaps that Gnus manages to guess.")
481
482 (defvar gnus-summary-thread-gathering-function 'gnus-gather-threads-by-subject
483   "Function used for gathering loose threads.
484 There are two pre-defined functions: `gnus-gather-threads-by-subject',
485 which only takes Subjects into consideration; and
486 `gnus-gather-threads-by-references', which compared the References
487 headers of the articles to find matches.")
488
489 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
490 (defvar gnus-summary-same-subject ""
491   "*String indicating that the current article has the same subject as the previous.
492 This variable will only be used if the value of
493 `gnus-summary-make-false-root' is `empty'.")
494
495 (defvar gnus-summary-goto-unread t
496   "*If non-nil, marking commands will go to the next unread article.
497 If `never', \\<gnus-summary-mode-map>\\[gnus-summary-next-page] will go to the next article,
498 whether it is read or not.")
499
500 (defvar gnus-group-goto-unread t
501   "*If non-nil, movement commands will go to the next unread and subscribed group.")
502
503 (defvar gnus-goto-next-group-when-activating t
504   "*If non-nil, the \\<gnus-group-mode-map>\\[gnus-group-get-new-news-this-group] command will advance point to the next group.")
505
506 (defvar gnus-check-new-newsgroups t
507   "*Non-nil means that Gnus will add new newsgroups at startup.
508 If this variable is `ask-server', Gnus will ask the server for new
509 groups since the last time it checked.  This means that the killed list
510 is no longer necessary, so you could set `gnus-save-killed-list' to
511 nil.
512
513 A variant is to have this variable be a list of select methods.  Gnus
514 will then use the `ask-server' method on all these select methods to
515 query for new groups from all those servers.
516
517 Eg.
518   (setq gnus-check-new-newsgroups
519         '((nntp \"some.server\") (nntp \"other.server\")))
520
521 If this variable is nil, then you have to tell Gnus explicitly to
522 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
523
524 (defvar gnus-check-bogus-newsgroups nil
525   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
526 If this variable is nil, then you have to tell Gnus explicitly to
527 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
528
529 (defvar gnus-read-active-file t
530   "*Non-nil means that Gnus will read the entire active file at startup.
531 If this variable is nil, Gnus will only know about the groups in your
532 `.newsrc' file.
533
534 If this variable is `some', Gnus will try to only read the relevant
535 parts of the active file from the server.  Not all servers support
536 this, and it might be quite slow with other servers, but this should
537 generally be faster than both the t and nil value.
538
539 If you set this variable to nil or `some', you probably still want to
540 be told about new newsgroups that arrive.  To do that, set
541 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
542 properly with all servers.")
543
544 (defvar gnus-level-subscribed 5
545   "*Groups with levels less than or equal to this variable are subscribed.")
546
547 (defvar gnus-level-unsubscribed 7
548   "*Groups with levels less than or equal to this variable are unsubscribed.
549 Groups with levels less than `gnus-level-subscribed', which should be
550 less than this variable, are subscribed.")
551
552 (defvar gnus-level-zombie 8
553   "*Groups with this level are zombie groups.")
554
555 (defvar gnus-level-killed 9
556   "*Groups with this level are killed.")
557
558 (defvar gnus-level-default-subscribed 3
559   "*New subscribed groups will be subscribed at this level.")
560
561 (defvar gnus-level-default-unsubscribed 6
562   "*New unsubscribed groups will be unsubscribed at this level.")
563
564 (defvar gnus-activate-level (1+ gnus-level-subscribed)
565   "*Groups higher than this level won't be activated on startup.
566 Setting this variable to something log might save lots of time when
567 you have many groups that you aren't interested in.")
568
569 (defvar gnus-activate-foreign-newsgroups 4
570   "*If nil, Gnus will not check foreign newsgroups at startup.
571 If it is non-nil, it should be a number between one and nine.  Foreign
572 newsgroups that have a level lower or equal to this number will be
573 activated on startup.  For instance, if you want to active all
574 subscribed newsgroups, but not the rest, you'd set this variable to
575 `gnus-level-subscribed'.
576
577 If you subscribe to lots of newsgroups from different servers, startup
578 might take a while.  By setting this variable to nil, you'll save time,
579 but you won't be told how many unread articles there are in the
580 groups.")
581
582 (defvar gnus-save-newsrc-file t
583   "*Non-nil means that Gnus will save the `.newsrc' file.
584 Gnus always saves its own startup file, which is called
585 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
586 be readily understood by other newsreaders.  If you don't plan on
587 using other newsreaders, set this variable to nil to save some time on
588 exit.")
589
590 (defvar gnus-save-killed-list t
591   "*If non-nil, save the list of killed groups to the startup file.
592 If you set this variable to nil, you'll save both time (when starting
593 and quitting) and space (both memory and disk), but it will also mean
594 that Gnus has no record of which groups are new and which are old, so
595 the automatic new newsgroups subscription methods become meaningless.
596
597 You should always set `gnus-check-new-newsgroups' to `ask-server' or
598 nil if you set this variable to nil.")
599
600 (defvar gnus-interactive-catchup t
601   "*If non-nil, require your confirmation when catching up a group.")
602
603 (defvar gnus-interactive-post t
604   "*If non-nil, group name will be asked for when posting.")
605
606 (defvar gnus-interactive-exit t
607   "*If non-nil, require your confirmation when exiting Gnus.")
608
609 (defvar gnus-kill-killed t
610   "*If non-nil, Gnus will apply kill files to already killed articles.
611 If it is nil, Gnus will never apply kill files to articles that have
612 already been through the scoring process, which might very well save lots
613 of time.")
614
615 (defvar gnus-extract-address-components 'gnus-extract-address-components
616   "*Function for extracting address components from a From header.
617 Two pre-defined function exist: `gnus-extract-address-components',
618 which is the default, quite fast, and too simplistic solution, and
619 `mail-extract-address-components', which works much better, but is
620 slower.")
621
622 (defvar gnus-summary-default-score 0
623   "*Default article score level.
624 If this variable is nil, scoring will be disabled.")
625
626 (defvar gnus-summary-zcore-fuzz 0
627   "*Fuzziness factor for the zcore in the summary buffer.
628 Articles with scores closer than this to `gnus-summary-default-score'
629 will not be marked.")
630
631 (defvar gnus-simplify-subject-fuzzy-regexp nil
632   "*Strings to be removed when doing fuzzy matches.
633 This can either be a regular expression or list of regular expressions
634 that will be removed from subject strings if fuzzy subject
635 simplification is selected.")
636
637 (defvar gnus-permanently-visible-groups nil
638   "*Regexp to match groups that should always be listed in the group buffer.
639 This means that they will still be listed when there are no unread
640 articles in the groups.")
641
642 (defvar gnus-list-groups-with-ticked-articles t
643   "*If non-nil, list groups that have only ticked articles.
644 If nil, only list groups that have unread articles.")
645
646 (defvar gnus-group-default-list-level gnus-level-subscribed
647   "*Default listing level.
648 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
649
650 (defvar gnus-group-use-permanent-levels nil
651   "*If non-nil, once you set a level, Gnus will use this level.")
652
653 (defvar gnus-group-list-inactive-groups t
654   "*If non-nil, inactive groups will be listed.")
655
656 (defvar gnus-show-mime nil
657   "*If non-nil, do mime processing of articles.
658 The articles will simply be fed to the function given by
659 `gnus-show-mime-method'.")
660
661 (defvar gnus-strict-mime t
662   "*If nil, MIME-decode even if there is no Mime-Version header in the article.")
663
664 (defvar gnus-show-mime-method 'metamail-buffer
665   "*Function to process a MIME message.
666 The function is called from the article buffer.")
667
668 (defvar gnus-decode-encoded-word-method (lambda ())
669   "*Function to decode a MIME encoded-words.
670 The function is called from the article buffer.")
671
672 (defvar gnus-show-threads t
673   "*If non-nil, display threads in summary mode.")
674
675 (defvar gnus-thread-hide-subtree nil
676   "*If non-nil, hide all threads initially.
677 If threads are hidden, you have to run the command
678 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
679 to expose hidden threads.")
680
681 (defvar gnus-thread-hide-killed t
682   "*If non-nil, hide killed threads automatically.")
683
684 (defvar gnus-thread-ignore-subject nil
685   "*If non-nil, ignore subjects and do all threading based on the Reference header.
686 If nil, which is the default, articles that have different subjects
687 from their parents will start separate threads.")
688
689 (defvar gnus-thread-operation-ignore-subject t
690   "*If non-nil, subjects will be ignored when doing thread commands.
691 This affects commands like `gnus-summary-kill-thread' and
692 `gnus-summary-lower-thread'.
693
694 If this variable is nil, articles in the same thread with different
695 subjects will not be included in the operation in question.  If this
696 variable is `fuzzy', only articles that have subjects that are fuzzily
697 equal will be included.")
698
699 (defvar gnus-thread-indent-level 4
700   "*Number that says how much each sub-thread should be indented.")
701
702 (defvar gnus-ignored-newsgroups
703   (purecopy (mapconcat 'identity
704                        '("^to\\."       ; not "real" groups
705                          "^[0-9. \t]+ " ; all digits in name
706                          "[][\"#'()]"   ; bogus characters
707                          )
708                        "\\|"))
709   "*A regexp to match uninteresting newsgroups in the active file.
710 Any lines in the active file matching this regular expression are
711 removed from the newsgroup list before anything else is done to it,
712 thus making them effectively non-existent.")
713
714 (defvar gnus-ignored-headers
715   "^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:"
716   "*All headers that match this regexp will be hidden.
717 This variable can also be a list of regexps of headers to be ignored.
718 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
719
720 (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-"
721   "*All headers that do not match this regexp will be hidden.
722 This variable can also be a list of regexp of headers to remain visible.
723 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
724
725 (defvar gnus-sorted-header-list
726   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:"
727     "^Cc:" "^Date:" "^Organization:")
728   "*This variable is a list of regular expressions.
729 If it is non-nil, headers that match the regular expressions will
730 be placed first in the article buffer in the sequence specified by
731 this list.")
732
733 (defvar gnus-boring-article-headers
734   '(empty followup-to reply-to)
735   "*Headers that are only to be displayed if they have interesting data.
736 Possible values in this list are `empty', `newsgroups', `followup-to',
737 `reply-to', and `date'.")
738
739 (defvar gnus-show-all-headers nil
740   "*If non-nil, don't hide any headers.")
741
742 (defvar gnus-save-all-headers t
743   "*If non-nil, don't remove any headers before saving.")
744
745 (defvar gnus-saved-headers gnus-visible-headers
746   "*Headers to keep if `gnus-save-all-headers' is nil.
747 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
748 If that variable is nil, however, all headers that match this regexp
749 will be kept while the rest will be deleted before saving.")
750
751 (defvar gnus-inhibit-startup-message nil
752   "*If non-nil, the startup message will not be displayed.")
753
754 (defvar gnus-signature-separator "^-- *$"
755   "Regexp matching signature separator.")
756
757 (defvar gnus-signature-limit nil
758   "Provide a limit to what is considered a signature.
759 If it is a number, no signature may not be longer (in characters) than
760 that number.  If it is a function, the function will be called without
761 any parameters, and if it returns nil, there is no signature in the
762 buffer.  If it is a string, it will be used as a regexp.  If it
763 matches, the text in question is not a signature.")
764
765 (defvar gnus-auto-extend-newsgroup t
766   "*If non-nil, extend newsgroup forward and backward when requested.")
767
768 (defvar gnus-auto-select-first t
769   "*If nil, don't select the first unread article when entering a group.
770 If this variable is `best', select the highest-scored unread article
771 in the group.  If neither nil nor `best', select the first unread
772 article.
773
774 If you want to prevent automatic selection of the first unread article
775 in some newsgroups, set the variable to nil in
776 `gnus-select-group-hook'.")
777
778 (defvar gnus-auto-select-next t
779   "*If non-nil, offer to go to the next group from the end of the previous.
780 If the value is t and the next newsgroup is empty, Gnus will exit
781 summary mode and go back to group mode.  If the value is neither nil
782 nor t, Gnus will select the following unread newsgroup.  In
783 particular, if the value is the symbol `quietly', the next unread
784 newsgroup will be selected without any confirmation, and if it is
785 `almost-quietly', the next group will be selected without any
786 confirmation if you are located on the last article in the group.
787 Finally, if this variable is `slightly-quietly', the `Z n' command
788 will go to the next group without confirmation.")
789
790 (defvar gnus-auto-select-same nil
791   "*If non-nil, select the next article with the same subject.")
792
793 (defvar gnus-summary-check-current nil
794   "*If non-nil, consider the current article when moving.
795 The \"unread\" movement commands will stay on the same line if the
796 current article is unread.")
797
798 (defvar gnus-auto-center-summary t
799   "*If non-nil, always center the current summary buffer.
800 In particular, if `vertical' do only vertical recentering.  If non-nil
801 and non-`vertical', do both horizontal and vertical recentering.")
802
803 (defvar gnus-break-pages t
804   "*If non-nil, do page breaking on articles.
805 The page delimiter is specified by the `gnus-page-delimiter'
806 variable.")
807
808 (defvar gnus-page-delimiter "^\^L"
809   "*Regexp describing what to use as article page delimiters.
810 The default value is \"^\^L\", which is a form linefeed at the
811 beginning of a line.")
812
813 (defvar gnus-use-full-window t
814   "*If non-nil, use the entire Emacs screen.")
815
816 (defvar gnus-window-configuration nil
817   "Obsolete variable.  See `gnus-buffer-configuration'.")
818
819 (defvar gnus-window-min-width 2
820   "*Minimum width of Gnus buffers.")
821
822 (defvar gnus-window-min-height 1
823   "*Minimum height of Gnus buffers.")
824
825 (defvar gnus-buffer-configuration
826   '((group
827      (vertical 1.0
828                (group 1.0 point)
829                (if gnus-carpal '(group-carpal 4))))
830     (summary
831      (vertical 1.0
832                (summary 1.0 point)
833                (if gnus-carpal '(summary-carpal 4))))
834     (article
835      (cond 
836       (gnus-use-picons
837        '(frame 1.0
838                (vertical 1.0
839                          (summary 0.25 point)
840                          (if gnus-carpal '(summary-carpal 4))
841                          (article 1.0))
842                (vertical ((height . 5) (width . 15)
843                           (user-position . t)
844                           (left . -1) (top . 1))
845                          (picons 1.0))))
846       (gnus-use-trees
847        '(vertical 1.0
848                   (summary 0.25 point)
849                   (tree 0.25)
850                   (article 1.0)))
851       (t
852        '(vertical 1.0
853                  (summary 0.25 point)
854                  (if gnus-carpal '(summary-carpal 4))
855                  (if gnus-use-trees '(tree 0.25))
856                  (article 1.0)))))
857     (server
858      (vertical 1.0
859                (server 1.0 point)
860                (if gnus-carpal '(server-carpal 2))))
861     (browse
862      (vertical 1.0
863                (browse 1.0 point)
864                (if gnus-carpal '(browse-carpal 2))))
865     (message
866      (vertical 1.0
867                (message 1.0 point)))
868     (pick
869      (vertical 1.0
870                (article 1.0 point)))
871     (info
872      (vertical 1.0
873                (info 1.0 point)))
874     (summary-faq
875      (vertical 1.0
876                (summary 0.25)
877                (faq 1.0 point)))
878     (edit-group
879      (vertical 1.0
880                (group 0.5)
881                (edit-group 1.0 point)))
882     (edit-server
883      (vertical 1.0
884                (server 0.5)
885                (edit-server 1.0 point)))
886     (edit-score
887      (vertical 1.0
888                (summary 0.25)
889                (edit-score 1.0 point)))
890     (post
891      (vertical 1.0
892                (post 1.0 point)))
893     (reply
894      (vertical 1.0
895                (article-copy 0.5)
896                (message 1.0 point)))
897     (forward
898      (vertical 1.0
899                (message 1.0 point)))
900     (reply-yank
901      (vertical 1.0
902                (message 1.0 point)))
903     (mail-bounce
904      (vertical 1.0
905                (article 0.5)
906                (message 1.0 point)))
907     (draft
908      (vertical 1.0
909                (draft 1.0 point)))
910     (pipe
911      (vertical 1.0
912                (summary 0.25 point)
913                (if gnus-carpal '(summary-carpal 4))
914                ("*Shell Command Output*" 1.0)))
915     (bug
916      (vertical 1.0
917                ("*Gnus Help Bug*" 0.5)
918                ("*Gnus Bug*" 1.0 point)))
919     (compose-bounce
920      (vertical 1.0
921                (article 0.5)
922                (message 1.0 point))))
923   "Window configuration for all possible Gnus buffers.
924 This variable is a list of lists.  Each of these lists has a NAME and
925 a RULE.  The NAMEs are commonsense names like `group', which names a
926 rule used when displaying the group buffer; `summary', which names a
927 rule for what happens when you enter a group and do not display an
928 article buffer; and so on.  See the value of this variable for a
929 complete list of NAMEs.
930
931 Each RULE is a list of vectors.  The first element in this vector is
932 the name of the buffer to be displayed; the second element is the
933 percentage of the screen this buffer is to occupy (a number in the
934 0.0-0.99 range); the optional third element is `point', which should
935 be present to denote which buffer point is to go to after making this
936 buffer configuration.")
937
938 (defvar gnus-window-to-buffer
939   '((group . gnus-group-buffer)
940     (summary . gnus-summary-buffer)
941     (article . gnus-article-buffer)
942     (server . gnus-server-buffer)
943     (browse . "*Gnus Browse Server*")
944     (edit-group . gnus-group-edit-buffer)
945     (edit-server . gnus-server-edit-buffer)
946     (group-carpal . gnus-carpal-group-buffer)
947     (summary-carpal . gnus-carpal-summary-buffer)
948     (server-carpal . gnus-carpal-server-buffer)
949     (browse-carpal . gnus-carpal-browse-buffer)
950     (edit-score . gnus-score-edit-buffer)
951     (message . gnus-message-buffer)
952     (mail . gnus-message-buffer)
953     (post-news . gnus-message-buffer)
954     (faq . gnus-faq-buffer)
955     (picons . "*Picons*")
956     (tree . gnus-tree-buffer)
957     (info . gnus-info-buffer)
958     (article-copy . gnus-article-copy)
959     (draft . gnus-draft-buffer))
960   "Mapping from short symbols to buffer names or buffer variables.")
961
962 (defvar gnus-carpal nil
963   "*If non-nil, display clickable icons.")
964
965 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
966   "*Function called with a group name when new group is detected.
967 A few pre-made functions are supplied: `gnus-subscribe-randomly'
968 inserts new groups at the beginning of the list of groups;
969 `gnus-subscribe-alphabetically' inserts new groups in strict
970 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
971 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
972 for your decision; `gnus-subscribe-killed' kills all new groups;
973 `gnus-subscribe-zombies' will make all new groups into zombies.")
974
975 ;; Suggested by a bug report by Hallvard B Furuseth.
976 ;; <h.b.furuseth@usit.uio.no>.
977 (defvar gnus-subscribe-options-newsgroup-method
978   (function gnus-subscribe-alphabetically)
979   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
980 If, for instance, you want to subscribe to all newsgroups in the
981 \"no\" and \"alt\" hierarchies, you'd put the following in your
982 .newsrc file:
983
984 options -n no.all alt.all
985
986 Gnus will the subscribe all new newsgroups in these hierarchies with
987 the subscription method in this variable.")
988
989 (defvar gnus-subscribe-hierarchical-interactive nil
990   "*If non-nil, Gnus will offer to subscribe hierarchically.
991 When a new hierarchy appears, Gnus will ask the user:
992
993 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
994
995 If the user pressed `d', Gnus will descend the hierarchy, `y' will
996 subscribe to all newsgroups in the hierarchy and `s' will skip this
997 hierarchy in its entirety.")
998
999 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
1000   "*Function used for sorting the group buffer.
1001 This function will be called with group info entries as the arguments
1002 for the groups to be sorted.  Pre-made functions include
1003 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread',
1004 `gnus-group-sort-by-level', `gnus-group-sort-by-score', and
1005 `gnus-group-sort-by-rank'.
1006
1007 This variable can also be a list of sorting functions.  In that case,
1008 the most significant sort function should be the last function in the
1009 list.")
1010
1011 ;; Mark variables suggested by Thomas Michanek
1012 ;; <Thomas.Michanek@telelogic.se>.
1013 (defvar gnus-unread-mark ? 
1014   "*Mark used for unread articles.")
1015 (defvar gnus-ticked-mark ?!
1016   "*Mark used for ticked articles.")
1017 (defvar gnus-dormant-mark ??
1018   "*Mark used for dormant articles.")
1019 (defvar gnus-del-mark ?r
1020   "*Mark used for del'd articles.")
1021 (defvar gnus-read-mark ?R
1022   "*Mark used for read articles.")
1023 (defvar gnus-expirable-mark ?E
1024   "*Mark used for expirable articles.")
1025 (defvar gnus-killed-mark ?K
1026   "*Mark used for killed articles.")
1027 (defvar gnus-souped-mark ?F
1028   "*Mark used for killed articles.")
1029 (defvar gnus-kill-file-mark ?X
1030   "*Mark used for articles killed by kill files.")
1031 (defvar gnus-low-score-mark ?Y
1032   "*Mark used for articles with a low score.")
1033 (defvar gnus-catchup-mark ?C
1034   "*Mark used for articles that are caught up.")
1035 (defvar gnus-replied-mark ?A
1036   "*Mark used for articles that have been replied to.")
1037 (defvar gnus-cached-mark ?*
1038   "*Mark used for articles that are in the cache.")
1039 (defvar gnus-saved-mark ?S
1040   "*Mark used for articles that have been saved to.")
1041 (defvar gnus-process-mark ?#
1042   "*Process mark.")
1043 (defvar gnus-ancient-mark ?O
1044   "*Mark used for ancient articles.")
1045 (defvar gnus-sparse-mark ?Q
1046   "*Mark used for sparsely reffed articles.")
1047 (defvar gnus-canceled-mark ?G
1048   "*Mark used for canceled articles.")
1049 (defvar gnus-score-over-mark ?+
1050   "*Score mark used for articles with high scores.")
1051 (defvar gnus-score-below-mark ?-
1052   "*Score mark used for articles with low scores.")
1053 (defvar gnus-empty-thread-mark ? 
1054   "*There is no thread under the article.")
1055 (defvar gnus-not-empty-thread-mark ?=
1056   "*There is a thread under the article.")
1057
1058 (defvar gnus-view-pseudo-asynchronously nil
1059   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
1060
1061 (defvar gnus-view-pseudos nil
1062   "*If `automatic', pseudo-articles will be viewed automatically.
1063 If `not-confirm', pseudos will be viewed automatically, and the user
1064 will not be asked to confirm the command.")
1065
1066 (defvar gnus-view-pseudos-separately t
1067   "*If non-nil, one pseudo-article will be created for each file to be viewed.
1068 If nil, all files that use the same viewing command will be given as a
1069 list of parameters to that command.")
1070
1071 (defvar gnus-insert-pseudo-articles t
1072   "*If non-nil, insert pseudo-articles when decoding articles.")
1073
1074 (defvar gnus-group-line-format "%M%S%p%P%5y: %(%g%)%l\n"
1075   "*Format of group lines.
1076 It works along the same lines as a normal formatting string,
1077 with some simple extensions.
1078
1079 %M    Only marked articles (character, \"*\" or \" \")
1080 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
1081 %L    Level of subscribedness (integer)
1082 %N    Number of unread articles (integer)
1083 %I    Number of dormant articles (integer)
1084 %i    Number of ticked and dormant (integer)
1085 %T    Number of ticked articles (integer)
1086 %R    Number of read articles (integer)
1087 %t    Total number of articles (integer)
1088 %y    Number of unread, unticked articles (integer)
1089 %G    Group name (string)
1090 %g    Qualified group name (string)
1091 %D    Group description (string)
1092 %s    Select method (string)
1093 %o    Moderated group (char, \"m\")
1094 %p    Process mark (char)
1095 %O    Moderated group (string, \"(m)\" or \"\")
1096 %P    Topic indentation (string)
1097 %l    Whether there are GroupLens predictions for this group (string)
1098 %n    Select from where (string)
1099 %z    A string that look like `<%s:%n>' if a foreign select method is used
1100 %u    User defined specifier.  The next character in the format string should
1101       be a letter.  Gnus will call the function gnus-user-format-function-X,
1102       where X is the letter following %u.  The function will be passed the
1103       current header as argument.  The function should return a string, which
1104       will be inserted into the buffer just like information from any other
1105       group specifier.
1106
1107 Text between %( and %) will be highlighted with `gnus-mouse-face' when
1108 the mouse point move inside the area.  There can only be one such area.
1109
1110 Note that this format specification is not always respected.  For
1111 reasons of efficiency, when listing killed groups, this specification
1112 is ignored altogether.  If the spec is changed considerably, your
1113 output may end up looking strange when listing both alive and killed
1114 groups.
1115
1116 If you use %o or %O, reading the active file will be slower and quite
1117 a bit of extra memory will be used. %D will also worsen performance.
1118 Also note that if you change the format specification to include any
1119 of these specs, you must probably re-start Gnus to see them go into
1120 effect.")
1121
1122 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
1123   "*The format specification of the lines in the summary buffer.
1124
1125 It works along the same lines as a normal formatting string,
1126 with some simple extensions.
1127
1128 %N   Article number, left padded with spaces (string)
1129 %S   Subject (string)
1130 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
1131 %n   Name of the poster (string)
1132 %a   Extracted name of the poster (string)
1133 %A   Extracted address of the poster (string)
1134 %F   Contents of the From: header (string)
1135 %x   Contents of the Xref: header (string)
1136 %D   Date of the article (string)
1137 %d   Date of the article (string) in DD-MMM format
1138 %M   Message-id of the article (string)
1139 %r   References of the article (string)
1140 %c   Number of characters in the article (integer)
1141 %L   Number of lines in the article (integer)
1142 %I   Indentation based on thread level (a string of spaces)
1143 %T   A string with two possible values: 80 spaces if the article
1144      is on thread level two or larger and 0 spaces on level one
1145 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
1146 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
1147 %[   Opening bracket (character, \"[\" or \"<\")
1148 %]   Closing bracket (character, \"]\" or \">\")
1149 %>   Spaces of length thread-level (string)
1150 %<   Spaces of length (- 20 thread-level) (string)
1151 %i   Article score (number)
1152 %z   Article zcore (character)
1153 %t   Number of articles under the current thread (number).
1154 %e   Whether the thread is empty or not (character).
1155 %l   GroupLens score (string).
1156 %u   User defined specifier.  The next character in the format string should
1157      be a letter.  Gnus will call the function gnus-user-format-function-X,
1158      where X is the letter following %u.  The function will be passed the
1159      current header as argument.  The function should return a string, which
1160      will be inserted into the summary just like information from any other
1161      summary specifier.
1162
1163 Text between %( and %) will be highlighted with `gnus-mouse-face'
1164 when the mouse point is placed inside the area.  There can only be one
1165 such area.
1166
1167 The %U (status), %R (replied) and %z (zcore) specs have to be handled
1168 with care.  For reasons of efficiency, Gnus will compute what column
1169 these characters will end up in, and \"hard-code\" that.  This means that
1170 it is illegal to have these specs after a variable-length spec.  Well,
1171 you might not be arrested, but your summary buffer will look strange,
1172 which is bad enough.
1173
1174 The smart choice is to have these specs as for to the left as
1175 possible.
1176
1177 This restriction may disappear in later versions of Gnus.")
1178
1179 (defvar gnus-summary-dummy-line-format
1180   "*  %(:                          :%) %S\n"
1181   "*The format specification for the dummy roots in the summary buffer.
1182 It works along the same lines as a normal formatting string,
1183 with some simple extensions.
1184
1185 %S  The subject")
1186
1187 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
1188   "*The format specification for the summary mode line.
1189 It works along the same lines as a normal formatting string,
1190 with some simple extensions:
1191
1192 %G  Group name
1193 %p  Unprefixed group name
1194 %A  Current article number
1195 %V  Gnus version
1196 %U  Number of unread articles in the group
1197 %e  Number of unselected articles in the group
1198 %Z  A string with unread/unselected article counts
1199 %g  Shortish group name
1200 %S  Subject of the current article
1201 %u  User-defined spec
1202 %s  Current score file name
1203 %d  Number of dormant articles
1204 %r  Number of articles that have been marked as read in this session
1205 %E  Number of articles expunged by the score files")
1206
1207 (defvar gnus-article-mode-line-format "Gnus: %%b %S"
1208   "*The format specification for the article mode line.
1209 See `gnus-summary-mode-line-format' for a closer description.")
1210
1211 (defvar gnus-group-mode-line-format "Gnus: %%b {%M:%S}"
1212   "*The format specification for the group mode line.
1213 It works along the same lines as a normal formatting string,
1214 with some simple extensions:
1215
1216 %S   The native news server.
1217 %M   The native select method.")
1218
1219 (defvar gnus-valid-select-methods
1220   '(("nntp" post address prompt-address)
1221     ("nnspool" post address)
1222     ("nnvirtual" post-mail virtual prompt-address)
1223     ("nnmbox" mail respool address)
1224     ("nnml" mail respool address)
1225     ("nnmh" mail respool address)
1226     ("nndir" post-mail prompt-address address)
1227     ("nneething" none address prompt-address)
1228     ("nndoc" none address prompt-address)
1229     ("nnbabyl" mail address respool)
1230     ("nnkiboze" post address virtual)
1231     ("nnsoup" post-mail address)
1232     ("nndraft" post-mail)
1233     ("nnfolder" mail respool address))
1234   "An alist of valid select methods.
1235 The first element of each list lists should be a string with the name
1236 of the select method.  The other elements may be be the category of
1237 this method (ie. `post', `mail', `none' or whatever) or other
1238 properties that this method has (like being respoolable).
1239 If you implement a new select method, all you should have to change is
1240 this variable.  I think.")
1241
1242 (defvar gnus-updated-mode-lines '(group article summary tree)
1243   "*List of buffers that should update their mode lines.
1244 The list may contain the symbols `group', `article' and `summary'.  If
1245 the corresponding symbol is present, Gnus will keep that mode line
1246 updated with information that may be pertinent.
1247 If this variable is nil, screen refresh may be quicker.")
1248
1249 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1250 (defvar gnus-mode-non-string-length nil
1251   "*Max length of mode-line non-string contents.
1252 If this is nil, Gnus will take space as is needed, leaving the rest
1253 of the modeline intact.")
1254
1255 ;see gnus-cus.el
1256 ;(defvar gnus-mouse-face 'highlight
1257 ;  "*Face used for mouse highlighting in Gnus.
1258 ;No mouse highlights will be done if `gnus-visual' is nil.")
1259
1260 (defvar gnus-summary-mark-below nil
1261   "*Mark all articles with a score below this variable as read.
1262 This variable is local to each summary buffer and usually set by the
1263 score file.")
1264
1265 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
1266   "*List of functions used for sorting articles in the summary buffer.
1267 This variable is only used when not using a threaded display.")
1268
1269 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1270   "*List of functions used for sorting threads in the summary buffer.
1271 By default, threads are sorted by article number.
1272
1273 Each function takes two threads and return non-nil if the first thread
1274 should be sorted before the other.  If you use more than one function,
1275 the primary sort function should be the last.  You should probably
1276 always include `gnus-thread-sort-by-number' in the list of sorting
1277 functions -- preferably first.
1278
1279 Ready-mady functions include `gnus-thread-sort-by-number',
1280 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1281 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1282 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1283
1284 (defvar gnus-thread-score-function '+
1285   "*Function used for calculating the total score of a thread.
1286
1287 The function is called with the scores of the article and each
1288 subthread and should then return the score of the thread.
1289
1290 Some functions you can use are `+', `max', or `min'.")
1291
1292 (defvar gnus-summary-expunge-below nil
1293   "All articles that have a score less than this variable will be expunged.")
1294
1295 (defvar gnus-thread-expunge-below nil
1296   "All threads that have a total score less than this variable will be expunged.
1297 See `gnus-thread-score-function' for en explanation of what a
1298 \"thread score\" is.")
1299
1300 (defvar gnus-auto-subscribed-groups
1301   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1302   "*All new groups that match this regexp will be subscribed automatically.
1303 Note that this variable only deals with new groups.  It has no effect
1304 whatsoever on old groups.")
1305
1306 (defvar gnus-options-subscribe nil
1307   "*All new groups matching this regexp will be subscribed unconditionally.
1308 Note that this variable deals only with new newsgroups.  This variable
1309 does not affect old newsgroups.")
1310
1311 (defvar gnus-options-not-subscribe nil
1312   "*All new groups matching this regexp will be ignored.
1313 Note that this variable deals only with new newsgroups.  This variable
1314 does not affect old (already subscribed) newsgroups.")
1315
1316 (defvar gnus-auto-expirable-newsgroups nil
1317   "*Groups in which to automatically mark read articles as expirable.
1318 If non-nil, this should be a regexp that should match all groups in
1319 which to perform auto-expiry.  This only makes sense for mail groups.")
1320
1321 (defvar gnus-total-expirable-newsgroups nil
1322   "*Groups in which to perform expiry of all read articles.
1323 Use with extreme caution.  All groups that match this regexp will be
1324 expiring - which means that all read articles will be deleted after
1325 (say) one week.  (This only goes for mail groups and the like, of
1326 course.)")
1327
1328 (defvar gnus-group-uncollapsed-levels 1
1329   "Number of group name elements to leave alone when making a short group name.")
1330
1331 (defvar gnus-hidden-properties '(invisible t intangible t)
1332   "Property list to use for hiding text.")
1333
1334 (defvar gnus-modtime-botch nil
1335   "*Non-nil means .newsrc should be deleted prior to save.  
1336 Its use is due to the bogus appearance that .newsrc was modified on
1337 disc.")
1338
1339 ;; Hooks.
1340
1341 (defvar gnus-group-mode-hook nil
1342   "*A hook for Gnus group mode.")
1343
1344 (defvar gnus-summary-mode-hook nil
1345   "*A hook for Gnus summary mode.
1346 This hook is run before any variables are set in the summary buffer.")
1347
1348 (defvar gnus-article-mode-hook nil
1349   "*A hook for Gnus article mode.")
1350
1351 (defvar gnus-summary-prepare-exit-hook nil
1352   "*A hook called when preparing to exit from the summary buffer.
1353 It calls `gnus-summary-expire-articles' by default.")
1354 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1355
1356 (defvar gnus-summary-exit-hook nil
1357   "*A hook called on exit from the summary buffer.")
1358
1359 (defvar gnus-group-catchup-group-hook nil
1360   "*A hook run when catching up a group from the group buffer.")
1361
1362 (defvar gnus-open-server-hook nil
1363   "*A hook called just before opening connection to the news server.")
1364
1365 (defvar gnus-load-hook nil
1366   "*A hook run while Gnus is loaded.")
1367
1368 (defvar gnus-startup-hook nil
1369   "*A hook called at startup.
1370 This hook is called after Gnus is connected to the NNTP server.")
1371
1372 (defvar gnus-get-new-news-hook nil
1373   "*A hook run just before Gnus checks for new news.")
1374
1375 (defvar gnus-after-getting-new-news-hook nil
1376   "*A hook run after Gnus checks for new news.")
1377
1378 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1379   "*A function that is called to generate the group buffer.
1380 The function is called with three arguments: The first is a number;
1381 all group with a level less or equal to that number should be listed,
1382 if the second is non-nil, empty groups should also be displayed.  If
1383 the third is non-nil, it is a number.  No groups with a level lower
1384 than this number should be displayed.
1385
1386 The only current function implemented is `gnus-group-prepare-flat'.")
1387
1388 (defvar gnus-group-prepare-hook nil
1389   "*A hook called after the group buffer has been generated.
1390 If you want to modify the group buffer, you can use this hook.")
1391
1392 (defvar gnus-summary-prepare-hook nil
1393   "*A hook called after the summary buffer has been generated.
1394 If you want to modify the summary buffer, you can use this hook.")
1395
1396 (defvar gnus-summary-generate-hook nil
1397   "*A hook run just before generating the summary buffer.
1398 This hook is commonly used to customize threading variables and the
1399 like.")
1400
1401 (defvar gnus-article-prepare-hook nil
1402   "*A hook called after an article has been prepared in the article buffer.
1403 If you want to run a special decoding program like nkf, use this hook.")
1404
1405 ;(defvar gnus-article-display-hook nil
1406 ;  "*A hook called after the article is displayed in the article buffer.
1407 ;The hook is designed to change the contents of the article
1408 ;buffer.  Typical functions that this hook may contain are
1409 ;`gnus-article-hide-headers' (hide selected headers),
1410 ;`gnus-article-maybe-highlight' (perform fancy article highlighting),
1411 ;`gnus-article-hide-signature' (hide signature) and
1412 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1413 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1414 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1415 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1416
1417 (defvar gnus-article-x-face-command
1418   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1419   "String or function to be executed to display an X-Face header.
1420 If it is a string, the command will be executed in a sub-shell
1421 asynchronously.  The compressed face will be piped to this command.")
1422
1423 (defvar gnus-article-x-face-too-ugly nil
1424   "Regexp matching posters whose face shouldn't be shown automatically.")
1425
1426 (defvar gnus-select-group-hook nil
1427   "*A hook called when a newsgroup is selected.
1428
1429 If you'd like to simplify subjects like the
1430 `gnus-summary-next-same-subject' command does, you can use the
1431 following hook:
1432
1433  (setq gnus-select-group-hook
1434       (list
1435         (lambda ()
1436           (mapcar (lambda (header)
1437                      (mail-header-set-subject
1438                       header
1439                       (gnus-simplify-subject
1440                        (mail-header-subject header) 're-only)))
1441                   gnus-newsgroup-headers))))")
1442
1443 (defvar gnus-select-article-hook nil
1444   "*A hook called when an article is selected.")
1445
1446 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1447   "*A hook called to apply kill files to a group.
1448 This hook is intended to apply a kill file to the selected newsgroup.
1449 The function `gnus-apply-kill-file' is called by default.
1450
1451 Since a general kill file is too heavy to use only for a few
1452 newsgroups, I recommend you to use a lighter hook function.  For
1453 example, if you'd like to apply a kill file to articles which contains
1454 a string `rmgroup' in subject in newsgroup `control', you can use the
1455 following hook:
1456
1457  (setq gnus-apply-kill-hook
1458       (list
1459         (lambda ()
1460           (cond ((string-match \"control\" gnus-newsgroup-name)
1461                  (gnus-kill \"Subject\" \"rmgroup\")
1462                  (gnus-expunge \"X\"))))))")
1463
1464 (defvar gnus-visual-mark-article-hook
1465   (list 'gnus-highlight-selected-summary)
1466   "*Hook run after selecting an article in the summary buffer.
1467 It is meant to be used for highlighting the article in some way.  It
1468 is not run if `gnus-visual' is nil.")
1469
1470 (defvar gnus-parse-headers-hook nil
1471   "*A hook called before parsing the headers.")
1472 (add-hook 'gnus-parse-headers-hook 'gnus-decode-rfc1522)
1473
1474 (defvar gnus-exit-group-hook nil
1475   "*A hook called when exiting (not quitting) summary mode.")
1476
1477 (defvar gnus-suspend-gnus-hook nil
1478   "*A hook called when suspending (not exiting) Gnus.")
1479
1480 (defvar gnus-exit-gnus-hook nil
1481   "*A hook called when exiting Gnus.")
1482
1483 (defvar gnus-after-exiting-gnus-hook nil
1484   "*A hook called after exiting Gnus.")
1485
1486 (defvar gnus-save-newsrc-hook nil
1487   "*A hook called before saving any of the newsrc files.")
1488
1489 (defvar gnus-save-quick-newsrc-hook nil
1490   "*A hook called just before saving the quick newsrc file.
1491 Can be used to turn version control on or off.")
1492
1493 (defvar gnus-save-standard-newsrc-hook nil
1494   "*A hook called just before saving the standard newsrc file.
1495 Can be used to turn version control on or off.")
1496
1497 (defvar gnus-summary-update-hook
1498   (list 'gnus-summary-highlight-line)
1499   "*A hook called when a summary line is changed.
1500 The hook will not be called if `gnus-visual' is nil.
1501
1502 The default function `gnus-summary-highlight-line' will
1503 highlight the line according to the `gnus-summary-highlight'
1504 variable.")
1505
1506 (defvar gnus-group-update-hook '(gnus-group-highlight-line)
1507   "*A hook called when a group line is changed.
1508 The hook will not be called if `gnus-visual' is nil.
1509
1510 The default function `gnus-group-highlight-line' will
1511 highlight the line according to the `gnus-group-highlight'
1512 variable.")
1513
1514 (defvar gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1515   "*A hook called when an article is selected for the first time.
1516 The hook is intended to mark an article as read (or unread)
1517 automatically when it is selected.")
1518
1519 (defvar gnus-group-change-level-function nil
1520   "Function run when a group level is changed.
1521 It is called with three parameters -- GROUP, LEVEL and OLDLEVEL.")
1522
1523 ;; Remove any hilit infestation.
1524 (add-hook 'gnus-startup-hook
1525           (lambda ()
1526             (remove-hook 'gnus-summary-prepare-hook
1527                          'hilit-rehighlight-buffer-quietly)
1528             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1529             (setq gnus-mark-article-hook
1530                   '(gnus-summary-mark-read-and-unread-as-read))
1531             (remove-hook 'gnus-article-prepare-hook
1532                          'hilit-rehighlight-buffer-quietly)))
1533
1534 \f
1535 ;; Internal variables
1536
1537 (defvar gnus-tree-buffer "*Tree*"
1538   "Buffer where Gnus thread trees are displayed.")
1539
1540 ;; Dummy variable.
1541 (defvar gnus-use-generic-from nil)
1542
1543 (defvar gnus-thread-indent-array nil)
1544 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1545
1546 (defvar gnus-newsrc-file-version nil)
1547
1548 (defvar gnus-method-history nil)
1549 ;; Variable holding the user answers to all method prompts.
1550
1551 (defvar gnus-group-history nil)
1552 ;; Variable holding the user answers to all group prompts.
1553
1554 (defvar gnus-server-alist nil
1555   "List of available servers.")
1556
1557 (defvar gnus-group-indentation-function nil)
1558
1559 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1560
1561 (defvar gnus-goto-missing-group-function nil)
1562
1563 (defvar gnus-override-subscribe-method nil)
1564
1565 (defvar gnus-group-goto-next-group-function nil
1566   "Function to override finding the next group after listing groups.")
1567
1568 (defconst gnus-article-mark-lists
1569   '((marked . tick) (replied . reply)
1570     (expirable . expire) (killed . killed)
1571     (bookmarks . bookmark) (dormant . dormant)
1572     (scored . score) (saved . save)
1573     (cached . cache)
1574     ))
1575
1576 ;; Avoid highlighting in kill files.
1577 (defvar gnus-summary-inhibit-highlight nil)
1578 (defvar gnus-newsgroup-selected-overlay nil)
1579
1580 (defvar gnus-inhibit-hiding nil)
1581 (defvar gnus-group-indentation "")
1582 (defvar gnus-inhibit-limiting nil)
1583 (defvar gnus-created-frames nil)
1584
1585 (defvar gnus-article-mode-map nil)
1586 (defvar gnus-dribble-buffer nil)
1587 (defvar gnus-headers-retrieved-by nil)
1588 (defvar gnus-article-reply nil)
1589 (defvar gnus-override-method nil)
1590 (defvar gnus-article-check-size nil)
1591
1592 (defvar gnus-current-score-file nil)
1593 (defvar gnus-newsgroup-adaptive-score-file nil)
1594 (defvar gnus-scores-exclude-files nil)
1595
1596 (defvar gnus-opened-servers nil)
1597
1598 (defvar gnus-current-move-group nil)
1599 (defvar gnus-current-copy-group nil)
1600 (defvar gnus-current-crosspost-group nil)
1601
1602 (defvar gnus-newsgroup-dependencies nil)
1603 (defvar gnus-newsgroup-async nil)
1604 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1605
1606 (defvar gnus-newsgroup-adaptive nil)
1607
1608 (defvar gnus-summary-display-table nil)
1609 (defvar gnus-summary-display-article-function nil)
1610
1611 (defvar gnus-summary-highlight-line-function nil
1612   "Function called after highlighting a summary line.")
1613
1614 (defvar gnus-group-line-format-alist
1615   `((?M gnus-tmp-marked-mark ?c)
1616     (?S gnus-tmp-subscribed ?c)
1617     (?L gnus-tmp-level ?d)
1618     (?N (cond ((eq number t) "*" )
1619               ((numberp number) 
1620                (int-to-string
1621                 (+ number
1622                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1623                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1624               (t number)) ?s)
1625     (?R gnus-tmp-number-of-read ?s)
1626     (?t gnus-tmp-number-total ?d)
1627     (?y gnus-tmp-number-of-unread ?s)
1628     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1629     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1630     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1631            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1632     (?g gnus-tmp-group ?s)
1633     (?G gnus-tmp-qualified-group ?s)
1634     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1635     (?D gnus-tmp-newsgroup-description ?s)
1636     (?o gnus-tmp-moderated ?c)
1637     (?O gnus-tmp-moderated-string ?s)
1638     (?p gnus-tmp-process-marked ?c)
1639     (?s gnus-tmp-news-server ?s)
1640     (?n gnus-tmp-news-method ?s)
1641     (?P gnus-group-indentation ?s)
1642     (?l gnus-tmp-grouplens ?s)
1643     (?z gnus-tmp-news-method-string ?s)
1644     (?u gnus-tmp-user-defined ?s)))
1645
1646 (defvar gnus-summary-line-format-alist
1647   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1648     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1649     (?s gnus-tmp-subject-or-nil ?s)
1650     (?n gnus-tmp-name ?s)
1651     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1652         ?s)
1653     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1654             gnus-tmp-from) ?s)
1655     (?F gnus-tmp-from ?s)
1656     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1657     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1658     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1659     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1660     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1661     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1662     (?L gnus-tmp-lines ?d)
1663     (?I gnus-tmp-indentation ?s)
1664     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1665     (?R gnus-tmp-replied ?c)
1666     (?\[ gnus-tmp-opening-bracket ?c)
1667     (?\] gnus-tmp-closing-bracket ?c)
1668     (?\> (make-string gnus-tmp-level ? ) ?s)
1669     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1670     (?i gnus-tmp-score ?d)
1671     (?z gnus-tmp-score-char ?c)
1672     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1673     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1674     (?U gnus-tmp-unread ?c)
1675     (?t (gnus-summary-number-of-articles-in-thread
1676          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1677         ?d)
1678     (?e (gnus-summary-number-of-articles-in-thread
1679          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1680         ?c)
1681     (?u gnus-tmp-user-defined ?s))
1682   "An alist of format specifications that can appear in summary lines,
1683 and what variables they correspond with, along with the type of the
1684 variable (string, integer, character, etc).")
1685
1686 (defvar gnus-summary-dummy-line-format-alist
1687   `((?S gnus-tmp-subject ?s)
1688     (?N gnus-tmp-number ?d)
1689     (?u gnus-tmp-user-defined ?s)))
1690
1691 (defvar gnus-summary-mode-line-format-alist
1692   `((?G gnus-tmp-group-name ?s)
1693     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1694     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1695     (?A gnus-tmp-article-number ?d)
1696     (?Z gnus-tmp-unread-and-unselected ?s)
1697     (?V gnus-version ?s)
1698     (?U gnus-tmp-unread ?d)
1699     (?S gnus-tmp-subject ?s)
1700     (?e gnus-tmp-unselected ?d)
1701     (?u gnus-tmp-user-defined ?s)
1702     (?d (length gnus-newsgroup-dormant) ?d)
1703     (?t (length gnus-newsgroup-marked) ?d)
1704     (?r (length gnus-newsgroup-reads) ?d)
1705     (?E gnus-newsgroup-expunged-tally ?d)
1706     (?s (gnus-current-score-file-nondirectory) ?s)))
1707
1708 (defvar gnus-article-mode-line-format-alist
1709   gnus-summary-mode-line-format-alist)
1710
1711 (defvar gnus-group-mode-line-format-alist
1712   `((?S gnus-tmp-news-server ?s)
1713     (?M gnus-tmp-news-method ?s)
1714     (?u gnus-tmp-user-defined ?s)))
1715
1716 (defvar gnus-have-read-active-file nil)
1717
1718 (defconst gnus-maintainer
1719   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1720   "The mail address of the Gnus maintainers.")
1721
1722 (defconst gnus-version "September Gnus v0.89"
1723   "Version number for this version of Gnus.")
1724
1725 (defvar gnus-info-nodes
1726   '((gnus-group-mode "(gnus)The Group Buffer")
1727     (gnus-summary-mode "(gnus)The Summary Buffer")
1728     (gnus-article-mode "(gnus)The Article Buffer"))
1729   "Assoc list of major modes and related Info nodes.")
1730
1731 (defvar gnus-group-buffer "*Group*")
1732 (defvar gnus-summary-buffer "*Summary*")
1733 (defvar gnus-article-buffer "*Article*")
1734 (defvar gnus-server-buffer "*Server*")
1735
1736 (defvar gnus-work-buffer " *gnus work*")
1737
1738 (defvar gnus-original-article-buffer " *Original Article*")
1739 (defvar gnus-original-article nil)
1740
1741 (defvar gnus-buffer-list nil
1742   "Gnus buffers that should be killed on exit.")
1743
1744 (defvar gnus-slave nil
1745   "Whether this Gnus is a slave or not.")
1746
1747 (defvar gnus-variable-list
1748   '(gnus-newsrc-options gnus-newsrc-options-n
1749     gnus-newsrc-last-checked-date
1750     gnus-newsrc-alist gnus-server-alist
1751     gnus-killed-list gnus-zombie-list
1752     gnus-topic-topology gnus-topic-alist
1753     gnus-format-specs)
1754   "Gnus variables saved in the quick startup file.")
1755
1756 (defvar gnus-newsrc-options nil
1757   "Options line in the .newsrc file.")
1758
1759 (defvar gnus-newsrc-options-n nil
1760   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1761
1762 (defvar gnus-newsrc-last-checked-date nil
1763   "Date Gnus last asked server for new newsgroups.")
1764
1765 (defvar gnus-topic-topology nil
1766   "The complete topic hierarchy.")
1767
1768 (defvar gnus-topic-alist nil
1769   "The complete topic-group alist.")
1770
1771 (defvar gnus-newsrc-alist nil
1772   "Assoc list of read articles.
1773 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1774
1775 (defvar gnus-newsrc-hashtb nil
1776   "Hashtable of gnus-newsrc-alist.")
1777
1778 (defvar gnus-killed-list nil
1779   "List of killed newsgroups.")
1780
1781 (defvar gnus-killed-hashtb nil
1782   "Hash table equivalent of gnus-killed-list.")
1783
1784 (defvar gnus-zombie-list nil
1785   "List of almost dead newsgroups.")
1786
1787 (defvar gnus-description-hashtb nil
1788   "Descriptions of newsgroups.")
1789
1790 (defvar gnus-list-of-killed-groups nil
1791   "List of newsgroups that have recently been killed by the user.")
1792
1793 (defvar gnus-active-hashtb nil
1794   "Hashtable of active articles.")
1795
1796 (defvar gnus-moderated-list nil
1797   "List of moderated newsgroups.")
1798
1799 (defvar gnus-group-marked nil)
1800
1801 (defvar gnus-current-startup-file nil
1802   "Startup file for the current host.")
1803
1804 (defvar gnus-last-search-regexp nil
1805   "Default regexp for article search command.")
1806
1807 (defvar gnus-last-shell-command nil
1808   "Default shell command on article.")
1809
1810 (defvar gnus-current-select-method nil
1811   "The current method for selecting a newsgroup.")
1812
1813 (defvar gnus-group-list-mode nil)
1814
1815 (defvar gnus-article-internal-prepare-hook nil)
1816
1817 (defvar gnus-newsgroup-name nil)
1818 (defvar gnus-newsgroup-begin nil)
1819 (defvar gnus-newsgroup-end nil)
1820 (defvar gnus-newsgroup-last-rmail nil)
1821 (defvar gnus-newsgroup-last-mail nil)
1822 (defvar gnus-newsgroup-last-folder nil)
1823 (defvar gnus-newsgroup-last-file nil)
1824 (defvar gnus-newsgroup-auto-expire nil)
1825 (defvar gnus-newsgroup-active nil)
1826
1827 (defvar gnus-newsgroup-data nil)
1828 (defvar gnus-newsgroup-data-reverse nil)
1829 (defvar gnus-newsgroup-limit nil)
1830 (defvar gnus-newsgroup-limits nil)
1831
1832 (defvar gnus-newsgroup-unreads nil
1833   "List of unread articles in the current newsgroup.")
1834
1835 (defvar gnus-newsgroup-unselected nil
1836   "List of unselected unread articles in the current newsgroup.")
1837
1838 (defvar gnus-newsgroup-reads nil
1839   "Alist of read articles and article marks in the current newsgroup.")
1840
1841 (defvar gnus-newsgroup-expunged-tally nil)
1842
1843 (defvar gnus-newsgroup-marked nil
1844   "List of ticked articles in the current newsgroup (a subset of unread art).")
1845
1846 (defvar gnus-newsgroup-killed nil
1847   "List of ranges of articles that have been through the scoring process.")
1848
1849 (defvar gnus-newsgroup-cached nil
1850   "List of articles that come from the article cache.")
1851
1852 (defvar gnus-newsgroup-saved nil
1853   "List of articles that have been saved.")
1854
1855 (defvar gnus-newsgroup-kill-headers nil)
1856
1857 (defvar gnus-newsgroup-replied nil
1858   "List of articles that have been replied to in the current newsgroup.")
1859
1860 (defvar gnus-newsgroup-expirable nil
1861   "List of articles in the current newsgroup that can be expired.")
1862
1863 (defvar gnus-newsgroup-processable nil
1864   "List of articles in the current newsgroup that can be processed.")
1865
1866 (defvar gnus-newsgroup-bookmarks nil
1867   "List of articles in the current newsgroup that have bookmarks.")
1868
1869 (defvar gnus-newsgroup-dormant nil
1870   "List of dormant articles in the current newsgroup.")
1871
1872 (defvar gnus-newsgroup-scored nil
1873   "List of scored articles in the current newsgroup.")
1874
1875 (defvar gnus-newsgroup-headers nil
1876   "List of article headers in the current newsgroup.")
1877
1878 (defvar gnus-newsgroup-threads nil)
1879
1880 (defvar gnus-newsgroup-prepared nil
1881   "Whether the current group has been prepared properly.")
1882
1883 (defvar gnus-newsgroup-ancient nil
1884   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1885
1886 (defvar gnus-newsgroup-sparse nil)
1887
1888 (defvar gnus-current-article nil)
1889 (defvar gnus-article-current nil)
1890 (defvar gnus-current-headers nil)
1891 (defvar gnus-have-all-headers nil)
1892 (defvar gnus-last-article nil)
1893 (defvar gnus-newsgroup-history nil)
1894 (defvar gnus-current-kill-article nil)
1895
1896 ;; Save window configuration.
1897 (defvar gnus-prev-winconf nil)
1898
1899 (defvar gnus-summary-mark-positions nil)
1900 (defvar gnus-group-mark-positions nil)
1901
1902 (defvar gnus-reffed-article-number nil)
1903
1904 ;;; Let the byte-compiler know that we know about this variable.
1905 (defvar rmail-default-rmail-file)
1906
1907 (defvar gnus-cache-removable-articles nil)
1908
1909 (defvar gnus-dead-summary nil)
1910
1911 (defconst gnus-summary-local-variables
1912   '(gnus-newsgroup-name
1913     gnus-newsgroup-begin gnus-newsgroup-end
1914     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1915     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1916     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1917     gnus-newsgroup-unselected gnus-newsgroup-marked
1918     gnus-newsgroup-reads gnus-newsgroup-saved
1919     gnus-newsgroup-replied gnus-newsgroup-expirable
1920     gnus-newsgroup-processable gnus-newsgroup-killed
1921     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1922     gnus-newsgroup-headers gnus-newsgroup-threads
1923     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1924     gnus-current-article gnus-current-headers gnus-have-all-headers
1925     gnus-last-article gnus-article-internal-prepare-hook
1926     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1927     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1928     gnus-newsgroup-async gnus-thread-expunge-below
1929     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1930     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1931     gnus-newsgroup-history gnus-newsgroup-ancient
1932     gnus-newsgroup-sparse
1933     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1934     gnus-newsgroup-adaptive-score-file
1935     (gnus-newsgroup-expunged-tally . 0)
1936     gnus-cache-removable-articles gnus-newsgroup-cached
1937     gnus-newsgroup-data gnus-newsgroup-data-reverse
1938     gnus-newsgroup-limit gnus-newsgroup-limits)
1939   "Variables that are buffer-local to the summary buffers.")
1940
1941 (defconst gnus-bug-message
1942   "Sending a bug report to the Gnus Towers.
1943 ========================================
1944
1945 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1946 be sent to the Gnus Bug Exterminators.
1947
1948 At the bottom of the buffer you'll see lots of variable settings.
1949 Please do not delete those.  They will tell the Bug People what your
1950 environment is, so that it will be easier to locate the bugs.
1951
1952 If you have found a bug that makes Emacs go \"beep\", set
1953 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1954 and include the backtrace in your bug report.
1955
1956 Please describe the bug in annoying, painstaking detail.
1957
1958 Thank you for your help in stamping out bugs.
1959 ")
1960
1961 ;;; End of variables.
1962
1963 ;; Define some autoload functions Gnus might use.
1964 (eval-and-compile
1965
1966   ;; This little mapcar goes through the list below and marks the
1967   ;; symbols in question as autoloaded functions.
1968   (mapcar
1969    (lambda (package)
1970      (let ((interactive (nth 1 (memq ':interactive package))))
1971        (mapcar
1972         (lambda (function)
1973           (let (keymap)
1974             (when (consp function)
1975               (setq keymap (car (memq 'keymap function)))
1976               (setq function (car function)))
1977             (autoload function (car package) nil interactive keymap)))
1978         (if (eq (nth 1 package) ':interactive)
1979             (cdddr package)
1980           (cdr package)))))
1981    '(("metamail" metamail-buffer)
1982      ("info" Info-goto-node)
1983      ("hexl" hexl-hex-string-to-integer)
1984      ("pp" pp pp-to-string pp-eval-expression)
1985      ("mail-extr" mail-extract-address-components)
1986      ("nnmail" nnmail-split-fancy nnmail-article-group)
1987      ("nnvirtual" nnvirtual-catchup-group)
1988      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
1989       timezone-make-sortable-date timezone-make-time-string)
1990      ("sendmail" mail-position-on-field mail-setup)
1991      ("rmailout" rmail-output)
1992      ("rnewspost" news-mail-other-window news-reply-yank-original
1993       news-caesar-buffer-body)
1994      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
1995       rmail-show-message)
1996      ("gnus-soup" :interactive t
1997       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
1998       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
1999      ("nnsoup" nnsoup-pack-replies)
2000      ("gnus-scomo" :interactive t gnus-score-mode)
2001      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
2002       gnus-Folder-save-name gnus-folder-save-name)
2003      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
2004      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
2005       gnus-server-make-menu-bar gnus-article-make-menu-bar
2006       gnus-browse-make-menu-bar gnus-highlight-selected-summary
2007       gnus-summary-highlight-line gnus-carpal-setup-buffer
2008       gnus-group-highlight-line
2009       gnus-article-add-button gnus-insert-next-page-button
2010       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
2011      ("gnus-vis" :interactive t
2012       gnus-article-push-button gnus-article-press-button
2013       gnus-article-highlight gnus-article-highlight-some
2014       gnus-article-highlight-headers gnus-article-highlight-signature
2015       gnus-article-add-buttons gnus-article-add-buttons-to-head
2016       gnus-article-next-button gnus-article-prev-button)
2017      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
2018       gnus-demon-add-disconnection gnus-demon-add-handler
2019       gnus-demon-remove-handler)
2020      ("gnus-demon" :interactive t
2021       gnus-demon-init gnus-demon-cancel)
2022      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
2023       gnus-tree-open gnus-tree-close)
2024      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
2025       gnus-nocem-unwanted-article-p)
2026      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2027      ("gnus-srvr" gnus-browse-foreign-server)
2028      ("gnus-cite" :interactive t
2029       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2030       gnus-article-hide-citation gnus-article-fill-cited-article
2031       gnus-article-hide-citation-in-followups)
2032      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2033       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2034       gnus-execute gnus-expunge)
2035      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2036       gnus-cache-possibly-remove-articles gnus-cache-request-article
2037       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2038       gnus-cache-enter-remove-article gnus-cached-article-p
2039       gnus-cache-open gnus-cache-close gnus-cache-update-article)
2040      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2041       gnus-cache-remove-article)
2042      ("gnus-score" :interactive t
2043       gnus-summary-increase-score gnus-summary-lower-score
2044       gnus-score-flush-cache gnus-score-close
2045       gnus-score-raise-same-subject-and-select
2046       gnus-score-raise-same-subject gnus-score-default
2047       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2048       gnus-score-lower-same-subject gnus-score-lower-thread
2049       gnus-possibly-score-headers)
2050      ("gnus-score"
2051       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2052       gnus-current-score-file-nondirectory gnus-score-adaptive
2053       gnus-score-find-trace gnus-score-file-name)
2054      ("gnus-edit" :interactive t gnus-score-customize)
2055      ("gnus-topic" :interactive t gnus-topic-mode)
2056      ("gnus-topic" gnus-topic-remove-group)
2057      ("gnus-salt" :interactive t gnus-pick-mode gnus-binary-mode)
2058      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2059      ("gnus-uu" :interactive t
2060       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2061       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2062       gnus-uu-mark-by-regexp gnus-uu-mark-all
2063       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2064       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2065       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2066       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2067       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2068       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2069       gnus-uu-decode-binhex-view)
2070      ("gnus-msg" (gnus-summary-send-map keymap)
2071       gnus-mail-yank-original gnus-mail-send-and-exit
2072       gnus-sendmail-setup-mail gnus-article-mail
2073       gnus-inews-message-id gnus-new-mail gnus-mail-reply)
2074      ("gnus-msg" :interactive t
2075       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2076       gnus-summary-followup gnus-summary-followup-with-original
2077       gnus-summary-followup-and-reply
2078       gnus-summary-followup-and-reply-with-original
2079       gnus-summary-cancel-article gnus-summary-supersede-article
2080       gnus-post-news gnus-inews-news gnus-cancel-news
2081       gnus-summary-reply gnus-summary-reply-with-original
2082       gnus-summary-mail-forward gnus-summary-mail-other-window
2083       gnus-bug)
2084      ("gnus-picon" :interactive t gnus-article-display-picons
2085       gnus-group-display-picons gnus-picons-article-display-x-face)
2086      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2087       gnus-grouplens-mode)
2088      ("gnus-vm" gnus-vm-mail-setup)
2089      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2090       gnus-summary-save-article-vm gnus-yank-article))))
2091
2092 \f
2093
2094 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2095 ;; If you want the cursor to go somewhere else, set these two
2096 ;; functions in some startup hook to whatever you want.
2097 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2098 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2099
2100 ;;; Various macros and substs.
2101
2102 (defun gnus-header-from (header)
2103   (mail-header-from header))
2104
2105 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2106   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2107   (let ((tempvar (make-symbol "GnusStartBufferWindow"))
2108         (w (make-symbol "w"))
2109         (buf (make-symbol "buf")))
2110     `(let* ((,tempvar (selected-window))
2111             (,buf ,buffer)
2112             (,w (get-buffer-window ,buf 'visible)))
2113        (unwind-protect
2114            (progn
2115              (if ,w
2116                  (select-window ,w)
2117                (pop-to-buffer ,buf))
2118              ,@forms)
2119          (select-window ,tempvar)))))
2120
2121 (defmacro gnus-gethash (string hashtable)
2122   "Get hash value of STRING in HASHTABLE."
2123   `(symbol-value (intern-soft ,string ,hashtable)))
2124
2125 (defmacro gnus-sethash (string value hashtable)
2126   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2127   `(set (intern ,string ,hashtable) ,value))
2128
2129 (defmacro gnus-intern-safe (string hashtable)
2130   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2131   `(let ((symbol (intern ,string ,hashtable)))
2132      (or (boundp symbol)
2133          (set symbol nil))
2134      symbol))
2135
2136 (defmacro gnus-group-unread (group)
2137   "Get the currently computed number of unread articles in GROUP."
2138   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2139
2140 (defmacro gnus-group-entry (group)
2141   "Get the newsrc entry for GROUP."
2142   `(gnus-gethash ,group gnus-newsrc-hashtb))
2143
2144 (defmacro gnus-active (group)
2145   "Get active info on GROUP."
2146   `(gnus-gethash ,group gnus-active-hashtb))
2147
2148 (defmacro gnus-set-active (group active)
2149   "Set GROUP's active info."
2150   `(gnus-sethash ,group ,active gnus-active-hashtb))
2151
2152 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2153 ;;   function `substring' might cut on a middle of multi-octet
2154 ;;   character.
2155 (defun gnus-truncate-string (str width)
2156   (substring str 0 width))
2157
2158 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2159 ;; to limit the length of a string.  This function is necessary since
2160 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2161 (defsubst gnus-limit-string (str width)
2162   (if (> (length str) width)
2163       (substring str 0 width)
2164     str))
2165
2166 (defsubst gnus-simplify-subject-re (subject)
2167   "Remove \"Re:\" from subject lines."
2168   (if (string-match "^[Rr][Ee]: *" subject)
2169       (substring subject (match-end 0))
2170     subject))
2171
2172 (defsubst gnus-functionp (form)
2173   "Return non-nil if FORM is funcallable."
2174   (or (and (symbolp form) (fboundp form))
2175       (and (listp form) (eq (car form) 'lambda))))
2176
2177 (defsubst gnus-goto-char (point)
2178   (and point (goto-char point)))
2179
2180 (defmacro gnus-buffer-exists-p (buffer)
2181   `(let ((buffer ,buffer))
2182      (and buffer
2183           (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
2184                    buffer))))
2185
2186 (defmacro gnus-kill-buffer (buffer)
2187   `(let ((buf ,buffer))
2188      (if (gnus-buffer-exists-p buf)
2189          (kill-buffer buf))))
2190
2191 (defsubst gnus-point-at-bol ()
2192   "Return point at the beginning of the line."
2193   (let ((p (point)))
2194     (beginning-of-line)
2195     (prog1
2196         (point)
2197       (goto-char p))))
2198
2199 (defsubst gnus-point-at-eol ()
2200   "Return point at the end of the line."
2201   (let ((p (point)))
2202     (end-of-line)
2203     (prog1
2204         (point)
2205       (goto-char p))))
2206
2207 (defun gnus-alive-p ()
2208   "Say whether Gnus is running or not."
2209   (and gnus-group-buffer
2210        (get-buffer gnus-group-buffer)))
2211
2212 ;; Delete the current line (and the next N lines.);
2213 (defmacro gnus-delete-line (&optional n)
2214   `(delete-region (progn (beginning-of-line) (point))
2215                   (progn (forward-line ,(or n 1)) (point))))
2216
2217 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2218 (defvar gnus-init-inhibit nil)
2219 (defun gnus-read-init-file (&optional inhibit-next)
2220   (if gnus-init-inhibit
2221       (setq gnus-init-inhibit nil)
2222     (setq gnus-init-inhibit inhibit-next)
2223     (and gnus-init-file
2224          (or (and (file-exists-p gnus-init-file)
2225                   ;; Don't try to load a directory.
2226                   (not (file-directory-p gnus-init-file)))
2227              (file-exists-p (concat gnus-init-file ".el"))
2228              (file-exists-p (concat gnus-init-file ".elc")))
2229          (condition-case var
2230              (load gnus-init-file nil t)
2231            (error
2232             (error "Error in %s: %s" gnus-init-file var))))))
2233
2234 ;; Info access macros.
2235
2236 (defmacro gnus-info-group (info)
2237   `(nth 0 ,info))
2238 (defmacro gnus-info-rank (info)
2239   `(nth 1 ,info))
2240 (defmacro gnus-info-read (info)
2241   `(nth 2 ,info))
2242 (defmacro gnus-info-marks (info)
2243   `(nth 3 ,info))
2244 (defmacro gnus-info-method (info)
2245   `(nth 4 ,info))
2246 (defmacro gnus-info-params (info)
2247   `(nth 5 ,info))
2248
2249 (defmacro gnus-info-level (info)
2250   `(let ((rank (gnus-info-rank ,info)))
2251      (if (consp rank)
2252          (car rank)
2253        rank)))
2254 (defmacro gnus-info-score (info)
2255   `(let ((rank (gnus-info-rank ,info)))
2256      (or (and (consp rank) (cdr rank)) 0)))
2257
2258 (defmacro gnus-info-set-group (info group)
2259   `(setcar ,info ,group))
2260 (defmacro gnus-info-set-rank (info rank)
2261   `(setcar (nthcdr 1 ,info) ,rank))
2262 (defmacro gnus-info-set-read (info read)
2263   `(setcar (nthcdr 2 ,info) ,read))
2264 (defmacro gnus-info-set-marks (info marks)
2265   `(setcar (nthcdr 3 ,info) ,marks))
2266 (defmacro gnus-info-set-method (info method)
2267   `(setcar (nthcdr 4 ,info) ,method))
2268 (defmacro gnus-info-set-params (info params)
2269   `(setcar (nthcdr 5 ,info) ,params))
2270
2271 (defmacro gnus-info-set-level (info level)
2272   `(let ((rank (cdr ,info)))
2273      (if (consp (car rank))
2274          (setcar (car rank) ,level)
2275        (setcar rank ,level))))
2276 (defmacro gnus-info-set-score (info score)
2277   `(let ((rank (cdr ,info)))
2278      (if (consp (car rank))
2279          (setcdr (car rank) ,score)
2280        (setcar rank (cons (car rank) ,score)))))
2281
2282 (defmacro gnus-get-info (group)
2283   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2284
2285 (defun gnus-byte-code (func)
2286   "Return a form that can be `eval'ed based on FUNC."
2287   (let ((fval (symbol-function func)))
2288     (if (byte-code-function-p fval)
2289         (let ((flist (append fval nil)))
2290           (setcar flist 'byte-code)
2291           flist)
2292       (cons 'progn (cddr fval)))))
2293
2294 ;;; Load the compatability functions.
2295
2296 (require 'gnus-cus)
2297 (require 'gnus-ems)
2298
2299 \f
2300 ;;;
2301 ;;; Shutdown
2302 ;;;
2303
2304 (defvar gnus-shutdown-alist nil)
2305
2306 (defun gnus-add-shutdown (function &rest symbols)
2307   "Run FUNCTION whenever one of SYMBOLS is shut down."
2308   (push (cons function symbols) gnus-shutdown-alist))
2309
2310 (defun gnus-shutdown (symbol)
2311   "Shut down everything that waits for SYMBOL."
2312   (let ((alist gnus-shutdown-alist)
2313         entry)
2314     (while (setq entry (pop alist))
2315       (when (memq symbol (cdr entry))
2316         (funcall (car entry))))))
2317
2318 \f
2319
2320 ;; Format specs.  The chunks below are the machine-generated forms
2321 ;; that are to be evaled as the result of the default format strings.
2322 ;; We write them in here to get them byte-compiled.  That way the
2323 ;; default actions will be quite fast, while still retaining the full
2324 ;; flexibility of the user-defined format specs.
2325
2326 ;; First we have lots of dummy defvars to let the compiler know these
2327 ;; are really dynamic variables.
2328
2329 (defvar gnus-tmp-unread)
2330 (defvar gnus-tmp-replied)
2331 (defvar gnus-tmp-score-char)
2332 (defvar gnus-tmp-indentation)
2333 (defvar gnus-tmp-opening-bracket)
2334 (defvar gnus-tmp-lines)
2335 (defvar gnus-tmp-name)
2336 (defvar gnus-tmp-closing-bracket)
2337 (defvar gnus-tmp-subject-or-nil)
2338 (defvar gnus-tmp-subject)
2339 (defvar gnus-tmp-marked)
2340 (defvar gnus-tmp-marked-mark)
2341 (defvar gnus-tmp-subscribed)
2342 (defvar gnus-tmp-process-marked)
2343 (defvar gnus-tmp-number-of-unread)
2344 (defvar gnus-tmp-group-name)
2345 (defvar gnus-tmp-group)
2346 (defvar gnus-tmp-article-number)
2347 (defvar gnus-tmp-unread-and-unselected)
2348 (defvar gnus-tmp-news-method)
2349 (defvar gnus-tmp-news-server)
2350 (defvar gnus-tmp-article-number)
2351 (defvar gnus-mouse-face)
2352 (defvar gnus-mouse-face-prop)
2353
2354 (defun gnus-summary-line-format-spec ()
2355   (insert gnus-tmp-unread gnus-tmp-replied
2356           gnus-tmp-score-char gnus-tmp-indentation)
2357   (gnus-put-text-property
2358    (point)
2359    (progn
2360      (insert
2361       gnus-tmp-opening-bracket
2362       (format "%4d: %-20s"
2363               gnus-tmp-lines
2364               (if (> (length gnus-tmp-name) 20)
2365                   (substring gnus-tmp-name 0 20)
2366                 gnus-tmp-name))
2367       gnus-tmp-closing-bracket)
2368      (point))
2369    gnus-mouse-face-prop gnus-mouse-face)
2370   (insert " " gnus-tmp-subject-or-nil "\n"))
2371
2372 (defvar gnus-summary-line-format-spec
2373   (gnus-byte-code 'gnus-summary-line-format-spec))
2374
2375 (defun gnus-summary-dummy-line-format-spec ()
2376   (insert "*  ")
2377   (gnus-put-text-property
2378    (point)
2379    (progn
2380      (insert ":                          :")
2381      (point))
2382    gnus-mouse-face-prop gnus-mouse-face)
2383   (insert " " gnus-tmp-subject "\n"))
2384
2385 (defvar gnus-summary-dummy-line-format-spec
2386   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2387
2388 (defun gnus-group-line-format-spec ()
2389   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2390           gnus-tmp-process-marked
2391           gnus-group-indentation
2392           (format "%5s: " gnus-tmp-number-of-unread))
2393   (gnus-put-text-property
2394    (point)
2395    (progn
2396      (insert gnus-tmp-group "\n")
2397      (1- (point)))
2398    gnus-mouse-face-prop gnus-mouse-face))
2399 (defvar gnus-group-line-format-spec
2400   (gnus-byte-code 'gnus-group-line-format-spec))
2401
2402 (defvar gnus-format-specs
2403   `((version . ,emacs-version)
2404     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2405     (summary-dummy ,gnus-summary-dummy-line-format
2406                    ,gnus-summary-dummy-line-format-spec)
2407     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2408
2409 (defvar gnus-article-mode-line-format-spec nil)
2410 (defvar gnus-summary-mode-line-format-spec nil)
2411 (defvar gnus-group-mode-line-format-spec nil)
2412
2413 ;;; Phew.  All that gruft is over, fortunately.
2414
2415 \f
2416 ;;;
2417 ;;; Gnus Utility Functions
2418 ;;;
2419
2420 (defun gnus-extract-address-components (from)
2421   (let (name address)
2422     ;; First find the address - the thing with the @ in it.  This may
2423     ;; not be accurate in mail addresses, but does the trick most of
2424     ;; the time in news messages.
2425     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2426         (setq address (substring from (match-beginning 0) (match-end 0))))
2427     ;; Then we check whether the "name <address>" format is used.
2428     (and address
2429          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2430          ;; Linear white space is not required.
2431          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2432          (and (setq name (substring from 0 (match-beginning 0)))
2433               ;; Strip any quotes from the name.
2434               (string-match "\".*\"" name)
2435               (setq name (substring name 1 (1- (match-end 0))))))
2436     ;; If not, then "address (name)" is used.
2437     (or name
2438         (and (string-match "(.+)" from)
2439              (setq name (substring from (1+ (match-beginning 0))
2440                                    (1- (match-end 0)))))
2441         (and (string-match "()" from)
2442              (setq name address))
2443         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2444         ;; XOVER might not support folded From headers.
2445         (and (string-match "(.*" from)
2446              (setq name (substring from (1+ (match-beginning 0))
2447                                    (match-end 0)))))
2448     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2449     (list (or name from) (or address from))))
2450
2451 (defun gnus-fetch-field (field)
2452   "Return the value of the header FIELD of current article."
2453   (save-excursion
2454     (save-restriction
2455       (let ((case-fold-search t)
2456             (inhibit-point-motion-hooks t))
2457         (nnheader-narrow-to-headers)
2458         (mail-fetch-field field)))))
2459
2460 (defun gnus-goto-colon ()
2461   (beginning-of-line)
2462   (search-forward ":" (gnus-point-at-eol) t))
2463
2464 ;;;###autoload
2465 (defun gnus-update-format (var)
2466   "Update the format specification near point."
2467   (interactive
2468    (list
2469     (save-excursion
2470       (eval-defun nil)
2471       ;; Find the end of the current word.
2472       (re-search-forward "[ \t\n]" nil t)
2473       ;; Search backward.
2474       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2475         (match-string 1)))))
2476   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2477                               (match-string 1 var))))
2478          (entry (assq type gnus-format-specs))
2479          value spec)
2480     (when entry
2481       (setq gnus-format-specs (delq entry gnus-format-specs)))
2482     (set
2483      (intern (format "%s-spec" var))
2484      (gnus-parse-format (setq value (symbol-value (intern var)))
2485                         (symbol-value (intern (format "%s-alist" var)))
2486                         (not (string-match "mode" var))))
2487     (setq spec (symbol-value (intern (format "%s-spec" var))))
2488     (push (list type value spec) gnus-format-specs)
2489
2490     (pop-to-buffer "*Gnus Format*")
2491     (erase-buffer)
2492     (lisp-interaction-mode)
2493     (insert (pp-to-string spec))))
2494
2495 (defun gnus-update-format-specifications (&optional force)
2496   "Update all (necessary) format specifications."
2497   ;; Make the indentation array.
2498   (gnus-make-thread-indent-array)
2499
2500   ;; See whether all the stored info needs to be flushed.
2501   (when (or force
2502             (not (equal emacs-version
2503                         (cdr (assq 'version gnus-format-specs)))))
2504     (setq gnus-format-specs nil))
2505
2506   ;; Go through all the formats and see whether they need updating.
2507   (let ((types '(summary summary-dummy group
2508                          summary-mode group-mode article-mode))
2509         new-format entry type val)
2510     (while (setq type (pop types))
2511       ;; Jump to the proper buffer to find out the value of
2512       ;; the variable, if possible.  (It may be buffer-local.)
2513       (save-excursion
2514         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2515               val)
2516           (when (and (boundp buffer)
2517                      (setq val (symbol-value buffer))
2518                      (get-buffer val)
2519                      (buffer-name (get-buffer val)))
2520             (set-buffer (get-buffer val)))
2521           (setq new-format (symbol-value
2522                             (intern (format "gnus-%s-line-format" type))))))
2523       (setq entry (cdr (assq type gnus-format-specs)))
2524       (if (and entry
2525                (equal (car entry) new-format))
2526           ;; Use the old format.
2527           (set (intern (format "gnus-%s-line-format-spec" type))
2528                (cadr entry))
2529         ;; This is a new format.
2530         (setq val
2531               (if (not (stringp new-format))
2532                   ;; This is a function call or something.
2533                   new-format
2534                 ;; This is a "real" format.
2535                 (gnus-parse-format
2536                  new-format
2537                  (symbol-value
2538                   (intern (format "gnus-%s-line-format-alist"
2539                                   (if (eq type 'article-mode)
2540                                       'summary-mode type))))
2541                  (not (string-match "mode$" (symbol-name type))))))
2542         ;; Enter the new format spec into the list.
2543         (if entry
2544             (progn
2545               (setcar (cdr entry) val)
2546               (setcar entry new-format))
2547           (push (list type new-format val) gnus-format-specs))
2548         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2549
2550   (unless (assq 'version gnus-format-specs)
2551     (push (cons 'version emacs-version) gnus-format-specs))
2552
2553   (gnus-update-group-mark-positions)
2554   (gnus-update-summary-mark-positions))
2555
2556 (defun gnus-update-summary-mark-positions ()
2557   "Compute where the summary marks are to go."
2558   (save-excursion
2559     (when (and gnus-summary-buffer
2560                (get-buffer gnus-summary-buffer)
2561                (buffer-name (get-buffer gnus-summary-buffer)))
2562       (set-buffer gnus-summary-buffer))
2563     (let ((gnus-replied-mark 129)
2564           (gnus-score-below-mark 130)
2565           (gnus-score-over-mark 130)
2566           (thread nil)
2567           (gnus-visual nil)
2568           (spec gnus-summary-line-format-spec)
2569           pos)
2570       (save-excursion
2571         (gnus-set-work-buffer)
2572         (let ((gnus-summary-line-format-spec spec))
2573           (gnus-summary-insert-line
2574            [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2575           (goto-char (point-min))
2576           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2577                                              (- (point) 2)))))
2578           (goto-char (point-min))
2579           (push (cons 'replied (and (search-forward "\201" nil t) 
2580                                     (- (point) 2)))
2581                 pos)
2582           (goto-char (point-min))
2583           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2584                 pos)))
2585       (setq gnus-summary-mark-positions pos))))
2586
2587 (defun gnus-update-group-mark-positions ()
2588   (save-excursion
2589     (let ((gnus-process-mark 128)
2590           (gnus-group-marked '("dummy.group"))
2591           (gnus-active-hashtb (make-vector 10 0)))
2592       (gnus-set-active "dummy.group" '(0 . 0))
2593       (gnus-set-work-buffer)
2594       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2595       (goto-char (point-min))
2596       (setq gnus-group-mark-positions
2597             (list (cons 'process (and (search-forward "\200" nil t)
2598                                       (- (point) 2))))))))
2599
2600 (defvar gnus-mouse-face-0 'highlight)
2601 (defvar gnus-mouse-face-1 'highlight)
2602 (defvar gnus-mouse-face-2 'highlight)
2603 (defvar gnus-mouse-face-3 'highlight)
2604 (defvar gnus-mouse-face-4 'highlight)
2605
2606 (defun gnus-mouse-face-function (form type)
2607   `(gnus-put-text-property
2608     (point) (progn ,@form (point))
2609     gnus-mouse-face-prop
2610     ,(if (equal type 0)
2611          'gnus-mouse-face
2612        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2613
2614 (defvar gnus-face-0 'bold)
2615 (defvar gnus-face-1 'italic)
2616 (defvar gnus-face-2 'bold-italic)
2617 (defvar gnus-face-3 'bold)
2618 (defvar gnus-face-4 'bold)
2619
2620 (defun gnus-face-face-function (form type)
2621   `(gnus-put-text-property
2622     (point) (progn ,@form (point))
2623     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2624
2625 (defun gnus-max-width-function (el max-width)
2626   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2627   (if (symbolp el)
2628       `(if (> (length ,el) ,max-width)
2629            (substring ,el 0 ,max-width)
2630          ,el)
2631     `(let ((val (eval ,el)))
2632        (if (numberp val)
2633            (setq val (int-to-string val)))
2634        (if (> (length val) ,max-width)
2635            (substring val 0 ,max-width)
2636          val))))
2637
2638 (defun gnus-parse-format (format spec-alist &optional insert)
2639   ;; This function parses the FORMAT string with the help of the
2640   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2641   ;; string.  If the FORMAT string contains the specifiers %( and %)
2642   ;; the text between them will have the mouse-face text property.
2643   (if (string-match
2644        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2645        format)
2646       (gnus-parse-complex-format format spec-alist)
2647     ;; This is a simple format.
2648     (gnus-parse-simple-format format spec-alist insert)))
2649
2650 (defun gnus-parse-complex-format (format spec-alist)
2651   (save-excursion
2652     (gnus-set-work-buffer)
2653     (insert format)
2654     (goto-char (point-min))
2655     (while (re-search-forward "\"" nil t)
2656       (replace-match "\\\"" nil t))
2657     (goto-char (point-min))
2658     (insert "(\"")
2659     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2660       (let ((number (if (match-beginning 1)
2661                         (match-string 1) "0"))
2662             (delim (aref (match-string 2) 0)))
2663         (if (or (= delim ?\() (= delim ?\{))
2664             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2665                                    " " number " \""))
2666           (replace-match "\")\""))))
2667     (goto-char (point-max))
2668     (insert "\")")
2669     (goto-char (point-min))
2670     (let ((form (read (current-buffer))))
2671       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2672
2673 (defun gnus-complex-form-to-spec (form spec-alist)
2674   (delq nil
2675         (mapcar
2676          (lambda (sform)
2677            (if (stringp sform)
2678                (gnus-parse-simple-format sform spec-alist t)
2679              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2680                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2681                       (nth 1 sform))))
2682          form)))
2683
2684 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2685   ;; This function parses the FORMAT string with the help of the
2686   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2687   ;; string.
2688   (let ((max-width 0)
2689         spec flist fstring newspec elem beg result dontinsert)
2690     (save-excursion
2691       (gnus-set-work-buffer)
2692       (insert format)
2693       (goto-char (point-min))
2694       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2695                                 nil t)
2696         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2697               (setq newspec "%"
2698                     beg (1+ (match-beginning 0)))
2699           ;; First check if there are any specs that look anything like
2700           ;; "%12,12A", ie. with a "max width specification".  These have
2701           ;; to be treated specially.
2702           (if (setq beg (match-beginning 1))
2703               (setq max-width
2704                     (string-to-int
2705                      (buffer-substring
2706                       (1+ (match-beginning 1)) (match-end 1))))
2707             (setq max-width 0)
2708             (setq beg (match-beginning 2)))
2709           ;; Find the specification from `spec-alist'.
2710           (unless (setq elem (cdr (assq spec spec-alist)))
2711             (setq elem '("*" ?s)))
2712           ;; Treat user defined format specifiers specially.
2713           (when (eq (car elem) 'gnus-tmp-user-defined)
2714             (setq elem
2715                   (list
2716                    (list (intern (concat "gnus-user-format-function-"
2717                                          (match-string 3)))
2718                          'gnus-tmp-header) ?s))
2719             (delete-region (match-beginning 3) (match-end 3)))
2720           (if (not (zerop max-width))
2721               (let ((el (car elem)))
2722                 (cond ((= (cadr elem) ?c)
2723                        (setq el (list 'char-to-string el)))
2724                       ((= (cadr elem) ?d)
2725                        (setq el (list 'int-to-string el))))
2726                 (setq flist (cons (gnus-max-width-function el max-width)
2727                                   flist))
2728                 (setq newspec ?s))
2729             (progn
2730               (setq flist (cons (car elem) flist))
2731               (setq newspec (cadr elem)))))
2732         ;; Remove the old specification (and possibly a ",12" string).
2733         (delete-region beg (match-end 2))
2734         ;; Insert the new specification.
2735         (goto-char beg)
2736         (insert newspec))
2737       (setq fstring (buffer-substring 1 (point-max))))
2738     ;; Do some postprocessing to increase efficiency.
2739     (setq
2740      result
2741      (cond
2742       ;; Emptyness.
2743       ((string= fstring "")
2744        nil)
2745       ;; Not a format string.
2746       ((not (string-match "%" fstring))
2747        (list fstring))
2748       ;; A format string with just a single string spec.
2749       ((string= fstring "%s")
2750        (list (car flist)))
2751       ;; A single character.
2752       ((string= fstring "%c")
2753        (list (car flist)))
2754       ;; A single number.
2755       ((string= fstring "%d")
2756        (setq dontinsert)
2757        (if insert
2758            (list `(princ ,(car flist)))
2759          (list `(int-to-string ,(car flist)))))
2760       ;; Just lots of chars and strings.
2761       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2762        (nreverse flist))
2763       ;; A single string spec at the beginning of the spec.
2764       ((string-match "\\`%[sc][^%]+\\'" fstring)
2765        (list (car flist) (substring fstring 2)))
2766       ;; A single string spec in the middle of the spec.
2767       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2768        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2769       ;; A single string spec in the end of the spec.
2770       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2771        (list (match-string 1 fstring) (car flist)))
2772       ;; A more complex spec.
2773       (t
2774        (list (cons 'format (cons fstring (nreverse flist)))))))
2775
2776     (if insert
2777         (when result
2778           (if dontinsert
2779               result
2780             (cons 'insert result)))
2781       (cond ((stringp result)
2782              result)
2783             ((consp result)
2784              (cons 'concat result))
2785             (t "")))))
2786
2787 (defun gnus-eval-format (format &optional alist props)
2788   "Eval the format variable FORMAT, using ALIST.
2789 If PROPS, insert the result."
2790   (let ((form (gnus-parse-format format alist props)))
2791     (if props
2792         (gnus-add-text-properties (point) (progn (eval form) (point)) props)
2793       (eval form))))
2794
2795 (defun gnus-remove-text-with-property (prop)
2796   "Delete all text in the current buffer with text property PROP."
2797   (save-excursion
2798     (goto-char (point-min))
2799     (while (not (eobp))
2800       (while (get-text-property (point) prop)
2801         (delete-char 1))
2802       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2803
2804 (defun gnus-set-work-buffer ()
2805   (if (get-buffer gnus-work-buffer)
2806       (progn
2807         (set-buffer gnus-work-buffer)
2808         (erase-buffer))
2809     (set-buffer (get-buffer-create gnus-work-buffer))
2810     (kill-all-local-variables)
2811     (buffer-disable-undo (current-buffer))
2812     (gnus-add-current-to-buffer-list)))
2813
2814 ;; Article file names when saving.
2815
2816 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2817   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2818 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2819 Otherwise, it is like ~/News/news/group/num."
2820   (let ((default
2821           (expand-file-name
2822            (concat (if (gnus-use-long-file-name 'not-save)
2823                        (gnus-capitalize-newsgroup newsgroup)
2824                      (gnus-newsgroup-directory-form newsgroup))
2825                    "/" (int-to-string (mail-header-number headers)))
2826            (or gnus-article-save-directory "~/News"))))
2827     (if (and last-file
2828              (string-equal (file-name-directory default)
2829                            (file-name-directory last-file))
2830              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2831         default
2832       (or last-file default))))
2833
2834 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2835   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2836 If variable `gnus-use-long-file-name' is non-nil, it is
2837 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2838   (let ((default
2839           (expand-file-name
2840            (concat (if (gnus-use-long-file-name 'not-save)
2841                        newsgroup
2842                      (gnus-newsgroup-directory-form newsgroup))
2843                    "/" (int-to-string (mail-header-number headers)))
2844            (or gnus-article-save-directory "~/News"))))
2845     (if (and last-file
2846              (string-equal (file-name-directory default)
2847                            (file-name-directory last-file))
2848              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2849         default
2850       (or last-file default))))
2851
2852 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2853   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2854 If variable `gnus-use-long-file-name' is non-nil, it is
2855 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2856   (or last-file
2857       (expand-file-name
2858        (if (gnus-use-long-file-name 'not-save)
2859            (gnus-capitalize-newsgroup newsgroup)
2860          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2861        (or gnus-article-save-directory "~/News"))))
2862
2863 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2864   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2865 If variable `gnus-use-long-file-name' is non-nil, it is
2866 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2867   (or last-file
2868       (expand-file-name
2869        (if (gnus-use-long-file-name 'not-save)
2870            newsgroup
2871          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2872        (or gnus-article-save-directory "~/News"))))
2873
2874 ;; For subscribing new newsgroup
2875
2876 (defun gnus-subscribe-hierarchical-interactive (groups)
2877   (let ((groups (sort groups 'string<))
2878         prefixes prefix start ans group starts)
2879     (while groups
2880       (setq prefixes (list "^"))
2881       (while (and groups prefixes)
2882         (while (not (string-match (car prefixes) (car groups)))
2883           (setq prefixes (cdr prefixes)))
2884         (setq prefix (car prefixes))
2885         (setq start (1- (length prefix)))
2886         (if (and (string-match "[^\\.]\\." (car groups) start)
2887                  (cdr groups)
2888                  (setq prefix
2889                        (concat "^" (substring (car groups) 0 (match-end 0))))
2890                  (string-match prefix (cadr groups)))
2891             (progn
2892               (setq prefixes (cons prefix prefixes))
2893               (message "Descend hierarchy %s? ([y]nsq): "
2894                        (substring prefix 1 (1- (length prefix))))
2895               (setq ans (read-char))
2896               (cond ((= ans ?n)
2897                      (while (and groups
2898                                  (string-match prefix
2899                                                (setq group (car groups))))
2900                        (setq gnus-killed-list
2901                              (cons group gnus-killed-list))
2902                        (gnus-sethash group group gnus-killed-hashtb)
2903                        (setq groups (cdr groups)))
2904                      (setq starts (cdr starts)))
2905                     ((= ans ?s)
2906                      (while (and groups
2907                                  (string-match prefix
2908                                                (setq group (car groups))))
2909                        (gnus-sethash group group gnus-killed-hashtb)
2910                        (gnus-subscribe-alphabetically (car groups))
2911                        (setq groups (cdr groups)))
2912                      (setq starts (cdr starts)))
2913                     ((= ans ?q)
2914                      (while groups
2915                        (setq group (car groups))
2916                        (setq gnus-killed-list (cons group gnus-killed-list))
2917                        (gnus-sethash group group gnus-killed-hashtb)
2918                        (setq groups (cdr groups))))
2919                     (t nil)))
2920           (message "Subscribe %s? ([n]yq)" (car groups))
2921           (setq ans (read-char))
2922           (setq group (car groups))
2923           (cond ((= ans ?y)
2924                  (gnus-subscribe-alphabetically (car groups))
2925                  (gnus-sethash group group gnus-killed-hashtb))
2926                 ((= ans ?q)
2927                  (while groups
2928                    (setq group (car groups))
2929                    (setq gnus-killed-list (cons group gnus-killed-list))
2930                    (gnus-sethash group group gnus-killed-hashtb)
2931                    (setq groups (cdr groups))))
2932                 (t
2933                  (setq gnus-killed-list (cons group gnus-killed-list))
2934                  (gnus-sethash group group gnus-killed-hashtb)))
2935           (setq groups (cdr groups)))))))
2936
2937 (defun gnus-subscribe-randomly (newsgroup)
2938   "Subscribe new NEWSGROUP by making it the first newsgroup."
2939   (gnus-subscribe-newsgroup newsgroup))
2940
2941 (defun gnus-subscribe-alphabetically (newgroup)
2942   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2943   (let ((groups (cdr gnus-newsrc-alist))
2944         before)
2945     (while (and (not before) groups)
2946       (if (string< newgroup (caar groups))
2947           (setq before (caar groups))
2948         (setq groups (cdr groups))))
2949     (gnus-subscribe-newsgroup newgroup before)))
2950
2951 (defun gnus-subscribe-hierarchically (newgroup)
2952   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2953   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2954   (save-excursion
2955     (set-buffer (find-file-noselect gnus-current-startup-file))
2956     (let ((groupkey newgroup)
2957           before)
2958       (while (and (not before) groupkey)
2959         (goto-char (point-min))
2960         (let ((groupkey-re
2961                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2962           (while (and (re-search-forward groupkey-re nil t)
2963                       (progn
2964                         (setq before (match-string 1))
2965                         (string< before newgroup)))))
2966         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2967         (setq groupkey
2968               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2969                   (substring groupkey (match-beginning 1) (match-end 1)))))
2970       (gnus-subscribe-newsgroup newgroup before))))
2971
2972 (defun gnus-subscribe-interactively (group)
2973   "Subscribe the new GROUP interactively.
2974 It is inserted in hierarchical newsgroup order if subscribed.  If not,
2975 it is killed."
2976   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
2977       (gnus-subscribe-hierarchically group)
2978     (push group gnus-killed-list)))
2979
2980 (defun gnus-subscribe-zombies (group)
2981   "Make the new GROUP into a zombie group."
2982   (push group gnus-zombie-list))
2983
2984 (defun gnus-subscribe-killed (group)
2985   "Make the new GROUP a killed group."
2986   (push group gnus-killed-list))
2987
2988 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2989   "Subscribe new NEWSGROUP.
2990 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
2991 the first newsgroup."
2992   ;; We subscribe the group by changing its level to `subscribed'.
2993   (gnus-group-change-level
2994    newsgroup gnus-level-default-subscribed
2995    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2996   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2997
2998 ;; For directories
2999
3000 (defun gnus-newsgroup-directory-form (newsgroup)
3001   "Make hierarchical directory name from NEWSGROUP name."
3002   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
3003         (len (length newsgroup))
3004         idx)
3005     ;; If this is a foreign group, we don't want to translate the
3006     ;; entire name.
3007     (if (setq idx (string-match ":" newsgroup))
3008         (aset newsgroup idx ?/)
3009       (setq idx 0))
3010     ;; Replace all occurrences of `.' with `/'.
3011     (while (< idx len)
3012       (if (= (aref newsgroup idx) ?.)
3013           (aset newsgroup idx ?/))
3014       (setq idx (1+ idx)))
3015     newsgroup))
3016
3017 (defun gnus-newsgroup-savable-name (group)
3018   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
3019   ;; with dots.
3020   (nnheader-replace-chars-in-string group ?/ ?.))
3021
3022 (defun gnus-make-directory (dir)
3023   "Make DIRECTORY recursively."
3024   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
3025   ;; of the many mysteries of the universe.
3026   (let* ((dir (expand-file-name dir default-directory))
3027          dirs err)
3028     (if (string-match "/$" dir)
3029         (setq dir (substring dir 0 (match-beginning 0))))
3030     ;; First go down the path until we find a directory that exists.
3031     (while (not (file-exists-p dir))
3032       (setq dirs (cons dir dirs))
3033       (string-match "/[^/]+$" dir)
3034       (setq dir (substring dir 0 (match-beginning 0))))
3035     ;; Then create all the subdirs.
3036     (while (and dirs (not err))
3037       (condition-case ()
3038           (make-directory (car dirs))
3039         (error (setq err t)))
3040       (setq dirs (cdr dirs)))
3041     ;; We return whether we were successful or not.
3042     (not dirs)))
3043
3044 (defun gnus-capitalize-newsgroup (newsgroup)
3045   "Capitalize NEWSGROUP name."
3046   (and (not (zerop (length newsgroup)))
3047        (concat (char-to-string (upcase (aref newsgroup 0)))
3048                (substring newsgroup 1))))
3049
3050 ;; Various... things.
3051
3052 (defun gnus-simplify-subject (subject &optional re-only)
3053   "Remove `Re:' and words in parentheses.
3054 If RE-ONLY is non-nil, strip leading `Re:'s only."
3055   (let ((case-fold-search t))           ;Ignore case.
3056     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
3057     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
3058       (setq subject (substring subject (match-end 0))))
3059     ;; Remove uninteresting prefixes.
3060     (if (and (not re-only)
3061              gnus-simplify-ignored-prefixes
3062              (string-match gnus-simplify-ignored-prefixes subject))
3063         (setq subject (substring subject (match-end 0))))
3064     ;; Remove words in parentheses from end.
3065     (unless re-only
3066       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3067         (setq subject (substring subject 0 (match-beginning 0)))))
3068     ;; Return subject string.
3069     subject))
3070
3071 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3072 ;; all whitespace.
3073 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3074 (defun gnus-simplify-buffer-fuzzy ()
3075   (goto-char (point-min))
3076   (while (search-forward "\t" nil t)
3077     (replace-match " " t t))
3078   (goto-char (point-min))
3079   (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *" nil t)
3080   (goto-char (match-beginning 0))
3081   (while (or
3082           (looking-at "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
3083           (looking-at "^[[].*: .*[]]$"))
3084     (goto-char (point-min))
3085     (while (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *"
3086                               nil t)
3087       (replace-match "" t t))
3088     (goto-char (point-min))
3089     (while (re-search-forward "^[[].*: .*[]]$" nil t)
3090       (goto-char (match-end 0))
3091       (delete-char -1)
3092       (delete-region
3093        (progn (goto-char (match-beginning 0)))
3094        (re-search-forward ":"))))
3095   (goto-char (point-min))
3096   (while (re-search-forward " *[[{(][^()\n]*[]})] *$" nil t)
3097     (replace-match "" t t))
3098   (goto-char (point-min))
3099   (while (re-search-forward "  +" nil t)
3100     (replace-match " " t t))
3101   (goto-char (point-min))
3102   (while (re-search-forward " $" nil t)
3103     (replace-match "" t t))
3104   (goto-char (point-min))
3105   (while (re-search-forward "^ +" nil t)
3106     (replace-match "" t t))
3107   (goto-char (point-min))
3108   (when gnus-simplify-subject-fuzzy-regexp
3109     (if (listp gnus-simplify-subject-fuzzy-regexp)
3110         (let ((list gnus-simplify-subject-fuzzy-regexp))
3111           (while list
3112             (goto-char (point-min))
3113             (while (re-search-forward (car list) nil t)
3114               (replace-match "" t t))
3115             (setq list (cdr list))))
3116       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3117         (replace-match "" t t)))))
3118
3119 (defun gnus-simplify-subject-fuzzy (subject)
3120   "Siplify a subject string fuzzily."
3121   (save-excursion
3122     (gnus-set-work-buffer)
3123     (let ((case-fold-search t))
3124       (insert subject)
3125       (inline (gnus-simplify-buffer-fuzzy))
3126       (buffer-string))))
3127
3128 ;; Add the current buffer to the list of buffers to be killed on exit.
3129 (defun gnus-add-current-to-buffer-list ()
3130   (or (memq (current-buffer) gnus-buffer-list)
3131       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3132
3133 (defun gnus-string> (s1 s2)
3134   (not (or (string< s1 s2)
3135            (string= s1 s2))))
3136
3137 (defun gnus-read-active-file-p ()
3138   "Say whether the active file has been read from `gnus-select-method'."
3139   (memq gnus-select-method gnus-have-read-active-file))
3140
3141 ;;; General various misc type functions.
3142
3143 (defun gnus-clear-system ()
3144   "Clear all variables and buffers."
3145   ;; Clear Gnus variables.
3146   (let ((variables gnus-variable-list))
3147     (while variables
3148       (set (car variables) nil)
3149       (setq variables (cdr variables))))
3150   ;; Clear other internal variables.
3151   (setq gnus-list-of-killed-groups nil
3152         gnus-have-read-active-file nil
3153         gnus-newsrc-alist nil
3154         gnus-newsrc-hashtb nil
3155         gnus-killed-list nil
3156         gnus-zombie-list nil
3157         gnus-killed-hashtb nil
3158         gnus-active-hashtb nil
3159         gnus-moderated-list nil
3160         gnus-description-hashtb nil
3161         gnus-current-headers nil
3162         gnus-thread-indent-array nil
3163         gnus-newsgroup-headers nil
3164         gnus-newsgroup-name nil
3165         gnus-server-alist nil
3166         gnus-group-list-mode nil
3167         gnus-opened-servers nil
3168         gnus-current-select-method nil)
3169   (gnus-shutdown 'gnus)
3170   ;; Kill the startup file.
3171   (and gnus-current-startup-file
3172        (get-file-buffer gnus-current-startup-file)
3173        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3174   ;; Clear the dribble buffer.
3175   (gnus-dribble-clear)
3176   ;; Kill global KILL file buffer.
3177   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3178     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3179   (gnus-kill-buffer nntp-server-buffer)
3180   ;; Kill Gnus buffers.
3181   (while gnus-buffer-list
3182     (gnus-kill-buffer (pop gnus-buffer-list)))
3183   ;; Remove Gnus frames.
3184   (while gnus-created-frames
3185     (when (frame-live-p (car gnus-created-frames))
3186       ;; We slap a condition-case around this `delete-frame' to ensure 
3187       ;; agains errors if we try do delete the single frame that's left.
3188       (condition-case ()
3189           (delete-frame (car gnus-created-frames))
3190         (error nil)))
3191     (pop gnus-created-frames)))
3192
3193 (defun gnus-windows-old-to-new (setting)
3194   ;; First we take care of the really, really old Gnus 3 actions.
3195   (when (symbolp setting)
3196     (setq setting
3197           ;; Take care of ooold GNUS 3.x values.
3198           (cond ((eq setting 'SelectArticle) 'article)
3199                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3200                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3201                 (t setting))))
3202   (if (or (listp setting)
3203           (not (and gnus-window-configuration
3204                     (memq setting '(group summary article)))))
3205       setting
3206     (let* ((setting (if (eq setting 'group)
3207                         (if (assq 'newsgroup gnus-window-configuration)
3208                             'newsgroup
3209                           'newsgroups) setting))
3210            (elem (cadr (assq setting gnus-window-configuration)))
3211            (total (apply '+ elem))
3212            (types '(group summary article))
3213            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3214            (i 0)
3215            perc
3216            out)
3217       (while (< i 3)
3218         (or (not (numberp (nth i elem)))
3219             (zerop (nth i elem))
3220             (progn
3221               (setq perc  (/ (float (nth 0 elem)) total))
3222               (setq out (cons (if (eq pbuf (nth i types))
3223                                   (vector (nth i types) perc 'point)
3224                                 (vector (nth i types) perc))
3225                               out))))
3226         (setq i (1+ i)))
3227       (list (nreverse out)))))
3228
3229 ;;;###autoload
3230 (defun gnus-add-configuration (conf)
3231   "Add the window configuration CONF to `gnus-buffer-configuration'."
3232   (setq gnus-buffer-configuration
3233         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3234                          gnus-buffer-configuration))))
3235
3236 (defvar gnus-frame-list nil)
3237
3238 (defun gnus-configure-frame (split &optional window)
3239   "Split WINDOW according to SPLIT."
3240   (unless window
3241     (setq window (get-buffer-window (current-buffer))))
3242   (select-window window)
3243   ;; This might be an old-stylee buffer config.
3244   (when (vectorp split)
3245     (setq split (append split nil)))
3246   (when (or (consp (car split))
3247             (vectorp (car split)))
3248     (push 1.0 split)
3249     (push 'vertical split))
3250   ;; The SPLIT might be something that is to be evaled to
3251   ;; return a new SPLIT.
3252   (while (and (not (assq (car split) gnus-window-to-buffer))
3253               (gnus-functionp (car split)))
3254     (setq split (eval split)))
3255   (let* ((type (car split))
3256          (subs (cddr split))
3257          (len (if (eq type 'horizontal) (window-width) (window-height)))
3258          (total 0)
3259          (window-min-width (or gnus-window-min-width window-min-width))
3260          (window-min-height (or gnus-window-min-height window-min-height))
3261          s result new-win rest comp-subs size sub)
3262     (cond
3263      ;; Nothing to do here.
3264      ((null split))
3265      ;; Don't switch buffers.
3266      ((null type)
3267       (and (memq 'point split) window))
3268      ;; This is a buffer to be selected.
3269      ((not (memq type '(frame horizontal vertical)))
3270       (let ((buffer (cond ((stringp type) type)
3271                           (t (cdr (assq type gnus-window-to-buffer)))))
3272             buf)
3273         (unless buffer
3274           (error "Illegal buffer type: %s" type))
3275         (unless (setq buf (get-buffer (if (symbolp buffer)
3276                                           (symbol-value buffer) buffer)))
3277           (setq buf (get-buffer-create (if (symbolp buffer)
3278                                            (symbol-value buffer) buffer))))
3279         (switch-to-buffer buf)
3280         ;; We return the window if it has the `point' spec.
3281         (and (memq 'point split) window)))
3282      ;; This is a frame split.
3283      ((eq type 'frame)
3284       (unless gnus-frame-list
3285         (setq gnus-frame-list (list (window-frame
3286                                      (get-buffer-window (current-buffer))))))
3287       (let ((i 0)
3288             params frame fresult)
3289         (while (< i (length subs))
3290           ;; Frame parameter is gotten from the sub-split.
3291           (setq params (cadr (elt subs i)))
3292           ;; It should be a list.
3293           (unless (listp params)
3294             (setq params nil))
3295           ;; Create a new frame?
3296           (unless (setq frame (elt gnus-frame-list i))
3297             (nconc gnus-frame-list (list (setq frame (make-frame params))))
3298             (push frame gnus-created-frames))
3299           ;; Is the old frame still alive?
3300           (unless (frame-live-p frame)
3301             (setcar (nthcdr i gnus-frame-list)
3302                     (setq frame (make-frame params))))
3303           ;; Select the frame in question and do more splits there.
3304           (select-frame frame)
3305           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3306           (incf i))
3307         ;; Select the frame that has the selected buffer.
3308         (when fresult
3309           (select-frame (window-frame fresult)))))
3310      ;; This is a normal split.
3311      (t
3312       (when (> (length subs) 0)
3313         ;; First we have to compute the sizes of all new windows.
3314         (while subs
3315           (setq sub (append (pop subs) nil))
3316           (while (and (not (assq (car sub) gnus-window-to-buffer))
3317                       (gnus-functionp (car sub)))
3318             (setq sub (eval sub)))
3319           (when sub
3320             (push sub comp-subs)
3321             (setq size (cadar comp-subs))
3322             (cond ((equal size 1.0)
3323                    (setq rest (car comp-subs))
3324                    (setq s 0))
3325                   ((floatp size)
3326                    (setq s (floor (* size len))))
3327                   ((integerp size)
3328                    (setq s size))
3329                   (t
3330                    (error "Illegal size: %s" size)))
3331             ;; Try to make sure that we are inside the safe limits.
3332             (cond ((zerop s))
3333                   ((eq type 'horizontal)
3334                    (setq s (max s window-min-width)))
3335                   ((eq type 'vertical)
3336                    (setq s (max s window-min-height))))
3337             (setcar (cdar comp-subs) s)
3338             (incf total s)))
3339         ;; Take care of the "1.0" spec.
3340         (if rest
3341             (setcar (cdr rest) (- len total))
3342           (error "No 1.0 specs in %s" split))
3343         ;; The we do the actual splitting in a nice recursive
3344         ;; fashion.
3345         (setq comp-subs (nreverse comp-subs))
3346         (while comp-subs
3347           (if (null (cdr comp-subs))
3348               (setq new-win window)
3349             (setq new-win
3350                   (split-window window (cadar comp-subs)
3351                                 (eq type 'horizontal))))
3352           (setq result (or (gnus-configure-frame
3353                             (car comp-subs) window) result))
3354           (select-window new-win)
3355           (setq window new-win)
3356           (setq comp-subs (cdr comp-subs))))
3357       ;; Return the proper window, if any.
3358       (when result
3359         (select-window result))))))
3360
3361 (defvar gnus-frame-split-p nil)
3362
3363 (defun gnus-configure-windows (setting &optional force)
3364   (setq setting (gnus-windows-old-to-new setting))
3365   (let ((split (if (symbolp setting)
3366                    (cadr (assq setting gnus-buffer-configuration))
3367                  setting))
3368         all-visible)
3369
3370     (setq gnus-frame-split-p nil)
3371
3372     (unless split
3373       (error "No such setting: %s" setting))
3374
3375     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3376              (not force))
3377         ;; All the windows mentioned are already visible, so we just
3378         ;; put point in the assigned buffer, and do not touch the
3379         ;; winconf.
3380         (select-window all-visible)
3381
3382       ;; Either remove all windows or just remove all Gnus windows.
3383       (let ((frame (selected-frame)))
3384         (unwind-protect
3385             (if gnus-use-full-window
3386                 ;; We want to remove all other windows.
3387                 (if (not gnus-frame-split-p)
3388                     ;; This is not a `frame' split, so we ignore the
3389                     ;; other frames.  
3390                     (delete-other-windows)
3391                   ;; This is a `frame' split, so we delete all windows
3392                   ;; on all frames.
3393                   (mapcar 
3394                    (lambda (frame)
3395                      (unless (eq (cdr (assq 'minibuffer
3396                                             (frame-parameters frame)))
3397                                  'only)
3398                        (select-frame frame)
3399                        (delete-other-windows)))
3400                    (frame-list)))
3401               ;; Just remove some windows.
3402               (gnus-remove-some-windows)
3403               (switch-to-buffer nntp-server-buffer))
3404           (select-frame frame)))
3405
3406       (switch-to-buffer nntp-server-buffer)
3407       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3408
3409 (defun gnus-all-windows-visible-p (split)
3410   "Say whether all buffers in SPLIT are currently visible.
3411 In particular, the value returned will be the window that
3412 should have point."
3413   (let ((stack (list split))
3414         (all-visible t)
3415         type buffer win buf)
3416     (while (and (setq split (pop stack))
3417                 all-visible)
3418       ;; Be backwards compatible.
3419       (when (vectorp split)
3420         (setq split (append split nil)))
3421       (when (or (consp (car split))
3422                 (vectorp (car split)))
3423         (push 1.0 split)
3424         (push 'vertical split))
3425       ;; The SPLIT might be something that is to be evaled to
3426       ;; return a new SPLIT.
3427       (while (and (not (assq (car split) gnus-window-to-buffer))
3428                   (gnus-functionp (car split)))
3429         (setq split (eval split)))
3430
3431       (setq type (elt split 0))
3432       (cond
3433        ;; Nothing here.
3434        ((null split) t)
3435        ;; A buffer.
3436        ((not (memq type '(horizontal vertical frame)))
3437         (setq buffer (cond ((stringp type) type)
3438                            (t (cdr (assq type gnus-window-to-buffer)))))
3439         (unless buffer
3440           (error "Illegal buffer type: %s" type))
3441         (when (setq buf (get-buffer (if (symbolp buffer)
3442                                         (symbol-value buffer)
3443                                       buffer)))
3444           (setq win (get-buffer-window buf t)))
3445         (if win
3446             (when (memq 'point split)
3447                 (setq all-visible win))
3448           (setq all-visible nil)))
3449        (t
3450         (when (eq type 'frame)
3451           (setq gnus-frame-split-p t))
3452         (setq stack (append (cddr split) stack)))))
3453     (unless (eq all-visible t)
3454       all-visible)))
3455
3456 (defun gnus-window-top-edge (&optional window)
3457   (nth 1 (window-edges window)))
3458
3459 (defun gnus-remove-some-windows ()
3460   (let ((buffers gnus-window-to-buffer)
3461         buf bufs lowest-buf lowest)
3462     (save-excursion
3463       ;; Remove windows on all known Gnus buffers.
3464       (while buffers
3465         (setq buf (cdar buffers))
3466         (if (symbolp buf)
3467             (setq buf (and (boundp buf) (symbol-value buf))))
3468         (and buf
3469              (get-buffer-window buf)
3470              (progn
3471                (setq bufs (cons buf bufs))
3472                (pop-to-buffer buf)
3473                (if (or (not lowest)
3474                        (< (gnus-window-top-edge) lowest))
3475                    (progn
3476                      (setq lowest (gnus-window-top-edge))
3477                      (setq lowest-buf buf)))))
3478         (setq buffers (cdr buffers)))
3479       ;; Remove windows on *all* summary buffers.
3480       (walk-windows
3481        (lambda (win)
3482          (let ((buf (window-buffer win)))
3483            (if (string-match    "^\\*Summary" (buffer-name buf))
3484                (progn
3485                  (setq bufs (cons buf bufs))
3486                  (pop-to-buffer buf)
3487                  (if (or (not lowest)
3488                          (< (gnus-window-top-edge) lowest))
3489                      (progn
3490                        (setq lowest-buf buf)
3491                        (setq lowest (gnus-window-top-edge)))))))))
3492       (and lowest-buf
3493            (progn
3494              (pop-to-buffer lowest-buf)
3495              (switch-to-buffer nntp-server-buffer)))
3496       (while bufs
3497         (and (not (eq (car bufs) lowest-buf))
3498              (delete-windows-on (car bufs)))
3499         (setq bufs (cdr bufs))))))
3500
3501 (defun gnus-version ()
3502   "Version numbers of this version of Gnus."
3503   (interactive)
3504   (let ((methods gnus-valid-select-methods)
3505         (mess gnus-version)
3506         meth)
3507     ;; Go through all the legal select methods and add their version
3508     ;; numbers to the total version string.  Only the backends that are
3509     ;; currently in use will have their message numbers taken into
3510     ;; consideration.
3511     (while methods
3512       (setq meth (intern (concat (caar methods) "-version")))
3513       (and (boundp meth)
3514            (stringp (symbol-value meth))
3515            (setq mess (concat mess "; " (symbol-value meth))))
3516       (setq methods (cdr methods)))
3517     (gnus-message 2 mess)))
3518
3519 (defun gnus-info-find-node ()
3520   "Find Info documentation of Gnus."
3521   (interactive)
3522   ;; Enlarge info window if needed.
3523   (let ((mode major-mode)
3524         gnus-info-buffer)
3525     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3526     (setq gnus-info-buffer (current-buffer))
3527     (gnus-configure-windows 'info)))
3528
3529 (defun gnus-days-between (date1 date2)
3530   ;; Return the number of days between date1 and date2.
3531   (- (gnus-day-number date1) (gnus-day-number date2)))
3532
3533 (defun gnus-day-number (date)
3534   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3535                      (timezone-parse-date date))))
3536     (timezone-absolute-from-gregorian
3537      (nth 1 dat) (nth 2 dat) (car dat))))
3538
3539 (defun gnus-encode-date (date)
3540   "Convert DATE to internal time."
3541   (let* ((parse (timezone-parse-date date))
3542          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3543          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3544     (encode-time (caddr time) (cadr time) (car time)
3545                  (caddr date) (cadr date) (car date) (nth 4 date))))
3546
3547 (defun gnus-time-minus (t1 t2)
3548   "Subtract two internal times."
3549   (let ((borrow (< (cadr t1) (cadr t2))))
3550     (list (- (car t1) (car t2) (if borrow 1 0))
3551           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3552
3553 (defun gnus-file-newer-than (file date)
3554   (let ((fdate (nth 5 (file-attributes file))))
3555     (or (> (car fdate) (car date))
3556         (and (= (car fdate) (car date))
3557              (> (nth 1 fdate) (nth 1 date))))))
3558
3559 (defmacro gnus-local-set-keys (&rest plist)
3560   "Set the keys in PLIST in the current keymap."
3561   `(gnus-define-keys-1 (current-local-map) ',plist))
3562
3563 (defmacro gnus-define-keys (keymap &rest plist)
3564   "Define all keys in PLIST in KEYMAP."
3565   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3566
3567 (put 'gnus-define-keys 'lisp-indent-function 1)
3568 (put 'gnus-define-keys 'lisp-indent-hook 1)
3569 (put 'gnus-define-keymap 'lisp-indent-function 1)
3570 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3571
3572 (defmacro gnus-define-keymap (keymap &rest plist)
3573   "Define all keys in PLIST in KEYMAP."
3574   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3575
3576 (defun gnus-define-keys-1 (keymap plist)
3577   (when (null keymap)
3578     (error "Can't set keys in a null keymap"))
3579   (cond ((symbolp keymap)
3580          (setq keymap (symbol-value keymap)))
3581         ((keymapp keymap))
3582         ((listp keymap)
3583          (set (car keymap) nil)
3584          (define-prefix-command (car keymap))
3585          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3586          (setq keymap (symbol-value (car keymap)))))
3587   (let (key)
3588     (while plist
3589       (when (symbolp (setq key (pop plist)))
3590         (setq key (symbol-value key)))
3591       (define-key keymap key (pop plist)))))
3592
3593 (defun gnus-group-read-only-p (&optional group)
3594   "Check whether GROUP supports editing or not.
3595 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3596 that that variable is buffer-local to the summary buffers."
3597   (let ((group (or group gnus-newsgroup-name)))
3598     (not (gnus-check-backend-function 'request-replace-article group))))
3599
3600 (defun gnus-group-total-expirable-p (group)
3601   "Check whether GROUP is total-expirable or not."
3602   (let ((params (gnus-info-params (gnus-get-info group))))
3603     (or (memq 'total-expire params)
3604         (cdr (assq 'total-expire params)) ; (total-expire . t)
3605         (and gnus-total-expirable-newsgroups ; Check var.
3606              (string-match gnus-total-expirable-newsgroups group)))))
3607
3608 (defun gnus-group-auto-expirable-p (group)
3609   "Check whether GROUP is total-expirable or not."
3610   (let ((params (gnus-info-params (gnus-get-info group))))
3611     (or (memq 'auto-expire params)
3612         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3613         (and gnus-auto-expirable-newsgroups ; Check var.
3614              (string-match gnus-auto-expirable-newsgroups group)))))
3615
3616 (defun gnus-virtual-group-p (group)
3617   "Say whether GROUP is virtual or not."
3618   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3619                         gnus-valid-select-methods)))
3620
3621 (defun gnus-news-group-p (group &optional article)
3622   "Return non-nil if GROUP (and ARTICLE) come from a news server."
3623   (or (gnus-member-of-valid 'post group) ; Ordinary news group.
3624       (and (gnus-member-of-valid 'post-mail group) ; Combined group.
3625            (eq (gnus-request-type group article) 'news))))
3626
3627 (defsubst gnus-simplify-subject-fully (subject)
3628   "Simplify a subject string according to the user's wishes."
3629   (cond
3630    ((null gnus-summary-gather-subject-limit)
3631     (gnus-simplify-subject-re subject))
3632    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3633     (gnus-simplify-subject-fuzzy subject))
3634    ((numberp gnus-summary-gather-subject-limit)
3635     (gnus-limit-string (gnus-simplify-subject-re subject)
3636                        gnus-summary-gather-subject-limit))
3637    (t
3638     subject)))
3639
3640 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3641   "Check whether two subjects are equal.  If optional argument
3642 simple-first is t, first argument is already simplified."
3643   (cond
3644    ((null simple-first)
3645     (equal (gnus-simplify-subject-fully s1)
3646            (gnus-simplify-subject-fully s2)))
3647    (t
3648     (equal s1
3649            (gnus-simplify-subject-fully s2)))))
3650
3651 ;; Returns a list of writable groups.
3652 (defun gnus-writable-groups ()
3653   (let ((alist gnus-newsrc-alist)
3654         groups group)
3655     (while (setq group (car (pop alist)))
3656       (unless (gnus-group-read-only-p group)
3657         (push group groups)))
3658     (nreverse groups)))
3659
3660 (defun gnus-completing-read (default prompt &rest args)
3661   ;; Like `completing-read', except that DEFAULT is the default argument.
3662   (let* ((prompt (if default 
3663                      (concat prompt " (default " default ") ")
3664                    (concat prompt " ")))
3665          (answer (apply 'completing-read prompt args)))
3666     (if (or (null answer) (zerop (length answer)))
3667         default
3668       answer)))
3669
3670 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3671 ;; the echo area.
3672 (defun gnus-y-or-n-p (prompt)
3673   (prog1
3674       (y-or-n-p prompt)
3675     (message "")))
3676
3677 (defun gnus-yes-or-no-p (prompt)
3678   (prog1
3679       (yes-or-no-p prompt)
3680     (message "")))
3681
3682 ;; Check whether to use long file names.
3683 (defun gnus-use-long-file-name (symbol)
3684   ;; The variable has to be set...
3685   (and gnus-use-long-file-name
3686        ;; If it isn't a list, then we return t.
3687        (or (not (listp gnus-use-long-file-name))
3688            ;; If it is a list, and the list contains `symbol', we
3689            ;; return nil.
3690            (not (memq symbol gnus-use-long-file-name)))))
3691
3692 ;; I suspect there's a better way, but I haven't taken the time to do
3693 ;; it yet. -erik selberg@cs.washington.edu
3694 (defun gnus-dd-mmm (messy-date)
3695   "Return a string like DD-MMM from a big messy string"
3696   (let ((datevec (timezone-parse-date messy-date)))
3697     (format "%2s-%s"
3698             (condition-case ()
3699                 ;; Make sure leading zeroes are stripped.
3700                 (number-to-string (string-to-number (aref datevec 2)))
3701               (error "??"))
3702             (capitalize
3703              (or (car
3704                   (nth (1- (string-to-number (aref datevec 1)))
3705                        timezone-months-assoc))
3706                  "???")))))
3707
3708 ;; Make a hash table (default and minimum size is 255).
3709 ;; Optional argument HASHSIZE specifies the table size.
3710 (defun gnus-make-hashtable (&optional hashsize)
3711   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3712
3713 ;; Make a number that is suitable for hashing; bigger than MIN and one
3714 ;; less than 2^x.
3715 (defun gnus-create-hash-size (min)
3716   (let ((i 1))
3717     (while (< i min)
3718       (setq i (* 2 i)))
3719     (1- i)))
3720
3721 ;; Show message if message has a lower level than `gnus-verbose'.
3722 ;; Guideline for numbers:
3723 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3724 ;; for things that take a long time, 7 - not very important messages
3725 ;; on stuff, 9 - messages inside loops.
3726 (defun gnus-message (level &rest args)
3727   (if (<= level gnus-verbose)
3728       (apply 'message args)
3729     ;; We have to do this format thingy here even if the result isn't
3730     ;; shown - the return value has to be the same as the return value
3731     ;; from `message'.
3732     (apply 'format args)))
3733
3734 (defun gnus-error (level &rest args)
3735   "Beep an error if `gnus-verbose' is on LEVEL or less."
3736   (when (<= (floor level) gnus-verbose)
3737     (apply 'message args)
3738     (ding)
3739     (let (duration)
3740       (when (and (floatp level)
3741                  (not (zerop (setq duration (* 10 (- level (floor level)))))))
3742         (sit-for duration))))
3743   nil)
3744
3745 ;; Generate a unique new group name.
3746 (defun gnus-generate-new-group-name (leaf)
3747   (let ((name leaf)
3748         (num 0))
3749     (while (gnus-gethash name gnus-newsrc-hashtb)
3750       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3751     name))
3752
3753 (defsubst gnus-hide-text (b e props)
3754   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
3755   (gnus-add-text-properties b e props)
3756   (when (memq 'intangible props)
3757     (gnus-put-text-property (max (1- b) (point-min))
3758                        b 'intangible (cddr (memq 'intangible props)))))
3759
3760 (defsubst gnus-unhide-text (b e)
3761   "Remove hidden text properties from region between B and E."
3762   (remove-text-properties b e gnus-hidden-properties)
3763   (when (memq 'intangible gnus-hidden-properties)
3764     (gnus-put-text-property (max (1- b) (point-min))
3765                        b 'intangible nil)))
3766
3767 (defun gnus-hide-text-type (b e type)
3768   "Hide text of TYPE between B and E."
3769   (gnus-hide-text b e (cons 'gnus-type (cons type gnus-hidden-properties))))
3770
3771 ;; Find out whether the gnus-visual TYPE is wanted.
3772 (defun gnus-visual-p (&optional type class)
3773   (and gnus-visual                      ; Has to be non-nil, at least.
3774        (if (not type)                   ; We don't care about type.
3775            gnus-visual
3776          (if (listp gnus-visual)        ; It's a list, so we check it.
3777              (or (memq type gnus-visual)
3778                  (memq class gnus-visual))
3779            t))))
3780
3781 (defun gnus-parent-id (references)
3782   "Return the last Message-ID in REFERENCES."
3783   (when (and references
3784              (string-match "\\(<[^\n<>]+>\\)[ \t\n]*\\'" references))
3785     (substring references (match-beginning 1) (match-end 1))))
3786
3787 (defun gnus-split-references (references)
3788   "Return a list of Message-IDs in REFERENCES."
3789   (let ((beg 0)
3790         ids)
3791     (while (string-match "<[^>]+>" references beg)
3792       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3793             ids))
3794     (nreverse ids)))
3795
3796 (defun gnus-buffer-live-p (buffer)
3797   "Say whether BUFFER is alive or not."
3798   (and buffer
3799        (get-buffer buffer)
3800        (buffer-name (get-buffer buffer))))
3801
3802 (defun gnus-ephemeral-group-p (group)
3803   "Say whether GROUP is ephemeral or not."
3804   (gnus-group-get-parameter group 'quit-config))
3805
3806 (defun gnus-group-quit-config (group)
3807   "Return the quit-config of GROUP."
3808   (gnus-group-get-parameter group 'quit-config))
3809
3810 (defun gnus-simplify-mode-line ()
3811   "Make mode lines a bit simpler."
3812   (setq mode-line-modified "-- ")
3813   (when (listp mode-line-format)
3814     (make-local-variable 'mode-line-format)
3815     (setq mode-line-format (copy-sequence mode-line-format))
3816     (when (equal (nth 3 mode-line-format) "   ")
3817       (setcar (nthcdr 3 mode-line-format) " "))))
3818
3819 ;;; List and range functions
3820
3821 (defun gnus-last-element (list)
3822   "Return last element of LIST."
3823   (while (cdr list)
3824     (setq list (cdr list)))
3825   (car list))
3826
3827 (defun gnus-copy-sequence (list)
3828   "Do a complete, total copy of a list."
3829   (if (and (consp list) (not (consp (cdr list))))
3830       (cons (car list) (cdr list))
3831     (mapcar (lambda (elem) (if (consp elem)
3832                                (if (consp (cdr elem))
3833                                    (gnus-copy-sequence elem)
3834                                  (cons (car elem) (cdr elem)))
3835                              elem))
3836             list)))
3837
3838 (defun gnus-set-difference (list1 list2)
3839   "Return a list of elements of LIST1 that do not appear in LIST2."
3840   (let ((list1 (copy-sequence list1)))
3841     (while list2
3842       (setq list1 (delq (car list2) list1))
3843       (setq list2 (cdr list2)))
3844     list1))
3845
3846 (defun gnus-sorted-complement (list1 list2)
3847   "Return a list of elements of LIST1 that do not appear in LIST2.
3848 Both lists have to be sorted over <."
3849   (let (out)
3850     (if (or (null list1) (null list2))
3851         (or list1 list2)
3852       (while (and list1 list2)
3853         (cond ((= (car list1) (car list2))
3854                (setq list1 (cdr list1)
3855                      list2 (cdr list2)))
3856               ((< (car list1) (car list2))
3857                (setq out (cons (car list1) out))
3858                (setq list1 (cdr list1)))
3859               (t
3860                (setq out (cons (car list2) out))
3861                (setq list2 (cdr list2)))))
3862       (nconc (nreverse out) (or list1 list2)))))
3863
3864 (defun gnus-intersection (list1 list2)
3865   (let ((result nil))
3866     (while list2
3867       (if (memq (car list2) list1)
3868           (setq result (cons (car list2) result)))
3869       (setq list2 (cdr list2)))
3870     result))
3871
3872 (defun gnus-sorted-intersection (list1 list2)
3873   ;; LIST1 and LIST2 have to be sorted over <.
3874   (let (out)
3875     (while (and list1 list2)
3876       (cond ((= (car list1) (car list2))
3877              (setq out (cons (car list1) out)
3878                    list1 (cdr list1)
3879                    list2 (cdr list2)))
3880             ((< (car list1) (car list2))
3881              (setq list1 (cdr list1)))
3882             (t
3883              (setq list2 (cdr list2)))))
3884     (nreverse out)))
3885
3886 (defun gnus-set-sorted-intersection (list1 list2)
3887   ;; LIST1 and LIST2 have to be sorted over <.
3888   ;; This function modifies LIST1.
3889   (let* ((top (cons nil list1))
3890          (prev top))
3891     (while (and list1 list2)
3892       (cond ((= (car list1) (car list2))
3893              (setq prev list1
3894                    list1 (cdr list1)
3895                    list2 (cdr list2)))
3896             ((< (car list1) (car list2))
3897              (setcdr prev (cdr list1))
3898              (setq list1 (cdr list1)))
3899             (t
3900              (setq list2 (cdr list2)))))
3901     (setcdr prev nil)
3902     (cdr top)))
3903
3904 (defun gnus-compress-sequence (numbers &optional always-list)
3905   "Convert list of numbers to a list of ranges or a single range.
3906 If ALWAYS-LIST is non-nil, this function will always release a list of
3907 ranges."
3908   (let* ((first (car numbers))
3909          (last (car numbers))
3910          result)
3911     (if (null numbers)
3912         nil
3913       (if (not (listp (cdr numbers)))
3914           numbers
3915         (while numbers
3916           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3917                 ((= (1+ last) (car numbers)) ;Still in sequence
3918                  (setq last (car numbers)))
3919                 (t                      ;End of one sequence
3920                  (setq result
3921                        (cons (if (= first last) first
3922                                (cons first last)) result))
3923                  (setq first (car numbers))
3924                  (setq last  (car numbers))))
3925           (setq numbers (cdr numbers)))
3926         (if (and (not always-list) (null result))
3927             (if (= first last) (list first) (cons first last))
3928           (nreverse (cons (if (= first last) first (cons first last))
3929                           result)))))))
3930
3931 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3932 (defun gnus-uncompress-range (ranges)
3933   "Expand a list of ranges into a list of numbers.
3934 RANGES is either a single range on the form `(num . num)' or a list of
3935 these ranges."
3936   (let (first last result)
3937     (cond
3938      ((null ranges)
3939       nil)
3940      ((not (listp (cdr ranges)))
3941       (setq first (car ranges))
3942       (setq last (cdr ranges))
3943       (while (<= first last)
3944         (setq result (cons first result))
3945         (setq first (1+ first)))
3946       (nreverse result))
3947      (t
3948       (while ranges
3949         (if (atom (car ranges))
3950             (if (numberp (car ranges))
3951                 (setq result (cons (car ranges) result)))
3952           (setq first (caar ranges))
3953           (setq last  (cdar ranges))
3954           (while (<= first last)
3955             (setq result (cons first result))
3956             (setq first (1+ first))))
3957         (setq ranges (cdr ranges)))
3958       (nreverse result)))))
3959
3960 (defun gnus-add-to-range (ranges list)
3961   "Return a list of ranges that has all articles from both RANGES and LIST.
3962 Note: LIST has to be sorted over `<'."
3963   (if (not ranges)
3964       (gnus-compress-sequence list t)
3965     (setq list (copy-sequence list))
3966     (or (listp (cdr ranges))
3967         (setq ranges (list ranges)))
3968     (let ((out ranges)
3969           ilist lowest highest temp)
3970       (while (and ranges list)
3971         (setq ilist list)
3972         (setq lowest (or (and (atom (car ranges)) (car ranges))
3973                          (caar ranges)))
3974         (while (and list (cdr list) (< (cadr list) lowest))
3975           (setq list (cdr list)))
3976         (if (< (car ilist) lowest)
3977             (progn
3978               (setq temp list)
3979               (setq list (cdr list))
3980               (setcdr temp nil)
3981               (setq out (nconc (gnus-compress-sequence ilist t) out))))
3982         (setq highest (or (and (atom (car ranges)) (car ranges))
3983                           (cdar ranges)))
3984         (while (and list (<= (car list) highest))
3985           (setq list (cdr list)))
3986         (setq ranges (cdr ranges)))
3987       (if list
3988           (setq out (nconc (gnus-compress-sequence list t) out)))
3989       (setq out (sort out (lambda (r1 r2)
3990                             (< (or (and (atom r1) r1) (car r1))
3991                                (or (and (atom r2) r2) (car r2))))))
3992       (setq ranges out)
3993       (while ranges
3994         (if (atom (car ranges))
3995             (if (cdr ranges)
3996                 (if (atom (cadr ranges))
3997                     (if (= (1+ (car ranges)) (cadr ranges))
3998                         (progn
3999                           (setcar ranges (cons (car ranges)
4000                                                (cadr ranges)))
4001                           (setcdr ranges (cddr ranges))))
4002                   (if (= (1+ (car ranges)) (caadr ranges))
4003                       (progn
4004                         (setcar (cadr ranges) (car ranges))
4005                         (setcar ranges (cadr ranges))
4006                         (setcdr ranges (cddr ranges))))))
4007           (if (cdr ranges)
4008               (if (atom (cadr ranges))
4009                   (if (= (1+ (cdar ranges)) (cadr ranges))
4010                       (progn
4011                         (setcdr (car ranges) (cadr ranges))
4012                         (setcdr ranges (cddr ranges))))
4013                 (if (= (1+ (cdar ranges)) (caadr ranges))
4014                     (progn
4015                       (setcdr (car ranges) (cdadr ranges))
4016                       (setcdr ranges (cddr ranges)))))))
4017         (setq ranges (cdr ranges)))
4018       out)))
4019
4020 (defun gnus-remove-from-range (ranges list)
4021   "Return a list of ranges that has all articles from LIST removed from RANGES.
4022 Note: LIST has to be sorted over `<'."
4023   ;; !!! This function shouldn't look like this, but I've got a headache.
4024   (gnus-compress-sequence
4025    (gnus-sorted-complement
4026     (gnus-uncompress-range ranges) list)))
4027
4028 (defun gnus-member-of-range (number ranges)
4029   (if (not (listp (cdr ranges)))
4030       (and (>= number (car ranges))
4031            (<= number (cdr ranges)))
4032     (let ((not-stop t))
4033       (while (and ranges
4034                   (if (numberp (car ranges))
4035                       (>= number (car ranges))
4036                     (>= number (caar ranges)))
4037                   not-stop)
4038         (if (if (numberp (car ranges))
4039                 (= number (car ranges))
4040               (and (>= number (caar ranges))
4041                    (<= number (cdar ranges))))
4042             (setq not-stop nil))
4043         (setq ranges (cdr ranges)))
4044       (not not-stop))))
4045
4046 (defun gnus-range-length (range)
4047   "Return the length RANGE would have if uncompressed."
4048   (length (gnus-uncompress-range range)))
4049
4050 (defun gnus-sublist-p (list sublist)
4051   "Test whether all elements in SUBLIST are members of LIST."
4052   (let ((sublistp t))
4053     (while sublist
4054       (unless (memq (pop sublist) list)
4055         (setq sublistp nil
4056               sublist nil)))
4057     sublistp))
4058
4059 \f
4060 ;;;
4061 ;;; Gnus group mode
4062 ;;;
4063
4064 (defvar gnus-group-mode-map nil)
4065 (put 'gnus-group-mode 'mode-class 'special)
4066
4067 (unless gnus-group-mode-map
4068   (setq gnus-group-mode-map (make-keymap))
4069   (suppress-keymap gnus-group-mode-map)
4070
4071   (gnus-define-keys gnus-group-mode-map
4072     " " gnus-group-read-group
4073     "=" gnus-group-select-group
4074     "\r" gnus-group-select-group
4075     "\M-\r" gnus-group-quick-select-group
4076     "j" gnus-group-jump-to-group
4077     "n" gnus-group-next-unread-group
4078     "p" gnus-group-prev-unread-group
4079     "\177" gnus-group-prev-unread-group
4080     [delete] gnus-group-prev-unread-group
4081     "N" gnus-group-next-group
4082     "P" gnus-group-prev-group
4083     "\M-n" gnus-group-next-unread-group-same-level
4084     "\M-p" gnus-group-prev-unread-group-same-level
4085     "," gnus-group-best-unread-group
4086     "." gnus-group-first-unread-group
4087     "u" gnus-group-unsubscribe-current-group
4088     "U" gnus-group-unsubscribe-group
4089     "c" gnus-group-catchup-current
4090     "C" gnus-group-catchup-current-all
4091     "l" gnus-group-list-groups
4092     "L" gnus-group-list-all-groups
4093     "m" gnus-group-mail
4094     "g" gnus-group-get-new-news
4095     "\M-g" gnus-group-get-new-news-this-group
4096     "R" gnus-group-restart
4097     "r" gnus-group-read-init-file
4098     "B" gnus-group-browse-foreign-server
4099     "b" gnus-group-check-bogus-groups
4100     "F" gnus-find-new-newsgroups
4101     "\C-c\C-d" gnus-group-describe-group
4102     "\M-d" gnus-group-describe-all-groups
4103     "\C-c\C-a" gnus-group-apropos
4104     "\C-c\M-\C-a" gnus-group-description-apropos
4105     "a" gnus-group-post-news
4106     "\ek" gnus-group-edit-local-kill
4107     "\eK" gnus-group-edit-global-kill
4108     "\C-k" gnus-group-kill-group
4109     "\C-y" gnus-group-yank-group
4110     "\C-w" gnus-group-kill-region
4111     "\C-x\C-t" gnus-group-transpose-groups
4112     "\C-c\C-l" gnus-group-list-killed
4113     "\C-c\C-x" gnus-group-expire-articles
4114     "\C-c\M-\C-x" gnus-group-expire-all-groups
4115     "V" gnus-version
4116     "s" gnus-group-save-newsrc
4117     "z" gnus-group-suspend
4118     "Z" gnus-group-clear-dribble
4119     "q" gnus-group-exit
4120     "Q" gnus-group-quit
4121     "?" gnus-group-describe-briefly
4122     "\C-c\C-i" gnus-info-find-node
4123     "\M-e" gnus-group-edit-group-method
4124     "^" gnus-group-enter-server-mode
4125     gnus-mouse-2 gnus-mouse-pick-group
4126     "<" beginning-of-buffer
4127     ">" end-of-buffer
4128     "\C-c\C-b" gnus-bug
4129     "\C-c\C-s" gnus-group-sort-groups
4130     "t" gnus-topic-mode
4131     "\C-c\M-g" gnus-activate-all-groups
4132     "\M-&" gnus-group-universal-argument
4133     "#" gnus-group-mark-group
4134     "\M-#" gnus-group-unmark-group)
4135
4136   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4137     "m" gnus-group-mark-group
4138     "u" gnus-group-unmark-group
4139     "w" gnus-group-mark-region
4140     "m" gnus-group-mark-buffer
4141     "r" gnus-group-mark-regexp
4142     "U" gnus-group-unmark-all-groups)
4143
4144   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4145     "d" gnus-group-make-directory-group
4146     "h" gnus-group-make-help-group
4147     "a" gnus-group-make-archive-group
4148     "k" gnus-group-make-kiboze-group
4149     "m" gnus-group-make-group
4150     "E" gnus-group-edit-group
4151     "e" gnus-group-edit-group-method
4152     "p" gnus-group-edit-group-parameters
4153     "v" gnus-group-add-to-virtual
4154     "V" gnus-group-make-empty-virtual
4155     "D" gnus-group-enter-directory
4156     "f" gnus-group-make-doc-group
4157     "r" gnus-group-rename-group
4158     "\177" gnus-group-delete-group
4159     [delete] gnus-group-delete-group)
4160
4161    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4162      "b" gnus-group-brew-soup
4163      "w" gnus-soup-save-areas
4164      "s" gnus-soup-send-replies
4165      "p" gnus-soup-pack-packet
4166      "r" nnsoup-pack-replies)
4167
4168    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4169      "s" gnus-group-sort-groups
4170      "a" gnus-group-sort-groups-by-alphabet
4171      "u" gnus-group-sort-groups-by-unread
4172      "l" gnus-group-sort-groups-by-level
4173      "v" gnus-group-sort-groups-by-score
4174      "r" gnus-group-sort-groups-by-rank
4175      "m" gnus-group-sort-groups-by-method)
4176
4177    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4178      "k" gnus-group-list-killed
4179      "z" gnus-group-list-zombies
4180      "s" gnus-group-list-groups
4181      "u" gnus-group-list-all-groups
4182      "A" gnus-group-list-active
4183      "a" gnus-group-apropos
4184      "d" gnus-group-description-apropos
4185      "m" gnus-group-list-matching
4186      "M" gnus-group-list-all-matching
4187      "l" gnus-group-list-level)
4188
4189    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4190      "f" gnus-score-flush-cache)
4191
4192    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4193      "f" gnus-group-fetch-faq)
4194
4195    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4196      "l" gnus-group-set-current-level
4197      "t" gnus-group-unsubscribe-current-group
4198      "s" gnus-group-unsubscribe-group
4199      "k" gnus-group-kill-group
4200      "y" gnus-group-yank-group
4201      "w" gnus-group-kill-region
4202      "\C-k" gnus-group-kill-level
4203      "z" gnus-group-kill-all-zombies))
4204
4205 (defun gnus-group-mode ()
4206   "Major mode for reading news.
4207
4208 All normal editing commands are switched off.
4209 \\<gnus-group-mode-map>
4210 The group buffer lists (some of) the groups available.  For instance,
4211 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4212 lists all zombie groups.
4213
4214 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4215 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4216
4217 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4218
4219 The following commands are available:
4220
4221 \\{gnus-group-mode-map}"
4222   (interactive)
4223   (when (and menu-bar-mode
4224              (gnus-visual-p 'group-menu 'menu))
4225     (gnus-group-make-menu-bar))
4226   (kill-all-local-variables)
4227   (gnus-simplify-mode-line)
4228   (setq major-mode 'gnus-group-mode)
4229   (setq mode-name "Group")
4230   (gnus-group-set-mode-line)
4231   (setq mode-line-process nil)
4232   (use-local-map gnus-group-mode-map)
4233   (buffer-disable-undo (current-buffer))
4234   (setq truncate-lines t)
4235   (setq buffer-read-only t)
4236   (run-hooks 'gnus-group-mode-hook))
4237
4238 (defun gnus-mouse-pick-group (e)
4239   "Enter the group under the mouse pointer."
4240   (interactive "e")
4241   (mouse-set-point e)
4242   (gnus-group-read-group nil))
4243
4244 ;; Look at LEVEL and find out what the level is really supposed to be.
4245 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4246 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4247 (defun gnus-group-default-level (&optional level number-or-nil)
4248   (cond
4249    (gnus-group-use-permanent-levels
4250     (or (setq gnus-group-use-permanent-levels
4251               (or level (if (numberp gnus-group-use-permanent-levels)
4252                             gnus-group-use-permanent-levels
4253                           (or gnus-group-default-list-level
4254                               gnus-level-subscribed))))
4255         gnus-group-default-list-level gnus-level-subscribed))
4256    (number-or-nil
4257     level)
4258    (t
4259     (or level gnus-group-default-list-level gnus-level-subscribed))))
4260
4261 ;;;###autoload
4262 (defun gnus-slave-no-server (&optional arg)
4263   "Read network news as a slave, without connecting to local server"
4264   (interactive "P")
4265   (gnus-no-server arg t))
4266
4267 ;;;###autoload
4268 (defun gnus-no-server (&optional arg slave)
4269   "Read network news.
4270 If ARG is a positive number, Gnus will use that as the
4271 startup level.  If ARG is nil, Gnus will be started at level 2.
4272 If ARG is non-nil and not a positive number, Gnus will
4273 prompt the user for the name of an NNTP server to use.
4274 As opposed to `gnus', this command will not connect to the local server."
4275   (interactive "P")
4276   (make-local-variable 'gnus-group-use-permanent-levels)
4277   (setq gnus-group-use-permanent-levels
4278         (or arg (1- gnus-level-default-subscribed)))
4279   (gnus gnus-group-use-permanent-levels t slave))
4280
4281 ;;;###autoload
4282 (defun gnus-slave (&optional arg)
4283   "Read news as a slave."
4284   (interactive "P")
4285   (gnus arg nil 'slave))
4286
4287 ;;;###autoload
4288 (defun gnus-other-frame (&optional arg)
4289   "Pop up a frame to read news."
4290   (interactive "P")
4291   (if (get-buffer gnus-group-buffer)
4292       (let ((pop-up-frames t))
4293         (gnus arg))
4294     (select-frame (make-frame))
4295     (gnus arg)))
4296
4297 ;;;###autoload
4298 (defun gnus (&optional arg dont-connect slave)
4299   "Read network news.
4300 If ARG is non-nil and a positive number, Gnus will use that as the
4301 startup level.  If ARG is non-nil and not a positive number, Gnus will
4302 prompt the user for the name of an NNTP server to use."
4303   (interactive "P")
4304
4305   (if (get-buffer gnus-group-buffer)
4306       (progn
4307         (switch-to-buffer gnus-group-buffer)
4308         (gnus-group-get-new-news))
4309
4310     (gnus-clear-system)
4311     (nnheader-init-server-buffer)
4312     (gnus-read-init-file)
4313     (setq gnus-slave slave)
4314
4315     (gnus-group-setup-buffer)
4316     (let ((buffer-read-only nil))
4317       (erase-buffer)
4318       (if (not gnus-inhibit-startup-message)
4319           (progn
4320             (gnus-group-startup-message)
4321             (sit-for 0))))
4322
4323     (let ((level (and (numberp arg) (> arg 0) arg))
4324           did-connect)
4325       (unwind-protect
4326           (progn
4327             (or dont-connect
4328                 (setq did-connect
4329                       (gnus-start-news-server (and arg (not level))))))
4330         (if (and (not dont-connect)
4331                  (not did-connect))
4332             (gnus-group-quit)
4333           (run-hooks 'gnus-startup-hook)
4334           ;; NNTP server is successfully open.
4335
4336           ;; Find the current startup file name.
4337           (setq gnus-current-startup-file
4338                 (gnus-make-newsrc-file gnus-startup-file))
4339
4340           ;; Read the dribble file.
4341           (and (or gnus-slave gnus-use-dribble-file) (gnus-dribble-read-file))
4342
4343           ;; Allow using GroupLens predictions.
4344           (when gnus-use-grouplens
4345             (bbb-login)
4346             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4347
4348           (gnus-summary-make-display-table)
4349           ;; Do the actual startup.
4350           (gnus-setup-news nil level dont-connect)
4351           ;; Generate the group buffer.
4352           (gnus-group-list-groups level)
4353           (gnus-group-first-unread-group)
4354           (gnus-configure-windows 'group)
4355           (gnus-group-set-mode-line))))))
4356
4357 (defun gnus-unload ()
4358   "Unload all Gnus features."
4359   (interactive)
4360   (or (boundp 'load-history)
4361       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4362   (let ((history load-history)
4363         feature)
4364     (while history
4365       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4366            (setq feature (cdr (assq 'provide (car history))))
4367            (unload-feature feature 'force))
4368       (setq history (cdr history)))))
4369
4370 (defun gnus-compile ()
4371   "Byte-compile the user-defined format specs."
4372   (interactive)
4373   (let ((entries gnus-format-specs)
4374         entry gnus-tmp-func)
4375     (save-excursion
4376       (gnus-message 7 "Compiling format specs...")
4377
4378       (while entries
4379         (setq entry (pop entries))
4380         (if (eq (car entry) 'version)
4381             (setq gnus-format-specs (delq entry gnus-format-specs))
4382           (when (and (listp (caddr entry))
4383                      (not (eq 'byte-code (caaddr entry))))
4384             (fset 'gnus-tmp-func
4385                   `(lambda () ,(caddr entry)))
4386             (byte-compile 'gnus-tmp-func)
4387             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4388
4389       (push (cons 'version emacs-version) gnus-format-specs)
4390
4391       (gnus-message 7 "Compiling user specs...done"))))
4392
4393 (defun gnus-indent-rigidly (start end arg)
4394   "Indent rigidly using only spaces and no tabs."
4395   (save-excursion
4396     (save-restriction
4397       (narrow-to-region start end)
4398       (indent-rigidly start end arg)
4399       (goto-char (point-min))
4400       (while (search-forward "\t" nil t)
4401         (replace-match "        " t t)))))
4402
4403 (defun gnus-group-startup-message (&optional x y)
4404   "Insert startup message in current buffer."
4405   ;; Insert the message.
4406   (erase-buffer)
4407   (insert
4408    (format "              %s
4409           _    ___ _             _
4410           _ ___ __ ___  __    _ ___
4411           __   _     ___    __  ___
4412               _           ___     _
4413              _  _ __             _
4414              ___   __            _
4415                    __           _
4416                     _      _   _
4417                    _      _    _
4418                       _  _    _
4419                   __  ___
4420                  _   _ _     _
4421                 _   _
4422               _    _
4423              _    _
4424             _
4425           __
4426
4427 "
4428            ""))
4429   ;; And then hack it.
4430   (gnus-indent-rigidly (point-min) (point-max)
4431                        (/ (max (- (window-width) (or x 46)) 0) 2))
4432   (goto-char (point-min))
4433   (forward-line 1)
4434   (let* ((pheight (count-lines (point-min) (point-max)))
4435          (wheight (window-height))
4436          (rest (- wheight pheight)))
4437     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4438   ;; Fontify some.
4439   (goto-char (point-min))
4440   (and (search-forward "Praxis" nil t)
4441        (gnus-put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4442   (goto-char (point-min))
4443   (let* ((mode-string (gnus-group-set-mode-line)))
4444     (setq mode-line-buffer-identification
4445           (list (concat gnus-version (substring (car mode-string) 4))))
4446     (set-buffer-modified-p t)))
4447
4448 (defun gnus-group-setup-buffer ()
4449   (or (get-buffer gnus-group-buffer)
4450       (progn
4451         (switch-to-buffer gnus-group-buffer)
4452         (gnus-add-current-to-buffer-list)
4453         (gnus-group-mode)
4454         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4455
4456 (defun gnus-group-list-groups (&optional level unread lowest)
4457   "List newsgroups with level LEVEL or lower that have unread articles.
4458 Default is all subscribed groups.
4459 If argument UNREAD is non-nil, groups with no unread articles are also
4460 listed."
4461   (interactive (list (if current-prefix-arg
4462                          (prefix-numeric-value current-prefix-arg)
4463                        (or
4464                         (gnus-group-default-level nil t)
4465                         gnus-group-default-list-level
4466                         gnus-level-subscribed))))
4467   (or level
4468       (setq level (car gnus-group-list-mode)
4469             unread (cdr gnus-group-list-mode)))
4470   (setq level (gnus-group-default-level level))
4471   (gnus-group-setup-buffer)             ;May call from out of group buffer
4472   (gnus-update-format-specifications)
4473   (let ((case-fold-search nil)
4474         (props (text-properties-at (gnus-point-at-bol)))
4475         (group (gnus-group-group-name)))
4476     (set-buffer gnus-group-buffer)
4477     (funcall gnus-group-prepare-function level unread lowest)
4478     (if (zerop (buffer-size))
4479         (gnus-message 5 gnus-no-groups-message)
4480       (goto-char (point-max))
4481       (when (or (not gnus-group-goto-next-group-function)
4482                 (not (funcall gnus-group-goto-next-group-function 
4483                               group props)))
4484         (if (not group)
4485             ;; Go to the first group with unread articles.
4486             (gnus-group-search-forward t)
4487           ;; Find the right group to put point on.  If the current group
4488           ;; has disappeared in the new listing, try to find the next
4489           ;; one.        If no next one can be found, just leave point at the
4490           ;; first newsgroup in the buffer.
4491           (if (not (gnus-goto-char
4492                     (text-property-any
4493                      (point-min) (point-max)
4494                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4495               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4496                 (while (and newsrc
4497                             (not (gnus-goto-char
4498                                   (text-property-any
4499                                    (point-min) (point-max) 'gnus-group
4500                                    (gnus-intern-safe
4501                                     (caar newsrc) gnus-active-hashtb)))))
4502                   (setq newsrc (cdr newsrc)))
4503                 (or newsrc (progn (goto-char (point-max))
4504                                   (forward-line -1)))))))
4505       ;; Adjust cursor point.
4506       (gnus-group-position-point))))
4507
4508 (defun gnus-group-list-level (level &optional all)
4509   "List groups on LEVEL.
4510 If ALL (the prefix), also list groups that have no unread articles."
4511   (interactive "nList groups on level: \nP")
4512   (gnus-group-list-groups level all level))
4513
4514 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4515   "List all newsgroups with unread articles of level LEVEL or lower.
4516 If ALL is non-nil, list groups that have no unread articles.
4517 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4518 If REGEXP, only list groups matching REGEXP."
4519   (set-buffer gnus-group-buffer)
4520   (let ((buffer-read-only nil)
4521         (newsrc (cdr gnus-newsrc-alist))
4522         (lowest (or lowest 1))
4523         info clevel unread group params)
4524     (erase-buffer)
4525     (if (< lowest gnus-level-zombie)
4526         ;; List living groups.
4527         (while newsrc
4528           (setq info (car newsrc)
4529                 group (gnus-info-group info)
4530                 params (gnus-info-params info)
4531                 newsrc (cdr newsrc)
4532                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4533           (and unread                   ; This group might be bogus
4534                (or (not regexp)
4535                    (string-match regexp group))
4536                (<= (setq clevel (gnus-info-level info)) level)
4537                (>= clevel lowest)
4538                (or all                  ; We list all groups?
4539                    (if (eq unread t)    ; Unactivated?
4540                        gnus-group-list-inactive-groups ; We list unactivated 
4541                      (> unread 0))      ; We list groups with unread articles
4542                    (and gnus-list-groups-with-ticked-articles
4543                         (cdr (assq 'tick (gnus-info-marks info))))
4544                                         ; And groups with tickeds
4545                    ;; Check for permanent visibility.
4546                    (and gnus-permanently-visible-groups
4547                         (string-match gnus-permanently-visible-groups
4548                                       group))
4549                    (memq 'visible params)
4550                    (cdr (assq 'visible params)))
4551                (gnus-group-insert-group-line
4552                 group (gnus-info-level info)
4553                 (gnus-info-marks info) unread (gnus-info-method info)))))
4554
4555     ;; List dead groups.
4556     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4557          (gnus-group-prepare-flat-list-dead
4558           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4559           gnus-level-zombie ?Z
4560           regexp))
4561     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4562          (gnus-group-prepare-flat-list-dead
4563           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4564           gnus-level-killed ?K regexp))
4565
4566     (gnus-group-set-mode-line)
4567     (setq gnus-group-list-mode (cons level all))
4568     (run-hooks 'gnus-group-prepare-hook)))
4569
4570 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4571   ;; List zombies and killed lists somewhat faster, which was
4572   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4573   ;; this by ignoring the group format specification altogether.
4574   (let (group)
4575     (if regexp
4576         ;; This loop is used when listing groups that match some
4577         ;; regexp.
4578         (while groups
4579           (setq group (pop groups))
4580           (when (string-match regexp group)
4581             (gnus-add-text-properties
4582              (point) (prog1 (1+ (point))
4583                        (insert " " mark "     *: " group "\n"))
4584              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4585                    'gnus-unread t
4586                    'gnus-level level))))
4587       ;; This loop is used when listing all groups.
4588       (while groups
4589         (gnus-add-text-properties
4590          (point) (prog1 (1+ (point))
4591                    (insert " " mark "     *: "
4592                            (setq group (pop groups)) "\n"))
4593          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4594                'gnus-unread t
4595                'gnus-level level))))))
4596
4597 (defmacro gnus-group-real-name (group)
4598   "Find the real name of a foreign newsgroup."
4599   `(let ((gname ,group))
4600      (if (string-match ":[^:]+$" gname)
4601          (substring gname (1+ (match-beginning 0)))
4602        gname)))
4603
4604 (defsubst gnus-server-add-address (method)
4605   (let ((method-name (symbol-name (car method))))
4606     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4607              (not (assq (intern (concat method-name "-address")) method)))
4608         (append method (list (list (intern (concat method-name "-address"))
4609                                    (nth 1 method))))
4610       method)))
4611
4612 (defsubst gnus-server-get-method (group method)
4613   ;; Input either a server name, and extended server name, or a
4614   ;; select method, and return a select method.
4615   (cond ((stringp method)
4616          (gnus-server-to-method method))
4617         ((equal method gnus-select-method)
4618          gnus-select-method)
4619         ((and (stringp (car method)) group)
4620          (gnus-server-extend-method group method))
4621         ((and method (not group)
4622               (equal (cadr method) ""))
4623          method)
4624         (t
4625          (gnus-server-add-address method))))
4626
4627 (defun gnus-server-to-method (server)
4628   "Map virtual server names to select methods."
4629   (or 
4630    ;; Perhaps this is the native server?
4631    (and (equal server "native") gnus-select-method)
4632    ;; It should be in the server alist.
4633    (cdr (assoc server gnus-server-alist))
4634    ;; If not, we look through all the opened server
4635    ;; to see whether we can find it there.
4636    (let ((opened gnus-opened-servers))
4637      (while (and opened
4638                  (not (equal server (format "%s:%s" (caaar opened)
4639                                             (cadaar opened)))))
4640        (pop opened))
4641      (caar opened))))
4642
4643 (defmacro gnus-method-equal (ss1 ss2)
4644   "Say whether two servers are equal."
4645   `(let ((s1 ,ss1)
4646          (s2 ,ss2))
4647      (or (equal s1 s2)
4648          (and (= (length s1) (length s2))
4649               (progn
4650                 (while (and s1 (member (car s1) s2))
4651                   (setq s1 (cdr s1)))
4652                 (null s1))))))
4653
4654 (defun gnus-server-equal (m1 m2)
4655   "Say whether two methods are equal."
4656   (let ((m1 (cond ((null m1) gnus-select-method)
4657                   ((stringp m1) (gnus-server-to-method m1))
4658                   (t m1)))
4659         (m2 (cond ((null m2) gnus-select-method)
4660                   ((stringp m2) (gnus-server-to-method m2))
4661                   (t m2))))
4662     (gnus-method-equal m1 m2)))
4663
4664 (defun gnus-servers-using-backend (backend)
4665   "Return a list of known servers using BACKEND."
4666   (let ((opened gnus-opened-servers)
4667         out)
4668     (while opened
4669       (when (eq backend (caaar opened))
4670         (push (caar opened) out))
4671       (pop opened))
4672     out))
4673
4674 (defun gnus-group-prefixed-name (group method)
4675   "Return the whole name from GROUP and METHOD."
4676   (and (stringp method) (setq method (gnus-server-to-method method)))
4677   (concat (format "%s" (car method))
4678           (if (and
4679                (or (assoc (format "%s" (car method)) 
4680                           (gnus-methods-using 'address))
4681                    (gnus-server-equal method gnus-message-archive-method))
4682                (nth 1 method)
4683                (not (string= (nth 1 method) "")))
4684               (concat "+" (nth 1 method)))
4685           ":" group))
4686
4687 (defun gnus-group-real-prefix (group)
4688   "Return the prefix of the current group name."
4689   (if (string-match "^[^:]+:" group)
4690       (substring group 0 (match-end 0))
4691     ""))
4692
4693 (defun gnus-group-method (group)
4694   "Return the server or method used for selecting GROUP."
4695   (let ((prefix (gnus-group-real-prefix group)))
4696     (if (equal prefix "")
4697         gnus-select-method
4698       (let ((servers gnus-opened-servers)
4699             (server "")
4700             backend possible found)
4701         (if (string-match "^[^\\+]+\\+" prefix)
4702             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4703                   server (substring prefix (match-end 0) (1- (length prefix))))
4704           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4705         (while servers
4706           (when (eq (caaar servers) backend)
4707             (setq possible (caar servers))
4708             (when (equal (cadaar servers) server)
4709               (setq found (caar servers))))
4710           (pop servers))
4711         (or (car (rassoc found gnus-server-alist))
4712             found
4713             (car (rassoc possible gnus-server-alist))
4714             possible
4715             (list backend server))))))
4716
4717 (defsubst gnus-secondary-method-p (method)
4718   "Return whether METHOD is a secondary select method."
4719   (let ((methods gnus-secondary-select-methods)
4720         (gmethod (gnus-server-get-method nil method)))
4721     (while (and methods
4722                 (not (equal (gnus-server-get-method nil (car methods))
4723                             gmethod)))
4724       (setq methods (cdr methods)))
4725     methods))
4726
4727 (defun gnus-group-foreign-p (group)
4728   "Say whether a group is foreign or not."
4729   (and (not (gnus-group-native-p group))
4730        (not (gnus-group-secondary-p group))))
4731
4732 (defun gnus-group-native-p (group)
4733   "Say whether the group is native or not."
4734   (not (string-match ":" group)))
4735
4736 (defun gnus-group-secondary-p (group)
4737   "Say whether the group is secondary or not."
4738   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4739
4740 (defun gnus-group-get-parameter (group &optional symbol)
4741   "Returns the group parameters for GROUP.
4742 If SYMBOL, return the value of that symbol in the group parameters."
4743   (let ((params (gnus-info-params (gnus-get-info group))))
4744     (if symbol
4745         (gnus-group-parameter-value params symbol)
4746       params)))
4747
4748 (defun gnus-group-parameter-value (params symbol)
4749   "Return the value of SYMBOL in group PARAMS."
4750   (or (car (memq symbol params))        ; It's either a simple symbol
4751       (cdr (assq symbol params))))      ; or a cons.
4752
4753 (defun gnus-group-add-parameter (group param)
4754   "Add parameter PARAM to GROUP."
4755   (let ((info (gnus-get-info group)))
4756     (if (not info)
4757         () ; This is a dead group.  We just ignore it.
4758       ;; Cons the new param to the old one and update.
4759       (gnus-group-set-info (cons param (gnus-info-params info))
4760                            group 'params))))
4761
4762 (defun gnus-group-set-parameter (group name value)
4763   "Set parameter NAME to VALUE in GROUP."
4764   (let ((info (gnus-get-info group)))
4765     (if (not info)
4766         () ; This is a dead group.  We just ignore it.
4767       (let ((old-params (gnus-info-params info))
4768             (new-params (list (cons name value))))
4769         (while old-params
4770           (if (or (not (listp (car old-params)))
4771                   (not (eq (caar old-params) name)))
4772               (setq new-params (append new-params (list (car old-params)))))
4773           (setq old-params (cdr old-params)))
4774         (gnus-group-set-info new-params group 'params)))))
4775
4776 (defun gnus-group-add-score (group &optional score)
4777   "Add SCORE to the GROUP score.
4778 If SCORE is nil, add 1 to the score of GROUP."
4779   (let ((info (gnus-get-info group)))
4780     (when info
4781       (gnus-info-set-score info (+ (gnus-info-score info) (or score 1))))))
4782
4783 (defun gnus-summary-bubble-group ()
4784   "Increase the score of the current group.
4785 This is a handy function to add to `gnus-summary-exit-hook' to
4786 increase the score of each group you read."
4787   (gnus-group-add-score gnus-newsgroup-name))
4788
4789 (defun gnus-group-set-info (info &optional method-only-group part)
4790   (let* ((entry (gnus-gethash
4791                  (or method-only-group (gnus-info-group info))
4792                  gnus-newsrc-hashtb))
4793          (part-info info)
4794          (info (if method-only-group (nth 2 entry) info))
4795          method)
4796     (when method-only-group
4797       (unless entry
4798         (error "Trying to change non-existent group %s" method-only-group))
4799       ;; We have received parts of the actual group info - either the
4800       ;; select method or the group parameters.  We first check
4801       ;; whether we have to extend the info, and if so, do that.
4802       (let ((len (length info))
4803             (total (if (eq part 'method) 5 6)))
4804         (when (< len total)
4805           (setcdr (nthcdr (1- len) info)
4806                   (make-list (- total len) nil)))
4807         ;; Then we enter the new info.
4808         (setcar (nthcdr (1- total) info) part-info)))
4809     (unless entry
4810       ;; This is a new group, so we just create it.
4811       (save-excursion
4812         (set-buffer gnus-group-buffer)
4813         (setq method (gnus-info-method info))
4814         (when (gnus-server-equal method "native")
4815           (setq method nil))
4816         (save-excursion
4817           (set-buffer gnus-group-buffer)
4818           (if method
4819               ;; It's a foreign group...
4820               (gnus-group-make-group
4821                (gnus-group-real-name (gnus-info-group info))
4822                (if (stringp method) method
4823                  (prin1-to-string (car method)))
4824                (and (consp method)
4825                     (nth 1 (gnus-info-method info))))
4826             ;; It's a native group.
4827             (gnus-group-make-group (gnus-info-group info))))
4828         (gnus-message 6 "Note: New group created")
4829         (setq entry
4830               (gnus-gethash (gnus-group-prefixed-name
4831                              (gnus-group-real-name (gnus-info-group info))
4832                              (or (gnus-info-method info) gnus-select-method))
4833                             gnus-newsrc-hashtb))))
4834     ;; Whether it was a new group or not, we now have the entry, so we
4835     ;; can do the update.
4836     (if entry
4837         (progn
4838           (setcar (nthcdr 2 entry) info)
4839           (when (and (not (eq (car entry) t))
4840                      (gnus-active (gnus-info-group info)))
4841             (setcar entry (length (gnus-list-of-unread-articles (car info))))))
4842       (error "No such group: %s" (gnus-info-group info)))))
4843
4844 (defun gnus-group-set-method-info (group select-method)
4845   (gnus-group-set-info select-method group 'method))
4846
4847 (defun gnus-group-set-params-info (group params)
4848   (gnus-group-set-info params group 'params))
4849
4850 (defun gnus-group-update-group-line ()
4851   "Update the current line in the group buffer."
4852   (let* ((buffer-read-only nil)
4853          (group (gnus-group-group-name))
4854          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
4855     (and entry
4856          (not (gnus-ephemeral-group-p group))
4857          (gnus-dribble-enter
4858           (concat "(gnus-group-set-info '"
4859                   (prin1-to-string (nth 2 entry)) ")")))
4860     (gnus-delete-line)
4861     (gnus-group-insert-group-line-info group)
4862     (forward-line -1)
4863     (gnus-group-position-point)))
4864
4865 (defun gnus-group-insert-group-line-info (group)
4866   "Insert GROUP on the current line."
4867   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4868         active info)
4869     (if entry
4870         (progn
4871           ;; (Un)subscribed group.
4872           (setq info (nth 2 entry))
4873           (gnus-group-insert-group-line
4874            group (gnus-info-level info) (gnus-info-marks info)
4875            (or (car entry) t) (gnus-info-method info)))
4876       ;; This group is dead.
4877       (gnus-group-insert-group-line
4878        group
4879        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4880        nil
4881        (if (setq active (gnus-active group))
4882            (- (1+ (cdr active)) (car active)) 0)
4883        nil))))
4884
4885 (defun gnus-group-insert-group-line (gnus-tmp-group gnus-tmp-level 
4886                                                     gnus-tmp-marked number
4887                                                     gnus-tmp-method)
4888   "Insert a group line in the group buffer."
4889   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4890          (gnus-tmp-number-total
4891           (if gnus-tmp-active
4892               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4893             0))
4894          (gnus-tmp-number-of-unread
4895           (if (numberp number) (int-to-string (max 0 number))
4896             "*"))
4897          (gnus-tmp-number-of-read
4898           (if (numberp number)
4899               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4900             "*"))
4901          (gnus-tmp-subscribed
4902           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4903                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4904                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4905                 (t ?K)))
4906          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4907          (gnus-tmp-newsgroup-description
4908           (if gnus-description-hashtb
4909               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4910             ""))
4911          (gnus-tmp-moderated
4912           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4913          (gnus-tmp-moderated-string
4914           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4915          (gnus-tmp-method
4916           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4917          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
4918          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4919          (gnus-tmp-news-method-string
4920           (if gnus-tmp-method
4921               (format "(%s:%s)" (car gnus-tmp-method)
4922                       (cadr gnus-tmp-method)) ""))
4923          (gnus-tmp-marked-mark
4924           (if (and (numberp number)
4925                    (zerop number)
4926                    (cdr (assq 'tick gnus-tmp-marked)))
4927               ?* ? ))
4928          (gnus-tmp-process-marked
4929           (if (member gnus-tmp-group gnus-group-marked)
4930               gnus-process-mark ? ))
4931          (gnus-tmp-grouplens
4932           (or (and gnus-use-grouplens
4933                    (bbb-grouplens-group-p gnus-tmp-group))
4934               ""))
4935          (buffer-read-only nil)
4936          header gnus-tmp-header)        ; passed as parameter to user-funcs.
4937     (beginning-of-line)
4938     (gnus-add-text-properties
4939      (point)
4940      (prog1 (1+ (point))
4941        ;; Insert the text.
4942        (eval gnus-group-line-format-spec))
4943      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
4944        gnus-unread ,(if (numberp number)
4945                         (string-to-int gnus-tmp-number-of-unread)
4946                       t)
4947        gnus-marked ,gnus-tmp-marked-mark
4948        gnus-indentation ,gnus-group-indentation
4949        gnus-level ,gnus-tmp-level))
4950     (when (inline (gnus-visual-p 'group-highlight 'highlight))
4951       (forward-line -1)
4952       (run-hooks 'gnus-group-update-hook)
4953       (forward-line))
4954     ;; Allow XEmacs to remove front-sticky text properties.
4955     (gnus-group-remove-excess-properties)))
4956
4957 (defun gnus-group-update-group (group &optional visible-only)
4958   "Update all lines where GROUP appear.
4959 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
4960 already."
4961   (save-excursion
4962     (set-buffer gnus-group-buffer)
4963     ;; The buffer may be narrowed.
4964     (save-restriction
4965       (widen)
4966       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
4967             (loc (point-min))
4968             found buffer-read-only)
4969         ;; Enter the current status into the dribble buffer.
4970         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
4971           (if (and entry (not (gnus-ephemeral-group-p group)))
4972               (gnus-dribble-enter
4973                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
4974                        ")"))))
4975         ;; Find all group instances.  If topics are in use, each group
4976         ;; may be listed in more than once.
4977         (while (setq loc (text-property-any
4978                           loc (point-max) 'gnus-group ident))
4979           (setq found t)
4980           (goto-char loc)
4981           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4982             (gnus-delete-line)
4983             (gnus-group-insert-group-line-info group))
4984           (setq loc (1+ loc)))
4985         (unless (or found visible-only)
4986           ;; No such line in the buffer, find out where it's supposed to
4987           ;; go, and insert it there (or at the end of the buffer).
4988           (if gnus-goto-missing-group-function
4989               (funcall gnus-goto-missing-group-function group)
4990             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
4991               (while (and entry (car entry)
4992                           (not
4993                            (gnus-goto-char
4994                             (text-property-any
4995                              (point-min) (point-max)
4996                              'gnus-group (gnus-intern-safe
4997                                           (caar entry) gnus-active-hashtb)))))
4998                 (setq entry (cdr entry)))
4999               (or entry (goto-char (point-max)))))
5000           ;; Finally insert the line.
5001           (let ((gnus-group-indentation (gnus-group-group-indentation)))
5002             (gnus-group-insert-group-line-info group)))
5003         (gnus-group-set-mode-line)))))
5004
5005 (defun gnus-group-set-mode-line ()
5006   (when (memq 'group gnus-updated-mode-lines)
5007     (save-excursion
5008       (set-buffer gnus-group-buffer)
5009       (let* ((gformat (or gnus-group-mode-line-format-spec
5010                           (setq gnus-group-mode-line-format-spec
5011                                 (gnus-parse-format
5012                                  gnus-group-mode-line-format
5013                                  gnus-group-mode-line-format-alist))))
5014              (gnus-tmp-news-server (cadr gnus-select-method))
5015              (gnus-tmp-news-method (car gnus-select-method))
5016              (max-len 60)
5017              gnus-tmp-header            ;Dummy binding for user-defined formats
5018              ;; Get the resulting string.
5019              (mode-string (eval gformat)))
5020         ;; Say whether the dribble buffer has been modified.
5021         (setq mode-line-modified
5022               (if (and gnus-dribble-buffer
5023                        (buffer-name gnus-dribble-buffer)
5024                        (buffer-modified-p gnus-dribble-buffer)
5025                        (save-excursion
5026                          (set-buffer gnus-dribble-buffer)
5027                          (not (zerop (buffer-size)))))
5028                   "-* " "-- "))
5029         ;; If the line is too long, we chop it off.
5030         (when (> (length mode-string) max-len)
5031           (setq mode-string (substring mode-string 0 (- max-len 4))))
5032         (prog1
5033             (setq mode-line-buffer-identification (list mode-string))
5034           (set-buffer-modified-p t))))))
5035
5036 (defun gnus-group-group-name ()
5037   "Get the name of the newsgroup on the current line."
5038   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
5039     (and group (symbol-name group))))
5040
5041 (defun gnus-group-group-level ()
5042   "Get the level of the newsgroup on the current line."
5043   (get-text-property (gnus-point-at-bol) 'gnus-level))
5044
5045 (defun gnus-group-group-indentation ()
5046   "Get the indentation of the newsgroup on the current line."
5047   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
5048       (and gnus-group-indentation-function
5049            (funcall gnus-group-indentation-function))
5050       ""))
5051
5052 (defun gnus-group-group-unread ()
5053   "Get the number of unread articles of the newsgroup on the current line."
5054   (get-text-property (gnus-point-at-bol) 'gnus-unread))
5055
5056 (defun gnus-group-search-forward (&optional backward all level first-too)
5057   "Find the next newsgroup with unread articles.
5058 If BACKWARD is non-nil, find the previous newsgroup instead.
5059 If ALL is non-nil, just find any newsgroup.
5060 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
5061 group exists.
5062 If FIRST-TOO, the current line is also eligible as a target."
5063   (let ((way (if backward -1 1))
5064         (low gnus-level-killed)
5065         (beg (point))
5066         pos found lev)
5067     (if (and backward (progn (beginning-of-line)) (bobp))
5068         nil
5069       (or first-too (forward-line way))
5070       (while (and
5071               (not (eobp))
5072               (not (setq
5073                     found
5074                     (and (or all
5075                              (and
5076                               (let ((unread
5077                                      (get-text-property (point) 'gnus-unread)))
5078                                 (and (numberp unread) (> unread 0)))
5079                               (setq lev (get-text-property (point)
5080                                                            'gnus-level))
5081                               (<= lev gnus-level-subscribed)))
5082                          (or (not level)
5083                              (and (setq lev (get-text-property (point)
5084                                                                'gnus-level))
5085                                   (or (= lev level)
5086                                       (and (< lev low)
5087                                            (< level lev)
5088                                            (progn
5089                                              (setq low lev)
5090                                              (setq pos (point))
5091                                              nil))))))))
5092               (zerop (forward-line way)))))
5093     (if found
5094         (progn (gnus-group-position-point) t)
5095       (goto-char (or pos beg))
5096       (and pos t))))
5097
5098 ;;; Gnus group mode commands
5099
5100 ;; Group marking.
5101
5102 (defun gnus-group-mark-group (n &optional unmark no-advance)
5103   "Mark the current group."
5104   (interactive "p")
5105   (let ((buffer-read-only nil)
5106         group)
5107     (while (and (> n 0)
5108                 (not (eobp)))
5109       (when (setq group (gnus-group-group-name))
5110         ;; Update the mark.
5111         (beginning-of-line)
5112         (forward-char
5113          (or (cdr (assq 'process gnus-group-mark-positions)) 2))
5114         (delete-char 1)
5115         (if unmark
5116             (progn
5117               (insert " ")
5118               (setq gnus-group-marked (delete group gnus-group-marked)))
5119           (insert "#")
5120           (setq gnus-group-marked
5121                 (cons group (delete group gnus-group-marked))))
5122         (or no-advance (zerop (gnus-group-next-group 1))))
5123       (decf n))
5124     (gnus-summary-position-point)
5125     n))
5126
5127 (defun gnus-group-unmark-group (n)
5128   "Remove the mark from the current group."
5129   (interactive "p")
5130   (gnus-group-mark-group n 'unmark)
5131   (gnus-group-position-point))
5132
5133 (defun gnus-group-unmark-all-groups ()
5134   "Unmark all groups."
5135   (interactive)
5136   (let ((groups gnus-group-marked))
5137     (save-excursion
5138       (while groups
5139         (gnus-group-remove-mark (pop groups)))))
5140   (gnus-group-position-point))
5141
5142 (defun gnus-group-mark-region (unmark beg end)
5143   "Mark all groups between point and mark.
5144 If UNMARK, remove the mark instead."
5145   (interactive "P\nr")
5146   (let ((num (count-lines beg end)))
5147     (save-excursion
5148       (goto-char beg)
5149       (- num (gnus-group-mark-group num unmark)))))
5150
5151 (defun gnus-group-mark-buffer (&optional unmark)
5152   "Mark all groups in the buffer.
5153 If UNMARK, remove the mark instead."
5154   (interactive "P")
5155   (gnus-group-mark-region unmark (point-min) (point-max)))
5156
5157 (defun gnus-group-mark-regexp (regexp)
5158   "Mark all groups that match some regexp."
5159   (interactive "sMark (regexp): ")
5160   (let ((alist (cdr gnus-newsrc-alist))
5161         group)
5162     (while alist
5163       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5164         (gnus-group-set-mark group))))
5165   (gnus-group-position-point))
5166
5167 (defun gnus-group-remove-mark (group)
5168   "Remove the process mark from GROUP and move point there.
5169 Return nil if the group isn't displayed."
5170   (if (gnus-group-goto-group group)
5171       (save-excursion
5172         (gnus-group-mark-group 1 'unmark t)
5173         t)
5174     (setq gnus-group-marked
5175           (delete group gnus-group-marked))
5176     nil))
5177
5178 (defun gnus-group-set-mark (group)
5179   "Set the process mark on GROUP."
5180   (if (gnus-group-goto-group group) 
5181       (save-excursion
5182         (gnus-group-mark-group 1 nil t))
5183     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
5184
5185 (defun gnus-group-universal-argument (arg &optional groups func)
5186   "Perform any command on all groups accoring to the process/prefix convention."
5187   (interactive "P")
5188   (let ((groups (or groups (gnus-group-process-prefix arg)))
5189         group func)
5190     (if (eq (setq func (or func
5191                            (key-binding
5192                             (read-key-sequence
5193                              (substitute-command-keys
5194                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5195             'undefined)
5196         (gnus-error 1 "Undefined key")
5197       (while groups
5198         (gnus-group-remove-mark (setq group (pop groups)))
5199         (command-execute func))))
5200   (gnus-group-position-point))
5201
5202 (defun gnus-group-process-prefix (n)
5203   "Return a list of groups to work on.
5204 Take into consideration N (the prefix) and the list of marked groups."
5205   (cond
5206    (n
5207     (setq n (prefix-numeric-value n))
5208     ;; There is a prefix, so we return a list of the N next
5209     ;; groups.
5210     (let ((way (if (< n 0) -1 1))
5211           (n (abs n))
5212           group groups)
5213       (save-excursion
5214         (while (and (> n 0)
5215                     (setq group (gnus-group-group-name)))
5216           (setq groups (cons group groups))
5217           (setq n (1- n))
5218           (gnus-group-next-group way)))
5219       (nreverse groups)))
5220    ((and (boundp 'transient-mark-mode)
5221          transient-mark-mode
5222          (boundp 'mark-active)
5223          mark-active)
5224     ;; Work on the region between point and mark.
5225     (let ((max (max (point) (mark)))
5226           groups)
5227       (save-excursion
5228         (goto-char (min (point) (mark)))
5229         (while
5230             (and
5231              (push (gnus-group-group-name) groups)
5232              (zerop (gnus-group-next-group 1))
5233              (< (point) max)))
5234         (nreverse groups))))
5235    (gnus-group-marked
5236     ;; No prefix, but a list of marked articles.
5237     (reverse gnus-group-marked))
5238    (t
5239     ;; Neither marked articles or a prefix, so we return the
5240     ;; current group.
5241     (let ((group (gnus-group-group-name)))
5242       (and group (list group))))))
5243
5244 ;; Selecting groups.
5245
5246 (defun gnus-group-read-group (&optional all no-article group)
5247   "Read news in this newsgroup.
5248 If the prefix argument ALL is non-nil, already read articles become
5249 readable.  IF ALL is a number, fetch this number of articles.  If the
5250 optional argument NO-ARTICLE is non-nil, no article will be
5251 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5252 group."
5253   (interactive "P")
5254   (let ((group (or group (gnus-group-group-name)))
5255         number active marked entry)
5256     (or group (error "No group on current line"))
5257     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5258                                             group gnus-newsrc-hashtb)))))
5259     ;; This group might be a dead group.  In that case we have to get
5260     ;; the number of unread articles from `gnus-active-hashtb'.
5261     (setq number
5262           (cond ((numberp all) all)
5263                 (entry (car entry))
5264                 ((setq active (gnus-active group))
5265                  (- (1+ (cdr active)) (car active)))))
5266     (gnus-summary-read-group
5267      group (or all (and (numberp number)
5268                         (zerop (+ number (length (cdr (assq 'tick marked)))
5269                                   (length (cdr (assq 'dormant marked)))))))
5270      no-article)))
5271
5272 (defun gnus-group-select-group (&optional all)
5273   "Select this newsgroup.
5274 No article is selected automatically.
5275 If ALL is non-nil, already read articles become readable.
5276 If ALL is a number, fetch this number of articles."
5277   (interactive "P")
5278   (gnus-group-read-group all t))
5279
5280 (defun gnus-group-quick-select-group (&optional all)
5281   "Select the current group \"quickly\".
5282 This means that no highlighting or scoring will be performed."
5283   (interactive "P")
5284   (let (gnus-visual
5285         gnus-score-find-score-files-function
5286         gnus-apply-kill-hook
5287         gnus-summary-expunge-below)
5288     (gnus-group-read-group all t)))
5289
5290 (defun gnus-group-visible-select-group (&optional all)
5291   "Select the current group without hiding any articles."
5292   (interactive "P")
5293   (let ((gnus-inhibit-limiting t))
5294     (gnus-group-read-group all t)))
5295
5296 ;;;###autoload
5297 (defun gnus-fetch-group (group)
5298   "Start Gnus if necessary and enter GROUP.
5299 Returns whether the fetching was successful or not."
5300   (interactive "sGroup name: ")
5301   (or (get-buffer gnus-group-buffer)
5302       (gnus))
5303   (gnus-group-read-group nil nil group))
5304
5305 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5306 ;; if selection was successful.
5307 (defun gnus-group-read-ephemeral-group
5308   (group method &optional activate quit-config)
5309   (let ((group (if (gnus-group-foreign-p group) group
5310                  (gnus-group-prefixed-name group method))))
5311     (gnus-sethash
5312      group
5313      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5314                      ((quit-config . ,(if quit-config quit-config
5315                                         (cons (current-buffer) 'summary))))))
5316      gnus-newsrc-hashtb)
5317     (set-buffer gnus-group-buffer)
5318     (or (gnus-check-server method)
5319         (error "Unable to contact server: %s" (gnus-status-message method)))
5320     (if activate (or (gnus-request-group group)
5321                      (error "Couldn't request group")))
5322     (condition-case ()
5323         (gnus-group-read-group t t group)
5324       (error nil)
5325       (quit nil))))
5326
5327 (defun gnus-group-jump-to-group (group)
5328   "Jump to newsgroup GROUP."
5329   (interactive
5330    (list (completing-read
5331           "Group: " gnus-active-hashtb nil
5332           (gnus-read-active-file-p)
5333           nil
5334           'gnus-group-history)))
5335
5336   (when (equal group "")
5337     (error "Empty group name"))
5338
5339   (when (string-match "[\000-\032]" group)
5340     (error "Control characters in group: %s" group))
5341
5342   (let ((b (text-property-any
5343             (point-min) (point-max)
5344             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5345     (unless (gnus-ephemeral-group-p group)
5346       (if b
5347           ;; Either go to the line in the group buffer...
5348           (goto-char b)
5349         ;; ... or insert the line.
5350         (or
5351          (gnus-active group)
5352          (gnus-activate-group group)
5353          (error "%s error: %s" group (gnus-status-message group)))
5354
5355         (gnus-group-update-group group)
5356         (goto-char (text-property-any
5357                     (point-min) (point-max)
5358                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5359     ;; Adjust cursor point.
5360     (gnus-group-position-point)))
5361
5362 (defun gnus-group-goto-group (group)
5363   "Goto to newsgroup GROUP."
5364   (when group
5365     (let ((b (text-property-any (point-min) (point-max)
5366                                 'gnus-group (gnus-intern-safe
5367                                              group gnus-active-hashtb))))
5368       (and b (goto-char b)))))
5369
5370 (defun gnus-group-next-group (n)
5371   "Go to next N'th newsgroup.
5372 If N is negative, search backward instead.
5373 Returns the difference between N and the number of skips actually
5374 done."
5375   (interactive "p")
5376   (gnus-group-next-unread-group n t))
5377
5378 (defun gnus-group-next-unread-group (n &optional all level)
5379   "Go to next N'th unread newsgroup.
5380 If N is negative, search backward instead.
5381 If ALL is non-nil, choose any newsgroup, unread or not.
5382 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5383 such group can be found, the next group with a level higher than
5384 LEVEL.
5385 Returns the difference between N and the number of skips actually
5386 made."
5387   (interactive "p")
5388   (let ((backward (< n 0))
5389         (n (abs n)))
5390     (while (and (> n 0)
5391                 (gnus-group-search-forward
5392                  backward (or (not gnus-group-goto-unread) all) level))
5393       (setq n (1- n)))
5394     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5395                                (if level " on this level or higher" "")))
5396     n))
5397
5398 (defun gnus-group-prev-group (n)
5399   "Go to previous N'th newsgroup.
5400 Returns the difference between N and the number of skips actually
5401 done."
5402   (interactive "p")
5403   (gnus-group-next-unread-group (- n) t))
5404
5405 (defun gnus-group-prev-unread-group (n)
5406   "Go to previous N'th unread newsgroup.
5407 Returns the difference between N and the number of skips actually
5408 done."
5409   (interactive "p")
5410   (gnus-group-next-unread-group (- n)))
5411
5412 (defun gnus-group-next-unread-group-same-level (n)
5413   "Go to next N'th unread newsgroup on the same level.
5414 If N is negative, search backward instead.
5415 Returns the difference between N and the number of skips actually
5416 done."
5417   (interactive "p")
5418   (gnus-group-next-unread-group n t (gnus-group-group-level))
5419   (gnus-group-position-point))
5420
5421 (defun gnus-group-prev-unread-group-same-level (n)
5422   "Go to next N'th unread newsgroup on the same level.
5423 Returns the difference between N and the number of skips actually
5424 done."
5425   (interactive "p")
5426   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5427   (gnus-group-position-point))
5428
5429 (defun gnus-group-best-unread-group (&optional exclude-group)
5430   "Go to the group with the highest level.
5431 If EXCLUDE-GROUP, do not go to that group."
5432   (interactive)
5433   (goto-char (point-min))
5434   (let ((best 100000)
5435         unread best-point)
5436     (while (not (eobp))
5437       (setq unread (get-text-property (point) 'gnus-unread))
5438       (if (and (numberp unread) (> unread 0))
5439           (progn
5440             (if (and (get-text-property (point) 'gnus-level)
5441                      (< (get-text-property (point) 'gnus-level) best)
5442                      (or (not exclude-group)
5443                          (not (equal exclude-group (gnus-group-group-name)))))
5444                 (progn
5445                   (setq best (get-text-property (point) 'gnus-level))
5446                   (setq best-point (point))))))
5447       (forward-line 1))
5448     (if best-point (goto-char best-point))
5449     (gnus-summary-position-point)
5450     (and best-point (gnus-group-group-name))))
5451
5452 (defun gnus-group-first-unread-group ()
5453   "Go to the first group with unread articles."
5454   (interactive)
5455   (prog1
5456       (let ((opoint (point))
5457             unread)
5458         (goto-char (point-min))
5459         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5460                 (and (numberp unread)   ; Not a topic.
5461                      (not (zerop unread))) ; Has unread articles.
5462                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5463             (point)                     ; Success.
5464           (goto-char opoint)
5465           nil))                         ; Not success.
5466     (gnus-group-position-point)))
5467
5468 (defun gnus-group-enter-server-mode ()
5469   "Jump to the server buffer."
5470   (interactive)
5471   (gnus-enter-server-buffer))
5472
5473 (defun gnus-group-make-group (name &optional method address)
5474   "Add a new newsgroup.
5475 The user will be prompted for a NAME, for a select METHOD, and an
5476 ADDRESS."
5477   (interactive
5478    (cons
5479     (read-string "Group name: ")
5480     (let ((method
5481            (completing-read
5482             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5483             nil t nil 'gnus-method-history)))
5484       (cond ((assoc method gnus-valid-select-methods)
5485              (list method
5486                    (if (memq 'prompt-address
5487                              (assoc method gnus-valid-select-methods))
5488                        (read-string "Address: ")
5489                      "")))
5490             ((assoc method gnus-server-alist)
5491              (list method))
5492             (t
5493              (list method ""))))))
5494
5495   (let* ((meth (and method (if address (list (intern method) address)
5496                              method)))
5497          (nname (if method (gnus-group-prefixed-name name meth) name))
5498          backend info)
5499     (when (gnus-gethash nname gnus-newsrc-hashtb)
5500       (error "Group %s already exists" nname))
5501     ;; Subscribe to the new group.
5502     (gnus-group-change-level
5503      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5504      gnus-level-default-subscribed gnus-level-killed
5505      (and (gnus-group-group-name)
5506           (gnus-gethash (gnus-group-group-name)
5507                         gnus-newsrc-hashtb))
5508      t)
5509     ;; Make it active.
5510     (gnus-set-active nname (cons 1 0))
5511     (or (gnus-ephemeral-group-p name)
5512         (gnus-dribble-enter
5513          (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5514     ;; Insert the line.
5515     (gnus-group-insert-group-line-info nname)
5516     (forward-line -1)
5517     (gnus-group-position-point)
5518
5519     ;; Load the backend and try to make the backend create
5520     ;; the group as well.
5521     (when (assoc (symbol-name (setq backend (car (gnus-server-get-method
5522                                                   nil meth))))
5523                  gnus-valid-select-methods)
5524       (require backend))
5525     (gnus-check-server meth)
5526     (and (gnus-check-backend-function 'request-create-group nname)
5527          (gnus-request-create-group nname))
5528     t))
5529
5530 (defun gnus-group-delete-group (group &optional force)
5531   "Delete the current group.
5532 If FORCE (the prefix) is non-nil, all the articles in the group will
5533 be deleted.  This is \"deleted\" as in \"removed forever from the face
5534 of the Earth\".  There is no undo."
5535   (interactive
5536    (list (gnus-group-group-name)
5537          current-prefix-arg))
5538   (or group (error "No group to rename"))
5539   (or (gnus-check-backend-function 'request-delete-group group)
5540       (error "This backend does not support group deletion"))
5541   (prog1
5542       (if (not (gnus-yes-or-no-p
5543                 (format
5544                  "Do you really want to delete %s%s? "
5545                  group (if force " and all its contents" ""))))
5546           () ; Whew!
5547         (gnus-message 6 "Deleting group %s..." group)
5548         (if (not (gnus-request-delete-group group force))
5549             (gnus-error 3 "Couldn't delete group %s" group)
5550           (gnus-message 6 "Deleting group %s...done" group)
5551           (gnus-group-goto-group group)
5552           (gnus-group-kill-group 1 t)
5553           (gnus-sethash group nil gnus-active-hashtb)
5554           t))
5555     (gnus-group-position-point)))
5556
5557 (defun gnus-group-rename-group (group new-name)
5558   (interactive
5559    (list
5560     (gnus-group-group-name)
5561     (progn
5562       (or (gnus-check-backend-function
5563            'request-rename-group (gnus-group-group-name))
5564           (error "This backend does not support renaming groups"))
5565       (read-string "New group name: "))))
5566
5567   (or (gnus-check-backend-function 'request-rename-group group)
5568       (error "This backend does not support renaming groups"))
5569
5570   (or group (error "No group to rename"))
5571   (and (string-match "^[ \t]*$" new-name)
5572        (error "Not a valid group name"))
5573
5574   ;; We find the proper prefixed name.
5575   (setq new-name
5576         (gnus-group-prefixed-name
5577          (gnus-group-real-name new-name)
5578          (gnus-info-method (gnus-get-info group))))
5579
5580   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5581   (prog1
5582       (if (not (gnus-request-rename-group group new-name))
5583           (gnus-error 3 "Couldn't rename group %s to %s" group new-name)
5584         ;; We rename the group internally by killing it...
5585         (gnus-group-goto-group group)
5586         (gnus-group-kill-group)
5587         ;; ... changing its name ...
5588         (setcar (cdar gnus-list-of-killed-groups) new-name)
5589         ;; ... and then yanking it.  Magic!
5590         (gnus-group-yank-group)
5591         (gnus-set-active new-name (gnus-active group))
5592         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5593         new-name)
5594     (gnus-group-position-point)))
5595
5596 (defun gnus-group-edit-group (group &optional part)
5597   "Edit the group on the current line."
5598   (interactive (list (gnus-group-group-name)))
5599   (let* ((part (or part 'info))
5600          (done-func `(lambda ()
5601                        "Exit editing mode and update the information."
5602                        (interactive)
5603                        (gnus-group-edit-group-done ',part ,group)))
5604          (winconf (current-window-configuration))
5605          info)
5606     (or group (error "No group on current line"))
5607     (or (setq info (gnus-get-info group))
5608         (error "Killed group; can't be edited"))
5609     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5610     (gnus-configure-windows 'edit-group)
5611     (gnus-add-current-to-buffer-list)
5612     (emacs-lisp-mode)
5613     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5614     (use-local-map (copy-keymap emacs-lisp-mode-map))
5615     (local-set-key "\C-c\C-c" done-func)
5616     (make-local-variable 'gnus-prev-winconf)
5617     (setq gnus-prev-winconf winconf)
5618     (erase-buffer)
5619     (insert
5620      (cond
5621       ((eq part 'method)
5622        ";; Type `C-c C-c' after editing the select method.\n\n")
5623       ((eq part 'params)
5624        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5625       ((eq part 'info)
5626        ";; Type `C-c C-c' after editing the group info.\n\n")))
5627     (insert
5628      (pp-to-string
5629       (cond ((eq part 'method)
5630              (or (gnus-info-method info) "native"))
5631             ((eq part 'params)
5632              (gnus-info-params info))
5633             (t info)))
5634      "\n")))
5635
5636 (defun gnus-group-edit-group-method (group)
5637   "Edit the select method of GROUP."
5638   (interactive (list (gnus-group-group-name)))
5639   (gnus-group-edit-group group 'method))
5640
5641 (defun gnus-group-edit-group-parameters (group)
5642   "Edit the group parameters of GROUP."
5643   (interactive (list (gnus-group-group-name)))
5644   (gnus-group-edit-group group 'params))
5645
5646 (defun gnus-group-edit-group-done (part group)
5647   "Get info from buffer, update variables and jump to the group buffer."
5648   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5649   (goto-char (point-min))
5650   (let* ((form (read (current-buffer)))
5651          (winconf gnus-prev-winconf)
5652          (method (cond ((eq part 'info) (nth 4 form))
5653                        ((eq part 'method) form)
5654                        (t nil)))
5655          (info (cond ((eq part 'info) form)
5656                      ((eq part 'method) (gnus-get-info group))
5657                      (t nil)))
5658          (new-group (if info
5659                       (if (or (not method)
5660                               (gnus-server-equal
5661                                gnus-select-method method))
5662                           (gnus-group-real-name (car info))
5663                         (gnus-group-prefixed-name
5664                          (gnus-group-real-name (car info)) method))
5665                       nil)))
5666     (when (and new-group
5667                (not (equal new-group group)))
5668       (when (gnus-group-goto-group group)
5669         (gnus-group-kill-group 1))
5670       (gnus-activate-group new-group))
5671     ;; Set the info.
5672     (if (and info new-group)
5673         (progn
5674           (setq info (gnus-copy-sequence info))
5675           (setcar info new-group)
5676           (unless (gnus-server-equal method "native")
5677             (unless (nthcdr 3 info)
5678               (nconc info (list nil nil)))
5679             (unless (nthcdr 4 info)
5680               (nconc info (list nil)))
5681             (gnus-info-set-method info method))
5682           (gnus-group-set-info info))
5683       (gnus-group-set-info form (or new-group group) part))
5684     (kill-buffer (current-buffer))
5685     (and winconf (set-window-configuration winconf))
5686     (set-buffer gnus-group-buffer)
5687     (gnus-group-update-group (or new-group group))
5688     (gnus-group-position-point)))
5689
5690 (defun gnus-group-make-help-group ()
5691   "Create the Gnus documentation group."
5692   (interactive)
5693   (let ((path load-path)
5694         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5695         file dir)
5696     (and (gnus-gethash name gnus-newsrc-hashtb)
5697          (error "Documentation group already exists"))
5698     (while path
5699       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5700             file nil)
5701       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5702                 (file-exists-p
5703                  (setq file (concat (file-name-directory
5704                                      (directory-file-name dir))
5705                                     "etc/gnus-tut.txt"))))
5706         (setq path nil)))
5707     (if (not file)
5708         (gnus-message 1 "Couldn't find doc group")
5709       (gnus-group-make-group
5710        (gnus-group-real-name name)
5711        (list 'nndoc "gnus-help"
5712              (list 'nndoc-address file)
5713              (list 'nndoc-article-type 'mbox)))))
5714   (gnus-group-position-point))
5715
5716 (defun gnus-group-make-doc-group (file type)
5717   "Create a group that uses a single file as the source."
5718   (interactive
5719    (list (read-file-name "File name: ")
5720          (and current-prefix-arg 'ask)))
5721   (when (eq type 'ask)
5722     (let ((err "")
5723           char found)
5724       (while (not found)
5725         (message
5726          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5727          err)
5728         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5729                           ((= char ?b) 'babyl)
5730                           ((= char ?d) 'digest)
5731                           ((= char ?f) 'forward)
5732                           ((= char ?a) 'mmfd)
5733                           (t (setq err (format "%c unknown. " char))
5734                              nil))))
5735       (setq type found)))
5736   (let* ((file (expand-file-name file))
5737          (name (gnus-generate-new-group-name
5738                 (gnus-group-prefixed-name
5739                  (file-name-nondirectory file) '(nndoc "")))))
5740     (gnus-group-make-group
5741      (gnus-group-real-name name)
5742      (list 'nndoc (file-name-nondirectory file)
5743            (list 'nndoc-address file)
5744            (list 'nndoc-article-type (or type 'guess))))))
5745
5746 (defun gnus-group-make-archive-group (&optional all)
5747   "Create the (ding) Gnus archive group of the most recent articles.
5748 Given a prefix, create a full group."
5749   (interactive "P")
5750   (let ((group (gnus-group-prefixed-name
5751                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5752     (and (gnus-gethash group gnus-newsrc-hashtb)
5753          (error "Archive group already exists"))
5754     (gnus-group-make-group
5755      (gnus-group-real-name group)
5756      (list 'nndir (if all "hpc" "edu")
5757            (list 'nndir-directory
5758                  (if all gnus-group-archive-directory
5759                    gnus-group-recent-archive-directory))))))
5760
5761 (defun gnus-group-make-directory-group (dir)
5762   "Create an nndir group.
5763 The user will be prompted for a directory.  The contents of this
5764 directory will be used as a newsgroup.  The directory should contain
5765 mail messages or news articles in files that have numeric names."
5766   (interactive
5767    (list (read-file-name "Create group from directory: ")))
5768   (or (file-exists-p dir) (error "No such directory"))
5769   (or (file-directory-p dir) (error "Not a directory"))
5770   (let ((ext "")
5771         (i 0)
5772         group)
5773     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5774       (setq group
5775             (gnus-group-prefixed-name
5776              (concat (file-name-as-directory (directory-file-name dir))
5777                      ext)
5778              '(nndir "")))
5779       (setq ext (format "<%d>" (setq i (1+ i)))))
5780     (gnus-group-make-group
5781      (gnus-group-real-name group)
5782      (list 'nndir group (list 'nndir-directory dir)))))
5783
5784 (defun gnus-group-make-kiboze-group (group address scores)
5785   "Create an nnkiboze group.
5786 The user will be prompted for a name, a regexp to match groups, and
5787 score file entries for articles to include in the group."
5788   (interactive
5789    (list
5790     (read-string "nnkiboze group name: ")
5791     (read-string "Source groups (regexp): ")
5792     (let ((headers (mapcar (lambda (group) (list group))
5793                            '("subject" "from" "number" "date" "message-id"
5794                              "references" "chars" "lines" "xref"
5795                              "followup" "all" "body" "head")))
5796           scores header regexp regexps)
5797       (while (not (equal "" (setq header (completing-read
5798                                           "Match on header: " headers nil t))))
5799         (setq regexps nil)
5800         (while (not (equal "" (setq regexp (read-string
5801                                             (format "Match on %s (string): "
5802                                                     header)))))
5803           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5804         (setq scores (cons (cons header regexps) scores)))
5805       scores)))
5806   (gnus-group-make-group group "nnkiboze" address)
5807   (nnheader-temp-write (gnus-score-file-name (concat "nnkiboze:" group))
5808     (let (emacs-lisp-mode-hook)
5809       (pp scores (current-buffer)))))
5810
5811 (defun gnus-group-add-to-virtual (n vgroup)
5812   "Add the current group to a virtual group."
5813   (interactive
5814    (list current-prefix-arg
5815          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5816                           "nnvirtual:")))
5817   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5818       (error "%s is not an nnvirtual group" vgroup))
5819   (let* ((groups (gnus-group-process-prefix n))
5820          (method (gnus-info-method (gnus-get-info vgroup))))
5821     (setcar (cdr method)
5822             (concat
5823              (nth 1 method) "\\|"
5824              (mapconcat
5825               (lambda (s)
5826                 (gnus-group-remove-mark s)
5827                 (concat "\\(^" (regexp-quote s) "$\\)"))
5828               groups "\\|"))))
5829   (gnus-group-position-point))
5830
5831 (defun gnus-group-make-empty-virtual (group)
5832   "Create a new, fresh, empty virtual group."
5833   (interactive "sCreate new, empty virtual group: ")
5834   (let* ((method (list 'nnvirtual "^$"))
5835          (pgroup (gnus-group-prefixed-name group method)))
5836     ;; Check whether it exists already.
5837     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5838          (error "Group %s already exists." pgroup))
5839     ;; Subscribe the new group after the group on the current line.
5840     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5841     (gnus-group-update-group pgroup)
5842     (forward-line -1)
5843     (gnus-group-position-point)))
5844
5845 (defun gnus-group-enter-directory (dir)
5846   "Enter an ephemeral nneething group."
5847   (interactive "DDirectory to read: ")
5848   (let* ((method (list 'nneething dir))
5849          (leaf (gnus-group-prefixed-name
5850                 (file-name-nondirectory (directory-file-name dir))
5851                 method))
5852          (name (gnus-generate-new-group-name leaf)))
5853     (let ((nneething-read-only t))
5854       (or (gnus-group-read-ephemeral-group
5855            name method t
5856            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5857                                       'summary 'group)))
5858           (error "Couldn't enter %s" dir)))))
5859
5860 ;; Group sorting commands
5861 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5862
5863 (defun gnus-group-sort-groups (func &optional reverse)
5864   "Sort the group buffer according to FUNC.
5865 If REVERSE, reverse the sorting order."
5866   (interactive (list gnus-group-sort-function
5867                      current-prefix-arg))
5868   (let ((func (cond 
5869                ((not (listp func)) func)
5870                ((null func) func)
5871                ((= 1 (length func)) (car func))
5872                (t `(lambda (t1 t2)
5873                      ,(gnus-make-sort-function 
5874                        (reverse func)))))))
5875     ;; We peel off the dummy group from the alist.
5876     (when func
5877       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5878         (pop gnus-newsrc-alist))
5879       ;; Do the sorting.
5880       (setq gnus-newsrc-alist
5881             (sort gnus-newsrc-alist func))
5882       (when reverse
5883         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5884       ;; Regenerate the hash table.
5885       (gnus-make-hashtable-from-newsrc-alist)
5886       (gnus-group-list-groups))))
5887
5888 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5889   "Sort the group buffer alphabetically by group name.
5890 If REVERSE, sort in reverse order."
5891   (interactive "P")
5892   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5893
5894 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5895   "Sort the group buffer by number of unread articles.
5896 If REVERSE, sort in reverse order."
5897   (interactive "P")
5898   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5899
5900 (defun gnus-group-sort-groups-by-level (&optional reverse)
5901   "Sort the group buffer by group level.
5902 If REVERSE, sort in reverse order."
5903   (interactive "P")
5904   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5905
5906 (defun gnus-group-sort-groups-by-score (&optional reverse)
5907   "Sort the group buffer by group score.
5908 If REVERSE, sort in reverse order."
5909   (interactive "P")
5910   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5911
5912 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5913   "Sort the group buffer by group rank.
5914 If REVERSE, sort in reverse order."
5915   (interactive "P")
5916   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5917
5918 (defun gnus-group-sort-groups-by-method (&optional reverse)
5919   "Sort the group buffer alphabetically by backend name.
5920 If REVERSE, sort in reverse order."
5921   (interactive "P")
5922   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
5923
5924 (defun gnus-group-sort-by-alphabet (info1 info2)
5925   "Sort alphabetically."
5926   (string< (gnus-info-group info1) (gnus-info-group info2)))
5927
5928 (defun gnus-group-sort-by-unread (info1 info2)
5929   "Sort by number of unread articles."
5930   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
5931         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
5932     (< (or (and (numberp n1) n1) 0)
5933        (or (and (numberp n2) n2) 0))))
5934
5935 (defun gnus-group-sort-by-level (info1 info2)
5936   "Sort by level."
5937   (< (gnus-info-level info1) (gnus-info-level info2)))
5938
5939 (defun gnus-group-sort-by-method (info1 info2)
5940   "Sort alphabetically by backend name."
5941   (string< (symbol-name (car (gnus-find-method-for-group
5942                               (gnus-info-group info1) info1)))
5943            (symbol-name (car (gnus-find-method-for-group
5944                               (gnus-info-group info2) info2)))))
5945
5946 (defun gnus-group-sort-by-score (info1 info2)
5947   "Sort by group score."
5948   (< (gnus-info-score info1) (gnus-info-score info2)))
5949
5950 (defun gnus-group-sort-by-rank (info1 info2)
5951   "Sort by level and score."
5952   (let ((level1 (gnus-info-level info1))
5953         (level2 (gnus-info-level info2)))
5954     (or (< level1 level2)
5955         (and (= level1 level2)
5956              (> (gnus-info-score info1) (gnus-info-score info2))))))
5957
5958 ;; Group catching up.
5959
5960 (defun gnus-group-clear-data (n)
5961   "Clear all marks and read ranges from the current group."
5962   (interactive "P")
5963   (let ((groups (gnus-group-process-prefix n))
5964         group info)
5965     (while (setq group (pop groups))
5966       (setq info (gnus-get-info group))
5967       (gnus-info-set-read info nil)
5968       (when (gnus-info-marks info)
5969         (gnus-info-set-marks info nil))
5970       (gnus-get-unread-articles-in-group info (gnus-active group) t)
5971       (when (gnus-group-goto-group group)
5972         (gnus-group-remove-mark group)
5973         (gnus-group-update-group-line)))))
5974
5975 (defun gnus-group-catchup-current (&optional n all)
5976   "Mark all articles not marked as unread in current newsgroup as read.
5977 If prefix argument N is numeric, the ARG next newsgroups will be
5978 caught up.  If ALL is non-nil, marked articles will also be marked as
5979 read.  Cross references (Xref: header) of articles are ignored.
5980 The difference between N and actual number of newsgroups that were
5981 caught up is returned."
5982   (interactive "P")
5983   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
5984                gnus-expert-user
5985                (gnus-y-or-n-p
5986                 (if all
5987                     "Do you really want to mark all articles as read? "
5988                   "Mark all unread articles as read? "))))
5989       n
5990     (let ((groups (gnus-group-process-prefix n))
5991           (ret 0))
5992       (while groups
5993         ;; Virtual groups have to be given special treatment.
5994         (let ((method (gnus-find-method-for-group (car groups))))
5995           (if (eq 'nnvirtual (car method))
5996               (nnvirtual-catchup-group
5997                (gnus-group-real-name (car groups)) (nth 1 method) all)))
5998         (gnus-group-remove-mark (car groups))
5999         (if (>= (gnus-group-group-level) gnus-level-zombie)
6000             (gnus-message 2 "Dead groups can't be caught up")
6001           (if (prog1
6002                   (gnus-group-goto-group (car groups))
6003                 (gnus-group-catchup (car groups) all))
6004               (gnus-group-update-group-line)
6005             (setq ret (1+ ret))))
6006         (setq groups (cdr groups)))
6007       (gnus-group-next-unread-group 1)
6008       ret)))
6009
6010 (defun gnus-group-catchup-current-all (&optional n)
6011   "Mark all articles in current newsgroup as read.
6012 Cross references (Xref: header) of articles are ignored."
6013   (interactive "P")
6014   (gnus-group-catchup-current n 'all))
6015
6016 (defun gnus-group-catchup (group &optional all)
6017   "Mark all articles in GROUP as read.
6018 If ALL is non-nil, all articles are marked as read.
6019 The return value is the number of articles that were marked as read,
6020 or nil if no action could be taken."
6021   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6022          (num (car entry)))
6023     ;; Do the updating only if the newsgroup isn't killed.
6024     (if (not (numberp (car entry)))
6025         (gnus-message 1 "Can't catch up; non-active group")
6026       ;; Do auto-expirable marks if that's required.
6027       (when (gnus-group-auto-expirable-p group)
6028         (gnus-add-marked-articles
6029          group 'expire (gnus-list-of-unread-articles group))
6030         (when all
6031           (let ((marks (nth 3 (nth 2 entry))))
6032             (gnus-add-marked-articles
6033              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
6034             (gnus-add-marked-articles
6035              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
6036       (when entry
6037         (gnus-update-read-articles group nil)
6038         ;; Also nix out the lists of marks and dormants.
6039         (when all
6040           (gnus-add-marked-articles group 'tick nil nil 'force)
6041           (gnus-add-marked-articles group 'dormant nil nil 'force))
6042         (run-hooks 'gnus-group-catchup-group-hook)
6043         num))))
6044
6045 (defun gnus-group-expire-articles (&optional n)
6046   "Expire all expirable articles in the current newsgroup."
6047   (interactive "P")
6048   (let ((groups (gnus-group-process-prefix n))
6049         group)
6050     (unless groups
6051       (error "No groups to expire"))
6052     (while (setq group (pop groups))
6053       (gnus-group-remove-mark group)
6054       (when (gnus-check-backend-function 'request-expire-articles group)
6055         (gnus-message 6 "Expiring articles in %s..." group)
6056         (let* ((info (gnus-get-info group))
6057                (expirable (if (gnus-group-total-expirable-p group)
6058                               (cons nil (gnus-list-of-read-articles group))
6059                             (assq 'expire (gnus-info-marks info))))
6060                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
6061           (when expirable
6062             (setcdr
6063              expirable
6064              (gnus-compress-sequence
6065               (if expiry-wait
6066                   ;; We set the expiry variables to the groupp
6067                   ;; parameter. 
6068                   (let ((nnmail-expiry-wait-function nil)
6069                         (nnmail-expiry-wait expiry-wait))
6070                     (gnus-request-expire-articles
6071                      (gnus-uncompress-sequence (cdr expirable)) group))
6072                 ;; Just expire using the normal expiry values.
6073                 (gnus-request-expire-articles
6074                  (gnus-uncompress-sequence (cdr expirable)) group)))))
6075           (gnus-message 6 "Expiring articles in %s...done" group)))
6076       (gnus-group-position-point))))
6077
6078 (defun gnus-group-expire-all-groups ()
6079   "Expire all expirable articles in all newsgroups."
6080   (interactive)
6081   (save-excursion
6082     (gnus-message 5 "Expiring...")
6083     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
6084                                      (cdr gnus-newsrc-alist))))
6085       (gnus-group-expire-articles nil)))
6086   (gnus-group-position-point)
6087   (gnus-message 5 "Expiring...done"))
6088
6089 (defun gnus-group-set-current-level (n level)
6090   "Set the level of the next N groups to LEVEL."
6091   (interactive
6092    (list
6093     current-prefix-arg
6094     (string-to-int
6095      (let ((s (read-string
6096                (format "Level (default %s): "
6097                        (or (gnus-group-group-level) 
6098                            gnus-level-default-subscribed)))))
6099        (if (string-match "^\\s-*$" s)
6100            (int-to-string (or (gnus-group-group-level) 
6101                               gnus-level-default-subscribed))
6102          s)))))
6103   (or (and (>= level 1) (<= level gnus-level-killed))
6104       (error "Illegal level: %d" level))
6105   (let ((groups (gnus-group-process-prefix n))
6106         group)
6107     (while (setq group (pop groups))
6108       (gnus-group-remove-mark group)
6109       (gnus-message 6 "Changed level of %s from %d to %d"
6110                     group (or (gnus-group-group-level) gnus-level-killed)
6111                     level)
6112       (gnus-group-change-level
6113        group level (or (gnus-group-group-level) gnus-level-killed))
6114       (gnus-group-update-group-line)))
6115   (gnus-group-position-point))
6116
6117 (defun gnus-group-unsubscribe-current-group (&optional n)
6118   "Toggle subscription of the current group.
6119 If given numerical prefix, toggle the N next groups."
6120   (interactive "P")
6121   (let ((groups (gnus-group-process-prefix n))
6122         group)
6123     (while groups
6124       (setq group (car groups)
6125             groups (cdr groups))
6126       (gnus-group-remove-mark group)
6127       (gnus-group-unsubscribe-group
6128        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
6129                  gnus-level-default-unsubscribed
6130                gnus-level-default-subscribed) t)
6131       (gnus-group-update-group-line))
6132     (gnus-group-next-group 1)))
6133
6134 (defun gnus-group-unsubscribe-group (group &optional level silent)
6135   "Toggle subscription to GROUP.
6136 Killed newsgroups are subscribed.  If SILENT, don't try to update the
6137 group line."
6138   (interactive
6139    (list (completing-read
6140           "Group: " gnus-active-hashtb nil
6141           (gnus-read-active-file-p)
6142           nil 
6143           'gnus-group-history)))
6144   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
6145     (cond
6146      ((string-match "^[ \t]$" group)
6147       (error "Empty group name"))
6148      (newsrc
6149       ;; Toggle subscription flag.
6150       (gnus-group-change-level
6151        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
6152                                       gnus-level-subscribed)
6153                                   (1+ gnus-level-subscribed)
6154                                 gnus-level-default-subscribed)))
6155       (unless silent
6156         (gnus-group-update-group group)))
6157      ((and (stringp group)
6158            (or (not (gnus-read-active-file-p))
6159                (gnus-active group)))
6160       ;; Add new newsgroup.
6161       (gnus-group-change-level
6162        group
6163        (if level level gnus-level-default-subscribed)
6164        (or (and (member group gnus-zombie-list)
6165                 gnus-level-zombie)
6166            gnus-level-killed)
6167        (and (gnus-group-group-name)
6168             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
6169       (unless silent
6170         (gnus-group-update-group group)))
6171      (t (error "No such newsgroup: %s" group)))
6172     (gnus-group-position-point)))
6173
6174 (defun gnus-group-transpose-groups (n)
6175   "Move the current newsgroup up N places.
6176 If given a negative prefix, move down instead.  The difference between
6177 N and the number of steps taken is returned."
6178   (interactive "p")
6179   (or (gnus-group-group-name)
6180       (error "No group on current line"))
6181   (gnus-group-kill-group 1)
6182   (prog1
6183       (forward-line (- n))
6184     (gnus-group-yank-group)
6185     (gnus-group-position-point)))
6186
6187 (defun gnus-group-kill-all-zombies ()
6188   "Kill all zombie newsgroups."
6189   (interactive)
6190   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6191   (setq gnus-zombie-list nil)
6192   (gnus-group-list-groups))
6193
6194 (defun gnus-group-kill-region (begin end)
6195   "Kill newsgroups in current region (excluding current point).
6196 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6197   (interactive "r")
6198   (let ((lines
6199          ;; Count lines.
6200          (save-excursion
6201            (count-lines
6202             (progn
6203               (goto-char begin)
6204               (beginning-of-line)
6205               (point))
6206             (progn
6207               (goto-char end)
6208               (beginning-of-line)
6209               (point))))))
6210     (goto-char begin)
6211     (beginning-of-line)                 ;Important when LINES < 1
6212     (gnus-group-kill-group lines)))
6213
6214 (defun gnus-group-kill-group (&optional n discard)
6215   "Kill the next N groups.
6216 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6217 However, only groups that were alive can be yanked; already killed
6218 groups or zombie groups can't be yanked.
6219 The return value is the name of the group that was killed, or a list
6220 of groups killed."
6221   (interactive "P")
6222   (let ((buffer-read-only nil)
6223         (groups (gnus-group-process-prefix n))
6224         group entry level out)
6225     (if (< (length groups) 10)
6226         ;; This is faster when there are few groups.
6227         (while groups
6228           (push (setq group (pop groups)) out)
6229           (gnus-group-remove-mark group)
6230           (setq level (gnus-group-group-level))
6231           (gnus-delete-line)
6232           (when (and (not discard)
6233                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6234             (push (cons (car entry) (nth 2 entry))
6235                   gnus-list-of-killed-groups))
6236           (gnus-group-change-level
6237            (if entry entry group) gnus-level-killed (if entry nil level)))
6238       ;; If there are lots and lots of groups to be killed, we use
6239       ;; this thing instead.
6240       (let (entry)
6241         (setq groups (nreverse groups))
6242         (while groups
6243           (gnus-group-remove-mark (setq group (pop groups)))
6244           (gnus-delete-line)
6245           (cond
6246            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6247             (push (cons (car entry) (nth 2 entry))
6248                   gnus-list-of-killed-groups)
6249             (setcdr (cdr entry) (cdddr entry)))
6250            ((member group gnus-zombie-list)
6251             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6252         (gnus-make-hashtable-from-newsrc-alist)))
6253
6254     (gnus-group-position-point)
6255     (if (< (length out) 2) (car out) (nreverse out))))
6256
6257 (defun gnus-group-yank-group (&optional arg)
6258   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6259 inserting it before the current newsgroup.  The numeric ARG specifies
6260 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6261 is returned, or (if several groups are yanked) a list of yanked groups
6262 is returned."
6263   (interactive "p")
6264   (setq arg (or arg 1))
6265   (let (info group prev out)
6266     (while (>= (decf arg) 0)
6267       (if (not (setq info (pop gnus-list-of-killed-groups)))
6268           (error "No more newsgroups to yank"))
6269       (push (setq group (nth 1 info)) out)
6270       ;; Find which newsgroup to insert this one before - search
6271       ;; backward until something suitable is found.  If there are no
6272       ;; other newsgroups in this buffer, just make this newsgroup the
6273       ;; first newsgroup.
6274       (setq prev (gnus-group-group-name))
6275       (gnus-group-change-level
6276        info (gnus-info-level (cdr info)) gnus-level-killed
6277        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6278        t)
6279       (gnus-group-insert-group-line-info group))
6280     (forward-line -1)
6281     (gnus-group-position-point)
6282     (if (< (length out) 2) (car out) (nreverse out))))
6283
6284 (defun gnus-group-kill-level (level)
6285   "Kill all groups that is on a certain LEVEL."
6286   (interactive "nKill all groups on level: ")
6287   (cond
6288    ((= level gnus-level-zombie)
6289     (setq gnus-killed-list
6290           (nconc gnus-zombie-list gnus-killed-list))
6291     (setq gnus-zombie-list nil))
6292    ((and (< level gnus-level-zombie)
6293          (> level 0)
6294          (or gnus-expert-user
6295              (gnus-yes-or-no-p
6296               (format
6297                "Do you really want to kill all groups on level %d? "
6298                level))))
6299     (let* ((prev gnus-newsrc-alist)
6300            (alist (cdr prev)))
6301       (while alist
6302         (if (= (gnus-info-level level) level)
6303             (setcdr prev (cdr alist))
6304           (setq prev alist))
6305         (setq alist (cdr alist)))
6306       (gnus-make-hashtable-from-newsrc-alist)
6307       (gnus-group-list-groups)))
6308    (t
6309     (error "Can't kill; illegal level: %d" level))))
6310
6311 (defun gnus-group-list-all-groups (&optional arg)
6312   "List all newsgroups with level ARG or lower.
6313 Default is gnus-level-unsubscribed, which lists all subscribed and most
6314 unsubscribed groups."
6315   (interactive "P")
6316   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6317
6318 ;; Redefine this to list ALL killed groups if prefix arg used.
6319 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6320 (defun gnus-group-list-killed (&optional arg)
6321   "List all killed newsgroups in the group buffer.
6322 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6323 entail asking the server for the groups."
6324   (interactive "P")
6325   ;; Find all possible killed newsgroups if arg.
6326   (when arg
6327     (gnus-get-killed-groups))
6328   (if (not gnus-killed-list)
6329       (gnus-message 6 "No killed groups")
6330     (let (gnus-group-list-mode)
6331       (funcall gnus-group-prepare-function
6332                gnus-level-killed t gnus-level-killed))
6333     (goto-char (point-min)))
6334   (gnus-group-position-point))
6335
6336 (defun gnus-group-list-zombies ()
6337   "List all zombie newsgroups in the group buffer."
6338   (interactive)
6339   (if (not gnus-zombie-list)
6340       (gnus-message 6 "No zombie groups")
6341     (let (gnus-group-list-mode)
6342       (funcall gnus-group-prepare-function
6343                gnus-level-zombie t gnus-level-zombie))
6344     (goto-char (point-min)))
6345   (gnus-group-position-point))
6346
6347 (defun gnus-group-list-active ()
6348   "List all groups that are available from the server(s)."
6349   (interactive)
6350   ;; First we make sure that we have really read the active file.
6351   (unless (gnus-read-active-file-p)
6352     (let ((gnus-read-active-file t))
6353       (gnus-read-active-file)))
6354   ;; Find all groups and sort them.
6355   (let ((groups
6356          (sort
6357           (let (list)
6358             (mapatoms
6359              (lambda (sym)
6360                (and (symbol-value sym)
6361                     (setq list (cons (symbol-name sym) list))))
6362              gnus-active-hashtb)
6363             list)
6364           'string<))
6365         (buffer-read-only nil))
6366     (erase-buffer)
6367     (while groups
6368       (gnus-group-insert-group-line-info (pop groups)))
6369     (goto-char (point-min))))
6370
6371 (defun gnus-activate-all-groups (level)
6372   "Activate absolutely all groups."
6373   (interactive (list 7))
6374   (let ((gnus-activate-level level)
6375         (gnus-activate-foreign-newsgroups level))
6376     (gnus-group-get-new-news)))
6377
6378 (defun gnus-group-get-new-news (&optional arg)
6379   "Get newly arrived articles.
6380 If ARG is a number, it specifies which levels you are interested in
6381 re-scanning.  If ARG is non-nil and not a number, this will force
6382 \"hard\" re-reading of the active files from all servers."
6383   (interactive "P")
6384   (run-hooks 'gnus-get-new-news-hook)
6385   ;; We might read in new NoCeM messages here.
6386   (when (and gnus-use-nocem 
6387              (null arg))
6388     (gnus-nocem-scan-groups))
6389   ;; If ARG is not a number, then we read the active file.
6390   (when (and arg (not (numberp arg)))
6391     (let ((gnus-read-active-file t))
6392       (gnus-read-active-file))
6393     (setq arg nil))
6394
6395   (setq arg (gnus-group-default-level arg t))
6396   (if (and gnus-read-active-file (not arg))
6397       (progn
6398         (gnus-read-active-file)
6399         (gnus-get-unread-articles arg))
6400     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6401       (gnus-get-unread-articles arg)))
6402   (run-hooks 'gnus-after-getting-new-news-hook)
6403   (gnus-group-list-groups))
6404
6405 (defun gnus-group-get-new-news-this-group (&optional n)
6406   "Check for newly arrived news in the current group (and the N-1 next groups).
6407 The difference between N and the number of newsgroup checked is returned.
6408 If N is negative, this group and the N-1 previous groups will be checked."
6409   (interactive "P")
6410   (let* ((groups (gnus-group-process-prefix n))
6411          (ret (if (numberp n) (- n (length groups)) 0))
6412          (beg (unless n (point)))
6413          group)
6414     (while (setq group (pop groups))
6415       (gnus-group-remove-mark group)
6416       (if (gnus-activate-group group 'scan)
6417           (progn
6418             (gnus-get-unread-articles-in-group
6419              (gnus-get-info group) (gnus-active group) t)
6420             (unless (gnus-virtual-group-p group)
6421               (gnus-close-group group))
6422             (gnus-group-update-group group))
6423         (gnus-error 3 "%s error: %s" group (gnus-status-message group))))
6424     (when beg (goto-char beg))
6425     (when gnus-goto-next-group-when-activating
6426       (gnus-group-next-unread-group 1 t))
6427     (gnus-summary-position-point)
6428     ret))
6429
6430 (defun gnus-group-fetch-faq (group &optional faq-dir)
6431   "Fetch the FAQ for the current group."
6432   (interactive
6433    (list
6434     (gnus-group-real-name (gnus-group-group-name))
6435     (cond (current-prefix-arg
6436            (completing-read
6437             "Faq dir: " (and (listp gnus-group-faq-directory)
6438                              gnus-group-faq-directory))))))
6439   (or faq-dir
6440       (setq faq-dir (if (listp gnus-group-faq-directory)
6441                         (car gnus-group-faq-directory)
6442                       gnus-group-faq-directory)))
6443   (or group (error "No group name given"))
6444   (let ((file (concat (file-name-as-directory faq-dir)
6445                       (gnus-group-real-name group))))
6446     (if (not (file-exists-p file))
6447         (error "No such file: %s" file)
6448       (find-file file))))
6449
6450 (defun gnus-group-describe-group (force &optional group)
6451   "Display a description of the current newsgroup."
6452   (interactive (list current-prefix-arg (gnus-group-group-name)))
6453   (and force (setq gnus-description-hashtb nil))
6454   (let ((method (gnus-find-method-for-group group))
6455         desc)
6456     (or group (error "No group name given"))
6457     (and (or (and gnus-description-hashtb
6458                   ;; We check whether this group's method has been
6459                   ;; queried for a description file.
6460                   (gnus-gethash
6461                    (gnus-group-prefixed-name "" method)
6462                    gnus-description-hashtb))
6463              (setq desc (gnus-group-get-description group))
6464              (gnus-read-descriptions-file method))
6465          (gnus-message 1
6466           (or desc (gnus-gethash group gnus-description-hashtb)
6467               "No description available")))))
6468
6469 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6470 (defun gnus-group-describe-all-groups (&optional force)
6471   "Pop up a buffer with descriptions of all newsgroups."
6472   (interactive "P")
6473   (and force (setq gnus-description-hashtb nil))
6474   (if (not (or gnus-description-hashtb
6475                (gnus-read-all-descriptions-files)))
6476       (error "Couldn't request descriptions file"))
6477   (let ((buffer-read-only nil)
6478         b)
6479     (erase-buffer)
6480     (mapatoms
6481      (lambda (group)
6482        (setq b (point))
6483        (insert (format "      *: %-20s %s\n" (symbol-name group)
6484                        (symbol-value group)))
6485        (gnus-add-text-properties
6486         b (1+ b) (list 'gnus-group group
6487                        'gnus-unread t 'gnus-marked nil
6488                        'gnus-level (1+ gnus-level-subscribed))))
6489      gnus-description-hashtb)
6490     (goto-char (point-min))
6491     (gnus-group-position-point)))
6492
6493 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6494 (defun gnus-group-apropos (regexp &optional search-description)
6495   "List all newsgroups that have names that match a regexp."
6496   (interactive "sGnus apropos (regexp): ")
6497   (let ((prev "")
6498         (obuf (current-buffer))
6499         groups des)
6500     ;; Go through all newsgroups that are known to Gnus.
6501     (mapatoms
6502      (lambda (group)
6503        (and (symbol-name group)
6504             (string-match regexp (symbol-name group))
6505             (setq groups (cons (symbol-name group) groups))))
6506      gnus-active-hashtb)
6507     ;; Also go through all descriptions that are known to Gnus.
6508     (when search-description
6509       (mapatoms
6510        (lambda (group)
6511          (and (string-match regexp (symbol-value group))
6512               (gnus-active (symbol-name group))
6513               (setq groups (cons (symbol-name group) groups))))
6514        gnus-description-hashtb))
6515     (if (not groups)
6516         (gnus-message 3 "No groups matched \"%s\"." regexp)
6517       ;; Print out all the groups.
6518       (save-excursion
6519         (pop-to-buffer "*Gnus Help*")
6520         (buffer-disable-undo (current-buffer))
6521         (erase-buffer)
6522         (setq groups (sort groups 'string<))
6523         (while groups
6524           ;; Groups may be entered twice into the list of groups.
6525           (if (not (string= (car groups) prev))
6526               (progn
6527                 (insert (setq prev (car groups)) "\n")
6528                 (if (and gnus-description-hashtb
6529                          (setq des (gnus-gethash (car groups)
6530                                                  gnus-description-hashtb)))
6531                     (insert "  " des "\n"))))
6532           (setq groups (cdr groups)))
6533         (goto-char (point-min))))
6534     (pop-to-buffer obuf)))
6535
6536 (defun gnus-group-description-apropos (regexp)
6537   "List all newsgroups that have names or descriptions that match a regexp."
6538   (interactive "sGnus description apropos (regexp): ")
6539   (if (not (or gnus-description-hashtb
6540                (gnus-read-all-descriptions-files)))
6541       (error "Couldn't request descriptions file"))
6542   (gnus-group-apropos regexp t))
6543
6544 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6545 (defun gnus-group-list-matching (level regexp &optional all lowest)
6546   "List all groups with unread articles that match REGEXP.
6547 If the prefix LEVEL is non-nil, it should be a number that says which
6548 level to cut off listing groups.
6549 If ALL, also list groups with no unread articles.
6550 If LOWEST, don't list groups with level lower than LOWEST.
6551
6552 This command may read the active file."
6553   (interactive "P\nsList newsgroups matching: ")
6554   ;; First make sure active file has been read.
6555   (when (and level
6556              (>= (prefix-numeric-value level) gnus-level-killed))
6557     (gnus-get-killed-groups))
6558   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6559                            all (or lowest 1) regexp)
6560   (goto-char (point-min))
6561   (gnus-group-position-point))
6562
6563 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6564   "List all groups that match REGEXP.
6565 If the prefix LEVEL is non-nil, it should be a number that says which
6566 level to cut off listing groups.
6567 If LOWEST, don't list groups with level lower than LOWEST."
6568   (interactive "P\nsList newsgroups matching: ")
6569   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6570
6571 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6572 (defun gnus-group-save-newsrc (&optional force)
6573   "Save the Gnus startup files.
6574 If FORCE, force saving whether it is necessary or not."
6575   (interactive "P")
6576   (gnus-save-newsrc-file force))
6577
6578 (defun gnus-group-restart (&optional arg)
6579   "Force Gnus to read the .newsrc file."
6580   (interactive "P")
6581   (when (gnus-yes-or-no-p
6582          (format "Are you sure you want to read %s? "
6583                  gnus-current-startup-file))
6584     (gnus-save-newsrc-file)
6585     (gnus-setup-news 'force)
6586     (gnus-group-list-groups arg)))
6587
6588 (defun gnus-group-read-init-file ()
6589   "Read the Gnus elisp init file."
6590   (interactive)
6591   (gnus-read-init-file))
6592
6593 (defun gnus-group-check-bogus-groups (&optional silent)
6594   "Check bogus newsgroups.
6595 If given a prefix, don't ask for confirmation before removing a bogus
6596 group."
6597   (interactive "P")
6598   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6599   (gnus-group-list-groups))
6600
6601 (defun gnus-group-edit-global-kill (&optional article group)
6602   "Edit the global kill file.
6603 If GROUP, edit that local kill file instead."
6604   (interactive "P")
6605   (setq gnus-current-kill-article article)
6606   (gnus-kill-file-edit-file group)
6607   (gnus-message
6608    6
6609    (substitute-command-keys
6610     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6611             (if group "local" "global")))))
6612
6613 (defun gnus-group-edit-local-kill (article group)
6614   "Edit a local kill file."
6615   (interactive (list nil (gnus-group-group-name)))
6616   (gnus-group-edit-global-kill article group))
6617
6618 (defun gnus-group-force-update ()
6619   "Update `.newsrc' file."
6620   (interactive)
6621   (gnus-save-newsrc-file))
6622
6623 (defun gnus-group-suspend ()
6624   "Suspend the current Gnus session.
6625 In fact, cleanup buffers except for group mode buffer.
6626 The hook gnus-suspend-gnus-hook is called before actually suspending."
6627   (interactive)
6628   (run-hooks 'gnus-suspend-gnus-hook)
6629   ;; Kill Gnus buffers except for group mode buffer.
6630   (let ((group-buf (get-buffer gnus-group-buffer)))
6631     ;; Do this on a separate list in case the user does a ^G before we finish
6632     (let ((gnus-buffer-list
6633            (delq group-buf (delq gnus-dribble-buffer
6634                                  (append gnus-buffer-list nil)))))
6635       (while gnus-buffer-list
6636         (gnus-kill-buffer (car gnus-buffer-list))
6637         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6638     (if group-buf
6639         (progn
6640           (setq gnus-buffer-list (list group-buf))
6641           (bury-buffer group-buf)
6642           (delete-windows-on group-buf t)))))
6643
6644 (defun gnus-group-clear-dribble ()
6645   "Clear all information from the dribble buffer."
6646   (interactive)
6647   (gnus-dribble-clear)
6648   (gnus-message 7 "Cleared dribble buffer"))
6649
6650 (defun gnus-group-exit ()
6651   "Quit reading news after updating .newsrc.eld and .newsrc.
6652 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6653   (interactive)
6654   (when 
6655       (or noninteractive                ;For gnus-batch-kill
6656           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
6657           (not gnus-interactive-exit)   ;Without confirmation
6658           gnus-expert-user
6659           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6660     (run-hooks 'gnus-exit-gnus-hook)
6661     ;; Offer to save data from non-quitted summary buffers.
6662     (gnus-offer-save-summaries)
6663     ;; Save the newsrc file(s).
6664     (gnus-save-newsrc-file)
6665     ;; Kill-em-all.
6666     (gnus-close-backends)
6667     ;; Reset everything.
6668     (gnus-clear-system)
6669     ;; Allow the user to do things after cleaning up.
6670     (run-hooks 'gnus-after-exiting-gnus-hook)))
6671
6672 (defun gnus-close-backends ()
6673   ;; Send a close request to all backends that support such a request.
6674   (let ((methods gnus-valid-select-methods)
6675         func)
6676     (while methods
6677       (if (fboundp (setq func (intern (concat (caar methods)
6678                                               "-request-close"))))
6679           (funcall func))
6680       (setq methods (cdr methods)))))
6681
6682 (defun gnus-group-quit ()
6683   "Quit reading news without updating .newsrc.eld or .newsrc.
6684 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6685   (interactive)
6686   (when (or noninteractive              ;For gnus-batch-kill
6687             (zerop (buffer-size))
6688             (not (gnus-server-opened gnus-select-method))
6689             gnus-expert-user
6690             (not gnus-current-startup-file)
6691             (gnus-yes-or-no-p
6692              (format "Quit reading news without saving %s? "
6693                      (file-name-nondirectory gnus-current-startup-file))))
6694     (run-hooks 'gnus-exit-gnus-hook)
6695     (if gnus-use-full-window
6696         (delete-other-windows)
6697       (gnus-remove-some-windows))
6698     (gnus-dribble-save)
6699     (gnus-close-backends)
6700     (gnus-clear-system)
6701     ;; Allow the user to do things after cleaning up.
6702     (run-hooks 'gnus-after-exiting-gnus-hook)))
6703
6704 (defun gnus-offer-save-summaries ()
6705   "Offer to save all active summary buffers."
6706   (save-excursion
6707     (let ((buflist (buffer-list))
6708           buffers bufname)
6709       ;; Go through all buffers and find all summaries.
6710       (while buflist
6711         (and (setq bufname (buffer-name (car buflist)))
6712              (string-match "Summary" bufname)
6713              (save-excursion
6714                (set-buffer bufname)
6715                ;; We check that this is, indeed, a summary buffer.
6716                (and (eq major-mode 'gnus-summary-mode)
6717                     ;; Also make sure this isn't bogus.
6718                     gnus-newsgroup-prepared))
6719              (push bufname buffers))
6720         (setq buflist (cdr buflist)))
6721       ;; Go through all these summary buffers and offer to save them.
6722       (when buffers
6723         (map-y-or-n-p
6724          "Update summary buffer %s? "
6725          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6726          buffers)))))
6727
6728 (defun gnus-group-describe-briefly ()
6729   "Give a one line description of the group mode commands."
6730   (interactive)
6731   (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")))
6732
6733 (defun gnus-group-browse-foreign-server (method)
6734   "Browse a foreign news server.
6735 If called interactively, this function will ask for a select method
6736  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6737 If not, METHOD should be a list where the first element is the method
6738 and the second element is the address."
6739   (interactive
6740    (list (let ((how (completing-read
6741                      "Which backend: "
6742                      (append gnus-valid-select-methods gnus-server-alist)
6743                      nil t (cons "nntp" 0) 'gnus-method-history)))
6744            ;; We either got a backend name or a virtual server name.
6745            ;; If the first, we also need an address.
6746            (if (assoc how gnus-valid-select-methods)
6747                (list (intern how)
6748                      ;; Suggested by mapjph@bath.ac.uk.
6749                      (completing-read
6750                       "Address: "
6751                       (mapcar (lambda (server) (list server))
6752                               gnus-secondary-servers)))
6753              ;; We got a server name, so we find the method.
6754              (gnus-server-to-method how)))))
6755   (gnus-browse-foreign-server method))
6756
6757 \f
6758 ;;;
6759 ;;; Gnus summary mode
6760 ;;;
6761
6762 (defvar gnus-summary-mode-map nil)
6763
6764 (put 'gnus-summary-mode 'mode-class 'special)
6765
6766 (unless gnus-summary-mode-map
6767   (setq gnus-summary-mode-map (make-keymap))
6768   (suppress-keymap gnus-summary-mode-map)
6769
6770   ;; Non-orthogonal keys
6771
6772   (gnus-define-keys gnus-summary-mode-map
6773     " " gnus-summary-next-page
6774     "\177" gnus-summary-prev-page
6775     [delete] gnus-summary-prev-page
6776     "\r" gnus-summary-scroll-up
6777     "n" gnus-summary-next-unread-article
6778     "p" gnus-summary-prev-unread-article
6779     "N" gnus-summary-next-article
6780     "P" gnus-summary-prev-article
6781     "\M-\C-n" gnus-summary-next-same-subject
6782     "\M-\C-p" gnus-summary-prev-same-subject
6783     "\M-n" gnus-summary-next-unread-subject
6784     "\M-p" gnus-summary-prev-unread-subject
6785     "." gnus-summary-first-unread-article
6786     "," gnus-summary-best-unread-article
6787     "\M-s" gnus-summary-search-article-forward
6788     "\M-r" gnus-summary-search-article-backward
6789     "<" gnus-summary-beginning-of-article
6790     ">" gnus-summary-end-of-article
6791     "j" gnus-summary-goto-article
6792     "^" gnus-summary-refer-parent-article
6793     "\M-^" gnus-summary-refer-article
6794     "u" gnus-summary-tick-article-forward
6795     "!" gnus-summary-tick-article-forward
6796     "U" gnus-summary-tick-article-backward
6797     "d" gnus-summary-mark-as-read-forward
6798     "D" gnus-summary-mark-as-read-backward
6799     "E" gnus-summary-mark-as-expirable
6800     "\M-u" gnus-summary-clear-mark-forward
6801     "\M-U" gnus-summary-clear-mark-backward
6802     "k" gnus-summary-kill-same-subject-and-select
6803     "\C-k" gnus-summary-kill-same-subject
6804     "\M-\C-k" gnus-summary-kill-thread
6805     "\M-\C-l" gnus-summary-lower-thread
6806     "e" gnus-summary-edit-article
6807     "#" gnus-summary-mark-as-processable
6808     "\M-#" gnus-summary-unmark-as-processable
6809     "\M-\C-t" gnus-summary-toggle-threads
6810     "\M-\C-s" gnus-summary-show-thread
6811     "\M-\C-h" gnus-summary-hide-thread
6812     "\M-\C-f" gnus-summary-next-thread
6813     "\M-\C-b" gnus-summary-prev-thread
6814     "\M-\C-u" gnus-summary-up-thread
6815     "\M-\C-d" gnus-summary-down-thread
6816     "&" gnus-summary-execute-command
6817     "c" gnus-summary-catchup-and-exit
6818     "\C-w" gnus-summary-mark-region-as-read
6819     "\C-t" gnus-summary-toggle-truncation
6820     "?" gnus-summary-mark-as-dormant
6821     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6822     "\C-c\C-s\C-n" gnus-summary-sort-by-number
6823     "\C-c\C-s\C-a" gnus-summary-sort-by-author
6824     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6825     "\C-c\C-s\C-d" gnus-summary-sort-by-date
6826     "\C-c\C-s\C-i" gnus-summary-sort-by-score
6827     "=" gnus-summary-expand-window
6828     "\C-x\C-s" gnus-summary-reselect-current-group
6829     "\M-g" gnus-summary-rescan-group
6830     "w" gnus-summary-stop-page-breaking
6831     "\C-c\C-r" gnus-summary-caesar-message
6832     "\M-t" gnus-summary-toggle-mime
6833     "f" gnus-summary-followup
6834     "F" gnus-summary-followup-with-original
6835     "C" gnus-summary-cancel-article
6836     "r" gnus-summary-reply
6837     "R" gnus-summary-reply-with-original
6838     "\C-c\C-f" gnus-summary-mail-forward
6839     "o" gnus-summary-save-article
6840     "\C-o" gnus-summary-save-article-mail
6841     "|" gnus-summary-pipe-output
6842     "\M-k" gnus-summary-edit-local-kill
6843     "\M-K" gnus-summary-edit-global-kill
6844     "V" gnus-version
6845     "\C-c\C-d" gnus-summary-describe-group
6846     "q" gnus-summary-exit
6847     "Q" gnus-summary-exit-no-update
6848     "\C-c\C-i" gnus-info-find-node
6849     gnus-mouse-2 gnus-mouse-pick-article
6850     "m" gnus-summary-mail-other-window
6851     "a" gnus-summary-post-news
6852     "x" gnus-summary-limit-to-unread
6853     "s" gnus-summary-isearch-article
6854     "t" gnus-article-hide-headers
6855     "g" gnus-summary-show-article
6856     "l" gnus-summary-goto-last-article
6857     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6858     "\C-d" gnus-summary-enter-digest-group
6859     "\C-c\C-b" gnus-bug
6860     "*" gnus-cache-enter-article
6861     "\M-*" gnus-cache-remove-article
6862     "\M-&" gnus-summary-universal-argument
6863     "\C-l" gnus-recenter
6864     "I" gnus-summary-increase-score
6865     "L" gnus-summary-lower-score
6866
6867     "V" gnus-summary-score-map
6868     "X" gnus-uu-extract-map
6869     "S" gnus-summary-send-map)
6870
6871   ;; Sort of orthogonal keymap
6872   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
6873     "t" gnus-summary-tick-article-forward
6874     "!" gnus-summary-tick-article-forward
6875     "d" gnus-summary-mark-as-read-forward
6876     "r" gnus-summary-mark-as-read-forward
6877     "c" gnus-summary-clear-mark-forward
6878     " " gnus-summary-clear-mark-forward
6879     "e" gnus-summary-mark-as-expirable
6880     "x" gnus-summary-mark-as-expirable
6881     "?" gnus-summary-mark-as-dormant
6882     "b" gnus-summary-set-bookmark
6883     "B" gnus-summary-remove-bookmark
6884     "#" gnus-summary-mark-as-processable
6885     "\M-#" gnus-summary-unmark-as-processable
6886     "S" gnus-summary-limit-include-expunged
6887     "C" gnus-summary-catchup
6888     "H" gnus-summary-catchup-to-here
6889     "\C-c" gnus-summary-catchup-all
6890     "k" gnus-summary-kill-same-subject-and-select
6891     "K" gnus-summary-kill-same-subject
6892     "P" gnus-uu-mark-map)
6893
6894   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
6895     "c" gnus-summary-clear-above
6896     "u" gnus-summary-tick-above
6897     "m" gnus-summary-mark-above
6898     "k" gnus-summary-kill-below)
6899
6900   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
6901     "/" gnus-summary-limit-to-subject
6902     "n" gnus-summary-limit-to-articles
6903     "w" gnus-summary-pop-limit
6904     "s" gnus-summary-limit-to-subject
6905     "a" gnus-summary-limit-to-author
6906     "u" gnus-summary-limit-to-unread
6907     "m" gnus-summary-limit-to-marks
6908     "v" gnus-summary-limit-to-score
6909     "D" gnus-summary-limit-include-dormant
6910     "d" gnus-summary-limit-exclude-dormant
6911     ;;  "t" gnus-summary-limit-exclude-thread
6912     "E" gnus-summary-limit-include-expunged
6913     "c" gnus-summary-limit-exclude-childless-dormant
6914     "C" gnus-summary-limit-mark-excluded-as-read)
6915
6916   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
6917     "n" gnus-summary-next-unread-article
6918     "p" gnus-summary-prev-unread-article
6919     "N" gnus-summary-next-article
6920     "P" gnus-summary-prev-article
6921     "\C-n" gnus-summary-next-same-subject
6922     "\C-p" gnus-summary-prev-same-subject
6923     "\M-n" gnus-summary-next-unread-subject
6924     "\M-p" gnus-summary-prev-unread-subject
6925     "f" gnus-summary-first-unread-article
6926     "b" gnus-summary-best-unread-article
6927     "j" gnus-summary-goto-article
6928     "g" gnus-summary-goto-subject
6929     "l" gnus-summary-goto-last-article
6930     "p" gnus-summary-pop-article)
6931
6932   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
6933     "k" gnus-summary-kill-thread
6934     "l" gnus-summary-lower-thread
6935     "i" gnus-summary-raise-thread
6936     "T" gnus-summary-toggle-threads
6937     "t" gnus-summary-rethread-current
6938     "^" gnus-summary-reparent-thread
6939     "s" gnus-summary-show-thread
6940     "S" gnus-summary-show-all-threads
6941     "h" gnus-summary-hide-thread
6942     "H" gnus-summary-hide-all-threads
6943     "n" gnus-summary-next-thread
6944     "p" gnus-summary-prev-thread
6945     "u" gnus-summary-up-thread
6946     "o" gnus-summary-top-thread
6947     "d" gnus-summary-down-thread
6948     "#" gnus-uu-mark-thread
6949     "\M-#" gnus-uu-unmark-thread)
6950
6951   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
6952     "c" gnus-summary-catchup-and-exit
6953     "C" gnus-summary-catchup-all-and-exit
6954     "E" gnus-summary-exit-no-update
6955     "Q" gnus-summary-exit
6956     "Z" gnus-summary-exit
6957     "n" gnus-summary-catchup-and-goto-next-group
6958     "R" gnus-summary-reselect-current-group
6959     "G" gnus-summary-rescan-group
6960     "N" gnus-summary-next-group
6961     "P" gnus-summary-prev-group)
6962
6963   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
6964     " " gnus-summary-next-page
6965     "n" gnus-summary-next-page
6966     "\177" gnus-summary-prev-page
6967     [delete] gnus-summary-prev-page
6968     "p" gnus-summary-prev-page
6969     "\r" gnus-summary-scroll-up
6970     "<" gnus-summary-beginning-of-article
6971     ">" gnus-summary-end-of-article
6972     "b" gnus-summary-beginning-of-article
6973     "e" gnus-summary-end-of-article
6974     "^" gnus-summary-refer-parent-article
6975     "r" gnus-summary-refer-parent-article
6976     "R" gnus-summary-refer-references
6977     "g" gnus-summary-show-article
6978     "s" gnus-summary-isearch-article)
6979
6980   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
6981     "b" gnus-article-add-buttons
6982     "B" gnus-article-add-buttons-to-head
6983     "o" gnus-article-treat-overstrike
6984     ;;  "w" gnus-article-word-wrap
6985     "w" gnus-article-fill-cited-article
6986     "c" gnus-article-remove-cr
6987     "L" gnus-article-remove-trailing-blank-lines
6988     "q" gnus-article-de-quoted-unreadable
6989     "f" gnus-article-display-x-face
6990     "l" gnus-summary-stop-page-breaking
6991     "r" gnus-summary-caesar-message
6992     "t" gnus-article-hide-headers
6993     "v" gnus-summary-verbose-headers
6994     "m" gnus-summary-toggle-mime)
6995
6996   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
6997     "a" gnus-article-hide
6998     "h" gnus-article-hide-headers
6999     "b" gnus-article-hide-boring-headers
7000     "s" gnus-article-hide-signature
7001     "c" gnus-article-hide-citation
7002     "p" gnus-article-hide-pgp
7003     "\C-c" gnus-article-hide-citation-maybe)
7004
7005   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
7006     "a" gnus-article-highlight
7007     "h" gnus-article-highlight-headers
7008     "c" gnus-article-highlight-citation
7009     "s" gnus-article-highlight-signature)
7010
7011   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
7012     "z" gnus-article-date-ut
7013     "u" gnus-article-date-ut
7014     "l" gnus-article-date-local
7015     "e" gnus-article-date-lapsed
7016     "o" gnus-article-date-original)
7017
7018   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
7019     "v" gnus-version
7020     "f" gnus-summary-fetch-faq
7021     "d" gnus-summary-describe-group
7022     "h" gnus-summary-describe-briefly
7023     "i" gnus-info-find-node)
7024
7025   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
7026     "e" gnus-summary-expire-articles
7027     "\M-\C-e" gnus-summary-expire-articles-now
7028     "\177" gnus-summary-delete-article
7029     [delete] gnus-summary-delete-article
7030     "m" gnus-summary-move-article
7031     "r" gnus-summary-respool-article
7032     "w" gnus-summary-edit-article
7033     "c" gnus-summary-copy-article
7034     "B" gnus-summary-crosspost-article
7035     "q" gnus-summary-respool-query
7036     "i" gnus-summary-import-article)
7037
7038   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
7039     "o" gnus-summary-save-article
7040     "m" gnus-summary-save-article-mail
7041     "r" gnus-summary-save-article-rmail
7042     "f" gnus-summary-save-article-file
7043     "b" gnus-summary-save-article-body-file
7044     "h" gnus-summary-save-article-folder
7045     "v" gnus-summary-save-article-vm
7046     "p" gnus-summary-pipe-output
7047     "s" gnus-soup-add-article)
7048   )
7049
7050 \f
7051
7052 (defun gnus-summary-mode (&optional group)
7053   "Major mode for reading articles.
7054
7055 All normal editing commands are switched off.
7056 \\<gnus-summary-mode-map>
7057 Each line in this buffer represents one article.  To read an
7058 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
7059 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
7060 respectively.
7061
7062 You can also post articles and send mail from this buffer.  To
7063 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
7064 of an article, type `\\[gnus-summary-reply]'.
7065
7066 There are approx. one gazillion commands you can execute in this
7067 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
7068
7069 The following commands are available:
7070
7071 \\{gnus-summary-mode-map}"
7072   (interactive)
7073   (when (and menu-bar-mode
7074              (gnus-visual-p 'summary-menu 'menu))
7075     (gnus-summary-make-menu-bar))
7076   (kill-all-local-variables)
7077   (let ((locals gnus-summary-local-variables))
7078     (while locals
7079       (if (consp (car locals))
7080           (progn
7081             (make-local-variable (caar locals))
7082             (set (caar locals) (eval (cdar locals))))
7083         (make-local-variable (car locals))
7084         (set (car locals) nil))
7085       (setq locals (cdr locals))))
7086   (gnus-make-thread-indent-array)
7087   (gnus-simplify-mode-line)
7088   (setq major-mode 'gnus-summary-mode)
7089   (setq mode-name "Summary")
7090   (make-local-variable 'minor-mode-alist)
7091   (use-local-map gnus-summary-mode-map)
7092   (buffer-disable-undo (current-buffer))
7093   (setq buffer-read-only t)             ;Disable modification
7094   (setq truncate-lines t)
7095   (setq selective-display t)
7096   (setq selective-display-ellipses t)   ;Display `...'
7097   (setq buffer-display-table gnus-summary-display-table)
7098   (setq gnus-newsgroup-name group)
7099   (make-local-variable 'gnus-summary-line-format)
7100   (make-local-variable 'gnus-summary-line-format-spec)
7101   (make-local-variable 'gnus-summary-mark-positions)
7102   (run-hooks 'gnus-summary-mode-hook))
7103
7104 (defun gnus-summary-make-display-table ()
7105   ;; Change the display table.  Odd characters have a tendency to mess
7106   ;; up nicely formatted displays - we make all possible glyphs
7107   ;; display only a single character.
7108
7109   ;; We start from the standard display table, if any.
7110   (setq gnus-summary-display-table
7111         (or (copy-sequence standard-display-table)
7112             (make-display-table)))
7113   ;; Nix out all the control chars...
7114   (let ((i 32))
7115     (while (>= (setq i (1- i)) 0)
7116       (aset gnus-summary-display-table i [??])))
7117   ;; ... but not newline and cr, of course. (cr is necessary for the
7118   ;; selective display).
7119   (aset gnus-summary-display-table ?\n nil)
7120   (aset gnus-summary-display-table ?\r nil)
7121   ;; We nix out any glyphs over 126 that are not set already.
7122   (let ((i 256))
7123     (while (>= (setq i (1- i)) 127)
7124       ;; Only modify if the entry is nil.
7125       (or (aref gnus-summary-display-table i)
7126           (aset gnus-summary-display-table i [??])))))
7127
7128 (defun gnus-summary-clear-local-variables ()
7129   (let ((locals gnus-summary-local-variables))
7130     (while locals
7131       (if (consp (car locals))
7132           (and (vectorp (caar locals))
7133                (set (caar locals) nil))
7134         (and (vectorp (car locals))
7135              (set (car locals) nil)))
7136       (setq locals (cdr locals)))))
7137
7138 ;; Summary data functions.
7139
7140 (defmacro gnus-data-number (data)
7141   `(car ,data))
7142
7143 (defmacro gnus-data-set-number (data number)
7144   `(setcar ,data ,number))
7145
7146 (defmacro gnus-data-mark (data)
7147   `(nth 1 ,data))
7148
7149 (defmacro gnus-data-set-mark (data mark)
7150   `(setcar (nthcdr 1 ,data) ,mark))
7151
7152 (defmacro gnus-data-pos (data)
7153   `(nth 2 ,data))
7154
7155 (defmacro gnus-data-set-pos (data pos)
7156   `(setcar (nthcdr 2 ,data) ,pos))
7157
7158 (defmacro gnus-data-header (data)
7159   `(nth 3 ,data))
7160
7161 (defmacro gnus-data-level (data)
7162   `(nth 4 ,data))
7163
7164 (defmacro gnus-data-unread-p (data)
7165   `(= (nth 1 ,data) gnus-unread-mark))
7166
7167 (defmacro gnus-data-pseudo-p (data)
7168   `(consp (nth 3 ,data)))
7169
7170 (defmacro gnus-data-find (number)
7171   `(assq ,number gnus-newsgroup-data))
7172
7173 (defmacro gnus-data-find-list (number &optional data)
7174   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7175      (memq (assq ,number bdata)
7176            bdata)))
7177
7178 (defmacro gnus-data-make (number mark pos header level)
7179   `(list ,number ,mark ,pos ,header ,level))
7180
7181 (defun gnus-data-enter (after-article number mark pos header level offset)
7182   (let ((data (gnus-data-find-list after-article)))
7183     (or data (error "No such article: %d" after-article))
7184     (setcdr data (cons (gnus-data-make number mark pos header level)
7185                        (cdr data)))
7186     (setq gnus-newsgroup-data-reverse nil)
7187     (gnus-data-update-list (cddr data) offset)))
7188
7189 (defun gnus-data-enter-list (after-article list &optional offset)
7190   (when list
7191     (let ((data (and after-article (gnus-data-find-list after-article)))
7192           (ilist list))
7193       (or data (not after-article) (error "No such article: %d" after-article))
7194       ;; Find the last element in the list to be spliced into the main
7195       ;; list.
7196       (while (cdr list)
7197         (setq list (cdr list)))
7198       (if (not data)
7199           (progn
7200             (setcdr list gnus-newsgroup-data)
7201             (setq gnus-newsgroup-data ilist)
7202             (and offset (gnus-data-update-list (cdr list) offset)))
7203         (setcdr list (cdr data))
7204         (setcdr data ilist)
7205         (and offset (gnus-data-update-list (cdr data) offset)))
7206       (setq gnus-newsgroup-data-reverse nil))))
7207
7208 (defun gnus-data-remove (article &optional offset)
7209   (let ((data gnus-newsgroup-data))
7210     (if (= (gnus-data-number (car data)) article)
7211         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7212               gnus-newsgroup-data-reverse nil)
7213       (while (cdr data)
7214         (and (= (gnus-data-number (cadr data)) article)
7215              (progn
7216                (setcdr data (cddr data))
7217                (and offset (gnus-data-update-list (cdr data) offset))
7218                (setq data nil
7219                      gnus-newsgroup-data-reverse nil)))
7220         (setq data (cdr data))))))
7221
7222 (defmacro gnus-data-list (backward)
7223   `(if ,backward
7224        (or gnus-newsgroup-data-reverse
7225            (setq gnus-newsgroup-data-reverse
7226                  (reverse gnus-newsgroup-data)))
7227      gnus-newsgroup-data))
7228
7229 (defun gnus-data-update-list (data offset)
7230   "Add OFFSET to the POS of all data entries in DATA."
7231   (while data
7232     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7233     (setq data (cdr data))))
7234
7235 (defun gnus-data-compute-positions ()
7236   "Compute the positions of all articles."
7237   (let ((data gnus-newsgroup-data)
7238         pos)
7239     (while data
7240       (when (setq pos (text-property-any
7241                        (point-min) (point-max)
7242                        'gnus-number (gnus-data-number (car data))))
7243         (gnus-data-set-pos (car data) (+ pos 3)))
7244       (setq data (cdr data)))))
7245
7246 (defun gnus-summary-article-pseudo-p (article)
7247   "Say whether this article is a pseudo article or not."
7248   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7249
7250 (defun gnus-article-parent-p (number)
7251   "Say whether this article is a parent or not."
7252   (let ((data (gnus-data-find-list number)))
7253     (and (cdr data)                     ; There has to be an article after...
7254          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7255             (gnus-data-level (nth 1 data))))))
7256
7257 (defun gnus-article-children (number)
7258   "Return a list of all children to NUMBER."
7259   (let* ((data (gnus-data-find-list number))
7260          (level (gnus-data-level (car data)))
7261          children)
7262     (setq data (cdr data))
7263     (while (and data            
7264                 (= (gnus-data-level (car data)) (1+ level)))
7265       (push (gnus-data-number (car data)) children)
7266       (setq data (cdr data)))
7267     children))
7268
7269 (defmacro gnus-summary-skip-intangible ()
7270   "If the current article is intangible, then jump to a different article."
7271   '(let ((to (get-text-property (point) 'gnus-intangible)))
7272     (and to (gnus-summary-goto-subject to))))
7273
7274 (defmacro gnus-summary-article-intangible-p ()
7275   "Say whether this article is intangible or not."
7276   '(get-text-property (point) 'gnus-intangible))
7277
7278 ;; Some summary mode macros.
7279
7280 (defmacro gnus-summary-article-number ()
7281   "The article number of the article on the current line.
7282 If there isn's an article number here, then we return the current
7283 article number."
7284   '(progn
7285      (gnus-summary-skip-intangible)
7286      (or (get-text-property (point) 'gnus-number)
7287          (gnus-summary-last-subject))))
7288
7289 (defmacro gnus-summary-article-header (&optional number)
7290   `(gnus-data-header (gnus-data-find
7291                       ,(or number '(gnus-summary-article-number)))))
7292
7293 (defmacro gnus-summary-thread-level (&optional number)
7294   `(if (and (eq gnus-summary-make-false-root 'dummy)
7295             (get-text-property (point) 'gnus-intangible))
7296        0
7297      (gnus-data-level (gnus-data-find
7298                        ,(or number '(gnus-summary-article-number))))))
7299
7300 (defmacro gnus-summary-article-mark (&optional number)
7301   `(gnus-data-mark (gnus-data-find
7302                     ,(or number '(gnus-summary-article-number)))))
7303
7304 (defmacro gnus-summary-article-pos (&optional number)
7305   `(gnus-data-pos (gnus-data-find
7306                    ,(or number '(gnus-summary-article-number)))))
7307
7308 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7309 (defmacro gnus-summary-article-subject (&optional number)
7310   "Return current subject string or nil if nothing."
7311   `(let ((headers
7312           ,(if number
7313                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7314              '(gnus-data-header (assq (gnus-summary-article-number)
7315                                       gnus-newsgroup-data)))))
7316      (and headers
7317           (vectorp headers)
7318           (mail-header-subject headers))))
7319
7320 (defmacro gnus-summary-article-score (&optional number)
7321   "Return current article score."
7322   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7323                   gnus-newsgroup-scored))
7324        gnus-summary-default-score 0))
7325
7326 (defun gnus-summary-article-children (&optional number)
7327   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7328          (level (gnus-data-level (car data)))
7329          l children)
7330     (while (and (setq data (cdr data))
7331                 (> (setq l (gnus-data-level (car data))) level))
7332       (and (= (1+ level) l)
7333            (setq children (cons (gnus-data-number (car data))
7334                                 children))))
7335     (nreverse children)))
7336
7337 (defun gnus-summary-article-parent (&optional number)
7338   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7339                                     (gnus-data-list t)))
7340          (level (gnus-data-level (car data))))
7341     (if (zerop level)
7342         () ; This is a root.
7343       ;; We search until we find an article with a level less than
7344       ;; this one.  That function has to be the parent.
7345       (while (and (setq data (cdr data))
7346                   (not (< (gnus-data-level (car data)) level))))
7347       (and data (gnus-data-number (car data))))))
7348
7349 (defun gnus-unread-mark-p (mark)
7350   "Say whether MARK is the unread mark."
7351   (= mark gnus-unread-mark))
7352
7353 (defun gnus-read-mark-p (mark)
7354   "Say whether MARK is one of the marks that mark as read.
7355 This is all marks except unread, ticked, dormant, and expirable."
7356   (not (or (= mark gnus-unread-mark)
7357            (= mark gnus-ticked-mark)
7358            (= mark gnus-dormant-mark)
7359            (= mark gnus-expirable-mark))))
7360
7361 ;; Various summary mode internalish functions.
7362
7363 (defun gnus-mouse-pick-article (e)
7364   (interactive "e")
7365   (mouse-set-point e)
7366   (gnus-summary-next-page nil t))
7367
7368 (defun gnus-summary-setup-buffer (group)
7369   "Initialize summary buffer."
7370   (let ((buffer (concat "*Summary " group "*")))
7371     (if (get-buffer buffer)
7372         (progn
7373           (set-buffer buffer)
7374           (setq gnus-summary-buffer (current-buffer))
7375           (not gnus-newsgroup-prepared))
7376       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7377       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7378       (gnus-add-current-to-buffer-list)
7379       (gnus-summary-mode group)
7380       (when gnus-carpal
7381         (gnus-carpal-setup-buffer 'summary))
7382       (unless gnus-single-article-buffer
7383         (make-local-variable 'gnus-article-buffer)
7384         (make-local-variable 'gnus-article-current)
7385         (make-local-variable 'gnus-original-article-buffer))
7386       (setq gnus-newsgroup-name group)
7387       t)))
7388
7389 (defun gnus-set-global-variables ()
7390   ;; Set the global equivalents of the summary buffer-local variables
7391   ;; to the latest values they had.  These reflect the summary buffer
7392   ;; that was in action when the last article was fetched.
7393   (when (eq major-mode 'gnus-summary-mode)
7394     (setq gnus-summary-buffer (current-buffer))
7395     (let ((name gnus-newsgroup-name)
7396           (marked gnus-newsgroup-marked)
7397           (unread gnus-newsgroup-unreads)
7398           (headers gnus-current-headers)
7399           (data gnus-newsgroup-data)
7400           (summary gnus-summary-buffer)
7401           (article-buffer gnus-article-buffer)
7402           (original gnus-original-article-buffer)
7403           (gac gnus-article-current)
7404           (score-file gnus-current-score-file))
7405       (save-excursion
7406         (set-buffer gnus-group-buffer)
7407         (setq gnus-newsgroup-name name)
7408         (setq gnus-newsgroup-marked marked)
7409         (setq gnus-newsgroup-unreads unread)
7410         (setq gnus-current-headers headers)
7411         (setq gnus-newsgroup-data data)
7412         (setq gnus-article-current gac)
7413         (setq gnus-summary-buffer summary)
7414         (setq gnus-article-buffer article-buffer)
7415         (setq gnus-original-article-buffer original)
7416         (setq gnus-current-score-file score-file)))))
7417
7418 (defun gnus-summary-last-article-p (&optional article)
7419   "Return whether ARTICLE is the last article in the buffer."
7420   (if (not (setq article (or article (gnus-summary-article-number))))
7421       t ; All non-existant numbers are the last article. :-)
7422     (not (cdr (gnus-data-find-list article)))))
7423
7424 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7425   "Insert a dummy root in the summary buffer."
7426   (beginning-of-line)
7427   (gnus-add-text-properties
7428    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7429    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7430
7431 (defun gnus-make-thread-indent-array ()
7432   (let ((n 200))
7433     (unless (and gnus-thread-indent-array
7434                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
7435       (setq gnus-thread-indent-array (make-vector 201 "")
7436             gnus-thread-indent-array-level gnus-thread-indent-level)
7437       (while (>= n 0)
7438         (aset gnus-thread-indent-array n
7439               (make-string (* n gnus-thread-indent-level) ? ))
7440         (setq n (1- n))))))
7441
7442 (defun gnus-summary-insert-line
7443   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7444                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7445                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7446   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7447          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7448          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7449          (gnus-tmp-score-char
7450           (if (or (null gnus-summary-default-score)
7451                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7452                       gnus-summary-zcore-fuzz)) ? 
7453             (if (< gnus-tmp-score gnus-summary-default-score)
7454                 gnus-score-below-mark gnus-score-over-mark)))
7455          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7456                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7457                                   gnus-cached-mark)
7458                                  (gnus-tmp-replied gnus-replied-mark)
7459                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7460                                   gnus-saved-mark)
7461                                  (t gnus-unread-mark)))
7462          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7463          (gnus-tmp-name
7464           (cond
7465            ((string-match "(.+)" gnus-tmp-from)
7466             (substring gnus-tmp-from
7467                        (1+ (match-beginning 0)) (1- (match-end 0))))
7468            ((string-match "<[^>]+> *$" gnus-tmp-from)
7469             (let ((beg (match-beginning 0)))
7470               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7471                        (substring gnus-tmp-from (1+ (match-beginning 0))
7472                                   (1- (match-end 0))))
7473                   (substring gnus-tmp-from 0 beg))))
7474            (t gnus-tmp-from)))
7475          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7476          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7477          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7478          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7479          (buffer-read-only nil))
7480     (when (string= gnus-tmp-name "")
7481       (setq gnus-tmp-name gnus-tmp-from))
7482     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7483     (gnus-put-text-property
7484      (point)
7485      (progn (eval gnus-summary-line-format-spec) (point))
7486      'gnus-number gnus-tmp-number)
7487     (when (gnus-visual-p 'summary-highlight 'highlight)
7488       (forward-line -1)
7489       (run-hooks 'gnus-summary-update-hook)
7490       (forward-line 1))))
7491
7492 (defun gnus-summary-update-line (&optional dont-update)
7493   ;; Update summary line after change.
7494   (when (and gnus-summary-default-score
7495              (not gnus-summary-inhibit-highlight))
7496     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7497            (article (gnus-summary-article-number))
7498            (score (gnus-summary-article-score article)))
7499       (unless dont-update
7500         (if (and gnus-summary-mark-below
7501                  (< (gnus-summary-article-score)
7502                     gnus-summary-mark-below))
7503             ;; This article has a low score, so we mark it as read.
7504             (when (memq article gnus-newsgroup-unreads)
7505               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7506           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7507             ;; This article was previously marked as read on account
7508             ;; of a low score, but now it has risen, so we mark it as
7509             ;; unread.
7510             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7511         (gnus-summary-update-mark
7512          (if (or (null gnus-summary-default-score)
7513                  (<= (abs (- score gnus-summary-default-score))
7514                      gnus-summary-zcore-fuzz)) ? 
7515            (if (< score gnus-summary-default-score)
7516                gnus-score-below-mark gnus-score-over-mark)) 'score))
7517       ;; Do visual highlighting.
7518       (when (gnus-visual-p 'summary-highlight 'highlight)
7519         (run-hooks 'gnus-summary-update-hook)))))
7520
7521 (defvar gnus-tmp-new-adopts nil)
7522
7523 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7524   ;; Sum up all elements (and sub-elements) in a list.
7525   (let* ((number
7526           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7527           (cond
7528            ((and (consp thread) (cdr thread))
7529             (apply
7530              '+ 1 (mapcar
7531                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7532            ((null thread)
7533             1)
7534            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7535             1)
7536            (t 1))))
7537     (when (and level (zerop level) gnus-tmp-new-adopts)
7538       (incf number
7539             (apply '+ (mapcar
7540                        'gnus-summary-number-of-articles-in-thread
7541                        gnus-tmp-new-adopts))))
7542     (if char
7543         (if (> number 1) gnus-not-empty-thread-mark
7544           gnus-empty-thread-mark)
7545       number)))
7546
7547 (defun gnus-summary-set-local-parameters (group)
7548  "Go through the local params of GROUP and set all variable specs in that list."
7549   (let ((params (gnus-info-params (gnus-get-info group)))
7550         elem)
7551     (while params
7552       (setq elem (car params)
7553             params (cdr params))
7554       (and (consp elem)                 ; Has to be a cons.
7555            (consp (cdr elem))           ; The cdr has to be a list.
7556            (symbolp (car elem))         ; Has to be a symbol in there.
7557            (not (memq (car elem) 
7558                       '(quit-config to-address to-list to-group)))
7559            (progn                       ; So we set it.
7560              (make-local-variable (car elem))
7561              (set (car elem) (eval (nth 1 elem))))))))
7562
7563 (defun gnus-summary-read-group (group &optional show-all no-article
7564                                       kill-buffer no-display)
7565   "Start reading news in newsgroup GROUP.
7566 If SHOW-ALL is non-nil, already read articles are also listed.
7567 If NO-ARTICLE is non-nil, no article is selected initially.
7568 If NO-DISPLAY, don't generate a summary buffer."
7569   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7570   (let* ((new-group (gnus-summary-setup-buffer group))
7571          (quit-config (gnus-group-quit-config group))
7572          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7573     (cond
7574      ;; This summary buffer exists already, so we just select it.
7575      ((not new-group)
7576       (gnus-set-global-variables)
7577       (when kill-buffer
7578         (gnus-kill-or-deaden-summary kill-buffer))
7579       (gnus-configure-windows 'summary 'force)
7580       (gnus-set-mode-line 'summary)
7581       (gnus-summary-position-point)
7582       (message "")
7583       t)
7584      ;; We couldn't select this group.
7585      ((null did-select)
7586       (when (and (eq major-mode 'gnus-summary-mode)
7587                  (not (equal (current-buffer) kill-buffer)))
7588         (kill-buffer (current-buffer))
7589         (if (not quit-config)
7590             (progn
7591               (set-buffer gnus-group-buffer)
7592               (gnus-group-jump-to-group group)
7593               (gnus-group-next-unread-group 1))
7594           (if (not (buffer-name (car quit-config)))
7595               (gnus-configure-windows 'group 'force)
7596             (set-buffer (car quit-config))
7597             (and (eq major-mode 'gnus-summary-mode)
7598                  (gnus-set-global-variables))
7599             (gnus-configure-windows (cdr quit-config)))))
7600       (gnus-message 3 "Can't select group")
7601       nil)
7602      ;; The user did a `C-g' while prompting for number of articles,
7603      ;; so we exit this group.
7604      ((eq did-select 'quit)
7605       (and (eq major-mode 'gnus-summary-mode)
7606            (not (equal (current-buffer) kill-buffer))
7607            (kill-buffer (current-buffer)))
7608       (when kill-buffer
7609         (gnus-kill-or-deaden-summary kill-buffer))
7610       (if (not quit-config)
7611           (progn
7612             (set-buffer gnus-group-buffer)
7613             (gnus-group-jump-to-group group)
7614             (gnus-group-next-unread-group 1)
7615             (gnus-configure-windows 'group 'force))
7616         (if (not (buffer-name (car quit-config)))
7617             (gnus-configure-windows 'group 'force)
7618           (set-buffer (car quit-config))
7619           (and (eq major-mode 'gnus-summary-mode)
7620                (gnus-set-global-variables))
7621           (gnus-configure-windows (cdr quit-config))))
7622       ;; Finally signal the quit.
7623       (signal 'quit nil))
7624      ;; The group was successfully selected.
7625      (t
7626       (gnus-set-global-variables)
7627       ;; Save the active value in effect when the group was entered.
7628       (setq gnus-newsgroup-active
7629             (gnus-copy-sequence
7630              (gnus-active gnus-newsgroup-name)))
7631       ;; You can change the summary buffer in some way with this hook.
7632       (run-hooks 'gnus-select-group-hook)
7633       ;; Set any local variables in the group parameters.
7634       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7635       (gnus-update-format-specifications)
7636       ;; Do score processing.
7637       (when gnus-use-scoring
7638         (gnus-possibly-score-headers))
7639       ;; Check whether to fill in the gaps in the threads.
7640       (when gnus-build-sparse-threads
7641         (gnus-build-sparse-threads))
7642       ;; Find the initial limit.
7643       (if gnus-show-threads
7644           (if show-all
7645               (let ((gnus-newsgroup-dormant nil))
7646                 (gnus-summary-initial-limit show-all))
7647             (gnus-summary-initial-limit show-all))
7648         (setq gnus-newsgroup-limit 
7649               (mapcar 
7650                (lambda (header) (mail-header-number header))
7651                gnus-newsgroup-headers)))
7652       ;; Generate the summary buffer.
7653       (unless no-display
7654         (gnus-summary-prepare))
7655       (when gnus-use-trees
7656         (gnus-tree-open group)
7657         (setq gnus-summary-highlight-line-function
7658               'gnus-tree-highlight-article))
7659       ;; If the summary buffer is empty, but there are some low-scored
7660       ;; articles or some excluded dormants, we include these in the
7661       ;; buffer.
7662       (when (and (zerop (buffer-size))
7663                  (not no-display))
7664         (cond (gnus-newsgroup-dormant
7665                (gnus-summary-limit-include-dormant))
7666               ((and gnus-newsgroup-scored show-all)
7667                (gnus-summary-limit-include-expunged))))
7668       ;; Function `gnus-apply-kill-file' must be called in this hook.
7669       (run-hooks 'gnus-apply-kill-hook)
7670       (if (and (zerop (buffer-size))
7671                (not no-display))
7672           (progn
7673             ;; This newsgroup is empty.
7674             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7675             (gnus-message 6 "No unread news")
7676             (when kill-buffer
7677               (gnus-kill-or-deaden-summary kill-buffer))
7678             ;; Return nil from this function.
7679             nil)
7680         ;; Hide conversation thread subtrees.  We cannot do this in
7681         ;; gnus-summary-prepare-hook since kill processing may not
7682         ;; work with hidden articles.
7683         (and gnus-show-threads
7684              gnus-thread-hide-subtree
7685              (gnus-summary-hide-all-threads))
7686         ;; Show first unread article if requested.
7687         (if (and (not no-article)
7688                  (not no-display)
7689                  gnus-newsgroup-unreads
7690                  gnus-auto-select-first)
7691             (unless (if (eq gnus-auto-select-first 'best)
7692                         (gnus-summary-best-unread-article)
7693                       (gnus-summary-first-unread-article))
7694               (gnus-configure-windows 'summary))
7695           ;; Don't select any articles, just move point to the first
7696           ;; article in the group.
7697           (goto-char (point-min))
7698           (gnus-summary-position-point)
7699           (gnus-set-mode-line 'summary)
7700           (gnus-configure-windows 'summary 'force))
7701         ;; If we are in async mode, we send some info to the backend.
7702         (when gnus-newsgroup-async
7703           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7704         (when kill-buffer
7705           (gnus-kill-or-deaden-summary kill-buffer))
7706         (when (get-buffer-window gnus-group-buffer t)
7707           ;; Gotta use windows, because recenter does wierd stuff if
7708           ;; the current buffer ain't the displayed window.
7709           (let ((owin (selected-window)))
7710             (select-window (get-buffer-window gnus-group-buffer t))
7711             (when (gnus-group-goto-group group)
7712               (recenter))
7713             (select-window owin))))
7714       ;; Mark this buffer as "prepared".
7715       (setq gnus-newsgroup-prepared t)
7716       t))))
7717
7718 (defun gnus-summary-prepare ()
7719   "Generate the summary buffer."
7720   (let ((buffer-read-only nil))
7721     (erase-buffer)
7722     (setq gnus-newsgroup-data nil
7723           gnus-newsgroup-data-reverse nil)
7724     (run-hooks 'gnus-summary-generate-hook)
7725     ;; Generate the buffer, either with threads or without.
7726     (when gnus-newsgroup-headers
7727       (gnus-summary-prepare-threads
7728        (if gnus-show-threads
7729            (gnus-sort-gathered-threads
7730             (funcall gnus-summary-thread-gathering-function
7731                      (gnus-sort-threads
7732                       (gnus-cut-threads (gnus-make-threads)))))
7733          ;; Unthreaded display.
7734          (gnus-sort-articles gnus-newsgroup-headers))))
7735     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7736     ;; Call hooks for modifying summary buffer.
7737     (goto-char (point-min))
7738     (run-hooks 'gnus-summary-prepare-hook)))
7739
7740 (defun gnus-gather-threads-by-subject (threads)
7741   "Gather threads by looking at Subject headers."
7742   (if (not gnus-summary-make-false-root)
7743       threads
7744     (let ((hashtb (gnus-make-hashtable 1023))
7745           (prev threads)
7746           (result threads)
7747           subject hthread whole-subject)
7748       (while threads
7749         (setq whole-subject (mail-header-subject (caar threads)))
7750         (setq subject
7751               (cond
7752                ;; Truncate the subject.
7753                ((numberp gnus-summary-gather-subject-limit)
7754                 (setq subject (gnus-simplify-subject-re whole-subject))
7755                 (if (> (length subject) gnus-summary-gather-subject-limit)
7756                     (substring subject 0 gnus-summary-gather-subject-limit)
7757                   subject))
7758                ;; Fuzzily simplify it.
7759                ((eq 'fuzzy gnus-summary-gather-subject-limit)
7760                 (gnus-simplify-subject-fuzzy whole-subject))
7761                ;; Just remove the leading "Re:".
7762                (t
7763                 (gnus-simplify-subject-re whole-subject))))
7764
7765         (if (and gnus-summary-gather-exclude-subject
7766                  (string-match gnus-summary-gather-exclude-subject
7767                                subject))
7768             ()          ; We don't want to do anything with this article.
7769           ;; We simplify the subject before looking it up in the
7770           ;; hash table.
7771
7772           (if (setq hthread (gnus-gethash subject hashtb))
7773               (progn
7774                 ;; We enter a dummy root into the thread, if we
7775                 ;; haven't done that already.
7776                 (unless (stringp (caar hthread))
7777                   (setcar hthread (list whole-subject (car hthread))))
7778                 ;; We add this new gathered thread to this gathered
7779                 ;; thread.
7780                 (setcdr (car hthread)
7781                         (nconc (cdar hthread) (list (car threads))))
7782                 ;; Remove it from the list of threads.
7783                 (setcdr prev (cdr threads))
7784                 (setq threads prev))
7785             ;; Enter this thread into the hash table.
7786             (gnus-sethash subject threads hashtb)))
7787         (setq prev threads)
7788         (setq threads (cdr threads)))
7789       result)))
7790
7791 (defun gnus-gather-threads-by-references (threads)
7792   "Gather threads by looking at References headers."
7793   (let ((idhashtb (gnus-make-hashtable 1023))
7794         (thhashtb (gnus-make-hashtable 1023))
7795         (prev threads)
7796         (result threads)
7797         ids references id gthread gid entered)
7798     (while threads
7799       (when (setq references (mail-header-references (caar threads)))
7800         (setq id (mail-header-id (caar threads)))
7801         (setq ids (gnus-split-references references))
7802         (setq entered nil)
7803         (while ids
7804           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7805               (progn
7806                 (gnus-sethash (car ids) id idhashtb)
7807                 (gnus-sethash id threads thhashtb))
7808             (setq gthread (gnus-gethash gid thhashtb))
7809             (unless entered
7810               ;; We enter a dummy root into the thread, if we
7811               ;; haven't done that already.
7812               (unless (stringp (caar gthread))
7813                 (setcar gthread (list (mail-header-subject (caar gthread))
7814                                       (car gthread))))
7815               ;; We add this new gathered thread to this gathered
7816               ;; thread.
7817               (setcdr (car gthread)
7818                       (nconc (cdar gthread) (list (car threads)))))
7819             ;; Add it into the thread hash table.
7820             (gnus-sethash id gthread thhashtb)
7821             (setq entered t)
7822             ;; Remove it from the list of threads.
7823             (setcdr prev (cdr threads))
7824             (setq threads prev))
7825           (setq ids (cdr ids))))
7826       (setq prev threads)
7827       (setq threads (cdr threads)))
7828     result))
7829
7830 (defun gnus-sort-gathered-threads (threads)
7831   "Sort subtreads inside each gathered thread by article number."
7832   (let ((result threads))
7833     (while threads
7834       (when (stringp (caar threads))
7835         (setcdr (car threads)
7836                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
7837       (setq threads (cdr threads)))
7838     result))
7839
7840 (defun gnus-make-threads ()
7841   "Go through the dependency hashtb and find the roots.  Return all threads."
7842   (let (threads)
7843     (mapatoms
7844      (lambda (refs)
7845        (unless (car (symbol-value refs))
7846          ;; These threads do not refer back to any other articles,
7847          ;; so they're roots.
7848          (setq threads (append (cdr (symbol-value refs)) threads))))
7849      gnus-newsgroup-dependencies)
7850     threads))
7851
7852 (defun gnus-build-sparse-threads ()
7853   (let ((headers gnus-newsgroup-headers)
7854         (deps gnus-newsgroup-dependencies)
7855         header references generation relations 
7856         cthread subject child end pthread relation)
7857     ;; First we create an alist of generations/relations, where 
7858     ;; generations is how much we trust the ralation, and the relation
7859     ;; is parent/child.
7860     (gnus-message 7 "Making sparse threads...")
7861     (save-excursion
7862       (nnheader-set-temp-buffer " *gnus sparse threads*")
7863       (while (setq header (pop headers))
7864         (when (and (setq references (mail-header-references header))
7865                    (not (string= references "")))
7866           (insert references)
7867           (setq child (mail-header-id header)
7868                 subject (mail-header-subject header))
7869           (setq generation 0)
7870           (while (search-backward ">" nil t)
7871             (setq end (1+ (point)))
7872             (when (search-backward "<" nil t)
7873               (push (list (incf generation) 
7874                           child (setq child (buffer-substring (point) end))
7875                           subject)
7876                     relations)))
7877           (push (list (1+ generation) child nil subject) relations)
7878           (erase-buffer)))
7879       (kill-buffer (current-buffer)))
7880     ;; Sort over trustworthiness.
7881     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
7882     (while (setq relation (pop relations))
7883       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
7884                 (unless (car (symbol-value cthread))
7885                   ;; Make this article the parent of these threads.
7886                   (setcar (symbol-value cthread)
7887                           (vector gnus-reffed-article-number 
7888                                   (cadddr relation) 
7889                                   "" ""
7890                                   (cadr relation) 
7891                                   (or (caddr relation) "") 0 0 "")))
7892               (set cthread (list (vector gnus-reffed-article-number
7893                                          (cadddr relation) 
7894                                          "" "" (cadr relation) 
7895                                          (or (caddr relation) "") 0 0 ""))))
7896         (push gnus-reffed-article-number gnus-newsgroup-limit)
7897         (push gnus-reffed-article-number gnus-newsgroup-sparse)
7898         (push (cons gnus-reffed-article-number gnus-sparse-mark)
7899               gnus-newsgroup-reads)
7900         (decf gnus-reffed-article-number)
7901         ;; Make this new thread the child of its parent.
7902         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
7903             (setcdr (symbol-value pthread)
7904                     (nconc (cdr (symbol-value pthread))
7905                            (list (symbol-value cthread))))
7906           (set pthread (list nil (symbol-value cthread))))))
7907     (gnus-message 7 "Making sparse threads...done")))
7908
7909 (defun gnus-build-old-threads ()
7910   ;; Look at all the articles that refer back to old articles, and
7911   ;; fetch the headers for the articles that aren't there.  This will
7912   ;; build complete threads - if the roots haven't been expired by the
7913   ;; server, that is.
7914   (let (id heads)
7915     (mapatoms
7916      (lambda (refs)
7917        (when (not (car (symbol-value refs)))
7918          (setq heads (cdr (symbol-value refs)))
7919          (while heads
7920            (if (memq (mail-header-number (caar heads))
7921                      gnus-newsgroup-dormant)
7922                (setq heads (cdr heads))
7923              (setq id (symbol-name refs))
7924              (while (and (setq id (gnus-build-get-header id))
7925                          (not (car (gnus-gethash
7926                                     id gnus-newsgroup-dependencies)))))
7927              (setq heads nil)))))
7928      gnus-newsgroup-dependencies)))
7929
7930 (defun gnus-build-get-header (id)
7931   ;; Look through the buffer of NOV lines and find the header to
7932   ;; ID.  Enter this line into the dependencies hash table, and return
7933   ;; the id of the parent article (if any).
7934   (let ((deps gnus-newsgroup-dependencies)
7935         found header)
7936     (prog1
7937         (save-excursion
7938           (set-buffer nntp-server-buffer)
7939           (goto-char (point-min))
7940           (while (and (not found) (search-forward id nil t))
7941             (beginning-of-line)
7942             (setq found (looking-at
7943                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
7944                                  (regexp-quote id))))
7945             (or found (beginning-of-line 2)))
7946           (when found
7947             (beginning-of-line)
7948             (and
7949              (setq header (gnus-nov-parse-line
7950                            (read (current-buffer)) deps))
7951              (gnus-parent-id (mail-header-references header)))))
7952       (when header
7953         (let ((number (mail-header-number header)))
7954           (push number gnus-newsgroup-limit)
7955           (push header gnus-newsgroup-headers)
7956           (if (memq number gnus-newsgroup-unselected)
7957               (progn
7958                 (push number gnus-newsgroup-unreads)
7959                 (setq gnus-newsgroup-unselected
7960                       (delq number gnus-newsgroup-unselected)))
7961             (push number gnus-newsgroup-ancient)))))))
7962
7963 (defun gnus-summary-update-article (article &optional header)
7964   "Update ARTICLE in the summary buffer."
7965   (set-buffer gnus-summary-buffer)
7966   (let* ((header (or header (gnus-summary-article-header article)))
7967          (id (mail-header-id header))
7968          (data (gnus-data-find article))
7969          (thread (gnus-id-to-thread id))
7970          (parent
7971           (gnus-id-to-thread (or (gnus-parent-id 
7972                                   (mail-header-references header))
7973                                  "tull")))
7974          (buffer-read-only nil)
7975          (old (car thread))
7976          (number (mail-header-number header))
7977          pos)
7978     (when thread
7979       (setcar thread nil)
7980       (when parent
7981         (delq thread parent))
7982       (if (gnus-summary-insert-subject id header)
7983           ;; Set the (possibly) new article number in the data structure.
7984           (gnus-data-set-number data (gnus-id-to-article id))
7985         (setcar thread old)
7986         nil))))
7987
7988 (defun gnus-rebuild-thread (id)
7989   "Rebuild the thread containing ID."
7990   (let ((buffer-read-only nil)
7991         current thread data)
7992     (if (not gnus-show-threads)
7993         (setq thread (list (car (gnus-id-to-thread id))))
7994       ;; Get the thread this article is part of.
7995       (setq thread (gnus-remove-thread id)))
7996     (setq current (save-excursion
7997                     (and (zerop (forward-line -1))
7998                          (gnus-summary-article-number))))
7999     ;; If this is a gathered thread, we have to go some re-gathering.
8000     (when (stringp (car thread))
8001       (let ((subject (car thread))
8002             roots thr)
8003         (setq thread (cdr thread))
8004         (while thread
8005           (unless (memq (setq thr (gnus-id-to-thread
8006                                       (gnus-root-id
8007                                        (mail-header-id (caar thread)))))
8008                         roots)
8009             (push thr roots))
8010           (setq thread (cdr thread)))
8011         ;; We now have all (unique) roots.
8012         (if (= (length roots) 1)
8013             ;; All the loose roots are now one solid root.
8014             (setq thread (car roots))
8015           (setq thread (cons subject (gnus-sort-threads roots))))))
8016     (let (threads)
8017       ;; We then insert this thread into the summary buffer.
8018       (let (gnus-newsgroup-data gnus-newsgroup-threads)
8019         (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
8020         (setq data (nreverse gnus-newsgroup-data))
8021         (setq threads gnus-newsgroup-threads))
8022       ;; We splice the new data into the data structure.
8023       (gnus-data-enter-list current data)
8024       (gnus-data-compute-positions)
8025       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
8026
8027 (defun gnus-id-to-thread (id)
8028   "Return the (sub-)thread where ID appears."
8029   (gnus-gethash id gnus-newsgroup-dependencies))
8030
8031 (defun gnus-id-to-article (id)
8032   "Return the article number of ID."
8033   (let ((thread (gnus-id-to-thread id)))
8034     (when (and thread
8035                (car thread))
8036       (mail-header-number (car thread)))))
8037
8038 (defun gnus-id-to-header (id)
8039   "Return the article headers of ID."
8040   (car (gnus-id-to-thread id)))
8041
8042 (defun gnus-article-displayed-root-p (article)
8043   "Say whether ARTICLE is a root(ish) article."
8044   (let ((level (gnus-summary-thread-level article))
8045         (refs (mail-header-references  (gnus-summary-article-header article)))
8046         particle)
8047     (cond 
8048      ((null level) nil)
8049      ((zerop level) t)
8050      ((null refs) t)
8051      ((null(gnus-parent-id refs)) t)
8052      ((and (= 1 level)
8053            (null (setq particle (gnus-id-to-article
8054                                  (gnus-parent-id refs))))
8055            (null (gnus-summary-thread-level particle)))))))
8056
8057 (defun gnus-root-id (id)
8058   "Return the id of the root of the thread where ID appears."
8059   (let (last-id prev)
8060     (while (and id (setq prev (car (gnus-gethash 
8061                                     id gnus-newsgroup-dependencies))))
8062       (setq last-id id
8063             id (gnus-parent-id (mail-header-references prev))))
8064     last-id))
8065
8066 (defun gnus-remove-thread (id &optional dont-remove)
8067   "Remove the thread that has ID in it."
8068   (let ((dep gnus-newsgroup-dependencies)
8069         headers thread last-id)
8070     ;; First go up in this thread until we find the root.
8071     (setq last-id (gnus-root-id id))
8072     (setq headers (list (car (gnus-id-to-thread last-id))
8073                         (caadr (gnus-id-to-thread last-id))))
8074     ;; We have now found the real root of this thread.  It might have
8075     ;; been gathered into some loose thread, so we have to search
8076     ;; through the threads to find the thread we wanted.
8077     (let ((threads gnus-newsgroup-threads)
8078           sub)
8079       (while threads
8080         (setq sub (car threads))
8081         (if (stringp (car sub))
8082             ;; This is a gathered threads, so we look at the roots
8083             ;; below it to find whether this article in in this
8084             ;; gathered root.
8085             (progn
8086               (setq sub (cdr sub))
8087               (while sub
8088                 (when (member (caar sub) headers)
8089                   (setq thread (car threads)
8090                         threads nil
8091                         sub nil))
8092                 (setq sub (cdr sub))))
8093           ;; It's an ordinary thread, so we check it.
8094           (when (eq (car sub) (car headers))
8095             (setq thread sub
8096                   threads nil)))
8097         (setq threads (cdr threads)))
8098       ;; If this article is in no thread, then it's a root.
8099       (if thread
8100           (unless dont-remove
8101             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
8102         (setq thread (gnus-gethash last-id dep)))
8103       (when thread
8104         (prog1
8105             thread ; We return this thread.
8106           (unless dont-remove
8107             (if (stringp (car thread))
8108                 (progn
8109                   ;; If we use dummy roots, then we have to remove the
8110                   ;; dummy root as well.
8111                   (when (eq gnus-summary-make-false-root 'dummy)
8112                     ;; Uhm.
8113                     )
8114                   (setq thread (cdr thread))
8115                   (while thread
8116                     (gnus-remove-thread-1 (car thread))
8117                     (setq thread (cdr thread))))
8118               (gnus-remove-thread-1 thread))))))))
8119
8120 (defun gnus-remove-thread-1 (thread)
8121   "Remove the thread THREAD recursively."
8122   (let ((number (mail-header-number (car thread)))
8123         pos)
8124     (when (setq pos (text-property-any
8125                      (point-min) (point-max) 'gnus-number number))
8126       (goto-char pos)
8127       (gnus-delete-line)
8128       (gnus-data-remove number))
8129     (setq thread (cdr thread))
8130     (while thread
8131       (gnus-remove-thread-1 (pop thread)))))
8132
8133 (defun gnus-sort-threads (threads)
8134   "Sort THREADS."
8135   (if (not gnus-thread-sort-functions)
8136       threads
8137     (let ((func (if (= 1 (length gnus-thread-sort-functions))
8138                     (car gnus-thread-sort-functions)
8139                   `(lambda (t1 t2)
8140                      ,(gnus-make-sort-function 
8141                        (reverse gnus-thread-sort-functions))))))
8142       (gnus-message 7 "Sorting threads...")
8143       (prog1
8144           (sort threads func)
8145         (gnus-message 7 "Sorting threads...done")))))
8146
8147 (defun gnus-sort-articles (articles)
8148   "Sort ARTICLES."
8149   (when gnus-article-sort-functions
8150     (let ((func (if (= 1 (length gnus-article-sort-functions))
8151                     (car gnus-article-sort-functions)
8152                   `(lambda (t1 t2)
8153                      ,(gnus-make-sort-function 
8154                        (reverse gnus-article-sort-functions))))))
8155       (gnus-message 7 "Sorting articles...")
8156       (prog1
8157           (setq gnus-newsgroup-headers (sort articles func))
8158         (gnus-message 7 "Sorting articles...done")))))
8159
8160 (defun gnus-make-sort-function (funs)
8161   "Return a composite sort condition based on the functions in FUNC."
8162   (if (cdr funs)
8163       `(or (,(car funs) t1 t2)
8164            (and (not (,(car funs) t2 t1))
8165                 ,(gnus-make-sort-function (cdr funs))))
8166     `(,(car funs) t1 t2)))
8167                  
8168 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
8169 (defmacro gnus-thread-header (thread)
8170   ;; Return header of first article in THREAD.
8171   ;; Note that THREAD must never, ever be anything else than a variable -
8172   ;; using some other form will lead to serious barfage.
8173   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8174   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8175   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8176         (vector thread) 2))
8177
8178 (defsubst gnus-article-sort-by-number (h1 h2)
8179   "Sort articles by article number."
8180   (< (mail-header-number h1)
8181      (mail-header-number h2)))
8182
8183 (defun gnus-thread-sort-by-number (h1 h2)
8184   "Sort threads by root article number."
8185   (gnus-article-sort-by-number
8186    (gnus-thread-header h1) (gnus-thread-header h2)))
8187
8188 (defsubst gnus-article-sort-by-author (h1 h2)
8189   "Sort articles by root author."
8190   (string-lessp
8191    (let ((extract (funcall
8192                    gnus-extract-address-components
8193                    (mail-header-from h1))))
8194      (or (car extract) (cdr extract)))
8195    (let ((extract (funcall
8196                    gnus-extract-address-components
8197                    (mail-header-from h2))))
8198      (or (car extract) (cdr extract)))))
8199
8200 (defun gnus-thread-sort-by-author (h1 h2)
8201   "Sort threads by root author."
8202   (gnus-article-sort-by-author
8203    (gnus-thread-header h1)  (gnus-thread-header h2)))
8204
8205 (defsubst gnus-article-sort-by-subject (h1 h2)
8206   "Sort articles by root subject."
8207   (string-lessp
8208    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8209    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8210
8211 (defun gnus-thread-sort-by-subject (h1 h2)
8212   "Sort threads by root subject."
8213   (gnus-article-sort-by-subject
8214    (gnus-thread-header h1) (gnus-thread-header h2)))
8215
8216 (defsubst gnus-article-sort-by-date (h1 h2)
8217   "Sort articles by root article date."
8218   (string-lessp
8219    (gnus-sortable-date (mail-header-date h1))
8220    (gnus-sortable-date (mail-header-date h2))))
8221
8222 (defun gnus-thread-sort-by-date (h1 h2)
8223   "Sort threads by root article date."
8224   (gnus-article-sort-by-date
8225    (gnus-thread-header h1) (gnus-thread-header h2)))
8226
8227 (defsubst gnus-article-sort-by-score (h1 h2)
8228   "Sort articles by root article score.
8229 Unscored articles will be counted as having a score of zero."
8230   (> (or (cdr (assq (mail-header-number h1)
8231                     gnus-newsgroup-scored))
8232          gnus-summary-default-score 0)
8233      (or (cdr (assq (mail-header-number h2)
8234                     gnus-newsgroup-scored))
8235          gnus-summary-default-score 0)))
8236
8237 (defun gnus-thread-sort-by-score (h1 h2)
8238   "Sort threads by root article score."
8239   (gnus-article-sort-by-score
8240    (gnus-thread-header h1) (gnus-thread-header h2)))
8241
8242 (defun gnus-thread-sort-by-total-score (h1 h2)
8243   "Sort threads by the sum of all scores in the thread.
8244 Unscored articles will be counted as having a score of zero."
8245   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8246
8247 (defun gnus-thread-total-score (thread)
8248   ;;  This function find the total score of THREAD.
8249   (if (consp thread)
8250       (if (stringp (car thread))
8251           (apply gnus-thread-score-function 0
8252                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8253         (gnus-thread-total-score-1 thread))
8254     (gnus-thread-total-score-1 (list thread))))
8255
8256 (defun gnus-thread-total-score-1 (root)
8257   ;; This function find the total score of the thread below ROOT.
8258   (setq root (car root))
8259   (apply gnus-thread-score-function
8260          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8261              gnus-summary-default-score 0)
8262          (mapcar 'gnus-thread-total-score
8263                  (cdr (gnus-gethash (mail-header-id root)
8264                                     gnus-newsgroup-dependencies)))))
8265
8266 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8267 (defvar gnus-tmp-prev-subject nil)
8268 (defvar gnus-tmp-false-parent nil)
8269 (defvar gnus-tmp-root-expunged nil)
8270 (defvar gnus-tmp-dummy-line nil)
8271
8272 (defun gnus-summary-prepare-threads (threads)
8273   "Prepare summary buffer from THREADS and indentation LEVEL.
8274 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8275 or a straight list of headers."
8276   (gnus-message 7 "Generating summary...")
8277
8278   (setq gnus-newsgroup-threads threads)
8279   (beginning-of-line)
8280
8281   (let ((gnus-tmp-level 0)
8282         (default-score (or gnus-summary-default-score 0))
8283         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8284         thread number subject stack state gnus-tmp-gathered beg-match
8285         new-roots gnus-tmp-new-adopts thread-end
8286         gnus-tmp-header gnus-tmp-unread
8287         gnus-tmp-replied gnus-tmp-subject-or-nil
8288         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8289         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8290         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8291
8292     (setq gnus-tmp-prev-subject nil)
8293
8294     (if (vectorp (car threads))
8295         ;; If this is a straight (sic) list of headers, then a
8296         ;; threaded summary display isn't required, so we just create
8297         ;; an unthreaded one.
8298         (gnus-summary-prepare-unthreaded threads)
8299
8300       ;; Do the threaded display.
8301
8302       (while (or threads stack gnus-tmp-new-adopts new-roots)
8303
8304         (if (and (= gnus-tmp-level 0)
8305                  (not (setq gnus-tmp-dummy-line nil))
8306                  (or (not stack)
8307                      (= (caar stack) 0))
8308                  (not gnus-tmp-false-parent)
8309                  (or gnus-tmp-new-adopts new-roots))
8310             (if gnus-tmp-new-adopts
8311                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8312                       thread (list (car gnus-tmp-new-adopts))
8313                       gnus-tmp-header (caar thread)
8314                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8315               (if new-roots
8316                   (setq thread (list (car new-roots))
8317                         gnus-tmp-header (caar thread)
8318                         new-roots (cdr new-roots))))
8319
8320           (if threads
8321               ;; If there are some threads, we do them before the
8322               ;; threads on the stack.
8323               (setq thread threads
8324                     gnus-tmp-header (caar thread))
8325             ;; There were no current threads, so we pop something off
8326             ;; the stack.
8327             (setq state (car stack)
8328                   gnus-tmp-level (car state)
8329                   thread (cdr state)
8330                   stack (cdr stack)
8331                   gnus-tmp-header (caar thread))))
8332
8333         (setq gnus-tmp-false-parent nil)
8334         (setq gnus-tmp-root-expunged nil)
8335         (setq thread-end nil)
8336
8337         (if (stringp gnus-tmp-header)
8338             ;; The header is a dummy root.
8339             (cond
8340              ((eq gnus-summary-make-false-root 'adopt)
8341               ;; We let the first article adopt the rest.
8342               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8343                                                (cddar thread)))
8344               (setq gnus-tmp-gathered
8345                     (nconc (mapcar
8346                             (lambda (h) (mail-header-number (car h)))
8347                             (cddar thread))
8348                            gnus-tmp-gathered))
8349               (setq thread (cons (list (caar thread)
8350                                        (cadar thread))
8351                                  (cdr thread)))
8352               (setq gnus-tmp-level -1
8353                     gnus-tmp-false-parent t))
8354              ((eq gnus-summary-make-false-root 'empty)
8355               ;; We print adopted articles with empty subject fields.
8356               (setq gnus-tmp-gathered
8357                     (nconc (mapcar
8358                             (lambda (h) (mail-header-number (car h)))
8359                             (cddar thread))
8360                            gnus-tmp-gathered))
8361               (setq gnus-tmp-level -1))
8362              ((eq gnus-summary-make-false-root 'dummy)
8363               ;; We remember that we probably want to output a dummy
8364               ;; root.
8365               (setq gnus-tmp-dummy-line gnus-tmp-header)
8366               (setq gnus-tmp-prev-subject gnus-tmp-header))
8367              (t
8368               ;; We do not make a root for the gathered
8369               ;; sub-threads at all.
8370               (setq gnus-tmp-level -1)))
8371
8372           (setq number (mail-header-number gnus-tmp-header)
8373                 subject (mail-header-subject gnus-tmp-header))
8374
8375           (cond
8376            ;; If the thread has changed subject, we might want to make
8377            ;; this subthread into a root.
8378            ((and (null gnus-thread-ignore-subject)
8379                  (not (zerop gnus-tmp-level))
8380                  gnus-tmp-prev-subject
8381                  (not (inline
8382                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8383             (setq new-roots (nconc new-roots (list (car thread)))
8384                   thread-end t
8385                   gnus-tmp-header nil))
8386            ;; If the article lies outside the current limit,
8387            ;; then we do not display it.
8388            ((and (not (memq number gnus-newsgroup-limit))
8389                  (not gnus-tmp-dummy-line))
8390             (setq gnus-tmp-gathered
8391                   (nconc (mapcar
8392                           (lambda (h) (mail-header-number (car h)))
8393                           (cdar thread))
8394                          gnus-tmp-gathered))
8395             (setq gnus-tmp-new-adopts (if (cdar thread)
8396                                           (append gnus-tmp-new-adopts
8397                                                   (cdar thread))
8398                                         gnus-tmp-new-adopts)
8399                   thread-end t
8400                   gnus-tmp-header nil)
8401             (when (zerop gnus-tmp-level)
8402               (setq gnus-tmp-root-expunged t)))
8403            ;; Perhaps this article is to be marked as read?
8404            ((and gnus-summary-mark-below
8405                  (< (or (cdr (assq number gnus-newsgroup-scored))
8406                         default-score)
8407                     gnus-summary-mark-below)
8408                  ;; Don't touch sparse articles.
8409                  (not (memq number gnus-newsgroup-sparse)))
8410             (setq gnus-newsgroup-unreads
8411                   (delq number gnus-newsgroup-unreads))
8412             (if gnus-newsgroup-auto-expire
8413                 (push number gnus-newsgroup-expirable)
8414               (push (cons number gnus-low-score-mark)
8415                     gnus-newsgroup-reads))))
8416
8417           (when gnus-tmp-header
8418             ;; We may have an old dummy line to output before this
8419             ;; article.
8420             (when gnus-tmp-dummy-line
8421               (gnus-summary-insert-dummy-line
8422                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8423               (setq gnus-tmp-dummy-line nil))
8424
8425             ;; Compute the mark.
8426             (setq
8427              gnus-tmp-unread
8428              (cond
8429               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8430               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8431               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8432               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8433               (t (or (cdr (assq number gnus-newsgroup-reads))
8434                      gnus-ancient-mark))))
8435
8436             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8437                                   gnus-tmp-header gnus-tmp-level)
8438                   gnus-newsgroup-data)
8439
8440             ;; Actually insert the line.
8441             (setq
8442              gnus-tmp-subject-or-nil
8443              (cond
8444               ((and gnus-thread-ignore-subject
8445                     gnus-tmp-prev-subject
8446                     (not (inline (gnus-subject-equal
8447                                   gnus-tmp-prev-subject subject))))
8448                subject)
8449               ((zerop gnus-tmp-level)
8450                (if (and (eq gnus-summary-make-false-root 'empty)
8451                         (memq number gnus-tmp-gathered)
8452                         gnus-tmp-prev-subject
8453                         (inline (gnus-subject-equal
8454                                  gnus-tmp-prev-subject subject)))
8455                    gnus-summary-same-subject
8456                  subject))
8457               (t gnus-summary-same-subject)))
8458             (if (and (eq gnus-summary-make-false-root 'adopt)
8459                      (= gnus-tmp-level 1)
8460                      (memq number gnus-tmp-gathered))
8461                 (setq gnus-tmp-opening-bracket ?\<
8462                       gnus-tmp-closing-bracket ?\>)
8463               (setq gnus-tmp-opening-bracket ?\[
8464                     gnus-tmp-closing-bracket ?\]))
8465             (setq
8466              gnus-tmp-indentation
8467              (aref gnus-thread-indent-array gnus-tmp-level)
8468              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8469              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8470                                 gnus-summary-default-score 0)
8471              gnus-tmp-score-char
8472              (if (or (null gnus-summary-default-score)
8473                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8474                          gnus-summary-zcore-fuzz)) ? 
8475                (if (< gnus-tmp-score gnus-summary-default-score)
8476                    gnus-score-below-mark gnus-score-over-mark))
8477              gnus-tmp-replied
8478              (cond ((memq number gnus-newsgroup-processable)
8479                     gnus-process-mark)
8480                    ((memq number gnus-newsgroup-cached)
8481                     gnus-cached-mark)
8482                    ((memq number gnus-newsgroup-replied)
8483                     gnus-replied-mark)
8484                    (t gnus-unread-mark))
8485              gnus-tmp-from (mail-header-from gnus-tmp-header)
8486              gnus-tmp-name
8487              (cond
8488               ((string-match "(.+)" gnus-tmp-from)
8489                (substring gnus-tmp-from
8490                           (1+ (match-beginning 0)) (1- (match-end 0))))
8491               ((string-match "<[^>]+> *$" gnus-tmp-from)
8492                (setq beg-match (match-beginning 0))
8493                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8494                         (substring gnus-tmp-from (1+ (match-beginning 0))
8495                                    (1- (match-end 0))))
8496                    (substring gnus-tmp-from 0 beg-match)))
8497               (t gnus-tmp-from)))
8498             (when (string= gnus-tmp-name "")
8499               (setq gnus-tmp-name gnus-tmp-from))
8500             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8501             (gnus-put-text-property
8502              (point)
8503              (progn (eval gnus-summary-line-format-spec) (point))
8504              'gnus-number number)
8505             (when gnus-visual-p
8506               (forward-line -1)
8507               (run-hooks 'gnus-summary-update-hook)
8508               (forward-line 1))
8509
8510             (setq gnus-tmp-prev-subject subject)))
8511
8512         (when (nth 1 thread)
8513           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8514         (incf gnus-tmp-level)
8515         (setq threads (if thread-end nil (cdar thread)))
8516         (unless threads
8517           (setq gnus-tmp-level 0)))))
8518   (gnus-message 7 "Generating summary...done"))
8519
8520 (defun gnus-summary-prepare-unthreaded (headers)
8521   "Generate an unthreaded summary buffer based on HEADERS."
8522   (let (header number mark)
8523
8524     (while headers
8525       ;; We may have to root out some bad articles...
8526       (when (memq (setq number (mail-header-number
8527                                 (setq header (pop headers))))
8528                   gnus-newsgroup-limit)
8529         ;; Mark article as read when it has a low score.
8530         (when (and gnus-summary-mark-below
8531                    (< (or (cdr (assq number gnus-newsgroup-scored))
8532                           gnus-summary-default-score 0)
8533                       gnus-summary-mark-below))
8534           (setq gnus-newsgroup-unreads
8535                 (delq number gnus-newsgroup-unreads))
8536           (if gnus-newsgroup-auto-expire
8537               (push number gnus-newsgroup-expirable)
8538             (push (cons number gnus-low-score-mark)
8539                   gnus-newsgroup-reads)))
8540
8541         (setq mark
8542               (cond
8543                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8544                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8545                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8546                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8547                (t (or (cdr (assq number gnus-newsgroup-reads))
8548                       gnus-ancient-mark))))
8549         (setq gnus-newsgroup-data
8550               (cons (gnus-data-make number mark (1+ (point)) header 0)
8551                     gnus-newsgroup-data))
8552         (gnus-summary-insert-line
8553          header 0 nil mark (memq number gnus-newsgroup-replied)
8554          (memq number gnus-newsgroup-expirable)
8555          (mail-header-subject header) nil
8556          (cdr (assq number gnus-newsgroup-scored))
8557          (memq number gnus-newsgroup-processable))))))
8558
8559 (defun gnus-select-newsgroup (group &optional read-all)
8560   "Select newsgroup GROUP.
8561 If READ-ALL is non-nil, all articles in the group are selected."
8562   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8563          (info (nth 2 entry))
8564          articles fetched-articles cached)
8565
8566     (or (gnus-check-server
8567          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8568         (error "Couldn't open server"))
8569
8570     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8571         (gnus-activate-group group) ; Or we can activate it...
8572         (progn ; Or we bug out.
8573           (when (equal major-mode 'gnus-summary-mode)
8574             (kill-buffer (current-buffer)))
8575           (error "Couldn't request group %s: %s"
8576                  group (gnus-status-message group))))
8577
8578     (setq gnus-newsgroup-name group)
8579     (setq gnus-newsgroup-unselected nil)
8580     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8581
8582     (and gnus-asynchronous
8583          (gnus-check-backend-function
8584           'request-asynchronous gnus-newsgroup-name)
8585          (setq gnus-newsgroup-async
8586                (gnus-request-asynchronous gnus-newsgroup-name)))
8587
8588     ;; Adjust and set lists of article marks.
8589     (when info
8590       (gnus-adjust-marked-articles info))
8591
8592     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8593     (when (gnus-virtual-group-p group)
8594       (setq cached gnus-newsgroup-cached))
8595
8596     (setq gnus-newsgroup-unreads
8597           (gnus-set-difference
8598            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8599            gnus-newsgroup-dormant))
8600
8601     (setq gnus-newsgroup-processable nil)
8602
8603     (setq articles (gnus-articles-to-read group read-all))
8604
8605     (cond
8606      ((null articles)
8607       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8608       'quit)
8609      ((eq articles 0) nil)
8610      (t
8611       ;; Init the dependencies hash table.
8612       (setq gnus-newsgroup-dependencies
8613             (gnus-make-hashtable (length articles)))
8614       ;; Retrieve the headers and read them in.
8615       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8616       (setq gnus-newsgroup-headers
8617             (if (eq 'nov
8618                     (setq gnus-headers-retrieved-by
8619                           (gnus-retrieve-headers
8620                            articles gnus-newsgroup-name
8621                            ;; We might want to fetch old headers, but
8622                            ;; not if there is only 1 article.
8623                            (and gnus-fetch-old-headers
8624                                 (or (and
8625                                      (not (eq gnus-fetch-old-headers 'some))
8626                                      (not (numberp gnus-fetch-old-headers)))
8627                                     (> (length articles) 1))))))
8628                 (gnus-get-newsgroup-headers-xover articles)
8629               (gnus-get-newsgroup-headers)))
8630       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
8631
8632       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8633       (when cached
8634         (setq gnus-newsgroup-cached cached))
8635
8636       ;; Set the initial limit.
8637       (setq gnus-newsgroup-limit (copy-sequence articles))
8638       ;; Remove canceled articles from the list of unread articles.
8639       (setq gnus-newsgroup-unreads
8640             (gnus-set-sorted-intersection
8641              gnus-newsgroup-unreads
8642              (setq fetched-articles
8643                    (mapcar (lambda (headers) (mail-header-number headers))
8644                            gnus-newsgroup-headers))))
8645       ;; Removed marked articles that do not exist.
8646       (gnus-update-missing-marks
8647        (gnus-sorted-complement fetched-articles articles))
8648       ;; We might want to build some more threads first.
8649       (and gnus-fetch-old-headers
8650            (eq gnus-headers-retrieved-by 'nov)
8651            (gnus-build-old-threads))
8652       ;; Check whether auto-expire is to be done in this group.
8653       (setq gnus-newsgroup-auto-expire
8654             (gnus-group-auto-expirable-p group))
8655       ;; Set up the article buffer now, if necessary.
8656       (unless gnus-single-article-buffer
8657         (gnus-article-setup-buffer))
8658       ;; First and last article in this newsgroup.
8659       (and gnus-newsgroup-headers
8660            (setq gnus-newsgroup-begin
8661                  (mail-header-number (car gnus-newsgroup-headers)))
8662            (setq gnus-newsgroup-end
8663                  (mail-header-number
8664                   (gnus-last-element gnus-newsgroup-headers))))
8665       (setq gnus-reffed-article-number -1)
8666       ;; GROUP is successfully selected.
8667       (or gnus-newsgroup-headers t)))))
8668
8669 (defun gnus-articles-to-read (group read-all)
8670   ;; Find out what articles the user wants to read.
8671   (let* ((articles
8672           ;; Select all articles if `read-all' is non-nil, or if there
8673           ;; are no unread articles.
8674           (if (or read-all
8675                   (and (zerop (length gnus-newsgroup-marked))
8676                        (zerop (length gnus-newsgroup-unreads))))
8677               (gnus-uncompress-range (gnus-active group))
8678             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8679                           (copy-sequence gnus-newsgroup-unreads))
8680                   '<)))
8681          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8682          (scored (length scored-list))
8683          (number (length articles))
8684          (marked (+ (length gnus-newsgroup-marked)
8685                     (length gnus-newsgroup-dormant)))
8686          (select
8687           (cond
8688            ((numberp read-all)
8689             read-all)
8690            (t
8691             (condition-case ()
8692                 (cond
8693                  ((and (or (<= scored marked) (= scored number))
8694                        (numberp gnus-large-newsgroup)
8695                        (> number gnus-large-newsgroup))
8696                   (let ((input
8697                          (read-string
8698                           (format
8699                            "How many articles from %s (default %d): "
8700                            gnus-newsgroup-name number))))
8701                     (if (string-match "^[ \t]*$" input) number input)))
8702                  ((and (> scored marked) (< scored number))
8703                   (let ((input
8704                          (read-string
8705                           (format "%s %s (%d scored, %d total): "
8706                                   "How many articles from"
8707                                   group scored number))))
8708                     (if (string-match "^[ \t]*$" input)
8709                         number input)))
8710                  (t number))
8711               (quit nil))))))
8712     (setq select (if (stringp select) (string-to-number select) select))
8713     (if (or (null select) (zerop select))
8714         select
8715       (if (and (not (zerop scored)) (<= (abs select) scored))
8716           (progn
8717             (setq articles (sort scored-list '<))
8718             (setq number (length articles)))
8719         (setq articles (copy-sequence articles)))
8720
8721       (if (< (abs select) number)
8722           (if (< select 0)
8723               ;; Select the N oldest articles.
8724               (setcdr (nthcdr (1- (abs select)) articles) nil)
8725             ;; Select the N most recent articles.
8726             (setq articles (nthcdr (- number select) articles))))
8727       (setq gnus-newsgroup-unselected
8728             (gnus-sorted-intersection
8729              gnus-newsgroup-unreads
8730              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8731       articles)))
8732
8733 (defun gnus-killed-articles (killed articles)
8734   (let (out)
8735     (while articles
8736       (if (inline (gnus-member-of-range (car articles) killed))
8737           (setq out (cons (car articles) out)))
8738       (setq articles (cdr articles)))
8739     out))
8740
8741 (defun gnus-uncompress-marks (marks)
8742   "Uncompress the mark ranges in MARKS."
8743   (let ((uncompressed '(score bookmark))
8744         out)
8745     (while marks
8746       (if (memq (caar marks) uncompressed)
8747           (push (car marks) out)
8748         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8749       (setq marks (cdr marks)))
8750     out))
8751
8752 (defun gnus-adjust-marked-articles (info)
8753   "Set all article lists and remove all marks that are no longer legal."
8754   (let* ((marked-lists (gnus-info-marks info))
8755          (active (gnus-active (gnus-info-group info)))
8756          (min (car active))
8757          (max (cdr active))
8758          (types gnus-article-mark-lists)
8759          (uncompressed '(score bookmark))
8760          marks var articles article mark)
8761
8762     (while marked-lists
8763       (setq marks (pop marked-lists))
8764       (set (setq var (intern (format "gnus-newsgroup-%s"
8765                                      (car (rassq (setq mark (car marks))
8766                                                  types)))))
8767            (if (memq (car marks) uncompressed) (cdr marks)
8768              (gnus-uncompress-range (cdr marks))))
8769
8770       (setq articles (symbol-value var))
8771
8772       ;; All articles have to be subsets of the active articles.
8773       (cond
8774        ;; Adjust "simple" lists.
8775        ((memq mark '(tick dormant expirable reply killed save))
8776         (while articles
8777           (when (or (< (setq article (pop articles)) min) (> article max))
8778             (set var (delq article (symbol-value var))))))
8779        ;; Adjust assocs.
8780        ((memq mark '(score bookmark))
8781         (while articles
8782           (when (or (< (car (setq article (pop articles))) min)
8783                     (> (car article) max))
8784             (set var (delq article (symbol-value var))))))))))
8785
8786 (defun gnus-update-missing-marks (missing)
8787   "Go through the list of MISSING articles and remove them mark lists."
8788   (when missing
8789     (let ((types gnus-article-mark-lists)
8790           var m)
8791       ;; Go through all types.
8792       (while types
8793         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8794         (when (symbol-value var)
8795           ;; This list has articles.  So we delete all missing articles
8796           ;; from it.
8797           (setq m missing)
8798           (while m
8799             (set var (delq (pop m) (symbol-value var)))))))))
8800
8801 (defun gnus-update-marks ()
8802   "Enter the various lists of marked articles into the newsgroup info list."
8803   (let ((types gnus-article-mark-lists)
8804         (info (gnus-get-info gnus-newsgroup-name))
8805         (uncompressed '(score bookmark killed))
8806         type list newmarked symbol)
8807     (when info
8808       ;; Add all marks lists that are non-nil to the list of marks lists.
8809       (while types
8810         (setq type (pop types))
8811         (when (setq list (symbol-value
8812                           (setq symbol
8813                                 (intern (format "gnus-newsgroup-%s"
8814                                                 (car type))))))
8815           (push (cons (cdr type)
8816                       (if (memq (cdr type) uncompressed) list
8817                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8818                 newmarked)))
8819
8820       ;; Enter these new marks into the info of the group.
8821       (if (nthcdr 3 info)
8822           (setcar (nthcdr 3 info) newmarked)
8823         ;; Add the marks lists to the end of the info.
8824         (when newmarked
8825           (setcdr (nthcdr 2 info) (list newmarked))))
8826
8827       ;; Cut off the end of the info if there's nothing else there.
8828       (let ((i 5))
8829         (while (and (> i 2)
8830                     (not (nth i info)))
8831           (when (nthcdr (decf i) info)
8832             (setcdr (nthcdr i info) nil)))))))
8833
8834 (defun gnus-add-marked-articles (group type articles &optional info force)
8835   ;; Add ARTICLES of TYPE to the info of GROUP.
8836   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8837   ;; add, but replace marked articles of TYPE with ARTICLES.
8838   (let ((info (or info (gnus-get-info group)))
8839         (uncompressed '(score bookmark killed))
8840         marked m)
8841     (or (not info)
8842         (and (not (setq marked (nthcdr 3 info)))
8843              (or (null articles)
8844                  (setcdr (nthcdr 2 info)
8845                          (list (list (cons type (gnus-compress-sequence
8846                                                  articles t)))))))
8847         (and (not (setq m (assq type (car marked))))
8848              (or (null articles)
8849                  (setcar marked
8850                          (cons (cons type (gnus-compress-sequence articles t) )
8851                                (car marked)))))
8852         (if force
8853             (if (null articles)
8854                 (setcar (nthcdr 3 info)
8855                         (delq (assq type (car marked)) (car marked)))
8856               (setcdr m (gnus-compress-sequence articles t)))
8857           (setcdr m (gnus-compress-sequence
8858                      (sort (nconc (gnus-uncompress-range (cdr m))
8859                                   (copy-sequence articles)) '<) t))))))
8860
8861 (defun gnus-set-mode-line (where)
8862   "This function sets the mode line of the article or summary buffers.
8863 If WHERE is `summary', the summary mode line format will be used."
8864   ;; Is this mode line one we keep updated?
8865   (when (memq where gnus-updated-mode-lines)
8866     (let (mode-string)
8867       (save-excursion
8868         ;; We evaluate this in the summary buffer since these
8869         ;; variables are buffer-local to that buffer.
8870         (set-buffer gnus-summary-buffer)
8871         ;; We bind all these variables that are used in the `eval' form
8872         ;; below.
8873         (let* ((mformat (symbol-value
8874                          (intern
8875                           (format "gnus-%s-mode-line-format-spec" where))))
8876                (gnus-tmp-group-name gnus-newsgroup-name)
8877                (gnus-tmp-article-number (or gnus-current-article 0))
8878                (gnus-tmp-unread gnus-newsgroup-unreads)
8879                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
8880                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
8881                (gnus-tmp-unread-and-unselected
8882                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
8883                             (zerop gnus-tmp-unselected)) "")
8884                       ((zerop gnus-tmp-unselected)
8885                        (format "{%d more}" gnus-tmp-unread-and-unticked))
8886                       (t (format "{%d(+%d) more}"
8887                                  gnus-tmp-unread-and-unticked
8888                                  gnus-tmp-unselected))))
8889                (gnus-tmp-subject
8890                 (if (and gnus-current-headers
8891                          (vectorp gnus-current-headers))
8892                     (mail-header-subject gnus-current-headers) ""))
8893                max-len
8894                gnus-tmp-header);; passed as argument to any user-format-funcs
8895           (setq mode-string (eval mformat))
8896           (setq max-len (max 4 (if gnus-mode-non-string-length
8897                                    (- (window-width)
8898                                       gnus-mode-non-string-length)
8899                                  (length mode-string))))
8900           ;; We might have to chop a bit of the string off...
8901           (when (> (length mode-string) max-len)
8902             (setq mode-string
8903                   (concat (gnus-truncate-string mode-string (- max-len 3))
8904                           "...")))
8905           ;; Pad the mode string a bit.
8906           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
8907       ;; Update the mode line.
8908       (setq mode-line-buffer-identification (list mode-string))
8909       (set-buffer-modified-p t))))
8910
8911 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
8912   "Go through the HEADERS list and add all Xrefs to a hash table.
8913 The resulting hash table is returned, or nil if no Xrefs were found."
8914   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
8915          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
8916          (xref-hashtb (make-vector 63 0))
8917          start group entry number xrefs header)
8918     (while headers
8919       (setq header (pop headers))
8920       (when (and (setq xrefs (mail-header-xref header))
8921                  (not (memq (setq number (mail-header-number header))
8922                             unreads)))
8923         (setq start 0)
8924         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
8925           (setq start (match-end 0))
8926           (setq group (if prefix
8927                           (concat prefix (substring xrefs (match-beginning 1)
8928                                                     (match-end 1)))
8929                         (substring xrefs (match-beginning 1) (match-end 1))))
8930           (setq number
8931                 (string-to-int (substring xrefs (match-beginning 2)
8932                                           (match-end 2))))
8933           (if (setq entry (gnus-gethash group xref-hashtb))
8934               (setcdr entry (cons number (cdr entry)))
8935             (gnus-sethash group (cons number nil) xref-hashtb)))))
8936     (and start xref-hashtb)))
8937
8938 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
8939   "Look through all the headers and mark the Xrefs as read."
8940   (let ((virtual (gnus-virtual-group-p from-newsgroup))
8941         name entry info xref-hashtb idlist method nth4)
8942     (save-excursion
8943       (set-buffer gnus-group-buffer)
8944       (when (setq xref-hashtb
8945                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
8946         (mapatoms
8947          (lambda (group)
8948            (unless (string= from-newsgroup (setq name (symbol-name group)))
8949              (setq idlist (symbol-value group))
8950              ;; Dead groups are not updated.
8951              (and (prog1
8952                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
8953                             info (nth 2 entry))
8954                     (if (stringp (setq nth4 (gnus-info-method info)))
8955                         (setq nth4 (gnus-server-to-method nth4))))
8956                   ;; Only do the xrefs if the group has the same
8957                   ;; select method as the group we have just read.
8958                   (or (gnus-methods-equal-p
8959                        nth4 (gnus-find-method-for-group from-newsgroup))
8960                       virtual
8961                       (equal nth4 (setq method (gnus-find-method-for-group
8962                                                 from-newsgroup)))
8963                       (and (equal (car nth4) (car method))
8964                            (equal (nth 1 nth4) (nth 1 method))))
8965                   gnus-use-cross-reference
8966                   (or (not (eq gnus-use-cross-reference t))
8967                       virtual
8968                       ;; Only do cross-references on subscribed
8969                       ;; groups, if that is what is wanted.
8970                       (<= (gnus-info-level info) gnus-level-subscribed))
8971                   (gnus-group-make-articles-read name idlist))))
8972          xref-hashtb)))))
8973
8974 (defun gnus-group-make-articles-read (group articles)
8975   (let* ((num 0)
8976          (entry (gnus-gethash group gnus-newsrc-hashtb))
8977          (info (nth 2 entry))
8978          (active (gnus-active group))
8979          range)
8980     ;; First peel off all illegal article numbers.
8981     (if active
8982         (let ((ids articles)
8983               id first)
8984           (while ids
8985             (setq id (car ids))
8986             (if (and first (> id (cdr active)))
8987                 (progn
8988                   ;; We'll end up in this situation in one particular
8989                   ;; obscure situation.  If you re-scan a group and get
8990                   ;; a new article that is cross-posted to a different
8991                   ;; group that has not been re-scanned, you might get
8992                   ;; crossposted article that has a higher number than
8993                   ;; Gnus believes possible.  So we re-activate this
8994                   ;; group as well.  This might mean doing the
8995                   ;; crossposting thingy will *increase* the number
8996                   ;; of articles in some groups.  Tsk, tsk.
8997                   (setq active (or (gnus-activate-group group) active))))
8998             (if (or (> id (cdr active))
8999                     (< id (car active)))
9000                 (setq articles (delq id articles)))
9001             (setq ids (cdr ids)))))
9002     ;; If the read list is nil, we init it.
9003     (and active
9004          (null (gnus-info-read info))
9005          (> (car active) 1)
9006          (gnus-info-set-read info (cons 1 (1- (car active)))))
9007     ;; Then we add the read articles to the range.
9008     (gnus-info-set-read
9009      info
9010      (setq range
9011            (gnus-add-to-range
9012             (gnus-info-read info) (setq articles (sort articles '<)))))
9013     ;; Then we have to re-compute how many unread
9014     ;; articles there are in this group.
9015     (if active
9016         (progn
9017           (cond
9018            ((not range)
9019             (setq num (- (1+ (cdr active)) (car active))))
9020            ((not (listp (cdr range)))
9021             (setq num (- (cdr active) (- (1+ (cdr range))
9022                                          (car range)))))
9023            (t
9024             (while range
9025               (if (numberp (car range))
9026                   (setq num (1+ num))
9027                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
9028               (setq range (cdr range)))
9029             (setq num (- (cdr active) num))))
9030           ;; Update the number of unread articles.
9031           (setcar entry num)
9032           ;; Update the group buffer.
9033           (gnus-group-update-group group t)))))
9034
9035 (defun gnus-methods-equal-p (m1 m2)
9036   (let ((m1 (or m1 gnus-select-method))
9037         (m2 (or m2 gnus-select-method)))
9038     (or (equal m1 m2)
9039         (and (eq (car m1) (car m2))
9040              (or (not (memq 'address (assoc (symbol-name (car m1))
9041                                             gnus-valid-select-methods)))
9042                  (equal (nth 1 m1) (nth 1 m2)))))))
9043
9044 (defsubst gnus-header-value ()
9045   (buffer-substring (match-end 0) (gnus-point-at-eol)))
9046
9047 (defvar gnus-newsgroup-none-id 0)
9048
9049 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
9050   (let ((cur nntp-server-buffer)
9051         (dependencies
9052          (or dependencies
9053              (save-excursion (set-buffer gnus-summary-buffer)
9054                              gnus-newsgroup-dependencies)))
9055         headers id id-dep ref-dep end ref)
9056     (save-excursion
9057       (set-buffer nntp-server-buffer)
9058       (run-hooks 'gnus-parse-headers-hook)
9059       (let ((case-fold-search t)
9060             in-reply-to header p lines)
9061         (goto-char (point-min))
9062         ;; Search to the beginning of the next header.  Error messages
9063         ;; do not begin with 2 or 3.
9064         (while (re-search-forward "^[23][0-9]+ " nil t)
9065           (setq id nil
9066                 ref nil)
9067           ;; This implementation of this function, with nine
9068           ;; search-forwards instead of the one re-search-forward and
9069           ;; a case (which basically was the old function) is actually
9070           ;; about twice as fast, even though it looks messier.  You
9071           ;; can't have everything, I guess.  Speed and elegance
9072           ;; doesn't always go hand in hand.
9073           (setq
9074            header
9075            (vector
9076             ;; Number.
9077             (prog1
9078                 (read cur)
9079               (end-of-line)
9080               (setq p (point))
9081               (narrow-to-region (point)
9082                                 (or (and (search-forward "\n.\n" nil t)
9083                                          (- (point) 2))
9084                                     (point))))
9085             ;; Subject.
9086             (progn
9087               (goto-char p)
9088               (if (search-forward "\nsubject: " nil t)
9089                   (gnus-header-value) "(none)"))
9090             ;; From.
9091             (progn
9092               (goto-char p)
9093               (if (search-forward "\nfrom: " nil t)
9094                   (gnus-header-value) "(nobody)"))
9095             ;; Date.
9096             (progn
9097               (goto-char p)
9098               (if (search-forward "\ndate: " nil t)
9099                   (gnus-header-value) ""))
9100             ;; Message-ID.
9101             (progn
9102               (goto-char p)
9103               (if (search-forward "\nmessage-id: " nil t)
9104                   (setq id (gnus-header-value))
9105                 ;; If there was no message-id, we just fake one to make
9106                 ;; subsequent routines simpler.
9107                 (setq id (concat "none+"
9108                                  (int-to-string
9109                                   (setq gnus-newsgroup-none-id
9110                                         (1+ gnus-newsgroup-none-id)))))))
9111             ;; References.
9112             (progn
9113               (goto-char p)
9114               (if (search-forward "\nreferences: " nil t)
9115                   (prog1
9116                       (gnus-header-value)
9117                     (setq end (match-end 0))
9118                     (save-excursion
9119                       (setq ref
9120                             (buffer-substring
9121                              (progn
9122                                (end-of-line)
9123                                (search-backward ">" end t)
9124                                (1+ (point)))
9125                              (progn
9126                                (search-backward "<" end t)
9127                                (point))))))
9128                 ;; Get the references from the in-reply-to header if there
9129                 ;; were no references and the in-reply-to header looks
9130                 ;; promising.
9131                 (if (and (search-forward "\nin-reply-to: " nil t)
9132                          (setq in-reply-to (gnus-header-value))
9133                          (string-match "<[^>]+>" in-reply-to))
9134                     (setq ref (substring in-reply-to (match-beginning 0)
9135                                          (match-end 0)))
9136                   (setq ref ""))))
9137             ;; Chars.
9138             0
9139             ;; Lines.
9140             (progn
9141               (goto-char p)
9142               (if (search-forward "\nlines: " nil t)
9143                   (if (numberp (setq lines (read cur)))
9144                       lines 0)
9145                 0))
9146             ;; Xref.
9147             (progn
9148               (goto-char p)
9149               (and (search-forward "\nxref: " nil t)
9150                    (gnus-header-value)))))
9151           ;; We do the threading while we read the headers.  The
9152           ;; message-id and the last reference are both entered into
9153           ;; the same hash table.  Some tippy-toeing around has to be
9154           ;; done in case an article has arrived before the article
9155           ;; which it refers to.
9156           (if (boundp (setq id-dep (intern id dependencies)))
9157               (if (and (car (symbol-value id-dep))
9158                        (not force-new))
9159                   ;; An article with this Message-ID has already
9160                   ;; been seen, so we ignore this one, except we add
9161                   ;; any additional Xrefs (in case the two articles
9162                   ;; came from different servers).
9163                   (progn
9164                     (mail-header-set-xref
9165                      (car (symbol-value id-dep))
9166                      (concat (or (mail-header-xref
9167                                   (car (symbol-value id-dep))) "")
9168                              (or (mail-header-xref header) "")))
9169                     (setq header nil))
9170                 (setcar (symbol-value id-dep) header))
9171             (set id-dep (list header)))
9172           (when header
9173             (if (boundp (setq ref-dep (intern ref dependencies)))
9174                 (setcdr (symbol-value ref-dep)
9175                         (nconc (cdr (symbol-value ref-dep))
9176                                (list (symbol-value id-dep))))
9177               (set ref-dep (list nil (symbol-value id-dep))))
9178             (setq headers (cons header headers)))
9179           (goto-char (point-max))
9180           (widen))
9181         (nreverse headers)))))
9182
9183 ;; The following macros and functions were written by Felix Lee
9184 ;; <flee@cse.psu.edu>.
9185
9186 (defmacro gnus-nov-read-integer ()
9187   '(prog1
9188        (if (= (following-char) ?\t)
9189            0
9190          (let ((num (condition-case nil (read buffer) (error nil))))
9191            (if (numberp num) num 0)))
9192      (or (eobp) (forward-char 1))))
9193
9194 (defmacro gnus-nov-skip-field ()
9195   '(search-forward "\t" eol 'move))
9196
9197 (defmacro gnus-nov-field ()
9198   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9199
9200 ;; Goes through the xover lines and returns a list of vectors
9201 (defun gnus-get-newsgroup-headers-xover (sequence &optional 
9202                                                   force-new dependencies)
9203   "Parse the news overview data in the server buffer, and return a
9204 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9205   ;; Get the Xref when the users reads the articles since most/some
9206   ;; NNTP servers do not include Xrefs when using XOVER.
9207   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9208   (let ((cur nntp-server-buffer)
9209         (dependencies (or dependencies gnus-newsgroup-dependencies))
9210         number headers header)
9211     (save-excursion
9212       (set-buffer nntp-server-buffer)
9213       ;; Allow the user to mangle the headers before parsing them.
9214       (run-hooks 'gnus-parse-headers-hook)
9215       (goto-char (point-min))
9216       (while (and sequence (not (eobp)))
9217         (setq number (read cur))
9218         (while (and sequence (< (car sequence) number))
9219           (setq sequence (cdr sequence)))
9220         (and sequence
9221              (eq number (car sequence))
9222              (progn
9223                (setq sequence (cdr sequence))
9224                (if (setq header
9225                          (inline (gnus-nov-parse-line
9226                                   number dependencies force-new)))
9227                    (setq headers (cons header headers)))))
9228         (forward-line 1))
9229       (setq headers (nreverse headers)))
9230     headers))
9231
9232 ;; This function has to be called with point after the article number
9233 ;; on the beginning of the line.
9234 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9235   (let ((none 0)
9236         (eol (gnus-point-at-eol))
9237         (buffer (current-buffer))
9238         header ref id id-dep ref-dep)
9239
9240     ;; overview: [num subject from date id refs chars lines misc]
9241     (narrow-to-region (point) eol)
9242     (or (eobp) (forward-char))
9243
9244     (condition-case nil
9245         (setq header
9246               (vector
9247                number                   ; number
9248                (gnus-nov-field)         ; subject
9249                (gnus-nov-field)         ; from
9250                (gnus-nov-field)         ; date
9251                (setq id (or (gnus-nov-field)
9252                             (concat "none+"
9253                                     (int-to-string
9254                                      (setq none (1+ none)))))) ; id
9255                (progn
9256                  (save-excursion
9257                    (let ((beg (point)))
9258                      (search-forward "\t" eol)
9259                      (if (search-backward ">" beg t)
9260                          (setq ref
9261                                (buffer-substring
9262                                 (1+ (point))
9263                                 (search-backward "<" beg t)))
9264                        (setq ref nil))))
9265                  (gnus-nov-field))      ; refs
9266                (gnus-nov-read-integer)  ; chars
9267                (gnus-nov-read-integer)  ; lines
9268                (if (= (following-char) ?\n)
9269                    nil
9270                  (gnus-nov-field))      ; misc
9271                ))
9272       (error (progn
9273                (gnus-error 4 "Strange nov line")
9274                (setq header nil)
9275                (goto-char eol))))
9276
9277     (widen)
9278
9279     ;; We build the thread tree.
9280     (when header
9281       (if (boundp (setq id-dep (intern id dependencies)))
9282           (if (and (car (symbol-value id-dep))
9283                    (not force-new))
9284               ;; An article with this Message-ID has already been seen,
9285               ;; so we ignore this one, except we add any additional
9286               ;; Xrefs (in case the two articles came from different
9287               ;; servers.
9288               (progn
9289                 (mail-header-set-xref
9290                  (car (symbol-value id-dep))
9291                  (concat (or (mail-header-xref
9292                               (car (symbol-value id-dep))) "")
9293                          (or (mail-header-xref header) "")))
9294                 (setq header nil))
9295             (setcar (symbol-value id-dep) header))
9296         (set id-dep (list header))))
9297     (if header
9298         (progn
9299           (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9300               (setcdr (symbol-value ref-dep)
9301                       (nconc (cdr (symbol-value ref-dep))
9302                              (list (symbol-value id-dep))))
9303             (set ref-dep (list nil (symbol-value id-dep))))))
9304     header))
9305
9306 (defun gnus-article-get-xrefs ()
9307   "Fill in the Xref value in `gnus-current-headers', if necessary.
9308 This is meant to be called in `gnus-article-internal-prepare-hook'."
9309   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9310                                  gnus-current-headers)))
9311     (or (not gnus-use-cross-reference)
9312         (not headers)
9313         (and (mail-header-xref headers)
9314              (not (string= (mail-header-xref headers) "")))
9315         (let ((case-fold-search t)
9316               xref)
9317           (save-restriction
9318             (nnheader-narrow-to-headers)
9319             (goto-char (point-min))
9320             (if (or (and (eq (downcase (following-char)) ?x)
9321                          (looking-at "Xref:"))
9322                     (search-forward "\nXref:" nil t))
9323                 (progn
9324                   (goto-char (1+ (match-end 0)))
9325                   (setq xref (buffer-substring (point)
9326                                                (progn (end-of-line) (point))))
9327                   (mail-header-set-xref headers xref))))))))
9328
9329 (defun gnus-summary-insert-subject (id &optional old-header)
9330   "Find article ID and insert the summary line for that article."
9331   (let ((header (gnus-read-header id))
9332         (number (and (numberp id) id))
9333         pos)
9334     (when header
9335       ;; Rebuild the thread that this article is part of and go to the
9336       ;; article we have fetched.
9337       (when old-header
9338         (when (setq pos (text-property-any
9339                          (point-min) (point-max) 'gnus-number 
9340                          (mail-header-number old-header)))
9341           (goto-char pos)
9342           (gnus-delete-line)
9343           (gnus-data-remove (mail-header-number old-header))))
9344       (when old-header
9345         (mail-header-set-number header (mail-header-number old-header)))
9346       (setq gnus-newsgroup-sparse
9347             (delq (mail-header-number header) gnus-newsgroup-sparse))
9348       (gnus-rebuild-thread (mail-header-id header))
9349       (gnus-summary-goto-subject (setq number (mail-header-number header))
9350                                  nil t))
9351     (when (and (numberp number)
9352                (> number 0))
9353       ;; We have to update the boundaries even if we can't fetch the
9354       ;; article if ID is a number -- so that the next `P' or `N'
9355       ;; command will fetch the previous (or next) article even
9356       ;; if the one we tried to fetch this time has been canceled.
9357       (and (> number gnus-newsgroup-end)
9358            (setq gnus-newsgroup-end number))
9359       (and (< number gnus-newsgroup-begin)
9360            (setq gnus-newsgroup-begin number))
9361       (setq gnus-newsgroup-unselected
9362             (delq number gnus-newsgroup-unselected)))
9363     ;; Report back a success?
9364     (and header (mail-header-number header))))
9365
9366 (defun gnus-summary-work-articles (n)
9367   "Return a list of articles to be worked upon.  The prefix argument,
9368 the list of process marked articles, and the current article will be
9369 taken into consideration."
9370   (cond
9371    ((and n (numberp n))
9372     ;; A numerical prefix has been given.
9373     (let ((backward (< n 0))
9374           (n (abs n))
9375           articles article)
9376       (save-excursion
9377         (while
9378             (and (> n 0)
9379                  (push (setq article (gnus-summary-article-number))
9380                        articles)
9381                  (if backward
9382                      (gnus-summary-find-prev nil article)
9383                    (gnus-summary-find-next nil article)))
9384           (decf n)))
9385       (nreverse articles)))
9386    ((and (boundp 'transient-mark-mode)
9387          transient-mark-mode
9388          mark-active)
9389     ;; Work on the region between point and mark.
9390     (let ((max (max (point) (mark)))
9391           articles article)
9392       (save-excursion
9393         (goto-char (min (point) (mark)))
9394         (while
9395             (and
9396              (push (setq article (gnus-summary-article-number)) articles)
9397              (gnus-summary-find-next nil article)
9398              (< (point) max)))
9399         (nreverse articles))))
9400    (gnus-newsgroup-processable
9401     ;; There are process-marked articles present.
9402     (reverse gnus-newsgroup-processable))
9403    (t
9404     ;; Just return the current article.
9405     (list (gnus-summary-article-number)))))
9406
9407 (defun gnus-summary-search-group (&optional backward use-level)
9408   "Search for next unread newsgroup.
9409 If optional argument BACKWARD is non-nil, search backward instead."
9410   (save-excursion
9411     (set-buffer gnus-group-buffer)
9412     (if (gnus-group-search-forward
9413          backward nil (if use-level (gnus-group-group-level) nil))
9414         (gnus-group-group-name))))
9415
9416 (defun gnus-summary-best-group (&optional exclude-group)
9417   "Find the name of the best unread group.
9418 If EXCLUDE-GROUP, do not go to this group."
9419   (save-excursion
9420     (set-buffer gnus-group-buffer)
9421     (save-excursion
9422       (gnus-group-best-unread-group exclude-group))))
9423
9424 (defun gnus-summary-find-next (&optional unread article backward)
9425   (if backward (gnus-summary-find-prev)
9426     (let* ((dummy (gnus-summary-article-intangible-p))
9427            (article (or article (gnus-summary-article-number)))
9428            (arts (gnus-data-find-list article))
9429            result)
9430       (when (and (not dummy)
9431                  (or (not gnus-summary-check-current)
9432                      (not unread)
9433                      (not (gnus-data-unread-p (car arts)))))
9434         (setq arts (cdr arts)))
9435       (when (setq result
9436                   (if unread
9437                       (progn
9438                         (while arts
9439                           (when (gnus-data-unread-p (car arts))
9440                             (setq result (car arts)
9441                                   arts nil))
9442                           (setq arts (cdr arts)))
9443                         result)
9444                     (car arts)))
9445         (goto-char (gnus-data-pos result))
9446         (gnus-data-number result)))))
9447
9448 (defun gnus-summary-find-prev (&optional unread article)
9449   (let* ((article (or article (gnus-summary-article-number)))
9450          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9451          result)
9452     (when (or (not gnus-summary-check-current)
9453               (not unread)
9454               (not (gnus-data-unread-p (car arts))))
9455       (setq arts (cdr arts)))
9456     (if (setq result
9457               (if unread
9458                   (progn
9459                     (while arts
9460                       (and (gnus-data-unread-p (car arts))
9461                            (setq result (car arts)
9462                                  arts nil))
9463                       (setq arts (cdr arts)))
9464                     result)
9465                 (car arts)))
9466         (progn
9467           (goto-char (gnus-data-pos result))
9468           (gnus-data-number result)))))
9469
9470 (defun gnus-summary-find-subject (subject &optional unread backward article)
9471   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9472          (article (or article (gnus-summary-article-number)))
9473          (articles (gnus-data-list backward))
9474          (arts (gnus-data-find-list article articles))
9475          result)
9476     (when (or (not gnus-summary-check-current)
9477               (not unread)
9478               (not (gnus-data-unread-p (car arts))))
9479       (setq arts (cdr arts)))
9480     (while arts
9481       (and (or (not unread)
9482                (gnus-data-unread-p (car arts)))
9483            (vectorp (gnus-data-header (car arts)))
9484            (gnus-subject-equal
9485             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9486            (setq result (car arts)
9487                  arts nil))
9488       (setq arts (cdr arts)))
9489     (and result
9490          (goto-char (gnus-data-pos result))
9491          (gnus-data-number result))))
9492
9493 (defun gnus-summary-search-forward (&optional unread subject backward)
9494   "Search forward for an article.
9495 If UNREAD, look for unread articles.  If SUBJECT, look for
9496 articles with that subject.  If BACKWARD, search backward instead."
9497   (cond (subject (gnus-summary-find-subject subject unread backward))
9498         (backward (gnus-summary-find-prev unread))
9499         (t (gnus-summary-find-next unread))))
9500
9501 (defun gnus-recenter (&optional n)
9502   "Center point in window and redisplay frame.
9503 Also do horizontal recentering."
9504   (interactive "P")
9505   (when (and gnus-auto-center-summary
9506              (not (eq gnus-auto-center-summary 'vertical)))
9507     (gnus-horizontal-recenter))
9508   (recenter n))
9509
9510 (defun gnus-summary-recenter ()
9511   "Center point in the summary window.
9512 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9513 displayed, no centering will be performed."
9514   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9515   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9516   (let* ((top (cond ((< (window-height) 4) 0)
9517                     ((< (window-height) 7) 1)
9518                     (t 2)))
9519          (height (1- (window-height)))
9520          (bottom (save-excursion (goto-char (point-max))
9521                                  (forward-line (- height))
9522                                  (point)))
9523          (window (get-buffer-window (current-buffer))))
9524     ;; The user has to want it.
9525     (when gnus-auto-center-summary
9526       (when (get-buffer-window gnus-article-buffer)
9527        ;; Only do recentering when the article buffer is displayed,
9528        ;; Set the window start to either `bottom', which is the biggest
9529        ;; possible valid number, or the second line from the top,
9530        ;; whichever is the least.
9531        (set-window-start
9532         window (min bottom (save-excursion 
9533                              (forward-line (- top)) (point)))))
9534       ;; Do horizontal recentering while we're at it.
9535       (when (and (get-buffer-window (current-buffer) t)
9536                  (not (eq gnus-auto-center-summary 'vertical)))
9537         (let ((selected (selected-window)))
9538           (select-window (get-buffer-window (current-buffer) t))
9539           (gnus-summary-position-point)
9540           (gnus-horizontal-recenter)
9541           (select-window selected))))))
9542
9543 (defun gnus-horizontal-recenter ()
9544   "Recenter the current buffer horizontally."
9545   (if (< (current-column) (/ (window-width) 2))
9546       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9547     (let* ((orig (point))
9548            (end (window-end (get-buffer-window (current-buffer) t)))
9549            (max 0))
9550       ;; Find the longest line currently displayed in the window.
9551       (goto-char (window-start))
9552       (while (and (not (eobp)) 
9553                   (< (point) end))
9554         (end-of-line)
9555         (setq max (max max (current-column)))
9556         (forward-line 1))
9557       (goto-char orig)
9558       ;; Scroll horizontally to center (sort of) the point.
9559       (if (> max (window-width))
9560           (set-window-hscroll 
9561            (get-buffer-window (current-buffer) t)
9562            (min (- (current-column) (/ (window-width) 3))
9563                 (+ 2 (- max (window-width)))))
9564         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9565       max)))
9566
9567 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9568 (defun gnus-short-group-name (group &optional levels)
9569   "Collapse GROUP name LEVELS."
9570   (let* ((name "") 
9571          (foreign "")
9572          (depth 0) 
9573          (skip 1)
9574          (levels (or levels
9575                      (progn
9576                        (while (string-match "\\." group skip)
9577                          (setq skip (match-end 0)
9578                                depth (+ depth 1)))
9579                        depth))))
9580     (if (string-match ":" group)
9581         (setq foreign (substring group 0 (match-end 0))
9582               group (substring group (match-end 0))))
9583     (while group
9584       (if (and (string-match "\\." group)
9585                (> levels (- gnus-group-uncollapsed-levels 1)))
9586           (setq name (concat name (substring group 0 1))
9587                 group (substring group (match-end 0))
9588                 levels (- levels 1)
9589                 name (concat name "."))
9590         (setq name (concat foreign name group)
9591               group nil)))
9592     name))
9593
9594 (defun gnus-summary-jump-to-group (newsgroup)
9595   "Move point to NEWSGROUP in group mode buffer."
9596   ;; Keep update point of group mode buffer if visible.
9597   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9598       (save-window-excursion
9599         ;; Take care of tree window mode.
9600         (if (get-buffer-window gnus-group-buffer)
9601             (pop-to-buffer gnus-group-buffer))
9602         (gnus-group-jump-to-group newsgroup))
9603     (save-excursion
9604       ;; Take care of tree window mode.
9605       (if (get-buffer-window gnus-group-buffer)
9606           (pop-to-buffer gnus-group-buffer)
9607         (set-buffer gnus-group-buffer))
9608       (gnus-group-jump-to-group newsgroup))))
9609
9610 ;; This function returns a list of article numbers based on the
9611 ;; difference between the ranges of read articles in this group and
9612 ;; the range of active articles.
9613 (defun gnus-list-of-unread-articles (group)
9614   (let* ((read (gnus-info-read (gnus-get-info group)))
9615          (active (gnus-active group))
9616          (last (cdr active))
9617          first nlast unread)
9618     ;; If none are read, then all are unread.
9619     (if (not read)
9620         (setq first (car active))
9621       ;; If the range of read articles is a single range, then the
9622       ;; first unread article is the article after the last read
9623       ;; article.  Sounds logical, doesn't it?
9624       (if (not (listp (cdr read)))
9625           (setq first (1+ (cdr read)))
9626         ;; `read' is a list of ranges.
9627         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9628                                 (caar read))) 1)
9629             (setq first 1))
9630         (while read
9631           (if first
9632               (while (< first nlast)
9633                 (setq unread (cons first unread))
9634                 (setq first (1+ first))))
9635           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9636           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9637           (setq read (cdr read)))))
9638     ;; And add the last unread articles.
9639     (while (<= first last)
9640       (setq unread (cons first unread))
9641       (setq first (1+ first)))
9642     ;; Return the list of unread articles.
9643     (nreverse unread)))
9644
9645 (defun gnus-list-of-read-articles (group)
9646   "Return a list of unread, unticked and non-dormant articles."
9647   (let* ((info (gnus-get-info group))
9648          (marked (gnus-info-marks info))
9649          (active (gnus-active group)))
9650     (and info active
9651          (gnus-set-difference
9652           (gnus-sorted-complement
9653            (gnus-uncompress-range active)
9654            (gnus-list-of-unread-articles group))
9655           (append
9656            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9657            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9658
9659 ;; Various summary commands
9660
9661 (defun gnus-summary-universal-argument (arg)
9662   "Perform any operation on all articles that are process/prefixed."
9663   (interactive "P")
9664   (gnus-set-global-variables)
9665   (let ((articles (gnus-summary-work-articles arg))
9666         func article)
9667     (if (eq
9668          (setq
9669           func
9670           (key-binding
9671            (read-key-sequence
9672             (substitute-command-keys
9673              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9674              ))))
9675          'undefined)
9676         (gnus-error 1 "Undefined key")
9677       (save-excursion
9678         (while articles
9679           (gnus-summary-goto-subject (setq article (pop articles)))
9680           (command-execute func)
9681           (gnus-summary-remove-process-mark article)))))
9682   (gnus-summary-position-point))
9683
9684 (defun gnus-summary-toggle-truncation (&optional arg)
9685   "Toggle truncation of summary lines.
9686 With arg, turn line truncation on iff arg is positive."
9687   (interactive "P")
9688   (setq truncate-lines
9689         (if (null arg) (not truncate-lines)
9690           (> (prefix-numeric-value arg) 0)))
9691   (redraw-display))
9692
9693 (defun gnus-summary-reselect-current-group (&optional all rescan)
9694   "Exit and then reselect the current newsgroup.
9695 The prefix argument ALL means to select all articles."
9696   (interactive "P")
9697   (gnus-set-global-variables)
9698   (let ((current-subject (gnus-summary-article-number))
9699         (group gnus-newsgroup-name))
9700     (setq gnus-newsgroup-begin nil)
9701     (gnus-summary-exit)
9702     ;; We have to adjust the point of group mode buffer because the
9703     ;; current point was moved to the next unread newsgroup by
9704     ;; exiting.
9705     (gnus-summary-jump-to-group group)
9706     (when rescan
9707       (save-excursion
9708         (gnus-group-get-new-news-this-group 1)))
9709     (gnus-group-read-group all t)
9710     (gnus-summary-goto-subject current-subject)))
9711
9712 (defun gnus-summary-rescan-group (&optional all)
9713   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9714   (interactive "P")
9715   (gnus-summary-reselect-current-group all t))
9716
9717 (defun gnus-summary-update-info ()
9718   (let* ((group gnus-newsgroup-name))
9719     (when gnus-newsgroup-kill-headers
9720       (setq gnus-newsgroup-killed
9721             (gnus-compress-sequence
9722              (nconc
9723               (gnus-set-sorted-intersection
9724                (gnus-uncompress-range gnus-newsgroup-killed)
9725                (setq gnus-newsgroup-unselected
9726                      (sort gnus-newsgroup-unselected '<)))
9727               (setq gnus-newsgroup-unreads
9728                     (sort gnus-newsgroup-unreads '<))) t)))
9729     (unless (listp (cdr gnus-newsgroup-killed))
9730       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9731     (let ((headers gnus-newsgroup-headers))
9732       (run-hooks 'gnus-exit-group-hook)
9733       (unless gnus-save-score
9734         (setq gnus-newsgroup-scored nil))
9735       ;; Set the new ranges of read articles.
9736       (gnus-update-read-articles
9737        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9738       ;; Set the current article marks.
9739       (gnus-update-marks)
9740       ;; Do the cross-ref thing.
9741       (when gnus-use-cross-reference
9742         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9743       ;; Do adaptive scoring, and possibly save score files.
9744       (when gnus-newsgroup-adaptive
9745         (gnus-score-adaptive))
9746       (when gnus-use-scoring
9747         (gnus-score-save))
9748       ;; Do not switch windows but change the buffer to work.
9749       (set-buffer gnus-group-buffer)
9750       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9751           (gnus-group-update-group group)))))
9752
9753 (defun gnus-summary-exit (&optional temporary)
9754   "Exit reading current newsgroup, and then return to group selection mode.
9755 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9756   (interactive)
9757   (gnus-set-global-variables)
9758   (gnus-kill-save-kill-buffer)
9759   (let* ((group gnus-newsgroup-name)
9760          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9761          (mode major-mode)
9762          (buf (current-buffer)))
9763     (run-hooks 'gnus-summary-prepare-exit-hook)
9764     ;; If we have several article buffers, we kill them at exit.
9765     (unless gnus-single-article-buffer
9766       (gnus-kill-buffer gnus-original-article-buffer)
9767       (setq gnus-article-current nil))
9768     (when gnus-use-cache
9769       (gnus-cache-possibly-remove-articles)
9770       (gnus-cache-save-buffers))
9771     (when gnus-use-trees
9772       (gnus-tree-close group))
9773     ;; Make all changes in this group permanent.
9774     (unless quit-config
9775       (gnus-summary-update-info))
9776     (gnus-close-group group)
9777     ;; Make sure where I was, and go to next newsgroup.
9778     (set-buffer gnus-group-buffer)
9779     (unless quit-config
9780       (gnus-group-jump-to-group group)
9781       (gnus-group-next-unread-group 1))
9782     (run-hooks 'gnus-summary-exit-hook)
9783     (if temporary
9784         nil                             ;Nothing to do.
9785       ;; If we have several article buffers, we kill them at exit.
9786       (unless gnus-single-article-buffer
9787         (gnus-kill-buffer gnus-article-buffer)
9788         (gnus-kill-buffer gnus-original-article-buffer)
9789         (setq gnus-article-current nil))
9790       (set-buffer buf)
9791       (if (not gnus-kill-summary-on-exit)
9792           (gnus-deaden-summary)
9793         ;; We set all buffer-local variables to nil.  It is unclear why
9794         ;; this is needed, but if we don't, buffer-local variables are
9795         ;; not garbage-collected, it seems.  This would the lead to en
9796         ;; ever-growing Emacs.
9797         (gnus-summary-clear-local-variables)
9798         (when (get-buffer gnus-article-buffer)
9799           (bury-buffer gnus-article-buffer))
9800         ;; We clear the global counterparts of the buffer-local
9801         ;; variables as well, just to be on the safe side.
9802         (gnus-configure-windows 'group 'force)
9803         (gnus-summary-clear-local-variables)
9804         ;; Return to group mode buffer.
9805         (if (eq mode 'gnus-summary-mode)
9806             (gnus-kill-buffer buf)))
9807       (setq gnus-current-select-method gnus-select-method)
9808       (pop-to-buffer gnus-group-buffer)
9809       ;; Clear the current group name.
9810       (if (not quit-config)
9811           (progn
9812             (gnus-group-jump-to-group group)
9813             (gnus-group-next-unread-group 1)
9814             (gnus-configure-windows 'group 'force))
9815         (if (not (buffer-name (car quit-config)))
9816             (gnus-configure-windows 'group 'force)
9817           (set-buffer (car quit-config))
9818           (and (eq major-mode 'gnus-summary-mode)
9819                (gnus-set-global-variables))
9820           (gnus-configure-windows (cdr quit-config))))
9821       (unless quit-config
9822         (setq gnus-newsgroup-name nil)))))
9823
9824 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9825 (defun gnus-summary-exit-no-update (&optional no-questions)
9826   "Quit reading current newsgroup without updating read article info."
9827   (interactive)
9828   (gnus-set-global-variables)
9829   (let* ((group gnus-newsgroup-name)
9830          (quit-config (gnus-group-quit-config group)))
9831     (when (or no-questions
9832               gnus-expert-user
9833               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9834       ;; If we have several article buffers, we kill them at exit.
9835       (unless gnus-single-article-buffer
9836         (gnus-kill-buffer gnus-article-buffer)
9837         (gnus-kill-buffer gnus-original-article-buffer)
9838         (setq gnus-article-current nil))
9839       (if (not gnus-kill-summary-on-exit)
9840           (gnus-deaden-summary)
9841         (gnus-close-group group)
9842         (gnus-summary-clear-local-variables)
9843         (set-buffer gnus-group-buffer)
9844         (gnus-summary-clear-local-variables)
9845         (when (get-buffer gnus-summary-buffer)
9846           (kill-buffer gnus-summary-buffer)))
9847       (unless gnus-single-article-buffer
9848         (setq gnus-article-current nil))
9849       (when gnus-use-trees
9850         (gnus-tree-close group))
9851       (when (get-buffer gnus-article-buffer)
9852         (bury-buffer gnus-article-buffer))
9853       ;; Return to the group buffer.
9854       (gnus-configure-windows 'group 'force)
9855       ;; Clear the current group name.
9856       (setq gnus-newsgroup-name nil)
9857       (when (equal (gnus-group-group-name) group)
9858         (gnus-group-next-unread-group 1))
9859       (when quit-config
9860         (if (not (buffer-name (car quit-config)))
9861             (gnus-configure-windows 'group 'force)
9862           (set-buffer (car quit-config))
9863           (when (eq major-mode 'gnus-summary-mode)
9864             (gnus-set-global-variables))
9865           (gnus-configure-windows (cdr quit-config)))))))
9866
9867 ;;; Dead summaries.
9868
9869 (defvar gnus-dead-summary-mode-map nil)
9870
9871 (if gnus-dead-summary-mode-map
9872     nil
9873   (setq gnus-dead-summary-mode-map (make-keymap))
9874   (suppress-keymap gnus-dead-summary-mode-map)
9875   (substitute-key-definition
9876    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
9877   (let ((keys '("\C-d" "\r" "\177")))
9878     (while keys
9879       (define-key gnus-dead-summary-mode-map
9880         (pop keys) 'gnus-summary-wake-up-the-dead))))
9881
9882 (defvar gnus-dead-summary-mode nil
9883   "Minor mode for Gnus summary buffers.")
9884
9885 (defun gnus-dead-summary-mode (&optional arg)
9886   "Minor mode for Gnus summary buffers."
9887   (interactive "P")
9888   (when (eq major-mode 'gnus-summary-mode)
9889     (make-local-variable 'gnus-dead-summary-mode)
9890     (setq gnus-dead-summary-mode
9891           (if (null arg) (not gnus-dead-summary-mode)
9892             (> (prefix-numeric-value arg) 0)))
9893     (when gnus-dead-summary-mode
9894       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
9895         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
9896       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
9897         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
9898               minor-mode-map-alist)))))
9899
9900 (defun gnus-deaden-summary ()
9901   "Make the current summary buffer into a dead summary buffer."
9902   ;; Kill any previous dead summary buffer.
9903   (when (and gnus-dead-summary
9904              (buffer-name gnus-dead-summary))
9905     (save-excursion
9906       (set-buffer gnus-dead-summary)
9907       (when gnus-dead-summary-mode
9908         (kill-buffer (current-buffer)))))
9909   ;; Make this the current dead summary.
9910   (setq gnus-dead-summary (current-buffer))
9911   (gnus-dead-summary-mode 1)
9912   (let ((name (buffer-name)))
9913     (when (string-match "Summary" name)
9914       (rename-buffer
9915        (concat (substring name 0 (match-beginning 0)) "Dead "
9916                (substring name (match-beginning 0))) t))))
9917
9918 (defun gnus-kill-or-deaden-summary (buffer)
9919   "Kill or deaden the summary BUFFER."
9920   (when (and (buffer-name buffer)
9921              (not gnus-single-article-buffer))
9922     (save-excursion
9923       (set-buffer buffer)
9924       (gnus-kill-buffer gnus-article-buffer)
9925       (gnus-kill-buffer gnus-original-article-buffer)))
9926   (cond (gnus-kill-summary-on-exit
9927          (when (and gnus-use-trees
9928                     (and (get-buffer buffer)
9929                          (buffer-name (get-buffer buffer))))
9930            (save-excursion
9931              (set-buffer (get-buffer buffer))
9932              (gnus-tree-close gnus-newsgroup-name)))
9933          (gnus-kill-buffer buffer))
9934         ((and (get-buffer buffer)
9935               (buffer-name (get-buffer buffer)))
9936          (save-excursion
9937            (set-buffer buffer)
9938            (gnus-deaden-summary)))))
9939
9940 (defun gnus-summary-wake-up-the-dead (&rest args)
9941   "Wake up the dead summary buffer."
9942   (interactive)
9943   (gnus-dead-summary-mode -1)
9944   (let ((name (buffer-name)))
9945     (when (string-match "Dead " name)
9946       (rename-buffer
9947        (concat (substring name 0 (match-beginning 0))
9948                (substring name (match-end 0))) t)))
9949   (gnus-message 3 "This dead summary is now alive again"))
9950
9951 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
9952 (defun gnus-summary-fetch-faq (&optional faq-dir)
9953   "Fetch the FAQ for the current group.
9954 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
9955 in."
9956   (interactive
9957    (list
9958     (if current-prefix-arg
9959         (completing-read
9960          "Faq dir: " (and (listp gnus-group-faq-directory)
9961                           gnus-group-faq-directory)))))
9962   (let (gnus-faq-buffer)
9963     (and (setq gnus-faq-buffer
9964                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
9965          (gnus-configure-windows 'summary-faq))))
9966
9967 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
9968 (defun gnus-summary-describe-group (&optional force)
9969   "Describe the current newsgroup."
9970   (interactive "P")
9971   (gnus-group-describe-group force gnus-newsgroup-name))
9972
9973 (defun gnus-summary-describe-briefly ()
9974   "Describe summary mode commands briefly."
9975   (interactive)
9976   (gnus-message 6
9977                 (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")))
9978
9979 ;; Walking around group mode buffer from summary mode.
9980
9981 (defun gnus-summary-next-group (&optional no-article target-group backward)
9982   "Exit current newsgroup and then select next unread newsgroup.
9983 If prefix argument NO-ARTICLE is non-nil, no article is selected
9984 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
9985 previous group instead."
9986   (interactive "P")
9987   (gnus-set-global-variables)
9988   (let ((current-group gnus-newsgroup-name)
9989         (current-buffer (current-buffer))
9990         entered)
9991     ;; First we semi-exit this group to update Xrefs and all variables.
9992     ;; We can't do a real exit, because the window conf must remain
9993     ;; the same in case the user is prompted for info, and we don't
9994     ;; want the window conf to change before that...
9995     (gnus-summary-exit t)
9996     (while (not entered)
9997       ;; Then we find what group we are supposed to enter.
9998       (set-buffer gnus-group-buffer)
9999       (gnus-group-jump-to-group current-group)
10000       (setq target-group
10001             (or target-group
10002                 (if (eq gnus-keep-same-level 'best)
10003                     (gnus-summary-best-group gnus-newsgroup-name)
10004                   (gnus-summary-search-group backward gnus-keep-same-level))))
10005       (if (not target-group)
10006           ;; There are no further groups, so we return to the group
10007           ;; buffer.
10008           (progn
10009             (gnus-message 5 "Returning to the group buffer")
10010             (setq entered t)
10011             (set-buffer current-buffer)
10012             (gnus-summary-exit))
10013         ;; We try to enter the target group.
10014         (gnus-group-jump-to-group target-group)
10015         (let ((unreads (gnus-group-group-unread)))
10016           (if (and (or (eq t unreads)
10017                        (and unreads (not (zerop unreads))))
10018                    (gnus-summary-read-group
10019                     target-group nil no-article current-buffer))
10020               (setq entered t)
10021             (setq current-group target-group
10022                   target-group nil)))))))
10023
10024 (defun gnus-summary-prev-group (&optional no-article)
10025   "Exit current newsgroup and then select previous unread newsgroup.
10026 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
10027   (interactive "P")
10028   (gnus-summary-next-group no-article nil t))
10029
10030 ;; Walking around summary lines.
10031
10032 (defun gnus-summary-first-subject (&optional unread)
10033   "Go to the first unread subject.
10034 If UNREAD is non-nil, go to the first unread article.
10035 Returns the article selected or nil if there are no unread articles."
10036   (interactive "P")
10037   (prog1
10038       (cond
10039        ;; Empty summary.
10040        ((null gnus-newsgroup-data)
10041         (gnus-message 3 "No articles in the group")
10042         nil)
10043        ;; Pick the first article.
10044        ((not unread)
10045         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
10046         (gnus-data-number (car gnus-newsgroup-data)))
10047        ;; No unread articles.
10048        ((null gnus-newsgroup-unreads)
10049         (gnus-message 3 "No more unread articles")
10050         nil)
10051        ;; Find the first unread article.
10052        (t
10053         (let ((data gnus-newsgroup-data))
10054           (while (and data
10055                       (not (gnus-data-unread-p (car data))))
10056             (setq data (cdr data)))
10057           (if data
10058               (progn
10059                 (goto-char (gnus-data-pos (car data)))
10060                 (gnus-data-number (car data)))))))
10061     (gnus-summary-position-point)))
10062
10063 (defun gnus-summary-next-subject (n &optional unread dont-display)
10064   "Go to next N'th summary line.
10065 If N is negative, go to the previous N'th subject line.
10066 If UNREAD is non-nil, only unread articles are selected.
10067 The difference between N and the actual number of steps taken is
10068 returned."
10069   (interactive "p")
10070   (let ((backward (< n 0))
10071         (n (abs n)))
10072     (while (and (> n 0)
10073                 (if backward
10074                     (gnus-summary-find-prev unread)
10075                   (gnus-summary-find-next unread)))
10076       (setq n (1- n)))
10077     (if (/= 0 n) (gnus-message 7 "No more%s articles"
10078                                (if unread " unread" "")))
10079     (unless dont-display
10080       (gnus-summary-recenter)
10081       (gnus-summary-position-point))
10082     n))
10083
10084 (defun gnus-summary-next-unread-subject (n)
10085   "Go to next N'th unread summary line."
10086   (interactive "p")
10087   (gnus-summary-next-subject n t))
10088
10089 (defun gnus-summary-prev-subject (n &optional unread)
10090   "Go to previous N'th summary line.
10091 If optional argument UNREAD is non-nil, only unread article is selected."
10092   (interactive "p")
10093   (gnus-summary-next-subject (- n) unread))
10094
10095 (defun gnus-summary-prev-unread-subject (n)
10096   "Go to previous N'th unread summary line."
10097   (interactive "p")
10098   (gnus-summary-next-subject (- n) t))
10099
10100 (defun gnus-summary-goto-subject (article &optional force silent)
10101   "Go the subject line of ARTICLE.
10102 If FORCE, also allow jumping to articles not currently shown."
10103   (let ((b (point))
10104         (data (gnus-data-find article)))
10105     ;; We read in the article if we have to.
10106     (and (not data)
10107          force
10108          (gnus-summary-insert-subject article)
10109          (setq data (gnus-data-find article)))
10110     (goto-char b)
10111     (if (not data)
10112         (progn
10113           (unless silent
10114             (gnus-message 3 "Can't find article %d" article))
10115           nil)
10116       (goto-char (gnus-data-pos data))
10117       article)))
10118
10119 ;; Walking around summary lines with displaying articles.
10120
10121 (defun gnus-summary-expand-window (&optional arg)
10122   "Make the summary buffer take up the entire Emacs frame.
10123 Given a prefix, will force an `article' buffer configuration."
10124   (interactive "P")
10125   (gnus-set-global-variables)
10126   (if arg
10127       (gnus-configure-windows 'article 'force)
10128     (gnus-configure-windows 'summary 'force)))
10129
10130 (defun gnus-summary-display-article (article &optional all-header)
10131   "Display ARTICLE in article buffer."
10132   (gnus-set-global-variables)
10133   (if (null article)
10134       nil
10135     (prog1
10136         (if gnus-summary-display-article-function
10137             (funcall gnus-summary-display-article-function article all-header)
10138           (gnus-article-prepare article all-header))
10139       (run-hooks 'gnus-select-article-hook)
10140       (unless (zerop gnus-current-article)
10141         (gnus-summary-goto-subject gnus-current-article))
10142       (gnus-summary-recenter)
10143       (when gnus-use-trees
10144         (gnus-possibly-generate-tree article)
10145         (gnus-highlight-selected-tree article))
10146       ;; Successfully display article.
10147       (gnus-article-set-window-start
10148        (cdr (assq article gnus-newsgroup-bookmarks))))))
10149
10150 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
10151   "Select the current article.
10152 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
10153 non-nil, the article will be re-fetched even if it already present in
10154 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
10155 be displayed."
10156   ;; Make sure we are in the summary buffer to work around bbdb bug.
10157   (unless (eq major-mode 'gnus-summary-mode)
10158     (set-buffer gnus-summary-buffer))
10159   (let ((article (or article (gnus-summary-article-number)))
10160         (all-headers (not (not all-headers))) ;Must be T or NIL.
10161         gnus-summary-display-article-function
10162         did)
10163     (and (not pseudo)
10164          (gnus-summary-article-pseudo-p article)
10165          (error "This is a pseudo-article."))
10166     (prog1
10167         (save-excursion
10168           (set-buffer gnus-summary-buffer)
10169           (if (or (and gnus-single-article-buffer
10170                        (or (null gnus-current-article)
10171                            (null gnus-article-current)
10172                            (null (get-buffer gnus-article-buffer))
10173                            (not (eq article (cdr gnus-article-current)))
10174                            (not (equal (car gnus-article-current)
10175                                        gnus-newsgroup-name))))
10176                   (and (not gnus-single-article-buffer)
10177                        (or (null gnus-current-article)
10178                            (not (eq gnus-current-article article))))
10179                   force)
10180               ;; The requested article is different from the current article.
10181               (prog1
10182                   (gnus-summary-display-article article all-headers)
10183                 (setq did article))
10184             (if (or all-headers gnus-show-all-headers)
10185                 (gnus-article-show-all-headers))
10186             'old))
10187       (if did
10188           (gnus-article-set-window-start
10189            (cdr (assq article gnus-newsgroup-bookmarks)))))))
10190
10191 (defun gnus-summary-set-current-mark (&optional current-mark)
10192   "Obsolete function."
10193   nil)
10194
10195 (defun gnus-summary-next-article (&optional unread subject backward push)
10196   "Select the next article.
10197 If UNREAD, only unread articles are selected.
10198 If SUBJECT, only articles with SUBJECT are selected.
10199 If BACKWARD, the previous article is selected instead of the next."
10200   (interactive "P")
10201   (gnus-set-global-variables)
10202   (cond
10203    ;; Is there such an article?
10204    ((and (gnus-summary-search-forward unread subject backward)
10205          (or (gnus-summary-display-article (gnus-summary-article-number))
10206              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10207     (gnus-summary-position-point))
10208    ;; If not, we try the first unread, if that is wanted.
10209    ((and subject
10210          gnus-auto-select-same
10211          (or (gnus-summary-first-unread-article)
10212              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10213     (gnus-summary-position-point)
10214     (gnus-message 6 "Wrapped"))
10215    ;; Try to get next/previous article not displayed in this group.
10216    ((and gnus-auto-extend-newsgroup
10217          (not unread) (not subject))
10218     (gnus-summary-goto-article
10219      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10220      nil t))
10221    ;; Go to next/previous group.
10222    (t
10223     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10224         (gnus-summary-jump-to-group gnus-newsgroup-name))
10225     (let ((cmd last-command-char)
10226           (group
10227            (if (eq gnus-keep-same-level 'best)
10228                (gnus-summary-best-group gnus-newsgroup-name)
10229              (gnus-summary-search-group backward gnus-keep-same-level))))
10230       ;; For some reason, the group window gets selected.  We change
10231       ;; it back.
10232       (select-window (get-buffer-window (current-buffer)))
10233       ;; Select next unread newsgroup automagically.
10234       (cond
10235        ((not gnus-auto-select-next)
10236         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10237        ((or (eq gnus-auto-select-next 'quietly)
10238             (and (eq gnus-auto-select-next 'slightly-quietly)
10239                  push)
10240             (and (eq gnus-auto-select-next 'almost-quietly)
10241                  (gnus-summary-last-article-p)))
10242         ;; Select quietly.
10243         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10244             (gnus-summary-exit)
10245           (gnus-message 7 "No more%s articles (%s)..."
10246                         (if unread " unread" "")
10247                         (if group (concat "selecting " group)
10248                           "exiting"))
10249           (gnus-summary-next-group nil group backward)))
10250        (t
10251         (gnus-summary-walk-group-buffer
10252          gnus-newsgroup-name cmd unread backward)))))))
10253
10254 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10255   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10256                       (?\C-p (gnus-group-prev-unread-group 1))))
10257         keve key group ended)
10258     (save-excursion
10259       (set-buffer gnus-group-buffer)
10260       (gnus-summary-jump-to-group from-group)
10261       (setq group
10262             (if (eq gnus-keep-same-level 'best)
10263                 (gnus-summary-best-group gnus-newsgroup-name)
10264               (gnus-summary-search-group backward gnus-keep-same-level))))
10265     (while (not ended)
10266       (gnus-message
10267        5 "No more%s articles%s" (if unread " unread" "")
10268        (if (and group
10269                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10270            (format " (Type %s for %s [%s])"
10271                    (single-key-description cmd) group
10272                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10273          (format " (Type %s to exit %s)"
10274                  (single-key-description cmd)
10275                  gnus-newsgroup-name)))
10276       ;; Confirm auto selection.
10277       (setq key (car (setq keve (gnus-read-event-char))))
10278       (setq ended t)
10279       (cond
10280        ((assq key keystrokes)
10281         (let ((obuf (current-buffer)))
10282           (switch-to-buffer gnus-group-buffer)
10283           (and group
10284                (gnus-group-jump-to-group group))
10285           (eval (cadr (assq key keystrokes)))
10286           (setq group (gnus-group-group-name))
10287           (switch-to-buffer obuf))
10288         (setq ended nil))
10289        ((equal key cmd)
10290         (if (or (not group)
10291                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10292             (gnus-summary-exit)
10293           (gnus-summary-next-group nil group backward)))
10294        (t
10295         (push (cdr keve) unread-command-events))))))
10296
10297 (defun gnus-read-event-char ()
10298   "Get the next event."
10299   (let ((event (read-event)))
10300     (cons (and (numberp event) event) event)))
10301
10302 (defun gnus-summary-next-unread-article ()
10303   "Select unread article after current one."
10304   (interactive)
10305   (gnus-summary-next-article t (and gnus-auto-select-same
10306                                     (gnus-summary-article-subject))))
10307
10308 (defun gnus-summary-prev-article (&optional unread subject)
10309   "Select the article after the current one.
10310 If UNREAD is non-nil, only unread articles are selected."
10311   (interactive "P")
10312   (gnus-summary-next-article unread subject t))
10313
10314 (defun gnus-summary-prev-unread-article ()
10315   "Select unred article before current one."
10316   (interactive)
10317   (gnus-summary-prev-article t (and gnus-auto-select-same
10318                                     (gnus-summary-article-subject))))
10319
10320 (defun gnus-summary-next-page (&optional lines circular)
10321   "Show next page of the selected article.
10322 If at the end of the current article, select the next article.
10323 LINES says how many lines should be scrolled up.
10324
10325 If CIRCULAR is non-nil, go to the start of the article instead of
10326 selecting the next article when reaching the end of the current
10327 article."
10328   (interactive "P")
10329   (setq gnus-summary-buffer (current-buffer))
10330   (gnus-set-global-variables)
10331   (let ((article (gnus-summary-article-number))
10332         (endp nil))
10333     (gnus-configure-windows 'article)
10334     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
10335         (if (and (eq gnus-summary-goto-unread 'never)
10336                  (not (gnus-summary-last-article-p article)))
10337             (gnus-summary-next-article)
10338           (gnus-summary-next-unread-article))
10339       (if (or (null gnus-current-article)
10340               (null gnus-article-current)
10341               (/= article (cdr gnus-article-current))
10342               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10343           ;; Selected subject is different from current article's.
10344           (gnus-summary-display-article article)
10345         (gnus-eval-in-buffer-window
10346          gnus-article-buffer
10347          (setq endp (gnus-article-next-page lines)))
10348         (if endp
10349             (cond (circular
10350                    (gnus-summary-beginning-of-article))
10351                   (lines
10352                    (gnus-message 3 "End of message"))
10353                   ((null lines)
10354                    (if (and (eq gnus-summary-goto-unread 'never)
10355                             (not (gnus-summary-last-article-p article)))
10356                        (gnus-summary-next-article)
10357                      (gnus-summary-next-unread-article)))))))
10358     (gnus-summary-recenter)
10359     (gnus-summary-position-point)))
10360
10361 (defun gnus-summary-prev-page (&optional lines)
10362   "Show previous page of selected article.
10363 Argument LINES specifies lines to be scrolled down."
10364   (interactive "P")
10365   (gnus-set-global-variables)
10366   (let ((article (gnus-summary-article-number)))
10367     (gnus-configure-windows 'article)
10368     (if (or (null gnus-current-article)
10369             (null gnus-article-current)
10370             (/= article (cdr gnus-article-current))
10371             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10372         ;; Selected subject is different from current article's.
10373         (gnus-summary-display-article article)
10374       (gnus-summary-recenter)
10375       (gnus-eval-in-buffer-window gnus-article-buffer
10376                                   (gnus-article-prev-page lines))))
10377   (gnus-summary-position-point))
10378
10379 (defun gnus-summary-scroll-up (lines)
10380   "Scroll up (or down) one line current article.
10381 Argument LINES specifies lines to be scrolled up (or down if negative)."
10382   (interactive "p")
10383   (gnus-set-global-variables)
10384   (gnus-configure-windows 'article)
10385   (gnus-summary-show-thread)
10386   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10387     (gnus-eval-in-buffer-window
10388      gnus-article-buffer
10389      (cond ((> lines 0)
10390             (if (gnus-article-next-page lines)
10391                 (gnus-message 3 "End of message")))
10392            ((< lines 0)
10393             (gnus-article-prev-page (- lines))))))
10394   (gnus-summary-recenter)
10395   (gnus-summary-position-point))
10396
10397 (defun gnus-summary-next-same-subject ()
10398   "Select next article which has the same subject as current one."
10399   (interactive)
10400   (gnus-set-global-variables)
10401   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10402
10403 (defun gnus-summary-prev-same-subject ()
10404   "Select previous article which has the same subject as current one."
10405   (interactive)
10406   (gnus-set-global-variables)
10407   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10408
10409 (defun gnus-summary-next-unread-same-subject ()
10410   "Select next unread article which has the same subject as current one."
10411   (interactive)
10412   (gnus-set-global-variables)
10413   (gnus-summary-next-article t (gnus-summary-article-subject)))
10414
10415 (defun gnus-summary-prev-unread-same-subject ()
10416   "Select previous unread article which has the same subject as current one."
10417   (interactive)
10418   (gnus-set-global-variables)
10419   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10420
10421 (defun gnus-summary-first-unread-article ()
10422   "Select the first unread article.
10423 Return nil if there are no unread articles."
10424   (interactive)
10425   (gnus-set-global-variables)
10426   (prog1
10427       (if (gnus-summary-first-subject t)
10428           (progn
10429             (gnus-summary-show-thread)
10430             (gnus-summary-first-subject t)
10431             (gnus-summary-display-article (gnus-summary-article-number))))
10432     (gnus-summary-position-point)))
10433
10434 (defun gnus-summary-best-unread-article ()
10435   "Select the unread article with the highest score."
10436   (interactive)
10437   (gnus-set-global-variables)
10438   (let ((best -1000000)
10439         (data gnus-newsgroup-data)
10440         article score)
10441     (while data
10442       (and (gnus-data-unread-p (car data))
10443            (> (setq score
10444                     (gnus-summary-article-score (gnus-data-number (car data))))
10445               best)
10446            (setq best score
10447                  article (gnus-data-number (car data))))
10448       (setq data (cdr data)))
10449     (prog1
10450         (if article
10451             (gnus-summary-goto-article article)
10452           (error "No unread articles"))
10453       (gnus-summary-position-point))))
10454
10455 (defun gnus-summary-last-subject ()
10456   "Go to the last displayed subject line in the group."
10457   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10458     (when article
10459       (gnus-summary-goto-subject article))))
10460
10461 (defun gnus-summary-goto-article (article &optional all-headers force)
10462   "Fetch ARTICLE and display it if it exists.
10463 If ALL-HEADERS is non-nil, no header lines are hidden."
10464   (interactive
10465    (list
10466     (string-to-int
10467      (completing-read
10468       "Article number: "
10469       (mapcar (lambda (number) (list (int-to-string number)))
10470               gnus-newsgroup-limit)))
10471     current-prefix-arg
10472     t))
10473   (prog1
10474       (if (gnus-summary-goto-subject article force)
10475           (gnus-summary-display-article article all-headers)
10476         (gnus-message 4 "Couldn't go to article %s" article) nil)
10477     (gnus-summary-position-point)))
10478
10479 (defun gnus-summary-goto-last-article ()
10480   "Go to the previously read article."
10481   (interactive)
10482   (prog1
10483       (and gnus-last-article
10484            (gnus-summary-goto-article gnus-last-article))
10485     (gnus-summary-position-point)))
10486
10487 (defun gnus-summary-pop-article (number)
10488   "Pop one article off the history and go to the previous.
10489 NUMBER articles will be popped off."
10490   (interactive "p")
10491   (let (to)
10492     (setq gnus-newsgroup-history
10493           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10494     (if to
10495         (gnus-summary-goto-article (car to))
10496       (error "Article history empty")))
10497   (gnus-summary-position-point))
10498
10499 ;; Summary commands and functions for limiting the summary buffer.
10500
10501 (defun gnus-summary-limit-to-articles (n)
10502   "Limit the summary buffer to the next N articles.
10503 If not given a prefix, use the process marked articles instead."
10504   (interactive "P")
10505   (gnus-set-global-variables)
10506   (prog1
10507       (let ((articles (gnus-summary-work-articles n)))
10508         (setq gnus-newsgroup-processable nil)
10509         (gnus-summary-limit articles))
10510     (gnus-summary-position-point)))
10511
10512 (defun gnus-summary-pop-limit (&optional total)
10513   "Restore the previous limit.
10514 If given a prefix, remove all limits."
10515   (interactive "P")
10516   (gnus-set-global-variables)
10517   (when total 
10518     (setq gnus-newsgroup-limits
10519           (list (mapcar (lambda (h) (mail-header-number h))
10520                         gnus-newsgroup-headers))))
10521   (unless gnus-newsgroup-limits
10522     (error "No limit to pop"))
10523   (prog1
10524       (gnus-summary-limit nil 'pop)
10525     (gnus-summary-position-point)))
10526
10527 (defun gnus-summary-limit-to-subject (subject &optional header)
10528   "Limit the summary buffer to articles that have subjects that match a regexp."
10529   (interactive "sRegexp: ")
10530   (unless header
10531     (setq header "subject"))
10532   (when (not (equal "" subject))
10533     (prog1
10534         (let ((articles (gnus-summary-find-matching
10535                          (or header "subject") subject 'all)))
10536           (or articles (error "Found no matches for \"%s\"" subject))
10537           (gnus-summary-limit articles))
10538       (gnus-summary-position-point))))
10539
10540 (defun gnus-summary-limit-to-author (from)
10541   "Limit the summary buffer to articles that have authors that match a regexp."
10542   (interactive "sRegexp: ")
10543   (gnus-summary-limit-to-subject from "from"))
10544
10545 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10546 (make-obsolete
10547  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10548
10549 (defun gnus-summary-limit-to-unread (&optional all)
10550   "Limit the summary buffer to articles that are not marked as read.
10551 If ALL is non-nil, limit strictly to unread articles."
10552   (interactive "P")
10553   (if all
10554       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10555     (gnus-summary-limit-to-marks
10556      ;; Concat all the marks that say that an article is read and have
10557      ;; those removed.
10558      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10559            gnus-killed-mark gnus-kill-file-mark
10560            gnus-low-score-mark gnus-expirable-mark
10561            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10562      'reverse)))
10563
10564 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10565 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10566
10567 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10568   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10569 If REVERSE, limit the summary buffer to articles that are not marked
10570 with MARKS.  MARKS can either be a string of marks or a list of marks.
10571 Returns how many articles were removed."
10572   (interactive "sMarks: ")
10573   (gnus-set-global-variables)
10574   (prog1
10575       (let ((data gnus-newsgroup-data)
10576             (marks (if (listp marks) marks
10577                      (append marks nil))) ; Transform to list.
10578             articles)
10579         (while data
10580           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10581                  (memq (gnus-data-mark (car data)) marks))
10582                (setq articles (cons (gnus-data-number (car data)) articles)))
10583           (setq data (cdr data)))
10584         (gnus-summary-limit articles))
10585     (gnus-summary-position-point)))
10586
10587 (defun gnus-summary-limit-to-score (&optional score)
10588   "Limit to articles with score at or above SCORE."
10589   (interactive "P")
10590   (gnus-set-global-variables)
10591   (setq score (if score
10592                   (prefix-numeric-value score)
10593                 (or gnus-summary-default-score 0)))
10594   (let ((data gnus-newsgroup-data)
10595         articles)
10596     (while data
10597       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10598                 score)
10599         (push (gnus-data-number (car data)) articles))
10600       (setq data (cdr data)))
10601     (prog1
10602         (gnus-summary-limit articles)
10603       (gnus-summary-position-point))))
10604
10605 (defun gnus-summary-limit-include-dormant ()
10606   "Display all the hidden articles that are marked as dormant."
10607   (interactive)
10608   (gnus-set-global-variables)
10609   (or gnus-newsgroup-dormant
10610       (error "There are no dormant articles in this group"))
10611   (prog1
10612       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10613     (gnus-summary-position-point)))
10614
10615 (defun gnus-summary-limit-exclude-dormant ()
10616   "Hide all dormant articles."
10617   (interactive)
10618   (gnus-set-global-variables)
10619   (prog1
10620       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10621     (gnus-summary-position-point)))
10622
10623 (defun gnus-summary-limit-exclude-childless-dormant ()
10624   "Hide all dormant articles that have no children."
10625   (interactive)
10626   (gnus-set-global-variables)
10627   (let ((data (gnus-data-list t))
10628         articles d children)
10629     ;; Find all articles that are either not dormant or have
10630     ;; children.
10631     (while (setq d (pop data))
10632       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10633                 (and (setq children 
10634                            (gnus-article-children (gnus-data-number d)))
10635                      (let (found)
10636                        (while children
10637                          (when (memq (car children) articles)
10638                            (setq children nil
10639                                  found t))
10640                          (pop children))
10641                        found)))
10642         (push (gnus-data-number d) articles)))
10643     ;; Do the limiting.
10644     (prog1
10645         (gnus-summary-limit articles)
10646       (gnus-summary-position-point))))
10647
10648 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10649   "Mark all unread excluded articles as read.
10650 If ALL, mark even excluded ticked and dormants as read."
10651   (interactive "P")
10652   (let ((articles (gnus-sorted-complement
10653                    (sort
10654                     (mapcar (lambda (h) (mail-header-number h))
10655                             gnus-newsgroup-headers)
10656                     '<)
10657                    (sort gnus-newsgroup-limit '<)))
10658         article)
10659     (setq gnus-newsgroup-unreads nil)
10660     (if all
10661         (setq gnus-newsgroup-dormant nil
10662               gnus-newsgroup-marked nil
10663               gnus-newsgroup-reads
10664               (nconc
10665                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10666                gnus-newsgroup-reads))
10667       (while (setq article (pop articles))
10668         (unless (or (memq article gnus-newsgroup-dormant)
10669                     (memq article gnus-newsgroup-marked))
10670           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10671
10672 (defun gnus-summary-limit (articles &optional pop)
10673   (if pop
10674       ;; We pop the previous limit off the stack and use that.
10675       (setq articles (car gnus-newsgroup-limits)
10676             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10677     ;; We use the new limit, so we push the old limit on the stack.
10678     (setq gnus-newsgroup-limits
10679           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10680   ;; Set the limit.
10681   (setq gnus-newsgroup-limit articles)
10682   (let ((total (length gnus-newsgroup-data))
10683         (data (gnus-data-find-list (gnus-summary-article-number)))
10684         found)
10685     ;; This will do all the work of generating the new summary buffer
10686     ;; according to the new limit.
10687     (gnus-summary-prepare)
10688     ;; Hide any threads, possibly.
10689     (and gnus-show-threads
10690          gnus-thread-hide-subtree
10691          (gnus-summary-hide-all-threads))
10692     ;; Try to return to the article you were at, or one in the
10693     ;; neighborhood.
10694     (if data
10695         ;; We try to find some article after the current one.
10696         (while data
10697           (and (gnus-summary-goto-subject
10698                 (gnus-data-number (car data)) nil t)
10699                (setq data nil
10700                      found t))
10701           (setq data (cdr data))))
10702     (or found
10703         ;; If there is no data, that means that we were after the last
10704         ;; article.  The same goes when we can't find any articles
10705         ;; after the current one.
10706         (progn
10707           (goto-char (point-max))
10708           (gnus-summary-find-prev)))
10709     ;; We return how many articles were removed from the summary
10710     ;; buffer as a result of the new limit.
10711     (- total (length gnus-newsgroup-data))))
10712
10713 (defsubst gnus-cut-thread (thread)
10714   "Go forwards in the thread until we find an article that we want to display."
10715   (when (eq gnus-fetch-old-headers 'some)
10716     ;; Deal with old-fetched headers.
10717     (while (and thread
10718                 (memq (mail-header-number (car thread)) 
10719                       gnus-newsgroup-ancient)
10720                 (<= (length (cdr thread)) 1))
10721       (setq thread (cadr thread))))
10722   ;; Deal with sparse threads.
10723   (when (or (eq gnus-build-sparse-threads 'some)
10724             (eq gnus-build-sparse-threads 'more))
10725     (while (and thread
10726                 (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10727                 (= (length (cdr thread)) 1))
10728       (setq thread (cadr thread))))
10729   thread)
10730
10731 (defun gnus-cut-threads (threads)
10732   "Cut off all uninteresting articles from the beginning of threads."
10733   (when (or (eq gnus-fetch-old-headers 'some)
10734             (eq gnus-build-sparse-threads 'some)
10735             (eq gnus-build-sparse-threads 'more))
10736     (let ((th threads))
10737       (while th
10738         (setcar th (gnus-cut-thread (car th)))
10739         (setq th (cdr th)))))
10740   ;; Remove nixed out threads.
10741   (delq nil threads))
10742
10743 (defun gnus-summary-initial-limit (&optional show-if-empty)
10744   "Figure out what the initial limit is supposed to be on group entry.
10745 This entails weeding out unwanted dormants, low-scored articles,
10746 fetch-old-headers verbiage, and so on."
10747   ;; Most groups have nothing to remove.
10748   (if (or gnus-inhibit-limiting
10749           (and (null gnus-newsgroup-dormant)
10750                (not (eq gnus-fetch-old-headers 'some))
10751                (null gnus-summary-expunge-below)
10752                (not (eq gnus-build-sparse-threads 'some))
10753                (not (eq gnus-build-sparse-threads 'more))
10754                (null gnus-thread-expunge-below)
10755                (not gnus-use-nocem)))
10756       () ; Do nothing.
10757     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10758     (setq gnus-newsgroup-limit nil)
10759     (mapatoms
10760      (lambda (node)
10761        (unless (car (symbol-value node))
10762          ;; These threads have no parents -- they are roots.
10763          (let ((nodes (cdr (symbol-value node)))
10764                thread)
10765            (while nodes
10766              (if (and gnus-thread-expunge-below
10767                       (< (gnus-thread-total-score (car nodes))
10768                          gnus-thread-expunge-below))
10769                  (gnus-expunge-thread (pop nodes))
10770                (setq thread (pop nodes))
10771                (gnus-summary-limit-children thread))))))
10772      gnus-newsgroup-dependencies)
10773     ;; If this limitation resulted in an empty group, we might
10774     ;; pop the previous limit and use it instead.
10775     (when (and (not gnus-newsgroup-limit)
10776                show-if-empty)
10777       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10778     gnus-newsgroup-limit))
10779
10780 (defun gnus-summary-limit-children (thread)
10781   "Return 1 if this subthread is visible and 0 if it is not."
10782   ;; First we get the number of visible children to this thread.  This
10783   ;; is done by recursing down the thread using this function, so this
10784   ;; will really go down to a leaf article first, before slowly
10785   ;; working its way up towards the root.
10786   (when thread
10787     (let ((children
10788            (if (cdr thread)
10789                (apply '+ (mapcar 'gnus-summary-limit-children
10790                                  (cdr thread)))
10791              0))
10792           (number (mail-header-number (car thread)))
10793           score)
10794       (if (or
10795            ;; If this article is dormant and has absolutely no visible
10796            ;; children, then this article isn't visible.
10797            (and (memq number gnus-newsgroup-dormant)
10798                 (= children 0))
10799            ;; If this is a "fetch-old-headered" and there is only one
10800            ;; visible child (or less), then we don't want this article.
10801            (and (eq gnus-fetch-old-headers 'some)
10802                 (memq number gnus-newsgroup-ancient)
10803                 (zerop children))
10804            ;; If this is a sparsely inserted article with no children,
10805            ;; we don't want it.
10806            (and (eq gnus-build-sparse-threads 'some)
10807                 (memq number gnus-newsgroup-sparse)
10808                 (zerop children))
10809            ;; If we use expunging, and this article is really
10810            ;; low-scored, then we don't want this article.
10811            (when (and gnus-summary-expunge-below
10812                       (< (setq score
10813                                (or (cdr (assq number gnus-newsgroup-scored))
10814                                    gnus-summary-default-score))
10815                          gnus-summary-expunge-below))
10816              ;; We increase the expunge-tally here, but that has
10817              ;; nothing to do with the limits, really.
10818              (incf gnus-newsgroup-expunged-tally)
10819              ;; We also mark as read here, if that's wanted.
10820              (when (and gnus-summary-mark-below
10821                         (< score gnus-summary-mark-below))
10822                (setq gnus-newsgroup-unreads
10823                      (delq number gnus-newsgroup-unreads))
10824                (if gnus-newsgroup-auto-expire
10825                    (push number gnus-newsgroup-expirable)
10826                  (push (cons number gnus-low-score-mark)
10827                        gnus-newsgroup-reads)))
10828              t)
10829            (and gnus-use-nocem
10830                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
10831           ;; Nope, invisible article.
10832           0
10833         ;; Ok, this article is to be visible, so we add it to the limit
10834         ;; and return 1.
10835         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
10836         1))))
10837
10838 (defun gnus-expunge-thread (thread)
10839   "Mark all articles in THREAD as read."
10840   (let* ((number (mail-header-number (car thread))))
10841     (incf gnus-newsgroup-expunged-tally)
10842     ;; We also mark as read here, if that's wanted.
10843     (setq gnus-newsgroup-unreads
10844           (delq number gnus-newsgroup-unreads))
10845     (if gnus-newsgroup-auto-expire
10846         (push number gnus-newsgroup-expirable)
10847       (push (cons number gnus-low-score-mark)
10848             gnus-newsgroup-reads)))
10849   ;; Go recursively through all subthreads.
10850   (mapcar 'gnus-expunge-thread (cdr thread)))
10851
10852 ;; Summary article oriented commands
10853
10854 (defun gnus-summary-refer-parent-article (n)
10855   "Refer parent article N times.
10856 The difference between N and the number of articles fetched is returned."
10857   (interactive "p")
10858   (gnus-set-global-variables)
10859   (while
10860       (and
10861        (> n 0)
10862        (let* ((header (gnus-summary-article-header))
10863               (ref
10864                ;; If we try to find the parent of the currently
10865                ;; displayed article, then we take a look at the actual
10866                ;; References header, since this is slightly more
10867                ;; reliable than the References field we got from the
10868                ;; server.
10869                (if (and (eq (mail-header-number header)
10870                             (cdr gnus-article-current))
10871                         (equal gnus-newsgroup-name
10872                                (car gnus-article-current)))
10873                    (save-excursion
10874                      (set-buffer gnus-original-article-buffer)
10875                      (nnheader-narrow-to-headers)
10876                      (prog1
10877                          (mail-fetch-field "references")
10878                        (widen)))
10879                  ;; It's not the current article, so we take a bet on
10880                  ;; the value we got from the server.
10881                  (mail-header-references header))))
10882          (if (setq ref (or ref (mail-header-references header)))
10883              (or (gnus-summary-refer-article (gnus-parent-id ref))
10884                  (gnus-message 1 "Couldn't find parent"))
10885            (gnus-message 1 "No references in article %d"
10886                          (gnus-summary-article-number))
10887            nil)))
10888     (setq n (1- n)))
10889   (gnus-summary-position-point)
10890   n)
10891
10892 (defun gnus-summary-refer-references ()
10893   "Fetch all articles mentioned in the References header.
10894 Return how many articles were fetched."
10895   (interactive)
10896   (gnus-set-global-variables)
10897   (let ((ref (mail-header-references (gnus-summary-article-header)))
10898         (current (gnus-summary-article-number))
10899         (n 0))
10900     ;; For each Message-ID in the References header...
10901     (while (string-match "<[^>]*>" ref)
10902       (incf n)
10903       ;; ... fetch that article.
10904       (gnus-summary-refer-article
10905        (prog1 (match-string 0 ref)
10906          (setq ref (substring ref (match-end 0))))))
10907     (gnus-summary-goto-subject current)
10908     (gnus-summary-position-point)
10909     n))
10910
10911 (defun gnus-summary-refer-article (message-id)
10912   "Fetch an article specified by MESSAGE-ID."
10913   (interactive "sMessage-ID: ")
10914   (when (and (stringp message-id)
10915              (not (zerop (length message-id))))
10916     ;; Construct the correct Message-ID if necessary.
10917     ;; Suggested by tale@pawl.rpi.edu.
10918     (unless (string-match "^<" message-id)
10919       (setq message-id (concat "<" message-id)))
10920     (unless (string-match ">$" message-id)
10921       (setq message-id (concat message-id ">")))
10922     (let ((header (car (gnus-gethash message-id
10923                                      gnus-newsgroup-dependencies))))
10924       (if header
10925           ;; The article is present in the buffer, to we just go to it.
10926           (gnus-summary-goto-article (mail-header-number header) nil t)
10927         ;; We fetch the article
10928         (let ((gnus-override-method 
10929                (and (gnus-news-group-p gnus-newsgroup-name)
10930                     gnus-refer-article-method))
10931               number)
10932           ;; Start the special refer-article method, if necessary.
10933           (when (and gnus-refer-article-method
10934                      (gnus-news-group-p gnus-newsgroup-name))
10935             (gnus-check-server gnus-refer-article-method))
10936           ;; Fetch the header, and display the article.
10937           (if (setq number (gnus-summary-insert-subject message-id))
10938               (gnus-summary-select-article nil nil nil number)
10939             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
10940
10941 (defun gnus-summary-enter-digest-group (&optional force)
10942   "Enter a digest group based on the current article."
10943   (interactive "P")
10944   (gnus-set-global-variables)
10945   (gnus-summary-select-article)
10946   (let ((name (format "%s-%d"
10947                       (gnus-group-prefixed-name
10948                        gnus-newsgroup-name (list 'nndoc ""))
10949                       gnus-current-article))
10950         (ogroup gnus-newsgroup-name)
10951         (case-fold-search t)
10952         (buf (current-buffer))
10953         dig)
10954     (save-excursion
10955       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
10956       (insert-buffer-substring gnus-original-article-buffer)
10957       (narrow-to-region
10958        (goto-char (point-min))
10959        (or (search-forward "\n\n" nil t) (point)))
10960       (goto-char (point-min))
10961       (delete-matching-lines "^\\(Path\\):\\|^From ")
10962       (widen))
10963     (unwind-protect
10964         (if (gnus-group-read-ephemeral-group
10965              name `(nndoc ,name (nndoc-address
10966                                  ,(get-buffer dig))
10967                           (nndoc-article-type ,(if force 'digest 'guess))) t)
10968             ;; Make all postings to this group go to the parent group.
10969             (nconc (gnus-info-params (gnus-get-info name))
10970                    (list (cons 'to-group ogroup)))
10971           ;; Couldn't select this doc group.
10972           (switch-to-buffer buf)
10973           (gnus-set-global-variables)
10974           (gnus-configure-windows 'summary)
10975           (gnus-message 3 "Article couldn't be entered?"))
10976       (kill-buffer dig))))
10977
10978 (defun gnus-summary-isearch-article (&optional regexp-p)
10979   "Do incremental search forward on the current article.
10980 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
10981   (interactive "P")
10982   (gnus-set-global-variables)
10983   (gnus-summary-select-article)
10984   (gnus-configure-windows 'article)
10985   (gnus-eval-in-buffer-window
10986    gnus-article-buffer
10987    (goto-char (point-min))
10988    (isearch-forward regexp-p)))
10989
10990 (defun gnus-summary-search-article-forward (regexp &optional backward)
10991   "Search for an article containing REGEXP forward.
10992 If BACKWARD, search backward instead."
10993   (interactive
10994    (list (read-string
10995           (format "Search article %s (regexp%s): "
10996                   (if current-prefix-arg "backward" "forward")
10997                   (if gnus-last-search-regexp
10998                       (concat ", default " gnus-last-search-regexp)
10999                     "")))
11000          current-prefix-arg))
11001   (gnus-set-global-variables)
11002   (if (string-equal regexp "")
11003       (setq regexp (or gnus-last-search-regexp ""))
11004     (setq gnus-last-search-regexp regexp))
11005   (if (gnus-summary-search-article regexp backward)
11006       (gnus-article-set-window-start
11007        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
11008     (error "Search failed: \"%s\"" regexp)))
11009
11010 (defun gnus-summary-search-article-backward (regexp)
11011   "Search for an article containing REGEXP backward."
11012   (interactive
11013    (list (read-string
11014           (format "Search article backward (regexp%s): "
11015                   (if gnus-last-search-regexp
11016                       (concat ", default " gnus-last-search-regexp)
11017                     "")))))
11018   (gnus-summary-search-article-forward regexp 'backward))
11019
11020 (defun gnus-summary-search-article (regexp &optional backward)
11021   "Search for an article containing REGEXP.
11022 Optional argument BACKWARD means do search for backward.
11023 gnus-select-article-hook is not called during the search."
11024   (let ((gnus-select-article-hook nil)  ;Disable hook.
11025         (gnus-article-display-hook nil)
11026         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
11027         (re-search
11028          (if backward
11029              (function re-search-backward) (function re-search-forward)))
11030         (found nil)
11031         (last nil))
11032     ;; Hidden thread subtrees must be searched for ,too.
11033     (gnus-summary-show-all-threads)
11034     ;; First of all, search current article.
11035     ;; We don't want to read article again from NNTP server nor reset
11036     ;; current point.
11037     (gnus-summary-select-article)
11038     (gnus-message 9 "Searching article: %d..." gnus-current-article)
11039     (setq last gnus-current-article)
11040     (gnus-eval-in-buffer-window
11041      gnus-article-buffer
11042      (save-restriction
11043        (widen)
11044        ;; Begin search from current point.
11045        (setq found (funcall re-search regexp nil t))))
11046     ;; Then search next articles.
11047     (while (and (not found)
11048                 (gnus-summary-display-article
11049                  (if backward (gnus-summary-find-prev)
11050                    (gnus-summary-find-next))))
11051       (gnus-message 9 "Searching article: %d..." gnus-current-article)
11052       (gnus-eval-in-buffer-window
11053        gnus-article-buffer
11054        (save-restriction
11055          (widen)
11056          (goto-char (if backward (point-max) (point-min)))
11057          (setq found (funcall re-search regexp nil t)))))
11058     (message "")
11059     ;; Adjust article pointer.
11060     (or (eq last gnus-current-article)
11061         (setq gnus-last-article last))
11062     ;; Return T if found such article.
11063     found))
11064
11065 (defun gnus-summary-find-matching (header regexp &optional backward unread
11066                                           not-case-fold)
11067   "Return a list of all articles that match REGEXP on HEADER.
11068 The search stars on the current article and goes forwards unless
11069 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
11070 If UNREAD is non-nil, only unread articles will
11071 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
11072 in the comparisons."
11073   (let ((data (if (eq backward 'all) gnus-newsgroup-data
11074                 (gnus-data-find-list
11075                  (gnus-summary-article-number) (gnus-data-list backward))))
11076         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
11077         (case-fold-search (not not-case-fold))
11078         articles d)
11079     (or (fboundp (intern (concat "mail-header-" header)))
11080         (error "%s is not a valid header" header))
11081     (while data
11082       (setq d (car data))
11083       (and (or (not unread)             ; We want all articles...
11084                (gnus-data-unread-p d))  ; Or just unreads.
11085            (vectorp (gnus-data-header d)) ; It's not a pseudo.
11086            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
11087            (setq articles (cons (gnus-data-number d) articles))) ; Success!
11088       (setq data (cdr data)))
11089     (nreverse articles)))
11090
11091 (defun gnus-summary-execute-command (header regexp command &optional backward)
11092   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
11093 If HEADER is an empty string (or nil), the match is done on the entire
11094 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
11095   (interactive
11096    (list (let ((completion-ignore-case t))
11097            (completing-read
11098             "Header name: "
11099             (mapcar (lambda (string) (list string))
11100                     '("Number" "Subject" "From" "Lines" "Date"
11101                       "Message-ID" "Xref" "References" "Body"))
11102             nil 'require-match))
11103          (read-string "Regexp: ")
11104          (read-key-sequence "Command: ")
11105          current-prefix-arg))
11106   (when (equal header "Body")
11107     (setq header ""))
11108   (gnus-set-global-variables)
11109   ;; Hidden thread subtrees must be searched as well.
11110   (gnus-summary-show-all-threads)
11111   ;; We don't want to change current point nor window configuration.
11112   (save-excursion
11113     (save-window-excursion
11114       (gnus-message 6 "Executing %s..." (key-description command))
11115       ;; We'd like to execute COMMAND interactively so as to give arguments.
11116       (gnus-execute header regexp
11117                     `(lambda () (call-interactively ',(key-binding command)))
11118                     backward)
11119       (gnus-message 6 "Executing %s...done" (key-description command)))))
11120
11121 (defun gnus-summary-beginning-of-article ()
11122   "Scroll the article back to the beginning."
11123   (interactive)
11124   (gnus-set-global-variables)
11125   (gnus-summary-select-article)
11126   (gnus-configure-windows 'article)
11127   (gnus-eval-in-buffer-window
11128    gnus-article-buffer
11129    (widen)
11130    (goto-char (point-min))
11131    (and gnus-break-pages (gnus-narrow-to-page))))
11132
11133 (defun gnus-summary-end-of-article ()
11134   "Scroll to the end of the article."
11135   (interactive)
11136   (gnus-set-global-variables)
11137   (gnus-summary-select-article)
11138   (gnus-configure-windows 'article)
11139   (gnus-eval-in-buffer-window
11140    gnus-article-buffer
11141    (widen)
11142    (goto-char (point-max))
11143    (recenter -3)
11144    (and gnus-break-pages (gnus-narrow-to-page))))
11145
11146 (defun gnus-summary-show-article (&optional arg)
11147   "Force re-fetching of the current article.
11148 If ARG (the prefix) is non-nil, show the raw article without any
11149 article massaging functions being run."
11150   (interactive "P")
11151   (gnus-set-global-variables)
11152   (if (not arg)
11153       ;; Select the article the normal way.
11154       (gnus-summary-select-article nil 'force)
11155     ;; Bind the article treatment functions to nil.
11156     (let ((gnus-have-all-headers t)
11157           gnus-article-display-hook
11158           gnus-article-prepare-hook
11159           gnus-break-pages
11160           gnus-visual)
11161       (gnus-summary-select-article nil 'force)))
11162   (gnus-summary-goto-subject gnus-current-article)
11163 ;  (gnus-configure-windows 'article)
11164   (gnus-summary-position-point))
11165
11166 (defun gnus-summary-verbose-headers (&optional arg)
11167   "Toggle permanent full header display.
11168 If ARG is a positive number, turn header display on.
11169 If ARG is a negative number, turn header display off."
11170   (interactive "P")
11171   (gnus-set-global-variables)
11172   (gnus-summary-toggle-header arg)
11173   (setq gnus-show-all-headers
11174         (cond ((or (not (numberp arg))
11175                    (zerop arg))
11176                (not gnus-show-all-headers))
11177               ((natnump arg)
11178                t))))
11179
11180 (defun gnus-summary-toggle-header (&optional arg)
11181   "Show the headers if they are hidden, or hide them if they are shown.
11182 If ARG is a positive number, show the entire header.
11183 If ARG is a negative number, hide the unwanted header lines."
11184   (interactive "P")
11185   (gnus-set-global-variables)
11186   (save-excursion
11187     (set-buffer gnus-article-buffer)
11188     (let* ((buffer-read-only nil)
11189            (inhibit-point-motion-hooks t)
11190            (hidden (text-property-any
11191                     (goto-char (point-min)) (search-forward "\n\n")
11192                     'invisible t))
11193            e)
11194       (goto-char (point-min))
11195       (when (search-forward "\n\n" nil t)
11196         (delete-region (point-min) (1- (point))))
11197       (goto-char (point-min))
11198       (save-excursion
11199         (set-buffer gnus-original-article-buffer)
11200         (goto-char (point-min))
11201         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11202       (insert-buffer-substring gnus-original-article-buffer 1 e)
11203       (let ((gnus-inhibit-hiding t))
11204         (run-hooks 'gnus-article-display-hook))
11205       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11206           (gnus-article-hide-headers)))))
11207
11208 (defun gnus-summary-show-all-headers ()
11209   "Make all header lines visible."
11210   (interactive)
11211   (gnus-set-global-variables)
11212   (gnus-article-show-all-headers))
11213
11214 (defun gnus-summary-toggle-mime (&optional arg)
11215   "Toggle MIME processing.
11216 If ARG is a positive number, turn MIME processing on."
11217   (interactive "P")
11218   (gnus-set-global-variables)
11219   (setq gnus-show-mime
11220         (if (null arg) (not gnus-show-mime)
11221           (> (prefix-numeric-value arg) 0)))
11222   (gnus-summary-select-article t 'force))
11223
11224 (defun gnus-summary-caesar-message (&optional arg)
11225   "Caesar rotate the current article by 13.
11226 The numerical prefix specifies how manu places to rotate each letter
11227 forward."
11228   (interactive "P")
11229   (gnus-set-global-variables)
11230   (gnus-summary-select-article)
11231   (let ((mail-header-separator ""))
11232     (gnus-eval-in-buffer-window
11233      gnus-article-buffer
11234      (save-restriction
11235        (widen)
11236        (let ((start (window-start)))
11237          (news-caesar-buffer-body arg)
11238          (set-window-start (get-buffer-window (current-buffer)) start))))))
11239
11240 (defun gnus-summary-stop-page-breaking ()
11241   "Stop page breaking in the current article."
11242   (interactive)
11243   (gnus-set-global-variables)
11244   (gnus-summary-select-article)
11245   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
11246
11247 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11248   "Move the current article to a different newsgroup.
11249 If N is a positive number, move the N next articles.
11250 If N is a negative number, move the N previous articles.
11251 If N is nil and any articles have been marked with the process mark,
11252 move those articles instead.
11253 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11254 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11255 re-spool using this method.
11256
11257 For this function to work, both the current newsgroup and the
11258 newsgroup that you want to move to have to support the `request-move'
11259 and `request-accept' functions."
11260   (interactive "P")
11261   (unless action (setq action 'move))
11262   (gnus-set-global-variables)
11263   ;; Check whether the source group supports the required functions.
11264   (cond ((and (eq action 'move)
11265               (not (gnus-check-backend-function
11266                     'request-move-article gnus-newsgroup-name)))
11267          (error "The current group does not support article moving"))
11268         ((and (eq action 'crosspost)
11269               (not (gnus-check-backend-function
11270                     'request-replace-article gnus-newsgroup-name)))
11271          (error "The current group does not support article editing")))
11272   (let ((articles (gnus-summary-work-articles n))
11273         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11274         (names '((move "Move" "Moving")
11275                  (copy "Copy" "Copying")
11276                  (crosspost "Crosspost" "Crossposting")))
11277         (copy-buf (save-excursion
11278                     (nnheader-set-temp-buffer " *copy article*")))
11279         art-group to-method new-xref article to-groups)
11280     (unless (assq action names)
11281       (error "Unknown action %s" action))
11282     ;; Read the newsgroup name.
11283     (when (and (not to-newsgroup)
11284                (not select-method))
11285       (setq to-newsgroup
11286             (gnus-read-move-group-name
11287              (cadr (assq action names))
11288              (symbol-value (intern (format "gnus-current-%s-group" action)))
11289              articles prefix))
11290       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11291     (setq to-method (or select-method 
11292                         (gnus-find-method-for-group to-newsgroup)))
11293     ;; Check the method we are to move this article to...
11294     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11295         (error "%s does not support article copying" (car to-method)))
11296     (or (gnus-check-server to-method)
11297         (error "Can't open server %s" (car to-method)))
11298     (gnus-message 6 "%s to %s: %s..."
11299                   (caddr (assq action names))
11300                   (or (car select-method) to-newsgroup) articles)
11301     (while articles
11302       (setq article (pop articles))
11303       (setq
11304        art-group
11305        (cond
11306         ;; Move the article.
11307         ((eq action 'move)
11308          (gnus-request-move-article
11309           article                       ; Article to move
11310           gnus-newsgroup-name           ; From newsgrouo
11311           (nth 1 (gnus-find-method-for-group
11312                   gnus-newsgroup-name)) ; Server
11313           (list 'gnus-request-accept-article
11314                 to-newsgroup (list 'quote select-method)
11315                 (not articles))         ; Accept form
11316           (not articles)))              ; Only save nov last time
11317         ;; Copy the article.
11318         ((eq action 'copy)
11319          (save-excursion
11320            (set-buffer copy-buf)
11321            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11322            (gnus-request-accept-article
11323             to-newsgroup select-method (not articles))))
11324         ;; Crosspost the article.
11325         ((eq action 'crosspost)
11326          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11327            (setq new-xref (concat gnus-newsgroup-name ":" article))
11328            (if (and xref (not (string= xref "")))
11329                (progn
11330                  (when (string-match "^Xref: " xref)
11331                    (setq xref (substring xref (match-end 0))))
11332                  (setq new-xref (concat xref " " new-xref)))
11333              (setq new-xref (concat (system-name) " " new-xref)))
11334            (save-excursion
11335              (set-buffer copy-buf)
11336              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11337              (nnheader-replace-header "xref" new-xref)
11338              (gnus-request-accept-article
11339               to-newsgroup select-method (not articles)))))))
11340       (if (not art-group)
11341           (gnus-message 1 "Couldn't %s article %s"
11342                         (cadr (assq action names)) article)
11343         (let* ((entry
11344                 (or
11345                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11346                  (gnus-gethash
11347                   (gnus-group-prefixed-name
11348                    (car art-group)
11349                    (or select-method 
11350                        (gnus-find-method-for-group to-newsgroup)))
11351                   gnus-newsrc-hashtb)))
11352                (info (nth 2 entry))
11353                (to-group (gnus-info-group info)))
11354           ;; Update the group that has been moved to.
11355           (when (and info
11356                      (memq action '(move copy)))
11357             (unless (member to-group to-groups)
11358               (push to-group to-groups))
11359
11360             (unless (memq article gnus-newsgroup-unreads)
11361               (gnus-info-set-read
11362                info (gnus-add-to-range (gnus-info-read info)
11363                                        (list (cdr art-group)))))
11364
11365             ;; Copy any marks over to the new group.
11366             (let ((marks gnus-article-mark-lists)
11367                   (to-article (cdr art-group)))
11368
11369               ;; See whether the article is to be put in the cache.
11370               (when gnus-use-cache
11371                 (gnus-cache-possibly-enter-article
11372                  to-group to-article
11373                  (let ((header (copy-sequence
11374                                 (gnus-summary-article-header article))))
11375                    (mail-header-set-number header to-article)
11376                    header)
11377                  (memq article gnus-newsgroup-marked)
11378                  (memq article gnus-newsgroup-dormant)
11379                  (memq article gnus-newsgroup-unreads)))
11380
11381               (while marks
11382                 (when (memq article (symbol-value
11383                                      (intern (format "gnus-newsgroup-%s"
11384                                                      (caar marks)))))
11385                   ;; If the other group is the same as this group,
11386                   ;; then we have to add the mark to the list.
11387                   (when (equal to-group gnus-newsgroup-name)
11388                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
11389                          (cons to-article
11390                                (symbol-value
11391                                 (intern (format "gnus-newsgroup-%s"
11392                                                 (caar marks)))))))
11393                   ;; Copy mark to other group.
11394                   (gnus-add-marked-articles
11395                    to-group (cdar marks) (list to-article) info))
11396                 (setq marks (cdr marks)))))
11397
11398           ;; Update the Xref header in this article to point to
11399           ;; the new crossposted article we have just created.
11400           (when (eq action 'crosspost)
11401             (save-excursion
11402               (set-buffer copy-buf)
11403               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11404               (nnheader-replace-header
11405                "xref" (concat new-xref " " (gnus-group-prefixed-name
11406                                             (car art-group) to-method)
11407                               ":" (cdr art-group)))
11408               (gnus-request-replace-article
11409                article gnus-newsgroup-name (current-buffer)))))
11410
11411         (gnus-summary-goto-subject article)
11412         (when (eq action 'move)
11413           (gnus-summary-mark-article article gnus-canceled-mark)))
11414       (gnus-summary-remove-process-mark article))
11415     ;; Re-activate all groups that have been moved to.
11416     (while to-groups
11417       (gnus-activate-group (pop to-groups)))
11418     
11419     (gnus-kill-buffer copy-buf)
11420     (gnus-summary-position-point)
11421     (gnus-set-mode-line 'summary)))
11422
11423 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11424   "Move the current article to a different newsgroup.
11425 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11426 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11427 re-spool using this method."
11428   (interactive "P")
11429   (gnus-summary-move-article n nil select-method 'copy))
11430
11431 (defun gnus-summary-crosspost-article (&optional n)
11432   "Crosspost the current article to some other group."
11433   (interactive "P")
11434   (gnus-summary-move-article n nil nil 'crosspost))
11435
11436 (defvar gnus-summary-respool-default-method nil
11437   "Default method for respooling an article.  
11438 If nil, use to the current newsgroup method.")
11439
11440 (defun gnus-summary-respool-article (&optional n method)
11441   "Respool the current article.
11442 The article will be squeezed through the mail spooling process again,
11443 which means that it will be put in some mail newsgroup or other
11444 depending on `nnmail-split-methods'.
11445 If N is a positive number, respool the N next articles.
11446 If N is a negative number, respool the N previous articles.
11447 If N is nil and any articles have been marked with the process mark,
11448 respool those articles instead.
11449
11450 Respooling can be done both from mail groups and \"real\" newsgroups.
11451 In the former case, the articles in question will be moved from the
11452 current group into whatever groups they are destined to.  In the
11453 latter case, they will be copied into the relevant groups."
11454   (interactive 
11455    (list current-prefix-arg
11456          (let* ((methods (gnus-methods-using 'respool))
11457                 (methname
11458                  (symbol-name (or gnus-summary-respool-default-method
11459                                   (car (gnus-find-method-for-group
11460                                         gnus-newsgroup-name)))))
11461                 (method
11462                  (gnus-completing-read 
11463                   methname "What backend do you want to use when? "
11464                   methods nil t nil 'gnus-method-history))
11465                 ms)
11466            (cond
11467             ((zerop (length (setq ms (gnus-servers-using-backend method))))
11468              (list (intern method) ""))
11469             ((= 1 (length ms))
11470              (car ms))
11471             (t
11472              (cdr (completing-read 
11473                    "Server name: "
11474                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
11475   (gnus-set-global-variables)
11476   (unless method
11477     (error "No method given for respooling"))
11478   (if (assoc (symbol-name
11479               (car (gnus-find-method-for-group gnus-newsgroup-name)))
11480              (gnus-methods-using 'respool))
11481       (gnus-summary-move-article n nil method)
11482     (gnus-summary-copy-article n nil method)))
11483
11484 (defun gnus-summary-import-article (file)
11485   "Import a random file into a mail newsgroup."
11486   (interactive "fImport file: ")
11487   (gnus-set-global-variables)
11488   (let ((group gnus-newsgroup-name)
11489         (now (current-time))
11490         atts lines)
11491     (or (gnus-check-backend-function 'request-accept-article group)
11492         (error "%s does not support article importing" group))
11493     (or (file-readable-p file)
11494         (not (file-regular-p file))
11495         (error "Can't read %s" file))
11496     (save-excursion
11497       (set-buffer (get-buffer-create " *import file*"))
11498       (buffer-disable-undo (current-buffer))
11499       (erase-buffer)
11500       (insert-file-contents file)
11501       (goto-char (point-min))
11502       (unless (nnheader-article-p)
11503         ;; This doesn't look like an article, so we fudge some headers.
11504         (setq atts (file-attributes file)
11505               lines (count-lines (point-min) (point-max)))
11506         (insert "From: " (read-string "From: ") "\n"
11507                 "Subject: " (read-string "Subject: ") "\n"
11508                 "Date: " (timezone-make-date-arpa-standard
11509                           (current-time-string (nth 5 atts))
11510                           (current-time-zone now)
11511                           (current-time-zone now)) "\n"
11512                 "Message-ID: " (gnus-inews-message-id) "\n"
11513                 "Lines: " (int-to-string lines) "\n"
11514                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11515       (gnus-request-accept-article group nil t)
11516       (kill-buffer (current-buffer)))))
11517
11518 (defun gnus-summary-expire-articles (&optional now)
11519   "Expire all articles that are marked as expirable in the current group."
11520   (interactive)
11521   (gnus-set-global-variables)
11522   (when (gnus-check-backend-function
11523          'request-expire-articles gnus-newsgroup-name)
11524     ;; This backend supports expiry.
11525     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11526            (expirable (if total
11527                           (gnus-list-of-read-articles gnus-newsgroup-name)
11528                         (setq gnus-newsgroup-expirable
11529                               (sort gnus-newsgroup-expirable '<))))
11530            (expiry-wait (if now 'immediate
11531                           (gnus-group-get-parameter
11532                            gnus-newsgroup-name 'expiry-wait)))
11533            es)
11534       (when expirable
11535         ;; There are expirable articles in this group, so we run them
11536         ;; through the expiry process.
11537         (gnus-message 6 "Expiring articles...")
11538         ;; The list of articles that weren't expired is returned.
11539         (if expiry-wait
11540             (let ((nnmail-expiry-wait-function nil)
11541                   (nnmail-expiry-wait expiry-wait))
11542               (setq es (gnus-request-expire-articles
11543                         expirable gnus-newsgroup-name)))
11544           (setq es (gnus-request-expire-articles
11545                     expirable gnus-newsgroup-name)))
11546         (or total (setq gnus-newsgroup-expirable es))
11547         ;; We go through the old list of expirable, and mark all
11548         ;; really expired articles as nonexistent.
11549         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11550           (let ((gnus-use-cache nil))
11551             (while expirable
11552               (unless (memq (car expirable) es)
11553                 (when (gnus-data-find (car expirable))
11554                   (gnus-summary-mark-article
11555                    (car expirable) gnus-canceled-mark)))
11556               (setq expirable (cdr expirable)))))
11557         (gnus-message 6 "Expiring articles...done")))))
11558
11559 (defun gnus-summary-expire-articles-now ()
11560   "Expunge all expirable articles in the current group.
11561 This means that *all* articles that are marked as expirable will be
11562 deleted forever, right now."
11563   (interactive)
11564   (gnus-set-global-variables)
11565   (or gnus-expert-user
11566       (gnus-y-or-n-p
11567        "Are you really, really, really sure you want to delete all these messages? ")
11568       (error "Phew!"))
11569   (gnus-summary-expire-articles t))
11570
11571 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11572 (defun gnus-summary-delete-article (&optional n)
11573   "Delete the N next (mail) articles.
11574 This command actually deletes articles.  This is not a marking
11575 command.  The article will disappear forever from your life, never to
11576 return.
11577 If N is negative, delete backwards.
11578 If N is nil and articles have been marked with the process mark,
11579 delete these instead."
11580   (interactive "P")
11581   (gnus-set-global-variables)
11582   (or (gnus-check-backend-function 'request-expire-articles
11583                                    gnus-newsgroup-name)
11584       (error "The current newsgroup does not support article deletion."))
11585   ;; Compute the list of articles to delete.
11586   (let ((articles (gnus-summary-work-articles n))
11587         not-deleted)
11588     (if (and gnus-novice-user
11589              (not (gnus-y-or-n-p
11590                    (format "Do you really want to delete %s forever? "
11591                            (if (> (length articles) 1) 
11592                                (format "these %s articles" (length articles))
11593                              "this article")))))
11594         ()
11595       ;; Delete the articles.
11596       (setq not-deleted (gnus-request-expire-articles
11597                          articles gnus-newsgroup-name 'force))
11598       (while articles
11599         (gnus-summary-remove-process-mark (car articles))
11600         ;; The backend might not have been able to delete the article
11601         ;; after all.
11602         (or (memq (car articles) not-deleted)
11603             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11604         (setq articles (cdr articles))))
11605     (gnus-summary-position-point)
11606     (gnus-set-mode-line 'summary)
11607     not-deleted))
11608
11609 (defun gnus-summary-edit-article (&optional force)
11610   "Enter into a buffer and edit the current article.
11611 This will have permanent effect only in mail groups.
11612 If FORCE is non-nil, allow editing of articles even in read-only
11613 groups."
11614   (interactive "P")
11615   (save-excursion
11616     (set-buffer gnus-summary-buffer)
11617     (gnus-set-global-variables)
11618     (when (and (not force)
11619                (gnus-group-read-only-p))
11620       (error "The current newsgroup does not support article editing."))
11621     (gnus-summary-select-article t nil t)
11622     (gnus-configure-windows 'article)
11623     (select-window (get-buffer-window gnus-article-buffer))
11624     (gnus-message 6 "C-c C-c to end edits")
11625     (setq buffer-read-only nil)
11626     (text-mode)
11627     (use-local-map (copy-keymap (current-local-map)))
11628     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11629     (buffer-enable-undo)
11630     (widen)
11631     (goto-char (point-min))
11632     (search-forward "\n\n" nil t)))
11633
11634 (defun gnus-summary-edit-article-done ()
11635   "Make edits to the current article permanent."
11636   (interactive)
11637   (if (gnus-group-read-only-p)
11638       (progn
11639         (gnus-summary-edit-article-postpone)
11640         (gnus-error
11641          1 "The current newsgroup does not support article editing."))
11642     (let ((buf (format "%s" (buffer-string))))
11643       (erase-buffer)
11644       (insert buf)
11645       (if (not (gnus-request-replace-article
11646                 (cdr gnus-article-current) (car gnus-article-current)
11647                 (current-buffer)))
11648           (error "Couldn't replace article.")
11649         (gnus-article-mode)
11650         (use-local-map gnus-article-mode-map)
11651         (setq buffer-read-only t)
11652         (buffer-disable-undo (current-buffer))
11653         (gnus-configure-windows 'summary)
11654         (gnus-summary-update-article (cdr gnus-article-current))
11655         (when gnus-use-cache
11656           (gnus-cache-update-article 
11657            (cdr gnus-article-current) (car gnus-article-current)))
11658         (when gnus-keep-backlog
11659           (gnus-backlog-remove-article 
11660            (car gnus-article-current) (cdr gnus-article-current))))
11661       (save-excursion
11662         (when (get-buffer gnus-original-article-buffer)
11663           (set-buffer gnus-original-article-buffer)
11664           (setq gnus-original-article nil)))
11665       (setq gnus-article-current nil
11666             gnus-current-article nil)
11667       (run-hooks 'gnus-article-display-hook)
11668       (and (gnus-visual-p 'summary-highlight 'highlight)
11669            (run-hooks 'gnus-visual-mark-article-hook)))))
11670
11671 (defun gnus-summary-edit-article-postpone ()
11672   "Postpone changes to the current article."
11673   (interactive)
11674   (gnus-article-mode)
11675   (use-local-map gnus-article-mode-map)
11676   (setq buffer-read-only t)
11677   (buffer-disable-undo (current-buffer))
11678   (gnus-configure-windows 'summary)
11679   (and (gnus-visual-p 'summary-highlight 'highlight)
11680        (run-hooks 'gnus-visual-mark-article-hook)))
11681
11682 (defun gnus-summary-respool-query ()
11683   "Query where the respool algorithm would put this article."
11684   (interactive)
11685   (gnus-set-global-variables)
11686   (gnus-summary-select-article)
11687   (save-excursion
11688     (set-buffer gnus-article-buffer)
11689     (save-restriction
11690       (goto-char (point-min))
11691       (search-forward "\n\n")
11692       (narrow-to-region (point-min) (point))
11693       (pp-eval-expression
11694        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11695
11696 ;; Summary score commands.
11697
11698 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
11699
11700 (defun gnus-summary-raise-score (n)
11701   "Raise the score of the current article by N."
11702   (interactive "p")
11703   (gnus-set-global-variables)
11704   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
11705
11706 (defun gnus-summary-set-score (n)
11707   "Set the score of the current article to N."
11708   (interactive "p")
11709   (gnus-set-global-variables)
11710   (save-excursion
11711     (gnus-summary-show-thread)
11712     (let ((buffer-read-only nil))
11713       ;; Set score.
11714       (gnus-summary-update-mark
11715        (if (= n (or gnus-summary-default-score 0)) ? 
11716          (if (< n (or gnus-summary-default-score 0))
11717              gnus-score-below-mark gnus-score-over-mark)) 'score))
11718     (let* ((article (gnus-summary-article-number))
11719            (score (assq article gnus-newsgroup-scored)))
11720       (if score (setcdr score n)
11721         (setq gnus-newsgroup-scored
11722               (cons (cons article n) gnus-newsgroup-scored))))
11723     (gnus-summary-update-line)))
11724
11725 (defun gnus-summary-current-score ()
11726   "Return the score of the current article."
11727   (interactive)
11728   (gnus-set-global-variables)
11729   (gnus-message 1 "%s" (gnus-summary-article-score)))
11730
11731 ;; Summary marking commands.
11732
11733 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11734   "Mark articles which has the same subject as read, and then select the next.
11735 If UNMARK is positive, remove any kind of mark.
11736 If UNMARK is negative, tick articles."
11737   (interactive "P")
11738   (gnus-set-global-variables)
11739   (if unmark
11740       (setq unmark (prefix-numeric-value unmark)))
11741   (let ((count
11742          (gnus-summary-mark-same-subject
11743           (gnus-summary-article-subject) unmark)))
11744     ;; Select next unread article.  If auto-select-same mode, should
11745     ;; select the first unread article.
11746     (gnus-summary-next-article t (and gnus-auto-select-same
11747                                       (gnus-summary-article-subject)))
11748     (gnus-message 7 "%d article%s marked as %s"
11749                   count (if (= count 1) " is" "s are")
11750                   (if unmark "unread" "read"))))
11751
11752 (defun gnus-summary-kill-same-subject (&optional unmark)
11753   "Mark articles which has the same subject as read.
11754 If UNMARK is positive, remove any kind of mark.
11755 If UNMARK is negative, tick articles."
11756   (interactive "P")
11757   (gnus-set-global-variables)
11758   (if unmark
11759       (setq unmark (prefix-numeric-value unmark)))
11760   (let ((count
11761          (gnus-summary-mark-same-subject
11762           (gnus-summary-article-subject) unmark)))
11763     ;; If marked as read, go to next unread subject.
11764     (if (null unmark)
11765         ;; Go to next unread subject.
11766         (gnus-summary-next-subject 1 t))
11767     (gnus-message 7 "%d articles are marked as %s"
11768                   count (if unmark "unread" "read"))))
11769
11770 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11771   "Mark articles with same SUBJECT as read, and return marked number.
11772 If optional argument UNMARK is positive, remove any kinds of marks.
11773 If optional argument UNMARK is negative, mark articles as unread instead."
11774   (let ((count 1))
11775     (save-excursion
11776       (cond
11777        ((null unmark)                   ; Mark as read.
11778         (while (and
11779                 (progn
11780                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11781                   (gnus-summary-show-thread) t)
11782                 (gnus-summary-find-subject subject))
11783           (setq count (1+ count))))
11784        ((> unmark 0)                    ; Tick.
11785         (while (and
11786                 (progn
11787                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11788                   (gnus-summary-show-thread) t)
11789                 (gnus-summary-find-subject subject))
11790           (setq count (1+ count))))
11791        (t                               ; Mark as unread.
11792         (while (and
11793                 (progn
11794                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11795                   (gnus-summary-show-thread) t)
11796                 (gnus-summary-find-subject subject))
11797           (setq count (1+ count)))))
11798       (gnus-set-mode-line 'summary)
11799       ;; Return the number of marked articles.
11800       count)))
11801
11802 (defun gnus-summary-mark-as-processable (n &optional unmark)
11803   "Set the process mark on the next N articles.
11804 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11805 the process mark instead.  The difference between N and the actual
11806 number of articles marked is returned."
11807   (interactive "p")
11808   (gnus-set-global-variables)
11809   (let ((backward (< n 0))
11810         (n (abs n)))
11811     (while (and
11812             (> n 0)
11813             (if unmark
11814                 (gnus-summary-remove-process-mark
11815                  (gnus-summary-article-number))
11816               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11817             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11818       (setq n (1- n)))
11819     (if (/= 0 n) (gnus-message 7 "No more articles"))
11820     (gnus-summary-recenter)
11821     (gnus-summary-position-point)
11822     n))
11823
11824 (defun gnus-summary-unmark-as-processable (n)
11825   "Remove the process mark from the next N articles.
11826 If N is negative, mark backward instead.  The difference between N and
11827 the actual number of articles marked is returned."
11828   (interactive "p")
11829   (gnus-set-global-variables)
11830   (gnus-summary-mark-as-processable n t))
11831
11832 (defun gnus-summary-unmark-all-processable ()
11833   "Remove the process mark from all articles."
11834   (interactive)
11835   (gnus-set-global-variables)
11836   (save-excursion
11837     (while gnus-newsgroup-processable
11838       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11839   (gnus-summary-position-point))
11840
11841 (defun gnus-summary-mark-as-expirable (n)
11842   "Mark N articles forward as expirable.
11843 If N is negative, mark backward instead.  The difference between N and
11844 the actual number of articles marked is returned."
11845   (interactive "p")
11846   (gnus-set-global-variables)
11847   (gnus-summary-mark-forward n gnus-expirable-mark))
11848
11849 (defun gnus-summary-mark-article-as-replied (article)
11850   "Mark ARTICLE replied and update the summary line."
11851   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11852   (let ((buffer-read-only nil))
11853     (when (gnus-summary-goto-subject article)
11854       (gnus-summary-update-secondary-mark article))))
11855
11856 (defun gnus-summary-set-bookmark (article)
11857   "Set a bookmark in current article."
11858   (interactive (list (gnus-summary-article-number)))
11859   (gnus-set-global-variables)
11860   (if (or (not (get-buffer gnus-article-buffer))
11861           (not gnus-current-article)
11862           (not gnus-article-current)
11863           (not (equal gnus-newsgroup-name (car gnus-article-current))))
11864       (error "No current article selected"))
11865   ;; Remove old bookmark, if one exists.
11866   (let ((old (assq article gnus-newsgroup-bookmarks)))
11867     (if old (setq gnus-newsgroup-bookmarks
11868                   (delq old gnus-newsgroup-bookmarks))))
11869   ;; Set the new bookmark, which is on the form
11870   ;; (article-number . line-number-in-body).
11871   (setq gnus-newsgroup-bookmarks
11872         (cons
11873          (cons article
11874                (save-excursion
11875                  (set-buffer gnus-article-buffer)
11876                  (count-lines
11877                   (min (point)
11878                        (save-excursion
11879                          (goto-char (point-min))
11880                          (search-forward "\n\n" nil t)
11881                          (point)))
11882                   (point))))
11883          gnus-newsgroup-bookmarks))
11884   (gnus-message 6 "A bookmark has been added to the current article."))
11885
11886 (defun gnus-summary-remove-bookmark (article)
11887   "Remove the bookmark from the current article."
11888   (interactive (list (gnus-summary-article-number)))
11889   (gnus-set-global-variables)
11890   ;; Remove old bookmark, if one exists.
11891   (let ((old (assq article gnus-newsgroup-bookmarks)))
11892     (if old
11893         (progn
11894           (setq gnus-newsgroup-bookmarks
11895                 (delq old gnus-newsgroup-bookmarks))
11896           (gnus-message 6 "Removed bookmark."))
11897       (gnus-message 6 "No bookmark in current article."))))
11898
11899 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11900 (defun gnus-summary-mark-as-dormant (n)
11901   "Mark N articles forward as dormant.
11902 If N is negative, mark backward instead.  The difference between N and
11903 the actual number of articles marked is returned."
11904   (interactive "p")
11905   (gnus-set-global-variables)
11906   (gnus-summary-mark-forward n gnus-dormant-mark))
11907
11908 (defun gnus-summary-set-process-mark (article)
11909   "Set the process mark on ARTICLE and update the summary line."
11910   (setq gnus-newsgroup-processable
11911         (cons article
11912               (delq article gnus-newsgroup-processable)))
11913   (when (gnus-summary-goto-subject article)
11914     (gnus-summary-show-thread)
11915     (gnus-summary-update-secondary-mark article)))
11916
11917 (defun gnus-summary-remove-process-mark (article)
11918   "Remove the process mark from ARTICLE and update the summary line."
11919   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
11920   (when (gnus-summary-goto-subject article)
11921     (gnus-summary-show-thread)
11922     (gnus-summary-update-secondary-mark article)))
11923
11924 (defun gnus-summary-set-saved-mark (article)
11925   "Set the process mark on ARTICLE and update the summary line."
11926   (push article gnus-newsgroup-saved)
11927   (when (gnus-summary-goto-subject article)
11928     (gnus-summary-update-secondary-mark article)))
11929
11930 (defun gnus-summary-mark-forward (n &optional mark no-expire)
11931   "Mark N articles as read forwards.
11932 If N is negative, mark backwards instead.
11933 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
11934 marked as unread.
11935 The difference between N and the actual number of articles marked is
11936 returned."
11937   (interactive "p")
11938   (gnus-set-global-variables)
11939   (let ((backward (< n 0))
11940         (gnus-summary-goto-unread
11941          (and gnus-summary-goto-unread
11942               (not (eq gnus-summary-goto-unread 'never))
11943               (not (memq mark (list gnus-unread-mark
11944                                     gnus-ticked-mark gnus-dormant-mark)))))
11945         (n (abs n))
11946         (mark (or mark gnus-del-mark)))
11947     (while (and (> n 0)
11948                 (gnus-summary-mark-article nil mark no-expire)
11949                 (zerop (gnus-summary-next-subject
11950                         (if backward -1 1)
11951                         (and gnus-summary-goto-unread
11952                              (not (eq gnus-summary-goto-unread 'never)))
11953                         t)))
11954       (setq n (1- n)))
11955     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11956     (gnus-summary-recenter)
11957     (gnus-summary-position-point)
11958     (gnus-set-mode-line 'summary)
11959     n))
11960
11961 (defun gnus-summary-mark-article-as-read (mark)
11962   "Mark the current article quickly as read with MARK."
11963   (let ((article (gnus-summary-article-number)))
11964     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11965     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11966     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11967     (setq gnus-newsgroup-reads
11968           (cons (cons article mark) gnus-newsgroup-reads))
11969     ;; Possibly remove from cache, if that is used.
11970     (and gnus-use-cache (gnus-cache-enter-remove-article article))
11971     ;; Allow the backend to change the mark.
11972     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
11973     ;; Check for auto-expiry.
11974     (when (and gnus-newsgroup-auto-expire
11975                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11976                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11977                    (= mark gnus-ancient-mark)
11978                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
11979       (setq mark gnus-expirable-mark)
11980       (push article gnus-newsgroup-expirable))
11981     ;; Set the mark in the buffer.
11982     (gnus-summary-update-mark mark 'unread)
11983     t))
11984
11985 (defun gnus-summary-mark-article-as-unread (mark)
11986   "Mark the current article quickly as unread with MARK."
11987   (let ((article (gnus-summary-article-number)))
11988     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11989     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11990     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11991     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
11992     (cond ((= mark gnus-ticked-mark)
11993            (push article gnus-newsgroup-marked))
11994           ((= mark gnus-dormant-mark)
11995            (push article gnus-newsgroup-dormant))
11996           (t
11997            (push article gnus-newsgroup-unreads)))
11998     (setq gnus-newsgroup-reads
11999           (delq (assq article gnus-newsgroup-reads)
12000                 gnus-newsgroup-reads))
12001
12002     ;; See whether the article is to be put in the cache.
12003     (and gnus-use-cache
12004          (vectorp (gnus-summary-article-header article))
12005          (save-excursion
12006            (gnus-cache-possibly-enter-article
12007             gnus-newsgroup-name article
12008             (gnus-summary-article-header article)
12009             (= mark gnus-ticked-mark)
12010             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12011
12012     ;; Fix the mark.
12013     (gnus-summary-update-mark mark 'unread)
12014     t))
12015
12016 (defun gnus-summary-mark-article (&optional article mark no-expire)
12017   "Mark ARTICLE with MARK.  MARK can be any character.
12018 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
12019 `??' (dormant) and `?E' (expirable).
12020 If MARK is nil, then the default character `?D' is used.
12021 If ARTICLE is nil, then the article on the current line will be
12022 marked."
12023   ;; The mark might be a string.
12024   (and (stringp mark)
12025        (setq mark (aref mark 0)))
12026   ;; If no mark is given, then we check auto-expiring.
12027   (and (not no-expire)
12028        gnus-newsgroup-auto-expire
12029        (or (not mark)
12030            (and (numberp mark)
12031                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12032                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12033                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
12034        (setq mark gnus-expirable-mark))
12035   (let* ((mark (or mark gnus-del-mark))
12036          (article (or article (gnus-summary-article-number))))
12037     (or article (error "No article on current line"))
12038     (if (or (= mark gnus-unread-mark)
12039             (= mark gnus-ticked-mark)
12040             (= mark gnus-dormant-mark))
12041         (gnus-mark-article-as-unread article mark)
12042       (gnus-mark-article-as-read article mark))
12043
12044     ;; See whether the article is to be put in the cache.
12045     (and gnus-use-cache
12046          (not (= mark gnus-canceled-mark))
12047          (vectorp (gnus-summary-article-header article))
12048          (save-excursion
12049            (gnus-cache-possibly-enter-article
12050             gnus-newsgroup-name article
12051             (gnus-summary-article-header article)
12052             (= mark gnus-ticked-mark)
12053             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12054
12055     (if (gnus-summary-goto-subject article nil t)
12056         (let ((buffer-read-only nil))
12057           (gnus-summary-show-thread)
12058           ;; Fix the mark.
12059           (gnus-summary-update-mark mark 'unread)
12060           t))))
12061
12062 (defun gnus-summary-update-secondary-mark (article)
12063   "Update the secondary (read, process, cache) mark."
12064   (gnus-summary-update-mark
12065    (cond ((memq article gnus-newsgroup-processable)
12066           gnus-process-mark)
12067          ((memq article gnus-newsgroup-cached)
12068           gnus-cached-mark)
12069          ((memq article gnus-newsgroup-replied)
12070           gnus-replied-mark)
12071          ((memq article gnus-newsgroup-saved)
12072           gnus-saved-mark)
12073          (t gnus-unread-mark))
12074    'replied)
12075   (when (gnus-visual-p 'summary-highlight 'highlight)
12076     (run-hooks 'gnus-summary-update-hook))
12077   t)
12078
12079 (defun gnus-summary-update-mark (mark type)
12080   (beginning-of-line)
12081   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
12082         (buffer-read-only nil))
12083     (when (and forward
12084                (<= (+ forward (point)) (point-max)))
12085       ;; Go to the right position on the line.
12086       (goto-char (+ forward (point)))
12087       ;; Replace the old mark with the new mark.
12088       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
12089       ;; Optionally update the marks by some user rule.
12090       (when (eq type 'unread)
12091         (gnus-data-set-mark
12092          (gnus-data-find (gnus-summary-article-number)) mark)
12093         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
12094
12095 (defun gnus-mark-article-as-read (article &optional mark)
12096   "Enter ARTICLE in the pertinent lists and remove it from others."
12097   ;; Make the article expirable.
12098   (let ((mark (or mark gnus-del-mark)))
12099     (if (= mark gnus-expirable-mark)
12100         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
12101       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
12102     ;; Remove from unread and marked lists.
12103     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12104     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12105     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12106     (push (cons article mark) gnus-newsgroup-reads)
12107     ;; Possibly remove from cache, if that is used.
12108     (when gnus-use-cache
12109       (gnus-cache-enter-remove-article article))))
12110
12111 (defun gnus-mark-article-as-unread (article &optional mark)
12112   "Enter ARTICLE in the pertinent lists and remove it from others."
12113   (let ((mark (or mark gnus-ticked-mark)))
12114     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12115     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12116     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12117     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12118     (cond ((= mark gnus-ticked-mark)
12119            (push article gnus-newsgroup-marked))
12120           ((= mark gnus-dormant-mark)
12121            (push article gnus-newsgroup-dormant))
12122           (t
12123            (push article gnus-newsgroup-unreads)))
12124     (setq gnus-newsgroup-reads
12125           (delq (assq article gnus-newsgroup-reads)
12126                 gnus-newsgroup-reads))))
12127
12128 (defalias 'gnus-summary-mark-as-unread-forward
12129   'gnus-summary-tick-article-forward)
12130 (make-obsolete 'gnus-summary-mark-as-unread-forward
12131                'gnus-summary-tick-article-forward)
12132 (defun gnus-summary-tick-article-forward (n)
12133   "Tick N articles forwards.
12134 If N is negative, tick backwards instead.
12135 The difference between N and the number of articles ticked is returned."
12136   (interactive "p")
12137   (gnus-summary-mark-forward n gnus-ticked-mark))
12138
12139 (defalias 'gnus-summary-mark-as-unread-backward
12140   'gnus-summary-tick-article-backward)
12141 (make-obsolete 'gnus-summary-mark-as-unread-backward
12142                'gnus-summary-tick-article-backward)
12143 (defun gnus-summary-tick-article-backward (n)
12144   "Tick N articles backwards.
12145 The difference between N and the number of articles ticked is returned."
12146   (interactive "p")
12147   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
12148
12149 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12150 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12151 (defun gnus-summary-tick-article (&optional article clear-mark)
12152   "Mark current article as unread.
12153 Optional 1st argument ARTICLE specifies article number to be marked as unread.
12154 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
12155   (interactive)
12156   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
12157                                        gnus-ticked-mark)))
12158
12159 (defun gnus-summary-mark-as-read-forward (n)
12160   "Mark N articles as read forwards.
12161 If N is negative, mark backwards instead.
12162 The difference between N and the actual number of articles marked is
12163 returned."
12164   (interactive "p")
12165   (gnus-summary-mark-forward n gnus-del-mark t))
12166
12167 (defun gnus-summary-mark-as-read-backward (n)
12168   "Mark the N articles as read backwards.
12169 The difference between N and the actual number of articles marked is
12170 returned."
12171   (interactive "p")
12172   (gnus-summary-mark-forward (- n) gnus-del-mark t))
12173
12174 (defun gnus-summary-mark-as-read (&optional article mark)
12175   "Mark current article as read.
12176 ARTICLE specifies the article to be marked as read.
12177 MARK specifies a string to be inserted at the beginning of the line."
12178   (gnus-summary-mark-article article mark))
12179
12180 (defun gnus-summary-clear-mark-forward (n)
12181   "Clear marks from N articles forward.
12182 If N is negative, clear backward instead.
12183 The difference between N and the number of marks cleared is returned."
12184   (interactive "p")
12185   (gnus-summary-mark-forward n gnus-unread-mark))
12186
12187 (defun gnus-summary-clear-mark-backward (n)
12188   "Clear marks from N articles backward.
12189 The difference between N and the number of marks cleared is returned."
12190   (interactive "p")
12191   (gnus-summary-mark-forward (- n) gnus-unread-mark))
12192
12193 (defun gnus-summary-mark-unread-as-read ()
12194   "Intended to be used by `gnus-summary-mark-article-hook'."
12195   (when (memq gnus-current-article gnus-newsgroup-unreads)
12196     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
12197
12198 (defun gnus-summary-mark-read-and-unread-as-read ()
12199   "Intended to be used by `gnus-summary-mark-article-hook'."
12200   (let ((mark (gnus-summary-article-mark)))
12201     (when (or (gnus-unread-mark-p mark)
12202               (gnus-read-mark-p mark))
12203       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
12204
12205 (defun gnus-summary-mark-region-as-read (point mark all)
12206   "Mark all unread articles between point and mark as read.
12207 If given a prefix, mark all articles between point and mark as read,
12208 even ticked and dormant ones."
12209   (interactive "r\nP")
12210   (save-excursion
12211     (let (article)
12212       (goto-char point)
12213       (beginning-of-line)
12214       (while (and
12215               (< (point) mark)
12216               (progn
12217                 (when (or all
12218                           (memq (setq article (gnus-summary-article-number))
12219                                 gnus-newsgroup-unreads))
12220                   (gnus-summary-mark-article article gnus-del-mark))
12221                 t)
12222               (gnus-summary-find-next))))))
12223
12224 (defun gnus-summary-mark-below (score mark)
12225   "Mark articles with score less than SCORE with MARK."
12226   (interactive "P\ncMark: ")
12227   (gnus-set-global-variables)
12228   (setq score (if score
12229                   (prefix-numeric-value score)
12230                 (or gnus-summary-default-score 0)))
12231   (save-excursion
12232     (set-buffer gnus-summary-buffer)
12233     (goto-char (point-min))
12234     (while 
12235         (progn
12236           (and (< (gnus-summary-article-score) score)
12237                (gnus-summary-mark-article nil mark))
12238           (gnus-summary-find-next)))))
12239
12240 (defun gnus-summary-kill-below (&optional score)
12241   "Mark articles with score below SCORE as read."
12242   (interactive "P")
12243   (gnus-set-global-variables)
12244   (gnus-summary-mark-below score gnus-killed-mark))
12245
12246 (defun gnus-summary-clear-above (&optional score)
12247   "Clear all marks from articles with score above SCORE."
12248   (interactive "P")
12249   (gnus-set-global-variables)
12250   (gnus-summary-mark-above score gnus-unread-mark))
12251
12252 (defun gnus-summary-tick-above (&optional score)
12253   "Tick all articles with score above SCORE."
12254   (interactive "P")
12255   (gnus-set-global-variables)
12256   (gnus-summary-mark-above score gnus-ticked-mark))
12257
12258 (defun gnus-summary-mark-above (score mark)
12259   "Mark articles with score over SCORE with MARK."
12260   (interactive "P\ncMark: ")
12261   (gnus-set-global-variables)
12262   (setq score (if score
12263                   (prefix-numeric-value score)
12264                 (or gnus-summary-default-score 0)))
12265   (save-excursion
12266     (set-buffer gnus-summary-buffer)
12267     (goto-char (point-min))
12268     (while (and (progn
12269                   (if (> (gnus-summary-article-score) score)
12270                       (gnus-summary-mark-article nil mark))
12271                   t)
12272                 (gnus-summary-find-next)))))
12273
12274 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12275 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12276 (defun gnus-summary-limit-include-expunged ()
12277   "Display all the hidden articles that were expunged for low scores."
12278   (interactive)
12279   (gnus-set-global-variables)
12280   (let ((buffer-read-only nil))
12281     (let ((scored gnus-newsgroup-scored)
12282           headers h)
12283       (while scored
12284         (or (gnus-summary-goto-subject (caar scored))
12285             (and (setq h (gnus-summary-article-header (caar scored)))
12286                  (< (cdar scored) gnus-summary-expunge-below)
12287                  (setq headers (cons h headers))))
12288         (setq scored (cdr scored)))
12289       (or headers (error "No expunged articles hidden."))
12290       (goto-char (point-min))
12291       (gnus-summary-prepare-unthreaded (nreverse headers)))
12292     (goto-char (point-min))
12293     (gnus-summary-position-point)))
12294
12295 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12296   "Mark all articles not marked as unread in this newsgroup as read.
12297 If prefix argument ALL is non-nil, all articles are marked as read.
12298 If QUIETLY is non-nil, no questions will be asked.
12299 If TO-HERE is non-nil, it should be a point in the buffer.  All
12300 articles before this point will be marked as read.
12301 The number of articles marked as read is returned."
12302   (interactive "P")
12303   (gnus-set-global-variables)
12304   (prog1
12305       (if (or quietly
12306               (not gnus-interactive-catchup) ;Without confirmation?
12307               gnus-expert-user
12308               (gnus-y-or-n-p
12309                (if all
12310                    "Mark absolutely all articles as read? "
12311                  "Mark all unread articles as read? ")))
12312           (if (and not-mark
12313                    (not gnus-newsgroup-adaptive)
12314                    (not gnus-newsgroup-auto-expire))
12315               (progn
12316                 (when all
12317                   (setq gnus-newsgroup-marked nil
12318                         gnus-newsgroup-dormant nil))
12319                 (setq gnus-newsgroup-unreads nil))
12320             ;; We actually mark all articles as canceled, which we
12321             ;; have to do when using auto-expiry or adaptive scoring.
12322             (gnus-summary-show-all-threads)
12323             (if (gnus-summary-first-subject (not all))
12324                 (while (and
12325                         (if to-here (< (point) to-here) t)
12326                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12327                         (gnus-summary-find-next (not all)))))
12328             (unless to-here
12329               (setq gnus-newsgroup-unreads nil))
12330             (gnus-set-mode-line 'summary)))
12331     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12332       (if (and (not to-here) (eq 'nnvirtual (car method)))
12333           (nnvirtual-catchup-group
12334            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12335     (gnus-summary-position-point)))
12336
12337 (defun gnus-summary-catchup-to-here (&optional all)
12338   "Mark all unticked articles before the current one as read.
12339 If ALL is non-nil, also mark ticked and dormant articles as read."
12340   (interactive "P")
12341   (gnus-set-global-variables)
12342   (save-excursion
12343     (let ((beg (point)))
12344       ;; We check that there are unread articles.
12345       (when (or all (gnus-summary-find-prev))
12346         (gnus-summary-catchup all t beg))))
12347   (gnus-summary-position-point))
12348
12349 (defun gnus-summary-catchup-all (&optional quietly)
12350   "Mark all articles in this newsgroup as read."
12351   (interactive "P")
12352   (gnus-set-global-variables)
12353   (gnus-summary-catchup t quietly))
12354
12355 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12356   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12357 If prefix argument ALL is non-nil, all articles are marked as read."
12358   (interactive "P")
12359   (gnus-set-global-variables)
12360   (gnus-summary-catchup all quietly nil 'fast)
12361   ;; Select next newsgroup or exit.
12362   (if (eq gnus-auto-select-next 'quietly)
12363       (gnus-summary-next-group nil)
12364     (gnus-summary-exit)))
12365
12366 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12367   "Mark all articles in this newsgroup as read, and then exit."
12368   (interactive "P")
12369   (gnus-set-global-variables)
12370   (gnus-summary-catchup-and-exit t quietly))
12371
12372 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12373 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12374   "Mark all articles in this group as read and select the next group.
12375 If given a prefix, mark all articles, unread as well as ticked, as
12376 read."
12377   (interactive "P")
12378   (gnus-set-global-variables)
12379   (save-excursion
12380     (gnus-summary-catchup all))
12381   (gnus-summary-next-article t nil nil t))
12382
12383 ;; Thread-based commands.
12384
12385 (defun gnus-summary-articles-in-thread (&optional article)
12386   "Return a list of all articles in the current thread.
12387 If ARTICLE is non-nil, return all articles in the thread that starts
12388 with that article."
12389   (let* ((article (or article (gnus-summary-article-number)))
12390          (data (gnus-data-find-list article))
12391          (top-level (gnus-data-level (car data)))
12392          (top-subject
12393           (cond ((null gnus-thread-operation-ignore-subject)
12394                  (gnus-simplify-subject-re
12395                   (mail-header-subject (gnus-data-header (car data)))))
12396                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12397                  (gnus-simplify-subject-fuzzy
12398                   (mail-header-subject (gnus-data-header (car data)))))
12399                 (t nil)))
12400          (end-point (save-excursion
12401                       (if (gnus-summary-go-to-next-thread) 
12402                           (point) (point-max))))
12403          articles)
12404     (while (and data
12405                 (< (gnus-data-pos (car data)) end-point))
12406       (when (or (not top-subject)
12407                 (string= top-subject
12408                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12409                              (gnus-simplify-subject-fuzzy
12410                               (mail-header-subject
12411                                (gnus-data-header (car data))))
12412                            (gnus-simplify-subject-re
12413                             (mail-header-subject
12414                              (gnus-data-header (car data)))))))
12415         (push (gnus-data-number (car data)) articles))
12416       (unless (and (setq data (cdr data))
12417                    (> (gnus-data-level (car data)) top-level))
12418         (setq data nil)))
12419     ;; Return the list of articles.
12420     (nreverse articles)))
12421
12422 (defun gnus-summary-rethread-current ()
12423   "Rethread the thread the current article is part of."
12424   (interactive)
12425   (gnus-set-global-variables)
12426   (let* ((gnus-show-threads t)
12427          (article (gnus-summary-article-number))
12428          (id (mail-header-id (gnus-summary-article-header)))
12429          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12430     (unless id
12431       (error "No article on the current line"))
12432     (gnus-rebuild-thread id)
12433     (gnus-summary-goto-subject article)))
12434
12435 (defun gnus-summary-reparent-thread ()
12436   "Make current article child of the marked (or previous) article.
12437
12438 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12439 is non-nil or the Subject: of both articles are the same."
12440   (interactive)
12441   (or (not (gnus-group-read-only-p))
12442       (error "The current newsgroup does not support article editing."))
12443   (or (<= (length gnus-newsgroup-processable) 1)
12444       (error "No more than one article may be marked."))
12445   (save-window-excursion
12446     (let ((gnus-article-buffer " *reparent*")
12447           (current-article (gnus-summary-article-number))
12448           ; first grab the marked article, otherwise one line up.
12449           (parent-article (if (not (null gnus-newsgroup-processable))
12450                               (car gnus-newsgroup-processable)
12451                             (save-excursion
12452                               (if (eq (forward-line -1) 0)
12453                                   (gnus-summary-article-number)
12454                                 (error "Beginning of summary buffer."))))))
12455       (or (not (eq current-article parent-article))
12456           (error "An article may not be self-referential."))
12457       (let ((message-id (mail-header-id 
12458                          (gnus-summary-article-header parent-article))))
12459         (or (and message-id (not (equal message-id "")))
12460             (error "No message-id in desired parent."))
12461         (gnus-summary-select-article t t nil current-article)
12462         (set-buffer gnus-article-buffer)
12463         (setq buffer-read-only nil)
12464         (let ((buf (format "%s" (buffer-string))))
12465           (erase-buffer)
12466           (insert buf))
12467         (goto-char (point-min))
12468         (if (search-forward-regexp "^References: " nil t)
12469             (insert message-id " " )
12470           (insert "References: " message-id "\n"))
12471         (or (gnus-request-replace-article current-article
12472                                           (car gnus-article-current)
12473                                           gnus-article-buffer)
12474             (error "Couldn't replace article."))
12475         (set-buffer gnus-summary-buffer)
12476         (gnus-summary-unmark-all-processable)
12477         (gnus-summary-rethread-current)
12478         (gnus-message 3 "Article %d is now the child of article %d."
12479                       current-article parent-article)))))
12480
12481 (defun gnus-summary-toggle-threads (&optional arg)
12482   "Toggle showing conversation threads.
12483 If ARG is positive number, turn showing conversation threads on."
12484   (interactive "P")
12485   (gnus-set-global-variables)
12486   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12487     (setq gnus-show-threads
12488           (if (null arg) (not gnus-show-threads)
12489             (> (prefix-numeric-value arg) 0)))
12490     (gnus-summary-prepare)
12491     (gnus-summary-goto-subject current)
12492     (gnus-summary-position-point)))
12493
12494 (defun gnus-summary-show-all-threads ()
12495   "Show all threads."
12496   (interactive)
12497   (gnus-set-global-variables)
12498   (save-excursion
12499     (let ((buffer-read-only nil))
12500       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12501   (gnus-summary-position-point))
12502
12503 (defun gnus-summary-show-thread ()
12504   "Show thread subtrees.
12505 Returns nil if no thread was there to be shown."
12506   (interactive)
12507   (gnus-set-global-variables)
12508   (let ((buffer-read-only nil)
12509         (orig (point))
12510         ;; first goto end then to beg, to have point at beg after let
12511         (end (progn (end-of-line) (point)))
12512         (beg (progn (beginning-of-line) (point))))
12513     (prog1
12514         ;; Any hidden lines here?
12515         (search-forward "\r" end t)
12516       (subst-char-in-region beg end ?\^M ?\n t)
12517       (goto-char orig)
12518       (gnus-summary-position-point))))
12519
12520 (defun gnus-summary-hide-all-threads ()
12521   "Hide all thread subtrees."
12522   (interactive)
12523   (gnus-set-global-variables)
12524   (save-excursion
12525     (goto-char (point-min))
12526     (gnus-summary-hide-thread)
12527     (while (zerop (gnus-summary-next-thread 1 t))
12528       (gnus-summary-hide-thread)))
12529   (gnus-summary-position-point))
12530
12531 (defun gnus-summary-hide-thread ()
12532   "Hide thread subtrees.
12533 Returns nil if no threads were there to be hidden."
12534   (interactive)
12535   (gnus-set-global-variables)
12536   (let ((buffer-read-only nil)
12537         (start (point))
12538         (article (gnus-summary-article-number)))
12539     (goto-char start)
12540     ;; Go forward until either the buffer ends or the subthread
12541     ;; ends.
12542     (when (and (not (eobp))
12543                (or (zerop (gnus-summary-next-thread 1 t))
12544                    (goto-char (point-max))))
12545       (prog1
12546           (if (and (> (point) start)
12547                    (search-backward "\n" start t))
12548               (progn
12549                 (subst-char-in-region start (point) ?\n ?\^M)
12550                 (gnus-summary-goto-subject article))
12551             (goto-char start)
12552             nil)
12553         ;;(gnus-summary-position-point)
12554         ))))
12555
12556 (defun gnus-summary-go-to-next-thread (&optional previous)
12557   "Go to the same level (or less) next thread.
12558 If PREVIOUS is non-nil, go to previous thread instead.
12559 Return the article number moved to, or nil if moving was impossible."
12560   (let ((level (gnus-summary-thread-level))
12561         (way (if previous -1 1))
12562         (beg (point)))
12563     (forward-line way)
12564     (while (and (not (eobp))
12565                 (< level (gnus-summary-thread-level)))
12566       (forward-line way))
12567     (if (eobp)
12568         (progn
12569           (goto-char beg)
12570           nil)
12571       (setq beg (point))
12572       (prog1
12573           (gnus-summary-article-number)
12574         (goto-char beg)))))
12575
12576 (defun gnus-summary-go-to-next-thread-old (&optional previous)
12577   "Go to the same level (or less) next thread.
12578 If PREVIOUS is non-nil, go to previous thread instead.
12579 Return the article number moved to, or nil if moving was impossible."
12580   (if (and (eq gnus-summary-make-false-root 'dummy)
12581            (gnus-summary-article-intangible-p))
12582       (let ((beg (point)))
12583         (while (and (zerop (forward-line 1))
12584                     (not (gnus-summary-article-intangible-p))
12585                     (not (zerop (save-excursion 
12586                                   (gnus-summary-thread-level))))))
12587         (if (eobp)
12588             (progn
12589               (goto-char beg)
12590               nil)
12591           (point)))
12592     (let* ((level (gnus-summary-thread-level))
12593            (article (gnus-summary-article-number))
12594            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12595            oart)
12596       (while data
12597         (if (<= (gnus-data-level (car data)) level)
12598             (setq oart (gnus-data-number (car data))
12599                   data nil)
12600           (setq data (cdr data))))
12601       (and oart
12602            (gnus-summary-goto-subject oart)))))
12603
12604 (defun gnus-summary-next-thread (n &optional silent)
12605   "Go to the same level next N'th thread.
12606 If N is negative, search backward instead.
12607 Returns the difference between N and the number of skips actually
12608 done.
12609
12610 If SILENT, don't output messages."
12611   (interactive "p")
12612   (gnus-set-global-variables)
12613   (let ((backward (< n 0))
12614         (n (abs n))
12615         old dum int)
12616     (while (and (> n 0)
12617                 (gnus-summary-go-to-next-thread backward))
12618       (decf n))
12619     (unless silent 
12620       (gnus-summary-position-point))
12621     (when (and (not silent) (/= 0 n))
12622       (gnus-message 7 "No more threads"))
12623     n))
12624
12625 (defun gnus-summary-prev-thread (n)
12626   "Go to the same level previous N'th thread.
12627 Returns the difference between N and the number of skips actually
12628 done."
12629   (interactive "p")
12630   (gnus-set-global-variables)
12631   (gnus-summary-next-thread (- n)))
12632
12633 (defun gnus-summary-go-down-thread ()
12634   "Go down one level in the current thread."
12635   (let ((children (gnus-summary-article-children)))
12636     (and children
12637          (gnus-summary-goto-subject (car children)))))
12638
12639 (defun gnus-summary-go-up-thread ()
12640   "Go up one level in the current thread."
12641   (let ((parent (gnus-summary-article-parent)))
12642     (and parent
12643          (gnus-summary-goto-subject parent))))
12644
12645 (defun gnus-summary-down-thread (n)
12646   "Go down thread N steps.
12647 If N is negative, go up instead.
12648 Returns the difference between N and how many steps down that were
12649 taken."
12650   (interactive "p")
12651   (gnus-set-global-variables)
12652   (let ((up (< n 0))
12653         (n (abs n)))
12654     (while (and (> n 0)
12655                 (if up (gnus-summary-go-up-thread)
12656                   (gnus-summary-go-down-thread)))
12657       (setq n (1- n)))
12658     (gnus-summary-position-point)
12659     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12660     n))
12661
12662 (defun gnus-summary-up-thread (n)
12663   "Go up thread N steps.
12664 If N is negative, go up instead.
12665 Returns the difference between N and how many steps down that were
12666 taken."
12667   (interactive "p")
12668   (gnus-set-global-variables)
12669   (gnus-summary-down-thread (- n)))
12670
12671 (defun gnus-summary-top-thread ()
12672   "Go to the top of the thread."
12673   (interactive)
12674   (gnus-set-global-variables)
12675   (while (gnus-summary-go-up-thread))
12676   (gnus-summary-article-number))
12677
12678 (defun gnus-summary-kill-thread (&optional unmark)
12679   "Mark articles under current thread as read.
12680 If the prefix argument is positive, remove any kinds of marks.
12681 If the prefix argument is negative, tick articles instead."
12682   (interactive "P")
12683   (gnus-set-global-variables)
12684   (if unmark
12685       (setq unmark (prefix-numeric-value unmark)))
12686   (let ((articles (gnus-summary-articles-in-thread)))
12687     (save-excursion
12688       ;; Expand the thread.
12689       (gnus-summary-show-thread)
12690       ;; Mark all the articles.
12691       (while articles
12692         (gnus-summary-goto-subject (car articles))
12693         (cond ((null unmark)
12694                (gnus-summary-mark-article-as-read gnus-killed-mark))
12695               ((> unmark 0)
12696                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12697               (t
12698                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12699         (setq articles (cdr articles))))
12700     ;; Hide killed subtrees.
12701     (and (null unmark)
12702          gnus-thread-hide-killed
12703          (gnus-summary-hide-thread))
12704     ;; If marked as read, go to next unread subject.
12705     (if (null unmark)
12706         ;; Go to next unread subject.
12707         (gnus-summary-next-subject 1 t)))
12708   (gnus-set-mode-line 'summary))
12709
12710 ;; Summary sorting commands
12711
12712 (defun gnus-summary-sort-by-number (&optional reverse)
12713   "Sort summary buffer by article number.
12714 Argument REVERSE means reverse order."
12715   (interactive "P")
12716   (gnus-summary-sort 'number reverse))
12717
12718 (defun gnus-summary-sort-by-author (&optional reverse)
12719   "Sort summary buffer by author name alphabetically.
12720 If case-fold-search is non-nil, case of letters is ignored.
12721 Argument REVERSE means reverse order."
12722   (interactive "P")
12723   (gnus-summary-sort 'author reverse))
12724
12725 (defun gnus-summary-sort-by-subject (&optional reverse)
12726   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12727 If case-fold-search is non-nil, case of letters is ignored.
12728 Argument REVERSE means reverse order."
12729   (interactive "P")
12730   (gnus-summary-sort 'subject reverse))
12731
12732 (defun gnus-summary-sort-by-date (&optional reverse)
12733   "Sort summary buffer by date.
12734 Argument REVERSE means reverse order."
12735   (interactive "P")
12736   (gnus-summary-sort 'date reverse))
12737
12738 (defun gnus-summary-sort-by-score (&optional reverse)
12739   "Sort summary buffer by score.
12740 Argument REVERSE means reverse order."
12741   (interactive "P")
12742   (gnus-summary-sort 'score reverse))
12743
12744 (defun gnus-summary-sort (predicate reverse)
12745   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12746   (gnus-set-global-variables)
12747   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12748          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12749          (gnus-thread-sort-functions
12750           (list
12751            (if (not reverse)
12752                thread
12753              `(lambda (t1 t2)
12754                 (,thread t2 t1)))))
12755          (gnus-article-sort-functions
12756           (list
12757            (if (not reverse)
12758                article
12759              `(lambda (t1 t2)
12760                 (,article t2 t1)))))
12761          (buffer-read-only)
12762          (gnus-summary-prepare-hook nil))
12763     ;; We do the sorting by regenerating the threads.
12764     (gnus-summary-prepare)
12765     ;; Hide subthreads if needed.
12766     (when (and gnus-show-threads gnus-thread-hide-subtree)
12767       (gnus-summary-hide-all-threads)))
12768   ;; If in async mode, we send some info to the backend.
12769   (when gnus-newsgroup-async
12770     (gnus-request-asynchronous
12771      gnus-newsgroup-name gnus-newsgroup-data)))
12772
12773 (defun gnus-sortable-date (date)
12774   "Make sortable string by string-lessp from DATE.
12775 Timezone package is used."
12776   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
12777          (year (aref date 0))
12778          (month (aref date 1))
12779          (day (aref date 2)))
12780     (timezone-make-sortable-date
12781      year month day
12782      (timezone-make-time-string
12783       (aref date 3) (aref date 4) (aref date 5)))))
12784
12785 ;; Summary saving commands.
12786
12787 (defun gnus-summary-save-article (&optional n not-saved)
12788   "Save the current article using the default saver function.
12789 If N is a positive number, save the N next articles.
12790 If N is a negative number, save the N previous articles.
12791 If N is nil and any articles have been marked with the process mark,
12792 save those articles instead.
12793 The variable `gnus-default-article-saver' specifies the saver function."
12794   (interactive "P")
12795   (gnus-set-global-variables)
12796   (let ((articles (gnus-summary-work-articles n))
12797         file header article)
12798     (while articles
12799       (setq header (gnus-summary-article-header
12800                     (setq article (pop articles))))
12801       (if (not (vectorp header))
12802           ;; This is a pseudo-article.
12803           (if (assq 'name header)
12804               (gnus-copy-file (cdr (assq 'name header)))
12805             (gnus-message 1 "Article %d is unsaveable" article))
12806         ;; This is a real article.
12807         (save-window-excursion
12808           (gnus-summary-select-article t nil nil article))
12809         (unless gnus-save-all-headers
12810           ;; Remove headers accoring to `gnus-saved-headers'.
12811           (let ((gnus-visible-headers
12812                  (or gnus-saved-headers gnus-visible-headers)))
12813             (gnus-article-hide-headers nil t)))
12814         ;; Remove any X-Gnus lines.
12815         (save-excursion
12816           (set-buffer gnus-article-buffer)
12817           (save-restriction
12818             (let ((buffer-read-only nil))
12819               (nnheader-narrow-to-headers)
12820               (while (re-search-forward "^X-Gnus" nil t)
12821                 (gnus-delete-line)))))
12822         (save-window-excursion
12823           (if (not gnus-default-article-saver)
12824               (error "No default saver is defined.")
12825             (setq file (funcall
12826                         gnus-default-article-saver
12827                         (cond
12828                          ((not gnus-prompt-before-saving)
12829                           'default)
12830                          ((eq gnus-prompt-before-saving 'always)
12831                           nil)
12832                          (t file))))))
12833         (gnus-summary-remove-process-mark article)
12834         (unless not-saved
12835           (gnus-summary-set-saved-mark article))))
12836     (gnus-summary-position-point)
12837     n))
12838
12839 (defun gnus-summary-pipe-output (&optional arg)
12840   "Pipe the current article to a subprocess.
12841 If N is a positive number, pipe the N next articles.
12842 If N is a negative number, pipe the N previous articles.
12843 If N is nil and any articles have been marked with the process mark,
12844 pipe those articles instead."
12845   (interactive "P")
12846   (gnus-set-global-variables)
12847   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12848     (gnus-summary-save-article arg t))
12849   (gnus-configure-windows 'pipe))
12850
12851 (defun gnus-summary-save-article-mail (&optional arg)
12852   "Append the current article to an mail file.
12853 If N is a positive number, save the N next articles.
12854 If N is a negative number, save the N previous articles.
12855 If N is nil and any articles have been marked with the process mark,
12856 save those articles instead."
12857   (interactive "P")
12858   (gnus-set-global-variables)
12859   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12860     (gnus-summary-save-article arg)))
12861
12862 (defun gnus-summary-save-article-rmail (&optional arg)
12863   "Append the current article to an rmail file.
12864 If N is a positive number, save the N next articles.
12865 If N is a negative number, save the N previous articles.
12866 If N is nil and any articles have been marked with the process mark,
12867 save those articles instead."
12868   (interactive "P")
12869   (gnus-set-global-variables)
12870   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12871     (gnus-summary-save-article arg)))
12872
12873 (defun gnus-summary-save-article-file (&optional arg)
12874   "Append the current article to a file.
12875 If N is a positive number, save the N next articles.
12876 If N is a negative number, save the N previous articles.
12877 If N is nil and any articles have been marked with the process mark,
12878 save those articles instead."
12879   (interactive "P")
12880   (gnus-set-global-variables)
12881   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12882     (gnus-summary-save-article arg)))
12883
12884 (defun gnus-summary-save-article-body-file (&optional arg)
12885   "Append the current article body to a file.
12886 If N is a positive number, save the N next articles.
12887 If N is a negative number, save the N previous articles.
12888 If N is nil and any articles have been marked with the process mark,
12889 save those articles instead."
12890   (interactive "P")
12891   (gnus-set-global-variables)
12892   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12893     (gnus-summary-save-article arg)))
12894
12895 (defun gnus-get-split-value (methods)
12896   "Return a value based on the split METHODS."
12897   (let (split-name method result match)
12898     (when methods
12899       (save-excursion
12900         (set-buffer gnus-original-article-buffer)
12901         (save-restriction
12902           (nnheader-narrow-to-headers)
12903           (while methods
12904             (goto-char (point-min))
12905             (setq method (pop methods))
12906             (setq match (car method))
12907             (when (cond
12908                    ((stringp match)
12909                     ;; Regular expression.
12910                     (condition-case ()
12911                         (re-search-forward match nil t)
12912                       (error nil)))
12913                    ((gnus-functionp match)
12914                     ;; Function.
12915                     (save-restriction
12916                       (widen)
12917                       (setq result (funcall match gnus-newsgroup-name))))
12918                    ((consp match)
12919                     ;; Form.
12920                     (save-restriction
12921                       (widen)
12922                       (setq result (eval match)))))
12923               (setq split-name (append (cdr method) split-name))
12924               (cond ((stringp result)
12925                      (push result split-name))
12926                     ((consp result)
12927                      (setq split-name (append result split-name)))))))))
12928     split-name))
12929
12930 (defun gnus-read-move-group-name (prompt default articles prefix)
12931   "Read a group name."
12932   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12933          group-map
12934          (dum (mapatoms
12935                (lambda (g) 
12936                  (and (boundp g)
12937                       (symbol-name g)
12938                       (memq 'respool
12939                             (assoc (symbol-name
12940                                     (car (gnus-find-method-for-group
12941                                           (symbol-name g))))
12942                                    gnus-valid-select-methods))
12943                       (push (list (symbol-name g)) group-map)))
12944                gnus-active-hashtb))
12945          (prom
12946           (format "%s %s to:"
12947                   prompt
12948                   (if (> (length articles) 1)
12949                       (format "these %d articles" (length articles))
12950                     "this article")))
12951          (to-newsgroup
12952           (cond
12953            ((null split-name)
12954             (gnus-completing-read default prom
12955                                   group-map nil nil prefix
12956                                   'gnus-group-history))
12957            ((= 1 (length split-name))
12958             (gnus-completing-read (car split-name) prom group-map
12959                                   nil nil nil
12960                                   'gnus-group-history))
12961            (t
12962             (gnus-completing-read nil prom 
12963                                   (mapcar (lambda (el) (list el))
12964                                           (nreverse split-name))
12965                                   nil nil nil
12966                                   'gnus-group-history)))))
12967     (when to-newsgroup
12968       (if (or (string= to-newsgroup "")
12969               (string= to-newsgroup prefix))
12970           (setq to-newsgroup (or default "")))
12971       (or (gnus-active to-newsgroup)
12972           (gnus-activate-group to-newsgroup)
12973           (error "No such group: %s" to-newsgroup)))
12974     to-newsgroup))
12975
12976 (defun gnus-read-save-file-name (prompt default-name)
12977   (let* ((split-name (gnus-get-split-value gnus-split-methods))
12978          (file
12979           ;; Let the split methods have their say.
12980           (cond
12981            ;; No split name was found.
12982            ((null split-name)
12983             (read-file-name
12984              (concat prompt " (default "
12985                      (file-name-nondirectory default-name) ") ")
12986              (file-name-directory default-name)
12987              default-name))
12988            ;; A single split name was found
12989            ((= 1 (length split-name))
12990             (read-file-name
12991              (concat prompt " (default " (car split-name) ") ")
12992              gnus-article-save-directory
12993              (concat gnus-article-save-directory (car split-name))))
12994            ;; A list of splits was found.
12995            (t
12996             (setq split-name (nreverse split-name))
12997             (let (result)
12998               (let ((file-name-history (nconc split-name file-name-history)))
12999                 (setq result
13000                       (read-file-name
13001                        (concat prompt " (`M-p' for defaults) ")
13002                        gnus-article-save-directory
13003                        (car split-name))))
13004               (car (push result file-name-history)))))))
13005     ;; If we have read a directory, we append the default file name.
13006     (when (file-directory-p file)
13007       (setq file (concat (file-name-as-directory file)
13008                          (file-name-nondirectory default-name))))
13009     ;; Possibly translate some charaters.
13010     (nnheader-translate-file-chars file)))
13011
13012 (defun gnus-article-archive-name (group)
13013   "Return the first instance of an \"Archive-name\" in the current buffer."
13014   (let ((case-fold-search t))
13015     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
13016       (match-string 1))))
13017
13018 (defun gnus-summary-save-in-rmail (&optional filename)
13019   "Append this article to Rmail file.
13020 Optional argument FILENAME specifies file name.
13021 Directory to save to is default to `gnus-article-save-directory' which
13022 is initialized from the SAVEDIR environment variable."
13023   (interactive)
13024   (gnus-set-global-variables)
13025   (let ((default-name
13026           (funcall gnus-rmail-save-name gnus-newsgroup-name
13027                    gnus-current-headers gnus-newsgroup-last-rmail)))
13028     (setq filename
13029           (cond ((eq filename 'default)
13030                  default-name)
13031                 (filename filename)
13032                 (t (gnus-read-save-file-name
13033                     "Save in rmail file:" default-name))))
13034     (gnus-make-directory (file-name-directory filename))
13035     (gnus-eval-in-buffer-window
13036      gnus-original-article-buffer
13037      (save-excursion
13038        (save-restriction
13039          (widen)
13040          (gnus-output-to-rmail filename))))
13041     ;; Remember the directory name to save articles
13042     (setq gnus-newsgroup-last-rmail filename)))
13043
13044 (defun gnus-summary-save-in-mail (&optional filename)
13045   "Append this article to Unix mail file.
13046 Optional argument FILENAME specifies file name.
13047 Directory to save to is default to `gnus-article-save-directory' which
13048 is initialized from the SAVEDIR environment variable."
13049   (interactive)
13050   (gnus-set-global-variables)
13051   (let ((default-name
13052           (funcall gnus-mail-save-name gnus-newsgroup-name
13053                    gnus-current-headers gnus-newsgroup-last-mail)))
13054     (setq filename
13055           (cond ((eq filename 'default)
13056                  default-name)
13057                 (filename filename)
13058                 (t (gnus-read-save-file-name
13059                     "Save in Unix mail file:" default-name))))
13060     (setq filename
13061           (expand-file-name filename
13062                             (and default-name
13063                                  (file-name-directory default-name))))
13064     (gnus-make-directory (file-name-directory filename))
13065     (gnus-eval-in-buffer-window
13066      gnus-original-article-buffer
13067      (save-excursion
13068        (save-restriction
13069          (widen)
13070          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
13071              (gnus-output-to-rmail filename)
13072            (let ((mail-use-rfc822 t))
13073              (rmail-output filename 1 t t))))))
13074     ;; Remember the directory name to save articles.
13075     (setq gnus-newsgroup-last-mail filename)))
13076
13077 (defun gnus-summary-save-in-file (&optional filename)
13078   "Append this article to file.
13079 Optional argument FILENAME specifies file name.
13080 Directory to save to is default to `gnus-article-save-directory' which
13081 is initialized from the SAVEDIR environment variable."
13082   (interactive)
13083   (gnus-set-global-variables)
13084   (let ((default-name
13085           (funcall gnus-file-save-name gnus-newsgroup-name
13086                    gnus-current-headers gnus-newsgroup-last-file)))
13087     (setq filename
13088           (cond ((eq filename 'default)
13089                  default-name)
13090                 (filename filename)
13091                 (t (gnus-read-save-file-name
13092                     "Save in file:" default-name))))
13093     (gnus-make-directory (file-name-directory filename))
13094     (gnus-eval-in-buffer-window
13095      gnus-original-article-buffer
13096      (save-excursion
13097        (save-restriction
13098          (widen)
13099          (gnus-output-to-file filename))))
13100     ;; Remember the directory name to save articles.
13101     (setq gnus-newsgroup-last-file filename)))
13102
13103 (defun gnus-summary-save-body-in-file (&optional filename)
13104   "Append this article body to a file.
13105 Optional argument FILENAME specifies file name.
13106 The directory to save in defaults to `gnus-article-save-directory' which
13107 is initialized from the SAVEDIR environment variable."
13108   (interactive)
13109   (gnus-set-global-variables)
13110   (let ((default-name
13111           (funcall gnus-file-save-name gnus-newsgroup-name
13112                    gnus-current-headers gnus-newsgroup-last-file)))
13113     (setq filename
13114           (cond ((eq filename 'default)
13115                  default-name)
13116                 (filename filename)
13117                 (t (gnus-read-save-file-name
13118                     "Save body in file:" default-name))))
13119     (gnus-make-directory (file-name-directory filename))
13120     (gnus-eval-in-buffer-window
13121      gnus-article-buffer
13122      (save-excursion
13123        (save-restriction
13124          (widen)
13125          (goto-char (point-min))
13126          (and (search-forward "\n\n" nil t)
13127               (narrow-to-region (point) (point-max)))
13128          (gnus-output-to-file filename))))
13129     ;; Remember the directory name to save articles.
13130     (setq gnus-newsgroup-last-file filename)))
13131
13132 (defun gnus-summary-save-in-pipe (&optional command)
13133   "Pipe this article to subprocess."
13134   (interactive)
13135   (gnus-set-global-variables)
13136   (setq command
13137         (cond ((eq command 'default)
13138                gnus-last-shell-command)
13139               (command command)
13140               (t (read-string "Shell command on article: "
13141                               gnus-last-shell-command))))
13142   (if (string-equal command "")
13143       (setq command gnus-last-shell-command))
13144   (gnus-eval-in-buffer-window
13145    gnus-article-buffer
13146    (save-restriction
13147      (widen)
13148      (shell-command-on-region (point-min) (point-max) command nil)))
13149   (setq gnus-last-shell-command command))
13150
13151 ;; Summary extract commands
13152
13153 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
13154   (let ((buffer-read-only nil)
13155         (article (gnus-summary-article-number))
13156         after-article b e)
13157     (or (gnus-summary-goto-subject article)
13158         (error (format "No such article: %d" article)))
13159     (gnus-summary-position-point)
13160     ;; If all commands are to be bunched up on one line, we collect
13161     ;; them here.
13162     (if gnus-view-pseudos-separately
13163         ()
13164       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
13165             files action)
13166         (while ps
13167           (setq action (cdr (assq 'action (car ps))))
13168           (setq files (list (cdr (assq 'name (car ps)))))
13169           (while (and ps (cdr ps)
13170                       (string= (or action "1")
13171                                (or (cdr (assq 'action (cadr ps))) "2")))
13172             (setq files (cons (cdr (assq 'name (cadr ps))) files))
13173             (setcdr ps (cddr ps)))
13174           (if (not files)
13175               ()
13176             (if (not (string-match "%s" action))
13177                 (setq files (cons " " files)))
13178             (setq files (cons " " files))
13179             (and (assq 'execute (car ps))
13180                  (setcdr (assq 'execute (car ps))
13181                          (funcall (if (string-match "%s" action)
13182                                       'format 'concat)
13183                                   action
13184                                   (mapconcat (lambda (f) f) files " ")))))
13185           (setq ps (cdr ps)))))
13186     (if (and gnus-view-pseudos (not not-view))
13187         (while pslist
13188           (and (assq 'execute (car pslist))
13189                (gnus-execute-command (cdr (assq 'execute (car pslist)))
13190                                      (eq gnus-view-pseudos 'not-confirm)))
13191           (setq pslist (cdr pslist)))
13192       (save-excursion
13193         (while pslist
13194           (setq after-article (or (cdr (assq 'article (car pslist)))
13195                                   (gnus-summary-article-number)))
13196           (gnus-summary-goto-subject after-article)
13197           (forward-line 1)
13198           (setq b (point))
13199           (insert "    " (file-name-nondirectory
13200                                 (cdr (assq 'name (car pslist))))
13201                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
13202           (setq e (point))
13203           (forward-line -1)             ; back to `b'
13204           (gnus-add-text-properties
13205            b e (list 'gnus-number gnus-reffed-article-number
13206                      gnus-mouse-face-prop gnus-mouse-face))
13207           (gnus-data-enter
13208            after-article gnus-reffed-article-number
13209            gnus-unread-mark b (car pslist) 0 (- e b))
13210           (push gnus-reffed-article-number gnus-newsgroup-unreads)
13211           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
13212           (setq pslist (cdr pslist)))))))
13213
13214 (defun gnus-pseudos< (p1 p2)
13215   (let ((c1 (cdr (assq 'action p1)))
13216         (c2 (cdr (assq 'action p2))))
13217     (and c1 c2 (string< c1 c2))))
13218
13219 (defun gnus-request-pseudo-article (props)
13220   (cond ((assq 'execute props)
13221          (gnus-execute-command (cdr (assq 'execute props)))))
13222   (let ((gnus-current-article (gnus-summary-article-number)))
13223     (run-hooks 'gnus-mark-article-hook)))
13224
13225 (defun gnus-execute-command (command &optional automatic)
13226   (save-excursion
13227     (gnus-article-setup-buffer)
13228     (set-buffer gnus-article-buffer)
13229     (setq buffer-read-only nil)
13230     (let ((command (if automatic command (read-string "Command: " command)))
13231           ;; Just binding this here doesn't help, because there might
13232           ;; be output from the process after exiting the scope of 
13233           ;; this `let'.
13234           ;; (buffer-read-only nil)
13235           )
13236       (erase-buffer)
13237       (insert "$ " command "\n\n")
13238       (if gnus-view-pseudo-asynchronously
13239           (start-process "gnus-execute" nil "sh" "-c" command)
13240         (call-process "sh" nil t nil "-c" command)))))
13241
13242 (defun gnus-copy-file (file &optional to)
13243   "Copy FILE to TO."
13244   (interactive
13245    (list (read-file-name "Copy file: " default-directory)
13246          (read-file-name "Copy file to: " default-directory)))
13247   (gnus-set-global-variables)
13248   (or to (setq to (read-file-name "Copy file to: " default-directory)))
13249   (and (file-directory-p to)
13250        (setq to (concat (file-name-as-directory to)
13251                         (file-name-nondirectory file))))
13252   (copy-file file to))
13253
13254 ;; Summary kill commands.
13255
13256 (defun gnus-summary-edit-global-kill (article)
13257   "Edit the \"global\" kill file."
13258   (interactive (list (gnus-summary-article-number)))
13259   (gnus-set-global-variables)
13260   (gnus-group-edit-global-kill article))
13261
13262 (defun gnus-summary-edit-local-kill ()
13263   "Edit a local kill file applied to the current newsgroup."
13264   (interactive)
13265   (gnus-set-global-variables)
13266   (setq gnus-current-headers (gnus-summary-article-header))
13267   (gnus-set-global-variables)
13268   (gnus-group-edit-local-kill
13269    (gnus-summary-article-number) gnus-newsgroup-name))
13270
13271 \f
13272 ;;;
13273 ;;; Gnus article mode
13274 ;;;
13275
13276 (put 'gnus-article-mode 'mode-class 'special)
13277
13278 (if gnus-article-mode-map
13279     nil
13280   (setq gnus-article-mode-map (make-keymap))
13281   (suppress-keymap gnus-article-mode-map)
13282
13283   (gnus-define-keys gnus-article-mode-map
13284     " " gnus-article-goto-next-page
13285     "\177" gnus-article-goto-prev-page
13286     [delete] gnus-article-goto-prev-page
13287     "\C-c^" gnus-article-refer-article
13288     "h" gnus-article-show-summary
13289     "s" gnus-article-show-summary
13290     "\C-c\C-m" gnus-article-mail
13291     "?" gnus-article-describe-briefly
13292     gnus-mouse-2 gnus-article-push-button
13293     "\r" gnus-article-press-button
13294     "\t" gnus-article-next-button
13295     "\M-\t" gnus-article-prev-button
13296     "\C-c\C-b" gnus-bug)
13297
13298   (substitute-key-definition
13299    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13300
13301 (defun gnus-article-mode ()
13302   "Major mode for displaying an article.
13303
13304 All normal editing commands are switched off.
13305
13306 The following commands are available:
13307
13308 \\<gnus-article-mode-map>
13309 \\[gnus-article-next-page]\t Scroll the article one page forwards
13310 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13311 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13312 \\[gnus-article-show-summary]\t Display the summary buffer
13313 \\[gnus-article-mail]\t Send a reply to the address near point
13314 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13315 \\[gnus-info-find-node]\t Go to the Gnus info node"
13316   (interactive)
13317   (when (and menu-bar-mode
13318              (gnus-visual-p 'article-menu 'menu))
13319     (gnus-article-make-menu-bar))
13320   (kill-all-local-variables)
13321   (gnus-simplify-mode-line)
13322   (setq mode-name "Article")
13323   (setq major-mode 'gnus-article-mode)
13324   (make-local-variable 'minor-mode-alist)
13325   (or (assq 'gnus-show-mime minor-mode-alist)
13326       (setq minor-mode-alist
13327             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13328   (use-local-map gnus-article-mode-map)
13329   (make-local-variable 'page-delimiter)
13330   (setq page-delimiter gnus-page-delimiter)
13331   (buffer-disable-undo (current-buffer))
13332   (setq buffer-read-only t)             ;Disable modification
13333   (run-hooks 'gnus-article-mode-hook))
13334
13335 (defun gnus-article-setup-buffer ()
13336   "Initialize the article buffer."
13337   (let* ((name (if gnus-single-article-buffer "*Article*"
13338                  (concat "*Article " gnus-newsgroup-name "*")))
13339          (original
13340           (progn (string-match "\\*Article" name)
13341                  (concat " *Original Article"
13342                          (substring name (match-end 0))))))
13343     (setq gnus-article-buffer name)
13344     (setq gnus-original-article-buffer original)
13345     ;; This might be a variable local to the summary buffer.
13346     (unless gnus-single-article-buffer
13347       (save-excursion
13348         (set-buffer gnus-summary-buffer)
13349         (setq gnus-article-buffer name)
13350         (setq gnus-original-article-buffer original)
13351         (gnus-set-global-variables))
13352       (make-local-variable 'gnus-summary-buffer))
13353     ;; Init original article buffer.
13354     (save-excursion
13355       (set-buffer (get-buffer-create gnus-original-article-buffer))
13356       (buffer-disable-undo (current-buffer))
13357       (setq major-mode 'gnus-original-article-mode)
13358       (make-local-variable 'gnus-original-article))
13359     (if (get-buffer name)
13360         (save-excursion
13361           (set-buffer name)
13362           (buffer-disable-undo (current-buffer))
13363           (setq buffer-read-only t)
13364           (gnus-add-current-to-buffer-list)
13365           (or (eq major-mode 'gnus-article-mode)
13366               (gnus-article-mode))
13367           (current-buffer))
13368       (save-excursion
13369         (set-buffer (get-buffer-create name))
13370         (gnus-add-current-to-buffer-list)
13371         (gnus-article-mode)
13372         (current-buffer)))))
13373
13374 ;; Set article window start at LINE, where LINE is the number of lines
13375 ;; from the head of the article.
13376 (defun gnus-article-set-window-start (&optional line)
13377   (set-window-start
13378    (get-buffer-window gnus-article-buffer t)
13379    (save-excursion
13380      (set-buffer gnus-article-buffer)
13381      (goto-char (point-min))
13382      (if (not line)
13383          (point-min)
13384        (gnus-message 6 "Moved to bookmark")
13385        (search-forward "\n\n" nil t)
13386        (forward-line line)
13387        (point)))))
13388
13389 (defun gnus-kill-all-overlays ()
13390   "Delete all overlays in the current buffer."
13391   (when (fboundp 'overlay-lists)
13392     (let* ((overlayss (overlay-lists))
13393            (buffer-read-only nil)
13394            (overlays (nconc (car overlayss) (cdr overlayss))))
13395       (while overlays
13396         (delete-overlay (pop overlays))))))
13397
13398 (defun gnus-request-article-this-buffer (article group)
13399   "Get an article and insert it into this buffer."
13400   (let (do-update-line)
13401     (prog1
13402         (save-excursion
13403           (erase-buffer)
13404           (gnus-kill-all-overlays)
13405           (setq group (or group gnus-newsgroup-name))
13406
13407           ;; Open server if it has closed.
13408           (gnus-check-server (gnus-find-method-for-group group))
13409
13410           ;; Using `gnus-request-article' directly will insert the article into
13411           ;; `nntp-server-buffer' - so we'll save some time by not having to
13412           ;; copy it from the server buffer into the article buffer.
13413
13414           ;; We only request an article by message-id when we do not have the
13415           ;; headers for it, so we'll have to get those.
13416           (when (stringp article)
13417             (let ((gnus-override-method gnus-refer-article-method))
13418               (gnus-read-header article)))
13419
13420           ;; If the article number is negative, that means that this article
13421           ;; doesn't belong in this newsgroup (possibly), so we find its
13422           ;; message-id and request it by id instead of number.
13423           (when (and (numberp article)
13424                      gnus-summary-buffer
13425                      (get-buffer gnus-summary-buffer)
13426                      (buffer-name (get-buffer gnus-summary-buffer)))
13427             (save-excursion
13428               (set-buffer gnus-summary-buffer)
13429               (let ((header (gnus-summary-article-header article)))
13430                 (if (< article 0)
13431                     (cond 
13432                      ((memq article gnus-newsgroup-sparse)
13433                       ;; This is a sparse gap article.
13434                       (setq do-update-line article)
13435                       (setq article (mail-header-id header))
13436                       (let ((gnus-override-method gnus-refer-article-method))
13437                         (gnus-read-header article))
13438                       (setq gnus-newsgroup-sparse
13439                             (delq article gnus-newsgroup-sparse)))
13440                      ((vectorp header)
13441                       ;; It's a real article.
13442                       (setq article (mail-header-id header)))
13443                      (t
13444                       ;; It is an extracted pseudo-article.
13445                       (setq article 'pseudo)
13446                       (gnus-request-pseudo-article header))))
13447                 
13448                 (let ((method (gnus-find-method-for-group 
13449                                gnus-newsgroup-name)))
13450                   (if (not (eq (car method) 'nneething))
13451                       ()
13452                     (let ((dir (concat (file-name-as-directory (nth 1 method))
13453                                        (mail-header-subject header))))
13454                       (if (file-directory-p dir)
13455                           (progn
13456                             (setq article 'nneething)
13457                             (gnus-group-enter-directory dir)))))))))
13458
13459           (cond
13460            ;; Refuse to select canceled articles.
13461            ((and (numberp article)
13462                  gnus-summary-buffer
13463                  (get-buffer gnus-summary-buffer)
13464                  (buffer-name (get-buffer gnus-summary-buffer))
13465                  (eq (cdr (save-excursion
13466                             (set-buffer gnus-summary-buffer)
13467                             (assq article gnus-newsgroup-reads)))
13468                      gnus-canceled-mark))
13469             nil)
13470            ;; We first check `gnus-original-article-buffer'.
13471            ((and (get-buffer gnus-original-article-buffer)
13472                  (numberp article)
13473                  (save-excursion
13474                    (set-buffer gnus-original-article-buffer)
13475                    (and (equal (car gnus-original-article) group)
13476                         (eq (cdr gnus-original-article) article))))
13477             (insert-buffer-substring gnus-original-article-buffer)
13478             'article)
13479            ;; Check the backlog.
13480            ((and gnus-keep-backlog
13481                  (gnus-backlog-request-article group article (current-buffer)))
13482             'article)
13483            ;; Check the cache.
13484            ((and gnus-use-cache
13485                  (numberp article)
13486                  (gnus-cache-request-article article group))
13487             'article)
13488            ;; Get the article and put into the article buffer.
13489            ((or (stringp article) (numberp article))
13490             (let ((gnus-override-method
13491                    (and (stringp article) gnus-refer-article-method))
13492                   (buffer-read-only nil))
13493               (erase-buffer)
13494               (gnus-kill-all-overlays)
13495               (if (gnus-request-article article group (current-buffer))
13496                   (progn
13497                     (and gnus-keep-backlog
13498                          (numberp article)
13499                          (gnus-backlog-enter-article
13500                           group article (current-buffer)))
13501                     'article))))
13502            ;; It was a pseudo.
13503            (t article)))
13504
13505       ;; Take the article from the original article buffer
13506       ;; and place it in the buffer it's supposed to be in.
13507       (when (and (get-buffer gnus-article-buffer)
13508                  (numberp article)
13509                  (equal (buffer-name (current-buffer))
13510                         (buffer-name (get-buffer gnus-article-buffer))))
13511         (save-excursion
13512           (if (get-buffer gnus-original-article-buffer)
13513               (set-buffer (get-buffer gnus-original-article-buffer))
13514             (set-buffer (get-buffer-create gnus-original-article-buffer))
13515             (buffer-disable-undo (current-buffer))
13516             (setq major-mode 'gnus-original-article-mode)
13517             (setq buffer-read-only t)
13518             (gnus-add-current-to-buffer-list))
13519           (let (buffer-read-only)
13520             (erase-buffer)
13521             (insert-buffer-substring gnus-article-buffer))
13522           (setq gnus-original-article (cons group article))))
13523     
13524       ;; Update sparse articles.
13525       (when (and do-update-line
13526                  (or (numberp article)
13527                      (stringp article)))
13528         (let ((buf (current-buffer)))
13529           (set-buffer gnus-summary-buffer)
13530           (gnus-summary-update-article do-update-line)
13531           (gnus-summary-goto-subject do-update-line nil t)
13532           (set-window-point (get-buffer-window (current-buffer) t)
13533                             (point))
13534           (set-buffer buf))))))
13535
13536 (defun gnus-read-header (id &optional header)
13537   "Read the headers of article ID and enter them into the Gnus system."
13538   (let ((group gnus-newsgroup-name)
13539         (gnus-override-method 
13540          (and (gnus-news-group-p gnus-newsgroup-name)
13541               gnus-refer-article-method))       
13542         where)
13543     ;; First we check to see whether the header in question is already
13544     ;; fetched.
13545     (if (stringp id)
13546         ;; This is a Message-ID.
13547         (setq header (or header (gnus-id-to-header id)))
13548       ;; This is an article number.
13549       (setq header (or header (gnus-summary-article-header id))))
13550     (if (and header
13551              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
13552         ;; We have found the header.
13553         header
13554       ;; We have to really fetch the header to this article.
13555       (when (setq where
13556                   (if (gnus-check-backend-function 'request-head group)
13557                       (gnus-request-head id group)
13558                     (gnus-request-article id group)))
13559         (save-excursion
13560           (set-buffer nntp-server-buffer)
13561           (and (search-forward "\n\n" nil t)
13562                (delete-region (1- (point)) (point-max)))
13563           (goto-char (point-max))
13564           (insert ".\n")
13565           (goto-char (point-min))
13566           (insert "211 ")
13567           (princ (cond
13568                   ((numberp id) id)
13569                   ((cdr where) (cdr where))
13570                   (header (mail-header-number header))
13571                   (t gnus-reffed-article-number))
13572                  (current-buffer))
13573           (insert " Article retrieved.\n"))
13574         ;(when (and header
13575         ;          (memq (mail-header-number header) gnus-newsgroup-sparse))
13576         ;  (setcar (gnus-id-to-thread id) nil))
13577         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13578             ()                          ; Malformed head.
13579           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
13580             (if (and (stringp id)
13581                      (not (string= (gnus-group-real-name group)
13582                                    (car where))))
13583                 ;; If we fetched by Message-ID and the article came
13584                 ;; from a different group, we fudge some bogus article
13585                 ;; numbers for this article.
13586                 (mail-header-set-number header gnus-reffed-article-number))
13587             (decf gnus-reffed-article-number)
13588             (push header gnus-newsgroup-headers)
13589             (setq gnus-current-headers header)
13590             (push (mail-header-number header) gnus-newsgroup-limit))
13591           header)))))
13592
13593 (defun gnus-article-prepare (article &optional all-headers header)
13594   "Prepare ARTICLE in article mode buffer.
13595 ARTICLE should either be an article number or a Message-ID.
13596 If ARTICLE is an id, HEADER should be the article headers.
13597 If ALL-HEADERS is non-nil, no headers are hidden."
13598   (save-excursion
13599     ;; Make sure we start in a summary buffer.
13600     (unless (eq major-mode 'gnus-summary-mode)
13601       (set-buffer gnus-summary-buffer))
13602     (setq gnus-summary-buffer (current-buffer))
13603     ;; Make sure the connection to the server is alive.
13604     (unless (gnus-server-opened
13605              (gnus-find-method-for-group gnus-newsgroup-name))
13606       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13607       (gnus-request-group gnus-newsgroup-name t))
13608     (let* ((article (if header (mail-header-number header) article))
13609            (summary-buffer (current-buffer))
13610            (internal-hook gnus-article-internal-prepare-hook)
13611            (group gnus-newsgroup-name)
13612            result)
13613       (save-excursion
13614         (gnus-article-setup-buffer)
13615         (set-buffer gnus-article-buffer)
13616         ;; Deactivate active regions.
13617         (when (and (boundp 'transient-mark-mode)
13618                    transient-mark-mode)
13619           (setq mark-active nil))
13620         (if (not (setq result (let ((buffer-read-only nil))
13621                                 (gnus-request-article-this-buffer
13622                                  article group))))
13623             ;; There is no such article.
13624             (save-excursion
13625               (when (and (numberp article)
13626                          (not (memq article gnus-newsgroup-sparse)))
13627                 (setq gnus-article-current
13628                       (cons gnus-newsgroup-name article))
13629                 (set-buffer gnus-summary-buffer)
13630                 (setq gnus-current-article article)
13631                 (gnus-summary-mark-article article gnus-canceled-mark))
13632               (unless (memq article gnus-newsgroup-sparse)
13633                 (gnus-error
13634                  1 "No such article (may have expired or been canceled)")))
13635           (if (or (eq result 'pseudo) (eq result 'nneething))
13636               (progn
13637                 (save-excursion
13638                   (set-buffer summary-buffer)
13639                   (setq gnus-last-article gnus-current-article
13640                         gnus-newsgroup-history (cons gnus-current-article
13641                                                      gnus-newsgroup-history)
13642                         gnus-current-article 0
13643                         gnus-current-headers nil
13644                         gnus-article-current nil)
13645                   (if (eq result 'nneething)
13646                       (gnus-configure-windows 'summary)
13647                     (gnus-configure-windows 'article))
13648                   (gnus-set-global-variables))
13649                 (gnus-set-mode-line 'article))
13650             ;; The result from the `request' was an actual article -
13651             ;; or at least some text that is now displayed in the
13652             ;; article buffer.
13653             (if (and (numberp article)
13654                      (not (eq article gnus-current-article)))
13655                 ;; Seems like a new article has been selected.
13656                 ;; `gnus-current-article' must be an article number.
13657                 (save-excursion
13658                   (set-buffer summary-buffer)
13659                   (setq gnus-last-article gnus-current-article
13660                         gnus-newsgroup-history (cons gnus-current-article
13661                                                      gnus-newsgroup-history)
13662                         gnus-current-article article
13663                         gnus-current-headers
13664                         (gnus-summary-article-header gnus-current-article)
13665                         gnus-article-current
13666                         (cons gnus-newsgroup-name gnus-current-article))
13667                   (unless (vectorp gnus-current-headers)
13668                     (setq gnus-current-headers nil))
13669                   (gnus-summary-show-thread)
13670                   (run-hooks 'gnus-mark-article-hook)
13671                   (gnus-set-mode-line 'summary)
13672                   (and (gnus-visual-p 'article-highlight 'highlight)
13673                        (run-hooks 'gnus-visual-mark-article-hook))
13674                   ;; Set the global newsgroup variables here.
13675                   ;; Suggested by Jim Sisolak
13676                   ;; <sisolak@trans4.neep.wisc.edu>.
13677                   (gnus-set-global-variables)
13678                   (setq gnus-have-all-headers
13679                         (or all-headers gnus-show-all-headers))
13680                   (and gnus-use-cache
13681                        (vectorp (gnus-summary-article-header article))
13682                        (gnus-cache-possibly-enter-article
13683                         group article
13684                         (gnus-summary-article-header article)
13685                         (memq article gnus-newsgroup-marked)
13686                         (memq article gnus-newsgroup-dormant)
13687                         (memq article gnus-newsgroup-unreads)))))
13688             (when (or (numberp article)
13689                       (stringp article))
13690               ;; Hooks for getting information from the article.
13691               ;; This hook must be called before being narrowed.
13692               (let (buffer-read-only)
13693                 (run-hooks 'internal-hook)
13694                 (run-hooks 'gnus-article-prepare-hook)
13695                 ;; Decode MIME message.
13696                 (if gnus-show-mime
13697                     (if (or (not gnus-strict-mime)
13698                             (gnus-fetch-field "Mime-Version"))
13699                         (funcall gnus-show-mime-method)
13700                       (funcall gnus-decode-encoded-word-method)))
13701                 ;; Perform the article display hooks.
13702                 (run-hooks 'gnus-article-display-hook))
13703               ;; Do page break.
13704               (goto-char (point-min))
13705               (and gnus-break-pages (gnus-narrow-to-page)))
13706             (gnus-set-mode-line 'article)
13707             (gnus-configure-windows 'article)
13708             (goto-char (point-min))
13709             t))))))
13710
13711 (defun gnus-article-show-all-headers ()
13712   "Show all article headers in article mode buffer."
13713   (save-excursion
13714     (gnus-article-setup-buffer)
13715     (set-buffer gnus-article-buffer)
13716     (let ((buffer-read-only nil))
13717       (gnus-unhide-text (point-min) (point-max)))))
13718
13719 (defun gnus-article-hide-headers-if-wanted ()
13720   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13721 Provided for backwards compatibility."
13722   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13723       gnus-inhibit-hiding
13724       (gnus-article-hide-headers)))
13725
13726 (defsubst gnus-article-header-rank ()
13727   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13728   (let ((list gnus-sorted-header-list)
13729         (i 0))
13730     (while list
13731       (when (looking-at (car list))
13732         (setq list nil))
13733       (setq list (cdr list))
13734       (incf i))
13735     i))
13736
13737 (defun gnus-article-hide-headers (&optional arg delete)
13738   "Toggle whether to hide unwanted headers and possibly sort them as well.
13739 If given a negative prefix, always show; if given a positive prefix,
13740 always hide."
13741   (interactive "P")
13742   (unless (gnus-article-check-hidden-text 'headers arg)
13743     ;; This function might be inhibited.
13744     (unless gnus-inhibit-hiding
13745       (save-excursion
13746         (set-buffer gnus-article-buffer)
13747         (save-restriction
13748           (let ((buffer-read-only nil)
13749                 (props (nconc (list 'gnus-type 'headers)
13750                               gnus-hidden-properties))
13751                 (max (1+ (length gnus-sorted-header-list)))
13752                 (ignored (when (not (stringp gnus-visible-headers))
13753                            (cond ((stringp gnus-ignored-headers)
13754                                   gnus-ignored-headers)
13755                                  ((listp gnus-ignored-headers)
13756                                   (mapconcat 'identity gnus-ignored-headers
13757                                              "\\|")))))
13758                 (visible
13759                  (cond ((stringp gnus-visible-headers)
13760                         gnus-visible-headers)
13761                        ((and gnus-visible-headers
13762                              (listp gnus-visible-headers))
13763                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13764                 want-list beg)
13765             ;; First we narrow to just the headers.
13766             (widen)
13767             (goto-char (point-min))
13768             ;; Hide any "From " lines at the beginning of (mail) articles.
13769             (while (looking-at "From ")
13770               (forward-line 1))
13771             (unless (bobp)
13772               (gnus-hide-text (point-min) (point) props))
13773             ;; Then treat the rest of the header lines.
13774             (narrow-to-region
13775              (point)
13776              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13777             ;; Then we use the two regular expressions
13778             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13779             ;; select which header lines is to remain visible in the
13780             ;; article buffer.
13781             (goto-char (point-min))
13782             (while (re-search-forward "^[^ \t]*:" nil t)
13783               (beginning-of-line)
13784               ;; We add the headers we want to keep to a list and delete
13785               ;; them from the buffer.
13786               (gnus-put-text-property 
13787                (point) (1+ (point)) 'message-rank
13788                (if (or (and visible (looking-at visible))
13789                        (and ignored
13790                             (not (looking-at ignored))))
13791                    (gnus-article-header-rank) 
13792                  (+ 2 max)))
13793               (forward-line 1))
13794             (message-sort-headers-1)
13795             (when (setq beg (text-property-any 
13796                              (point-min) (point-max) 'message-rank (+ 2 max)))
13797               ;; We make the unwanted headers invisible.
13798               (if delete
13799                   (delete-region beg (point-max))
13800                 ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13801                 (gnus-hide-text-type beg (point-max) 'headers))
13802               ;; Work around XEmacs lossage.
13803               (gnus-put-text-property (point-min) beg 'invisible nil))))))))
13804
13805 (defun gnus-article-hide-boring-headers (&optional arg)
13806   "Toggle hiding of headers that aren't very interesting.
13807 If given a negative prefix, always show; if given a positive prefix,
13808 always hide."
13809   (interactive "P")
13810   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13811     (save-excursion
13812       (set-buffer gnus-article-buffer)
13813       (save-restriction
13814         (let ((buffer-read-only nil)
13815               (list gnus-boring-article-headers)
13816               (inhibit-point-motion-hooks t)
13817               elem)
13818           (nnheader-narrow-to-headers)
13819           (while list
13820             (setq elem (pop list))
13821             (goto-char (point-min))
13822             (cond
13823              ;; Hide empty headers.
13824              ((eq elem 'empty)
13825               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
13826                 (forward-line -1)
13827                 (gnus-hide-text-type
13828                  (progn (beginning-of-line) (point))
13829                  (progn 
13830                    (end-of-line)
13831                    (if (re-search-forward "^[^ \t]" nil t)
13832                        (match-beginning 0)
13833                      (point-max)))
13834                  'boring-headers)))
13835              ;; Hide boring Newsgroups header.
13836              ((eq elem 'newsgroups)
13837               (when (equal (mail-fetch-field "newsgroups")
13838                            (gnus-group-real-name gnus-newsgroup-name))
13839                 (gnus-article-hide-header "newsgroups")))
13840              ((eq elem 'followup-to)
13841               (when (equal (mail-fetch-field "followup-to")
13842                            (mail-fetch-field "newsgroups"))
13843                 (gnus-article-hide-header "followup-to")))
13844              ((eq elem 'reply-to)
13845               (let ((from (mail-fetch-field "from"))
13846                     (reply-to (mail-fetch-field "reply-to")))
13847                 (when (and
13848                        from reply-to
13849                        (equal 
13850                         (nth 1 (funcall gnus-extract-address-components from))
13851                         (nth 1 (funcall gnus-extract-address-components
13852                                         reply-to))))
13853                   (gnus-article-hide-header "reply-to"))))
13854              ((eq elem 'date)
13855               (let ((date (mail-fetch-field "date")))
13856                 (when (and date
13857                            (< (gnus-days-between date (current-time-string))
13858                               4))
13859                   (gnus-article-hide-header "date")))))))))))
13860
13861 (defun gnus-article-hide-header (header)
13862   (save-excursion
13863     (goto-char (point-min))
13864     (when (re-search-forward (concat "^" header ":") nil t)
13865       (gnus-hide-text-type
13866        (progn (beginning-of-line) (point))
13867        (progn 
13868          (end-of-line)
13869          (if (re-search-forward "^[^ \t]" nil t)
13870              (match-beginning 0)
13871            (point-max)))
13872        'boring-headers))))
13873
13874 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
13875 (defun gnus-article-treat-overstrike ()
13876   "Translate overstrikes into bold text."
13877   (interactive)
13878   (save-excursion
13879     (set-buffer gnus-article-buffer)
13880     (let ((buffer-read-only nil))
13881       (while (search-forward "\b" nil t)
13882         (let ((next (following-char))
13883               (previous (char-after (- (point) 2))))
13884           (cond ((eq next previous)
13885                  (gnus-put-text-property (- (point) 2) (point) 'invisible t)
13886                  (gnus-put-text-property (point) (1+ (point)) 'face 'bold))
13887                 ((eq next ?_)
13888                  (gnus-put-text-property (1- (point)) (1+ (point)) 'invisible t)
13889                  (gnus-put-text-property
13890                   (- (point) 2) (1- (point)) 'face 'underline))
13891                 ((eq previous ?_)
13892                  (gnus-put-text-property (- (point) 2) (point) 'invisible t)
13893                  (gnus-put-text-property
13894                   (point) (1+ (point))  'face 'underline))))))))
13895
13896 (defun gnus-article-word-wrap ()
13897   "Format too long lines."
13898   (interactive)
13899   (save-excursion
13900     (set-buffer gnus-article-buffer)
13901     (let ((buffer-read-only nil))
13902       (widen)
13903       (goto-char (point-min))
13904       (search-forward "\n\n" nil t)
13905       (end-of-line 1)
13906       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
13907             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
13908             (adaptive-fill-mode t))
13909         (while (not (eobp))
13910           (and (>= (current-column) (min fill-column (window-width)))
13911                (/= (preceding-char) ?:)
13912                (fill-paragraph nil))
13913           (end-of-line 2))))))
13914
13915 (defun gnus-article-remove-cr ()
13916   "Remove carriage returns from an article."
13917   (interactive)
13918   (save-excursion
13919     (set-buffer gnus-article-buffer)
13920     (let ((buffer-read-only nil))
13921       (goto-char (point-min))
13922       (while (search-forward "\r" nil t)
13923         (replace-match "" t t)))))
13924
13925 (defun gnus-article-remove-trailing-blank-lines ()
13926   "Remove all trailing blank lines from the article."
13927   (interactive)
13928   (save-excursion
13929     (set-buffer gnus-article-buffer)
13930     (let ((buffer-read-only nil))
13931       (goto-char (point-max))
13932       (delete-region
13933        (point)
13934        (progn
13935          (while (looking-at "^[ \t]*$")
13936            (forward-line -1))
13937          (forward-line 1)
13938          (point))))))
13939
13940 (defun gnus-article-display-x-face (&optional force)
13941   "Look for an X-Face header and display it if present."
13942   (interactive (list 'force))
13943   (save-excursion
13944     (set-buffer gnus-article-buffer)
13945     ;; Delete the old process, if any.
13946     (when (process-status "gnus-x-face")
13947       (delete-process "gnus-x-face"))
13948     (let ((inhibit-point-motion-hooks t)
13949           (case-fold-search nil)
13950           from)
13951       (save-restriction
13952         (nnheader-narrow-to-headers)
13953         (setq from (mail-fetch-field "from"))
13954         (goto-char (point-min))
13955         (when (and gnus-article-x-face-command
13956                    (or force
13957                        ;; Check whether this face is censored.
13958                        (not gnus-article-x-face-too-ugly)
13959                        (and gnus-article-x-face-too-ugly from
13960                             (not (string-match gnus-article-x-face-too-ugly
13961                                                from))))
13962                    ;; Has to be present.
13963                    (re-search-forward "^X-Face: " nil t))
13964           ;; We now have the area of the buffer where the X-Face is stored.
13965           (let ((beg (point))
13966                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
13967             ;; We display the face.
13968             (if (symbolp gnus-article-x-face-command)
13969                 ;; The command is a lisp function, so we call it.
13970                 (if (gnus-functionp gnus-article-x-face-command)
13971                     (funcall gnus-article-x-face-command beg end)
13972                   (error "%s is not a function" gnus-article-x-face-command))
13973               ;; The command is a string, so we interpret the command
13974               ;; as a, well, command, and fork it off.
13975               (let ((process-connection-type nil))
13976                 (process-kill-without-query
13977                  (start-process
13978                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
13979                 (process-send-region "gnus-x-face" beg end)
13980                 (process-send-eof "gnus-x-face")))))))))
13981
13982 (defalias 'gnus-header-decode-quoted-printable 'gnus-decode-rfc1522)
13983 (defun gnus-decode-rfc1522 ()
13984   "Hack to remove QP encoding from headers."
13985   (let ((case-fold-search t)
13986         (inhibit-point-motion-hooks t)
13987         (buffer-read-only nil)
13988         string)
13989     (save-restriction
13990       (narrow-to-region
13991        (goto-char (point-min))
13992        (or (search-forward "\n\n" nil t) (point-max)))
13993
13994       (while (re-search-forward 
13995               "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
13996         (setq string (match-string 1))
13997         (narrow-to-region (match-beginning 0) (match-end 0))
13998         (delete-region (point-min) (point-max))
13999         (insert string)
14000         (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
14001         (subst-char-in-region (point-min) (point-max) ?_ ? )
14002         (widen)
14003         (goto-char (point-min))))))
14004
14005 (defun gnus-article-de-quoted-unreadable (&optional force)
14006   "Do a naive translation of a quoted-printable-encoded article.
14007 This is in no way, shape or form meant as a replacement for real MIME
14008 processing, but is simply a stop-gap measure until MIME support is
14009 written.
14010 If FORCE, decode the article whether it is marked as quoted-printable
14011 or not."
14012   (interactive (list 'force))
14013   (save-excursion
14014     (set-buffer gnus-article-buffer)
14015     (let ((case-fold-search t)
14016           (buffer-read-only nil)
14017           (type (gnus-fetch-field "content-transfer-encoding")))
14018       (gnus-decode-rfc1522)
14019       (when (or force
14020                 (and type (string-match "quoted-printable" (downcase type))))
14021         (goto-char (point-min))
14022         (search-forward "\n\n" nil 'move)
14023         (gnus-mime-decode-quoted-printable (point) (point-max))))))
14024
14025 (defun gnus-mime-decode-quoted-printable (from to)
14026   "Decode Quoted-Printable in the region between FROM and TO."
14027   (interactive "r")
14028   (goto-char from)
14029   (while (search-forward "=" to t)
14030     (cond ((eq (following-char) ?\n)
14031            (delete-char -1)
14032            (delete-char 1))
14033           ((looking-at "[0-9A-F][0-9A-F]")
14034            (subst-char-in-region
14035             (1- (point)) (point) ?=
14036             (hexl-hex-string-to-integer
14037              (buffer-substring (point) (+ 2 (point)))))
14038            (delete-char 2))
14039           ((looking-at "=")
14040            (delete-char 1))
14041           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
14042
14043 (defun gnus-article-hide-pgp (&optional arg)
14044   "Toggle hiding of any PGP headers and signatures in the current article.
14045 If given a negative prefix, always show; if given a positive prefix,
14046 always hide."
14047   (interactive "P")
14048   (unless (gnus-article-check-hidden-text 'pgp arg)
14049     (save-excursion
14050       (set-buffer gnus-article-buffer)
14051       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
14052             buffer-read-only beg end)
14053         (widen)
14054         (goto-char (point-min))
14055         ;; Hide the "header".
14056         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
14057              (gnus-hide-text (match-beginning 0) (match-end 0) props))
14058         (setq beg (point))
14059         ;; Hide the actual signature.
14060         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
14061              (setq end (1+ (match-beginning 0)))
14062              (gnus-hide-text
14063               end
14064               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
14065                   (match-end 0)
14066                 ;; Perhaps we shouldn't hide to the end of the buffer
14067                 ;; if there is no end to the signature?
14068                 (point-max))
14069               props))
14070         ;; Hide "- " PGP quotation markers.
14071         (when (and beg end)
14072           (narrow-to-region beg end)
14073           (goto-char (point-min))
14074           (while (re-search-forward "^- " nil t)
14075             (gnus-hide-text (match-beginning 0) (match-end 0) props))
14076           (widen))))))
14077
14078 (defun gnus-article-hide-signature (&optional arg)
14079   "Hide the signature in the current article.
14080 If given a negative prefix, always show; if given a positive prefix,
14081 always hide."
14082   (interactive "P")
14083   (unless (gnus-article-check-hidden-text 'signature arg)
14084     (save-excursion
14085       (set-buffer gnus-article-buffer)
14086       (save-restriction
14087         (let ((buffer-read-only nil))
14088           (when (gnus-narrow-to-signature)
14089             (gnus-hide-text-type (point-min) (point-max) 'signature)))))))
14090
14091 (defun gnus-article-strip-leading-blank-lines ()
14092   "Remove all blank lines from the beginning of the article."
14093   (interactive)
14094   (save-excursion
14095     (set-buffer gnus-article-buffer)
14096     (let (buffer-read-only)
14097       (goto-char (point-min))
14098       (when (search-forward "\n\n" nil t)
14099         (while (looking-at "[ \t]$")
14100           (gnus-delete-line))))))
14101
14102 (defun gnus-narrow-to-signature ()
14103   "Narrow to the signature."
14104   (widen)
14105   (goto-char (point-max))
14106   (when (re-search-backward gnus-signature-separator nil t)
14107     (forward-line 1)
14108     (when (or (null gnus-signature-limit)
14109               (and (numberp gnus-signature-limit)
14110                    (< (- (point-max) (point)) gnus-signature-limit))
14111               (and (gnus-functionp gnus-signature-limit)
14112                    (funcall gnus-signature-limit))
14113               (and (stringp gnus-signature-limit)
14114                    (not (re-search-forward gnus-signature-limit nil t))))
14115       (narrow-to-region (point) (point-max))
14116       t)))
14117
14118 (defun gnus-article-check-hidden-text (type arg)
14119   "Return nil if hiding is necessary."
14120   (save-excursion
14121     (set-buffer gnus-article-buffer)
14122     (let ((hide (gnus-article-hidden-text-p type)))
14123       (cond ((or (and (null arg) (eq hide 'hidden))
14124                  (and arg (< 0 (prefix-numeric-value arg))))
14125              (gnus-article-show-hidden-text type))
14126             ((eq hide 'shown)
14127              (gnus-article-show-hidden-text type t))
14128             (t nil)))))
14129
14130 (defun gnus-article-hidden-text-p (type)
14131   "Say whether the current buffer contains hidden text of type TYPE."
14132   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type)))
14133     (when pos
14134       (if (get-text-property pos 'invisible)
14135           'hidden
14136         'shown))))
14137
14138 (defun gnus-article-hide (&optional arg force)
14139   "Hide all the gruft in the current article.
14140 This means that PGP stuff, signatures, cited text and (some)
14141 headers will be hidden.
14142 If given a prefix, show the hidden text instead."
14143   (interactive (list current-prefix-arg 'force))
14144   (gnus-article-hide-headers arg)
14145   (gnus-article-hide-pgp arg)
14146   (gnus-article-hide-citation-maybe arg force)
14147   (gnus-article-hide-signature arg))
14148
14149 (defun gnus-article-show-hidden-text (type &optional hide)
14150   "Show all hidden text of type TYPE.
14151 If HIDE, hide the text instead."
14152   (save-excursion
14153     (set-buffer gnus-article-buffer)
14154     (let ((buffer-read-only nil)
14155           (inhibit-point-motion-hooks t)
14156           (beg (point-min)))
14157       (while (gnus-goto-char (text-property-any
14158                               beg (point-max) 'gnus-type type))
14159         (setq beg (point))
14160         (forward-char)
14161         (if hide
14162             (gnus-hide-text beg (point) gnus-hidden-properties)
14163           (gnus-unhide-text beg (point)))
14164         (setq beg (point)))
14165       t)))
14166
14167 (defvar gnus-article-time-units
14168   `((year . ,(* 365.25 24 60 60))
14169     (week . ,(* 7 24 60 60))
14170     (day . ,(* 24 60 60))
14171     (hour . ,(* 60 60))
14172     (minute . 60)
14173     (second . 1))
14174   "Mapping from time units to seconds.")
14175
14176 (defun gnus-article-date-ut (&optional type highlight)
14177   "Convert DATE date to universal time in the current article.
14178 If TYPE is `local', convert to local time; if it is `lapsed', output
14179 how much time has lapsed since DATE."
14180   (interactive (list 'ut t))
14181   (let* ((header (or gnus-current-headers
14182                      (gnus-summary-article-header) ""))
14183          (date (and (vectorp header) (mail-header-date header)))
14184          (date-regexp "^Date: \\|^X-Sent: ")
14185          (now (current-time))
14186          (inhibit-point-motion-hooks t)
14187          bface eface)
14188     (when (and date (not (string= date "")))
14189       (save-excursion
14190         (set-buffer gnus-article-buffer)
14191         (save-restriction
14192           (nnheader-narrow-to-headers)
14193           (let ((buffer-read-only nil))
14194             ;; Delete any old Date headers.
14195             (if (re-search-forward date-regexp nil t)
14196                 (progn
14197                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
14198                         eface (get-text-property (1- (gnus-point-at-eol))
14199                                                  'face))
14200                   (message-remove-header date-regexp t)
14201                   (beginning-of-line))
14202               (goto-char (point-max)))
14203             (insert (gnus-make-date-line date type))
14204             ;; Do highlighting.
14205             (forward-line -1)
14206             (when (and (gnus-visual-p 'article-highlight 'highlight)
14207                        (looking-at "\\([^:]+\\): *\\(.*\\)$"))
14208               (gnus-put-text-property (match-beginning 1) (match-end 1)
14209                                  'face bface)
14210               (gnus-put-text-property (match-beginning 2) (match-end 2)
14211                                  'face eface))))))))
14212
14213 (defun gnus-make-date-line (date type)
14214   "Return a DATE line of TYPE."
14215   (cond
14216    ;; Convert to the local timezone.     We have to slap a
14217    ;; `condition-case' round the calls to the timezone
14218    ;; functions since they aren't particularly resistant to
14219    ;; buggy dates.
14220    ((eq type 'local)
14221     (concat "Date: " (condition-case ()
14222                          (timezone-make-date-arpa-standard date)
14223                        (error date))
14224             "\n"))
14225    ;; Convert to Universal Time.
14226    ((eq type 'ut)
14227     (concat "Date: "
14228             (condition-case ()
14229                 (timezone-make-date-arpa-standard date nil "UT")
14230               (error date))
14231             "\n"))
14232    ;; Get the original date from the article.
14233    ((eq type 'original)
14234     (concat "Date: " date "\n"))
14235    ;; Do an X-Sent lapsed format.
14236    ((eq type 'lapsed)
14237     ;; If the date is seriously mangled, the timezone
14238     ;; functions are liable to bug out, so we condition-case
14239     ;; the entire thing.
14240     (let* ((now (current-time))
14241            (real-time
14242             (condition-case ()
14243                 (gnus-time-minus
14244                  (gnus-encode-date
14245                   (timezone-make-date-arpa-standard
14246                    (current-time-string now)
14247                    (current-time-zone now) "UT"))
14248                  (gnus-encode-date
14249                   (timezone-make-date-arpa-standard
14250                    date nil "UT")))
14251               (error '(0 0))))
14252            (real-sec (+ (* (float (car real-time)) 65536)
14253                         (cadr real-time)))
14254            (sec (abs real-sec))
14255            num prev)
14256       (cond
14257        ((equal real-time '(0 0))
14258         "X-Sent: Unknown\n")
14259        ((zerop sec)
14260         "X-Sent: Now\n")
14261        (t
14262         (concat
14263          "X-Sent: "
14264          ;; This is a bit convoluted, but basically we go
14265          ;; through the time units for years, weeks, etc,
14266          ;; and divide things to see whether that results
14267          ;; in positive answers.
14268          (mapconcat
14269           (lambda (unit)
14270             (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
14271                 ;; The (remaining) seconds are too few to
14272                 ;; be divided into this time unit.
14273                 ""
14274               ;; It's big enough, so we output it.
14275               (setq sec (- sec (* num (cdr unit))))
14276               (prog1
14277                   (concat (if prev ", " "") (int-to-string
14278                                              (floor num))
14279                           " " (symbol-name (car unit))
14280                           (if (> num 1) "s" ""))
14281                 (setq prev t))))
14282           gnus-article-time-units "")
14283          ;; If dates are odd, then it might appear like the
14284          ;; article was sent in the future.
14285          (if (> real-sec 0)
14286              " ago\n"
14287            " in the future\n"))))))
14288    (t
14289     (error "Unknown conversion type: %s" type))))
14290
14291 (defun gnus-article-date-local (&optional highlight)
14292   "Convert the current article date to the local timezone."
14293   (interactive (list t))
14294   (gnus-article-date-ut 'local highlight))
14295
14296 (defun gnus-article-date-original (&optional highlight)
14297   "Convert the current article date to what it was originally.
14298 This is only useful if you have used some other date conversion
14299 function and want to see what the date was before converting."
14300   (interactive (list t))
14301   (gnus-article-date-ut 'original highlight))
14302
14303 (defun gnus-article-date-lapsed (&optional highlight)
14304   "Convert the current article date to time lapsed since it was sent."
14305   (interactive (list t))
14306   (gnus-article-date-ut 'lapsed highlight))
14307
14308 (defun gnus-article-maybe-highlight ()
14309   "Do some article highlighting if `gnus-visual' is non-nil."
14310   (if (gnus-visual-p 'article-highlight 'highlight)
14311       (gnus-article-highlight-some)))
14312
14313 ;; Article savers.
14314
14315 (defun gnus-output-to-rmail (file-name)
14316   "Append the current article to an Rmail file named FILE-NAME."
14317   (require 'rmail)
14318   ;; Most of these codes are borrowed from rmailout.el.
14319   (setq file-name (expand-file-name file-name))
14320   (setq rmail-default-rmail-file file-name)
14321   (let ((artbuf (current-buffer))
14322         (tmpbuf (get-buffer-create " *Gnus-output*")))
14323     (save-excursion
14324       (or (get-file-buffer file-name)
14325           (file-exists-p file-name)
14326           (if (gnus-yes-or-no-p
14327                (concat "\"" file-name "\" does not exist, create it? "))
14328               (let ((file-buffer (create-file-buffer file-name)))
14329                 (save-excursion
14330                   (set-buffer file-buffer)
14331                   (rmail-insert-rmail-file-header)
14332                   (let ((require-final-newline nil))
14333                     (write-region (point-min) (point-max) file-name t 1)))
14334                 (kill-buffer file-buffer))
14335             (error "Output file does not exist")))
14336       (set-buffer tmpbuf)
14337       (buffer-disable-undo (current-buffer))
14338       (erase-buffer)
14339       (insert-buffer-substring artbuf)
14340       (gnus-convert-article-to-rmail)
14341       ;; Decide whether to append to a file or to an Emacs buffer.
14342       (let ((outbuf (get-file-buffer file-name)))
14343         (if (not outbuf)
14344             (append-to-file (point-min) (point-max) file-name)
14345           ;; File has been visited, in buffer OUTBUF.
14346           (set-buffer outbuf)
14347           (let ((buffer-read-only nil)
14348                 (msg (and (boundp 'rmail-current-message)
14349                           (symbol-value 'rmail-current-message))))
14350             ;; If MSG is non-nil, buffer is in RMAIL mode.
14351             (if msg
14352                 (progn (widen)
14353                        (narrow-to-region (point-max) (point-max))))
14354             (insert-buffer-substring tmpbuf)
14355             (if msg
14356                 (progn
14357                   (goto-char (point-min))
14358                   (widen)
14359                   (search-backward "\^_")
14360                   (narrow-to-region (point) (point-max))
14361                   (goto-char (1+ (point-min)))
14362                   (rmail-count-new-messages t)
14363                   (rmail-show-message msg)))))))
14364     (kill-buffer tmpbuf)))
14365
14366 (defun gnus-output-to-file (file-name)
14367   "Append the current article to a file named FILE-NAME."
14368   (setq file-name (expand-file-name file-name))
14369   (let ((artbuf (current-buffer))
14370         (tmpbuf (get-buffer-create " *Gnus-output*")))
14371     (save-excursion
14372       (set-buffer tmpbuf)
14373       (buffer-disable-undo (current-buffer))
14374       (erase-buffer)
14375       (insert-buffer-substring artbuf)
14376       ;; Append newline at end of the buffer as separator, and then
14377       ;; save it to file.
14378       (goto-char (point-max))
14379       (insert "\n")
14380       (append-to-file (point-min) (point-max) file-name))
14381     (kill-buffer tmpbuf)))
14382
14383 (defun gnus-convert-article-to-rmail ()
14384   "Convert article in current buffer to Rmail message format."
14385   (let ((buffer-read-only nil))
14386     ;; Convert article directly into Babyl format.
14387     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14388     (goto-char (point-min))
14389     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14390     (while (search-forward "\n\^_" nil t) ;single char
14391       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14392     (goto-char (point-max))
14393     (insert "\^_")))
14394
14395 (defun gnus-narrow-to-page (&optional arg)
14396   "Narrow the article buffer to a page.
14397 If given a numerical ARG, move forward ARG pages."
14398   (interactive "P")
14399   (setq arg (if arg (prefix-numeric-value arg) 0))
14400   (save-excursion
14401     (set-buffer gnus-article-buffer)
14402     (goto-char (point-min))
14403     (widen)
14404     (when (gnus-visual-p 'page-marker)
14405       (let ((buffer-read-only nil))
14406         (gnus-remove-text-with-property 'gnus-prev)
14407         (gnus-remove-text-with-property 'gnus-next)))
14408     (when
14409         (cond ((< arg 0)
14410                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14411               ((> arg 0)
14412                (re-search-forward page-delimiter nil 'move arg)))
14413       (goto-char (match-end 0)))
14414     (narrow-to-region
14415      (point)
14416      (if (re-search-forward page-delimiter nil 'move)
14417          (match-beginning 0)
14418        (point)))
14419     (when (and (gnus-visual-p 'page-marker)
14420                (not (= (point-min) 1)))
14421       (save-excursion
14422         (goto-char (point-min))
14423         (gnus-insert-prev-page-button)))
14424     (when (and (gnus-visual-p 'page-marker)
14425                (not (= (1- (point-max)) (buffer-size))))
14426       (save-excursion
14427         (goto-char (point-max))
14428         (gnus-insert-next-page-button)))))
14429
14430 ;; Article mode commands
14431
14432 (defun gnus-article-goto-next-page ()
14433   "Show the next page of the article."
14434   (interactive)
14435   (when (gnus-article-next-page)
14436     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
14437
14438 (defun gnus-article-goto-prev-page ()
14439   "Show the next page of the article."
14440   (interactive)
14441   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))
14442     (gnus-article-prev-page nil)))
14443
14444 (defun gnus-article-next-page (&optional lines)
14445   "Show the next page of the current article.
14446 If end of article, return non-nil.  Otherwise return nil.
14447 Argument LINES specifies lines to be scrolled up."
14448   (interactive "p")
14449   (move-to-window-line -1)
14450   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14451   (if (save-excursion
14452         (end-of-line)
14453         (and (pos-visible-in-window-p)  ;Not continuation line.
14454              (eobp)))
14455       ;; Nothing in this page.
14456       (if (or (not gnus-break-pages)
14457               (save-excursion
14458                 (save-restriction
14459                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14460           t                             ;Nothing more.
14461         (gnus-narrow-to-page 1)         ;Go to next page.
14462         nil)
14463     ;; More in this page.
14464     (condition-case ()
14465         (scroll-up lines)
14466       (end-of-buffer
14467        ;; Long lines may cause an end-of-buffer error.
14468        (goto-char (point-max))))
14469     (move-to-window-line 0)
14470     nil))
14471
14472 (defun gnus-article-prev-page (&optional lines)
14473   "Show previous page of current article.
14474 Argument LINES specifies lines to be scrolled down."
14475   (interactive "p")
14476   (move-to-window-line 0)
14477   (if (and gnus-break-pages
14478            (bobp)
14479            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14480       (progn
14481         (gnus-narrow-to-page -1)        ;Go to previous page.
14482         (goto-char (point-max))
14483         (recenter -1))
14484     (prog1
14485         (condition-case ()
14486             (scroll-down lines)
14487           (error nil))
14488       (move-to-window-line 0))))
14489
14490 (defun gnus-article-refer-article ()
14491   "Read article specified by message-id around point."
14492   (interactive)
14493   (let ((point (point)))
14494     (search-forward ">" nil t)          ;Move point to end of "<....>".
14495     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14496         (let ((message-id (match-string 1)))
14497           (goto-char point)
14498           (set-buffer gnus-summary-buffer)
14499           (gnus-summary-refer-article message-id))
14500       (goto-char (point))
14501       (error "No references around point"))))
14502
14503 (defun gnus-article-show-summary ()
14504   "Reconfigure windows to show summary buffer."
14505   (interactive)
14506   (gnus-configure-windows 'article)
14507   (gnus-summary-goto-subject gnus-current-article))
14508
14509 (defun gnus-article-describe-briefly ()
14510   "Describe article mode commands briefly."
14511   (interactive)
14512   (gnus-message 6
14513                 (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")))
14514
14515 (defun gnus-article-summary-command ()
14516   "Execute the last keystroke in the summary buffer."
14517   (interactive)
14518   (let ((obuf (current-buffer))
14519         (owin (current-window-configuration))
14520         func)
14521     (switch-to-buffer gnus-summary-buffer 'norecord)
14522     (setq func (lookup-key (current-local-map) (this-command-keys)))
14523     (call-interactively func)
14524     (set-buffer obuf)
14525     (set-window-configuration owin)
14526     (set-window-point (get-buffer-window (current-buffer)) (point))))
14527
14528 (defun gnus-article-summary-command-nosave ()
14529   "Execute the last keystroke in the summary buffer."
14530   (interactive)
14531   (let (func)
14532     (pop-to-buffer gnus-summary-buffer 'norecord)
14533     (setq func (lookup-key (current-local-map) (this-command-keys)))
14534     (call-interactively func)))
14535
14536 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14537   "Read a summary buffer key sequence and execute it from the article buffer."
14538   (interactive "P")
14539   (let ((nosaves
14540          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14541            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14542            "=" "^" "\M-^"))
14543         keys)
14544     (save-excursion
14545       (set-buffer gnus-summary-buffer)
14546       (push (or key last-command-event) unread-command-events)
14547       (setq keys (read-key-sequence nil)))
14548     (message "")
14549
14550     (if (member keys nosaves)
14551         (let (func)
14552           (pop-to-buffer gnus-summary-buffer 'norecord)
14553           (if (setq func (lookup-key (current-local-map) keys))
14554               (call-interactively func)
14555             (ding)))
14556       (let ((obuf (current-buffer))
14557             (owin (current-window-configuration))
14558             (opoint (point))
14559             func in-buffer)
14560         (if not-restore-window
14561             (pop-to-buffer gnus-summary-buffer 'norecord)
14562           (switch-to-buffer gnus-summary-buffer 'norecord))
14563         (setq in-buffer (current-buffer))
14564         (if (setq func (lookup-key (current-local-map) keys))
14565             (call-interactively func)
14566           (ding))
14567         (when (eq in-buffer (current-buffer))
14568           (set-buffer obuf)
14569           (unless not-restore-window
14570             (set-window-configuration owin))
14571           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14572
14573 \f
14574 ;;;
14575 ;;; Kill file handling.
14576 ;;;
14577
14578 ;;;###autoload
14579 (defalias 'gnus-batch-kill 'gnus-batch-score)
14580 ;;;###autoload
14581 (defun gnus-batch-score ()
14582   "Run batched scoring.
14583 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14584 Newsgroups is a list of strings in Bnews format.  If you want to score
14585 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14586 score the alt hierarchy, you'd say \"!alt.all\"."
14587   (interactive)
14588   (let* ((yes-and-no
14589           (gnus-newsrc-parse-options
14590            (apply (function concat)
14591                   (mapcar (lambda (g) (concat g " "))
14592                           command-line-args-left))))
14593          (gnus-expert-user t)
14594          (nnmail-spool-file nil)
14595          (gnus-use-dribble-file nil)
14596          (yes (car yes-and-no))
14597          (no (cdr yes-and-no))
14598          group newsrc entry
14599          ;; Disable verbose message.
14600          gnus-novice-user gnus-large-newsgroup)
14601     ;; Eat all arguments.
14602     (setq command-line-args-left nil)
14603     ;; Start Gnus.
14604     (gnus)
14605     ;; Apply kills to specified newsgroups in command line arguments.
14606     (setq newsrc (cdr gnus-newsrc-alist))
14607     (while newsrc
14608       (setq group (caar newsrc))
14609       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14610       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14611                (and (car entry)
14612                     (or (eq (car entry) t)
14613                         (not (zerop (car entry)))))
14614                (if yes (string-match yes group) t)
14615                (or (null no) (not (string-match no group))))
14616           (progn
14617             (gnus-summary-read-group group nil t nil t)
14618             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14619                  (gnus-summary-exit))))
14620       (setq newsrc (cdr newsrc)))
14621     ;; Exit Emacs.
14622     (switch-to-buffer gnus-group-buffer)
14623     (gnus-group-save-newsrc)))
14624
14625 (defun gnus-apply-kill-file ()
14626   "Apply a kill file to the current newsgroup.
14627 Returns the number of articles marked as read."
14628   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14629           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14630       (gnus-apply-kill-file-internal)
14631     0))
14632
14633 (defun gnus-kill-save-kill-buffer ()
14634   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14635     (when (get-file-buffer file)
14636       (save-excursion
14637         (set-buffer (get-file-buffer file))
14638         (and (buffer-modified-p) (save-buffer))
14639         (kill-buffer (current-buffer))))))
14640
14641 (defvar gnus-kill-file-name "KILL"
14642   "Suffix of the kill files.")
14643
14644 (defun gnus-newsgroup-kill-file (newsgroup)
14645   "Return the name of a kill file name for NEWSGROUP.
14646 If NEWSGROUP is nil, return the global kill file name instead."
14647   (cond 
14648    ;; The global KILL file is placed at top of the directory.
14649    ((or (null newsgroup)
14650         (string-equal newsgroup ""))
14651     (expand-file-name gnus-kill-file-name
14652                       (or gnus-kill-files-directory "~/News")))
14653    ;; Append ".KILL" to newsgroup name.
14654    ((gnus-use-long-file-name 'not-kill)
14655     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14656                               "." gnus-kill-file-name)
14657                       (or gnus-kill-files-directory "~/News")))
14658    ;; Place "KILL" under the hierarchical directory.
14659    (t
14660     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14661                               "/" gnus-kill-file-name)
14662                       (or gnus-kill-files-directory "~/News")))))
14663
14664 \f
14665 ;;;
14666 ;;; Dribble file
14667 ;;;
14668
14669 (defvar gnus-dribble-ignore nil)
14670 (defvar gnus-dribble-eval-file nil)
14671
14672 (defun gnus-dribble-file-name ()
14673   "Return the dribble file for the current .newsrc."
14674   (concat
14675    (if gnus-dribble-directory
14676        (concat (file-name-as-directory gnus-dribble-directory)
14677                (file-name-nondirectory gnus-current-startup-file))
14678      gnus-current-startup-file)
14679    "-dribble"))
14680
14681 (defun gnus-dribble-enter (string)
14682   "Enter STRING into the dribble buffer."
14683   (if (and (not gnus-dribble-ignore)
14684            gnus-dribble-buffer
14685            (buffer-name gnus-dribble-buffer))
14686       (let ((obuf (current-buffer)))
14687         (set-buffer gnus-dribble-buffer)
14688         (insert string "\n")
14689         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14690         (set-buffer obuf))))
14691
14692 (defun gnus-dribble-read-file ()
14693   "Read the dribble file from disk."
14694   (let ((dribble-file (gnus-dribble-file-name)))
14695     (save-excursion
14696       (set-buffer (setq gnus-dribble-buffer
14697                         (get-buffer-create
14698                          (file-name-nondirectory dribble-file))))
14699       (gnus-add-current-to-buffer-list)
14700       (erase-buffer)
14701       (setq buffer-file-name dribble-file)
14702       (auto-save-mode t)
14703       (buffer-disable-undo (current-buffer))
14704       (bury-buffer (current-buffer))
14705       (set-buffer-modified-p nil)
14706       (let ((auto (make-auto-save-file-name))
14707             (gnus-dribble-ignore t))
14708         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14709           ;; Load whichever file is newest -- the auto save file
14710           ;; or the "real" file.
14711           (if (file-newer-than-file-p auto dribble-file)
14712               (insert-file-contents auto)
14713             (insert-file-contents dribble-file))
14714           (unless (zerop (buffer-size))
14715             (set-buffer-modified-p t))
14716           ;; Set the file modes to reflect the .newsrc file modes.
14717           (save-buffer)
14718           (when (file-exists-p gnus-current-startup-file)
14719             (set-file-modes dribble-file
14720                             (file-modes gnus-current-startup-file)))
14721           ;; Possibly eval the file later.
14722           (when (gnus-y-or-n-p
14723                  "Auto-save file exists.  Do you want to read it? ")
14724             (setq gnus-dribble-eval-file t)))))))
14725
14726 (defun gnus-dribble-eval-file ()
14727   (if (not gnus-dribble-eval-file)
14728       ()
14729     (setq gnus-dribble-eval-file nil)
14730     (save-excursion
14731       (let ((gnus-dribble-ignore t))
14732         (set-buffer gnus-dribble-buffer)
14733         (eval-buffer (current-buffer))))))
14734
14735 (defun gnus-dribble-delete-file ()
14736   (if (file-exists-p (gnus-dribble-file-name))
14737       (delete-file (gnus-dribble-file-name)))
14738   (if gnus-dribble-buffer
14739       (save-excursion
14740         (set-buffer gnus-dribble-buffer)
14741         (let ((auto (make-auto-save-file-name)))
14742           (if (file-exists-p auto)
14743               (delete-file auto))
14744           (erase-buffer)
14745           (set-buffer-modified-p nil)))))
14746
14747 (defun gnus-dribble-save ()
14748   (if (and gnus-dribble-buffer
14749            (buffer-name gnus-dribble-buffer))
14750       (save-excursion
14751         (set-buffer gnus-dribble-buffer)
14752         (save-buffer))))
14753
14754 (defun gnus-dribble-clear ()
14755   (save-excursion
14756     (if (gnus-buffer-exists-p gnus-dribble-buffer)
14757         (progn
14758           (set-buffer gnus-dribble-buffer)
14759           (erase-buffer)
14760           (set-buffer-modified-p nil)
14761           (setq buffer-saved-size (buffer-size))))))
14762
14763 \f
14764 ;;;
14765 ;;; Server Communication
14766 ;;;
14767
14768 (defun gnus-start-news-server (&optional confirm)
14769   "Open a method for getting news.
14770 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14771   (let (how)
14772     (if gnus-current-select-method
14773         ;; Stream is already opened.
14774         nil
14775       ;; Open NNTP server.
14776       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14777       (if confirm
14778           (progn
14779             ;; Read server name with completion.
14780             (setq gnus-nntp-server
14781                   (completing-read "NNTP server: "
14782                                    (mapcar (lambda (server) (list server))
14783                                            (cons (list gnus-nntp-server)
14784                                                  gnus-secondary-servers))
14785                                    nil nil gnus-nntp-server))))
14786
14787       (if (and gnus-nntp-server
14788                (stringp gnus-nntp-server)
14789                (not (string= gnus-nntp-server "")))
14790           (setq gnus-select-method
14791                 (cond ((or (string= gnus-nntp-server "")
14792                            (string= gnus-nntp-server "::"))
14793                        (list 'nnspool (system-name)))
14794                       ((string-match "^:" gnus-nntp-server)
14795                        (list 'nnmh gnus-nntp-server
14796                              (list 'nnmh-directory
14797                                    (file-name-as-directory
14798                                     (expand-file-name
14799                                      (concat "~/" (substring
14800                                                    gnus-nntp-server 1)))))
14801                              (list 'nnmh-get-new-mail nil)))
14802                       (t
14803                        (list 'nntp gnus-nntp-server)))))
14804
14805       (setq how (car gnus-select-method))
14806       (cond ((eq how 'nnspool)
14807              (require 'nnspool)
14808              (gnus-message 5 "Looking up local news spool..."))
14809             ((eq how 'nnmh)
14810              (require 'nnmh)
14811              (gnus-message 5 "Looking up mh spool..."))
14812             (t
14813              (require 'nntp)))
14814       (setq gnus-current-select-method gnus-select-method)
14815       (run-hooks 'gnus-open-server-hook)
14816       (or
14817        ;; gnus-open-server-hook might have opened it
14818        (gnus-server-opened gnus-select-method)
14819        (gnus-open-server gnus-select-method)
14820        (gnus-y-or-n-p
14821         (format
14822          "%s (%s) open error: '%s'.     Continue? "
14823          (car gnus-select-method) (cadr gnus-select-method)
14824          (gnus-status-message gnus-select-method)))
14825        (gnus-error 1 "Couldn't open server on %s"
14826                    (nth 1 gnus-select-method))))))
14827
14828 (defun gnus-check-group (group)
14829   "Try to make sure that the server where GROUP exists is alive."
14830   (let ((method (gnus-find-method-for-group group)))
14831     (or (gnus-server-opened method)
14832         (gnus-open-server method))))
14833
14834 (defun gnus-check-server (&optional method silent)
14835   "Check whether the connection to METHOD is down.
14836 If METHOD is nil, use `gnus-select-method'.
14837 If it is down, start it up (again)."
14838   (let ((method (or method gnus-select-method)))
14839     ;; Transform virtual server names into select methods.
14840     (when (stringp method)
14841       (setq method (gnus-server-to-method method)))
14842     (if (gnus-server-opened method)
14843         ;; The stream is already opened.
14844         t
14845       ;; Open the server.
14846       (unless silent
14847         (gnus-message 5 "Opening %s server%s..." (car method)
14848                       (if (equal (nth 1 method) "") ""
14849                         (format " on %s" (nth 1 method)))))
14850       (run-hooks 'gnus-open-server-hook)
14851       (prog1
14852           (gnus-open-server method)
14853         (unless silent
14854           (message ""))))))
14855
14856 (defun gnus-get-function (method function)
14857   "Return a function symbol based on METHOD and FUNCTION."
14858   ;; Translate server names into methods.
14859   (unless method
14860     (error "Attempted use of a nil select method"))
14861   (when (stringp method)
14862     (setq method (gnus-server-to-method method)))
14863   (let ((func (intern (format "%s-%s" (car method) function))))
14864     ;; If the functions isn't bound, we require the backend in
14865     ;; question.
14866     (unless (fboundp func)
14867       (require (car method))
14868       (unless (fboundp func)
14869         ;; This backend doesn't implement this function.
14870         (error "No such function: %s" func)))
14871     func))
14872
14873 \f
14874 ;;;
14875 ;;; Interface functions to the backends.
14876 ;;;
14877
14878 (defun gnus-open-server (method)
14879   "Open a connection to METHOD."
14880   (when (stringp method)
14881     (setq method (gnus-server-to-method method)))
14882   (let ((elem (assoc method gnus-opened-servers)))
14883     ;; If this method was previously denied, we just return nil.
14884     (if (eq (nth 1 elem) 'denied)
14885         (progn
14886           (gnus-message 1 "Denied server")
14887           nil)
14888       ;; Open the server.
14889       (let ((result
14890              (funcall (gnus-get-function method 'open-server)
14891                       (nth 1 method) (nthcdr 2 method))))
14892         ;; If this hasn't been opened before, we add it to the list.
14893         (unless elem
14894           (setq elem (list method nil)
14895                 gnus-opened-servers (cons elem gnus-opened-servers)))
14896         ;; Set the status of this server.
14897         (setcar (cdr elem) (if result 'ok 'denied))
14898         ;; Return the result from the "open" call.
14899         result))))
14900
14901 (defun gnus-close-server (method)
14902   "Close the connection to METHOD."
14903   (when (stringp method)
14904     (setq method (gnus-server-to-method method)))
14905   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
14906
14907 (defun gnus-request-list (method)
14908   "Request the active file from METHOD."
14909   (when (stringp method)
14910     (setq method (gnus-server-to-method method)))
14911   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
14912
14913 (defun gnus-request-list-newsgroups (method)
14914   "Request the newsgroups file from METHOD."
14915   (when (stringp method)
14916     (setq method (gnus-server-to-method method)))
14917   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
14918
14919 (defun gnus-request-newgroups (date method)
14920   "Request all new groups since DATE from METHOD."
14921   (when (stringp method)
14922     (setq method (gnus-server-to-method method)))
14923   (funcall (gnus-get-function method 'request-newgroups)
14924            date (nth 1 method)))
14925
14926 (defun gnus-server-opened (method)
14927   "Check whether a connection to METHOD has been opened."
14928   (when (stringp method)
14929     (setq method (gnus-server-to-method method)))
14930   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
14931
14932 (defun gnus-status-message (method)
14933   "Return the status message from METHOD.
14934 If METHOD is a string, it is interpreted as a group name.   The method
14935 this group uses will be queried."
14936   (let ((method (if (stringp method) (gnus-find-method-for-group method)
14937                   method)))
14938     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
14939
14940 (defun gnus-request-group (group &optional dont-check method)
14941   "Request GROUP.  If DONT-CHECK, no information is required."
14942   (let ((method (or method (gnus-find-method-for-group group))))
14943     (when (stringp method)
14944       (setq method (gnus-server-to-method method)))
14945     (funcall (gnus-get-function method 'request-group)
14946              (gnus-group-real-name group) (nth 1 method) dont-check)))
14947
14948 (defun gnus-request-asynchronous (group &optional articles)
14949   "Request that GROUP behave asynchronously.
14950 ARTICLES is the `data' of the group."
14951   (let ((method (gnus-find-method-for-group group)))
14952     (funcall (gnus-get-function method 'request-asynchronous)
14953              (gnus-group-real-name group) (nth 1 method) articles)))
14954
14955 (defun gnus-list-active-group (group)
14956   "Request active information on GROUP."
14957   (let ((method (gnus-find-method-for-group group))
14958         (func 'list-active-group))
14959     (when (gnus-check-backend-function func group)
14960       (funcall (gnus-get-function method func)
14961                (gnus-group-real-name group) (nth 1 method)))))
14962
14963 (defun gnus-request-group-description (group)
14964   "Request a description of GROUP."
14965   (let ((method (gnus-find-method-for-group group))
14966         (func 'request-group-description))
14967     (when (gnus-check-backend-function func group)
14968       (funcall (gnus-get-function method func)
14969                (gnus-group-real-name group) (nth 1 method)))))
14970
14971 (defun gnus-close-group (group)
14972   "Request the GROUP be closed."
14973   (let ((method (gnus-find-method-for-group group)))
14974     (funcall (gnus-get-function method 'close-group)
14975              (gnus-group-real-name group) (nth 1 method))))
14976
14977 (defun gnus-retrieve-headers (articles group &optional fetch-old)
14978   "Request headers for ARTICLES in GROUP.
14979 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
14980   (let ((method (gnus-find-method-for-group group)))
14981     (if (and gnus-use-cache (numberp (car articles)))
14982         (gnus-cache-retrieve-headers articles group fetch-old)
14983       (funcall (gnus-get-function method 'retrieve-headers)
14984                articles (gnus-group-real-name group) (nth 1 method)
14985                fetch-old))))
14986
14987 (defun gnus-retrieve-groups (groups method)
14988   "Request active information on GROUPS from METHOD."
14989   (when (stringp method)
14990     (setq method (gnus-server-to-method method)))
14991   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
14992
14993 (defun gnus-request-type (group &optional article)
14994   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14995   (let ((method (gnus-find-method-for-group group)))
14996     (if (not (gnus-check-backend-function 'request-type (car method)))
14997         'unknown
14998       (funcall (gnus-get-function method 'request-type)
14999                (gnus-group-real-name group) article))))
15000
15001 (defun gnus-request-update-mark (group article mark)
15002   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
15003   (let ((method (gnus-find-method-for-group group)))
15004     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
15005         mark
15006       (funcall (gnus-get-function method 'request-update-mark)
15007                (gnus-group-real-name group) article mark))))
15008
15009 (defun gnus-request-article (article group &optional buffer)
15010   "Request the ARTICLE in GROUP.
15011 ARTICLE can either be an article number or an article Message-ID.
15012 If BUFFER, insert the article in that group."
15013   (let ((method (gnus-find-method-for-group group)))
15014     (funcall (gnus-get-function method 'request-article)
15015              article (gnus-group-real-name group) (nth 1 method) buffer)))
15016
15017 (defun gnus-request-head (article group)
15018   "Request the head of ARTICLE in GROUP."
15019   (let ((method (gnus-find-method-for-group group)))
15020     (funcall (gnus-get-function method 'request-head)
15021              article (gnus-group-real-name group) (nth 1 method))))
15022
15023 (defun gnus-request-body (article group)
15024   "Request the body of ARTICLE in GROUP."
15025   (let ((method (gnus-find-method-for-group group)))
15026     (funcall (gnus-get-function method 'request-body)
15027              article (gnus-group-real-name group) (nth 1 method))))
15028
15029 (defun gnus-request-post (method)
15030   "Post the current buffer using METHOD."
15031   (when (stringp method)
15032     (setq method (gnus-server-to-method method)))
15033   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
15034
15035 (defun gnus-request-scan (group method)
15036   "Request a SCAN being performed in GROUP from METHOD.
15037 If GROUP is nil, all groups on METHOD are scanned."
15038   (let ((method (if group (gnus-find-method-for-group group) method)))
15039     (funcall (gnus-get-function method 'request-scan)
15040              (and group (gnus-group-real-name group)) (nth 1 method))))
15041
15042 (defsubst gnus-request-update-info (info method)
15043   "Request that METHOD update INFO."
15044   (when (stringp method)
15045     (setq method (gnus-server-to-method method)))
15046   (when (gnus-check-backend-function 'request-update-info (car method))
15047     (funcall (gnus-get-function method 'request-update-info)
15048              (gnus-group-real-name (gnus-info-group info))
15049              info (nth 1 method))))
15050
15051 (defun gnus-request-expire-articles (articles group &optional force)
15052   (let ((method (gnus-find-method-for-group group)))
15053     (funcall (gnus-get-function method 'request-expire-articles)
15054              articles (gnus-group-real-name group) (nth 1 method)
15055              force)))
15056
15057 (defun gnus-request-move-article
15058   (article group server accept-function &optional last)
15059   (let ((method (gnus-find-method-for-group group)))
15060     (funcall (gnus-get-function method 'request-move-article)
15061              article (gnus-group-real-name group)
15062              (nth 1 method) accept-function last)))
15063
15064 (defun gnus-request-accept-article (group method &optional last)
15065   ;; Make sure there's a newline at the end of the article.
15066   (when (stringp method)
15067     (setq method (gnus-server-to-method method)))
15068   (when (and (not method)
15069              (stringp group))
15070     (setq method (gnus-find-method-for-group group)))
15071   (goto-char (point-max))
15072   (unless (bolp)
15073     (insert "\n"))
15074   (let ((func (car (or method (gnus-find-method-for-group group)))))
15075     (funcall (intern (format "%s-request-accept-article" func))
15076              (if (stringp group) (gnus-group-real-name group) group)
15077              (cadr method)
15078              last)))
15079
15080 (defun gnus-request-replace-article (article group buffer)
15081   (let ((func (car (gnus-find-method-for-group group))))
15082     (funcall (intern (format "%s-request-replace-article" func))
15083              article (gnus-group-real-name group) buffer)))
15084
15085 (defun gnus-request-associate-buffer (group)
15086   (let ((method (gnus-find-method-for-group group)))
15087     (funcall (gnus-get-function method 'request-associate-buffer)
15088              (gnus-group-real-name group))))
15089
15090 (defun gnus-request-restore-buffer (article group)
15091   "Request a new buffer restored to the state of ARTICLE."
15092   (let ((method (gnus-find-method-for-group group)))
15093     (funcall (gnus-get-function method 'request-restore-buffer)
15094              article (gnus-group-real-name group) (nth 1 method))))
15095
15096 (defun gnus-request-create-group (group &optional method)
15097   (when (stringp method)
15098     (setq method (gnus-server-to-method method)))
15099   (let ((method (or method (gnus-find-method-for-group group))))
15100     (funcall (gnus-get-function method 'request-create-group)
15101              (gnus-group-real-name group) (nth 1 method))))
15102
15103 (defun gnus-request-delete-group (group &optional force)
15104   (let ((method (gnus-find-method-for-group group)))
15105     (funcall (gnus-get-function method 'request-delete-group)
15106              (gnus-group-real-name group) force (nth 1 method))))
15107
15108 (defun gnus-request-rename-group (group new-name)
15109   (let ((method (gnus-find-method-for-group group)))
15110     (funcall (gnus-get-function method 'request-rename-group)
15111              (gnus-group-real-name group)
15112              (gnus-group-real-name new-name) (nth 1 method))))
15113
15114 (defun gnus-member-of-valid (symbol group)
15115   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
15116   (memq symbol (assoc
15117                 (symbol-name (car (gnus-find-method-for-group group)))
15118                 gnus-valid-select-methods)))
15119
15120 (defun gnus-method-option-p (method option)
15121   "Return non-nil if select METHOD has OPTION as a parameter."
15122   (when (stringp method)
15123     (setq method (gnus-server-to-method method)))
15124   (memq option (assoc (format "%s" (car method))
15125                       gnus-valid-select-methods)))
15126
15127 (defun gnus-server-extend-method (group method)
15128   ;; This function "extends" a virtual server.  If the server is
15129   ;; "hello", and the select method is ("hello" (my-var "something"))
15130   ;; in the group "alt.alt", this will result in a new virtual server
15131   ;; called "hello+alt.alt".
15132   (let ((entry
15133          (gnus-copy-sequence
15134           (if (equal (car method) "native") gnus-select-method
15135             (cdr (assoc (car method) gnus-server-alist))))))
15136     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
15137     (nconc entry (cdr method))))
15138
15139 (defun gnus-find-method-for-group (group &optional info)
15140   "Find the select method that GROUP uses."
15141   (or gnus-override-method
15142       (and (not group)
15143            gnus-select-method)
15144       (let ((info (or info (gnus-get-info group)))
15145             method)
15146         (if (or (not info)
15147                 (not (setq method (gnus-info-method info)))
15148                 (equal method "native"))
15149             gnus-select-method
15150           (setq method
15151                 (cond ((stringp method)
15152                        (gnus-server-to-method method))
15153                       ((stringp (car method))
15154                        (gnus-server-extend-method group method))
15155                       (t
15156                        method)))
15157           (cond ((equal (cadr method) "")
15158                  method)
15159                 ((null (cadr method))
15160                  (list (car method) ""))
15161                 (t
15162                  (gnus-server-add-address method)))))))
15163
15164 (defun gnus-check-backend-function (func group)
15165   "Check whether GROUP supports function FUNC."
15166   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
15167                   group)))
15168     (fboundp (intern (format "%s-%s" method func)))))
15169
15170 (defun gnus-methods-using (feature)
15171   "Find all methods that have FEATURE."
15172   (let ((valids gnus-valid-select-methods)
15173         outs)
15174     (while valids
15175       (if (memq feature (car valids))
15176           (setq outs (cons (car valids) outs)))
15177       (setq valids (cdr valids)))
15178     outs))
15179
15180 \f
15181 ;;;
15182 ;;; Active & Newsrc File Handling
15183 ;;;
15184
15185 (defun gnus-setup-news (&optional rawfile level dont-connect)
15186   "Setup news information.
15187 If RAWFILE is non-nil, the .newsrc file will also be read.
15188 If LEVEL is non-nil, the news will be set up at level LEVEL."
15189   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
15190
15191     (when init 
15192       ;; Clear some variables to re-initialize news information.
15193       (setq gnus-newsrc-alist nil
15194             gnus-active-hashtb nil)
15195       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
15196       (gnus-read-newsrc-file rawfile))
15197
15198     (when (and (not (assoc "archive" gnus-server-alist))
15199                gnus-message-archive-method)
15200       (push (cons "archive" gnus-message-archive-method)
15201             gnus-server-alist))
15202
15203     ;; If we don't read the complete active file, we fill in the
15204     ;; hashtb here.
15205     (if (or (null gnus-read-active-file)
15206             (eq gnus-read-active-file 'some))
15207         (gnus-update-active-hashtb-from-killed))
15208
15209     ;; Read the active file and create `gnus-active-hashtb'.
15210     ;; If `gnus-read-active-file' is nil, then we just create an empty
15211     ;; hash table.  The partial filling out of the hash table will be
15212     ;; done in `gnus-get-unread-articles'.
15213     (and gnus-read-active-file
15214          (not level)
15215          (gnus-read-active-file))
15216
15217     (or gnus-active-hashtb
15218         (setq gnus-active-hashtb (make-vector 4095 0)))
15219
15220     ;; Initialize the cache.
15221     (when gnus-use-cache
15222       (gnus-cache-open))
15223
15224     ;; Possibly eval the dribble file.
15225     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
15226
15227     (gnus-update-format-specifications)
15228
15229     ;; See whether we need to read the description file.
15230     (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
15231              (not gnus-description-hashtb)
15232              (not dont-connect)
15233              gnus-read-active-file)
15234         (gnus-read-all-descriptions-files))
15235
15236     ;; Find new newsgroups and treat them.
15237     (if (and init gnus-check-new-newsgroups (not level)
15238              (gnus-check-server gnus-select-method))
15239         (gnus-find-new-newsgroups))
15240
15241     ;; We might read in new NoCeM messages here.
15242     (when (and gnus-use-nocem 
15243                (not level)
15244                (not dont-connect))
15245       (gnus-nocem-scan-groups))
15246
15247     ;; Find the number of unread articles in each non-dead group.
15248     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
15249       (gnus-get-unread-articles level))
15250
15251     (if (and init gnus-check-bogus-newsgroups
15252              gnus-read-active-file (not level)
15253              (gnus-server-opened gnus-select-method))
15254         (gnus-check-bogus-newsgroups))))
15255
15256 (defun gnus-find-new-newsgroups (&optional arg)
15257   "Search for new newsgroups and add them.
15258 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
15259 The `-n' option line from .newsrc is respected.
15260 If ARG (the prefix), use the `ask-server' method to query
15261 the server for new groups."
15262   (interactive "P")
15263   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
15264                        (null gnus-read-active-file)
15265                        (eq gnus-read-active-file 'some))
15266                    'ask-server gnus-check-new-newsgroups)))
15267     (unless (gnus-check-first-time-used)
15268       (if (or (consp check)
15269               (eq check 'ask-server))
15270           ;; Ask the server for new groups.
15271           (gnus-ask-server-for-new-groups)
15272         ;; Go through the active hashtb and look for new groups.
15273         (let ((groups 0)
15274               group new-newsgroups)
15275           (gnus-message 5 "Looking for new newsgroups...")
15276           (unless gnus-have-read-active-file
15277             (gnus-read-active-file))
15278           (setq gnus-newsrc-last-checked-date (current-time-string))
15279           (unless gnus-killed-hashtb
15280             (gnus-make-hashtable-from-killed))
15281           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
15282           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
15283           (mapatoms
15284            (lambda (sym)
15285              (if (or (null (setq group (symbol-name sym)))
15286                      (not (boundp sym))
15287                      (null (symbol-value sym))
15288                      (gnus-gethash group gnus-killed-hashtb)
15289                      (gnus-gethash group gnus-newsrc-hashtb))
15290                  ()
15291                (let ((do-sub (gnus-matches-options-n group)))
15292                  (cond
15293                   ((eq do-sub 'subscribe)
15294                    (setq groups (1+ groups))
15295                    (gnus-sethash group group gnus-killed-hashtb)
15296                    (funcall gnus-subscribe-options-newsgroup-method group))
15297                   ((eq do-sub 'ignore)
15298                    nil)
15299                   (t
15300                    (setq groups (1+ groups))
15301                    (gnus-sethash group group gnus-killed-hashtb)
15302                    (if gnus-subscribe-hierarchical-interactive
15303                        (setq new-newsgroups (cons group new-newsgroups))
15304                      (funcall gnus-subscribe-newsgroup-method group)))))))
15305            gnus-active-hashtb)
15306           (when new-newsgroups
15307             (gnus-subscribe-hierarchical-interactive new-newsgroups))
15308           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15309           (if (> groups 0)
15310               (gnus-message 6 "%d new newsgroup%s arrived."
15311                             groups (if (> groups 1) "s have" " has"))
15312             (gnus-message 6 "No new newsgroups.")))))))
15313
15314 (defun gnus-matches-options-n (group)
15315   ;; Returns `subscribe' if the group is to be unconditionally
15316   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
15317   ;; no match for the group.
15318
15319   ;; First we check the two user variables.
15320   (cond
15321    ((and gnus-options-subscribe
15322          (string-match gnus-options-subscribe group))
15323     'subscribe)
15324    ((and gnus-auto-subscribed-groups
15325          (string-match gnus-auto-subscribed-groups group))
15326     'subscribe)
15327    ((and gnus-options-not-subscribe
15328          (string-match gnus-options-not-subscribe group))
15329     'ignore)
15330    ;; Then we go through the list that was retrieved from the .newsrc
15331    ;; file.  This list has elements on the form
15332    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
15333    ;; is in the reverse order of the options line) is returned.
15334    (t
15335     (let ((regs gnus-newsrc-options-n))
15336       (while (and regs
15337                   (not (string-match (caar regs) group)))
15338         (setq regs (cdr regs)))
15339       (and regs (cdar regs))))))
15340
15341 (defun gnus-ask-server-for-new-groups ()
15342   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
15343          (methods (cons gnus-select-method
15344                         (nconc
15345                          (when gnus-message-archive-method
15346                            (list "archive"))
15347                          (append
15348                           (and (consp gnus-check-new-newsgroups)
15349                                gnus-check-new-newsgroups)
15350                           gnus-secondary-select-methods))))
15351          (groups 0)
15352          (new-date (current-time-string))
15353          group new-newsgroups got-new method hashtb
15354          gnus-override-subscribe-method)
15355     ;; Go through both primary and secondary select methods and
15356     ;; request new newsgroups.
15357     (while (setq method (gnus-server-get-method nil (pop methods)))
15358       (setq new-newsgroups nil)
15359       (setq gnus-override-subscribe-method method)
15360       (when (and (gnus-check-server method)
15361                  (gnus-request-newgroups date method))
15362         (save-excursion
15363           (setq got-new t)
15364           (setq hashtb (gnus-make-hashtable 100))
15365           (set-buffer nntp-server-buffer)
15366           ;; Enter all the new groups into a hashtable.
15367           (gnus-active-to-gnus-format method hashtb 'ignore))
15368         ;; Now all new groups from `method' are in `hashtb'.
15369         (mapatoms
15370          (lambda (group-sym)
15371            (if (or (null (setq group (symbol-name group-sym)))
15372                    (not (boundp group-sym))
15373                    (null (symbol-value group-sym))
15374                    (gnus-gethash group gnus-newsrc-hashtb)
15375                    (member group gnus-zombie-list)
15376                    (member group gnus-killed-list))
15377                ;; The group is already known.
15378                ()
15379              ;; Make this group active.
15380              (when (symbol-value group-sym)
15381                (gnus-set-active group (symbol-value group-sym)))
15382              ;; Check whether we want it or not.
15383              (let ((do-sub (gnus-matches-options-n group)))
15384                (cond
15385                 ((eq do-sub 'subscribe)
15386                  (incf groups)
15387                  (gnus-sethash group group gnus-killed-hashtb)
15388                  (funcall gnus-subscribe-options-newsgroup-method group))
15389                 ((eq do-sub 'ignore)
15390                  nil)
15391                 (t
15392                  (incf groups)
15393                  (gnus-sethash group group gnus-killed-hashtb)
15394                  (if gnus-subscribe-hierarchical-interactive
15395                      (push group new-newsgroups)
15396                    (funcall gnus-subscribe-newsgroup-method group)))))))
15397          hashtb))
15398       (when new-newsgroups
15399         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
15400     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15401     (when (> groups 0)
15402       (gnus-message 6 "%d new newsgroup%s arrived."
15403                     groups (if (> groups 1) "s have" " has")))
15404     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15405     got-new))
15406
15407 (defun gnus-check-first-time-used ()
15408   (if (or (> (length gnus-newsrc-alist) 1)
15409           (file-exists-p gnus-startup-file)
15410           (file-exists-p (concat gnus-startup-file ".el"))
15411           (file-exists-p (concat gnus-startup-file ".eld")))
15412       nil
15413     (gnus-message 6 "First time user; subscribing you to default groups")
15414     (unless (gnus-read-active-file-p)
15415       (gnus-read-active-file))
15416     (setq gnus-newsrc-last-checked-date (current-time-string))
15417     (let ((groups gnus-default-subscribed-newsgroups)
15418           group)
15419       (if (eq groups t)
15420           nil
15421         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15422         (mapatoms
15423          (lambda (sym)
15424            (if (null (setq group (symbol-name sym)))
15425                ()
15426              (let ((do-sub (gnus-matches-options-n group)))
15427                (cond
15428                 ((eq do-sub 'subscribe)
15429                  (gnus-sethash group group gnus-killed-hashtb)
15430                  (funcall gnus-subscribe-options-newsgroup-method group))
15431                 ((eq do-sub 'ignore)
15432                  nil)
15433                 (t
15434                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15435          gnus-active-hashtb)
15436         (while groups
15437           (if (gnus-active (car groups))
15438               (gnus-group-change-level
15439                (car groups) gnus-level-default-subscribed gnus-level-killed))
15440           (setq groups (cdr groups)))
15441         (gnus-group-make-help-group)
15442         (and gnus-novice-user
15443              (gnus-message 7 "`A k' to list killed groups"))))))
15444
15445 (defun gnus-subscribe-group (group previous &optional method)
15446   (gnus-group-change-level
15447    (if method
15448        (list t group gnus-level-default-subscribed nil nil method)
15449      group)
15450    gnus-level-default-subscribed gnus-level-killed previous t))
15451
15452 ;; `gnus-group-change-level' is the fundamental function for changing
15453 ;; subscription levels of newsgroups.  This might mean just changing
15454 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15455 ;; again, which subscribes/unsubscribes a group, which is equally
15456 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15457 ;; from 8-9 to 1-7 means that you remove the group from the list of
15458 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15459 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15460 ;; which is trivial.
15461 ;; ENTRY can either be a string (newsgroup name) or a list (if
15462 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15463 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15464 ;; entries.
15465 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15466 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15467 ;; after.
15468 (defun gnus-group-change-level (entry level &optional oldlevel
15469                                       previous fromkilled)
15470   (let (group info active num)
15471     ;; Glean what info we can from the arguments
15472     (if (consp entry)
15473         (if fromkilled (setq group (nth 1 entry))
15474           (setq group (car (nth 2 entry))))
15475       (setq group entry))
15476     (if (and (stringp entry)
15477              oldlevel
15478              (< oldlevel gnus-level-zombie))
15479         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15480     (if (and (not oldlevel)
15481              (consp entry))
15482         (setq oldlevel (gnus-info-level (nth 2 entry)))
15483       (setq oldlevel (or oldlevel 9)))
15484     (if (stringp previous)
15485         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15486
15487     (if (and (>= oldlevel gnus-level-zombie)
15488              (gnus-gethash group gnus-newsrc-hashtb))
15489         ;; We are trying to subscribe a group that is already
15490         ;; subscribed.
15491         ()                              ; Do nothing.
15492
15493       (or (gnus-ephemeral-group-p group)
15494           (gnus-dribble-enter
15495            (format "(gnus-group-change-level %S %S %S %S %S)"
15496                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15497
15498       ;; Then we remove the newgroup from any old structures, if needed.
15499       ;; If the group was killed, we remove it from the killed or zombie
15500       ;; list.  If not, and it is in fact going to be killed, we remove
15501       ;; it from the newsrc hash table and assoc.
15502       (cond
15503        ((>= oldlevel gnus-level-zombie)
15504         (if (= oldlevel gnus-level-zombie)
15505             (setq gnus-zombie-list (delete group gnus-zombie-list))
15506           (setq gnus-killed-list (delete group gnus-killed-list))))
15507        (t
15508         (if (and (>= level gnus-level-zombie)
15509                  entry)
15510             (progn
15511               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15512               (if (nth 3 entry)
15513                   (setcdr (gnus-gethash (car (nth 3 entry))
15514                                         gnus-newsrc-hashtb)
15515                           (cdr entry)))
15516               (setcdr (cdr entry) (cdddr entry))))))
15517
15518       ;; Finally we enter (if needed) the list where it is supposed to
15519       ;; go, and change the subscription level.  If it is to be killed,
15520       ;; we enter it into the killed or zombie list.
15521       (cond 
15522        ((>= level gnus-level-zombie)
15523         ;; Remove from the hash table.
15524         (gnus-sethash group nil gnus-newsrc-hashtb)
15525         ;; We do not enter foreign groups into the list of dead
15526         ;; groups.
15527         (unless (gnus-group-foreign-p group)
15528           (if (= level gnus-level-zombie)
15529               (setq gnus-zombie-list (cons group gnus-zombie-list))
15530             (setq gnus-killed-list (cons group gnus-killed-list)))))
15531        (t
15532         ;; If the list is to be entered into the newsrc assoc, and
15533         ;; it was killed, we have to create an entry in the newsrc
15534         ;; hashtb format and fix the pointers in the newsrc assoc.
15535         (if (< oldlevel gnus-level-zombie)
15536             ;; It was alive, and it is going to stay alive, so we
15537             ;; just change the level and don't change any pointers or
15538             ;; hash table entries.
15539             (setcar (cdaddr entry) level)
15540           (if (listp entry)
15541               (setq info (cdr entry)
15542                     num (car entry))
15543             (setq active (gnus-active group))
15544             (setq num
15545                   (if active (- (1+ (cdr active)) (car active)) t))
15546             ;; Check whether the group is foreign.  If so, the
15547             ;; foreign select method has to be entered into the
15548             ;; info.
15549             (let ((method (or gnus-override-subscribe-method
15550                               (gnus-group-method group))))
15551               (if (eq method gnus-select-method)
15552                   (setq info (list group level nil))
15553                 (setq info (list group level nil nil method)))))
15554           (unless previous
15555             (setq previous
15556                   (let ((p gnus-newsrc-alist))
15557                     (while (cddr p)
15558                       (setq p (cdr p)))
15559                     p)))
15560           (setq entry (cons info (cddr previous)))
15561           (if (cdr previous)
15562               (progn
15563                 (setcdr (cdr previous) entry)
15564                 (gnus-sethash group (cons num (cdr previous))
15565                               gnus-newsrc-hashtb))
15566             (setcdr previous entry)
15567             (gnus-sethash group (cons num previous)
15568                           gnus-newsrc-hashtb))
15569           (when (cdr entry)
15570             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15571       (when gnus-group-change-level-function
15572         (funcall gnus-group-change-level-function group level oldlevel)))))
15573
15574 (defun gnus-kill-newsgroup (newsgroup)
15575   "Obsolete function.  Kills a newsgroup."
15576   (gnus-group-change-level
15577    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15578
15579 (defun gnus-check-bogus-newsgroups (&optional confirm)
15580   "Remove bogus newsgroups.
15581 If CONFIRM is non-nil, the user has to confirm the deletion of every
15582 newsgroup."
15583   (let ((newsrc (cdr gnus-newsrc-alist))
15584         bogus group entry info)
15585     (gnus-message 5 "Checking bogus newsgroups...")
15586     (unless (gnus-read-active-file-p)
15587       (gnus-read-active-file))
15588     (when (gnus-read-active-file-p)
15589       ;; Find all bogus newsgroup that are subscribed.
15590       (while newsrc
15591         (setq info (pop newsrc)
15592               group (gnus-info-group info))
15593         (unless (or (gnus-active group) ; Active
15594                     (gnus-info-method info) ; Foreign
15595                     (and confirm
15596                          (not (gnus-y-or-n-p
15597                                (format "Remove bogus newsgroup: %s " group)))))
15598           ;; Found a bogus newsgroup.
15599           (push group bogus)))
15600       ;; Remove all bogus subscribed groups by first killing them, and
15601       ;; then removing them from the list of killed groups.
15602       (while bogus
15603         (when (setq entry (gnus-gethash (setq group (pop bogus))
15604                                         gnus-newsrc-hashtb))
15605           (gnus-group-change-level entry gnus-level-killed)
15606           (setq gnus-killed-list (delete group gnus-killed-list))))
15607       ;; Then we remove all bogus groups from the list of killed and
15608       ;; zombie groups.  They are are removed without confirmation.
15609       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15610             killed)
15611         (while dead-lists
15612           (setq killed (symbol-value (car dead-lists)))
15613           (while killed
15614             (unless (gnus-active (setq group (pop killed)))
15615               ;; The group is bogus.
15616               ;; !!!Slow as hell.
15617               (set (car dead-lists)
15618                    (delete group (symbol-value (car dead-lists))))))
15619           (setq dead-lists (cdr dead-lists))))
15620       (gnus-message 5 "Checking bogus newsgroups...done"))))
15621
15622 (defun gnus-check-duplicate-killed-groups ()
15623   "Remove duplicates from the list of killed groups."
15624   (interactive)
15625   (let ((killed gnus-killed-list))
15626     (while killed
15627       (gnus-message 9 "%d" (length killed))
15628       (setcdr killed (delete (car killed) (cdr killed)))
15629       (setq killed (cdr killed)))))
15630
15631 ;; We want to inline a function from gnus-cache, so we cheat here:
15632 (eval-when-compile
15633   (provide 'gnus)
15634   (require 'gnus-cache))
15635
15636 (defun gnus-get-unread-articles-in-group (info active &optional update)
15637   (when active
15638     ;; Allow the backend to update the info in the group.
15639     (when (and update 
15640                (gnus-request-update-info
15641                 info (gnus-find-method-for-group (gnus-info-group info))))
15642       (gnus-activate-group (gnus-info-group info) nil t))
15643     (let* ((range (gnus-info-read info))
15644            (num 0))
15645       ;; If a cache is present, we may have to alter the active info.
15646       (when (and gnus-use-cache info)
15647         (inline (gnus-cache-possibly-alter-active 
15648                  (gnus-info-group info) active)))
15649       ;; Modify the list of read articles according to what articles
15650       ;; are available; then tally the unread articles and add the
15651       ;; number to the group hash table entry.
15652       (cond
15653        ((zerop (cdr active))
15654         (setq num 0))
15655        ((not range)
15656         (setq num (- (1+ (cdr active)) (car active))))
15657        ((not (listp (cdr range)))
15658         ;; Fix a single (num . num) range according to the
15659         ;; active hash table.
15660         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15661         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15662         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15663         ;; Compute number of unread articles.
15664         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15665        (t
15666         ;; The read list is a list of ranges.  Fix them according to
15667         ;; the active hash table.
15668         ;; First peel off any elements that are below the lower
15669         ;; active limit.
15670         (while (and (cdr range)
15671                     (>= (car active)
15672                         (or (and (atom (cadr range)) (cadr range))
15673                             (caadr range))))
15674           (if (numberp (car range))
15675               (setcar range
15676                       (cons (car range)
15677                             (or (and (numberp (cadr range))
15678                                      (cadr range))
15679                                 (cdadr range))))
15680             (setcdr (car range)
15681                     (or (and (numberp (nth 1 range)) (nth 1 range))
15682                         (cdadr range))))
15683           (setcdr range (cddr range)))
15684         ;; Adjust the first element to be the same as the lower limit.
15685         (if (and (not (atom (car range)))
15686                  (< (cdar range) (car active)))
15687             (setcdr (car range) (1- (car active))))
15688         ;; Then we want to peel off any elements that are higher
15689         ;; than the upper active limit.
15690         (let ((srange range))
15691           ;; Go past all legal elements.
15692           (while (and (cdr srange)
15693                       (<= (or (and (atom (cadr srange))
15694                                    (cadr srange))
15695                               (caadr srange)) (cdr active)))
15696             (setq srange (cdr srange)))
15697           (if (cdr srange)
15698               ;; Nuke all remaining illegal elements.
15699               (setcdr srange nil))
15700
15701           ;; Adjust the final element.
15702           (if (and (not (atom (car srange)))
15703                    (> (cdar srange) (cdr active)))
15704               (setcdr (car srange) (cdr active))))
15705         ;; Compute the number of unread articles.
15706         (while range
15707           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15708                                       (cdar range)))
15709                               (or (and (atom (car range)) (car range))
15710                                   (caar range)))))
15711           (setq range (cdr range)))
15712         (setq num (max 0 (- (cdr active) num)))))
15713       ;; Set the number of unread articles.
15714       (when info
15715         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
15716       num)))
15717
15718 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15719 ;; and compute how many unread articles there are in each group.
15720 (defun gnus-get-unread-articles (&optional level)
15721   (let* ((newsrc (cdr gnus-newsrc-alist))
15722          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15723          (foreign-level
15724           (min
15725            (cond ((and gnus-activate-foreign-newsgroups
15726                        (not (numberp gnus-activate-foreign-newsgroups)))
15727                   (1+ gnus-level-subscribed))
15728                  ((numberp gnus-activate-foreign-newsgroups)
15729                   gnus-activate-foreign-newsgroups)
15730                  (t 0))
15731            level))
15732          info group active method)
15733     (gnus-message 5 "Checking new news...")
15734
15735     (while newsrc
15736       (setq active (gnus-active (setq group (gnus-info-group
15737                                              (setq info (pop newsrc))))))
15738
15739       ;; Check newsgroups.  If the user doesn't want to check them, or
15740       ;; they can't be checked (for instance, if the news server can't
15741       ;; be reached) we just set the number of unread articles in this
15742       ;; newsgroup to t.  This means that Gnus thinks that there are
15743       ;; unread articles, but it has no idea how many.
15744       (if (and (setq method (gnus-info-method info))
15745                (not (gnus-server-equal
15746                      gnus-select-method
15747                      (setq method (gnus-server-get-method nil method))))
15748                (not (gnus-secondary-method-p method)))
15749           ;; These groups are foreign.  Check the level.
15750           (when (<= (gnus-info-level info) foreign-level)
15751             (setq active (gnus-activate-group group 'scan))
15752             (unless (inline (gnus-virtual-group-p group))
15753               (inline (gnus-close-group group)))
15754             (when (fboundp (intern (concat (symbol-name (car method))
15755                                            "-request-update-info")))
15756               (inline (gnus-request-update-info info method))))
15757         ;; These groups are native or secondary.
15758         (when (and (<= (gnus-info-level info) level)
15759                    (not gnus-read-active-file))
15760           (setq active (gnus-activate-group group 'scan))
15761           (inline (gnus-close-group group))))
15762
15763       ;; Get the number of unread articles in the group.
15764       (if active
15765           (inline (gnus-get-unread-articles-in-group info active))
15766         ;; The group couldn't be reached, so we nix out the number of
15767         ;; unread articles and stuff.
15768         (gnus-set-active group nil)
15769         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
15770
15771     (gnus-message 5 "Checking new news...done")))
15772
15773 ;; Create a hash table out of the newsrc alist.  The `car's of the
15774 ;; alist elements are used as keys.
15775 (defun gnus-make-hashtable-from-newsrc-alist ()
15776   (let ((alist gnus-newsrc-alist)
15777         (ohashtb gnus-newsrc-hashtb)
15778         prev)
15779     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15780     (setq alist
15781           (setq prev (setq gnus-newsrc-alist
15782                            (if (equal (caar gnus-newsrc-alist)
15783                                       "dummy.group")
15784                                gnus-newsrc-alist
15785                              (cons (list "dummy.group" 0 nil) alist)))))
15786     (while alist
15787       (gnus-sethash
15788        (caar alist)
15789        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
15790              prev)
15791        gnus-newsrc-hashtb)
15792       (setq prev alist
15793             alist (cdr alist)))))
15794
15795 (defun gnus-make-hashtable-from-killed ()
15796   "Create a hash table from the killed and zombie lists."
15797   (let ((lists '(gnus-killed-list gnus-zombie-list))
15798         list)
15799     (setq gnus-killed-hashtb
15800           (gnus-make-hashtable
15801            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15802     (while (setq list (pop lists))
15803       (setq list (symbol-value list))
15804       (while list
15805         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
15806
15807 (defun gnus-activate-group (group &optional scan dont-check)
15808   ;; Check whether a group has been activated or not.
15809   ;; If SCAN, request a scan of that group as well.
15810   (let ((method (gnus-find-method-for-group group))
15811         active)
15812     (and (gnus-check-server method)
15813          ;; We escape all bugs and quit here to make it possible to
15814          ;; continue if a group is so out-there that it reports bugs
15815          ;; and stuff.
15816          (progn
15817            (and scan
15818                 (gnus-check-backend-function 'request-scan (car method))
15819                 (gnus-request-scan group method))
15820            t)
15821          (condition-case ()
15822              (gnus-request-group group dont-check)
15823         ;   (error nil)
15824            (quit nil))
15825          (save-excursion
15826            (set-buffer nntp-server-buffer)
15827            (goto-char (point-min))
15828            ;; Parse the result we got from `gnus-request-group'.
15829            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
15830                 (progn
15831                   (goto-char (match-beginning 1))
15832                   (gnus-set-active
15833                    group (setq active (cons (read (current-buffer))
15834                                             (read (current-buffer)))))
15835                   ;; Return the new active info.
15836                   active))))))
15837
15838 (defun gnus-update-read-articles (group unread)
15839   "Update the list of read and ticked articles in GROUP using the
15840 UNREAD and TICKED lists.
15841 Note: UNSELECTED has to be sorted over `<'.
15842 Returns whether the updating was successful."
15843   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
15844          (entry (gnus-gethash group gnus-newsrc-hashtb))
15845          (info (nth 2 entry))
15846          (prev 1)
15847          (unread (sort (copy-sequence unread) '<))
15848          read)
15849     (if (or (not info) (not active))
15850         ;; There is no info on this group if it was, in fact,
15851         ;; killed.  Gnus stores no information on killed groups, so
15852         ;; there's nothing to be done.
15853         ;; One could store the information somewhere temporarily,
15854         ;; perhaps...  Hmmm...
15855         ()
15856       ;; Remove any negative articles numbers.
15857       (while (and unread (< (car unread) 0))
15858         (setq unread (cdr unread)))
15859       ;; Remove any expired article numbers
15860       (while (and unread (< (car unread) (car active)))
15861         (setq unread (cdr unread)))
15862       ;; Compute the ranges of read articles by looking at the list of
15863       ;; unread articles.
15864       (while unread
15865         (if (/= (car unread) prev)
15866             (setq read (cons (if (= prev (1- (car unread))) prev
15867                                (cons prev (1- (car unread)))) read)))
15868         (setq prev (1+ (car unread)))
15869         (setq unread (cdr unread)))
15870       (when (<= prev (cdr active))
15871         (setq read (cons (cons prev (cdr active)) read)))
15872       ;; Enter this list into the group info.
15873       (gnus-info-set-read
15874        info (if (> (length read) 1) (nreverse read) read))
15875       ;; Set the number of unread articles in gnus-newsrc-hashtb.
15876       (gnus-get-unread-articles-in-group info (gnus-active group))
15877       t)))
15878
15879 (defun gnus-make-articles-unread (group articles)
15880   "Mark ARTICLES in GROUP as unread."
15881   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
15882                           (gnus-gethash (gnus-group-real-name group)
15883                                         gnus-newsrc-hashtb))))
15884          (ranges (gnus-info-read info))
15885          news article)
15886     (while articles
15887       (when (gnus-member-of-range
15888              (setq article (pop articles)) ranges)
15889         (setq news (cons article news))))
15890     (when news
15891       (gnus-info-set-read
15892        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
15893       (gnus-group-update-group group t))))
15894
15895 ;; Enter all dead groups into the hashtb.
15896 (defun gnus-update-active-hashtb-from-killed ()
15897   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
15898         (lists (list gnus-killed-list gnus-zombie-list))
15899         killed)
15900     (while lists
15901       (setq killed (car lists))
15902       (while killed
15903         (gnus-sethash (car killed) nil hashtb)
15904         (setq killed (cdr killed)))
15905       (setq lists (cdr lists)))))
15906
15907 (defun gnus-get-killed-groups ()
15908   "Go through the active hashtb and all all unknown groups as killed."
15909   ;; First make sure active file has been read.
15910   (unless (gnus-read-active-file-p)
15911     (let ((gnus-read-active-file t))
15912       (gnus-read-active-file)))
15913   (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
15914   ;; Go through all newsgroups that are known to Gnus - enlarge kill list.
15915   (mapatoms
15916    (lambda (sym)
15917      (let ((groups 0)
15918            (group (symbol-name sym)))
15919        (if (or (null group)
15920                (gnus-gethash group gnus-killed-hashtb)
15921                (gnus-gethash group gnus-newsrc-hashtb))
15922            ()
15923          (let ((do-sub (gnus-matches-options-n group)))
15924            (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
15925                ()
15926              (setq groups (1+ groups))
15927              (setq gnus-killed-list
15928                    (cons group gnus-killed-list))
15929              (gnus-sethash group group gnus-killed-hashtb))))))
15930    gnus-active-hashtb))
15931
15932 ;; Get the active file(s) from the backend(s).
15933 (defun gnus-read-active-file ()
15934   (gnus-group-set-mode-line)
15935   (let ((methods 
15936          (append
15937           (if (gnus-check-server gnus-select-method)
15938               ;; The native server is available.
15939               (cons gnus-select-method gnus-secondary-select-methods)
15940             ;; The native server is down, so we just do the
15941             ;; secondary ones.
15942             gnus-secondary-select-methods)
15943           ;; Also read from the archive server.
15944           (when gnus-message-archive-method
15945             (list "archive"))))
15946         list-type)
15947     (setq gnus-have-read-active-file nil)
15948     (save-excursion
15949       (set-buffer nntp-server-buffer)
15950       (while methods
15951         (let* ((method (if (stringp (car methods))
15952                            (gnus-server-get-method nil (car methods))
15953                          (car methods)))
15954                (where (nth 1 method))
15955                (mesg (format "Reading active file%s via %s..."
15956                              (if (and where (not (zerop (length where))))
15957                                  (concat " from " where) "")
15958                              (car method))))
15959           (gnus-message 5 mesg)
15960           (when (gnus-check-server method)
15961             ;; Request that the backend scan its incoming messages.
15962             (and (gnus-check-backend-function 'request-scan (car method))
15963                  (gnus-request-scan nil method))
15964             (cond
15965              ((and (eq gnus-read-active-file 'some)
15966                    (gnus-check-backend-function 'retrieve-groups (car method)))
15967               (let ((newsrc (cdr gnus-newsrc-alist))
15968                     (gmethod (gnus-server-get-method nil method))
15969                     groups info)
15970                 (while (setq info (pop newsrc))
15971                   (when (gnus-server-equal
15972                          (gnus-find-method-for-group 
15973                           (gnus-info-group info) info)
15974                          gmethod)
15975                     (push (gnus-group-real-name (gnus-info-group info)) 
15976                           groups)))
15977                 (when groups
15978                   (gnus-check-server method)
15979                   (setq list-type (gnus-retrieve-groups groups method))
15980                   (cond
15981                    ((not list-type)
15982                     (gnus-error
15983                      1.2 "Cannot read partial active file from %s server."
15984                      (car method)))
15985                    ((eq list-type 'active)
15986                     (gnus-active-to-gnus-format method gnus-active-hashtb))
15987                    (t
15988                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
15989              (t
15990               (if (not (gnus-request-list method))
15991                   (unless (equal method gnus-message-archive-method)
15992                     (gnus-error 1 "Cannot read active file from %s server."
15993                                 (car method)))
15994                 (gnus-active-to-gnus-format method gnus-active-hashtb)
15995                 ;; We mark this active file as read.
15996                 (push method gnus-have-read-active-file)
15997                 (gnus-message 5 "%sdone" mesg))))))
15998         (setq methods (cdr methods))))))
15999
16000 ;; Read an active file and place the results in `gnus-active-hashtb'.
16001 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
16002   (unless method
16003     (setq method gnus-select-method))
16004   (let ((cur (current-buffer))
16005         (hashtb (or hashtb
16006                     (if (and gnus-active-hashtb
16007                              (not (equal method gnus-select-method)))
16008                         gnus-active-hashtb
16009                       (setq gnus-active-hashtb
16010                             (if (equal method gnus-select-method)
16011                                 (gnus-make-hashtable
16012                                  (count-lines (point-min) (point-max)))
16013                               (gnus-make-hashtable 4096)))))))
16014     ;; Delete unnecessary lines.
16015     (goto-char (point-min))
16016     (while (search-forward "\nto." nil t)
16017       (delete-region (1+ (match-beginning 0))
16018                      (progn (forward-line 1) (point))))
16019     (or (string= gnus-ignored-newsgroups "")
16020         (progn
16021           (goto-char (point-min))
16022           (delete-matching-lines gnus-ignored-newsgroups)))
16023     ;; Make the group names readable as a lisp expression even if they
16024     ;; contain special characters.
16025     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
16026     (goto-char (point-max))
16027     (while (re-search-backward "[][';?()#]" nil t)
16028       (insert ?\\))
16029     ;; If these are groups from a foreign select method, we insert the
16030     ;; group prefix in front of the group names.
16031     (and method (not (gnus-server-equal
16032                       (gnus-server-get-method nil method)
16033                       (gnus-server-get-method nil gnus-select-method)))
16034          (let ((prefix (gnus-group-prefixed-name "" method)))
16035            (goto-char (point-min))
16036            (while (and (not (eobp))
16037                        (progn (insert prefix)
16038                               (zerop (forward-line 1)))))))
16039     ;; Store the active file in a hash table.
16040     (goto-char (point-min))
16041     (if (string-match "%[oO]" gnus-group-line-format)
16042         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
16043         ;; If we want information on moderated groups, we use this
16044         ;; loop...
16045         (let* ((mod-hashtb (make-vector 7 0))
16046                (m (intern "m" mod-hashtb))
16047                group max min)
16048           (while (not (eobp))
16049             (condition-case nil
16050                 (progn
16051                   (narrow-to-region (point) (gnus-point-at-eol))
16052                   (setq group (let ((obarray hashtb)) (read cur)))
16053                   (if (and (numberp (setq max (read cur)))
16054                            (numberp (setq min (read cur)))
16055                            (progn
16056                              (skip-chars-forward " \t")
16057                              (not
16058                               (or (= (following-char) ?=)
16059                                   (= (following-char) ?x)
16060                                   (= (following-char) ?j)))))
16061                       (set group (cons min max))
16062                     (set group nil))
16063                   ;; Enter moderated groups into a list.
16064                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
16065                       (setq gnus-moderated-list
16066                             (cons (symbol-name group) gnus-moderated-list))))
16067               (error
16068                (and group
16069                     (symbolp group)
16070                     (set group nil))))
16071             (widen)
16072             (forward-line 1)))
16073       ;; And if we do not care about moderation, we use this loop,
16074       ;; which is faster.
16075       (let (group max min)
16076         (while (not (eobp))
16077           (condition-case ()
16078               (progn
16079                 (narrow-to-region (point) (gnus-point-at-eol))
16080                 ;; group gets set to a symbol interned in the hash table
16081                 ;; (what a hack!!) - jwz
16082                 (setq group (let ((obarray hashtb)) (read cur)))
16083                 (if (and (numberp (setq max (read cur)))
16084                          (numberp (setq min (read cur)))
16085                          (progn
16086                            (skip-chars-forward " \t")
16087                            (not
16088                             (or (= (following-char) ?=)
16089                                 (= (following-char) ?x)
16090                                 (= (following-char) ?j)))))
16091                     (set group (cons min max))
16092                   (set group nil)))
16093             (error
16094              (progn
16095                (and group
16096                     (symbolp group)
16097                     (set group nil))
16098                (or ignore-errors
16099                    (gnus-message 3 "Warning - illegal active: %s"
16100                                  (buffer-substring
16101                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
16102           (widen)
16103           (forward-line 1))))))
16104
16105 (defun gnus-groups-to-gnus-format (method &optional hashtb)
16106   ;; Parse a "groups" active file.
16107   (let ((cur (current-buffer))
16108         (hashtb (or hashtb
16109                     (if (and method gnus-active-hashtb)
16110                         gnus-active-hashtb
16111                       (setq gnus-active-hashtb
16112                             (gnus-make-hashtable
16113                              (count-lines (point-min) (point-max)))))))
16114         (prefix (and method
16115                      (not (gnus-server-equal
16116                            (gnus-server-get-method nil method)
16117                            (gnus-server-get-method nil gnus-select-method)))
16118                      (gnus-group-prefixed-name "" method))))
16119
16120     (goto-char (point-min))
16121     ;; We split this into to separate loops, one with the prefix
16122     ;; and one without to speed the reading up somewhat.
16123     (if prefix
16124         (let (min max opoint group)
16125           (while (not (eobp))
16126             (condition-case ()
16127                 (progn
16128                   (read cur) (read cur)
16129                   (setq min (read cur)
16130                         max (read cur)
16131                         opoint (point))
16132                   (skip-chars-forward " \t")
16133                   (insert prefix)
16134                   (goto-char opoint)
16135                   (set (let ((obarray hashtb)) (read cur))
16136                        (cons min max)))
16137               (error (and group (symbolp group) (set group nil))))
16138             (forward-line 1)))
16139       (let (min max group)
16140         (while (not (eobp))
16141           (condition-case ()
16142               (if (= (following-char) ?2)
16143                   (progn
16144                     (read cur) (read cur)
16145                     (setq min (read cur)
16146                           max (read cur))
16147                     (set (setq group (let ((obarray hashtb)) (read cur)))
16148                          (cons min max))))
16149             (error (and group (symbolp group) (set group nil))))
16150           (forward-line 1))))))
16151
16152 (defun gnus-read-newsrc-file (&optional force)
16153   "Read startup file.
16154 If FORCE is non-nil, the .newsrc file is read."
16155   ;; Reset variables that might be defined in the .newsrc.eld file.
16156   (let ((variables gnus-variable-list))
16157     (while variables
16158       (set (car variables) nil)
16159       (setq variables (cdr variables))))
16160   (let* ((newsrc-file gnus-current-startup-file)
16161          (quick-file (concat newsrc-file ".el")))
16162     (save-excursion
16163       ;; We always load the .newsrc.eld file.  If always contains
16164       ;; much information that can not be gotten from the .newsrc
16165       ;; file (ticked articles, killed groups, foreign methods, etc.)
16166       (gnus-read-newsrc-el-file quick-file)
16167
16168       (if (and (file-exists-p gnus-current-startup-file)
16169                (or force
16170                    (and (file-newer-than-file-p newsrc-file quick-file)
16171                         (file-newer-than-file-p newsrc-file
16172                                                 (concat quick-file "d")))
16173                    (not gnus-newsrc-alist)))
16174           ;; We read the .newsrc file.  Note that if there if a
16175           ;; .newsrc.eld file exists, it has already been read, and
16176           ;; the `gnus-newsrc-hashtb' has been created.  While reading
16177           ;; the .newsrc file, Gnus will only use the information it
16178           ;; can find there for changing the data already read -
16179           ;; ie. reading the .newsrc file will not trash the data
16180           ;; already read (except for read articles).
16181           (save-excursion
16182             (gnus-message 5 "Reading %s..." newsrc-file)
16183             (set-buffer (find-file-noselect newsrc-file))
16184             (buffer-disable-undo (current-buffer))
16185             (gnus-newsrc-to-gnus-format)
16186             (kill-buffer (current-buffer))
16187             (gnus-message 5 "Reading %s...done" newsrc-file)))
16188
16189       ;; Read any slave files.
16190       (unless gnus-slave
16191         (gnus-master-read-slave-newsrc))
16192       
16193       ;; Convert old to new.
16194       (gnus-convert-old-newsrc))))
16195
16196 (defun gnus-continuum-version (version)
16197   "Return VERSION as a floating point number."
16198   (when (string-match "^\\([^ ]+\\)? ?Gnus v?\\([0-9.]+\\)$" version)
16199     (let* ((alpha (and (match-beginning 1) (match-string 1 version)))
16200            (number (match-string 2 version))
16201            major minor least)
16202       (string-match "\\([0-9]\\)\\.\\([0-9]+\\)\\.?\\([0-9]+\\)?" number)
16203       (setq major (string-to-number (match-string 1 number)))
16204       (setq minor (string-to-number (match-string 2 number)))
16205       (setq least (if (match-beginning 3)
16206                       (string-to-number (match-string 3 number))
16207                     0))
16208       (string-to-number
16209        (if (zerop major)
16210            (format "%s00%02d%02d"
16211                    (cond 
16212                     ((string= alpha "(ding)") "4.99")
16213                     ((string= alpha "September") "5.01")
16214                     ((string= alpha "Red") "5.03"))
16215                    minor least)
16216          (format "%d.%02d%20d" major minor least))))))
16217
16218 (defun gnus-convert-old-newsrc ()
16219   "Convert old newsrc into the new format, if needed."
16220   (let ((fcv (and gnus-newsrc-file-version
16221                   (gnus-continuum-version gnus-newsrc-file-version))))
16222     (cond
16223      ;; No .newsrc.eld file was loaded.
16224      ((null fcv) nil)
16225      ;; Gnus 5 .newsrc.eld was loaded.
16226      ((< fcv (gnus-continuum-version "September Gnus v0.1"))
16227       (gnus-convert-old-ticks)))))
16228
16229 (defun gnus-convert-old-ticks ()
16230   (let ((newsrc (cdr gnus-newsrc-alist))
16231         marks info dormant ticked)
16232     (while (setq info (pop newsrc))
16233       (when (setq marks (gnus-info-marks info))
16234         (setq dormant (cdr (assq 'dormant marks))
16235               ticked (cdr (assq 'tick marks)))
16236         (when (or dormant ticked)
16237           (gnus-info-set-read
16238            info
16239            (gnus-add-to-range
16240             (gnus-info-read info)
16241             (nconc (gnus-uncompress-range dormant)
16242                    (gnus-uncompress-range ticked)))))))))
16243
16244 (defun gnus-read-newsrc-el-file (file)
16245   (let ((ding-file (concat file "d")))
16246     ;; We always, always read the .eld file.
16247     (gnus-message 5 "Reading %s..." ding-file)
16248     (let (gnus-newsrc-assoc)
16249       (condition-case nil
16250           (load ding-file t t t)
16251         (error
16252          (gnus-error 1 "Error in %s" ding-file)))
16253       (when gnus-newsrc-assoc
16254         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
16255     (gnus-make-hashtable-from-newsrc-alist)
16256     (when (file-newer-than-file-p file ding-file)
16257       ;; Old format quick file
16258       (gnus-message 5 "Reading %s..." file)
16259       ;; The .el file is newer than the .eld file, so we read that one
16260       ;; as well.
16261       (gnus-read-old-newsrc-el-file file))))
16262
16263 ;; Parse the old-style quick startup file
16264 (defun gnus-read-old-newsrc-el-file (file)
16265   (let (newsrc killed marked group m info)
16266     (prog1
16267         (let ((gnus-killed-assoc nil)
16268               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
16269           (prog1
16270               (condition-case nil
16271                   (load file t t t)
16272                 (error nil))
16273             (setq newsrc gnus-newsrc-assoc
16274                   killed gnus-killed-assoc
16275                   marked gnus-marked-assoc)))
16276       (setq gnus-newsrc-alist nil)
16277       (while (setq info (gnus-get-info (setq group (pop newsrc))))
16278         (if info
16279             (progn
16280               (gnus-info-set-read info (cddr group))
16281               (gnus-info-set-level
16282                info (if (nth 1 group) gnus-level-default-subscribed
16283                       gnus-level-default-unsubscribed))
16284               (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
16285           (push (setq info
16286                       (list (car group)
16287                             (if (nth 1 group) gnus-level-default-subscribed
16288                               gnus-level-default-unsubscribed)
16289                             (cddr group)))
16290                 gnus-newsrc-alist))
16291         ;; Copy marks into info.
16292         (when (setq m (assoc (car group) marked))
16293           (unless (nthcdr 3 info)
16294             (nconc info (list nil)))
16295           (gnus-info-set-marks
16296            info (list (cons 'tick (gnus-compress-sequence 
16297                                    (sort (cdr m) '<) t))))))
16298       (setq newsrc killed)
16299       (while newsrc
16300         (setcar newsrc (caar newsrc))
16301         (setq newsrc (cdr newsrc)))
16302       (setq gnus-killed-list killed))
16303     ;; The .el file version of this variable does not begin with
16304     ;; "options", while the .eld version does, so we just add it if it
16305     ;; isn't there.
16306     (and
16307      gnus-newsrc-options
16308      (progn
16309        (and (not (string-match "^ *options" gnus-newsrc-options))
16310             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
16311        (and (not (string-match "\n$" gnus-newsrc-options))
16312             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
16313        ;; Finally, if we read some options lines, we parse them.
16314        (or (string= gnus-newsrc-options "")
16315            (gnus-newsrc-parse-options gnus-newsrc-options))))
16316
16317     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
16318     (gnus-make-hashtable-from-newsrc-alist)))
16319
16320 (defun gnus-make-newsrc-file (file)
16321   "Make server dependent file name by catenating FILE and server host name."
16322   (let* ((file (expand-file-name file nil))
16323          (real-file (concat file "-" (nth 1 gnus-select-method))))
16324     (if (or (file-exists-p real-file)
16325             (file-exists-p (concat real-file ".el"))
16326             (file-exists-p (concat real-file ".eld")))
16327         real-file file)))
16328
16329 (defun gnus-newsrc-to-gnus-format ()
16330   (setq gnus-newsrc-options "")
16331   (setq gnus-newsrc-options-n nil)
16332
16333   (or gnus-active-hashtb
16334       (setq gnus-active-hashtb (make-vector 4095 0)))
16335   (let ((buf (current-buffer))
16336         (already-read (> (length gnus-newsrc-alist) 1))
16337         group subscribed options-symbol newsrc Options-symbol
16338         symbol reads num1)
16339     (goto-char (point-min))
16340     ;; We intern the symbol `options' in the active hashtb so that we
16341     ;; can `eq' against it later.
16342     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
16343     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
16344
16345     (while (not (eobp))
16346       ;; We first read the first word on the line by narrowing and
16347       ;; then reading into `gnus-active-hashtb'.  Most groups will
16348       ;; already exist in that hashtb, so this will save some string
16349       ;; space.
16350       (narrow-to-region
16351        (point)
16352        (progn (skip-chars-forward "^ \t!:\n") (point)))
16353       (goto-char (point-min))
16354       (setq symbol
16355             (and (/= (point-min) (point-max))
16356                  (let ((obarray gnus-active-hashtb)) (read buf))))
16357       (widen)
16358       ;; Now, the symbol we have read is either `options' or a group
16359       ;; name.  If it is an options line, we just add it to a string.
16360       (cond
16361        ((or (eq symbol options-symbol)
16362             (eq symbol Options-symbol))
16363         (setq gnus-newsrc-options
16364               ;; This concating is quite inefficient, but since our
16365               ;; thorough studies show that approx 99.37% of all
16366               ;; .newsrc files only contain a single options line, we
16367               ;; don't give a damn, frankly, my dear.
16368               (concat gnus-newsrc-options
16369                       (buffer-substring
16370                        (gnus-point-at-bol)
16371                        ;; Options may continue on the next line.
16372                        (or (and (re-search-forward "^[^ \t]" nil 'move)
16373                                 (progn (beginning-of-line) (point)))
16374                            (point)))))
16375         (forward-line -1))
16376        (symbol
16377         ;; Group names can be just numbers.  
16378         (when (numberp symbol) 
16379           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
16380         (or (boundp symbol) (set symbol nil))
16381         ;; It was a group name.
16382         (setq subscribed (= (following-char) ?:)
16383               group (symbol-name symbol)
16384               reads nil)
16385         (if (eolp)
16386             ;; If the line ends here, this is clearly a buggy line, so
16387             ;; we put point a the beginning of line and let the cond
16388             ;; below do the error handling.
16389             (beginning-of-line)
16390           ;; We skip to the beginning of the ranges.
16391           (skip-chars-forward "!: \t"))
16392         ;; We are now at the beginning of the list of read articles.
16393         ;; We read them range by range.
16394         (while
16395             (cond
16396              ((looking-at "[0-9]+")
16397               ;; We narrow and read a number instead of buffer-substring/
16398               ;; string-to-int because it's faster.  narrow/widen is
16399               ;; faster than save-restriction/narrow, and save-restriction
16400               ;; produces a garbage object.
16401               (setq num1 (progn
16402                            (narrow-to-region (match-beginning 0) (match-end 0))
16403                            (read buf)))
16404               (widen)
16405               ;; If the next character is a dash, then this is a range.
16406               (if (= (following-char) ?-)
16407                   (progn
16408                     ;; We read the upper bound of the range.
16409                     (forward-char 1)
16410                     (if (not (looking-at "[0-9]+"))
16411                         ;; This is a buggy line, by we pretend that
16412                         ;; it's kinda OK.  Perhaps the user should be
16413                         ;; dinged?
16414                         (setq reads (cons num1 reads))
16415                       (setq reads
16416                             (cons
16417                              (cons num1
16418                                    (progn
16419                                      (narrow-to-region (match-beginning 0)
16420                                                        (match-end 0))
16421                                      (read buf)))
16422                              reads))
16423                       (widen)))
16424                 ;; It was just a simple number, so we add it to the
16425                 ;; list of ranges.
16426                 (setq reads (cons num1 reads)))
16427               ;; If the next char in ?\n, then we have reached the end
16428               ;; of the line and return nil.
16429               (/= (following-char) ?\n))
16430              ((= (following-char) ?\n)
16431               ;; End of line, so we end.
16432               nil)
16433              (t
16434               ;; Not numbers and not eol, so this might be a buggy
16435               ;; line...
16436               (or (eobp)
16437                   ;; If it was eob instead of ?\n, we allow it.
16438                   (progn
16439                     ;; The line was buggy.
16440                     (setq group nil)
16441                     (gnus-error 3.1 "Mangled line: %s"
16442                                 (buffer-substring (gnus-point-at-bol)
16443                                                   (gnus-point-at-eol)))))
16444               nil))
16445           ;; Skip past ", ".  Spaces are illegal in these ranges, but
16446           ;; we allow them, because it's a common mistake to put a
16447           ;; space after the comma.
16448           (skip-chars-forward ", "))
16449
16450         ;; We have already read .newsrc.eld, so we gently update the
16451         ;; data in the hash table with the information we have just
16452         ;; read.
16453         (when group
16454           (let ((info (gnus-get-info group))
16455                 level)
16456             (if info
16457                 ;; There is an entry for this file in the alist.
16458                 (progn
16459                   (gnus-info-set-read info (nreverse reads))
16460                   ;; We update the level very gently.  In fact, we
16461                   ;; only change it if there's been a status change
16462                   ;; from subscribed to unsubscribed, or vice versa.
16463                   (setq level (gnus-info-level info))
16464                   (cond ((and (<= level gnus-level-subscribed)
16465                               (not subscribed))
16466                          (setq level (if reads
16467                                          gnus-level-default-unsubscribed
16468                                        (1+ gnus-level-default-unsubscribed))))
16469                         ((and (> level gnus-level-subscribed) subscribed)
16470                          (setq level gnus-level-default-subscribed)))
16471                   (gnus-info-set-level info level))
16472               ;; This is a new group.
16473               (setq info (list group
16474                                (if subscribed
16475                                    gnus-level-default-subscribed
16476                                  (if reads
16477                                      (1+ gnus-level-subscribed)
16478                                    gnus-level-default-unsubscribed))
16479                                (nreverse reads))))
16480             (setq newsrc (cons info newsrc))))))
16481       (forward-line 1))
16482
16483     (setq newsrc (nreverse newsrc))
16484
16485     (if (not already-read)
16486         ()
16487       ;; We now have two newsrc lists - `newsrc', which is what we
16488       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16489       ;; what we've read from .newsrc.eld.  We have to merge these
16490       ;; lists.  We do this by "attaching" any (foreign) groups in the
16491       ;; gnus-newsrc-alist to the (native) group that precedes them.
16492       (let ((rc (cdr gnus-newsrc-alist))
16493             (prev gnus-newsrc-alist)
16494             entry mentry)
16495         (while rc
16496           (or (null (nth 4 (car rc)))   ; It's a native group.
16497               (assoc (caar rc) newsrc) ; It's already in the alist.
16498               (if (setq entry (assoc (caar prev) newsrc))
16499                   (setcdr (setq mentry (memq entry newsrc))
16500                           (cons (car rc) (cdr mentry)))
16501                 (setq newsrc (cons (car rc) newsrc))))
16502           (setq prev rc
16503                 rc (cdr rc)))))
16504
16505     (setq gnus-newsrc-alist newsrc)
16506     ;; We make the newsrc hashtb.
16507     (gnus-make-hashtable-from-newsrc-alist)
16508
16509     ;; Finally, if we read some options lines, we parse them.
16510     (or (string= gnus-newsrc-options "")
16511         (gnus-newsrc-parse-options gnus-newsrc-options))))
16512
16513 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16514 ;; The return value will be a list on the form
16515 ;; ((regexp1 . ignore)
16516 ;;  (regexp2 . subscribe)...)
16517 ;; When handling new newsgroups, groups that match a `ignore' regexp
16518 ;; will be ignored, and groups that match a `subscribe' regexp will be
16519 ;; subscribed.  A line like
16520 ;; options -n !all rec.all
16521 ;; will lead to a list that looks like
16522 ;; (("^rec\\..+" . subscribe)
16523 ;;  ("^.+" . ignore))
16524 ;; So all "rec.*" groups will be subscribed, while all the other
16525 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16526 ;; different from "options -n rec.all !all".
16527 (defun gnus-newsrc-parse-options (options)
16528   (let (out eol)
16529     (save-excursion
16530       (gnus-set-work-buffer)
16531       (insert (regexp-quote options))
16532       ;; First we treat all continuation lines.
16533       (goto-char (point-min))
16534       (while (re-search-forward "\n[ \t]+" nil t)
16535         (replace-match " " t t))
16536       ;; Then we transform all "all"s into ".+"s.
16537       (goto-char (point-min))
16538       (while (re-search-forward "\\ball\\b" nil t)
16539         (replace-match ".+" t t))
16540       (goto-char (point-min))
16541       ;; We remove all other options than the "-n" ones.
16542       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16543         (replace-match " ")
16544         (forward-char -1))
16545       (goto-char (point-min))
16546
16547       ;; We are only interested in "options -n" lines - we
16548       ;; ignore the other option lines.
16549       (while (re-search-forward "[ \t]-n" nil t)
16550         (setq eol
16551               (or (save-excursion
16552                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16553                          (- (point) 2)))
16554                   (gnus-point-at-eol)))
16555         ;; Search for all "words"...
16556         (while (re-search-forward "[^ \t,\n]+" eol t)
16557           (if (= (char-after (match-beginning 0)) ?!)
16558               ;; If the word begins with a bang (!), this is a "not"
16559               ;; spec.  We put this spec (minus the bang) and the
16560               ;; symbol `ignore' into the list.
16561               (setq out (cons (cons (concat
16562                                      "^" (buffer-substring
16563                                           (1+ (match-beginning 0))
16564                                           (match-end 0)))
16565                                     'ignore) out))
16566             ;; There was no bang, so this is a "yes" spec.
16567             (setq out (cons (cons (concat "^" (match-string 0))
16568                                   'subscribe) out)))))
16569
16570       (setq gnus-newsrc-options-n out))))
16571
16572 (defun gnus-save-newsrc-file (&optional force)
16573   "Save .newsrc file."
16574   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16575   ;; from the variable gnus-newsrc-alist.
16576   (when (and (or gnus-newsrc-alist gnus-killed-list)
16577              gnus-current-startup-file)
16578     (save-excursion
16579       (if (and (or gnus-use-dribble-file gnus-slave)
16580                (not force)
16581                (or (not gnus-dribble-buffer)
16582                    (not (buffer-name gnus-dribble-buffer))
16583                    (zerop (save-excursion
16584                             (set-buffer gnus-dribble-buffer)
16585                             (buffer-size)))))
16586           (gnus-message 4 "(No changes need to be saved)")
16587         (run-hooks 'gnus-save-newsrc-hook)
16588         (if gnus-slave
16589             (gnus-slave-save-newsrc)
16590           ;; Save .newsrc.
16591           (when gnus-save-newsrc-file
16592             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16593             (gnus-gnus-to-newsrc-format)
16594             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16595           ;; Save .newsrc.eld.
16596           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16597           (make-local-variable 'version-control)
16598           (setq version-control 'never)
16599           (setq buffer-file-name
16600                 (concat gnus-current-startup-file ".eld"))
16601           (gnus-add-current-to-buffer-list)
16602           (buffer-disable-undo (current-buffer))
16603           (erase-buffer)
16604           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16605           (gnus-gnus-to-quick-newsrc-format)
16606           (run-hooks 'gnus-save-quick-newsrc-hook)
16607           (save-buffer)
16608           (kill-buffer (current-buffer))
16609           (gnus-message
16610            5 "Saving %s.eld...done" gnus-current-startup-file))
16611         (gnus-dribble-delete-file)
16612         (gnus-group-set-mode-line)))))
16613
16614 (defun gnus-gnus-to-quick-newsrc-format ()
16615   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16616   (insert ";; Gnus startup file.\n")
16617   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16618   (insert ";; to read .newsrc.\n")
16619   (insert "(setq gnus-newsrc-file-version "
16620           (prin1-to-string gnus-version) ")\n")
16621   (let ((variables
16622          (if gnus-save-killed-list gnus-variable-list
16623            ;; Remove the `gnus-killed-list' from the list of variables
16624            ;; to be saved, if required.
16625            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16626         ;; Peel off the "dummy" group.
16627         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16628         variable)
16629     ;; Insert the variables into the file.
16630     (while variables
16631       (when (and (boundp (setq variable (pop variables)))
16632                  (symbol-value variable))
16633         (insert "(setq " (symbol-name variable) " '")
16634         (prin1 (symbol-value variable) (current-buffer))
16635         (insert ")\n")))))
16636
16637 (defun gnus-gnus-to-newsrc-format ()
16638   ;; Generate and save the .newsrc file.
16639   (save-excursion
16640     (set-buffer (create-file-buffer gnus-current-startup-file))
16641     (let ((newsrc (cdr gnus-newsrc-alist))
16642           (standard-output (current-buffer))
16643           info ranges range method)
16644       (setq buffer-file-name gnus-current-startup-file)
16645       (buffer-disable-undo (current-buffer))
16646       (erase-buffer)
16647       ;; Write options.
16648       (if gnus-newsrc-options (insert gnus-newsrc-options))
16649       ;; Write subscribed and unsubscribed.
16650       (while (setq info (pop newsrc))
16651         ;; Don't write foreign groups to .newsrc.
16652         (when (or (null (setq method (gnus-info-method info)))
16653                   (equal method "native")
16654                   (gnus-server-equal method gnus-select-method))
16655           (insert (gnus-info-group info)
16656                   (if (> (gnus-info-level info) gnus-level-subscribed)
16657                       "!" ":"))
16658           (when (setq ranges (gnus-info-read info))
16659             (insert " ")
16660             (if (not (listp (cdr ranges)))
16661                 (if (= (car ranges) (cdr ranges))
16662                     (princ (car ranges))
16663                   (princ (car ranges))
16664                   (insert "-")
16665                   (princ (cdr ranges)))
16666               (while (setq range (pop ranges))
16667                 (if (or (atom range) (= (car range) (cdr range)))
16668                     (princ (or (and (atom range) range) (car range)))
16669                   (princ (car range))
16670                   (insert "-")
16671                   (princ (cdr range)))
16672                 (if ranges (insert ",")))))
16673           (insert "\n")))
16674       (make-local-variable 'version-control)
16675       (setq version-control 'never)
16676       ;; It has been reported that sometime the modtime on the .newsrc
16677       ;; file seems to be off.  We really do want to overwrite it, so
16678       ;; we clear the modtime here before saving.  It's a bit odd,
16679       ;; though...
16680       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16681       ;; delete the silly thing entirely first.  but this fails to provide
16682       ;; such niceties as .newsrc~ creation.
16683       (if gnus-modtime-botch
16684           (delete-file gnus-startup-file)
16685         (clear-visited-file-modtime))
16686       (run-hooks 'gnus-save-standard-newsrc-hook)
16687       (save-buffer)
16688       (kill-buffer (current-buffer)))))
16689
16690 \f
16691 ;;;
16692 ;;; Slave functions.
16693 ;;;
16694
16695 (defun gnus-slave-save-newsrc ()
16696   (save-excursion
16697     (set-buffer gnus-dribble-buffer)
16698     (let ((slave-name
16699            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16700       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16701
16702 (defun gnus-master-read-slave-newsrc ()
16703   (let ((slave-files
16704          (directory-files
16705           (file-name-directory gnus-current-startup-file)
16706           t (concat
16707              "^" (regexp-quote
16708                   (concat
16709                    (file-name-nondirectory gnus-current-startup-file)
16710                    "-slave-")))
16711           t))
16712         file)
16713     (if (not slave-files)
16714         ()                              ; There are no slave files to read.
16715       (gnus-message 7 "Reading slave newsrcs...")
16716       (save-excursion
16717         (set-buffer (get-buffer-create " *gnus slave*"))
16718         (buffer-disable-undo (current-buffer))
16719         (setq slave-files
16720               (sort (mapcar (lambda (file)
16721                               (list (nth 5 (file-attributes file)) file))
16722                             slave-files)
16723                     (lambda (f1 f2)
16724                       (or (< (caar f1) (caar f2))
16725                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16726         (while slave-files
16727           (erase-buffer)
16728           (setq file (nth 1 (car slave-files)))
16729           (insert-file-contents file)
16730           (if (condition-case ()
16731                   (progn
16732                     (eval-buffer (current-buffer))
16733                     t)
16734                 (error
16735                  (gnus-error 3.2 "Possible error in %s" file)
16736                  nil))
16737               (or gnus-slave ; Slaves shouldn't delete these files.
16738                   (condition-case ()
16739                       (delete-file file)
16740                     (error nil))))
16741           (setq slave-files (cdr slave-files))))
16742       (gnus-message 7 "Reading slave newsrcs...done"))))
16743
16744 \f
16745 ;;;
16746 ;;; Group description.
16747 ;;;
16748
16749 (defun gnus-read-all-descriptions-files ()
16750   (let ((methods (cons gnus-select-method 
16751                        (nconc
16752                         (when gnus-message-archive-method
16753                           (list "archive"))
16754                         gnus-secondary-select-methods))))
16755     (while methods
16756       (gnus-read-descriptions-file (car methods))
16757       (setq methods (cdr methods)))
16758     t))
16759
16760 (defun gnus-read-descriptions-file (&optional method)
16761   (let ((method (or method gnus-select-method)))
16762     (when (stringp method)
16763       (setq method (gnus-server-to-method method)))
16764     ;; We create the hashtable whether we manage to read the desc file
16765     ;; to avoid trying to re-read after a failed read.
16766     (or gnus-description-hashtb
16767         (setq gnus-description-hashtb
16768               (gnus-make-hashtable (length gnus-active-hashtb))))
16769     ;; Mark this method's desc file as read.
16770     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16771                   gnus-description-hashtb)
16772
16773     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16774     (cond
16775      ((not (gnus-check-server method))
16776       (gnus-message 1 "Couldn't open server")
16777       nil)
16778      ((not (gnus-request-list-newsgroups method))
16779       (gnus-message 1 "Couldn't read newsgroups descriptions")
16780       nil)
16781      (t
16782       (let (group)
16783         (save-excursion
16784           (save-restriction
16785             (set-buffer nntp-server-buffer)
16786             (goto-char (point-min))
16787             (if (or (search-forward "\n.\n" nil t)
16788                     (goto-char (point-max)))
16789                 (progn
16790                   (beginning-of-line)
16791                   (narrow-to-region (point-min) (point))))
16792             (goto-char (point-min))
16793             (while (not (eobp))
16794               ;; If we get an error, we set group to 0, which is not a
16795               ;; symbol...
16796               (setq group
16797                     (condition-case ()
16798                         (let ((obarray gnus-description-hashtb))
16799                           ;; Group is set to a symbol interned in this
16800                           ;; hash table.
16801                           (read nntp-server-buffer))
16802                       (error 0)))
16803               (skip-chars-forward " \t")
16804               ;; ...  which leads to this line being effectively ignored.
16805               (and (symbolp group)
16806                    (set group (buffer-substring
16807                                (point) (progn (end-of-line) (point)))))
16808               (forward-line 1))))
16809         (gnus-message 5 "Reading descriptions file...done")
16810         t)))))
16811
16812 (defun gnus-group-get-description (group)
16813   "Get the description of a group by sending XGTITLE to the server."
16814   (when (gnus-request-group-description group)
16815     (save-excursion
16816       (set-buffer nntp-server-buffer)
16817       (goto-char (point-min))
16818       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
16819         (match-string 1)))))
16820
16821 \f
16822 ;;;
16823 ;;; Buffering of read articles.
16824 ;;;
16825
16826 (defvar gnus-backlog-buffer " *Gnus Backlog*")
16827 (defvar gnus-backlog-articles nil)
16828 (defvar gnus-backlog-hashtb nil)
16829
16830 (defun gnus-backlog-buffer ()
16831   "Return the backlog buffer."
16832   (or (get-buffer gnus-backlog-buffer)
16833       (save-excursion
16834         (set-buffer (get-buffer-create gnus-backlog-buffer))
16835         (buffer-disable-undo (current-buffer))
16836         (setq buffer-read-only t)
16837         (gnus-add-current-to-buffer-list)
16838         (get-buffer gnus-backlog-buffer))))
16839
16840 (defun gnus-backlog-setup ()
16841   "Initialize backlog variables."
16842   (unless gnus-backlog-hashtb
16843     (setq gnus-backlog-hashtb (make-vector 1023 0))))
16844
16845 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
16846
16847 (defun gnus-backlog-shutdown ()
16848   "Clear all backlog variables and buffers."
16849   (when (get-buffer gnus-backlog-buffer)
16850     (kill-buffer gnus-backlog-buffer))
16851   (setq gnus-backlog-hashtb nil
16852         gnus-backlog-articles nil))
16853
16854 (defun gnus-backlog-enter-article (group number buffer)
16855   (gnus-backlog-setup)
16856   (let ((ident (intern (concat group ":" (int-to-string number))
16857                        gnus-backlog-hashtb))
16858         b)
16859     (if (memq ident gnus-backlog-articles)
16860         () ; It's already kept.
16861       ;; Remove the oldest article, if necessary.
16862       (and (numberp gnus-keep-backlog)
16863            (>= (length gnus-backlog-articles) gnus-keep-backlog)
16864            (gnus-backlog-remove-oldest-article))
16865       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
16866       ;; Insert the new article.
16867       (save-excursion
16868         (set-buffer (gnus-backlog-buffer))
16869         (let (buffer-read-only)
16870           (goto-char (point-max))
16871           (or (bolp) (insert "\n"))
16872           (setq b (point))
16873           (insert-buffer-substring buffer)
16874           ;; Tag the beginning of the article with the ident.
16875           (gnus-put-text-property b (1+ b) 'gnus-backlog ident))))))
16876
16877 (defun gnus-backlog-remove-oldest-article ()
16878   (save-excursion
16879     (set-buffer (gnus-backlog-buffer))
16880     (goto-char (point-min))
16881     (if (zerop (buffer-size))
16882         () ; The buffer is empty.
16883       (let ((ident (get-text-property (point) 'gnus-backlog))
16884             buffer-read-only)
16885         ;; Remove the ident from the list of articles.
16886         (when ident
16887           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16888         ;; Delete the article itself.
16889         (delete-region
16890          (point) (next-single-property-change
16891                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
16892
16893 (defun gnus-backlog-remove-article (group number)
16894   "Remove article NUMBER in GROUP from the backlog."
16895   (when (numberp number)
16896     (gnus-backlog-setup)
16897     (let ((ident (intern (concat group ":" (int-to-string number))
16898                          gnus-backlog-hashtb))
16899           beg end)
16900       (when (memq ident gnus-backlog-articles)
16901         ;; It was in the backlog.
16902         (save-excursion
16903           (set-buffer (gnus-backlog-buffer))
16904           (let (buffer-read-only)
16905             (when (setq beg (text-property-any
16906                              (point-min) (point-max) 'gnus-backlog
16907                              ident))
16908               ;; Find the end (i. e., the beginning of the next article).
16909               (setq end
16910                     (next-single-property-change
16911                      (1+ beg) 'gnus-backlog (current-buffer) (point-max)))
16912               (delete-region beg end)
16913               ;; Return success.
16914               t)))))))
16915
16916 (defun gnus-backlog-request-article (group number buffer)
16917   (when (numberp number)
16918     (gnus-backlog-setup)
16919     (let ((ident (intern (concat group ":" (int-to-string number))
16920                          gnus-backlog-hashtb))
16921           beg end)
16922       (when (memq ident gnus-backlog-articles)
16923         ;; It was in the backlog.
16924         (save-excursion
16925           (set-buffer (gnus-backlog-buffer))
16926           (if (not (setq beg (text-property-any
16927                               (point-min) (point-max) 'gnus-backlog
16928                               ident)))
16929               ;; It wasn't in the backlog after all.
16930               (ignore
16931                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16932             ;; Find the end (i. e., the beginning of the next article).
16933             (setq end
16934                   (next-single-property-change
16935                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
16936         (let ((buffer-read-only nil))
16937           (erase-buffer)
16938           (insert-buffer-substring gnus-backlog-buffer beg end)
16939           t)))))
16940
16941 ;; Allow redefinition of Gnus functions.
16942
16943 (gnus-ems-redefine)
16944
16945 (provide 'gnus)
16946
16947 ;;; gnus.el ends here