*** 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 ;;;###autoload
41 (defvar gnus-directory (or (getenv "SAVEDIR") "~/News/")
42   "*Directory variable from which all other Gnus file variables are derived.")
43
44 ;; Site dependent variables.  These variables should be defined in
45 ;; paths.el.
46
47 (defvar gnus-default-nntp-server nil
48   "Specify a default NNTP server.
49 This variable should be defined in paths.el, and should never be set
50 by the user.
51 If you want to change servers, you should use `gnus-select-method'.
52 See the documentation to that variable.")
53
54 (defvar gnus-backup-default-subscribed-newsgroups
55   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
56   "Default default new newsgroups the first time Gnus is run.
57 Should be set in paths.el, and shouldn't be touched by the user.")
58
59 (defvar gnus-local-domain nil
60   "Local domain name without a host name.
61 The DOMAINNAME environment variable is used instead if it is defined.
62 If the `system-name' function returns the full Internet name, there is
63 no need to set this variable.")
64
65 (defvar gnus-local-organization nil
66   "String with a description of what organization (if any) the user belongs to.
67 The ORGANIZATION environment variable is used instead if it is defined.
68 If this variable contains a function, this function will be called
69 with the current newsgroup name as the argument.  The function should
70 return a string.
71
72 In any case, if the string (either in the variable, in the environment
73 variable, or returned by the function) is a file name, the contents of
74 this file will be used as the organization.")
75
76 ;; Customization variables
77
78 ;; Don't touch this variable.
79 (defvar gnus-nntp-service "nntp"
80   "*NNTP service name (\"nntp\" or 119).
81 This is an obsolete variable, which is scarcely used.  If you use an
82 nntp server for your newsgroup and want to change the port number
83 used to 899, you would say something along these lines:
84
85  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
86
87 (defvar gnus-nntpserver-file "/etc/nntpserver"
88   "*A file with only the name of the nntp server in it.")
89
90 ;; This function is used to check both the environment variable
91 ;; NNTPSERVER and the /etc/nntpserver file to see whether one can find
92 ;; an nntp server name default.
93 (defun gnus-getenv-nntpserver ()
94   (or (getenv "NNTPSERVER")
95       (and (file-readable-p gnus-nntpserver-file)
96            (save-excursion
97              (set-buffer (get-buffer-create " *gnus nntp*"))
98              (buffer-disable-undo (current-buffer))
99              (insert-file-contents gnus-nntpserver-file)
100              (let ((name (buffer-string)))
101                (prog1
102                    (if (string-match "^[ \t\n]*$" name)
103                        nil
104                      name)
105                  (kill-buffer (current-buffer))))))))
106
107 (defvar gnus-select-method
108   (nconc
109    (list 'nntp (or (condition-case ()
110                        (gnus-getenv-nntpserver)
111                      (error nil))
112                    (if (and gnus-default-nntp-server
113                             (not (string= gnus-default-nntp-server "")))
114                        gnus-default-nntp-server)
115                    (system-name)))
116    (if (or (null gnus-nntp-service)
117            (equal gnus-nntp-service "nntp"))
118        nil
119      (list gnus-nntp-service)))
120   "*Default method for selecting a newsgroup.
121 This variable should be a list, where the first element is how the
122 news is to be fetched, the second is the address.
123
124 For instance, if you want to get your news via NNTP from
125 \"flab.flab.edu\", you could say:
126
127 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
128
129 If you want to use your local spool, say:
130
131 (setq gnus-select-method (list 'nnspool (system-name)))
132
133 If you use this variable, you must set `gnus-nntp-server' to nil.
134
135 There is a lot more to know about select methods and virtual servers -
136 see the manual for details.")
137
138 (defvar gnus-message-archive-method 
139   `(nnfolder
140     "archive"
141     (nnfolder-directory ,(nnheader-concat message-directory "archive"))
142     (nnfolder-active-file 
143      ,(nnheader-concat message-directory "archive/active"))
144     (nnfolder-get-new-mail nil)
145     (nnfolder-inhibit-expiry t))
146   "*Method used for archiving messages you've sent.
147 This should be a mail method.
148
149 It's probably not a very effective to change this variable once you've
150 run Gnus once.  After doing that, you must edit this server from the
151 server buffer.")
152
153 (defvar gnus-refer-article-method nil
154   "*Preferred method for fetching an article by Message-ID.
155 If you are reading news from the local spool (with nnspool), fetching
156 articles by Message-ID is painfully slow.  By setting this method to an
157 nntp method, you might get acceptable results.
158
159 The value of this variable must be a valid select method as discussed
160 in the documentation of `gnus-select-method'.")
161
162 (defvar gnus-secondary-select-methods nil
163   "*A list of secondary methods that will be used for reading news.
164 This is a list where each element is a complete select method (see
165 `gnus-select-method').
166
167 If, for instance, you want to read your mail with the nnml backend,
168 you could set this variable:
169
170 (setq gnus-secondary-select-methods '((nnml \"\")))")
171
172 (defvar gnus-secondary-servers nil
173   "*List of NNTP servers that the user can choose between interactively.
174 To make Gnus query you for a server, you have to give `gnus' a
175 non-numeric prefix - `C-u M-x gnus', in short.")
176
177 (defvar gnus-nntp-server nil
178   "*The name of the host running the NNTP server.
179 This variable is semi-obsolete.  Use the `gnus-select-method'
180 variable instead.")
181
182 (defvar gnus-startup-file "~/.newsrc"
183   "*Your `.newsrc' file.
184 `.newsrc-SERVER' will be used instead if that exists.")
185
186 (defvar gnus-init-file "~/.gnus"
187   "*Your Gnus elisp startup file.
188 If a file with the .el or .elc suffixes exist, it will be read
189 instead.")
190
191 (defvar gnus-group-faq-directory
192   '("/ftp@mirrors.aol.com:/pub/rtfm/usenet/"
193     "/ftp@sunsite.auc.dk:/pub/usenet/"
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 address 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.20"
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   "Alist of major modes and related Info nodes.")
1744
1745 (defvar gnus-group-buffer "*Group*")
1746 (defvar gnus-summary-buffer "*Summary*")
1747 (defvar gnus-article-buffer "*Article*")
1748 (defvar gnus-server-buffer "*Server*")
1749
1750 (defvar gnus-work-buffer " *gnus work*")
1751
1752 (defvar gnus-original-article-buffer " *Original Article*")
1753 (defvar gnus-original-article nil)
1754
1755 (defvar gnus-buffer-list nil
1756   "Gnus buffers that should be killed on exit.")
1757
1758 (defvar gnus-slave nil
1759   "Whether this Gnus is a slave or not.")
1760
1761 (defvar gnus-variable-list
1762   '(gnus-newsrc-options gnus-newsrc-options-n
1763     gnus-newsrc-last-checked-date
1764     gnus-newsrc-alist gnus-server-alist
1765     gnus-killed-list gnus-zombie-list
1766     gnus-topic-topology gnus-topic-alist
1767     gnus-format-specs)
1768   "Gnus variables saved in the quick startup file.")
1769
1770 (defvar gnus-newsrc-options nil
1771   "Options line in the .newsrc file.")
1772
1773 (defvar gnus-newsrc-options-n nil
1774   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1775
1776 (defvar gnus-newsrc-last-checked-date nil
1777   "Date Gnus last asked server for new newsgroups.")
1778
1779 (defvar gnus-topic-topology nil
1780   "The complete topic hierarchy.")
1781
1782 (defvar gnus-topic-alist nil
1783   "The complete topic-group alist.")
1784
1785 (defvar gnus-newsrc-alist nil
1786   "Assoc list of read articles.
1787 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1788
1789 (defvar gnus-newsrc-hashtb nil
1790   "Hashtable of gnus-newsrc-alist.")
1791
1792 (defvar gnus-killed-list nil
1793   "List of killed newsgroups.")
1794
1795 (defvar gnus-killed-hashtb nil
1796   "Hash table equivalent of gnus-killed-list.")
1797
1798 (defvar gnus-zombie-list nil
1799   "List of almost dead newsgroups.")
1800
1801 (defvar gnus-description-hashtb nil
1802   "Descriptions of newsgroups.")
1803
1804 (defvar gnus-list-of-killed-groups nil
1805   "List of newsgroups that have recently been killed by the user.")
1806
1807 (defvar gnus-active-hashtb nil
1808   "Hashtable of active articles.")
1809
1810 (defvar gnus-moderated-list nil
1811   "List of moderated newsgroups.")
1812
1813 (defvar gnus-group-marked nil)
1814
1815 (defvar gnus-current-startup-file nil
1816   "Startup file for the current host.")
1817
1818 (defvar gnus-last-search-regexp nil
1819   "Default regexp for article search command.")
1820
1821 (defvar gnus-last-shell-command nil
1822   "Default shell command on article.")
1823
1824 (defvar gnus-current-select-method nil
1825   "The current method for selecting a newsgroup.")
1826
1827 (defvar gnus-group-list-mode nil)
1828
1829 (defvar gnus-article-internal-prepare-hook nil)
1830
1831 (defvar gnus-newsgroup-name nil)
1832 (defvar gnus-newsgroup-begin nil)
1833 (defvar gnus-newsgroup-end nil)
1834 (defvar gnus-newsgroup-last-rmail nil)
1835 (defvar gnus-newsgroup-last-mail nil)
1836 (defvar gnus-newsgroup-last-folder nil)
1837 (defvar gnus-newsgroup-last-file nil)
1838 (defvar gnus-newsgroup-auto-expire nil)
1839 (defvar gnus-newsgroup-active nil)
1840
1841 (defvar gnus-newsgroup-data nil)
1842 (defvar gnus-newsgroup-data-reverse nil)
1843 (defvar gnus-newsgroup-limit nil)
1844 (defvar gnus-newsgroup-limits nil)
1845
1846 (defvar gnus-newsgroup-unreads nil
1847   "List of unread articles in the current newsgroup.")
1848
1849 (defvar gnus-newsgroup-unselected nil
1850   "List of unselected unread articles in the current newsgroup.")
1851
1852 (defvar gnus-newsgroup-reads nil
1853   "Alist of read articles and article marks in the current newsgroup.")
1854
1855 (defvar gnus-newsgroup-expunged-tally nil)
1856
1857 (defvar gnus-newsgroup-marked nil
1858   "List of ticked articles in the current newsgroup (a subset of unread art).")
1859
1860 (defvar gnus-newsgroup-killed nil
1861   "List of ranges of articles that have been through the scoring process.")
1862
1863 (defvar gnus-newsgroup-cached nil
1864   "List of articles that come from the article cache.")
1865
1866 (defvar gnus-newsgroup-saved nil
1867   "List of articles that have been saved.")
1868
1869 (defvar gnus-newsgroup-kill-headers nil)
1870
1871 (defvar gnus-newsgroup-replied nil
1872   "List of articles that have been replied to in the current newsgroup.")
1873
1874 (defvar gnus-newsgroup-expirable nil
1875   "List of articles in the current newsgroup that can be expired.")
1876
1877 (defvar gnus-newsgroup-processable nil
1878   "List of articles in the current newsgroup that can be processed.")
1879
1880 (defvar gnus-newsgroup-bookmarks nil
1881   "List of articles in the current newsgroup that have bookmarks.")
1882
1883 (defvar gnus-newsgroup-dormant nil
1884   "List of dormant articles in the current newsgroup.")
1885
1886 (defvar gnus-newsgroup-scored nil
1887   "List of scored articles in the current newsgroup.")
1888
1889 (defvar gnus-newsgroup-headers nil
1890   "List of article headers in the current newsgroup.")
1891
1892 (defvar gnus-newsgroup-threads nil)
1893
1894 (defvar gnus-newsgroup-prepared nil
1895   "Whether the current group has been prepared properly.")
1896
1897 (defvar gnus-newsgroup-ancient nil
1898   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1899
1900 (defvar gnus-newsgroup-sparse nil)
1901
1902 (defvar gnus-current-article nil)
1903 (defvar gnus-article-current nil)
1904 (defvar gnus-current-headers nil)
1905 (defvar gnus-have-all-headers nil)
1906 (defvar gnus-last-article nil)
1907 (defvar gnus-newsgroup-history nil)
1908 (defvar gnus-current-kill-article nil)
1909
1910 ;; Save window configuration.
1911 (defvar gnus-prev-winconf nil)
1912
1913 (defvar gnus-summary-mark-positions nil)
1914 (defvar gnus-group-mark-positions nil)
1915
1916 (defvar gnus-reffed-article-number nil)
1917
1918 ;;; Let the byte-compiler know that we know about this variable.
1919 (defvar rmail-default-rmail-file)
1920
1921 (defvar gnus-cache-removable-articles nil)
1922
1923 (defvar gnus-dead-summary nil)
1924
1925 (defconst gnus-summary-local-variables
1926   '(gnus-newsgroup-name
1927     gnus-newsgroup-begin gnus-newsgroup-end
1928     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1929     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1930     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1931     gnus-newsgroup-unselected gnus-newsgroup-marked
1932     gnus-newsgroup-reads gnus-newsgroup-saved
1933     gnus-newsgroup-replied gnus-newsgroup-expirable
1934     gnus-newsgroup-processable gnus-newsgroup-killed
1935     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1936     gnus-newsgroup-headers gnus-newsgroup-threads
1937     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1938     gnus-current-article gnus-current-headers gnus-have-all-headers
1939     gnus-last-article gnus-article-internal-prepare-hook
1940     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1941     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1942     gnus-newsgroup-async gnus-thread-expunge-below
1943     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1944     (gnus-summary-mark-below . global)
1945     gnus-newsgroup-active gnus-scores-exclude-files
1946     gnus-newsgroup-history gnus-newsgroup-ancient
1947     gnus-newsgroup-sparse
1948     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1949     gnus-newsgroup-adaptive-score-file
1950     (gnus-newsgroup-expunged-tally . 0)
1951     gnus-cache-removable-articles gnus-newsgroup-cached
1952     gnus-newsgroup-data gnus-newsgroup-data-reverse
1953     gnus-newsgroup-limit gnus-newsgroup-limits)
1954   "Variables that are buffer-local to the summary buffers.")
1955
1956 (defconst gnus-bug-message
1957   "Sending a bug report to the Gnus Towers.
1958 ========================================
1959
1960 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1961 be sent to the Gnus Bug Exterminators.
1962
1963 At the bottom of the buffer you'll see lots of variable settings.
1964 Please do not delete those.  They will tell the Bug People what your
1965 environment is, so that it will be easier to locate the bugs.
1966
1967 If you have found a bug that makes Emacs go \"beep\", set
1968 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1969 and include the backtrace in your bug report.
1970
1971 Please describe the bug in annoying, painstaking detail.
1972
1973 Thank you for your help in stamping out bugs.
1974 ")
1975
1976 ;;; End of variables.
1977
1978 ;; Define some autoload functions Gnus might use.
1979 (eval-and-compile
1980
1981   ;; This little mapcar goes through the list below and marks the
1982   ;; symbols in question as autoloaded functions.
1983   (mapcar
1984    (lambda (package)
1985      (let ((interactive (nth 1 (memq ':interactive package))))
1986        (mapcar
1987         (lambda (function)
1988           (let (keymap)
1989             (when (consp function)
1990               (setq keymap (car (memq 'keymap function)))
1991               (setq function (car function)))
1992             (autoload function (car package) nil interactive keymap)))
1993         (if (eq (nth 1 package) ':interactive)
1994             (cdddr package)
1995           (cdr package)))))
1996    '(("metamail" metamail-buffer)
1997      ("info" Info-goto-node)
1998      ("hexl" hexl-hex-string-to-integer)
1999      ("pp" pp pp-to-string pp-eval-expression)
2000      ("mail-extr" mail-extract-address-components)
2001      ("nnmail" nnmail-split-fancy nnmail-article-group)
2002      ("nnvirtual" nnvirtual-catchup-group)
2003      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
2004       timezone-make-sortable-date timezone-make-time-string)
2005      ("rmailout" rmail-output)
2006      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
2007       rmail-show-message)
2008      ("gnus-soup" :interactive t
2009       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
2010       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
2011      ("nnsoup" nnsoup-pack-replies)
2012      ("gnus-scomo" :interactive t gnus-score-mode)
2013      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
2014       gnus-Folder-save-name gnus-folder-save-name)
2015      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
2016      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
2017       gnus-server-make-menu-bar gnus-article-make-menu-bar
2018       gnus-browse-make-menu-bar gnus-highlight-selected-summary
2019       gnus-summary-highlight-line gnus-carpal-setup-buffer
2020       gnus-group-highlight-line
2021       gnus-article-add-button gnus-insert-next-page-button
2022       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
2023      ("gnus-vis" :interactive t
2024       gnus-article-push-button gnus-article-press-button
2025       gnus-article-highlight gnus-article-highlight-some
2026       gnus-article-highlight-headers gnus-article-highlight-signature
2027       gnus-article-add-buttons gnus-article-add-buttons-to-head
2028       gnus-article-next-button gnus-article-prev-button)
2029      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
2030       gnus-demon-add-disconnection gnus-demon-add-handler
2031       gnus-demon-remove-handler)
2032      ("gnus-demon" :interactive t
2033       gnus-demon-init gnus-demon-cancel)
2034      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
2035       gnus-tree-open gnus-tree-close)
2036      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
2037       gnus-nocem-unwanted-article-p)
2038      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2039      ("gnus-srvr" gnus-browse-foreign-server)
2040      ("gnus-cite" :interactive t
2041       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2042       gnus-article-hide-citation gnus-article-fill-cited-article
2043       gnus-article-hide-citation-in-followups)
2044      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2045       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2046       gnus-execute gnus-expunge)
2047      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2048       gnus-cache-possibly-remove-articles gnus-cache-request-article
2049       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2050       gnus-cache-enter-remove-article gnus-cached-article-p
2051       gnus-cache-open gnus-cache-close gnus-cache-update-article)
2052      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2053       gnus-cache-remove-article)
2054      ("gnus-score" :interactive t
2055       gnus-summary-increase-score gnus-summary-lower-score
2056       gnus-score-flush-cache gnus-score-close
2057       gnus-score-raise-same-subject-and-select
2058       gnus-score-raise-same-subject gnus-score-default
2059       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2060       gnus-score-lower-same-subject gnus-score-lower-thread
2061       gnus-possibly-score-headers gnus-summary-raise-score 
2062       gnus-summary-set-score gnus-summary-current-score)
2063      ("gnus-score"
2064       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2065       gnus-current-score-file-nondirectory gnus-score-adaptive
2066       gnus-score-find-trace gnus-score-file-name)
2067      ("gnus-edit" :interactive t gnus-score-customize)
2068      ("gnus-topic" :interactive t gnus-topic-mode)
2069      ("gnus-topic" gnus-topic-remove-group)
2070      ("gnus-salt" :interactive t gnus-pick-mode gnus-binary-mode)
2071      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2072      ("gnus-uu" :interactive t
2073       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2074       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2075       gnus-uu-mark-by-regexp gnus-uu-mark-all
2076       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2077       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2078       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2079       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2080       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2081       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2082       gnus-uu-decode-binhex-view)
2083      ("gnus-msg" (gnus-summary-send-map keymap)
2084       gnus-mail-yank-original gnus-mail-send-and-exit
2085       gnus-article-mail gnus-new-mail gnus-mail-reply)
2086      ("gnus-msg" :interactive t
2087       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2088       gnus-summary-followup gnus-summary-followup-with-original
2089       gnus-summary-cancel-article gnus-summary-supersede-article
2090       gnus-post-news gnus-inews-news 
2091       gnus-summary-reply gnus-summary-reply-with-original
2092       gnus-summary-mail-forward gnus-summary-mail-other-window
2093       gnus-bug)
2094      ("gnus-picon" :interactive t gnus-article-display-picons
2095       gnus-group-display-picons gnus-picons-article-display-x-face)
2096      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2097       gnus-grouplens-mode)
2098      ("smiley" :interactive t gnus-smiley-display)
2099      ("gnus-vm" gnus-vm-mail-setup)
2100      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2101       gnus-summary-save-article-vm))))
2102
2103 \f
2104
2105 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2106 ;; If you want the cursor to go somewhere else, set these two
2107 ;; functions in some startup hook to whatever you want.
2108 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2109 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2110
2111 ;;; Various macros and substs.
2112
2113 (defun gnus-header-from (header)
2114   (mail-header-from header))
2115
2116 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2117   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2118   (let ((tempvar (make-symbol "GnusStartBufferWindow"))
2119         (w (make-symbol "w"))
2120         (buf (make-symbol "buf")))
2121     `(let* ((,tempvar (selected-window))
2122             (,buf ,buffer)
2123             (,w (get-buffer-window ,buf 'visible)))
2124        (unwind-protect
2125            (progn
2126              (if ,w
2127                  (select-window ,w)
2128                (pop-to-buffer ,buf))
2129              ,@forms)
2130          (select-window ,tempvar)))))
2131
2132 (put 'gnus-eval-in-buffer-window 'lisp-indent-function 1)
2133 (put 'gnus-eval-in-buffer-window 'lisp-indent-hook 1)
2134 (put 'gnus-eval-in-buffer-window 'edebug-form-spec '(form body))
2135
2136 (defmacro gnus-gethash (string hashtable)
2137   "Get hash value of STRING in HASHTABLE."
2138   `(symbol-value (intern-soft ,string ,hashtable)))
2139
2140 (defmacro gnus-sethash (string value hashtable)
2141   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2142   `(set (intern ,string ,hashtable) ,value))
2143
2144 (defmacro gnus-intern-safe (string hashtable)
2145   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2146   `(let ((symbol (intern ,string ,hashtable)))
2147      (or (boundp symbol)
2148          (set symbol nil))
2149      symbol))
2150
2151 (defmacro gnus-group-unread (group)
2152   "Get the currently computed number of unread articles in GROUP."
2153   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2154
2155 (defmacro gnus-group-entry (group)
2156   "Get the newsrc entry for GROUP."
2157   `(gnus-gethash ,group gnus-newsrc-hashtb))
2158
2159 (defmacro gnus-active (group)
2160   "Get active info on GROUP."
2161   `(gnus-gethash ,group gnus-active-hashtb))
2162
2163 (defmacro gnus-set-active (group active)
2164   "Set GROUP's active info."
2165   `(gnus-sethash ,group ,active gnus-active-hashtb))
2166
2167 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2168 ;;   function `substring' might cut on a middle of multi-octet
2169 ;;   character.
2170 (defun gnus-truncate-string (str width)
2171   (substring str 0 width))
2172
2173 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2174 ;; to limit the length of a string.  This function is necessary since
2175 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2176 (defsubst gnus-limit-string (str width)
2177   (if (> (length str) width)
2178       (substring str 0 width)
2179     str))
2180
2181 (defsubst gnus-simplify-subject-re (subject)
2182   "Remove \"Re:\" from subject lines."
2183   (if (string-match "^[Rr][Ee]: *" subject)
2184       (substring subject (match-end 0))
2185     subject))
2186
2187 (defsubst gnus-functionp (form)
2188   "Return non-nil if FORM is funcallable."
2189   (or (and (symbolp form) (fboundp form))
2190       (and (listp form) (eq (car form) 'lambda))))
2191
2192 (defsubst gnus-goto-char (point)
2193   (and point (goto-char point)))
2194
2195 (defmacro gnus-buffer-exists-p (buffer)
2196   `(let ((buffer ,buffer))
2197      (and buffer
2198           (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
2199                    buffer))))
2200
2201 (defmacro gnus-kill-buffer (buffer)
2202   `(let ((buf ,buffer))
2203      (if (gnus-buffer-exists-p buf)
2204          (kill-buffer buf))))
2205
2206 (defsubst gnus-point-at-bol ()
2207   "Return point at the beginning of the line."
2208   (let ((p (point)))
2209     (beginning-of-line)
2210     (prog1
2211         (point)
2212       (goto-char p))))
2213
2214 (defsubst gnus-point-at-eol ()
2215   "Return point at the end of the line."
2216   (let ((p (point)))
2217     (end-of-line)
2218     (prog1
2219         (point)
2220       (goto-char p))))
2221
2222 (defun gnus-alive-p ()
2223   "Say whether Gnus is running or not."
2224   (and gnus-group-buffer
2225        (get-buffer gnus-group-buffer)))
2226
2227 (defun gnus-delete-first (elt list)
2228   "Delete by side effect the first occurrence of ELT as a member of LIST."
2229   (if (equal (car list) elt)
2230       (cdr list)
2231     (let ((total list))
2232       (while (and (cdr list)
2233                   (not (equal (cadr list) elt)))
2234         (setq list (cdr list)))
2235       (when (cdr list)
2236         (setcdr list (cddr list)))
2237       total)))
2238
2239 ;; Delete the current line (and the next N lines.);
2240 (defmacro gnus-delete-line (&optional n)
2241   `(delete-region (progn (beginning-of-line) (point))
2242                   (progn (forward-line ,(or n 1)) (point))))
2243
2244 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2245 (defvar gnus-init-inhibit nil)
2246 (defun gnus-read-init-file (&optional inhibit-next)
2247   (if gnus-init-inhibit
2248       (setq gnus-init-inhibit nil)
2249     (setq gnus-init-inhibit inhibit-next)
2250     (and gnus-init-file
2251          (or (and (file-exists-p gnus-init-file)
2252                   ;; Don't try to load a directory.
2253                   (not (file-directory-p gnus-init-file)))
2254              (file-exists-p (concat gnus-init-file ".el"))
2255              (file-exists-p (concat gnus-init-file ".elc")))
2256          (condition-case var
2257              (load gnus-init-file nil t)
2258            (error
2259             (error "Error in %s: %s" gnus-init-file var))))))
2260
2261 ;; Info access macros.
2262
2263 (defmacro gnus-info-group (info)
2264   `(nth 0 ,info))
2265 (defmacro gnus-info-rank (info)
2266   `(nth 1 ,info))
2267 (defmacro gnus-info-read (info)
2268   `(nth 2 ,info))
2269 (defmacro gnus-info-marks (info)
2270   `(nth 3 ,info))
2271 (defmacro gnus-info-method (info)
2272   `(nth 4 ,info))
2273 (defmacro gnus-info-params (info)
2274   `(nth 5 ,info))
2275
2276 (defmacro gnus-info-level (info)
2277   `(let ((rank (gnus-info-rank ,info)))
2278      (if (consp rank)
2279          (car rank)
2280        rank)))
2281 (defmacro gnus-info-score (info)
2282   `(let ((rank (gnus-info-rank ,info)))
2283      (or (and (consp rank) (cdr rank)) 0)))
2284
2285 (defmacro gnus-info-set-group (info group)
2286   `(setcar ,info ,group))
2287 (defmacro gnus-info-set-rank (info rank)
2288   `(setcar (nthcdr 1 ,info) ,rank))
2289 (defmacro gnus-info-set-read (info read)
2290   `(setcar (nthcdr 2 ,info) ,read))
2291 (defmacro gnus-info-set-marks (info marks)
2292   `(setcar (nthcdr 3 ,info) ,marks))
2293 (defmacro gnus-info-set-method (info method)
2294   `(setcar (nthcdr 4 ,info) ,method))
2295 (defmacro gnus-info-set-params (info params)
2296   `(setcar (nthcdr 5 ,info) ,params))
2297
2298 (defmacro gnus-info-set-level (info level)
2299   `(let ((rank (cdr ,info)))
2300      (if (consp (car rank))
2301          (setcar (car rank) ,level)
2302        (setcar rank ,level))))
2303 (defmacro gnus-info-set-score (info score)
2304   `(let ((rank (cdr ,info)))
2305      (if (consp (car rank))
2306          (setcdr (car rank) ,score)
2307        (setcar rank (cons (car rank) ,score)))))
2308
2309 (defmacro gnus-get-info (group)
2310   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2311
2312 (defun gnus-byte-code (func)
2313   "Return a form that can be `eval'ed based on FUNC."
2314   (let ((fval (symbol-function func)))
2315     (if (byte-code-function-p fval)
2316         (let ((flist (append fval nil)))
2317           (setcar flist 'byte-code)
2318           flist)
2319       (cons 'progn (cddr fval)))))
2320
2321 ;; Find out whether the gnus-visual TYPE is wanted.
2322 (defun gnus-visual-p (&optional type class)
2323   (and gnus-visual                      ; Has to be non-nil, at least.
2324        (if (not type)                   ; We don't care about type.
2325            gnus-visual
2326          (if (listp gnus-visual)        ; It's a list, so we check it.
2327              (or (memq type gnus-visual)
2328                  (memq class gnus-visual))
2329            t))))
2330
2331 ;;; Load the compatability functions.
2332
2333 (require 'gnus-cus)
2334 (require 'gnus-ems)
2335
2336 \f
2337 ;;;
2338 ;;; Shutdown
2339 ;;;
2340
2341 (defvar gnus-shutdown-alist nil)
2342
2343 (defun gnus-add-shutdown (function &rest symbols)
2344   "Run FUNCTION whenever one of SYMBOLS is shut down."
2345   (push (cons function symbols) gnus-shutdown-alist))
2346
2347 (defun gnus-shutdown (symbol)
2348   "Shut down everything that waits for SYMBOL."
2349   (let ((alist gnus-shutdown-alist)
2350         entry)
2351     (while (setq entry (pop alist))
2352       (when (memq symbol (cdr entry))
2353         (funcall (car entry))))))
2354
2355 \f
2356
2357 ;; Format specs.  The chunks below are the machine-generated forms
2358 ;; that are to be evaled as the result of the default format strings.
2359 ;; We write them in here to get them byte-compiled.  That way the
2360 ;; default actions will be quite fast, while still retaining the full
2361 ;; flexibility of the user-defined format specs.
2362
2363 ;; First we have lots of dummy defvars to let the compiler know these
2364 ;; are really dynamic variables.
2365
2366 (defvar gnus-tmp-unread)
2367 (defvar gnus-tmp-replied)
2368 (defvar gnus-tmp-score-char)
2369 (defvar gnus-tmp-indentation)
2370 (defvar gnus-tmp-opening-bracket)
2371 (defvar gnus-tmp-lines)
2372 (defvar gnus-tmp-name)
2373 (defvar gnus-tmp-closing-bracket)
2374 (defvar gnus-tmp-subject-or-nil)
2375 (defvar gnus-tmp-subject)
2376 (defvar gnus-tmp-marked)
2377 (defvar gnus-tmp-marked-mark)
2378 (defvar gnus-tmp-subscribed)
2379 (defvar gnus-tmp-process-marked)
2380 (defvar gnus-tmp-number-of-unread)
2381 (defvar gnus-tmp-group-name)
2382 (defvar gnus-tmp-group)
2383 (defvar gnus-tmp-article-number)
2384 (defvar gnus-tmp-unread-and-unselected)
2385 (defvar gnus-tmp-news-method)
2386 (defvar gnus-tmp-news-server)
2387 (defvar gnus-tmp-article-number)
2388 (defvar gnus-mouse-face)
2389 (defvar gnus-mouse-face-prop)
2390
2391 (defun gnus-summary-line-format-spec ()
2392   (insert gnus-tmp-unread gnus-tmp-replied
2393           gnus-tmp-score-char gnus-tmp-indentation)
2394   (gnus-put-text-property
2395    (point)
2396    (progn
2397      (insert
2398       gnus-tmp-opening-bracket
2399       (format "%4d: %-20s"
2400               gnus-tmp-lines
2401               (if (> (length gnus-tmp-name) 20)
2402                   (substring gnus-tmp-name 0 20)
2403                 gnus-tmp-name))
2404       gnus-tmp-closing-bracket)
2405      (point))
2406    gnus-mouse-face-prop gnus-mouse-face)
2407   (insert " " gnus-tmp-subject-or-nil "\n"))
2408
2409 (defvar gnus-summary-line-format-spec
2410   (gnus-byte-code 'gnus-summary-line-format-spec))
2411
2412 (defun gnus-summary-dummy-line-format-spec ()
2413   (insert "*  ")
2414   (gnus-put-text-property
2415    (point)
2416    (progn
2417      (insert ":                          :")
2418      (point))
2419    gnus-mouse-face-prop gnus-mouse-face)
2420   (insert " " gnus-tmp-subject "\n"))
2421
2422 (defvar gnus-summary-dummy-line-format-spec
2423   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2424
2425 (defun gnus-group-line-format-spec ()
2426   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2427           gnus-tmp-process-marked
2428           gnus-group-indentation
2429           (format "%5s: " gnus-tmp-number-of-unread))
2430   (gnus-put-text-property
2431    (point)
2432    (progn
2433      (insert gnus-tmp-group "\n")
2434      (1- (point)))
2435    gnus-mouse-face-prop gnus-mouse-face))
2436 (defvar gnus-group-line-format-spec
2437   (gnus-byte-code 'gnus-group-line-format-spec))
2438
2439 (defvar gnus-format-specs
2440   `((version . ,emacs-version)
2441     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2442     (summary-dummy ,gnus-summary-dummy-line-format
2443                    ,gnus-summary-dummy-line-format-spec)
2444     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2445
2446 (defvar gnus-article-mode-line-format-spec nil)
2447 (defvar gnus-summary-mode-line-format-spec nil)
2448 (defvar gnus-group-mode-line-format-spec nil)
2449
2450 ;;; Phew.  All that gruft is over, fortunately.
2451
2452 \f
2453 ;;;
2454 ;;; Gnus Utility Functions
2455 ;;;
2456
2457 (defun gnus-extract-address-components (from)
2458   (let (name address)
2459     ;; First find the address - the thing with the @ in it.  This may
2460     ;; not be accurate in mail addresses, but does the trick most of
2461     ;; the time in news messages.
2462     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2463         (setq address (substring from (match-beginning 0) (match-end 0))))
2464     ;; Then we check whether the "name <address>" format is used.
2465     (and address
2466          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2467          ;; Linear white space is not required.
2468          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2469          (and (setq name (substring from 0 (match-beginning 0)))
2470               ;; Strip any quotes from the name.
2471               (string-match "\".*\"" name)
2472               (setq name (substring name 1 (1- (match-end 0))))))
2473     ;; If not, then "address (name)" is used.
2474     (or name
2475         (and (string-match "(.+)" from)
2476              (setq name (substring from (1+ (match-beginning 0))
2477                                    (1- (match-end 0)))))
2478         (and (string-match "()" from)
2479              (setq name address))
2480         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2481         ;; XOVER might not support folded From headers.
2482         (and (string-match "(.*" from)
2483              (setq name (substring from (1+ (match-beginning 0))
2484                                    (match-end 0)))))
2485     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2486     (list (or name from) (or address from))))
2487
2488 (defun gnus-fetch-field (field)
2489   "Return the value of the header FIELD of current article."
2490   (save-excursion
2491     (save-restriction
2492       (let ((case-fold-search t)
2493             (inhibit-point-motion-hooks t))
2494         (nnheader-narrow-to-headers)
2495         (message-fetch-field field)))))
2496
2497 (defun gnus-goto-colon ()
2498   (beginning-of-line)
2499   (search-forward ":" (gnus-point-at-eol) t))
2500
2501 ;;;###autoload
2502 (defun gnus-update-format (var)
2503   "Update the format specification near point."
2504   (interactive
2505    (list
2506     (save-excursion
2507       (eval-defun nil)
2508       ;; Find the end of the current word.
2509       (re-search-forward "[ \t\n]" nil t)
2510       ;; Search backward.
2511       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2512         (match-string 1)))))
2513   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2514                               (match-string 1 var))))
2515          (entry (assq type gnus-format-specs))
2516          value spec)
2517     (when entry
2518       (setq gnus-format-specs (delq entry gnus-format-specs)))
2519     (set
2520      (intern (format "%s-spec" var))
2521      (gnus-parse-format (setq value (symbol-value (intern var)))
2522                         (symbol-value (intern (format "%s-alist" var)))
2523                         (not (string-match "mode" var))))
2524     (setq spec (symbol-value (intern (format "%s-spec" var))))
2525     (push (list type value spec) gnus-format-specs)
2526
2527     (pop-to-buffer "*Gnus Format*")
2528     (erase-buffer)
2529     (lisp-interaction-mode)
2530     (insert (pp-to-string spec))))
2531
2532 (defun gnus-update-format-specifications (&optional force)
2533   "Update all (necessary) format specifications."
2534   ;; Make the indentation array.
2535   (gnus-make-thread-indent-array)
2536
2537   ;; See whether all the stored info needs to be flushed.
2538   (when (or force
2539             (not (equal emacs-version
2540                         (cdr (assq 'version gnus-format-specs)))))
2541     (setq gnus-format-specs nil))
2542
2543   ;; Go through all the formats and see whether they need updating.
2544   (let ((types '(summary summary-dummy group
2545                          summary-mode group-mode article-mode))
2546         new-format entry type val)
2547     (while (setq type (pop types))
2548       ;; Jump to the proper buffer to find out the value of
2549       ;; the variable, if possible.  (It may be buffer-local.)
2550       (save-excursion
2551         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2552               val)
2553           (when (and (boundp buffer)
2554                      (setq val (symbol-value buffer))
2555                      (get-buffer val)
2556                      (buffer-name (get-buffer val)))
2557             (set-buffer (get-buffer val)))
2558           (setq new-format (symbol-value
2559                             (intern (format "gnus-%s-line-format" type))))))
2560       (setq entry (cdr (assq type gnus-format-specs)))
2561       (if (and entry
2562                (equal (car entry) new-format))
2563           ;; Use the old format.
2564           (set (intern (format "gnus-%s-line-format-spec" type))
2565                (cadr entry))
2566         ;; This is a new format.
2567         (setq val
2568               (if (not (stringp new-format))
2569                   ;; This is a function call or something.
2570                   new-format
2571                 ;; This is a "real" format.
2572                 (gnus-parse-format
2573                  new-format
2574                  (symbol-value
2575                   (intern (format "gnus-%s-line-format-alist"
2576                                   (if (eq type 'article-mode)
2577                                       'summary-mode type))))
2578                  (not (string-match "mode$" (symbol-name type))))))
2579         ;; Enter the new format spec into the list.
2580         (if entry
2581             (progn
2582               (setcar (cdr entry) val)
2583               (setcar entry new-format))
2584           (push (list type new-format val) gnus-format-specs))
2585         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2586
2587   (unless (assq 'version gnus-format-specs)
2588     (push (cons 'version emacs-version) gnus-format-specs))
2589
2590   (gnus-update-group-mark-positions)
2591   (gnus-update-summary-mark-positions))
2592
2593 (defun gnus-update-summary-mark-positions ()
2594   "Compute where the summary marks are to go."
2595   (save-excursion
2596     (when (and gnus-summary-buffer
2597                (get-buffer gnus-summary-buffer)
2598                (buffer-name (get-buffer gnus-summary-buffer)))
2599       (set-buffer gnus-summary-buffer))
2600     (let ((gnus-replied-mark 129)
2601           (gnus-score-below-mark 130)
2602           (gnus-score-over-mark 130)
2603           (thread nil)
2604           (gnus-visual nil)
2605           (spec gnus-summary-line-format-spec)
2606           pos)
2607       (save-excursion
2608         (gnus-set-work-buffer)
2609         (let ((gnus-summary-line-format-spec spec))
2610           (gnus-summary-insert-line
2611            [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2612           (goto-char (point-min))
2613           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2614                                              (- (point) 2)))))
2615           (goto-char (point-min))
2616           (push (cons 'replied (and (search-forward "\201" nil t) 
2617                                     (- (point) 2)))
2618                 pos)
2619           (goto-char (point-min))
2620           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2621                 pos)))
2622       (setq gnus-summary-mark-positions pos))))
2623
2624 (defun gnus-update-group-mark-positions ()
2625   (save-excursion
2626     (let ((gnus-process-mark 128)
2627           (gnus-group-marked '("dummy.group"))
2628           (gnus-active-hashtb (make-vector 10 0)))
2629       (gnus-set-active "dummy.group" '(0 . 0))
2630       (gnus-set-work-buffer)
2631       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2632       (goto-char (point-min))
2633       (setq gnus-group-mark-positions
2634             (list (cons 'process (and (search-forward "\200" nil t)
2635                                       (- (point) 2))))))))
2636
2637 (defvar gnus-mouse-face-0 'highlight)
2638 (defvar gnus-mouse-face-1 'highlight)
2639 (defvar gnus-mouse-face-2 'highlight)
2640 (defvar gnus-mouse-face-3 'highlight)
2641 (defvar gnus-mouse-face-4 'highlight)
2642
2643 (defun gnus-mouse-face-function (form type)
2644   `(gnus-put-text-property
2645     (point) (progn ,@form (point))
2646     gnus-mouse-face-prop
2647     ,(if (equal type 0)
2648          'gnus-mouse-face
2649        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2650
2651 (defvar gnus-face-0 'bold)
2652 (defvar gnus-face-1 'italic)
2653 (defvar gnus-face-2 'bold-italic)
2654 (defvar gnus-face-3 'bold)
2655 (defvar gnus-face-4 'bold)
2656
2657 (defun gnus-face-face-function (form type)
2658   `(gnus-put-text-property
2659     (point) (progn ,@form (point))
2660     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2661
2662 (defun gnus-max-width-function (el max-width)
2663   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2664   (if (symbolp el)
2665       `(if (> (length ,el) ,max-width)
2666            (substring ,el 0 ,max-width)
2667          ,el)
2668     `(let ((val (eval ,el)))
2669        (if (numberp val)
2670            (setq val (int-to-string val)))
2671        (if (> (length val) ,max-width)
2672            (substring val 0 ,max-width)
2673          val))))
2674
2675 (defun gnus-parse-format (format spec-alist &optional insert)
2676   ;; This function parses the FORMAT string with the help of the
2677   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2678   ;; string.  If the FORMAT string contains the specifiers %( and %)
2679   ;; the text between them will have the mouse-face text property.
2680   (if (string-match
2681        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2682        format)
2683       (gnus-parse-complex-format format spec-alist)
2684     ;; This is a simple format.
2685     (gnus-parse-simple-format format spec-alist insert)))
2686
2687 (defun gnus-parse-complex-format (format spec-alist)
2688   (save-excursion
2689     (gnus-set-work-buffer)
2690     (insert format)
2691     (goto-char (point-min))
2692     (while (re-search-forward "\"" nil t)
2693       (replace-match "\\\"" nil t))
2694     (goto-char (point-min))
2695     (insert "(\"")
2696     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2697       (let ((number (if (match-beginning 1)
2698                         (match-string 1) "0"))
2699             (delim (aref (match-string 2) 0)))
2700         (if (or (= delim ?\() (= delim ?\{))
2701             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2702                                    " " number " \""))
2703           (replace-match "\")\""))))
2704     (goto-char (point-max))
2705     (insert "\")")
2706     (goto-char (point-min))
2707     (let ((form (read (current-buffer))))
2708       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2709
2710 (defun gnus-complex-form-to-spec (form spec-alist)
2711   (delq nil
2712         (mapcar
2713          (lambda (sform)
2714            (if (stringp sform)
2715                (gnus-parse-simple-format sform spec-alist t)
2716              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2717                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2718                       (nth 1 sform))))
2719          form)))
2720
2721 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2722   ;; This function parses the FORMAT string with the help of the
2723   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2724   ;; string.
2725   (let ((max-width 0)
2726         spec flist fstring newspec elem beg result dontinsert)
2727     (save-excursion
2728       (gnus-set-work-buffer)
2729       (insert format)
2730       (goto-char (point-min))
2731       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2732                                 nil t)
2733         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2734               (setq newspec "%"
2735                     beg (1+ (match-beginning 0)))
2736           ;; First check if there are any specs that look anything like
2737           ;; "%12,12A", ie. with a "max width specification".  These have
2738           ;; to be treated specially.
2739           (if (setq beg (match-beginning 1))
2740               (setq max-width
2741                     (string-to-int
2742                      (buffer-substring
2743                       (1+ (match-beginning 1)) (match-end 1))))
2744             (setq max-width 0)
2745             (setq beg (match-beginning 2)))
2746           ;; Find the specification from `spec-alist'.
2747           (unless (setq elem (cdr (assq spec spec-alist)))
2748             (setq elem '("*" ?s)))
2749           ;; Treat user defined format specifiers specially.
2750           (when (eq (car elem) 'gnus-tmp-user-defined)
2751             (setq elem
2752                   (list
2753                    (list (intern (concat "gnus-user-format-function-"
2754                                          (match-string 3)))
2755                          'gnus-tmp-header) ?s))
2756             (delete-region (match-beginning 3) (match-end 3)))
2757           (if (not (zerop max-width))
2758               (let ((el (car elem)))
2759                 (cond ((= (cadr elem) ?c)
2760                        (setq el (list 'char-to-string el)))
2761                       ((= (cadr elem) ?d)
2762                        (setq el (list 'int-to-string el))))
2763                 (setq flist (cons (gnus-max-width-function el max-width)
2764                                   flist))
2765                 (setq newspec ?s))
2766             (progn
2767               (setq flist (cons (car elem) flist))
2768               (setq newspec (cadr elem)))))
2769         ;; Remove the old specification (and possibly a ",12" string).
2770         (delete-region beg (match-end 2))
2771         ;; Insert the new specification.
2772         (goto-char beg)
2773         (insert newspec))
2774       (setq fstring (buffer-substring 1 (point-max))))
2775     ;; Do some postprocessing to increase efficiency.
2776     (setq
2777      result
2778      (cond
2779       ;; Emptyness.
2780       ((string= fstring "")
2781        nil)
2782       ;; Not a format string.
2783       ((not (string-match "%" fstring))
2784        (list fstring))
2785       ;; A format string with just a single string spec.
2786       ((string= fstring "%s")
2787        (list (car flist)))
2788       ;; A single character.
2789       ((string= fstring "%c")
2790        (list (car flist)))
2791       ;; A single number.
2792       ((string= fstring "%d")
2793        (setq dontinsert)
2794        (if insert
2795            (list `(princ ,(car flist)))
2796          (list `(int-to-string ,(car flist)))))
2797       ;; Just lots of chars and strings.
2798       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2799        (nreverse flist))
2800       ;; A single string spec at the beginning of the spec.
2801       ((string-match "\\`%[sc][^%]+\\'" fstring)
2802        (list (car flist) (substring fstring 2)))
2803       ;; A single string spec in the middle of the spec.
2804       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2805        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2806       ;; A single string spec in the end of the spec.
2807       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2808        (list (match-string 1 fstring) (car flist)))
2809       ;; A more complex spec.
2810       (t
2811        (list (cons 'format (cons fstring (nreverse flist)))))))
2812
2813     (if insert
2814         (when result
2815           (if dontinsert
2816               result
2817             (cons 'insert result)))
2818       (cond ((stringp result)
2819              result)
2820             ((consp result)
2821              (cons 'concat result))
2822             (t "")))))
2823
2824 (defun gnus-eval-format (format &optional alist props)
2825   "Eval the format variable FORMAT, using ALIST.
2826 If PROPS, insert the result."
2827   (let ((form (gnus-parse-format format alist props)))
2828     (if props
2829         (gnus-add-text-properties (point) (progn (eval form) (point)) props)
2830       (eval form))))
2831
2832 (defun gnus-remove-text-with-property (prop)
2833   "Delete all text in the current buffer with text property PROP."
2834   (save-excursion
2835     (goto-char (point-min))
2836     (while (not (eobp))
2837       (while (get-text-property (point) prop)
2838         (delete-char 1))
2839       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2840
2841 (defun gnus-set-work-buffer ()
2842   (if (get-buffer gnus-work-buffer)
2843       (progn
2844         (set-buffer gnus-work-buffer)
2845         (erase-buffer))
2846     (set-buffer (get-buffer-create gnus-work-buffer))
2847     (kill-all-local-variables)
2848     (buffer-disable-undo (current-buffer))
2849     (gnus-add-current-to-buffer-list)))
2850
2851 ;; Article file names when saving.
2852
2853 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2854   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2855 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2856 Otherwise, it is like ~/News/news/group/num."
2857   (let ((default
2858           (expand-file-name
2859            (concat (if (gnus-use-long-file-name 'not-save)
2860                        (gnus-capitalize-newsgroup newsgroup)
2861                      (gnus-newsgroup-directory-form newsgroup))
2862                    "/" (int-to-string (mail-header-number headers)))
2863            gnus-article-save-directory)))
2864     (if (and last-file
2865              (string-equal (file-name-directory default)
2866                            (file-name-directory last-file))
2867              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2868         default
2869       (or last-file default))))
2870
2871 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2872   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2873 If variable `gnus-use-long-file-name' is non-nil, it is
2874 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2875   (let ((default
2876           (expand-file-name
2877            (concat (if (gnus-use-long-file-name 'not-save)
2878                        newsgroup
2879                      (gnus-newsgroup-directory-form newsgroup))
2880                    "/" (int-to-string (mail-header-number headers)))
2881            gnus-article-save-directory)))
2882     (if (and last-file
2883              (string-equal (file-name-directory default)
2884                            (file-name-directory last-file))
2885              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2886         default
2887       (or last-file default))))
2888
2889 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2890   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2891 If variable `gnus-use-long-file-name' is non-nil, it is
2892 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2893   (or last-file
2894       (expand-file-name
2895        (if (gnus-use-long-file-name 'not-save)
2896            (gnus-capitalize-newsgroup newsgroup)
2897          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2898        gnus-article-save-directory)))
2899
2900 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2901   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2902 If variable `gnus-use-long-file-name' is non-nil, it is
2903 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2904   (or last-file
2905       (expand-file-name
2906        (if (gnus-use-long-file-name 'not-save)
2907            newsgroup
2908          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2909        gnus-article-save-directory)))
2910
2911 ;; For subscribing new newsgroup
2912
2913 (defun gnus-subscribe-hierarchical-interactive (groups)
2914   (let ((groups (sort groups 'string<))
2915         prefixes prefix start ans group starts)
2916     (while groups
2917       (setq prefixes (list "^"))
2918       (while (and groups prefixes)
2919         (while (not (string-match (car prefixes) (car groups)))
2920           (setq prefixes (cdr prefixes)))
2921         (setq prefix (car prefixes))
2922         (setq start (1- (length prefix)))
2923         (if (and (string-match "[^\\.]\\." (car groups) start)
2924                  (cdr groups)
2925                  (setq prefix
2926                        (concat "^" (substring (car groups) 0 (match-end 0))))
2927                  (string-match prefix (cadr groups)))
2928             (progn
2929               (setq prefixes (cons prefix prefixes))
2930               (message "Descend hierarchy %s? ([y]nsq): "
2931                        (substring prefix 1 (1- (length prefix))))
2932               (while (not (memq (setq ans (read-char)) '(?y ?\n ?n ?s ?q)))
2933                 (ding)
2934                 (message "Descend hierarchy %s? ([y]nsq): "
2935                          (substring prefix 1 (1- (length prefix)))))
2936               (cond ((= ans ?n)
2937                      (while (and groups
2938                                  (string-match prefix
2939                                                (setq group (car groups))))
2940                        (setq gnus-killed-list
2941                              (cons group gnus-killed-list))
2942                        (gnus-sethash group group gnus-killed-hashtb)
2943                        (setq groups (cdr groups)))
2944                      (setq starts (cdr starts)))
2945                     ((= ans ?s)
2946                      (while (and groups
2947                                  (string-match prefix
2948                                                (setq group (car groups))))
2949                        (gnus-sethash group group gnus-killed-hashtb)
2950                        (gnus-subscribe-alphabetically (car groups))
2951                        (setq groups (cdr groups)))
2952                      (setq starts (cdr starts)))
2953                     ((= ans ?q)
2954                      (while groups
2955                        (setq group (car groups))
2956                        (setq gnus-killed-list (cons group gnus-killed-list))
2957                        (gnus-sethash group group gnus-killed-hashtb)
2958                        (setq groups (cdr groups))))
2959                     (t nil)))
2960           (message "Subscribe %s? ([n]yq)" (car groups))
2961           (while (not (memq (setq ans (read-char)) '(?y ?\n ?q ?n)))
2962             (ding)
2963             (message "Subscribe %s? ([n]yq)" (car groups)))
2964           (setq group (car groups))
2965           (cond ((= ans ?y)
2966                  (gnus-subscribe-alphabetically (car groups))
2967                  (gnus-sethash group group gnus-killed-hashtb))
2968                 ((= ans ?q)
2969                  (while groups
2970                    (setq group (car groups))
2971                    (setq gnus-killed-list (cons group gnus-killed-list))
2972                    (gnus-sethash group group gnus-killed-hashtb)
2973                    (setq groups (cdr groups))))
2974                 (t
2975                  (setq gnus-killed-list (cons group gnus-killed-list))
2976                  (gnus-sethash group group gnus-killed-hashtb)))
2977           (setq groups (cdr groups)))))))
2978
2979 (defun gnus-subscribe-randomly (newsgroup)
2980   "Subscribe new NEWSGROUP by making it the first newsgroup."
2981   (gnus-subscribe-newsgroup newsgroup))
2982
2983 (defun gnus-subscribe-alphabetically (newgroup)
2984   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2985   (let ((groups (cdr gnus-newsrc-alist))
2986         before)
2987     (while (and (not before) groups)
2988       (if (string< newgroup (caar groups))
2989           (setq before (caar groups))
2990         (setq groups (cdr groups))))
2991     (gnus-subscribe-newsgroup newgroup before)))
2992
2993 (defun gnus-subscribe-hierarchically (newgroup)
2994   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2995   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2996   (save-excursion
2997     (set-buffer (find-file-noselect gnus-current-startup-file))
2998     (let ((groupkey newgroup)
2999           before)
3000       (while (and (not before) groupkey)
3001         (goto-char (point-min))
3002         (let ((groupkey-re
3003                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
3004           (while (and (re-search-forward groupkey-re nil t)
3005                       (progn
3006                         (setq before (match-string 1))
3007                         (string< before newgroup)))))
3008         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
3009         (setq groupkey
3010               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
3011                   (substring groupkey (match-beginning 1) (match-end 1)))))
3012       (gnus-subscribe-newsgroup newgroup before))))
3013
3014 (defun gnus-subscribe-interactively (group)
3015   "Subscribe the new GROUP interactively.
3016 It is inserted in hierarchical newsgroup order if subscribed.  If not,
3017 it is killed."
3018   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
3019       (gnus-subscribe-hierarchically group)
3020     (push group gnus-killed-list)))
3021
3022 (defun gnus-subscribe-zombies (group)
3023   "Make the new GROUP into a zombie group."
3024   (push group gnus-zombie-list))
3025
3026 (defun gnus-subscribe-killed (group)
3027   "Make the new GROUP a killed group."
3028   (push group gnus-killed-list))
3029
3030 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
3031   "Subscribe new NEWSGROUP.
3032 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
3033 the first newsgroup."
3034   ;; We subscribe the group by changing its level to `subscribed'.
3035   (gnus-group-change-level
3036    newsgroup gnus-level-default-subscribed
3037    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
3038   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
3039
3040 ;; For directories
3041
3042 (defun gnus-newsgroup-directory-form (newsgroup)
3043   "Make hierarchical directory name from NEWSGROUP name."
3044   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
3045         (len (length newsgroup))
3046         idx)
3047     ;; If this is a foreign group, we don't want to translate the
3048     ;; entire name.
3049     (if (setq idx (string-match ":" newsgroup))
3050         (aset newsgroup idx ?/)
3051       (setq idx 0))
3052     ;; Replace all occurrences of `.' with `/'.
3053     (while (< idx len)
3054       (if (= (aref newsgroup idx) ?.)
3055           (aset newsgroup idx ?/))
3056       (setq idx (1+ idx)))
3057     newsgroup))
3058
3059 (defun gnus-newsgroup-savable-name (group)
3060   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
3061   ;; with dots.
3062   (nnheader-replace-chars-in-string group ?/ ?.))
3063
3064 (defun gnus-make-directory (dir)
3065   "Make DIRECTORY recursively."
3066   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
3067   ;; of the many mysteries of the universe.
3068   (let* ((dir (expand-file-name dir default-directory))
3069          dirs err)
3070     (if (string-match "/$" dir)
3071         (setq dir (substring dir 0 (match-beginning 0))))
3072     ;; First go down the path until we find a directory that exists.
3073     (while (not (file-exists-p dir))
3074       (setq dirs (cons dir dirs))
3075       (string-match "/[^/]+$" dir)
3076       (setq dir (substring dir 0 (match-beginning 0))))
3077     ;; Then create all the subdirs.
3078     (while (and dirs (not err))
3079       (condition-case ()
3080           (make-directory (car dirs))
3081         (error (setq err t)))
3082       (setq dirs (cdr dirs)))
3083     ;; We return whether we were successful or not.
3084     (not dirs)))
3085
3086 (defun gnus-capitalize-newsgroup (newsgroup)
3087   "Capitalize NEWSGROUP name."
3088   (and (not (zerop (length newsgroup)))
3089        (concat (char-to-string (upcase (aref newsgroup 0)))
3090                (substring newsgroup 1))))
3091
3092 ;; Various... things.
3093
3094 (defun gnus-simplify-subject (subject &optional re-only)
3095   "Remove `Re:' and words in parentheses.
3096 If RE-ONLY is non-nil, strip leading `Re:'s only."
3097   (let ((case-fold-search t))           ;Ignore case.
3098     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
3099     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
3100       (setq subject (substring subject (match-end 0))))
3101     ;; Remove uninteresting prefixes.
3102     (if (and (not re-only)
3103              gnus-simplify-ignored-prefixes
3104              (string-match gnus-simplify-ignored-prefixes subject))
3105         (setq subject (substring subject (match-end 0))))
3106     ;; Remove words in parentheses from end.
3107     (unless re-only
3108       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3109         (setq subject (substring subject 0 (match-beginning 0)))))
3110     ;; Return subject string.
3111     subject))
3112
3113 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3114 ;; all whitespace.
3115 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3116 (defun gnus-simplify-buffer-fuzzy ()
3117   (let ((case-fold-search t))
3118     (goto-char (point-min))
3119     (while (search-forward "\t" nil t)
3120       (replace-match " " t t))
3121     (goto-char (point-min))
3122     (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *" nil t)
3123     (goto-char (match-beginning 0))
3124     (while (or
3125             (looking-at "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
3126             (looking-at "^[[].*: .*[]]$"))
3127       (goto-char (point-min))
3128       (while (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *"
3129                                 nil t)
3130         (replace-match "" t t))
3131       (goto-char (point-min))
3132       (while (re-search-forward "^[[].*: .*[]]$" nil t)
3133         (goto-char (match-end 0))
3134         (delete-char -1)
3135         (delete-region
3136          (progn (goto-char (match-beginning 0)))
3137          (re-search-forward ":"))))
3138     (goto-char (point-min))
3139     (while (re-search-forward " *[[{(][^()\n]*[]})] *$" nil t)
3140       (replace-match "" t t))
3141     (goto-char (point-min))
3142     (while (re-search-forward "  +" nil t)
3143       (replace-match " " t t))
3144     (goto-char (point-min))
3145     (while (re-search-forward " $" nil t)
3146       (replace-match "" t t))
3147     (goto-char (point-min))
3148     (while (re-search-forward "^ +" nil t)
3149       (replace-match "" t t))
3150     (goto-char (point-min))
3151     (when gnus-simplify-subject-fuzzy-regexp
3152       (if (listp gnus-simplify-subject-fuzzy-regexp)
3153           (let ((list gnus-simplify-subject-fuzzy-regexp))
3154             (while list
3155               (goto-char (point-min))
3156               (while (re-search-forward (car list) nil t)
3157                 (replace-match "" t t))
3158               (setq list (cdr list))))
3159         (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3160           (replace-match "" t t))))))
3161
3162 (defun gnus-simplify-subject-fuzzy (subject)
3163   "Siplify a subject string fuzzily."
3164   (save-excursion
3165     (gnus-set-work-buffer)
3166     (let ((case-fold-search t))
3167       (insert subject)
3168       (inline (gnus-simplify-buffer-fuzzy))
3169       (buffer-string))))
3170
3171 ;; Add the current buffer to the list of buffers to be killed on exit.
3172 (defun gnus-add-current-to-buffer-list ()
3173   (or (memq (current-buffer) gnus-buffer-list)
3174       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3175
3176 (defun gnus-string> (s1 s2)
3177   (not (or (string< s1 s2)
3178            (string= s1 s2))))
3179
3180 (defun gnus-read-active-file-p ()
3181   "Say whether the active file has been read from `gnus-select-method'."
3182   (memq gnus-select-method gnus-have-read-active-file))
3183
3184 ;;; General various misc type functions.
3185
3186 (defun gnus-clear-system ()
3187   "Clear all variables and buffers."
3188   ;; Clear Gnus variables.
3189   (let ((variables gnus-variable-list))
3190     (while variables
3191       (set (car variables) nil)
3192       (setq variables (cdr variables))))
3193   ;; Clear other internal variables.
3194   (setq gnus-list-of-killed-groups nil
3195         gnus-have-read-active-file nil
3196         gnus-newsrc-alist nil
3197         gnus-newsrc-hashtb nil
3198         gnus-killed-list nil
3199         gnus-zombie-list nil
3200         gnus-killed-hashtb nil
3201         gnus-active-hashtb nil
3202         gnus-moderated-list nil
3203         gnus-description-hashtb nil
3204         gnus-current-headers nil
3205         gnus-thread-indent-array nil
3206         gnus-newsgroup-headers nil
3207         gnus-newsgroup-name nil
3208         gnus-server-alist nil
3209         gnus-group-list-mode nil
3210         gnus-opened-servers nil
3211         gnus-current-select-method nil)
3212   (gnus-shutdown 'gnus)
3213   ;; Kill the startup file.
3214   (and gnus-current-startup-file
3215        (get-file-buffer gnus-current-startup-file)
3216        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3217   ;; Clear the dribble buffer.
3218   (gnus-dribble-clear)
3219   ;; Kill global KILL file buffer.
3220   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3221     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3222   (gnus-kill-buffer nntp-server-buffer)
3223   ;; Kill Gnus buffers.
3224   (while gnus-buffer-list
3225     (gnus-kill-buffer (pop gnus-buffer-list)))
3226   ;; Remove Gnus frames.
3227   (while gnus-created-frames
3228     (when (frame-live-p (car gnus-created-frames))
3229       ;; We slap a condition-case around this `delete-frame' to ensure 
3230       ;; agains errors if we try do delete the single frame that's left.
3231       (condition-case ()
3232           (delete-frame (car gnus-created-frames))
3233         (error nil)))
3234     (pop gnus-created-frames)))
3235
3236 (defun gnus-windows-old-to-new (setting)
3237   ;; First we take care of the really, really old Gnus 3 actions.
3238   (when (symbolp setting)
3239     (setq setting
3240           ;; Take care of ooold GNUS 3.x values.
3241           (cond ((eq setting 'SelectArticle) 'article)
3242                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3243                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3244                 (t setting))))
3245   (if (or (listp setting)
3246           (not (and gnus-window-configuration
3247                     (memq setting '(group summary article)))))
3248       setting
3249     (let* ((setting (if (eq setting 'group)
3250                         (if (assq 'newsgroup gnus-window-configuration)
3251                             'newsgroup
3252                           'newsgroups) setting))
3253            (elem (cadr (assq setting gnus-window-configuration)))
3254            (total (apply '+ elem))
3255            (types '(group summary article))
3256            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3257            (i 0)
3258            perc
3259            out)
3260       (while (< i 3)
3261         (or (not (numberp (nth i elem)))
3262             (zerop (nth i elem))
3263             (progn
3264               (setq perc  (/ (float (nth 0 elem)) total))
3265               (setq out (cons (if (eq pbuf (nth i types))
3266                                   (vector (nth i types) perc 'point)
3267                                 (vector (nth i types) perc))
3268                               out))))
3269         (setq i (1+ i)))
3270       (list (nreverse out)))))
3271
3272 ;;;###autoload
3273 (defun gnus-add-configuration (conf)
3274   "Add the window configuration CONF to `gnus-buffer-configuration'."
3275   (setq gnus-buffer-configuration
3276         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3277                          gnus-buffer-configuration))))
3278
3279 (defvar gnus-frame-list nil)
3280
3281 (defun gnus-configure-frame (split &optional window)
3282   "Split WINDOW according to SPLIT."
3283   (unless window
3284     (setq window (get-buffer-window (current-buffer))))
3285   (select-window window)
3286   ;; This might be an old-stylee buffer config.
3287   (when (vectorp split)
3288     (setq split (append split nil)))
3289   (when (or (consp (car split))
3290             (vectorp (car split)))
3291     (push 1.0 split)
3292     (push 'vertical split))
3293   ;; The SPLIT might be something that is to be evaled to
3294   ;; return a new SPLIT.
3295   (while (and (not (assq (car split) gnus-window-to-buffer))
3296               (gnus-functionp (car split)))
3297     (setq split (eval split)))
3298   (let* ((type (car split))
3299          (subs (cddr split))
3300          (len (if (eq type 'horizontal) (window-width) (window-height)))
3301          (total 0)
3302          (window-min-width (or gnus-window-min-width window-min-width))
3303          (window-min-height (or gnus-window-min-height window-min-height))
3304          s result new-win rest comp-subs size sub)
3305     (cond
3306      ;; Nothing to do here.
3307      ((null split))
3308      ;; Don't switch buffers.
3309      ((null type)
3310       (and (memq 'point split) window))
3311      ;; This is a buffer to be selected.
3312      ((not (memq type '(frame horizontal vertical)))
3313       (let ((buffer (cond ((stringp type) type)
3314                           (t (cdr (assq type gnus-window-to-buffer)))))
3315             buf)
3316         (unless buffer
3317           (error "Illegal buffer type: %s" type))
3318         (unless (setq buf (get-buffer (if (symbolp buffer)
3319                                           (symbol-value buffer) buffer)))
3320           (setq buf (get-buffer-create (if (symbolp buffer)
3321                                            (symbol-value buffer) buffer))))
3322         (switch-to-buffer buf)
3323         ;; We return the window if it has the `point' spec.
3324         (and (memq 'point split) window)))
3325      ;; This is a frame split.
3326      ((eq type 'frame)
3327       (unless gnus-frame-list
3328         (setq gnus-frame-list (list (window-frame
3329                                      (get-buffer-window (current-buffer))))))
3330       (let ((i 0)
3331             params frame fresult)
3332         (while (< i (length subs))
3333           ;; Frame parameter is gotten from the sub-split.
3334           (setq params (cadr (elt subs i)))
3335           ;; It should be a list.
3336           (unless (listp params)
3337             (setq params nil))
3338           ;; Create a new frame?
3339           (unless (setq frame (elt gnus-frame-list i))
3340             (nconc gnus-frame-list (list (setq frame (make-frame params))))
3341             (push frame gnus-created-frames))
3342           ;; Is the old frame still alive?
3343           (unless (frame-live-p frame)
3344             (setcar (nthcdr i gnus-frame-list)
3345                     (setq frame (make-frame params))))
3346           ;; Select the frame in question and do more splits there.
3347           (select-frame frame)
3348           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3349           (incf i))
3350         ;; Select the frame that has the selected buffer.
3351         (when fresult
3352           (select-frame (window-frame fresult)))))
3353      ;; This is a normal split.
3354      (t
3355       (when (> (length subs) 0)
3356         ;; First we have to compute the sizes of all new windows.
3357         (while subs
3358           (setq sub (append (pop subs) nil))
3359           (while (and (not (assq (car sub) gnus-window-to-buffer))
3360                       (gnus-functionp (car sub)))
3361             (setq sub (eval sub)))
3362           (when sub
3363             (push sub comp-subs)
3364             (setq size (cadar comp-subs))
3365             (cond ((equal size 1.0)
3366                    (setq rest (car comp-subs))
3367                    (setq s 0))
3368                   ((floatp size)
3369                    (setq s (floor (* size len))))
3370                   ((integerp size)
3371                    (setq s size))
3372                   (t
3373                    (error "Illegal size: %s" size)))
3374             ;; Try to make sure that we are inside the safe limits.
3375             (cond ((zerop s))
3376                   ((eq type 'horizontal)
3377                    (setq s (max s window-min-width)))
3378                   ((eq type 'vertical)
3379                    (setq s (max s window-min-height))))
3380             (setcar (cdar comp-subs) s)
3381             (incf total s)))
3382         ;; Take care of the "1.0" spec.
3383         (if rest
3384             (setcar (cdr rest) (- len total))
3385           (error "No 1.0 specs in %s" split))
3386         ;; The we do the actual splitting in a nice recursive
3387         ;; fashion.
3388         (setq comp-subs (nreverse comp-subs))
3389         (while comp-subs
3390           (if (null (cdr comp-subs))
3391               (setq new-win window)
3392             (setq new-win
3393                   (split-window window (cadar comp-subs)
3394                                 (eq type 'horizontal))))
3395           (setq result (or (gnus-configure-frame
3396                             (car comp-subs) window) result))
3397           (select-window new-win)
3398           (setq window new-win)
3399           (setq comp-subs (cdr comp-subs))))
3400       ;; Return the proper window, if any.
3401       (when result
3402         (select-window result))))))
3403
3404 (defvar gnus-frame-split-p nil)
3405
3406 (defun gnus-configure-windows (setting &optional force)
3407   (setq setting (gnus-windows-old-to-new setting))
3408   (let ((split (if (symbolp setting)
3409                    (cadr (assq setting gnus-buffer-configuration))
3410                  setting))
3411         all-visible)
3412
3413     (setq gnus-frame-split-p nil)
3414
3415     (unless split
3416       (error "No such setting: %s" setting))
3417
3418     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3419              (not force))
3420         ;; All the windows mentioned are already visible, so we just
3421         ;; put point in the assigned buffer, and do not touch the
3422         ;; winconf.
3423         (select-window all-visible)
3424
3425       ;; Either remove all windows or just remove all Gnus windows.
3426       (let ((frame (selected-frame)))
3427         (unwind-protect
3428             (if gnus-use-full-window
3429                 ;; We want to remove all other windows.
3430                 (if (not gnus-frame-split-p)
3431                     ;; This is not a `frame' split, so we ignore the
3432                     ;; other frames.  
3433                     (delete-other-windows)
3434                   ;; This is a `frame' split, so we delete all windows
3435                   ;; on all frames.
3436                   (mapcar 
3437                    (lambda (frame)
3438                      (unless (eq (cdr (assq 'minibuffer
3439                                             (frame-parameters frame)))
3440                                  'only)
3441                        (select-frame frame)
3442                        (delete-other-windows)))
3443                    (frame-list)))
3444               ;; Just remove some windows.
3445               (gnus-remove-some-windows)
3446               (switch-to-buffer nntp-server-buffer))
3447           (select-frame frame)))
3448
3449       (switch-to-buffer nntp-server-buffer)
3450       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3451
3452 (defun gnus-all-windows-visible-p (split)
3453   "Say whether all buffers in SPLIT are currently visible.
3454 In particular, the value returned will be the window that
3455 should have point."
3456   (let ((stack (list split))
3457         (all-visible t)
3458         type buffer win buf)
3459     (while (and (setq split (pop stack))
3460                 all-visible)
3461       ;; Be backwards compatible.
3462       (when (vectorp split)
3463         (setq split (append split nil)))
3464       (when (or (consp (car split))
3465                 (vectorp (car split)))
3466         (push 1.0 split)
3467         (push 'vertical split))
3468       ;; The SPLIT might be something that is to be evaled to
3469       ;; return a new SPLIT.
3470       (while (and (not (assq (car split) gnus-window-to-buffer))
3471                   (gnus-functionp (car split)))
3472         (setq split (eval split)))
3473
3474       (setq type (elt split 0))
3475       (cond
3476        ;; Nothing here.
3477        ((null split) t)
3478        ;; A buffer.
3479        ((not (memq type '(horizontal vertical frame)))
3480         (setq buffer (cond ((stringp type) type)
3481                            (t (cdr (assq type gnus-window-to-buffer)))))
3482         (unless buffer
3483           (error "Illegal buffer type: %s" type))
3484         (when (setq buf (get-buffer (if (symbolp buffer)
3485                                         (symbol-value buffer)
3486                                       buffer)))
3487           (setq win (get-buffer-window buf t)))
3488         (if win
3489             (when (memq 'point split)
3490                 (setq all-visible win))
3491           (setq all-visible nil)))
3492        (t
3493         (when (eq type 'frame)
3494           (setq gnus-frame-split-p t))
3495         (setq stack (append (cddr split) stack)))))
3496     (unless (eq all-visible t)
3497       all-visible)))
3498
3499 (defun gnus-window-top-edge (&optional window)
3500   (nth 1 (window-edges window)))
3501
3502 (defun gnus-remove-some-windows ()
3503   (let ((buffers gnus-window-to-buffer)
3504         buf bufs lowest-buf lowest)
3505     (save-excursion
3506       ;; Remove windows on all known Gnus buffers.
3507       (while buffers
3508         (setq buf (cdar buffers))
3509         (if (symbolp buf)
3510             (setq buf (and (boundp buf) (symbol-value buf))))
3511         (and buf
3512              (get-buffer-window buf)
3513              (progn
3514                (setq bufs (cons buf bufs))
3515                (pop-to-buffer buf)
3516                (if (or (not lowest)
3517                        (< (gnus-window-top-edge) lowest))
3518                    (progn
3519                      (setq lowest (gnus-window-top-edge))
3520                      (setq lowest-buf buf)))))
3521         (setq buffers (cdr buffers)))
3522       ;; Remove windows on *all* summary buffers.
3523       (walk-windows
3524        (lambda (win)
3525          (let ((buf (window-buffer win)))
3526            (if (string-match    "^\\*Summary" (buffer-name buf))
3527                (progn
3528                  (setq bufs (cons buf bufs))
3529                  (pop-to-buffer buf)
3530                  (if (or (not lowest)
3531                          (< (gnus-window-top-edge) lowest))
3532                      (progn
3533                        (setq lowest-buf buf)
3534                        (setq lowest (gnus-window-top-edge)))))))))
3535       (and lowest-buf
3536            (progn
3537              (pop-to-buffer lowest-buf)
3538              (switch-to-buffer nntp-server-buffer)))
3539       (while bufs
3540         (and (not (eq (car bufs) lowest-buf))
3541              (delete-windows-on (car bufs)))
3542         (setq bufs (cdr bufs))))))
3543
3544 (defun gnus-version (&optional arg)
3545   "Version number of this version of Gnus.
3546 If ARG, insert string at point."
3547   (interactive "P")
3548   (let ((methods gnus-valid-select-methods)
3549         (mess gnus-version)
3550         meth)
3551     ;; Go through all the legal select methods and add their version
3552     ;; numbers to the total version string.  Only the backends that are
3553     ;; currently in use will have their message numbers taken into
3554     ;; consideration.
3555     (while methods
3556       (setq meth (intern (concat (caar methods) "-version")))
3557       (and (boundp meth)
3558            (stringp (symbol-value meth))
3559            (setq mess (concat mess "; " (symbol-value meth))))
3560       (setq methods (cdr methods)))
3561     (if arg
3562         (insert (message mess))
3563       (message mess))))
3564
3565 (defun gnus-info-find-node ()
3566   "Find Info documentation of Gnus."
3567   (interactive)
3568   ;; Enlarge info window if needed.
3569   (let ((mode major-mode)
3570         gnus-info-buffer)
3571     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3572     (setq gnus-info-buffer (current-buffer))
3573     (gnus-configure-windows 'info)))
3574
3575 (defun gnus-days-between (date1 date2)
3576   ;; Return the number of days between date1 and date2.
3577   (- (gnus-day-number date1) (gnus-day-number date2)))
3578
3579 (defun gnus-day-number (date)
3580   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3581                      (timezone-parse-date date))))
3582     (timezone-absolute-from-gregorian
3583      (nth 1 dat) (nth 2 dat) (car dat))))
3584
3585 (defun gnus-encode-date (date)
3586   "Convert DATE to internal time."
3587   (let* ((parse (timezone-parse-date date))
3588          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3589          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3590     (encode-time (caddr time) (cadr time) (car time)
3591                  (caddr date) (cadr date) (car date) (nth 4 date))))
3592
3593 (defun gnus-time-minus (t1 t2)
3594   "Subtract two internal times."
3595   (let ((borrow (< (cadr t1) (cadr t2))))
3596     (list (- (car t1) (car t2) (if borrow 1 0))
3597           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3598
3599 (defun gnus-file-newer-than (file date)
3600   (let ((fdate (nth 5 (file-attributes file))))
3601     (or (> (car fdate) (car date))
3602         (and (= (car fdate) (car date))
3603              (> (nth 1 fdate) (nth 1 date))))))
3604
3605 (defmacro gnus-local-set-keys (&rest plist)
3606   "Set the keys in PLIST in the current keymap."
3607   `(gnus-define-keys-1 (current-local-map) ',plist))
3608
3609 (defmacro gnus-define-keys (keymap &rest plist)
3610   "Define all keys in PLIST in KEYMAP."
3611   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3612
3613 (put 'gnus-define-keys 'lisp-indent-function 1)
3614 (put 'gnus-define-keys 'lisp-indent-hook 1)
3615 (put 'gnus-define-keymap 'lisp-indent-function 1)
3616 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3617
3618 (defmacro gnus-define-keymap (keymap &rest plist)
3619   "Define all keys in PLIST in KEYMAP."
3620   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3621
3622 (defun gnus-define-keys-1 (keymap plist)
3623   (when (null keymap)
3624     (error "Can't set keys in a null keymap"))
3625   (cond ((symbolp keymap)
3626          (setq keymap (symbol-value keymap)))
3627         ((keymapp keymap))
3628         ((listp keymap)
3629          (set (car keymap) nil)
3630          (define-prefix-command (car keymap))
3631          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3632          (setq keymap (symbol-value (car keymap)))))
3633   (let (key)
3634     (while plist
3635       (when (symbolp (setq key (pop plist)))
3636         (setq key (symbol-value key)))
3637       (define-key keymap key (pop plist)))))
3638
3639 (defun gnus-group-read-only-p (&optional group)
3640   "Check whether GROUP supports editing or not.
3641 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3642 that that variable is buffer-local to the summary buffers."
3643   (let ((group (or group gnus-newsgroup-name)))
3644     (not (gnus-check-backend-function 'request-replace-article group))))
3645
3646 (defun gnus-group-total-expirable-p (group)
3647   "Check whether GROUP is total-expirable or not."
3648   (let ((params (gnus-info-params (gnus-get-info group))))
3649     (or (memq 'total-expire params)
3650         (cdr (assq 'total-expire params)) ; (total-expire . t)
3651         (and gnus-total-expirable-newsgroups ; Check var.
3652              (string-match gnus-total-expirable-newsgroups group)))))
3653
3654 (defun gnus-group-auto-expirable-p (group)
3655   "Check whether GROUP is total-expirable or not."
3656   (let ((params (gnus-info-params (gnus-get-info group))))
3657     (or (memq 'auto-expire params)
3658         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3659         (and gnus-auto-expirable-newsgroups ; Check var.
3660              (string-match gnus-auto-expirable-newsgroups group)))))
3661
3662 (defun gnus-virtual-group-p (group)
3663   "Say whether GROUP is virtual or not."
3664   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3665                         gnus-valid-select-methods)))
3666
3667 (defun gnus-news-group-p (group &optional article)
3668   "Return non-nil if GROUP (and ARTICLE) come from a news server."
3669   (or (gnus-member-of-valid 'post group) ; Ordinary news group.
3670       (and (gnus-member-of-valid 'post-mail group) ; Combined group.
3671            (eq (gnus-request-type group article) 'news))))
3672
3673 (defsubst gnus-simplify-subject-fully (subject)
3674   "Simplify a subject string according to the user's wishes."
3675   (cond
3676    ((null gnus-summary-gather-subject-limit)
3677     (gnus-simplify-subject-re subject))
3678    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3679     (gnus-simplify-subject-fuzzy subject))
3680    ((numberp gnus-summary-gather-subject-limit)
3681     (gnus-limit-string (gnus-simplify-subject-re subject)
3682                        gnus-summary-gather-subject-limit))
3683    (t
3684     subject)))
3685
3686 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3687   "Check whether two subjects are equal.  If optional argument
3688 simple-first is t, first argument is already simplified."
3689   (cond
3690    ((null simple-first)
3691     (equal (gnus-simplify-subject-fully s1)
3692            (gnus-simplify-subject-fully s2)))
3693    (t
3694     (equal s1
3695            (gnus-simplify-subject-fully s2)))))
3696
3697 ;; Returns a list of writable groups.
3698 (defun gnus-writable-groups ()
3699   (let ((alist gnus-newsrc-alist)
3700         groups group)
3701     (while (setq group (car (pop alist)))
3702       (unless (gnus-group-read-only-p group)
3703         (push group groups)))
3704     (nreverse groups)))
3705
3706 (defun gnus-completing-read (default prompt &rest args)
3707   ;; Like `completing-read', except that DEFAULT is the default argument.
3708   (let* ((prompt (if default 
3709                      (concat prompt " (default " default ") ")
3710                    (concat prompt " ")))
3711          (answer (apply 'completing-read prompt args)))
3712     (if (or (null answer) (zerop (length answer)))
3713         default
3714       answer)))
3715
3716 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3717 ;; the echo area.
3718 (defun gnus-y-or-n-p (prompt)
3719   (prog1
3720       (y-or-n-p prompt)
3721     (message "")))
3722
3723 (defun gnus-yes-or-no-p (prompt)
3724   (prog1
3725       (yes-or-no-p prompt)
3726     (message "")))
3727
3728 ;; Check whether to use long file names.
3729 (defun gnus-use-long-file-name (symbol)
3730   ;; The variable has to be set...
3731   (and gnus-use-long-file-name
3732        ;; If it isn't a list, then we return t.
3733        (or (not (listp gnus-use-long-file-name))
3734            ;; If it is a list, and the list contains `symbol', we
3735            ;; return nil.
3736            (not (memq symbol gnus-use-long-file-name)))))
3737
3738 ;; I suspect there's a better way, but I haven't taken the time to do
3739 ;; it yet. -erik selberg@cs.washington.edu
3740 (defun gnus-dd-mmm (messy-date)
3741   "Return a string like DD-MMM from a big messy string"
3742   (let ((datevec (condition-case () (timezone-parse-date messy-date) 
3743                    (error nil))))
3744     (if (not datevec)
3745         "??-???"
3746       (format "%2s-%s"
3747               (condition-case ()
3748                   ;; Make sure leading zeroes are stripped.
3749                   (number-to-string (string-to-number (aref datevec 2)))
3750                 (error "??"))
3751               (capitalize
3752                (or (car
3753                     (nth (1- (string-to-number (aref datevec 1)))
3754                          timezone-months-assoc))
3755                    "???"))))))
3756
3757 (defun gnus-mode-string-quote (string)
3758   "Quote all \"%\" in STRING."
3759   (save-excursion
3760     (gnus-set-work-buffer)
3761     (insert string)
3762     (goto-char (point-min))
3763     (while (search-forward "%" nil t)
3764       (insert "%"))
3765     (buffer-string)))
3766
3767 ;; Make a hash table (default and minimum size is 255).
3768 ;; Optional argument HASHSIZE specifies the table size.
3769 (defun gnus-make-hashtable (&optional hashsize)
3770   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3771
3772 ;; Make a number that is suitable for hashing; bigger than MIN and one
3773 ;; less than 2^x.
3774 (defun gnus-create-hash-size (min)
3775   (let ((i 1))
3776     (while (< i min)
3777       (setq i (* 2 i)))
3778     (1- i)))
3779
3780 ;; Show message if message has a lower level than `gnus-verbose'.
3781 ;; Guideline for numbers:
3782 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3783 ;; for things that take a long time, 7 - not very important messages
3784 ;; on stuff, 9 - messages inside loops.
3785 (defun gnus-message (level &rest args)
3786   (if (<= level gnus-verbose)
3787       (apply 'message args)
3788     ;; We have to do this format thingy here even if the result isn't
3789     ;; shown - the return value has to be the same as the return value
3790     ;; from `message'.
3791     (apply 'format args)))
3792
3793 (defun gnus-error (level &rest args)
3794   "Beep an error if `gnus-verbose' is on LEVEL or less."
3795   (when (<= (floor level) gnus-verbose)
3796     (apply 'message args)
3797     (ding)
3798     (let (duration)
3799       (when (and (floatp level)
3800                  (not (zerop (setq duration (* 10 (- level (floor level)))))))
3801         (sit-for duration))))
3802   nil)
3803
3804 ;; Generate a unique new group name.
3805 (defun gnus-generate-new-group-name (leaf)
3806   (let ((name leaf)
3807         (num 0))
3808     (while (gnus-gethash name gnus-newsrc-hashtb)
3809       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3810     name))
3811
3812 (defsubst gnus-hide-text (b e props)
3813   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
3814   (gnus-add-text-properties b e props)
3815   (when (memq 'intangible props)
3816     (gnus-put-text-property (max (1- b) (point-min))
3817                        b 'intangible (cddr (memq 'intangible props)))))
3818
3819 (defsubst gnus-unhide-text (b e)
3820   "Remove hidden text properties from region between B and E."
3821   (remove-text-properties b e gnus-hidden-properties)
3822   (when (memq 'intangible gnus-hidden-properties)
3823     (gnus-put-text-property (max (1- b) (point-min))
3824                             b 'intangible nil)))
3825
3826 (defun gnus-hide-text-type (b e type)
3827   "Hide text of TYPE between B and E."
3828   (gnus-hide-text b e (cons 'gnus-type (cons type gnus-hidden-properties))))
3829
3830 (defun gnus-parent-headers (headers &optional generation)
3831   "Return the headers of the GENERATIONeth parent of HEADERS."
3832   (unless generation 
3833     (setq generation 1))
3834   (let (references parent)
3835     (while (and headers (not (zerop generation)))
3836       (setq references (mail-header-references headers))
3837       (when (and references
3838                  (setq parent (gnus-parent-id references))
3839                  (setq headers (car (gnus-id-to-thread parent))))
3840         (decf generation)))
3841     headers))
3842
3843 (defun gnus-parent-id (references)
3844   "Return the last Message-ID in REFERENCES."
3845   (when (and references
3846              (string-match "\\(<[^\n<>]+>\\)[ \t\n]*\\'" references))
3847     (substring references (match-beginning 1) (match-end 1))))
3848
3849 (defun gnus-split-references (references)
3850   "Return a list of Message-IDs in REFERENCES."
3851   (let ((beg 0)
3852         ids)
3853     (while (string-match "<[^>]+>" references beg)
3854       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3855             ids))
3856     (nreverse ids)))
3857
3858 (defun gnus-buffer-live-p (buffer)
3859   "Say whether BUFFER is alive or not."
3860   (and buffer
3861        (get-buffer buffer)
3862        (buffer-name (get-buffer buffer))))
3863
3864 (defun gnus-ephemeral-group-p (group)
3865   "Say whether GROUP is ephemeral or not."
3866   (gnus-group-get-parameter group 'quit-config))
3867
3868 (defun gnus-group-quit-config (group)
3869   "Return the quit-config of GROUP."
3870   (gnus-group-get-parameter group 'quit-config))
3871
3872 (defun gnus-simplify-mode-line ()
3873   "Make mode lines a bit simpler."
3874   (setq mode-line-modified "-- ")
3875   (when (listp mode-line-format)
3876     (make-local-variable 'mode-line-format)
3877     (setq mode-line-format (copy-sequence mode-line-format))
3878     (when (equal (nth 3 mode-line-format) "   ")
3879       (setcar (nthcdr 3 mode-line-format) " "))))
3880
3881 ;;; List and range functions
3882
3883 (defun gnus-last-element (list)
3884   "Return last element of LIST."
3885   (while (cdr list)
3886     (setq list (cdr list)))
3887   (car list))
3888
3889 (defun gnus-copy-sequence (list)
3890   "Do a complete, total copy of a list."
3891   (if (and (consp list) (not (consp (cdr list))))
3892       (cons (car list) (cdr list))
3893     (mapcar (lambda (elem) (if (consp elem)
3894                                (if (consp (cdr elem))
3895                                    (gnus-copy-sequence elem)
3896                                  (cons (car elem) (cdr elem)))
3897                              elem))
3898             list)))
3899
3900 (defun gnus-set-difference (list1 list2)
3901   "Return a list of elements of LIST1 that do not appear in LIST2."
3902   (let ((list1 (copy-sequence list1)))
3903     (while list2
3904       (setq list1 (delq (car list2) list1))
3905       (setq list2 (cdr list2)))
3906     list1))
3907
3908 (defun gnus-sorted-complement (list1 list2)
3909   "Return a list of elements of LIST1 that do not appear in LIST2.
3910 Both lists have to be sorted over <."
3911   (let (out)
3912     (if (or (null list1) (null list2))
3913         (or list1 list2)
3914       (while (and list1 list2)
3915         (cond ((= (car list1) (car list2))
3916                (setq list1 (cdr list1)
3917                      list2 (cdr list2)))
3918               ((< (car list1) (car list2))
3919                (setq out (cons (car list1) out))
3920                (setq list1 (cdr list1)))
3921               (t
3922                (setq out (cons (car list2) out))
3923                (setq list2 (cdr list2)))))
3924       (nconc (nreverse out) (or list1 list2)))))
3925
3926 (defun gnus-intersection (list1 list2)
3927   (let ((result nil))
3928     (while list2
3929       (if (memq (car list2) list1)
3930           (setq result (cons (car list2) result)))
3931       (setq list2 (cdr list2)))
3932     result))
3933
3934 (defun gnus-sorted-intersection (list1 list2)
3935   ;; LIST1 and LIST2 have to be sorted over <.
3936   (let (out)
3937     (while (and list1 list2)
3938       (cond ((= (car list1) (car list2))
3939              (setq out (cons (car list1) out)
3940                    list1 (cdr list1)
3941                    list2 (cdr list2)))
3942             ((< (car list1) (car list2))
3943              (setq list1 (cdr list1)))
3944             (t
3945              (setq list2 (cdr list2)))))
3946     (nreverse out)))
3947
3948 (defun gnus-set-sorted-intersection (list1 list2)
3949   ;; LIST1 and LIST2 have to be sorted over <.
3950   ;; This function modifies LIST1.
3951   (let* ((top (cons nil list1))
3952          (prev top))
3953     (while (and list1 list2)
3954       (cond ((= (car list1) (car list2))
3955              (setq prev list1
3956                    list1 (cdr list1)
3957                    list2 (cdr list2)))
3958             ((< (car list1) (car list2))
3959              (setcdr prev (cdr list1))
3960              (setq list1 (cdr list1)))
3961             (t
3962              (setq list2 (cdr list2)))))
3963     (setcdr prev nil)
3964     (cdr top)))
3965
3966 (defun gnus-compress-sequence (numbers &optional always-list)
3967   "Convert list of numbers to a list of ranges or a single range.
3968 If ALWAYS-LIST is non-nil, this function will always release a list of
3969 ranges."
3970   (let* ((first (car numbers))
3971          (last (car numbers))
3972          result)
3973     (if (null numbers)
3974         nil
3975       (if (not (listp (cdr numbers)))
3976           numbers
3977         (while numbers
3978           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3979                 ((= (1+ last) (car numbers)) ;Still in sequence
3980                  (setq last (car numbers)))
3981                 (t                      ;End of one sequence
3982                  (setq result
3983                        (cons (if (= first last) first
3984                                (cons first last)) result))
3985                  (setq first (car numbers))
3986                  (setq last  (car numbers))))
3987           (setq numbers (cdr numbers)))
3988         (if (and (not always-list) (null result))
3989             (if (= first last) (list first) (cons first last))
3990           (nreverse (cons (if (= first last) first (cons first last))
3991                           result)))))))
3992
3993 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3994 (defun gnus-uncompress-range (ranges)
3995   "Expand a list of ranges into a list of numbers.
3996 RANGES is either a single range on the form `(num . num)' or a list of
3997 these ranges."
3998   (let (first last result)
3999     (cond
4000      ((null ranges)
4001       nil)
4002      ((not (listp (cdr ranges)))
4003       (setq first (car ranges))
4004       (setq last (cdr ranges))
4005       (while (<= first last)
4006         (setq result (cons first result))
4007         (setq first (1+ first)))
4008       (nreverse result))
4009      (t
4010       (while ranges
4011         (if (atom (car ranges))
4012             (if (numberp (car ranges))
4013                 (setq result (cons (car ranges) result)))
4014           (setq first (caar ranges))
4015           (setq last  (cdar ranges))
4016           (while (<= first last)
4017             (setq result (cons first result))
4018             (setq first (1+ first))))
4019         (setq ranges (cdr ranges)))
4020       (nreverse result)))))
4021
4022 (defun gnus-add-to-range (ranges list)
4023   "Return a list of ranges that has all articles from both RANGES and LIST.
4024 Note: LIST has to be sorted over `<'."
4025   (if (not ranges)
4026       (gnus-compress-sequence list t)
4027     (setq list (copy-sequence list))
4028     (or (listp (cdr ranges))
4029         (setq ranges (list ranges)))
4030     (let ((out ranges)
4031           ilist lowest highest temp)
4032       (while (and ranges list)
4033         (setq ilist list)
4034         (setq lowest (or (and (atom (car ranges)) (car ranges))
4035                          (caar ranges)))
4036         (while (and list (cdr list) (< (cadr list) lowest))
4037           (setq list (cdr list)))
4038         (if (< (car ilist) lowest)
4039             (progn
4040               (setq temp list)
4041               (setq list (cdr list))
4042               (setcdr temp nil)
4043               (setq out (nconc (gnus-compress-sequence ilist t) out))))
4044         (setq highest (or (and (atom (car ranges)) (car ranges))
4045                           (cdar ranges)))
4046         (while (and list (<= (car list) highest))
4047           (setq list (cdr list)))
4048         (setq ranges (cdr ranges)))
4049       (if list
4050           (setq out (nconc (gnus-compress-sequence list t) out)))
4051       (setq out (sort out (lambda (r1 r2)
4052                             (< (or (and (atom r1) r1) (car r1))
4053                                (or (and (atom r2) r2) (car r2))))))
4054       (setq ranges out)
4055       (while ranges
4056         (if (atom (car ranges))
4057             (if (cdr ranges)
4058                 (if (atom (cadr ranges))
4059                     (if (= (1+ (car ranges)) (cadr ranges))
4060                         (progn
4061                           (setcar ranges (cons (car ranges)
4062                                                (cadr ranges)))
4063                           (setcdr ranges (cddr ranges))))
4064                   (if (= (1+ (car ranges)) (caadr ranges))
4065                       (progn
4066                         (setcar (cadr ranges) (car ranges))
4067                         (setcar ranges (cadr ranges))
4068                         (setcdr ranges (cddr ranges))))))
4069           (if (cdr ranges)
4070               (if (atom (cadr ranges))
4071                   (if (= (1+ (cdar ranges)) (cadr ranges))
4072                       (progn
4073                         (setcdr (car ranges) (cadr ranges))
4074                         (setcdr ranges (cddr ranges))))
4075                 (if (= (1+ (cdar ranges)) (caadr ranges))
4076                     (progn
4077                       (setcdr (car ranges) (cdadr ranges))
4078                       (setcdr ranges (cddr ranges)))))))
4079         (setq ranges (cdr ranges)))
4080       out)))
4081
4082 (defun gnus-remove-from-range (ranges list)
4083   "Return a list of ranges that has all articles from LIST removed from RANGES.
4084 Note: LIST has to be sorted over `<'."
4085   ;; !!! This function shouldn't look like this, but I've got a headache.
4086   (gnus-compress-sequence
4087    (gnus-sorted-complement
4088     (gnus-uncompress-range ranges) list)))
4089
4090 (defun gnus-member-of-range (number ranges)
4091   (if (not (listp (cdr ranges)))
4092       (and (>= number (car ranges))
4093            (<= number (cdr ranges)))
4094     (let ((not-stop t))
4095       (while (and ranges
4096                   (if (numberp (car ranges))
4097                       (>= number (car ranges))
4098                     (>= number (caar ranges)))
4099                   not-stop)
4100         (if (if (numberp (car ranges))
4101                 (= number (car ranges))
4102               (and (>= number (caar ranges))
4103                    (<= number (cdar ranges))))
4104             (setq not-stop nil))
4105         (setq ranges (cdr ranges)))
4106       (not not-stop))))
4107
4108 (defun gnus-range-length (range)
4109   "Return the length RANGE would have if uncompressed."
4110   (length (gnus-uncompress-range range)))
4111
4112 (defun gnus-sublist-p (list sublist)
4113   "Test whether all elements in SUBLIST are members of LIST."
4114   (let ((sublistp t))
4115     (while sublist
4116       (unless (memq (pop sublist) list)
4117         (setq sublistp nil
4118               sublist nil)))
4119     sublistp))
4120
4121 \f
4122 ;;;
4123 ;;; Gnus group mode
4124 ;;;
4125
4126 (defvar gnus-group-mode-map nil)
4127 (put 'gnus-group-mode 'mode-class 'special)
4128
4129 (unless gnus-group-mode-map
4130   (setq gnus-group-mode-map (make-keymap))
4131   (suppress-keymap gnus-group-mode-map)
4132
4133   (gnus-define-keys gnus-group-mode-map
4134     " " gnus-group-read-group
4135     "=" gnus-group-select-group
4136     "\r" gnus-group-select-group
4137     "\M-\r" gnus-group-quick-select-group
4138     "j" gnus-group-jump-to-group
4139     "n" gnus-group-next-unread-group
4140     "p" gnus-group-prev-unread-group
4141     "\177" gnus-group-prev-unread-group
4142     [delete] gnus-group-prev-unread-group
4143     "N" gnus-group-next-group
4144     "P" gnus-group-prev-group
4145     "\M-n" gnus-group-next-unread-group-same-level
4146     "\M-p" gnus-group-prev-unread-group-same-level
4147     "," gnus-group-best-unread-group
4148     "." gnus-group-first-unread-group
4149     "u" gnus-group-unsubscribe-current-group
4150     "U" gnus-group-unsubscribe-group
4151     "c" gnus-group-catchup-current
4152     "C" gnus-group-catchup-current-all
4153     "l" gnus-group-list-groups
4154     "L" gnus-group-list-all-groups
4155     "m" gnus-group-mail
4156     "g" gnus-group-get-new-news
4157     "\M-g" gnus-group-get-new-news-this-group
4158     "R" gnus-group-restart
4159     "r" gnus-group-read-init-file
4160     "B" gnus-group-browse-foreign-server
4161     "b" gnus-group-check-bogus-groups
4162     "F" gnus-find-new-newsgroups
4163     "\C-c\C-d" gnus-group-describe-group
4164     "\M-d" gnus-group-describe-all-groups
4165     "\C-c\C-a" gnus-group-apropos
4166     "\C-c\M-\C-a" gnus-group-description-apropos
4167     "a" gnus-group-post-news
4168     "\ek" gnus-group-edit-local-kill
4169     "\eK" gnus-group-edit-global-kill
4170     "\C-k" gnus-group-kill-group
4171     "\C-y" gnus-group-yank-group
4172     "\C-w" gnus-group-kill-region
4173     "\C-x\C-t" gnus-group-transpose-groups
4174     "\C-c\C-l" gnus-group-list-killed
4175     "\C-c\C-x" gnus-group-expire-articles
4176     "\C-c\M-\C-x" gnus-group-expire-all-groups
4177     "V" gnus-version
4178     "s" gnus-group-save-newsrc
4179     "z" gnus-group-suspend
4180     "Z" gnus-group-clear-dribble
4181     "q" gnus-group-exit
4182     "Q" gnus-group-quit
4183     "?" gnus-group-describe-briefly
4184     "\C-c\C-i" gnus-info-find-node
4185     "\M-e" gnus-group-edit-group-method
4186     "^" gnus-group-enter-server-mode
4187     gnus-mouse-2 gnus-mouse-pick-group
4188     "<" beginning-of-buffer
4189     ">" end-of-buffer
4190     "\C-c\C-b" gnus-bug
4191     "\C-c\C-s" gnus-group-sort-groups
4192     "t" gnus-topic-mode
4193     "\C-c\M-g" gnus-activate-all-groups
4194     "\M-&" gnus-group-universal-argument
4195     "#" gnus-group-mark-group
4196     "\M-#" gnus-group-unmark-group)
4197
4198   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4199     "m" gnus-group-mark-group
4200     "u" gnus-group-unmark-group
4201     "w" gnus-group-mark-region
4202     "m" gnus-group-mark-buffer
4203     "r" gnus-group-mark-regexp
4204     "U" gnus-group-unmark-all-groups)
4205
4206   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4207     "d" gnus-group-make-directory-group
4208     "h" gnus-group-make-help-group
4209     "a" gnus-group-make-archive-group
4210     "k" gnus-group-make-kiboze-group
4211     "m" gnus-group-make-group
4212     "E" gnus-group-edit-group
4213     "e" gnus-group-edit-group-method
4214     "p" gnus-group-edit-group-parameters
4215     "v" gnus-group-add-to-virtual
4216     "V" gnus-group-make-empty-virtual
4217     "D" gnus-group-enter-directory
4218     "f" gnus-group-make-doc-group
4219     "r" gnus-group-rename-group
4220     "\177" gnus-group-delete-group
4221     [delete] gnus-group-delete-group)
4222
4223    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4224      "b" gnus-group-brew-soup
4225      "w" gnus-soup-save-areas
4226      "s" gnus-soup-send-replies
4227      "p" gnus-soup-pack-packet
4228      "r" nnsoup-pack-replies)
4229
4230    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4231      "s" gnus-group-sort-groups
4232      "a" gnus-group-sort-groups-by-alphabet
4233      "u" gnus-group-sort-groups-by-unread
4234      "l" gnus-group-sort-groups-by-level
4235      "v" gnus-group-sort-groups-by-score
4236      "r" gnus-group-sort-groups-by-rank
4237      "m" gnus-group-sort-groups-by-method)
4238
4239    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4240      "k" gnus-group-list-killed
4241      "z" gnus-group-list-zombies
4242      "s" gnus-group-list-groups
4243      "u" gnus-group-list-all-groups
4244      "A" gnus-group-list-active
4245      "a" gnus-group-apropos
4246      "d" gnus-group-description-apropos
4247      "m" gnus-group-list-matching
4248      "M" gnus-group-list-all-matching
4249      "l" gnus-group-list-level)
4250
4251    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4252      "f" gnus-score-flush-cache)
4253
4254    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4255      "f" gnus-group-fetch-faq)
4256
4257    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4258      "l" gnus-group-set-current-level
4259      "t" gnus-group-unsubscribe-current-group
4260      "s" gnus-group-unsubscribe-group
4261      "k" gnus-group-kill-group
4262      "y" gnus-group-yank-group
4263      "w" gnus-group-kill-region
4264      "\C-k" gnus-group-kill-level
4265      "z" gnus-group-kill-all-zombies))
4266
4267 (defun gnus-group-mode ()
4268   "Major mode for reading news.
4269
4270 All normal editing commands are switched off.
4271 \\<gnus-group-mode-map>
4272 The group buffer lists (some of) the groups available.  For instance,
4273 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4274 lists all zombie groups.
4275
4276 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4277 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4278
4279 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4280
4281 The following commands are available:
4282
4283 \\{gnus-group-mode-map}"
4284   (interactive)
4285   (when (and menu-bar-mode
4286              (gnus-visual-p 'group-menu 'menu))
4287     (gnus-group-make-menu-bar))
4288   (kill-all-local-variables)
4289   (gnus-simplify-mode-line)
4290   (setq major-mode 'gnus-group-mode)
4291   (setq mode-name "Group")
4292   (gnus-group-set-mode-line)
4293   (setq mode-line-process nil)
4294   (use-local-map gnus-group-mode-map)
4295   (buffer-disable-undo (current-buffer))
4296   (setq truncate-lines t)
4297   (setq buffer-read-only t)
4298   (gnus-make-local-hook 'post-command-hook)
4299   (gnus-add-hook 'post-command-hook 'gnus-clear-inboxes-moved nil t)
4300   (run-hooks 'gnus-group-mode-hook))
4301
4302 (defun gnus-clear-inboxes-moved ()
4303   (setq nnmail-moved-inboxes nil))
4304
4305 (defun gnus-mouse-pick-group (e)
4306   "Enter the group under the mouse pointer."
4307   (interactive "e")
4308   (mouse-set-point e)
4309   (gnus-group-read-group nil))
4310
4311 ;; Look at LEVEL and find out what the level is really supposed to be.
4312 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4313 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4314 (defun gnus-group-default-level (&optional level number-or-nil)
4315   (cond
4316    (gnus-group-use-permanent-levels
4317     (or (setq gnus-group-use-permanent-levels
4318               (or level (if (numberp gnus-group-use-permanent-levels)
4319                             gnus-group-use-permanent-levels
4320                           (or gnus-group-default-list-level
4321                               gnus-level-subscribed))))
4322         gnus-group-default-list-level gnus-level-subscribed))
4323    (number-or-nil
4324     level)
4325    (t
4326     (or level gnus-group-default-list-level gnus-level-subscribed))))
4327
4328 ;;;###autoload
4329 (defun gnus-slave-no-server (&optional arg)
4330   "Read network news as a slave, without connecting to local server"
4331   (interactive "P")
4332   (gnus-no-server arg t))
4333
4334 ;;;###autoload
4335 (defun gnus-no-server (&optional arg slave)
4336   "Read network news.
4337 If ARG is a positive number, Gnus will use that as the
4338 startup level.  If ARG is nil, Gnus will be started at level 2.
4339 If ARG is non-nil and not a positive number, Gnus will
4340 prompt the user for the name of an NNTP server to use.
4341 As opposed to `gnus', this command will not connect to the local server."
4342   (interactive "P")
4343   (let ((val (or arg (1- gnus-level-default-subscribed))))
4344     (gnus val t slave)
4345     (make-local-variable 'gnus-group-use-permanent-levels)
4346     (setq gnus-group-use-permanent-levels val)))
4347
4348 ;;;###autoload
4349 (defun gnus-slave (&optional arg)
4350   "Read news as a slave."
4351   (interactive "P")
4352   (gnus arg nil 'slave))
4353
4354 ;;;###autoload
4355 (defun gnus-other-frame (&optional arg)
4356   "Pop up a frame to read news."
4357   (interactive "P")
4358   (if (get-buffer gnus-group-buffer)
4359       (let ((pop-up-frames t))
4360         (gnus arg))
4361     (select-frame (make-frame))
4362     (gnus arg)))
4363
4364 ;;;###autoload
4365 (defun gnus (&optional arg dont-connect slave)
4366   "Read network news.
4367 If ARG is non-nil and a positive number, Gnus will use that as the
4368 startup level.  If ARG is non-nil and not a positive number, Gnus will
4369 prompt the user for the name of an NNTP server to use."
4370   (interactive "P")
4371
4372   (if (get-buffer gnus-group-buffer)
4373       (progn
4374         (switch-to-buffer gnus-group-buffer)
4375         (gnus-group-get-new-news))
4376
4377     (gnus-clear-system)
4378     (nnheader-init-server-buffer)
4379     (gnus-read-init-file)
4380     (setq gnus-slave slave)
4381
4382     (gnus-group-setup-buffer)
4383     (let ((buffer-read-only nil))
4384       (erase-buffer)
4385       (if (not gnus-inhibit-startup-message)
4386           (progn
4387             (gnus-group-startup-message)
4388             (sit-for 0))))
4389
4390     (let ((level (and (numberp arg) (> arg 0) arg))
4391           did-connect)
4392       (unwind-protect
4393           (progn
4394             (or dont-connect
4395                 (setq did-connect
4396                       (gnus-start-news-server (and arg (not level))))))
4397         (if (and (not dont-connect)
4398                  (not did-connect))
4399             (gnus-group-quit)
4400           (run-hooks 'gnus-startup-hook)
4401           ;; NNTP server is successfully open.
4402
4403           ;; Find the current startup file name.
4404           (setq gnus-current-startup-file
4405                 (gnus-make-newsrc-file gnus-startup-file))
4406
4407           ;; Read the dribble file.
4408           (when (or gnus-slave gnus-use-dribble-file)
4409             (gnus-dribble-read-file))
4410
4411           ;; Allow using GroupLens predictions.
4412           (when gnus-use-grouplens
4413             (bbb-login)
4414             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4415
4416           (gnus-summary-make-display-table)
4417           ;; Do the actual startup.
4418           (gnus-setup-news nil level dont-connect)
4419           ;; Generate the group buffer.
4420           (gnus-group-list-groups level)
4421           (gnus-group-first-unread-group)
4422           (gnus-configure-windows 'group)
4423           (gnus-group-set-mode-line))))))
4424
4425 (defun gnus-unload ()
4426   "Unload all Gnus features."
4427   (interactive)
4428   (or (boundp 'load-history)
4429       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4430   (let ((history load-history)
4431         feature)
4432     (while history
4433       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4434            (setq feature (cdr (assq 'provide (car history))))
4435            (unload-feature feature 'force))
4436       (setq history (cdr history)))))
4437
4438 (defun gnus-compile ()
4439   "Byte-compile the user-defined format specs."
4440   (interactive)
4441   (let ((entries gnus-format-specs)
4442         entry gnus-tmp-func)
4443     (save-excursion
4444       (gnus-message 7 "Compiling format specs...")
4445
4446       (while entries
4447         (setq entry (pop entries))
4448         (if (eq (car entry) 'version)
4449             (setq gnus-format-specs (delq entry gnus-format-specs))
4450           (when (and (listp (caddr entry))
4451                      (not (eq 'byte-code (caaddr entry))))
4452             (fset 'gnus-tmp-func
4453                   `(lambda () ,(caddr entry)))
4454             (byte-compile 'gnus-tmp-func)
4455             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4456
4457       (push (cons 'version emacs-version) gnus-format-specs)
4458
4459       (gnus-message 7 "Compiling user specs...done"))))
4460
4461 (defun gnus-indent-rigidly (start end arg)
4462   "Indent rigidly using only spaces and no tabs."
4463   (save-excursion
4464     (save-restriction
4465       (narrow-to-region start end)
4466       (indent-rigidly start end arg)
4467       (goto-char (point-min))
4468       (while (search-forward "\t" nil t)
4469         (replace-match "        " t t)))))
4470
4471 (defun gnus-group-startup-message (&optional x y)
4472   "Insert startup message in current buffer."
4473   ;; Insert the message.
4474   (erase-buffer)
4475   (insert
4476    (format "              %s
4477           _    ___ _             _
4478           _ ___ __ ___  __    _ ___
4479           __   _     ___    __  ___
4480               _           ___     _
4481              _  _ __             _
4482              ___   __            _
4483                    __           _
4484                     _      _   _
4485                    _      _    _
4486                       _  _    _
4487                   __  ___
4488                  _   _ _     _
4489                 _   _
4490               _    _
4491              _    _
4492             _
4493           __
4494
4495 "
4496            ""))
4497   ;; And then hack it.
4498   (gnus-indent-rigidly (point-min) (point-max)
4499                        (/ (max (- (window-width) (or x 46)) 0) 2))
4500   (goto-char (point-min))
4501   (forward-line 1)
4502   (let* ((pheight (count-lines (point-min) (point-max)))
4503          (wheight (window-height))
4504          (rest (- wheight pheight)))
4505     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4506   ;; Fontify some.
4507   (goto-char (point-min))
4508   (and (search-forward "Praxis" nil t)
4509        (gnus-put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4510   (goto-char (point-min))
4511   (let* ((mode-string (gnus-group-set-mode-line)))
4512     (setq mode-line-buffer-identification
4513           (list (concat gnus-version (substring (car mode-string) 4))))
4514     (set-buffer-modified-p t)))
4515
4516 (defun gnus-group-setup-buffer ()
4517   (or (get-buffer gnus-group-buffer)
4518       (progn
4519         (switch-to-buffer gnus-group-buffer)
4520         (gnus-add-current-to-buffer-list)
4521         (gnus-group-mode)
4522         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4523
4524 (defun gnus-group-list-groups (&optional level unread lowest)
4525   "List newsgroups with level LEVEL or lower that have unread articles.
4526 Default is all subscribed groups.
4527 If argument UNREAD is non-nil, groups with no unread articles are also
4528 listed."
4529   (interactive (list (if current-prefix-arg
4530                          (prefix-numeric-value current-prefix-arg)
4531                        (or
4532                         (gnus-group-default-level nil t)
4533                         gnus-group-default-list-level
4534                         gnus-level-subscribed))))
4535   (or level
4536       (setq level (car gnus-group-list-mode)
4537             unread (cdr gnus-group-list-mode)))
4538   (setq level (gnus-group-default-level level))
4539   (gnus-group-setup-buffer)             ;May call from out of group buffer
4540   (gnus-update-format-specifications)
4541   (let ((case-fold-search nil)
4542         (props (text-properties-at (gnus-point-at-bol)))
4543         (group (gnus-group-group-name)))
4544     (set-buffer gnus-group-buffer)
4545     (funcall gnus-group-prepare-function level unread lowest)
4546     (if (zerop (buffer-size))
4547         (gnus-message 5 gnus-no-groups-message)
4548       (goto-char (point-max))
4549       (when (or (not gnus-group-goto-next-group-function)
4550                 (not (funcall gnus-group-goto-next-group-function 
4551                               group props)))
4552         (if (not group)
4553             ;; Go to the first group with unread articles.
4554             (gnus-group-search-forward t)
4555           ;; Find the right group to put point on.  If the current group
4556           ;; has disappeared in the new listing, try to find the next
4557           ;; one.        If no next one can be found, just leave point at the
4558           ;; first newsgroup in the buffer.
4559           (if (not (gnus-goto-char
4560                     (text-property-any
4561                      (point-min) (point-max)
4562                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4563               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4564                 (while (and newsrc
4565                             (not (gnus-goto-char
4566                                   (text-property-any
4567                                    (point-min) (point-max) 'gnus-group
4568                                    (gnus-intern-safe
4569                                     (caar newsrc) gnus-active-hashtb)))))
4570                   (setq newsrc (cdr newsrc)))
4571                 (or newsrc (progn (goto-char (point-max))
4572                                   (forward-line -1)))))))
4573       ;; Adjust cursor point.
4574       (gnus-group-position-point))))
4575
4576 (defun gnus-group-list-level (level &optional all)
4577   "List groups on LEVEL.
4578 If ALL (the prefix), also list groups that have no unread articles."
4579   (interactive "nList groups on level: \nP")
4580   (gnus-group-list-groups level all level))
4581
4582 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4583   "List all newsgroups with unread articles of level LEVEL or lower.
4584 If ALL is non-nil, list groups that have no unread articles.
4585 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4586 If REGEXP, only list groups matching REGEXP."
4587   (set-buffer gnus-group-buffer)
4588   (let ((buffer-read-only nil)
4589         (newsrc (cdr gnus-newsrc-alist))
4590         (lowest (or lowest 1))
4591         info clevel unread group params)
4592     (erase-buffer)
4593     (if (< lowest gnus-level-zombie)
4594         ;; List living groups.
4595         (while newsrc
4596           (setq info (car newsrc)
4597                 group (gnus-info-group info)
4598                 params (gnus-info-params info)
4599                 newsrc (cdr newsrc)
4600                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4601           (and unread                   ; This group might be bogus
4602                (or (not regexp)
4603                    (string-match regexp group))
4604                (<= (setq clevel (gnus-info-level info)) level)
4605                (>= clevel lowest)
4606                (or all                  ; We list all groups?
4607                    (if (eq unread t)    ; Unactivated?
4608                        gnus-group-list-inactive-groups ; We list unactivated 
4609                      (> unread 0))      ; We list groups with unread articles
4610                    (and gnus-list-groups-with-ticked-articles
4611                         (cdr (assq 'tick (gnus-info-marks info))))
4612                                         ; And groups with tickeds
4613                    ;; Check for permanent visibility.
4614                    (and gnus-permanently-visible-groups
4615                         (string-match gnus-permanently-visible-groups
4616                                       group))
4617                    (memq 'visible params)
4618                    (cdr (assq 'visible params)))
4619                (gnus-group-insert-group-line
4620                 group (gnus-info-level info)
4621                 (gnus-info-marks info) unread (gnus-info-method info)))))
4622
4623     ;; List dead groups.
4624     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4625          (gnus-group-prepare-flat-list-dead
4626           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4627           gnus-level-zombie ?Z
4628           regexp))
4629     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4630          (gnus-group-prepare-flat-list-dead
4631           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4632           gnus-level-killed ?K regexp))
4633
4634     (gnus-group-set-mode-line)
4635     (setq gnus-group-list-mode (cons level all))
4636     (run-hooks 'gnus-group-prepare-hook)))
4637
4638 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4639   ;; List zombies and killed lists somewhat faster, which was
4640   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4641   ;; this by ignoring the group format specification altogether.
4642   (let (group)
4643     (if regexp
4644         ;; This loop is used when listing groups that match some
4645         ;; regexp.
4646         (while groups
4647           (setq group (pop groups))
4648           (when (string-match regexp group)
4649             (gnus-add-text-properties
4650              (point) (prog1 (1+ (point))
4651                        (insert " " mark "     *: " group "\n"))
4652              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4653                    'gnus-unread t
4654                    'gnus-level level))))
4655       ;; This loop is used when listing all groups.
4656       (while groups
4657         (gnus-add-text-properties
4658          (point) (prog1 (1+ (point))
4659                    (insert " " mark "     *: "
4660                            (setq group (pop groups)) "\n"))
4661          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4662                'gnus-unread t
4663                'gnus-level level))))))
4664
4665 (defmacro gnus-group-real-name (group)
4666   "Find the real name of a foreign newsgroup."
4667   `(let ((gname ,group))
4668      (if (string-match ":[^:]+$" gname)
4669          (substring gname (1+ (match-beginning 0)))
4670        gname)))
4671
4672 (defsubst gnus-server-add-address (method)
4673   (let ((method-name (symbol-name (car method))))
4674     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4675              (not (assq (intern (concat method-name "-address")) method)))
4676         (append method (list (list (intern (concat method-name "-address"))
4677                                    (nth 1 method))))
4678       method)))
4679
4680 (defsubst gnus-server-get-method (group method)
4681   ;; Input either a server name, and extended server name, or a
4682   ;; select method, and return a select method.
4683   (cond ((stringp method)
4684          (gnus-server-to-method method))
4685         ((equal method gnus-select-method)
4686          gnus-select-method)
4687         ((and (stringp (car method)) group)
4688          (gnus-server-extend-method group method))
4689         ((and method (not group)
4690               (equal (cadr method) ""))
4691          method)
4692         (t
4693          (gnus-server-add-address method))))
4694
4695 (defun gnus-server-to-method (server)
4696   "Map virtual server names to select methods."
4697   (or 
4698    ;; Is this a method, perhaps?
4699    (and server (listp server) server)
4700    ;; Perhaps this is the native server?
4701    (and (equal server "native") gnus-select-method)
4702    ;; It should be in the server alist.
4703    (cdr (assoc server gnus-server-alist))
4704    ;; If not, we look through all the opened server
4705    ;; to see whether we can find it there.
4706    (let ((opened gnus-opened-servers))
4707      (while (and opened
4708                  (not (equal server (format "%s:%s" (caaar opened)
4709                                             (cadaar opened)))))
4710        (pop opened))
4711      (caar opened))))
4712
4713 (defmacro gnus-method-equal (ss1 ss2)
4714   "Say whether two servers are equal."
4715   `(let ((s1 ,ss1)
4716          (s2 ,ss2))
4717      (or (equal s1 s2)
4718          (and (= (length s1) (length s2))
4719               (progn
4720                 (while (and s1 (member (car s1) s2))
4721                   (setq s1 (cdr s1)))
4722                 (null s1))))))
4723
4724 (defun gnus-server-equal (m1 m2)
4725   "Say whether two methods are equal."
4726   (let ((m1 (cond ((null m1) gnus-select-method)
4727                   ((stringp m1) (gnus-server-to-method m1))
4728                   (t m1)))
4729         (m2 (cond ((null m2) gnus-select-method)
4730                   ((stringp m2) (gnus-server-to-method m2))
4731                   (t m2))))
4732     (gnus-method-equal m1 m2)))
4733
4734 (defun gnus-servers-using-backend (backend)
4735   "Return a list of known servers using BACKEND."
4736   (let ((opened gnus-opened-servers)
4737         out)
4738     (while opened
4739       (when (eq backend (caaar opened))
4740         (push (caar opened) out))
4741       (pop opened))
4742     out))
4743
4744 (defun gnus-group-prefixed-name (group method)
4745   "Return the whole name from GROUP and METHOD."
4746   (and (stringp method) (setq method (gnus-server-to-method method)))
4747   (concat (format "%s" (car method))
4748           (if (and
4749                (or (assoc (format "%s" (car method)) 
4750                           (gnus-methods-using 'address))
4751                    (gnus-server-equal method gnus-message-archive-method))
4752                (nth 1 method)
4753                (not (string= (nth 1 method) "")))
4754               (concat "+" (nth 1 method)))
4755           ":" group))
4756
4757 (defun gnus-group-real-prefix (group)
4758   "Return the prefix of the current group name."
4759   (if (string-match "^[^:]+:" group)
4760       (substring group 0 (match-end 0))
4761     ""))
4762
4763 (defun gnus-group-method (group)
4764   "Return the server or method used for selecting GROUP."
4765   (let ((prefix (gnus-group-real-prefix group)))
4766     (if (equal prefix "")
4767         gnus-select-method
4768       (let ((servers gnus-opened-servers)
4769             (server "")
4770             backend possible found)
4771         (if (string-match "^[^\\+]+\\+" prefix)
4772             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4773                   server (substring prefix (match-end 0) (1- (length prefix))))
4774           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4775         (while servers
4776           (when (eq (caaar servers) backend)
4777             (setq possible (caar servers))
4778             (when (equal (cadaar servers) server)
4779               (setq found (caar servers))))
4780           (pop servers))
4781         (or (car (rassoc found gnus-server-alist))
4782             found
4783             (car (rassoc possible gnus-server-alist))
4784             possible
4785             (list backend server))))))
4786
4787 (defsubst gnus-secondary-method-p (method)
4788   "Return whether METHOD is a secondary select method."
4789   (let ((methods gnus-secondary-select-methods)
4790         (gmethod (gnus-server-get-method nil method)))
4791     (while (and methods
4792                 (not (equal (gnus-server-get-method nil (car methods))
4793                             gmethod)))
4794       (setq methods (cdr methods)))
4795     methods))
4796
4797 (defun gnus-group-foreign-p (group)
4798   "Say whether a group is foreign or not."
4799   (and (not (gnus-group-native-p group))
4800        (not (gnus-group-secondary-p group))))
4801
4802 (defun gnus-group-native-p (group)
4803   "Say whether the group is native or not."
4804   (not (string-match ":" group)))
4805
4806 (defun gnus-group-secondary-p (group)
4807   "Say whether the group is secondary or not."
4808   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4809
4810 (defun gnus-group-get-parameter (group &optional symbol)
4811   "Returns the group parameters for GROUP.
4812 If SYMBOL, return the value of that symbol in the group parameters."
4813   (let ((params (gnus-info-params (gnus-get-info group))))
4814     (if symbol
4815         (gnus-group-parameter-value params symbol)
4816       params)))
4817
4818 (defun gnus-group-parameter-value (params symbol)
4819   "Return the value of SYMBOL in group PARAMS."
4820   (or (car (memq symbol params))        ; It's either a simple symbol
4821       (cdr (assq symbol params))))      ; or a cons.
4822
4823 (defun gnus-group-add-parameter (group param)
4824   "Add parameter PARAM to GROUP."
4825   (let ((info (gnus-get-info group)))
4826     (if (not info)
4827         () ; This is a dead group.  We just ignore it.
4828       ;; Cons the new param to the old one and update.
4829       (gnus-group-set-info (cons param (gnus-info-params info))
4830                            group 'params))))
4831
4832 (defun gnus-group-set-parameter (group name value)
4833   "Set parameter NAME to VALUE in GROUP."
4834   (let ((info (gnus-get-info group)))
4835     (if (not info)
4836         () ; This is a dead group.  We just ignore it.
4837       (let ((old-params (gnus-info-params info))
4838             (new-params (list (cons name value))))
4839         (while old-params
4840           (if (or (not (listp (car old-params)))
4841                   (not (eq (caar old-params) name)))
4842               (setq new-params (append new-params (list (car old-params)))))
4843           (setq old-params (cdr old-params)))
4844         (gnus-group-set-info new-params group 'params)))))
4845
4846 (defun gnus-group-add-score (group &optional score)
4847   "Add SCORE to the GROUP score.
4848 If SCORE is nil, add 1 to the score of GROUP."
4849   (let ((info (gnus-get-info group)))
4850     (when info
4851       (gnus-info-set-score info (+ (gnus-info-score info) (or score 1))))))
4852
4853 (defun gnus-summary-bubble-group ()
4854   "Increase the score of the current group.
4855 This is a handy function to add to `gnus-summary-exit-hook' to
4856 increase the score of each group you read."
4857   (gnus-group-add-score gnus-newsgroup-name))
4858
4859 (defun gnus-group-set-info (info &optional method-only-group part)
4860   (let* ((entry (gnus-gethash
4861                  (or method-only-group (gnus-info-group info))
4862                  gnus-newsrc-hashtb))
4863          (part-info info)
4864          (info (if method-only-group (nth 2 entry) info))
4865          method)
4866     (when method-only-group
4867       (unless entry
4868         (error "Trying to change non-existent group %s" method-only-group))
4869       ;; We have received parts of the actual group info - either the
4870       ;; select method or the group parameters.  We first check
4871       ;; whether we have to extend the info, and if so, do that.
4872       (let ((len (length info))
4873             (total (if (eq part 'method) 5 6)))
4874         (when (< len total)
4875           (setcdr (nthcdr (1- len) info)
4876                   (make-list (- total len) nil)))
4877         ;; Then we enter the new info.
4878         (setcar (nthcdr (1- total) info) part-info)))
4879     (unless entry
4880       ;; This is a new group, so we just create it.
4881       (save-excursion
4882         (set-buffer gnus-group-buffer)
4883         (setq method (gnus-info-method info))
4884         (when (gnus-server-equal method "native")
4885           (setq method nil))
4886         (save-excursion
4887           (set-buffer gnus-group-buffer)
4888           (if method
4889               ;; It's a foreign group...
4890               (gnus-group-make-group
4891                (gnus-group-real-name (gnus-info-group info))
4892                (if (stringp method) method
4893                  (prin1-to-string (car method)))
4894                (and (consp method)
4895                     (nth 1 (gnus-info-method info))))
4896             ;; It's a native group.
4897             (gnus-group-make-group (gnus-info-group info))))
4898         (gnus-message 6 "Note: New group created")
4899         (setq entry
4900               (gnus-gethash (gnus-group-prefixed-name
4901                              (gnus-group-real-name (gnus-info-group info))
4902                              (or (gnus-info-method info) gnus-select-method))
4903                             gnus-newsrc-hashtb))))
4904     ;; Whether it was a new group or not, we now have the entry, so we
4905     ;; can do the update.
4906     (if entry
4907         (progn
4908           (setcar (nthcdr 2 entry) info)
4909           (when (and (not (eq (car entry) t))
4910                      (gnus-active (gnus-info-group info)))
4911             (setcar entry (length (gnus-list-of-unread-articles (car info))))))
4912       (error "No such group: %s" (gnus-info-group info)))))
4913
4914 (defun gnus-group-set-method-info (group select-method)
4915   (gnus-group-set-info select-method group 'method))
4916
4917 (defun gnus-group-set-params-info (group params)
4918   (gnus-group-set-info params group 'params))
4919
4920 (defun gnus-group-update-group-line ()
4921   "Update the current line in the group buffer."
4922   (let* ((buffer-read-only nil)
4923          (group (gnus-group-group-name))
4924          (entry (and group (gnus-gethash group gnus-newsrc-hashtb)))
4925          gnus-group-indentation)
4926     (when group
4927       (and entry
4928            (not (gnus-ephemeral-group-p group))
4929            (gnus-dribble-enter
4930             (concat "(gnus-group-set-info '"
4931                     (prin1-to-string (nth 2 entry)) ")")))
4932       (setq gnus-group-indentation (gnus-group-group-indentation))
4933       (gnus-delete-line)
4934       (gnus-group-insert-group-line-info group)
4935       (forward-line -1)
4936       (gnus-group-position-point))))
4937
4938 (defun gnus-group-insert-group-line-info (group)
4939   "Insert GROUP on the current line."
4940   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4941         active info)
4942     (if entry
4943         (progn
4944           ;; (Un)subscribed group.
4945           (setq info (nth 2 entry))
4946           (gnus-group-insert-group-line
4947            group (gnus-info-level info) (gnus-info-marks info)
4948            (or (car entry) t) (gnus-info-method info)))
4949       ;; This group is dead.
4950       (gnus-group-insert-group-line
4951        group
4952        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4953        nil
4954        (if (setq active (gnus-active group))
4955            (- (1+ (cdr active)) (car active)) 0)
4956        nil))))
4957
4958 (defun gnus-group-insert-group-line (gnus-tmp-group gnus-tmp-level 
4959                                                     gnus-tmp-marked number
4960                                                     gnus-tmp-method)
4961   "Insert a group line in the group buffer."
4962   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4963          (gnus-tmp-number-total
4964           (if gnus-tmp-active
4965               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4966             0))
4967          (gnus-tmp-number-of-unread
4968           (if (numberp number) (int-to-string (max 0 number))
4969             "*"))
4970          (gnus-tmp-number-of-read
4971           (if (numberp number)
4972               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4973             "*"))
4974          (gnus-tmp-subscribed
4975           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4976                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4977                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4978                 (t ?K)))
4979          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4980          (gnus-tmp-newsgroup-description
4981           (if gnus-description-hashtb
4982               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4983             ""))
4984          (gnus-tmp-moderated
4985           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4986          (gnus-tmp-moderated-string
4987           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4988          (gnus-tmp-method
4989           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4990          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
4991          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4992          (gnus-tmp-news-method-string
4993           (if gnus-tmp-method
4994               (format "(%s:%s)" (car gnus-tmp-method)
4995                       (cadr gnus-tmp-method)) ""))
4996          (gnus-tmp-marked-mark
4997           (if (and (numberp number)
4998                    (zerop number)
4999                    (cdr (assq 'tick gnus-tmp-marked)))
5000               ?* ? ))
5001          (gnus-tmp-process-marked
5002           (if (member gnus-tmp-group gnus-group-marked)
5003               gnus-process-mark ? ))
5004          (gnus-tmp-grouplens
5005           (or (and gnus-use-grouplens
5006                    (bbb-grouplens-group-p gnus-tmp-group))
5007               ""))
5008          (buffer-read-only nil)
5009          header gnus-tmp-header)        ; passed as parameter to user-funcs.
5010     (beginning-of-line)
5011     (gnus-add-text-properties
5012      (point)
5013      (prog1 (1+ (point))
5014        ;; Insert the text.
5015        (eval gnus-group-line-format-spec))
5016      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
5017        gnus-unread ,(if (numberp number)
5018                         (string-to-int gnus-tmp-number-of-unread)
5019                       t)
5020        gnus-marked ,gnus-tmp-marked-mark
5021        gnus-indentation ,gnus-group-indentation
5022        gnus-level ,gnus-tmp-level))
5023     (when (inline (gnus-visual-p 'group-highlight 'highlight))
5024       (forward-line -1)
5025       (run-hooks 'gnus-group-update-hook)
5026       (forward-line))
5027     ;; Allow XEmacs to remove front-sticky text properties.
5028     (gnus-group-remove-excess-properties)))
5029
5030 (defun gnus-group-update-group (group &optional visible-only)
5031   "Update all lines where GROUP appear.
5032 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
5033 already."
5034   (save-excursion
5035     (set-buffer gnus-group-buffer)
5036     ;; The buffer may be narrowed.
5037     (save-restriction
5038       (widen)
5039       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
5040             (loc (point-min))
5041             found buffer-read-only)
5042         ;; Enter the current status into the dribble buffer.
5043         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
5044           (if (and entry (not (gnus-ephemeral-group-p group)))
5045               (gnus-dribble-enter
5046                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
5047                        ")"))))
5048         ;; Find all group instances.  If topics are in use, each group
5049         ;; may be listed in more than once.
5050         (while (setq loc (text-property-any
5051                           loc (point-max) 'gnus-group ident))
5052           (setq found t)
5053           (goto-char loc)
5054           (let ((gnus-group-indentation (gnus-group-group-indentation)))
5055             (gnus-delete-line)
5056             (gnus-group-insert-group-line-info group)
5057             (save-excursion
5058               (forward-line -1)
5059               (run-hooks 'gnus-group-update-group-hook)))
5060           (setq loc (1+ loc)))
5061         (unless (or found visible-only)
5062           ;; No such line in the buffer, find out where it's supposed to
5063           ;; go, and insert it there (or at the end of the buffer).
5064           (if gnus-goto-missing-group-function
5065               (funcall gnus-goto-missing-group-function group)
5066             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
5067               (while (and entry (car entry)
5068                           (not
5069                            (gnus-goto-char
5070                             (text-property-any
5071                              (point-min) (point-max)
5072                              'gnus-group (gnus-intern-safe
5073                                           (caar entry) gnus-active-hashtb)))))
5074                 (setq entry (cdr entry)))
5075               (or entry (goto-char (point-max)))))
5076           ;; Finally insert the line.
5077           (let ((gnus-group-indentation (gnus-group-group-indentation)))
5078             (gnus-group-insert-group-line-info group)
5079             (save-excursion
5080               (forward-line -1)
5081               (run-hooks 'gnus-group-update-group-hook))))
5082         (gnus-group-set-mode-line)))))
5083
5084 (defun gnus-group-set-mode-line ()
5085   "Update the mode line in the group buffer."
5086   (when (memq 'group gnus-updated-mode-lines)
5087     ;; Yes, we want to keep this mode line updated.
5088     (save-excursion
5089       (set-buffer gnus-group-buffer)
5090       (let* ((gformat (or gnus-group-mode-line-format-spec
5091                           (setq gnus-group-mode-line-format-spec
5092                                 (gnus-parse-format
5093                                  gnus-group-mode-line-format
5094                                  gnus-group-mode-line-format-alist))))
5095              (gnus-tmp-news-server (cadr gnus-select-method))
5096              (gnus-tmp-news-method (car gnus-select-method))
5097              (gnus-tmp-colon (if (equal gnus-tmp-news-server "") "" ":"))
5098              (max-len 60)
5099              gnus-tmp-header            ;Dummy binding for user-defined formats
5100              ;; Get the resulting string.
5101              (mode-string (eval gformat)))
5102         ;; Say whether the dribble buffer has been modified.
5103         (setq mode-line-modified
5104               (if (and gnus-dribble-buffer
5105                        (buffer-name gnus-dribble-buffer)
5106                        (buffer-modified-p gnus-dribble-buffer)
5107                        (save-excursion
5108                          (set-buffer gnus-dribble-buffer)
5109                          (not (zerop (buffer-size)))))
5110                   "---*- " "----- "))
5111         ;; If the line is too long, we chop it off.
5112         (when (> (length mode-string) max-len)
5113           (setq mode-string (substring mode-string 0 (- max-len 4))))
5114         (prog1
5115             (setq mode-line-buffer-identification 
5116                   (gnus-mode-line-buffer-identification
5117                    (list mode-string)))
5118           (set-buffer-modified-p t))))))
5119
5120 (defun gnus-group-group-name ()
5121   "Get the name of the newsgroup on the current line."
5122   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
5123     (and group (symbol-name group))))
5124
5125 (defun gnus-group-group-level ()
5126   "Get the level of the newsgroup on the current line."
5127   (get-text-property (gnus-point-at-bol) 'gnus-level))
5128
5129 (defun gnus-group-group-indentation ()
5130   "Get the indentation of the newsgroup on the current line."
5131   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
5132       (and gnus-group-indentation-function
5133            (funcall gnus-group-indentation-function))
5134       ""))
5135
5136 (defun gnus-group-group-unread ()
5137   "Get the number of unread articles of the newsgroup on the current line."
5138   (get-text-property (gnus-point-at-bol) 'gnus-unread))
5139
5140 (defun gnus-group-search-forward (&optional backward all level first-too)
5141   "Find the next newsgroup with unread articles.
5142 If BACKWARD is non-nil, find the previous newsgroup instead.
5143 If ALL is non-nil, just find any newsgroup.
5144 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
5145 group exists.
5146 If FIRST-TOO, the current line is also eligible as a target."
5147   (let ((way (if backward -1 1))
5148         (low gnus-level-killed)
5149         (beg (point))
5150         pos found lev)
5151     (if (and backward (progn (beginning-of-line)) (bobp))
5152         nil
5153       (or first-too (forward-line way))
5154       (while (and
5155               (not (eobp))
5156               (not (setq
5157                     found
5158                     (and (or all
5159                              (and
5160                               (let ((unread
5161                                      (get-text-property (point) 'gnus-unread)))
5162                                 (and (numberp unread) (> unread 0)))
5163                               (setq lev (get-text-property (point)
5164                                                            'gnus-level))
5165                               (<= lev gnus-level-subscribed)))
5166                          (or (not level)
5167                              (and (setq lev (get-text-property (point)
5168                                                                'gnus-level))
5169                                   (or (= lev level)
5170                                       (and (< lev low)
5171                                            (< level lev)
5172                                            (progn
5173                                              (setq low lev)
5174                                              (setq pos (point))
5175                                              nil))))))))
5176               (zerop (forward-line way)))))
5177     (if found
5178         (progn (gnus-group-position-point) t)
5179       (goto-char (or pos beg))
5180       (and pos t))))
5181
5182 ;;; Gnus group mode commands
5183
5184 ;; Group marking.
5185
5186 (defun gnus-group-mark-group (n &optional unmark no-advance)
5187   "Mark the current group."
5188   (interactive "p")
5189   (let ((buffer-read-only nil)
5190         group)
5191     (while (and (> n 0)
5192                 (not (eobp)))
5193       (when (setq group (gnus-group-group-name))
5194         ;; Update the mark.
5195         (beginning-of-line)
5196         (forward-char
5197          (or (cdr (assq 'process gnus-group-mark-positions)) 2))
5198         (delete-char 1)
5199         (if unmark
5200             (progn
5201               (insert " ")
5202               (setq gnus-group-marked (delete group gnus-group-marked)))
5203           (insert "#")
5204           (setq gnus-group-marked
5205                 (cons group (delete group gnus-group-marked)))))
5206       (or no-advance (gnus-group-next-group 1))
5207       (decf n))
5208     (gnus-summary-position-point)
5209     n))
5210
5211 (defun gnus-group-unmark-group (n)
5212   "Remove the mark from the current group."
5213   (interactive "p")
5214   (gnus-group-mark-group n 'unmark)
5215   (gnus-group-position-point))
5216
5217 (defun gnus-group-unmark-all-groups ()
5218   "Unmark all groups."
5219   (interactive)
5220   (let ((groups gnus-group-marked))
5221     (save-excursion
5222       (while groups
5223         (gnus-group-remove-mark (pop groups)))))
5224   (gnus-group-position-point))
5225
5226 (defun gnus-group-mark-region (unmark beg end)
5227   "Mark all groups between point and mark.
5228 If UNMARK, remove the mark instead."
5229   (interactive "P\nr")
5230   (let ((num (count-lines beg end)))
5231     (save-excursion
5232       (goto-char beg)
5233       (- num (gnus-group-mark-group num unmark)))))
5234
5235 (defun gnus-group-mark-buffer (&optional unmark)
5236   "Mark all groups in the buffer.
5237 If UNMARK, remove the mark instead."
5238   (interactive "P")
5239   (gnus-group-mark-region unmark (point-min) (point-max)))
5240
5241 (defun gnus-group-mark-regexp (regexp)
5242   "Mark all groups that match some regexp."
5243   (interactive "sMark (regexp): ")
5244   (let ((alist (cdr gnus-newsrc-alist))
5245         group)
5246     (while alist
5247       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5248         (gnus-group-set-mark group))))
5249   (gnus-group-position-point))
5250
5251 (defun gnus-group-remove-mark (group)
5252   "Remove the process mark from GROUP and move point there.
5253 Return nil if the group isn't displayed."
5254   (if (gnus-group-goto-group group)
5255       (save-excursion
5256         (gnus-group-mark-group 1 'unmark t)
5257         t)
5258     (setq gnus-group-marked
5259           (delete group gnus-group-marked))
5260     nil))
5261
5262 (defun gnus-group-set-mark (group)
5263   "Set the process mark on GROUP."
5264   (if (gnus-group-goto-group group) 
5265       (save-excursion
5266         (gnus-group-mark-group 1 nil t))
5267     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
5268
5269 (defun gnus-group-universal-argument (arg &optional groups func)
5270   "Perform any command on all groups accoring to the process/prefix convention."
5271   (interactive "P")
5272   (let ((groups (or groups (gnus-group-process-prefix arg)))
5273         group func)
5274     (if (eq (setq func (or func
5275                            (key-binding
5276                             (read-key-sequence
5277                              (substitute-command-keys
5278                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5279             'undefined)
5280         (gnus-error 1 "Undefined key")
5281       (while groups
5282         (gnus-group-remove-mark (setq group (pop groups)))
5283         (command-execute func))))
5284   (gnus-group-position-point))
5285
5286 (defun gnus-group-process-prefix (n)
5287   "Return a list of groups to work on.
5288 Take into consideration N (the prefix) and the list of marked groups."
5289   (cond
5290    (n
5291     (setq n (prefix-numeric-value n))
5292     ;; There is a prefix, so we return a list of the N next
5293     ;; groups.
5294     (let ((way (if (< n 0) -1 1))
5295           (n (abs n))
5296           group groups)
5297       (save-excursion
5298         (while (and (> n 0)
5299                     (setq group (gnus-group-group-name)))
5300           (setq groups (cons group groups))
5301           (setq n (1- n))
5302           (gnus-group-next-group way)))
5303       (nreverse groups)))
5304    ((and (boundp 'transient-mark-mode)
5305          transient-mark-mode
5306          (boundp 'mark-active)
5307          mark-active)
5308     ;; Work on the region between point and mark.
5309     (let ((max (max (point) (mark)))
5310           groups)
5311       (save-excursion
5312         (goto-char (min (point) (mark)))
5313         (while
5314             (and
5315              (push (gnus-group-group-name) groups)
5316              (zerop (gnus-group-next-group 1))
5317              (< (point) max)))
5318         (nreverse groups))))
5319    (gnus-group-marked
5320     ;; No prefix, but a list of marked articles.
5321     (reverse gnus-group-marked))
5322    (t
5323     ;; Neither marked articles or a prefix, so we return the
5324     ;; current group.
5325     (let ((group (gnus-group-group-name)))
5326       (and group (list group))))))
5327
5328 ;; Selecting groups.
5329
5330 (defun gnus-group-read-group (&optional all no-article group)
5331   "Read news in this newsgroup.
5332 If the prefix argument ALL is non-nil, already read articles become
5333 readable.  IF ALL is a number, fetch this number of articles.  If the
5334 optional argument NO-ARTICLE is non-nil, no article will be
5335 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5336 group."
5337   (interactive "P")
5338   (let ((group (or group (gnus-group-group-name)))
5339         number active marked entry)
5340     (or group (error "No group on current line"))
5341     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5342                                             group gnus-newsrc-hashtb)))))
5343     ;; This group might be a dead group.  In that case we have to get
5344     ;; the number of unread articles from `gnus-active-hashtb'.
5345     (setq number
5346           (cond ((numberp all) all)
5347                 (entry (car entry))
5348                 ((setq active (gnus-active group))
5349                  (- (1+ (cdr active)) (car active)))))
5350     (gnus-summary-read-group
5351      group (or all (and (numberp number)
5352                         (zerop (+ number (length (cdr (assq 'tick marked)))
5353                                   (length (cdr (assq 'dormant marked)))))))
5354      no-article)))
5355
5356 (defun gnus-group-select-group (&optional all)
5357   "Select this newsgroup.
5358 No article is selected automatically.
5359 If ALL is non-nil, already read articles become readable.
5360 If ALL is a number, fetch this number of articles."
5361   (interactive "P")
5362   (gnus-group-read-group all t))
5363
5364 (defun gnus-group-quick-select-group (&optional all)
5365   "Select the current group \"quickly\".
5366 This means that no highlighting or scoring will be performed."
5367   (interactive "P")
5368   (let (gnus-visual
5369         gnus-score-find-score-files-function
5370         gnus-apply-kill-hook
5371         gnus-summary-expunge-below)
5372     (gnus-group-read-group all t)))
5373
5374 (defun gnus-group-visible-select-group (&optional all)
5375   "Select the current group without hiding any articles."
5376   (interactive "P")
5377   (let ((gnus-inhibit-limiting t))
5378     (gnus-group-read-group all t)))
5379
5380 ;;;###autoload
5381 (defun gnus-fetch-group (group)
5382   "Start Gnus if necessary and enter GROUP.
5383 Returns whether the fetching was successful or not."
5384   (interactive "sGroup name: ")
5385   (or (get-buffer gnus-group-buffer)
5386       (gnus))
5387   (gnus-group-read-group nil nil group))
5388
5389 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5390 ;; if selection was successful.
5391 (defun gnus-group-read-ephemeral-group
5392   (group method &optional activate quit-config)
5393   (let ((group (if (gnus-group-foreign-p group) group
5394                  (gnus-group-prefixed-name group method))))
5395     (gnus-sethash
5396      group
5397      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5398                      ((quit-config . ,(if quit-config quit-config
5399                                         (cons (current-buffer) 'summary))))))
5400      gnus-newsrc-hashtb)
5401     (set-buffer gnus-group-buffer)
5402     (or (gnus-check-server method)
5403         (error "Unable to contact server: %s" (gnus-status-message method)))
5404     (if activate (or (gnus-request-group group)
5405                      (error "Couldn't request group")))
5406     (condition-case ()
5407         (gnus-group-read-group t t group)
5408       (error nil)
5409       (quit nil))))
5410
5411 (defun gnus-group-jump-to-group (group)
5412   "Jump to newsgroup GROUP."
5413   (interactive
5414    (list (completing-read
5415           "Group: " gnus-active-hashtb nil
5416           (gnus-read-active-file-p)
5417           nil
5418           'gnus-group-history)))
5419
5420   (when (equal group "")
5421     (error "Empty group name"))
5422
5423   (when (string-match "[\000-\032]" group)
5424     (error "Control characters in group: %s" group))
5425
5426   (let ((b (text-property-any
5427             (point-min) (point-max)
5428             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5429     (unless (gnus-ephemeral-group-p group)
5430       (if b
5431           ;; Either go to the line in the group buffer...
5432           (goto-char b)
5433         ;; ... or insert the line.
5434         (or
5435          (gnus-active group)
5436          (gnus-activate-group group)
5437          (error "%s error: %s" group (gnus-status-message group)))
5438
5439         (gnus-group-update-group group)
5440         (goto-char (text-property-any
5441                     (point-min) (point-max)
5442                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5443     ;; Adjust cursor point.
5444     (gnus-group-position-point)))
5445
5446 (defun gnus-group-goto-group (group)
5447   "Goto to newsgroup GROUP."
5448   (when group
5449     (let ((b (text-property-any (point-min) (point-max)
5450                                 'gnus-group (gnus-intern-safe
5451                                              group gnus-active-hashtb))))
5452       (and b (goto-char b)))))
5453
5454 (defun gnus-group-next-group (n)
5455   "Go to next N'th newsgroup.
5456 If N is negative, search backward instead.
5457 Returns the difference between N and the number of skips actually
5458 done."
5459   (interactive "p")
5460   (gnus-group-next-unread-group n t))
5461
5462 (defun gnus-group-next-unread-group (n &optional all level)
5463   "Go to next N'th unread newsgroup.
5464 If N is negative, search backward instead.
5465 If ALL is non-nil, choose any newsgroup, unread or not.
5466 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5467 such group can be found, the next group with a level higher than
5468 LEVEL.
5469 Returns the difference between N and the number of skips actually
5470 made."
5471   (interactive "p")
5472   (let ((backward (< n 0))
5473         (n (abs n)))
5474     (while (and (> n 0)
5475                 (gnus-group-search-forward
5476                  backward (or (not gnus-group-goto-unread) all) level))
5477       (setq n (1- n)))
5478     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5479                                (if level " on this level or higher" "")))
5480     n))
5481
5482 (defun gnus-group-prev-group (n)
5483   "Go to previous N'th newsgroup.
5484 Returns the difference between N and the number of skips actually
5485 done."
5486   (interactive "p")
5487   (gnus-group-next-unread-group (- n) t))
5488
5489 (defun gnus-group-prev-unread-group (n)
5490   "Go to previous N'th unread newsgroup.
5491 Returns the difference between N and the number of skips actually
5492 done."
5493   (interactive "p")
5494   (gnus-group-next-unread-group (- n)))
5495
5496 (defun gnus-group-next-unread-group-same-level (n)
5497   "Go to next N'th unread newsgroup on the same level.
5498 If N is negative, search backward instead.
5499 Returns the difference between N and the number of skips actually
5500 done."
5501   (interactive "p")
5502   (gnus-group-next-unread-group n t (gnus-group-group-level))
5503   (gnus-group-position-point))
5504
5505 (defun gnus-group-prev-unread-group-same-level (n)
5506   "Go to next N'th unread newsgroup on the same level.
5507 Returns the difference between N and the number of skips actually
5508 done."
5509   (interactive "p")
5510   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5511   (gnus-group-position-point))
5512
5513 (defun gnus-group-best-unread-group (&optional exclude-group)
5514   "Go to the group with the highest level.
5515 If EXCLUDE-GROUP, do not go to that group."
5516   (interactive)
5517   (goto-char (point-min))
5518   (let ((best 100000)
5519         unread best-point)
5520     (while (not (eobp))
5521       (setq unread (get-text-property (point) 'gnus-unread))
5522       (if (and (numberp unread) (> unread 0))
5523           (progn
5524             (if (and (get-text-property (point) 'gnus-level)
5525                      (< (get-text-property (point) 'gnus-level) best)
5526                      (or (not exclude-group)
5527                          (not (equal exclude-group (gnus-group-group-name)))))
5528                 (progn
5529                   (setq best (get-text-property (point) 'gnus-level))
5530                   (setq best-point (point))))))
5531       (forward-line 1))
5532     (if best-point (goto-char best-point))
5533     (gnus-summary-position-point)
5534     (and best-point (gnus-group-group-name))))
5535
5536 (defun gnus-group-first-unread-group ()
5537   "Go to the first group with unread articles."
5538   (interactive)
5539   (prog1
5540       (let ((opoint (point))
5541             unread)
5542         (goto-char (point-min))
5543         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5544                 (and (numberp unread)   ; Not a topic.
5545                      (not (zerop unread))) ; Has unread articles.
5546                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5547             (point)                     ; Success.
5548           (goto-char opoint)
5549           nil))                         ; Not success.
5550     (gnus-group-position-point)))
5551
5552 (defun gnus-group-enter-server-mode ()
5553   "Jump to the server buffer."
5554   (interactive)
5555   (gnus-enter-server-buffer))
5556
5557 (defun gnus-group-make-group (name &optional method address)
5558   "Add a new newsgroup.
5559 The user will be prompted for a NAME, for a select METHOD, and an
5560 ADDRESS."
5561   (interactive
5562    (cons
5563     (read-string "Group name: ")
5564     (let ((method
5565            (completing-read
5566             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5567             nil t nil 'gnus-method-history)))
5568       (cond ((assoc method gnus-valid-select-methods)
5569              (list method
5570                    (if (memq 'prompt-address
5571                              (assoc method gnus-valid-select-methods))
5572                        (read-string "Address: ")
5573                      "")))
5574             ((assoc method gnus-server-alist)
5575              (list method))
5576             (t
5577              (list method ""))))))
5578
5579   (let* ((meth (and method (if address (list (intern method) address)
5580                              method)))
5581          (nname (if method (gnus-group-prefixed-name name meth) name))
5582          backend info)
5583     (when (gnus-gethash nname gnus-newsrc-hashtb)
5584       (error "Group %s already exists" nname))
5585     ;; Subscribe to the new group.
5586     (gnus-group-change-level
5587      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5588      gnus-level-default-subscribed gnus-level-killed
5589      (and (gnus-group-group-name)
5590           (gnus-gethash (gnus-group-group-name)
5591                         gnus-newsrc-hashtb))
5592      t)
5593     ;; Make it active.
5594     (gnus-set-active nname (cons 1 0))
5595     (or (gnus-ephemeral-group-p name)
5596         (gnus-dribble-enter
5597          (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5598     ;; Insert the line.
5599     (gnus-group-insert-group-line-info nname)
5600     (forward-line -1)
5601     (gnus-group-position-point)
5602
5603     ;; Load the backend and try to make the backend create
5604     ;; the group as well.
5605     (when (assoc (symbol-name (setq backend (car (gnus-server-get-method
5606                                                   nil meth))))
5607                  gnus-valid-select-methods)
5608       (require backend))
5609     (gnus-check-server meth)
5610     (and (gnus-check-backend-function 'request-create-group nname)
5611          (gnus-request-create-group nname))
5612     t))
5613
5614 (defun gnus-group-delete-group (group &optional force)
5615   "Delete the current group.  Only meaningful with mail groups.
5616 If FORCE (the prefix) is non-nil, all the articles in the group will
5617 be deleted.  This is \"deleted\" as in \"removed forever from the face
5618 of the Earth\".  There is no undo.  The user will be prompted before
5619 doing the deletion."
5620   (interactive
5621    (list (gnus-group-group-name)
5622          current-prefix-arg))
5623   (or group (error "No group to rename"))
5624   (or (gnus-check-backend-function 'request-delete-group group)
5625       (error "This backend does not support group deletion"))
5626   (prog1
5627       (if (not (gnus-yes-or-no-p
5628                 (format
5629                  "Do you really want to delete %s%s? "
5630                  group (if force " and all its contents" ""))))
5631           () ; Whew!
5632         (gnus-message 6 "Deleting group %s..." group)
5633         (if (not (gnus-request-delete-group group force))
5634             (gnus-error 3 "Couldn't delete group %s" group)
5635           (gnus-message 6 "Deleting group %s...done" group)
5636           (gnus-group-goto-group group)
5637           (gnus-group-kill-group 1 t)
5638           (gnus-sethash group nil gnus-active-hashtb)
5639           t))
5640     (gnus-group-position-point)))
5641
5642 (defun gnus-group-rename-group (group new-name)
5643   (interactive
5644    (list
5645     (gnus-group-group-name)
5646     (progn
5647       (or (gnus-check-backend-function
5648            'request-rename-group (gnus-group-group-name))
5649           (error "This backend does not support renaming groups"))
5650       (read-string "New group name: "))))
5651
5652   (or (gnus-check-backend-function 'request-rename-group group)
5653       (error "This backend does not support renaming groups"))
5654
5655   (or group (error "No group to rename"))
5656   (and (string-match "^[ \t]*$" new-name)
5657        (error "Not a valid group name"))
5658
5659   ;; We find the proper prefixed name.
5660   (setq new-name
5661         (gnus-group-prefixed-name
5662          (gnus-group-real-name new-name)
5663          (gnus-info-method (gnus-get-info group))))
5664
5665   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5666   (prog1
5667       (if (not (gnus-request-rename-group group new-name))
5668           (gnus-error 3 "Couldn't rename group %s to %s" group new-name)
5669         ;; We rename the group internally by killing it...
5670         (gnus-group-goto-group group)
5671         (gnus-group-kill-group)
5672         ;; ... changing its name ...
5673         (setcar (cdar gnus-list-of-killed-groups) new-name)
5674         ;; ... and then yanking it.  Magic!
5675         (gnus-group-yank-group)
5676         (gnus-set-active new-name (gnus-active group))
5677         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5678         new-name)
5679     (gnus-group-position-point)))
5680
5681 (defun gnus-group-edit-group (group &optional part)
5682   "Edit the group on the current line."
5683   (interactive (list (gnus-group-group-name)))
5684   (let* ((part (or part 'info))
5685          (done-func `(lambda ()
5686                        "Exit editing mode and update the information."
5687                        (interactive)
5688                        (gnus-group-edit-group-done ',part ,group)))
5689          (winconf (current-window-configuration))
5690          info)
5691     (or group (error "No group on current line"))
5692     (or (setq info (gnus-get-info group))
5693         (error "Killed group; can't be edited"))
5694     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5695     (gnus-configure-windows 'edit-group)
5696     (gnus-add-current-to-buffer-list)
5697     (emacs-lisp-mode)
5698     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5699     (use-local-map (copy-keymap emacs-lisp-mode-map))
5700     (local-set-key "\C-c\C-c" done-func)
5701     (make-local-variable 'gnus-prev-winconf)
5702     (setq gnus-prev-winconf winconf)
5703     (erase-buffer)
5704     (insert
5705      (cond
5706       ((eq part 'method)
5707        ";; Type `C-c C-c' after editing the select method.\n\n")
5708       ((eq part 'params)
5709        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5710       ((eq part 'info)
5711        ";; Type `C-c C-c' after editing the group info.\n\n")))
5712     (insert
5713      (pp-to-string
5714       (cond ((eq part 'method)
5715              (or (gnus-info-method info) "native"))
5716             ((eq part 'params)
5717              (gnus-info-params info))
5718             (t info)))
5719      "\n")))
5720
5721 (defun gnus-group-edit-group-method (group)
5722   "Edit the select method of GROUP."
5723   (interactive (list (gnus-group-group-name)))
5724   (gnus-group-edit-group group 'method))
5725
5726 (defun gnus-group-edit-group-parameters (group)
5727   "Edit the group parameters of GROUP."
5728   (interactive (list (gnus-group-group-name)))
5729   (gnus-group-edit-group group 'params))
5730
5731 (defun gnus-group-edit-group-done (part group)
5732   "Get info from buffer, update variables and jump to the group buffer."
5733   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5734   (goto-char (point-min))
5735   (let* ((form (read (current-buffer)))
5736          (winconf gnus-prev-winconf)
5737          (method (cond ((eq part 'info) (nth 4 form))
5738                        ((eq part 'method) form)
5739                        (t nil)))
5740          (info (cond ((eq part 'info) form)
5741                      ((eq part 'method) (gnus-get-info group))
5742                      (t nil)))
5743          (new-group (if info
5744                       (if (or (not method)
5745                               (gnus-server-equal
5746                                gnus-select-method method))
5747                           (gnus-group-real-name (car info))
5748                         (gnus-group-prefixed-name
5749                          (gnus-group-real-name (car info)) method))
5750                       nil)))
5751     (when (and new-group
5752                (not (equal new-group group)))
5753       (when (gnus-group-goto-group group)
5754         (gnus-group-kill-group 1))
5755       (gnus-activate-group new-group))
5756     ;; Set the info.
5757     (if (and info new-group)
5758         (progn
5759           (setq info (gnus-copy-sequence info))
5760           (setcar info new-group)
5761           (unless (gnus-server-equal method "native")
5762             (unless (nthcdr 3 info)
5763               (nconc info (list nil nil)))
5764             (unless (nthcdr 4 info)
5765               (nconc info (list nil)))
5766             (gnus-info-set-method info method))
5767           (gnus-group-set-info info))
5768       (gnus-group-set-info form (or new-group group) part))
5769     (kill-buffer (current-buffer))
5770     (and winconf (set-window-configuration winconf))
5771     (set-buffer gnus-group-buffer)
5772     (gnus-group-update-group (or new-group group))
5773     (gnus-group-position-point)))
5774
5775 (defun gnus-group-make-help-group ()
5776   "Create the Gnus documentation group."
5777   (interactive)
5778   (let ((path load-path)
5779         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5780         file dir)
5781     (and (gnus-gethash name gnus-newsrc-hashtb)
5782          (error "Documentation group already exists"))
5783     (while path
5784       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5785             file nil)
5786       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5787                 (file-exists-p
5788                  (setq file (concat (file-name-directory
5789                                      (directory-file-name dir))
5790                                     "etc/gnus-tut.txt"))))
5791         (setq path nil)))
5792     (if (not file)
5793         (gnus-message 1 "Couldn't find doc group")
5794       (gnus-group-make-group
5795        (gnus-group-real-name name)
5796        (list 'nndoc "gnus-help"
5797              (list 'nndoc-address file)
5798              (list 'nndoc-article-type 'mbox)))))
5799   (gnus-group-position-point))
5800
5801 (defun gnus-group-make-doc-group (file type)
5802   "Create a group that uses a single file as the source."
5803   (interactive
5804    (list (read-file-name "File name: ")
5805          (and current-prefix-arg 'ask)))
5806   (when (eq type 'ask)
5807     (let ((err "")
5808           char found)
5809       (while (not found)
5810         (message
5811          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5812          err)
5813         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5814                           ((= char ?b) 'babyl)
5815                           ((= char ?d) 'digest)
5816                           ((= char ?f) 'forward)
5817                           ((= char ?a) 'mmfd)
5818                           (t (setq err (format "%c unknown. " char))
5819                              nil))))
5820       (setq type found)))
5821   (let* ((file (expand-file-name file))
5822          (name (gnus-generate-new-group-name
5823                 (gnus-group-prefixed-name
5824                  (file-name-nondirectory file) '(nndoc "")))))
5825     (gnus-group-make-group
5826      (gnus-group-real-name name)
5827      (list 'nndoc (file-name-nondirectory file)
5828            (list 'nndoc-address file)
5829            (list 'nndoc-article-type (or type 'guess))))))
5830
5831 (defun gnus-group-make-archive-group (&optional all)
5832   "Create the (ding) Gnus archive group of the most recent articles.
5833 Given a prefix, create a full group."
5834   (interactive "P")
5835   (let ((group (gnus-group-prefixed-name
5836                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5837     (and (gnus-gethash group gnus-newsrc-hashtb)
5838          (error "Archive group already exists"))
5839     (gnus-group-make-group
5840      (gnus-group-real-name group)
5841      (list 'nndir (if all "hpc" "edu")
5842            (list 'nndir-directory
5843                  (if all gnus-group-archive-directory
5844                    gnus-group-recent-archive-directory))))))
5845
5846 (defun gnus-group-make-directory-group (dir)
5847   "Create an nndir group.
5848 The user will be prompted for a directory.  The contents of this
5849 directory will be used as a newsgroup.  The directory should contain
5850 mail messages or news articles in files that have numeric names."
5851   (interactive
5852    (list (read-file-name "Create group from directory: ")))
5853   (or (file-exists-p dir) (error "No such directory"))
5854   (or (file-directory-p dir) (error "Not a directory"))
5855   (let ((ext "")
5856         (i 0)
5857         group)
5858     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5859       (setq group
5860             (gnus-group-prefixed-name
5861              (concat (file-name-as-directory (directory-file-name dir))
5862                      ext)
5863              '(nndir "")))
5864       (setq ext (format "<%d>" (setq i (1+ i)))))
5865     (gnus-group-make-group
5866      (gnus-group-real-name group)
5867      (list 'nndir group (list 'nndir-directory dir)))))
5868
5869 (defun gnus-group-make-kiboze-group (group address scores)
5870   "Create an nnkiboze group.
5871 The user will be prompted for a name, a regexp to match groups, and
5872 score file entries for articles to include in the group."
5873   (interactive
5874    (list
5875     (read-string "nnkiboze group name: ")
5876     (read-string "Source groups (regexp): ")
5877     (let ((headers (mapcar (lambda (group) (list group))
5878                            '("subject" "from" "number" "date" "message-id"
5879                              "references" "chars" "lines" "xref"
5880                              "followup" "all" "body" "head")))
5881           scores header regexp regexps)
5882       (while (not (equal "" (setq header (completing-read
5883                                           "Match on header: " headers nil t))))
5884         (setq regexps nil)
5885         (while (not (equal "" (setq regexp (read-string
5886                                             (format "Match on %s (string): "
5887                                                     header)))))
5888           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5889         (setq scores (cons (cons header regexps) scores)))
5890       scores)))
5891   (gnus-group-make-group group "nnkiboze" address)
5892   (nnheader-temp-write (gnus-score-file-name (concat "nnkiboze:" group))
5893     (let (emacs-lisp-mode-hook)
5894       (pp scores (current-buffer)))))
5895
5896 (defun gnus-group-add-to-virtual (n vgroup)
5897   "Add the current group to a virtual group."
5898   (interactive
5899    (list current-prefix-arg
5900          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5901                           "nnvirtual:")))
5902   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5903       (error "%s is not an nnvirtual group" vgroup))
5904   (let* ((groups (gnus-group-process-prefix n))
5905          (method (gnus-info-method (gnus-get-info vgroup))))
5906     (setcar (cdr method)
5907             (concat
5908              (nth 1 method) "\\|"
5909              (mapconcat
5910               (lambda (s)
5911                 (gnus-group-remove-mark s)
5912                 (concat "\\(^" (regexp-quote s) "$\\)"))
5913               groups "\\|"))))
5914   (gnus-group-position-point))
5915
5916 (defun gnus-group-make-empty-virtual (group)
5917   "Create a new, fresh, empty virtual group."
5918   (interactive "sCreate new, empty virtual group: ")
5919   (let* ((method (list 'nnvirtual "^$"))
5920          (pgroup (gnus-group-prefixed-name group method)))
5921     ;; Check whether it exists already.
5922     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5923          (error "Group %s already exists." pgroup))
5924     ;; Subscribe the new group after the group on the current line.
5925     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5926     (gnus-group-update-group pgroup)
5927     (forward-line -1)
5928     (gnus-group-position-point)))
5929
5930 (defun gnus-group-enter-directory (dir)
5931   "Enter an ephemeral nneething group."
5932   (interactive "DDirectory to read: ")
5933   (let* ((method (list 'nneething dir))
5934          (leaf (gnus-group-prefixed-name
5935                 (file-name-nondirectory (directory-file-name dir))
5936                 method))
5937          (name (gnus-generate-new-group-name leaf)))
5938     (let ((nneething-read-only t))
5939       (or (gnus-group-read-ephemeral-group
5940            name method t
5941            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5942                                       'summary 'group)))
5943           (error "Couldn't enter %s" dir)))))
5944
5945 ;; Group sorting commands
5946 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5947
5948 (defun gnus-group-sort-groups (func &optional reverse)
5949   "Sort the group buffer according to FUNC.
5950 If REVERSE, reverse the sorting order."
5951   (interactive (list gnus-group-sort-function
5952                      current-prefix-arg))
5953   (let ((func (cond 
5954                ((not (listp func)) func)
5955                ((null func) func)
5956                ((= 1 (length func)) (car func))
5957                (t `(lambda (t1 t2)
5958                      ,(gnus-make-sort-function 
5959                        (reverse func)))))))
5960     ;; We peel off the dummy group from the alist.
5961     (when func
5962       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5963         (pop gnus-newsrc-alist))
5964       ;; Do the sorting.
5965       (setq gnus-newsrc-alist
5966             (sort gnus-newsrc-alist func))
5967       (when reverse
5968         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5969       ;; Regenerate the hash table.
5970       (gnus-make-hashtable-from-newsrc-alist)
5971       (gnus-group-list-groups))))
5972
5973 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5974   "Sort the group buffer alphabetically by group name.
5975 If REVERSE, sort in reverse order."
5976   (interactive "P")
5977   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5978
5979 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5980   "Sort the group buffer by number of unread articles.
5981 If REVERSE, sort in reverse order."
5982   (interactive "P")
5983   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5984
5985 (defun gnus-group-sort-groups-by-level (&optional reverse)
5986   "Sort the group buffer by group level.
5987 If REVERSE, sort in reverse order."
5988   (interactive "P")
5989   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5990
5991 (defun gnus-group-sort-groups-by-score (&optional reverse)
5992   "Sort the group buffer by group score.
5993 If REVERSE, sort in reverse order."
5994   (interactive "P")
5995   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5996
5997 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5998   "Sort the group buffer by group rank.
5999 If REVERSE, sort in reverse order."
6000   (interactive "P")
6001   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
6002
6003 (defun gnus-group-sort-groups-by-method (&optional reverse)
6004   "Sort the group buffer alphabetically by backend name.
6005 If REVERSE, sort in reverse order."
6006   (interactive "P")
6007   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
6008
6009 (defun gnus-group-sort-by-alphabet (info1 info2)
6010   "Sort alphabetically."
6011   (string< (gnus-info-group info1) (gnus-info-group info2)))
6012
6013 (defun gnus-group-sort-by-unread (info1 info2)
6014   "Sort by number of unread articles."
6015   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
6016         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
6017     (< (or (and (numberp n1) n1) 0)
6018        (or (and (numberp n2) n2) 0))))
6019
6020 (defun gnus-group-sort-by-level (info1 info2)
6021   "Sort by level."
6022   (< (gnus-info-level info1) (gnus-info-level info2)))
6023
6024 (defun gnus-group-sort-by-method (info1 info2)
6025   "Sort alphabetically by backend name."
6026   (string< (symbol-name (car (gnus-find-method-for-group
6027                               (gnus-info-group info1) info1)))
6028            (symbol-name (car (gnus-find-method-for-group
6029                               (gnus-info-group info2) info2)))))
6030
6031 (defun gnus-group-sort-by-score (info1 info2)
6032   "Sort by group score."
6033   (< (gnus-info-score info1) (gnus-info-score info2)))
6034
6035 (defun gnus-group-sort-by-rank (info1 info2)
6036   "Sort by level and score."
6037   (let ((level1 (gnus-info-level info1))
6038         (level2 (gnus-info-level info2)))
6039     (or (< level1 level2)
6040         (and (= level1 level2)
6041              (> (gnus-info-score info1) (gnus-info-score info2))))))
6042
6043 ;; Group catching up.
6044
6045 (defun gnus-group-clear-data (n)
6046   "Clear all marks and read ranges from the current group."
6047   (interactive "P")
6048   (let ((groups (gnus-group-process-prefix n))
6049         group info)
6050     (while (setq group (pop groups))
6051       (setq info (gnus-get-info group))
6052       (gnus-info-set-read info nil)
6053       (when (gnus-info-marks info)
6054         (gnus-info-set-marks info nil))
6055       (gnus-get-unread-articles-in-group info (gnus-active group) t)
6056       (when (gnus-group-goto-group group)
6057         (gnus-group-remove-mark group)
6058         (gnus-group-update-group-line)))))
6059
6060 (defun gnus-group-catchup-current (&optional n all)
6061   "Mark all articles not marked as unread in current newsgroup as read.
6062 If prefix argument N is numeric, the ARG next newsgroups will be
6063 caught up.  If ALL is non-nil, marked articles will also be marked as
6064 read.  Cross references (Xref: header) of articles are ignored.
6065 The difference between N and actual number of newsgroups that were
6066 caught up is returned."
6067   (interactive "P")
6068   (unless (gnus-group-group-name)
6069     (error "No group on the current line"))
6070   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
6071                gnus-expert-user
6072                (gnus-y-or-n-p
6073                 (if all
6074                     "Do you really want to mark all articles as read? "
6075                   "Mark all unread articles as read? "))))
6076       n
6077     (let ((groups (gnus-group-process-prefix n))
6078           (ret 0))
6079       (while groups
6080         ;; Virtual groups have to be given special treatment.
6081         (let ((method (gnus-find-method-for-group (car groups))))
6082           (if (eq 'nnvirtual (car method))
6083               (nnvirtual-catchup-group
6084                (gnus-group-real-name (car groups)) (nth 1 method) all)))
6085         (gnus-group-remove-mark (car groups))
6086         (if (>= (gnus-group-group-level) gnus-level-zombie)
6087             (gnus-message 2 "Dead groups can't be caught up")
6088           (if (prog1
6089                   (gnus-group-goto-group (car groups))
6090                 (gnus-group-catchup (car groups) all))
6091               (gnus-group-update-group-line)
6092             (setq ret (1+ ret))))
6093         (setq groups (cdr groups)))
6094       (gnus-group-next-unread-group 1)
6095       ret)))
6096
6097 (defun gnus-group-catchup-current-all (&optional n)
6098   "Mark all articles in current newsgroup as read.
6099 Cross references (Xref: header) of articles are ignored."
6100   (interactive "P")
6101   (gnus-group-catchup-current n 'all))
6102
6103 (defun gnus-group-catchup (group &optional all)
6104   "Mark all articles in GROUP as read.
6105 If ALL is non-nil, all articles are marked as read.
6106 The return value is the number of articles that were marked as read,
6107 or nil if no action could be taken."
6108   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6109          (num (car entry)))
6110     ;; Do the updating only if the newsgroup isn't killed.
6111     (if (not (numberp (car entry)))
6112         (gnus-message 1 "Can't catch up; non-active group")
6113       ;; Do auto-expirable marks if that's required.
6114       (when (gnus-group-auto-expirable-p group)
6115         (gnus-add-marked-articles
6116          group 'expire (gnus-list-of-unread-articles group))
6117         (when all
6118           (let ((marks (nth 3 (nth 2 entry))))
6119             (gnus-add-marked-articles
6120              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
6121             (gnus-add-marked-articles
6122              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
6123       (when entry
6124         (gnus-update-read-articles group nil)
6125         ;; Also nix out the lists of marks and dormants.
6126         (when all
6127           (gnus-add-marked-articles group 'tick nil nil 'force)
6128           (gnus-add-marked-articles group 'dormant nil nil 'force))
6129         (run-hooks 'gnus-group-catchup-group-hook)
6130         num))))
6131
6132 (defun gnus-group-expire-articles (&optional n)
6133   "Expire all expirable articles in the current newsgroup."
6134   (interactive "P")
6135   (let ((groups (gnus-group-process-prefix n))
6136         group)
6137     (unless groups
6138       (error "No groups to expire"))
6139     (while (setq group (pop groups))
6140       (gnus-group-remove-mark group)
6141       (when (gnus-check-backend-function 'request-expire-articles group)
6142         (gnus-message 6 "Expiring articles in %s..." group)
6143         (let* ((info (gnus-get-info group))
6144                (expirable (if (gnus-group-total-expirable-p group)
6145                               (cons nil (gnus-list-of-read-articles group))
6146                             (assq 'expire (gnus-info-marks info))))
6147                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
6148           (when expirable
6149             (setcdr
6150              expirable
6151              (gnus-compress-sequence
6152               (if expiry-wait
6153                   ;; We set the expiry variables to the groupp
6154                   ;; parameter. 
6155                   (let ((nnmail-expiry-wait-function nil)
6156                         (nnmail-expiry-wait expiry-wait))
6157                     (gnus-request-expire-articles
6158                      (gnus-uncompress-sequence (cdr expirable)) group))
6159                 ;; Just expire using the normal expiry values.
6160                 (gnus-request-expire-articles
6161                  (gnus-uncompress-sequence (cdr expirable)) group)))))
6162           (gnus-message 6 "Expiring articles in %s...done" group)))
6163       (gnus-group-position-point))))
6164
6165 (defun gnus-group-expire-all-groups ()
6166   "Expire all expirable articles in all newsgroups."
6167   (interactive)
6168   (save-excursion
6169     (gnus-message 5 "Expiring...")
6170     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
6171                                      (cdr gnus-newsrc-alist))))
6172       (gnus-group-expire-articles nil)))
6173   (gnus-group-position-point)
6174   (gnus-message 5 "Expiring...done"))
6175
6176 (defun gnus-group-set-current-level (n level)
6177   "Set the level of the next N groups to LEVEL."
6178   (interactive
6179    (list
6180     current-prefix-arg
6181     (string-to-int
6182      (let ((s (read-string
6183                (format "Level (default %s): "
6184                        (or (gnus-group-group-level) 
6185                            gnus-level-default-subscribed)))))
6186        (if (string-match "^\\s-*$" s)
6187            (int-to-string (or (gnus-group-group-level) 
6188                               gnus-level-default-subscribed))
6189          s)))))
6190   (or (and (>= level 1) (<= level gnus-level-killed))
6191       (error "Illegal level: %d" level))
6192   (let ((groups (gnus-group-process-prefix n))
6193         group)
6194     (while (setq group (pop groups))
6195       (gnus-group-remove-mark group)
6196       (gnus-message 6 "Changed level of %s from %d to %d"
6197                     group (or (gnus-group-group-level) gnus-level-killed)
6198                     level)
6199       (gnus-group-change-level
6200        group level (or (gnus-group-group-level) gnus-level-killed))
6201       (gnus-group-update-group-line)))
6202   (gnus-group-position-point))
6203
6204 (defun gnus-group-unsubscribe-current-group (&optional n)
6205   "Toggle subscription of the current group.
6206 If given numerical prefix, toggle the N next groups."
6207   (interactive "P")
6208   (let ((groups (gnus-group-process-prefix n))
6209         group)
6210     (while groups
6211       (setq group (car groups)
6212             groups (cdr groups))
6213       (gnus-group-remove-mark group)
6214       (gnus-group-unsubscribe-group
6215        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
6216                  gnus-level-default-unsubscribed
6217                gnus-level-default-subscribed) t)
6218       (gnus-group-update-group-line))
6219     (gnus-group-next-group 1)))
6220
6221 (defun gnus-group-unsubscribe-group (group &optional level silent)
6222   "Toggle subscription to GROUP.
6223 Killed newsgroups are subscribed.  If SILENT, don't try to update the
6224 group line."
6225   (interactive
6226    (list (completing-read
6227           "Group: " gnus-active-hashtb nil
6228           (gnus-read-active-file-p)
6229           nil 
6230           'gnus-group-history)))
6231   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
6232     (cond
6233      ((string-match "^[ \t]$" group)
6234       (error "Empty group name"))
6235      (newsrc
6236       ;; Toggle subscription flag.
6237       (gnus-group-change-level
6238        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
6239                                       gnus-level-subscribed)
6240                                   (1+ gnus-level-subscribed)
6241                                 gnus-level-default-subscribed)))
6242       (unless silent
6243         (gnus-group-update-group group)))
6244      ((and (stringp group)
6245            (or (not (gnus-read-active-file-p))
6246                (gnus-active group)))
6247       ;; Add new newsgroup.
6248       (gnus-group-change-level
6249        group
6250        (if level level gnus-level-default-subscribed)
6251        (or (and (member group gnus-zombie-list)
6252                 gnus-level-zombie)
6253            gnus-level-killed)
6254        (and (gnus-group-group-name)
6255             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
6256       (unless silent
6257         (gnus-group-update-group group)))
6258      (t (error "No such newsgroup: %s" group)))
6259     (gnus-group-position-point)))
6260
6261 (defun gnus-group-transpose-groups (n)
6262   "Move the current newsgroup up N places.
6263 If given a negative prefix, move down instead.  The difference between
6264 N and the number of steps taken is returned."
6265   (interactive "p")
6266   (or (gnus-group-group-name)
6267       (error "No group on current line"))
6268   (gnus-group-kill-group 1)
6269   (prog1
6270       (forward-line (- n))
6271     (gnus-group-yank-group)
6272     (gnus-group-position-point)))
6273
6274 (defun gnus-group-kill-all-zombies ()
6275   "Kill all zombie newsgroups."
6276   (interactive)
6277   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6278   (setq gnus-zombie-list nil)
6279   (gnus-group-list-groups))
6280
6281 (defun gnus-group-kill-region (begin end)
6282   "Kill newsgroups in current region (excluding current point).
6283 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6284   (interactive "r")
6285   (let ((lines
6286          ;; Count lines.
6287          (save-excursion
6288            (count-lines
6289             (progn
6290               (goto-char begin)
6291               (beginning-of-line)
6292               (point))
6293             (progn
6294               (goto-char end)
6295               (beginning-of-line)
6296               (point))))))
6297     (goto-char begin)
6298     (beginning-of-line)                 ;Important when LINES < 1
6299     (gnus-group-kill-group lines)))
6300
6301 (defun gnus-group-kill-group (&optional n discard)
6302   "Kill the next N groups.
6303 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6304 However, only groups that were alive can be yanked; already killed
6305 groups or zombie groups can't be yanked.
6306 The return value is the name of the group that was killed, or a list
6307 of groups killed."
6308   (interactive "P")
6309   (let ((buffer-read-only nil)
6310         (groups (gnus-group-process-prefix n))
6311         group entry level out)
6312     (if (< (length groups) 10)
6313         ;; This is faster when there are few groups.
6314         (while groups
6315           (push (setq group (pop groups)) out)
6316           (gnus-group-remove-mark group)
6317           (setq level (gnus-group-group-level))
6318           (gnus-delete-line)
6319           (when (and (not discard)
6320                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6321             (push (cons (car entry) (nth 2 entry))
6322                   gnus-list-of-killed-groups))
6323           (gnus-group-change-level
6324            (if entry entry group) gnus-level-killed (if entry nil level)))
6325       ;; If there are lots and lots of groups to be killed, we use
6326       ;; this thing instead.
6327       (let (entry)
6328         (setq groups (nreverse groups))
6329         (while groups
6330           (gnus-group-remove-mark (setq group (pop groups)))
6331           (gnus-delete-line)
6332           (push group gnus-killed-list)
6333           (setq gnus-newsrc-alist
6334                 (delq (assoc group gnus-newsrc-alist)
6335                       gnus-newsrc-alist))
6336           (when gnus-group-change-level-function
6337             (funcall gnus-group-change-level-function group 9 3))
6338           (cond
6339            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6340             (push (cons (car entry) (nth 2 entry))
6341                   gnus-list-of-killed-groups)
6342             (setcdr (cdr entry) (cdddr entry)))
6343            ((member group gnus-zombie-list)
6344             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6345         (gnus-make-hashtable-from-newsrc-alist)))
6346
6347     (gnus-group-position-point)
6348     (if (< (length out) 2) (car out) (nreverse out))))
6349
6350 (defun gnus-group-yank-group (&optional arg)
6351   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6352 inserting it before the current newsgroup.  The numeric ARG specifies
6353 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6354 is returned, or (if several groups are yanked) a list of yanked groups
6355 is returned."
6356   (interactive "p")
6357   (setq arg (or arg 1))
6358   (let (info group prev out)
6359     (while (>= (decf arg) 0)
6360       (if (not (setq info (pop gnus-list-of-killed-groups)))
6361           (error "No more newsgroups to yank"))
6362       (push (setq group (nth 1 info)) out)
6363       ;; Find which newsgroup to insert this one before - search
6364       ;; backward until something suitable is found.  If there are no
6365       ;; other newsgroups in this buffer, just make this newsgroup the
6366       ;; first newsgroup.
6367       (setq prev (gnus-group-group-name))
6368       (gnus-group-change-level
6369        info (gnus-info-level (cdr info)) gnus-level-killed
6370        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6371        t)
6372       (gnus-group-insert-group-line-info group))
6373     (forward-line -1)
6374     (gnus-group-position-point)
6375     (if (< (length out) 2) (car out) (nreverse out))))
6376
6377 (defun gnus-group-kill-level (level)
6378   "Kill all groups that is on a certain LEVEL."
6379   (interactive "nKill all groups on level: ")
6380   (cond
6381    ((= level gnus-level-zombie)
6382     (setq gnus-killed-list
6383           (nconc gnus-zombie-list gnus-killed-list))
6384     (setq gnus-zombie-list nil))
6385    ((and (< level gnus-level-zombie)
6386          (> level 0)
6387          (or gnus-expert-user
6388              (gnus-yes-or-no-p
6389               (format
6390                "Do you really want to kill all groups on level %d? "
6391                level))))
6392     (let* ((prev gnus-newsrc-alist)
6393            (alist (cdr prev)))
6394       (while alist
6395         (if (= (gnus-info-level level) level)
6396             (setcdr prev (cdr alist))
6397           (setq prev alist))
6398         (setq alist (cdr alist)))
6399       (gnus-make-hashtable-from-newsrc-alist)
6400       (gnus-group-list-groups)))
6401    (t
6402     (error "Can't kill; illegal level: %d" level))))
6403
6404 (defun gnus-group-list-all-groups (&optional arg)
6405   "List all newsgroups with level ARG or lower.
6406 Default is gnus-level-unsubscribed, which lists all subscribed and most
6407 unsubscribed groups."
6408   (interactive "P")
6409   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6410
6411 ;; Redefine this to list ALL killed groups if prefix arg used.
6412 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6413 (defun gnus-group-list-killed (&optional arg)
6414   "List all killed newsgroups in the group buffer.
6415 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6416 entail asking the server for the groups."
6417   (interactive "P")
6418   ;; Find all possible killed newsgroups if arg.
6419   (when arg
6420     (gnus-get-killed-groups))
6421   (if (not gnus-killed-list)
6422       (gnus-message 6 "No killed groups")
6423     (let (gnus-group-list-mode)
6424       (funcall gnus-group-prepare-function
6425                gnus-level-killed t gnus-level-killed))
6426     (goto-char (point-min)))
6427   (gnus-group-position-point))
6428
6429 (defun gnus-group-list-zombies ()
6430   "List all zombie newsgroups in the group buffer."
6431   (interactive)
6432   (if (not gnus-zombie-list)
6433       (gnus-message 6 "No zombie groups")
6434     (let (gnus-group-list-mode)
6435       (funcall gnus-group-prepare-function
6436                gnus-level-zombie t gnus-level-zombie))
6437     (goto-char (point-min)))
6438   (gnus-group-position-point))
6439
6440 (defun gnus-group-list-active ()
6441   "List all groups that are available from the server(s)."
6442   (interactive)
6443   ;; First we make sure that we have really read the active file.
6444   (unless (gnus-read-active-file-p)
6445     (let ((gnus-read-active-file t))
6446       (gnus-read-active-file)))
6447   ;; Find all groups and sort them.
6448   (let ((groups
6449          (sort
6450           (let (list)
6451             (mapatoms
6452              (lambda (sym)
6453                (and (boundp sym)
6454                     (symbol-value sym)
6455                     (setq list (cons (symbol-name sym) list))))
6456              gnus-active-hashtb)
6457             list)
6458           'string<))
6459         (buffer-read-only nil))
6460     (erase-buffer)
6461     (while groups
6462       (gnus-group-insert-group-line-info (pop groups)))
6463     (goto-char (point-min))))
6464
6465 (defun gnus-activate-all-groups (level)
6466   "Activate absolutely all groups."
6467   (interactive (list 7))
6468   (let ((gnus-activate-level level)
6469         (gnus-activate-foreign-newsgroups level))
6470     (gnus-group-get-new-news)))
6471
6472 (defun gnus-group-get-new-news (&optional arg)
6473   "Get newly arrived articles.
6474 If ARG is a number, it specifies which levels you are interested in
6475 re-scanning.  If ARG is non-nil and not a number, this will force
6476 \"hard\" re-reading of the active files from all servers."
6477   (interactive "P")
6478   (run-hooks 'gnus-get-new-news-hook)
6479   ;; We might read in new NoCeM messages here.
6480   (when (and gnus-use-nocem 
6481              (null arg))
6482     (gnus-nocem-scan-groups))
6483   ;; If ARG is not a number, then we read the active file.
6484   (when (and arg (not (numberp arg)))
6485     (let ((gnus-read-active-file t))
6486       (gnus-read-active-file))
6487     (setq arg nil))
6488
6489   (setq arg (gnus-group-default-level arg t))
6490   (if (and gnus-read-active-file (not arg))
6491       (progn
6492         (gnus-read-active-file)
6493         (gnus-get-unread-articles arg))
6494     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6495       (gnus-get-unread-articles arg)))
6496   (run-hooks 'gnus-after-getting-new-news-hook)
6497   (gnus-group-list-groups))
6498
6499 (defun gnus-group-get-new-news-this-group (&optional n)
6500   "Check for newly arrived news in the current group (and the N-1 next groups).
6501 The difference between N and the number of newsgroup checked is returned.
6502 If N is negative, this group and the N-1 previous groups will be checked."
6503   (interactive "P")
6504   (let* ((groups (gnus-group-process-prefix n))
6505          (ret (if (numberp n) (- n (length groups)) 0))
6506          (beg (unless n (point)))
6507          group)
6508     (while (setq group (pop groups))
6509       (gnus-group-remove-mark group)
6510       (if (gnus-activate-group group 'scan)
6511           (progn
6512             (gnus-get-unread-articles-in-group
6513              (gnus-get-info group) (gnus-active group) t)
6514             (unless (gnus-virtual-group-p group)
6515               (gnus-close-group group))
6516             (gnus-group-update-group group))
6517         (gnus-error 3 "%s error: %s" group (gnus-status-message group))))
6518     (when beg (goto-char beg))
6519     (when gnus-goto-next-group-when-activating
6520       (gnus-group-next-unread-group 1 t))
6521     (gnus-summary-position-point)
6522     ret))
6523
6524 (defun gnus-group-fetch-faq (group &optional faq-dir)
6525   "Fetch the FAQ for the current group."
6526   (interactive
6527    (list
6528     (and (gnus-group-group-name)
6529          (gnus-group-real-name (gnus-group-group-name)))
6530     (cond (current-prefix-arg
6531            (completing-read
6532             "Faq dir: " (and (listp gnus-group-faq-directory)
6533                              (mapcar (lambda (file) (list file))
6534                                      gnus-group-faq-directory)))))))
6535   (or faq-dir
6536       (setq faq-dir (if (listp gnus-group-faq-directory)
6537                         (car gnus-group-faq-directory)
6538                       gnus-group-faq-directory)))
6539   (or group (error "No group name given"))
6540   (let ((file (concat (file-name-as-directory faq-dir)
6541                       (gnus-group-real-name group))))
6542     (if (not (file-exists-p file))
6543         (error "No such file: %s" file)
6544       (find-file file))))
6545
6546 (defun gnus-group-describe-group (force &optional group)
6547   "Display a description of the current newsgroup."
6548   (interactive (list current-prefix-arg (gnus-group-group-name)))
6549   (when (and force
6550              gnus-description-hashtb)
6551     (gnus-sethash group nil gnus-description-hashtb))
6552   (let ((method (gnus-find-method-for-group group))
6553         desc)
6554     (or group (error "No group name given"))
6555     (and (or (and gnus-description-hashtb
6556                   ;; We check whether this group's method has been
6557                   ;; queried for a description file.
6558                   (gnus-gethash
6559                    (gnus-group-prefixed-name "" method)
6560                    gnus-description-hashtb))
6561              (setq desc (gnus-group-get-description group))
6562              (gnus-read-descriptions-file method))
6563          (gnus-message 1
6564           (or desc (gnus-gethash group gnus-description-hashtb)
6565               "No description available")))))
6566
6567 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6568 (defun gnus-group-describe-all-groups (&optional force)
6569   "Pop up a buffer with descriptions of all newsgroups."
6570   (interactive "P")
6571   (and force (setq gnus-description-hashtb nil))
6572   (if (not (or gnus-description-hashtb
6573                (gnus-read-all-descriptions-files)))
6574       (error "Couldn't request descriptions file"))
6575   (let ((buffer-read-only nil)
6576         b)
6577     (erase-buffer)
6578     (mapatoms
6579      (lambda (group)
6580        (setq b (point))
6581        (insert (format "      *: %-20s %s\n" (symbol-name group)
6582                        (symbol-value group)))
6583        (gnus-add-text-properties
6584         b (1+ b) (list 'gnus-group group
6585                        'gnus-unread t 'gnus-marked nil
6586                        'gnus-level (1+ gnus-level-subscribed))))
6587      gnus-description-hashtb)
6588     (goto-char (point-min))
6589     (gnus-group-position-point)))
6590
6591 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6592 (defun gnus-group-apropos (regexp &optional search-description)
6593   "List all newsgroups that have names that match a regexp."
6594   (interactive "sGnus apropos (regexp): ")
6595   (let ((prev "")
6596         (obuf (current-buffer))
6597         groups des)
6598     ;; Go through all newsgroups that are known to Gnus.
6599     (mapatoms
6600      (lambda (group)
6601        (and (symbol-name group)
6602             (string-match regexp (symbol-name group))
6603             (setq groups (cons (symbol-name group) groups))))
6604      gnus-active-hashtb)
6605     ;; Also go through all descriptions that are known to Gnus.
6606     (when search-description
6607       (mapatoms
6608        (lambda (group)
6609          (and (string-match regexp (symbol-value group))
6610               (gnus-active (symbol-name group))
6611               (setq groups (cons (symbol-name group) groups))))
6612        gnus-description-hashtb))
6613     (if (not groups)
6614         (gnus-message 3 "No groups matched \"%s\"." regexp)
6615       ;; Print out all the groups.
6616       (save-excursion
6617         (pop-to-buffer "*Gnus Help*")
6618         (buffer-disable-undo (current-buffer))
6619         (erase-buffer)
6620         (setq groups (sort groups 'string<))
6621         (while groups
6622           ;; Groups may be entered twice into the list of groups.
6623           (if (not (string= (car groups) prev))
6624               (progn
6625                 (insert (setq prev (car groups)) "\n")
6626                 (if (and gnus-description-hashtb
6627                          (setq des (gnus-gethash (car groups)
6628                                                  gnus-description-hashtb)))
6629                     (insert "  " des "\n"))))
6630           (setq groups (cdr groups)))
6631         (goto-char (point-min))))
6632     (pop-to-buffer obuf)))
6633
6634 (defun gnus-group-description-apropos (regexp)
6635   "List all newsgroups that have names or descriptions that match a regexp."
6636   (interactive "sGnus description apropos (regexp): ")
6637   (if (not (or gnus-description-hashtb
6638                (gnus-read-all-descriptions-files)))
6639       (error "Couldn't request descriptions file"))
6640   (gnus-group-apropos regexp t))
6641
6642 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6643 (defun gnus-group-list-matching (level regexp &optional all lowest)
6644   "List all groups with unread articles that match REGEXP.
6645 If the prefix LEVEL is non-nil, it should be a number that says which
6646 level to cut off listing groups.
6647 If ALL, also list groups with no unread articles.
6648 If LOWEST, don't list groups with level lower than LOWEST.
6649
6650 This command may read the active file."
6651   (interactive "P\nsList newsgroups matching: ")
6652   ;; First make sure active file has been read.
6653   (when (and level
6654              (> (prefix-numeric-value level) gnus-level-killed))
6655     (gnus-get-killed-groups))
6656   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6657                            all (or lowest 1) regexp)
6658   (goto-char (point-min))
6659   (gnus-group-position-point))
6660
6661 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6662   "List all groups that match REGEXP.
6663 If the prefix LEVEL is non-nil, it should be a number that says which
6664 level to cut off listing groups.
6665 If LOWEST, don't list groups with level lower than LOWEST."
6666   (interactive "P\nsList newsgroups matching: ")
6667   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6668
6669 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6670 (defun gnus-group-save-newsrc (&optional force)
6671   "Save the Gnus startup files.
6672 If FORCE, force saving whether it is necessary or not."
6673   (interactive "P")
6674   (gnus-save-newsrc-file force))
6675
6676 (defun gnus-group-restart (&optional arg)
6677   "Force Gnus to read the .newsrc file."
6678   (interactive "P")
6679   (when (gnus-yes-or-no-p
6680          (format "Are you sure you want to read %s? "
6681                  gnus-current-startup-file))
6682     (gnus-save-newsrc-file)
6683     (gnus-setup-news 'force)
6684     (gnus-group-list-groups arg)))
6685
6686 (defun gnus-group-read-init-file ()
6687   "Read the Gnus elisp init file."
6688   (interactive)
6689   (gnus-read-init-file))
6690
6691 (defun gnus-group-check-bogus-groups (&optional silent)
6692   "Check bogus newsgroups.
6693 If given a prefix, don't ask for confirmation before removing a bogus
6694 group."
6695   (interactive "P")
6696   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6697   (gnus-group-list-groups))
6698
6699 (defun gnus-group-edit-global-kill (&optional article group)
6700   "Edit the global kill file.
6701 If GROUP, edit that local kill file instead."
6702   (interactive "P")
6703   (setq gnus-current-kill-article article)
6704   (gnus-kill-file-edit-file group)
6705   (gnus-message
6706    6
6707    (substitute-command-keys
6708     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6709             (if group "local" "global")))))
6710
6711 (defun gnus-group-edit-local-kill (article group)
6712   "Edit a local kill file."
6713   (interactive (list nil (gnus-group-group-name)))
6714   (gnus-group-edit-global-kill article group))
6715
6716 (defun gnus-group-force-update ()
6717   "Update `.newsrc' file."
6718   (interactive)
6719   (gnus-save-newsrc-file))
6720
6721 (defun gnus-group-suspend ()
6722   "Suspend the current Gnus session.
6723 In fact, cleanup buffers except for group mode buffer.
6724 The hook gnus-suspend-gnus-hook is called before actually suspending."
6725   (interactive)
6726   (run-hooks 'gnus-suspend-gnus-hook)
6727   ;; Kill Gnus buffers except for group mode buffer.
6728   (let ((group-buf (get-buffer gnus-group-buffer)))
6729     ;; Do this on a separate list in case the user does a ^G before we finish
6730     (let ((gnus-buffer-list
6731            (delq group-buf (delq gnus-dribble-buffer
6732                                  (append gnus-buffer-list nil)))))
6733       (while gnus-buffer-list
6734         (gnus-kill-buffer (car gnus-buffer-list))
6735         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6736     (if group-buf
6737         (progn
6738           (setq gnus-buffer-list (list group-buf))
6739           (bury-buffer group-buf)
6740           (delete-windows-on group-buf t)))))
6741
6742 (defun gnus-group-clear-dribble ()
6743   "Clear all information from the dribble buffer."
6744   (interactive)
6745   (gnus-dribble-clear)
6746   (gnus-message 7 "Cleared dribble buffer"))
6747
6748 (defun gnus-group-exit ()
6749   "Quit reading news after updating .newsrc.eld and .newsrc.
6750 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6751   (interactive)
6752   (when 
6753       (or noninteractive                ;For gnus-batch-kill
6754           (not gnus-interactive-exit)   ;Without confirmation
6755           gnus-expert-user
6756           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6757     (run-hooks 'gnus-exit-gnus-hook)
6758     ;; Offer to save data from non-quitted summary buffers.
6759     (gnus-offer-save-summaries)
6760     ;; Save the newsrc file(s).
6761     (gnus-save-newsrc-file)
6762     ;; Kill-em-all.
6763     (gnus-close-backends)
6764     ;; Reset everything.
6765     (gnus-clear-system)
6766     ;; Allow the user to do things after cleaning up.
6767     (run-hooks 'gnus-after-exiting-gnus-hook)))
6768
6769 (defun gnus-close-backends ()
6770   ;; Send a close request to all backends that support such a request.
6771   (let ((methods gnus-valid-select-methods)
6772         func)
6773     (while methods
6774       (if (fboundp (setq func (intern (concat (caar methods)
6775                                               "-request-close"))))
6776           (funcall func))
6777       (setq methods (cdr methods)))))
6778
6779 (defun gnus-group-quit ()
6780   "Quit reading news without updating .newsrc.eld or .newsrc.
6781 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6782   (interactive)
6783   (when (or noninteractive              ;For gnus-batch-kill
6784             (zerop (buffer-size))
6785             (not (gnus-server-opened gnus-select-method))
6786             gnus-expert-user
6787             (not gnus-current-startup-file)
6788             (gnus-yes-or-no-p
6789              (format "Quit reading news without saving %s? "
6790                      (file-name-nondirectory gnus-current-startup-file))))
6791     (run-hooks 'gnus-exit-gnus-hook)
6792     (if gnus-use-full-window
6793         (delete-other-windows)
6794       (gnus-remove-some-windows))
6795     (gnus-dribble-save)
6796     (gnus-close-backends)
6797     (gnus-clear-system)
6798     ;; Allow the user to do things after cleaning up.
6799     (run-hooks 'gnus-after-exiting-gnus-hook)))
6800
6801 (defun gnus-offer-save-summaries ()
6802   "Offer to save all active summary buffers."
6803   (save-excursion
6804     (let ((buflist (buffer-list))
6805           buffers bufname)
6806       ;; Go through all buffers and find all summaries.
6807       (while buflist
6808         (and (setq bufname (buffer-name (car buflist)))
6809              (string-match "Summary" bufname)
6810              (save-excursion
6811                (set-buffer bufname)
6812                ;; We check that this is, indeed, a summary buffer.
6813                (and (eq major-mode 'gnus-summary-mode)
6814                     ;; Also make sure this isn't bogus.
6815                     gnus-newsgroup-prepared))
6816              (push bufname buffers))
6817         (setq buflist (cdr buflist)))
6818       ;; Go through all these summary buffers and offer to save them.
6819       (when buffers
6820         (map-y-or-n-p
6821          "Update summary buffer %s? "
6822          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6823          buffers)))))
6824
6825 (defun gnus-group-describe-briefly ()
6826   "Give a one line description of the group mode commands."
6827   (interactive)
6828   (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")))
6829
6830 (defun gnus-group-browse-foreign-server (method)
6831   "Browse a foreign news server.
6832 If called interactively, this function will ask for a select method
6833  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6834 If not, METHOD should be a list where the first element is the method
6835 and the second element is the address."
6836   (interactive
6837    (list (let ((how (completing-read
6838                      "Which backend: "
6839                      (append gnus-valid-select-methods gnus-server-alist)
6840                      nil t (cons "nntp" 0) 'gnus-method-history)))
6841            ;; We either got a backend name or a virtual server name.
6842            ;; If the first, we also need an address.
6843            (if (assoc how gnus-valid-select-methods)
6844                (list (intern how)
6845                      ;; Suggested by mapjph@bath.ac.uk.
6846                      (completing-read
6847                       "Address: "
6848                       (mapcar (lambda (server) (list server))
6849                               gnus-secondary-servers)))
6850              ;; We got a server name, so we find the method.
6851              (gnus-server-to-method how)))))
6852   (gnus-browse-foreign-server method))
6853
6854 \f
6855 ;;;
6856 ;;; Gnus summary mode
6857 ;;;
6858
6859 (defvar gnus-summary-mode-map nil)
6860
6861 (put 'gnus-summary-mode 'mode-class 'special)
6862
6863 (unless gnus-summary-mode-map
6864   (setq gnus-summary-mode-map (make-keymap))
6865   (suppress-keymap gnus-summary-mode-map)
6866
6867   ;; Non-orthogonal keys
6868
6869   (gnus-define-keys gnus-summary-mode-map
6870     " " gnus-summary-next-page
6871     "\177" gnus-summary-prev-page
6872     [delete] gnus-summary-prev-page
6873     "\r" gnus-summary-scroll-up
6874     "n" gnus-summary-next-unread-article
6875     "p" gnus-summary-prev-unread-article
6876     "N" gnus-summary-next-article
6877     "P" gnus-summary-prev-article
6878     "\M-\C-n" gnus-summary-next-same-subject
6879     "\M-\C-p" gnus-summary-prev-same-subject
6880     "\M-n" gnus-summary-next-unread-subject
6881     "\M-p" gnus-summary-prev-unread-subject
6882     "." gnus-summary-first-unread-article
6883     "," gnus-summary-best-unread-article
6884     "\M-s" gnus-summary-search-article-forward
6885     "\M-r" gnus-summary-search-article-backward
6886     "<" gnus-summary-beginning-of-article
6887     ">" gnus-summary-end-of-article
6888     "j" gnus-summary-goto-article
6889     "^" gnus-summary-refer-parent-article
6890     "\M-^" gnus-summary-refer-article
6891     "u" gnus-summary-tick-article-forward
6892     "!" gnus-summary-tick-article-forward
6893     "U" gnus-summary-tick-article-backward
6894     "d" gnus-summary-mark-as-read-forward
6895     "D" gnus-summary-mark-as-read-backward
6896     "E" gnus-summary-mark-as-expirable
6897     "\M-u" gnus-summary-clear-mark-forward
6898     "\M-U" gnus-summary-clear-mark-backward
6899     "k" gnus-summary-kill-same-subject-and-select
6900     "\C-k" gnus-summary-kill-same-subject
6901     "\M-\C-k" gnus-summary-kill-thread
6902     "\M-\C-l" gnus-summary-lower-thread
6903     "e" gnus-summary-edit-article
6904     "#" gnus-summary-mark-as-processable
6905     "\M-#" gnus-summary-unmark-as-processable
6906     "\M-\C-t" gnus-summary-toggle-threads
6907     "\M-\C-s" gnus-summary-show-thread
6908     "\M-\C-h" gnus-summary-hide-thread
6909     "\M-\C-f" gnus-summary-next-thread
6910     "\M-\C-b" gnus-summary-prev-thread
6911     "\M-\C-u" gnus-summary-up-thread
6912     "\M-\C-d" gnus-summary-down-thread
6913     "&" gnus-summary-execute-command
6914     "c" gnus-summary-catchup-and-exit
6915     "\C-w" gnus-summary-mark-region-as-read
6916     "\C-t" gnus-summary-toggle-truncation
6917     "?" gnus-summary-mark-as-dormant
6918     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6919     "\C-c\C-s\C-n" gnus-summary-sort-by-number
6920     "\C-c\C-s\C-a" gnus-summary-sort-by-author
6921     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6922     "\C-c\C-s\C-d" gnus-summary-sort-by-date
6923     "\C-c\C-s\C-i" gnus-summary-sort-by-score
6924     "=" gnus-summary-expand-window
6925     "\C-x\C-s" gnus-summary-reselect-current-group
6926     "\M-g" gnus-summary-rescan-group
6927     "w" gnus-summary-stop-page-breaking
6928     "\C-c\C-r" gnus-summary-caesar-message
6929     "\M-t" gnus-summary-toggle-mime
6930     "f" gnus-summary-followup
6931     "F" gnus-summary-followup-with-original
6932     "C" gnus-summary-cancel-article
6933     "r" gnus-summary-reply
6934     "R" gnus-summary-reply-with-original
6935     "\C-c\C-f" gnus-summary-mail-forward
6936     "o" gnus-summary-save-article
6937     "\C-o" gnus-summary-save-article-mail
6938     "|" gnus-summary-pipe-output
6939     "\M-k" gnus-summary-edit-local-kill
6940     "\M-K" gnus-summary-edit-global-kill
6941     "V" gnus-version
6942     "\C-c\C-d" gnus-summary-describe-group
6943     "q" gnus-summary-exit
6944     "Q" gnus-summary-exit-no-update
6945     "\C-c\C-i" gnus-info-find-node
6946     gnus-mouse-2 gnus-mouse-pick-article
6947     "m" gnus-summary-mail-other-window
6948     "a" gnus-summary-post-news
6949     "x" gnus-summary-limit-to-unread
6950     "s" gnus-summary-isearch-article
6951     "t" gnus-article-hide-headers
6952     "g" gnus-summary-show-article
6953     "l" gnus-summary-goto-last-article
6954     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6955     "\C-d" gnus-summary-enter-digest-group
6956     "\C-c\C-b" gnus-bug
6957     "*" gnus-cache-enter-article
6958     "\M-*" gnus-cache-remove-article
6959     "\M-&" gnus-summary-universal-argument
6960     "\C-l" gnus-recenter
6961     "I" gnus-summary-increase-score
6962     "L" gnus-summary-lower-score
6963
6964     "V" gnus-summary-score-map
6965     "X" gnus-uu-extract-map
6966     "S" gnus-summary-send-map)
6967
6968   ;; Sort of orthogonal keymap
6969   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
6970     "t" gnus-summary-tick-article-forward
6971     "!" gnus-summary-tick-article-forward
6972     "d" gnus-summary-mark-as-read-forward
6973     "r" gnus-summary-mark-as-read-forward
6974     "c" gnus-summary-clear-mark-forward
6975     " " gnus-summary-clear-mark-forward
6976     "e" gnus-summary-mark-as-expirable
6977     "x" gnus-summary-mark-as-expirable
6978     "?" gnus-summary-mark-as-dormant
6979     "b" gnus-summary-set-bookmark
6980     "B" gnus-summary-remove-bookmark
6981     "#" gnus-summary-mark-as-processable
6982     "\M-#" gnus-summary-unmark-as-processable
6983     "S" gnus-summary-limit-include-expunged
6984     "C" gnus-summary-catchup
6985     "H" gnus-summary-catchup-to-here
6986     "\C-c" gnus-summary-catchup-all
6987     "k" gnus-summary-kill-same-subject-and-select
6988     "K" gnus-summary-kill-same-subject
6989     "P" gnus-uu-mark-map)
6990
6991   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
6992     "c" gnus-summary-clear-above
6993     "u" gnus-summary-tick-above
6994     "m" gnus-summary-mark-above
6995     "k" gnus-summary-kill-below)
6996
6997   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
6998     "/" gnus-summary-limit-to-subject
6999     "n" gnus-summary-limit-to-articles
7000     "w" gnus-summary-pop-limit
7001     "s" gnus-summary-limit-to-subject
7002     "a" gnus-summary-limit-to-author
7003     "u" gnus-summary-limit-to-unread
7004     "m" gnus-summary-limit-to-marks
7005     "v" gnus-summary-limit-to-score
7006     "D" gnus-summary-limit-include-dormant
7007     "d" gnus-summary-limit-exclude-dormant
7008     ;;  "t" gnus-summary-limit-exclude-thread
7009     "E" gnus-summary-limit-include-expunged
7010     "c" gnus-summary-limit-exclude-childless-dormant
7011     "C" gnus-summary-limit-mark-excluded-as-read)
7012
7013   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
7014     "n" gnus-summary-next-unread-article
7015     "p" gnus-summary-prev-unread-article
7016     "N" gnus-summary-next-article
7017     "P" gnus-summary-prev-article
7018     "\C-n" gnus-summary-next-same-subject
7019     "\C-p" gnus-summary-prev-same-subject
7020     "\M-n" gnus-summary-next-unread-subject
7021     "\M-p" gnus-summary-prev-unread-subject
7022     "f" gnus-summary-first-unread-article
7023     "b" gnus-summary-best-unread-article
7024     "j" gnus-summary-goto-article
7025     "g" gnus-summary-goto-subject
7026     "l" gnus-summary-goto-last-article
7027     "p" gnus-summary-pop-article)
7028
7029   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
7030     "k" gnus-summary-kill-thread
7031     "l" gnus-summary-lower-thread
7032     "i" gnus-summary-raise-thread
7033     "T" gnus-summary-toggle-threads
7034     "t" gnus-summary-rethread-current
7035     "^" gnus-summary-reparent-thread
7036     "s" gnus-summary-show-thread
7037     "S" gnus-summary-show-all-threads
7038     "h" gnus-summary-hide-thread
7039     "H" gnus-summary-hide-all-threads
7040     "n" gnus-summary-next-thread
7041     "p" gnus-summary-prev-thread
7042     "u" gnus-summary-up-thread
7043     "o" gnus-summary-top-thread
7044     "d" gnus-summary-down-thread
7045     "#" gnus-uu-mark-thread
7046     "\M-#" gnus-uu-unmark-thread)
7047
7048   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
7049     "c" gnus-summary-catchup-and-exit
7050     "C" gnus-summary-catchup-all-and-exit
7051     "E" gnus-summary-exit-no-update
7052     "Q" gnus-summary-exit
7053     "Z" gnus-summary-exit
7054     "n" gnus-summary-catchup-and-goto-next-group
7055     "R" gnus-summary-reselect-current-group
7056     "G" gnus-summary-rescan-group
7057     "N" gnus-summary-next-group
7058     "P" gnus-summary-prev-group)
7059
7060   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
7061     " " gnus-summary-next-page
7062     "n" gnus-summary-next-page
7063     "\177" gnus-summary-prev-page
7064     [delete] gnus-summary-prev-page
7065     "p" gnus-summary-prev-page
7066     "\r" gnus-summary-scroll-up
7067     "<" gnus-summary-beginning-of-article
7068     ">" gnus-summary-end-of-article
7069     "b" gnus-summary-beginning-of-article
7070     "e" gnus-summary-end-of-article
7071     "^" gnus-summary-refer-parent-article
7072     "r" gnus-summary-refer-parent-article
7073     "R" gnus-summary-refer-references
7074     "g" gnus-summary-show-article
7075     "s" gnus-summary-isearch-article)
7076
7077   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
7078     "b" gnus-article-add-buttons
7079     "B" gnus-article-add-buttons-to-head
7080     "o" gnus-article-treat-overstrike
7081     ;;  "w" gnus-article-word-wrap
7082     "w" gnus-article-fill-cited-article
7083     "c" gnus-article-remove-cr
7084     "L" gnus-article-remove-trailing-blank-lines
7085     "q" gnus-article-de-quoted-unreadable
7086     "f" gnus-article-display-x-face
7087     "l" gnus-summary-stop-page-breaking
7088     "r" gnus-summary-caesar-message
7089     "t" gnus-article-hide-headers
7090     "v" gnus-summary-verbose-headers
7091     "m" gnus-summary-toggle-mime)
7092
7093   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
7094     "a" gnus-article-hide
7095     "h" gnus-article-hide-headers
7096     "b" gnus-article-hide-boring-headers
7097     "s" gnus-article-hide-signature
7098     "c" gnus-article-hide-citation
7099     "p" gnus-article-hide-pgp
7100     "\C-c" gnus-article-hide-citation-maybe)
7101
7102   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
7103     "a" gnus-article-highlight
7104     "h" gnus-article-highlight-headers
7105     "c" gnus-article-highlight-citation
7106     "s" gnus-article-highlight-signature)
7107
7108   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
7109     "z" gnus-article-date-ut
7110     "u" gnus-article-date-ut
7111     "l" gnus-article-date-local
7112     "e" gnus-article-date-lapsed
7113     "o" gnus-article-date-original)
7114
7115   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
7116     "v" gnus-version
7117     "f" gnus-summary-fetch-faq
7118     "d" gnus-summary-describe-group
7119     "h" gnus-summary-describe-briefly
7120     "i" gnus-info-find-node)
7121
7122   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
7123     "e" gnus-summary-expire-articles
7124     "\M-\C-e" gnus-summary-expire-articles-now
7125     "\177" gnus-summary-delete-article
7126     [delete] gnus-summary-delete-article
7127     "m" gnus-summary-move-article
7128     "r" gnus-summary-respool-article
7129     "w" gnus-summary-edit-article
7130     "c" gnus-summary-copy-article
7131     "B" gnus-summary-crosspost-article
7132     "q" gnus-summary-respool-query
7133     "i" gnus-summary-import-article)
7134
7135   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
7136     "o" gnus-summary-save-article
7137     "m" gnus-summary-save-article-mail
7138     "r" gnus-summary-save-article-rmail
7139     "f" gnus-summary-save-article-file
7140     "b" gnus-summary-save-article-body-file
7141     "h" gnus-summary-save-article-folder
7142     "v" gnus-summary-save-article-vm
7143     "p" gnus-summary-pipe-output
7144     "s" gnus-soup-add-article)
7145   )
7146
7147 \f
7148
7149 (defun gnus-summary-mode (&optional group)
7150   "Major mode for reading articles.
7151
7152 All normal editing commands are switched off.
7153 \\<gnus-summary-mode-map>
7154 Each line in this buffer represents one article.  To read an
7155 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
7156 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
7157 respectively.
7158
7159 You can also post articles and send mail from this buffer.  To
7160 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
7161 of an article, type `\\[gnus-summary-reply]'.
7162
7163 There are approx. one gazillion commands you can execute in this
7164 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
7165
7166 The following commands are available:
7167
7168 \\{gnus-summary-mode-map}"
7169   (interactive)
7170   (when (and menu-bar-mode
7171              (gnus-visual-p 'summary-menu 'menu))
7172     (gnus-summary-make-menu-bar))
7173   (kill-all-local-variables)
7174   (gnus-summary-make-local-variables)
7175   (gnus-make-thread-indent-array)
7176   (gnus-simplify-mode-line)
7177   (setq major-mode 'gnus-summary-mode)
7178   (setq mode-name "Summary")
7179   (make-local-variable 'minor-mode-alist)
7180   (use-local-map gnus-summary-mode-map)
7181   (buffer-disable-undo (current-buffer))
7182   (setq buffer-read-only t)             ;Disable modification
7183   (setq truncate-lines t)
7184   (setq selective-display t)
7185   (setq selective-display-ellipses t)   ;Display `...'
7186   (setq buffer-display-table gnus-summary-display-table)
7187   (setq gnus-newsgroup-name group)
7188   (make-local-variable 'gnus-summary-line-format)
7189   (make-local-variable 'gnus-summary-line-format-spec)
7190   (make-local-variable 'gnus-summary-mark-positions)
7191   (run-hooks 'gnus-summary-mode-hook))
7192
7193 (defun gnus-summary-make-local-variables ()
7194   "Make all the local summary buffer variables."
7195   (let ((locals gnus-summary-local-variables)
7196         global local)
7197     (while (setq local (pop locals))
7198       (if (consp local)
7199           (progn
7200             (if (eq (cdr local) 'global)
7201                 ;; Copy the global value of the variable.
7202                 (setq global (symbol-value (car local)))
7203               ;; Use the value from the list.
7204               (setq global (eval (cdr local))))
7205             (make-local-variable (car local))
7206             (set (car local) global))
7207         ;; Simple nil-valued local variable.
7208         (make-local-variable local)
7209         (set local nil)))))
7210
7211 (defun gnus-summary-make-display-table ()
7212   ;; Change the display table.  Odd characters have a tendency to mess
7213   ;; up nicely formatted displays - we make all possible glyphs
7214   ;; display only a single character.
7215
7216   ;; We start from the standard display table, if any.
7217   (setq gnus-summary-display-table
7218         (or (copy-sequence standard-display-table)
7219             (make-display-table)))
7220   ;; Nix out all the control chars...
7221   (let ((i 32))
7222     (while (>= (setq i (1- i)) 0)
7223       (aset gnus-summary-display-table i [??])))
7224   ;; ... but not newline and cr, of course. (cr is necessary for the
7225   ;; selective display).
7226   (aset gnus-summary-display-table ?\n nil)
7227   (aset gnus-summary-display-table ?\r nil)
7228   ;; We nix out any glyphs over 126 that are not set already.
7229   (let ((i 256))
7230     (while (>= (setq i (1- i)) 127)
7231       ;; Only modify if the entry is nil.
7232       (or (aref gnus-summary-display-table i)
7233           (aset gnus-summary-display-table i [??])))))
7234
7235 (defun gnus-summary-clear-local-variables ()
7236   (let ((locals gnus-summary-local-variables))
7237     (while locals
7238       (if (consp (car locals))
7239           (and (vectorp (caar locals))
7240                (set (caar locals) nil))
7241         (and (vectorp (car locals))
7242              (set (car locals) nil)))
7243       (setq locals (cdr locals)))))
7244
7245 ;; Summary data functions.
7246
7247 (defmacro gnus-data-number (data)
7248   `(car ,data))
7249
7250 (defmacro gnus-data-set-number (data number)
7251   `(setcar ,data ,number))
7252
7253 (defmacro gnus-data-mark (data)
7254   `(nth 1 ,data))
7255
7256 (defmacro gnus-data-set-mark (data mark)
7257   `(setcar (nthcdr 1 ,data) ,mark))
7258
7259 (defmacro gnus-data-pos (data)
7260   `(nth 2 ,data))
7261
7262 (defmacro gnus-data-set-pos (data pos)
7263   `(setcar (nthcdr 2 ,data) ,pos))
7264
7265 (defmacro gnus-data-header (data)
7266   `(nth 3 ,data))
7267
7268 (defmacro gnus-data-level (data)
7269   `(nth 4 ,data))
7270
7271 (defmacro gnus-data-unread-p (data)
7272   `(= (nth 1 ,data) gnus-unread-mark))
7273
7274 (defmacro gnus-data-pseudo-p (data)
7275   `(consp (nth 3 ,data)))
7276
7277 (defmacro gnus-data-find (number)
7278   `(assq ,number gnus-newsgroup-data))
7279
7280 (defmacro gnus-data-find-list (number &optional data)
7281   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7282      (memq (assq ,number bdata)
7283            bdata)))
7284
7285 (defmacro gnus-data-make (number mark pos header level)
7286   `(list ,number ,mark ,pos ,header ,level))
7287
7288 (defun gnus-data-enter (after-article number mark pos header level offset)
7289   (let ((data (gnus-data-find-list after-article)))
7290     (or data (error "No such article: %d" after-article))
7291     (setcdr data (cons (gnus-data-make number mark pos header level)
7292                        (cdr data)))
7293     (setq gnus-newsgroup-data-reverse nil)
7294     (gnus-data-update-list (cddr data) offset)))
7295
7296 (defun gnus-data-enter-list (after-article list &optional offset)
7297   (when list
7298     (let ((data (and after-article (gnus-data-find-list after-article)))
7299           (ilist list))
7300       (or data (not after-article) (error "No such article: %d" after-article))
7301       ;; Find the last element in the list to be spliced into the main
7302       ;; list.
7303       (while (cdr list)
7304         (setq list (cdr list)))
7305       (if (not data)
7306           (progn
7307             (setcdr list gnus-newsgroup-data)
7308             (setq gnus-newsgroup-data ilist)
7309             (and offset (gnus-data-update-list (cdr list) offset)))
7310         (setcdr list (cdr data))
7311         (setcdr data ilist)
7312         (and offset (gnus-data-update-list (cdr data) offset)))
7313       (setq gnus-newsgroup-data-reverse nil))))
7314
7315 (defun gnus-data-remove (article &optional offset)
7316   (let ((data gnus-newsgroup-data))
7317     (if (= (gnus-data-number (car data)) article)
7318         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7319               gnus-newsgroup-data-reverse nil)
7320       (while (cdr data)
7321         (and (= (gnus-data-number (cadr data)) article)
7322              (progn
7323                (setcdr data (cddr data))
7324                (and offset (gnus-data-update-list (cdr data) offset))
7325                (setq data nil
7326                      gnus-newsgroup-data-reverse nil)))
7327         (setq data (cdr data))))))
7328
7329 (defmacro gnus-data-list (backward)
7330   `(if ,backward
7331        (or gnus-newsgroup-data-reverse
7332            (setq gnus-newsgroup-data-reverse
7333                  (reverse gnus-newsgroup-data)))
7334      gnus-newsgroup-data))
7335
7336 (defun gnus-data-update-list (data offset)
7337   "Add OFFSET to the POS of all data entries in DATA."
7338   (while data
7339     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7340     (setq data (cdr data))))
7341
7342 (defun gnus-data-compute-positions ()
7343   "Compute the positions of all articles."
7344   (let ((data gnus-newsgroup-data)
7345         pos)
7346     (while data
7347       (when (setq pos (text-property-any
7348                        (point-min) (point-max)
7349                        'gnus-number (gnus-data-number (car data))))
7350         (gnus-data-set-pos (car data) (+ pos 3)))
7351       (setq data (cdr data)))))
7352
7353 (defun gnus-summary-article-pseudo-p (article)
7354   "Say whether this article is a pseudo article or not."
7355   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7356
7357 (defun gnus-article-parent-p (number)
7358   "Say whether this article is a parent or not."
7359   (let ((data (gnus-data-find-list number)))
7360     (and (cdr data)                     ; There has to be an article after...
7361          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7362             (gnus-data-level (nth 1 data))))))
7363
7364 (defun gnus-article-children (number)
7365   "Return a list of all children to NUMBER."
7366   (let* ((data (gnus-data-find-list number))
7367          (level (gnus-data-level (car data)))
7368          children)
7369     (setq data (cdr data))
7370     (while (and data            
7371                 (= (gnus-data-level (car data)) (1+ level)))
7372       (push (gnus-data-number (car data)) children)
7373       (setq data (cdr data)))
7374     children))
7375
7376 (defmacro gnus-summary-skip-intangible ()
7377   "If the current article is intangible, then jump to a different article."
7378   '(let ((to (get-text-property (point) 'gnus-intangible)))
7379     (and to (gnus-summary-goto-subject to))))
7380
7381 (defmacro gnus-summary-article-intangible-p ()
7382   "Say whether this article is intangible or not."
7383   '(get-text-property (point) 'gnus-intangible))
7384
7385 ;; Some summary mode macros.
7386
7387 (defmacro gnus-summary-article-number ()
7388   "The article number of the article on the current line.
7389 If there isn's an article number here, then we return the current
7390 article number."
7391   '(progn
7392      (gnus-summary-skip-intangible)
7393      (or (get-text-property (point) 'gnus-number)
7394          (gnus-summary-last-subject))))
7395
7396 (defmacro gnus-summary-article-header (&optional number)
7397   `(gnus-data-header (gnus-data-find
7398                       ,(or number '(gnus-summary-article-number)))))
7399
7400 (defmacro gnus-summary-thread-level (&optional number)
7401   `(if (and (eq gnus-summary-make-false-root 'dummy)
7402             (get-text-property (point) 'gnus-intangible))
7403        0
7404      (gnus-data-level (gnus-data-find
7405                        ,(or number '(gnus-summary-article-number))))))
7406
7407 (defmacro gnus-summary-article-mark (&optional number)
7408   `(gnus-data-mark (gnus-data-find
7409                     ,(or number '(gnus-summary-article-number)))))
7410
7411 (defmacro gnus-summary-article-pos (&optional number)
7412   `(gnus-data-pos (gnus-data-find
7413                    ,(or number '(gnus-summary-article-number)))))
7414
7415 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7416 (defmacro gnus-summary-article-subject (&optional number)
7417   "Return current subject string or nil if nothing."
7418   `(let ((headers
7419           ,(if number
7420                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7421              '(gnus-data-header (assq (gnus-summary-article-number)
7422                                       gnus-newsgroup-data)))))
7423      (and headers
7424           (vectorp headers)
7425           (mail-header-subject headers))))
7426
7427 (defmacro gnus-summary-article-score (&optional number)
7428   "Return current article score."
7429   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7430                   gnus-newsgroup-scored))
7431        gnus-summary-default-score 0))
7432
7433 (defun gnus-summary-article-children (&optional number)
7434   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7435          (level (gnus-data-level (car data)))
7436          l children)
7437     (while (and (setq data (cdr data))
7438                 (> (setq l (gnus-data-level (car data))) level))
7439       (and (= (1+ level) l)
7440            (setq children (cons (gnus-data-number (car data))
7441                                 children))))
7442     (nreverse children)))
7443
7444 (defun gnus-summary-article-parent (&optional number)
7445   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7446                                     (gnus-data-list t)))
7447          (level (gnus-data-level (car data))))
7448     (if (zerop level)
7449         () ; This is a root.
7450       ;; We search until we find an article with a level less than
7451       ;; this one.  That function has to be the parent.
7452       (while (and (setq data (cdr data))
7453                   (not (< (gnus-data-level (car data)) level))))
7454       (and data (gnus-data-number (car data))))))
7455
7456 (defun gnus-unread-mark-p (mark)
7457   "Say whether MARK is the unread mark."
7458   (= mark gnus-unread-mark))
7459
7460 (defun gnus-read-mark-p (mark)
7461   "Say whether MARK is one of the marks that mark as read.
7462 This is all marks except unread, ticked, dormant, and expirable."
7463   (not (or (= mark gnus-unread-mark)
7464            (= mark gnus-ticked-mark)
7465            (= mark gnus-dormant-mark)
7466            (= mark gnus-expirable-mark))))
7467
7468 ;; Saving hidden threads.
7469
7470 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
7471 (put 'gnus-save-hidden-threads 'lisp-indent-hook 0)
7472 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
7473
7474 (defmacro gnus-save-hidden-threads (&rest forms)
7475   "Save hidden threads, eval FORMS, and restore the hidden threads."
7476   (let ((config (make-symbol "config")))
7477     `(let ((,config (gnus-hidden-threads-configuration)))
7478        (unwind-protect
7479            (progn
7480              ,@forms)
7481          (gnus-restore-hidden-threads-configuration ,config)))))
7482
7483 (defun gnus-hidden-threads-configuration ()
7484   "Return the current hidden threads configuration."
7485   (save-excursion
7486     (let (config)
7487       (goto-char (point-min))
7488       (while (search-forward "\r" nil t)
7489         (push (1- (point)) config))
7490       config)))
7491
7492 (defun gnus-restore-hidden-threads-configuration (config)
7493   "Restore hidden threads configuration from CONFIG."
7494   (let (point buffer-read-only)
7495     (while (setq point (pop config))
7496       (when (and (< point (point-max))
7497                  (goto-char point)
7498                  (= (following-char) ?\n))
7499         (subst-char-in-region point (1+ point) ?\n ?\r)))))
7500
7501 ;; Various summary mode internalish functions.
7502
7503 (defun gnus-mouse-pick-article (e)
7504   (interactive "e")
7505   (mouse-set-point e)
7506   (gnus-summary-next-page nil t))
7507
7508 (defun gnus-summary-setup-buffer (group)
7509   "Initialize summary buffer."
7510   (let ((buffer (concat "*Summary " group "*")))
7511     (if (get-buffer buffer)
7512         (progn
7513           (set-buffer buffer)
7514           (setq gnus-summary-buffer (current-buffer))
7515           (not gnus-newsgroup-prepared))
7516       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7517       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7518       (gnus-add-current-to-buffer-list)
7519       (gnus-summary-mode group)
7520       (when gnus-carpal
7521         (gnus-carpal-setup-buffer 'summary))
7522       (unless gnus-single-article-buffer
7523         (make-local-variable 'gnus-article-buffer)
7524         (make-local-variable 'gnus-article-current)
7525         (make-local-variable 'gnus-original-article-buffer))
7526       (setq gnus-newsgroup-name group)
7527       t)))
7528
7529 (defun gnus-set-global-variables ()
7530   ;; Set the global equivalents of the summary buffer-local variables
7531   ;; to the latest values they had.  These reflect the summary buffer
7532   ;; that was in action when the last article was fetched.
7533   (when (eq major-mode 'gnus-summary-mode)
7534     (setq gnus-summary-buffer (current-buffer))
7535     (let ((name gnus-newsgroup-name)
7536           (marked gnus-newsgroup-marked)
7537           (unread gnus-newsgroup-unreads)
7538           (headers gnus-current-headers)
7539           (data gnus-newsgroup-data)
7540           (summary gnus-summary-buffer)
7541           (article-buffer gnus-article-buffer)
7542           (original gnus-original-article-buffer)
7543           (gac gnus-article-current)
7544           (score-file gnus-current-score-file))
7545       (save-excursion
7546         (set-buffer gnus-group-buffer)
7547         (setq gnus-newsgroup-name name)
7548         (setq gnus-newsgroup-marked marked)
7549         (setq gnus-newsgroup-unreads unread)
7550         (setq gnus-current-headers headers)
7551         (setq gnus-newsgroup-data data)
7552         (setq gnus-article-current gac)
7553         (setq gnus-summary-buffer summary)
7554         (setq gnus-article-buffer article-buffer)
7555         (setq gnus-original-article-buffer original)
7556         (setq gnus-current-score-file score-file)))))
7557
7558 (defun gnus-summary-last-article-p (&optional article)
7559   "Return whether ARTICLE is the last article in the buffer."
7560   (if (not (setq article (or article (gnus-summary-article-number))))
7561       t ; All non-existant numbers are the last article. :-)
7562     (not (cdr (gnus-data-find-list article)))))
7563
7564 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7565   "Insert a dummy root in the summary buffer."
7566   (beginning-of-line)
7567   (gnus-add-text-properties
7568    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7569    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7570
7571 (defun gnus-make-thread-indent-array ()
7572   (let ((n 200))
7573     (unless (and gnus-thread-indent-array
7574                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
7575       (setq gnus-thread-indent-array (make-vector 201 "")
7576             gnus-thread-indent-array-level gnus-thread-indent-level)
7577       (while (>= n 0)
7578         (aset gnus-thread-indent-array n
7579               (make-string (* n gnus-thread-indent-level) ? ))
7580         (setq n (1- n))))))
7581
7582 (defun gnus-summary-insert-line
7583   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7584                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7585                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7586   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7587          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7588          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7589          (gnus-tmp-score-char
7590           (if (or (null gnus-summary-default-score)
7591                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7592                       gnus-summary-zcore-fuzz)) ? 
7593             (if (< gnus-tmp-score gnus-summary-default-score)
7594                 gnus-score-below-mark gnus-score-over-mark)))
7595          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7596                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7597                                   gnus-cached-mark)
7598                                  (gnus-tmp-replied gnus-replied-mark)
7599                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7600                                   gnus-saved-mark)
7601                                  (t gnus-unread-mark)))
7602          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7603          (gnus-tmp-name
7604           (cond
7605            ((string-match "(.+)" gnus-tmp-from)
7606             (substring gnus-tmp-from
7607                        (1+ (match-beginning 0)) (1- (match-end 0))))
7608            ((string-match "<[^>]+> *$" gnus-tmp-from)
7609             (let ((beg (match-beginning 0)))
7610               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7611                        (substring gnus-tmp-from (1+ (match-beginning 0))
7612                                   (1- (match-end 0))))
7613                   (substring gnus-tmp-from 0 beg))))
7614            (t gnus-tmp-from)))
7615          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7616          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7617          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7618          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7619          (buffer-read-only nil))
7620     (when (string= gnus-tmp-name "")
7621       (setq gnus-tmp-name gnus-tmp-from))
7622     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7623     (gnus-put-text-property
7624      (point)
7625      (progn (eval gnus-summary-line-format-spec) (point))
7626      'gnus-number gnus-tmp-number)
7627     (when (gnus-visual-p 'summary-highlight 'highlight)
7628       (forward-line -1)
7629       (run-hooks 'gnus-summary-update-hook)
7630       (forward-line 1))))
7631
7632 (defun gnus-summary-update-line (&optional dont-update)
7633   ;; Update summary line after change.
7634   (when (and gnus-summary-default-score
7635              (not gnus-summary-inhibit-highlight))
7636     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7637            (article (gnus-summary-article-number))
7638            (score (gnus-summary-article-score article)))
7639       (unless dont-update
7640         (if (and gnus-summary-mark-below
7641                  (< (gnus-summary-article-score)
7642                     gnus-summary-mark-below))
7643             ;; This article has a low score, so we mark it as read.
7644             (when (memq article gnus-newsgroup-unreads)
7645               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7646           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7647             ;; This article was previously marked as read on account
7648             ;; of a low score, but now it has risen, so we mark it as
7649             ;; unread.
7650             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7651         (gnus-summary-update-mark
7652          (if (or (null gnus-summary-default-score)
7653                  (<= (abs (- score gnus-summary-default-score))
7654                      gnus-summary-zcore-fuzz)) ? 
7655            (if (< score gnus-summary-default-score)
7656                gnus-score-below-mark gnus-score-over-mark)) 'score))
7657       ;; Do visual highlighting.
7658       (when (gnus-visual-p 'summary-highlight 'highlight)
7659         (run-hooks 'gnus-summary-update-hook)))))
7660
7661 (defvar gnus-tmp-new-adopts nil)
7662
7663 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7664   ;; Sum up all elements (and sub-elements) in a list.
7665   (let* ((number
7666           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7667           (cond
7668            ((and (consp thread) (cdr thread))
7669             (apply
7670              '+ 1 (mapcar
7671                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7672            ((null thread)
7673             1)
7674            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7675             1)
7676            (t 0))))
7677     (when (and level (zerop level) gnus-tmp-new-adopts)
7678       (incf number
7679             (apply '+ (mapcar
7680                        'gnus-summary-number-of-articles-in-thread
7681                        gnus-tmp-new-adopts))))
7682     (if char
7683         (if (> number 1) gnus-not-empty-thread-mark
7684           gnus-empty-thread-mark)
7685       number)))
7686
7687 (defun gnus-summary-set-local-parameters (group)
7688  "Go through the local params of GROUP and set all variable specs in that list."
7689   (let ((params (gnus-info-params (gnus-get-info group)))
7690         elem)
7691     (while params
7692       (setq elem (car params)
7693             params (cdr params))
7694       (and (consp elem)                 ; Has to be a cons.
7695            (consp (cdr elem))           ; The cdr has to be a list.
7696            (symbolp (car elem))         ; Has to be a symbol in there.
7697            (not (memq (car elem) 
7698                       '(quit-config to-address to-list to-group)))
7699            (progn                       ; So we set it.
7700              (make-local-variable (car elem))
7701              (set (car elem) (eval (nth 1 elem))))))))
7702
7703 (defun gnus-summary-read-group (group &optional show-all no-article
7704                                       kill-buffer no-display)
7705   "Start reading news in newsgroup GROUP.
7706 If SHOW-ALL is non-nil, already read articles are also listed.
7707 If NO-ARTICLE is non-nil, no article is selected initially.
7708 If NO-DISPLAY, don't generate a summary buffer."
7709   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7710   (let* ((new-group (gnus-summary-setup-buffer group))
7711          (quit-config (gnus-group-quit-config group))
7712          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7713     (cond
7714      ;; This summary buffer exists already, so we just select it.
7715      ((not new-group)
7716       (gnus-set-global-variables)
7717       (when kill-buffer
7718         (gnus-kill-or-deaden-summary kill-buffer))
7719       (gnus-configure-windows 'summary 'force)
7720       (gnus-set-mode-line 'summary)
7721       (gnus-summary-position-point)
7722       (message "")
7723       t)
7724      ;; We couldn't select this group.
7725      ((null did-select)
7726       (when (and (eq major-mode 'gnus-summary-mode)
7727                  (not (equal (current-buffer) kill-buffer)))
7728         (kill-buffer (current-buffer))
7729         (if (not quit-config)
7730             (progn
7731               (set-buffer gnus-group-buffer)
7732               (gnus-group-jump-to-group group)
7733               (gnus-group-next-unread-group 1))
7734           (if (not (buffer-name (car quit-config)))
7735               (gnus-configure-windows 'group 'force)
7736             (set-buffer (car quit-config))
7737             (and (eq major-mode 'gnus-summary-mode)
7738                  (gnus-set-global-variables))
7739             (gnus-configure-windows (cdr quit-config)))))
7740       (gnus-message 3 "Can't select group")
7741       nil)
7742      ;; The user did a `C-g' while prompting for number of articles,
7743      ;; so we exit this group.
7744      ((eq did-select 'quit)
7745       (and (eq major-mode 'gnus-summary-mode)
7746            (not (equal (current-buffer) kill-buffer))
7747            (kill-buffer (current-buffer)))
7748       (when kill-buffer
7749         (gnus-kill-or-deaden-summary kill-buffer))
7750       (if (not quit-config)
7751           (progn
7752             (set-buffer gnus-group-buffer)
7753             (gnus-group-jump-to-group group)
7754             (gnus-group-next-unread-group 1)
7755             (gnus-configure-windows 'group 'force))
7756         (if (not (buffer-name (car quit-config)))
7757             (gnus-configure-windows 'group 'force)
7758           (set-buffer (car quit-config))
7759           (and (eq major-mode 'gnus-summary-mode)
7760                (gnus-set-global-variables))
7761           (gnus-configure-windows (cdr quit-config))))
7762       ;; Finally signal the quit.
7763       (signal 'quit nil))
7764      ;; The group was successfully selected.
7765      (t
7766       (gnus-set-global-variables)
7767       ;; Save the active value in effect when the group was entered.
7768       (setq gnus-newsgroup-active
7769             (gnus-copy-sequence
7770              (gnus-active gnus-newsgroup-name)))
7771       ;; You can change the summary buffer in some way with this hook.
7772       (run-hooks 'gnus-select-group-hook)
7773       ;; Set any local variables in the group parameters.
7774       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7775       (gnus-update-format-specifications)
7776       ;; Do score processing.
7777       (when gnus-use-scoring
7778         (gnus-possibly-score-headers))
7779       ;; Check whether to fill in the gaps in the threads.
7780       (when gnus-build-sparse-threads
7781         (gnus-build-sparse-threads))
7782       ;; Find the initial limit.
7783       (if gnus-show-threads
7784           (if show-all
7785               (let ((gnus-newsgroup-dormant nil))
7786                 (gnus-summary-initial-limit show-all))
7787             (gnus-summary-initial-limit show-all))
7788         (setq gnus-newsgroup-limit 
7789               (mapcar 
7790                (lambda (header) (mail-header-number header))
7791                gnus-newsgroup-headers)))
7792       ;; Generate the summary buffer.
7793       (unless no-display
7794         (gnus-summary-prepare))
7795       (when gnus-use-trees
7796         (gnus-tree-open group)
7797         (setq gnus-summary-highlight-line-function
7798               'gnus-tree-highlight-article))
7799       ;; If the summary buffer is empty, but there are some low-scored
7800       ;; articles or some excluded dormants, we include these in the
7801       ;; buffer.
7802       (when (and (zerop (buffer-size))
7803                  (not no-display))
7804         (cond (gnus-newsgroup-dormant
7805                (gnus-summary-limit-include-dormant))
7806               ((and gnus-newsgroup-scored show-all)
7807                (gnus-summary-limit-include-expunged))))
7808       ;; Function `gnus-apply-kill-file' must be called in this hook.
7809       (run-hooks 'gnus-apply-kill-hook)
7810       (if (and (zerop (buffer-size))
7811                (not no-display))
7812           (progn
7813             ;; This newsgroup is empty.
7814             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7815             (gnus-message 6 "No unread news")
7816             (when kill-buffer
7817               (gnus-kill-or-deaden-summary kill-buffer))
7818             ;; Return nil from this function.
7819             nil)
7820         ;; Hide conversation thread subtrees.  We cannot do this in
7821         ;; gnus-summary-prepare-hook since kill processing may not
7822         ;; work with hidden articles.
7823         (and gnus-show-threads
7824              gnus-thread-hide-subtree
7825              (gnus-summary-hide-all-threads))
7826         ;; Show first unread article if requested.
7827         (if (and (not no-article)
7828                  (not no-display)
7829                  gnus-newsgroup-unreads
7830                  gnus-auto-select-first)
7831             (unless (if (eq gnus-auto-select-first 'best)
7832                         (gnus-summary-best-unread-article)
7833                       (gnus-summary-first-unread-article))
7834               (gnus-configure-windows 'summary))
7835           ;; Don't select any articles, just move point to the first
7836           ;; article in the group.
7837           (goto-char (point-min))
7838           (gnus-summary-position-point)
7839           (gnus-set-mode-line 'summary)
7840           (gnus-configure-windows 'summary 'force))
7841         ;; If we are in async mode, we send some info to the backend.
7842         (when gnus-newsgroup-async
7843           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7844         (when kill-buffer
7845           (gnus-kill-or-deaden-summary kill-buffer))
7846         (when (get-buffer-window gnus-group-buffer t)
7847           ;; Gotta use windows, because recenter does wierd stuff if
7848           ;; the current buffer ain't the displayed window.
7849           (let ((owin (selected-window)))
7850             (select-window (get-buffer-window gnus-group-buffer t))
7851             (when (gnus-group-goto-group group)
7852               (recenter))
7853             (select-window owin))))
7854       ;; Mark this buffer as "prepared".
7855       (setq gnus-newsgroup-prepared t)
7856       t))))
7857
7858 (defun gnus-summary-prepare ()
7859   "Generate the summary buffer."
7860   (let ((buffer-read-only nil))
7861     (erase-buffer)
7862     (setq gnus-newsgroup-data nil
7863           gnus-newsgroup-data-reverse nil)
7864     (run-hooks 'gnus-summary-generate-hook)
7865     ;; Generate the buffer, either with threads or without.
7866     (when gnus-newsgroup-headers
7867       (gnus-summary-prepare-threads
7868        (if gnus-show-threads
7869            (gnus-sort-gathered-threads
7870             (funcall gnus-summary-thread-gathering-function
7871                      (gnus-sort-threads
7872                       (gnus-cut-threads (gnus-make-threads)))))
7873          ;; Unthreaded display.
7874          (gnus-sort-articles gnus-newsgroup-headers))))
7875     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7876     ;; Call hooks for modifying summary buffer.
7877     (goto-char (point-min))
7878     (run-hooks 'gnus-summary-prepare-hook)))
7879
7880 (defun gnus-gather-threads-by-subject (threads)
7881   "Gather threads by looking at Subject headers."
7882   (if (not gnus-summary-make-false-root)
7883       threads
7884     (let ((hashtb (gnus-make-hashtable 1023))
7885           (prev threads)
7886           (result threads)
7887           subject hthread whole-subject)
7888       (while threads
7889         (setq whole-subject (mail-header-subject (caar threads)))
7890         (setq subject
7891               (cond
7892                ;; Truncate the subject.
7893                ((numberp gnus-summary-gather-subject-limit)
7894                 (setq subject (gnus-simplify-subject-re whole-subject))
7895                 (if (> (length subject) gnus-summary-gather-subject-limit)
7896                     (substring subject 0 gnus-summary-gather-subject-limit)
7897                   subject))
7898                ;; Fuzzily simplify it.
7899                ((eq 'fuzzy gnus-summary-gather-subject-limit)
7900                 (gnus-simplify-subject-fuzzy whole-subject))
7901                ;; Just remove the leading "Re:".
7902                (t
7903                 (gnus-simplify-subject-re whole-subject))))
7904
7905         (if (and gnus-summary-gather-exclude-subject
7906                  (string-match gnus-summary-gather-exclude-subject
7907                                subject))
7908             ()          ; We don't want to do anything with this article.
7909           ;; We simplify the subject before looking it up in the
7910           ;; hash table.
7911
7912           (if (setq hthread (gnus-gethash subject hashtb))
7913               (progn
7914                 ;; We enter a dummy root into the thread, if we
7915                 ;; haven't done that already.
7916                 (unless (stringp (caar hthread))
7917                   (setcar hthread (list whole-subject (car hthread))))
7918                 ;; We add this new gathered thread to this gathered
7919                 ;; thread.
7920                 (setcdr (car hthread)
7921                         (nconc (cdar hthread) (list (car threads))))
7922                 ;; Remove it from the list of threads.
7923                 (setcdr prev (cdr threads))
7924                 (setq threads prev))
7925             ;; Enter this thread into the hash table.
7926             (gnus-sethash subject threads hashtb)))
7927         (setq prev threads)
7928         (setq threads (cdr threads)))
7929       result)))
7930
7931 (defun gnus-gather-threads-by-references (threads)
7932   "Gather threads by looking at References headers."
7933   (let ((idhashtb (gnus-make-hashtable 1023))
7934         (thhashtb (gnus-make-hashtable 1023))
7935         (prev threads)
7936         (result threads)
7937         ids references id gthread gid entered)
7938     (while threads
7939       (when (setq references (mail-header-references (caar threads)))
7940         (setq id (mail-header-id (caar threads)))
7941         (setq ids (gnus-split-references references))
7942         (setq entered nil)
7943         (while ids
7944           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7945               (progn
7946                 (gnus-sethash (car ids) id idhashtb)
7947                 (gnus-sethash id threads thhashtb))
7948             (setq gthread (gnus-gethash gid thhashtb))
7949             (unless entered
7950               ;; We enter a dummy root into the thread, if we
7951               ;; haven't done that already.
7952               (unless (stringp (caar gthread))
7953                 (setcar gthread (list (mail-header-subject (caar gthread))
7954                                       (car gthread))))
7955               ;; We add this new gathered thread to this gathered
7956               ;; thread.
7957               (setcdr (car gthread)
7958                       (nconc (cdar gthread) (list (car threads)))))
7959             ;; Add it into the thread hash table.
7960             (gnus-sethash id gthread thhashtb)
7961             (setq entered t)
7962             ;; Remove it from the list of threads.
7963             (setcdr prev (cdr threads))
7964             (setq threads prev))
7965           (setq ids (cdr ids))))
7966       (setq prev threads)
7967       (setq threads (cdr threads)))
7968     result))
7969
7970 (defun gnus-sort-gathered-threads (threads)
7971   "Sort subtreads inside each gathered thread by article number."
7972   (let ((result threads))
7973     (while threads
7974       (when (stringp (caar threads))
7975         (setcdr (car threads)
7976                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
7977       (setq threads (cdr threads)))
7978     result))
7979
7980 (defun gnus-make-threads ()
7981   "Go through the dependency hashtb and find the roots.  Return all threads."
7982   (let (threads)
7983     (mapatoms
7984      (lambda (refs)
7985        (unless (car (symbol-value refs))
7986          ;; These threads do not refer back to any other articles,
7987          ;; so they're roots.
7988          (setq threads (append (cdr (symbol-value refs)) threads))))
7989      gnus-newsgroup-dependencies)
7990     threads))
7991
7992 (defun gnus-build-sparse-threads ()
7993   (let ((headers gnus-newsgroup-headers)
7994         (deps gnus-newsgroup-dependencies)
7995         header references generation relations 
7996         cthread subject child end pthread relation)
7997     ;; First we create an alist of generations/relations, where 
7998     ;; generations is how much we trust the ralation, and the relation
7999     ;; is parent/child.
8000     (gnus-message 7 "Making sparse threads...")
8001     (save-excursion
8002       (nnheader-set-temp-buffer " *gnus sparse threads*")
8003       (while (setq header (pop headers))
8004         (when (and (setq references (mail-header-references header))
8005                    (not (string= references "")))
8006           (insert references)
8007           (setq child (mail-header-id header)
8008                 subject (mail-header-subject header))
8009           (setq generation 0)
8010           (while (search-backward ">" nil t)
8011             (setq end (1+ (point)))
8012             (when (search-backward "<" nil t)
8013               (push (list (incf generation) 
8014                           child (setq child (buffer-substring (point) end))
8015                           subject)
8016                     relations)))
8017           (push (list (1+ generation) child nil subject) relations)
8018           (erase-buffer)))
8019       (kill-buffer (current-buffer)))
8020     ;; Sort over trustworthiness.
8021     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
8022     (while (setq relation (pop relations))
8023       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
8024                 (unless (car (symbol-value cthread))
8025                   ;; Make this article the parent of these threads.
8026                   (setcar (symbol-value cthread)
8027                           (vector gnus-reffed-article-number 
8028                                   (cadddr relation) 
8029                                   "" ""
8030                                   (cadr relation) 
8031                                   (or (caddr relation) "") 0 0 "")))
8032               (set cthread (list (vector gnus-reffed-article-number
8033                                          (cadddr relation) 
8034                                          "" "" (cadr relation) 
8035                                          (or (caddr relation) "") 0 0 ""))))
8036         (push gnus-reffed-article-number gnus-newsgroup-limit)
8037         (push gnus-reffed-article-number gnus-newsgroup-sparse)
8038         (push (cons gnus-reffed-article-number gnus-sparse-mark)
8039               gnus-newsgroup-reads)
8040         (decf gnus-reffed-article-number)
8041         ;; Make this new thread the child of its parent.
8042         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
8043             (setcdr (symbol-value pthread)
8044                     (nconc (cdr (symbol-value pthread))
8045                            (list (symbol-value cthread))))
8046           (set pthread (list nil (symbol-value cthread))))))
8047     (gnus-message 7 "Making sparse threads...done")))
8048
8049 (defun gnus-build-old-threads ()
8050   ;; Look at all the articles that refer back to old articles, and
8051   ;; fetch the headers for the articles that aren't there.  This will
8052   ;; build complete threads - if the roots haven't been expired by the
8053   ;; server, that is.
8054   (let (id heads)
8055     (mapatoms
8056      (lambda (refs)
8057        (when (not (car (symbol-value refs)))
8058          (setq heads (cdr (symbol-value refs)))
8059          (while heads
8060            (if (memq (mail-header-number (caar heads))
8061                      gnus-newsgroup-dormant)
8062                (setq heads (cdr heads))
8063              (setq id (symbol-name refs))
8064              (while (and (setq id (gnus-build-get-header id))
8065                          (not (car (gnus-gethash
8066                                     id gnus-newsgroup-dependencies)))))
8067              (setq heads nil)))))
8068      gnus-newsgroup-dependencies)))
8069
8070 (defun gnus-build-get-header (id)
8071   ;; Look through the buffer of NOV lines and find the header to
8072   ;; ID.  Enter this line into the dependencies hash table, and return
8073   ;; the id of the parent article (if any).
8074   (let ((deps gnus-newsgroup-dependencies)
8075         found header)
8076     (prog1
8077         (save-excursion
8078           (set-buffer nntp-server-buffer)
8079           (goto-char (point-min))
8080           (while (and (not found) (search-forward id nil t))
8081             (beginning-of-line)
8082             (setq found (looking-at
8083                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
8084                                  (regexp-quote id))))
8085             (or found (beginning-of-line 2)))
8086           (when found
8087             (beginning-of-line)
8088             (and
8089              (setq header (gnus-nov-parse-line
8090                            (read (current-buffer)) deps))
8091              (gnus-parent-id (mail-header-references header)))))
8092       (when header
8093         (let ((number (mail-header-number header)))
8094           (push number gnus-newsgroup-limit)
8095           (push header gnus-newsgroup-headers)
8096           (if (memq number gnus-newsgroup-unselected)
8097               (progn
8098                 (push number gnus-newsgroup-unreads)
8099                 (setq gnus-newsgroup-unselected
8100                       (delq number gnus-newsgroup-unselected)))
8101             (push number gnus-newsgroup-ancient)))))))
8102
8103 (defun gnus-summary-update-article (article &optional iheader)
8104   "Update ARTICLE in the summary buffer."
8105   (set-buffer gnus-summary-buffer)
8106   (let* ((header (or iheader (gnus-summary-article-header article)))
8107          (id (mail-header-id header))
8108          (data (gnus-data-find article))
8109          (thread (gnus-id-to-thread id))
8110          (references (mail-header-references header))
8111          (parent
8112           (gnus-id-to-thread
8113            (or (gnus-parent-id 
8114                 (if (and references
8115                          (not (equal "" references)))
8116                     references))
8117                "none")))
8118          (buffer-read-only nil)
8119          (old (car thread))
8120          (number (mail-header-number header))
8121          pos)
8122     (when thread
8123       ;; !!! Should this be in or not?
8124       (unless iheader
8125         (setcar thread nil))
8126       (when parent
8127         (delq thread parent))
8128       (if (gnus-summary-insert-subject id header iheader)
8129           ;; Set the (possibly) new article number in the data structure.
8130           (gnus-data-set-number data (gnus-id-to-article id))
8131         (setcar thread old)
8132         nil))))
8133
8134 (defun gnus-rebuild-thread (id)
8135   "Rebuild the thread containing ID."
8136   (let ((buffer-read-only nil)
8137         current thread data)
8138     (if (not gnus-show-threads)
8139         (setq thread (list (car (gnus-id-to-thread id))))
8140       ;; Get the thread this article is part of.
8141       (setq thread (gnus-remove-thread id)))
8142     (setq current (save-excursion
8143                     (and (zerop (forward-line -1))
8144                          (gnus-summary-article-number))))
8145     ;; If this is a gathered thread, we have to go some re-gathering.
8146     (when (stringp (car thread))
8147       (let ((subject (car thread))
8148             roots thr)
8149         (setq thread (cdr thread))
8150         (while thread
8151           (unless (memq (setq thr (gnus-id-to-thread
8152                                       (gnus-root-id
8153                                        (mail-header-id (caar thread)))))
8154                         roots)
8155             (push thr roots))
8156           (setq thread (cdr thread)))
8157         ;; We now have all (unique) roots.
8158         (if (= (length roots) 1)
8159             ;; All the loose roots are now one solid root.
8160             (setq thread (car roots))
8161           (setq thread (cons subject (gnus-sort-threads roots))))))
8162     (let (threads)
8163       ;; We then insert this thread into the summary buffer.
8164       (let (gnus-newsgroup-data gnus-newsgroup-threads)
8165         (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
8166         (setq data (nreverse gnus-newsgroup-data))
8167         (setq threads gnus-newsgroup-threads))
8168       ;; We splice the new data into the data structure.
8169       (gnus-data-enter-list current data)
8170       (gnus-data-compute-positions)
8171       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
8172
8173 (defun gnus-number-to-header (number)
8174   "Return the header for article NUMBER."
8175   (let ((headers gnus-newsgroup-headers))
8176     (while (and headers
8177                 (not (= number (mail-header-number (car headers)))))
8178       (pop headers))
8179     (when headers
8180       (car headers))))
8181
8182 (defun gnus-id-to-thread (id)
8183   "Return the (sub-)thread where ID appears."
8184   (gnus-gethash id gnus-newsgroup-dependencies))
8185
8186 (defun gnus-id-to-article (id)
8187   "Return the article number of ID."
8188   (let ((thread (gnus-id-to-thread id)))
8189     (when (and thread
8190                (car thread))
8191       (mail-header-number (car thread)))))
8192
8193 (defun gnus-id-to-header (id)
8194   "Return the article headers of ID."
8195   (car (gnus-id-to-thread id)))
8196
8197 (defun gnus-article-displayed-root-p (article)
8198   "Say whether ARTICLE is a root(ish) article."
8199   (let ((level (gnus-summary-thread-level article))
8200         (refs (mail-header-references  (gnus-summary-article-header article)))
8201         particle)
8202     (cond 
8203      ((null level) nil)
8204      ((zerop level) t)
8205      ((null refs) t)
8206      ((null (gnus-parent-id refs)) t)
8207      ((and (= 1 level)
8208            (null (setq particle (gnus-id-to-article
8209                                  (gnus-parent-id refs))))
8210            (null (gnus-summary-thread-level particle)))))))
8211
8212 (defun gnus-root-id (id)
8213   "Return the id of the root of the thread where ID appears."
8214   (let (last-id prev)
8215     (while (and id (setq prev (car (gnus-gethash 
8216                                     id gnus-newsgroup-dependencies))))
8217       (setq last-id id
8218             id (gnus-parent-id (mail-header-references prev))))
8219     last-id))
8220
8221 (defun gnus-remove-thread (id &optional dont-remove)
8222   "Remove the thread that has ID in it."
8223   (let ((dep gnus-newsgroup-dependencies)
8224         headers thread last-id)
8225     ;; First go up in this thread until we find the root.
8226     (setq last-id (gnus-root-id id))
8227     (setq headers (list (car (gnus-id-to-thread last-id))
8228                         (caadr (gnus-id-to-thread last-id))))
8229     ;; We have now found the real root of this thread.  It might have
8230     ;; been gathered into some loose thread, so we have to search
8231     ;; through the threads to find the thread we wanted.
8232     (let ((threads gnus-newsgroup-threads)
8233           sub)
8234       (while threads
8235         (setq sub (car threads))
8236         (if (stringp (car sub))
8237             ;; This is a gathered threads, so we look at the roots
8238             ;; below it to find whether this article in in this
8239             ;; gathered root.
8240             (progn
8241               (setq sub (cdr sub))
8242               (while sub
8243                 (when (member (caar sub) headers)
8244                   (setq thread (car threads)
8245                         threads nil
8246                         sub nil))
8247                 (setq sub (cdr sub))))
8248           ;; It's an ordinary thread, so we check it.
8249           (when (eq (car sub) (car headers))
8250             (setq thread sub
8251                   threads nil)))
8252         (setq threads (cdr threads)))
8253       ;; If this article is in no thread, then it's a root.
8254       (if thread
8255           (unless dont-remove
8256             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
8257         (setq thread (gnus-gethash last-id dep)))
8258       (when thread
8259         (prog1
8260             thread ; We return this thread.
8261           (unless dont-remove
8262             (if (stringp (car thread))
8263                 (progn
8264                   ;; If we use dummy roots, then we have to remove the
8265                   ;; dummy root as well.
8266                   (when (eq gnus-summary-make-false-root 'dummy)
8267                     ;; Uhm.
8268                     )
8269                   (setq thread (cdr thread))
8270                   (while thread
8271                     (gnus-remove-thread-1 (car thread))
8272                     (setq thread (cdr thread))))
8273               (gnus-remove-thread-1 thread))))))))
8274
8275 (defun gnus-remove-thread-1 (thread)
8276   "Remove the thread THREAD recursively."
8277   (let ((number (mail-header-number (car thread)))
8278         pos)
8279     (when (setq pos (text-property-any
8280                      (point-min) (point-max) 'gnus-number number))
8281       (goto-char pos)
8282       (gnus-delete-line)
8283       (gnus-data-remove number))
8284     (setq thread (cdr thread))
8285     (while thread
8286       (gnus-remove-thread-1 (pop thread)))))
8287
8288 (defun gnus-sort-threads (threads)
8289   "Sort THREADS."
8290   (if (not gnus-thread-sort-functions)
8291       threads
8292     (let ((func (if (= 1 (length gnus-thread-sort-functions))
8293                     (car gnus-thread-sort-functions)
8294                   `(lambda (t1 t2)
8295                      ,(gnus-make-sort-function 
8296                        (reverse gnus-thread-sort-functions))))))
8297       (gnus-message 7 "Sorting threads...")
8298       (prog1
8299           (sort threads func)
8300         (gnus-message 7 "Sorting threads...done")))))
8301
8302 (defun gnus-sort-articles (articles)
8303   "Sort ARTICLES."
8304   (when gnus-article-sort-functions
8305     (let ((func (if (= 1 (length gnus-article-sort-functions))
8306                     (car gnus-article-sort-functions)
8307                   `(lambda (t1 t2)
8308                      ,(gnus-make-sort-function 
8309                        (reverse gnus-article-sort-functions))))))
8310       (gnus-message 7 "Sorting articles...")
8311       (prog1
8312           (setq gnus-newsgroup-headers (sort articles func))
8313         (gnus-message 7 "Sorting articles...done")))))
8314
8315 (defun gnus-make-sort-function (funs)
8316   "Return a composite sort condition based on the functions in FUNC."
8317   (if (cdr funs)
8318       `(or (,(car funs) t1 t2)
8319            (and (not (,(car funs) t2 t1))
8320                 ,(gnus-make-sort-function (cdr funs))))
8321     `(,(car funs) t1 t2)))
8322                  
8323 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
8324 (defmacro gnus-thread-header (thread)
8325   ;; Return header of first article in THREAD.
8326   ;; Note that THREAD must never, ever be anything else than a variable -
8327   ;; using some other form will lead to serious barfage.
8328   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8329   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8330   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8331         (vector thread) 2))
8332
8333 (defsubst gnus-article-sort-by-number (h1 h2)
8334   "Sort articles by article number."
8335   (< (mail-header-number h1)
8336      (mail-header-number h2)))
8337
8338 (defun gnus-thread-sort-by-number (h1 h2)
8339   "Sort threads by root article number."
8340   (gnus-article-sort-by-number
8341    (gnus-thread-header h1) (gnus-thread-header h2)))
8342
8343 (defsubst gnus-article-sort-by-author (h1 h2)
8344   "Sort articles by root author."
8345   (string-lessp
8346    (let ((extract (funcall
8347                    gnus-extract-address-components
8348                    (mail-header-from h1))))
8349      (or (car extract) (cdr extract)))
8350    (let ((extract (funcall
8351                    gnus-extract-address-components
8352                    (mail-header-from h2))))
8353      (or (car extract) (cdr extract)))))
8354
8355 (defun gnus-thread-sort-by-author (h1 h2)
8356   "Sort threads by root author."
8357   (gnus-article-sort-by-author
8358    (gnus-thread-header h1)  (gnus-thread-header h2)))
8359
8360 (defsubst gnus-article-sort-by-subject (h1 h2)
8361   "Sort articles by root subject."
8362   (string-lessp
8363    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8364    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8365
8366 (defun gnus-thread-sort-by-subject (h1 h2)
8367   "Sort threads by root subject."
8368   (gnus-article-sort-by-subject
8369    (gnus-thread-header h1) (gnus-thread-header h2)))
8370
8371 (defsubst gnus-article-sort-by-date (h1 h2)
8372   "Sort articles by root article date."
8373   (string-lessp
8374    (inline (gnus-sortable-date (mail-header-date h1)))
8375    (inline (gnus-sortable-date (mail-header-date h2)))))
8376
8377 (defun gnus-thread-sort-by-date (h1 h2)
8378   "Sort threads by root article date."
8379   (gnus-article-sort-by-date
8380    (gnus-thread-header h1) (gnus-thread-header h2)))
8381
8382 (defsubst gnus-article-sort-by-score (h1 h2)
8383   "Sort articles by root article score.
8384 Unscored articles will be counted as having a score of zero."
8385   (> (or (cdr (assq (mail-header-number h1)
8386                     gnus-newsgroup-scored))
8387          gnus-summary-default-score 0)
8388      (or (cdr (assq (mail-header-number h2)
8389                     gnus-newsgroup-scored))
8390          gnus-summary-default-score 0)))
8391
8392 (defun gnus-thread-sort-by-score (h1 h2)
8393   "Sort threads by root article score."
8394   (gnus-article-sort-by-score
8395    (gnus-thread-header h1) (gnus-thread-header h2)))
8396
8397 (defun gnus-thread-sort-by-total-score (h1 h2)
8398   "Sort threads by the sum of all scores in the thread.
8399 Unscored articles will be counted as having a score of zero."
8400   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8401
8402 (defun gnus-thread-total-score (thread)
8403   ;;  This function find the total score of THREAD.
8404   (cond ((null thread)
8405          0)
8406         ((consp thread)
8407          (if (stringp (car thread))
8408              (apply gnus-thread-score-function 0
8409                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8410            (gnus-thread-total-score-1 thread)))
8411         (t
8412          (gnus-thread-total-score-1 (list thread)))))
8413
8414 (defun gnus-thread-total-score-1 (root)
8415   ;; This function find the total score of the thread below ROOT.
8416   (setq root (car root))
8417   (apply gnus-thread-score-function
8418          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8419              gnus-summary-default-score 0)
8420          (mapcar 'gnus-thread-total-score
8421                  (cdr (gnus-gethash (mail-header-id root)
8422                                     gnus-newsgroup-dependencies)))))
8423
8424 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8425 (defvar gnus-tmp-prev-subject nil)
8426 (defvar gnus-tmp-false-parent nil)
8427 (defvar gnus-tmp-root-expunged nil)
8428 (defvar gnus-tmp-dummy-line nil)
8429
8430 (defun gnus-summary-prepare-threads (threads)
8431   "Prepare summary buffer from THREADS and indentation LEVEL.
8432 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8433 or a straight list of headers."
8434   (gnus-message 7 "Generating summary...")
8435
8436   (setq gnus-newsgroup-threads threads)
8437   (beginning-of-line)
8438
8439   (let ((gnus-tmp-level 0)
8440         (default-score (or gnus-summary-default-score 0))
8441         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8442         thread number subject stack state gnus-tmp-gathered beg-match
8443         new-roots gnus-tmp-new-adopts thread-end
8444         gnus-tmp-header gnus-tmp-unread
8445         gnus-tmp-replied gnus-tmp-subject-or-nil
8446         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8447         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8448         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8449
8450     (setq gnus-tmp-prev-subject nil)
8451
8452     (if (vectorp (car threads))
8453         ;; If this is a straight (sic) list of headers, then a
8454         ;; threaded summary display isn't required, so we just create
8455         ;; an unthreaded one.
8456         (gnus-summary-prepare-unthreaded threads)
8457
8458       ;; Do the threaded display.
8459
8460       (while (or threads stack gnus-tmp-new-adopts new-roots)
8461
8462         (if (and (= gnus-tmp-level 0)
8463                  (not (setq gnus-tmp-dummy-line nil))
8464                  (or (not stack)
8465                      (= (caar stack) 0))
8466                  (not gnus-tmp-false-parent)
8467                  (or gnus-tmp-new-adopts new-roots))
8468             (if gnus-tmp-new-adopts
8469                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8470                       thread (list (car gnus-tmp-new-adopts))
8471                       gnus-tmp-header (caar thread)
8472                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8473               (if new-roots
8474                   (setq thread (list (car new-roots))
8475                         gnus-tmp-header (caar thread)
8476                         new-roots (cdr new-roots))))
8477
8478           (if threads
8479               ;; If there are some threads, we do them before the
8480               ;; threads on the stack.
8481               (setq thread threads
8482                     gnus-tmp-header (caar thread))
8483             ;; There were no current threads, so we pop something off
8484             ;; the stack.
8485             (setq state (car stack)
8486                   gnus-tmp-level (car state)
8487                   thread (cdr state)
8488                   stack (cdr stack)
8489                   gnus-tmp-header (caar thread))))
8490
8491         (setq gnus-tmp-false-parent nil)
8492         (setq gnus-tmp-root-expunged nil)
8493         (setq thread-end nil)
8494
8495         (if (stringp gnus-tmp-header)
8496             ;; The header is a dummy root.
8497             (cond
8498              ((eq gnus-summary-make-false-root 'adopt)
8499               ;; We let the first article adopt the rest.
8500               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8501                                                (cddar thread)))
8502               (setq gnus-tmp-gathered
8503                     (nconc (mapcar
8504                             (lambda (h) (mail-header-number (car h)))
8505                             (cddar thread))
8506                            gnus-tmp-gathered))
8507               (setq thread (cons (list (caar thread)
8508                                        (cadar thread))
8509                                  (cdr thread)))
8510               (setq gnus-tmp-level -1
8511                     gnus-tmp-false-parent t))
8512              ((eq gnus-summary-make-false-root 'empty)
8513               ;; We print adopted articles with empty subject fields.
8514               (setq gnus-tmp-gathered
8515                     (nconc (mapcar
8516                             (lambda (h) (mail-header-number (car h)))
8517                             (cddar thread))
8518                            gnus-tmp-gathered))
8519               (setq gnus-tmp-level -1))
8520              ((eq gnus-summary-make-false-root 'dummy)
8521               ;; We remember that we probably want to output a dummy
8522               ;; root.
8523               (setq gnus-tmp-dummy-line gnus-tmp-header)
8524               (setq gnus-tmp-prev-subject gnus-tmp-header))
8525              (t
8526               ;; We do not make a root for the gathered
8527               ;; sub-threads at all.
8528               (setq gnus-tmp-level -1)))
8529
8530           (setq number (mail-header-number gnus-tmp-header)
8531                 subject (mail-header-subject gnus-tmp-header))
8532
8533           (cond
8534            ;; If the thread has changed subject, we might want to make
8535            ;; this subthread into a root.
8536            ((and (null gnus-thread-ignore-subject)
8537                  (not (zerop gnus-tmp-level))
8538                  gnus-tmp-prev-subject
8539                  (not (inline
8540                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8541             (setq new-roots (nconc new-roots (list (car thread)))
8542                   thread-end t
8543                   gnus-tmp-header nil))
8544            ;; If the article lies outside the current limit,
8545            ;; then we do not display it.
8546            ((and (not (memq number gnus-newsgroup-limit))
8547                  (not gnus-tmp-dummy-line))
8548             (setq gnus-tmp-gathered
8549                   (nconc (mapcar
8550                           (lambda (h) (mail-header-number (car h)))
8551                           (cdar thread))
8552                          gnus-tmp-gathered))
8553             (setq gnus-tmp-new-adopts (if (cdar thread)
8554                                           (append gnus-tmp-new-adopts
8555                                                   (cdar thread))
8556                                         gnus-tmp-new-adopts)
8557                   thread-end t
8558                   gnus-tmp-header nil)
8559             (when (zerop gnus-tmp-level)
8560               (setq gnus-tmp-root-expunged t)))
8561            ;; Perhaps this article is to be marked as read?
8562            ((and gnus-summary-mark-below
8563                  (< (or (cdr (assq number gnus-newsgroup-scored))
8564                         default-score)
8565                     gnus-summary-mark-below)
8566                  ;; Don't touch sparse articles.
8567                  (not (memq number gnus-newsgroup-sparse))
8568                  (not (memq number gnus-newsgroup-ancient)))
8569             (setq gnus-newsgroup-unreads
8570                   (delq number gnus-newsgroup-unreads))
8571             (if gnus-newsgroup-auto-expire
8572                 (push number gnus-newsgroup-expirable)
8573               (push (cons number gnus-low-score-mark)
8574                     gnus-newsgroup-reads))))
8575
8576           (when gnus-tmp-header
8577             ;; We may have an old dummy line to output before this
8578             ;; article.
8579             (when gnus-tmp-dummy-line
8580               (gnus-summary-insert-dummy-line
8581                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8582               (setq gnus-tmp-dummy-line nil))
8583
8584             ;; Compute the mark.
8585             (setq
8586              gnus-tmp-unread
8587              (cond
8588               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8589               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8590               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8591               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8592               (t (or (cdr (assq number gnus-newsgroup-reads))
8593                      gnus-ancient-mark))))
8594
8595             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8596                                   gnus-tmp-header gnus-tmp-level)
8597                   gnus-newsgroup-data)
8598
8599             ;; Actually insert the line.
8600             (setq
8601              gnus-tmp-subject-or-nil
8602              (cond
8603               ((and gnus-thread-ignore-subject
8604                     gnus-tmp-prev-subject
8605                     (not (inline (gnus-subject-equal
8606                                   gnus-tmp-prev-subject subject))))
8607                subject)
8608               ((zerop gnus-tmp-level)
8609                (if (and (eq gnus-summary-make-false-root 'empty)
8610                         (memq number gnus-tmp-gathered)
8611                         gnus-tmp-prev-subject
8612                         (inline (gnus-subject-equal
8613                                  gnus-tmp-prev-subject subject)))
8614                    gnus-summary-same-subject
8615                  subject))
8616               (t gnus-summary-same-subject)))
8617             (if (and (eq gnus-summary-make-false-root 'adopt)
8618                      (= gnus-tmp-level 1)
8619                      (memq number gnus-tmp-gathered))
8620                 (setq gnus-tmp-opening-bracket ?\<
8621                       gnus-tmp-closing-bracket ?\>)
8622               (setq gnus-tmp-opening-bracket ?\[
8623                     gnus-tmp-closing-bracket ?\]))
8624             (setq
8625              gnus-tmp-indentation
8626              (aref gnus-thread-indent-array gnus-tmp-level)
8627              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8628              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8629                                 gnus-summary-default-score 0)
8630              gnus-tmp-score-char
8631              (if (or (null gnus-summary-default-score)
8632                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8633                          gnus-summary-zcore-fuzz)) ? 
8634                (if (< gnus-tmp-score gnus-summary-default-score)
8635                    gnus-score-below-mark gnus-score-over-mark))
8636              gnus-tmp-replied
8637              (cond ((memq number gnus-newsgroup-processable)
8638                     gnus-process-mark)
8639                    ((memq number gnus-newsgroup-cached)
8640                     gnus-cached-mark)
8641                    ((memq number gnus-newsgroup-replied)
8642                     gnus-replied-mark)
8643                    ((memq number gnus-newsgroup-saved)
8644                     gnus-saved-mark)
8645                    (t gnus-unread-mark))
8646              gnus-tmp-from (mail-header-from gnus-tmp-header)
8647              gnus-tmp-name
8648              (cond
8649               ((string-match "(.+)" gnus-tmp-from)
8650                (substring gnus-tmp-from
8651                           (1+ (match-beginning 0)) (1- (match-end 0))))
8652               ((string-match "<[^>]+> *$" gnus-tmp-from)
8653                (setq beg-match (match-beginning 0))
8654                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8655                         (substring gnus-tmp-from (1+ (match-beginning 0))
8656                                    (1- (match-end 0))))
8657                    (substring gnus-tmp-from 0 beg-match)))
8658               (t gnus-tmp-from)))
8659             (when (string= gnus-tmp-name "")
8660               (setq gnus-tmp-name gnus-tmp-from))
8661             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8662             (gnus-put-text-property
8663              (point)
8664              (progn (eval gnus-summary-line-format-spec) (point))
8665              'gnus-number number)
8666             (when gnus-visual-p
8667               (forward-line -1)
8668               (run-hooks 'gnus-summary-update-hook)
8669               (forward-line 1))
8670
8671             (setq gnus-tmp-prev-subject subject)))
8672
8673         (when (nth 1 thread)
8674           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8675         (incf gnus-tmp-level)
8676         (setq threads (if thread-end nil (cdar thread)))
8677         (unless threads
8678           (setq gnus-tmp-level 0)))))
8679   (gnus-message 7 "Generating summary...done"))
8680
8681 (defun gnus-summary-prepare-unthreaded (headers)
8682   "Generate an unthreaded summary buffer based on HEADERS."
8683   (let (header number mark)
8684
8685     (while headers
8686       ;; We may have to root out some bad articles...
8687       (when (memq (setq number (mail-header-number
8688                                 (setq header (pop headers))))
8689                   gnus-newsgroup-limit)
8690         ;; Mark article as read when it has a low score.
8691         (when (and gnus-summary-mark-below
8692                    (< (or (cdr (assq number gnus-newsgroup-scored))
8693                           gnus-summary-default-score 0)
8694                       gnus-summary-mark-below)
8695                    (not (memq number gnus-newsgroup-ancient)))
8696           (setq gnus-newsgroup-unreads
8697                 (delq number gnus-newsgroup-unreads))
8698           (if gnus-newsgroup-auto-expire
8699               (push number gnus-newsgroup-expirable)
8700             (push (cons number gnus-low-score-mark)
8701                   gnus-newsgroup-reads)))
8702
8703         (setq mark
8704               (cond
8705                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8706                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8707                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8708                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8709                (t (or (cdr (assq number gnus-newsgroup-reads))
8710                       gnus-ancient-mark))))
8711         (setq gnus-newsgroup-data
8712               (cons (gnus-data-make number mark (1+ (point)) header 0)
8713                     gnus-newsgroup-data))
8714         (gnus-summary-insert-line
8715          header 0 nil mark (memq number gnus-newsgroup-replied)
8716          (memq number gnus-newsgroup-expirable)
8717          (mail-header-subject header) nil
8718          (cdr (assq number gnus-newsgroup-scored))
8719          (memq number gnus-newsgroup-processable))))))
8720
8721 (defun gnus-select-newsgroup (group &optional read-all)
8722   "Select newsgroup GROUP.
8723 If READ-ALL is non-nil, all articles in the group are selected."
8724   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8725          (info (nth 2 entry))
8726          articles fetched-articles cached)
8727
8728     (or (gnus-check-server
8729          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8730         (error "Couldn't open server"))
8731
8732     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8733         (gnus-activate-group group)     ; Or we can activate it...
8734         (progn                          ; Or we bug out.
8735           (when (equal major-mode 'gnus-summary-mode)
8736             (kill-buffer (current-buffer)))
8737           (error "Couldn't request group %s: %s"
8738                  group (gnus-status-message group))))
8739
8740     (unless (gnus-request-group group t)
8741       (when (equal major-mode 'gnus-summary-mode)
8742         (kill-buffer (current-buffer)))
8743       (error "Couldn't request group %s: %s"
8744              group (gnus-status-message group)))      
8745
8746     (setq gnus-newsgroup-name group)
8747     (setq gnus-newsgroup-unselected nil)
8748     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8749
8750     (and gnus-asynchronous
8751          (gnus-check-backend-function
8752           'request-asynchronous gnus-newsgroup-name)
8753          (setq gnus-newsgroup-async
8754                (gnus-request-asynchronous gnus-newsgroup-name)))
8755
8756     ;; Adjust and set lists of article marks.
8757     (when info
8758       (gnus-adjust-marked-articles info))
8759
8760     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8761     (when (gnus-virtual-group-p group)
8762       (setq cached gnus-newsgroup-cached))
8763
8764     (setq gnus-newsgroup-unreads
8765           (gnus-set-difference
8766            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8767            gnus-newsgroup-dormant))
8768
8769     (setq gnus-newsgroup-processable nil)
8770
8771     (setq articles (gnus-articles-to-read group read-all))
8772
8773     (cond
8774      ((null articles)
8775       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8776       'quit)
8777      ((eq articles 0) nil)
8778      (t
8779       ;; Init the dependencies hash table.
8780       (setq gnus-newsgroup-dependencies
8781             (gnus-make-hashtable (length articles)))
8782       ;; Retrieve the headers and read them in.
8783       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8784       (setq gnus-newsgroup-headers
8785             (if (eq 'nov
8786                     (setq gnus-headers-retrieved-by
8787                           (gnus-retrieve-headers
8788                            articles gnus-newsgroup-name
8789                            ;; We might want to fetch old headers, but
8790                            ;; not if there is only 1 article.
8791                            (and gnus-fetch-old-headers
8792                                 (or (and
8793                                      (not (eq gnus-fetch-old-headers 'some))
8794                                      (not (numberp gnus-fetch-old-headers)))
8795                                     (> (length articles) 1))))))
8796                 (gnus-get-newsgroup-headers-xover articles)
8797               (gnus-get-newsgroup-headers)))
8798       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
8799
8800       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8801       (when cached
8802         (setq gnus-newsgroup-cached cached))
8803
8804       ;; Set the initial limit.
8805       (setq gnus-newsgroup-limit (copy-sequence articles))
8806       ;; Remove canceled articles from the list of unread articles.
8807       (setq gnus-newsgroup-unreads
8808             (gnus-set-sorted-intersection
8809              gnus-newsgroup-unreads
8810              (setq fetched-articles
8811                    (mapcar (lambda (headers) (mail-header-number headers))
8812                            gnus-newsgroup-headers))))
8813       ;; Removed marked articles that do not exist.
8814       (gnus-update-missing-marks
8815        (gnus-sorted-complement fetched-articles articles))
8816       ;; We might want to build some more threads first.
8817       (and gnus-fetch-old-headers
8818            (eq gnus-headers-retrieved-by 'nov)
8819            (gnus-build-old-threads))
8820       ;; Check whether auto-expire is to be done in this group.
8821       (setq gnus-newsgroup-auto-expire
8822             (gnus-group-auto-expirable-p group))
8823       ;; Set up the article buffer now, if necessary.
8824       (unless gnus-single-article-buffer
8825         (gnus-article-setup-buffer))
8826       ;; First and last article in this newsgroup.
8827       (when gnus-newsgroup-headers
8828         (setq gnus-newsgroup-begin
8829               (mail-header-number (car gnus-newsgroup-headers))
8830               gnus-newsgroup-end
8831               (mail-header-number
8832                (gnus-last-element gnus-newsgroup-headers))))
8833       (setq gnus-reffed-article-number -1)
8834       ;; GROUP is successfully selected.
8835       (or gnus-newsgroup-headers t)))))
8836
8837 (defun gnus-articles-to-read (group read-all)
8838   ;; Find out what articles the user wants to read.
8839   (let* ((articles
8840           ;; Select all articles if `read-all' is non-nil, or if there
8841           ;; are no unread articles.
8842           (if (or read-all
8843                   (and (zerop (length gnus-newsgroup-marked))
8844                        (zerop (length gnus-newsgroup-unreads))))
8845               (gnus-uncompress-range (gnus-active group))
8846             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8847                           (copy-sequence gnus-newsgroup-unreads))
8848                   '<)))
8849          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8850          (scored (length scored-list))
8851          (number (length articles))
8852          (marked (+ (length gnus-newsgroup-marked)
8853                     (length gnus-newsgroup-dormant)))
8854          (select
8855           (cond
8856            ((numberp read-all)
8857             read-all)
8858            (t
8859             (condition-case ()
8860                 (cond
8861                  ((and (or (<= scored marked) (= scored number))
8862                        (numberp gnus-large-newsgroup)
8863                        (> number gnus-large-newsgroup))
8864                   (let ((input
8865                          (read-string
8866                           (format
8867                            "How many articles from %s (default %d): "
8868                            gnus-newsgroup-name number))))
8869                     (if (string-match "^[ \t]*$" input) number input)))
8870                  ((and (> scored marked) (< scored number)
8871                        (> (- scored number) 20))
8872                   (let ((input
8873                          (read-string
8874                           (format "%s %s (%d scored, %d total): "
8875                                   "How many articles from"
8876                                   group scored number))))
8877                     (if (string-match "^[ \t]*$" input)
8878                         number input)))
8879                  (t number))
8880               (quit nil))))))
8881     (setq select (if (stringp select) (string-to-number select) select))
8882     (if (or (null select) (zerop select))
8883         select
8884       (if (and (not (zerop scored)) (<= (abs select) scored))
8885           (progn
8886             (setq articles (sort scored-list '<))
8887             (setq number (length articles)))
8888         (setq articles (copy-sequence articles)))
8889
8890       (if (< (abs select) number)
8891           (if (< select 0)
8892               ;; Select the N oldest articles.
8893               (setcdr (nthcdr (1- (abs select)) articles) nil)
8894             ;; Select the N most recent articles.
8895             (setq articles (nthcdr (- number select) articles))))
8896       (setq gnus-newsgroup-unselected
8897             (gnus-sorted-intersection
8898              gnus-newsgroup-unreads
8899              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8900       articles)))
8901
8902 (defun gnus-killed-articles (killed articles)
8903   (let (out)
8904     (while articles
8905       (if (inline (gnus-member-of-range (car articles) killed))
8906           (setq out (cons (car articles) out)))
8907       (setq articles (cdr articles)))
8908     out))
8909
8910 (defun gnus-uncompress-marks (marks)
8911   "Uncompress the mark ranges in MARKS."
8912   (let ((uncompressed '(score bookmark))
8913         out)
8914     (while marks
8915       (if (memq (caar marks) uncompressed)
8916           (push (car marks) out)
8917         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8918       (setq marks (cdr marks)))
8919     out))
8920
8921 (defun gnus-adjust-marked-articles (info)
8922   "Set all article lists and remove all marks that are no longer legal."
8923   (let* ((marked-lists (gnus-info-marks info))
8924          (active (gnus-active (gnus-info-group info)))
8925          (min (car active))
8926          (max (cdr active))
8927          (types gnus-article-mark-lists)
8928          (uncompressed '(score bookmark))
8929          marks var articles article mark)
8930
8931     (while marked-lists
8932       (setq marks (pop marked-lists))
8933       (set (setq var (intern (format "gnus-newsgroup-%s"
8934                                      (car (rassq (setq mark (car marks))
8935                                                  types)))))
8936            (if (memq (car marks) uncompressed) (cdr marks)
8937              (gnus-uncompress-range (cdr marks))))
8938
8939       (setq articles (symbol-value var))
8940
8941       ;; All articles have to be subsets of the active articles.
8942       (cond
8943        ;; Adjust "simple" lists.
8944        ((memq mark '(tick dormant expirable reply killed save))
8945         (while articles
8946           (when (or (< (setq article (pop articles)) min) (> article max))
8947             (set var (delq article (symbol-value var))))))
8948        ;; Adjust assocs.
8949        ((memq mark '(score bookmark))
8950         (while articles
8951           (when (or (< (car (setq article (pop articles))) min)
8952                     (> (car article) max))
8953             (set var (delq article (symbol-value var))))))))))
8954
8955 (defun gnus-update-missing-marks (missing)
8956   "Go through the list of MISSING articles and remove them mark lists."
8957   (when missing
8958     (let ((types gnus-article-mark-lists)
8959           var m)
8960       ;; Go through all types.
8961       (while types
8962         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8963         (when (symbol-value var)
8964           ;; This list has articles.  So we delete all missing articles
8965           ;; from it.
8966           (setq m missing)
8967           (while m
8968             (set var (delq (pop m) (symbol-value var)))))))))
8969
8970 (defun gnus-update-marks ()
8971   "Enter the various lists of marked articles into the newsgroup info list."
8972   (let ((types gnus-article-mark-lists)
8973         (info (gnus-get-info gnus-newsgroup-name))
8974         (uncompressed '(score bookmark killed))
8975         type list newmarked symbol)
8976     (when info
8977       ;; Add all marks lists that are non-nil to the list of marks lists.
8978       (while types
8979         (setq type (pop types))
8980         (when (setq list (symbol-value
8981                           (setq symbol
8982                                 (intern (format "gnus-newsgroup-%s"
8983                                                 (car type))))))
8984           (push (cons (cdr type)
8985                       (if (memq (cdr type) uncompressed) list
8986                         (gnus-compress-sequence 
8987                          (set symbol (sort list '<)) t)))
8988                 newmarked)))
8989
8990       ;; Enter these new marks into the info of the group.
8991       (if (nthcdr 3 info)
8992           (setcar (nthcdr 3 info) newmarked)
8993         ;; Add the marks lists to the end of the info.
8994         (when newmarked
8995           (setcdr (nthcdr 2 info) (list newmarked))))
8996
8997       ;; Cut off the end of the info if there's nothing else there.
8998       (let ((i 5))
8999         (while (and (> i 2)
9000                     (not (nth i info)))
9001           (when (nthcdr (decf i) info)
9002             (setcdr (nthcdr i info) nil)))))))
9003
9004 (defun gnus-add-marked-articles (group type articles &optional info force)
9005   ;; Add ARTICLES of TYPE to the info of GROUP.
9006   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
9007   ;; add, but replace marked articles of TYPE with ARTICLES.
9008   (let ((info (or info (gnus-get-info group)))
9009         (uncompressed '(score bookmark killed))
9010         marked m)
9011     (or (not info)
9012         (and (not (setq marked (nthcdr 3 info)))
9013              (or (null articles)
9014                  (setcdr (nthcdr 2 info)
9015                          (list (list (cons type (gnus-compress-sequence
9016                                                  articles t)))))))
9017         (and (not (setq m (assq type (car marked))))
9018              (or (null articles)
9019                  (setcar marked
9020                          (cons (cons type (gnus-compress-sequence articles t) )
9021                                (car marked)))))
9022         (if force
9023             (if (null articles)
9024                 (setcar (nthcdr 3 info)
9025                         (delq (assq type (car marked)) (car marked)))
9026               (setcdr m (gnus-compress-sequence articles t)))
9027           (setcdr m (gnus-compress-sequence
9028                      (sort (nconc (gnus-uncompress-range (cdr m))
9029                                   (copy-sequence articles)) '<) t))))))
9030
9031 (defun gnus-set-mode-line (where)
9032   "This function sets the mode line of the article or summary buffers.
9033 If WHERE is `summary', the summary mode line format will be used."
9034   ;; Is this mode line one we keep updated?
9035   (when (memq where gnus-updated-mode-lines)
9036     (let (mode-string)
9037       (save-excursion
9038         ;; We evaluate this in the summary buffer since these
9039         ;; variables are buffer-local to that buffer.
9040         (set-buffer gnus-summary-buffer)
9041         ;; We bind all these variables that are used in the `eval' form
9042         ;; below.
9043         (let* ((mformat (symbol-value
9044                          (intern
9045                           (format "gnus-%s-mode-line-format-spec" where))))
9046                (gnus-tmp-group-name gnus-newsgroup-name)
9047                (gnus-tmp-article-number (or gnus-current-article 0))
9048                (gnus-tmp-unread gnus-newsgroup-unreads)
9049                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
9050                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
9051                (gnus-tmp-unread-and-unselected
9052                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
9053                             (zerop gnus-tmp-unselected)) "")
9054                       ((zerop gnus-tmp-unselected)
9055                        (format "{%d more}" gnus-tmp-unread-and-unticked))
9056                       (t (format "{%d(+%d) more}"
9057                                  gnus-tmp-unread-and-unticked
9058                                  gnus-tmp-unselected))))
9059                (gnus-tmp-subject
9060                 (if (and gnus-current-headers
9061                          (vectorp gnus-current-headers))
9062                     (gnus-mode-string-quote
9063                      (mail-header-subject gnus-current-headers)) ""))
9064                max-len
9065                gnus-tmp-header);; passed as argument to any user-format-funcs
9066           (setq mode-string (eval mformat))
9067           (setq max-len (max 4 (if gnus-mode-non-string-length
9068                                    (- (window-width)
9069                                       gnus-mode-non-string-length)
9070                                  (length mode-string))))
9071           ;; We might have to chop a bit of the string off...
9072           (when (> (length mode-string) max-len)
9073             (setq mode-string
9074                   (concat (gnus-truncate-string mode-string (- max-len 3))
9075                           "...")))
9076           ;; Pad the mode string a bit.
9077           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
9078       ;; Update the mode line.
9079       (setq mode-line-buffer-identification 
9080             (gnus-mode-line-buffer-identification
9081              (list mode-string)))
9082       (set-buffer-modified-p t))))
9083
9084 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
9085   "Go through the HEADERS list and add all Xrefs to a hash table.
9086 The resulting hash table is returned, or nil if no Xrefs were found."
9087   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
9088          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
9089          (xref-hashtb (make-vector 63 0))
9090          start group entry number xrefs header)
9091     (while headers
9092       (setq header (pop headers))
9093       (when (and (setq xrefs (mail-header-xref header))
9094                  (not (memq (setq number (mail-header-number header))
9095                             unreads)))
9096         (setq start 0)
9097         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
9098           (setq start (match-end 0))
9099           (setq group (if prefix
9100                           (concat prefix (substring xrefs (match-beginning 1)
9101                                                     (match-end 1)))
9102                         (substring xrefs (match-beginning 1) (match-end 1))))
9103           (setq number
9104                 (string-to-int (substring xrefs (match-beginning 2)
9105                                           (match-end 2))))
9106           (if (setq entry (gnus-gethash group xref-hashtb))
9107               (setcdr entry (cons number (cdr entry)))
9108             (gnus-sethash group (cons number nil) xref-hashtb)))))
9109     (and start xref-hashtb)))
9110
9111 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
9112   "Look through all the headers and mark the Xrefs as read."
9113   (let ((virtual (gnus-virtual-group-p from-newsgroup))
9114         name entry info xref-hashtb idlist method nth4)
9115     (save-excursion
9116       (set-buffer gnus-group-buffer)
9117       (when (setq xref-hashtb
9118                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
9119         (mapatoms
9120          (lambda (group)
9121            (unless (string= from-newsgroup (setq name (symbol-name group)))
9122              (setq idlist (symbol-value group))
9123              ;; Dead groups are not updated.
9124              (and (prog1
9125                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
9126                             info (nth 2 entry))
9127                     (if (stringp (setq nth4 (gnus-info-method info)))
9128                         (setq nth4 (gnus-server-to-method nth4))))
9129                   ;; Only do the xrefs if the group has the same
9130                   ;; select method as the group we have just read.
9131                   (or (gnus-methods-equal-p
9132                        nth4 (gnus-find-method-for-group from-newsgroup))
9133                       virtual
9134                       (equal nth4 (setq method (gnus-find-method-for-group
9135                                                 from-newsgroup)))
9136                       (and (equal (car nth4) (car method))
9137                            (equal (nth 1 nth4) (nth 1 method))))
9138                   gnus-use-cross-reference
9139                   (or (not (eq gnus-use-cross-reference t))
9140                       virtual
9141                       ;; Only do cross-references on subscribed
9142                       ;; groups, if that is what is wanted.
9143                       (<= (gnus-info-level info) gnus-level-subscribed))
9144                   (gnus-group-make-articles-read name idlist))))
9145          xref-hashtb)))))
9146
9147 (defun gnus-group-make-articles-read (group articles)
9148   (let* ((num 0)
9149          (entry (gnus-gethash group gnus-newsrc-hashtb))
9150          (info (nth 2 entry))
9151          (active (gnus-active group))
9152          range)
9153     ;; First peel off all illegal article numbers.
9154     (if active
9155         (let ((ids articles)
9156               id first)
9157           (while ids
9158             (setq id (car ids))
9159             (if (and first (> id (cdr active)))
9160                 (progn
9161                   ;; We'll end up in this situation in one particular
9162                   ;; obscure situation.  If you re-scan a group and get
9163                   ;; a new article that is cross-posted to a different
9164                   ;; group that has not been re-scanned, you might get
9165                   ;; crossposted article that has a higher number than
9166                   ;; Gnus believes possible.  So we re-activate this
9167                   ;; group as well.  This might mean doing the
9168                   ;; crossposting thingy will *increase* the number
9169                   ;; of articles in some groups.  Tsk, tsk.
9170                   (setq active (or (gnus-activate-group group) active))))
9171             (if (or (> id (cdr active))
9172                     (< id (car active)))
9173                 (setq articles (delq id articles)))
9174             (setq ids (cdr ids)))))
9175     ;; If the read list is nil, we init it.
9176     (and active
9177          (null (gnus-info-read info))
9178          (> (car active) 1)
9179          (gnus-info-set-read info (cons 1 (1- (car active)))))
9180     ;; Then we add the read articles to the range.
9181     (gnus-info-set-read
9182      info
9183      (setq range
9184            (gnus-add-to-range
9185             (gnus-info-read info) (setq articles (sort articles '<)))))
9186     ;; Then we have to re-compute how many unread
9187     ;; articles there are in this group.
9188     (if active
9189         (progn
9190           (cond
9191            ((not range)
9192             (setq num (- (1+ (cdr active)) (car active))))
9193            ((not (listp (cdr range)))
9194             (setq num (- (cdr active) (- (1+ (cdr range))
9195                                          (car range)))))
9196            (t
9197             (while range
9198               (if (numberp (car range))
9199                   (setq num (1+ num))
9200                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
9201               (setq range (cdr range)))
9202             (setq num (- (cdr active) num))))
9203           ;; Update the number of unread articles.
9204           (setcar entry num)
9205           ;; Update the group buffer.
9206           (gnus-group-update-group group t)))))
9207
9208 (defun gnus-methods-equal-p (m1 m2)
9209   (let ((m1 (or m1 gnus-select-method))
9210         (m2 (or m2 gnus-select-method)))
9211     (or (equal m1 m2)
9212         (and (eq (car m1) (car m2))
9213              (or (not (memq 'address (assoc (symbol-name (car m1))
9214                                             gnus-valid-select-methods)))
9215                  (equal (nth 1 m1) (nth 1 m2)))))))
9216
9217 (defsubst gnus-header-value ()
9218   (buffer-substring (match-end 0) (gnus-point-at-eol)))
9219
9220 (defvar gnus-newsgroup-none-id 0)
9221
9222 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
9223   (let ((cur nntp-server-buffer)
9224         (dependencies
9225          (or dependencies
9226              (save-excursion (set-buffer gnus-summary-buffer)
9227                              gnus-newsgroup-dependencies)))
9228         headers id id-dep ref-dep end ref)
9229     (save-excursion
9230       (set-buffer nntp-server-buffer)
9231       (run-hooks 'gnus-parse-headers-hook)
9232       (let ((case-fold-search t)
9233             in-reply-to header p lines)
9234         (goto-char (point-min))
9235         ;; Search to the beginning of the next header.  Error messages
9236         ;; do not begin with 2 or 3.
9237         (while (re-search-forward "^[23][0-9]+ " nil t)
9238           (setq id nil
9239                 ref nil)
9240           ;; This implementation of this function, with nine
9241           ;; search-forwards instead of the one re-search-forward and
9242           ;; a case (which basically was the old function) is actually
9243           ;; about twice as fast, even though it looks messier.  You
9244           ;; can't have everything, I guess.  Speed and elegance
9245           ;; doesn't always go hand in hand.
9246           (setq
9247            header
9248            (vector
9249             ;; Number.
9250             (prog1
9251                 (read cur)
9252               (end-of-line)
9253               (setq p (point))
9254               (narrow-to-region (point)
9255                                 (or (and (search-forward "\n.\n" nil t)
9256                                          (- (point) 2))
9257                                     (point))))
9258             ;; Subject.
9259             (progn
9260               (goto-char p)
9261               (if (search-forward "\nsubject: " nil t)
9262                   (gnus-header-value) "(none)"))
9263             ;; From.
9264             (progn
9265               (goto-char p)
9266               (if (search-forward "\nfrom: " nil t)
9267                   (gnus-header-value) "(nobody)"))
9268             ;; Date.
9269             (progn
9270               (goto-char p)
9271               (if (search-forward "\ndate: " nil t)
9272                   (gnus-header-value) ""))
9273             ;; Message-ID.
9274             (progn
9275               (goto-char p)
9276               (if (search-forward "\nmessage-id: " nil t)
9277                   (setq id (gnus-header-value))
9278                 ;; If there was no message-id, we just fake one to make
9279                 ;; subsequent routines simpler.
9280                 (setq id (concat "none+"
9281                                  (int-to-string
9282                                   (setq gnus-newsgroup-none-id
9283                                         (1+ gnus-newsgroup-none-id)))))))
9284             ;; References.
9285             (progn
9286               (goto-char p)
9287               (if (search-forward "\nreferences: " nil t)
9288                   (progn
9289                     (setq end (point))
9290                     (prog1
9291                         (gnus-header-value)
9292                       (setq ref
9293                             (buffer-substring
9294                              (progn
9295                                (end-of-line)
9296                                (search-backward ">" end t)
9297                                (1+ (point)))
9298                              (progn
9299                                (search-backward "<" end t)
9300                                (point))))))
9301                 ;; Get the references from the in-reply-to header if there
9302                 ;; were no references and the in-reply-to header looks
9303                 ;; promising.
9304                 (if (and (search-forward "\nin-reply-to: " nil t)
9305                          (setq in-reply-to (gnus-header-value))
9306                          (string-match "<[^>]+>" in-reply-to))
9307                     (setq ref (substring in-reply-to (match-beginning 0)
9308                                          (match-end 0)))
9309                   (setq ref ""))))
9310             ;; Chars.
9311             0
9312             ;; Lines.
9313             (progn
9314               (goto-char p)
9315               (if (search-forward "\nlines: " nil t)
9316                   (if (numberp (setq lines (read cur)))
9317                       lines 0)
9318                 0))
9319             ;; Xref.
9320             (progn
9321               (goto-char p)
9322               (and (search-forward "\nxref: " nil t)
9323                    (gnus-header-value)))))
9324           ;; We do the threading while we read the headers.  The
9325           ;; message-id and the last reference are both entered into
9326           ;; the same hash table.  Some tippy-toeing around has to be
9327           ;; done in case an article has arrived before the article
9328           ;; which it refers to.
9329           (if (boundp (setq id-dep (intern id dependencies)))
9330               (if (and (car (symbol-value id-dep))
9331                        (not force-new))
9332                   ;; An article with this Message-ID has already
9333                   ;; been seen, so we ignore this one, except we add
9334                   ;; any additional Xrefs (in case the two articles
9335                   ;; came from different servers).
9336                   (progn
9337                     (mail-header-set-xref
9338                      (car (symbol-value id-dep))
9339                      (concat (or (mail-header-xref
9340                                   (car (symbol-value id-dep))) "")
9341                              (or (mail-header-xref header) "")))
9342                     (setq header nil))
9343                 (setcar (symbol-value id-dep) header))
9344             (set id-dep (list header)))
9345           (when header
9346             (if (boundp (setq ref-dep (intern ref dependencies)))
9347                 (setcdr (symbol-value ref-dep)
9348                         (nconc (cdr (symbol-value ref-dep))
9349                                (list (symbol-value id-dep))))
9350               (set ref-dep (list nil (symbol-value id-dep))))
9351             (setq headers (cons header headers)))
9352           (goto-char (point-max))
9353           (widen))
9354         (nreverse headers)))))
9355
9356 ;; The following macros and functions were written by Felix Lee
9357 ;; <flee@cse.psu.edu>.
9358
9359 (defmacro gnus-nov-read-integer ()
9360   '(prog1
9361        (if (= (following-char) ?\t)
9362            0
9363          (let ((num (condition-case nil (read buffer) (error nil))))
9364            (if (numberp num) num 0)))
9365      (or (eobp) (forward-char 1))))
9366
9367 (defmacro gnus-nov-skip-field ()
9368   '(search-forward "\t" eol 'move))
9369
9370 (defmacro gnus-nov-field ()
9371   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9372
9373 ;; Goes through the xover lines and returns a list of vectors
9374 (defun gnus-get-newsgroup-headers-xover (sequence &optional 
9375                                                   force-new dependencies)
9376   "Parse the news overview data in the server buffer, and return a
9377 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9378   ;; Get the Xref when the users reads the articles since most/some
9379   ;; NNTP servers do not include Xrefs when using XOVER.
9380   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9381   (let ((cur nntp-server-buffer)
9382         (dependencies (or dependencies gnus-newsgroup-dependencies))
9383         number headers header)
9384     (save-excursion
9385       (set-buffer nntp-server-buffer)
9386       ;; Allow the user to mangle the headers before parsing them.
9387       (run-hooks 'gnus-parse-headers-hook)
9388       (goto-char (point-min))
9389       (while (and sequence (not (eobp)))
9390         (setq number (read cur))
9391         (while (and sequence (< (car sequence) number))
9392           (setq sequence (cdr sequence)))
9393         (and sequence
9394              (eq number (car sequence))
9395              (progn
9396                (setq sequence (cdr sequence))
9397                (if (setq header
9398                          (inline (gnus-nov-parse-line
9399                                   number dependencies force-new)))
9400                    (setq headers (cons header headers)))))
9401         (forward-line 1))
9402       (setq headers (nreverse headers)))
9403     headers))
9404
9405 ;; This function has to be called with point after the article number
9406 ;; on the beginning of the line.
9407 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9408   (let ((none 0)
9409         (eol (gnus-point-at-eol))
9410         (buffer (current-buffer))
9411         header ref id id-dep ref-dep)
9412
9413     ;; overview: [num subject from date id refs chars lines misc]
9414     (narrow-to-region (point) eol)
9415     (or (eobp) (forward-char))
9416
9417     (condition-case nil
9418         (setq header
9419               (vector
9420                number                   ; number
9421                (gnus-nov-field)         ; subject
9422                (gnus-nov-field)         ; from
9423                (gnus-nov-field)         ; date
9424                (setq id (or (gnus-nov-field)
9425                             (concat "none+"
9426                                     (int-to-string
9427                                      (setq none (1+ none)))))) ; id
9428                (progn
9429                  (save-excursion
9430                    (let ((beg (point)))
9431                      (search-forward "\t" eol)
9432                      (if (search-backward ">" beg t)
9433                          (setq ref
9434                                (buffer-substring
9435                                 (1+ (point))
9436                                 (search-backward "<" beg t)))
9437                        (setq ref nil))))
9438                  (gnus-nov-field))      ; refs
9439                (gnus-nov-read-integer)  ; chars
9440                (gnus-nov-read-integer)  ; lines
9441                (if (= (following-char) ?\n)
9442                    nil
9443                  (gnus-nov-field))      ; misc
9444                ))
9445       (error (progn
9446                (gnus-error 4 "Strange nov line")
9447                (setq header nil)
9448                (goto-char eol))))
9449
9450     (widen)
9451
9452     ;; We build the thread tree.
9453     (when header
9454       (if (boundp (setq id-dep (intern id dependencies)))
9455           (if (and (car (symbol-value id-dep))
9456                    (not force-new))
9457               ;; An article with this Message-ID has already been seen,
9458               ;; so we ignore this one, except we add any additional
9459               ;; Xrefs (in case the two articles came from different
9460               ;; servers.
9461               (progn
9462                 (mail-header-set-xref
9463                  (car (symbol-value id-dep))
9464                  (concat (or (mail-header-xref
9465                               (car (symbol-value id-dep))) "")
9466                          (or (mail-header-xref header) "")))
9467                 (setq header nil))
9468             (setcar (symbol-value id-dep) header))
9469         (set id-dep (list header))))
9470     (when header
9471       (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9472           (setcdr (symbol-value ref-dep)
9473                   (nconc (cdr (symbol-value ref-dep))
9474                          (list (symbol-value id-dep))))
9475         (set ref-dep (list nil (symbol-value id-dep)))))
9476     header))
9477
9478 (defun gnus-article-get-xrefs ()
9479   "Fill in the Xref value in `gnus-current-headers', if necessary.
9480 This is meant to be called in `gnus-article-internal-prepare-hook'."
9481   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9482                                  gnus-current-headers)))
9483     (or (not gnus-use-cross-reference)
9484         (not headers)
9485         (and (mail-header-xref headers)
9486              (not (string= (mail-header-xref headers) "")))
9487         (let ((case-fold-search t)
9488               xref)
9489           (save-restriction
9490             (nnheader-narrow-to-headers)
9491             (goto-char (point-min))
9492             (if (or (and (eq (downcase (following-char)) ?x)
9493                          (looking-at "Xref:"))
9494                     (search-forward "\nXref:" nil t))
9495                 (progn
9496                   (goto-char (1+ (match-end 0)))
9497                   (setq xref (buffer-substring (point)
9498                                                (progn (end-of-line) (point))))
9499                   (mail-header-set-xref headers xref))))))))
9500
9501 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
9502   "Find article ID and insert the summary line for that article."
9503   (let ((header (if (and old-header use-old-header)
9504                     old-header (gnus-read-header id)))
9505         (number (and (numberp id) id))
9506         pos)
9507     (when header
9508       ;; Rebuild the thread that this article is part of and go to the
9509       ;; article we have fetched.
9510       (when (and (not gnus-show-threads)
9511                  old-header)
9512         (when (setq pos (text-property-any
9513                          (point-min) (point-max) 'gnus-number 
9514                          (mail-header-number old-header)))
9515           (goto-char pos)
9516           (gnus-delete-line)
9517           (gnus-data-remove (mail-header-number old-header))))
9518       (when old-header
9519         (mail-header-set-number header (mail-header-number old-header)))
9520       (setq gnus-newsgroup-sparse
9521             (delq (setq number (mail-header-number header)) 
9522                   gnus-newsgroup-sparse))
9523       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
9524       (gnus-rebuild-thread (mail-header-id header))
9525       (gnus-summary-goto-subject number nil t))
9526     (when (and (numberp number)
9527                (> number 0))
9528       ;; We have to update the boundaries even if we can't fetch the
9529       ;; article if ID is a number -- so that the next `P' or `N'
9530       ;; command will fetch the previous (or next) article even
9531       ;; if the one we tried to fetch this time has been canceled.
9532       (and (> number gnus-newsgroup-end)
9533            (setq gnus-newsgroup-end number))
9534       (and (< number gnus-newsgroup-begin)
9535            (setq gnus-newsgroup-begin number))
9536       (setq gnus-newsgroup-unselected
9537             (delq number gnus-newsgroup-unselected)))
9538     ;; Report back a success?
9539     (and header (mail-header-number header))))
9540
9541 (defun gnus-summary-work-articles (n)
9542   "Return a list of articles to be worked upon.  The prefix argument,
9543 the list of process marked articles, and the current article will be
9544 taken into consideration."
9545   (cond
9546    ((and n (numberp n))
9547     ;; A numerical prefix has been given.
9548     (let ((backward (< n 0))
9549           (n (abs n))
9550           articles article)
9551       (save-excursion
9552         (while
9553             (and (> n 0)
9554                  (push (setq article (gnus-summary-article-number))
9555                        articles)
9556                  (if backward
9557                      (gnus-summary-find-prev nil article)
9558                    (gnus-summary-find-next nil article)))
9559           (decf n)))
9560       (nreverse articles)))
9561    ((and (boundp 'transient-mark-mode)
9562          transient-mark-mode
9563          mark-active)
9564     ;; Work on the region between point and mark.
9565     (let ((max (max (point) (mark)))
9566           articles article)
9567       (save-excursion
9568         (goto-char (min (point) (mark)))
9569         (while
9570             (and
9571              (push (setq article (gnus-summary-article-number)) articles)
9572              (gnus-summary-find-next nil article)
9573              (< (point) max)))
9574         (nreverse articles))))
9575    (gnus-newsgroup-processable
9576     ;; There are process-marked articles present.
9577     (reverse gnus-newsgroup-processable))
9578    (t
9579     ;; Just return the current article.
9580     (list (gnus-summary-article-number)))))
9581
9582 (defun gnus-summary-search-group (&optional backward use-level)
9583   "Search for next unread newsgroup.
9584 If optional argument BACKWARD is non-nil, search backward instead."
9585   (save-excursion
9586     (set-buffer gnus-group-buffer)
9587     (if (gnus-group-search-forward
9588          backward nil (if use-level (gnus-group-group-level) nil))
9589         (gnus-group-group-name))))
9590
9591 (defun gnus-summary-best-group (&optional exclude-group)
9592   "Find the name of the best unread group.
9593 If EXCLUDE-GROUP, do not go to this group."
9594   (save-excursion
9595     (set-buffer gnus-group-buffer)
9596     (save-excursion
9597       (gnus-group-best-unread-group exclude-group))))
9598
9599 (defun gnus-summary-find-next (&optional unread article backward)
9600   (if backward (gnus-summary-find-prev)
9601     (let* ((dummy (gnus-summary-article-intangible-p))
9602            (article (or article (gnus-summary-article-number)))
9603            (arts (gnus-data-find-list article))
9604            result)
9605       (when (and (not dummy)
9606                  (or (not gnus-summary-check-current)
9607                      (not unread)
9608                      (not (gnus-data-unread-p (car arts)))))
9609         (setq arts (cdr arts)))
9610       (when (setq result
9611                   (if unread
9612                       (progn
9613                         (while arts
9614                           (when (gnus-data-unread-p (car arts))
9615                             (setq result (car arts)
9616                                   arts nil))
9617                           (setq arts (cdr arts)))
9618                         result)
9619                     (car arts)))
9620         (goto-char (gnus-data-pos result))
9621         (gnus-data-number result)))))
9622
9623 (defun gnus-summary-find-prev (&optional unread article)
9624   (let* ((eobp (eobp))
9625          (article (or article (gnus-summary-article-number)))
9626          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9627          result)
9628     (when (and (not eobp)
9629                (or (not gnus-summary-check-current)
9630                    (not unread)
9631                    (not (gnus-data-unread-p (car arts)))))
9632       (setq arts (cdr arts)))
9633     (if (setq result
9634               (if unread
9635                   (progn
9636                     (while arts
9637                       (and (gnus-data-unread-p (car arts))
9638                            (setq result (car arts)
9639                                  arts nil))
9640                       (setq arts (cdr arts)))
9641                     result)
9642                 (car arts)))
9643         (progn
9644           (goto-char (gnus-data-pos result))
9645           (gnus-data-number result)))))
9646
9647 (defun gnus-summary-find-subject (subject &optional unread backward article)
9648   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9649          (article (or article (gnus-summary-article-number)))
9650          (articles (gnus-data-list backward))
9651          (arts (gnus-data-find-list article articles))
9652          result)
9653     (when (or (not gnus-summary-check-current)
9654               (not unread)
9655               (not (gnus-data-unread-p (car arts))))
9656       (setq arts (cdr arts)))
9657     (while arts
9658       (and (or (not unread)
9659                (gnus-data-unread-p (car arts)))
9660            (vectorp (gnus-data-header (car arts)))
9661            (gnus-subject-equal
9662             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9663            (setq result (car arts)
9664                  arts nil))
9665       (setq arts (cdr arts)))
9666     (and result
9667          (goto-char (gnus-data-pos result))
9668          (gnus-data-number result))))
9669
9670 (defun gnus-summary-search-forward (&optional unread subject backward)
9671   "Search forward for an article.
9672 If UNREAD, look for unread articles.  If SUBJECT, look for
9673 articles with that subject.  If BACKWARD, search backward instead."
9674   (cond (subject (gnus-summary-find-subject subject unread backward))
9675         (backward (gnus-summary-find-prev unread))
9676         (t (gnus-summary-find-next unread))))
9677
9678 (defun gnus-recenter (&optional n)
9679   "Center point in window and redisplay frame.
9680 Also do horizontal recentering."
9681   (interactive "P")
9682   (when (and gnus-auto-center-summary
9683              (not (eq gnus-auto-center-summary 'vertical)))
9684     (gnus-horizontal-recenter))
9685   (recenter n))
9686
9687 (defun gnus-summary-recenter ()
9688   "Center point in the summary window.
9689 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9690 displayed, no centering will be performed."
9691   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9692   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9693   (let* ((top (cond ((< (window-height) 4) 0)
9694                     ((< (window-height) 7) 1)
9695                     (t 2)))
9696          (height (1- (window-height)))
9697          (bottom (save-excursion (goto-char (point-max))
9698                                  (forward-line (- height))
9699                                  (point)))
9700          (window (get-buffer-window (current-buffer))))
9701     ;; The user has to want it.
9702     (when gnus-auto-center-summary
9703       (when (get-buffer-window gnus-article-buffer)
9704        ;; Only do recentering when the article buffer is displayed,
9705        ;; Set the window start to either `bottom', which is the biggest
9706        ;; possible valid number, or the second line from the top,
9707        ;; whichever is the least.
9708        (set-window-start
9709         window (min bottom (save-excursion 
9710                              (forward-line (- top)) (point)))))
9711       ;; Do horizontal recentering while we're at it.
9712       (when (and (get-buffer-window (current-buffer) t)
9713                  (not (eq gnus-auto-center-summary 'vertical)))
9714         (let ((selected (selected-window)))
9715           (select-window (get-buffer-window (current-buffer) t))
9716           (gnus-summary-position-point)
9717           (gnus-horizontal-recenter)
9718           (select-window selected))))))
9719
9720 (defun gnus-horizontal-recenter ()
9721   "Recenter the current buffer horizontally."
9722   (if (< (current-column) (/ (window-width) 2))
9723       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9724     (let* ((orig (point))
9725            (end (window-end (get-buffer-window (current-buffer) t)))
9726            (max 0))
9727       ;; Find the longest line currently displayed in the window.
9728       (goto-char (window-start))
9729       (while (and (not (eobp)) 
9730                   (< (point) end))
9731         (end-of-line)
9732         (setq max (max max (current-column)))
9733         (forward-line 1))
9734       (goto-char orig)
9735       ;; Scroll horizontally to center (sort of) the point.
9736       (if (> max (window-width))
9737           (set-window-hscroll 
9738            (get-buffer-window (current-buffer) t)
9739            (min (- (current-column) (/ (window-width) 3))
9740                 (+ 2 (- max (window-width)))))
9741         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9742       max)))
9743
9744 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9745 (defun gnus-short-group-name (group &optional levels)
9746   "Collapse GROUP name LEVELS."
9747   (let* ((name "") 
9748          (foreign "")
9749          (depth 0) 
9750          (skip 1)
9751          (levels (or levels
9752                      (progn
9753                        (while (string-match "\\." group skip)
9754                          (setq skip (match-end 0)
9755                                depth (+ depth 1)))
9756                        depth))))
9757     (if (string-match ":" group)
9758         (setq foreign (substring group 0 (match-end 0))
9759               group (substring group (match-end 0))))
9760     (while group
9761       (if (and (string-match "\\." group)
9762                (> levels (- gnus-group-uncollapsed-levels 1)))
9763           (setq name (concat name (substring group 0 1))
9764                 group (substring group (match-end 0))
9765                 levels (- levels 1)
9766                 name (concat name "."))
9767         (setq name (concat foreign name group)
9768               group nil)))
9769     name))
9770
9771 (defun gnus-summary-jump-to-group (newsgroup)
9772   "Move point to NEWSGROUP in group mode buffer."
9773   ;; Keep update point of group mode buffer if visible.
9774   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9775       (save-window-excursion
9776         ;; Take care of tree window mode.
9777         (if (get-buffer-window gnus-group-buffer)
9778             (pop-to-buffer gnus-group-buffer))
9779         (gnus-group-jump-to-group newsgroup))
9780     (save-excursion
9781       ;; Take care of tree window mode.
9782       (if (get-buffer-window gnus-group-buffer)
9783           (pop-to-buffer gnus-group-buffer)
9784         (set-buffer gnus-group-buffer))
9785       (gnus-group-jump-to-group newsgroup))))
9786
9787 ;; This function returns a list of article numbers based on the
9788 ;; difference between the ranges of read articles in this group and
9789 ;; the range of active articles.
9790 (defun gnus-list-of-unread-articles (group)
9791   (let* ((read (gnus-info-read (gnus-get-info group)))
9792          (active (gnus-active group))
9793          (last (cdr active))
9794          first nlast unread)
9795     ;; If none are read, then all are unread.
9796     (if (not read)
9797         (setq first (car active))
9798       ;; If the range of read articles is a single range, then the
9799       ;; first unread article is the article after the last read
9800       ;; article.  Sounds logical, doesn't it?
9801       (if (not (listp (cdr read)))
9802           (setq first (1+ (cdr read)))
9803         ;; `read' is a list of ranges.
9804         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9805                                 (caar read))) 1)
9806             (setq first 1))
9807         (while read
9808           (if first
9809               (while (< first nlast)
9810                 (setq unread (cons first unread))
9811                 (setq first (1+ first))))
9812           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9813           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9814           (setq read (cdr read)))))
9815     ;; And add the last unread articles.
9816     (while (<= first last)
9817       (setq unread (cons first unread))
9818       (setq first (1+ first)))
9819     ;; Return the list of unread articles.
9820     (nreverse unread)))
9821
9822 (defun gnus-list-of-read-articles (group)
9823   "Return a list of unread, unticked and non-dormant articles."
9824   (let* ((info (gnus-get-info group))
9825          (marked (gnus-info-marks info))
9826          (active (gnus-active group)))
9827     (and info active
9828          (gnus-set-difference
9829           (gnus-sorted-complement
9830            (gnus-uncompress-range active)
9831            (gnus-list-of-unread-articles group))
9832           (append
9833            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9834            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9835
9836 ;; Various summary commands
9837
9838 (defun gnus-summary-universal-argument (arg)
9839   "Perform any operation on all articles that are process/prefixed."
9840   (interactive "P")
9841   (gnus-set-global-variables)
9842   (let ((articles (gnus-summary-work-articles arg))
9843         func article)
9844     (if (eq
9845          (setq
9846           func
9847           (key-binding
9848            (read-key-sequence
9849             (substitute-command-keys
9850              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9851              ))))
9852          'undefined)
9853         (gnus-error 1 "Undefined key")
9854       (save-excursion
9855         (while articles
9856           (gnus-summary-goto-subject (setq article (pop articles)))
9857           (command-execute func)
9858           (gnus-summary-remove-process-mark article)))))
9859   (gnus-summary-position-point))
9860
9861 (defun gnus-summary-toggle-truncation (&optional arg)
9862   "Toggle truncation of summary lines.
9863 With arg, turn line truncation on iff arg is positive."
9864   (interactive "P")
9865   (setq truncate-lines
9866         (if (null arg) (not truncate-lines)
9867           (> (prefix-numeric-value arg) 0)))
9868   (redraw-display))
9869
9870 (defun gnus-summary-reselect-current-group (&optional all rescan)
9871   "Exit and then reselect the current newsgroup.
9872 The prefix argument ALL means to select all articles."
9873   (interactive "P")
9874   (gnus-set-global-variables)
9875   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
9876     (error "Ephemeral groups can't be reselected"))
9877   (let ((current-subject (gnus-summary-article-number))
9878         (group gnus-newsgroup-name))
9879     (setq gnus-newsgroup-begin nil)
9880     (gnus-summary-exit)
9881     ;; We have to adjust the point of group mode buffer because the
9882     ;; current point was moved to the next unread newsgroup by
9883     ;; exiting.
9884     (gnus-summary-jump-to-group group)
9885     (when rescan
9886       (save-excursion
9887         (gnus-group-get-new-news-this-group 1)))
9888     (gnus-group-read-group all t)
9889     (gnus-summary-goto-subject current-subject)))
9890
9891 (defun gnus-summary-rescan-group (&optional all)
9892   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9893   (interactive "P")
9894   (gnus-summary-reselect-current-group all t))
9895
9896 (defun gnus-summary-update-info ()
9897   (let* ((group gnus-newsgroup-name))
9898     (when gnus-newsgroup-kill-headers
9899       (setq gnus-newsgroup-killed
9900             (gnus-compress-sequence
9901              (nconc
9902               (gnus-set-sorted-intersection
9903                (gnus-uncompress-range gnus-newsgroup-killed)
9904                (setq gnus-newsgroup-unselected
9905                      (sort gnus-newsgroup-unselected '<)))
9906               (setq gnus-newsgroup-unreads
9907                     (sort gnus-newsgroup-unreads '<))) t)))
9908     (unless (listp (cdr gnus-newsgroup-killed))
9909       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9910     (let ((headers gnus-newsgroup-headers))
9911       (run-hooks 'gnus-exit-group-hook)
9912       (unless gnus-save-score
9913         (setq gnus-newsgroup-scored nil))
9914       ;; Set the new ranges of read articles.
9915       (gnus-update-read-articles
9916        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9917       ;; Set the current article marks.
9918       (gnus-update-marks)
9919       ;; Do the cross-ref thing.
9920       (when gnus-use-cross-reference
9921         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9922       ;; Do adaptive scoring, and possibly save score files.
9923       (when gnus-newsgroup-adaptive
9924         (gnus-score-adaptive))
9925       (when gnus-use-scoring
9926         (gnus-score-save))
9927       ;; Do not switch windows but change the buffer to work.
9928       (set-buffer gnus-group-buffer)
9929       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9930           (gnus-group-update-group group)))))
9931
9932 (defun gnus-summary-exit (&optional temporary)
9933   "Exit reading current newsgroup, and then return to group selection mode.
9934 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9935   (interactive)
9936   (gnus-set-global-variables)
9937   (gnus-kill-save-kill-buffer)
9938   (let* ((group gnus-newsgroup-name)
9939          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9940          (mode major-mode)
9941          (buf (current-buffer)))
9942     (run-hooks 'gnus-summary-prepare-exit-hook)
9943     ;; If we have several article buffers, we kill them at exit.
9944     (unless gnus-single-article-buffer
9945       (gnus-kill-buffer gnus-original-article-buffer)
9946       (setq gnus-article-current nil))
9947     (when gnus-use-cache
9948       (gnus-cache-possibly-remove-articles)
9949       (gnus-cache-save-buffers))
9950     (when gnus-use-trees
9951       (gnus-tree-close group))
9952     ;; Make all changes in this group permanent.
9953     (unless quit-config
9954       (gnus-summary-update-info))
9955     (gnus-close-group group)
9956     ;; Make sure where I was, and go to next newsgroup.
9957     (set-buffer gnus-group-buffer)
9958     (unless quit-config
9959       (gnus-group-jump-to-group group))
9960     (run-hooks 'gnus-summary-exit-hook)
9961     (unless quit-config
9962       (gnus-group-next-unread-group 1))
9963     (if temporary
9964         nil                             ;Nothing to do.
9965       ;; If we have several article buffers, we kill them at exit.
9966       (unless gnus-single-article-buffer
9967         (gnus-kill-buffer gnus-article-buffer)
9968         (gnus-kill-buffer gnus-original-article-buffer)
9969         (setq gnus-article-current nil))
9970       (set-buffer buf)
9971       (if (not gnus-kill-summary-on-exit)
9972           (gnus-deaden-summary)
9973         ;; We set all buffer-local variables to nil.  It is unclear why
9974         ;; this is needed, but if we don't, buffer-local variables are
9975         ;; not garbage-collected, it seems.  This would the lead to en
9976         ;; ever-growing Emacs.
9977         (gnus-summary-clear-local-variables)
9978         (when (get-buffer gnus-article-buffer)
9979           (bury-buffer gnus-article-buffer))
9980         ;; We clear the global counterparts of the buffer-local
9981         ;; variables as well, just to be on the safe side.
9982         (gnus-configure-windows 'group 'force)
9983         (gnus-summary-clear-local-variables)
9984         ;; Return to group mode buffer.
9985         (if (eq mode 'gnus-summary-mode)
9986             (gnus-kill-buffer buf)))
9987       (setq gnus-current-select-method gnus-select-method)
9988       (pop-to-buffer gnus-group-buffer)
9989       ;; Clear the current group name.
9990       (if (not quit-config)
9991           (progn
9992             (gnus-group-jump-to-group group)
9993             (gnus-group-next-unread-group 1)
9994             (gnus-configure-windows 'group 'force))
9995         (if (not (buffer-name (car quit-config)))
9996             (gnus-configure-windows 'group 'force)
9997           (set-buffer (car quit-config))
9998           (and (eq major-mode 'gnus-summary-mode)
9999                (gnus-set-global-variables))
10000           (gnus-configure-windows (cdr quit-config))))
10001       (unless quit-config
10002         (setq gnus-newsgroup-name nil)))))
10003
10004 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
10005 (defun gnus-summary-exit-no-update (&optional no-questions)
10006   "Quit reading current newsgroup without updating read article info."
10007   (interactive)
10008   (gnus-set-global-variables)
10009   (let* ((group gnus-newsgroup-name)
10010          (quit-config (gnus-group-quit-config group)))
10011     (when (or no-questions
10012               gnus-expert-user
10013               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
10014       ;; If we have several article buffers, we kill them at exit.
10015       (unless gnus-single-article-buffer
10016         (gnus-kill-buffer gnus-article-buffer)
10017         (gnus-kill-buffer gnus-original-article-buffer)
10018         (setq gnus-article-current nil))
10019       (if (not gnus-kill-summary-on-exit)
10020           (gnus-deaden-summary)
10021         (gnus-close-group group)
10022         (gnus-summary-clear-local-variables)
10023         (set-buffer gnus-group-buffer)
10024         (gnus-summary-clear-local-variables)
10025         (when (get-buffer gnus-summary-buffer)
10026           (kill-buffer gnus-summary-buffer)))
10027       (unless gnus-single-article-buffer
10028         (setq gnus-article-current nil))
10029       (when gnus-use-trees
10030         (gnus-tree-close group))
10031       (when (get-buffer gnus-article-buffer)
10032         (bury-buffer gnus-article-buffer))
10033       ;; Return to the group buffer.
10034       (gnus-configure-windows 'group 'force)
10035       ;; Clear the current group name.
10036       (setq gnus-newsgroup-name nil)
10037       (when (equal (gnus-group-group-name) group)
10038         (gnus-group-next-unread-group 1))
10039       (when quit-config
10040         (if (not (buffer-name (car quit-config)))
10041             (gnus-configure-windows 'group 'force)
10042           (set-buffer (car quit-config))
10043           (when (eq major-mode 'gnus-summary-mode)
10044             (gnus-set-global-variables))
10045           (gnus-configure-windows (cdr quit-config)))))))
10046
10047 ;;; Dead summaries.
10048
10049 (defvar gnus-dead-summary-mode-map nil)
10050
10051 (if gnus-dead-summary-mode-map
10052     nil
10053   (setq gnus-dead-summary-mode-map (make-keymap))
10054   (suppress-keymap gnus-dead-summary-mode-map)
10055   (substitute-key-definition
10056    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
10057   (let ((keys '("\C-d" "\r" "\177")))
10058     (while keys
10059       (define-key gnus-dead-summary-mode-map
10060         (pop keys) 'gnus-summary-wake-up-the-dead))))
10061
10062 (defvar gnus-dead-summary-mode nil
10063   "Minor mode for Gnus summary buffers.")
10064
10065 (defun gnus-dead-summary-mode (&optional arg)
10066   "Minor mode for Gnus summary buffers."
10067   (interactive "P")
10068   (when (eq major-mode 'gnus-summary-mode)
10069     (make-local-variable 'gnus-dead-summary-mode)
10070     (setq gnus-dead-summary-mode
10071           (if (null arg) (not gnus-dead-summary-mode)
10072             (> (prefix-numeric-value arg) 0)))
10073     (when gnus-dead-summary-mode
10074       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
10075         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
10076       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
10077         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
10078               minor-mode-map-alist)))))
10079
10080 (defun gnus-deaden-summary ()
10081   "Make the current summary buffer into a dead summary buffer."
10082   ;; Kill any previous dead summary buffer.
10083   (when (and gnus-dead-summary
10084              (buffer-name gnus-dead-summary))
10085     (save-excursion
10086       (set-buffer gnus-dead-summary)
10087       (when gnus-dead-summary-mode
10088         (kill-buffer (current-buffer)))))
10089   ;; Make this the current dead summary.
10090   (setq gnus-dead-summary (current-buffer))
10091   (gnus-dead-summary-mode 1)
10092   (let ((name (buffer-name)))
10093     (when (string-match "Summary" name)
10094       (rename-buffer
10095        (concat (substring name 0 (match-beginning 0)) "Dead "
10096                (substring name (match-beginning 0))) t))))
10097
10098 (defun gnus-kill-or-deaden-summary (buffer)
10099   "Kill or deaden the summary BUFFER."
10100   (when (and (buffer-name buffer)
10101              (not gnus-single-article-buffer))
10102     (save-excursion
10103       (set-buffer buffer)
10104       (gnus-kill-buffer gnus-article-buffer)
10105       (gnus-kill-buffer gnus-original-article-buffer)))
10106   (cond (gnus-kill-summary-on-exit
10107          (when (and gnus-use-trees
10108                     (and (get-buffer buffer)
10109                          (buffer-name (get-buffer buffer))))
10110            (save-excursion
10111              (set-buffer (get-buffer buffer))
10112              (gnus-tree-close gnus-newsgroup-name)))
10113          (gnus-kill-buffer buffer))
10114         ((and (get-buffer buffer)
10115               (buffer-name (get-buffer buffer)))
10116          (save-excursion
10117            (set-buffer buffer)
10118            (gnus-deaden-summary)))))
10119
10120 (defun gnus-summary-wake-up-the-dead (&rest args)
10121   "Wake up the dead summary buffer."
10122   (interactive)
10123   (gnus-dead-summary-mode -1)
10124   (let ((name (buffer-name)))
10125     (when (string-match "Dead " name)
10126       (rename-buffer
10127        (concat (substring name 0 (match-beginning 0))
10128                (substring name (match-end 0))) t)))
10129   (gnus-message 3 "This dead summary is now alive again"))
10130
10131 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
10132 (defun gnus-summary-fetch-faq (&optional faq-dir)
10133   "Fetch the FAQ for the current group.
10134 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
10135 in."
10136   (interactive
10137    (list
10138     (if current-prefix-arg
10139         (completing-read
10140          "Faq dir: " (and (listp gnus-group-faq-directory)
10141                           gnus-group-faq-directory)))))
10142   (let (gnus-faq-buffer)
10143     (and (setq gnus-faq-buffer
10144                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
10145          (gnus-configure-windows 'summary-faq))))
10146
10147 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
10148 (defun gnus-summary-describe-group (&optional force)
10149   "Describe the current newsgroup."
10150   (interactive "P")
10151   (gnus-group-describe-group force gnus-newsgroup-name))
10152
10153 (defun gnus-summary-describe-briefly ()
10154   "Describe summary mode commands briefly."
10155   (interactive)
10156   (gnus-message 6
10157                 (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")))
10158
10159 ;; Walking around group mode buffer from summary mode.
10160
10161 (defun gnus-summary-next-group (&optional no-article target-group backward)
10162   "Exit current newsgroup and then select next unread newsgroup.
10163 If prefix argument NO-ARTICLE is non-nil, no article is selected
10164 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
10165 previous group instead."
10166   (interactive "P")
10167   (gnus-set-global-variables)
10168   (let ((current-group gnus-newsgroup-name)
10169         (current-buffer (current-buffer))
10170         entered)
10171     ;; First we semi-exit this group to update Xrefs and all variables.
10172     ;; We can't do a real exit, because the window conf must remain
10173     ;; the same in case the user is prompted for info, and we don't
10174     ;; want the window conf to change before that...
10175     (gnus-summary-exit t)
10176     (while (not entered)
10177       ;; Then we find what group we are supposed to enter.
10178       (set-buffer gnus-group-buffer)
10179       (gnus-group-jump-to-group current-group)
10180       (setq target-group
10181             (or target-group
10182                 (if (eq gnus-keep-same-level 'best)
10183                     (gnus-summary-best-group gnus-newsgroup-name)
10184                   (gnus-summary-search-group backward gnus-keep-same-level))))
10185       (if (not target-group)
10186           ;; There are no further groups, so we return to the group
10187           ;; buffer.
10188           (progn
10189             (gnus-message 5 "Returning to the group buffer")
10190             (setq entered t)
10191             (set-buffer current-buffer)
10192             (gnus-summary-exit))
10193         ;; We try to enter the target group.
10194         (gnus-group-jump-to-group target-group)
10195         (let ((unreads (gnus-group-group-unread)))
10196           (if (and (or (eq t unreads)
10197                        (and unreads (not (zerop unreads))))
10198                    (gnus-summary-read-group
10199                     target-group nil no-article current-buffer))
10200               (setq entered t)
10201             (setq current-group target-group
10202                   target-group nil)))))))
10203
10204 (defun gnus-summary-prev-group (&optional no-article)
10205   "Exit current newsgroup and then select previous unread newsgroup.
10206 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
10207   (interactive "P")
10208   (gnus-summary-next-group no-article nil t))
10209
10210 ;; Walking around summary lines.
10211
10212 (defun gnus-summary-first-subject (&optional unread)
10213   "Go to the first unread subject.
10214 If UNREAD is non-nil, go to the first unread article.
10215 Returns the article selected or nil if there are no unread articles."
10216   (interactive "P")
10217   (prog1
10218       (cond
10219        ;; Empty summary.
10220        ((null gnus-newsgroup-data)
10221         (gnus-message 3 "No articles in the group")
10222         nil)
10223        ;; Pick the first article.
10224        ((not unread)
10225         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
10226         (gnus-data-number (car gnus-newsgroup-data)))
10227        ;; No unread articles.
10228        ((null gnus-newsgroup-unreads)
10229         (gnus-message 3 "No more unread articles")
10230         nil)
10231        ;; Find the first unread article.
10232        (t
10233         (let ((data gnus-newsgroup-data))
10234           (while (and data
10235                       (not (gnus-data-unread-p (car data))))
10236             (setq data (cdr data)))
10237           (if data
10238               (progn
10239                 (goto-char (gnus-data-pos (car data)))
10240                 (gnus-data-number (car data)))))))
10241     (gnus-summary-position-point)))
10242
10243 (defun gnus-summary-next-subject (n &optional unread dont-display)
10244   "Go to next N'th summary line.
10245 If N is negative, go to the previous N'th subject line.
10246 If UNREAD is non-nil, only unread articles are selected.
10247 The difference between N and the actual number of steps taken is
10248 returned."
10249   (interactive "p")
10250   (let ((backward (< n 0))
10251         (n (abs n)))
10252     (while (and (> n 0)
10253                 (if backward
10254                     (gnus-summary-find-prev unread)
10255                   (gnus-summary-find-next unread)))
10256       (setq n (1- n)))
10257     (if (/= 0 n) (gnus-message 7 "No more%s articles"
10258                                (if unread " unread" "")))
10259     (unless dont-display
10260       (gnus-summary-recenter)
10261       (gnus-summary-position-point))
10262     n))
10263
10264 (defun gnus-summary-next-unread-subject (n)
10265   "Go to next N'th unread summary line."
10266   (interactive "p")
10267   (gnus-summary-next-subject n t))
10268
10269 (defun gnus-summary-prev-subject (n &optional unread)
10270   "Go to previous N'th summary line.
10271 If optional argument UNREAD is non-nil, only unread article is selected."
10272   (interactive "p")
10273   (gnus-summary-next-subject (- n) unread))
10274
10275 (defun gnus-summary-prev-unread-subject (n)
10276   "Go to previous N'th unread summary line."
10277   (interactive "p")
10278   (gnus-summary-next-subject (- n) t))
10279
10280 (defun gnus-summary-goto-subject (article &optional force silent)
10281   "Go the subject line of ARTICLE.
10282 If FORCE, also allow jumping to articles not currently shown."
10283   (let ((b (point))
10284         (data (gnus-data-find article)))
10285     ;; We read in the article if we have to.
10286     (and (not data)
10287          force
10288          (gnus-summary-insert-subject article (and (vectorp force) force) t)
10289          (setq data (gnus-data-find article)))
10290     (goto-char b)
10291     (if (not data)
10292         (progn
10293           (unless silent
10294             (gnus-message 3 "Can't find article %d" article))
10295           nil)
10296       (goto-char (gnus-data-pos data))
10297       article)))
10298
10299 ;; Walking around summary lines with displaying articles.
10300
10301 (defun gnus-summary-expand-window (&optional arg)
10302   "Make the summary buffer take up the entire Emacs frame.
10303 Given a prefix, will force an `article' buffer configuration."
10304   (interactive "P")
10305   (gnus-set-global-variables)
10306   (if arg
10307       (gnus-configure-windows 'article 'force)
10308     (gnus-configure-windows 'summary 'force)))
10309
10310 (defun gnus-summary-display-article (article &optional all-header)
10311   "Display ARTICLE in article buffer."
10312   (gnus-set-global-variables)
10313   (if (null article)
10314       nil
10315     (prog1
10316         (if gnus-summary-display-article-function
10317             (funcall gnus-summary-display-article-function article all-header)
10318           (gnus-article-prepare article all-header))
10319       (run-hooks 'gnus-select-article-hook)
10320       (unless (zerop gnus-current-article)
10321         (gnus-summary-goto-subject gnus-current-article))
10322       (gnus-summary-recenter)
10323       (when gnus-use-trees
10324         (gnus-possibly-generate-tree article)
10325         (gnus-highlight-selected-tree article))
10326       ;; Successfully display article.
10327       (gnus-article-set-window-start
10328        (cdr (assq article gnus-newsgroup-bookmarks))))))
10329
10330 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
10331   "Select the current article.
10332 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
10333 non-nil, the article will be re-fetched even if it already present in
10334 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
10335 be displayed."
10336   ;; Make sure we are in the summary buffer to work around bbdb bug.
10337   (unless (eq major-mode 'gnus-summary-mode)
10338     (set-buffer gnus-summary-buffer))
10339   (let ((article (or article (gnus-summary-article-number)))
10340         (all-headers (not (not all-headers))) ;Must be T or NIL.
10341         gnus-summary-display-article-function
10342         did)
10343     (and (not pseudo)
10344          (gnus-summary-article-pseudo-p article)
10345          (error "This is a pseudo-article."))
10346     (prog1
10347         (save-excursion
10348           (set-buffer gnus-summary-buffer)
10349           (if (or (and gnus-single-article-buffer
10350                        (or (null gnus-current-article)
10351                            (null gnus-article-current)
10352                            (null (get-buffer gnus-article-buffer))
10353                            (not (eq article (cdr gnus-article-current)))
10354                            (not (equal (car gnus-article-current)
10355                                        gnus-newsgroup-name))))
10356                   (and (not gnus-single-article-buffer)
10357                        (or (null gnus-current-article)
10358                            (not (eq gnus-current-article article))))
10359                   force)
10360               ;; The requested article is different from the current article.
10361               (prog1
10362                   (gnus-summary-display-article article all-headers)
10363                 (setq did article))
10364             (if (or all-headers gnus-show-all-headers)
10365                 (gnus-article-show-all-headers))
10366             'old))
10367       (if did
10368           (gnus-article-set-window-start
10369            (cdr (assq article gnus-newsgroup-bookmarks)))))))
10370
10371 (defun gnus-summary-set-current-mark (&optional current-mark)
10372   "Obsolete function."
10373   nil)
10374
10375 (defun gnus-summary-next-article (&optional unread subject backward push)
10376   "Select the next article.
10377 If UNREAD, only unread articles are selected.
10378 If SUBJECT, only articles with SUBJECT are selected.
10379 If BACKWARD, the previous article is selected instead of the next."
10380   (interactive "P")
10381   (gnus-set-global-variables)
10382   (cond
10383    ;; Is there such an article?
10384    ((and (gnus-summary-search-forward unread subject backward)
10385          (or (gnus-summary-display-article (gnus-summary-article-number))
10386              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10387     (gnus-summary-position-point))
10388    ;; If not, we try the first unread, if that is wanted.
10389    ((and subject
10390          gnus-auto-select-same
10391          (or (gnus-summary-first-unread-article)
10392              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10393     (gnus-summary-position-point)
10394     (gnus-message 6 "Wrapped"))
10395    ;; Try to get next/previous article not displayed in this group.
10396    ((and gnus-auto-extend-newsgroup
10397          (not unread) (not subject))
10398     (gnus-summary-goto-article
10399      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10400      nil t))
10401    ;; Go to next/previous group.
10402    (t
10403     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10404         (gnus-summary-jump-to-group gnus-newsgroup-name))
10405     (let ((cmd last-command-char)
10406           (group
10407            (if (eq gnus-keep-same-level 'best)
10408                (gnus-summary-best-group gnus-newsgroup-name)
10409              (gnus-summary-search-group backward gnus-keep-same-level))))
10410       ;; For some reason, the group window gets selected.  We change
10411       ;; it back.
10412       (select-window (get-buffer-window (current-buffer)))
10413       ;; Select next unread newsgroup automagically.
10414       (cond
10415        ((or (not gnus-auto-select-next)
10416             (not cmd))
10417         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10418        ((or (eq gnus-auto-select-next 'quietly)
10419             (and (eq gnus-auto-select-next 'slightly-quietly)
10420                  push)
10421             (and (eq gnus-auto-select-next 'almost-quietly)
10422                  (gnus-summary-last-article-p)))
10423         ;; Select quietly.
10424         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10425             (gnus-summary-exit)
10426           (gnus-message 7 "No more%s articles (%s)..."
10427                         (if unread " unread" "")
10428                         (if group (concat "selecting " group)
10429                           "exiting"))
10430           (gnus-summary-next-group nil group backward)))
10431        (t
10432         (gnus-summary-walk-group-buffer
10433          gnus-newsgroup-name cmd unread backward)))))))
10434
10435 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10436   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10437                       (?\C-p (gnus-group-prev-unread-group 1))))
10438         keve key group ended)
10439     (save-excursion
10440       (set-buffer gnus-group-buffer)
10441       (gnus-summary-jump-to-group from-group)
10442       (setq group
10443             (if (eq gnus-keep-same-level 'best)
10444                 (gnus-summary-best-group gnus-newsgroup-name)
10445               (gnus-summary-search-group backward gnus-keep-same-level))))
10446     (while (not ended)
10447       (gnus-message
10448        5 "No more%s articles%s" (if unread " unread" "")
10449        (if (and group
10450                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10451            (format " (Type %s for %s [%s])"
10452                    (single-key-description cmd) group
10453                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10454          (format " (Type %s to exit %s)"
10455                  (single-key-description cmd)
10456                  gnus-newsgroup-name)))
10457       ;; Confirm auto selection.
10458       (setq key (car (setq keve (gnus-read-event-char))))
10459       (setq ended t)
10460       (cond
10461        ((assq key keystrokes)
10462         (let ((obuf (current-buffer)))
10463           (switch-to-buffer gnus-group-buffer)
10464           (and group
10465                (gnus-group-jump-to-group group))
10466           (eval (cadr (assq key keystrokes)))
10467           (setq group (gnus-group-group-name))
10468           (switch-to-buffer obuf))
10469         (setq ended nil))
10470        ((equal key cmd)
10471         (if (or (not group)
10472                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10473             (gnus-summary-exit)
10474           (gnus-summary-next-group nil group backward)))
10475        (t
10476         (push (cdr keve) unread-command-events))))))
10477
10478 (defun gnus-read-event-char ()
10479   "Get the next event."
10480   (let ((event (read-event)))
10481     (cons (and (numberp event) event) event)))
10482
10483 (defun gnus-summary-next-unread-article ()
10484   "Select unread article after current one."
10485   (interactive)
10486   (gnus-summary-next-article t (and gnus-auto-select-same
10487                                     (gnus-summary-article-subject))))
10488
10489 (defun gnus-summary-prev-article (&optional unread subject)
10490   "Select the article after the current one.
10491 If UNREAD is non-nil, only unread articles are selected."
10492   (interactive "P")
10493   (gnus-summary-next-article unread subject t))
10494
10495 (defun gnus-summary-prev-unread-article ()
10496   "Select unred article before current one."
10497   (interactive)
10498   (gnus-summary-prev-article t (and gnus-auto-select-same
10499                                     (gnus-summary-article-subject))))
10500
10501 (defun gnus-summary-next-page (&optional lines circular)
10502   "Show next page of the selected article.
10503 If at the end of the current article, select the next article.
10504 LINES says how many lines should be scrolled up.
10505
10506 If CIRCULAR is non-nil, go to the start of the article instead of
10507 selecting the next article when reaching the end of the current
10508 article."
10509   (interactive "P")
10510   (setq gnus-summary-buffer (current-buffer))
10511   (gnus-set-global-variables)
10512   (let ((article (gnus-summary-article-number))
10513         (endp nil))
10514     (gnus-configure-windows 'article)
10515     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
10516         (if (and (eq gnus-summary-goto-unread 'never)
10517                  (not (gnus-summary-last-article-p article)))
10518             (gnus-summary-next-article)
10519           (gnus-summary-next-unread-article))
10520       (if (or (null gnus-current-article)
10521               (null gnus-article-current)
10522               (/= article (cdr gnus-article-current))
10523               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10524           ;; Selected subject is different from current article's.
10525           (gnus-summary-display-article article)
10526         (gnus-eval-in-buffer-window gnus-article-buffer
10527           (setq endp (gnus-article-next-page lines)))
10528         (if endp
10529             (cond (circular
10530                    (gnus-summary-beginning-of-article))
10531                   (lines
10532                    (gnus-message 3 "End of message"))
10533                   ((null lines)
10534                    (if (and (eq gnus-summary-goto-unread 'never)
10535                             (not (gnus-summary-last-article-p article)))
10536                        (gnus-summary-next-article)
10537                      (gnus-summary-next-unread-article)))))))
10538     (gnus-summary-recenter)
10539     (gnus-summary-position-point)))
10540
10541 (defun gnus-summary-prev-page (&optional lines)
10542   "Show previous page of selected article.
10543 Argument LINES specifies lines to be scrolled down."
10544   (interactive "P")
10545   (gnus-set-global-variables)
10546   (let ((article (gnus-summary-article-number)))
10547     (gnus-configure-windows 'article)
10548     (if (or (null gnus-current-article)
10549             (null gnus-article-current)
10550             (/= article (cdr gnus-article-current))
10551             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10552         ;; Selected subject is different from current article's.
10553         (gnus-summary-display-article article)
10554       (gnus-summary-recenter)
10555       (gnus-eval-in-buffer-window gnus-article-buffer
10556         (gnus-article-prev-page lines))))
10557   (gnus-summary-position-point))
10558
10559 (defun gnus-summary-scroll-up (lines)
10560   "Scroll up (or down) one line current article.
10561 Argument LINES specifies lines to be scrolled up (or down if negative)."
10562   (interactive "p")
10563   (gnus-set-global-variables)
10564   (gnus-configure-windows 'article)
10565   (gnus-summary-show-thread)
10566   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10567     (gnus-eval-in-buffer-window gnus-article-buffer
10568       (cond ((> lines 0)
10569              (if (gnus-article-next-page lines)
10570                  (gnus-message 3 "End of message")))
10571             ((< lines 0)
10572              (gnus-article-prev-page (- lines))))))
10573   (gnus-summary-recenter)
10574   (gnus-summary-position-point))
10575
10576 (defun gnus-summary-next-same-subject ()
10577   "Select next article which has the same subject as current one."
10578   (interactive)
10579   (gnus-set-global-variables)
10580   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10581
10582 (defun gnus-summary-prev-same-subject ()
10583   "Select previous article which has the same subject as current one."
10584   (interactive)
10585   (gnus-set-global-variables)
10586   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10587
10588 (defun gnus-summary-next-unread-same-subject ()
10589   "Select next unread article which has the same subject as current one."
10590   (interactive)
10591   (gnus-set-global-variables)
10592   (gnus-summary-next-article t (gnus-summary-article-subject)))
10593
10594 (defun gnus-summary-prev-unread-same-subject ()
10595   "Select previous unread article which has the same subject as current one."
10596   (interactive)
10597   (gnus-set-global-variables)
10598   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10599
10600 (defun gnus-summary-first-unread-article ()
10601   "Select the first unread article.
10602 Return nil if there are no unread articles."
10603   (interactive)
10604   (gnus-set-global-variables)
10605   (prog1
10606       (if (gnus-summary-first-subject t)
10607           (progn
10608             (gnus-summary-show-thread)
10609             (gnus-summary-first-subject t)
10610             (gnus-summary-display-article (gnus-summary-article-number))))
10611     (gnus-summary-position-point)))
10612
10613 (defun gnus-summary-best-unread-article ()
10614   "Select the unread article with the highest score."
10615   (interactive)
10616   (gnus-set-global-variables)
10617   (let ((best -1000000)
10618         (data gnus-newsgroup-data)
10619         article score)
10620     (while data
10621       (and (gnus-data-unread-p (car data))
10622            (> (setq score
10623                     (gnus-summary-article-score (gnus-data-number (car data))))
10624               best)
10625            (setq best score
10626                  article (gnus-data-number (car data))))
10627       (setq data (cdr data)))
10628     (prog1
10629         (if article
10630             (gnus-summary-goto-article article)
10631           (error "No unread articles"))
10632       (gnus-summary-position-point))))
10633
10634 (defun gnus-summary-last-subject ()
10635   "Go to the last displayed subject line in the group."
10636   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10637     (when article
10638       (gnus-summary-goto-subject article))))
10639
10640 (defun gnus-summary-goto-article (article &optional all-headers force)
10641   "Fetch ARTICLE and display it if it exists.
10642 If ALL-HEADERS is non-nil, no header lines are hidden."
10643   (interactive
10644    (list
10645     (string-to-int
10646      (completing-read
10647       "Article number: "
10648       (mapcar (lambda (number) (list (int-to-string number)))
10649               gnus-newsgroup-limit)))
10650     current-prefix-arg
10651     t))
10652   (prog1
10653       (if (gnus-summary-goto-subject article force)
10654           (gnus-summary-display-article article all-headers)
10655         (gnus-message 4 "Couldn't go to article %s" article) nil)
10656     (gnus-summary-position-point)))
10657
10658 (defun gnus-summary-goto-last-article ()
10659   "Go to the previously read article."
10660   (interactive)
10661   (prog1
10662       (and gnus-last-article
10663            (gnus-summary-goto-article gnus-last-article))
10664     (gnus-summary-position-point)))
10665
10666 (defun gnus-summary-pop-article (number)
10667   "Pop one article off the history and go to the previous.
10668 NUMBER articles will be popped off."
10669   (interactive "p")
10670   (let (to)
10671     (setq gnus-newsgroup-history
10672           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10673     (if to
10674         (gnus-summary-goto-article (car to))
10675       (error "Article history empty")))
10676   (gnus-summary-position-point))
10677
10678 ;; Summary commands and functions for limiting the summary buffer.
10679
10680 (defun gnus-summary-limit-to-articles (n)
10681   "Limit the summary buffer to the next N articles.
10682 If not given a prefix, use the process marked articles instead."
10683   (interactive "P")
10684   (gnus-set-global-variables)
10685   (prog1
10686       (let ((articles (gnus-summary-work-articles n)))
10687         (setq gnus-newsgroup-processable nil)
10688         (gnus-summary-limit articles))
10689     (gnus-summary-position-point)))
10690
10691 (defun gnus-summary-pop-limit (&optional total)
10692   "Restore the previous limit.
10693 If given a prefix, remove all limits."
10694   (interactive "P")
10695   (gnus-set-global-variables)
10696   (when total 
10697     (setq gnus-newsgroup-limits
10698           (list (mapcar (lambda (h) (mail-header-number h))
10699                         gnus-newsgroup-headers))))
10700   (unless gnus-newsgroup-limits
10701     (error "No limit to pop"))
10702   (prog1
10703       (gnus-summary-limit nil 'pop)
10704     (gnus-summary-position-point)))
10705
10706 (defun gnus-summary-limit-to-subject (subject &optional header)
10707   "Limit the summary buffer to articles that have subjects that match a regexp."
10708   (interactive "sRegexp: ")
10709   (unless header
10710     (setq header "subject"))
10711   (when (not (equal "" subject))
10712     (prog1
10713         (let ((articles (gnus-summary-find-matching
10714                          (or header "subject") subject 'all)))
10715           (or articles (error "Found no matches for \"%s\"" subject))
10716           (gnus-summary-limit articles))
10717       (gnus-summary-position-point))))
10718
10719 (defun gnus-summary-limit-to-author (from)
10720   "Limit the summary buffer to articles that have authors that match a regexp."
10721   (interactive "sRegexp: ")
10722   (gnus-summary-limit-to-subject from "from"))
10723
10724 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10725 (make-obsolete
10726  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10727
10728 (defun gnus-summary-limit-to-unread (&optional all)
10729   "Limit the summary buffer to articles that are not marked as read.
10730 If ALL is non-nil, limit strictly to unread articles."
10731   (interactive "P")
10732   (if all
10733       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10734     (gnus-summary-limit-to-marks
10735      ;; Concat all the marks that say that an article is read and have
10736      ;; those removed.
10737      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10738            gnus-killed-mark gnus-kill-file-mark
10739            gnus-low-score-mark gnus-expirable-mark
10740            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10741      'reverse)))
10742
10743 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10744 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10745
10746 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10747   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10748 If REVERSE, limit the summary buffer to articles that are not marked
10749 with MARKS.  MARKS can either be a string of marks or a list of marks.
10750 Returns how many articles were removed."
10751   (interactive "sMarks: ")
10752   (gnus-set-global-variables)
10753   (prog1
10754       (let ((data gnus-newsgroup-data)
10755             (marks (if (listp marks) marks
10756                      (append marks nil))) ; Transform to list.
10757             articles)
10758         (while data
10759           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10760                  (memq (gnus-data-mark (car data)) marks))
10761                (setq articles (cons (gnus-data-number (car data)) articles)))
10762           (setq data (cdr data)))
10763         (gnus-summary-limit articles))
10764     (gnus-summary-position-point)))
10765
10766 (defun gnus-summary-limit-to-score (&optional score)
10767   "Limit to articles with score at or above SCORE."
10768   (interactive "P")
10769   (gnus-set-global-variables)
10770   (setq score (if score
10771                   (prefix-numeric-value score)
10772                 (or gnus-summary-default-score 0)))
10773   (let ((data gnus-newsgroup-data)
10774         articles)
10775     (while data
10776       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10777                 score)
10778         (push (gnus-data-number (car data)) articles))
10779       (setq data (cdr data)))
10780     (prog1
10781         (gnus-summary-limit articles)
10782       (gnus-summary-position-point))))
10783
10784 (defun gnus-summary-limit-include-dormant ()
10785   "Display all the hidden articles that are marked as dormant."
10786   (interactive)
10787   (gnus-set-global-variables)
10788   (or gnus-newsgroup-dormant
10789       (error "There are no dormant articles in this group"))
10790   (prog1
10791       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10792     (gnus-summary-position-point)))
10793
10794 (defun gnus-summary-limit-exclude-dormant ()
10795   "Hide all dormant articles."
10796   (interactive)
10797   (gnus-set-global-variables)
10798   (prog1
10799       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10800     (gnus-summary-position-point)))
10801
10802 (defun gnus-summary-limit-exclude-childless-dormant ()
10803   "Hide all dormant articles that have no children."
10804   (interactive)
10805   (gnus-set-global-variables)
10806   (let ((data (gnus-data-list t))
10807         articles d children)
10808     ;; Find all articles that are either not dormant or have
10809     ;; children.
10810     (while (setq d (pop data))
10811       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10812                 (and (setq children 
10813                            (gnus-article-children (gnus-data-number d)))
10814                      (let (found)
10815                        (while children
10816                          (when (memq (car children) articles)
10817                            (setq children nil
10818                                  found t))
10819                          (pop children))
10820                        found)))
10821         (push (gnus-data-number d) articles)))
10822     ;; Do the limiting.
10823     (prog1
10824         (gnus-summary-limit articles)
10825       (gnus-summary-position-point))))
10826
10827 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10828   "Mark all unread excluded articles as read.
10829 If ALL, mark even excluded ticked and dormants as read."
10830   (interactive "P")
10831   (let ((articles (gnus-sorted-complement
10832                    (sort
10833                     (mapcar (lambda (h) (mail-header-number h))
10834                             gnus-newsgroup-headers)
10835                     '<)
10836                    (sort gnus-newsgroup-limit '<)))
10837         article)
10838     (setq gnus-newsgroup-unreads nil)
10839     (if all
10840         (setq gnus-newsgroup-dormant nil
10841               gnus-newsgroup-marked nil
10842               gnus-newsgroup-reads
10843               (nconc
10844                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10845                gnus-newsgroup-reads))
10846       (while (setq article (pop articles))
10847         (unless (or (memq article gnus-newsgroup-dormant)
10848                     (memq article gnus-newsgroup-marked))
10849           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10850
10851 (defun gnus-summary-limit (articles &optional pop)
10852   (if pop
10853       ;; We pop the previous limit off the stack and use that.
10854       (setq articles (car gnus-newsgroup-limits)
10855             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10856     ;; We use the new limit, so we push the old limit on the stack.
10857     (setq gnus-newsgroup-limits
10858           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10859   ;; Set the limit.
10860   (setq gnus-newsgroup-limit articles)
10861   (let ((total (length gnus-newsgroup-data))
10862         (data (gnus-data-find-list (gnus-summary-article-number)))
10863         found)
10864     ;; This will do all the work of generating the new summary buffer
10865     ;; according to the new limit.
10866     (gnus-summary-prepare)
10867     ;; Hide any threads, possibly.
10868     (and gnus-show-threads
10869          gnus-thread-hide-subtree
10870          (gnus-summary-hide-all-threads))
10871     ;; Try to return to the article you were at, or one in the
10872     ;; neighborhood.
10873     (if data
10874         ;; We try to find some article after the current one.
10875         (while data
10876           (and (gnus-summary-goto-subject
10877                 (gnus-data-number (car data)) nil t)
10878                (setq data nil
10879                      found t))
10880           (setq data (cdr data))))
10881     (or found
10882         ;; If there is no data, that means that we were after the last
10883         ;; article.  The same goes when we can't find any articles
10884         ;; after the current one.
10885         (progn
10886           (goto-char (point-max))
10887           (gnus-summary-find-prev)))
10888     ;; We return how many articles were removed from the summary
10889     ;; buffer as a result of the new limit.
10890     (- total (length gnus-newsgroup-data))))
10891
10892 (defsubst gnus-invisible-cut-children (threads)
10893   (let ((num 0))
10894     (while threads
10895       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
10896         (incf num))
10897       (pop threads))
10898     (< num 2)))
10899
10900 (defsubst gnus-cut-thread (thread)
10901   "Go forwards in the thread until we find an article that we want to display."
10902   (when (or (eq gnus-fetch-old-headers 'some)
10903             (eq gnus-build-sparse-threads 'some)
10904             (eq gnus-build-sparse-threads 'more))
10905     ;; Deal with old-fetched headers and sparse threads.
10906     (while (and
10907             thread
10908             (or
10909              (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10910              (memq (mail-header-number (car thread)) gnus-newsgroup-ancient))
10911             (or (<= (length (cdr thread)) 1)
10912                 (gnus-invisible-cut-children (cdr thread))))
10913       (setq thread (cadr thread))))
10914   thread)
10915
10916 (defun gnus-cut-threads (threads)
10917   "Cut off all uninteresting articles from the beginning of threads."
10918   (when (or (eq gnus-fetch-old-headers 'some)
10919             (eq gnus-build-sparse-threads 'some)
10920             (eq gnus-build-sparse-threads 'more))
10921     (let ((th threads))
10922       (while th
10923         (setcar th (gnus-cut-thread (car th)))
10924         (setq th (cdr th)))))
10925   ;; Remove nixed out threads.
10926   (delq nil threads))
10927
10928 (defun gnus-summary-initial-limit (&optional show-if-empty)
10929   "Figure out what the initial limit is supposed to be on group entry.
10930 This entails weeding out unwanted dormants, low-scored articles,
10931 fetch-old-headers verbiage, and so on."
10932   ;; Most groups have nothing to remove.
10933   (if (or gnus-inhibit-limiting
10934           (and (null gnus-newsgroup-dormant)
10935                (not (eq gnus-fetch-old-headers 'some))
10936                (null gnus-summary-expunge-below)
10937                (not (eq gnus-build-sparse-threads 'some))
10938                (not (eq gnus-build-sparse-threads 'more))
10939                (null gnus-thread-expunge-below)
10940                (not gnus-use-nocem)))
10941       () ; Do nothing.
10942     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10943     (setq gnus-newsgroup-limit nil)
10944     (mapatoms
10945      (lambda (node)
10946        (unless (car (symbol-value node))
10947          ;; These threads have no parents -- they are roots.
10948          (let ((nodes (cdr (symbol-value node)))
10949                thread)
10950            (while nodes
10951              (if (and gnus-thread-expunge-below
10952                       (< (gnus-thread-total-score (car nodes))
10953                          gnus-thread-expunge-below))
10954                  (gnus-expunge-thread (pop nodes))
10955                (setq thread (pop nodes))
10956                (gnus-summary-limit-children thread))))))
10957      gnus-newsgroup-dependencies)
10958     ;; If this limitation resulted in an empty group, we might
10959     ;; pop the previous limit and use it instead.
10960     (when (and (not gnus-newsgroup-limit)
10961                show-if-empty)
10962       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10963     gnus-newsgroup-limit))
10964
10965 (defun gnus-summary-limit-children (thread)
10966   "Return 1 if this subthread is visible and 0 if it is not."
10967   ;; First we get the number of visible children to this thread.  This
10968   ;; is done by recursing down the thread using this function, so this
10969   ;; will really go down to a leaf article first, before slowly
10970   ;; working its way up towards the root.
10971   (when thread
10972     (let ((children
10973            (if (cdr thread)
10974                (apply '+ (mapcar 'gnus-summary-limit-children
10975                                  (cdr thread)))
10976              0))
10977           (number (mail-header-number (car thread)))
10978           score)
10979       (if (or
10980            ;; If this article is dormant and has absolutely no visible
10981            ;; children, then this article isn't visible.
10982            (and (memq number gnus-newsgroup-dormant)
10983                 (= children 0))
10984            ;; If this is "fetch-old-headered" and there is only one
10985            ;; visible child (or less), then we don't want this article.
10986            (and (eq gnus-fetch-old-headers 'some)
10987                 (memq number gnus-newsgroup-ancient)
10988                 (zerop children))
10989            ;; If this is a sparsely inserted article with no children,
10990            ;; we don't want it.
10991            (and (eq gnus-build-sparse-threads 'some)
10992                 (memq number gnus-newsgroup-sparse)
10993                 (zerop children))
10994            ;; If we use expunging, and this article is really
10995            ;; low-scored, then we don't want this article.
10996            (when (and gnus-summary-expunge-below
10997                       (< (setq score
10998                                (or (cdr (assq number gnus-newsgroup-scored))
10999                                    gnus-summary-default-score))
11000                          gnus-summary-expunge-below))
11001              ;; We increase the expunge-tally here, but that has
11002              ;; nothing to do with the limits, really.
11003              (incf gnus-newsgroup-expunged-tally)
11004              ;; We also mark as read here, if that's wanted.
11005              (when (and gnus-summary-mark-below
11006                         (< score gnus-summary-mark-below))
11007                (setq gnus-newsgroup-unreads
11008                      (delq number gnus-newsgroup-unreads))
11009                (if gnus-newsgroup-auto-expire
11010                    (push number gnus-newsgroup-expirable)
11011                  (push (cons number gnus-low-score-mark)
11012                        gnus-newsgroup-reads)))
11013              t)
11014            (and gnus-use-nocem
11015                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
11016           ;; Nope, invisible article.
11017           0
11018         ;; Ok, this article is to be visible, so we add it to the limit
11019         ;; and return 1.
11020         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
11021         1))))
11022
11023 (defun gnus-expunge-thread (thread)
11024   "Mark all articles in THREAD as read."
11025   (let* ((number (mail-header-number (car thread))))
11026     (incf gnus-newsgroup-expunged-tally)
11027     ;; We also mark as read here, if that's wanted.
11028     (setq gnus-newsgroup-unreads
11029           (delq number gnus-newsgroup-unreads))
11030     (if gnus-newsgroup-auto-expire
11031         (push number gnus-newsgroup-expirable)
11032       (push (cons number gnus-low-score-mark)
11033             gnus-newsgroup-reads)))
11034   ;; Go recursively through all subthreads.
11035   (mapcar 'gnus-expunge-thread (cdr thread)))
11036
11037 ;; Summary article oriented commands
11038
11039 (defun gnus-summary-refer-parent-article (n)
11040   "Refer parent article N times.
11041 The difference between N and the number of articles fetched is returned."
11042   (interactive "p")
11043   (gnus-set-global-variables)
11044   (while
11045       (and
11046        (> n 0)
11047        (let* ((header (gnus-summary-article-header))
11048               (ref
11049                ;; If we try to find the parent of the currently
11050                ;; displayed article, then we take a look at the actual
11051                ;; References header, since this is slightly more
11052                ;; reliable than the References field we got from the
11053                ;; server.
11054                (if (and (eq (mail-header-number header)
11055                             (cdr gnus-article-current))
11056                         (equal gnus-newsgroup-name
11057                                (car gnus-article-current)))
11058                    (save-excursion
11059                      (set-buffer gnus-original-article-buffer)
11060                      (nnheader-narrow-to-headers)
11061                      (prog1
11062                          (message-fetch-field "references")
11063                        (widen)))
11064                  ;; It's not the current article, so we take a bet on
11065                  ;; the value we got from the server.
11066                  (mail-header-references header))))
11067          (if (setq ref (or ref (mail-header-references header)))
11068              (or (gnus-summary-refer-article (gnus-parent-id ref))
11069                  (gnus-message 1 "Couldn't find parent"))
11070            (gnus-message 1 "No references in article %d"
11071                          (gnus-summary-article-number))
11072            nil)))
11073     (setq n (1- n)))
11074   (gnus-summary-position-point)
11075   n)
11076
11077 (defun gnus-summary-refer-references ()
11078   "Fetch all articles mentioned in the References header.
11079 Return how many articles were fetched."
11080   (interactive)
11081   (gnus-set-global-variables)
11082   (let ((ref (mail-header-references (gnus-summary-article-header)))
11083         (current (gnus-summary-article-number))
11084         (n 0))
11085     ;; For each Message-ID in the References header...
11086     (while (string-match "<[^>]*>" ref)
11087       (incf n)
11088       ;; ... fetch that article.
11089       (gnus-summary-refer-article
11090        (prog1 (match-string 0 ref)
11091          (setq ref (substring ref (match-end 0))))))
11092     (gnus-summary-goto-subject current)
11093     (gnus-summary-position-point)
11094     n))
11095
11096 (defun gnus-summary-refer-article (message-id)
11097   "Fetch an article specified by MESSAGE-ID."
11098   (interactive "sMessage-ID: ")
11099   (when (and (stringp message-id)
11100              (not (zerop (length message-id))))
11101     ;; Construct the correct Message-ID if necessary.
11102     ;; Suggested by tale@pawl.rpi.edu.
11103     (unless (string-match "^<" message-id)
11104       (setq message-id (concat "<" message-id)))
11105     (unless (string-match ">$" message-id)
11106       (setq message-id (concat message-id ">")))
11107     (let* ((header (gnus-id-to-header message-id))
11108            (sparse (and header
11109                         (memq (mail-header-number header)
11110                               gnus-newsgroup-sparse))))
11111       (if header
11112           (prog1
11113               ;; The article is present in the buffer, to we just go to it.
11114               (gnus-summary-goto-article 
11115                (mail-header-number header) nil header)
11116             (when sparse
11117               (gnus-summary-update-article (mail-header-number header))))
11118         ;; We fetch the article
11119         (let ((gnus-override-method 
11120                (and (gnus-news-group-p gnus-newsgroup-name)
11121                     gnus-refer-article-method))
11122               number)
11123           ;; Start the special refer-article method, if necessary.
11124           (when (and gnus-refer-article-method
11125                      (gnus-news-group-p gnus-newsgroup-name))
11126             (gnus-check-server gnus-refer-article-method))
11127           ;; Fetch the header, and display the article.
11128           (if (setq number (gnus-summary-insert-subject message-id))
11129               (gnus-summary-select-article nil nil nil number)
11130             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
11131
11132 (defun gnus-summary-enter-digest-group (&optional force)
11133   "Enter a digest group based on the current article."
11134   (interactive "P")
11135   (gnus-set-global-variables)
11136   (gnus-summary-select-article)
11137   (let ((name (format "%s-%d"
11138                       (gnus-group-prefixed-name
11139                        gnus-newsgroup-name (list 'nndoc ""))
11140                       gnus-current-article))
11141         (ogroup gnus-newsgroup-name)
11142         (case-fold-search t)
11143         (buf (current-buffer))
11144         dig)
11145     (save-excursion
11146       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
11147       (insert-buffer-substring gnus-original-article-buffer)
11148       (narrow-to-region
11149        (goto-char (point-min))
11150        (or (search-forward "\n\n" nil t) (point)))
11151       (goto-char (point-min))
11152       (delete-matching-lines "^\\(Path\\):\\|^From ")
11153       (widen))
11154     (unwind-protect
11155         (if (gnus-group-read-ephemeral-group
11156              name `(nndoc ,name (nndoc-address
11157                                  ,(get-buffer dig))
11158                           (nndoc-article-type ,(if force 'digest 'guess))) t)
11159             ;; Make all postings to this group go to the parent group.
11160             (nconc (gnus-info-params (gnus-get-info name))
11161                    (list (cons 'to-group ogroup)))
11162           ;; Couldn't select this doc group.
11163           (switch-to-buffer buf)
11164           (gnus-set-global-variables)
11165           (gnus-configure-windows 'summary)
11166           (gnus-message 3 "Article couldn't be entered?"))
11167       (kill-buffer dig))))
11168
11169 (defun gnus-summary-isearch-article (&optional regexp-p)
11170   "Do incremental search forward on the current article.
11171 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
11172   (interactive "P")
11173   (gnus-set-global-variables)
11174   (gnus-summary-select-article)
11175   (gnus-configure-windows 'article)
11176   (gnus-eval-in-buffer-window gnus-article-buffer
11177     (goto-char (point-min))
11178     (isearch-forward regexp-p)))
11179
11180 (defun gnus-summary-search-article-forward (regexp &optional backward)
11181   "Search for an article containing REGEXP forward.
11182 If BACKWARD, search backward instead."
11183   (interactive
11184    (list (read-string
11185           (format "Search article %s (regexp%s): "
11186                   (if current-prefix-arg "backward" "forward")
11187                   (if gnus-last-search-regexp
11188                       (concat ", default " gnus-last-search-regexp)
11189                     "")))
11190          current-prefix-arg))
11191   (gnus-set-global-variables)
11192   (if (string-equal regexp "")
11193       (setq regexp (or gnus-last-search-regexp ""))
11194     (setq gnus-last-search-regexp regexp))
11195   (unless (gnus-summary-search-article regexp backward)
11196     (error "Search failed: \"%s\"" regexp)))
11197
11198 (defun gnus-summary-search-article-backward (regexp)
11199   "Search for an article containing REGEXP backward."
11200   (interactive
11201    (list (read-string
11202           (format "Search article backward (regexp%s): "
11203                   (if gnus-last-search-regexp
11204                       (concat ", default " gnus-last-search-regexp)
11205                     "")))))
11206   (gnus-summary-search-article-forward regexp 'backward))
11207
11208 (defun gnus-summary-search-article (regexp &optional backward)
11209   "Search for an article containing REGEXP.
11210 Optional argument BACKWARD means do search for backward.
11211 `gnus-select-article-hook' is not called during the search."
11212   (let ((gnus-select-article-hook nil)  ;Disable hook.
11213         (gnus-article-display-hook nil)
11214         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
11215         (re-search
11216          (if backward
11217              're-search-backward 're-search-forward))
11218         (sum (current-buffer))
11219         (found nil))
11220     (gnus-save-hidden-threads
11221       (gnus-summary-select-article)
11222       (set-buffer gnus-article-buffer)
11223       (when backward
11224         (forward-line -1))
11225       (while (not found)
11226         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
11227         (if (if backward
11228                 (re-search-backward regexp nil t)
11229               (re-search-forward regexp nil t))
11230             ;; We found the regexp.
11231             (progn
11232               (setq found 'found)
11233               (beginning-of-line)
11234               (set-window-start
11235                (get-buffer-window (current-buffer))
11236                (point))
11237               (forward-line 1)
11238               (set-buffer sum))
11239           ;; We didn't find it, so we go to the next article.
11240           (set-buffer sum)
11241           (if (not (if backward (gnus-summary-find-prev)
11242                      (gnus-summary-find-next)))
11243               ;; No more articles.
11244               (setq found t)
11245             ;; Select the next article and adjust point.
11246             (gnus-summary-select-article)
11247             (set-buffer gnus-article-buffer)
11248             (widen)
11249             (goto-char (if backward (point-max) (point-min))))))
11250       (gnus-message 7 ""))
11251     ;; Return whether we found the regexp.
11252     (when (eq found 'found)
11253       (gnus-summary-show-thread)
11254       (gnus-summary-goto-subject gnus-current-article)
11255       (gnus-summary-position-point)
11256       t)))
11257
11258 (defun gnus-summary-find-matching (header regexp &optional backward unread
11259                                           not-case-fold)
11260   "Return a list of all articles that match REGEXP on HEADER.
11261 The search stars on the current article and goes forwards unless
11262 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
11263 If UNREAD is non-nil, only unread articles will
11264 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
11265 in the comparisons."
11266   (let ((data (if (eq backward 'all) gnus-newsgroup-data
11267                 (gnus-data-find-list
11268                  (gnus-summary-article-number) (gnus-data-list backward))))
11269         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
11270         (case-fold-search (not not-case-fold))
11271         articles d)
11272     (or (fboundp (intern (concat "mail-header-" header)))
11273         (error "%s is not a valid header" header))
11274     (while data
11275       (setq d (car data))
11276       (and (or (not unread)             ; We want all articles...
11277                (gnus-data-unread-p d))  ; Or just unreads.
11278            (vectorp (gnus-data-header d)) ; It's not a pseudo.
11279            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
11280            (setq articles (cons (gnus-data-number d) articles))) ; Success!
11281       (setq data (cdr data)))
11282     (nreverse articles)))
11283
11284 (defun gnus-summary-execute-command (header regexp command &optional backward)
11285   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
11286 If HEADER is an empty string (or nil), the match is done on the entire
11287 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
11288   (interactive
11289    (list (let ((completion-ignore-case t))
11290            (completing-read
11291             "Header name: "
11292             (mapcar (lambda (string) (list string))
11293                     '("Number" "Subject" "From" "Lines" "Date"
11294                       "Message-ID" "Xref" "References" "Body"))
11295             nil 'require-match))
11296          (read-string "Regexp: ")
11297          (read-key-sequence "Command: ")
11298          current-prefix-arg))
11299   (when (equal header "Body")
11300     (setq header ""))
11301   (gnus-set-global-variables)
11302   ;; Hidden thread subtrees must be searched as well.
11303   (gnus-summary-show-all-threads)
11304   ;; We don't want to change current point nor window configuration.
11305   (save-excursion
11306     (save-window-excursion
11307       (gnus-message 6 "Executing %s..." (key-description command))
11308       ;; We'd like to execute COMMAND interactively so as to give arguments.
11309       (gnus-execute header regexp
11310                     `(lambda () (call-interactively ',(key-binding command)))
11311                     backward)
11312       (gnus-message 6 "Executing %s...done" (key-description command)))))
11313
11314 (defun gnus-summary-beginning-of-article ()
11315   "Scroll the article back to the beginning."
11316   (interactive)
11317   (gnus-set-global-variables)
11318   (gnus-summary-select-article)
11319   (gnus-configure-windows 'article)
11320   (gnus-eval-in-buffer-window gnus-article-buffer
11321     (widen)
11322     (goto-char (point-min))
11323     (and gnus-break-pages (gnus-narrow-to-page))))
11324
11325 (defun gnus-summary-end-of-article ()
11326   "Scroll to the end of the article."
11327   (interactive)
11328   (gnus-set-global-variables)
11329   (gnus-summary-select-article)
11330   (gnus-configure-windows 'article)
11331   (gnus-eval-in-buffer-window gnus-article-buffer
11332     (widen)
11333     (goto-char (point-max))
11334     (recenter -3)
11335     (and gnus-break-pages (gnus-narrow-to-page))))
11336
11337 (defun gnus-summary-show-article (&optional arg)
11338   "Force re-fetching of the current article.
11339 If ARG (the prefix) is non-nil, show the raw article without any
11340 article massaging functions being run."
11341   (interactive "P")
11342   (gnus-set-global-variables)
11343   (if (not arg)
11344       ;; Select the article the normal way.
11345       (gnus-summary-select-article nil 'force)
11346     ;; Bind the article treatment functions to nil.
11347     (let ((gnus-have-all-headers t)
11348           gnus-article-display-hook
11349           gnus-article-prepare-hook
11350           gnus-break-pages
11351           gnus-visual)
11352       (gnus-summary-select-article nil 'force)))
11353   (gnus-summary-goto-subject gnus-current-article)
11354 ;  (gnus-configure-windows 'article)
11355   (gnus-summary-position-point))
11356
11357 (defun gnus-summary-verbose-headers (&optional arg)
11358   "Toggle permanent full header display.
11359 If ARG is a positive number, turn header display on.
11360 If ARG is a negative number, turn header display off."
11361   (interactive "P")
11362   (gnus-set-global-variables)
11363   (gnus-summary-toggle-header arg)
11364   (setq gnus-show-all-headers
11365         (cond ((or (not (numberp arg))
11366                    (zerop arg))
11367                (not gnus-show-all-headers))
11368               ((natnump arg)
11369                t))))
11370
11371 (defun gnus-summary-toggle-header (&optional arg)
11372   "Show the headers if they are hidden, or hide them if they are shown.
11373 If ARG is a positive number, show the entire header.
11374 If ARG is a negative number, hide the unwanted header lines."
11375   (interactive "P")
11376   (gnus-set-global-variables)
11377   (save-excursion
11378     (set-buffer gnus-article-buffer)
11379     (let* ((buffer-read-only nil)
11380            (inhibit-point-motion-hooks t)
11381            (hidden (text-property-any
11382                     (goto-char (point-min)) (search-forward "\n\n")
11383                     'invisible t))
11384            e)
11385       (goto-char (point-min))
11386       (when (search-forward "\n\n" nil t)
11387         (delete-region (point-min) (1- (point))))
11388       (goto-char (point-min))
11389       (save-excursion
11390         (set-buffer gnus-original-article-buffer)
11391         (goto-char (point-min))
11392         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11393       (insert-buffer-substring gnus-original-article-buffer 1 e)
11394       (let ((gnus-inhibit-hiding t))
11395         (run-hooks 'gnus-article-display-hook))
11396       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11397           (gnus-article-hide-headers)))))
11398
11399 (defun gnus-summary-show-all-headers ()
11400   "Make all header lines visible."
11401   (interactive)
11402   (gnus-set-global-variables)
11403   (gnus-article-show-all-headers))
11404
11405 (defun gnus-summary-toggle-mime (&optional arg)
11406   "Toggle MIME processing.
11407 If ARG is a positive number, turn MIME processing on."
11408   (interactive "P")
11409   (gnus-set-global-variables)
11410   (setq gnus-show-mime
11411         (if (null arg) (not gnus-show-mime)
11412           (> (prefix-numeric-value arg) 0)))
11413   (gnus-summary-select-article t 'force))
11414
11415 (defun gnus-summary-caesar-message (&optional arg)
11416   "Caesar rotate the current article by 13.
11417 The numerical prefix specifies how manu places to rotate each letter
11418 forward."
11419   (interactive "P")
11420   (gnus-set-global-variables)
11421   (gnus-summary-select-article)
11422   (let ((mail-header-separator ""))
11423     (gnus-eval-in-buffer-window gnus-article-buffer
11424       (save-restriction
11425         (widen)
11426         (let ((start (window-start))
11427               buffer-read-only)
11428           (message-caesar-buffer-body arg)
11429           (set-window-start (get-buffer-window (current-buffer)) start))))))
11430
11431 (defun gnus-summary-stop-page-breaking ()
11432   "Stop page breaking in the current article."
11433   (interactive)
11434   (gnus-set-global-variables)
11435   (gnus-summary-select-article)
11436   (gnus-eval-in-buffer-window gnus-article-buffer
11437     (widen)))
11438
11439 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11440   "Move the current article to a different newsgroup.
11441 If N is a positive number, move the N next articles.
11442 If N is a negative number, move the N previous articles.
11443 If N is nil and any articles have been marked with the process mark,
11444 move those articles instead.
11445 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11446 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11447 re-spool using this method.
11448
11449 For this function to work, both the current newsgroup and the
11450 newsgroup that you want to move to have to support the `request-move'
11451 and `request-accept' functions."
11452   (interactive "P")
11453   (unless action (setq action 'move))
11454   (gnus-set-global-variables)
11455   ;; Check whether the source group supports the required functions.
11456   (cond ((and (eq action 'move)
11457               (not (gnus-check-backend-function
11458                     'request-move-article gnus-newsgroup-name)))
11459          (error "The current group does not support article moving"))
11460         ((and (eq action 'crosspost)
11461               (not (gnus-check-backend-function
11462                     'request-replace-article gnus-newsgroup-name)))
11463          (error "The current group does not support article editing")))
11464   (let ((articles (gnus-summary-work-articles n))
11465         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11466         (names '((move "Move" "Moving")
11467                  (copy "Copy" "Copying")
11468                  (crosspost "Crosspost" "Crossposting")))
11469         (copy-buf (save-excursion
11470                     (nnheader-set-temp-buffer " *copy article*")))
11471         art-group to-method new-xref article to-groups)
11472     (unless (assq action names)
11473       (error "Unknown action %s" action))
11474     ;; Read the newsgroup name.
11475     (when (and (not to-newsgroup)
11476                (not select-method))
11477       (setq to-newsgroup
11478             (gnus-read-move-group-name
11479              (cadr (assq action names))
11480              (symbol-value (intern (format "gnus-current-%s-group" action)))
11481              articles prefix))
11482       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11483     (setq to-method (or select-method 
11484                         (gnus-find-method-for-group to-newsgroup)))
11485     ;; Check the method we are to move this article to...
11486     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11487         (error "%s does not support article copying" (car to-method)))
11488     (or (gnus-check-server to-method)
11489         (error "Can't open server %s" (car to-method)))
11490     (gnus-message 6 "%s to %s: %s..."
11491                   (caddr (assq action names))
11492                   (or (car select-method) to-newsgroup) articles)
11493     (while articles
11494       (setq article (pop articles))
11495       (setq
11496        art-group
11497        (cond
11498         ;; Move the article.
11499         ((eq action 'move)
11500          (gnus-request-move-article
11501           article                       ; Article to move
11502           gnus-newsgroup-name           ; From newsgrouo
11503           (nth 1 (gnus-find-method-for-group
11504                   gnus-newsgroup-name)) ; Server
11505           (list 'gnus-request-accept-article
11506                 to-newsgroup (list 'quote select-method)
11507                 (not articles))         ; Accept form
11508           (not articles)))              ; Only save nov last time
11509         ;; Copy the article.
11510         ((eq action 'copy)
11511          (save-excursion
11512            (set-buffer copy-buf)
11513            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11514            (gnus-request-accept-article
11515             to-newsgroup select-method (not articles))))
11516         ;; Crosspost the article.
11517         ((eq action 'crosspost)
11518          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11519            (setq new-xref (concat gnus-newsgroup-name ":" article))
11520            (if (and xref (not (string= xref "")))
11521                (progn
11522                  (when (string-match "^Xref: " xref)
11523                    (setq xref (substring xref (match-end 0))))
11524                  (setq new-xref (concat xref " " new-xref)))
11525              (setq new-xref (concat (system-name) " " new-xref)))
11526            (save-excursion
11527              (set-buffer copy-buf)
11528              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11529              (nnheader-replace-header "xref" new-xref)
11530              (gnus-request-accept-article
11531               to-newsgroup select-method (not articles)))))))
11532       (if (not art-group)
11533           (gnus-message 1 "Couldn't %s article %s"
11534                         (cadr (assq action names)) article)
11535         (let* ((entry
11536                 (or
11537                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11538                  (gnus-gethash
11539                   (gnus-group-prefixed-name
11540                    (car art-group)
11541                    (or select-method 
11542                        (gnus-find-method-for-group to-newsgroup)))
11543                   gnus-newsrc-hashtb)))
11544                (info (nth 2 entry))
11545                (to-group (gnus-info-group info)))
11546           ;; Update the group that has been moved to.
11547           (when (and info
11548                      (memq action '(move copy)))
11549             (unless (member to-group to-groups)
11550               (push to-group to-groups))
11551
11552             (unless (memq article gnus-newsgroup-unreads)
11553               (gnus-info-set-read
11554                info (gnus-add-to-range (gnus-info-read info)
11555                                        (list (cdr art-group)))))
11556
11557             ;; Copy any marks over to the new group.
11558             (let ((marks gnus-article-mark-lists)
11559                   (to-article (cdr art-group)))
11560
11561               ;; See whether the article is to be put in the cache.
11562               (when gnus-use-cache
11563                 (gnus-cache-possibly-enter-article
11564                  to-group to-article
11565                  (let ((header (copy-sequence
11566                                 (gnus-summary-article-header article))))
11567                    (mail-header-set-number header to-article)
11568                    header)
11569                  (memq article gnus-newsgroup-marked)
11570                  (memq article gnus-newsgroup-dormant)
11571                  (memq article gnus-newsgroup-unreads)))
11572
11573               (while marks
11574                 (when (memq article (symbol-value
11575                                      (intern (format "gnus-newsgroup-%s"
11576                                                      (caar marks)))))
11577                   ;; If the other group is the same as this group,
11578                   ;; then we have to add the mark to the list.
11579                   (when (equal to-group gnus-newsgroup-name)
11580                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
11581                          (cons to-article
11582                                (symbol-value
11583                                 (intern (format "gnus-newsgroup-%s"
11584                                                 (caar marks)))))))
11585                   ;; Copy mark to other group.
11586                   (gnus-add-marked-articles
11587                    to-group (cdar marks) (list to-article) info))
11588                 (setq marks (cdr marks)))))
11589
11590           ;; Update the Xref header in this article to point to
11591           ;; the new crossposted article we have just created.
11592           (when (eq action 'crosspost)
11593             (save-excursion
11594               (set-buffer copy-buf)
11595               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11596               (nnheader-replace-header
11597                "xref" (concat new-xref " " (gnus-group-prefixed-name
11598                                             (car art-group) to-method)
11599                               ":" (cdr art-group)))
11600               (gnus-request-replace-article
11601                article gnus-newsgroup-name (current-buffer)))))
11602
11603         (gnus-summary-goto-subject article)
11604         (when (eq action 'move)
11605           (gnus-summary-mark-article article gnus-canceled-mark)))
11606       (gnus-summary-remove-process-mark article))
11607     ;; Re-activate all groups that have been moved to.
11608     (while to-groups
11609       (gnus-activate-group (pop to-groups)))
11610     
11611     (gnus-kill-buffer copy-buf)
11612     (gnus-summary-position-point)
11613     (gnus-set-mode-line 'summary)))
11614
11615 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11616   "Move the current article to a different newsgroup.
11617 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11618 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11619 re-spool using this method."
11620   (interactive "P")
11621   (gnus-summary-move-article n nil select-method 'copy))
11622
11623 (defun gnus-summary-crosspost-article (&optional n)
11624   "Crosspost the current article to some other group."
11625   (interactive "P")
11626   (gnus-summary-move-article n nil nil 'crosspost))
11627
11628 (defvar gnus-summary-respool-default-method nil
11629   "Default method for respooling an article.  
11630 If nil, use to the current newsgroup method.")
11631
11632 (defun gnus-summary-respool-article (&optional n method)
11633   "Respool the current article.
11634 The article will be squeezed through the mail spooling process again,
11635 which means that it will be put in some mail newsgroup or other
11636 depending on `nnmail-split-methods'.
11637 If N is a positive number, respool the N next articles.
11638 If N is a negative number, respool the N previous articles.
11639 If N is nil and any articles have been marked with the process mark,
11640 respool those articles instead.
11641
11642 Respooling can be done both from mail groups and \"real\" newsgroups.
11643 In the former case, the articles in question will be moved from the
11644 current group into whatever groups they are destined to.  In the
11645 latter case, they will be copied into the relevant groups."
11646   (interactive 
11647    (list current-prefix-arg
11648          (let* ((methods (gnus-methods-using 'respool))
11649                 (methname
11650                  (symbol-name (or gnus-summary-respool-default-method
11651                                   (car (gnus-find-method-for-group
11652                                         gnus-newsgroup-name)))))
11653                 (method
11654                  (gnus-completing-read 
11655                   methname "What backend do you want to use when? "
11656                   methods nil t nil 'gnus-method-history))
11657                 ms)
11658            (cond
11659             ((zerop (length (setq ms (gnus-servers-using-backend method))))
11660              (list (intern method) ""))
11661             ((= 1 (length ms))
11662              (car ms))
11663             (t
11664              (cdr (completing-read 
11665                    "Server name: "
11666                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
11667   (gnus-set-global-variables)
11668   (unless method
11669     (error "No method given for respooling"))
11670   (if (assoc (symbol-name
11671               (car (gnus-find-method-for-group gnus-newsgroup-name)))
11672              (gnus-methods-using 'respool))
11673       (gnus-summary-move-article n nil method)
11674     (gnus-summary-copy-article n nil method)))
11675
11676 (defun gnus-summary-import-article (file)
11677   "Import a random file into a mail newsgroup."
11678   (interactive "fImport file: ")
11679   (gnus-set-global-variables)
11680   (let ((group gnus-newsgroup-name)
11681         (now (current-time))
11682         atts lines)
11683     (or (gnus-check-backend-function 'request-accept-article group)
11684         (error "%s does not support article importing" group))
11685     (or (file-readable-p file)
11686         (not (file-regular-p file))
11687         (error "Can't read %s" file))
11688     (save-excursion
11689       (set-buffer (get-buffer-create " *import file*"))
11690       (buffer-disable-undo (current-buffer))
11691       (erase-buffer)
11692       (insert-file-contents file)
11693       (goto-char (point-min))
11694       (unless (nnheader-article-p)
11695         ;; This doesn't look like an article, so we fudge some headers.
11696         (setq atts (file-attributes file)
11697               lines (count-lines (point-min) (point-max)))
11698         (insert "From: " (read-string "From: ") "\n"
11699                 "Subject: " (read-string "Subject: ") "\n"
11700                 "Date: " (timezone-make-date-arpa-standard
11701                           (current-time-string (nth 5 atts))
11702                           (current-time-zone now)
11703                           (current-time-zone now)) "\n"
11704                 "Message-ID: " (message-make-message-id) "\n"
11705                 "Lines: " (int-to-string lines) "\n"
11706                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11707       (gnus-request-accept-article group nil t)
11708       (kill-buffer (current-buffer)))))
11709
11710 (defun gnus-summary-expire-articles (&optional now)
11711   "Expire all articles that are marked as expirable in the current group."
11712   (interactive)
11713   (gnus-set-global-variables)
11714   (when (gnus-check-backend-function
11715          'request-expire-articles gnus-newsgroup-name)
11716     ;; This backend supports expiry.
11717     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11718            (expirable (if total
11719                           (gnus-list-of-read-articles gnus-newsgroup-name)
11720                         (setq gnus-newsgroup-expirable
11721                               (sort gnus-newsgroup-expirable '<))))
11722            (expiry-wait (if now 'immediate
11723                           (gnus-group-get-parameter
11724                            gnus-newsgroup-name 'expiry-wait)))
11725            es)
11726       (when expirable
11727         ;; There are expirable articles in this group, so we run them
11728         ;; through the expiry process.
11729         (gnus-message 6 "Expiring articles...")
11730         ;; The list of articles that weren't expired is returned.
11731         (if expiry-wait
11732             (let ((nnmail-expiry-wait-function nil)
11733                   (nnmail-expiry-wait expiry-wait))
11734               (setq es (gnus-request-expire-articles
11735                         expirable gnus-newsgroup-name)))
11736           (setq es (gnus-request-expire-articles
11737                     expirable gnus-newsgroup-name)))
11738         (or total (setq gnus-newsgroup-expirable es))
11739         ;; We go through the old list of expirable, and mark all
11740         ;; really expired articles as nonexistent.
11741         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11742           (let ((gnus-use-cache nil))
11743             (while expirable
11744               (unless (memq (car expirable) es)
11745                 (when (gnus-data-find (car expirable))
11746                   (gnus-summary-mark-article
11747                    (car expirable) gnus-canceled-mark)))
11748               (setq expirable (cdr expirable)))))
11749         (gnus-message 6 "Expiring articles...done")))))
11750
11751 (defun gnus-summary-expire-articles-now ()
11752   "Expunge all expirable articles in the current group.
11753 This means that *all* articles that are marked as expirable will be
11754 deleted forever, right now."
11755   (interactive)
11756   (gnus-set-global-variables)
11757   (or gnus-expert-user
11758       (gnus-y-or-n-p
11759        "Are you really, really, really sure you want to delete all these messages? ")
11760       (error "Phew!"))
11761   (gnus-summary-expire-articles t))
11762
11763 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11764 (defun gnus-summary-delete-article (&optional n)
11765   "Delete the N next (mail) articles.
11766 This command actually deletes articles.  This is not a marking
11767 command.  The article will disappear forever from your life, never to
11768 return.
11769 If N is negative, delete backwards.
11770 If N is nil and articles have been marked with the process mark,
11771 delete these instead."
11772   (interactive "P")
11773   (gnus-set-global-variables)
11774   (or (gnus-check-backend-function 'request-expire-articles
11775                                    gnus-newsgroup-name)
11776       (error "The current newsgroup does not support article deletion."))
11777   ;; Compute the list of articles to delete.
11778   (let ((articles (gnus-summary-work-articles n))
11779         not-deleted)
11780     (if (and gnus-novice-user
11781              (not (gnus-y-or-n-p
11782                    (format "Do you really want to delete %s forever? "
11783                            (if (> (length articles) 1) 
11784                                (format "these %s articles" (length articles))
11785                              "this article")))))
11786         ()
11787       ;; Delete the articles.
11788       (setq not-deleted (gnus-request-expire-articles
11789                          articles gnus-newsgroup-name 'force))
11790       (while articles
11791         (gnus-summary-remove-process-mark (car articles))
11792         ;; The backend might not have been able to delete the article
11793         ;; after all.
11794         (or (memq (car articles) not-deleted)
11795             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11796         (setq articles (cdr articles))))
11797     (gnus-summary-position-point)
11798     (gnus-set-mode-line 'summary)
11799     not-deleted))
11800
11801 (defun gnus-summary-edit-article (&optional force)
11802   "Enter into a buffer and edit the current article.
11803 This will have permanent effect only in mail groups.
11804 If FORCE is non-nil, allow editing of articles even in read-only
11805 groups."
11806   (interactive "P")
11807   (save-excursion
11808     (set-buffer gnus-summary-buffer)
11809     (gnus-set-global-variables)
11810     (when (and (not force)
11811                (gnus-group-read-only-p))
11812       (error "The current newsgroup does not support article editing."))
11813     (gnus-summary-select-article t nil t)
11814     (gnus-configure-windows 'article)
11815     (select-window (get-buffer-window gnus-article-buffer))
11816     (gnus-message 6 "C-c C-c to end edits")
11817     (setq buffer-read-only nil)
11818     (text-mode)
11819     (use-local-map (copy-keymap (current-local-map)))
11820     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11821     (buffer-enable-undo)
11822     (widen)
11823     (goto-char (point-min))
11824     (search-forward "\n\n" nil t)))
11825
11826 (defun gnus-summary-edit-article-done ()
11827   "Make edits to the current article permanent."
11828   (interactive)
11829   (if (gnus-group-read-only-p)
11830       (progn
11831         (gnus-summary-edit-article-postpone)
11832         (gnus-error
11833          1 "The current newsgroup does not support article editing."))
11834     (let ((buf (format "%s" (buffer-string))))
11835       (erase-buffer)
11836       (insert buf)
11837       (if (not (gnus-request-replace-article
11838                 (cdr gnus-article-current) (car gnus-article-current)
11839                 (current-buffer)))
11840           (error "Couldn't replace article.")
11841         (gnus-article-mode)
11842         (use-local-map gnus-article-mode-map)
11843         (setq buffer-read-only t)
11844         (buffer-disable-undo (current-buffer))
11845         (gnus-configure-windows 'summary)
11846         (gnus-summary-update-article (cdr gnus-article-current))
11847         (when gnus-use-cache
11848           (gnus-cache-update-article    
11849            (car gnus-article-current) (cdr gnus-article-current)))
11850         (when gnus-keep-backlog
11851           (gnus-backlog-remove-article 
11852            (car gnus-article-current) (cdr gnus-article-current))))
11853       (save-excursion
11854         (when (get-buffer gnus-original-article-buffer)
11855           (set-buffer gnus-original-article-buffer)
11856           (setq gnus-original-article nil)))
11857       (setq gnus-article-current nil
11858             gnus-current-article nil)
11859       (run-hooks 'gnus-article-display-hook)
11860       (and (gnus-visual-p 'summary-highlight 'highlight)
11861            (run-hooks 'gnus-visual-mark-article-hook)))))
11862
11863 (defun gnus-summary-edit-article-postpone ()
11864   "Postpone changes to the current article."
11865   (interactive)
11866   (gnus-article-mode)
11867   (use-local-map gnus-article-mode-map)
11868   (setq buffer-read-only t)
11869   (buffer-disable-undo (current-buffer))
11870   (gnus-configure-windows 'summary)
11871   (and (gnus-visual-p 'summary-highlight 'highlight)
11872        (run-hooks 'gnus-visual-mark-article-hook)))
11873
11874 (defun gnus-summary-respool-query ()
11875   "Query where the respool algorithm would put this article."
11876   (interactive)
11877   (gnus-set-global-variables)
11878   (gnus-summary-select-article)
11879   (save-excursion
11880     (set-buffer gnus-article-buffer)
11881     (save-restriction
11882       (goto-char (point-min))
11883       (search-forward "\n\n")
11884       (narrow-to-region (point-min) (point))
11885       (pp-eval-expression
11886        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11887
11888 ;; Summary marking commands.
11889
11890 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11891   "Mark articles which has the same subject as read, and then select the next.
11892 If UNMARK is positive, remove any kind of mark.
11893 If UNMARK is negative, tick articles."
11894   (interactive "P")
11895   (gnus-set-global-variables)
11896   (if unmark
11897       (setq unmark (prefix-numeric-value unmark)))
11898   (let ((count
11899          (gnus-summary-mark-same-subject
11900           (gnus-summary-article-subject) unmark)))
11901     ;; Select next unread article.  If auto-select-same mode, should
11902     ;; select the first unread article.
11903     (gnus-summary-next-article t (and gnus-auto-select-same
11904                                       (gnus-summary-article-subject)))
11905     (gnus-message 7 "%d article%s marked as %s"
11906                   count (if (= count 1) " is" "s are")
11907                   (if unmark "unread" "read"))))
11908
11909 (defun gnus-summary-kill-same-subject (&optional unmark)
11910   "Mark articles which has the same subject as read.
11911 If UNMARK is positive, remove any kind of mark.
11912 If UNMARK is negative, tick articles."
11913   (interactive "P")
11914   (gnus-set-global-variables)
11915   (if unmark
11916       (setq unmark (prefix-numeric-value unmark)))
11917   (let ((count
11918          (gnus-summary-mark-same-subject
11919           (gnus-summary-article-subject) unmark)))
11920     ;; If marked as read, go to next unread subject.
11921     (if (null unmark)
11922         ;; Go to next unread subject.
11923         (gnus-summary-next-subject 1 t))
11924     (gnus-message 7 "%d articles are marked as %s"
11925                   count (if unmark "unread" "read"))))
11926
11927 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11928   "Mark articles with same SUBJECT as read, and return marked number.
11929 If optional argument UNMARK is positive, remove any kinds of marks.
11930 If optional argument UNMARK is negative, mark articles as unread instead."
11931   (let ((count 1))
11932     (save-excursion
11933       (cond
11934        ((null unmark)                   ; Mark as read.
11935         (while (and
11936                 (progn
11937                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11938                   (gnus-summary-show-thread) t)
11939                 (gnus-summary-find-subject subject))
11940           (setq count (1+ count))))
11941        ((> unmark 0)                    ; Tick.
11942         (while (and
11943                 (progn
11944                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11945                   (gnus-summary-show-thread) t)
11946                 (gnus-summary-find-subject subject))
11947           (setq count (1+ count))))
11948        (t                               ; Mark as unread.
11949         (while (and
11950                 (progn
11951                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11952                   (gnus-summary-show-thread) t)
11953                 (gnus-summary-find-subject subject))
11954           (setq count (1+ count)))))
11955       (gnus-set-mode-line 'summary)
11956       ;; Return the number of marked articles.
11957       count)))
11958
11959 (defun gnus-summary-mark-as-processable (n &optional unmark)
11960   "Set the process mark on the next N articles.
11961 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11962 the process mark instead.  The difference between N and the actual
11963 number of articles marked is returned."
11964   (interactive "p")
11965   (gnus-set-global-variables)
11966   (let ((backward (< n 0))
11967         (n (abs n)))
11968     (while (and
11969             (> n 0)
11970             (if unmark
11971                 (gnus-summary-remove-process-mark
11972                  (gnus-summary-article-number))
11973               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11974             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11975       (setq n (1- n)))
11976     (if (/= 0 n) (gnus-message 7 "No more articles"))
11977     (gnus-summary-recenter)
11978     (gnus-summary-position-point)
11979     n))
11980
11981 (defun gnus-summary-unmark-as-processable (n)
11982   "Remove the process mark from the next N articles.
11983 If N is negative, mark backward instead.  The difference between N and
11984 the actual number of articles marked is returned."
11985   (interactive "p")
11986   (gnus-set-global-variables)
11987   (gnus-summary-mark-as-processable n t))
11988
11989 (defun gnus-summary-unmark-all-processable ()
11990   "Remove the process mark from all articles."
11991   (interactive)
11992   (gnus-set-global-variables)
11993   (save-excursion
11994     (while gnus-newsgroup-processable
11995       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11996   (gnus-summary-position-point))
11997
11998 (defun gnus-summary-mark-as-expirable (n)
11999   "Mark N articles forward as expirable.
12000 If N is negative, mark backward instead.  The difference between N and
12001 the actual number of articles marked is returned."
12002   (interactive "p")
12003   (gnus-set-global-variables)
12004   (gnus-summary-mark-forward n gnus-expirable-mark))
12005
12006 (defun gnus-summary-mark-article-as-replied (article)
12007   "Mark ARTICLE replied and update the summary line."
12008   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
12009   (let ((buffer-read-only nil))
12010     (when (gnus-summary-goto-subject article)
12011       (gnus-summary-update-secondary-mark article))))
12012
12013 (defun gnus-summary-set-bookmark (article)
12014   "Set a bookmark in current article."
12015   (interactive (list (gnus-summary-article-number)))
12016   (gnus-set-global-variables)
12017   (if (or (not (get-buffer gnus-article-buffer))
12018           (not gnus-current-article)
12019           (not gnus-article-current)
12020           (not (equal gnus-newsgroup-name (car gnus-article-current))))
12021       (error "No current article selected"))
12022   ;; Remove old bookmark, if one exists.
12023   (let ((old (assq article gnus-newsgroup-bookmarks)))
12024     (if old (setq gnus-newsgroup-bookmarks
12025                   (delq old gnus-newsgroup-bookmarks))))
12026   ;; Set the new bookmark, which is on the form
12027   ;; (article-number . line-number-in-body).
12028   (setq gnus-newsgroup-bookmarks
12029         (cons
12030          (cons article
12031                (save-excursion
12032                  (set-buffer gnus-article-buffer)
12033                  (count-lines
12034                   (min (point)
12035                        (save-excursion
12036                          (goto-char (point-min))
12037                          (search-forward "\n\n" nil t)
12038                          (point)))
12039                   (point))))
12040          gnus-newsgroup-bookmarks))
12041   (gnus-message 6 "A bookmark has been added to the current article."))
12042
12043 (defun gnus-summary-remove-bookmark (article)
12044   "Remove the bookmark from the current article."
12045   (interactive (list (gnus-summary-article-number)))
12046   (gnus-set-global-variables)
12047   ;; Remove old bookmark, if one exists.
12048   (let ((old (assq article gnus-newsgroup-bookmarks)))
12049     (if old
12050         (progn
12051           (setq gnus-newsgroup-bookmarks
12052                 (delq old gnus-newsgroup-bookmarks))
12053           (gnus-message 6 "Removed bookmark."))
12054       (gnus-message 6 "No bookmark in current article."))))
12055
12056 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12057 (defun gnus-summary-mark-as-dormant (n)
12058   "Mark N articles forward as dormant.
12059 If N is negative, mark backward instead.  The difference between N and
12060 the actual number of articles marked is returned."
12061   (interactive "p")
12062   (gnus-set-global-variables)
12063   (gnus-summary-mark-forward n gnus-dormant-mark))
12064
12065 (defun gnus-summary-set-process-mark (article)
12066   "Set the process mark on ARTICLE and update the summary line."
12067   (setq gnus-newsgroup-processable
12068         (cons article
12069               (delq article gnus-newsgroup-processable)))
12070   (when (gnus-summary-goto-subject article)
12071     (gnus-summary-show-thread)
12072     (gnus-summary-update-secondary-mark article)))
12073
12074 (defun gnus-summary-remove-process-mark (article)
12075   "Remove the process mark from ARTICLE and update the summary line."
12076   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
12077   (when (gnus-summary-goto-subject article)
12078     (gnus-summary-show-thread)
12079     (gnus-summary-update-secondary-mark article)))
12080
12081 (defun gnus-summary-set-saved-mark (article)
12082   "Set the process mark on ARTICLE and update the summary line."
12083   (push article gnus-newsgroup-saved)
12084   (when (gnus-summary-goto-subject article)
12085     (gnus-summary-update-secondary-mark article)))
12086
12087 (defun gnus-summary-mark-forward (n &optional mark no-expire)
12088   "Mark N articles as read forwards.
12089 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
12090 The difference between N and the actual number of articles marked is
12091 returned."
12092   (interactive "p")
12093   (gnus-set-global-variables)
12094   (let ((backward (< n 0))
12095         (gnus-summary-goto-unread
12096          (and gnus-summary-goto-unread
12097               (not (eq gnus-summary-goto-unread 'never))
12098               (not (memq mark (list gnus-unread-mark
12099                                     gnus-ticked-mark gnus-dormant-mark)))))
12100         (n (abs n))
12101         (mark (or mark gnus-del-mark)))
12102     (while (and (> n 0)
12103                 (gnus-summary-mark-article nil mark no-expire)
12104                 (zerop (gnus-summary-next-subject
12105                         (if backward -1 1)
12106                         (and gnus-summary-goto-unread
12107                              (not (eq gnus-summary-goto-unread 'never)))
12108                         t)))
12109       (setq n (1- n)))
12110     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12111     (gnus-summary-recenter)
12112     (gnus-summary-position-point)
12113     (gnus-set-mode-line 'summary)
12114     n))
12115
12116 (defun gnus-summary-mark-article-as-read (mark)
12117   "Mark the current article quickly as read with MARK."
12118   (let ((article (gnus-summary-article-number)))
12119     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12120     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12121     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12122     (setq gnus-newsgroup-reads
12123           (cons (cons article mark) gnus-newsgroup-reads))
12124     ;; Possibly remove from cache, if that is used.
12125     (and gnus-use-cache (gnus-cache-enter-remove-article article))
12126     ;; Allow the backend to change the mark.
12127     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
12128     ;; Check for auto-expiry.
12129     (when (and gnus-newsgroup-auto-expire
12130                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12131                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12132                    (= mark gnus-ancient-mark)
12133                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
12134       (setq mark gnus-expirable-mark)
12135       (push article gnus-newsgroup-expirable))
12136     ;; Set the mark in the buffer.
12137     (gnus-summary-update-mark mark 'unread)
12138     t))
12139
12140 (defun gnus-summary-mark-article-as-unread (mark)
12141   "Mark the current article quickly as unread with MARK."
12142   (let ((article (gnus-summary-article-number)))
12143     (if (< article 0)
12144         (gnus-error 1 "Unmarkable article")
12145       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12146       (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12147       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12148       (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
12149       (cond ((= mark gnus-ticked-mark)
12150              (push article gnus-newsgroup-marked))
12151             ((= mark gnus-dormant-mark)
12152              (push article gnus-newsgroup-dormant))
12153             (t
12154              (push article gnus-newsgroup-unreads)))
12155       (setq gnus-newsgroup-reads
12156             (delq (assq article gnus-newsgroup-reads)
12157                   gnus-newsgroup-reads))
12158
12159       ;; See whether the article is to be put in the cache.
12160       (and gnus-use-cache
12161            (vectorp (gnus-summary-article-header article))
12162            (save-excursion
12163              (gnus-cache-possibly-enter-article
12164               gnus-newsgroup-name article
12165               (gnus-summary-article-header article)
12166               (= mark gnus-ticked-mark)
12167               (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12168
12169       ;; Fix the mark.
12170       (gnus-summary-update-mark mark 'unread))
12171     t))
12172
12173 (defun gnus-summary-mark-article (&optional article mark no-expire)
12174   "Mark ARTICLE with MARK.  MARK can be any character.
12175 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
12176 `??' (dormant) and `?E' (expirable).
12177 If MARK is nil, then the default character `?D' is used.
12178 If ARTICLE is nil, then the article on the current line will be
12179 marked."
12180   ;; The mark might be a string.
12181   (and (stringp mark)
12182        (setq mark (aref mark 0)))
12183   ;; If no mark is given, then we check auto-expiring.
12184   (and (not no-expire)
12185        gnus-newsgroup-auto-expire
12186        (or (not mark)
12187            (and (numberp mark)
12188                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12189                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12190                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
12191        (setq mark gnus-expirable-mark))
12192   (let* ((mark (or mark gnus-del-mark))
12193          (article (or article (gnus-summary-article-number))))
12194     (or article (error "No article on current line"))
12195     (if (or (= mark gnus-unread-mark)
12196             (= mark gnus-ticked-mark)
12197             (= mark gnus-dormant-mark))
12198         (gnus-mark-article-as-unread article mark)
12199       (gnus-mark-article-as-read article mark))
12200
12201     ;; See whether the article is to be put in the cache.
12202     (and gnus-use-cache
12203          (not (= mark gnus-canceled-mark))
12204          (vectorp (gnus-summary-article-header article))
12205          (save-excursion
12206            (gnus-cache-possibly-enter-article
12207             gnus-newsgroup-name article
12208             (gnus-summary-article-header article)
12209             (= mark gnus-ticked-mark)
12210             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12211
12212     (if (gnus-summary-goto-subject article nil t)
12213         (let ((buffer-read-only nil))
12214           (gnus-summary-show-thread)
12215           ;; Fix the mark.
12216           (gnus-summary-update-mark mark 'unread)
12217           t))))
12218
12219 (defun gnus-summary-update-secondary-mark (article)
12220   "Update the secondary (read, process, cache) mark."
12221   (gnus-summary-update-mark
12222    (cond ((memq article gnus-newsgroup-processable)
12223           gnus-process-mark)
12224          ((memq article gnus-newsgroup-cached)
12225           gnus-cached-mark)
12226          ((memq article gnus-newsgroup-replied)
12227           gnus-replied-mark)
12228          ((memq article gnus-newsgroup-saved)
12229           gnus-saved-mark)
12230          (t gnus-unread-mark))
12231    'replied)
12232   (when (gnus-visual-p 'summary-highlight 'highlight)
12233     (run-hooks 'gnus-summary-update-hook))
12234   t)
12235
12236 (defun gnus-summary-update-mark (mark type)
12237   (beginning-of-line)
12238   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
12239         (buffer-read-only nil))
12240     (when (and forward
12241                (<= (+ forward (point)) (point-max)))
12242       ;; Go to the right position on the line.
12243       (goto-char (+ forward (point)))
12244       ;; Replace the old mark with the new mark.
12245       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
12246       ;; Optionally update the marks by some user rule.
12247       (when (eq type 'unread)
12248         (gnus-data-set-mark
12249          (gnus-data-find (gnus-summary-article-number)) mark)
12250         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
12251
12252 (defun gnus-mark-article-as-read (article &optional mark)
12253   "Enter ARTICLE in the pertinent lists and remove it from others."
12254   ;; Make the article expirable.
12255   (let ((mark (or mark gnus-del-mark)))
12256     (if (= mark gnus-expirable-mark)
12257         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
12258       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
12259     ;; Remove from unread and marked lists.
12260     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12261     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12262     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12263     (push (cons article mark) gnus-newsgroup-reads)
12264     ;; Possibly remove from cache, if that is used.
12265     (when gnus-use-cache
12266       (gnus-cache-enter-remove-article article))))
12267
12268 (defun gnus-mark-article-as-unread (article &optional mark)
12269   "Enter ARTICLE in the pertinent lists and remove it from others."
12270   (let ((mark (or mark gnus-ticked-mark)))
12271     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12272     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12273     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12274     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12275     (cond ((= mark gnus-ticked-mark)
12276            (push article gnus-newsgroup-marked))
12277           ((= mark gnus-dormant-mark)
12278            (push article gnus-newsgroup-dormant))
12279           (t
12280            (push article gnus-newsgroup-unreads)))
12281     (setq gnus-newsgroup-reads
12282           (delq (assq article gnus-newsgroup-reads)
12283                 gnus-newsgroup-reads))))
12284
12285 (defalias 'gnus-summary-mark-as-unread-forward
12286   'gnus-summary-tick-article-forward)
12287 (make-obsolete 'gnus-summary-mark-as-unread-forward
12288                'gnus-summary-tick-article-forward)
12289 (defun gnus-summary-tick-article-forward (n)
12290   "Tick N articles forwards.
12291 If N is negative, tick backwards instead.
12292 The difference between N and the number of articles ticked is returned."
12293   (interactive "p")
12294   (gnus-summary-mark-forward n gnus-ticked-mark))
12295
12296 (defalias 'gnus-summary-mark-as-unread-backward
12297   'gnus-summary-tick-article-backward)
12298 (make-obsolete 'gnus-summary-mark-as-unread-backward
12299                'gnus-summary-tick-article-backward)
12300 (defun gnus-summary-tick-article-backward (n)
12301   "Tick N articles backwards.
12302 The difference between N and the number of articles ticked is returned."
12303   (interactive "p")
12304   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
12305
12306 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12307 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12308 (defun gnus-summary-tick-article (&optional article clear-mark)
12309   "Mark current article as unread.
12310 Optional 1st argument ARTICLE specifies article number to be marked as unread.
12311 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
12312   (interactive)
12313   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
12314                                        gnus-ticked-mark)))
12315
12316 (defun gnus-summary-mark-as-read-forward (n)
12317   "Mark N articles as read forwards.
12318 If N is negative, mark backwards instead.
12319 The difference between N and the actual number of articles marked is
12320 returned."
12321   (interactive "p")
12322   (gnus-summary-mark-forward n gnus-del-mark t))
12323
12324 (defun gnus-summary-mark-as-read-backward (n)
12325   "Mark the N articles as read backwards.
12326 The difference between N and the actual number of articles marked is
12327 returned."
12328   (interactive "p")
12329   (gnus-summary-mark-forward (- n) gnus-del-mark t))
12330
12331 (defun gnus-summary-mark-as-read (&optional article mark)
12332   "Mark current article as read.
12333 ARTICLE specifies the article to be marked as read.
12334 MARK specifies a string to be inserted at the beginning of the line."
12335   (gnus-summary-mark-article article mark))
12336
12337 (defun gnus-summary-clear-mark-forward (n)
12338   "Clear marks from N articles forward.
12339 If N is negative, clear backward instead.
12340 The difference between N and the number of marks cleared is returned."
12341   (interactive "p")
12342   (gnus-summary-mark-forward n gnus-unread-mark))
12343
12344 (defun gnus-summary-clear-mark-backward (n)
12345   "Clear marks from N articles backward.
12346 The difference between N and the number of marks cleared is returned."
12347   (interactive "p")
12348   (gnus-summary-mark-forward (- n) gnus-unread-mark))
12349
12350 (defun gnus-summary-mark-unread-as-read ()
12351   "Intended to be used by `gnus-summary-mark-article-hook'."
12352   (when (memq gnus-current-article gnus-newsgroup-unreads)
12353     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
12354
12355 (defun gnus-summary-mark-read-and-unread-as-read ()
12356   "Intended to be used by `gnus-summary-mark-article-hook'."
12357   (let ((mark (gnus-summary-article-mark)))
12358     (when (or (gnus-unread-mark-p mark)
12359               (gnus-read-mark-p mark))
12360       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
12361
12362 (defun gnus-summary-mark-region-as-read (point mark all)
12363   "Mark all unread articles between point and mark as read.
12364 If given a prefix, mark all articles between point and mark as read,
12365 even ticked and dormant ones."
12366   (interactive "r\nP")
12367   (save-excursion
12368     (let (article)
12369       (goto-char point)
12370       (beginning-of-line)
12371       (while (and
12372               (< (point) mark)
12373               (progn
12374                 (when (or all
12375                           (memq (setq article (gnus-summary-article-number))
12376                                 gnus-newsgroup-unreads))
12377                   (gnus-summary-mark-article article gnus-del-mark))
12378                 t)
12379               (gnus-summary-find-next))))))
12380
12381 (defun gnus-summary-mark-below (score mark)
12382   "Mark articles with score less than SCORE with MARK."
12383   (interactive "P\ncMark: ")
12384   (gnus-set-global-variables)
12385   (setq score (if score
12386                   (prefix-numeric-value score)
12387                 (or gnus-summary-default-score 0)))
12388   (save-excursion
12389     (set-buffer gnus-summary-buffer)
12390     (goto-char (point-min))
12391     (while 
12392         (progn
12393           (and (< (gnus-summary-article-score) score)
12394                (gnus-summary-mark-article nil mark))
12395           (gnus-summary-find-next)))))
12396
12397 (defun gnus-summary-kill-below (&optional score)
12398   "Mark articles with score below SCORE as read."
12399   (interactive "P")
12400   (gnus-set-global-variables)
12401   (gnus-summary-mark-below score gnus-killed-mark))
12402
12403 (defun gnus-summary-clear-above (&optional score)
12404   "Clear all marks from articles with score above SCORE."
12405   (interactive "P")
12406   (gnus-set-global-variables)
12407   (gnus-summary-mark-above score gnus-unread-mark))
12408
12409 (defun gnus-summary-tick-above (&optional score)
12410   "Tick all articles with score above SCORE."
12411   (interactive "P")
12412   (gnus-set-global-variables)
12413   (gnus-summary-mark-above score gnus-ticked-mark))
12414
12415 (defun gnus-summary-mark-above (score mark)
12416   "Mark articles with score over SCORE with MARK."
12417   (interactive "P\ncMark: ")
12418   (gnus-set-global-variables)
12419   (setq score (if score
12420                   (prefix-numeric-value score)
12421                 (or gnus-summary-default-score 0)))
12422   (save-excursion
12423     (set-buffer gnus-summary-buffer)
12424     (goto-char (point-min))
12425     (while (and (progn
12426                   (if (> (gnus-summary-article-score) score)
12427                       (gnus-summary-mark-article nil mark))
12428                   t)
12429                 (gnus-summary-find-next)))))
12430
12431 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12432 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12433 (defun gnus-summary-limit-include-expunged ()
12434   "Display all the hidden articles that were expunged for low scores."
12435   (interactive)
12436   (gnus-set-global-variables)
12437   (let ((buffer-read-only nil))
12438     (let ((scored gnus-newsgroup-scored)
12439           headers h)
12440       (while scored
12441         (or (gnus-summary-goto-subject (caar scored))
12442             (and (setq h (gnus-summary-article-header (caar scored)))
12443                  (< (cdar scored) gnus-summary-expunge-below)
12444                  (setq headers (cons h headers))))
12445         (setq scored (cdr scored)))
12446       (or headers (error "No expunged articles hidden."))
12447       (goto-char (point-min))
12448       (gnus-summary-prepare-unthreaded (nreverse headers)))
12449     (goto-char (point-min))
12450     (gnus-summary-position-point)))
12451
12452 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12453   "Mark all articles not marked as unread in this newsgroup as read.
12454 If prefix argument ALL is non-nil, all articles are marked as read.
12455 If QUIETLY is non-nil, no questions will be asked.
12456 If TO-HERE is non-nil, it should be a point in the buffer.  All
12457 articles before this point will be marked as read.
12458 The number of articles marked as read is returned."
12459   (interactive "P")
12460   (gnus-set-global-variables)
12461   (prog1
12462       (if (or quietly
12463               (not gnus-interactive-catchup) ;Without confirmation?
12464               gnus-expert-user
12465               (gnus-y-or-n-p
12466                (if all
12467                    "Mark absolutely all articles as read? "
12468                  "Mark all unread articles as read? ")))
12469           (if (and not-mark
12470                    (not gnus-newsgroup-adaptive)
12471                    (not gnus-newsgroup-auto-expire))
12472               (progn
12473                 (when all
12474                   (setq gnus-newsgroup-marked nil
12475                         gnus-newsgroup-dormant nil))
12476                 (setq gnus-newsgroup-unreads nil))
12477             ;; We actually mark all articles as canceled, which we
12478             ;; have to do when using auto-expiry or adaptive scoring.
12479             (gnus-summary-show-all-threads)
12480             (if (gnus-summary-first-subject (not all))
12481                 (while (and
12482                         (if to-here (< (point) to-here) t)
12483                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12484                         (gnus-summary-find-next (not all)))))
12485             (unless to-here
12486               (setq gnus-newsgroup-unreads nil))
12487             (gnus-set-mode-line 'summary)))
12488     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12489       (if (and (not to-here) (eq 'nnvirtual (car method)))
12490           (nnvirtual-catchup-group
12491            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12492     (gnus-summary-position-point)))
12493
12494 (defun gnus-summary-catchup-to-here (&optional all)
12495   "Mark all unticked articles before the current one as read.
12496 If ALL is non-nil, also mark ticked and dormant articles as read."
12497   (interactive "P")
12498   (gnus-set-global-variables)
12499   (save-excursion
12500     (gnus-save-hidden-threads
12501       (let ((beg (point)))
12502         ;; We check that there are unread articles.
12503         (when (or all (gnus-summary-find-prev))
12504           (gnus-summary-catchup all t beg)))))
12505   (gnus-summary-position-point))
12506
12507 (defun gnus-summary-catchup-all (&optional quietly)
12508   "Mark all articles in this newsgroup as read."
12509   (interactive "P")
12510   (gnus-set-global-variables)
12511   (gnus-summary-catchup t quietly))
12512
12513 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12514   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12515 If prefix argument ALL is non-nil, all articles are marked as read."
12516   (interactive "P")
12517   (gnus-set-global-variables)
12518   (gnus-summary-catchup all quietly nil 'fast)
12519   ;; Select next newsgroup or exit.
12520   (if (eq gnus-auto-select-next 'quietly)
12521       (gnus-summary-next-group nil)
12522     (gnus-summary-exit)))
12523
12524 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12525   "Mark all articles in this newsgroup as read, and then exit."
12526   (interactive "P")
12527   (gnus-set-global-variables)
12528   (gnus-summary-catchup-and-exit t quietly))
12529
12530 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12531 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12532   "Mark all articles in this group as read and select the next group.
12533 If given a prefix, mark all articles, unread as well as ticked, as
12534 read."
12535   (interactive "P")
12536   (gnus-set-global-variables)
12537   (save-excursion
12538     (gnus-summary-catchup all))
12539   (gnus-summary-next-article t nil nil t))
12540
12541 ;; Thread-based commands.
12542
12543 (defun gnus-summary-articles-in-thread (&optional article)
12544   "Return a list of all articles in the current thread.
12545 If ARTICLE is non-nil, return all articles in the thread that starts
12546 with that article."
12547   (let* ((article (or article (gnus-summary-article-number)))
12548          (data (gnus-data-find-list article))
12549          (top-level (gnus-data-level (car data)))
12550          (top-subject
12551           (cond ((null gnus-thread-operation-ignore-subject)
12552                  (gnus-simplify-subject-re
12553                   (mail-header-subject (gnus-data-header (car data)))))
12554                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12555                  (gnus-simplify-subject-fuzzy
12556                   (mail-header-subject (gnus-data-header (car data)))))
12557                 (t nil)))
12558          (end-point (save-excursion
12559                       (if (gnus-summary-go-to-next-thread) 
12560                           (point) (point-max))))
12561          articles)
12562     (while (and data
12563                 (< (gnus-data-pos (car data)) end-point))
12564       (when (or (not top-subject)
12565                 (string= top-subject
12566                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12567                              (gnus-simplify-subject-fuzzy
12568                               (mail-header-subject
12569                                (gnus-data-header (car data))))
12570                            (gnus-simplify-subject-re
12571                             (mail-header-subject
12572                              (gnus-data-header (car data)))))))
12573         (push (gnus-data-number (car data)) articles))
12574       (unless (and (setq data (cdr data))
12575                    (> (gnus-data-level (car data)) top-level))
12576         (setq data nil)))
12577     ;; Return the list of articles.
12578     (nreverse articles)))
12579
12580 (defun gnus-summary-rethread-current ()
12581   "Rethread the thread the current article is part of."
12582   (interactive)
12583   (gnus-set-global-variables)
12584   (let* ((gnus-show-threads t)
12585          (article (gnus-summary-article-number))
12586          (id (mail-header-id (gnus-summary-article-header)))
12587          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12588     (unless id
12589       (error "No article on the current line"))
12590     (gnus-rebuild-thread id)
12591     (gnus-summary-goto-subject article)))
12592
12593 (defun gnus-summary-reparent-thread ()
12594   "Make current article child of the marked (or previous) article.
12595
12596 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12597 is non-nil or the Subject: of both articles are the same."
12598   (interactive)
12599   (or (not (gnus-group-read-only-p))
12600       (error "The current newsgroup does not support article editing."))
12601   (or (<= (length gnus-newsgroup-processable) 1)
12602       (error "No more than one article may be marked."))
12603   (save-window-excursion
12604     (let ((gnus-article-buffer " *reparent*")
12605           (current-article (gnus-summary-article-number))
12606           ; first grab the marked article, otherwise one line up.
12607           (parent-article (if (not (null gnus-newsgroup-processable))
12608                               (car gnus-newsgroup-processable)
12609                             (save-excursion
12610                               (if (eq (forward-line -1) 0)
12611                                   (gnus-summary-article-number)
12612                                 (error "Beginning of summary buffer."))))))
12613       (or (not (eq current-article parent-article))
12614           (error "An article may not be self-referential."))
12615       (let ((message-id (mail-header-id 
12616                          (gnus-summary-article-header parent-article))))
12617         (or (and message-id (not (equal message-id "")))
12618             (error "No message-id in desired parent."))
12619         (gnus-summary-select-article t t nil current-article)
12620         (set-buffer gnus-article-buffer)
12621         (setq buffer-read-only nil)
12622         (let ((buf (format "%s" (buffer-string))))
12623           (erase-buffer)
12624           (insert buf))
12625         (goto-char (point-min))
12626         (if (search-forward-regexp "^References: " nil t)
12627             (insert message-id " " )
12628           (insert "References: " message-id "\n"))
12629         (or (gnus-request-replace-article current-article
12630                                           (car gnus-article-current)
12631                                           gnus-article-buffer)
12632             (error "Couldn't replace article."))
12633         (set-buffer gnus-summary-buffer)
12634         (gnus-summary-unmark-all-processable)
12635         (gnus-summary-rethread-current)
12636         (gnus-message 3 "Article %d is now the child of article %d."
12637                       current-article parent-article)))))
12638
12639 (defun gnus-summary-toggle-threads (&optional arg)
12640   "Toggle showing conversation threads.
12641 If ARG is positive number, turn showing conversation threads on."
12642   (interactive "P")
12643   (gnus-set-global-variables)
12644   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12645     (setq gnus-show-threads
12646           (if (null arg) (not gnus-show-threads)
12647             (> (prefix-numeric-value arg) 0)))
12648     (gnus-summary-prepare)
12649     (gnus-summary-goto-subject current)
12650     (gnus-summary-position-point)))
12651
12652 (defun gnus-summary-show-all-threads ()
12653   "Show all threads."
12654   (interactive)
12655   (gnus-set-global-variables)
12656   (save-excursion
12657     (let ((buffer-read-only nil))
12658       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12659   (gnus-summary-position-point))
12660
12661 (defun gnus-summary-show-thread ()
12662   "Show thread subtrees.
12663 Returns nil if no thread was there to be shown."
12664   (interactive)
12665   (gnus-set-global-variables)
12666   (let ((buffer-read-only nil)
12667         (orig (point))
12668         ;; first goto end then to beg, to have point at beg after let
12669         (end (progn (end-of-line) (point)))
12670         (beg (progn (beginning-of-line) (point))))
12671     (prog1
12672         ;; Any hidden lines here?
12673         (search-forward "\r" end t)
12674       (subst-char-in-region beg end ?\^M ?\n t)
12675       (goto-char orig)
12676       (gnus-summary-position-point))))
12677
12678 (defun gnus-summary-hide-all-threads ()
12679   "Hide all thread subtrees."
12680   (interactive)
12681   (gnus-set-global-variables)
12682   (save-excursion
12683     (goto-char (point-min))
12684     (gnus-summary-hide-thread)
12685     (while (zerop (gnus-summary-next-thread 1 t))
12686       (gnus-summary-hide-thread)))
12687   (gnus-summary-position-point))
12688
12689 (defun gnus-summary-hide-thread ()
12690   "Hide thread subtrees.
12691 Returns nil if no threads were there to be hidden."
12692   (interactive)
12693   (gnus-set-global-variables)
12694   (let ((buffer-read-only nil)
12695         (start (point))
12696         (article (gnus-summary-article-number)))
12697     (goto-char start)
12698     ;; Go forward until either the buffer ends or the subthread
12699     ;; ends.
12700     (when (and (not (eobp))
12701                (or (zerop (gnus-summary-next-thread 1 t))
12702                    (goto-char (point-max))))
12703       (prog1
12704           (if (and (> (point) start)
12705                    (search-backward "\n" start t))
12706               (progn
12707                 (subst-char-in-region start (point) ?\n ?\^M)
12708                 (gnus-summary-goto-subject article))
12709             (goto-char start)
12710             nil)
12711         ;;(gnus-summary-position-point)
12712         ))))
12713
12714 (defun gnus-summary-go-to-next-thread (&optional previous)
12715   "Go to the same level (or less) next thread.
12716 If PREVIOUS is non-nil, go to previous thread instead.
12717 Return the article number moved to, or nil if moving was impossible."
12718   (let ((level (gnus-summary-thread-level))
12719         (way (if previous -1 1))
12720         (beg (point)))
12721     (forward-line way)
12722     (while (and (not (eobp))
12723                 (< level (gnus-summary-thread-level)))
12724       (forward-line way))
12725     (if (eobp)
12726         (progn
12727           (goto-char beg)
12728           nil)
12729       (setq beg (point))
12730       (prog1
12731           (gnus-summary-article-number)
12732         (goto-char beg)))))
12733
12734 (defun gnus-summary-go-to-next-thread-old (&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   (if (and (eq gnus-summary-make-false-root 'dummy)
12739            (gnus-summary-article-intangible-p))
12740       (let ((beg (point)))
12741         (while (and (zerop (forward-line 1))
12742                     (not (gnus-summary-article-intangible-p))
12743                     (not (zerop (save-excursion 
12744                                   (gnus-summary-thread-level))))))
12745         (if (eobp)
12746             (progn
12747               (goto-char beg)
12748               nil)
12749           (point)))
12750     (let* ((level (gnus-summary-thread-level))
12751            (article (gnus-summary-article-number))
12752            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12753            oart)
12754       (while data
12755         (if (<= (gnus-data-level (car data)) level)
12756             (setq oart (gnus-data-number (car data))
12757                   data nil)
12758           (setq data (cdr data))))
12759       (and oart
12760            (gnus-summary-goto-subject oart)))))
12761
12762 (defun gnus-summary-next-thread (n &optional silent)
12763   "Go to the same level next N'th thread.
12764 If N is negative, search backward instead.
12765 Returns the difference between N and the number of skips actually
12766 done.
12767
12768 If SILENT, don't output messages."
12769   (interactive "p")
12770   (gnus-set-global-variables)
12771   (let ((backward (< n 0))
12772         (n (abs n))
12773         old dum int)
12774     (while (and (> n 0)
12775                 (gnus-summary-go-to-next-thread backward))
12776       (decf n))
12777     (unless silent 
12778       (gnus-summary-position-point))
12779     (when (and (not silent) (/= 0 n))
12780       (gnus-message 7 "No more threads"))
12781     n))
12782
12783 (defun gnus-summary-prev-thread (n)
12784   "Go to the same level previous N'th thread.
12785 Returns the difference between N and the number of skips actually
12786 done."
12787   (interactive "p")
12788   (gnus-set-global-variables)
12789   (gnus-summary-next-thread (- n)))
12790
12791 (defun gnus-summary-go-down-thread ()
12792   "Go down one level in the current thread."
12793   (let ((children (gnus-summary-article-children)))
12794     (and children
12795          (gnus-summary-goto-subject (car children)))))
12796
12797 (defun gnus-summary-go-up-thread ()
12798   "Go up one level in the current thread."
12799   (let ((parent (gnus-summary-article-parent)))
12800     (and parent
12801          (gnus-summary-goto-subject parent))))
12802
12803 (defun gnus-summary-down-thread (n)
12804   "Go down thread N steps.
12805 If N is negative, go up instead.
12806 Returns the difference between N and how many steps down that were
12807 taken."
12808   (interactive "p")
12809   (gnus-set-global-variables)
12810   (let ((up (< n 0))
12811         (n (abs n)))
12812     (while (and (> n 0)
12813                 (if up (gnus-summary-go-up-thread)
12814                   (gnus-summary-go-down-thread)))
12815       (setq n (1- n)))
12816     (gnus-summary-position-point)
12817     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12818     n))
12819
12820 (defun gnus-summary-up-thread (n)
12821   "Go up thread N steps.
12822 If N is negative, go up instead.
12823 Returns the difference between N and how many steps down that were
12824 taken."
12825   (interactive "p")
12826   (gnus-set-global-variables)
12827   (gnus-summary-down-thread (- n)))
12828
12829 (defun gnus-summary-top-thread ()
12830   "Go to the top of the thread."
12831   (interactive)
12832   (gnus-set-global-variables)
12833   (while (gnus-summary-go-up-thread))
12834   (gnus-summary-article-number))
12835
12836 (defun gnus-summary-kill-thread (&optional unmark)
12837   "Mark articles under current thread as read.
12838 If the prefix argument is positive, remove any kinds of marks.
12839 If the prefix argument is negative, tick articles instead."
12840   (interactive "P")
12841   (gnus-set-global-variables)
12842   (when unmark
12843     (setq unmark (prefix-numeric-value unmark)))
12844   (let ((articles (gnus-summary-articles-in-thread)))
12845     (save-excursion
12846       ;; Expand the thread.
12847       (gnus-summary-show-thread)
12848       ;; Mark all the articles.
12849       (while articles
12850         (gnus-summary-goto-subject (car articles))
12851         (cond ((null unmark)
12852                (gnus-summary-mark-article-as-read gnus-killed-mark))
12853               ((> unmark 0)
12854                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12855               (t
12856                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12857         (setq articles (cdr articles))))
12858     ;; Hide killed subtrees.
12859     (and (null unmark)
12860          gnus-thread-hide-killed
12861          (gnus-summary-hide-thread))
12862     ;; If marked as read, go to next unread subject.
12863     (if (null unmark)
12864         ;; Go to next unread subject.
12865         (gnus-summary-next-subject 1 t)))
12866   (gnus-set-mode-line 'summary))
12867
12868 ;; Summary sorting commands
12869
12870 (defun gnus-summary-sort-by-number (&optional reverse)
12871   "Sort summary buffer by article number.
12872 Argument REVERSE means reverse order."
12873   (interactive "P")
12874   (gnus-summary-sort 'number reverse))
12875
12876 (defun gnus-summary-sort-by-author (&optional reverse)
12877   "Sort summary buffer by author name alphabetically.
12878 If case-fold-search is non-nil, case of letters is ignored.
12879 Argument REVERSE means reverse order."
12880   (interactive "P")
12881   (gnus-summary-sort 'author reverse))
12882
12883 (defun gnus-summary-sort-by-subject (&optional reverse)
12884   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12885 If case-fold-search is non-nil, case of letters is ignored.
12886 Argument REVERSE means reverse order."
12887   (interactive "P")
12888   (gnus-summary-sort 'subject reverse))
12889
12890 (defun gnus-summary-sort-by-date (&optional reverse)
12891   "Sort summary buffer by date.
12892 Argument REVERSE means reverse order."
12893   (interactive "P")
12894   (gnus-summary-sort 'date reverse))
12895
12896 (defun gnus-summary-sort-by-score (&optional reverse)
12897   "Sort summary buffer by score.
12898 Argument REVERSE means reverse order."
12899   (interactive "P")
12900   (gnus-summary-sort 'score reverse))
12901
12902 (defun gnus-summary-sort (predicate reverse)
12903   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12904   (gnus-set-global-variables)
12905   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12906          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12907          (gnus-thread-sort-functions
12908           (list
12909            (if (not reverse)
12910                thread
12911              `(lambda (t1 t2)
12912                 (,thread t2 t1)))))
12913          (gnus-article-sort-functions
12914           (list
12915            (if (not reverse)
12916                article
12917              `(lambda (t1 t2)
12918                 (,article t2 t1)))))
12919          (buffer-read-only)
12920          (gnus-summary-prepare-hook nil))
12921     ;; We do the sorting by regenerating the threads.
12922     (gnus-summary-prepare)
12923     ;; Hide subthreads if needed.
12924     (when (and gnus-show-threads gnus-thread-hide-subtree)
12925       (gnus-summary-hide-all-threads)))
12926   ;; If in async mode, we send some info to the backend.
12927   (when gnus-newsgroup-async
12928     (gnus-request-asynchronous
12929      gnus-newsgroup-name gnus-newsgroup-data)))
12930
12931 (defun gnus-sortable-date (date)
12932   "Make sortable string by string-lessp from DATE.
12933 Timezone package is used."
12934   (condition-case ()
12935       (progn
12936         (setq date (inline (timezone-fix-time 
12937                             date nil 
12938                             (aref (inline (timezone-parse-date date)) 4))))
12939         (inline
12940           (timezone-make-sortable-date
12941            (aref date 0) (aref date 1) (aref date 2)
12942            (inline
12943              (timezone-make-time-string
12944               (aref date 3) (aref date 4) (aref date 5))))))
12945     (error "")))
12946   
12947 ;; Summary saving commands.
12948
12949 (defun gnus-summary-save-article (&optional n not-saved)
12950   "Save the current article using the default saver function.
12951 If N is a positive number, save the N next articles.
12952 If N is a negative number, save the N previous articles.
12953 If N is nil and any articles have been marked with the process mark,
12954 save those articles instead.
12955 The variable `gnus-default-article-saver' specifies the saver function."
12956   (interactive "P")
12957   (gnus-set-global-variables)
12958   (let ((articles (gnus-summary-work-articles n))
12959         (save-buffer (save-excursion 
12960                        (nnheader-set-temp-buffer " *Gnus Save*")))
12961         file header article)
12962     (while articles
12963       (setq header (gnus-summary-article-header
12964                     (setq article (pop articles))))
12965       (if (not (vectorp header))
12966           ;; This is a pseudo-article.
12967           (if (assq 'name header)
12968               (gnus-copy-file (cdr (assq 'name header)))
12969             (gnus-message 1 "Article %d is unsaveable" article))
12970         ;; This is a real article.
12971         (save-window-excursion
12972           (gnus-summary-select-article t nil nil article))
12973         (save-excursion
12974           (set-buffer save-buffer)
12975           (erase-buffer)
12976           (insert-buffer-substring gnus-original-article-buffer))
12977         (unless gnus-save-all-headers
12978           ;; Remove headers accoring to `gnus-saved-headers'.
12979           (let ((gnus-visible-headers
12980                  (or gnus-saved-headers gnus-visible-headers))
12981                 (gnus-article-buffer save-buffer))
12982             (gnus-article-hide-headers 1 t)))
12983         (save-window-excursion
12984           (if (not gnus-default-article-saver)
12985               (error "No default saver is defined.")
12986             ;; !!! Magic!  The saving functions all save
12987             ;; `gnus-original-article-buffer' (or so they think),
12988             ;; but we bind that variable to our save-buffer.
12989             (set-buffer gnus-article-buffer)
12990             (let ((gnus-original-article-buffer save-buffer))
12991               (set-buffer gnus-summary-buffer)
12992               (setq file (funcall
12993                           gnus-default-article-saver
12994                           (cond
12995                            ((not gnus-prompt-before-saving)
12996                             'default)
12997                            ((eq gnus-prompt-before-saving 'always)
12998                             nil)
12999                            (t file)))))))
13000         (gnus-summary-remove-process-mark article)
13001         (unless not-saved
13002           (gnus-summary-set-saved-mark article))))
13003     (gnus-kill-buffer save-buffer)
13004     (gnus-summary-position-point)
13005     n))
13006
13007 (defun gnus-summary-pipe-output (&optional arg)
13008   "Pipe the current article to a subprocess.
13009 If N is a positive number, pipe the N next articles.
13010 If N is a negative number, pipe the N previous articles.
13011 If N is nil and any articles have been marked with the process mark,
13012 pipe those articles instead."
13013   (interactive "P")
13014   (gnus-set-global-variables)
13015   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
13016     (gnus-summary-save-article arg t))
13017   (gnus-configure-windows 'pipe))
13018
13019 (defun gnus-summary-save-article-mail (&optional arg)
13020   "Append the current article to an mail file.
13021 If N is a positive number, save the N next articles.
13022 If N is a negative number, save the N previous articles.
13023 If N is nil and any articles have been marked with the process mark,
13024 save those articles instead."
13025   (interactive "P")
13026   (gnus-set-global-variables)
13027   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
13028     (gnus-summary-save-article arg)))
13029
13030 (defun gnus-summary-save-article-rmail (&optional arg)
13031   "Append the current article to an rmail file.
13032 If N is a positive number, save the N next articles.
13033 If N is a negative number, save the N previous articles.
13034 If N is nil and any articles have been marked with the process mark,
13035 save those articles instead."
13036   (interactive "P")
13037   (gnus-set-global-variables)
13038   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
13039     (gnus-summary-save-article arg)))
13040
13041 (defun gnus-summary-save-article-file (&optional arg)
13042   "Append the current article to a file.
13043 If N is a positive number, save the N next articles.
13044 If N is a negative number, save the N previous articles.
13045 If N is nil and any articles have been marked with the process mark,
13046 save those articles instead."
13047   (interactive "P")
13048   (gnus-set-global-variables)
13049   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
13050     (gnus-summary-save-article arg)))
13051
13052 (defun gnus-summary-save-article-body-file (&optional arg)
13053   "Append the current article body to a file.
13054 If N is a positive number, save the N next articles.
13055 If N is a negative number, save the N previous articles.
13056 If N is nil and any articles have been marked with the process mark,
13057 save those articles instead."
13058   (interactive "P")
13059   (gnus-set-global-variables)
13060   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
13061     (gnus-summary-save-article arg)))
13062
13063 (defun gnus-get-split-value (methods)
13064   "Return a value based on the split METHODS."
13065   (let (split-name method result match)
13066     (when methods
13067       (save-excursion
13068         (set-buffer gnus-original-article-buffer)
13069         (save-restriction
13070           (nnheader-narrow-to-headers)
13071           (while methods
13072             (goto-char (point-min))
13073             (setq method (pop methods))
13074             (setq match (car method))
13075             (when (cond
13076                    ((stringp match)
13077                     ;; Regular expression.
13078                     (condition-case ()
13079                         (re-search-forward match nil t)
13080                       (error nil)))
13081                    ((gnus-functionp match)
13082                     ;; Function.
13083                     (save-restriction
13084                       (widen)
13085                       (setq result (funcall match gnus-newsgroup-name))))
13086                    ((consp match)
13087                     ;; Form.
13088                     (save-restriction
13089                       (widen)
13090                       (setq result (eval match)))))
13091               (setq split-name (append (cdr method) split-name))
13092               (cond ((stringp result)
13093                      (push result split-name))
13094                     ((consp result)
13095                      (setq split-name (append result split-name)))))))))
13096     split-name))
13097
13098 (defun gnus-read-move-group-name (prompt default articles prefix)
13099   "Read a group name."
13100   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
13101          (minibuffer-confirm-incomplete nil) ; XEmacs
13102          group-map
13103          (dum (mapatoms
13104                (lambda (g) 
13105                  (and (boundp g)
13106                       (symbol-name g)
13107                       (memq 'respool
13108                             (assoc (symbol-name
13109                                     (car (gnus-find-method-for-group
13110                                           (symbol-name g))))
13111                                    gnus-valid-select-methods))
13112                       (push (list (symbol-name g)) group-map)))
13113                gnus-active-hashtb))
13114          (prom
13115           (format "%s %s to:"
13116                   prompt
13117                   (if (> (length articles) 1)
13118                       (format "these %d articles" (length articles))
13119                     "this article")))
13120          (to-newsgroup
13121           (cond
13122            ((null split-name)
13123             (gnus-completing-read default prom
13124                                   group-map nil nil prefix
13125                                   'gnus-group-history))
13126            ((= 1 (length split-name))
13127             (gnus-completing-read (car split-name) prom group-map
13128                                   nil nil nil
13129                                   'gnus-group-history))
13130            (t
13131             (gnus-completing-read nil prom 
13132                                   (mapcar (lambda (el) (list el))
13133                                           (nreverse split-name))
13134                                   nil nil nil
13135                                   'gnus-group-history)))))
13136     (when to-newsgroup
13137       (if (or (string= to-newsgroup "")
13138               (string= to-newsgroup prefix))
13139           (setq to-newsgroup (or default "")))
13140       (or (gnus-active to-newsgroup)
13141           (gnus-activate-group to-newsgroup)
13142           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
13143                                      to-newsgroup))
13144               (or (gnus-request-create-group to-newsgroup)
13145                   (error "Couldn't create group %s" to-newsgroup)))
13146           (error "No such group: %s" to-newsgroup)))
13147     to-newsgroup))
13148
13149 (defun gnus-read-save-file-name (prompt default-name)
13150   (let* ((split-name (gnus-get-split-value gnus-split-methods))
13151          (file
13152           ;; Let the split methods have their say.
13153           (cond
13154            ;; No split name was found.
13155            ((null split-name)
13156             (read-file-name
13157              (concat prompt " (default "
13158                      (file-name-nondirectory default-name) ") ")
13159              (file-name-directory default-name)
13160              default-name))
13161            ;; A single split name was found
13162            ((= 1 (length split-name))
13163             (let* ((name (car split-name))
13164                    (dir (cond ((file-directory-p name)
13165                                (file-name-as-directory name))
13166                               ((file-exists-p name) name)
13167                               (t gnus-article-save-directory))))
13168               (read-file-name
13169                (concat prompt " (default " name ") ")
13170                dir name)))
13171            ;; A list of splits was found.
13172            (t
13173             (setq split-name (nreverse split-name))
13174             (let (result)
13175               (let ((file-name-history (nconc split-name file-name-history)))
13176                 (setq result
13177                       (read-file-name
13178                        (concat prompt " (`M-p' for defaults) ")
13179                        gnus-article-save-directory
13180                        (car split-name))))
13181               (car (push result file-name-history)))))))
13182     ;; If we have read a directory, we append the default file name.
13183     (when (file-directory-p file)
13184       (setq file (concat (file-name-as-directory file)
13185                          (file-name-nondirectory default-name))))
13186     ;; Possibly translate some charaters.
13187     (nnheader-translate-file-chars file)))
13188
13189 (defun gnus-article-archive-name (group)
13190   "Return the first instance of an \"Archive-name\" in the current buffer."
13191   (let ((case-fold-search t))
13192     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
13193       (match-string 1))))
13194
13195 (defun gnus-summary-save-in-rmail (&optional filename)
13196   "Append this article to Rmail file.
13197 Optional argument FILENAME specifies file name.
13198 Directory to save to is default to `gnus-article-save-directory'."
13199   (interactive)
13200   (gnus-set-global-variables)
13201   (let ((default-name
13202           (funcall gnus-rmail-save-name gnus-newsgroup-name
13203                    gnus-current-headers gnus-newsgroup-last-rmail)))
13204     (setq filename
13205           (cond ((eq filename 'default)
13206                  default-name)
13207                 (filename filename)
13208                 (t (gnus-read-save-file-name
13209                     "Save in rmail file:" default-name))))
13210     (gnus-make-directory (file-name-directory filename))
13211     (gnus-eval-in-buffer-window gnus-original-article-buffer
13212       (save-excursion
13213         (save-restriction
13214           (widen)
13215           (gnus-output-to-rmail filename))))
13216     ;; Remember the directory name to save articles
13217     (setq gnus-newsgroup-last-rmail filename)))
13218
13219 (defun gnus-summary-save-in-mail (&optional filename)
13220   "Append this article to Unix mail 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-mail-save-name gnus-newsgroup-name
13227                    gnus-current-headers gnus-newsgroup-last-mail)))
13228     (setq filename
13229           (cond ((eq filename 'default)
13230                  default-name)
13231                 (filename filename)
13232                 (t (gnus-read-save-file-name
13233                     "Save in Unix mail file:" default-name))))
13234     (setq filename
13235           (expand-file-name filename
13236                             (and default-name
13237                                  (file-name-directory default-name))))
13238     (gnus-make-directory (file-name-directory filename))
13239     (gnus-eval-in-buffer-window gnus-original-article-buffer
13240       (save-excursion
13241         (save-restriction
13242           (widen)
13243           (if (and (file-readable-p filename) (mail-file-babyl-p filename))
13244               (gnus-output-to-rmail filename)
13245             (let ((mail-use-rfc822 t))
13246               (rmail-output filename 1 t t))))))
13247     ;; Remember the directory name to save articles.
13248     (setq gnus-newsgroup-last-mail filename)))
13249
13250 (defun gnus-summary-save-in-file (&optional filename)
13251   "Append this article to file.
13252 Optional argument FILENAME specifies file name.
13253 Directory to save to is default to `gnus-article-save-directory'."
13254   (interactive)
13255   (gnus-set-global-variables)
13256   (let ((default-name
13257           (funcall gnus-file-save-name gnus-newsgroup-name
13258                    gnus-current-headers gnus-newsgroup-last-file)))
13259     (setq filename
13260           (cond ((eq filename 'default)
13261                  default-name)
13262                 (filename filename)
13263                 (t (gnus-read-save-file-name
13264                     "Save in file:" default-name))))
13265     (gnus-make-directory (file-name-directory filename))
13266     (gnus-eval-in-buffer-window gnus-original-article-buffer
13267       (save-excursion
13268         (save-restriction
13269           (widen)
13270           (gnus-output-to-file filename))))
13271     ;; Remember the directory name to save articles.
13272     (setq gnus-newsgroup-last-file filename)))
13273
13274 (defun gnus-summary-save-body-in-file (&optional filename)
13275   "Append this article body to a file.
13276 Optional argument FILENAME specifies file name.
13277 The directory to save in defaults 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 body in file:" default-name))))
13289     (gnus-make-directory (file-name-directory filename))
13290     (gnus-eval-in-buffer-window gnus-article-buffer
13291       (save-excursion
13292         (save-restriction
13293           (widen)
13294           (goto-char (point-min))
13295           (and (search-forward "\n\n" nil t)
13296                (narrow-to-region (point) (point-max)))
13297           (gnus-output-to-file filename))))
13298     ;; Remember the directory name to save articles.
13299     (setq gnus-newsgroup-last-file filename)))
13300
13301 (defun gnus-summary-save-in-pipe (&optional command)
13302   "Pipe this article to subprocess."
13303   (interactive)
13304   (gnus-set-global-variables)
13305   (setq command
13306         (cond ((eq command 'default)
13307                gnus-last-shell-command)
13308               (command command)
13309               (t (read-string "Shell command on article: "
13310                               gnus-last-shell-command))))
13311   (if (string-equal command "")
13312       (setq command gnus-last-shell-command))
13313   (gnus-eval-in-buffer-window gnus-article-buffer
13314     (save-restriction
13315       (widen)
13316       (shell-command-on-region (point-min) (point-max) command nil)))
13317   (setq gnus-last-shell-command command))
13318
13319 ;; Summary extract commands
13320
13321 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
13322   (let ((buffer-read-only nil)
13323         (article (gnus-summary-article-number))
13324         after-article b e)
13325     (or (gnus-summary-goto-subject article)
13326         (error (format "No such article: %d" article)))
13327     (gnus-summary-position-point)
13328     ;; If all commands are to be bunched up on one line, we collect
13329     ;; them here.
13330     (if gnus-view-pseudos-separately
13331         ()
13332       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
13333             files action)
13334         (while ps
13335           (setq action (cdr (assq 'action (car ps))))
13336           (setq files (list (cdr (assq 'name (car ps)))))
13337           (while (and ps (cdr ps)
13338                       (string= (or action "1")
13339                                (or (cdr (assq 'action (cadr ps))) "2")))
13340             (setq files (cons (cdr (assq 'name (cadr ps))) files))
13341             (setcdr ps (cddr ps)))
13342           (if (not files)
13343               ()
13344             (if (not (string-match "%s" action))
13345                 (setq files (cons " " files)))
13346             (setq files (cons " " files))
13347             (and (assq 'execute (car ps))
13348                  (setcdr (assq 'execute (car ps))
13349                          (funcall (if (string-match "%s" action)
13350                                       'format 'concat)
13351                                   action
13352                                   (mapconcat (lambda (f) f) files " ")))))
13353           (setq ps (cdr ps)))))
13354     (if (and gnus-view-pseudos (not not-view))
13355         (while pslist
13356           (and (assq 'execute (car pslist))
13357                (gnus-execute-command (cdr (assq 'execute (car pslist)))
13358                                      (eq gnus-view-pseudos 'not-confirm)))
13359           (setq pslist (cdr pslist)))
13360       (save-excursion
13361         (while pslist
13362           (setq after-article (or (cdr (assq 'article (car pslist)))
13363                                   (gnus-summary-article-number)))
13364           (gnus-summary-goto-subject after-article)
13365           (forward-line 1)
13366           (setq b (point))
13367           (insert "    " (file-name-nondirectory
13368                                 (cdr (assq 'name (car pslist))))
13369                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
13370           (setq e (point))
13371           (forward-line -1)             ; back to `b'
13372           (gnus-add-text-properties
13373            b (1- e) (list 'gnus-number gnus-reffed-article-number
13374                           gnus-mouse-face-prop gnus-mouse-face))
13375           (gnus-data-enter
13376            after-article gnus-reffed-article-number
13377            gnus-unread-mark b (car pslist) 0 (- e b))
13378           (push gnus-reffed-article-number gnus-newsgroup-unreads)
13379           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
13380           (setq pslist (cdr pslist)))))))
13381
13382 (defun gnus-pseudos< (p1 p2)
13383   (let ((c1 (cdr (assq 'action p1)))
13384         (c2 (cdr (assq 'action p2))))
13385     (and c1 c2 (string< c1 c2))))
13386
13387 (defun gnus-request-pseudo-article (props)
13388   (cond ((assq 'execute props)
13389          (gnus-execute-command (cdr (assq 'execute props)))))
13390   (let ((gnus-current-article (gnus-summary-article-number)))
13391     (run-hooks 'gnus-mark-article-hook)))
13392
13393 (defun gnus-execute-command (command &optional automatic)
13394   (save-excursion
13395     (gnus-article-setup-buffer)
13396     (set-buffer gnus-article-buffer)
13397     (setq buffer-read-only nil)
13398     (let ((command (if automatic command (read-string "Command: " command)))
13399           ;; Just binding this here doesn't help, because there might
13400           ;; be output from the process after exiting the scope of 
13401           ;; this `let'.
13402           ;; (buffer-read-only nil)
13403           )
13404       (erase-buffer)
13405       (insert "$ " command "\n\n")
13406       (if gnus-view-pseudo-asynchronously
13407           (start-process "gnus-execute" nil shell-file-name
13408                          shell-command-switch command)
13409         (call-process shell-file-name nil t nil
13410                       shell-command-switch command)))))
13411
13412 (defun gnus-copy-file (file &optional to)
13413   "Copy FILE to TO."
13414   (interactive
13415    (list (read-file-name "Copy file: " default-directory)
13416          (read-file-name "Copy file to: " default-directory)))
13417   (gnus-set-global-variables)
13418   (or to (setq to (read-file-name "Copy file to: " default-directory)))
13419   (and (file-directory-p to)
13420        (setq to (concat (file-name-as-directory to)
13421                         (file-name-nondirectory file))))
13422   (copy-file file to))
13423
13424 ;; Summary kill commands.
13425
13426 (defun gnus-summary-edit-global-kill (article)
13427   "Edit the \"global\" kill file."
13428   (interactive (list (gnus-summary-article-number)))
13429   (gnus-set-global-variables)
13430   (gnus-group-edit-global-kill article))
13431
13432 (defun gnus-summary-edit-local-kill ()
13433   "Edit a local kill file applied to the current newsgroup."
13434   (interactive)
13435   (gnus-set-global-variables)
13436   (setq gnus-current-headers (gnus-summary-article-header))
13437   (gnus-set-global-variables)
13438   (gnus-group-edit-local-kill
13439    (gnus-summary-article-number) gnus-newsgroup-name))
13440
13441 \f
13442 ;;;
13443 ;;; Gnus article mode
13444 ;;;
13445
13446 (put 'gnus-article-mode 'mode-class 'special)
13447
13448 (if gnus-article-mode-map
13449     nil
13450   (setq gnus-article-mode-map (make-keymap))
13451   (suppress-keymap gnus-article-mode-map)
13452
13453   (gnus-define-keys gnus-article-mode-map
13454     " " gnus-article-goto-next-page
13455     "\177" gnus-article-goto-prev-page
13456     [delete] gnus-article-goto-prev-page
13457     "\C-c^" gnus-article-refer-article
13458     "h" gnus-article-show-summary
13459     "s" gnus-article-show-summary
13460     "\C-c\C-m" gnus-article-mail
13461     "?" gnus-article-describe-briefly
13462     gnus-mouse-2 gnus-article-push-button
13463     "\r" gnus-article-press-button
13464     "\t" gnus-article-next-button
13465     "\M-\t" gnus-article-prev-button
13466     "\C-c\C-b" gnus-bug)
13467
13468   (substitute-key-definition
13469    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13470
13471 (defun gnus-article-mode ()
13472   "Major mode for displaying an article.
13473
13474 All normal editing commands are switched off.
13475
13476 The following commands are available:
13477
13478 \\<gnus-article-mode-map>
13479 \\[gnus-article-next-page]\t Scroll the article one page forwards
13480 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13481 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13482 \\[gnus-article-show-summary]\t Display the summary buffer
13483 \\[gnus-article-mail]\t Send a reply to the address near point
13484 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13485 \\[gnus-info-find-node]\t Go to the Gnus info node"
13486   (interactive)
13487   (when (and menu-bar-mode
13488              (gnus-visual-p 'article-menu 'menu))
13489     (gnus-article-make-menu-bar))
13490   (kill-all-local-variables)
13491   (gnus-simplify-mode-line)
13492   (setq mode-name "Article")
13493   (setq major-mode 'gnus-article-mode)
13494   (make-local-variable 'minor-mode-alist)
13495   (or (assq 'gnus-show-mime minor-mode-alist)
13496       (setq minor-mode-alist
13497             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13498   (use-local-map gnus-article-mode-map)
13499   (make-local-variable 'page-delimiter)
13500   (setq page-delimiter gnus-page-delimiter)
13501   (buffer-disable-undo (current-buffer))
13502   (setq buffer-read-only t)             ;Disable modification
13503   (run-hooks 'gnus-article-mode-hook))
13504
13505 (defun gnus-article-setup-buffer ()
13506   "Initialize the article buffer."
13507   (let* ((name (if gnus-single-article-buffer "*Article*"
13508                  (concat "*Article " gnus-newsgroup-name "*")))
13509          (original
13510           (progn (string-match "\\*Article" name)
13511                  (concat " *Original Article"
13512                          (substring name (match-end 0))))))
13513     (setq gnus-article-buffer name)
13514     (setq gnus-original-article-buffer original)
13515     ;; This might be a variable local to the summary buffer.
13516     (unless gnus-single-article-buffer
13517       (save-excursion
13518         (set-buffer gnus-summary-buffer)
13519         (setq gnus-article-buffer name)
13520         (setq gnus-original-article-buffer original)
13521         (gnus-set-global-variables))
13522       (make-local-variable 'gnus-summary-buffer))
13523     ;; Init original article buffer.
13524     (save-excursion
13525       (set-buffer (get-buffer-create gnus-original-article-buffer))
13526       (buffer-disable-undo (current-buffer))
13527       (setq major-mode 'gnus-original-article-mode)
13528       (make-local-variable 'gnus-original-article))
13529     (if (get-buffer name)
13530         (save-excursion
13531           (set-buffer name)
13532           (buffer-disable-undo (current-buffer))
13533           (setq buffer-read-only t)
13534           (gnus-add-current-to-buffer-list)
13535           (or (eq major-mode 'gnus-article-mode)
13536               (gnus-article-mode))
13537           (current-buffer))
13538       (save-excursion
13539         (set-buffer (get-buffer-create name))
13540         (gnus-add-current-to-buffer-list)
13541         (gnus-article-mode)
13542         (current-buffer)))))
13543
13544 ;; Set article window start at LINE, where LINE is the number of lines
13545 ;; from the head of the article.
13546 (defun gnus-article-set-window-start (&optional line)
13547   (set-window-start
13548    (get-buffer-window gnus-article-buffer t)
13549    (save-excursion
13550      (set-buffer gnus-article-buffer)
13551      (goto-char (point-min))
13552      (if (not line)
13553          (point-min)
13554        (gnus-message 6 "Moved to bookmark")
13555        (search-forward "\n\n" nil t)
13556        (forward-line line)
13557        (point)))))
13558
13559 (defun gnus-kill-all-overlays ()
13560   "Delete all overlays in the current buffer."
13561   (when (fboundp 'overlay-lists)
13562     (let* ((overlayss (overlay-lists))
13563            (buffer-read-only nil)
13564            (overlays (nconc (car overlayss) (cdr overlayss))))
13565       (while overlays
13566         (delete-overlay (pop overlays))))))
13567
13568 (defun gnus-request-article-this-buffer (article group)
13569   "Get an article and insert it into this buffer."
13570   (let (do-update-line)
13571     (prog1
13572         (save-excursion
13573           (erase-buffer)
13574           (gnus-kill-all-overlays)
13575           (setq group (or group gnus-newsgroup-name))
13576
13577           ;; Open server if it has closed.
13578           (gnus-check-server (gnus-find-method-for-group group))
13579
13580           ;; Using `gnus-request-article' directly will insert the article into
13581           ;; `nntp-server-buffer' - so we'll save some time by not having to
13582           ;; copy it from the server buffer into the article buffer.
13583
13584           ;; We only request an article by message-id when we do not have the
13585           ;; headers for it, so we'll have to get those.
13586           (when (stringp article)
13587             (let ((gnus-override-method gnus-refer-article-method))
13588               (gnus-read-header article)))
13589
13590           ;; If the article number is negative, that means that this article
13591           ;; doesn't belong in this newsgroup (possibly), so we find its
13592           ;; message-id and request it by id instead of number.
13593           (when (and (numberp article)
13594                      gnus-summary-buffer
13595                      (get-buffer gnus-summary-buffer)
13596                      (buffer-name (get-buffer gnus-summary-buffer)))
13597             (save-excursion
13598               (set-buffer gnus-summary-buffer)
13599               (let ((header (gnus-summary-article-header article)))
13600                 (if (< article 0)
13601                     (cond 
13602                      ((memq article gnus-newsgroup-sparse)
13603                       ;; This is a sparse gap article.
13604                       (setq do-update-line article)
13605                       (setq article (mail-header-id header))
13606                       (let ((gnus-override-method gnus-refer-article-method))
13607                         (gnus-read-header article))
13608                       (setq gnus-newsgroup-sparse
13609                             (delq article gnus-newsgroup-sparse)))
13610                      ((vectorp header)
13611                       ;; It's a real article.
13612                       (setq article (mail-header-id header)))
13613                      (t
13614                       ;; It is an extracted pseudo-article.
13615                       (setq article 'pseudo)
13616                       (gnus-request-pseudo-article header))))
13617                 
13618                 (let ((method (gnus-find-method-for-group 
13619                                gnus-newsgroup-name)))
13620                   (if (not (eq (car method) 'nneething))
13621                       ()
13622                     (let ((dir (concat (file-name-as-directory (nth 1 method))
13623                                        (mail-header-subject header))))
13624                       (if (file-directory-p dir)
13625                           (progn
13626                             (setq article 'nneething)
13627                             (gnus-group-enter-directory dir)))))))))
13628
13629           (cond
13630            ;; Refuse to select canceled articles.
13631            ((and (numberp article)
13632                  gnus-summary-buffer
13633                  (get-buffer gnus-summary-buffer)
13634                  (buffer-name (get-buffer gnus-summary-buffer))
13635                  (eq (cdr (save-excursion
13636                             (set-buffer gnus-summary-buffer)
13637                             (assq article gnus-newsgroup-reads)))
13638                      gnus-canceled-mark))
13639             nil)
13640            ;; We first check `gnus-original-article-buffer'.
13641            ((and (get-buffer gnus-original-article-buffer)
13642                  (numberp article)
13643                  (save-excursion
13644                    (set-buffer gnus-original-article-buffer)
13645                    (and (equal (car gnus-original-article) group)
13646                         (eq (cdr gnus-original-article) article))))
13647             (insert-buffer-substring gnus-original-article-buffer)
13648             'article)
13649            ;; Check the backlog.
13650            ((and gnus-keep-backlog
13651                  (gnus-backlog-request-article group article (current-buffer)))
13652             'article)
13653            ;; Check the cache.
13654            ((and gnus-use-cache
13655                  (numberp article)
13656                  (gnus-cache-request-article article group))
13657             'article)
13658            ;; Get the article and put into the article buffer.
13659            ((or (stringp article) (numberp article))
13660             (let ((gnus-override-method
13661                    (and (stringp article) gnus-refer-article-method))
13662                   (buffer-read-only nil))
13663               (erase-buffer)
13664               (gnus-kill-all-overlays)
13665               (if (gnus-request-article article group (current-buffer))
13666                   (progn
13667                     (and gnus-keep-backlog
13668                          (numberp article)
13669                          (gnus-backlog-enter-article
13670                           group article (current-buffer)))
13671                     'article))))
13672            ;; It was a pseudo.
13673            (t article)))
13674
13675       ;; Take the article from the original article buffer
13676       ;; and place it in the buffer it's supposed to be in.
13677       (when (and (get-buffer gnus-article-buffer)
13678                  ;;(numberp article)
13679                  (equal (buffer-name (current-buffer))
13680                         (buffer-name (get-buffer gnus-article-buffer))))
13681         (save-excursion
13682           (if (get-buffer gnus-original-article-buffer)
13683               (set-buffer (get-buffer gnus-original-article-buffer))
13684             (set-buffer (get-buffer-create gnus-original-article-buffer))
13685             (buffer-disable-undo (current-buffer))
13686             (setq major-mode 'gnus-original-article-mode)
13687             (setq buffer-read-only t)
13688             (gnus-add-current-to-buffer-list))
13689           (let (buffer-read-only)
13690             (erase-buffer)
13691             (insert-buffer-substring gnus-article-buffer))
13692           (setq gnus-original-article (cons group article))))
13693     
13694       ;; Update sparse articles.
13695       (when (and do-update-line
13696                  (or (numberp article)
13697                      (stringp article)))
13698         (let ((buf (current-buffer)))
13699           (set-buffer gnus-summary-buffer)
13700           (gnus-summary-update-article do-update-line)
13701           (gnus-summary-goto-subject do-update-line nil t)
13702           (set-window-point (get-buffer-window (current-buffer) t)
13703                             (point))
13704           (set-buffer buf))))))
13705
13706 (defun gnus-read-header (id &optional header)
13707   "Read the headers of article ID and enter them into the Gnus system."
13708   (let ((group gnus-newsgroup-name)
13709         (gnus-override-method 
13710          (and (gnus-news-group-p gnus-newsgroup-name)
13711               gnus-refer-article-method))       
13712         where)
13713     ;; First we check to see whether the header in question is already
13714     ;; fetched.
13715     (if (stringp id)
13716         ;; This is a Message-ID.
13717         (setq header (or header (gnus-id-to-header id)))
13718       ;; This is an article number.
13719       (setq header (or header (gnus-summary-article-header id))))
13720     (if (and header
13721              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
13722         ;; We have found the header.
13723         header
13724       ;; We have to really fetch the header to this article.
13725       (when (setq where (gnus-request-head id group))
13726         (save-excursion
13727           (set-buffer nntp-server-buffer)
13728           (goto-char (point-max))
13729           (insert ".\n")
13730           (goto-char (point-min))
13731           (insert "211 ")
13732           (princ (cond
13733                   ((numberp id) id)
13734                   ((cdr where) (cdr where))
13735                   (header (mail-header-number header))
13736                   (t gnus-reffed-article-number))
13737                  (current-buffer))
13738           (insert " Article retrieved.\n"))
13739         ;(when (and header
13740         ;          (memq (mail-header-number header) gnus-newsgroup-sparse))
13741         ;  (setcar (gnus-id-to-thread id) nil))
13742         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13743             ()                          ; Malformed head.
13744           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
13745             (if (and (stringp id)
13746                      (not (string= (gnus-group-real-name group)
13747                                    (car where))))
13748                 ;; If we fetched by Message-ID and the article came
13749                 ;; from a different group, we fudge some bogus article
13750                 ;; numbers for this article.
13751                 (mail-header-set-number header gnus-reffed-article-number))
13752             (decf gnus-reffed-article-number)
13753             (gnus-remove-header (mail-header-number header))
13754             (push header gnus-newsgroup-headers)
13755             (setq gnus-current-headers header)
13756             (push (mail-header-number header) gnus-newsgroup-limit))
13757           header)))))
13758
13759 (defun gnus-remove-header (number)
13760   "Remove header NUMBER from `gnus-newsgroup-headers'."
13761   (if (and gnus-newsgroup-headers
13762            (= number (mail-header-number (car gnus-newsgroup-headers))))
13763       (pop gnus-newsgroup-headers)
13764     (let ((headers gnus-newsgroup-headers))
13765       (while (and (cdr headers)
13766                   (not (= number (mail-header-number (cadr headers)))))
13767         (pop headers))
13768       (when (cdr headers)
13769         (setcdr headers (cddr headers))))))
13770
13771 (defun gnus-article-prepare (article &optional all-headers header)
13772   "Prepare ARTICLE in article mode buffer.
13773 ARTICLE should either be an article number or a Message-ID.
13774 If ARTICLE is an id, HEADER should be the article headers.
13775 If ALL-HEADERS is non-nil, no headers are hidden."
13776   (save-excursion
13777     ;; Make sure we start in a summary buffer.
13778     (unless (eq major-mode 'gnus-summary-mode)
13779       (set-buffer gnus-summary-buffer))
13780     (setq gnus-summary-buffer (current-buffer))
13781     ;; Make sure the connection to the server is alive.
13782     (unless (gnus-server-opened
13783              (gnus-find-method-for-group gnus-newsgroup-name))
13784       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13785       (gnus-request-group gnus-newsgroup-name t))
13786     (let* ((article (if header (mail-header-number header) article))
13787            (summary-buffer (current-buffer))
13788            (internal-hook gnus-article-internal-prepare-hook)
13789            (group gnus-newsgroup-name)
13790            result)
13791       (save-excursion
13792         (gnus-article-setup-buffer)
13793         (set-buffer gnus-article-buffer)
13794         ;; Deactivate active regions.
13795         (when (and (boundp 'transient-mark-mode)
13796                    transient-mark-mode)
13797           (setq mark-active nil))
13798         (if (not (setq result (let ((buffer-read-only nil))
13799                                 (gnus-request-article-this-buffer
13800                                  article group))))
13801             ;; There is no such article.
13802             (save-excursion
13803               (when (and (numberp article)
13804                          (not (memq article gnus-newsgroup-sparse)))
13805                 (setq gnus-article-current
13806                       (cons gnus-newsgroup-name article))
13807                 (set-buffer gnus-summary-buffer)
13808                 (setq gnus-current-article article)
13809                 (gnus-summary-mark-article article gnus-canceled-mark))
13810               (unless (memq article gnus-newsgroup-sparse)
13811                 (gnus-error
13812                  1 "No such article (may have expired or been canceled)")))
13813           (if (or (eq result 'pseudo) (eq result 'nneething))
13814               (progn
13815                 (save-excursion
13816                   (set-buffer summary-buffer)
13817                   (setq gnus-last-article gnus-current-article
13818                         gnus-newsgroup-history (cons gnus-current-article
13819                                                      gnus-newsgroup-history)
13820                         gnus-current-article 0
13821                         gnus-current-headers nil
13822                         gnus-article-current nil)
13823                   (if (eq result 'nneething)
13824                       (gnus-configure-windows 'summary)
13825                     (gnus-configure-windows 'article))
13826                   (gnus-set-global-variables))
13827                 (gnus-set-mode-line 'article))
13828             ;; The result from the `request' was an actual article -
13829             ;; or at least some text that is now displayed in the
13830             ;; article buffer.
13831             (if (and (numberp article)
13832                      (not (eq article gnus-current-article)))
13833                 ;; Seems like a new article has been selected.
13834                 ;; `gnus-current-article' must be an article number.
13835                 (save-excursion
13836                   (set-buffer summary-buffer)
13837                   (setq gnus-last-article gnus-current-article
13838                         gnus-newsgroup-history (cons gnus-current-article
13839                                                      gnus-newsgroup-history)
13840                         gnus-current-article article
13841                         gnus-current-headers
13842                         (gnus-summary-article-header gnus-current-article)
13843                         gnus-article-current
13844                         (cons gnus-newsgroup-name gnus-current-article))
13845                   (unless (vectorp gnus-current-headers)
13846                     (setq gnus-current-headers nil))
13847                   (gnus-summary-show-thread)
13848                   (run-hooks 'gnus-mark-article-hook)
13849                   (gnus-set-mode-line 'summary)
13850                   (and (gnus-visual-p 'article-highlight 'highlight)
13851                        (run-hooks 'gnus-visual-mark-article-hook))
13852                   ;; Set the global newsgroup variables here.
13853                   ;; Suggested by Jim Sisolak
13854                   ;; <sisolak@trans4.neep.wisc.edu>.
13855                   (gnus-set-global-variables)
13856                   (setq gnus-have-all-headers
13857                         (or all-headers gnus-show-all-headers))
13858                   (and gnus-use-cache
13859                        (vectorp (gnus-summary-article-header article))
13860                        (gnus-cache-possibly-enter-article
13861                         group article
13862                         (gnus-summary-article-header article)
13863                         (memq article gnus-newsgroup-marked)
13864                         (memq article gnus-newsgroup-dormant)
13865                         (memq article gnus-newsgroup-unreads)))))
13866             (when (or (numberp article)
13867                       (stringp article))
13868               ;; Hooks for getting information from the article.
13869               ;; This hook must be called before being narrowed.
13870               (let (buffer-read-only)
13871                 (run-hooks 'internal-hook)
13872                 (run-hooks 'gnus-article-prepare-hook)
13873                 ;; Decode MIME message.
13874                 (if gnus-show-mime
13875                     (if (or (not gnus-strict-mime)
13876                             (gnus-fetch-field "Mime-Version"))
13877                         (funcall gnus-show-mime-method)
13878                       (funcall gnus-decode-encoded-word-method)))
13879                 ;; Perform the article display hooks.
13880                 (run-hooks 'gnus-article-display-hook))
13881               ;; Do page break.
13882               (goto-char (point-min))
13883               (and gnus-break-pages (gnus-narrow-to-page)))
13884             (gnus-set-mode-line 'article)
13885             (gnus-configure-windows 'article)
13886             (goto-char (point-min))
13887             t))))))
13888
13889 (defun gnus-article-show-all-headers ()
13890   "Show all article headers in article mode buffer."
13891   (save-excursion
13892     (gnus-article-setup-buffer)
13893     (set-buffer gnus-article-buffer)
13894     (let ((buffer-read-only nil))
13895       (gnus-unhide-text (point-min) (point-max)))))
13896
13897 (defun gnus-article-hide-headers-if-wanted ()
13898   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13899 Provided for backwards compatibility."
13900   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13901       gnus-inhibit-hiding
13902       (gnus-article-hide-headers)))
13903
13904 (defsubst gnus-article-header-rank ()
13905   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13906   (let ((list gnus-sorted-header-list)
13907         (i 0))
13908     (while list
13909       (when (looking-at (car list))
13910         (setq list nil))
13911       (setq list (cdr list))
13912       (incf i))
13913     i))
13914
13915 (defun gnus-article-hide-headers (&optional arg delete)
13916   "Toggle whether to hide unwanted headers and possibly sort them as well.
13917 If given a negative prefix, always show; if given a positive prefix,
13918 always hide."
13919   (interactive (gnus-hidden-arg))
13920   (if (gnus-article-check-hidden-text 'headers arg)
13921       ;; Show boring headers as well.
13922       (gnus-article-show-hidden-text 'boring-headers)
13923     ;; This function might be inhibited.
13924     (unless gnus-inhibit-hiding
13925       (save-excursion
13926         (set-buffer gnus-article-buffer)
13927         (save-restriction
13928           (let ((buffer-read-only nil)
13929                 (props (nconc (list 'gnus-type 'headers)
13930                               gnus-hidden-properties))
13931                 (max (1+ (length gnus-sorted-header-list)))
13932                 (ignored (when (not (stringp gnus-visible-headers))
13933                            (cond ((stringp gnus-ignored-headers)
13934                                   gnus-ignored-headers)
13935                                  ((listp gnus-ignored-headers)
13936                                   (mapconcat 'identity gnus-ignored-headers
13937                                              "\\|")))))
13938                 (visible
13939                  (cond ((stringp gnus-visible-headers)
13940                         gnus-visible-headers)
13941                        ((and gnus-visible-headers
13942                              (listp gnus-visible-headers))
13943                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13944                 (inhibit-point-motion-hooks t)
13945                 want-list beg)
13946             ;; First we narrow to just the headers.
13947             (widen)
13948             (goto-char (point-min))
13949             ;; Hide any "From " lines at the beginning of (mail) articles.
13950             (while (looking-at "From ")
13951               (forward-line 1))
13952             (unless (bobp)
13953               (if delete
13954                   (delete-region (point-min) (point))
13955                 (gnus-hide-text (point-min) (point) props)))
13956             ;; Then treat the rest of the header lines.
13957             (narrow-to-region
13958              (point)
13959              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13960             ;; Then we use the two regular expressions
13961             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13962             ;; select which header lines is to remain visible in the
13963             ;; article buffer.
13964             (goto-char (point-min))
13965             (while (re-search-forward "^[^ \t]*:" nil t)
13966               (beginning-of-line)
13967               ;; We add the headers we want to keep to a list and delete
13968               ;; them from the buffer.
13969               (gnus-put-text-property 
13970                (point) (1+ (point)) 'message-rank
13971                (if (or (and visible (looking-at visible))
13972                        (and ignored
13973                             (not (looking-at ignored))))
13974                    (gnus-article-header-rank) 
13975                  (+ 2 max)))
13976               (forward-line 1))
13977             (message-sort-headers-1)
13978             (when (setq beg (text-property-any 
13979                              (point-min) (point-max) 'message-rank (+ 2 max)))
13980               ;; We make the unwanted headers invisible.
13981               (if delete
13982                   (delete-region beg (point-max))
13983                 ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13984                 (gnus-hide-text-type beg (point-max) 'headers))
13985               ;; Work around XEmacs lossage.
13986               (gnus-put-text-property (point-min) beg 'invisible nil))))))))
13987
13988 (defun gnus-article-hide-boring-headers (&optional arg)
13989   "Toggle hiding of headers that aren't very interesting.
13990 If given a negative prefix, always show; if given a positive prefix,
13991 always hide."
13992   (interactive (gnus-hidden-arg))
13993   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13994     (save-excursion
13995       (set-buffer gnus-article-buffer)
13996       (save-restriction
13997         (let ((buffer-read-only nil)
13998               (list gnus-boring-article-headers)
13999               (inhibit-point-motion-hooks t)
14000               elem)
14001           (nnheader-narrow-to-headers)
14002           (while list
14003             (setq elem (pop list))
14004             (goto-char (point-min))
14005             (cond
14006              ;; Hide empty headers.
14007              ((eq elem 'empty)
14008               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
14009                 (forward-line -1)
14010                 (gnus-hide-text-type
14011                  (progn (beginning-of-line) (point))
14012                  (progn 
14013                    (end-of-line)
14014                    (if (re-search-forward "^[^ \t]" nil t)
14015                        (match-beginning 0)
14016                      (point-max)))
14017                  'boring-headers)))
14018              ;; Hide boring Newsgroups header.
14019              ((eq elem 'newsgroups)
14020               (when (equal (message-fetch-field "newsgroups")
14021                            (gnus-group-real-name gnus-newsgroup-name))
14022                 (gnus-article-hide-header "newsgroups")))
14023              ((eq elem 'followup-to)
14024               (when (equal (message-fetch-field "followup-to")
14025                            (message-fetch-field "newsgroups"))
14026                 (gnus-article-hide-header "followup-to")))
14027              ((eq elem 'reply-to)
14028               (let ((from (message-fetch-field "from"))
14029                     (reply-to (message-fetch-field "reply-to")))
14030                 (when (and
14031                        from reply-to
14032                        (equal 
14033                         (nth 1 (funcall gnus-extract-address-components from))
14034                         (nth 1 (funcall gnus-extract-address-components
14035                                         reply-to))))
14036                   (gnus-article-hide-header "reply-to"))))
14037              ((eq elem 'date)
14038               (let ((date (message-fetch-field "date")))
14039                 (when (and date
14040                            (< (gnus-days-between date (current-time-string))
14041                               4))
14042                   (gnus-article-hide-header "date")))))))))))
14043
14044 (defun gnus-article-hide-header (header)
14045   (save-excursion
14046     (goto-char (point-min))
14047     (when (re-search-forward (concat "^" header ":") nil t)
14048       (gnus-hide-text-type
14049        (progn (beginning-of-line) (point))
14050        (progn 
14051          (end-of-line)
14052          (if (re-search-forward "^[^ \t]" nil t)
14053              (match-beginning 0)
14054            (point-max)))
14055        'boring-headers))))
14056
14057 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
14058 (defun gnus-article-treat-overstrike ()
14059   "Translate overstrikes into bold text."
14060   (interactive)
14061   (save-excursion
14062     (set-buffer gnus-article-buffer)
14063     (let ((buffer-read-only nil))
14064       (while (search-forward "\b" nil t)
14065         (let ((next (following-char))
14066               (previous (char-after (- (point) 2))))
14067           (cond 
14068            ((eq next previous)
14069             (gnus-put-text-property (- (point) 2) (point) 'invisible t)
14070             (gnus-put-text-property (point) (1+ (point)) 'face 'bold))
14071            ((eq next ?_)
14072             (gnus-put-text-property (1- (point)) (1+ (point)) 'invisible t)
14073             (gnus-put-text-property
14074              (- (point) 2) (1- (point)) 'face 'underline))
14075            ((eq previous ?_)
14076             (gnus-put-text-property (- (point) 2) (point) 'invisible t)
14077             (gnus-put-text-property
14078              (point) (1+ (point))       'face 'underline))))))))
14079
14080 (defun gnus-article-word-wrap ()
14081   "Format too long lines."
14082   (interactive)
14083   (save-excursion
14084     (set-buffer gnus-article-buffer)
14085     (let ((buffer-read-only nil))
14086       (widen)
14087       (goto-char (point-min))
14088       (search-forward "\n\n" nil t)
14089       (end-of-line 1)
14090       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
14091             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
14092             (adaptive-fill-mode t))
14093         (while (not (eobp))
14094           (and (>= (current-column) (min fill-column (window-width)))
14095                (/= (preceding-char) ?:)
14096                (fill-paragraph nil))
14097           (end-of-line 2))))))
14098
14099 (defun gnus-article-remove-cr ()
14100   "Remove carriage returns from an article."
14101   (interactive)
14102   (save-excursion
14103     (set-buffer gnus-article-buffer)
14104     (let ((buffer-read-only nil))
14105       (goto-char (point-min))
14106       (while (search-forward "\r" nil t)
14107         (replace-match "" t t)))))
14108
14109 (defun gnus-article-remove-trailing-blank-lines ()
14110   "Remove all trailing blank lines from the article."
14111   (interactive)
14112   (save-excursion
14113     (set-buffer gnus-article-buffer)
14114     (let ((buffer-read-only nil))
14115       (goto-char (point-max))
14116       (delete-region
14117        (point)
14118        (progn
14119          (while (looking-at "^[ \t]*$")
14120            (forward-line -1))
14121          (forward-line 1)
14122          (point))))))
14123
14124 (defun gnus-article-display-x-face (&optional force)
14125   "Look for an X-Face header and display it if present."
14126   (interactive (list 'force))
14127   (save-excursion
14128     (set-buffer gnus-article-buffer)
14129     ;; Delete the old process, if any.
14130     (when (process-status "gnus-x-face")
14131       (delete-process "gnus-x-face"))
14132     (let ((inhibit-point-motion-hooks t)
14133           (case-fold-search nil)
14134           from)
14135       (save-restriction
14136         (nnheader-narrow-to-headers)
14137         (setq from (message-fetch-field "from"))
14138         (goto-char (point-min))
14139         (when (and gnus-article-x-face-command
14140                    (or force
14141                        ;; Check whether this face is censored.
14142                        (not gnus-article-x-face-too-ugly)
14143                        (and gnus-article-x-face-too-ugly from
14144                             (not (string-match gnus-article-x-face-too-ugly
14145                                                from))))
14146                    ;; Has to be present.
14147                    (re-search-forward "^X-Face: " nil t))
14148           ;; We now have the area of the buffer where the X-Face is stored.
14149           (let ((beg (point))
14150                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
14151             ;; We display the face.
14152             (if (symbolp gnus-article-x-face-command)
14153                 ;; The command is a lisp function, so we call it.
14154                 (if (gnus-functionp gnus-article-x-face-command)
14155                     (funcall gnus-article-x-face-command beg end)
14156                   (error "%s is not a function" gnus-article-x-face-command))
14157               ;; The command is a string, so we interpret the command
14158               ;; as a, well, command, and fork it off.
14159               (let ((process-connection-type nil))
14160                 (process-kill-without-query
14161                  (start-process
14162                   "gnus-x-face" nil shell-file-name shell-command-switch
14163                   gnus-article-x-face-command))
14164                 (process-send-region "gnus-x-face" beg end)
14165                 (process-send-eof "gnus-x-face")))))))))
14166
14167 (defalias 'gnus-headers-decode-quoted-printable 'gnus-decode-rfc1522)
14168 (defun gnus-decode-rfc1522 ()
14169   "Hack to remove QP encoding from headers."
14170   (let ((case-fold-search t)
14171         (inhibit-point-motion-hooks t)
14172         (buffer-read-only nil)
14173         string)
14174     (save-restriction
14175       (narrow-to-region
14176        (goto-char (point-min))
14177        (or (search-forward "\n\n" nil t) (point-max)))
14178
14179       (while (re-search-forward 
14180               "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
14181         (setq string (match-string 1))
14182         (narrow-to-region (match-beginning 0) (match-end 0))
14183         (delete-region (point-min) (point-max))
14184         (insert string)
14185         (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
14186         (subst-char-in-region (point-min) (point-max) ?_ ? )
14187         (widen)
14188         (goto-char (point-min))))))
14189
14190 (defun gnus-article-de-quoted-unreadable (&optional force)
14191   "Do a naive translation of a quoted-printable-encoded article.
14192 This is in no way, shape or form meant as a replacement for real MIME
14193 processing, but is simply a stop-gap measure until MIME support is
14194 written.
14195 If FORCE, decode the article whether it is marked as quoted-printable
14196 or not."
14197   (interactive (list 'force))
14198   (save-excursion
14199     (set-buffer gnus-article-buffer)
14200     (let ((case-fold-search t)
14201           (buffer-read-only nil)
14202           (type (gnus-fetch-field "content-transfer-encoding")))
14203       (gnus-decode-rfc1522)
14204       (when (or force
14205                 (and type (string-match "quoted-printable" (downcase type))))
14206         (goto-char (point-min))
14207         (search-forward "\n\n" nil 'move)
14208         (gnus-mime-decode-quoted-printable (point) (point-max))))))
14209
14210 (defun gnus-mime-decode-quoted-printable (from to)
14211   "Decode Quoted-Printable in the region between FROM and TO."
14212   (interactive "r")
14213   (goto-char from)
14214   (while (search-forward "=" to t)
14215     (cond ((eq (following-char) ?\n)
14216            (delete-char -1)
14217            (delete-char 1))
14218           ((looking-at "[0-9A-F][0-9A-F]")
14219            (subst-char-in-region
14220             (1- (point)) (point) ?=
14221             (hexl-hex-string-to-integer
14222              (buffer-substring (point) (+ 2 (point)))))
14223            (delete-char 2))
14224           ((looking-at "=")
14225            (delete-char 1))
14226           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
14227
14228 (defun gnus-article-hide-pgp (&optional arg)
14229   "Toggle hiding of any PGP headers and signatures in the current article.
14230 If given a negative prefix, always show; if given a positive prefix,
14231 always hide."
14232   (interactive (gnus-hidden-arg))
14233   (unless (gnus-article-check-hidden-text 'pgp arg)
14234     (save-excursion
14235       (set-buffer gnus-article-buffer)
14236       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
14237             buffer-read-only beg end)
14238         (widen)
14239         (goto-char (point-min))
14240         ;; Hide the "header".
14241         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
14242              (gnus-hide-text (match-beginning 0) (match-end 0) props))
14243         (setq beg (point))
14244         ;; Hide the actual signature.
14245         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
14246              (setq end (1+ (match-beginning 0)))
14247              (gnus-hide-text
14248               end
14249               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
14250                   (match-end 0)
14251                 ;; Perhaps we shouldn't hide to the end of the buffer
14252                 ;; if there is no end to the signature?
14253                 (point-max))
14254               props))
14255         ;; Hide "- " PGP quotation markers.
14256         (when (and beg end)
14257           (narrow-to-region beg end)
14258           (goto-char (point-min))
14259           (while (re-search-forward "^- " nil t)
14260             (gnus-hide-text (match-beginning 0) (match-end 0) props))
14261           (widen))))))
14262
14263 (defun gnus-article-hide-signature (&optional arg)
14264   "Hide the signature in the current article.
14265 If given a negative prefix, always show; if given a positive prefix,
14266 always hide."
14267   (interactive (gnus-hidden-arg))
14268   (unless (gnus-article-check-hidden-text 'signature arg)
14269     (save-excursion
14270       (set-buffer gnus-article-buffer)
14271       (save-restriction
14272         (let ((buffer-read-only nil))
14273           (when (gnus-narrow-to-signature)
14274             (gnus-hide-text-type (point-min) (point-max) 'signature)))))))
14275
14276 (defun gnus-article-strip-leading-blank-lines ()
14277   "Remove all blank lines from the beginning of the article."
14278   (interactive)
14279   (save-excursion
14280     (set-buffer gnus-article-buffer)
14281     (let (buffer-read-only)
14282       (goto-char (point-min))
14283       (when (search-forward "\n\n" nil t)
14284         (while (looking-at "[ \t]$")
14285           (gnus-delete-line))))))
14286
14287 (defvar mime::preview/content-list)
14288 (defvar mime::preview-content-info/point-min)
14289 (defun gnus-narrow-to-signature ()
14290   "Narrow to the signature."
14291   (widen)
14292   (if (and (boundp 'mime::preview/content-list)
14293            mime::preview/content-list)
14294       (let ((pcinfo (car (last mime::preview/content-list))))
14295         (condition-case ()
14296             (narrow-to-region
14297              (funcall (intern "mime::preview-content-info/point-min") pcinfo)
14298              (point-max))
14299           (error nil))))
14300   (goto-char (point-max))
14301   (when (re-search-backward gnus-signature-separator nil t)
14302     (forward-line 1)
14303     (when (or (null gnus-signature-limit)
14304               (and (numberp gnus-signature-limit)
14305                    (< (- (point-max) (point)) gnus-signature-limit))
14306               (and (gnus-functionp gnus-signature-limit)
14307                    (funcall gnus-signature-limit))
14308               (and (stringp gnus-signature-limit)
14309                    (not (re-search-forward gnus-signature-limit nil t))))
14310       (narrow-to-region (point) (point-max))
14311       t)))
14312
14313 (defun gnus-hidden-arg ()
14314   "Return the current prefix arg as a number, or 0 if no prefix."
14315   (list (if current-prefix-arg
14316             (prefix-numeric-value current-prefix-arg)
14317           0)))
14318
14319 (defun gnus-article-check-hidden-text (type arg)
14320   "Return nil if hiding is necessary.
14321 Arg can be nil or a number.  Nil and positive means hide, negative
14322 means show, 0 means toggle."
14323   (save-excursion
14324     (set-buffer gnus-article-buffer)
14325     (let ((hide (gnus-article-hidden-text-p type)))
14326       (cond
14327        ((or (null arg)
14328             (> arg 0))
14329         nil)
14330        ((< arg 0)
14331         (gnus-article-show-hidden-text type))
14332        (t
14333         (if (eq hide 'hidden)
14334             (gnus-article-show-hidden-text type)
14335           nil))))))
14336
14337 (defun gnus-article-hidden-text-p (type)
14338   "Say whether the current buffer contains hidden text of type TYPE."
14339   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type)))
14340     (when pos
14341       (if (get-text-property pos 'invisible)
14342           'hidden
14343         'shown))))
14344
14345 (defun gnus-article-hide (&optional arg force)
14346   "Hide all the gruft in the current article.
14347 This means that PGP stuff, signatures, cited text and (some)
14348 headers will be hidden.
14349 If given a prefix, show the hidden text instead."
14350   (interactive (list current-prefix-arg 'force))
14351   (gnus-article-hide-headers arg)
14352   (gnus-article-hide-pgp arg)
14353   (gnus-article-hide-citation-maybe arg force)
14354   (gnus-article-hide-signature arg))
14355
14356 (defun gnus-article-show-hidden-text (type &optional hide)
14357   "Show all hidden text of type TYPE.
14358 If HIDE, hide the text instead."
14359   (save-excursion
14360     (set-buffer gnus-article-buffer)
14361     (let ((buffer-read-only nil)
14362           (inhibit-point-motion-hooks t)
14363           (beg (point-min)))
14364       (while (gnus-goto-char (text-property-any
14365                               beg (point-max) 'gnus-type type))
14366         (setq beg (point))
14367         (forward-char)
14368         (if hide
14369             (gnus-hide-text beg (point) gnus-hidden-properties)
14370           (gnus-unhide-text beg (point)))
14371         (setq beg (point)))
14372       t)))
14373
14374 (defvar gnus-article-time-units
14375   `((year . ,(* 365.25 24 60 60))
14376     (week . ,(* 7 24 60 60))
14377     (day . ,(* 24 60 60))
14378     (hour . ,(* 60 60))
14379     (minute . 60)
14380     (second . 1))
14381   "Mapping from time units to seconds.")
14382
14383 (defun gnus-article-date-ut (&optional type highlight)
14384   "Convert DATE date to universal time in the current article.
14385 If TYPE is `local', convert to local time; if it is `lapsed', output
14386 how much time has lapsed since DATE."
14387   (interactive (list 'ut t))
14388   (let* ((header (or gnus-current-headers
14389                      (gnus-summary-article-header) ""))
14390          (date (and (vectorp header) (mail-header-date header)))
14391          (date-regexp "^Date: \\|^X-Sent: ")
14392          (now (current-time))
14393          (inhibit-point-motion-hooks t)
14394          bface eface)
14395     (when (and date (not (string= date "")))
14396       (save-excursion
14397         (set-buffer gnus-article-buffer)
14398         (save-restriction
14399           (nnheader-narrow-to-headers)
14400           (let ((buffer-read-only nil))
14401             ;; Delete any old Date headers.
14402             (if (re-search-forward date-regexp nil t)
14403                 (progn
14404                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
14405                         eface (get-text-property (1- (gnus-point-at-eol))
14406                                                  'face))
14407                   (message-remove-header date-regexp t)
14408                   (beginning-of-line))
14409               (goto-char (point-max)))
14410             (insert (gnus-make-date-line date type))
14411             ;; Do highlighting.
14412             (forward-line -1)
14413             (when (and (gnus-visual-p 'article-highlight 'highlight)
14414                        (looking-at "\\([^:]+\\): *\\(.*\\)$"))
14415               (gnus-put-text-property (match-beginning 1) (match-end 1)
14416                                  'face bface)
14417               (gnus-put-text-property (match-beginning 2) (match-end 2)
14418                                  'face eface))))))))
14419
14420 (defun gnus-make-date-line (date type)
14421   "Return a DATE line of TYPE."
14422   (cond
14423    ;; Convert to the local timezone.  We have to slap a
14424    ;; `condition-case' round the calls to the timezone
14425    ;; functions since they aren't particularly resistant to
14426    ;; buggy dates.
14427    ((eq type 'local)
14428     (concat "Date: " (condition-case ()
14429                          (timezone-make-date-arpa-standard date)
14430                        (error date))
14431             "\n"))
14432    ;; Convert to Universal Time.
14433    ((eq type 'ut)
14434     (concat "Date: "
14435             (condition-case ()
14436                 (timezone-make-date-arpa-standard date nil "UT")
14437               (error date))
14438             "\n"))
14439    ;; Get the original date from the article.
14440    ((eq type 'original)
14441     (concat "Date: " date "\n"))
14442    ;; Do an X-Sent lapsed format.
14443    ((eq type 'lapsed)
14444     ;; If the date is seriously mangled, the timezone
14445     ;; functions are liable to bug out, so we condition-case
14446     ;; the entire thing.
14447     (let* ((now (current-time))
14448            (real-time
14449             (condition-case ()
14450                 (gnus-time-minus
14451                  (gnus-encode-date
14452                   (timezone-make-date-arpa-standard
14453                    (current-time-string now)
14454                    (current-time-zone now) "UT"))
14455                  (gnus-encode-date
14456                   (timezone-make-date-arpa-standard
14457                    date nil "UT")))
14458               (error '(0 0))))
14459            (real-sec (+ (* (float (car real-time)) 65536)
14460                         (cadr real-time)))
14461            (sec (abs real-sec))
14462            num prev)
14463       (cond
14464        ((equal real-time '(0 0))
14465         "X-Sent: Unknown\n")
14466        ((zerop sec)
14467         "X-Sent: Now\n")
14468        (t
14469         (concat
14470          "X-Sent: "
14471          ;; This is a bit convoluted, but basically we go
14472          ;; through the time units for years, weeks, etc,
14473          ;; and divide things to see whether that results
14474          ;; in positive answers.
14475          (mapconcat
14476           (lambda (unit)
14477             (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
14478                 ;; The (remaining) seconds are too few to
14479                 ;; be divided into this time unit.
14480                 ""
14481               ;; It's big enough, so we output it.
14482               (setq sec (- sec (* num (cdr unit))))
14483               (prog1
14484                   (concat (if prev ", " "") (int-to-string
14485                                              (floor num))
14486                           " " (symbol-name (car unit)) 
14487                           (if (> num 1) "s" ""))
14488                 (setq prev t))))
14489           gnus-article-time-units "")
14490          ;; If dates are odd, then it might appear like the
14491          ;; article was sent in the future.
14492          (if (> real-sec 0)
14493              " ago\n"
14494            " in the future\n"))))))
14495    (t
14496     (error "Unknown conversion type: %s" type))))
14497
14498 (defun gnus-article-date-local (&optional highlight)
14499   "Convert the current article date to the local timezone."
14500   (interactive (list t))
14501   (gnus-article-date-ut 'local highlight))
14502
14503 (defun gnus-article-date-original (&optional highlight)
14504   "Convert the current article date to what it was originally.
14505 This is only useful if you have used some other date conversion
14506 function and want to see what the date was before converting."
14507   (interactive (list t))
14508   (gnus-article-date-ut 'original highlight))
14509
14510 (defun gnus-article-date-lapsed (&optional highlight)
14511   "Convert the current article date to time lapsed since it was sent."
14512   (interactive (list t))
14513   (gnus-article-date-ut 'lapsed highlight))
14514
14515 (defun gnus-article-maybe-highlight ()
14516   "Do some article highlighting if `gnus-visual' is non-nil."
14517   (if (gnus-visual-p 'article-highlight 'highlight)
14518       (gnus-article-highlight-some)))
14519
14520 ;;; Article savers.
14521
14522 (defun gnus-output-to-rmail (file-name)
14523   "Append the current article to an Rmail file named FILE-NAME."
14524   (require 'rmail)
14525   ;; Most of these codes are borrowed from rmailout.el.
14526   (setq file-name (expand-file-name file-name))
14527   (setq rmail-default-rmail-file file-name)
14528   (let ((artbuf (current-buffer))
14529         (tmpbuf (get-buffer-create " *Gnus-output*")))
14530     (save-excursion
14531       (or (get-file-buffer file-name)
14532           (file-exists-p file-name)
14533           (if (gnus-yes-or-no-p
14534                (concat "\"" file-name "\" does not exist, create it? "))
14535               (let ((file-buffer (create-file-buffer file-name)))
14536                 (save-excursion
14537                   (set-buffer file-buffer)
14538                   (rmail-insert-rmail-file-header)
14539                   (let ((require-final-newline nil))
14540                     (write-region (point-min) (point-max) file-name t 1)))
14541                 (kill-buffer file-buffer))
14542             (error "Output file does not exist")))
14543       (set-buffer tmpbuf)
14544       (buffer-disable-undo (current-buffer))
14545       (erase-buffer)
14546       (insert-buffer-substring artbuf)
14547       (gnus-convert-article-to-rmail)
14548       ;; Decide whether to append to a file or to an Emacs buffer.
14549       (let ((outbuf (get-file-buffer file-name)))
14550         (if (not outbuf)
14551             (append-to-file (point-min) (point-max) file-name)
14552           ;; File has been visited, in buffer OUTBUF.
14553           (set-buffer outbuf)
14554           (let ((buffer-read-only nil)
14555                 (msg (and (boundp 'rmail-current-message)
14556                           (symbol-value 'rmail-current-message))))
14557             ;; If MSG is non-nil, buffer is in RMAIL mode.
14558             (if msg
14559                 (progn (widen)
14560                        (narrow-to-region (point-max) (point-max))))
14561             (insert-buffer-substring tmpbuf)
14562             (if msg
14563                 (progn
14564                   (goto-char (point-min))
14565                   (widen)
14566                   (search-backward "\^_")
14567                   (narrow-to-region (point) (point-max))
14568                   (goto-char (1+ (point-min)))
14569                   (rmail-count-new-messages t)
14570                   (rmail-show-message msg)))))))
14571     (kill-buffer tmpbuf)))
14572
14573 (defun gnus-output-to-file (file-name)
14574   "Append the current article to a file named FILE-NAME."
14575   (let ((artbuf (current-buffer)))
14576     (nnheader-temp-write nil
14577       (insert-buffer-substring artbuf)
14578       ;; Append newline at end of the buffer as separator, and then
14579       ;; save it to file.
14580       (goto-char (point-max))
14581       (insert "\n")
14582       (append-to-file (point-min) (point-max) file-name))))
14583
14584 (defun gnus-convert-article-to-rmail ()
14585   "Convert article in current buffer to Rmail message format."
14586   (let ((buffer-read-only nil))
14587     ;; Convert article directly into Babyl format.
14588     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14589     (goto-char (point-min))
14590     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14591     (while (search-forward "\n\^_" nil t) ;single char
14592       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14593     (goto-char (point-max))
14594     (insert "\^_")))
14595
14596 (defun gnus-narrow-to-page (&optional arg)
14597   "Narrow the article buffer to a page.
14598 If given a numerical ARG, move forward ARG pages."
14599   (interactive "P")
14600   (setq arg (if arg (prefix-numeric-value arg) 0))
14601   (save-excursion
14602     (set-buffer gnus-article-buffer)
14603     (goto-char (point-min))
14604     (widen)
14605     (when (gnus-visual-p 'page-marker)
14606       (let ((buffer-read-only nil))
14607         (gnus-remove-text-with-property 'gnus-prev)
14608         (gnus-remove-text-with-property 'gnus-next)))
14609     (when
14610         (cond ((< arg 0)
14611                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14612               ((> arg 0)
14613                (re-search-forward page-delimiter nil 'move arg)))
14614       (goto-char (match-end 0)))
14615     (narrow-to-region
14616      (point)
14617      (if (re-search-forward page-delimiter nil 'move)
14618          (match-beginning 0)
14619        (point)))
14620     (when (and (gnus-visual-p 'page-marker)
14621                (not (= (point-min) 1)))
14622       (save-excursion
14623         (goto-char (point-min))
14624         (gnus-insert-prev-page-button)))
14625     (when (and (gnus-visual-p 'page-marker)
14626                (not (= (1- (point-max)) (buffer-size))))
14627       (save-excursion
14628         (goto-char (point-max))
14629         (gnus-insert-next-page-button)))))
14630
14631 ;; Article mode commands
14632
14633 (defun gnus-article-goto-next-page ()
14634   "Show the next page of the article."
14635   (interactive)
14636   (when (gnus-article-next-page)
14637     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
14638
14639 (defun gnus-article-goto-prev-page ()
14640   "Show the next page of the article."
14641   (interactive)
14642   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))
14643     (gnus-article-prev-page nil)))
14644
14645 (defun gnus-article-next-page (&optional lines)
14646   "Show the next page of the current article.
14647 If end of article, return non-nil.  Otherwise return nil.
14648 Argument LINES specifies lines to be scrolled up."
14649   (interactive "p")
14650   (move-to-window-line -1)
14651   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14652   (if (save-excursion
14653         (end-of-line)
14654         (and (pos-visible-in-window-p)  ;Not continuation line.
14655              (eobp)))
14656       ;; Nothing in this page.
14657       (if (or (not gnus-break-pages)
14658               (save-excursion
14659                 (save-restriction
14660                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14661           t                             ;Nothing more.
14662         (gnus-narrow-to-page 1)         ;Go to next page.
14663         nil)
14664     ;; More in this page.
14665     (condition-case ()
14666         (scroll-up lines)
14667       (end-of-buffer
14668        ;; Long lines may cause an end-of-buffer error.
14669        (goto-char (point-max))))
14670     (move-to-window-line 0)
14671     nil))
14672
14673 (defun gnus-article-prev-page (&optional lines)
14674   "Show previous page of current article.
14675 Argument LINES specifies lines to be scrolled down."
14676   (interactive "p")
14677   (move-to-window-line 0)
14678   (if (and gnus-break-pages
14679            (bobp)
14680            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14681       (progn
14682         (gnus-narrow-to-page -1)        ;Go to previous page.
14683         (goto-char (point-max))
14684         (recenter -1))
14685     (prog1
14686         (condition-case ()
14687             (scroll-down lines)
14688           (error nil))
14689       (move-to-window-line 0))))
14690
14691 (defun gnus-article-refer-article ()
14692   "Read article specified by message-id around point."
14693   (interactive)
14694   (let ((point (point)))
14695     (search-forward ">" nil t)          ;Move point to end of "<....>".
14696     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14697         (let ((message-id (match-string 1)))
14698           (goto-char point)
14699           (set-buffer gnus-summary-buffer)
14700           (gnus-summary-refer-article message-id))
14701       (goto-char (point))
14702       (error "No references around point"))))
14703
14704 (defun gnus-article-show-summary ()
14705   "Reconfigure windows to show summary buffer."
14706   (interactive)
14707   (gnus-configure-windows 'article)
14708   (gnus-summary-goto-subject gnus-current-article))
14709
14710 (defun gnus-article-describe-briefly ()
14711   "Describe article mode commands briefly."
14712   (interactive)
14713   (gnus-message 6
14714                 (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")))
14715
14716 (defun gnus-article-summary-command ()
14717   "Execute the last keystroke in the summary buffer."
14718   (interactive)
14719   (let ((obuf (current-buffer))
14720         (owin (current-window-configuration))
14721         func)
14722     (switch-to-buffer gnus-summary-buffer 'norecord)
14723     (setq func (lookup-key (current-local-map) (this-command-keys)))
14724     (call-interactively func)
14725     (set-buffer obuf)
14726     (set-window-configuration owin)
14727     (set-window-point (get-buffer-window (current-buffer)) (point))))
14728
14729 (defun gnus-article-summary-command-nosave ()
14730   "Execute the last keystroke in the summary buffer."
14731   (interactive)
14732   (let (func)
14733     (pop-to-buffer gnus-summary-buffer 'norecord)
14734     (setq func (lookup-key (current-local-map) (this-command-keys)))
14735     (call-interactively func)))
14736
14737 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14738   "Read a summary buffer key sequence and execute it from the article buffer."
14739   (interactive "P")
14740   (let ((nosaves
14741          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14742            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14743            "=" "^" "\M-^" "|"))
14744         keys)
14745     (save-excursion
14746       (set-buffer gnus-summary-buffer)
14747       (push (or key last-command-event) unread-command-events)
14748       (setq keys (read-key-sequence nil)))
14749     (message "")
14750
14751     (if (member keys nosaves)
14752         (let (func)
14753           (pop-to-buffer gnus-summary-buffer 'norecord)
14754           (if (setq func (lookup-key (current-local-map) keys))
14755               (call-interactively func)
14756             (ding)))
14757       (let ((obuf (current-buffer))
14758             (owin (current-window-configuration))
14759             (opoint (point))
14760             func in-buffer)
14761         (if not-restore-window
14762             (pop-to-buffer gnus-summary-buffer 'norecord)
14763           (switch-to-buffer gnus-summary-buffer 'norecord))
14764         (setq in-buffer (current-buffer))
14765         (if (setq func (lookup-key (current-local-map) keys))
14766             (call-interactively func)
14767           (ding))
14768         (when (eq in-buffer (current-buffer))
14769           (set-buffer obuf)
14770           (unless not-restore-window
14771             (set-window-configuration owin))
14772           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14773
14774 \f
14775 ;;;
14776 ;;; Kill file handling.
14777 ;;;
14778
14779 ;;;###autoload
14780 (defalias 'gnus-batch-kill 'gnus-batch-score)
14781 ;;;###autoload
14782 (defun gnus-batch-score ()
14783   "Run batched scoring.
14784 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14785 Newsgroups is a list of strings in Bnews format.  If you want to score
14786 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14787 score the alt hierarchy, you'd say \"!alt.all\"."
14788   (interactive)
14789   (let* ((yes-and-no
14790           (gnus-newsrc-parse-options
14791            (apply (function concat)
14792                   (mapcar (lambda (g) (concat g " "))
14793                           command-line-args-left))))
14794          (gnus-expert-user t)
14795          (nnmail-spool-file nil)
14796          (gnus-use-dribble-file nil)
14797          (yes (car yes-and-no))
14798          (no (cdr yes-and-no))
14799          group newsrc entry
14800          ;; Disable verbose message.
14801          gnus-novice-user gnus-large-newsgroup)
14802     ;; Eat all arguments.
14803     (setq command-line-args-left nil)
14804     ;; Start Gnus.
14805     (gnus)
14806     ;; Apply kills to specified newsgroups in command line arguments.
14807     (setq newsrc (cdr gnus-newsrc-alist))
14808     (while newsrc
14809       (setq group (caar newsrc))
14810       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14811       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14812                (and (car entry)
14813                     (or (eq (car entry) t)
14814                         (not (zerop (car entry)))))
14815                (if yes (string-match yes group) t)
14816                (or (null no) (not (string-match no group))))
14817           (progn
14818             (gnus-summary-read-group group nil t nil t)
14819             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14820                  (gnus-summary-exit))))
14821       (setq newsrc (cdr newsrc)))
14822     ;; Exit Emacs.
14823     (switch-to-buffer gnus-group-buffer)
14824     (gnus-group-save-newsrc)))
14825
14826 (defun gnus-apply-kill-file ()
14827   "Apply a kill file to the current newsgroup.
14828 Returns the number of articles marked as read."
14829   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14830           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14831       (gnus-apply-kill-file-internal)
14832     0))
14833
14834 (defun gnus-kill-save-kill-buffer ()
14835   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14836     (when (get-file-buffer file)
14837       (save-excursion
14838         (set-buffer (get-file-buffer file))
14839         (and (buffer-modified-p) (save-buffer))
14840         (kill-buffer (current-buffer))))))
14841
14842 (defvar gnus-kill-file-name "KILL"
14843   "Suffix of the kill files.")
14844
14845 (defun gnus-newsgroup-kill-file (newsgroup)
14846   "Return the name of a kill file name for NEWSGROUP.
14847 If NEWSGROUP is nil, return the global kill file name instead."
14848   (cond 
14849    ;; The global KILL file is placed at top of the directory.
14850    ((or (null newsgroup)
14851         (string-equal newsgroup ""))
14852     (expand-file-name gnus-kill-file-name
14853                       gnus-kill-files-directory))
14854    ;; Append ".KILL" to newsgroup name.
14855    ((gnus-use-long-file-name 'not-kill)
14856     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14857                               "." gnus-kill-file-name)
14858                       gnus-kill-files-directory))
14859    ;; Place "KILL" under the hierarchical directory.
14860    (t
14861     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14862                               "/" gnus-kill-file-name)
14863                       gnus-kill-files-directory))))
14864
14865 \f
14866 ;;;
14867 ;;; Dribble file
14868 ;;;
14869
14870 (defvar gnus-dribble-ignore nil)
14871 (defvar gnus-dribble-eval-file nil)
14872
14873 (defun gnus-dribble-file-name ()
14874   "Return the dribble file for the current .newsrc."
14875   (concat
14876    (if gnus-dribble-directory
14877        (concat (file-name-as-directory gnus-dribble-directory)
14878                (file-name-nondirectory gnus-current-startup-file))
14879      gnus-current-startup-file)
14880    "-dribble"))
14881
14882 (defun gnus-dribble-enter (string)
14883   "Enter STRING into the dribble buffer."
14884   (if (and (not gnus-dribble-ignore)
14885            gnus-dribble-buffer
14886            (buffer-name gnus-dribble-buffer))
14887       (let ((obuf (current-buffer)))
14888         (set-buffer gnus-dribble-buffer)
14889         (insert string "\n")
14890         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14891         (set-buffer obuf))))
14892
14893 (defun gnus-dribble-read-file ()
14894   "Read the dribble file from disk."
14895   (let ((dribble-file (gnus-dribble-file-name)))
14896     (save-excursion
14897       (set-buffer (setq gnus-dribble-buffer
14898                         (get-buffer-create
14899                          (file-name-nondirectory dribble-file))))
14900       (gnus-add-current-to-buffer-list)
14901       (erase-buffer)
14902       (setq buffer-file-name dribble-file)
14903       (auto-save-mode t)
14904       (buffer-disable-undo (current-buffer))
14905       (bury-buffer (current-buffer))
14906       (set-buffer-modified-p nil)
14907       (let ((auto (make-auto-save-file-name))
14908             (gnus-dribble-ignore t)
14909             modes)
14910         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14911           ;; Load whichever file is newest -- the auto save file
14912           ;; or the "real" file.
14913           (if (file-newer-than-file-p auto dribble-file)
14914               (insert-file-contents auto)
14915             (insert-file-contents dribble-file))
14916           (unless (zerop (buffer-size))
14917             (set-buffer-modified-p t))
14918           ;; Set the file modes to reflect the .newsrc file modes.
14919           (save-buffer)
14920           (when (and (file-exists-p gnus-current-startup-file)
14921                      (setq modes (file-modes gnus-current-startup-file)))
14922             (set-file-modes dribble-file modes))
14923           ;; Possibly eval the file later.
14924           (when (gnus-y-or-n-p
14925                  "Auto-save file exists.  Do you want to read it? ")
14926             (setq gnus-dribble-eval-file t)))))))
14927
14928 (defun gnus-dribble-eval-file ()
14929   (when gnus-dribble-eval-file
14930     (setq gnus-dribble-eval-file nil)
14931     (save-excursion
14932       (let ((gnus-dribble-ignore t))
14933         (set-buffer gnus-dribble-buffer)
14934         (eval-buffer (current-buffer))))))
14935
14936 (defun gnus-dribble-delete-file ()
14937   (when (file-exists-p (gnus-dribble-file-name))
14938     (delete-file (gnus-dribble-file-name)))
14939   (when gnus-dribble-buffer
14940     (save-excursion
14941       (set-buffer gnus-dribble-buffer)
14942       (let ((auto (make-auto-save-file-name)))
14943         (if (file-exists-p auto)
14944             (delete-file auto))
14945         (erase-buffer)
14946         (set-buffer-modified-p nil)))))
14947
14948 (defun gnus-dribble-save ()
14949   (when (and gnus-dribble-buffer
14950              (buffer-name gnus-dribble-buffer))
14951     (save-excursion
14952       (set-buffer gnus-dribble-buffer)
14953       (save-buffer))))
14954
14955 (defun gnus-dribble-clear ()
14956   (when (gnus-buffer-exists-p gnus-dribble-buffer)
14957     (save-excursion
14958       (set-buffer gnus-dribble-buffer)
14959       (erase-buffer)
14960       (set-buffer-modified-p nil)
14961       (setq buffer-saved-size (buffer-size)))))
14962
14963 \f
14964 ;;;
14965 ;;; Server Communication
14966 ;;;
14967
14968 (defun gnus-start-news-server (&optional confirm)
14969   "Open a method for getting news.
14970 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14971   (let (how)
14972     (if gnus-current-select-method
14973         ;; Stream is already opened.
14974         nil
14975       ;; Open NNTP server.
14976       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14977       (if confirm
14978           (progn
14979             ;; Read server name with completion.
14980             (setq gnus-nntp-server
14981                   (completing-read "NNTP server: "
14982                                    (mapcar (lambda (server) (list server))
14983                                            (cons (list gnus-nntp-server)
14984                                                  gnus-secondary-servers))
14985                                    nil nil gnus-nntp-server))))
14986
14987       (if (and gnus-nntp-server
14988                (stringp gnus-nntp-server)
14989                (not (string= gnus-nntp-server "")))
14990           (setq gnus-select-method
14991                 (cond ((or (string= gnus-nntp-server "")
14992                            (string= gnus-nntp-server "::"))
14993                        (list 'nnspool (system-name)))
14994                       ((string-match "^:" gnus-nntp-server)
14995                        (list 'nnmh gnus-nntp-server
14996                              (list 'nnmh-directory
14997                                    (file-name-as-directory
14998                                     (expand-file-name
14999                                      (concat "~/" (substring
15000                                                    gnus-nntp-server 1)))))
15001                              (list 'nnmh-get-new-mail nil)))
15002                       (t
15003                        (list 'nntp gnus-nntp-server)))))
15004
15005       (setq how (car gnus-select-method))
15006       (cond ((eq how 'nnspool)
15007              (require 'nnspool)
15008              (gnus-message 5 "Looking up local news spool..."))
15009             ((eq how 'nnmh)
15010              (require 'nnmh)
15011              (gnus-message 5 "Looking up mh spool..."))
15012             (t
15013              (require 'nntp)))
15014       (setq gnus-current-select-method gnus-select-method)
15015       (run-hooks 'gnus-open-server-hook)
15016       (or
15017        ;; gnus-open-server-hook might have opened it
15018        (gnus-server-opened gnus-select-method)
15019        (gnus-open-server gnus-select-method)
15020        (gnus-y-or-n-p
15021         (format
15022          "%s (%s) open error: '%s'.     Continue? "
15023          (car gnus-select-method) (cadr gnus-select-method)
15024          (gnus-status-message gnus-select-method)))
15025        (gnus-error 1 "Couldn't open server on %s"
15026                    (nth 1 gnus-select-method))))))
15027
15028 (defun gnus-check-group (group)
15029   "Try to make sure that the server where GROUP exists is alive."
15030   (let ((method (gnus-find-method-for-group group)))
15031     (or (gnus-server-opened method)
15032         (gnus-open-server method))))
15033
15034 (defun gnus-check-server (&optional method silent)
15035   "Check whether the connection to METHOD is down.
15036 If METHOD is nil, use `gnus-select-method'.
15037 If it is down, start it up (again)."
15038   (let ((method (or method gnus-select-method)))
15039     ;; Transform virtual server names into select methods.
15040     (when (stringp method)
15041       (setq method (gnus-server-to-method method)))
15042     (if (gnus-server-opened method)
15043         ;; The stream is already opened.
15044         t
15045       ;; Open the server.
15046       (unless silent
15047         (gnus-message 5 "Opening %s server%s..." (car method)
15048                       (if (equal (nth 1 method) "") ""
15049                         (format " on %s" (nth 1 method)))))
15050       (run-hooks 'gnus-open-server-hook)
15051       (prog1
15052           (gnus-open-server method)
15053         (unless silent
15054           (message ""))))))
15055
15056 (defun gnus-get-function (method function &optional noerror)
15057   "Return a function symbol based on METHOD and FUNCTION."
15058   ;; Translate server names into methods.
15059   (unless method
15060     (error "Attempted use of a nil select method"))
15061   (when (stringp method)
15062     (setq method (gnus-server-to-method method)))
15063   (let ((func (intern (format "%s-%s" (car method) function))))
15064     ;; If the functions isn't bound, we require the backend in
15065     ;; question.
15066     (unless (fboundp func)
15067       (require (car method))
15068       (when (and (not (fboundp func))
15069                  (not noerror))
15070         ;; This backend doesn't implement this function.
15071         (error "No such function: %s" func)))
15072     func))
15073
15074 \f
15075 ;;;
15076 ;;; Interface functions to the backends.
15077 ;;;
15078
15079 (defun gnus-open-server (method)
15080   "Open a connection to METHOD."
15081   (when (stringp method)
15082     (setq method (gnus-server-to-method method)))
15083   (let ((elem (assoc method gnus-opened-servers)))
15084     ;; If this method was previously denied, we just return nil.
15085     (if (eq (nth 1 elem) 'denied)
15086         (progn
15087           (gnus-message 1 "Denied server")
15088           nil)
15089       ;; Open the server.
15090       (let ((result
15091              (funcall (gnus-get-function method 'open-server)
15092                       (nth 1 method) (nthcdr 2 method))))
15093         ;; If this hasn't been opened before, we add it to the list.
15094         (unless elem
15095           (setq elem (list method nil)
15096                 gnus-opened-servers (cons elem gnus-opened-servers)))
15097         ;; Set the status of this server.
15098         (setcar (cdr elem) (if result 'ok 'denied))
15099         ;; Return the result from the "open" call.
15100         result))))
15101
15102 (defun gnus-close-server (method)
15103   "Close the connection to METHOD."
15104   (when (stringp method)
15105     (setq method (gnus-server-to-method method)))
15106   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
15107
15108 (defun gnus-request-list (method)
15109   "Request the active file from METHOD."
15110   (when (stringp method)
15111     (setq method (gnus-server-to-method method)))
15112   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
15113
15114 (defun gnus-request-list-newsgroups (method)
15115   "Request the newsgroups file from METHOD."
15116   (when (stringp method)
15117     (setq method (gnus-server-to-method method)))
15118   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
15119
15120 (defun gnus-request-newgroups (date method)
15121   "Request all new groups since DATE from METHOD."
15122   (when (stringp method)
15123     (setq method (gnus-server-to-method method)))
15124   (funcall (gnus-get-function method 'request-newgroups)
15125            date (nth 1 method)))
15126
15127 (defun gnus-server-opened (method)
15128   "Check whether a connection to METHOD has been opened."
15129   (when (stringp method)
15130     (setq method (gnus-server-to-method method)))
15131   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
15132
15133 (defun gnus-status-message (method)
15134   "Return the status message from METHOD.
15135 If METHOD is a string, it is interpreted as a group name.   The method
15136 this group uses will be queried."
15137   (let ((method (if (stringp method) (gnus-find-method-for-group method)
15138                   method)))
15139     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
15140
15141 (defun gnus-request-group (group &optional dont-check method)
15142   "Request GROUP.  If DONT-CHECK, no information is required."
15143   (let ((method (or method (gnus-find-method-for-group group))))
15144     (when (stringp method)
15145       (setq method (gnus-server-to-method method)))
15146     (funcall (gnus-get-function method 'request-group)
15147              (gnus-group-real-name group) (nth 1 method) dont-check)))
15148
15149 (defun gnus-request-asynchronous (group &optional articles)
15150   "Request that GROUP behave asynchronously.
15151 ARTICLES is the `data' of the group."
15152   (let ((method (gnus-find-method-for-group group)))
15153     (funcall (gnus-get-function method 'request-asynchronous)
15154              (gnus-group-real-name group) (nth 1 method) articles)))
15155
15156 (defun gnus-list-active-group (group)
15157   "Request active information on GROUP."
15158   (let ((method (gnus-find-method-for-group group))
15159         (func 'list-active-group))
15160     (when (gnus-check-backend-function func group)
15161       (funcall (gnus-get-function method func)
15162                (gnus-group-real-name group) (nth 1 method)))))
15163
15164 (defun gnus-request-group-description (group)
15165   "Request a description of GROUP."
15166   (let ((method (gnus-find-method-for-group group))
15167         (func 'request-group-description))
15168     (when (gnus-check-backend-function func group)
15169       (funcall (gnus-get-function method func)
15170                (gnus-group-real-name group) (nth 1 method)))))
15171
15172 (defun gnus-close-group (group)
15173   "Request the GROUP be closed."
15174   (let ((method (gnus-find-method-for-group group)))
15175     (funcall (gnus-get-function method 'close-group)
15176              (gnus-group-real-name group) (nth 1 method))))
15177
15178 (defun gnus-retrieve-headers (articles group &optional fetch-old)
15179   "Request headers for ARTICLES in GROUP.
15180 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
15181   (let ((method (gnus-find-method-for-group group)))
15182     (if (and gnus-use-cache (numberp (car articles)))
15183         (gnus-cache-retrieve-headers articles group fetch-old)
15184       (funcall (gnus-get-function method 'retrieve-headers)
15185                articles (gnus-group-real-name group) (nth 1 method)
15186                fetch-old))))
15187
15188 (defun gnus-retrieve-groups (groups method)
15189   "Request active information on GROUPS from METHOD."
15190   (when (stringp method)
15191     (setq method (gnus-server-to-method method)))
15192   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
15193
15194 (defun gnus-request-type (group &optional article)
15195   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
15196   (let ((method (gnus-find-method-for-group group)))
15197     (if (not (gnus-check-backend-function 'request-type (car method)))
15198         'unknown
15199       (funcall (gnus-get-function method 'request-type)
15200                (gnus-group-real-name group) article))))
15201
15202 (defun gnus-request-update-mark (group article mark)
15203   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
15204   (let ((method (gnus-find-method-for-group group)))
15205     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
15206         mark
15207       (funcall (gnus-get-function method 'request-update-mark)
15208                (gnus-group-real-name group) article mark))))
15209
15210 (defun gnus-request-article (article group &optional buffer)
15211   "Request the ARTICLE in GROUP.
15212 ARTICLE can either be an article number or an article Message-ID.
15213 If BUFFER, insert the article in that group."
15214   (let ((method (gnus-find-method-for-group group)))
15215     (funcall (gnus-get-function method 'request-article)
15216              article (gnus-group-real-name group) (nth 1 method) buffer)))
15217
15218 (defun gnus-request-head (article group)
15219   "Request the head of ARTICLE in GROUP."
15220   (let* ((method (gnus-find-method-for-group group))
15221          (head (gnus-get-function method 'request-head t)))
15222     (if (fboundp head)
15223         (funcall head article (gnus-group-real-name group) (nth 1 method))
15224       (let ((res (gnus-request-article article group)))
15225         (when res
15226           (save-excursion
15227             (set-buffer nntp-server-buffer)
15228             (goto-char (point-min))
15229             (when (search-forward "\n\n" nil t)
15230               (delete-region (1- (point)) (point-max)))
15231             (nnheader-fold-continuation-lines)))
15232         res))))
15233
15234 (defun gnus-request-body (article group)
15235   "Request the body of ARTICLE in GROUP."
15236   (let ((method (gnus-find-method-for-group group)))
15237     (funcall (gnus-get-function method 'request-body)
15238              article (gnus-group-real-name group) (nth 1 method))))
15239
15240 (defun gnus-request-post (method)
15241   "Post the current buffer using METHOD."
15242   (when (stringp method)
15243     (setq method (gnus-server-to-method method)))
15244   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
15245
15246 (defun gnus-request-scan (group method)
15247   "Request a SCAN being performed in GROUP from METHOD.
15248 If GROUP is nil, all groups on METHOD are scanned."
15249   (let ((method (if group (gnus-find-method-for-group group) method)))
15250     (funcall (gnus-get-function method 'request-scan)
15251              (and group (gnus-group-real-name group)) (nth 1 method))))
15252
15253 (defsubst gnus-request-update-info (info method)
15254   "Request that METHOD update INFO."
15255   (when (stringp method)
15256     (setq method (gnus-server-to-method method)))
15257   (when (gnus-check-backend-function 'request-update-info (car method))
15258     (funcall (gnus-get-function method 'request-update-info)
15259              (gnus-group-real-name (gnus-info-group info))
15260              info (nth 1 method))))
15261
15262 (defun gnus-request-expire-articles (articles group &optional force)
15263   (let ((method (gnus-find-method-for-group group)))
15264     (funcall (gnus-get-function method 'request-expire-articles)
15265              articles (gnus-group-real-name group) (nth 1 method)
15266              force)))
15267
15268 (defun gnus-request-move-article
15269   (article group server accept-function &optional last)
15270   (let ((method (gnus-find-method-for-group group)))
15271     (funcall (gnus-get-function method 'request-move-article)
15272              article (gnus-group-real-name group)
15273              (nth 1 method) accept-function last)))
15274
15275 (defun gnus-request-accept-article (group method &optional last)
15276   ;; Make sure there's a newline at the end of the article.
15277   (when (stringp method)
15278     (setq method (gnus-server-to-method method)))
15279   (when (and (not method)
15280              (stringp group))
15281     (setq method (gnus-find-method-for-group group)))
15282   (goto-char (point-max))
15283   (unless (bolp)
15284     (insert "\n"))
15285   (let ((func (car (or method (gnus-find-method-for-group group)))))
15286     (funcall (intern (format "%s-request-accept-article" func))
15287              (if (stringp group) (gnus-group-real-name group) group)
15288              (cadr method)
15289              last)))
15290
15291 (defun gnus-request-replace-article (article group buffer)
15292   (let ((func (car (gnus-find-method-for-group group))))
15293     (funcall (intern (format "%s-request-replace-article" func))
15294              article (gnus-group-real-name group) buffer)))
15295
15296 (defun gnus-request-associate-buffer (group)
15297   (let ((method (gnus-find-method-for-group group)))
15298     (funcall (gnus-get-function method 'request-associate-buffer)
15299              (gnus-group-real-name group))))
15300
15301 (defun gnus-request-restore-buffer (article group)
15302   "Request a new buffer restored to the state of ARTICLE."
15303   (let ((method (gnus-find-method-for-group group)))
15304     (funcall (gnus-get-function method 'request-restore-buffer)
15305              article (gnus-group-real-name group) (nth 1 method))))
15306
15307 (defun gnus-request-create-group (group &optional method)
15308   (when (stringp method)
15309     (setq method (gnus-server-to-method method)))
15310   (let ((method (or method (gnus-find-method-for-group group))))
15311     (funcall (gnus-get-function method 'request-create-group)
15312              (gnus-group-real-name group) (nth 1 method))))
15313
15314 (defun gnus-request-delete-group (group &optional force)
15315   (let ((method (gnus-find-method-for-group group)))
15316     (funcall (gnus-get-function method 'request-delete-group)
15317              (gnus-group-real-name group) force (nth 1 method))))
15318
15319 (defun gnus-request-rename-group (group new-name)
15320   (let ((method (gnus-find-method-for-group group)))
15321     (funcall (gnus-get-function method 'request-rename-group)
15322              (gnus-group-real-name group)
15323              (gnus-group-real-name new-name) (nth 1 method))))
15324
15325 (defun gnus-member-of-valid (symbol group)
15326   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
15327   (memq symbol (assoc
15328                 (symbol-name (car (gnus-find-method-for-group group)))
15329                 gnus-valid-select-methods)))
15330
15331 (defun gnus-method-option-p (method option)
15332   "Return non-nil if select METHOD has OPTION as a parameter."
15333   (when (stringp method)
15334     (setq method (gnus-server-to-method method)))
15335   (memq option (assoc (format "%s" (car method))
15336                       gnus-valid-select-methods)))
15337
15338 (defun gnus-server-extend-method (group method)
15339   ;; This function "extends" a virtual server.  If the server is
15340   ;; "hello", and the select method is ("hello" (my-var "something"))
15341   ;; in the group "alt.alt", this will result in a new virtual server
15342   ;; called "hello+alt.alt".
15343   (let ((entry
15344          (gnus-copy-sequence
15345           (if (equal (car method) "native") gnus-select-method
15346             (cdr (assoc (car method) gnus-server-alist))))))
15347     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
15348     (nconc entry (cdr method))))
15349
15350 (defun gnus-find-method-for-group (group &optional info)
15351   "Find the select method that GROUP uses."
15352   (or gnus-override-method
15353       (and (not group)
15354            gnus-select-method)
15355       (let ((info (or info (gnus-get-info group)))
15356             method)
15357         (if (or (not info)
15358                 (not (setq method (gnus-info-method info)))
15359                 (equal method "native"))
15360             gnus-select-method
15361           (setq method
15362                 (cond ((stringp method)
15363                        (gnus-server-to-method method))
15364                       ((stringp (car method))
15365                        (gnus-server-extend-method group method))
15366                       (t
15367                        method)))
15368           (cond ((equal (cadr method) "")
15369                  method)
15370                 ((null (cadr method))
15371                  (list (car method) ""))
15372                 (t
15373                  (gnus-server-add-address method)))))))
15374
15375 (defun gnus-check-backend-function (func group)
15376   "Check whether GROUP supports function FUNC."
15377   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
15378                   group)))
15379     (fboundp (intern (format "%s-%s" method func)))))
15380
15381 (defun gnus-methods-using (feature)
15382   "Find all methods that have FEATURE."
15383   (let ((valids gnus-valid-select-methods)
15384         outs)
15385     (while valids
15386       (if (memq feature (car valids))
15387           (setq outs (cons (car valids) outs)))
15388       (setq valids (cdr valids)))
15389     outs))
15390
15391 \f
15392 ;;;
15393 ;;; Active & Newsrc File Handling
15394 ;;;
15395
15396 (defun gnus-setup-news (&optional rawfile level dont-connect)
15397   "Setup news information.
15398 If RAWFILE is non-nil, the .newsrc file will also be read.
15399 If LEVEL is non-nil, the news will be set up at level LEVEL."
15400   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
15401
15402     (when init 
15403       ;; Clear some variables to re-initialize news information.
15404       (setq gnus-newsrc-alist nil
15405             gnus-active-hashtb nil)
15406       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
15407       (gnus-read-newsrc-file rawfile))
15408
15409     (when (and (not (assoc "archive" gnus-server-alist))
15410                gnus-message-archive-method)
15411       (push (cons "archive" gnus-message-archive-method)
15412             gnus-server-alist))
15413
15414     ;; If we don't read the complete active file, we fill in the
15415     ;; hashtb here.
15416     (if (or (null gnus-read-active-file)
15417             (eq gnus-read-active-file 'some))
15418         (gnus-update-active-hashtb-from-killed))
15419
15420     ;; Read the active file and create `gnus-active-hashtb'.
15421     ;; If `gnus-read-active-file' is nil, then we just create an empty
15422     ;; hash table.  The partial filling out of the hash table will be
15423     ;; done in `gnus-get-unread-articles'.
15424     (and gnus-read-active-file
15425          (not level)
15426          (gnus-read-active-file))
15427
15428     (or gnus-active-hashtb
15429         (setq gnus-active-hashtb (make-vector 4095 0)))
15430
15431     ;; Initialize the cache.
15432     (when gnus-use-cache
15433       (gnus-cache-open))
15434
15435     ;; Possibly eval the dribble file.
15436     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
15437
15438     ;; Slave Gnusii should then clear the dribble buffer.
15439     (when (and init gnus-slave)
15440       (gnus-dribble-clear))
15441
15442     (gnus-update-format-specifications)
15443
15444     ;; See whether we need to read the description file.
15445     (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
15446              (not gnus-description-hashtb)
15447              (not dont-connect)
15448              gnus-read-active-file)
15449         (gnus-read-all-descriptions-files))
15450
15451     ;; Find new newsgroups and treat them.
15452     (if (and init gnus-check-new-newsgroups (not level)
15453              (gnus-check-server gnus-select-method))
15454         (gnus-find-new-newsgroups))
15455
15456     ;; We might read in new NoCeM messages here.
15457     (when (and gnus-use-nocem 
15458                (not level)
15459                (not dont-connect))
15460       (gnus-nocem-scan-groups))
15461
15462     ;; Find the number of unread articles in each non-dead group.
15463     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
15464       (gnus-get-unread-articles level))
15465
15466     (if (and init gnus-check-bogus-newsgroups
15467              gnus-read-active-file (not level)
15468              (gnus-server-opened gnus-select-method))
15469         (gnus-check-bogus-newsgroups))))
15470
15471 (defun gnus-find-new-newsgroups (&optional arg)
15472   "Search for new newsgroups and add them.
15473 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
15474 The `-n' option line from .newsrc is respected.
15475 If ARG (the prefix), use the `ask-server' method to query
15476 the server for new groups."
15477   (interactive "P")
15478   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
15479                        (null gnus-read-active-file)
15480                        (eq gnus-read-active-file 'some))
15481                    'ask-server gnus-check-new-newsgroups)))
15482     (unless (gnus-check-first-time-used)
15483       (if (or (consp check)
15484               (eq check 'ask-server))
15485           ;; Ask the server for new groups.
15486           (gnus-ask-server-for-new-groups)
15487         ;; Go through the active hashtb and look for new groups.
15488         (let ((groups 0)
15489               group new-newsgroups)
15490           (gnus-message 5 "Looking for new newsgroups...")
15491           (unless gnus-have-read-active-file
15492             (gnus-read-active-file))
15493           (setq gnus-newsrc-last-checked-date (current-time-string))
15494           (unless gnus-killed-hashtb
15495             (gnus-make-hashtable-from-killed))
15496           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
15497           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
15498           (mapatoms
15499            (lambda (sym)
15500              (if (or (null (setq group (symbol-name sym)))
15501                      (not (boundp sym))
15502                      (null (symbol-value sym))
15503                      (gnus-gethash group gnus-killed-hashtb)
15504                      (gnus-gethash group gnus-newsrc-hashtb))
15505                  ()
15506                (let ((do-sub (gnus-matches-options-n group)))
15507                  (cond
15508                   ((eq do-sub 'subscribe)
15509                    (setq groups (1+ groups))
15510                    (gnus-sethash group group gnus-killed-hashtb)
15511                    (funcall gnus-subscribe-options-newsgroup-method group))
15512                   ((eq do-sub 'ignore)
15513                    nil)
15514                   (t
15515                    (setq groups (1+ groups))
15516                    (gnus-sethash group group gnus-killed-hashtb)
15517                    (if gnus-subscribe-hierarchical-interactive
15518                        (setq new-newsgroups (cons group new-newsgroups))
15519                      (funcall gnus-subscribe-newsgroup-method group)))))))
15520            gnus-active-hashtb)
15521           (when new-newsgroups
15522             (gnus-subscribe-hierarchical-interactive new-newsgroups))
15523           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15524           (if (> groups 0)
15525               (gnus-message 6 "%d new newsgroup%s arrived."
15526                             groups (if (> groups 1) "s have" " has"))
15527             (gnus-message 6 "No new newsgroups.")))))))
15528
15529 (defun gnus-matches-options-n (group)
15530   ;; Returns `subscribe' if the group is to be unconditionally
15531   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
15532   ;; no match for the group.
15533
15534   ;; First we check the two user variables.
15535   (cond
15536    ((and gnus-options-subscribe
15537          (string-match gnus-options-subscribe group))
15538     'subscribe)
15539    ((and gnus-auto-subscribed-groups
15540          (string-match gnus-auto-subscribed-groups group))
15541     'subscribe)
15542    ((and gnus-options-not-subscribe
15543          (string-match gnus-options-not-subscribe group))
15544     'ignore)
15545    ;; Then we go through the list that was retrieved from the .newsrc
15546    ;; file.  This list has elements on the form
15547    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
15548    ;; is in the reverse order of the options line) is returned.
15549    (t
15550     (let ((regs gnus-newsrc-options-n))
15551       (while (and regs
15552                   (not (string-match (caar regs) group)))
15553         (setq regs (cdr regs)))
15554       (and regs (cdar regs))))))
15555
15556 (defun gnus-ask-server-for-new-groups ()
15557   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
15558          (methods (cons gnus-select-method
15559                         (nconc
15560                          (when gnus-message-archive-method
15561                            (list "archive"))
15562                          (append
15563                           (and (consp gnus-check-new-newsgroups)
15564                                gnus-check-new-newsgroups)
15565                           gnus-secondary-select-methods))))
15566          (groups 0)
15567          (new-date (current-time-string))
15568          group new-newsgroups got-new method hashtb
15569          gnus-override-subscribe-method)
15570     ;; Go through both primary and secondary select methods and
15571     ;; request new newsgroups.
15572     (while (setq method (gnus-server-get-method nil (pop methods)))
15573       (setq new-newsgroups nil)
15574       (setq gnus-override-subscribe-method method)
15575       (when (and (gnus-check-server method)
15576                  (gnus-request-newgroups date method))
15577         (save-excursion
15578           (setq got-new t)
15579           (setq hashtb (gnus-make-hashtable 100))
15580           (set-buffer nntp-server-buffer)
15581           ;; Enter all the new groups into a hashtable.
15582           (gnus-active-to-gnus-format method hashtb 'ignore))
15583         ;; Now all new groups from `method' are in `hashtb'.
15584         (mapatoms
15585          (lambda (group-sym)
15586            (if (or (null (setq group (symbol-name group-sym)))
15587                    (not (boundp group-sym))
15588                    (null (symbol-value group-sym))
15589                    (gnus-gethash group gnus-newsrc-hashtb)
15590                    (member group gnus-zombie-list)
15591                    (member group gnus-killed-list))
15592                ;; The group is already known.
15593                ()
15594              ;; Make this group active.
15595              (when (symbol-value group-sym)
15596                (gnus-set-active group (symbol-value group-sym)))
15597              ;; Check whether we want it or not.
15598              (let ((do-sub (gnus-matches-options-n group)))
15599                (cond
15600                 ((eq do-sub 'subscribe)
15601                  (incf groups)
15602                  (gnus-sethash group group gnus-killed-hashtb)
15603                  (funcall gnus-subscribe-options-newsgroup-method group))
15604                 ((eq do-sub 'ignore)
15605                  nil)
15606                 (t
15607                  (incf groups)
15608                  (gnus-sethash group group gnus-killed-hashtb)
15609                  (if gnus-subscribe-hierarchical-interactive
15610                      (push group new-newsgroups)
15611                    (funcall gnus-subscribe-newsgroup-method group)))))))
15612          hashtb))
15613       (when new-newsgroups
15614         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
15615     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15616     (when (> groups 0)
15617       (gnus-message 6 "%d new newsgroup%s arrived."
15618                     groups (if (> groups 1) "s have" " has")))
15619     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15620     got-new))
15621
15622 (defun gnus-check-first-time-used ()
15623   (if (or (> (length gnus-newsrc-alist) 1)
15624           (file-exists-p gnus-startup-file)
15625           (file-exists-p (concat gnus-startup-file ".el"))
15626           (file-exists-p (concat gnus-startup-file ".eld")))
15627       nil
15628     (gnus-message 6 "First time user; subscribing you to default groups")
15629     (unless (gnus-read-active-file-p)
15630       (gnus-read-active-file))
15631     (setq gnus-newsrc-last-checked-date (current-time-string))
15632     (let ((groups gnus-default-subscribed-newsgroups)
15633           group)
15634       (if (eq groups t)
15635           nil
15636         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15637         (mapatoms
15638          (lambda (sym)
15639            (if (null (setq group (symbol-name sym)))
15640                ()
15641              (let ((do-sub (gnus-matches-options-n group)))
15642                (cond
15643                 ((eq do-sub 'subscribe)
15644                  (gnus-sethash group group gnus-killed-hashtb)
15645                  (funcall gnus-subscribe-options-newsgroup-method group))
15646                 ((eq do-sub 'ignore)
15647                  nil)
15648                 (t
15649                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15650          gnus-active-hashtb)
15651         (while groups
15652           (if (gnus-active (car groups))
15653               (gnus-group-change-level
15654                (car groups) gnus-level-default-subscribed gnus-level-killed))
15655           (setq groups (cdr groups)))
15656         (gnus-group-make-help-group)
15657         (and gnus-novice-user
15658              (gnus-message 7 "`A k' to list killed groups"))))))
15659
15660 (defun gnus-subscribe-group (group previous &optional method)
15661   (gnus-group-change-level
15662    (if method
15663        (list t group gnus-level-default-subscribed nil nil method)
15664      group)
15665    gnus-level-default-subscribed gnus-level-killed previous t))
15666
15667 ;; `gnus-group-change-level' is the fundamental function for changing
15668 ;; subscription levels of newsgroups.  This might mean just changing
15669 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15670 ;; again, which subscribes/unsubscribes a group, which is equally
15671 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15672 ;; from 8-9 to 1-7 means that you remove the group from the list of
15673 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15674 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15675 ;; which is trivial.
15676 ;; ENTRY can either be a string (newsgroup name) or a list (if
15677 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15678 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15679 ;; entries.
15680 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15681 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15682 ;; after.
15683 (defun gnus-group-change-level (entry level &optional oldlevel
15684                                       previous fromkilled)
15685   (let (group info active num)
15686     ;; Glean what info we can from the arguments
15687     (if (consp entry)
15688         (if fromkilled (setq group (nth 1 entry))
15689           (setq group (car (nth 2 entry))))
15690       (setq group entry))
15691     (if (and (stringp entry)
15692              oldlevel
15693              (< oldlevel gnus-level-zombie))
15694         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15695     (if (and (not oldlevel)
15696              (consp entry))
15697         (setq oldlevel (gnus-info-level (nth 2 entry)))
15698       (setq oldlevel (or oldlevel 9)))
15699     (if (stringp previous)
15700         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15701
15702     (if (and (>= oldlevel gnus-level-zombie)
15703              (gnus-gethash group gnus-newsrc-hashtb))
15704         ;; We are trying to subscribe a group that is already
15705         ;; subscribed.
15706         ()                              ; Do nothing.
15707
15708       (or (gnus-ephemeral-group-p group)
15709           (gnus-dribble-enter
15710            (format "(gnus-group-change-level %S %S %S %S %S)"
15711                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15712
15713       ;; Then we remove the newgroup from any old structures, if needed.
15714       ;; If the group was killed, we remove it from the killed or zombie
15715       ;; list.  If not, and it is in fact going to be killed, we remove
15716       ;; it from the newsrc hash table and assoc.
15717       (cond
15718        ((>= oldlevel gnus-level-zombie)
15719         (if (= oldlevel gnus-level-zombie)
15720             (setq gnus-zombie-list (delete group gnus-zombie-list))
15721           (setq gnus-killed-list (delete group gnus-killed-list))))
15722        (t
15723         (if (and (>= level gnus-level-zombie)
15724                  entry)
15725             (progn
15726               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15727               (if (nth 3 entry)
15728                   (setcdr (gnus-gethash (car (nth 3 entry))
15729                                         gnus-newsrc-hashtb)
15730                           (cdr entry)))
15731               (setcdr (cdr entry) (cdddr entry))))))
15732
15733       ;; Finally we enter (if needed) the list where it is supposed to
15734       ;; go, and change the subscription level.  If it is to be killed,
15735       ;; we enter it into the killed or zombie list.
15736       (cond 
15737        ((>= level gnus-level-zombie)
15738         ;; Remove from the hash table.
15739         (gnus-sethash group nil gnus-newsrc-hashtb)
15740         ;; We do not enter foreign groups into the list of dead
15741         ;; groups.
15742         (unless (gnus-group-foreign-p group)
15743           (if (= level gnus-level-zombie)
15744               (setq gnus-zombie-list (cons group gnus-zombie-list))
15745             (setq gnus-killed-list (cons group gnus-killed-list)))))
15746        (t
15747         ;; If the list is to be entered into the newsrc assoc, and
15748         ;; it was killed, we have to create an entry in the newsrc
15749         ;; hashtb format and fix the pointers in the newsrc assoc.
15750         (if (< oldlevel gnus-level-zombie)
15751             ;; It was alive, and it is going to stay alive, so we
15752             ;; just change the level and don't change any pointers or
15753             ;; hash table entries.
15754             (setcar (cdaddr entry) level)
15755           (if (listp entry)
15756               (setq info (cdr entry)
15757                     num (car entry))
15758             (setq active (gnus-active group))
15759             (setq num
15760                   (if active (- (1+ (cdr active)) (car active)) t))
15761             ;; Check whether the group is foreign.  If so, the
15762             ;; foreign select method has to be entered into the
15763             ;; info.
15764             (let ((method (or gnus-override-subscribe-method
15765                               (gnus-group-method group))))
15766               (if (eq method gnus-select-method)
15767                   (setq info (list group level nil))
15768                 (setq info (list group level nil nil method)))))
15769           (unless previous
15770             (setq previous
15771                   (let ((p gnus-newsrc-alist))
15772                     (while (cddr p)
15773                       (setq p (cdr p)))
15774                     p)))
15775           (setq entry (cons info (cddr previous)))
15776           (if (cdr previous)
15777               (progn
15778                 (setcdr (cdr previous) entry)
15779                 (gnus-sethash group (cons num (cdr previous))
15780                               gnus-newsrc-hashtb))
15781             (setcdr previous entry)
15782             (gnus-sethash group (cons num previous)
15783                           gnus-newsrc-hashtb))
15784           (when (cdr entry)
15785             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15786       (when gnus-group-change-level-function
15787         (funcall gnus-group-change-level-function group level oldlevel)))))
15788
15789 (defun gnus-kill-newsgroup (newsgroup)
15790   "Obsolete function.  Kills a newsgroup."
15791   (gnus-group-change-level
15792    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15793
15794 (defun gnus-check-bogus-newsgroups (&optional confirm)
15795   "Remove bogus newsgroups.
15796 If CONFIRM is non-nil, the user has to confirm the deletion of every
15797 newsgroup."
15798   (let ((newsrc (cdr gnus-newsrc-alist))
15799         bogus group entry info)
15800     (gnus-message 5 "Checking bogus newsgroups...")
15801     (unless (gnus-read-active-file-p)
15802       (gnus-read-active-file))
15803     (when (gnus-read-active-file-p)
15804       ;; Find all bogus newsgroup that are subscribed.
15805       (while newsrc
15806         (setq info (pop newsrc)
15807               group (gnus-info-group info))
15808         (unless (or (gnus-active group) ; Active
15809                     (gnus-info-method info) ; Foreign
15810                     (and confirm
15811                          (not (gnus-y-or-n-p
15812                                (format "Remove bogus newsgroup: %s " group)))))
15813           ;; Found a bogus newsgroup.
15814           (push group bogus)))
15815       ;; Remove all bogus subscribed groups by first killing them, and
15816       ;; then removing them from the list of killed groups.
15817       (while bogus
15818         (when (setq entry (gnus-gethash (setq group (pop bogus))
15819                                         gnus-newsrc-hashtb))
15820           (gnus-group-change-level entry gnus-level-killed)
15821           (setq gnus-killed-list (delete group gnus-killed-list))))
15822       ;; Then we remove all bogus groups from the list of killed and
15823       ;; zombie groups.  They are are removed without confirmation.
15824       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15825             killed)
15826         (while dead-lists
15827           (setq killed (symbol-value (car dead-lists)))
15828           (while killed
15829             (unless (gnus-active (setq group (pop killed)))
15830               ;; The group is bogus.
15831               ;; !!!Slow as hell.
15832               (set (car dead-lists)
15833                    (delete group (symbol-value (car dead-lists))))))
15834           (setq dead-lists (cdr dead-lists))))
15835       (gnus-message 5 "Checking bogus newsgroups...done"))))
15836
15837 (defun gnus-check-duplicate-killed-groups ()
15838   "Remove duplicates from the list of killed groups."
15839   (interactive)
15840   (let ((killed gnus-killed-list))
15841     (while killed
15842       (gnus-message 9 "%d" (length killed))
15843       (setcdr killed (delete (car killed) (cdr killed)))
15844       (setq killed (cdr killed)))))
15845
15846 ;; We want to inline a function from gnus-cache, so we cheat here:
15847 (eval-when-compile
15848   (provide 'gnus)
15849   (require 'gnus-cache))
15850
15851 (defun gnus-get-unread-articles-in-group (info active &optional update)
15852   (when active
15853     ;; Allow the backend to update the info in the group.
15854     (when (and update 
15855                (gnus-request-update-info
15856                 info (gnus-find-method-for-group (gnus-info-group info))))
15857       (gnus-activate-group (gnus-info-group info) nil t))
15858     (let* ((range (gnus-info-read info))
15859            (num 0))
15860       ;; If a cache is present, we may have to alter the active info.
15861       (when (and gnus-use-cache info)
15862         (inline (gnus-cache-possibly-alter-active 
15863                  (gnus-info-group info) active)))
15864       ;; Modify the list of read articles according to what articles
15865       ;; are available; then tally the unread articles and add the
15866       ;; number to the group hash table entry.
15867       (cond
15868        ((zerop (cdr active))
15869         (setq num 0))
15870        ((not range)
15871         (setq num (- (1+ (cdr active)) (car active))))
15872        ((not (listp (cdr range)))
15873         ;; Fix a single (num . num) range according to the
15874         ;; active hash table.
15875         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15876         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15877         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15878         ;; Compute number of unread articles.
15879         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15880        (t
15881         ;; The read list is a list of ranges.  Fix them according to
15882         ;; the active hash table.
15883         ;; First peel off any elements that are below the lower
15884         ;; active limit.
15885         (while (and (cdr range)
15886                     (>= (car active)
15887                         (or (and (atom (cadr range)) (cadr range))
15888                             (caadr range))))
15889           (if (numberp (car range))
15890               (setcar range
15891                       (cons (car range)
15892                             (or (and (numberp (cadr range))
15893                                      (cadr range))
15894                                 (cdadr range))))
15895             (setcdr (car range)
15896                     (or (and (numberp (nth 1 range)) (nth 1 range))
15897                         (cdadr range))))
15898           (setcdr range (cddr range)))
15899         ;; Adjust the first element to be the same as the lower limit.
15900         (if (and (not (atom (car range)))
15901                  (< (cdar range) (car active)))
15902             (setcdr (car range) (1- (car active))))
15903         ;; Then we want to peel off any elements that are higher
15904         ;; than the upper active limit.
15905         (let ((srange range))
15906           ;; Go past all legal elements.
15907           (while (and (cdr srange)
15908                       (<= (or (and (atom (cadr srange))
15909                                    (cadr srange))
15910                               (caadr srange)) (cdr active)))
15911             (setq srange (cdr srange)))
15912           (if (cdr srange)
15913               ;; Nuke all remaining illegal elements.
15914               (setcdr srange nil))
15915
15916           ;; Adjust the final element.
15917           (if (and (not (atom (car srange)))
15918                    (> (cdar srange) (cdr active)))
15919               (setcdr (car srange) (cdr active))))
15920         ;; Compute the number of unread articles.
15921         (while range
15922           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15923                                       (cdar range)))
15924                               (or (and (atom (car range)) (car range))
15925                                   (caar range)))))
15926           (setq range (cdr range)))
15927         (setq num (max 0 (- (cdr active) num)))))
15928       ;; Set the number of unread articles.
15929       (when info
15930         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
15931       num)))
15932
15933 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15934 ;; and compute how many unread articles there are in each group.
15935 (defun gnus-get-unread-articles (&optional level)
15936   (let* ((newsrc (cdr gnus-newsrc-alist))
15937          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15938          (foreign-level
15939           (min
15940            (cond ((and gnus-activate-foreign-newsgroups
15941                        (not (numberp gnus-activate-foreign-newsgroups)))
15942                   (1+ gnus-level-subscribed))
15943                  ((numberp gnus-activate-foreign-newsgroups)
15944                   gnus-activate-foreign-newsgroups)
15945                  (t 0))
15946            level))
15947          info group active method)
15948     (gnus-message 5 "Checking new news...")
15949
15950     (while newsrc
15951       (setq active (gnus-active (setq group (gnus-info-group
15952                                              (setq info (pop newsrc))))))
15953
15954       ;; Check newsgroups.  If the user doesn't want to check them, or
15955       ;; they can't be checked (for instance, if the news server can't
15956       ;; be reached) we just set the number of unread articles in this
15957       ;; newsgroup to t.  This means that Gnus thinks that there are
15958       ;; unread articles, but it has no idea how many.
15959       (if (and (setq method (gnus-info-method info))
15960                (not (gnus-server-equal
15961                      gnus-select-method
15962                      (setq method (gnus-server-get-method nil method))))
15963                (not (gnus-secondary-method-p method)))
15964           ;; These groups are foreign.  Check the level.
15965           (when (<= (gnus-info-level info) foreign-level)
15966             (setq active (gnus-activate-group group 'scan))
15967             (unless (inline (gnus-virtual-group-p group))
15968               (inline (gnus-close-group group)))
15969             (when (fboundp (intern (concat (symbol-name (car method))
15970                                            "-request-update-info")))
15971               (inline (gnus-request-update-info info method))))
15972         ;; These groups are native or secondary.
15973         (when (and (<= (gnus-info-level info) level)
15974                    (not gnus-read-active-file))
15975           (setq active (gnus-activate-group group 'scan))
15976           (inline (gnus-close-group group))))
15977
15978       ;; Get the number of unread articles in the group.
15979       (if active
15980           (inline (gnus-get-unread-articles-in-group info active))
15981         ;; The group couldn't be reached, so we nix out the number of
15982         ;; unread articles and stuff.
15983         (gnus-set-active group nil)
15984         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
15985
15986     (gnus-message 5 "Checking new news...done")))
15987
15988 ;; Create a hash table out of the newsrc alist.  The `car's of the
15989 ;; alist elements are used as keys.
15990 (defun gnus-make-hashtable-from-newsrc-alist ()
15991   (let ((alist gnus-newsrc-alist)
15992         (ohashtb gnus-newsrc-hashtb)
15993         prev)
15994     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15995     (setq alist
15996           (setq prev (setq gnus-newsrc-alist
15997                            (if (equal (caar gnus-newsrc-alist)
15998                                       "dummy.group")
15999                                gnus-newsrc-alist
16000                              (cons (list "dummy.group" 0 nil) alist)))))
16001     (while alist
16002       (gnus-sethash
16003        (caar alist)
16004        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
16005              prev)
16006        gnus-newsrc-hashtb)
16007       (setq prev alist
16008             alist (cdr alist)))))
16009
16010 (defun gnus-make-hashtable-from-killed ()
16011   "Create a hash table from the killed and zombie lists."
16012   (let ((lists '(gnus-killed-list gnus-zombie-list))
16013         list)
16014     (setq gnus-killed-hashtb
16015           (gnus-make-hashtable
16016            (+ (length gnus-killed-list) (length gnus-zombie-list))))
16017     (while (setq list (pop lists))
16018       (setq list (symbol-value list))
16019       (while list
16020         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
16021
16022 (defun gnus-activate-group (group &optional scan dont-check)
16023   ;; Check whether a group has been activated or not.
16024   ;; If SCAN, request a scan of that group as well.
16025   (let ((method (gnus-find-method-for-group group))
16026         active)
16027     (and (gnus-check-server method)
16028          ;; We escape all bugs and quit here to make it possible to
16029          ;; continue if a group is so out-there that it reports bugs
16030          ;; and stuff.
16031          (progn
16032            (and scan
16033                 (gnus-check-backend-function 'request-scan (car method))
16034                 (gnus-request-scan group method))
16035            t)
16036          (condition-case ()
16037              (gnus-request-group group dont-check)
16038         ;   (error nil)
16039            (quit nil))
16040          (save-excursion
16041            (set-buffer nntp-server-buffer)
16042            (goto-char (point-min))
16043            ;; Parse the result we got from `gnus-request-group'.
16044            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
16045                 (progn
16046                   (goto-char (match-beginning 1))
16047                   (gnus-set-active
16048                    group (setq active (cons (read (current-buffer))
16049                                             (read (current-buffer)))))
16050                   ;; Return the new active info.
16051                   active))))))
16052
16053 (defun gnus-update-read-articles (group unread)
16054   "Update the list of read and ticked articles in GROUP using the
16055 UNREAD and TICKED lists.
16056 Note: UNSELECTED has to be sorted over `<'.
16057 Returns whether the updating was successful."
16058   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
16059          (entry (gnus-gethash group gnus-newsrc-hashtb))
16060          (info (nth 2 entry))
16061          (prev 1)
16062          (unread (sort (copy-sequence unread) '<))
16063          read)
16064     (if (or (not info) (not active))
16065         ;; There is no info on this group if it was, in fact,
16066         ;; killed.  Gnus stores no information on killed groups, so
16067         ;; there's nothing to be done.
16068         ;; One could store the information somewhere temporarily,
16069         ;; perhaps...  Hmmm...
16070         ()
16071       ;; Remove any negative articles numbers.
16072       (while (and unread (< (car unread) 0))
16073         (setq unread (cdr unread)))
16074       ;; Remove any expired article numbers
16075       (while (and unread (< (car unread) (car active)))
16076         (setq unread (cdr unread)))
16077       ;; Compute the ranges of read articles by looking at the list of
16078       ;; unread articles.
16079       (while unread
16080         (if (/= (car unread) prev)
16081             (setq read (cons (if (= prev (1- (car unread))) prev
16082                                (cons prev (1- (car unread)))) read)))
16083         (setq prev (1+ (car unread)))
16084         (setq unread (cdr unread)))
16085       (when (<= prev (cdr active))
16086         (setq read (cons (cons prev (cdr active)) read)))
16087       ;; Enter this list into the group info.
16088       (gnus-info-set-read
16089        info (if (> (length read) 1) (nreverse read) read))
16090       ;; Set the number of unread articles in gnus-newsrc-hashtb.
16091       (gnus-get-unread-articles-in-group info (gnus-active group))
16092       t)))
16093
16094 (defun gnus-make-articles-unread (group articles)
16095   "Mark ARTICLES in GROUP as unread."
16096   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
16097                           (gnus-gethash (gnus-group-real-name group)
16098                                         gnus-newsrc-hashtb))))
16099          (ranges (gnus-info-read info))
16100          news article)
16101     (while articles
16102       (when (gnus-member-of-range
16103              (setq article (pop articles)) ranges)
16104         (setq news (cons article news))))
16105     (when news
16106       (gnus-info-set-read
16107        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
16108       (gnus-group-update-group group t))))
16109
16110 ;; Enter all dead groups into the hashtb.
16111 (defun gnus-update-active-hashtb-from-killed ()
16112   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
16113         (lists (list gnus-killed-list gnus-zombie-list))
16114         killed)
16115     (while lists
16116       (setq killed (car lists))
16117       (while killed
16118         (gnus-sethash (car killed) nil hashtb)
16119         (setq killed (cdr killed)))
16120       (setq lists (cdr lists)))))
16121
16122 (defun gnus-get-killed-groups ()
16123   "Go through the active hashtb and all all unknown groups as killed."
16124   ;; First make sure active file has been read.
16125   (unless (gnus-read-active-file-p)
16126     (let ((gnus-read-active-file t))
16127       (gnus-read-active-file)))
16128   (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
16129   ;; Go through all newsgroups that are known to Gnus - enlarge kill list.
16130   (mapatoms
16131    (lambda (sym)
16132      (let ((groups 0)
16133            (group (symbol-name sym)))
16134        (if (or (null group)
16135                (gnus-gethash group gnus-killed-hashtb)
16136                (gnus-gethash group gnus-newsrc-hashtb))
16137            ()
16138          (let ((do-sub (gnus-matches-options-n group)))
16139            (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
16140                ()
16141              (setq groups (1+ groups))
16142              (setq gnus-killed-list
16143                    (cons group gnus-killed-list))
16144              (gnus-sethash group group gnus-killed-hashtb))))))
16145    gnus-active-hashtb))
16146
16147 ;; Get the active file(s) from the backend(s).
16148 (defun gnus-read-active-file ()
16149   (gnus-group-set-mode-line)
16150   (let ((methods 
16151          (append
16152           (if (gnus-check-server gnus-select-method)
16153               ;; The native server is available.
16154               (cons gnus-select-method gnus-secondary-select-methods)
16155             ;; The native server is down, so we just do the
16156             ;; secondary ones.
16157             gnus-secondary-select-methods)
16158           ;; Also read from the archive server.
16159           (when gnus-message-archive-method
16160             (list "archive"))))
16161         list-type)
16162     (setq gnus-have-read-active-file nil)
16163     (save-excursion
16164       (set-buffer nntp-server-buffer)
16165       (while methods
16166         (let* ((method (if (stringp (car methods))
16167                            (gnus-server-get-method nil (car methods))
16168                          (car methods)))
16169                (where (nth 1 method))
16170                (mesg (format "Reading active file%s via %s..."
16171                              (if (and where (not (zerop (length where))))
16172                                  (concat " from " where) "")
16173                              (car method))))
16174           (gnus-message 5 mesg)
16175           (when (gnus-check-server method)
16176             ;; Request that the backend scan its incoming messages.
16177             (and (gnus-check-backend-function 'request-scan (car method))
16178                  (gnus-request-scan nil method))
16179             (cond
16180              ((and (eq gnus-read-active-file 'some)
16181                    (gnus-check-backend-function 'retrieve-groups (car method)))
16182               (let ((newsrc (cdr gnus-newsrc-alist))
16183                     (gmethod (gnus-server-get-method nil method))
16184                     groups info)
16185                 (while (setq info (pop newsrc))
16186                   (when (gnus-server-equal
16187                          (gnus-find-method-for-group 
16188                           (gnus-info-group info) info)
16189                          gmethod)
16190                     (push (gnus-group-real-name (gnus-info-group info)) 
16191                           groups)))
16192                 (when groups
16193                   (gnus-check-server method)
16194                   (setq list-type (gnus-retrieve-groups groups method))
16195                   (cond
16196                    ((not list-type)
16197                     (gnus-error
16198                      1.2 "Cannot read partial active file from %s server."
16199                      (car method)))
16200                    ((eq list-type 'active)
16201                     (gnus-active-to-gnus-format method gnus-active-hashtb))
16202                    (t
16203                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
16204              (t
16205               (if (not (gnus-request-list method))
16206                   (unless (equal method gnus-message-archive-method)
16207                     (gnus-error 1 "Cannot read active file from %s server."
16208                                 (car method)))
16209                 (gnus-message 5 mesg)
16210                 (gnus-active-to-gnus-format method gnus-active-hashtb)
16211                 ;; We mark this active file as read.
16212                 (push method gnus-have-read-active-file)
16213                 (gnus-message 5 "%sdone" mesg))))))
16214         (setq methods (cdr methods))))))
16215
16216 ;; Read an active file and place the results in `gnus-active-hashtb'.
16217 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
16218   (unless method
16219     (setq method gnus-select-method))
16220   (let ((cur (current-buffer))
16221         (hashtb (or hashtb
16222                     (if (and gnus-active-hashtb
16223                              (not (equal method gnus-select-method)))
16224                         gnus-active-hashtb
16225                       (setq gnus-active-hashtb
16226                             (if (equal method gnus-select-method)
16227                                 (gnus-make-hashtable
16228                                  (count-lines (point-min) (point-max)))
16229                               (gnus-make-hashtable 4096)))))))
16230     ;; Delete unnecessary lines.
16231     (goto-char (point-min))
16232     (while (search-forward "\nto." nil t)
16233       (delete-region (1+ (match-beginning 0))
16234                      (progn (forward-line 1) (point))))
16235     (or (string= gnus-ignored-newsgroups "")
16236         (progn
16237           (goto-char (point-min))
16238           (delete-matching-lines gnus-ignored-newsgroups)))
16239     ;; Make the group names readable as a lisp expression even if they
16240     ;; contain special characters.
16241     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
16242     (goto-char (point-max))
16243     (while (re-search-backward "[][';?()#]" nil t)
16244       (insert ?\\))
16245     ;; If these are groups from a foreign select method, we insert the
16246     ;; group prefix in front of the group names.
16247     (and method (not (gnus-server-equal
16248                       (gnus-server-get-method nil method)
16249                       (gnus-server-get-method nil gnus-select-method)))
16250          (let ((prefix (gnus-group-prefixed-name "" method)))
16251            (goto-char (point-min))
16252            (while (and (not (eobp))
16253                        (progn (insert prefix)
16254                               (zerop (forward-line 1)))))))
16255     ;; Store the active file in a hash table.
16256     (goto-char (point-min))
16257     (if (string-match "%[oO]" gnus-group-line-format)
16258         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
16259         ;; If we want information on moderated groups, we use this
16260         ;; loop...
16261         (let* ((mod-hashtb (make-vector 7 0))
16262                (m (intern "m" mod-hashtb))
16263                group max min)
16264           (while (not (eobp))
16265             (condition-case nil
16266                 (progn
16267                   (narrow-to-region (point) (gnus-point-at-eol))
16268                   (setq group (let ((obarray hashtb)) (read cur)))
16269                   (if (and (numberp (setq max (read cur)))
16270                            (numberp (setq min (read cur)))
16271                            (progn
16272                              (skip-chars-forward " \t")
16273                              (not
16274                               (or (= (following-char) ?=)
16275                                   (= (following-char) ?x)
16276                                   (= (following-char) ?j)))))
16277                       (set group (cons min max))
16278                     (set group nil))
16279                   ;; Enter moderated groups into a list.
16280                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
16281                       (setq gnus-moderated-list
16282                             (cons (symbol-name group) gnus-moderated-list))))
16283               (error
16284                (and group
16285                     (symbolp group)
16286                     (set group nil))))
16287             (widen)
16288             (forward-line 1)))
16289       ;; And if we do not care about moderation, we use this loop,
16290       ;; which is faster.
16291       (let (group max min)
16292         (while (not (eobp))
16293           (condition-case ()
16294               (progn
16295                 (narrow-to-region (point) (gnus-point-at-eol))
16296                 ;; group gets set to a symbol interned in the hash table
16297                 ;; (what a hack!!) - jwz
16298                 (setq group (let ((obarray hashtb)) (read cur)))
16299                 (if (and (numberp (setq max (read cur)))
16300                          (numberp (setq min (read cur)))
16301                          (progn
16302                            (skip-chars-forward " \t")
16303                            (not
16304                             (or (= (following-char) ?=)
16305                                 (= (following-char) ?x)
16306                                 (= (following-char) ?j)))))
16307                     (set group (cons min max))
16308                   (set group nil)))
16309             (error
16310              (progn
16311                (and group
16312                     (symbolp group)
16313                     (set group nil))
16314                (or ignore-errors
16315                    (gnus-message 3 "Warning - illegal active: %s"
16316                                  (buffer-substring
16317                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
16318           (widen)
16319           (forward-line 1))))))
16320
16321 (defun gnus-groups-to-gnus-format (method &optional hashtb)
16322   ;; Parse a "groups" active file.
16323   (let ((cur (current-buffer))
16324         (hashtb (or hashtb
16325                     (if (and method gnus-active-hashtb)
16326                         gnus-active-hashtb
16327                       (setq gnus-active-hashtb
16328                             (gnus-make-hashtable
16329                              (count-lines (point-min) (point-max)))))))
16330         (prefix (and method
16331                      (not (gnus-server-equal
16332                            (gnus-server-get-method nil method)
16333                            (gnus-server-get-method nil gnus-select-method)))
16334                      (gnus-group-prefixed-name "" method))))
16335
16336     (goto-char (point-min))
16337     ;; We split this into to separate loops, one with the prefix
16338     ;; and one without to speed the reading up somewhat.
16339     (if prefix
16340         (let (min max opoint group)
16341           (while (not (eobp))
16342             (condition-case ()
16343                 (progn
16344                   (read cur) (read cur)
16345                   (setq min (read cur)
16346                         max (read cur)
16347                         opoint (point))
16348                   (skip-chars-forward " \t")
16349                   (insert prefix)
16350                   (goto-char opoint)
16351                   (set (let ((obarray hashtb)) (read cur))
16352                        (cons min max)))
16353               (error (and group (symbolp group) (set group nil))))
16354             (forward-line 1)))
16355       (let (min max group)
16356         (while (not (eobp))
16357           (condition-case ()
16358               (if (= (following-char) ?2)
16359                   (progn
16360                     (read cur) (read cur)
16361                     (setq min (read cur)
16362                           max (read cur))
16363                     (set (setq group (let ((obarray hashtb)) (read cur)))
16364                          (cons min max))))
16365             (error (and group (symbolp group) (set group nil))))
16366           (forward-line 1))))))
16367
16368 (defun gnus-read-newsrc-file (&optional force)
16369   "Read startup file.
16370 If FORCE is non-nil, the .newsrc file is read."
16371   ;; Reset variables that might be defined in the .newsrc.eld file.
16372   (let ((variables gnus-variable-list))
16373     (while variables
16374       (set (car variables) nil)
16375       (setq variables (cdr variables))))
16376   (let* ((newsrc-file gnus-current-startup-file)
16377          (quick-file (concat newsrc-file ".el")))
16378     (save-excursion
16379       ;; We always load the .newsrc.eld file.  If always contains
16380       ;; much information that can not be gotten from the .newsrc
16381       ;; file (ticked articles, killed groups, foreign methods, etc.)
16382       (gnus-read-newsrc-el-file quick-file)
16383
16384       (if (and (file-exists-p gnus-current-startup-file)
16385                (or force
16386                    (and (file-newer-than-file-p newsrc-file quick-file)
16387                         (file-newer-than-file-p newsrc-file
16388                                                 (concat quick-file "d")))
16389                    (not gnus-newsrc-alist)))
16390           ;; We read the .newsrc file.  Note that if there if a
16391           ;; .newsrc.eld file exists, it has already been read, and
16392           ;; the `gnus-newsrc-hashtb' has been created.  While reading
16393           ;; the .newsrc file, Gnus will only use the information it
16394           ;; can find there for changing the data already read -
16395           ;; ie. reading the .newsrc file will not trash the data
16396           ;; already read (except for read articles).
16397           (save-excursion
16398             (gnus-message 5 "Reading %s..." newsrc-file)
16399             (set-buffer (find-file-noselect newsrc-file))
16400             (buffer-disable-undo (current-buffer))
16401             (gnus-newsrc-to-gnus-format)
16402             (kill-buffer (current-buffer))
16403             (gnus-message 5 "Reading %s...done" newsrc-file)))
16404
16405       ;; Read any slave files.
16406       (unless gnus-slave
16407         (gnus-master-read-slave-newsrc))
16408       
16409       ;; Convert old to new.
16410       (gnus-convert-old-newsrc))))
16411
16412 (defun gnus-continuum-version (version)
16413   "Return VERSION as a floating point number."
16414   (when (or (string-match "^\\([^ ]+\\)? ?Gnus v?\\([0-9.]+\\)$" version)
16415             (string-match "^\\(.?\\)gnus-\\([0-9.]+\\)$" version))
16416     (let* ((alpha (and (match-beginning 1) (match-string 1 version)))
16417            (number (match-string 2 version))
16418            major minor least)
16419       (string-match "\\([0-9]\\)\\.\\([0-9]+\\)\\.?\\([0-9]+\\)?" number)
16420       (setq major (string-to-number (match-string 1 number)))
16421       (setq minor (string-to-number (match-string 2 number)))
16422       (setq least (if (match-beginning 3)
16423                       (string-to-number (match-string 3 number))
16424                     0))
16425       (string-to-number
16426        (if (zerop major)
16427            (format "%s00%02d%02d"
16428                    (cond 
16429                     ((member alpha '("(ding)" "d")) "4.99")
16430                     ((member alpha '("September" "s")) "5.01")
16431                     ((member alpha '("Red" "r")) "5.03"))
16432                    minor least)
16433          (format "%d.%02d%02d" major minor least))))))
16434
16435 (defun gnus-convert-old-newsrc ()
16436   "Convert old newsrc into the new format, if needed."
16437   (let ((fcv (and gnus-newsrc-file-version
16438                   (gnus-continuum-version gnus-newsrc-file-version))))
16439     (cond
16440      ;; No .newsrc.eld file was loaded.
16441      ((null fcv) nil)
16442      ;; Gnus 5 .newsrc.eld was loaded.
16443      ((< fcv (gnus-continuum-version "September Gnus v0.1"))
16444       (gnus-convert-old-ticks)))))
16445
16446 (defun gnus-convert-old-ticks ()
16447   (let ((newsrc (cdr gnus-newsrc-alist))
16448         marks info dormant ticked)
16449     (while (setq info (pop newsrc))
16450       (when (setq marks (gnus-info-marks info))
16451         (setq dormant (cdr (assq 'dormant marks))
16452               ticked (cdr (assq 'tick marks)))
16453         (when (or dormant ticked)
16454           (gnus-info-set-read
16455            info
16456            (gnus-add-to-range
16457             (gnus-info-read info)
16458             (nconc (gnus-uncompress-range dormant)
16459                    (gnus-uncompress-range ticked)))))))))
16460
16461 (defun gnus-read-newsrc-el-file (file)
16462   (let ((ding-file (concat file "d")))
16463     ;; We always, always read the .eld file.
16464     (gnus-message 5 "Reading %s..." ding-file)
16465     (let (gnus-newsrc-assoc)
16466       (condition-case nil
16467           (load ding-file t t t)
16468         (error
16469          (gnus-error 1 "Error in %s" ding-file)))
16470       (when gnus-newsrc-assoc
16471         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
16472     (gnus-make-hashtable-from-newsrc-alist)
16473     (when (file-newer-than-file-p file ding-file)
16474       ;; Old format quick file
16475       (gnus-message 5 "Reading %s..." file)
16476       ;; The .el file is newer than the .eld file, so we read that one
16477       ;; as well.
16478       (gnus-read-old-newsrc-el-file file))))
16479
16480 ;; Parse the old-style quick startup file
16481 (defun gnus-read-old-newsrc-el-file (file)
16482   (let (newsrc killed marked group m info)
16483     (prog1
16484         (let ((gnus-killed-assoc nil)
16485               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
16486           (prog1
16487               (condition-case nil
16488                   (load file t t t)
16489                 (error nil))
16490             (setq newsrc gnus-newsrc-assoc
16491                   killed gnus-killed-assoc
16492                   marked gnus-marked-assoc)))
16493       (setq gnus-newsrc-alist nil)
16494       (while (setq group (pop newsrc))
16495         (if (setq info (gnus-get-info (car group)))
16496             (progn
16497               (gnus-info-set-read info (cddr group))
16498               (gnus-info-set-level
16499                info (if (nth 1 group) gnus-level-default-subscribed
16500                       gnus-level-default-unsubscribed))
16501               (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
16502           (push (setq info
16503                       (list (car group)
16504                             (if (nth 1 group) gnus-level-default-subscribed
16505                               gnus-level-default-unsubscribed)
16506                             (cddr group)))
16507                 gnus-newsrc-alist))
16508         ;; Copy marks into info.
16509         (when (setq m (assoc (car group) marked))
16510           (unless (nthcdr 3 info)
16511             (nconc info (list nil)))
16512           (gnus-info-set-marks
16513            info (list (cons 'tick (gnus-compress-sequence 
16514                                    (sort (cdr m) '<) t))))))
16515       (setq newsrc killed)
16516       (while newsrc
16517         (setcar newsrc (caar newsrc))
16518         (setq newsrc (cdr newsrc)))
16519       (setq gnus-killed-list killed))
16520     ;; The .el file version of this variable does not begin with
16521     ;; "options", while the .eld version does, so we just add it if it
16522     ;; isn't there.
16523     (and
16524      gnus-newsrc-options
16525      (progn
16526        (and (not (string-match "^ *options" gnus-newsrc-options))
16527             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
16528        (and (not (string-match "\n$" gnus-newsrc-options))
16529             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
16530        ;; Finally, if we read some options lines, we parse them.
16531        (or (string= gnus-newsrc-options "")
16532            (gnus-newsrc-parse-options gnus-newsrc-options))))
16533
16534     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
16535     (gnus-make-hashtable-from-newsrc-alist)))
16536
16537 (defun gnus-make-newsrc-file (file)
16538   "Make server dependent file name by catenating FILE and server host name."
16539   (let* ((file (expand-file-name file nil))
16540          (real-file (concat file "-" (nth 1 gnus-select-method))))
16541     (if (or (file-exists-p real-file)
16542             (file-exists-p (concat real-file ".el"))
16543             (file-exists-p (concat real-file ".eld")))
16544         real-file file)))
16545
16546 (defun gnus-newsrc-to-gnus-format ()
16547   (setq gnus-newsrc-options "")
16548   (setq gnus-newsrc-options-n nil)
16549
16550   (or gnus-active-hashtb
16551       (setq gnus-active-hashtb (make-vector 4095 0)))
16552   (let ((buf (current-buffer))
16553         (already-read (> (length gnus-newsrc-alist) 1))
16554         group subscribed options-symbol newsrc Options-symbol
16555         symbol reads num1)
16556     (goto-char (point-min))
16557     ;; We intern the symbol `options' in the active hashtb so that we
16558     ;; can `eq' against it later.
16559     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
16560     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
16561
16562     (while (not (eobp))
16563       ;; We first read the first word on the line by narrowing and
16564       ;; then reading into `gnus-active-hashtb'.  Most groups will
16565       ;; already exist in that hashtb, so this will save some string
16566       ;; space.
16567       (narrow-to-region
16568        (point)
16569        (progn (skip-chars-forward "^ \t!:\n") (point)))
16570       (goto-char (point-min))
16571       (setq symbol
16572             (and (/= (point-min) (point-max))
16573                  (let ((obarray gnus-active-hashtb)) (read buf))))
16574       (widen)
16575       ;; Now, the symbol we have read is either `options' or a group
16576       ;; name.  If it is an options line, we just add it to a string.
16577       (cond
16578        ((or (eq symbol options-symbol)
16579             (eq symbol Options-symbol))
16580         (setq gnus-newsrc-options
16581               ;; This concating is quite inefficient, but since our
16582               ;; thorough studies show that approx 99.37% of all
16583               ;; .newsrc files only contain a single options line, we
16584               ;; don't give a damn, frankly, my dear.
16585               (concat gnus-newsrc-options
16586                       (buffer-substring
16587                        (gnus-point-at-bol)
16588                        ;; Options may continue on the next line.
16589                        (or (and (re-search-forward "^[^ \t]" nil 'move)
16590                                 (progn (beginning-of-line) (point)))
16591                            (point)))))
16592         (forward-line -1))
16593        (symbol
16594         ;; Group names can be just numbers.  
16595         (when (numberp symbol) 
16596           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
16597         (or (boundp symbol) (set symbol nil))
16598         ;; It was a group name.
16599         (setq subscribed (= (following-char) ?:)
16600               group (symbol-name symbol)
16601               reads nil)
16602         (if (eolp)
16603             ;; If the line ends here, this is clearly a buggy line, so
16604             ;; we put point a the beginning of line and let the cond
16605             ;; below do the error handling.
16606             (beginning-of-line)
16607           ;; We skip to the beginning of the ranges.
16608           (skip-chars-forward "!: \t"))
16609         ;; We are now at the beginning of the list of read articles.
16610         ;; We read them range by range.
16611         (while
16612             (cond
16613              ((looking-at "[0-9]+")
16614               ;; We narrow and read a number instead of buffer-substring/
16615               ;; string-to-int because it's faster.  narrow/widen is
16616               ;; faster than save-restriction/narrow, and save-restriction
16617               ;; produces a garbage object.
16618               (setq num1 (progn
16619                            (narrow-to-region (match-beginning 0) (match-end 0))
16620                            (read buf)))
16621               (widen)
16622               ;; If the next character is a dash, then this is a range.
16623               (if (= (following-char) ?-)
16624                   (progn
16625                     ;; We read the upper bound of the range.
16626                     (forward-char 1)
16627                     (if (not (looking-at "[0-9]+"))
16628                         ;; This is a buggy line, by we pretend that
16629                         ;; it's kinda OK.  Perhaps the user should be
16630                         ;; dinged?
16631                         (setq reads (cons num1 reads))
16632                       (setq reads
16633                             (cons
16634                              (cons num1
16635                                    (progn
16636                                      (narrow-to-region (match-beginning 0)
16637                                                        (match-end 0))
16638                                      (read buf)))
16639                              reads))
16640                       (widen)))
16641                 ;; It was just a simple number, so we add it to the
16642                 ;; list of ranges.
16643                 (setq reads (cons num1 reads)))
16644               ;; If the next char in ?\n, then we have reached the end
16645               ;; of the line and return nil.
16646               (/= (following-char) ?\n))
16647              ((= (following-char) ?\n)
16648               ;; End of line, so we end.
16649               nil)
16650              (t
16651               ;; Not numbers and not eol, so this might be a buggy
16652               ;; line...
16653               (or (eobp)
16654                   ;; If it was eob instead of ?\n, we allow it.
16655                   (progn
16656                     ;; The line was buggy.
16657                     (setq group nil)
16658                     (gnus-error 3.1 "Mangled line: %s"
16659                                 (buffer-substring (gnus-point-at-bol)
16660                                                   (gnus-point-at-eol)))))
16661               nil))
16662           ;; Skip past ", ".  Spaces are illegal in these ranges, but
16663           ;; we allow them, because it's a common mistake to put a
16664           ;; space after the comma.
16665           (skip-chars-forward ", "))
16666
16667         ;; We have already read .newsrc.eld, so we gently update the
16668         ;; data in the hash table with the information we have just
16669         ;; read.
16670         (when group
16671           (let ((info (gnus-get-info group))
16672                 level)
16673             (if info
16674                 ;; There is an entry for this file in the alist.
16675                 (progn
16676                   (gnus-info-set-read info (nreverse reads))
16677                   ;; We update the level very gently.  In fact, we
16678                   ;; only change it if there's been a status change
16679                   ;; from subscribed to unsubscribed, or vice versa.
16680                   (setq level (gnus-info-level info))
16681                   (cond ((and (<= level gnus-level-subscribed)
16682                               (not subscribed))
16683                          (setq level (if reads
16684                                          gnus-level-default-unsubscribed
16685                                        (1+ gnus-level-default-unsubscribed))))
16686                         ((and (> level gnus-level-subscribed) subscribed)
16687                          (setq level gnus-level-default-subscribed)))
16688                   (gnus-info-set-level info level))
16689               ;; This is a new group.
16690               (setq info (list group
16691                                (if subscribed
16692                                    gnus-level-default-subscribed
16693                                  (if reads
16694                                      (1+ gnus-level-subscribed)
16695                                    gnus-level-default-unsubscribed))
16696                                (nreverse reads))))
16697             (setq newsrc (cons info newsrc))))))
16698       (forward-line 1))
16699
16700     (setq newsrc (nreverse newsrc))
16701
16702     (if (not already-read)
16703         ()
16704       ;; We now have two newsrc lists - `newsrc', which is what we
16705       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16706       ;; what we've read from .newsrc.eld.  We have to merge these
16707       ;; lists.  We do this by "attaching" any (foreign) groups in the
16708       ;; gnus-newsrc-alist to the (native) group that precedes them.
16709       (let ((rc (cdr gnus-newsrc-alist))
16710             (prev gnus-newsrc-alist)
16711             entry mentry)
16712         (while rc
16713           (or (null (nth 4 (car rc)))   ; It's a native group.
16714               (assoc (caar rc) newsrc) ; It's already in the alist.
16715               (if (setq entry (assoc (caar prev) newsrc))
16716                   (setcdr (setq mentry (memq entry newsrc))
16717                           (cons (car rc) (cdr mentry)))
16718                 (setq newsrc (cons (car rc) newsrc))))
16719           (setq prev rc
16720                 rc (cdr rc)))))
16721
16722     (setq gnus-newsrc-alist newsrc)
16723     ;; We make the newsrc hashtb.
16724     (gnus-make-hashtable-from-newsrc-alist)
16725
16726     ;; Finally, if we read some options lines, we parse them.
16727     (or (string= gnus-newsrc-options "")
16728         (gnus-newsrc-parse-options gnus-newsrc-options))))
16729
16730 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16731 ;; The return value will be a list on the form
16732 ;; ((regexp1 . ignore)
16733 ;;  (regexp2 . subscribe)...)
16734 ;; When handling new newsgroups, groups that match a `ignore' regexp
16735 ;; will be ignored, and groups that match a `subscribe' regexp will be
16736 ;; subscribed.  A line like
16737 ;; options -n !all rec.all
16738 ;; will lead to a list that looks like
16739 ;; (("^rec\\..+" . subscribe)
16740 ;;  ("^.+" . ignore))
16741 ;; So all "rec.*" groups will be subscribed, while all the other
16742 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16743 ;; different from "options -n rec.all !all".
16744 (defun gnus-newsrc-parse-options (options)
16745   (let (out eol)
16746     (save-excursion
16747       (gnus-set-work-buffer)
16748       (insert (regexp-quote options))
16749       ;; First we treat all continuation lines.
16750       (goto-char (point-min))
16751       (while (re-search-forward "\n[ \t]+" nil t)
16752         (replace-match " " t t))
16753       ;; Then we transform all "all"s into ".+"s.
16754       (goto-char (point-min))
16755       (while (re-search-forward "\\ball\\b" nil t)
16756         (replace-match ".+" t t))
16757       (goto-char (point-min))
16758       ;; We remove all other options than the "-n" ones.
16759       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16760         (replace-match " ")
16761         (forward-char -1))
16762       (goto-char (point-min))
16763
16764       ;; We are only interested in "options -n" lines - we
16765       ;; ignore the other option lines.
16766       (while (re-search-forward "[ \t]-n" nil t)
16767         (setq eol
16768               (or (save-excursion
16769                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16770                          (- (point) 2)))
16771                   (gnus-point-at-eol)))
16772         ;; Search for all "words"...
16773         (while (re-search-forward "[^ \t,\n]+" eol t)
16774           (if (= (char-after (match-beginning 0)) ?!)
16775               ;; If the word begins with a bang (!), this is a "not"
16776               ;; spec.  We put this spec (minus the bang) and the
16777               ;; symbol `ignore' into the list.
16778               (setq out (cons (cons (concat
16779                                      "^" (buffer-substring
16780                                           (1+ (match-beginning 0))
16781                                           (match-end 0)))
16782                                     'ignore) out))
16783             ;; There was no bang, so this is a "yes" spec.
16784             (setq out (cons (cons (concat "^" (match-string 0))
16785                                   'subscribe) out)))))
16786
16787       (setq gnus-newsrc-options-n out))))
16788
16789 (defun gnus-save-newsrc-file (&optional force)
16790   "Save .newsrc file."
16791   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16792   ;; from the variable gnus-newsrc-alist.
16793   (when (and (or gnus-newsrc-alist gnus-killed-list)
16794              gnus-current-startup-file)
16795     (save-excursion
16796       (if (and (or gnus-use-dribble-file gnus-slave)
16797                (not force)
16798                (or (not gnus-dribble-buffer)
16799                    (not (buffer-name gnus-dribble-buffer))
16800                    (zerop (save-excursion
16801                             (set-buffer gnus-dribble-buffer)
16802                             (buffer-size)))))
16803           (gnus-message 4 "(No changes need to be saved)")
16804         (run-hooks 'gnus-save-newsrc-hook)
16805         (if gnus-slave
16806             (gnus-slave-save-newsrc)
16807           ;; Save .newsrc.
16808           (when gnus-save-newsrc-file
16809             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16810             (gnus-gnus-to-newsrc-format)
16811             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16812           ;; Save .newsrc.eld.
16813           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16814           (make-local-variable 'version-control)
16815           (setq version-control 'never)
16816           (setq buffer-file-name
16817                 (concat gnus-current-startup-file ".eld"))
16818           (setq default-directory (file-name-directory buffer-file-name))
16819           (gnus-add-current-to-buffer-list)
16820           (buffer-disable-undo (current-buffer))
16821           (erase-buffer)
16822           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16823           (gnus-gnus-to-quick-newsrc-format)
16824           (run-hooks 'gnus-save-quick-newsrc-hook)
16825           (save-buffer)
16826           (kill-buffer (current-buffer))
16827           (gnus-message
16828            5 "Saving %s.eld...done" gnus-current-startup-file))
16829         (gnus-dribble-delete-file)
16830         (gnus-group-set-mode-line)))))
16831
16832 (defun gnus-gnus-to-quick-newsrc-format ()
16833   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16834   (insert ";; Gnus startup file.\n")
16835   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16836   (insert ";; to read .newsrc.\n")
16837   (insert "(setq gnus-newsrc-file-version "
16838           (prin1-to-string gnus-version) ")\n")
16839   (let ((variables
16840          (if gnus-save-killed-list gnus-variable-list
16841            ;; Remove the `gnus-killed-list' from the list of variables
16842            ;; to be saved, if required.
16843            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16844         ;; Peel off the "dummy" group.
16845         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16846         variable)
16847     ;; Insert the variables into the file.
16848     (while variables
16849       (when (and (boundp (setq variable (pop variables)))
16850                  (symbol-value variable))
16851         (insert "(setq " (symbol-name variable) " '")
16852         (prin1 (symbol-value variable) (current-buffer))
16853         (insert ")\n")))))
16854
16855 (defun gnus-gnus-to-newsrc-format ()
16856   ;; Generate and save the .newsrc file.
16857   (save-excursion
16858     (set-buffer (create-file-buffer gnus-current-startup-file))
16859     (let ((newsrc (cdr gnus-newsrc-alist))
16860           (standard-output (current-buffer))
16861           info ranges range method)
16862       (setq buffer-file-name gnus-current-startup-file)
16863       (setq default-directory (file-name-directory buffer-file-name))
16864       (buffer-disable-undo (current-buffer))
16865       (erase-buffer)
16866       ;; Write options.
16867       (if gnus-newsrc-options (insert gnus-newsrc-options))
16868       ;; Write subscribed and unsubscribed.
16869       (while (setq info (pop newsrc))
16870         ;; Don't write foreign groups to .newsrc.
16871         (when (or (null (setq method (gnus-info-method info)))
16872                   (equal method "native")
16873                   (gnus-server-equal method gnus-select-method))
16874           (insert (gnus-info-group info)
16875                   (if (> (gnus-info-level info) gnus-level-subscribed)
16876                       "!" ":"))
16877           (when (setq ranges (gnus-info-read info))
16878             (insert " ")
16879             (if (not (listp (cdr ranges)))
16880                 (if (= (car ranges) (cdr ranges))
16881                     (princ (car ranges))
16882                   (princ (car ranges))
16883                   (insert "-")
16884                   (princ (cdr ranges)))
16885               (while (setq range (pop ranges))
16886                 (if (or (atom range) (= (car range) (cdr range)))
16887                     (princ (or (and (atom range) range) (car range)))
16888                   (princ (car range))
16889                   (insert "-")
16890                   (princ (cdr range)))
16891                 (if ranges (insert ",")))))
16892           (insert "\n")))
16893       (make-local-variable 'version-control)
16894       (setq version-control 'never)
16895       ;; It has been reported that sometime the modtime on the .newsrc
16896       ;; file seems to be off.  We really do want to overwrite it, so
16897       ;; we clear the modtime here before saving.  It's a bit odd,
16898       ;; though...
16899       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16900       ;; delete the silly thing entirely first.  but this fails to provide
16901       ;; such niceties as .newsrc~ creation.
16902       (if gnus-modtime-botch
16903           (delete-file gnus-startup-file)
16904         (clear-visited-file-modtime))
16905       (run-hooks 'gnus-save-standard-newsrc-hook)
16906       (save-buffer)
16907       (kill-buffer (current-buffer)))))
16908
16909 \f
16910 ;;;
16911 ;;; Slave functions.
16912 ;;;
16913
16914 (defun gnus-slave-save-newsrc ()
16915   (save-excursion
16916     (set-buffer gnus-dribble-buffer)
16917     (let ((slave-name
16918            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16919       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16920
16921 (defun gnus-master-read-slave-newsrc ()
16922   (let ((slave-files
16923          (directory-files
16924           (file-name-directory gnus-current-startup-file)
16925           t (concat
16926              "^" (regexp-quote
16927                   (concat
16928                    (file-name-nondirectory gnus-current-startup-file)
16929                    "-slave-")))
16930           t))
16931         file)
16932     (if (not slave-files)
16933         ()                              ; There are no slave files to read.
16934       (gnus-message 7 "Reading slave newsrcs...")
16935       (save-excursion
16936         (set-buffer (get-buffer-create " *gnus slave*"))
16937         (buffer-disable-undo (current-buffer))
16938         (setq slave-files
16939               (sort (mapcar (lambda (file)
16940                               (list (nth 5 (file-attributes file)) file))
16941                             slave-files)
16942                     (lambda (f1 f2)
16943                       (or (< (caar f1) (caar f2))
16944                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16945         (while slave-files
16946           (erase-buffer)
16947           (setq file (nth 1 (car slave-files)))
16948           (insert-file-contents file)
16949           (if (condition-case ()
16950                   (progn
16951                     (eval-buffer (current-buffer))
16952                     t)
16953                 (error
16954                  (gnus-error 3.2 "Possible error in %s" file)
16955                  nil))
16956               (or gnus-slave ; Slaves shouldn't delete these files.
16957                   (condition-case ()
16958                       (delete-file file)
16959                     (error nil))))
16960           (setq slave-files (cdr slave-files))))
16961       (gnus-message 7 "Reading slave newsrcs...done"))))
16962
16963 \f
16964 ;;;
16965 ;;; Group description.
16966 ;;;
16967
16968 (defun gnus-read-all-descriptions-files ()
16969   (let ((methods (cons gnus-select-method 
16970                        (nconc
16971                         (when gnus-message-archive-method
16972                           (list "archive"))
16973                         gnus-secondary-select-methods))))
16974     (while methods
16975       (gnus-read-descriptions-file (car methods))
16976       (setq methods (cdr methods)))
16977     t))
16978
16979 (defun gnus-read-descriptions-file (&optional method)
16980   (let ((method (or method gnus-select-method))
16981         group)
16982     (when (stringp method)
16983       (setq method (gnus-server-to-method method)))
16984     ;; We create the hashtable whether we manage to read the desc file
16985     ;; to avoid trying to re-read after a failed read.
16986     (or gnus-description-hashtb
16987         (setq gnus-description-hashtb
16988               (gnus-make-hashtable (length gnus-active-hashtb))))
16989     ;; Mark this method's desc file as read.
16990     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16991                   gnus-description-hashtb)
16992
16993     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16994     (cond
16995      ((not (gnus-check-server method))
16996       (gnus-message 1 "Couldn't open server")
16997       nil)
16998      ((not (gnus-request-list-newsgroups method))
16999       (gnus-message 1 "Couldn't read newsgroups descriptions")
17000       nil)
17001      (t
17002       (save-excursion
17003         (save-restriction
17004           (set-buffer nntp-server-buffer)
17005           (goto-char (point-min))
17006           (when (or (search-forward "\n.\n" nil t)
17007                     (goto-char (point-max)))
17008             (beginning-of-line)
17009             (narrow-to-region (point-min) (point)))
17010           ;; If these are groups from a foreign select method, we insert the
17011           ;; group prefix in front of the group names.
17012           (and method (not (gnus-server-equal
17013                             (gnus-server-get-method nil method)
17014                             (gnus-server-get-method nil gnus-select-method)))
17015                (let ((prefix (gnus-group-prefixed-name "" method)))
17016                  (goto-char (point-min))
17017                  (while (and (not (eobp))
17018                              (progn (insert prefix)
17019                                     (zerop (forward-line 1)))))))
17020           (goto-char (point-min))
17021           (while (not (eobp))
17022             ;; If we get an error, we set group to 0, which is not a
17023             ;; symbol...
17024             (setq group
17025                   (condition-case ()
17026                       (let ((obarray gnus-description-hashtb))
17027                         ;; Group is set to a symbol interned in this
17028                         ;; hash table.
17029                         (read nntp-server-buffer))
17030                     (error 0)))
17031             (skip-chars-forward " \t")
17032             ;; ...  which leads to this line being effectively ignored.
17033             (and (symbolp group)
17034                  (set group (buffer-substring
17035                              (point) (progn (end-of-line) (point)))))
17036             (forward-line 1))))
17037       (gnus-message 5 "Reading descriptions file...done")
17038       t))))
17039
17040 (defun gnus-group-get-description (group)
17041   "Get the description of a group by sending XGTITLE to the server."
17042   (when (gnus-request-group-description group)
17043     (save-excursion
17044       (set-buffer nntp-server-buffer)
17045       (goto-char (point-min))
17046       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
17047         (match-string 1)))))
17048
17049 \f
17050 ;;;
17051 ;;; Buffering of read articles.
17052 ;;;
17053
17054 (defvar gnus-backlog-buffer " *Gnus Backlog*")
17055 (defvar gnus-backlog-articles nil)
17056 (defvar gnus-backlog-hashtb nil)
17057
17058 (defun gnus-backlog-buffer ()
17059   "Return the backlog buffer."
17060   (or (get-buffer gnus-backlog-buffer)
17061       (save-excursion
17062         (set-buffer (get-buffer-create gnus-backlog-buffer))
17063         (buffer-disable-undo (current-buffer))
17064         (setq buffer-read-only t)
17065         (gnus-add-current-to-buffer-list)
17066         (get-buffer gnus-backlog-buffer))))
17067
17068 (defun gnus-backlog-setup ()
17069   "Initialize backlog variables."
17070   (unless gnus-backlog-hashtb
17071     (setq gnus-backlog-hashtb (make-vector 1023 0))))
17072
17073 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
17074
17075 (defun gnus-backlog-shutdown ()
17076   "Clear all backlog variables and buffers."
17077   (when (get-buffer gnus-backlog-buffer)
17078     (kill-buffer gnus-backlog-buffer))
17079   (setq gnus-backlog-hashtb nil
17080         gnus-backlog-articles nil))
17081
17082 (defun gnus-backlog-enter-article (group number buffer)
17083   (gnus-backlog-setup)
17084   (let ((ident (intern (concat group ":" (int-to-string number))
17085                        gnus-backlog-hashtb))
17086         b)
17087     (if (memq ident gnus-backlog-articles)
17088         () ; It's already kept.
17089       ;; Remove the oldest article, if necessary.
17090       (and (numberp gnus-keep-backlog)
17091            (>= (length gnus-backlog-articles) gnus-keep-backlog)
17092            (gnus-backlog-remove-oldest-article))
17093       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
17094       ;; Insert the new article.
17095       (save-excursion
17096         (set-buffer (gnus-backlog-buffer))
17097         (let (buffer-read-only)
17098           (goto-char (point-max))
17099           (or (bolp) (insert "\n"))
17100           (setq b (point))
17101           (insert-buffer-substring buffer)
17102           ;; Tag the beginning of the article with the ident.
17103           (gnus-put-text-property b (1+ b) 'gnus-backlog ident))))))
17104
17105 (defun gnus-backlog-remove-oldest-article ()
17106   (save-excursion
17107     (set-buffer (gnus-backlog-buffer))
17108     (goto-char (point-min))
17109     (if (zerop (buffer-size))
17110         () ; The buffer is empty.
17111       (let ((ident (get-text-property (point) 'gnus-backlog))
17112             buffer-read-only)
17113         ;; Remove the ident from the list of articles.
17114         (when ident
17115           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
17116         ;; Delete the article itself.
17117         (delete-region
17118          (point) (next-single-property-change
17119                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
17120
17121 (defun gnus-backlog-remove-article (group number)
17122   "Remove article NUMBER in GROUP from the backlog."
17123   (when (numberp number)
17124     (gnus-backlog-setup)
17125     (let ((ident (intern (concat group ":" (int-to-string number))
17126                          gnus-backlog-hashtb))
17127           beg end)
17128       (when (memq ident gnus-backlog-articles)
17129         ;; It was in the backlog.
17130         (save-excursion
17131           (set-buffer (gnus-backlog-buffer))
17132           (let (buffer-read-only)
17133             (when (setq beg (text-property-any
17134                              (point-min) (point-max) 'gnus-backlog
17135                              ident))
17136               ;; Find the end (i. e., the beginning of the next article).
17137               (setq end
17138                     (next-single-property-change
17139                      (1+ beg) 'gnus-backlog (current-buffer) (point-max)))
17140               (delete-region beg end)
17141               ;; Return success.
17142               t)))))))
17143
17144 (defun gnus-backlog-request-article (group number buffer)
17145   (when (numberp number)
17146     (gnus-backlog-setup)
17147     (let ((ident (intern (concat group ":" (int-to-string number))
17148                          gnus-backlog-hashtb))
17149           beg end)
17150       (when (memq ident gnus-backlog-articles)
17151         ;; It was in the backlog.
17152         (save-excursion
17153           (set-buffer (gnus-backlog-buffer))
17154           (if (not (setq beg (text-property-any
17155                               (point-min) (point-max) 'gnus-backlog
17156                               ident)))
17157               ;; It wasn't in the backlog after all.
17158               (ignore
17159                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
17160             ;; Find the end (i. e., the beginning of the next article).
17161             (setq end
17162                   (next-single-property-change
17163                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
17164         (let ((buffer-read-only nil))
17165           (erase-buffer)
17166           (insert-buffer-substring gnus-backlog-buffer beg end)
17167           t)))))
17168
17169 ;; Allow redefinition of Gnus functions.
17170
17171 (gnus-ems-redefine)
17172
17173 (provide 'gnus)
17174
17175 ;;; gnus.el ends here