*** 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     (faq . gnus-faq-buffer)
953     (picons . "*Picons*")
954     (tree . gnus-tree-buffer)
955     (info . gnus-info-buffer)
956     (article-copy . gnus-article-copy)
957     (draft . gnus-draft-buffer))
958   "Mapping from short symbols to buffer names or buffer variables.")
959
960 (defvar gnus-carpal nil
961   "*If non-nil, display clickable icons.")
962
963 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
964   "*Function called with a group name when new group is detected.
965 A few pre-made functions are supplied: `gnus-subscribe-randomly'
966 inserts new groups at the beginning of the list of groups;
967 `gnus-subscribe-alphabetically' inserts new groups in strict
968 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
969 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
970 for your decision; `gnus-subscribe-killed' kills all new groups.")
971
972 ;; Suggested by a bug report by Hallvard B Furuseth.
973 ;; <h.b.furuseth@usit.uio.no>.
974 (defvar gnus-subscribe-options-newsgroup-method
975   (function gnus-subscribe-alphabetically)
976   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
977 If, for instance, you want to subscribe to all newsgroups in the
978 \"no\" and \"alt\" hierarchies, you'd put the following in your
979 .newsrc file:
980
981 options -n no.all alt.all
982
983 Gnus will the subscribe all new newsgroups in these hierarchies with
984 the subscription method in this variable.")
985
986 (defvar gnus-subscribe-hierarchical-interactive nil
987   "*If non-nil, Gnus will offer to subscribe hierarchically.
988 When a new hierarchy appears, Gnus will ask the user:
989
990 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
991
992 If the user pressed `d', Gnus will descend the hierarchy, `y' will
993 subscribe to all newsgroups in the hierarchy and `s' will skip this
994 hierarchy in its entirety.")
995
996 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
997   "*Function used for sorting the group buffer.
998 This function will be called with group info entries as the arguments
999 for the groups to be sorted.  Pre-made functions include
1000 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread',
1001 `gnus-group-sort-by-level', `gnus-group-sort-by-score', and
1002 `gnus-group-sort-by-rank'.
1003
1004 This variable can also be a list of sorting functions.  In that case,
1005 the most significant sort function should be the last function in the
1006 list.")
1007
1008 ;; Mark variables suggested by Thomas Michanek
1009 ;; <Thomas.Michanek@telelogic.se>.
1010 (defvar gnus-unread-mark ? 
1011   "*Mark used for unread articles.")
1012 (defvar gnus-ticked-mark ?!
1013   "*Mark used for ticked articles.")
1014 (defvar gnus-dormant-mark ??
1015   "*Mark used for dormant articles.")
1016 (defvar gnus-del-mark ?r
1017   "*Mark used for del'd articles.")
1018 (defvar gnus-read-mark ?R
1019   "*Mark used for read articles.")
1020 (defvar gnus-expirable-mark ?E
1021   "*Mark used for expirable articles.")
1022 (defvar gnus-killed-mark ?K
1023   "*Mark used for killed articles.")
1024 (defvar gnus-souped-mark ?F
1025   "*Mark used for killed articles.")
1026 (defvar gnus-kill-file-mark ?X
1027   "*Mark used for articles killed by kill files.")
1028 (defvar gnus-low-score-mark ?Y
1029   "*Mark used for articles with a low score.")
1030 (defvar gnus-catchup-mark ?C
1031   "*Mark used for articles that are caught up.")
1032 (defvar gnus-replied-mark ?A
1033   "*Mark used for articles that have been replied to.")
1034 (defvar gnus-cached-mark ?*
1035   "*Mark used for articles that are in the cache.")
1036 (defvar gnus-saved-mark ?S
1037   "*Mark used for articles that have been saved to.")
1038 (defvar gnus-process-mark ?#
1039   "*Process mark.")
1040 (defvar gnus-ancient-mark ?O
1041   "*Mark used for ancient articles.")
1042 (defvar gnus-sparse-mark ?Q
1043   "*Mark used for sparsely reffed articles.")
1044 (defvar gnus-canceled-mark ?G
1045   "*Mark used for canceled articles.")
1046 (defvar gnus-score-over-mark ?+
1047   "*Score mark used for articles with high scores.")
1048 (defvar gnus-score-below-mark ?-
1049   "*Score mark used for articles with low scores.")
1050 (defvar gnus-empty-thread-mark ? 
1051   "*There is no thread under the article.")
1052 (defvar gnus-not-empty-thread-mark ?=
1053   "*There is a thread under the article.")
1054
1055 (defvar gnus-view-pseudo-asynchronously nil
1056   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
1057
1058 (defvar gnus-view-pseudos nil
1059   "*If `automatic', pseudo-articles will be viewed automatically.
1060 If `not-confirm', pseudos will be viewed automatically, and the user
1061 will not be asked to confirm the command.")
1062
1063 (defvar gnus-view-pseudos-separately t
1064   "*If non-nil, one pseudo-article will be created for each file to be viewed.
1065 If nil, all files that use the same viewing command will be given as a
1066 list of parameters to that command.")
1067
1068 (defvar gnus-insert-pseudo-articles t
1069   "*If non-nil, insert pseudo-articles when decoding articles.")
1070
1071 (defvar gnus-group-line-format "%M%S%p%P%5y: %(%g%)%l\n"
1072   "*Format of group lines.
1073 It works along the same lines as a normal formatting string,
1074 with some simple extensions.
1075
1076 %M    Only marked articles (character, \"*\" or \" \")
1077 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
1078 %L    Level of subscribedness (integer)
1079 %N    Number of unread articles (integer)
1080 %I    Number of dormant articles (integer)
1081 %i    Number of ticked and dormant (integer)
1082 %T    Number of ticked articles (integer)
1083 %R    Number of read articles (integer)
1084 %t    Total number of articles (integer)
1085 %y    Number of unread, unticked articles (integer)
1086 %G    Group name (string)
1087 %g    Qualified group name (string)
1088 %D    Group description (string)
1089 %s    Select method (string)
1090 %o    Moderated group (char, \"m\")
1091 %p    Process mark (char)
1092 %O    Moderated group (string, \"(m)\" or \"\")
1093 %P    Topic indentation (string)
1094 %l    Whether there are GroupLens predictions for this group (string)
1095 %n    Select from where (string)
1096 %z    A string that look like `<%s:%n>' if a foreign select method is used
1097 %u    User defined specifier.  The next character in the format string should
1098       be a letter.  Gnus will call the function gnus-user-format-function-X,
1099       where X is the letter following %u.  The function will be passed the
1100       current header as argument.  The function should return a string, which
1101       will be inserted into the buffer just like information from any other
1102       group specifier.
1103
1104 Text between %( and %) will be highlighted with `gnus-mouse-face' when
1105 the mouse point move inside the area.  There can only be one such area.
1106
1107 Note that this format specification is not always respected.  For
1108 reasons of efficiency, when listing killed groups, this specification
1109 is ignored altogether.  If the spec is changed considerably, your
1110 output may end up looking strange when listing both alive and killed
1111 groups.
1112
1113 If you use %o or %O, reading the active file will be slower and quite
1114 a bit of extra memory will be used. %D will also worsen performance.
1115 Also note that if you change the format specification to include any
1116 of these specs, you must probably re-start Gnus to see them go into
1117 effect.")
1118
1119 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
1120   "*The format specification of the lines in the summary buffer.
1121
1122 It works along the same lines as a normal formatting string,
1123 with some simple extensions.
1124
1125 %N   Article number, left padded with spaces (string)
1126 %S   Subject (string)
1127 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
1128 %n   Name of the poster (string)
1129 %a   Extracted name of the poster (string)
1130 %A   Extracted address of the poster (string)
1131 %F   Contents of the From: header (string)
1132 %x   Contents of the Xref: header (string)
1133 %D   Date of the article (string)
1134 %d   Date of the article (string) in DD-MMM format
1135 %M   Message-id of the article (string)
1136 %r   References of the article (string)
1137 %c   Number of characters in the article (integer)
1138 %L   Number of lines in the article (integer)
1139 %I   Indentation based on thread level (a string of spaces)
1140 %T   A string with two possible values: 80 spaces if the article
1141      is on thread level two or larger and 0 spaces on level one
1142 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
1143 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
1144 %[   Opening bracket (character, \"[\" or \"<\")
1145 %]   Closing bracket (character, \"]\" or \">\")
1146 %>   Spaces of length thread-level (string)
1147 %<   Spaces of length (- 20 thread-level) (string)
1148 %i   Article score (number)
1149 %z   Article zcore (character)
1150 %t   Number of articles under the current thread (number).
1151 %e   Whether the thread is empty or not (character).
1152 %l   GroupLens score (number)
1153 %u   User defined specifier.  The next character in the format string should
1154      be a letter.  Gnus will call the function gnus-user-format-function-X,
1155      where X is the letter following %u.  The function will be passed the
1156      current header as argument.  The function should return a string, which
1157      will be inserted into the summary just like information from any other
1158      summary specifier.
1159
1160 Text between %( and %) will be highlighted with `gnus-mouse-face'
1161 when the mouse point is placed inside the area.  There can only be one
1162 such area.
1163
1164 The %U (status), %R (replied) and %z (zcore) specs have to be handled
1165 with care.  For reasons of efficiency, Gnus will compute what column
1166 these characters will end up in, and \"hard-code\" that.  This means that
1167 it is illegal to have these specs after a variable-length spec.  Well,
1168 you might not be arrested, but your summary buffer will look strange,
1169 which is bad enough.
1170
1171 The smart choice is to have these specs as for to the left as
1172 possible.
1173
1174 This restriction may disappear in later versions of Gnus.")
1175
1176 (defvar gnus-summary-dummy-line-format
1177   "*  %(:                          :%) %S\n"
1178   "*The format specification for the dummy roots in the summary buffer.
1179 It works along the same lines as a normal formatting string,
1180 with some simple extensions.
1181
1182 %S  The subject")
1183
1184 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
1185   "*The format specification for the summary mode line.
1186 It works along the same lines as a normal formatting string,
1187 with some simple extensions:
1188
1189 %G  Group name
1190 %p  Unprefixed group name
1191 %A  Current article number
1192 %V  Gnus version
1193 %U  Number of unread articles in the group
1194 %e  Number of unselected articles in the group
1195 %Z  A string with unread/unselected article counts
1196 %g  Shortish group name
1197 %S  Subject of the current article
1198 %u  User-defined spec
1199 %s  Current score file name
1200 %d  Number of dormant articles
1201 %r  Number of articles that have been marked as read in this session
1202 %E  Number of articles expunged by the score files")
1203
1204 (defvar gnus-article-mode-line-format "Gnus: %%b %S"
1205   "*The format specification for the article mode line.
1206 See `gnus-summary-mode-line-format' for a closer description.")
1207
1208 (defvar gnus-group-mode-line-format "Gnus: %%b {%M:%S}"
1209   "*The format specification for the group mode line.
1210 It works along the same lines as a normal formatting string,
1211 with some simple extensions:
1212
1213 %S   The native news server.
1214 %M   The native select method.")
1215
1216 (defvar gnus-valid-select-methods
1217   '(("nntp" post address prompt-address)
1218     ("nnspool" post address)
1219     ("nnvirtual" post-mail virtual prompt-address)
1220     ("nnmbox" mail respool address)
1221     ("nnml" mail respool address)
1222     ("nnmh" mail respool address)
1223     ("nndir" post-mail prompt-address address)
1224     ("nneething" none address prompt-address)
1225     ("nndoc" none address prompt-address)
1226     ("nnbabyl" mail address respool)
1227     ("nnkiboze" post address virtual)
1228     ("nnsoup" post-mail address)
1229     ("nndraft" post-mail)
1230     ("nnfolder" mail respool address))
1231   "An alist of valid select methods.
1232 The first element of each list lists should be a string with the name
1233 of the select method.  The other elements may be be the category of
1234 this method (ie. `post', `mail', `none' or whatever) or other
1235 properties that this method has (like being respoolable).
1236 If you implement a new select method, all you should have to change is
1237 this variable.  I think.")
1238
1239 (defvar gnus-updated-mode-lines '(group article summary tree)
1240   "*List of buffers that should update their mode lines.
1241 The list may contain the symbols `group', `article' and `summary'.  If
1242 the corresponding symbol is present, Gnus will keep that mode line
1243 updated with information that may be pertinent.
1244 If this variable is nil, screen refresh may be quicker.")
1245
1246 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1247 (defvar gnus-mode-non-string-length nil
1248   "*Max length of mode-line non-string contents.
1249 If this is nil, Gnus will take space as is needed, leaving the rest
1250 of the modeline intact.")
1251
1252 ;see gnus-cus.el
1253 ;(defvar gnus-mouse-face 'highlight
1254 ;  "*Face used for mouse highlighting in Gnus.
1255 ;No mouse highlights will be done if `gnus-visual' is nil.")
1256
1257 (defvar gnus-summary-mark-below nil
1258   "*Mark all articles with a score below this variable as read.
1259 This variable is local to each summary buffer and usually set by the
1260 score file.")
1261
1262 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
1263   "*List of functions used for sorting articles in the summary buffer.
1264 This variable is only used when not using a threaded display.")
1265
1266 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1267   "*List of functions used for sorting threads in the summary buffer.
1268 By default, threads are sorted by article number.
1269
1270 Each function takes two threads and return non-nil if the first thread
1271 should be sorted before the other.  If you use more than one function,
1272 the primary sort function should be the last.  You should probably
1273 always include `gnus-thread-sort-by-number' in the list of sorting
1274 functions -- preferably first.
1275
1276 Ready-mady functions include `gnus-thread-sort-by-number',
1277 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1278 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1279 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1280
1281 (defvar gnus-thread-score-function '+
1282   "*Function used for calculating the total score of a thread.
1283
1284 The function is called with the scores of the article and each
1285 subthread and should then return the score of the thread.
1286
1287 Some functions you can use are `+', `max', or `min'.")
1288
1289 (defvar gnus-summary-expunge-below nil
1290   "All articles that have a score less than this variable will be expunged.")
1291
1292 (defvar gnus-thread-expunge-below nil
1293   "All threads that have a total score less than this variable will be expunged.
1294 See `gnus-thread-score-function' for en explanation of what a
1295 \"thread score\" is.")
1296
1297 (defvar gnus-auto-subscribed-groups
1298   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1299   "*All new groups that match this regexp will be subscribed automatically.
1300 Note that this variable only deals with new groups.  It has no effect
1301 whatsoever on old groups.")
1302
1303 (defvar gnus-options-subscribe nil
1304   "*All new groups matching this regexp will be subscribed unconditionally.
1305 Note that this variable deals only with new newsgroups.  This variable
1306 does not affect old newsgroups.")
1307
1308 (defvar gnus-options-not-subscribe nil
1309   "*All new groups matching this regexp will be ignored.
1310 Note that this variable deals only with new newsgroups.  This variable
1311 does not affect old (already subscribed) newsgroups.")
1312
1313 (defvar gnus-auto-expirable-newsgroups nil
1314   "*Groups in which to automatically mark read articles as expirable.
1315 If non-nil, this should be a regexp that should match all groups in
1316 which to perform auto-expiry.  This only makes sense for mail groups.")
1317
1318 (defvar gnus-total-expirable-newsgroups nil
1319   "*Groups in which to perform expiry of all read articles.
1320 Use with extreme caution.  All groups that match this regexp will be
1321 expiring - which means that all read articles will be deleted after
1322 (say) one week.  (This only goes for mail groups and the like, of
1323 course.)")
1324
1325 (defvar gnus-group-uncollapsed-levels 1
1326   "Number of group name elements to leave alone when making a short group name.")
1327
1328 (defvar gnus-hidden-properties '(invisible t intangible t)
1329   "Property list to use for hiding text.")
1330
1331 (defvar gnus-modtime-botch nil
1332   "*Non-nil means .newsrc should be deleted prior to save.  
1333 Its use is due to the bogus appearance that .newsrc was modified on
1334 disc.")
1335
1336 ;; Hooks.
1337
1338 (defvar gnus-group-mode-hook nil
1339   "*A hook for Gnus group mode.")
1340
1341 (defvar gnus-summary-mode-hook nil
1342   "*A hook for Gnus summary mode.
1343 This hook is run before any variables are set in the summary buffer.")
1344
1345 (defvar gnus-article-mode-hook nil
1346   "*A hook for Gnus article mode.")
1347
1348 (defvar gnus-summary-prepare-exit-hook nil
1349   "*A hook called when preparing to exit from the summary buffer.
1350 It calls `gnus-summary-expire-articles' by default.")
1351 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1352
1353 (defvar gnus-summary-exit-hook nil
1354   "*A hook called on exit from the summary buffer.")
1355
1356 (defvar gnus-group-catchup-group-hook nil
1357   "*A hook run when catching up a group from the group buffer.")
1358
1359 (defvar gnus-open-server-hook nil
1360   "*A hook called just before opening connection to the news server.")
1361
1362 (defvar gnus-load-hook nil
1363   "*A hook run while Gnus is loaded.")
1364
1365 (defvar gnus-startup-hook nil
1366   "*A hook called at startup.
1367 This hook is called after Gnus is connected to the NNTP server.")
1368
1369 (defvar gnus-get-new-news-hook nil
1370   "*A hook run just before Gnus checks for new news.")
1371
1372 (defvar gnus-after-getting-new-news-hook nil
1373   "*A hook run after Gnus checks for new news.")
1374
1375 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1376   "*A function that is called to generate the group buffer.
1377 The function is called with three arguments: The first is a number;
1378 all group with a level less or equal to that number should be listed,
1379 if the second is non-nil, empty groups should also be displayed.  If
1380 the third is non-nil, it is a number.  No groups with a level lower
1381 than this number should be displayed.
1382
1383 The only current function implemented is `gnus-group-prepare-flat'.")
1384
1385 (defvar gnus-group-prepare-hook nil
1386   "*A hook called after the group buffer has been generated.
1387 If you want to modify the group buffer, you can use this hook.")
1388
1389 (defvar gnus-summary-prepare-hook nil
1390   "*A hook called after the summary buffer has been generated.
1391 If you want to modify the summary buffer, you can use this hook.")
1392
1393 (defvar gnus-summary-generate-hook nil
1394   "*A hook run just before generating the summary buffer.
1395 This hook is commonly used to customize threading variables and the
1396 like.")
1397
1398 (defvar gnus-article-prepare-hook nil
1399   "*A hook called after an article has been prepared in the article buffer.
1400 If you want to run a special decoding program like nkf, use this hook.")
1401
1402 ;(defvar gnus-article-display-hook nil
1403 ;  "*A hook called after the article is displayed in the article buffer.
1404 ;The hook is designed to change the contents of the article
1405 ;buffer.  Typical functions that this hook may contain are
1406 ;`gnus-article-hide-headers' (hide selected headers),
1407 ;`gnus-article-maybe-highlight' (perform fancy article highlighting),
1408 ;`gnus-article-hide-signature' (hide signature) and
1409 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1410 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1411 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1412 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1413
1414 (defvar gnus-article-x-face-command
1415   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1416   "String or function to be executed to display an X-Face header.
1417 If it is a string, the command will be executed in a sub-shell
1418 asynchronously.  The compressed face will be piped to this command.")
1419
1420 (defvar gnus-article-x-face-too-ugly nil
1421   "Regexp matching posters whose face shouldn't be shown automatically.")
1422
1423 (defvar gnus-select-group-hook nil
1424   "*A hook called when a newsgroup is selected.
1425
1426 If you'd like to simplify subjects like the
1427 `gnus-summary-next-same-subject' command does, you can use the
1428 following hook:
1429
1430  (setq gnus-select-group-hook
1431       (list
1432         (lambda ()
1433           (mapcar (lambda (header)
1434                      (mail-header-set-subject
1435                       header
1436                       (gnus-simplify-subject
1437                        (mail-header-subject header) 're-only)))
1438                   gnus-newsgroup-headers))))")
1439
1440 (defvar gnus-select-article-hook nil
1441   "*A hook called when an article is selected.")
1442
1443 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1444   "*A hook called to apply kill files to a group.
1445 This hook is intended to apply a kill file to the selected newsgroup.
1446 The function `gnus-apply-kill-file' is called by default.
1447
1448 Since a general kill file is too heavy to use only for a few
1449 newsgroups, I recommend you to use a lighter hook function.  For
1450 example, if you'd like to apply a kill file to articles which contains
1451 a string `rmgroup' in subject in newsgroup `control', you can use the
1452 following hook:
1453
1454  (setq gnus-apply-kill-hook
1455       (list
1456         (lambda ()
1457           (cond ((string-match \"control\" gnus-newsgroup-name)
1458                  (gnus-kill \"Subject\" \"rmgroup\")
1459                  (gnus-expunge \"X\"))))))")
1460
1461 (defvar gnus-visual-mark-article-hook
1462   (list 'gnus-highlight-selected-summary)
1463   "*Hook run after selecting an article in the summary buffer.
1464 It is meant to be used for highlighting the article in some way.  It
1465 is not run if `gnus-visual' is nil.")
1466
1467 (defvar gnus-parse-headers-hook nil
1468   "*A hook called before parsing the headers.")
1469
1470 (defvar gnus-exit-group-hook nil
1471   "*A hook called when exiting (not quitting) summary mode.")
1472
1473 (defvar gnus-suspend-gnus-hook nil
1474   "*A hook called when suspending (not exiting) Gnus.")
1475
1476 (defvar gnus-exit-gnus-hook nil
1477   "*A hook called when exiting Gnus.")
1478
1479 (defvar gnus-after-exiting-gnus-hook nil
1480   "*A hook called after exiting Gnus.")
1481
1482 (defvar gnus-save-newsrc-hook nil
1483   "*A hook called before saving any of the newsrc files.")
1484
1485 (defvar gnus-save-quick-newsrc-hook nil
1486   "*A hook called just before saving the quick newsrc file.
1487 Can be used to turn version control on or off.")
1488
1489 (defvar gnus-save-standard-newsrc-hook nil
1490   "*A hook called just before saving the standard newsrc file.
1491 Can be used to turn version control on or off.")
1492
1493 (defvar gnus-summary-update-hook
1494   (list 'gnus-summary-highlight-line)
1495   "*A hook called when a summary line is changed.
1496 The hook will not be called if `gnus-visual' is nil.
1497
1498 The default function `gnus-summary-highlight-line' will
1499 highlight the line according to the `gnus-summary-highlight'
1500 variable.")
1501
1502 (defvar gnus-group-update-hook '(gnus-group-highlight-line)
1503   "*A hook called when a group line is changed.
1504 The hook will not be called if `gnus-visual' is nil.
1505
1506 The default function `gnus-group-highlight-line' will
1507 highlight the line according to the `gnus-group-highlight'
1508 variable.")
1509
1510 (defvar gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1511   "*A hook called when an article is selected for the first time.
1512 The hook is intended to mark an article as read (or unread)
1513 automatically when it is selected.")
1514
1515 (defvar gnus-group-change-level-function nil
1516   "Function run when a group level is changed.
1517 It is called with three parameters -- GROUP, LEVEL and OLDLEVEL.")
1518
1519 ;; Remove any hilit infestation.
1520 (add-hook 'gnus-startup-hook
1521           (lambda ()
1522             (remove-hook 'gnus-summary-prepare-hook
1523                          'hilit-rehighlight-buffer-quietly)
1524             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1525             (setq gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read))
1526             (remove-hook 'gnus-article-prepare-hook
1527                          'hilit-rehighlight-buffer-quietly)))
1528
1529 \f
1530 ;; Internal variables
1531
1532 (defvar gnus-server-alist nil
1533   "List of available servers.")
1534
1535 (defvar gnus-group-indentation-function nil)
1536
1537 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1538
1539 (defvar gnus-goto-missing-group-function nil)
1540
1541 (defvar gnus-override-subscribe-method nil)
1542
1543 (defvar gnus-group-goto-next-group-function nil
1544   "Function to override finding the next group after listing groups.")
1545
1546 (defconst gnus-article-mark-lists
1547   '((marked . tick) (replied . reply)
1548     (expirable . expire) (killed . killed)
1549     (bookmarks . bookmark) (dormant . dormant)
1550     (scored . score) (saved . save)
1551     (cached . cache)
1552     ))
1553
1554 ;; Avoid highlighting in kill files.
1555 (defvar gnus-summary-inhibit-highlight nil)
1556 (defvar gnus-newsgroup-selected-overlay nil)
1557
1558 (defvar gnus-inhibit-hiding nil)
1559 (defvar gnus-group-indentation "")
1560 (defvar gnus-inhibit-limiting nil)
1561 (defvar gnus-created-frames nil)
1562
1563 (defvar gnus-article-mode-map nil)
1564 (defvar gnus-dribble-buffer nil)
1565 (defvar gnus-headers-retrieved-by nil)
1566 (defvar gnus-article-reply nil)
1567 (defvar gnus-override-method nil)
1568 (defvar gnus-article-check-size nil)
1569
1570 (defvar gnus-current-score-file nil)
1571 (defvar gnus-newsgroup-adaptive-score-file nil)
1572 (defvar gnus-scores-exclude-files nil)
1573
1574 (defvar gnus-opened-servers nil)
1575
1576 (defvar gnus-current-move-group nil)
1577
1578 (defvar gnus-newsgroup-dependencies nil)
1579 (defvar gnus-newsgroup-async nil)
1580 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1581
1582 (defvar gnus-newsgroup-adaptive nil)
1583
1584 (defvar gnus-summary-display-table nil)
1585 (defvar gnus-summary-display-article-function nil)
1586
1587 (defvar gnus-summary-highlight-line-function nil
1588   "Function called after highlighting a summary line.")
1589
1590 (defvar gnus-group-line-format-alist
1591   `((?M gnus-tmp-marked-mark ?c)
1592     (?S gnus-tmp-subscribed ?c)
1593     (?L gnus-tmp-level ?d)
1594     (?N (cond ((eq number t) "*" )
1595               ((numberp number) 
1596                (int-to-string
1597                 (+ number
1598                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1599                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1600               (t number)) ?s)
1601     (?R gnus-tmp-number-of-read ?s)
1602     (?t gnus-tmp-number-total ?d)
1603     (?y gnus-tmp-number-of-unread ?s)
1604     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1605     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1606     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1607            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1608     (?g gnus-tmp-group ?s)
1609     (?G gnus-tmp-qualified-group ?s)
1610     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1611     (?D gnus-tmp-newsgroup-description ?s)
1612     (?o gnus-tmp-moderated ?c)
1613     (?O gnus-tmp-moderated-string ?s)
1614     (?p gnus-tmp-process-marked ?c)
1615     (?s gnus-tmp-news-server ?s)
1616     (?n gnus-tmp-news-method ?s)
1617     (?P gnus-group-indentation ?s)
1618     (?l gnus-tmp-grouplens ?s)
1619     (?z gnus-tmp-news-method-string ?s)
1620     (?u gnus-tmp-user-defined ?s)))
1621
1622 (defvar gnus-summary-line-format-alist
1623   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1624     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1625     (?s gnus-tmp-subject-or-nil ?s)
1626     (?n gnus-tmp-name ?s)
1627     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1628         ?s)
1629     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1630             gnus-tmp-from) ?s)
1631     (?F gnus-tmp-from ?s)
1632     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1633     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1634     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1635     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1636     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1637     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1638     (?L gnus-tmp-lines ?d)
1639     (?I gnus-tmp-indentation ?s)
1640     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1641     (?R gnus-tmp-replied ?c)
1642     (?\[ gnus-tmp-opening-bracket ?c)
1643     (?\] gnus-tmp-closing-bracket ?c)
1644     (?\> (make-string gnus-tmp-level ? ) ?s)
1645     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1646     (?i gnus-tmp-score ?d)
1647     (?z gnus-tmp-score-char ?c)
1648     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1649     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1650     (?U gnus-tmp-unread ?c)
1651     (?t (gnus-summary-number-of-articles-in-thread
1652          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1653         ?d)
1654     (?e (gnus-summary-number-of-articles-in-thread
1655          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1656         ?c)
1657     (?u gnus-tmp-user-defined ?s))
1658   "An alist of format specifications that can appear in summary lines,
1659 and what variables they correspond with, along with the type of the
1660 variable (string, integer, character, etc).")
1661
1662 (defvar gnus-summary-dummy-line-format-alist
1663   `((?S gnus-tmp-subject ?s)
1664     (?N gnus-tmp-number ?d)
1665     (?u gnus-tmp-user-defined ?s)))
1666
1667 (defvar gnus-summary-mode-line-format-alist
1668   `((?G gnus-tmp-group-name ?s)
1669     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1670     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1671     (?A gnus-tmp-article-number ?d)
1672     (?Z gnus-tmp-unread-and-unselected ?s)
1673     (?V gnus-version ?s)
1674     (?U gnus-tmp-unread ?d)
1675     (?S gnus-tmp-subject ?s)
1676     (?e gnus-tmp-unselected ?d)
1677     (?u gnus-tmp-user-defined ?s)
1678     (?d (length gnus-newsgroup-dormant) ?d)
1679     (?t (length gnus-newsgroup-marked) ?d)
1680     (?r (length gnus-newsgroup-reads) ?d)
1681     (?E gnus-newsgroup-expunged-tally ?d)
1682     (?s (gnus-current-score-file-nondirectory) ?s)))
1683
1684 (defvar gnus-article-mode-line-format-alist
1685   gnus-summary-mode-line-format-alist)
1686
1687 (defvar gnus-group-mode-line-format-alist
1688   `((?S gnus-tmp-news-server ?s)
1689     (?M gnus-tmp-news-method ?s)
1690     (?u gnus-tmp-user-defined ?s)))
1691
1692 (defvar gnus-have-read-active-file nil)
1693
1694 (defconst gnus-maintainer
1695   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1696   "The mail address of the Gnus maintainers.")
1697
1698 (defconst gnus-version "September Gnus v0.65"
1699   "Version number for this version of Gnus.")
1700
1701 (defvar gnus-info-nodes
1702   '((gnus-group-mode            "(gnus)The Group Buffer")
1703     (gnus-summary-mode          "(gnus)The Summary Buffer")
1704     (gnus-article-mode          "(gnus)The Article Buffer"))
1705   "Assoc list of major modes and related Info nodes.")
1706
1707 (defvar gnus-group-buffer "*Group*")
1708 (defvar gnus-summary-buffer "*Summary*")
1709 (defvar gnus-article-buffer "*Article*")
1710 (defvar gnus-server-buffer "*Server*")
1711
1712 (defvar gnus-work-buffer " *gnus work*")
1713
1714 (defvar gnus-original-article-buffer " *Original Article*")
1715 (defvar gnus-original-article nil)
1716
1717 (defvar gnus-buffer-list nil
1718   "Gnus buffers that should be killed on exit.")
1719
1720 (defvar gnus-slave nil
1721   "Whether this Gnus is a slave or not.")
1722
1723 (defvar gnus-variable-list
1724   '(gnus-newsrc-options gnus-newsrc-options-n
1725     gnus-newsrc-last-checked-date
1726     gnus-newsrc-alist gnus-server-alist
1727     gnus-killed-list gnus-zombie-list
1728     gnus-topic-topology gnus-topic-alist
1729     gnus-format-specs)
1730   "Gnus variables saved in the quick startup file.")
1731
1732 (defvar gnus-newsrc-options nil
1733   "Options line in the .newsrc file.")
1734
1735 (defvar gnus-newsrc-options-n nil
1736   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1737
1738 (defvar gnus-newsrc-last-checked-date nil
1739   "Date Gnus last asked server for new newsgroups.")
1740
1741 (defvar gnus-topic-topology nil
1742   "The complete topic hierarchy.")
1743
1744 (defvar gnus-topic-alist nil
1745   "The complete topic-group alist.")
1746
1747 (defvar gnus-newsrc-alist nil
1748   "Assoc list of read articles.
1749 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1750
1751 (defvar gnus-newsrc-hashtb nil
1752   "Hashtable of gnus-newsrc-alist.")
1753
1754 (defvar gnus-killed-list nil
1755   "List of killed newsgroups.")
1756
1757 (defvar gnus-killed-hashtb nil
1758   "Hash table equivalent of gnus-killed-list.")
1759
1760 (defvar gnus-zombie-list nil
1761   "List of almost dead newsgroups.")
1762
1763 (defvar gnus-description-hashtb nil
1764   "Descriptions of newsgroups.")
1765
1766 (defvar gnus-list-of-killed-groups nil
1767   "List of newsgroups that have recently been killed by the user.")
1768
1769 (defvar gnus-active-hashtb nil
1770   "Hashtable of active articles.")
1771
1772 (defvar gnus-moderated-list nil
1773   "List of moderated newsgroups.")
1774
1775 (defvar gnus-group-marked nil)
1776
1777 (defvar gnus-current-startup-file nil
1778   "Startup file for the current host.")
1779
1780 (defvar gnus-last-search-regexp nil
1781   "Default regexp for article search command.")
1782
1783 (defvar gnus-last-shell-command nil
1784   "Default shell command on article.")
1785
1786 (defvar gnus-current-select-method nil
1787   "The current method for selecting a newsgroup.")
1788
1789 (defvar gnus-group-list-mode nil)
1790
1791 (defvar gnus-article-internal-prepare-hook nil)
1792
1793 (defvar gnus-newsgroup-name nil)
1794 (defvar gnus-newsgroup-begin nil)
1795 (defvar gnus-newsgroup-end nil)
1796 (defvar gnus-newsgroup-last-rmail nil)
1797 (defvar gnus-newsgroup-last-mail nil)
1798 (defvar gnus-newsgroup-last-folder nil)
1799 (defvar gnus-newsgroup-last-file nil)
1800 (defvar gnus-newsgroup-auto-expire nil)
1801 (defvar gnus-newsgroup-active nil)
1802
1803 (defvar gnus-newsgroup-data nil)
1804 (defvar gnus-newsgroup-data-reverse nil)
1805 (defvar gnus-newsgroup-limit nil)
1806 (defvar gnus-newsgroup-limits nil)
1807
1808 (defvar gnus-newsgroup-unreads nil
1809   "List of unread articles in the current newsgroup.")
1810
1811 (defvar gnus-newsgroup-unselected nil
1812   "List of unselected unread articles in the current newsgroup.")
1813
1814 (defvar gnus-newsgroup-reads nil
1815   "Alist of read articles and article marks in the current newsgroup.")
1816
1817 (defvar gnus-newsgroup-expunged-tally nil)
1818
1819 (defvar gnus-newsgroup-marked nil
1820   "List of ticked articles in the current newsgroup (a subset of unread art).")
1821
1822 (defvar gnus-newsgroup-killed nil
1823   "List of ranges of articles that have been through the scoring process.")
1824
1825 (defvar gnus-newsgroup-cached nil
1826   "List of articles that come from the article cache.")
1827
1828 (defvar gnus-newsgroup-saved nil
1829   "List of articles that have been saved.")
1830
1831 (defvar gnus-newsgroup-kill-headers nil)
1832
1833 (defvar gnus-newsgroup-replied nil
1834   "List of articles that have been replied to in the current newsgroup.")
1835
1836 (defvar gnus-newsgroup-expirable nil
1837   "List of articles in the current newsgroup that can be expired.")
1838
1839 (defvar gnus-newsgroup-processable nil
1840   "List of articles in the current newsgroup that can be processed.")
1841
1842 (defvar gnus-newsgroup-bookmarks nil
1843   "List of articles in the current newsgroup that have bookmarks.")
1844
1845 (defvar gnus-newsgroup-dormant nil
1846   "List of dormant articles in the current newsgroup.")
1847
1848 (defvar gnus-newsgroup-scored nil
1849   "List of scored articles in the current newsgroup.")
1850
1851 (defvar gnus-newsgroup-headers nil
1852   "List of article headers in the current newsgroup.")
1853
1854 (defvar gnus-newsgroup-threads nil)
1855
1856 (defvar gnus-newsgroup-prepared nil
1857   "Whether the current group has been prepared properly.")
1858
1859 (defvar gnus-newsgroup-ancient nil
1860   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1861
1862 (defvar gnus-newsgroup-sparse nil)
1863
1864 (defvar gnus-current-article nil)
1865 (defvar gnus-article-current nil)
1866 (defvar gnus-current-headers nil)
1867 (defvar gnus-have-all-headers nil)
1868 (defvar gnus-last-article nil)
1869 (defvar gnus-newsgroup-history nil)
1870 (defvar gnus-current-kill-article nil)
1871
1872 ;; Save window configuration.
1873 (defvar gnus-prev-winconf nil)
1874
1875 (defvar gnus-summary-mark-positions nil)
1876 (defvar gnus-group-mark-positions nil)
1877
1878 (defvar gnus-reffed-article-number nil)
1879
1880 ;;; Let the byte-compiler know that we know about this variable.
1881 (defvar rmail-default-rmail-file)
1882
1883 (defvar gnus-cache-removable-articles nil)
1884
1885 (defvar gnus-dead-summary nil)
1886
1887 (defconst gnus-summary-local-variables
1888   '(gnus-newsgroup-name
1889     gnus-newsgroup-begin gnus-newsgroup-end
1890     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1891     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1892     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1893     gnus-newsgroup-unselected gnus-newsgroup-marked
1894     gnus-newsgroup-reads gnus-newsgroup-saved
1895     gnus-newsgroup-replied gnus-newsgroup-expirable
1896     gnus-newsgroup-processable gnus-newsgroup-killed
1897     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1898     gnus-newsgroup-headers gnus-newsgroup-threads
1899     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1900     gnus-current-article gnus-current-headers gnus-have-all-headers
1901     gnus-last-article gnus-article-internal-prepare-hook
1902     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1903     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1904     gnus-newsgroup-async 
1905     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1906     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1907     gnus-newsgroup-history gnus-newsgroup-ancient
1908     gnus-newsgroup-sparse
1909     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1910     gnus-newsgroup-adaptive-score-file
1911     (gnus-newsgroup-expunged-tally . 0)
1912     gnus-cache-removable-articles gnus-newsgroup-cached
1913     gnus-newsgroup-data gnus-newsgroup-data-reverse
1914     gnus-newsgroup-limit gnus-newsgroup-limits)
1915   "Variables that are buffer-local to the summary buffers.")
1916
1917 (defconst gnus-bug-message
1918   "Sending a bug report to the Gnus Towers.
1919 ========================================
1920
1921 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1922 be sent to the Gnus Bug Exterminators.
1923
1924 At the bottom of the buffer you'll see lots of variable settings.
1925 Please do not delete those.  They will tell the Bug People what your
1926 environment is, so that it will be easier to locate the bugs.
1927
1928 If you have found a bug that makes Emacs go \"beep\", set
1929 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1930 and include the backtrace in your bug report.
1931
1932 Please describe the bug in annoying, painstaking detail.
1933
1934 Thank you for your help in stamping out bugs.
1935 ")
1936
1937 ;;; End of variables.
1938
1939 ;; Define some autoload functions Gnus might use.
1940 (eval-and-compile
1941
1942   ;; This little mapcar goes through the list below and marks the
1943   ;; symbols in question as autoloaded functions.
1944   (mapcar
1945    (lambda (package)
1946      (let ((interactive (nth 1 (memq ':interactive package))))
1947        (mapcar
1948         (lambda (function)
1949           (let (keymap)
1950             (when (consp function)
1951               (setq keymap (car (memq 'keymap function)))
1952               (setq function (car function)))
1953             (autoload function (car package) nil interactive keymap)))
1954         (if (eq (nth 1 package) ':interactive)
1955             (cdddr package)
1956           (cdr package)))))
1957    '(("metamail" metamail-buffer)
1958      ("info" Info-goto-node)
1959      ("hexl" hexl-hex-string-to-integer)
1960      ("pp" pp pp-to-string pp-eval-expression)
1961      ("mail-extr" mail-extract-address-components)
1962      ("nnmail" nnmail-split-fancy nnmail-article-group)
1963      ("nnvirtual" nnvirtual-catchup-group)
1964      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
1965       timezone-make-sortable-date timezone-make-time-string)
1966      ("sendmail" mail-position-on-field mail-setup)
1967      ("rmailout" rmail-output)
1968      ("rnewspost" news-mail-other-window news-reply-yank-original
1969       news-caesar-buffer-body)
1970      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
1971       rmail-show-message)
1972      ("gnus-soup" :interactive t
1973       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
1974       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
1975      ("nnsoup" nnsoup-pack-replies)
1976      ("gnus-scomo" :interactive t gnus-score-mode)
1977      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
1978       gnus-Folder-save-name gnus-folder-save-name)
1979      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
1980      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
1981       gnus-server-make-menu-bar gnus-article-make-menu-bar
1982       gnus-browse-make-menu-bar gnus-highlight-selected-summary
1983       gnus-summary-highlight-line gnus-carpal-setup-buffer
1984       gnus-group-highlight-line
1985       gnus-article-add-button gnus-insert-next-page-button
1986       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
1987      ("gnus-vis" :interactive t
1988       gnus-article-push-button gnus-article-press-button
1989       gnus-article-highlight gnus-article-highlight-some
1990       gnus-article-highlight-headers gnus-article-highlight-signature
1991       gnus-article-add-buttons gnus-article-add-buttons-to-head
1992       gnus-article-next-button gnus-article-prev-button)
1993      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
1994       gnus-demon-add-disconnection gnus-demon-add-handler
1995       gnus-demon-remove-handler)
1996      ("gnus-demon" :interactive t
1997       gnus-demon-init gnus-demon-cancel)
1998      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
1999       gnus-tree-open gnus-tree-close)
2000      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
2001       gnus-nocem-unwanted-article-p)
2002      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2003      ("gnus-srvr" gnus-browse-foreign-server)
2004      ("gnus-cite" :interactive t
2005       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2006       gnus-article-hide-citation gnus-article-fill-cited-article)
2007      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2008       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2009       gnus-execute gnus-expunge)
2010      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2011       gnus-cache-possibly-remove-articles gnus-cache-request-article
2012       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2013       gnus-cache-enter-remove-article gnus-cached-article-p
2014       gnus-cache-open gnus-cache-close gnus-cache-update-article)
2015      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2016       gnus-cache-remove-article)
2017      ("gnus-score" :interactive t
2018       gnus-summary-increase-score gnus-summary-lower-score
2019       gnus-score-flush-cache gnus-score-close
2020       gnus-score-raise-same-subject-and-select
2021       gnus-score-raise-same-subject gnus-score-default
2022       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2023       gnus-score-lower-same-subject gnus-score-lower-thread
2024       gnus-possibly-score-headers)
2025      ("gnus-score"
2026       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2027       gnus-current-score-file-nondirectory gnus-score-adaptive
2028       gnus-score-find-trace gnus-score-file-name)
2029      ("gnus-edit" :interactive t gnus-score-customize)
2030      ("gnus-topic" :interactive t gnus-topic-mode)
2031      ("gnus-topic" gnus-topic-remove-group)
2032      ("gnus-salt" :interactive t gnus-pick-mode gnus-binary-mode)
2033      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2034      ("gnus-uu" :interactive t
2035       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2036       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2037       gnus-uu-mark-by-regexp gnus-uu-mark-all
2038       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2039       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2040       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2041       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2042       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2043       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2044       gnus-uu-decode-binhex-view)
2045      ("gnus-msg" (gnus-summary-send-map keymap)
2046       gnus-mail-yank-original gnus-mail-send-and-exit
2047       gnus-sendmail-setup-mail gnus-article-mail
2048       gnus-inews-message-id gnus-new-mail gnus-mail-reply)
2049      ("gnus-msg" :interactive t
2050       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2051       gnus-summary-followup gnus-summary-followup-with-original
2052       gnus-summary-followup-and-reply
2053       gnus-summary-followup-and-reply-with-original
2054       gnus-summary-cancel-article gnus-summary-supersede-article
2055       gnus-post-news gnus-inews-news gnus-cancel-news
2056       gnus-summary-reply gnus-summary-reply-with-original
2057       gnus-summary-mail-forward gnus-summary-mail-other-window
2058       gnus-bug)
2059      ("gnus-picon" :interactive t gnus-article-display-picons
2060       gnus-group-display-picons gnus-picons-article-display-x-face)
2061      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2062       gnus-grouplens-mode)
2063      ("gnus-vm" gnus-vm-mail-setup)
2064      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2065       gnus-summary-save-article-vm gnus-yank-article))))
2066
2067 \f
2068
2069 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2070 ;; If you want the cursor to go somewhere else, set these two
2071 ;; functions in some startup hook to whatever you want.
2072 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2073 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2074
2075 ;;; Various macros and substs.
2076
2077 (defun gnus-header-from (header)
2078   (mail-header-from header))
2079
2080 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2081   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2082   (let ((tempvar (make-symbol "GnusStartBufferWindow")))
2083     `(let ((,tempvar (selected-window)))
2084        (unwind-protect
2085            (progn
2086              (pop-to-buffer ,buffer)
2087              ,@forms)
2088          (select-window ,tempvar)))))
2089
2090 (defmacro gnus-gethash (string hashtable)
2091   "Get hash value of STRING in HASHTABLE."
2092   `(symbol-value (intern-soft ,string ,hashtable)))
2093
2094 (defmacro gnus-sethash (string value hashtable)
2095   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2096   `(set (intern ,string ,hashtable) ,value))
2097
2098 (defmacro gnus-intern-safe (string hashtable)
2099   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2100   `(let ((symbol (intern ,string ,hashtable)))
2101      (or (boundp symbol)
2102          (set symbol nil))
2103      symbol))
2104
2105 (defmacro gnus-group-unread (group)
2106   "Get the currently computed number of unread articles in GROUP."
2107   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2108
2109 (defmacro gnus-group-entry (group)
2110   "Get the newsrc entry for GROUP."
2111   `(gnus-gethash ,group gnus-newsrc-hashtb))
2112
2113 (defmacro gnus-active (group)
2114   "Get active info on GROUP."
2115   `(gnus-gethash ,group gnus-active-hashtb))
2116
2117 (defmacro gnus-set-active (group active)
2118   "Set GROUP's active info."
2119   `(gnus-sethash ,group ,active gnus-active-hashtb))
2120
2121 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2122 ;;   function `substring' might cut on a middle of multi-octet
2123 ;;   character.
2124 (defun gnus-truncate-string (str width)
2125   (substring str 0 width))
2126
2127 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2128 ;; to limit the length of a string.  This function is necessary since
2129 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2130 (defsubst gnus-limit-string (str width)
2131   (if (> (length str) width)
2132       (substring str 0 width)
2133     str))
2134
2135 (defsubst gnus-simplify-subject-re (subject)
2136   "Remove \"Re:\" from subject lines."
2137   (if (string-match "^[Rr][Ee]: *" subject)
2138       (substring subject (match-end 0))
2139     subject))
2140
2141 (defsubst gnus-functionp (form)
2142   "Return non-nil if FORM is funcallable."
2143   (or (and (symbolp form) (fboundp form))
2144       (and (listp form) (eq (car form) 'lambda))))
2145
2146 (defsubst gnus-goto-char (point)
2147   (and point (goto-char point)))
2148
2149 (defmacro gnus-buffer-exists-p (buffer)
2150   `(let ((buffer ,buffer))
2151      (and buffer
2152           (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
2153                    buffer))))
2154
2155 (defmacro gnus-kill-buffer (buffer)
2156   `(let ((buf ,buffer))
2157      (if (gnus-buffer-exists-p buf)
2158          (kill-buffer buf))))
2159
2160 (defsubst gnus-point-at-bol ()
2161   "Return point at the beginning of the line."
2162   (let ((p (point)))
2163     (beginning-of-line)
2164     (prog1
2165         (point)
2166       (goto-char p))))
2167
2168 (defsubst gnus-point-at-eol ()
2169   "Return point at the end of the line."
2170   (let ((p (point)))
2171     (end-of-line)
2172     (prog1
2173         (point)
2174       (goto-char p))))
2175
2176 (defun gnus-alive-p ()
2177   "Say whether Gnus is running or not."
2178   (and gnus-group-buffer
2179        (get-buffer gnus-group-buffer)))
2180
2181 ;; Delete the current line (and the next N lines.);
2182 (defmacro gnus-delete-line (&optional n)
2183   `(delete-region (progn (beginning-of-line) (point))
2184                   (progn (forward-line ,(or n 1)) (point))))
2185
2186 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2187 (defvar gnus-init-inhibit nil)
2188 (defun gnus-read-init-file (&optional inhibit-next)
2189   (if gnus-init-inhibit
2190       (setq gnus-init-inhibit nil)
2191     (setq gnus-init-inhibit inhibit-next)
2192     (and gnus-init-file
2193          (or (and (file-exists-p gnus-init-file)
2194                   ;; Don't try to load a directory.
2195                   (not (file-directory-p gnus-init-file)))
2196              (file-exists-p (concat gnus-init-file ".el"))
2197              (file-exists-p (concat gnus-init-file ".elc")))
2198          (condition-case var
2199              (load gnus-init-file nil t)
2200            (error
2201             (error "Error in %s: %s" gnus-init-file var))))))
2202
2203 ;; Info access macros.
2204
2205 (defmacro gnus-info-group (info)
2206   `(nth 0 ,info))
2207 (defmacro gnus-info-rank (info)
2208   `(nth 1 ,info))
2209 (defmacro gnus-info-read (info)
2210   `(nth 2 ,info))
2211 (defmacro gnus-info-marks (info)
2212   `(nth 3 ,info))
2213 (defmacro gnus-info-method (info)
2214   `(nth 4 ,info))
2215 (defmacro gnus-info-params (info)
2216   `(nth 5 ,info))
2217
2218 (defmacro gnus-info-level (info)
2219   `(let ((rank (gnus-info-rank ,info)))
2220      (if (consp rank)
2221          (car rank)
2222        rank)))
2223 (defmacro gnus-info-score (info)
2224   `(let ((rank (gnus-info-rank ,info)))
2225      (or (and (consp rank) (cdr rank)) 0)))
2226
2227 (defmacro gnus-info-set-group (info group)
2228   `(setcar ,info ,group))
2229 (defmacro gnus-info-set-rank (info rank)
2230   `(setcar (nthcdr 1 ,info) ,rank))
2231 (defmacro gnus-info-set-read (info read)
2232   `(setcar (nthcdr 2 ,info) ,read))
2233 (defmacro gnus-info-set-marks (info marks)
2234   `(setcar (nthcdr 3 ,info) ,marks))
2235 (defmacro gnus-info-set-method (info method)
2236   `(setcar (nthcdr 4 ,info) ,method))
2237 (defmacro gnus-info-set-params (info params)
2238   `(setcar (nthcdr 5 ,info) ,params))
2239
2240 (defmacro gnus-info-set-level (info level)
2241   `(let ((rank (cdr ,info)))
2242      (if (consp (car rank))
2243          (setcar (car rank) ,level)
2244        (setcar rank ,level))))
2245 (defmacro gnus-info-set-score (info score)
2246   `(let ((rank (cdr ,info)))
2247      (if (consp (car rank))
2248          (setcdr (car rank) ,score)
2249        (setcar rank (cons (car rank) ,score)))))
2250
2251 (defmacro gnus-get-info (group)
2252   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2253
2254 (defun gnus-byte-code (func)
2255   "Return a form that can be `eval'ed based on FUNC."
2256   (let ((fval (symbol-function func)))
2257     (if (byte-code-function-p fval)
2258         (let ((flist (append fval nil)))
2259           (setcar flist 'byte-code)
2260           flist)
2261       (cons 'progn (cddr fval)))))
2262
2263 ;;; Load the compatability functions.
2264
2265 (require 'gnus-cus)
2266 (require 'gnus-ems)
2267
2268 \f
2269 ;;;
2270 ;;; Shutdown
2271 ;;;
2272
2273 (defvar gnus-shutdown-alist nil)
2274
2275 (defun gnus-add-shutdown (function &rest symbols)
2276   "Run FUNCTION whenever one of SYMBOLS is shut down."
2277   (push (cons function symbols) gnus-shutdown-alist))
2278
2279 (defun gnus-shutdown (symbol)
2280   "Shut down everything that waits for SYMBOL."
2281   (let ((alist gnus-shutdown-alist)
2282         entry)
2283     (while (setq entry (pop alist))
2284       (when (memq symbol (cdr entry))
2285         (funcall (car entry))))))
2286
2287 \f
2288
2289 ;; Format specs.  The chunks below are the machine-generated forms
2290 ;; that are to be evaled as the result of the default format strings.
2291 ;; We write them in here to get them byte-compiled.  That way the
2292 ;; default actions will be quite fast, while still retaining the full
2293 ;; flexibility of the user-defined format specs.
2294
2295 ;; First we have lots of dummy defvars to let the compiler know these
2296 ;; are really dynamic variables.
2297
2298 (defvar gnus-tmp-unread)
2299 (defvar gnus-tmp-replied)
2300 (defvar gnus-tmp-score-char)
2301 (defvar gnus-tmp-indentation)
2302 (defvar gnus-tmp-opening-bracket)
2303 (defvar gnus-tmp-lines)
2304 (defvar gnus-tmp-name)
2305 (defvar gnus-tmp-closing-bracket)
2306 (defvar gnus-tmp-subject-or-nil)
2307 (defvar gnus-tmp-subject)
2308 (defvar gnus-tmp-marked)
2309 (defvar gnus-tmp-marked-mark)
2310 (defvar gnus-tmp-subscribed)
2311 (defvar gnus-tmp-process-marked)
2312 (defvar gnus-tmp-number-of-unread)
2313 (defvar gnus-tmp-group-name)
2314 (defvar gnus-tmp-group)
2315 (defvar gnus-tmp-article-number)
2316 (defvar gnus-tmp-unread-and-unselected)
2317 (defvar gnus-tmp-news-method)
2318 (defvar gnus-tmp-news-server)
2319 (defvar gnus-tmp-article-number)
2320 (defvar gnus-mouse-face)
2321 (defvar gnus-mouse-face-prop)
2322
2323 (defun gnus-summary-line-format-spec ()
2324   (insert gnus-tmp-unread gnus-tmp-replied
2325           gnus-tmp-score-char gnus-tmp-indentation)
2326   (put-text-property
2327    (point)
2328    (progn
2329      (insert
2330       gnus-tmp-opening-bracket
2331       (format "%4d: %-20s"
2332               gnus-tmp-lines
2333               (if (> (length gnus-tmp-name) 20)
2334                   (substring gnus-tmp-name 0 20)
2335                 gnus-tmp-name))
2336       gnus-tmp-closing-bracket)
2337      (point))
2338    gnus-mouse-face-prop gnus-mouse-face)
2339   (insert " " gnus-tmp-subject-or-nil "\n"))
2340
2341 (defvar gnus-summary-line-format-spec
2342   (gnus-byte-code 'gnus-summary-line-format-spec))
2343
2344 (defun gnus-summary-dummy-line-format-spec ()
2345   (insert "*  ")
2346   (put-text-property
2347    (point)
2348    (progn
2349      (insert ":                          :")
2350      (point))
2351    gnus-mouse-face-prop gnus-mouse-face)
2352   (insert " " gnus-tmp-subject "\n"))
2353
2354 (defvar gnus-summary-dummy-line-format-spec
2355   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2356
2357 (defun gnus-group-line-format-spec ()
2358   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2359           gnus-tmp-process-marked
2360           gnus-group-indentation
2361           (format "%5s: " gnus-tmp-number-of-unread))
2362   (put-text-property
2363    (point)
2364    (progn
2365      (insert gnus-tmp-group "\n")
2366      (1- (point)))
2367    gnus-mouse-face-prop gnus-mouse-face))
2368 (defvar gnus-group-line-format-spec
2369   (gnus-byte-code 'gnus-group-line-format-spec))
2370
2371 (defvar gnus-format-specs
2372   `((version . ,emacs-version)
2373     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2374     (summary-dummy ,gnus-summary-dummy-line-format
2375                    ,gnus-summary-dummy-line-format-spec)
2376     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2377
2378 (defvar gnus-article-mode-line-format-spec nil)
2379 (defvar gnus-summary-mode-line-format-spec nil)
2380 (defvar gnus-group-mode-line-format-spec nil)
2381
2382 ;;; Phew.  All that gruft is over, fortunately.
2383
2384 \f
2385 ;;;
2386 ;;; Gnus Utility Functions
2387 ;;;
2388
2389 (defun gnus-extract-address-components (from)
2390   (let (name address)
2391     ;; First find the address - the thing with the @ in it.  This may
2392     ;; not be accurate in mail addresses, but does the trick most of
2393     ;; the time in news messages.
2394     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2395         (setq address (substring from (match-beginning 0) (match-end 0))))
2396     ;; Then we check whether the "name <address>" format is used.
2397     (and address
2398          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2399          ;; Linear white space is not required.
2400          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2401          (and (setq name (substring from 0 (match-beginning 0)))
2402               ;; Strip any quotes from the name.
2403               (string-match "\".*\"" name)
2404               (setq name (substring name 1 (1- (match-end 0))))))
2405     ;; If not, then "address (name)" is used.
2406     (or name
2407         (and (string-match "(.+)" from)
2408              (setq name (substring from (1+ (match-beginning 0))
2409                                    (1- (match-end 0)))))
2410         (and (string-match "()" from)
2411              (setq name address))
2412         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2413         ;; XOVER might not support folded From headers.
2414         (and (string-match "(.*" from)
2415              (setq name (substring from (1+ (match-beginning 0))
2416                                    (match-end 0)))))
2417     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2418     (list (or name from) (or address from))))
2419
2420 (defun gnus-fetch-field (field)
2421   "Return the value of the header FIELD of current article."
2422   (save-excursion
2423     (save-restriction
2424       (let ((case-fold-search t))
2425         (nnheader-narrow-to-headers)
2426         (mail-fetch-field field)))))
2427
2428 (defun gnus-goto-colon ()
2429   (beginning-of-line)
2430   (search-forward ":" (gnus-point-at-eol) t))
2431
2432 ;;;###autoload
2433 (defun gnus-update-format (var)
2434   "Update the format specification near point."
2435   (interactive
2436    (list
2437     (save-excursion
2438       (eval-defun nil)
2439       ;; Find the end of the current word.
2440       (re-search-forward "[ \t\n]" nil t)
2441       ;; Search backward.
2442       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2443         (match-string 1)))))
2444   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2445                               (match-string 1 var))))
2446          (entry (assq type gnus-format-specs))
2447          value spec)
2448     (when entry
2449       (setq gnus-format-specs (delq entry gnus-format-specs)))
2450     (set
2451      (intern (format "%s-spec" var))
2452      (gnus-parse-format (setq value (symbol-value (intern var)))
2453                         (symbol-value (intern (format "%s-alist" var)))
2454                         (not (string-match "mode" var))))
2455     (setq spec (symbol-value (intern (format "%s-spec" var))))
2456     (push (list type value spec) gnus-format-specs)
2457
2458     (pop-to-buffer "*Gnus Format*")
2459     (erase-buffer)
2460     (lisp-interaction-mode)
2461     (insert (pp-to-string spec))))
2462
2463 (defun gnus-update-format-specifications (&optional force)
2464   "Update all (necessary) format specifications."
2465   ;; Make the indentation array.
2466   (gnus-make-thread-indent-array)
2467
2468   ;; See whether all the stored info needs to be flushed.
2469   (when (or force
2470             (not (equal emacs-version
2471                         (cdr (assq 'version gnus-format-specs)))))
2472     (setq gnus-format-specs nil))
2473
2474   ;; Go through all the formats and see whether they need updating.
2475   (let ((types '(summary summary-dummy group
2476                          summary-mode group-mode article-mode))
2477         new-format entry type val)
2478     (while (setq type (pop types))
2479       ;; Jump to the proper buffer to find out the value of
2480       ;; the variable, if possible.  (It may be buffer-local.)
2481       (save-excursion
2482         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2483               val)
2484           (when (and (boundp buffer)
2485                      (setq val (symbol-value buffer))
2486                      (get-buffer val)
2487                      (buffer-name (get-buffer val)))
2488             (set-buffer (get-buffer val)))
2489           (setq new-format (symbol-value
2490                             (intern (format "gnus-%s-line-format" type))))))
2491       (setq entry (cdr (assq type gnus-format-specs)))
2492       (if (and entry
2493                (equal (car entry) new-format))
2494           ;; Use the old format.
2495           (set (intern (format "gnus-%s-line-format-spec" type))
2496                (cadr entry))
2497         ;; This is a new format.
2498         (setq val
2499               (if (not (stringp new-format))
2500                   ;; This is a function call or something.
2501                   new-format
2502                 ;; This is a "real" format.
2503                 (gnus-parse-format
2504                  new-format
2505                  (symbol-value
2506                   (intern (format "gnus-%s-line-format-alist"
2507                                   (if (eq type 'article-mode)
2508                                       'summary-mode type))))
2509                  (not (string-match "mode$" (symbol-name type))))))
2510         ;; Enter the new format spec into the list.
2511         (if entry
2512             (progn
2513               (setcar (cdr entry) val)
2514               (setcar entry new-format))
2515           (push (list type new-format val) gnus-format-specs))
2516         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2517
2518   (unless (assq 'version gnus-format-specs)
2519     (push (cons 'version emacs-version) gnus-format-specs))
2520
2521   (gnus-update-group-mark-positions)
2522   (gnus-update-summary-mark-positions))
2523
2524 (defun gnus-update-summary-mark-positions ()
2525   "Compute where the summary marks are to go."
2526   (save-excursion
2527     (let ((gnus-replied-mark 129)
2528           (gnus-score-below-mark 130)
2529           (gnus-score-over-mark 130)
2530           (thread nil)
2531           (gnus-visual nil)
2532           pos)
2533       (gnus-set-work-buffer)
2534       (gnus-summary-insert-line
2535        [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2536       (goto-char (point-min))
2537       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2538                                          (- (point) 2)))))
2539       (goto-char (point-min))
2540       (push (cons 'replied (and (search-forward "\201" nil t) (- (point) 2)))
2541             pos)
2542       (goto-char (point-min))
2543       (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2544             pos)
2545       (setq gnus-summary-mark-positions pos))))
2546
2547 (defun gnus-update-group-mark-positions ()
2548   (save-excursion
2549     (let ((gnus-process-mark 128)
2550           (gnus-group-marked '("dummy.group")))
2551       (gnus-set-active "dummy.group" '(0 . 0))
2552       (gnus-set-work-buffer)
2553       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2554       (goto-char (point-min))
2555       (setq gnus-group-mark-positions
2556             (list (cons 'process (and (search-forward "\200" nil t)
2557                                       (- (point) 2))))))))
2558
2559 (defvar gnus-mouse-face-0 'highlight)
2560 (defvar gnus-mouse-face-1 'highlight)
2561 (defvar gnus-mouse-face-2 'highlight)
2562 (defvar gnus-mouse-face-3 'highlight)
2563 (defvar gnus-mouse-face-4 'highlight)
2564
2565 (defun gnus-mouse-face-function (form type)
2566   `(put-text-property
2567     (point) (progn ,@form (point))
2568     gnus-mouse-face-prop
2569     ,(if (equal type 0)
2570          'gnus-mouse-face
2571        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2572
2573 (defvar gnus-face-0 'bold)
2574 (defvar gnus-face-1 'italic)
2575 (defvar gnus-face-2 'bold-italic)
2576 (defvar gnus-face-3 'bold)
2577 (defvar gnus-face-4 'bold)
2578
2579 (defun gnus-face-face-function (form type)
2580   `(put-text-property
2581     (point) (progn ,@form (point))
2582     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2583
2584 (defun gnus-max-width-function (el max-width)
2585   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2586   (if (symbolp el)
2587       `(if (> (length ,el) ,max-width)
2588            (substring ,el 0 ,max-width)
2589          ,el)
2590     `(let ((val (eval ,el)))
2591        (if (numberp val)
2592            (setq val (int-to-string val)))
2593        (if (> (length val) ,max-width)
2594            (substring val 0 ,max-width)
2595          val))))
2596
2597 (defun gnus-parse-format (format spec-alist &optional insert)
2598   ;; This function parses the FORMAT string with the help of the
2599   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2600   ;; string.  If the FORMAT string contains the specifiers %( and %)
2601   ;; the text between them will have the mouse-face text property.
2602   (if (string-match
2603        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2604        format)
2605       (gnus-parse-complex-format format spec-alist)
2606     ;; This is a simple format.
2607     (gnus-parse-simple-format format spec-alist insert)))
2608
2609 (defun gnus-parse-complex-format (format spec-alist)
2610   (save-excursion
2611     (gnus-set-work-buffer)
2612     (insert format)
2613     (goto-char (point-min))
2614     (while (re-search-forward "\"" nil t)
2615       (replace-match "\\\"" nil t))
2616     (goto-char (point-min))
2617     (insert "(\"")
2618     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2619       (let ((number (if (match-beginning 1)
2620                         (match-string 1) "0"))
2621             (delim (aref (match-string 2) 0)))
2622         (if (or (= delim ?\() (= delim ?\{))
2623             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2624                                    " " number " \""))
2625           (replace-match "\")\""))))
2626     (goto-char (point-max))
2627     (insert "\")")
2628     (goto-char (point-min))
2629     (let ((form (read (current-buffer))))
2630       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2631
2632 (defun gnus-complex-form-to-spec (form spec-alist)
2633   (delq nil
2634         (mapcar
2635          (lambda (sform)
2636            (if (stringp sform)
2637                (gnus-parse-simple-format sform spec-alist t)
2638              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2639                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2640                       (nth 1 sform))))
2641          form)))
2642
2643 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2644   ;; This function parses the FORMAT string with the help of the
2645   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2646   ;; string.
2647   (let ((max-width 0)
2648         spec flist fstring newspec elem beg result dontinsert)
2649     (save-excursion
2650       (gnus-set-work-buffer)
2651       (insert format)
2652       (goto-char (point-min))
2653       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2654                                 nil t)
2655         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2656               (setq newspec "%"
2657                     beg (1+ (match-beginning 0)))
2658           ;; First check if there are any specs that look anything like
2659           ;; "%12,12A", ie. with a "max width specification".  These have
2660           ;; to be treated specially.
2661           (if (setq beg (match-beginning 1))
2662               (setq max-width
2663                     (string-to-int
2664                      (buffer-substring
2665                       (1+ (match-beginning 1)) (match-end 1))))
2666             (setq max-width 0)
2667             (setq beg (match-beginning 2)))
2668           ;; Find the specification from `spec-alist'.
2669           (unless (setq elem (cdr (assq spec spec-alist)))
2670             (setq elem '("*" ?s)))
2671           ;; Treat user defined format specifiers specially.
2672           (when (eq (car elem) 'gnus-tmp-user-defined)
2673             (setq elem
2674                   (list
2675                    (list (intern (concat "gnus-user-format-function-"
2676                                          (match-string 3)))
2677                          'gnus-tmp-header) ?s))
2678             (delete-region (match-beginning 3) (match-end 3)))
2679           (if (not (zerop max-width))
2680               (let ((el (car elem)))
2681                 (cond ((= (cadr elem) ?c)
2682                        (setq el (list 'char-to-string el)))
2683                       ((= (cadr elem) ?d)
2684                        (setq el (list 'int-to-string el))))
2685                 (setq flist (cons (gnus-max-width-function el max-width)
2686                                   flist))
2687                 (setq newspec ?s))
2688             (progn
2689               (setq flist (cons (car elem) flist))
2690               (setq newspec (cadr elem)))))
2691         ;; Remove the old specification (and possibly a ",12" string).
2692         (delete-region beg (match-end 2))
2693         ;; Insert the new specification.
2694         (goto-char beg)
2695         (insert newspec))
2696       (setq fstring (buffer-substring 1 (point-max))))
2697     ;; Do some postprocessing to increase efficiency.
2698     (setq
2699      result
2700      (cond
2701       ;; Emptyness.
2702       ((string= fstring "")
2703        nil)
2704       ;; Not a format string.
2705       ((not (string-match "%" fstring))
2706        (list fstring))
2707       ;; A format string with just a single string spec.
2708       ((string= fstring "%s")
2709        (list (car flist)))
2710       ;; A single character.
2711       ((string= fstring "%c")
2712        (list (car flist)))
2713       ;; A single number.
2714       ((string= fstring "%d")
2715        (setq dontinsert)
2716        (if insert
2717            (list `(princ ,(car flist)))
2718          (list `(int-to-string ,(car flist)))))
2719       ;; Just lots of chars and strings.
2720       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2721        (nreverse flist))
2722       ;; A single string spec at the beginning of the spec.
2723       ((string-match "\\`%[sc][^%]+\\'" fstring)
2724        (list (car flist) (substring fstring 2)))
2725       ;; A single string spec in the middle of the spec.
2726       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2727        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2728       ;; A single string spec in the end of the spec.
2729       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2730        (list (match-string 1 fstring) (car flist)))
2731       ;; A more complex spec.
2732       (t
2733        (list (cons 'format (cons fstring (nreverse flist)))))))
2734
2735     (if insert
2736         (when result
2737           (if dontinsert
2738               result
2739             (cons 'insert result)))
2740       (cond ((stringp result)
2741              result)
2742             ((consp result)
2743              (cons 'concat result))
2744             (t "")))))
2745
2746 (defun gnus-eval-format (format &optional alist props)
2747   "Eval the format variable FORMAT, using ALIST.
2748 If PROPS, insert the result."
2749   (let ((form (gnus-parse-format format alist props)))
2750     (if props
2751         (add-text-properties (point) (progn (eval form) (point)) props)
2752       (eval form))))
2753
2754 (defun gnus-remove-text-with-property (prop)
2755   "Delete all text in the current buffer with text property PROP."
2756   (save-excursion
2757     (goto-char (point-min))
2758     (while (not (eobp))
2759       (while (get-text-property (point) prop)
2760         (delete-char 1))
2761       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2762
2763 (defun gnus-set-work-buffer ()
2764   (if (get-buffer gnus-work-buffer)
2765       (progn
2766         (set-buffer gnus-work-buffer)
2767         (erase-buffer))
2768     (set-buffer (get-buffer-create gnus-work-buffer))
2769     (kill-all-local-variables)
2770     (buffer-disable-undo (current-buffer))
2771     (gnus-add-current-to-buffer-list)))
2772
2773 ;; Article file names when saving.
2774
2775 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2776   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2777 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2778 Otherwise, it is like ~/News/news/group/num."
2779   (let ((default
2780           (expand-file-name
2781            (concat (if (gnus-use-long-file-name 'not-save)
2782                        (gnus-capitalize-newsgroup newsgroup)
2783                      (gnus-newsgroup-directory-form newsgroup))
2784                    "/" (int-to-string (mail-header-number headers)))
2785            (or gnus-article-save-directory "~/News"))))
2786     (if (and last-file
2787              (string-equal (file-name-directory default)
2788                            (file-name-directory last-file))
2789              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2790         default
2791       (or last-file default))))
2792
2793 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2794   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2795 If variable `gnus-use-long-file-name' is non-nil, it is
2796 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2797   (let ((default
2798           (expand-file-name
2799            (concat (if (gnus-use-long-file-name 'not-save)
2800                        newsgroup
2801                      (gnus-newsgroup-directory-form newsgroup))
2802                    "/" (int-to-string (mail-header-number headers)))
2803            (or gnus-article-save-directory "~/News"))))
2804     (if (and last-file
2805              (string-equal (file-name-directory default)
2806                            (file-name-directory last-file))
2807              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2808         default
2809       (or last-file default))))
2810
2811 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2812   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2813 If variable `gnus-use-long-file-name' is non-nil, it is
2814 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2815   (or last-file
2816       (expand-file-name
2817        (if (gnus-use-long-file-name 'not-save)
2818            (gnus-capitalize-newsgroup newsgroup)
2819          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2820        (or gnus-article-save-directory "~/News"))))
2821
2822 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2823   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2824 If variable `gnus-use-long-file-name' is non-nil, it is
2825 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2826   (or last-file
2827       (expand-file-name
2828        (if (gnus-use-long-file-name 'not-save)
2829            newsgroup
2830          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2831        (or gnus-article-save-directory "~/News"))))
2832
2833 ;; For subscribing new newsgroup
2834
2835 (defun gnus-subscribe-hierarchical-interactive (groups)
2836   (let ((groups (sort groups 'string<))
2837         prefixes prefix start ans group starts)
2838     (while groups
2839       (setq prefixes (list "^"))
2840       (while (and groups prefixes)
2841         (while (not (string-match (car prefixes) (car groups)))
2842           (setq prefixes (cdr prefixes)))
2843         (setq prefix (car prefixes))
2844         (setq start (1- (length prefix)))
2845         (if (and (string-match "[^\\.]\\." (car groups) start)
2846                  (cdr groups)
2847                  (setq prefix
2848                        (concat "^" (substring (car groups) 0 (match-end 0))))
2849                  (string-match prefix (cadr groups)))
2850             (progn
2851               (setq prefixes (cons prefix prefixes))
2852               (message "Descend hierarchy %s? ([y]nsq): "
2853                        (substring prefix 1 (1- (length prefix))))
2854               (setq ans (read-char))
2855               (cond ((= ans ?n)
2856                      (while (and groups
2857                                  (string-match prefix
2858                                                (setq group (car groups))))
2859                        (setq gnus-killed-list
2860                              (cons group gnus-killed-list))
2861                        (gnus-sethash group group gnus-killed-hashtb)
2862                        (setq groups (cdr groups)))
2863                      (setq starts (cdr starts)))
2864                     ((= ans ?s)
2865                      (while (and groups
2866                                  (string-match prefix
2867                                                (setq group (car groups))))
2868                        (gnus-sethash group group gnus-killed-hashtb)
2869                        (gnus-subscribe-alphabetically (car groups))
2870                        (setq groups (cdr groups)))
2871                      (setq starts (cdr starts)))
2872                     ((= ans ?q)
2873                      (while groups
2874                        (setq group (car groups))
2875                        (setq gnus-killed-list (cons group gnus-killed-list))
2876                        (gnus-sethash group group gnus-killed-hashtb)
2877                        (setq groups (cdr groups))))
2878                     (t nil)))
2879           (message "Subscribe %s? ([n]yq)" (car groups))
2880           (setq ans (read-char))
2881           (setq group (car groups))
2882           (cond ((= ans ?y)
2883                  (gnus-subscribe-alphabetically (car groups))
2884                  (gnus-sethash group group gnus-killed-hashtb))
2885                 ((= ans ?q)
2886                  (while groups
2887                    (setq group (car groups))
2888                    (setq gnus-killed-list (cons group gnus-killed-list))
2889                    (gnus-sethash group group gnus-killed-hashtb)
2890                    (setq groups (cdr groups))))
2891                 (t
2892                  (setq gnus-killed-list (cons group gnus-killed-list))
2893                  (gnus-sethash group group gnus-killed-hashtb)))
2894           (setq groups (cdr groups)))))))
2895
2896 (defun gnus-subscribe-randomly (newsgroup)
2897   "Subscribe new NEWSGROUP by making it the first newsgroup."
2898   (gnus-subscribe-newsgroup newsgroup))
2899
2900 (defun gnus-subscribe-alphabetically (newgroup)
2901   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2902   (let ((groups (cdr gnus-newsrc-alist))
2903         before)
2904     (while (and (not before) groups)
2905       (if (string< newgroup (caar groups))
2906           (setq before (caar groups))
2907         (setq groups (cdr groups))))
2908     (gnus-subscribe-newsgroup newgroup before)))
2909
2910 (defun gnus-subscribe-hierarchically (newgroup)
2911   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2912   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2913   (save-excursion
2914     (set-buffer (find-file-noselect gnus-current-startup-file))
2915     (let ((groupkey newgroup)
2916           before)
2917       (while (and (not before) groupkey)
2918         (goto-char (point-min))
2919         (let ((groupkey-re
2920                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2921           (while (and (re-search-forward groupkey-re nil t)
2922                       (progn
2923                         (setq before (match-string 1))
2924                         (string< before newgroup)))))
2925         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2926         (setq groupkey
2927               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2928                   (substring groupkey (match-beginning 1) (match-end 1)))))
2929       (gnus-subscribe-newsgroup newgroup before))))
2930
2931 (defun gnus-subscribe-interactively (group)
2932   "Subscribe the new GROUP interactively.
2933 It is inserted in hierarchical newsgroup order if subscribed.  If not,
2934 it is killed."
2935   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
2936       (gnus-subscribe-hierarchically group)
2937     (push group gnus-killed-list)))
2938
2939 (defun gnus-subscribe-zombies (group)
2940   "Make the new GROUP into a zombie group."
2941   (push group gnus-zombie-list))
2942
2943 (defun gnus-subscribe-killed (group)
2944   "Make the new GROUP a killed group."
2945   (push group gnus-killed-list))
2946
2947 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2948   "Subscribe new NEWSGROUP.
2949 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
2950 the first newsgroup."
2951   ;; We subscribe the group by changing its level to `subscribed'.
2952   (gnus-group-change-level
2953    newsgroup gnus-level-default-subscribed
2954    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2955   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2956
2957 ;; For directories
2958
2959 (defun gnus-newsgroup-directory-form (newsgroup)
2960   "Make hierarchical directory name from NEWSGROUP name."
2961   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
2962         (len (length newsgroup))
2963         idx)
2964     ;; If this is a foreign group, we don't want to translate the
2965     ;; entire name.
2966     (if (setq idx (string-match ":" newsgroup))
2967         (aset newsgroup idx ?/)
2968       (setq idx 0))
2969     ;; Replace all occurrences of `.' with `/'.
2970     (while (< idx len)
2971       (if (= (aref newsgroup idx) ?.)
2972           (aset newsgroup idx ?/))
2973       (setq idx (1+ idx)))
2974     newsgroup))
2975
2976 (defun gnus-newsgroup-savable-name (group)
2977   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
2978   ;; with dots.
2979   (nnheader-replace-chars-in-string group ?/ ?.))
2980
2981 (defun gnus-make-directory (dir)
2982   "Make DIRECTORY recursively."
2983   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
2984   ;; of the many mysteries of the universe.
2985   (let* ((dir (expand-file-name dir default-directory))
2986          dirs err)
2987     (if (string-match "/$" dir)
2988         (setq dir (substring dir 0 (match-beginning 0))))
2989     ;; First go down the path until we find a directory that exists.
2990     (while (not (file-exists-p dir))
2991       (setq dirs (cons dir dirs))
2992       (string-match "/[^/]+$" dir)
2993       (setq dir (substring dir 0 (match-beginning 0))))
2994     ;; Then create all the subdirs.
2995     (while (and dirs (not err))
2996       (condition-case ()
2997           (make-directory (car dirs))
2998         (error (setq err t)))
2999       (setq dirs (cdr dirs)))
3000     ;; We return whether we were successful or not.
3001     (not dirs)))
3002
3003 (defun gnus-capitalize-newsgroup (newsgroup)
3004   "Capitalize NEWSGROUP name."
3005   (and (not (zerop (length newsgroup)))
3006        (concat (char-to-string (upcase (aref newsgroup 0)))
3007                (substring newsgroup 1))))
3008
3009 ;; Various... things.
3010
3011 (defun gnus-simplify-subject (subject &optional re-only)
3012   "Remove `Re:' and words in parentheses.
3013 If RE-ONLY is non-nil, strip leading `Re:'s only."
3014   (let ((case-fold-search t))           ;Ignore case.
3015     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
3016     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
3017       (setq subject (substring subject (match-end 0))))
3018     ;; Remove uninteresting prefixes.
3019     (if (and (not re-only)
3020              gnus-simplify-ignored-prefixes
3021              (string-match gnus-simplify-ignored-prefixes subject))
3022         (setq subject (substring subject (match-end 0))))
3023     ;; Remove words in parentheses from end.
3024     (unless re-only
3025       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3026         (setq subject (substring subject 0 (match-beginning 0)))))
3027     ;; Return subject string.
3028     subject))
3029
3030 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3031 ;; all whitespace.
3032 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3033 (defun gnus-simplify-buffer-fuzzy ()
3034   (goto-char (point-min))
3035   (while (search-forward "\t" nil t)
3036     (replace-match " " t t))
3037   (goto-char (point-min))
3038   (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *" nil t)
3039   (goto-char (match-beginning 0))
3040   (while (or
3041           (looking-at "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
3042           (looking-at "^[[].*: .*[]]$"))
3043     (goto-char (point-min))
3044     (while (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *"
3045                               nil t)
3046       (replace-match "" t t))
3047     (goto-char (point-min))
3048     (while (re-search-forward "^[[].*: .*[]]$" nil t)
3049       (goto-char (match-end 0))
3050       (delete-char -1)
3051       (delete-region
3052        (progn (goto-char (match-beginning 0)))
3053        (re-search-forward ":"))))
3054   (goto-char (point-min))
3055   (while (re-search-forward " *[[{(][^()\n]*[]})] *$" nil t)
3056     (replace-match "" t t))
3057   (goto-char (point-min))
3058   (while (re-search-forward "  +" nil t)
3059     (replace-match " " t t))
3060   (goto-char (point-min))
3061   (while (re-search-forward " $" nil t)
3062     (replace-match "" t t))
3063   (goto-char (point-min))
3064   (while (re-search-forward "^ +" nil t)
3065     (replace-match "" t t))
3066   (goto-char (point-min))
3067   (when gnus-simplify-subject-fuzzy-regexp
3068     (if (listp gnus-simplify-subject-fuzzy-regexp)
3069         (let ((list gnus-simplify-subject-fuzzy-regexp))
3070           (while list
3071             (goto-char (point-min))
3072             (while (re-search-forward (car list) nil t)
3073               (replace-match "" t t))
3074             (setq list (cdr list))))
3075       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3076         (replace-match "" t t)))))
3077
3078 (defun gnus-simplify-subject-fuzzy (subject)
3079   "Siplify a subject string fuzzily."
3080   (save-excursion
3081     (gnus-set-work-buffer)
3082     (let ((case-fold-search t))
3083       (insert subject)
3084       (inline (gnus-simplify-buffer-fuzzy))
3085       (buffer-string))))
3086
3087 ;; Add the current buffer to the list of buffers to be killed on exit.
3088 (defun gnus-add-current-to-buffer-list ()
3089   (or (memq (current-buffer) gnus-buffer-list)
3090       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3091
3092 (defun gnus-string> (s1 s2)
3093   (not (or (string< s1 s2)
3094            (string= s1 s2))))
3095
3096 ;;; General various misc type functions.
3097
3098 (defun gnus-clear-system ()
3099   "Clear all variables and buffers."
3100   ;; Clear Gnus variables.
3101   (let ((variables gnus-variable-list))
3102     (while variables
3103       (set (car variables) nil)
3104       (setq variables (cdr variables))))
3105   ;; Clear other internal variables.
3106   (setq gnus-list-of-killed-groups nil
3107         gnus-have-read-active-file nil
3108         gnus-newsrc-alist nil
3109         gnus-newsrc-hashtb nil
3110         gnus-killed-list nil
3111         gnus-zombie-list nil
3112         gnus-killed-hashtb nil
3113         gnus-active-hashtb nil
3114         gnus-moderated-list nil
3115         gnus-description-hashtb nil
3116         gnus-current-headers nil
3117         gnus-thread-indent-array nil
3118         gnus-newsgroup-headers nil
3119         gnus-newsgroup-name nil
3120         gnus-server-alist nil
3121         gnus-group-list-mode nil
3122         gnus-opened-servers nil
3123         gnus-current-select-method nil)
3124   (gnus-shutdown 'gnus)
3125   ;; Kill the startup file.
3126   (and gnus-current-startup-file
3127        (get-file-buffer gnus-current-startup-file)
3128        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3129   ;; Clear the dribble buffer.
3130   (gnus-dribble-clear)
3131   ;; Kill global KILL file buffer.
3132   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3133     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3134   (gnus-kill-buffer nntp-server-buffer)
3135   ;; Kill Gnus buffers.
3136   (while gnus-buffer-list
3137     (gnus-kill-buffer (pop gnus-buffer-list)))
3138   ;; Remove Gnus frames.
3139   (while gnus-created-frames
3140     (when (frame-live-p (car gnus-created-frames))
3141       ;; We slap a condition-case around this `delete-frame' to ensure 
3142       ;; agains errors if we try do delete the single frame that's left.
3143       (condition-case ()
3144           (delete-frame (car gnus-created-frames))
3145         (error nil)))
3146     (pop gnus-created-frames)))
3147
3148 (defun gnus-windows-old-to-new (setting)
3149   ;; First we take care of the really, really old Gnus 3 actions.
3150   (when (symbolp setting)
3151     (setq setting
3152           ;; Take care of ooold GNUS 3.x values.
3153           (cond ((eq setting 'SelectArticle) 'article)
3154                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3155                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3156                 (t setting))))
3157   (if (or (listp setting)
3158           (not (and gnus-window-configuration
3159                     (memq setting '(group summary article)))))
3160       setting
3161     (let* ((setting (if (eq setting 'group)
3162                         (if (assq 'newsgroup gnus-window-configuration)
3163                             'newsgroup
3164                           'newsgroups) setting))
3165            (elem (cadr (assq setting gnus-window-configuration)))
3166            (total (apply '+ elem))
3167            (types '(group summary article))
3168            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3169            (i 0)
3170            perc
3171            out)
3172       (while (< i 3)
3173         (or (not (numberp (nth i elem)))
3174             (zerop (nth i elem))
3175             (progn
3176               (setq perc  (/ (float (nth 0 elem)) total))
3177               (setq out (cons (if (eq pbuf (nth i types))
3178                                   (vector (nth i types) perc 'point)
3179                                 (vector (nth i types) perc))
3180                               out))))
3181         (setq i (1+ i)))
3182       (list (nreverse out)))))
3183
3184 ;;;###autoload
3185 (defun gnus-add-configuration (conf)
3186   "Add the window configuration CONF to `gnus-buffer-configuration'."
3187   (setq gnus-buffer-configuration
3188         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3189                          gnus-buffer-configuration))))
3190
3191 (defvar gnus-frame-list nil)
3192
3193 (defun gnus-configure-frame (split &optional window)
3194   "Split WINDOW according to SPLIT."
3195   (unless window
3196     (setq window (get-buffer-window (current-buffer))))
3197   (select-window window)
3198   ;; This might be an old-stylee buffer config.
3199   (when (vectorp split)
3200     (setq split (append split nil)))
3201   (when (or (consp (car split))
3202             (vectorp (car split)))
3203     (push 1.0 split)
3204     (push 'vertical split))
3205   ;; The SPLIT might be something that is to be evaled to
3206   ;; return a new SPLIT.
3207   (while (and (not (assq (car split) gnus-window-to-buffer))
3208               (gnus-functionp (car split)))
3209     (setq split (eval split)))
3210   (let* ((type (car split))
3211          (subs (cddr split))
3212          (len (if (eq type 'horizontal) (window-width) (window-height)))
3213          (total 0)
3214          (window-min-width (or gnus-window-min-width window-min-width))
3215          (window-min-height (or gnus-window-min-height window-min-height))
3216          s result new-win rest comp-subs size sub)
3217     (cond
3218      ;; Nothing to do here.
3219      ((null split))
3220      ;; Don't switch buffers.
3221      ((null type)
3222       (and (memq 'point split) window))
3223      ;; This is a buffer to be selected.
3224      ((not (memq type '(frame horizontal vertical)))
3225       (let ((buffer (cond ((stringp type) type)
3226                           (t (cdr (assq type gnus-window-to-buffer)))))
3227             buf)
3228         (unless buffer
3229           (error "Illegal buffer type: %s" type))
3230         (unless (setq buf (get-buffer (if (symbolp buffer)
3231                                           (symbol-value buffer) buffer)))
3232           (setq buf (get-buffer-create (if (symbolp buffer)
3233                                            (symbol-value buffer) buffer))))
3234         (switch-to-buffer buf)
3235         ;; We return the window if it has the `point' spec.
3236         (and (memq 'point split) window)))
3237      ;; This is a frame split.
3238      ((eq type 'frame)
3239       (unless gnus-frame-list
3240         (setq gnus-frame-list (list (window-frame
3241                                      (get-buffer-window (current-buffer))))))
3242       (let ((i 0)
3243             params frame fresult)
3244         (while (< i (length subs))
3245           ;; Frame parameter is gotten from the sub-split.
3246           (setq params (cadr (elt subs i)))
3247           ;; It should be a list.
3248           (unless (listp params)
3249             (setq params nil))
3250           ;; Create a new frame?
3251           (unless (setq frame (elt gnus-frame-list i))
3252             (nconc gnus-frame-list (list (setq frame (make-frame params))))
3253             (push frame gnus-created-frames))
3254           ;; Is the old frame still alive?
3255           (unless (frame-live-p frame)
3256             (setcar (nthcdr i gnus-frame-list)
3257                     (setq frame (make-frame params))))
3258           ;; Select the frame in question and do more splits there.
3259           (select-frame frame)
3260           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3261           (incf i))
3262         ;; Select the frame that has the selected buffer.
3263         (when fresult
3264           (select-frame (window-frame fresult)))))
3265      ;; This is a normal split.
3266      (t
3267       (when (> (length subs) 0)
3268         ;; First we have to compute the sizes of all new windows.
3269         (while subs
3270           (setq sub (append (pop subs) nil))
3271           (while (and (not (assq (car sub) gnus-window-to-buffer))
3272                       (gnus-functionp (car sub)))
3273             (setq sub (eval sub)))
3274           (when sub
3275             (push sub comp-subs)
3276             (setq size (cadar comp-subs))
3277             (cond ((equal size 1.0)
3278                    (setq rest (car comp-subs))
3279                    (setq s 0))
3280                   ((floatp size)
3281                    (setq s (floor (* size len))))
3282                   ((integerp size)
3283                    (setq s size))
3284                   (t
3285                    (error "Illegal size: %s" size)))
3286             ;; Try to make sure that we are inside the safe limits.
3287             (cond ((zerop s))
3288                   ((eq type 'horizontal)
3289                    (setq s (max s window-min-width)))
3290                   ((eq type 'vertical)
3291                    (setq s (max s window-min-height))))
3292             (setcar (cdar comp-subs) s)
3293             (incf total s)))
3294         ;; Take care of the "1.0" spec.
3295         (if rest
3296             (setcar (cdr rest) (- len total))
3297           (error "No 1.0 specs in %s" split))
3298         ;; The we do the actual splitting in a nice recursive
3299         ;; fashion.
3300         (setq comp-subs (nreverse comp-subs))
3301         (while comp-subs
3302           (if (null (cdr comp-subs))
3303               (setq new-win window)
3304             (setq new-win
3305                   (split-window window (cadar comp-subs)
3306                                 (eq type 'horizontal))))
3307           (setq result (or (gnus-configure-frame
3308                             (car comp-subs) window) result))
3309           (select-window new-win)
3310           (setq window new-win)
3311           (setq comp-subs (cdr comp-subs))))
3312       ;; Return the proper window, if any.
3313       (when result
3314         (select-window result))))))
3315
3316 (defvar gnus-frame-split-p nil)
3317
3318 (defun gnus-configure-windows (setting &optional force)
3319   (setq setting (gnus-windows-old-to-new setting))
3320   (let ((split (if (symbolp setting)
3321                    (cadr (assq setting gnus-buffer-configuration))
3322                  setting))
3323         all-visible)
3324
3325     (setq gnus-frame-split-p nil)
3326
3327     (unless split
3328       (error "No such setting: %s" setting))
3329
3330     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3331              (not force))
3332         ;; All the windows mentioned are already visible, so we just
3333         ;; put point in the assigned buffer, and do not touch the
3334         ;; winconf.
3335         (select-window all-visible)
3336
3337       ;; Either remove all windows or just remove all Gnus windows.
3338       (let ((frame (selected-frame)))
3339         (unwind-protect
3340             (if gnus-use-full-window
3341                 ;; We want to remove all other windows.
3342                 (if (not gnus-frame-split-p)
3343                     ;; This is not a `frame' split, so we ignore the
3344                     ;; other frames.  
3345                     (delete-other-windows)
3346                   ;; This is a `frame' split, so we delete all windows
3347                   ;; on all frames.
3348                   (mapcar 
3349                    (lambda (frame)
3350                      (unless (eq (cdr (assq 'minibuffer
3351                                             (frame-parameters frame)))
3352                                  'only)
3353                        (select-frame frame)
3354                        (delete-other-windows)))
3355                    (frame-list)))
3356               ;; Just remove some windows.
3357               (gnus-remove-some-windows)
3358               (switch-to-buffer nntp-server-buffer))
3359           (select-frame frame)))
3360
3361       (switch-to-buffer nntp-server-buffer)
3362       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3363
3364 (defun gnus-all-windows-visible-p (split)
3365   "Say whether all buffers in SPLIT are currently visible.
3366 In particular, the value returned will be the window that
3367 should have point."
3368   (let ((stack (list split))
3369         (all-visible t)
3370         type buffer win buf)
3371     (while (and (setq split (pop stack))
3372                 all-visible)
3373       ;; Be backwards compatible.
3374       (when (vectorp split)
3375         (setq split (append split nil)))
3376       (when (or (consp (car split))
3377                 (vectorp (car split)))
3378         (push 1.0 split)
3379         (push 'vertical split))
3380       ;; The SPLIT might be something that is to be evaled to
3381       ;; return a new SPLIT.
3382       (while (and (not (assq (car split) gnus-window-to-buffer))
3383                   (gnus-functionp (car split)))
3384         (setq split (eval split)))
3385
3386       (setq type (elt split 0))
3387       (cond
3388        ;; Nothing here.
3389        ((null split) t)
3390        ;; A buffer.
3391        ((not (memq type '(horizontal vertical frame)))
3392         (setq buffer (cond ((stringp type) type)
3393                            (t (cdr (assq type gnus-window-to-buffer)))))
3394         (unless buffer
3395           (error "Illegal buffer type: %s" type))
3396         (when (setq buf (get-buffer (if (symbolp buffer)
3397                                         (symbol-value buffer)
3398                                       buffer)))
3399           (setq win (get-buffer-window buf t)))
3400         (if win
3401             (when (memq 'point split)
3402                 (setq all-visible win))
3403           (setq all-visible nil)))
3404        (t
3405         (when (eq type 'frame)
3406           (setq gnus-frame-split-p t))
3407         (setq stack (append (cddr split) stack)))))
3408     (unless (eq all-visible t)
3409       all-visible)))
3410
3411 (defun gnus-window-top-edge (&optional window)
3412   (nth 1 (window-edges window)))
3413
3414 (defun gnus-remove-some-windows ()
3415   (let ((buffers gnus-window-to-buffer)
3416         buf bufs lowest-buf lowest)
3417     (save-excursion
3418       ;; Remove windows on all known Gnus buffers.
3419       (while buffers
3420         (setq buf (cdar buffers))
3421         (if (symbolp buf)
3422             (setq buf (and (boundp buf) (symbol-value buf))))
3423         (and buf
3424              (get-buffer-window buf)
3425              (progn
3426                (setq bufs (cons buf bufs))
3427                (pop-to-buffer buf)
3428                (if (or (not lowest)
3429                        (< (gnus-window-top-edge) lowest))
3430                    (progn
3431                      (setq lowest (gnus-window-top-edge))
3432                      (setq lowest-buf buf)))))
3433         (setq buffers (cdr buffers)))
3434       ;; Remove windows on *all* summary buffers.
3435       (walk-windows
3436        (lambda (win)
3437          (let ((buf (window-buffer win)))
3438            (if (string-match    "^\\*Summary" (buffer-name buf))
3439                (progn
3440                  (setq bufs (cons buf bufs))
3441                  (pop-to-buffer buf)
3442                  (if (or (not lowest)
3443                          (< (gnus-window-top-edge) lowest))
3444                      (progn
3445                        (setq lowest-buf buf)
3446                        (setq lowest (gnus-window-top-edge)))))))))
3447       (and lowest-buf
3448            (progn
3449              (pop-to-buffer lowest-buf)
3450              (switch-to-buffer nntp-server-buffer)))
3451       (while bufs
3452         (and (not (eq (car bufs) lowest-buf))
3453              (delete-windows-on (car bufs)))
3454         (setq bufs (cdr bufs))))))
3455
3456 (defun gnus-version ()
3457   "Version numbers of this version of Gnus."
3458   (interactive)
3459   (let ((methods gnus-valid-select-methods)
3460         (mess gnus-version)
3461         meth)
3462     ;; Go through all the legal select methods and add their version
3463     ;; numbers to the total version string.  Only the backends that are
3464     ;; currently in use will have their message numbers taken into
3465     ;; consideration.
3466     (while methods
3467       (setq meth (intern (concat (caar methods) "-version")))
3468       (and (boundp meth)
3469            (stringp (symbol-value meth))
3470            (setq mess (concat mess "; " (symbol-value meth))))
3471       (setq methods (cdr methods)))
3472     (gnus-message 2 mess)))
3473
3474 (defun gnus-info-find-node ()
3475   "Find Info documentation of Gnus."
3476   (interactive)
3477   ;; Enlarge info window if needed.
3478   (let ((mode major-mode)
3479         gnus-info-buffer)
3480     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3481     (setq gnus-info-buffer (current-buffer))
3482     (gnus-configure-windows 'info)))
3483
3484 (defun gnus-days-between (date1 date2)
3485   ;; Return the number of days between date1 and date2.
3486   (- (gnus-day-number date1) (gnus-day-number date2)))
3487
3488 (defun gnus-day-number (date)
3489   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3490                      (timezone-parse-date date))))
3491     (timezone-absolute-from-gregorian
3492      (nth 1 dat) (nth 2 dat) (car dat))))
3493
3494 (defun gnus-encode-date (date)
3495   "Convert DATE to internal time."
3496   (let* ((parse (timezone-parse-date date))
3497          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3498          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3499     (encode-time (caddr time) (cadr time) (car time)
3500                  (caddr date) (cadr date) (car date) (nth 4 date))))
3501
3502 (defun gnus-time-minus (t1 t2)
3503   "Subtract two internal times."
3504   (let ((borrow (< (cadr t1) (cadr t2))))
3505     (list (- (car t1) (car t2) (if borrow 1 0))
3506           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3507
3508 (defun gnus-file-newer-than (file date)
3509   (let ((fdate (nth 5 (file-attributes file))))
3510     (or (> (car fdate) (car date))
3511         (and (= (car fdate) (car date))
3512              (> (nth 1 fdate) (nth 1 date))))))
3513
3514 (defmacro gnus-local-set-keys (&rest plist)
3515   "Set the keys in PLIST in the current keymap."
3516   `(gnus-define-keys-1 (current-local-map) ',plist))
3517
3518 (defmacro gnus-define-keys (keymap &rest plist)
3519   "Define all keys in PLIST in KEYMAP."
3520   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3521
3522 (put 'gnus-define-keys 'lisp-indent-function 1)
3523 (put 'gnus-define-keys 'lisp-indent-hook 1)
3524 (put 'gnus-define-keymap 'lisp-indent-function 1)
3525 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3526
3527 (defmacro gnus-define-keymap (keymap &rest plist)
3528   "Define all keys in PLIST in KEYMAP."
3529   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3530
3531 (defun gnus-define-keys-1 (keymap plist)
3532   (when (null keymap)
3533     (error "Can't set keys in a null keymap"))
3534   (cond ((symbolp keymap)
3535          (setq keymap (symbol-value keymap)))
3536         ((keymapp keymap))
3537         ((listp keymap)
3538          (set (car keymap) nil)
3539          (define-prefix-command (car keymap))
3540          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3541          (setq keymap (symbol-value (car keymap)))))
3542   (let (key)
3543     (while plist
3544       (when (symbolp (setq key (pop plist)))
3545         (setq key (symbol-value key)))
3546       (define-key keymap key (pop plist)))))
3547
3548 (defun gnus-group-read-only-p (&optional group)
3549   "Check whether GROUP supports editing or not.
3550 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3551 that that variable is buffer-local to the summary buffers."
3552   (let ((group (or group gnus-newsgroup-name)))
3553     (not (gnus-check-backend-function 'request-replace-article group))))
3554
3555 (defun gnus-group-total-expirable-p (group)
3556   "Check whether GROUP is total-expirable or not."
3557   (let ((params (gnus-info-params (gnus-get-info group))))
3558     (or (memq 'total-expire params)
3559         (cdr (assq 'total-expire params)) ; (total-expire . t)
3560         (and gnus-total-expirable-newsgroups ; Check var.
3561              (string-match gnus-total-expirable-newsgroups group)))))
3562
3563 (defun gnus-group-auto-expirable-p (group)
3564   "Check whether GROUP is total-expirable or not."
3565   (let ((params (gnus-info-params (gnus-get-info group))))
3566     (or (memq 'auto-expire params)
3567         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3568         (and gnus-auto-expirable-newsgroups ; Check var.
3569              (string-match gnus-auto-expirable-newsgroups group)))))
3570
3571 (defun gnus-virtual-group-p (group)
3572   "Say whether GROUP is virtual or not."
3573   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3574                         gnus-valid-select-methods)))
3575
3576 (defun gnus-news-group-p (group &optional article)
3577   "Return non-nil if GROUP (and ARTICLE) come from a news server."
3578   (or (gnus-member-of-valid 'post group) ; Ordinary news group.
3579       (and (gnus-member-of-valid 'post-mail group) ; Combined group.
3580            (eq (gnus-request-type group article) 'news))))
3581
3582 (defsubst gnus-simplify-subject-fully (subject)
3583   "Simplify a subject string according to the user's wishes."
3584   (cond
3585    ((null gnus-summary-gather-subject-limit)
3586     (gnus-simplify-subject-re subject))
3587    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3588     (gnus-simplify-subject-fuzzy subject))
3589    ((numberp gnus-summary-gather-subject-limit)
3590     (gnus-limit-string (gnus-simplify-subject-re subject)
3591                        gnus-summary-gather-subject-limit))
3592    (t
3593     subject)))
3594
3595 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3596   "Check whether two subjects are equal.  If optional argument
3597 simple-first is t, first argument is already simplified."
3598   (cond
3599    ((null simple-first)
3600     (equal (gnus-simplify-subject-fully s1)
3601            (gnus-simplify-subject-fully s2)))
3602    (t
3603     (equal s1
3604            (gnus-simplify-subject-fully s2)))))
3605
3606 ;; Returns a list of writable groups.
3607 (defun gnus-writable-groups ()
3608   (let ((alist gnus-newsrc-alist)
3609         groups group)
3610     (while (setq group (car (pop alist)))
3611       (unless (gnus-group-read-only-p group)
3612         (push group groups)))
3613     (nreverse groups)))
3614
3615 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3616 ;; the echo area.
3617 (defun gnus-y-or-n-p (prompt)
3618   (prog1
3619       (y-or-n-p prompt)
3620     (message "")))
3621
3622 (defun gnus-yes-or-no-p (prompt)
3623   (prog1
3624       (yes-or-no-p prompt)
3625     (message "")))
3626
3627 ;; Check whether to use long file names.
3628 (defun gnus-use-long-file-name (symbol)
3629   ;; The variable has to be set...
3630   (and gnus-use-long-file-name
3631        ;; If it isn't a list, then we return t.
3632        (or (not (listp gnus-use-long-file-name))
3633            ;; If it is a list, and the list contains `symbol', we
3634            ;; return nil.
3635            (not (memq symbol gnus-use-long-file-name)))))
3636
3637 ;; I suspect there's a better way, but I haven't taken the time to do
3638 ;; it yet. -erik selberg@cs.washington.edu
3639 (defun gnus-dd-mmm (messy-date)
3640   "Return a string like DD-MMM from a big messy string"
3641   (let ((datevec (timezone-parse-date messy-date)))
3642     (format "%2s-%s"
3643             (condition-case ()
3644                 ;; Make sure leading zeroes are stripped.
3645                 (number-to-string (string-to-number (aref datevec 2)))
3646               (error "??"))
3647             (capitalize
3648              (or (car
3649                   (nth (1- (string-to-number (aref datevec 1)))
3650                        timezone-months-assoc))
3651                  "???")))))
3652
3653 ;; Make a hash table (default and minimum size is 255).
3654 ;; Optional argument HASHSIZE specifies the table size.
3655 (defun gnus-make-hashtable (&optional hashsize)
3656   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3657
3658 ;; Make a number that is suitable for hashing; bigger than MIN and one
3659 ;; less than 2^x.
3660 (defun gnus-create-hash-size (min)
3661   (let ((i 1))
3662     (while (< i min)
3663       (setq i (* 2 i)))
3664     (1- i)))
3665
3666 ;; Show message if message has a lower level than `gnus-verbose'.
3667 ;; Guideline for numbers:
3668 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3669 ;; for things that take a long time, 7 - not very important messages
3670 ;; on stuff, 9 - messages inside loops.
3671 (defun gnus-message (level &rest args)
3672   (if (<= level gnus-verbose)
3673       (apply 'message args)
3674     ;; We have to do this format thingy here even if the result isn't
3675     ;; shown - the return value has to be the same as the return value
3676     ;; from `message'.
3677     (apply 'format args)))
3678
3679 ;; Generate a unique new group name.
3680 (defun gnus-generate-new-group-name (leaf)
3681   (let ((name leaf)
3682         (num 0))
3683     (while (gnus-gethash name gnus-newsrc-hashtb)
3684       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3685     name))
3686
3687 (defsubst gnus-hide-text (b e props)
3688   "Set text PROPS on the B to E region, extending `intangble' 1 past B."
3689   (add-text-properties b e props)
3690   (when (memq 'intangible props)
3691     (put-text-property (max (1- b) (point-min))
3692                        b 'intangible (cddr (memq 'intangible props)))))
3693
3694 (defsubst gnus-unhide-text (b e)
3695   "Remove hidden text properties from region between B and E."
3696   (remove-text-properties b e gnus-hidden-properties)
3697   (when (memq 'intangible gnus-hidden-properties)
3698     (put-text-property (max (1- b) (point-min))
3699                        b 'intangible nil)))
3700
3701 (defun gnus-hide-text-type (b e type)
3702   "Hide text of TYPE between B and E."
3703   (gnus-hide-text b e (cons 'gnus-type (cons type gnus-hidden-properties))))
3704
3705 ;; Find out whether the gnus-visual TYPE is wanted.
3706 (defun gnus-visual-p (&optional type class)
3707   (and gnus-visual                      ; Has to be non-nil, at least.
3708        (if (not type)                   ; We don't care about type.
3709            gnus-visual
3710          (if (listp gnus-visual)        ; It's a list, so we check it.
3711              (or (memq type gnus-visual)
3712                  (memq class gnus-visual))
3713            t))))
3714
3715 (defun gnus-parent-id (references)
3716   "Return the last Message-ID in REFERENCES."
3717   (when (and references
3718              (string-match "\\(<[^<>]+>\\)[ \t\n]*\\'" references))
3719     (substring references (match-beginning 1) (match-end 1))))
3720
3721 (defun gnus-split-references (references)
3722   "Return a list of Message-IDs in REFERENCES."
3723   (let ((beg 0)
3724         ids)
3725     (while (string-match "<[^>]+>" references beg)
3726       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3727             ids))
3728     (nreverse ids)))
3729
3730 (defun gnus-buffer-live-p (buffer)
3731   "Say whether BUFFER is alive or not."
3732   (and buffer
3733        (get-buffer buffer)
3734        (buffer-name (get-buffer buffer))))
3735
3736 (defun gnus-ephemeral-group-p (group)
3737   "Say whether GROUP is ephemeral or not."
3738   (gnus-group-get-parameter group 'quit-config))
3739
3740 (defun gnus-group-quit-config (group)
3741   "Return the quit-config of GROUP."
3742   (gnus-group-get-parameter group 'quit-config))
3743
3744 (defun gnus-simplify-mode-line ()
3745   "Make mode lines a bit simpler."
3746   (setq mode-line-modified "-- ")
3747   (when (listp mode-line-format)
3748     (make-local-variable 'mode-line-format)
3749     (setq mode-line-format (copy-sequence mode-line-format))
3750     (when (equal (nth 3 mode-line-format) "   ")
3751       (setcar (nthcdr 3 mode-line-format) " "))))
3752
3753 ;;; List and range functions
3754
3755 (defun gnus-last-element (list)
3756   "Return last element of LIST."
3757   (while (cdr list)
3758     (setq list (cdr list)))
3759   (car list))
3760
3761 (defun gnus-copy-sequence (list)
3762   "Do a complete, total copy of a list."
3763   (if (and (consp list) (not (consp (cdr list))))
3764       (cons (car list) (cdr list))
3765     (mapcar (lambda (elem) (if (consp elem)
3766                                (if (consp (cdr elem))
3767                                    (gnus-copy-sequence elem)
3768                                  (cons (car elem) (cdr elem)))
3769                              elem))
3770             list)))
3771
3772 (defun gnus-set-difference (list1 list2)
3773   "Return a list of elements of LIST1 that do not appear in LIST2."
3774   (let ((list1 (copy-sequence list1)))
3775     (while list2
3776       (setq list1 (delq (car list2) list1))
3777       (setq list2 (cdr list2)))
3778     list1))
3779
3780 (defun gnus-sorted-complement (list1 list2)
3781   "Return a list of elements of LIST1 that do not appear in LIST2.
3782 Both lists have to be sorted over <."
3783   (let (out)
3784     (if (or (null list1) (null list2))
3785         (or list1 list2)
3786       (while (and list1 list2)
3787         (cond ((= (car list1) (car list2))
3788                (setq list1 (cdr list1)
3789                      list2 (cdr list2)))
3790               ((< (car list1) (car list2))
3791                (setq out (cons (car list1) out))
3792                (setq list1 (cdr list1)))
3793               (t
3794                (setq out (cons (car list2) out))
3795                (setq list2 (cdr list2)))))
3796       (nconc (nreverse out) (or list1 list2)))))
3797
3798 (defun gnus-intersection (list1 list2)
3799   (let ((result nil))
3800     (while list2
3801       (if (memq (car list2) list1)
3802           (setq result (cons (car list2) result)))
3803       (setq list2 (cdr list2)))
3804     result))
3805
3806 (defun gnus-sorted-intersection (list1 list2)
3807   ;; LIST1 and LIST2 have to be sorted over <.
3808   (let (out)
3809     (while (and list1 list2)
3810       (cond ((= (car list1) (car list2))
3811              (setq out (cons (car list1) out)
3812                    list1 (cdr list1)
3813                    list2 (cdr list2)))
3814             ((< (car list1) (car list2))
3815              (setq list1 (cdr list1)))
3816             (t
3817              (setq list2 (cdr list2)))))
3818     (nreverse out)))
3819
3820 (defun gnus-set-sorted-intersection (list1 list2)
3821   ;; LIST1 and LIST2 have to be sorted over <.
3822   ;; This function modifies LIST1.
3823   (let* ((top (cons nil list1))
3824          (prev top))
3825     (while (and list1 list2)
3826       (cond ((= (car list1) (car list2))
3827              (setq prev list1
3828                    list1 (cdr list1)
3829                    list2 (cdr list2)))
3830             ((< (car list1) (car list2))
3831              (setcdr prev (cdr list1))
3832              (setq list1 (cdr list1)))
3833             (t
3834              (setq list2 (cdr list2)))))
3835     (setcdr prev nil)
3836     (cdr top)))
3837
3838 (defun gnus-compress-sequence (numbers &optional always-list)
3839   "Convert list of numbers to a list of ranges or a single range.
3840 If ALWAYS-LIST is non-nil, this function will always release a list of
3841 ranges."
3842   (let* ((first (car numbers))
3843          (last (car numbers))
3844          result)
3845     (if (null numbers)
3846         nil
3847       (if (not (listp (cdr numbers)))
3848           numbers
3849         (while numbers
3850           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3851                 ((= (1+ last) (car numbers)) ;Still in sequence
3852                  (setq last (car numbers)))
3853                 (t                      ;End of one sequence
3854                  (setq result
3855                        (cons (if (= first last) first
3856                                (cons first last)) result))
3857                  (setq first (car numbers))
3858                  (setq last  (car numbers))))
3859           (setq numbers (cdr numbers)))
3860         (if (and (not always-list) (null result))
3861             (if (= first last) (list first) (cons first last))
3862           (nreverse (cons (if (= first last) first (cons first last))
3863                           result)))))))
3864
3865 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3866 (defun gnus-uncompress-range (ranges)
3867   "Expand a list of ranges into a list of numbers.
3868 RANGES is either a single range on the form `(num . num)' or a list of
3869 these ranges."
3870   (let (first last result)
3871     (cond
3872      ((null ranges)
3873       nil)
3874      ((not (listp (cdr ranges)))
3875       (setq first (car ranges))
3876       (setq last (cdr ranges))
3877       (while (<= first last)
3878         (setq result (cons first result))
3879         (setq first (1+ first)))
3880       (nreverse result))
3881      (t
3882       (while ranges
3883         (if (atom (car ranges))
3884             (if (numberp (car ranges))
3885                 (setq result (cons (car ranges) result)))
3886           (setq first (caar ranges))
3887           (setq last  (cdar ranges))
3888           (while (<= first last)
3889             (setq result (cons first result))
3890             (setq first (1+ first))))
3891         (setq ranges (cdr ranges)))
3892       (nreverse result)))))
3893
3894 (defun gnus-add-to-range (ranges list)
3895   "Return a list of ranges that has all articles from both RANGES and LIST.
3896 Note: LIST has to be sorted over `<'."
3897   (if (not ranges)
3898       (gnus-compress-sequence list t)
3899     (setq list (copy-sequence list))
3900     (or (listp (cdr ranges))
3901         (setq ranges (list ranges)))
3902     (let ((out ranges)
3903           ilist lowest highest temp)
3904       (while (and ranges list)
3905         (setq ilist list)
3906         (setq lowest (or (and (atom (car ranges)) (car ranges))
3907                          (caar ranges)))
3908         (while (and list (cdr list) (< (cadr list) lowest))
3909           (setq list (cdr list)))
3910         (if (< (car ilist) lowest)
3911             (progn
3912               (setq temp list)
3913               (setq list (cdr list))
3914               (setcdr temp nil)
3915               (setq out (nconc (gnus-compress-sequence ilist t) out))))
3916         (setq highest (or (and (atom (car ranges)) (car ranges))
3917                           (cdar ranges)))
3918         (while (and list (<= (car list) highest))
3919           (setq list (cdr list)))
3920         (setq ranges (cdr ranges)))
3921       (if list
3922           (setq out (nconc (gnus-compress-sequence list t) out)))
3923       (setq out (sort out (lambda (r1 r2)
3924                             (< (or (and (atom r1) r1) (car r1))
3925                                (or (and (atom r2) r2) (car r2))))))
3926       (setq ranges out)
3927       (while ranges
3928         (if (atom (car ranges))
3929             (if (cdr ranges)
3930                 (if (atom (cadr ranges))
3931                     (if (= (1+ (car ranges)) (cadr ranges))
3932                         (progn
3933                           (setcar ranges (cons (car ranges)
3934                                                (cadr ranges)))
3935                           (setcdr ranges (cddr ranges))))
3936                   (if (= (1+ (car ranges)) (caadr ranges))
3937                       (progn
3938                         (setcar (cadr ranges) (car ranges))
3939                         (setcar ranges (cadr ranges))
3940                         (setcdr ranges (cddr ranges))))))
3941           (if (cdr ranges)
3942               (if (atom (cadr ranges))
3943                   (if (= (1+ (cdar ranges)) (cadr ranges))
3944                       (progn
3945                         (setcdr (car ranges) (cadr ranges))
3946                         (setcdr ranges (cddr ranges))))
3947                 (if (= (1+ (cdar ranges)) (caadr ranges))
3948                     (progn
3949                       (setcdr (car ranges) (cdadr ranges))
3950                       (setcdr ranges (cddr ranges)))))))
3951         (setq ranges (cdr ranges)))
3952       out)))
3953
3954 (defun gnus-remove-from-range (ranges list)
3955   "Return a list of ranges that has all articles from LIST removed from RANGES.
3956 Note: LIST has to be sorted over `<'."
3957   ;; !!! This function shouldn't look like this, but I've got a headache.
3958   (gnus-compress-sequence
3959    (gnus-sorted-complement
3960     (gnus-uncompress-range ranges) list)))
3961
3962 (defun gnus-member-of-range (number ranges)
3963   (if (not (listp (cdr ranges)))
3964       (and (>= number (car ranges))
3965            (<= number (cdr ranges)))
3966     (let ((not-stop t))
3967       (while (and ranges
3968                   (if (numberp (car ranges))
3969                       (>= number (car ranges))
3970                     (>= number (caar ranges)))
3971                   not-stop)
3972         (if (if (numberp (car ranges))
3973                 (= number (car ranges))
3974               (and (>= number (caar ranges))
3975                    (<= number (cdar ranges))))
3976             (setq not-stop nil))
3977         (setq ranges (cdr ranges)))
3978       (not not-stop))))
3979
3980 (defun gnus-range-length (range)
3981   "Return the length RANGE would have if uncompressed."
3982   (length (gnus-uncompress-range range)))
3983
3984 (defun gnus-sublist-p (list sublist)
3985   "Test whether all elements in SUBLIST are members of LIST."
3986   (let ((sublistp t))
3987     (while sublist
3988       (unless (memq (pop sublist) list)
3989         (setq sublistp nil
3990               sublist nil)))
3991     sublistp))
3992
3993 \f
3994 ;;;
3995 ;;; Gnus group mode
3996 ;;;
3997
3998 (defvar gnus-group-mode-map nil)
3999 (put 'gnus-group-mode 'mode-class 'special)
4000
4001 (unless gnus-group-mode-map
4002   (setq gnus-group-mode-map (make-keymap))
4003   (suppress-keymap gnus-group-mode-map)
4004
4005   (gnus-define-keys gnus-group-mode-map
4006     " " gnus-group-read-group
4007     "=" gnus-group-select-group
4008     "\r" gnus-group-select-group
4009     "\M-\r" gnus-group-quick-select-group
4010     "j" gnus-group-jump-to-group
4011     "n" gnus-group-next-unread-group
4012     "p" gnus-group-prev-unread-group
4013     "\177" gnus-group-prev-unread-group
4014     [delete] gnus-group-prev-unread-group
4015     "N" gnus-group-next-group
4016     "P" gnus-group-prev-group
4017     "\M-n" gnus-group-next-unread-group-same-level
4018     "\M-p" gnus-group-prev-unread-group-same-level
4019     "," gnus-group-best-unread-group
4020     "." gnus-group-first-unread-group
4021     "u" gnus-group-unsubscribe-current-group
4022     "U" gnus-group-unsubscribe-group
4023     "c" gnus-group-catchup-current
4024     "C" gnus-group-catchup-current-all
4025     "l" gnus-group-list-groups
4026     "L" gnus-group-list-all-groups
4027     "m" gnus-group-mail
4028     "g" gnus-group-get-new-news
4029     "\M-g" gnus-group-get-new-news-this-group
4030     "R" gnus-group-restart
4031     "r" gnus-group-read-init-file
4032     "B" gnus-group-browse-foreign-server
4033     "b" gnus-group-check-bogus-groups
4034     "F" gnus-find-new-newsgroups
4035     "\C-c\C-d" gnus-group-describe-group
4036     "\M-d" gnus-group-describe-all-groups
4037     "\C-c\C-a" gnus-group-apropos
4038     "\C-c\M-\C-a" gnus-group-description-apropos
4039     "a" gnus-group-post-news
4040     "\ek" gnus-group-edit-local-kill
4041     "\eK" gnus-group-edit-global-kill
4042     "\C-k" gnus-group-kill-group
4043     "\C-y" gnus-group-yank-group
4044     "\C-w" gnus-group-kill-region
4045     "\C-x\C-t" gnus-group-transpose-groups
4046     "\C-c\C-l" gnus-group-list-killed
4047     "\C-c\C-x" gnus-group-expire-articles
4048     "\C-c\M-\C-x" gnus-group-expire-all-groups
4049     "V" gnus-version
4050     "s" gnus-group-save-newsrc
4051     "z" gnus-group-suspend
4052     "Z" gnus-group-clear-dribble
4053     "q" gnus-group-exit
4054     "Q" gnus-group-quit
4055     "?" gnus-group-describe-briefly
4056     "\C-c\C-i" gnus-info-find-node
4057     "\M-e" gnus-group-edit-group-method
4058     "^" gnus-group-enter-server-mode
4059     gnus-mouse-2 gnus-mouse-pick-group
4060     "<" beginning-of-buffer
4061     ">" end-of-buffer
4062     "\C-c\C-b" gnus-bug
4063     "\C-c\C-s" gnus-group-sort-groups
4064     "t" gnus-topic-mode
4065     "\C-c\M-g" gnus-activate-all-groups
4066     "\M-&" gnus-group-universal-argument
4067     "#" gnus-group-mark-group
4068     "\M-#" gnus-group-unmark-group)
4069
4070   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4071     "m" gnus-group-mark-group
4072     "u" gnus-group-unmark-group
4073     "w" gnus-group-mark-region
4074     "m" gnus-group-mark-buffer
4075     "r" gnus-group-mark-regexp
4076     "U" gnus-group-unmark-all-groups)
4077
4078   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4079     "d" gnus-group-make-directory-group
4080     "h" gnus-group-make-help-group
4081     "a" gnus-group-make-archive-group
4082     "k" gnus-group-make-kiboze-group
4083     "m" gnus-group-make-group
4084     "E" gnus-group-edit-group
4085     "e" gnus-group-edit-group-method
4086     "p" gnus-group-edit-group-parameters
4087     "v" gnus-group-add-to-virtual
4088     "V" gnus-group-make-empty-virtual
4089     "D" gnus-group-enter-directory
4090     "f" gnus-group-make-doc-group
4091     "r" gnus-group-rename-group
4092     "\177" gnus-group-delete-group
4093     [delete] gnus-group-delete-group)
4094
4095    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4096      "b" gnus-group-brew-soup
4097      "w" gnus-soup-save-areas
4098      "s" gnus-soup-send-replies
4099      "p" gnus-soup-pack-packet
4100      "r" nnsoup-pack-replies)
4101
4102    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4103      "s" gnus-group-sort-groups
4104      "a" gnus-group-sort-groups-by-alphabet
4105      "u" gnus-group-sort-groups-by-unread
4106      "l" gnus-group-sort-groups-by-level
4107      "v" gnus-group-sort-groups-by-score
4108      "r" gnus-group-sort-groups-by-rank
4109      "m" gnus-group-sort-groups-by-method)
4110
4111    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4112      "k" gnus-group-list-killed
4113      "z" gnus-group-list-zombies
4114      "s" gnus-group-list-groups
4115      "u" gnus-group-list-all-groups
4116      "A" gnus-group-list-active
4117      "a" gnus-group-apropos
4118      "d" gnus-group-description-apropos
4119      "m" gnus-group-list-matching
4120      "M" gnus-group-list-all-matching
4121      "l" gnus-group-list-level)
4122
4123    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4124      "f" gnus-score-flush-cache)
4125
4126    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4127      "f" gnus-group-fetch-faq)
4128
4129    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4130      "l" gnus-group-set-current-level
4131      "t" gnus-group-unsubscribe-current-group
4132      "s" gnus-group-unsubscribe-group
4133      "k" gnus-group-kill-group
4134      "y" gnus-group-yank-group
4135      "w" gnus-group-kill-region
4136      "\C-k" gnus-group-kill-level
4137      "z" gnus-group-kill-all-zombies))
4138
4139 (defun gnus-group-mode ()
4140   "Major mode for reading news.
4141
4142 All normal editing commands are switched off.
4143 \\<gnus-group-mode-map>
4144 The group buffer lists (some of) the groups available.  For instance,
4145 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4146 lists all zombie groups.
4147
4148 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4149 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4150
4151 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4152
4153 The following commands are available:
4154
4155 \\{gnus-group-mode-map}"
4156   (interactive)
4157   (when (and menu-bar-mode
4158              (gnus-visual-p 'group-menu 'menu))
4159     (gnus-group-make-menu-bar))
4160   (kill-all-local-variables)
4161   (gnus-simplify-mode-line)
4162   (setq major-mode 'gnus-group-mode)
4163   (setq mode-name "Group")
4164   (gnus-group-set-mode-line)
4165   (setq mode-line-process nil)
4166   (use-local-map gnus-group-mode-map)
4167   (buffer-disable-undo (current-buffer))
4168   (setq truncate-lines t)
4169   (setq buffer-read-only t)
4170   (run-hooks 'gnus-group-mode-hook))
4171
4172 (defun gnus-mouse-pick-group (e)
4173   "Enter the group under the mouse pointer."
4174   (interactive "e")
4175   (mouse-set-point e)
4176   (gnus-group-read-group nil))
4177
4178 ;; Look at LEVEL and find out what the level is really supposed to be.
4179 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4180 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4181 (defun gnus-group-default-level (&optional level number-or-nil)
4182   (cond
4183    (gnus-group-use-permanent-levels
4184 ;    (setq gnus-group-default-list-level
4185 ;         (or level gnus-group-default-list-level))
4186     (or level gnus-group-default-list-level gnus-level-subscribed))
4187    (number-or-nil
4188     level)
4189    (t
4190     (or level gnus-group-default-list-level gnus-level-subscribed))))
4191
4192 ;;;###autoload
4193 (defun gnus-slave-no-server (&optional arg)
4194   "Read network news as a slave, without connecting to local server"
4195   (interactive "P")
4196   (gnus-no-server arg t))
4197
4198 ;;;###autoload
4199 (defun gnus-no-server (&optional arg slave)
4200   "Read network news.
4201 If ARG is a positive number, Gnus will use that as the
4202 startup level.  If ARG is nil, Gnus will be started at level 2.
4203 If ARG is non-nil and not a positive number, Gnus will
4204 prompt the user for the name of an NNTP server to use.
4205 As opposed to `gnus', this command will not connect to the local server."
4206   (interactive "P")
4207   (let ((gnus-group-use-permanent-levels t))
4208     (gnus (or arg (1- gnus-level-default-subscribed)) t slave))
4209   (make-local-variable 'gnus-group-use-permanent-levels)
4210   (setq gnus-group-use-permanent-levels t))
4211
4212 ;;;###autoload
4213 (defun gnus-slave (&optional arg)
4214   "Read news as a slave."
4215   (interactive "P")
4216   (gnus arg nil 'slave))
4217
4218 ;;;###autoload
4219 (defun gnus-other-frame (&optional arg)
4220   "Pop up a frame to read news."
4221   (interactive "P")
4222   (if (get-buffer gnus-group-buffer)
4223       (let ((pop-up-frames t))
4224         (gnus arg))
4225     (select-frame (make-frame))
4226     (gnus arg)))
4227
4228 ;;;###autoload
4229 (defun gnus (&optional arg dont-connect slave)
4230   "Read network news.
4231 If ARG is non-nil and a positive number, Gnus will use that as the
4232 startup level.  If ARG is non-nil and not a positive number, Gnus will
4233 prompt the user for the name of an NNTP server to use."
4234   (interactive "P")
4235
4236   (if (get-buffer gnus-group-buffer)
4237       (progn
4238         (switch-to-buffer gnus-group-buffer)
4239         (gnus-group-get-new-news))
4240
4241     (gnus-clear-system)
4242     (nnheader-init-server-buffer)
4243     (gnus-read-init-file)
4244     (setq gnus-slave slave)
4245
4246     (gnus-group-setup-buffer)
4247     (let ((buffer-read-only nil))
4248       (erase-buffer)
4249       (if (not gnus-inhibit-startup-message)
4250           (progn
4251             (gnus-group-startup-message)
4252             (sit-for 0))))
4253
4254     (let ((level (and (numberp arg) (> arg 0) arg))
4255           did-connect)
4256       (unwind-protect
4257           (progn
4258             (or dont-connect
4259                 (setq did-connect
4260                       (gnus-start-news-server (and arg (not level))))))
4261         (if (and (not dont-connect)
4262                  (not did-connect))
4263             (gnus-group-quit)
4264           (run-hooks 'gnus-startup-hook)
4265           ;; NNTP server is successfully open.
4266
4267           ;; Find the current startup file name.
4268           (setq gnus-current-startup-file
4269                 (gnus-make-newsrc-file gnus-startup-file))
4270
4271           ;; Read the dribble file.
4272           (and (or gnus-slave gnus-use-dribble-file) (gnus-dribble-read-file))
4273
4274           ;; Allow using GroupLens predictions.
4275           (when gnus-use-grouplens
4276             (bbb-login)
4277             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4278
4279           (gnus-summary-make-display-table)
4280           ;; Do the actual startup.
4281           (gnus-setup-news nil level dont-connect)
4282           ;; Generate the group buffer.
4283           (gnus-group-list-groups level)
4284           (gnus-group-first-unread-group)
4285           (gnus-configure-windows 'group)
4286           (gnus-group-set-mode-line))))))
4287
4288 (defun gnus-unload ()
4289   "Unload all Gnus features."
4290   (interactive)
4291   (or (boundp 'load-history)
4292       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4293   (let ((history load-history)
4294         feature)
4295     (while history
4296       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4297            (setq feature (cdr (assq 'provide (car history))))
4298            (unload-feature feature 'force))
4299       (setq history (cdr history)))))
4300
4301 (defun gnus-compile ()
4302   "Byte-compile the user-defined format specs."
4303   (interactive)
4304   (let ((entries gnus-format-specs)
4305         entry gnus-tmp-func)
4306     (save-excursion
4307       (gnus-message 7 "Compiling format specs...")
4308
4309       (while entries
4310         (setq entry (pop entries))
4311         (if (eq (car entry) 'version)
4312             (setq gnus-format-specs (delq entry gnus-format-specs))
4313           (when (and (listp (caddr entry))
4314                      (not (eq 'byte-code (caaddr entry))))
4315             (fset 'gnus-tmp-func
4316                   `(lambda () ,(caddr entry)))
4317             (byte-compile 'gnus-tmp-func)
4318             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4319
4320       (push (cons 'version emacs-version) gnus-format-specs)
4321
4322       (gnus-message 7 "Compiling user specs...done"))))
4323
4324 (defun gnus-indent-rigidly (start end arg)
4325   "Indent rigidly using only spaces and no tabs."
4326   (save-excursion
4327     (save-restriction
4328       (narrow-to-region start end)
4329       (indent-rigidly start end arg)
4330       (goto-char (point-min))
4331       (while (search-forward "\t" nil t)
4332         (replace-match "        " t t)))))
4333
4334 (defun gnus-group-startup-message (&optional x y)
4335   "Insert startup message in current buffer."
4336   ;; Insert the message.
4337   (erase-buffer)
4338   (insert
4339    (format "              %s
4340           _    ___ _             _
4341           _ ___ __ ___  __    _ ___
4342           __   _     ___    __  ___
4343               _           ___     _
4344              _  _ __             _
4345              ___   __            _
4346                    __           _
4347                     _      _   _
4348                    _      _    _
4349                       _  _    _
4350                   __  ___
4351                  _   _ _     _
4352                 _   _
4353               _    _
4354              _    _
4355             _
4356           __
4357
4358 "
4359            ""))
4360   ;; And then hack it.
4361   (gnus-indent-rigidly (point-min) (point-max)
4362                        (/ (max (- (window-width) (or x 46)) 0) 2))
4363   (goto-char (point-min))
4364   (forward-line 1)
4365   (let* ((pheight (count-lines (point-min) (point-max)))
4366          (wheight (window-height))
4367          (rest (- wheight pheight)))
4368     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4369   ;; Fontify some.
4370   (goto-char (point-min))
4371   (and (search-forward "Praxis" nil t)
4372        (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4373   (goto-char (point-min))
4374   (let* ((mode-string (gnus-group-set-mode-line)))
4375     (setq mode-line-buffer-identification
4376           (list (concat gnus-version (substring (car mode-string) 4))))
4377     (set-buffer-modified-p t)))
4378
4379 (defun gnus-group-setup-buffer ()
4380   (or (get-buffer gnus-group-buffer)
4381       (progn
4382         (switch-to-buffer gnus-group-buffer)
4383         (gnus-add-current-to-buffer-list)
4384         (gnus-group-mode)
4385         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4386
4387 (defun gnus-group-list-groups (&optional level unread lowest)
4388   "List newsgroups with level LEVEL or lower that have unread articles.
4389 Default is all subscribed groups.
4390 If argument UNREAD is non-nil, groups with no unread articles are also
4391 listed."
4392   (interactive (list (if current-prefix-arg
4393                          (prefix-numeric-value current-prefix-arg)
4394                        (or
4395                         (gnus-group-default-level nil t)
4396                         gnus-group-default-list-level
4397                         gnus-level-subscribed))))
4398   (or level
4399       (setq level (car gnus-group-list-mode)
4400             unread (cdr gnus-group-list-mode)))
4401   (setq level (gnus-group-default-level level))
4402   (gnus-group-setup-buffer)             ;May call from out of group buffer
4403   (gnus-update-format-specifications)
4404   (let ((case-fold-search nil)
4405         (props (text-properties-at (gnus-point-at-bol)))
4406         (group (gnus-group-group-name)))
4407     (set-buffer gnus-group-buffer)
4408     (funcall gnus-group-prepare-function level unread lowest)
4409     (if (zerop (buffer-size))
4410         (gnus-message 5 gnus-no-groups-message)
4411       (goto-char (point-max))
4412       (when (or (not gnus-group-goto-next-group-function)
4413                 (not (funcall gnus-group-goto-next-group-function 
4414                               group props)))
4415         (if (not group)
4416             ;; Go to the first group with unread articles.
4417             (gnus-group-search-forward t)
4418           ;; Find the right group to put point on.  If the current group
4419           ;; has disappeared in the new listing, try to find the next
4420           ;; one.        If no next one can be found, just leave point at the
4421           ;; first newsgroup in the buffer.
4422           (if (not (gnus-goto-char
4423                     (text-property-any
4424                      (point-min) (point-max)
4425                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4426               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4427                 (while (and newsrc
4428                             (not (gnus-goto-char
4429                                   (text-property-any
4430                                    (point-min) (point-max) 'gnus-group
4431                                    (gnus-intern-safe
4432                                     (caar newsrc) gnus-active-hashtb)))))
4433                   (setq newsrc (cdr newsrc)))
4434                 (or newsrc (progn (goto-char (point-max))
4435                                   (forward-line -1)))))))
4436       ;; Adjust cursor point.
4437       (gnus-group-position-point))))
4438
4439 (defun gnus-group-list-level (level &optional all)
4440   "List groups on LEVEL.
4441 If ALL (the prefix), also list groups that have no unread articles."
4442   (interactive "nList groups on level: \nP")
4443   (gnus-group-list-groups level all level))
4444
4445 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4446   "List all newsgroups with unread articles of level LEVEL or lower.
4447 If ALL is non-nil, list groups that have no unread articles.
4448 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4449 If REGEXP, only list groups matching REGEXP."
4450   (set-buffer gnus-group-buffer)
4451   (let ((buffer-read-only nil)
4452         (newsrc (cdr gnus-newsrc-alist))
4453         (lowest (or lowest 1))
4454         info clevel unread group params)
4455     (erase-buffer)
4456     (if (< lowest gnus-level-zombie)
4457         ;; List living groups.
4458         (while newsrc
4459           (setq info (car newsrc)
4460                 group (gnus-info-group info)
4461                 params (gnus-info-params info)
4462                 newsrc (cdr newsrc)
4463                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4464           (and unread                   ; This group might be bogus
4465                (or (not regexp)
4466                    (string-match regexp group))
4467                (<= (setq clevel (gnus-info-level info)) level)
4468                (>= clevel lowest)
4469                (or all                  ; We list all groups?
4470                    (if (eq unread t)    ; Unactivated?
4471                        gnus-group-list-inactive-groups ; We list unactivated 
4472                      (> unread 0))      ; We list groups with unread articles
4473                    (and gnus-list-groups-with-ticked-articles
4474                         (cdr (assq 'tick (gnus-info-marks info))))
4475                                         ; And groups with tickeds
4476                    ;; Check for permanent visibility.
4477                    (and gnus-permanently-visible-groups
4478                         (string-match gnus-permanently-visible-groups
4479                                       group))
4480                    (memq 'visible params)
4481                    (cdr (assq 'visible params)))
4482                (gnus-group-insert-group-line
4483                 group (gnus-info-level info)
4484                 (gnus-info-marks info) unread (gnus-info-method info)))))
4485
4486     ;; List dead groups.
4487     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4488          (gnus-group-prepare-flat-list-dead
4489           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4490           gnus-level-zombie ?Z
4491           regexp))
4492     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4493          (gnus-group-prepare-flat-list-dead
4494           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4495           gnus-level-killed ?K regexp))
4496
4497     (gnus-group-set-mode-line)
4498     (setq gnus-group-list-mode (cons level all))
4499     (run-hooks 'gnus-group-prepare-hook)))
4500
4501 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4502   ;; List zombies and killed lists somewhat faster, which was
4503   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4504   ;; this by ignoring the group format specification altogether.
4505   (let (group)
4506     (if regexp
4507         ;; This loop is used when listing groups that match some
4508         ;; regexp.
4509         (while groups
4510           (setq group (pop groups))
4511           (when (string-match regexp group)
4512             (add-text-properties
4513              (point) (prog1 (1+ (point))
4514                        (insert " " mark "     *: " group "\n"))
4515              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4516                    'gnus-unread t
4517                    'gnus-level level))))
4518       ;; This loop is used when listing all groups.
4519       (while groups
4520         (add-text-properties
4521          (point) (prog1 (1+ (point))
4522                    (insert " " mark "     *: "
4523                            (setq group (pop groups)) "\n"))
4524          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4525                'gnus-unread t
4526                'gnus-level level))))))
4527
4528 (defmacro gnus-group-real-name (group)
4529   "Find the real name of a foreign newsgroup."
4530   `(let ((gname ,group))
4531      (if (string-match ":[^:]+$" gname)
4532          (substring gname (1+ (match-beginning 0)))
4533        gname)))
4534
4535 (defsubst gnus-server-add-address (method)
4536   (let ((method-name (symbol-name (car method))))
4537     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4538              (not (assq (intern (concat method-name "-address")) method)))
4539         (append method (list (list (intern (concat method-name "-address"))
4540                                    (nth 1 method))))
4541       method)))
4542
4543 (defsubst gnus-server-get-method (group method)
4544   ;; Input either a server name, and extended server name, or a
4545   ;; select method, and return a select method.
4546   (cond ((stringp method)
4547          (gnus-server-to-method method))
4548         ((equal method gnus-select-method)
4549          gnus-select-method)
4550         ((and (stringp (car method)) group)
4551          (gnus-server-extend-method group method))
4552         ((and method (not group)
4553               (equal (cadr method) ""))
4554          method)
4555         (t
4556          (gnus-server-add-address method))))
4557
4558 (defun gnus-server-to-method (server)
4559   "Map virtual server names to select methods."
4560   (or 
4561    ;; Perhaps this is the native server?
4562    (and (equal server "native") gnus-select-method)
4563    ;; It should be in the server alist.
4564    (cdr (assoc server gnus-server-alist))
4565    ;; If not, we look through all the opened server
4566    ;; to see whether we can find it there.
4567    (let ((opened gnus-opened-servers))
4568      (while (and opened
4569                  (not (equal server (format "%s:%s" (caaar opened)
4570                                             (cadaar opened)))))
4571        (pop opened))
4572      (caar opened))))
4573
4574 (defmacro gnus-method-equal (ss1 ss2)
4575   "Say whether two servers are equal."
4576   `(let ((s1 ,ss1)
4577          (s2 ,ss2))
4578      (or (equal s1 s2)
4579          (and (= (length s1) (length s2))
4580               (progn
4581                 (while (and s1 (member (car s1) s2))
4582                   (setq s1 (cdr s1)))
4583                 (null s1))))))
4584
4585 (defun gnus-server-equal (m1 m2)
4586   "Say whether two methods are equal."
4587   (let ((m1 (cond ((null m1) gnus-select-method)
4588                   ((stringp m1) (gnus-server-to-method m1))
4589                   (t m1)))
4590         (m2 (cond ((null m2) gnus-select-method)
4591                   ((stringp m2) (gnus-server-to-method m2))
4592                   (t m2))))
4593     (gnus-method-equal m1 m2)))
4594
4595 (defun gnus-servers-using-backend (backend)
4596   "Return a list of known servers using BACKEND."
4597   (let ((opened gnus-opened-servers)
4598         out)
4599     (while opened
4600       (when (eq backend (caaar opened))
4601         (push (caar opened) out))
4602       (pop opened))
4603     out))
4604
4605 (defun gnus-group-prefixed-name (group method)
4606   "Return the whole name from GROUP and METHOD."
4607   (and (stringp method) (setq method (gnus-server-to-method method)))
4608   (concat (format "%s" (car method))
4609           (if (and
4610                (or (assoc (format "%s" (car method)) 
4611                           (gnus-methods-using 'address))
4612                    (gnus-server-equal method gnus-message-archive-method))
4613                (nth 1 method)
4614                (not (string= (nth 1 method) "")))
4615               (concat "+" (nth 1 method)))
4616           ":" group))
4617
4618 (defun gnus-group-real-prefix (group)
4619   "Return the prefix of the current group name."
4620   (if (string-match "^[^:]+:" group)
4621       (substring group 0 (match-end 0))
4622     ""))
4623
4624 (defun gnus-group-method (group)
4625   "Return the server or method used for selecting GROUP."
4626   (let ((prefix (gnus-group-real-prefix group)))
4627     (if (equal prefix "")
4628         gnus-select-method
4629       (let ((servers gnus-opened-servers)
4630             (server "")
4631             backend possible found)
4632         (if (string-match "^[^\\+]+\\+" prefix)
4633             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4634                   server (substring prefix (match-end 0) (1- (length prefix))))
4635           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4636         (while servers
4637           (when (eq (caaar servers) backend)
4638             (setq possible (caar servers))
4639             (when (equal (cadaar servers) server)
4640               (setq found (caar servers))))
4641           (pop servers))
4642         (or (car (rassoc found gnus-server-alist))
4643             found
4644             (car (rassoc possible gnus-server-alist))
4645             possible
4646             (list backend server))))))
4647
4648 (defsubst gnus-secondary-method-p (method)
4649   "Return whether METHOD is a secondary select method."
4650   (let ((methods gnus-secondary-select-methods)
4651         (gmethod (gnus-server-get-method nil method)))
4652     (while (and methods
4653                 (not (equal (gnus-server-get-method nil (car methods))
4654                             gmethod)))
4655       (setq methods (cdr methods)))
4656     methods))
4657
4658 (defun gnus-group-foreign-p (group)
4659   "Say whether a group is foreign or not."
4660   (and (not (gnus-group-native-p group))
4661        (not (gnus-group-secondary-p group))))
4662
4663 (defun gnus-group-native-p (group)
4664   "Say whether the group is native or not."
4665   (not (string-match ":" group)))
4666
4667 (defun gnus-group-secondary-p (group)
4668   "Say whether the group is secondary or not."
4669   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4670
4671 (defun gnus-group-get-parameter (group &optional symbol)
4672   "Returns the group parameters for GROUP.
4673 If SYMBOL, return the value of that symbol in the group parameters."
4674   (let ((params (gnus-info-params (gnus-get-info group))))
4675     (if symbol
4676         (gnus-group-parameter-value params symbol)
4677       params)))
4678
4679 (defun gnus-group-parameter-value (params symbol)
4680   "Return the value of SYMBOL in group PARAMS."
4681   (or (car (memq symbol params))        ; It's either a simple symbol
4682       (cdr (assq symbol params))))      ; or a cons.
4683
4684 (defun gnus-group-add-parameter (group param)
4685   "Add parameter PARAM to GROUP."
4686   (let ((info (gnus-get-info group)))
4687     (if (not info)
4688         () ; This is a dead group.  We just ignore it.
4689       ;; Cons the new param to the old one and update.
4690       (gnus-group-set-info (cons param (gnus-info-params info))
4691                            group 'params))))
4692
4693 (defun gnus-group-set-parameter (group name value)
4694   "Set parameter NAME to VALUE in GROUP."
4695   (let ((info (gnus-get-info group)))
4696     (if (not info)
4697         () ; This is a dead group.  We just ignore it.
4698       (let ((old-params (gnus-info-params info))
4699             (new-params (list (cons name value))))
4700         (while old-params
4701           (if (or (not (listp (car old-params)))
4702                   (not (eq (caar old-params) name)))
4703               (setq new-params (append new-params (list (car old-params)))))
4704           (setq old-params (cdr old-params)))
4705         (gnus-group-set-info new-params group 'params)))))
4706
4707 (defun gnus-group-add-score (group &optional score)
4708   "Add SCORE to the GROUP score.
4709 If SCORE is nil, add 1 to the score of GROUP."
4710   (let ((info (gnus-get-info group)))
4711     (when info
4712       (gnus-info-set-score info (+ (gnus-info-score info) (or score 1))))))
4713
4714 (defun gnus-summary-bubble-group ()
4715   "Increase the score of the current group.
4716 This is a handy function to add to `gnus-summary-exit-hook' to
4717 increase the score of each group you read."
4718   (gnus-group-add-score gnus-newsgroup-name))
4719
4720 (defun gnus-group-set-info (info &optional method-only-group part)
4721   (let* ((entry (gnus-gethash
4722                  (or method-only-group (gnus-info-group info))
4723                  gnus-newsrc-hashtb))
4724          (part-info info)
4725          (info (if method-only-group (nth 2 entry) info))
4726          method)
4727     (when method-only-group
4728       (unless entry
4729         (error "Trying to change non-existent group %s" method-only-group))
4730       ;; We have received parts of the actual group info - either the
4731       ;; select method or the group parameters.  We first check
4732       ;; whether we have to extend the info, and if so, do that.
4733       (let ((len (length info))
4734             (total (if (eq part 'method) 5 6)))
4735         (when (< len total)
4736           (setcdr (nthcdr (1- len) info)
4737                   (make-list (- total len) nil)))
4738         ;; Then we enter the new info.
4739         (setcar (nthcdr (1- total) info) part-info)))
4740     (unless entry
4741       ;; This is a new group, so we just create it.
4742       (save-excursion
4743         (set-buffer gnus-group-buffer)
4744         (setq method (gnus-info-method info))
4745         (when (gnus-server-equal method "native")
4746           (setq method nil))
4747         (if method
4748             ;; It's a foreign group...
4749             (gnus-group-make-group
4750              (gnus-group-real-name (gnus-info-group info))
4751              (if (stringp method) method
4752                (prin1-to-string (car method)))
4753              (and (consp method)
4754                   (nth 1 (gnus-info-method info))))
4755           ;; It's a native group.
4756           (gnus-group-make-group (gnus-info-group info)))
4757         (gnus-message 6 "Note: New group created")
4758         (setq entry
4759               (gnus-gethash (gnus-group-prefixed-name
4760                              (gnus-group-real-name (gnus-info-group info))
4761                              (or (gnus-info-method info) gnus-select-method))
4762                             gnus-newsrc-hashtb))))
4763     ;; Whether it was a new group or not, we now have the entry, so we
4764     ;; can do the update.
4765     (if entry
4766         (progn
4767           (setcar (nthcdr 2 entry) info)
4768           (when (and (not (eq (car entry) t))
4769                      (gnus-active (gnus-info-group info)))
4770             (setcar entry (length (gnus-list-of-unread-articles (car info))))))
4771       (error "No such group: %s" (gnus-info-group info)))))
4772
4773 (defun gnus-group-set-method-info (group select-method)
4774   (gnus-group-set-info select-method group 'method))
4775
4776 (defun gnus-group-set-params-info (group params)
4777   (gnus-group-set-info params group 'params))
4778
4779 (defun gnus-group-update-group-line ()
4780   "Update the current line in the group buffer."
4781   (let* ((buffer-read-only nil)
4782          (group (gnus-group-group-name))
4783          (gnus-group-indentation (gnus-group-group-indentation))
4784          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
4785     (and entry
4786          (not (gnus-ephemeral-group-p group))
4787          (gnus-dribble-enter
4788           (concat "(gnus-group-set-info '"
4789                   (prin1-to-string (nth 2 entry)) ")")))
4790     (gnus-delete-line)
4791     (gnus-group-insert-group-line-info group)
4792     (forward-line -1)
4793     (gnus-group-position-point)))
4794
4795 (defun gnus-group-insert-group-line-info (group)
4796   "Insert GROUP on the current line."
4797   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4798         active info)
4799     (if entry
4800         (progn
4801           ;; (Un)subscribed group.
4802           (setq info (nth 2 entry))
4803           (gnus-group-insert-group-line
4804            group (gnus-info-level info) (gnus-info-marks info)
4805            (or (car entry) t) (gnus-info-method info)))
4806       ;; This group is dead.
4807       (gnus-group-insert-group-line
4808        group
4809        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4810        nil
4811        (if (setq active (gnus-active group))
4812            (- (1+ (cdr active)) (car active)) 0)
4813        nil))))
4814
4815 (defun gnus-group-insert-group-line (gnus-tmp-group gnus-tmp-level 
4816                                                     gnus-tmp-marked number
4817                                                     gnus-tmp-method)
4818   "Insert a group line in the group buffer."
4819   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4820          (gnus-tmp-number-total
4821           (if gnus-tmp-active
4822               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4823             0))
4824          (gnus-tmp-number-of-unread
4825           (if (numberp number) (int-to-string (max 0 number))
4826             "*"))
4827          (gnus-tmp-number-of-read
4828           (if (numberp number)
4829               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4830             "*"))
4831          (gnus-tmp-subscribed
4832           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4833                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4834                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4835                 (t ?K)))
4836          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4837          (gnus-tmp-newsgroup-description
4838           (if gnus-description-hashtb
4839               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4840             ""))
4841          (gnus-tmp-moderated
4842           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4843          (gnus-tmp-moderated-string
4844           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4845          (gnus-tmp-method
4846           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4847          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
4848          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4849          (gnus-tmp-news-method-string
4850           (if gnus-tmp-method
4851               (format "(%s:%s)" (car gnus-tmp-method)
4852                       (cadr gnus-tmp-method)) ""))
4853          (gnus-tmp-marked-mark
4854           (if (and (numberp number)
4855                    (zerop number)
4856                    (cdr (assq 'tick gnus-tmp-marked)))
4857               ?* ? ))
4858          (gnus-tmp-process-marked
4859           (if (member gnus-tmp-group gnus-group-marked)
4860               gnus-process-mark ? ))
4861          (gnus-tmp-grouplens
4862           (or (and gnus-use-grouplens
4863                    (bbb-grouplens-group-p gnus-tmp-group))
4864               ""))
4865          (buffer-read-only nil)
4866          header gnus-tmp-header)        ; passed as parameter to user-funcs.
4867     (beginning-of-line)
4868     (add-text-properties
4869      (point)
4870      (prog1 (1+ (point))
4871        ;; Insert the text.
4872        (eval gnus-group-line-format-spec))
4873      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
4874        gnus-unread ,(if (numberp number)
4875                         (string-to-int gnus-tmp-number-of-unread)
4876                       t)
4877        gnus-marked ,gnus-tmp-marked-mark
4878        gnus-indentation ,gnus-group-indentation
4879        gnus-level ,gnus-tmp-level))
4880     (when (inline (gnus-visual-p 'group-highlight 'highlight))
4881       (forward-line -1)
4882       (run-hooks 'gnus-group-update-hook)
4883       (forward-line))
4884     ;; Allow XEmacs to remove front-sticky text properties.
4885     (gnus-group-remove-excess-properties)))
4886
4887 (defun gnus-group-update-group (group &optional visible-only)
4888   "Update all lines where GROUP appear.
4889 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
4890 already."
4891   (save-excursion
4892     (set-buffer gnus-group-buffer)
4893     ;; The buffer may be narrowed.
4894     (save-restriction
4895       (widen)
4896       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
4897             (loc (point-min))
4898             found buffer-read-only)
4899         ;; Enter the current status into the dribble buffer.
4900         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
4901           (if (and entry (not (gnus-ephemeral-group-p group)))
4902               (gnus-dribble-enter
4903                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
4904                        ")"))))
4905         ;; Find all group instances.  If topics are in use, each group
4906         ;; may be listed in more than once.
4907         (while (setq loc (text-property-any
4908                           loc (point-max) 'gnus-group ident))
4909           (setq found t)
4910           (goto-char loc)
4911           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4912             (gnus-delete-line)
4913             (gnus-group-insert-group-line-info group))
4914           (setq loc (1+ loc)))
4915         (unless (or found visible-only)
4916           ;; No such line in the buffer, find out where it's supposed to
4917           ;; go, and insert it there (or at the end of the buffer).
4918           (if gnus-goto-missing-group-function
4919               (funcall gnus-goto-missing-group-function group)
4920             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
4921               (while (and entry (car entry)
4922                           (not
4923                            (gnus-goto-char
4924                             (text-property-any
4925                              (point-min) (point-max)
4926                              'gnus-group (gnus-intern-safe
4927                                           (caar entry) gnus-active-hashtb)))))
4928                 (setq entry (cdr entry)))
4929               (or entry (goto-char (point-max)))))
4930           ;; Finally insert the line.
4931           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4932             (gnus-group-insert-group-line-info group)))
4933         (gnus-group-set-mode-line)))))
4934
4935 (defun gnus-group-set-mode-line ()
4936   (when (memq 'group gnus-updated-mode-lines)
4937     (let* ((gformat (or gnus-group-mode-line-format-spec
4938                         (setq gnus-group-mode-line-format-spec
4939                               (gnus-parse-format
4940                                gnus-group-mode-line-format
4941                                gnus-group-mode-line-format-alist))))
4942            (gnus-tmp-news-server (cadr gnus-select-method))
4943            (gnus-tmp-news-method (car gnus-select-method))
4944            (max-len 60)
4945            gnus-tmp-header                      ;Dummy binding for user-defined formats
4946            ;; Get the resulting string.
4947            (mode-string (eval gformat)))
4948       ;; If the line is too long, we chop it off.
4949       (when (> (length mode-string) max-len)
4950         (setq mode-string (substring mode-string 0 (- max-len 4))))
4951       (prog1
4952           (setq mode-line-buffer-identification (list mode-string))
4953         (set-buffer-modified-p t)))))
4954
4955 (defun gnus-group-group-name ()
4956   "Get the name of the newsgroup on the current line."
4957   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
4958     (and group (symbol-name group))))
4959
4960 (defun gnus-group-group-level ()
4961   "Get the level of the newsgroup on the current line."
4962   (get-text-property (gnus-point-at-bol) 'gnus-level))
4963
4964 (defun gnus-group-group-indentation ()
4965   "Get the indentation of the newsgroup on the current line."
4966   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
4967       (and gnus-group-indentation-function
4968            (funcall gnus-group-indentation-function))
4969       ""))
4970
4971 (defun gnus-group-group-unread ()
4972   "Get the number of unread articles of the newsgroup on the current line."
4973   (get-text-property (gnus-point-at-bol) 'gnus-unread))
4974
4975 (defun gnus-group-search-forward (&optional backward all level first-too)
4976   "Find the next newsgroup with unread articles.
4977 If BACKWARD is non-nil, find the previous newsgroup instead.
4978 If ALL is non-nil, just find any newsgroup.
4979 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
4980 group exists.
4981 If FIRST-TOO, the current line is also eligible as a target."
4982   (let ((way (if backward -1 1))
4983         (low gnus-level-killed)
4984         (beg (point))
4985         pos found lev)
4986     (if (and backward (progn (beginning-of-line)) (bobp))
4987         nil
4988       (or first-too (forward-line way))
4989       (while (and
4990               (not (eobp))
4991               (not (setq
4992                     found
4993                     (and (or all
4994                              (and
4995                               (let ((unread
4996                                      (get-text-property (point) 'gnus-unread)))
4997                                 (and (numberp unread) (> unread 0)))
4998                               (setq lev (get-text-property (point)
4999                                                            'gnus-level))
5000                               (<= lev gnus-level-subscribed)))
5001                          (or (not level)
5002                              (and (setq lev (get-text-property (point)
5003                                                                'gnus-level))
5004                                   (or (= lev level)
5005                                       (and (< lev low)
5006                                            (< level lev)
5007                                            (progn
5008                                              (setq low lev)
5009                                              (setq pos (point))
5010                                              nil))))))))
5011               (zerop (forward-line way)))))
5012     (if found
5013         (progn (gnus-group-position-point) t)
5014       (goto-char (or pos beg))
5015       (and pos t))))
5016
5017 ;;; Gnus group mode commands
5018
5019 ;; Group marking.
5020
5021 (defun gnus-group-mark-group (n &optional unmark no-advance)
5022   "Mark the current group."
5023   (interactive "p")
5024   (let ((buffer-read-only nil)
5025         group)
5026     (while
5027         (and (> n 0)
5028              (setq group (gnus-group-group-name))
5029              (progn
5030                (beginning-of-line)
5031                (forward-char
5032                 (or (cdr (assq 'process gnus-group-mark-positions)) 2))
5033                (delete-char 1)
5034                (if unmark
5035                    (progn
5036                      (insert " ")
5037                      (setq gnus-group-marked (delete group gnus-group-marked)))
5038                  (insert "#")
5039                  (setq gnus-group-marked
5040                        (cons group (delete group gnus-group-marked))))
5041                t)
5042              (or no-advance (zerop (gnus-group-next-group 1))))
5043       (setq n (1- n)))
5044     (gnus-summary-position-point)
5045     n))
5046
5047 (defun gnus-group-unmark-group (n)
5048   "Remove the mark from the current group."
5049   (interactive "p")
5050   (gnus-group-mark-group n 'unmark)
5051   (gnus-group-position-point))
5052
5053 (defun gnus-group-unmark-all-groups ()
5054   "Unmark all groups."
5055   (interactive)
5056   (let ((groups gnus-group-marked))
5057     (save-excursion
5058       (while groups
5059         (gnus-group-remove-mark (pop groups)))))
5060   (gnus-group-position-point))
5061
5062 (defun gnus-group-mark-region (unmark beg end)
5063   "Mark all groups between point and mark.
5064 If UNMARK, remove the mark instead."
5065   (interactive "P\nr")
5066   (let ((num (count-lines beg end)))
5067     (save-excursion
5068       (goto-char beg)
5069       (- num (gnus-group-mark-group num unmark)))))
5070
5071 (defun gnus-group-mark-buffer (&optional unmark)
5072   "Mark all groups in the buffer.
5073 If UNMARK, remove the mark instead."
5074   (interactive "P")
5075   (gnus-group-mark-region unmark (point-min) (point-max)))
5076
5077 (defun gnus-group-mark-regexp (regexp)
5078   "Mark all groups that match some regexp."
5079   (interactive "sMark (regexp): ")
5080   (let ((alist (cdr gnus-newsrc-alist))
5081         group)
5082     (while alist
5083       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5084         (gnus-group-set-mark group))))
5085   (gnus-group-position-point))
5086
5087 (defun gnus-group-remove-mark (group)
5088   "Remove the process mark from GROUP and move point there.
5089 Return nil if the group isn't displayed."
5090   (if (gnus-group-goto-group group)
5091       (save-excursion
5092         (gnus-group-mark-group 1 'unmark t)
5093         t)
5094     (setq gnus-group-marked
5095           (delete group gnus-group-marked))
5096     nil))
5097
5098 (defun gnus-group-set-mark (group)
5099   "Set the process mark on GROUP."
5100   (if (gnus-group-goto-group group) 
5101       (save-excursion
5102         (gnus-group-mark-group 1 nil t))
5103     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
5104
5105 (defun gnus-group-universal-argument (arg &optional groups func)
5106   "Perform any command on all groups accoring to the process/prefix convention."
5107   (interactive "P")
5108   (let ((groups (or groups (gnus-group-process-prefix arg)))
5109         group func)
5110     (if (eq (setq func (or func
5111                            (key-binding
5112                             (read-key-sequence
5113                              (substitute-command-keys
5114                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5115             'undefined)
5116         (progn
5117           (message "Undefined key")
5118           (ding))
5119       (while groups
5120         (gnus-group-remove-mark (setq group (pop groups)))
5121         (command-execute func))))
5122   (gnus-group-position-point))
5123
5124 (defun gnus-group-process-prefix (n)
5125   "Return a list of groups to work on.
5126 Take into consideration N (the prefix) and the list of marked groups."
5127   (cond
5128    (n
5129     (setq n (prefix-numeric-value n))
5130     ;; There is a prefix, so we return a list of the N next
5131     ;; groups.
5132     (let ((way (if (< n 0) -1 1))
5133           (n (abs n))
5134           group groups)
5135       (save-excursion
5136         (while (and (> n 0)
5137                     (setq group (gnus-group-group-name)))
5138           (setq groups (cons group groups))
5139           (setq n (1- n))
5140           (gnus-group-next-group way)))
5141       (nreverse groups)))
5142    ((and (boundp 'transient-mark-mode)
5143          transient-mark-mode
5144          mark-active)
5145     ;; Work on the region between point and mark.
5146     (let ((max (max (point) (mark)))
5147           groups)
5148       (save-excursion
5149         (goto-char (min (point) (mark)))
5150         (while
5151             (and
5152              (push (gnus-group-group-name) groups)
5153              (zerop (gnus-group-next-group 1))
5154              (< (point) max)))
5155         (nreverse groups))))
5156    (gnus-group-marked
5157     ;; No prefix, but a list of marked articles.
5158     (reverse gnus-group-marked))
5159    (t
5160     ;; Neither marked articles or a prefix, so we return the
5161     ;; current group.
5162     (let ((group (gnus-group-group-name)))
5163       (and group (list group))))))
5164
5165 ;; Selecting groups.
5166
5167 (defun gnus-group-read-group (&optional all no-article group)
5168   "Read news in this newsgroup.
5169 If the prefix argument ALL is non-nil, already read articles become
5170 readable.  IF ALL is a number, fetch this number of articles.  If the
5171 optional argument NO-ARTICLE is non-nil, no article will be
5172 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5173 group."
5174   (interactive "P")
5175   (let ((group (or group (gnus-group-group-name)))
5176         number active marked entry)
5177     (or group (error "No group on current line"))
5178     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5179                                             group gnus-newsrc-hashtb)))))
5180     ;; This group might be a dead group.  In that case we have to get
5181     ;; the number of unread articles from `gnus-active-hashtb'.
5182     (setq number
5183           (cond ((numberp all) all)
5184                 (entry (car entry))
5185                 ((setq active (gnus-active group))
5186                  (- (1+ (cdr active)) (car active)))))
5187     (gnus-summary-read-group
5188      group (or all (and (numberp number)
5189                         (zerop (+ number (length (cdr (assq 'tick marked)))
5190                                   (length (cdr (assq 'dormant marked)))))))
5191      no-article)))
5192
5193 (defun gnus-group-select-group (&optional all)
5194   "Select this newsgroup.
5195 No article is selected automatically.
5196 If ALL is non-nil, already read articles become readable.
5197 If ALL is a number, fetch this number of articles."
5198   (interactive "P")
5199   (gnus-group-read-group all t))
5200
5201 (defun gnus-group-quick-select-group (&optional all)
5202   "Select the current group \"quickly\".
5203 This means that no highlighting or scoring will be performed."
5204   (interactive "P")
5205   (let (gnus-visual
5206         gnus-score-find-score-files-function
5207         gnus-apply-kill-hook
5208         gnus-summary-expunge-below)
5209     (gnus-group-read-group all t)))
5210
5211 (defun gnus-group-visible-select-group (&optional all)
5212   "Select the current group without hiding any articles."
5213   (interactive "P")
5214   (let ((gnus-inhibit-limiting t))
5215     (gnus-group-read-group all t)))
5216
5217 ;;;###autoload
5218 (defun gnus-fetch-group (group)
5219   "Start Gnus if necessary and enter GROUP.
5220 Returns whether the fetching was successful or not."
5221   (interactive "sGroup name: ")
5222   (or (get-buffer gnus-group-buffer)
5223       (gnus))
5224   (gnus-group-read-group nil nil group))
5225
5226 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5227 ;; if selection was successful.
5228 (defun gnus-group-read-ephemeral-group
5229   (group method &optional activate quit-config)
5230   (let ((group (if (gnus-group-foreign-p group) group
5231                  (gnus-group-prefixed-name group method))))
5232     (gnus-sethash
5233      group
5234      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5235                      ((quit-config . ,(if quit-config quit-config
5236                                         (cons (current-buffer) 'summary))))))
5237      gnus-newsrc-hashtb)
5238     (set-buffer gnus-group-buffer)
5239     (or (gnus-check-server method)
5240         (error "Unable to contact server: %s" (gnus-status-message method)))
5241     (if activate (or (gnus-request-group group)
5242                      (error "Couldn't request group")))
5243     (condition-case ()
5244         (gnus-group-read-group t t group)
5245       (error nil)
5246       (quit nil))))
5247
5248 (defun gnus-group-jump-to-group (group)
5249   "Jump to newsgroup GROUP."
5250   (interactive
5251    (list (completing-read
5252           "Group: " gnus-active-hashtb nil
5253           (memq gnus-select-method gnus-have-read-active-file))))
5254
5255   (if (equal group "")
5256       (error "Empty group name"))
5257
5258   (let ((b (text-property-any
5259             (point-min) (point-max)
5260             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5261     (unless (gnus-ephemeral-group-p group)
5262       (if b
5263           ;; Either go to the line in the group buffer...
5264           (goto-char b)
5265         ;; ... or insert the line.
5266         (or
5267          (gnus-active group)
5268          (gnus-activate-group group)
5269          (error "%s error: %s" group (gnus-status-message group)))
5270
5271         (gnus-group-update-group group)
5272         (goto-char (text-property-any
5273                     (point-min) (point-max)
5274                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5275     ;; Adjust cursor point.
5276     (gnus-group-position-point)))
5277
5278 (defun gnus-group-goto-group (group)
5279   "Goto to newsgroup GROUP."
5280   (when group
5281     (let ((b (text-property-any (point-min) (point-max)
5282                                 'gnus-group (gnus-intern-safe
5283                                              group gnus-active-hashtb))))
5284       (and b (goto-char b)))))
5285
5286 (defun gnus-group-next-group (n)
5287   "Go to next N'th newsgroup.
5288 If N is negative, search backward instead.
5289 Returns the difference between N and the number of skips actually
5290 done."
5291   (interactive "p")
5292   (gnus-group-next-unread-group n t))
5293
5294 (defun gnus-group-next-unread-group (n &optional all level)
5295   "Go to next N'th unread newsgroup.
5296 If N is negative, search backward instead.
5297 If ALL is non-nil, choose any newsgroup, unread or not.
5298 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5299 such group can be found, the next group with a level higher than
5300 LEVEL.
5301 Returns the difference between N and the number of skips actually
5302 made."
5303   (interactive "p")
5304   (let ((backward (< n 0))
5305         (n (abs n)))
5306     (while (and (> n 0)
5307                 (gnus-group-search-forward
5308                  backward (or (not gnus-group-goto-unread) all) level))
5309       (setq n (1- n)))
5310     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5311                                (if level " on this level or higher" "")))
5312     n))
5313
5314 (defun gnus-group-prev-group (n)
5315   "Go to previous N'th newsgroup.
5316 Returns the difference between N and the number of skips actually
5317 done."
5318   (interactive "p")
5319   (gnus-group-next-unread-group (- n) t))
5320
5321 (defun gnus-group-prev-unread-group (n)
5322   "Go to previous N'th unread newsgroup.
5323 Returns the difference between N and the number of skips actually
5324 done."
5325   (interactive "p")
5326   (gnus-group-next-unread-group (- n)))
5327
5328 (defun gnus-group-next-unread-group-same-level (n)
5329   "Go to next N'th unread newsgroup on the same level.
5330 If N is negative, search backward instead.
5331 Returns the difference between N and the number of skips actually
5332 done."
5333   (interactive "p")
5334   (gnus-group-next-unread-group n t (gnus-group-group-level))
5335   (gnus-group-position-point))
5336
5337 (defun gnus-group-prev-unread-group-same-level (n)
5338   "Go to next N'th unread newsgroup on the same level.
5339 Returns the difference between N and the number of skips actually
5340 done."
5341   (interactive "p")
5342   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5343   (gnus-group-position-point))
5344
5345 (defun gnus-group-best-unread-group (&optional exclude-group)
5346   "Go to the group with the highest level.
5347 If EXCLUDE-GROUP, do not go to that group."
5348   (interactive)
5349   (goto-char (point-min))
5350   (let ((best 100000)
5351         unread best-point)
5352     (while (not (eobp))
5353       (setq unread (get-text-property (point) 'gnus-unread))
5354       (if (and (numberp unread) (> unread 0))
5355           (progn
5356             (if (and (get-text-property (point) 'gnus-level)
5357                      (< (get-text-property (point) 'gnus-level) best)
5358                      (or (not exclude-group)
5359                          (not (equal exclude-group (gnus-group-group-name)))))
5360                 (progn
5361                   (setq best (get-text-property (point) 'gnus-level))
5362                   (setq best-point (point))))))
5363       (forward-line 1))
5364     (if best-point (goto-char best-point))
5365     (gnus-summary-position-point)
5366     (and best-point (gnus-group-group-name))))
5367
5368 (defun gnus-group-first-unread-group ()
5369   "Go to the first group with unread articles."
5370   (interactive)
5371   (prog1
5372       (let ((opoint (point))
5373             unread)
5374         (goto-char (point-min))
5375         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5376                 (and (numberp unread)   ; Not a topic.
5377                      (not (zerop unread))) ; Has unread articles.
5378                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5379             (point)                     ; Success.
5380           (goto-char opoint)
5381           nil))                         ; Not success.
5382     (gnus-group-position-point)))
5383
5384 (defun gnus-group-enter-server-mode ()
5385   "Jump to the server buffer."
5386   (interactive)
5387   (gnus-enter-server-buffer))
5388
5389 (defun gnus-group-make-group (name &optional method address)
5390   "Add a new newsgroup.
5391 The user will be prompted for a NAME, for a select METHOD, and an
5392 ADDRESS."
5393   (interactive
5394    (cons
5395     (read-string "Group name: ")
5396     (let ((method
5397            (completing-read
5398             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5399             nil t)))
5400       (cond ((assoc method gnus-valid-select-methods)
5401              (list method
5402                    (if (memq 'prompt-address
5403                              (assoc method gnus-valid-select-methods))
5404                        (read-string "Address: ")
5405                      "")))
5406             ((assoc method gnus-server-alist)
5407              (list method))
5408             (t
5409              (list method ""))))))
5410
5411   (save-excursion
5412     (set-buffer gnus-group-buffer)
5413     (let* ((meth (and method (if address (list (intern method) address)
5414                                method)))
5415            (nname (if method (gnus-group-prefixed-name name meth) name))
5416            backend info)
5417       (and (gnus-gethash nname gnus-newsrc-hashtb)
5418            (error "Group %s already exists" nname))
5419       (gnus-group-change-level
5420        (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5421        gnus-level-default-subscribed gnus-level-killed
5422        (and (gnus-group-group-name)
5423             (gnus-gethash (gnus-group-group-name)
5424                           gnus-newsrc-hashtb))
5425        t)
5426       (gnus-set-active nname (cons 1 0))
5427       (or (gnus-ephemeral-group-p name)
5428           (gnus-dribble-enter
5429            (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5430       (gnus-group-insert-group-line-info nname)
5431
5432       (when (assoc (symbol-name (setq backend (car (gnus-server-get-method
5433                                                     nil meth))))
5434                    gnus-valid-select-methods)
5435         (require backend))
5436       (gnus-check-server meth)
5437       (and (gnus-check-backend-function 'request-create-group nname)
5438            (gnus-request-create-group nname))
5439       t)))
5440
5441 (defun gnus-group-delete-group (group &optional force)
5442   "Delete the current group.
5443 If FORCE (the prefix) is non-nil, all the articles in the group will
5444 be deleted.  This is \"deleted\" as in \"removed forever from the face
5445 of the Earth\".  There is no undo."
5446   (interactive
5447    (list (gnus-group-group-name)
5448          current-prefix-arg))
5449   (or group (error "No group to rename"))
5450   (or (gnus-check-backend-function 'request-delete-group group)
5451       (error "This backend does not support group deletion"))
5452   (prog1
5453       (if (not (gnus-yes-or-no-p
5454                 (format
5455                  "Do you really want to delete %s%s? "
5456                  group (if force " and all its contents" ""))))
5457           () ; Whew!
5458         (gnus-message 6 "Deleting group %s..." group)
5459         (if (not (gnus-request-delete-group group force))
5460             (progn
5461               (gnus-message 3 "Couldn't delete group %s" group)
5462               (ding))
5463           (gnus-message 6 "Deleting group %s...done" group)
5464           (gnus-group-goto-group group)
5465           (gnus-group-kill-group 1 t)
5466           (gnus-sethash group nil gnus-active-hashtb)
5467           t))
5468     (gnus-group-position-point)))
5469
5470 (defun gnus-group-rename-group (group new-name)
5471   (interactive
5472    (list
5473     (gnus-group-group-name)
5474     (progn
5475       (or (gnus-check-backend-function
5476            'request-rename-group (gnus-group-group-name))
5477           (error "This backend does not support renaming groups"))
5478       (read-string "New group name: "))))
5479
5480   (or (gnus-check-backend-function 'request-rename-group group)
5481       (error "This backend does not support renaming groups"))
5482
5483   (or group (error "No group to rename"))
5484   (and (string-match "^[ \t]*$" new-name)
5485        (error "Not a valid group name"))
5486
5487   ;; We find the proper prefixed name.
5488   (setq new-name
5489         (gnus-group-prefixed-name
5490          (gnus-group-real-name new-name)
5491          (gnus-info-method (gnus-get-info group))))
5492
5493   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5494   (prog1
5495       (if (not (gnus-request-rename-group group new-name))
5496           (progn
5497             (gnus-message 3 "Couldn't rename group %s to %s" group new-name)
5498             (ding))
5499         ;; We rename the group internally by killing it...
5500         (gnus-group-goto-group group)
5501         (gnus-group-kill-group)
5502         ;; ... changing its name ...
5503         (setcar (cdar gnus-list-of-killed-groups) new-name)
5504         ;; ... and then yanking it.  Magic!
5505         (gnus-group-yank-group)
5506         (gnus-set-active new-name (gnus-active group))
5507         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5508         new-name)
5509     (gnus-group-position-point)))
5510
5511 (defun gnus-group-edit-group (group &optional part)
5512   "Edit the group on the current line."
5513   (interactive (list (gnus-group-group-name)))
5514   (let* ((part (or part 'info))
5515          (done-func `(lambda ()
5516                        "Exit editing mode and update the information."
5517                        (interactive)
5518                        (gnus-group-edit-group-done ',part ,group)))
5519          (winconf (current-window-configuration))
5520          info)
5521     (or group (error "No group on current line"))
5522     (or (setq info (gnus-get-info group))
5523         (error "Killed group; can't be edited"))
5524     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5525     (gnus-configure-windows 'edit-group)
5526     (gnus-add-current-to-buffer-list)
5527     (emacs-lisp-mode)
5528     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5529     (use-local-map (copy-keymap emacs-lisp-mode-map))
5530     (local-set-key "\C-c\C-c" done-func)
5531     (make-local-variable 'gnus-prev-winconf)
5532     (setq gnus-prev-winconf winconf)
5533     (erase-buffer)
5534     (insert
5535      (cond
5536       ((eq part 'method)
5537        ";; Type `C-c C-c' after editing the select method.\n\n")
5538       ((eq part 'params)
5539        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5540       ((eq part 'info)
5541        ";; Type `C-c C-c' after editing the group info.\n\n")))
5542     (insert
5543      (pp-to-string
5544       (cond ((eq part 'method)
5545              (or (gnus-info-method info) "native"))
5546             ((eq part 'params)
5547              (gnus-info-params info))
5548             (t info)))
5549      "\n")))
5550
5551 (defun gnus-group-edit-group-method (group)
5552   "Edit the select method of GROUP."
5553   (interactive (list (gnus-group-group-name)))
5554   (gnus-group-edit-group group 'method))
5555
5556 (defun gnus-group-edit-group-parameters (group)
5557   "Edit the group parameters of GROUP."
5558   (interactive (list (gnus-group-group-name)))
5559   (gnus-group-edit-group group 'params))
5560
5561 (defun gnus-group-edit-group-done (part group)
5562   "Get info from buffer, update variables and jump to the group buffer."
5563   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5564   (goto-char (point-min))
5565   (let* ((form (read (current-buffer)))
5566          (winconf gnus-prev-winconf)
5567          (method (cond ((eq part 'info) (nth 4 form))
5568                        ((eq part 'method) form)
5569                        (t nil)))
5570          (info (cond ((eq part 'info) form)
5571                      ((eq part 'method) (gnus-get-info group))
5572                      (t nil)))
5573          (new-group (if info
5574                       (if (or (not method)
5575                               (gnus-server-equal
5576                                gnus-select-method method))
5577                           (gnus-group-real-name (car info))
5578                         (gnus-group-prefixed-name
5579                          (gnus-group-real-name (car info)) method))
5580                       nil)))
5581     (when (and new-group
5582                (not (equal new-group group)))
5583       (when (gnus-group-goto-group group)
5584         (gnus-group-kill-group 1))
5585       (gnus-activate-group new-group))
5586     ;; Set the info.
5587     (if (and info new-group)
5588         (progn
5589           (setq info (gnus-copy-sequence info))
5590           (setcar info new-group)
5591           (unless (gnus-server-equal method "native")
5592             (unless (nthcdr 3 info)
5593               (nconc info (list nil nil)))
5594             (unless (nthcdr 4 info)
5595               (nconc info (list nil)))
5596             (gnus-info-set-method info method))
5597           (gnus-group-set-info info))
5598       (gnus-group-set-info form (or new-group group) part))
5599     (kill-buffer (current-buffer))
5600     (and winconf (set-window-configuration winconf))
5601     (set-buffer gnus-group-buffer)
5602     (gnus-group-update-group (or new-group group))
5603     (gnus-group-position-point)))
5604
5605 (defun gnus-group-make-help-group ()
5606   "Create the Gnus documentation group."
5607   (interactive)
5608   (let ((path load-path)
5609         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5610         file dir)
5611     (and (gnus-gethash name gnus-newsrc-hashtb)
5612          (error "Documentation group already exists"))
5613     (while path
5614       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5615             file nil)
5616       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5617                 (file-exists-p
5618                  (setq file (concat (file-name-directory
5619                                      (directory-file-name dir))
5620                                     "etc/gnus-tut.txt"))))
5621         (setq path nil)))
5622     (if (not file)
5623         (message "Couldn't find doc group")
5624       (gnus-group-make-group
5625        (gnus-group-real-name name)
5626        (list 'nndoc "gnus-help"
5627              (list 'nndoc-address file)
5628              (list 'nndoc-article-type 'mbox)))))
5629   (gnus-group-position-point))
5630
5631 (defun gnus-group-make-doc-group (file type)
5632   "Create a group that uses a single file as the source."
5633   (interactive
5634    (list (read-file-name "File name: ")
5635          (and current-prefix-arg 'ask)))
5636   (when (eq type 'ask)
5637     (let ((err "")
5638           char found)
5639       (while (not found)
5640         (message
5641          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5642          err)
5643         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5644                           ((= char ?b) 'babyl)
5645                           ((= char ?d) 'digest)
5646                           ((= char ?f) 'forward)
5647                           ((= char ?a) 'mmfd)
5648                           (t (setq err (format "%c unknown. " char))
5649                              nil))))
5650       (setq type found)))
5651   (let* ((file (expand-file-name file))
5652          (name (gnus-generate-new-group-name
5653                 (gnus-group-prefixed-name
5654                  (file-name-nondirectory file) '(nndoc "")))))
5655     (gnus-group-make-group
5656      (gnus-group-real-name name)
5657      (list 'nndoc (file-name-nondirectory file)
5658            (list 'nndoc-address file)
5659            (list 'nndoc-article-type (or type 'guess))))
5660     (forward-line -1)
5661     (gnus-group-position-point)))
5662
5663 (defun gnus-group-make-archive-group (&optional all)
5664   "Create the (ding) Gnus archive group of the most recent articles.
5665 Given a prefix, create a full group."
5666   (interactive "P")
5667   (let ((group (gnus-group-prefixed-name
5668                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5669     (and (gnus-gethash group gnus-newsrc-hashtb)
5670          (error "Archive group already exists"))
5671     (gnus-group-make-group
5672      (gnus-group-real-name group)
5673      (list 'nndir (if all "hpc" "edu")
5674            (list 'nndir-directory
5675                  (if all gnus-group-archive-directory
5676                    gnus-group-recent-archive-directory)))))
5677   (forward-line -1)
5678   (gnus-group-position-point))
5679
5680 (defun gnus-group-make-directory-group (dir)
5681   "Create an nndir group.
5682 The user will be prompted for a directory.  The contents of this
5683 directory will be used as a newsgroup.  The directory should contain
5684 mail messages or news articles in files that have numeric names."
5685   (interactive
5686    (list (read-file-name "Create group from directory: ")))
5687   (or (file-exists-p dir) (error "No such directory"))
5688   (or (file-directory-p dir) (error "Not a directory"))
5689   (let ((ext "")
5690         (i 0)
5691         group)
5692     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5693       (setq group
5694             (gnus-group-prefixed-name
5695              (concat (file-name-as-directory (directory-file-name dir))
5696                      ext)
5697              '(nndir "")))
5698       (setq ext (format "<%d>" (setq i (1+ i)))))
5699     (gnus-group-make-group
5700      (gnus-group-real-name group)
5701      (list 'nndir group (list 'nndir-directory dir))))
5702   (forward-line -1)
5703   (gnus-group-position-point))
5704
5705 (defun gnus-group-make-kiboze-group (group address scores)
5706   "Create an nnkiboze group.
5707 The user will be prompted for a name, a regexp to match groups, and
5708 score file entries for articles to include in the group."
5709   (interactive
5710    (list
5711     (read-string "nnkiboze group name: ")
5712     (read-string "Source groups (regexp): ")
5713     (let ((headers (mapcar (lambda (group) (list group))
5714                            '("subject" "from" "number" "date" "message-id"
5715                              "references" "chars" "lines" "xref"
5716                              "followup" "all" "body" "head")))
5717           scores header regexp regexps)
5718       (while (not (equal "" (setq header (completing-read
5719                                           "Match on header: " headers nil t))))
5720         (setq regexps nil)
5721         (while (not (equal "" (setq regexp (read-string
5722                                             (format "Match on %s (string): "
5723                                                     header)))))
5724           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5725         (setq scores (cons (cons header regexps) scores)))
5726       scores)))
5727   (gnus-group-make-group group "nnkiboze" address)
5728   (save-excursion
5729     (gnus-set-work-buffer)
5730     (let (emacs-lisp-mode-hook)
5731       (pp scores (current-buffer)))
5732     (write-region (point-min) (point-max)
5733                   (gnus-score-file-name (concat "nnkiboze:" group))))
5734   (forward-line -1)
5735   (gnus-group-position-point))
5736
5737 (defun gnus-group-add-to-virtual (n vgroup)
5738   "Add the current group to a virtual group."
5739   (interactive
5740    (list current-prefix-arg
5741          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5742                           "nnvirtual:")))
5743   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5744       (error "%s is not an nnvirtual group" vgroup))
5745   (let* ((groups (gnus-group-process-prefix n))
5746          (method (gnus-info-method (gnus-get-info vgroup))))
5747     (setcar (cdr method)
5748             (concat
5749              (nth 1 method) "\\|"
5750              (mapconcat
5751               (lambda (s)
5752                 (gnus-group-remove-mark s)
5753                 (concat "\\(^" (regexp-quote s) "$\\)"))
5754               groups "\\|"))))
5755   (gnus-group-position-point))
5756
5757 (defun gnus-group-make-empty-virtual (group)
5758   "Create a new, fresh, empty virtual group."
5759   (interactive "sCreate new, empty virtual group: ")
5760   (let* ((method (list 'nnvirtual "^$"))
5761          (pgroup (gnus-group-prefixed-name group method)))
5762     ;; Check whether it exists already.
5763     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5764          (error "Group %s already exists." pgroup))
5765     ;; Subscribe the new group after the group on the current line.
5766     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5767     (gnus-group-update-group pgroup)
5768     (forward-line -1)
5769     (gnus-group-position-point)))
5770
5771 (defun gnus-group-enter-directory (dir)
5772   "Enter an ephemeral nneething group."
5773   (interactive "DDirectory to read: ")
5774   (let* ((method (list 'nneething dir))
5775          (leaf (gnus-group-prefixed-name
5776                 (file-name-nondirectory (directory-file-name dir))
5777                 method))
5778          (name (gnus-generate-new-group-name leaf)))
5779     (let ((nneething-read-only t))
5780       (or (gnus-group-read-ephemeral-group
5781            name method t
5782            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5783                                       'summary 'group)))
5784           (error "Couldn't enter %s" dir)))))
5785
5786 ;; Group sorting commands
5787 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5788
5789 (defun gnus-group-sort-groups (func &optional reverse)
5790   "Sort the group buffer according to FUNC.
5791 If REVERSE, reverse the sorting order."
5792   (interactive (list gnus-group-sort-function
5793                      current-prefix-arg))
5794   (let ((func (cond 
5795                ((not (listp func)) func)
5796                ((null func) func)
5797                ((= 1 (length func)) (car func))
5798                (t `(lambda (t1 t2)
5799                      ,(gnus-make-sort-function 
5800                        (reverse func)))))))
5801     ;; We peel off the dummy group from the alist.
5802     (when func
5803       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5804         (pop gnus-newsrc-alist))
5805       ;; Do the sorting.
5806       (setq gnus-newsrc-alist
5807             (sort gnus-newsrc-alist func))
5808       (when reverse
5809         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5810       ;; Regenerate the hash table.
5811       (gnus-make-hashtable-from-newsrc-alist)
5812       (gnus-group-list-groups))))
5813
5814 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5815   "Sort the group buffer alphabetically by group name.
5816 If REVERSE, sort in reverse order."
5817   (interactive "P")
5818   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5819
5820 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5821   "Sort the group buffer by number of unread articles.
5822 If REVERSE, sort in reverse order."
5823   (interactive "P")
5824   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5825
5826 (defun gnus-group-sort-groups-by-level (&optional reverse)
5827   "Sort the group buffer by group level.
5828 If REVERSE, sort in reverse order."
5829   (interactive "P")
5830   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5831
5832 (defun gnus-group-sort-groups-by-score (&optional reverse)
5833   "Sort the group buffer by group score.
5834 If REVERSE, sort in reverse order."
5835   (interactive "P")
5836   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5837
5838 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5839   "Sort the group buffer by group rank.
5840 If REVERSE, sort in reverse order."
5841   (interactive "P")
5842   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5843
5844 (defun gnus-group-sort-groups-by-method (&optional reverse)
5845   "Sort the group buffer alphabetically by backend name.
5846 If REVERSE, sort in reverse order."
5847   (interactive "P")
5848   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
5849
5850 (defun gnus-group-sort-by-alphabet (info1 info2)
5851   "Sort alphabetically."
5852   (string< (gnus-info-group info1) (gnus-info-group info2)))
5853
5854 (defun gnus-group-sort-by-unread (info1 info2)
5855   "Sort by number of unread articles."
5856   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
5857         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
5858     (< (or (and (numberp n1) n1) 0)
5859        (or (and (numberp n2) n2) 0))))
5860
5861 (defun gnus-group-sort-by-level (info1 info2)
5862   "Sort by level."
5863   (< (gnus-info-level info1) (gnus-info-level info2)))
5864
5865 (defun gnus-group-sort-by-method (info1 info2)
5866   "Sort alphabetically by backend name."
5867   (string< (symbol-name (car (gnus-find-method-for-group
5868                               (gnus-info-group info1) info1)))
5869            (symbol-name (car (gnus-find-method-for-group
5870                               (gnus-info-group info2) info2)))))
5871
5872 (defun gnus-group-sort-by-score (info1 info2)
5873   "Sort by group score."
5874   (< (gnus-info-score info1) (gnus-info-score info2)))
5875
5876 (defun gnus-group-sort-by-rank (info1 info2)
5877   "Sort by level and score."
5878   (let ((level1 (gnus-info-level info1))
5879         (level2 (gnus-info-level info2)))
5880     (or (< level1 level2)
5881         (and (= level1 level2)
5882              (< (gnus-info-score info1) (gnus-info-score info2))))))
5883
5884 ;; Group catching up.
5885
5886 (defun gnus-group-clear-data (n)
5887   "Clear all marks and read ranges from the current group."
5888   (interactive "P")
5889   (let ((groups (gnus-group-process-prefix n))
5890         group info)
5891     (while (setq group (pop groups))
5892       (setq info (gnus-get-info group))
5893       (gnus-info-set-read info nil)
5894       (when (gnus-info-marks info)
5895         (gnus-info-set-marks info nil))
5896       (gnus-get-unread-articles-in-group info (gnus-active group) t)
5897       (when (gnus-group-goto-group group)
5898         (gnus-group-remove-mark group)
5899         (gnus-group-update-group-line)))))
5900
5901 (defun gnus-group-catchup-current (&optional n all)
5902   "Mark all articles not marked as unread in current newsgroup as read.
5903 If prefix argument N is numeric, the ARG next newsgroups will be
5904 caught up.  If ALL is non-nil, marked articles will also be marked as
5905 read.  Cross references (Xref: header) of articles are ignored.
5906 The difference between N and actual number of newsgroups that were
5907 caught up is returned."
5908   (interactive "P")
5909   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
5910                gnus-expert-user
5911                (gnus-y-or-n-p
5912                 (if all
5913                     "Do you really want to mark all articles as read? "
5914                   "Mark all unread articles as read? "))))
5915       n
5916     (let ((groups (gnus-group-process-prefix n))
5917           (ret 0))
5918       (while groups
5919         ;; Virtual groups have to be given special treatment.
5920         (let ((method (gnus-find-method-for-group (car groups))))
5921           (if (eq 'nnvirtual (car method))
5922               (nnvirtual-catchup-group
5923                (gnus-group-real-name (car groups)) (nth 1 method) all)))
5924         (gnus-group-remove-mark (car groups))
5925         (if (prog1
5926                 (gnus-group-goto-group (car groups))
5927               (gnus-group-catchup (car groups) all))
5928             (gnus-group-update-group-line)
5929           (setq ret (1+ ret)))
5930         (setq groups (cdr groups)))
5931       (gnus-group-next-unread-group 1)
5932       ret)))
5933
5934 (defun gnus-group-catchup-current-all (&optional n)
5935   "Mark all articles in current newsgroup as read.
5936 Cross references (Xref: header) of articles are ignored."
5937   (interactive "P")
5938   (gnus-group-catchup-current n 'all))
5939
5940 (defun gnus-group-catchup (group &optional all)
5941   "Mark all articles in GROUP as read.
5942 If ALL is non-nil, all articles are marked as read.
5943 The return value is the number of articles that were marked as read,
5944 or nil if no action could be taken."
5945   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5946          (num (car entry)))
5947     ;; Do the updating only if the newsgroup isn't killed.
5948     (if (not (numberp (car entry)))
5949         (gnus-message 1 "Can't catch up; non-active group")
5950       ;; Do auto-expirable marks if that's required.
5951       (when (gnus-group-auto-expirable-p group)
5952         (gnus-add-marked-articles
5953          group 'expire (gnus-list-of-unread-articles group))
5954         (when all
5955           (let ((marks (nth 3 (nth 2 entry))))
5956             (gnus-add-marked-articles
5957              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
5958             (gnus-add-marked-articles
5959              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
5960       (when entry
5961         (gnus-update-read-articles group nil)
5962         ;; Also nix out the lists of marks and dormants.
5963         (when all
5964           (gnus-add-marked-articles group 'tick nil nil 'force)
5965           (gnus-add-marked-articles group 'dormant nil nil 'force))
5966         (run-hooks 'gnus-group-catchup-group-hook)
5967         num))))
5968
5969 (defun gnus-group-expire-articles (&optional n)
5970   "Expire all expirable articles in the current newsgroup."
5971   (interactive "P")
5972   (let ((groups (gnus-group-process-prefix n))
5973         group)
5974     (unless groups
5975       (error "No groups to expire"))
5976     (while (setq group (pop groups))
5977       (gnus-group-remove-mark group)
5978       (when (gnus-check-backend-function 'request-expire-articles group)
5979         (gnus-message 6 "Expiring articles in %s..." group)
5980         (let* ((info (gnus-get-info group))
5981                (expirable (if (gnus-group-total-expirable-p group)
5982                               (cons nil (gnus-list-of-read-articles group))
5983                             (assq 'expire (gnus-info-marks info))))
5984                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
5985           (when expirable
5986             (setcdr
5987              expirable
5988              (gnus-compress-sequence
5989               (if expiry-wait
5990                   ;; We set the expiry variables to the groupp
5991                   ;; parameter. 
5992                   (let ((nnmail-expiry-wait-function nil)
5993                         (nnmail-expiry-wait expiry-wait))
5994                     (gnus-request-expire-articles
5995                      (gnus-uncompress-sequence (cdr expirable)) group))
5996                 ;; Just expire using the normal expiry values.
5997                 (gnus-request-expire-articles
5998                  (gnus-uncompress-sequence (cdr expirable)) group)))))
5999           (gnus-message 6 "Expiring articles in %s...done" group)))
6000       (gnus-group-position-point))))
6001
6002 (defun gnus-group-expire-all-groups ()
6003   "Expire all expirable articles in all newsgroups."
6004   (interactive)
6005   (save-excursion
6006     (gnus-message 5 "Expiring...")
6007     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
6008                                      (cdr gnus-newsrc-alist))))
6009       (gnus-group-expire-articles nil)))
6010   (gnus-group-position-point)
6011   (gnus-message 5 "Expiring...done"))
6012
6013 (defun gnus-group-set-current-level (n level)
6014   "Set the level of the next N groups to LEVEL."
6015   (interactive
6016    (list
6017     current-prefix-arg
6018     (string-to-int
6019      (let ((s (read-string
6020                (format "Level (default %s): "
6021                        (or (gnus-group-group-level) 
6022                            gnus-level-default-subscribed)))))
6023        (if (string-match "^\\s-*$" s)
6024            (int-to-string (or (gnus-group-group-level) 
6025                               gnus-level-default-subscribed))
6026          s)))))
6027   (or (and (>= level 1) (<= level gnus-level-killed))
6028       (error "Illegal level: %d" level))
6029   (let ((groups (gnus-group-process-prefix n))
6030         group)
6031     (while (setq group (pop groups))
6032       (gnus-group-remove-mark group)
6033       (gnus-message 6 "Changed level of %s from %d to %d"
6034                     group (or (gnus-group-group-level) gnus-level-killed)
6035                     level)
6036       (gnus-group-change-level
6037        group level (or (gnus-group-group-level) gnus-level-killed))
6038       (gnus-group-update-group-line)))
6039   (gnus-group-position-point))
6040
6041 (defun gnus-group-unsubscribe-current-group (&optional n)
6042   "Toggle subscription of the current group.
6043 If given numerical prefix, toggle the N next groups."
6044   (interactive "P")
6045   (let ((groups (gnus-group-process-prefix n))
6046         group)
6047     (while groups
6048       (setq group (car groups)
6049             groups (cdr groups))
6050       (gnus-group-remove-mark group)
6051       (gnus-group-unsubscribe-group
6052        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
6053                  gnus-level-default-unsubscribed
6054                gnus-level-default-subscribed) t)
6055       (gnus-group-update-group-line))
6056     (gnus-group-next-group 1)))
6057
6058 (defun gnus-group-unsubscribe-group (group &optional level silent)
6059   "Toggle subscription to GROUP.
6060 Killed newsgroups are subscribed.  If SILENT, don't try to update the
6061 group line."
6062   (interactive
6063    (list (completing-read
6064           "Group: " gnus-active-hashtb nil
6065           (memq gnus-select-method gnus-have-read-active-file))))
6066   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
6067     (cond
6068      ((string-match "^[ \t]$" group)
6069       (error "Empty group name"))
6070      (newsrc
6071       ;; Toggle subscription flag.
6072       (gnus-group-change-level
6073        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
6074                                       gnus-level-subscribed)
6075                                   (1+ gnus-level-subscribed)
6076                                 gnus-level-default-subscribed)))
6077       (unless silent
6078         (gnus-group-update-group group)))
6079      ((and (stringp group)
6080            (or (not (memq gnus-select-method gnus-have-read-active-file))
6081                (gnus-active group)))
6082       ;; Add new newsgroup.
6083       (gnus-group-change-level
6084        group
6085        (if level level gnus-level-default-subscribed)
6086        (or (and (member group gnus-zombie-list)
6087                 gnus-level-zombie)
6088            gnus-level-killed)
6089        (and (gnus-group-group-name)
6090             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
6091       (unless silent
6092         (gnus-group-update-group group)))
6093      (t (error "No such newsgroup: %s" group)))
6094     (gnus-group-position-point)))
6095
6096 (defun gnus-group-transpose-groups (n)
6097   "Move the current newsgroup up N places.
6098 If given a negative prefix, move down instead.  The difference between
6099 N and the number of steps taken is returned."
6100   (interactive "p")
6101   (or (gnus-group-group-name)
6102       (error "No group on current line"))
6103   (gnus-group-kill-group 1)
6104   (prog1
6105       (forward-line (- n))
6106     (gnus-group-yank-group)
6107     (gnus-group-position-point)))
6108
6109 (defun gnus-group-kill-all-zombies ()
6110   "Kill all zombie newsgroups."
6111   (interactive)
6112   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6113   (setq gnus-zombie-list nil)
6114   (gnus-group-list-groups))
6115
6116 (defun gnus-group-kill-region (begin end)
6117   "Kill newsgroups in current region (excluding current point).
6118 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6119   (interactive "r")
6120   (let ((lines
6121          ;; Count lines.
6122          (save-excursion
6123            (count-lines
6124             (progn
6125               (goto-char begin)
6126               (beginning-of-line)
6127               (point))
6128             (progn
6129               (goto-char end)
6130               (beginning-of-line)
6131               (point))))))
6132     (goto-char begin)
6133     (beginning-of-line)                 ;Important when LINES < 1
6134     (gnus-group-kill-group lines)))
6135
6136 (defun gnus-group-kill-group (&optional n discard)
6137   "Kill the next N groups.
6138 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6139 However, only groups that were alive can be yanked; already killed
6140 groups or zombie groups can't be yanked.
6141 The return value is the name of the group that was killed, or a list
6142 of groups killed."
6143   (interactive "P")
6144   (let ((buffer-read-only nil)
6145         (groups (gnus-group-process-prefix n))
6146         group entry level out)
6147     (if (< (length groups) 10)
6148         ;; This is faster when there are few groups.
6149         (while groups
6150           (push (setq group (pop groups)) out)
6151           (gnus-group-remove-mark group)
6152           (setq level (gnus-group-group-level))
6153           (gnus-delete-line)
6154           (when (and (not discard)
6155                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6156             (push (cons (car entry) (nth 2 entry))
6157                   gnus-list-of-killed-groups))
6158           (gnus-group-change-level
6159            (if entry entry group) gnus-level-killed (if entry nil level)))
6160       ;; If there are lots and lots of groups to be killed, we use
6161       ;; this thing instead.
6162       (let (entry)
6163         (setq groups (nreverse groups))
6164         (while groups
6165           (gnus-group-remove-mark (setq group (pop groups)))
6166           (gnus-delete-line)
6167           (cond
6168            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6169             (push (cons (car entry) (nth 2 entry))
6170                   gnus-list-of-killed-groups)
6171             (setcdr (cdr entry) (cdddr entry)))
6172            ((member group gnus-zombie-list)
6173             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6174         (gnus-make-hashtable-from-newsrc-alist)))
6175
6176     (gnus-group-position-point)
6177     (if (< (length out) 2) (car out) (nreverse out))))
6178
6179 (defun gnus-group-yank-group (&optional arg)
6180   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6181 inserting it before the current newsgroup.  The numeric ARG specifies
6182 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6183 is returned, or (if several groups are yanked) a list of yanked groups
6184 is returned."
6185   (interactive "p")
6186   (setq arg (or arg 1))
6187   (let (info group prev out)
6188     (while (>= (decf arg) 0)
6189       (if (not (setq info (pop gnus-list-of-killed-groups)))
6190           (error "No more newsgroups to yank"))
6191       (push (setq group (nth 1 info)) out)
6192       ;; Find which newsgroup to insert this one before - search
6193       ;; backward until something suitable is found.  If there are no
6194       ;; other newsgroups in this buffer, just make this newsgroup the
6195       ;; first newsgroup.
6196       (setq prev (gnus-group-group-name))
6197       (gnus-group-change-level
6198        info (gnus-info-level (cdr info)) gnus-level-killed
6199        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6200        t)
6201       (gnus-group-insert-group-line-info group))
6202     (forward-line -1)
6203     (gnus-group-position-point)
6204     (if (< (length out) 2) (car out) (nreverse out))))
6205
6206 (defun gnus-group-kill-level (level)
6207   "Kill all groups that is on a certain LEVEL."
6208   (interactive "nKill all groups on level: ")
6209   (cond
6210    ((= level gnus-level-zombie)
6211     (setq gnus-killed-list
6212           (nconc gnus-zombie-list gnus-killed-list))
6213     (setq gnus-zombie-list nil))
6214    ((and (< level gnus-level-zombie)
6215          (> level 0)
6216          (or gnus-expert-user
6217              (gnus-yes-or-no-p
6218               (format
6219                "Do you really want to kill all groups on level %d? "
6220                level))))
6221     (let* ((prev gnus-newsrc-alist)
6222            (alist (cdr prev)))
6223       (while alist
6224         (if (= (gnus-info-level level) level)
6225             (setcdr prev (cdr alist))
6226           (setq prev alist))
6227         (setq alist (cdr alist)))
6228       (gnus-make-hashtable-from-newsrc-alist)
6229       (gnus-group-list-groups)))
6230    (t
6231     (error "Can't kill; illegal level: %d" level))))
6232
6233 (defun gnus-group-list-all-groups (&optional arg)
6234   "List all newsgroups with level ARG or lower.
6235 Default is gnus-level-unsubscribed, which lists all subscribed and most
6236 unsubscribed groups."
6237   (interactive "P")
6238   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6239
6240 ;; Redefine this to list ALL killed groups if prefix arg used.
6241 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6242 (defun gnus-group-list-killed (&optional arg)
6243   "List all killed newsgroups in the group buffer.
6244 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6245 entail asking the server for the groups."
6246   (interactive "P")
6247   ;; Find all possible killed newsgroups if arg.
6248   (when arg
6249     ;; First make sure active file has been read.
6250     (unless gnus-have-read-active-file
6251       (let ((gnus-read-active-file t))
6252         (gnus-read-active-file)))
6253     (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
6254     ;; Go through all newsgroups that are known to Gnus - enlarge kill list
6255     (mapatoms
6256      (lambda (sym)
6257        (let ((groups 0)
6258              (group (symbol-name sym)))
6259          (if (or (null group)
6260                  (gnus-gethash group gnus-killed-hashtb)
6261                  (gnus-gethash group gnus-newsrc-hashtb))
6262              ()
6263            (let ((do-sub (gnus-matches-options-n group)))
6264              (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
6265                  ()
6266                (setq groups (1+ groups))
6267                (setq gnus-killed-list
6268                      (cons group gnus-killed-list))
6269                (gnus-sethash group group gnus-killed-hashtb))))))
6270      gnus-active-hashtb))
6271   (if (not gnus-killed-list)
6272       (gnus-message 6 "No killed groups")
6273     (let (gnus-group-list-mode)
6274       (funcall gnus-group-prepare-function
6275                gnus-level-killed t gnus-level-killed))
6276     (goto-char (point-min)))
6277   (gnus-group-position-point))
6278
6279 (defun gnus-group-list-zombies ()
6280   "List all zombie newsgroups in the group buffer."
6281   (interactive)
6282   (if (not gnus-zombie-list)
6283       (gnus-message 6 "No zombie groups")
6284     (let (gnus-group-list-mode)
6285       (funcall gnus-group-prepare-function
6286                gnus-level-zombie t gnus-level-zombie))
6287     (goto-char (point-min)))
6288   (gnus-group-position-point))
6289
6290 (defun gnus-group-list-active ()
6291   "List all groups that are available from the server(s)."
6292   (interactive)
6293   ;; First we make sure that we have really read the active file.
6294   (unless gnus-have-read-active-file
6295     (let ((gnus-read-active-file t))
6296       (gnus-read-active-file)))
6297   ;; Find all groups and sort them.
6298   (let ((groups
6299          (sort
6300           (let (list)
6301             (mapatoms
6302              (lambda (sym)
6303                (and (symbol-value sym)
6304                     (setq list (cons (symbol-name sym) list))))
6305              gnus-active-hashtb)
6306             list)
6307           'string<))
6308         (buffer-read-only nil))
6309     (erase-buffer)
6310     (while groups
6311       (gnus-group-insert-group-line-info (pop groups)))
6312     (goto-char (point-min))))
6313
6314 (defun gnus-activate-all-groups (level)
6315   "Activate absolutely all groups."
6316   (interactive (list 7))
6317   (let ((gnus-activate-level level)
6318         (gnus-activate-foreign-newsgroups level))
6319     (gnus-group-get-new-news)))
6320
6321 (defun gnus-group-get-new-news (&optional arg)
6322   "Get newly arrived articles.
6323 If ARG is a number, it specifies which levels you are interested in
6324 re-scanning.  If ARG is non-nil and not a number, this will force
6325 \"hard\" re-reading of the active files from all servers."
6326   (interactive "P")
6327   (run-hooks 'gnus-get-new-news-hook)
6328   ;; We might read in new NoCeM messages here.
6329   (when (and gnus-use-nocem 
6330              (null arg))
6331     (gnus-nocem-scan-groups))
6332   ;; If ARG is not a number, then we read the active file.
6333   (when (and arg (not (numberp arg)))
6334     (let ((gnus-read-active-file t))
6335       (gnus-read-active-file))
6336     (setq arg nil))
6337
6338   (setq arg (gnus-group-default-level arg t))
6339   (if (and gnus-read-active-file (not arg))
6340       (progn
6341         (gnus-read-active-file)
6342         (gnus-get-unread-articles arg))
6343     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6344       (gnus-get-unread-articles arg)))
6345   (run-hooks 'gnus-after-getting-new-news-hook)
6346   (gnus-group-list-groups))
6347
6348 (defun gnus-group-get-new-news-this-group (&optional n)
6349   "Check for newly arrived news in the current group (and the N-1 next groups).
6350 The difference between N and the number of newsgroup checked is returned.
6351 If N is negative, this group and the N-1 previous groups will be checked."
6352   (interactive "P")
6353   (let* ((groups (gnus-group-process-prefix n))
6354          (ret (if (numberp n) (- n (length groups)) 0))
6355          group)
6356     (while groups
6357       (setq group (car groups)
6358             groups (cdr groups))
6359       (gnus-group-remove-mark group)
6360       (if (and group (gnus-activate-group group 'scan))
6361           (progn
6362             (gnus-get-unread-articles-in-group
6363              (gnus-get-info group) (gnus-active group) t)
6364             (gnus-close-group group)
6365             (gnus-group-update-group group))
6366         (ding)
6367         (gnus-message 3 "%s error: %s" group (gnus-status-message group))))
6368     (when gnus-goto-next-group-when-activating
6369       (gnus-group-next-unread-group 1 t))
6370     (gnus-summary-position-point)
6371     ret))
6372
6373 (defun gnus-group-fetch-faq (group &optional faq-dir)
6374   "Fetch the FAQ for the current group."
6375   (interactive
6376    (list
6377     (gnus-group-real-name (gnus-group-group-name))
6378     (cond (current-prefix-arg
6379            (completing-read
6380             "Faq dir: " (and (listp gnus-group-faq-directory)
6381                              gnus-group-faq-directory))))))
6382   (or faq-dir
6383       (setq faq-dir (if (listp gnus-group-faq-directory)
6384                         (car gnus-group-faq-directory)
6385                       gnus-group-faq-directory)))
6386   (or group (error "No group name given"))
6387   (let ((file (concat (file-name-as-directory faq-dir)
6388                       (gnus-group-real-name group))))
6389     (if (not (file-exists-p file))
6390         (error "No such file: %s" file)
6391       (find-file file))))
6392
6393 (defun gnus-group-describe-group (force &optional group)
6394   "Display a description of the current newsgroup."
6395   (interactive (list current-prefix-arg (gnus-group-group-name)))
6396   (and force (setq gnus-description-hashtb nil))
6397   (let ((method (gnus-find-method-for-group group))
6398         desc)
6399     (or group (error "No group name given"))
6400     (and (or (and gnus-description-hashtb
6401                   ;; We check whether this group's method has been
6402                   ;; queried for a description file.
6403                   (gnus-gethash
6404                    (gnus-group-prefixed-name "" method)
6405                    gnus-description-hashtb))
6406              (setq desc (gnus-group-get-description group))
6407              (gnus-read-descriptions-file method))
6408          (message
6409           (or desc (gnus-gethash group gnus-description-hashtb)
6410               "No description available")))))
6411
6412 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6413 (defun gnus-group-describe-all-groups (&optional force)
6414   "Pop up a buffer with descriptions of all newsgroups."
6415   (interactive "P")
6416   (and force (setq gnus-description-hashtb nil))
6417   (if (not (or gnus-description-hashtb
6418                (gnus-read-all-descriptions-files)))
6419       (error "Couldn't request descriptions file"))
6420   (let ((buffer-read-only nil)
6421         b)
6422     (erase-buffer)
6423     (mapatoms
6424      (lambda (group)
6425        (setq b (point))
6426        (insert (format "      *: %-20s %s\n" (symbol-name group)
6427                        (symbol-value group)))
6428        (add-text-properties
6429         b (1+ b) (list 'gnus-group group
6430                        'gnus-unread t 'gnus-marked nil
6431                        'gnus-level (1+ gnus-level-subscribed))))
6432      gnus-description-hashtb)
6433     (goto-char (point-min))
6434     (gnus-group-position-point)))
6435
6436 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6437 (defun gnus-group-apropos (regexp &optional search-description)
6438   "List all newsgroups that have names that match a regexp."
6439   (interactive "sGnus apropos (regexp): ")
6440   (let ((prev "")
6441         (obuf (current-buffer))
6442         groups des)
6443     ;; Go through all newsgroups that are known to Gnus.
6444     (mapatoms
6445      (lambda (group)
6446        (and (symbol-name group)
6447             (string-match regexp (symbol-name group))
6448             (setq groups (cons (symbol-name group) groups))))
6449      gnus-active-hashtb)
6450     ;; Also go through all descriptions that are known to Gnus.
6451     (when search-description
6452       (mapatoms
6453        (lambda (group)
6454          (and (string-match regexp (symbol-value group))
6455               (gnus-active (symbol-name group))
6456               (setq groups (cons (symbol-name group) groups))))
6457        gnus-description-hashtb))
6458     (if (not groups)
6459         (gnus-message 3 "No groups matched \"%s\"." regexp)
6460       ;; Print out all the groups.
6461       (save-excursion
6462         (pop-to-buffer "*Gnus Help*")
6463         (buffer-disable-undo (current-buffer))
6464         (erase-buffer)
6465         (setq groups (sort groups 'string<))
6466         (while groups
6467           ;; Groups may be entered twice into the list of groups.
6468           (if (not (string= (car groups) prev))
6469               (progn
6470                 (insert (setq prev (car groups)) "\n")
6471                 (if (and gnus-description-hashtb
6472                          (setq des (gnus-gethash (car groups)
6473                                                  gnus-description-hashtb)))
6474                     (insert "  " des "\n"))))
6475           (setq groups (cdr groups)))
6476         (goto-char (point-min))))
6477     (pop-to-buffer obuf)))
6478
6479 (defun gnus-group-description-apropos (regexp)
6480   "List all newsgroups that have names or descriptions that match a regexp."
6481   (interactive "sGnus description apropos (regexp): ")
6482   (if (not (or gnus-description-hashtb
6483                (gnus-read-all-descriptions-files)))
6484       (error "Couldn't request descriptions file"))
6485   (gnus-group-apropos regexp t))
6486
6487 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6488 (defun gnus-group-list-matching (level regexp &optional all lowest)
6489   "List all groups with unread articles that match REGEXP.
6490 If the prefix LEVEL is non-nil, it should be a number that says which
6491 level to cut off listing groups.
6492 If ALL, also list groups with no unread articles.
6493 If LOWEST, don't list groups with level lower than LOWEST."
6494   (interactive "P\nsList newsgroups matching: ")
6495   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6496                            all (or lowest 1) regexp)
6497   (goto-char (point-min))
6498   (gnus-group-position-point))
6499
6500 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6501   "List all groups that match REGEXP.
6502 If the prefix LEVEL is non-nil, it should be a number that says which
6503 level to cut off listing groups.
6504 If LOWEST, don't list groups with level lower than LOWEST."
6505   (interactive "P\nsList newsgroups matching: ")
6506   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6507
6508 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6509 (defun gnus-group-save-newsrc (&optional force)
6510   "Save the Gnus startup files.
6511 If FORCE, force saving whether it is necessary or not."
6512   (interactive "P")
6513   (gnus-save-newsrc-file force))
6514
6515 (defun gnus-group-restart (&optional arg)
6516   "Force Gnus to read the .newsrc file."
6517   (interactive "P")
6518   (when (gnus-yes-or-no-p
6519          (format "Are you sure you want to read %s? "
6520                  gnus-current-startup-file))
6521     (gnus-save-newsrc-file)
6522     (gnus-setup-news 'force)
6523     (gnus-group-list-groups arg)))
6524
6525 (defun gnus-group-read-init-file ()
6526   "Read the Gnus elisp init file."
6527   (interactive)
6528   (gnus-read-init-file))
6529
6530 (defun gnus-group-check-bogus-groups (&optional silent)
6531   "Check bogus newsgroups.
6532 If given a prefix, don't ask for confirmation before removing a bogus
6533 group."
6534   (interactive "P")
6535   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6536   (gnus-group-list-groups))
6537
6538 (defun gnus-group-edit-global-kill (&optional article group)
6539   "Edit the global kill file.
6540 If GROUP, edit that local kill file instead."
6541   (interactive "P")
6542   (setq gnus-current-kill-article article)
6543   (gnus-kill-file-edit-file group)
6544   (gnus-message
6545    6
6546    (substitute-command-keys
6547     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6548             (if group "local" "global")))))
6549
6550 (defun gnus-group-edit-local-kill (article group)
6551   "Edit a local kill file."
6552   (interactive (list nil (gnus-group-group-name)))
6553   (gnus-group-edit-global-kill article group))
6554
6555 (defun gnus-group-force-update ()
6556   "Update `.newsrc' file."
6557   (interactive)
6558   (gnus-save-newsrc-file))
6559
6560 (defun gnus-group-suspend ()
6561   "Suspend the current Gnus session.
6562 In fact, cleanup buffers except for group mode buffer.
6563 The hook gnus-suspend-gnus-hook is called before actually suspending."
6564   (interactive)
6565   (run-hooks 'gnus-suspend-gnus-hook)
6566   ;; Kill Gnus buffers except for group mode buffer.
6567   (let ((group-buf (get-buffer gnus-group-buffer)))
6568     ;; Do this on a separate list in case the user does a ^G before we finish
6569     (let ((gnus-buffer-list
6570            (delq group-buf (delq gnus-dribble-buffer
6571                                  (append gnus-buffer-list nil)))))
6572       (while gnus-buffer-list
6573         (gnus-kill-buffer (car gnus-buffer-list))
6574         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6575     (if group-buf
6576         (progn
6577           (setq gnus-buffer-list (list group-buf))
6578           (bury-buffer group-buf)
6579           (delete-windows-on group-buf t)))))
6580
6581 (defun gnus-group-clear-dribble ()
6582   "Clear all information from the dribble buffer."
6583   (interactive)
6584   (gnus-dribble-clear)
6585   (gnus-message 7 "Cleared dribble buffer"))
6586
6587 (defun gnus-group-exit ()
6588   "Quit reading news after updating .newsrc.eld and .newsrc.
6589 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6590   (interactive)
6591   (when 
6592       (or noninteractive                ;For gnus-batch-kill
6593           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
6594           (not gnus-interactive-exit)   ;Without confirmation
6595           gnus-expert-user
6596           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6597     (run-hooks 'gnus-exit-gnus-hook)
6598     ;; Offer to save data from non-quitted summary buffers.
6599     (gnus-offer-save-summaries)
6600     ;; Save the newsrc file(s).
6601     (gnus-save-newsrc-file)
6602     ;; Kill-em-all.
6603     (gnus-close-backends)
6604     ;; Reset everything.
6605     (gnus-clear-system)
6606     ;; Allow the user to do things after cleaning up.
6607     (run-hooks 'gnus-after-exiting-gnus-hook)))
6608
6609 (defun gnus-close-backends ()
6610   ;; Send a close request to all backends that support such a request.
6611   (let ((methods gnus-valid-select-methods)
6612         func)
6613     (while methods
6614       (if (fboundp (setq func (intern (concat (caar methods)
6615                                               "-request-close"))))
6616           (funcall func))
6617       (setq methods (cdr methods)))))
6618
6619 (defun gnus-group-quit ()
6620   "Quit reading news without updating .newsrc.eld or .newsrc.
6621 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6622   (interactive)
6623   (when (or noninteractive              ;For gnus-batch-kill
6624             (zerop (buffer-size))
6625             (not (gnus-server-opened gnus-select-method))
6626             gnus-expert-user
6627             (not gnus-current-startup-file)
6628             (gnus-yes-or-no-p
6629              (format "Quit reading news without saving %s? "
6630                      (file-name-nondirectory gnus-current-startup-file))))
6631     (run-hooks 'gnus-exit-gnus-hook)
6632     (if gnus-use-full-window
6633         (delete-other-windows)
6634       (gnus-remove-some-windows))
6635     (gnus-dribble-save)
6636     (gnus-close-backends)
6637     (gnus-clear-system)
6638     ;; Allow the user to do things after cleaning up.
6639     (run-hooks 'gnus-after-exiting-gnus-hook)))
6640
6641 (defun gnus-offer-save-summaries ()
6642   "Offer to save all active summary buffers."
6643   (save-excursion
6644     (let ((buflist (buffer-list))
6645           buffers bufname)
6646       ;; Go through all buffers and find all summaries.
6647       (while buflist
6648         (and (setq bufname (buffer-name (car buflist)))
6649              (string-match "Summary" bufname)
6650              (save-excursion
6651                (set-buffer bufname)
6652                ;; We check that this is, indeed, a summary buffer.
6653                (and (eq major-mode 'gnus-summary-mode)
6654                     ;; Also make sure this isn't bogus.
6655                     gnus-newsgroup-prepared))
6656              (push bufname buffers))
6657         (setq buflist (cdr buflist)))
6658       ;; Go through all these summary buffers and offer to save them.
6659       (when buffers
6660         (map-y-or-n-p
6661          "Update summary buffer %s? "
6662          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6663          buffers)))))
6664
6665 (defun gnus-group-describe-briefly ()
6666   "Give a one line description of the group mode commands."
6667   (interactive)
6668   (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")))
6669
6670 (defun gnus-group-browse-foreign-server (method)
6671   "Browse a foreign news server.
6672 If called interactively, this function will ask for a select method
6673  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6674 If not, METHOD should be a list where the first element is the method
6675 and the second element is the address."
6676   (interactive
6677    (list (let ((how (completing-read
6678                      "Which backend: "
6679                      (append gnus-valid-select-methods gnus-server-alist)
6680                      nil t (cons "nntp" 0))))
6681            ;; We either got a backend name or a virtual server name.
6682            ;; If the first, we also need an address.
6683            (if (assoc how gnus-valid-select-methods)
6684                (list (intern how)
6685                      ;; Suggested by mapjph@bath.ac.uk.
6686                      (completing-read
6687                       "Address: "
6688                       (mapcar (lambda (server) (list server))
6689                               gnus-secondary-servers)))
6690              ;; We got a server name, so we find the method.
6691              (gnus-server-to-method how)))))
6692   (gnus-browse-foreign-server method))
6693
6694 \f
6695 ;;;
6696 ;;; Gnus summary mode
6697 ;;;
6698
6699 (defvar gnus-summary-mode-map nil)
6700
6701 (put 'gnus-summary-mode 'mode-class 'special)
6702
6703 (unless gnus-summary-mode-map
6704   (setq gnus-summary-mode-map (make-keymap))
6705   (suppress-keymap gnus-summary-mode-map)
6706
6707   ;; Non-orthogonal keys
6708
6709   (gnus-define-keys gnus-summary-mode-map
6710     " " gnus-summary-next-page
6711     "\177" gnus-summary-prev-page
6712     [delete] gnus-summary-prev-page
6713     "\r" gnus-summary-scroll-up
6714     "n" gnus-summary-next-unread-article
6715     "p" gnus-summary-prev-unread-article
6716     "N" gnus-summary-next-article
6717     "P" gnus-summary-prev-article
6718     "\M-\C-n" gnus-summary-next-same-subject
6719     "\M-\C-p" gnus-summary-prev-same-subject
6720     "\M-n" gnus-summary-next-unread-subject
6721     "\M-p" gnus-summary-prev-unread-subject
6722     "." gnus-summary-first-unread-article
6723     "," gnus-summary-best-unread-article
6724     "\M-s" gnus-summary-search-article-forward
6725     "\M-r" gnus-summary-search-article-backward
6726     "<" gnus-summary-beginning-of-article
6727     ">" gnus-summary-end-of-article
6728     "j" gnus-summary-goto-article
6729     "^" gnus-summary-refer-parent-article
6730     "\M-^" gnus-summary-refer-article
6731     "u" gnus-summary-tick-article-forward
6732     "!" gnus-summary-tick-article-forward
6733     "U" gnus-summary-tick-article-backward
6734     "d" gnus-summary-mark-as-read-forward
6735     "D" gnus-summary-mark-as-read-backward
6736     "E" gnus-summary-mark-as-expirable
6737     "\M-u" gnus-summary-clear-mark-forward
6738     "\M-U" gnus-summary-clear-mark-backward
6739     "k" gnus-summary-kill-same-subject-and-select
6740     "\C-k" gnus-summary-kill-same-subject
6741     "\M-\C-k" gnus-summary-kill-thread
6742     "\M-\C-l" gnus-summary-lower-thread
6743     "e" gnus-summary-edit-article
6744     "#" gnus-summary-mark-as-processable
6745     "\M-#" gnus-summary-unmark-as-processable
6746     "\M-\C-t" gnus-summary-toggle-threads
6747     "\M-\C-s" gnus-summary-show-thread
6748     "\M-\C-h" gnus-summary-hide-thread
6749     "\M-\C-f" gnus-summary-next-thread
6750     "\M-\C-b" gnus-summary-prev-thread
6751     "\M-\C-u" gnus-summary-up-thread
6752     "\M-\C-d" gnus-summary-down-thread
6753     "&" gnus-summary-execute-command
6754     "c" gnus-summary-catchup-and-exit
6755     "\C-w" gnus-summary-mark-region-as-read
6756     "\C-t" gnus-summary-toggle-truncation
6757     "?" gnus-summary-mark-as-dormant
6758     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6759     "\C-c\C-s\C-n" gnus-summary-sort-by-number
6760     "\C-c\C-s\C-a" gnus-summary-sort-by-author
6761     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6762     "\C-c\C-s\C-d" gnus-summary-sort-by-date
6763     "\C-c\C-s\C-i" gnus-summary-sort-by-score
6764     "=" gnus-summary-expand-window
6765     "\C-x\C-s" gnus-summary-reselect-current-group
6766     "\M-g" gnus-summary-rescan-group
6767     "w" gnus-summary-stop-page-breaking
6768     "\C-c\C-r" gnus-summary-caesar-message
6769     "\M-t" gnus-summary-toggle-mime
6770     "f" gnus-summary-followup
6771     "F" gnus-summary-followup-with-original
6772     "C" gnus-summary-cancel-article
6773     "r" gnus-summary-reply
6774     "R" gnus-summary-reply-with-original
6775     "\C-c\C-f" gnus-summary-mail-forward
6776     "o" gnus-summary-save-article
6777     "\C-o" gnus-summary-save-article-mail
6778     "|" gnus-summary-pipe-output
6779     "\M-k" gnus-summary-edit-local-kill
6780     "\M-K" gnus-summary-edit-global-kill
6781     "V" gnus-version
6782     "\C-c\C-d" gnus-summary-describe-group
6783     "q" gnus-summary-exit
6784     "Q" gnus-summary-exit-no-update
6785     "\C-c\C-i" gnus-info-find-node
6786     gnus-mouse-2 gnus-mouse-pick-article
6787     "m" gnus-summary-mail-other-window
6788     "a" gnus-summary-post-news
6789     "x" gnus-summary-limit-to-unread
6790     "s" gnus-summary-isearch-article
6791     "t" gnus-article-hide-headers
6792     "g" gnus-summary-show-article
6793     "l" gnus-summary-goto-last-article
6794     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6795     "\C-d" gnus-summary-enter-digest-group
6796     "\C-c\C-b" gnus-bug
6797     "*" gnus-cache-enter-article
6798     "\M-*" gnus-cache-remove-article
6799     "\M-&" gnus-summary-universal-argument
6800     "\C-l" gnus-recenter
6801     "I" gnus-summary-increase-score
6802     "L" gnus-summary-lower-score
6803
6804     "V" gnus-summary-score-map
6805     "X" gnus-uu-extract-map
6806     "S" gnus-summary-send-map)
6807
6808   ;; Sort of orthogonal keymap
6809   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
6810     "t" gnus-summary-tick-article-forward
6811     "!" gnus-summary-tick-article-forward
6812     "d" gnus-summary-mark-as-read-forward
6813     "r" gnus-summary-mark-as-read-forward
6814     "c" gnus-summary-clear-mark-forward
6815     " " gnus-summary-clear-mark-forward
6816     "e" gnus-summary-mark-as-expirable
6817     "x" gnus-summary-mark-as-expirable
6818     "?" gnus-summary-mark-as-dormant
6819     "b" gnus-summary-set-bookmark
6820     "B" gnus-summary-remove-bookmark
6821     "#" gnus-summary-mark-as-processable
6822     "\M-#" gnus-summary-unmark-as-processable
6823     "S" gnus-summary-limit-include-expunged
6824     "C" gnus-summary-catchup
6825     "H" gnus-summary-catchup-to-here
6826     "\C-c" gnus-summary-catchup-all
6827     "k" gnus-summary-kill-same-subject-and-select
6828     "K" gnus-summary-kill-same-subject
6829     "P" gnus-uu-mark-map)
6830
6831   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
6832     "c" gnus-summary-clear-above
6833     "u" gnus-summary-tick-above
6834     "m" gnus-summary-mark-above
6835     "k" gnus-summary-kill-below)
6836
6837   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
6838     "/" gnus-summary-limit-to-subject
6839     "n" gnus-summary-limit-to-articles
6840     "w" gnus-summary-pop-limit
6841     "s" gnus-summary-limit-to-subject
6842     "a" gnus-summary-limit-to-author
6843     "u" gnus-summary-limit-to-unread
6844     "m" gnus-summary-limit-to-marks
6845     "v" gnus-summary-limit-to-score
6846     "D" gnus-summary-limit-include-dormant
6847     "d" gnus-summary-limit-exclude-dormant
6848     ;;  "t" gnus-summary-limit-exclude-thread
6849     "E" gnus-summary-limit-include-expunged
6850     "c" gnus-summary-limit-exclude-childless-dormant
6851     "C" gnus-summary-limit-mark-excluded-as-read)
6852
6853   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
6854     "n" gnus-summary-next-unread-article
6855     "p" gnus-summary-prev-unread-article
6856     "N" gnus-summary-next-article
6857     "P" gnus-summary-prev-article
6858     "\C-n" gnus-summary-next-same-subject
6859     "\C-p" gnus-summary-prev-same-subject
6860     "\M-n" gnus-summary-next-unread-subject
6861     "\M-p" gnus-summary-prev-unread-subject
6862     "f" gnus-summary-first-unread-article
6863     "b" gnus-summary-best-unread-article
6864     "j" gnus-summary-goto-article
6865     "g" gnus-summary-goto-subject
6866     "l" gnus-summary-goto-last-article
6867     "p" gnus-summary-pop-article)
6868
6869   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
6870     "k" gnus-summary-kill-thread
6871     "l" gnus-summary-lower-thread
6872     "i" gnus-summary-raise-thread
6873     "T" gnus-summary-toggle-threads
6874     "t" gnus-summary-rethread-current
6875     "^" gnus-summary-reparent-thread
6876     "s" gnus-summary-show-thread
6877     "S" gnus-summary-show-all-threads
6878     "h" gnus-summary-hide-thread
6879     "H" gnus-summary-hide-all-threads
6880     "n" gnus-summary-next-thread
6881     "p" gnus-summary-prev-thread
6882     "u" gnus-summary-up-thread
6883     "o" gnus-summary-top-thread
6884     "d" gnus-summary-down-thread
6885     "#" gnus-uu-mark-thread
6886     "\M-#" gnus-uu-unmark-thread)
6887
6888   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
6889     "c" gnus-summary-catchup-and-exit
6890     "C" gnus-summary-catchup-all-and-exit
6891     "E" gnus-summary-exit-no-update
6892     "Q" gnus-summary-exit
6893     "Z" gnus-summary-exit
6894     "n" gnus-summary-catchup-and-goto-next-group
6895     "R" gnus-summary-reselect-current-group
6896     "G" gnus-summary-rescan-group
6897     "N" gnus-summary-next-group
6898     "P" gnus-summary-prev-group)
6899
6900   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
6901     " " gnus-summary-next-page
6902     "n" gnus-summary-next-page
6903     "\177" gnus-summary-prev-page
6904     [delete] gnus-summary-prev-page
6905     "p" gnus-summary-prev-page
6906     "\r" gnus-summary-scroll-up
6907     "<" gnus-summary-beginning-of-article
6908     ">" gnus-summary-end-of-article
6909     "b" gnus-summary-beginning-of-article
6910     "e" gnus-summary-end-of-article
6911     "^" gnus-summary-refer-parent-article
6912     "r" gnus-summary-refer-parent-article
6913     "R" gnus-summary-refer-references
6914     "g" gnus-summary-show-article
6915     "s" gnus-summary-isearch-article)
6916
6917   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
6918     "b" gnus-article-add-buttons
6919     "B" gnus-article-add-buttons-to-head
6920     "o" gnus-article-treat-overstrike
6921     ;;  "w" gnus-article-word-wrap
6922     "w" gnus-article-fill-cited-article
6923     "c" gnus-article-remove-cr
6924     "L" gnus-article-remove-trailing-blank-lines
6925     "q" gnus-article-de-quoted-unreadable
6926     "f" gnus-article-display-x-face
6927     "l" gnus-summary-stop-page-breaking
6928     "r" gnus-summary-caesar-message
6929     "t" gnus-article-hide-headers
6930     "v" gnus-summary-verbose-headers
6931     "m" gnus-summary-toggle-mime)
6932
6933   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
6934     "a" gnus-article-hide
6935     "h" gnus-article-hide-headers
6936     "b" gnus-article-hide-boring-headers
6937     "s" gnus-article-hide-signature
6938     "c" gnus-article-hide-citation
6939     "p" gnus-article-hide-pgp
6940     "\C-c" gnus-article-hide-citation-maybe)
6941
6942   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
6943     "a" gnus-article-highlight
6944     "h" gnus-article-highlight-headers
6945     "c" gnus-article-highlight-citation
6946     "s" gnus-article-highlight-signature)
6947
6948   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
6949     "z" gnus-article-date-ut
6950     "u" gnus-article-date-ut
6951     "l" gnus-article-date-local
6952     "e" gnus-article-date-lapsed
6953     "o" gnus-article-date-original)
6954
6955   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
6956     "v" gnus-version
6957     "f" gnus-summary-fetch-faq
6958     "d" gnus-summary-describe-group
6959     "h" gnus-summary-describe-briefly
6960     "i" gnus-info-find-node)
6961
6962   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
6963     "e" gnus-summary-expire-articles
6964     "\M-\C-e" gnus-summary-expire-articles-now
6965     "\177" gnus-summary-delete-article
6966     [delete] gnus-summary-delete-article
6967     "m" gnus-summary-move-article
6968     "r" gnus-summary-respool-article
6969     "w" gnus-summary-edit-article
6970     "c" gnus-summary-copy-article
6971     "B" gnus-summary-crosspost-article
6972     "q" gnus-summary-respool-query
6973     "i" gnus-summary-import-article)
6974
6975   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
6976     "o" gnus-summary-save-article
6977     "m" gnus-summary-save-article-mail
6978     "r" gnus-summary-save-article-rmail
6979     "f" gnus-summary-save-article-file
6980     "b" gnus-summary-save-article-body-file
6981     "h" gnus-summary-save-article-folder
6982     "v" gnus-summary-save-article-vm
6983     "p" gnus-summary-pipe-output
6984     "s" gnus-soup-add-article)
6985   )
6986
6987 \f
6988
6989 (defun gnus-summary-mode (&optional group)
6990   "Major mode for reading articles.
6991
6992 All normal editing commands are switched off.
6993 \\<gnus-summary-mode-map>
6994 Each line in this buffer represents one article.  To read an
6995 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
6996 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
6997 respectively.
6998
6999 You can also post articles and send mail from this buffer.  To
7000 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
7001 of an article, type `\\[gnus-summary-reply]'.
7002
7003 There are approx. one gazillion commands you can execute in this
7004 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
7005
7006 The following commands are available:
7007
7008 \\{gnus-summary-mode-map}"
7009   (interactive)
7010   (when (and menu-bar-mode
7011              (gnus-visual-p 'summary-menu 'menu))
7012     (gnus-summary-make-menu-bar))
7013   (kill-all-local-variables)
7014   (let ((locals gnus-summary-local-variables))
7015     (while locals
7016       (if (consp (car locals))
7017           (progn
7018             (make-local-variable (caar locals))
7019             (set (caar locals) (eval (cdar locals))))
7020         (make-local-variable (car locals))
7021         (set (car locals) nil))
7022       (setq locals (cdr locals))))
7023   (gnus-make-thread-indent-array)
7024   (gnus-simplify-mode-line)
7025   (setq major-mode 'gnus-summary-mode)
7026   (setq mode-name "Summary")
7027   (make-local-variable 'minor-mode-alist)
7028   (use-local-map gnus-summary-mode-map)
7029   (buffer-disable-undo (current-buffer))
7030   (setq buffer-read-only t)             ;Disable modification
7031   (setq truncate-lines t)
7032   (setq selective-display t)
7033   (setq selective-display-ellipses t)   ;Display `...'
7034   (setq buffer-display-table gnus-summary-display-table)
7035   (setq gnus-newsgroup-name group)
7036   (run-hooks 'gnus-summary-mode-hook))
7037
7038 (defun gnus-summary-make-display-table ()
7039   ;; Change the display table.  Odd characters have a tendency to mess
7040   ;; up nicely formatted displays - we make all possible glyphs
7041   ;; display only a single character.
7042
7043   ;; We start from the standard display table, if any.
7044   (setq gnus-summary-display-table
7045         (or (copy-sequence standard-display-table)
7046             (make-display-table)))
7047   ;; Nix out all the control chars...
7048   (let ((i 32))
7049     (while (>= (setq i (1- i)) 0)
7050       (aset gnus-summary-display-table i [??])))
7051   ;; ... but not newline and cr, of course. (cr is necessary for the
7052   ;; selective display).
7053   (aset gnus-summary-display-table ?\n nil)
7054   (aset gnus-summary-display-table ?\r nil)
7055   ;; We nix out any glyphs over 126 that are not set already.
7056   (let ((i 256))
7057     (while (>= (setq i (1- i)) 127)
7058       ;; Only modify if the entry is nil.
7059       (or (aref gnus-summary-display-table i)
7060           (aset gnus-summary-display-table i [??])))))
7061
7062 (defun gnus-summary-clear-local-variables ()
7063   (let ((locals gnus-summary-local-variables))
7064     (while locals
7065       (if (consp (car locals))
7066           (and (vectorp (caar locals))
7067                (set (caar locals) nil))
7068         (and (vectorp (car locals))
7069              (set (car locals) nil)))
7070       (setq locals (cdr locals)))))
7071
7072 ;; Summary data functions.
7073
7074 (defmacro gnus-data-number (data)
7075   `(car ,data))
7076
7077 (defmacro gnus-data-set-number (data number)
7078   `(setcar ,data ,number))
7079
7080 (defmacro gnus-data-mark (data)
7081   `(nth 1 ,data))
7082
7083 (defmacro gnus-data-set-mark (data mark)
7084   `(setcar (nthcdr 1 ,data) ,mark))
7085
7086 (defmacro gnus-data-pos (data)
7087   `(nth 2 ,data))
7088
7089 (defmacro gnus-data-set-pos (data pos)
7090   `(setcar (nthcdr 2 ,data) ,pos))
7091
7092 (defmacro gnus-data-header (data)
7093   `(nth 3 ,data))
7094
7095 (defmacro gnus-data-level (data)
7096   `(nth 4 ,data))
7097
7098 (defmacro gnus-data-unread-p (data)
7099   `(= (nth 1 ,data) gnus-unread-mark))
7100
7101 (defmacro gnus-data-pseudo-p (data)
7102   `(consp (nth 3 ,data)))
7103
7104 (defmacro gnus-data-find (number)
7105   `(assq ,number gnus-newsgroup-data))
7106
7107 (defmacro gnus-data-find-list (number &optional data)
7108   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7109      (memq (assq ,number bdata)
7110            bdata)))
7111
7112 (defmacro gnus-data-make (number mark pos header level)
7113   `(list ,number ,mark ,pos ,header ,level))
7114
7115 (defun gnus-data-enter (after-article number mark pos header level offset)
7116   (let ((data (gnus-data-find-list after-article)))
7117     (or data (error "No such article: %d" after-article))
7118     (setcdr data (cons (gnus-data-make number mark pos header level)
7119                        (cdr data)))
7120     (setq gnus-newsgroup-data-reverse nil)
7121     (gnus-data-update-list (cddr data) offset)))
7122
7123 (defun gnus-data-enter-list (after-article list &optional offset)
7124   (when list
7125     (let ((data (and after-article (gnus-data-find-list after-article)))
7126           (ilist list))
7127       (or data (not after-article) (error "No such article: %d" after-article))
7128       ;; Find the last element in the list to be spliced into the main
7129       ;; list.
7130       (while (cdr list)
7131         (setq list (cdr list)))
7132       (if (not data)
7133           (progn
7134             (setcdr list gnus-newsgroup-data)
7135             (setq gnus-newsgroup-data ilist)
7136             (and offset (gnus-data-update-list (cdr list) offset)))
7137         (setcdr list (cdr data))
7138         (setcdr data ilist)
7139         (and offset (gnus-data-update-list (cdr data) offset)))
7140       (setq gnus-newsgroup-data-reverse nil))))
7141
7142 (defun gnus-data-remove (article &optional offset)
7143   (let ((data gnus-newsgroup-data))
7144     (if (= (gnus-data-number (car data)) article)
7145         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7146               gnus-newsgroup-data-reverse nil)
7147       (while (cdr data)
7148         (and (= (gnus-data-number (cadr data)) article)
7149              (progn
7150                (setcdr data (cddr data))
7151                (and offset (gnus-data-update-list (cdr data) offset))
7152                (setq data nil
7153                      gnus-newsgroup-data-reverse nil)))
7154         (setq data (cdr data))))))
7155
7156 (defmacro gnus-data-list (backward)
7157   `(if ,backward
7158        (or gnus-newsgroup-data-reverse
7159            (setq gnus-newsgroup-data-reverse
7160                  (reverse gnus-newsgroup-data)))
7161      gnus-newsgroup-data))
7162
7163 (defun gnus-data-update-list (data offset)
7164   "Add OFFSET to the POS of all data entries in DATA."
7165   (while data
7166     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7167     (setq data (cdr data))))
7168
7169 (defun gnus-data-compute-positions ()
7170   "Compute the positions of all articles."
7171   (let ((data gnus-newsgroup-data)
7172         pos)
7173     (while data
7174       (when (setq pos (text-property-any
7175                        (point-min) (point-max)
7176                        'gnus-number (gnus-data-number (car data))))
7177         (gnus-data-set-pos (car data) (+ pos 3)))
7178       (setq data (cdr data)))))
7179
7180 (defun gnus-summary-article-pseudo-p (article)
7181   "Say whether this article is a pseudo article or not."
7182   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7183
7184 (defun gnus-article-parent-p (number)
7185   "Say whether this article is a parent or not."
7186   (let ((data (gnus-data-find-list number)))
7187     (and (cdr data)                     ; There has to be an article after...
7188          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7189             (gnus-data-level (nth 1 data))))))
7190
7191 (defun gnus-article-children (number)
7192   "Return a list of all children to NUMBER."
7193   (let* ((data (gnus-data-find-list number))
7194          (level (gnus-data-level (car data)))
7195          children)
7196     (setq data (cdr data))
7197     (while (and data            
7198                 (= (gnus-data-level (car data)) (1+ level)))
7199       (push (gnus-data-number (car data)) children)
7200       (setq data (cdr data)))
7201     children))
7202
7203 (defmacro gnus-summary-skip-intangible ()
7204   "If the current article is intangible, then jump to a different article."
7205   '(let ((to (get-text-property (point) 'gnus-intangible)))
7206     (and to (gnus-summary-goto-subject to))))
7207
7208 (defmacro gnus-summary-article-intangible-p ()
7209   "Say whether this article is intangible or not."
7210   '(get-text-property (point) 'gnus-intangible))
7211
7212 ;; Some summary mode macros.
7213
7214 (defmacro gnus-summary-article-number ()
7215   "The article number of the article on the current line.
7216 If there isn's an article number here, then we return the current
7217 article number."
7218   '(progn
7219      (gnus-summary-skip-intangible)
7220      (or (get-text-property (point) 'gnus-number)
7221          (gnus-summary-last-subject))))
7222
7223 (defmacro gnus-summary-article-header (&optional number)
7224   `(gnus-data-header (gnus-data-find
7225                       ,(or number '(gnus-summary-article-number)))))
7226
7227 (defmacro gnus-summary-thread-level (&optional number)
7228   `(if (and (eq gnus-summary-make-false-root 'dummy)
7229             (get-text-property (point) 'gnus-intangible))
7230        0
7231      (gnus-data-level (gnus-data-find
7232                        ,(or number '(gnus-summary-article-number))))))
7233
7234 (defmacro gnus-summary-article-mark (&optional number)
7235   `(gnus-data-mark (gnus-data-find
7236                     ,(or number '(gnus-summary-article-number)))))
7237
7238 (defmacro gnus-summary-article-pos (&optional number)
7239   `(gnus-data-pos (gnus-data-find
7240                    ,(or number '(gnus-summary-article-number)))))
7241
7242 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7243 (defmacro gnus-summary-article-subject (&optional number)
7244   "Return current subject string or nil if nothing."
7245   `(let ((headers
7246           ,(if number
7247                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7248              '(gnus-data-header (assq (gnus-summary-article-number)
7249                                       gnus-newsgroup-data)))))
7250      (and headers
7251           (vectorp headers)
7252           (mail-header-subject headers))))
7253
7254 (defmacro gnus-summary-article-score (&optional number)
7255   "Return current article score."
7256   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7257                   gnus-newsgroup-scored))
7258        gnus-summary-default-score 0))
7259
7260 (defun gnus-summary-article-children (&optional number)
7261   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7262          (level (gnus-data-level (car data)))
7263          l children)
7264     (while (and (setq data (cdr data))
7265                 (> (setq l (gnus-data-level (car data))) level))
7266       (and (= (1+ level) l)
7267            (setq children (cons (gnus-data-number (car data))
7268                                 children))))
7269     (nreverse children)))
7270
7271 (defun gnus-summary-article-parent (&optional number)
7272   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7273                                     (gnus-data-list t)))
7274          (level (gnus-data-level (car data))))
7275     (if (zerop level)
7276         () ; This is a root.
7277       ;; We search until we find an article with a level less than
7278       ;; this one.  That function has to be the parent.
7279       (while (and (setq data (cdr data))
7280                   (not (< (gnus-data-level (car data)) level))))
7281       (and data (gnus-data-number (car data))))))
7282
7283 (defun gnus-unread-mark-p (mark)
7284   "Say whether MARK is the unread mark."
7285   (= mark gnus-unread-mark))
7286
7287 (defun gnus-read-mark-p (mark)
7288   "Say whether MARK is one of the marks that mark as read.
7289 This is all marks except unread, ticked, dormant, and expirable."
7290   (not (or (= mark gnus-unread-mark)
7291            (= mark gnus-ticked-mark)
7292            (= mark gnus-dormant-mark)
7293            (= mark gnus-expirable-mark))))
7294
7295 ;; Various summary mode internalish functions.
7296
7297 (defun gnus-mouse-pick-article (e)
7298   (interactive "e")
7299   (mouse-set-point e)
7300   (gnus-summary-next-page nil t))
7301
7302 (defun gnus-summary-setup-buffer (group)
7303   "Initialize summary buffer."
7304   (let ((buffer (concat "*Summary " group "*")))
7305     (if (get-buffer buffer)
7306         (progn
7307           (set-buffer buffer)
7308           (setq gnus-summary-buffer (current-buffer))
7309           (not gnus-newsgroup-prepared))
7310       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7311       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7312       (gnus-add-current-to-buffer-list)
7313       (gnus-summary-mode group)
7314       (when gnus-carpal
7315         (gnus-carpal-setup-buffer 'summary))
7316       (unless gnus-single-article-buffer
7317         (make-local-variable 'gnus-article-buffer)
7318         (make-local-variable 'gnus-article-current)
7319         (make-local-variable 'gnus-original-article-buffer))
7320       (setq gnus-newsgroup-name group)
7321       t)))
7322
7323 (defun gnus-set-global-variables ()
7324   ;; Set the global equivalents of the summary buffer-local variables
7325   ;; to the latest values they had.  These reflect the summary buffer
7326   ;; that was in action when the last article was fetched.
7327   (when (eq major-mode 'gnus-summary-mode)
7328     (setq gnus-summary-buffer (current-buffer))
7329     (let ((name gnus-newsgroup-name)
7330           (marked gnus-newsgroup-marked)
7331           (unread gnus-newsgroup-unreads)
7332           (headers gnus-current-headers)
7333           (data gnus-newsgroup-data)
7334           (summary gnus-summary-buffer)
7335           (article-buffer gnus-article-buffer)
7336           (original gnus-original-article-buffer)
7337           (gac gnus-article-current)
7338           (score-file gnus-current-score-file))
7339       (save-excursion
7340         (set-buffer gnus-group-buffer)
7341         (setq gnus-newsgroup-name name)
7342         (setq gnus-newsgroup-marked marked)
7343         (setq gnus-newsgroup-unreads unread)
7344         (setq gnus-current-headers headers)
7345         (setq gnus-newsgroup-data data)
7346         (setq gnus-article-current gac)
7347         (setq gnus-summary-buffer summary)
7348         (setq gnus-article-buffer article-buffer)
7349         (setq gnus-original-article-buffer original)
7350         (setq gnus-current-score-file score-file)))))
7351
7352 (defun gnus-summary-last-article-p (&optional article)
7353   "Return whether ARTICLE is the last article in the buffer."
7354   (if (not (setq article (or article (gnus-summary-article-number))))
7355       t ; All non-existant numbers are the last article. :-)
7356     (not (cdr (gnus-data-find-list article)))))
7357
7358 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7359   "Insert a dummy root in the summary buffer."
7360   (beginning-of-line)
7361   (add-text-properties
7362    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7363    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7364
7365 (defvar gnus-thread-indent-array nil)
7366 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
7367 (defun gnus-make-thread-indent-array ()
7368   (let ((n 200))
7369     (unless (and gnus-thread-indent-array
7370                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
7371       (setq gnus-thread-indent-array (make-vector 201 "")
7372             gnus-thread-indent-array-level gnus-thread-indent-level)
7373       (while (>= n 0)
7374         (aset gnus-thread-indent-array n
7375               (make-string (* n gnus-thread-indent-level) ? ))
7376         (setq n (1- n))))))
7377
7378 (defun gnus-summary-insert-line
7379   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7380                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7381                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7382   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7383          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7384          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7385          (gnus-tmp-score-char
7386           (if (or (null gnus-summary-default-score)
7387                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7388                       gnus-summary-zcore-fuzz)) ? 
7389             (if (< gnus-tmp-score gnus-summary-default-score)
7390                 gnus-score-below-mark gnus-score-over-mark)))
7391          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7392                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7393                                   gnus-cached-mark)
7394                                  (gnus-tmp-replied gnus-replied-mark)
7395                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7396                                   gnus-saved-mark)
7397                                  (t gnus-unread-mark)))
7398          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7399          (gnus-tmp-name
7400           (cond
7401            ((string-match "(.+)" gnus-tmp-from)
7402             (substring gnus-tmp-from
7403                        (1+ (match-beginning 0)) (1- (match-end 0))))
7404            ((string-match "<[^>]+> *$" gnus-tmp-from)
7405             (let ((beg (match-beginning 0)))
7406               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7407                        (substring gnus-tmp-from (1+ (match-beginning 0))
7408                                   (1- (match-end 0))))
7409                   (substring gnus-tmp-from 0 beg))))
7410            (t gnus-tmp-from)))
7411          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7412          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7413          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7414          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7415          (buffer-read-only nil))
7416     (when (string= gnus-tmp-name "")
7417       (setq gnus-tmp-name gnus-tmp-from))
7418     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7419     (put-text-property
7420      (point)
7421      (progn (eval gnus-summary-line-format-spec) (point))
7422      'gnus-number gnus-tmp-number)
7423     (when (gnus-visual-p 'summary-highlight 'highlight)
7424       (forward-line -1)
7425       (run-hooks 'gnus-summary-update-hook)
7426       (forward-line 1))))
7427
7428 (defun gnus-summary-update-line (&optional dont-update)
7429   ;; Update summary line after change.
7430   (when (and gnus-summary-default-score
7431              (not gnus-summary-inhibit-highlight))
7432     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7433            (article (gnus-summary-article-number))
7434            (score (gnus-summary-article-score article)))
7435       (unless dont-update
7436         (if (and gnus-summary-mark-below
7437                  (< (gnus-summary-article-score)
7438                     gnus-summary-mark-below))
7439             ;; This article has a low score, so we mark it as read.
7440             (when (memq article gnus-newsgroup-unreads)
7441               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7442           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7443             ;; This article was previously marked as read on account
7444             ;; of a low score, but now it has risen, so we mark it as
7445             ;; unread.
7446             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7447         (gnus-summary-update-mark
7448          (if (or (null gnus-summary-default-score)
7449                  (<= (abs (- score gnus-summary-default-score))
7450                      gnus-summary-zcore-fuzz)) ? 
7451            (if (< score gnus-summary-default-score)
7452                gnus-score-below-mark gnus-score-over-mark)) 'score))
7453       ;; Do visual highlighting.
7454       (when (gnus-visual-p 'summary-highlight 'highlight)
7455         (run-hooks 'gnus-summary-update-hook)))))
7456
7457 (defvar gnus-tmp-new-adopts nil)
7458
7459 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7460   ;; Sum up all elements (and sub-elements) in a list.
7461   (let* ((number
7462           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7463           (cond
7464            ((and (consp thread) (cdr thread))
7465             (apply
7466              '+ 1 (mapcar
7467                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7468            ((null thread)
7469             1)
7470            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7471             1)
7472            (t 1))))
7473     (when (and level (zerop level) gnus-tmp-new-adopts)
7474       (incf number
7475             (apply '+ (mapcar
7476                        'gnus-summary-number-of-articles-in-thread
7477                        gnus-tmp-new-adopts))))
7478     (if char
7479         (if (> number 1) gnus-not-empty-thread-mark
7480           gnus-empty-thread-mark)
7481       number)))
7482
7483 (defun gnus-summary-set-local-parameters (group)
7484  "Go through the local params of GROUP and set all variable specs in that list."
7485   (let ((params (gnus-info-params (gnus-get-info group)))
7486         elem)
7487     (while params
7488       (setq elem (car params)
7489             params (cdr params))
7490       (and (consp elem)                 ; Has to be a cons.
7491            (consp (cdr elem))           ; The cdr has to be a list.
7492            (symbolp (car elem))         ; Has to be a symbol in there.
7493            (not (memq (car elem) 
7494                       '(quit-config to-address to-list to-group)))
7495            (progn                       ; So we set it.
7496              (make-local-variable (car elem))
7497              (set (car elem) (eval (nth 1 elem))))))))
7498
7499 (defun gnus-summary-read-group (group &optional show-all no-article
7500                                       kill-buffer no-display)
7501   "Start reading news in newsgroup GROUP.
7502 If SHOW-ALL is non-nil, already read articles are also listed.
7503 If NO-ARTICLE is non-nil, no article is selected initially.
7504 If NO-DISPLAY, don't generate a summary buffer."
7505   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7506   (let* ((new-group (gnus-summary-setup-buffer group))
7507          (quit-config (gnus-group-quit-config group))
7508          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7509     (cond
7510      ;; This summary buffer exists already, so we just select it.
7511      ((not new-group)
7512       (gnus-set-global-variables)
7513       (when kill-buffer
7514         (gnus-kill-or-deaden-summary kill-buffer))
7515       (gnus-configure-windows 'summary 'force)
7516       (gnus-set-mode-line 'summary)
7517       (gnus-summary-position-point)
7518       (message "")
7519       t)
7520      ;; We couldn't select this group.
7521      ((null did-select)
7522       (when (and (eq major-mode 'gnus-summary-mode)
7523                  (not (equal (current-buffer) kill-buffer)))
7524         (kill-buffer (current-buffer))
7525         (if (not quit-config)
7526             (progn
7527               (set-buffer gnus-group-buffer)
7528               (gnus-group-jump-to-group group)
7529               (gnus-group-next-unread-group 1))
7530           (if (not (buffer-name (car quit-config)))
7531               (gnus-configure-windows 'group 'force)
7532             (set-buffer (car quit-config))
7533             (and (eq major-mode 'gnus-summary-mode)
7534                  (gnus-set-global-variables))
7535             (gnus-configure-windows (cdr quit-config)))))
7536       (gnus-message 3 "Can't select group")
7537       nil)
7538      ;; The user did a `C-g' while prompting for number of articles,
7539      ;; so we exit this group.
7540      ((eq did-select 'quit)
7541       (and (eq major-mode 'gnus-summary-mode)
7542            (not (equal (current-buffer) kill-buffer))
7543            (kill-buffer (current-buffer)))
7544       (when kill-buffer
7545         (gnus-kill-or-deaden-summary kill-buffer))
7546       (if (not quit-config)
7547           (progn
7548             (set-buffer gnus-group-buffer)
7549             (gnus-group-jump-to-group group)
7550             (gnus-group-next-unread-group 1)
7551             (gnus-configure-windows 'group 'force))
7552         (if (not (buffer-name (car quit-config)))
7553             (gnus-configure-windows 'group 'force)
7554           (set-buffer (car quit-config))
7555           (and (eq major-mode 'gnus-summary-mode)
7556                (gnus-set-global-variables))
7557           (gnus-configure-windows (cdr quit-config))))
7558       ;; Finally signal the quit.
7559       (signal 'quit nil))
7560      ;; The group was successfully selected.
7561      (t
7562       (gnus-set-global-variables)
7563       ;; Save the active value in effect when the group was entered.
7564       (setq gnus-newsgroup-active
7565             (gnus-copy-sequence
7566              (gnus-active gnus-newsgroup-name)))
7567       ;; You can change the summary buffer in some way with this hook.
7568       (run-hooks 'gnus-select-group-hook)
7569       ;; Set any local variables in the group parameters.
7570       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7571       (gnus-update-format-specifications)
7572       ;; Do score processing.
7573       (when gnus-use-scoring
7574         (gnus-possibly-score-headers))
7575       ;; Check whether to fill in the gaps in the threads.
7576       (when gnus-build-sparse-threads
7577         (gnus-build-sparse-threads))
7578       ;; Find the initial limit.
7579       (if show-all
7580           (let ((gnus-newsgroup-dormant nil))
7581             (gnus-summary-initial-limit show-all))
7582         (gnus-summary-initial-limit show-all))
7583       ;; Generate the summary buffer.
7584       (unless no-display
7585         (gnus-summary-prepare))
7586       (when gnus-use-trees
7587         (gnus-tree-open group)
7588         (setq gnus-summary-highlight-line-function
7589               'gnus-tree-highlight-article))
7590       ;; If the summary buffer is empty, but there are some low-scored
7591       ;; articles or some excluded dormants, we include these in the
7592       ;; buffer.
7593       (when (and (zerop (buffer-size))
7594                  (not no-display))
7595         (cond (gnus-newsgroup-dormant
7596                (gnus-summary-limit-include-dormant))
7597               ((and gnus-newsgroup-scored show-all)
7598                (gnus-summary-limit-include-expunged))))
7599       ;; Function `gnus-apply-kill-file' must be called in this hook.
7600       (run-hooks 'gnus-apply-kill-hook)
7601       (if (and (zerop (buffer-size))
7602                (not no-display))
7603           (progn
7604             ;; This newsgroup is empty.
7605             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7606             (gnus-message 6 "No unread news")
7607             (when kill-buffer
7608               (gnus-kill-or-deaden-summary kill-buffer))
7609             ;; Return nil from this function.
7610             nil)
7611         ;; Hide conversation thread subtrees.  We cannot do this in
7612         ;; gnus-summary-prepare-hook since kill processing may not
7613         ;; work with hidden articles.
7614         (and gnus-show-threads
7615              gnus-thread-hide-subtree
7616              (gnus-summary-hide-all-threads))
7617         ;; Show first unread article if requested.
7618         (if (and (not no-article)
7619                  (not no-display)
7620                  gnus-newsgroup-unreads
7621                  gnus-auto-select-first)
7622             (unless (if (eq gnus-auto-select-first 'best)
7623                         (gnus-summary-best-unread-article)
7624                       (gnus-summary-first-unread-article))
7625               (gnus-configure-windows 'summary))
7626           ;; Don't select any articles, just move point to the first
7627           ;; article in the group.
7628           (goto-char (point-min))
7629           (gnus-summary-position-point)
7630           (gnus-set-mode-line 'summary)
7631           (gnus-configure-windows 'summary 'force))
7632         ;; If we are in async mode, we send some info to the backend.
7633         (when gnus-newsgroup-async
7634           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7635         (when kill-buffer
7636           (gnus-kill-or-deaden-summary kill-buffer))
7637         (when (get-buffer-window gnus-group-buffer t)
7638           ;; Gotta use windows, because recenter does wierd stuff if
7639           ;; the current buffer ain't the displayed window.
7640           (let ((owin (selected-window)))
7641             (select-window (get-buffer-window gnus-group-buffer t))
7642             (when (gnus-group-goto-group group)
7643               (recenter))
7644             (select-window owin))))
7645       ;; Mark this buffer as "prepared".
7646       (setq gnus-newsgroup-prepared t)
7647       t))))
7648
7649 (defun gnus-summary-prepare ()
7650   "Generate the summary buffer."
7651   (let ((buffer-read-only nil))
7652     (erase-buffer)
7653     (setq gnus-newsgroup-data nil
7654           gnus-newsgroup-data-reverse nil)
7655     (run-hooks 'gnus-summary-generate-hook)
7656     ;; Generate the buffer, either with threads or without.
7657     (when gnus-newsgroup-headers
7658       (gnus-summary-prepare-threads
7659        (if gnus-show-threads
7660            (gnus-sort-gathered-threads
7661             (funcall gnus-summary-thread-gathering-function
7662                      (gnus-sort-threads
7663                       (gnus-cut-threads (gnus-make-threads)))))
7664          ;; Unthreaded display.
7665          (gnus-sort-articles gnus-newsgroup-headers))))
7666     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7667     ;; Call hooks for modifying summary buffer.
7668     (goto-char (point-min))
7669     (run-hooks 'gnus-summary-prepare-hook)))
7670
7671 (defun gnus-gather-threads-by-subject (threads)
7672   "Gather threads by looking at Subject headers."
7673   (if (not gnus-summary-make-false-root)
7674       threads
7675     (let ((hashtb (gnus-make-hashtable 1023))
7676           (prev threads)
7677           (result threads)
7678           subject hthread whole-subject)
7679       (while threads
7680         (setq whole-subject (mail-header-subject (caar threads)))
7681         (setq subject
7682               (cond
7683                ;; Truncate the subject.
7684                ((numberp gnus-summary-gather-subject-limit)
7685                 (setq subject (gnus-simplify-subject-re whole-subject))
7686                 (if (> (length subject) gnus-summary-gather-subject-limit)
7687                     (substring subject 0 gnus-summary-gather-subject-limit)
7688                   subject))
7689                ;; Fuzzily simplify it.
7690                ((eq 'fuzzy gnus-summary-gather-subject-limit)
7691                 (gnus-simplify-subject-fuzzy whole-subject))
7692                ;; Just remove the leading "Re:".
7693                (t
7694                 (gnus-simplify-subject-re whole-subject))))
7695
7696         (if (and gnus-summary-gather-exclude-subject
7697                  (string-match gnus-summary-gather-exclude-subject
7698                                subject))
7699             ()          ; We don't want to do anything with this article.
7700           ;; We simplify the subject before looking it up in the
7701           ;; hash table.
7702
7703           (if (setq hthread (gnus-gethash subject hashtb))
7704               (progn
7705                 ;; We enter a dummy root into the thread, if we
7706                 ;; haven't done that already.
7707                 (unless (stringp (caar hthread))
7708                   (setcar hthread (list whole-subject (car hthread))))
7709                 ;; We add this new gathered thread to this gathered
7710                 ;; thread.
7711                 (setcdr (car hthread)
7712                         (nconc (cdar hthread) (list (car threads))))
7713                 ;; Remove it from the list of threads.
7714                 (setcdr prev (cdr threads))
7715                 (setq threads prev))
7716             ;; Enter this thread into the hash table.
7717             (gnus-sethash subject threads hashtb)))
7718         (setq prev threads)
7719         (setq threads (cdr threads)))
7720       result)))
7721
7722 (defun gnus-gather-threads-by-references (threads)
7723   "Gather threads by looking at References headers."
7724   (let ((idhashtb (gnus-make-hashtable 1023))
7725         (thhashtb (gnus-make-hashtable 1023))
7726         (prev threads)
7727         (result threads)
7728         ids references id gthread gid entered)
7729     (while threads
7730       (when (setq references (mail-header-references (caar threads)))
7731         (setq id (mail-header-id (caar threads)))
7732         (setq ids (gnus-split-references references))
7733         (setq entered nil)
7734         (while ids
7735           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7736               (progn
7737                 (gnus-sethash (car ids) id idhashtb)
7738                 (gnus-sethash id threads thhashtb))
7739             (setq gthread (gnus-gethash gid thhashtb))
7740             (unless entered
7741               ;; We enter a dummy root into the thread, if we
7742               ;; haven't done that already.
7743               (unless (stringp (caar gthread))
7744                 (setcar gthread (list (mail-header-subject (caar gthread))
7745                                       (car gthread))))
7746               ;; We add this new gathered thread to this gathered
7747               ;; thread.
7748               (setcdr (car gthread)
7749                       (nconc (cdar gthread) (list (car threads)))))
7750             ;; Add it into the thread hash table.
7751             (gnus-sethash id gthread thhashtb)
7752             (setq entered t)
7753             ;; Remove it from the list of threads.
7754             (setcdr prev (cdr threads))
7755             (setq threads prev))
7756           (setq ids (cdr ids))))
7757       (setq prev threads)
7758       (setq threads (cdr threads)))
7759     result))
7760
7761 (defun gnus-sort-gathered-threads (threads)
7762   "Sort subtreads inside each gathered thread by article number."
7763   (let ((result threads))
7764     (while threads
7765       (when (stringp (caar threads))
7766         (setcdr (car threads)
7767                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
7768       (setq threads (cdr threads)))
7769     result))
7770
7771 (defun gnus-make-threads ()
7772   "Go through the dependency hashtb and find the roots.  Return all threads."
7773   (let (threads)
7774     (mapatoms
7775      (lambda (refs)
7776        (unless (car (symbol-value refs))
7777          ;; These threads do not refer back to any other articles,
7778          ;; so they're roots.
7779          (setq threads (append (cdr (symbol-value refs)) threads))))
7780      gnus-newsgroup-dependencies)
7781     threads))
7782
7783 (defun gnus-build-sparse-threads ()
7784   (let ((headers gnus-newsgroup-headers)
7785         (deps gnus-newsgroup-dependencies)
7786         header references generation relations 
7787         cthread subject child end pthread relation)
7788     ;; First we create an alist of generations/relations, where 
7789     ;; generations is how much we trust the ralation, and the relation
7790     ;; is parent/child.
7791     (gnus-message 7 "Making sparse threads...")
7792     (save-excursion
7793       (nnheader-set-temp-buffer " *gnus sparse threads*")
7794       (while (setq header (pop headers))
7795         (when (and (setq references (mail-header-references header))
7796                    (not (string= references "")))
7797           (insert references)
7798           (setq child (mail-header-id header)
7799                 subject (mail-header-subject header))
7800           (setq generation 0)
7801           (while (search-backward ">" nil t)
7802             (setq end (1+ (point)))
7803             (when (search-backward "<" nil t)
7804               (push (list (incf generation) 
7805                           child (setq child (buffer-substring (point) end))
7806                           subject)
7807                     relations)))
7808           (push (list (1+ generation) child nil subject) relations)
7809           (erase-buffer)))
7810       (kill-buffer (current-buffer)))
7811     ;; Sort over trustworthiness.
7812     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
7813     (while (setq relation (pop relations))
7814       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
7815                 (unless (car (symbol-value cthread))
7816                   ;; Make this article the parent of these threads.
7817                   (setcar (symbol-value cthread)
7818                           (vector gnus-reffed-article-number 
7819                                   (cadddr relation) 
7820                                   "" ""
7821                                   (cadr relation) 
7822                                   (or (caddr relation) "") 0 0 "")))
7823               (set cthread (list (vector gnus-reffed-article-number
7824                                          (cadddr relation) 
7825                                          "" "" (cadr relation) 
7826                                          (or (caddr relation) "") 0 0 ""))))
7827         (push gnus-reffed-article-number gnus-newsgroup-limit)
7828         (push gnus-reffed-article-number gnus-newsgroup-sparse)
7829         (push (cons gnus-reffed-article-number gnus-sparse-mark)
7830               gnus-newsgroup-reads)
7831         (decf gnus-reffed-article-number)
7832         ;; Make this new thread the child of its parent.
7833         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
7834             (setcdr (symbol-value pthread)
7835                     (nconc (cdr (symbol-value pthread))
7836                            (list (symbol-value cthread))))
7837           (set pthread (list nil (symbol-value cthread))))))
7838     (gnus-message 7 "Making sparse threads...done")))
7839
7840 (defun gnus-build-old-threads ()
7841   ;; Look at all the articles that refer back to old articles, and
7842   ;; fetch the headers for the articles that aren't there.  This will
7843   ;; build complete threads - if the roots haven't been expired by the
7844   ;; server, that is.
7845   (let (id heads)
7846     (mapatoms
7847      (lambda (refs)
7848        (when (not (car (symbol-value refs)))
7849          (setq heads (cdr (symbol-value refs)))
7850          (while heads
7851            (if (memq (mail-header-number (caar heads))
7852                      gnus-newsgroup-dormant)
7853                (setq heads (cdr heads))
7854              (setq id (symbol-name refs))
7855              (while (and (setq id (gnus-build-get-header id))
7856                          (not (car (gnus-gethash
7857                                     id gnus-newsgroup-dependencies)))))
7858              (setq heads nil)))))
7859      gnus-newsgroup-dependencies)))
7860
7861 (defun gnus-build-get-header (id)
7862   ;; Look through the buffer of NOV lines and find the header to
7863   ;; ID.  Enter this line into the dependencies hash table, and return
7864   ;; the id of the parent article (if any).
7865   (let ((deps gnus-newsgroup-dependencies)
7866         found header)
7867     (prog1
7868         (save-excursion
7869           (set-buffer nntp-server-buffer)
7870           (goto-char (point-min))
7871           (while (and (not found) (search-forward id nil t))
7872             (beginning-of-line)
7873             (setq found (looking-at
7874                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
7875                                  (regexp-quote id))))
7876             (or found (beginning-of-line 2)))
7877           (when found
7878             (beginning-of-line)
7879             (and
7880              (setq header (gnus-nov-parse-line
7881                            (read (current-buffer)) deps))
7882              (gnus-parent-id (mail-header-references header)))))
7883       (when header
7884         (let ((number (mail-header-number header)))
7885           (push number gnus-newsgroup-limit)
7886           (push header gnus-newsgroup-headers)
7887           (if (memq number gnus-newsgroup-unselected)
7888               (progn
7889                 (push number gnus-newsgroup-unreads)
7890                 (setq gnus-newsgroup-unselected
7891                       (delq number gnus-newsgroup-unselected)))
7892             (push number gnus-newsgroup-ancient)))))))
7893
7894 (defun gnus-summary-update-article (article &optional header)
7895   "Update ARTICLE in the summary buffer."
7896   (set-buffer gnus-summary-buffer)
7897   (let* ((header (or header (gnus-summary-article-header article)))
7898          (id (mail-header-id header))
7899          (data (gnus-data-find article))
7900          (thread (gnus-id-to-thread id))
7901          (parent
7902           (gnus-id-to-thread (or (gnus-parent-id 
7903                                   (mail-header-references header))
7904                                  "tull")))
7905          (buffer-read-only nil)
7906          (old (car thread))
7907          (number (mail-header-number header))
7908          pos)
7909     (when thread
7910       (setcar thread nil)
7911       (when parent
7912         (delq thread parent))
7913       (if (gnus-summary-insert-subject id header)
7914           ;; Set the (possibly) new article number in the data structure.
7915           (gnus-data-set-number data (gnus-id-to-article id))
7916         (setcar thread old)
7917         nil))))
7918
7919 (defun gnus-rebuild-thread (id)
7920   "Rebuild the thread containing ID."
7921   (let ((buffer-read-only nil)
7922         current thread data)
7923     (if (not gnus-show-threads)
7924         (setq thread (list (car (gnus-id-to-thread id))))
7925       ;; Get the thread this article is part of.
7926       (setq thread (gnus-remove-thread id)))
7927     (setq current (save-excursion
7928                     (and (zerop (forward-line -1))
7929                          (gnus-summary-article-number))))
7930     ;; If this is a gathered thread, we have to go some re-gathering.
7931     (when (stringp (car thread))
7932       (let ((subject (car thread))
7933             roots thr)
7934         (setq thread (cdr thread))
7935         (while thread
7936           (unless (memq (setq thr (gnus-id-to-thread
7937                                       (gnus-root-id
7938                                        (mail-header-id (caar thread)))))
7939                         roots)
7940             (push thr roots))
7941           (setq thread (cdr thread)))
7942         ;; We now have all (unique) roots.
7943         (if (= (length roots) 1)
7944             ;; All the loose roots are now one solid root.
7945             (setq thread (car roots))
7946           (setq thread (cons subject (gnus-sort-threads roots))))))
7947     (let (threads)
7948       ;; We then insert this thread into the summary buffer.
7949       (let (gnus-newsgroup-data gnus-newsgroup-threads)
7950         (gnus-summary-prepare-threads (list thread))
7951         (setq data (nreverse gnus-newsgroup-data))
7952         (setq threads gnus-newsgroup-threads))
7953       ;; We splice the new data into the data structure.
7954       (gnus-data-enter-list current data)
7955       (gnus-data-compute-positions)
7956       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
7957
7958 (defun gnus-id-to-thread (id)
7959   "Return the (sub-)thread where ID appears."
7960   (gnus-gethash id gnus-newsgroup-dependencies))
7961
7962 (defun gnus-id-to-article (id)
7963   "Return the article number of ID."
7964   (let ((thread (gnus-id-to-thread id)))
7965     (when thread
7966       (mail-header-number (car thread)))))
7967
7968 (defun gnus-id-to-header (id)
7969   "Return the article headers of ID."
7970   (car (gnus-id-to-thread id)))
7971
7972 (defun gnus-article-displayed-root-p (article)
7973   "Say whether ARTICLE is a root(ish) article."
7974   (let ((level (gnus-summary-thread-level article))
7975         particle)
7976     (cond 
7977      ((null level) nil)
7978      ((zerop level) t)
7979      ((and (= 1 level)
7980            (null (setq particle (gnus-id-to-article
7981                                  (gnus-parent-id 
7982                                   (mail-header-references 
7983                                    (gnus-summary-article-header article))))))
7984            (null (gnus-summary-thread-level particle)))))))
7985
7986 (defun gnus-root-id (id)
7987   "Return the id of the root of the thread where ID appears."
7988   (let (last-id prev)
7989     (while (and id (setq prev (car (gnus-gethash 
7990                                     id gnus-newsgroup-dependencies))))
7991       (setq last-id id
7992             id (gnus-parent-id (mail-header-references prev))))
7993     last-id))
7994
7995 (defun gnus-remove-thread (id &optional dont-remove)
7996   "Remove the thread that has ID in it."
7997   (let ((dep gnus-newsgroup-dependencies)
7998         headers thread last-id)
7999     ;; First go up in this thread until we find the root.
8000     (setq last-id (gnus-root-id id))
8001     (setq headers (list (car (gnus-id-to-thread last-id))
8002                         (caadr (gnus-id-to-thread last-id))))
8003     ;; We have now found the real root of this thread.  It might have
8004     ;; been gathered into some loose thread, so we have to search
8005     ;; through the threads to find the thread we wanted.
8006     (let ((threads gnus-newsgroup-threads)
8007           sub)
8008       (while threads
8009         (setq sub (car threads))
8010         (if (stringp (car sub))
8011             ;; This is a gathered threads, so we look at the roots
8012             ;; below it to find whether this article in in this
8013             ;; gathered root.
8014             (progn
8015               (setq sub (cdr sub))
8016               (while sub
8017                 (when (member (caar sub) headers)
8018                   (setq thread (car threads)
8019                         threads nil
8020                         sub nil))
8021                 (setq sub (cdr sub))))
8022           ;; It's an ordinary thread, so we check it.
8023           (when (eq (car sub) (car headers))
8024             (setq thread sub
8025                   threads nil)))
8026         (setq threads (cdr threads)))
8027       ;; If this article is in no thread, then it's a root.
8028       (if thread
8029           (unless dont-remove
8030             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
8031         (setq thread (gnus-gethash last-id dep)))
8032       (when thread
8033         (prog1
8034             thread ; We return this thread.
8035           (unless dont-remove
8036             (if (stringp (car thread))
8037                 (progn
8038                   ;; If we use dummy roots, then we have to remove the
8039                   ;; dummy root as well.
8040                   (when (eq gnus-summary-make-false-root 'dummy)
8041                     ;; Uhm.
8042                     )
8043                   (setq thread (cdr thread))
8044                   (while thread
8045                     (gnus-remove-thread-1 (car thread))
8046                     (setq thread (cdr thread))))
8047               (gnus-remove-thread-1 thread))))))))
8048
8049 (defun gnus-remove-thread-1 (thread)
8050   "Remove the thread THREAD recursively."
8051   (let ((number (mail-header-number (car thread)))
8052         pos)
8053     (when (setq pos (text-property-any
8054                      (point-min) (point-max) 'gnus-number number))
8055       (goto-char pos)
8056       (gnus-delete-line)
8057       (gnus-data-remove number))
8058     (setq thread (cdr thread))
8059     (while thread
8060       (gnus-remove-thread-1 (pop thread)))))
8061
8062 (defun gnus-sort-threads (threads)
8063   "Sort THREADS."
8064   (if (not gnus-thread-sort-functions)
8065       threads
8066     (let ((func (if (= 1 (length gnus-thread-sort-functions))
8067                     (car gnus-thread-sort-functions)
8068                   `(lambda (t1 t2)
8069                      ,(gnus-make-sort-function 
8070                        (reverse gnus-thread-sort-functions))))))
8071       (gnus-message 7 "Sorting threads...")
8072       (prog1
8073           (sort threads func)
8074         (gnus-message 7 "Sorting threads...done")))))
8075
8076 (defun gnus-sort-articles (articles)
8077   "Sort ARTICLES."
8078   (when gnus-article-sort-functions
8079     (let ((func (if (= 1 (length gnus-article-sort-functions))
8080                     (car gnus-article-sort-functions)
8081                   `(lambda (t1 t2)
8082                      ,(gnus-make-sort-function 
8083                        (reverse gnus-article-sort-functions))))))
8084       (gnus-message 7 "Sorting articles...")
8085       (prog1
8086           (setq gnus-newsgroup-headers (sort articles func))
8087         (gnus-message 7 "Sorting articles...done")))))
8088
8089 (defun gnus-make-sort-function (funs)
8090   "Return a composite sort condition based on the functions in FUNC."
8091   (if (cdr funs)
8092       `(or (,(car funs) t1 t2)
8093            (and (not (,(car funs) t2 t1))
8094                 ,(gnus-make-sort-function (cdr funs))))
8095     `(,(car funs) t1 t2)))
8096                  
8097 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
8098 (defmacro gnus-thread-header (thread)
8099   ;; Return header of first article in THREAD.
8100   ;; Note that THREAD must never, ever be anything else than a variable -
8101   ;; using some other form will lead to serious barfage.
8102   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8103   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8104   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8105         (vector thread) 2))
8106
8107 (defsubst gnus-article-sort-by-number (h1 h2)
8108   "Sort articles by article number."
8109   (< (mail-header-number h1)
8110      (mail-header-number h2)))
8111
8112 (defun gnus-thread-sort-by-number (h1 h2)
8113   "Sort threads by root article number."
8114   (gnus-article-sort-by-number
8115    (gnus-thread-header h1) (gnus-thread-header h2)))
8116
8117 (defsubst gnus-article-sort-by-author (h1 h2)
8118   "Sort articles by root author."
8119   (string-lessp
8120    (let ((extract (funcall
8121                    gnus-extract-address-components
8122                    (mail-header-from h1))))
8123      (or (car extract) (cdr extract)))
8124    (let ((extract (funcall
8125                    gnus-extract-address-components
8126                    (mail-header-from h2))))
8127      (or (car extract) (cdr extract)))))
8128
8129 (defun gnus-thread-sort-by-author (h1 h2)
8130   "Sort threads by root author."
8131   (gnus-article-sort-by-author
8132    (gnus-thread-header h1)  (gnus-thread-header h2)))
8133
8134 (defsubst gnus-article-sort-by-subject (h1 h2)
8135   "Sort articles by root subject."
8136   (string-lessp
8137    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8138    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8139
8140 (defun gnus-thread-sort-by-subject (h1 h2)
8141   "Sort threads by root subject."
8142   (gnus-article-sort-by-subject
8143    (gnus-thread-header h1) (gnus-thread-header h2)))
8144
8145 (defsubst gnus-article-sort-by-date (h1 h2)
8146   "Sort articles by root article date."
8147   (string-lessp
8148    (gnus-sortable-date (mail-header-date h1))
8149    (gnus-sortable-date (mail-header-date h2))))
8150
8151 (defun gnus-thread-sort-by-date (h1 h2)
8152   "Sort threads by root article date."
8153   (gnus-article-sort-by-date
8154    (gnus-thread-header h1) (gnus-thread-header h2)))
8155
8156 (defsubst gnus-article-sort-by-score (h1 h2)
8157   "Sort articles by root article score.
8158 Unscored articles will be counted as having a score of zero."
8159   (> (or (cdr (assq (mail-header-number h1)
8160                     gnus-newsgroup-scored))
8161          gnus-summary-default-score 0)
8162      (or (cdr (assq (mail-header-number h2)
8163                     gnus-newsgroup-scored))
8164          gnus-summary-default-score 0)))
8165
8166 (defun gnus-thread-sort-by-score (h1 h2)
8167   "Sort threads by root article score."
8168   (gnus-article-sort-by-score
8169    (gnus-thread-header h1) (gnus-thread-header h2)))
8170
8171 (defun gnus-thread-sort-by-total-score (h1 h2)
8172   "Sort threads by the sum of all scores in the thread.
8173 Unscored articles will be counted as having a score of zero."
8174   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8175
8176 (defun gnus-thread-total-score (thread)
8177   ;;  This function find the total score of THREAD.
8178   (if (consp thread)
8179       (if (stringp (car thread))
8180           (apply gnus-thread-score-function 0
8181                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8182         (gnus-thread-total-score-1 thread))
8183     (gnus-thread-total-score-1 (list thread))))
8184
8185 (defun gnus-thread-total-score-1 (root)
8186   ;; This function find the total score of the thread below ROOT.
8187   (setq root (car root))
8188   (apply gnus-thread-score-function
8189          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8190              gnus-summary-default-score 0)
8191          (mapcar 'gnus-thread-total-score
8192                  (cdr (gnus-gethash (mail-header-id root)
8193                                     gnus-newsgroup-dependencies)))))
8194
8195 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8196 (defvar gnus-tmp-prev-subject nil)
8197 (defvar gnus-tmp-false-parent nil)
8198 (defvar gnus-tmp-root-expunged nil)
8199 (defvar gnus-tmp-dummy-line nil)
8200
8201 (defun gnus-summary-prepare-threads (threads)
8202   "Prepare summary buffer from THREADS and indentation LEVEL.
8203 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8204 or a straight list of headers."
8205   (gnus-message 7 "Generating summary...")
8206
8207   (setq gnus-newsgroup-threads threads)
8208   (beginning-of-line)
8209
8210   (let ((gnus-tmp-level 0)
8211         (default-score (or gnus-summary-default-score 0))
8212         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8213         thread number subject stack state gnus-tmp-gathered beg-match
8214         new-roots gnus-tmp-new-adopts thread-end
8215         gnus-tmp-header gnus-tmp-unread
8216         gnus-tmp-replied gnus-tmp-subject-or-nil
8217         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8218         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8219         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8220
8221     (setq gnus-tmp-prev-subject nil)
8222
8223     (if (vectorp (car threads))
8224         ;; If this is a straight (sic) list of headers, then a
8225         ;; threaded summary display isn't required, so we just create
8226         ;; an unthreaded one.
8227         (gnus-summary-prepare-unthreaded threads)
8228
8229       ;; Do the threaded display.
8230
8231       (while (or threads stack gnus-tmp-new-adopts new-roots)
8232
8233         (if (and (= gnus-tmp-level 0)
8234                  (not (setq gnus-tmp-dummy-line nil))
8235                  (or (not stack)
8236                      (= (caar stack) 0))
8237                  (not gnus-tmp-false-parent)
8238                  (or gnus-tmp-new-adopts new-roots))
8239             (if gnus-tmp-new-adopts
8240                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8241                       thread (list (car gnus-tmp-new-adopts))
8242                       gnus-tmp-header (caar thread)
8243                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8244               (if new-roots
8245                   (setq thread (list (car new-roots))
8246                         gnus-tmp-header (caar thread)
8247                         new-roots (cdr new-roots))))
8248
8249           (if threads
8250               ;; If there are some threads, we do them before the
8251               ;; threads on the stack.
8252               (setq thread threads
8253                     gnus-tmp-header (caar thread))
8254             ;; There were no current threads, so we pop something off
8255             ;; the stack.
8256             (setq state (car stack)
8257                   gnus-tmp-level (car state)
8258                   thread (cdr state)
8259                   stack (cdr stack)
8260                   gnus-tmp-header (caar thread))))
8261
8262         (setq gnus-tmp-false-parent nil)
8263         (setq gnus-tmp-root-expunged nil)
8264         (setq thread-end nil)
8265
8266         (if (stringp gnus-tmp-header)
8267             ;; The header is a dummy root.
8268             (cond
8269              ((eq gnus-summary-make-false-root 'adopt)
8270               ;; We let the first article adopt the rest.
8271               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8272                                                (cddar thread)))
8273               (setq gnus-tmp-gathered
8274                     (nconc (mapcar
8275                             (lambda (h) (mail-header-number (car h)))
8276                             (cddar thread))
8277                            gnus-tmp-gathered))
8278               (setq thread (cons (list (caar thread)
8279                                        (cadar thread))
8280                                  (cdr thread)))
8281               (setq gnus-tmp-level -1
8282                     gnus-tmp-false-parent t))
8283              ((eq gnus-summary-make-false-root 'empty)
8284               ;; We print adopted articles with empty subject fields.
8285               (setq gnus-tmp-gathered
8286                     (nconc (mapcar
8287                             (lambda (h) (mail-header-number (car h)))
8288                             (cddar thread))
8289                            gnus-tmp-gathered))
8290               (setq gnus-tmp-level -1))
8291              ((eq gnus-summary-make-false-root 'dummy)
8292               ;; We remember that we probably want to output a dummy
8293               ;; root.
8294               (setq gnus-tmp-dummy-line gnus-tmp-header)
8295               (setq gnus-tmp-prev-subject gnus-tmp-header))
8296              (t
8297               ;; We do not make a root for the gathered
8298               ;; sub-threads at all.
8299               (setq gnus-tmp-level -1)))
8300
8301           (setq number (mail-header-number gnus-tmp-header)
8302                 subject (mail-header-subject gnus-tmp-header))
8303
8304           (cond
8305            ;; If the thread has changed subject, we might want to make
8306            ;; this subthread into a root.
8307            ((and (null gnus-thread-ignore-subject)
8308                  (not (zerop gnus-tmp-level))
8309                  gnus-tmp-prev-subject
8310                  (not (inline
8311                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8312             (setq new-roots (nconc new-roots (list (car thread)))
8313                   thread-end t
8314                   gnus-tmp-header nil))
8315            ;; If the article lies outside the current limit,
8316            ;; then we do not display it.
8317            ((and (not (memq number gnus-newsgroup-limit))
8318                  (not gnus-tmp-dummy-line))
8319             (setq gnus-tmp-gathered
8320                   (nconc (mapcar
8321                           (lambda (h) (mail-header-number (car h)))
8322                           (cdar thread))
8323                          gnus-tmp-gathered))
8324             (setq gnus-tmp-new-adopts (if (cdar thread)
8325                                           (append gnus-tmp-new-adopts
8326                                                   (cdar thread))
8327                                         gnus-tmp-new-adopts)
8328                   thread-end t
8329                   gnus-tmp-header nil)
8330             (when (zerop gnus-tmp-level)
8331               (setq gnus-tmp-root-expunged t)))
8332            ;; Perhaps this article is to be marked as read?
8333            ((and gnus-summary-mark-below
8334                  (< (or (cdr (assq number gnus-newsgroup-scored))
8335                         default-score)
8336                     gnus-summary-mark-below)
8337                  ;; Don't touch sparse articles.
8338                  (not (memq number gnus-newsgroup-sparse)))
8339             (setq gnus-newsgroup-unreads
8340                   (delq number gnus-newsgroup-unreads))
8341             (if gnus-newsgroup-auto-expire
8342                 (push number gnus-newsgroup-expirable)
8343               (push (cons number gnus-low-score-mark)
8344                     gnus-newsgroup-reads))))
8345
8346           (when gnus-tmp-header
8347             ;; We may have an old dummy line to output before this
8348             ;; article.
8349             (when gnus-tmp-dummy-line
8350               (gnus-summary-insert-dummy-line
8351                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8352               (setq gnus-tmp-dummy-line nil))
8353
8354             ;; Compute the mark.
8355             (setq
8356              gnus-tmp-unread
8357              (cond
8358               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8359               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8360               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8361               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8362               (t (or (cdr (assq number gnus-newsgroup-reads))
8363                      gnus-ancient-mark))))
8364
8365             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8366                                   gnus-tmp-header gnus-tmp-level)
8367                   gnus-newsgroup-data)
8368
8369             ;; Actually insert the line.
8370             (setq
8371              gnus-tmp-subject-or-nil
8372              (cond
8373               ((and gnus-thread-ignore-subject
8374                     gnus-tmp-prev-subject
8375                     (not (inline (gnus-subject-equal
8376                                   gnus-tmp-prev-subject subject))))
8377                subject)
8378               ((zerop gnus-tmp-level)
8379                (if (and (eq gnus-summary-make-false-root 'empty)
8380                         (memq number gnus-tmp-gathered)
8381                         gnus-tmp-prev-subject
8382                         (inline (gnus-subject-equal
8383                                  gnus-tmp-prev-subject subject)))
8384                    gnus-summary-same-subject
8385                  subject))
8386               (t gnus-summary-same-subject)))
8387             (if (and (eq gnus-summary-make-false-root 'adopt)
8388                      (= gnus-tmp-level 1)
8389                      (memq number gnus-tmp-gathered))
8390                 (setq gnus-tmp-opening-bracket ?\<
8391                       gnus-tmp-closing-bracket ?\>)
8392               (setq gnus-tmp-opening-bracket ?\[
8393                     gnus-tmp-closing-bracket ?\]))
8394             (setq
8395              gnus-tmp-indentation
8396              (aref gnus-thread-indent-array gnus-tmp-level)
8397              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8398              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8399                                 gnus-summary-default-score 0)
8400              gnus-tmp-score-char
8401              (if (or (null gnus-summary-default-score)
8402                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8403                          gnus-summary-zcore-fuzz)) ? 
8404                (if (< gnus-tmp-score gnus-summary-default-score)
8405                    gnus-score-below-mark gnus-score-over-mark))
8406              gnus-tmp-replied
8407              (cond ((memq number gnus-newsgroup-processable)
8408                     gnus-process-mark)
8409                    ((memq number gnus-newsgroup-cached)
8410                     gnus-cached-mark)
8411                    ((memq number gnus-newsgroup-replied)
8412                     gnus-replied-mark)
8413                    (t gnus-unread-mark))
8414              gnus-tmp-from (mail-header-from gnus-tmp-header)
8415              gnus-tmp-name
8416              (cond
8417               ((string-match "(.+)" gnus-tmp-from)
8418                (substring gnus-tmp-from
8419                           (1+ (match-beginning 0)) (1- (match-end 0))))
8420               ((string-match "<[^>]+> *$" gnus-tmp-from)
8421                (setq beg-match (match-beginning 0))
8422                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8423                         (substring gnus-tmp-from (1+ (match-beginning 0))
8424                                    (1- (match-end 0))))
8425                    (substring gnus-tmp-from 0 beg-match)))
8426               (t gnus-tmp-from)))
8427             (when (string= gnus-tmp-name "")
8428               (setq gnus-tmp-name gnus-tmp-from))
8429             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8430             (put-text-property
8431              (point)
8432              (progn (eval gnus-summary-line-format-spec) (point))
8433              'gnus-number number)
8434             (when gnus-visual-p
8435               (forward-line -1)
8436               (run-hooks 'gnus-summary-update-hook)
8437               (forward-line 1))
8438
8439             (setq gnus-tmp-prev-subject subject)))
8440
8441         (when (nth 1 thread)
8442           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8443         (incf gnus-tmp-level)
8444         (setq threads (if thread-end nil (cdar thread)))
8445         (unless threads
8446           (setq gnus-tmp-level 0)))))
8447   (gnus-message 7 "Generating summary...done"))
8448
8449 (defun gnus-summary-prepare-unthreaded (headers)
8450   "Generate an unthreaded summary buffer based on HEADERS."
8451   (let (header number mark)
8452
8453     (while headers
8454       (setq header (car headers)
8455             headers (cdr headers)
8456             number (mail-header-number header))
8457
8458       ;; We may have to root out some bad articles...
8459       (when (memq number gnus-newsgroup-limit)
8460         (when (and gnus-summary-mark-below
8461                    (< (or (cdr (assq number gnus-newsgroup-scored))
8462                           gnus-summary-default-score 0)
8463                       gnus-summary-mark-below))
8464           (setq gnus-newsgroup-unreads
8465                 (delq number gnus-newsgroup-unreads))
8466           (if gnus-newsgroup-auto-expire
8467               (push number gnus-newsgroup-expirable)
8468             (push (cons number gnus-low-score-mark)
8469                   gnus-newsgroup-reads)))
8470
8471         (setq mark
8472               (cond
8473                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8474                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8475                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8476                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8477                (t (or (cdr (assq number gnus-newsgroup-reads))
8478                       gnus-ancient-mark))))
8479         (setq gnus-newsgroup-data
8480               (cons (gnus-data-make number mark (1+ (point)) header 0)
8481                     gnus-newsgroup-data))
8482         (gnus-summary-insert-line
8483          header 0 nil mark (memq number gnus-newsgroup-replied)
8484          (memq number gnus-newsgroup-expirable)
8485          (mail-header-subject header) nil
8486          (cdr (assq number gnus-newsgroup-scored))
8487          (memq number gnus-newsgroup-processable))))))
8488
8489 (defun gnus-select-newsgroup (group &optional read-all)
8490   "Select newsgroup GROUP.
8491 If READ-ALL is non-nil, all articles in the group are selected."
8492   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8493          (info (nth 2 entry))
8494          articles fetched-articles cached)
8495
8496     (or (gnus-check-server
8497          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8498         (error "Couldn't open server"))
8499
8500     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8501         (gnus-activate-group group) ; Or we can activate it...
8502         (progn ; Or we bug out.
8503           (when (equal major-mode 'gnus-summary-mode)
8504             (kill-buffer (current-buffer)))
8505           (error "Couldn't request group %s: %s"
8506                  group (gnus-status-message group))))
8507
8508     (setq gnus-newsgroup-name group)
8509     (setq gnus-newsgroup-unselected nil)
8510     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8511
8512     (and gnus-asynchronous
8513          (gnus-check-backend-function
8514           'request-asynchronous gnus-newsgroup-name)
8515          (setq gnus-newsgroup-async
8516                (gnus-request-asynchronous gnus-newsgroup-name)))
8517
8518     ;; Adjust and set lists of article marks.
8519     (when info
8520       (gnus-adjust-marked-articles info))
8521
8522     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8523     (when (gnus-virtual-group-p group)
8524       (setq cached gnus-newsgroup-cached))
8525
8526     (setq gnus-newsgroup-unreads
8527           (gnus-set-difference
8528            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8529            gnus-newsgroup-dormant))
8530
8531     (setq gnus-newsgroup-processable nil)
8532
8533     (setq articles (gnus-articles-to-read group read-all))
8534
8535     (cond
8536      ((null articles)
8537       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8538       'quit)
8539      ((eq articles 0) nil)
8540      (t
8541       ;; Init the dependencies hash table.
8542       (setq gnus-newsgroup-dependencies
8543             (gnus-make-hashtable (length articles)))
8544       ;; Retrieve the headers and read them in.
8545       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8546       (setq gnus-newsgroup-headers
8547             (if (eq 'nov
8548                     (setq gnus-headers-retrieved-by
8549                           (gnus-retrieve-headers
8550                            articles gnus-newsgroup-name
8551                            ;; We might want to fetch old headers, but
8552                            ;; not if there is only 1 article.
8553                            (and gnus-fetch-old-headers
8554                                 (or (and
8555                                      (not (eq gnus-fetch-old-headers 'some))
8556                                      (not (numberp gnus-fetch-old-headers)))
8557                                     (> (length articles) 1))))))
8558                 (gnus-get-newsgroup-headers-xover articles)
8559               (gnus-get-newsgroup-headers)))
8560       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
8561
8562       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8563       (when cached
8564         (setq gnus-newsgroup-cached cached))
8565
8566       ;; Set the initial limit.
8567       (setq gnus-newsgroup-limit (copy-sequence articles))
8568       ;; Remove canceled articles from the list of unread articles.
8569       (setq gnus-newsgroup-unreads
8570             (gnus-set-sorted-intersection
8571              gnus-newsgroup-unreads
8572              (setq fetched-articles
8573                    (mapcar (lambda (headers) (mail-header-number headers))
8574                            gnus-newsgroup-headers))))
8575       ;; Removed marked articles that do not exist.
8576       (gnus-update-missing-marks
8577        (gnus-sorted-complement fetched-articles articles))
8578       ;; We might want to build some more threads first.
8579       (and gnus-fetch-old-headers
8580            (eq gnus-headers-retrieved-by 'nov)
8581            (gnus-build-old-threads))
8582       ;; Check whether auto-expire is to be done in this group.
8583       (setq gnus-newsgroup-auto-expire
8584             (gnus-group-auto-expirable-p group))
8585       ;; Set up the article buffer now, if necessary.
8586       (unless gnus-single-article-buffer
8587         (gnus-article-setup-buffer))
8588       ;; First and last article in this newsgroup.
8589       (and gnus-newsgroup-headers
8590            (setq gnus-newsgroup-begin
8591                  (mail-header-number (car gnus-newsgroup-headers)))
8592            (setq gnus-newsgroup-end
8593                  (mail-header-number
8594                   (gnus-last-element gnus-newsgroup-headers))))
8595       (setq gnus-reffed-article-number -1)
8596       ;; GROUP is successfully selected.
8597       (or gnus-newsgroup-headers t)))))
8598
8599 (defun gnus-articles-to-read (group read-all)
8600   ;; Find out what articles the user wants to read.
8601   (let* ((articles
8602           ;; Select all articles if `read-all' is non-nil, or if there
8603           ;; are no unread articles.
8604           (if (or read-all
8605                   (and (zerop (length gnus-newsgroup-marked))
8606                        (zerop (length gnus-newsgroup-unreads))))
8607               (gnus-uncompress-range (gnus-active group))
8608             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8609                           (copy-sequence gnus-newsgroup-unreads))
8610                   '<)))
8611          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8612          (scored (length scored-list))
8613          (number (length articles))
8614          (marked (+ (length gnus-newsgroup-marked)
8615                     (length gnus-newsgroup-dormant)))
8616          (select
8617           (cond
8618            ((numberp read-all)
8619             read-all)
8620            (t
8621             (condition-case ()
8622                 (cond
8623                  ((and (or (<= scored marked) (= scored number))
8624                        (numberp gnus-large-newsgroup)
8625                        (> number gnus-large-newsgroup))
8626                   (let ((input
8627                          (read-string
8628                           (format
8629                            "How many articles from %s (default %d): "
8630                            gnus-newsgroup-name number))))
8631                     (if (string-match "^[ \t]*$" input) number input)))
8632                  ((and (> scored marked) (< scored number))
8633                   (let ((input
8634                          (read-string
8635                           (format "%s %s (%d scored, %d total): "
8636                                   "How many articles from"
8637                                   group scored number))))
8638                     (if (string-match "^[ \t]*$" input)
8639                         number input)))
8640                  (t number))
8641               (quit nil))))))
8642     (setq select (if (stringp select) (string-to-number select) select))
8643     (if (or (null select) (zerop select))
8644         select
8645       (if (and (not (zerop scored)) (<= (abs select) scored))
8646           (progn
8647             (setq articles (sort scored-list '<))
8648             (setq number (length articles)))
8649         (setq articles (copy-sequence articles)))
8650
8651       (if (< (abs select) number)
8652           (if (< select 0)
8653               ;; Select the N oldest articles.
8654               (setcdr (nthcdr (1- (abs select)) articles) nil)
8655             ;; Select the N most recent articles.
8656             (setq articles (nthcdr (- number select) articles))))
8657       (setq gnus-newsgroup-unselected
8658             (gnus-sorted-intersection
8659              gnus-newsgroup-unreads
8660              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8661       articles)))
8662
8663 (defun gnus-killed-articles (killed articles)
8664   (let (out)
8665     (while articles
8666       (if (inline (gnus-member-of-range (car articles) killed))
8667           (setq out (cons (car articles) out)))
8668       (setq articles (cdr articles)))
8669     out))
8670
8671 (defun gnus-uncompress-marks (marks)
8672   "Uncompress the mark ranges in MARKS."
8673   (let ((uncompressed '(score bookmark))
8674         out)
8675     (while marks
8676       (if (memq (caar marks) uncompressed)
8677           (push (car marks) out)
8678         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8679       (setq marks (cdr marks)))
8680     out))
8681
8682 (defun gnus-adjust-marked-articles (info)
8683   "Set all article lists and remove all marks that are no longer legal."
8684   (let* ((marked-lists (gnus-info-marks info))
8685          (active (gnus-active (gnus-info-group info)))
8686          (min (car active))
8687          (max (cdr active))
8688          (types gnus-article-mark-lists)
8689          (uncompressed '(score bookmark))
8690          marks var articles article mark)
8691
8692     (while marked-lists
8693       (setq marks (pop marked-lists))
8694       (set (setq var (intern (format "gnus-newsgroup-%s"
8695                                      (car (rassq (setq mark (car marks))
8696                                                  types)))))
8697            (if (memq (car marks) uncompressed) (cdr marks)
8698              (gnus-uncompress-range (cdr marks))))
8699
8700       (setq articles (symbol-value var))
8701
8702       ;; All articles have to be subsets of the active articles.
8703       (cond
8704        ;; Adjust "simple" lists.
8705        ((memq mark '(tick dormant expirable reply killed save))
8706         (while articles
8707           (when (or (< (setq article (pop articles)) min) (> article max))
8708             (set var (delq article (symbol-value var))))))
8709        ;; Adjust assocs.
8710        ((memq mark '(score bookmark))
8711         (while articles
8712           (when (or (< (car (setq article (pop articles))) min)
8713                     (> (car article) max))
8714             (set var (delq article (symbol-value var))))))))))
8715
8716 (defun gnus-update-missing-marks (missing)
8717   "Go through the list of MISSING articles and remove them mark lists."
8718   (when missing
8719     (let ((types gnus-article-mark-lists)
8720           var m)
8721       ;; Go through all types.
8722       (while types
8723         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8724         (when (symbol-value var)
8725           ;; This list has articles.  So we delete all missing articles
8726           ;; from it.
8727           (setq m missing)
8728           (while m
8729             (set var (delq (pop m) (symbol-value var)))))))))
8730
8731 (defun gnus-update-marks ()
8732   "Enter the various lists of marked articles into the newsgroup info list."
8733   (let ((types gnus-article-mark-lists)
8734         (info (gnus-get-info gnus-newsgroup-name))
8735         (uncompressed '(score bookmark killed))
8736         type list newmarked symbol)
8737     (when info
8738       ;; Add all marks lists that are non-nil to the list of marks lists.
8739       (while types
8740         (setq type (pop types))
8741         (when (setq list (symbol-value
8742                           (setq symbol
8743                                 (intern (format "gnus-newsgroup-%s"
8744                                                 (car type))))))
8745           (push (cons (cdr type)
8746                       (if (memq (cdr type) uncompressed) list
8747                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8748                 newmarked)))
8749
8750       ;; Enter these new marks into the info of the group.
8751       (if (nthcdr 3 info)
8752           (setcar (nthcdr 3 info) newmarked)
8753         ;; Add the marks lists to the end of the info.
8754         (when newmarked
8755           (setcdr (nthcdr 2 info) (list newmarked))))
8756
8757       ;; Cut off the end of the info if there's nothing else there.
8758       (let ((i 5))
8759         (while (and (> i 2)
8760                     (not (nth i info)))
8761           (when (nthcdr (decf i) info)
8762             (setcdr (nthcdr i info) nil)))))))
8763
8764 (defun gnus-add-marked-articles (group type articles &optional info force)
8765   ;; Add ARTICLES of TYPE to the info of GROUP.
8766   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8767   ;; add, but replace marked articles of TYPE with ARTICLES.
8768   (let ((info (or info (gnus-get-info group)))
8769         (uncompressed '(score bookmark killed))
8770         marked m)
8771     (or (not info)
8772         (and (not (setq marked (nthcdr 3 info)))
8773              (or (null articles)
8774                  (setcdr (nthcdr 2 info)
8775                          (list (list (cons type (gnus-compress-sequence
8776                                                  articles t)))))))
8777         (and (not (setq m (assq type (car marked))))
8778              (or (null articles)
8779                  (setcar marked
8780                          (cons (cons type (gnus-compress-sequence articles t) )
8781                                (car marked)))))
8782         (if force
8783             (if (null articles)
8784                 (setcar (nthcdr 3 info)
8785                         (delq (assq type (car marked)) (car marked)))
8786               (setcdr m (gnus-compress-sequence articles t)))
8787           (setcdr m (gnus-compress-sequence
8788                      (sort (nconc (gnus-uncompress-range (cdr m))
8789                                   (copy-sequence articles)) '<) t))))))
8790
8791 (defun gnus-set-mode-line (where)
8792   "This function sets the mode line of the article or summary buffers.
8793 If WHERE is `summary', the summary mode line format will be used."
8794   ;; Is this mode line one we keep updated?
8795   (when (memq where gnus-updated-mode-lines)
8796     (let (mode-string)
8797       (save-excursion
8798         ;; We evaluate this in the summary buffer since these
8799         ;; variables are buffer-local to that buffer.
8800         (set-buffer gnus-summary-buffer)
8801         ;; We bind all these variables that are used in the `eval' form
8802         ;; below.
8803         (let* ((mformat (symbol-value
8804                          (intern
8805                           (format "gnus-%s-mode-line-format-spec" where))))
8806                (gnus-tmp-group-name gnus-newsgroup-name)
8807                (gnus-tmp-article-number (or gnus-current-article 0))
8808                (gnus-tmp-unread gnus-newsgroup-unreads)
8809                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
8810                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
8811                (gnus-tmp-unread-and-unselected
8812                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
8813                             (zerop gnus-tmp-unselected)) "")
8814                       ((zerop gnus-tmp-unselected)
8815                        (format "{%d more}" gnus-tmp-unread-and-unticked))
8816                       (t (format "{%d(+%d) more}"
8817                                  gnus-tmp-unread-and-unticked
8818                                  gnus-tmp-unselected))))
8819                (gnus-tmp-subject
8820                 (if (and gnus-current-headers
8821                          (vectorp gnus-current-headers))
8822                     (mail-header-subject gnus-current-headers) ""))
8823                max-len
8824                gnus-tmp-header);; passed as argument to any user-format-funcs
8825           (setq mode-string (eval mformat))
8826           (setq max-len (max 4 (if gnus-mode-non-string-length
8827                                    (- (frame-width)
8828                                       gnus-mode-non-string-length)
8829                                  (length mode-string))))
8830           ;; We might have to chop a bit of the string off...
8831           (when (> (length mode-string) max-len)
8832             (setq mode-string
8833                   (concat (gnus-truncate-string mode-string (- max-len 3))
8834                           "...")))
8835           ;; Pad the mode string a bit.
8836           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
8837       ;; Update the mode line.
8838       (setq mode-line-buffer-identification (list mode-string))
8839       (set-buffer-modified-p t))))
8840
8841 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
8842   "Go through the HEADERS list and add all Xrefs to a hash table.
8843 The resulting hash table is returned, or nil if no Xrefs were found."
8844   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
8845          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
8846          (xref-hashtb (make-vector 63 0))
8847          start group entry number xrefs header)
8848     (while headers
8849       (setq header (pop headers))
8850       (when (and (setq xrefs (mail-header-xref header))
8851                  (not (memq (setq number (mail-header-number header))
8852                             unreads)))
8853         (setq start 0)
8854         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
8855           (setq start (match-end 0))
8856           (setq group (if prefix
8857                           (concat prefix (substring xrefs (match-beginning 1)
8858                                                     (match-end 1)))
8859                         (substring xrefs (match-beginning 1) (match-end 1))))
8860           (setq number
8861                 (string-to-int (substring xrefs (match-beginning 2)
8862                                           (match-end 2))))
8863           (if (setq entry (gnus-gethash group xref-hashtb))
8864               (setcdr entry (cons number (cdr entry)))
8865             (gnus-sethash group (cons number nil) xref-hashtb)))))
8866     (and start xref-hashtb)))
8867
8868 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
8869   "Look through all the headers and mark the Xrefs as read."
8870   (let ((virtual (gnus-virtual-group-p from-newsgroup))
8871         name entry info xref-hashtb idlist method nth4)
8872     (save-excursion
8873       (set-buffer gnus-group-buffer)
8874       (when (setq xref-hashtb
8875                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
8876         (mapatoms
8877          (lambda (group)
8878            (unless (string= from-newsgroup (setq name (symbol-name group)))
8879              (setq idlist (symbol-value group))
8880              ;; Dead groups are not updated.
8881              (and (prog1
8882                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
8883                             info (nth 2 entry))
8884                     (if (stringp (setq nth4 (gnus-info-method info)))
8885                         (setq nth4 (gnus-server-to-method nth4))))
8886                   ;; Only do the xrefs if the group has the same
8887                   ;; select method as the group we have just read.
8888                   (or (gnus-methods-equal-p
8889                        nth4 (gnus-find-method-for-group from-newsgroup))
8890                       virtual
8891                       (equal nth4 (setq method (gnus-find-method-for-group
8892                                                 from-newsgroup)))
8893                       (and (equal (car nth4) (car method))
8894                            (equal (nth 1 nth4) (nth 1 method))))
8895                   gnus-use-cross-reference
8896                   (or (not (eq gnus-use-cross-reference t))
8897                       virtual
8898                       ;; Only do cross-references on subscribed
8899                       ;; groups, if that is what is wanted.
8900                       (<= (gnus-info-level info) gnus-level-subscribed))
8901                   (gnus-group-make-articles-read name idlist))))
8902          xref-hashtb)))))
8903
8904 (defun gnus-group-make-articles-read (group articles)
8905   (let* ((num 0)
8906          (entry (gnus-gethash group gnus-newsrc-hashtb))
8907          (info (nth 2 entry))
8908          (active (gnus-active group))
8909          range)
8910     ;; First peel off all illegal article numbers.
8911     (if active
8912         (let ((ids articles)
8913               id first)
8914           (while ids
8915             (setq id (car ids))
8916             (if (and first (> id (cdr active)))
8917                 (progn
8918                   ;; We'll end up in this situation in one particular
8919                   ;; obscure situation.  If you re-scan a group and get
8920                   ;; a new article that is cross-posted to a different
8921                   ;; group that has not been re-scanned, you might get
8922                   ;; crossposted article that has a higher number than
8923                   ;; Gnus believes possible.  So we re-activate this
8924                   ;; group as well.  This might mean doing the
8925                   ;; crossposting thingy will *increase* the number
8926                   ;; of articles in some groups.  Tsk, tsk.
8927                   (setq active (or (gnus-activate-group group) active))))
8928             (if (or (> id (cdr active))
8929                     (< id (car active)))
8930                 (setq articles (delq id articles)))
8931             (setq ids (cdr ids)))))
8932     ;; If the read list is nil, we init it.
8933     (and active
8934          (null (gnus-info-read info))
8935          (> (car active) 1)
8936          (gnus-info-set-read info (cons 1 (1- (car active)))))
8937     ;; Then we add the read articles to the range.
8938     (gnus-info-set-read
8939      info
8940      (setq range
8941            (gnus-add-to-range
8942             (gnus-info-read info) (setq articles (sort articles '<)))))
8943     ;; Then we have to re-compute how many unread
8944     ;; articles there are in this group.
8945     (if active
8946         (progn
8947           (cond
8948            ((not range)
8949             (setq num (- (1+ (cdr active)) (car active))))
8950            ((not (listp (cdr range)))
8951             (setq num (- (cdr active) (- (1+ (cdr range))
8952                                          (car range)))))
8953            (t
8954             (while range
8955               (if (numberp (car range))
8956                   (setq num (1+ num))
8957                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
8958               (setq range (cdr range)))
8959             (setq num (- (cdr active) num))))
8960           ;; Update the number of unread articles.
8961           (setcar entry num)
8962           ;; Update the group buffer.
8963           (gnus-group-update-group group t)))))
8964
8965 (defun gnus-methods-equal-p (m1 m2)
8966   (let ((m1 (or m1 gnus-select-method))
8967         (m2 (or m2 gnus-select-method)))
8968     (or (equal m1 m2)
8969         (and (eq (car m1) (car m2))
8970              (or (not (memq 'address (assoc (symbol-name (car m1))
8971                                             gnus-valid-select-methods)))
8972                  (equal (nth 1 m1) (nth 1 m2)))))))
8973
8974 (defsubst gnus-header-value ()
8975   (buffer-substring (match-end 0) (gnus-point-at-eol)))
8976
8977 (defvar gnus-newsgroup-none-id 0)
8978
8979 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
8980   (let ((cur nntp-server-buffer)
8981         (dependencies
8982          (or dependencies
8983              (save-excursion (set-buffer gnus-summary-buffer)
8984                              gnus-newsgroup-dependencies)))
8985         headers id id-dep ref-dep end ref)
8986     (save-excursion
8987       (set-buffer nntp-server-buffer)
8988       (let ((case-fold-search t)
8989             in-reply-to header p lines)
8990         (goto-char (point-min))
8991         ;; Search to the beginning of the next header.  Error messages
8992         ;; do not begin with 2 or 3.
8993         (while (re-search-forward "^[23][0-9]+ " nil t)
8994           (setq id nil
8995                 ref nil)
8996           ;; This implementation of this function, with nine
8997           ;; search-forwards instead of the one re-search-forward and
8998           ;; a case (which basically was the old function) is actually
8999           ;; about twice as fast, even though it looks messier.  You
9000           ;; can't have everything, I guess.  Speed and elegance
9001           ;; doesn't always go hand in hand.
9002           (setq
9003            header
9004            (vector
9005             ;; Number.
9006             (prog1
9007                 (read cur)
9008               (end-of-line)
9009               (setq p (point))
9010               (narrow-to-region (point)
9011                                 (or (and (search-forward "\n.\n" nil t)
9012                                          (- (point) 2))
9013                                     (point))))
9014             ;; Subject.
9015             (progn
9016               (goto-char p)
9017               (if (search-forward "\nsubject: " nil t)
9018                   (gnus-header-value) "(none)"))
9019             ;; From.
9020             (progn
9021               (goto-char p)
9022               (if (search-forward "\nfrom: " nil t)
9023                   (gnus-header-value) "(nobody)"))
9024             ;; Date.
9025             (progn
9026               (goto-char p)
9027               (if (search-forward "\ndate: " nil t)
9028                   (gnus-header-value) ""))
9029             ;; Message-ID.
9030             (progn
9031               (goto-char p)
9032               (if (search-forward "\nmessage-id: " nil t)
9033                   (setq id (gnus-header-value))
9034                 ;; If there was no message-id, we just fake one to make
9035                 ;; subsequent routines simpler.
9036                 (setq id (concat "none+"
9037                                  (int-to-string
9038                                   (setq gnus-newsgroup-none-id
9039                                         (1+ gnus-newsgroup-none-id)))))))
9040             ;; References.
9041             (progn
9042               (goto-char p)
9043               (if (search-forward "\nreferences: " nil t)
9044                   (prog1
9045                       (gnus-header-value)
9046                     (setq end (match-end 0))
9047                     (save-excursion
9048                       (setq ref
9049                             (buffer-substring
9050                              (progn
9051                                (end-of-line)
9052                                (search-backward ">" end t)
9053                                (1+ (point)))
9054                              (progn
9055                                (search-backward "<" end t)
9056                                (point))))))
9057                 ;; Get the references from the in-reply-to header if there
9058                 ;; were no references and the in-reply-to header looks
9059                 ;; promising.
9060                 (if (and (search-forward "\nin-reply-to: " nil t)
9061                          (setq in-reply-to (gnus-header-value))
9062                          (string-match "<[^>]+>" in-reply-to))
9063                     (setq ref (substring in-reply-to (match-beginning 0)
9064                                          (match-end 0)))
9065                   (setq ref ""))))
9066             ;; Chars.
9067             0
9068             ;; Lines.
9069             (progn
9070               (goto-char p)
9071               (if (search-forward "\nlines: " nil t)
9072                   (if (numberp (setq lines (read cur)))
9073                       lines 0)
9074                 0))
9075             ;; Xref.
9076             (progn
9077               (goto-char p)
9078               (and (search-forward "\nxref: " nil t)
9079                    (gnus-header-value)))))
9080           ;; We do the threading while we read the headers.  The
9081           ;; message-id and the last reference are both entered into
9082           ;; the same hash table.  Some tippy-toeing around has to be
9083           ;; done in case an article has arrived before the article
9084           ;; which it refers to.
9085           (if (boundp (setq id-dep (intern id dependencies)))
9086               (if (and (car (symbol-value id-dep))
9087                        (not force-new))
9088                   ;; An article with this Message-ID has already
9089                   ;; been seen, so we ignore this one, except we add
9090                   ;; any additional Xrefs (in case the two articles
9091                   ;; came from different servers).
9092                   (progn
9093                     (mail-header-set-xref
9094                      (car (symbol-value id-dep))
9095                      (concat (or (mail-header-xref
9096                                   (car (symbol-value id-dep))) "")
9097                              (or (mail-header-xref header) "")))
9098                     (setq header nil))
9099                 (setcar (symbol-value id-dep) header))
9100             (set id-dep (list header)))
9101           (when header
9102             (if (boundp (setq ref-dep (intern ref dependencies)))
9103                 (setcdr (symbol-value ref-dep)
9104                         (nconc (cdr (symbol-value ref-dep))
9105                                (list (symbol-value id-dep))))
9106               (set ref-dep (list nil (symbol-value id-dep))))
9107             (setq headers (cons header headers)))
9108           (goto-char (point-max))
9109           (widen))
9110         (nreverse headers)))))
9111
9112 ;; The following macros and functions were written by Felix Lee
9113 ;; <flee@cse.psu.edu>.
9114
9115 (defmacro gnus-nov-read-integer ()
9116   '(prog1
9117        (if (= (following-char) ?\t)
9118            0
9119          (let ((num (condition-case nil (read buffer) (error nil))))
9120            (if (numberp num) num 0)))
9121      (or (eobp) (forward-char 1))))
9122
9123 (defmacro gnus-nov-skip-field ()
9124   '(search-forward "\t" eol 'move))
9125
9126 (defmacro gnus-nov-field ()
9127   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9128
9129 ;; Goes through the xover lines and returns a list of vectors
9130 (defun gnus-get-newsgroup-headers-xover (sequence &optional force-new)
9131   "Parse the news overview data in the server buffer, and return a
9132 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9133   ;; Get the Xref when the users reads the articles since most/some
9134   ;; NNTP servers do not include Xrefs when using XOVER.
9135   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9136   (let ((cur nntp-server-buffer)
9137         (dependencies gnus-newsgroup-dependencies)
9138         number headers header)
9139     (save-excursion
9140       (set-buffer nntp-server-buffer)
9141       ;; Allow the user to mangle the headers before parsing them.
9142       (run-hooks 'gnus-parse-headers-hook)
9143       ;; Allow the user to mangle the headers before parsing them.
9144       (run-hooks 'gnus-parse-headers-hook)
9145       (goto-char (point-min))
9146       (while (and sequence (not (eobp)))
9147         (setq number (read cur))
9148         (while (and sequence (< (car sequence) number))
9149           (setq sequence (cdr sequence)))
9150         (and sequence
9151              (eq number (car sequence))
9152              (progn
9153                (setq sequence (cdr sequence))
9154                (if (setq header
9155                          (inline (gnus-nov-parse-line
9156                                   number dependencies force-new)))
9157                    (setq headers (cons header headers)))))
9158         (forward-line 1))
9159       (setq headers (nreverse headers)))
9160     headers))
9161
9162 ;; This function has to be called with point after the article number
9163 ;; on the beginning of the line.
9164 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9165   (let ((none 0)
9166         (eol (gnus-point-at-eol))
9167         (buffer (current-buffer))
9168         header ref id id-dep ref-dep)
9169
9170     ;; overview: [num subject from date id refs chars lines misc]
9171     (narrow-to-region (point) eol)
9172     (or (eobp) (forward-char))
9173
9174     (condition-case nil
9175         (setq header
9176               (vector
9177                number                   ; number
9178                (gnus-nov-field)         ; subject
9179                (gnus-nov-field)         ; from
9180                (gnus-nov-field)         ; date
9181                (setq id (or (gnus-nov-field)
9182                             (concat "none+"
9183                                     (int-to-string
9184                                      (setq none (1+ none)))))) ; id
9185                (progn
9186                  (save-excursion
9187                    (let ((beg (point)))
9188                      (search-forward "\t" eol)
9189                      (if (search-backward ">" beg t)
9190                          (setq ref
9191                                (buffer-substring
9192                                 (1+ (point))
9193                                 (search-backward "<" beg t)))
9194                        (setq ref nil))))
9195                  (gnus-nov-field))      ; refs
9196                (gnus-nov-read-integer)  ; chars
9197                (gnus-nov-read-integer)  ; lines
9198                (if (= (following-char) ?\n)
9199                    nil
9200                  (gnus-nov-field))      ; misc
9201                ))
9202       (error (progn
9203                (ding)
9204                (gnus-message 4 "Strange nov line")
9205                (setq header nil)
9206                (goto-char eol))))
9207
9208     (widen)
9209
9210     ;; We build the thread tree.
9211     (when header
9212       (if (boundp (setq id-dep (intern id dependencies)))
9213           (if (and (car (symbol-value id-dep))
9214                    (not force-new))
9215               ;; An article with this Message-ID has already been seen,
9216               ;; so we ignore this one, except we add any additional
9217               ;; Xrefs (in case the two articles came from different
9218               ;; servers.
9219               (progn
9220                 (mail-header-set-xref
9221                  (car (symbol-value id-dep))
9222                  (concat (or (mail-header-xref
9223                               (car (symbol-value id-dep))) "")
9224                          (or (mail-header-xref header) "")))
9225                 (setq header nil))
9226             (setcar (symbol-value id-dep) header))
9227         (set id-dep (list header))))
9228     (if header
9229         (progn
9230           (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9231               (setcdr (symbol-value ref-dep)
9232                       (nconc (cdr (symbol-value ref-dep))
9233                              (list (symbol-value id-dep))))
9234             (set ref-dep (list nil (symbol-value id-dep))))))
9235     header))
9236
9237 (defun gnus-article-get-xrefs ()
9238   "Fill in the Xref value in `gnus-current-headers', if necessary.
9239 This is meant to be called in `gnus-article-internal-prepare-hook'."
9240   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9241                                  gnus-current-headers)))
9242     (or (not gnus-use-cross-reference)
9243         (not headers)
9244         (and (mail-header-xref headers)
9245              (not (string= (mail-header-xref headers) "")))
9246         (let ((case-fold-search t)
9247               xref)
9248           (save-restriction
9249             (nnheader-narrow-to-headers)
9250             (goto-char (point-min))
9251             (if (or (and (eq (downcase (following-char)) ?x)
9252                          (looking-at "Xref:"))
9253                     (search-forward "\nXref:" nil t))
9254                 (progn
9255                   (goto-char (1+ (match-end 0)))
9256                   (setq xref (buffer-substring (point)
9257                                                (progn (end-of-line) (point))))
9258                   (mail-header-set-xref headers xref))))))))
9259
9260 (defun gnus-summary-insert-subject (id &optional old-header)
9261   "Find article ID and insert the summary line for that article."
9262   (let ((header (gnus-read-header id))
9263         (number (and (numberp id) id))
9264         pos)
9265     (when header
9266       ;; Rebuild the thread that this article is part of and go to the
9267       ;; article we have fetched.
9268       (when old-header
9269         (when (setq pos (text-property-any
9270                          (point-min) (point-max) 'gnus-number 
9271                          (mail-header-number old-header)))
9272           (goto-char pos)
9273           (gnus-delete-line)
9274           (gnus-data-remove (mail-header-number old-header))))
9275       (gnus-rebuild-thread (mail-header-id header))
9276       (gnus-summary-goto-subject (setq number (mail-header-number header))))
9277     (when (and (numberp number)
9278                (> number 0))
9279       ;; We have to update the boundaries even if we can't fetch the
9280       ;; article if ID is a number -- so that the next `P' or `N'
9281       ;; command will fetch the previous (or next) article even
9282       ;; if the one we tried to fetch this time has been canceled.
9283       (and (> number gnus-newsgroup-end)
9284            (setq gnus-newsgroup-end number))
9285       (and (< number gnus-newsgroup-begin)
9286            (setq gnus-newsgroup-begin number))
9287       (setq gnus-newsgroup-unselected
9288             (delq number gnus-newsgroup-unselected)))
9289     ;; Report back a success?
9290     (and header (mail-header-number header))))
9291
9292 (defun gnus-summary-work-articles (n)
9293   "Return a list of articles to be worked upon.  The prefix argument,
9294 the list of process marked articles, and the current article will be
9295 taken into consideration."
9296   (cond
9297    ((and n (numberp n))
9298     ;; A numerical prefix has been given.
9299     (let ((backward (< n 0))
9300           (n (abs n))
9301           articles article)
9302       (save-excursion
9303         (while
9304             (and (> n 0)
9305                  (push (setq article (gnus-summary-article-number))
9306                        articles)
9307                  (if backward
9308                      (gnus-summary-find-prev nil article)
9309                    (gnus-summary-find-next nil article)))
9310           (decf n)))
9311       (nreverse articles)))
9312    ((and (boundp 'transient-mark-mode)
9313          transient-mark-mode
9314          mark-active)
9315     ;; Work on the region between point and mark.
9316     (let ((max (max (point) (mark)))
9317           articles article)
9318       (save-excursion
9319         (goto-char (min (point) (mark)))
9320         (while
9321             (and
9322              (push (setq article (gnus-summary-article-number)) articles)
9323              (gnus-summary-find-next nil article)
9324              (< (point) max)))
9325         (nreverse articles))))
9326    (gnus-newsgroup-processable
9327     ;; There are process-marked articles present.
9328     (reverse gnus-newsgroup-processable))
9329    (t
9330     ;; Just return the current article.
9331     (list (gnus-summary-article-number)))))
9332
9333 (defun gnus-summary-search-group (&optional backward use-level)
9334   "Search for next unread newsgroup.
9335 If optional argument BACKWARD is non-nil, search backward instead."
9336   (save-excursion
9337     (set-buffer gnus-group-buffer)
9338     (if (gnus-group-search-forward
9339          backward nil (if use-level (gnus-group-group-level) nil))
9340         (gnus-group-group-name))))
9341
9342 (defun gnus-summary-best-group (&optional exclude-group)
9343   "Find the name of the best unread group.
9344 If EXCLUDE-GROUP, do not go to this group."
9345   (save-excursion
9346     (set-buffer gnus-group-buffer)
9347     (save-excursion
9348       (gnus-group-best-unread-group exclude-group))))
9349
9350 (defun gnus-summary-find-next (&optional unread article backward)
9351   (if backward (gnus-summary-find-prev)
9352     (let* ((article (or article (gnus-summary-article-number)))
9353            (arts (gnus-data-find-list article))
9354            result)
9355       (when (or (not gnus-summary-check-current)
9356                 (not unread)
9357                 (not (gnus-data-unread-p (car arts))))
9358         (setq arts (cdr arts)))
9359       (when (setq result
9360                   (if unread
9361                       (progn
9362                         (while arts
9363                           (when (gnus-data-unread-p (car arts))
9364                             (setq result (car arts)
9365                                   arts nil))
9366                           (setq arts (cdr arts)))
9367                         result)
9368                     (car arts)))
9369         (goto-char (gnus-data-pos result))
9370         (gnus-data-number result)))))
9371
9372 (defun gnus-summary-find-prev (&optional unread article)
9373   (let* ((article (or article (gnus-summary-article-number)))
9374          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9375          result)
9376     (when (or (not gnus-summary-check-current)
9377               (not unread)
9378               (not (gnus-data-unread-p (car arts))))
9379       (setq arts (cdr arts)))
9380     (if (setq result
9381               (if unread
9382                   (progn
9383                     (while arts
9384                       (and (gnus-data-unread-p (car arts))
9385                            (setq result (car arts)
9386                                  arts nil))
9387                       (setq arts (cdr arts)))
9388                     result)
9389                 (car arts)))
9390         (progn
9391           (goto-char (gnus-data-pos result))
9392           (gnus-data-number result)))))
9393
9394 (defun gnus-summary-find-subject (subject &optional unread backward article)
9395   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9396          (article (or article (gnus-summary-article-number)))
9397          (articles (gnus-data-list backward))
9398          (arts (gnus-data-find-list article articles))
9399          result)
9400     (when (or (not gnus-summary-check-current)
9401               (not unread)
9402               (not (gnus-data-unread-p (car arts))))
9403       (setq arts (cdr arts)))
9404     (while arts
9405       (and (or (not unread)
9406                (gnus-data-unread-p (car arts)))
9407            (vectorp (gnus-data-header (car arts)))
9408            (gnus-subject-equal
9409             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9410            (setq result (car arts)
9411                  arts nil))
9412       (setq arts (cdr arts)))
9413     (and result
9414          (goto-char (gnus-data-pos result))
9415          (gnus-data-number result))))
9416
9417 (defun gnus-summary-search-forward (&optional unread subject backward)
9418   "Search forward for an article.
9419 If UNREAD, look for unread articles.  If SUBJECT, look for
9420 articles with that subject.  If BACKWARD, search backward instead."
9421   (cond (subject (gnus-summary-find-subject subject unread backward))
9422         (backward (gnus-summary-find-prev unread))
9423         (t (gnus-summary-find-next unread))))
9424
9425 (defun gnus-recenter (&optional n)
9426   "Center point in window and redisplay frame.
9427 Also do horizontal recentering."
9428   (interactive "P")
9429   (when (and gnus-auto-center-summary
9430              (not (eq gnus-auto-center-summary 'vertical)))
9431     (gnus-horizontal-recenter))
9432   (recenter n))
9433
9434 (defun gnus-summary-recenter ()
9435   "Center point in the summary window.
9436 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9437 displayed, no centering will be performed."
9438   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9439   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9440   (let* ((top (cond ((< (window-height) 4) 0)
9441                     ((< (window-height) 7) 1)
9442                     (t 2)))
9443          (height (1- (window-height)))
9444          (bottom (save-excursion (goto-char (point-max))
9445                                  (forward-line (- height))
9446                                  (point)))
9447          (window (get-buffer-window (current-buffer))))
9448     ;; The user has to want it.
9449     (when gnus-auto-center-summary
9450       (when (get-buffer-window gnus-article-buffer)
9451        ;; Only do recentering when the article buffer is displayed,
9452        ;; Set the window start to either `bottom', which is the biggest
9453        ;; possible valid number, or the second line from the top,
9454        ;; whichever is the least.
9455        (set-window-start
9456         window (min bottom (save-excursion 
9457                              (forward-line (- top)) (point)))))
9458       ;; Do horizontal recentering while we're at it.
9459       (when (and (get-buffer-window (current-buffer) t)
9460                  (not (eq gnus-auto-center-summary 'vertical)))
9461         (let ((selected (selected-window)))
9462           (select-window (get-buffer-window (current-buffer) t))
9463           (gnus-summary-position-point)
9464           (gnus-horizontal-recenter)
9465           (select-window selected))))))
9466
9467 (defun gnus-horizontal-recenter ()
9468   "Recenter the current buffer horizontally."
9469   (if (< (current-column) (/ (window-width) 2))
9470       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9471     (let* ((orig (point))
9472            (end (window-end (get-buffer-window (current-buffer) t)))
9473            (max 0))
9474       ;; Find the longest line currently displayed in the window.
9475       (goto-char (window-start))
9476       (while (and (not (eobp)) 
9477                   (< (point) end))
9478         (end-of-line)
9479         (setq max (max max (current-column)))
9480         (forward-line 1))
9481       (goto-char orig)
9482       ;; Scroll horizontally to center (sort of) the point.
9483       (if (> max (window-width))
9484           (set-window-hscroll 
9485            (get-buffer-window (current-buffer) t)
9486            (min (- (current-column) (/ (window-width) 3))
9487                 (+ 2 (- max (window-width)))))
9488         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9489       max)))
9490
9491 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9492 (defun gnus-short-group-name (group &optional levels)
9493   "Collapse GROUP name LEVELS."
9494   (let* ((name "") 
9495          (foreign "")
9496          (depth 0) 
9497          (skip 1)
9498          (levels (or levels
9499                      (progn
9500                        (while (string-match "\\." group skip)
9501                          (setq skip (match-end 0)
9502                                depth (+ depth 1)))
9503                        depth))))
9504     (if (string-match ":" group)
9505         (setq foreign (substring group 0 (match-end 0))
9506               group (substring group (match-end 0))))
9507     (while group
9508       (if (and (string-match "\\." group)
9509                (> levels (- gnus-group-uncollapsed-levels 1)))
9510           (setq name (concat name (substring group 0 1))
9511                 group (substring group (match-end 0))
9512                 levels (- levels 1)
9513                 name (concat name "."))
9514         (setq name (concat foreign name group)
9515               group nil)))
9516     name))
9517
9518 (defun gnus-summary-jump-to-group (newsgroup)
9519   "Move point to NEWSGROUP in group mode buffer."
9520   ;; Keep update point of group mode buffer if visible.
9521   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9522       (save-window-excursion
9523         ;; Take care of tree window mode.
9524         (if (get-buffer-window gnus-group-buffer)
9525             (pop-to-buffer gnus-group-buffer))
9526         (gnus-group-jump-to-group newsgroup))
9527     (save-excursion
9528       ;; Take care of tree window mode.
9529       (if (get-buffer-window gnus-group-buffer)
9530           (pop-to-buffer gnus-group-buffer)
9531         (set-buffer gnus-group-buffer))
9532       (gnus-group-jump-to-group newsgroup))))
9533
9534 ;; This function returns a list of article numbers based on the
9535 ;; difference between the ranges of read articles in this group and
9536 ;; the range of active articles.
9537 (defun gnus-list-of-unread-articles (group)
9538   (let* ((read (gnus-info-read (gnus-get-info group)))
9539          (active (gnus-active group))
9540          (last (cdr active))
9541          first nlast unread)
9542     ;; If none are read, then all are unread.
9543     (if (not read)
9544         (setq first (car active))
9545       ;; If the range of read articles is a single range, then the
9546       ;; first unread article is the article after the last read
9547       ;; article.  Sounds logical, doesn't it?
9548       (if (not (listp (cdr read)))
9549           (setq first (1+ (cdr read)))
9550         ;; `read' is a list of ranges.
9551         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9552                                 (caar read))) 1)
9553             (setq first 1))
9554         (while read
9555           (if first
9556               (while (< first nlast)
9557                 (setq unread (cons first unread))
9558                 (setq first (1+ first))))
9559           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9560           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9561           (setq read (cdr read)))))
9562     ;; And add the last unread articles.
9563     (while (<= first last)
9564       (setq unread (cons first unread))
9565       (setq first (1+ first)))
9566     ;; Return the list of unread articles.
9567     (nreverse unread)))
9568
9569 (defun gnus-list-of-read-articles (group)
9570   "Return a list of unread, unticked and non-dormant articles."
9571   (let* ((info (gnus-get-info group))
9572          (marked (gnus-info-marks info))
9573          (active (gnus-active group)))
9574     (and info active
9575          (gnus-set-difference
9576           (gnus-sorted-complement
9577            (gnus-uncompress-range active)
9578            (gnus-list-of-unread-articles group))
9579           (append
9580            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9581            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9582
9583 ;; Various summary commands
9584
9585 (defun gnus-summary-universal-argument (arg)
9586   "Perform any operation on all articles that are process/prefixed."
9587   (interactive "P")
9588   (gnus-set-global-variables)
9589   (let ((articles (gnus-summary-work-articles arg))
9590         func article)
9591     (if (eq
9592          (setq
9593           func
9594           (key-binding
9595            (read-key-sequence
9596             (substitute-command-keys
9597              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9598              ))))
9599          'undefined)
9600         (progn
9601           (message "Undefined key")
9602           (ding))
9603       (save-excursion
9604         (while articles
9605           (gnus-summary-goto-subject (setq article (pop articles)))
9606           (command-execute func)
9607           (gnus-summary-remove-process-mark article)))))
9608   (gnus-summary-position-point))
9609
9610 (defun gnus-summary-toggle-truncation (&optional arg)
9611   "Toggle truncation of summary lines.
9612 With arg, turn line truncation on iff arg is positive."
9613   (interactive "P")
9614   (setq truncate-lines
9615         (if (null arg) (not truncate-lines)
9616           (> (prefix-numeric-value arg) 0)))
9617   (redraw-display))
9618
9619 (defun gnus-summary-reselect-current-group (&optional all rescan)
9620   "Exit and then reselect the current newsgroup.
9621 The prefix argument ALL means to select all articles."
9622   (interactive "P")
9623   (gnus-set-global-variables)
9624   (let ((current-subject (gnus-summary-article-number))
9625         (group gnus-newsgroup-name))
9626     (setq gnus-newsgroup-begin nil)
9627     (gnus-summary-exit)
9628     ;; We have to adjust the point of group mode buffer because the
9629     ;; current point was moved to the next unread newsgroup by
9630     ;; exiting.
9631     (gnus-summary-jump-to-group group)
9632     (when rescan
9633       (save-excursion
9634         (gnus-group-get-new-news-this-group 1)))
9635     (gnus-group-read-group all t)
9636     (gnus-summary-goto-subject current-subject)))
9637
9638 (defun gnus-summary-rescan-group (&optional all)
9639   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9640   (interactive "P")
9641   (gnus-summary-reselect-current-group all t))
9642
9643 (defun gnus-summary-update-info ()
9644   (let* ((group gnus-newsgroup-name))
9645     (when gnus-newsgroup-kill-headers
9646       (setq gnus-newsgroup-killed
9647             (gnus-compress-sequence
9648              (nconc
9649               (gnus-set-sorted-intersection
9650                (gnus-uncompress-range gnus-newsgroup-killed)
9651                (setq gnus-newsgroup-unselected
9652                      (sort gnus-newsgroup-unselected '<)))
9653               (setq gnus-newsgroup-unreads
9654                     (sort gnus-newsgroup-unreads '<))) t)))
9655     (unless (listp (cdr gnus-newsgroup-killed))
9656       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9657     (let ((headers gnus-newsgroup-headers))
9658       (run-hooks 'gnus-exit-group-hook)
9659       (unless gnus-save-score
9660         (setq gnus-newsgroup-scored nil))
9661       ;; Set the new ranges of read articles.
9662       (gnus-update-read-articles
9663        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9664       ;; Set the current article marks.
9665       (gnus-update-marks)
9666       ;; Do the cross-ref thing.
9667       (when gnus-use-cross-reference
9668         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9669       ;; Do adaptive scoring, and possibly save score files.
9670       (when gnus-newsgroup-adaptive
9671         (gnus-score-adaptive))
9672       (when gnus-use-scoring
9673         (gnus-score-save))
9674       ;; Do not switch windows but change the buffer to work.
9675       (set-buffer gnus-group-buffer)
9676       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9677           (gnus-group-update-group group)))))
9678
9679 (defun gnus-summary-exit (&optional temporary)
9680   "Exit reading current newsgroup, and then return to group selection mode.
9681 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9682   (interactive)
9683   (gnus-set-global-variables)
9684   (gnus-kill-save-kill-buffer)
9685   (let* ((group gnus-newsgroup-name)
9686          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9687          (mode major-mode)
9688          (buf (current-buffer)))
9689     (unless temporary
9690       (run-hooks 'gnus-summary-prepare-exit-hook))
9691     ;; If we have several article buffers, we kill them at exit.
9692     (unless gnus-single-article-buffer
9693       (gnus-kill-buffer gnus-article-buffer)
9694       (gnus-kill-buffer gnus-original-article-buffer)
9695       (setq gnus-article-current nil))
9696     (when gnus-use-cache
9697       (gnus-cache-possibly-remove-articles)
9698       (gnus-cache-save-buffers))
9699     (when gnus-use-trees
9700       (gnus-tree-close group))
9701     ;; Make all changes in this group permanent.
9702     (unless quit-config
9703       (gnus-summary-update-info))
9704     (gnus-close-group group)
9705     ;; Make sure where I was, and go to next newsgroup.
9706     (set-buffer gnus-group-buffer)
9707     (unless quit-config
9708       (gnus-group-jump-to-group group)
9709       (gnus-group-next-unread-group 1))
9710     (run-hooks 'gnus-summary-exit-hook)
9711     (unless gnus-single-article-buffer
9712       (setq gnus-article-current nil))
9713     (if temporary
9714         nil                             ;Nothing to do.
9715       ;; If we have several article buffers, we kill them at exit.
9716       (unless gnus-single-article-buffer
9717         (gnus-kill-buffer gnus-article-buffer)
9718         (gnus-kill-buffer gnus-original-article-buffer)
9719         (setq gnus-article-current nil))
9720       (set-buffer buf)
9721       (if (not gnus-kill-summary-on-exit)
9722           (gnus-deaden-summary)
9723         ;; We set all buffer-local variables to nil.  It is unclear why
9724         ;; this is needed, but if we don't, buffer-local variables are
9725         ;; not garbage-collected, it seems.  This would the lead to en
9726         ;; ever-growing Emacs.
9727         (gnus-summary-clear-local-variables)
9728         (when (get-buffer gnus-article-buffer)
9729           (bury-buffer gnus-article-buffer))
9730         ;; We clear the global counterparts of the buffer-local
9731         ;; variables as well, just to be on the safe side.
9732         (gnus-configure-windows 'group 'force)
9733         (gnus-summary-clear-local-variables)
9734         ;; Return to group mode buffer.
9735         (if (eq mode 'gnus-summary-mode)
9736             (gnus-kill-buffer buf)))
9737       (setq gnus-current-select-method gnus-select-method)
9738       (pop-to-buffer gnus-group-buffer)
9739       ;; Clear the current group name.
9740       (if (not quit-config)
9741           (progn
9742             (gnus-group-jump-to-group group)
9743             (gnus-group-next-unread-group 1)
9744             (gnus-configure-windows 'group 'force))
9745         (if (not (buffer-name (car quit-config)))
9746             (gnus-configure-windows 'group 'force)
9747           (set-buffer (car quit-config))
9748           (and (eq major-mode 'gnus-summary-mode)
9749                (gnus-set-global-variables))
9750           (gnus-configure-windows (cdr quit-config))))
9751       (unless quit-config
9752         (setq gnus-newsgroup-name nil)))))
9753
9754 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9755 (defun gnus-summary-exit-no-update (&optional no-questions)
9756   "Quit reading current newsgroup without updating read article info."
9757   (interactive)
9758   (gnus-set-global-variables)
9759   (let* ((group gnus-newsgroup-name)
9760          (quit-config (gnus-group-quit-config group)))
9761     (when (or no-questions
9762               gnus-expert-user
9763               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9764       ;; If we have several article buffers, we kill them at exit.
9765       (unless gnus-single-article-buffer
9766         (gnus-kill-buffer gnus-article-buffer)
9767         (gnus-kill-buffer gnus-original-article-buffer)
9768         (setq gnus-article-current nil))
9769       (if (not gnus-kill-summary-on-exit)
9770           (gnus-deaden-summary)
9771         (gnus-close-group group)
9772         (gnus-summary-clear-local-variables)
9773         (set-buffer gnus-group-buffer)
9774         (gnus-summary-clear-local-variables)
9775         (when (get-buffer gnus-summary-buffer)
9776           (kill-buffer gnus-summary-buffer)))
9777       (unless gnus-single-article-buffer
9778         (setq gnus-article-current nil))
9779       (when gnus-use-trees
9780         (gnus-tree-close group))
9781       (when (get-buffer gnus-article-buffer)
9782         (bury-buffer gnus-article-buffer))
9783       ;; Return to the group buffer.
9784       (gnus-configure-windows 'group 'force)
9785       ;; Clear the current group name.
9786       (setq gnus-newsgroup-name nil)
9787       (when (equal (gnus-group-group-name) group)
9788         (gnus-group-next-unread-group 1))
9789       (when quit-config
9790         (if (not (buffer-name (car quit-config)))
9791             (gnus-configure-windows 'group 'force)
9792           (set-buffer (car quit-config))
9793           (when (eq major-mode 'gnus-summary-mode)
9794             (gnus-set-global-variables))
9795           (gnus-configure-windows (cdr quit-config)))))))
9796
9797 ;;; Dead summaries.
9798
9799 (defvar gnus-dead-summary-mode-map nil)
9800
9801 (if gnus-dead-summary-mode-map
9802     nil
9803   (setq gnus-dead-summary-mode-map (make-keymap))
9804   (suppress-keymap gnus-dead-summary-mode-map)
9805   (substitute-key-definition
9806    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
9807   (let ((keys '("\C-d" "\r" "\177")))
9808     (while keys
9809       (define-key gnus-dead-summary-mode-map
9810         (pop keys) 'gnus-summary-wake-up-the-dead))))
9811
9812 (defvar gnus-dead-summary-mode nil
9813   "Minor mode for Gnus summary buffers.")
9814
9815 (defun gnus-dead-summary-mode (&optional arg)
9816   "Minor mode for Gnus summary buffers."
9817   (interactive "P")
9818   (when (eq major-mode 'gnus-summary-mode)
9819     (make-local-variable 'gnus-dead-summary-mode)
9820     (setq gnus-dead-summary-mode
9821           (if (null arg) (not gnus-dead-summary-mode)
9822             (> (prefix-numeric-value arg) 0)))
9823     (when gnus-dead-summary-mode
9824       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
9825         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
9826       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
9827         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
9828               minor-mode-map-alist)))))
9829
9830 (defun gnus-deaden-summary ()
9831   "Make the current summary buffer into a dead summary buffer."
9832   ;; Kill any previous dead summary buffer.
9833   (when (and gnus-dead-summary
9834              (buffer-name gnus-dead-summary))
9835     (save-excursion
9836       (set-buffer gnus-dead-summary)
9837       (when gnus-dead-summary-mode
9838         (kill-buffer (current-buffer)))))
9839   ;; Make this the current dead summary.
9840   (setq gnus-dead-summary (current-buffer))
9841   (gnus-dead-summary-mode 1)
9842   (let ((name (buffer-name)))
9843     (when (string-match "Summary" name)
9844       (rename-buffer
9845        (concat (substring name 0 (match-beginning 0)) "Dead "
9846                (substring name (match-beginning 0))) t))))
9847
9848 (defun gnus-kill-or-deaden-summary (buffer)
9849   "Kill or deaden the summary BUFFER."
9850   (cond (gnus-kill-summary-on-exit
9851          (when (and gnus-use-trees
9852                     (and (get-buffer buffer)
9853                          (buffer-name (get-buffer buffer))))
9854            (save-excursion
9855              (set-buffer (get-buffer buffer))
9856              (gnus-tree-close gnus-newsgroup-name)))
9857          (gnus-kill-buffer buffer))
9858         ((and (get-buffer buffer)
9859               (buffer-name (get-buffer buffer)))
9860          (save-excursion
9861            (set-buffer buffer)
9862            (gnus-deaden-summary)))))
9863
9864 (defun gnus-summary-wake-up-the-dead (&rest args)
9865   "Wake up the dead summary buffer."
9866   (interactive)
9867   (gnus-dead-summary-mode -1)
9868   (let ((name (buffer-name)))
9869     (when (string-match "Dead " name)
9870       (rename-buffer
9871        (concat (substring name 0 (match-beginning 0))
9872                (substring name (match-end 0))) t)))
9873   (gnus-message 3 "This dead summary is now alive again"))
9874
9875 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
9876 (defun gnus-summary-fetch-faq (&optional faq-dir)
9877   "Fetch the FAQ for the current group.
9878 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
9879 in."
9880   (interactive
9881    (list
9882     (if current-prefix-arg
9883         (completing-read
9884          "Faq dir: " (and (listp gnus-group-faq-directory)
9885                           gnus-group-faq-directory)))))
9886   (let (gnus-faq-buffer)
9887     (and (setq gnus-faq-buffer
9888                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
9889          (gnus-configure-windows 'summary-faq))))
9890
9891 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
9892 (defun gnus-summary-describe-group (&optional force)
9893   "Describe the current newsgroup."
9894   (interactive "P")
9895   (gnus-group-describe-group force gnus-newsgroup-name))
9896
9897 (defun gnus-summary-describe-briefly ()
9898   "Describe summary mode commands briefly."
9899   (interactive)
9900   (gnus-message 6
9901                 (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")))
9902
9903 ;; Walking around group mode buffer from summary mode.
9904
9905 (defun gnus-summary-next-group (&optional no-article target-group backward)
9906   "Exit current newsgroup and then select next unread newsgroup.
9907 If prefix argument NO-ARTICLE is non-nil, no article is selected
9908 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
9909 previous group instead."
9910   (interactive "P")
9911   (gnus-set-global-variables)
9912   (let ((current-group gnus-newsgroup-name)
9913         (current-buffer (current-buffer))
9914         entered)
9915     ;; First we semi-exit this group to update Xrefs and all variables.
9916     ;; We can't do a real exit, because the window conf must remain
9917     ;; the same in case the user is prompted for info, and we don't
9918     ;; want the window conf to change before that...
9919     (gnus-summary-exit t)
9920     (while (not entered)
9921       ;; Then we find what group we are supposed to enter.
9922       (set-buffer gnus-group-buffer)
9923       (gnus-group-jump-to-group current-group)
9924       (setq target-group
9925             (or target-group
9926                 (if (eq gnus-keep-same-level 'best)
9927                     (gnus-summary-best-group gnus-newsgroup-name)
9928                   (gnus-summary-search-group backward gnus-keep-same-level))))
9929       (if (not target-group)
9930           ;; There are no further groups, so we return to the group
9931           ;; buffer.
9932           (progn
9933             (gnus-message 5 "Returning to the group buffer")
9934             (setq entered t)
9935             (set-buffer current-buffer)
9936             (gnus-summary-exit))
9937         ;; We try to enter the target group.
9938         (gnus-group-jump-to-group target-group)
9939         (let ((unreads (gnus-group-group-unread)))
9940           (if (and (or (eq t unreads)
9941                        (and unreads (not (zerop unreads))))
9942                    (gnus-summary-read-group
9943                     target-group nil no-article current-buffer))
9944               (setq entered t)
9945             (setq current-group target-group
9946                   target-group nil)))))))
9947
9948 (defun gnus-summary-prev-group (&optional no-article)
9949   "Exit current newsgroup and then select previous unread newsgroup.
9950 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
9951   (interactive "P")
9952   (gnus-summary-next-group no-article nil t))
9953
9954 ;; Walking around summary lines.
9955
9956 (defun gnus-summary-first-subject (&optional unread)
9957   "Go to the first unread subject.
9958 If UNREAD is non-nil, go to the first unread article.
9959 Returns the article selected or nil if there are no unread articles."
9960   (interactive "P")
9961   (prog1
9962       (cond
9963        ;; Empty summary.
9964        ((null gnus-newsgroup-data)
9965         (gnus-message 3 "No articles in the group")
9966         nil)
9967        ;; Pick the first article.
9968        ((not unread)
9969         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
9970         (gnus-data-number (car gnus-newsgroup-data)))
9971        ;; No unread articles.
9972        ((null gnus-newsgroup-unreads)
9973         (gnus-message 3 "No more unread articles")
9974         nil)
9975        ;; Find the first unread article.
9976        (t
9977         (let ((data gnus-newsgroup-data))
9978           (while (and data
9979                       (not (gnus-data-unread-p (car data))))
9980             (setq data (cdr data)))
9981           (if data
9982               (progn
9983                 (goto-char (gnus-data-pos (car data)))
9984                 (gnus-data-number (car data)))))))
9985     (gnus-summary-position-point)))
9986
9987 (defun gnus-summary-next-subject (n &optional unread dont-display)
9988   "Go to next N'th summary line.
9989 If N is negative, go to the previous N'th subject line.
9990 If UNREAD is non-nil, only unread articles are selected.
9991 The difference between N and the actual number of steps taken is
9992 returned."
9993   (interactive "p")
9994   (let ((backward (< n 0))
9995         (n (abs n)))
9996     (while (and (> n 0)
9997                 (if backward
9998                     (gnus-summary-find-prev unread)
9999                   (gnus-summary-find-next unread)))
10000       (setq n (1- n)))
10001     (if (/= 0 n) (gnus-message 7 "No more%s articles"
10002                                (if unread " unread" "")))
10003     (unless dont-display
10004       (gnus-summary-recenter)
10005       (gnus-summary-position-point))
10006     n))
10007
10008 (defun gnus-summary-next-unread-subject (n)
10009   "Go to next N'th unread summary line."
10010   (interactive "p")
10011   (gnus-summary-next-subject n t))
10012
10013 (defun gnus-summary-prev-subject (n &optional unread)
10014   "Go to previous N'th summary line.
10015 If optional argument UNREAD is non-nil, only unread article is selected."
10016   (interactive "p")
10017   (gnus-summary-next-subject (- n) unread))
10018
10019 (defun gnus-summary-prev-unread-subject (n)
10020   "Go to previous N'th unread summary line."
10021   (interactive "p")
10022   (gnus-summary-next-subject (- n) t))
10023
10024 (defun gnus-summary-goto-subject (article &optional force silent)
10025   "Go the subject line of ARTICLE.
10026 If FORCE, also allow jumping to articles not currently shown."
10027   (let ((b (point))
10028         (data (gnus-data-find article)))
10029     ;; We read in the article if we have to.
10030     (and (not data)
10031          force
10032          (gnus-summary-insert-subject article)
10033          (setq data (gnus-data-find article)))
10034     (goto-char b)
10035     (if (not data)
10036         (progn
10037           (unless silent
10038             (gnus-message 3 "Can't find article %d" article))
10039           nil)
10040       (goto-char (gnus-data-pos data))
10041       article)))
10042
10043 ;; Walking around summary lines with displaying articles.
10044
10045 (defun gnus-summary-expand-window (&optional arg)
10046   "Make the summary buffer take up the entire Emacs frame.
10047 Given a prefix, will force an `article' buffer configuration."
10048   (interactive "P")
10049   (gnus-set-global-variables)
10050   (if arg
10051       (gnus-configure-windows 'article 'force)
10052     (gnus-configure-windows 'summary 'force)))
10053
10054 (defun gnus-summary-display-article (article &optional all-header)
10055   "Display ARTICLE in article buffer."
10056   (gnus-set-global-variables)
10057   (if (null article)
10058       nil
10059     (prog1
10060         (if gnus-summary-display-article-function
10061             (funcall gnus-summary-display-article-function article all-header)
10062           (gnus-article-prepare article all-header))
10063       (run-hooks 'gnus-select-article-hook)
10064       (gnus-summary-recenter)
10065       (gnus-summary-goto-subject article)
10066       (when gnus-use-trees
10067         (gnus-possibly-generate-tree article)
10068         (gnus-highlight-selected-tree article))
10069       ;; Successfully display article.
10070       (gnus-article-set-window-start
10071        (cdr (assq article gnus-newsgroup-bookmarks)))
10072       t)))
10073
10074 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
10075   "Select the current article.
10076 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
10077 non-nil, the article will be re-fetched even if it already present in
10078 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
10079 be displayed."
10080   (let ((article (or article (gnus-summary-article-number)))
10081         (all-headers (not (not all-headers))) ;Must be T or NIL.
10082         gnus-summary-display-article-function
10083         did)
10084     (and (not pseudo)
10085          (gnus-summary-article-pseudo-p article)
10086          (error "This is a pseudo-article."))
10087     (prog1
10088         (save-excursion
10089           (set-buffer gnus-summary-buffer)
10090           (if (or (and gnus-single-article-buffer
10091                        (or (null gnus-current-article)
10092                            (null gnus-article-current)
10093                            (null (get-buffer gnus-article-buffer))
10094                            (not (eq article (cdr gnus-article-current)))
10095                            (not (equal (car gnus-article-current)
10096                                        gnus-newsgroup-name))))
10097                   (and (not gnus-single-article-buffer)
10098                        (or (null gnus-current-article)
10099                            (not (eq gnus-current-article article))))
10100                   force)
10101               ;; The requested article is different from the current article.
10102               (prog1
10103                   (gnus-summary-display-article article all-headers)
10104                 (setq did article))
10105             (if (or all-headers gnus-show-all-headers)
10106                 (gnus-article-show-all-headers))
10107             'old))
10108       (if did
10109           (gnus-article-set-window-start
10110            (cdr (assq article gnus-newsgroup-bookmarks)))))))
10111
10112 (defun gnus-summary-set-current-mark (&optional current-mark)
10113   "Obsolete function."
10114   nil)
10115
10116 (defun gnus-summary-next-article (&optional unread subject backward push)
10117   "Select the next article.
10118 If UNREAD, only unread articles are selected.
10119 If SUBJECT, only articles with SUBJECT are selected.
10120 If BACKWARD, the previous article is selected instead of the next."
10121   (interactive "P")
10122   (gnus-set-global-variables)
10123   (cond
10124    ;; Is there such an article?
10125    ((and (gnus-summary-search-forward unread subject backward)
10126          (or (gnus-summary-display-article (gnus-summary-article-number))
10127              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10128     (gnus-summary-position-point))
10129    ;; If not, we try the first unread, if that is wanted.
10130    ((and subject
10131          gnus-auto-select-same
10132          (or (gnus-summary-first-unread-article)
10133              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10134     (gnus-summary-position-point)
10135     (gnus-message 6 "Wrapped"))
10136    ;; Try to get next/previous article not displayed in this group.
10137    ((and gnus-auto-extend-newsgroup
10138          (not unread) (not subject))
10139     (gnus-summary-goto-article
10140      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10141      nil t))
10142    ;; Go to next/previous group.
10143    (t
10144     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10145         (gnus-summary-jump-to-group gnus-newsgroup-name))
10146     (let ((cmd last-command-char)
10147           (group
10148            (if (eq gnus-keep-same-level 'best)
10149                (gnus-summary-best-group gnus-newsgroup-name)
10150              (gnus-summary-search-group backward gnus-keep-same-level))))
10151       ;; For some reason, the group window gets selected.  We change
10152       ;; it back.
10153       (select-window (get-buffer-window (current-buffer)))
10154       ;; Select next unread newsgroup automagically.
10155       (cond
10156        ((not gnus-auto-select-next)
10157         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10158        ((or (eq gnus-auto-select-next 'quietly)
10159             (and (eq gnus-auto-select-next 'slightly-quietly)
10160                  push)
10161             (and (eq gnus-auto-select-next 'almost-quietly)
10162                  (gnus-summary-last-article-p)))
10163         ;; Select quietly.
10164         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10165             (gnus-summary-exit)
10166           (gnus-message 7 "No more%s articles (%s)..."
10167                         (if unread " unread" "")
10168                         (if group (concat "selecting " group)
10169                           "exiting"))
10170           (gnus-summary-next-group nil group backward)))
10171        (t
10172         (gnus-summary-walk-group-buffer
10173          gnus-newsgroup-name cmd unread backward)))))))
10174
10175 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10176   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10177                       (?\C-p (gnus-group-prev-unread-group 1))))
10178         keve key group ended)
10179     (save-excursion
10180       (set-buffer gnus-group-buffer)
10181       (gnus-summary-jump-to-group from-group)
10182       (setq group
10183             (if (eq gnus-keep-same-level 'best)
10184                 (gnus-summary-best-group gnus-newsgroup-name)
10185               (gnus-summary-search-group backward gnus-keep-same-level))))
10186     (while (not ended)
10187       (gnus-message
10188        5 "No more%s articles%s" (if unread " unread" "")
10189        (if (and group
10190                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10191            (format " (Type %s for %s [%s])"
10192                    (single-key-description cmd) group
10193                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10194          (format " (Type %s to exit %s)"
10195                  (single-key-description cmd)
10196                  gnus-newsgroup-name)))
10197       ;; Confirm auto selection.
10198       (setq key (car (setq keve (gnus-read-event-char))))
10199       (setq ended t)
10200       (cond
10201        ((assq key keystrokes)
10202         (let ((obuf (current-buffer)))
10203           (switch-to-buffer gnus-group-buffer)
10204           (and group
10205                (gnus-group-jump-to-group group))
10206           (eval (cadr (assq key keystrokes)))
10207           (setq group (gnus-group-group-name))
10208           (switch-to-buffer obuf))
10209         (setq ended nil))
10210        ((equal key cmd)
10211         (if (or (not group)
10212                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10213             (gnus-summary-exit)
10214           (gnus-summary-next-group nil group backward)))
10215        (t
10216         (push (cdr keve) unread-command-events))))))
10217
10218 (defun gnus-read-event-char ()
10219   "Get the next event."
10220   (let ((event (read-event)))
10221     (cons (and (numberp event) event) event)))
10222
10223 (defun gnus-summary-next-unread-article ()
10224   "Select unread article after current one."
10225   (interactive)
10226   (gnus-summary-next-article t (and gnus-auto-select-same
10227                                     (gnus-summary-article-subject))))
10228
10229 (defun gnus-summary-prev-article (&optional unread subject)
10230   "Select the article after the current one.
10231 If UNREAD is non-nil, only unread articles are selected."
10232   (interactive "P")
10233   (gnus-summary-next-article unread subject t))
10234
10235 (defun gnus-summary-prev-unread-article ()
10236   "Select unred article before current one."
10237   (interactive)
10238   (gnus-summary-prev-article t (and gnus-auto-select-same
10239                                     (gnus-summary-article-subject))))
10240
10241 (defun gnus-summary-next-page (&optional lines circular)
10242   "Show next page of the selected article.
10243 If at the end of the current article, select the next article.
10244 LINES says how many lines should be scrolled up.
10245
10246 If CIRCULAR is non-nil, go to the start of the article instead of
10247 selecting the next article when reaching the end of the current
10248 article."
10249   (interactive "P")
10250   (setq gnus-summary-buffer (current-buffer))
10251   (gnus-set-global-variables)
10252   (let ((article (gnus-summary-article-number))
10253         (endp nil))
10254     (gnus-configure-windows 'article)
10255     (if (or (null gnus-current-article)
10256             (null gnus-article-current)
10257             (/= article (cdr gnus-article-current))
10258             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10259         ;; Selected subject is different from current article's.
10260         (gnus-summary-display-article article)
10261       (gnus-eval-in-buffer-window
10262        gnus-article-buffer
10263        (setq endp (gnus-article-next-page lines)))
10264       (if endp
10265           (cond (circular
10266                  (gnus-summary-beginning-of-article))
10267                 (lines
10268                  (gnus-message 3 "End of message"))
10269                 ((null lines)
10270                  (if (and (eq gnus-summary-goto-unread 'never)
10271                           (not (gnus-summary-last-article-p article)))
10272                      (gnus-summary-next-article)
10273                    (gnus-summary-next-unread-article))))))
10274     (gnus-summary-recenter)
10275     (gnus-summary-position-point)))
10276
10277 (defun gnus-summary-prev-page (&optional lines)
10278   "Show previous page of selected article.
10279 Argument LINES specifies lines to be scrolled down."
10280   (interactive "P")
10281   (gnus-set-global-variables)
10282   (let ((article (gnus-summary-article-number)))
10283     (gnus-configure-windows 'article)
10284     (if (or (null gnus-current-article)
10285             (null gnus-article-current)
10286             (/= article (cdr gnus-article-current))
10287             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10288         ;; Selected subject is different from current article's.
10289         (gnus-summary-display-article article)
10290       (gnus-summary-recenter)
10291       (gnus-eval-in-buffer-window gnus-article-buffer
10292                                   (gnus-article-prev-page lines))))
10293   (gnus-summary-position-point))
10294
10295 (defun gnus-summary-scroll-up (lines)
10296   "Scroll up (or down) one line current article.
10297 Argument LINES specifies lines to be scrolled up (or down if negative)."
10298   (interactive "p")
10299   (gnus-set-global-variables)
10300   (gnus-configure-windows 'article)
10301   (gnus-summary-show-thread)
10302   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10303     (gnus-eval-in-buffer-window
10304      gnus-article-buffer
10305      (cond ((> lines 0)
10306             (if (gnus-article-next-page lines)
10307                 (gnus-message 3 "End of message")))
10308            ((< lines 0)
10309             (gnus-article-prev-page (- lines))))))
10310   (gnus-summary-recenter)
10311   (gnus-summary-position-point))
10312
10313 (defun gnus-summary-next-same-subject ()
10314   "Select next article which has the same subject as current one."
10315   (interactive)
10316   (gnus-set-global-variables)
10317   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10318
10319 (defun gnus-summary-prev-same-subject ()
10320   "Select previous article which has the same subject as current one."
10321   (interactive)
10322   (gnus-set-global-variables)
10323   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10324
10325 (defun gnus-summary-next-unread-same-subject ()
10326   "Select next unread article which has the same subject as current one."
10327   (interactive)
10328   (gnus-set-global-variables)
10329   (gnus-summary-next-article t (gnus-summary-article-subject)))
10330
10331 (defun gnus-summary-prev-unread-same-subject ()
10332   "Select previous unread article which has the same subject as current one."
10333   (interactive)
10334   (gnus-set-global-variables)
10335   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10336
10337 (defun gnus-summary-first-unread-article ()
10338   "Select the first unread article.
10339 Return nil if there are no unread articles."
10340   (interactive)
10341   (gnus-set-global-variables)
10342   (prog1
10343       (if (gnus-summary-first-subject t)
10344           (progn
10345             (gnus-summary-show-thread)
10346             (gnus-summary-first-subject t)
10347             (gnus-summary-display-article (gnus-summary-article-number))))
10348     (gnus-summary-position-point)))
10349
10350 (defun gnus-summary-best-unread-article ()
10351   "Select the unread article with the highest score."
10352   (interactive)
10353   (gnus-set-global-variables)
10354   (let ((best -1000000)
10355         (data gnus-newsgroup-data)
10356         article score)
10357     (while data
10358       (and (gnus-data-unread-p (car data))
10359            (> (setq score
10360                     (gnus-summary-article-score (gnus-data-number (car data))))
10361               best)
10362            (setq best score
10363                  article (gnus-data-number (car data))))
10364       (setq data (cdr data)))
10365     (prog1
10366         (if article
10367             (gnus-summary-goto-article article)
10368           (error "No unread articles"))
10369       (gnus-summary-position-point))))
10370
10371 (defun gnus-summary-last-subject ()
10372   "Go to the last displayed subject line in the group."
10373   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10374     (when article
10375       (gnus-summary-goto-subject article))))
10376
10377 (defun gnus-summary-goto-article (article &optional all-headers force)
10378   "Fetch ARTICLE and display it if it exists.
10379 If ALL-HEADERS is non-nil, no header lines are hidden."
10380   (interactive
10381    (list
10382     (string-to-int
10383      (completing-read
10384       "Article number: "
10385       (mapcar (lambda (number) (list (int-to-string number)))
10386               gnus-newsgroup-limit)))
10387     current-prefix-arg
10388     t))
10389   (prog1
10390       (if (gnus-summary-goto-subject article force)
10391           (gnus-summary-display-article article all-headers)
10392         (gnus-message 4 "Couldn't go to article %s" article) nil)
10393     (gnus-summary-position-point)))
10394
10395 (defun gnus-summary-goto-last-article ()
10396   "Go to the previously read article."
10397   (interactive)
10398   (prog1
10399       (and gnus-last-article
10400            (gnus-summary-goto-article gnus-last-article))
10401     (gnus-summary-position-point)))
10402
10403 (defun gnus-summary-pop-article (number)
10404   "Pop one article off the history and go to the previous.
10405 NUMBER articles will be popped off."
10406   (interactive "p")
10407   (let (to)
10408     (setq gnus-newsgroup-history
10409           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10410     (if to
10411         (gnus-summary-goto-article (car to))
10412       (error "Article history empty")))
10413   (gnus-summary-position-point))
10414
10415 ;; Summary commands and functions for limiting the summary buffer.
10416
10417 (defun gnus-summary-limit-to-articles (n)
10418   "Limit the summary buffer to the next N articles.
10419 If not given a prefix, use the process marked articles instead."
10420   (interactive "P")
10421   (gnus-set-global-variables)
10422   (prog1
10423       (let ((articles (gnus-summary-work-articles n)))
10424         (setq gnus-newsgroup-processable nil)
10425         (gnus-summary-limit articles))
10426     (gnus-summary-position-point)))
10427
10428 (defun gnus-summary-pop-limit (&optional total)
10429   "Restore the previous limit.
10430 If given a prefix, remove all limits."
10431   (interactive "P")
10432   (gnus-set-global-variables)
10433   (when total 
10434     (setq gnus-newsgroup-limits
10435           (list (mapcar (lambda (h) (mail-header-number h))
10436                         gnus-newsgroup-headers))))
10437   (unless gnus-newsgroup-limits
10438     (error "No limit to pop"))
10439   (prog1
10440       (gnus-summary-limit nil 'pop)
10441     (gnus-summary-position-point)))
10442
10443 (defun gnus-summary-limit-to-subject (subject &optional header)
10444   "Limit the summary buffer to articles that have subjects that match a regexp."
10445   (interactive "sRegexp: ")
10446   (unless header
10447     (setq header "subject"))
10448   (when (not (equal "" subject))
10449     (prog1
10450         (let ((articles (gnus-summary-find-matching
10451                          (or header "subject") subject 'all)))
10452           (or articles (error "Found no matches for \"%s\"" subject))
10453           (gnus-summary-limit articles))
10454       (gnus-summary-position-point))))
10455
10456 (defun gnus-summary-limit-to-author (from)
10457   "Limit the summary buffer to articles that have authors that match a regexp."
10458   (interactive "sRegexp: ")
10459   (gnus-summary-limit-to-subject from "from"))
10460
10461 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10462 (make-obsolete
10463  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10464
10465 (defun gnus-summary-limit-to-unread (&optional all)
10466   "Limit the summary buffer to articles that are not marked as read.
10467 If ALL is non-nil, limit strictly to unread articles."
10468   (interactive "P")
10469   (if all
10470       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10471     (gnus-summary-limit-to-marks
10472      ;; Concat all the marks that say that an article is read and have
10473      ;; those removed.
10474      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10475            gnus-killed-mark gnus-kill-file-mark
10476            gnus-low-score-mark gnus-expirable-mark
10477            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10478      'reverse)))
10479
10480 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10481 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10482
10483 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10484   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10485 If REVERSE, limit the summary buffer to articles that are not marked
10486 with MARKS.  MARKS can either be a string of marks or a list of marks.
10487 Returns how many articles were removed."
10488   (interactive "sMarks: ")
10489   (gnus-set-global-variables)
10490   (prog1
10491       (let ((data gnus-newsgroup-data)
10492             (marks (if (listp marks) marks
10493                      (append marks nil))) ; Transform to list.
10494             articles)
10495         (while data
10496           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10497                  (memq (gnus-data-mark (car data)) marks))
10498                (setq articles (cons (gnus-data-number (car data)) articles)))
10499           (setq data (cdr data)))
10500         (gnus-summary-limit articles))
10501     (gnus-summary-position-point)))
10502
10503 (defun gnus-summary-limit-to-score (&optional score)
10504   "Limit to articles with score at or above SCORE."
10505   (interactive "P")
10506   (gnus-set-global-variables)
10507   (setq score (if score
10508                   (prefix-numeric-value score)
10509                 (or gnus-summary-default-score 0)))
10510   (let ((data gnus-newsgroup-data)
10511         articles)
10512     (while data
10513       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10514                 score)
10515         (push (gnus-data-number (car data)) articles))
10516       (setq data (cdr data)))
10517     (prog1
10518         (gnus-summary-limit articles)
10519       (gnus-summary-position-point))))
10520
10521 (defun gnus-summary-limit-include-dormant ()
10522   "Display all the hidden articles that are marked as dormant."
10523   (interactive)
10524   (gnus-set-global-variables)
10525   (or gnus-newsgroup-dormant
10526       (error "There are no dormant articles in this group"))
10527   (prog1
10528       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10529     (gnus-summary-position-point)))
10530
10531 (defun gnus-summary-limit-exclude-dormant ()
10532   "Hide all dormant articles."
10533   (interactive)
10534   (gnus-set-global-variables)
10535   (prog1
10536       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10537     (gnus-summary-position-point)))
10538
10539 (defun gnus-summary-limit-exclude-childless-dormant ()
10540   "Hide all dormant articles that have no children."
10541   (interactive)
10542   (gnus-set-global-variables)
10543   (let ((data (gnus-data-list t))
10544         articles d children)
10545     ;; Find all articles that are either not dormant or have
10546     ;; children.
10547     (while (setq d (pop data))
10548       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10549                 (and (setq children 
10550                            (gnus-article-children (gnus-data-number d)))
10551                      (let (found)
10552                        (while children
10553                          (when (memq (car children) articles)
10554                            (setq children nil
10555                                  found t))
10556                          (pop children))
10557                        found)))
10558         (push (gnus-data-number d) articles)))
10559     ;; Do the limiting.
10560     (prog1
10561         (gnus-summary-limit articles)
10562       (gnus-summary-position-point))))
10563
10564 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10565   "Mark all unread excluded articles as read.
10566 If ALL, mark even excluded ticked and dormants as read."
10567   (interactive "P")
10568   (let ((articles (gnus-sorted-complement
10569                    (sort
10570                     (mapcar (lambda (h) (mail-header-number h))
10571                             gnus-newsgroup-headers)
10572                     '<)
10573                    (sort gnus-newsgroup-limit '<)))
10574         article)
10575     (setq gnus-newsgroup-unreads nil)
10576     (if all
10577         (setq gnus-newsgroup-dormant nil
10578               gnus-newsgroup-marked nil
10579               gnus-newsgroup-reads
10580               (nconc
10581                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10582                gnus-newsgroup-reads))
10583       (while (setq article (pop articles))
10584         (unless (or (memq article gnus-newsgroup-dormant)
10585                     (memq article gnus-newsgroup-marked))
10586           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10587
10588 (defun gnus-summary-limit (articles &optional pop)
10589   (if pop
10590       ;; We pop the previous limit off the stack and use that.
10591       (setq articles (car gnus-newsgroup-limits)
10592             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10593     ;; We use the new limit, so we push the old limit on the stack.
10594     (setq gnus-newsgroup-limits
10595           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10596   ;; Set the limit.
10597   (setq gnus-newsgroup-limit articles)
10598   (let ((total (length gnus-newsgroup-data))
10599         (data (gnus-data-find-list (gnus-summary-article-number)))
10600         found)
10601     ;; This will do all the work of generating the new summary buffer
10602     ;; according to the new limit.
10603     (gnus-summary-prepare)
10604     ;; Hide any threads, possibly.
10605     (and gnus-show-threads
10606          gnus-thread-hide-subtree
10607          (gnus-summary-hide-all-threads))
10608     ;; Try to return to the article you were at, or one in the
10609     ;; neighborhood.
10610     (if data
10611         ;; We try to find some article after the current one.
10612         (while data
10613           (and (gnus-summary-goto-subject
10614                 (gnus-data-number (car data)) nil t)
10615                (setq data nil
10616                      found t))
10617           (setq data (cdr data))))
10618     (or found
10619         ;; If there is no data, that means that we were after the last
10620         ;; article.  The same goes when we can't find any articles
10621         ;; after the current one.
10622         (progn
10623           (goto-char (point-max))
10624           (gnus-summary-find-prev)))
10625     ;; We return how many articles were removed from the summary
10626     ;; buffer as a result of the new limit.
10627     (- total (length gnus-newsgroup-data))))
10628
10629 (defsubst gnus-cut-thread (thread)
10630   "Go forwards in the thread until we find an article that we want to display."
10631   (when (eq gnus-fetch-old-headers 'some)
10632     ;; Deal with old-fetched headers.
10633     (while (and thread
10634                 (memq (mail-header-number (car thread)) 
10635                       gnus-newsgroup-ancient)
10636                 (<= (length (cdr thread)) 1))
10637       (setq thread (cadr thread))))
10638   ;; Deal with sparse threads.
10639   (when (or (eq gnus-build-sparse-threads 'some)
10640             (eq gnus-build-sparse-threads 'more))
10641     (while (and thread
10642                 (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10643                 (= (length (cdr thread)) 1))
10644       (setq thread (cadr thread))))
10645   thread)
10646
10647 (defun gnus-cut-threads (threads)
10648   "Cut off all uninteresting articles from the beginning of threads."
10649   (when (or (eq gnus-fetch-old-headers 'some)
10650             (eq gnus-build-sparse-threads 'some)
10651             (eq gnus-build-sparse-threads 'more))
10652     (let ((th threads))
10653       (while th
10654         (setcar th (gnus-cut-thread (car th)))
10655         (setq th (cdr th)))))
10656   ;; Remove nixed out threads.
10657   (delq nil threads))
10658
10659 (defun gnus-summary-initial-limit (&optional show-if-empty)
10660   "Figure out what the initial limit is supposed to be on group entry.
10661 This entails weeding out unwanted dormants, low-scored articles,
10662 fetch-old-headers verbiage, and so on."
10663   ;; Most groups have nothing to remove.
10664   (if (or gnus-inhibit-limiting
10665           (and (null gnus-newsgroup-dormant)
10666                (not (eq gnus-fetch-old-headers 'some))
10667                (null gnus-summary-expunge-below)
10668                (not (eq gnus-build-sparse-threads 'some))
10669                (not (eq gnus-build-sparse-threads 'more))
10670                (null gnus-thread-expunge-below)
10671                (not gnus-use-nocem)))
10672       () ; Do nothing.
10673     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10674     (setq gnus-newsgroup-limit nil)
10675     (mapatoms
10676      (lambda (node)
10677        (unless (car (symbol-value node))
10678          ;; These threads have no parents -- they are roots.
10679          (let ((nodes (cdr (symbol-value node)))
10680                thread)
10681            (while nodes
10682              (if (and gnus-thread-expunge-below
10683                       (< (gnus-thread-total-score (car nodes))
10684                          gnus-thread-expunge-below))
10685                  (gnus-expunge-thread (pop nodes))
10686                (setq thread (pop nodes))
10687                (gnus-summary-limit-children thread))))))
10688      gnus-newsgroup-dependencies)
10689     ;; If this limitation resulted in an empty group, we might
10690     ;; pop the previous limit and use it instead.
10691     (when (and (not gnus-newsgroup-limit)
10692                show-if-empty)
10693       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10694     gnus-newsgroup-limit))
10695
10696 (defun gnus-summary-limit-children (thread)
10697   "Return 1 if this subthread is visible and 0 if it is not."
10698   ;; First we get the number of visible children to this thread.  This
10699   ;; is done by recursing down the thread using this function, so this
10700   ;; will really go down to a leaf article first, before slowly
10701   ;; working its way up towards the root.
10702   (when thread
10703     (let ((children
10704            (if (cdr thread)
10705                (apply '+ (mapcar 'gnus-summary-limit-children
10706                                  (cdr thread)))
10707              0))
10708           (number (mail-header-number (car thread)))
10709           score)
10710       (if (or
10711            ;; If this article is dormant and has absolutely no visible
10712            ;; children, then this article isn't visible.
10713            (and (memq number gnus-newsgroup-dormant)
10714                 (= children 0))
10715            ;; If this is a "fetch-old-headered" and there is only one
10716            ;; visible child (or less), then we don't want this article.
10717            (and (eq gnus-fetch-old-headers 'some)
10718                 (memq number gnus-newsgroup-ancient)
10719                 (zerop children))
10720            ;; If this is a sparsely inserted article with no children,
10721            ;; we don't want it.
10722            (and (eq gnus-build-sparse-threads 'some)
10723                 (memq number gnus-newsgroup-sparse)
10724                 (zerop children))
10725            ;; If we use expunging, and this article is really
10726            ;; low-scored, then we don't want this article.
10727            (when (and gnus-summary-expunge-below
10728                       (< (setq score
10729                                (or (cdr (assq number gnus-newsgroup-scored))
10730                                    gnus-summary-default-score))
10731                          gnus-summary-expunge-below))
10732              ;; We increase the expunge-tally here, but that has
10733              ;; nothing to do with the limits, really.
10734              (incf gnus-newsgroup-expunged-tally)
10735              ;; We also mark as read here, if that's wanted.
10736              (when (and gnus-summary-mark-below
10737                         (< score gnus-summary-mark-below))
10738                (setq gnus-newsgroup-unreads
10739                      (delq number gnus-newsgroup-unreads))
10740                (if gnus-newsgroup-auto-expire
10741                    (push number gnus-newsgroup-expirable)
10742                  (push (cons number gnus-low-score-mark)
10743                        gnus-newsgroup-reads)))
10744              t)
10745            (and gnus-use-nocem
10746                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
10747           ;; Nope, invisible article.
10748           0
10749         ;; Ok, this article is to be visible, so we add it to the limit
10750         ;; and return 1.
10751         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
10752         1))))
10753
10754 (defun gnus-expunge-thread (thread)
10755   "Mark all articles in THREAD as read."
10756   (let* ((number (mail-header-number (car thread))))
10757     (incf gnus-newsgroup-expunged-tally)
10758     ;; We also mark as read here, if that's wanted.
10759     (setq gnus-newsgroup-unreads
10760           (delq number gnus-newsgroup-unreads))
10761     (if gnus-newsgroup-auto-expire
10762         (push number gnus-newsgroup-expirable)
10763       (push (cons number gnus-low-score-mark)
10764             gnus-newsgroup-reads)))
10765   ;; Go recursively through all subthreads.
10766   (mapcar 'gnus-expunge-thread (cdr thread)))
10767
10768 ;; Summary article oriented commands
10769
10770 (defun gnus-summary-refer-parent-article (n)
10771   "Refer parent article N times.
10772 The difference between N and the number of articles fetched is returned."
10773   (interactive "p")
10774   (gnus-set-global-variables)
10775   (while
10776       (and
10777        (> n 0)
10778        (let* ((header (gnus-summary-article-header))
10779               (ref
10780                ;; If we try to find the parent of the currently
10781                ;; displayed article, then we take a look at the actual
10782                ;; References header, since this is slightly more
10783                ;; reliable than the References field we got from the
10784                ;; server.
10785                (if (and (eq (mail-header-number header)
10786                             (cdr gnus-article-current))
10787                         (equal gnus-newsgroup-name
10788                                (car gnus-article-current)))
10789                    (save-excursion
10790                      (set-buffer gnus-original-article-buffer)
10791                      (nnheader-narrow-to-headers)
10792                      (prog1
10793                          (mail-fetch-field "references")
10794                        (widen)))
10795                  ;; It's not the current article, so we take a bet on
10796                  ;; the value we got from the server.
10797                  (mail-header-references header))))
10798          (if (setq ref (or ref (mail-header-references header)))
10799              (or (gnus-summary-refer-article (gnus-parent-id ref))
10800                  (gnus-message 1 "Couldn't find parent"))
10801            (gnus-message 1 "No references in article %d"
10802                          (gnus-summary-article-number))
10803            nil)))
10804     (setq n (1- n)))
10805   (gnus-summary-position-point)
10806   n)
10807
10808 (defun gnus-summary-refer-references ()
10809   "Fetch all articles mentioned in the References header.
10810 Return how many articles were fetched."
10811   (interactive)
10812   (gnus-set-global-variables)
10813   (let ((ref (mail-header-references (gnus-summary-article-header)))
10814         (current (gnus-summary-article-number))
10815         (n 0))
10816     ;; For each Message-ID in the References header...
10817     (while (string-match "<[^>]*>" ref)
10818       (incf n)
10819       ;; ... fetch that article.
10820       (gnus-summary-refer-article
10821        (prog1 (match-string 0 ref)
10822          (setq ref (substring ref (match-end 0))))))
10823     (gnus-summary-goto-subject current)
10824     (gnus-summary-position-point)
10825     n))
10826
10827 (defun gnus-summary-refer-article (message-id)
10828   "Fetch an article specified by MESSAGE-ID."
10829   (interactive "sMessage-ID: ")
10830   (when (and (stringp message-id)
10831              (not (zerop (length message-id))))
10832     ;; Construct the correct Message-ID if necessary.
10833     ;; Suggested by tale@pawl.rpi.edu.
10834     (unless (string-match "^<" message-id)
10835       (setq message-id (concat "<" message-id)))
10836     (unless (string-match ">$" message-id)
10837       (setq message-id (concat message-id ">")))
10838     (let ((header (car (gnus-gethash message-id
10839                                      gnus-newsgroup-dependencies))))
10840       (if header
10841           ;; The article is present in the buffer, to we just go to it.
10842           (gnus-summary-goto-article (mail-header-number header) nil t)
10843         ;; We fetch the article
10844         (let ((gnus-override-method 
10845                (and (gnus-news-group-p gnus-newsgroup-name)
10846                     gnus-refer-article-method))
10847               number)
10848           ;; Start the special refer-article method, if necessary.
10849           (when gnus-refer-article-method
10850             (gnus-check-server gnus-refer-article-method))
10851           ;; Fetch the header, and display the article.
10852           (if (setq number (gnus-summary-insert-subject message-id))
10853               (gnus-summary-select-article nil nil nil number)
10854             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
10855
10856 (defun gnus-summary-enter-digest-group (&optional force)
10857   "Enter a digest group based on the current article."
10858   (interactive "P")
10859   (gnus-set-global-variables)
10860   (gnus-summary-select-article)
10861   (let ((name (format "%s-%d"
10862                       (gnus-group-prefixed-name
10863                        gnus-newsgroup-name (list 'nndoc ""))
10864                       gnus-current-article))
10865         (ogroup gnus-newsgroup-name)
10866         (case-fold-search t)
10867         (buf (current-buffer))
10868         dig)
10869     (save-excursion
10870       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
10871       (insert-buffer-substring gnus-original-article-buffer)
10872       (narrow-to-region
10873        (goto-char (point-min))
10874        (or (search-forward "\n\n" nil t) (point)))
10875       (goto-char (point-min))
10876       (delete-matching-lines "^\\(Path\\):\\|^From ")
10877       (widen))
10878     (unwind-protect
10879         (if (gnus-group-read-ephemeral-group
10880              name `(nndoc ,name (nndoc-address
10881                                  ,(get-buffer dig))
10882                           (nndoc-article-type ,(if force 'digest 'guess))) t)
10883             ;; Make all postings to this group go to the parent group.
10884             (nconc (gnus-info-params (gnus-get-info name))
10885                    (list (cons 'to-group ogroup)))
10886           ;; Couldn't select this doc group.
10887           (switch-to-buffer buf)
10888           (gnus-set-global-variables)
10889           (gnus-configure-windows 'summary)
10890           (gnus-message 3 "Article couldn't be entered?"))
10891       (kill-buffer dig))))
10892
10893 (defun gnus-summary-isearch-article (&optional regexp-p)
10894   "Do incremental search forward on the current article.
10895 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
10896   (interactive "P")
10897   (gnus-set-global-variables)
10898   (gnus-summary-select-article)
10899   (gnus-configure-windows 'article)
10900   (gnus-eval-in-buffer-window
10901    gnus-article-buffer
10902    (goto-char (point-min))
10903    (isearch-forward regexp-p)))
10904
10905 (defun gnus-summary-search-article-forward (regexp &optional backward)
10906   "Search for an article containing REGEXP forward.
10907 If BACKWARD, search backward instead."
10908   (interactive
10909    (list (read-string
10910           (format "Search article %s (regexp%s): "
10911                   (if current-prefix-arg "backward" "forward")
10912                   (if gnus-last-search-regexp
10913                       (concat ", default " gnus-last-search-regexp)
10914                     "")))
10915          current-prefix-arg))
10916   (gnus-set-global-variables)
10917   (if (string-equal regexp "")
10918       (setq regexp (or gnus-last-search-regexp ""))
10919     (setq gnus-last-search-regexp regexp))
10920   (if (gnus-summary-search-article regexp backward)
10921       (gnus-article-set-window-start
10922        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
10923     (error "Search failed: \"%s\"" regexp)))
10924
10925 (defun gnus-summary-search-article-backward (regexp)
10926   "Search for an article containing REGEXP backward."
10927   (interactive
10928    (list (read-string
10929           (format "Search article backward (regexp%s): "
10930                   (if gnus-last-search-regexp
10931                       (concat ", default " gnus-last-search-regexp)
10932                     "")))))
10933   (gnus-summary-search-article-forward regexp 'backward))
10934
10935 (defun gnus-summary-search-article (regexp &optional backward)
10936   "Search for an article containing REGEXP.
10937 Optional argument BACKWARD means do search for backward.
10938 gnus-select-article-hook is not called during the search."
10939   (let ((gnus-select-article-hook nil)  ;Disable hook.
10940         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
10941         (re-search
10942          (if backward
10943              (function re-search-backward) (function re-search-forward)))
10944         (found nil)
10945         (last nil))
10946     ;; Hidden thread subtrees must be searched for ,too.
10947     (gnus-summary-show-all-threads)
10948     ;; First of all, search current article.
10949     ;; We don't want to read article again from NNTP server nor reset
10950     ;; current point.
10951     (gnus-summary-select-article)
10952     (gnus-message 9 "Searching article: %d..." gnus-current-article)
10953     (setq last gnus-current-article)
10954     (gnus-eval-in-buffer-window
10955      gnus-article-buffer
10956      (save-restriction
10957        (widen)
10958        ;; Begin search from current point.
10959        (setq found (funcall re-search regexp nil t))))
10960     ;; Then search next articles.
10961     (while (and (not found)
10962                 (gnus-summary-display-article
10963                  (if backward (gnus-summary-find-prev)
10964                    (gnus-summary-find-next))))
10965       (gnus-message 9 "Searching article: %d..." gnus-current-article)
10966       (gnus-eval-in-buffer-window
10967        gnus-article-buffer
10968        (save-restriction
10969          (widen)
10970          (goto-char (if backward (point-max) (point-min)))
10971          (setq found (funcall re-search regexp nil t)))))
10972     (message "")
10973     ;; Adjust article pointer.
10974     (or (eq last gnus-current-article)
10975         (setq gnus-last-article last))
10976     ;; Return T if found such article.
10977     found))
10978
10979 (defun gnus-summary-find-matching (header regexp &optional backward unread
10980                                           not-case-fold)
10981   "Return a list of all articles that match REGEXP on HEADER.
10982 The search stars on the current article and goes forwards unless
10983 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
10984 If UNREAD is non-nil, only unread articles will
10985 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
10986 in the comparisons."
10987   (let ((data (if (eq backward 'all) gnus-newsgroup-data
10988                 (gnus-data-find-list
10989                  (gnus-summary-article-number) (gnus-data-list backward))))
10990         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
10991         (case-fold-search (not not-case-fold))
10992         articles d)
10993     (or (fboundp (intern (concat "mail-header-" header)))
10994         (error "%s is not a valid header" header))
10995     (while data
10996       (setq d (car data))
10997       (and (or (not unread)             ; We want all articles...
10998                (gnus-data-unread-p d))  ; Or just unreads.
10999            (vectorp (gnus-data-header d)) ; It's not a pseudo.
11000            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
11001            (setq articles (cons (gnus-data-number d) articles))) ; Success!
11002       (setq data (cdr data)))
11003     (nreverse articles)))
11004
11005 (defun gnus-summary-execute-command (header regexp command &optional backward)
11006   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
11007 If HEADER is an empty string (or nil), the match is done on the entire
11008 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
11009   (interactive
11010    (list (let ((completion-ignore-case t))
11011            (completing-read
11012             "Header name: "
11013             (mapcar (lambda (string) (list string))
11014                     '("Number" "Subject" "From" "Lines" "Date"
11015                       "Message-ID" "Xref" "References" "Body"))
11016             nil 'require-match))
11017          (read-string "Regexp: ")
11018          (read-key-sequence "Command: ")
11019          current-prefix-arg))
11020   (when (equal header "Body")
11021     (setq header ""))
11022   (gnus-set-global-variables)
11023   ;; Hidden thread subtrees must be searched as well.
11024   (gnus-summary-show-all-threads)
11025   ;; We don't want to change current point nor window configuration.
11026   (save-excursion
11027     (save-window-excursion
11028       (gnus-message 6 "Executing %s..." (key-description command))
11029       ;; We'd like to execute COMMAND interactively so as to give arguments.
11030       (gnus-execute header regexp
11031                     `(lambda () (call-interactively ',(key-binding command)))
11032                     backward)
11033       (gnus-message 6 "Executing %s...done" (key-description command)))))
11034
11035 (defun gnus-summary-beginning-of-article ()
11036   "Scroll the article back to the beginning."
11037   (interactive)
11038   (gnus-set-global-variables)
11039   (gnus-summary-select-article)
11040   (gnus-configure-windows 'article)
11041   (gnus-eval-in-buffer-window
11042    gnus-article-buffer
11043    (widen)
11044    (goto-char (point-min))
11045    (and gnus-break-pages (gnus-narrow-to-page))))
11046
11047 (defun gnus-summary-end-of-article ()
11048   "Scroll to the end of the article."
11049   (interactive)
11050   (gnus-set-global-variables)
11051   (gnus-summary-select-article)
11052   (gnus-configure-windows 'article)
11053   (gnus-eval-in-buffer-window
11054    gnus-article-buffer
11055    (widen)
11056    (goto-char (point-max))
11057    (recenter -3)
11058    (and gnus-break-pages (gnus-narrow-to-page))))
11059
11060 (defun gnus-summary-show-article (&optional arg)
11061   "Force re-fetching of the current article.
11062 If ARG (the prefix) is non-nil, show the raw article without any
11063 article massaging functions being run."
11064   (interactive "P")
11065   (gnus-set-global-variables)
11066   (if (not arg)
11067       ;; Select the article the normal way.
11068       (gnus-summary-select-article nil 'force)
11069     ;; Bind the article treatment functions to nil.
11070     (let ((gnus-have-all-headers t)
11071           gnus-article-display-hook
11072           gnus-article-prepare-hook
11073           gnus-visual)
11074       (gnus-summary-select-article nil 'force)))
11075 ;  (gnus-configure-windows 'article)
11076   (gnus-summary-position-point))
11077
11078 (defun gnus-summary-verbose-headers (&optional arg)
11079   "Toggle permanent full header display.
11080 If ARG is a positive number, turn header display on.
11081 If ARG is a negative number, turn header display off."
11082   (interactive "P")
11083   (gnus-set-global-variables)
11084   (gnus-summary-toggle-header arg)
11085   (setq gnus-show-all-headers
11086         (cond ((or (not (numberp arg))
11087                    (zerop arg))
11088                (not gnus-show-all-headers))
11089               ((natnump arg)
11090                t))))
11091
11092 (defun gnus-summary-toggle-header (&optional arg)
11093   "Show the headers if they are hidden, or hide them if they are shown.
11094 If ARG is a positive number, show the entire header.
11095 If ARG is a negative number, hide the unwanted header lines."
11096   (interactive "P")
11097   (gnus-set-global-variables)
11098   (save-excursion
11099     (set-buffer gnus-article-buffer)
11100     (let* ((buffer-read-only nil)
11101            (inhibit-point-motion-hooks t)
11102            (hidden (text-property-any
11103                     (goto-char (point-min)) (search-forward "\n\n")
11104                     'invisible t))
11105            e)
11106       (goto-char (point-min))
11107       (when (search-forward "\n\n" nil t)
11108         (delete-region (point-min) (1- (point))))
11109       (goto-char (point-min))
11110       (save-excursion
11111         (set-buffer gnus-original-article-buffer)
11112         (goto-char (point-min))
11113         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11114       (insert-buffer-substring gnus-original-article-buffer 1 e)
11115       (let ((gnus-inhibit-hiding t))
11116         (run-hooks 'gnus-article-display-hook))
11117       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11118           (gnus-article-hide-headers)))))
11119
11120 (defun gnus-summary-show-all-headers ()
11121   "Make all header lines visible."
11122   (interactive)
11123   (gnus-set-global-variables)
11124   (gnus-article-show-all-headers))
11125
11126 (defun gnus-summary-toggle-mime (&optional arg)
11127   "Toggle MIME processing.
11128 If ARG is a positive number, turn MIME processing on."
11129   (interactive "P")
11130   (gnus-set-global-variables)
11131   (setq gnus-show-mime
11132         (if (null arg) (not gnus-show-mime)
11133           (> (prefix-numeric-value arg) 0)))
11134   (gnus-summary-select-article t 'force))
11135
11136 (defun gnus-summary-caesar-message (&optional arg)
11137   "Caesar rotate the current article by 13.
11138 The numerical prefix specifies how manu places to rotate each letter
11139 forward."
11140   (interactive "P")
11141   (gnus-set-global-variables)
11142   (gnus-summary-select-article)
11143   (let ((mail-header-separator ""))
11144     (gnus-eval-in-buffer-window
11145      gnus-article-buffer
11146      (save-restriction
11147        (widen)
11148        (let ((start (window-start)))
11149          (news-caesar-buffer-body arg)
11150          (set-window-start (get-buffer-window (current-buffer)) start))))))
11151
11152 (defun gnus-summary-stop-page-breaking ()
11153   "Stop page breaking in the current article."
11154   (interactive)
11155   (gnus-set-global-variables)
11156   (gnus-summary-select-article)
11157   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
11158
11159 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11160   "Move the current article to a different newsgroup.
11161 If N is a positive number, move the N next articles.
11162 If N is a negative number, move the N previous articles.
11163 If N is nil and any articles have been marked with the process mark,
11164 move those articles instead.
11165 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11166 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11167 re-spool using this method.
11168
11169 For this function to work, both the current newsgroup and the
11170 newsgroup that you want to move to have to support the `request-move'
11171 and `request-accept' functions."
11172   (interactive "P")
11173   (unless action (setq action 'move))
11174   (gnus-set-global-variables)
11175   ;; Check whether the source group supports the required functions.
11176   (cond ((and (eq action 'move)
11177               (not (gnus-check-backend-function
11178                     'request-move-article gnus-newsgroup-name)))
11179          (error "The current group does not support article moving"))
11180         ((and (eq action 'crosspost)
11181               (not (gnus-check-backend-function
11182                     'request-replace-article gnus-newsgroup-name)))
11183          (error "The current group does not support article editing")))
11184   (let ((articles (gnus-summary-work-articles n))
11185         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11186         (names '((move "move" "Moving")
11187                  (copy "copy" "Copying")
11188                  (crosspost "crosspost" "Crossposting")))
11189         (copy-buf (save-excursion
11190                     (nnheader-set-temp-buffer " *copy article*")))
11191         art-group to-method new-xref article to-groups)
11192     (unless (assq action names)
11193       (error "Unknown action %s" action))
11194     ;; Read the newsgroup name.
11195     (when (and (not to-newsgroup)
11196                (not select-method))
11197       (setq to-newsgroup
11198             (gnus-read-move-group-name
11199              (cadr (assq action names))
11200              gnus-current-move-group articles prefix))
11201       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11202     (setq to-method (or select-method 
11203                         (gnus-find-method-for-group to-newsgroup)))
11204     ;; Check the method we are to move this article to...
11205     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11206         (error "%s does not support article copying" (car to-method)))
11207     (or (gnus-check-server to-method)
11208         (error "Can't open server %s" (car to-method)))
11209     (gnus-message 6 "%s to %s: %s..."
11210                   (caddr (assq action names))
11211                   (or (car select-method) to-newsgroup) articles)
11212     (while articles
11213       (setq article (pop articles))
11214       (setq
11215        art-group
11216        (cond
11217         ;; Move the article.
11218         ((eq action 'move)
11219          (gnus-request-move-article
11220           article                       ; Article to move
11221           gnus-newsgroup-name           ; From newsgrouo
11222           (nth 1 (gnus-find-method-for-group
11223                   gnus-newsgroup-name)) ; Server
11224           (list 'gnus-request-accept-article
11225                 to-newsgroup (list 'quote select-method)
11226                 (not articles))         ; Accept form
11227           (not articles)))              ; Only save nov last time
11228         ;; Copy the article.
11229         ((eq action 'copy)
11230          (save-excursion
11231            (set-buffer copy-buf)
11232            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11233            (gnus-request-accept-article
11234             to-newsgroup select-method (not articles))))
11235         ;; Crosspost the article.
11236         ((eq action 'crosspost)
11237          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11238            (setq new-xref (concat gnus-newsgroup-name ":" article))
11239            (if (and xref (not (string= xref "")))
11240                (progn
11241                  (when (string-match "^Xref: " xref)
11242                    (setq xref (substring xref (match-end 0))))
11243                  (setq new-xref (concat xref " " new-xref)))
11244              (setq new-xref (concat (system-name) " " new-xref)))
11245            (save-excursion
11246              (set-buffer copy-buf)
11247              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11248              (nnheader-replace-header "xref" new-xref)
11249              (gnus-request-accept-article
11250               to-newsgroup select-method (not articles)))))))
11251       (if (not art-group)
11252           (gnus-message 1 "Couldn't %s article %s"
11253                         (cadr (assq action names)) article)
11254         (let* ((entry
11255                 (or
11256                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11257                  (gnus-gethash
11258                   (gnus-group-prefixed-name
11259                    (car art-group)
11260                    (or select-method 
11261                        (gnus-find-method-for-group to-newsgroup)))
11262                   gnus-newsrc-hashtb)))
11263                (info (nth 2 entry))
11264                (to-group (gnus-info-group info)))
11265           ;; Update the group that has been moved to.
11266           (when (and info
11267                      (memq action '(move copy)))
11268             (unless (member to-group to-groups)
11269               (push to-group to-groups))
11270
11271             (unless (memq article gnus-newsgroup-unreads)
11272               (gnus-info-set-read
11273                info (gnus-add-to-range (gnus-info-read info)
11274                                        (list (cdr art-group)))))
11275
11276             ;; Copy any marks over to the new group.
11277             (let ((marks gnus-article-mark-lists)
11278                   (to-article (cdr art-group)))
11279
11280               ;; See whether the article is to be put in the cache.
11281               (when gnus-use-cache
11282                 (gnus-cache-possibly-enter-article
11283                  to-group to-article
11284                  (let ((header (copy-sequence
11285                                 (gnus-summary-article-header article))))
11286                    (mail-header-set-number header to-article)
11287                    header)
11288                  (memq article gnus-newsgroup-marked)
11289                  (memq article gnus-newsgroup-dormant)
11290                  (memq article gnus-newsgroup-unreads)))
11291
11292               (while marks
11293                 (when (memq article (symbol-value
11294                                      (intern (format "gnus-newsgroup-%s"
11295                                                      (caar marks)))))
11296                   ;; If the other group is the same as this group,
11297                   ;; then we have to add the mark to the list.
11298                   (when (equal to-group gnus-newsgroup-name)
11299                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
11300                          (cons to-article
11301                                (symbol-value
11302                                 (intern (format "gnus-newsgroup-%s"
11303                                                 (caar marks)))))))
11304                   ;; Copy mark to other group.
11305                   (gnus-add-marked-articles
11306                    to-group (cdar marks) (list to-article) info))
11307                 (setq marks (cdr marks)))))
11308
11309           ;; Update the Xref header in this article to point to
11310           ;; the new crossposted article we have just created.
11311           (when (eq action 'crosspost)
11312             (save-excursion
11313               (set-buffer copy-buf)
11314               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11315               (nnheader-replace-header
11316                "xref" (concat new-xref " " (gnus-group-prefixed-name
11317                                             (car art-group) to-method)
11318                               ":" (cdr art-group)))
11319               (gnus-request-replace-article
11320                article gnus-newsgroup-name (current-buffer)))))
11321
11322         (gnus-summary-goto-subject article)
11323         (when (eq action 'move)
11324           (gnus-summary-mark-article article gnus-canceled-mark)))
11325       (gnus-summary-remove-process-mark article))
11326     ;; Re-activate all groups that have been moved to.
11327     (while to-groups
11328       (gnus-activate-group (pop to-groups)))
11329     
11330     (gnus-kill-buffer copy-buf)
11331     (gnus-summary-position-point)
11332     (gnus-set-mode-line 'summary)))
11333
11334 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11335   "Move the current article to a different newsgroup.
11336 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11337 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11338 re-spool using this method."
11339   (interactive "P")
11340   (gnus-summary-move-article n nil select-method 'copy))
11341
11342 (defun gnus-summary-crosspost-article (&optional n)
11343   "Crosspost the current article to some other group."
11344   (interactive "P")
11345   (gnus-summary-move-article n nil nil 'crosspost))
11346
11347 (defun gnus-summary-respool-article (&optional n method)
11348   "Respool the current article.
11349 The article will be squeezed through the mail spooling process again,
11350 which means that it will be put in some mail newsgroup or other
11351 depending on `nnmail-split-methods'.
11352 If N is a positive number, respool the N next articles.
11353 If N is a negative number, respool the N previous articles.
11354 If N is nil and any articles have been marked with the process mark,
11355 respool those articles instead.
11356
11357 Respooling can be done both from mail groups and \"real\" newsgroups.
11358 In the former case, the articles in question will be moved from the
11359 current group into whatever groups they are destined to.  In the
11360 latter case, they will be copied into the relevant groups."
11361   (interactive 
11362    (list current-prefix-arg
11363          (let* ((methods (gnus-methods-using 'respool))
11364                 (methname
11365                  (symbol-name (car (gnus-find-method-for-group
11366                                     gnus-newsgroup-name))))
11367                 (method
11368                  (completing-read
11369                   "What backend do you want to use when respooling? "
11370                   methods nil t (cons methname 0)))
11371                 ms)
11372            (cond
11373             ((zerop (length (setq ms (gnus-servers-using-backend method))))
11374              (list (intern method) ""))
11375             ((= 1 (length ms))
11376              (car ms))
11377             (t
11378              (cdr (completing-read 
11379                    "Server name: "
11380                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
11381   (gnus-set-global-variables)
11382   (unless method
11383     (error "No method given for respooling"))
11384   (if (assoc (symbol-name
11385               (car (gnus-find-method-for-group gnus-newsgroup-name)))
11386              (gnus-methods-using 'respool))
11387       (gnus-summary-move-article n nil method)
11388     (gnus-summary-copy-article n nil method)))
11389
11390 (defun gnus-summary-import-article (file)
11391   "Import a random file into a mail newsgroup."
11392   (interactive "fImport file: ")
11393   (gnus-set-global-variables)
11394   (let ((group gnus-newsgroup-name)
11395         (now (current-time))
11396         atts lines)
11397     (or (gnus-check-backend-function 'request-accept-article group)
11398         (error "%s does not support article importing" group))
11399     (or (file-readable-p file)
11400         (not (file-regular-p file))
11401         (error "Can't read %s" file))
11402     (save-excursion
11403       (set-buffer (get-buffer-create " *import file*"))
11404       (buffer-disable-undo (current-buffer))
11405       (erase-buffer)
11406       (insert-file-contents file)
11407       (goto-char (point-min))
11408       (unless (nnheader-article-p)
11409         ;; This doesn't look like an article, so we fudge some headers.
11410         (setq atts (file-attributes file)
11411               lines (count-lines (point-min) (point-max)))
11412         (insert "From: " (read-string "From: ") "\n"
11413                 "Subject: " (read-string "Subject: ") "\n"
11414                 "Date: " (timezone-make-date-arpa-standard
11415                           (current-time-string (nth 5 atts))
11416                           (current-time-zone now)
11417                           (current-time-zone now)) "\n"
11418                 "Message-ID: " (gnus-inews-message-id) "\n"
11419                 "Lines: " (int-to-string lines) "\n"
11420                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11421       (gnus-request-accept-article group nil t)
11422       (kill-buffer (current-buffer)))))
11423
11424 (defun gnus-summary-expire-articles ()
11425   "Expire all articles that are marked as expirable in the current group."
11426   (interactive)
11427   (gnus-set-global-variables)
11428   (when (gnus-check-backend-function
11429          'request-expire-articles gnus-newsgroup-name)
11430     ;; This backend supports expiry.
11431     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11432            (expirable (if total
11433                           (gnus-list-of-read-articles gnus-newsgroup-name)
11434                         (setq gnus-newsgroup-expirable
11435                               (sort gnus-newsgroup-expirable '<))))
11436            (expiry-wait (gnus-group-get-parameter
11437                          gnus-newsgroup-name 'expiry-wait))
11438            es)
11439       (when expirable
11440         ;; There are expirable articles in this group, so we run them
11441         ;; through the expiry process.
11442         (gnus-message 6 "Expiring articles...")
11443         ;; The list of articles that weren't expired is returned.
11444         (if expiry-wait
11445             (let ((nnmail-expiry-wait-function nil)
11446                   (nnmail-expiry-wait expiry-wait))
11447               (setq es (gnus-request-expire-articles
11448                         expirable gnus-newsgroup-name)))
11449           (setq es (gnus-request-expire-articles
11450                     expirable gnus-newsgroup-name)))
11451         (or total (setq gnus-newsgroup-expirable es))
11452         ;; We go through the old list of expirable, and mark all
11453         ;; really expired articles as nonexistent.
11454         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11455           (let ((gnus-use-cache nil))
11456             (while expirable
11457               (unless (memq (car expirable) es)
11458                 (when (gnus-data-find (car expirable))
11459                   (gnus-summary-mark-article
11460                    (car expirable) gnus-canceled-mark)))
11461               (setq expirable (cdr expirable)))))
11462         (gnus-message 6 "Expiring articles...done")))))
11463
11464 (defun gnus-summary-expire-articles-now ()
11465   "Expunge all expirable articles in the current group.
11466 This means that *all* articles that are marked as expirable will be
11467 deleted forever, right now."
11468   (interactive)
11469   (gnus-set-global-variables)
11470   (or gnus-expert-user
11471       (gnus-y-or-n-p
11472        "Are you really, really, really sure you want to expunge? ")
11473       (error "Phew!"))
11474   (let ((nnmail-expiry-wait 'immediate)
11475         (nnmail-expiry-wait-function nil))
11476     (gnus-summary-expire-articles)))
11477
11478 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11479 (defun gnus-summary-delete-article (&optional n)
11480   "Delete the N next (mail) articles.
11481 This command actually deletes articles.  This is not a marking
11482 command.  The article will disappear forever from your life, never to
11483 return.
11484 If N is negative, delete backwards.
11485 If N is nil and articles have been marked with the process mark,
11486 delete these instead."
11487   (interactive "P")
11488   (gnus-set-global-variables)
11489   (or (gnus-check-backend-function 'request-expire-articles
11490                                    gnus-newsgroup-name)
11491       (error "The current newsgroup does not support article deletion."))
11492   ;; Compute the list of articles to delete.
11493   (let ((articles (gnus-summary-work-articles n))
11494         not-deleted)
11495     (if (and gnus-novice-user
11496              (not (gnus-y-or-n-p
11497                    (format "Do you really want to delete %s forever? "
11498                            (if (> (length articles) 1) "these articles"
11499                              "this article")))))
11500         ()
11501       ;; Delete the articles.
11502       (setq not-deleted (gnus-request-expire-articles
11503                          articles gnus-newsgroup-name 'force))
11504       (while articles
11505         (gnus-summary-remove-process-mark (car articles))
11506         ;; The backend might not have been able to delete the article
11507         ;; after all.
11508         (or (memq (car articles) not-deleted)
11509             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11510         (setq articles (cdr articles))))
11511     (gnus-summary-position-point)
11512     (gnus-set-mode-line 'summary)
11513     not-deleted))
11514
11515 (defun gnus-summary-edit-article (&optional force)
11516   "Enter into a buffer and edit the current article.
11517 This will have permanent effect only in mail groups.
11518 If FORCE is non-nil, allow editing of articles even in read-only
11519 groups."
11520   (interactive "P")
11521   (save-excursion
11522     (set-buffer gnus-summary-buffer)
11523     (gnus-set-global-variables)
11524     (when (and (not force)
11525                (gnus-group-read-only-p))
11526       (error "The current newsgroup does not support article editing."))
11527     (gnus-summary-select-article t nil t)
11528     (gnus-configure-windows 'article)
11529     (select-window (get-buffer-window gnus-article-buffer))
11530     (gnus-message 6 "C-c C-c to end edits")
11531     (setq buffer-read-only nil)
11532     (text-mode)
11533     (use-local-map (copy-keymap (current-local-map)))
11534     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11535     (buffer-enable-undo)
11536     (widen)
11537     (goto-char (point-min))
11538     (search-forward "\n\n" nil t)))
11539
11540 (defun gnus-summary-edit-article-done ()
11541   "Make edits to the current article permanent."
11542   (interactive)
11543   (if (gnus-group-read-only-p)
11544       (progn
11545         (gnus-summary-edit-article-postpone)
11546         (gnus-message
11547          1 "The current newsgroup does not support article editing.")
11548         (ding))
11549     (let ((buf (format "%s" (buffer-string))))
11550       (erase-buffer)
11551       (insert buf)
11552       (if (not (gnus-request-replace-article
11553                 (cdr gnus-article-current) (car gnus-article-current)
11554                 (current-buffer)))
11555           (error "Couldn't replace article.")
11556         (gnus-article-mode)
11557         (use-local-map gnus-article-mode-map)
11558         (setq buffer-read-only t)
11559         (buffer-disable-undo (current-buffer))
11560         (gnus-configure-windows 'summary)
11561         (gnus-summary-update-article (cdr gnus-article-current))
11562         (when gnus-use-cache
11563           (gnus-cache-update-article 
11564            (cdr gnus-article-current) (car gnus-article-current))))
11565       (run-hooks 'gnus-article-display-hook)
11566       (and (gnus-visual-p 'summary-highlight 'highlight)
11567            (run-hooks 'gnus-visual-mark-article-hook)))))
11568
11569 (defun gnus-summary-edit-article-postpone ()
11570   "Postpone changes to the current article."
11571   (interactive)
11572   (gnus-article-mode)
11573   (use-local-map gnus-article-mode-map)
11574   (setq buffer-read-only t)
11575   (buffer-disable-undo (current-buffer))
11576   (gnus-configure-windows 'summary)
11577   (and (gnus-visual-p 'summary-highlight 'highlight)
11578        (run-hooks 'gnus-visual-mark-article-hook)))
11579
11580 (defun gnus-summary-respool-query ()
11581   "Query where the respool algorithm would put this article."
11582   (interactive)
11583   (gnus-set-global-variables)
11584   (gnus-summary-select-article)
11585   (save-excursion
11586     (set-buffer gnus-article-buffer)
11587     (save-restriction
11588       (goto-char (point-min))
11589       (search-forward "\n\n")
11590       (narrow-to-region (point-min) (point))
11591       (pp-eval-expression
11592        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11593
11594 ;; Summary score commands.
11595
11596 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
11597
11598 (defun gnus-summary-raise-score (n)
11599   "Raise the score of the current article by N."
11600   (interactive "p")
11601   (gnus-set-global-variables)
11602   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
11603
11604 (defun gnus-summary-set-score (n)
11605   "Set the score of the current article to N."
11606   (interactive "p")
11607   (gnus-set-global-variables)
11608   (save-excursion
11609     (gnus-summary-show-thread)
11610     (let ((buffer-read-only nil))
11611       ;; Set score.
11612       (gnus-summary-update-mark
11613        (if (= n (or gnus-summary-default-score 0)) ? 
11614          (if (< n (or gnus-summary-default-score 0))
11615              gnus-score-below-mark gnus-score-over-mark)) 'score))
11616     (let* ((article (gnus-summary-article-number))
11617            (score (assq article gnus-newsgroup-scored)))
11618       (if score (setcdr score n)
11619         (setq gnus-newsgroup-scored
11620               (cons (cons article n) gnus-newsgroup-scored))))
11621     (gnus-summary-update-line)))
11622
11623 (defun gnus-summary-current-score ()
11624   "Return the score of the current article."
11625   (interactive)
11626   (gnus-set-global-variables)
11627   (message "%s" (gnus-summary-article-score)))
11628
11629 ;; Summary marking commands.
11630
11631 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11632   "Mark articles which has the same subject as read, and then select the next.
11633 If UNMARK is positive, remove any kind of mark.
11634 If UNMARK is negative, tick articles."
11635   (interactive "P")
11636   (gnus-set-global-variables)
11637   (if unmark
11638       (setq unmark (prefix-numeric-value unmark)))
11639   (let ((count
11640          (gnus-summary-mark-same-subject
11641           (gnus-summary-article-subject) unmark)))
11642     ;; Select next unread article.  If auto-select-same mode, should
11643     ;; select the first unread article.
11644     (gnus-summary-next-article t (and gnus-auto-select-same
11645                                       (gnus-summary-article-subject)))
11646     (gnus-message 7 "%d article%s marked as %s"
11647                   count (if (= count 1) " is" "s are")
11648                   (if unmark "unread" "read"))))
11649
11650 (defun gnus-summary-kill-same-subject (&optional unmark)
11651   "Mark articles which has the same subject as read.
11652 If UNMARK is positive, remove any kind of mark.
11653 If UNMARK is negative, tick articles."
11654   (interactive "P")
11655   (gnus-set-global-variables)
11656   (if unmark
11657       (setq unmark (prefix-numeric-value unmark)))
11658   (let ((count
11659          (gnus-summary-mark-same-subject
11660           (gnus-summary-article-subject) unmark)))
11661     ;; If marked as read, go to next unread subject.
11662     (if (null unmark)
11663         ;; Go to next unread subject.
11664         (gnus-summary-next-subject 1 t))
11665     (gnus-message 7 "%d articles are marked as %s"
11666                   count (if unmark "unread" "read"))))
11667
11668 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11669   "Mark articles with same SUBJECT as read, and return marked number.
11670 If optional argument UNMARK is positive, remove any kinds of marks.
11671 If optional argument UNMARK is negative, mark articles as unread instead."
11672   (let ((count 1))
11673     (save-excursion
11674       (cond
11675        ((null unmark)                   ; Mark as read.
11676         (while (and
11677                 (progn
11678                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11679                   (gnus-summary-show-thread) t)
11680                 (gnus-summary-find-subject subject))
11681           (setq count (1+ count))))
11682        ((> unmark 0)                    ; Tick.
11683         (while (and
11684                 (progn
11685                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11686                   (gnus-summary-show-thread) t)
11687                 (gnus-summary-find-subject subject))
11688           (setq count (1+ count))))
11689        (t                               ; Mark as unread.
11690         (while (and
11691                 (progn
11692                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11693                   (gnus-summary-show-thread) t)
11694                 (gnus-summary-find-subject subject))
11695           (setq count (1+ count)))))
11696       (gnus-set-mode-line 'summary)
11697       ;; Return the number of marked articles.
11698       count)))
11699
11700 (defun gnus-summary-mark-as-processable (n &optional unmark)
11701   "Set the process mark on the next N articles.
11702 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11703 the process mark instead.  The difference between N and the actual
11704 number of articles marked is returned."
11705   (interactive "p")
11706   (gnus-set-global-variables)
11707   (let ((backward (< n 0))
11708         (n (abs n)))
11709     (while (and
11710             (> n 0)
11711             (if unmark
11712                 (gnus-summary-remove-process-mark
11713                  (gnus-summary-article-number))
11714               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11715             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11716       (setq n (1- n)))
11717     (if (/= 0 n) (gnus-message 7 "No more articles"))
11718     (gnus-summary-recenter)
11719     (gnus-summary-position-point)
11720     n))
11721
11722 (defun gnus-summary-unmark-as-processable (n)
11723   "Remove the process mark from the next N articles.
11724 If N is negative, mark backward instead.  The difference between N and
11725 the actual number of articles marked is returned."
11726   (interactive "p")
11727   (gnus-set-global-variables)
11728   (gnus-summary-mark-as-processable n t))
11729
11730 (defun gnus-summary-unmark-all-processable ()
11731   "Remove the process mark from all articles."
11732   (interactive)
11733   (gnus-set-global-variables)
11734   (save-excursion
11735     (while gnus-newsgroup-processable
11736       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11737   (gnus-summary-position-point))
11738
11739 (defun gnus-summary-mark-as-expirable (n)
11740   "Mark N articles forward as expirable.
11741 If N is negative, mark backward instead.  The difference between N and
11742 the actual number of articles marked is returned."
11743   (interactive "p")
11744   (gnus-set-global-variables)
11745   (gnus-summary-mark-forward n gnus-expirable-mark))
11746
11747 (defun gnus-summary-mark-article-as-replied (article)
11748   "Mark ARTICLE replied and update the summary line."
11749   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11750   (let ((buffer-read-only nil))
11751     (when (gnus-summary-goto-subject article)
11752       (gnus-summary-update-secondary-mark article))))
11753
11754 (defun gnus-summary-set-bookmark (article)
11755   "Set a bookmark in current article."
11756   (interactive (list (gnus-summary-article-number)))
11757   (gnus-set-global-variables)
11758   (if (or (not (get-buffer gnus-article-buffer))
11759           (not gnus-current-article)
11760           (not gnus-article-current)
11761           (not (equal gnus-newsgroup-name (car gnus-article-current))))
11762       (error "No current article selected"))
11763   ;; Remove old bookmark, if one exists.
11764   (let ((old (assq article gnus-newsgroup-bookmarks)))
11765     (if old (setq gnus-newsgroup-bookmarks
11766                   (delq old gnus-newsgroup-bookmarks))))
11767   ;; Set the new bookmark, which is on the form
11768   ;; (article-number . line-number-in-body).
11769   (setq gnus-newsgroup-bookmarks
11770         (cons
11771          (cons article
11772                (save-excursion
11773                  (set-buffer gnus-article-buffer)
11774                  (count-lines
11775                   (min (point)
11776                        (save-excursion
11777                          (goto-char (point-min))
11778                          (search-forward "\n\n" nil t)
11779                          (point)))
11780                   (point))))
11781          gnus-newsgroup-bookmarks))
11782   (gnus-message 6 "A bookmark has been added to the current article."))
11783
11784 (defun gnus-summary-remove-bookmark (article)
11785   "Remove the bookmark from the current article."
11786   (interactive (list (gnus-summary-article-number)))
11787   (gnus-set-global-variables)
11788   ;; Remove old bookmark, if one exists.
11789   (let ((old (assq article gnus-newsgroup-bookmarks)))
11790     (if old
11791         (progn
11792           (setq gnus-newsgroup-bookmarks
11793                 (delq old gnus-newsgroup-bookmarks))
11794           (gnus-message 6 "Removed bookmark."))
11795       (gnus-message 6 "No bookmark in current article."))))
11796
11797 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11798 (defun gnus-summary-mark-as-dormant (n)
11799   "Mark N articles forward as dormant.
11800 If N is negative, mark backward instead.  The difference between N and
11801 the actual number of articles marked is returned."
11802   (interactive "p")
11803   (gnus-set-global-variables)
11804   (gnus-summary-mark-forward n gnus-dormant-mark))
11805
11806 (defun gnus-summary-set-process-mark (article)
11807   "Set the process mark on ARTICLE and update the summary line."
11808   (setq gnus-newsgroup-processable
11809         (cons article
11810               (delq article gnus-newsgroup-processable)))
11811   (when (gnus-summary-goto-subject article)
11812     (gnus-summary-show-thread)
11813     (gnus-summary-update-secondary-mark article)))
11814
11815 (defun gnus-summary-remove-process-mark (article)
11816   "Remove the process mark from ARTICLE and update the summary line."
11817   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
11818   (when (gnus-summary-goto-subject article)
11819     (gnus-summary-show-thread)
11820     (gnus-summary-update-secondary-mark article)))
11821
11822 (defun gnus-summary-set-saved-mark (article)
11823   "Set the process mark on ARTICLE and update the summary line."
11824   (push article gnus-newsgroup-saved)
11825   (when (gnus-summary-goto-subject article)
11826     (gnus-summary-update-secondary-mark article)))
11827
11828 (defun gnus-summary-mark-forward (n &optional mark no-expire)
11829   "Mark N articles as read forwards.
11830 If N is negative, mark backwards instead.
11831 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
11832 marked as unread.
11833 The difference between N and the actual number of articles marked is
11834 returned."
11835   (interactive "p")
11836   (gnus-set-global-variables)
11837   (let ((backward (< n 0))
11838         (gnus-summary-goto-unread
11839          (and gnus-summary-goto-unread
11840               (not (eq gnus-summary-goto-unread 'never))
11841               (not (memq mark (list gnus-unread-mark
11842                                     gnus-ticked-mark gnus-dormant-mark)))))
11843         (n (abs n))
11844         (mark (or mark gnus-del-mark)))
11845     (while (and (> n 0)
11846                 (gnus-summary-mark-article nil mark no-expire)
11847                 (zerop (gnus-summary-next-subject
11848                         (if backward -1 1)
11849                         (and gnus-summary-goto-unread
11850                              (not (eq gnus-summary-goto-unread 'never)))
11851                         t)))
11852       (setq n (1- n)))
11853     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11854     (gnus-summary-recenter)
11855     (gnus-summary-position-point)
11856     (gnus-set-mode-line 'summary)
11857     n))
11858
11859 (defun gnus-summary-mark-article-as-read (mark)
11860   "Mark the current article quickly as read with MARK."
11861   (let ((article (gnus-summary-article-number)))
11862     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11863     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11864     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11865     (setq gnus-newsgroup-reads
11866           (cons (cons article mark) gnus-newsgroup-reads))
11867     ;; Possibly remove from cache, if that is used.
11868     (and gnus-use-cache (gnus-cache-enter-remove-article article))
11869     ;; Allow the backend to change the mark.
11870     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
11871     ;; Check for auto-expiry.
11872     (when (and gnus-newsgroup-auto-expire
11873                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11874                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11875                    (= mark gnus-ancient-mark)
11876                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
11877       (setq mark gnus-expirable-mark)
11878       (push article gnus-newsgroup-expirable))
11879     ;; Set the mark in the buffer.
11880     (gnus-summary-update-mark mark 'unread)
11881     t))
11882
11883 (defun gnus-summary-mark-article-as-unread (mark)
11884   "Mark the current article quickly as unread with MARK."
11885   (let ((article (gnus-summary-article-number)))
11886     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11887     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11888     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11889     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
11890     (cond ((= mark gnus-ticked-mark)
11891            (push article gnus-newsgroup-marked))
11892           ((= mark gnus-dormant-mark)
11893            (push article gnus-newsgroup-dormant))
11894           (t
11895            (push article gnus-newsgroup-unreads)))
11896     (setq gnus-newsgroup-reads
11897           (delq (assq article gnus-newsgroup-reads)
11898                 gnus-newsgroup-reads))
11899
11900     ;; See whether the article is to be put in the cache.
11901     (and gnus-use-cache
11902          (vectorp (gnus-summary-article-header article))
11903          (save-excursion
11904            (gnus-cache-possibly-enter-article
11905             gnus-newsgroup-name article
11906             (gnus-summary-article-header article)
11907             (= mark gnus-ticked-mark)
11908             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11909
11910     ;; Fix the mark.
11911     (gnus-summary-update-mark mark 'unread)
11912     t))
11913
11914 (defun gnus-summary-mark-article (&optional article mark no-expire)
11915   "Mark ARTICLE with MARK.  MARK can be any character.
11916 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
11917 `??' (dormant) and `?E' (expirable).
11918 If MARK is nil, then the default character `?D' is used.
11919 If ARTICLE is nil, then the article on the current line will be
11920 marked."
11921   ;; The mark might be a string.
11922   (and (stringp mark)
11923        (setq mark (aref mark 0)))
11924   ;; If no mark is given, then we check auto-expiring.
11925   (and (not no-expire)
11926        gnus-newsgroup-auto-expire
11927        (or (not mark)
11928            (and (numberp mark)
11929                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11930                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11931                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
11932        (setq mark gnus-expirable-mark))
11933   (let* ((mark (or mark gnus-del-mark))
11934          (article (or article (gnus-summary-article-number))))
11935     (or article (error "No article on current line"))
11936     (if (or (= mark gnus-unread-mark)
11937             (= mark gnus-ticked-mark)
11938             (= mark gnus-dormant-mark))
11939         (gnus-mark-article-as-unread article mark)
11940       (gnus-mark-article-as-read article mark))
11941
11942     ;; See whether the article is to be put in the cache.
11943     (and gnus-use-cache
11944          (not (= mark gnus-canceled-mark))
11945          (vectorp (gnus-summary-article-header article))
11946          (save-excursion
11947            (gnus-cache-possibly-enter-article
11948             gnus-newsgroup-name article
11949             (gnus-summary-article-header article)
11950             (= mark gnus-ticked-mark)
11951             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11952
11953     (if (gnus-summary-goto-subject article nil t)
11954         (let ((buffer-read-only nil))
11955           (gnus-summary-show-thread)
11956           ;; Fix the mark.
11957           (gnus-summary-update-mark mark 'unread)
11958           t))))
11959
11960 (defun gnus-summary-update-secondary-mark (article)
11961   "Update the secondary (read, process, cache) mark."
11962   (gnus-summary-update-mark
11963    (cond ((memq article gnus-newsgroup-processable)
11964           gnus-process-mark)
11965          ((memq article gnus-newsgroup-cached)
11966           gnus-cached-mark)
11967          ((memq article gnus-newsgroup-replied)
11968           gnus-replied-mark)
11969          ((memq article gnus-newsgroup-saved)
11970           gnus-saved-mark)
11971          (t gnus-unread-mark))
11972    'replied)
11973   (when (gnus-visual-p 'summary-highlight 'highlight)
11974     (run-hooks 'gnus-summary-update-hook))
11975   t)
11976
11977 (defun gnus-summary-update-mark (mark type)
11978   (beginning-of-line)
11979   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11980         (buffer-read-only nil))
11981     (when forward
11982       ;; Go to the right position on the line.
11983       (forward-char forward)
11984       ;; Replace the old mark with the new mark.
11985       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
11986       ;; Optionally update the marks by some user rule.
11987       (when (eq type 'unread)
11988         (gnus-data-set-mark
11989          (gnus-data-find (gnus-summary-article-number)) mark)
11990         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
11991
11992 (defun gnus-mark-article-as-read (article &optional mark)
11993   "Enter ARTICLE in the pertinent lists and remove it from others."
11994   ;; Make the article expirable.
11995   (let ((mark (or mark gnus-del-mark)))
11996     (if (= mark gnus-expirable-mark)
11997         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
11998       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
11999     ;; Remove from unread and marked lists.
12000     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12001     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12002     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12003     (push (cons article mark) gnus-newsgroup-reads)
12004     ;; Possibly remove from cache, if that is used.
12005     (when gnus-use-cache
12006       (gnus-cache-enter-remove-article article))))
12007
12008 (defun gnus-mark-article-as-unread (article &optional mark)
12009   "Enter ARTICLE in the pertinent lists and remove it from others."
12010   (let ((mark (or mark gnus-ticked-mark)))
12011     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12012     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12013     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12014     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12015     (cond ((= mark gnus-ticked-mark)
12016            (push article gnus-newsgroup-marked))
12017           ((= mark gnus-dormant-mark)
12018            (push article gnus-newsgroup-dormant))
12019           (t
12020            (push article gnus-newsgroup-unreads)))
12021     (setq gnus-newsgroup-reads
12022           (delq (assq article gnus-newsgroup-reads)
12023                 gnus-newsgroup-reads))))
12024
12025 (defalias 'gnus-summary-mark-as-unread-forward
12026   'gnus-summary-tick-article-forward)
12027 (make-obsolete 'gnus-summary-mark-as-unread-forward
12028                'gnus-summary-tick-article-forward)
12029 (defun gnus-summary-tick-article-forward (n)
12030   "Tick N articles forwards.
12031 If N is negative, tick backwards instead.
12032 The difference between N and the number of articles ticked is returned."
12033   (interactive "p")
12034   (gnus-summary-mark-forward n gnus-ticked-mark))
12035
12036 (defalias 'gnus-summary-mark-as-unread-backward
12037   'gnus-summary-tick-article-backward)
12038 (make-obsolete 'gnus-summary-mark-as-unread-backward
12039                'gnus-summary-tick-article-backward)
12040 (defun gnus-summary-tick-article-backward (n)
12041   "Tick N articles backwards.
12042 The difference between N and the number of articles ticked is returned."
12043   (interactive "p")
12044   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
12045
12046 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12047 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12048 (defun gnus-summary-tick-article (&optional article clear-mark)
12049   "Mark current article as unread.
12050 Optional 1st argument ARTICLE specifies article number to be marked as unread.
12051 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
12052   (interactive)
12053   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
12054                                        gnus-ticked-mark)))
12055
12056 (defun gnus-summary-mark-as-read-forward (n)
12057   "Mark N articles as read forwards.
12058 If N is negative, mark backwards instead.
12059 The difference between N and the actual number of articles marked is
12060 returned."
12061   (interactive "p")
12062   (gnus-summary-mark-forward n gnus-del-mark t))
12063
12064 (defun gnus-summary-mark-as-read-backward (n)
12065   "Mark the N articles as read backwards.
12066 The difference between N and the actual number of articles marked is
12067 returned."
12068   (interactive "p")
12069   (gnus-summary-mark-forward (- n) gnus-del-mark t))
12070
12071 (defun gnus-summary-mark-as-read (&optional article mark)
12072   "Mark current article as read.
12073 ARTICLE specifies the article to be marked as read.
12074 MARK specifies a string to be inserted at the beginning of the line."
12075   (gnus-summary-mark-article article mark))
12076
12077 (defun gnus-summary-clear-mark-forward (n)
12078   "Clear marks from N articles forward.
12079 If N is negative, clear backward instead.
12080 The difference between N and the number of marks cleared is returned."
12081   (interactive "p")
12082   (gnus-summary-mark-forward n gnus-unread-mark))
12083
12084 (defun gnus-summary-clear-mark-backward (n)
12085   "Clear marks from N articles backward.
12086 The difference between N and the number of marks cleared is returned."
12087   (interactive "p")
12088   (gnus-summary-mark-forward (- n) gnus-unread-mark))
12089
12090 (defun gnus-summary-mark-unread-as-read ()
12091   "Intended to be used by `gnus-summary-mark-article-hook'."
12092   (when (memq gnus-current-article gnus-newsgroup-unreads)
12093     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
12094
12095 (defun gnus-summary-mark-read-and-unread-as-read ()
12096   "Intended to be used by `gnus-summary-mark-article-hook'."
12097   (let ((mark (gnus-summary-article-mark)))
12098     (when (or (gnus-unread-mark-p mark)
12099               (gnus-read-mark-p mark))
12100       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
12101
12102 (defun gnus-summary-mark-region-as-read (point mark all)
12103   "Mark all unread articles between point and mark as read.
12104 If given a prefix, mark all articles between point and mark as read,
12105 even ticked and dormant ones."
12106   (interactive "r\nP")
12107   (save-excursion
12108     (let (article)
12109       (goto-char point)
12110       (beginning-of-line)
12111       (while (and
12112               (< (point) mark)
12113               (progn
12114                 (when (or all
12115                           (memq (setq article (gnus-summary-article-number))
12116                                 gnus-newsgroup-unreads))
12117                   (gnus-summary-mark-article article gnus-del-mark))
12118                 t)
12119               (gnus-summary-find-next))))))
12120
12121 (defun gnus-summary-mark-below (score mark)
12122   "Mark articles with score less than SCORE with MARK."
12123   (interactive "P\ncMark: ")
12124   (gnus-set-global-variables)
12125   (setq score (if score
12126                   (prefix-numeric-value score)
12127                 (or gnus-summary-default-score 0)))
12128   (save-excursion
12129     (set-buffer gnus-summary-buffer)
12130     (goto-char (point-min))
12131     (while 
12132         (progn
12133           (and (< (gnus-summary-article-score) score)
12134                (gnus-summary-mark-article nil mark))
12135           (gnus-summary-find-next)))))
12136
12137 (defun gnus-summary-kill-below (&optional score)
12138   "Mark articles with score below SCORE as read."
12139   (interactive "P")
12140   (gnus-set-global-variables)
12141   (gnus-summary-mark-below score gnus-killed-mark))
12142
12143 (defun gnus-summary-clear-above (&optional score)
12144   "Clear all marks from articles with score above SCORE."
12145   (interactive "P")
12146   (gnus-set-global-variables)
12147   (gnus-summary-mark-above score gnus-unread-mark))
12148
12149 (defun gnus-summary-tick-above (&optional score)
12150   "Tick all articles with score above SCORE."
12151   (interactive "P")
12152   (gnus-set-global-variables)
12153   (gnus-summary-mark-above score gnus-ticked-mark))
12154
12155 (defun gnus-summary-mark-above (score mark)
12156   "Mark articles with score over SCORE with MARK."
12157   (interactive "P\ncMark: ")
12158   (gnus-set-global-variables)
12159   (setq score (if score
12160                   (prefix-numeric-value score)
12161                 (or gnus-summary-default-score 0)))
12162   (save-excursion
12163     (set-buffer gnus-summary-buffer)
12164     (goto-char (point-min))
12165     (while (and (progn
12166                   (if (> (gnus-summary-article-score) score)
12167                       (gnus-summary-mark-article nil mark))
12168                   t)
12169                 (gnus-summary-find-next)))))
12170
12171 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12172 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12173 (defun gnus-summary-limit-include-expunged ()
12174   "Display all the hidden articles that were expunged for low scores."
12175   (interactive)
12176   (gnus-set-global-variables)
12177   (let ((buffer-read-only nil))
12178     (let ((scored gnus-newsgroup-scored)
12179           headers h)
12180       (while scored
12181         (or (gnus-summary-goto-subject (caar scored))
12182             (and (setq h (gnus-summary-article-header (caar scored)))
12183                  (< (cdar scored) gnus-summary-expunge-below)
12184                  (setq headers (cons h headers))))
12185         (setq scored (cdr scored)))
12186       (or headers (error "No expunged articles hidden."))
12187       (goto-char (point-min))
12188       (gnus-summary-prepare-unthreaded (nreverse headers)))
12189     (goto-char (point-min))
12190     (gnus-summary-position-point)))
12191
12192 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12193   "Mark all articles not marked as unread in this newsgroup as read.
12194 If prefix argument ALL is non-nil, all articles are marked as read.
12195 If QUIETLY is non-nil, no questions will be asked.
12196 If TO-HERE is non-nil, it should be a point in the buffer.  All
12197 articles before this point will be marked as read.
12198 The number of articles marked as read is returned."
12199   (interactive "P")
12200   (gnus-set-global-variables)
12201   (prog1
12202       (if (or quietly
12203               (not gnus-interactive-catchup) ;Without confirmation?
12204               gnus-expert-user
12205               (gnus-y-or-n-p
12206                (if all
12207                    "Mark absolutely all articles as read? "
12208                  "Mark all unread articles as read? ")))
12209           (if (and not-mark
12210                    (not gnus-newsgroup-adaptive)
12211                    (not gnus-newsgroup-auto-expire))
12212               (progn
12213                 (when all
12214                   (setq gnus-newsgroup-marked nil
12215                         gnus-newsgroup-dormant nil))
12216                 (setq gnus-newsgroup-unreads nil))
12217             ;; We actually mark all articles as canceled, which we
12218             ;; have to do when using auto-expiry or adaptive scoring.
12219             (gnus-summary-show-all-threads)
12220             (if (gnus-summary-first-subject (not all))
12221                 (while (and
12222                         (if to-here (< (point) to-here) t)
12223                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12224                         (gnus-summary-find-next (not all)))))
12225             (unless to-here
12226               (setq gnus-newsgroup-unreads nil))
12227             (gnus-set-mode-line 'summary)))
12228     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12229       (if (and (not to-here) (eq 'nnvirtual (car method)))
12230           (nnvirtual-catchup-group
12231            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12232     (gnus-summary-position-point)))
12233
12234 (defun gnus-summary-catchup-to-here (&optional all)
12235   "Mark all unticked articles before the current one as read.
12236 If ALL is non-nil, also mark ticked and dormant articles as read."
12237   (interactive "P")
12238   (gnus-set-global-variables)
12239   (save-excursion
12240     (let ((beg (point)))
12241       ;; We check that there are unread articles.
12242       (when (or all (gnus-summary-find-prev))
12243         (gnus-summary-catchup all t beg))))
12244   (gnus-summary-position-point))
12245
12246 (defun gnus-summary-catchup-all (&optional quietly)
12247   "Mark all articles in this newsgroup as read."
12248   (interactive "P")
12249   (gnus-set-global-variables)
12250   (gnus-summary-catchup t quietly))
12251
12252 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12253   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12254 If prefix argument ALL is non-nil, all articles are marked as read."
12255   (interactive "P")
12256   (gnus-set-global-variables)
12257   (gnus-summary-catchup all quietly nil 'fast)
12258   ;; Select next newsgroup or exit.
12259   (if (eq gnus-auto-select-next 'quietly)
12260       (gnus-summary-next-group nil)
12261     (gnus-summary-exit)))
12262
12263 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12264   "Mark all articles in this newsgroup as read, and then exit."
12265   (interactive "P")
12266   (gnus-set-global-variables)
12267   (gnus-summary-catchup-and-exit t quietly))
12268
12269 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12270 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12271   "Mark all articles in this group as read and select the next group.
12272 If given a prefix, mark all articles, unread as well as ticked, as
12273 read."
12274   (interactive "P")
12275   (gnus-set-global-variables)
12276   (save-excursion
12277     (gnus-summary-catchup all))
12278   (gnus-summary-next-article t nil nil t))
12279
12280 ;; Thread-based commands.
12281
12282 (defun gnus-summary-articles-in-thread (&optional article)
12283   "Return a list of all articles in the current thread.
12284 If ARTICLE is non-nil, return all articles in the thread that starts
12285 with that article."
12286   (let* ((article (or article (gnus-summary-article-number)))
12287          (data (gnus-data-find-list article))
12288          (top-level (gnus-data-level (car data)))
12289          (top-subject
12290           (cond ((null gnus-thread-operation-ignore-subject)
12291                  (gnus-simplify-subject-re
12292                   (mail-header-subject (gnus-data-header (car data)))))
12293                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12294                  (gnus-simplify-subject-fuzzy
12295                   (mail-header-subject (gnus-data-header (car data)))))
12296                 (t nil)))
12297          articles)
12298     (if (not data)
12299         ()                              ; This article doesn't exist.
12300       (while data
12301         (and (or (not top-subject)
12302                  (string= top-subject
12303                           (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12304                               (gnus-simplify-subject-fuzzy
12305                                (mail-header-subject
12306                                 (gnus-data-header (car data))))
12307                             (gnus-simplify-subject-re
12308                              (mail-header-subject
12309                               (gnus-data-header (car data)))))))
12310              (setq articles (cons (gnus-data-number (car data)) articles)))
12311         (if (and (setq data (cdr data))
12312                  (> (gnus-data-level (car data)) top-level))
12313             ()
12314           (setq data nil)))
12315       ;; Return the list of articles.
12316       (nreverse articles))))
12317
12318 (defun gnus-summary-rethread-current ()
12319   "Rethread the thread the current article is part of."
12320   (interactive)
12321   (gnus-set-global-variables)
12322   (let* ((gnus-show-threads t)
12323          (article (gnus-summary-article-number))
12324          (id (mail-header-id (gnus-summary-article-header)))
12325          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12326     (unless id
12327       (error "No article on the current line"))
12328     (gnus-rebuild-thread id)
12329     (gnus-summary-goto-subject article)))
12330
12331 (defun gnus-summary-reparent-thread ()
12332   "Make current article child of the marked (or previous) article.
12333
12334 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12335 is non-nil or the Subject: of both articles are the same."
12336   (interactive)
12337   (or (not (gnus-group-read-only-p))
12338       (error "The current newsgroup does not support article editing."))
12339   (or (<= (length gnus-newsgroup-processable) 1)
12340       (error "No more than one article may be marked."))
12341   (save-window-excursion
12342     (let ((gnus-article-buffer " *reparent*")
12343           (current-article (gnus-summary-article-number))
12344           ; first grab the marked article, otherwise one line up.
12345           (parent-article (if (not (null gnus-newsgroup-processable))
12346                               (car gnus-newsgroup-processable)
12347                             (save-excursion
12348                               (if (eq (forward-line -1) 0)
12349                                   (gnus-summary-article-number)
12350                                 (error "Beginning of summary buffer."))))))
12351       (or (not (eq current-article parent-article))
12352           (error "An article may not be self-referential."))
12353       (let ((message-id (mail-header-id 
12354                          (gnus-summary-article-header parent-article))))
12355         (or (and message-id (not (equal message-id "")))
12356             (error "No message-id in desired parent."))
12357         (gnus-summary-select-article t t nil current-article)
12358         (set-buffer gnus-article-buffer)
12359         (setq buffer-read-only nil)
12360         (let ((buf (format "%s" (buffer-string))))
12361           (erase-buffer)
12362           (insert buf))
12363         (goto-char (point-min))
12364         (if (search-forward-regexp "^References: " nil t)
12365             (insert message-id " " )
12366           (insert "References: " message-id "\n"))
12367         (or (gnus-request-replace-article current-article
12368                                           (car gnus-article-current)
12369                                           gnus-article-buffer)
12370             (error "Couldn't replace article."))
12371         (set-buffer gnus-summary-buffer)
12372         (gnus-summary-unmark-all-processable)
12373         (gnus-summary-rethread-current)
12374         (message "Article %d is now the child of article %d."
12375                  current-article parent-article)))))
12376
12377 (defun gnus-summary-toggle-threads (&optional arg)
12378   "Toggle showing conversation threads.
12379 If ARG is positive number, turn showing conversation threads on."
12380   (interactive "P")
12381   (gnus-set-global-variables)
12382   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12383     (setq gnus-show-threads
12384           (if (null arg) (not gnus-show-threads)
12385             (> (prefix-numeric-value arg) 0)))
12386     (gnus-summary-prepare)
12387     (gnus-summary-goto-subject current)
12388     (gnus-summary-position-point)))
12389
12390 (defun gnus-summary-show-all-threads ()
12391   "Show all threads."
12392   (interactive)
12393   (gnus-set-global-variables)
12394   (save-excursion
12395     (let ((buffer-read-only nil))
12396       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12397   (gnus-summary-position-point))
12398
12399 (defun gnus-summary-show-thread ()
12400   "Show thread subtrees.
12401 Returns nil if no thread was there to be shown."
12402   (interactive)
12403   (gnus-set-global-variables)
12404   (let ((buffer-read-only nil)
12405         (orig (point))
12406         ;; first goto end then to beg, to have point at beg after let
12407         (end (progn (end-of-line) (point)))
12408         (beg (progn (beginning-of-line) (point))))
12409     (prog1
12410         ;; Any hidden lines here?
12411         (search-forward "\r" end t)
12412       (subst-char-in-region beg end ?\^M ?\n t)
12413       (goto-char orig)
12414       (gnus-summary-position-point))))
12415
12416 (defun gnus-summary-hide-all-threads ()
12417   "Hide all thread subtrees."
12418   (interactive)
12419   (gnus-set-global-variables)
12420   (save-excursion
12421     (goto-char (point-min))
12422     (gnus-summary-hide-thread)
12423     (while (zerop (gnus-summary-next-thread 1 t))
12424       (gnus-summary-hide-thread)))
12425   (gnus-summary-position-point))
12426
12427 (defun gnus-summary-hide-thread ()
12428   "Hide thread subtrees.
12429 Returns nil if no threads were there to be hidden."
12430   (interactive)
12431   (gnus-set-global-variables)
12432   (let ((buffer-read-only nil)
12433         (start (point))
12434         (article (gnus-summary-article-number)))
12435     (goto-char start)
12436     ;; Go forward until either the buffer ends or the subthread
12437     ;; ends.
12438     (when (and (not (eobp))
12439                (or (zerop (gnus-summary-next-thread 1 t))
12440                    (goto-char (point-max))))
12441       (prog1
12442           (if (and (> (point) start)
12443                    (search-backward "\n" start t))
12444               (progn
12445                 (subst-char-in-region start (point) ?\n ?\^M)
12446                 (gnus-summary-goto-subject article))
12447             (goto-char start)
12448             nil)
12449         ;;(gnus-summary-position-point)
12450         ))))
12451
12452 (defun gnus-summary-go-to-next-thread (&optional previous)
12453   "Go to the same level (or less) next thread.
12454 If PREVIOUS is non-nil, go to previous thread instead.
12455 Return the article number moved to, or nil if moving was impossible."
12456   (let ((level (gnus-summary-thread-level))
12457         (way (if previous -1 1))
12458         (beg (point)))
12459     (forward-line way)
12460     (while (and (not (eobp))
12461                 (< level (gnus-summary-thread-level)))
12462       (forward-line way))
12463     (if (eobp)
12464         (progn
12465           (goto-char beg)
12466           nil)
12467       (setq beg (point))
12468       (prog1
12469           (gnus-summary-article-number)
12470         (goto-char beg)))))
12471
12472 (defun gnus-summary-go-to-next-thread-old (&optional previous)
12473   "Go to the same level (or less) next thread.
12474 If PREVIOUS is non-nil, go to previous thread instead.
12475 Return the article number moved to, or nil if moving was impossible."
12476   (if (and (eq gnus-summary-make-false-root 'dummy)
12477            (gnus-summary-article-intangible-p))
12478       (let ((beg (point)))
12479         (while (and (zerop (forward-line 1))
12480                     (not (gnus-summary-article-intangible-p))
12481                     (not (zerop (save-excursion 
12482                                   (gnus-summary-thread-level))))))
12483         (if (eobp)
12484             (progn
12485               (goto-char beg)
12486               nil)
12487           (point)))
12488     (let* ((level (gnus-summary-thread-level))
12489            (article (gnus-summary-article-number))
12490            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12491            oart)
12492       (while data
12493         (if (<= (gnus-data-level (car data)) level)
12494             (setq oart (gnus-data-number (car data))
12495                   data nil)
12496           (setq data (cdr data))))
12497       (and oart
12498            (gnus-summary-goto-subject oart)))))
12499
12500 (defun gnus-summary-next-thread (n &optional silent)
12501   "Go to the same level next N'th thread.
12502 If N is negative, search backward instead.
12503 Returns the difference between N and the number of skips actually
12504 done.
12505
12506 If SILENT, don't output messages."
12507   (interactive "p")
12508   (gnus-set-global-variables)
12509   (let ((backward (< n 0))
12510         (n (abs n))
12511         old dum int)
12512     (while (and (> n 0)
12513                 (gnus-summary-go-to-next-thread backward))
12514       (decf n))
12515     (unless silent 
12516       (gnus-summary-position-point))
12517     (when (and (not silent) (/= 0 n))
12518       (gnus-message 7 "No more threads"))
12519     n))
12520
12521 (defun gnus-summary-prev-thread (n)
12522   "Go to the same level previous N'th thread.
12523 Returns the difference between N and the number of skips actually
12524 done."
12525   (interactive "p")
12526   (gnus-set-global-variables)
12527   (gnus-summary-next-thread (- n)))
12528
12529 (defun gnus-summary-go-down-thread ()
12530   "Go down one level in the current thread."
12531   (let ((children (gnus-summary-article-children)))
12532     (and children
12533          (gnus-summary-goto-subject (car children)))))
12534
12535 (defun gnus-summary-go-up-thread ()
12536   "Go up one level in the current thread."
12537   (let ((parent (gnus-summary-article-parent)))
12538     (and parent
12539          (gnus-summary-goto-subject parent))))
12540
12541 (defun gnus-summary-down-thread (n)
12542   "Go down thread N steps.
12543 If N is negative, go up instead.
12544 Returns the difference between N and how many steps down that were
12545 taken."
12546   (interactive "p")
12547   (gnus-set-global-variables)
12548   (let ((up (< n 0))
12549         (n (abs n)))
12550     (while (and (> n 0)
12551                 (if up (gnus-summary-go-up-thread)
12552                   (gnus-summary-go-down-thread)))
12553       (setq n (1- n)))
12554     (gnus-summary-position-point)
12555     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12556     n))
12557
12558 (defun gnus-summary-up-thread (n)
12559   "Go up thread N steps.
12560 If N is negative, go up instead.
12561 Returns the difference between N and how many steps down that were
12562 taken."
12563   (interactive "p")
12564   (gnus-set-global-variables)
12565   (gnus-summary-down-thread (- n)))
12566
12567 (defun gnus-summary-top-thread ()
12568   "Go to the top of the thread."
12569   (interactive)
12570   (gnus-set-global-variables)
12571   (while (gnus-summary-go-up-thread))
12572   (gnus-summary-article-number))
12573
12574 (defun gnus-summary-kill-thread (&optional unmark)
12575   "Mark articles under current thread as read.
12576 If the prefix argument is positive, remove any kinds of marks.
12577 If the prefix argument is negative, tick articles instead."
12578   (interactive "P")
12579   (gnus-set-global-variables)
12580   (if unmark
12581       (setq unmark (prefix-numeric-value unmark)))
12582   (let ((articles (gnus-summary-articles-in-thread)))
12583     (save-excursion
12584       ;; Expand the thread.
12585       (gnus-summary-show-thread)
12586       ;; Mark all the articles.
12587       (while articles
12588         (gnus-summary-goto-subject (car articles))
12589         (cond ((null unmark)
12590                (gnus-summary-mark-article-as-read gnus-killed-mark))
12591               ((> unmark 0)
12592                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12593               (t
12594                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12595         (setq articles (cdr articles))))
12596     ;; Hide killed subtrees.
12597     (and (null unmark)
12598          gnus-thread-hide-killed
12599          (gnus-summary-hide-thread))
12600     ;; If marked as read, go to next unread subject.
12601     (if (null unmark)
12602         ;; Go to next unread subject.
12603         (gnus-summary-next-subject 1 t)))
12604   (gnus-set-mode-line 'summary))
12605
12606 ;; Summary sorting commands
12607
12608 (defun gnus-summary-sort-by-number (&optional reverse)
12609   "Sort summary buffer by article number.
12610 Argument REVERSE means reverse order."
12611   (interactive "P")
12612   (gnus-summary-sort 'number reverse))
12613
12614 (defun gnus-summary-sort-by-author (&optional reverse)
12615   "Sort summary buffer by author name alphabetically.
12616 If case-fold-search is non-nil, case of letters is ignored.
12617 Argument REVERSE means reverse order."
12618   (interactive "P")
12619   (gnus-summary-sort 'author reverse))
12620
12621 (defun gnus-summary-sort-by-subject (&optional reverse)
12622   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12623 If case-fold-search is non-nil, case of letters is ignored.
12624 Argument REVERSE means reverse order."
12625   (interactive "P")
12626   (gnus-summary-sort 'subject reverse))
12627
12628 (defun gnus-summary-sort-by-date (&optional reverse)
12629   "Sort summary buffer by date.
12630 Argument REVERSE means reverse order."
12631   (interactive "P")
12632   (gnus-summary-sort 'date reverse))
12633
12634 (defun gnus-summary-sort-by-score (&optional reverse)
12635   "Sort summary buffer by score.
12636 Argument REVERSE means reverse order."
12637   (interactive "P")
12638   (gnus-summary-sort 'score reverse))
12639
12640 (defun gnus-summary-sort (predicate reverse)
12641   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12642   (gnus-set-global-variables)
12643   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12644          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12645          (gnus-thread-sort-functions
12646           (list
12647            (if (not reverse)
12648                thread
12649              `(lambda (t1 t2)
12650                 (,thread t2 t1)))))
12651          (gnus-article-sort-functions
12652           (list
12653            (if (not reverse)
12654                article
12655              `(lambda (t1 t2)
12656                 (,article t2 t1)))))
12657          (buffer-read-only)
12658          (gnus-summary-prepare-hook nil))
12659     ;; We do the sorting by regenerating the threads.
12660     (gnus-summary-prepare)
12661     ;; Hide subthreads if needed.
12662     (when (and gnus-show-threads gnus-thread-hide-subtree)
12663       (gnus-summary-hide-all-threads)))
12664   ;; If in async mode, we send some info to the backend.
12665   (when gnus-newsgroup-async
12666     (gnus-request-asynchronous
12667      gnus-newsgroup-name gnus-newsgroup-data)))
12668
12669 (defun gnus-sortable-date (date)
12670   "Make sortable string by string-lessp from DATE.
12671 Timezone package is used."
12672   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
12673          (year (aref date 0))
12674          (month (aref date 1))
12675          (day (aref date 2)))
12676     (timezone-make-sortable-date
12677      year month day
12678      (timezone-make-time-string
12679       (aref date 3) (aref date 4) (aref date 5)))))
12680
12681 ;; Summary saving commands.
12682
12683 (defun gnus-summary-save-article (&optional n not-saved)
12684   "Save the current article using the default saver function.
12685 If N is a positive number, save the N next articles.
12686 If N is a negative number, save the N previous articles.
12687 If N is nil and any articles have been marked with the process mark,
12688 save those articles instead.
12689 The variable `gnus-default-article-saver' specifies the saver function."
12690   (interactive "P")
12691   (gnus-set-global-variables)
12692   (let ((articles (gnus-summary-work-articles n))
12693         file header article)
12694     (while articles
12695       (setq header (gnus-summary-article-header
12696                     (setq article (pop articles))))
12697       (if (not (vectorp header))
12698           ;; This is a pseudo-article.
12699           (if (assq 'name header)
12700               (gnus-copy-file (cdr (assq 'name header)))
12701             (gnus-message 1 "Article %d is unsaveable" article))
12702         ;; This is a real article.
12703         (save-window-excursion
12704           (gnus-summary-select-article t nil nil article))
12705         (unless gnus-save-all-headers
12706           ;; Remove headers accoring to `gnus-saved-headers'.
12707           (let ((gnus-visible-headers
12708                  (or gnus-saved-headers gnus-visible-headers)))
12709             (gnus-article-hide-headers nil t)))
12710         ;; Remove any X-Gnus lines.
12711         (save-excursion
12712           (set-buffer gnus-article-buffer)
12713           (save-restriction
12714             (let ((buffer-read-only nil))
12715               (nnheader-narrow-to-headers)
12716               (while (re-search-forward "^X-Gnus" nil t)
12717                 (gnus-delete-line)))))
12718         (save-window-excursion
12719           (if (not gnus-default-article-saver)
12720               (error "No default saver is defined.")
12721             (setq file (funcall
12722                         gnus-default-article-saver
12723                         (cond
12724                          ((not gnus-prompt-before-saving)
12725                           'default)
12726                          ((eq gnus-prompt-before-saving 'always)
12727                           nil)
12728                          (t file))))))
12729         (gnus-summary-remove-process-mark article)
12730         (unless not-saved
12731           (gnus-summary-set-saved-mark article))))
12732     (gnus-summary-position-point)
12733     n))
12734
12735 (defun gnus-summary-pipe-output (&optional arg)
12736   "Pipe the current article to a subprocess.
12737 If N is a positive number, pipe the N next articles.
12738 If N is a negative number, pipe the N previous articles.
12739 If N is nil and any articles have been marked with the process mark,
12740 pipe those articles instead."
12741   (interactive "P")
12742   (gnus-set-global-variables)
12743   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12744     (gnus-summary-save-article arg t))
12745   (gnus-configure-windows 'pipe))
12746
12747 (defun gnus-summary-save-article-mail (&optional arg)
12748   "Append the current article to an mail file.
12749 If N is a positive number, save the N next articles.
12750 If N is a negative number, save the N previous articles.
12751 If N is nil and any articles have been marked with the process mark,
12752 save those articles instead."
12753   (interactive "P")
12754   (gnus-set-global-variables)
12755   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12756     (gnus-summary-save-article arg)))
12757
12758 (defun gnus-summary-save-article-rmail (&optional arg)
12759   "Append the current article to an rmail file.
12760 If N is a positive number, save the N next articles.
12761 If N is a negative number, save the N previous articles.
12762 If N is nil and any articles have been marked with the process mark,
12763 save those articles instead."
12764   (interactive "P")
12765   (gnus-set-global-variables)
12766   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12767     (gnus-summary-save-article arg)))
12768
12769 (defun gnus-summary-save-article-file (&optional arg)
12770   "Append the current article to a file.
12771 If N is a positive number, save the N next articles.
12772 If N is a negative number, save the N previous articles.
12773 If N is nil and any articles have been marked with the process mark,
12774 save those articles instead."
12775   (interactive "P")
12776   (gnus-set-global-variables)
12777   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12778     (gnus-summary-save-article arg)))
12779
12780 (defun gnus-summary-save-article-body-file (&optional arg)
12781   "Append the current article body to a file.
12782 If N is a positive number, save the N next articles.
12783 If N is a negative number, save the N previous articles.
12784 If N is nil and any articles have been marked with the process mark,
12785 save those articles instead."
12786   (interactive "P")
12787   (gnus-set-global-variables)
12788   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12789     (gnus-summary-save-article arg)))
12790
12791 (defun gnus-get-split-value (methods)
12792   "Return a value based on the split METHODS."
12793   (let (split-name method result match)
12794     (when methods
12795       (save-excursion
12796         (set-buffer gnus-original-article-buffer)
12797         (save-restriction
12798           (nnheader-narrow-to-headers)
12799           (while methods
12800             (goto-char (point-min))
12801             (setq method (pop methods))
12802             (setq match (car method))
12803             (when (cond
12804                    ((stringp match)
12805                     ;; Regular expression.
12806                     (condition-case ()
12807                         (re-search-forward match nil t)
12808                       (error nil)))
12809                    ((gnus-functionp match)
12810                     ;; Function.
12811                     (save-restriction
12812                       (widen)
12813                       (setq result (funcall match gnus-newsgroup-name))))
12814                    ((consp match)
12815                     ;; Form.
12816                     (save-restriction
12817                       (widen)
12818                       (setq result (eval match)))))
12819               (setq split-name (append (cdr method) split-name))
12820               (cond ((stringp result)
12821                      (push result split-name))
12822                     ((consp result)
12823                      (setq split-name (append result split-name)))))))))
12824     split-name))
12825
12826 (defun gnus-read-move-group-name (prompt default articles prefix)
12827   "Read a group name."
12828   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12829          (prom
12830           (format "Where do you want to %s %s? "
12831                   prompt
12832                   (if (> (length articles) 1)
12833                       (format "these %d articles" (length articles))
12834                     "this article")))
12835          (to-newsgroup
12836           (cond
12837            ((null split-name)
12838             (completing-read
12839              (concat prom
12840                      (if default
12841                          (format "(default %s) " default)
12842                        ""))
12843              gnus-active-hashtb nil nil prefix))
12844            ((= 1 (length split-name))
12845             (completing-read prom gnus-active-hashtb
12846                              nil nil (cons (car split-name) 0)))
12847            (t
12848             (completing-read
12849              prom (mapcar (lambda (el) (list el)) (nreverse split-name)))))))
12850
12851     (when to-newsgroup
12852       (if (or (string= to-newsgroup "")
12853               (string= to-newsgroup prefix))
12854           (setq to-newsgroup (or default "")))
12855       (or (gnus-active to-newsgroup)
12856           (gnus-activate-group to-newsgroup)
12857           (error "No such group: %s" to-newsgroup)))
12858     to-newsgroup))
12859
12860 (defun gnus-read-save-file-name (prompt default-name)
12861   (let* ((split-name (gnus-get-split-value gnus-split-methods))
12862          (file
12863           ;; Let the split methods have their say.
12864           (cond
12865            ;; No split name was found.
12866            ((null split-name)
12867             (read-file-name
12868              (concat prompt " (default "
12869                      (file-name-nondirectory default-name) ") ")
12870              (file-name-directory default-name)
12871              default-name))
12872            ;; A single split name was found
12873            ((= 1 (length split-name))
12874             (read-file-name
12875              (concat prompt " (default " (car split-name) ") ")
12876              gnus-article-save-directory
12877              (concat gnus-article-save-directory (car split-name))))
12878            ;; A list of splits was found.
12879            (t
12880             (setq split-name (mapcar (lambda (el) (list el))
12881                                      (nreverse split-name)))
12882             (let ((result (completing-read
12883                            (concat prompt " ") split-name nil nil)))
12884               (concat gnus-article-save-directory
12885                       (if (string= result "")
12886                           (caar split-name)
12887                         result)))))))
12888     ;; If we have read a directory, we append the default file name.
12889     (when (file-directory-p file)
12890       (setq file (concat (file-name-as-directory file)
12891                          (file-name-nondirectory default-name))))
12892     ;; Possibly translate some charaters.
12893     (nnheader-translate-file-chars file)))
12894
12895 (defun gnus-article-archive-name (group)
12896   "Return the first instance of an \"Archive-name\" in the current buffer."
12897   (let ((case-fold-search t))
12898     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
12899       (match-string 1))))
12900
12901 (defun gnus-summary-save-in-rmail (&optional filename)
12902   "Append this article to Rmail file.
12903 Optional argument FILENAME specifies file name.
12904 Directory to save to is default to `gnus-article-save-directory' which
12905 is initialized from the SAVEDIR environment variable."
12906   (interactive)
12907   (gnus-set-global-variables)
12908   (let ((default-name
12909           (funcall gnus-rmail-save-name gnus-newsgroup-name
12910                    gnus-current-headers gnus-newsgroup-last-rmail)))
12911     (setq filename
12912           (cond ((eq filename 'default)
12913                  default-name)
12914                 (filename filename)
12915                 (t (gnus-read-save-file-name
12916                     "Save in rmail file:" default-name))))
12917     (gnus-make-directory (file-name-directory filename))
12918     (gnus-eval-in-buffer-window
12919      gnus-original-article-buffer
12920      (save-excursion
12921        (save-restriction
12922          (widen)
12923          (gnus-output-to-rmail filename))))
12924     ;; Remember the directory name to save articles
12925     (setq gnus-newsgroup-last-rmail filename)))
12926
12927 (defun gnus-summary-save-in-mail (&optional filename)
12928   "Append this article to Unix mail file.
12929 Optional argument FILENAME specifies file name.
12930 Directory to save to is default to `gnus-article-save-directory' which
12931 is initialized from the SAVEDIR environment variable."
12932   (interactive)
12933   (gnus-set-global-variables)
12934   (let ((default-name
12935           (funcall gnus-mail-save-name gnus-newsgroup-name
12936                    gnus-current-headers gnus-newsgroup-last-mail)))
12937     (setq filename
12938           (cond ((eq filename 'default)
12939                  default-name)
12940                 (filename filename)
12941                 (t (gnus-read-save-file-name
12942                     "Save in Unix mail file:" default-name))))
12943     (setq filename
12944           (expand-file-name filename
12945                             (and default-name
12946                                  (file-name-directory default-name))))
12947     (gnus-make-directory (file-name-directory filename))
12948     (gnus-eval-in-buffer-window
12949      gnus-original-article-buffer
12950      (save-excursion
12951        (save-restriction
12952          (widen)
12953          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
12954              (gnus-output-to-rmail filename)
12955            (let ((mail-use-rfc822 t))
12956              (rmail-output filename 1 t t))))))
12957     ;; Remember the directory name to save articles.
12958     (setq gnus-newsgroup-last-mail filename)))
12959
12960 (defun gnus-summary-save-in-file (&optional filename)
12961   "Append this article to file.
12962 Optional argument FILENAME specifies file name.
12963 Directory to save to is default to `gnus-article-save-directory' which
12964 is initialized from the SAVEDIR environment variable."
12965   (interactive)
12966   (gnus-set-global-variables)
12967   (let ((default-name
12968           (funcall gnus-file-save-name gnus-newsgroup-name
12969                    gnus-current-headers gnus-newsgroup-last-file)))
12970     (setq filename
12971           (cond ((eq filename 'default)
12972                  default-name)
12973                 (filename filename)
12974                 (t (gnus-read-save-file-name
12975                     "Save in file:" default-name))))
12976     (gnus-make-directory (file-name-directory filename))
12977     (gnus-eval-in-buffer-window
12978      gnus-original-article-buffer
12979      (save-excursion
12980        (save-restriction
12981          (widen)
12982          (gnus-output-to-file filename))))
12983     ;; Remember the directory name to save articles.
12984     (setq gnus-newsgroup-last-file filename)))
12985
12986 (defun gnus-summary-save-body-in-file (&optional filename)
12987   "Append this article body to a file.
12988 Optional argument FILENAME specifies file name.
12989 The directory to save in defaults to `gnus-article-save-directory' which
12990 is initialized from the SAVEDIR environment variable."
12991   (interactive)
12992   (gnus-set-global-variables)
12993   (let ((default-name
12994           (funcall gnus-file-save-name gnus-newsgroup-name
12995                    gnus-current-headers gnus-newsgroup-last-file)))
12996     (setq filename
12997           (cond ((eq filename 'default)
12998                  default-name)
12999                 (filename filename)
13000                 (t (gnus-read-save-file-name
13001                     "Save body in file:" default-name))))
13002     (gnus-make-directory (file-name-directory filename))
13003     (gnus-eval-in-buffer-window
13004      gnus-article-buffer
13005      (save-excursion
13006        (save-restriction
13007          (widen)
13008          (goto-char (point-min))
13009          (and (search-forward "\n\n" nil t)
13010               (narrow-to-region (point) (point-max)))
13011          (gnus-output-to-file filename))))
13012     ;; Remember the directory name to save articles.
13013     (setq gnus-newsgroup-last-file filename)))
13014
13015 (defun gnus-summary-save-in-pipe (&optional command)
13016   "Pipe this article to subprocess."
13017   (interactive)
13018   (gnus-set-global-variables)
13019   (setq command
13020         (cond ((eq command 'default)
13021                gnus-last-shell-command)
13022               (command command)
13023               (t (read-string "Shell command on article: "
13024                               gnus-last-shell-command))))
13025   (if (string-equal command "")
13026       (setq command gnus-last-shell-command))
13027   (gnus-eval-in-buffer-window
13028    gnus-article-buffer
13029    (save-restriction
13030      (widen)
13031      (shell-command-on-region (point-min) (point-max) command nil)))
13032   (setq gnus-last-shell-command command))
13033
13034 ;; Summary extract commands
13035
13036 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
13037   (let ((buffer-read-only nil)
13038         (article (gnus-summary-article-number))
13039         after-article b e)
13040     (or (gnus-summary-goto-subject article)
13041         (error (format "No such article: %d" article)))
13042     (gnus-summary-position-point)
13043     ;; If all commands are to be bunched up on one line, we collect
13044     ;; them here.
13045     (if gnus-view-pseudos-separately
13046         ()
13047       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
13048             files action)
13049         (while ps
13050           (setq action (cdr (assq 'action (car ps))))
13051           (setq files (list (cdr (assq 'name (car ps)))))
13052           (while (and ps (cdr ps)
13053                       (string= (or action "1")
13054                                (or (cdr (assq 'action (cadr ps))) "2")))
13055             (setq files (cons (cdr (assq 'name (cadr ps))) files))
13056             (setcdr ps (cddr ps)))
13057           (if (not files)
13058               ()
13059             (if (not (string-match "%s" action))
13060                 (setq files (cons " " files)))
13061             (setq files (cons " " files))
13062             (and (assq 'execute (car ps))
13063                  (setcdr (assq 'execute (car ps))
13064                          (funcall (if (string-match "%s" action)
13065                                       'format 'concat)
13066                                   action
13067                                   (mapconcat (lambda (f) f) files " ")))))
13068           (setq ps (cdr ps)))))
13069     (if (and gnus-view-pseudos (not not-view))
13070         (while pslist
13071           (and (assq 'execute (car pslist))
13072                (gnus-execute-command (cdr (assq 'execute (car pslist)))
13073                                      (eq gnus-view-pseudos 'not-confirm)))
13074           (setq pslist (cdr pslist)))
13075       (save-excursion
13076         (while pslist
13077           (setq after-article (or (cdr (assq 'article (car pslist)))
13078                                   (gnus-summary-article-number)))
13079           (gnus-summary-goto-subject after-article)
13080           (forward-line 1)
13081           (setq b (point))
13082           (insert "    " (file-name-nondirectory
13083                                 (cdr (assq 'name (car pslist))))
13084                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
13085           (setq e (point))
13086           (forward-line -1)             ; back to `b'
13087           (add-text-properties
13088            b e (list 'gnus-number gnus-reffed-article-number
13089                      gnus-mouse-face-prop gnus-mouse-face))
13090           (gnus-data-enter
13091            after-article gnus-reffed-article-number
13092            gnus-unread-mark b (car pslist) 0 (- e b))
13093           (push gnus-reffed-article-number gnus-newsgroup-unreads)
13094           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
13095           (setq pslist (cdr pslist)))))))
13096
13097 (defun gnus-pseudos< (p1 p2)
13098   (let ((c1 (cdr (assq 'action p1)))
13099         (c2 (cdr (assq 'action p2))))
13100     (and c1 c2 (string< c1 c2))))
13101
13102 (defun gnus-request-pseudo-article (props)
13103   (cond ((assq 'execute props)
13104          (gnus-execute-command (cdr (assq 'execute props)))))
13105   (let ((gnus-current-article (gnus-summary-article-number)))
13106     (run-hooks 'gnus-mark-article-hook)))
13107
13108 (defun gnus-execute-command (command &optional automatic)
13109   (save-excursion
13110     (gnus-article-setup-buffer)
13111     (set-buffer gnus-article-buffer)
13112     (let ((command (if automatic command (read-string "Command: " command)))
13113           (buffer-read-only nil))
13114       (erase-buffer)
13115       (insert "$ " command "\n\n")
13116       (if gnus-view-pseudo-asynchronously
13117           (start-process "gnus-execute" nil "sh" "-c" command)
13118         (call-process "sh" nil t nil "-c" command)))))
13119
13120 (defun gnus-copy-file (file &optional to)
13121   "Copy FILE to TO."
13122   (interactive
13123    (list (read-file-name "Copy file: " default-directory)
13124          (read-file-name "Copy file to: " default-directory)))
13125   (gnus-set-global-variables)
13126   (or to (setq to (read-file-name "Copy file to: " default-directory)))
13127   (and (file-directory-p to)
13128        (setq to (concat (file-name-as-directory to)
13129                         (file-name-nondirectory file))))
13130   (copy-file file to))
13131
13132 ;; Summary kill commands.
13133
13134 (defun gnus-summary-edit-global-kill (article)
13135   "Edit the \"global\" kill file."
13136   (interactive (list (gnus-summary-article-number)))
13137   (gnus-set-global-variables)
13138   (gnus-group-edit-global-kill article))
13139
13140 (defun gnus-summary-edit-local-kill ()
13141   "Edit a local kill file applied to the current newsgroup."
13142   (interactive)
13143   (gnus-set-global-variables)
13144   (setq gnus-current-headers (gnus-summary-article-header))
13145   (gnus-set-global-variables)
13146   (gnus-group-edit-local-kill
13147    (gnus-summary-article-number) gnus-newsgroup-name))
13148
13149 \f
13150 ;;;
13151 ;;; Gnus article mode
13152 ;;;
13153
13154 (put 'gnus-article-mode 'mode-class 'special)
13155
13156 (if gnus-article-mode-map
13157     nil
13158   (setq gnus-article-mode-map (make-keymap))
13159   (suppress-keymap gnus-article-mode-map)
13160
13161   (gnus-define-keys gnus-article-mode-map
13162     " " gnus-article-goto-next-page
13163     "\177" gnus-article-goto-prev-page
13164     [delete] gnus-article-goto-prev-page
13165     "\C-c^" gnus-article-refer-article
13166     "h" gnus-article-show-summary
13167     "s" gnus-article-show-summary
13168     "\C-c\C-m" gnus-article-mail
13169     "?" gnus-article-describe-briefly
13170     gnus-mouse-2 gnus-article-push-button
13171     "\r" gnus-article-press-button
13172     "\t" gnus-article-next-button
13173     "\M-\t" gnus-article-prev-button
13174     "\C-c\C-b" gnus-bug)
13175
13176   (substitute-key-definition
13177    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13178
13179 (defun gnus-article-mode ()
13180   "Major mode for displaying an article.
13181
13182 All normal editing commands are switched off.
13183
13184 The following commands are available:
13185
13186 \\<gnus-article-mode-map>
13187 \\[gnus-article-next-page]\t Scroll the article one page forwards
13188 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13189 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13190 \\[gnus-article-show-summary]\t Display the summary buffer
13191 \\[gnus-article-mail]\t Send a reply to the address near point
13192 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13193 \\[gnus-info-find-node]\t Go to the Gnus info node"
13194   (interactive)
13195   (when (and menu-bar-mode
13196              (gnus-visual-p 'article-menu 'menu))
13197     (gnus-article-make-menu-bar))
13198   (kill-all-local-variables)
13199   (gnus-simplify-mode-line)
13200   (setq mode-name "Article")
13201   (setq major-mode 'gnus-article-mode)
13202   (make-local-variable 'minor-mode-alist)
13203   (or (assq 'gnus-show-mime minor-mode-alist)
13204       (setq minor-mode-alist
13205             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13206   (use-local-map gnus-article-mode-map)
13207   (make-local-variable 'page-delimiter)
13208   (setq page-delimiter gnus-page-delimiter)
13209   (buffer-disable-undo (current-buffer))
13210   (setq buffer-read-only t)             ;Disable modification
13211   (run-hooks 'gnus-article-mode-hook))
13212
13213 (defun gnus-article-setup-buffer ()
13214   "Initialize the article buffer."
13215   (let* ((name (if gnus-single-article-buffer "*Article*"
13216                  (concat "*Article " gnus-newsgroup-name "*")))
13217          (original
13218           (progn (string-match "\\*Article" name)
13219                  (concat " *Original Article"
13220                          (substring name (match-end 0))))))
13221     (setq gnus-article-buffer name)
13222     (setq gnus-original-article-buffer original)
13223     ;; This might be a variable local to the summary buffer.
13224     (unless gnus-single-article-buffer
13225       (save-excursion
13226         (set-buffer gnus-summary-buffer)
13227         (setq gnus-article-buffer name)
13228         (setq gnus-original-article-buffer original)
13229         (gnus-set-global-variables))
13230       (make-local-variable 'gnus-summary-buffer))
13231     ;; Init original article buffer.
13232     (save-excursion
13233       (set-buffer (get-buffer-create gnus-original-article-buffer))
13234       (buffer-disable-undo (current-buffer))
13235       (setq major-mode 'gnus-original-article-mode)
13236       (make-local-variable 'gnus-original-article))
13237     (if (get-buffer name)
13238         (save-excursion
13239           (set-buffer name)
13240           (buffer-disable-undo (current-buffer))
13241           (setq buffer-read-only t)
13242           (gnus-add-current-to-buffer-list)
13243           (or (eq major-mode 'gnus-article-mode)
13244               (gnus-article-mode))
13245           (current-buffer))
13246       (save-excursion
13247         (set-buffer (get-buffer-create name))
13248         (gnus-add-current-to-buffer-list)
13249         (gnus-article-mode)
13250         (current-buffer)))))
13251
13252 ;; Set article window start at LINE, where LINE is the number of lines
13253 ;; from the head of the article.
13254 (defun gnus-article-set-window-start (&optional line)
13255   (set-window-start
13256    (get-buffer-window gnus-article-buffer)
13257    (save-excursion
13258      (set-buffer gnus-article-buffer)
13259      (goto-char (point-min))
13260      (if (not line)
13261          (point-min)
13262        (gnus-message 6 "Moved to bookmark")
13263        (search-forward "\n\n" nil t)
13264        (forward-line line)
13265        (point)))))
13266
13267 (defun gnus-kill-all-overlays ()
13268   "Delete all overlays in the current buffer."
13269   (when (fboundp 'overlay-lists)
13270     (let* ((overlayss (overlay-lists))
13271            (buffer-read-only nil)
13272            (overlays (nconc (car overlayss) (cdr overlayss))))
13273       (while overlays
13274         (delete-overlay (pop overlays))))))
13275
13276 (defun gnus-request-article-this-buffer (article group)
13277   "Get an article and insert it into this buffer."
13278   (let (do-update-line)
13279     (prog1
13280         (save-excursion
13281           (erase-buffer)
13282           (gnus-kill-all-overlays)
13283           (setq group (or group gnus-newsgroup-name))
13284
13285           ;; Open server if it has closed.
13286           (gnus-check-server (gnus-find-method-for-group group))
13287
13288           ;; Using `gnus-request-article' directly will insert the article into
13289           ;; `nntp-server-buffer' - so we'll save some time by not having to
13290           ;; copy it from the server buffer into the article buffer.
13291
13292           ;; We only request an article by message-id when we do not have the
13293           ;; headers for it, so we'll have to get those.
13294           (when (stringp article)
13295             (let ((gnus-override-method gnus-refer-article-method))
13296               (gnus-read-header article)))
13297
13298           ;; If the article number is negative, that means that this article
13299           ;; doesn't belong in this newsgroup (possibly), so we find its
13300           ;; message-id and request it by id instead of number.
13301           (when (and (numberp article)
13302                      gnus-summary-buffer
13303                      (get-buffer gnus-summary-buffer)
13304                      (buffer-name (get-buffer gnus-summary-buffer)))
13305             (save-excursion
13306               (set-buffer gnus-summary-buffer)
13307               (let ((header (gnus-summary-article-header article)))
13308                 (if (< article 0)
13309                     (cond 
13310                      ((memq article gnus-newsgroup-sparse)
13311                       ;; This is a sparse gap article.
13312                       (setq do-update-line article)
13313                       (setq article (mail-header-id header))
13314                       (let ((gnus-override-method gnus-refer-article-method))
13315                         (gnus-read-header article)))
13316                      ((vectorp header)
13317                       ;; It's a real article.
13318                       (setq article (mail-header-id header)))
13319                      (t
13320                       ;; It is an extracted pseudo-article.
13321                       (setq article 'pseudo)
13322                       (gnus-request-pseudo-article header))))
13323                 
13324                 (let ((method (gnus-find-method-for-group 
13325                                gnus-newsgroup-name)))
13326                   (if (not (eq (car method) 'nneething))
13327                       ()
13328                     (let ((dir (concat (file-name-as-directory (nth 1 method))
13329                                        (mail-header-subject header))))
13330                       (if (file-directory-p dir)
13331                           (progn
13332                             (setq article 'nneething)
13333                             (gnus-group-enter-directory dir)))))))))
13334
13335           (cond
13336            ;; We first check `gnus-original-article-buffer'.
13337            ((and (get-buffer gnus-original-article-buffer)
13338                  (save-excursion
13339                    (set-buffer gnus-original-article-buffer)
13340                    (and (equal (car gnus-original-article) group)
13341                         (eq (cdr gnus-original-article) article))))
13342             (insert-buffer-substring gnus-original-article-buffer)
13343             'article)
13344            ;; Check the backlog.
13345            ((and gnus-keep-backlog
13346                  (gnus-backlog-request-article group article (current-buffer)))
13347             'article)
13348            ;; Check the cache.
13349            ((and gnus-use-cache
13350                  (numberp article)
13351                  (gnus-cache-request-article article group))
13352             'article)
13353            ;; Get the article and put into the article buffer.
13354            ((or (stringp article) (numberp article))
13355             (let ((gnus-override-method
13356                    (and (stringp article) gnus-refer-article-method))
13357                   (buffer-read-only nil))
13358               (erase-buffer)
13359               (gnus-kill-all-overlays)
13360               (if (gnus-request-article article group (current-buffer))
13361                   (progn
13362                     (and gnus-keep-backlog
13363                          (numberp article)
13364                          (gnus-backlog-enter-article
13365                           group article (current-buffer)))
13366                     'article))))
13367            ;; It was a pseudo.
13368            (t article)))
13369
13370       ;; Take the article from the original article buffer
13371       ;; and place it in the buffer it's supposed to be in.
13372       (when (and (get-buffer gnus-article-buffer)
13373                  (equal (buffer-name (current-buffer))
13374                         (buffer-name (get-buffer gnus-article-buffer))))
13375         (save-excursion
13376           (if (get-buffer gnus-original-article-buffer)
13377               (set-buffer (get-buffer gnus-original-article-buffer))
13378             (set-buffer (get-buffer-create gnus-original-article-buffer))
13379             (buffer-disable-undo (current-buffer))
13380             (setq major-mode 'gnus-original-article-mode)
13381             (setq buffer-read-only t)
13382             (gnus-add-current-to-buffer-list))
13383           (let (buffer-read-only)
13384             (erase-buffer)
13385             (insert-buffer-substring gnus-article-buffer))
13386           (setq gnus-original-article (cons group article))))
13387     
13388       ;; Update sparse articles.
13389       (when do-update-line
13390         (save-excursion
13391           (set-buffer gnus-summary-buffer)
13392           (gnus-summary-update-article do-update-line)
13393           (gnus-summary-goto-subject do-update-line)
13394           (set-window-point (get-buffer-window (current-buffer) t)
13395                             (point)))))))
13396
13397 (defun gnus-read-header (id &optional header)
13398   "Read the headers of article ID and enter them into the Gnus system."
13399   (let ((group gnus-newsgroup-name)
13400         where)
13401     ;; First we check to see whether the header in question is already
13402     ;; fetched.
13403     (if (stringp id)
13404         ;; This is a Message-ID.
13405         (setq header (or header (gnus-id-to-header id)))
13406       ;; This is an article number.
13407       (setq header (or header (gnus-summary-article-header id))))
13408     (if (and header
13409              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
13410         ;; We have found the header.
13411         header
13412       ;; We have to really fetch the header to this article.
13413       (when (setq where
13414                   (if (gnus-check-backend-function 'request-head group)
13415                       (gnus-request-head id group)
13416                     (gnus-request-article id group)))
13417         (save-excursion
13418           (set-buffer nntp-server-buffer)
13419           (and (search-forward "\n\n" nil t)
13420                (delete-region (1- (point)) (point-max)))
13421           (goto-char (point-max))
13422           (insert ".\n")
13423           (goto-char (point-min))
13424           (insert "211 ")
13425           (princ (cond
13426                   ((numberp id) id)
13427                   ((cdr where) (cdr where))
13428                   (header (mail-header-number header))
13429                   (t gnus-reffed-article-number))
13430                  (current-buffer))
13431           (insert " Article retrieved.\n"))
13432         ;(when (and header
13433         ;          (memq (mail-header-number header) gnus-newsgroup-sparse))
13434         ;  (setcar (gnus-id-to-thread id) nil))
13435         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13436             ()                          ; Malformed head.
13437           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
13438             (if (and (stringp id)
13439                      (not (string= (gnus-group-real-name group)
13440                                    (car where))))
13441                 ;; If we fetched by Message-ID and the article came
13442                 ;; from a different group, we fudge some bogus article
13443                 ;; numbers for this article.
13444                 (mail-header-set-number header gnus-reffed-article-number))
13445             (decf gnus-reffed-article-number)
13446             (push header gnus-newsgroup-headers)
13447             (setq gnus-current-headers header)
13448             (push (mail-header-number header) gnus-newsgroup-limit))
13449           header)))))
13450
13451 (defun gnus-article-prepare (article &optional all-headers header)
13452   "Prepare ARTICLE in article mode buffer.
13453 ARTICLE should either be an article number or a Message-ID.
13454 If ARTICLE is an id, HEADER should be the article headers.
13455 If ALL-HEADERS is non-nil, no headers are hidden."
13456   (save-excursion
13457     ;; Make sure we start in a summary buffer.
13458     (unless (eq major-mode 'gnus-summary-mode)
13459       (set-buffer gnus-summary-buffer))
13460     (setq gnus-summary-buffer (current-buffer))
13461     ;; Make sure the connection to the server is alive.
13462     (unless (gnus-server-opened
13463              (gnus-find-method-for-group gnus-newsgroup-name))
13464       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13465       (gnus-request-group gnus-newsgroup-name t))
13466     (let* ((article (if header (mail-header-number header) article))
13467            (summary-buffer (current-buffer))
13468            (internal-hook gnus-article-internal-prepare-hook)
13469            (group gnus-newsgroup-name)
13470            result)
13471       (save-excursion
13472         (gnus-article-setup-buffer)
13473         (set-buffer gnus-article-buffer)
13474         ;; Deactivate active regions.
13475         (when (and (boundp 'transient-mark-mode)
13476                    transient-mark-mode)
13477           (setq mark-active nil))
13478         (if (not (setq result (let ((buffer-read-only nil))
13479                                 (gnus-request-article-this-buffer
13480                                  article group))))
13481             ;; There is no such article.
13482             (save-excursion
13483               (when (and (numberp article)
13484                          (not (memq article gnus-newsgroup-sparse)))
13485                 (setq gnus-article-current
13486                       (cons gnus-newsgroup-name article))
13487                 (set-buffer gnus-summary-buffer)
13488                 (setq gnus-current-article article)
13489                 (gnus-summary-mark-article article gnus-canceled-mark))
13490               (unless (memq article gnus-newsgroup-sparse)
13491                 (gnus-message
13492                  1 "No such article (may have expired or been canceled)")
13493                 (ding)
13494                 nil))
13495           (if (or (eq result 'pseudo) (eq result 'nneething))
13496               (progn
13497                 (save-excursion
13498                   (set-buffer summary-buffer)
13499                   (setq gnus-last-article gnus-current-article
13500                         gnus-newsgroup-history (cons gnus-current-article
13501                                                      gnus-newsgroup-history)
13502                         gnus-current-article 0
13503                         gnus-current-headers nil
13504                         gnus-article-current nil)
13505                   (if (eq result 'nneething)
13506                       (gnus-configure-windows 'summary)
13507                     (gnus-configure-windows 'article))
13508                   (gnus-set-global-variables))
13509                 (gnus-set-mode-line 'article))
13510             ;; The result from the `request' was an actual article -
13511             ;; or at least some text that is now displayed in the
13512             ;; article buffer.
13513             (if (and (numberp article)
13514                      (not (eq article gnus-current-article)))
13515                 ;; Seems like a new article has been selected.
13516                 ;; `gnus-current-article' must be an article number.
13517                 (save-excursion
13518                   (set-buffer summary-buffer)
13519                   (setq gnus-last-article gnus-current-article
13520                         gnus-newsgroup-history (cons gnus-current-article
13521                                                      gnus-newsgroup-history)
13522                         gnus-current-article article
13523                         gnus-current-headers
13524                         (gnus-summary-article-header gnus-current-article)
13525                         gnus-article-current
13526                         (cons gnus-newsgroup-name gnus-current-article))
13527                   (unless (vectorp gnus-current-headers)
13528                     (setq gnus-current-headers nil))
13529                   (gnus-summary-show-thread)
13530                   (run-hooks 'gnus-mark-article-hook)
13531                   (gnus-set-mode-line 'summary)
13532                   (and (gnus-visual-p 'article-highlight 'highlight)
13533                        (run-hooks 'gnus-visual-mark-article-hook))
13534                   ;; Set the global newsgroup variables here.
13535                   ;; Suggested by Jim Sisolak
13536                   ;; <sisolak@trans4.neep.wisc.edu>.
13537                   (gnus-set-global-variables)
13538                   (setq gnus-have-all-headers
13539                         (or all-headers gnus-show-all-headers))
13540                   (and gnus-use-cache
13541                        (vectorp (gnus-summary-article-header article))
13542                        (gnus-cache-possibly-enter-article
13543                         group article
13544                         (gnus-summary-article-header article)
13545                         (memq article gnus-newsgroup-marked)
13546                         (memq article gnus-newsgroup-dormant)
13547                         (memq article gnus-newsgroup-unreads)))))
13548             ;; Hooks for getting information from the article.
13549             ;; This hook must be called before being narrowed.
13550             (let (buffer-read-only)
13551               (run-hooks 'internal-hook)
13552               (run-hooks 'gnus-article-prepare-hook)
13553               ;; Decode MIME message.
13554               (if gnus-show-mime
13555                   (if (or (not gnus-strict-mime)
13556                           (gnus-fetch-field "Mime-Version"))
13557                       (funcall gnus-show-mime-method)
13558                     (funcall gnus-decode-encoded-word-method)))
13559               ;; Perform the article display hooks.
13560               (run-hooks 'gnus-article-display-hook))
13561             ;; Do page break.
13562             (goto-char (point-min))
13563             (and gnus-break-pages (gnus-narrow-to-page))
13564             (gnus-set-mode-line 'article)
13565             (gnus-configure-windows 'article)
13566             (goto-char (point-min))
13567             t))))))
13568
13569 (defun gnus-article-show-all-headers ()
13570   "Show all article headers in article mode buffer."
13571   (save-excursion
13572     (gnus-article-setup-buffer)
13573     (set-buffer gnus-article-buffer)
13574     (let ((buffer-read-only nil))
13575       (gnus-unhide-text (point-min) (point-max)))))
13576
13577 (defun gnus-article-hide-headers-if-wanted ()
13578   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13579 Provided for backwards compatibility."
13580   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13581       gnus-inhibit-hiding
13582       (gnus-article-hide-headers)))
13583
13584 (defun gnus-article-hide-headers (&optional arg delete)
13585   "Toggle whether to hide unwanted headers and possibly sort them as well.
13586 If given a negative prefix, always show; if given a positive prefix,
13587 always hide."
13588   (interactive "P")
13589   (unless (gnus-article-check-hidden-text 'headers arg)
13590     ;; This function might be inhibited.
13591     (unless gnus-inhibit-hiding
13592       (save-excursion
13593         (set-buffer gnus-article-buffer)
13594         (save-restriction
13595           (let ((buffer-read-only nil)
13596                 (props (nconc (list 'gnus-type 'headers)
13597                               gnus-hidden-properties))
13598                 (ignored (when (not (stringp gnus-visible-headers))
13599                            (cond ((stringp gnus-ignored-headers)
13600                                   gnus-ignored-headers)
13601                                  ((listp gnus-ignored-headers)
13602                                   (mapconcat 'identity gnus-ignored-headers
13603                                              "\\|")))))
13604                 (visible
13605                  (cond ((stringp gnus-visible-headers)
13606                         gnus-visible-headers)
13607                        ((and gnus-visible-headers
13608                              (listp gnus-visible-headers))
13609                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13610                 want-list beg)
13611             ;; First we narrow to just the headers.
13612             (widen)
13613             (goto-char (point-min))
13614             ;; Hide any "From " lines at the beginning of (mail) articles.
13615             (while (looking-at "From ")
13616               (forward-line 1))
13617             (unless (bobp)
13618               (gnus-hide-text (point-min) (point) props))
13619             ;; Then treat the rest of the header lines.
13620             (narrow-to-region
13621              (point)
13622              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13623             ;; Then we use the two regular expressions
13624             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13625             ;; select which header lines is to remain visible in the
13626             ;; article buffer.
13627             (goto-char (point-min))
13628             (while (re-search-forward "^[^ \t]*:" nil t)
13629               (beginning-of-line)
13630               ;; We add the headers we want to keep to a list and delete
13631               ;; them from the buffer.
13632               (if (or (and visible (looking-at visible))
13633                       (and ignored (not (looking-at ignored))))
13634                   (progn
13635                     (push (buffer-substring
13636                            (setq beg (point))
13637                            (progn
13638                              (forward-line 1)
13639                              ;; Be sure to get multi-line headers...
13640                              (re-search-forward "^[^ \t]*:" nil t)
13641                              (beginning-of-line)
13642                              (point)))
13643                           want-list)
13644                     (delete-region beg (point)))
13645                 (forward-line 1)))
13646             ;; Sort the headers that we want to display.
13647             (setq want-list (sort want-list 'gnus-article-header-less))
13648             (goto-char (point-min))
13649             (while want-list
13650               (insert (pop want-list)))
13651             ;; We make the unwanted headers invisible.
13652             (if delete
13653                 (delete-region (point-min) (point-max))
13654               ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13655               (gnus-hide-text-type (point) (point-max) 'headers))))))))
13656
13657 (defsubst gnus-article-header-rank (header)
13658   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13659   (let ((list gnus-sorted-header-list)
13660         (i 0))
13661     (while list
13662       (when (string-match (car list) header)
13663         (setq list nil))
13664       (setq list (cdr list))
13665       (incf i))
13666     i))
13667
13668 (defun gnus-article-header-less (h1 h2)
13669   "Say whether string H1 is \"less\" than string H2."
13670   (< (gnus-article-header-rank h1)
13671      (gnus-article-header-rank h2)))
13672
13673 (defun gnus-article-hide-boring-headers (&optional arg)
13674   "Toggle hiding of headers that aren't very interesting.
13675 If given a negative prefix, always show; if given a positive prefix,
13676 always hide."
13677   (interactive "P")
13678   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13679     (save-excursion
13680       (set-buffer gnus-article-buffer)
13681       (save-restriction
13682         (let ((buffer-read-only nil)
13683               (list gnus-boring-article-headers)
13684               (inhibit-point-motion-hooks t)
13685               elem)
13686           (nnheader-narrow-to-headers)
13687           (while list
13688             (setq elem (pop list))
13689             (goto-char (point-min))
13690             (cond
13691              ;; Hide empty headers.
13692              ((eq elem 'empty)
13693               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
13694                 (forward-line -1)
13695                 (gnus-hide-text-type
13696                  (progn (beginning-of-line) (point))
13697                  (progn 
13698                    (end-of-line)
13699                    (if (re-search-forward "^[^ \t]" nil t)
13700                        (match-beginning 0)
13701                      (point-max)))
13702                  'boring-headers)))
13703              ;; Hide boring Newsgroups header.
13704              ((eq elem 'newsgroups)
13705               (when (equal (mail-fetch-field "newsgroups")
13706                            (gnus-group-real-name gnus-newsgroup-name))
13707                 (gnus-article-hide-header "newsgroups")))
13708              ((eq elem 'followup-to)
13709               (when (equal (mail-fetch-field "followup-to")
13710                            (mail-fetch-field "newsgroups"))
13711                 (gnus-article-hide-header "followup-to")))
13712              ((eq elem 'reply-to)
13713               (let ((from (mail-fetch-field "from"))
13714                     (reply-to (mail-fetch-field "reply-to")))
13715                 (when (and
13716                        from reply-to
13717                        (equal 
13718                         (nth 1 (funcall gnus-extract-address-components from))
13719                         (nth 1 (funcall gnus-extract-address-components
13720                                         reply-to))))
13721                   (gnus-article-hide-header "reply-to"))))
13722              ((eq elem 'date)
13723               (let ((date (mail-fetch-field "date")))
13724                 (when (and date
13725                            (< (gnus-days-between date (current-time-string))
13726                               4))
13727                   (gnus-article-hide-header "date")))))))))))
13728
13729 (defun gnus-article-hide-header (header)
13730   (save-excursion
13731     (goto-char (point-min))
13732     (when (re-search-forward (concat "^" header ":") nil t)
13733       (gnus-hide-text-type
13734        (progn (beginning-of-line) (point))
13735        (progn 
13736          (end-of-line)
13737          (if (re-search-forward "^[^ \t]" nil t)
13738              (match-beginning 0)
13739            (point-max)))
13740        'boring-headers))))
13741
13742 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
13743 (defun gnus-article-treat-overstrike ()
13744   "Translate overstrikes into bold text."
13745   (interactive)
13746   (save-excursion
13747     (set-buffer gnus-article-buffer)
13748     (let ((buffer-read-only nil))
13749       (while (search-forward "\b" nil t)
13750         (let ((next (following-char))
13751               (previous (char-after (- (point) 2))))
13752           (cond ((eq next previous)
13753                  (put-text-property (- (point) 2) (point) 'invisible t)
13754                  (put-text-property (point) (1+ (point)) 'face 'bold))
13755                 ((eq next ?_)
13756                  (put-text-property (1- (point)) (1+ (point)) 'invisible t)
13757                  (put-text-property
13758                   (- (point) 2) (1- (point)) 'face 'underline))
13759                 ((eq previous ?_)
13760                  (put-text-property (- (point) 2) (point) 'invisible t)
13761                  (put-text-property
13762                   (point) (1+ (point))  'face 'underline))))))))
13763
13764 (defun gnus-article-word-wrap ()
13765   "Format too long lines."
13766   (interactive)
13767   (save-excursion
13768     (set-buffer gnus-article-buffer)
13769     (let ((buffer-read-only nil))
13770       (widen)
13771       (goto-char (point-min))
13772       (search-forward "\n\n" nil t)
13773       (end-of-line 1)
13774       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
13775             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
13776             (adaptive-fill-mode t))
13777         (while (not (eobp))
13778           (and (>= (current-column) (min fill-column (window-width)))
13779                (/= (preceding-char) ?:)
13780                (fill-paragraph nil))
13781           (end-of-line 2))))))
13782
13783 (defun gnus-article-remove-cr ()
13784   "Remove carriage returns from an article."
13785   (interactive)
13786   (save-excursion
13787     (set-buffer gnus-article-buffer)
13788     (let ((buffer-read-only nil))
13789       (goto-char (point-min))
13790       (while (search-forward "\r" nil t)
13791         (replace-match "" t t)))))
13792
13793 (defun gnus-article-remove-trailing-blank-lines ()
13794   "Remove all trailing blank lines from the article."
13795   (interactive)
13796   (save-excursion
13797     (set-buffer gnus-article-buffer)
13798     (let ((buffer-read-only nil))
13799       (goto-char (point-max))
13800       (delete-region
13801        (point)
13802        (progn
13803          (while (looking-at "^[ \t]*$")
13804            (forward-line -1))
13805          (forward-line 1)
13806          (point))))))
13807
13808 (defun gnus-article-display-x-face (&optional force)
13809   "Look for an X-Face header and display it if present."
13810   (interactive (list 'force))
13811   (save-excursion
13812     (set-buffer gnus-article-buffer)
13813     ;; Delete the old process, if any.
13814     (when (process-status "gnus-x-face")
13815       (delete-process "gnus-x-face"))
13816     (let ((inhibit-point-motion-hooks t)
13817           (case-fold-search nil)
13818           from)
13819       (save-restriction
13820         (nnheader-narrow-to-headers)
13821         (setq from (mail-fetch-field "from"))
13822         (goto-char (point-min))
13823         (when (and gnus-article-x-face-command
13824                    (or force
13825                        ;; Check whether this face is censored.
13826                        (not gnus-article-x-face-too-ugly)
13827                        (and gnus-article-x-face-too-ugly from
13828                             (not (string-match gnus-article-x-face-too-ugly
13829                                                from))))
13830                    ;; Has to be present.
13831                    (re-search-forward "^X-Face: " nil t))
13832           ;; We now have the area of the buffer where the X-Face is stored.
13833           (let ((beg (point))
13834                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
13835             ;; We display the face.
13836             (if (symbolp gnus-article-x-face-command)
13837                 ;; The command is a lisp function, so we call it.
13838                 (if (gnus-functionp gnus-article-x-face-command)
13839                     (funcall gnus-article-x-face-command beg end)
13840                   (error "%s is not a function" gnus-article-x-face-command))
13841               ;; The command is a string, so we interpret the command
13842               ;; as a, well, command, and fork it off.
13843               (let ((process-connection-type nil))
13844                 (process-kill-without-query
13845                  (start-process
13846                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
13847                 (process-send-region "gnus-x-face" beg end)
13848                 (process-send-eof "gnus-x-face")))))))))
13849
13850 (defun gnus-headers-decode-quoted-printable ()
13851   "Hack to remove QP encoding from headers."
13852   (let ((case-fold-search t)
13853         (inhibit-point-motion-hooks t)
13854         string)
13855     (goto-char (point-min))
13856     (while (re-search-forward "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
13857       (setq string (match-string 1))
13858       (narrow-to-region (match-beginning 0) (match-end 0))
13859       (delete-region (point-min) (point-max))
13860       (insert string)
13861       (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
13862       (subst-char-in-region (point-min) (point-max) ?_ ? )
13863       (widen)
13864       (goto-char (point-min)))))
13865
13866 (defun gnus-article-de-quoted-unreadable (&optional force)
13867   "Do a naive translation of a quoted-printable-encoded article.
13868 This is in no way, shape or form meant as a replacement for real MIME
13869 processing, but is simply a stop-gap measure until MIME support is
13870 written.
13871 If FORCE, decode the article whether it is marked as quoted-printable
13872 or not."
13873   (interactive (list 'force))
13874   (save-excursion
13875     (set-buffer gnus-article-buffer)
13876     (let ((case-fold-search t)
13877           (buffer-read-only nil)
13878           (type (gnus-fetch-field "content-transfer-encoding")))
13879       (when (or force
13880                 (and type (string-match "quoted-printable" type)))
13881         (gnus-headers-decode-quoted-printable)
13882         (goto-char (point-min))
13883         (search-forward "\n\n" nil 'move)
13884         (gnus-mime-decode-quoted-printable (point) (point-max))))))
13885
13886 (defun gnus-mime-decode-quoted-printable (from to)
13887   "Decode Quoted-Printable in the region between FROM and TO."
13888   (goto-char from)
13889   (while (search-forward "=" to t)
13890     (cond ((eq (following-char) ?\n)
13891            (delete-char -1)
13892            (delete-char 1))
13893           ((looking-at "[0-9A-F][0-9A-F]")
13894            (delete-char -1)
13895            (insert (hexl-hex-string-to-integer
13896                     (buffer-substring (point) (+ 2 (point)))))
13897            (delete-char 2))
13898           ((looking-at "=")
13899            (delete-char 1))
13900           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
13901
13902 (defun gnus-article-hide-pgp (&optional arg)
13903   "Toggle hiding of any PGP headers and signatures in the current article.
13904 If given a negative prefix, always show; if given a positive prefix,
13905 always hide."
13906   (interactive "P")
13907   (unless (gnus-article-check-hidden-text 'pgp arg)
13908     (save-excursion
13909       (set-buffer gnus-article-buffer)
13910       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
13911             buffer-read-only beg end)
13912         (widen)
13913         (goto-char (point-min))
13914         ;; Hide the "header".
13915         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
13916              (gnus-hide-text (match-beginning 0) (match-end 0) props))
13917         (setq beg (point))
13918         ;; Hide the actual signature.
13919         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
13920              (setq end (1+ (match-beginning 0)))
13921              (gnus-hide-text
13922               end
13923               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
13924                   (match-end 0)
13925                 ;; Perhaps we shouldn't hide to the end of the buffer
13926                 ;; if there is no end to the signature?
13927                 (point-max))
13928               props))
13929         ;; Hide "- " PGP quotation markers.
13930         (when (and beg end)
13931           (narrow-to-region beg end)
13932           (goto-char (point-min))
13933           (while (re-search-forward "^- " nil t)
13934             (gnus-hide-text (match-beginning 0) (match-end 0) props))
13935           (widen))))))
13936
13937 (defun gnus-article-hide-signature (&optional arg)
13938   "Hide the signature in the current article.
13939 If given a negative prefix, always show; if given a positive prefix,
13940 always hide."
13941   (interactive "P")
13942   (unless (gnus-article-check-hidden-text 'signature arg)
13943     (save-excursion
13944       (set-buffer gnus-article-buffer)
13945       (save-restriction
13946         (let ((buffer-read-only nil))
13947           (when (gnus-narrow-to-signature)
13948             (gnus-hide-text-type (point-min) (point-max) 'signature)))))))
13949
13950 (defun gnus-article-strip-leading-blank-lines ()
13951   "Remove all blank lines from the beginning of the article."
13952   (interactive)
13953   (save-excursion
13954     (set-buffer gnus-article-buffer)
13955     (let (buffer-read-only)
13956       (goto-char (point-min))
13957       (when (search-forward "\n\n" nil t)
13958         (while (looking-at "[ \t]$")
13959           (gnus-delete-line))))))
13960
13961 (defun gnus-narrow-to-signature ()
13962   "Narrow to the signature."
13963   (widen)
13964   (goto-char (point-max))
13965   (when (re-search-backward gnus-signature-separator nil t)
13966     (forward-line 1)
13967     (when (or (null gnus-signature-limit)
13968               (and (numberp gnus-signature-limit)
13969                    (< (- (point-max) (point)) gnus-signature-limit))
13970               (and (gnus-functionp gnus-signature-limit)
13971                    (funcall gnus-signature-limit))
13972               (and (stringp gnus-signature-limit)
13973                    (not (re-search-forward gnus-signature-limit nil t))))
13974       (narrow-to-region (point) (point-max))
13975       t)))
13976
13977 (defun gnus-article-check-hidden-text (type arg)
13978   "Return nil if hiding is necessary."
13979   (save-excursion
13980     (set-buffer gnus-article-buffer)
13981     (let ((hide (gnus-article-hidden-text-p type)))
13982       (cond ((or (and (null arg) (eq hide 'hidden))
13983                  (and arg (< 0 (prefix-numeric-value arg))))
13984              (gnus-article-show-hidden-text type))
13985             ((eq hide 'shown)
13986              (gnus-article-show-hidden-text type t))
13987             (t nil)))))
13988
13989 (defun gnus-article-hidden-text-p (type)
13990   "Say whether the current buffer contains hidden text of type TYPE."
13991   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type)))
13992     (when pos
13993       (if (get-text-property pos 'invisible)
13994           'hidden
13995         'shown))))
13996
13997 (defun gnus-article-hide (&optional arg force)
13998   "Hide all the gruft in the current article.
13999 This means that PGP stuff, signatures, cited text and (some)
14000 headers will be hidden.
14001 If given a prefix, show the hidden text instead."
14002   (interactive (list current-prefix-arg 'force))
14003   (gnus-article-hide-headers arg)
14004   (gnus-article-hide-pgp arg)
14005   (gnus-article-hide-citation-maybe arg force)
14006   (gnus-article-hide-signature arg))
14007
14008 (defun gnus-article-show-hidden-text (type &optional hide)
14009   "Show all hidden text of type TYPE.
14010 If HIDE, hide the text instead."
14011   (save-excursion
14012     (set-buffer gnus-article-buffer)
14013     (let ((buffer-read-only nil)
14014           (inhibit-point-motion-hooks t)
14015           (beg (point-min)))
14016       (while (gnus-goto-char (text-property-any
14017                               beg (point-max) 'gnus-type type))
14018         (setq beg (point))
14019         (forward-char)
14020         (if hide
14021             (gnus-hide-text beg (point) gnus-hidden-properties)
14022           (gnus-unhide-text beg (point)))
14023         (setq beg (point)))
14024       t)))
14025
14026 (defvar gnus-article-time-units
14027   `((year . ,(* 365.25 24 60 60))
14028     (week . ,(* 7 24 60 60))
14029     (day . ,(* 24 60 60))
14030     (hour . ,(* 60 60))
14031     (minute . 60)
14032     (second . 1))
14033   "Mapping from time units to seconds.")
14034
14035 (defun gnus-article-date-ut (&optional type highlight)
14036   "Convert DATE date to universal time in the current article.
14037 If TYPE is `local', convert to local time; if it is `lapsed', output
14038 how much time has lapsed since DATE."
14039   (interactive (list 'ut t))
14040   (let* ((header (or gnus-current-headers
14041                      (gnus-summary-article-header) ""))
14042          (date (and (vectorp header) (mail-header-date header)))
14043          (date-regexp "^Date: \\|^X-Sent: ")
14044          (now (current-time))
14045          (inhibit-point-motion-hooks t)
14046          bface eface)
14047     (when (and date (not (string= date "")))
14048       (save-excursion
14049         (set-buffer gnus-article-buffer)
14050         (save-restriction
14051           (nnheader-narrow-to-headers)
14052           (let ((buffer-read-only nil))
14053             ;; Delete any old Date headers.
14054             (if (re-search-forward date-regexp nil t)
14055                 (progn
14056                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
14057                         eface (get-text-property (gnus-point-at-eol) 'face))
14058                   (message-remove-header date-regexp t)
14059                   (beginning-of-line))
14060               (goto-char (point-max)))
14061             (insert
14062              (cond
14063               ;; Convert to the local timezone.  We have to slap a
14064               ;; `condition-case' round the calls to the timezone
14065               ;; functions since they aren't particularly resistant to
14066               ;; buggy dates.
14067               ((eq type 'local)
14068                (concat "Date: " (condition-case ()
14069                                     (timezone-make-date-arpa-standard date)
14070                                   (error date))
14071                        "\n"))
14072               ;; Convert to Universal Time.
14073               ((eq type 'ut)
14074                (concat "Date: "
14075                        (condition-case ()
14076                            (timezone-make-date-arpa-standard date nil "UT")
14077                          (error date))
14078                        "\n"))
14079               ;; Get the original date from the article.
14080               ((eq type 'original)
14081                (concat "Date: " date "\n"))
14082               ;; Do an X-Sent lapsed format.
14083               ((eq type 'lapsed)
14084                ;; If the date is seriously mangled, the timezone
14085                ;; functions are liable to bug out, so we condition-case
14086                ;; the entire thing.
14087                (let* ((real-time
14088                        (condition-case ()
14089                            (gnus-time-minus
14090                             (gnus-encode-date
14091                              (timezone-make-date-arpa-standard
14092                               (current-time-string now)
14093                               (current-time-zone now) "UT"))
14094                             (gnus-encode-date
14095                              (timezone-make-date-arpa-standard
14096                               date nil "UT")))
14097                          (error '(0 0))))
14098                       (real-sec (+ (* (float (car real-time)) 65536)
14099                                    (cadr real-time)))
14100                       (sec (abs real-sec))
14101                       num prev)
14102                  (if (zerop sec)
14103                      "X-Sent: Now\n"
14104                    (concat
14105                     "X-Sent: "
14106                     ;; This is a bit convoluted, but basically we go
14107                     ;; through the time units for years, weeks, etc,
14108                     ;; and divide things to see whether that results
14109                     ;; in positive answers.
14110                     (mapconcat
14111                      (lambda (unit)
14112                        (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
14113                            ;; The (remaining) seconds are too few to
14114                            ;; be divided into this time unit.
14115                            ""
14116                          ;; It's big enough, so we output it.
14117                          (setq sec (- sec (* num (cdr unit))))
14118                          (prog1
14119                              (concat (if prev ", " "") (int-to-string
14120                                                         (floor num))
14121                                      " " (symbol-name (car unit))
14122                                      (if (> num 1) "s" ""))
14123                            (setq prev t))))
14124                      gnus-article-time-units "")
14125                     ;; If dates are odd, then it might appear like the
14126                     ;; article was sent in the future.
14127                     (if (> real-sec 0)
14128                         " ago\n"
14129                       " in the future\n")))))
14130               (t
14131                (error "Unknown conversion type: %s" type)))))
14132           ;; Do highlighting.
14133           (beginning-of-line)
14134           (when (and highlight (gnus-visual-p 'article-highlight 'highlight)
14135                      (looking-at "\\([^:]\\): *\\(.*\\)$"))
14136             (put-text-property (match-beginning 1) (match-end 1)
14137                                'face bface)
14138             (put-text-property (match-beginning 2) (match-end 2)
14139                                'face eface)))))))
14140
14141 (defun gnus-article-date-local (&optional highlight)
14142   "Convert the current article date to the local timezone."
14143   (interactive (list t))
14144   (gnus-article-date-ut 'local highlight))
14145
14146 (defun gnus-article-date-original (&optional highlight)
14147   "Convert the current article date to what it was originally.
14148 This is only useful if you have used some other date conversion
14149 function and want to see what the date was before converting."
14150   (interactive (list t))
14151   (gnus-article-date-ut 'original highlight))
14152
14153 (defun gnus-article-date-lapsed (&optional highlight)
14154   "Convert the current article date to time lapsed since it was sent."
14155   (interactive (list t))
14156   (gnus-article-date-ut 'lapsed highlight))
14157
14158 (defun gnus-article-maybe-highlight ()
14159   "Do some article highlighting if `gnus-visual' is non-nil."
14160   (if (gnus-visual-p 'article-highlight 'highlight)
14161       (gnus-article-highlight-some)))
14162
14163 ;; Article savers.
14164
14165 (defun gnus-output-to-rmail (file-name)
14166   "Append the current article to an Rmail file named FILE-NAME."
14167   (require 'rmail)
14168   ;; Most of these codes are borrowed from rmailout.el.
14169   (setq file-name (expand-file-name file-name))
14170   (setq rmail-default-rmail-file file-name)
14171   (let ((artbuf (current-buffer))
14172         (tmpbuf (get-buffer-create " *Gnus-output*")))
14173     (save-excursion
14174       (or (get-file-buffer file-name)
14175           (file-exists-p file-name)
14176           (if (gnus-yes-or-no-p
14177                (concat "\"" file-name "\" does not exist, create it? "))
14178               (let ((file-buffer (create-file-buffer file-name)))
14179                 (save-excursion
14180                   (set-buffer file-buffer)
14181                   (rmail-insert-rmail-file-header)
14182                   (let ((require-final-newline nil))
14183                     (write-region (point-min) (point-max) file-name t 1)))
14184                 (kill-buffer file-buffer))
14185             (error "Output file does not exist")))
14186       (set-buffer tmpbuf)
14187       (buffer-disable-undo (current-buffer))
14188       (erase-buffer)
14189       (insert-buffer-substring artbuf)
14190       (gnus-convert-article-to-rmail)
14191       ;; Decide whether to append to a file or to an Emacs buffer.
14192       (let ((outbuf (get-file-buffer file-name)))
14193         (if (not outbuf)
14194             (append-to-file (point-min) (point-max) file-name)
14195           ;; File has been visited, in buffer OUTBUF.
14196           (set-buffer outbuf)
14197           (let ((buffer-read-only nil)
14198                 (msg (and (boundp 'rmail-current-message)
14199                           (symbol-value 'rmail-current-message))))
14200             ;; If MSG is non-nil, buffer is in RMAIL mode.
14201             (if msg
14202                 (progn (widen)
14203                        (narrow-to-region (point-max) (point-max))))
14204             (insert-buffer-substring tmpbuf)
14205             (if msg
14206                 (progn
14207                   (goto-char (point-min))
14208                   (widen)
14209                   (search-backward "\^_")
14210                   (narrow-to-region (point) (point-max))
14211                   (goto-char (1+ (point-min)))
14212                   (rmail-count-new-messages t)
14213                   (rmail-show-message msg)))))))
14214     (kill-buffer tmpbuf)))
14215
14216 (defun gnus-output-to-file (file-name)
14217   "Append the current article to a file named FILE-NAME."
14218   (setq file-name (expand-file-name file-name))
14219   (let ((artbuf (current-buffer))
14220         (tmpbuf (get-buffer-create " *Gnus-output*")))
14221     (save-excursion
14222       (set-buffer tmpbuf)
14223       (buffer-disable-undo (current-buffer))
14224       (erase-buffer)
14225       (insert-buffer-substring artbuf)
14226       ;; Append newline at end of the buffer as separator, and then
14227       ;; save it to file.
14228       (goto-char (point-max))
14229       (insert "\n")
14230       (append-to-file (point-min) (point-max) file-name))
14231     (kill-buffer tmpbuf)))
14232
14233 (defun gnus-convert-article-to-rmail ()
14234   "Convert article in current buffer to Rmail message format."
14235   (let ((buffer-read-only nil))
14236     ;; Convert article directly into Babyl format.
14237     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14238     (goto-char (point-min))
14239     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14240     (while (search-forward "\n\^_" nil t) ;single char
14241       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14242     (goto-char (point-max))
14243     (insert "\^_")))
14244
14245 (defun gnus-narrow-to-page (&optional arg)
14246   "Narrow the article buffer to a page.
14247 If given a numerical ARG, move forward ARG pages."
14248   (interactive "P")
14249   (setq arg (if arg (prefix-numeric-value arg) 0))
14250   (save-excursion
14251     (set-buffer gnus-article-buffer)
14252     (goto-char (point-min))
14253     (widen)
14254     (when (gnus-visual-p 'page-marker)
14255       (let ((buffer-read-only nil))
14256         (gnus-remove-text-with-property 'gnus-prev)
14257         (gnus-remove-text-with-property 'gnus-next)))
14258     (when
14259         (cond ((< arg 0)
14260                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14261               ((> arg 0)
14262                (re-search-forward page-delimiter nil 'move arg)))
14263       (goto-char (match-end 0)))
14264     (narrow-to-region
14265      (point)
14266      (if (re-search-forward page-delimiter nil 'move)
14267          (match-beginning 0)
14268        (point)))
14269     (when (and (gnus-visual-p 'page-marker)
14270                (not (= (point-min) 1)))
14271       (save-excursion
14272         (goto-char (point-min))
14273         (gnus-insert-prev-page-button)))
14274     (when (and (gnus-visual-p 'page-marker)
14275                (not (= (1- (point-max)) (buffer-size))))
14276       (save-excursion
14277         (goto-char (point-max))
14278         (gnus-insert-next-page-button)))))
14279
14280 ;; Article mode commands
14281
14282 (defun gnus-article-goto-next-page ()
14283   "Show the next page of the article."
14284   (interactive)
14285   (when (gnus-article-next-page)
14286     (gnus-article-read-summary-keys nil ?n)))
14287
14288 (defun gnus-article-goto-prev-page ()
14289   "Show the next page of the article."
14290   (interactive)
14291   (if (bobp) (gnus-article-read-summary-keys nil ?n)
14292     (gnus-article-prev-page nil)))
14293
14294 (defun gnus-article-next-page (&optional lines)
14295   "Show the next page of the current article.
14296 If end of article, return non-nil.  Otherwise return nil.
14297 Argument LINES specifies lines to be scrolled up."
14298   (interactive "p")
14299   (move-to-window-line -1)
14300   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14301   (if (save-excursion
14302         (end-of-line)
14303         (and (pos-visible-in-window-p)  ;Not continuation line.
14304              (eobp)))
14305       ;; Nothing in this page.
14306       (if (or (not gnus-break-pages)
14307               (save-excursion
14308                 (save-restriction
14309                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14310           t                             ;Nothing more.
14311         (gnus-narrow-to-page 1)         ;Go to next page.
14312         nil)
14313     ;; More in this page.
14314     (condition-case ()
14315         (scroll-up lines)
14316       (end-of-buffer
14317        ;; Long lines may cause an end-of-buffer error.
14318        (goto-char (point-max))))
14319     (move-to-window-line 0)
14320     nil))
14321
14322 (defun gnus-article-prev-page (&optional lines)
14323   "Show previous page of current article.
14324 Argument LINES specifies lines to be scrolled down."
14325   (interactive "p")
14326   (move-to-window-line 0)
14327   (if (and gnus-break-pages
14328            (bobp)
14329            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14330       (progn
14331         (gnus-narrow-to-page -1)        ;Go to previous page.
14332         (goto-char (point-max))
14333         (recenter -1))
14334     (prog1
14335         (condition-case ()
14336             (scroll-down lines)
14337           (error nil))
14338       (move-to-window-line 0))))
14339
14340 (defun gnus-article-refer-article ()
14341   "Read article specified by message-id around point."
14342   (interactive)
14343   (let ((point (point)))
14344     (search-forward ">" nil t)          ;Move point to end of "<....>".
14345     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14346         (let ((message-id (match-string 1)))
14347           (goto-char point)
14348           (set-buffer gnus-summary-buffer)
14349           (gnus-summary-refer-article message-id))
14350       (goto-char (point))
14351       (error "No references around point"))))
14352
14353 (defun gnus-article-show-summary ()
14354   "Reconfigure windows to show summary buffer."
14355   (interactive)
14356   (gnus-configure-windows 'article)
14357   (gnus-summary-goto-subject gnus-current-article))
14358
14359 (defun gnus-article-describe-briefly ()
14360   "Describe article mode commands briefly."
14361   (interactive)
14362   (gnus-message 6
14363                 (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")))
14364
14365 (defun gnus-article-summary-command ()
14366   "Execute the last keystroke in the summary buffer."
14367   (interactive)
14368   (let ((obuf (current-buffer))
14369         (owin (current-window-configuration))
14370         func)
14371     (switch-to-buffer gnus-summary-buffer 'norecord)
14372     (setq func (lookup-key (current-local-map) (this-command-keys)))
14373     (call-interactively func)
14374     (set-buffer obuf)
14375     (set-window-configuration owin)
14376     (set-window-point (get-buffer-window (current-buffer)) (point))))
14377
14378 (defun gnus-article-summary-command-nosave ()
14379   "Execute the last keystroke in the summary buffer."
14380   (interactive)
14381   (let (func)
14382     (pop-to-buffer gnus-summary-buffer 'norecord)
14383     (setq func (lookup-key (current-local-map) (this-command-keys)))
14384     (call-interactively func)))
14385
14386 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14387   "Read a summary buffer key sequence and execute it from the article buffer."
14388   (interactive "P")
14389   (let ((nosaves
14390          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14391            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14392            "=" "^" "\M-^"))
14393         keys)
14394     (save-excursion
14395       (set-buffer gnus-summary-buffer)
14396       (push (or key last-command-event) unread-command-events)
14397       (setq keys (read-key-sequence nil)))
14398     (message "")
14399
14400     (if (member keys nosaves)
14401         (let (func)
14402           (pop-to-buffer gnus-summary-buffer 'norecord)
14403           (if (setq func (lookup-key (current-local-map) keys))
14404               (call-interactively func)
14405             (ding)))
14406       (let ((obuf (current-buffer))
14407             (owin (current-window-configuration))
14408             (opoint (point))
14409             func in-buffer)
14410         (if not-restore-window
14411             (pop-to-buffer gnus-summary-buffer 'norecord)
14412           (switch-to-buffer gnus-summary-buffer 'norecord))
14413         (setq in-buffer (current-buffer))
14414         (if (setq func (lookup-key (current-local-map) keys))
14415             (call-interactively func)
14416           (ding))
14417         (when (eq in-buffer (current-buffer))
14418           (set-buffer obuf)
14419           (unless not-restore-window
14420             (set-window-configuration owin))
14421           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14422
14423 \f
14424 ;;;
14425 ;;; Kill file handling.
14426 ;;;
14427
14428 ;;;###autoload
14429 (defalias 'gnus-batch-kill 'gnus-batch-score)
14430 ;;;###autoload
14431 (defun gnus-batch-score ()
14432   "Run batched scoring.
14433 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14434 Newsgroups is a list of strings in Bnews format.  If you want to score
14435 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14436 score the alt hierarchy, you'd say \"!alt.all\"."
14437   (interactive)
14438   (let* ((yes-and-no
14439           (gnus-newsrc-parse-options
14440            (apply (function concat)
14441                   (mapcar (lambda (g) (concat g " "))
14442                           command-line-args-left))))
14443          (gnus-expert-user t)
14444          (nnmail-spool-file nil)
14445          (gnus-use-dribble-file nil)
14446          (yes (car yes-and-no))
14447          (no (cdr yes-and-no))
14448          group newsrc entry
14449          ;; Disable verbose message.
14450          gnus-novice-user gnus-large-newsgroup)
14451     ;; Eat all arguments.
14452     (setq command-line-args-left nil)
14453     ;; Start Gnus.
14454     (gnus)
14455     ;; Apply kills to specified newsgroups in command line arguments.
14456     (setq newsrc (cdr gnus-newsrc-alist))
14457     (while newsrc
14458       (setq group (caar newsrc))
14459       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14460       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14461                (and (car entry)
14462                     (or (eq (car entry) t)
14463                         (not (zerop (car entry)))))
14464                (if yes (string-match yes group) t)
14465                (or (null no) (not (string-match no group))))
14466           (progn
14467             (gnus-summary-read-group group nil t nil t)
14468             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14469                  (gnus-summary-exit))))
14470       (setq newsrc (cdr newsrc)))
14471     ;; Exit Emacs.
14472     (switch-to-buffer gnus-group-buffer)
14473     (gnus-group-save-newsrc)))
14474
14475 (defun gnus-apply-kill-file ()
14476   "Apply a kill file to the current newsgroup.
14477 Returns the number of articles marked as read."
14478   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14479           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14480       (gnus-apply-kill-file-internal)
14481     0))
14482
14483 (defun gnus-kill-save-kill-buffer ()
14484   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14485     (when (get-file-buffer file)
14486       (save-excursion
14487         (set-buffer (get-file-buffer file))
14488         (and (buffer-modified-p) (save-buffer))
14489         (kill-buffer (current-buffer))))))
14490
14491 (defvar gnus-kill-file-name "KILL"
14492   "Suffix of the kill files.")
14493
14494 (defun gnus-newsgroup-kill-file (newsgroup)
14495   "Return the name of a kill file name for NEWSGROUP.
14496 If NEWSGROUP is nil, return the global kill file name instead."
14497   (cond 
14498    ;; The global KILL file is placed at top of the directory.
14499    ((or (null newsgroup)
14500         (string-equal newsgroup ""))
14501     (expand-file-name gnus-kill-file-name
14502                       (or gnus-kill-files-directory "~/News")))
14503    ;; Append ".KILL" to newsgroup name.
14504    ((gnus-use-long-file-name 'not-kill)
14505     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14506                               "." gnus-kill-file-name)
14507                       (or gnus-kill-files-directory "~/News")))
14508    ;; Place "KILL" under the hierarchical directory.
14509    (t
14510     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14511                               "/" gnus-kill-file-name)
14512                       (or gnus-kill-files-directory "~/News")))))
14513
14514 \f
14515 ;;;
14516 ;;; Dribble file
14517 ;;;
14518
14519 (defvar gnus-dribble-ignore nil)
14520 (defvar gnus-dribble-eval-file nil)
14521
14522 (defun gnus-dribble-file-name ()
14523   "Return the dribble file for the current .newsrc."
14524   (concat
14525    (if gnus-dribble-directory
14526        (concat (file-name-as-directory gnus-dribble-directory)
14527                (file-name-nondirectory gnus-current-startup-file))
14528      gnus-current-startup-file)
14529    "-dribble"))
14530
14531 (defun gnus-dribble-enter (string)
14532   "Enter STRING into the dribble buffer."
14533   (if (and (not gnus-dribble-ignore)
14534            gnus-dribble-buffer
14535            (buffer-name gnus-dribble-buffer))
14536       (let ((obuf (current-buffer)))
14537         (set-buffer gnus-dribble-buffer)
14538         (insert string "\n")
14539         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14540         (set-buffer obuf))))
14541
14542 (defun gnus-dribble-read-file ()
14543   "Read the dribble file from disk."
14544   (let ((dribble-file (gnus-dribble-file-name)))
14545     (save-excursion
14546       (set-buffer (setq gnus-dribble-buffer
14547                         (get-buffer-create
14548                          (file-name-nondirectory dribble-file))))
14549       (gnus-add-current-to-buffer-list)
14550       (erase-buffer)
14551       (setq buffer-file-name dribble-file)
14552       (auto-save-mode t)
14553       (buffer-disable-undo (current-buffer))
14554       (bury-buffer (current-buffer))
14555       (set-buffer-modified-p nil)
14556       (let ((auto (make-auto-save-file-name))
14557             (gnus-dribble-ignore t))
14558         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14559           ;; Load whichever file is newest -- the auto save file
14560           ;; or the "real" file.
14561           (if (file-newer-than-file-p auto dribble-file)
14562               (insert-file-contents auto)
14563             (insert-file-contents dribble-file))
14564           (unless (zerop (buffer-size))
14565             (set-buffer-modified-p t))
14566           ;; Set the file modes to reflect the .newsrc file modes.
14567           (save-buffer)
14568           (when (file-exists-p gnus-current-startup-file)
14569             (set-file-modes dribble-file
14570                             (file-modes gnus-current-startup-file)))
14571           ;; Possibly eval the file later.
14572           (when (gnus-y-or-n-p
14573                  "Auto-save file exists.  Do you want to read it? ")
14574             (setq gnus-dribble-eval-file t)))))))
14575
14576 (defun gnus-dribble-eval-file ()
14577   (if (not gnus-dribble-eval-file)
14578       ()
14579     (setq gnus-dribble-eval-file nil)
14580     (save-excursion
14581       (let ((gnus-dribble-ignore t))
14582         (set-buffer gnus-dribble-buffer)
14583         (eval-buffer (current-buffer))))))
14584
14585 (defun gnus-dribble-delete-file ()
14586   (if (file-exists-p (gnus-dribble-file-name))
14587       (delete-file (gnus-dribble-file-name)))
14588   (if gnus-dribble-buffer
14589       (save-excursion
14590         (set-buffer gnus-dribble-buffer)
14591         (let ((auto (make-auto-save-file-name)))
14592           (if (file-exists-p auto)
14593               (delete-file auto))
14594           (erase-buffer)
14595           (set-buffer-modified-p nil)))))
14596
14597 (defun gnus-dribble-save ()
14598   (if (and gnus-dribble-buffer
14599            (buffer-name gnus-dribble-buffer))
14600       (save-excursion
14601         (set-buffer gnus-dribble-buffer)
14602         (save-buffer))))
14603
14604 (defun gnus-dribble-clear ()
14605   (save-excursion
14606     (if (gnus-buffer-exists-p gnus-dribble-buffer)
14607         (progn
14608           (set-buffer gnus-dribble-buffer)
14609           (erase-buffer)
14610           (set-buffer-modified-p nil)
14611           (setq buffer-saved-size (buffer-size))))))
14612
14613 \f
14614 ;;;
14615 ;;; Server Communication
14616 ;;;
14617
14618 (defun gnus-start-news-server (&optional confirm)
14619   "Open a method for getting news.
14620 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14621   (let (how)
14622     (if gnus-current-select-method
14623         ;; Stream is already opened.
14624         nil
14625       ;; Open NNTP server.
14626       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14627       (if confirm
14628           (progn
14629             ;; Read server name with completion.
14630             (setq gnus-nntp-server
14631                   (completing-read "NNTP server: "
14632                                    (mapcar (lambda (server) (list server))
14633                                            (cons (list gnus-nntp-server)
14634                                                  gnus-secondary-servers))
14635                                    nil nil gnus-nntp-server))))
14636
14637       (if (and gnus-nntp-server
14638                (stringp gnus-nntp-server)
14639                (not (string= gnus-nntp-server "")))
14640           (setq gnus-select-method
14641                 (cond ((or (string= gnus-nntp-server "")
14642                            (string= gnus-nntp-server "::"))
14643                        (list 'nnspool (system-name)))
14644                       ((string-match "^:" gnus-nntp-server)
14645                        (list 'nnmh gnus-nntp-server
14646                              (list 'nnmh-directory
14647                                    (file-name-as-directory
14648                                     (expand-file-name
14649                                      (concat "~/" (substring
14650                                                    gnus-nntp-server 1)))))
14651                              (list 'nnmh-get-new-mail nil)))
14652                       (t
14653                        (list 'nntp gnus-nntp-server)))))
14654
14655       (setq how (car gnus-select-method))
14656       (cond ((eq how 'nnspool)
14657              (require 'nnspool)
14658              (gnus-message 5 "Looking up local news spool..."))
14659             ((eq how 'nnmh)
14660              (require 'nnmh)
14661              (gnus-message 5 "Looking up mh spool..."))
14662             (t
14663              (require 'nntp)))
14664       (setq gnus-current-select-method gnus-select-method)
14665       (run-hooks 'gnus-open-server-hook)
14666       (or
14667        ;; gnus-open-server-hook might have opened it
14668        (gnus-server-opened gnus-select-method)
14669        (gnus-open-server gnus-select-method)
14670        (gnus-y-or-n-p
14671         (format
14672          "%s (%s) open error: '%s'.     Continue? "
14673          (car gnus-select-method) (cadr gnus-select-method)
14674          (gnus-status-message gnus-select-method)))
14675        (progn
14676          (gnus-message 1 "Couldn't open server on %s"
14677                        (nth 1 gnus-select-method))
14678          (ding)
14679          nil)))))
14680
14681 (defun gnus-check-group (group)
14682   "Try to make sure that the server where GROUP exists is alive."
14683   (let ((method (gnus-find-method-for-group group)))
14684     (or (gnus-server-opened method)
14685         (gnus-open-server method))))
14686
14687 (defun gnus-check-server (&optional method silent)
14688   "Check whether the connection to METHOD is down.
14689 If METHOD is nil, use `gnus-select-method'.
14690 If it is down, start it up (again)."
14691   (let ((method (or method gnus-select-method)))
14692     ;; Transform virtual server names into select methods.
14693     (when (stringp method)
14694       (setq method (gnus-server-to-method method)))
14695     (if (gnus-server-opened method)
14696         ;; The stream is already opened.
14697         t
14698       ;; Open the server.
14699       (unless silent
14700         (gnus-message 5 "Opening %s server%s..." (car method)
14701                       (if (equal (nth 1 method) "") ""
14702                         (format " on %s" (nth 1 method)))))
14703       (run-hooks 'gnus-open-server-hook)
14704       (prog1
14705           (gnus-open-server method)
14706         (unless silent
14707           (message ""))))))
14708
14709 (defun gnus-get-function (method function)
14710   "Return a function symbol based on METHOD and FUNCTION."
14711   ;; Translate server names into methods.
14712   (unless method
14713     (error "Attempted use of a nil select method"))
14714   (when (stringp method)
14715     (setq method (gnus-server-to-method method)))
14716   (let ((func (intern (format "%s-%s" (car method) function))))
14717     ;; If the functions isn't bound, we require the backend in
14718     ;; question.
14719     (unless (fboundp func)
14720       (require (car method))
14721       (unless (fboundp func)
14722         ;; This backend doesn't implement this function.
14723         (error "No such function: %s" func)))
14724     func))
14725
14726 \f
14727 ;;;
14728 ;;; Interface functions to the backends.
14729 ;;;
14730
14731 (defun gnus-open-server (method)
14732   "Open a connection to METHOD."
14733   (when (stringp method)
14734     (setq method (gnus-server-to-method method)))
14735   (let ((elem (assoc method gnus-opened-servers)))
14736     ;; If this method was previously denied, we just return nil.
14737     (if (eq (nth 1 elem) 'denied)
14738         (progn
14739           (gnus-message 1 "Denied server")
14740           nil)
14741       ;; Open the server.
14742       (let ((result
14743              (funcall (gnus-get-function method 'open-server)
14744                       (nth 1 method) (nthcdr 2 method))))
14745         ;; If this hasn't been opened before, we add it to the list.
14746         (unless elem
14747           (setq elem (list method nil)
14748                 gnus-opened-servers (cons elem gnus-opened-servers)))
14749         ;; Set the status of this server.
14750         (setcar (cdr elem) (if result 'ok 'denied))
14751         ;; Return the result from the "open" call.
14752         result))))
14753
14754 (defun gnus-close-server (method)
14755   "Close the connection to METHOD."
14756   (when (stringp method)
14757     (setq method (gnus-server-to-method method)))
14758   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
14759
14760 (defun gnus-request-list (method)
14761   "Request the active file from METHOD."
14762   (when (stringp method)
14763     (setq method (gnus-server-to-method method)))
14764   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
14765
14766 (defun gnus-request-list-newsgroups (method)
14767   "Request the newsgroups file from METHOD."
14768   (when (stringp method)
14769     (setq method (gnus-server-to-method method)))
14770   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
14771
14772 (defun gnus-request-newgroups (date method)
14773   "Request all new groups since DATE from METHOD."
14774   (when (stringp method)
14775     (setq method (gnus-server-to-method method)))
14776   (funcall (gnus-get-function method 'request-newgroups)
14777            date (nth 1 method)))
14778
14779 (defun gnus-server-opened (method)
14780   "Check whether a connection to METHOD has been opened."
14781   (when (stringp method)
14782     (setq method (gnus-server-to-method method)))
14783   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
14784
14785 (defun gnus-status-message (method)
14786   "Return the status message from METHOD.
14787 If METHOD is a string, it is interpreted as a group name.   The method
14788 this group uses will be queried."
14789   (let ((method (if (stringp method) (gnus-find-method-for-group method)
14790                   method)))
14791     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
14792
14793 (defun gnus-request-group (group &optional dont-check method)
14794   "Request GROUP.  If DONT-CHECK, no information is required."
14795   (let ((method (or method (gnus-find-method-for-group group))))
14796     (when (stringp method)
14797       (setq method (gnus-server-to-method method)))
14798     (funcall (gnus-get-function method 'request-group)
14799              (gnus-group-real-name group) (nth 1 method) dont-check)))
14800
14801 (defun gnus-request-asynchronous (group &optional articles)
14802   "Request that GROUP behave asynchronously.
14803 ARTICLES is the `data' of the group."
14804   (let ((method (gnus-find-method-for-group group)))
14805     (funcall (gnus-get-function method 'request-asynchronous)
14806              (gnus-group-real-name group) (nth 1 method) articles)))
14807
14808 (defun gnus-list-active-group (group)
14809   "Request active information on GROUP."
14810   (let ((method (gnus-find-method-for-group group))
14811         (func 'list-active-group))
14812     (when (gnus-check-backend-function func group)
14813       (funcall (gnus-get-function method func)
14814                (gnus-group-real-name group) (nth 1 method)))))
14815
14816 (defun gnus-request-group-description (group)
14817   "Request a description of GROUP."
14818   (let ((method (gnus-find-method-for-group group))
14819         (func 'request-group-description))
14820     (when (gnus-check-backend-function func group)
14821       (funcall (gnus-get-function method func)
14822                (gnus-group-real-name group) (nth 1 method)))))
14823
14824 (defun gnus-close-group (group)
14825   "Request the GROUP be closed."
14826   (let ((method (gnus-find-method-for-group group)))
14827     (funcall (gnus-get-function method 'close-group)
14828              (gnus-group-real-name group) (nth 1 method))))
14829
14830 (defun gnus-retrieve-headers (articles group &optional fetch-old)
14831   "Request headers for ARTICLES in GROUP.
14832 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
14833   (let ((method (gnus-find-method-for-group group)))
14834     (if (and gnus-use-cache (numberp (car articles)))
14835         (gnus-cache-retrieve-headers articles group fetch-old)
14836       (funcall (gnus-get-function method 'retrieve-headers)
14837                articles (gnus-group-real-name group) (nth 1 method)
14838                fetch-old))))
14839
14840 (defun gnus-retrieve-groups (groups method)
14841   "Request active information on GROUPS from METHOD."
14842   (when (stringp method)
14843     (setq method (gnus-server-to-method method)))
14844   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
14845
14846 (defun gnus-request-type (group &optional article)
14847   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14848   (let ((method (gnus-find-method-for-group group)))
14849     (if (not (gnus-check-backend-function 'request-type (car method)))
14850         'unknown
14851       (funcall (gnus-get-function method 'request-type)
14852                (gnus-group-real-name group) article))))
14853
14854 (defun gnus-request-update-mark (group article mark)
14855   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14856   (let ((method (gnus-find-method-for-group group)))
14857     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
14858         mark
14859       (funcall (gnus-get-function method 'request-update-mark)
14860                (gnus-group-real-name group) article mark))))
14861
14862 (defun gnus-request-article (article group &optional buffer)
14863   "Request the ARTICLE in GROUP.
14864 ARTICLE can either be an article number or an article Message-ID.
14865 If BUFFER, insert the article in that group."
14866   (let ((method (gnus-find-method-for-group group)))
14867     (funcall (gnus-get-function method 'request-article)
14868              article (gnus-group-real-name group) (nth 1 method) buffer)))
14869
14870 (defun gnus-request-head (article group)
14871   "Request the head of ARTICLE in GROUP."
14872   (let ((method (gnus-find-method-for-group group)))
14873     (funcall (gnus-get-function method 'request-head)
14874              article (gnus-group-real-name group) (nth 1 method))))
14875
14876 (defun gnus-request-body (article group)
14877   "Request the body of ARTICLE in GROUP."
14878   (let ((method (gnus-find-method-for-group group)))
14879     (funcall (gnus-get-function method 'request-body)
14880              article (gnus-group-real-name group) (nth 1 method))))
14881
14882 (defun gnus-request-post (method)
14883   "Post the current buffer using METHOD."
14884   (when (stringp method)
14885     (setq method (gnus-server-to-method method)))
14886   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
14887
14888 (defun gnus-request-scan (group method)
14889   "Request a SCAN being performed in GROUP from METHOD.
14890 If GROUP is nil, all groups on METHOD are scanned."
14891   (let ((method (if group (gnus-find-method-for-group group) method)))
14892     (funcall (gnus-get-function method 'request-scan)
14893              (and group (gnus-group-real-name group)) (nth 1 method))))
14894
14895 (defsubst gnus-request-update-info (info method)
14896   "Request that METHOD update INFO."
14897   (when (stringp method)
14898     (setq method (gnus-server-to-method method)))
14899   (when (gnus-check-backend-function 'request-update-info (car method))
14900     (funcall (gnus-get-function method 'request-update-info)
14901              (gnus-group-real-name (gnus-info-group info))
14902              info (nth 1 method))))
14903
14904 (defun gnus-request-expire-articles (articles group &optional force)
14905   (let ((method (gnus-find-method-for-group group)))
14906     (funcall (gnus-get-function method 'request-expire-articles)
14907              articles (gnus-group-real-name group) (nth 1 method)
14908              force)))
14909
14910 (defun gnus-request-move-article
14911   (article group server accept-function &optional last)
14912   (let ((method (gnus-find-method-for-group group)))
14913     (funcall (gnus-get-function method 'request-move-article)
14914              article (gnus-group-real-name group)
14915              (nth 1 method) accept-function last)))
14916
14917 (defun gnus-request-accept-article (group method &optional last)
14918   ;; Make sure there's a newline at the end of the article.
14919   (when (stringp method)
14920     (setq method (gnus-server-to-method method)))
14921   (when (and (not method)
14922              (stringp group))
14923     (setq method (gnus-find-method-for-group group)))
14924   (goto-char (point-max))
14925   (unless (bolp)
14926     (insert "\n"))
14927   (let ((func (car (or method (gnus-find-method-for-group group)))))
14928     (funcall (intern (format "%s-request-accept-article" func))
14929              (if (stringp group) (gnus-group-real-name group) group)
14930              (cadr method)
14931              last)))
14932
14933 (defun gnus-request-replace-article (article group buffer)
14934   (let ((func (car (gnus-find-method-for-group group))))
14935     (funcall (intern (format "%s-request-replace-article" func))
14936              article (gnus-group-real-name group) buffer)))
14937
14938 (defun gnus-request-associate-buffer (group)
14939   (let ((method (gnus-find-method-for-group group)))
14940     (funcall (gnus-get-function method 'request-associate-buffer)
14941              (gnus-group-real-name group))))
14942
14943 (defun gnus-request-restore-buffer (article group)
14944   "Request a new buffer restored to the state of ARTICLE."
14945   (let ((method (gnus-find-method-for-group group)))
14946     (funcall (gnus-get-function method 'request-restore-buffer)
14947              article (gnus-group-real-name group) (nth 1 method))))
14948
14949 (defun gnus-request-create-group (group &optional method)
14950   (when (stringp method)
14951     (setq method (gnus-server-to-method method)))
14952   (let ((method (or method (gnus-find-method-for-group group))))
14953     (funcall (gnus-get-function method 'request-create-group)
14954              (gnus-group-real-name group) (nth 1 method))))
14955
14956 (defun gnus-request-delete-group (group &optional force)
14957   (let ((method (gnus-find-method-for-group group)))
14958     (funcall (gnus-get-function method 'request-delete-group)
14959              (gnus-group-real-name group) force (nth 1 method))))
14960
14961 (defun gnus-request-rename-group (group new-name)
14962   (let ((method (gnus-find-method-for-group group)))
14963     (funcall (gnus-get-function method 'request-rename-group)
14964              (gnus-group-real-name group)
14965              (gnus-group-real-name new-name) (nth 1 method))))
14966
14967 (defun gnus-member-of-valid (symbol group)
14968   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
14969   (memq symbol (assoc
14970                 (symbol-name (car (gnus-find-method-for-group group)))
14971                 gnus-valid-select-methods)))
14972
14973 (defun gnus-method-option-p (method option)
14974   "Return non-nil if select METHOD has OPTION as a parameter."
14975   (when (stringp method)
14976     (setq method (gnus-server-to-method method)))
14977   (memq option (assoc (format "%s" (car method))
14978                       gnus-valid-select-methods)))
14979
14980 (defun gnus-server-extend-method (group method)
14981   ;; This function "extends" a virtual server.  If the server is
14982   ;; "hello", and the select method is ("hello" (my-var "something"))
14983   ;; in the group "alt.alt", this will result in a new virtual server
14984   ;; called "hello+alt.alt".
14985   (let ((entry
14986          (gnus-copy-sequence
14987           (if (equal (car method) "native") gnus-select-method
14988             (cdr (assoc (car method) gnus-server-alist))))))
14989     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
14990     (nconc entry (cdr method))))
14991
14992 (defun gnus-find-method-for-group (group &optional info)
14993   "Find the select method that GROUP uses."
14994   (or gnus-override-method
14995       (and (not group)
14996            gnus-select-method)
14997       (let ((info (or info (gnus-get-info group)))
14998             method)
14999         (if (or (not info)
15000                 (not (setq method (gnus-info-method info)))
15001                 (equal method "native"))
15002             gnus-select-method
15003           (setq method
15004                 (cond ((stringp method)
15005                        (gnus-server-to-method method))
15006                       ((stringp (car method))
15007                        (gnus-server-extend-method group method))
15008                       (t
15009                        method)))
15010           (if (equal (cadr method) "")
15011               method
15012             (gnus-server-add-address method))))))
15013
15014 (defun gnus-check-backend-function (func group)
15015   "Check whether GROUP supports function FUNC."
15016   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
15017                   group)))
15018     (fboundp (intern (format "%s-%s" method func)))))
15019
15020 (defun gnus-methods-using (feature)
15021   "Find all methods that have FEATURE."
15022   (let ((valids gnus-valid-select-methods)
15023         outs)
15024     (while valids
15025       (if (memq feature (car valids))
15026           (setq outs (cons (car valids) outs)))
15027       (setq valids (cdr valids)))
15028     outs))
15029
15030 \f
15031 ;;;
15032 ;;; Active & Newsrc File Handling
15033 ;;;
15034
15035 (defun gnus-setup-news (&optional rawfile level dont-connect)
15036   "Setup news information.
15037 If RAWFILE is non-nil, the .newsrc file will also be read.
15038 If LEVEL is non-nil, the news will be set up at level LEVEL."
15039   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
15040
15041     (when init 
15042       ;; Clear some variables to re-initialize news information.
15043       (setq gnus-newsrc-alist nil
15044             gnus-active-hashtb nil)
15045       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
15046       (gnus-read-newsrc-file rawfile))
15047
15048     (when (and (not (assoc "archive" gnus-server-alist))
15049                gnus-message-archive-method)
15050       (push (cons "archive" gnus-message-archive-method)
15051             gnus-server-alist))
15052
15053     ;; If we don't read the complete active file, we fill in the
15054     ;; hashtb here.
15055     (if (or (null gnus-read-active-file)
15056             (eq gnus-read-active-file 'some))
15057         (gnus-update-active-hashtb-from-killed))
15058
15059     ;; Read the active file and create `gnus-active-hashtb'.
15060     ;; If `gnus-read-active-file' is nil, then we just create an empty
15061     ;; hash table.  The partial filling out of the hash table will be
15062     ;; done in `gnus-get-unread-articles'.
15063     (and gnus-read-active-file
15064          (not level)
15065          (gnus-read-active-file))
15066
15067     (or gnus-active-hashtb
15068         (setq gnus-active-hashtb (make-vector 4095 0)))
15069
15070     ;; Initialize the cache.
15071     (when gnus-use-cache
15072       (gnus-cache-open))
15073
15074     ;; Possibly eval the dribble file.
15075     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
15076
15077     (gnus-update-format-specifications)
15078
15079     ;; See whether we need to read the description file.
15080     (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
15081              (not gnus-description-hashtb)
15082              (not dont-connect)
15083              gnus-read-active-file)
15084         (gnus-read-all-descriptions-files))
15085
15086     ;; Find new newsgroups and treat them.
15087     (if (and init gnus-check-new-newsgroups (not level)
15088              (gnus-check-server gnus-select-method))
15089         (gnus-find-new-newsgroups))
15090
15091     ;; We might read in new NoCeM messages here.
15092     (when gnus-use-nocem 
15093       (gnus-nocem-scan-groups))
15094
15095     ;; Find the number of unread articles in each non-dead group.
15096     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
15097       (gnus-get-unread-articles level))
15098
15099     (if (and init gnus-check-bogus-newsgroups
15100              gnus-read-active-file (not level)
15101              (gnus-server-opened gnus-select-method))
15102         (gnus-check-bogus-newsgroups))))
15103
15104 (defun gnus-find-new-newsgroups (&optional arg)
15105   "Search for new newsgroups and add them.
15106 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
15107 The `-n' option line from .newsrc is respected.
15108 If ARG (the prefix), use the `ask-server' method to query
15109 the server for new groups."
15110   (interactive "P")
15111   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
15112                        (null gnus-read-active-file)
15113                        (eq gnus-read-active-file 'some))
15114                    'ask-server gnus-check-new-newsgroups)))
15115     (unless (gnus-check-first-time-used)
15116       (if (or (consp check)
15117               (eq check 'ask-server))
15118           ;; Ask the server for new groups.
15119           (gnus-ask-server-for-new-groups)
15120         ;; Go through the active hashtb and look for new groups.
15121         (let ((groups 0)
15122               group new-newsgroups)
15123           (gnus-message 5 "Looking for new newsgroups...")
15124           (unless gnus-have-read-active-file
15125             (gnus-read-active-file))
15126           (setq gnus-newsrc-last-checked-date (current-time-string))
15127           (unless gnus-killed-hashtb
15128             (gnus-make-hashtable-from-killed))
15129           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
15130           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
15131           (mapatoms
15132            (lambda (sym)
15133              (if (or (null (setq group (symbol-name sym)))
15134                      (not (boundp sym))
15135                      (null (symbol-value sym))
15136                      (gnus-gethash group gnus-killed-hashtb)
15137                      (gnus-gethash group gnus-newsrc-hashtb))
15138                  ()
15139                (let ((do-sub (gnus-matches-options-n group)))
15140                  (cond
15141                   ((eq do-sub 'subscribe)
15142                    (setq groups (1+ groups))
15143                    (gnus-sethash group group gnus-killed-hashtb)
15144                    (funcall gnus-subscribe-options-newsgroup-method group))
15145                   ((eq do-sub 'ignore)
15146                    nil)
15147                   (t
15148                    (setq groups (1+ groups))
15149                    (gnus-sethash group group gnus-killed-hashtb)
15150                    (if gnus-subscribe-hierarchical-interactive
15151                        (setq new-newsgroups (cons group new-newsgroups))
15152                      (funcall gnus-subscribe-newsgroup-method group)))))))
15153            gnus-active-hashtb)
15154           (when new-newsgroups
15155             (gnus-subscribe-hierarchical-interactive new-newsgroups))
15156           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15157           (if (> groups 0)
15158               (gnus-message 6 "%d new newsgroup%s arrived."
15159                             groups (if (> groups 1) "s have" " has"))
15160             (gnus-message 6 "No new newsgroups.")))))))
15161
15162 (defun gnus-matches-options-n (group)
15163   ;; Returns `subscribe' if the group is to be unconditionally
15164   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
15165   ;; no match for the group.
15166
15167   ;; First we check the two user variables.
15168   (cond
15169    ((and gnus-options-subscribe
15170          (string-match gnus-options-subscribe group))
15171     'subscribe)
15172    ((and gnus-auto-subscribed-groups
15173          (string-match gnus-auto-subscribed-groups group))
15174     'subscribe)
15175    ((and gnus-options-not-subscribe
15176          (string-match gnus-options-not-subscribe group))
15177     'ignore)
15178    ;; Then we go through the list that was retrieved from the .newsrc
15179    ;; file.  This list has elements on the form
15180    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
15181    ;; is in the reverse order of the options line) is returned.
15182    (t
15183     (let ((regs gnus-newsrc-options-n))
15184       (while (and regs
15185                   (not (string-match (caar regs) group)))
15186         (setq regs (cdr regs)))
15187       (and regs (cdar regs))))))
15188
15189 (defun gnus-ask-server-for-new-groups ()
15190   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
15191          (methods (cons gnus-select-method
15192                         (nconc
15193                          (when gnus-message-archive-method
15194                            (list "archive"))
15195                          (append
15196                           (and (consp gnus-check-new-newsgroups)
15197                                gnus-check-new-newsgroups)
15198                           gnus-secondary-select-methods))))
15199          (groups 0)
15200          (new-date (current-time-string))
15201          group new-newsgroups got-new method hashtb
15202          gnus-override-subscribe-method)
15203     ;; Go through both primary and secondary select methods and
15204     ;; request new newsgroups.
15205     (while (setq method (gnus-server-get-method nil (pop methods)))
15206       (setq new-newsgroups nil)
15207       (setq gnus-override-subscribe-method method)
15208       (when (and (gnus-check-server method)
15209                  (gnus-request-newgroups date method))
15210         (save-excursion
15211           (setq got-new t)
15212           (setq hashtb (gnus-make-hashtable 100))
15213           (set-buffer nntp-server-buffer)
15214           ;; Enter all the new groups into a hashtable.
15215           (gnus-active-to-gnus-format method hashtb 'ignore)))
15216       ;; Now all new groups from `method' are in `hashtb'.
15217       (mapatoms
15218        (lambda (group-sym)
15219          (if (or (null (setq group (symbol-name group-sym)))
15220                  (null (symbol-value group-sym))
15221                  (gnus-gethash group gnus-newsrc-hashtb)
15222                  (member group gnus-zombie-list)
15223                  (member group gnus-killed-list))
15224              ;; The group is already known.
15225              ()
15226            ;; Make this group active.
15227            (when (symbol-value group-sym)
15228              (gnus-set-active group (symbol-value group-sym)))
15229            ;; Check whether we want it or not.
15230            (let ((do-sub (gnus-matches-options-n group)))
15231              (cond
15232               ((eq do-sub 'subscribe)
15233                (incf groups)
15234                (gnus-sethash group group gnus-killed-hashtb)
15235                (funcall gnus-subscribe-options-newsgroup-method group))
15236               ((eq do-sub 'ignore)
15237                nil)
15238               (t
15239                (incf groups)
15240                (gnus-sethash group group gnus-killed-hashtb)
15241                (if gnus-subscribe-hierarchical-interactive
15242                    (push group new-newsgroups)
15243                  (funcall gnus-subscribe-newsgroup-method group)))))))
15244        hashtb)
15245       (when new-newsgroups
15246         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
15247     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15248     (when (> groups 0)
15249       (gnus-message 6 "%d new newsgroup%s arrived."
15250                     groups (if (> groups 1) "s have" " has")))
15251     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15252     got-new))
15253
15254 (defun gnus-check-first-time-used ()
15255   (if (or (> (length gnus-newsrc-alist) 1)
15256           (file-exists-p gnus-startup-file)
15257           (file-exists-p (concat gnus-startup-file ".el"))
15258           (file-exists-p (concat gnus-startup-file ".eld")))
15259       nil
15260     (gnus-message 6 "First time user; subscribing you to default groups")
15261     (unless gnus-have-read-active-file
15262       (gnus-read-active-file))
15263     (setq gnus-newsrc-last-checked-date (current-time-string))
15264     (let ((groups gnus-default-subscribed-newsgroups)
15265           group)
15266       (if (eq groups t)
15267           nil
15268         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15269         (mapatoms
15270          (lambda (sym)
15271            (if (null (setq group (symbol-name sym)))
15272                ()
15273              (let ((do-sub (gnus-matches-options-n group)))
15274                (cond
15275                 ((eq do-sub 'subscribe)
15276                  (gnus-sethash group group gnus-killed-hashtb)
15277                  (funcall gnus-subscribe-options-newsgroup-method group))
15278                 ((eq do-sub 'ignore)
15279                  nil)
15280                 (t
15281                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15282          gnus-active-hashtb)
15283         (while groups
15284           (if (gnus-active (car groups))
15285               (gnus-group-change-level
15286                (car groups) gnus-level-default-subscribed gnus-level-killed))
15287           (setq groups (cdr groups)))
15288         (gnus-group-make-help-group)
15289         (and gnus-novice-user
15290              (gnus-message 7 "`A k' to list killed groups"))))))
15291
15292 (defun gnus-subscribe-group (group previous &optional method)
15293   (gnus-group-change-level
15294    (if method
15295        (list t group gnus-level-default-subscribed nil nil method)
15296      group)
15297    gnus-level-default-subscribed gnus-level-killed previous t))
15298
15299 ;; `gnus-group-change-level' is the fundamental function for changing
15300 ;; subscription levels of newsgroups.  This might mean just changing
15301 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15302 ;; again, which subscribes/unsubscribes a group, which is equally
15303 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15304 ;; from 8-9 to 1-7 means that you remove the group from the list of
15305 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15306 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15307 ;; which is trivial.
15308 ;; ENTRY can either be a string (newsgroup name) or a list (if
15309 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15310 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15311 ;; entries.
15312 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15313 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15314 ;; after.
15315 (defun gnus-group-change-level (entry level &optional oldlevel
15316                                       previous fromkilled)
15317   (let (group info active num)
15318     ;; Glean what info we can from the arguments
15319     (if (consp entry)
15320         (if fromkilled (setq group (nth 1 entry))
15321           (setq group (car (nth 2 entry))))
15322       (setq group entry))
15323     (if (and (stringp entry)
15324              oldlevel
15325              (< oldlevel gnus-level-zombie))
15326         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15327     (if (and (not oldlevel)
15328              (consp entry))
15329         (setq oldlevel (gnus-info-level (nth 2 entry)))
15330       (setq oldlevel (or oldlevel 9)))
15331     (if (stringp previous)
15332         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15333
15334     (if (and (>= oldlevel gnus-level-zombie)
15335              (gnus-gethash group gnus-newsrc-hashtb))
15336         ;; We are trying to subscribe a group that is already
15337         ;; subscribed.
15338         ()                              ; Do nothing.
15339
15340       (or (gnus-ephemeral-group-p group)
15341           (gnus-dribble-enter
15342            (format "(gnus-group-change-level %S %S %S %S %S)"
15343                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15344
15345       ;; Then we remove the newgroup from any old structures, if needed.
15346       ;; If the group was killed, we remove it from the killed or zombie
15347       ;; list.  If not, and it is in fact going to be killed, we remove
15348       ;; it from the newsrc hash table and assoc.
15349       (cond
15350        ((>= oldlevel gnus-level-zombie)
15351         (if (= oldlevel gnus-level-zombie)
15352             (setq gnus-zombie-list (delete group gnus-zombie-list))
15353           (setq gnus-killed-list (delete group gnus-killed-list))))
15354        (t
15355         (if (and (>= level gnus-level-zombie)
15356                  entry)
15357             (progn
15358               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15359               (if (nth 3 entry)
15360                   (setcdr (gnus-gethash (car (nth 3 entry))
15361                                         gnus-newsrc-hashtb)
15362                           (cdr entry)))
15363               (setcdr (cdr entry) (cdddr entry))))))
15364
15365       ;; Finally we enter (if needed) the list where it is supposed to
15366       ;; go, and change the subscription level.  If it is to be killed,
15367       ;; we enter it into the killed or zombie list.
15368       (cond 
15369        ((>= level gnus-level-zombie)
15370         ;; Remove from the hash table.
15371         (gnus-sethash group nil gnus-newsrc-hashtb)
15372         ;; We do not enter foreign groups into the list of dead
15373         ;; groups.
15374         (unless (gnus-group-foreign-p group)
15375           (if (= level gnus-level-zombie)
15376               (setq gnus-zombie-list (cons group gnus-zombie-list))
15377             (setq gnus-killed-list (cons group gnus-killed-list)))))
15378        (t
15379         ;; If the list is to be entered into the newsrc assoc, and
15380         ;; it was killed, we have to create an entry in the newsrc
15381         ;; hashtb format and fix the pointers in the newsrc assoc.
15382         (if (< oldlevel gnus-level-zombie)
15383             ;; It was alive, and it is going to stay alive, so we
15384             ;; just change the level and don't change any pointers or
15385             ;; hash table entries.
15386             (setcar (cdaddr entry) level)
15387           (if (listp entry)
15388               (setq info (cdr entry)
15389                     num (car entry))
15390             (setq active (gnus-active group))
15391             (setq num
15392                   (if active (- (1+ (cdr active)) (car active)) t))
15393             ;; Check whether the group is foreign.  If so, the
15394             ;; foreign select method has to be entered into the
15395             ;; info.
15396             (let ((method (or gnus-override-subscribe-method
15397                               (gnus-group-method group))))
15398               (if (eq method gnus-select-method)
15399                   (setq info (list group level nil))
15400                 (setq info (list group level nil nil method)))))
15401           (unless previous
15402             (setq previous
15403                   (let ((p gnus-newsrc-alist))
15404                     (while (cddr p)
15405                       (setq p (cdr p)))
15406                     p)))
15407           (setq entry (cons info (cddr previous)))
15408           (if (cdr previous)
15409               (progn
15410                 (setcdr (cdr previous) entry)
15411                 (gnus-sethash group (cons num (cdr previous))
15412                               gnus-newsrc-hashtb))
15413             (setcdr previous entry)
15414             (gnus-sethash group (cons num previous)
15415                           gnus-newsrc-hashtb))
15416           (when (cdr entry)
15417             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15418       (when gnus-group-change-level-function
15419         (funcall gnus-group-change-level-function group level oldlevel)))))
15420
15421 (defun gnus-kill-newsgroup (newsgroup)
15422   "Obsolete function.  Kills a newsgroup."
15423   (gnus-group-change-level
15424    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15425
15426 (defun gnus-check-bogus-newsgroups (&optional confirm)
15427   "Remove bogus newsgroups.
15428 If CONFIRM is non-nil, the user has to confirm the deletion of every
15429 newsgroup."
15430   (let ((newsrc (cdr gnus-newsrc-alist))
15431         bogus group entry info)
15432     (gnus-message 5 "Checking bogus newsgroups...")
15433     (unless gnus-have-read-active-file
15434       (gnus-read-active-file))
15435     (when (member gnus-select-method gnus-have-read-active-file)
15436       ;; Find all bogus newsgroup that are subscribed.
15437       (while newsrc
15438         (setq info (pop newsrc)
15439               group (gnus-info-group info))
15440         (unless (or (gnus-active group) ; Active
15441                     (gnus-info-method info) ; Foreign
15442                     (and confirm
15443                          (not (gnus-y-or-n-p
15444                                (format "Remove bogus newsgroup: %s " group)))))
15445           ;; Found a bogus newsgroup.
15446           (push group bogus)))
15447       ;; Remove all bogus subscribed groups by first killing them, and
15448       ;; then removing them from the list of killed groups.
15449       (while bogus
15450         (when (setq entry (gnus-gethash (setq group (pop bogus))
15451                                         gnus-newsrc-hashtb))
15452           (gnus-group-change-level entry gnus-level-killed)
15453           (setq gnus-killed-list (delete group gnus-killed-list))))
15454       ;; Then we remove all bogus groups from the list of killed and
15455       ;; zombie groups.  They are are removed without confirmation.
15456       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15457             killed)
15458         (while dead-lists
15459           (setq killed (symbol-value (car dead-lists)))
15460           (while killed
15461             (unless (gnus-active (setq group (pop killed)))
15462               ;; The group is bogus.
15463               ;; !!!Slow as hell.
15464               (set (car dead-lists)
15465                    (delete group (symbol-value (car dead-lists))))))
15466           (setq dead-lists (cdr dead-lists))))
15467       (gnus-message 5 "Checking bogus newsgroups...done"))))
15468
15469 (defun gnus-check-duplicate-killed-groups ()
15470   "Remove duplicates from the list of killed groups."
15471   (interactive)
15472   (let ((killed gnus-killed-list))
15473     (while killed
15474       (gnus-message 9 "%d" (length killed))
15475       (setcdr killed (delete (car killed) (cdr killed)))
15476       (setq killed (cdr killed)))))
15477
15478 ;; We want to inline a function from gnus-cache, so we cheat here:
15479 (eval-when-compile
15480   (provide 'gnus)
15481   (require 'gnus-cache))
15482
15483 (defun gnus-get-unread-articles-in-group (info active &optional update)
15484   (when active
15485     ;; Allow the backend to update the info in the group.
15486     (when (and update 
15487                (gnus-request-update-info
15488                 info (gnus-find-method-for-group (gnus-info-group info))))
15489       (gnus-activate-group (gnus-info-group info)))
15490     (let* ((range (gnus-info-read info))
15491            (num 0))
15492       ;; If a cache is present, we may have to alter the active info.
15493       (when (and gnus-use-cache info)
15494         (inline (gnus-cache-possibly-alter-active 
15495                  (gnus-info-group info) active)))
15496       ;; Modify the list of read articles according to what articles
15497       ;; are available; then tally the unread articles and add the
15498       ;; number to the group hash table entry.
15499       (cond
15500        ((zerop (cdr active))
15501         (setq num 0))
15502        ((not range)
15503         (setq num (- (1+ (cdr active)) (car active))))
15504        ((not (listp (cdr range)))
15505         ;; Fix a single (num . num) range according to the
15506         ;; active hash table.
15507         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15508         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15509         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15510         ;; Compute number of unread articles.
15511         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15512        (t
15513         ;; The read list is a list of ranges.  Fix them according to
15514         ;; the active hash table.
15515         ;; First peel off any elements that are below the lower
15516         ;; active limit.
15517         (while (and (cdr range)
15518                     (>= (car active)
15519                         (or (and (atom (cadr range)) (cadr range))
15520                             (caadr range))))
15521           (if (numberp (car range))
15522               (setcar range
15523                       (cons (car range)
15524                             (or (and (numberp (cadr range))
15525                                      (cadr range))
15526                                 (cdadr range))))
15527             (setcdr (car range)
15528                     (or (and (numberp (nth 1 range)) (nth 1 range))
15529                         (cdadr range))))
15530           (setcdr range (cddr range)))
15531         ;; Adjust the first element to be the same as the lower limit.
15532         (if (and (not (atom (car range)))
15533                  (< (cdar range) (car active)))
15534             (setcdr (car range) (1- (car active))))
15535         ;; Then we want to peel off any elements that are higher
15536         ;; than the upper active limit.
15537         (let ((srange range))
15538           ;; Go past all legal elements.
15539           (while (and (cdr srange)
15540                       (<= (or (and (atom (cadr srange))
15541                                    (cadr srange))
15542                               (caadr srange)) (cdr active)))
15543             (setq srange (cdr srange)))
15544           (if (cdr srange)
15545               ;; Nuke all remaining illegal elements.
15546               (setcdr srange nil))
15547
15548           ;; Adjust the final element.
15549           (if (and (not (atom (car srange)))
15550                    (> (cdar srange) (cdr active)))
15551               (setcdr (car srange) (cdr active))))
15552         ;; Compute the number of unread articles.
15553         (while range
15554           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15555                                       (cdar range)))
15556                               (or (and (atom (car range)) (car range))
15557                                   (caar range)))))
15558           (setq range (cdr range)))
15559         (setq num (max 0 (- (cdr active) num)))))
15560       ;; Set the number of unread articles.
15561       (when info
15562         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
15563       num)))
15564
15565 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15566 ;; and compute how many unread articles there are in each group.
15567 (defun gnus-get-unread-articles (&optional level)
15568   (let* ((newsrc (cdr gnus-newsrc-alist))
15569          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15570          (foreign-level
15571           (min
15572            (cond ((and gnus-activate-foreign-newsgroups
15573                        (not (numberp gnus-activate-foreign-newsgroups)))
15574                   (1+ gnus-level-subscribed))
15575                  ((numberp gnus-activate-foreign-newsgroups)
15576                   gnus-activate-foreign-newsgroups)
15577                  (t 0))
15578            level))
15579          info group active method)
15580     (gnus-message 5 "Checking new news...")
15581
15582     (while newsrc
15583       (setq active (gnus-active (setq group (gnus-info-group
15584                                              (setq info (pop newsrc))))))
15585
15586       ;; Check newsgroups.  If the user doesn't want to check them, or
15587       ;; they can't be checked (for instance, if the news server can't
15588       ;; be reached) we just set the number of unread articles in this
15589       ;; newsgroup to t.  This means that Gnus thinks that there are
15590       ;; unread articles, but it has no idea how many.
15591       (if (and (setq method (gnus-info-method info))
15592                (not (gnus-server-equal
15593                      gnus-select-method
15594                      (setq method (gnus-server-get-method nil method))))
15595                (not (gnus-secondary-method-p method)))
15596           ;; These groups are foreign.  Check the level.
15597           (when (<= (gnus-info-level info) foreign-level)
15598             (setq active (gnus-activate-group group 'scan))
15599             (unless (inline (gnus-virtual-group-p group))
15600               (inline (gnus-close-group group))))
15601
15602         ;; These groups are native or secondary.
15603         (when (and (<= (gnus-info-level info) level)
15604                    (not gnus-read-active-file))
15605           (setq active (gnus-activate-group group 'scan))
15606           (inline (gnus-close-group group))))
15607
15608       (if active
15609           (inline (gnus-get-unread-articles-in-group 
15610                    info active
15611                    (and method
15612                         (fboundp (intern (concat (symbol-name (car method))
15613                                                  "-request-scan"))))))
15614         ;; The group couldn't be reached, so we nix out the number of
15615         ;; unread articles and stuff.
15616         (gnus-set-active group nil)
15617         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
15618
15619     (gnus-message 5 "Checking new news...done")))
15620
15621 ;; Create a hash table out of the newsrc alist.  The `car's of the
15622 ;; alist elements are used as keys.
15623 (defun gnus-make-hashtable-from-newsrc-alist ()
15624   (let ((alist gnus-newsrc-alist)
15625         (ohashtb gnus-newsrc-hashtb)
15626         prev)
15627     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15628     (setq alist
15629           (setq prev (setq gnus-newsrc-alist
15630                            (if (equal (caar gnus-newsrc-alist)
15631                                       "dummy.group")
15632                                gnus-newsrc-alist
15633                              (cons (list "dummy.group" 0 nil) alist)))))
15634     (while alist
15635       (gnus-sethash
15636        (caar alist)
15637        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
15638              prev)
15639        gnus-newsrc-hashtb)
15640       (setq prev alist
15641             alist (cdr alist)))))
15642
15643 (defun gnus-make-hashtable-from-killed ()
15644   "Create a hash table from the killed and zombie lists."
15645   (let ((lists '(gnus-killed-list gnus-zombie-list))
15646         list)
15647     (setq gnus-killed-hashtb
15648           (gnus-make-hashtable
15649            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15650     (while (setq list (pop lists))
15651       (setq list (symbol-value list))
15652       (while list
15653         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
15654
15655 (defun gnus-activate-group (group &optional scan)
15656   ;; Check whether a group has been activated or not.
15657   ;; If SCAN, request a scan of that group as well.
15658   (let ((method (gnus-find-method-for-group group))
15659         active)
15660     (and (gnus-check-server method)
15661          ;; We escape all bugs and quit here to make it possible to
15662          ;; continue if a group is so out-there that it reports bugs
15663          ;; and stuff.
15664          (progn
15665            (and scan
15666                 (gnus-check-backend-function 'request-scan (car method))
15667                 (gnus-request-scan group method))
15668            t)
15669          (condition-case ()
15670              (gnus-request-group group)
15671         ;   (error nil)
15672            (quit nil))
15673          (save-excursion
15674            (set-buffer nntp-server-buffer)
15675            (goto-char (point-min))
15676            ;; Parse the result we got from `gnus-request-group'.
15677            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
15678                 (progn
15679                   (goto-char (match-beginning 1))
15680                   (gnus-set-active
15681                    group (setq active (cons (read (current-buffer))
15682                                             (read (current-buffer)))))
15683                   ;; Return the new active info.
15684                   active))))))
15685
15686 (defun gnus-update-read-articles (group unread)
15687   "Update the list of read and ticked articles in GROUP using the
15688 UNREAD and TICKED lists.
15689 Note: UNSELECTED has to be sorted over `<'.
15690 Returns whether the updating was successful."
15691   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
15692          (entry (gnus-gethash group gnus-newsrc-hashtb))
15693          (info (nth 2 entry))
15694          (prev 1)
15695          (unread (sort (copy-sequence unread) '<))
15696          read)
15697     (if (or (not info) (not active))
15698         ;; There is no info on this group if it was, in fact,
15699         ;; killed.  Gnus stores no information on killed groups, so
15700         ;; there's nothing to be done.
15701         ;; One could store the information somewhere temporarily,
15702         ;; perhaps...  Hmmm...
15703         ()
15704       ;; Remove any negative articles numbers.
15705       (while (and unread (< (car unread) 0))
15706         (setq unread (cdr unread)))
15707       ;; Remove any expired article numbers
15708       (while (and unread (< (car unread) (car active)))
15709         (setq unread (cdr unread)))
15710       ;; Compute the ranges of read articles by looking at the list of
15711       ;; unread articles.
15712       (while unread
15713         (if (/= (car unread) prev)
15714             (setq read (cons (if (= prev (1- (car unread))) prev
15715                                (cons prev (1- (car unread)))) read)))
15716         (setq prev (1+ (car unread)))
15717         (setq unread (cdr unread)))
15718       (when (<= prev (cdr active))
15719         (setq read (cons (cons prev (cdr active)) read)))
15720       ;; Enter this list into the group info.
15721       (gnus-info-set-read
15722        info (if (> (length read) 1) (nreverse read) read))
15723       ;; Set the number of unread articles in gnus-newsrc-hashtb.
15724       (gnus-get-unread-articles-in-group info (gnus-active group))
15725       t)))
15726
15727 (defun gnus-make-articles-unread (group articles)
15728   "Mark ARTICLES in GROUP as unread."
15729   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
15730                           (gnus-gethash (gnus-group-real-name group)
15731                                         gnus-newsrc-hashtb))))
15732          (ranges (gnus-info-read info))
15733          news article)
15734     (while articles
15735       (when (gnus-member-of-range
15736              (setq article (pop articles)) ranges)
15737         (setq news (cons article news))))
15738     (when news
15739       (gnus-info-set-read
15740        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
15741       (gnus-group-update-group group t))))
15742
15743 ;; Enter all dead groups into the hashtb.
15744 (defun gnus-update-active-hashtb-from-killed ()
15745   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
15746         (lists (list gnus-killed-list gnus-zombie-list))
15747         killed)
15748     (while lists
15749       (setq killed (car lists))
15750       (while killed
15751         (gnus-sethash (car killed) nil hashtb)
15752         (setq killed (cdr killed)))
15753       (setq lists (cdr lists)))))
15754
15755 ;; Get the active file(s) from the backend(s).
15756 (defun gnus-read-active-file ()
15757   (gnus-group-set-mode-line)
15758   (let ((methods 
15759          (append
15760           (if (gnus-check-server gnus-select-method)
15761               ;; The native server is available.
15762               (cons gnus-select-method gnus-secondary-select-methods)
15763             ;; The native server is down, so we just do the
15764             ;; secondary ones.
15765             gnus-secondary-select-methods)
15766           ;; Also read from the archive server.
15767           (when gnus-message-archive-method
15768             (list "archive"))))
15769         list-type)
15770     (setq gnus-have-read-active-file nil)
15771     (save-excursion
15772       (set-buffer nntp-server-buffer)
15773       (while methods
15774         (let* ((method (if (stringp (car methods))
15775                            (gnus-server-get-method nil (car methods))
15776                          (car methods)))
15777                (where (nth 1 method))
15778                (mesg (format "Reading active file%s via %s..."
15779                              (if (and where (not (zerop (length where))))
15780                                  (concat " from " where) "")
15781                              (car method))))
15782           (gnus-message 5 mesg)
15783           (when (gnus-check-server method)
15784             ;; Request that the backend scan its incoming messages.
15785             (and (gnus-check-backend-function 'request-scan (car method))
15786                  (gnus-request-scan nil method))
15787             (cond
15788              ((and (eq gnus-read-active-file 'some)
15789                    (gnus-check-backend-function 'retrieve-groups (car method)))
15790               (let ((newsrc (cdr gnus-newsrc-alist))
15791                     (gmethod (gnus-server-get-method nil method))
15792                     groups info)
15793                 (while (setq info (pop newsrc))
15794                   (when (gnus-server-equal
15795                          (gnus-find-method-for-group 
15796                           (gnus-info-group info) info)
15797                          gmethod)
15798                     (push (gnus-group-real-name (gnus-info-group info)) 
15799                           groups)))
15800                 (when groups
15801                   (gnus-check-server method)
15802                   (setq list-type (gnus-retrieve-groups groups method))
15803                   (cond
15804                    ((not list-type)
15805                     (gnus-message
15806                      1 "Cannot read partial active file from %s server."
15807                      (car method))
15808                     (ding)
15809                     (sit-for 2))
15810                    ((eq list-type 'active)
15811                     (gnus-active-to-gnus-format method gnus-active-hashtb))
15812                    (t
15813                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
15814              (t
15815               (if (not (gnus-request-list method))
15816                   (progn
15817                     (unless (equal method gnus-message-archive-method)
15818                       (gnus-message 1 "Cannot read active file from %s server."
15819                                     (car method))
15820                       (ding)))
15821                 (gnus-active-to-gnus-format method)
15822                 ;; We mark this active file as read.
15823                 (push method gnus-have-read-active-file)
15824                 (gnus-message 5 "%sdone" mesg))))))
15825         (setq methods (cdr methods))))))
15826
15827 ;; Read an active file and place the results in `gnus-active-hashtb'.
15828 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
15829   (unless method
15830     (setq method gnus-select-method))
15831   (let ((cur (current-buffer))
15832         (hashtb (or hashtb
15833                     (if (and gnus-active-hashtb
15834                              (not (equal method gnus-select-method)))
15835                         gnus-active-hashtb
15836                       (setq gnus-active-hashtb
15837                             (if (equal method gnus-select-method)
15838                                 (gnus-make-hashtable
15839                                  (count-lines (point-min) (point-max)))
15840                               (gnus-make-hashtable 4096)))))))
15841     ;; Delete unnecessary lines.
15842     (goto-char (point-min))
15843     (while (search-forward "\nto." nil t)
15844       (delete-region (1+ (match-beginning 0))
15845                      (progn (forward-line 1) (point))))
15846     (or (string= gnus-ignored-newsgroups "")
15847         (progn
15848           (goto-char (point-min))
15849           (delete-matching-lines gnus-ignored-newsgroups)))
15850     ;; Make the group names readable as a lisp expression even if they
15851     ;; contain special characters.
15852     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
15853     (goto-char (point-max))
15854     (while (re-search-backward "[][';?()#]" nil t)
15855       (insert ?\\))
15856     ;; If these are groups from a foreign select method, we insert the
15857     ;; group prefix in front of the group names.
15858     (and method (not (gnus-server-equal
15859                       (gnus-server-get-method nil method)
15860                       (gnus-server-get-method nil gnus-select-method)))
15861          (let ((prefix (gnus-group-prefixed-name "" method)))
15862            (goto-char (point-min))
15863            (while (and (not (eobp))
15864                        (progn (insert prefix)
15865                               (zerop (forward-line 1)))))))
15866     ;; Store the active file in a hash table.
15867     (goto-char (point-min))
15868     (if (string-match "%[oO]" gnus-group-line-format)
15869         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
15870         ;; If we want information on moderated groups, we use this
15871         ;; loop...
15872         (let* ((mod-hashtb (make-vector 7 0))
15873                (m (intern "m" mod-hashtb))
15874                group max min)
15875           (while (not (eobp))
15876             (condition-case nil
15877                 (progn
15878                   (narrow-to-region (point) (gnus-point-at-eol))
15879                   (setq group (let ((obarray hashtb)) (read cur)))
15880                   (if (and (numberp (setq max (read cur)))
15881                            (numberp (setq min (read cur)))
15882                            (progn
15883                              (skip-chars-forward " \t")
15884                              (not
15885                               (or (= (following-char) ?=)
15886                                   (= (following-char) ?x)
15887                                   (= (following-char) ?j)))))
15888                       (set group (cons min max))
15889                     (set group nil))
15890                   ;; Enter moderated groups into a list.
15891                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
15892                       (setq gnus-moderated-list
15893                             (cons (symbol-name group) gnus-moderated-list))))
15894               (error
15895                (and group
15896                     (symbolp group)
15897                     (set group nil))))
15898             (widen)
15899             (forward-line 1)))
15900       ;; And if we do not care about moderation, we use this loop,
15901       ;; which is faster.
15902       (let (group max min)
15903         (while (not (eobp))
15904           (condition-case ()
15905               (progn
15906                 (narrow-to-region (point) (gnus-point-at-eol))
15907                 ;; group gets set to a symbol interned in the hash table
15908                 ;; (what a hack!!) - jwz
15909                 (setq group (let ((obarray hashtb)) (read cur)))
15910                 (if (and (numberp (setq max (read cur)))
15911                          (numberp (setq min (read cur)))
15912                          (progn
15913                            (skip-chars-forward " \t")
15914                            (not
15915                             (or (= (following-char) ?=)
15916                                 (= (following-char) ?x)
15917                                 (= (following-char) ?j)))))
15918                     (set group (cons min max))
15919                   (set group nil)))
15920             (error
15921              (progn
15922                (and group
15923                     (symbolp group)
15924                     (set group nil))
15925                (or ignore-errors
15926                    (gnus-message 3 "Warning - illegal active: %s"
15927                                  (buffer-substring
15928                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
15929           (widen)
15930           (forward-line 1))))))
15931
15932 (defun gnus-groups-to-gnus-format (method &optional hashtb)
15933   ;; Parse a "groups" active file.
15934   (let ((cur (current-buffer))
15935         (hashtb (or hashtb
15936                     (if (and method gnus-active-hashtb)
15937                         gnus-active-hashtb
15938                       (setq gnus-active-hashtb
15939                             (gnus-make-hashtable
15940                              (count-lines (point-min) (point-max)))))))
15941         (prefix (and method
15942                      (not (gnus-server-equal
15943                            (gnus-server-get-method nil method)
15944                            (gnus-server-get-method nil gnus-select-method)))
15945                      (gnus-group-prefixed-name "" method))))
15946
15947     (goto-char (point-min))
15948     ;; We split this into to separate loops, one with the prefix
15949     ;; and one without to speed the reading up somewhat.
15950     (if prefix
15951         (let (min max opoint group)
15952           (while (not (eobp))
15953             (condition-case ()
15954                 (progn
15955                   (read cur) (read cur)
15956                   (setq min (read cur)
15957                         max (read cur)
15958                         opoint (point))
15959                   (skip-chars-forward " \t")
15960                   (insert prefix)
15961                   (goto-char opoint)
15962                   (set (let ((obarray hashtb)) (read cur))
15963                        (cons min max)))
15964               (error (and group (symbolp group) (set group nil))))
15965             (forward-line 1)))
15966       (let (min max group)
15967         (while (not (eobp))
15968           (condition-case ()
15969               (if (= (following-char) ?2)
15970                   (progn
15971                     (read cur) (read cur)
15972                     (setq min (read cur)
15973                           max (read cur))
15974                     (set (setq group (let ((obarray hashtb)) (read cur)))
15975                          (cons min max))))
15976             (error (and group (symbolp group) (set group nil))))
15977           (forward-line 1))))))
15978
15979 (defun gnus-read-newsrc-file (&optional force)
15980   "Read startup file.
15981 If FORCE is non-nil, the .newsrc file is read."
15982   ;; Reset variables that might be defined in the .newsrc.eld file.
15983   (let ((variables gnus-variable-list))
15984     (while variables
15985       (set (car variables) nil)
15986       (setq variables (cdr variables))))
15987   (let* ((newsrc-file gnus-current-startup-file)
15988          (quick-file (concat newsrc-file ".el")))
15989     (save-excursion
15990       ;; We always load the .newsrc.eld file.  If always contains
15991       ;; much information that can not be gotten from the .newsrc
15992       ;; file (ticked articles, killed groups, foreign methods, etc.)
15993       (gnus-read-newsrc-el-file quick-file)
15994
15995       (if (and (file-exists-p gnus-current-startup-file)
15996                (or force
15997                    (and (file-newer-than-file-p newsrc-file quick-file)
15998                         (file-newer-than-file-p newsrc-file
15999                                                 (concat quick-file "d")))
16000                    (not gnus-newsrc-alist)))
16001           ;; We read the .newsrc file.  Note that if there if a
16002           ;; .newsrc.eld file exists, it has already been read, and
16003           ;; the `gnus-newsrc-hashtb' has been created.  While reading
16004           ;; the .newsrc file, Gnus will only use the information it
16005           ;; can find there for changing the data already read -
16006           ;; ie. reading the .newsrc file will not trash the data
16007           ;; already read (except for read articles).
16008           (save-excursion
16009             (gnus-message 5 "Reading %s..." newsrc-file)
16010             (set-buffer (find-file-noselect newsrc-file))
16011             (buffer-disable-undo (current-buffer))
16012             (gnus-newsrc-to-gnus-format)
16013             (kill-buffer (current-buffer))
16014             (gnus-message 5 "Reading %s...done" newsrc-file)))
16015
16016       ;; Read any slave files.
16017       (or gnus-slave
16018           (gnus-master-read-slave-newsrc)))))
16019
16020 (defun gnus-read-newsrc-el-file (file)
16021   (let ((ding-file (concat file "d")))
16022     ;; We always, always read the .eld file.
16023     (gnus-message 5 "Reading %s..." ding-file)
16024     (let (gnus-newsrc-assoc)
16025       (condition-case nil
16026           (load ding-file t t t)
16027         (error
16028          (gnus-message 1 "Error in %s" ding-file)
16029          (ding)))
16030       (when gnus-newsrc-assoc
16031         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
16032     (gnus-make-hashtable-from-newsrc-alist)
16033     (when (file-newer-than-file-p file ding-file)
16034       ;; Old format quick file
16035       (gnus-message 5 "Reading %s..." file)
16036       ;; The .el file is newer than the .eld file, so we read that one
16037       ;; as well.
16038       (gnus-read-old-newsrc-el-file file))))
16039
16040 ;; Parse the old-style quick startup file
16041 (defun gnus-read-old-newsrc-el-file (file)
16042   (let (newsrc killed marked group m info)
16043     (prog1
16044         (let ((gnus-killed-assoc nil)
16045               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
16046           (prog1
16047               (condition-case nil
16048                   (load file t t t)
16049                 (error nil))
16050             (setq newsrc gnus-newsrc-assoc
16051                   killed gnus-killed-assoc
16052                   marked gnus-marked-assoc)))
16053       (setq gnus-newsrc-alist nil)
16054       (while (setq info (gnus-get-info (setq group (pop newsrc))))
16055         (if info
16056             (progn
16057               (gnus-info-set-read info (cddr group))
16058               (gnus-info-set-level
16059                info (if (nth 1 group) gnus-level-default-subscribed
16060                       gnus-level-default-unsubscribed))
16061               (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
16062           (push (setq info
16063                       (list (car group)
16064                             (if (nth 1 group) gnus-level-default-subscribed
16065                               gnus-level-default-unsubscribed)
16066                             (cddr group)))
16067                 gnus-newsrc-alist))
16068         ;; Copy marks into info.
16069         (when (setq m (assoc (car group) marked))
16070           (unless (nthcdr 3 info)
16071             (nconc info (list nil)))
16072           (gnus-info-set-marks
16073            info (list (cons 'tick (gnus-compress-sequence 
16074                                    (sort (cdr m) '<) t))))))
16075       (setq newsrc killed)
16076       (while newsrc
16077         (setcar newsrc (caar newsrc))
16078         (setq newsrc (cdr newsrc)))
16079       (setq gnus-killed-list killed))
16080     ;; The .el file version of this variable does not begin with
16081     ;; "options", while the .eld version does, so we just add it if it
16082     ;; isn't there.
16083     (and
16084      gnus-newsrc-options
16085      (progn
16086        (and (not (string-match "^ *options" gnus-newsrc-options))
16087             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
16088        (and (not (string-match "\n$" gnus-newsrc-options))
16089             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
16090        ;; Finally, if we read some options lines, we parse them.
16091        (or (string= gnus-newsrc-options "")
16092            (gnus-newsrc-parse-options gnus-newsrc-options))))
16093
16094     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
16095     (gnus-make-hashtable-from-newsrc-alist)))
16096
16097 (defun gnus-make-newsrc-file (file)
16098   "Make server dependent file name by catenating FILE and server host name."
16099   (let* ((file (expand-file-name file nil))
16100          (real-file (concat file "-" (nth 1 gnus-select-method))))
16101     (if (or (file-exists-p real-file)
16102             (file-exists-p (concat real-file ".el"))
16103             (file-exists-p (concat real-file ".eld")))
16104         real-file file)))
16105
16106 (defun gnus-newsrc-to-gnus-format ()
16107   (setq gnus-newsrc-options "")
16108   (setq gnus-newsrc-options-n nil)
16109
16110   (or gnus-active-hashtb
16111       (setq gnus-active-hashtb (make-vector 4095 0)))
16112   (let ((buf (current-buffer))
16113         (already-read (> (length gnus-newsrc-alist) 1))
16114         group subscribed options-symbol newsrc Options-symbol
16115         symbol reads num1)
16116     (goto-char (point-min))
16117     ;; We intern the symbol `options' in the active hashtb so that we
16118     ;; can `eq' against it later.
16119     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
16120     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
16121
16122     (while (not (eobp))
16123       ;; We first read the first word on the line by narrowing and
16124       ;; then reading into `gnus-active-hashtb'.  Most groups will
16125       ;; already exist in that hashtb, so this will save some string
16126       ;; space.
16127       (narrow-to-region
16128        (point)
16129        (progn (skip-chars-forward "^ \t!:\n") (point)))
16130       (goto-char (point-min))
16131       (setq symbol
16132             (and (/= (point-min) (point-max))
16133                  (let ((obarray gnus-active-hashtb)) (read buf))))
16134       (widen)
16135       ;; Now, the symbol we have read is either `options' or a group
16136       ;; name.  If it is an options line, we just add it to a string.
16137       (cond
16138        ((or (eq symbol options-symbol)
16139             (eq symbol Options-symbol))
16140         (setq gnus-newsrc-options
16141               ;; This concating is quite inefficient, but since our
16142               ;; thorough studies show that approx 99.37% of all
16143               ;; .newsrc files only contain a single options line, we
16144               ;; don't give a damn, frankly, my dear.
16145               (concat gnus-newsrc-options
16146                       (buffer-substring
16147                        (gnus-point-at-bol)
16148                        ;; Options may continue on the next line.
16149                        (or (and (re-search-forward "^[^ \t]" nil 'move)
16150                                 (progn (beginning-of-line) (point)))
16151                            (point)))))
16152         (forward-line -1))
16153        (symbol
16154         ;; Group names can be just numbers.  
16155         (when (numberp symbol) 
16156           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
16157         (or (boundp symbol) (set symbol nil))
16158         ;; It was a group name.
16159         (setq subscribed (= (following-char) ?:)
16160               group (symbol-name symbol)
16161               reads nil)
16162         (if (eolp)
16163             ;; If the line ends here, this is clearly a buggy line, so
16164             ;; we put point a the beginning of line and let the cond
16165             ;; below do the error handling.
16166             (beginning-of-line)
16167           ;; We skip to the beginning of the ranges.
16168           (skip-chars-forward "!: \t"))
16169         ;; We are now at the beginning of the list of read articles.
16170         ;; We read them range by range.
16171         (while
16172             (cond
16173              ((looking-at "[0-9]+")
16174               ;; We narrow and read a number instead of buffer-substring/
16175               ;; string-to-int because it's faster.  narrow/widen is
16176               ;; faster than save-restriction/narrow, and save-restriction
16177               ;; produces a garbage object.
16178               (setq num1 (progn
16179                            (narrow-to-region (match-beginning 0) (match-end 0))
16180                            (read buf)))
16181               (widen)
16182               ;; If the next character is a dash, then this is a range.
16183               (if (= (following-char) ?-)
16184                   (progn
16185                     ;; We read the upper bound of the range.
16186                     (forward-char 1)
16187                     (if (not (looking-at "[0-9]+"))
16188                         ;; This is a buggy line, by we pretend that
16189                         ;; it's kinda OK.  Perhaps the user should be
16190                         ;; dinged?
16191                         (setq reads (cons num1 reads))
16192                       (setq reads
16193                             (cons
16194                              (cons num1
16195                                    (progn
16196                                      (narrow-to-region (match-beginning 0)
16197                                                        (match-end 0))
16198                                      (read buf)))
16199                              reads))
16200                       (widen)))
16201                 ;; It was just a simple number, so we add it to the
16202                 ;; list of ranges.
16203                 (setq reads (cons num1 reads)))
16204               ;; If the next char in ?\n, then we have reached the end
16205               ;; of the line and return nil.
16206               (/= (following-char) ?\n))
16207              ((= (following-char) ?\n)
16208               ;; End of line, so we end.
16209               nil)
16210              (t
16211               ;; Not numbers and not eol, so this might be a buggy
16212               ;; line...
16213               (or (eobp)
16214                   ;; If it was eob instead of ?\n, we allow it.
16215                   (progn
16216                     ;; The line was buggy.
16217                     (setq group nil)
16218                     (gnus-message 3 "Mangled line: %s"
16219                                   (buffer-substring (gnus-point-at-bol)
16220                                                     (gnus-point-at-eol)))
16221                     (ding)
16222                     (sit-for 1)))
16223               nil))
16224           ;; Skip past ", ".  Spaces are illegal in these ranges, but
16225           ;; we allow them, because it's a common mistake to put a
16226           ;; space after the comma.
16227           (skip-chars-forward ", "))
16228
16229         ;; We have already read .newsrc.eld, so we gently update the
16230         ;; data in the hash table with the information we have just
16231         ;; read.
16232         (when group
16233           (let ((info (gnus-get-info group))
16234                 level)
16235             (if info
16236                 ;; There is an entry for this file in the alist.
16237                 (progn
16238                   (gnus-info-set-read info (nreverse reads))
16239                   ;; We update the level very gently.  In fact, we
16240                   ;; only change it if there's been a status change
16241                   ;; from subscribed to unsubscribed, or vice versa.
16242                   (setq level (gnus-info-level info))
16243                   (cond ((and (<= level gnus-level-subscribed)
16244                               (not subscribed))
16245                          (setq level (if reads
16246                                          gnus-level-default-unsubscribed
16247                                        (1+ gnus-level-default-unsubscribed))))
16248                         ((and (> level gnus-level-subscribed) subscribed)
16249                          (setq level gnus-level-default-subscribed)))
16250                   (gnus-info-set-level info level))
16251               ;; This is a new group.
16252               (setq info (list group
16253                                (if subscribed
16254                                    gnus-level-default-subscribed
16255                                  (if reads
16256                                      (1+ gnus-level-subscribed)
16257                                    gnus-level-default-unsubscribed))
16258                                (nreverse reads))))
16259             (setq newsrc (cons info newsrc))))))
16260       (forward-line 1))
16261
16262     (setq newsrc (nreverse newsrc))
16263
16264     (if (not already-read)
16265         ()
16266       ;; We now have two newsrc lists - `newsrc', which is what we
16267       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16268       ;; what we've read from .newsrc.eld.  We have to merge these
16269       ;; lists.  We do this by "attaching" any (foreign) groups in the
16270       ;; gnus-newsrc-alist to the (native) group that precedes them.
16271       (let ((rc (cdr gnus-newsrc-alist))
16272             (prev gnus-newsrc-alist)
16273             entry mentry)
16274         (while rc
16275           (or (null (nth 4 (car rc)))   ; It's a native group.
16276               (assoc (caar rc) newsrc) ; It's already in the alist.
16277               (if (setq entry (assoc (caar prev) newsrc))
16278                   (setcdr (setq mentry (memq entry newsrc))
16279                           (cons (car rc) (cdr mentry)))
16280                 (setq newsrc (cons (car rc) newsrc))))
16281           (setq prev rc
16282                 rc (cdr rc)))))
16283
16284     (setq gnus-newsrc-alist newsrc)
16285     ;; We make the newsrc hashtb.
16286     (gnus-make-hashtable-from-newsrc-alist)
16287
16288     ;; Finally, if we read some options lines, we parse them.
16289     (or (string= gnus-newsrc-options "")
16290         (gnus-newsrc-parse-options gnus-newsrc-options))))
16291
16292 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16293 ;; The return value will be a list on the form
16294 ;; ((regexp1 . ignore)
16295 ;;  (regexp2 . subscribe)...)
16296 ;; When handling new newsgroups, groups that match a `ignore' regexp
16297 ;; will be ignored, and groups that match a `subscribe' regexp will be
16298 ;; subscribed.  A line like
16299 ;; options -n !all rec.all
16300 ;; will lead to a list that looks like
16301 ;; (("^rec\\..+" . subscribe)
16302 ;;  ("^.+" . ignore))
16303 ;; So all "rec.*" groups will be subscribed, while all the other
16304 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16305 ;; different from "options -n rec.all !all".
16306 (defun gnus-newsrc-parse-options (options)
16307   (let (out eol)
16308     (save-excursion
16309       (gnus-set-work-buffer)
16310       (insert (regexp-quote options))
16311       ;; First we treat all continuation lines.
16312       (goto-char (point-min))
16313       (while (re-search-forward "\n[ \t]+" nil t)
16314         (replace-match " " t t))
16315       ;; Then we transform all "all"s into ".+"s.
16316       (goto-char (point-min))
16317       (while (re-search-forward "\\ball\\b" nil t)
16318         (replace-match ".+" t t))
16319       (goto-char (point-min))
16320       ;; We remove all other options than the "-n" ones.
16321       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16322         (replace-match " ")
16323         (forward-char -1))
16324       (goto-char (point-min))
16325
16326       ;; We are only interested in "options -n" lines - we
16327       ;; ignore the other option lines.
16328       (while (re-search-forward "[ \t]-n" nil t)
16329         (setq eol
16330               (or (save-excursion
16331                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16332                          (- (point) 2)))
16333                   (gnus-point-at-eol)))
16334         ;; Search for all "words"...
16335         (while (re-search-forward "[^ \t,\n]+" eol t)
16336           (if (= (char-after (match-beginning 0)) ?!)
16337               ;; If the word begins with a bang (!), this is a "not"
16338               ;; spec.  We put this spec (minus the bang) and the
16339               ;; symbol `ignore' into the list.
16340               (setq out (cons (cons (concat
16341                                      "^" (buffer-substring
16342                                           (1+ (match-beginning 0))
16343                                           (match-end 0)))
16344                                     'ignore) out))
16345             ;; There was no bang, so this is a "yes" spec.
16346             (setq out (cons (cons (concat "^" (match-string 0))
16347                                   'subscribe) out)))))
16348
16349       (setq gnus-newsrc-options-n out))))
16350
16351 (defun gnus-save-newsrc-file (&optional force)
16352   "Save .newsrc file."
16353   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16354   ;; from the variable gnus-newsrc-alist.
16355   (when (and (or gnus-newsrc-alist gnus-killed-list)
16356              gnus-current-startup-file)
16357     (save-excursion
16358       (if (and (or gnus-use-dribble-file gnus-slave)
16359                (not force)
16360                (or (not gnus-dribble-buffer)
16361                    (not (buffer-name gnus-dribble-buffer))
16362                    (zerop (save-excursion
16363                             (set-buffer gnus-dribble-buffer)
16364                             (buffer-size)))))
16365           (gnus-message 4 "(No changes need to be saved)")
16366         (run-hooks 'gnus-save-newsrc-hook)
16367         (if gnus-slave
16368             (gnus-slave-save-newsrc)
16369           ;; Save .newsrc.
16370           (when gnus-save-newsrc-file
16371             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16372             (gnus-gnus-to-newsrc-format)
16373             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16374           ;; Save .newsrc.eld.
16375           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16376           (make-local-variable 'version-control)
16377           (setq version-control 'never)
16378           (setq buffer-file-name
16379                 (concat gnus-current-startup-file ".eld"))
16380           (gnus-add-current-to-buffer-list)
16381           (buffer-disable-undo (current-buffer))
16382           (erase-buffer)
16383           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16384           (gnus-gnus-to-quick-newsrc-format)
16385           (run-hooks 'gnus-save-quick-newsrc-hook)
16386           (save-buffer)
16387           (kill-buffer (current-buffer))
16388           (gnus-message
16389            5 "Saving %s.eld...done" gnus-current-startup-file))
16390         (gnus-dribble-delete-file)))))
16391
16392 (defun gnus-gnus-to-quick-newsrc-format ()
16393   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16394   (insert ";; Gnus startup file.\n")
16395   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16396   (insert ";; to read .newsrc.\n")
16397   (insert "(setq gnus-newsrc-file-version "
16398           (prin1-to-string gnus-version) ")\n")
16399   (let ((variables
16400          (if gnus-save-killed-list gnus-variable-list
16401            ;; Remove the `gnus-killed-list' from the list of variables
16402            ;; to be saved, if required.
16403            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16404         ;; Peel off the "dummy" group.
16405         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16406         variable)
16407     ;; Insert the variables into the file.
16408     (while variables
16409       (when (and (boundp (setq variable (pop variables)))
16410                  (symbol-value variable))
16411         (insert "(setq " (symbol-name variable) " '")
16412         (prin1 (symbol-value variable) (current-buffer))
16413         (insert ")\n")))))
16414
16415 (defun gnus-gnus-to-newsrc-format ()
16416   ;; Generate and save the .newsrc file.
16417   (save-excursion
16418     (set-buffer (create-file-buffer gnus-current-startup-file))
16419     (let ((newsrc (cdr gnus-newsrc-alist))
16420           (standard-output (current-buffer))
16421           info ranges range method)
16422       (setq buffer-file-name gnus-current-startup-file)
16423       (buffer-disable-undo (current-buffer))
16424       (erase-buffer)
16425       ;; Write options.
16426       (if gnus-newsrc-options (insert gnus-newsrc-options))
16427       ;; Write subscribed and unsubscribed.
16428       (while (setq info (pop newsrc))
16429         ;; Don't write foreign groups to .newsrc.
16430         (when (or (null (setq method (gnus-info-method info)))
16431                   (equal method "native")
16432                   (gnus-server-equal method gnus-select-method))
16433           (insert (gnus-info-group info)
16434                   (if (> (gnus-info-level info) gnus-level-subscribed)
16435                       "!" ":"))
16436           (when (setq ranges (gnus-info-read info))
16437             (insert " ")
16438             (if (not (listp (cdr ranges)))
16439                 (if (= (car ranges) (cdr ranges))
16440                     (princ (car ranges))
16441                   (princ (car ranges))
16442                   (insert "-")
16443                   (princ (cdr ranges)))
16444               (while (setq range (pop ranges))
16445                 (if (or (atom range) (= (car range) (cdr range)))
16446                     (princ (or (and (atom range) range) (car range)))
16447                   (princ (car range))
16448                   (insert "-")
16449                   (princ (cdr range)))
16450                 (if ranges (insert ",")))))
16451           (insert "\n")))
16452       (make-local-variable 'version-control)
16453       (setq version-control 'never)
16454       ;; It has been reported that sometime the modtime on the .newsrc
16455       ;; file seems to be off.  We really do want to overwrite it, so
16456       ;; we clear the modtime here before saving.  It's a bit odd,
16457       ;; though...
16458       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16459       ;; delete the silly thing entirely first.  but this fails to provide
16460       ;; such niceties as .newsrc~ creation.
16461       (if gnus-modtime-botch
16462           (delete-file gnus-startup-file)
16463         (clear-visited-file-modtime))
16464       (run-hooks 'gnus-save-standard-newsrc-hook)
16465       (save-buffer)
16466       (kill-buffer (current-buffer)))))
16467
16468 \f
16469 ;;;
16470 ;;; Slave functions.
16471 ;;;
16472
16473 (defun gnus-slave-save-newsrc ()
16474   (save-excursion
16475     (set-buffer gnus-dribble-buffer)
16476     (let ((slave-name
16477            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16478       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16479
16480 (defun gnus-master-read-slave-newsrc ()
16481   (let ((slave-files
16482          (directory-files
16483           (file-name-directory gnus-current-startup-file)
16484           t (concat
16485              "^" (regexp-quote
16486                   (concat
16487                    (file-name-nondirectory gnus-current-startup-file)
16488                    "-slave-")))
16489           t))
16490         file)
16491     (if (not slave-files)
16492         ()                              ; There are no slave files to read.
16493       (gnus-message 7 "Reading slave newsrcs...")
16494       (save-excursion
16495         (set-buffer (get-buffer-create " *gnus slave*"))
16496         (buffer-disable-undo (current-buffer))
16497         (setq slave-files
16498               (sort (mapcar (lambda (file)
16499                               (list (nth 5 (file-attributes file)) file))
16500                             slave-files)
16501                     (lambda (f1 f2)
16502                       (or (< (caar f1) (caar f2))
16503                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16504         (while slave-files
16505           (erase-buffer)
16506           (setq file (nth 1 (car slave-files)))
16507           (insert-file-contents file)
16508           (if (condition-case ()
16509                   (progn
16510                     (eval-buffer (current-buffer))
16511                     t)
16512                 (error
16513                  (gnus-message 3 "Possible error in %s" file)
16514                  (ding)
16515                  (sit-for 2)
16516                  nil))
16517               (or gnus-slave ; Slaves shouldn't delete these files.
16518                   (condition-case ()
16519                       (delete-file file)
16520                     (error nil))))
16521           (setq slave-files (cdr slave-files))))
16522       (gnus-message 7 "Reading slave newsrcs...done"))))
16523
16524 \f
16525 ;;;
16526 ;;; Group description.
16527 ;;;
16528
16529 (defun gnus-read-all-descriptions-files ()
16530   (let ((methods (cons gnus-select-method 
16531                        (nconc
16532                         (when gnus-message-archive-method
16533                           (list "archive"))
16534                         gnus-secondary-select-methods))))
16535     (while methods
16536       (gnus-read-descriptions-file (car methods))
16537       (setq methods (cdr methods)))
16538     t))
16539
16540 (defun gnus-read-descriptions-file (&optional method)
16541   (let ((method (or method gnus-select-method)))
16542     (when (stringp method)
16543       (setq method (gnus-server-to-method method)))
16544     ;; We create the hashtable whether we manage to read the desc file
16545     ;; to avoid trying to re-read after a failed read.
16546     (or gnus-description-hashtb
16547         (setq gnus-description-hashtb
16548               (gnus-make-hashtable (length gnus-active-hashtb))))
16549     ;; Mark this method's desc file as read.
16550     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16551                   gnus-description-hashtb)
16552
16553     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16554     (cond
16555      ((not (gnus-check-server method))
16556       (gnus-message 1 "Couldn't open server")
16557       nil)
16558      ((not (gnus-request-list-newsgroups method))
16559       (gnus-message 1 "Couldn't read newsgroups descriptions")
16560       nil)
16561      (t
16562       (let (group)
16563         (save-excursion
16564           (save-restriction
16565             (set-buffer nntp-server-buffer)
16566             (goto-char (point-min))
16567             (if (or (search-forward "\n.\n" nil t)
16568                     (goto-char (point-max)))
16569                 (progn
16570                   (beginning-of-line)
16571                   (narrow-to-region (point-min) (point))))
16572             (goto-char (point-min))
16573             (while (not (eobp))
16574               ;; If we get an error, we set group to 0, which is not a
16575               ;; symbol...
16576               (setq group
16577                     (condition-case ()
16578                         (let ((obarray gnus-description-hashtb))
16579                           ;; Group is set to a symbol interned in this
16580                           ;; hash table.
16581                           (read nntp-server-buffer))
16582                       (error 0)))
16583               (skip-chars-forward " \t")
16584               ;; ...  which leads to this line being effectively ignored.
16585               (and (symbolp group)
16586                    (set group (buffer-substring
16587                                (point) (progn (end-of-line) (point)))))
16588               (forward-line 1))))
16589         (gnus-message 5 "Reading descriptions file...done")
16590         t)))))
16591
16592 (defun gnus-group-get-description (group)
16593   "Get the description of a group by sending XGTITLE to the server."
16594   (when (gnus-request-group-description group)
16595     (save-excursion
16596       (set-buffer nntp-server-buffer)
16597       (goto-char (point-min))
16598       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
16599         (match-string 1)))))
16600
16601 \f
16602 ;;;
16603 ;;; Buffering of read articles.
16604 ;;;
16605
16606 (defvar gnus-backlog-buffer " *Gnus Backlog*")
16607 (defvar gnus-backlog-articles nil)
16608 (defvar gnus-backlog-hashtb nil)
16609
16610 (defun gnus-backlog-buffer ()
16611   "Return the backlog buffer."
16612   (or (get-buffer gnus-backlog-buffer)
16613       (save-excursion
16614         (set-buffer (get-buffer-create gnus-backlog-buffer))
16615         (buffer-disable-undo (current-buffer))
16616         (setq buffer-read-only t)
16617         (gnus-add-current-to-buffer-list)
16618         (get-buffer gnus-backlog-buffer))))
16619
16620 (defun gnus-backlog-setup ()
16621   "Initialize backlog variables."
16622   (unless gnus-backlog-hashtb
16623     (setq gnus-backlog-hashtb (make-vector 1023 0))))
16624
16625 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
16626
16627 (defun gnus-backlog-shutdown ()
16628   "Clear all backlog variables and buffers."
16629   (when (get-buffer gnus-backlog-buffer)
16630     (kill-buffer gnus-backlog-buffer))
16631   (setq gnus-backlog-hashtb nil
16632         gnus-backlog-articles nil))
16633
16634 (defun gnus-backlog-enter-article (group number buffer)
16635   (gnus-backlog-setup)
16636   (let ((ident (intern (concat group ":" (int-to-string number))
16637                        gnus-backlog-hashtb))
16638         b)
16639     (if (memq ident gnus-backlog-articles)
16640         () ; It's already kept.
16641       ;; Remove the oldest article, if necessary.
16642       (and (numberp gnus-keep-backlog)
16643            (>= (length gnus-backlog-articles) gnus-keep-backlog)
16644            (gnus-backlog-remove-oldest-article))
16645       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
16646       ;; Insert the new article.
16647       (save-excursion
16648         (set-buffer (gnus-backlog-buffer))
16649         (let (buffer-read-only)
16650           (goto-char (point-max))
16651           (or (bolp) (insert "\n"))
16652           (setq b (point))
16653           (insert-buffer-substring buffer)
16654           ;; Tag the beginning of the article with the ident.
16655           (put-text-property b (1+ b) 'gnus-backlog ident))))))
16656
16657 (defun gnus-backlog-remove-oldest-article ()
16658   (save-excursion
16659     (set-buffer (gnus-backlog-buffer))
16660     (goto-char (point-min))
16661     (if (zerop (buffer-size))
16662         () ; The buffer is empty.
16663       (let ((ident (get-text-property (point) 'gnus-backlog))
16664             buffer-read-only)
16665         ;; Remove the ident from the list of articles.
16666         (when ident
16667           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16668         ;; Delete the article itself.
16669         (delete-region
16670          (point) (next-single-property-change
16671                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
16672
16673 (defun gnus-backlog-request-article (group number buffer)
16674   (when (numberp number)
16675     (gnus-backlog-setup)
16676     (let ((ident (intern (concat group ":" (int-to-string number))
16677                          gnus-backlog-hashtb))
16678           beg end)
16679       (when (memq ident gnus-backlog-articles)
16680         ;; It was in the backlog.
16681         (save-excursion
16682           (set-buffer (gnus-backlog-buffer))
16683           (if (not (setq beg (text-property-any
16684                               (point-min) (point-max) 'gnus-backlog
16685                               ident)))
16686               ;; It wasn't in the backlog after all.
16687               (ignore
16688                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16689             ;; Find the end (i. e., the beginning of the next article).
16690             (setq end
16691                   (next-single-property-change
16692                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
16693         (let ((buffer-read-only nil))
16694           (erase-buffer)
16695           (insert-buffer-substring gnus-backlog-buffer beg end)
16696           t)))))
16697
16698 ;; Allow redefinition of Gnus functions.
16699
16700 (gnus-ems-redefine)
16701
16702 (provide 'gnus)
16703
16704 ;;; gnus.el ends here