*** empty log message ***
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95,96 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval '(run-hooks 'gnus-load-hook))
30
31 (require 'mail-utils)
32 (require 'timezone)
33 (require 'nnheader)
34 (require 'message)
35 (require 'nnmail)
36 (require 'backquote)
37
38 (eval-when-compile (require 'cl))
39
40 (defvar gnus-directory (or (getenv "SAVEDIR") "~/News/")
41   "*Directory variable from which all other Gnus file variables are derived.")
42
43 ;; Site dependent variables.  These variables should be defined in
44 ;; paths.el.
45
46 (defvar gnus-default-nntp-server nil
47   "Specify a default NNTP server.
48 This variable should be defined in paths.el, and should never be set
49 by the user.
50 If you want to change servers, you should use `gnus-select-method'.
51 See the documentation to that variable.")
52
53 (defvar gnus-backup-default-subscribed-newsgroups
54   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
55   "Default default new newsgroups the first time Gnus is run.
56 Should be set in paths.el, and shouldn't be touched by the user.")
57
58 (defvar gnus-local-domain nil
59   "Local domain name without a host name.
60 The DOMAINNAME environment variable is used instead if it is defined.
61 If the `system-name' function returns the full Internet name, there is
62 no need to set this variable.")
63
64 (defvar gnus-local-organization nil
65   "String with a description of what organization (if any) the user belongs to.
66 The ORGANIZATION environment variable is used instead if it is defined.
67 If this variable contains a function, this function will be called
68 with the current newsgroup name as the argument.  The function should
69 return a string.
70
71 In any case, if the string (either in the variable, in the environment
72 variable, or returned by the function) is a file name, the contents of
73 this file will be used as the organization.")
74
75 ;; Customization variables
76
77 ;; Don't touch this variable.
78 (defvar gnus-nntp-service "nntp"
79   "*NNTP service name (\"nntp\" or 119).
80 This is an obsolete variable, which is scarcely used.  If you use an
81 nntp server for your newsgroup and want to change the port number
82 used to 899, you would say something along these lines:
83
84  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
85
86 (defvar gnus-nntpserver-file "/etc/nntpserver"
87   "*A file with only the name of the nntp server in it.")
88
89 ;; This function is used to check both the environment variable
90 ;; NNTPSERVER and the /etc/nntpserver file to see whether one can find
91 ;; an nntp server name default.
92 (defun gnus-getenv-nntpserver ()
93   (or (getenv "NNTPSERVER")
94       (and (file-readable-p gnus-nntpserver-file)
95            (save-excursion
96              (set-buffer (get-buffer-create " *gnus nntp*"))
97              (buffer-disable-undo (current-buffer))
98              (insert-file-contents gnus-nntpserver-file)
99              (let ((name (buffer-string)))
100                (prog1
101                    (if (string-match "^[ \t\n]*$" name)
102                        nil
103                      name)
104                  (kill-buffer (current-buffer))))))))
105
106 (defvar gnus-select-method
107   (nconc
108    (list 'nntp (or (condition-case ()
109                        (gnus-getenv-nntpserver)
110                      (error nil))
111                    (if (and gnus-default-nntp-server
112                             (not (string= gnus-default-nntp-server "")))
113                        gnus-default-nntp-server)
114                    (system-name)))
115    (if (or (null gnus-nntp-service)
116            (equal gnus-nntp-service "nntp"))
117        nil
118      (list gnus-nntp-service)))
119   "*Default method for selecting a newsgroup.
120 This variable should be a list, where the first element is how the
121 news is to be fetched, the second is the address.
122
123 For instance, if you want to get your news via NNTP from
124 \"flab.flab.edu\", you could say:
125
126 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
127
128 If you want to use your local spool, say:
129
130 (setq gnus-select-method (list 'nnspool (system-name)))
131
132 If you use this variable, you must set `gnus-nntp-server' to nil.
133
134 There is a lot more to know about select methods and virtual servers -
135 see the manual for details.")
136
137 (defvar gnus-message-archive-method 
138   `(nnfolder
139     "archive"
140     (nnfolder-directory ,(nnheader-concat message-directory "archive"))
141     (nnfolder-active-file 
142      ,(nnheader-concat message-directory "archive/active"))
143     (nnfolder-get-new-mail nil)
144     (nnfolder-inhibit-expiry t))
145   "*Method used for archiving messages you've sent.
146 This should be a mail method.
147
148 It's probably not a very effective to change this variable once you've
149 run Gnus once.  After doing that, you must edit this server from the
150 server buffer.")
151
152 (defvar gnus-refer-article-method nil
153   "*Preferred method for fetching an article by Message-ID.
154 If you are reading news from the local spool (with nnspool), fetching
155 articles by Message-ID is painfully slow.  By setting this method to an
156 nntp method, you might get acceptable results.
157
158 The value of this variable must be a valid select method as discussed
159 in the documentation of `gnus-select-method'.")
160
161 (defvar gnus-secondary-select-methods nil
162   "*A list of secondary methods that will be used for reading news.
163 This is a list where each element is a complete select method (see
164 `gnus-select-method').
165
166 If, for instance, you want to read your mail with the nnml backend,
167 you could set this variable:
168
169 (setq gnus-secondary-select-methods '((nnml \"\")))")
170
171 (defvar gnus-secondary-servers nil
172   "*List of NNTP servers that the user can choose between interactively.
173 To make Gnus query you for a server, you have to give `gnus' a
174 non-numeric prefix - `C-u M-x gnus', in short.")
175
176 (defvar gnus-nntp-server nil
177   "*The name of the host running the NNTP server.
178 This variable is semi-obsolete.  Use the `gnus-select-method'
179 variable instead.")
180
181 (defvar gnus-startup-file "~/.newsrc"
182   "*Your `.newsrc' file.
183 `.newsrc-SERVER' will be used instead if that exists.")
184
185 (defvar gnus-init-file "~/.gnus"
186   "*Your Gnus elisp startup file.
187 If a file with the .el or .elc suffixes exist, it will be read
188 instead.")
189
190 (defvar gnus-group-faq-directory
191   '("/ftp@mirrors.aol.com:/pub/rtfm/usenet/"
192     "/ftp@sunsite.auc.dk:/pub/usenet/"
193     "/ftp@sunsite.doc.ic.ac.uk:/pub/usenet/news-faqs/"
194     "/ftp@src.doc.ic.ac.uk:/usenet/news-FAQS/"
195     "/ftp@ftp.seas.gwu.edu:/pub/rtfm/"
196     "/ftp@rtfm.mit.edu:/pub/usenet/"
197     "/ftp@ftp.uni-paderborn.de:/pub/FAQ/"
198     "/ftp@ftp.sunet.se:/pub/usenet/"
199     "/ftp@nctuccca.edu.tw:/USENET/FAQ/"
200     "/ftp@hwarang.postech.ac.kr:/pub/usenet/"
201     "/ftp@ftp.hk.super.net:/mirror/faqs/")
202   "*Directory where the group FAQs are stored.
203 This will most commonly be on a remote machine, and the file will be
204 fetched by ange-ftp.
205
206 This variable can also be a list of directories.  In that case, the
207 first element in the list will be used by default, and the others will
208 be used as backup sites.
209
210 Note that Gnus uses an aol machine as the default directory.  If this
211 feels fundamentally unclean, just think of it as a way to finally get
212 something of value back from them.
213
214 If the default site is too slow, try one of these:
215
216    North America: mirrors.aol.com                /pub/rtfm/usenet
217                   ftp.seas.gwu.edu               /pub/rtfm
218                   rtfm.mit.edu                   /pub/usenet
219    Europe:        ftp.uni-paderborn.de           /pub/FAQ
220                   src.doc.ic.ac.uk               /usenet/news-FAQS
221                   ftp.sunet.se                   /pub/usenet
222                   sunsite.auc.dk                 /pub/usenet
223    Asia:          nctuccca.edu.tw                /USENET/FAQ
224                   hwarang.postech.ac.kr          /pub/usenet
225                   ftp.hk.super.net               /mirror/faqs")
226
227 (defvar gnus-group-archive-directory
228   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/"
229   "*The address of the (ding) archives.")
230
231 (defvar gnus-group-recent-archive-directory
232   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
233   "*The address of the most recent (ding) articles.")
234
235 (defvar gnus-default-subscribed-newsgroups nil
236   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
237 It should be a list of strings.
238 If it is `t', Gnus will not do anything special the first time it is
239 started; it'll just use the normal newsgroups subscription methods.")
240
241 (defvar gnus-use-cross-reference t
242   "*Non-nil means that cross referenced articles will be marked as read.
243 If nil, ignore cross references.  If t, mark articles as read in
244 subscribed newsgroups.  If neither t nor nil, mark as read in all
245 newsgroups.")
246
247 (defvar gnus-single-article-buffer t
248   "*If non-nil, display all articles in the same buffer.
249 If nil, each group will get its own article buffer.")
250
251 (defvar gnus-use-dribble-file t
252   "*Non-nil means that Gnus will use a dribble file to store user updates.
253 If Emacs should crash without saving the .newsrc files, complete
254 information can be restored from the dribble file.")
255
256 (defvar gnus-dribble-directory nil
257   "*The directory where dribble files will be saved.
258 If this variable is nil, the directory where the .newsrc files are
259 saved will be used.")
260
261 (defvar gnus-asynchronous nil
262   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
263
264 (defvar gnus-kill-summary-on-exit t
265   "*If non-nil, kill the summary buffer when you exit from it.
266 If nil, the summary will become a \"*Dead Summary*\" buffer, and
267 it will be killed sometime later.")
268
269 (defvar gnus-large-newsgroup 200
270   "*The number of articles which indicates a large newsgroup.
271 If the number of articles in a newsgroup is greater than this value,
272 confirmation is required for selecting the newsgroup.")
273
274 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
275 (defvar gnus-no-groups-message "No news is horrible news"
276   "*Message displayed by Gnus when no groups are available.")
277
278 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
279   "*Non-nil means that the default name of a file to save articles in is the group name.
280 If it's nil, the directory form of the group name is used instead.
281
282 If this variable is a list, and the list contains the element
283 `not-score', long file names will not be used for score files; if it
284 contains the element `not-save', long file names will not be used for
285 saving; and if it contains the element `not-kill', long file names
286 will not be used for kill files.
287
288 Note that the default for this variable varies according to what system
289 type you're using.  On `usg-unix-v' and `xenix' this variable defaults
290 to nil while on all other systems it defaults to t.")
291
292 (defvar gnus-article-save-directory gnus-directory
293   "*Name of the directory articles will be saved in (default \"~/News\").")
294
295 (defvar gnus-kill-files-directory gnus-directory
296   "*Name of the directory where kill files will be stored (default \"~/News\").")
297
298 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
299   "*A function to save articles in your favorite format.
300 The function must be interactively callable (in other words, it must
301 be an Emacs command).
302
303 Gnus provides the following functions:
304
305 * gnus-summary-save-in-rmail (Rmail format)
306 * gnus-summary-save-in-mail (Unix mail format)
307 * gnus-summary-save-in-folder (MH folder)
308 * gnus-summary-save-in-file (article format).
309 * gnus-summary-save-in-vm (use VM's folder format).")
310
311 (defvar gnus-prompt-before-saving 'always
312   "*This variable says how much prompting is to be done when saving articles.
313 If it is nil, no prompting will be done, and the articles will be
314 saved to the default files.  If this variable is `always', each and
315 every article that is saved will be preceded by a prompt, even when
316 saving large batches of articles.  If this variable is neither nil not
317 `always', there the user will be prompted once for a file name for
318 each invocation of the saving commands.")
319
320 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
321   "*A function generating a file name to save articles in Rmail format.
322 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
323
324 (defvar gnus-mail-save-name (function gnus-plain-save-name)
325   "*A function generating a file name to save articles in Unix mail format.
326 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
327
328 (defvar gnus-folder-save-name (function gnus-folder-save-name)
329   "*A function generating a file name to save articles in MH folder.
330 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
331
332 (defvar gnus-file-save-name (function gnus-numeric-save-name)
333   "*A function generating a file name to save articles in article format.
334 The function is called with NEWSGROUP, HEADERS, and optional
335 LAST-FILE.")
336
337 (defvar gnus-split-methods
338   '((gnus-article-archive-name))
339   "*Variable used to suggest where articles are to be saved.
340 For instance, if you would like to save articles related to Gnus in
341 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
342 you could set this variable to something like:
343
344  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
345    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
346
347 This variable is an alist where the where the key is the match and the
348 value is a list of possible files to save in if the match is non-nil.
349
350 If the match is a string, it is used as a regexp match on the
351 article.  If the match is a symbol, that symbol will be funcalled
352 from the buffer of the article to be saved with the newsgroup as the
353 parameter.  If it is a list, it will be evaled in the same buffer.
354
355 If this form or function returns a string, this string will be used as
356 a possible file name; and if it returns a non-nil list, that list will
357 be used as possible file names.")
358
359 (defvar gnus-move-split-methods nil
360   "*Variable used to suggest where articles are to be moved to.
361 It uses the same syntax as the `gnus-split-methods' variable.")
362
363 (defvar gnus-save-score nil
364   "*If non-nil, save group scoring info.")
365
366 (defvar gnus-use-adaptive-scoring nil
367   "*If non-nil, use some adaptive scoring scheme.")
368
369 (defvar gnus-use-cache 'passive
370   "*If nil, Gnus will ignore the article cache.
371 If `passive', it will allow entering (and reading) articles
372 explicitly entered into the cache.  If anything else, use the
373 cache to the full extent of the law.")
374
375 (defvar gnus-use-trees nil
376   "*If non-nil, display a thread tree buffer.")
377
378 (defvar gnus-use-grouplens nil
379   "*If non-nil, use GroupLens ratings.")
380
381 (defvar gnus-keep-backlog nil
382   "*If non-nil, Gnus will keep read articles for later re-retrieval.
383 If it is a number N, then Gnus will only keep the last N articles
384 read.  If it is neither nil nor a number, Gnus will keep all read
385 articles.  This is not a good idea.")
386
387 (defvar gnus-use-nocem nil
388   "*If non-nil, Gnus will read NoCeM cancel messages.")
389
390 (defvar gnus-use-demon nil
391   "If non-nil, Gnus might use some demons.")
392
393 (defvar gnus-use-scoring t
394   "*If non-nil, enable scoring.")
395
396 (defvar gnus-use-picons nil
397   "*If non-nil, display picons.")
398
399 (defvar gnus-fetch-old-headers nil
400   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
401 If an unread article in the group refers to an older, already read (or
402 just marked as read) article, the old article will not normally be
403 displayed in the Summary buffer.  If this variable is non-nil, Gnus
404 will attempt to grab the headers to the old articles, and thereby
405 build complete threads.  If it has the value `some', only enough
406 headers to connect otherwise loose threads will be displayed.
407 This variable can also be a number.  In that case, no more than that
408 number of old headers will be fetched.
409
410 The server has to support NOV for any of this to work.")
411
412 ;see gnus-cus.el
413 ;(defvar gnus-visual t
414 ;  "*If non-nil, will do various highlighting.
415 ;If nil, no mouse highlights (or any other highlights) will be
416 ;performed.  This might speed up Gnus some when generating large group
417 ;and summary buffers.")
418
419 (defvar gnus-novice-user t
420   "*Non-nil means that you are a usenet novice.
421 If non-nil, verbose messages may be displayed and confirmations may be
422 required.")
423
424 (defvar gnus-expert-user nil
425   "*Non-nil means that you will never be asked for confirmation about anything.
426 And that means *anything*.")
427
428 (defvar gnus-verbose 7
429   "*Integer that says how verbose Gnus should be.
430 The higher the number, the more messages Gnus will flash to say what
431 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
432 display most important messages; and at ten, Gnus will keep on
433 jabbering all the time.")
434
435 (defvar gnus-keep-same-level nil
436   "*Non-nil means that the next newsgroup after the current will be on the same level.
437 When you type, for instance, `n' after reading the last article in the
438 current newsgroup, you will go to the next newsgroup.  If this variable
439 is nil, the next newsgroup will be the next from the group
440 buffer.
441 If this variable is non-nil, Gnus will either put you in the
442 next newsgroup with the same level, or, if no such newsgroup is
443 available, the next newsgroup with the lowest possible level higher
444 than the current level.
445 If this variable is `best', Gnus will make the next newsgroup the one
446 with the best level.")
447
448 (defvar gnus-summary-make-false-root 'adopt
449   "*nil means that Gnus won't gather loose threads.
450 If the root of a thread has expired or been read in a previous
451 session, the information necessary to build a complete thread has been
452 lost.  Instead of having many small sub-threads from this original thread
453 scattered all over the summary buffer, Gnus can gather them.
454
455 If non-nil, Gnus will try to gather all loose sub-threads from an
456 original thread into one large thread.
457
458 If this variable is non-nil, it should be one of `none', `adopt',
459 `dummy' or `empty'.
460
461 If this variable is `none', Gnus will not make a false root, but just
462 present the sub-threads after another.
463 If this variable is `dummy', Gnus will create a dummy root that will
464 have all the sub-threads as children.
465 If this variable is `adopt', Gnus will make one of the \"children\"
466 the parent and mark all the step-children as such.
467 If this variable is `empty', the \"children\" are printed with empty
468 subject fields.  (Or rather, they will be printed with a string
469 given by the `gnus-summary-same-subject' variable.)")
470
471 (defvar gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
472   "*A regexp to match subjects to be excluded from loose thread gathering.
473 As loose thread gathering is done on subjects only, that means that
474 there can be many false gatherings performed.  By rooting out certain
475 common subjects, gathering might become saner.")
476
477 (defvar gnus-summary-gather-subject-limit nil
478   "*Maximum length of subject comparisons when gathering loose threads.
479 Use nil to compare full subjects.  Setting this variable to a low
480 number will help gather threads that have been corrupted by
481 newsreaders chopping off subject lines, but it might also mean that
482 unrelated articles that have subject that happen to begin with the
483 same few characters will be incorrectly gathered.
484
485 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
486 comparing subjects.")
487
488 (defvar gnus-simplify-ignored-prefixes nil
489   "*Regexp, matches for which are removed from subject lines when simplifying.")
490
491 (defvar gnus-build-sparse-threads nil
492   "*If non-nil, fill in the gaps in threads.
493 If `some', only fill in the gaps that are needed to tie loose threads
494 together.  If `more', fill in all leaf nodes that Gnus can find.  If
495 non-nil and non-`some', fill in all gaps that Gnus manages to guess.")
496
497 (defvar gnus-summary-thread-gathering-function 'gnus-gather-threads-by-subject
498   "Function used for gathering loose threads.
499 There are two pre-defined functions: `gnus-gather-threads-by-subject',
500 which only takes Subjects into consideration; and
501 `gnus-gather-threads-by-references', which compared the References
502 headers of the articles to find matches.")
503
504 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
505 (defvar gnus-summary-same-subject ""
506   "*String indicating that the current article has the same subject as the previous.
507 This variable will only be used if the value of
508 `gnus-summary-make-false-root' is `empty'.")
509
510 (defvar gnus-summary-goto-unread t
511   "*If non-nil, marking commands will go to the next unread article.
512 If `never', \\<gnus-summary-mode-map>\\[gnus-summary-next-page] will go to the next article,
513 whether it is read or not.")
514
515 (defvar gnus-group-goto-unread t
516   "*If non-nil, movement commands will go to the next unread and subscribed group.")
517
518 (defvar gnus-goto-next-group-when-activating t
519   "*If non-nil, the \\<gnus-group-mode-map>\\[gnus-group-get-new-news-this-group] command will advance point to the next group.")
520
521 (defvar gnus-check-new-newsgroups t
522   "*Non-nil means that Gnus will add new newsgroups at startup.
523 If this variable is `ask-server', Gnus will ask the server for new
524 groups since the last time it checked.  This means that the killed list
525 is no longer necessary, so you could set `gnus-save-killed-list' to
526 nil.
527
528 A variant is to have this variable be a list of select methods.  Gnus
529 will then use the `ask-server' method on all these select methods to
530 query for new groups from all those servers.
531
532 Eg.
533   (setq gnus-check-new-newsgroups
534         '((nntp \"some.server\") (nntp \"other.server\")))
535
536 If this variable is nil, then you have to tell Gnus explicitly to
537 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
538
539 (defvar gnus-check-bogus-newsgroups nil
540   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
541 If this variable is nil, then you have to tell Gnus explicitly to
542 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
543
544 (defvar gnus-read-active-file t
545   "*Non-nil means that Gnus will read the entire active file at startup.
546 If this variable is nil, Gnus will only know about the groups in your
547 `.newsrc' file.
548
549 If this variable is `some', Gnus will try to only read the relevant
550 parts of the active file from the server.  Not all servers support
551 this, and it might be quite slow with other servers, but this should
552 generally be faster than both the t and nil value.
553
554 If you set this variable to nil or `some', you probably still want to
555 be told about new newsgroups that arrive.  To do that, set
556 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
557 properly with all servers.")
558
559 (defvar gnus-level-subscribed 5
560   "*Groups with levels less than or equal to this variable are subscribed.")
561
562 (defvar gnus-level-unsubscribed 7
563   "*Groups with levels less than or equal to this variable are unsubscribed.
564 Groups with levels less than `gnus-level-subscribed', which should be
565 less than this variable, are subscribed.")
566
567 (defvar gnus-level-zombie 8
568   "*Groups with this level are zombie groups.")
569
570 (defvar gnus-level-killed 9
571   "*Groups with this level are killed.")
572
573 (defvar gnus-level-default-subscribed 3
574   "*New subscribed groups will be subscribed at this level.")
575
576 (defvar gnus-level-default-unsubscribed 6
577   "*New unsubscribed groups will be unsubscribed at this level.")
578
579 (defvar gnus-activate-level (1+ gnus-level-subscribed)
580   "*Groups higher than this level won't be activated on startup.
581 Setting this variable to something log might save lots of time when
582 you have many groups that you aren't interested in.")
583
584 (defvar gnus-activate-foreign-newsgroups 4
585   "*If nil, Gnus will not check foreign newsgroups at startup.
586 If it is non-nil, it should be a number between one and nine.  Foreign
587 newsgroups that have a level lower or equal to this number will be
588 activated on startup.  For instance, if you want to active all
589 subscribed newsgroups, but not the rest, you'd set this variable to
590 `gnus-level-subscribed'.
591
592 If you subscribe to lots of newsgroups from different servers, startup
593 might take a while.  By setting this variable to nil, you'll save time,
594 but you won't be told how many unread articles there are in the
595 groups.")
596
597 (defvar gnus-save-newsrc-file t
598   "*Non-nil means that Gnus will save the `.newsrc' file.
599 Gnus always saves its own startup file, which is called
600 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
601 be readily understood by other newsreaders.  If you don't plan on
602 using other newsreaders, set this variable to nil to save some time on
603 exit.")
604
605 (defvar gnus-save-killed-list t
606   "*If non-nil, save the list of killed groups to the startup file.
607 If you set this variable to nil, you'll save both time (when starting
608 and quitting) and space (both memory and disk), but it will also mean
609 that Gnus has no record of which groups are new and which are old, so
610 the automatic new newsgroups subscription methods become meaningless.
611
612 You should always set `gnus-check-new-newsgroups' to `ask-server' or
613 nil if you set this variable to nil.")
614
615 (defvar gnus-interactive-catchup t
616   "*If non-nil, require your confirmation when catching up a group.")
617
618 (defvar gnus-interactive-exit t
619   "*If non-nil, require your confirmation when exiting Gnus.")
620
621 (defvar gnus-kill-killed t
622   "*If non-nil, Gnus will apply kill files to already killed articles.
623 If it is nil, Gnus will never apply kill files to articles that have
624 already been through the scoring process, which might very well save lots
625 of time.")
626
627 (defvar gnus-extract-address-components 'gnus-extract-address-components
628   "*Function for extracting address components from a From header.
629 Two pre-defined function exist: `gnus-extract-address-components',
630 which is the default, quite fast, and too simplistic solution, and
631 `mail-extract-address-components', which works much better, but is
632 slower.")
633
634 (defvar gnus-summary-default-score 0
635   "*Default article score level.
636 If this variable is nil, scoring will be disabled.")
637
638 (defvar gnus-summary-zcore-fuzz 0
639   "*Fuzziness factor for the zcore in the summary buffer.
640 Articles with scores closer than this to `gnus-summary-default-score'
641 will not be marked.")
642
643 (defvar gnus-simplify-subject-fuzzy-regexp nil
644   "*Strings to be removed when doing fuzzy matches.
645 This can either be a regular expression or list of regular expressions
646 that will be removed from subject strings if fuzzy subject
647 simplification is selected.")
648
649 (defvar gnus-permanently-visible-groups nil
650   "*Regexp to match groups that should always be listed in the group buffer.
651 This means that they will still be listed when there are no unread
652 articles in the groups.")
653
654 (defvar gnus-list-groups-with-ticked-articles t
655   "*If non-nil, list groups that have only ticked articles.
656 If nil, only list groups that have unread articles.")
657
658 (defvar gnus-group-default-list-level gnus-level-subscribed
659   "*Default listing level.
660 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
661
662 (defvar gnus-group-use-permanent-levels nil
663   "*If non-nil, once you set a level, Gnus will use this level.")
664
665 (defvar gnus-group-list-inactive-groups t
666   "*If non-nil, inactive groups will be listed.")
667
668 (defvar gnus-show-mime nil
669   "*If non-nil, do mime processing of articles.
670 The articles will simply be fed to the function given by
671 `gnus-show-mime-method'.")
672
673 (defvar gnus-strict-mime t
674   "*If nil, MIME-decode even if there is no Mime-Version header in the article.")
675
676 (defvar gnus-show-mime-method 'metamail-buffer
677   "*Function to process a MIME message.
678 The function is called from the article buffer.")
679
680 (defvar gnus-decode-encoded-word-method (lambda ())
681   "*Function to decode a MIME encoded-words.
682 The function is called from the article buffer.")
683
684 (defvar gnus-show-threads t
685   "*If non-nil, display threads in summary mode.")
686
687 (defvar gnus-thread-hide-subtree nil
688   "*If non-nil, hide all threads initially.
689 If threads are hidden, you have to run the command
690 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
691 to expose hidden threads.")
692
693 (defvar gnus-thread-hide-killed t
694   "*If non-nil, hide killed threads automatically.")
695
696 (defvar gnus-thread-ignore-subject nil
697   "*If non-nil, ignore subjects and do all threading based on the Reference header.
698 If nil, which is the default, articles that have different subjects
699 from their parents will start separate threads.")
700
701 (defvar gnus-thread-operation-ignore-subject t
702   "*If non-nil, subjects will be ignored when doing thread commands.
703 This affects commands like `gnus-summary-kill-thread' and
704 `gnus-summary-lower-thread'.
705
706 If this variable is nil, articles in the same thread with different
707 subjects will not be included in the operation in question.  If this
708 variable is `fuzzy', only articles that have subjects that are fuzzily
709 equal will be included.")
710
711 (defvar gnus-thread-indent-level 4
712   "*Number that says how much each sub-thread should be indented.")
713
714 (defvar gnus-ignored-newsgroups
715   (purecopy (mapconcat 'identity
716                        '("^to\\."       ; not "real" groups
717                          "^[0-9. \t]+ " ; all digits in name
718                          "[][\"#'()]"   ; bogus characters
719                          )
720                        "\\|"))
721   "*A regexp to match uninteresting newsgroups in the active file.
722 Any lines in the active file matching this regular expression are
723 removed from the newsgroup list before anything else is done to it,
724 thus making them effectively non-existent.")
725
726 (defvar gnus-ignored-headers
727   "^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:"
728   "*All headers that match this regexp will be hidden.
729 This variable can also be a list of regexps of headers to be ignored.
730 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
731
732 (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-"
733   "*All headers that do not match this regexp will be hidden.
734 This variable can also be a list of regexp of headers to remain visible.
735 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
736
737 (defvar gnus-sorted-header-list
738   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:"
739     "^Cc:" "^Date:" "^Organization:")
740   "*This variable is a list of regular expressions.
741 If it is non-nil, headers that match the regular expressions will
742 be placed first in the article buffer in the sequence specified by
743 this list.")
744
745 (defvar gnus-boring-article-headers
746   '(empty followup-to reply-to)
747   "*Headers that are only to be displayed if they have interesting data.
748 Possible values in this list are `empty', `newsgroups', `followup-to',
749 `reply-to', and `date'.")
750
751 (defvar gnus-show-all-headers nil
752   "*If non-nil, don't hide any headers.")
753
754 (defvar gnus-save-all-headers t
755   "*If non-nil, don't remove any headers before saving.")
756
757 (defvar gnus-saved-headers gnus-visible-headers
758   "*Headers to keep if `gnus-save-all-headers' is nil.
759 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
760 If that variable is nil, however, all headers that match this regexp
761 will be kept while the rest will be deleted before saving.")
762
763 (defvar gnus-inhibit-startup-message nil
764   "*If non-nil, the startup message will not be displayed.")
765
766 (defvar gnus-signature-separator "^-- *$"
767   "Regexp matching signature separator.")
768
769 (defvar gnus-signature-limit nil
770   "Provide a limit to what is considered a signature.
771 If it is a number, no signature may not be longer (in characters) than
772 that number.  If it is a function, the function will be called without
773 any parameters, and if it returns nil, there is no signature in the
774 buffer.  If it is a string, it will be used as a regexp.  If it
775 matches, the text in question is not a signature.")
776
777 (defvar gnus-auto-extend-newsgroup t
778   "*If non-nil, extend newsgroup forward and backward when requested.")
779
780 (defvar gnus-auto-select-first t
781   "*If nil, don't select the first unread article when entering a group.
782 If this variable is `best', select the highest-scored unread article
783 in the group.  If neither nil nor `best', select the first unread
784 article.
785
786 If you want to prevent automatic selection of the first unread article
787 in some newsgroups, set the variable to nil in
788 `gnus-select-group-hook'.")
789
790 (defvar gnus-auto-select-next t
791   "*If non-nil, offer to go to the next group from the end of the previous.
792 If the value is t and the next newsgroup is empty, Gnus will exit
793 summary mode and go back to group mode.  If the value is neither nil
794 nor t, Gnus will select the following unread newsgroup.  In
795 particular, if the value is the symbol `quietly', the next unread
796 newsgroup will be selected without any confirmation, and if it is
797 `almost-quietly', the next group will be selected without any
798 confirmation if you are located on the last article in the group.
799 Finally, if this variable is `slightly-quietly', the `Z n' command
800 will go to the next group without confirmation.")
801
802 (defvar gnus-auto-select-same nil
803   "*If non-nil, select the next article with the same subject.")
804
805 (defvar gnus-summary-check-current nil
806   "*If non-nil, consider the current article when moving.
807 The \"unread\" movement commands will stay on the same line if the
808 current article is unread.")
809
810 (defvar gnus-auto-center-summary t
811   "*If non-nil, always center the current summary buffer.
812 In particular, if `vertical' do only vertical recentering.  If non-nil
813 and non-`vertical', do both horizontal and vertical recentering.")
814
815 (defvar gnus-break-pages t
816   "*If non-nil, do page breaking on articles.
817 The page delimiter is specified by the `gnus-page-delimiter'
818 variable.")
819
820 (defvar gnus-page-delimiter "^\^L"
821   "*Regexp describing what to use as article page delimiters.
822 The default value is \"^\^L\", which is a form linefeed at the
823 beginning of a line.")
824
825 (defvar gnus-use-full-window t
826   "*If non-nil, use the entire Emacs screen.")
827
828 (defvar gnus-window-configuration nil
829   "Obsolete variable.  See `gnus-buffer-configuration'.")
830
831 (defvar gnus-window-min-width 2
832   "*Minimum width of Gnus buffers.")
833
834 (defvar gnus-window-min-height 1
835   "*Minimum height of Gnus buffers.")
836
837 (defvar gnus-buffer-configuration
838   '((group
839      (vertical 1.0
840                (group 1.0 point)
841                (if gnus-carpal '(group-carpal 4))))
842     (summary
843      (vertical 1.0
844                (summary 1.0 point)
845                (if gnus-carpal '(summary-carpal 4))))
846     (article
847      (cond 
848       (gnus-use-picons
849        '(frame 1.0
850                (vertical 1.0
851                          (summary 0.25 point)
852                          (if gnus-carpal '(summary-carpal 4))
853                          (article 1.0))
854                (vertical ((height . 5) (width . 15)
855                           (user-position . t)
856                           (left . -1) (top . 1))
857                          (picons 1.0))))
858       (gnus-use-trees
859        '(vertical 1.0
860                   (summary 0.25 point)
861                   (tree 0.25)
862                   (article 1.0)))
863       (t
864        '(vertical 1.0
865                  (summary 0.25 point)
866                  (if gnus-carpal '(summary-carpal 4))
867                  (if gnus-use-trees '(tree 0.25))
868                  (article 1.0)))))
869     (server
870      (vertical 1.0
871                (server 1.0 point)
872                (if gnus-carpal '(server-carpal 2))))
873     (browse
874      (vertical 1.0
875                (browse 1.0 point)
876                (if gnus-carpal '(browse-carpal 2))))
877     (message
878      (vertical 1.0
879                (message 1.0 point)))
880     (pick
881      (vertical 1.0
882                (article 1.0 point)))
883     (info
884      (vertical 1.0
885                (info 1.0 point)))
886     (summary-faq
887      (vertical 1.0
888                (summary 0.25)
889                (faq 1.0 point)))
890     (edit-group
891      (vertical 1.0
892                (group 0.5)
893                (edit-group 1.0 point)))
894     (edit-server
895      (vertical 1.0
896                (server 0.5)
897                (edit-server 1.0 point)))
898     (edit-score
899      (vertical 1.0
900                (summary 0.25)
901                (edit-score 1.0 point)))
902     (post
903      (vertical 1.0
904                (post 1.0 point)))
905     (reply
906      (vertical 1.0
907                (article-copy 0.5)
908                (message 1.0 point)))
909     (forward
910      (vertical 1.0
911                (message 1.0 point)))
912     (reply-yank
913      (vertical 1.0
914                (message 1.0 point)))
915     (mail-bounce
916      (vertical 1.0
917                (article 0.5)
918                (message 1.0 point)))
919     (draft
920      (vertical 1.0
921                (draft 1.0 point)))
922     (pipe
923      (vertical 1.0
924                (summary 0.25 point)
925                (if gnus-carpal '(summary-carpal 4))
926                ("*Shell Command Output*" 1.0)))
927     (bug
928      (vertical 1.0
929                ("*Gnus Help Bug*" 0.5)
930                ("*Gnus Bug*" 1.0 point)))
931     (compose-bounce
932      (vertical 1.0
933                (article 0.5)
934                (message 1.0 point))))
935   "Window configuration for all possible Gnus buffers.
936 This variable is a list of lists.  Each of these lists has a NAME and
937 a RULE.  The NAMEs are commonsense names like `group', which names a
938 rule used when displaying the group buffer; `summary', which names a
939 rule for what happens when you enter a group and do not display an
940 article buffer; and so on.  See the value of this variable for a
941 complete list of NAMEs.
942
943 Each RULE is a list of vectors.  The first element in this vector is
944 the name of the buffer to be displayed; the second element is the
945 percentage of the screen this buffer is to occupy (a number in the
946 0.0-0.99 range); the optional third element is `point', which should
947 be present to denote which buffer point is to go to after making this
948 buffer configuration.")
949
950 (defvar gnus-window-to-buffer
951   '((group . gnus-group-buffer)
952     (summary . gnus-summary-buffer)
953     (article . gnus-article-buffer)
954     (server . gnus-server-buffer)
955     (browse . "*Gnus Browse Server*")
956     (edit-group . gnus-group-edit-buffer)
957     (edit-server . gnus-server-edit-buffer)
958     (group-carpal . gnus-carpal-group-buffer)
959     (summary-carpal . gnus-carpal-summary-buffer)
960     (server-carpal . gnus-carpal-server-buffer)
961     (browse-carpal . gnus-carpal-browse-buffer)
962     (edit-score . gnus-score-edit-buffer)
963     (message . gnus-message-buffer)
964     (mail . gnus-message-buffer)
965     (post-news . gnus-message-buffer)
966     (faq . gnus-faq-buffer)
967     (picons . "*Picons*")
968     (tree . gnus-tree-buffer)
969     (info . gnus-info-buffer)
970     (article-copy . gnus-article-copy)
971     (draft . gnus-draft-buffer))
972   "Mapping from short symbols to buffer names or buffer variables.")
973
974 (defvar gnus-carpal nil
975   "*If non-nil, display clickable icons.")
976
977 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
978   "*Function called with a group name when new group is detected.
979 A few pre-made functions are supplied: `gnus-subscribe-randomly'
980 inserts new groups at the beginning of the list of groups;
981 `gnus-subscribe-alphabetically' inserts new groups in strict
982 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
983 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
984 for your decision; `gnus-subscribe-killed' kills all new groups;
985 `gnus-subscribe-zombies' will make all new groups into zombies.")
986
987 ;; Suggested by a bug report by Hallvard B Furuseth.
988 ;; <h.b.furuseth@usit.uio.no>.
989 (defvar gnus-subscribe-options-newsgroup-method
990   (function gnus-subscribe-alphabetically)
991   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
992 If, for instance, you want to subscribe to all newsgroups in the
993 \"no\" and \"alt\" hierarchies, you'd put the following in your
994 .newsrc file:
995
996 options -n no.all alt.all
997
998 Gnus will the subscribe all new newsgroups in these hierarchies with
999 the subscription method in this variable.")
1000
1001 (defvar gnus-subscribe-hierarchical-interactive nil
1002   "*If non-nil, Gnus will offer to subscribe hierarchically.
1003 When a new hierarchy appears, Gnus will ask the user:
1004
1005 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
1006
1007 If the user pressed `d', Gnus will descend the hierarchy, `y' will
1008 subscribe to all newsgroups in the hierarchy and `s' will skip this
1009 hierarchy in its entirety.")
1010
1011 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
1012   "*Function used for sorting the group buffer.
1013 This function will be called with group info entries as the arguments
1014 for the groups to be sorted.  Pre-made functions include
1015 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread',
1016 `gnus-group-sort-by-level', `gnus-group-sort-by-score', and
1017 `gnus-group-sort-by-rank'.
1018
1019 This variable can also be a list of sorting functions.  In that case,
1020 the most significant sort function should be the last function in the
1021 list.")
1022
1023 ;; Mark variables suggested by Thomas Michanek
1024 ;; <Thomas.Michanek@telelogic.se>.
1025 (defvar gnus-unread-mark ? 
1026   "*Mark used for unread articles.")
1027 (defvar gnus-ticked-mark ?!
1028   "*Mark used for ticked articles.")
1029 (defvar gnus-dormant-mark ??
1030   "*Mark used for dormant articles.")
1031 (defvar gnus-del-mark ?r
1032   "*Mark used for del'd articles.")
1033 (defvar gnus-read-mark ?R
1034   "*Mark used for read articles.")
1035 (defvar gnus-expirable-mark ?E
1036   "*Mark used for expirable articles.")
1037 (defvar gnus-killed-mark ?K
1038   "*Mark used for killed articles.")
1039 (defvar gnus-souped-mark ?F
1040   "*Mark used for killed articles.")
1041 (defvar gnus-kill-file-mark ?X
1042   "*Mark used for articles killed by kill files.")
1043 (defvar gnus-low-score-mark ?Y
1044   "*Mark used for articles with a low score.")
1045 (defvar gnus-catchup-mark ?C
1046   "*Mark used for articles that are caught up.")
1047 (defvar gnus-replied-mark ?A
1048   "*Mark used for articles that have been replied to.")
1049 (defvar gnus-cached-mark ?*
1050   "*Mark used for articles that are in the cache.")
1051 (defvar gnus-saved-mark ?S
1052   "*Mark used for articles that have been saved to.")
1053 (defvar gnus-process-mark ?#
1054   "*Process mark.")
1055 (defvar gnus-ancient-mark ?O
1056   "*Mark used for ancient articles.")
1057 (defvar gnus-sparse-mark ?Q
1058   "*Mark used for sparsely reffed articles.")
1059 (defvar gnus-canceled-mark ?G
1060   "*Mark used for canceled articles.")
1061 (defvar gnus-score-over-mark ?+
1062   "*Score mark used for articles with high scores.")
1063 (defvar gnus-score-below-mark ?-
1064   "*Score mark used for articles with low scores.")
1065 (defvar gnus-empty-thread-mark ? 
1066   "*There is no thread under the article.")
1067 (defvar gnus-not-empty-thread-mark ?=
1068   "*There is a thread under the article.")
1069
1070 (defvar gnus-view-pseudo-asynchronously nil
1071   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
1072
1073 (defvar gnus-view-pseudos nil
1074   "*If `automatic', pseudo-articles will be viewed automatically.
1075 If `not-confirm', pseudos will be viewed automatically, and the user
1076 will not be asked to confirm the command.")
1077
1078 (defvar gnus-view-pseudos-separately t
1079   "*If non-nil, one pseudo-article will be created for each file to be viewed.
1080 If nil, all files that use the same viewing command will be given as a
1081 list of parameters to that command.")
1082
1083 (defvar gnus-insert-pseudo-articles t
1084   "*If non-nil, insert pseudo-articles when decoding articles.")
1085
1086 (defvar gnus-group-line-format "%M%S%p%P%5y: %(%g%)%l\n"
1087   "*Format of group lines.
1088 It works along the same lines as a normal formatting string,
1089 with some simple extensions.
1090
1091 %M    Only marked articles (character, \"*\" or \" \")
1092 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
1093 %L    Level of subscribedness (integer)
1094 %N    Number of unread articles (integer)
1095 %I    Number of dormant articles (integer)
1096 %i    Number of ticked and dormant (integer)
1097 %T    Number of ticked articles (integer)
1098 %R    Number of read articles (integer)
1099 %t    Total number of articles (integer)
1100 %y    Number of unread, unticked articles (integer)
1101 %G    Group name (string)
1102 %g    Qualified group name (string)
1103 %D    Group description (string)
1104 %s    Select method (string)
1105 %o    Moderated group (char, \"m\")
1106 %p    Process mark (char)
1107 %O    Moderated group (string, \"(m)\" or \"\")
1108 %P    Topic indentation (string)
1109 %l    Whether there are GroupLens predictions for this group (string)
1110 %n    Select from where (string)
1111 %z    A string that look like `<%s:%n>' if a foreign select method is used
1112 %u    User defined specifier.  The next character in the format string should
1113       be a letter.  Gnus will call the function gnus-user-format-function-X,
1114       where X is the letter following %u.  The function will be passed the
1115       current header as argument.  The function should return a string, which
1116       will be inserted into the buffer just like information from any other
1117       group specifier.
1118
1119 Text between %( and %) will be highlighted with `gnus-mouse-face' when
1120 the mouse point move inside the area.  There can only be one such area.
1121
1122 Note that this format specification is not always respected.  For
1123 reasons of efficiency, when listing killed groups, this specification
1124 is ignored altogether.  If the spec is changed considerably, your
1125 output may end up looking strange when listing both alive and killed
1126 groups.
1127
1128 If you use %o or %O, reading the active file will be slower and quite
1129 a bit of extra memory will be used. %D will also worsen performance.
1130 Also note that if you change the format specification to include any
1131 of these specs, you must probably re-start Gnus to see them go into
1132 effect.")
1133
1134 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
1135   "*The format specification of the lines in the summary buffer.
1136
1137 It works along the same lines as a normal formatting string,
1138 with some simple extensions.
1139
1140 %N   Article number, left padded with spaces (string)
1141 %S   Subject (string)
1142 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
1143 %n   Name of the poster (string)
1144 %a   Extracted name of the poster (string)
1145 %A   Extracted address of the poster (string)
1146 %F   Contents of the From: header (string)
1147 %x   Contents of the Xref: header (string)
1148 %D   Date of the article (string)
1149 %d   Date of the article (string) in DD-MMM format
1150 %M   Message-id of the article (string)
1151 %r   References of the article (string)
1152 %c   Number of characters in the article (integer)
1153 %L   Number of lines in the article (integer)
1154 %I   Indentation based on thread level (a string of spaces)
1155 %T   A string with two possible values: 80 spaces if the article
1156      is on thread level two or larger and 0 spaces on level one
1157 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
1158 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
1159 %[   Opening bracket (character, \"[\" or \"<\")
1160 %]   Closing bracket (character, \"]\" or \">\")
1161 %>   Spaces of length thread-level (string)
1162 %<   Spaces of length (- 20 thread-level) (string)
1163 %i   Article score (number)
1164 %z   Article zcore (character)
1165 %t   Number of articles under the current thread (number).
1166 %e   Whether the thread is empty or not (character).
1167 %l   GroupLens score (string).
1168 %u   User defined specifier.  The next character in the format string should
1169      be a letter.  Gnus will call the function gnus-user-format-function-X,
1170      where X is the letter following %u.  The function will be passed the
1171      current header as argument.  The function should return a string, which
1172      will be inserted into the summary just like information from any other
1173      summary specifier.
1174
1175 Text between %( and %) will be highlighted with `gnus-mouse-face'
1176 when the mouse point is placed inside the area.  There can only be one
1177 such area.
1178
1179 The %U (status), %R (replied) and %z (zcore) specs have to be handled
1180 with care.  For reasons of efficiency, Gnus will compute what column
1181 these characters will end up in, and \"hard-code\" that.  This means that
1182 it is illegal to have these specs after a variable-length spec.  Well,
1183 you might not be arrested, but your summary buffer will look strange,
1184 which is bad enough.
1185
1186 The smart choice is to have these specs as for to the left as
1187 possible.
1188
1189 This restriction may disappear in later versions of Gnus.")
1190
1191 (defvar gnus-summary-dummy-line-format
1192   "*  %(:                          :%) %S\n"
1193   "*The format specification for the dummy roots in the summary buffer.
1194 It works along the same lines as a normal formatting string,
1195 with some simple extensions.
1196
1197 %S  The subject")
1198
1199 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
1200   "*The format specification for the summary mode line.
1201 It works along the same lines as a normal formatting string,
1202 with some simple extensions:
1203
1204 %G  Group name
1205 %p  Unprefixed group name
1206 %A  Current article number
1207 %V  Gnus version
1208 %U  Number of unread articles in the group
1209 %e  Number of unselected articles in the group
1210 %Z  A string with unread/unselected article counts
1211 %g  Shortish group name
1212 %S  Subject of the current article
1213 %u  User-defined spec
1214 %s  Current score file name
1215 %d  Number of dormant articles
1216 %r  Number of articles that have been marked as read in this session
1217 %E  Number of articles expunged by the score files")
1218
1219 (defvar gnus-article-mode-line-format "Gnus: %%b %S"
1220   "*The format specification for the article mode line.
1221 See `gnus-summary-mode-line-format' for a closer description.")
1222
1223 (defvar gnus-group-mode-line-format "Gnus: %%b {%M%:%S}"
1224   "*The format specification for the group mode line.
1225 It works along the same lines as a normal formatting string,
1226 with some simple extensions:
1227
1228 %S   The native news server.
1229 %M   The native select method.
1230 %:   \":\" if %S isn't \"\".")
1231
1232 (defvar gnus-valid-select-methods
1233   '(("nntp" post address prompt-address)
1234     ("nnspool" post address)
1235     ("nnvirtual" post-mail virtual prompt-address)
1236     ("nnmbox" mail respool address)
1237     ("nnml" mail respool address)
1238     ("nnmh" mail respool address)
1239     ("nndir" post-mail prompt-address address)
1240     ("nneething" none address prompt-address)
1241     ("nndoc" none address prompt-address)
1242     ("nnbabyl" mail address respool)
1243     ("nnkiboze" post virtual)
1244     ("nnsoup" post-mail address)
1245     ("nndraft" post-mail)
1246     ("nnfolder" mail respool address))
1247   "An alist of valid select methods.
1248 The first element of each list lists should be a string with the name
1249 of the select method.  The other elements may be be the category of
1250 this method (ie. `post', `mail', `none' or whatever) or other
1251 properties that this method has (like being respoolable).
1252 If you implement a new select method, all you should have to change is
1253 this variable.  I think.")
1254
1255 (defvar gnus-updated-mode-lines '(group article summary tree)
1256   "*List of buffers that should update their mode lines.
1257 The list may contain the symbols `group', `article' and `summary'.  If
1258 the corresponding symbol is present, Gnus will keep that mode line
1259 updated with information that may be pertinent.
1260 If this variable is nil, screen refresh may be quicker.")
1261
1262 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1263 (defvar gnus-mode-non-string-length nil
1264   "*Max length of mode-line non-string contents.
1265 If this is nil, Gnus will take space as is needed, leaving the rest
1266 of the modeline intact.")
1267
1268 ;see gnus-cus.el
1269 ;(defvar gnus-mouse-face 'highlight
1270 ;  "*Face used for mouse highlighting in Gnus.
1271 ;No mouse highlights will be done if `gnus-visual' is nil.")
1272
1273 (defvar gnus-summary-mark-below 0
1274   "*Mark all articles with a score below this variable as read.
1275 This variable is local to each summary buffer and usually set by the
1276 score file.")
1277
1278 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
1279   "*List of functions used for sorting articles in the summary buffer.
1280 This variable is only used when not using a threaded display.")
1281
1282 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1283   "*List of functions used for sorting threads in the summary buffer.
1284 By default, threads are sorted by article number.
1285
1286 Each function takes two threads and return non-nil if the first thread
1287 should be sorted before the other.  If you use more than one function,
1288 the primary sort function should be the last.  You should probably
1289 always include `gnus-thread-sort-by-number' in the list of sorting
1290 functions -- preferably first.
1291
1292 Ready-mady functions include `gnus-thread-sort-by-number',
1293 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1294 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1295 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1296
1297 (defvar gnus-thread-score-function '+
1298   "*Function used for calculating the total score of a thread.
1299
1300 The function is called with the scores of the article and each
1301 subthread and should then return the score of the thread.
1302
1303 Some functions you can use are `+', `max', or `min'.")
1304
1305 (defvar gnus-summary-expunge-below nil
1306   "All articles that have a score less than this variable will be expunged.")
1307
1308 (defvar gnus-thread-expunge-below nil
1309   "All threads that have a total score less than this variable will be expunged.
1310 See `gnus-thread-score-function' for en explanation of what a
1311 \"thread score\" is.")
1312
1313 (defvar gnus-auto-subscribed-groups
1314   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1315   "*All new groups that match this regexp will be subscribed automatically.
1316 Note that this variable only deals with new groups.  It has no effect
1317 whatsoever on old groups.")
1318
1319 (defvar gnus-options-subscribe nil
1320   "*All new groups matching this regexp will be subscribed unconditionally.
1321 Note that this variable deals only with new newsgroups.  This variable
1322 does not affect old newsgroups.")
1323
1324 (defvar gnus-options-not-subscribe nil
1325   "*All new groups matching this regexp will be ignored.
1326 Note that this variable deals only with new newsgroups.  This variable
1327 does not affect old (already subscribed) newsgroups.")
1328
1329 (defvar gnus-auto-expirable-newsgroups nil
1330   "*Groups in which to automatically mark read articles as expirable.
1331 If non-nil, this should be a regexp that should match all groups in
1332 which to perform auto-expiry.  This only makes sense for mail groups.")
1333
1334 (defvar gnus-total-expirable-newsgroups nil
1335   "*Groups in which to perform expiry of all read articles.
1336 Use with extreme caution.  All groups that match this regexp will be
1337 expiring - which means that all read articles will be deleted after
1338 (say) one week.  (This only goes for mail groups and the like, of
1339 course.)")
1340
1341 (defvar gnus-group-uncollapsed-levels 1
1342   "Number of group name elements to leave alone when making a short group name.")
1343
1344 (defvar gnus-hidden-properties '(invisible t intangible t)
1345   "Property list to use for hiding text.")
1346
1347 (defvar gnus-modtime-botch nil
1348   "*Non-nil means .newsrc should be deleted prior to save.  
1349 Its use is due to the bogus appearance that .newsrc was modified on
1350 disc.")
1351
1352 ;; Hooks.
1353
1354 (defvar gnus-group-mode-hook nil
1355   "*A hook for Gnus group mode.")
1356
1357 (defvar gnus-summary-mode-hook nil
1358   "*A hook for Gnus summary mode.
1359 This hook is run before any variables are set in the summary buffer.")
1360
1361 (defvar gnus-article-mode-hook nil
1362   "*A hook for Gnus article mode.")
1363
1364 (defvar gnus-summary-prepare-exit-hook nil
1365   "*A hook called when preparing to exit from the summary buffer.
1366 It calls `gnus-summary-expire-articles' by default.")
1367 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1368
1369 (defvar gnus-summary-exit-hook nil
1370   "*A hook called on exit from the summary buffer.")
1371
1372 (defvar gnus-group-catchup-group-hook nil
1373   "*A hook run when catching up a group from the group buffer.")
1374
1375 (defvar gnus-group-update-group-hook nil
1376   "*A hook called when updating group lines.")
1377
1378 (defvar gnus-open-server-hook nil
1379   "*A hook called just before opening connection to the news server.")
1380
1381 (defvar gnus-load-hook nil
1382   "*A hook run while Gnus is loaded.")
1383
1384 (defvar gnus-startup-hook nil
1385   "*A hook called at startup.
1386 This hook is called after Gnus is connected to the NNTP server.")
1387
1388 (defvar gnus-get-new-news-hook nil
1389   "*A hook run just before Gnus checks for new news.")
1390
1391 (defvar gnus-after-getting-new-news-hook nil
1392   "*A hook run after Gnus checks for new news.")
1393
1394 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1395   "*A function that is called to generate the group buffer.
1396 The function is called with three arguments: The first is a number;
1397 all group with a level less or equal to that number should be listed,
1398 if the second is non-nil, empty groups should also be displayed.  If
1399 the third is non-nil, it is a number.  No groups with a level lower
1400 than this number should be displayed.
1401
1402 The only current function implemented is `gnus-group-prepare-flat'.")
1403
1404 (defvar gnus-group-prepare-hook nil
1405   "*A hook called after the group buffer has been generated.
1406 If you want to modify the group buffer, you can use this hook.")
1407
1408 (defvar gnus-summary-prepare-hook nil
1409   "*A hook called after the summary buffer has been generated.
1410 If you want to modify the summary buffer, you can use this hook.")
1411
1412 (defvar gnus-summary-generate-hook nil
1413   "*A hook run just before generating the summary buffer.
1414 This hook is commonly used to customize threading variables and the
1415 like.")
1416
1417 (defvar gnus-article-prepare-hook nil
1418   "*A hook called after an article has been prepared in the article buffer.
1419 If you want to run a special decoding program like nkf, use this hook.")
1420
1421 ;(defvar gnus-article-display-hook nil
1422 ;  "*A hook called after the article is displayed in the article buffer.
1423 ;The hook is designed to change the contents of the article
1424 ;buffer.  Typical functions that this hook may contain are
1425 ;`gnus-article-hide-headers' (hide selected headers),
1426 ;`gnus-article-maybe-highlight' (perform fancy article highlighting),
1427 ;`gnus-article-hide-signature' (hide signature) and
1428 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1429 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1430 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1431 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1432
1433 (defvar gnus-article-x-face-too-ugly nil
1434   "Regexp matching posters whose face shouldn't be shown automatically.")
1435
1436 (defvar gnus-select-group-hook nil
1437   "*A hook called when a newsgroup is selected.
1438
1439 If you'd like to simplify subjects like the
1440 `gnus-summary-next-same-subject' command does, you can use the
1441 following hook:
1442
1443  (setq gnus-select-group-hook
1444       (list
1445         (lambda ()
1446           (mapcar (lambda (header)
1447                      (mail-header-set-subject
1448                       header
1449                       (gnus-simplify-subject
1450                        (mail-header-subject header) 're-only)))
1451                   gnus-newsgroup-headers))))")
1452
1453 (defvar gnus-select-article-hook nil
1454   "*A hook called when an article is selected.")
1455
1456 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1457   "*A hook called to apply kill files to a group.
1458 This hook is intended to apply a kill file to the selected newsgroup.
1459 The function `gnus-apply-kill-file' is called by default.
1460
1461 Since a general kill file is too heavy to use only for a few
1462 newsgroups, I recommend you to use a lighter hook function.  For
1463 example, if you'd like to apply a kill file to articles which contains
1464 a string `rmgroup' in subject in newsgroup `control', you can use the
1465 following hook:
1466
1467  (setq gnus-apply-kill-hook
1468       (list
1469         (lambda ()
1470           (cond ((string-match \"control\" gnus-newsgroup-name)
1471                  (gnus-kill \"Subject\" \"rmgroup\")
1472                  (gnus-expunge \"X\"))))))")
1473
1474 (defvar gnus-visual-mark-article-hook
1475   (list 'gnus-highlight-selected-summary)
1476   "*Hook run after selecting an article in the summary buffer.
1477 It is meant to be used for highlighting the article in some way.  It
1478 is not run if `gnus-visual' is nil.")
1479
1480 (defvar gnus-parse-headers-hook nil
1481   "*A hook called before parsing the headers.")
1482 (add-hook 'gnus-parse-headers-hook 'gnus-decode-rfc1522)
1483
1484 (defvar gnus-exit-group-hook nil
1485   "*A hook called when exiting (not quitting) summary mode.")
1486
1487 (defvar gnus-suspend-gnus-hook nil
1488   "*A hook called when suspending (not exiting) Gnus.")
1489
1490 (defvar gnus-exit-gnus-hook nil
1491   "*A hook called when exiting Gnus.")
1492
1493 (defvar gnus-after-exiting-gnus-hook nil
1494   "*A hook called after exiting Gnus.")
1495
1496 (defvar gnus-save-newsrc-hook nil
1497   "*A hook called before saving any of the newsrc files.")
1498
1499 (defvar gnus-save-quick-newsrc-hook nil
1500   "*A hook called just before saving the quick newsrc file.
1501 Can be used to turn version control on or off.")
1502
1503 (defvar gnus-save-standard-newsrc-hook nil
1504   "*A hook called just before saving the standard newsrc file.
1505 Can be used to turn version control on or off.")
1506
1507 (defvar gnus-summary-update-hook
1508   (list 'gnus-summary-highlight-line)
1509   "*A hook called when a summary line is changed.
1510 The hook will not be called if `gnus-visual' is nil.
1511
1512 The default function `gnus-summary-highlight-line' will
1513 highlight the line according to the `gnus-summary-highlight'
1514 variable.")
1515
1516 (defvar gnus-group-update-hook '(gnus-group-highlight-line)
1517   "*A hook called when a group line is changed.
1518 The hook will not be called if `gnus-visual' is nil.
1519
1520 The default function `gnus-group-highlight-line' will
1521 highlight the line according to the `gnus-group-highlight'
1522 variable.")
1523
1524 (defvar gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1525   "*A hook called when an article is selected for the first time.
1526 The hook is intended to mark an article as read (or unread)
1527 automatically when it is selected.")
1528
1529 (defvar gnus-group-change-level-function nil
1530   "Function run when a group level is changed.
1531 It is called with three parameters -- GROUP, LEVEL and OLDLEVEL.")
1532
1533 ;; Remove any hilit infestation.
1534 (add-hook 'gnus-startup-hook
1535           (lambda ()
1536             (remove-hook 'gnus-summary-prepare-hook
1537                          'hilit-rehighlight-buffer-quietly)
1538             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1539             (setq gnus-mark-article-hook
1540                   '(gnus-summary-mark-read-and-unread-as-read))
1541             (remove-hook 'gnus-article-prepare-hook
1542                          'hilit-rehighlight-buffer-quietly)))
1543
1544 \f
1545 ;; Internal variables
1546
1547 (defvar gnus-tree-buffer "*Tree*"
1548   "Buffer where Gnus thread trees are displayed.")
1549
1550 ;; Dummy variable.
1551 (defvar gnus-use-generic-from nil)
1552
1553 (defvar gnus-thread-indent-array nil)
1554 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1555
1556 (defvar gnus-newsrc-file-version nil)
1557
1558 (defvar gnus-method-history nil)
1559 ;; Variable holding the user answers to all method prompts.
1560
1561 (defvar gnus-group-history nil)
1562 ;; Variable holding the user answers to all group prompts.
1563
1564 (defvar gnus-server-alist nil
1565   "List of available servers.")
1566
1567 (defvar gnus-group-indentation-function nil)
1568
1569 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1570
1571 (defvar gnus-goto-missing-group-function nil)
1572
1573 (defvar gnus-override-subscribe-method nil)
1574
1575 (defvar gnus-group-goto-next-group-function nil
1576   "Function to override finding the next group after listing groups.")
1577
1578 (defconst gnus-article-mark-lists
1579   '((marked . tick) (replied . reply)
1580     (expirable . expire) (killed . killed)
1581     (bookmarks . bookmark) (dormant . dormant)
1582     (scored . score) (saved . save)
1583     (cached . cache)
1584     ))
1585
1586 ;; Avoid highlighting in kill files.
1587 (defvar gnus-summary-inhibit-highlight nil)
1588 (defvar gnus-newsgroup-selected-overlay nil)
1589
1590 (defvar gnus-inhibit-hiding nil)
1591 (defvar gnus-group-indentation "")
1592 (defvar gnus-inhibit-limiting nil)
1593 (defvar gnus-created-frames nil)
1594
1595 (defvar gnus-article-mode-map nil)
1596 (defvar gnus-dribble-buffer nil)
1597 (defvar gnus-headers-retrieved-by nil)
1598 (defvar gnus-article-reply nil)
1599 (defvar gnus-override-method nil)
1600 (defvar gnus-article-check-size nil)
1601
1602 (defvar gnus-current-score-file nil)
1603 (defvar gnus-newsgroup-adaptive-score-file nil)
1604 (defvar gnus-scores-exclude-files nil)
1605
1606 (defvar gnus-opened-servers nil)
1607
1608 (defvar gnus-current-move-group nil)
1609 (defvar gnus-current-copy-group nil)
1610 (defvar gnus-current-crosspost-group nil)
1611
1612 (defvar gnus-newsgroup-dependencies nil)
1613 (defvar gnus-newsgroup-async nil)
1614 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1615
1616 (defvar gnus-newsgroup-adaptive nil)
1617
1618 (defvar gnus-summary-display-table nil)
1619 (defvar gnus-summary-display-article-function nil)
1620
1621 (defvar gnus-summary-highlight-line-function nil
1622   "Function called after highlighting a summary line.")
1623
1624 (defvar gnus-group-line-format-alist
1625   `((?M gnus-tmp-marked-mark ?c)
1626     (?S gnus-tmp-subscribed ?c)
1627     (?L gnus-tmp-level ?d)
1628     (?N (cond ((eq number t) "*" )
1629               ((numberp number) 
1630                (int-to-string
1631                 (+ number
1632                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1633                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1634               (t number)) ?s)
1635     (?R gnus-tmp-number-of-read ?s)
1636     (?t gnus-tmp-number-total ?d)
1637     (?y gnus-tmp-number-of-unread ?s)
1638     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1639     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1640     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1641            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1642     (?g gnus-tmp-group ?s)
1643     (?G gnus-tmp-qualified-group ?s)
1644     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1645     (?D gnus-tmp-newsgroup-description ?s)
1646     (?o gnus-tmp-moderated ?c)
1647     (?O gnus-tmp-moderated-string ?s)
1648     (?p gnus-tmp-process-marked ?c)
1649     (?s gnus-tmp-news-server ?s)
1650     (?n gnus-tmp-news-method ?s)
1651     (?P gnus-group-indentation ?s)
1652     (?l gnus-tmp-grouplens ?s)
1653     (?z gnus-tmp-news-method-string ?s)
1654     (?u gnus-tmp-user-defined ?s)))
1655
1656 (defvar gnus-summary-line-format-alist
1657   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1658     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1659     (?s gnus-tmp-subject-or-nil ?s)
1660     (?n gnus-tmp-name ?s)
1661     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1662         ?s)
1663     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1664             gnus-tmp-from) ?s)
1665     (?F gnus-tmp-from ?s)
1666     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1667     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1668     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1669     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1670     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1671     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1672     (?L gnus-tmp-lines ?d)
1673     (?I gnus-tmp-indentation ?s)
1674     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1675     (?R gnus-tmp-replied ?c)
1676     (?\[ gnus-tmp-opening-bracket ?c)
1677     (?\] gnus-tmp-closing-bracket ?c)
1678     (?\> (make-string gnus-tmp-level ? ) ?s)
1679     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1680     (?i gnus-tmp-score ?d)
1681     (?z gnus-tmp-score-char ?c)
1682     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1683     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1684     (?U gnus-tmp-unread ?c)
1685     (?t (gnus-summary-number-of-articles-in-thread
1686          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1687         ?d)
1688     (?e (gnus-summary-number-of-articles-in-thread
1689          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1690         ?c)
1691     (?u gnus-tmp-user-defined ?s))
1692   "An alist of format specifications that can appear in summary lines,
1693 and what variables they correspond with, along with the type of the
1694 variable (string, integer, character, etc).")
1695
1696 (defvar gnus-summary-dummy-line-format-alist
1697   `((?S gnus-tmp-subject ?s)
1698     (?N gnus-tmp-number ?d)
1699     (?u gnus-tmp-user-defined ?s)))
1700
1701 (defvar gnus-summary-mode-line-format-alist
1702   `((?G gnus-tmp-group-name ?s)
1703     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1704     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1705     (?A gnus-tmp-article-number ?d)
1706     (?Z gnus-tmp-unread-and-unselected ?s)
1707     (?V gnus-version ?s)
1708     (?U gnus-tmp-unread-and-unticked ?d)
1709     (?S gnus-tmp-subject ?s)
1710     (?e gnus-tmp-unselected ?d)
1711     (?u gnus-tmp-user-defined ?s)
1712     (?d (length gnus-newsgroup-dormant) ?d)
1713     (?t (length gnus-newsgroup-marked) ?d)
1714     (?r (length gnus-newsgroup-reads) ?d)
1715     (?E gnus-newsgroup-expunged-tally ?d)
1716     (?s (gnus-current-score-file-nondirectory) ?s)))
1717
1718 (defvar gnus-article-mode-line-format-alist
1719   gnus-summary-mode-line-format-alist)
1720
1721 (defvar gnus-group-mode-line-format-alist
1722   `((?S gnus-tmp-news-server ?s)
1723     (?M gnus-tmp-news-method ?s)
1724     (?u gnus-tmp-user-defined ?s)
1725     (?: gnus-tmp-colon ?s)))
1726
1727 (defvar gnus-have-read-active-file nil)
1728
1729 (defconst gnus-maintainer
1730   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1731   "The mail address of the Gnus maintainers.")
1732
1733 (defconst gnus-version-number "5.2.24"
1734   "Version number for this version of Gnus.")
1735
1736 (defconst gnus-version (format "Gnus v%s" gnus-version-number)
1737   "Version string for this version of Gnus.")
1738
1739 (defvar gnus-info-nodes
1740   '((gnus-group-mode "(gnus)The Group Buffer")
1741     (gnus-summary-mode "(gnus)The Summary Buffer")
1742     (gnus-article-mode "(gnus)The Article Buffer")
1743     (gnus-server-mode "(gnus)The Server Buffer")
1744     (gnus-browse-mode "(gnus)Browse Foreign Server")
1745     (gnus-tree-mode "(gnus)Tree Display")
1746     )
1747   "Alist of major modes and related Info nodes.")
1748
1749 (defvar gnus-group-buffer "*Group*")
1750 (defvar gnus-summary-buffer "*Summary*")
1751 (defvar gnus-article-buffer "*Article*")
1752 (defvar gnus-server-buffer "*Server*")
1753
1754 (defvar gnus-work-buffer " *gnus work*")
1755
1756 (defvar gnus-original-article-buffer " *Original Article*")
1757 (defvar gnus-original-article nil)
1758
1759 (defvar gnus-buffer-list nil
1760   "Gnus buffers that should be killed on exit.")
1761
1762 (defvar gnus-slave nil
1763   "Whether this Gnus is a slave or not.")
1764
1765 (defvar gnus-variable-list
1766   '(gnus-newsrc-options gnus-newsrc-options-n
1767     gnus-newsrc-last-checked-date
1768     gnus-newsrc-alist gnus-server-alist
1769     gnus-killed-list gnus-zombie-list
1770     gnus-topic-topology gnus-topic-alist
1771     gnus-format-specs)
1772   "Gnus variables saved in the quick startup file.")
1773
1774 (defvar gnus-newsrc-options nil
1775   "Options line in the .newsrc file.")
1776
1777 (defvar gnus-newsrc-options-n nil
1778   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1779
1780 (defvar gnus-newsrc-last-checked-date nil
1781   "Date Gnus last asked server for new newsgroups.")
1782
1783 (defvar gnus-topic-topology nil
1784   "The complete topic hierarchy.")
1785
1786 (defvar gnus-topic-alist nil
1787   "The complete topic-group alist.")
1788
1789 (defvar gnus-newsrc-alist nil
1790   "Assoc list of read articles.
1791 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1792
1793 (defvar gnus-newsrc-hashtb nil
1794   "Hashtable of gnus-newsrc-alist.")
1795
1796 (defvar gnus-killed-list nil
1797   "List of killed newsgroups.")
1798
1799 (defvar gnus-killed-hashtb nil
1800   "Hash table equivalent of gnus-killed-list.")
1801
1802 (defvar gnus-zombie-list nil
1803   "List of almost dead newsgroups.")
1804
1805 (defvar gnus-description-hashtb nil
1806   "Descriptions of newsgroups.")
1807
1808 (defvar gnus-list-of-killed-groups nil
1809   "List of newsgroups that have recently been killed by the user.")
1810
1811 (defvar gnus-active-hashtb nil
1812   "Hashtable of active articles.")
1813
1814 (defvar gnus-moderated-list nil
1815   "List of moderated newsgroups.")
1816
1817 (defvar gnus-group-marked nil)
1818
1819 (defvar gnus-current-startup-file nil
1820   "Startup file for the current host.")
1821
1822 (defvar gnus-last-search-regexp nil
1823   "Default regexp for article search command.")
1824
1825 (defvar gnus-last-shell-command nil
1826   "Default shell command on article.")
1827
1828 (defvar gnus-current-select-method nil
1829   "The current method for selecting a newsgroup.")
1830
1831 (defvar gnus-group-list-mode nil)
1832
1833 (defvar gnus-article-internal-prepare-hook nil)
1834
1835 (defvar gnus-newsgroup-name nil)
1836 (defvar gnus-newsgroup-begin nil)
1837 (defvar gnus-newsgroup-end nil)
1838 (defvar gnus-newsgroup-last-rmail nil)
1839 (defvar gnus-newsgroup-last-mail nil)
1840 (defvar gnus-newsgroup-last-folder nil)
1841 (defvar gnus-newsgroup-last-file nil)
1842 (defvar gnus-newsgroup-auto-expire nil)
1843 (defvar gnus-newsgroup-active nil)
1844
1845 (defvar gnus-newsgroup-data nil)
1846 (defvar gnus-newsgroup-data-reverse nil)
1847 (defvar gnus-newsgroup-limit nil)
1848 (defvar gnus-newsgroup-limits nil)
1849
1850 (defvar gnus-newsgroup-unreads nil
1851   "List of unread articles in the current newsgroup.")
1852
1853 (defvar gnus-newsgroup-unselected nil
1854   "List of unselected unread articles in the current newsgroup.")
1855
1856 (defvar gnus-newsgroup-reads nil
1857   "Alist of read articles and article marks in the current newsgroup.")
1858
1859 (defvar gnus-newsgroup-expunged-tally nil)
1860
1861 (defvar gnus-newsgroup-marked nil
1862   "List of ticked articles in the current newsgroup (a subset of unread art).")
1863
1864 (defvar gnus-newsgroup-killed nil
1865   "List of ranges of articles that have been through the scoring process.")
1866
1867 (defvar gnus-newsgroup-cached nil
1868   "List of articles that come from the article cache.")
1869
1870 (defvar gnus-newsgroup-saved nil
1871   "List of articles that have been saved.")
1872
1873 (defvar gnus-newsgroup-kill-headers nil)
1874
1875 (defvar gnus-newsgroup-replied nil
1876   "List of articles that have been replied to in the current newsgroup.")
1877
1878 (defvar gnus-newsgroup-expirable nil
1879   "List of articles in the current newsgroup that can be expired.")
1880
1881 (defvar gnus-newsgroup-processable nil
1882   "List of articles in the current newsgroup that can be processed.")
1883
1884 (defvar gnus-newsgroup-bookmarks nil
1885   "List of articles in the current newsgroup that have bookmarks.")
1886
1887 (defvar gnus-newsgroup-dormant nil
1888   "List of dormant articles in the current newsgroup.")
1889
1890 (defvar gnus-newsgroup-scored nil
1891   "List of scored articles in the current newsgroup.")
1892
1893 (defvar gnus-newsgroup-headers nil
1894   "List of article headers in the current newsgroup.")
1895
1896 (defvar gnus-newsgroup-threads nil)
1897
1898 (defvar gnus-newsgroup-prepared nil
1899   "Whether the current group has been prepared properly.")
1900
1901 (defvar gnus-newsgroup-ancient nil
1902   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1903
1904 (defvar gnus-newsgroup-sparse nil)
1905
1906 (defvar gnus-current-article nil)
1907 (defvar gnus-article-current nil)
1908 (defvar gnus-current-headers nil)
1909 (defvar gnus-have-all-headers nil)
1910 (defvar gnus-last-article nil)
1911 (defvar gnus-newsgroup-history nil)
1912 (defvar gnus-current-kill-article nil)
1913
1914 ;; Save window configuration.
1915 (defvar gnus-prev-winconf nil)
1916
1917 (defvar gnus-summary-mark-positions nil)
1918 (defvar gnus-group-mark-positions nil)
1919
1920 (defvar gnus-reffed-article-number nil)
1921
1922 ;;; Let the byte-compiler know that we know about this variable.
1923 (defvar rmail-default-rmail-file)
1924
1925 (defvar gnus-cache-removable-articles nil)
1926
1927 (defvar gnus-dead-summary nil)
1928
1929 (defconst gnus-summary-local-variables
1930   '(gnus-newsgroup-name
1931     gnus-newsgroup-begin gnus-newsgroup-end
1932     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1933     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1934     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1935     gnus-newsgroup-unselected gnus-newsgroup-marked
1936     gnus-newsgroup-reads gnus-newsgroup-saved
1937     gnus-newsgroup-replied gnus-newsgroup-expirable
1938     gnus-newsgroup-processable gnus-newsgroup-killed
1939     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1940     gnus-newsgroup-headers gnus-newsgroup-threads
1941     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1942     gnus-current-article gnus-current-headers gnus-have-all-headers
1943     gnus-last-article gnus-article-internal-prepare-hook
1944     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1945     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1946     gnus-newsgroup-async gnus-thread-expunge-below
1947     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1948     (gnus-summary-mark-below . global)
1949     gnus-newsgroup-active gnus-scores-exclude-files
1950     gnus-newsgroup-history gnus-newsgroup-ancient
1951     gnus-newsgroup-sparse
1952     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1953     gnus-newsgroup-adaptive-score-file
1954     (gnus-newsgroup-expunged-tally . 0)
1955     gnus-cache-removable-articles gnus-newsgroup-cached
1956     gnus-newsgroup-data gnus-newsgroup-data-reverse
1957     gnus-newsgroup-limit gnus-newsgroup-limits)
1958   "Variables that are buffer-local to the summary buffers.")
1959
1960 (defconst gnus-bug-message
1961   "Sending a bug report to the Gnus Towers.
1962 ========================================
1963
1964 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1965 be sent to the Gnus Bug Exterminators.
1966
1967 At the bottom of the buffer you'll see lots of variable settings.
1968 Please do not delete those.  They will tell the Bug People what your
1969 environment is, so that it will be easier to locate the bugs.
1970
1971 If you have found a bug that makes Emacs go \"beep\", set
1972 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1973 and include the backtrace in your bug report.
1974
1975 Please describe the bug in annoying, painstaking detail.
1976
1977 Thank you for your help in stamping out bugs.
1978 ")
1979
1980 ;;; End of variables.
1981
1982 ;; Define some autoload functions Gnus might use.
1983 (eval-and-compile
1984
1985   ;; This little mapcar goes through the list below and marks the
1986   ;; symbols in question as autoloaded functions.
1987   (mapcar
1988    (lambda (package)
1989      (let ((interactive (nth 1 (memq ':interactive package))))
1990        (mapcar
1991         (lambda (function)
1992           (let (keymap)
1993             (when (consp function)
1994               (setq keymap (car (memq 'keymap function)))
1995               (setq function (car function)))
1996             (autoload function (car package) nil interactive keymap)))
1997         (if (eq (nth 1 package) ':interactive)
1998             (cdddr package)
1999           (cdr package)))))
2000    '(("metamail" metamail-buffer)
2001      ("info" Info-goto-node)
2002      ("hexl" hexl-hex-string-to-integer)
2003      ("pp" pp pp-to-string pp-eval-expression)
2004      ("mail-extr" mail-extract-address-components)
2005      ("nnmail" nnmail-split-fancy nnmail-article-group)
2006      ("nnvirtual" nnvirtual-catchup-group)
2007      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
2008       timezone-make-sortable-date timezone-make-time-string)
2009      ("rmailout" rmail-output)
2010      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
2011       rmail-show-message)
2012      ("gnus-soup" :interactive t
2013       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
2014       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
2015      ("nnsoup" nnsoup-pack-replies)
2016      ("gnus-scomo" :interactive t gnus-score-mode)
2017      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
2018       gnus-Folder-save-name gnus-folder-save-name)
2019      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
2020      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
2021       gnus-server-make-menu-bar gnus-article-make-menu-bar
2022       gnus-browse-make-menu-bar gnus-highlight-selected-summary
2023       gnus-summary-highlight-line gnus-carpal-setup-buffer
2024       gnus-group-highlight-line
2025       gnus-article-add-button gnus-insert-next-page-button
2026       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
2027      ("gnus-vis" :interactive t
2028       gnus-article-push-button gnus-article-press-button
2029       gnus-article-highlight gnus-article-highlight-some
2030       gnus-article-highlight-headers gnus-article-highlight-signature
2031       gnus-article-add-buttons gnus-article-add-buttons-to-head
2032       gnus-article-next-button gnus-article-prev-button)
2033      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
2034       gnus-demon-add-disconnection gnus-demon-add-handler
2035       gnus-demon-remove-handler)
2036      ("gnus-demon" :interactive t
2037       gnus-demon-init gnus-demon-cancel)
2038      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
2039       gnus-tree-open gnus-tree-close)
2040      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
2041       gnus-nocem-unwanted-article-p)
2042      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2043      ("gnus-srvr" gnus-browse-foreign-server)
2044      ("gnus-cite" :interactive t
2045       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2046       gnus-article-hide-citation gnus-article-fill-cited-article
2047       gnus-article-hide-citation-in-followups)
2048      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2049       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2050       gnus-execute gnus-expunge)
2051      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2052       gnus-cache-possibly-remove-articles gnus-cache-request-article
2053       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2054       gnus-cache-enter-remove-article gnus-cached-article-p
2055       gnus-cache-open gnus-cache-close gnus-cache-update-article)
2056      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2057       gnus-cache-remove-article)
2058      ("gnus-score" :interactive t
2059       gnus-summary-increase-score gnus-summary-lower-score
2060       gnus-score-flush-cache gnus-score-close
2061       gnus-score-raise-same-subject-and-select
2062       gnus-score-raise-same-subject gnus-score-default
2063       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2064       gnus-score-lower-same-subject gnus-score-lower-thread
2065       gnus-possibly-score-headers gnus-summary-raise-score 
2066       gnus-summary-set-score gnus-summary-current-score)
2067      ("gnus-score"
2068       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2069       gnus-current-score-file-nondirectory gnus-score-adaptive
2070       gnus-score-find-trace gnus-score-file-name)
2071      ("gnus-edit" :interactive t gnus-score-customize)
2072      ("gnus-topic" :interactive t gnus-topic-mode)
2073      ("gnus-topic" gnus-topic-remove-group)
2074      ("gnus-salt" :interactive t gnus-pick-mode gnus-binary-mode)
2075      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2076      ("gnus-uu" :interactive t
2077       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2078       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2079       gnus-uu-mark-by-regexp gnus-uu-mark-all
2080       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2081       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2082       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2083       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2084       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2085       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2086       gnus-uu-decode-binhex-view)
2087      ("gnus-msg" (gnus-summary-send-map keymap)
2088       gnus-mail-yank-original gnus-mail-send-and-exit
2089       gnus-article-mail gnus-new-mail gnus-mail-reply)
2090      ("gnus-msg" :interactive t
2091       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2092       gnus-summary-followup gnus-summary-followup-with-original
2093       gnus-summary-cancel-article gnus-summary-supersede-article
2094       gnus-post-news gnus-inews-news 
2095       gnus-summary-reply gnus-summary-reply-with-original
2096       gnus-summary-mail-forward gnus-summary-mail-other-window
2097       gnus-bug)
2098      ("gnus-picon" :interactive t gnus-article-display-picons
2099       gnus-group-display-picons gnus-picons-article-display-x-face)
2100      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2101       gnus-grouplens-mode)
2102      ("smiley" :interactive t gnus-smiley-display)
2103      ("gnus-vm" gnus-vm-mail-setup)
2104      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2105       gnus-summary-save-article-vm))))
2106
2107 \f
2108
2109 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2110 ;; If you want the cursor to go somewhere else, set these two
2111 ;; functions in some startup hook to whatever you want.
2112 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2113 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2114
2115 ;;; Various macros and substs.
2116
2117 (defun gnus-header-from (header)
2118   (mail-header-from header))
2119
2120 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2121   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2122   (let ((tempvar (make-symbol "GnusStartBufferWindow"))
2123         (w (make-symbol "w"))
2124         (buf (make-symbol "buf")))
2125     `(let* ((,tempvar (selected-window))
2126             (,buf ,buffer)
2127             (,w (get-buffer-window ,buf 'visible)))
2128        (unwind-protect
2129            (progn
2130              (if ,w
2131                  (select-window ,w)
2132                (pop-to-buffer ,buf))
2133              ,@forms)
2134          (select-window ,tempvar)))))
2135
2136 (put 'gnus-eval-in-buffer-window 'lisp-indent-function 1)
2137 (put 'gnus-eval-in-buffer-window 'lisp-indent-hook 1)
2138 (put 'gnus-eval-in-buffer-window 'edebug-form-spec '(form body))
2139
2140 (defmacro gnus-gethash (string hashtable)
2141   "Get hash value of STRING in HASHTABLE."
2142   `(symbol-value (intern-soft ,string ,hashtable)))
2143
2144 (defmacro gnus-sethash (string value hashtable)
2145   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2146   `(set (intern ,string ,hashtable) ,value))
2147
2148 (defmacro gnus-intern-safe (string hashtable)
2149   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2150   `(let ((symbol (intern ,string ,hashtable)))
2151      (or (boundp symbol)
2152          (set symbol nil))
2153      symbol))
2154
2155 (defmacro gnus-group-unread (group)
2156   "Get the currently computed number of unread articles in GROUP."
2157   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2158
2159 (defmacro gnus-group-entry (group)
2160   "Get the newsrc entry for GROUP."
2161   `(gnus-gethash ,group gnus-newsrc-hashtb))
2162
2163 (defmacro gnus-active (group)
2164   "Get active info on GROUP."
2165   `(gnus-gethash ,group gnus-active-hashtb))
2166
2167 (defmacro gnus-set-active (group active)
2168   "Set GROUP's active info."
2169   `(gnus-sethash ,group ,active gnus-active-hashtb))
2170
2171 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2172 ;;   function `substring' might cut on a middle of multi-octet
2173 ;;   character.
2174 (defun gnus-truncate-string (str width)
2175   (substring str 0 width))
2176
2177 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2178 ;; to limit the length of a string.  This function is necessary since
2179 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2180 (defsubst gnus-limit-string (str width)
2181   (if (> (length str) width)
2182       (substring str 0 width)
2183     str))
2184
2185 (defsubst gnus-simplify-subject-re (subject)
2186   "Remove \"Re:\" from subject lines."
2187   (if (string-match "^[Rr][Ee]: *" subject)
2188       (substring subject (match-end 0))
2189     subject))
2190
2191 (defsubst gnus-functionp (form)
2192   "Return non-nil if FORM is funcallable."
2193   (or (and (symbolp form) (fboundp form))
2194       (and (listp form) (eq (car form) 'lambda))))
2195
2196 (defsubst gnus-goto-char (point)
2197   (and point (goto-char point)))
2198
2199 (defmacro gnus-buffer-exists-p (buffer)
2200   `(let ((buffer ,buffer))
2201      (and buffer
2202           (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
2203                    buffer))))
2204
2205 (defmacro gnus-kill-buffer (buffer)
2206   `(let ((buf ,buffer))
2207      (if (gnus-buffer-exists-p buf)
2208          (kill-buffer buf))))
2209
2210 (defsubst gnus-point-at-bol ()
2211   "Return point at the beginning of the line."
2212   (let ((p (point)))
2213     (beginning-of-line)
2214     (prog1
2215         (point)
2216       (goto-char p))))
2217
2218 (defsubst gnus-point-at-eol ()
2219   "Return point at the end of the line."
2220   (let ((p (point)))
2221     (end-of-line)
2222     (prog1
2223         (point)
2224       (goto-char p))))
2225
2226 (defun gnus-alive-p ()
2227   "Say whether Gnus is running or not."
2228   (and gnus-group-buffer
2229        (get-buffer gnus-group-buffer)))
2230
2231 (defun gnus-delete-first (elt list)
2232   "Delete by side effect the first occurrence of ELT as a member of LIST."
2233   (if (equal (car list) elt)
2234       (cdr list)
2235     (let ((total list))
2236       (while (and (cdr list)
2237                   (not (equal (cadr list) elt)))
2238         (setq list (cdr list)))
2239       (when (cdr list)
2240         (setcdr list (cddr list)))
2241       total)))
2242
2243 ;; Delete the current line (and the next N lines.);
2244 (defmacro gnus-delete-line (&optional n)
2245   `(delete-region (progn (beginning-of-line) (point))
2246                   (progn (forward-line ,(or n 1)) (point))))
2247
2248 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2249 (defvar gnus-init-inhibit nil)
2250 (defun gnus-read-init-file (&optional inhibit-next)
2251   (if gnus-init-inhibit
2252       (setq gnus-init-inhibit nil)
2253     (setq gnus-init-inhibit inhibit-next)
2254     (and gnus-init-file
2255          (or (and (file-exists-p gnus-init-file)
2256                   ;; Don't try to load a directory.
2257                   (not (file-directory-p gnus-init-file)))
2258              (file-exists-p (concat gnus-init-file ".el"))
2259              (file-exists-p (concat gnus-init-file ".elc")))
2260          (condition-case var
2261              (load gnus-init-file nil t)
2262            (error
2263             (error "Error in %s: %s" gnus-init-file var))))))
2264
2265 ;; Info access macros.
2266
2267 (defmacro gnus-info-group (info)
2268   `(nth 0 ,info))
2269 (defmacro gnus-info-rank (info)
2270   `(nth 1 ,info))
2271 (defmacro gnus-info-read (info)
2272   `(nth 2 ,info))
2273 (defmacro gnus-info-marks (info)
2274   `(nth 3 ,info))
2275 (defmacro gnus-info-method (info)
2276   `(nth 4 ,info))
2277 (defmacro gnus-info-params (info)
2278   `(nth 5 ,info))
2279
2280 (defmacro gnus-info-level (info)
2281   `(let ((rank (gnus-info-rank ,info)))
2282      (if (consp rank)
2283          (car rank)
2284        rank)))
2285 (defmacro gnus-info-score (info)
2286   `(let ((rank (gnus-info-rank ,info)))
2287      (or (and (consp rank) (cdr rank)) 0)))
2288
2289 (defmacro gnus-info-set-group (info group)
2290   `(setcar ,info ,group))
2291 (defmacro gnus-info-set-rank (info rank)
2292   `(setcar (nthcdr 1 ,info) ,rank))
2293 (defmacro gnus-info-set-read (info read)
2294   `(setcar (nthcdr 2 ,info) ,read))
2295 (defmacro gnus-info-set-marks (info marks)
2296   `(setcar (nthcdr 3 ,info) ,marks))
2297 (defmacro gnus-info-set-method (info method)
2298   `(setcar (nthcdr 4 ,info) ,method))
2299 (defmacro gnus-info-set-params (info params)
2300   `(setcar (nthcdr 5 ,info) ,params))
2301
2302 (defmacro gnus-info-set-level (info level)
2303   `(let ((rank (cdr ,info)))
2304      (if (consp (car rank))
2305          (setcar (car rank) ,level)
2306        (setcar rank ,level))))
2307 (defmacro gnus-info-set-score (info score)
2308   `(let ((rank (cdr ,info)))
2309      (if (consp (car rank))
2310          (setcdr (car rank) ,score)
2311        (setcar rank (cons (car rank) ,score)))))
2312
2313 (defmacro gnus-get-info (group)
2314   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2315
2316 (defun gnus-byte-code (func)
2317   "Return a form that can be `eval'ed based on FUNC."
2318   (let ((fval (symbol-function func)))
2319     (if (byte-code-function-p fval)
2320         (let ((flist (append fval nil)))
2321           (setcar flist 'byte-code)
2322           flist)
2323       (cons 'progn (cddr fval)))))
2324
2325 ;; Find out whether the gnus-visual TYPE is wanted.
2326 (defun gnus-visual-p (&optional type class)
2327   (and gnus-visual                      ; Has to be non-nil, at least.
2328        (if (not type)                   ; We don't care about type.
2329            gnus-visual
2330          (if (listp gnus-visual)        ; It's a list, so we check it.
2331              (or (memq type gnus-visual)
2332                  (memq class gnus-visual))
2333            t))))
2334
2335 ;;; Load the compatability functions.
2336
2337 (require 'gnus-cus)
2338 (require 'gnus-ems)
2339
2340 \f
2341 ;;;
2342 ;;; Shutdown
2343 ;;;
2344
2345 (defvar gnus-shutdown-alist nil)
2346
2347 (defun gnus-add-shutdown (function &rest symbols)
2348   "Run FUNCTION whenever one of SYMBOLS is shut down."
2349   (push (cons function symbols) gnus-shutdown-alist))
2350
2351 (defun gnus-shutdown (symbol)
2352   "Shut down everything that waits for SYMBOL."
2353   (let ((alist gnus-shutdown-alist)
2354         entry)
2355     (while (setq entry (pop alist))
2356       (when (memq symbol (cdr entry))
2357         (funcall (car entry))))))
2358
2359 \f
2360
2361 ;; Format specs.  The chunks below are the machine-generated forms
2362 ;; that are to be evaled as the result of the default format strings.
2363 ;; We write them in here to get them byte-compiled.  That way the
2364 ;; default actions will be quite fast, while still retaining the full
2365 ;; flexibility of the user-defined format specs.
2366
2367 ;; First we have lots of dummy defvars to let the compiler know these
2368 ;; are really dynamic variables.
2369
2370 (defvar gnus-tmp-unread)
2371 (defvar gnus-tmp-replied)
2372 (defvar gnus-tmp-score-char)
2373 (defvar gnus-tmp-indentation)
2374 (defvar gnus-tmp-opening-bracket)
2375 (defvar gnus-tmp-lines)
2376 (defvar gnus-tmp-name)
2377 (defvar gnus-tmp-closing-bracket)
2378 (defvar gnus-tmp-subject-or-nil)
2379 (defvar gnus-tmp-subject)
2380 (defvar gnus-tmp-marked)
2381 (defvar gnus-tmp-marked-mark)
2382 (defvar gnus-tmp-subscribed)
2383 (defvar gnus-tmp-process-marked)
2384 (defvar gnus-tmp-number-of-unread)
2385 (defvar gnus-tmp-group-name)
2386 (defvar gnus-tmp-group)
2387 (defvar gnus-tmp-article-number)
2388 (defvar gnus-tmp-unread-and-unselected)
2389 (defvar gnus-tmp-news-method)
2390 (defvar gnus-tmp-news-server)
2391 (defvar gnus-tmp-article-number)
2392 (defvar gnus-mouse-face)
2393 (defvar gnus-mouse-face-prop)
2394
2395 (defun gnus-summary-line-format-spec ()
2396   (insert gnus-tmp-unread gnus-tmp-replied
2397           gnus-tmp-score-char gnus-tmp-indentation)
2398   (gnus-put-text-property
2399    (point)
2400    (progn
2401      (insert
2402       gnus-tmp-opening-bracket
2403       (format "%4d: %-20s"
2404               gnus-tmp-lines
2405               (if (> (length gnus-tmp-name) 20)
2406                   (substring gnus-tmp-name 0 20)
2407                 gnus-tmp-name))
2408       gnus-tmp-closing-bracket)
2409      (point))
2410    gnus-mouse-face-prop gnus-mouse-face)
2411   (insert " " gnus-tmp-subject-or-nil "\n"))
2412
2413 (defvar gnus-summary-line-format-spec
2414   (gnus-byte-code 'gnus-summary-line-format-spec))
2415
2416 (defun gnus-summary-dummy-line-format-spec ()
2417   (insert "*  ")
2418   (gnus-put-text-property
2419    (point)
2420    (progn
2421      (insert ":                          :")
2422      (point))
2423    gnus-mouse-face-prop gnus-mouse-face)
2424   (insert " " gnus-tmp-subject "\n"))
2425
2426 (defvar gnus-summary-dummy-line-format-spec
2427   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2428
2429 (defun gnus-group-line-format-spec ()
2430   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2431           gnus-tmp-process-marked
2432           gnus-group-indentation
2433           (format "%5s: " gnus-tmp-number-of-unread))
2434   (gnus-put-text-property
2435    (point)
2436    (progn
2437      (insert gnus-tmp-group "\n")
2438      (1- (point)))
2439    gnus-mouse-face-prop gnus-mouse-face))
2440 (defvar gnus-group-line-format-spec
2441   (gnus-byte-code 'gnus-group-line-format-spec))
2442
2443 (defvar gnus-format-specs
2444   `((version . ,emacs-version)
2445     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2446     (summary-dummy ,gnus-summary-dummy-line-format
2447                    ,gnus-summary-dummy-line-format-spec)
2448     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2449
2450 (defvar gnus-article-mode-line-format-spec nil)
2451 (defvar gnus-summary-mode-line-format-spec nil)
2452 (defvar gnus-group-mode-line-format-spec nil)
2453
2454 ;;; Phew.  All that gruft is over, fortunately.
2455
2456 \f
2457 ;;;
2458 ;;; Gnus Utility Functions
2459 ;;;
2460
2461 (defun gnus-extract-address-components (from)
2462   (let (name address)
2463     ;; First find the address - the thing with the @ in it.  This may
2464     ;; not be accurate in mail addresses, but does the trick most of
2465     ;; the time in news messages.
2466     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2467         (setq address (substring from (match-beginning 0) (match-end 0))))
2468     ;; Then we check whether the "name <address>" format is used.
2469     (and address
2470          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2471          ;; Linear white space is not required.
2472          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2473          (and (setq name (substring from 0 (match-beginning 0)))
2474               ;; Strip any quotes from the name.
2475               (string-match "\".*\"" name)
2476               (setq name (substring name 1 (1- (match-end 0))))))
2477     ;; If not, then "address (name)" is used.
2478     (or name
2479         (and (string-match "(.+)" from)
2480              (setq name (substring from (1+ (match-beginning 0))
2481                                    (1- (match-end 0)))))
2482         (and (string-match "()" from)
2483              (setq name address))
2484         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2485         ;; XOVER might not support folded From headers.
2486         (and (string-match "(.*" from)
2487              (setq name (substring from (1+ (match-beginning 0))
2488                                    (match-end 0)))))
2489     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2490     (list (or name from) (or address from))))
2491
2492 (defun gnus-fetch-field (field)
2493   "Return the value of the header FIELD of current article."
2494   (save-excursion
2495     (save-restriction
2496       (let ((case-fold-search t)
2497             (inhibit-point-motion-hooks t))
2498         (nnheader-narrow-to-headers)
2499         (message-fetch-field field)))))
2500
2501 (defun gnus-goto-colon ()
2502   (beginning-of-line)
2503   (search-forward ":" (gnus-point-at-eol) t))
2504
2505 ;;;###autoload
2506 (defun gnus-update-format (var)
2507   "Update the format specification near point."
2508   (interactive
2509    (list
2510     (save-excursion
2511       (eval-defun nil)
2512       ;; Find the end of the current word.
2513       (re-search-forward "[ \t\n]" nil t)
2514       ;; Search backward.
2515       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2516         (match-string 1)))))
2517   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2518                               (match-string 1 var))))
2519          (entry (assq type gnus-format-specs))
2520          value spec)
2521     (when entry
2522       (setq gnus-format-specs (delq entry gnus-format-specs)))
2523     (set
2524      (intern (format "%s-spec" var))
2525      (gnus-parse-format (setq value (symbol-value (intern var)))
2526                         (symbol-value (intern (format "%s-alist" var)))
2527                         (not (string-match "mode" var))))
2528     (setq spec (symbol-value (intern (format "%s-spec" var))))
2529     (push (list type value spec) gnus-format-specs)
2530
2531     (pop-to-buffer "*Gnus Format*")
2532     (erase-buffer)
2533     (lisp-interaction-mode)
2534     (insert (pp-to-string spec))))
2535
2536 (defun gnus-update-format-specifications (&optional force)
2537   "Update all (necessary) format specifications."
2538   ;; Make the indentation array.
2539   (gnus-make-thread-indent-array)
2540
2541   ;; See whether all the stored info needs to be flushed.
2542   (when (or force
2543             (not (equal emacs-version
2544                         (cdr (assq 'version gnus-format-specs)))))
2545     (setq gnus-format-specs nil))
2546
2547   ;; Go through all the formats and see whether they need updating.
2548   (let ((types '(summary summary-dummy group
2549                          summary-mode group-mode article-mode))
2550         new-format entry type val)
2551     (while (setq type (pop types))
2552       ;; Jump to the proper buffer to find out the value of
2553       ;; the variable, if possible.  (It may be buffer-local.)
2554       (save-excursion
2555         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2556               val)
2557           (when (and (boundp buffer)
2558                      (setq val (symbol-value buffer))
2559                      (get-buffer val)
2560                      (buffer-name (get-buffer val)))
2561             (set-buffer (get-buffer val)))
2562           (setq new-format (symbol-value
2563                             (intern (format "gnus-%s-line-format" type))))))
2564       (setq entry (cdr (assq type gnus-format-specs)))
2565       (if (and entry
2566                (equal (car entry) new-format))
2567           ;; Use the old format.
2568           (set (intern (format "gnus-%s-line-format-spec" type))
2569                (cadr entry))
2570         ;; This is a new format.
2571         (setq val
2572               (if (not (stringp new-format))
2573                   ;; This is a function call or something.
2574                   new-format
2575                 ;; This is a "real" format.
2576                 (gnus-parse-format
2577                  new-format
2578                  (symbol-value
2579                   (intern (format "gnus-%s-line-format-alist"
2580                                   (if (eq type 'article-mode)
2581                                       'summary-mode type))))
2582                  (not (string-match "mode$" (symbol-name type))))))
2583         ;; Enter the new format spec into the list.
2584         (if entry
2585             (progn
2586               (setcar (cdr entry) val)
2587               (setcar entry new-format))
2588           (push (list type new-format val) gnus-format-specs))
2589         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2590
2591   (unless (assq 'version gnus-format-specs)
2592     (push (cons 'version emacs-version) gnus-format-specs))
2593
2594   (gnus-update-group-mark-positions)
2595   (gnus-update-summary-mark-positions))
2596
2597 (defun gnus-update-summary-mark-positions ()
2598   "Compute where the summary marks are to go."
2599   (save-excursion
2600     (when (and gnus-summary-buffer
2601                (get-buffer gnus-summary-buffer)
2602                (buffer-name (get-buffer gnus-summary-buffer)))
2603       (set-buffer gnus-summary-buffer))
2604     (let ((gnus-replied-mark 129)
2605           (gnus-score-below-mark 130)
2606           (gnus-score-over-mark 130)
2607           (thread nil)
2608           (gnus-visual nil)
2609           (spec gnus-summary-line-format-spec)
2610           pos)
2611       (save-excursion
2612         (gnus-set-work-buffer)
2613         (let ((gnus-summary-line-format-spec spec))
2614           (gnus-summary-insert-line
2615            [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2616           (goto-char (point-min))
2617           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2618                                              (- (point) 2)))))
2619           (goto-char (point-min))
2620           (push (cons 'replied (and (search-forward "\201" nil t) 
2621                                     (- (point) 2)))
2622                 pos)
2623           (goto-char (point-min))
2624           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2625                 pos)))
2626       (setq gnus-summary-mark-positions pos))))
2627
2628 (defun gnus-update-group-mark-positions ()
2629   (save-excursion
2630     (let ((gnus-process-mark 128)
2631           (gnus-group-marked '("dummy.group"))
2632           (gnus-active-hashtb (make-vector 10 0)))
2633       (gnus-set-active "dummy.group" '(0 . 0))
2634       (gnus-set-work-buffer)
2635       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2636       (goto-char (point-min))
2637       (setq gnus-group-mark-positions
2638             (list (cons 'process (and (search-forward "\200" nil t)
2639                                       (- (point) 2))))))))
2640
2641 (defvar gnus-mouse-face-0 'highlight)
2642 (defvar gnus-mouse-face-1 'highlight)
2643 (defvar gnus-mouse-face-2 'highlight)
2644 (defvar gnus-mouse-face-3 'highlight)
2645 (defvar gnus-mouse-face-4 'highlight)
2646
2647 (defun gnus-mouse-face-function (form type)
2648   `(gnus-put-text-property
2649     (point) (progn ,@form (point))
2650     gnus-mouse-face-prop
2651     ,(if (equal type 0)
2652          'gnus-mouse-face
2653        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2654
2655 (defvar gnus-face-0 'bold)
2656 (defvar gnus-face-1 'italic)
2657 (defvar gnus-face-2 'bold-italic)
2658 (defvar gnus-face-3 'bold)
2659 (defvar gnus-face-4 'bold)
2660
2661 (defun gnus-face-face-function (form type)
2662   `(gnus-put-text-property
2663     (point) (progn ,@form (point))
2664     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2665
2666 (defun gnus-max-width-function (el max-width)
2667   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2668   (if (symbolp el)
2669       `(if (> (length ,el) ,max-width)
2670            (substring ,el 0 ,max-width)
2671          ,el)
2672     `(let ((val (eval ,el)))
2673        (if (numberp val)
2674            (setq val (int-to-string val)))
2675        (if (> (length val) ,max-width)
2676            (substring val 0 ,max-width)
2677          val))))
2678
2679 (defun gnus-parse-format (format spec-alist &optional insert)
2680   ;; This function parses the FORMAT string with the help of the
2681   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2682   ;; string.  If the FORMAT string contains the specifiers %( and %)
2683   ;; the text between them will have the mouse-face text property.
2684   (if (string-match
2685        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2686        format)
2687       (gnus-parse-complex-format format spec-alist)
2688     ;; This is a simple format.
2689     (gnus-parse-simple-format format spec-alist insert)))
2690
2691 (defun gnus-parse-complex-format (format spec-alist)
2692   (save-excursion
2693     (gnus-set-work-buffer)
2694     (insert format)
2695     (goto-char (point-min))
2696     (while (re-search-forward "\"" nil t)
2697       (replace-match "\\\"" nil t))
2698     (goto-char (point-min))
2699     (insert "(\"")
2700     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2701       (let ((number (if (match-beginning 1)
2702                         (match-string 1) "0"))
2703             (delim (aref (match-string 2) 0)))
2704         (if (or (= delim ?\() (= delim ?\{))
2705             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2706                                    " " number " \""))
2707           (replace-match "\")\""))))
2708     (goto-char (point-max))
2709     (insert "\")")
2710     (goto-char (point-min))
2711     (let ((form (read (current-buffer))))
2712       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2713
2714 (defun gnus-complex-form-to-spec (form spec-alist)
2715   (delq nil
2716         (mapcar
2717          (lambda (sform)
2718            (if (stringp sform)
2719                (gnus-parse-simple-format sform spec-alist t)
2720              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2721                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2722                       (nth 1 sform))))
2723          form)))
2724
2725 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2726   ;; This function parses the FORMAT string with the help of the
2727   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2728   ;; string.
2729   (let ((max-width 0)
2730         spec flist fstring newspec elem beg result dontinsert)
2731     (save-excursion
2732       (gnus-set-work-buffer)
2733       (insert format)
2734       (goto-char (point-min))
2735       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2736                                 nil t)
2737         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2738               (setq newspec "%"
2739                     beg (1+ (match-beginning 0)))
2740           ;; First check if there are any specs that look anything like
2741           ;; "%12,12A", ie. with a "max width specification".  These have
2742           ;; to be treated specially.
2743           (if (setq beg (match-beginning 1))
2744               (setq max-width
2745                     (string-to-int
2746                      (buffer-substring
2747                       (1+ (match-beginning 1)) (match-end 1))))
2748             (setq max-width 0)
2749             (setq beg (match-beginning 2)))
2750           ;; Find the specification from `spec-alist'.
2751           (unless (setq elem (cdr (assq spec spec-alist)))
2752             (setq elem '("*" ?s)))
2753           ;; Treat user defined format specifiers specially.
2754           (when (eq (car elem) 'gnus-tmp-user-defined)
2755             (setq elem
2756                   (list
2757                    (list (intern (concat "gnus-user-format-function-"
2758                                          (match-string 3)))
2759                          'gnus-tmp-header) ?s))
2760             (delete-region (match-beginning 3) (match-end 3)))
2761           (if (not (zerop max-width))
2762               (let ((el (car elem)))
2763                 (cond ((= (cadr elem) ?c)
2764                        (setq el (list 'char-to-string el)))
2765                       ((= (cadr elem) ?d)
2766                        (setq el (list 'int-to-string el))))
2767                 (setq flist (cons (gnus-max-width-function el max-width)
2768                                   flist))
2769                 (setq newspec ?s))
2770             (progn
2771               (setq flist (cons (car elem) flist))
2772               (setq newspec (cadr elem)))))
2773         ;; Remove the old specification (and possibly a ",12" string).
2774         (delete-region beg (match-end 2))
2775         ;; Insert the new specification.
2776         (goto-char beg)
2777         (insert newspec))
2778       (setq fstring (buffer-substring 1 (point-max))))
2779     ;; Do some postprocessing to increase efficiency.
2780     (setq
2781      result
2782      (cond
2783       ;; Emptyness.
2784       ((string= fstring "")
2785        nil)
2786       ;; Not a format string.
2787       ((not (string-match "%" fstring))
2788        (list fstring))
2789       ;; A format string with just a single string spec.
2790       ((string= fstring "%s")
2791        (list (car flist)))
2792       ;; A single character.
2793       ((string= fstring "%c")
2794        (list (car flist)))
2795       ;; A single number.
2796       ((string= fstring "%d")
2797        (setq dontinsert)
2798        (if insert
2799            (list `(princ ,(car flist)))
2800          (list `(int-to-string ,(car flist)))))
2801       ;; Just lots of chars and strings.
2802       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2803        (nreverse flist))
2804       ;; A single string spec at the beginning of the spec.
2805       ((string-match "\\`%[sc][^%]+\\'" fstring)
2806        (list (car flist) (substring fstring 2)))
2807       ;; A single string spec in the middle of the spec.
2808       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2809        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2810       ;; A single string spec in the end of the spec.
2811       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2812        (list (match-string 1 fstring) (car flist)))
2813       ;; A more complex spec.
2814       (t
2815        (list (cons 'format (cons fstring (nreverse flist)))))))
2816
2817     (if insert
2818         (when result
2819           (if dontinsert
2820               result
2821             (cons 'insert result)))
2822       (cond ((stringp result)
2823              result)
2824             ((consp result)
2825              (cons 'concat result))
2826             (t "")))))
2827
2828 (defun gnus-eval-format (format &optional alist props)
2829   "Eval the format variable FORMAT, using ALIST.
2830 If PROPS, insert the result."
2831   (let ((form (gnus-parse-format format alist props)))
2832     (if props
2833         (gnus-add-text-properties (point) (progn (eval form) (point)) props)
2834       (eval form))))
2835
2836 (defun gnus-remove-text-with-property (prop)
2837   "Delete all text in the current buffer with text property PROP."
2838   (save-excursion
2839     (goto-char (point-min))
2840     (while (not (eobp))
2841       (while (get-text-property (point) prop)
2842         (delete-char 1))
2843       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2844
2845 (defun gnus-set-work-buffer ()
2846   (if (get-buffer gnus-work-buffer)
2847       (progn
2848         (set-buffer gnus-work-buffer)
2849         (erase-buffer))
2850     (set-buffer (get-buffer-create gnus-work-buffer))
2851     (kill-all-local-variables)
2852     (buffer-disable-undo (current-buffer))
2853     (gnus-add-current-to-buffer-list)))
2854
2855 ;; Article file names when saving.
2856
2857 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2858   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2859 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2860 Otherwise, it is like ~/News/news/group/num."
2861   (let ((default
2862           (expand-file-name
2863            (concat (if (gnus-use-long-file-name 'not-save)
2864                        (gnus-capitalize-newsgroup newsgroup)
2865                      (gnus-newsgroup-directory-form newsgroup))
2866                    "/" (int-to-string (mail-header-number headers)))
2867            gnus-article-save-directory)))
2868     (if (and last-file
2869              (string-equal (file-name-directory default)
2870                            (file-name-directory last-file))
2871              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2872         default
2873       (or last-file default))))
2874
2875 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2876   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2877 If variable `gnus-use-long-file-name' is non-nil, it is
2878 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2879   (let ((default
2880           (expand-file-name
2881            (concat (if (gnus-use-long-file-name 'not-save)
2882                        newsgroup
2883                      (gnus-newsgroup-directory-form newsgroup))
2884                    "/" (int-to-string (mail-header-number headers)))
2885            gnus-article-save-directory)))
2886     (if (and last-file
2887              (string-equal (file-name-directory default)
2888                            (file-name-directory last-file))
2889              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2890         default
2891       (or last-file default))))
2892
2893 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2894   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2895 If variable `gnus-use-long-file-name' is non-nil, it is
2896 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2897   (or last-file
2898       (expand-file-name
2899        (if (gnus-use-long-file-name 'not-save)
2900            (gnus-capitalize-newsgroup newsgroup)
2901          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2902        gnus-article-save-directory)))
2903
2904 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2905   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2906 If variable `gnus-use-long-file-name' is non-nil, it is
2907 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2908   (or last-file
2909       (expand-file-name
2910        (if (gnus-use-long-file-name 'not-save)
2911            newsgroup
2912          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2913        gnus-article-save-directory)))
2914
2915 ;; For subscribing new newsgroup
2916
2917 (defun gnus-subscribe-hierarchical-interactive (groups)
2918   (let ((groups (sort groups 'string<))
2919         prefixes prefix start ans group starts)
2920     (while groups
2921       (setq prefixes (list "^"))
2922       (while (and groups prefixes)
2923         (while (not (string-match (car prefixes) (car groups)))
2924           (setq prefixes (cdr prefixes)))
2925         (setq prefix (car prefixes))
2926         (setq start (1- (length prefix)))
2927         (if (and (string-match "[^\\.]\\." (car groups) start)
2928                  (cdr groups)
2929                  (setq prefix
2930                        (concat "^" (substring (car groups) 0 (match-end 0))))
2931                  (string-match prefix (cadr groups)))
2932             (progn
2933               (setq prefixes (cons prefix prefixes))
2934               (message "Descend hierarchy %s? ([y]nsq): "
2935                        (substring prefix 1 (1- (length prefix))))
2936               (while (not (memq (setq ans (read-char)) '(?y ?\n ?n ?s ?q)))
2937                 (ding)
2938                 (message "Descend hierarchy %s? ([y]nsq): "
2939                          (substring prefix 1 (1- (length prefix)))))
2940               (cond ((= ans ?n)
2941                      (while (and groups
2942                                  (string-match prefix
2943                                                (setq group (car groups))))
2944                        (setq gnus-killed-list
2945                              (cons group gnus-killed-list))
2946                        (gnus-sethash group group gnus-killed-hashtb)
2947                        (setq groups (cdr groups)))
2948                      (setq starts (cdr starts)))
2949                     ((= ans ?s)
2950                      (while (and groups
2951                                  (string-match prefix
2952                                                (setq group (car groups))))
2953                        (gnus-sethash group group gnus-killed-hashtb)
2954                        (gnus-subscribe-alphabetically (car groups))
2955                        (setq groups (cdr groups)))
2956                      (setq starts (cdr starts)))
2957                     ((= ans ?q)
2958                      (while groups
2959                        (setq group (car groups))
2960                        (setq gnus-killed-list (cons group gnus-killed-list))
2961                        (gnus-sethash group group gnus-killed-hashtb)
2962                        (setq groups (cdr groups))))
2963                     (t nil)))
2964           (message "Subscribe %s? ([n]yq)" (car groups))
2965           (while (not (memq (setq ans (read-char)) '(?y ?\n ?q ?n)))
2966             (ding)
2967             (message "Subscribe %s? ([n]yq)" (car groups)))
2968           (setq group (car groups))
2969           (cond ((= ans ?y)
2970                  (gnus-subscribe-alphabetically (car groups))
2971                  (gnus-sethash group group gnus-killed-hashtb))
2972                 ((= ans ?q)
2973                  (while groups
2974                    (setq group (car groups))
2975                    (setq gnus-killed-list (cons group gnus-killed-list))
2976                    (gnus-sethash group group gnus-killed-hashtb)
2977                    (setq groups (cdr groups))))
2978                 (t
2979                  (setq gnus-killed-list (cons group gnus-killed-list))
2980                  (gnus-sethash group group gnus-killed-hashtb)))
2981           (setq groups (cdr groups)))))))
2982
2983 (defun gnus-subscribe-randomly (newsgroup)
2984   "Subscribe new NEWSGROUP by making it the first newsgroup."
2985   (gnus-subscribe-newsgroup newsgroup))
2986
2987 (defun gnus-subscribe-alphabetically (newgroup)
2988   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2989   (let ((groups (cdr gnus-newsrc-alist))
2990         before)
2991     (while (and (not before) groups)
2992       (if (string< newgroup (caar groups))
2993           (setq before (caar groups))
2994         (setq groups (cdr groups))))
2995     (gnus-subscribe-newsgroup newgroup before)))
2996
2997 (defun gnus-subscribe-hierarchically (newgroup)
2998   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2999   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
3000   (save-excursion
3001     (set-buffer (find-file-noselect gnus-current-startup-file))
3002     (let ((groupkey newgroup)
3003           before)
3004       (while (and (not before) groupkey)
3005         (goto-char (point-min))
3006         (let ((groupkey-re
3007                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
3008           (while (and (re-search-forward groupkey-re nil t)
3009                       (progn
3010                         (setq before (match-string 1))
3011                         (string< before newgroup)))))
3012         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
3013         (setq groupkey
3014               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
3015                   (substring groupkey (match-beginning 1) (match-end 1)))))
3016       (gnus-subscribe-newsgroup newgroup before))))
3017
3018 (defun gnus-subscribe-interactively (group)
3019   "Subscribe the new GROUP interactively.
3020 It is inserted in hierarchical newsgroup order if subscribed.  If not,
3021 it is killed."
3022   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
3023       (gnus-subscribe-hierarchically group)
3024     (push group gnus-killed-list)))
3025
3026 (defun gnus-subscribe-zombies (group)
3027   "Make the new GROUP into a zombie group."
3028   (push group gnus-zombie-list))
3029
3030 (defun gnus-subscribe-killed (group)
3031   "Make the new GROUP a killed group."
3032   (push group gnus-killed-list))
3033
3034 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
3035   "Subscribe new NEWSGROUP.
3036 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
3037 the first newsgroup."
3038   ;; We subscribe the group by changing its level to `subscribed'.
3039   (gnus-group-change-level
3040    newsgroup gnus-level-default-subscribed
3041    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
3042   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
3043
3044 ;; For directories
3045
3046 (defun gnus-newsgroup-directory-form (newsgroup)
3047   "Make hierarchical directory name from NEWSGROUP name."
3048   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
3049         (len (length newsgroup))
3050         idx)
3051     ;; If this is a foreign group, we don't want to translate the
3052     ;; entire name.
3053     (if (setq idx (string-match ":" newsgroup))
3054         (aset newsgroup idx ?/)
3055       (setq idx 0))
3056     ;; Replace all occurrences of `.' with `/'.
3057     (while (< idx len)
3058       (if (= (aref newsgroup idx) ?.)
3059           (aset newsgroup idx ?/))
3060       (setq idx (1+ idx)))
3061     newsgroup))
3062
3063 (defun gnus-newsgroup-savable-name (group)
3064   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
3065   ;; with dots.
3066   (nnheader-replace-chars-in-string group ?/ ?.))
3067
3068 (defun gnus-make-directory (dir)
3069   "Make DIRECTORY recursively."
3070   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
3071   ;; of the many mysteries of the universe.
3072   (let* ((dir (expand-file-name dir default-directory))
3073          dirs err)
3074     (if (string-match "/$" dir)
3075         (setq dir (substring dir 0 (match-beginning 0))))
3076     ;; First go down the path until we find a directory that exists.
3077     (while (not (file-exists-p dir))
3078       (setq dirs (cons dir dirs))
3079       (string-match "/[^/]+$" dir)
3080       (setq dir (substring dir 0 (match-beginning 0))))
3081     ;; Then create all the subdirs.
3082     (while (and dirs (not err))
3083       (condition-case ()
3084           (make-directory (car dirs))
3085         (error (setq err t)))
3086       (setq dirs (cdr dirs)))
3087     ;; We return whether we were successful or not.
3088     (not dirs)))
3089
3090 (defun gnus-capitalize-newsgroup (newsgroup)
3091   "Capitalize NEWSGROUP name."
3092   (and (not (zerop (length newsgroup)))
3093        (concat (char-to-string (upcase (aref newsgroup 0)))
3094                (substring newsgroup 1))))
3095
3096 ;; Various... things.
3097
3098 (defun gnus-simplify-subject (subject &optional re-only)
3099   "Remove `Re:' and words in parentheses.
3100 If RE-ONLY is non-nil, strip leading `Re:'s only."
3101   (let ((case-fold-search t))           ;Ignore case.
3102     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
3103     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
3104       (setq subject (substring subject (match-end 0))))
3105     ;; Remove uninteresting prefixes.
3106     (if (and (not re-only)
3107              gnus-simplify-ignored-prefixes
3108              (string-match gnus-simplify-ignored-prefixes subject))
3109         (setq subject (substring subject (match-end 0))))
3110     ;; Remove words in parentheses from end.
3111     (unless re-only
3112       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3113         (setq subject (substring subject 0 (match-beginning 0)))))
3114     ;; Return subject string.
3115     subject))
3116
3117 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3118 ;; all whitespace.
3119 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3120 (defun gnus-simplify-buffer-fuzzy ()
3121   (let ((case-fold-search t))
3122     (goto-char (point-min))
3123     (while (search-forward "\t" nil t)
3124       (replace-match " " t t))
3125     (goto-char (point-min))
3126     (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *" nil t)
3127     (goto-char (match-beginning 0))
3128     (while (or
3129             (looking-at "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
3130             (looking-at "^[[].*: .*[]]$"))
3131       (goto-char (point-min))
3132       (while (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *"
3133                                 nil t)
3134         (replace-match "" t t))
3135       (goto-char (point-min))
3136       (while (re-search-forward "^[[].*: .*[]]$" nil t)
3137         (goto-char (match-end 0))
3138         (delete-char -1)
3139         (delete-region
3140          (progn (goto-char (match-beginning 0)))
3141          (re-search-forward ":"))))
3142     (goto-char (point-min))
3143     (while (re-search-forward " *[[{(][^()\n]*[]})] *$" nil t)
3144       (replace-match "" t t))
3145     (goto-char (point-min))
3146     (while (re-search-forward "  +" nil t)
3147       (replace-match " " t t))
3148     (goto-char (point-min))
3149     (while (re-search-forward " $" nil t)
3150       (replace-match "" t t))
3151     (goto-char (point-min))
3152     (while (re-search-forward "^ +" nil t)
3153       (replace-match "" t t))
3154     (goto-char (point-min))
3155     (when gnus-simplify-subject-fuzzy-regexp
3156       (if (listp gnus-simplify-subject-fuzzy-regexp)
3157           (let ((list gnus-simplify-subject-fuzzy-regexp))
3158             (while list
3159               (goto-char (point-min))
3160               (while (re-search-forward (car list) nil t)
3161                 (replace-match "" t t))
3162               (setq list (cdr list))))
3163         (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3164           (replace-match "" t t))))))
3165
3166 (defun gnus-simplify-subject-fuzzy (subject)
3167   "Siplify a subject string fuzzily."
3168   (save-excursion
3169     (gnus-set-work-buffer)
3170     (let ((case-fold-search t))
3171       (insert subject)
3172       (inline (gnus-simplify-buffer-fuzzy))
3173       (buffer-string))))
3174
3175 ;; Add the current buffer to the list of buffers to be killed on exit.
3176 (defun gnus-add-current-to-buffer-list ()
3177   (or (memq (current-buffer) gnus-buffer-list)
3178       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3179
3180 (defun gnus-string> (s1 s2)
3181   (not (or (string< s1 s2)
3182            (string= s1 s2))))
3183
3184 (defun gnus-read-active-file-p ()
3185   "Say whether the active file has been read from `gnus-select-method'."
3186   (memq gnus-select-method gnus-have-read-active-file))
3187
3188 ;;; General various misc type functions.
3189
3190 (defun gnus-clear-system ()
3191   "Clear all variables and buffers."
3192   ;; Clear Gnus variables.
3193   (let ((variables gnus-variable-list))
3194     (while variables
3195       (set (car variables) nil)
3196       (setq variables (cdr variables))))
3197   ;; Clear other internal variables.
3198   (setq gnus-list-of-killed-groups nil
3199         gnus-have-read-active-file nil
3200         gnus-newsrc-alist nil
3201         gnus-newsrc-hashtb nil
3202         gnus-killed-list nil
3203         gnus-zombie-list nil
3204         gnus-killed-hashtb nil
3205         gnus-active-hashtb nil
3206         gnus-moderated-list nil
3207         gnus-description-hashtb nil
3208         gnus-current-headers nil
3209         gnus-thread-indent-array nil
3210         gnus-newsgroup-headers nil
3211         gnus-newsgroup-name nil
3212         gnus-server-alist nil
3213         gnus-group-list-mode nil
3214         gnus-opened-servers nil
3215         gnus-current-select-method nil)
3216   (gnus-shutdown 'gnus)
3217   ;; Kill the startup file.
3218   (and gnus-current-startup-file
3219        (get-file-buffer gnus-current-startup-file)
3220        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3221   ;; Clear the dribble buffer.
3222   (gnus-dribble-clear)
3223   ;; Kill global KILL file buffer.
3224   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3225     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3226   (gnus-kill-buffer nntp-server-buffer)
3227   ;; Kill Gnus buffers.
3228   (while gnus-buffer-list
3229     (gnus-kill-buffer (pop gnus-buffer-list)))
3230   ;; Remove Gnus frames.
3231   (gnus-kill-gnus-frames))
3232
3233 (defun gnus-kill-gnus-frames ()
3234   "Kill all frames Gnus has created."
3235   (while gnus-created-frames
3236     (when (frame-live-p (car gnus-created-frames))
3237       ;; We slap a condition-case around this `delete-frame' to ensure 
3238       ;; against errors if we try do delete the single frame that's left.
3239       (condition-case ()
3240           (delete-frame (car gnus-created-frames))
3241         (error nil)))
3242     (pop gnus-created-frames)))
3243
3244 (defun gnus-windows-old-to-new (setting)
3245   ;; First we take care of the really, really old Gnus 3 actions.
3246   (when (symbolp setting)
3247     (setq setting
3248           ;; Take care of ooold GNUS 3.x values.
3249           (cond ((eq setting 'SelectArticle) 'article)
3250                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3251                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3252                 (t setting))))
3253   (if (or (listp setting)
3254           (not (and gnus-window-configuration
3255                     (memq setting '(group summary article)))))
3256       setting
3257     (let* ((setting (if (eq setting 'group)
3258                         (if (assq 'newsgroup gnus-window-configuration)
3259                             'newsgroup
3260                           'newsgroups) setting))
3261            (elem (cadr (assq setting gnus-window-configuration)))
3262            (total (apply '+ elem))
3263            (types '(group summary article))
3264            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3265            (i 0)
3266            perc
3267            out)
3268       (while (< i 3)
3269         (or (not (numberp (nth i elem)))
3270             (zerop (nth i elem))
3271             (progn
3272               (setq perc (if (= i 2)
3273                              1.0
3274                            (/ (float (nth 0 elem)) total)))
3275               (setq out (cons (if (eq pbuf (nth i types))
3276                                   (list (nth i types) perc 'point)
3277                                 (list (nth i types) perc))
3278                               out))))
3279         (setq i (1+ i)))
3280       `(vertical 1.0 ,@(nreverse out)))))
3281
3282 ;;;###autoload
3283 (defun gnus-add-configuration (conf)
3284   "Add the window configuration CONF to `gnus-buffer-configuration'."
3285   (setq gnus-buffer-configuration
3286         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3287                          gnus-buffer-configuration))))
3288
3289 (defvar gnus-frame-list nil)
3290
3291 (defun gnus-configure-frame (split &optional window)
3292   "Split WINDOW according to SPLIT."
3293   (unless window
3294     (setq window (get-buffer-window (current-buffer))))
3295   (select-window window)
3296   ;; This might be an old-stylee buffer config.
3297   (when (vectorp split)
3298     (setq split (append split nil)))
3299   (when (or (consp (car split))
3300             (vectorp (car split)))
3301     (push 1.0 split)
3302     (push 'vertical split))
3303   ;; The SPLIT might be something that is to be evaled to
3304   ;; return a new SPLIT.
3305   (while (and (not (assq (car split) gnus-window-to-buffer))
3306               (gnus-functionp (car split)))
3307     (setq split (eval split)))
3308   (let* ((type (car split))
3309          (subs (cddr split))
3310          (len (if (eq type 'horizontal) (window-width) (window-height)))
3311          (total 0)
3312          (window-min-width (or gnus-window-min-width window-min-width))
3313          (window-min-height (or gnus-window-min-height window-min-height))
3314          s result new-win rest comp-subs size sub)
3315     (cond
3316      ;; Nothing to do here.
3317      ((null split))
3318      ;; Don't switch buffers.
3319      ((null type)
3320       (and (memq 'point split) window))
3321      ;; This is a buffer to be selected.
3322      ((not (memq type '(frame horizontal vertical)))
3323       (let ((buffer (cond ((stringp type) type)
3324                           (t (cdr (assq type gnus-window-to-buffer)))))
3325             buf)
3326         (unless buffer
3327           (error "Illegal buffer type: %s" type))
3328         (unless (setq buf (get-buffer (if (symbolp buffer)
3329                                           (symbol-value buffer) buffer)))
3330           (setq buf (get-buffer-create (if (symbolp buffer)
3331                                            (symbol-value buffer) buffer))))
3332         (switch-to-buffer buf)
3333         ;; We return the window if it has the `point' spec.
3334         (and (memq 'point split) window)))
3335      ;; This is a frame split.
3336      ((eq type 'frame)
3337       (unless gnus-frame-list
3338         (setq gnus-frame-list (list (window-frame
3339                                      (get-buffer-window (current-buffer))))))
3340       (let ((i 0)
3341             params frame fresult)
3342         (while (< i (length subs))
3343           ;; Frame parameter is gotten from the sub-split.
3344           (setq params (cadr (elt subs i)))
3345           ;; It should be a list.
3346           (unless (listp params)
3347             (setq params nil))
3348           ;; Create a new frame?
3349           (unless (setq frame (elt gnus-frame-list i))
3350             (nconc gnus-frame-list (list (setq frame (make-frame params))))
3351             (push frame gnus-created-frames))
3352           ;; Is the old frame still alive?
3353           (unless (frame-live-p frame)
3354             (setcar (nthcdr i gnus-frame-list)
3355                     (setq frame (make-frame params))))
3356           ;; Select the frame in question and do more splits there.
3357           (select-frame frame)
3358           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3359           (incf i))
3360         ;; Select the frame that has the selected buffer.
3361         (when fresult
3362           (select-frame (window-frame fresult)))))
3363      ;; This is a normal split.
3364      (t
3365       (when (> (length subs) 0)
3366         ;; First we have to compute the sizes of all new windows.
3367         (while subs
3368           (setq sub (append (pop subs) nil))
3369           (while (and (not (assq (car sub) gnus-window-to-buffer))
3370                       (gnus-functionp (car sub)))
3371             (setq sub (eval sub)))
3372           (when sub
3373             (push sub comp-subs)
3374             (setq size (cadar comp-subs))
3375             (cond ((equal size 1.0)
3376                    (setq rest (car comp-subs))
3377                    (setq s 0))
3378                   ((floatp size)
3379                    (setq s (floor (* size len))))
3380                   ((integerp size)
3381                    (setq s size))
3382                   (t
3383                    (error "Illegal size: %s" size)))
3384             ;; Try to make sure that we are inside the safe limits.
3385             (cond ((zerop s))
3386                   ((eq type 'horizontal)
3387                    (setq s (max s window-min-width)))
3388                   ((eq type 'vertical)
3389                    (setq s (max s window-min-height))))
3390             (setcar (cdar comp-subs) s)
3391             (incf total s)))
3392         ;; Take care of the "1.0" spec.
3393         (if rest
3394             (setcar (cdr rest) (- len total))
3395           (error "No 1.0 specs in %s" split))
3396         ;; The we do the actual splitting in a nice recursive
3397         ;; fashion.
3398         (setq comp-subs (nreverse comp-subs))
3399         (while comp-subs
3400           (if (null (cdr comp-subs))
3401               (setq new-win window)
3402             (setq new-win
3403                   (split-window window (cadar comp-subs)
3404                                 (eq type 'horizontal))))
3405           (setq result (or (gnus-configure-frame
3406                             (car comp-subs) window) result))
3407           (select-window new-win)
3408           (setq window new-win)
3409           (setq comp-subs (cdr comp-subs))))
3410       ;; Return the proper window, if any.
3411       (when result
3412         (select-window result))))))
3413
3414 (defvar gnus-frame-split-p nil)
3415
3416 (defun gnus-configure-windows (setting &optional force)
3417   (setq setting (gnus-windows-old-to-new setting))
3418   (let ((split (if (symbolp setting)
3419                    (cadr (assq setting gnus-buffer-configuration))
3420                  setting))
3421         all-visible)
3422
3423     (setq gnus-frame-split-p nil)
3424
3425     (unless split
3426       (error "No such setting: %s" setting))
3427
3428     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3429              (not force))
3430         ;; All the windows mentioned are already visible, so we just
3431         ;; put point in the assigned buffer, and do not touch the
3432         ;; winconf.
3433         (select-window all-visible)
3434
3435       ;; Either remove all windows or just remove all Gnus windows.
3436       (let ((frame (selected-frame)))
3437         (unwind-protect
3438             (if gnus-use-full-window
3439                 ;; We want to remove all other windows.
3440                 (if (not gnus-frame-split-p)
3441                     ;; This is not a `frame' split, so we ignore the
3442                     ;; other frames.  
3443                     (delete-other-windows)
3444                   ;; This is a `frame' split, so we delete all windows
3445                   ;; on all frames.
3446                   (mapcar 
3447                    (lambda (frame)
3448                      (unless (eq (cdr (assq 'minibuffer
3449                                             (frame-parameters frame)))
3450                                  'only)
3451                        (select-frame frame)
3452                        (delete-other-windows)))
3453                    (frame-list)))
3454               ;; Just remove some windows.
3455               (gnus-remove-some-windows)
3456               (switch-to-buffer nntp-server-buffer))
3457           (select-frame frame)))
3458
3459       (switch-to-buffer nntp-server-buffer)
3460       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3461
3462 (defun gnus-all-windows-visible-p (split)
3463   "Say whether all buffers in SPLIT are currently visible.
3464 In particular, the value returned will be the window that
3465 should have point."
3466   (let ((stack (list split))
3467         (all-visible t)
3468         type buffer win buf)
3469     (while (and (setq split (pop stack))
3470                 all-visible)
3471       ;; Be backwards compatible.
3472       (when (vectorp split)
3473         (setq split (append split nil)))
3474       (when (or (consp (car split))
3475                 (vectorp (car split)))
3476         (push 1.0 split)
3477         (push 'vertical split))
3478       ;; The SPLIT might be something that is to be evaled to
3479       ;; return a new SPLIT.
3480       (while (and (not (assq (car split) gnus-window-to-buffer))
3481                   (gnus-functionp (car split)))
3482         (setq split (eval split)))
3483
3484       (setq type (elt split 0))
3485       (cond
3486        ;; Nothing here.
3487        ((null split) t)
3488        ;; A buffer.
3489        ((not (memq type '(horizontal vertical frame)))
3490         (setq buffer (cond ((stringp type) type)
3491                            (t (cdr (assq type gnus-window-to-buffer)))))
3492         (unless buffer
3493           (error "Illegal buffer type: %s" type))
3494         (when (setq buf (get-buffer (if (symbolp buffer)
3495                                         (symbol-value buffer)
3496                                       buffer)))
3497           (setq win (get-buffer-window buf t)))
3498         (if win
3499             (when (memq 'point split)
3500                 (setq all-visible win))
3501           (setq all-visible nil)))
3502        (t
3503         (when (eq type 'frame)
3504           (setq gnus-frame-split-p t))
3505         (setq stack (append (cddr split) stack)))))
3506     (unless (eq all-visible t)
3507       all-visible)))
3508
3509 (defun gnus-window-top-edge (&optional window)
3510   (nth 1 (window-edges window)))
3511
3512 (defun gnus-remove-some-windows ()
3513   (let ((buffers gnus-window-to-buffer)
3514         buf bufs lowest-buf lowest)
3515     (save-excursion
3516       ;; Remove windows on all known Gnus buffers.
3517       (while buffers
3518         (setq buf (cdar buffers))
3519         (if (symbolp buf)
3520             (setq buf (and (boundp buf) (symbol-value buf))))
3521         (and buf
3522              (get-buffer-window buf)
3523              (progn
3524                (setq bufs (cons buf bufs))
3525                (pop-to-buffer buf)
3526                (if (or (not lowest)
3527                        (< (gnus-window-top-edge) lowest))
3528                    (progn
3529                      (setq lowest (gnus-window-top-edge))
3530                      (setq lowest-buf buf)))))
3531         (setq buffers (cdr buffers)))
3532       ;; Remove windows on *all* summary buffers.
3533       (walk-windows
3534        (lambda (win)
3535          (let ((buf (window-buffer win)))
3536            (if (string-match    "^\\*Summary" (buffer-name buf))
3537                (progn
3538                  (setq bufs (cons buf bufs))
3539                  (pop-to-buffer buf)
3540                  (if (or (not lowest)
3541                          (< (gnus-window-top-edge) lowest))
3542                      (progn
3543                        (setq lowest-buf buf)
3544                        (setq lowest (gnus-window-top-edge)))))))))
3545       (and lowest-buf
3546            (progn
3547              (pop-to-buffer lowest-buf)
3548              (switch-to-buffer nntp-server-buffer)))
3549       (while bufs
3550         (and (not (eq (car bufs) lowest-buf))
3551              (delete-windows-on (car bufs)))
3552         (setq bufs (cdr bufs))))))
3553
3554 (defun gnus-version (&optional arg)
3555   "Version number of this version of Gnus.
3556 If ARG, insert string at point."
3557   (interactive "P")
3558   (let ((methods gnus-valid-select-methods)
3559         (mess gnus-version)
3560         meth)
3561     ;; Go through all the legal select methods and add their version
3562     ;; numbers to the total version string.  Only the backends that are
3563     ;; currently in use will have their message numbers taken into
3564     ;; consideration.
3565     (while methods
3566       (setq meth (intern (concat (caar methods) "-version")))
3567       (and (boundp meth)
3568            (stringp (symbol-value meth))
3569            (setq mess (concat mess "; " (symbol-value meth))))
3570       (setq methods (cdr methods)))
3571     (if arg
3572         (insert (message mess))
3573       (message mess))))
3574
3575 (defun gnus-info-find-node ()
3576   "Find Info documentation of Gnus."
3577   (interactive)
3578   ;; Enlarge info window if needed.
3579   (let ((mode major-mode)
3580         gnus-info-buffer)
3581     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3582     (setq gnus-info-buffer (current-buffer))
3583     (gnus-configure-windows 'info)))
3584
3585 (defun gnus-days-between (date1 date2)
3586   ;; Return the number of days between date1 and date2.
3587   (- (gnus-day-number date1) (gnus-day-number date2)))
3588
3589 (defun gnus-day-number (date)
3590   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3591                      (timezone-parse-date date))))
3592     (timezone-absolute-from-gregorian
3593      (nth 1 dat) (nth 2 dat) (car dat))))
3594
3595 (defun gnus-encode-date (date)
3596   "Convert DATE to internal time."
3597   (let* ((parse (timezone-parse-date date))
3598          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3599          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3600     (encode-time (caddr time) (cadr time) (car time)
3601                  (caddr date) (cadr date) (car date) (nth 4 date))))
3602
3603 (defun gnus-time-minus (t1 t2)
3604   "Subtract two internal times."
3605   (let ((borrow (< (cadr t1) (cadr t2))))
3606     (list (- (car t1) (car t2) (if borrow 1 0))
3607           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3608
3609 (defun gnus-file-newer-than (file date)
3610   (let ((fdate (nth 5 (file-attributes file))))
3611     (or (> (car fdate) (car date))
3612         (and (= (car fdate) (car date))
3613              (> (nth 1 fdate) (nth 1 date))))))
3614
3615 (defmacro gnus-local-set-keys (&rest plist)
3616   "Set the keys in PLIST in the current keymap."
3617   `(gnus-define-keys-1 (current-local-map) ',plist))
3618
3619 (defmacro gnus-define-keys (keymap &rest plist)
3620   "Define all keys in PLIST in KEYMAP."
3621   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3622
3623 (put 'gnus-define-keys 'lisp-indent-function 1)
3624 (put 'gnus-define-keys 'lisp-indent-hook 1)
3625 (put 'gnus-define-keymap 'lisp-indent-function 1)
3626 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3627
3628 (defmacro gnus-define-keymap (keymap &rest plist)
3629   "Define all keys in PLIST in KEYMAP."
3630   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3631
3632 (defun gnus-define-keys-1 (keymap plist)
3633   (when (null keymap)
3634     (error "Can't set keys in a null keymap"))
3635   (cond ((symbolp keymap)
3636          (setq keymap (symbol-value keymap)))
3637         ((keymapp keymap))
3638         ((listp keymap)
3639          (set (car keymap) nil)
3640          (define-prefix-command (car keymap))
3641          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3642          (setq keymap (symbol-value (car keymap)))))
3643   (let (key)
3644     (while plist
3645       (when (symbolp (setq key (pop plist)))
3646         (setq key (symbol-value key)))
3647       (define-key keymap key (pop plist)))))
3648
3649 (defun gnus-group-read-only-p (&optional group)
3650   "Check whether GROUP supports editing or not.
3651 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3652 that that variable is buffer-local to the summary buffers."
3653   (let ((group (or group gnus-newsgroup-name)))
3654     (not (gnus-check-backend-function 'request-replace-article group))))
3655
3656 (defun gnus-group-total-expirable-p (group)
3657   "Check whether GROUP is total-expirable or not."
3658   (let ((params (gnus-info-params (gnus-get-info group))))
3659     (or (memq 'total-expire params)
3660         (cdr (assq 'total-expire params)) ; (total-expire . t)
3661         (and gnus-total-expirable-newsgroups ; Check var.
3662              (string-match gnus-total-expirable-newsgroups group)))))
3663
3664 (defun gnus-group-auto-expirable-p (group)
3665   "Check whether GROUP is total-expirable or not."
3666   (let ((params (gnus-info-params (gnus-get-info group))))
3667     (or (memq 'auto-expire params)
3668         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3669         (and gnus-auto-expirable-newsgroups ; Check var.
3670              (string-match gnus-auto-expirable-newsgroups group)))))
3671
3672 (defun gnus-virtual-group-p (group)
3673   "Say whether GROUP is virtual or not."
3674   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3675                         gnus-valid-select-methods)))
3676
3677 (defun gnus-news-group-p (group &optional article)
3678   "Return non-nil if GROUP (and ARTICLE) come from a news server."
3679   (or (gnus-member-of-valid 'post group) ; Ordinary news group.
3680       (and (gnus-member-of-valid 'post-mail group) ; Combined group.
3681            (eq (gnus-request-type group article) 'news))))
3682
3683 (defsubst gnus-simplify-subject-fully (subject)
3684   "Simplify a subject string according to the user's wishes."
3685   (cond
3686    ((null gnus-summary-gather-subject-limit)
3687     (gnus-simplify-subject-re subject))
3688    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3689     (gnus-simplify-subject-fuzzy subject))
3690    ((numberp gnus-summary-gather-subject-limit)
3691     (gnus-limit-string (gnus-simplify-subject-re subject)
3692                        gnus-summary-gather-subject-limit))
3693    (t
3694     subject)))
3695
3696 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3697   "Check whether two subjects are equal.  If optional argument
3698 simple-first is t, first argument is already simplified."
3699   (cond
3700    ((null simple-first)
3701     (equal (gnus-simplify-subject-fully s1)
3702            (gnus-simplify-subject-fully s2)))
3703    (t
3704     (equal s1
3705            (gnus-simplify-subject-fully s2)))))
3706
3707 ;; Returns a list of writable groups.
3708 (defun gnus-writable-groups ()
3709   (let ((alist gnus-newsrc-alist)
3710         groups group)
3711     (while (setq group (car (pop alist)))
3712       (unless (gnus-group-read-only-p group)
3713         (push group groups)))
3714     (nreverse groups)))
3715
3716 (defun gnus-completing-read (default prompt &rest args)
3717   ;; Like `completing-read', except that DEFAULT is the default argument.
3718   (let* ((prompt (if default 
3719                      (concat prompt " (default " default ") ")
3720                    (concat prompt " ")))
3721          (answer (apply 'completing-read prompt args)))
3722     (if (or (null answer) (zerop (length answer)))
3723         default
3724       answer)))
3725
3726 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3727 ;; the echo area.
3728 (defun gnus-y-or-n-p (prompt)
3729   (prog1
3730       (y-or-n-p prompt)
3731     (message "")))
3732
3733 (defun gnus-yes-or-no-p (prompt)
3734   (prog1
3735       (yes-or-no-p prompt)
3736     (message "")))
3737
3738 ;; Check whether to use long file names.
3739 (defun gnus-use-long-file-name (symbol)
3740   ;; The variable has to be set...
3741   (and gnus-use-long-file-name
3742        ;; If it isn't a list, then we return t.
3743        (or (not (listp gnus-use-long-file-name))
3744            ;; If it is a list, and the list contains `symbol', we
3745            ;; return nil.
3746            (not (memq symbol gnus-use-long-file-name)))))
3747
3748 ;; I suspect there's a better way, but I haven't taken the time to do
3749 ;; it yet. -erik selberg@cs.washington.edu
3750 (defun gnus-dd-mmm (messy-date)
3751   "Return a string like DD-MMM from a big messy string"
3752   (let ((datevec (condition-case () (timezone-parse-date messy-date) 
3753                    (error nil))))
3754     (if (not datevec)
3755         "??-???"
3756       (format "%2s-%s"
3757               (condition-case ()
3758                   ;; Make sure leading zeroes are stripped.
3759                   (number-to-string (string-to-number (aref datevec 2)))
3760                 (error "??"))
3761               (capitalize
3762                (or (car
3763                     (nth (1- (string-to-number (aref datevec 1)))
3764                          timezone-months-assoc))
3765                    "???"))))))
3766
3767 (defun gnus-mode-string-quote (string)
3768   "Quote all \"%\" in STRING."
3769   (save-excursion
3770     (gnus-set-work-buffer)
3771     (insert string)
3772     (goto-char (point-min))
3773     (while (search-forward "%" nil t)
3774       (insert "%"))
3775     (buffer-string)))
3776
3777 ;; Make a hash table (default and minimum size is 255).
3778 ;; Optional argument HASHSIZE specifies the table size.
3779 (defun gnus-make-hashtable (&optional hashsize)
3780   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3781
3782 ;; Make a number that is suitable for hashing; bigger than MIN and one
3783 ;; less than 2^x.
3784 (defun gnus-create-hash-size (min)
3785   (let ((i 1))
3786     (while (< i min)
3787       (setq i (* 2 i)))
3788     (1- i)))
3789
3790 ;; Show message if message has a lower level than `gnus-verbose'.
3791 ;; Guideline for numbers:
3792 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3793 ;; for things that take a long time, 7 - not very important messages
3794 ;; on stuff, 9 - messages inside loops.
3795 (defun gnus-message (level &rest args)
3796   (if (<= level gnus-verbose)
3797       (apply 'message args)
3798     ;; We have to do this format thingy here even if the result isn't
3799     ;; shown - the return value has to be the same as the return value
3800     ;; from `message'.
3801     (apply 'format args)))
3802
3803 (defun gnus-error (level &rest args)
3804   "Beep an error if `gnus-verbose' is on LEVEL or less."
3805   (when (<= (floor level) gnus-verbose)
3806     (apply 'message args)
3807     (ding)
3808     (let (duration)
3809       (when (and (floatp level)
3810                  (not (zerop (setq duration (* 10 (- level (floor level)))))))
3811         (sit-for duration))))
3812   nil)
3813
3814 ;; Generate a unique new group name.
3815 (defun gnus-generate-new-group-name (leaf)
3816   (let ((name leaf)
3817         (num 0))
3818     (while (gnus-gethash name gnus-newsrc-hashtb)
3819       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3820     name))
3821
3822 (defsubst gnus-hide-text (b e props)
3823   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
3824   (gnus-add-text-properties b e props)
3825   (when (memq 'intangible props)
3826     (gnus-put-text-property (max (1- b) (point-min))
3827                        b 'intangible (cddr (memq 'intangible props)))))
3828
3829 (defsubst gnus-unhide-text (b e)
3830   "Remove hidden text properties from region between B and E."
3831   (remove-text-properties b e gnus-hidden-properties)
3832   (when (memq 'intangible gnus-hidden-properties)
3833     (gnus-put-text-property (max (1- b) (point-min))
3834                             b 'intangible nil)))
3835
3836 (defun gnus-hide-text-type (b e type)
3837   "Hide text of TYPE between B and E."
3838   (gnus-hide-text b e (cons 'gnus-type (cons type gnus-hidden-properties))))
3839
3840 (defun gnus-parent-headers (headers &optional generation)
3841   "Return the headers of the GENERATIONeth parent of HEADERS."
3842   (unless generation 
3843     (setq generation 1))
3844   (let (references parent)
3845     (while (and headers (not (zerop generation)))
3846       (setq references (mail-header-references headers))
3847       (when (and references
3848                  (setq parent (gnus-parent-id references))
3849                  (setq headers (car (gnus-id-to-thread parent))))
3850         (decf generation)))
3851     headers))
3852
3853 (defun gnus-parent-id (references)
3854   "Return the last Message-ID in REFERENCES."
3855   (when (and references
3856              (string-match "\\(<[^\n<>]+>\\)[ \t\n]*\\'" references))
3857     (substring references (match-beginning 1) (match-end 1))))
3858
3859 (defun gnus-split-references (references)
3860   "Return a list of Message-IDs in REFERENCES."
3861   (let ((beg 0)
3862         ids)
3863     (while (string-match "<[^>]+>" references beg)
3864       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3865             ids))
3866     (nreverse ids)))
3867
3868 (defun gnus-buffer-live-p (buffer)
3869   "Say whether BUFFER is alive or not."
3870   (and buffer
3871        (get-buffer buffer)
3872        (buffer-name (get-buffer buffer))))
3873
3874 (defun gnus-ephemeral-group-p (group)
3875   "Say whether GROUP is ephemeral or not."
3876   (gnus-group-get-parameter group 'quit-config))
3877
3878 (defun gnus-group-quit-config (group)
3879   "Return the quit-config of GROUP."
3880   (gnus-group-get-parameter group 'quit-config))
3881
3882 (defun gnus-simplify-mode-line ()
3883   "Make mode lines a bit simpler."
3884   (setq mode-line-modified "-- ")
3885   (when (listp mode-line-format)
3886     (make-local-variable 'mode-line-format)
3887     (setq mode-line-format (copy-sequence mode-line-format))
3888     (when (equal (nth 3 mode-line-format) "   ")
3889       (setcar (nthcdr 3 mode-line-format) " "))))
3890
3891 ;;; List and range functions
3892
3893 (defun gnus-last-element (list)
3894   "Return last element of LIST."
3895   (while (cdr list)
3896     (setq list (cdr list)))
3897   (car list))
3898
3899 (defun gnus-copy-sequence (list)
3900   "Do a complete, total copy of a list."
3901   (if (and (consp list) (not (consp (cdr list))))
3902       (cons (car list) (cdr list))
3903     (mapcar (lambda (elem) (if (consp elem)
3904                                (if (consp (cdr elem))
3905                                    (gnus-copy-sequence elem)
3906                                  (cons (car elem) (cdr elem)))
3907                              elem))
3908             list)))
3909
3910 (defun gnus-set-difference (list1 list2)
3911   "Return a list of elements of LIST1 that do not appear in LIST2."
3912   (let ((list1 (copy-sequence list1)))
3913     (while list2
3914       (setq list1 (delq (car list2) list1))
3915       (setq list2 (cdr list2)))
3916     list1))
3917
3918 (defun gnus-sorted-complement (list1 list2)
3919   "Return a list of elements of LIST1 that do not appear in LIST2.
3920 Both lists have to be sorted over <."
3921   (let (out)
3922     (if (or (null list1) (null list2))
3923         (or list1 list2)
3924       (while (and list1 list2)
3925         (cond ((= (car list1) (car list2))
3926                (setq list1 (cdr list1)
3927                      list2 (cdr list2)))
3928               ((< (car list1) (car list2))
3929                (setq out (cons (car list1) out))
3930                (setq list1 (cdr list1)))
3931               (t
3932                (setq out (cons (car list2) out))
3933                (setq list2 (cdr list2)))))
3934       (nconc (nreverse out) (or list1 list2)))))
3935
3936 (defun gnus-intersection (list1 list2)
3937   (let ((result nil))
3938     (while list2
3939       (if (memq (car list2) list1)
3940           (setq result (cons (car list2) result)))
3941       (setq list2 (cdr list2)))
3942     result))
3943
3944 (defun gnus-sorted-intersection (list1 list2)
3945   ;; LIST1 and LIST2 have to be sorted over <.
3946   (let (out)
3947     (while (and list1 list2)
3948       (cond ((= (car list1) (car list2))
3949              (setq out (cons (car list1) out)
3950                    list1 (cdr list1)
3951                    list2 (cdr list2)))
3952             ((< (car list1) (car list2))
3953              (setq list1 (cdr list1)))
3954             (t
3955              (setq list2 (cdr list2)))))
3956     (nreverse out)))
3957
3958 (defun gnus-set-sorted-intersection (list1 list2)
3959   ;; LIST1 and LIST2 have to be sorted over <.
3960   ;; This function modifies LIST1.
3961   (let* ((top (cons nil list1))
3962          (prev top))
3963     (while (and list1 list2)
3964       (cond ((= (car list1) (car list2))
3965              (setq prev list1
3966                    list1 (cdr list1)
3967                    list2 (cdr list2)))
3968             ((< (car list1) (car list2))
3969              (setcdr prev (cdr list1))
3970              (setq list1 (cdr list1)))
3971             (t
3972              (setq list2 (cdr list2)))))
3973     (setcdr prev nil)
3974     (cdr top)))
3975
3976 (defun gnus-compress-sequence (numbers &optional always-list)
3977   "Convert list of numbers to a list of ranges or a single range.
3978 If ALWAYS-LIST is non-nil, this function will always release a list of
3979 ranges."
3980   (let* ((first (car numbers))
3981          (last (car numbers))
3982          result)
3983     (if (null numbers)
3984         nil
3985       (if (not (listp (cdr numbers)))
3986           numbers
3987         (while numbers
3988           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3989                 ((= (1+ last) (car numbers)) ;Still in sequence
3990                  (setq last (car numbers)))
3991                 (t                      ;End of one sequence
3992                  (setq result
3993                        (cons (if (= first last) first
3994                                (cons first last)) result))
3995                  (setq first (car numbers))
3996                  (setq last  (car numbers))))
3997           (setq numbers (cdr numbers)))
3998         (if (and (not always-list) (null result))
3999             (if (= first last) (list first) (cons first last))
4000           (nreverse (cons (if (= first last) first (cons first last))
4001                           result)))))))
4002
4003 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
4004 (defun gnus-uncompress-range (ranges)
4005   "Expand a list of ranges into a list of numbers.
4006 RANGES is either a single range on the form `(num . num)' or a list of
4007 these ranges."
4008   (let (first last result)
4009     (cond
4010      ((null ranges)
4011       nil)
4012      ((not (listp (cdr ranges)))
4013       (setq first (car ranges))
4014       (setq last (cdr ranges))
4015       (while (<= first last)
4016         (setq result (cons first result))
4017         (setq first (1+ first)))
4018       (nreverse result))
4019      (t
4020       (while ranges
4021         (if (atom (car ranges))
4022             (if (numberp (car ranges))
4023                 (setq result (cons (car ranges) result)))
4024           (setq first (caar ranges))
4025           (setq last  (cdar ranges))
4026           (while (<= first last)
4027             (setq result (cons first result))
4028             (setq first (1+ first))))
4029         (setq ranges (cdr ranges)))
4030       (nreverse result)))))
4031
4032 (defun gnus-add-to-range (ranges list)
4033   "Return a list of ranges that has all articles from both RANGES and LIST.
4034 Note: LIST has to be sorted over `<'."
4035   (if (not ranges)
4036       (gnus-compress-sequence list t)
4037     (setq list (copy-sequence list))
4038     (or (listp (cdr ranges))
4039         (setq ranges (list ranges)))
4040     (let ((out ranges)
4041           ilist lowest highest temp)
4042       (while (and ranges list)
4043         (setq ilist list)
4044         (setq lowest (or (and (atom (car ranges)) (car ranges))
4045                          (caar ranges)))
4046         (while (and list (cdr list) (< (cadr list) lowest))
4047           (setq list (cdr list)))
4048         (if (< (car ilist) lowest)
4049             (progn
4050               (setq temp list)
4051               (setq list (cdr list))
4052               (setcdr temp nil)
4053               (setq out (nconc (gnus-compress-sequence ilist t) out))))
4054         (setq highest (or (and (atom (car ranges)) (car ranges))
4055                           (cdar ranges)))
4056         (while (and list (<= (car list) highest))
4057           (setq list (cdr list)))
4058         (setq ranges (cdr ranges)))
4059       (if list
4060           (setq out (nconc (gnus-compress-sequence list t) out)))
4061       (setq out (sort out (lambda (r1 r2)
4062                             (< (or (and (atom r1) r1) (car r1))
4063                                (or (and (atom r2) r2) (car r2))))))
4064       (setq ranges out)
4065       (while ranges
4066         (if (atom (car ranges))
4067             (if (cdr ranges)
4068                 (if (atom (cadr ranges))
4069                     (if (= (1+ (car ranges)) (cadr ranges))
4070                         (progn
4071                           (setcar ranges (cons (car ranges)
4072                                                (cadr ranges)))
4073                           (setcdr ranges (cddr ranges))))
4074                   (if (= (1+ (car ranges)) (caadr ranges))
4075                       (progn
4076                         (setcar (cadr ranges) (car ranges))
4077                         (setcar ranges (cadr ranges))
4078                         (setcdr ranges (cddr ranges))))))
4079           (if (cdr ranges)
4080               (if (atom (cadr ranges))
4081                   (if (= (1+ (cdar ranges)) (cadr ranges))
4082                       (progn
4083                         (setcdr (car ranges) (cadr ranges))
4084                         (setcdr ranges (cddr ranges))))
4085                 (if (= (1+ (cdar ranges)) (caadr ranges))
4086                     (progn
4087                       (setcdr (car ranges) (cdadr ranges))
4088                       (setcdr ranges (cddr ranges)))))))
4089         (setq ranges (cdr ranges)))
4090       out)))
4091
4092 (defun gnus-remove-from-range (ranges list)
4093   "Return a list of ranges that has all articles from LIST removed from RANGES.
4094 Note: LIST has to be sorted over `<'."
4095   ;; !!! This function shouldn't look like this, but I've got a headache.
4096   (gnus-compress-sequence
4097    (gnus-sorted-complement
4098     (gnus-uncompress-range ranges) list)))
4099
4100 (defun gnus-member-of-range (number ranges)
4101   (if (not (listp (cdr ranges)))
4102       (and (>= number (car ranges))
4103            (<= number (cdr ranges)))
4104     (let ((not-stop t))
4105       (while (and ranges
4106                   (if (numberp (car ranges))
4107                       (>= number (car ranges))
4108                     (>= number (caar ranges)))
4109                   not-stop)
4110         (if (if (numberp (car ranges))
4111                 (= number (car ranges))
4112               (and (>= number (caar ranges))
4113                    (<= number (cdar ranges))))
4114             (setq not-stop nil))
4115         (setq ranges (cdr ranges)))
4116       (not not-stop))))
4117
4118 (defun gnus-range-length (range)
4119   "Return the length RANGE would have if uncompressed."
4120   (length (gnus-uncompress-range range)))
4121
4122 (defun gnus-sublist-p (list sublist)
4123   "Test whether all elements in SUBLIST are members of LIST."
4124   (let ((sublistp t))
4125     (while sublist
4126       (unless (memq (pop sublist) list)
4127         (setq sublistp nil
4128               sublist nil)))
4129     sublistp))
4130
4131 \f
4132 ;;;
4133 ;;; Gnus group mode
4134 ;;;
4135
4136 (defvar gnus-group-mode-map nil)
4137 (put 'gnus-group-mode 'mode-class 'special)
4138
4139 (unless gnus-group-mode-map
4140   (setq gnus-group-mode-map (make-keymap))
4141   (suppress-keymap gnus-group-mode-map)
4142
4143   (gnus-define-keys gnus-group-mode-map
4144     " " gnus-group-read-group
4145     "=" gnus-group-select-group
4146     "\r" gnus-group-select-group
4147     "\M-\r" gnus-group-quick-select-group
4148     "j" gnus-group-jump-to-group
4149     "n" gnus-group-next-unread-group
4150     "p" gnus-group-prev-unread-group
4151     "\177" gnus-group-prev-unread-group
4152     [delete] gnus-group-prev-unread-group
4153     "N" gnus-group-next-group
4154     "P" gnus-group-prev-group
4155     "\M-n" gnus-group-next-unread-group-same-level
4156     "\M-p" gnus-group-prev-unread-group-same-level
4157     "," gnus-group-best-unread-group
4158     "." gnus-group-first-unread-group
4159     "u" gnus-group-unsubscribe-current-group
4160     "U" gnus-group-unsubscribe-group
4161     "c" gnus-group-catchup-current
4162     "C" gnus-group-catchup-current-all
4163     "l" gnus-group-list-groups
4164     "L" gnus-group-list-all-groups
4165     "m" gnus-group-mail
4166     "g" gnus-group-get-new-news
4167     "\M-g" gnus-group-get-new-news-this-group
4168     "R" gnus-group-restart
4169     "r" gnus-group-read-init-file
4170     "B" gnus-group-browse-foreign-server
4171     "b" gnus-group-check-bogus-groups
4172     "F" gnus-find-new-newsgroups
4173     "\C-c\C-d" gnus-group-describe-group
4174     "\M-d" gnus-group-describe-all-groups
4175     "\C-c\C-a" gnus-group-apropos
4176     "\C-c\M-\C-a" gnus-group-description-apropos
4177     "a" gnus-group-post-news
4178     "\ek" gnus-group-edit-local-kill
4179     "\eK" gnus-group-edit-global-kill
4180     "\C-k" gnus-group-kill-group
4181     "\C-y" gnus-group-yank-group
4182     "\C-w" gnus-group-kill-region
4183     "\C-x\C-t" gnus-group-transpose-groups
4184     "\C-c\C-l" gnus-group-list-killed
4185     "\C-c\C-x" gnus-group-expire-articles
4186     "\C-c\M-\C-x" gnus-group-expire-all-groups
4187     "V" gnus-version
4188     "s" gnus-group-save-newsrc
4189     "z" gnus-group-suspend
4190     "Z" gnus-group-clear-dribble
4191     "q" gnus-group-exit
4192     "Q" gnus-group-quit
4193     "?" gnus-group-describe-briefly
4194     "\C-c\C-i" gnus-info-find-node
4195     "\M-e" gnus-group-edit-group-method
4196     "^" gnus-group-enter-server-mode
4197     gnus-mouse-2 gnus-mouse-pick-group
4198     "<" beginning-of-buffer
4199     ">" end-of-buffer
4200     "\C-c\C-b" gnus-bug
4201     "\C-c\C-s" gnus-group-sort-groups
4202     "t" gnus-topic-mode
4203     "\C-c\M-g" gnus-activate-all-groups
4204     "\M-&" gnus-group-universal-argument
4205     "#" gnus-group-mark-group
4206     "\M-#" gnus-group-unmark-group)
4207
4208   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4209     "m" gnus-group-mark-group
4210     "u" gnus-group-unmark-group
4211     "w" gnus-group-mark-region
4212     "m" gnus-group-mark-buffer
4213     "r" gnus-group-mark-regexp
4214     "U" gnus-group-unmark-all-groups)
4215
4216   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4217     "d" gnus-group-make-directory-group
4218     "h" gnus-group-make-help-group
4219     "a" gnus-group-make-archive-group
4220     "k" gnus-group-make-kiboze-group
4221     "m" gnus-group-make-group
4222     "E" gnus-group-edit-group
4223     "e" gnus-group-edit-group-method
4224     "p" gnus-group-edit-group-parameters
4225     "v" gnus-group-add-to-virtual
4226     "V" gnus-group-make-empty-virtual
4227     "D" gnus-group-enter-directory
4228     "f" gnus-group-make-doc-group
4229     "r" gnus-group-rename-group
4230     "\177" gnus-group-delete-group
4231     [delete] gnus-group-delete-group)
4232
4233    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4234      "b" gnus-group-brew-soup
4235      "w" gnus-soup-save-areas
4236      "s" gnus-soup-send-replies
4237      "p" gnus-soup-pack-packet
4238      "r" nnsoup-pack-replies)
4239
4240    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4241      "s" gnus-group-sort-groups
4242      "a" gnus-group-sort-groups-by-alphabet
4243      "u" gnus-group-sort-groups-by-unread
4244      "l" gnus-group-sort-groups-by-level
4245      "v" gnus-group-sort-groups-by-score
4246      "r" gnus-group-sort-groups-by-rank
4247      "m" gnus-group-sort-groups-by-method)
4248
4249    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4250      "k" gnus-group-list-killed
4251      "z" gnus-group-list-zombies
4252      "s" gnus-group-list-groups
4253      "u" gnus-group-list-all-groups
4254      "A" gnus-group-list-active
4255      "a" gnus-group-apropos
4256      "d" gnus-group-description-apropos
4257      "m" gnus-group-list-matching
4258      "M" gnus-group-list-all-matching
4259      "l" gnus-group-list-level)
4260
4261    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4262      "f" gnus-score-flush-cache)
4263
4264    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4265      "f" gnus-group-fetch-faq)
4266
4267    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4268      "l" gnus-group-set-current-level
4269      "t" gnus-group-unsubscribe-current-group
4270      "s" gnus-group-unsubscribe-group
4271      "k" gnus-group-kill-group
4272      "y" gnus-group-yank-group
4273      "w" gnus-group-kill-region
4274      "\C-k" gnus-group-kill-level
4275      "z" gnus-group-kill-all-zombies))
4276
4277 (defun gnus-group-mode ()
4278   "Major mode for reading news.
4279
4280 All normal editing commands are switched off.
4281 \\<gnus-group-mode-map>
4282 The group buffer lists (some of) the groups available.  For instance,
4283 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4284 lists all zombie groups.
4285
4286 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4287 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4288
4289 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4290
4291 The following commands are available:
4292
4293 \\{gnus-group-mode-map}"
4294   (interactive)
4295   (when (and menu-bar-mode
4296              (gnus-visual-p 'group-menu 'menu))
4297     (gnus-group-make-menu-bar))
4298   (kill-all-local-variables)
4299   (gnus-simplify-mode-line)
4300   (setq major-mode 'gnus-group-mode)
4301   (setq mode-name "Group")
4302   (gnus-group-set-mode-line)
4303   (setq mode-line-process nil)
4304   (use-local-map gnus-group-mode-map)
4305   (buffer-disable-undo (current-buffer))
4306   (setq truncate-lines t)
4307   (setq buffer-read-only t)
4308   (gnus-make-local-hook 'post-command-hook)
4309   (gnus-add-hook 'post-command-hook 'gnus-clear-inboxes-moved nil t)
4310   (run-hooks 'gnus-group-mode-hook))
4311
4312 (defun gnus-clear-inboxes-moved ()
4313   (setq nnmail-moved-inboxes nil))
4314
4315 (defun gnus-mouse-pick-group (e)
4316   "Enter the group under the mouse pointer."
4317   (interactive "e")
4318   (mouse-set-point e)
4319   (gnus-group-read-group nil))
4320
4321 ;; Look at LEVEL and find out what the level is really supposed to be.
4322 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4323 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4324 (defun gnus-group-default-level (&optional level number-or-nil)
4325   (cond
4326    (gnus-group-use-permanent-levels
4327     (or (setq gnus-group-use-permanent-levels
4328               (or level (if (numberp gnus-group-use-permanent-levels)
4329                             gnus-group-use-permanent-levels
4330                           (or gnus-group-default-list-level
4331                               gnus-level-subscribed))))
4332         gnus-group-default-list-level gnus-level-subscribed))
4333    (number-or-nil
4334     level)
4335    (t
4336     (or level gnus-group-default-list-level gnus-level-subscribed))))
4337
4338 ;;;###autoload
4339 (defun gnus-slave-no-server (&optional arg)
4340   "Read network news as a slave, without connecting to local server"
4341   (interactive "P")
4342   (gnus-no-server arg t))
4343
4344 ;;;###autoload
4345 (defun gnus-no-server (&optional arg slave)
4346   "Read network news.
4347 If ARG is a positive number, Gnus will use that as the
4348 startup level.  If ARG is nil, Gnus will be started at level 2.
4349 If ARG is non-nil and not a positive number, Gnus will
4350 prompt the user for the name of an NNTP server to use.
4351 As opposed to `gnus', this command will not connect to the local server."
4352   (interactive "P")
4353   (let ((val (or arg (1- gnus-level-default-subscribed))))
4354     (gnus val t slave)
4355     (make-local-variable 'gnus-group-use-permanent-levels)
4356     (setq gnus-group-use-permanent-levels val)))
4357
4358 ;;;###autoload
4359 (defun gnus-slave (&optional arg)
4360   "Read news as a slave."
4361   (interactive "P")
4362   (gnus arg nil 'slave))
4363
4364 ;;;###autoload
4365 (defun gnus-other-frame (&optional arg)
4366   "Pop up a frame to read news."
4367   (interactive "P")
4368   (if (get-buffer gnus-group-buffer)
4369       (let ((pop-up-frames t))
4370         (gnus arg))
4371     (select-frame (make-frame))
4372     (gnus arg)))
4373
4374 ;;;###autoload
4375 (defun gnus (&optional arg dont-connect slave)
4376   "Read network news.
4377 If ARG is non-nil and a positive number, Gnus will use that as the
4378 startup level.  If ARG is non-nil and not a positive number, Gnus will
4379 prompt the user for the name of an NNTP server to use."
4380   (interactive "P")
4381
4382   (if (get-buffer gnus-group-buffer)
4383       (progn
4384         (switch-to-buffer gnus-group-buffer)
4385         (gnus-group-get-new-news))
4386
4387     (gnus-clear-system)
4388     (nnheader-init-server-buffer)
4389     (gnus-read-init-file)
4390     (setq gnus-slave slave)
4391
4392     (gnus-group-setup-buffer)
4393     (let ((buffer-read-only nil))
4394       (erase-buffer)
4395       (if (not gnus-inhibit-startup-message)
4396           (progn
4397             (gnus-group-startup-message)
4398             (sit-for 0))))
4399
4400     (let ((level (and (numberp arg) (> arg 0) arg))
4401           did-connect)
4402       (unwind-protect
4403           (progn
4404             (or dont-connect
4405                 (setq did-connect
4406                       (gnus-start-news-server (and arg (not level))))))
4407         (if (and (not dont-connect)
4408                  (not did-connect))
4409             (gnus-group-quit)
4410           (run-hooks 'gnus-startup-hook)
4411           ;; NNTP server is successfully open.
4412
4413           ;; Find the current startup file name.
4414           (setq gnus-current-startup-file
4415                 (gnus-make-newsrc-file gnus-startup-file))
4416
4417           ;; Read the dribble file.
4418           (when (or gnus-slave gnus-use-dribble-file)
4419             (gnus-dribble-read-file))
4420
4421           ;; Allow using GroupLens predictions.
4422           (when gnus-use-grouplens
4423             (bbb-login)
4424             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4425
4426           (gnus-summary-make-display-table)
4427           ;; Do the actual startup.
4428           (gnus-setup-news nil level dont-connect)
4429           ;; Generate the group buffer.
4430           (gnus-group-list-groups level)
4431           (gnus-group-first-unread-group)
4432           (gnus-configure-windows 'group)
4433           (gnus-group-set-mode-line))))))
4434
4435 (defun gnus-unload ()
4436   "Unload all Gnus features."
4437   (interactive)
4438   (or (boundp 'load-history)
4439       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4440   (let ((history load-history)
4441         feature)
4442     (while history
4443       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4444            (setq feature (cdr (assq 'provide (car history))))
4445            (unload-feature feature 'force))
4446       (setq history (cdr history)))))
4447
4448 (defun gnus-compile ()
4449   "Byte-compile the user-defined format specs."
4450   (interactive)
4451   (let ((entries gnus-format-specs)
4452         entry gnus-tmp-func)
4453     (save-excursion
4454       (gnus-message 7 "Compiling format specs...")
4455
4456       (while entries
4457         (setq entry (pop entries))
4458         (if (eq (car entry) 'version)
4459             (setq gnus-format-specs (delq entry gnus-format-specs))
4460           (when (and (listp (caddr entry))
4461                      (not (eq 'byte-code (caaddr entry))))
4462             (fset 'gnus-tmp-func
4463                   `(lambda () ,(caddr entry)))
4464             (byte-compile 'gnus-tmp-func)
4465             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4466
4467       (push (cons 'version emacs-version) gnus-format-specs)
4468
4469       (gnus-message 7 "Compiling user specs...done"))))
4470
4471 (defun gnus-indent-rigidly (start end arg)
4472   "Indent rigidly using only spaces and no tabs."
4473   (save-excursion
4474     (save-restriction
4475       (narrow-to-region start end)
4476       (indent-rigidly start end arg)
4477       (goto-char (point-min))
4478       (while (search-forward "\t" nil t)
4479         (replace-match "        " t t)))))
4480
4481 (defun gnus-group-startup-message (&optional x y)
4482   "Insert startup message in current buffer."
4483   ;; Insert the message.
4484   (erase-buffer)
4485   (insert
4486    (format "              %s
4487           _    ___ _             _
4488           _ ___ __ ___  __    _ ___
4489           __   _     ___    __  ___
4490               _           ___     _
4491              _  _ __             _
4492              ___   __            _
4493                    __           _
4494                     _      _   _
4495                    _      _    _
4496                       _  _    _
4497                   __  ___
4498                  _   _ _     _
4499                 _   _
4500               _    _
4501              _    _
4502             _
4503           __
4504
4505 "
4506            ""))
4507   ;; And then hack it.
4508   (gnus-indent-rigidly (point-min) (point-max)
4509                        (/ (max (- (window-width) (or x 46)) 0) 2))
4510   (goto-char (point-min))
4511   (forward-line 1)
4512   (let* ((pheight (count-lines (point-min) (point-max)))
4513          (wheight (window-height))
4514          (rest (- wheight pheight)))
4515     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4516   ;; Fontify some.
4517   (goto-char (point-min))
4518   (and (search-forward "Praxis" nil t)
4519        (gnus-put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4520   (goto-char (point-min))
4521   (let* ((mode-string (gnus-group-set-mode-line)))
4522     (setq mode-line-buffer-identification
4523           (list (concat gnus-version (substring (car mode-string) 4))))
4524     (set-buffer-modified-p t)))
4525
4526 (defun gnus-group-setup-buffer ()
4527   (or (get-buffer gnus-group-buffer)
4528       (progn
4529         (switch-to-buffer gnus-group-buffer)
4530         (gnus-add-current-to-buffer-list)
4531         (gnus-group-mode)
4532         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4533
4534 (defun gnus-group-list-groups (&optional level unread lowest)
4535   "List newsgroups with level LEVEL or lower that have unread articles.
4536 Default is all subscribed groups.
4537 If argument UNREAD is non-nil, groups with no unread articles are also
4538 listed."
4539   (interactive (list (if current-prefix-arg
4540                          (prefix-numeric-value current-prefix-arg)
4541                        (or
4542                         (gnus-group-default-level nil t)
4543                         gnus-group-default-list-level
4544                         gnus-level-subscribed))))
4545   (or level
4546       (setq level (car gnus-group-list-mode)
4547             unread (cdr gnus-group-list-mode)))
4548   (setq level (gnus-group-default-level level))
4549   (gnus-group-setup-buffer)             ;May call from out of group buffer
4550   (gnus-update-format-specifications)
4551   (let ((case-fold-search nil)
4552         (props (text-properties-at (gnus-point-at-bol)))
4553         (group (gnus-group-group-name)))
4554     (set-buffer gnus-group-buffer)
4555     (funcall gnus-group-prepare-function level unread lowest)
4556     (if (zerop (buffer-size))
4557         (gnus-message 5 gnus-no-groups-message)
4558       (goto-char (point-max))
4559       (when (or (not gnus-group-goto-next-group-function)
4560                 (not (funcall gnus-group-goto-next-group-function 
4561                               group props)))
4562         (if (not group)
4563             ;; Go to the first group with unread articles.
4564             (gnus-group-search-forward t)
4565           ;; Find the right group to put point on.  If the current group
4566           ;; has disappeared in the new listing, try to find the next
4567           ;; one.        If no next one can be found, just leave point at the
4568           ;; first newsgroup in the buffer.
4569           (if (not (gnus-goto-char
4570                     (text-property-any
4571                      (point-min) (point-max)
4572                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4573               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4574                 (while (and newsrc
4575                             (not (gnus-goto-char
4576                                   (text-property-any
4577                                    (point-min) (point-max) 'gnus-group
4578                                    (gnus-intern-safe
4579                                     (caar newsrc) gnus-active-hashtb)))))
4580                   (setq newsrc (cdr newsrc)))
4581                 (or newsrc (progn (goto-char (point-max))
4582                                   (forward-line -1)))))))
4583       ;; Adjust cursor point.
4584       (gnus-group-position-point))))
4585
4586 (defun gnus-group-list-level (level &optional all)
4587   "List groups on LEVEL.
4588 If ALL (the prefix), also list groups that have no unread articles."
4589   (interactive "nList groups on level: \nP")
4590   (gnus-group-list-groups level all level))
4591
4592 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4593   "List all newsgroups with unread articles of level LEVEL or lower.
4594 If ALL is non-nil, list groups that have no unread articles.
4595 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4596 If REGEXP, only list groups matching REGEXP."
4597   (set-buffer gnus-group-buffer)
4598   (let ((buffer-read-only nil)
4599         (newsrc (cdr gnus-newsrc-alist))
4600         (lowest (or lowest 1))
4601         info clevel unread group params)
4602     (erase-buffer)
4603     (if (< lowest gnus-level-zombie)
4604         ;; List living groups.
4605         (while newsrc
4606           (setq info (car newsrc)
4607                 group (gnus-info-group info)
4608                 params (gnus-info-params info)
4609                 newsrc (cdr newsrc)
4610                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4611           (and unread                   ; This group might be bogus
4612                (or (not regexp)
4613                    (string-match regexp group))
4614                (<= (setq clevel (gnus-info-level info)) level)
4615                (>= clevel lowest)
4616                (or all                  ; We list all groups?
4617                    (if (eq unread t)    ; Unactivated?
4618                        gnus-group-list-inactive-groups ; We list unactivated 
4619                      (> unread 0))      ; We list groups with unread articles
4620                    (and gnus-list-groups-with-ticked-articles
4621                         (cdr (assq 'tick (gnus-info-marks info))))
4622                                         ; And groups with tickeds
4623                    ;; Check for permanent visibility.
4624                    (and gnus-permanently-visible-groups
4625                         (string-match gnus-permanently-visible-groups
4626                                       group))
4627                    (memq 'visible params)
4628                    (cdr (assq 'visible params)))
4629                (gnus-group-insert-group-line
4630                 group (gnus-info-level info)
4631                 (gnus-info-marks info) unread (gnus-info-method info)))))
4632
4633     ;; List dead groups.
4634     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4635          (gnus-group-prepare-flat-list-dead
4636           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4637           gnus-level-zombie ?Z
4638           regexp))
4639     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4640          (gnus-group-prepare-flat-list-dead
4641           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4642           gnus-level-killed ?K regexp))
4643
4644     (gnus-group-set-mode-line)
4645     (setq gnus-group-list-mode (cons level all))
4646     (run-hooks 'gnus-group-prepare-hook)))
4647
4648 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4649   ;; List zombies and killed lists somewhat faster, which was
4650   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4651   ;; this by ignoring the group format specification altogether.
4652   (let (group)
4653     (if regexp
4654         ;; This loop is used when listing groups that match some
4655         ;; regexp.
4656         (while groups
4657           (setq group (pop groups))
4658           (when (string-match regexp group)
4659             (gnus-add-text-properties
4660              (point) (prog1 (1+ (point))
4661                        (insert " " mark "     *: " group "\n"))
4662              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4663                    'gnus-unread t
4664                    'gnus-level level))))
4665       ;; This loop is used when listing all groups.
4666       (while groups
4667         (gnus-add-text-properties
4668          (point) (prog1 (1+ (point))
4669                    (insert " " mark "     *: "
4670                            (setq group (pop groups)) "\n"))
4671          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4672                'gnus-unread t
4673                'gnus-level level))))))
4674
4675 (defmacro gnus-group-real-name (group)
4676   "Find the real name of a foreign newsgroup."
4677   `(let ((gname ,group))
4678      (if (string-match ":[^:]+$" gname)
4679          (substring gname (1+ (match-beginning 0)))
4680        gname)))
4681
4682 (defsubst gnus-server-add-address (method)
4683   (let ((method-name (symbol-name (car method))))
4684     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4685              (not (assq (intern (concat method-name "-address")) method)))
4686         (append method (list (list (intern (concat method-name "-address"))
4687                                    (nth 1 method))))
4688       method)))
4689
4690 (defsubst gnus-server-get-method (group method)
4691   ;; Input either a server name, and extended server name, or a
4692   ;; select method, and return a select method.
4693   (cond ((stringp method)
4694          (gnus-server-to-method method))
4695         ((equal method gnus-select-method)
4696          gnus-select-method)
4697         ((and (stringp (car method)) group)
4698          (gnus-server-extend-method group method))
4699         ((and method (not group)
4700               (equal (cadr method) ""))
4701          method)
4702         (t
4703          (gnus-server-add-address method))))
4704
4705 (defun gnus-server-to-method (server)
4706   "Map virtual server names to select methods."
4707   (or 
4708    ;; Is this a method, perhaps?
4709    (and server (listp server) server)
4710    ;; Perhaps this is the native server?
4711    (and (equal server "native") gnus-select-method)
4712    ;; It should be in the server alist.
4713    (cdr (assoc server gnus-server-alist))
4714    ;; If not, we look through all the opened server
4715    ;; to see whether we can find it there.
4716    (let ((opened gnus-opened-servers))
4717      (while (and opened
4718                  (not (equal server (format "%s:%s" (caaar opened)
4719                                             (cadaar opened)))))
4720        (pop opened))
4721      (caar opened))))
4722
4723 (defmacro gnus-method-equal (ss1 ss2)
4724   "Say whether two servers are equal."
4725   `(let ((s1 ,ss1)
4726          (s2 ,ss2))
4727      (or (equal s1 s2)
4728          (and (= (length s1) (length s2))
4729               (progn
4730                 (while (and s1 (member (car s1) s2))
4731                   (setq s1 (cdr s1)))
4732                 (null s1))))))
4733
4734 (defun gnus-server-equal (m1 m2)
4735   "Say whether two methods are equal."
4736   (let ((m1 (cond ((null m1) gnus-select-method)
4737                   ((stringp m1) (gnus-server-to-method m1))
4738                   (t m1)))
4739         (m2 (cond ((null m2) gnus-select-method)
4740                   ((stringp m2) (gnus-server-to-method m2))
4741                   (t m2))))
4742     (gnus-method-equal m1 m2)))
4743
4744 (defun gnus-servers-using-backend (backend)
4745   "Return a list of known servers using BACKEND."
4746   (let ((opened gnus-opened-servers)
4747         out)
4748     (while opened
4749       (when (eq backend (caaar opened))
4750         (push (caar opened) out))
4751       (pop opened))
4752     out))
4753
4754 (defun gnus-group-prefixed-name (group method)
4755   "Return the whole name from GROUP and METHOD."
4756   (and (stringp method) (setq method (gnus-server-to-method method)))
4757   (concat (format "%s" (car method))
4758           (if (and
4759                (or (assoc (format "%s" (car method)) 
4760                           (gnus-methods-using 'address))
4761                    (gnus-server-equal method gnus-message-archive-method))
4762                (nth 1 method)
4763                (not (string= (nth 1 method) "")))
4764               (concat "+" (nth 1 method)))
4765           ":" group))
4766
4767 (defun gnus-group-real-prefix (group)
4768   "Return the prefix of the current group name."
4769   (if (string-match "^[^:]+:" group)
4770       (substring group 0 (match-end 0))
4771     ""))
4772
4773 (defun gnus-group-method (group)
4774   "Return the server or method used for selecting GROUP."
4775   (let ((prefix (gnus-group-real-prefix group)))
4776     (if (equal prefix "")
4777         gnus-select-method
4778       (let ((servers gnus-opened-servers)
4779             (server "")
4780             backend possible found)
4781         (if (string-match "^[^\\+]+\\+" prefix)
4782             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4783                   server (substring prefix (match-end 0) (1- (length prefix))))
4784           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4785         (while servers
4786           (when (eq (caaar servers) backend)
4787             (setq possible (caar servers))
4788             (when (equal (cadaar servers) server)
4789               (setq found (caar servers))))
4790           (pop servers))
4791         (or (car (rassoc found gnus-server-alist))
4792             found
4793             (car (rassoc possible gnus-server-alist))
4794             possible
4795             (list backend server))))))
4796
4797 (defsubst gnus-secondary-method-p (method)
4798   "Return whether METHOD is a secondary select method."
4799   (let ((methods gnus-secondary-select-methods)
4800         (gmethod (gnus-server-get-method nil method)))
4801     (while (and methods
4802                 (not (equal (gnus-server-get-method nil (car methods))
4803                             gmethod)))
4804       (setq methods (cdr methods)))
4805     methods))
4806
4807 (defun gnus-group-foreign-p (group)
4808   "Say whether a group is foreign or not."
4809   (and (not (gnus-group-native-p group))
4810        (not (gnus-group-secondary-p group))))
4811
4812 (defun gnus-group-native-p (group)
4813   "Say whether the group is native or not."
4814   (not (string-match ":" group)))
4815
4816 (defun gnus-group-secondary-p (group)
4817   "Say whether the group is secondary or not."
4818   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4819
4820 (defun gnus-group-get-parameter (group &optional symbol)
4821   "Returns the group parameters for GROUP.
4822 If SYMBOL, return the value of that symbol in the group parameters."
4823   (let ((params (gnus-info-params (gnus-get-info group))))
4824     (if symbol
4825         (gnus-group-parameter-value params symbol)
4826       params)))
4827
4828 (defun gnus-group-parameter-value (params symbol)
4829   "Return the value of SYMBOL in group PARAMS."
4830   (or (car (memq symbol params))        ; It's either a simple symbol
4831       (cdr (assq symbol params))))      ; or a cons.
4832
4833 (defun gnus-group-add-parameter (group param)
4834   "Add parameter PARAM to GROUP."
4835   (let ((info (gnus-get-info group)))
4836     (if (not info)
4837         () ; This is a dead group.  We just ignore it.
4838       ;; Cons the new param to the old one and update.
4839       (gnus-group-set-info (cons param (gnus-info-params info))
4840                            group 'params))))
4841
4842 (defun gnus-group-set-parameter (group name value)
4843   "Set parameter NAME to VALUE in GROUP."
4844   (let ((info (gnus-get-info group)))
4845     (if (not info)
4846         () ; This is a dead group.  We just ignore it.
4847       (let ((old-params (gnus-info-params info))
4848             (new-params (list (cons name value))))
4849         (while old-params
4850           (if (or (not (listp (car old-params)))
4851                   (not (eq (caar old-params) name)))
4852               (setq new-params (append new-params (list (car old-params)))))
4853           (setq old-params (cdr old-params)))
4854         (gnus-group-set-info new-params group 'params)))))
4855
4856 (defun gnus-group-add-score (group &optional score)
4857   "Add SCORE to the GROUP score.
4858 If SCORE is nil, add 1 to the score of GROUP."
4859   (let ((info (gnus-get-info group)))
4860     (when info
4861       (gnus-info-set-score info (+ (gnus-info-score info) (or score 1))))))
4862
4863 (defun gnus-summary-bubble-group ()
4864   "Increase the score of the current group.
4865 This is a handy function to add to `gnus-summary-exit-hook' to
4866 increase the score of each group you read."
4867   (gnus-group-add-score gnus-newsgroup-name))
4868
4869 (defun gnus-group-set-info (info &optional method-only-group part)
4870   (let* ((entry (gnus-gethash
4871                  (or method-only-group (gnus-info-group info))
4872                  gnus-newsrc-hashtb))
4873          (part-info info)
4874          (info (if method-only-group (nth 2 entry) info))
4875          method)
4876     (when method-only-group
4877       (unless entry
4878         (error "Trying to change non-existent group %s" method-only-group))
4879       ;; We have received parts of the actual group info - either the
4880       ;; select method or the group parameters.  We first check
4881       ;; whether we have to extend the info, and if so, do that.
4882       (let ((len (length info))
4883             (total (if (eq part 'method) 5 6)))
4884         (when (< len total)
4885           (setcdr (nthcdr (1- len) info)
4886                   (make-list (- total len) nil)))
4887         ;; Then we enter the new info.
4888         (setcar (nthcdr (1- total) info) part-info)))
4889     (unless entry
4890       ;; This is a new group, so we just create it.
4891       (save-excursion
4892         (set-buffer gnus-group-buffer)
4893         (setq method (gnus-info-method info))
4894         (when (gnus-server-equal method "native")
4895           (setq method nil))
4896         (save-excursion
4897           (set-buffer gnus-group-buffer)
4898           (if method
4899               ;; It's a foreign group...
4900               (gnus-group-make-group
4901                (gnus-group-real-name (gnus-info-group info))
4902                (if (stringp method) method
4903                  (prin1-to-string (car method)))
4904                (and (consp method)
4905                     (nth 1 (gnus-info-method info))))
4906             ;; It's a native group.
4907             (gnus-group-make-group (gnus-info-group info))))
4908         (gnus-message 6 "Note: New group created")
4909         (setq entry
4910               (gnus-gethash (gnus-group-prefixed-name
4911                              (gnus-group-real-name (gnus-info-group info))
4912                              (or (gnus-info-method info) gnus-select-method))
4913                             gnus-newsrc-hashtb))))
4914     ;; Whether it was a new group or not, we now have the entry, so we
4915     ;; can do the update.
4916     (if entry
4917         (progn
4918           (setcar (nthcdr 2 entry) info)
4919           (when (and (not (eq (car entry) t))
4920                      (gnus-active (gnus-info-group info)))
4921             (setcar entry (length (gnus-list-of-unread-articles (car info))))))
4922       (error "No such group: %s" (gnus-info-group info)))))
4923
4924 (defun gnus-group-set-method-info (group select-method)
4925   (gnus-group-set-info select-method group 'method))
4926
4927 (defun gnus-group-set-params-info (group params)
4928   (gnus-group-set-info params group 'params))
4929
4930 (defun gnus-group-update-group-line ()
4931   "Update the current line in the group buffer."
4932   (let* ((buffer-read-only nil)
4933          (group (gnus-group-group-name))
4934          (entry (and group (gnus-gethash group gnus-newsrc-hashtb)))
4935          gnus-group-indentation)
4936     (when group
4937       (and entry
4938            (not (gnus-ephemeral-group-p group))
4939            (gnus-dribble-enter
4940             (concat "(gnus-group-set-info '"
4941                     (prin1-to-string (nth 2 entry)) ")")))
4942       (setq gnus-group-indentation (gnus-group-group-indentation))
4943       (gnus-delete-line)
4944       (gnus-group-insert-group-line-info group)
4945       (forward-line -1)
4946       (gnus-group-position-point))))
4947
4948 (defun gnus-group-insert-group-line-info (group)
4949   "Insert GROUP on the current line."
4950   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4951         active info)
4952     (if entry
4953         (progn
4954           ;; (Un)subscribed group.
4955           (setq info (nth 2 entry))
4956           (gnus-group-insert-group-line
4957            group (gnus-info-level info) (gnus-info-marks info)
4958            (or (car entry) t) (gnus-info-method info)))
4959       ;; This group is dead.
4960       (gnus-group-insert-group-line
4961        group
4962        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4963        nil
4964        (if (setq active (gnus-active group))
4965            (- (1+ (cdr active)) (car active)) 0)
4966        nil))))
4967
4968 (defun gnus-group-insert-group-line (gnus-tmp-group gnus-tmp-level 
4969                                                     gnus-tmp-marked number
4970                                                     gnus-tmp-method)
4971   "Insert a group line in the group buffer."
4972   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4973          (gnus-tmp-number-total
4974           (if gnus-tmp-active
4975               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4976             0))
4977          (gnus-tmp-number-of-unread
4978           (if (numberp number) (int-to-string (max 0 number))
4979             "*"))
4980          (gnus-tmp-number-of-read
4981           (if (numberp number)
4982               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4983             "*"))
4984          (gnus-tmp-subscribed
4985           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4986                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4987                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4988                 (t ?K)))
4989          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4990          (gnus-tmp-newsgroup-description
4991           (if gnus-description-hashtb
4992               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4993             ""))
4994          (gnus-tmp-moderated
4995           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4996          (gnus-tmp-moderated-string
4997           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4998          (gnus-tmp-method
4999           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
5000          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
5001          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
5002          (gnus-tmp-news-method-string
5003           (if gnus-tmp-method
5004               (format "(%s:%s)" (car gnus-tmp-method)
5005                       (cadr gnus-tmp-method)) ""))
5006          (gnus-tmp-marked-mark
5007           (if (and (numberp number)
5008                    (zerop number)
5009                    (cdr (assq 'tick gnus-tmp-marked)))
5010               ?* ? ))
5011          (gnus-tmp-process-marked
5012           (if (member gnus-tmp-group gnus-group-marked)
5013               gnus-process-mark ? ))
5014          (gnus-tmp-grouplens
5015           (or (and gnus-use-grouplens
5016                    (bbb-grouplens-group-p gnus-tmp-group))
5017               ""))
5018          (buffer-read-only nil)
5019          header gnus-tmp-header)        ; passed as parameter to user-funcs.
5020     (beginning-of-line)
5021     (gnus-add-text-properties
5022      (point)
5023      (prog1 (1+ (point))
5024        ;; Insert the text.
5025        (eval gnus-group-line-format-spec))
5026      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
5027        gnus-unread ,(if (numberp number)
5028                         (string-to-int gnus-tmp-number-of-unread)
5029                       t)
5030        gnus-marked ,gnus-tmp-marked-mark
5031        gnus-indentation ,gnus-group-indentation
5032        gnus-level ,gnus-tmp-level))
5033     (when (inline (gnus-visual-p 'group-highlight 'highlight))
5034       (forward-line -1)
5035       (run-hooks 'gnus-group-update-hook)
5036       (forward-line))
5037     ;; Allow XEmacs to remove front-sticky text properties.
5038     (gnus-group-remove-excess-properties)))
5039
5040 (defun gnus-group-update-group (group &optional visible-only)
5041   "Update all lines where GROUP appear.
5042 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
5043 already."
5044   (save-excursion
5045     (set-buffer gnus-group-buffer)
5046     ;; The buffer may be narrowed.
5047     (save-restriction
5048       (widen)
5049       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
5050             (loc (point-min))
5051             found buffer-read-only)
5052         ;; Enter the current status into the dribble buffer.
5053         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
5054           (if (and entry (not (gnus-ephemeral-group-p group)))
5055               (gnus-dribble-enter
5056                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
5057                        ")"))))
5058         ;; Find all group instances.  If topics are in use, each group
5059         ;; may be listed in more than once.
5060         (while (setq loc (text-property-any
5061                           loc (point-max) 'gnus-group ident))
5062           (setq found t)
5063           (goto-char loc)
5064           (let ((gnus-group-indentation (gnus-group-group-indentation)))
5065             (gnus-delete-line)
5066             (gnus-group-insert-group-line-info group)
5067             (save-excursion
5068               (forward-line -1)
5069               (run-hooks 'gnus-group-update-group-hook)))
5070           (setq loc (1+ loc)))
5071         (unless (or found visible-only)
5072           ;; No such line in the buffer, find out where it's supposed to
5073           ;; go, and insert it there (or at the end of the buffer).
5074           (if gnus-goto-missing-group-function
5075               (funcall gnus-goto-missing-group-function group)
5076             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
5077               (while (and entry (car entry)
5078                           (not
5079                            (gnus-goto-char
5080                             (text-property-any
5081                              (point-min) (point-max)
5082                              'gnus-group (gnus-intern-safe
5083                                           (caar entry) gnus-active-hashtb)))))
5084                 (setq entry (cdr entry)))
5085               (or entry (goto-char (point-max)))))
5086           ;; Finally insert the line.
5087           (let ((gnus-group-indentation (gnus-group-group-indentation)))
5088             (gnus-group-insert-group-line-info group)
5089             (save-excursion
5090               (forward-line -1)
5091               (run-hooks 'gnus-group-update-group-hook))))
5092         (gnus-group-set-mode-line)))))
5093
5094 (defun gnus-group-set-mode-line ()
5095   "Update the mode line in the group buffer."
5096   (when (memq 'group gnus-updated-mode-lines)
5097     ;; Yes, we want to keep this mode line updated.
5098     (save-excursion
5099       (set-buffer gnus-group-buffer)
5100       (let* ((gformat (or gnus-group-mode-line-format-spec
5101                           (setq gnus-group-mode-line-format-spec
5102                                 (gnus-parse-format
5103                                  gnus-group-mode-line-format
5104                                  gnus-group-mode-line-format-alist))))
5105              (gnus-tmp-news-server (cadr gnus-select-method))
5106              (gnus-tmp-news-method (car gnus-select-method))
5107              (gnus-tmp-colon (if (equal gnus-tmp-news-server "") "" ":"))
5108              (max-len 60)
5109              gnus-tmp-header            ;Dummy binding for user-defined formats
5110              ;; Get the resulting string.
5111              (modified 
5112               (and gnus-dribble-buffer
5113                    (buffer-name gnus-dribble-buffer)
5114                    (buffer-modified-p gnus-dribble-buffer)
5115                    (save-excursion
5116                      (set-buffer gnus-dribble-buffer)
5117                      (not (zerop (buffer-size))))))
5118              (mode-string (eval gformat)))
5119         ;; Say whether the dribble buffer has been modified.
5120         (setq mode-line-modified
5121               (if modified "---*- " "----- "))
5122         ;; If the line is too long, we chop it off.
5123         (when (> (length mode-string) max-len)
5124           (setq mode-string (substring mode-string 0 (- max-len 4))))
5125         (prog1
5126             (setq mode-line-buffer-identification 
5127                   (gnus-mode-line-buffer-identification
5128                    (list mode-string)))
5129           (set-buffer-modified-p modified))))))
5130
5131 (defun gnus-group-group-name ()
5132   "Get the name of the newsgroup on the current line."
5133   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
5134     (and group (symbol-name group))))
5135
5136 (defun gnus-group-group-level ()
5137   "Get the level of the newsgroup on the current line."
5138   (get-text-property (gnus-point-at-bol) 'gnus-level))
5139
5140 (defun gnus-group-group-indentation ()
5141   "Get the indentation of the newsgroup on the current line."
5142   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
5143       (and gnus-group-indentation-function
5144            (funcall gnus-group-indentation-function))
5145       ""))
5146
5147 (defun gnus-group-group-unread ()
5148   "Get the number of unread articles of the newsgroup on the current line."
5149   (get-text-property (gnus-point-at-bol) 'gnus-unread))
5150
5151 (defun gnus-group-search-forward (&optional backward all level first-too)
5152   "Find the next newsgroup with unread articles.
5153 If BACKWARD is non-nil, find the previous newsgroup instead.
5154 If ALL is non-nil, just find any newsgroup.
5155 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
5156 group exists.
5157 If FIRST-TOO, the current line is also eligible as a target."
5158   (let ((way (if backward -1 1))
5159         (low gnus-level-killed)
5160         (beg (point))
5161         pos found lev)
5162     (if (and backward (progn (beginning-of-line)) (bobp))
5163         nil
5164       (or first-too (forward-line way))
5165       (while (and
5166               (not (eobp))
5167               (not (setq
5168                     found
5169                     (and (or all
5170                              (and
5171                               (let ((unread
5172                                      (get-text-property (point) 'gnus-unread)))
5173                                 (and (numberp unread) (> unread 0)))
5174                               (setq lev (get-text-property (point)
5175                                                            'gnus-level))
5176                               (<= lev gnus-level-subscribed)))
5177                          (or (not level)
5178                              (and (setq lev (get-text-property (point)
5179                                                                'gnus-level))
5180                                   (or (= lev level)
5181                                       (and (< lev low)
5182                                            (< level lev)
5183                                            (progn
5184                                              (setq low lev)
5185                                              (setq pos (point))
5186                                              nil))))))))
5187               (zerop (forward-line way)))))
5188     (if found
5189         (progn (gnus-group-position-point) t)
5190       (goto-char (or pos beg))
5191       (and pos t))))
5192
5193 ;;; Gnus group mode commands
5194
5195 ;; Group marking.
5196
5197 (defun gnus-group-mark-group (n &optional unmark no-advance)
5198   "Mark the current group."
5199   (interactive "p")
5200   (let ((buffer-read-only nil)
5201         group)
5202     (while (and (> n 0)
5203                 (not (eobp)))
5204       (when (setq group (gnus-group-group-name))
5205         ;; Update the mark.
5206         (beginning-of-line)
5207         (forward-char
5208          (or (cdr (assq 'process gnus-group-mark-positions)) 2))
5209         (delete-char 1)
5210         (if unmark
5211             (progn
5212               (insert " ")
5213               (setq gnus-group-marked (delete group gnus-group-marked)))
5214           (insert "#")
5215           (setq gnus-group-marked
5216                 (cons group (delete group gnus-group-marked)))))
5217       (or no-advance (gnus-group-next-group 1))
5218       (decf n))
5219     (gnus-summary-position-point)
5220     n))
5221
5222 (defun gnus-group-unmark-group (n)
5223   "Remove the mark from the current group."
5224   (interactive "p")
5225   (gnus-group-mark-group n 'unmark)
5226   (gnus-group-position-point))
5227
5228 (defun gnus-group-unmark-all-groups ()
5229   "Unmark all groups."
5230   (interactive)
5231   (let ((groups gnus-group-marked))
5232     (save-excursion
5233       (while groups
5234         (gnus-group-remove-mark (pop groups)))))
5235   (gnus-group-position-point))
5236
5237 (defun gnus-group-mark-region (unmark beg end)
5238   "Mark all groups between point and mark.
5239 If UNMARK, remove the mark instead."
5240   (interactive "P\nr")
5241   (let ((num (count-lines beg end)))
5242     (save-excursion
5243       (goto-char beg)
5244       (- num (gnus-group-mark-group num unmark)))))
5245
5246 (defun gnus-group-mark-buffer (&optional unmark)
5247   "Mark all groups in the buffer.
5248 If UNMARK, remove the mark instead."
5249   (interactive "P")
5250   (gnus-group-mark-region unmark (point-min) (point-max)))
5251
5252 (defun gnus-group-mark-regexp (regexp)
5253   "Mark all groups that match some regexp."
5254   (interactive "sMark (regexp): ")
5255   (let ((alist (cdr gnus-newsrc-alist))
5256         group)
5257     (while alist
5258       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5259         (gnus-group-set-mark group))))
5260   (gnus-group-position-point))
5261
5262 (defun gnus-group-remove-mark (group)
5263   "Remove the process mark from GROUP and move point there.
5264 Return nil if the group isn't displayed."
5265   (if (gnus-group-goto-group group)
5266       (save-excursion
5267         (gnus-group-mark-group 1 'unmark t)
5268         t)
5269     (setq gnus-group-marked
5270           (delete group gnus-group-marked))
5271     nil))
5272
5273 (defun gnus-group-set-mark (group)
5274   "Set the process mark on GROUP."
5275   (if (gnus-group-goto-group group) 
5276       (save-excursion
5277         (gnus-group-mark-group 1 nil t))
5278     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
5279
5280 (defun gnus-group-universal-argument (arg &optional groups func)
5281   "Perform any command on all groups accoring to the process/prefix convention."
5282   (interactive "P")
5283   (let ((groups (or groups (gnus-group-process-prefix arg)))
5284         group func)
5285     (if (eq (setq func (or func
5286                            (key-binding
5287                             (read-key-sequence
5288                              (substitute-command-keys
5289                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5290             'undefined)
5291         (gnus-error 1 "Undefined key")
5292       (while groups
5293         (gnus-group-remove-mark (setq group (pop groups)))
5294         (command-execute func))))
5295   (gnus-group-position-point))
5296
5297 (defun gnus-group-process-prefix (n)
5298   "Return a list of groups to work on.
5299 Take into consideration N (the prefix) and the list of marked groups."
5300   (cond
5301    (n
5302     (setq n (prefix-numeric-value n))
5303     ;; There is a prefix, so we return a list of the N next
5304     ;; groups.
5305     (let ((way (if (< n 0) -1 1))
5306           (n (abs n))
5307           group groups)
5308       (save-excursion
5309         (while (and (> n 0)
5310                     (setq group (gnus-group-group-name)))
5311           (setq groups (cons group groups))
5312           (setq n (1- n))
5313           (gnus-group-next-group way)))
5314       (nreverse groups)))
5315    ((and (boundp 'transient-mark-mode)
5316          transient-mark-mode
5317          (boundp 'mark-active)
5318          mark-active)
5319     ;; Work on the region between point and mark.
5320     (let ((max (max (point) (mark)))
5321           groups)
5322       (save-excursion
5323         (goto-char (min (point) (mark)))
5324         (while
5325             (and
5326              (push (gnus-group-group-name) groups)
5327              (zerop (gnus-group-next-group 1))
5328              (< (point) max)))
5329         (nreverse groups))))
5330    (gnus-group-marked
5331     ;; No prefix, but a list of marked articles.
5332     (reverse gnus-group-marked))
5333    (t
5334     ;; Neither marked articles or a prefix, so we return the
5335     ;; current group.
5336     (let ((group (gnus-group-group-name)))
5337       (and group (list group))))))
5338
5339 ;; Selecting groups.
5340
5341 (defun gnus-group-read-group (&optional all no-article group)
5342   "Read news in this newsgroup.
5343 If the prefix argument ALL is non-nil, already read articles become
5344 readable.  IF ALL is a number, fetch this number of articles.  If the
5345 optional argument NO-ARTICLE is non-nil, no article will be
5346 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5347 group."
5348   (interactive "P")
5349   (let ((group (or group (gnus-group-group-name)))
5350         number active marked entry)
5351     (or group (error "No group on current line"))
5352     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5353                                             group gnus-newsrc-hashtb)))))
5354     ;; This group might be a dead group.  In that case we have to get
5355     ;; the number of unread articles from `gnus-active-hashtb'.
5356     (setq number
5357           (cond ((numberp all) all)
5358                 (entry (car entry))
5359                 ((setq active (gnus-active group))
5360                  (- (1+ (cdr active)) (car active)))))
5361     (gnus-summary-read-group
5362      group (or all (and (numberp number)
5363                         (zerop (+ number (length (cdr (assq 'tick marked)))
5364                                   (length (cdr (assq 'dormant marked)))))))
5365      no-article)))
5366
5367 (defun gnus-group-select-group (&optional all)
5368   "Select this newsgroup.
5369 No article is selected automatically.
5370 If ALL is non-nil, already read articles become readable.
5371 If ALL is a number, fetch this number of articles."
5372   (interactive "P")
5373   (gnus-group-read-group all t))
5374
5375 (defun gnus-group-quick-select-group (&optional all)
5376   "Select the current group \"quickly\".
5377 This means that no highlighting or scoring will be performed."
5378   (interactive "P")
5379   (let (gnus-visual
5380         gnus-score-find-score-files-function
5381         gnus-apply-kill-hook
5382         gnus-summary-expunge-below)
5383     (gnus-group-read-group all t)))
5384
5385 (defun gnus-group-visible-select-group (&optional all)
5386   "Select the current group without hiding any articles."
5387   (interactive "P")
5388   (let ((gnus-inhibit-limiting t))
5389     (gnus-group-read-group all t)))
5390
5391 ;;;###autoload
5392 (defun gnus-fetch-group (group)
5393   "Start Gnus if necessary and enter GROUP.
5394 Returns whether the fetching was successful or not."
5395   (interactive "sGroup name: ")
5396   (or (get-buffer gnus-group-buffer)
5397       (gnus))
5398   (gnus-group-read-group nil nil group))
5399
5400 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5401 ;; if selection was successful.
5402 (defun gnus-group-read-ephemeral-group
5403   (group method &optional activate quit-config)
5404   (let ((group (if (gnus-group-foreign-p group) group
5405                  (gnus-group-prefixed-name group method))))
5406     (gnus-sethash
5407      group
5408      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5409                      ((quit-config . ,(if quit-config quit-config
5410                                         (cons (current-buffer) 'summary))))))
5411      gnus-newsrc-hashtb)
5412     (set-buffer gnus-group-buffer)
5413     (or (gnus-check-server method)
5414         (error "Unable to contact server: %s" (gnus-status-message method)))
5415     (if activate (or (gnus-request-group group)
5416                      (error "Couldn't request group")))
5417     (condition-case ()
5418         (gnus-group-read-group t t group)
5419       (error nil)
5420       (quit nil))))
5421
5422 (defun gnus-group-jump-to-group (group)
5423   "Jump to newsgroup GROUP."
5424   (interactive
5425    (list (completing-read
5426           "Group: " gnus-active-hashtb nil
5427           (gnus-read-active-file-p)
5428           nil
5429           'gnus-group-history)))
5430
5431   (when (equal group "")
5432     (error "Empty group name"))
5433
5434   (when (string-match "[\000-\032]" group)
5435     (error "Control characters in group: %s" group))
5436
5437   (let ((b (text-property-any
5438             (point-min) (point-max)
5439             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5440     (unless (gnus-ephemeral-group-p group)
5441       (if b
5442           ;; Either go to the line in the group buffer...
5443           (goto-char b)
5444         ;; ... or insert the line.
5445         (or
5446          (gnus-active group)
5447          (gnus-activate-group group)
5448          (error "%s error: %s" group (gnus-status-message group)))
5449
5450         (gnus-group-update-group group)
5451         (goto-char (text-property-any
5452                     (point-min) (point-max)
5453                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5454     ;; Adjust cursor point.
5455     (gnus-group-position-point)))
5456
5457 (defun gnus-group-goto-group (group)
5458   "Goto to newsgroup GROUP."
5459   (when group
5460     (let ((b (text-property-any (point-min) (point-max)
5461                                 'gnus-group (gnus-intern-safe
5462                                              group gnus-active-hashtb))))
5463       (and b (goto-char b)))))
5464
5465 (defun gnus-group-next-group (n)
5466   "Go to next N'th newsgroup.
5467 If N is negative, search backward instead.
5468 Returns the difference between N and the number of skips actually
5469 done."
5470   (interactive "p")
5471   (gnus-group-next-unread-group n t))
5472
5473 (defun gnus-group-next-unread-group (n &optional all level)
5474   "Go to next N'th unread newsgroup.
5475 If N is negative, search backward instead.
5476 If ALL is non-nil, choose any newsgroup, unread or not.
5477 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5478 such group can be found, the next group with a level higher than
5479 LEVEL.
5480 Returns the difference between N and the number of skips actually
5481 made."
5482   (interactive "p")
5483   (let ((backward (< n 0))
5484         (n (abs n)))
5485     (while (and (> n 0)
5486                 (gnus-group-search-forward
5487                  backward (or (not gnus-group-goto-unread) all) level))
5488       (setq n (1- n)))
5489     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5490                                (if level " on this level or higher" "")))
5491     n))
5492
5493 (defun gnus-group-prev-group (n)
5494   "Go to previous N'th newsgroup.
5495 Returns the difference between N and the number of skips actually
5496 done."
5497   (interactive "p")
5498   (gnus-group-next-unread-group (- n) t))
5499
5500 (defun gnus-group-prev-unread-group (n)
5501   "Go to previous N'th unread newsgroup.
5502 Returns the difference between N and the number of skips actually
5503 done."
5504   (interactive "p")
5505   (gnus-group-next-unread-group (- n)))
5506
5507 (defun gnus-group-next-unread-group-same-level (n)
5508   "Go to next N'th unread newsgroup on the same level.
5509 If N is negative, search backward instead.
5510 Returns the difference between N and the number of skips actually
5511 done."
5512   (interactive "p")
5513   (gnus-group-next-unread-group n t (gnus-group-group-level))
5514   (gnus-group-position-point))
5515
5516 (defun gnus-group-prev-unread-group-same-level (n)
5517   "Go to next N'th unread newsgroup on the same level.
5518 Returns the difference between N and the number of skips actually
5519 done."
5520   (interactive "p")
5521   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5522   (gnus-group-position-point))
5523
5524 (defun gnus-group-best-unread-group (&optional exclude-group)
5525   "Go to the group with the highest level.
5526 If EXCLUDE-GROUP, do not go to that group."
5527   (interactive)
5528   (goto-char (point-min))
5529   (let ((best 100000)
5530         unread best-point)
5531     (while (not (eobp))
5532       (setq unread (get-text-property (point) 'gnus-unread))
5533       (if (and (numberp unread) (> unread 0))
5534           (progn
5535             (if (and (get-text-property (point) 'gnus-level)
5536                      (< (get-text-property (point) 'gnus-level) best)
5537                      (or (not exclude-group)
5538                          (not (equal exclude-group (gnus-group-group-name)))))
5539                 (progn
5540                   (setq best (get-text-property (point) 'gnus-level))
5541                   (setq best-point (point))))))
5542       (forward-line 1))
5543     (if best-point (goto-char best-point))
5544     (gnus-summary-position-point)
5545     (and best-point (gnus-group-group-name))))
5546
5547 (defun gnus-group-first-unread-group ()
5548   "Go to the first group with unread articles."
5549   (interactive)
5550   (prog1
5551       (let ((opoint (point))
5552             unread)
5553         (goto-char (point-min))
5554         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5555                 (and (numberp unread)   ; Not a topic.
5556                      (not (zerop unread))) ; Has unread articles.
5557                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5558             (point)                     ; Success.
5559           (goto-char opoint)
5560           nil))                         ; Not success.
5561     (gnus-group-position-point)))
5562
5563 (defun gnus-group-enter-server-mode ()
5564   "Jump to the server buffer."
5565   (interactive)
5566   (gnus-enter-server-buffer))
5567
5568 (defun gnus-group-make-group (name &optional method address)
5569   "Add a new newsgroup.
5570 The user will be prompted for a NAME, for a select METHOD, and an
5571 ADDRESS."
5572   (interactive
5573    (cons
5574     (read-string "Group name: ")
5575     (let ((method
5576            (completing-read
5577             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5578             nil t nil 'gnus-method-history)))
5579       (cond ((assoc method gnus-valid-select-methods)
5580              (list method
5581                    (if (memq 'prompt-address
5582                              (assoc method gnus-valid-select-methods))
5583                        (read-string "Address: ")
5584                      "")))
5585             ((assoc method gnus-server-alist)
5586              (list method))
5587             (t
5588              (list method ""))))))
5589
5590   (let* ((meth (and method (if address (list (intern method) address)
5591                              method)))
5592          (nname (if method (gnus-group-prefixed-name name meth) name))
5593          backend info)
5594     (when (gnus-gethash nname gnus-newsrc-hashtb)
5595       (error "Group %s already exists" nname))
5596     ;; Subscribe to the new group.
5597     (gnus-group-change-level
5598      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5599      gnus-level-default-subscribed gnus-level-killed
5600      (and (gnus-group-group-name)
5601           (gnus-gethash (gnus-group-group-name)
5602                         gnus-newsrc-hashtb))
5603      t)
5604     ;; Make it active.
5605     (gnus-set-active nname (cons 1 0))
5606     (or (gnus-ephemeral-group-p name)
5607         (gnus-dribble-enter
5608          (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5609     ;; Insert the line.
5610     (gnus-group-insert-group-line-info nname)
5611     (forward-line -1)
5612     (gnus-group-position-point)
5613
5614     ;; Load the backend and try to make the backend create
5615     ;; the group as well.
5616     (when (assoc (symbol-name (setq backend (car (gnus-server-get-method
5617                                                   nil meth))))
5618                  gnus-valid-select-methods)
5619       (require backend))
5620     (gnus-check-server meth)
5621     (and (gnus-check-backend-function 'request-create-group nname)
5622          (gnus-request-create-group nname))
5623     t))
5624
5625 (defun gnus-group-delete-group (group &optional force)
5626   "Delete the current group.  Only meaningful with mail groups.
5627 If FORCE (the prefix) is non-nil, all the articles in the group will
5628 be deleted.  This is \"deleted\" as in \"removed forever from the face
5629 of the Earth\".  There is no undo.  The user will be prompted before
5630 doing the deletion."
5631   (interactive
5632    (list (gnus-group-group-name)
5633          current-prefix-arg))
5634   (or group (error "No group to rename"))
5635   (or (gnus-check-backend-function 'request-delete-group group)
5636       (error "This backend does not support group deletion"))
5637   (prog1
5638       (if (not (gnus-yes-or-no-p
5639                 (format
5640                  "Do you really want to delete %s%s? "
5641                  group (if force " and all its contents" ""))))
5642           () ; Whew!
5643         (gnus-message 6 "Deleting group %s..." group)
5644         (if (not (gnus-request-delete-group group force))
5645             (gnus-error 3 "Couldn't delete group %s" group)
5646           (gnus-message 6 "Deleting group %s...done" group)
5647           (gnus-group-goto-group group)
5648           (gnus-group-kill-group 1 t)
5649           (gnus-sethash group nil gnus-active-hashtb)
5650           t))
5651     (gnus-group-position-point)))
5652
5653 (defun gnus-group-rename-group (group new-name)
5654   (interactive
5655    (list
5656     (gnus-group-group-name)
5657     (progn
5658       (or (gnus-check-backend-function
5659            'request-rename-group (gnus-group-group-name))
5660           (error "This backend does not support renaming groups"))
5661       (read-string "New group name: "))))
5662
5663   (or (gnus-check-backend-function 'request-rename-group group)
5664       (error "This backend does not support renaming groups"))
5665
5666   (or group (error "No group to rename"))
5667   (and (string-match "^[ \t]*$" new-name)
5668        (error "Not a valid group name"))
5669
5670   ;; We find the proper prefixed name.
5671   (setq new-name
5672         (gnus-group-prefixed-name
5673          (gnus-group-real-name new-name)
5674          (gnus-info-method (gnus-get-info group))))
5675
5676   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5677   (prog1
5678       (if (not (gnus-request-rename-group group new-name))
5679           (gnus-error 3 "Couldn't rename group %s to %s" group new-name)
5680         ;; We rename the group internally by killing it...
5681         (gnus-group-goto-group group)
5682         (gnus-group-kill-group)
5683         ;; ... changing its name ...
5684         (setcar (cdar gnus-list-of-killed-groups) new-name)
5685         ;; ... and then yanking it.  Magic!
5686         (gnus-group-yank-group)
5687         (gnus-set-active new-name (gnus-active group))
5688         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5689         new-name)
5690     (gnus-group-position-point)))
5691
5692 (defun gnus-group-edit-group (group &optional part)
5693   "Edit the group on the current line."
5694   (interactive (list (gnus-group-group-name)))
5695   (let* ((part (or part 'info))
5696          (done-func `(lambda ()
5697                        "Exit editing mode and update the information."
5698                        (interactive)
5699                        (gnus-group-edit-group-done ',part ,group)))
5700          (winconf (current-window-configuration))
5701          info)
5702     (or group (error "No group on current line"))
5703     (or (setq info (gnus-get-info group))
5704         (error "Killed group; can't be edited"))
5705     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5706     (gnus-configure-windows 'edit-group)
5707     (gnus-add-current-to-buffer-list)
5708     (emacs-lisp-mode)
5709     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5710     (use-local-map (copy-keymap emacs-lisp-mode-map))
5711     (local-set-key "\C-c\C-c" done-func)
5712     (make-local-variable 'gnus-prev-winconf)
5713     (setq gnus-prev-winconf winconf)
5714     (erase-buffer)
5715     (insert
5716      (cond
5717       ((eq part 'method)
5718        ";; Type `C-c C-c' after editing the select method.\n\n")
5719       ((eq part 'params)
5720        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5721       ((eq part 'info)
5722        ";; Type `C-c C-c' after editing the group info.\n\n")))
5723     (insert
5724      (pp-to-string
5725       (cond ((eq part 'method)
5726              (or (gnus-info-method info) "native"))
5727             ((eq part 'params)
5728              (gnus-info-params info))
5729             (t info)))
5730      "\n")))
5731
5732 (defun gnus-group-edit-group-method (group)
5733   "Edit the select method of GROUP."
5734   (interactive (list (gnus-group-group-name)))
5735   (gnus-group-edit-group group 'method))
5736
5737 (defun gnus-group-edit-group-parameters (group)
5738   "Edit the group parameters of GROUP."
5739   (interactive (list (gnus-group-group-name)))
5740   (gnus-group-edit-group group 'params))
5741
5742 (defun gnus-group-edit-group-done (part group)
5743   "Get info from buffer, update variables and jump to the group buffer."
5744   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5745   (goto-char (point-min))
5746   (let* ((form (read (current-buffer)))
5747          (winconf gnus-prev-winconf)
5748          (method (cond ((eq part 'info) (nth 4 form))
5749                        ((eq part 'method) form)
5750                        (t nil)))
5751          (info (cond ((eq part 'info) form)
5752                      ((eq part 'method) (gnus-get-info group))
5753                      (t nil)))
5754          (new-group (if info
5755                       (if (or (not method)
5756                               (gnus-server-equal
5757                                gnus-select-method method))
5758                           (gnus-group-real-name (car info))
5759                         (gnus-group-prefixed-name
5760                          (gnus-group-real-name (car info)) method))
5761                       nil)))
5762     (when (and new-group
5763                (not (equal new-group group)))
5764       (when (gnus-group-goto-group group)
5765         (gnus-group-kill-group 1))
5766       (gnus-activate-group new-group))
5767     ;; Set the info.
5768     (if (and info new-group)
5769         (progn
5770           (setq info (gnus-copy-sequence info))
5771           (setcar info new-group)
5772           (unless (gnus-server-equal method "native")
5773             (unless (nthcdr 3 info)
5774               (nconc info (list nil nil)))
5775             (unless (nthcdr 4 info)
5776               (nconc info (list nil)))
5777             (gnus-info-set-method info method))
5778           (gnus-group-set-info info))
5779       (gnus-group-set-info form (or new-group group) part))
5780     (kill-buffer (current-buffer))
5781     (and winconf (set-window-configuration winconf))
5782     (set-buffer gnus-group-buffer)
5783     (gnus-group-update-group (or new-group group))
5784     (gnus-group-position-point)))
5785
5786 (defun gnus-group-make-help-group ()
5787   "Create the Gnus documentation group."
5788   (interactive)
5789   (let ((path load-path)
5790         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5791         file dir)
5792     (and (gnus-gethash name gnus-newsrc-hashtb)
5793          (error "Documentation group already exists"))
5794     (while path
5795       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5796             file nil)
5797       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5798                 (file-exists-p
5799                  (setq file (concat (file-name-directory
5800                                      (directory-file-name dir))
5801                                     "etc/gnus-tut.txt"))))
5802         (setq path nil)))
5803     (if (not file)
5804         (gnus-message 1 "Couldn't find doc group")
5805       (gnus-group-make-group
5806        (gnus-group-real-name name)
5807        (list 'nndoc "gnus-help"
5808              (list 'nndoc-address file)
5809              (list 'nndoc-article-type 'mbox)))))
5810   (gnus-group-position-point))
5811
5812 (defun gnus-group-make-doc-group (file type)
5813   "Create a group that uses a single file as the source."
5814   (interactive
5815    (list (read-file-name "File name: ")
5816          (and current-prefix-arg 'ask)))
5817   (when (eq type 'ask)
5818     (let ((err "")
5819           char found)
5820       (while (not found)
5821         (message
5822          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5823          err)
5824         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5825                           ((= char ?b) 'babyl)
5826                           ((= char ?d) 'digest)
5827                           ((= char ?f) 'forward)
5828                           ((= char ?a) 'mmfd)
5829                           (t (setq err (format "%c unknown. " char))
5830                              nil))))
5831       (setq type found)))
5832   (let* ((file (expand-file-name file))
5833          (name (gnus-generate-new-group-name
5834                 (gnus-group-prefixed-name
5835                  (file-name-nondirectory file) '(nndoc "")))))
5836     (gnus-group-make-group
5837      (gnus-group-real-name name)
5838      (list 'nndoc (file-name-nondirectory file)
5839            (list 'nndoc-address file)
5840            (list 'nndoc-article-type (or type 'guess))))))
5841
5842 (defun gnus-group-make-archive-group (&optional all)
5843   "Create the (ding) Gnus archive group of the most recent articles.
5844 Given a prefix, create a full group."
5845   (interactive "P")
5846   (let ((group (gnus-group-prefixed-name
5847                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5848     (and (gnus-gethash group gnus-newsrc-hashtb)
5849          (error "Archive group already exists"))
5850     (gnus-group-make-group
5851      (gnus-group-real-name group)
5852      (list 'nndir (if all "hpc" "edu")
5853            (list 'nndir-directory
5854                  (if all gnus-group-archive-directory
5855                    gnus-group-recent-archive-directory))))))
5856
5857 (defun gnus-group-make-directory-group (dir)
5858   "Create an nndir group.
5859 The user will be prompted for a directory.  The contents of this
5860 directory will be used as a newsgroup.  The directory should contain
5861 mail messages or news articles in files that have numeric names."
5862   (interactive
5863    (list (read-file-name "Create group from directory: ")))
5864   (or (file-exists-p dir) (error "No such directory"))
5865   (or (file-directory-p dir) (error "Not a directory"))
5866   (let ((ext "")
5867         (i 0)
5868         group)
5869     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5870       (setq group
5871             (gnus-group-prefixed-name
5872              (concat (file-name-as-directory (directory-file-name dir))
5873                      ext)
5874              '(nndir "")))
5875       (setq ext (format "<%d>" (setq i (1+ i)))))
5876     (gnus-group-make-group
5877      (gnus-group-real-name group)
5878      (list 'nndir group (list 'nndir-directory dir)))))
5879
5880 (defun gnus-group-make-kiboze-group (group address scores)
5881   "Create an nnkiboze group.
5882 The user will be prompted for a name, a regexp to match groups, and
5883 score file entries for articles to include in the group."
5884   (interactive
5885    (list
5886     (read-string "nnkiboze group name: ")
5887     (read-string "Source groups (regexp): ")
5888     (let ((headers (mapcar (lambda (group) (list group))
5889                            '("subject" "from" "number" "date" "message-id"
5890                              "references" "chars" "lines" "xref"
5891                              "followup" "all" "body" "head")))
5892           scores header regexp regexps)
5893       (while (not (equal "" (setq header (completing-read
5894                                           "Match on header: " headers nil t))))
5895         (setq regexps nil)
5896         (while (not (equal "" (setq regexp (read-string
5897                                             (format "Match on %s (string): "
5898                                                     header)))))
5899           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5900         (setq scores (cons (cons header regexps) scores)))
5901       scores)))
5902   (gnus-group-make-group group "nnkiboze" address)
5903   (nnheader-temp-write (gnus-score-file-name (concat "nnkiboze:" group))
5904     (let (emacs-lisp-mode-hook)
5905       (pp scores (current-buffer)))))
5906
5907 (defun gnus-group-add-to-virtual (n vgroup)
5908   "Add the current group to a virtual group."
5909   (interactive
5910    (list current-prefix-arg
5911          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5912                           "nnvirtual:")))
5913   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5914       (error "%s is not an nnvirtual group" vgroup))
5915   (let* ((groups (gnus-group-process-prefix n))
5916          (method (gnus-info-method (gnus-get-info vgroup))))
5917     (setcar (cdr method)
5918             (concat
5919              (nth 1 method) "\\|"
5920              (mapconcat
5921               (lambda (s)
5922                 (gnus-group-remove-mark s)
5923                 (concat "\\(^" (regexp-quote s) "$\\)"))
5924               groups "\\|"))))
5925   (gnus-group-position-point))
5926
5927 (defun gnus-group-make-empty-virtual (group)
5928   "Create a new, fresh, empty virtual group."
5929   (interactive "sCreate new, empty virtual group: ")
5930   (let* ((method (list 'nnvirtual "^$"))
5931          (pgroup (gnus-group-prefixed-name group method)))
5932     ;; Check whether it exists already.
5933     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5934          (error "Group %s already exists." pgroup))
5935     ;; Subscribe the new group after the group on the current line.
5936     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5937     (gnus-group-update-group pgroup)
5938     (forward-line -1)
5939     (gnus-group-position-point)))
5940
5941 (defun gnus-group-enter-directory (dir)
5942   "Enter an ephemeral nneething group."
5943   (interactive "DDirectory to read: ")
5944   (let* ((method (list 'nneething dir))
5945          (leaf (gnus-group-prefixed-name
5946                 (file-name-nondirectory (directory-file-name dir))
5947                 method))
5948          (name (gnus-generate-new-group-name leaf)))
5949     (let ((nneething-read-only t))
5950       (or (gnus-group-read-ephemeral-group
5951            name method t
5952            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5953                                       'summary 'group)))
5954           (error "Couldn't enter %s" dir)))))
5955
5956 ;; Group sorting commands
5957 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5958
5959 (defun gnus-group-sort-groups (func &optional reverse)
5960   "Sort the group buffer according to FUNC.
5961 If REVERSE, reverse the sorting order."
5962   (interactive (list gnus-group-sort-function
5963                      current-prefix-arg))
5964   (let ((func (cond 
5965                ((not (listp func)) func)
5966                ((null func) func)
5967                ((= 1 (length func)) (car func))
5968                (t `(lambda (t1 t2)
5969                      ,(gnus-make-sort-function 
5970                        (reverse func)))))))
5971     ;; We peel off the dummy group from the alist.
5972     (when func
5973       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5974         (pop gnus-newsrc-alist))
5975       ;; Do the sorting.
5976       (setq gnus-newsrc-alist
5977             (sort gnus-newsrc-alist func))
5978       (when reverse
5979         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5980       ;; Regenerate the hash table.
5981       (gnus-make-hashtable-from-newsrc-alist)
5982       (gnus-group-list-groups))))
5983
5984 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5985   "Sort the group buffer alphabetically by group name.
5986 If REVERSE, sort in reverse order."
5987   (interactive "P")
5988   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5989
5990 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5991   "Sort the group buffer by number of unread articles.
5992 If REVERSE, sort in reverse order."
5993   (interactive "P")
5994   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5995
5996 (defun gnus-group-sort-groups-by-level (&optional reverse)
5997   "Sort the group buffer by group level.
5998 If REVERSE, sort in reverse order."
5999   (interactive "P")
6000   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
6001
6002 (defun gnus-group-sort-groups-by-score (&optional reverse)
6003   "Sort the group buffer by group score.
6004 If REVERSE, sort in reverse order."
6005   (interactive "P")
6006   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
6007
6008 (defun gnus-group-sort-groups-by-rank (&optional reverse)
6009   "Sort the group buffer by group rank.
6010 If REVERSE, sort in reverse order."
6011   (interactive "P")
6012   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
6013
6014 (defun gnus-group-sort-groups-by-method (&optional reverse)
6015   "Sort the group buffer alphabetically by backend name.
6016 If REVERSE, sort in reverse order."
6017   (interactive "P")
6018   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
6019
6020 (defun gnus-group-sort-by-alphabet (info1 info2)
6021   "Sort alphabetically."
6022   (string< (gnus-info-group info1) (gnus-info-group info2)))
6023
6024 (defun gnus-group-sort-by-unread (info1 info2)
6025   "Sort by number of unread articles."
6026   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
6027         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
6028     (< (or (and (numberp n1) n1) 0)
6029        (or (and (numberp n2) n2) 0))))
6030
6031 (defun gnus-group-sort-by-level (info1 info2)
6032   "Sort by level."
6033   (< (gnus-info-level info1) (gnus-info-level info2)))
6034
6035 (defun gnus-group-sort-by-method (info1 info2)
6036   "Sort alphabetically by backend name."
6037   (string< (symbol-name (car (gnus-find-method-for-group
6038                               (gnus-info-group info1) info1)))
6039            (symbol-name (car (gnus-find-method-for-group
6040                               (gnus-info-group info2) info2)))))
6041
6042 (defun gnus-group-sort-by-score (info1 info2)
6043   "Sort by group score."
6044   (< (gnus-info-score info1) (gnus-info-score info2)))
6045
6046 (defun gnus-group-sort-by-rank (info1 info2)
6047   "Sort by level and score."
6048   (let ((level1 (gnus-info-level info1))
6049         (level2 (gnus-info-level info2)))
6050     (or (< level1 level2)
6051         (and (= level1 level2)
6052              (> (gnus-info-score info1) (gnus-info-score info2))))))
6053
6054 ;; Group catching up.
6055
6056 (defun gnus-group-clear-data (n)
6057   "Clear all marks and read ranges from the current group."
6058   (interactive "P")
6059   (let ((groups (gnus-group-process-prefix n))
6060         group info)
6061     (while (setq group (pop groups))
6062       (setq info (gnus-get-info group))
6063       (gnus-info-set-read info nil)
6064       (when (gnus-info-marks info)
6065         (gnus-info-set-marks info nil))
6066       (gnus-get-unread-articles-in-group info (gnus-active group) t)
6067       (when (gnus-group-goto-group group)
6068         (gnus-group-remove-mark group)
6069         (gnus-group-update-group-line)))))
6070
6071 (defun gnus-group-catchup-current (&optional n all)
6072   "Mark all articles not marked as unread in current newsgroup as read.
6073 If prefix argument N is numeric, the ARG next newsgroups will be
6074 caught up.  If ALL is non-nil, marked articles will also be marked as
6075 read.  Cross references (Xref: header) of articles are ignored.
6076 The difference between N and actual number of newsgroups that were
6077 caught up is returned."
6078   (interactive "P")
6079   (unless (gnus-group-group-name)
6080     (error "No group on the current line"))
6081   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
6082                gnus-expert-user
6083                (gnus-y-or-n-p
6084                 (if all
6085                     "Do you really want to mark all articles as read? "
6086                   "Mark all unread articles as read? "))))
6087       n
6088     (let ((groups (gnus-group-process-prefix n))
6089           (ret 0))
6090       (while groups
6091         ;; Virtual groups have to be given special treatment.
6092         (let ((method (gnus-find-method-for-group (car groups))))
6093           (if (eq 'nnvirtual (car method))
6094               (nnvirtual-catchup-group
6095                (gnus-group-real-name (car groups)) (nth 1 method) all)))
6096         (gnus-group-remove-mark (car groups))
6097         (if (>= (gnus-group-group-level) gnus-level-zombie)
6098             (gnus-message 2 "Dead groups can't be caught up")
6099           (if (prog1
6100                   (gnus-group-goto-group (car groups))
6101                 (gnus-group-catchup (car groups) all))
6102               (gnus-group-update-group-line)
6103             (setq ret (1+ ret))))
6104         (setq groups (cdr groups)))
6105       (gnus-group-next-unread-group 1)
6106       ret)))
6107
6108 (defun gnus-group-catchup-current-all (&optional n)
6109   "Mark all articles in current newsgroup as read.
6110 Cross references (Xref: header) of articles are ignored."
6111   (interactive "P")
6112   (gnus-group-catchup-current n 'all))
6113
6114 (defun gnus-group-catchup (group &optional all)
6115   "Mark all articles in GROUP as read.
6116 If ALL is non-nil, all articles are marked as read.
6117 The return value is the number of articles that were marked as read,
6118 or nil if no action could be taken."
6119   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6120          (num (car entry)))
6121     ;; Do the updating only if the newsgroup isn't killed.
6122     (if (not (numberp (car entry)))
6123         (gnus-message 1 "Can't catch up; non-active group")
6124       ;; Do auto-expirable marks if that's required.
6125       (when (gnus-group-auto-expirable-p group)
6126         (gnus-add-marked-articles
6127          group 'expire (gnus-list-of-unread-articles group))
6128         (when all
6129           (let ((marks (nth 3 (nth 2 entry))))
6130             (gnus-add-marked-articles
6131              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
6132             (gnus-add-marked-articles
6133              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
6134       (when entry
6135         (gnus-update-read-articles group nil)
6136         ;; Also nix out the lists of marks and dormants.
6137         (when all
6138           (gnus-add-marked-articles group 'tick nil nil 'force)
6139           (gnus-add-marked-articles group 'dormant nil nil 'force))
6140         (run-hooks 'gnus-group-catchup-group-hook)
6141         num))))
6142
6143 (defun gnus-group-expire-articles (&optional n)
6144   "Expire all expirable articles in the current newsgroup."
6145   (interactive "P")
6146   (let ((groups (gnus-group-process-prefix n))
6147         group)
6148     (unless groups
6149       (error "No groups to expire"))
6150     (while (setq group (pop groups))
6151       (gnus-group-remove-mark group)
6152       (when (gnus-check-backend-function 'request-expire-articles group)
6153         (gnus-message 6 "Expiring articles in %s..." group)
6154         (let* ((info (gnus-get-info group))
6155                (expirable (if (gnus-group-total-expirable-p group)
6156                               (cons nil (gnus-list-of-read-articles group))
6157                             (assq 'expire (gnus-info-marks info))))
6158                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
6159           (when expirable
6160             (setcdr
6161              expirable
6162              (gnus-compress-sequence
6163               (if expiry-wait
6164                   ;; We set the expiry variables to the groupp
6165                   ;; parameter. 
6166                   (let ((nnmail-expiry-wait-function nil)
6167                         (nnmail-expiry-wait expiry-wait))
6168                     (gnus-request-expire-articles
6169                      (gnus-uncompress-sequence (cdr expirable)) group))
6170                 ;; Just expire using the normal expiry values.
6171                 (gnus-request-expire-articles
6172                  (gnus-uncompress-sequence (cdr expirable)) group))))
6173             (gnus-close-group group))
6174           (gnus-message 6 "Expiring articles in %s...done" group)))
6175       (gnus-group-position-point))))
6176
6177 (defun gnus-group-expire-all-groups ()
6178   "Expire all expirable articles in all newsgroups."
6179   (interactive)
6180   (save-excursion
6181     (gnus-message 5 "Expiring...")
6182     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
6183                                      (cdr gnus-newsrc-alist))))
6184       (gnus-group-expire-articles nil)))
6185   (gnus-group-position-point)
6186   (gnus-message 5 "Expiring...done"))
6187
6188 (defun gnus-group-set-current-level (n level)
6189   "Set the level of the next N groups to LEVEL."
6190   (interactive
6191    (list
6192     current-prefix-arg
6193     (string-to-int
6194      (let ((s (read-string
6195                (format "Level (default %s): "
6196                        (or (gnus-group-group-level) 
6197                            gnus-level-default-subscribed)))))
6198        (if (string-match "^\\s-*$" s)
6199            (int-to-string (or (gnus-group-group-level) 
6200                               gnus-level-default-subscribed))
6201          s)))))
6202   (or (and (>= level 1) (<= level gnus-level-killed))
6203       (error "Illegal level: %d" level))
6204   (let ((groups (gnus-group-process-prefix n))
6205         group)
6206     (while (setq group (pop groups))
6207       (gnus-group-remove-mark group)
6208       (gnus-message 6 "Changed level of %s from %d to %d"
6209                     group (or (gnus-group-group-level) gnus-level-killed)
6210                     level)
6211       (gnus-group-change-level
6212        group level (or (gnus-group-group-level) gnus-level-killed))
6213       (gnus-group-update-group-line)))
6214   (gnus-group-position-point))
6215
6216 (defun gnus-group-unsubscribe-current-group (&optional n)
6217   "Toggle subscription of the current group.
6218 If given numerical prefix, toggle the N next groups."
6219   (interactive "P")
6220   (let ((groups (gnus-group-process-prefix n))
6221         group)
6222     (while groups
6223       (setq group (car groups)
6224             groups (cdr groups))
6225       (gnus-group-remove-mark group)
6226       (gnus-group-unsubscribe-group
6227        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
6228                  gnus-level-default-unsubscribed
6229                gnus-level-default-subscribed) t)
6230       (gnus-group-update-group-line))
6231     (gnus-group-next-group 1)))
6232
6233 (defun gnus-group-unsubscribe-group (group &optional level silent)
6234   "Toggle subscription to GROUP.
6235 Killed newsgroups are subscribed.  If SILENT, don't try to update the
6236 group line."
6237   (interactive
6238    (list (completing-read
6239           "Group: " gnus-active-hashtb nil
6240           (gnus-read-active-file-p)
6241           nil 
6242           'gnus-group-history)))
6243   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
6244     (cond
6245      ((string-match "^[ \t]$" group)
6246       (error "Empty group name"))
6247      (newsrc
6248       ;; Toggle subscription flag.
6249       (gnus-group-change-level
6250        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
6251                                       gnus-level-subscribed)
6252                                   (1+ gnus-level-subscribed)
6253                                 gnus-level-default-subscribed)))
6254       (unless silent
6255         (gnus-group-update-group group)))
6256      ((and (stringp group)
6257            (or (not (gnus-read-active-file-p))
6258                (gnus-active group)))
6259       ;; Add new newsgroup.
6260       (gnus-group-change-level
6261        group
6262        (if level level gnus-level-default-subscribed)
6263        (or (and (member group gnus-zombie-list)
6264                 gnus-level-zombie)
6265            gnus-level-killed)
6266        (and (gnus-group-group-name)
6267             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
6268       (unless silent
6269         (gnus-group-update-group group)))
6270      (t (error "No such newsgroup: %s" group)))
6271     (gnus-group-position-point)))
6272
6273 (defun gnus-group-transpose-groups (n)
6274   "Move the current newsgroup up N places.
6275 If given a negative prefix, move down instead.  The difference between
6276 N and the number of steps taken is returned."
6277   (interactive "p")
6278   (or (gnus-group-group-name)
6279       (error "No group on current line"))
6280   (gnus-group-kill-group 1)
6281   (prog1
6282       (forward-line (- n))
6283     (gnus-group-yank-group)
6284     (gnus-group-position-point)))
6285
6286 (defun gnus-group-kill-all-zombies ()
6287   "Kill all zombie newsgroups."
6288   (interactive)
6289   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6290   (setq gnus-zombie-list nil)
6291   (gnus-group-list-groups))
6292
6293 (defun gnus-group-kill-region (begin end)
6294   "Kill newsgroups in current region (excluding current point).
6295 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6296   (interactive "r")
6297   (let ((lines
6298          ;; Count lines.
6299          (save-excursion
6300            (count-lines
6301             (progn
6302               (goto-char begin)
6303               (beginning-of-line)
6304               (point))
6305             (progn
6306               (goto-char end)
6307               (beginning-of-line)
6308               (point))))))
6309     (goto-char begin)
6310     (beginning-of-line)                 ;Important when LINES < 1
6311     (gnus-group-kill-group lines)))
6312
6313 (defun gnus-group-kill-group (&optional n discard)
6314   "Kill the next N groups.
6315 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6316 However, only groups that were alive can be yanked; already killed
6317 groups or zombie groups can't be yanked.
6318 The return value is the name of the group that was killed, or a list
6319 of groups killed."
6320   (interactive "P")
6321   (let ((buffer-read-only nil)
6322         (groups (gnus-group-process-prefix n))
6323         group entry level out)
6324     (if (< (length groups) 10)
6325         ;; This is faster when there are few groups.
6326         (while groups
6327           (push (setq group (pop groups)) out)
6328           (gnus-group-remove-mark group)
6329           (setq level (gnus-group-group-level))
6330           (gnus-delete-line)
6331           (when (and (not discard)
6332                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6333             (push (cons (car entry) (nth 2 entry))
6334                   gnus-list-of-killed-groups))
6335           (gnus-group-change-level
6336            (if entry entry group) gnus-level-killed (if entry nil level)))
6337       ;; If there are lots and lots of groups to be killed, we use
6338       ;; this thing instead.
6339       (let (entry)
6340         (setq groups (nreverse groups))
6341         (while groups
6342           (gnus-group-remove-mark (setq group (pop groups)))
6343           (gnus-delete-line)
6344           (push group gnus-killed-list)
6345           (setq gnus-newsrc-alist
6346                 (delq (assoc group gnus-newsrc-alist)
6347                       gnus-newsrc-alist))
6348           (when gnus-group-change-level-function
6349             (funcall gnus-group-change-level-function group 9 3))
6350           (cond
6351            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6352             (push (cons (car entry) (nth 2 entry))
6353                   gnus-list-of-killed-groups)
6354             (setcdr (cdr entry) (cdddr entry)))
6355            ((member group gnus-zombie-list)
6356             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6357         (gnus-make-hashtable-from-newsrc-alist)))
6358
6359     (gnus-group-position-point)
6360     (if (< (length out) 2) (car out) (nreverse out))))
6361
6362 (defun gnus-group-yank-group (&optional arg)
6363   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6364 inserting it before the current newsgroup.  The numeric ARG specifies
6365 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6366 is returned, or (if several groups are yanked) a list of yanked groups
6367 is returned."
6368   (interactive "p")
6369   (setq arg (or arg 1))
6370   (let (info group prev out)
6371     (while (>= (decf arg) 0)
6372       (if (not (setq info (pop gnus-list-of-killed-groups)))
6373           (error "No more newsgroups to yank"))
6374       (push (setq group (nth 1 info)) out)
6375       ;; Find which newsgroup to insert this one before - search
6376       ;; backward until something suitable is found.  If there are no
6377       ;; other newsgroups in this buffer, just make this newsgroup the
6378       ;; first newsgroup.
6379       (setq prev (gnus-group-group-name))
6380       (gnus-group-change-level
6381        info (gnus-info-level (cdr info)) gnus-level-killed
6382        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6383        t)
6384       (gnus-group-insert-group-line-info group))
6385     (forward-line -1)
6386     (gnus-group-position-point)
6387     (if (< (length out) 2) (car out) (nreverse out))))
6388
6389 (defun gnus-group-kill-level (level)
6390   "Kill all groups that is on a certain LEVEL."
6391   (interactive "nKill all groups on level: ")
6392   (cond
6393    ((= level gnus-level-zombie)
6394     (setq gnus-killed-list
6395           (nconc gnus-zombie-list gnus-killed-list))
6396     (setq gnus-zombie-list nil))
6397    ((and (< level gnus-level-zombie)
6398          (> level 0)
6399          (or gnus-expert-user
6400              (gnus-yes-or-no-p
6401               (format
6402                "Do you really want to kill all groups on level %d? "
6403                level))))
6404     (let* ((prev gnus-newsrc-alist)
6405            (alist (cdr prev)))
6406       (while alist
6407         (if (= (gnus-info-level level) level)
6408             (setcdr prev (cdr alist))
6409           (setq prev alist))
6410         (setq alist (cdr alist)))
6411       (gnus-make-hashtable-from-newsrc-alist)
6412       (gnus-group-list-groups)))
6413    (t
6414     (error "Can't kill; illegal level: %d" level))))
6415
6416 (defun gnus-group-list-all-groups (&optional arg)
6417   "List all newsgroups with level ARG or lower.
6418 Default is gnus-level-unsubscribed, which lists all subscribed and most
6419 unsubscribed groups."
6420   (interactive "P")
6421   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6422
6423 ;; Redefine this to list ALL killed groups if prefix arg used.
6424 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6425 (defun gnus-group-list-killed (&optional arg)
6426   "List all killed newsgroups in the group buffer.
6427 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6428 entail asking the server for the groups."
6429   (interactive "P")
6430   ;; Find all possible killed newsgroups if arg.
6431   (when arg
6432     (gnus-get-killed-groups))
6433   (if (not gnus-killed-list)
6434       (gnus-message 6 "No killed groups")
6435     (let (gnus-group-list-mode)
6436       (funcall gnus-group-prepare-function
6437                gnus-level-killed t gnus-level-killed))
6438     (goto-char (point-min)))
6439   (gnus-group-position-point))
6440
6441 (defun gnus-group-list-zombies ()
6442   "List all zombie newsgroups in the group buffer."
6443   (interactive)
6444   (if (not gnus-zombie-list)
6445       (gnus-message 6 "No zombie groups")
6446     (let (gnus-group-list-mode)
6447       (funcall gnus-group-prepare-function
6448                gnus-level-zombie t gnus-level-zombie))
6449     (goto-char (point-min)))
6450   (gnus-group-position-point))
6451
6452 (defun gnus-group-list-active ()
6453   "List all groups that are available from the server(s)."
6454   (interactive)
6455   ;; First we make sure that we have really read the active file.
6456   (unless (gnus-read-active-file-p)
6457     (let ((gnus-read-active-file t))
6458       (gnus-read-active-file)))
6459   ;; Find all groups and sort them.
6460   (let ((groups
6461          (sort
6462           (let (list)
6463             (mapatoms
6464              (lambda (sym)
6465                (and (boundp sym)
6466                     (symbol-value sym)
6467                     (setq list (cons (symbol-name sym) list))))
6468              gnus-active-hashtb)
6469             list)
6470           'string<))
6471         (buffer-read-only nil))
6472     (erase-buffer)
6473     (while groups
6474       (gnus-group-insert-group-line-info (pop groups)))
6475     (goto-char (point-min))))
6476
6477 (defun gnus-activate-all-groups (level)
6478   "Activate absolutely all groups."
6479   (interactive (list 7))
6480   (let ((gnus-activate-level level)
6481         (gnus-activate-foreign-newsgroups level))
6482     (gnus-group-get-new-news)))
6483
6484 (defun gnus-group-get-new-news (&optional arg)
6485   "Get newly arrived articles.
6486 If ARG is a number, it specifies which levels you are interested in
6487 re-scanning.  If ARG is non-nil and not a number, this will force
6488 \"hard\" re-reading of the active files from all servers."
6489   (interactive "P")
6490   (run-hooks 'gnus-get-new-news-hook)
6491   ;; We might read in new NoCeM messages here.
6492   (when (and gnus-use-nocem 
6493              (null arg))
6494     (gnus-nocem-scan-groups))
6495   ;; If ARG is not a number, then we read the active file.
6496   (when (and arg (not (numberp arg)))
6497     (let ((gnus-read-active-file t))
6498       (gnus-read-active-file))
6499     (setq arg nil))
6500
6501   (setq arg (gnus-group-default-level arg t))
6502   (if (and gnus-read-active-file (not arg))
6503       (progn
6504         (gnus-read-active-file)
6505         (gnus-get-unread-articles arg))
6506     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6507       (gnus-get-unread-articles arg)))
6508   (run-hooks 'gnus-after-getting-new-news-hook)
6509   (gnus-group-list-groups))
6510
6511 (defun gnus-group-get-new-news-this-group (&optional n)
6512   "Check for newly arrived news in the current group (and the N-1 next groups).
6513 The difference between N and the number of newsgroup checked is returned.
6514 If N is negative, this group and the N-1 previous groups will be checked."
6515   (interactive "P")
6516   (let* ((groups (gnus-group-process-prefix n))
6517          (ret (if (numberp n) (- n (length groups)) 0))
6518          (beg (unless n (point)))
6519          group)
6520     (while (setq group (pop groups))
6521       (gnus-group-remove-mark group)
6522       (if (gnus-activate-group group 'scan)
6523           (progn
6524             (gnus-get-unread-articles-in-group
6525              (gnus-get-info group) (gnus-active group) t)
6526             (unless (gnus-virtual-group-p group)
6527               (gnus-close-group group))
6528             (gnus-group-update-group group))
6529         (gnus-error 3 "%s error: %s" group (gnus-status-message group))))
6530     (when beg (goto-char beg))
6531     (when gnus-goto-next-group-when-activating
6532       (gnus-group-next-unread-group 1 t))
6533     (gnus-summary-position-point)
6534     ret))
6535
6536 (defun gnus-group-fetch-faq (group &optional faq-dir)
6537   "Fetch the FAQ for the current group."
6538   (interactive
6539    (list
6540     (and (gnus-group-group-name)
6541          (gnus-group-real-name (gnus-group-group-name)))
6542     (cond (current-prefix-arg
6543            (completing-read
6544             "Faq dir: " (and (listp gnus-group-faq-directory)
6545                              (mapcar (lambda (file) (list file))
6546                                      gnus-group-faq-directory)))))))
6547   (or faq-dir
6548       (setq faq-dir (if (listp gnus-group-faq-directory)
6549                         (car gnus-group-faq-directory)
6550                       gnus-group-faq-directory)))
6551   (or group (error "No group name given"))
6552   (let ((file (concat (file-name-as-directory faq-dir)
6553                       (gnus-group-real-name group))))
6554     (if (not (file-exists-p file))
6555         (error "No such file: %s" file)
6556       (find-file file))))
6557
6558 (defun gnus-group-describe-group (force &optional group)
6559   "Display a description of the current newsgroup."
6560   (interactive (list current-prefix-arg (gnus-group-group-name)))
6561   (when (and force
6562              gnus-description-hashtb)
6563     (gnus-sethash group nil gnus-description-hashtb))
6564   (let ((method (gnus-find-method-for-group group))
6565         desc)
6566     (or group (error "No group name given"))
6567     (and (or (and gnus-description-hashtb
6568                   ;; We check whether this group's method has been
6569                   ;; queried for a description file.
6570                   (gnus-gethash
6571                    (gnus-group-prefixed-name "" method)
6572                    gnus-description-hashtb))
6573              (setq desc (gnus-group-get-description group))
6574              (gnus-read-descriptions-file method))
6575          (gnus-message 1
6576           (or desc (gnus-gethash group gnus-description-hashtb)
6577               "No description available")))))
6578
6579 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6580 (defun gnus-group-describe-all-groups (&optional force)
6581   "Pop up a buffer with descriptions of all newsgroups."
6582   (interactive "P")
6583   (and force (setq gnus-description-hashtb nil))
6584   (if (not (or gnus-description-hashtb
6585                (gnus-read-all-descriptions-files)))
6586       (error "Couldn't request descriptions file"))
6587   (let ((buffer-read-only nil)
6588         b)
6589     (erase-buffer)
6590     (mapatoms
6591      (lambda (group)
6592        (setq b (point))
6593        (insert (format "      *: %-20s %s\n" (symbol-name group)
6594                        (symbol-value group)))
6595        (gnus-add-text-properties
6596         b (1+ b) (list 'gnus-group group
6597                        'gnus-unread t 'gnus-marked nil
6598                        'gnus-level (1+ gnus-level-subscribed))))
6599      gnus-description-hashtb)
6600     (goto-char (point-min))
6601     (gnus-group-position-point)))
6602
6603 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6604 (defun gnus-group-apropos (regexp &optional search-description)
6605   "List all newsgroups that have names that match a regexp."
6606   (interactive "sGnus apropos (regexp): ")
6607   (let ((prev "")
6608         (obuf (current-buffer))
6609         groups des)
6610     ;; Go through all newsgroups that are known to Gnus.
6611     (mapatoms
6612      (lambda (group)
6613        (and (symbol-name group)
6614             (string-match regexp (symbol-name group))
6615             (setq groups (cons (symbol-name group) groups))))
6616      gnus-active-hashtb)
6617     ;; Also go through all descriptions that are known to Gnus.
6618     (when search-description
6619       (mapatoms
6620        (lambda (group)
6621          (and (string-match regexp (symbol-value group))
6622               (gnus-active (symbol-name group))
6623               (setq groups (cons (symbol-name group) groups))))
6624        gnus-description-hashtb))
6625     (if (not groups)
6626         (gnus-message 3 "No groups matched \"%s\"." regexp)
6627       ;; Print out all the groups.
6628       (save-excursion
6629         (pop-to-buffer "*Gnus Help*")
6630         (buffer-disable-undo (current-buffer))
6631         (erase-buffer)
6632         (setq groups (sort groups 'string<))
6633         (while groups
6634           ;; Groups may be entered twice into the list of groups.
6635           (if (not (string= (car groups) prev))
6636               (progn
6637                 (insert (setq prev (car groups)) "\n")
6638                 (if (and gnus-description-hashtb
6639                          (setq des (gnus-gethash (car groups)
6640                                                  gnus-description-hashtb)))
6641                     (insert "  " des "\n"))))
6642           (setq groups (cdr groups)))
6643         (goto-char (point-min))))
6644     (pop-to-buffer obuf)))
6645
6646 (defun gnus-group-description-apropos (regexp)
6647   "List all newsgroups that have names or descriptions that match a regexp."
6648   (interactive "sGnus description apropos (regexp): ")
6649   (if (not (or gnus-description-hashtb
6650                (gnus-read-all-descriptions-files)))
6651       (error "Couldn't request descriptions file"))
6652   (gnus-group-apropos regexp t))
6653
6654 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6655 (defun gnus-group-list-matching (level regexp &optional all lowest)
6656   "List all groups with unread articles that match REGEXP.
6657 If the prefix LEVEL is non-nil, it should be a number that says which
6658 level to cut off listing groups.
6659 If ALL, also list groups with no unread articles.
6660 If LOWEST, don't list groups with level lower than LOWEST.
6661
6662 This command may read the active file."
6663   (interactive "P\nsList newsgroups matching: ")
6664   ;; First make sure active file has been read.
6665   (when (and level
6666              (> (prefix-numeric-value level) gnus-level-killed))
6667     (gnus-get-killed-groups))
6668   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6669                            all (or lowest 1) regexp)
6670   (goto-char (point-min))
6671   (gnus-group-position-point))
6672
6673 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6674   "List all groups that match REGEXP.
6675 If the prefix LEVEL is non-nil, it should be a number that says which
6676 level to cut off listing groups.
6677 If LOWEST, don't list groups with level lower than LOWEST."
6678   (interactive "P\nsList newsgroups matching: ")
6679   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6680
6681 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6682 (defun gnus-group-save-newsrc (&optional force)
6683   "Save the Gnus startup files.
6684 If FORCE, force saving whether it is necessary or not."
6685   (interactive "P")
6686   (gnus-save-newsrc-file force))
6687
6688 (defun gnus-group-restart (&optional arg)
6689   "Force Gnus to read the .newsrc file."
6690   (interactive "P")
6691   (when (gnus-yes-or-no-p
6692          (format "Are you sure you want to read %s? "
6693                  gnus-current-startup-file))
6694     (gnus-save-newsrc-file)
6695     (gnus-setup-news 'force)
6696     (gnus-group-list-groups arg)))
6697
6698 (defun gnus-group-read-init-file ()
6699   "Read the Gnus elisp init file."
6700   (interactive)
6701   (gnus-read-init-file))
6702
6703 (defun gnus-group-check-bogus-groups (&optional silent)
6704   "Check bogus newsgroups.
6705 If given a prefix, don't ask for confirmation before removing a bogus
6706 group."
6707   (interactive "P")
6708   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6709   (gnus-group-list-groups))
6710
6711 (defun gnus-group-edit-global-kill (&optional article group)
6712   "Edit the global kill file.
6713 If GROUP, edit that local kill file instead."
6714   (interactive "P")
6715   (setq gnus-current-kill-article article)
6716   (gnus-kill-file-edit-file group)
6717   (gnus-message
6718    6
6719    (substitute-command-keys
6720     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6721             (if group "local" "global")))))
6722
6723 (defun gnus-group-edit-local-kill (article group)
6724   "Edit a local kill file."
6725   (interactive (list nil (gnus-group-group-name)))
6726   (gnus-group-edit-global-kill article group))
6727
6728 (defun gnus-group-force-update ()
6729   "Update `.newsrc' file."
6730   (interactive)
6731   (gnus-save-newsrc-file))
6732
6733 (defun gnus-group-suspend ()
6734   "Suspend the current Gnus session.
6735 In fact, cleanup buffers except for group mode buffer.
6736 The hook gnus-suspend-gnus-hook is called before actually suspending."
6737   (interactive)
6738   (run-hooks 'gnus-suspend-gnus-hook)
6739   ;; Kill Gnus buffers except for group mode buffer.
6740   (let* ((group-buf (get-buffer gnus-group-buffer))
6741          ;; Do this on a separate list in case the user does a ^G before we finish
6742          (gnus-buffer-list
6743           (delete group-buf (delete gnus-dribble-buffer
6744                                     (append gnus-buffer-list nil)))))
6745     (while gnus-buffer-list
6746       (gnus-kill-buffer (pop gnus-buffer-list)))
6747     (gnus-kill-gnus-frames)
6748     (when group-buf
6749       (setq gnus-buffer-list (list group-buf))
6750       (bury-buffer group-buf)
6751       (delete-windows-on group-buf t))))
6752
6753 (defun gnus-group-clear-dribble ()
6754   "Clear all information from the dribble buffer."
6755   (interactive)
6756   (gnus-dribble-clear)
6757   (gnus-message 7 "Cleared dribble buffer"))
6758
6759 (defun gnus-group-exit ()
6760   "Quit reading news after updating .newsrc.eld and .newsrc.
6761 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6762   (interactive)
6763   (when 
6764       (or noninteractive                ;For gnus-batch-kill
6765           (not gnus-interactive-exit)   ;Without confirmation
6766           gnus-expert-user
6767           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6768     (run-hooks 'gnus-exit-gnus-hook)
6769     ;; Offer to save data from non-quitted summary buffers.
6770     (gnus-offer-save-summaries)
6771     ;; Save the newsrc file(s).
6772     (gnus-save-newsrc-file)
6773     ;; Kill-em-all.
6774     (gnus-close-backends)
6775     ;; Reset everything.
6776     (gnus-clear-system)
6777     ;; Allow the user to do things after cleaning up.
6778     (run-hooks 'gnus-after-exiting-gnus-hook)))
6779
6780 (defun gnus-close-backends ()
6781   ;; Send a close request to all backends that support such a request.
6782   (let ((methods gnus-valid-select-methods)
6783         func)
6784     (while methods
6785       (if (fboundp (setq func (intern (concat (caar methods)
6786                                               "-request-close"))))
6787           (funcall func))
6788       (setq methods (cdr methods)))))
6789
6790 (defun gnus-group-quit ()
6791   "Quit reading news without updating .newsrc.eld or .newsrc.
6792 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6793   (interactive)
6794   (when (or noninteractive              ;For gnus-batch-kill
6795             (zerop (buffer-size))
6796             (not (gnus-server-opened gnus-select-method))
6797             gnus-expert-user
6798             (not gnus-current-startup-file)
6799             (gnus-yes-or-no-p
6800              (format "Quit reading news without saving %s? "
6801                      (file-name-nondirectory gnus-current-startup-file))))
6802     (run-hooks 'gnus-exit-gnus-hook)
6803     (if gnus-use-full-window
6804         (delete-other-windows)
6805       (gnus-remove-some-windows))
6806     (gnus-dribble-save)
6807     (gnus-close-backends)
6808     (gnus-clear-system)
6809     ;; Allow the user to do things after cleaning up.
6810     (run-hooks 'gnus-after-exiting-gnus-hook)))
6811
6812 (defun gnus-offer-save-summaries ()
6813   "Offer to save all active summary buffers."
6814   (save-excursion
6815     (let ((buflist (buffer-list))
6816           buffers bufname)
6817       ;; Go through all buffers and find all summaries.
6818       (while buflist
6819         (and (setq bufname (buffer-name (car buflist)))
6820              (string-match "Summary" bufname)
6821              (save-excursion
6822                (set-buffer bufname)
6823                ;; We check that this is, indeed, a summary buffer.
6824                (and (eq major-mode 'gnus-summary-mode)
6825                     ;; Also make sure this isn't bogus.
6826                     gnus-newsgroup-prepared))
6827              (push bufname buffers))
6828         (setq buflist (cdr buflist)))
6829       ;; Go through all these summary buffers and offer to save them.
6830       (when buffers
6831         (map-y-or-n-p
6832          "Update summary buffer %s? "
6833          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6834          buffers)))))
6835
6836 (defun gnus-group-describe-briefly ()
6837   "Give a one line description of the group mode commands."
6838   (interactive)
6839   (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")))
6840
6841 (defun gnus-group-browse-foreign-server (method)
6842   "Browse a foreign news server.
6843 If called interactively, this function will ask for a select method
6844  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6845 If not, METHOD should be a list where the first element is the method
6846 and the second element is the address."
6847   (interactive
6848    (list (let ((how (completing-read
6849                      "Which backend: "
6850                      (append gnus-valid-select-methods gnus-server-alist)
6851                      nil t (cons "nntp" 0) 'gnus-method-history)))
6852            ;; We either got a backend name or a virtual server name.
6853            ;; If the first, we also need an address.
6854            (if (assoc how gnus-valid-select-methods)
6855                (list (intern how)
6856                      ;; Suggested by mapjph@bath.ac.uk.
6857                      (completing-read
6858                       "Address: "
6859                       (mapcar (lambda (server) (list server))
6860                               gnus-secondary-servers)))
6861              ;; We got a server name, so we find the method.
6862              (gnus-server-to-method how)))))
6863   (gnus-browse-foreign-server method))
6864
6865 \f
6866 ;;;
6867 ;;; Gnus summary mode
6868 ;;;
6869
6870 (defvar gnus-summary-mode-map nil)
6871
6872 (put 'gnus-summary-mode 'mode-class 'special)
6873
6874 (unless gnus-summary-mode-map
6875   (setq gnus-summary-mode-map (make-keymap))
6876   (suppress-keymap gnus-summary-mode-map)
6877
6878   ;; Non-orthogonal keys
6879
6880   (gnus-define-keys gnus-summary-mode-map
6881     " " gnus-summary-next-page
6882     "\177" gnus-summary-prev-page
6883     [delete] gnus-summary-prev-page
6884     "\r" gnus-summary-scroll-up
6885     "n" gnus-summary-next-unread-article
6886     "p" gnus-summary-prev-unread-article
6887     "N" gnus-summary-next-article
6888     "P" gnus-summary-prev-article
6889     "\M-\C-n" gnus-summary-next-same-subject
6890     "\M-\C-p" gnus-summary-prev-same-subject
6891     "\M-n" gnus-summary-next-unread-subject
6892     "\M-p" gnus-summary-prev-unread-subject
6893     "." gnus-summary-first-unread-article
6894     "," gnus-summary-best-unread-article
6895     "\M-s" gnus-summary-search-article-forward
6896     "\M-r" gnus-summary-search-article-backward
6897     "<" gnus-summary-beginning-of-article
6898     ">" gnus-summary-end-of-article
6899     "j" gnus-summary-goto-article
6900     "^" gnus-summary-refer-parent-article
6901     "\M-^" gnus-summary-refer-article
6902     "u" gnus-summary-tick-article-forward
6903     "!" gnus-summary-tick-article-forward
6904     "U" gnus-summary-tick-article-backward
6905     "d" gnus-summary-mark-as-read-forward
6906     "D" gnus-summary-mark-as-read-backward
6907     "E" gnus-summary-mark-as-expirable
6908     "\M-u" gnus-summary-clear-mark-forward
6909     "\M-U" gnus-summary-clear-mark-backward
6910     "k" gnus-summary-kill-same-subject-and-select
6911     "\C-k" gnus-summary-kill-same-subject
6912     "\M-\C-k" gnus-summary-kill-thread
6913     "\M-\C-l" gnus-summary-lower-thread
6914     "e" gnus-summary-edit-article
6915     "#" gnus-summary-mark-as-processable
6916     "\M-#" gnus-summary-unmark-as-processable
6917     "\M-\C-t" gnus-summary-toggle-threads
6918     "\M-\C-s" gnus-summary-show-thread
6919     "\M-\C-h" gnus-summary-hide-thread
6920     "\M-\C-f" gnus-summary-next-thread
6921     "\M-\C-b" gnus-summary-prev-thread
6922     "\M-\C-u" gnus-summary-up-thread
6923     "\M-\C-d" gnus-summary-down-thread
6924     "&" gnus-summary-execute-command
6925     "c" gnus-summary-catchup-and-exit
6926     "\C-w" gnus-summary-mark-region-as-read
6927     "\C-t" gnus-summary-toggle-truncation
6928     "?" gnus-summary-mark-as-dormant
6929     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6930     "\C-c\C-s\C-n" gnus-summary-sort-by-number
6931     "\C-c\C-s\C-a" gnus-summary-sort-by-author
6932     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6933     "\C-c\C-s\C-d" gnus-summary-sort-by-date
6934     "\C-c\C-s\C-i" gnus-summary-sort-by-score
6935     "=" gnus-summary-expand-window
6936     "\C-x\C-s" gnus-summary-reselect-current-group
6937     "\M-g" gnus-summary-rescan-group
6938     "w" gnus-summary-stop-page-breaking
6939     "\C-c\C-r" gnus-summary-caesar-message
6940     "\M-t" gnus-summary-toggle-mime
6941     "f" gnus-summary-followup
6942     "F" gnus-summary-followup-with-original
6943     "C" gnus-summary-cancel-article
6944     "r" gnus-summary-reply
6945     "R" gnus-summary-reply-with-original
6946     "\C-c\C-f" gnus-summary-mail-forward
6947     "o" gnus-summary-save-article
6948     "\C-o" gnus-summary-save-article-mail
6949     "|" gnus-summary-pipe-output
6950     "\M-k" gnus-summary-edit-local-kill
6951     "\M-K" gnus-summary-edit-global-kill
6952     "V" gnus-version
6953     "\C-c\C-d" gnus-summary-describe-group
6954     "q" gnus-summary-exit
6955     "Q" gnus-summary-exit-no-update
6956     "\C-c\C-i" gnus-info-find-node
6957     gnus-mouse-2 gnus-mouse-pick-article
6958     "m" gnus-summary-mail-other-window
6959     "a" gnus-summary-post-news
6960     "x" gnus-summary-limit-to-unread
6961     "s" gnus-summary-isearch-article
6962     "t" gnus-article-hide-headers
6963     "g" gnus-summary-show-article
6964     "l" gnus-summary-goto-last-article
6965     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6966     "\C-d" gnus-summary-enter-digest-group
6967     "\C-c\C-b" gnus-bug
6968     "*" gnus-cache-enter-article
6969     "\M-*" gnus-cache-remove-article
6970     "\M-&" gnus-summary-universal-argument
6971     "\C-l" gnus-recenter
6972     "I" gnus-summary-increase-score
6973     "L" gnus-summary-lower-score
6974
6975     "V" gnus-summary-score-map
6976     "X" gnus-uu-extract-map
6977     "S" gnus-summary-send-map)
6978
6979   ;; Sort of orthogonal keymap
6980   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
6981     "t" gnus-summary-tick-article-forward
6982     "!" gnus-summary-tick-article-forward
6983     "d" gnus-summary-mark-as-read-forward
6984     "r" gnus-summary-mark-as-read-forward
6985     "c" gnus-summary-clear-mark-forward
6986     " " gnus-summary-clear-mark-forward
6987     "e" gnus-summary-mark-as-expirable
6988     "x" gnus-summary-mark-as-expirable
6989     "?" gnus-summary-mark-as-dormant
6990     "b" gnus-summary-set-bookmark
6991     "B" gnus-summary-remove-bookmark
6992     "#" gnus-summary-mark-as-processable
6993     "\M-#" gnus-summary-unmark-as-processable
6994     "S" gnus-summary-limit-include-expunged
6995     "C" gnus-summary-catchup
6996     "H" gnus-summary-catchup-to-here
6997     "\C-c" gnus-summary-catchup-all
6998     "k" gnus-summary-kill-same-subject-and-select
6999     "K" gnus-summary-kill-same-subject
7000     "P" gnus-uu-mark-map)
7001
7002   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
7003     "c" gnus-summary-clear-above
7004     "u" gnus-summary-tick-above
7005     "m" gnus-summary-mark-above
7006     "k" gnus-summary-kill-below)
7007
7008   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
7009     "/" gnus-summary-limit-to-subject
7010     "n" gnus-summary-limit-to-articles
7011     "w" gnus-summary-pop-limit
7012     "s" gnus-summary-limit-to-subject
7013     "a" gnus-summary-limit-to-author
7014     "u" gnus-summary-limit-to-unread
7015     "m" gnus-summary-limit-to-marks
7016     "v" gnus-summary-limit-to-score
7017     "D" gnus-summary-limit-include-dormant
7018     "d" gnus-summary-limit-exclude-dormant
7019     ;;  "t" gnus-summary-limit-exclude-thread
7020     "E" gnus-summary-limit-include-expunged
7021     "c" gnus-summary-limit-exclude-childless-dormant
7022     "C" gnus-summary-limit-mark-excluded-as-read)
7023
7024   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
7025     "n" gnus-summary-next-unread-article
7026     "p" gnus-summary-prev-unread-article
7027     "N" gnus-summary-next-article
7028     "P" gnus-summary-prev-article
7029     "\C-n" gnus-summary-next-same-subject
7030     "\C-p" gnus-summary-prev-same-subject
7031     "\M-n" gnus-summary-next-unread-subject
7032     "\M-p" gnus-summary-prev-unread-subject
7033     "f" gnus-summary-first-unread-article
7034     "b" gnus-summary-best-unread-article
7035     "j" gnus-summary-goto-article
7036     "g" gnus-summary-goto-subject
7037     "l" gnus-summary-goto-last-article
7038     "p" gnus-summary-pop-article)
7039
7040   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
7041     "k" gnus-summary-kill-thread
7042     "l" gnus-summary-lower-thread
7043     "i" gnus-summary-raise-thread
7044     "T" gnus-summary-toggle-threads
7045     "t" gnus-summary-rethread-current
7046     "^" gnus-summary-reparent-thread
7047     "s" gnus-summary-show-thread
7048     "S" gnus-summary-show-all-threads
7049     "h" gnus-summary-hide-thread
7050     "H" gnus-summary-hide-all-threads
7051     "n" gnus-summary-next-thread
7052     "p" gnus-summary-prev-thread
7053     "u" gnus-summary-up-thread
7054     "o" gnus-summary-top-thread
7055     "d" gnus-summary-down-thread
7056     "#" gnus-uu-mark-thread
7057     "\M-#" gnus-uu-unmark-thread)
7058
7059   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
7060     "c" gnus-summary-catchup-and-exit
7061     "C" gnus-summary-catchup-all-and-exit
7062     "E" gnus-summary-exit-no-update
7063     "Q" gnus-summary-exit
7064     "Z" gnus-summary-exit
7065     "n" gnus-summary-catchup-and-goto-next-group
7066     "R" gnus-summary-reselect-current-group
7067     "G" gnus-summary-rescan-group
7068     "N" gnus-summary-next-group
7069     "P" gnus-summary-prev-group)
7070
7071   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
7072     " " gnus-summary-next-page
7073     "n" gnus-summary-next-page
7074     "\177" gnus-summary-prev-page
7075     [delete] gnus-summary-prev-page
7076     "p" gnus-summary-prev-page
7077     "\r" gnus-summary-scroll-up
7078     "<" gnus-summary-beginning-of-article
7079     ">" gnus-summary-end-of-article
7080     "b" gnus-summary-beginning-of-article
7081     "e" gnus-summary-end-of-article
7082     "^" gnus-summary-refer-parent-article
7083     "r" gnus-summary-refer-parent-article
7084     "R" gnus-summary-refer-references
7085     "g" gnus-summary-show-article
7086     "s" gnus-summary-isearch-article)
7087
7088   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
7089     "b" gnus-article-add-buttons
7090     "B" gnus-article-add-buttons-to-head
7091     "o" gnus-article-treat-overstrike
7092     ;;  "w" gnus-article-word-wrap
7093     "w" gnus-article-fill-cited-article
7094     "c" gnus-article-remove-cr
7095     "L" gnus-article-remove-trailing-blank-lines
7096     "q" gnus-article-de-quoted-unreadable
7097     "f" gnus-article-display-x-face
7098     "l" gnus-summary-stop-page-breaking
7099     "r" gnus-summary-caesar-message
7100     "t" gnus-article-hide-headers
7101     "v" gnus-summary-verbose-headers
7102     "m" gnus-summary-toggle-mime)
7103
7104   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
7105     "a" gnus-article-hide
7106     "h" gnus-article-hide-headers
7107     "b" gnus-article-hide-boring-headers
7108     "s" gnus-article-hide-signature
7109     "c" gnus-article-hide-citation
7110     "p" gnus-article-hide-pgp
7111     "\C-c" gnus-article-hide-citation-maybe)
7112
7113   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
7114     "a" gnus-article-highlight
7115     "h" gnus-article-highlight-headers
7116     "c" gnus-article-highlight-citation
7117     "s" gnus-article-highlight-signature)
7118
7119   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
7120     "z" gnus-article-date-ut
7121     "u" gnus-article-date-ut
7122     "l" gnus-article-date-local
7123     "e" gnus-article-date-lapsed
7124     "o" gnus-article-date-original)
7125
7126   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
7127     "v" gnus-version
7128     "f" gnus-summary-fetch-faq
7129     "d" gnus-summary-describe-group
7130     "h" gnus-summary-describe-briefly
7131     "i" gnus-info-find-node)
7132
7133   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
7134     "e" gnus-summary-expire-articles
7135     "\M-\C-e" gnus-summary-expire-articles-now
7136     "\177" gnus-summary-delete-article
7137     [delete] gnus-summary-delete-article
7138     "m" gnus-summary-move-article
7139     "r" gnus-summary-respool-article
7140     "w" gnus-summary-edit-article
7141     "c" gnus-summary-copy-article
7142     "B" gnus-summary-crosspost-article
7143     "q" gnus-summary-respool-query
7144     "i" gnus-summary-import-article)
7145
7146   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
7147     "o" gnus-summary-save-article
7148     "m" gnus-summary-save-article-mail
7149     "r" gnus-summary-save-article-rmail
7150     "f" gnus-summary-save-article-file
7151     "b" gnus-summary-save-article-body-file
7152     "h" gnus-summary-save-article-folder
7153     "v" gnus-summary-save-article-vm
7154     "p" gnus-summary-pipe-output
7155     "s" gnus-soup-add-article)
7156   )
7157
7158 \f
7159
7160 (defun gnus-summary-mode (&optional group)
7161   "Major mode for reading articles.
7162
7163 All normal editing commands are switched off.
7164 \\<gnus-summary-mode-map>
7165 Each line in this buffer represents one article.  To read an
7166 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
7167 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
7168 respectively.
7169
7170 You can also post articles and send mail from this buffer.  To
7171 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
7172 of an article, type `\\[gnus-summary-reply]'.
7173
7174 There are approx. one gazillion commands you can execute in this
7175 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
7176
7177 The following commands are available:
7178
7179 \\{gnus-summary-mode-map}"
7180   (interactive)
7181   (when (and menu-bar-mode
7182              (gnus-visual-p 'summary-menu 'menu))
7183     (gnus-summary-make-menu-bar))
7184   (kill-all-local-variables)
7185   (gnus-summary-make-local-variables)
7186   (gnus-make-thread-indent-array)
7187   (gnus-simplify-mode-line)
7188   (setq major-mode 'gnus-summary-mode)
7189   (setq mode-name "Summary")
7190   (make-local-variable 'minor-mode-alist)
7191   (use-local-map gnus-summary-mode-map)
7192   (buffer-disable-undo (current-buffer))
7193   (setq buffer-read-only t)             ;Disable modification
7194   (setq truncate-lines t)
7195   (setq selective-display t)
7196   (setq selective-display-ellipses t)   ;Display `...'
7197   (setq buffer-display-table gnus-summary-display-table)
7198   (setq gnus-newsgroup-name group)
7199   (make-local-variable 'gnus-summary-line-format)
7200   (make-local-variable 'gnus-summary-line-format-spec)
7201   (make-local-variable 'gnus-summary-mark-positions)
7202   (run-hooks 'gnus-summary-mode-hook))
7203
7204 (defun gnus-summary-make-local-variables ()
7205   "Make all the local summary buffer variables."
7206   (let ((locals gnus-summary-local-variables)
7207         global local)
7208     (while (setq local (pop locals))
7209       (if (consp local)
7210           (progn
7211             (if (eq (cdr local) 'global)
7212                 ;; Copy the global value of the variable.
7213                 (setq global (symbol-value (car local)))
7214               ;; Use the value from the list.
7215               (setq global (eval (cdr local))))
7216             (make-local-variable (car local))
7217             (set (car local) global))
7218         ;; Simple nil-valued local variable.
7219         (make-local-variable local)
7220         (set local nil)))))
7221
7222 (defun gnus-summary-make-display-table ()
7223   ;; Change the display table.  Odd characters have a tendency to mess
7224   ;; up nicely formatted displays - we make all possible glyphs
7225   ;; display only a single character.
7226
7227   ;; We start from the standard display table, if any.
7228   (setq gnus-summary-display-table
7229         (or (copy-sequence standard-display-table)
7230             (make-display-table)))
7231   ;; Nix out all the control chars...
7232   (let ((i 32))
7233     (while (>= (setq i (1- i)) 0)
7234       (aset gnus-summary-display-table i [??])))
7235   ;; ... but not newline and cr, of course. (cr is necessary for the
7236   ;; selective display).
7237   (aset gnus-summary-display-table ?\n nil)
7238   (aset gnus-summary-display-table ?\r nil)
7239   ;; We nix out any glyphs over 126 that are not set already.
7240   (let ((i 256))
7241     (while (>= (setq i (1- i)) 127)
7242       ;; Only modify if the entry is nil.
7243       (or (aref gnus-summary-display-table i)
7244           (aset gnus-summary-display-table i [??])))))
7245
7246 (defun gnus-summary-clear-local-variables ()
7247   (let ((locals gnus-summary-local-variables))
7248     (while locals
7249       (if (consp (car locals))
7250           (and (vectorp (caar locals))
7251                (set (caar locals) nil))
7252         (and (vectorp (car locals))
7253              (set (car locals) nil)))
7254       (setq locals (cdr locals)))))
7255
7256 ;; Summary data functions.
7257
7258 (defmacro gnus-data-number (data)
7259   `(car ,data))
7260
7261 (defmacro gnus-data-set-number (data number)
7262   `(setcar ,data ,number))
7263
7264 (defmacro gnus-data-mark (data)
7265   `(nth 1 ,data))
7266
7267 (defmacro gnus-data-set-mark (data mark)
7268   `(setcar (nthcdr 1 ,data) ,mark))
7269
7270 (defmacro gnus-data-pos (data)
7271   `(nth 2 ,data))
7272
7273 (defmacro gnus-data-set-pos (data pos)
7274   `(setcar (nthcdr 2 ,data) ,pos))
7275
7276 (defmacro gnus-data-header (data)
7277   `(nth 3 ,data))
7278
7279 (defmacro gnus-data-level (data)
7280   `(nth 4 ,data))
7281
7282 (defmacro gnus-data-unread-p (data)
7283   `(= (nth 1 ,data) gnus-unread-mark))
7284
7285 (defmacro gnus-data-pseudo-p (data)
7286   `(consp (nth 3 ,data)))
7287
7288 (defmacro gnus-data-find (number)
7289   `(assq ,number gnus-newsgroup-data))
7290
7291 (defmacro gnus-data-find-list (number &optional data)
7292   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7293      (memq (assq ,number bdata)
7294            bdata)))
7295
7296 (defmacro gnus-data-make (number mark pos header level)
7297   `(list ,number ,mark ,pos ,header ,level))
7298
7299 (defun gnus-data-enter (after-article number mark pos header level offset)
7300   (let ((data (gnus-data-find-list after-article)))
7301     (or data (error "No such article: %d" after-article))
7302     (setcdr data (cons (gnus-data-make number mark pos header level)
7303                        (cdr data)))
7304     (setq gnus-newsgroup-data-reverse nil)
7305     (gnus-data-update-list (cddr data) offset)))
7306
7307 (defun gnus-data-enter-list (after-article list &optional offset)
7308   (when list
7309     (let ((data (and after-article (gnus-data-find-list after-article)))
7310           (ilist list))
7311       (or data (not after-article) (error "No such article: %d" after-article))
7312       ;; Find the last element in the list to be spliced into the main
7313       ;; list.
7314       (while (cdr list)
7315         (setq list (cdr list)))
7316       (if (not data)
7317           (progn
7318             (setcdr list gnus-newsgroup-data)
7319             (setq gnus-newsgroup-data ilist)
7320             (and offset (gnus-data-update-list (cdr list) offset)))
7321         (setcdr list (cdr data))
7322         (setcdr data ilist)
7323         (and offset (gnus-data-update-list (cdr data) offset)))
7324       (setq gnus-newsgroup-data-reverse nil))))
7325
7326 (defun gnus-data-remove (article &optional offset)
7327   (let ((data gnus-newsgroup-data))
7328     (if (= (gnus-data-number (car data)) article)
7329         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7330               gnus-newsgroup-data-reverse nil)
7331       (while (cdr data)
7332         (and (= (gnus-data-number (cadr data)) article)
7333              (progn
7334                (setcdr data (cddr data))
7335                (and offset (gnus-data-update-list (cdr data) offset))
7336                (setq data nil
7337                      gnus-newsgroup-data-reverse nil)))
7338         (setq data (cdr data))))))
7339
7340 (defmacro gnus-data-list (backward)
7341   `(if ,backward
7342        (or gnus-newsgroup-data-reverse
7343            (setq gnus-newsgroup-data-reverse
7344                  (reverse gnus-newsgroup-data)))
7345      gnus-newsgroup-data))
7346
7347 (defun gnus-data-update-list (data offset)
7348   "Add OFFSET to the POS of all data entries in DATA."
7349   (while data
7350     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7351     (setq data (cdr data))))
7352
7353 (defun gnus-data-compute-positions ()
7354   "Compute the positions of all articles."
7355   (let ((data gnus-newsgroup-data)
7356         pos)
7357     (while data
7358       (when (setq pos (text-property-any
7359                        (point-min) (point-max)
7360                        'gnus-number (gnus-data-number (car data))))
7361         (gnus-data-set-pos (car data) (+ pos 3)))
7362       (setq data (cdr data)))))
7363
7364 (defun gnus-summary-article-pseudo-p (article)
7365   "Say whether this article is a pseudo article or not."
7366   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7367
7368 (defun gnus-article-parent-p (number)
7369   "Say whether this article is a parent or not."
7370   (let ((data (gnus-data-find-list number)))
7371     (and (cdr data)                     ; There has to be an article after...
7372          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7373             (gnus-data-level (nth 1 data))))))
7374
7375 (defun gnus-article-children (number)
7376   "Return a list of all children to NUMBER."
7377   (let* ((data (gnus-data-find-list number))
7378          (level (gnus-data-level (car data)))
7379          children)
7380     (setq data (cdr data))
7381     (while (and data            
7382                 (= (gnus-data-level (car data)) (1+ level)))
7383       (push (gnus-data-number (car data)) children)
7384       (setq data (cdr data)))
7385     children))
7386
7387 (defmacro gnus-summary-skip-intangible ()
7388   "If the current article is intangible, then jump to a different article."
7389   '(let ((to (get-text-property (point) 'gnus-intangible)))
7390     (and to (gnus-summary-goto-subject to))))
7391
7392 (defmacro gnus-summary-article-intangible-p ()
7393   "Say whether this article is intangible or not."
7394   '(get-text-property (point) 'gnus-intangible))
7395
7396 ;; Some summary mode macros.
7397
7398 (defmacro gnus-summary-article-number ()
7399   "The article number of the article on the current line.
7400 If there isn's an article number here, then we return the current
7401 article number."
7402   '(progn
7403      (gnus-summary-skip-intangible)
7404      (or (get-text-property (point) 'gnus-number)
7405          (gnus-summary-last-subject))))
7406
7407 (defmacro gnus-summary-article-header (&optional number)
7408   `(gnus-data-header (gnus-data-find
7409                       ,(or number '(gnus-summary-article-number)))))
7410
7411 (defmacro gnus-summary-thread-level (&optional number)
7412   `(if (and (eq gnus-summary-make-false-root 'dummy)
7413             (get-text-property (point) 'gnus-intangible))
7414        0
7415      (gnus-data-level (gnus-data-find
7416                        ,(or number '(gnus-summary-article-number))))))
7417
7418 (defmacro gnus-summary-article-mark (&optional number)
7419   `(gnus-data-mark (gnus-data-find
7420                     ,(or number '(gnus-summary-article-number)))))
7421
7422 (defmacro gnus-summary-article-pos (&optional number)
7423   `(gnus-data-pos (gnus-data-find
7424                    ,(or number '(gnus-summary-article-number)))))
7425
7426 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7427 (defmacro gnus-summary-article-subject (&optional number)
7428   "Return current subject string or nil if nothing."
7429   `(let ((headers
7430           ,(if number
7431                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7432              '(gnus-data-header (assq (gnus-summary-article-number)
7433                                       gnus-newsgroup-data)))))
7434      (and headers
7435           (vectorp headers)
7436           (mail-header-subject headers))))
7437
7438 (defmacro gnus-summary-article-score (&optional number)
7439   "Return current article score."
7440   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7441                   gnus-newsgroup-scored))
7442        gnus-summary-default-score 0))
7443
7444 (defun gnus-summary-article-children (&optional number)
7445   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7446          (level (gnus-data-level (car data)))
7447          l children)
7448     (while (and (setq data (cdr data))
7449                 (> (setq l (gnus-data-level (car data))) level))
7450       (and (= (1+ level) l)
7451            (setq children (cons (gnus-data-number (car data))
7452                                 children))))
7453     (nreverse children)))
7454
7455 (defun gnus-summary-article-parent (&optional number)
7456   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7457                                     (gnus-data-list t)))
7458          (level (gnus-data-level (car data))))
7459     (if (zerop level)
7460         () ; This is a root.
7461       ;; We search until we find an article with a level less than
7462       ;; this one.  That function has to be the parent.
7463       (while (and (setq data (cdr data))
7464                   (not (< (gnus-data-level (car data)) level))))
7465       (and data (gnus-data-number (car data))))))
7466
7467 (defun gnus-unread-mark-p (mark)
7468   "Say whether MARK is the unread mark."
7469   (= mark gnus-unread-mark))
7470
7471 (defun gnus-read-mark-p (mark)
7472   "Say whether MARK is one of the marks that mark as read.
7473 This is all marks except unread, ticked, dormant, and expirable."
7474   (not (or (= mark gnus-unread-mark)
7475            (= mark gnus-ticked-mark)
7476            (= mark gnus-dormant-mark)
7477            (= mark gnus-expirable-mark))))
7478
7479 ;; Saving hidden threads.
7480
7481 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
7482 (put 'gnus-save-hidden-threads 'lisp-indent-hook 0)
7483 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
7484
7485 (defmacro gnus-save-hidden-threads (&rest forms)
7486   "Save hidden threads, eval FORMS, and restore the hidden threads."
7487   (let ((config (make-symbol "config")))
7488     `(let ((,config (gnus-hidden-threads-configuration)))
7489        (unwind-protect
7490            (progn
7491              ,@forms)
7492          (gnus-restore-hidden-threads-configuration ,config)))))
7493
7494 (defun gnus-hidden-threads-configuration ()
7495   "Return the current hidden threads configuration."
7496   (save-excursion
7497     (let (config)
7498       (goto-char (point-min))
7499       (while (search-forward "\r" nil t)
7500         (push (1- (point)) config))
7501       config)))
7502
7503 (defun gnus-restore-hidden-threads-configuration (config)
7504   "Restore hidden threads configuration from CONFIG."
7505   (let (point buffer-read-only)
7506     (while (setq point (pop config))
7507       (when (and (< point (point-max))
7508                  (goto-char point)
7509                  (= (following-char) ?\n))
7510         (subst-char-in-region point (1+ point) ?\n ?\r)))))
7511
7512 ;; Various summary mode internalish functions.
7513
7514 (defun gnus-mouse-pick-article (e)
7515   (interactive "e")
7516   (mouse-set-point e)
7517   (gnus-summary-next-page nil t))
7518
7519 (defun gnus-summary-setup-buffer (group)
7520   "Initialize summary buffer."
7521   (let ((buffer (concat "*Summary " group "*")))
7522     (if (get-buffer buffer)
7523         (progn
7524           (set-buffer buffer)
7525           (setq gnus-summary-buffer (current-buffer))
7526           (not gnus-newsgroup-prepared))
7527       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7528       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7529       (gnus-add-current-to-buffer-list)
7530       (gnus-summary-mode group)
7531       (when gnus-carpal
7532         (gnus-carpal-setup-buffer 'summary))
7533       (unless gnus-single-article-buffer
7534         (make-local-variable 'gnus-article-buffer)
7535         (make-local-variable 'gnus-article-current)
7536         (make-local-variable 'gnus-original-article-buffer))
7537       (setq gnus-newsgroup-name group)
7538       t)))
7539
7540 (defun gnus-set-global-variables ()
7541   ;; Set the global equivalents of the summary buffer-local variables
7542   ;; to the latest values they had.  These reflect the summary buffer
7543   ;; that was in action when the last article was fetched.
7544   (when (eq major-mode 'gnus-summary-mode)
7545     (setq gnus-summary-buffer (current-buffer))
7546     (let ((name gnus-newsgroup-name)
7547           (marked gnus-newsgroup-marked)
7548           (unread gnus-newsgroup-unreads)
7549           (headers gnus-current-headers)
7550           (data gnus-newsgroup-data)
7551           (summary gnus-summary-buffer)
7552           (article-buffer gnus-article-buffer)
7553           (original gnus-original-article-buffer)
7554           (gac gnus-article-current)
7555           (score-file gnus-current-score-file))
7556       (save-excursion
7557         (set-buffer gnus-group-buffer)
7558         (setq gnus-newsgroup-name name)
7559         (setq gnus-newsgroup-marked marked)
7560         (setq gnus-newsgroup-unreads unread)
7561         (setq gnus-current-headers headers)
7562         (setq gnus-newsgroup-data data)
7563         (setq gnus-article-current gac)
7564         (setq gnus-summary-buffer summary)
7565         (setq gnus-article-buffer article-buffer)
7566         (setq gnus-original-article-buffer original)
7567         (setq gnus-current-score-file score-file)))))
7568
7569 (defun gnus-summary-last-article-p (&optional article)
7570   "Return whether ARTICLE is the last article in the buffer."
7571   (if (not (setq article (or article (gnus-summary-article-number))))
7572       t ; All non-existant numbers are the last article. :-)
7573     (not (cdr (gnus-data-find-list article)))))
7574
7575 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7576   "Insert a dummy root in the summary buffer."
7577   (beginning-of-line)
7578   (gnus-add-text-properties
7579    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7580    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7581
7582 (defun gnus-make-thread-indent-array ()
7583   (let ((n 200))
7584     (unless (and gnus-thread-indent-array
7585                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
7586       (setq gnus-thread-indent-array (make-vector 201 "")
7587             gnus-thread-indent-array-level gnus-thread-indent-level)
7588       (while (>= n 0)
7589         (aset gnus-thread-indent-array n
7590               (make-string (* n gnus-thread-indent-level) ? ))
7591         (setq n (1- n))))))
7592
7593 (defun gnus-summary-insert-line
7594   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7595                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7596                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7597   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7598          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7599          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7600          (gnus-tmp-score-char
7601           (if (or (null gnus-summary-default-score)
7602                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7603                       gnus-summary-zcore-fuzz)) ? 
7604             (if (< gnus-tmp-score gnus-summary-default-score)
7605                 gnus-score-below-mark gnus-score-over-mark)))
7606          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7607                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7608                                   gnus-cached-mark)
7609                                  (gnus-tmp-replied gnus-replied-mark)
7610                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7611                                   gnus-saved-mark)
7612                                  (t gnus-unread-mark)))
7613          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7614          (gnus-tmp-name
7615           (cond
7616            ((string-match "(.+)" gnus-tmp-from)
7617             (substring gnus-tmp-from
7618                        (1+ (match-beginning 0)) (1- (match-end 0))))
7619            ((string-match "<[^>]+> *$" gnus-tmp-from)
7620             (let ((beg (match-beginning 0)))
7621               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7622                        (substring gnus-tmp-from (1+ (match-beginning 0))
7623                                   (1- (match-end 0))))
7624                   (substring gnus-tmp-from 0 beg))))
7625            (t gnus-tmp-from)))
7626          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7627          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7628          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7629          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7630          (buffer-read-only nil))
7631     (when (string= gnus-tmp-name "")
7632       (setq gnus-tmp-name gnus-tmp-from))
7633     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7634     (gnus-put-text-property
7635      (point)
7636      (progn (eval gnus-summary-line-format-spec) (point))
7637      'gnus-number gnus-tmp-number)
7638     (when (gnus-visual-p 'summary-highlight 'highlight)
7639       (forward-line -1)
7640       (run-hooks 'gnus-summary-update-hook)
7641       (forward-line 1))))
7642
7643 (defun gnus-summary-update-line (&optional dont-update)
7644   ;; Update summary line after change.
7645   (when (and gnus-summary-default-score
7646              (not gnus-summary-inhibit-highlight))
7647     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7648            (article (gnus-summary-article-number))
7649            (score (gnus-summary-article-score article)))
7650       (unless dont-update
7651         (if (and gnus-summary-mark-below
7652                  (< (gnus-summary-article-score)
7653                     gnus-summary-mark-below))
7654             ;; This article has a low score, so we mark it as read.
7655             (when (memq article gnus-newsgroup-unreads)
7656               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7657           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7658             ;; This article was previously marked as read on account
7659             ;; of a low score, but now it has risen, so we mark it as
7660             ;; unread.
7661             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7662         (gnus-summary-update-mark
7663          (if (or (null gnus-summary-default-score)
7664                  (<= (abs (- score gnus-summary-default-score))
7665                      gnus-summary-zcore-fuzz)) ? 
7666            (if (< score gnus-summary-default-score)
7667                gnus-score-below-mark gnus-score-over-mark)) 'score))
7668       ;; Do visual highlighting.
7669       (when (gnus-visual-p 'summary-highlight 'highlight)
7670         (run-hooks 'gnus-summary-update-hook)))))
7671
7672 (defvar gnus-tmp-new-adopts nil)
7673
7674 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7675   ;; Sum up all elements (and sub-elements) in a list.
7676   (let* ((number
7677           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7678           (cond
7679            ((and (consp thread) (cdr thread))
7680             (apply
7681              '+ 1 (mapcar
7682                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7683            ((null thread)
7684             1)
7685            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7686             1)
7687            (t 0))))
7688     (when (and level (zerop level) gnus-tmp-new-adopts)
7689       (incf number
7690             (apply '+ (mapcar
7691                        'gnus-summary-number-of-articles-in-thread
7692                        gnus-tmp-new-adopts))))
7693     (if char
7694         (if (> number 1) gnus-not-empty-thread-mark
7695           gnus-empty-thread-mark)
7696       number)))
7697
7698 (defun gnus-summary-set-local-parameters (group)
7699  "Go through the local params of GROUP and set all variable specs in that list."
7700   (let ((params (gnus-info-params (gnus-get-info group)))
7701         elem)
7702     (while params
7703       (setq elem (car params)
7704             params (cdr params))
7705       (and (consp elem)                 ; Has to be a cons.
7706            (consp (cdr elem))           ; The cdr has to be a list.
7707            (symbolp (car elem))         ; Has to be a symbol in there.
7708            (not (memq (car elem) 
7709                       '(quit-config to-address to-list to-group)))
7710            (progn                       ; So we set it.
7711              (make-local-variable (car elem))
7712              (set (car elem) (eval (nth 1 elem))))))))
7713
7714 (defun gnus-summary-read-group (group &optional show-all no-article
7715                                       kill-buffer no-display)
7716   "Start reading news in newsgroup GROUP.
7717 If SHOW-ALL is non-nil, already read articles are also listed.
7718 If NO-ARTICLE is non-nil, no article is selected initially.
7719 If NO-DISPLAY, don't generate a summary buffer."
7720   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7721   (let* ((new-group (gnus-summary-setup-buffer group))
7722          (quit-config (gnus-group-quit-config group))
7723          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7724     (cond
7725      ;; This summary buffer exists already, so we just select it.
7726      ((not new-group)
7727       (gnus-set-global-variables)
7728       (when kill-buffer
7729         (gnus-kill-or-deaden-summary kill-buffer))
7730       (gnus-configure-windows 'summary 'force)
7731       (gnus-set-mode-line 'summary)
7732       (gnus-summary-position-point)
7733       (message "")
7734       t)
7735      ;; We couldn't select this group.
7736      ((null did-select)
7737       (when (and (eq major-mode 'gnus-summary-mode)
7738                  (not (equal (current-buffer) kill-buffer)))
7739         (kill-buffer (current-buffer))
7740         (if (not quit-config)
7741             (progn
7742               (set-buffer gnus-group-buffer)
7743               (gnus-group-jump-to-group group)
7744               (gnus-group-next-unread-group 1))
7745           (if (not (buffer-name (car quit-config)))
7746               (gnus-configure-windows 'group 'force)
7747             (set-buffer (car quit-config))
7748             (and (eq major-mode 'gnus-summary-mode)
7749                  (gnus-set-global-variables))
7750             (gnus-configure-windows (cdr quit-config)))))
7751       (gnus-message 3 "Can't select group")
7752       nil)
7753      ;; The user did a `C-g' while prompting for number of articles,
7754      ;; so we exit this group.
7755      ((eq did-select 'quit)
7756       (and (eq major-mode 'gnus-summary-mode)
7757            (not (equal (current-buffer) kill-buffer))
7758            (kill-buffer (current-buffer)))
7759       (when kill-buffer
7760         (gnus-kill-or-deaden-summary kill-buffer))
7761       (if (not quit-config)
7762           (progn
7763             (set-buffer gnus-group-buffer)
7764             (gnus-group-jump-to-group group)
7765             (gnus-group-next-unread-group 1)
7766             (gnus-configure-windows 'group 'force))
7767         (if (not (buffer-name (car quit-config)))
7768             (gnus-configure-windows 'group 'force)
7769           (set-buffer (car quit-config))
7770           (and (eq major-mode 'gnus-summary-mode)
7771                (gnus-set-global-variables))
7772           (gnus-configure-windows (cdr quit-config))))
7773       ;; Finally signal the quit.
7774       (signal 'quit nil))
7775      ;; The group was successfully selected.
7776      (t
7777       (gnus-set-global-variables)
7778       ;; Save the active value in effect when the group was entered.
7779       (setq gnus-newsgroup-active
7780             (gnus-copy-sequence
7781              (gnus-active gnus-newsgroup-name)))
7782       ;; You can change the summary buffer in some way with this hook.
7783       (run-hooks 'gnus-select-group-hook)
7784       ;; Set any local variables in the group parameters.
7785       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7786       (gnus-update-format-specifications)
7787       ;; Do score processing.
7788       (when gnus-use-scoring
7789         (gnus-possibly-score-headers))
7790       ;; Check whether to fill in the gaps in the threads.
7791       (when gnus-build-sparse-threads
7792         (gnus-build-sparse-threads))
7793       ;; Find the initial limit.
7794       (if gnus-show-threads
7795           (if show-all
7796               (let ((gnus-newsgroup-dormant nil))
7797                 (gnus-summary-initial-limit show-all))
7798             (gnus-summary-initial-limit show-all))
7799         (setq gnus-newsgroup-limit 
7800               (mapcar 
7801                (lambda (header) (mail-header-number header))
7802                gnus-newsgroup-headers)))
7803       ;; Generate the summary buffer.
7804       (unless no-display
7805         (gnus-summary-prepare))
7806       (when gnus-use-trees
7807         (gnus-tree-open group)
7808         (setq gnus-summary-highlight-line-function
7809               'gnus-tree-highlight-article))
7810       ;; If the summary buffer is empty, but there are some low-scored
7811       ;; articles or some excluded dormants, we include these in the
7812       ;; buffer.
7813       (when (and (zerop (buffer-size))
7814                  (not no-display))
7815         (cond (gnus-newsgroup-dormant
7816                (gnus-summary-limit-include-dormant))
7817               ((and gnus-newsgroup-scored show-all)
7818                (gnus-summary-limit-include-expunged))))
7819       ;; Function `gnus-apply-kill-file' must be called in this hook.
7820       (run-hooks 'gnus-apply-kill-hook)
7821       (if (and (zerop (buffer-size))
7822                (not no-display))
7823           (progn
7824             ;; This newsgroup is empty.
7825             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7826             (gnus-message 6 "No unread news")
7827             (when kill-buffer
7828               (gnus-kill-or-deaden-summary kill-buffer))
7829             ;; Return nil from this function.
7830             nil)
7831         ;; Hide conversation thread subtrees.  We cannot do this in
7832         ;; gnus-summary-prepare-hook since kill processing may not
7833         ;; work with hidden articles.
7834         (and gnus-show-threads
7835              gnus-thread-hide-subtree
7836              (gnus-summary-hide-all-threads))
7837         ;; Show first unread article if requested.
7838         (if (and (not no-article)
7839                  (not no-display)
7840                  gnus-newsgroup-unreads
7841                  gnus-auto-select-first)
7842             (unless (if (eq gnus-auto-select-first 'best)
7843                         (gnus-summary-best-unread-article)
7844                       (gnus-summary-first-unread-article))
7845               (gnus-configure-windows 'summary))
7846           ;; Don't select any articles, just move point to the first
7847           ;; article in the group.
7848           (goto-char (point-min))
7849           (gnus-summary-position-point)
7850           (gnus-set-mode-line 'summary)
7851           (gnus-configure-windows 'summary 'force))
7852         ;; If we are in async mode, we send some info to the backend.
7853         (when gnus-newsgroup-async
7854           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7855         (when kill-buffer
7856           (gnus-kill-or-deaden-summary kill-buffer))
7857         (when (get-buffer-window gnus-group-buffer t)
7858           ;; Gotta use windows, because recenter does wierd stuff if
7859           ;; the current buffer ain't the displayed window.
7860           (let ((owin (selected-window)))
7861             (select-window (get-buffer-window gnus-group-buffer t))
7862             (when (gnus-group-goto-group group)
7863               (recenter))
7864             (select-window owin))))
7865       ;; Mark this buffer as "prepared".
7866       (setq gnus-newsgroup-prepared t)
7867       t))))
7868
7869 (defun gnus-summary-prepare ()
7870   "Generate the summary buffer."
7871   (let ((buffer-read-only nil))
7872     (erase-buffer)
7873     (setq gnus-newsgroup-data nil
7874           gnus-newsgroup-data-reverse nil)
7875     (run-hooks 'gnus-summary-generate-hook)
7876     ;; Generate the buffer, either with threads or without.
7877     (when gnus-newsgroup-headers
7878       (gnus-summary-prepare-threads
7879        (if gnus-show-threads
7880            (gnus-sort-gathered-threads
7881             (funcall gnus-summary-thread-gathering-function
7882                      (gnus-sort-threads
7883                       (gnus-cut-threads (gnus-make-threads)))))
7884          ;; Unthreaded display.
7885          (gnus-sort-articles gnus-newsgroup-headers))))
7886     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7887     ;; Call hooks for modifying summary buffer.
7888     (goto-char (point-min))
7889     (run-hooks 'gnus-summary-prepare-hook)))
7890
7891 (defun gnus-gather-threads-by-subject (threads)
7892   "Gather threads by looking at Subject headers."
7893   (if (not gnus-summary-make-false-root)
7894       threads
7895     (let ((hashtb (gnus-make-hashtable 1023))
7896           (prev threads)
7897           (result threads)
7898           subject hthread whole-subject)
7899       (while threads
7900         (setq whole-subject (mail-header-subject (caar threads)))
7901         (setq subject
7902               (cond
7903                ;; Truncate the subject.
7904                ((numberp gnus-summary-gather-subject-limit)
7905                 (setq subject (gnus-simplify-subject-re whole-subject))
7906                 (if (> (length subject) gnus-summary-gather-subject-limit)
7907                     (substring subject 0 gnus-summary-gather-subject-limit)
7908                   subject))
7909                ;; Fuzzily simplify it.
7910                ((eq 'fuzzy gnus-summary-gather-subject-limit)
7911                 (gnus-simplify-subject-fuzzy whole-subject))
7912                ;; Just remove the leading "Re:".
7913                (t
7914                 (gnus-simplify-subject-re whole-subject))))
7915
7916         (if (and gnus-summary-gather-exclude-subject
7917                  (string-match gnus-summary-gather-exclude-subject
7918                                subject))
7919             ()          ; We don't want to do anything with this article.
7920           ;; We simplify the subject before looking it up in the
7921           ;; hash table.
7922
7923           (if (setq hthread (gnus-gethash subject hashtb))
7924               (progn
7925                 ;; We enter a dummy root into the thread, if we
7926                 ;; haven't done that already.
7927                 (unless (stringp (caar hthread))
7928                   (setcar hthread (list whole-subject (car hthread))))
7929                 ;; We add this new gathered thread to this gathered
7930                 ;; thread.
7931                 (setcdr (car hthread)
7932                         (nconc (cdar hthread) (list (car threads))))
7933                 ;; Remove it from the list of threads.
7934                 (setcdr prev (cdr threads))
7935                 (setq threads prev))
7936             ;; Enter this thread into the hash table.
7937             (gnus-sethash subject threads hashtb)))
7938         (setq prev threads)
7939         (setq threads (cdr threads)))
7940       result)))
7941
7942 (defun gnus-gather-threads-by-references (threads)
7943   "Gather threads by looking at References headers."
7944   (let ((idhashtb (gnus-make-hashtable 1023))
7945         (thhashtb (gnus-make-hashtable 1023))
7946         (prev threads)
7947         (result threads)
7948         ids references id gthread gid entered)
7949     (while threads
7950       (when (setq references (mail-header-references (caar threads)))
7951         (setq id (mail-header-id (caar threads)))
7952         (setq ids (gnus-split-references references))
7953         (setq entered nil)
7954         (while ids
7955           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7956               (progn
7957                 (gnus-sethash (car ids) id idhashtb)
7958                 (gnus-sethash id threads thhashtb))
7959             (setq gthread (gnus-gethash gid thhashtb))
7960             (unless entered
7961               ;; We enter a dummy root into the thread, if we
7962               ;; haven't done that already.
7963               (unless (stringp (caar gthread))
7964                 (setcar gthread (list (mail-header-subject (caar gthread))
7965                                       (car gthread))))
7966               ;; We add this new gathered thread to this gathered
7967               ;; thread.
7968               (setcdr (car gthread)
7969                       (nconc (cdar gthread) (list (car threads)))))
7970             ;; Add it into the thread hash table.
7971             (gnus-sethash id gthread thhashtb)
7972             (setq entered t)
7973             ;; Remove it from the list of threads.
7974             (setcdr prev (cdr threads))
7975             (setq threads prev))
7976           (setq ids (cdr ids))))
7977       (setq prev threads)
7978       (setq threads (cdr threads)))
7979     result))
7980
7981 (defun gnus-sort-gathered-threads (threads)
7982   "Sort subtreads inside each gathered thread by article number."
7983   (let ((result threads))
7984     (while threads
7985       (when (stringp (caar threads))
7986         (setcdr (car threads)
7987                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
7988       (setq threads (cdr threads)))
7989     result))
7990
7991 (defun gnus-make-threads ()
7992   "Go through the dependency hashtb and find the roots.  Return all threads."
7993   (let (threads)
7994     (mapatoms
7995      (lambda (refs)
7996        (unless (car (symbol-value refs))
7997          ;; These threads do not refer back to any other articles,
7998          ;; so they're roots.
7999          (setq threads (append (cdr (symbol-value refs)) threads))))
8000      gnus-newsgroup-dependencies)
8001     threads))
8002
8003 (defun gnus-build-sparse-threads ()
8004   (let ((headers gnus-newsgroup-headers)
8005         (deps gnus-newsgroup-dependencies)
8006         header references generation relations 
8007         cthread subject child end pthread relation)
8008     ;; First we create an alist of generations/relations, where 
8009     ;; generations is how much we trust the ralation, and the relation
8010     ;; is parent/child.
8011     (gnus-message 7 "Making sparse threads...")
8012     (save-excursion
8013       (nnheader-set-temp-buffer " *gnus sparse threads*")
8014       (while (setq header (pop headers))
8015         (when (and (setq references (mail-header-references header))
8016                    (not (string= references "")))
8017           (insert references)
8018           (setq child (mail-header-id header)
8019                 subject (mail-header-subject header))
8020           (setq generation 0)
8021           (while (search-backward ">" nil t)
8022             (setq end (1+ (point)))
8023             (when (search-backward "<" nil t)
8024               (push (list (incf generation) 
8025                           child (setq child (buffer-substring (point) end))
8026                           subject)
8027                     relations)))
8028           (push (list (1+ generation) child nil subject) relations)
8029           (erase-buffer)))
8030       (kill-buffer (current-buffer)))
8031     ;; Sort over trustworthiness.
8032     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
8033     (while (setq relation (pop relations))
8034       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
8035                 (unless (car (symbol-value cthread))
8036                   ;; Make this article the parent of these threads.
8037                   (setcar (symbol-value cthread)
8038                           (vector gnus-reffed-article-number 
8039                                   (cadddr relation) 
8040                                   "" ""
8041                                   (cadr relation) 
8042                                   (or (caddr relation) "") 0 0 "")))
8043               (set cthread (list (vector gnus-reffed-article-number
8044                                          (cadddr relation) 
8045                                          "" "" (cadr relation) 
8046                                          (or (caddr relation) "") 0 0 ""))))
8047         (push gnus-reffed-article-number gnus-newsgroup-limit)
8048         (push gnus-reffed-article-number gnus-newsgroup-sparse)
8049         (push (cons gnus-reffed-article-number gnus-sparse-mark)
8050               gnus-newsgroup-reads)
8051         (decf gnus-reffed-article-number)
8052         ;; Make this new thread the child of its parent.
8053         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
8054             (setcdr (symbol-value pthread)
8055                     (nconc (cdr (symbol-value pthread))
8056                            (list (symbol-value cthread))))
8057           (set pthread (list nil (symbol-value cthread))))))
8058     (gnus-message 7 "Making sparse threads...done")))
8059
8060 (defun gnus-build-old-threads ()
8061   ;; Look at all the articles that refer back to old articles, and
8062   ;; fetch the headers for the articles that aren't there.  This will
8063   ;; build complete threads - if the roots haven't been expired by the
8064   ;; server, that is.
8065   (let (id heads)
8066     (mapatoms
8067      (lambda (refs)
8068        (when (not (car (symbol-value refs)))
8069          (setq heads (cdr (symbol-value refs)))
8070          (while heads
8071            (if (memq (mail-header-number (caar heads))
8072                      gnus-newsgroup-dormant)
8073                (setq heads (cdr heads))
8074              (setq id (symbol-name refs))
8075              (while (and (setq id (gnus-build-get-header id))
8076                          (not (car (gnus-gethash
8077                                     id gnus-newsgroup-dependencies)))))
8078              (setq heads nil)))))
8079      gnus-newsgroup-dependencies)))
8080
8081 (defun gnus-build-get-header (id)
8082   ;; Look through the buffer of NOV lines and find the header to
8083   ;; ID.  Enter this line into the dependencies hash table, and return
8084   ;; the id of the parent article (if any).
8085   (let ((deps gnus-newsgroup-dependencies)
8086         found header)
8087     (prog1
8088         (save-excursion
8089           (set-buffer nntp-server-buffer)
8090           (goto-char (point-min))
8091           (while (and (not found) (search-forward id nil t))
8092             (beginning-of-line)
8093             (setq found (looking-at
8094                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
8095                                  (regexp-quote id))))
8096             (or found (beginning-of-line 2)))
8097           (when found
8098             (beginning-of-line)
8099             (and
8100              (setq header (gnus-nov-parse-line
8101                            (read (current-buffer)) deps))
8102              (gnus-parent-id (mail-header-references header)))))
8103       (when header
8104         (let ((number (mail-header-number header)))
8105           (push number gnus-newsgroup-limit)
8106           (push header gnus-newsgroup-headers)
8107           (if (memq number gnus-newsgroup-unselected)
8108               (progn
8109                 (push number gnus-newsgroup-unreads)
8110                 (setq gnus-newsgroup-unselected
8111                       (delq number gnus-newsgroup-unselected)))
8112             (push number gnus-newsgroup-ancient)))))))
8113
8114 (defun gnus-summary-update-article (article &optional iheader)
8115   "Update ARTICLE in the summary buffer."
8116   (set-buffer gnus-summary-buffer)
8117   (let* ((header (or iheader (gnus-summary-article-header article)))
8118          (id (mail-header-id header))
8119          (data (gnus-data-find article))
8120          (thread (gnus-id-to-thread id))
8121          (references (mail-header-references header))
8122          (parent
8123           (gnus-id-to-thread
8124            (or (gnus-parent-id 
8125                 (if (and references
8126                          (not (equal "" references)))
8127                     references))
8128                "none")))
8129          (buffer-read-only nil)
8130          (old (car thread))
8131          (number (mail-header-number header))
8132          pos)
8133     (when thread
8134       ;; !!! Should this be in or not?
8135       (unless iheader
8136         (setcar thread nil))
8137       (when parent
8138         (delq thread parent))
8139       (if (gnus-summary-insert-subject id header iheader)
8140           ;; Set the (possibly) new article number in the data structure.
8141           (gnus-data-set-number data (gnus-id-to-article id))
8142         (setcar thread old)
8143         nil))))
8144
8145 (defun gnus-rebuild-thread (id)
8146   "Rebuild the thread containing ID."
8147   (let ((buffer-read-only nil)
8148         current thread data)
8149     (if (not gnus-show-threads)
8150         (setq thread (list (car (gnus-id-to-thread id))))
8151       ;; Get the thread this article is part of.
8152       (setq thread (gnus-remove-thread id)))
8153     (setq current (save-excursion
8154                     (and (zerop (forward-line -1))
8155                          (gnus-summary-article-number))))
8156     ;; If this is a gathered thread, we have to go some re-gathering.
8157     (when (stringp (car thread))
8158       (let ((subject (car thread))
8159             roots thr)
8160         (setq thread (cdr thread))
8161         (while thread
8162           (unless (memq (setq thr (gnus-id-to-thread
8163                                       (gnus-root-id
8164                                        (mail-header-id (caar thread)))))
8165                         roots)
8166             (push thr roots))
8167           (setq thread (cdr thread)))
8168         ;; We now have all (unique) roots.
8169         (if (= (length roots) 1)
8170             ;; All the loose roots are now one solid root.
8171             (setq thread (car roots))
8172           (setq thread (cons subject (gnus-sort-threads roots))))))
8173     (let (threads)
8174       ;; We then insert this thread into the summary buffer.
8175       (let (gnus-newsgroup-data gnus-newsgroup-threads)
8176         (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
8177         (setq data (nreverse gnus-newsgroup-data))
8178         (setq threads gnus-newsgroup-threads))
8179       ;; We splice the new data into the data structure.
8180       (gnus-data-enter-list current data)
8181       (gnus-data-compute-positions)
8182       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
8183
8184 (defun gnus-number-to-header (number)
8185   "Return the header for article NUMBER."
8186   (let ((headers gnus-newsgroup-headers))
8187     (while (and headers
8188                 (not (= number (mail-header-number (car headers)))))
8189       (pop headers))
8190     (when headers
8191       (car headers))))
8192
8193 (defun gnus-id-to-thread (id)
8194   "Return the (sub-)thread where ID appears."
8195   (gnus-gethash id gnus-newsgroup-dependencies))
8196
8197 (defun gnus-id-to-article (id)
8198   "Return the article number of ID."
8199   (let ((thread (gnus-id-to-thread id)))
8200     (when (and thread
8201                (car thread))
8202       (mail-header-number (car thread)))))
8203
8204 (defun gnus-id-to-header (id)
8205   "Return the article headers of ID."
8206   (car (gnus-id-to-thread id)))
8207
8208 (defun gnus-article-displayed-root-p (article)
8209   "Say whether ARTICLE is a root(ish) article."
8210   (let ((level (gnus-summary-thread-level article))
8211         (refs (mail-header-references  (gnus-summary-article-header article)))
8212         particle)
8213     (cond 
8214      ((null level) nil)
8215      ((zerop level) t)
8216      ((null refs) t)
8217      ((null (gnus-parent-id refs)) t)
8218      ((and (= 1 level)
8219            (null (setq particle (gnus-id-to-article
8220                                  (gnus-parent-id refs))))
8221            (null (gnus-summary-thread-level particle)))))))
8222
8223 (defun gnus-root-id (id)
8224   "Return the id of the root of the thread where ID appears."
8225   (let (last-id prev)
8226     (while (and id (setq prev (car (gnus-gethash 
8227                                     id gnus-newsgroup-dependencies))))
8228       (setq last-id id
8229             id (gnus-parent-id (mail-header-references prev))))
8230     last-id))
8231
8232 (defun gnus-remove-thread (id &optional dont-remove)
8233   "Remove the thread that has ID in it."
8234   (let ((dep gnus-newsgroup-dependencies)
8235         headers thread last-id)
8236     ;; First go up in this thread until we find the root.
8237     (setq last-id (gnus-root-id id))
8238     (setq headers (list (car (gnus-id-to-thread last-id))
8239                         (caadr (gnus-id-to-thread last-id))))
8240     ;; We have now found the real root of this thread.  It might have
8241     ;; been gathered into some loose thread, so we have to search
8242     ;; through the threads to find the thread we wanted.
8243     (let ((threads gnus-newsgroup-threads)
8244           sub)
8245       (while threads
8246         (setq sub (car threads))
8247         (if (stringp (car sub))
8248             ;; This is a gathered threads, so we look at the roots
8249             ;; below it to find whether this article in in this
8250             ;; gathered root.
8251             (progn
8252               (setq sub (cdr sub))
8253               (while sub
8254                 (when (member (caar sub) headers)
8255                   (setq thread (car threads)
8256                         threads nil
8257                         sub nil))
8258                 (setq sub (cdr sub))))
8259           ;; It's an ordinary thread, so we check it.
8260           (when (eq (car sub) (car headers))
8261             (setq thread sub
8262                   threads nil)))
8263         (setq threads (cdr threads)))
8264       ;; If this article is in no thread, then it's a root.
8265       (if thread
8266           (unless dont-remove
8267             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
8268         (setq thread (gnus-gethash last-id dep)))
8269       (when thread
8270         (prog1
8271             thread ; We return this thread.
8272           (unless dont-remove
8273             (if (stringp (car thread))
8274                 (progn
8275                   ;; If we use dummy roots, then we have to remove the
8276                   ;; dummy root as well.
8277                   (when (eq gnus-summary-make-false-root 'dummy)
8278                     ;; Uhm.
8279                     )
8280                   (setq thread (cdr thread))
8281                   (while thread
8282                     (gnus-remove-thread-1 (car thread))
8283                     (setq thread (cdr thread))))
8284               (gnus-remove-thread-1 thread))))))))
8285
8286 (defun gnus-remove-thread-1 (thread)
8287   "Remove the thread THREAD recursively."
8288   (let ((number (mail-header-number (car thread)))
8289         pos)
8290     (when (setq pos (text-property-any
8291                      (point-min) (point-max) 'gnus-number number))
8292       (goto-char pos)
8293       (gnus-delete-line)
8294       (gnus-data-remove number))
8295     (setq thread (cdr thread))
8296     (while thread
8297       (gnus-remove-thread-1 (pop thread)))))
8298
8299 (defun gnus-sort-threads (threads)
8300   "Sort THREADS."
8301   (if (not gnus-thread-sort-functions)
8302       threads
8303     (let ((func (if (= 1 (length gnus-thread-sort-functions))
8304                     (car gnus-thread-sort-functions)
8305                   `(lambda (t1 t2)
8306                      ,(gnus-make-sort-function 
8307                        (reverse gnus-thread-sort-functions))))))
8308       (gnus-message 7 "Sorting threads...")
8309       (prog1
8310           (sort threads func)
8311         (gnus-message 7 "Sorting threads...done")))))
8312
8313 (defun gnus-sort-articles (articles)
8314   "Sort ARTICLES."
8315   (when gnus-article-sort-functions
8316     (let ((func (if (= 1 (length gnus-article-sort-functions))
8317                     (car gnus-article-sort-functions)
8318                   `(lambda (t1 t2)
8319                      ,(gnus-make-sort-function 
8320                        (reverse gnus-article-sort-functions))))))
8321       (gnus-message 7 "Sorting articles...")
8322       (prog1
8323           (setq gnus-newsgroup-headers (sort articles func))
8324         (gnus-message 7 "Sorting articles...done")))))
8325
8326 (defun gnus-make-sort-function (funs)
8327   "Return a composite sort condition based on the functions in FUNC."
8328   (if (cdr funs)
8329       `(or (,(car funs) t1 t2)
8330            (and (not (,(car funs) t2 t1))
8331                 ,(gnus-make-sort-function (cdr funs))))
8332     `(,(car funs) t1 t2)))
8333                  
8334 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
8335 (defmacro gnus-thread-header (thread)
8336   ;; Return header of first article in THREAD.
8337   ;; Note that THREAD must never, ever be anything else than a variable -
8338   ;; using some other form will lead to serious barfage.
8339   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8340   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8341   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8342         (vector thread) 2))
8343
8344 (defsubst gnus-article-sort-by-number (h1 h2)
8345   "Sort articles by article number."
8346   (< (mail-header-number h1)
8347      (mail-header-number h2)))
8348
8349 (defun gnus-thread-sort-by-number (h1 h2)
8350   "Sort threads by root article number."
8351   (gnus-article-sort-by-number
8352    (gnus-thread-header h1) (gnus-thread-header h2)))
8353
8354 (defsubst gnus-article-sort-by-author (h1 h2)
8355   "Sort articles by root author."
8356   (string-lessp
8357    (let ((extract (funcall
8358                    gnus-extract-address-components
8359                    (mail-header-from h1))))
8360      (or (car extract) (cdr extract)))
8361    (let ((extract (funcall
8362                    gnus-extract-address-components
8363                    (mail-header-from h2))))
8364      (or (car extract) (cdr extract)))))
8365
8366 (defun gnus-thread-sort-by-author (h1 h2)
8367   "Sort threads by root author."
8368   (gnus-article-sort-by-author
8369    (gnus-thread-header h1)  (gnus-thread-header h2)))
8370
8371 (defsubst gnus-article-sort-by-subject (h1 h2)
8372   "Sort articles by root subject."
8373   (string-lessp
8374    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8375    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8376
8377 (defun gnus-thread-sort-by-subject (h1 h2)
8378   "Sort threads by root subject."
8379   (gnus-article-sort-by-subject
8380    (gnus-thread-header h1) (gnus-thread-header h2)))
8381
8382 (defsubst gnus-article-sort-by-date (h1 h2)
8383   "Sort articles by root article date."
8384   (string-lessp
8385    (inline (gnus-sortable-date (mail-header-date h1)))
8386    (inline (gnus-sortable-date (mail-header-date h2)))))
8387
8388 (defun gnus-thread-sort-by-date (h1 h2)
8389   "Sort threads by root article date."
8390   (gnus-article-sort-by-date
8391    (gnus-thread-header h1) (gnus-thread-header h2)))
8392
8393 (defsubst gnus-article-sort-by-score (h1 h2)
8394   "Sort articles by root article score.
8395 Unscored articles will be counted as having a score of zero."
8396   (> (or (cdr (assq (mail-header-number h1)
8397                     gnus-newsgroup-scored))
8398          gnus-summary-default-score 0)
8399      (or (cdr (assq (mail-header-number h2)
8400                     gnus-newsgroup-scored))
8401          gnus-summary-default-score 0)))
8402
8403 (defun gnus-thread-sort-by-score (h1 h2)
8404   "Sort threads by root article score."
8405   (gnus-article-sort-by-score
8406    (gnus-thread-header h1) (gnus-thread-header h2)))
8407
8408 (defun gnus-thread-sort-by-total-score (h1 h2)
8409   "Sort threads by the sum of all scores in the thread.
8410 Unscored articles will be counted as having a score of zero."
8411   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8412
8413 (defun gnus-thread-total-score (thread)
8414   ;;  This function find the total score of THREAD.
8415   (cond ((null thread)
8416          0)
8417         ((consp thread)
8418          (if (stringp (car thread))
8419              (apply gnus-thread-score-function 0
8420                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8421            (gnus-thread-total-score-1 thread)))
8422         (t
8423          (gnus-thread-total-score-1 (list thread)))))
8424
8425 (defun gnus-thread-total-score-1 (root)
8426   ;; This function finds the total score of the thread below ROOT.
8427   (setq root (car root))
8428   (let ((number (mail-header-number root)))
8429     (if (and (not (memq number gnus-newsgroup-limit))
8430              (not (memq number gnus-newsgroup-sparse)))
8431         ;; This article shouldn't be counted.
8432         (apply gnus-thread-score-function
8433                (mapcar 'gnus-thread-total-score
8434                        (cdr (gnus-gethash (mail-header-id root)
8435                                           gnus-newsgroup-dependencies))))
8436       ;; This article should be counted.
8437       (apply gnus-thread-score-function
8438              (or (cdr (assq number gnus-newsgroup-scored))
8439                  gnus-summary-default-score 0)
8440              (mapcar 'gnus-thread-total-score
8441                      (cdr (gnus-gethash (mail-header-id root)
8442                                         gnus-newsgroup-dependencies)))))))
8443
8444 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8445 (defvar gnus-tmp-prev-subject nil)
8446 (defvar gnus-tmp-false-parent nil)
8447 (defvar gnus-tmp-root-expunged nil)
8448 (defvar gnus-tmp-dummy-line nil)
8449
8450 (defun gnus-summary-prepare-threads (threads)
8451   "Prepare summary buffer from THREADS and indentation LEVEL.
8452 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8453 or a straight list of headers."
8454   (gnus-message 7 "Generating summary...")
8455
8456   (setq gnus-newsgroup-threads threads)
8457   (beginning-of-line)
8458
8459   (let ((gnus-tmp-level 0)
8460         (default-score (or gnus-summary-default-score 0))
8461         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8462         thread number subject stack state gnus-tmp-gathered beg-match
8463         new-roots gnus-tmp-new-adopts thread-end
8464         gnus-tmp-header gnus-tmp-unread
8465         gnus-tmp-replied gnus-tmp-subject-or-nil
8466         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8467         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8468         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8469
8470     (setq gnus-tmp-prev-subject nil)
8471
8472     (if (vectorp (car threads))
8473         ;; If this is a straight (sic) list of headers, then a
8474         ;; threaded summary display isn't required, so we just create
8475         ;; an unthreaded one.
8476         (gnus-summary-prepare-unthreaded threads)
8477
8478       ;; Do the threaded display.
8479
8480       (while (or threads stack gnus-tmp-new-adopts new-roots)
8481
8482         (if (and (= gnus-tmp-level 0)
8483                  (not (setq gnus-tmp-dummy-line nil))
8484                  (or (not stack)
8485                      (= (caar stack) 0))
8486                  (not gnus-tmp-false-parent)
8487                  (or gnus-tmp-new-adopts new-roots))
8488             (if gnus-tmp-new-adopts
8489                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8490                       thread (list (car gnus-tmp-new-adopts))
8491                       gnus-tmp-header (caar thread)
8492                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8493               (if new-roots
8494                   (setq thread (list (car new-roots))
8495                         gnus-tmp-header (caar thread)
8496                         new-roots (cdr new-roots))))
8497
8498           (if threads
8499               ;; If there are some threads, we do them before the
8500               ;; threads on the stack.
8501               (setq thread threads
8502                     gnus-tmp-header (caar thread))
8503             ;; There were no current threads, so we pop something off
8504             ;; the stack.
8505             (setq state (car stack)
8506                   gnus-tmp-level (car state)
8507                   thread (cdr state)
8508                   stack (cdr stack)
8509                   gnus-tmp-header (caar thread))))
8510
8511         (setq gnus-tmp-false-parent nil)
8512         (setq gnus-tmp-root-expunged nil)
8513         (setq thread-end nil)
8514
8515         (if (stringp gnus-tmp-header)
8516             ;; The header is a dummy root.
8517             (cond
8518              ((eq gnus-summary-make-false-root 'adopt)
8519               ;; We let the first article adopt the rest.
8520               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8521                                                (cddar thread)))
8522               (setq gnus-tmp-gathered
8523                     (nconc (mapcar
8524                             (lambda (h) (mail-header-number (car h)))
8525                             (cddar thread))
8526                            gnus-tmp-gathered))
8527               (setq thread (cons (list (caar thread)
8528                                        (cadar thread))
8529                                  (cdr thread)))
8530               (setq gnus-tmp-level -1
8531                     gnus-tmp-false-parent t))
8532              ((eq gnus-summary-make-false-root 'empty)
8533               ;; We print adopted articles with empty subject fields.
8534               (setq gnus-tmp-gathered
8535                     (nconc (mapcar
8536                             (lambda (h) (mail-header-number (car h)))
8537                             (cddar thread))
8538                            gnus-tmp-gathered))
8539               (setq gnus-tmp-level -1))
8540              ((eq gnus-summary-make-false-root 'dummy)
8541               ;; We remember that we probably want to output a dummy
8542               ;; root.
8543               (setq gnus-tmp-dummy-line gnus-tmp-header)
8544               (setq gnus-tmp-prev-subject gnus-tmp-header))
8545              (t
8546               ;; We do not make a root for the gathered
8547               ;; sub-threads at all.
8548               (setq gnus-tmp-level -1)))
8549
8550           (setq number (mail-header-number gnus-tmp-header)
8551                 subject (mail-header-subject gnus-tmp-header))
8552
8553           (cond
8554            ;; If the thread has changed subject, we might want to make
8555            ;; this subthread into a root.
8556            ((and (null gnus-thread-ignore-subject)
8557                  (not (zerop gnus-tmp-level))
8558                  gnus-tmp-prev-subject
8559                  (not (inline
8560                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8561             (setq new-roots (nconc new-roots (list (car thread)))
8562                   thread-end t
8563                   gnus-tmp-header nil))
8564            ;; If the article lies outside the current limit,
8565            ;; then we do not display it.
8566            ((and (not (memq number gnus-newsgroup-limit))
8567                  (not gnus-tmp-dummy-line))
8568             (setq gnus-tmp-gathered
8569                   (nconc (mapcar
8570                           (lambda (h) (mail-header-number (car h)))
8571                           (cdar thread))
8572                          gnus-tmp-gathered))
8573             (setq gnus-tmp-new-adopts (if (cdar thread)
8574                                           (append gnus-tmp-new-adopts
8575                                                   (cdar thread))
8576                                         gnus-tmp-new-adopts)
8577                   thread-end t
8578                   gnus-tmp-header nil)
8579             (when (zerop gnus-tmp-level)
8580               (setq gnus-tmp-root-expunged t)))
8581            ;; Perhaps this article is to be marked as read?
8582            ((and gnus-summary-mark-below
8583                  (< (or (cdr (assq number gnus-newsgroup-scored))
8584                         default-score)
8585                     gnus-summary-mark-below)
8586                  ;; Don't touch sparse articles.
8587                  (not (memq number gnus-newsgroup-sparse))
8588                  (not (memq number gnus-newsgroup-ancient)))
8589             (setq gnus-newsgroup-unreads
8590                   (delq number gnus-newsgroup-unreads))
8591             (if gnus-newsgroup-auto-expire
8592                 (push number gnus-newsgroup-expirable)
8593               (push (cons number gnus-low-score-mark)
8594                     gnus-newsgroup-reads))))
8595
8596           (when gnus-tmp-header
8597             ;; We may have an old dummy line to output before this
8598             ;; article.
8599             (when gnus-tmp-dummy-line
8600               (gnus-summary-insert-dummy-line
8601                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8602               (setq gnus-tmp-dummy-line nil))
8603
8604             ;; Compute the mark.
8605             (setq
8606              gnus-tmp-unread
8607              (cond
8608               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8609               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8610               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8611               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8612               (t (or (cdr (assq number gnus-newsgroup-reads))
8613                      gnus-ancient-mark))))
8614
8615             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8616                                   gnus-tmp-header gnus-tmp-level)
8617                   gnus-newsgroup-data)
8618
8619             ;; Actually insert the line.
8620             (setq
8621              gnus-tmp-subject-or-nil
8622              (cond
8623               ((and gnus-thread-ignore-subject
8624                     gnus-tmp-prev-subject
8625                     (not (inline (gnus-subject-equal
8626                                   gnus-tmp-prev-subject subject))))
8627                subject)
8628               ((zerop gnus-tmp-level)
8629                (if (and (eq gnus-summary-make-false-root 'empty)
8630                         (memq number gnus-tmp-gathered)
8631                         gnus-tmp-prev-subject
8632                         (inline (gnus-subject-equal
8633                                  gnus-tmp-prev-subject subject)))
8634                    gnus-summary-same-subject
8635                  subject))
8636               (t gnus-summary-same-subject)))
8637             (if (and (eq gnus-summary-make-false-root 'adopt)
8638                      (= gnus-tmp-level 1)
8639                      (memq number gnus-tmp-gathered))
8640                 (setq gnus-tmp-opening-bracket ?\<
8641                       gnus-tmp-closing-bracket ?\>)
8642               (setq gnus-tmp-opening-bracket ?\[
8643                     gnus-tmp-closing-bracket ?\]))
8644             (setq
8645              gnus-tmp-indentation
8646              (aref gnus-thread-indent-array gnus-tmp-level)
8647              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8648              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8649                                 gnus-summary-default-score 0)
8650              gnus-tmp-score-char
8651              (if (or (null gnus-summary-default-score)
8652                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8653                          gnus-summary-zcore-fuzz)) ? 
8654                (if (< gnus-tmp-score gnus-summary-default-score)
8655                    gnus-score-below-mark gnus-score-over-mark))
8656              gnus-tmp-replied
8657              (cond ((memq number gnus-newsgroup-processable)
8658                     gnus-process-mark)
8659                    ((memq number gnus-newsgroup-cached)
8660                     gnus-cached-mark)
8661                    ((memq number gnus-newsgroup-replied)
8662                     gnus-replied-mark)
8663                    ((memq number gnus-newsgroup-saved)
8664                     gnus-saved-mark)
8665                    (t gnus-unread-mark))
8666              gnus-tmp-from (mail-header-from gnus-tmp-header)
8667              gnus-tmp-name
8668              (cond
8669               ((string-match "(.+)" gnus-tmp-from)
8670                (substring gnus-tmp-from
8671                           (1+ (match-beginning 0)) (1- (match-end 0))))
8672               ((string-match "<[^>]+> *$" gnus-tmp-from)
8673                (setq beg-match (match-beginning 0))
8674                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8675                         (substring gnus-tmp-from (1+ (match-beginning 0))
8676                                    (1- (match-end 0))))
8677                    (substring gnus-tmp-from 0 beg-match)))
8678               (t gnus-tmp-from)))
8679             (when (string= gnus-tmp-name "")
8680               (setq gnus-tmp-name gnus-tmp-from))
8681             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8682             (gnus-put-text-property
8683              (point)
8684              (progn (eval gnus-summary-line-format-spec) (point))
8685              'gnus-number number)
8686             (when gnus-visual-p
8687               (forward-line -1)
8688               (run-hooks 'gnus-summary-update-hook)
8689               (forward-line 1))
8690
8691             (setq gnus-tmp-prev-subject subject)))
8692
8693         (when (nth 1 thread)
8694           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8695         (incf gnus-tmp-level)
8696         (setq threads (if thread-end nil (cdar thread)))
8697         (unless threads
8698           (setq gnus-tmp-level 0)))))
8699   (gnus-message 7 "Generating summary...done"))
8700
8701 (defun gnus-summary-prepare-unthreaded (headers)
8702   "Generate an unthreaded summary buffer based on HEADERS."
8703   (let (header number mark)
8704
8705     (while headers
8706       ;; We may have to root out some bad articles...
8707       (when (memq (setq number (mail-header-number
8708                                 (setq header (pop headers))))
8709                   gnus-newsgroup-limit)
8710         ;; Mark article as read when it has a low score.
8711         (when (and gnus-summary-mark-below
8712                    (< (or (cdr (assq number gnus-newsgroup-scored))
8713                           gnus-summary-default-score 0)
8714                       gnus-summary-mark-below)
8715                    (not (memq number gnus-newsgroup-ancient)))
8716           (setq gnus-newsgroup-unreads
8717                 (delq number gnus-newsgroup-unreads))
8718           (if gnus-newsgroup-auto-expire
8719               (push number gnus-newsgroup-expirable)
8720             (push (cons number gnus-low-score-mark)
8721                   gnus-newsgroup-reads)))
8722
8723         (setq mark
8724               (cond
8725                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8726                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8727                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8728                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8729                (t (or (cdr (assq number gnus-newsgroup-reads))
8730                       gnus-ancient-mark))))
8731         (setq gnus-newsgroup-data
8732               (cons (gnus-data-make number mark (1+ (point)) header 0)
8733                     gnus-newsgroup-data))
8734         (gnus-summary-insert-line
8735          header 0 nil mark (memq number gnus-newsgroup-replied)
8736          (memq number gnus-newsgroup-expirable)
8737          (mail-header-subject header) nil
8738          (cdr (assq number gnus-newsgroup-scored))
8739          (memq number gnus-newsgroup-processable))))))
8740
8741 (defun gnus-select-newsgroup (group &optional read-all)
8742   "Select newsgroup GROUP.
8743 If READ-ALL is non-nil, all articles in the group are selected."
8744   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8745          (info (nth 2 entry))
8746          articles fetched-articles cached)
8747
8748     (or (gnus-check-server
8749          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8750         (error "Couldn't open server"))
8751
8752     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8753         (gnus-activate-group group)     ; Or we can activate it...
8754         (progn                          ; Or we bug out.
8755           (when (equal major-mode 'gnus-summary-mode)
8756             (kill-buffer (current-buffer)))
8757           (error "Couldn't request group %s: %s"
8758                  group (gnus-status-message group))))
8759
8760     (unless (gnus-request-group group t)
8761       (when (equal major-mode 'gnus-summary-mode)
8762         (kill-buffer (current-buffer)))
8763       (error "Couldn't request group %s: %s"
8764              group (gnus-status-message group)))      
8765
8766     (setq gnus-newsgroup-name group)
8767     (setq gnus-newsgroup-unselected nil)
8768     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8769
8770     (and gnus-asynchronous
8771          (gnus-check-backend-function
8772           'request-asynchronous gnus-newsgroup-name)
8773          (setq gnus-newsgroup-async
8774                (gnus-request-asynchronous gnus-newsgroup-name)))
8775
8776     ;; Adjust and set lists of article marks.
8777     (when info
8778       (gnus-adjust-marked-articles info))
8779
8780     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8781     (when (gnus-virtual-group-p group)
8782       (setq cached gnus-newsgroup-cached))
8783
8784     (setq gnus-newsgroup-unreads
8785           (gnus-set-difference
8786            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8787            gnus-newsgroup-dormant))
8788
8789     (setq gnus-newsgroup-processable nil)
8790
8791     (setq articles (gnus-articles-to-read group read-all))
8792
8793     (cond
8794      ((null articles)
8795       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8796       'quit)
8797      ((eq articles 0) nil)
8798      (t
8799       ;; Init the dependencies hash table.
8800       (setq gnus-newsgroup-dependencies
8801             (gnus-make-hashtable (length articles)))
8802       ;; Retrieve the headers and read them in.
8803       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8804       (setq gnus-newsgroup-headers
8805             (if (eq 'nov
8806                     (setq gnus-headers-retrieved-by
8807                           (gnus-retrieve-headers
8808                            articles gnus-newsgroup-name
8809                            ;; We might want to fetch old headers, but
8810                            ;; not if there is only 1 article.
8811                            (and gnus-fetch-old-headers
8812                                 (or (and
8813                                      (not (eq gnus-fetch-old-headers 'some))
8814                                      (not (numberp gnus-fetch-old-headers)))
8815                                     (> (length articles) 1))))))
8816                 (gnus-get-newsgroup-headers-xover articles)
8817               (gnus-get-newsgroup-headers)))
8818       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
8819
8820       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8821       (when cached
8822         (setq gnus-newsgroup-cached cached))
8823
8824       ;; Set the initial limit.
8825       (setq gnus-newsgroup-limit (copy-sequence articles))
8826       ;; Remove canceled articles from the list of unread articles.
8827       (setq gnus-newsgroup-unreads
8828             (gnus-set-sorted-intersection
8829              gnus-newsgroup-unreads
8830              (setq fetched-articles
8831                    (mapcar (lambda (headers) (mail-header-number headers))
8832                            gnus-newsgroup-headers))))
8833       ;; Removed marked articles that do not exist.
8834       (gnus-update-missing-marks
8835        (gnus-sorted-complement fetched-articles articles))
8836       ;; We might want to build some more threads first.
8837       (and gnus-fetch-old-headers
8838            (eq gnus-headers-retrieved-by 'nov)
8839            (gnus-build-old-threads))
8840       ;; Check whether auto-expire is to be done in this group.
8841       (setq gnus-newsgroup-auto-expire
8842             (gnus-group-auto-expirable-p group))
8843       ;; Set up the article buffer now, if necessary.
8844       (unless gnus-single-article-buffer
8845         (gnus-article-setup-buffer))
8846       ;; First and last article in this newsgroup.
8847       (when gnus-newsgroup-headers
8848         (setq gnus-newsgroup-begin
8849               (mail-header-number (car gnus-newsgroup-headers))
8850               gnus-newsgroup-end
8851               (mail-header-number
8852                (gnus-last-element gnus-newsgroup-headers))))
8853       (setq gnus-reffed-article-number -1)
8854       ;; GROUP is successfully selected.
8855       (or gnus-newsgroup-headers t)))))
8856
8857 (defun gnus-articles-to-read (group read-all)
8858   ;; Find out what articles the user wants to read.
8859   (let* ((articles
8860           ;; Select all articles if `read-all' is non-nil, or if there
8861           ;; are no unread articles.
8862           (if (or read-all
8863                   (and (zerop (length gnus-newsgroup-marked))
8864                        (zerop (length gnus-newsgroup-unreads))))
8865               (gnus-uncompress-range (gnus-active group))
8866             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8867                           (copy-sequence gnus-newsgroup-unreads))
8868                   '<)))
8869          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8870          (scored (length scored-list))
8871          (number (length articles))
8872          (marked (+ (length gnus-newsgroup-marked)
8873                     (length gnus-newsgroup-dormant)))
8874          (select
8875           (cond
8876            ((numberp read-all)
8877             read-all)
8878            (t
8879             (condition-case ()
8880                 (cond
8881                  ((and (or (<= scored marked) (= scored number))
8882                        (numberp gnus-large-newsgroup)
8883                        (> number gnus-large-newsgroup))
8884                   (let ((input
8885                          (read-string
8886                           (format
8887                            "How many articles from %s (default %d): "
8888                            gnus-newsgroup-name number))))
8889                     (if (string-match "^[ \t]*$" input) number input)))
8890                  ((and (> scored marked) (< scored number)
8891                        (> (- scored number) 20))
8892                   (let ((input
8893                          (read-string
8894                           (format "%s %s (%d scored, %d total): "
8895                                   "How many articles from"
8896                                   group scored number))))
8897                     (if (string-match "^[ \t]*$" input)
8898                         number input)))
8899                  (t number))
8900               (quit nil))))))
8901     (setq select (if (stringp select) (string-to-number select) select))
8902     (if (or (null select) (zerop select))
8903         select
8904       (if (and (not (zerop scored)) (<= (abs select) scored))
8905           (progn
8906             (setq articles (sort scored-list '<))
8907             (setq number (length articles)))
8908         (setq articles (copy-sequence articles)))
8909
8910       (if (< (abs select) number)
8911           (if (< select 0)
8912               ;; Select the N oldest articles.
8913               (setcdr (nthcdr (1- (abs select)) articles) nil)
8914             ;; Select the N most recent articles.
8915             (setq articles (nthcdr (- number select) articles))))
8916       (setq gnus-newsgroup-unselected
8917             (gnus-sorted-intersection
8918              gnus-newsgroup-unreads
8919              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8920       articles)))
8921
8922 (defun gnus-killed-articles (killed articles)
8923   (let (out)
8924     (while articles
8925       (if (inline (gnus-member-of-range (car articles) killed))
8926           (setq out (cons (car articles) out)))
8927       (setq articles (cdr articles)))
8928     out))
8929
8930 (defun gnus-uncompress-marks (marks)
8931   "Uncompress the mark ranges in MARKS."
8932   (let ((uncompressed '(score bookmark))
8933         out)
8934     (while marks
8935       (if (memq (caar marks) uncompressed)
8936           (push (car marks) out)
8937         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8938       (setq marks (cdr marks)))
8939     out))
8940
8941 (defun gnus-adjust-marked-articles (info)
8942   "Set all article lists and remove all marks that are no longer legal."
8943   (let* ((marked-lists (gnus-info-marks info))
8944          (active (gnus-active (gnus-info-group info)))
8945          (min (car active))
8946          (max (cdr active))
8947          (types gnus-article-mark-lists)
8948          (uncompressed '(score bookmark))
8949          marks var articles article mark)
8950
8951     (while marked-lists
8952       (setq marks (pop marked-lists))
8953       (set (setq var (intern (format "gnus-newsgroup-%s"
8954                                      (car (rassq (setq mark (car marks))
8955                                                  types)))))
8956            (if (memq (car marks) uncompressed) (cdr marks)
8957              (gnus-uncompress-range (cdr marks))))
8958
8959       (setq articles (symbol-value var))
8960
8961       ;; All articles have to be subsets of the active articles.
8962       (cond
8963        ;; Adjust "simple" lists.
8964        ((memq mark '(tick dormant expirable reply killed save))
8965         (while articles
8966           (when (or (< (setq article (pop articles)) min) (> article max))
8967             (set var (delq article (symbol-value var))))))
8968        ;; Adjust assocs.
8969        ((memq mark '(score bookmark))
8970         (while articles
8971           (when (or (< (car (setq article (pop articles))) min)
8972                     (> (car article) max))
8973             (set var (delq article (symbol-value var))))))))))
8974
8975 (defun gnus-update-missing-marks (missing)
8976   "Go through the list of MISSING articles and remove them mark lists."
8977   (when missing
8978     (let ((types gnus-article-mark-lists)
8979           var m)
8980       ;; Go through all types.
8981       (while types
8982         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8983         (when (symbol-value var)
8984           ;; This list has articles.  So we delete all missing articles
8985           ;; from it.
8986           (setq m missing)
8987           (while m
8988             (set var (delq (pop m) (symbol-value var)))))))))
8989
8990 (defun gnus-update-marks ()
8991   "Enter the various lists of marked articles into the newsgroup info list."
8992   (let ((types gnus-article-mark-lists)
8993         (info (gnus-get-info gnus-newsgroup-name))
8994         (uncompressed '(score bookmark killed))
8995         type list newmarked symbol)
8996     (when info
8997       ;; Add all marks lists that are non-nil to the list of marks lists.
8998       (while types
8999         (setq type (pop types))
9000         (when (setq list (symbol-value
9001                           (setq symbol
9002                                 (intern (format "gnus-newsgroup-%s"
9003                                                 (car type))))))
9004           (push (cons (cdr type)
9005                       (if (memq (cdr type) uncompressed) list
9006                         (gnus-compress-sequence 
9007                          (set symbol (sort list '<)) t)))
9008                 newmarked)))
9009
9010       ;; Enter these new marks into the info of the group.
9011       (if (nthcdr 3 info)
9012           (setcar (nthcdr 3 info) newmarked)
9013         ;; Add the marks lists to the end of the info.
9014         (when newmarked
9015           (setcdr (nthcdr 2 info) (list newmarked))))
9016
9017       ;; Cut off the end of the info if there's nothing else there.
9018       (let ((i 5))
9019         (while (and (> i 2)
9020                     (not (nth i info)))
9021           (when (nthcdr (decf i) info)
9022             (setcdr (nthcdr i info) nil)))))))
9023
9024 (defun gnus-add-marked-articles (group type articles &optional info force)
9025   ;; Add ARTICLES of TYPE to the info of GROUP.
9026   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
9027   ;; add, but replace marked articles of TYPE with ARTICLES.
9028   (let ((info (or info (gnus-get-info group)))
9029         (uncompressed '(score bookmark killed))
9030         marked m)
9031     (or (not info)
9032         (and (not (setq marked (nthcdr 3 info)))
9033              (or (null articles)
9034                  (setcdr (nthcdr 2 info)
9035                          (list (list (cons type (gnus-compress-sequence
9036                                                  articles t)))))))
9037         (and (not (setq m (assq type (car marked))))
9038              (or (null articles)
9039                  (setcar marked
9040                          (cons (cons type (gnus-compress-sequence articles t) )
9041                                (car marked)))))
9042         (if force
9043             (if (null articles)
9044                 (setcar (nthcdr 3 info)
9045                         (delq (assq type (car marked)) (car marked)))
9046               (setcdr m (gnus-compress-sequence articles t)))
9047           (setcdr m (gnus-compress-sequence
9048                      (sort (nconc (gnus-uncompress-range (cdr m))
9049                                   (copy-sequence articles)) '<) t))))))
9050
9051 (defun gnus-set-mode-line (where)
9052   "This function sets the mode line of the article or summary buffers.
9053 If WHERE is `summary', the summary mode line format will be used."
9054   ;; Is this mode line one we keep updated?
9055   (when (memq where gnus-updated-mode-lines)
9056     (let (mode-string)
9057       (save-excursion
9058         ;; We evaluate this in the summary buffer since these
9059         ;; variables are buffer-local to that buffer.
9060         (set-buffer gnus-summary-buffer)
9061         ;; We bind all these variables that are used in the `eval' form
9062         ;; below.
9063         (let* ((mformat (symbol-value
9064                          (intern
9065                           (format "gnus-%s-mode-line-format-spec" where))))
9066                (gnus-tmp-group-name gnus-newsgroup-name)
9067                (gnus-tmp-article-number (or gnus-current-article 0))
9068                (gnus-tmp-unread gnus-newsgroup-unreads)
9069                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
9070                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
9071                (gnus-tmp-unread-and-unselected
9072                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
9073                             (zerop gnus-tmp-unselected)) "")
9074                       ((zerop gnus-tmp-unselected)
9075                        (format "{%d more}" gnus-tmp-unread-and-unticked))
9076                       (t (format "{%d(+%d) more}"
9077                                  gnus-tmp-unread-and-unticked
9078                                  gnus-tmp-unselected))))
9079                (gnus-tmp-subject
9080                 (if (and gnus-current-headers
9081                          (vectorp gnus-current-headers))
9082                     (gnus-mode-string-quote
9083                      (mail-header-subject gnus-current-headers)) ""))
9084                max-len
9085                gnus-tmp-header);; passed as argument to any user-format-funcs
9086           (setq mode-string (eval mformat))
9087           (setq max-len (max 4 (if gnus-mode-non-string-length
9088                                    (- (window-width)
9089                                       gnus-mode-non-string-length)
9090                                  (length mode-string))))
9091           ;; We might have to chop a bit of the string off...
9092           (when (> (length mode-string) max-len)
9093             (setq mode-string
9094                   (concat (gnus-truncate-string mode-string (- max-len 3))
9095                           "...")))
9096           ;; Pad the mode string a bit.
9097           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
9098       ;; Update the mode line.
9099       (setq mode-line-buffer-identification 
9100             (gnus-mode-line-buffer-identification
9101              (list mode-string)))
9102       (set-buffer-modified-p t))))
9103
9104 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
9105   "Go through the HEADERS list and add all Xrefs to a hash table.
9106 The resulting hash table is returned, or nil if no Xrefs were found."
9107   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
9108          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
9109          (xref-hashtb (make-vector 63 0))
9110          start group entry number xrefs header)
9111     (while headers
9112       (setq header (pop headers))
9113       (when (and (setq xrefs (mail-header-xref header))
9114                  (not (memq (setq number (mail-header-number header))
9115                             unreads)))
9116         (setq start 0)
9117         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
9118           (setq start (match-end 0))
9119           (setq group (if prefix
9120                           (concat prefix (substring xrefs (match-beginning 1)
9121                                                     (match-end 1)))
9122                         (substring xrefs (match-beginning 1) (match-end 1))))
9123           (setq number
9124                 (string-to-int (substring xrefs (match-beginning 2)
9125                                           (match-end 2))))
9126           (if (setq entry (gnus-gethash group xref-hashtb))
9127               (setcdr entry (cons number (cdr entry)))
9128             (gnus-sethash group (cons number nil) xref-hashtb)))))
9129     (and start xref-hashtb)))
9130
9131 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
9132   "Look through all the headers and mark the Xrefs as read."
9133   (let ((virtual (gnus-virtual-group-p from-newsgroup))
9134         name entry info xref-hashtb idlist method nth4)
9135     (save-excursion
9136       (set-buffer gnus-group-buffer)
9137       (when (setq xref-hashtb
9138                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
9139         (mapatoms
9140          (lambda (group)
9141            (unless (string= from-newsgroup (setq name (symbol-name group)))
9142              (setq idlist (symbol-value group))
9143              ;; Dead groups are not updated.
9144              (and (prog1
9145                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
9146                             info (nth 2 entry))
9147                     (if (stringp (setq nth4 (gnus-info-method info)))
9148                         (setq nth4 (gnus-server-to-method nth4))))
9149                   ;; Only do the xrefs if the group has the same
9150                   ;; select method as the group we have just read.
9151                   (or (gnus-methods-equal-p
9152                        nth4 (gnus-find-method-for-group from-newsgroup))
9153                       virtual
9154                       (equal nth4 (setq method (gnus-find-method-for-group
9155                                                 from-newsgroup)))
9156                       (and (equal (car nth4) (car method))
9157                            (equal (nth 1 nth4) (nth 1 method))))
9158                   gnus-use-cross-reference
9159                   (or (not (eq gnus-use-cross-reference t))
9160                       virtual
9161                       ;; Only do cross-references on subscribed
9162                       ;; groups, if that is what is wanted.
9163                       (<= (gnus-info-level info) gnus-level-subscribed))
9164                   (gnus-group-make-articles-read name idlist))))
9165          xref-hashtb)))))
9166
9167 (defun gnus-group-make-articles-read (group articles)
9168   (let* ((num 0)
9169          (entry (gnus-gethash group gnus-newsrc-hashtb))
9170          (info (nth 2 entry))
9171          (active (gnus-active group))
9172          range)
9173     ;; First peel off all illegal article numbers.
9174     (if active
9175         (let ((ids articles)
9176               id first)
9177           (while ids
9178             (setq id (car ids))
9179             (if (and first (> id (cdr active)))
9180                 (progn
9181                   ;; We'll end up in this situation in one particular
9182                   ;; obscure situation.  If you re-scan a group and get
9183                   ;; a new article that is cross-posted to a different
9184                   ;; group that has not been re-scanned, you might get
9185                   ;; crossposted article that has a higher number than
9186                   ;; Gnus believes possible.  So we re-activate this
9187                   ;; group as well.  This might mean doing the
9188                   ;; crossposting thingy will *increase* the number
9189                   ;; of articles in some groups.  Tsk, tsk.
9190                   (setq active (or (gnus-activate-group group) active))))
9191             (if (or (> id (cdr active))
9192                     (< id (car active)))
9193                 (setq articles (delq id articles)))
9194             (setq ids (cdr ids)))))
9195     ;; If the read list is nil, we init it.
9196     (and active
9197          (null (gnus-info-read info))
9198          (> (car active) 1)
9199          (gnus-info-set-read info (cons 1 (1- (car active)))))
9200     ;; Then we add the read articles to the range.
9201     (gnus-info-set-read
9202      info
9203      (setq range
9204            (gnus-add-to-range
9205             (gnus-info-read info) (setq articles (sort articles '<)))))
9206     ;; Then we have to re-compute how many unread
9207     ;; articles there are in this group.
9208     (if active
9209         (progn
9210           (cond
9211            ((not range)
9212             (setq num (- (1+ (cdr active)) (car active))))
9213            ((not (listp (cdr range)))
9214             (setq num (- (cdr active) (- (1+ (cdr range))
9215                                          (car range)))))
9216            (t
9217             (while range
9218               (if (numberp (car range))
9219                   (setq num (1+ num))
9220                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
9221               (setq range (cdr range)))
9222             (setq num (- (cdr active) num))))
9223           ;; Update the number of unread articles.
9224           (setcar entry num)
9225           ;; Update the group buffer.
9226           (gnus-group-update-group group t)))))
9227
9228 (defun gnus-methods-equal-p (m1 m2)
9229   (let ((m1 (or m1 gnus-select-method))
9230         (m2 (or m2 gnus-select-method)))
9231     (or (equal m1 m2)
9232         (and (eq (car m1) (car m2))
9233              (or (not (memq 'address (assoc (symbol-name (car m1))
9234                                             gnus-valid-select-methods)))
9235                  (equal (nth 1 m1) (nth 1 m2)))))))
9236
9237 (defsubst gnus-header-value ()
9238   (buffer-substring (match-end 0) (gnus-point-at-eol)))
9239
9240 (defvar gnus-newsgroup-none-id 0)
9241
9242 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
9243   (let ((cur nntp-server-buffer)
9244         (dependencies
9245          (or dependencies
9246              (save-excursion (set-buffer gnus-summary-buffer)
9247                              gnus-newsgroup-dependencies)))
9248         headers id id-dep ref-dep end ref)
9249     (save-excursion
9250       (set-buffer nntp-server-buffer)
9251       (run-hooks 'gnus-parse-headers-hook)
9252       (let ((case-fold-search t)
9253             in-reply-to header p lines)
9254         (goto-char (point-min))
9255         ;; Search to the beginning of the next header.  Error messages
9256         ;; do not begin with 2 or 3.
9257         (while (re-search-forward "^[23][0-9]+ " nil t)
9258           (setq id nil
9259                 ref nil)
9260           ;; This implementation of this function, with nine
9261           ;; search-forwards instead of the one re-search-forward and
9262           ;; a case (which basically was the old function) is actually
9263           ;; about twice as fast, even though it looks messier.  You
9264           ;; can't have everything, I guess.  Speed and elegance
9265           ;; doesn't always go hand in hand.
9266           (setq
9267            header
9268            (vector
9269             ;; Number.
9270             (prog1
9271                 (read cur)
9272               (end-of-line)
9273               (setq p (point))
9274               (narrow-to-region (point)
9275                                 (or (and (search-forward "\n.\n" nil t)
9276                                          (- (point) 2))
9277                                     (point))))
9278             ;; Subject.
9279             (progn
9280               (goto-char p)
9281               (if (search-forward "\nsubject: " nil t)
9282                   (gnus-header-value) "(none)"))
9283             ;; From.
9284             (progn
9285               (goto-char p)
9286               (if (search-forward "\nfrom: " nil t)
9287                   (gnus-header-value) "(nobody)"))
9288             ;; Date.
9289             (progn
9290               (goto-char p)
9291               (if (search-forward "\ndate: " nil t)
9292                   (gnus-header-value) ""))
9293             ;; Message-ID.
9294             (progn
9295               (goto-char p)
9296               (if (search-forward "\nmessage-id: " nil t)
9297                   (setq id (gnus-header-value))
9298                 ;; If there was no message-id, we just fake one to make
9299                 ;; subsequent routines simpler.
9300                 (setq id (concat "none+"
9301                                  (int-to-string
9302                                   (setq gnus-newsgroup-none-id
9303                                         (1+ gnus-newsgroup-none-id)))))))
9304             ;; References.
9305             (progn
9306               (goto-char p)
9307               (if (search-forward "\nreferences: " nil t)
9308                   (progn
9309                     (setq end (point))
9310                     (prog1
9311                         (gnus-header-value)
9312                       (setq ref
9313                             (buffer-substring
9314                              (progn
9315                                (end-of-line)
9316                                (search-backward ">" end t)
9317                                (1+ (point)))
9318                              (progn
9319                                (search-backward "<" end t)
9320                                (point))))))
9321                 ;; Get the references from the in-reply-to header if there
9322                 ;; were no references and the in-reply-to header looks
9323                 ;; promising.
9324                 (if (and (search-forward "\nin-reply-to: " nil t)
9325                          (setq in-reply-to (gnus-header-value))
9326                          (string-match "<[^>]+>" in-reply-to))
9327                     (setq ref (substring in-reply-to (match-beginning 0)
9328                                          (match-end 0)))
9329                   (setq ref ""))))
9330             ;; Chars.
9331             0
9332             ;; Lines.
9333             (progn
9334               (goto-char p)
9335               (if (search-forward "\nlines: " nil t)
9336                   (if (numberp (setq lines (read cur)))
9337                       lines 0)
9338                 0))
9339             ;; Xref.
9340             (progn
9341               (goto-char p)
9342               (and (search-forward "\nxref: " nil t)
9343                    (gnus-header-value)))))
9344           ;; We do the threading while we read the headers.  The
9345           ;; message-id and the last reference are both entered into
9346           ;; the same hash table.  Some tippy-toeing around has to be
9347           ;; done in case an article has arrived before the article
9348           ;; which it refers to.
9349           (if (boundp (setq id-dep (intern id dependencies)))
9350               (if (and (car (symbol-value id-dep))
9351                        (not force-new))
9352                   ;; An article with this Message-ID has already
9353                   ;; been seen, so we ignore this one, except we add
9354                   ;; any additional Xrefs (in case the two articles
9355                   ;; came from different servers).
9356                   (progn
9357                     (mail-header-set-xref
9358                      (car (symbol-value id-dep))
9359                      (concat (or (mail-header-xref
9360                                   (car (symbol-value id-dep))) "")
9361                              (or (mail-header-xref header) "")))
9362                     (setq header nil))
9363                 (setcar (symbol-value id-dep) header))
9364             (set id-dep (list header)))
9365           (when header
9366             (if (boundp (setq ref-dep (intern ref dependencies)))
9367                 (setcdr (symbol-value ref-dep)
9368                         (nconc (cdr (symbol-value ref-dep))
9369                                (list (symbol-value id-dep))))
9370               (set ref-dep (list nil (symbol-value id-dep))))
9371             (setq headers (cons header headers)))
9372           (goto-char (point-max))
9373           (widen))
9374         (nreverse headers)))))
9375
9376 ;; The following macros and functions were written by Felix Lee
9377 ;; <flee@cse.psu.edu>.
9378
9379 (defmacro gnus-nov-read-integer ()
9380   '(prog1
9381        (if (= (following-char) ?\t)
9382            0
9383          (let ((num (condition-case nil (read buffer) (error nil))))
9384            (if (numberp num) num 0)))
9385      (or (eobp) (forward-char 1))))
9386
9387 (defmacro gnus-nov-skip-field ()
9388   '(search-forward "\t" eol 'move))
9389
9390 (defmacro gnus-nov-field ()
9391   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9392
9393 ;; Goes through the xover lines and returns a list of vectors
9394 (defun gnus-get-newsgroup-headers-xover (sequence &optional 
9395                                                   force-new dependencies)
9396   "Parse the news overview data in the server buffer, and return a
9397 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9398   ;; Get the Xref when the users reads the articles since most/some
9399   ;; NNTP servers do not include Xrefs when using XOVER.
9400   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9401   (let ((cur nntp-server-buffer)
9402         (dependencies (or dependencies gnus-newsgroup-dependencies))
9403         number headers header)
9404     (save-excursion
9405       (set-buffer nntp-server-buffer)
9406       ;; Allow the user to mangle the headers before parsing them.
9407       (run-hooks 'gnus-parse-headers-hook)
9408       (goto-char (point-min))
9409       (while (and sequence (not (eobp)))
9410         (setq number (read cur))
9411         (while (and sequence (< (car sequence) number))
9412           (setq sequence (cdr sequence)))
9413         (and sequence
9414              (eq number (car sequence))
9415              (progn
9416                (setq sequence (cdr sequence))
9417                (if (setq header
9418                          (inline (gnus-nov-parse-line
9419                                   number dependencies force-new)))
9420                    (setq headers (cons header headers)))))
9421         (forward-line 1))
9422       (setq headers (nreverse headers)))
9423     headers))
9424
9425 ;; This function has to be called with point after the article number
9426 ;; on the beginning of the line.
9427 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9428   (let ((none 0)
9429         (eol (gnus-point-at-eol))
9430         (buffer (current-buffer))
9431         header ref id id-dep ref-dep)
9432
9433     ;; overview: [num subject from date id refs chars lines misc]
9434     (narrow-to-region (point) eol)
9435     (or (eobp) (forward-char))
9436
9437     (condition-case nil
9438         (setq header
9439               (vector
9440                number                   ; number
9441                (gnus-nov-field)         ; subject
9442                (gnus-nov-field)         ; from
9443                (gnus-nov-field)         ; date
9444                (setq id (or (gnus-nov-field)
9445                             (concat "none+"
9446                                     (int-to-string
9447                                      (setq none (1+ none)))))) ; id
9448                (progn
9449                  (save-excursion
9450                    (let ((beg (point)))
9451                      (search-forward "\t" eol)
9452                      (if (search-backward ">" beg t)
9453                          (setq ref
9454                                (buffer-substring
9455                                 (1+ (point))
9456                                 (search-backward "<" beg t)))
9457                        (setq ref nil))))
9458                  (gnus-nov-field))      ; refs
9459                (gnus-nov-read-integer)  ; chars
9460                (gnus-nov-read-integer)  ; lines
9461                (if (= (following-char) ?\n)
9462                    nil
9463                  (gnus-nov-field))      ; misc
9464                ))
9465       (error (progn
9466                (gnus-error 4 "Strange nov line")
9467                (setq header nil)
9468                (goto-char eol))))
9469
9470     (widen)
9471
9472     ;; We build the thread tree.
9473     (when header
9474       (if (boundp (setq id-dep (intern id dependencies)))
9475           (if (and (car (symbol-value id-dep))
9476                    (not force-new))
9477               ;; An article with this Message-ID has already been seen,
9478               ;; so we ignore this one, except we add any additional
9479               ;; Xrefs (in case the two articles came from different
9480               ;; servers.
9481               (progn
9482                 (mail-header-set-xref
9483                  (car (symbol-value id-dep))
9484                  (concat (or (mail-header-xref
9485                               (car (symbol-value id-dep))) "")
9486                          (or (mail-header-xref header) "")))
9487                 (setq header nil))
9488             (setcar (symbol-value id-dep) header))
9489         (set id-dep (list header))))
9490     (when header
9491       (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9492           (setcdr (symbol-value ref-dep)
9493                   (nconc (cdr (symbol-value ref-dep))
9494                          (list (symbol-value id-dep))))
9495         (set ref-dep (list nil (symbol-value id-dep)))))
9496     header))
9497
9498 (defun gnus-article-get-xrefs ()
9499   "Fill in the Xref value in `gnus-current-headers', if necessary.
9500 This is meant to be called in `gnus-article-internal-prepare-hook'."
9501   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9502                                  gnus-current-headers)))
9503     (or (not gnus-use-cross-reference)
9504         (not headers)
9505         (and (mail-header-xref headers)
9506              (not (string= (mail-header-xref headers) "")))
9507         (let ((case-fold-search t)
9508               xref)
9509           (save-restriction
9510             (nnheader-narrow-to-headers)
9511             (goto-char (point-min))
9512             (if (or (and (eq (downcase (following-char)) ?x)
9513                          (looking-at "Xref:"))
9514                     (search-forward "\nXref:" nil t))
9515                 (progn
9516                   (goto-char (1+ (match-end 0)))
9517                   (setq xref (buffer-substring (point)
9518                                                (progn (end-of-line) (point))))
9519                   (mail-header-set-xref headers xref))))))))
9520
9521 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
9522   "Find article ID and insert the summary line for that article."
9523   (let ((header (if (and old-header use-old-header)
9524                     old-header (gnus-read-header id)))
9525         (number (and (numberp id) id))
9526         pos)
9527     (when header
9528       ;; Rebuild the thread that this article is part of and go to the
9529       ;; article we have fetched.
9530       (when (and (not gnus-show-threads)
9531                  old-header)
9532         (when (setq pos (text-property-any
9533                          (point-min) (point-max) 'gnus-number 
9534                          (mail-header-number old-header)))
9535           (goto-char pos)
9536           (gnus-delete-line)
9537           (gnus-data-remove (mail-header-number old-header))))
9538       (when old-header
9539         (mail-header-set-number header (mail-header-number old-header)))
9540       (setq gnus-newsgroup-sparse
9541             (delq (setq number (mail-header-number header)) 
9542                   gnus-newsgroup-sparse))
9543       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
9544       (gnus-rebuild-thread (mail-header-id header))
9545       (gnus-summary-goto-subject number nil t))
9546     (when (and (numberp number)
9547                (> number 0))
9548       ;; We have to update the boundaries even if we can't fetch the
9549       ;; article if ID is a number -- so that the next `P' or `N'
9550       ;; command will fetch the previous (or next) article even
9551       ;; if the one we tried to fetch this time has been canceled.
9552       (and (> number gnus-newsgroup-end)
9553            (setq gnus-newsgroup-end number))
9554       (and (< number gnus-newsgroup-begin)
9555            (setq gnus-newsgroup-begin number))
9556       (setq gnus-newsgroup-unselected
9557             (delq number gnus-newsgroup-unselected)))
9558     ;; Report back a success?
9559     (and header (mail-header-number header))))
9560
9561 (defun gnus-summary-work-articles (n)
9562   "Return a list of articles to be worked upon.  The prefix argument,
9563 the list of process marked articles, and the current article will be
9564 taken into consideration."
9565   (cond
9566    (n
9567     ;; A numerical prefix has been given.
9568     (let ((backward (< n 0))
9569           (n (abs (prefix-numeric-value n)))
9570           articles article)
9571       (save-excursion
9572         (while
9573             (and (> n 0)
9574                  (push (setq article (gnus-summary-article-number))
9575                        articles)
9576                  (if backward
9577                      (gnus-summary-find-prev nil article)
9578                    (gnus-summary-find-next nil article)))
9579           (decf n)))
9580       (nreverse articles)))
9581    ((and (boundp 'transient-mark-mode)
9582          transient-mark-mode
9583          mark-active)
9584     ;; Work on the region between point and mark.
9585     (let ((max (max (point) (mark)))
9586           articles article)
9587       (save-excursion
9588         (goto-char (min (point) (mark)))
9589         (while
9590             (and
9591              (push (setq article (gnus-summary-article-number)) articles)
9592              (gnus-summary-find-next nil article)
9593              (< (point) max)))
9594         (nreverse articles))))
9595    (gnus-newsgroup-processable
9596     ;; There are process-marked articles present.
9597     (reverse gnus-newsgroup-processable))
9598    (t
9599     ;; Just return the current article.
9600     (list (gnus-summary-article-number)))))
9601
9602 (defun gnus-summary-search-group (&optional backward use-level)
9603   "Search for next unread newsgroup.
9604 If optional argument BACKWARD is non-nil, search backward instead."
9605   (save-excursion
9606     (set-buffer gnus-group-buffer)
9607     (if (gnus-group-search-forward
9608          backward nil (if use-level (gnus-group-group-level) nil))
9609         (gnus-group-group-name))))
9610
9611 (defun gnus-summary-best-group (&optional exclude-group)
9612   "Find the name of the best unread group.
9613 If EXCLUDE-GROUP, do not go to this group."
9614   (save-excursion
9615     (set-buffer gnus-group-buffer)
9616     (save-excursion
9617       (gnus-group-best-unread-group exclude-group))))
9618
9619 (defun gnus-summary-find-next (&optional unread article backward)
9620   (if backward (gnus-summary-find-prev)
9621     (let* ((dummy (gnus-summary-article-intangible-p))
9622            (article (or article (gnus-summary-article-number)))
9623            (arts (gnus-data-find-list article))
9624            result)
9625       (when (and (not dummy)
9626                  (or (not gnus-summary-check-current)
9627                      (not unread)
9628                      (not (gnus-data-unread-p (car arts)))))
9629         (setq arts (cdr arts)))
9630       (when (setq result
9631                   (if unread
9632                       (progn
9633                         (while arts
9634                           (when (gnus-data-unread-p (car arts))
9635                             (setq result (car arts)
9636                                   arts nil))
9637                           (setq arts (cdr arts)))
9638                         result)
9639                     (car arts)))
9640         (goto-char (gnus-data-pos result))
9641         (gnus-data-number result)))))
9642
9643 (defun gnus-summary-find-prev (&optional unread article)
9644   (let* ((eobp (eobp))
9645          (article (or article (gnus-summary-article-number)))
9646          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9647          result)
9648     (when (and (not eobp)
9649                (or (not gnus-summary-check-current)
9650                    (not unread)
9651                    (not (gnus-data-unread-p (car arts)))))
9652       (setq arts (cdr arts)))
9653     (if (setq result
9654               (if unread
9655                   (progn
9656                     (while arts
9657                       (and (gnus-data-unread-p (car arts))
9658                            (setq result (car arts)
9659                                  arts nil))
9660                       (setq arts (cdr arts)))
9661                     result)
9662                 (car arts)))
9663         (progn
9664           (goto-char (gnus-data-pos result))
9665           (gnus-data-number result)))))
9666
9667 (defun gnus-summary-find-subject (subject &optional unread backward article)
9668   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9669          (article (or article (gnus-summary-article-number)))
9670          (articles (gnus-data-list backward))
9671          (arts (gnus-data-find-list article articles))
9672          result)
9673     (when (or (not gnus-summary-check-current)
9674               (not unread)
9675               (not (gnus-data-unread-p (car arts))))
9676       (setq arts (cdr arts)))
9677     (while arts
9678       (and (or (not unread)
9679                (gnus-data-unread-p (car arts)))
9680            (vectorp (gnus-data-header (car arts)))
9681            (gnus-subject-equal
9682             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9683            (setq result (car arts)
9684                  arts nil))
9685       (setq arts (cdr arts)))
9686     (and result
9687          (goto-char (gnus-data-pos result))
9688          (gnus-data-number result))))
9689
9690 (defun gnus-summary-search-forward (&optional unread subject backward)
9691   "Search forward for an article.
9692 If UNREAD, look for unread articles.  If SUBJECT, look for
9693 articles with that subject.  If BACKWARD, search backward instead."
9694   (cond (subject (gnus-summary-find-subject subject unread backward))
9695         (backward (gnus-summary-find-prev unread))
9696         (t (gnus-summary-find-next unread))))
9697
9698 (defun gnus-recenter (&optional n)
9699   "Center point in window and redisplay frame.
9700 Also do horizontal recentering."
9701   (interactive "P")
9702   (when (and gnus-auto-center-summary
9703              (not (eq gnus-auto-center-summary 'vertical)))
9704     (gnus-horizontal-recenter))
9705   (recenter n))
9706
9707 (defun gnus-summary-recenter ()
9708   "Center point in the summary window.
9709 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9710 displayed, no centering will be performed."
9711   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9712   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9713   (let* ((top (cond ((< (window-height) 4) 0)
9714                     ((< (window-height) 7) 1)
9715                     (t 2)))
9716          (height (1- (window-height)))
9717          (bottom (save-excursion (goto-char (point-max))
9718                                  (forward-line (- height))
9719                                  (point)))
9720          (window (get-buffer-window (current-buffer))))
9721     ;; The user has to want it.
9722     (when gnus-auto-center-summary
9723       (when (get-buffer-window gnus-article-buffer)
9724        ;; Only do recentering when the article buffer is displayed,
9725        ;; Set the window start to either `bottom', which is the biggest
9726        ;; possible valid number, or the second line from the top,
9727        ;; whichever is the least.
9728        (set-window-start
9729         window (min bottom (save-excursion 
9730                              (forward-line (- top)) (point)))))
9731       ;; Do horizontal recentering while we're at it.
9732       (when (and (get-buffer-window (current-buffer) t)
9733                  (not (eq gnus-auto-center-summary 'vertical)))
9734         (let ((selected (selected-window)))
9735           (select-window (get-buffer-window (current-buffer) t))
9736           (gnus-summary-position-point)
9737           (gnus-horizontal-recenter)
9738           (select-window selected))))))
9739
9740 (defun gnus-horizontal-recenter ()
9741   "Recenter the current buffer horizontally."
9742   (if (< (current-column) (/ (window-width) 2))
9743       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9744     (let* ((orig (point))
9745            (end (window-end (get-buffer-window (current-buffer) t)))
9746            (max 0))
9747       ;; Find the longest line currently displayed in the window.
9748       (goto-char (window-start))
9749       (while (and (not (eobp)) 
9750                   (< (point) end))
9751         (end-of-line)
9752         (setq max (max max (current-column)))
9753         (forward-line 1))
9754       (goto-char orig)
9755       ;; Scroll horizontally to center (sort of) the point.
9756       (if (> max (window-width))
9757           (set-window-hscroll 
9758            (get-buffer-window (current-buffer) t)
9759            (min (- (current-column) (/ (window-width) 3))
9760                 (+ 2 (- max (window-width)))))
9761         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9762       max)))
9763
9764 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9765 (defun gnus-short-group-name (group &optional levels)
9766   "Collapse GROUP name LEVELS."
9767   (let* ((name "") 
9768          (foreign "")
9769          (depth 0) 
9770          (skip 1)
9771          (levels (or levels
9772                      (progn
9773                        (while (string-match "\\." group skip)
9774                          (setq skip (match-end 0)
9775                                depth (+ depth 1)))
9776                        depth))))
9777     (if (string-match ":" group)
9778         (setq foreign (substring group 0 (match-end 0))
9779               group (substring group (match-end 0))))
9780     (while group
9781       (if (and (string-match "\\." group)
9782                (> levels (- gnus-group-uncollapsed-levels 1)))
9783           (setq name (concat name (substring group 0 1))
9784                 group (substring group (match-end 0))
9785                 levels (- levels 1)
9786                 name (concat name "."))
9787         (setq name (concat foreign name group)
9788               group nil)))
9789     name))
9790
9791 (defun gnus-summary-jump-to-group (newsgroup)
9792   "Move point to NEWSGROUP in group mode buffer."
9793   ;; Keep update point of group mode buffer if visible.
9794   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9795       (save-window-excursion
9796         ;; Take care of tree window mode.
9797         (if (get-buffer-window gnus-group-buffer)
9798             (pop-to-buffer gnus-group-buffer))
9799         (gnus-group-jump-to-group newsgroup))
9800     (save-excursion
9801       ;; Take care of tree window mode.
9802       (if (get-buffer-window gnus-group-buffer)
9803           (pop-to-buffer gnus-group-buffer)
9804         (set-buffer gnus-group-buffer))
9805       (gnus-group-jump-to-group newsgroup))))
9806
9807 ;; This function returns a list of article numbers based on the
9808 ;; difference between the ranges of read articles in this group and
9809 ;; the range of active articles.
9810 (defun gnus-list-of-unread-articles (group)
9811   (let* ((read (gnus-info-read (gnus-get-info group)))
9812          (active (gnus-active group))
9813          (last (cdr active))
9814          first nlast unread)
9815     ;; If none are read, then all are unread.
9816     (if (not read)
9817         (setq first (car active))
9818       ;; If the range of read articles is a single range, then the
9819       ;; first unread article is the article after the last read
9820       ;; article.  Sounds logical, doesn't it?
9821       (if (not (listp (cdr read)))
9822           (setq first (1+ (cdr read)))
9823         ;; `read' is a list of ranges.
9824         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9825                                 (caar read))) 1)
9826             (setq first 1))
9827         (while read
9828           (if first
9829               (while (< first nlast)
9830                 (setq unread (cons first unread))
9831                 (setq first (1+ first))))
9832           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9833           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9834           (setq read (cdr read)))))
9835     ;; And add the last unread articles.
9836     (while (<= first last)
9837       (setq unread (cons first unread))
9838       (setq first (1+ first)))
9839     ;; Return the list of unread articles.
9840     (nreverse unread)))
9841
9842 (defun gnus-list-of-read-articles (group)
9843   "Return a list of unread, unticked and non-dormant articles."
9844   (let* ((info (gnus-get-info group))
9845          (marked (gnus-info-marks info))
9846          (active (gnus-active group)))
9847     (and info active
9848          (gnus-set-difference
9849           (gnus-sorted-complement
9850            (gnus-uncompress-range active)
9851            (gnus-list-of-unread-articles group))
9852           (append
9853            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9854            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9855
9856 ;; Various summary commands
9857
9858 (defun gnus-summary-universal-argument (arg)
9859   "Perform any operation on all articles that are process/prefixed."
9860   (interactive "P")
9861   (gnus-set-global-variables)
9862   (let ((articles (gnus-summary-work-articles arg))
9863         func article)
9864     (if (eq
9865          (setq
9866           func
9867           (key-binding
9868            (read-key-sequence
9869             (substitute-command-keys
9870              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9871              ))))
9872          'undefined)
9873         (gnus-error 1 "Undefined key")
9874       (save-excursion
9875         (while articles
9876           (gnus-summary-goto-subject (setq article (pop articles)))
9877           (command-execute func)
9878           (gnus-summary-remove-process-mark article)))))
9879   (gnus-summary-position-point))
9880
9881 (defun gnus-summary-toggle-truncation (&optional arg)
9882   "Toggle truncation of summary lines.
9883 With arg, turn line truncation on iff arg is positive."
9884   (interactive "P")
9885   (setq truncate-lines
9886         (if (null arg) (not truncate-lines)
9887           (> (prefix-numeric-value arg) 0)))
9888   (redraw-display))
9889
9890 (defun gnus-summary-reselect-current-group (&optional all rescan)
9891   "Exit and then reselect the current newsgroup.
9892 The prefix argument ALL means to select all articles."
9893   (interactive "P")
9894   (gnus-set-global-variables)
9895   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
9896     (error "Ephemeral groups can't be reselected"))
9897   (let ((current-subject (gnus-summary-article-number))
9898         (group gnus-newsgroup-name))
9899     (setq gnus-newsgroup-begin nil)
9900     (gnus-summary-exit)
9901     ;; We have to adjust the point of group mode buffer because the
9902     ;; current point was moved to the next unread newsgroup by
9903     ;; exiting.
9904     (gnus-summary-jump-to-group group)
9905     (when rescan
9906       (save-excursion
9907         (gnus-group-get-new-news-this-group 1)))
9908     (gnus-group-read-group all t)
9909     (gnus-summary-goto-subject current-subject nil t)))
9910
9911 (defun gnus-summary-rescan-group (&optional all)
9912   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9913   (interactive "P")
9914   (gnus-summary-reselect-current-group all t))
9915
9916 (defun gnus-summary-update-info ()
9917   (let* ((group gnus-newsgroup-name))
9918     (when gnus-newsgroup-kill-headers
9919       (setq gnus-newsgroup-killed
9920             (gnus-compress-sequence
9921              (nconc
9922               (gnus-set-sorted-intersection
9923                (gnus-uncompress-range gnus-newsgroup-killed)
9924                (setq gnus-newsgroup-unselected
9925                      (sort gnus-newsgroup-unselected '<)))
9926               (setq gnus-newsgroup-unreads
9927                     (sort gnus-newsgroup-unreads '<))) t)))
9928     (unless (listp (cdr gnus-newsgroup-killed))
9929       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9930     (let ((headers gnus-newsgroup-headers))
9931       (run-hooks 'gnus-exit-group-hook)
9932       (unless gnus-save-score
9933         (setq gnus-newsgroup-scored nil))
9934       ;; Set the new ranges of read articles.
9935       (gnus-update-read-articles
9936        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9937       ;; Set the current article marks.
9938       (gnus-update-marks)
9939       ;; Do the cross-ref thing.
9940       (when gnus-use-cross-reference
9941         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9942       ;; Do adaptive scoring, and possibly save score files.
9943       (when gnus-newsgroup-adaptive
9944         (gnus-score-adaptive))
9945       (when gnus-use-scoring
9946         (gnus-score-save))
9947       ;; Do not switch windows but change the buffer to work.
9948       (set-buffer gnus-group-buffer)
9949       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9950           (gnus-group-update-group group)))))
9951
9952 (defun gnus-summary-exit (&optional temporary)
9953   "Exit reading current newsgroup, and then return to group selection mode.
9954 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9955   (interactive)
9956   (gnus-set-global-variables)
9957   (gnus-kill-save-kill-buffer)
9958   (let* ((group gnus-newsgroup-name)
9959          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9960          (mode major-mode)
9961          (buf (current-buffer)))
9962     (run-hooks 'gnus-summary-prepare-exit-hook)
9963     ;; If we have several article buffers, we kill them at exit.
9964     (unless gnus-single-article-buffer
9965       (gnus-kill-buffer gnus-original-article-buffer)
9966       (setq gnus-article-current nil))
9967     (when gnus-use-cache
9968       (gnus-cache-possibly-remove-articles)
9969       (gnus-cache-save-buffers))
9970     (when gnus-use-trees
9971       (gnus-tree-close group))
9972     ;; Make all changes in this group permanent.
9973     (unless quit-config
9974       (gnus-summary-update-info))
9975     (gnus-close-group group)
9976     ;; Make sure where I was, and go to next newsgroup.
9977     (set-buffer gnus-group-buffer)
9978     (unless quit-config
9979       (gnus-group-jump-to-group group))
9980     (run-hooks 'gnus-summary-exit-hook)
9981     (unless quit-config
9982       (gnus-group-next-unread-group 1))
9983     (if temporary
9984         nil                             ;Nothing to do.
9985       ;; If we have several article buffers, we kill them at exit.
9986       (unless gnus-single-article-buffer
9987         (gnus-kill-buffer gnus-article-buffer)
9988         (gnus-kill-buffer gnus-original-article-buffer)
9989         (setq gnus-article-current nil))
9990       (set-buffer buf)
9991       (if (not gnus-kill-summary-on-exit)
9992           (gnus-deaden-summary)
9993         ;; We set all buffer-local variables to nil.  It is unclear why
9994         ;; this is needed, but if we don't, buffer-local variables are
9995         ;; not garbage-collected, it seems.  This would the lead to en
9996         ;; ever-growing Emacs.
9997         (gnus-summary-clear-local-variables)
9998         (when (get-buffer gnus-article-buffer)
9999           (bury-buffer gnus-article-buffer))
10000         ;; We clear the global counterparts of the buffer-local
10001         ;; variables as well, just to be on the safe side.
10002         (gnus-configure-windows 'group 'force)
10003         (gnus-summary-clear-local-variables)
10004         ;; Return to group mode buffer.
10005         (if (eq mode 'gnus-summary-mode)
10006             (gnus-kill-buffer buf)))
10007       (setq gnus-current-select-method gnus-select-method)
10008       (pop-to-buffer gnus-group-buffer)
10009       ;; Clear the current group name.
10010       (if (not quit-config)
10011           (progn
10012             (gnus-group-jump-to-group group)
10013             (gnus-group-next-unread-group 1)
10014             (gnus-configure-windows 'group 'force))
10015         (if (not (buffer-name (car quit-config)))
10016             (gnus-configure-windows 'group 'force)
10017           (set-buffer (car quit-config))
10018           (and (eq major-mode 'gnus-summary-mode)
10019                (gnus-set-global-variables))
10020           (gnus-configure-windows (cdr quit-config))))
10021       (unless quit-config
10022         (setq gnus-newsgroup-name nil)))))
10023
10024 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
10025 (defun gnus-summary-exit-no-update (&optional no-questions)
10026   "Quit reading current newsgroup without updating read article info."
10027   (interactive)
10028   (gnus-set-global-variables)
10029   (let* ((group gnus-newsgroup-name)
10030          (quit-config (gnus-group-quit-config group)))
10031     (when (or no-questions
10032               gnus-expert-user
10033               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
10034       ;; If we have several article buffers, we kill them at exit.
10035       (unless gnus-single-article-buffer
10036         (gnus-kill-buffer gnus-article-buffer)
10037         (gnus-kill-buffer gnus-original-article-buffer)
10038         (setq gnus-article-current nil))
10039       (if (not gnus-kill-summary-on-exit)
10040           (gnus-deaden-summary)
10041         (gnus-close-group group)
10042         (gnus-summary-clear-local-variables)
10043         (set-buffer gnus-group-buffer)
10044         (gnus-summary-clear-local-variables)
10045         (when (get-buffer gnus-summary-buffer)
10046           (kill-buffer gnus-summary-buffer)))
10047       (unless gnus-single-article-buffer
10048         (setq gnus-article-current nil))
10049       (when gnus-use-trees
10050         (gnus-tree-close group))
10051       (when (get-buffer gnus-article-buffer)
10052         (bury-buffer gnus-article-buffer))
10053       ;; Return to the group buffer.
10054       (gnus-configure-windows 'group 'force)
10055       ;; Clear the current group name.
10056       (setq gnus-newsgroup-name nil)
10057       (when (equal (gnus-group-group-name) group)
10058         (gnus-group-next-unread-group 1))
10059       (when quit-config
10060         (if (not (buffer-name (car quit-config)))
10061             (gnus-configure-windows 'group 'force)
10062           (set-buffer (car quit-config))
10063           (when (eq major-mode 'gnus-summary-mode)
10064             (gnus-set-global-variables))
10065           (gnus-configure-windows (cdr quit-config)))))))
10066
10067 ;;; Dead summaries.
10068
10069 (defvar gnus-dead-summary-mode-map nil)
10070
10071 (if gnus-dead-summary-mode-map
10072     nil
10073   (setq gnus-dead-summary-mode-map (make-keymap))
10074   (suppress-keymap gnus-dead-summary-mode-map)
10075   (substitute-key-definition
10076    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
10077   (let ((keys '("\C-d" "\r" "\177")))
10078     (while keys
10079       (define-key gnus-dead-summary-mode-map
10080         (pop keys) 'gnus-summary-wake-up-the-dead))))
10081
10082 (defvar gnus-dead-summary-mode nil
10083   "Minor mode for Gnus summary buffers.")
10084
10085 (defun gnus-dead-summary-mode (&optional arg)
10086   "Minor mode for Gnus summary buffers."
10087   (interactive "P")
10088   (when (eq major-mode 'gnus-summary-mode)
10089     (make-local-variable 'gnus-dead-summary-mode)
10090     (setq gnus-dead-summary-mode
10091           (if (null arg) (not gnus-dead-summary-mode)
10092             (> (prefix-numeric-value arg) 0)))
10093     (when gnus-dead-summary-mode
10094       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
10095         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
10096       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
10097         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
10098               minor-mode-map-alist)))))
10099
10100 (defun gnus-deaden-summary ()
10101   "Make the current summary buffer into a dead summary buffer."
10102   ;; Kill any previous dead summary buffer.
10103   (when (and gnus-dead-summary
10104              (buffer-name gnus-dead-summary))
10105     (save-excursion
10106       (set-buffer gnus-dead-summary)
10107       (when gnus-dead-summary-mode
10108         (kill-buffer (current-buffer)))))
10109   ;; Make this the current dead summary.
10110   (setq gnus-dead-summary (current-buffer))
10111   (gnus-dead-summary-mode 1)
10112   (let ((name (buffer-name)))
10113     (when (string-match "Summary" name)
10114       (rename-buffer
10115        (concat (substring name 0 (match-beginning 0)) "Dead "
10116                (substring name (match-beginning 0))) t))))
10117
10118 (defun gnus-kill-or-deaden-summary (buffer)
10119   "Kill or deaden the summary BUFFER."
10120   (when (and (buffer-name buffer)
10121              (not gnus-single-article-buffer))
10122     (save-excursion
10123       (set-buffer buffer)
10124       (gnus-kill-buffer gnus-article-buffer)
10125       (gnus-kill-buffer gnus-original-article-buffer)))
10126   (cond (gnus-kill-summary-on-exit
10127          (when (and gnus-use-trees
10128                     (and (get-buffer buffer)
10129                          (buffer-name (get-buffer buffer))))
10130            (save-excursion
10131              (set-buffer (get-buffer buffer))
10132              (gnus-tree-close gnus-newsgroup-name)))
10133          (gnus-kill-buffer buffer))
10134         ((and (get-buffer buffer)
10135               (buffer-name (get-buffer buffer)))
10136          (save-excursion
10137            (set-buffer buffer)
10138            (gnus-deaden-summary)))))
10139
10140 (defun gnus-summary-wake-up-the-dead (&rest args)
10141   "Wake up the dead summary buffer."
10142   (interactive)
10143   (gnus-dead-summary-mode -1)
10144   (let ((name (buffer-name)))
10145     (when (string-match "Dead " name)
10146       (rename-buffer
10147        (concat (substring name 0 (match-beginning 0))
10148                (substring name (match-end 0))) t)))
10149   (gnus-message 3 "This dead summary is now alive again"))
10150
10151 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
10152 (defun gnus-summary-fetch-faq (&optional faq-dir)
10153   "Fetch the FAQ for the current group.
10154 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
10155 in."
10156   (interactive
10157    (list
10158     (if current-prefix-arg
10159         (completing-read
10160          "Faq dir: " (and (listp gnus-group-faq-directory)
10161                           gnus-group-faq-directory)))))
10162   (let (gnus-faq-buffer)
10163     (and (setq gnus-faq-buffer
10164                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
10165          (gnus-configure-windows 'summary-faq))))
10166
10167 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
10168 (defun gnus-summary-describe-group (&optional force)
10169   "Describe the current newsgroup."
10170   (interactive "P")
10171   (gnus-group-describe-group force gnus-newsgroup-name))
10172
10173 (defun gnus-summary-describe-briefly ()
10174   "Describe summary mode commands briefly."
10175   (interactive)
10176   (gnus-message 6
10177                 (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")))
10178
10179 ;; Walking around group mode buffer from summary mode.
10180
10181 (defun gnus-summary-next-group (&optional no-article target-group backward)
10182   "Exit current newsgroup and then select next unread newsgroup.
10183 If prefix argument NO-ARTICLE is non-nil, no article is selected
10184 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
10185 previous group instead."
10186   (interactive "P")
10187   (gnus-set-global-variables)
10188   (let ((current-group gnus-newsgroup-name)
10189         (current-buffer (current-buffer))
10190         entered)
10191     ;; First we semi-exit this group to update Xrefs and all variables.
10192     ;; We can't do a real exit, because the window conf must remain
10193     ;; the same in case the user is prompted for info, and we don't
10194     ;; want the window conf to change before that...
10195     (gnus-summary-exit t)
10196     (while (not entered)
10197       ;; Then we find what group we are supposed to enter.
10198       (set-buffer gnus-group-buffer)
10199       (gnus-group-jump-to-group current-group)
10200       (setq target-group
10201             (or target-group
10202                 (if (eq gnus-keep-same-level 'best)
10203                     (gnus-summary-best-group gnus-newsgroup-name)
10204                   (gnus-summary-search-group backward gnus-keep-same-level))))
10205       (if (not target-group)
10206           ;; There are no further groups, so we return to the group
10207           ;; buffer.
10208           (progn
10209             (gnus-message 5 "Returning to the group buffer")
10210             (setq entered t)
10211             (set-buffer current-buffer)
10212             (gnus-summary-exit))
10213         ;; We try to enter the target group.
10214         (gnus-group-jump-to-group target-group)
10215         (let ((unreads (gnus-group-group-unread)))
10216           (if (and (or (eq t unreads)
10217                        (and unreads (not (zerop unreads))))
10218                    (gnus-summary-read-group
10219                     target-group nil no-article current-buffer))
10220               (setq entered t)
10221             (setq current-group target-group
10222                   target-group nil)))))))
10223
10224 (defun gnus-summary-prev-group (&optional no-article)
10225   "Exit current newsgroup and then select previous unread newsgroup.
10226 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
10227   (interactive "P")
10228   (gnus-summary-next-group no-article nil t))
10229
10230 ;; Walking around summary lines.
10231
10232 (defun gnus-summary-first-subject (&optional unread)
10233   "Go to the first unread subject.
10234 If UNREAD is non-nil, go to the first unread article.
10235 Returns the article selected or nil if there are no unread articles."
10236   (interactive "P")
10237   (prog1
10238       (cond
10239        ;; Empty summary.
10240        ((null gnus-newsgroup-data)
10241         (gnus-message 3 "No articles in the group")
10242         nil)
10243        ;; Pick the first article.
10244        ((not unread)
10245         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
10246         (gnus-data-number (car gnus-newsgroup-data)))
10247        ;; No unread articles.
10248        ((null gnus-newsgroup-unreads)
10249         (gnus-message 3 "No more unread articles")
10250         nil)
10251        ;; Find the first unread article.
10252        (t
10253         (let ((data gnus-newsgroup-data))
10254           (while (and data
10255                       (not (gnus-data-unread-p (car data))))
10256             (setq data (cdr data)))
10257           (if data
10258               (progn
10259                 (goto-char (gnus-data-pos (car data)))
10260                 (gnus-data-number (car data)))))))
10261     (gnus-summary-position-point)))
10262
10263 (defun gnus-summary-next-subject (n &optional unread dont-display)
10264   "Go to next N'th summary line.
10265 If N is negative, go to the previous N'th subject line.
10266 If UNREAD is non-nil, only unread articles are selected.
10267 The difference between N and the actual number of steps taken is
10268 returned."
10269   (interactive "p")
10270   (let ((backward (< n 0))
10271         (n (abs n)))
10272     (while (and (> n 0)
10273                 (if backward
10274                     (gnus-summary-find-prev unread)
10275                   (gnus-summary-find-next unread)))
10276       (setq n (1- n)))
10277     (if (/= 0 n) (gnus-message 7 "No more%s articles"
10278                                (if unread " unread" "")))
10279     (unless dont-display
10280       (gnus-summary-recenter)
10281       (gnus-summary-position-point))
10282     n))
10283
10284 (defun gnus-summary-next-unread-subject (n)
10285   "Go to next N'th unread summary line."
10286   (interactive "p")
10287   (gnus-summary-next-subject n t))
10288
10289 (defun gnus-summary-prev-subject (n &optional unread)
10290   "Go to previous N'th summary line.
10291 If optional argument UNREAD is non-nil, only unread article is selected."
10292   (interactive "p")
10293   (gnus-summary-next-subject (- n) unread))
10294
10295 (defun gnus-summary-prev-unread-subject (n)
10296   "Go to previous N'th unread summary line."
10297   (interactive "p")
10298   (gnus-summary-next-subject (- n) t))
10299
10300 (defun gnus-summary-goto-subject (article &optional force silent)
10301   "Go the subject line of ARTICLE.
10302 If FORCE, also allow jumping to articles not currently shown."
10303   (let ((b (point))
10304         (data (gnus-data-find article)))
10305     ;; We read in the article if we have to.
10306     (and (not data)
10307          force
10308          (gnus-summary-insert-subject article (and (vectorp force) force) t)
10309          (setq data (gnus-data-find article)))
10310     (goto-char b)
10311     (if (not data)
10312         (progn
10313           (unless silent
10314             (gnus-message 3 "Can't find article %d" article))
10315           nil)
10316       (goto-char (gnus-data-pos data))
10317       article)))
10318
10319 ;; Walking around summary lines with displaying articles.
10320
10321 (defun gnus-summary-expand-window (&optional arg)
10322   "Make the summary buffer take up the entire Emacs frame.
10323 Given a prefix, will force an `article' buffer configuration."
10324   (interactive "P")
10325   (gnus-set-global-variables)
10326   (if arg
10327       (gnus-configure-windows 'article 'force)
10328     (gnus-configure-windows 'summary 'force)))
10329
10330 (defun gnus-summary-display-article (article &optional all-header)
10331   "Display ARTICLE in article buffer."
10332   (gnus-set-global-variables)
10333   (if (null article)
10334       nil
10335     (prog1
10336         (if gnus-summary-display-article-function
10337             (funcall gnus-summary-display-article-function article all-header)
10338           (gnus-article-prepare article all-header))
10339       (run-hooks 'gnus-select-article-hook)
10340       (unless (zerop gnus-current-article)
10341         (gnus-summary-goto-subject gnus-current-article))
10342       (gnus-summary-recenter)
10343       (when gnus-use-trees
10344         (gnus-possibly-generate-tree article)
10345         (gnus-highlight-selected-tree article))
10346       ;; Successfully display article.
10347       (gnus-article-set-window-start
10348        (cdr (assq article gnus-newsgroup-bookmarks))))))
10349
10350 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
10351   "Select the current article.
10352 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
10353 non-nil, the article will be re-fetched even if it already present in
10354 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
10355 be displayed."
10356   ;; Make sure we are in the summary buffer to work around bbdb bug.
10357   (unless (eq major-mode 'gnus-summary-mode)
10358     (set-buffer gnus-summary-buffer))
10359   (let ((article (or article (gnus-summary-article-number)))
10360         (all-headers (not (not all-headers))) ;Must be T or NIL.
10361         gnus-summary-display-article-function
10362         did)
10363     (and (not pseudo)
10364          (gnus-summary-article-pseudo-p article)
10365          (error "This is a pseudo-article."))
10366     (prog1
10367         (save-excursion
10368           (set-buffer gnus-summary-buffer)
10369           (if (or (and gnus-single-article-buffer
10370                        (or (null gnus-current-article)
10371                            (null gnus-article-current)
10372                            (null (get-buffer gnus-article-buffer))
10373                            (not (eq article (cdr gnus-article-current)))
10374                            (not (equal (car gnus-article-current)
10375                                        gnus-newsgroup-name))))
10376                   (and (not gnus-single-article-buffer)
10377                        (or (null gnus-current-article)
10378                            (not (eq gnus-current-article article))))
10379                   force)
10380               ;; The requested article is different from the current article.
10381               (prog1
10382                   (gnus-summary-display-article article all-headers)
10383                 (setq did article))
10384             (if (or all-headers gnus-show-all-headers)
10385                 (gnus-article-show-all-headers))
10386             'old))
10387       (if did
10388           (gnus-article-set-window-start
10389            (cdr (assq article gnus-newsgroup-bookmarks)))))))
10390
10391 (defun gnus-summary-set-current-mark (&optional current-mark)
10392   "Obsolete function."
10393   nil)
10394
10395 (defun gnus-summary-next-article (&optional unread subject backward push)
10396   "Select the next article.
10397 If UNREAD, only unread articles are selected.
10398 If SUBJECT, only articles with SUBJECT are selected.
10399 If BACKWARD, the previous article is selected instead of the next."
10400   (interactive "P")
10401   (gnus-set-global-variables)
10402   (cond
10403    ;; Is there such an article?
10404    ((and (gnus-summary-search-forward unread subject backward)
10405          (or (gnus-summary-display-article (gnus-summary-article-number))
10406              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10407     (gnus-summary-position-point))
10408    ;; If not, we try the first unread, if that is wanted.
10409    ((and subject
10410          gnus-auto-select-same
10411          (or (gnus-summary-first-unread-article)
10412              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10413     (gnus-summary-position-point)
10414     (gnus-message 6 "Wrapped"))
10415    ;; Try to get next/previous article not displayed in this group.
10416    ((and gnus-auto-extend-newsgroup
10417          (not unread) (not subject))
10418     (gnus-summary-goto-article
10419      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10420      nil t))
10421    ;; Go to next/previous group.
10422    (t
10423     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10424         (gnus-summary-jump-to-group gnus-newsgroup-name))
10425     (let ((cmd last-command-char)
10426           (group
10427            (if (eq gnus-keep-same-level 'best)
10428                (gnus-summary-best-group gnus-newsgroup-name)
10429              (gnus-summary-search-group backward gnus-keep-same-level))))
10430       ;; For some reason, the group window gets selected.  We change
10431       ;; it back.
10432       (select-window (get-buffer-window (current-buffer)))
10433       ;; Select next unread newsgroup automagically.
10434       (cond
10435        ((or (not gnus-auto-select-next)
10436             (not cmd))
10437         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10438        ((or (eq gnus-auto-select-next 'quietly)
10439             (and (eq gnus-auto-select-next 'slightly-quietly)
10440                  push)
10441             (and (eq gnus-auto-select-next 'almost-quietly)
10442                  (gnus-summary-last-article-p)))
10443         ;; Select quietly.
10444         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10445             (gnus-summary-exit)
10446           (gnus-message 7 "No more%s articles (%s)..."
10447                         (if unread " unread" "")
10448                         (if group (concat "selecting " group)
10449                           "exiting"))
10450           (gnus-summary-next-group nil group backward)))
10451        (t
10452         (gnus-summary-walk-group-buffer
10453          gnus-newsgroup-name cmd unread backward)))))))
10454
10455 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10456   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10457                       (?\C-p (gnus-group-prev-unread-group 1))))
10458         keve key group ended)
10459     (save-excursion
10460       (set-buffer gnus-group-buffer)
10461       (gnus-summary-jump-to-group from-group)
10462       (setq group
10463             (if (eq gnus-keep-same-level 'best)
10464                 (gnus-summary-best-group gnus-newsgroup-name)
10465               (gnus-summary-search-group backward gnus-keep-same-level))))
10466     (while (not ended)
10467       (gnus-message
10468        5 "No more%s articles%s" (if unread " unread" "")
10469        (if (and group
10470                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10471            (format " (Type %s for %s [%s])"
10472                    (single-key-description cmd) group
10473                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10474          (format " (Type %s to exit %s)"
10475                  (single-key-description cmd)
10476                  gnus-newsgroup-name)))
10477       ;; Confirm auto selection.
10478       (setq key (car (setq keve (gnus-read-event-char))))
10479       (setq ended t)
10480       (cond
10481        ((assq key keystrokes)
10482         (let ((obuf (current-buffer)))
10483           (switch-to-buffer gnus-group-buffer)
10484           (and group
10485                (gnus-group-jump-to-group group))
10486           (eval (cadr (assq key keystrokes)))
10487           (setq group (gnus-group-group-name))
10488           (switch-to-buffer obuf))
10489         (setq ended nil))
10490        ((equal key cmd)
10491         (if (or (not group)
10492                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10493             (gnus-summary-exit)
10494           (gnus-summary-next-group nil group backward)))
10495        (t
10496         (push (cdr keve) unread-command-events))))))
10497
10498 (defun gnus-read-event-char ()
10499   "Get the next event."
10500   (let ((event (read-event)))
10501     (cons (and (numberp event) event) event)))
10502
10503 (defun gnus-summary-next-unread-article ()
10504   "Select unread article after current one."
10505   (interactive)
10506   (gnus-summary-next-article t (and gnus-auto-select-same
10507                                     (gnus-summary-article-subject))))
10508
10509 (defun gnus-summary-prev-article (&optional unread subject)
10510   "Select the article after the current one.
10511 If UNREAD is non-nil, only unread articles are selected."
10512   (interactive "P")
10513   (gnus-summary-next-article unread subject t))
10514
10515 (defun gnus-summary-prev-unread-article ()
10516   "Select unred article before current one."
10517   (interactive)
10518   (gnus-summary-prev-article t (and gnus-auto-select-same
10519                                     (gnus-summary-article-subject))))
10520
10521 (defun gnus-summary-next-page (&optional lines circular)
10522   "Show next page of the selected article.
10523 If at the end of the current article, select the next article.
10524 LINES says how many lines should be scrolled up.
10525
10526 If CIRCULAR is non-nil, go to the start of the article instead of
10527 selecting the next article when reaching the end of the current
10528 article."
10529   (interactive "P")
10530   (setq gnus-summary-buffer (current-buffer))
10531   (gnus-set-global-variables)
10532   (let ((article (gnus-summary-article-number))
10533         (endp nil))
10534     (gnus-configure-windows 'article)
10535     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
10536         (if (and (eq gnus-summary-goto-unread 'never)
10537                  (not (gnus-summary-last-article-p article)))
10538             (gnus-summary-next-article)
10539           (gnus-summary-next-unread-article))
10540       (if (or (null gnus-current-article)
10541               (null gnus-article-current)
10542               (/= article (cdr gnus-article-current))
10543               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10544           ;; Selected subject is different from current article's.
10545           (gnus-summary-display-article article)
10546         (gnus-eval-in-buffer-window gnus-article-buffer
10547           (setq endp (gnus-article-next-page lines)))
10548         (if endp
10549             (cond (circular
10550                    (gnus-summary-beginning-of-article))
10551                   (lines
10552                    (gnus-message 3 "End of message"))
10553                   ((null lines)
10554                    (if (and (eq gnus-summary-goto-unread 'never)
10555                             (not (gnus-summary-last-article-p article)))
10556                        (gnus-summary-next-article)
10557                      (gnus-summary-next-unread-article)))))))
10558     (gnus-summary-recenter)
10559     (gnus-summary-position-point)))
10560
10561 (defun gnus-summary-prev-page (&optional lines)
10562   "Show previous page of selected article.
10563 Argument LINES specifies lines to be scrolled down."
10564   (interactive "P")
10565   (gnus-set-global-variables)
10566   (let ((article (gnus-summary-article-number)))
10567     (gnus-configure-windows 'article)
10568     (if (or (null gnus-current-article)
10569             (null gnus-article-current)
10570             (/= article (cdr gnus-article-current))
10571             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10572         ;; Selected subject is different from current article's.
10573         (gnus-summary-display-article article)
10574       (gnus-summary-recenter)
10575       (gnus-eval-in-buffer-window gnus-article-buffer
10576         (gnus-article-prev-page lines))))
10577   (gnus-summary-position-point))
10578
10579 (defun gnus-summary-scroll-up (lines)
10580   "Scroll up (or down) one line current article.
10581 Argument LINES specifies lines to be scrolled up (or down if negative)."
10582   (interactive "p")
10583   (gnus-set-global-variables)
10584   (gnus-configure-windows 'article)
10585   (gnus-summary-show-thread)
10586   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10587     (gnus-eval-in-buffer-window gnus-article-buffer
10588       (cond ((> lines 0)
10589              (if (gnus-article-next-page lines)
10590                  (gnus-message 3 "End of message")))
10591             ((< lines 0)
10592              (gnus-article-prev-page (- lines))))))
10593   (gnus-summary-recenter)
10594   (gnus-summary-position-point))
10595
10596 (defun gnus-summary-next-same-subject ()
10597   "Select next article which has the same subject as current one."
10598   (interactive)
10599   (gnus-set-global-variables)
10600   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10601
10602 (defun gnus-summary-prev-same-subject ()
10603   "Select previous article which has the same subject as current one."
10604   (interactive)
10605   (gnus-set-global-variables)
10606   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10607
10608 (defun gnus-summary-next-unread-same-subject ()
10609   "Select next unread article which has the same subject as current one."
10610   (interactive)
10611   (gnus-set-global-variables)
10612   (gnus-summary-next-article t (gnus-summary-article-subject)))
10613
10614 (defun gnus-summary-prev-unread-same-subject ()
10615   "Select previous unread article which has the same subject as current one."
10616   (interactive)
10617   (gnus-set-global-variables)
10618   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10619
10620 (defun gnus-summary-first-unread-article ()
10621   "Select the first unread article.
10622 Return nil if there are no unread articles."
10623   (interactive)
10624   (gnus-set-global-variables)
10625   (prog1
10626       (if (gnus-summary-first-subject t)
10627           (progn
10628             (gnus-summary-show-thread)
10629             (gnus-summary-first-subject t)
10630             (gnus-summary-display-article (gnus-summary-article-number))))
10631     (gnus-summary-position-point)))
10632
10633 (defun gnus-summary-best-unread-article ()
10634   "Select the unread article with the highest score."
10635   (interactive)
10636   (gnus-set-global-variables)
10637   (let ((best -1000000)
10638         (data gnus-newsgroup-data)
10639         article score)
10640     (while data
10641       (and (gnus-data-unread-p (car data))
10642            (> (setq score
10643                     (gnus-summary-article-score (gnus-data-number (car data))))
10644               best)
10645            (setq best score
10646                  article (gnus-data-number (car data))))
10647       (setq data (cdr data)))
10648     (prog1
10649         (if article
10650             (gnus-summary-goto-article article)
10651           (error "No unread articles"))
10652       (gnus-summary-position-point))))
10653
10654 (defun gnus-summary-last-subject ()
10655   "Go to the last displayed subject line in the group."
10656   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10657     (when article
10658       (gnus-summary-goto-subject article))))
10659
10660 (defun gnus-summary-goto-article (article &optional all-headers force)
10661   "Fetch ARTICLE and display it if it exists.
10662 If ALL-HEADERS is non-nil, no header lines are hidden."
10663   (interactive
10664    (list
10665     (string-to-int
10666      (completing-read
10667       "Article number: "
10668       (mapcar (lambda (number) (list (int-to-string number)))
10669               gnus-newsgroup-limit)))
10670     current-prefix-arg
10671     t))
10672   (prog1
10673       (if (gnus-summary-goto-subject article force)
10674           (gnus-summary-display-article article all-headers)
10675         (gnus-message 4 "Couldn't go to article %s" article) nil)
10676     (gnus-summary-position-point)))
10677
10678 (defun gnus-summary-goto-last-article ()
10679   "Go to the previously read article."
10680   (interactive)
10681   (prog1
10682       (and gnus-last-article
10683            (gnus-summary-goto-article gnus-last-article))
10684     (gnus-summary-position-point)))
10685
10686 (defun gnus-summary-pop-article (number)
10687   "Pop one article off the history and go to the previous.
10688 NUMBER articles will be popped off."
10689   (interactive "p")
10690   (let (to)
10691     (setq gnus-newsgroup-history
10692           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10693     (if to
10694         (gnus-summary-goto-article (car to))
10695       (error "Article history empty")))
10696   (gnus-summary-position-point))
10697
10698 ;; Summary commands and functions for limiting the summary buffer.
10699
10700 (defun gnus-summary-limit-to-articles (n)
10701   "Limit the summary buffer to the next N articles.
10702 If not given a prefix, use the process marked articles instead."
10703   (interactive "P")
10704   (gnus-set-global-variables)
10705   (prog1
10706       (let ((articles (gnus-summary-work-articles n)))
10707         (setq gnus-newsgroup-processable nil)
10708         (gnus-summary-limit articles))
10709     (gnus-summary-position-point)))
10710
10711 (defun gnus-summary-pop-limit (&optional total)
10712   "Restore the previous limit.
10713 If given a prefix, remove all limits."
10714   (interactive "P")
10715   (gnus-set-global-variables)
10716   (when total 
10717     (setq gnus-newsgroup-limits
10718           (list (mapcar (lambda (h) (mail-header-number h))
10719                         gnus-newsgroup-headers))))
10720   (unless gnus-newsgroup-limits
10721     (error "No limit to pop"))
10722   (prog1
10723       (gnus-summary-limit nil 'pop)
10724     (gnus-summary-position-point)))
10725
10726 (defun gnus-summary-limit-to-subject (subject &optional header)
10727   "Limit the summary buffer to articles that have subjects that match a regexp."
10728   (interactive "sRegexp: ")
10729   (unless header
10730     (setq header "subject"))
10731   (when (not (equal "" subject))
10732     (prog1
10733         (let ((articles (gnus-summary-find-matching
10734                          (or header "subject") subject 'all)))
10735           (or articles (error "Found no matches for \"%s\"" subject))
10736           (gnus-summary-limit articles))
10737       (gnus-summary-position-point))))
10738
10739 (defun gnus-summary-limit-to-author (from)
10740   "Limit the summary buffer to articles that have authors that match a regexp."
10741   (interactive "sRegexp: ")
10742   (gnus-summary-limit-to-subject from "from"))
10743
10744 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10745 (make-obsolete
10746  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10747
10748 (defun gnus-summary-limit-to-unread (&optional all)
10749   "Limit the summary buffer to articles that are not marked as read.
10750 If ALL is non-nil, limit strictly to unread articles."
10751   (interactive "P")
10752   (if all
10753       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10754     (gnus-summary-limit-to-marks
10755      ;; Concat all the marks that say that an article is read and have
10756      ;; those removed.
10757      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10758            gnus-killed-mark gnus-kill-file-mark
10759            gnus-low-score-mark gnus-expirable-mark
10760            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10761      'reverse)))
10762
10763 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10764 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10765
10766 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10767   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10768 If REVERSE, limit the summary buffer to articles that are not marked
10769 with MARKS.  MARKS can either be a string of marks or a list of marks.
10770 Returns how many articles were removed."
10771   (interactive "sMarks: ")
10772   (gnus-set-global-variables)
10773   (prog1
10774       (let ((data gnus-newsgroup-data)
10775             (marks (if (listp marks) marks
10776                      (append marks nil))) ; Transform to list.
10777             articles)
10778         (while data
10779           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10780                  (memq (gnus-data-mark (car data)) marks))
10781                (setq articles (cons (gnus-data-number (car data)) articles)))
10782           (setq data (cdr data)))
10783         (gnus-summary-limit articles))
10784     (gnus-summary-position-point)))
10785
10786 (defun gnus-summary-limit-to-score (&optional score)
10787   "Limit to articles with score at or above SCORE."
10788   (interactive "P")
10789   (gnus-set-global-variables)
10790   (setq score (if score
10791                   (prefix-numeric-value score)
10792                 (or gnus-summary-default-score 0)))
10793   (let ((data gnus-newsgroup-data)
10794         articles)
10795     (while data
10796       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10797                 score)
10798         (push (gnus-data-number (car data)) articles))
10799       (setq data (cdr data)))
10800     (prog1
10801         (gnus-summary-limit articles)
10802       (gnus-summary-position-point))))
10803
10804 (defun gnus-summary-limit-include-dormant ()
10805   "Display all the hidden articles that are marked as dormant."
10806   (interactive)
10807   (gnus-set-global-variables)
10808   (or gnus-newsgroup-dormant
10809       (error "There are no dormant articles in this group"))
10810   (prog1
10811       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10812     (gnus-summary-position-point)))
10813
10814 (defun gnus-summary-limit-exclude-dormant ()
10815   "Hide all dormant articles."
10816   (interactive)
10817   (gnus-set-global-variables)
10818   (prog1
10819       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10820     (gnus-summary-position-point)))
10821
10822 (defun gnus-summary-limit-exclude-childless-dormant ()
10823   "Hide all dormant articles that have no children."
10824   (interactive)
10825   (gnus-set-global-variables)
10826   (let ((data (gnus-data-list t))
10827         articles d children)
10828     ;; Find all articles that are either not dormant or have
10829     ;; children.
10830     (while (setq d (pop data))
10831       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10832                 (and (setq children 
10833                            (gnus-article-children (gnus-data-number d)))
10834                      (let (found)
10835                        (while children
10836                          (when (memq (car children) articles)
10837                            (setq children nil
10838                                  found t))
10839                          (pop children))
10840                        found)))
10841         (push (gnus-data-number d) articles)))
10842     ;; Do the limiting.
10843     (prog1
10844         (gnus-summary-limit articles)
10845       (gnus-summary-position-point))))
10846
10847 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10848   "Mark all unread excluded articles as read.
10849 If ALL, mark even excluded ticked and dormants as read."
10850   (interactive "P")
10851   (let ((articles (gnus-sorted-complement
10852                    (sort
10853                     (mapcar (lambda (h) (mail-header-number h))
10854                             gnus-newsgroup-headers)
10855                     '<)
10856                    (sort gnus-newsgroup-limit '<)))
10857         article)
10858     (setq gnus-newsgroup-unreads nil)
10859     (if all
10860         (setq gnus-newsgroup-dormant nil
10861               gnus-newsgroup-marked nil
10862               gnus-newsgroup-reads
10863               (nconc
10864                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10865                gnus-newsgroup-reads))
10866       (while (setq article (pop articles))
10867         (unless (or (memq article gnus-newsgroup-dormant)
10868                     (memq article gnus-newsgroup-marked))
10869           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10870
10871 (defun gnus-summary-limit (articles &optional pop)
10872   (if pop
10873       ;; We pop the previous limit off the stack and use that.
10874       (setq articles (car gnus-newsgroup-limits)
10875             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10876     ;; We use the new limit, so we push the old limit on the stack.
10877     (setq gnus-newsgroup-limits
10878           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10879   ;; Set the limit.
10880   (setq gnus-newsgroup-limit articles)
10881   (let ((total (length gnus-newsgroup-data))
10882         (data (gnus-data-find-list (gnus-summary-article-number)))
10883         found)
10884     ;; This will do all the work of generating the new summary buffer
10885     ;; according to the new limit.
10886     (gnus-summary-prepare)
10887     ;; Hide any threads, possibly.
10888     (and gnus-show-threads
10889          gnus-thread-hide-subtree
10890          (gnus-summary-hide-all-threads))
10891     ;; Try to return to the article you were at, or one in the
10892     ;; neighborhood.
10893     (if data
10894         ;; We try to find some article after the current one.
10895         (while data
10896           (and (gnus-summary-goto-subject
10897                 (gnus-data-number (car data)) nil t)
10898                (setq data nil
10899                      found t))
10900           (setq data (cdr data))))
10901     (or found
10902         ;; If there is no data, that means that we were after the last
10903         ;; article.  The same goes when we can't find any articles
10904         ;; after the current one.
10905         (progn
10906           (goto-char (point-max))
10907           (gnus-summary-find-prev)))
10908     ;; We return how many articles were removed from the summary
10909     ;; buffer as a result of the new limit.
10910     (- total (length gnus-newsgroup-data))))
10911
10912 (defsubst gnus-invisible-cut-children (threads)
10913   (let ((num 0))
10914     (while threads
10915       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
10916         (incf num))
10917       (pop threads))
10918     (< num 2)))
10919
10920 (defsubst gnus-cut-thread (thread)
10921   "Go forwards in the thread until we find an article that we want to display."
10922   (when (or (eq gnus-fetch-old-headers 'some)
10923             (eq gnus-build-sparse-threads 'some)
10924             (eq gnus-build-sparse-threads 'more))
10925     ;; Deal with old-fetched headers and sparse threads.
10926     (while (and
10927             thread
10928             (or
10929              (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10930              (memq (mail-header-number (car thread)) gnus-newsgroup-ancient))
10931             (or (<= (length (cdr thread)) 1)
10932                 (gnus-invisible-cut-children (cdr thread))))
10933       (setq thread (cadr thread))))
10934   thread)
10935
10936 (defun gnus-cut-threads (threads)
10937   "Cut off all uninteresting articles from the beginning of threads."
10938   (when (or (eq gnus-fetch-old-headers 'some)
10939             (eq gnus-build-sparse-threads 'some)
10940             (eq gnus-build-sparse-threads 'more))
10941     (let ((th threads))
10942       (while th
10943         (setcar th (gnus-cut-thread (car th)))
10944         (setq th (cdr th)))))
10945   ;; Remove nixed out threads.
10946   (delq nil threads))
10947
10948 (defun gnus-summary-initial-limit (&optional show-if-empty)
10949   "Figure out what the initial limit is supposed to be on group entry.
10950 This entails weeding out unwanted dormants, low-scored articles,
10951 fetch-old-headers verbiage, and so on."
10952   ;; Most groups have nothing to remove.
10953   (if (or gnus-inhibit-limiting
10954           (and (null gnus-newsgroup-dormant)
10955                (not (eq gnus-fetch-old-headers 'some))
10956                (null gnus-summary-expunge-below)
10957                (not (eq gnus-build-sparse-threads 'some))
10958                (not (eq gnus-build-sparse-threads 'more))
10959                (null gnus-thread-expunge-below)
10960                (not gnus-use-nocem)))
10961       () ; Do nothing.
10962     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10963     (setq gnus-newsgroup-limit nil)
10964     (mapatoms
10965      (lambda (node)
10966        (unless (car (symbol-value node))
10967          ;; These threads have no parents -- they are roots.
10968          (let ((nodes (cdr (symbol-value node)))
10969                thread)
10970            (while nodes
10971              (if (and gnus-thread-expunge-below
10972                       (< (gnus-thread-total-score (car nodes))
10973                          gnus-thread-expunge-below))
10974                  (gnus-expunge-thread (pop nodes))
10975                (setq thread (pop nodes))
10976                (gnus-summary-limit-children thread))))))
10977      gnus-newsgroup-dependencies)
10978     ;; If this limitation resulted in an empty group, we might
10979     ;; pop the previous limit and use it instead.
10980     (when (and (not gnus-newsgroup-limit)
10981                show-if-empty)
10982       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10983     gnus-newsgroup-limit))
10984
10985 (defun gnus-summary-limit-children (thread)
10986   "Return 1 if this subthread is visible and 0 if it is not."
10987   ;; First we get the number of visible children to this thread.  This
10988   ;; is done by recursing down the thread using this function, so this
10989   ;; will really go down to a leaf article first, before slowly
10990   ;; working its way up towards the root.
10991   (when thread
10992     (let ((children
10993            (if (cdr thread)
10994                (apply '+ (mapcar 'gnus-summary-limit-children
10995                                  (cdr thread)))
10996              0))
10997           (number (mail-header-number (car thread)))
10998           score)
10999       (if (or
11000            ;; If this article is dormant and has absolutely no visible
11001            ;; children, then this article isn't visible.
11002            (and (memq number gnus-newsgroup-dormant)
11003                 (= children 0))
11004            ;; If this is "fetch-old-headered" and there is only one
11005            ;; visible child (or less), then we don't want this article.
11006            (and (eq gnus-fetch-old-headers 'some)
11007                 (memq number gnus-newsgroup-ancient)
11008                 (zerop children))
11009            ;; If this is a sparsely inserted article with no children,
11010            ;; we don't want it.
11011            (and (eq gnus-build-sparse-threads 'some)
11012                 (memq number gnus-newsgroup-sparse)
11013                 (zerop children))
11014            ;; If we use expunging, and this article is really
11015            ;; low-scored, then we don't want this article.
11016            (when (and gnus-summary-expunge-below
11017                       (< (setq score
11018                                (or (cdr (assq number gnus-newsgroup-scored))
11019                                    gnus-summary-default-score))
11020                          gnus-summary-expunge-below))
11021              ;; We increase the expunge-tally here, but that has
11022              ;; nothing to do with the limits, really.
11023              (incf gnus-newsgroup-expunged-tally)
11024              ;; We also mark as read here, if that's wanted.
11025              (when (and gnus-summary-mark-below
11026                         (< score gnus-summary-mark-below))
11027                (setq gnus-newsgroup-unreads
11028                      (delq number gnus-newsgroup-unreads))
11029                (if gnus-newsgroup-auto-expire
11030                    (push number gnus-newsgroup-expirable)
11031                  (push (cons number gnus-low-score-mark)
11032                        gnus-newsgroup-reads)))
11033              t)
11034            (and gnus-use-nocem
11035                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
11036           ;; Nope, invisible article.
11037           0
11038         ;; Ok, this article is to be visible, so we add it to the limit
11039         ;; and return 1.
11040         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
11041         1))))
11042
11043 (defun gnus-expunge-thread (thread)
11044   "Mark all articles in THREAD as read."
11045   (let* ((number (mail-header-number (car thread))))
11046     (incf gnus-newsgroup-expunged-tally)
11047     ;; We also mark as read here, if that's wanted.
11048     (setq gnus-newsgroup-unreads
11049           (delq number gnus-newsgroup-unreads))
11050     (if gnus-newsgroup-auto-expire
11051         (push number gnus-newsgroup-expirable)
11052       (push (cons number gnus-low-score-mark)
11053             gnus-newsgroup-reads)))
11054   ;; Go recursively through all subthreads.
11055   (mapcar 'gnus-expunge-thread (cdr thread)))
11056
11057 ;; Summary article oriented commands
11058
11059 (defun gnus-summary-refer-parent-article (n)
11060   "Refer parent article N times.
11061 The difference between N and the number of articles fetched is returned."
11062   (interactive "p")
11063   (gnus-set-global-variables)
11064   (while
11065       (and
11066        (> n 0)
11067        (let* ((header (gnus-summary-article-header))
11068               (ref
11069                ;; If we try to find the parent of the currently
11070                ;; displayed article, then we take a look at the actual
11071                ;; References header, since this is slightly more
11072                ;; reliable than the References field we got from the
11073                ;; server.
11074                (if (and (eq (mail-header-number header)
11075                             (cdr gnus-article-current))
11076                         (equal gnus-newsgroup-name
11077                                (car gnus-article-current)))
11078                    (save-excursion
11079                      (set-buffer gnus-original-article-buffer)
11080                      (nnheader-narrow-to-headers)
11081                      (prog1
11082                          (message-fetch-field "references")
11083                        (widen)))
11084                  ;; It's not the current article, so we take a bet on
11085                  ;; the value we got from the server.
11086                  (mail-header-references header))))
11087          (if (setq ref (or ref (mail-header-references header)))
11088              (or (gnus-summary-refer-article (gnus-parent-id ref))
11089                  (gnus-message 1 "Couldn't find parent"))
11090            (gnus-message 1 "No references in article %d"
11091                          (gnus-summary-article-number))
11092            nil)))
11093     (setq n (1- n)))
11094   (gnus-summary-position-point)
11095   n)
11096
11097 (defun gnus-summary-refer-references ()
11098   "Fetch all articles mentioned in the References header.
11099 Return how many articles were fetched."
11100   (interactive)
11101   (gnus-set-global-variables)
11102   (let ((ref (mail-header-references (gnus-summary-article-header)))
11103         (current (gnus-summary-article-number))
11104         (n 0))
11105     ;; For each Message-ID in the References header...
11106     (while (string-match "<[^>]*>" ref)
11107       (incf n)
11108       ;; ... fetch that article.
11109       (gnus-summary-refer-article
11110        (prog1 (match-string 0 ref)
11111          (setq ref (substring ref (match-end 0))))))
11112     (gnus-summary-goto-subject current)
11113     (gnus-summary-position-point)
11114     n))
11115
11116 (defun gnus-summary-refer-article (message-id)
11117   "Fetch an article specified by MESSAGE-ID."
11118   (interactive "sMessage-ID: ")
11119   (when (and (stringp message-id)
11120              (not (zerop (length message-id))))
11121     ;; Construct the correct Message-ID if necessary.
11122     ;; Suggested by tale@pawl.rpi.edu.
11123     (unless (string-match "^<" message-id)
11124       (setq message-id (concat "<" message-id)))
11125     (unless (string-match ">$" message-id)
11126       (setq message-id (concat message-id ">")))
11127     (let* ((header (gnus-id-to-header message-id))
11128            (sparse (and header
11129                         (memq (mail-header-number header)
11130                               gnus-newsgroup-sparse))))
11131       (if header
11132           (prog1
11133               ;; The article is present in the buffer, to we just go to it.
11134               (gnus-summary-goto-article 
11135                (mail-header-number header) nil header)
11136             (when sparse
11137               (gnus-summary-update-article (mail-header-number header))))
11138         ;; We fetch the article
11139         (let ((gnus-override-method 
11140                (and (gnus-news-group-p gnus-newsgroup-name)
11141                     gnus-refer-article-method))
11142               number)
11143           ;; Start the special refer-article method, if necessary.
11144           (when (and gnus-refer-article-method
11145                      (gnus-news-group-p gnus-newsgroup-name))
11146             (gnus-check-server gnus-refer-article-method))
11147           ;; Fetch the header, and display the article.
11148           (if (setq number (gnus-summary-insert-subject message-id))
11149               (gnus-summary-select-article nil nil nil number)
11150             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
11151
11152 (defun gnus-summary-enter-digest-group (&optional force)
11153   "Enter a digest group based on the current article."
11154   (interactive "P")
11155   (gnus-set-global-variables)
11156   (gnus-summary-select-article)
11157   (let ((name (format "%s-%d"
11158                       (gnus-group-prefixed-name
11159                        gnus-newsgroup-name (list 'nndoc ""))
11160                       gnus-current-article))
11161         (ogroup gnus-newsgroup-name)
11162         (case-fold-search t)
11163         (buf (current-buffer))
11164         dig)
11165     (save-excursion
11166       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
11167       (insert-buffer-substring gnus-original-article-buffer)
11168       (narrow-to-region
11169        (goto-char (point-min))
11170        (or (search-forward "\n\n" nil t) (point)))
11171       (goto-char (point-min))
11172       (delete-matching-lines "^\\(Path\\):\\|^From ")
11173       (widen))
11174     (unwind-protect
11175         (if (gnus-group-read-ephemeral-group
11176              name `(nndoc ,name (nndoc-address
11177                                  ,(get-buffer dig))
11178                           (nndoc-article-type ,(if force 'digest 'guess))) t)
11179             ;; Make all postings to this group go to the parent group.
11180             (nconc (gnus-info-params (gnus-get-info name))
11181                    (list (cons 'to-group ogroup)))
11182           ;; Couldn't select this doc group.
11183           (switch-to-buffer buf)
11184           (gnus-set-global-variables)
11185           (gnus-configure-windows 'summary)
11186           (gnus-message 3 "Article couldn't be entered?"))
11187       (kill-buffer dig))))
11188
11189 (defun gnus-summary-isearch-article (&optional regexp-p)
11190   "Do incremental search forward on the current article.
11191 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
11192   (interactive "P")
11193   (gnus-set-global-variables)
11194   (gnus-summary-select-article)
11195   (gnus-configure-windows 'article)
11196   (gnus-eval-in-buffer-window gnus-article-buffer
11197     (goto-char (point-min))
11198     (isearch-forward regexp-p)))
11199
11200 (defun gnus-summary-search-article-forward (regexp &optional backward)
11201   "Search for an article containing REGEXP forward.
11202 If BACKWARD, search backward instead."
11203   (interactive
11204    (list (read-string
11205           (format "Search article %s (regexp%s): "
11206                   (if current-prefix-arg "backward" "forward")
11207                   (if gnus-last-search-regexp
11208                       (concat ", default " gnus-last-search-regexp)
11209                     "")))
11210          current-prefix-arg))
11211   (gnus-set-global-variables)
11212   (if (string-equal regexp "")
11213       (setq regexp (or gnus-last-search-regexp ""))
11214     (setq gnus-last-search-regexp regexp))
11215   (unless (gnus-summary-search-article regexp backward)
11216     (error "Search failed: \"%s\"" regexp)))
11217
11218 (defun gnus-summary-search-article-backward (regexp)
11219   "Search for an article containing REGEXP backward."
11220   (interactive
11221    (list (read-string
11222           (format "Search article backward (regexp%s): "
11223                   (if gnus-last-search-regexp
11224                       (concat ", default " gnus-last-search-regexp)
11225                     "")))))
11226   (gnus-summary-search-article-forward regexp 'backward))
11227
11228 (defun gnus-summary-search-article (regexp &optional backward)
11229   "Search for an article containing REGEXP.
11230 Optional argument BACKWARD means do search for backward.
11231 `gnus-select-article-hook' is not called during the search."
11232   (let ((gnus-select-article-hook nil)  ;Disable hook.
11233         (gnus-article-display-hook nil)
11234         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
11235         (re-search
11236          (if backward
11237              're-search-backward 're-search-forward))
11238         (sum (current-buffer))
11239         (found nil))
11240     (gnus-save-hidden-threads
11241       (gnus-summary-select-article)
11242       (set-buffer gnus-article-buffer)
11243       (when backward
11244         (forward-line -1))
11245       (while (not found)
11246         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
11247         (if (if backward
11248                 (re-search-backward regexp nil t)
11249               (re-search-forward regexp nil t))
11250             ;; We found the regexp.
11251             (progn
11252               (setq found 'found)
11253               (beginning-of-line)
11254               (set-window-start
11255                (get-buffer-window (current-buffer))
11256                (point))
11257               (forward-line 1)
11258               (set-buffer sum))
11259           ;; We didn't find it, so we go to the next article.
11260           (set-buffer sum)
11261           (if (not (if backward (gnus-summary-find-prev)
11262                      (gnus-summary-find-next)))
11263               ;; No more articles.
11264               (setq found t)
11265             ;; Select the next article and adjust point.
11266             (gnus-summary-select-article)
11267             (set-buffer gnus-article-buffer)
11268             (widen)
11269             (goto-char (if backward (point-max) (point-min))))))
11270       (gnus-message 7 ""))
11271     ;; Return whether we found the regexp.
11272     (when (eq found 'found)
11273       (gnus-summary-show-thread)
11274       (gnus-summary-goto-subject gnus-current-article)
11275       (gnus-summary-position-point)
11276       t)))
11277
11278 (defun gnus-summary-find-matching (header regexp &optional backward unread
11279                                           not-case-fold)
11280   "Return a list of all articles that match REGEXP on HEADER.
11281 The search stars on the current article and goes forwards unless
11282 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
11283 If UNREAD is non-nil, only unread articles will
11284 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
11285 in the comparisons."
11286   (let ((data (if (eq backward 'all) gnus-newsgroup-data
11287                 (gnus-data-find-list
11288                  (gnus-summary-article-number) (gnus-data-list backward))))
11289         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
11290         (case-fold-search (not not-case-fold))
11291         articles d)
11292     (or (fboundp (intern (concat "mail-header-" header)))
11293         (error "%s is not a valid header" header))
11294     (while data
11295       (setq d (car data))
11296       (and (or (not unread)             ; We want all articles...
11297                (gnus-data-unread-p d))  ; Or just unreads.
11298            (vectorp (gnus-data-header d)) ; It's not a pseudo.
11299            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
11300            (setq articles (cons (gnus-data-number d) articles))) ; Success!
11301       (setq data (cdr data)))
11302     (nreverse articles)))
11303
11304 (defun gnus-summary-execute-command (header regexp command &optional backward)
11305   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
11306 If HEADER is an empty string (or nil), the match is done on the entire
11307 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
11308   (interactive
11309    (list (let ((completion-ignore-case t))
11310            (completing-read
11311             "Header name: "
11312             (mapcar (lambda (string) (list string))
11313                     '("Number" "Subject" "From" "Lines" "Date"
11314                       "Message-ID" "Xref" "References" "Body"))
11315             nil 'require-match))
11316          (read-string "Regexp: ")
11317          (read-key-sequence "Command: ")
11318          current-prefix-arg))
11319   (when (equal header "Body")
11320     (setq header ""))
11321   (gnus-set-global-variables)
11322   ;; Hidden thread subtrees must be searched as well.
11323   (gnus-summary-show-all-threads)
11324   ;; We don't want to change current point nor window configuration.
11325   (save-excursion
11326     (save-window-excursion
11327       (gnus-message 6 "Executing %s..." (key-description command))
11328       ;; We'd like to execute COMMAND interactively so as to give arguments.
11329       (gnus-execute header regexp
11330                     `(lambda () (call-interactively ',(key-binding command)))
11331                     backward)
11332       (gnus-message 6 "Executing %s...done" (key-description command)))))
11333
11334 (defun gnus-summary-beginning-of-article ()
11335   "Scroll the article back to the beginning."
11336   (interactive)
11337   (gnus-set-global-variables)
11338   (gnus-summary-select-article)
11339   (gnus-configure-windows 'article)
11340   (gnus-eval-in-buffer-window gnus-article-buffer
11341     (widen)
11342     (goto-char (point-min))
11343     (and gnus-break-pages (gnus-narrow-to-page))))
11344
11345 (defun gnus-summary-end-of-article ()
11346   "Scroll to the end of the article."
11347   (interactive)
11348   (gnus-set-global-variables)
11349   (gnus-summary-select-article)
11350   (gnus-configure-windows 'article)
11351   (gnus-eval-in-buffer-window gnus-article-buffer
11352     (widen)
11353     (goto-char (point-max))
11354     (recenter -3)
11355     (and gnus-break-pages (gnus-narrow-to-page))))
11356
11357 (defun gnus-summary-show-article (&optional arg)
11358   "Force re-fetching of the current article.
11359 If ARG (the prefix) is non-nil, show the raw article without any
11360 article massaging functions being run."
11361   (interactive "P")
11362   (gnus-set-global-variables)
11363   (if (not arg)
11364       ;; Select the article the normal way.
11365       (gnus-summary-select-article nil 'force)
11366     ;; Bind the article treatment functions to nil.
11367     (let ((gnus-have-all-headers t)
11368           gnus-article-display-hook
11369           gnus-article-prepare-hook
11370           gnus-break-pages
11371           gnus-visual)
11372       (gnus-summary-select-article nil 'force)))
11373   (gnus-summary-goto-subject gnus-current-article)
11374 ;  (gnus-configure-windows 'article)
11375   (gnus-summary-position-point))
11376
11377 (defun gnus-summary-verbose-headers (&optional arg)
11378   "Toggle permanent full header display.
11379 If ARG is a positive number, turn header display on.
11380 If ARG is a negative number, turn header display off."
11381   (interactive "P")
11382   (gnus-set-global-variables)
11383   (gnus-summary-toggle-header arg)
11384   (setq gnus-show-all-headers
11385         (cond ((or (not (numberp arg))
11386                    (zerop arg))
11387                (not gnus-show-all-headers))
11388               ((natnump arg)
11389                t))))
11390
11391 (defun gnus-summary-toggle-header (&optional arg)
11392   "Show the headers if they are hidden, or hide them if they are shown.
11393 If ARG is a positive number, show the entire header.
11394 If ARG is a negative number, hide the unwanted header lines."
11395   (interactive "P")
11396   (gnus-set-global-variables)
11397   (save-excursion
11398     (set-buffer gnus-article-buffer)
11399     (let* ((buffer-read-only nil)
11400            (inhibit-point-motion-hooks t)
11401            (hidden (text-property-any
11402                     (goto-char (point-min)) (search-forward "\n\n")
11403                     'invisible t))
11404            e)
11405       (goto-char (point-min))
11406       (when (search-forward "\n\n" nil t)
11407         (delete-region (point-min) (1- (point))))
11408       (goto-char (point-min))
11409       (save-excursion
11410         (set-buffer gnus-original-article-buffer)
11411         (goto-char (point-min))
11412         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11413       (insert-buffer-substring gnus-original-article-buffer 1 e)
11414       (let ((gnus-inhibit-hiding t))
11415         (run-hooks 'gnus-article-display-hook))
11416       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11417           (gnus-article-hide-headers)))))
11418
11419 (defun gnus-summary-show-all-headers ()
11420   "Make all header lines visible."
11421   (interactive)
11422   (gnus-set-global-variables)
11423   (gnus-article-show-all-headers))
11424
11425 (defun gnus-summary-toggle-mime (&optional arg)
11426   "Toggle MIME processing.
11427 If ARG is a positive number, turn MIME processing on."
11428   (interactive "P")
11429   (gnus-set-global-variables)
11430   (setq gnus-show-mime
11431         (if (null arg) (not gnus-show-mime)
11432           (> (prefix-numeric-value arg) 0)))
11433   (gnus-summary-select-article t 'force))
11434
11435 (defun gnus-summary-caesar-message (&optional arg)
11436   "Caesar rotate the current article by 13.
11437 The numerical prefix specifies how manu places to rotate each letter
11438 forward."
11439   (interactive "P")
11440   (gnus-set-global-variables)
11441   (gnus-summary-select-article)
11442   (let ((mail-header-separator ""))
11443     (gnus-eval-in-buffer-window gnus-article-buffer
11444       (save-restriction
11445         (widen)
11446         (let ((start (window-start))
11447               buffer-read-only)
11448           (message-caesar-buffer-body arg)
11449           (set-window-start (get-buffer-window (current-buffer)) start))))))
11450
11451 (defun gnus-summary-stop-page-breaking ()
11452   "Stop page breaking in the current article."
11453   (interactive)
11454   (gnus-set-global-variables)
11455   (gnus-summary-select-article)
11456   (gnus-eval-in-buffer-window gnus-article-buffer
11457     (widen)))
11458
11459 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11460   "Move the current article to a different newsgroup.
11461 If N is a positive number, move the N next articles.
11462 If N is a negative number, move the N previous articles.
11463 If N is nil and any articles have been marked with the process mark,
11464 move those articles instead.
11465 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11466 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11467 re-spool using this method.
11468
11469 For this function to work, both the current newsgroup and the
11470 newsgroup that you want to move to have to support the `request-move'
11471 and `request-accept' functions."
11472   (interactive "P")
11473   (unless action (setq action 'move))
11474   (gnus-set-global-variables)
11475   ;; Check whether the source group supports the required functions.
11476   (cond ((and (eq action 'move)
11477               (not (gnus-check-backend-function
11478                     'request-move-article gnus-newsgroup-name)))
11479          (error "The current group does not support article moving"))
11480         ((and (eq action 'crosspost)
11481               (not (gnus-check-backend-function
11482                     'request-replace-article gnus-newsgroup-name)))
11483          (error "The current group does not support article editing")))
11484   (let ((articles (gnus-summary-work-articles n))
11485         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11486         (names '((move "Move" "Moving")
11487                  (copy "Copy" "Copying")
11488                  (crosspost "Crosspost" "Crossposting")))
11489         (copy-buf (save-excursion
11490                     (nnheader-set-temp-buffer " *copy article*")))
11491         art-group to-method new-xref article to-groups)
11492     (unless (assq action names)
11493       (error "Unknown action %s" action))
11494     ;; Read the newsgroup name.
11495     (when (and (not to-newsgroup)
11496                (not select-method))
11497       (setq to-newsgroup
11498             (gnus-read-move-group-name
11499              (cadr (assq action names))
11500              (symbol-value (intern (format "gnus-current-%s-group" action)))
11501              articles prefix))
11502       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11503     (setq to-method (or select-method 
11504                         (gnus-group-name-to-method to-newsgroup)))
11505     ;; Check the method we are to move this article to...
11506     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11507         (error "%s does not support article copying" (car to-method)))
11508     (or (gnus-check-server to-method)
11509         (error "Can't open server %s" (car to-method)))
11510     (gnus-message 6 "%s to %s: %s..."
11511                   (caddr (assq action names))
11512                   (or (car select-method) to-newsgroup) articles)
11513     (while articles
11514       (setq article (pop articles))
11515       (setq
11516        art-group
11517        (cond
11518         ;; Move the article.
11519         ((eq action 'move)
11520          (gnus-request-move-article
11521           article                       ; Article to move
11522           gnus-newsgroup-name           ; From newsgrouo
11523           (nth 1 (gnus-find-method-for-group
11524                   gnus-newsgroup-name)) ; Server
11525           (list 'gnus-request-accept-article
11526                 to-newsgroup (list 'quote select-method)
11527                 (not articles))         ; Accept form
11528           (not articles)))              ; Only save nov last time
11529         ;; Copy the article.
11530         ((eq action 'copy)
11531          (save-excursion
11532            (set-buffer copy-buf)
11533            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11534            (gnus-request-accept-article
11535             to-newsgroup select-method (not articles))))
11536         ;; Crosspost the article.
11537         ((eq action 'crosspost)
11538          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11539            (setq new-xref (concat gnus-newsgroup-name ":" article))
11540            (if (and xref (not (string= xref "")))
11541                (progn
11542                  (when (string-match "^Xref: " xref)
11543                    (setq xref (substring xref (match-end 0))))
11544                  (setq new-xref (concat xref " " new-xref)))
11545              (setq new-xref (concat (system-name) " " new-xref)))
11546            (save-excursion
11547              (set-buffer copy-buf)
11548              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11549              (nnheader-replace-header "xref" new-xref)
11550              (gnus-request-accept-article
11551               to-newsgroup select-method (not articles)))))))
11552       (if (not art-group)
11553           (gnus-message 1 "Couldn't %s article %s"
11554                         (cadr (assq action names)) article)
11555         (let* ((entry
11556                 (or
11557                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11558                  (gnus-gethash
11559                   (gnus-group-prefixed-name
11560                    (car art-group)
11561                    (or select-method 
11562                        (gnus-find-method-for-group to-newsgroup)))
11563                   gnus-newsrc-hashtb)))
11564                (info (nth 2 entry))
11565                (to-group (gnus-info-group info)))
11566           ;; Update the group that has been moved to.
11567           (when (and info
11568                      (memq action '(move copy)))
11569             (unless (member to-group to-groups)
11570               (push to-group to-groups))
11571
11572             (unless (memq article gnus-newsgroup-unreads)
11573               (gnus-info-set-read
11574                info (gnus-add-to-range (gnus-info-read info)
11575                                        (list (cdr art-group)))))
11576
11577             ;; Copy any marks over to the new group.
11578             (let ((marks gnus-article-mark-lists)
11579                   (to-article (cdr art-group)))
11580
11581               ;; See whether the article is to be put in the cache.
11582               (when gnus-use-cache
11583                 (gnus-cache-possibly-enter-article
11584                  to-group to-article
11585                  (let ((header (copy-sequence
11586                                 (gnus-summary-article-header article))))
11587                    (mail-header-set-number header to-article)
11588                    header)
11589                  (memq article gnus-newsgroup-marked)
11590                  (memq article gnus-newsgroup-dormant)
11591                  (memq article gnus-newsgroup-unreads)))
11592
11593               (while marks
11594                 (when (memq article (symbol-value
11595                                      (intern (format "gnus-newsgroup-%s"
11596                                                      (caar marks)))))
11597                   ;; If the other group is the same as this group,
11598                   ;; then we have to add the mark to the list.
11599                   (when (equal to-group gnus-newsgroup-name)
11600                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
11601                          (cons to-article
11602                                (symbol-value
11603                                 (intern (format "gnus-newsgroup-%s"
11604                                                 (caar marks)))))))
11605                   ;; Copy mark to other group.
11606                   (gnus-add-marked-articles
11607                    to-group (cdar marks) (list to-article) info))
11608                 (setq marks (cdr marks)))))
11609
11610           ;; Update the Xref header in this article to point to
11611           ;; the new crossposted article we have just created.
11612           (when (eq action 'crosspost)
11613             (save-excursion
11614               (set-buffer copy-buf)
11615               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11616               (nnheader-replace-header
11617                "xref" (concat new-xref " " (gnus-group-prefixed-name
11618                                             (car art-group) to-method)
11619                               ":" (cdr art-group)))
11620               (gnus-request-replace-article
11621                article gnus-newsgroup-name (current-buffer)))))
11622
11623         (gnus-summary-goto-subject article)
11624         (when (eq action 'move)
11625           (gnus-summary-mark-article article gnus-canceled-mark)))
11626       (gnus-summary-remove-process-mark article))
11627     ;; Re-activate all groups that have been moved to.
11628     (while to-groups
11629       (gnus-activate-group (pop to-groups)))
11630     
11631     (gnus-kill-buffer copy-buf)
11632     (gnus-summary-position-point)
11633     (gnus-set-mode-line 'summary)))
11634
11635 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11636   "Move the current article to a different newsgroup.
11637 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11638 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11639 re-spool using this method."
11640   (interactive "P")
11641   (gnus-summary-move-article n nil select-method 'copy))
11642
11643 (defun gnus-summary-crosspost-article (&optional n)
11644   "Crosspost the current article to some other group."
11645   (interactive "P")
11646   (gnus-summary-move-article n nil nil 'crosspost))
11647
11648 (defvar gnus-summary-respool-default-method nil
11649   "Default method for respooling an article.  
11650 If nil, use to the current newsgroup method.")
11651
11652 (defun gnus-summary-respool-article (&optional n method)
11653   "Respool the current article.
11654 The article will be squeezed through the mail spooling process again,
11655 which means that it will be put in some mail newsgroup or other
11656 depending on `nnmail-split-methods'.
11657 If N is a positive number, respool the N next articles.
11658 If N is a negative number, respool the N previous articles.
11659 If N is nil and any articles have been marked with the process mark,
11660 respool those articles instead.
11661
11662 Respooling can be done both from mail groups and \"real\" newsgroups.
11663 In the former case, the articles in question will be moved from the
11664 current group into whatever groups they are destined to.  In the
11665 latter case, they will be copied into the relevant groups."
11666   (interactive 
11667    (list current-prefix-arg
11668          (let* ((methods (gnus-methods-using 'respool))
11669                 (methname
11670                  (symbol-name (or gnus-summary-respool-default-method
11671                                   (car (gnus-find-method-for-group
11672                                         gnus-newsgroup-name)))))
11673                 (method
11674                  (gnus-completing-read 
11675                   methname "What backend do you want to use when respooling?"
11676                   methods nil t nil 'gnus-method-history))
11677                 ms)
11678            (cond
11679             ((zerop (length (setq ms (gnus-servers-using-backend method))))
11680              (list (intern method) ""))
11681             ((= 1 (length ms))
11682              (car ms))
11683             (t
11684              (cdr (completing-read 
11685                    "Server name: "
11686                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
11687   (gnus-set-global-variables)
11688   (unless method
11689     (error "No method given for respooling"))
11690   (if (assoc (symbol-name
11691               (car (gnus-find-method-for-group gnus-newsgroup-name)))
11692              (gnus-methods-using 'respool))
11693       (gnus-summary-move-article n nil method)
11694     (gnus-summary-copy-article n nil method)))
11695
11696 (defun gnus-summary-import-article (file)
11697   "Import a random file into a mail newsgroup."
11698   (interactive "fImport file: ")
11699   (gnus-set-global-variables)
11700   (let ((group gnus-newsgroup-name)
11701         (now (current-time))
11702         atts lines)
11703     (or (gnus-check-backend-function 'request-accept-article group)
11704         (error "%s does not support article importing" group))
11705     (or (file-readable-p file)
11706         (not (file-regular-p file))
11707         (error "Can't read %s" file))
11708     (save-excursion
11709       (set-buffer (get-buffer-create " *import file*"))
11710       (buffer-disable-undo (current-buffer))
11711       (erase-buffer)
11712       (insert-file-contents file)
11713       (goto-char (point-min))
11714       (unless (nnheader-article-p)
11715         ;; This doesn't look like an article, so we fudge some headers.
11716         (setq atts (file-attributes file)
11717               lines (count-lines (point-min) (point-max)))
11718         (insert "From: " (read-string "From: ") "\n"
11719                 "Subject: " (read-string "Subject: ") "\n"
11720                 "Date: " (timezone-make-date-arpa-standard
11721                           (current-time-string (nth 5 atts))
11722                           (current-time-zone now)
11723                           (current-time-zone now)) "\n"
11724                 "Message-ID: " (message-make-message-id) "\n"
11725                 "Lines: " (int-to-string lines) "\n"
11726                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11727       (gnus-request-accept-article group nil t)
11728       (kill-buffer (current-buffer)))))
11729
11730 (defun gnus-summary-expire-articles (&optional now)
11731   "Expire all articles that are marked as expirable in the current group."
11732   (interactive)
11733   (gnus-set-global-variables)
11734   (when (gnus-check-backend-function
11735          'request-expire-articles gnus-newsgroup-name)
11736     ;; This backend supports expiry.
11737     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11738            (expirable (if total
11739                           (gnus-list-of-read-articles gnus-newsgroup-name)
11740                         (setq gnus-newsgroup-expirable
11741                               (sort gnus-newsgroup-expirable '<))))
11742            (expiry-wait (if now 'immediate
11743                           (gnus-group-get-parameter
11744                            gnus-newsgroup-name 'expiry-wait)))
11745            es)
11746       (when expirable
11747         ;; There are expirable articles in this group, so we run them
11748         ;; through the expiry process.
11749         (gnus-message 6 "Expiring articles...")
11750         ;; The list of articles that weren't expired is returned.
11751         (if expiry-wait
11752             (let ((nnmail-expiry-wait-function nil)
11753                   (nnmail-expiry-wait expiry-wait))
11754               (setq es (gnus-request-expire-articles
11755                         expirable gnus-newsgroup-name)))
11756           (setq es (gnus-request-expire-articles
11757                     expirable gnus-newsgroup-name)))
11758         (or total (setq gnus-newsgroup-expirable es))
11759         ;; We go through the old list of expirable, and mark all
11760         ;; really expired articles as nonexistent.
11761         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11762           (let ((gnus-use-cache nil))
11763             (while expirable
11764               (unless (memq (car expirable) es)
11765                 (when (gnus-data-find (car expirable))
11766                   (gnus-summary-mark-article
11767                    (car expirable) gnus-canceled-mark)))
11768               (setq expirable (cdr expirable)))))
11769         (gnus-message 6 "Expiring articles...done")))))
11770
11771 (defun gnus-summary-expire-articles-now ()
11772   "Expunge all expirable articles in the current group.
11773 This means that *all* articles that are marked as expirable will be
11774 deleted forever, right now."
11775   (interactive)
11776   (gnus-set-global-variables)
11777   (or gnus-expert-user
11778       (gnus-y-or-n-p
11779        "Are you really, really, really sure you want to delete all these messages? ")
11780       (error "Phew!"))
11781   (gnus-summary-expire-articles t))
11782
11783 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11784 (defun gnus-summary-delete-article (&optional n)
11785   "Delete the N next (mail) articles.
11786 This command actually deletes articles.  This is not a marking
11787 command.  The article will disappear forever from your life, never to
11788 return.
11789 If N is negative, delete backwards.
11790 If N is nil and articles have been marked with the process mark,
11791 delete these instead."
11792   (interactive "P")
11793   (gnus-set-global-variables)
11794   (or (gnus-check-backend-function 'request-expire-articles
11795                                    gnus-newsgroup-name)
11796       (error "The current newsgroup does not support article deletion."))
11797   ;; Compute the list of articles to delete.
11798   (let ((articles (gnus-summary-work-articles n))
11799         not-deleted)
11800     (if (and gnus-novice-user
11801              (not (gnus-y-or-n-p
11802                    (format "Do you really want to delete %s forever? "
11803                            (if (> (length articles) 1) 
11804                                (format "these %s articles" (length articles))
11805                              "this article")))))
11806         ()
11807       ;; Delete the articles.
11808       (setq not-deleted (gnus-request-expire-articles
11809                          articles gnus-newsgroup-name 'force))
11810       (while articles
11811         (gnus-summary-remove-process-mark (car articles))
11812         ;; The backend might not have been able to delete the article
11813         ;; after all.
11814         (or (memq (car articles) not-deleted)
11815             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11816         (setq articles (cdr articles))))
11817     (gnus-summary-position-point)
11818     (gnus-set-mode-line 'summary)
11819     not-deleted))
11820
11821 (defun gnus-summary-edit-article (&optional force)
11822   "Enter into a buffer and edit the current article.
11823 This will have permanent effect only in mail groups.
11824 If FORCE is non-nil, allow editing of articles even in read-only
11825 groups."
11826   (interactive "P")
11827   (save-excursion
11828     (set-buffer gnus-summary-buffer)
11829     (gnus-set-global-variables)
11830     (when (and (not force)
11831                (gnus-group-read-only-p))
11832       (error "The current newsgroup does not support article editing."))
11833     (gnus-summary-select-article t nil t)
11834     (gnus-configure-windows 'article)
11835     (select-window (get-buffer-window gnus-article-buffer))
11836     (gnus-message 6 "C-c C-c to end edits")
11837     (setq buffer-read-only nil)
11838     (text-mode)
11839     (use-local-map (copy-keymap (current-local-map)))
11840     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11841     (buffer-enable-undo)
11842     (widen)
11843     (goto-char (point-min))
11844     (search-forward "\n\n" nil t)))
11845
11846 (defun gnus-summary-edit-article-done ()
11847   "Make edits to the current article permanent."
11848   (interactive)
11849   (if (gnus-group-read-only-p)
11850       (progn
11851         (gnus-summary-edit-article-postpone)
11852         (gnus-error
11853          1 "The current newsgroup does not support article editing."))
11854     (let ((buf (format "%s" (buffer-string))))
11855       (erase-buffer)
11856       (insert buf)
11857       (if (not (gnus-request-replace-article
11858                 (cdr gnus-article-current) (car gnus-article-current)
11859                 (current-buffer)))
11860           (error "Couldn't replace article.")
11861         (gnus-article-mode)
11862         (use-local-map gnus-article-mode-map)
11863         (setq buffer-read-only t)
11864         (buffer-disable-undo (current-buffer))
11865         (gnus-configure-windows 'summary)
11866         (gnus-summary-update-article (cdr gnus-article-current))
11867         (when gnus-use-cache
11868           (gnus-cache-update-article    
11869            (car gnus-article-current) (cdr gnus-article-current)))
11870         (when gnus-keep-backlog
11871           (gnus-backlog-remove-article 
11872            (car gnus-article-current) (cdr gnus-article-current))))
11873       (save-excursion
11874         (when (get-buffer gnus-original-article-buffer)
11875           (set-buffer gnus-original-article-buffer)
11876           (setq gnus-original-article nil)))
11877       (setq gnus-article-current nil
11878             gnus-current-article nil)
11879       (run-hooks 'gnus-article-display-hook)
11880       (and (gnus-visual-p 'summary-highlight 'highlight)
11881            (run-hooks 'gnus-visual-mark-article-hook)))))
11882
11883 (defun gnus-summary-edit-article-postpone ()
11884   "Postpone changes to the current article."
11885   (interactive)
11886   (gnus-article-mode)
11887   (use-local-map gnus-article-mode-map)
11888   (setq buffer-read-only t)
11889   (buffer-disable-undo (current-buffer))
11890   (gnus-configure-windows 'summary)
11891   (and (gnus-visual-p 'summary-highlight 'highlight)
11892        (run-hooks 'gnus-visual-mark-article-hook)))
11893
11894 (defun gnus-summary-respool-query ()
11895   "Query where the respool algorithm would put this article."
11896   (interactive)
11897   (gnus-set-global-variables)
11898   (gnus-summary-select-article)
11899   (save-excursion
11900     (set-buffer gnus-article-buffer)
11901     (save-restriction
11902       (goto-char (point-min))
11903       (search-forward "\n\n")
11904       (narrow-to-region (point-min) (point))
11905       (pp-eval-expression
11906        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11907
11908 ;; Summary marking commands.
11909
11910 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11911   "Mark articles which has the same subject as read, and then select the next.
11912 If UNMARK is positive, remove any kind of mark.
11913 If UNMARK is negative, tick articles."
11914   (interactive "P")
11915   (gnus-set-global-variables)
11916   (if unmark
11917       (setq unmark (prefix-numeric-value unmark)))
11918   (let ((count
11919          (gnus-summary-mark-same-subject
11920           (gnus-summary-article-subject) unmark)))
11921     ;; Select next unread article.  If auto-select-same mode, should
11922     ;; select the first unread article.
11923     (gnus-summary-next-article t (and gnus-auto-select-same
11924                                       (gnus-summary-article-subject)))
11925     (gnus-message 7 "%d article%s marked as %s"
11926                   count (if (= count 1) " is" "s are")
11927                   (if unmark "unread" "read"))))
11928
11929 (defun gnus-summary-kill-same-subject (&optional unmark)
11930   "Mark articles which has the same subject as read.
11931 If UNMARK is positive, remove any kind of mark.
11932 If UNMARK is negative, tick articles."
11933   (interactive "P")
11934   (gnus-set-global-variables)
11935   (if unmark
11936       (setq unmark (prefix-numeric-value unmark)))
11937   (let ((count
11938          (gnus-summary-mark-same-subject
11939           (gnus-summary-article-subject) unmark)))
11940     ;; If marked as read, go to next unread subject.
11941     (if (null unmark)
11942         ;; Go to next unread subject.
11943         (gnus-summary-next-subject 1 t))
11944     (gnus-message 7 "%d articles are marked as %s"
11945                   count (if unmark "unread" "read"))))
11946
11947 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11948   "Mark articles with same SUBJECT as read, and return marked number.
11949 If optional argument UNMARK is positive, remove any kinds of marks.
11950 If optional argument UNMARK is negative, mark articles as unread instead."
11951   (let ((count 1))
11952     (save-excursion
11953       (cond
11954        ((null unmark)                   ; Mark as read.
11955         (while (and
11956                 (progn
11957                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11958                   (gnus-summary-show-thread) t)
11959                 (gnus-summary-find-subject subject))
11960           (setq count (1+ count))))
11961        ((> unmark 0)                    ; Tick.
11962         (while (and
11963                 (progn
11964                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11965                   (gnus-summary-show-thread) t)
11966                 (gnus-summary-find-subject subject))
11967           (setq count (1+ count))))
11968        (t                               ; Mark as unread.
11969         (while (and
11970                 (progn
11971                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11972                   (gnus-summary-show-thread) t)
11973                 (gnus-summary-find-subject subject))
11974           (setq count (1+ count)))))
11975       (gnus-set-mode-line 'summary)
11976       ;; Return the number of marked articles.
11977       count)))
11978
11979 (defun gnus-summary-mark-as-processable (n &optional unmark)
11980   "Set the process mark on the next N articles.
11981 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11982 the process mark instead.  The difference between N and the actual
11983 number of articles marked is returned."
11984   (interactive "p")
11985   (gnus-set-global-variables)
11986   (let ((backward (< n 0))
11987         (n (abs n)))
11988     (while (and
11989             (> n 0)
11990             (if unmark
11991                 (gnus-summary-remove-process-mark
11992                  (gnus-summary-article-number))
11993               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11994             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11995       (setq n (1- n)))
11996     (if (/= 0 n) (gnus-message 7 "No more articles"))
11997     (gnus-summary-recenter)
11998     (gnus-summary-position-point)
11999     n))
12000
12001 (defun gnus-summary-unmark-as-processable (n)
12002   "Remove the process mark from the next N articles.
12003 If N is negative, mark backward instead.  The difference between N and
12004 the actual number of articles marked is returned."
12005   (interactive "p")
12006   (gnus-set-global-variables)
12007   (gnus-summary-mark-as-processable n t))
12008
12009 (defun gnus-summary-unmark-all-processable ()
12010   "Remove the process mark from all articles."
12011   (interactive)
12012   (gnus-set-global-variables)
12013   (save-excursion
12014     (while gnus-newsgroup-processable
12015       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
12016   (gnus-summary-position-point))
12017
12018 (defun gnus-summary-mark-as-expirable (n)
12019   "Mark N articles forward as expirable.
12020 If N is negative, mark backward instead.  The difference between N and
12021 the actual number of articles marked is returned."
12022   (interactive "p")
12023   (gnus-set-global-variables)
12024   (gnus-summary-mark-forward n gnus-expirable-mark))
12025
12026 (defun gnus-summary-mark-article-as-replied (article)
12027   "Mark ARTICLE replied and update the summary line."
12028   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
12029   (let ((buffer-read-only nil))
12030     (when (gnus-summary-goto-subject article)
12031       (gnus-summary-update-secondary-mark article))))
12032
12033 (defun gnus-summary-set-bookmark (article)
12034   "Set a bookmark in current article."
12035   (interactive (list (gnus-summary-article-number)))
12036   (gnus-set-global-variables)
12037   (if (or (not (get-buffer gnus-article-buffer))
12038           (not gnus-current-article)
12039           (not gnus-article-current)
12040           (not (equal gnus-newsgroup-name (car gnus-article-current))))
12041       (error "No current article selected"))
12042   ;; Remove old bookmark, if one exists.
12043   (let ((old (assq article gnus-newsgroup-bookmarks)))
12044     (if old (setq gnus-newsgroup-bookmarks
12045                   (delq old gnus-newsgroup-bookmarks))))
12046   ;; Set the new bookmark, which is on the form
12047   ;; (article-number . line-number-in-body).
12048   (setq gnus-newsgroup-bookmarks
12049         (cons
12050          (cons article
12051                (save-excursion
12052                  (set-buffer gnus-article-buffer)
12053                  (count-lines
12054                   (min (point)
12055                        (save-excursion
12056                          (goto-char (point-min))
12057                          (search-forward "\n\n" nil t)
12058                          (point)))
12059                   (point))))
12060          gnus-newsgroup-bookmarks))
12061   (gnus-message 6 "A bookmark has been added to the current article."))
12062
12063 (defun gnus-summary-remove-bookmark (article)
12064   "Remove the bookmark from the current article."
12065   (interactive (list (gnus-summary-article-number)))
12066   (gnus-set-global-variables)
12067   ;; Remove old bookmark, if one exists.
12068   (let ((old (assq article gnus-newsgroup-bookmarks)))
12069     (if old
12070         (progn
12071           (setq gnus-newsgroup-bookmarks
12072                 (delq old gnus-newsgroup-bookmarks))
12073           (gnus-message 6 "Removed bookmark."))
12074       (gnus-message 6 "No bookmark in current article."))))
12075
12076 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12077 (defun gnus-summary-mark-as-dormant (n)
12078   "Mark N articles forward as dormant.
12079 If N is negative, mark backward instead.  The difference between N and
12080 the actual number of articles marked is returned."
12081   (interactive "p")
12082   (gnus-set-global-variables)
12083   (gnus-summary-mark-forward n gnus-dormant-mark))
12084
12085 (defun gnus-summary-set-process-mark (article)
12086   "Set the process mark on ARTICLE and update the summary line."
12087   (setq gnus-newsgroup-processable
12088         (cons article
12089               (delq article gnus-newsgroup-processable)))
12090   (when (gnus-summary-goto-subject article)
12091     (gnus-summary-show-thread)
12092     (gnus-summary-update-secondary-mark article)))
12093
12094 (defun gnus-summary-remove-process-mark (article)
12095   "Remove the process mark from ARTICLE and update the summary line."
12096   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
12097   (when (gnus-summary-goto-subject article)
12098     (gnus-summary-show-thread)
12099     (gnus-summary-update-secondary-mark article)))
12100
12101 (defun gnus-summary-set-saved-mark (article)
12102   "Set the process mark on ARTICLE and update the summary line."
12103   (push article gnus-newsgroup-saved)
12104   (when (gnus-summary-goto-subject article)
12105     (gnus-summary-update-secondary-mark article)))
12106
12107 (defun gnus-summary-mark-forward (n &optional mark no-expire)
12108   "Mark N articles as read forwards.
12109 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
12110 The difference between N and the actual number of articles marked is
12111 returned."
12112   (interactive "p")
12113   (gnus-set-global-variables)
12114   (let ((backward (< n 0))
12115         (gnus-summary-goto-unread
12116          (and gnus-summary-goto-unread
12117               (not (eq gnus-summary-goto-unread 'never))
12118               (not (memq mark (list gnus-unread-mark
12119                                     gnus-ticked-mark gnus-dormant-mark)))))
12120         (n (abs n))
12121         (mark (or mark gnus-del-mark)))
12122     (while (and (> n 0)
12123                 (gnus-summary-mark-article nil mark no-expire)
12124                 (zerop (gnus-summary-next-subject
12125                         (if backward -1 1)
12126                         (and gnus-summary-goto-unread
12127                              (not (eq gnus-summary-goto-unread 'never)))
12128                         t)))
12129       (setq n (1- n)))
12130     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12131     (gnus-summary-recenter)
12132     (gnus-summary-position-point)
12133     (gnus-set-mode-line 'summary)
12134     n))
12135
12136 (defun gnus-summary-mark-article-as-read (mark)
12137   "Mark the current article quickly as read with MARK."
12138   (let ((article (gnus-summary-article-number)))
12139     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12140     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12141     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12142     (setq gnus-newsgroup-reads
12143           (cons (cons article mark) gnus-newsgroup-reads))
12144     ;; Possibly remove from cache, if that is used.
12145     (and gnus-use-cache (gnus-cache-enter-remove-article article))
12146     ;; Allow the backend to change the mark.
12147     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
12148     ;; Check for auto-expiry.
12149     (when (and gnus-newsgroup-auto-expire
12150                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12151                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12152                    (= mark gnus-ancient-mark)
12153                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
12154       (setq mark gnus-expirable-mark)
12155       (push article gnus-newsgroup-expirable))
12156     ;; Set the mark in the buffer.
12157     (gnus-summary-update-mark mark 'unread)
12158     t))
12159
12160 (defun gnus-summary-mark-article-as-unread (mark)
12161   "Mark the current article quickly as unread with MARK."
12162   (let ((article (gnus-summary-article-number)))
12163     (if (< article 0)
12164         (gnus-error 1 "Unmarkable article")
12165       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12166       (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12167       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12168       (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
12169       (cond ((= mark gnus-ticked-mark)
12170              (push article gnus-newsgroup-marked))
12171             ((= mark gnus-dormant-mark)
12172              (push article gnus-newsgroup-dormant))
12173             (t
12174              (push article gnus-newsgroup-unreads)))
12175       (setq gnus-newsgroup-reads
12176             (delq (assq article gnus-newsgroup-reads)
12177                   gnus-newsgroup-reads))
12178
12179       ;; See whether the article is to be put in the cache.
12180       (and gnus-use-cache
12181            (vectorp (gnus-summary-article-header article))
12182            (save-excursion
12183              (gnus-cache-possibly-enter-article
12184               gnus-newsgroup-name article
12185               (gnus-summary-article-header article)
12186               (= mark gnus-ticked-mark)
12187               (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12188
12189       ;; Fix the mark.
12190       (gnus-summary-update-mark mark 'unread))
12191     t))
12192
12193 (defun gnus-summary-mark-article (&optional article mark no-expire)
12194   "Mark ARTICLE with MARK.  MARK can be any character.
12195 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
12196 `??' (dormant) and `?E' (expirable).
12197 If MARK is nil, then the default character `?D' is used.
12198 If ARTICLE is nil, then the article on the current line will be
12199 marked."
12200   ;; The mark might be a string.
12201   (and (stringp mark)
12202        (setq mark (aref mark 0)))
12203   ;; If no mark is given, then we check auto-expiring.
12204   (and (not no-expire)
12205        gnus-newsgroup-auto-expire
12206        (or (not mark)
12207            (and (numberp mark)
12208                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12209                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12210                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
12211        (setq mark gnus-expirable-mark))
12212   (let* ((mark (or mark gnus-del-mark))
12213          (article (or article (gnus-summary-article-number))))
12214     (or article (error "No article on current line"))
12215     (if (or (= mark gnus-unread-mark)
12216             (= mark gnus-ticked-mark)
12217             (= mark gnus-dormant-mark))
12218         (gnus-mark-article-as-unread article mark)
12219       (gnus-mark-article-as-read article mark))
12220
12221     ;; See whether the article is to be put in the cache.
12222     (and gnus-use-cache
12223          (not (= mark gnus-canceled-mark))
12224          (vectorp (gnus-summary-article-header article))
12225          (save-excursion
12226            (gnus-cache-possibly-enter-article
12227             gnus-newsgroup-name article
12228             (gnus-summary-article-header article)
12229             (= mark gnus-ticked-mark)
12230             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12231
12232     (if (gnus-summary-goto-subject article nil t)
12233         (let ((buffer-read-only nil))
12234           (gnus-summary-show-thread)
12235           ;; Fix the mark.
12236           (gnus-summary-update-mark mark 'unread)
12237           t))))
12238
12239 (defun gnus-summary-update-secondary-mark (article)
12240   "Update the secondary (read, process, cache) mark."
12241   (gnus-summary-update-mark
12242    (cond ((memq article gnus-newsgroup-processable)
12243           gnus-process-mark)
12244          ((memq article gnus-newsgroup-cached)
12245           gnus-cached-mark)
12246          ((memq article gnus-newsgroup-replied)
12247           gnus-replied-mark)
12248          ((memq article gnus-newsgroup-saved)
12249           gnus-saved-mark)
12250          (t gnus-unread-mark))
12251    'replied)
12252   (when (gnus-visual-p 'summary-highlight 'highlight)
12253     (run-hooks 'gnus-summary-update-hook))
12254   t)
12255
12256 (defun gnus-summary-update-mark (mark type)
12257   (beginning-of-line)
12258   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
12259         (buffer-read-only nil))
12260     (when (and forward
12261                (<= (+ forward (point)) (point-max)))
12262       ;; Go to the right position on the line.
12263       (goto-char (+ forward (point)))
12264       ;; Replace the old mark with the new mark.
12265       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
12266       ;; Optionally update the marks by some user rule.
12267       (when (eq type 'unread)
12268         (gnus-data-set-mark
12269          (gnus-data-find (gnus-summary-article-number)) mark)
12270         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
12271
12272 (defun gnus-mark-article-as-read (article &optional mark)
12273   "Enter ARTICLE in the pertinent lists and remove it from others."
12274   ;; Make the article expirable.
12275   (let ((mark (or mark gnus-del-mark)))
12276     (if (= mark gnus-expirable-mark)
12277         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
12278       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
12279     ;; Remove from unread and marked lists.
12280     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12281     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12282     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12283     (push (cons article mark) gnus-newsgroup-reads)
12284     ;; Possibly remove from cache, if that is used.
12285     (when gnus-use-cache
12286       (gnus-cache-enter-remove-article article))))
12287
12288 (defun gnus-mark-article-as-unread (article &optional mark)
12289   "Enter ARTICLE in the pertinent lists and remove it from others."
12290   (let ((mark (or mark gnus-ticked-mark)))
12291     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12292     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12293     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12294     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12295     (cond ((= mark gnus-ticked-mark)
12296            (push article gnus-newsgroup-marked))
12297           ((= mark gnus-dormant-mark)
12298            (push article gnus-newsgroup-dormant))
12299           (t
12300            (push article gnus-newsgroup-unreads)))
12301     (setq gnus-newsgroup-reads
12302           (delq (assq article gnus-newsgroup-reads)
12303                 gnus-newsgroup-reads))))
12304
12305 (defalias 'gnus-summary-mark-as-unread-forward
12306   'gnus-summary-tick-article-forward)
12307 (make-obsolete 'gnus-summary-mark-as-unread-forward
12308                'gnus-summary-tick-article-forward)
12309 (defun gnus-summary-tick-article-forward (n)
12310   "Tick N articles forwards.
12311 If N is negative, tick backwards instead.
12312 The difference between N and the number of articles ticked is returned."
12313   (interactive "p")
12314   (gnus-summary-mark-forward n gnus-ticked-mark))
12315
12316 (defalias 'gnus-summary-mark-as-unread-backward
12317   'gnus-summary-tick-article-backward)
12318 (make-obsolete 'gnus-summary-mark-as-unread-backward
12319                'gnus-summary-tick-article-backward)
12320 (defun gnus-summary-tick-article-backward (n)
12321   "Tick N articles backwards.
12322 The difference between N and the number of articles ticked is returned."
12323   (interactive "p")
12324   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
12325
12326 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12327 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12328 (defun gnus-summary-tick-article (&optional article clear-mark)
12329   "Mark current article as unread.
12330 Optional 1st argument ARTICLE specifies article number to be marked as unread.
12331 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
12332   (interactive)
12333   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
12334                                        gnus-ticked-mark)))
12335
12336 (defun gnus-summary-mark-as-read-forward (n)
12337   "Mark N articles as read forwards.
12338 If N is negative, mark backwards instead.
12339 The difference between N and the actual number of articles marked is
12340 returned."
12341   (interactive "p")
12342   (gnus-summary-mark-forward n gnus-del-mark t))
12343
12344 (defun gnus-summary-mark-as-read-backward (n)
12345   "Mark the N articles as read backwards.
12346 The difference between N and the actual number of articles marked is
12347 returned."
12348   (interactive "p")
12349   (gnus-summary-mark-forward (- n) gnus-del-mark t))
12350
12351 (defun gnus-summary-mark-as-read (&optional article mark)
12352   "Mark current article as read.
12353 ARTICLE specifies the article to be marked as read.
12354 MARK specifies a string to be inserted at the beginning of the line."
12355   (gnus-summary-mark-article article mark))
12356
12357 (defun gnus-summary-clear-mark-forward (n)
12358   "Clear marks from N articles forward.
12359 If N is negative, clear backward instead.
12360 The difference between N and the number of marks cleared is returned."
12361   (interactive "p")
12362   (gnus-summary-mark-forward n gnus-unread-mark))
12363
12364 (defun gnus-summary-clear-mark-backward (n)
12365   "Clear marks from N articles backward.
12366 The difference between N and the number of marks cleared is returned."
12367   (interactive "p")
12368   (gnus-summary-mark-forward (- n) gnus-unread-mark))
12369
12370 (defun gnus-summary-mark-unread-as-read ()
12371   "Intended to be used by `gnus-summary-mark-article-hook'."
12372   (when (memq gnus-current-article gnus-newsgroup-unreads)
12373     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
12374
12375 (defun gnus-summary-mark-read-and-unread-as-read ()
12376   "Intended to be used by `gnus-summary-mark-article-hook'."
12377   (let ((mark (gnus-summary-article-mark)))
12378     (when (or (gnus-unread-mark-p mark)
12379               (gnus-read-mark-p mark))
12380       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
12381
12382 (defun gnus-summary-mark-region-as-read (point mark all)
12383   "Mark all unread articles between point and mark as read.
12384 If given a prefix, mark all articles between point and mark as read,
12385 even ticked and dormant ones."
12386   (interactive "r\nP")
12387   (save-excursion
12388     (let (article)
12389       (goto-char point)
12390       (beginning-of-line)
12391       (while (and
12392               (< (point) mark)
12393               (progn
12394                 (when (or all
12395                           (memq (setq article (gnus-summary-article-number))
12396                                 gnus-newsgroup-unreads))
12397                   (gnus-summary-mark-article article gnus-del-mark))
12398                 t)
12399               (gnus-summary-find-next))))))
12400
12401 (defun gnus-summary-mark-below (score mark)
12402   "Mark articles with score less than SCORE with MARK."
12403   (interactive "P\ncMark: ")
12404   (gnus-set-global-variables)
12405   (setq score (if score
12406                   (prefix-numeric-value score)
12407                 (or gnus-summary-default-score 0)))
12408   (save-excursion
12409     (set-buffer gnus-summary-buffer)
12410     (goto-char (point-min))
12411     (while 
12412         (progn
12413           (and (< (gnus-summary-article-score) score)
12414                (gnus-summary-mark-article nil mark))
12415           (gnus-summary-find-next)))))
12416
12417 (defun gnus-summary-kill-below (&optional score)
12418   "Mark articles with score below SCORE as read."
12419   (interactive "P")
12420   (gnus-set-global-variables)
12421   (gnus-summary-mark-below score gnus-killed-mark))
12422
12423 (defun gnus-summary-clear-above (&optional score)
12424   "Clear all marks from articles with score above SCORE."
12425   (interactive "P")
12426   (gnus-set-global-variables)
12427   (gnus-summary-mark-above score gnus-unread-mark))
12428
12429 (defun gnus-summary-tick-above (&optional score)
12430   "Tick all articles with score above SCORE."
12431   (interactive "P")
12432   (gnus-set-global-variables)
12433   (gnus-summary-mark-above score gnus-ticked-mark))
12434
12435 (defun gnus-summary-mark-above (score mark)
12436   "Mark articles with score over SCORE with MARK."
12437   (interactive "P\ncMark: ")
12438   (gnus-set-global-variables)
12439   (setq score (if score
12440                   (prefix-numeric-value score)
12441                 (or gnus-summary-default-score 0)))
12442   (save-excursion
12443     (set-buffer gnus-summary-buffer)
12444     (goto-char (point-min))
12445     (while (and (progn
12446                   (if (> (gnus-summary-article-score) score)
12447                       (gnus-summary-mark-article nil mark))
12448                   t)
12449                 (gnus-summary-find-next)))))
12450
12451 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12452 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12453 (defun gnus-summary-limit-include-expunged ()
12454   "Display all the hidden articles that were expunged for low scores."
12455   (interactive)
12456   (gnus-set-global-variables)
12457   (let ((buffer-read-only nil))
12458     (let ((scored gnus-newsgroup-scored)
12459           headers h)
12460       (while scored
12461         (or (gnus-summary-goto-subject (caar scored))
12462             (and (setq h (gnus-summary-article-header (caar scored)))
12463                  (< (cdar scored) gnus-summary-expunge-below)
12464                  (setq headers (cons h headers))))
12465         (setq scored (cdr scored)))
12466       (or headers (error "No expunged articles hidden."))
12467       (goto-char (point-min))
12468       (gnus-summary-prepare-unthreaded (nreverse headers)))
12469     (goto-char (point-min))
12470     (gnus-summary-position-point)))
12471
12472 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12473   "Mark all articles not marked as unread in this newsgroup as read.
12474 If prefix argument ALL is non-nil, all articles are marked as read.
12475 If QUIETLY is non-nil, no questions will be asked.
12476 If TO-HERE is non-nil, it should be a point in the buffer.  All
12477 articles before this point will be marked as read.
12478 The number of articles marked as read is returned."
12479   (interactive "P")
12480   (gnus-set-global-variables)
12481   (prog1
12482       (if (or quietly
12483               (not gnus-interactive-catchup) ;Without confirmation?
12484               gnus-expert-user
12485               (gnus-y-or-n-p
12486                (if all
12487                    "Mark absolutely all articles as read? "
12488                  "Mark all unread articles as read? ")))
12489           (if (and not-mark
12490                    (not gnus-newsgroup-adaptive)
12491                    (not gnus-newsgroup-auto-expire))
12492               (progn
12493                 (when all
12494                   (setq gnus-newsgroup-marked nil
12495                         gnus-newsgroup-dormant nil))
12496                 (setq gnus-newsgroup-unreads nil))
12497             ;; We actually mark all articles as canceled, which we
12498             ;; have to do when using auto-expiry or adaptive scoring.
12499             (gnus-summary-show-all-threads)
12500             (if (gnus-summary-first-subject (not all))
12501                 (while (and
12502                         (if to-here (< (point) to-here) t)
12503                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12504                         (gnus-summary-find-next (not all)))))
12505             (unless to-here
12506               (setq gnus-newsgroup-unreads nil))
12507             (gnus-set-mode-line 'summary)))
12508     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12509       (if (and (not to-here) (eq 'nnvirtual (car method)))
12510           (nnvirtual-catchup-group
12511            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12512     (gnus-summary-position-point)))
12513
12514 (defun gnus-summary-catchup-to-here (&optional all)
12515   "Mark all unticked articles before the current one as read.
12516 If ALL is non-nil, also mark ticked and dormant articles as read."
12517   (interactive "P")
12518   (gnus-set-global-variables)
12519   (save-excursion
12520     (gnus-save-hidden-threads
12521       (let ((beg (point)))
12522         ;; We check that there are unread articles.
12523         (when (or all (gnus-summary-find-prev))
12524           (gnus-summary-catchup all t beg)))))
12525   (gnus-summary-position-point))
12526
12527 (defun gnus-summary-catchup-all (&optional quietly)
12528   "Mark all articles in this newsgroup as read."
12529   (interactive "P")
12530   (gnus-set-global-variables)
12531   (gnus-summary-catchup t quietly))
12532
12533 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12534   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12535 If prefix argument ALL is non-nil, all articles are marked as read."
12536   (interactive "P")
12537   (gnus-set-global-variables)
12538   (gnus-summary-catchup all quietly nil 'fast)
12539   ;; Select next newsgroup or exit.
12540   (if (eq gnus-auto-select-next 'quietly)
12541       (gnus-summary-next-group nil)
12542     (gnus-summary-exit)))
12543
12544 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12545   "Mark all articles in this newsgroup as read, and then exit."
12546   (interactive "P")
12547   (gnus-set-global-variables)
12548   (gnus-summary-catchup-and-exit t quietly))
12549
12550 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12551 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12552   "Mark all articles in this group as read and select the next group.
12553 If given a prefix, mark all articles, unread as well as ticked, as
12554 read."
12555   (interactive "P")
12556   (gnus-set-global-variables)
12557   (save-excursion
12558     (gnus-summary-catchup all))
12559   (gnus-summary-next-article t nil nil t))
12560
12561 ;; Thread-based commands.
12562
12563 (defun gnus-summary-articles-in-thread (&optional article)
12564   "Return a list of all articles in the current thread.
12565 If ARTICLE is non-nil, return all articles in the thread that starts
12566 with that article."
12567   (let* ((article (or article (gnus-summary-article-number)))
12568          (data (gnus-data-find-list article))
12569          (top-level (gnus-data-level (car data)))
12570          (top-subject
12571           (cond ((null gnus-thread-operation-ignore-subject)
12572                  (gnus-simplify-subject-re
12573                   (mail-header-subject (gnus-data-header (car data)))))
12574                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12575                  (gnus-simplify-subject-fuzzy
12576                   (mail-header-subject (gnus-data-header (car data)))))
12577                 (t nil)))
12578          (end-point (save-excursion
12579                       (if (gnus-summary-go-to-next-thread) 
12580                           (point) (point-max))))
12581          articles)
12582     (while (and data
12583                 (< (gnus-data-pos (car data)) end-point))
12584       (when (or (not top-subject)
12585                 (string= top-subject
12586                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12587                              (gnus-simplify-subject-fuzzy
12588                               (mail-header-subject
12589                                (gnus-data-header (car data))))
12590                            (gnus-simplify-subject-re
12591                             (mail-header-subject
12592                              (gnus-data-header (car data)))))))
12593         (push (gnus-data-number (car data)) articles))
12594       (unless (and (setq data (cdr data))
12595                    (> (gnus-data-level (car data)) top-level))
12596         (setq data nil)))
12597     ;; Return the list of articles.
12598     (nreverse articles)))
12599
12600 (defun gnus-summary-rethread-current ()
12601   "Rethread the thread the current article is part of."
12602   (interactive)
12603   (gnus-set-global-variables)
12604   (let* ((gnus-show-threads t)
12605          (article (gnus-summary-article-number))
12606          (id (mail-header-id (gnus-summary-article-header)))
12607          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12608     (unless id
12609       (error "No article on the current line"))
12610     (gnus-rebuild-thread id)
12611     (gnus-summary-goto-subject article)))
12612
12613 (defun gnus-summary-reparent-thread ()
12614   "Make current article child of the marked (or previous) article.
12615
12616 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12617 is non-nil or the Subject: of both articles are the same."
12618   (interactive)
12619   (or (not (gnus-group-read-only-p))
12620       (error "The current newsgroup does not support article editing."))
12621   (or (<= (length gnus-newsgroup-processable) 1)
12622       (error "No more than one article may be marked."))
12623   (save-window-excursion
12624     (let ((gnus-article-buffer " *reparent*")
12625           (current-article (gnus-summary-article-number))
12626           ; first grab the marked article, otherwise one line up.
12627           (parent-article (if (not (null gnus-newsgroup-processable))
12628                               (car gnus-newsgroup-processable)
12629                             (save-excursion
12630                               (if (eq (forward-line -1) 0)
12631                                   (gnus-summary-article-number)
12632                                 (error "Beginning of summary buffer."))))))
12633       (or (not (eq current-article parent-article))
12634           (error "An article may not be self-referential."))
12635       (let ((message-id (mail-header-id 
12636                          (gnus-summary-article-header parent-article))))
12637         (or (and message-id (not (equal message-id "")))
12638             (error "No message-id in desired parent."))
12639         (gnus-summary-select-article t t nil current-article)
12640         (set-buffer gnus-article-buffer)
12641         (setq buffer-read-only nil)
12642         (let ((buf (format "%s" (buffer-string))))
12643           (erase-buffer)
12644           (insert buf))
12645         (goto-char (point-min))
12646         (if (search-forward-regexp "^References: " nil t)
12647             (insert message-id " " )
12648           (insert "References: " message-id "\n"))
12649         (or (gnus-request-replace-article current-article
12650                                           (car gnus-article-current)
12651                                           gnus-article-buffer)
12652             (error "Couldn't replace article."))
12653         (set-buffer gnus-summary-buffer)
12654         (gnus-summary-unmark-all-processable)
12655         (gnus-summary-rethread-current)
12656         (gnus-message 3 "Article %d is now the child of article %d."
12657                       current-article parent-article)))))
12658
12659 (defun gnus-summary-toggle-threads (&optional arg)
12660   "Toggle showing conversation threads.
12661 If ARG is positive number, turn showing conversation threads on."
12662   (interactive "P")
12663   (gnus-set-global-variables)
12664   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12665     (setq gnus-show-threads
12666           (if (null arg) (not gnus-show-threads)
12667             (> (prefix-numeric-value arg) 0)))
12668     (gnus-summary-prepare)
12669     (gnus-summary-goto-subject current)
12670     (gnus-summary-position-point)))
12671
12672 (defun gnus-summary-show-all-threads ()
12673   "Show all threads."
12674   (interactive)
12675   (gnus-set-global-variables)
12676   (save-excursion
12677     (let ((buffer-read-only nil))
12678       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12679   (gnus-summary-position-point))
12680
12681 (defun gnus-summary-show-thread ()
12682   "Show thread subtrees.
12683 Returns nil if no thread was there to be shown."
12684   (interactive)
12685   (gnus-set-global-variables)
12686   (let ((buffer-read-only nil)
12687         (orig (point))
12688         ;; first goto end then to beg, to have point at beg after let
12689         (end (progn (end-of-line) (point)))
12690         (beg (progn (beginning-of-line) (point))))
12691     (prog1
12692         ;; Any hidden lines here?
12693         (search-forward "\r" end t)
12694       (subst-char-in-region beg end ?\^M ?\n t)
12695       (goto-char orig)
12696       (gnus-summary-position-point))))
12697
12698 (defun gnus-summary-hide-all-threads ()
12699   "Hide all thread subtrees."
12700   (interactive)
12701   (gnus-set-global-variables)
12702   (save-excursion
12703     (goto-char (point-min))
12704     (gnus-summary-hide-thread)
12705     (while (zerop (gnus-summary-next-thread 1 t))
12706       (gnus-summary-hide-thread)))
12707   (gnus-summary-position-point))
12708
12709 (defun gnus-summary-hide-thread ()
12710   "Hide thread subtrees.
12711 Returns nil if no threads were there to be hidden."
12712   (interactive)
12713   (gnus-set-global-variables)
12714   (let ((buffer-read-only nil)
12715         (start (point))
12716         (article (gnus-summary-article-number)))
12717     (goto-char start)
12718     ;; Go forward until either the buffer ends or the subthread
12719     ;; ends.
12720     (when (and (not (eobp))
12721                (or (zerop (gnus-summary-next-thread 1 t))
12722                    (goto-char (point-max))))
12723       (prog1
12724           (if (and (> (point) start)
12725                    (search-backward "\n" start t))
12726               (progn
12727                 (subst-char-in-region start (point) ?\n ?\^M)
12728                 (gnus-summary-goto-subject article))
12729             (goto-char start)
12730             nil)
12731         ;;(gnus-summary-position-point)
12732         ))))
12733
12734 (defun gnus-summary-go-to-next-thread (&optional previous)
12735   "Go to the same level (or less) next thread.
12736 If PREVIOUS is non-nil, go to previous thread instead.
12737 Return the article number moved to, or nil if moving was impossible."
12738   (let ((level (gnus-summary-thread-level))
12739         (way (if previous -1 1))
12740         (beg (point)))
12741     (forward-line way)
12742     (while (and (not (eobp))
12743                 (< level (gnus-summary-thread-level)))
12744       (forward-line way))
12745     (if (eobp)
12746         (progn
12747           (goto-char beg)
12748           nil)
12749       (setq beg (point))
12750       (prog1
12751           (gnus-summary-article-number)
12752         (goto-char beg)))))
12753
12754 (defun gnus-summary-go-to-next-thread-old (&optional previous)
12755   "Go to the same level (or less) next thread.
12756 If PREVIOUS is non-nil, go to previous thread instead.
12757 Return the article number moved to, or nil if moving was impossible."
12758   (if (and (eq gnus-summary-make-false-root 'dummy)
12759            (gnus-summary-article-intangible-p))
12760       (let ((beg (point)))
12761         (while (and (zerop (forward-line 1))
12762                     (not (gnus-summary-article-intangible-p))
12763                     (not (zerop (save-excursion 
12764                                   (gnus-summary-thread-level))))))
12765         (if (eobp)
12766             (progn
12767               (goto-char beg)
12768               nil)
12769           (point)))
12770     (let* ((level (gnus-summary-thread-level))
12771            (article (gnus-summary-article-number))
12772            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12773            oart)
12774       (while data
12775         (if (<= (gnus-data-level (car data)) level)
12776             (setq oart (gnus-data-number (car data))
12777                   data nil)
12778           (setq data (cdr data))))
12779       (and oart
12780            (gnus-summary-goto-subject oart)))))
12781
12782 (defun gnus-summary-next-thread (n &optional silent)
12783   "Go to the same level next N'th thread.
12784 If N is negative, search backward instead.
12785 Returns the difference between N and the number of skips actually
12786 done.
12787
12788 If SILENT, don't output messages."
12789   (interactive "p")
12790   (gnus-set-global-variables)
12791   (let ((backward (< n 0))
12792         (n (abs n))
12793         old dum int)
12794     (while (and (> n 0)
12795                 (gnus-summary-go-to-next-thread backward))
12796       (decf n))
12797     (unless silent 
12798       (gnus-summary-position-point))
12799     (when (and (not silent) (/= 0 n))
12800       (gnus-message 7 "No more threads"))
12801     n))
12802
12803 (defun gnus-summary-prev-thread (n)
12804   "Go to the same level previous N'th thread.
12805 Returns the difference between N and the number of skips actually
12806 done."
12807   (interactive "p")
12808   (gnus-set-global-variables)
12809   (gnus-summary-next-thread (- n)))
12810
12811 (defun gnus-summary-go-down-thread ()
12812   "Go down one level in the current thread."
12813   (let ((children (gnus-summary-article-children)))
12814     (and children
12815          (gnus-summary-goto-subject (car children)))))
12816
12817 (defun gnus-summary-go-up-thread ()
12818   "Go up one level in the current thread."
12819   (let ((parent (gnus-summary-article-parent)))
12820     (and parent
12821          (gnus-summary-goto-subject parent))))
12822
12823 (defun gnus-summary-down-thread (n)
12824   "Go down thread N steps.
12825 If N is negative, go up instead.
12826 Returns the difference between N and how many steps down that were
12827 taken."
12828   (interactive "p")
12829   (gnus-set-global-variables)
12830   (let ((up (< n 0))
12831         (n (abs n)))
12832     (while (and (> n 0)
12833                 (if up (gnus-summary-go-up-thread)
12834                   (gnus-summary-go-down-thread)))
12835       (setq n (1- n)))
12836     (gnus-summary-position-point)
12837     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12838     n))
12839
12840 (defun gnus-summary-up-thread (n)
12841   "Go up thread N steps.
12842 If N is negative, go up instead.
12843 Returns the difference between N and how many steps down that were
12844 taken."
12845   (interactive "p")
12846   (gnus-set-global-variables)
12847   (gnus-summary-down-thread (- n)))
12848
12849 (defun gnus-summary-top-thread ()
12850   "Go to the top of the thread."
12851   (interactive)
12852   (gnus-set-global-variables)
12853   (while (gnus-summary-go-up-thread))
12854   (gnus-summary-article-number))
12855
12856 (defun gnus-summary-kill-thread (&optional unmark)
12857   "Mark articles under current thread as read.
12858 If the prefix argument is positive, remove any kinds of marks.
12859 If the prefix argument is negative, tick articles instead."
12860   (interactive "P")
12861   (gnus-set-global-variables)
12862   (when unmark
12863     (setq unmark (prefix-numeric-value unmark)))
12864   (let ((articles (gnus-summary-articles-in-thread)))
12865     (save-excursion
12866       ;; Expand the thread.
12867       (gnus-summary-show-thread)
12868       ;; Mark all the articles.
12869       (while articles
12870         (gnus-summary-goto-subject (car articles))
12871         (cond ((null unmark)
12872                (gnus-summary-mark-article-as-read gnus-killed-mark))
12873               ((> unmark 0)
12874                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12875               (t
12876                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12877         (setq articles (cdr articles))))
12878     ;; Hide killed subtrees.
12879     (and (null unmark)
12880          gnus-thread-hide-killed
12881          (gnus-summary-hide-thread))
12882     ;; If marked as read, go to next unread subject.
12883     (if (null unmark)
12884         ;; Go to next unread subject.
12885         (gnus-summary-next-subject 1 t)))
12886   (gnus-set-mode-line 'summary))
12887
12888 ;; Summary sorting commands
12889
12890 (defun gnus-summary-sort-by-number (&optional reverse)
12891   "Sort summary buffer by article number.
12892 Argument REVERSE means reverse order."
12893   (interactive "P")
12894   (gnus-summary-sort 'number reverse))
12895
12896 (defun gnus-summary-sort-by-author (&optional reverse)
12897   "Sort summary buffer by author name alphabetically.
12898 If case-fold-search is non-nil, case of letters is ignored.
12899 Argument REVERSE means reverse order."
12900   (interactive "P")
12901   (gnus-summary-sort 'author reverse))
12902
12903 (defun gnus-summary-sort-by-subject (&optional reverse)
12904   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12905 If case-fold-search is non-nil, case of letters is ignored.
12906 Argument REVERSE means reverse order."
12907   (interactive "P")
12908   (gnus-summary-sort 'subject reverse))
12909
12910 (defun gnus-summary-sort-by-date (&optional reverse)
12911   "Sort summary buffer by date.
12912 Argument REVERSE means reverse order."
12913   (interactive "P")
12914   (gnus-summary-sort 'date reverse))
12915
12916 (defun gnus-summary-sort-by-score (&optional reverse)
12917   "Sort summary buffer by score.
12918 Argument REVERSE means reverse order."
12919   (interactive "P")
12920   (gnus-summary-sort 'score reverse))
12921
12922 (defun gnus-summary-sort (predicate reverse)
12923   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12924   (gnus-set-global-variables)
12925   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12926          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12927          (gnus-thread-sort-functions
12928           (list
12929            (if (not reverse)
12930                thread
12931              `(lambda (t1 t2)
12932                 (,thread t2 t1)))))
12933          (gnus-article-sort-functions
12934           (list
12935            (if (not reverse)
12936                article
12937              `(lambda (t1 t2)
12938                 (,article t2 t1)))))
12939          (buffer-read-only)
12940          (gnus-summary-prepare-hook nil))
12941     ;; We do the sorting by regenerating the threads.
12942     (gnus-summary-prepare)
12943     ;; Hide subthreads if needed.
12944     (when (and gnus-show-threads gnus-thread-hide-subtree)
12945       (gnus-summary-hide-all-threads)))
12946   ;; If in async mode, we send some info to the backend.
12947   (when gnus-newsgroup-async
12948     (gnus-request-asynchronous
12949      gnus-newsgroup-name gnus-newsgroup-data)))
12950
12951 (defun gnus-sortable-date (date)
12952   "Make sortable string by string-lessp from DATE.
12953 Timezone package is used."
12954   (condition-case ()
12955       (progn
12956         (setq date (inline (timezone-fix-time 
12957                             date nil 
12958                             (aref (inline (timezone-parse-date date)) 4))))
12959         (inline
12960           (timezone-make-sortable-date
12961            (aref date 0) (aref date 1) (aref date 2)
12962            (inline
12963              (timezone-make-time-string
12964               (aref date 3) (aref date 4) (aref date 5))))))
12965     (error "")))
12966   
12967 ;; Summary saving commands.
12968
12969 (defun gnus-summary-save-article (&optional n not-saved)
12970   "Save the current article using the default saver function.
12971 If N is a positive number, save the N next articles.
12972 If N is a negative number, save the N previous articles.
12973 If N is nil and any articles have been marked with the process mark,
12974 save those articles instead.
12975 The variable `gnus-default-article-saver' specifies the saver function."
12976   (interactive "P")
12977   (gnus-set-global-variables)
12978   (let ((articles (gnus-summary-work-articles n))
12979         (save-buffer (save-excursion 
12980                        (nnheader-set-temp-buffer " *Gnus Save*")))
12981         file header article)
12982     (while articles
12983       (setq header (gnus-summary-article-header
12984                     (setq article (pop articles))))
12985       (if (not (vectorp header))
12986           ;; This is a pseudo-article.
12987           (if (assq 'name header)
12988               (gnus-copy-file (cdr (assq 'name header)))
12989             (gnus-message 1 "Article %d is unsaveable" article))
12990         ;; This is a real article.
12991         (save-window-excursion
12992           (gnus-summary-select-article t nil nil article))
12993         (save-excursion
12994           (set-buffer save-buffer)
12995           (erase-buffer)
12996           (insert-buffer-substring gnus-original-article-buffer))
12997         (unless gnus-save-all-headers
12998           ;; Remove headers accoring to `gnus-saved-headers'.
12999           (let ((gnus-visible-headers
13000                  (or gnus-saved-headers gnus-visible-headers))
13001                 (gnus-article-buffer save-buffer))
13002             (gnus-article-hide-headers 1 t)))
13003         (save-window-excursion
13004           (if (not gnus-default-article-saver)
13005               (error "No default saver is defined.")
13006             ;; !!! Magic!  The saving functions all save
13007             ;; `gnus-original-article-buffer' (or so they think),
13008             ;; but we bind that variable to our save-buffer.
13009             (set-buffer gnus-article-buffer)
13010             (let ((gnus-original-article-buffer save-buffer))
13011               (set-buffer gnus-summary-buffer)
13012               (setq file (funcall
13013                           gnus-default-article-saver
13014                           (cond
13015                            ((not gnus-prompt-before-saving)
13016                             'default)
13017                            ((eq gnus-prompt-before-saving 'always)
13018                             nil)
13019                            (t file)))))))
13020         (gnus-summary-remove-process-mark article)
13021         (unless not-saved
13022           (gnus-summary-set-saved-mark article))))
13023     (gnus-kill-buffer save-buffer)
13024     (gnus-summary-position-point)
13025     n))
13026
13027 (defun gnus-summary-pipe-output (&optional arg)
13028   "Pipe the current article to a subprocess.
13029 If N is a positive number, pipe the N next articles.
13030 If N is a negative number, pipe the N previous articles.
13031 If N is nil and any articles have been marked with the process mark,
13032 pipe those articles instead."
13033   (interactive "P")
13034   (gnus-set-global-variables)
13035   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
13036     (gnus-summary-save-article arg t))
13037   (gnus-configure-windows 'pipe))
13038
13039 (defun gnus-summary-save-article-mail (&optional arg)
13040   "Append the current article to an mail file.
13041 If N is a positive number, save the N next articles.
13042 If N is a negative number, save the N previous articles.
13043 If N is nil and any articles have been marked with the process mark,
13044 save those articles instead."
13045   (interactive "P")
13046   (gnus-set-global-variables)
13047   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
13048     (gnus-summary-save-article arg)))
13049
13050 (defun gnus-summary-save-article-rmail (&optional arg)
13051   "Append the current article to an rmail file.
13052 If N is a positive number, save the N next articles.
13053 If N is a negative number, save the N previous articles.
13054 If N is nil and any articles have been marked with the process mark,
13055 save those articles instead."
13056   (interactive "P")
13057   (gnus-set-global-variables)
13058   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
13059     (gnus-summary-save-article arg)))
13060
13061 (defun gnus-summary-save-article-file (&optional arg)
13062   "Append the current article to a file.
13063 If N is a positive number, save the N next articles.
13064 If N is a negative number, save the N previous articles.
13065 If N is nil and any articles have been marked with the process mark,
13066 save those articles instead."
13067   (interactive "P")
13068   (gnus-set-global-variables)
13069   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
13070     (gnus-summary-save-article arg)))
13071
13072 (defun gnus-summary-save-article-body-file (&optional arg)
13073   "Append the current article body to a file.
13074 If N is a positive number, save the N next articles.
13075 If N is a negative number, save the N previous articles.
13076 If N is nil and any articles have been marked with the process mark,
13077 save those articles instead."
13078   (interactive "P")
13079   (gnus-set-global-variables)
13080   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
13081     (gnus-summary-save-article arg)))
13082
13083 (defun gnus-get-split-value (methods)
13084   "Return a value based on the split METHODS."
13085   (let (split-name method result match)
13086     (when methods
13087       (save-excursion
13088         (set-buffer gnus-original-article-buffer)
13089         (save-restriction
13090           (nnheader-narrow-to-headers)
13091           (while methods
13092             (goto-char (point-min))
13093             (setq method (pop methods))
13094             (setq match (car method))
13095             (when (cond
13096                    ((stringp match)
13097                     ;; Regular expression.
13098                     (condition-case ()
13099                         (re-search-forward match nil t)
13100                       (error nil)))
13101                    ((gnus-functionp match)
13102                     ;; Function.
13103                     (save-restriction
13104                       (widen)
13105                       (setq result (funcall match gnus-newsgroup-name))))
13106                    ((consp match)
13107                     ;; Form.
13108                     (save-restriction
13109                       (widen)
13110                       (setq result (eval match)))))
13111               (setq split-name (append (cdr method) split-name))
13112               (cond ((stringp result)
13113                      (push result split-name))
13114                     ((consp result)
13115                      (setq split-name (append result split-name)))))))))
13116     split-name))
13117
13118 (defun gnus-read-move-group-name (prompt default articles prefix)
13119   "Read a group name."
13120   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
13121          (minibuffer-confirm-incomplete nil) ; XEmacs
13122          group-map
13123          (dum (mapatoms
13124                (lambda (g) 
13125                  (and (boundp g)
13126                       (symbol-name g)
13127                       (memq 'respool
13128                             (assoc (symbol-name
13129                                     (car (gnus-find-method-for-group
13130                                           (symbol-name g))))
13131                                    gnus-valid-select-methods))
13132                       (push (list (symbol-name g)) group-map)))
13133                gnus-active-hashtb))
13134          (prom
13135           (format "%s %s to:"
13136                   prompt
13137                   (if (> (length articles) 1)
13138                       (format "these %d articles" (length articles))
13139                     "this article")))
13140          (to-newsgroup
13141           (cond
13142            ((null split-name)
13143             (gnus-completing-read default prom
13144                                   group-map nil nil prefix
13145                                   'gnus-group-history))
13146            ((= 1 (length split-name))
13147             (gnus-completing-read (car split-name) prom group-map
13148                                   nil nil nil
13149                                   'gnus-group-history))
13150            (t
13151             (gnus-completing-read nil prom 
13152                                   (mapcar (lambda (el) (list el))
13153                                           (nreverse split-name))
13154                                   nil nil nil
13155                                   'gnus-group-history)))))
13156     (when to-newsgroup
13157       (if (or (string= to-newsgroup "")
13158               (string= to-newsgroup prefix))
13159           (setq to-newsgroup (or default "")))
13160       (or (gnus-active to-newsgroup)
13161           (gnus-activate-group to-newsgroup)
13162           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
13163                                      to-newsgroup))
13164               (or (and (gnus-request-create-group 
13165                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
13166                        (gnus-activate-group to-newsgroup nil nil
13167                                             (gnus-group-name-to-method
13168                                              to-newsgroup)))
13169                   (error "Couldn't create group %s" to-newsgroup)))
13170           (error "No such group: %s" to-newsgroup)))
13171     to-newsgroup))
13172
13173 (defun gnus-read-save-file-name (prompt default-name)
13174   (let* ((split-name (gnus-get-split-value gnus-split-methods))
13175          (file
13176           ;; Let the split methods have their say.
13177           (cond
13178            ;; No split name was found.
13179            ((null split-name)
13180             (read-file-name
13181              (concat prompt " (default "
13182                      (file-name-nondirectory default-name) ") ")
13183              (file-name-directory default-name)
13184              default-name))
13185            ;; A single split name was found
13186            ((= 1 (length split-name))
13187             (let* ((name (car split-name))
13188                    (dir (cond ((file-directory-p name)
13189                                (file-name-as-directory name))
13190                               ((file-exists-p name) name)
13191                               (t gnus-article-save-directory))))
13192               (read-file-name
13193                (concat prompt " (default " name ") ")
13194                dir name)))
13195            ;; A list of splits was found.
13196            (t
13197             (setq split-name (nreverse split-name))
13198             (let (result)
13199               (let ((file-name-history (nconc split-name file-name-history)))
13200                 (setq result
13201                       (read-file-name
13202                        (concat prompt " (`M-p' for defaults) ")
13203                        gnus-article-save-directory
13204                        (car split-name))))
13205               (car (push result file-name-history)))))))
13206     ;; If we have read a directory, we append the default file name.
13207     (when (file-directory-p file)
13208       (setq file (concat (file-name-as-directory file)
13209                          (file-name-nondirectory default-name))))
13210     ;; Possibly translate some charaters.
13211     (nnheader-translate-file-chars file)))
13212
13213 (defun gnus-article-archive-name (group)
13214   "Return the first instance of an \"Archive-name\" in the current buffer."
13215   (let ((case-fold-search t))
13216     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
13217       (match-string 1))))
13218
13219 (defun gnus-summary-save-in-rmail (&optional filename)
13220   "Append this article to Rmail file.
13221 Optional argument FILENAME specifies file name.
13222 Directory to save to is default to `gnus-article-save-directory'."
13223   (interactive)
13224   (gnus-set-global-variables)
13225   (let ((default-name
13226           (funcall gnus-rmail-save-name gnus-newsgroup-name
13227                    gnus-current-headers gnus-newsgroup-last-rmail)))
13228     (setq filename
13229           (cond ((eq filename 'default)
13230                  default-name)
13231                 (filename filename)
13232                 (t (gnus-read-save-file-name
13233                     "Save in rmail file:" default-name))))
13234     (gnus-make-directory (file-name-directory filename))
13235     (gnus-eval-in-buffer-window gnus-original-article-buffer
13236       (save-excursion
13237         (save-restriction
13238           (widen)
13239           (gnus-output-to-rmail filename))))
13240     ;; Remember the directory name to save articles
13241     (setq gnus-newsgroup-last-rmail filename)))
13242
13243 (defun gnus-summary-save-in-mail (&optional filename)
13244   "Append this article to Unix mail file.
13245 Optional argument FILENAME specifies file name.
13246 Directory to save to is default to `gnus-article-save-directory'."
13247   (interactive)
13248   (gnus-set-global-variables)
13249   (let ((default-name
13250           (funcall gnus-mail-save-name gnus-newsgroup-name
13251                    gnus-current-headers gnus-newsgroup-last-mail)))
13252     (setq filename
13253           (cond ((eq filename 'default)
13254                  default-name)
13255                 (filename filename)
13256                 (t (gnus-read-save-file-name
13257                     "Save in Unix mail file:" default-name))))
13258     (setq filename
13259           (expand-file-name filename
13260                             (and default-name
13261                                  (file-name-directory default-name))))
13262     (gnus-make-directory (file-name-directory filename))
13263     (gnus-eval-in-buffer-window gnus-original-article-buffer
13264       (save-excursion
13265         (save-restriction
13266           (widen)
13267           (if (and (file-readable-p filename) (mail-file-babyl-p filename))
13268               (gnus-output-to-rmail filename)
13269             (let ((mail-use-rfc822 t))
13270               (rmail-output filename 1 t t))))))
13271     ;; Remember the directory name to save articles.
13272     (setq gnus-newsgroup-last-mail filename)))
13273
13274 (defun gnus-summary-save-in-file (&optional filename)
13275   "Append this article to file.
13276 Optional argument FILENAME specifies file name.
13277 Directory to save to is default to `gnus-article-save-directory'."
13278   (interactive)
13279   (gnus-set-global-variables)
13280   (let ((default-name
13281           (funcall gnus-file-save-name gnus-newsgroup-name
13282                    gnus-current-headers gnus-newsgroup-last-file)))
13283     (setq filename
13284           (cond ((eq filename 'default)
13285                  default-name)
13286                 (filename filename)
13287                 (t (gnus-read-save-file-name
13288                     "Save in file:" default-name))))
13289     (gnus-make-directory (file-name-directory filename))
13290     (gnus-eval-in-buffer-window gnus-original-article-buffer
13291       (save-excursion
13292         (save-restriction
13293           (widen)
13294           (gnus-output-to-file filename))))
13295     ;; Remember the directory name to save articles.
13296     (setq gnus-newsgroup-last-file filename)))
13297
13298 (defun gnus-summary-save-body-in-file (&optional filename)
13299   "Append this article body to a file.
13300 Optional argument FILENAME specifies file name.
13301 The directory to save in defaults to `gnus-article-save-directory'."
13302   (interactive)
13303   (gnus-set-global-variables)
13304   (let ((default-name
13305           (funcall gnus-file-save-name gnus-newsgroup-name
13306                    gnus-current-headers gnus-newsgroup-last-file)))
13307     (setq filename
13308           (cond ((eq filename 'default)
13309                  default-name)
13310                 (filename filename)
13311                 (t (gnus-read-save-file-name
13312                     "Save body in file:" default-name))))
13313     (gnus-make-directory (file-name-directory filename))
13314     (gnus-eval-in-buffer-window gnus-article-buffer
13315       (save-excursion
13316         (save-restriction
13317           (widen)
13318           (goto-char (point-min))
13319           (and (search-forward "\n\n" nil t)
13320                (narrow-to-region (point) (point-max)))
13321           (gnus-output-to-file filename))))
13322     ;; Remember the directory name to save articles.
13323     (setq gnus-newsgroup-last-file filename)))
13324
13325 (defun gnus-summary-save-in-pipe (&optional command)
13326   "Pipe this article to subprocess."
13327   (interactive)
13328   (gnus-set-global-variables)
13329   (setq command
13330         (cond ((eq command 'default)
13331                gnus-last-shell-command)
13332               (command command)
13333               (t (read-string "Shell command on article: "
13334                               gnus-last-shell-command))))
13335   (if (string-equal command "")
13336       (setq command gnus-last-shell-command))
13337   (gnus-eval-in-buffer-window gnus-article-buffer
13338     (save-restriction
13339       (widen)
13340       (shell-command-on-region (point-min) (point-max) command nil)))
13341   (setq gnus-last-shell-command command))
13342
13343 ;; Summary extract commands
13344
13345 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
13346   (let ((buffer-read-only nil)
13347         (article (gnus-summary-article-number))
13348         after-article b e)
13349     (or (gnus-summary-goto-subject article)
13350         (error (format "No such article: %d" article)))
13351     (gnus-summary-position-point)
13352     ;; If all commands are to be bunched up on one line, we collect
13353     ;; them here.
13354     (if gnus-view-pseudos-separately
13355         ()
13356       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
13357             files action)
13358         (while ps
13359           (setq action (cdr (assq 'action (car ps))))
13360           (setq files (list (cdr (assq 'name (car ps)))))
13361           (while (and ps (cdr ps)
13362                       (string= (or action "1")
13363                                (or (cdr (assq 'action (cadr ps))) "2")))
13364             (setq files (cons (cdr (assq 'name (cadr ps))) files))
13365             (setcdr ps (cddr ps)))
13366           (if (not files)
13367               ()
13368             (if (not (string-match "%s" action))
13369                 (setq files (cons " " files)))
13370             (setq files (cons " " files))
13371             (and (assq 'execute (car ps))
13372                  (setcdr (assq 'execute (car ps))
13373                          (funcall (if (string-match "%s" action)
13374                                       'format 'concat)
13375                                   action
13376                                   (mapconcat (lambda (f) f) files " ")))))
13377           (setq ps (cdr ps)))))
13378     (if (and gnus-view-pseudos (not not-view))
13379         (while pslist
13380           (and (assq 'execute (car pslist))
13381                (gnus-execute-command (cdr (assq 'execute (car pslist)))
13382                                      (eq gnus-view-pseudos 'not-confirm)))
13383           (setq pslist (cdr pslist)))
13384       (save-excursion
13385         (while pslist
13386           (setq after-article (or (cdr (assq 'article (car pslist)))
13387                                   (gnus-summary-article-number)))
13388           (gnus-summary-goto-subject after-article)
13389           (forward-line 1)
13390           (setq b (point))
13391           (insert "    " (file-name-nondirectory
13392                                 (cdr (assq 'name (car pslist))))
13393                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
13394           (setq e (point))
13395           (forward-line -1)             ; back to `b'
13396           (gnus-add-text-properties
13397            b (1- e) (list 'gnus-number gnus-reffed-article-number
13398                           gnus-mouse-face-prop gnus-mouse-face))
13399           (gnus-data-enter
13400            after-article gnus-reffed-article-number
13401            gnus-unread-mark b (car pslist) 0 (- e b))
13402           (push gnus-reffed-article-number gnus-newsgroup-unreads)
13403           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
13404           (setq pslist (cdr pslist)))))))
13405
13406 (defun gnus-pseudos< (p1 p2)
13407   (let ((c1 (cdr (assq 'action p1)))
13408         (c2 (cdr (assq 'action p2))))
13409     (and c1 c2 (string< c1 c2))))
13410
13411 (defun gnus-request-pseudo-article (props)
13412   (cond ((assq 'execute props)
13413          (gnus-execute-command (cdr (assq 'execute props)))))
13414   (let ((gnus-current-article (gnus-summary-article-number)))
13415     (run-hooks 'gnus-mark-article-hook)))
13416
13417 (defun gnus-execute-command (command &optional automatic)
13418   (save-excursion
13419     (gnus-article-setup-buffer)
13420     (set-buffer gnus-article-buffer)
13421     (setq buffer-read-only nil)
13422     (let ((command (if automatic command (read-string "Command: " command)))
13423           ;; Just binding this here doesn't help, because there might
13424           ;; be output from the process after exiting the scope of 
13425           ;; this `let'.
13426           ;; (buffer-read-only nil)
13427           )
13428       (erase-buffer)
13429       (insert "$ " command "\n\n")
13430       (if gnus-view-pseudo-asynchronously
13431           (start-process "gnus-execute" nil shell-file-name
13432                          shell-command-switch command)
13433         (call-process shell-file-name nil t nil
13434                       shell-command-switch command)))))
13435
13436 (defun gnus-copy-file (file &optional to)
13437   "Copy FILE to TO."
13438   (interactive
13439    (list (read-file-name "Copy file: " default-directory)
13440          (read-file-name "Copy file to: " default-directory)))
13441   (gnus-set-global-variables)
13442   (or to (setq to (read-file-name "Copy file to: " default-directory)))
13443   (and (file-directory-p to)
13444        (setq to (concat (file-name-as-directory to)
13445                         (file-name-nondirectory file))))
13446   (copy-file file to))
13447
13448 ;; Summary kill commands.
13449
13450 (defun gnus-summary-edit-global-kill (article)
13451   "Edit the \"global\" kill file."
13452   (interactive (list (gnus-summary-article-number)))
13453   (gnus-set-global-variables)
13454   (gnus-group-edit-global-kill article))
13455
13456 (defun gnus-summary-edit-local-kill ()
13457   "Edit a local kill file applied to the current newsgroup."
13458   (interactive)
13459   (gnus-set-global-variables)
13460   (setq gnus-current-headers (gnus-summary-article-header))
13461   (gnus-set-global-variables)
13462   (gnus-group-edit-local-kill
13463    (gnus-summary-article-number) gnus-newsgroup-name))
13464
13465 \f
13466 ;;;
13467 ;;; Gnus article mode
13468 ;;;
13469
13470 (put 'gnus-article-mode 'mode-class 'special)
13471
13472 (if gnus-article-mode-map
13473     nil
13474   (setq gnus-article-mode-map (make-keymap))
13475   (suppress-keymap gnus-article-mode-map)
13476
13477   (gnus-define-keys gnus-article-mode-map
13478     " " gnus-article-goto-next-page
13479     "\177" gnus-article-goto-prev-page
13480     [delete] gnus-article-goto-prev-page
13481     "\C-c^" gnus-article-refer-article
13482     "h" gnus-article-show-summary
13483     "s" gnus-article-show-summary
13484     "\C-c\C-m" gnus-article-mail
13485     "?" gnus-article-describe-briefly
13486     gnus-mouse-2 gnus-article-push-button
13487     "\r" gnus-article-press-button
13488     "\t" gnus-article-next-button
13489     "\M-\t" gnus-article-prev-button
13490     "<" beginning-of-buffer
13491     ">" end-of-buffer
13492     "\C-c\C-b" gnus-bug)
13493
13494   (substitute-key-definition
13495    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13496
13497 (defun gnus-article-mode ()
13498   "Major mode for displaying an article.
13499
13500 All normal editing commands are switched off.
13501
13502 The following commands are available:
13503
13504 \\<gnus-article-mode-map>
13505 \\[gnus-article-next-page]\t Scroll the article one page forwards
13506 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13507 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13508 \\[gnus-article-show-summary]\t Display the summary buffer
13509 \\[gnus-article-mail]\t Send a reply to the address near point
13510 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13511 \\[gnus-info-find-node]\t Go to the Gnus info node"
13512   (interactive)
13513   (when (and menu-bar-mode
13514              (gnus-visual-p 'article-menu 'menu))
13515     (gnus-article-make-menu-bar))
13516   (kill-all-local-variables)
13517   (gnus-simplify-mode-line)
13518   (setq mode-name "Article")
13519   (setq major-mode 'gnus-article-mode)
13520   (make-local-variable 'minor-mode-alist)
13521   (or (assq 'gnus-show-mime minor-mode-alist)
13522       (setq minor-mode-alist
13523             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13524   (use-local-map gnus-article-mode-map)
13525   (make-local-variable 'page-delimiter)
13526   (setq page-delimiter gnus-page-delimiter)
13527   (buffer-disable-undo (current-buffer))
13528   (setq buffer-read-only t)             ;Disable modification
13529   (run-hooks 'gnus-article-mode-hook))
13530
13531 (defun gnus-article-setup-buffer ()
13532   "Initialize the article buffer."
13533   (let* ((name (if gnus-single-article-buffer "*Article*"
13534                  (concat "*Article " gnus-newsgroup-name "*")))
13535          (original
13536           (progn (string-match "\\*Article" name)
13537                  (concat " *Original Article"
13538                          (substring name (match-end 0))))))
13539     (setq gnus-article-buffer name)
13540     (setq gnus-original-article-buffer original)
13541     ;; This might be a variable local to the summary buffer.
13542     (unless gnus-single-article-buffer
13543       (save-excursion
13544         (set-buffer gnus-summary-buffer)
13545         (setq gnus-article-buffer name)
13546         (setq gnus-original-article-buffer original)
13547         (gnus-set-global-variables))
13548       (make-local-variable 'gnus-summary-buffer))
13549     ;; Init original article buffer.
13550     (save-excursion
13551       (set-buffer (get-buffer-create gnus-original-article-buffer))
13552       (buffer-disable-undo (current-buffer))
13553       (setq major-mode 'gnus-original-article-mode)
13554       (make-local-variable 'gnus-original-article))
13555     (if (get-buffer name)
13556         (save-excursion
13557           (set-buffer name)
13558           (buffer-disable-undo (current-buffer))
13559           (setq buffer-read-only t)
13560           (gnus-add-current-to-buffer-list)
13561           (or (eq major-mode 'gnus-article-mode)
13562               (gnus-article-mode))
13563           (current-buffer))
13564       (save-excursion
13565         (set-buffer (get-buffer-create name))
13566         (gnus-add-current-to-buffer-list)
13567         (gnus-article-mode)
13568         (current-buffer)))))
13569
13570 ;; Set article window start at LINE, where LINE is the number of lines
13571 ;; from the head of the article.
13572 (defun gnus-article-set-window-start (&optional line)
13573   (set-window-start
13574    (get-buffer-window gnus-article-buffer t)
13575    (save-excursion
13576      (set-buffer gnus-article-buffer)
13577      (goto-char (point-min))
13578      (if (not line)
13579          (point-min)
13580        (gnus-message 6 "Moved to bookmark")
13581        (search-forward "\n\n" nil t)
13582        (forward-line line)
13583        (point)))))
13584
13585 (defun gnus-kill-all-overlays ()
13586   "Delete all overlays in the current buffer."
13587   (when (fboundp 'overlay-lists)
13588     (let* ((overlayss (overlay-lists))
13589            (buffer-read-only nil)
13590            (overlays (nconc (car overlayss) (cdr overlayss))))
13591       (while overlays
13592         (delete-overlay (pop overlays))))))
13593
13594 (defun gnus-request-article-this-buffer (article group)
13595   "Get an article and insert it into this buffer."
13596   (let (do-update-line)
13597     (prog1
13598         (save-excursion
13599           (erase-buffer)
13600           (gnus-kill-all-overlays)
13601           (setq group (or group gnus-newsgroup-name))
13602
13603           ;; Open server if it has closed.
13604           (gnus-check-server (gnus-find-method-for-group group))
13605
13606           ;; Using `gnus-request-article' directly will insert the article into
13607           ;; `nntp-server-buffer' - so we'll save some time by not having to
13608           ;; copy it from the server buffer into the article buffer.
13609
13610           ;; We only request an article by message-id when we do not have the
13611           ;; headers for it, so we'll have to get those.
13612           (when (stringp article)
13613             (let ((gnus-override-method gnus-refer-article-method))
13614               (gnus-read-header article)))
13615
13616           ;; If the article number is negative, that means that this article
13617           ;; doesn't belong in this newsgroup (possibly), so we find its
13618           ;; message-id and request it by id instead of number.
13619           (when (and (numberp article)
13620                      gnus-summary-buffer
13621                      (get-buffer gnus-summary-buffer)
13622                      (buffer-name (get-buffer gnus-summary-buffer)))
13623             (save-excursion
13624               (set-buffer gnus-summary-buffer)
13625               (let ((header (gnus-summary-article-header article)))
13626                 (if (< article 0)
13627                     (cond 
13628                      ((memq article gnus-newsgroup-sparse)
13629                       ;; This is a sparse gap article.
13630                       (setq do-update-line article)
13631                       (setq article (mail-header-id header))
13632                       (let ((gnus-override-method gnus-refer-article-method))
13633                         (gnus-read-header article))
13634                       (setq gnus-newsgroup-sparse
13635                             (delq article gnus-newsgroup-sparse)))
13636                      ((vectorp header)
13637                       ;; It's a real article.
13638                       (setq article (mail-header-id header)))
13639                      (t
13640                       ;; It is an extracted pseudo-article.
13641                       (setq article 'pseudo)
13642                       (gnus-request-pseudo-article header))))
13643                 
13644                 (let ((method (gnus-find-method-for-group 
13645                                gnus-newsgroup-name)))
13646                   (if (not (eq (car method) 'nneething))
13647                       ()
13648                     (let ((dir (concat (file-name-as-directory (nth 1 method))
13649                                        (mail-header-subject header))))
13650                       (if (file-directory-p dir)
13651                           (progn
13652                             (setq article 'nneething)
13653                             (gnus-group-enter-directory dir)))))))))
13654
13655           (cond
13656            ;; Refuse to select canceled articles.
13657            ((and (numberp article)
13658                  gnus-summary-buffer
13659                  (get-buffer gnus-summary-buffer)
13660                  (buffer-name (get-buffer gnus-summary-buffer))
13661                  (eq (cdr (save-excursion
13662                             (set-buffer gnus-summary-buffer)
13663                             (assq article gnus-newsgroup-reads)))
13664                      gnus-canceled-mark))
13665             nil)
13666            ;; We first check `gnus-original-article-buffer'.
13667            ((and (get-buffer gnus-original-article-buffer)
13668                  (numberp article)
13669                  (save-excursion
13670                    (set-buffer gnus-original-article-buffer)
13671                    (and (equal (car gnus-original-article) group)
13672                         (eq (cdr gnus-original-article) article))))
13673             (insert-buffer-substring gnus-original-article-buffer)
13674             'article)
13675            ;; Check the backlog.
13676            ((and gnus-keep-backlog
13677                  (gnus-backlog-request-article group article (current-buffer)))
13678             'article)
13679            ;; Check the cache.
13680            ((and gnus-use-cache
13681                  (numberp article)
13682                  (gnus-cache-request-article article group))
13683             'article)
13684            ;; Get the article and put into the article buffer.
13685            ((or (stringp article) (numberp article))
13686             (let ((gnus-override-method
13687                    (and (stringp article) gnus-refer-article-method))
13688                   (buffer-read-only nil))
13689               (erase-buffer)
13690               (gnus-kill-all-overlays)
13691               (if (gnus-request-article article group (current-buffer))
13692                   (progn
13693                     (and gnus-keep-backlog
13694                          (numberp article)
13695                          (gnus-backlog-enter-article
13696                           group article (current-buffer)))
13697                     'article))))
13698            ;; It was a pseudo.
13699            (t article)))
13700
13701       ;; Take the article from the original article buffer
13702       ;; and place it in the buffer it's supposed to be in.
13703       (when (and (get-buffer gnus-article-buffer)
13704                  ;;(numberp article)
13705                  (equal (buffer-name (current-buffer))
13706                         (buffer-name (get-buffer gnus-article-buffer))))
13707         (save-excursion
13708           (if (get-buffer gnus-original-article-buffer)
13709               (set-buffer (get-buffer gnus-original-article-buffer))
13710             (set-buffer (get-buffer-create gnus-original-article-buffer))
13711             (buffer-disable-undo (current-buffer))
13712             (setq major-mode 'gnus-original-article-mode)
13713             (setq buffer-read-only t)
13714             (gnus-add-current-to-buffer-list))
13715           (let (buffer-read-only)
13716             (erase-buffer)
13717             (insert-buffer-substring gnus-article-buffer))
13718           (setq gnus-original-article (cons group article))))
13719     
13720       ;; Update sparse articles.
13721       (when (and do-update-line
13722                  (or (numberp article)
13723                      (stringp article)))
13724         (let ((buf (current-buffer)))
13725           (set-buffer gnus-summary-buffer)
13726           (gnus-summary-update-article do-update-line)
13727           (gnus-summary-goto-subject do-update-line nil t)
13728           (set-window-point (get-buffer-window (current-buffer) t)
13729                             (point))
13730           (set-buffer buf))))))
13731
13732 (defun gnus-read-header (id &optional header)
13733   "Read the headers of article ID and enter them into the Gnus system."
13734   (let ((group gnus-newsgroup-name)
13735         (gnus-override-method 
13736          (and (gnus-news-group-p gnus-newsgroup-name)
13737               gnus-refer-article-method))       
13738         where)
13739     ;; First we check to see whether the header in question is already
13740     ;; fetched.
13741     (if (stringp id)
13742         ;; This is a Message-ID.
13743         (setq header (or header (gnus-id-to-header id)))
13744       ;; This is an article number.
13745       (setq header (or header (gnus-summary-article-header id))))
13746     (if (and header
13747              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
13748         ;; We have found the header.
13749         header
13750       ;; We have to really fetch the header to this article.
13751       (when (setq where (gnus-request-head id group))
13752         (save-excursion
13753           (set-buffer nntp-server-buffer)
13754           (goto-char (point-max))
13755           (insert ".\n")
13756           (goto-char (point-min))
13757           (insert "211 ")
13758           (princ (cond
13759                   ((numberp id) id)
13760                   ((cdr where) (cdr where))
13761                   (header (mail-header-number header))
13762                   (t gnus-reffed-article-number))
13763                  (current-buffer))
13764           (insert " Article retrieved.\n"))
13765         ;(when (and header
13766         ;          (memq (mail-header-number header) gnus-newsgroup-sparse))
13767         ;  (setcar (gnus-id-to-thread id) nil))
13768         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13769             ()                          ; Malformed head.
13770           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
13771             (if (and (stringp id)
13772                      (not (string= (gnus-group-real-name group)
13773                                    (car where))))
13774                 ;; If we fetched by Message-ID and the article came
13775                 ;; from a different group, we fudge some bogus article
13776                 ;; numbers for this article.
13777                 (mail-header-set-number header gnus-reffed-article-number))
13778             (decf gnus-reffed-article-number)
13779             (gnus-remove-header (mail-header-number header))
13780             (push header gnus-newsgroup-headers)
13781             (setq gnus-current-headers header)
13782             (push (mail-header-number header) gnus-newsgroup-limit))
13783           header)))))
13784
13785 (defun gnus-remove-header (number)
13786   "Remove header NUMBER from `gnus-newsgroup-headers'."
13787   (if (and gnus-newsgroup-headers
13788            (= number (mail-header-number (car gnus-newsgroup-headers))))
13789       (pop gnus-newsgroup-headers)
13790     (let ((headers gnus-newsgroup-headers))
13791       (while (and (cdr headers)
13792                   (not (= number (mail-header-number (cadr headers)))))
13793         (pop headers))
13794       (when (cdr headers)
13795         (setcdr headers (cddr headers))))))
13796
13797 (defun gnus-article-prepare (article &optional all-headers header)
13798   "Prepare ARTICLE in article mode buffer.
13799 ARTICLE should either be an article number or a Message-ID.
13800 If ARTICLE is an id, HEADER should be the article headers.
13801 If ALL-HEADERS is non-nil, no headers are hidden."
13802   (save-excursion
13803     ;; Make sure we start in a summary buffer.
13804     (unless (eq major-mode 'gnus-summary-mode)
13805       (set-buffer gnus-summary-buffer))
13806     (setq gnus-summary-buffer (current-buffer))
13807     ;; Make sure the connection to the server is alive.
13808     (unless (gnus-server-opened
13809              (gnus-find-method-for-group gnus-newsgroup-name))
13810       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13811       (gnus-request-group gnus-newsgroup-name t))
13812     (let* ((article (if header (mail-header-number header) article))
13813            (summary-buffer (current-buffer))
13814            (internal-hook gnus-article-internal-prepare-hook)
13815            (group gnus-newsgroup-name)
13816            result)
13817       (save-excursion
13818         (gnus-article-setup-buffer)
13819         (set-buffer gnus-article-buffer)
13820         ;; Deactivate active regions.
13821         (when (and (boundp 'transient-mark-mode)
13822                    transient-mark-mode)
13823           (setq mark-active nil))
13824         (if (not (setq result (let ((buffer-read-only nil))
13825                                 (gnus-request-article-this-buffer
13826                                  article group))))
13827             ;; There is no such article.
13828             (save-excursion
13829               (when (and (numberp article)
13830                          (not (memq article gnus-newsgroup-sparse)))
13831                 (setq gnus-article-current
13832                       (cons gnus-newsgroup-name article))
13833                 (set-buffer gnus-summary-buffer)
13834                 (setq gnus-current-article article)
13835                 (gnus-summary-mark-article article gnus-canceled-mark))
13836               (unless (memq article gnus-newsgroup-sparse)
13837                 (gnus-error
13838                  1 "No such article (may have expired or been canceled)")))
13839           (if (or (eq result 'pseudo) (eq result 'nneething))
13840               (progn
13841                 (save-excursion
13842                   (set-buffer summary-buffer)
13843                   (setq gnus-last-article gnus-current-article
13844                         gnus-newsgroup-history (cons gnus-current-article
13845                                                      gnus-newsgroup-history)
13846                         gnus-current-article 0
13847                         gnus-current-headers nil
13848                         gnus-article-current nil)
13849                   (if (eq result 'nneething)
13850                       (gnus-configure-windows 'summary)
13851                     (gnus-configure-windows 'article))
13852                   (gnus-set-global-variables))
13853                 (gnus-set-mode-line 'article))
13854             ;; The result from the `request' was an actual article -
13855             ;; or at least some text that is now displayed in the
13856             ;; article buffer.
13857             (if (and (numberp article)
13858                      (not (eq article gnus-current-article)))
13859                 ;; Seems like a new article has been selected.
13860                 ;; `gnus-current-article' must be an article number.
13861                 (save-excursion
13862                   (set-buffer summary-buffer)
13863                   (setq gnus-last-article gnus-current-article
13864                         gnus-newsgroup-history (cons gnus-current-article
13865                                                      gnus-newsgroup-history)
13866                         gnus-current-article article
13867                         gnus-current-headers
13868                         (gnus-summary-article-header gnus-current-article)
13869                         gnus-article-current
13870                         (cons gnus-newsgroup-name gnus-current-article))
13871                   (unless (vectorp gnus-current-headers)
13872                     (setq gnus-current-headers nil))
13873                   (gnus-summary-show-thread)
13874                   (run-hooks 'gnus-mark-article-hook)
13875                   (gnus-set-mode-line 'summary)
13876                   (and (gnus-visual-p 'article-highlight 'highlight)
13877                        (run-hooks 'gnus-visual-mark-article-hook))
13878                   ;; Set the global newsgroup variables here.
13879                   ;; Suggested by Jim Sisolak
13880                   ;; <sisolak@trans4.neep.wisc.edu>.
13881                   (gnus-set-global-variables)
13882                   (setq gnus-have-all-headers
13883                         (or all-headers gnus-show-all-headers))
13884                   (and gnus-use-cache
13885                        (vectorp (gnus-summary-article-header article))
13886                        (gnus-cache-possibly-enter-article
13887                         group article
13888                         (gnus-summary-article-header article)
13889                         (memq article gnus-newsgroup-marked)
13890                         (memq article gnus-newsgroup-dormant)
13891                         (memq article gnus-newsgroup-unreads)))))
13892             (when (or (numberp article)
13893                       (stringp article))
13894               ;; Hooks for getting information from the article.
13895               ;; This hook must be called before being narrowed.
13896               (let (buffer-read-only)
13897                 (run-hooks 'internal-hook)
13898                 (run-hooks 'gnus-article-prepare-hook)
13899                 ;; Decode MIME message.
13900                 (if gnus-show-mime
13901                     (if (or (not gnus-strict-mime)
13902                             (gnus-fetch-field "Mime-Version"))
13903                         (funcall gnus-show-mime-method)
13904                       (funcall gnus-decode-encoded-word-method)))
13905                 ;; Perform the article display hooks.
13906                 (run-hooks 'gnus-article-display-hook))
13907               ;; Do page break.
13908               (goto-char (point-min))
13909               (and gnus-break-pages (gnus-narrow-to-page)))
13910             (gnus-set-mode-line 'article)
13911             (gnus-configure-windows 'article)
13912             (goto-char (point-min))
13913             t))))))
13914
13915 (defun gnus-article-show-all-headers ()
13916   "Show all article headers in article mode buffer."
13917   (save-excursion
13918     (gnus-article-setup-buffer)
13919     (set-buffer gnus-article-buffer)
13920     (let ((buffer-read-only nil))
13921       (gnus-unhide-text (point-min) (point-max)))))
13922
13923 (defun gnus-article-hide-headers-if-wanted ()
13924   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13925 Provided for backwards compatibility."
13926   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13927       gnus-inhibit-hiding
13928       (gnus-article-hide-headers)))
13929
13930 (defsubst gnus-article-header-rank ()
13931   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13932   (let ((list gnus-sorted-header-list)
13933         (i 0))
13934     (while list
13935       (when (looking-at (car list))
13936         (setq list nil))
13937       (setq list (cdr list))
13938       (incf i))
13939     i))
13940
13941 (defun gnus-article-hide-headers (&optional arg delete)
13942   "Toggle whether to hide unwanted headers and possibly sort them as well.
13943 If given a negative prefix, always show; if given a positive prefix,
13944 always hide."
13945   (interactive (gnus-hidden-arg))
13946   (if (gnus-article-check-hidden-text 'headers arg)
13947       ;; Show boring headers as well.
13948       (gnus-article-show-hidden-text 'boring-headers)
13949     ;; This function might be inhibited.
13950     (unless gnus-inhibit-hiding
13951       (save-excursion
13952         (set-buffer gnus-article-buffer)
13953         (save-restriction
13954           (let ((buffer-read-only nil)
13955                 (props (nconc (list 'gnus-type 'headers)
13956                               gnus-hidden-properties))
13957                 (max (1+ (length gnus-sorted-header-list)))
13958                 (ignored (when (not (stringp gnus-visible-headers))
13959                            (cond ((stringp gnus-ignored-headers)
13960                                   gnus-ignored-headers)
13961                                  ((listp gnus-ignored-headers)
13962                                   (mapconcat 'identity gnus-ignored-headers
13963                                              "\\|")))))
13964                 (visible
13965                  (cond ((stringp gnus-visible-headers)
13966                         gnus-visible-headers)
13967                        ((and gnus-visible-headers
13968                              (listp gnus-visible-headers))
13969                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13970                 (inhibit-point-motion-hooks t)
13971                 want-list beg)
13972             ;; First we narrow to just the headers.
13973             (widen)
13974             (goto-char (point-min))
13975             ;; Hide any "From " lines at the beginning of (mail) articles.
13976             (while (looking-at "From ")
13977               (forward-line 1))
13978             (unless (bobp)
13979               (if delete
13980                   (delete-region (point-min) (point))
13981                 (gnus-hide-text (point-min) (point) props)))
13982             ;; Then treat the rest of the header lines.
13983             (narrow-to-region
13984              (point)
13985              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13986             ;; Then we use the two regular expressions
13987             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13988             ;; select which header lines is to remain visible in the
13989             ;; article buffer.
13990             (goto-char (point-min))
13991             (while (re-search-forward "^[^ \t]*:" nil t)
13992               (beginning-of-line)
13993               ;; We add the headers we want to keep to a list and delete
13994               ;; them from the buffer.
13995               (gnus-put-text-property 
13996                (point) (1+ (point)) 'message-rank
13997                (if (or (and visible (looking-at visible))
13998                        (and ignored
13999                             (not (looking-at ignored))))
14000                    (gnus-article-header-rank) 
14001                  (+ 2 max)))
14002               (forward-line 1))
14003             (message-sort-headers-1)
14004             (when (setq beg (text-property-any 
14005                              (point-min) (point-max) 'message-rank (+ 2 max)))
14006               ;; We make the unwanted headers invisible.
14007               (if delete
14008                   (delete-region beg (point-max))
14009                 ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
14010                 (gnus-hide-text-type beg (point-max) 'headers))
14011               ;; Work around XEmacs lossage.
14012               (gnus-put-text-property (point-min) beg 'invisible nil))))))))
14013
14014 (defun gnus-article-hide-boring-headers (&optional arg)
14015   "Toggle hiding of headers that aren't very interesting.
14016 If given a negative prefix, always show; if given a positive prefix,
14017 always hide."
14018   (interactive (gnus-hidden-arg))
14019   (unless (gnus-article-check-hidden-text 'boring-headers arg)
14020     (save-excursion
14021       (set-buffer gnus-article-buffer)
14022       (save-restriction
14023         (let ((buffer-read-only nil)
14024               (list gnus-boring-article-headers)
14025               (inhibit-point-motion-hooks t)
14026               elem)
14027           (nnheader-narrow-to-headers)
14028           (while list
14029             (setq elem (pop list))
14030             (goto-char (point-min))
14031             (cond
14032              ;; Hide empty headers.
14033              ((eq elem 'empty)
14034               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
14035                 (forward-line -1)
14036                 (gnus-hide-text-type
14037                  (progn (beginning-of-line) (point))
14038                  (progn 
14039                    (end-of-line)
14040                    (if (re-search-forward "^[^ \t]" nil t)
14041                        (match-beginning 0)
14042                      (point-max)))
14043                  'boring-headers)))
14044              ;; Hide boring Newsgroups header.
14045              ((eq elem 'newsgroups)
14046               (when (equal (message-fetch-field "newsgroups")
14047                            (gnus-group-real-name gnus-newsgroup-name))
14048                 (gnus-article-hide-header "newsgroups")))
14049              ((eq elem 'followup-to)
14050               (when (equal (message-fetch-field "followup-to")
14051                            (message-fetch-field "newsgroups"))
14052                 (gnus-article-hide-header "followup-to")))
14053              ((eq elem 'reply-to)
14054               (let ((from (message-fetch-field "from"))
14055                     (reply-to (message-fetch-field "reply-to")))
14056                 (when (and
14057                        from reply-to
14058                        (equal 
14059                         (nth 1 (funcall gnus-extract-address-components from))
14060                         (nth 1 (funcall gnus-extract-address-components
14061                                         reply-to))))
14062                   (gnus-article-hide-header "reply-to"))))
14063              ((eq elem 'date)
14064               (let ((date (message-fetch-field "date")))
14065                 (when (and date
14066                            (< (gnus-days-between date (current-time-string))
14067                               4))
14068                   (gnus-article-hide-header "date")))))))))))
14069
14070 (defun gnus-article-hide-header (header)
14071   (save-excursion
14072     (goto-char (point-min))
14073     (when (re-search-forward (concat "^" header ":") nil t)
14074       (gnus-hide-text-type
14075        (progn (beginning-of-line) (point))
14076        (progn 
14077          (end-of-line)
14078          (if (re-search-forward "^[^ \t]" nil t)
14079              (match-beginning 0)
14080            (point-max)))
14081        'boring-headers))))
14082
14083 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
14084 (defun gnus-article-treat-overstrike ()
14085   "Translate overstrikes into bold text."
14086   (interactive)
14087   (save-excursion
14088     (set-buffer gnus-article-buffer)
14089     (let ((buffer-read-only nil))
14090       (while (search-forward "\b" nil t)
14091         (let ((next (following-char))
14092               (previous (char-after (- (point) 2))))
14093           (cond 
14094            ((eq next previous)
14095             (gnus-put-text-property (- (point) 2) (point) 'invisible t)
14096             (gnus-put-text-property (point) (1+ (point)) 'face 'bold))
14097            ((eq next ?_)
14098             (gnus-put-text-property (1- (point)) (1+ (point)) 'invisible t)
14099             (gnus-put-text-property
14100              (- (point) 2) (1- (point)) 'face 'underline))
14101            ((eq previous ?_)
14102             (gnus-put-text-property (- (point) 2) (point) 'invisible t)
14103             (gnus-put-text-property
14104              (point) (1+ (point))       'face 'underline))))))))
14105
14106 (defun gnus-article-word-wrap ()
14107   "Format too long lines."
14108   (interactive)
14109   (save-excursion
14110     (set-buffer gnus-article-buffer)
14111     (let ((buffer-read-only nil))
14112       (widen)
14113       (goto-char (point-min))
14114       (search-forward "\n\n" nil t)
14115       (end-of-line 1)
14116       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
14117             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
14118             (adaptive-fill-mode t))
14119         (while (not (eobp))
14120           (and (>= (current-column) (min fill-column (window-width)))
14121                (/= (preceding-char) ?:)
14122                (fill-paragraph nil))
14123           (end-of-line 2))))))
14124
14125 (defun gnus-article-remove-cr ()
14126   "Remove carriage returns from an article."
14127   (interactive)
14128   (save-excursion
14129     (set-buffer gnus-article-buffer)
14130     (let ((buffer-read-only nil))
14131       (goto-char (point-min))
14132       (while (search-forward "\r" nil t)
14133         (replace-match "" t t)))))
14134
14135 (defun gnus-article-remove-trailing-blank-lines ()
14136   "Remove all trailing blank lines from the article."
14137   (interactive)
14138   (save-excursion
14139     (set-buffer gnus-article-buffer)
14140     (let ((buffer-read-only nil))
14141       (goto-char (point-max))
14142       (delete-region
14143        (point)
14144        (progn
14145          (while (looking-at "^[ \t]*$")
14146            (forward-line -1))
14147          (forward-line 1)
14148          (point))))))
14149
14150 (defun gnus-article-display-x-face (&optional force)
14151   "Look for an X-Face header and display it if present."
14152   (interactive (list 'force))
14153   (save-excursion
14154     (set-buffer gnus-article-buffer)
14155     ;; Delete the old process, if any.
14156     (when (process-status "gnus-x-face")
14157       (delete-process "gnus-x-face"))
14158     (let ((inhibit-point-motion-hooks t)
14159           (case-fold-search nil)
14160           from)
14161       (save-restriction
14162         (nnheader-narrow-to-headers)
14163         (setq from (message-fetch-field "from"))
14164         (goto-char (point-min))
14165         (when (and gnus-article-x-face-command
14166                    (or force
14167                        ;; Check whether this face is censored.
14168                        (not gnus-article-x-face-too-ugly)
14169                        (and gnus-article-x-face-too-ugly from
14170                             (not (string-match gnus-article-x-face-too-ugly
14171                                                from))))
14172                    ;; Has to be present.
14173                    (re-search-forward "^X-Face: " nil t))
14174           ;; We now have the area of the buffer where the X-Face is stored.
14175           (let ((beg (point))
14176                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
14177             ;; We display the face.
14178             (if (symbolp gnus-article-x-face-command)
14179                 ;; The command is a lisp function, so we call it.
14180                 (if (gnus-functionp gnus-article-x-face-command)
14181                     (funcall gnus-article-x-face-command beg end)
14182                   (error "%s is not a function" gnus-article-x-face-command))
14183               ;; The command is a string, so we interpret the command
14184               ;; as a, well, command, and fork it off.
14185               (let ((process-connection-type nil))
14186                 (process-kill-without-query
14187                  (start-process
14188                   "gnus-x-face" nil shell-file-name shell-command-switch
14189                   gnus-article-x-face-command))
14190                 (process-send-region "gnus-x-face" beg end)
14191                 (process-send-eof "gnus-x-face")))))))))
14192
14193 (defalias 'gnus-headers-decode-quoted-printable 'gnus-decode-rfc1522)
14194 (defun gnus-decode-rfc1522 ()
14195   "Hack to remove QP encoding from headers."
14196   (let ((case-fold-search t)
14197         (inhibit-point-motion-hooks t)
14198         (buffer-read-only nil)
14199         string)
14200     (save-restriction
14201       (narrow-to-region
14202        (goto-char (point-min))
14203        (or (search-forward "\n\n" nil t) (point-max)))
14204
14205       (while (re-search-forward 
14206               "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
14207         (setq string (match-string 1))
14208         (narrow-to-region (match-beginning 0) (match-end 0))
14209         (delete-region (point-min) (point-max))
14210         (insert string)
14211         (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
14212         (subst-char-in-region (point-min) (point-max) ?_ ? )
14213         (widen)
14214         (goto-char (point-min))))))
14215
14216 (defun gnus-article-de-quoted-unreadable (&optional force)
14217   "Do a naive translation of a quoted-printable-encoded article.
14218 This is in no way, shape or form meant as a replacement for real MIME
14219 processing, but is simply a stop-gap measure until MIME support is
14220 written.
14221 If FORCE, decode the article whether it is marked as quoted-printable
14222 or not."
14223   (interactive (list 'force))
14224   (save-excursion
14225     (set-buffer gnus-article-buffer)
14226     (let ((case-fold-search t)
14227           (buffer-read-only nil)
14228           (type (gnus-fetch-field "content-transfer-encoding")))
14229       (gnus-decode-rfc1522)
14230       (when (or force
14231                 (and type (string-match "quoted-printable" (downcase type))))
14232         (goto-char (point-min))
14233         (search-forward "\n\n" nil 'move)
14234         (gnus-mime-decode-quoted-printable (point) (point-max))))))
14235
14236 (defun gnus-mime-decode-quoted-printable (from to)
14237   "Decode Quoted-Printable in the region between FROM and TO."
14238   (interactive "r")
14239   (goto-char from)
14240   (while (search-forward "=" to t)
14241     (cond ((eq (following-char) ?\n)
14242            (delete-char -1)
14243            (delete-char 1))
14244           ((looking-at "[0-9A-F][0-9A-F]")
14245            (subst-char-in-region
14246             (1- (point)) (point) ?=
14247             (hexl-hex-string-to-integer
14248              (buffer-substring (point) (+ 2 (point)))))
14249            (delete-char 2))
14250           ((looking-at "=")
14251            (delete-char 1))
14252           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
14253
14254 (defun gnus-article-hide-pgp (&optional arg)
14255   "Toggle hiding of any PGP headers and signatures in the current article.
14256 If given a negative prefix, always show; if given a positive prefix,
14257 always hide."
14258   (interactive (gnus-hidden-arg))
14259   (unless (gnus-article-check-hidden-text 'pgp arg)
14260     (save-excursion
14261       (set-buffer gnus-article-buffer)
14262       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
14263             buffer-read-only beg end)
14264         (widen)
14265         (goto-char (point-min))
14266         ;; Hide the "header".
14267         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
14268              (gnus-hide-text (match-beginning 0) (match-end 0) props))
14269         (setq beg (point))
14270         ;; Hide the actual signature.
14271         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
14272              (setq end (1+ (match-beginning 0)))
14273              (gnus-hide-text
14274               end
14275               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
14276                   (match-end 0)
14277                 ;; Perhaps we shouldn't hide to the end of the buffer
14278                 ;; if there is no end to the signature?
14279                 (point-max))
14280               props))
14281         ;; Hide "- " PGP quotation markers.
14282         (when (and beg end)
14283           (narrow-to-region beg end)
14284           (goto-char (point-min))
14285           (while (re-search-forward "^- " nil t)
14286             (gnus-hide-text (match-beginning 0) (match-end 0) props))
14287           (widen))))))
14288
14289 (defun gnus-article-hide-signature (&optional arg)
14290   "Hide the signature in the current article.
14291 If given a negative prefix, always show; if given a positive prefix,
14292 always hide."
14293   (interactive (gnus-hidden-arg))
14294   (unless (gnus-article-check-hidden-text 'signature arg)
14295     (save-excursion
14296       (set-buffer gnus-article-buffer)
14297       (save-restriction
14298         (let ((buffer-read-only nil))
14299           (when (gnus-narrow-to-signature)
14300             (gnus-hide-text-type (point-min) (point-max) 'signature)))))))
14301
14302 (defun gnus-article-strip-leading-blank-lines ()
14303   "Remove all blank lines from the beginning of the article."
14304   (interactive)
14305   (save-excursion
14306     (set-buffer gnus-article-buffer)
14307     (let (buffer-read-only)
14308       (goto-char (point-min))
14309       (when (search-forward "\n\n" nil t)
14310         (while (looking-at "[ \t]$")
14311           (gnus-delete-line))))))
14312
14313 (defvar mime::preview/content-list)
14314 (defvar mime::preview-content-info/point-min)
14315 (defun gnus-narrow-to-signature ()
14316   "Narrow to the signature."
14317   (widen)
14318   (if (and (boundp 'mime::preview/content-list)
14319            mime::preview/content-list)
14320       (let ((pcinfo (car (last mime::preview/content-list))))
14321         (condition-case ()
14322             (narrow-to-region
14323              (funcall (intern "mime::preview-content-info/point-min") pcinfo)
14324              (point-max))
14325           (error nil))))
14326   (goto-char (point-max))
14327   (when (re-search-backward gnus-signature-separator nil t)
14328     (forward-line 1)
14329     (when (or (null gnus-signature-limit)
14330               (and (numberp gnus-signature-limit)
14331                    (< (- (point-max) (point)) gnus-signature-limit))
14332               (and (gnus-functionp gnus-signature-limit)
14333                    (funcall gnus-signature-limit))
14334               (and (stringp gnus-signature-limit)
14335                    (not (re-search-forward gnus-signature-limit nil t))))
14336       (narrow-to-region (point) (point-max))
14337       t)))
14338
14339 (defun gnus-hidden-arg ()
14340   "Return the current prefix arg as a number, or 0 if no prefix."
14341   (list (if current-prefix-arg
14342             (prefix-numeric-value current-prefix-arg)
14343           0)))
14344
14345 (defun gnus-article-check-hidden-text (type arg)
14346   "Return nil if hiding is necessary.
14347 Arg can be nil or a number.  Nil and positive means hide, negative
14348 means show, 0 means toggle."
14349   (save-excursion
14350     (set-buffer gnus-article-buffer)
14351     (let ((hide (gnus-article-hidden-text-p type)))
14352       (cond
14353        ((or (null arg)
14354             (> arg 0))
14355         nil)
14356        ((< arg 0)
14357         (gnus-article-show-hidden-text type))
14358        (t
14359         (if (eq hide 'hidden)
14360             (gnus-article-show-hidden-text type)
14361           nil))))))
14362
14363 (defun gnus-article-hidden-text-p (type)
14364   "Say whether the current buffer contains hidden text of type TYPE."
14365   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type)))
14366     (when pos
14367       (if (get-text-property pos 'invisible)
14368           'hidden
14369         'shown))))
14370
14371 (defun gnus-article-hide (&optional arg force)
14372   "Hide all the gruft in the current article.
14373 This means that PGP stuff, signatures, cited text and (some)
14374 headers will be hidden.
14375 If given a prefix, show the hidden text instead."
14376   (interactive (list current-prefix-arg 'force))
14377   (gnus-article-hide-headers arg)
14378   (gnus-article-hide-pgp arg)
14379   (gnus-article-hide-citation-maybe arg force)
14380   (gnus-article-hide-signature arg))
14381
14382 (defun gnus-article-show-hidden-text (type &optional hide)
14383   "Show all hidden text of type TYPE.
14384 If HIDE, hide the text instead."
14385   (save-excursion
14386     (set-buffer gnus-article-buffer)
14387     (let ((buffer-read-only nil)
14388           (inhibit-point-motion-hooks t)
14389           (beg (point-min)))
14390       (while (gnus-goto-char (text-property-any
14391                               beg (point-max) 'gnus-type type))
14392         (setq beg (point))
14393         (forward-char)
14394         (if hide
14395             (gnus-hide-text beg (point) gnus-hidden-properties)
14396           (gnus-unhide-text beg (point)))
14397         (setq beg (point)))
14398       t)))
14399
14400 (defvar gnus-article-time-units
14401   `((year . ,(* 365.25 24 60 60))
14402     (week . ,(* 7 24 60 60))
14403     (day . ,(* 24 60 60))
14404     (hour . ,(* 60 60))
14405     (minute . 60)
14406     (second . 1))
14407   "Mapping from time units to seconds.")
14408
14409 (defun gnus-article-date-ut (&optional type highlight)
14410   "Convert DATE date to universal time in the current article.
14411 If TYPE is `local', convert to local time; if it is `lapsed', output
14412 how much time has lapsed since DATE."
14413   (interactive (list 'ut t))
14414   (let* ((header (or gnus-current-headers
14415                      (gnus-summary-article-header) ""))
14416          (date (and (vectorp header) (mail-header-date header)))
14417          (date-regexp "^Date: \\|^X-Sent: ")
14418          (now (current-time))
14419          (inhibit-point-motion-hooks t)
14420          bface eface)
14421     (when (and date (not (string= date "")))
14422       (save-excursion
14423         (set-buffer gnus-article-buffer)
14424         (save-restriction
14425           (nnheader-narrow-to-headers)
14426           (let ((buffer-read-only nil))
14427             ;; Delete any old Date headers.
14428             (if (re-search-forward date-regexp nil t)
14429                 (progn
14430                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
14431                         eface (get-text-property (1- (gnus-point-at-eol))
14432                                                  'face))
14433                   (message-remove-header date-regexp t)
14434                   (beginning-of-line))
14435               (goto-char (point-max)))
14436             (insert (gnus-make-date-line date type))
14437             ;; Do highlighting.
14438             (forward-line -1)
14439             (when (and (gnus-visual-p 'article-highlight 'highlight)
14440                        (looking-at "\\([^:]+\\): *\\(.*\\)$"))
14441               (gnus-put-text-property (match-beginning 1) (match-end 1)
14442                                  'face bface)
14443               (gnus-put-text-property (match-beginning 2) (match-end 2)
14444                                  'face eface))))))))
14445
14446 (defun gnus-make-date-line (date type)
14447   "Return a DATE line of TYPE."
14448   (cond
14449    ;; Convert to the local timezone.  We have to slap a
14450    ;; `condition-case' round the calls to the timezone
14451    ;; functions since they aren't particularly resistant to
14452    ;; buggy dates.
14453    ((eq type 'local)
14454     (concat "Date: " (condition-case ()
14455                          (timezone-make-date-arpa-standard date)
14456                        (error date))
14457             "\n"))
14458    ;; Convert to Universal Time.
14459    ((eq type 'ut)
14460     (concat "Date: "
14461             (condition-case ()
14462                 (timezone-make-date-arpa-standard date nil "UT")
14463               (error date))
14464             "\n"))
14465    ;; Get the original date from the article.
14466    ((eq type 'original)
14467     (concat "Date: " date "\n"))
14468    ;; Do an X-Sent lapsed format.
14469    ((eq type 'lapsed)
14470     ;; If the date is seriously mangled, the timezone
14471     ;; functions are liable to bug out, so we condition-case
14472     ;; the entire thing.
14473     (let* ((now (current-time))
14474            (real-time
14475             (condition-case ()
14476                 (gnus-time-minus
14477                  (gnus-encode-date
14478                   (timezone-make-date-arpa-standard
14479                    (current-time-string now)
14480                    (current-time-zone now) "UT"))
14481                  (gnus-encode-date
14482                   (timezone-make-date-arpa-standard
14483                    date nil "UT")))
14484               (error '(0 0))))
14485            (real-sec (+ (* (float (car real-time)) 65536)
14486                         (cadr real-time)))
14487            (sec (abs real-sec))
14488            num prev)
14489       (cond
14490        ((equal real-time '(0 0))
14491         "X-Sent: Unknown\n")
14492        ((zerop sec)
14493         "X-Sent: Now\n")
14494        (t
14495         (concat
14496          "X-Sent: "
14497          ;; This is a bit convoluted, but basically we go
14498          ;; through the time units for years, weeks, etc,
14499          ;; and divide things to see whether that results
14500          ;; in positive answers.
14501          (mapconcat
14502           (lambda (unit)
14503             (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
14504                 ;; The (remaining) seconds are too few to
14505                 ;; be divided into this time unit.
14506                 ""
14507               ;; It's big enough, so we output it.
14508               (setq sec (- sec (* num (cdr unit))))
14509               (prog1
14510                   (concat (if prev ", " "") (int-to-string
14511                                              (floor num))
14512                           " " (symbol-name (car unit)) 
14513                           (if (> num 1) "s" ""))
14514                 (setq prev t))))
14515           gnus-article-time-units "")
14516          ;; If dates are odd, then it might appear like the
14517          ;; article was sent in the future.
14518          (if (> real-sec 0)
14519              " ago\n"
14520            " in the future\n"))))))
14521    (t
14522     (error "Unknown conversion type: %s" type))))
14523
14524 (defun gnus-article-date-local (&optional highlight)
14525   "Convert the current article date to the local timezone."
14526   (interactive (list t))
14527   (gnus-article-date-ut 'local highlight))
14528
14529 (defun gnus-article-date-original (&optional highlight)
14530   "Convert the current article date to what it was originally.
14531 This is only useful if you have used some other date conversion
14532 function and want to see what the date was before converting."
14533   (interactive (list t))
14534   (gnus-article-date-ut 'original highlight))
14535
14536 (defun gnus-article-date-lapsed (&optional highlight)
14537   "Convert the current article date to time lapsed since it was sent."
14538   (interactive (list t))
14539   (gnus-article-date-ut 'lapsed highlight))
14540
14541 (defun gnus-article-maybe-highlight ()
14542   "Do some article highlighting if `gnus-visual' is non-nil."
14543   (if (gnus-visual-p 'article-highlight 'highlight)
14544       (gnus-article-highlight-some)))
14545
14546 ;;; Article savers.
14547
14548 (defun gnus-output-to-rmail (file-name)
14549   "Append the current article to an Rmail file named FILE-NAME."
14550   (require 'rmail)
14551   ;; Most of these codes are borrowed from rmailout.el.
14552   (setq file-name (expand-file-name file-name))
14553   (setq rmail-default-rmail-file file-name)
14554   (let ((artbuf (current-buffer))
14555         (tmpbuf (get-buffer-create " *Gnus-output*")))
14556     (save-excursion
14557       (or (get-file-buffer file-name)
14558           (file-exists-p file-name)
14559           (if (gnus-yes-or-no-p
14560                (concat "\"" file-name "\" does not exist, create it? "))
14561               (let ((file-buffer (create-file-buffer file-name)))
14562                 (save-excursion
14563                   (set-buffer file-buffer)
14564                   (rmail-insert-rmail-file-header)
14565                   (let ((require-final-newline nil))
14566                     (write-region (point-min) (point-max) file-name t 1)))
14567                 (kill-buffer file-buffer))
14568             (error "Output file does not exist")))
14569       (set-buffer tmpbuf)
14570       (buffer-disable-undo (current-buffer))
14571       (erase-buffer)
14572       (insert-buffer-substring artbuf)
14573       (gnus-convert-article-to-rmail)
14574       ;; Decide whether to append to a file or to an Emacs buffer.
14575       (let ((outbuf (get-file-buffer file-name)))
14576         (if (not outbuf)
14577             (append-to-file (point-min) (point-max) file-name)
14578           ;; File has been visited, in buffer OUTBUF.
14579           (set-buffer outbuf)
14580           (let ((buffer-read-only nil)
14581                 (msg (and (boundp 'rmail-current-message)
14582                           (symbol-value 'rmail-current-message))))
14583             ;; If MSG is non-nil, buffer is in RMAIL mode.
14584             (if msg
14585                 (progn (widen)
14586                        (narrow-to-region (point-max) (point-max))))
14587             (insert-buffer-substring tmpbuf)
14588             (if msg
14589                 (progn
14590                   (goto-char (point-min))
14591                   (widen)
14592                   (search-backward "\^_")
14593                   (narrow-to-region (point) (point-max))
14594                   (goto-char (1+ (point-min)))
14595                   (rmail-count-new-messages t)
14596                   (rmail-show-message msg)))))))
14597     (kill-buffer tmpbuf)))
14598
14599 (defun gnus-output-to-file (file-name)
14600   "Append the current article to a file named FILE-NAME."
14601   (let ((artbuf (current-buffer)))
14602     (nnheader-temp-write nil
14603       (insert-buffer-substring artbuf)
14604       ;; Append newline at end of the buffer as separator, and then
14605       ;; save it to file.
14606       (goto-char (point-max))
14607       (insert "\n")
14608       (append-to-file (point-min) (point-max) file-name))))
14609
14610 (defun gnus-convert-article-to-rmail ()
14611   "Convert article in current buffer to Rmail message format."
14612   (let ((buffer-read-only nil))
14613     ;; Convert article directly into Babyl format.
14614     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14615     (goto-char (point-min))
14616     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14617     (while (search-forward "\n\^_" nil t) ;single char
14618       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14619     (goto-char (point-max))
14620     (insert "\^_")))
14621
14622 (defun gnus-narrow-to-page (&optional arg)
14623   "Narrow the article buffer to a page.
14624 If given a numerical ARG, move forward ARG pages."
14625   (interactive "P")
14626   (setq arg (if arg (prefix-numeric-value arg) 0))
14627   (save-excursion
14628     (set-buffer gnus-article-buffer)
14629     (goto-char (point-min))
14630     (widen)
14631     (when (gnus-visual-p 'page-marker)
14632       (let ((buffer-read-only nil))
14633         (gnus-remove-text-with-property 'gnus-prev)
14634         (gnus-remove-text-with-property 'gnus-next)))
14635     (when
14636         (cond ((< arg 0)
14637                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14638               ((> arg 0)
14639                (re-search-forward page-delimiter nil 'move arg)))
14640       (goto-char (match-end 0)))
14641     (narrow-to-region
14642      (point)
14643      (if (re-search-forward page-delimiter nil 'move)
14644          (match-beginning 0)
14645        (point)))
14646     (when (and (gnus-visual-p 'page-marker)
14647                (not (= (point-min) 1)))
14648       (save-excursion
14649         (goto-char (point-min))
14650         (gnus-insert-prev-page-button)))
14651     (when (and (gnus-visual-p 'page-marker)
14652                (not (= (1- (point-max)) (buffer-size))))
14653       (save-excursion
14654         (goto-char (point-max))
14655         (gnus-insert-next-page-button)))))
14656
14657 ;; Article mode commands
14658
14659 (defun gnus-article-goto-next-page ()
14660   "Show the next page of the article."
14661   (interactive)
14662   (when (gnus-article-next-page)
14663     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
14664
14665 (defun gnus-article-goto-prev-page ()
14666   "Show the next page of the article."
14667   (interactive)
14668   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))
14669     (gnus-article-prev-page nil)))
14670
14671 (defun gnus-article-next-page (&optional lines)
14672   "Show the next page of the current article.
14673 If end of article, return non-nil.  Otherwise return nil.
14674 Argument LINES specifies lines to be scrolled up."
14675   (interactive "p")
14676   (move-to-window-line -1)
14677   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14678   (if (save-excursion
14679         (end-of-line)
14680         (and (pos-visible-in-window-p)  ;Not continuation line.
14681              (eobp)))
14682       ;; Nothing in this page.
14683       (if (or (not gnus-break-pages)
14684               (save-excursion
14685                 (save-restriction
14686                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14687           t                             ;Nothing more.
14688         (gnus-narrow-to-page 1)         ;Go to next page.
14689         nil)
14690     ;; More in this page.
14691     (condition-case ()
14692         (scroll-up lines)
14693       (end-of-buffer
14694        ;; Long lines may cause an end-of-buffer error.
14695        (goto-char (point-max))))
14696     (move-to-window-line 0)
14697     nil))
14698
14699 (defun gnus-article-prev-page (&optional lines)
14700   "Show previous page of current article.
14701 Argument LINES specifies lines to be scrolled down."
14702   (interactive "p")
14703   (move-to-window-line 0)
14704   (if (and gnus-break-pages
14705            (bobp)
14706            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14707       (progn
14708         (gnus-narrow-to-page -1)        ;Go to previous page.
14709         (goto-char (point-max))
14710         (recenter -1))
14711     (prog1
14712         (condition-case ()
14713             (scroll-down lines)
14714           (error nil))
14715       (move-to-window-line 0))))
14716
14717 (defun gnus-article-refer-article ()
14718   "Read article specified by message-id around point."
14719   (interactive)
14720   (let ((point (point)))
14721     (search-forward ">" nil t)          ;Move point to end of "<....>".
14722     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14723         (let ((message-id (match-string 1)))
14724           (goto-char point)
14725           (set-buffer gnus-summary-buffer)
14726           (gnus-summary-refer-article message-id))
14727       (goto-char (point))
14728       (error "No references around point"))))
14729
14730 (defun gnus-article-show-summary ()
14731   "Reconfigure windows to show summary buffer."
14732   (interactive)
14733   (gnus-configure-windows 'article)
14734   (gnus-summary-goto-subject gnus-current-article))
14735
14736 (defun gnus-article-describe-briefly ()
14737   "Describe article mode commands briefly."
14738   (interactive)
14739   (gnus-message 6
14740                 (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")))
14741
14742 (defun gnus-article-summary-command ()
14743   "Execute the last keystroke in the summary buffer."
14744   (interactive)
14745   (let ((obuf (current-buffer))
14746         (owin (current-window-configuration))
14747         func)
14748     (switch-to-buffer gnus-summary-buffer 'norecord)
14749     (setq func (lookup-key (current-local-map) (this-command-keys)))
14750     (call-interactively func)
14751     (set-buffer obuf)
14752     (set-window-configuration owin)
14753     (set-window-point (get-buffer-window (current-buffer)) (point))))
14754
14755 (defun gnus-article-summary-command-nosave ()
14756   "Execute the last keystroke in the summary buffer."
14757   (interactive)
14758   (let (func)
14759     (pop-to-buffer gnus-summary-buffer 'norecord)
14760     (setq func (lookup-key (current-local-map) (this-command-keys)))
14761     (call-interactively func)))
14762
14763 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14764   "Read a summary buffer key sequence and execute it from the article buffer."
14765   (interactive "P")
14766   (let ((nosaves
14767          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14768            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14769            "=" "^" "\M-^" "|"))
14770         keys)
14771     (save-excursion
14772       (set-buffer gnus-summary-buffer)
14773       (push (or key last-command-event) unread-command-events)
14774       (setq keys (read-key-sequence nil)))
14775     (message "")
14776
14777     (if (member keys nosaves)
14778         (let (func)
14779           (pop-to-buffer gnus-summary-buffer 'norecord)
14780           (if (setq func (lookup-key (current-local-map) keys))
14781               (call-interactively func)
14782             (ding)))
14783       (let ((obuf (current-buffer))
14784             (owin (current-window-configuration))
14785             (opoint (point))
14786             func in-buffer)
14787         (if not-restore-window
14788             (pop-to-buffer gnus-summary-buffer 'norecord)
14789           (switch-to-buffer gnus-summary-buffer 'norecord))
14790         (setq in-buffer (current-buffer))
14791         (if (setq func (lookup-key (current-local-map) keys))
14792             (call-interactively func)
14793           (ding))
14794         (when (eq in-buffer (current-buffer))
14795           (set-buffer obuf)
14796           (unless not-restore-window
14797             (set-window-configuration owin))
14798           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14799
14800 \f
14801 ;;;
14802 ;;; Kill file handling.
14803 ;;;
14804
14805 ;;;###autoload
14806 (defalias 'gnus-batch-kill 'gnus-batch-score)
14807 ;;;###autoload
14808 (defun gnus-batch-score ()
14809   "Run batched scoring.
14810 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14811 Newsgroups is a list of strings in Bnews format.  If you want to score
14812 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14813 score the alt hierarchy, you'd say \"!alt.all\"."
14814   (interactive)
14815   (let* ((yes-and-no
14816           (gnus-newsrc-parse-options
14817            (apply (function concat)
14818                   (mapcar (lambda (g) (concat g " "))
14819                           command-line-args-left))))
14820          (gnus-expert-user t)
14821          (nnmail-spool-file nil)
14822          (gnus-use-dribble-file nil)
14823          (yes (car yes-and-no))
14824          (no (cdr yes-and-no))
14825          group newsrc entry
14826          ;; Disable verbose message.
14827          gnus-novice-user gnus-large-newsgroup)
14828     ;; Eat all arguments.
14829     (setq command-line-args-left nil)
14830     ;; Start Gnus.
14831     (gnus)
14832     ;; Apply kills to specified newsgroups in command line arguments.
14833     (setq newsrc (cdr gnus-newsrc-alist))
14834     (while newsrc
14835       (setq group (caar newsrc))
14836       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14837       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14838                (and (car entry)
14839                     (or (eq (car entry) t)
14840                         (not (zerop (car entry)))))
14841                (if yes (string-match yes group) t)
14842                (or (null no) (not (string-match no group))))
14843           (progn
14844             (gnus-summary-read-group group nil t nil t)
14845             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14846                  (gnus-summary-exit))))
14847       (setq newsrc (cdr newsrc)))
14848     ;; Exit Emacs.
14849     (switch-to-buffer gnus-group-buffer)
14850     (gnus-group-save-newsrc)))
14851
14852 (defun gnus-apply-kill-file ()
14853   "Apply a kill file to the current newsgroup.
14854 Returns the number of articles marked as read."
14855   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14856           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14857       (gnus-apply-kill-file-internal)
14858     0))
14859
14860 (defun gnus-kill-save-kill-buffer ()
14861   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14862     (when (get-file-buffer file)
14863       (save-excursion
14864         (set-buffer (get-file-buffer file))
14865         (and (buffer-modified-p) (save-buffer))
14866         (kill-buffer (current-buffer))))))
14867
14868 (defvar gnus-kill-file-name "KILL"
14869   "Suffix of the kill files.")
14870
14871 (defun gnus-newsgroup-kill-file (newsgroup)
14872   "Return the name of a kill file name for NEWSGROUP.
14873 If NEWSGROUP is nil, return the global kill file name instead."
14874   (cond 
14875    ;; The global KILL file is placed at top of the directory.
14876    ((or (null newsgroup)
14877         (string-equal newsgroup ""))
14878     (expand-file-name gnus-kill-file-name
14879                       gnus-kill-files-directory))
14880    ;; Append ".KILL" to newsgroup name.
14881    ((gnus-use-long-file-name 'not-kill)
14882     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14883                               "." gnus-kill-file-name)
14884                       gnus-kill-files-directory))
14885    ;; Place "KILL" under the hierarchical directory.
14886    (t
14887     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14888                               "/" gnus-kill-file-name)
14889                       gnus-kill-files-directory))))
14890
14891 \f
14892 ;;;
14893 ;;; Dribble file
14894 ;;;
14895
14896 (defvar gnus-dribble-ignore nil)
14897 (defvar gnus-dribble-eval-file nil)
14898
14899 (defun gnus-dribble-file-name ()
14900   "Return the dribble file for the current .newsrc."
14901   (concat
14902    (if gnus-dribble-directory
14903        (concat (file-name-as-directory gnus-dribble-directory)
14904                (file-name-nondirectory gnus-current-startup-file))
14905      gnus-current-startup-file)
14906    "-dribble"))
14907
14908 (defun gnus-dribble-enter (string)
14909   "Enter STRING into the dribble buffer."
14910   (if (and (not gnus-dribble-ignore)
14911            gnus-dribble-buffer
14912            (buffer-name gnus-dribble-buffer))
14913       (let ((obuf (current-buffer)))
14914         (set-buffer gnus-dribble-buffer)
14915         (insert string "\n")
14916         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14917         (set-buffer obuf))))
14918
14919 (defun gnus-dribble-read-file ()
14920   "Read the dribble file from disk."
14921   (let ((dribble-file (gnus-dribble-file-name)))
14922     (save-excursion
14923       (set-buffer (setq gnus-dribble-buffer
14924                         (get-buffer-create
14925                          (file-name-nondirectory dribble-file))))
14926       (gnus-add-current-to-buffer-list)
14927       (erase-buffer)
14928       (setq buffer-file-name dribble-file)
14929       (auto-save-mode t)
14930       (buffer-disable-undo (current-buffer))
14931       (bury-buffer (current-buffer))
14932       (set-buffer-modified-p nil)
14933       (let ((auto (make-auto-save-file-name))
14934             (gnus-dribble-ignore t)
14935             modes)
14936         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14937           ;; Load whichever file is newest -- the auto save file
14938           ;; or the "real" file.
14939           (if (file-newer-than-file-p auto dribble-file)
14940               (insert-file-contents auto)
14941             (insert-file-contents dribble-file))
14942           (unless (zerop (buffer-size))
14943             (set-buffer-modified-p t))
14944           ;; Set the file modes to reflect the .newsrc file modes.
14945           (save-buffer)
14946           (when (and (file-exists-p gnus-current-startup-file)
14947                      (setq modes (file-modes gnus-current-startup-file)))
14948             (set-file-modes dribble-file modes))
14949           ;; Possibly eval the file later.
14950           (when (gnus-y-or-n-p
14951                  "Auto-save file exists.  Do you want to read it? ")
14952             (setq gnus-dribble-eval-file t)))))))
14953
14954 (defun gnus-dribble-eval-file ()
14955   (when gnus-dribble-eval-file
14956     (setq gnus-dribble-eval-file nil)
14957     (save-excursion
14958       (let ((gnus-dribble-ignore t))
14959         (set-buffer gnus-dribble-buffer)
14960         (eval-buffer (current-buffer))))))
14961
14962 (defun gnus-dribble-delete-file ()
14963   (when (file-exists-p (gnus-dribble-file-name))
14964     (delete-file (gnus-dribble-file-name)))
14965   (when gnus-dribble-buffer
14966     (save-excursion
14967       (set-buffer gnus-dribble-buffer)
14968       (let ((auto (make-auto-save-file-name)))
14969         (if (file-exists-p auto)
14970             (delete-file auto))
14971         (erase-buffer)
14972         (set-buffer-modified-p nil)))))
14973
14974 (defun gnus-dribble-save ()
14975   (when (and gnus-dribble-buffer
14976              (buffer-name gnus-dribble-buffer))
14977     (save-excursion
14978       (set-buffer gnus-dribble-buffer)
14979       (save-buffer))))
14980
14981 (defun gnus-dribble-clear ()
14982   (when (gnus-buffer-exists-p gnus-dribble-buffer)
14983     (save-excursion
14984       (set-buffer gnus-dribble-buffer)
14985       (erase-buffer)
14986       (set-buffer-modified-p nil)
14987       (setq buffer-saved-size (buffer-size)))))
14988
14989 \f
14990 ;;;
14991 ;;; Server Communication
14992 ;;;
14993
14994 (defun gnus-start-news-server (&optional confirm)
14995   "Open a method for getting news.
14996 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14997   (let (how)
14998     (if gnus-current-select-method
14999         ;; Stream is already opened.
15000         nil
15001       ;; Open NNTP server.
15002       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
15003       (if confirm
15004           (progn
15005             ;; Read server name with completion.
15006             (setq gnus-nntp-server
15007                   (completing-read "NNTP server: "
15008                                    (mapcar (lambda (server) (list server))
15009                                            (cons (list gnus-nntp-server)
15010                                                  gnus-secondary-servers))
15011                                    nil nil gnus-nntp-server))))
15012
15013       (if (and gnus-nntp-server
15014                (stringp gnus-nntp-server)
15015                (not (string= gnus-nntp-server "")))
15016           (setq gnus-select-method
15017                 (cond ((or (string= gnus-nntp-server "")
15018                            (string= gnus-nntp-server "::"))
15019                        (list 'nnspool (system-name)))
15020                       ((string-match "^:" gnus-nntp-server)
15021                        (list 'nnmh gnus-nntp-server
15022                              (list 'nnmh-directory
15023                                    (file-name-as-directory
15024                                     (expand-file-name
15025                                      (concat "~/" (substring
15026                                                    gnus-nntp-server 1)))))
15027                              (list 'nnmh-get-new-mail nil)))
15028                       (t
15029                        (list 'nntp gnus-nntp-server)))))
15030
15031       (setq how (car gnus-select-method))
15032       (cond ((eq how 'nnspool)
15033              (require 'nnspool)
15034              (gnus-message 5 "Looking up local news spool..."))
15035             ((eq how 'nnmh)
15036              (require 'nnmh)
15037              (gnus-message 5 "Looking up mh spool..."))
15038             (t
15039              (require 'nntp)))
15040       (setq gnus-current-select-method gnus-select-method)
15041       (run-hooks 'gnus-open-server-hook)
15042       (or
15043        ;; gnus-open-server-hook might have opened it
15044        (gnus-server-opened gnus-select-method)
15045        (gnus-open-server gnus-select-method)
15046        (gnus-y-or-n-p
15047         (format
15048          "%s (%s) open error: '%s'.     Continue? "
15049          (car gnus-select-method) (cadr gnus-select-method)
15050          (gnus-status-message gnus-select-method)))
15051        (gnus-error 1 "Couldn't open server on %s"
15052                    (nth 1 gnus-select-method))))))
15053
15054 (defun gnus-check-group (group)
15055   "Try to make sure that the server where GROUP exists is alive."
15056   (let ((method (gnus-find-method-for-group group)))
15057     (or (gnus-server-opened method)
15058         (gnus-open-server method))))
15059
15060 (defun gnus-check-server (&optional method silent)
15061   "Check whether the connection to METHOD is down.
15062 If METHOD is nil, use `gnus-select-method'.
15063 If it is down, start it up (again)."
15064   (let ((method (or method gnus-select-method)))
15065     ;; Transform virtual server names into select methods.
15066     (when (stringp method)
15067       (setq method (gnus-server-to-method method)))
15068     (if (gnus-server-opened method)
15069         ;; The stream is already opened.
15070         t
15071       ;; Open the server.
15072       (unless silent
15073         (gnus-message 5 "Opening %s server%s..." (car method)
15074                       (if (equal (nth 1 method) "") ""
15075                         (format " on %s" (nth 1 method)))))
15076       (run-hooks 'gnus-open-server-hook)
15077       (prog1
15078           (gnus-open-server method)
15079         (unless silent
15080           (message ""))))))
15081
15082 (defun gnus-get-function (method function &optional noerror)
15083   "Return a function symbol based on METHOD and FUNCTION."
15084   ;; Translate server names into methods.
15085   (unless method
15086     (error "Attempted use of a nil select method"))
15087   (when (stringp method)
15088     (setq method (gnus-server-to-method method)))
15089   (let ((func (intern (format "%s-%s" (car method) function))))
15090     ;; If the functions isn't bound, we require the backend in
15091     ;; question.
15092     (unless (fboundp func)
15093       (require (car method))
15094       (when (and (not (fboundp func))
15095                  (not noerror))
15096         ;; This backend doesn't implement this function.
15097         (error "No such function: %s" func)))
15098     func))
15099
15100 \f
15101 ;;;
15102 ;;; Interface functions to the backends.
15103 ;;;
15104
15105 (defun gnus-open-server (method)
15106   "Open a connection to METHOD."
15107   (when (stringp method)
15108     (setq method (gnus-server-to-method method)))
15109   (let ((elem (assoc method gnus-opened-servers)))
15110     ;; If this method was previously denied, we just return nil.
15111     (if (eq (nth 1 elem) 'denied)
15112         (progn
15113           (gnus-message 1 "Denied server")
15114           nil)
15115       ;; Open the server.
15116       (let ((result
15117              (funcall (gnus-get-function method 'open-server)
15118                       (nth 1 method) (nthcdr 2 method))))
15119         ;; If this hasn't been opened before, we add it to the list.
15120         (unless elem
15121           (setq elem (list method nil)
15122                 gnus-opened-servers (cons elem gnus-opened-servers)))
15123         ;; Set the status of this server.
15124         (setcar (cdr elem) (if result 'ok 'denied))
15125         ;; Return the result from the "open" call.
15126         result))))
15127
15128 (defun gnus-close-server (method)
15129   "Close the connection to METHOD."
15130   (when (stringp method)
15131     (setq method (gnus-server-to-method method)))
15132   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
15133
15134 (defun gnus-request-list (method)
15135   "Request the active file from METHOD."
15136   (when (stringp method)
15137     (setq method (gnus-server-to-method method)))
15138   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
15139
15140 (defun gnus-request-list-newsgroups (method)
15141   "Request the newsgroups file from METHOD."
15142   (when (stringp method)
15143     (setq method (gnus-server-to-method method)))
15144   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
15145
15146 (defun gnus-request-newgroups (date method)
15147   "Request all new groups since DATE from METHOD."
15148   (when (stringp method)
15149     (setq method (gnus-server-to-method method)))
15150   (funcall (gnus-get-function method 'request-newgroups)
15151            date (nth 1 method)))
15152
15153 (defun gnus-server-opened (method)
15154   "Check whether a connection to METHOD has been opened."
15155   (when (stringp method)
15156     (setq method (gnus-server-to-method method)))
15157   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
15158
15159 (defun gnus-status-message (method)
15160   "Return the status message from METHOD.
15161 If METHOD is a string, it is interpreted as a group name.   The method
15162 this group uses will be queried."
15163   (let ((method (if (stringp method) (gnus-find-method-for-group method)
15164                   method)))
15165     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
15166
15167 (defun gnus-request-group (group &optional dont-check method)
15168   "Request GROUP.  If DONT-CHECK, no information is required."
15169   (let ((method (or method (gnus-find-method-for-group group))))
15170     (when (stringp method)
15171       (setq method (gnus-server-to-method method)))
15172     (funcall (gnus-get-function method 'request-group)
15173              (gnus-group-real-name group) (nth 1 method) dont-check)))
15174
15175 (defun gnus-request-asynchronous (group &optional articles)
15176   "Request that GROUP behave asynchronously.
15177 ARTICLES is the `data' of the group."
15178   (let ((method (gnus-find-method-for-group group)))
15179     (funcall (gnus-get-function method 'request-asynchronous)
15180              (gnus-group-real-name group) (nth 1 method) articles)))
15181
15182 (defun gnus-list-active-group (group)
15183   "Request active information on GROUP."
15184   (let ((method (gnus-find-method-for-group group))
15185         (func 'list-active-group))
15186     (when (gnus-check-backend-function func group)
15187       (funcall (gnus-get-function method func)
15188                (gnus-group-real-name group) (nth 1 method)))))
15189
15190 (defun gnus-request-group-description (group)
15191   "Request a description of GROUP."
15192   (let ((method (gnus-find-method-for-group group))
15193         (func 'request-group-description))
15194     (when (gnus-check-backend-function func group)
15195       (funcall (gnus-get-function method func)
15196                (gnus-group-real-name group) (nth 1 method)))))
15197
15198 (defun gnus-close-group (group)
15199   "Request the GROUP be closed."
15200   (let ((method (gnus-find-method-for-group group)))
15201     (funcall (gnus-get-function method 'close-group)
15202              (gnus-group-real-name group) (nth 1 method))))
15203
15204 (defun gnus-retrieve-headers (articles group &optional fetch-old)
15205   "Request headers for ARTICLES in GROUP.
15206 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
15207   (let ((method (gnus-find-method-for-group group)))
15208     (if (and gnus-use-cache (numberp (car articles)))
15209         (gnus-cache-retrieve-headers articles group fetch-old)
15210       (funcall (gnus-get-function method 'retrieve-headers)
15211                articles (gnus-group-real-name group) (nth 1 method)
15212                fetch-old))))
15213
15214 (defun gnus-retrieve-groups (groups method)
15215   "Request active information on GROUPS from METHOD."
15216   (when (stringp method)
15217     (setq method (gnus-server-to-method method)))
15218   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
15219
15220 (defun gnus-request-type (group &optional article)
15221   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
15222   (let ((method (gnus-find-method-for-group group)))
15223     (if (not (gnus-check-backend-function 'request-type (car method)))
15224         'unknown
15225       (funcall (gnus-get-function method 'request-type)
15226                (gnus-group-real-name group) article))))
15227
15228 (defun gnus-request-update-mark (group article mark)
15229   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
15230   (let ((method (gnus-find-method-for-group group)))
15231     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
15232         mark
15233       (funcall (gnus-get-function method 'request-update-mark)
15234                (gnus-group-real-name group) article mark))))
15235
15236 (defun gnus-request-article (article group &optional buffer)
15237   "Request the ARTICLE in GROUP.
15238 ARTICLE can either be an article number or an article Message-ID.
15239 If BUFFER, insert the article in that group."
15240   (let ((method (gnus-find-method-for-group group)))
15241     (funcall (gnus-get-function method 'request-article)
15242              article (gnus-group-real-name group) (nth 1 method) buffer)))
15243
15244 (defun gnus-request-head (article group)
15245   "Request the head of ARTICLE in GROUP."
15246   (let* ((method (gnus-find-method-for-group group))
15247          (head (gnus-get-function method 'request-head t)))
15248     (if (fboundp head)
15249         (funcall head article (gnus-group-real-name group) (nth 1 method))
15250       (let ((res (gnus-request-article article group)))
15251         (when res
15252           (save-excursion
15253             (set-buffer nntp-server-buffer)
15254             (goto-char (point-min))
15255             (when (search-forward "\n\n" nil t)
15256               (delete-region (1- (point)) (point-max)))
15257             (nnheader-fold-continuation-lines)))
15258         res))))
15259
15260 (defun gnus-request-body (article group)
15261   "Request the body of ARTICLE in GROUP."
15262   (let ((method (gnus-find-method-for-group group)))
15263     (funcall (gnus-get-function method 'request-body)
15264              article (gnus-group-real-name group) (nth 1 method))))
15265
15266 (defun gnus-request-post (method)
15267   "Post the current buffer using METHOD."
15268   (when (stringp method)
15269     (setq method (gnus-server-to-method method)))
15270   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
15271
15272 (defun gnus-request-scan (group method)
15273   "Request a SCAN being performed in GROUP from METHOD.
15274 If GROUP is nil, all groups on METHOD are scanned."
15275   (let ((method (if group (gnus-find-method-for-group group) method)))
15276     (funcall (gnus-get-function method 'request-scan)
15277              (and group (gnus-group-real-name group)) (nth 1 method))))
15278
15279 (defsubst gnus-request-update-info (info method)
15280   "Request that METHOD update INFO."
15281   (when (stringp method)
15282     (setq method (gnus-server-to-method method)))
15283   (when (gnus-check-backend-function 'request-update-info (car method))
15284     (funcall (gnus-get-function method 'request-update-info)
15285              (gnus-group-real-name (gnus-info-group info))
15286              info (nth 1 method))))
15287
15288 (defun gnus-request-expire-articles (articles group &optional force)
15289   (let ((method (gnus-find-method-for-group group)))
15290     (funcall (gnus-get-function method 'request-expire-articles)
15291              articles (gnus-group-real-name group) (nth 1 method)
15292              force)))
15293
15294 (defun gnus-request-move-article
15295   (article group server accept-function &optional last)
15296   (let ((method (gnus-find-method-for-group group)))
15297     (funcall (gnus-get-function method 'request-move-article)
15298              article (gnus-group-real-name group)
15299              (nth 1 method) accept-function last)))
15300
15301 (defun gnus-request-accept-article (group method &optional last)
15302   ;; Make sure there's a newline at the end of the article.
15303   (when (stringp method)
15304     (setq method (gnus-server-to-method method)))
15305   (when (and (not method)
15306              (stringp group))
15307     (setq method (gnus-group-name-to-method group)))
15308   (goto-char (point-max))
15309   (unless (bolp)
15310     (insert "\n"))
15311   (let ((func (car (or method (gnus-find-method-for-group group)))))
15312     (funcall (intern (format "%s-request-accept-article" func))
15313              (if (stringp group) (gnus-group-real-name group) group)
15314              (cadr method)
15315              last)))
15316
15317 (defun gnus-request-replace-article (article group buffer)
15318   (let ((func (car (gnus-find-method-for-group group))))
15319     (funcall (intern (format "%s-request-replace-article" func))
15320              article (gnus-group-real-name group) buffer)))
15321
15322 (defun gnus-request-associate-buffer (group)
15323   (let ((method (gnus-find-method-for-group group)))
15324     (funcall (gnus-get-function method 'request-associate-buffer)
15325              (gnus-group-real-name group))))
15326
15327 (defun gnus-request-restore-buffer (article group)
15328   "Request a new buffer restored to the state of ARTICLE."
15329   (let ((method (gnus-find-method-for-group group)))
15330     (funcall (gnus-get-function method 'request-restore-buffer)
15331              article (gnus-group-real-name group) (nth 1 method))))
15332
15333 (defun gnus-request-create-group (group &optional method)
15334   (when (stringp method)
15335     (setq method (gnus-server-to-method method)))
15336   (let ((method (or method (gnus-find-method-for-group group))))
15337     (funcall (gnus-get-function method 'request-create-group)
15338              (gnus-group-real-name group) (nth 1 method))))
15339
15340 (defun gnus-request-delete-group (group &optional force)
15341   (let ((method (gnus-find-method-for-group group)))
15342     (funcall (gnus-get-function method 'request-delete-group)
15343              (gnus-group-real-name group) force (nth 1 method))))
15344
15345 (defun gnus-request-rename-group (group new-name)
15346   (let ((method (gnus-find-method-for-group group)))
15347     (funcall (gnus-get-function method 'request-rename-group)
15348              (gnus-group-real-name group)
15349              (gnus-group-real-name new-name) (nth 1 method))))
15350
15351 (defun gnus-member-of-valid (symbol group)
15352   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
15353   (memq symbol (assoc
15354                 (symbol-name (car (gnus-find-method-for-group group)))
15355                 gnus-valid-select-methods)))
15356
15357 (defun gnus-method-option-p (method option)
15358   "Return non-nil if select METHOD has OPTION as a parameter."
15359   (when (stringp method)
15360     (setq method (gnus-server-to-method method)))
15361   (memq option (assoc (format "%s" (car method))
15362                       gnus-valid-select-methods)))
15363
15364 (defun gnus-server-extend-method (group method)
15365   ;; This function "extends" a virtual server.  If the server is
15366   ;; "hello", and the select method is ("hello" (my-var "something"))
15367   ;; in the group "alt.alt", this will result in a new virtual server
15368   ;; called "hello+alt.alt".
15369   (let ((entry
15370          (gnus-copy-sequence
15371           (if (equal (car method) "native") gnus-select-method
15372             (cdr (assoc (car method) gnus-server-alist))))))
15373     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
15374     (nconc entry (cdr method))))
15375
15376 (defun gnus-group-name-to-method (group)
15377   "Return a select method suitable for GROUP."
15378   (if (string-match ":" group)
15379       (let ((server (substring group 0 (match-beginning 0))))
15380         (if (string-match "\\+" server)
15381             (list (intern (substring server 0 (match-beginning 0)))
15382                   (substring server (match-end 0)))
15383           (list (intern server) "")))
15384     gnus-select-method))
15385
15386 (defun gnus-find-method-for-group (group &optional info)
15387   "Find the select method that GROUP uses."
15388   (or gnus-override-method
15389       (and (not group)
15390            gnus-select-method)
15391       (let ((info (or info (gnus-get-info group)))
15392             method)
15393         (if (or (not info)
15394                 (not (setq method (gnus-info-method info)))
15395                 (equal method "native"))
15396             gnus-select-method
15397           (setq method
15398                 (cond ((stringp method)
15399                        (gnus-server-to-method method))
15400                       ((stringp (car method))
15401                        (gnus-server-extend-method group method))
15402                       (t
15403                        method)))
15404           (cond ((equal (cadr method) "")
15405                  method)
15406                 ((null (cadr method))
15407                  (list (car method) ""))
15408                 (t
15409                  (gnus-server-add-address method)))))))
15410
15411 (defun gnus-check-backend-function (func group)
15412   "Check whether GROUP supports function FUNC."
15413   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
15414                   group)))
15415     (fboundp (intern (format "%s-%s" method func)))))
15416
15417 (defun gnus-methods-using (feature)
15418   "Find all methods that have FEATURE."
15419   (let ((valids gnus-valid-select-methods)
15420         outs)
15421     (while valids
15422       (if (memq feature (car valids))
15423           (setq outs (cons (car valids) outs)))
15424       (setq valids (cdr valids)))
15425     outs))
15426
15427 \f
15428 ;;;
15429 ;;; Active & Newsrc File Handling
15430 ;;;
15431
15432 (defun gnus-setup-news (&optional rawfile level dont-connect)
15433   "Setup news information.
15434 If RAWFILE is non-nil, the .newsrc file will also be read.
15435 If LEVEL is non-nil, the news will be set up at level LEVEL."
15436   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
15437
15438     (when init 
15439       ;; Clear some variables to re-initialize news information.
15440       (setq gnus-newsrc-alist nil
15441             gnus-active-hashtb nil)
15442       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
15443       (gnus-read-newsrc-file rawfile))
15444
15445     (when (and (not (assoc "archive" gnus-server-alist))
15446                gnus-message-archive-method)
15447       (push (cons "archive" gnus-message-archive-method)
15448             gnus-server-alist))
15449
15450     ;; If we don't read the complete active file, we fill in the
15451     ;; hashtb here.
15452     (if (or (null gnus-read-active-file)
15453             (eq gnus-read-active-file 'some))
15454         (gnus-update-active-hashtb-from-killed))
15455
15456     ;; Read the active file and create `gnus-active-hashtb'.
15457     ;; If `gnus-read-active-file' is nil, then we just create an empty
15458     ;; hash table.  The partial filling out of the hash table will be
15459     ;; done in `gnus-get-unread-articles'.
15460     (and gnus-read-active-file
15461          (not level)
15462          (gnus-read-active-file))
15463
15464     (or gnus-active-hashtb
15465         (setq gnus-active-hashtb (make-vector 4095 0)))
15466
15467     ;; Initialize the cache.
15468     (when gnus-use-cache
15469       (gnus-cache-open))
15470
15471     ;; Possibly eval the dribble file.
15472     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
15473
15474     ;; Slave Gnusii should then clear the dribble buffer.
15475     (when (and init gnus-slave)
15476       (gnus-dribble-clear))
15477
15478     (gnus-update-format-specifications)
15479
15480     ;; See whether we need to read the description file.
15481     (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
15482              (not gnus-description-hashtb)
15483              (not dont-connect)
15484              gnus-read-active-file)
15485         (gnus-read-all-descriptions-files))
15486
15487     ;; Find new newsgroups and treat them.
15488     (if (and init gnus-check-new-newsgroups (not level)
15489              (gnus-check-server gnus-select-method))
15490         (gnus-find-new-newsgroups))
15491
15492     ;; We might read in new NoCeM messages here.
15493     (when (and gnus-use-nocem 
15494                (not level)
15495                (not dont-connect))
15496       (gnus-nocem-scan-groups))
15497
15498     ;; Find the number of unread articles in each non-dead group.
15499     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
15500       (gnus-get-unread-articles level))
15501
15502     (if (and init gnus-check-bogus-newsgroups
15503              gnus-read-active-file (not level)
15504              (gnus-server-opened gnus-select-method))
15505         (gnus-check-bogus-newsgroups))))
15506
15507 (defun gnus-find-new-newsgroups (&optional arg)
15508   "Search for new newsgroups and add them.
15509 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
15510 The `-n' option line from .newsrc is respected.
15511 If ARG (the prefix), use the `ask-server' method to query
15512 the server for new groups."
15513   (interactive "P")
15514   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
15515                        (null gnus-read-active-file)
15516                        (eq gnus-read-active-file 'some))
15517                    'ask-server gnus-check-new-newsgroups)))
15518     (unless (gnus-check-first-time-used)
15519       (if (or (consp check)
15520               (eq check 'ask-server))
15521           ;; Ask the server for new groups.
15522           (gnus-ask-server-for-new-groups)
15523         ;; Go through the active hashtb and look for new groups.
15524         (let ((groups 0)
15525               group new-newsgroups)
15526           (gnus-message 5 "Looking for new newsgroups...")
15527           (unless gnus-have-read-active-file
15528             (gnus-read-active-file))
15529           (setq gnus-newsrc-last-checked-date (current-time-string))
15530           (unless gnus-killed-hashtb
15531             (gnus-make-hashtable-from-killed))
15532           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
15533           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
15534           (mapatoms
15535            (lambda (sym)
15536              (if (or (null (setq group (symbol-name sym)))
15537                      (not (boundp sym))
15538                      (null (symbol-value sym))
15539                      (gnus-gethash group gnus-killed-hashtb)
15540                      (gnus-gethash group gnus-newsrc-hashtb))
15541                  ()
15542                (let ((do-sub (gnus-matches-options-n group)))
15543                  (cond
15544                   ((eq do-sub 'subscribe)
15545                    (setq groups (1+ groups))
15546                    (gnus-sethash group group gnus-killed-hashtb)
15547                    (funcall gnus-subscribe-options-newsgroup-method group))
15548                   ((eq do-sub 'ignore)
15549                    nil)
15550                   (t
15551                    (setq groups (1+ groups))
15552                    (gnus-sethash group group gnus-killed-hashtb)
15553                    (if gnus-subscribe-hierarchical-interactive
15554                        (setq new-newsgroups (cons group new-newsgroups))
15555                      (funcall gnus-subscribe-newsgroup-method group)))))))
15556            gnus-active-hashtb)
15557           (when new-newsgroups
15558             (gnus-subscribe-hierarchical-interactive new-newsgroups))
15559           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15560           (if (> groups 0)
15561               (gnus-message 6 "%d new newsgroup%s arrived."
15562                             groups (if (> groups 1) "s have" " has"))
15563             (gnus-message 6 "No new newsgroups.")))))))
15564
15565 (defun gnus-matches-options-n (group)
15566   ;; Returns `subscribe' if the group is to be unconditionally
15567   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
15568   ;; no match for the group.
15569
15570   ;; First we check the two user variables.
15571   (cond
15572    ((and gnus-options-subscribe
15573          (string-match gnus-options-subscribe group))
15574     'subscribe)
15575    ((and gnus-auto-subscribed-groups
15576          (string-match gnus-auto-subscribed-groups group))
15577     'subscribe)
15578    ((and gnus-options-not-subscribe
15579          (string-match gnus-options-not-subscribe group))
15580     'ignore)
15581    ;; Then we go through the list that was retrieved from the .newsrc
15582    ;; file.  This list has elements on the form
15583    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
15584    ;; is in the reverse order of the options line) is returned.
15585    (t
15586     (let ((regs gnus-newsrc-options-n))
15587       (while (and regs
15588                   (not (string-match (caar regs) group)))
15589         (setq regs (cdr regs)))
15590       (and regs (cdar regs))))))
15591
15592 (defun gnus-ask-server-for-new-groups ()
15593   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
15594          (methods (cons gnus-select-method
15595                         (nconc
15596                          (when gnus-message-archive-method
15597                            (list "archive"))
15598                          (append
15599                           (and (consp gnus-check-new-newsgroups)
15600                                gnus-check-new-newsgroups)
15601                           gnus-secondary-select-methods))))
15602          (groups 0)
15603          (new-date (current-time-string))
15604          group new-newsgroups got-new method hashtb
15605          gnus-override-subscribe-method)
15606     ;; Go through both primary and secondary select methods and
15607     ;; request new newsgroups.
15608     (while (setq method (gnus-server-get-method nil (pop methods)))
15609       (setq new-newsgroups nil)
15610       (setq gnus-override-subscribe-method method)
15611       (when (and (gnus-check-server method)
15612                  (gnus-request-newgroups date method))
15613         (save-excursion
15614           (setq got-new t)
15615           (setq hashtb (gnus-make-hashtable 100))
15616           (set-buffer nntp-server-buffer)
15617           ;; Enter all the new groups into a hashtable.
15618           (gnus-active-to-gnus-format method hashtb 'ignore))
15619         ;; Now all new groups from `method' are in `hashtb'.
15620         (mapatoms
15621          (lambda (group-sym)
15622            (if (or (null (setq group (symbol-name group-sym)))
15623                    (not (boundp group-sym))
15624                    (null (symbol-value group-sym))
15625                    (gnus-gethash group gnus-newsrc-hashtb)
15626                    (member group gnus-zombie-list)
15627                    (member group gnus-killed-list))
15628                ;; The group is already known.
15629                ()
15630              ;; Make this group active.
15631              (when (symbol-value group-sym)
15632                (gnus-set-active group (symbol-value group-sym)))
15633              ;; Check whether we want it or not.
15634              (let ((do-sub (gnus-matches-options-n group)))
15635                (cond
15636                 ((eq do-sub 'subscribe)
15637                  (incf groups)
15638                  (gnus-sethash group group gnus-killed-hashtb)
15639                  (funcall gnus-subscribe-options-newsgroup-method group))
15640                 ((eq do-sub 'ignore)
15641                  nil)
15642                 (t
15643                  (incf groups)
15644                  (gnus-sethash group group gnus-killed-hashtb)
15645                  (if gnus-subscribe-hierarchical-interactive
15646                      (push group new-newsgroups)
15647                    (funcall gnus-subscribe-newsgroup-method group)))))))
15648          hashtb))
15649       (when new-newsgroups
15650         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
15651     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15652     (when (> groups 0)
15653       (gnus-message 6 "%d new newsgroup%s arrived."
15654                     groups (if (> groups 1) "s have" " has")))
15655     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15656     got-new))
15657
15658 (defun gnus-check-first-time-used ()
15659   (if (or (> (length gnus-newsrc-alist) 1)
15660           (file-exists-p gnus-startup-file)
15661           (file-exists-p (concat gnus-startup-file ".el"))
15662           (file-exists-p (concat gnus-startup-file ".eld")))
15663       nil
15664     (gnus-message 6 "First time user; subscribing you to default groups")
15665     (unless (gnus-read-active-file-p)
15666       (gnus-read-active-file))
15667     (setq gnus-newsrc-last-checked-date (current-time-string))
15668     (let ((groups gnus-default-subscribed-newsgroups)
15669           group)
15670       (if (eq groups t)
15671           nil
15672         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15673         (mapatoms
15674          (lambda (sym)
15675            (if (null (setq group (symbol-name sym)))
15676                ()
15677              (let ((do-sub (gnus-matches-options-n group)))
15678                (cond
15679                 ((eq do-sub 'subscribe)
15680                  (gnus-sethash group group gnus-killed-hashtb)
15681                  (funcall gnus-subscribe-options-newsgroup-method group))
15682                 ((eq do-sub 'ignore)
15683                  nil)
15684                 (t
15685                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15686          gnus-active-hashtb)
15687         (while groups
15688           (if (gnus-active (car groups))
15689               (gnus-group-change-level
15690                (car groups) gnus-level-default-subscribed gnus-level-killed))
15691           (setq groups (cdr groups)))
15692         (gnus-group-make-help-group)
15693         (and gnus-novice-user
15694              (gnus-message 7 "`A k' to list killed groups"))))))
15695
15696 (defun gnus-subscribe-group (group previous &optional method)
15697   (gnus-group-change-level
15698    (if method
15699        (list t group gnus-level-default-subscribed nil nil method)
15700      group)
15701    gnus-level-default-subscribed gnus-level-killed previous t))
15702
15703 ;; `gnus-group-change-level' is the fundamental function for changing
15704 ;; subscription levels of newsgroups.  This might mean just changing
15705 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15706 ;; again, which subscribes/unsubscribes a group, which is equally
15707 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15708 ;; from 8-9 to 1-7 means that you remove the group from the list of
15709 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15710 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15711 ;; which is trivial.
15712 ;; ENTRY can either be a string (newsgroup name) or a list (if
15713 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15714 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15715 ;; entries.
15716 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15717 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15718 ;; after.
15719 (defun gnus-group-change-level (entry level &optional oldlevel
15720                                       previous fromkilled)
15721   (let (group info active num)
15722     ;; Glean what info we can from the arguments
15723     (if (consp entry)
15724         (if fromkilled (setq group (nth 1 entry))
15725           (setq group (car (nth 2 entry))))
15726       (setq group entry))
15727     (if (and (stringp entry)
15728              oldlevel
15729              (< oldlevel gnus-level-zombie))
15730         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15731     (if (and (not oldlevel)
15732              (consp entry))
15733         (setq oldlevel (gnus-info-level (nth 2 entry)))
15734       (setq oldlevel (or oldlevel 9)))
15735     (if (stringp previous)
15736         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15737
15738     (if (and (>= oldlevel gnus-level-zombie)
15739              (gnus-gethash group gnus-newsrc-hashtb))
15740         ;; We are trying to subscribe a group that is already
15741         ;; subscribed.
15742         ()                              ; Do nothing.
15743
15744       (or (gnus-ephemeral-group-p group)
15745           (gnus-dribble-enter
15746            (format "(gnus-group-change-level %S %S %S %S %S)"
15747                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15748
15749       ;; Then we remove the newgroup from any old structures, if needed.
15750       ;; If the group was killed, we remove it from the killed or zombie
15751       ;; list.  If not, and it is in fact going to be killed, we remove
15752       ;; it from the newsrc hash table and assoc.
15753       (cond
15754        ((>= oldlevel gnus-level-zombie)
15755         (if (= oldlevel gnus-level-zombie)
15756             (setq gnus-zombie-list (delete group gnus-zombie-list))
15757           (setq gnus-killed-list (delete group gnus-killed-list))))
15758        (t
15759         (if (and (>= level gnus-level-zombie)
15760                  entry)
15761             (progn
15762               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15763               (if (nth 3 entry)
15764                   (setcdr (gnus-gethash (car (nth 3 entry))
15765                                         gnus-newsrc-hashtb)
15766                           (cdr entry)))
15767               (setcdr (cdr entry) (cdddr entry))))))
15768
15769       ;; Finally we enter (if needed) the list where it is supposed to
15770       ;; go, and change the subscription level.  If it is to be killed,
15771       ;; we enter it into the killed or zombie list.
15772       (cond 
15773        ((>= level gnus-level-zombie)
15774         ;; Remove from the hash table.
15775         (gnus-sethash group nil gnus-newsrc-hashtb)
15776         ;; We do not enter foreign groups into the list of dead
15777         ;; groups.
15778         (unless (gnus-group-foreign-p group)
15779           (if (= level gnus-level-zombie)
15780               (setq gnus-zombie-list (cons group gnus-zombie-list))
15781             (setq gnus-killed-list (cons group gnus-killed-list)))))
15782        (t
15783         ;; If the list is to be entered into the newsrc assoc, and
15784         ;; it was killed, we have to create an entry in the newsrc
15785         ;; hashtb format and fix the pointers in the newsrc assoc.
15786         (if (< oldlevel gnus-level-zombie)
15787             ;; It was alive, and it is going to stay alive, so we
15788             ;; just change the level and don't change any pointers or
15789             ;; hash table entries.
15790             (setcar (cdaddr entry) level)
15791           (if (listp entry)
15792               (setq info (cdr entry)
15793                     num (car entry))
15794             (setq active (gnus-active group))
15795             (setq num
15796                   (if active (- (1+ (cdr active)) (car active)) t))
15797             ;; Check whether the group is foreign.  If so, the
15798             ;; foreign select method has to be entered into the
15799             ;; info.
15800             (let ((method (or gnus-override-subscribe-method
15801                               (gnus-group-method group))))
15802               (if (eq method gnus-select-method)
15803                   (setq info (list group level nil))
15804                 (setq info (list group level nil nil method)))))
15805           (unless previous
15806             (setq previous
15807                   (let ((p gnus-newsrc-alist))
15808                     (while (cddr p)
15809                       (setq p (cdr p)))
15810                     p)))
15811           (setq entry (cons info (cddr previous)))
15812           (if (cdr previous)
15813               (progn
15814                 (setcdr (cdr previous) entry)
15815                 (gnus-sethash group (cons num (cdr previous))
15816                               gnus-newsrc-hashtb))
15817             (setcdr previous entry)
15818             (gnus-sethash group (cons num previous)
15819                           gnus-newsrc-hashtb))
15820           (when (cdr entry)
15821             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15822       (when gnus-group-change-level-function
15823         (funcall gnus-group-change-level-function group level oldlevel)))))
15824
15825 (defun gnus-kill-newsgroup (newsgroup)
15826   "Obsolete function.  Kills a newsgroup."
15827   (gnus-group-change-level
15828    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15829
15830 (defun gnus-check-bogus-newsgroups (&optional confirm)
15831   "Remove bogus newsgroups.
15832 If CONFIRM is non-nil, the user has to confirm the deletion of every
15833 newsgroup."
15834   (let ((newsrc (cdr gnus-newsrc-alist))
15835         bogus group entry info)
15836     (gnus-message 5 "Checking bogus newsgroups...")
15837     (unless (gnus-read-active-file-p)
15838       (gnus-read-active-file))
15839     (when (gnus-read-active-file-p)
15840       ;; Find all bogus newsgroup that are subscribed.
15841       (while newsrc
15842         (setq info (pop newsrc)
15843               group (gnus-info-group info))
15844         (unless (or (gnus-active group) ; Active
15845                     (gnus-info-method info) ; Foreign
15846                     (and confirm
15847                          (not (gnus-y-or-n-p
15848                                (format "Remove bogus newsgroup: %s " group)))))
15849           ;; Found a bogus newsgroup.
15850           (push group bogus)))
15851       ;; Remove all bogus subscribed groups by first killing them, and
15852       ;; then removing them from the list of killed groups.
15853       (while bogus
15854         (when (setq entry (gnus-gethash (setq group (pop bogus))
15855                                         gnus-newsrc-hashtb))
15856           (gnus-group-change-level entry gnus-level-killed)
15857           (setq gnus-killed-list (delete group gnus-killed-list))))
15858       ;; Then we remove all bogus groups from the list of killed and
15859       ;; zombie groups.  They are are removed without confirmation.
15860       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15861             killed)
15862         (while dead-lists
15863           (setq killed (symbol-value (car dead-lists)))
15864           (while killed
15865             (unless (gnus-active (setq group (pop killed)))
15866               ;; The group is bogus.
15867               ;; !!!Slow as hell.
15868               (set (car dead-lists)
15869                    (delete group (symbol-value (car dead-lists))))))
15870           (setq dead-lists (cdr dead-lists))))
15871       (gnus-message 5 "Checking bogus newsgroups...done"))))
15872
15873 (defun gnus-check-duplicate-killed-groups ()
15874   "Remove duplicates from the list of killed groups."
15875   (interactive)
15876   (let ((killed gnus-killed-list))
15877     (while killed
15878       (gnus-message 9 "%d" (length killed))
15879       (setcdr killed (delete (car killed) (cdr killed)))
15880       (setq killed (cdr killed)))))
15881
15882 ;; We want to inline a function from gnus-cache, so we cheat here:
15883 (eval-when-compile
15884   (provide 'gnus)
15885   (require 'gnus-cache))
15886
15887 (defun gnus-get-unread-articles-in-group (info active &optional update)
15888   (when active
15889     ;; Allow the backend to update the info in the group.
15890     (when (and update 
15891                (gnus-request-update-info
15892                 info (gnus-find-method-for-group (gnus-info-group info))))
15893       (gnus-activate-group (gnus-info-group info) nil t))
15894     (let* ((range (gnus-info-read info))
15895            (num 0))
15896       ;; If a cache is present, we may have to alter the active info.
15897       (when (and gnus-use-cache info)
15898         (inline (gnus-cache-possibly-alter-active 
15899                  (gnus-info-group info) active)))
15900       ;; Modify the list of read articles according to what articles
15901       ;; are available; then tally the unread articles and add the
15902       ;; number to the group hash table entry.
15903       (cond
15904        ((zerop (cdr active))
15905         (setq num 0))
15906        ((not range)
15907         (setq num (- (1+ (cdr active)) (car active))))
15908        ((not (listp (cdr range)))
15909         ;; Fix a single (num . num) range according to the
15910         ;; active hash table.
15911         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15912         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15913         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15914         ;; Compute number of unread articles.
15915         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15916        (t
15917         ;; The read list is a list of ranges.  Fix them according to
15918         ;; the active hash table.
15919         ;; First peel off any elements that are below the lower
15920         ;; active limit.
15921         (while (and (cdr range)
15922                     (>= (car active)
15923                         (or (and (atom (cadr range)) (cadr range))
15924                             (caadr range))))
15925           (if (numberp (car range))
15926               (setcar range
15927                       (cons (car range)
15928                             (or (and (numberp (cadr range))
15929                                      (cadr range))
15930                                 (cdadr range))))
15931             (setcdr (car range)
15932                     (or (and (numberp (nth 1 range)) (nth 1 range))
15933                         (cdadr range))))
15934           (setcdr range (cddr range)))
15935         ;; Adjust the first element to be the same as the lower limit.
15936         (if (and (not (atom (car range)))
15937                  (< (cdar range) (car active)))
15938             (setcdr (car range) (1- (car active))))
15939         ;; Then we want to peel off any elements that are higher
15940         ;; than the upper active limit.
15941         (let ((srange range))
15942           ;; Go past all legal elements.
15943           (while (and (cdr srange)
15944                       (<= (or (and (atom (cadr srange))
15945                                    (cadr srange))
15946                               (caadr srange)) (cdr active)))
15947             (setq srange (cdr srange)))
15948           (if (cdr srange)
15949               ;; Nuke all remaining illegal elements.
15950               (setcdr srange nil))
15951
15952           ;; Adjust the final element.
15953           (if (and (not (atom (car srange)))
15954                    (> (cdar srange) (cdr active)))
15955               (setcdr (car srange) (cdr active))))
15956         ;; Compute the number of unread articles.
15957         (while range
15958           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15959                                       (cdar range)))
15960                               (or (and (atom (car range)) (car range))
15961                                   (caar range)))))
15962           (setq range (cdr range)))
15963         (setq num (max 0 (- (cdr active) num)))))
15964       ;; Set the number of unread articles.
15965       (when info
15966         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
15967       num)))
15968
15969 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15970 ;; and compute how many unread articles there are in each group.
15971 (defun gnus-get-unread-articles (&optional level)
15972   (let* ((newsrc (cdr gnus-newsrc-alist))
15973          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15974          (foreign-level
15975           (min
15976            (cond ((and gnus-activate-foreign-newsgroups
15977                        (not (numberp gnus-activate-foreign-newsgroups)))
15978                   (1+ gnus-level-subscribed))
15979                  ((numberp gnus-activate-foreign-newsgroups)
15980                   gnus-activate-foreign-newsgroups)
15981                  (t 0))
15982            level))
15983          info group active method)
15984     (gnus-message 5 "Checking new news...")
15985
15986     (while newsrc
15987       (setq active (gnus-active (setq group (gnus-info-group
15988                                              (setq info (pop newsrc))))))
15989
15990       ;; Check newsgroups.  If the user doesn't want to check them, or
15991       ;; they can't be checked (for instance, if the news server can't
15992       ;; be reached) we just set the number of unread articles in this
15993       ;; newsgroup to t.  This means that Gnus thinks that there are
15994       ;; unread articles, but it has no idea how many.
15995       (if (and (setq method (gnus-info-method info))
15996                (not (gnus-server-equal
15997                      gnus-select-method
15998                      (setq method (gnus-server-get-method nil method))))
15999                (not (gnus-secondary-method-p method)))
16000           ;; These groups are foreign.  Check the level.
16001           (when (<= (gnus-info-level info) foreign-level)
16002             (setq active (gnus-activate-group group 'scan))
16003             (unless (inline (gnus-virtual-group-p group))
16004               (inline (gnus-close-group group)))
16005             (when (fboundp (intern (concat (symbol-name (car method))
16006                                            "-request-update-info")))
16007               (inline (gnus-request-update-info info method))))
16008         ;; These groups are native or secondary.
16009         (when (and (<= (gnus-info-level info) level)
16010                    (not gnus-read-active-file))
16011           (setq active (gnus-activate-group group 'scan))
16012           (inline (gnus-close-group group))))
16013
16014       ;; Get the number of unread articles in the group.
16015       (if active
16016           (inline (gnus-get-unread-articles-in-group info active))
16017         ;; The group couldn't be reached, so we nix out the number of
16018         ;; unread articles and stuff.
16019         (gnus-set-active group nil)
16020         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
16021
16022     (gnus-message 5 "Checking new news...done")))
16023
16024 ;; Create a hash table out of the newsrc alist.  The `car's of the
16025 ;; alist elements are used as keys.
16026 (defun gnus-make-hashtable-from-newsrc-alist ()
16027   (let ((alist gnus-newsrc-alist)
16028         (ohashtb gnus-newsrc-hashtb)
16029         prev)
16030     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
16031     (setq alist
16032           (setq prev (setq gnus-newsrc-alist
16033                            (if (equal (caar gnus-newsrc-alist)
16034                                       "dummy.group")
16035                                gnus-newsrc-alist
16036                              (cons (list "dummy.group" 0 nil) alist)))))
16037     (while alist
16038       (gnus-sethash
16039        (caar alist)
16040        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
16041              prev)
16042        gnus-newsrc-hashtb)
16043       (setq prev alist
16044             alist (cdr alist)))))
16045
16046 (defun gnus-make-hashtable-from-killed ()
16047   "Create a hash table from the killed and zombie lists."
16048   (let ((lists '(gnus-killed-list gnus-zombie-list))
16049         list)
16050     (setq gnus-killed-hashtb
16051           (gnus-make-hashtable
16052            (+ (length gnus-killed-list) (length gnus-zombie-list))))
16053     (while (setq list (pop lists))
16054       (setq list (symbol-value list))
16055       (while list
16056         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
16057
16058 (defun gnus-activate-group (group &optional scan dont-check method)
16059   ;; Check whether a group has been activated or not.
16060   ;; If SCAN, request a scan of that group as well.
16061   (let ((method (or method (gnus-find-method-for-group group)))
16062         active)
16063     (and (gnus-check-server method)
16064          ;; We escape all bugs and quit here to make it possible to
16065          ;; continue if a group is so out-there that it reports bugs
16066          ;; and stuff.
16067          (progn
16068            (and scan
16069                 (gnus-check-backend-function 'request-scan (car method))
16070                 (gnus-request-scan group method))
16071            t)
16072          (condition-case ()
16073              (gnus-request-group group dont-check method)
16074         ;   (error nil)
16075            (quit nil))
16076          (save-excursion
16077            (set-buffer nntp-server-buffer)
16078            (goto-char (point-min))
16079            ;; Parse the result we got from `gnus-request-group'.
16080            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
16081                 (progn
16082                   (goto-char (match-beginning 1))
16083                   (gnus-set-active
16084                    group (setq active (cons (read (current-buffer))
16085                                             (read (current-buffer)))))
16086                   ;; Return the new active info.
16087                   active))))))
16088
16089 (defun gnus-update-read-articles (group unread)
16090   "Update the list of read and ticked articles in GROUP using the
16091 UNREAD and TICKED lists.
16092 Note: UNSELECTED has to be sorted over `<'.
16093 Returns whether the updating was successful."
16094   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
16095          (entry (gnus-gethash group gnus-newsrc-hashtb))
16096          (info (nth 2 entry))
16097          (prev 1)
16098          (unread (sort (copy-sequence unread) '<))
16099          read)
16100     (if (or (not info) (not active))
16101         ;; There is no info on this group if it was, in fact,
16102         ;; killed.  Gnus stores no information on killed groups, so
16103         ;; there's nothing to be done.
16104         ;; One could store the information somewhere temporarily,
16105         ;; perhaps...  Hmmm...
16106         ()
16107       ;; Remove any negative articles numbers.
16108       (while (and unread (< (car unread) 0))
16109         (setq unread (cdr unread)))
16110       ;; Remove any expired article numbers
16111       (while (and unread (< (car unread) (car active)))
16112         (setq unread (cdr unread)))
16113       ;; Compute the ranges of read articles by looking at the list of
16114       ;; unread articles.
16115       (while unread
16116         (if (/= (car unread) prev)
16117             (setq read (cons (if (= prev (1- (car unread))) prev
16118                                (cons prev (1- (car unread)))) read)))
16119         (setq prev (1+ (car unread)))
16120         (setq unread (cdr unread)))
16121       (when (<= prev (cdr active))
16122         (setq read (cons (cons prev (cdr active)) read)))
16123       ;; Enter this list into the group info.
16124       (gnus-info-set-read
16125        info (if (> (length read) 1) (nreverse read) read))
16126       ;; Set the number of unread articles in gnus-newsrc-hashtb.
16127       (gnus-get-unread-articles-in-group info (gnus-active group))
16128       t)))
16129
16130 (defun gnus-make-articles-unread (group articles)
16131   "Mark ARTICLES in GROUP as unread."
16132   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
16133                           (gnus-gethash (gnus-group-real-name group)
16134                                         gnus-newsrc-hashtb))))
16135          (ranges (gnus-info-read info))
16136          news article)
16137     (while articles
16138       (when (gnus-member-of-range
16139              (setq article (pop articles)) ranges)
16140         (setq news (cons article news))))
16141     (when news
16142       (gnus-info-set-read
16143        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
16144       (gnus-group-update-group group t))))
16145
16146 ;; Enter all dead groups into the hashtb.
16147 (defun gnus-update-active-hashtb-from-killed ()
16148   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
16149         (lists (list gnus-killed-list gnus-zombie-list))
16150         killed)
16151     (while lists
16152       (setq killed (car lists))
16153       (while killed
16154         (gnus-sethash (car killed) nil hashtb)
16155         (setq killed (cdr killed)))
16156       (setq lists (cdr lists)))))
16157
16158 (defun gnus-get-killed-groups ()
16159   "Go through the active hashtb and all all unknown groups as killed."
16160   ;; First make sure active file has been read.
16161   (unless (gnus-read-active-file-p)
16162     (let ((gnus-read-active-file t))
16163       (gnus-read-active-file)))
16164   (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
16165   ;; Go through all newsgroups that are known to Gnus - enlarge kill list.
16166   (mapatoms
16167    (lambda (sym)
16168      (let ((groups 0)
16169            (group (symbol-name sym)))
16170        (if (or (null group)
16171                (gnus-gethash group gnus-killed-hashtb)
16172                (gnus-gethash group gnus-newsrc-hashtb))
16173            ()
16174          (let ((do-sub (gnus-matches-options-n group)))
16175            (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
16176                ()
16177              (setq groups (1+ groups))
16178              (setq gnus-killed-list
16179                    (cons group gnus-killed-list))
16180              (gnus-sethash group group gnus-killed-hashtb))))))
16181    gnus-active-hashtb))
16182
16183 ;; Get the active file(s) from the backend(s).
16184 (defun gnus-read-active-file ()
16185   (gnus-group-set-mode-line)
16186   (let ((methods 
16187          (append
16188           (if (gnus-check-server gnus-select-method)
16189               ;; The native server is available.
16190               (cons gnus-select-method gnus-secondary-select-methods)
16191             ;; The native server is down, so we just do the
16192             ;; secondary ones.
16193             gnus-secondary-select-methods)
16194           ;; Also read from the archive server.
16195           (when gnus-message-archive-method
16196             (list "archive"))))
16197         list-type)
16198     (setq gnus-have-read-active-file nil)
16199     (save-excursion
16200       (set-buffer nntp-server-buffer)
16201       (while methods
16202         (let* ((method (if (stringp (car methods))
16203                            (gnus-server-get-method nil (car methods))
16204                          (car methods)))
16205                (where (nth 1 method))
16206                (mesg (format "Reading active file%s via %s..."
16207                              (if (and where (not (zerop (length where))))
16208                                  (concat " from " where) "")
16209                              (car method))))
16210           (gnus-message 5 mesg)
16211           (when (gnus-check-server method)
16212             ;; Request that the backend scan its incoming messages.
16213             (and (gnus-check-backend-function 'request-scan (car method))
16214                  (gnus-request-scan nil method))
16215             (cond
16216              ((and (eq gnus-read-active-file 'some)
16217                    (gnus-check-backend-function 'retrieve-groups (car method)))
16218               (let ((newsrc (cdr gnus-newsrc-alist))
16219                     (gmethod (gnus-server-get-method nil method))
16220                     groups info)
16221                 (while (setq info (pop newsrc))
16222                   (when (gnus-server-equal
16223                          (gnus-find-method-for-group 
16224                           (gnus-info-group info) info)
16225                          gmethod)
16226                     (push (gnus-group-real-name (gnus-info-group info)) 
16227                           groups)))
16228                 (when groups
16229                   (gnus-check-server method)
16230                   (setq list-type (gnus-retrieve-groups groups method))
16231                   (cond
16232                    ((not list-type)
16233                     (gnus-error
16234                      1.2 "Cannot read partial active file from %s server."
16235                      (car method)))
16236                    ((eq list-type 'active)
16237                     (gnus-active-to-gnus-format method gnus-active-hashtb))
16238                    (t
16239                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
16240              (t
16241               (if (not (gnus-request-list method))
16242                   (unless (equal method gnus-message-archive-method)
16243                     (gnus-error 1 "Cannot read active file from %s server."
16244                                 (car method)))
16245                 (gnus-message 5 mesg)
16246                 (gnus-active-to-gnus-format method gnus-active-hashtb)
16247                 ;; We mark this active file as read.
16248                 (push method gnus-have-read-active-file)
16249                 (gnus-message 5 "%sdone" mesg))))))
16250         (setq methods (cdr methods))))))
16251
16252 ;; Read an active file and place the results in `gnus-active-hashtb'.
16253 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
16254   (unless method
16255     (setq method gnus-select-method))
16256   (let ((cur (current-buffer))
16257         (hashtb (or hashtb
16258                     (if (and gnus-active-hashtb
16259                              (not (equal method gnus-select-method)))
16260                         gnus-active-hashtb
16261                       (setq gnus-active-hashtb
16262                             (if (equal method gnus-select-method)
16263                                 (gnus-make-hashtable
16264                                  (count-lines (point-min) (point-max)))
16265                               (gnus-make-hashtable 4096)))))))
16266     ;; Delete unnecessary lines.
16267     (goto-char (point-min))
16268     (while (search-forward "\nto." nil t)
16269       (delete-region (1+ (match-beginning 0))
16270                      (progn (forward-line 1) (point))))
16271     (or (string= gnus-ignored-newsgroups "")
16272         (progn
16273           (goto-char (point-min))
16274           (delete-matching-lines gnus-ignored-newsgroups)))
16275     ;; Make the group names readable as a lisp expression even if they
16276     ;; contain special characters.
16277     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
16278     (goto-char (point-max))
16279     (while (re-search-backward "[][';?()#]" nil t)
16280       (insert ?\\))
16281     ;; If these are groups from a foreign select method, we insert the
16282     ;; group prefix in front of the group names.
16283     (and method (not (gnus-server-equal
16284                       (gnus-server-get-method nil method)
16285                       (gnus-server-get-method nil gnus-select-method)))
16286          (let ((prefix (gnus-group-prefixed-name "" method)))
16287            (goto-char (point-min))
16288            (while (and (not (eobp))
16289                        (progn (insert prefix)
16290                               (zerop (forward-line 1)))))))
16291     ;; Store the active file in a hash table.
16292     (goto-char (point-min))
16293     (if (string-match "%[oO]" gnus-group-line-format)
16294         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
16295         ;; If we want information on moderated groups, we use this
16296         ;; loop...
16297         (let* ((mod-hashtb (make-vector 7 0))
16298                (m (intern "m" mod-hashtb))
16299                group max min)
16300           (while (not (eobp))
16301             (condition-case nil
16302                 (progn
16303                   (narrow-to-region (point) (gnus-point-at-eol))
16304                   (setq group (let ((obarray hashtb)) (read cur)))
16305                   (if (and (numberp (setq max (read cur)))
16306                            (numberp (setq min (read cur)))
16307                            (progn
16308                              (skip-chars-forward " \t")
16309                              (not
16310                               (or (= (following-char) ?=)
16311                                   (= (following-char) ?x)
16312                                   (= (following-char) ?j)))))
16313                       (set group (cons min max))
16314                     (set group nil))
16315                   ;; Enter moderated groups into a list.
16316                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
16317                       (setq gnus-moderated-list
16318                             (cons (symbol-name group) gnus-moderated-list))))
16319               (error
16320                (and group
16321                     (symbolp group)
16322                     (set group nil))))
16323             (widen)
16324             (forward-line 1)))
16325       ;; And if we do not care about moderation, we use this loop,
16326       ;; which is faster.
16327       (let (group max min)
16328         (while (not (eobp))
16329           (condition-case ()
16330               (progn
16331                 (narrow-to-region (point) (gnus-point-at-eol))
16332                 ;; group gets set to a symbol interned in the hash table
16333                 ;; (what a hack!!) - jwz
16334                 (setq group (let ((obarray hashtb)) (read cur)))
16335                 (if (and (numberp (setq max (read cur)))
16336                          (numberp (setq min (read cur)))
16337                          (progn
16338                            (skip-chars-forward " \t")
16339                            (not
16340                             (or (= (following-char) ?=)
16341                                 (= (following-char) ?x)
16342                                 (= (following-char) ?j)))))
16343                     (set group (cons min max))
16344                   (set group nil)))
16345             (error
16346              (progn
16347                (and group
16348                     (symbolp group)
16349                     (set group nil))
16350                (or ignore-errors
16351                    (gnus-message 3 "Warning - illegal active: %s"
16352                                  (buffer-substring
16353                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
16354           (widen)
16355           (forward-line 1))))))
16356
16357 (defun gnus-groups-to-gnus-format (method &optional hashtb)
16358   ;; Parse a "groups" active file.
16359   (let ((cur (current-buffer))
16360         (hashtb (or hashtb
16361                     (if (and method gnus-active-hashtb)
16362                         gnus-active-hashtb
16363                       (setq gnus-active-hashtb
16364                             (gnus-make-hashtable
16365                              (count-lines (point-min) (point-max)))))))
16366         (prefix (and method
16367                      (not (gnus-server-equal
16368                            (gnus-server-get-method nil method)
16369                            (gnus-server-get-method nil gnus-select-method)))
16370                      (gnus-group-prefixed-name "" method))))
16371
16372     (goto-char (point-min))
16373     ;; We split this into to separate loops, one with the prefix
16374     ;; and one without to speed the reading up somewhat.
16375     (if prefix
16376         (let (min max opoint group)
16377           (while (not (eobp))
16378             (condition-case ()
16379                 (progn
16380                   (read cur) (read cur)
16381                   (setq min (read cur)
16382                         max (read cur)
16383                         opoint (point))
16384                   (skip-chars-forward " \t")
16385                   (insert prefix)
16386                   (goto-char opoint)
16387                   (set (let ((obarray hashtb)) (read cur))
16388                        (cons min max)))
16389               (error (and group (symbolp group) (set group nil))))
16390             (forward-line 1)))
16391       (let (min max group)
16392         (while (not (eobp))
16393           (condition-case ()
16394               (if (= (following-char) ?2)
16395                   (progn
16396                     (read cur) (read cur)
16397                     (setq min (read cur)
16398                           max (read cur))
16399                     (set (setq group (let ((obarray hashtb)) (read cur)))
16400                          (cons min max))))
16401             (error (and group (symbolp group) (set group nil))))
16402           (forward-line 1))))))
16403
16404 (defun gnus-read-newsrc-file (&optional force)
16405   "Read startup file.
16406 If FORCE is non-nil, the .newsrc file is read."
16407   ;; Reset variables that might be defined in the .newsrc.eld file.
16408   (let ((variables gnus-variable-list))
16409     (while variables
16410       (set (car variables) nil)
16411       (setq variables (cdr variables))))
16412   (let* ((newsrc-file gnus-current-startup-file)
16413          (quick-file (concat newsrc-file ".el")))
16414     (save-excursion
16415       ;; We always load the .newsrc.eld file.  If always contains
16416       ;; much information that can not be gotten from the .newsrc
16417       ;; file (ticked articles, killed groups, foreign methods, etc.)
16418       (gnus-read-newsrc-el-file quick-file)
16419
16420       (if (and (file-exists-p gnus-current-startup-file)
16421                (or force
16422                    (and (file-newer-than-file-p newsrc-file quick-file)
16423                         (file-newer-than-file-p newsrc-file
16424                                                 (concat quick-file "d")))
16425                    (not gnus-newsrc-alist)))
16426           ;; We read the .newsrc file.  Note that if there if a
16427           ;; .newsrc.eld file exists, it has already been read, and
16428           ;; the `gnus-newsrc-hashtb' has been created.  While reading
16429           ;; the .newsrc file, Gnus will only use the information it
16430           ;; can find there for changing the data already read -
16431           ;; ie. reading the .newsrc file will not trash the data
16432           ;; already read (except for read articles).
16433           (save-excursion
16434             (gnus-message 5 "Reading %s..." newsrc-file)
16435             (set-buffer (find-file-noselect newsrc-file))
16436             (buffer-disable-undo (current-buffer))
16437             (gnus-newsrc-to-gnus-format)
16438             (kill-buffer (current-buffer))
16439             (gnus-message 5 "Reading %s...done" newsrc-file)))
16440
16441       ;; Read any slave files.
16442       (unless gnus-slave
16443         (gnus-master-read-slave-newsrc))
16444       
16445       ;; Convert old to new.
16446       (gnus-convert-old-newsrc))))
16447
16448 (defun gnus-continuum-version (version)
16449   "Return VERSION as a floating point number."
16450   (when (or (string-match "^\\([^ ]+\\)? ?Gnus v?\\([0-9.]+\\)$" version)
16451             (string-match "^\\(.?\\)gnus-\\([0-9.]+\\)$" version))
16452     (let* ((alpha (and (match-beginning 1) (match-string 1 version)))
16453            (number (match-string 2 version))
16454            major minor least)
16455       (string-match "\\([0-9]\\)\\.\\([0-9]+\\)\\.?\\([0-9]+\\)?" number)
16456       (setq major (string-to-number (match-string 1 number)))
16457       (setq minor (string-to-number (match-string 2 number)))
16458       (setq least (if (match-beginning 3)
16459                       (string-to-number (match-string 3 number))
16460                     0))
16461       (string-to-number
16462        (if (zerop major)
16463            (format "%s00%02d%02d"
16464                    (cond 
16465                     ((member alpha '("(ding)" "d")) "4.99")
16466                     ((member alpha '("September" "s")) "5.01")
16467                     ((member alpha '("Red" "r")) "5.03"))
16468                    minor least)
16469          (format "%d.%02d%02d" major minor least))))))
16470
16471 (defun gnus-convert-old-newsrc ()
16472   "Convert old newsrc into the new format, if needed."
16473   (let ((fcv (and gnus-newsrc-file-version
16474                   (gnus-continuum-version gnus-newsrc-file-version))))
16475     (cond
16476      ;; No .newsrc.eld file was loaded.
16477      ((null fcv) nil)
16478      ;; Gnus 5 .newsrc.eld was loaded.
16479      ((< fcv (gnus-continuum-version "September Gnus v0.1"))
16480       (gnus-convert-old-ticks)))))
16481
16482 (defun gnus-convert-old-ticks ()
16483   (let ((newsrc (cdr gnus-newsrc-alist))
16484         marks info dormant ticked)
16485     (while (setq info (pop newsrc))
16486       (when (setq marks (gnus-info-marks info))
16487         (setq dormant (cdr (assq 'dormant marks))
16488               ticked (cdr (assq 'tick marks)))
16489         (when (or dormant ticked)
16490           (gnus-info-set-read
16491            info
16492            (gnus-add-to-range
16493             (gnus-info-read info)
16494             (nconc (gnus-uncompress-range dormant)
16495                    (gnus-uncompress-range ticked)))))))))
16496
16497 (defun gnus-read-newsrc-el-file (file)
16498   (let ((ding-file (concat file "d")))
16499     ;; We always, always read the .eld file.
16500     (gnus-message 5 "Reading %s..." ding-file)
16501     (let (gnus-newsrc-assoc)
16502       (condition-case nil
16503           (load ding-file t t t)
16504         (error
16505          (gnus-error 1 "Error in %s" ding-file)))
16506       (when gnus-newsrc-assoc
16507         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
16508     (gnus-make-hashtable-from-newsrc-alist)
16509     (when (file-newer-than-file-p file ding-file)
16510       ;; Old format quick file
16511       (gnus-message 5 "Reading %s..." file)
16512       ;; The .el file is newer than the .eld file, so we read that one
16513       ;; as well.
16514       (gnus-read-old-newsrc-el-file file))))
16515
16516 ;; Parse the old-style quick startup file
16517 (defun gnus-read-old-newsrc-el-file (file)
16518   (let (newsrc killed marked group m info)
16519     (prog1
16520         (let ((gnus-killed-assoc nil)
16521               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
16522           (prog1
16523               (condition-case nil
16524                   (load file t t t)
16525                 (error nil))
16526             (setq newsrc gnus-newsrc-assoc
16527                   killed gnus-killed-assoc
16528                   marked gnus-marked-assoc)))
16529       (setq gnus-newsrc-alist nil)
16530       (while (setq group (pop newsrc))
16531         (if (setq info (gnus-get-info (car group)))
16532             (progn
16533               (gnus-info-set-read info (cddr group))
16534               (gnus-info-set-level
16535                info (if (nth 1 group) gnus-level-default-subscribed
16536                       gnus-level-default-unsubscribed))
16537               (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
16538           (push (setq info
16539                       (list (car group)
16540                             (if (nth 1 group) gnus-level-default-subscribed
16541                               gnus-level-default-unsubscribed)
16542                             (cddr group)))
16543                 gnus-newsrc-alist))
16544         ;; Copy marks into info.
16545         (when (setq m (assoc (car group) marked))
16546           (unless (nthcdr 3 info)
16547             (nconc info (list nil)))
16548           (gnus-info-set-marks
16549            info (list (cons 'tick (gnus-compress-sequence 
16550                                    (sort (cdr m) '<) t))))))
16551       (setq newsrc killed)
16552       (while newsrc
16553         (setcar newsrc (caar newsrc))
16554         (setq newsrc (cdr newsrc)))
16555       (setq gnus-killed-list killed))
16556     ;; The .el file version of this variable does not begin with
16557     ;; "options", while the .eld version does, so we just add it if it
16558     ;; isn't there.
16559     (and
16560      gnus-newsrc-options
16561      (progn
16562        (and (not (string-match "^ *options" gnus-newsrc-options))
16563             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
16564        (and (not (string-match "\n$" gnus-newsrc-options))
16565             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
16566        ;; Finally, if we read some options lines, we parse them.
16567        (or (string= gnus-newsrc-options "")
16568            (gnus-newsrc-parse-options gnus-newsrc-options))))
16569
16570     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
16571     (gnus-make-hashtable-from-newsrc-alist)))
16572
16573 (defun gnus-make-newsrc-file (file)
16574   "Make server dependent file name by catenating FILE and server host name."
16575   (let* ((file (expand-file-name file nil))
16576          (real-file (concat file "-" (nth 1 gnus-select-method))))
16577     (if (or (file-exists-p real-file)
16578             (file-exists-p (concat real-file ".el"))
16579             (file-exists-p (concat real-file ".eld")))
16580         real-file file)))
16581
16582 (defun gnus-newsrc-to-gnus-format ()
16583   (setq gnus-newsrc-options "")
16584   (setq gnus-newsrc-options-n nil)
16585
16586   (or gnus-active-hashtb
16587       (setq gnus-active-hashtb (make-vector 4095 0)))
16588   (let ((buf (current-buffer))
16589         (already-read (> (length gnus-newsrc-alist) 1))
16590         group subscribed options-symbol newsrc Options-symbol
16591         symbol reads num1)
16592     (goto-char (point-min))
16593     ;; We intern the symbol `options' in the active hashtb so that we
16594     ;; can `eq' against it later.
16595     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
16596     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
16597
16598     (while (not (eobp))
16599       ;; We first read the first word on the line by narrowing and
16600       ;; then reading into `gnus-active-hashtb'.  Most groups will
16601       ;; already exist in that hashtb, so this will save some string
16602       ;; space.
16603       (narrow-to-region
16604        (point)
16605        (progn (skip-chars-forward "^ \t!:\n") (point)))
16606       (goto-char (point-min))
16607       (setq symbol
16608             (and (/= (point-min) (point-max))
16609                  (let ((obarray gnus-active-hashtb)) (read buf))))
16610       (widen)
16611       ;; Now, the symbol we have read is either `options' or a group
16612       ;; name.  If it is an options line, we just add it to a string.
16613       (cond
16614        ((or (eq symbol options-symbol)
16615             (eq symbol Options-symbol))
16616         (setq gnus-newsrc-options
16617               ;; This concating is quite inefficient, but since our
16618               ;; thorough studies show that approx 99.37% of all
16619               ;; .newsrc files only contain a single options line, we
16620               ;; don't give a damn, frankly, my dear.
16621               (concat gnus-newsrc-options
16622                       (buffer-substring
16623                        (gnus-point-at-bol)
16624                        ;; Options may continue on the next line.
16625                        (or (and (re-search-forward "^[^ \t]" nil 'move)
16626                                 (progn (beginning-of-line) (point)))
16627                            (point)))))
16628         (forward-line -1))
16629        (symbol
16630         ;; Group names can be just numbers.  
16631         (when (numberp symbol) 
16632           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
16633         (or (boundp symbol) (set symbol nil))
16634         ;; It was a group name.
16635         (setq subscribed (= (following-char) ?:)
16636               group (symbol-name symbol)
16637               reads nil)
16638         (if (eolp)
16639             ;; If the line ends here, this is clearly a buggy line, so
16640             ;; we put point a the beginning of line and let the cond
16641             ;; below do the error handling.
16642             (beginning-of-line)
16643           ;; We skip to the beginning of the ranges.
16644           (skip-chars-forward "!: \t"))
16645         ;; We are now at the beginning of the list of read articles.
16646         ;; We read them range by range.
16647         (while
16648             (cond
16649              ((looking-at "[0-9]+")
16650               ;; We narrow and read a number instead of buffer-substring/
16651               ;; string-to-int because it's faster.  narrow/widen is
16652               ;; faster than save-restriction/narrow, and save-restriction
16653               ;; produces a garbage object.
16654               (setq num1 (progn
16655                            (narrow-to-region (match-beginning 0) (match-end 0))
16656                            (read buf)))
16657               (widen)
16658               ;; If the next character is a dash, then this is a range.
16659               (if (= (following-char) ?-)
16660                   (progn
16661                     ;; We read the upper bound of the range.
16662                     (forward-char 1)
16663                     (if (not (looking-at "[0-9]+"))
16664                         ;; This is a buggy line, by we pretend that
16665                         ;; it's kinda OK.  Perhaps the user should be
16666                         ;; dinged?
16667                         (setq reads (cons num1 reads))
16668                       (setq reads
16669                             (cons
16670                              (cons num1
16671                                    (progn
16672                                      (narrow-to-region (match-beginning 0)
16673                                                        (match-end 0))
16674                                      (read buf)))
16675                              reads))
16676                       (widen)))
16677                 ;; It was just a simple number, so we add it to the
16678                 ;; list of ranges.
16679                 (setq reads (cons num1 reads)))
16680               ;; If the next char in ?\n, then we have reached the end
16681               ;; of the line and return nil.
16682               (/= (following-char) ?\n))
16683              ((= (following-char) ?\n)
16684               ;; End of line, so we end.
16685               nil)
16686              (t
16687               ;; Not numbers and not eol, so this might be a buggy
16688               ;; line...
16689               (or (eobp)
16690                   ;; If it was eob instead of ?\n, we allow it.
16691                   (progn
16692                     ;; The line was buggy.
16693                     (setq group nil)
16694                     (gnus-error 3.1 "Mangled line: %s"
16695                                 (buffer-substring (gnus-point-at-bol)
16696                                                   (gnus-point-at-eol)))))
16697               nil))
16698           ;; Skip past ", ".  Spaces are illegal in these ranges, but
16699           ;; we allow them, because it's a common mistake to put a
16700           ;; space after the comma.
16701           (skip-chars-forward ", "))
16702
16703         ;; We have already read .newsrc.eld, so we gently update the
16704         ;; data in the hash table with the information we have just
16705         ;; read.
16706         (when group
16707           (let ((info (gnus-get-info group))
16708                 level)
16709             (if info
16710                 ;; There is an entry for this file in the alist.
16711                 (progn
16712                   (gnus-info-set-read info (nreverse reads))
16713                   ;; We update the level very gently.  In fact, we
16714                   ;; only change it if there's been a status change
16715                   ;; from subscribed to unsubscribed, or vice versa.
16716                   (setq level (gnus-info-level info))
16717                   (cond ((and (<= level gnus-level-subscribed)
16718                               (not subscribed))
16719                          (setq level (if reads
16720                                          gnus-level-default-unsubscribed
16721                                        (1+ gnus-level-default-unsubscribed))))
16722                         ((and (> level gnus-level-subscribed) subscribed)
16723                          (setq level gnus-level-default-subscribed)))
16724                   (gnus-info-set-level info level))
16725               ;; This is a new group.
16726               (setq info (list group
16727                                (if subscribed
16728                                    gnus-level-default-subscribed
16729                                  (if reads
16730                                      (1+ gnus-level-subscribed)
16731                                    gnus-level-default-unsubscribed))
16732                                (nreverse reads))))
16733             (setq newsrc (cons info newsrc))))))
16734       (forward-line 1))
16735
16736     (setq newsrc (nreverse newsrc))
16737
16738     (if (not already-read)
16739         ()
16740       ;; We now have two newsrc lists - `newsrc', which is what we
16741       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16742       ;; what we've read from .newsrc.eld.  We have to merge these
16743       ;; lists.  We do this by "attaching" any (foreign) groups in the
16744       ;; gnus-newsrc-alist to the (native) group that precedes them.
16745       (let ((rc (cdr gnus-newsrc-alist))
16746             (prev gnus-newsrc-alist)
16747             entry mentry)
16748         (while rc
16749           (or (null (nth 4 (car rc)))   ; It's a native group.
16750               (assoc (caar rc) newsrc) ; It's already in the alist.
16751               (if (setq entry (assoc (caar prev) newsrc))
16752                   (setcdr (setq mentry (memq entry newsrc))
16753                           (cons (car rc) (cdr mentry)))
16754                 (setq newsrc (cons (car rc) newsrc))))
16755           (setq prev rc
16756                 rc (cdr rc)))))
16757
16758     (setq gnus-newsrc-alist newsrc)
16759     ;; We make the newsrc hashtb.
16760     (gnus-make-hashtable-from-newsrc-alist)
16761
16762     ;; Finally, if we read some options lines, we parse them.
16763     (or (string= gnus-newsrc-options "")
16764         (gnus-newsrc-parse-options gnus-newsrc-options))))
16765
16766 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16767 ;; The return value will be a list on the form
16768 ;; ((regexp1 . ignore)
16769 ;;  (regexp2 . subscribe)...)
16770 ;; When handling new newsgroups, groups that match a `ignore' regexp
16771 ;; will be ignored, and groups that match a `subscribe' regexp will be
16772 ;; subscribed.  A line like
16773 ;; options -n !all rec.all
16774 ;; will lead to a list that looks like
16775 ;; (("^rec\\..+" . subscribe)
16776 ;;  ("^.+" . ignore))
16777 ;; So all "rec.*" groups will be subscribed, while all the other
16778 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16779 ;; different from "options -n rec.all !all".
16780 (defun gnus-newsrc-parse-options (options)
16781   (let (out eol)
16782     (save-excursion
16783       (gnus-set-work-buffer)
16784       (insert (regexp-quote options))
16785       ;; First we treat all continuation lines.
16786       (goto-char (point-min))
16787       (while (re-search-forward "\n[ \t]+" nil t)
16788         (replace-match " " t t))
16789       ;; Then we transform all "all"s into ".+"s.
16790       (goto-char (point-min))
16791       (while (re-search-forward "\\ball\\b" nil t)
16792         (replace-match ".+" t t))
16793       (goto-char (point-min))
16794       ;; We remove all other options than the "-n" ones.
16795       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16796         (replace-match " ")
16797         (forward-char -1))
16798       (goto-char (point-min))
16799
16800       ;; We are only interested in "options -n" lines - we
16801       ;; ignore the other option lines.
16802       (while (re-search-forward "[ \t]-n" nil t)
16803         (setq eol
16804               (or (save-excursion
16805                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16806                          (- (point) 2)))
16807                   (gnus-point-at-eol)))
16808         ;; Search for all "words"...
16809         (while (re-search-forward "[^ \t,\n]+" eol t)
16810           (if (= (char-after (match-beginning 0)) ?!)
16811               ;; If the word begins with a bang (!), this is a "not"
16812               ;; spec.  We put this spec (minus the bang) and the
16813               ;; symbol `ignore' into the list.
16814               (setq out (cons (cons (concat
16815                                      "^" (buffer-substring
16816                                           (1+ (match-beginning 0))
16817                                           (match-end 0)))
16818                                     'ignore) out))
16819             ;; There was no bang, so this is a "yes" spec.
16820             (setq out (cons (cons (concat "^" (match-string 0))
16821                                   'subscribe) out)))))
16822
16823       (setq gnus-newsrc-options-n out))))
16824
16825 (defun gnus-save-newsrc-file (&optional force)
16826   "Save .newsrc file."
16827   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16828   ;; from the variable gnus-newsrc-alist.
16829   (when (and (or gnus-newsrc-alist gnus-killed-list)
16830              gnus-current-startup-file)
16831     (save-excursion
16832       (if (and (or gnus-use-dribble-file gnus-slave)
16833                (not force)
16834                (or (not gnus-dribble-buffer)
16835                    (not (buffer-name gnus-dribble-buffer))
16836                    (zerop (save-excursion
16837                             (set-buffer gnus-dribble-buffer)
16838                             (buffer-size)))))
16839           (gnus-message 4 "(No changes need to be saved)")
16840         (run-hooks 'gnus-save-newsrc-hook)
16841         (if gnus-slave
16842             (gnus-slave-save-newsrc)
16843           ;; Save .newsrc.
16844           (when gnus-save-newsrc-file
16845             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16846             (gnus-gnus-to-newsrc-format)
16847             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16848           ;; Save .newsrc.eld.
16849           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16850           (make-local-variable 'version-control)
16851           (setq version-control 'never)
16852           (setq buffer-file-name
16853                 (concat gnus-current-startup-file ".eld"))
16854           (setq default-directory (file-name-directory buffer-file-name))
16855           (gnus-add-current-to-buffer-list)
16856           (buffer-disable-undo (current-buffer))
16857           (erase-buffer)
16858           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16859           (gnus-gnus-to-quick-newsrc-format)
16860           (run-hooks 'gnus-save-quick-newsrc-hook)
16861           (save-buffer)
16862           (kill-buffer (current-buffer))
16863           (gnus-message
16864            5 "Saving %s.eld...done" gnus-current-startup-file))
16865         (gnus-dribble-delete-file)
16866         (gnus-group-set-mode-line)))))
16867
16868 (defun gnus-gnus-to-quick-newsrc-format ()
16869   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16870   (insert ";; Gnus startup file.\n")
16871   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16872   (insert ";; to read .newsrc.\n")
16873   (insert "(setq gnus-newsrc-file-version "
16874           (prin1-to-string gnus-version) ")\n")
16875   (let ((variables
16876          (if gnus-save-killed-list gnus-variable-list
16877            ;; Remove the `gnus-killed-list' from the list of variables
16878            ;; to be saved, if required.
16879            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16880         ;; Peel off the "dummy" group.
16881         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16882         variable)
16883     ;; Insert the variables into the file.
16884     (while variables
16885       (when (and (boundp (setq variable (pop variables)))
16886                  (symbol-value variable))
16887         (insert "(setq " (symbol-name variable) " '")
16888         (prin1 (symbol-value variable) (current-buffer))
16889         (insert ")\n")))))
16890
16891 (defun gnus-gnus-to-newsrc-format ()
16892   ;; Generate and save the .newsrc file.
16893   (save-excursion
16894     (set-buffer (create-file-buffer gnus-current-startup-file))
16895     (let ((newsrc (cdr gnus-newsrc-alist))
16896           (standard-output (current-buffer))
16897           info ranges range method)
16898       (setq buffer-file-name gnus-current-startup-file)
16899       (setq default-directory (file-name-directory buffer-file-name))
16900       (buffer-disable-undo (current-buffer))
16901       (erase-buffer)
16902       ;; Write options.
16903       (if gnus-newsrc-options (insert gnus-newsrc-options))
16904       ;; Write subscribed and unsubscribed.
16905       (while (setq info (pop newsrc))
16906         ;; Don't write foreign groups to .newsrc.
16907         (when (or (null (setq method (gnus-info-method info)))
16908                   (equal method "native")
16909                   (gnus-server-equal method gnus-select-method))
16910           (insert (gnus-info-group info)
16911                   (if (> (gnus-info-level info) gnus-level-subscribed)
16912                       "!" ":"))
16913           (when (setq ranges (gnus-info-read info))
16914             (insert " ")
16915             (if (not (listp (cdr ranges)))
16916                 (if (= (car ranges) (cdr ranges))
16917                     (princ (car ranges))
16918                   (princ (car ranges))
16919                   (insert "-")
16920                   (princ (cdr ranges)))
16921               (while (setq range (pop ranges))
16922                 (if (or (atom range) (= (car range) (cdr range)))
16923                     (princ (or (and (atom range) range) (car range)))
16924                   (princ (car range))
16925                   (insert "-")
16926                   (princ (cdr range)))
16927                 (if ranges (insert ",")))))
16928           (insert "\n")))
16929       (make-local-variable 'version-control)
16930       (setq version-control 'never)
16931       ;; It has been reported that sometime the modtime on the .newsrc
16932       ;; file seems to be off.  We really do want to overwrite it, so
16933       ;; we clear the modtime here before saving.  It's a bit odd,
16934       ;; though...
16935       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16936       ;; delete the silly thing entirely first.  but this fails to provide
16937       ;; such niceties as .newsrc~ creation.
16938       (if gnus-modtime-botch
16939           (delete-file gnus-startup-file)
16940         (clear-visited-file-modtime))
16941       (run-hooks 'gnus-save-standard-newsrc-hook)
16942       (save-buffer)
16943       (kill-buffer (current-buffer)))))
16944
16945 \f
16946 ;;;
16947 ;;; Slave functions.
16948 ;;;
16949
16950 (defun gnus-slave-save-newsrc ()
16951   (save-excursion
16952     (set-buffer gnus-dribble-buffer)
16953     (let ((slave-name
16954            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16955       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16956
16957 (defun gnus-master-read-slave-newsrc ()
16958   (let ((slave-files
16959          (directory-files
16960           (file-name-directory gnus-current-startup-file)
16961           t (concat
16962              "^" (regexp-quote
16963                   (concat
16964                    (file-name-nondirectory gnus-current-startup-file)
16965                    "-slave-")))
16966           t))
16967         file)
16968     (if (not slave-files)
16969         ()                              ; There are no slave files to read.
16970       (gnus-message 7 "Reading slave newsrcs...")
16971       (save-excursion
16972         (set-buffer (get-buffer-create " *gnus slave*"))
16973         (buffer-disable-undo (current-buffer))
16974         (setq slave-files
16975               (sort (mapcar (lambda (file)
16976                               (list (nth 5 (file-attributes file)) file))
16977                             slave-files)
16978                     (lambda (f1 f2)
16979                       (or (< (caar f1) (caar f2))
16980                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16981         (while slave-files
16982           (erase-buffer)
16983           (setq file (nth 1 (car slave-files)))
16984           (insert-file-contents file)
16985           (if (condition-case ()
16986                   (progn
16987                     (eval-buffer (current-buffer))
16988                     t)
16989                 (error
16990                  (gnus-error 3.2 "Possible error in %s" file)
16991                  nil))
16992               (or gnus-slave ; Slaves shouldn't delete these files.
16993                   (condition-case ()
16994                       (delete-file file)
16995                     (error nil))))
16996           (setq slave-files (cdr slave-files))))
16997       (gnus-message 7 "Reading slave newsrcs...done"))))
16998
16999 \f
17000 ;;;
17001 ;;; Group description.
17002 ;;;
17003
17004 (defun gnus-read-all-descriptions-files ()
17005   (let ((methods (cons gnus-select-method 
17006                        (nconc
17007                         (when gnus-message-archive-method
17008                           (list "archive"))
17009                         gnus-secondary-select-methods))))
17010     (while methods
17011       (gnus-read-descriptions-file (car methods))
17012       (setq methods (cdr methods)))
17013     t))
17014
17015 (defun gnus-read-descriptions-file (&optional method)
17016   (let ((method (or method gnus-select-method))
17017         group)
17018     (when (stringp method)
17019       (setq method (gnus-server-to-method method)))
17020     ;; We create the hashtable whether we manage to read the desc file
17021     ;; to avoid trying to re-read after a failed read.
17022     (or gnus-description-hashtb
17023         (setq gnus-description-hashtb
17024               (gnus-make-hashtable (length gnus-active-hashtb))))
17025     ;; Mark this method's desc file as read.
17026     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
17027                   gnus-description-hashtb)
17028
17029     (gnus-message 5 "Reading descriptions file via %s..." (car method))
17030     (cond
17031      ((not (gnus-check-server method))
17032       (gnus-message 1 "Couldn't open server")
17033       nil)
17034      ((not (gnus-request-list-newsgroups method))
17035       (gnus-message 1 "Couldn't read newsgroups descriptions")
17036       nil)
17037      (t
17038       (save-excursion
17039         (save-restriction
17040           (set-buffer nntp-server-buffer)
17041           (goto-char (point-min))
17042           (when (or (search-forward "\n.\n" nil t)
17043                     (goto-char (point-max)))
17044             (beginning-of-line)
17045             (narrow-to-region (point-min) (point)))
17046           ;; If these are groups from a foreign select method, we insert the
17047           ;; group prefix in front of the group names.
17048           (and method (not (gnus-server-equal
17049                             (gnus-server-get-method nil method)
17050                             (gnus-server-get-method nil gnus-select-method)))
17051                (let ((prefix (gnus-group-prefixed-name "" method)))
17052                  (goto-char (point-min))
17053                  (while (and (not (eobp))
17054                              (progn (insert prefix)
17055                                     (zerop (forward-line 1)))))))
17056           (goto-char (point-min))
17057           (while (not (eobp))
17058             ;; If we get an error, we set group to 0, which is not a
17059             ;; symbol...
17060             (setq group
17061                   (condition-case ()
17062                       (let ((obarray gnus-description-hashtb))
17063                         ;; Group is set to a symbol interned in this
17064                         ;; hash table.
17065                         (read nntp-server-buffer))
17066                     (error 0)))
17067             (skip-chars-forward " \t")
17068             ;; ...  which leads to this line being effectively ignored.
17069             (and (symbolp group)
17070                  (set group (buffer-substring
17071                              (point) (progn (end-of-line) (point)))))
17072             (forward-line 1))))
17073       (gnus-message 5 "Reading descriptions file...done")
17074       t))))
17075
17076 (defun gnus-group-get-description (group)
17077   "Get the description of a group by sending XGTITLE to the server."
17078   (when (gnus-request-group-description group)
17079     (save-excursion
17080       (set-buffer nntp-server-buffer)
17081       (goto-char (point-min))
17082       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
17083         (match-string 1)))))
17084
17085 \f
17086 ;;;
17087 ;;; Buffering of read articles.
17088 ;;;
17089
17090 (defvar gnus-backlog-buffer " *Gnus Backlog*")
17091 (defvar gnus-backlog-articles nil)
17092 (defvar gnus-backlog-hashtb nil)
17093
17094 (defun gnus-backlog-buffer ()
17095   "Return the backlog buffer."
17096   (or (get-buffer gnus-backlog-buffer)
17097       (save-excursion
17098         (set-buffer (get-buffer-create gnus-backlog-buffer))
17099         (buffer-disable-undo (current-buffer))
17100         (setq buffer-read-only t)
17101         (gnus-add-current-to-buffer-list)
17102         (get-buffer gnus-backlog-buffer))))
17103
17104 (defun gnus-backlog-setup ()
17105   "Initialize backlog variables."
17106   (unless gnus-backlog-hashtb
17107     (setq gnus-backlog-hashtb (make-vector 1023 0))))
17108
17109 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
17110
17111 (defun gnus-backlog-shutdown ()
17112   "Clear all backlog variables and buffers."
17113   (when (get-buffer gnus-backlog-buffer)
17114     (kill-buffer gnus-backlog-buffer))
17115   (setq gnus-backlog-hashtb nil
17116         gnus-backlog-articles nil))
17117
17118 (defun gnus-backlog-enter-article (group number buffer)
17119   (gnus-backlog-setup)
17120   (let ((ident (intern (concat group ":" (int-to-string number))
17121                        gnus-backlog-hashtb))
17122         b)
17123     (if (memq ident gnus-backlog-articles)
17124         () ; It's already kept.
17125       ;; Remove the oldest article, if necessary.
17126       (and (numberp gnus-keep-backlog)
17127            (>= (length gnus-backlog-articles) gnus-keep-backlog)
17128            (gnus-backlog-remove-oldest-article))
17129       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
17130       ;; Insert the new article.
17131       (save-excursion
17132         (set-buffer (gnus-backlog-buffer))
17133         (let (buffer-read-only)
17134           (goto-char (point-max))
17135           (or (bolp) (insert "\n"))
17136           (setq b (point))
17137           (insert-buffer-substring buffer)
17138           ;; Tag the beginning of the article with the ident.
17139           (gnus-put-text-property b (1+ b) 'gnus-backlog ident))))))
17140
17141 (defun gnus-backlog-remove-oldest-article ()
17142   (save-excursion
17143     (set-buffer (gnus-backlog-buffer))
17144     (goto-char (point-min))
17145     (if (zerop (buffer-size))
17146         () ; The buffer is empty.
17147       (let ((ident (get-text-property (point) 'gnus-backlog))
17148             buffer-read-only)
17149         ;; Remove the ident from the list of articles.
17150         (when ident
17151           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
17152         ;; Delete the article itself.
17153         (delete-region
17154          (point) (next-single-property-change
17155                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
17156
17157 (defun gnus-backlog-remove-article (group number)
17158   "Remove article NUMBER in GROUP from the backlog."
17159   (when (numberp number)
17160     (gnus-backlog-setup)
17161     (let ((ident (intern (concat group ":" (int-to-string number))
17162                          gnus-backlog-hashtb))
17163           beg end)
17164       (when (memq ident gnus-backlog-articles)
17165         ;; It was in the backlog.
17166         (save-excursion
17167           (set-buffer (gnus-backlog-buffer))
17168           (let (buffer-read-only)
17169             (when (setq beg (text-property-any
17170                              (point-min) (point-max) 'gnus-backlog
17171                              ident))
17172               ;; Find the end (i. e., the beginning of the next article).
17173               (setq end
17174                     (next-single-property-change
17175                      (1+ beg) 'gnus-backlog (current-buffer) (point-max)))
17176               (delete-region beg end)
17177               ;; Return success.
17178               t)))))))
17179
17180 (defun gnus-backlog-request-article (group number buffer)
17181   (when (numberp number)
17182     (gnus-backlog-setup)
17183     (let ((ident (intern (concat group ":" (int-to-string number))
17184                          gnus-backlog-hashtb))
17185           beg end)
17186       (when (memq ident gnus-backlog-articles)
17187         ;; It was in the backlog.
17188         (save-excursion
17189           (set-buffer (gnus-backlog-buffer))
17190           (if (not (setq beg (text-property-any
17191                               (point-min) (point-max) 'gnus-backlog
17192                               ident)))
17193               ;; It wasn't in the backlog after all.
17194               (ignore
17195                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
17196             ;; Find the end (i. e., the beginning of the next article).
17197             (setq end
17198                   (next-single-property-change
17199                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
17200         (let ((buffer-read-only nil))
17201           (erase-buffer)
17202           (insert-buffer-substring gnus-backlog-buffer beg end)
17203           t)))))
17204
17205 ;; Allow redefinition of Gnus functions.
17206
17207 (gnus-ems-redefine)
17208
17209 (provide 'gnus)
17210
17211 ;;; gnus.el ends here