*** empty log message ***
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996,97 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'gnus)
29 (require 'gnus-group)
30 (require 'gnus-spec)
31 (require 'gnus-range)
32 (require 'gnus-int)
33 (require 'gnus-undo)
34
35 (defcustom gnus-kill-summary-on-exit t
36   "*If non-nil, kill the summary buffer when you exit from it.
37 If nil, the summary will become a \"*Dead Summary*\" buffer, and
38 it will be killed sometime later."
39   :group 'gnus-summary-exit
40   :type 'boolean)
41
42 (defcustom gnus-fetch-old-headers nil
43   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
44 If an unread article in the group refers to an older, already read (or
45 just marked as read) article, the old article will not normally be
46 displayed in the Summary buffer.  If this variable is non-nil, Gnus
47 will attempt to grab the headers to the old articles, and thereby
48 build complete threads.  If it has the value `some', only enough
49 headers to connect otherwise loose threads will be displayed.
50 This variable can also be a number.  In that case, no more than that
51 number of old headers will be fetched.
52
53 The server has to support NOV for any of this to work."
54   :group 'gnus-thread
55   :type '(choice (const :tag "off" nil)
56                  (const some)
57                  number
58                  (sexp :menu-tag "other" t)))
59
60 (defcustom gnus-summary-make-false-root 'adopt
61   "*nil means that Gnus won't gather loose threads.
62 If the root of a thread has expired or been read in a previous
63 session, the information necessary to build a complete thread has been
64 lost.  Instead of having many small sub-threads from this original thread
65 scattered all over the summary buffer, Gnus can gather them.
66
67 If non-nil, Gnus will try to gather all loose sub-threads from an
68 original thread into one large thread.
69
70 If this variable is non-nil, it should be one of `none', `adopt',
71 `dummy' or `empty'.
72
73 If this variable is `none', Gnus will not make a false root, but just
74 present the sub-threads after another.
75 If this variable is `dummy', Gnus will create a dummy root that will
76 have all the sub-threads as children.
77 If this variable is `adopt', Gnus will make one of the \"children\"
78 the parent and mark all the step-children as such.
79 If this variable is `empty', the \"children\" are printed with empty
80 subject fields.  (Or rather, they will be printed with a string
81 given by the `gnus-summary-same-subject' variable.)"
82   :group 'gnus-thread
83   :type '(choice (const :tag "off" nil)
84                  (const none)
85                  (const dummy)
86                  (const adopt)
87                  (const empty)))
88
89 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
90   "*A regexp to match subjects to be excluded from loose thread gathering.
91 As loose thread gathering is done on subjects only, that means that
92 there can be many false gatherings performed.  By rooting out certain
93 common subjects, gathering might become saner."
94   :group 'gnus-thread
95   :type 'regexp)
96
97 (defcustom gnus-summary-gather-subject-limit nil
98   "*Maximum length of subject comparisons when gathering loose threads.
99 Use nil to compare full subjects.  Setting this variable to a low
100 number will help gather threads that have been corrupted by
101 newsreaders chopping off subject lines, but it might also mean that
102 unrelated articles that have subject that happen to begin with the
103 same few characters will be incorrectly gathered.
104
105 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
106 comparing subjects."
107   :group 'gnus-thread
108   :type '(choice (const :tag "off" nil)
109                  (const fuzzy)
110                  (sexp :menu-tag "on" t)))
111
112 (defcustom gnus-simplify-ignored-prefixes nil
113   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
114   :group 'gnus-thread
115   :type '(choice (const :tag "off" nil)
116                  regexp))
117
118 (defcustom gnus-build-sparse-threads nil
119   "*If non-nil, fill in the gaps in threads.
120 If `some', only fill in the gaps that are needed to tie loose threads
121 together.  If `more', fill in all leaf nodes that Gnus can find.  If
122 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
123   :group 'gnus-thread
124   :type '(choice (const :tag "off" nil)
125                  (const some)
126                  (const more)
127                  (sexp :menu-tag "all" t)))
128
129 (defcustom gnus-summary-thread-gathering-function 'gnus-gather-threads-by-subject
130   "Function used for gathering loose threads.
131 There are two pre-defined functions: `gnus-gather-threads-by-subject',
132 which only takes Subjects into consideration; and
133 `gnus-gather-threads-by-references', which compared the References
134 headers of the articles to find matches."
135   :group 'gnus-thread
136   :type '(set (function-item gnus-gather-threads-by-subject)
137               (function-item gnus-gather-threads-by-references)
138               (function :tag "other")))
139
140 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
141 (defcustom gnus-summary-same-subject ""
142   "*String indicating that the current article has the same subject as the previous.
143 This variable will only be used if the value of
144 `gnus-summary-make-false-root' is `empty'."
145   :group 'gnus-summary-format
146   :type 'string)
147
148 (defcustom gnus-summary-goto-unread t
149   "*If t, marking commands will go to the next unread article.
150 If `never', commands that usually go to the next unread article, will
151 go to the next article, whether it is read or not.
152 If nil, only the marking commands will go to the next (un)read article."
153   :group 'gnus-summary-marks
154   :link '(custom-manual "(gnus)Setting Marks")
155   :type '(choice (const :tag "off" nil)
156                  (const never)
157                  (sexp :menu-tag "on" t)))
158
159 (defcustom gnus-summary-default-score 0
160   "*Default article score level.
161 If this variable is nil, scoring will be disabled."
162   :group 'gnus-score-default
163   :type '(choice (const :tag "disable")
164                  integer))
165
166 (defcustom gnus-summary-zcore-fuzz 0
167   "*Fuzziness factor for the zcore in the summary buffer.
168 Articles with scores closer than this to `gnus-summary-default-score'
169 will not be marked."
170   :group 'gnus-summary-format
171   :type 'integer)
172
173 (defcustom gnus-simplify-subject-fuzzy-regexp nil
174   "*Strings to be removed when doing fuzzy matches.
175 This can either be a regular expression or list of regular expressions
176 that will be removed from subject strings if fuzzy subject
177 simplification is selected."
178   :group 'gnus-thread
179   :type '(repeat regexp))
180
181 (defcustom gnus-show-threads t
182   "*If non-nil, display threads in summary mode."
183   :group 'gnus-thread
184   :type 'boolean)
185
186 (defcustom gnus-thread-hide-subtree nil
187   "*If non-nil, hide all threads initially.
188 If threads are hidden, you have to run the command
189 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
190 to expose hidden threads."
191   :group 'gnus-thread
192   :type 'boolean)
193
194 (defcustom gnus-thread-hide-killed t
195   "*If non-nil, hide killed threads automatically."
196   :group 'gnus-thread
197   :type 'boolean)
198
199 (defcustom gnus-thread-ignore-subject nil
200   "*If non-nil, ignore subjects and do all threading based on the Reference header.
201 If nil, which is the default, articles that have different subjects
202 from their parents will start separate threads."
203   :group 'gnus-thread
204   :type 'boolean)
205
206 (defcustom gnus-thread-operation-ignore-subject t
207   "*If non-nil, subjects will be ignored when doing thread commands.
208 This affects commands like `gnus-summary-kill-thread' and
209 `gnus-summary-lower-thread'.
210
211 If this variable is nil, articles in the same thread with different
212 subjects will not be included in the operation in question.  If this
213 variable is `fuzzy', only articles that have subjects that are fuzzily
214 equal will be included."
215   :group 'gnus-thread
216   :type '(choice (const :tag "off" nil)
217                  (const fuzzy)
218                  (sexp :tag "on" t)))
219
220 (defcustom gnus-thread-indent-level 4
221   "*Number that says how much each sub-thread should be indented."
222   :group 'gnus-thread
223   :type 'integer)
224
225 (defcustom gnus-auto-extend-newsgroup t
226   "*If non-nil, extend newsgroup forward and backward when requested."
227   :group 'gnus-summary-choose
228   :type 'boolean)
229
230 (defcustom gnus-auto-select-first t
231   "*If nil, don't select the first unread article when entering a group.
232 If this variable is `best', select the highest-scored unread article
233 in the group.  If neither nil nor `best', select the first unread
234 article.
235
236 If you want to prevent automatic selection of the first unread article
237 in some newsgroups, set the variable to nil in
238 `gnus-select-group-hook'."
239   :group 'gnus-group-select
240   :type '(choice (const :tag "none" nil)
241                  (const best)
242                  (sexp :menu-tag "first" t)))
243
244 (defcustom gnus-auto-select-next t
245   "*If non-nil, offer to go to the next group from the end of the previous.
246 If the value is t and the next newsgroup is empty, Gnus will exit
247 summary mode and go back to group mode.  If the value is neither nil
248 nor t, Gnus will select the following unread newsgroup.  In
249 particular, if the value is the symbol `quietly', the next unread
250 newsgroup will be selected without any confirmation, and if it is
251 `almost-quietly', the next group will be selected without any
252 confirmation if you are located on the last article in the group.
253 Finally, if this variable is `slightly-quietly', the `Z n' command
254 will go to the next group without confirmation."
255   :group 'gnus-summary-maneuvering
256   :type '(choice (const :tag "off" nil)
257                  (const quietly)
258                  (const almost-quietly)
259                  (const slightly-quietly)
260                  (sexp :menu-tag "on" t)))
261
262 (defcustom gnus-auto-select-same nil
263   "*If non-nil, select the next article with the same subject."
264   :group 'gnus-summary-maneuvering
265   :type 'boolean)
266
267 (defcustom gnus-summary-check-current nil
268   "*If non-nil, consider the current article when moving.
269 The \"unread\" movement commands will stay on the same line if the
270 current article is unread."
271   :group 'gnus-summary-maneuvering
272   :type 'boolean)
273
274 (defcustom gnus-auto-center-summary t
275   "*If non-nil, always center the current summary buffer.
276 In particular, if `vertical' do only vertical recentering.  If non-nil
277 and non-`vertical', do both horizontal and vertical recentering."
278   :group 'gnus-summary-maneuvering
279   :type '(choice (const :tag "none" nil)
280                  (const vertical)
281                  (sexp :menu-tag "both" t)))
282
283 (defcustom gnus-show-all-headers nil
284   "*If non-nil, don't hide any headers."
285   :group 'gnus-article-hiding
286   :group 'gnus-article-headers
287   :type 'boolean)
288
289 (defcustom gnus-single-article-buffer t
290   "*If non-nil, display all articles in the same buffer.
291 If nil, each group will get its own article buffer."
292   :group 'gnus-article-various
293   :type 'boolean)
294
295 (defcustom gnus-break-pages t
296   "*If non-nil, do page breaking on articles.
297 The page delimiter is specified by the `gnus-page-delimiter'
298 variable."
299   :group 'gnus-article-various
300   :type 'boolean)
301
302 (defcustom gnus-show-mime nil
303   "*If non-nil, do mime processing of articles.
304 The articles will simply be fed to the function given by
305 `gnus-show-mime-method'."
306   :group 'gnus-article-mime
307   :type 'boolean)
308
309 (defcustom gnus-move-split-methods nil
310   "*Variable used to suggest where articles are to be moved to.
311 It uses the same syntax as the `gnus-split-methods' variable."
312   :group 'gnus-summary-mail
313   :type '(repeat (choice (list function)
314                          (cons regexp (repeat string))
315                          sexp)))
316
317 ;; Mark variables suggested by Thomas Michanek
318 ;; <Thomas.Michanek@telelogic.se>.
319
320 (defcustom gnus-unread-mark ? 
321   "*Mark used for unread articles."
322   :group 'gnus-summary-marks
323   :type 'character)
324
325 (defcustom gnus-ticked-mark ?!
326   "*Mark used for ticked articles."
327   :group 'gnus-summary-marks
328   :type 'character)
329
330 (defcustom gnus-dormant-mark ??
331   "*Mark used for dormant articles."
332   :group 'gnus-summary-marks
333   :type 'character)
334
335 (defcustom gnus-del-mark ?r
336   "*Mark used for del'd articles."
337   :group 'gnus-summary-marks
338   :type 'character)
339
340 (defcustom gnus-read-mark ?R
341   "*Mark used for read articles."
342   :group 'gnus-summary-marks
343   :type 'character)
344
345 (defcustom gnus-expirable-mark ?E
346   "*Mark used for expirable articles."
347   :group 'gnus-summary-marks
348   :type 'character)
349
350 (defcustom gnus-killed-mark ?K
351   "*Mark used for killed articles."
352   :group 'gnus-summary-marks
353   :type 'character)
354
355 (defcustom gnus-souped-mark ?F
356   "*Mark used for killed articles."
357   :group 'gnus-summary-marks
358   :type 'character)
359
360 (defcustom gnus-kill-file-mark ?X
361   "*Mark used for articles killed by kill files."
362   :group 'gnus-summary-marks
363   :type 'character)
364
365 (defcustom gnus-low-score-mark ?Y
366   "*Mark used for articles with a low score."
367   :group 'gnus-summary-marks
368   :type 'character)
369
370 (defcustom gnus-catchup-mark ?C
371   "*Mark used for articles that are caught up."
372   :group 'gnus-summary-marks
373   :type 'character)
374
375 (defcustom gnus-replied-mark ?A
376   "*Mark used for articles that have been replied to."
377   :group 'gnus-summary-marks
378   :type 'character)
379
380 (defcustom gnus-cached-mark ?*
381   "*Mark used for articles that are in the cache."
382   :group 'gnus-summary-marks
383   :type 'character)
384
385 (defcustom gnus-saved-mark ?S
386   "*Mark used for articles that have been saved to."
387   :group 'gnus-summary-marks
388   :type 'character)
389
390 (defcustom gnus-ancient-mark ?O
391   "*Mark used for ancient articles."
392   :group 'gnus-summary-marks
393   :type 'character)
394
395 (defcustom gnus-sparse-mark ?Q
396   "*Mark used for sparsely reffed articles."
397   :group 'gnus-summary-marks
398   :type 'character)
399
400 (defcustom gnus-canceled-mark ?G
401   "*Mark used for canceled articles."
402   :group 'gnus-summary-marks
403   :type 'character)
404
405 (defcustom gnus-duplicate-mark ?M
406   "*Mark used for duplicate articles."
407   :group 'gnus-summary-marks
408   :type 'character)
409
410 (defcustom gnus-score-over-mark ?+
411   "*Score mark used for articles with high scores."
412   :group 'gnus-summary-marks
413   :type 'character)
414
415 (defcustom gnus-score-below-mark ?-
416   "*Score mark used for articles with low scores."
417   :group 'gnus-summary-marks
418   :type 'character)
419
420 (defcustom gnus-empty-thread-mark ? 
421   "*There is no thread under the article."
422   :group 'gnus-summary-marks
423   :type 'character)
424
425 (defcustom gnus-not-empty-thread-mark ?=
426   "*There is a thread under the article."
427   :group 'gnus-summary-marks
428   :type 'character)
429
430 (defcustom gnus-view-pseudo-asynchronously nil
431   "*If non-nil, Gnus will view pseudo-articles asynchronously."
432   :group 'gnus-extract-view
433   :type 'boolean)
434
435 (defcustom gnus-view-pseudos nil
436   "*If `automatic', pseudo-articles will be viewed automatically.
437 If `not-confirm', pseudos will be viewed automatically, and the user
438 will not be asked to confirm the command."
439   :group 'gnus-extract-view
440   :type '(choice (const :tag "off" nil)
441                  (const automatic)
442                  (const not-confirm)))
443
444 (defcustom gnus-view-pseudos-separately t
445   "*If non-nil, one pseudo-article will be created for each file to be viewed.
446 If nil, all files that use the same viewing command will be given as a
447 list of parameters to that command."
448   :group 'gnus-extract-view
449   :type 'boolean)
450
451 (defcustom gnus-insert-pseudo-articles t
452   "*If non-nil, insert pseudo-articles when decoding articles."
453   :group 'gnus-extract-view
454   :type 'boolean)
455
456 (defcustom gnus-summary-dummy-line-format
457   "*  %(:                          :%) %S\n"
458   "*The format specification for the dummy roots in the summary buffer.
459 It works along the same lines as a normal formatting string,
460 with some simple extensions.
461
462 %S  The subject"
463   :group 'gnus-threading
464   :type 'string)
465
466 (defcustom gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
467   "*The format specification for the summary mode line.
468 It works along the same lines as a normal formatting string,
469 with some simple extensions:
470
471 %G  Group name
472 %p  Unprefixed group name
473 %A  Current article number
474 %V  Gnus version
475 %U  Number of unread articles in the group
476 %e  Number of unselected articles in the group
477 %Z  A string with unread/unselected article counts
478 %g  Shortish group name
479 %S  Subject of the current article
480 %u  User-defined spec
481 %s  Current score file name
482 %d  Number of dormant articles
483 %r  Number of articles that have been marked as read in this session
484 %E  Number of articles expunged by the score files"
485   :group 'gnus-summary-format
486   :type 'string)
487
488 (defcustom gnus-summary-mark-below 0
489   "*Mark all articles with a score below this variable as read.
490 This variable is local to each summary buffer and usually set by the
491 score file."
492   :group 'gnus-score-default
493   :type 'integer)
494
495 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
496   "*List of functions used for sorting articles in the summary buffer.
497 This variable is only used when not using a threaded display."
498   :group 'gnus-summary-sort
499   :type '(repeat (choice (function-item gnus-article-sort-by-number)
500                          (function-item gnus-article-sort-by-author)
501                          (function-item gnus-article-sort-by-subject)
502                          (function-item gnus-article-sort-by-date)
503                          (function-item gnus-article-sort-by-score)
504                          (function :tag "other"))))
505
506 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
507   "*List of functions used for sorting threads in the summary buffer.
508 By default, threads are sorted by article number.
509
510 Each function takes two threads and return non-nil if the first thread
511 should be sorted before the other.  If you use more than one function,
512 the primary sort function should be the last.  You should probably
513 always include `gnus-thread-sort-by-number' in the list of sorting
514 functions -- preferably first.
515
516 Ready-made functions include `gnus-thread-sort-by-number',
517 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
518 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
519 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
520   :group 'gnus-summary-sort
521   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
522                          (function-item gnus-thread-sort-by-author)
523                          (function-item gnus-thread-sort-by-subject)
524                          (function-item gnus-thread-sort-by-date)
525                          (function-item gnus-thread-sort-by-score)
526                          (function-item gnus-thread-sort-by-total-score)
527                          (function :tag "other"))))
528
529 (defcustom gnus-thread-score-function '+
530   "*Function used for calculating the total score of a thread.
531
532 The function is called with the scores of the article and each
533 subthread and should then return the score of the thread.
534
535 Some functions you can use are `+', `max', or `min'."
536   :group 'gnus-summary-sort
537   :type 'function)
538
539 (defcustom gnus-summary-expunge-below nil
540   "All articles that have a score less than this variable will be expunged."
541   :group 'gnus-score-default
542   :type '(choice (const :tag "off" nil)
543                  integer))
544
545 (defcustom gnus-thread-expunge-below nil
546   "All threads that have a total score less than this variable will be expunged.
547 See `gnus-thread-score-function' for en explanation of what a
548 \"thread score\" is."
549   :group 'gnus-treading
550   :group 'gnus-score-default
551   :type '(choice (const :tag "off" nil)
552                  integer))
553
554 (defcustom gnus-summary-mode-hook nil
555   "*A hook for Gnus summary mode.
556 This hook is run before any variables are set in the summary buffer."
557   :group 'gnus-summary-various
558   :type 'hook)
559
560 (defcustom gnus-summary-menu-hook nil
561   "*Hook run after the creation of the summary mode menu."
562   :group 'gnus-summary-visual
563   :type 'hook)
564
565 (defcustom gnus-summary-exit-hook nil
566   "*A hook called on exit from the summary buffer.
567 It will be called with point in the group buffer."
568   :group 'gnus-summary-exit
569   :type 'hook)
570
571 (defcustom gnus-summary-prepare-hook nil
572   "*A hook called after the summary buffer has been generated.
573 If you want to modify the summary buffer, you can use this hook."
574   :group 'gnus-summary-various
575   :type 'hook)
576
577 (defcustom gnus-summary-generate-hook nil
578   "*A hook run just before generating the summary buffer.
579 This hook is commonly used to customize threading variables and the
580 like."
581   :group 'gnus-summary-various
582   :type 'hook)
583
584 (defcustom gnus-select-group-hook nil
585   "*A hook called when a newsgroup is selected.
586
587 If you'd like to simplify subjects like the
588 `gnus-summary-next-same-subject' command does, you can use the
589 following hook:
590
591  (setq gnus-select-group-hook
592       (list
593         (lambda ()
594           (mapcar (lambda (header)
595                      (mail-header-set-subject
596                       header
597                       (gnus-simplify-subject
598                        (mail-header-subject header) 're-only)))
599                   gnus-newsgroup-headers))))"
600   :group 'gnus-group-select
601   :type 'hook)
602
603 (defcustom gnus-select-article-hook nil
604   "*A hook called when an article is selected."
605   :group 'gnus-summary-choose
606   :type 'hook)
607
608 (defcustom gnus-visual-mark-article-hook
609   (list 'gnus-highlight-selected-summary)
610   "*Hook run after selecting an article in the summary buffer.
611 It is meant to be used for highlighting the article in some way.  It
612 is not run if `gnus-visual' is nil."
613   :group 'gnus-summary-visual
614   :type 'hook)
615
616 (defcustom gnus-parse-headers-hook 
617   (list 'gnus-decode-rfc1522)
618   "*A hook called before parsing the headers."
619   :group 'gnus-various
620   :type 'hook)
621
622 (defcustom gnus-exit-group-hook nil
623   "*A hook called when exiting (not quitting) summary mode."
624   :group 'gnus-various
625   :type 'hook)
626
627 (defcustom gnus-summary-update-hook
628   (list 'gnus-summary-highlight-line)
629   "*A hook called when a summary line is changed.
630 The hook will not be called if `gnus-visual' is nil.
631
632 The default function `gnus-summary-highlight-line' will
633 highlight the line according to the `gnus-summary-highlight'
634 variable."
635   :group 'gnus-summary-visual
636   :type 'hook)
637
638 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
639   "*A hook called when an article is selected for the first time.
640 The hook is intended to mark an article as read (or unread)
641 automatically when it is selected."
642   :group 'gnus-summary-choose
643   :type 'hook)
644
645 (defcustom gnus-group-no-more-groups-hook nil
646   "*A hook run when returning to group mode having no more (unread) groups."
647   :group 'gnus-group-select
648   :type 'hook)
649
650 (defcustom gnus-ps-print-hook nil
651   "*A hook run before ps-printing something from Gnus."
652   :group 'gnus-summary
653   :type 'hook)
654
655 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
656   "Face used for highlighting the current article in the summary buffer."
657   :group 'gnus-summary-visual
658   :type 'face)
659
660 (defcustom gnus-summary-highlight 
661   '(((= mark gnus-canceled-mark)
662      . gnus-summary-cancelled-face)
663     ((and (> score default)
664           (or (= mark gnus-dormant-mark)
665               (= mark gnus-ticked-mark)))
666      . gnus-summary-high-ticked-face)
667     ((and (< score default)
668           (or (= mark gnus-dormant-mark)
669               (= mark gnus-ticked-mark)))
670      . gnus-summary-low-ticked-face)
671     ((or (= mark gnus-dormant-mark)
672          (= mark gnus-ticked-mark))
673      . gnus-summary-normal-ticked-face)
674     ((and (> score default) (= mark gnus-ancient-mark))
675      . gnus-summary-high-ancient-face)
676     ((and (< score default) (= mark gnus-ancient-mark))
677      . gnus-summary-low-ancient-face)
678     ((= mark gnus-ancient-mark)
679      . gnus-summary-normal-ancient-face)
680     ((and (> score default) (= mark gnus-unread-mark))
681      . gnus-summary-high-unread-face)
682     ((and (< score default) (= mark gnus-unread-mark))
683      . gnus-summary-low-unread-face)
684     ((and (= mark gnus-unread-mark))
685      . gnus-summary-normal-unread-face)
686     ((> score default) 
687      . gnus-summary-high-read-face)
688     ((< score default) 
689      . gnus-summary-low-read-face)
690     (t 
691      . gnus-summary-normal-read-face))
692   "Controls the highlighting of summary buffer lines. 
693
694 A list of (FORM . FACE) pairs.  When deciding how a a particular
695 summary line should be displayed, each form is evaluated.  The content
696 of the face field after the first true form is used.  You can change
697 how those summary lines are displayed, by editing the face field.
698
699 You can use the following variables in the FORM field.
700
701 score:   The articles score
702 default: The default article score.
703 below:   The score below which articles are automatically marked as read. 
704 mark:    The articles mark."
705   :group 'gnus-summary-visual
706   :type '(repeat (cons (sexp :tag "Form" nil)
707                        face)))
708
709
710 ;;; Internal variables
711
712 (defvar gnus-scores-exclude-files nil)
713
714 (defvar gnus-summary-display-table 
715   ;; Change the display table.  Odd characters have a tendency to mess
716   ;; up nicely formatted displays - we make all possible glyphs
717   ;; display only a single character.
718
719   ;; We start from the standard display table, if any.
720   (let ((table (or (copy-sequence standard-display-table)
721                    (make-display-table)))
722         ;; Nix out all the control chars...
723         (i 32))
724     (while (>= (setq i (1- i)) 0)
725       (aset table i [??]))
726     ;; ... but not newline and cr, of course.  (cr is necessary for the
727     ;; selective display).
728     (aset table ?\n nil)
729     (aset table ?\r nil)
730     ;; We nix out any glyphs over 126 that are not set already.
731     (let ((i 256))
732       (while (>= (setq i (1- i)) 127)
733         ;; Only modify if the entry is nil.
734         (or (aref table i)
735             (aset table i [??]))))
736     table)
737   "Display table used in summary mode buffers.")
738
739 (defvar gnus-original-article nil)
740 (defvar gnus-article-internal-prepare-hook nil)
741 (defvar gnus-newsgroup-process-stack nil)
742
743 (defvar gnus-thread-indent-array nil)
744 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
745
746 ;; Avoid highlighting in kill files.
747 (defvar gnus-summary-inhibit-highlight nil)
748 (defvar gnus-newsgroup-selected-overlay nil)
749 (defvar gnus-inhibit-limiting nil)
750 (defvar gnus-newsgroup-adaptive-score-file nil)
751 (defvar gnus-current-score-file nil)
752 (defvar gnus-current-move-group nil)
753 (defvar gnus-current-copy-group nil)
754 (defvar gnus-current-crosspost-group nil)
755
756 (defvar gnus-newsgroup-dependencies nil)
757 (defvar gnus-newsgroup-adaptive nil)
758 (defvar gnus-summary-display-article-function nil)
759 (defvar gnus-summary-highlight-line-function nil
760   "Function called after highlighting a summary line.")
761
762 (defvar gnus-summary-line-format-alist
763   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
764     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
765     (?s gnus-tmp-subject-or-nil ?s)
766     (?n gnus-tmp-name ?s)
767     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
768         ?s)
769     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
770             gnus-tmp-from) ?s)
771     (?F gnus-tmp-from ?s)
772     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
773     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
774     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
775     (?o (gnus-date-iso8601 gnus-tmp-header) ?s)
776     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
777     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
778     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
779     (?L gnus-tmp-lines ?d)
780     (?I gnus-tmp-indentation ?s)
781     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
782     (?R gnus-tmp-replied ?c)
783     (?\[ gnus-tmp-opening-bracket ?c)
784     (?\] gnus-tmp-closing-bracket ?c)
785     (?\> (make-string gnus-tmp-level ? ) ?s)
786     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
787     (?i gnus-tmp-score ?d)
788     (?z gnus-tmp-score-char ?c)
789     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
790     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
791     (?U gnus-tmp-unread ?c)
792     (?t (gnus-summary-number-of-articles-in-thread
793          (and (boundp 'thread) (car thread)) gnus-tmp-level)
794         ?d)
795     (?e (gnus-summary-number-of-articles-in-thread
796          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
797         ?c)
798     (?u gnus-tmp-user-defined ?s)
799     (?P (gnus-pick-line-number) ?d))
800   "An alist of format specifications that can appear in summary lines,
801 and what variables they correspond with, along with the type of the
802 variable (string, integer, character, etc).")
803
804 (defvar gnus-summary-dummy-line-format-alist
805   `((?S gnus-tmp-subject ?s)
806     (?N gnus-tmp-number ?d)
807     (?u gnus-tmp-user-defined ?s)))
808
809 (defvar gnus-summary-mode-line-format-alist
810   `((?G gnus-tmp-group-name ?s)
811     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
812     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
813     (?A gnus-tmp-article-number ?d)
814     (?Z gnus-tmp-unread-and-unselected ?s)
815     (?V gnus-version ?s)
816     (?U gnus-tmp-unread-and-unticked ?d)
817     (?S gnus-tmp-subject ?s)
818     (?e gnus-tmp-unselected ?d)
819     (?u gnus-tmp-user-defined ?s)
820     (?d (length gnus-newsgroup-dormant) ?d)
821     (?t (length gnus-newsgroup-marked) ?d)
822     (?r (length gnus-newsgroup-reads) ?d)
823     (?E gnus-newsgroup-expunged-tally ?d)
824     (?s (gnus-current-score-file-nondirectory) ?s)))
825
826 (defvar gnus-last-search-regexp nil
827   "Default regexp for article search command.")
828
829 (defvar gnus-last-shell-command nil
830   "Default shell command on article.")
831
832 (defvar gnus-newsgroup-begin nil)
833 (defvar gnus-newsgroup-end nil)
834 (defvar gnus-newsgroup-last-rmail nil)
835 (defvar gnus-newsgroup-last-mail nil)
836 (defvar gnus-newsgroup-last-folder nil)
837 (defvar gnus-newsgroup-last-file nil)
838 (defvar gnus-newsgroup-auto-expire nil)
839 (defvar gnus-newsgroup-active nil)
840
841 (defvar gnus-newsgroup-data nil)
842 (defvar gnus-newsgroup-data-reverse nil)
843 (defvar gnus-newsgroup-limit nil)
844 (defvar gnus-newsgroup-limits nil)
845
846 (defvar gnus-newsgroup-unreads nil
847   "List of unread articles in the current newsgroup.")
848
849 (defvar gnus-newsgroup-unselected nil
850   "List of unselected unread articles in the current newsgroup.")
851
852 (defvar gnus-newsgroup-reads nil
853   "Alist of read articles and article marks in the current newsgroup.")
854
855 (defvar gnus-newsgroup-expunged-tally nil)
856
857 (defvar gnus-newsgroup-marked nil
858   "List of ticked articles in the current newsgroup (a subset of unread art).")
859
860 (defvar gnus-newsgroup-killed nil
861   "List of ranges of articles that have been through the scoring process.")
862
863 (defvar gnus-newsgroup-cached nil
864   "List of articles that come from the article cache.")
865
866 (defvar gnus-newsgroup-saved nil
867   "List of articles that have been saved.")
868
869 (defvar gnus-newsgroup-kill-headers nil)
870
871 (defvar gnus-newsgroup-replied nil
872   "List of articles that have been replied to in the current newsgroup.")
873
874 (defvar gnus-newsgroup-expirable nil
875   "List of articles in the current newsgroup that can be expired.")
876
877 (defvar gnus-newsgroup-processable nil
878   "List of articles in the current newsgroup that can be processed.")
879
880 (defvar gnus-newsgroup-bookmarks nil
881   "List of articles in the current newsgroup that have bookmarks.")
882
883 (defvar gnus-newsgroup-dormant nil
884   "List of dormant articles in the current newsgroup.")
885
886 (defvar gnus-newsgroup-scored nil
887   "List of scored articles in the current newsgroup.")
888
889 (defvar gnus-newsgroup-headers nil
890   "List of article headers in the current newsgroup.")
891
892 (defvar gnus-newsgroup-threads nil)
893
894 (defvar gnus-newsgroup-prepared nil
895   "Whether the current group has been prepared properly.")
896
897 (defvar gnus-newsgroup-ancient nil
898   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
899
900 (defvar gnus-newsgroup-sparse nil)
901
902 (defvar gnus-current-article nil)
903 (defvar gnus-article-current nil)
904 (defvar gnus-current-headers nil)
905 (defvar gnus-have-all-headers nil)
906 (defvar gnus-last-article nil)
907 (defvar gnus-newsgroup-history nil)
908
909 (defconst gnus-summary-local-variables
910   '(gnus-newsgroup-name
911     gnus-newsgroup-begin gnus-newsgroup-end
912     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
913     gnus-newsgroup-last-folder gnus-newsgroup-last-file
914     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
915     gnus-newsgroup-unselected gnus-newsgroup-marked
916     gnus-newsgroup-reads gnus-newsgroup-saved
917     gnus-newsgroup-replied gnus-newsgroup-expirable
918     gnus-newsgroup-processable gnus-newsgroup-killed
919     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
920     gnus-newsgroup-headers gnus-newsgroup-threads
921     gnus-newsgroup-prepared gnus-summary-highlight-line-function
922     gnus-current-article gnus-current-headers gnus-have-all-headers
923     gnus-last-article gnus-article-internal-prepare-hook
924     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
925     gnus-newsgroup-scored gnus-newsgroup-kill-headers
926     gnus-thread-expunge-below
927     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
928     (gnus-summary-mark-below . global)
929     gnus-newsgroup-active gnus-scores-exclude-files
930     gnus-newsgroup-history gnus-newsgroup-ancient
931     gnus-newsgroup-sparse gnus-newsgroup-process-stack
932     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
933     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
934     (gnus-newsgroup-expunged-tally . 0)
935     gnus-cache-removable-articles gnus-newsgroup-cached
936     gnus-newsgroup-data gnus-newsgroup-data-reverse
937     gnus-newsgroup-limit gnus-newsgroup-limits)
938   "Variables that are buffer-local to the summary buffers.")
939
940 ;; Byte-compiler warning.
941 (defvar gnus-article-mode-map)
942
943 ;; Subject simplification.
944
945 (defsubst gnus-simplify-subject-re (subject)
946   "Remove \"Re:\" from subject lines."
947   (if (string-match "^[Rr][Ee]: *" subject)
948       (substring subject (match-end 0))
949     subject))
950
951 (defun gnus-simplify-subject (subject &optional re-only)
952   "Remove `Re:' and words in parentheses.
953 If RE-ONLY is non-nil, strip leading `Re:'s only."
954   (let ((case-fold-search t))           ;Ignore case.
955     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
956     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
957       (setq subject (substring subject (match-end 0))))
958     ;; Remove uninteresting prefixes.
959     (when (and (not re-only)
960                gnus-simplify-ignored-prefixes
961                (string-match gnus-simplify-ignored-prefixes subject))
962       (setq subject (substring subject (match-end 0))))
963     ;; Remove words in parentheses from end.
964     (unless re-only
965       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
966         (setq subject (substring subject 0 (match-beginning 0)))))
967     ;; Return subject string.
968     subject))
969
970 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
971 ;; all whitespace.
972 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
973   (goto-char (point-min))
974   (while (re-search-forward regexp nil t)
975       (replace-match (or newtext ""))))
976
977 (defun gnus-simplify-buffer-fuzzy ()
978   "Simplify string in the buffer fuzzily.
979 The string in the accessible portion of the current buffer is simplified.
980 It is assumed to be a single-line subject.
981 Whitespace is generally cleaned up, and miscellaneous leading/trailing
982 matter is removed.  Additional things can be deleted by setting
983 gnus-simplify-subject-fuzzy-regexp."
984   (let ((case-fold-search t)
985         (modified-tick))
986     (gnus-simplify-buffer-fuzzy-step "\t" " ")
987
988     (while (not (eq modified-tick (buffer-modified-tick)))
989       (setq modified-tick (buffer-modified-tick))
990       (cond
991        ((listp gnus-simplify-subject-fuzzy-regexp)
992         (mapcar 'gnus-simplify-buffer-fuzzy-step
993                 gnus-simplify-subject-fuzzy-regexp))
994        (gnus-simplify-subject-fuzzy-regexp
995         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
996       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
997       (gnus-simplify-buffer-fuzzy-step
998        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
999       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1000
1001     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1002     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1003     (gnus-simplify-buffer-fuzzy-step " $")
1004     (gnus-simplify-buffer-fuzzy-step "^ +")))
1005
1006 (defun gnus-simplify-subject-fuzzy (subject)
1007   "Simplify a subject string fuzzily.
1008 See gnus-simplify-buffer-fuzzy for details."
1009   (save-excursion
1010     (gnus-set-work-buffer)
1011     (let ((case-fold-search t))
1012       (insert subject)
1013       (inline (gnus-simplify-buffer-fuzzy))
1014       (buffer-string))))
1015
1016 (defsubst gnus-simplify-subject-fully (subject)
1017   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1018   (cond
1019    ((null gnus-summary-gather-subject-limit)
1020     (gnus-simplify-subject-re subject))
1021    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1022     (gnus-simplify-subject-fuzzy subject))
1023    ((numberp gnus-summary-gather-subject-limit)
1024     (gnus-limit-string (gnus-simplify-subject-re subject)
1025                        gnus-summary-gather-subject-limit))
1026    (t
1027     subject)))
1028
1029 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1030   "Check whether two subjects are equal.  If optional argument
1031 simple-first is t, first argument is already simplified."
1032   (cond
1033    ((null simple-first)
1034     (equal (gnus-simplify-subject-fully s1)
1035            (gnus-simplify-subject-fully s2)))
1036    (t
1037     (equal s1
1038            (gnus-simplify-subject-fully s2)))))
1039
1040 (defun gnus-summary-bubble-group ()
1041   "Increase the score of the current group.
1042 This is a handy function to add to `gnus-summary-exit-hook' to
1043 increase the score of each group you read."
1044   (gnus-group-add-score gnus-newsgroup-name))
1045
1046 \f
1047 ;;;
1048 ;;; Gnus summary mode
1049 ;;;
1050
1051 (put 'gnus-summary-mode 'mode-class 'special)
1052
1053 (when t
1054   ;; Non-orthogonal keys
1055
1056   (gnus-define-keys gnus-summary-mode-map
1057     " " gnus-summary-next-page
1058     "\177" gnus-summary-prev-page
1059     [delete] gnus-summary-prev-page
1060     "\r" gnus-summary-scroll-up
1061     "n" gnus-summary-next-unread-article
1062     "p" gnus-summary-prev-unread-article
1063     "N" gnus-summary-next-article
1064     "P" gnus-summary-prev-article
1065     "\M-\C-n" gnus-summary-next-same-subject
1066     "\M-\C-p" gnus-summary-prev-same-subject
1067     "\M-n" gnus-summary-next-unread-subject
1068     "\M-p" gnus-summary-prev-unread-subject
1069     "." gnus-summary-first-unread-article
1070     "," gnus-summary-best-unread-article
1071     "\M-s" gnus-summary-search-article-forward
1072     "\M-r" gnus-summary-search-article-backward
1073     "<" gnus-summary-beginning-of-article
1074     ">" gnus-summary-end-of-article
1075     "j" gnus-summary-goto-article
1076     "^" gnus-summary-refer-parent-article
1077     "\M-^" gnus-summary-refer-article
1078     "u" gnus-summary-tick-article-forward
1079     "!" gnus-summary-tick-article-forward
1080     "U" gnus-summary-tick-article-backward
1081     "d" gnus-summary-mark-as-read-forward
1082     "D" gnus-summary-mark-as-read-backward
1083     "E" gnus-summary-mark-as-expirable
1084     "\M-u" gnus-summary-clear-mark-forward
1085     "\M-U" gnus-summary-clear-mark-backward
1086     "k" gnus-summary-kill-same-subject-and-select
1087     "\C-k" gnus-summary-kill-same-subject
1088     "\M-\C-k" gnus-summary-kill-thread
1089     "\M-\C-l" gnus-summary-lower-thread
1090     "e" gnus-summary-edit-article
1091     "#" gnus-summary-mark-as-processable
1092     "\M-#" gnus-summary-unmark-as-processable
1093     "\M-\C-t" gnus-summary-toggle-threads
1094     "\M-\C-s" gnus-summary-show-thread
1095     "\M-\C-h" gnus-summary-hide-thread
1096     "\M-\C-f" gnus-summary-next-thread
1097     "\M-\C-b" gnus-summary-prev-thread
1098     "\M-\C-u" gnus-summary-up-thread
1099     "\M-\C-d" gnus-summary-down-thread
1100     "&" gnus-summary-execute-command
1101     "c" gnus-summary-catchup-and-exit
1102     "\C-w" gnus-summary-mark-region-as-read
1103     "\C-t" gnus-summary-toggle-truncation
1104     "?" gnus-summary-mark-as-dormant
1105     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1106     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1107     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1108     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1109     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1110     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1111     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1112     "=" gnus-summary-expand-window
1113     "\C-x\C-s" gnus-summary-reselect-current-group
1114     "\M-g" gnus-summary-rescan-group
1115     "w" gnus-summary-stop-page-breaking
1116     "\C-c\C-r" gnus-summary-caesar-message
1117     "\M-t" gnus-summary-toggle-mime
1118     "f" gnus-summary-followup
1119     "F" gnus-summary-followup-with-original
1120     "C" gnus-summary-cancel-article
1121     "r" gnus-summary-reply
1122     "R" gnus-summary-reply-with-original
1123     "\C-c\C-f" gnus-summary-mail-forward
1124     "o" gnus-summary-save-article
1125     "\C-o" gnus-summary-save-article-mail
1126     "|" gnus-summary-pipe-output
1127     "\M-k" gnus-summary-edit-local-kill
1128     "\M-K" gnus-summary-edit-global-kill
1129     ;; "V" gnus-version
1130     "\C-c\C-d" gnus-summary-describe-group
1131     "q" gnus-summary-exit
1132     "Q" gnus-summary-exit-no-update
1133     "\C-c\C-i" gnus-info-find-node
1134     gnus-mouse-2 gnus-mouse-pick-article
1135     "m" gnus-summary-mail-other-window
1136     "a" gnus-summary-post-news
1137     "x" gnus-summary-limit-to-unread
1138     "s" gnus-summary-isearch-article
1139     "t" gnus-article-hide-headers
1140     "g" gnus-summary-show-article
1141     "l" gnus-summary-goto-last-article
1142     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1143     "\C-d" gnus-summary-enter-digest-group
1144     "\M-\C-d" gnus-summary-read-document
1145     "\C-c\C-b" gnus-bug
1146     "*" gnus-cache-enter-article
1147     "\M-*" gnus-cache-remove-article
1148     "\M-&" gnus-summary-universal-argument
1149     "\C-l" gnus-recenter
1150     "I" gnus-summary-increase-score
1151     "L" gnus-summary-lower-score
1152
1153     "V" gnus-summary-score-map
1154     "X" gnus-uu-extract-map
1155     "S" gnus-summary-send-map)
1156
1157   ;; Sort of orthogonal keymap
1158   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1159     "t" gnus-summary-tick-article-forward
1160     "!" gnus-summary-tick-article-forward
1161     "d" gnus-summary-mark-as-read-forward
1162     "r" gnus-summary-mark-as-read-forward
1163     "c" gnus-summary-clear-mark-forward
1164     " " gnus-summary-clear-mark-forward
1165     "e" gnus-summary-mark-as-expirable
1166     "x" gnus-summary-mark-as-expirable
1167     "?" gnus-summary-mark-as-dormant
1168     "b" gnus-summary-set-bookmark
1169     "B" gnus-summary-remove-bookmark
1170     "#" gnus-summary-mark-as-processable
1171     "\M-#" gnus-summary-unmark-as-processable
1172     "S" gnus-summary-limit-include-expunged
1173     "C" gnus-summary-catchup
1174     "H" gnus-summary-catchup-to-here
1175     "\C-c" gnus-summary-catchup-all
1176     "k" gnus-summary-kill-same-subject-and-select
1177     "K" gnus-summary-kill-same-subject
1178     "P" gnus-uu-mark-map)
1179
1180   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1181     "c" gnus-summary-clear-above
1182     "u" gnus-summary-tick-above
1183     "m" gnus-summary-mark-above
1184     "k" gnus-summary-kill-below)
1185
1186   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1187     "/" gnus-summary-limit-to-subject
1188     "n" gnus-summary-limit-to-articles
1189     "w" gnus-summary-pop-limit
1190     "s" gnus-summary-limit-to-subject
1191     "a" gnus-summary-limit-to-author
1192     "u" gnus-summary-limit-to-unread
1193     "m" gnus-summary-limit-to-marks
1194     "v" gnus-summary-limit-to-score
1195     "D" gnus-summary-limit-include-dormant
1196     "d" gnus-summary-limit-exclude-dormant
1197     "t" gnus-summary-limit-to-age
1198     "E" gnus-summary-limit-include-expunged
1199     "c" gnus-summary-limit-exclude-childless-dormant
1200     "C" gnus-summary-limit-mark-excluded-as-read)
1201
1202   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1203     "n" gnus-summary-next-unread-article
1204     "p" gnus-summary-prev-unread-article
1205     "N" gnus-summary-next-article
1206     "P" gnus-summary-prev-article
1207     "\C-n" gnus-summary-next-same-subject
1208     "\C-p" gnus-summary-prev-same-subject
1209     "\M-n" gnus-summary-next-unread-subject
1210     "\M-p" gnus-summary-prev-unread-subject
1211     "f" gnus-summary-first-unread-article
1212     "b" gnus-summary-best-unread-article
1213     "j" gnus-summary-goto-article
1214     "g" gnus-summary-goto-subject
1215     "l" gnus-summary-goto-last-article
1216     "p" gnus-summary-pop-article)
1217
1218   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1219     "k" gnus-summary-kill-thread
1220     "l" gnus-summary-lower-thread
1221     "i" gnus-summary-raise-thread
1222     "T" gnus-summary-toggle-threads
1223     "t" gnus-summary-rethread-current
1224     "^" gnus-summary-reparent-thread
1225     "s" gnus-summary-show-thread
1226     "S" gnus-summary-show-all-threads
1227     "h" gnus-summary-hide-thread
1228     "H" gnus-summary-hide-all-threads
1229     "n" gnus-summary-next-thread
1230     "p" gnus-summary-prev-thread
1231     "u" gnus-summary-up-thread
1232     "o" gnus-summary-top-thread
1233     "d" gnus-summary-down-thread
1234     "#" gnus-uu-mark-thread
1235     "\M-#" gnus-uu-unmark-thread)
1236
1237   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1238     "g" gnus-summary-prepare 
1239     "c" gnus-summary-insert-cached-articles)
1240
1241   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1242     "c" gnus-summary-catchup-and-exit
1243     "C" gnus-summary-catchup-all-and-exit
1244     "E" gnus-summary-exit-no-update
1245     "Q" gnus-summary-exit
1246     "Z" gnus-summary-exit
1247     "n" gnus-summary-catchup-and-goto-next-group
1248     "R" gnus-summary-reselect-current-group
1249     "G" gnus-summary-rescan-group
1250     "N" gnus-summary-next-group
1251     "s" gnus-summary-save-newsrc
1252     "P" gnus-summary-prev-group)
1253
1254   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1255     " " gnus-summary-next-page
1256     "n" gnus-summary-next-page
1257     "\177" gnus-summary-prev-page
1258     [delete] gnus-summary-prev-page
1259     "p" gnus-summary-prev-page
1260     "\r" gnus-summary-scroll-up
1261     "<" gnus-summary-beginning-of-article
1262     ">" gnus-summary-end-of-article
1263     "b" gnus-summary-beginning-of-article
1264     "e" gnus-summary-end-of-article
1265     "^" gnus-summary-refer-parent-article
1266     "r" gnus-summary-refer-parent-article
1267     "R" gnus-summary-refer-references
1268     "g" gnus-summary-show-article
1269     "s" gnus-summary-isearch-article
1270     "P" gnus-summary-print-article)
1271
1272   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1273     "b" gnus-article-add-buttons
1274     "B" gnus-article-add-buttons-to-head
1275     "o" gnus-article-treat-overstrike
1276     "e" gnus-article-emphasize
1277     "w" gnus-article-fill-cited-article
1278     "c" gnus-article-remove-cr
1279     "q" gnus-article-de-quoted-unreadable
1280     "f" gnus-article-display-x-face
1281     "l" gnus-summary-stop-page-breaking
1282     "r" gnus-summary-caesar-message
1283     "t" gnus-article-hide-headers
1284     "v" gnus-summary-verbose-headers
1285     "m" gnus-summary-toggle-mime
1286     "h" gnus-article-treat-html)
1287
1288   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1289     "a" gnus-article-hide
1290     "h" gnus-article-hide-headers
1291     "b" gnus-article-hide-boring-headers
1292     "s" gnus-article-hide-signature
1293     "c" gnus-article-hide-citation
1294     "p" gnus-article-hide-pgp
1295     "P" gnus-article-hide-pem
1296     "\C-c" gnus-article-hide-citation-maybe)
1297
1298   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1299     "a" gnus-article-highlight
1300     "h" gnus-article-highlight-headers
1301     "c" gnus-article-highlight-citation
1302     "s" gnus-article-highlight-signature)
1303
1304   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1305     "z" gnus-article-date-ut
1306     "u" gnus-article-date-ut
1307     "l" gnus-article-date-local
1308     "e" gnus-article-date-lapsed
1309     "o" gnus-article-date-original
1310     "s" gnus-article-date-user)
1311
1312   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1313     "t" gnus-article-remove-trailing-blank-lines
1314     "l" gnus-article-strip-leading-blank-lines
1315     "m" gnus-article-strip-multiple-blank-lines
1316     "a" gnus-article-strip-blank-lines)
1317
1318   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1319     "v" gnus-version
1320     "f" gnus-summary-fetch-faq
1321     "d" gnus-summary-describe-group
1322     "h" gnus-summary-describe-briefly
1323     "i" gnus-info-find-node)
1324
1325   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1326     "e" gnus-summary-expire-articles
1327     "\M-\C-e" gnus-summary-expire-articles-now
1328     "\177" gnus-summary-delete-article
1329     [delete] gnus-summary-delete-article
1330     "m" gnus-summary-move-article
1331     "r" gnus-summary-respool-article
1332     "w" gnus-summary-edit-article
1333     "c" gnus-summary-copy-article
1334     "B" gnus-summary-crosspost-article
1335     "q" gnus-summary-respool-query
1336     "i" gnus-summary-import-article
1337     "p" gnus-summary-article-posted-p)
1338
1339   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1340     "o" gnus-summary-save-article
1341     "m" gnus-summary-save-article-mail
1342     "F" gnus-summary-write-article-file
1343     "r" gnus-summary-save-article-rmail
1344     "f" gnus-summary-save-article-file
1345     "b" gnus-summary-save-article-body-file
1346     "h" gnus-summary-save-article-folder
1347     "v" gnus-summary-save-article-vm
1348     "p" gnus-summary-pipe-output
1349     "s" gnus-soup-add-article))
1350
1351 (defun gnus-summary-make-menu-bar ()
1352   (gnus-turn-off-edit-menu 'summary)
1353
1354   (unless (boundp 'gnus-summary-misc-menu)
1355
1356     (easy-menu-define
1357      gnus-summary-kill-menu gnus-summary-mode-map ""
1358      (cons
1359       "Score"
1360       (nconc
1361        (list
1362         ["Enter score..." gnus-summary-score-entry t]
1363         ["Customize" gnus-score-customize t])
1364        (gnus-make-score-map 'increase)
1365        (gnus-make-score-map 'lower)
1366        '(("Mark"
1367           ["Kill below" gnus-summary-kill-below t]
1368           ["Mark above" gnus-summary-mark-above t]
1369           ["Tick above" gnus-summary-tick-above t]
1370           ["Clear above" gnus-summary-clear-above t])
1371          ["Current score" gnus-summary-current-score t]
1372          ["Set score" gnus-summary-set-score t]
1373          ["Switch current score file..." gnus-score-change-score-file t]
1374          ["Set mark below..." gnus-score-set-mark-below t]
1375          ["Set expunge below..." gnus-score-set-expunge-below t]
1376          ["Edit current score file" gnus-score-edit-current-scores t]
1377          ["Edit score file" gnus-score-edit-file t]
1378          ["Trace score" gnus-score-find-trace t]
1379          ["Find words" gnus-score-find-favourite-words t]
1380          ["Rescore buffer" gnus-summary-rescore t]
1381          ["Increase score..." gnus-summary-increase-score t]
1382          ["Lower score..." gnus-summary-lower-score t]))))
1383
1384     '(("Default header"
1385        ["Ask" (gnus-score-set-default 'gnus-score-default-header nil)
1386         :style radio 
1387         :selected (null gnus-score-default-header)]
1388        ["From" (gnus-score-set-default 'gnus-score-default-header 'a)
1389         :style radio 
1390         :selected (eq gnus-score-default-header 'a)]
1391        ["Subject" (gnus-score-set-default 'gnus-score-default-header 's)
1392         :style radio 
1393         :selected (eq gnus-score-default-header 's)]
1394        ["Article body"
1395         (gnus-score-set-default 'gnus-score-default-header 'b)
1396         :style radio 
1397         :selected (eq gnus-score-default-header 'b )]
1398        ["All headers"
1399         (gnus-score-set-default 'gnus-score-default-header 'h)
1400         :style radio 
1401         :selected (eq gnus-score-default-header 'h )]
1402        ["Message-ID" (gnus-score-set-default 'gnus-score-default-header 'i)
1403         :style radio 
1404         :selected (eq gnus-score-default-header 'i )]
1405        ["Thread" (gnus-score-set-default 'gnus-score-default-header 't)
1406         :style radio 
1407         :selected (eq gnus-score-default-header 't )]
1408        ["Crossposting"
1409         (gnus-score-set-default 'gnus-score-default-header 'x)
1410         :style radio 
1411         :selected (eq gnus-score-default-header 'x )]
1412        ["Lines" (gnus-score-set-default 'gnus-score-default-header 'l)
1413         :style radio 
1414         :selected (eq gnus-score-default-header 'l )]
1415        ["Date" (gnus-score-set-default 'gnus-score-default-header 'd)
1416         :style radio 
1417         :selected (eq gnus-score-default-header 'd )]
1418        ["Followups to author"
1419         (gnus-score-set-default 'gnus-score-default-header 'f)
1420         :style radio 
1421         :selected (eq gnus-score-default-header 'f )])
1422       ("Default type"
1423        ["Ask" (gnus-score-set-default 'gnus-score-default-type nil)
1424         :style radio 
1425         :selected (null gnus-score-default-type)]
1426        ;; The `:active' key is commented out in the following,
1427        ;; because the GNU Emacs hack to support radio buttons use
1428        ;; active to indicate which button is selected.  
1429        ["Substring" (gnus-score-set-default 'gnus-score-default-type 's)
1430         :style radio 
1431         ;; :active (not (memq gnus-score-default-header '(l d)))
1432         :selected (eq gnus-score-default-type 's)]
1433        ["Regexp" (gnus-score-set-default 'gnus-score-default-type 'r)
1434         :style radio
1435         ;; :active (not (memq gnus-score-default-header '(l d)))
1436         :selected (eq gnus-score-default-type 'r)]
1437        ["Exact" (gnus-score-set-default 'gnus-score-default-type 'e)
1438         :style radio
1439         ;; :active (not (memq gnus-score-default-header '(l d)))
1440         :selected (eq gnus-score-default-type 'e)]
1441        ["Fuzzy" (gnus-score-set-default 'gnus-score-default-type 'f)
1442         :style radio 
1443         ;; :active (not (memq gnus-score-default-header '(l d)))
1444         :selected (eq gnus-score-default-type 'f)]
1445        ["Before date" (gnus-score-set-default 'gnus-score-default-type 'b)
1446         :style radio 
1447         ;; :active (eq (gnus-score-default-header 'd))
1448         :selected (eq gnus-score-default-type 'b)]
1449        ["At date" (gnus-score-set-default 'gnus-score-default-type 'n)
1450         :style radio 
1451         ;; :active (eq (gnus-score-default-header 'd))
1452         :selected (eq gnus-score-default-type 'n)]
1453        ["After date" (gnus-score-set-default 'gnus-score-default-type 'a)
1454         :style radio 
1455         ;; :active (eq (gnus-score-default-header 'd))
1456         :selected (eq gnus-score-default-type 'a)]
1457        ["Less than number"
1458         (gnus-score-set-default 'gnus-score-default-type '<)
1459         :style radio 
1460         ;; :active (eq (gnus-score-default-header 'l))
1461         :selected (eq gnus-score-default-type '<)]
1462        ["Equal to number"
1463         (gnus-score-set-default 'gnus-score-default-type '=)
1464         :style radio 
1465         ;; :active (eq (gnus-score-default-header 'l))
1466         :selected (eq gnus-score-default-type '=)]
1467        ["Greater than number" 
1468         (gnus-score-set-default 'gnus-score-default-type '>)
1469         :style radio 
1470         ;; :active (eq (gnus-score-default-header 'l))
1471         :selected (eq gnus-score-default-type '>)])
1472       ["Default fold" gnus-score-default-fold-toggle
1473        :style toggle
1474        :selected gnus-score-default-fold]
1475       ("Default duration"
1476        ["Ask" (gnus-score-set-default 'gnus-score-default-duration nil)
1477         :style radio
1478         :selected (null gnus-score-default-duration)]
1479        ["Permanent"
1480         (gnus-score-set-default 'gnus-score-default-duration 'p)
1481         :style radio
1482         :selected (eq gnus-score-default-duration 'p)]
1483        ["Temporary"
1484         (gnus-score-set-default 'gnus-score-default-duration 't)
1485         :style radio
1486         :selected (eq gnus-score-default-duration 't)]
1487        ["Immediate" 
1488         (gnus-score-set-default 'gnus-score-default-duration 'i)
1489         :style radio
1490         :selected (eq gnus-score-default-duration 'i)]))
1491
1492     (easy-menu-define
1493      gnus-summary-article-menu gnus-summary-mode-map ""
1494      '("Article"
1495        ("Hide"
1496         ["All" gnus-article-hide t]
1497         ["Headers" gnus-article-hide-headers t]
1498         ["Signature" gnus-article-hide-signature t]
1499         ["Citation" gnus-article-hide-citation t]
1500         ["PGP" gnus-article-hide-pgp t]
1501         ["Boring headers" gnus-article-hide-boring-headers t])
1502        ("Highlight"
1503         ["All" gnus-article-highlight t]
1504         ["Headers" gnus-article-highlight-headers t]
1505         ["Signature" gnus-article-highlight-signature t]
1506         ["Citation" gnus-article-highlight-citation t])
1507        ("Date"
1508         ["Local" gnus-article-date-local t]
1509         ["UT" gnus-article-date-ut t]
1510         ["Original" gnus-article-date-original t]
1511         ["Lapsed" gnus-article-date-lapsed t]
1512         ["User-defined" gnus-article-date-user t])
1513        ("Washing"
1514         ("Remove Blanks"
1515          ["Leading" gnus-article-strip-leading-blank-lines t]
1516          ["Multiple" gnus-article-strip-multiple-blank-lines t]
1517          ["Trailing" gnus-article-remove-trailing-blank-lines t]
1518          ["All of the above" gnus-article-strip-blank-lines t])
1519         ["Overstrike" gnus-article-treat-overstrike t]
1520         ["Emphasis" gnus-article-emphasize t]
1521         ["Word wrap" gnus-article-fill-cited-article t]
1522         ["CR" gnus-article-remove-cr t]
1523         ["Show X-Face" gnus-article-display-x-face t]
1524         ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1525         ["UnHTMLize" gnus-article-treat-html t]
1526         ["Rot 13" gnus-summary-caesar-message t]
1527         ["Unix pipe" gnus-summary-pipe-message t]
1528         ["Add buttons" gnus-article-add-buttons t]
1529         ["Add buttons to head" gnus-article-add-buttons-to-head t]
1530         ["Stop page breaking" gnus-summary-stop-page-breaking t]
1531         ["Toggle MIME" gnus-summary-toggle-mime t]
1532         ["Verbose header" gnus-summary-verbose-headers t]
1533         ["Toggle header" gnus-summary-toggle-header t])
1534        ("Output"
1535         ["Save in default format" gnus-summary-save-article t]
1536         ["Save in file" gnus-summary-save-article-file t]
1537         ["Save in Unix mail format" gnus-summary-save-article-mail t]
1538         ["Write to file" gnus-summary-write-article-mail t]
1539         ["Save in MH folder" gnus-summary-save-article-folder t]
1540         ["Save in VM folder" gnus-summary-save-article-vm t]
1541         ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1542         ["Save body in file" gnus-summary-save-article-body-file t]
1543         ["Pipe through a filter" gnus-summary-pipe-output t]
1544         ["Add to SOUP packet" gnus-soup-add-article t]
1545         ["Print" gnus-summary-print-article t])
1546        ("Backend"
1547         ["Respool article..." gnus-summary-respool-article t]
1548         ["Move article..." gnus-summary-move-article
1549          (gnus-check-backend-function
1550           'request-move-article gnus-newsgroup-name)]
1551         ["Copy article..." gnus-summary-copy-article t]
1552         ["Crosspost article..." gnus-summary-crosspost-article
1553          (gnus-check-backend-function
1554           'request-replace-article gnus-newsgroup-name)]
1555         ["Import file..." gnus-summary-import-article t]
1556         ["Check if posted" gnus-summary-article-posted-p t]
1557         ["Edit article" gnus-summary-edit-article
1558          (not (gnus-group-read-only-p))]
1559         ["Delete article" gnus-summary-delete-article
1560          (gnus-check-backend-function
1561           'request-expire-articles gnus-newsgroup-name)]
1562         ["Query respool" gnus-summary-respool-query t]
1563         ["Delete expirable articles" gnus-summary-expire-articles-now
1564          (gnus-check-backend-function
1565           'request-expire-articles gnus-newsgroup-name)])
1566        ("Extract"
1567         ["Uudecode" gnus-uu-decode-uu t]
1568         ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1569         ["Unshar" gnus-uu-decode-unshar t]
1570         ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1571         ["Save" gnus-uu-decode-save t]
1572         ["Binhex" gnus-uu-decode-binhex t]
1573         ["Postscript" gnus-uu-decode-postscript t])
1574        ("Cache"
1575         ["Enter article" gnus-cache-enter-article t]
1576         ["Remove article" gnus-cache-remove-article t])
1577        ["Enter digest buffer" gnus-summary-enter-digest-group t]
1578        ["Isearch article..." gnus-summary-isearch-article t]
1579        ["Beginning of the article" gnus-summary-beginning-of-article t]
1580        ["End of the article" gnus-summary-end-of-article t]
1581        ["Fetch parent of article" gnus-summary-refer-parent-article t]
1582        ["Fetch referenced articles" gnus-summary-refer-references t]
1583        ["Fetch article with id..." gnus-summary-refer-article t]
1584        ["Redisplay" gnus-summary-show-article t]))
1585
1586     (easy-menu-define
1587      gnus-summary-thread-menu gnus-summary-mode-map ""
1588      '("Threads"
1589        ["Toggle threading" gnus-summary-toggle-threads t]
1590        ["Hide threads" gnus-summary-hide-all-threads t]
1591        ["Show threads" gnus-summary-show-all-threads t]
1592        ["Hide thread" gnus-summary-hide-thread t]
1593        ["Show thread" gnus-summary-show-thread t]
1594        ["Go to next thread" gnus-summary-next-thread t]
1595        ["Go to previous thread" gnus-summary-prev-thread t]
1596        ["Go down thread" gnus-summary-down-thread t]
1597        ["Go up thread" gnus-summary-up-thread t]
1598        ["Top of thread" gnus-summary-top-thread t]
1599        ["Mark thread as read" gnus-summary-kill-thread t]
1600        ["Lower thread score" gnus-summary-lower-thread t]
1601        ["Raise thread score" gnus-summary-raise-thread t]
1602        ["Rethread current" gnus-summary-rethread-current t]
1603        ))
1604
1605     (easy-menu-define
1606      gnus-summary-post-menu gnus-summary-mode-map ""
1607      '("Post"
1608        ["Post an article" gnus-summary-post-news t]
1609        ["Followup" gnus-summary-followup t]
1610        ["Followup and yank" gnus-summary-followup-with-original t]
1611        ["Supersede article" gnus-summary-supersede-article t]
1612        ["Cancel article" gnus-summary-cancel-article t]
1613        ["Reply" gnus-summary-reply t]
1614        ["Reply and yank" gnus-summary-reply-with-original t]
1615        ["Wide reply" gnus-summary-wide-reply t]
1616        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1617        ["Mail forward" gnus-summary-mail-forward t]
1618        ["Post forward" gnus-summary-post-forward t]
1619        ["Digest and mail" gnus-uu-digest-mail-forward t]
1620        ["Digest and post" gnus-uu-digest-post-forward t]
1621        ["Resend message" gnus-summary-resend-message t]
1622        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1623        ["Send a mail" gnus-summary-mail-other-window t]
1624        ["Uuencode and post" gnus-uu-post-news t]
1625        ["Followup via news" gnus-summary-followup-to-mail t]
1626        ["Followup via news and yank"
1627         gnus-summary-followup-to-mail-with-original t]
1628        ;;("Draft"
1629        ;;["Send" gnus-summary-send-draft t]
1630        ;;["Send bounced" gnus-resend-bounced-mail t])
1631        ))
1632
1633     (easy-menu-define
1634      gnus-summary-misc-menu gnus-summary-mode-map ""
1635      '("Misc"
1636        ("Mark Read"
1637         ["Mark as read" gnus-summary-mark-as-read-forward t]
1638         ["Mark same subject and select"
1639          gnus-summary-kill-same-subject-and-select t]
1640         ["Mark same subject" gnus-summary-kill-same-subject t]
1641         ["Catchup" gnus-summary-catchup t]
1642         ["Catchup all" gnus-summary-catchup-all t]
1643         ["Catchup to here" gnus-summary-catchup-to-here t]
1644         ["Catchup region" gnus-summary-mark-region-as-read t]
1645         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1646        ("Mark Various"
1647         ["Tick" gnus-summary-tick-article-forward t]
1648         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1649         ["Remove marks" gnus-summary-clear-mark-forward t]
1650         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1651         ["Set bookmark" gnus-summary-set-bookmark t]
1652         ["Remove bookmark" gnus-summary-remove-bookmark t])
1653        ("Mark Limit"
1654         ["Marks..." gnus-summary-limit-to-marks t]
1655         ["Subject..." gnus-summary-limit-to-subject t]
1656         ["Author..." gnus-summary-limit-to-author t]
1657         ["Age..." gnus-summary-limit-to-age t]
1658         ["Score" gnus-summary-limit-to-score t]
1659         ["Unread" gnus-summary-limit-to-unread t]
1660         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1661         ["Articles" gnus-summary-limit-to-articles t]
1662         ["Pop limit" gnus-summary-pop-limit t]
1663         ["Show dormant" gnus-summary-limit-include-dormant t]
1664         ["Hide childless dormant" 
1665          gnus-summary-limit-exclude-childless-dormant t]
1666         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1667         ["Show expunged" gnus-summary-show-all-expunged t])
1668        ("Process Mark"
1669         ["Set mark" gnus-summary-mark-as-processable t]
1670         ["Remove mark" gnus-summary-unmark-as-processable t]
1671         ["Remove all marks" gnus-summary-unmark-all-processable t]
1672         ["Mark above" gnus-uu-mark-over t]
1673         ["Mark series" gnus-uu-mark-series t]
1674         ["Mark region" gnus-uu-mark-region t]
1675         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1676         ["Mark all" gnus-uu-mark-all t]
1677         ["Mark buffer" gnus-uu-mark-buffer t]
1678         ["Mark sparse" gnus-uu-mark-sparse t]
1679         ["Mark thread" gnus-uu-mark-thread t]
1680         ["Unmark thread" gnus-uu-unmark-thread t]
1681         ("Process Mark Sets"
1682          ["Kill" gnus-summary-kill-process-mark t]
1683          ["Yank" gnus-summary-yank-process-mark
1684           gnus-newsgroup-process-stack]
1685          ["Save" gnus-summary-save-process-mark t]))
1686        ("Scroll article"
1687         ["Page forward" gnus-summary-next-page t]
1688         ["Page backward" gnus-summary-prev-page t]
1689         ["Line forward" gnus-summary-scroll-up t])
1690        ("Move"
1691         ["Next unread article" gnus-summary-next-unread-article t]
1692         ["Previous unread article" gnus-summary-prev-unread-article t]
1693         ["Next article" gnus-summary-next-article t]
1694         ["Previous article" gnus-summary-prev-article t]
1695         ["Next unread subject" gnus-summary-next-unread-subject t]
1696         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1697         ["Next article same subject" gnus-summary-next-same-subject t]
1698         ["Previous article same subject" gnus-summary-prev-same-subject t]
1699         ["First unread article" gnus-summary-first-unread-article t]
1700         ["Best unread article" gnus-summary-best-unread-article t]
1701         ["Go to subject number..." gnus-summary-goto-subject t]
1702         ["Go to article number..." gnus-summary-goto-article t]
1703         ["Go to the last article" gnus-summary-goto-last-article t]
1704         ["Pop article off history" gnus-summary-pop-article t])
1705        ("Sort"
1706         ["Sort by number" gnus-summary-sort-by-number t]
1707         ["Sort by author" gnus-summary-sort-by-author t]
1708         ["Sort by subject" gnus-summary-sort-by-subject t]
1709         ["Sort by date" gnus-summary-sort-by-date t]
1710         ["Sort by score" gnus-summary-sort-by-score t]
1711         ["Sort by lines" gnus-summary-sort-by-lines t])
1712        ("Help"
1713         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1714         ["Describe group" gnus-summary-describe-group t]
1715         ["Read manual" gnus-info-find-node t])
1716        ("Modes"
1717         ["Pick and read" gnus-pick-mode t]
1718         ["Binary" gnus-binary-mode t])
1719        ("Regeneration"
1720         ["Regenerate" gnus-summary-prepare t]
1721         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1722         ["Toggle threading" gnus-summary-toggle-threads t])
1723        ["Filter articles..." gnus-summary-execute-command t]
1724        ["Run command on subjects..." gnus-summary-universal-argument t]
1725        ["Search articles forward..." gnus-summary-search-article-forward t]
1726        ["Search articles backward..." gnus-summary-search-article-backward t]
1727        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1728        ["Expand window" gnus-summary-expand-window t]
1729        ["Expire expirable articles" gnus-summary-expire-articles
1730         (gnus-check-backend-function
1731          'request-expire-articles gnus-newsgroup-name)]
1732        ["Edit local kill file" gnus-summary-edit-local-kill t]
1733        ["Edit main kill file" gnus-summary-edit-global-kill t]
1734        ("Exit"
1735         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1736         ["Catchup all and exit" gnus-summary-catchup-and-exit t]
1737         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1738         ["Exit group" gnus-summary-exit t]
1739         ["Exit group without updating" gnus-summary-exit-no-update t]
1740         ["Exit and goto next group" gnus-summary-next-group t]
1741         ["Exit and goto prev group" gnus-summary-prev-group t]
1742         ["Reselect group" gnus-summary-reselect-current-group t]
1743         ["Rescan group" gnus-summary-rescan-group t]
1744         ["Update dribble" gnus-summary-save-newsrc t])))
1745
1746     (run-hooks 'gnus-summary-menu-hook)))
1747
1748 (defun gnus-score-set-default (var value)
1749   "A version of set that updates the GNU Emacs menu-bar."
1750   (set var value)
1751   ;; It is the message that forces the active status to be updated.
1752   (message ""))
1753
1754 (defun gnus-make-score-map (type)
1755   "Make a summary score map of type TYPE."
1756   (if t
1757       nil
1758     (let ((headers '(("author" "from" string)
1759                      ("subject" "subject" string)
1760                      ("article body" "body" string)
1761                      ("article head" "head" string)
1762                      ("xref" "xref" string)
1763                      ("lines" "lines" number)
1764                      ("followups to author" "followup" string)))
1765           (types '((number ("less than" <)
1766                            ("greater than" >)
1767                            ("equal" =))
1768                    (string ("substring" s)
1769                            ("exact string" e)
1770                            ("fuzzy string" f)
1771                            ("regexp" r))))
1772           (perms '(("temporary" (current-time-string))
1773                    ("permanent" nil)
1774                    ("immediate" now)))
1775           header)
1776       (list 
1777        (apply 
1778         'nconc
1779         (list
1780          (if (eq type 'lower)
1781              "Lower score"
1782            "Increase score"))
1783         (let (outh)
1784           (while headers
1785             (setq header (car headers))
1786             (setq outh 
1787                   (cons 
1788                    (apply 
1789                     'nconc
1790                     (list (car header))
1791                     (let ((ts (cdr (assoc (nth 2 header) types)))
1792                           outt)
1793                       (while ts
1794                         (setq outt
1795                               (cons 
1796                                (apply 
1797                                 'nconc
1798                                 (list (caar ts))
1799                                 (let ((ps perms)
1800                                       outp)
1801                                   (while ps
1802                                     (setq outp
1803                                           (cons
1804                                            (vector
1805                                             (caar ps)
1806                                             (list
1807                                              'gnus-summary-score-entry
1808                                              (nth 1 header)
1809                                              (if (or (string= (nth 1 header)
1810                                                               "head")
1811                                                      (string= (nth 1 header)
1812                                                               "body"))
1813                                                  ""
1814                                                (list 'gnus-summary-header 
1815                                                      (nth 1 header)))
1816                                              (list 'quote (nth 1 (car ts)))
1817                                              (list 'gnus-score-default nil)
1818                                              (nth 1 (car ps))
1819                                              t)
1820                                             t)
1821                                            outp))
1822                                     (setq ps (cdr ps)))
1823                                   (list (nreverse outp))))
1824                                outt))
1825                         (setq ts (cdr ts)))
1826                       (list (nreverse outt))))
1827                    outh))
1828             (setq headers (cdr headers)))
1829           (list (nreverse outh))))))))
1830
1831 \f
1832
1833 (defun gnus-summary-mode (&optional group)
1834   "Major mode for reading articles.
1835
1836 All normal editing commands are switched off.
1837 \\<gnus-summary-mode-map>
1838 Each line in this buffer represents one article.  To read an
1839 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
1840 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
1841 respectively.
1842
1843 You can also post articles and send mail from this buffer.  To
1844 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
1845 of an article, type `\\[gnus-summary-reply]'.
1846
1847 There are approx. one gazillion commands you can execute in this
1848 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
1849
1850 The following commands are available:
1851
1852 \\{gnus-summary-mode-map}"
1853   (interactive)
1854   (when (gnus-visual-p 'summary-menu 'menu)
1855     (gnus-summary-make-menu-bar))
1856   (kill-all-local-variables)
1857   (gnus-summary-make-local-variables)
1858   (gnus-make-thread-indent-array)
1859   (gnus-simplify-mode-line)
1860   (setq major-mode 'gnus-summary-mode)
1861   (setq mode-name "Summary")
1862   (make-local-variable 'minor-mode-alist)
1863   (use-local-map gnus-summary-mode-map)
1864   (buffer-disable-undo (current-buffer))
1865   (setq buffer-read-only t)             ;Disable modification
1866   (setq truncate-lines t)
1867   (setq selective-display t)
1868   (setq selective-display-ellipses t)   ;Display `...'
1869   (setq buffer-display-table gnus-summary-display-table)
1870   (gnus-set-default-directory)
1871   (setq gnus-newsgroup-name group)
1872   (make-local-variable 'gnus-summary-line-format)
1873   (make-local-variable 'gnus-summary-line-format-spec)
1874   (make-local-variable 'gnus-summary-mark-positions)
1875   (make-local-hook 'post-command-hook)
1876   (gnus-add-hook 'post-command-hook 'gnus-clear-inboxes-moved nil t)
1877   (run-hooks 'gnus-summary-mode-hook)
1878   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
1879   (gnus-update-summary-mark-positions))
1880
1881 (defun gnus-summary-make-local-variables ()
1882   "Make all the local summary buffer variables."
1883   (let ((locals gnus-summary-local-variables)
1884         global local)
1885     (while (setq local (pop locals))
1886       (if (consp local)
1887           (progn
1888             (if (eq (cdr local) 'global)
1889                 ;; Copy the global value of the variable.
1890                 (setq global (symbol-value (car local)))
1891               ;; Use the value from the list.
1892               (setq global (eval (cdr local))))
1893             (make-local-variable (car local))
1894             (set (car local) global))
1895         ;; Simple nil-valued local variable.
1896         (make-local-variable local)
1897         (set local nil)))))
1898
1899 (defun gnus-summary-clear-local-variables ()
1900   (let ((locals gnus-summary-local-variables))
1901     (while locals
1902       (if (consp (car locals))
1903           (and (vectorp (caar locals))
1904                (set (caar locals) nil))
1905         (and (vectorp (car locals))
1906              (set (car locals) nil)))
1907       (setq locals (cdr locals)))))
1908
1909 ;; Summary data functions.
1910
1911 (defmacro gnus-data-number (data)
1912   `(car ,data))
1913
1914 (defmacro gnus-data-set-number (data number)
1915   `(setcar ,data ,number))
1916
1917 (defmacro gnus-data-mark (data)
1918   `(nth 1 ,data))
1919
1920 (defmacro gnus-data-set-mark (data mark)
1921   `(setcar (nthcdr 1 ,data) ,mark))
1922
1923 (defmacro gnus-data-pos (data)
1924   `(nth 2 ,data))
1925
1926 (defmacro gnus-data-set-pos (data pos)
1927   `(setcar (nthcdr 2 ,data) ,pos))
1928
1929 (defmacro gnus-data-header (data)
1930   `(nth 3 ,data))
1931
1932 (defmacro gnus-data-set-header (data header)
1933   `(setf (nth 3 ,data) ,header))
1934
1935 (defmacro gnus-data-level (data)
1936   `(nth 4 ,data))
1937
1938 (defmacro gnus-data-unread-p (data)
1939   `(= (nth 1 ,data) gnus-unread-mark))
1940
1941 (defmacro gnus-data-read-p (data)
1942   `(/= (nth 1 ,data) gnus-unread-mark))
1943
1944 (defmacro gnus-data-pseudo-p (data)
1945   `(consp (nth 3 ,data)))
1946
1947 (defmacro gnus-data-find (number)
1948   `(assq ,number gnus-newsgroup-data))
1949
1950 (defmacro gnus-data-find-list (number &optional data)
1951   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
1952      (memq (assq ,number bdata)
1953            bdata)))
1954
1955 (defmacro gnus-data-make (number mark pos header level)
1956   `(list ,number ,mark ,pos ,header ,level))
1957
1958 (defun gnus-data-enter (after-article number mark pos header level offset)
1959   (let ((data (gnus-data-find-list after-article)))
1960     (unless data
1961       (error "No such article: %d" after-article))
1962     (setcdr data (cons (gnus-data-make number mark pos header level)
1963                        (cdr data)))
1964     (setq gnus-newsgroup-data-reverse nil)
1965     (gnus-data-update-list (cddr data) offset)))
1966
1967 (defun gnus-data-enter-list (after-article list &optional offset)
1968   (when list
1969     (let ((data (and after-article (gnus-data-find-list after-article)))
1970           (ilist list))
1971       (or data (not after-article) (error "No such article: %d" after-article))
1972       ;; Find the last element in the list to be spliced into the main
1973       ;; list.
1974       (while (cdr list)
1975         (setq list (cdr list)))
1976       (if (not data)
1977           (progn
1978             (setcdr list gnus-newsgroup-data)
1979             (setq gnus-newsgroup-data ilist)
1980             (when offset
1981               (gnus-data-update-list (cdr list) offset)))
1982         (setcdr list (cdr data))
1983         (setcdr data ilist)
1984         (when offset
1985           (gnus-data-update-list (cdr list) offset)))
1986       (setq gnus-newsgroup-data-reverse nil))))
1987
1988 (defun gnus-data-remove (article &optional offset)
1989   (let ((data gnus-newsgroup-data))
1990     (if (= (gnus-data-number (car data)) article)
1991         (progn
1992           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
1993                 gnus-newsgroup-data-reverse nil)
1994           (when offset
1995             (gnus-data-update-list gnus-newsgroup-data offset)))
1996       (while (cdr data)
1997         (when (= (gnus-data-number (cadr data)) article)
1998           (setcdr data (cddr data))
1999           (when offset
2000             (gnus-data-update-list (cdr data) offset))
2001           (setq data nil
2002                 gnus-newsgroup-data-reverse nil))
2003         (setq data (cdr data))))))
2004
2005 (defmacro gnus-data-list (backward)
2006   `(if ,backward
2007        (or gnus-newsgroup-data-reverse
2008            (setq gnus-newsgroup-data-reverse
2009                  (reverse gnus-newsgroup-data)))
2010      gnus-newsgroup-data))
2011
2012 (defun gnus-data-update-list (data offset)
2013   "Add OFFSET to the POS of all data entries in DATA."
2014   (while data
2015     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2016     (setq data (cdr data))))
2017
2018 (defun gnus-data-compute-positions ()
2019   "Compute the positions of all articles."
2020   (let ((data gnus-newsgroup-data)
2021         pos)
2022     (while data
2023       (when (setq pos (text-property-any
2024                        (point-min) (point-max)
2025                        'gnus-number (gnus-data-number (car data))))
2026         (gnus-data-set-pos (car data) (+ pos 3)))
2027       (setq data (cdr data)))))
2028
2029 (defun gnus-summary-article-pseudo-p (article)
2030   "Say whether this article is a pseudo article or not."
2031   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2032
2033 (defmacro gnus-summary-article-sparse-p (article)
2034   "Say whether this article is a sparse article or not."
2035   ` (memq ,article gnus-newsgroup-sparse))
2036
2037 (defmacro gnus-summary-article-ancient-p (article)
2038   "Say whether this article is a sparse article or not."
2039   `(memq ,article gnus-newsgroup-ancient))
2040
2041 (defun gnus-article-parent-p (number)
2042   "Say whether this article is a parent or not."
2043   (let ((data (gnus-data-find-list number)))
2044     (and (cdr data)                     ; There has to be an article after...
2045          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2046             (gnus-data-level (nth 1 data))))))
2047
2048 (defun gnus-article-children (number)
2049   "Return a list of all children to NUMBER."
2050   (let* ((data (gnus-data-find-list number))
2051          (level (gnus-data-level (car data)))
2052          children)
2053     (setq data (cdr data))
2054     (while (and data            
2055                 (= (gnus-data-level (car data)) (1+ level)))
2056       (push (gnus-data-number (car data)) children)
2057       (setq data (cdr data)))
2058     children))
2059
2060 (defmacro gnus-summary-skip-intangible ()
2061   "If the current article is intangible, then jump to a different article."
2062   '(let ((to (get-text-property (point) 'gnus-intangible)))
2063      (and to (gnus-summary-goto-subject to))))
2064
2065 (defmacro gnus-summary-article-intangible-p ()
2066   "Say whether this article is intangible or not."
2067   '(get-text-property (point) 'gnus-intangible))
2068
2069 (defun gnus-article-read-p (article)
2070   "Say whether ARTICLE is read or not."
2071   (not (or (memq article gnus-newsgroup-marked)
2072            (memq article gnus-newsgroup-unreads)
2073            (memq article gnus-newsgroup-unselected)
2074            (memq article gnus-newsgroup-dormant))))
2075
2076 ;; Some summary mode macros.
2077
2078 (defmacro gnus-summary-article-number ()
2079   "The article number of the article on the current line.
2080 If there isn's an article number here, then we return the current
2081 article number."
2082   '(progn
2083      (gnus-summary-skip-intangible)
2084      (or (get-text-property (point) 'gnus-number)
2085          (gnus-summary-last-subject))))
2086
2087 (defmacro gnus-summary-article-header (&optional number)
2088   `(gnus-data-header (gnus-data-find
2089                       ,(or number '(gnus-summary-article-number)))))
2090
2091 (defmacro gnus-summary-thread-level (&optional number)
2092   `(if (and (eq gnus-summary-make-false-root 'dummy)
2093             (get-text-property (point) 'gnus-intangible))
2094        0
2095      (gnus-data-level (gnus-data-find
2096                        ,(or number '(gnus-summary-article-number))))))
2097
2098 (defmacro gnus-summary-article-mark (&optional number)
2099   `(gnus-data-mark (gnus-data-find
2100                     ,(or number '(gnus-summary-article-number)))))
2101
2102 (defmacro gnus-summary-article-pos (&optional number)
2103   `(gnus-data-pos (gnus-data-find
2104                    ,(or number '(gnus-summary-article-number)))))
2105
2106 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2107 (defmacro gnus-summary-article-subject (&optional number)
2108   "Return current subject string or nil if nothing."
2109   `(let ((headers
2110           ,(if number
2111                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2112              '(gnus-data-header (assq (gnus-summary-article-number)
2113                                       gnus-newsgroup-data)))))
2114      (and headers
2115           (vectorp headers)
2116           (mail-header-subject headers))))
2117
2118 (defmacro gnus-summary-article-score (&optional number)
2119   "Return current article score."
2120   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2121                   gnus-newsgroup-scored))
2122        gnus-summary-default-score 0))
2123
2124 (defun gnus-summary-article-children (&optional number)
2125   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2126          (level (gnus-data-level (car data)))
2127          l children)
2128     (while (and (setq data (cdr data))
2129                 (> (setq l (gnus-data-level (car data))) level))
2130       (and (= (1+ level) l)
2131            (push (gnus-data-number (car data))
2132                  children)))
2133     (nreverse children)))
2134
2135 (defun gnus-summary-article-parent (&optional number)
2136   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2137                                     (gnus-data-list t)))
2138          (level (gnus-data-level (car data))))
2139     (if (zerop level)
2140         ()                              ; This is a root.
2141       ;; We search until we find an article with a level less than
2142       ;; this one.  That function has to be the parent.
2143       (while (and (setq data (cdr data))
2144                   (not (< (gnus-data-level (car data)) level))))
2145       (and data (gnus-data-number (car data))))))
2146
2147 (defun gnus-unread-mark-p (mark)
2148   "Say whether MARK is the unread mark."
2149   (= mark gnus-unread-mark))
2150
2151 (defun gnus-read-mark-p (mark)
2152   "Say whether MARK is one of the marks that mark as read.
2153 This is all marks except unread, ticked, dormant, and expirable."
2154   (not (or (= mark gnus-unread-mark)
2155            (= mark gnus-ticked-mark)
2156            (= mark gnus-dormant-mark)
2157            (= mark gnus-expirable-mark))))
2158
2159 (defmacro gnus-article-mark (number)
2160   `(cond
2161     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2162     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2163     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2164     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2165     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2166            gnus-ancient-mark))))
2167
2168 ;; Saving hidden threads.
2169
2170 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2171 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2172
2173 (defmacro gnus-save-hidden-threads (&rest forms)
2174   "Save hidden threads, eval FORMS, and restore the hidden threads."
2175   (let ((config (make-symbol "config")))
2176     `(let ((,config (gnus-hidden-threads-configuration)))
2177        (unwind-protect
2178            (save-excursion
2179              ,@forms)
2180          (gnus-restore-hidden-threads-configuration ,config)))))
2181
2182 (defun gnus-hidden-threads-configuration ()
2183   "Return the current hidden threads configuration."
2184   (save-excursion
2185     (let (config)
2186       (goto-char (point-min))
2187       (while (search-forward "\r" nil t)
2188         (push (1- (point)) config))
2189       config)))
2190
2191 (defun gnus-restore-hidden-threads-configuration (config)
2192   "Restore hidden threads configuration from CONFIG."
2193   (let (point buffer-read-only)
2194     (while (setq point (pop config))
2195       (when (and (< point (point-max))
2196                  (goto-char point)
2197                  (= (following-char) ?\n))
2198         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2199
2200 ;; Various summary mode internalish functions.
2201
2202 (defun gnus-mouse-pick-article (e)
2203   (interactive "e")
2204   (mouse-set-point e)
2205   (gnus-summary-next-page nil t))
2206
2207 (defun gnus-summary-setup-buffer (group)
2208   "Initialize summary buffer."
2209   (let ((buffer (concat "*Summary " group "*")))
2210     (if (get-buffer buffer)
2211         (progn
2212           (set-buffer buffer)
2213           (setq gnus-summary-buffer (current-buffer))
2214           (not gnus-newsgroup-prepared))
2215       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2216       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
2217       (gnus-add-current-to-buffer-list)
2218       (gnus-summary-mode group)
2219       (when gnus-carpal
2220         (gnus-carpal-setup-buffer 'summary))
2221       (unless gnus-single-article-buffer
2222         (make-local-variable 'gnus-article-buffer)
2223         (make-local-variable 'gnus-article-current)
2224         (make-local-variable 'gnus-original-article-buffer))
2225       (setq gnus-newsgroup-name group)
2226       t)))
2227
2228 (defun gnus-set-global-variables ()
2229   ;; Set the global equivalents of the summary buffer-local variables
2230   ;; to the latest values they had.  These reflect the summary buffer
2231   ;; that was in action when the last article was fetched.
2232   (when (eq major-mode 'gnus-summary-mode)
2233     (setq gnus-summary-buffer (current-buffer))
2234     (let ((name gnus-newsgroup-name)
2235           (marked gnus-newsgroup-marked)
2236           (unread gnus-newsgroup-unreads)
2237           (headers gnus-current-headers)
2238           (data gnus-newsgroup-data)
2239           (summary gnus-summary-buffer)
2240           (article-buffer gnus-article-buffer)
2241           (original gnus-original-article-buffer)
2242           (gac gnus-article-current)
2243           (reffed gnus-reffed-article-number)
2244           (score-file gnus-current-score-file))
2245       (save-excursion
2246         (set-buffer gnus-group-buffer)
2247         (setq gnus-newsgroup-name name)
2248         (setq gnus-newsgroup-marked marked)
2249         (setq gnus-newsgroup-unreads unread)
2250         (setq gnus-current-headers headers)
2251         (setq gnus-newsgroup-data data)
2252         (setq gnus-article-current gac)
2253         (setq gnus-summary-buffer summary)
2254         (setq gnus-article-buffer article-buffer)
2255         (setq gnus-original-article-buffer original)
2256         (setq gnus-reffed-article-number reffed)
2257         (setq gnus-current-score-file score-file)
2258         ;; The article buffer also has local variables.
2259         (when (gnus-buffer-live-p gnus-article-buffer)
2260           (set-buffer gnus-article-buffer)
2261           (setq gnus-summary-buffer summary))))))
2262
2263 (defun gnus-summary-article-unread-p (article)
2264   "Say whether ARTICLE is unread or not."
2265   (memq article gnus-newsgroup-unreads))
2266
2267 (defun gnus-summary-first-article-p (&optional article)
2268   "Return whether ARTICLE is the first article in the buffer."
2269   (if (not (setq article (or article (gnus-summary-article-number))))
2270       nil
2271     (eq article (caar gnus-newsgroup-data))))
2272
2273 (defun gnus-summary-last-article-p (&optional article)
2274   "Return whether ARTICLE is the last article in the buffer."
2275   (if (not (setq article (or article (gnus-summary-article-number))))
2276       t         ; All non-existent numbers are the last article.  :-)
2277     (not (cdr (gnus-data-find-list article)))))
2278
2279 (defun gnus-make-thread-indent-array ()
2280   (let ((n 200))
2281     (unless (and gnus-thread-indent-array
2282                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2283       (setq gnus-thread-indent-array (make-vector 201 "")
2284             gnus-thread-indent-array-level gnus-thread-indent-level)
2285       (while (>= n 0)
2286         (aset gnus-thread-indent-array n
2287               (make-string (* n gnus-thread-indent-level) ? ))
2288         (setq n (1- n))))))
2289
2290 (defun gnus-update-summary-mark-positions ()
2291   "Compute where the summary marks are to go."
2292   (save-excursion
2293     (when (and gnus-summary-buffer
2294                (get-buffer gnus-summary-buffer)
2295                (buffer-name (get-buffer gnus-summary-buffer)))
2296       (set-buffer gnus-summary-buffer))
2297     (let ((gnus-replied-mark 129)
2298           (gnus-score-below-mark 130)
2299           (gnus-score-over-mark 130)
2300           (spec gnus-summary-line-format-spec)
2301           thread gnus-visual pos)
2302       (save-excursion
2303         (gnus-set-work-buffer)
2304         (let ((gnus-summary-line-format-spec spec))
2305           (gnus-summary-insert-line
2306            [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2307           (goto-char (point-min))
2308           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2309                                              (- (point) 2)))))
2310           (goto-char (point-min))
2311           (push (cons 'replied (and (search-forward "\201" nil t)
2312                                     (- (point) 2)))
2313                 pos)
2314           (goto-char (point-min))
2315           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2316                 pos)))
2317       (setq gnus-summary-mark-positions pos))))
2318
2319 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2320   "Insert a dummy root in the summary buffer."
2321   (beginning-of-line)
2322   (gnus-add-text-properties
2323    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2324    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2325
2326 (defun gnus-summary-insert-line (gnus-tmp-header 
2327                                  gnus-tmp-level gnus-tmp-current 
2328                                  gnus-tmp-unread gnus-tmp-replied 
2329                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2330                                  &optional gnus-tmp-dummy gnus-tmp-score 
2331                                  gnus-tmp-process)
2332   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2333          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2334          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2335          (gnus-tmp-score-char
2336           (if (or (null gnus-summary-default-score)
2337                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2338                       gnus-summary-zcore-fuzz))
2339               ? 
2340             (if (< gnus-tmp-score gnus-summary-default-score)
2341                 gnus-score-below-mark gnus-score-over-mark)))
2342          (gnus-tmp-replied
2343           (cond (gnus-tmp-process gnus-process-mark)
2344                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2345                  gnus-cached-mark)
2346                 (gnus-tmp-replied gnus-replied-mark)
2347                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2348                  gnus-saved-mark)
2349                 (t gnus-unread-mark)))
2350          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2351          (gnus-tmp-name
2352           (cond
2353            ((string-match "<[^>]+> *$" gnus-tmp-from)
2354             (let ((beg (match-beginning 0)))
2355               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2356                        (substring gnus-tmp-from (1+ (match-beginning 0))
2357                                   (1- (match-end 0))))
2358                   (substring gnus-tmp-from 0 beg))))
2359            ((string-match "(.+)" gnus-tmp-from)
2360             (substring gnus-tmp-from
2361                        (1+ (match-beginning 0)) (1- (match-end 0))))
2362            (t gnus-tmp-from)))
2363          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2364          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2365          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2366          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2367          (buffer-read-only nil))
2368     (when (string= gnus-tmp-name "")
2369       (setq gnus-tmp-name gnus-tmp-from))
2370     (unless (numberp gnus-tmp-lines)
2371       (setq gnus-tmp-lines 0))
2372     (gnus-put-text-property
2373      (point)
2374      (progn (eval gnus-summary-line-format-spec) (point))
2375      'gnus-number gnus-tmp-number)
2376     (when (gnus-visual-p 'summary-highlight 'highlight)
2377       (forward-line -1)
2378       (run-hooks 'gnus-summary-update-hook)
2379       (forward-line 1))))
2380
2381 (defun gnus-summary-update-line (&optional dont-update)
2382   ;; Update summary line after change.
2383   (when (and gnus-summary-default-score
2384              (not gnus-summary-inhibit-highlight))
2385     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2386            (article (gnus-summary-article-number))
2387            (score (gnus-summary-article-score article)))
2388       (unless dont-update
2389         (if (and gnus-summary-mark-below
2390                  (< (gnus-summary-article-score)
2391                     gnus-summary-mark-below))
2392             ;; This article has a low score, so we mark it as read.
2393             (when (memq article gnus-newsgroup-unreads)
2394               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2395           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2396             ;; This article was previously marked as read on account
2397             ;; of a low score, but now it has risen, so we mark it as
2398             ;; unread.
2399             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2400         (gnus-summary-update-mark
2401          (if (or (null gnus-summary-default-score)
2402                  (<= (abs (- score gnus-summary-default-score))
2403                      gnus-summary-zcore-fuzz))
2404              ? 
2405            (if (< score gnus-summary-default-score)
2406                gnus-score-below-mark gnus-score-over-mark))
2407          'score))
2408       ;; Do visual highlighting.
2409       (when (gnus-visual-p 'summary-highlight 'highlight)
2410         (run-hooks 'gnus-summary-update-hook)))))
2411
2412 (defvar gnus-tmp-new-adopts nil)
2413
2414 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2415   "Return the number of articles in THREAD.  
2416 This may be 0 in some cases -- if none of the articles in
2417 the thread are to be displayed."
2418   (let* ((number
2419           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2420           (cond
2421            ((not (listp thread))
2422             1)
2423            ((and (consp thread) (cdr thread))
2424             (apply
2425              '+ 1 (mapcar
2426                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2427            ((null thread)
2428             1)
2429            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2430             1)
2431            (t 0))))
2432     (when (and level (zerop level) gnus-tmp-new-adopts)
2433       (incf number
2434             (apply '+ (mapcar
2435                        'gnus-summary-number-of-articles-in-thread
2436                        gnus-tmp-new-adopts))))
2437     (if char
2438         (if (> number 1) gnus-not-empty-thread-mark
2439           gnus-empty-thread-mark)
2440       number)))
2441
2442 (defun gnus-summary-set-local-parameters (group)
2443   "Go through the local params of GROUP and set all variable specs in that list."
2444   (let ((params (gnus-group-find-parameter group))
2445         elem)
2446     (while params
2447       (setq elem (car params)
2448             params (cdr params))
2449       (and (consp elem)                 ; Has to be a cons.
2450            (consp (cdr elem))           ; The cdr has to be a list.
2451            (symbolp (car elem))         ; Has to be a symbol in there.
2452            (not (memq (car elem)
2453                       '(quit-config to-address to-list to-group)))
2454            (progn                       ; So we set it.
2455              (make-local-variable (car elem))
2456              (set (car elem) (eval (nth 1 elem))))))))
2457
2458 (defun gnus-summary-read-group (group &optional show-all no-article
2459                                       kill-buffer no-display)
2460   "Start reading news in newsgroup GROUP.
2461 If SHOW-ALL is non-nil, already read articles are also listed.
2462 If NO-ARTICLE is non-nil, no article is selected initially.
2463 If NO-DISPLAY, don't generate a summary buffer."
2464   ;; Killed foreign groups can't be entered.
2465   (when (and (not (gnus-group-native-p group))
2466              (not (gnus-gethash group gnus-newsrc-hashtb)))
2467     (error "Dead non-native groups can't be entered"))
2468   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2469   (let* ((new-group (gnus-summary-setup-buffer group))
2470          (quit-config (gnus-group-quit-config group))
2471          (did-select (and new-group (gnus-select-newsgroup group show-all))))
2472     (cond
2473      ;; This summary buffer exists already, so we just select it.
2474      ((not new-group)
2475       (gnus-set-global-variables)
2476       (when kill-buffer
2477         (gnus-kill-or-deaden-summary kill-buffer))
2478       (gnus-configure-windows 'summary 'force)
2479       (gnus-set-mode-line 'summary)
2480       (gnus-summary-position-point)
2481       (message "")
2482       t)
2483      ;; We couldn't select this group.
2484      ((null did-select)
2485       (when (and (eq major-mode 'gnus-summary-mode)
2486                  (not (equal (current-buffer) kill-buffer)))
2487         (kill-buffer (current-buffer))
2488         (if (not quit-config)
2489             (progn
2490               (set-buffer gnus-group-buffer)
2491               (gnus-group-jump-to-group group)
2492               (gnus-group-next-unread-group 1))
2493           (gnus-handle-ephemeral-exit quit-config)))
2494       (gnus-message 3 "Can't select group")
2495       nil)
2496      ;; The user did a `C-g' while prompting for number of articles,
2497      ;; so we exit this group.
2498      ((eq did-select 'quit)
2499       (and (eq major-mode 'gnus-summary-mode)
2500            (not (equal (current-buffer) kill-buffer))
2501            (kill-buffer (current-buffer)))
2502       (when kill-buffer
2503         (gnus-kill-or-deaden-summary kill-buffer))
2504       (if (not quit-config)
2505           (progn
2506             (set-buffer gnus-group-buffer)
2507             (gnus-group-jump-to-group group)
2508             (gnus-group-next-unread-group 1)
2509             (gnus-configure-windows 'group 'force))
2510         (gnus-handle-ephemeral-exit quit-config))
2511       ;; Finally signal the quit.
2512       (signal 'quit nil))
2513      ;; The group was successfully selected.
2514      (t
2515       (gnus-set-global-variables)
2516       ;; Save the active value in effect when the group was entered.
2517       (setq gnus-newsgroup-active
2518             (gnus-copy-sequence
2519              (gnus-active gnus-newsgroup-name)))
2520       ;; You can change the summary buffer in some way with this hook.
2521       (run-hooks 'gnus-select-group-hook)
2522       ;; Set any local variables in the group parameters.
2523       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2524       (gnus-update-format-specifications
2525        nil 'summary 'summary-mode 'summary-dummy)
2526       ;; Do score processing.
2527       (when gnus-use-scoring
2528         (gnus-possibly-score-headers))
2529       ;; Check whether to fill in the gaps in the threads.
2530       (when gnus-build-sparse-threads
2531         (gnus-build-sparse-threads))
2532       ;; Find the initial limit.
2533       (if gnus-show-threads
2534           (if show-all
2535               (let ((gnus-newsgroup-dormant nil))
2536                 (gnus-summary-initial-limit show-all))
2537             (gnus-summary-initial-limit show-all))
2538         (setq gnus-newsgroup-limit 
2539               (mapcar 
2540                (lambda (header) (mail-header-number header))
2541                gnus-newsgroup-headers)))
2542       ;; Generate the summary buffer.
2543       (unless no-display
2544         (gnus-summary-prepare))
2545       (when gnus-use-trees
2546         (gnus-tree-open group)
2547         (setq gnus-summary-highlight-line-function
2548               'gnus-tree-highlight-article))
2549       ;; If the summary buffer is empty, but there are some low-scored
2550       ;; articles or some excluded dormants, we include these in the
2551       ;; buffer.
2552       (when (and (zerop (buffer-size))
2553                  (not no-display))
2554         (cond (gnus-newsgroup-dormant
2555                (gnus-summary-limit-include-dormant))
2556               ((and gnus-newsgroup-scored show-all)
2557                (gnus-summary-limit-include-expunged t))))
2558       ;; Function `gnus-apply-kill-file' must be called in this hook.
2559       (run-hooks 'gnus-apply-kill-hook)
2560       (if (and (zerop (buffer-size))
2561                (not no-display))
2562           (progn
2563             ;; This newsgroup is empty.
2564             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
2565             (gnus-message 6 "No unread news")
2566             (when kill-buffer
2567               (gnus-kill-or-deaden-summary kill-buffer))
2568             ;; Return nil from this function.
2569             nil)
2570         ;; Hide conversation thread subtrees.  We cannot do this in
2571         ;; gnus-summary-prepare-hook since kill processing may not
2572         ;; work with hidden articles.
2573         (and gnus-show-threads
2574              gnus-thread-hide-subtree
2575              (gnus-summary-hide-all-threads))
2576         ;; Show first unread article if requested.
2577         (if (and (not no-article)
2578                  (not no-display)
2579                  gnus-newsgroup-unreads
2580                  gnus-auto-select-first)
2581             (unless (if (eq gnus-auto-select-first 'best)
2582                         (gnus-summary-best-unread-article)
2583                       (gnus-summary-first-unread-article))
2584               (gnus-configure-windows 'summary))
2585           ;; Don't select any articles, just move point to the first
2586           ;; article in the group.
2587           (goto-char (point-min))
2588           (gnus-summary-position-point)
2589           (gnus-set-mode-line 'summary)
2590           (gnus-configure-windows 'summary 'force))
2591         (when kill-buffer
2592           (gnus-kill-or-deaden-summary kill-buffer))
2593         (when (get-buffer-window gnus-group-buffer t)
2594           ;; Gotta use windows, because recenter does weird stuff if
2595           ;; the current buffer ain't the displayed window.
2596           (let ((owin (selected-window)))
2597             (select-window (get-buffer-window gnus-group-buffer t))
2598             (when (gnus-group-goto-group group)
2599               (recenter))
2600             (select-window owin))))
2601       ;; Mark this buffer as "prepared".
2602       (setq gnus-newsgroup-prepared t)
2603       t))))
2604
2605 (defun gnus-summary-prepare ()
2606   "Generate the summary buffer."
2607   (interactive)
2608   (let ((buffer-read-only nil))
2609     (erase-buffer)
2610     (setq gnus-newsgroup-data nil
2611           gnus-newsgroup-data-reverse nil)
2612     (run-hooks 'gnus-summary-generate-hook)
2613     ;; Generate the buffer, either with threads or without.
2614     (when gnus-newsgroup-headers
2615       (gnus-summary-prepare-threads
2616        (if gnus-show-threads
2617            (gnus-sort-gathered-threads
2618             (funcall gnus-summary-thread-gathering-function
2619                      (gnus-sort-threads
2620                       (gnus-cut-threads (gnus-make-threads)))))
2621          ;; Unthreaded display.
2622          (gnus-sort-articles gnus-newsgroup-headers))))
2623     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2624     ;; Call hooks for modifying summary buffer.
2625     (goto-char (point-min))
2626     (run-hooks 'gnus-summary-prepare-hook)))
2627
2628 (defsubst gnus-general-simplify-subject (subject)
2629   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2630   (setq subject
2631         (cond
2632          ;; Truncate the subject.
2633          ((numberp gnus-summary-gather-subject-limit)
2634           (setq subject (gnus-simplify-subject-re subject))
2635           (if (> (length subject) gnus-summary-gather-subject-limit)
2636               (substring subject 0 gnus-summary-gather-subject-limit)
2637             subject))
2638          ;; Fuzzily simplify it.
2639          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2640           (gnus-simplify-subject-fuzzy subject))
2641          ;; Just remove the leading "Re:".
2642          (t
2643           (gnus-simplify-subject-re subject))))
2644   
2645   (if (and gnus-summary-gather-exclude-subject
2646            (string-match gnus-summary-gather-exclude-subject subject))
2647       nil                               ; This article shouldn't be gathered
2648     subject))
2649
2650 (defun gnus-summary-simplify-subject-query ()
2651   "Query where the respool algorithm would put this article."
2652   (interactive)
2653   (gnus-set-global-variables)
2654   (gnus-summary-select-article)
2655   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2656
2657 (defun gnus-gather-threads-by-subject (threads)
2658   "Gather threads by looking at Subject headers."
2659   (if (not gnus-summary-make-false-root)
2660       threads
2661     (let ((hashtb (gnus-make-hashtable 1024))
2662           (prev threads)
2663           (result threads)
2664           subject hthread whole-subject)
2665       (while threads
2666         (setq subject (gnus-general-simplify-subject
2667                        (setq whole-subject (mail-header-subject 
2668                                             (caar threads)))))
2669         (when subject
2670           (if (setq hthread (gnus-gethash subject hashtb))
2671               (progn
2672                 ;; We enter a dummy root into the thread, if we
2673                 ;; haven't done that already.
2674                 (unless (stringp (caar hthread))
2675                   (setcar hthread (list whole-subject (car hthread))))
2676                 ;; We add this new gathered thread to this gathered
2677                 ;; thread.
2678                 (setcdr (car hthread)
2679                         (nconc (cdar hthread) (list (car threads))))
2680                 ;; Remove it from the list of threads.
2681                 (setcdr prev (cdr threads))
2682                 (setq threads prev))
2683             ;; Enter this thread into the hash table.
2684             (gnus-sethash subject threads hashtb)))
2685         (setq prev threads)
2686         (setq threads (cdr threads)))
2687       result)))
2688
2689 (defun gnus-gather-threads-by-references (threads)
2690   "Gather threads by looking at References headers."
2691   (let ((idhashtb (gnus-make-hashtable 1024))
2692         (thhashtb (gnus-make-hashtable 1024))
2693         (prev threads)
2694         (result threads)
2695         ids references id gthread gid entered ref)
2696     (while threads
2697       (when (setq references (mail-header-references (caar threads)))
2698         (setq id (mail-header-id (caar threads))
2699               ids (gnus-split-references references)
2700               entered nil)
2701         (while (setq ref (pop ids))
2702           (setq ids (delete ref ids))
2703           (if (not (setq gid (gnus-gethash ref idhashtb)))
2704               (progn
2705                 (gnus-sethash ref id idhashtb)
2706                 (gnus-sethash id threads thhashtb))
2707             (setq gthread (gnus-gethash gid thhashtb))
2708             (unless entered
2709               ;; We enter a dummy root into the thread, if we
2710               ;; haven't done that already.
2711               (unless (stringp (caar gthread))
2712                 (setcar gthread (list (mail-header-subject (caar gthread))
2713                                       (car gthread))))
2714               ;; We add this new gathered thread to this gathered
2715               ;; thread.
2716               (setcdr (car gthread)
2717                       (nconc (cdar gthread) (list (car threads)))))
2718             ;; Add it into the thread hash table.
2719             (gnus-sethash id gthread thhashtb)
2720             (setq entered t)
2721             ;; Remove it from the list of threads.
2722             (setcdr prev (cdr threads))
2723             (setq threads prev))))
2724       (setq prev threads)
2725       (setq threads (cdr threads)))
2726     result))
2727
2728 (defun gnus-sort-gathered-threads (threads)
2729   "Sort subtreads inside each gathered thread by article number."
2730   (let ((result threads))
2731     (while threads
2732       (when (stringp (caar threads))
2733         (setcdr (car threads)
2734                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
2735       (setq threads (cdr threads)))
2736     result))
2737
2738 (defun gnus-thread-loop-p (root thread)
2739   "Say whether ROOT is in THREAD."
2740   (let ((th (cdr thread)))
2741     (while (and th
2742                 (not (eq (caar th) root)))
2743       (pop th))
2744     (if th
2745         ;; We have found a loop.
2746         (let (ref-dep)
2747           (setcdr thread (delq (car th) (cdr thread)))
2748           (if (boundp (setq ref-dep (intern "none"
2749                                             gnus-newsgroup-dependencies)))
2750               (setcdr (symbol-value ref-dep)
2751                       (nconc (cdr (symbol-value ref-dep))
2752                              (list (car th))))
2753             (set ref-dep (list nil (car th))))
2754           1)
2755       ;; Recurse down into the sub-threads and look for loops.
2756       (apply '+
2757              (mapcar
2758               (lambda (thread) (gnus-thread-loop-p root thread))
2759               (cdr thread))))))
2760
2761 (defun gnus-make-threads ()
2762   "Go through the dependency hashtb and find the roots.  Return all threads."
2763   (let (threads)
2764     (while (catch 'infloop
2765              (mapatoms
2766               (lambda (refs)
2767                 ;; Deal with self-referencing References loops.
2768                 (when (and (car (symbol-value refs))
2769                            (not (zerop
2770                                  (apply 
2771                                   '+
2772                                   (mapcar
2773                                    (lambda (thread)
2774                                      (gnus-thread-loop-p
2775                                       (car (symbol-value refs)) thread))
2776                                    (cdr (symbol-value refs)))))))
2777                   (setq threads nil)
2778                   (throw 'infloop t))
2779                 (unless (car (symbol-value refs))
2780                   ;; These threads do not refer back to any other articles,
2781                   ;; so they're roots.
2782                   (setq threads (append (cdr (symbol-value refs)) threads))))
2783               gnus-newsgroup-dependencies)))
2784     threads))
2785
2786 (defun gnus-build-sparse-threads ()
2787   (let ((headers gnus-newsgroup-headers)
2788         (deps gnus-newsgroup-dependencies)
2789         header references generation relations 
2790         cthread subject child end pthread relation)
2791     ;; First we create an alist of generations/relations, where 
2792     ;; generations is how much we trust the relation, and the relation
2793     ;; is parent/child.
2794     (gnus-message 7 "Making sparse threads...")
2795     (save-excursion
2796       (nnheader-set-temp-buffer " *gnus sparse threads*")
2797       (while (setq header (pop headers))
2798         (when (and (setq references (mail-header-references header))
2799                    (not (string= references "")))
2800           (insert references)
2801           (setq child (mail-header-id header)
2802                 subject (mail-header-subject header))
2803           (setq generation 0)
2804           (while (search-backward ">" nil t)
2805             (setq end (1+ (point)))
2806             (when (search-backward "<" nil t)
2807               (push (list (incf generation)
2808                           child (setq child (buffer-substring (point) end))
2809                           subject)
2810                     relations)))
2811           (push (list (1+ generation) child nil subject) relations)
2812           (erase-buffer)))
2813       (kill-buffer (current-buffer)))
2814     ;; Sort over trustworthiness.
2815     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
2816     (while (setq relation (pop relations))
2817       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
2818                 (unless (car (symbol-value cthread))
2819                   ;; Make this article the parent of these threads.
2820                   (setcar (symbol-value cthread)
2821                           (vector gnus-reffed-article-number 
2822                                   (cadddr relation)
2823                                   "" ""
2824                                   (cadr relation)
2825                                   (or (caddr relation) "") 0 0 "")))
2826               (set cthread (list (vector gnus-reffed-article-number
2827                                          (cadddr relation)
2828                                          "" "" (cadr relation)
2829                                          (or (caddr relation) "") 0 0 ""))))
2830         (push gnus-reffed-article-number gnus-newsgroup-limit)
2831         (push gnus-reffed-article-number gnus-newsgroup-sparse)
2832         (push (cons gnus-reffed-article-number gnus-sparse-mark)
2833               gnus-newsgroup-reads)
2834         (decf gnus-reffed-article-number)
2835         ;; Make this new thread the child of its parent.
2836         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
2837             (setcdr (symbol-value pthread)
2838                     (nconc (cdr (symbol-value pthread))
2839                            (list (symbol-value cthread))))
2840           (set pthread (list nil (symbol-value cthread))))))
2841     (gnus-message 7 "Making sparse threads...done")))
2842
2843 (defun gnus-build-old-threads ()
2844   ;; Look at all the articles that refer back to old articles, and
2845   ;; fetch the headers for the articles that aren't there.  This will
2846   ;; build complete threads - if the roots haven't been expired by the
2847   ;; server, that is.
2848   (let (id heads)
2849     (mapatoms
2850      (lambda (refs)
2851        (when (not (car (symbol-value refs)))
2852          (setq heads (cdr (symbol-value refs)))
2853          (while heads
2854            (if (memq (mail-header-number (caar heads))
2855                      gnus-newsgroup-dormant)
2856                (setq heads (cdr heads))
2857              (setq id (symbol-name refs))
2858              (while (and (setq id (gnus-build-get-header id))
2859                          (not (car (gnus-gethash
2860                                     id gnus-newsgroup-dependencies)))))
2861              (setq heads nil)))))
2862      gnus-newsgroup-dependencies)))
2863
2864 (defun gnus-build-get-header (id)
2865   ;; Look through the buffer of NOV lines and find the header to
2866   ;; ID.  Enter this line into the dependencies hash table, and return
2867   ;; the id of the parent article (if any).
2868   (let ((deps gnus-newsgroup-dependencies)
2869         found header)
2870     (prog1
2871         (save-excursion
2872           (set-buffer nntp-server-buffer)
2873           (goto-char (point-min))
2874           (while (and (not found) (search-forward id nil t))
2875             (beginning-of-line)
2876             (setq found (looking-at
2877                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
2878                                  (regexp-quote id))))
2879             (or found (beginning-of-line 2)))
2880           (when found
2881             (beginning-of-line)
2882             (and
2883              (setq header (gnus-nov-parse-line
2884                            (read (current-buffer)) deps))
2885              (gnus-parent-id (mail-header-references header)))))
2886       (when header
2887         (let ((number (mail-header-number header)))
2888           (push number gnus-newsgroup-limit)
2889           (push header gnus-newsgroup-headers)
2890           (if (memq number gnus-newsgroup-unselected)
2891               (progn
2892                 (push number gnus-newsgroup-unreads)
2893                 (setq gnus-newsgroup-unselected
2894                       (delq number gnus-newsgroup-unselected)))
2895             (push number gnus-newsgroup-ancient)))))))
2896
2897 (defun gnus-summary-update-article-line (article header)
2898   "Update the line for ARTICLE using HEADERS."
2899   (let* ((id (mail-header-id header))
2900          (thread (gnus-id-to-thread id)))
2901     (unless thread
2902       (error "Article in no thread"))
2903     ;; Update the thread.
2904     (setcar thread header)
2905     (gnus-summary-goto-subject article)
2906     (let* ((datal (gnus-data-find-list article))
2907            (data (car datal))
2908            (length (when (cdr datal)
2909                      (- (gnus-data-pos data)
2910                         (gnus-data-pos (cadr datal)))))
2911            (buffer-read-only nil)
2912            (level (gnus-summary-thread-level)))
2913       (gnus-delete-line)
2914       (gnus-summary-insert-line
2915        header level nil (gnus-article-mark article)
2916        (memq article gnus-newsgroup-replied)
2917        (memq article gnus-newsgroup-expirable)
2918        ;; Only insert the Subject string when it's different
2919        ;; from the previous Subject string.
2920        (unless (gnus-subject-equal
2921                 (condition-case ()
2922                     (mail-header-subject
2923                      (gnus-data-header
2924                       (cadr 
2925                        (gnus-data-find-list
2926                         article
2927                         (gnus-data-list t)))))
2928                   (error ""))
2929                 (mail-header-subject header))
2930          (mail-header-subject header))
2931        nil (cdr (assq article gnus-newsgroup-scored))
2932        (memq article gnus-newsgroup-processable))
2933       (when length
2934         (gnus-data-update-list
2935          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
2936      
2937 (defun gnus-summary-update-article (article &optional iheader)
2938   "Update ARTICLE in the summary buffer."
2939   (set-buffer gnus-summary-buffer)
2940   (let* ((header (or iheader (gnus-summary-article-header article)))
2941          (id (mail-header-id header))
2942          (data (gnus-data-find article))
2943          (thread (gnus-id-to-thread id))
2944          (references (mail-header-references header))
2945          (parent
2946           (gnus-id-to-thread
2947            (or (gnus-parent-id 
2948                 (when (and references
2949                            (not (equal "" references)))
2950                   references))
2951                "none")))
2952          (buffer-read-only nil)
2953          (old (car thread))
2954          (number (mail-header-number header))
2955          pos)
2956     (when thread
2957       ;; !!! Should this be in or not?
2958       (unless iheader
2959         (setcar thread nil))
2960       (when parent
2961         (delq thread parent))
2962       (if (gnus-summary-insert-subject id header iheader)
2963           ;; Set the (possibly) new article number in the data structure.
2964           (gnus-data-set-number data (gnus-id-to-article id))
2965         (setcar thread old)
2966         nil))))
2967
2968 (defun gnus-rebuild-thread (id)
2969   "Rebuild the thread containing ID."
2970   (let ((buffer-read-only nil)
2971         old-pos current thread data)
2972     (if (not gnus-show-threads)
2973         (setq thread (list (car (gnus-id-to-thread id))))
2974       ;; Get the thread this article is part of.
2975       (setq thread (gnus-remove-thread id)))
2976     (setq old-pos (gnus-point-at-bol))
2977     (setq current (save-excursion
2978                     (and (zerop (forward-line -1))
2979                          (gnus-summary-article-number))))
2980     ;; If this is a gathered thread, we have to go some re-gathering.
2981     (when (stringp (car thread))
2982       (let ((subject (car thread))
2983             roots thr)
2984         (setq thread (cdr thread))
2985         (while thread
2986           (unless (memq (setq thr (gnus-id-to-thread
2987                                    (gnus-root-id
2988                                     (mail-header-id (caar thread)))))
2989                         roots)
2990             (push thr roots))
2991           (setq thread (cdr thread)))
2992         ;; We now have all (unique) roots.
2993         (if (= (length roots) 1)
2994             ;; All the loose roots are now one solid root.
2995             (setq thread (car roots))
2996           (setq thread (cons subject (gnus-sort-threads roots))))))
2997     (let (threads)
2998       ;; We then insert this thread into the summary buffer.
2999       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3000         (if gnus-show-threads
3001             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3002           (gnus-summary-prepare-unthreaded thread))
3003         (setq data (nreverse gnus-newsgroup-data))
3004         (setq threads gnus-newsgroup-threads))
3005       ;; We splice the new data into the data structure.
3006       (gnus-data-enter-list current data (- (point) old-pos))
3007       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
3008
3009 (defun gnus-number-to-header (number)
3010   "Return the header for article NUMBER."
3011   (let ((headers gnus-newsgroup-headers))
3012     (while (and headers
3013                 (not (= number (mail-header-number (car headers)))))
3014       (pop headers))
3015     (when headers
3016       (car headers))))
3017
3018 (defun gnus-parent-headers (headers &optional generation)
3019   "Return the headers of the GENERATIONeth parent of HEADERS."
3020   (unless generation 
3021     (setq generation 1))
3022   (let (references parent)
3023     (while (and headers (not (zerop generation)))
3024       (setq references (mail-header-references headers))
3025       (when (and references
3026                  (setq parent (gnus-parent-id references))
3027                  (setq headers (car (gnus-id-to-thread parent))))
3028         (decf generation)))
3029     headers))
3030
3031 (defun gnus-id-to-thread (id)
3032   "Return the (sub-)thread where ID appears."
3033   (gnus-gethash id gnus-newsgroup-dependencies))
3034
3035 (defun gnus-id-to-article (id)
3036   "Return the article number of ID."
3037   (let ((thread (gnus-id-to-thread id)))
3038     (when (and thread
3039                (car thread))
3040       (mail-header-number (car thread)))))
3041
3042 (defun gnus-id-to-header (id)
3043   "Return the article headers of ID."
3044   (car (gnus-id-to-thread id)))
3045
3046 (defun gnus-article-displayed-root-p (article)
3047   "Say whether ARTICLE is a root(ish) article."
3048   (let ((level (gnus-summary-thread-level article))
3049         (refs (mail-header-references  (gnus-summary-article-header article)))
3050         particle)
3051     (cond 
3052      ((null level) nil)
3053      ((zerop level) t)
3054      ((null refs) t)
3055      ((null (gnus-parent-id refs)) t)
3056      ((and (= 1 level)
3057            (null (setq particle (gnus-id-to-article
3058                                  (gnus-parent-id refs))))
3059            (null (gnus-summary-thread-level particle)))))))
3060
3061 (defun gnus-root-id (id)
3062   "Return the id of the root of the thread where ID appears."
3063   (let (last-id prev)
3064     (while (and id (setq prev (car (gnus-gethash 
3065                                     id gnus-newsgroup-dependencies))))
3066       (setq last-id id
3067             id (gnus-parent-id (mail-header-references prev))))
3068     last-id))
3069
3070 (defun gnus-remove-thread (id &optional dont-remove)
3071   "Remove the thread that has ID in it."
3072   (let ((dep gnus-newsgroup-dependencies)
3073         headers thread last-id)
3074     ;; First go up in this thread until we find the root.
3075     (setq last-id (gnus-root-id id))
3076     (setq headers (list (car (gnus-id-to-thread last-id))
3077                         (caadr (gnus-id-to-thread last-id))))
3078     ;; We have now found the real root of this thread.  It might have
3079     ;; been gathered into some loose thread, so we have to search
3080     ;; through the threads to find the thread we wanted.
3081     (let ((threads gnus-newsgroup-threads)
3082           sub)
3083       (while threads
3084         (setq sub (car threads))
3085         (if (stringp (car sub))
3086             ;; This is a gathered thread, so we look at the roots
3087             ;; below it to find whether this article is in this
3088             ;; gathered root.
3089             (progn
3090               (setq sub (cdr sub))
3091               (while sub
3092                 (when (member (caar sub) headers)
3093                   (setq thread (car threads)
3094                         threads nil
3095                         sub nil))
3096                 (setq sub (cdr sub))))
3097           ;; It's an ordinary thread, so we check it.
3098           (when (eq (car sub) (car headers))
3099             (setq thread sub
3100                   threads nil)))
3101         (setq threads (cdr threads)))
3102       ;; If this article is in no thread, then it's a root.
3103       (if thread
3104           (unless dont-remove
3105             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3106         (setq thread (gnus-gethash last-id dep)))
3107       (when thread
3108         (prog1
3109             thread                      ; We return this thread.
3110           (unless dont-remove
3111             (if (stringp (car thread))
3112                 (progn
3113                   ;; If we use dummy roots, then we have to remove the
3114                   ;; dummy root as well.
3115                   (when (eq gnus-summary-make-false-root 'dummy)
3116                     (gnus-delete-line)
3117                     (gnus-data-compute-positions))
3118                   (setq thread (cdr thread))
3119                   (while thread
3120                     (gnus-remove-thread-1 (car thread))
3121                     (setq thread (cdr thread))))
3122               (gnus-remove-thread-1 thread))))))))
3123
3124 (defun gnus-remove-thread-1 (thread)
3125   "Remove the thread THREAD recursively."
3126   (let ((number (mail-header-number (pop thread)))
3127         d)
3128     (setq thread (reverse thread))
3129     (while thread
3130       (gnus-remove-thread-1 (pop thread)))
3131     (when (setq d (gnus-data-find number))
3132       (goto-char (gnus-data-pos d))
3133       (gnus-data-remove 
3134        number
3135        (- (gnus-point-at-bol)
3136           (prog1
3137               (1+ (gnus-point-at-eol))
3138             (gnus-delete-line)))))))
3139
3140 (defun gnus-sort-threads (threads)
3141   "Sort THREADS."
3142   (if (not gnus-thread-sort-functions)
3143       threads
3144     (gnus-message 7 "Sorting threads...")
3145     (prog1
3146         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3147       (gnus-message 7 "Sorting threads...done"))))
3148
3149 (defun gnus-sort-articles (articles)
3150   "Sort ARTICLES."
3151   (when gnus-article-sort-functions
3152     (gnus-message 7 "Sorting articles...")
3153     (prog1
3154         (setq gnus-newsgroup-headers
3155               (sort articles (gnus-make-sort-function 
3156                               gnus-article-sort-functions)))
3157       (gnus-message 7 "Sorting articles...done"))))
3158
3159 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3160 (defmacro gnus-thread-header (thread)
3161   ;; Return header of first article in THREAD.
3162   ;; Note that THREAD must never, ever be anything else than a variable -
3163   ;; using some other form will lead to serious barfage.
3164   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3165   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3166   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3167         (vector thread) 2))
3168
3169 (defsubst gnus-article-sort-by-number (h1 h2)
3170   "Sort articles by article number."
3171   (< (mail-header-number h1)
3172      (mail-header-number h2)))
3173
3174 (defun gnus-thread-sort-by-number (h1 h2)
3175   "Sort threads by root article number."
3176   (gnus-article-sort-by-number
3177    (gnus-thread-header h1) (gnus-thread-header h2)))
3178
3179 (defsubst gnus-article-sort-by-lines (h1 h2)
3180   "Sort articles by article Lines header."
3181   (< (mail-header-lines h1)
3182      (mail-header-lines h2)))
3183
3184 (defun gnus-thread-sort-by-lines (h1 h2)
3185   "Sort threads by root article Lines header."
3186   (gnus-article-sort-by-lines
3187    (gnus-thread-header h1) (gnus-thread-header h2)))
3188
3189 (defsubst gnus-article-sort-by-author (h1 h2)
3190   "Sort articles by root author."
3191   (string-lessp
3192    (let ((extract (funcall
3193                    gnus-extract-address-components
3194                    (mail-header-from h1))))
3195      (or (car extract) (cdr extract)))
3196    (let ((extract (funcall
3197                    gnus-extract-address-components
3198                    (mail-header-from h2))))
3199      (or (car extract) (cdr extract)))))
3200
3201 (defun gnus-thread-sort-by-author (h1 h2)
3202   "Sort threads by root author."
3203   (gnus-article-sort-by-author
3204    (gnus-thread-header h1)  (gnus-thread-header h2)))
3205
3206 (defsubst gnus-article-sort-by-subject (h1 h2)
3207   "Sort articles by root subject."
3208   (string-lessp
3209    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3210    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3211
3212 (defun gnus-thread-sort-by-subject (h1 h2)
3213   "Sort threads by root subject."
3214   (gnus-article-sort-by-subject
3215    (gnus-thread-header h1) (gnus-thread-header h2)))
3216
3217 (defsubst gnus-article-sort-by-date (h1 h2)
3218   "Sort articles by root article date."
3219   (gnus-time-less
3220    (gnus-date-get-time (mail-header-date h1))
3221    (gnus-date-get-time (mail-header-date h2))))
3222
3223 (defun gnus-thread-sort-by-date (h1 h2)
3224   "Sort threads by root article date."
3225   (gnus-article-sort-by-date
3226    (gnus-thread-header h1) (gnus-thread-header h2)))
3227
3228 (defsubst gnus-article-sort-by-score (h1 h2)
3229   "Sort articles by root article score.
3230 Unscored articles will be counted as having a score of zero."
3231   (> (or (cdr (assq (mail-header-number h1)
3232                     gnus-newsgroup-scored))
3233          gnus-summary-default-score 0)
3234      (or (cdr (assq (mail-header-number h2)
3235                     gnus-newsgroup-scored))
3236          gnus-summary-default-score 0)))
3237
3238 (defun gnus-thread-sort-by-score (h1 h2)
3239   "Sort threads by root article score."
3240   (gnus-article-sort-by-score
3241    (gnus-thread-header h1) (gnus-thread-header h2)))
3242
3243 (defun gnus-thread-sort-by-total-score (h1 h2)
3244   "Sort threads by the sum of all scores in the thread.
3245 Unscored articles will be counted as having a score of zero."
3246   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3247
3248 (defun gnus-thread-total-score (thread)
3249   ;;  This function find the total score of THREAD.
3250   (cond ((null thread)
3251          0)
3252         ((consp thread)
3253          (if (stringp (car thread))
3254              (apply gnus-thread-score-function 0
3255                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3256            (gnus-thread-total-score-1 thread)))
3257         (t
3258          (gnus-thread-total-score-1 (list thread)))))
3259
3260 (defun gnus-thread-total-score-1 (root)
3261   ;; This function find the total score of the thread below ROOT.
3262   (setq root (car root))
3263   (apply gnus-thread-score-function
3264          (or (append
3265               (mapcar 'gnus-thread-total-score
3266                       (cdr (gnus-gethash (mail-header-id root)
3267                                          gnus-newsgroup-dependencies)))
3268               (when (> (mail-header-number root) 0)
3269                 (list (or (cdr (assq (mail-header-number root)
3270                                      gnus-newsgroup-scored))
3271                           gnus-summary-default-score 0))))
3272              (list gnus-summary-default-score)
3273              '(0))))
3274
3275 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3276 (defvar gnus-tmp-prev-subject nil)
3277 (defvar gnus-tmp-false-parent nil)
3278 (defvar gnus-tmp-root-expunged nil)
3279 (defvar gnus-tmp-dummy-line nil)
3280
3281 (defun gnus-summary-prepare-threads (threads)
3282   "Prepare summary buffer from THREADS and indentation LEVEL.
3283 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3284 or a straight list of headers."
3285   (gnus-message 7 "Generating summary...")
3286
3287   (setq gnus-newsgroup-threads threads)
3288   (beginning-of-line)
3289
3290   (let ((gnus-tmp-level 0)
3291         (default-score (or gnus-summary-default-score 0))
3292         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3293         thread number subject stack state gnus-tmp-gathered beg-match
3294         new-roots gnus-tmp-new-adopts thread-end
3295         gnus-tmp-header gnus-tmp-unread
3296         gnus-tmp-replied gnus-tmp-subject-or-nil
3297         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3298         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3299         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3300
3301     (setq gnus-tmp-prev-subject nil)
3302
3303     (if (vectorp (car threads))
3304         ;; If this is a straight (sic) list of headers, then a
3305         ;; threaded summary display isn't required, so we just create
3306         ;; an unthreaded one.
3307         (gnus-summary-prepare-unthreaded threads)
3308
3309       ;; Do the threaded display.
3310
3311       (while (or threads stack gnus-tmp-new-adopts new-roots)
3312
3313         (if (and (= gnus-tmp-level 0)
3314                  (not (setq gnus-tmp-dummy-line nil))
3315                  (or (not stack)
3316                      (= (caar stack) 0))
3317                  (not gnus-tmp-false-parent)
3318                  (or gnus-tmp-new-adopts new-roots))
3319             (if gnus-tmp-new-adopts
3320                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3321                       thread (list (car gnus-tmp-new-adopts))
3322                       gnus-tmp-header (caar thread)
3323                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3324               (when new-roots
3325                 (setq thread (list (car new-roots))
3326                       gnus-tmp-header (caar thread)
3327                       new-roots (cdr new-roots))))
3328
3329           (if threads
3330               ;; If there are some threads, we do them before the
3331               ;; threads on the stack.
3332               (setq thread threads
3333                     gnus-tmp-header (caar thread))
3334             ;; There were no current threads, so we pop something off
3335             ;; the stack.
3336             (setq state (car stack)
3337                   gnus-tmp-level (car state)
3338                   thread (cdr state)
3339                   stack (cdr stack)
3340                   gnus-tmp-header (caar thread))))
3341
3342         (setq gnus-tmp-false-parent nil)
3343         (setq gnus-tmp-root-expunged nil)
3344         (setq thread-end nil)
3345
3346         (if (stringp gnus-tmp-header)
3347             ;; The header is a dummy root.
3348             (cond
3349              ((eq gnus-summary-make-false-root 'adopt)
3350               ;; We let the first article adopt the rest.
3351               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3352                                                (cddar thread)))
3353               (setq gnus-tmp-gathered
3354                     (nconc (mapcar
3355                             (lambda (h) (mail-header-number (car h)))
3356                             (cddar thread))
3357                            gnus-tmp-gathered))
3358               (setq thread (cons (list (caar thread)
3359                                        (cadar thread))
3360                                  (cdr thread)))
3361               (setq gnus-tmp-level -1
3362                     gnus-tmp-false-parent t))
3363              ((eq gnus-summary-make-false-root 'empty)
3364               ;; We print adopted articles with empty subject fields.
3365               (setq gnus-tmp-gathered
3366                     (nconc (mapcar
3367                             (lambda (h) (mail-header-number (car h)))
3368                             (cddar thread))
3369                            gnus-tmp-gathered))
3370               (setq gnus-tmp-level -1))
3371              ((eq gnus-summary-make-false-root 'dummy)
3372               ;; We remember that we probably want to output a dummy
3373               ;; root.
3374               (setq gnus-tmp-dummy-line gnus-tmp-header)
3375               (setq gnus-tmp-prev-subject gnus-tmp-header))
3376              (t
3377               ;; We do not make a root for the gathered
3378               ;; sub-threads at all.
3379               (setq gnus-tmp-level -1)))
3380
3381           (setq number (mail-header-number gnus-tmp-header)
3382                 subject (mail-header-subject gnus-tmp-header))
3383
3384           (cond
3385            ;; If the thread has changed subject, we might want to make
3386            ;; this subthread into a root.
3387            ((and (null gnus-thread-ignore-subject)
3388                  (not (zerop gnus-tmp-level))
3389                  gnus-tmp-prev-subject
3390                  (not (inline
3391                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3392             (setq new-roots (nconc new-roots (list (car thread)))
3393                   thread-end t
3394                   gnus-tmp-header nil))
3395            ;; If the article lies outside the current limit,
3396            ;; then we do not display it.
3397            ((not (memq number gnus-newsgroup-limit))
3398             (setq gnus-tmp-gathered
3399                   (nconc (mapcar
3400                           (lambda (h) (mail-header-number (car h)))
3401                           (cdar thread))
3402                          gnus-tmp-gathered))
3403             (setq gnus-tmp-new-adopts (if (cdar thread)
3404                                           (append gnus-tmp-new-adopts
3405                                                   (cdar thread))
3406                                         gnus-tmp-new-adopts)
3407                   thread-end t
3408                   gnus-tmp-header nil)
3409             (when (zerop gnus-tmp-level)
3410               (setq gnus-tmp-root-expunged t)))
3411            ;; Perhaps this article is to be marked as read?
3412            ((and gnus-summary-mark-below
3413                  (< (or (cdr (assq number gnus-newsgroup-scored))
3414                         default-score)
3415                     gnus-summary-mark-below)
3416                  ;; Don't touch sparse articles.
3417                  (not (gnus-summary-article-sparse-p number))
3418                  (not (gnus-summary-article-ancient-p number)))
3419             (setq gnus-newsgroup-unreads
3420                   (delq number gnus-newsgroup-unreads))
3421             (if gnus-newsgroup-auto-expire
3422                 (push number gnus-newsgroup-expirable)
3423               (push (cons number gnus-low-score-mark)
3424                     gnus-newsgroup-reads))))
3425
3426           (when gnus-tmp-header
3427             ;; We may have an old dummy line to output before this
3428             ;; article.
3429             (when gnus-tmp-dummy-line
3430               (gnus-summary-insert-dummy-line
3431                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3432               (setq gnus-tmp-dummy-line nil))
3433
3434             ;; Compute the mark.
3435             (setq gnus-tmp-unread (gnus-article-mark number))
3436
3437             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3438                                   gnus-tmp-header gnus-tmp-level)
3439                   gnus-newsgroup-data)
3440
3441             ;; Actually insert the line.
3442             (setq
3443              gnus-tmp-subject-or-nil
3444              (cond
3445               ((and gnus-thread-ignore-subject
3446                     gnus-tmp-prev-subject
3447                     (not (inline (gnus-subject-equal
3448                                   gnus-tmp-prev-subject subject))))
3449                subject)
3450               ((zerop gnus-tmp-level)
3451                (if (and (eq gnus-summary-make-false-root 'empty)
3452                         (memq number gnus-tmp-gathered)
3453                         gnus-tmp-prev-subject
3454                         (inline (gnus-subject-equal
3455                                  gnus-tmp-prev-subject subject)))
3456                    gnus-summary-same-subject
3457                  subject))
3458               (t gnus-summary-same-subject)))
3459             (if (and (eq gnus-summary-make-false-root 'adopt)
3460                      (= gnus-tmp-level 1)
3461                      (memq number gnus-tmp-gathered))
3462                 (setq gnus-tmp-opening-bracket ?\<
3463                       gnus-tmp-closing-bracket ?\>)
3464               (setq gnus-tmp-opening-bracket ?\[
3465                     gnus-tmp-closing-bracket ?\]))
3466             (setq
3467              gnus-tmp-indentation
3468              (aref gnus-thread-indent-array gnus-tmp-level)
3469              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3470              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3471                                 gnus-summary-default-score 0)
3472              gnus-tmp-score-char
3473              (if (or (null gnus-summary-default-score)
3474                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3475                          gnus-summary-zcore-fuzz))
3476                  ? 
3477                (if (< gnus-tmp-score gnus-summary-default-score)
3478                    gnus-score-below-mark gnus-score-over-mark))
3479              gnus-tmp-replied
3480              (cond ((memq number gnus-newsgroup-processable)
3481                     gnus-process-mark)
3482                    ((memq number gnus-newsgroup-cached)
3483                     gnus-cached-mark)
3484                    ((memq number gnus-newsgroup-replied)
3485                     gnus-replied-mark)
3486                    ((memq number gnus-newsgroup-saved)
3487                     gnus-saved-mark)
3488                    (t gnus-unread-mark))
3489              gnus-tmp-from (mail-header-from gnus-tmp-header)
3490              gnus-tmp-name
3491              (cond
3492               ((string-match "<[^>]+> *$" gnus-tmp-from)
3493                (setq beg-match (match-beginning 0))
3494                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3495                         (substring gnus-tmp-from (1+ (match-beginning 0))
3496                                    (1- (match-end 0))))
3497                    (substring gnus-tmp-from 0 beg-match)))
3498               ((string-match "(.+)" gnus-tmp-from)
3499                (substring gnus-tmp-from
3500                           (1+ (match-beginning 0)) (1- (match-end 0))))
3501               (t gnus-tmp-from)))
3502             (when (string= gnus-tmp-name "")
3503               (setq gnus-tmp-name gnus-tmp-from))
3504             (unless (numberp gnus-tmp-lines)
3505               (setq gnus-tmp-lines 0))
3506             (gnus-put-text-property
3507              (point)
3508              (progn (eval gnus-summary-line-format-spec) (point))
3509              'gnus-number number)
3510             (when gnus-visual-p
3511               (forward-line -1)
3512               (run-hooks 'gnus-summary-update-hook)
3513               (forward-line 1))
3514
3515             (setq gnus-tmp-prev-subject subject)))
3516
3517         (when (nth 1 thread)
3518           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3519         (incf gnus-tmp-level)
3520         (setq threads (if thread-end nil (cdar thread)))
3521         (unless threads
3522           (setq gnus-tmp-level 0)))))
3523   (gnus-message 7 "Generating summary...done"))
3524
3525 (defun gnus-summary-prepare-unthreaded (headers)
3526   "Generate an unthreaded summary buffer based on HEADERS."
3527   (let (header number mark)
3528
3529     (beginning-of-line)
3530
3531     (while headers
3532       ;; We may have to root out some bad articles...
3533       (when (memq (setq number (mail-header-number
3534                                 (setq header (pop headers))))
3535                   gnus-newsgroup-limit)
3536         ;; Mark article as read when it has a low score.
3537         (when (and gnus-summary-mark-below
3538                    (< (or (cdr (assq number gnus-newsgroup-scored))
3539                           gnus-summary-default-score 0)
3540                       gnus-summary-mark-below)
3541                    (not (gnus-summary-article-ancient-p number)))
3542           (setq gnus-newsgroup-unreads
3543                 (delq number gnus-newsgroup-unreads))
3544           (if gnus-newsgroup-auto-expire
3545               (push number gnus-newsgroup-expirable)
3546             (push (cons number gnus-low-score-mark)
3547                   gnus-newsgroup-reads)))
3548
3549         (setq mark (gnus-article-mark number))
3550         (push (gnus-data-make number mark (1+ (point)) header 0)
3551               gnus-newsgroup-data)
3552         (gnus-summary-insert-line
3553          header 0 number
3554          mark (memq number gnus-newsgroup-replied)
3555          (memq number gnus-newsgroup-expirable)
3556          (mail-header-subject header) nil
3557          (cdr (assq number gnus-newsgroup-scored))
3558          (memq number gnus-newsgroup-processable))))))
3559
3560 (defun gnus-select-newsgroup (group &optional read-all)
3561   "Select newsgroup GROUP.
3562 If READ-ALL is non-nil, all articles in the group are selected."
3563   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3564          (info (nth 2 entry))
3565          articles fetched-articles cached)
3566
3567     (unless (gnus-check-server
3568              (setq gnus-current-select-method 
3569                    (gnus-find-method-for-group group)))
3570       (error "Couldn't open server"))
3571
3572     (or (and entry (not (eq (car entry) t))) ; Either it's active...
3573         (gnus-activate-group group)     ; Or we can activate it...
3574         (progn                          ; Or we bug out.
3575           (when (equal major-mode 'gnus-summary-mode)
3576             (kill-buffer (current-buffer)))
3577           (error "Couldn't request group %s: %s"
3578                  group (gnus-status-message group))))
3579
3580     (unless (gnus-request-group group t)
3581       (when (equal major-mode 'gnus-summary-mode)
3582         (kill-buffer (current-buffer)))
3583       (error "Couldn't request group %s: %s"
3584              group (gnus-status-message group)))
3585
3586     (setq gnus-newsgroup-name group)
3587     (setq gnus-newsgroup-unselected nil)
3588     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
3589
3590     ;; Adjust and set lists of article marks.
3591     (when info
3592       (gnus-adjust-marked-articles info))
3593
3594     ;; Kludge to avoid having cached articles nixed out in virtual groups.
3595     (when (gnus-virtual-group-p group)
3596       (setq cached gnus-newsgroup-cached))
3597
3598     (setq gnus-newsgroup-unreads
3599           (gnus-set-difference
3600            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
3601            gnus-newsgroup-dormant))
3602
3603     (setq gnus-newsgroup-processable nil)
3604
3605     (gnus-update-read-articles group gnus-newsgroup-unreads)
3606     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
3607       (gnus-group-update-group group))
3608  
3609     (setq articles (gnus-articles-to-read group read-all))
3610
3611     (cond
3612      ((null articles)
3613       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
3614       'quit)
3615      ((eq articles 0) nil)
3616      (t
3617       ;; Init the dependencies hash table.
3618       (setq gnus-newsgroup-dependencies
3619             (gnus-make-hashtable (length articles)))
3620       ;; Retrieve the headers and read them in.
3621       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
3622       (setq gnus-newsgroup-headers
3623             (if (eq 'nov
3624                     (setq gnus-headers-retrieved-by
3625                           (gnus-retrieve-headers
3626                            articles gnus-newsgroup-name
3627                            ;; We might want to fetch old headers, but
3628                            ;; not if there is only 1 article.
3629                            (and gnus-fetch-old-headers
3630                                 (or (and
3631                                      (not (eq gnus-fetch-old-headers 'some))
3632                                      (not (numberp gnus-fetch-old-headers)))
3633                                     (> (length articles) 1))))))
3634                 (gnus-get-newsgroup-headers-xover 
3635                  articles nil nil gnus-newsgroup-name t)
3636               (gnus-get-newsgroup-headers)))
3637       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
3638
3639       ;; Kludge to avoid having cached articles nixed out in virtual groups.
3640       (when cached
3641         (setq gnus-newsgroup-cached cached))
3642
3643       ;; Suppress duplicates?
3644       (when gnus-suppress-duplicates
3645         (gnus-dup-suppress-articles))
3646
3647       ;; Set the initial limit.
3648       (setq gnus-newsgroup-limit (copy-sequence articles))
3649       ;; Remove canceled articles from the list of unread articles.
3650       (setq gnus-newsgroup-unreads
3651             (gnus-set-sorted-intersection
3652              gnus-newsgroup-unreads
3653              (setq fetched-articles
3654                    (mapcar (lambda (headers) (mail-header-number headers))
3655                            gnus-newsgroup-headers))))
3656       ;; Removed marked articles that do not exist.
3657       (gnus-update-missing-marks
3658        (gnus-sorted-complement fetched-articles articles))
3659       ;; We might want to build some more threads first.
3660       (and gnus-fetch-old-headers
3661            (eq gnus-headers-retrieved-by 'nov)
3662            (gnus-build-old-threads))
3663       ;; Check whether auto-expire is to be done in this group.
3664       (setq gnus-newsgroup-auto-expire
3665             (gnus-group-auto-expirable-p group))
3666       ;; Set up the article buffer now, if necessary.
3667       (unless gnus-single-article-buffer
3668         (gnus-article-setup-buffer))
3669       ;; First and last article in this newsgroup.
3670       (when gnus-newsgroup-headers
3671         (setq gnus-newsgroup-begin
3672               (mail-header-number (car gnus-newsgroup-headers))
3673               gnus-newsgroup-end
3674               (mail-header-number
3675                (gnus-last-element gnus-newsgroup-headers))))
3676       ;; GROUP is successfully selected.
3677       (or gnus-newsgroup-headers t)))))
3678
3679 (defun gnus-articles-to-read (group &optional read-all)
3680   ;; Find out what articles the user wants to read.
3681   (let* ((articles
3682           ;; Select all articles if `read-all' is non-nil, or if there
3683           ;; are no unread articles.
3684           (if (or read-all
3685                   (and (zerop (length gnus-newsgroup-marked))
3686                        (zerop (length gnus-newsgroup-unreads)))
3687                   (eq (gnus-group-find-parameter group 'display)
3688                       'all))
3689               (gnus-uncompress-range (gnus-active group))
3690             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
3691                           (copy-sequence gnus-newsgroup-unreads))
3692                   '<)))
3693          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
3694          (scored (length scored-list))
3695          (number (length articles))
3696          (marked (+ (length gnus-newsgroup-marked)
3697                     (length gnus-newsgroup-dormant)))
3698          (select
3699           (cond
3700            ((numberp read-all)
3701             read-all)
3702            (t
3703             (condition-case ()
3704                 (cond
3705                  ((and (or (<= scored marked) (= scored number))
3706                        (numberp gnus-large-newsgroup)
3707                        (> number gnus-large-newsgroup))
3708                   (let ((input
3709                          (read-string
3710                           (format
3711                            "How many articles from %s (default %d): "
3712                            (gnus-limit-string gnus-newsgroup-name 35)
3713                            number))))
3714                     (if (string-match "^[ \t]*$" input) number input)))
3715                  ((and (> scored marked) (< scored number)
3716                        (> (- scored number) 20))
3717                   (let ((input
3718                          (read-string
3719                           (format "%s %s (%d scored, %d total): "
3720                                   "How many articles from"
3721                                   group scored number))))
3722                     (if (string-match "^[ \t]*$" input)
3723                         number input)))
3724                  (t number))
3725               (quit nil))))))
3726     (setq select (if (stringp select) (string-to-number select) select))
3727     (if (or (null select) (zerop select))
3728         select
3729       (if (and (not (zerop scored)) (<= (abs select) scored))
3730           (progn
3731             (setq articles (sort scored-list '<))
3732             (setq number (length articles)))
3733         (setq articles (copy-sequence articles)))
3734
3735       (when (< (abs select) number)
3736         (if (< select 0)
3737             ;; Select the N oldest articles.
3738             (setcdr (nthcdr (1- (abs select)) articles) nil)
3739           ;; Select the N most recent articles.
3740           (setq articles (nthcdr (- number select) articles))))
3741       (setq gnus-newsgroup-unselected
3742             (gnus-sorted-intersection
3743              gnus-newsgroup-unreads
3744              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
3745       articles)))
3746
3747 (defun gnus-killed-articles (killed articles)
3748   (let (out)
3749     (while articles
3750       (when (inline (gnus-member-of-range (car articles) killed))
3751         (push (car articles) out))
3752       (setq articles (cdr articles)))
3753     out))
3754
3755 (defun gnus-uncompress-marks (marks)
3756   "Uncompress the mark ranges in MARKS."
3757   (let ((uncompressed '(score bookmark))
3758         out)
3759     (while marks
3760       (if (memq (caar marks) uncompressed)
3761           (push (car marks) out)
3762         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
3763       (setq marks (cdr marks)))
3764     out))
3765
3766 (defun gnus-adjust-marked-articles (info)
3767   "Set all article lists and remove all marks that are no longer legal."
3768   (let* ((marked-lists (gnus-info-marks info))
3769          (active (gnus-active (gnus-info-group info)))
3770          (min (car active))
3771          (max (cdr active))
3772          (types gnus-article-mark-lists)
3773          (uncompressed '(score bookmark killed))
3774          marks var articles article mark)
3775
3776     (while marked-lists
3777       (setq marks (pop marked-lists))
3778       (set (setq var (intern (format "gnus-newsgroup-%s"
3779                                      (car (rassq (setq mark (car marks))
3780                                                  types)))))
3781            (if (memq (car marks) uncompressed) (cdr marks)
3782              (gnus-uncompress-range (cdr marks))))
3783
3784       (setq articles (symbol-value var))
3785
3786       ;; All articles have to be subsets of the active articles.
3787       (cond
3788        ;; Adjust "simple" lists.
3789        ((memq mark '(tick dormant expirable reply save))
3790         (while articles
3791           (when (or (< (setq article (pop articles)) min) (> article max))
3792             (set var (delq article (symbol-value var))))))
3793        ;; Adjust assocs.
3794        ((memq mark uncompressed)
3795         (while articles
3796           (when (or (not (consp (setq article (pop articles))))
3797                     (< (car article) min)
3798                     (> (car article) max))
3799             (set var (delq article (symbol-value var))))))))))
3800
3801 (defun gnus-update-missing-marks (missing)
3802   "Go through the list of MISSING articles and remove them mark lists."
3803   (when missing
3804     (let ((types gnus-article-mark-lists)
3805           var m)
3806       ;; Go through all types.
3807       (while types
3808         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
3809         (when (symbol-value var)
3810           ;; This list has articles.  So we delete all missing articles
3811           ;; from it.
3812           (setq m missing)
3813           (while m
3814             (set var (delq (pop m) (symbol-value var)))))))))
3815
3816 (defun gnus-update-marks ()
3817   "Enter the various lists of marked articles into the newsgroup info list."
3818   (let ((types gnus-article-mark-lists)
3819         (info (gnus-get-info gnus-newsgroup-name))
3820         (uncompressed '(score bookmark killed))
3821         type list newmarked symbol)
3822     (when info
3823       ;; Add all marks lists that are non-nil to the list of marks lists.
3824       (while types
3825         (setq type (pop types))
3826         (when (setq list (symbol-value
3827                           (setq symbol
3828                                 (intern (format "gnus-newsgroup-%s"
3829                                                 (car type))))))
3830           (push (cons (cdr type)
3831                       (if (memq (cdr type) uncompressed) list
3832                         (gnus-compress-sequence 
3833                          (set symbol (sort list '<)) t)))
3834                 newmarked)))
3835
3836       ;; Enter these new marks into the info of the group.
3837       (if (nthcdr 3 info)
3838           (setcar (nthcdr 3 info) newmarked)
3839         ;; Add the marks lists to the end of the info.
3840         (when newmarked
3841           (setcdr (nthcdr 2 info) (list newmarked))))
3842
3843       ;; Cut off the end of the info if there's nothing else there.
3844       (let ((i 5))
3845         (while (and (> i 2)
3846                     (not (nth i info)))
3847           (when (nthcdr (decf i) info)
3848             (setcdr (nthcdr i info) nil)))))))
3849
3850 (defun gnus-set-mode-line (where)
3851   "This function sets the mode line of the article or summary buffers.
3852 If WHERE is `summary', the summary mode line format will be used."
3853   ;; Is this mode line one we keep updated?
3854   (when (memq where gnus-updated-mode-lines)
3855     (let (mode-string)
3856       (save-excursion
3857         ;; We evaluate this in the summary buffer since these
3858         ;; variables are buffer-local to that buffer.
3859         (set-buffer gnus-summary-buffer)
3860         ;; We bind all these variables that are used in the `eval' form
3861         ;; below.
3862         (let* ((mformat (symbol-value
3863                          (intern
3864                           (format "gnus-%s-mode-line-format-spec" where))))
3865                (gnus-tmp-group-name gnus-newsgroup-name)
3866                (gnus-tmp-article-number (or gnus-current-article 0))
3867                (gnus-tmp-unread gnus-newsgroup-unreads)
3868                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
3869                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
3870                (gnus-tmp-unread-and-unselected
3871                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
3872                             (zerop gnus-tmp-unselected))
3873                        "")
3874                       ((zerop gnus-tmp-unselected)
3875                        (format "{%d more}" gnus-tmp-unread-and-unticked))
3876                       (t (format "{%d(+%d) more}"
3877                                  gnus-tmp-unread-and-unticked
3878                                  gnus-tmp-unselected))))
3879                (gnus-tmp-subject
3880                 (if (and gnus-current-headers
3881                          (vectorp gnus-current-headers))
3882                     (gnus-mode-string-quote
3883                      (mail-header-subject gnus-current-headers))
3884                   ""))
3885                bufname-length max-len
3886                gnus-tmp-header);; passed as argument to any user-format-funcs
3887           (setq mode-string (eval mformat))
3888           (setq bufname-length (if (string-match "%b" mode-string)
3889                                    (- (length
3890                                        (buffer-name
3891                                         (if (eq where 'summary)
3892                                             nil
3893                                           (get-buffer gnus-article-buffer))))
3894                                       2)
3895                                  0))
3896           (setq max-len (max 4 (if gnus-mode-non-string-length
3897                                    (- (window-width)
3898                                       gnus-mode-non-string-length
3899                                       bufname-length)
3900                                  (length mode-string))))
3901           ;; We might have to chop a bit of the string off...
3902           (when (> (length mode-string) max-len)
3903             (setq mode-string
3904                   (concat (gnus-truncate-string mode-string (- max-len 3))
3905                           "...")))
3906           ;; Pad the mode string a bit.
3907           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
3908       ;; Update the mode line.
3909       (setq mode-line-buffer-identification 
3910             (gnus-mode-line-buffer-identification (list mode-string)))
3911       (set-buffer-modified-p t))))
3912
3913 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
3914   "Go through the HEADERS list and add all Xrefs to a hash table.
3915 The resulting hash table is returned, or nil if no Xrefs were found."
3916   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
3917          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
3918          (xref-hashtb (gnus-make-hashtable))
3919          start group entry number xrefs header)
3920     (while headers
3921       (setq header (pop headers))
3922       (when (and (setq xrefs (mail-header-xref header))
3923                  (not (memq (setq number (mail-header-number header))
3924                             unreads)))
3925         (setq start 0)
3926         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
3927           (setq start (match-end 0))
3928           (setq group (if prefix
3929                           (concat prefix (substring xrefs (match-beginning 1)
3930                                                     (match-end 1)))
3931                         (substring xrefs (match-beginning 1) (match-end 1))))
3932           (setq number
3933                 (string-to-int (substring xrefs (match-beginning 2)
3934                                           (match-end 2))))
3935           (if (setq entry (gnus-gethash group xref-hashtb))
3936               (setcdr entry (cons number (cdr entry)))
3937             (gnus-sethash group (cons number nil) xref-hashtb)))))
3938     (and start xref-hashtb)))
3939
3940 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
3941   "Look through all the headers and mark the Xrefs as read."
3942   (let ((virtual (gnus-virtual-group-p from-newsgroup))
3943         name entry info xref-hashtb idlist method nth4)
3944     (save-excursion
3945       (set-buffer gnus-group-buffer)
3946       (when (setq xref-hashtb
3947                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
3948         (mapatoms
3949          (lambda (group)
3950            (unless (string= from-newsgroup (setq name (symbol-name group)))
3951              (setq idlist (symbol-value group))
3952              ;; Dead groups are not updated.
3953              (and (prog1
3954                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
3955                             info (nth 2 entry))
3956                     (when (stringp (setq nth4 (gnus-info-method info)))
3957                       (setq nth4 (gnus-server-to-method nth4))))
3958                   ;; Only do the xrefs if the group has the same
3959                   ;; select method as the group we have just read.
3960                   (or (gnus-methods-equal-p
3961                        nth4 (gnus-find-method-for-group from-newsgroup))
3962                       virtual
3963                       (equal nth4 (setq method (gnus-find-method-for-group
3964                                                 from-newsgroup)))
3965                       (and (equal (car nth4) (car method))
3966                            (equal (nth 1 nth4) (nth 1 method))))
3967                   gnus-use-cross-reference
3968                   (or (not (eq gnus-use-cross-reference t))
3969                       virtual
3970                       ;; Only do cross-references on subscribed
3971                       ;; groups, if that is what is wanted.
3972                       (<= (gnus-info-level info) gnus-level-subscribed))
3973                   (gnus-group-make-articles-read name idlist))))
3974          xref-hashtb)))))
3975
3976 (defun gnus-group-make-articles-read (group articles)
3977   "Update the info of GROUP to say that only ARTICLES are unread."
3978   (let* ((num 0)
3979          (entry (gnus-gethash group gnus-newsrc-hashtb))
3980          (info (nth 2 entry))
3981          (active (gnus-active group))
3982          range)
3983     ;; First peel off all illegal article numbers.
3984     (when active
3985       (let ((ids articles)
3986             id first)
3987         (while (setq id (pop ids))
3988           (when (and first (> id (cdr active)))
3989             ;; We'll end up in this situation in one particular
3990             ;; obscure situation.  If you re-scan a group and get
3991             ;; a new article that is cross-posted to a different
3992             ;; group that has not been re-scanned, you might get
3993             ;; crossposted article that has a higher number than
3994             ;; Gnus believes possible.  So we re-activate this
3995             ;; group as well.  This might mean doing the
3996             ;; crossposting thingy will *increase* the number
3997             ;; of articles in some groups.  Tsk, tsk.
3998             (setq active (or (gnus-activate-group group) active)))
3999           (when (or (> id (cdr active))
4000                     (< id (car active)))
4001             (setq articles (delq id articles))))))
4002     (save-excursion
4003       (set-buffer gnus-group-buffer)
4004       (gnus-undo-register
4005         `(progn
4006            (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4007            (gnus-info-set-read ',info ',(gnus-info-read info))
4008            (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4009            (gnus-group-update-group ,group t))))
4010     ;; If the read list is nil, we init it.
4011     (and active
4012          (null (gnus-info-read info))
4013          (> (car active) 1)
4014          (gnus-info-set-read info (cons 1 (1- (car active)))))
4015     ;; Then we add the read articles to the range.
4016     (gnus-info-set-read
4017      info
4018      (setq range
4019            (gnus-add-to-range
4020             (gnus-info-read info) (setq articles (sort articles '<)))))
4021     ;; Then we have to re-compute how many unread
4022     ;; articles there are in this group.
4023     (when active
4024       (cond
4025        ((not range)
4026         (setq num (- (1+ (cdr active)) (car active))))
4027        ((not (listp (cdr range)))
4028         (setq num (- (cdr active) (- (1+ (cdr range))
4029                                      (car range)))))
4030        (t
4031         (while range
4032           (if (numberp (car range))
4033               (setq num (1+ num))
4034             (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4035           (setq range (cdr range)))
4036         (setq num (- (cdr active) num))))
4037       ;; Update the number of unread articles.
4038       (setcar entry num)
4039       ;; Update the group buffer.
4040       (gnus-group-update-group group t))))
4041
4042 (defun gnus-methods-equal-p (m1 m2)
4043   (let ((m1 (or m1 gnus-select-method))
4044         (m2 (or m2 gnus-select-method)))
4045     (or (equal m1 m2)
4046         (and (eq (car m1) (car m2))
4047              (or (not (memq 'address (assoc (symbol-name (car m1))
4048                                             gnus-valid-select-methods)))
4049                  (equal (nth 1 m1) (nth 1 m2)))))))
4050
4051 (defvar gnus-newsgroup-none-id 0)
4052
4053 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4054   (let ((cur nntp-server-buffer)
4055         (dependencies
4056          (or dependencies
4057              (save-excursion (set-buffer gnus-summary-buffer)
4058                              gnus-newsgroup-dependencies)))
4059         headers id id-dep ref-dep end ref)
4060     (save-excursion
4061       (set-buffer nntp-server-buffer)
4062       ;; Translate all TAB characters into SPACE characters.
4063       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4064       (run-hooks 'gnus-parse-headers-hook)
4065       (let ((case-fold-search t)
4066             in-reply-to header p lines)
4067         (goto-char (point-min))
4068         ;; Search to the beginning of the next header.  Error messages
4069         ;; do not begin with 2 or 3.
4070         (while (re-search-forward "^[23][0-9]+ " nil t)
4071           (setq id nil
4072                 ref nil)
4073           ;; This implementation of this function, with nine
4074           ;; search-forwards instead of the one re-search-forward and
4075           ;; a case (which basically was the old function) is actually
4076           ;; about twice as fast, even though it looks messier.  You
4077           ;; can't have everything, I guess.  Speed and elegance
4078           ;; doesn't always go hand in hand.
4079           (setq
4080            header
4081            (vector
4082             ;; Number.
4083             (prog1
4084                 (read cur)
4085               (end-of-line)
4086               (setq p (point))
4087               (narrow-to-region (point)
4088                                 (or (and (search-forward "\n.\n" nil t)
4089                                          (- (point) 2))
4090                                     (point))))
4091             ;; Subject.
4092             (progn
4093               (goto-char p)
4094               (if (search-forward "\nsubject: " nil t)
4095                   (nnheader-header-value) "(none)"))
4096             ;; From.
4097             (progn
4098               (goto-char p)
4099               (if (search-forward "\nfrom: " nil t)
4100                   (nnheader-header-value) "(nobody)"))
4101             ;; Date.
4102             (progn
4103               (goto-char p)
4104               (if (search-forward "\ndate: " nil t)
4105                   (nnheader-header-value) ""))
4106             ;; Message-ID.
4107             (progn
4108               (goto-char p)
4109               (setq id (if (search-forward "\nmessage-id: " nil t)
4110                            (nnheader-header-value)
4111                          ;; If there was no message-id, we just fake one
4112                          ;; to make subsequent routines simpler.
4113                          (nnheader-generate-fake-message-id))))
4114             ;; References.
4115             (progn
4116               (goto-char p)
4117               (if (search-forward "\nreferences: " nil t)
4118                   (progn
4119                     (setq end (point))
4120                     (prog1
4121                         (nnheader-header-value)
4122                       (setq ref
4123                             (buffer-substring
4124                              (progn
4125                                (end-of-line)
4126                                (search-backward ">" end t)
4127                                (1+ (point)))
4128                              (progn
4129                                (search-backward "<" end t)
4130                                (point))))))
4131                 ;; Get the references from the in-reply-to header if there
4132                 ;; were no references and the in-reply-to header looks
4133                 ;; promising.
4134                 (if (and (search-forward "\nin-reply-to: " nil t)
4135                          (setq in-reply-to (nnheader-header-value))
4136                          (string-match "<[^>]+>" in-reply-to))
4137                     (setq ref (substring in-reply-to (match-beginning 0)
4138                                          (match-end 0)))
4139                   (setq ref nil))))
4140             ;; Chars.
4141             0
4142             ;; Lines.
4143             (progn
4144               (goto-char p)
4145               (if (search-forward "\nlines: " nil t)
4146                   (if (numberp (setq lines (read cur)))
4147                       lines 0)
4148                 0))
4149             ;; Xref.
4150             (progn
4151               (goto-char p)
4152               (and (search-forward "\nxref: " nil t)
4153                    (nnheader-header-value)))))
4154           (when (equal id ref)
4155             (setq ref nil))
4156           ;; We do the threading while we read the headers.  The
4157           ;; message-id and the last reference are both entered into
4158           ;; the same hash table.  Some tippy-toeing around has to be
4159           ;; done in case an article has arrived before the article
4160           ;; which it refers to.
4161           (if (boundp (setq id-dep (intern id dependencies)))
4162               (if (and (car (symbol-value id-dep))
4163                        (not force-new))
4164                   ;; An article with this Message-ID has already
4165                   ;; been seen, so we ignore this one, except we add
4166                   ;; any additional Xrefs (in case the two articles
4167                   ;; came from different servers).
4168                   (progn
4169                     (mail-header-set-xref
4170                      (car (symbol-value id-dep))
4171                      (concat (or (mail-header-xref
4172                                   (car (symbol-value id-dep)))
4173                                  "")
4174                              (or (mail-header-xref header) "")))
4175                     (setq header nil))
4176                 (setcar (symbol-value id-dep) header))
4177             (set id-dep (list header)))
4178           (when  header
4179             (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
4180                 (setcdr (symbol-value ref-dep)
4181                         (nconc (cdr (symbol-value ref-dep))
4182                                (list (symbol-value id-dep))))
4183               (set ref-dep (list nil (symbol-value id-dep))))
4184             (push header headers))
4185           (goto-char (point-max))
4186           (widen))
4187         (nreverse headers)))))
4188
4189 ;; The following macros and functions were written by Felix Lee
4190 ;; <flee@cse.psu.edu>.
4191
4192 (defmacro gnus-nov-read-integer ()
4193   '(prog1
4194        (if (= (following-char) ?\t)
4195            0
4196          (let ((num (ignore-errors (read buffer))))
4197            (if (numberp num) num 0)))
4198      (unless (eobp)
4199        (forward-char 1))))
4200
4201 (defmacro gnus-nov-skip-field ()
4202   '(search-forward "\t" eol 'move))
4203
4204 (defmacro gnus-nov-field ()
4205   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
4206
4207 ;; (defvar gnus-nov-none-counter 0)
4208
4209 ;; This function has to be called with point after the article number
4210 ;; on the beginning of the line.
4211 (defun gnus-nov-parse-line (number dependencies &optional force-new)
4212   (let ((eol (gnus-point-at-eol))
4213         (buffer (current-buffer))
4214         header ref id id-dep ref-dep)
4215
4216     ;; overview: [num subject from date id refs chars lines misc]
4217     (unwind-protect
4218         (progn
4219           (narrow-to-region (point) eol)
4220           (unless (eobp)
4221             (forward-char))
4222
4223           (setq header
4224                 (vector
4225                  number                 ; number
4226                  (gnus-nov-field)       ; subject
4227                  (gnus-nov-field)       ; from
4228                  (gnus-nov-field)       ; date
4229                  (setq id (or (gnus-nov-field)
4230                               (nnheader-generate-fake-message-id))) ; id
4231                  (progn
4232                    (let ((beg (point)))
4233                      (search-forward "\t" eol)
4234                      (if (search-backward ">" beg t)
4235                          (setq ref
4236                                (buffer-substring
4237                                 (1+ (point))
4238                                 (search-backward "<" beg t)))
4239                        (setq ref nil))
4240                      (goto-char beg))
4241                    (gnus-nov-field))    ; refs
4242                  (gnus-nov-read-integer) ; chars
4243                  (gnus-nov-read-integer) ; lines
4244                  (if (= (following-char) ?\n)
4245                      nil
4246                    (gnus-nov-field))    ; misc
4247                  )))
4248
4249       (widen))
4250
4251     ;; We build the thread tree.
4252     (when (equal id ref)
4253       ;; This article refers back to itself.  Naughty, naughty.
4254       (setq ref nil))
4255     (if (boundp (setq id-dep (intern id dependencies)))
4256         (if (and (car (symbol-value id-dep))
4257                  (not force-new))
4258             ;; An article with this Message-ID has already been seen,
4259             ;; so we ignore this one, except we add any additional
4260             ;; Xrefs (in case the two articles came from different
4261             ;; servers.
4262             (progn
4263               (mail-header-set-xref
4264                (car (symbol-value id-dep))
4265                (concat (or (mail-header-xref
4266                             (car (symbol-value id-dep)))
4267                            "")
4268                        (or (mail-header-xref header) "")))
4269               (setq header nil))
4270           (setcar (symbol-value id-dep) header))
4271       (set id-dep (list header)))
4272     (when header
4273       (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
4274           (setcdr (symbol-value ref-dep)
4275                   (nconc (cdr (symbol-value ref-dep))
4276                          (list (symbol-value id-dep))))
4277         (set ref-dep (list nil (symbol-value id-dep)))))
4278     header))
4279
4280 ;; Goes through the xover lines and returns a list of vectors
4281 (defun gnus-get-newsgroup-headers-xover (sequence &optional 
4282                                                   force-new dependencies
4283                                                   group also-fetch-heads)
4284   "Parse the news overview data in the server buffer, and return a
4285 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4286   ;; Get the Xref when the users reads the articles since most/some
4287   ;; NNTP servers do not include Xrefs when using XOVER.
4288   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4289   (let ((cur nntp-server-buffer)
4290         (dependencies (or dependencies gnus-newsgroup-dependencies))
4291         number headers header)
4292     (save-excursion
4293       (set-buffer nntp-server-buffer)
4294       ;; Allow the user to mangle the headers before parsing them.
4295       (run-hooks 'gnus-parse-headers-hook)
4296       (goto-char (point-min))
4297       (while (not (eobp))
4298         (condition-case ()
4299             (while (and sequence (not (eobp)))
4300               (setq number (read cur))
4301               (while (and sequence
4302                           (< (car sequence) number))
4303                 (setq sequence (cdr sequence)))
4304               (and sequence
4305                    (eq number (car sequence))
4306                    (progn
4307                      (setq sequence (cdr sequence))
4308                      (setq header (inline
4309                                     (gnus-nov-parse-line
4310                                      number dependencies force-new))))
4311                    (push header headers))
4312               (forward-line 1))
4313           (error
4314            (gnus-error 4 "Strange nov line (%d)"
4315                        (count-lines (point-min) (point)))))
4316         (forward-line 1))
4317       ;; A common bug in inn is that if you have posted an article and
4318       ;; then retrieves the active file, it will answer correctly --
4319       ;; the new article is included.  However, a NOV entry for the
4320       ;; article may not have been generated yet, so this may fail.
4321       ;; We work around this problem by retrieving the last few
4322       ;; headers using HEAD.
4323       (if (or (not also-fetch-heads)
4324               (not sequence))
4325           ;; We (probably) got all the headers.
4326           (nreverse headers)
4327         (let ((gnus-nov-is-evil t))
4328           (nconc
4329            (nreverse headers)
4330            (when (gnus-retrieve-headers sequence group)
4331              (gnus-get-newsgroup-headers))))))))
4332
4333 (defun gnus-article-get-xrefs ()
4334   "Fill in the Xref value in `gnus-current-headers', if necessary.
4335 This is meant to be called in `gnus-article-internal-prepare-hook'."
4336   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4337                                  gnus-current-headers)))
4338     (or (not gnus-use-cross-reference)
4339         (not headers)
4340         (and (mail-header-xref headers)
4341              (not (string= (mail-header-xref headers) "")))
4342         (let ((case-fold-search t)
4343               xref)
4344           (save-restriction
4345             (nnheader-narrow-to-headers)
4346             (goto-char (point-min))
4347             (when (or (and (eq (downcase (following-char)) ?x)
4348                            (looking-at "Xref:"))
4349                       (search-forward "\nXref:" nil t))
4350               (goto-char (1+ (match-end 0)))
4351               (setq xref (buffer-substring (point)
4352                                            (progn (end-of-line) (point))))
4353               (mail-header-set-xref headers xref)))))))
4354
4355 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4356   "Find article ID and insert the summary line for that article."
4357   (let ((header (if (and old-header use-old-header)
4358                     old-header (gnus-read-header id)))
4359         (number (and (numberp id) id))
4360         pos d)
4361     (when header
4362       ;; Rebuild the thread that this article is part of and go to the
4363       ;; article we have fetched.
4364       (when (and (not gnus-show-threads)
4365                  old-header)
4366         (when (setq d (gnus-data-find (mail-header-number old-header)))
4367           (goto-char (gnus-data-pos d))
4368           (gnus-data-remove 
4369            number
4370            (- (gnus-point-at-bol)
4371               (prog1
4372                   (1+ (gnus-point-at-eol))
4373                 (gnus-delete-line))))))
4374       (when old-header
4375         (mail-header-set-number header (mail-header-number old-header)))
4376       (setq gnus-newsgroup-sparse
4377             (delq (setq number (mail-header-number header))
4378                   gnus-newsgroup-sparse))
4379       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4380       (gnus-rebuild-thread (mail-header-id header))
4381       (gnus-summary-goto-subject number nil t))
4382     (when (and (numberp number)
4383                (> number 0))
4384       ;; We have to update the boundaries even if we can't fetch the
4385       ;; article if ID is a number -- so that the next `P' or `N'
4386       ;; command will fetch the previous (or next) article even
4387       ;; if the one we tried to fetch this time has been canceled.
4388       (when (> number gnus-newsgroup-end)
4389         (setq gnus-newsgroup-end number))
4390       (when (< number gnus-newsgroup-begin)
4391         (setq gnus-newsgroup-begin number))
4392       (setq gnus-newsgroup-unselected
4393             (delq number gnus-newsgroup-unselected)))
4394     ;; Report back a success?
4395     (and header (mail-header-number header))))
4396
4397 ;;; Process/prefix in the summary buffer
4398
4399 (defun gnus-summary-work-articles (n)
4400   "Return a list of articles to be worked upon.  The prefix argument,
4401 the list of process marked articles, and the current article will be
4402 taken into consideration."
4403   (cond
4404    (n
4405     ;; A numerical prefix has been given.
4406     (setq n (prefix-numeric-value n))
4407     (let ((backward (< n 0))
4408           (n (abs (prefix-numeric-value n)))
4409           articles article)
4410       (save-excursion
4411         (while
4412             (and (> n 0)
4413                  (push (setq article (gnus-summary-article-number))
4414                        articles)
4415                  (if backward
4416                      (gnus-summary-find-prev nil article)
4417                    (gnus-summary-find-next nil article)))
4418           (decf n)))
4419       (nreverse articles)))
4420    ((gnus-region-active-p)
4421     ;; Work on the region between point and mark.
4422     (let ((max (max (point) (mark)))
4423           articles article)
4424       (save-excursion
4425         (goto-char (min (point) (mark)))
4426         (while
4427             (and
4428              (push (setq article (gnus-summary-article-number)) articles)
4429              (gnus-summary-find-next nil article)
4430              (< (point) max)))
4431         (nreverse articles))))
4432    (gnus-newsgroup-processable
4433     ;; There are process-marked articles present.
4434     ;; Save current state.
4435     (gnus-summary-save-process-mark)
4436     ;; Return the list.
4437     (reverse gnus-newsgroup-processable))
4438    (t
4439     ;; Just return the current article.
4440     (list (gnus-summary-article-number)))))
4441
4442 (defun gnus-summary-save-process-mark ()
4443   "Push the current set of process marked articles on the stack."
4444   (interactive)
4445   (push (copy-sequence gnus-newsgroup-processable)
4446         gnus-newsgroup-process-stack))
4447
4448 (defun gnus-summary-kill-process-mark ()
4449   "Push the current set of process marked articles on the stack and unmark."
4450   (interactive)
4451   (gnus-summary-save-process-mark)
4452   (gnus-summary-unmark-all-processable))
4453
4454 (defun gnus-summary-yank-process-mark ()
4455   "Pop the last process mark state off the stack and restore it."
4456   (interactive)
4457   (unless gnus-newsgroup-process-stack
4458     (error "Empty mark stack"))
4459   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4460
4461 (defun gnus-summary-process-mark-set (set)
4462   "Make SET into the current process marked articles."
4463   (gnus-summary-unmark-all-processable)
4464   (while set
4465     (gnus-summary-set-process-mark (pop set))))
4466
4467 ;;; Searching and stuff
4468
4469 (defun gnus-summary-search-group (&optional backward use-level)
4470   "Search for next unread newsgroup.
4471 If optional argument BACKWARD is non-nil, search backward instead."
4472   (save-excursion
4473     (set-buffer gnus-group-buffer)
4474     (when (gnus-group-search-forward
4475            backward nil (if use-level (gnus-group-group-level) nil))
4476       (gnus-group-group-name))))
4477
4478 (defun gnus-summary-best-group (&optional exclude-group)
4479   "Find the name of the best unread group.
4480 If EXCLUDE-GROUP, do not go to this group."
4481   (save-excursion
4482     (set-buffer gnus-group-buffer)
4483     (save-excursion
4484       (gnus-group-best-unread-group exclude-group))))
4485
4486 (defun gnus-summary-find-next (&optional unread article backward)
4487   (if backward (gnus-summary-find-prev)
4488     (let* ((dummy (gnus-summary-article-intangible-p))
4489            (article (or article (gnus-summary-article-number)))
4490            (arts (gnus-data-find-list article))
4491            result)
4492       (when (and (not dummy)
4493                  (or (not gnus-summary-check-current)
4494                      (not unread)
4495                      (not (gnus-data-unread-p (car arts)))))
4496         (setq arts (cdr arts)))
4497       (when (setq result
4498                   (if unread
4499                       (progn
4500                         (while arts
4501                           (when (gnus-data-unread-p (car arts))
4502                             (setq result (car arts)
4503                                   arts nil))
4504                           (setq arts (cdr arts)))
4505                         result)
4506                     (car arts)))
4507         (goto-char (gnus-data-pos result))
4508         (gnus-data-number result)))))
4509
4510 (defun gnus-summary-find-prev (&optional unread article)
4511   (let* ((eobp (eobp))
4512          (article (or article (gnus-summary-article-number)))
4513          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
4514          result)
4515     (when (and (not eobp)
4516                (or (not gnus-summary-check-current)
4517                    (not unread)
4518                    (not (gnus-data-unread-p (car arts)))))
4519       (setq arts (cdr arts)))
4520     (when (setq result
4521                 (if unread
4522                     (progn
4523                       (while arts
4524                         (when (gnus-data-unread-p (car arts))
4525                           (setq result (car arts)
4526                                 arts nil))
4527                         (setq arts (cdr arts)))
4528                       result)
4529                   (car arts)))
4530       (goto-char (gnus-data-pos result))
4531       (gnus-data-number result))))
4532
4533 (defun gnus-summary-find-subject (subject &optional unread backward article)
4534   (let* ((simp-subject (gnus-simplify-subject-fully subject))
4535          (article (or article (gnus-summary-article-number)))
4536          (articles (gnus-data-list backward))
4537          (arts (gnus-data-find-list article articles))
4538          result)
4539     (when (or (not gnus-summary-check-current)
4540               (not unread)
4541               (not (gnus-data-unread-p (car arts))))
4542       (setq arts (cdr arts)))
4543     (while arts
4544       (and (or (not unread)
4545                (gnus-data-unread-p (car arts)))
4546            (vectorp (gnus-data-header (car arts)))
4547            (gnus-subject-equal
4548             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
4549            (setq result (car arts)
4550                  arts nil))
4551       (setq arts (cdr arts)))
4552     (and result
4553          (goto-char (gnus-data-pos result))
4554          (gnus-data-number result))))
4555
4556 (defun gnus-summary-search-forward (&optional unread subject backward)
4557   "Search forward for an article.
4558 If UNREAD, look for unread articles.  If SUBJECT, look for
4559 articles with that subject.  If BACKWARD, search backward instead."
4560   (cond (subject (gnus-summary-find-subject subject unread backward))
4561         (backward (gnus-summary-find-prev unread))
4562         (t (gnus-summary-find-next unread))))
4563
4564 (defun gnus-recenter (&optional n)
4565   "Center point in window and redisplay frame.
4566 Also do horizontal recentering."
4567   (interactive "P")
4568   (when (and gnus-auto-center-summary
4569              (not (eq gnus-auto-center-summary 'vertical)))
4570     (gnus-horizontal-recenter))
4571   (recenter n))
4572
4573 (defun gnus-summary-recenter ()
4574   "Center point in the summary window.
4575 If `gnus-auto-center-summary' is nil, or the article buffer isn't
4576 displayed, no centering will be performed."
4577   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
4578   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
4579   (let* ((top (cond ((< (window-height) 4) 0)
4580                     ((< (window-height) 7) 1)
4581                     (t 2)))
4582          (height (1- (window-height)))
4583          (bottom (save-excursion (goto-char (point-max))
4584                                  (forward-line (- height))
4585                                  (point)))
4586          (window (get-buffer-window (current-buffer))))
4587     ;; The user has to want it.
4588     (when gnus-auto-center-summary
4589       (when (get-buffer-window gnus-article-buffer)
4590         ;; Only do recentering when the article buffer is displayed,
4591         ;; Set the window start to either `bottom', which is the biggest
4592         ;; possible valid number, or the second line from the top,
4593         ;; whichever is the least.
4594         (set-window-start
4595          window (min bottom (save-excursion 
4596                               (forward-line (- top)) (point)))))
4597       ;; Do horizontal recentering while we're at it.
4598       (when (and (get-buffer-window (current-buffer) t)
4599                  (not (eq gnus-auto-center-summary 'vertical)))
4600         (let ((selected (selected-window)))
4601           (select-window (get-buffer-window (current-buffer) t))
4602           (gnus-summary-position-point)
4603           (gnus-horizontal-recenter)
4604           (select-window selected))))))
4605
4606 (defun gnus-summary-jump-to-group (newsgroup)
4607   "Move point to NEWSGROUP in group mode buffer."
4608   ;; Keep update point of group mode buffer if visible.
4609   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
4610       (save-window-excursion
4611         ;; Take care of tree window mode.
4612         (when (get-buffer-window gnus-group-buffer)
4613           (pop-to-buffer gnus-group-buffer))
4614         (gnus-group-jump-to-group newsgroup))
4615     (save-excursion
4616       ;; Take care of tree window mode.
4617       (if (get-buffer-window gnus-group-buffer)
4618           (pop-to-buffer gnus-group-buffer)
4619         (set-buffer gnus-group-buffer))
4620       (gnus-group-jump-to-group newsgroup))))
4621
4622 ;; This function returns a list of article numbers based on the
4623 ;; difference between the ranges of read articles in this group and
4624 ;; the range of active articles.
4625 (defun gnus-list-of-unread-articles (group)
4626   (let* ((read (gnus-info-read (gnus-get-info group)))
4627          (active (or (gnus-active group) (gnus-activate-group group)))
4628          (last (cdr active))
4629          first nlast unread)
4630     ;; If none are read, then all are unread.
4631     (if (not read)
4632         (setq first (car active))
4633       ;; If the range of read articles is a single range, then the
4634       ;; first unread article is the article after the last read
4635       ;; article.  Sounds logical, doesn't it?
4636       (if (not (listp (cdr read)))
4637           (setq first (1+ (cdr read)))
4638         ;; `read' is a list of ranges.
4639         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
4640                                   (caar read)))
4641                   1)
4642           (setq first 1))
4643         (while read
4644           (when first
4645             (while (< first nlast)
4646               (push first unread)
4647               (setq first (1+ first))))
4648           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
4649           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
4650           (setq read (cdr read)))))
4651     ;; And add the last unread articles.
4652     (while (<= first last)
4653       (push first unread)
4654       (setq first (1+ first)))
4655     ;; Return the list of unread articles.
4656     (nreverse unread)))
4657
4658 (defun gnus-list-of-read-articles (group)
4659   "Return a list of unread, unticked and non-dormant articles."
4660   (let* ((info (gnus-get-info group))
4661          (marked (gnus-info-marks info))
4662          (active (gnus-active group)))
4663     (and info active
4664          (gnus-set-difference
4665           (gnus-sorted-complement
4666            (gnus-uncompress-range active)
4667            (gnus-list-of-unread-articles group))
4668           (append
4669            (gnus-uncompress-range (cdr (assq 'dormant marked)))
4670            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
4671
4672 ;; Various summary commands
4673
4674 (defun gnus-summary-universal-argument (arg)
4675   "Perform any operation on all articles that are process/prefixed."
4676   (interactive "P")
4677   (gnus-set-global-variables)
4678   (let ((articles (gnus-summary-work-articles arg))
4679         func article)
4680     (if (eq
4681          (setq
4682           func
4683           (key-binding
4684            (read-key-sequence
4685             (substitute-command-keys
4686              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
4687              ))))
4688          'undefined)
4689         (gnus-error 1 "Undefined key")
4690       (save-excursion
4691         (while articles
4692           (gnus-summary-goto-subject (setq article (pop articles)))
4693           (let (gnus-newsgroup-processable)
4694             (command-execute func))
4695           (gnus-summary-remove-process-mark article)))))
4696   (gnus-summary-position-point))
4697
4698 (defun gnus-summary-toggle-truncation (&optional arg)
4699   "Toggle truncation of summary lines.
4700 With arg, turn line truncation on iff arg is positive."
4701   (interactive "P")
4702   (setq truncate-lines
4703         (if (null arg) (not truncate-lines)
4704           (> (prefix-numeric-value arg) 0)))
4705   (redraw-display))
4706
4707 (defun gnus-summary-reselect-current-group (&optional all rescan)
4708   "Exit and then reselect the current newsgroup.
4709 The prefix argument ALL means to select all articles."
4710   (interactive "P")
4711   (gnus-set-global-variables)
4712   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
4713     (error "Ephemeral groups can't be reselected"))
4714   (let ((current-subject (gnus-summary-article-number))
4715         (group gnus-newsgroup-name))
4716     (setq gnus-newsgroup-begin nil)
4717     (gnus-summary-exit)
4718     ;; We have to adjust the point of group mode buffer because 
4719     ;; point was moved to the next unread newsgroup by exiting.
4720     (gnus-summary-jump-to-group group)
4721     (when rescan
4722       (save-excursion
4723         (gnus-group-get-new-news-this-group 1)))
4724     (gnus-group-read-group all t)
4725     (gnus-summary-goto-subject current-subject nil t)))
4726
4727 (defun gnus-summary-rescan-group (&optional all)
4728   "Exit the newsgroup, ask for new articles, and select the newsgroup."
4729   (interactive "P")
4730   (gnus-summary-reselect-current-group all t))
4731
4732 (defun gnus-summary-update-info (&optional non-destructive)
4733   (save-excursion
4734     (let ((group gnus-newsgroup-name))
4735       (when gnus-newsgroup-kill-headers
4736         (setq gnus-newsgroup-killed
4737               (gnus-compress-sequence
4738                (nconc
4739                 (gnus-set-sorted-intersection
4740                  (gnus-uncompress-range gnus-newsgroup-killed)
4741                  (setq gnus-newsgroup-unselected
4742                        (sort gnus-newsgroup-unselected '<)))
4743                 (setq gnus-newsgroup-unreads
4744                       (sort gnus-newsgroup-unreads '<)))
4745                t)))
4746       (unless (listp (cdr gnus-newsgroup-killed))
4747         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
4748       (let ((headers gnus-newsgroup-headers))
4749         (when (and (not gnus-save-score)
4750                    (not non-destructive))
4751           (setq gnus-newsgroup-scored nil))
4752         ;; Set the new ranges of read articles.
4753         (gnus-update-read-articles
4754          group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
4755         ;; Set the current article marks.
4756         (gnus-update-marks)
4757         ;; Do the cross-ref thing.
4758         (when gnus-use-cross-reference
4759           (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
4760         ;; Do adaptive scoring, and possibly save score files.
4761         (when gnus-newsgroup-adaptive
4762           (gnus-score-adaptive))
4763         (when gnus-use-scoring
4764           (gnus-score-save))
4765         ;; Do not switch windows but change the buffer to work.
4766         (set-buffer gnus-group-buffer)
4767         (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
4768           (gnus-group-update-group group))))))
4769
4770 (defun gnus-summary-save-newsrc (&optional force)
4771   "Save the current number of read/marked articles in the dribble buffer.
4772 The dribble buffer will then be saved.
4773 If FORCE (the prefix), also save the .newsrc file(s)."
4774   (interactive "P")
4775   (gnus-summary-update-info t)
4776   (if force
4777       (gnus-save-newsrc-file)
4778     (gnus-dribble-save)))
4779
4780 (defun gnus-summary-exit (&optional temporary)
4781   "Exit reading current newsgroup, and then return to group selection mode.
4782 gnus-exit-group-hook is called with no arguments if that value is non-nil."
4783   (interactive)
4784   (gnus-set-global-variables)
4785   (gnus-kill-save-kill-buffer)
4786   (let* ((group gnus-newsgroup-name)
4787          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
4788          (mode major-mode)
4789          (buf (current-buffer)))
4790     (run-hooks 'gnus-summary-prepare-exit-hook)
4791     ;; If we have several article buffers, we kill them at exit.
4792     (unless gnus-single-article-buffer
4793       (gnus-kill-buffer gnus-original-article-buffer)
4794       (setq gnus-article-current nil))
4795     (when gnus-use-cache
4796       (gnus-cache-possibly-remove-articles)
4797       (gnus-cache-save-buffers))
4798     (gnus-async-prefetch-remove-group group)
4799     (when gnus-suppress-duplicates
4800       (gnus-dup-enter-articles))
4801     (when gnus-use-trees
4802       (gnus-tree-close group))
4803     ;; Make all changes in this group permanent.
4804     (unless quit-config
4805       (run-hooks 'gnus-exit-group-hook)
4806       (gnus-summary-update-info))
4807     (gnus-close-group group)
4808     ;; Make sure where I was, and go to next newsgroup.
4809     (set-buffer gnus-group-buffer)
4810     (unless quit-config
4811       (gnus-group-jump-to-group group))
4812     (run-hooks 'gnus-summary-exit-hook)
4813     (unless quit-config
4814       (gnus-group-next-unread-group 1))
4815     (if temporary
4816         nil                             ;Nothing to do.
4817       ;; If we have several article buffers, we kill them at exit.
4818       (unless gnus-single-article-buffer
4819         (gnus-kill-buffer gnus-article-buffer)
4820         (gnus-kill-buffer gnus-original-article-buffer)
4821         (setq gnus-article-current nil))
4822       (set-buffer buf)
4823       (if (not gnus-kill-summary-on-exit)
4824           (gnus-deaden-summary)
4825         ;; We set all buffer-local variables to nil.  It is unclear why
4826         ;; this is needed, but if we don't, buffer-local variables are
4827         ;; not garbage-collected, it seems.  This would the lead to en
4828         ;; ever-growing Emacs.
4829         (gnus-summary-clear-local-variables)
4830         (when (get-buffer gnus-article-buffer)
4831           (bury-buffer gnus-article-buffer))
4832         ;; We clear the global counterparts of the buffer-local
4833         ;; variables as well, just to be on the safe side.
4834         (set-buffer gnus-group-buffer)
4835         (gnus-summary-clear-local-variables)
4836         ;; Return to group mode buffer.
4837         (when (eq mode 'gnus-summary-mode)
4838           (gnus-kill-buffer buf)))
4839       (setq gnus-current-select-method gnus-select-method)
4840       (pop-to-buffer gnus-group-buffer)
4841       ;; Clear the current group name.
4842       (if (not quit-config)
4843           (progn
4844             (gnus-group-jump-to-group group)
4845             (gnus-group-next-unread-group 1)
4846             (gnus-configure-windows 'group 'force))
4847         (gnus-handle-ephemeral-exit quit-config))
4848       (unless quit-config
4849         (setq gnus-newsgroup-name nil)))))
4850
4851 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
4852 (defun gnus-summary-exit-no-update (&optional no-questions)
4853   "Quit reading current newsgroup without updating read article info."
4854   (interactive)
4855   (gnus-set-global-variables)
4856   (let* ((group gnus-newsgroup-name)
4857          (quit-config (gnus-group-quit-config group)))
4858     (when (or no-questions
4859               gnus-expert-user
4860               (gnus-y-or-n-p "Discard changes to this group and exit? "))
4861       ;; If we have several article buffers, we kill them at exit.
4862       (unless gnus-single-article-buffer
4863         (gnus-kill-buffer gnus-article-buffer)
4864         (gnus-kill-buffer gnus-original-article-buffer)
4865         (setq gnus-article-current nil))
4866       (if (not gnus-kill-summary-on-exit)
4867           (gnus-deaden-summary)
4868         (gnus-close-group group)
4869         (gnus-summary-clear-local-variables)
4870         (set-buffer gnus-group-buffer)
4871         (gnus-summary-clear-local-variables)
4872         (when (get-buffer gnus-summary-buffer)
4873           (kill-buffer gnus-summary-buffer)))
4874       (unless gnus-single-article-buffer
4875         (setq gnus-article-current nil))
4876       (when gnus-use-trees
4877         (gnus-tree-close group))
4878       (gnus-async-prefetch-remove-group group)
4879       (when (get-buffer gnus-article-buffer)
4880         (bury-buffer gnus-article-buffer))
4881       ;; Return to the group buffer.
4882       (gnus-configure-windows 'group 'force)
4883       ;; Clear the current group name.
4884       (setq gnus-newsgroup-name nil)
4885       (when (equal (gnus-group-group-name) group)
4886         (gnus-group-next-unread-group 1))
4887       (when quit-config
4888         (gnus-handle-ephemeral-exit quit-config)))))
4889
4890 (defun gnus-handle-ephemeral-exit (quit-config)
4891   "Handle movement when leaving an ephemeral group.  The state
4892 which existed when entering the ephemeral is reset."
4893   (if (not (buffer-name (car quit-config)))
4894       (gnus-configure-windows 'group 'force)
4895     (set-buffer (car quit-config))
4896     (cond ((eq major-mode 'gnus-summary-mode)
4897            (gnus-set-global-variables))
4898           ((eq major-mode 'gnus-article-mode)
4899            (save-excursion
4900              ;; The `gnus-summary-buffer' variable may point
4901              ;; to the old summary buffer when using a single
4902              ;; article buffer.
4903              (unless (gnus-buffer-live-p gnus-summary-buffer)
4904                (set-buffer gnus-group-buffer))
4905              (set-buffer gnus-summary-buffer)
4906              (gnus-set-global-variables))))
4907     (if (or (eq (cdr quit-config) 'article)
4908             (eq (cdr quit-config) 'pick))
4909         (progn
4910           ;; The current article may be from the ephemeral group
4911           ;; thus it is best that we reload this article
4912           (gnus-summary-show-article)
4913           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
4914               (gnus-configure-windows 'pick 'force)
4915             (gnus-configure-windows (cdr quit-config) 'force)))
4916       (gnus-configure-windows (cdr quit-config) 'force))
4917     (when (eq major-mode 'gnus-summary-mode)
4918       (gnus-summary-next-subject 1 nil t)
4919       (gnus-summary-recenter)
4920       (gnus-summary-position-point))))
4921
4922 ;;; Dead summaries.
4923
4924 (defvar gnus-dead-summary-mode-map nil)
4925
4926 (unless gnus-dead-summary-mode-map
4927   (setq gnus-dead-summary-mode-map (make-keymap))
4928   (suppress-keymap gnus-dead-summary-mode-map)
4929   (substitute-key-definition
4930    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
4931   (let ((keys '("\C-d" "\r" "\177")))
4932     (while keys
4933       (define-key gnus-dead-summary-mode-map
4934         (pop keys) 'gnus-summary-wake-up-the-dead))))
4935
4936 (defvar gnus-dead-summary-mode nil
4937   "Minor mode for Gnus summary buffers.")
4938
4939 (defun gnus-dead-summary-mode (&optional arg)
4940   "Minor mode for Gnus summary buffers."
4941   (interactive "P")
4942   (when (eq major-mode 'gnus-summary-mode)
4943     (make-local-variable 'gnus-dead-summary-mode)
4944     (setq gnus-dead-summary-mode
4945           (if (null arg) (not gnus-dead-summary-mode)
4946             (> (prefix-numeric-value arg) 0)))
4947     (when gnus-dead-summary-mode
4948       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
4949         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
4950       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
4951         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
4952               minor-mode-map-alist)))))
4953
4954 (defun gnus-deaden-summary ()
4955   "Make the current summary buffer into a dead summary buffer."
4956   ;; Kill any previous dead summary buffer.
4957   (when (and gnus-dead-summary
4958              (buffer-name gnus-dead-summary))
4959     (save-excursion
4960       (set-buffer gnus-dead-summary)
4961       (when gnus-dead-summary-mode
4962         (kill-buffer (current-buffer)))))
4963   ;; Make this the current dead summary.
4964   (setq gnus-dead-summary (current-buffer))
4965   (gnus-dead-summary-mode 1)
4966   (let ((name (buffer-name)))
4967     (when (string-match "Summary" name)
4968       (rename-buffer
4969        (concat (substring name 0 (match-beginning 0)) "Dead "
4970                (substring name (match-beginning 0)))
4971        t))))
4972
4973 (defun gnus-kill-or-deaden-summary (buffer)
4974   "Kill or deaden the summary BUFFER."
4975   (when (and (buffer-name buffer)
4976              (not gnus-single-article-buffer))
4977     (save-excursion
4978       (set-buffer buffer)
4979       (gnus-kill-buffer gnus-article-buffer)
4980       (gnus-kill-buffer gnus-original-article-buffer)))
4981   (cond (gnus-kill-summary-on-exit
4982          (when (and gnus-use-trees
4983                     (and (get-buffer buffer)
4984                          (buffer-name (get-buffer buffer))))
4985            (save-excursion
4986              (set-buffer (get-buffer buffer))
4987              (gnus-tree-close gnus-newsgroup-name)))
4988          (gnus-kill-buffer buffer))
4989         ((and (get-buffer buffer)
4990               (buffer-name (get-buffer buffer)))
4991          (save-excursion
4992            (set-buffer buffer)
4993            (gnus-deaden-summary)))))
4994
4995 (defun gnus-summary-wake-up-the-dead (&rest args)
4996   "Wake up the dead summary buffer."
4997   (interactive)
4998   (gnus-dead-summary-mode -1)
4999   (let ((name (buffer-name)))
5000     (when (string-match "Dead " name)
5001       (rename-buffer
5002        (concat (substring name 0 (match-beginning 0))
5003                (substring name (match-end 0)))
5004        t)))
5005   (gnus-message 3 "This dead summary is now alive again"))
5006
5007 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5008 (defun gnus-summary-fetch-faq (&optional faq-dir)
5009   "Fetch the FAQ for the current group.
5010 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5011 in."
5012   (interactive
5013    (list
5014     (when current-prefix-arg
5015       (completing-read
5016        "Faq dir: " (and (listp gnus-group-faq-directory)
5017                         gnus-group-faq-directory)))))
5018   (let (gnus-faq-buffer)
5019     (when (setq gnus-faq-buffer
5020                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5021       (gnus-configure-windows 'summary-faq))))
5022
5023 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5024 (defun gnus-summary-describe-group (&optional force)
5025   "Describe the current newsgroup."
5026   (interactive "P")
5027   (gnus-group-describe-group force gnus-newsgroup-name))
5028
5029 (defun gnus-summary-describe-briefly ()
5030   "Describe summary mode commands briefly."
5031   (interactive)
5032   (gnus-message 6
5033                 (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")))
5034
5035 ;; Walking around group mode buffer from summary mode.
5036
5037 (defun gnus-summary-next-group (&optional no-article target-group backward)
5038   "Exit current newsgroup and then select next unread newsgroup.
5039 If prefix argument NO-ARTICLE is non-nil, no article is selected
5040 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5041 previous group instead."
5042   (interactive "P")
5043   (gnus-set-global-variables)
5044   ;; Stop pre-fetching.
5045   (gnus-async-halt-prefetch)
5046   (let ((current-group gnus-newsgroup-name)
5047         (current-buffer (current-buffer))
5048         entered)
5049     ;; First we semi-exit this group to update Xrefs and all variables.
5050     ;; We can't do a real exit, because the window conf must remain
5051     ;; the same in case the user is prompted for info, and we don't
5052     ;; want the window conf to change before that...
5053     (gnus-summary-exit t)
5054     (while (not entered)
5055       ;; Then we find what group we are supposed to enter.
5056       (set-buffer gnus-group-buffer)
5057       (gnus-group-jump-to-group current-group)
5058       (setq target-group
5059             (or target-group
5060                 (if (eq gnus-keep-same-level 'best)
5061                     (gnus-summary-best-group gnus-newsgroup-name)
5062                   (gnus-summary-search-group backward gnus-keep-same-level))))
5063       (if (not target-group)
5064           ;; There are no further groups, so we return to the group
5065           ;; buffer.
5066           (progn
5067             (gnus-message 5 "Returning to the group buffer")
5068             (setq entered t)
5069             (set-buffer current-buffer)
5070             (gnus-summary-exit)
5071             (run-hooks 'gnus-group-no-more-groups-hook))
5072         ;; We try to enter the target group.
5073         (gnus-group-jump-to-group target-group)
5074         (let ((unreads (gnus-group-group-unread)))
5075           (if (and (or (eq t unreads)
5076                        (and unreads (not (zerop unreads))))
5077                    (gnus-summary-read-group
5078                     target-group nil no-article current-buffer))
5079               (setq entered t)
5080             (setq current-group target-group
5081                   target-group nil)))))))
5082
5083 (defun gnus-summary-prev-group (&optional no-article)
5084   "Exit current newsgroup and then select previous unread newsgroup.
5085 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5086   (interactive "P")
5087   (gnus-summary-next-group no-article nil t))
5088
5089 ;; Walking around summary lines.
5090
5091 (defun gnus-summary-first-subject (&optional unread)
5092   "Go to the first unread subject.
5093 If UNREAD is non-nil, go to the first unread article.
5094 Returns the article selected or nil if there are no unread articles."
5095   (interactive "P")
5096   (prog1
5097       (cond
5098        ;; Empty summary.
5099        ((null gnus-newsgroup-data)
5100         (gnus-message 3 "No articles in the group")
5101         nil)
5102        ;; Pick the first article.
5103        ((not unread)
5104         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5105         (gnus-data-number (car gnus-newsgroup-data)))
5106        ;; No unread articles.
5107        ((null gnus-newsgroup-unreads)
5108         (gnus-message 3 "No more unread articles")
5109         nil)
5110        ;; Find the first unread article.
5111        (t
5112         (let ((data gnus-newsgroup-data))
5113           (while (and data
5114                       (not (gnus-data-unread-p (car data))))
5115             (setq data (cdr data)))
5116           (when data
5117             (goto-char (gnus-data-pos (car data)))
5118             (gnus-data-number (car data))))))
5119     (gnus-summary-position-point)))
5120
5121 (defun gnus-summary-next-subject (n &optional unread dont-display)
5122   "Go to next N'th summary line.
5123 If N is negative, go to the previous N'th subject line.
5124 If UNREAD is non-nil, only unread articles are selected.
5125 The difference between N and the actual number of steps taken is
5126 returned."
5127   (interactive "p")
5128   (let ((backward (< n 0))
5129         (n (abs n)))
5130     (while (and (> n 0)
5131                 (if backward
5132                     (gnus-summary-find-prev unread)
5133                   (gnus-summary-find-next unread)))
5134       (setq n (1- n)))
5135     (when (/= 0 n)
5136       (gnus-message 7 "No more%s articles"
5137                     (if unread " unread" "")))
5138     (unless dont-display
5139       (gnus-summary-recenter)
5140       (gnus-summary-position-point))
5141     n))
5142
5143 (defun gnus-summary-next-unread-subject (n)
5144   "Go to next N'th unread summary line."
5145   (interactive "p")
5146   (gnus-summary-next-subject n t))
5147
5148 (defun gnus-summary-prev-subject (n &optional unread)
5149   "Go to previous N'th summary line.
5150 If optional argument UNREAD is non-nil, only unread article is selected."
5151   (interactive "p")
5152   (gnus-summary-next-subject (- n) unread))
5153
5154 (defun gnus-summary-prev-unread-subject (n)
5155   "Go to previous N'th unread summary line."
5156   (interactive "p")
5157   (gnus-summary-next-subject (- n) t))
5158
5159 (defun gnus-summary-goto-subject (article &optional force silent)
5160   "Go the subject line of ARTICLE.
5161 If FORCE, also allow jumping to articles not currently shown."
5162   (interactive "nArticle number: ")
5163   (let ((b (point))
5164         (data (gnus-data-find article)))
5165     ;; We read in the article if we have to.
5166     (and (not data)
5167          force
5168          (gnus-summary-insert-subject article (and (vectorp force) force) t)
5169          (setq data (gnus-data-find article)))
5170     (goto-char b)
5171     (if (not data)
5172         (progn
5173           (unless silent
5174             (gnus-message 3 "Can't find article %d" article))
5175           nil)
5176       (goto-char (gnus-data-pos data))
5177       article)))
5178
5179 ;; Walking around summary lines with displaying articles.
5180
5181 (defun gnus-summary-expand-window (&optional arg)
5182   "Make the summary buffer take up the entire Emacs frame.
5183 Given a prefix, will force an `article' buffer configuration."
5184   (interactive "P")
5185   (gnus-set-global-variables)
5186   (if arg
5187       (gnus-configure-windows 'article 'force)
5188     (gnus-configure-windows 'summary 'force)))
5189
5190 (defun gnus-summary-display-article (article &optional all-header)
5191   "Display ARTICLE in article buffer."
5192   (gnus-set-global-variables)
5193   (if (null article)
5194       nil
5195     (prog1
5196         (if gnus-summary-display-article-function
5197             (funcall gnus-summary-display-article-function article all-header)
5198           (gnus-article-prepare article all-header))
5199       (run-hooks 'gnus-select-article-hook)
5200       (when (and gnus-current-article
5201                  (not (zerop gnus-current-article)))
5202         (gnus-summary-goto-subject gnus-current-article))
5203       (gnus-summary-recenter)
5204       (when (and gnus-use-trees gnus-show-threads)
5205         (gnus-possibly-generate-tree article)
5206         (gnus-highlight-selected-tree article))
5207       ;; Successfully display article.
5208       (gnus-article-set-window-start
5209        (cdr (assq article gnus-newsgroup-bookmarks))))))
5210
5211 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5212   "Select the current article.
5213 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5214 non-nil, the article will be re-fetched even if it already present in
5215 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5216 be displayed."
5217   ;; Make sure we are in the summary buffer to work around bbdb bug.
5218   (unless (eq major-mode 'gnus-summary-mode)
5219     (set-buffer gnus-summary-buffer))
5220   (let ((article (or article (gnus-summary-article-number)))
5221         (all-headers (not (not all-headers))) ;Must be T or NIL.
5222         gnus-summary-display-article-function
5223         did)
5224     (and (not pseudo)
5225          (gnus-summary-article-pseudo-p article)
5226          (error "This is a pseudo-article."))
5227     (prog1
5228         (save-excursion
5229           (set-buffer gnus-summary-buffer)
5230           (if (or (and gnus-single-article-buffer
5231                        (or (null gnus-current-article)
5232                            (null gnus-article-current)
5233                            (null (get-buffer gnus-article-buffer))
5234                            (not (eq article (cdr gnus-article-current)))
5235                            (not (equal (car gnus-article-current)
5236                                        gnus-newsgroup-name))))
5237                   (and (not gnus-single-article-buffer)
5238                        (or (null gnus-current-article)
5239                            (not (eq gnus-current-article article))))
5240                   force)
5241               ;; The requested article is different from the current article.
5242               (prog1
5243                   (gnus-summary-display-article article all-headers)
5244                 (setq did article))
5245             (when (or all-headers gnus-show-all-headers)
5246               (gnus-article-show-all-headers))
5247             'old))
5248       (when did
5249         (gnus-article-set-window-start
5250          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5251
5252 (defun gnus-summary-set-current-mark (&optional current-mark)
5253   "Obsolete function."
5254   nil)
5255
5256 (defun gnus-summary-next-article (&optional unread subject backward push)
5257   "Select the next article.
5258 If UNREAD, only unread articles are selected.
5259 If SUBJECT, only articles with SUBJECT are selected.
5260 If BACKWARD, the previous article is selected instead of the next."
5261   (interactive "P")
5262   (gnus-set-global-variables)
5263   (cond
5264    ;; Is there such an article?
5265    ((and (gnus-summary-search-forward unread subject backward)
5266          (or (gnus-summary-display-article (gnus-summary-article-number))
5267              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5268     (gnus-summary-position-point))
5269    ;; If not, we try the first unread, if that is wanted.
5270    ((and subject
5271          gnus-auto-select-same
5272          (gnus-summary-first-unread-article))
5273     (gnus-summary-position-point)
5274     (gnus-message 6 "Wrapped"))
5275    ;; Try to get next/previous article not displayed in this group.
5276    ((and gnus-auto-extend-newsgroup
5277          (not unread) (not subject))
5278     (gnus-summary-goto-article
5279      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5280      nil t))
5281    ;; Go to next/previous group.
5282    (t
5283     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5284       (gnus-summary-jump-to-group gnus-newsgroup-name))
5285     (let ((cmd last-command-char)
5286           (point
5287            (save-excursion
5288              (set-buffer gnus-group-buffer)
5289              (point)))
5290           (group
5291            (if (eq gnus-keep-same-level 'best)
5292                (gnus-summary-best-group gnus-newsgroup-name)
5293              (gnus-summary-search-group backward gnus-keep-same-level))))
5294       ;; For some reason, the group window gets selected.  We change
5295       ;; it back.
5296       (select-window (get-buffer-window (current-buffer)))
5297       ;; Select next unread newsgroup automagically.
5298       (cond
5299        ((or (not gnus-auto-select-next)
5300             (not cmd))
5301         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5302        ((or (eq gnus-auto-select-next 'quietly)
5303             (and (eq gnus-auto-select-next 'slightly-quietly)
5304                  push)
5305             (and (eq gnus-auto-select-next 'almost-quietly)
5306                  (gnus-summary-last-article-p)))
5307         ;; Select quietly.
5308         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5309             (gnus-summary-exit)
5310           (gnus-message 7 "No more%s articles (%s)..."
5311                         (if unread " unread" "")
5312                         (if group (concat "selecting " group)
5313                           "exiting"))
5314           (gnus-summary-next-group nil group backward)))
5315        (t
5316         (when (gnus-key-press-event-p last-input-event)
5317           (gnus-summary-walk-group-buffer
5318            gnus-newsgroup-name cmd unread backward point))))))))
5319
5320 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5321   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5322                       (?\C-p (gnus-group-prev-unread-group 1))))
5323         (cursor-in-echo-area t)
5324         keve key group ended)
5325     (save-excursion
5326       (set-buffer gnus-group-buffer)
5327       (goto-char start)
5328       (setq group
5329             (if (eq gnus-keep-same-level 'best)
5330                 (gnus-summary-best-group gnus-newsgroup-name)
5331               (gnus-summary-search-group backward gnus-keep-same-level))))
5332     (while (not ended)
5333       (gnus-message
5334        5 "No more%s articles%s" (if unread " unread" "")
5335        (if (and group
5336                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5337            (format " (Type %s for %s [%s])"
5338                    (single-key-description cmd) group
5339                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5340          (format " (Type %s to exit %s)"
5341                  (single-key-description cmd)
5342                  gnus-newsgroup-name)))
5343       ;; Confirm auto selection.
5344       (setq key (car (setq keve (gnus-read-event-char))))
5345       (setq ended t)
5346       (cond
5347        ((assq key keystrokes)
5348         (let ((obuf (current-buffer)))
5349           (switch-to-buffer gnus-group-buffer)
5350           (when group
5351             (gnus-group-jump-to-group group))
5352           (eval (cadr (assq key keystrokes)))
5353           (setq group (gnus-group-group-name))
5354           (switch-to-buffer obuf))
5355         (setq ended nil))
5356        ((equal key cmd)
5357         (if (or (not group)
5358                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5359             (gnus-summary-exit)
5360           (gnus-summary-next-group nil group backward)))
5361        (t
5362         (push (cdr keve) unread-command-events))))))
5363
5364 (defun gnus-summary-next-unread-article ()
5365   "Select unread article after current one."
5366   (interactive)
5367   (gnus-summary-next-article 
5368    (or (not (eq gnus-summary-goto-unread 'never))
5369        (gnus-summary-last-article-p (gnus-summary-article-number)))
5370    (and gnus-auto-select-same
5371         (gnus-summary-article-subject))))
5372
5373 (defun gnus-summary-prev-article (&optional unread subject)
5374   "Select the article after the current one.
5375 If UNREAD is non-nil, only unread articles are selected."
5376   (interactive "P")
5377   (gnus-summary-next-article unread subject t))
5378
5379 (defun gnus-summary-prev-unread-article ()
5380   "Select unread article before current one."
5381   (interactive)
5382   (gnus-summary-prev-article
5383    (or (not (eq gnus-summary-goto-unread 'never))
5384        (gnus-summary-first-article-p (gnus-summary-article-number)))
5385    (and gnus-auto-select-same
5386         (gnus-summary-article-subject))))
5387
5388 (defun gnus-summary-next-page (&optional lines circular)
5389   "Show next page of the selected article.
5390 If at the end of the current article, select the next article.
5391 LINES says how many lines should be scrolled up.
5392
5393 If CIRCULAR is non-nil, go to the start of the article instead of
5394 selecting the next article when reaching the end of the current
5395 article."
5396   (interactive "P")
5397   (setq gnus-summary-buffer (current-buffer))
5398   (gnus-set-global-variables)
5399   (let ((article (gnus-summary-article-number))
5400         (article-window (get-buffer-window gnus-article-buffer t))
5401         (endp nil))
5402     (gnus-configure-windows 'article)
5403     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5404         (if (and (eq gnus-summary-goto-unread 'never)
5405                  (not (gnus-summary-last-article-p article)))
5406             (gnus-summary-next-article)
5407           (gnus-summary-next-unread-article))
5408       (if (or (null gnus-current-article)
5409               (null gnus-article-current)
5410               (/= article (cdr gnus-article-current))
5411               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5412           ;; Selected subject is different from current article's.
5413           (gnus-summary-display-article article)
5414         (when article-window
5415           (gnus-eval-in-buffer-window gnus-article-buffer
5416             (setq endp (gnus-article-next-page lines)))
5417           (when endp
5418             (cond (circular
5419                    (gnus-summary-beginning-of-article))
5420                   (lines
5421                    (gnus-message 3 "End of message"))
5422                   ((null lines)
5423                    (if (and (eq gnus-summary-goto-unread 'never)
5424                             (not (gnus-summary-last-article-p article)))
5425                        (gnus-summary-next-article)
5426                      (gnus-summary-next-unread-article))))))))
5427     (gnus-summary-recenter)
5428     (gnus-summary-position-point)))
5429
5430 (defun gnus-summary-prev-page (&optional lines)
5431   "Show previous page of selected article.
5432 Argument LINES specifies lines to be scrolled down."
5433   (interactive "P")
5434   (gnus-set-global-variables)
5435   (let ((article (gnus-summary-article-number))
5436         (article-window (get-buffer-window gnus-article-buffer t)))
5437     (gnus-configure-windows 'article)
5438     (if (or (null gnus-current-article)
5439             (null gnus-article-current)
5440             (/= article (cdr gnus-article-current))
5441             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5442         ;; Selected subject is different from current article's.
5443         (gnus-summary-display-article article)
5444       (gnus-summary-recenter)
5445       (when article-window
5446         (gnus-eval-in-buffer-window gnus-article-buffer
5447           (gnus-article-prev-page lines)))))
5448   (gnus-summary-position-point))
5449
5450 (defun gnus-summary-scroll-up (lines)
5451   "Scroll up (or down) one line current article.
5452 Argument LINES specifies lines to be scrolled up (or down if negative)."
5453   (interactive "p")
5454   (gnus-set-global-variables)
5455   (gnus-configure-windows 'article)
5456   (gnus-summary-show-thread)
5457   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5458     (gnus-eval-in-buffer-window gnus-article-buffer
5459       (cond ((> lines 0)
5460              (when (gnus-article-next-page lines)
5461                (gnus-message 3 "End of message")))
5462             ((< lines 0)
5463              (gnus-article-prev-page (- lines))))))
5464   (gnus-summary-recenter)
5465   (gnus-summary-position-point))
5466
5467 (defun gnus-summary-next-same-subject ()
5468   "Select next article which has the same subject as current one."
5469   (interactive)
5470   (gnus-set-global-variables)
5471   (gnus-summary-next-article nil (gnus-summary-article-subject)))
5472
5473 (defun gnus-summary-prev-same-subject ()
5474   "Select previous article which has the same subject as current one."
5475   (interactive)
5476   (gnus-set-global-variables)
5477   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
5478
5479 (defun gnus-summary-next-unread-same-subject ()
5480   "Select next unread article which has the same subject as current one."
5481   (interactive)
5482   (gnus-set-global-variables)
5483   (gnus-summary-next-article t (gnus-summary-article-subject)))
5484
5485 (defun gnus-summary-prev-unread-same-subject ()
5486   "Select previous unread article which has the same subject as current one."
5487   (interactive)
5488   (gnus-set-global-variables)
5489   (gnus-summary-prev-article t (gnus-summary-article-subject)))
5490
5491 (defun gnus-summary-first-unread-article ()
5492   "Select the first unread article.
5493 Return nil if there are no unread articles."
5494   (interactive)
5495   (gnus-set-global-variables)
5496   (prog1
5497       (when (gnus-summary-first-subject t)
5498         (gnus-summary-show-thread)
5499         (gnus-summary-first-subject t)
5500         (gnus-summary-display-article (gnus-summary-article-number)))
5501     (gnus-summary-position-point)))
5502
5503 (defun gnus-summary-first-article ()
5504   "Select the first article.
5505 Return nil if there are no articles."
5506   (interactive)
5507   (gnus-set-global-variables)
5508   (prog1
5509       (when (gnus-summary-first-subject)
5510       (gnus-summary-show-thread)
5511       (gnus-summary-first-subject)
5512       (gnus-summary-display-article (gnus-summary-article-number)))
5513     (gnus-summary-position-point)))
5514
5515 (defun gnus-summary-best-unread-article ()
5516   "Select the unread article with the highest score."
5517   (interactive)
5518   (gnus-set-global-variables)
5519   (let ((best -1000000)
5520         (data gnus-newsgroup-data)
5521         article score)
5522     (while data
5523       (and (gnus-data-unread-p (car data))
5524            (> (setq score
5525                     (gnus-summary-article-score (gnus-data-number (car data))))
5526               best)
5527            (setq best score
5528                  article (gnus-data-number (car data))))
5529       (setq data (cdr data)))
5530     (prog1
5531         (if article
5532             (gnus-summary-goto-article article)
5533           (error "No unread articles"))
5534       (gnus-summary-position-point))))
5535
5536 (defun gnus-summary-last-subject ()
5537   "Go to the last displayed subject line in the group."
5538   (let ((article (gnus-data-number (car (gnus-data-list t)))))
5539     (when article
5540       (gnus-summary-goto-subject article))))
5541
5542 (defun gnus-summary-goto-article (article &optional all-headers force)
5543   "Fetch ARTICLE and display it if it exists.
5544 If ALL-HEADERS is non-nil, no header lines are hidden."
5545   (interactive
5546    (list
5547     (string-to-int
5548      (completing-read
5549       "Article number: "
5550       (mapcar (lambda (number) (list (int-to-string number)))
5551               gnus-newsgroup-limit)))
5552     current-prefix-arg
5553     t))
5554   (prog1
5555       (if (gnus-summary-goto-subject article force)
5556           (gnus-summary-display-article article all-headers)
5557         (gnus-message 4 "Couldn't go to article %s" article) nil)
5558     (gnus-summary-position-point)))
5559
5560 (defun gnus-summary-goto-last-article ()
5561   "Go to the previously read article."
5562   (interactive)
5563   (prog1
5564       (when gnus-last-article
5565         (gnus-summary-goto-article gnus-last-article))
5566     (gnus-summary-position-point)))
5567
5568 (defun gnus-summary-pop-article (number)
5569   "Pop one article off the history and go to the previous.
5570 NUMBER articles will be popped off."
5571   (interactive "p")
5572   (let (to)
5573     (setq gnus-newsgroup-history
5574           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
5575     (if to
5576         (gnus-summary-goto-article (car to))
5577       (error "Article history empty")))
5578   (gnus-summary-position-point))
5579
5580 ;; Summary commands and functions for limiting the summary buffer.
5581
5582 (defun gnus-summary-limit-to-articles (n)
5583   "Limit the summary buffer to the next N articles.
5584 If not given a prefix, use the process marked articles instead."
5585   (interactive "P")
5586   (gnus-set-global-variables)
5587   (prog1
5588       (let ((articles (gnus-summary-work-articles n)))
5589         (setq gnus-newsgroup-processable nil)
5590         (gnus-summary-limit articles))
5591     (gnus-summary-position-point)))
5592
5593 (defun gnus-summary-pop-limit (&optional total)
5594   "Restore the previous limit.
5595 If given a prefix, remove all limits."
5596   (interactive "P")
5597   (gnus-set-global-variables)
5598   (when total 
5599     (setq gnus-newsgroup-limits
5600           (list (mapcar (lambda (h) (mail-header-number h))
5601                         gnus-newsgroup-headers))))
5602   (unless gnus-newsgroup-limits
5603     (error "No limit to pop"))
5604   (prog1
5605       (gnus-summary-limit nil 'pop)
5606     (gnus-summary-position-point)))
5607
5608 (defun gnus-summary-limit-to-subject (subject &optional header)
5609   "Limit the summary buffer to articles that have subjects that match a regexp."
5610   (interactive "sLimit to subject (regexp): ")
5611   (unless header
5612     (setq header "subject"))
5613   (when (not (equal "" subject))
5614     (prog1
5615         (let ((articles (gnus-summary-find-matching
5616                          (or header "subject") subject 'all)))
5617           (unless articles
5618             (error "Found no matches for \"%s\"" subject))
5619           (gnus-summary-limit articles))
5620       (gnus-summary-position-point))))
5621
5622 (defun gnus-summary-limit-to-author (from)
5623   "Limit the summary buffer to articles that have authors that match a regexp."
5624   (interactive "sLimit to author (regexp): ")
5625   (gnus-summary-limit-to-subject from "from"))
5626
5627 (defun gnus-summary-limit-to-age (age &optional younger-p)
5628   "Limit the summary buffer to articles that are older than (or equal) AGE days. 
5629 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
5630 articles that are younger than AGE days."
5631   (interactive "nTime in days: \nP")
5632   (prog1
5633       (let ((data gnus-newsgroup-data)
5634             (cutoff (nnmail-days-to-time age))
5635             articles d date is-younger)
5636         (while (setq d (pop data))
5637           (when (and (vectorp (gnus-data-header d))
5638                      (setq date (mail-header-date (gnus-data-header d))))
5639             (setq is-younger (nnmail-time-less
5640                               (nnmail-time-since (nnmail-date-to-time date))
5641                               cutoff))
5642             (when (if younger-p is-younger (not is-younger))
5643               (push (gnus-data-number d) articles))))
5644         (gnus-summary-limit (nreverse articles)))
5645     (gnus-summary-position-point)))
5646
5647 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
5648 (make-obsolete
5649  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
5650
5651 (defun gnus-summary-limit-to-unread (&optional all)
5652   "Limit the summary buffer to articles that are not marked as read.
5653 If ALL is non-nil, limit strictly to unread articles."
5654   (interactive "P")
5655   (if all
5656       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
5657     (gnus-summary-limit-to-marks
5658      ;; Concat all the marks that say that an article is read and have
5659      ;; those removed.
5660      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
5661            gnus-killed-mark gnus-kill-file-mark
5662            gnus-low-score-mark gnus-expirable-mark
5663            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
5664            gnus-duplicate-mark)
5665      'reverse)))
5666
5667 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
5668 (make-obsolete 'gnus-summary-delete-marked-with
5669                'gnus-summary-limit-exlude-marks)
5670
5671 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
5672   "Exclude articles that are marked with MARKS (e.g. \"DK\").
5673 If REVERSE, limit the summary buffer to articles that are marked
5674 with MARKS.  MARKS can either be a string of marks or a list of marks.
5675 Returns how many articles were removed."
5676   (interactive "sMarks: ")
5677   (gnus-summary-limit-to-marks marks t))
5678   
5679 (defun gnus-summary-limit-to-marks (marks &optional reverse)
5680   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
5681 If REVERSE (the prefix), limit the summary buffer to articles that are
5682 not marked with MARKS.  MARKS can either be a string of marks or a
5683 list of marks.
5684 Returns how many articles were removed."
5685   (interactive (list (read-string "Marks: ") current-prefix-arg))
5686   (gnus-set-global-variables)
5687   (prog1
5688       (let ((data gnus-newsgroup-data)
5689             (marks (if (listp marks) marks
5690                      (append marks nil))) ; Transform to list.
5691             articles)
5692         (while data
5693           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
5694                   (memq (gnus-data-mark (car data)) marks))
5695             (push (gnus-data-number (car data)) articles))
5696           (setq data (cdr data)))
5697         (gnus-summary-limit articles))
5698     (gnus-summary-position-point)))
5699
5700 (defun gnus-summary-limit-to-score (&optional score)
5701   "Limit to articles with score at or above SCORE."
5702   (interactive "P")
5703   (gnus-set-global-variables)
5704   (setq score (if score
5705                   (prefix-numeric-value score)
5706                 (or gnus-summary-default-score 0)))
5707   (let ((data gnus-newsgroup-data)
5708         articles)
5709     (while data
5710       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
5711                 score)
5712         (push (gnus-data-number (car data)) articles))
5713       (setq data (cdr data)))
5714     (prog1
5715         (gnus-summary-limit articles)
5716       (gnus-summary-position-point))))
5717
5718 (defun gnus-summary-limit-include-dormant ()
5719   "Display all the hidden articles that are marked as dormant."
5720   (interactive)
5721   (gnus-set-global-variables)
5722   (unless gnus-newsgroup-dormant
5723     (error "There are no dormant articles in this group"))
5724   (prog1
5725       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
5726     (gnus-summary-position-point)))
5727
5728 (defun gnus-summary-limit-exclude-dormant ()
5729   "Hide all dormant articles."
5730   (interactive)
5731   (gnus-set-global-variables)
5732   (prog1
5733       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
5734     (gnus-summary-position-point)))
5735
5736 (defun gnus-summary-limit-exclude-childless-dormant ()
5737   "Hide all dormant articles that have no children."
5738   (interactive)
5739   (gnus-set-global-variables)
5740   (let ((data (gnus-data-list t))
5741         articles d children)
5742     ;; Find all articles that are either not dormant or have
5743     ;; children.
5744     (while (setq d (pop data))
5745       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
5746                 (and (setq children 
5747                            (gnus-article-children (gnus-data-number d)))
5748                      (let (found)
5749                        (while children
5750                          (when (memq (car children) articles)
5751                            (setq children nil
5752                                  found t))
5753                          (pop children))
5754                        found)))
5755         (push (gnus-data-number d) articles)))
5756     ;; Do the limiting.
5757     (prog1
5758         (gnus-summary-limit articles)
5759       (gnus-summary-position-point))))
5760
5761 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
5762   "Mark all unread excluded articles as read.
5763 If ALL, mark even excluded ticked and dormants as read."
5764   (interactive "P")
5765   (let ((articles (gnus-sorted-complement
5766                    (sort
5767                     (mapcar (lambda (h) (mail-header-number h))
5768                             gnus-newsgroup-headers)
5769                     '<)
5770                    (sort gnus-newsgroup-limit '<)))
5771         article)
5772     (setq gnus-newsgroup-unreads nil)
5773     (if all
5774         (setq gnus-newsgroup-dormant nil
5775               gnus-newsgroup-marked nil
5776               gnus-newsgroup-reads
5777               (nconc
5778                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
5779                gnus-newsgroup-reads))
5780       (while (setq article (pop articles))
5781         (unless (or (memq article gnus-newsgroup-dormant)
5782                     (memq article gnus-newsgroup-marked))
5783           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
5784
5785 (defun gnus-summary-limit (articles &optional pop)
5786   (if pop
5787       ;; We pop the previous limit off the stack and use that.
5788       (setq articles (car gnus-newsgroup-limits)
5789             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
5790     ;; We use the new limit, so we push the old limit on the stack.
5791     (push gnus-newsgroup-limit gnus-newsgroup-limits))
5792   ;; Set the limit.
5793   (setq gnus-newsgroup-limit articles)
5794   (let ((total (length gnus-newsgroup-data))
5795         (data (gnus-data-find-list (gnus-summary-article-number)))
5796         (gnus-summary-mark-below nil)   ; Inhibit this.
5797         found)
5798     ;; This will do all the work of generating the new summary buffer
5799     ;; according to the new limit.
5800     (gnus-summary-prepare)
5801     ;; Hide any threads, possibly.
5802     (and gnus-show-threads
5803          gnus-thread-hide-subtree
5804          (gnus-summary-hide-all-threads))
5805     ;; Try to return to the article you were at, or one in the
5806     ;; neighborhood.
5807     (when data
5808       ;; We try to find some article after the current one.
5809       (while data
5810         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
5811           (setq data nil
5812                 found t))
5813         (setq data (cdr data))))
5814     (unless found
5815       ;; If there is no data, that means that we were after the last
5816       ;; article.  The same goes when we can't find any articles
5817       ;; after the current one.
5818       (goto-char (point-max))
5819       (gnus-summary-find-prev))
5820     ;; We return how many articles were removed from the summary
5821     ;; buffer as a result of the new limit.
5822     (- total (length gnus-newsgroup-data))))
5823
5824 (defsubst gnus-invisible-cut-children (threads)
5825   (let ((num 0))
5826     (while threads
5827       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
5828         (incf num))
5829       (pop threads))
5830     (< num 2)))
5831
5832 (defsubst gnus-cut-thread (thread)
5833   "Go forwards in the thread until we find an article that we want to display."
5834   (when (or (eq gnus-fetch-old-headers 'some)
5835             (eq gnus-build-sparse-threads 'some)
5836             (eq gnus-build-sparse-threads 'more))
5837     ;; Deal with old-fetched headers and sparse threads.
5838     (while (and
5839             thread
5840             (or
5841              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
5842              (gnus-summary-article-ancient-p
5843               (mail-header-number (car thread))))
5844             (progn
5845               (if (<= (length (cdr thread)) 1)
5846                   (setq thread (cadr thread))
5847                 (when (gnus-invisible-cut-children (cdr thread))
5848                   (let ((th (cdr thread)))
5849                     (while th
5850                       (if (memq (mail-header-number (caar th))
5851                                 gnus-newsgroup-limit)
5852                           (setq thread (car th)
5853                                 th nil)
5854                         (setq th (cdr th)))))))))
5855       ))
5856   thread)
5857
5858 (defun gnus-cut-threads (threads)
5859   "Cut off all uninteresting articles from the beginning of threads."
5860   (when (or (eq gnus-fetch-old-headers 'some)
5861             (eq gnus-build-sparse-threads 'some)
5862             (eq gnus-build-sparse-threads 'more))
5863     (let ((th threads))
5864       (while th
5865         (setcar th (gnus-cut-thread (car th)))
5866         (setq th (cdr th)))))
5867   ;; Remove nixed out threads.
5868   (delq nil threads))
5869
5870 (defun gnus-summary-initial-limit (&optional show-if-empty)
5871   "Figure out what the initial limit is supposed to be on group entry.
5872 This entails weeding out unwanted dormants, low-scored articles,
5873 fetch-old-headers verbiage, and so on."
5874   ;; Most groups have nothing to remove.
5875   (if (or gnus-inhibit-limiting
5876           (and (null gnus-newsgroup-dormant)
5877                (not (eq gnus-fetch-old-headers 'some))
5878                (null gnus-summary-expunge-below)
5879                (not (eq gnus-build-sparse-threads 'some))
5880                (not (eq gnus-build-sparse-threads 'more))
5881                (null gnus-thread-expunge-below)
5882                (not gnus-use-nocem)))
5883       ()                                ; Do nothing.
5884     (push gnus-newsgroup-limit gnus-newsgroup-limits)
5885     (setq gnus-newsgroup-limit nil)
5886     (mapatoms
5887      (lambda (node)
5888        (unless (car (symbol-value node))
5889          ;; These threads have no parents -- they are roots.
5890          (let ((nodes (cdr (symbol-value node)))
5891                thread)
5892            (while nodes
5893              (if (and gnus-thread-expunge-below
5894                       (< (gnus-thread-total-score (car nodes))
5895                          gnus-thread-expunge-below))
5896                  (gnus-expunge-thread (pop nodes))
5897                (setq thread (pop nodes))
5898                (gnus-summary-limit-children thread))))))
5899      gnus-newsgroup-dependencies)
5900     ;; If this limitation resulted in an empty group, we might
5901     ;; pop the previous limit and use it instead.
5902     (when (and (not gnus-newsgroup-limit)
5903                show-if-empty)
5904       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
5905     gnus-newsgroup-limit))
5906
5907 (defun gnus-summary-limit-children (thread)
5908   "Return 1 if this subthread is visible and 0 if it is not."
5909   ;; First we get the number of visible children to this thread.  This
5910   ;; is done by recursing down the thread using this function, so this
5911   ;; will really go down to a leaf article first, before slowly
5912   ;; working its way up towards the root.
5913   (when thread
5914     (let ((children
5915            (if (cdr thread)
5916                (apply '+ (mapcar 'gnus-summary-limit-children
5917                                  (cdr thread)))
5918              0))
5919           (number (mail-header-number (car thread)))
5920           score)
5921       (if (and
5922            (not (memq number gnus-newsgroup-marked))
5923            (or
5924             ;; If this article is dormant and has absolutely no visible
5925             ;; children, then this article isn't visible.
5926             (and (memq number gnus-newsgroup-dormant)
5927                  (zerop children))
5928             ;; If this is "fetch-old-headered" and there is no
5929             ;; visible children, then we don't want this article.
5930             (and (eq gnus-fetch-old-headers 'some)
5931                  (gnus-summary-article-ancient-p number)
5932                  (zerop children))
5933             ;; If this is a sparsely inserted article with no children,
5934             ;; we don't want it.
5935             (and (eq gnus-build-sparse-threads 'some)
5936                  (gnus-summary-article-sparse-p number)
5937                  (zerop children))
5938             ;; If we use expunging, and this article is really
5939             ;; low-scored, then we don't want this article.
5940             (when (and gnus-summary-expunge-below
5941                        (< (setq score
5942                                 (or (cdr (assq number gnus-newsgroup-scored))
5943                                     gnus-summary-default-score))
5944                           gnus-summary-expunge-below))
5945               ;; We increase the expunge-tally here, but that has
5946               ;; nothing to do with the limits, really.
5947               (incf gnus-newsgroup-expunged-tally)
5948               ;; We also mark as read here, if that's wanted.
5949               (when (and gnus-summary-mark-below
5950                          (< score gnus-summary-mark-below))
5951                 (setq gnus-newsgroup-unreads
5952                       (delq number gnus-newsgroup-unreads))
5953                 (if gnus-newsgroup-auto-expire
5954                     (push number gnus-newsgroup-expirable)
5955                   (push (cons number gnus-low-score-mark)
5956                         gnus-newsgroup-reads)))
5957               t)
5958             ;; Check NoCeM things.
5959             (if (and gnus-use-nocem
5960                      (gnus-nocem-unwanted-article-p
5961                       (mail-header-id (car thread))))
5962                 (progn
5963                   (setq gnus-newsgroup-reads 
5964                         (delq number gnus-newsgroup-unreads))
5965                   t))))
5966           ;; Nope, invisible article.
5967           0
5968         ;; Ok, this article is to be visible, so we add it to the limit
5969         ;; and return 1.
5970         (push number gnus-newsgroup-limit)
5971         1))))
5972
5973 (defun gnus-expunge-thread (thread)
5974   "Mark all articles in THREAD as read."
5975   (let* ((number (mail-header-number (car thread))))
5976     (incf gnus-newsgroup-expunged-tally)
5977     ;; We also mark as read here, if that's wanted.
5978     (setq gnus-newsgroup-unreads
5979           (delq number gnus-newsgroup-unreads))
5980     (if gnus-newsgroup-auto-expire
5981         (push number gnus-newsgroup-expirable)
5982       (push (cons number gnus-low-score-mark)
5983             gnus-newsgroup-reads)))
5984   ;; Go recursively through all subthreads.
5985   (mapcar 'gnus-expunge-thread (cdr thread)))
5986
5987 ;; Summary article oriented commands
5988
5989 (defun gnus-summary-refer-parent-article (n)
5990   "Refer parent article N times.
5991 If N is negative, go to ancestor -N instead.
5992 The difference between N and the number of articles fetched is returned."
5993   (interactive "p")
5994   (gnus-set-global-variables)
5995   (let ((skip 1)
5996         error header ref)
5997     (when (not (natnump n))
5998       (setq skip (abs n)
5999             n 1))
6000     (while (and (> n 0)
6001                 (not error))
6002       (setq header (gnus-summary-article-header))
6003       (if (and (eq (mail-header-number header)
6004                    (cdr gnus-article-current))
6005                (equal gnus-newsgroup-name
6006                       (car gnus-article-current)))
6007           ;; If we try to find the parent of the currently
6008           ;; displayed article, then we take a look at the actual
6009           ;; References header, since this is slightly more
6010           ;; reliable than the References field we got from the
6011           ;; server.
6012           (save-excursion
6013             (set-buffer gnus-original-article-buffer)
6014             (nnheader-narrow-to-headers)
6015             (unless (setq ref (message-fetch-field "references"))
6016               (setq ref (message-fetch-field "in-reply-to")))
6017             (widen))
6018         (setq ref
6019               ;; It's not the current article, so we take a bet on
6020               ;; the value we got from the server.
6021               (mail-header-references header)))
6022       (if (and ref
6023                (not (equal ref "")))
6024           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6025             (gnus-message 1 "Couldn't find parent"))
6026         (gnus-message 1 "No references in article %d"
6027                       (gnus-summary-article-number))
6028         (setq error t))
6029       (decf n))
6030     (gnus-summary-position-point)
6031     n))
6032
6033 (defun gnus-summary-refer-references ()
6034   "Fetch all articles mentioned in the References header.
6035 Return how many articles were fetched."
6036   (interactive)
6037   (gnus-set-global-variables)
6038   (let ((ref (mail-header-references (gnus-summary-article-header)))
6039         (current (gnus-summary-article-number))
6040         (n 0))
6041     (if (or (not ref)
6042             (equal ref ""))
6043         (error "No References in the current article")
6044       ;; For each Message-ID in the References header...
6045       (while (string-match "<[^>]*>" ref)
6046         (incf n)
6047         ;; ... fetch that article.
6048         (gnus-summary-refer-article
6049          (prog1 (match-string 0 ref)
6050            (setq ref (substring ref (match-end 0))))))
6051       (gnus-summary-goto-subject current)
6052       (gnus-summary-position-point)
6053       n)))
6054
6055 (defun gnus-summary-refer-article (message-id)
6056   "Fetch an article specified by MESSAGE-ID."
6057   (interactive "sMessage-ID: ")
6058   (when (and (stringp message-id)
6059              (not (zerop (length message-id))))
6060     ;; Construct the correct Message-ID if necessary.
6061     ;; Suggested by tale@pawl.rpi.edu.
6062     (unless (string-match "^<" message-id)
6063       (setq message-id (concat "<" message-id)))
6064     (unless (string-match ">$" message-id)
6065       (setq message-id (concat message-id ">")))
6066     (let* ((header (gnus-id-to-header message-id))
6067            (sparse (and header
6068                         (gnus-summary-article-sparse-p 
6069                          (mail-header-number header)))))
6070       (if header
6071           (prog1
6072               ;; The article is present in the buffer, to we just go to it.
6073               (gnus-summary-goto-article 
6074                (mail-header-number header) nil header)
6075             (when sparse
6076               (gnus-summary-update-article (mail-header-number header))))
6077         ;; We fetch the article
6078         (let ((gnus-override-method 
6079                (and (gnus-news-group-p gnus-newsgroup-name)
6080                     gnus-refer-article-method))
6081               number)
6082           ;; Start the special refer-article method, if necessary.
6083           (when (and gnus-refer-article-method
6084                      (gnus-news-group-p gnus-newsgroup-name))
6085             (gnus-check-server gnus-refer-article-method))
6086           ;; Fetch the header, and display the article.
6087           (if (setq number (gnus-summary-insert-subject message-id))
6088               (gnus-summary-select-article nil nil nil number)
6089             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6090
6091 (defun gnus-summary-enter-digest-group (&optional force)
6092   "Enter an nndoc group based on the current article.
6093 If FORCE, force a digest interpretation.  If not, try
6094 to guess what the document format is."
6095   (interactive "P")
6096   (gnus-set-global-variables)
6097   (let ((conf gnus-current-window-configuration))
6098     (save-excursion
6099       (gnus-summary-select-article))
6100     (setq gnus-current-window-configuration conf)
6101     (let* ((name (format "%s-%d"
6102                          (gnus-group-prefixed-name
6103                           gnus-newsgroup-name (list 'nndoc ""))
6104                          (save-excursion
6105                            (set-buffer gnus-summary-buffer)
6106                            gnus-current-article)))
6107            (ogroup gnus-newsgroup-name)
6108            (params (append (gnus-info-params (gnus-get-info ogroup))
6109                            (list (cons 'to-group ogroup))))
6110            (case-fold-search t)
6111            (buf (current-buffer))
6112            dig)
6113       (save-excursion
6114         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6115         (insert-buffer-substring gnus-original-article-buffer)
6116         ;; Remove lines that may lead nndoc to misinterpret the
6117         ;; document type.
6118         (narrow-to-region
6119          (goto-char (point-min))
6120          (or (search-forward "\n\n" nil t) (point)))
6121         (goto-char (point-min))
6122         (delete-matching-lines "^\\(Path\\):\\|^From ")
6123         (widen))
6124       (unwind-protect
6125           (if (gnus-group-read-ephemeral-group
6126                name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6127                             (nndoc-article-type 
6128                              ,(if force 'digest 'guess))) t)
6129               ;; Make all postings to this group go to the parent group.
6130               (nconc (gnus-info-params (gnus-get-info name))
6131                      params)
6132             ;; Couldn't select this doc group.
6133             (switch-to-buffer buf)
6134             (gnus-set-global-variables)
6135             (gnus-configure-windows 'summary)
6136             (gnus-message 3 "Article couldn't be entered?"))
6137         (kill-buffer dig)))))
6138
6139 (defun gnus-summary-read-document (n)
6140   "Open a new group based on the current article(s).
6141 This will allow you to read digests and other similar
6142 documents as newsgroups.
6143 Obeys the standard process/prefix convention."
6144   (interactive "P")
6145   (let* ((articles (gnus-summary-work-articles n))
6146          (ogroup gnus-newsgroup-name)
6147          (params (append (gnus-info-params (gnus-get-info ogroup))
6148                          (list (cons 'to-group ogroup))))
6149          article group egroup groups vgroup)
6150     (while (setq article (pop articles))
6151       (setq group (format "%s-%d" gnus-newsgroup-name article))
6152       (gnus-summary-remove-process-mark article)
6153       (when (gnus-summary-display-article article)
6154         (save-excursion
6155           (nnheader-temp-write nil
6156             (insert-buffer-substring gnus-original-article-buffer)
6157             ;; Remove some headers that may lead nndoc to make
6158             ;; the wrong guess.
6159             (message-narrow-to-head)
6160             (goto-char (point-min))
6161             (delete-matching-lines "^\\(Path\\):\\|^From ")
6162             (widen)
6163             (if (setq egroup
6164                       (gnus-group-read-ephemeral-group
6165                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6166                                      (nndoc-article-type guess))
6167                        t nil t))
6168                 (progn
6169                   ;; Make all postings to this group go to the parent group.
6170                   (nconc (gnus-info-params (gnus-get-info egroup))
6171                          params)
6172                   (push egroup groups))
6173               ;; Couldn't select this doc group.
6174               (gnus-error 3 "Article couldn't be entered"))))))
6175     ;; Now we have selected all the documents.
6176     (cond
6177      ((not groups)
6178       (error "None of the articles could be interpreted as documents"))
6179      ((gnus-group-read-ephemeral-group
6180        (setq vgroup (format
6181                      "nnvirtual:%s-%s" gnus-newsgroup-name
6182                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6183        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6184        t
6185        (cons (current-buffer) 'summary)))
6186      (t
6187       (error "Couldn't select virtual nndoc group")))))
6188       
6189 (defun gnus-summary-isearch-article (&optional regexp-p)
6190   "Do incremental search forward on the current article.
6191 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6192   (interactive "P")
6193   (gnus-set-global-variables)
6194   (gnus-summary-select-article)
6195   (gnus-configure-windows 'article)
6196   (gnus-eval-in-buffer-window gnus-article-buffer
6197     ;;(goto-char (point-min))
6198     (isearch-forward regexp-p)))
6199
6200 (defun gnus-summary-search-article-forward (regexp &optional backward)
6201   "Search for an article containing REGEXP forward.
6202 If BACKWARD, search backward instead."
6203   (interactive
6204    (list (read-string
6205           (format "Search article %s (regexp%s): "
6206                   (if current-prefix-arg "backward" "forward")
6207                   (if gnus-last-search-regexp
6208                       (concat ", default " gnus-last-search-regexp)
6209                     "")))
6210          current-prefix-arg))
6211   (gnus-set-global-variables)
6212   (if (string-equal regexp "")
6213       (setq regexp (or gnus-last-search-regexp ""))
6214     (setq gnus-last-search-regexp regexp))
6215   (if (gnus-summary-search-article regexp backward)
6216       (gnus-summary-show-thread)
6217     (error "Search failed: \"%s\"" regexp)))
6218
6219 (defun gnus-summary-search-article-backward (regexp)
6220   "Search for an article containing REGEXP backward."
6221   (interactive
6222    (list (read-string
6223           (format "Search article backward (regexp%s): "
6224                   (if gnus-last-search-regexp
6225                       (concat ", default " gnus-last-search-regexp)
6226                     "")))))
6227   (gnus-summary-search-article-forward regexp 'backward))
6228
6229 (defun gnus-summary-search-article (regexp &optional backward)
6230   "Search for an article containing REGEXP.
6231 Optional argument BACKWARD means do search for backward.
6232 `gnus-select-article-hook' is not called during the search."
6233   (let ((gnus-select-article-hook nil)  ;Disable hook.
6234         (gnus-article-display-hook nil)
6235         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6236         (gnus-use-article-prefetch nil)
6237         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6238         (sum (current-buffer))
6239         (found nil)
6240         point)
6241     (gnus-save-hidden-threads
6242       (gnus-summary-select-article)
6243       (set-buffer gnus-article-buffer)
6244       (when backward
6245         (forward-line -1))
6246       (while (not found)
6247         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6248         (if (if backward
6249                 (re-search-backward regexp nil t)
6250               (re-search-forward regexp nil t))
6251             ;; We found the regexp.
6252             (progn
6253               (setq found 'found)
6254               (beginning-of-line)
6255               (set-window-start
6256                (get-buffer-window (current-buffer))
6257                (point))
6258               (forward-line 1)
6259               (set-buffer sum)
6260               (setq point (point)))
6261           ;; We didn't find it, so we go to the next article.
6262           (set-buffer sum)
6263           (setq found 'not)
6264           (while (eq found 'not)
6265             (if (not (if backward (gnus-summary-find-prev)
6266                        (gnus-summary-find-next)))
6267                 ;; No more articles.
6268                 (setq found t)
6269               ;; Select the next article and adjust point.
6270               (unless (gnus-summary-article-sparse-p
6271                        (gnus-summary-article-number))
6272                 (setq found nil)
6273                 (gnus-summary-select-article)
6274                 (set-buffer gnus-article-buffer)
6275                 (widen)
6276                 (goto-char (if backward (point-max) (point-min))))))))
6277       (gnus-message 7 ""))
6278     ;; Return whether we found the regexp.
6279     (when (eq found 'found)
6280       (goto-char point)
6281       (gnus-summary-show-thread)
6282       (gnus-summary-goto-subject gnus-current-article)
6283       (gnus-summary-position-point)
6284       t)))
6285
6286 (defun gnus-summary-find-matching (header regexp &optional backward unread
6287                                           not-case-fold)
6288   "Return a list of all articles that match REGEXP on HEADER.
6289 The search stars on the current article and goes forwards unless
6290 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6291 If UNREAD is non-nil, only unread articles will
6292 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6293 in the comparisons."
6294   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6295                 (gnus-data-find-list
6296                  (gnus-summary-article-number) (gnus-data-list backward))))
6297         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
6298         (case-fold-search (not not-case-fold))
6299         articles d)
6300     (unless (fboundp (intern (concat "mail-header-" header)))
6301       (error "%s is not a valid header" header))
6302     (while data
6303       (setq d (car data))
6304       (and (or (not unread)             ; We want all articles...
6305                (gnus-data-unread-p d))  ; Or just unreads.
6306            (vectorp (gnus-data-header d)) ; It's not a pseudo.
6307            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
6308            (push (gnus-data-number d) articles)) ; Success!
6309       (setq data (cdr data)))
6310     (nreverse articles)))
6311
6312 (defun gnus-summary-execute-command (header regexp command &optional backward)
6313   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6314 If HEADER is an empty string (or nil), the match is done on the entire
6315 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
6316   (interactive
6317    (list (let ((completion-ignore-case t))
6318            (completing-read
6319             "Header name: "
6320             (mapcar (lambda (string) (list string))
6321                     '("Number" "Subject" "From" "Lines" "Date"
6322                       "Message-ID" "Xref" "References" "Body"))
6323             nil 'require-match))
6324          (read-string "Regexp: ")
6325          (read-key-sequence "Command: ")
6326          current-prefix-arg))
6327   (when (equal header "Body")
6328     (setq header ""))
6329   (gnus-set-global-variables)
6330   ;; Hidden thread subtrees must be searched as well.
6331   (gnus-summary-show-all-threads)
6332   ;; We don't want to change current point nor window configuration.
6333   (save-excursion
6334     (save-window-excursion
6335       (gnus-message 6 "Executing %s..." (key-description command))
6336       ;; We'd like to execute COMMAND interactively so as to give arguments.
6337       (gnus-execute header regexp
6338                     `(call-interactively ',(key-binding command))
6339                     backward)
6340       (gnus-message 6 "Executing %s...done" (key-description command)))))
6341
6342 (defun gnus-summary-beginning-of-article ()
6343   "Scroll the article back to the beginning."
6344   (interactive)
6345   (gnus-set-global-variables)
6346   (gnus-summary-select-article)
6347   (gnus-configure-windows 'article)
6348   (gnus-eval-in-buffer-window gnus-article-buffer
6349     (widen)
6350     (goto-char (point-min))
6351     (when gnus-break-pages
6352       (gnus-narrow-to-page))))
6353
6354 (defun gnus-summary-end-of-article ()
6355   "Scroll to the end of the article."
6356   (interactive)
6357   (gnus-set-global-variables)
6358   (gnus-summary-select-article)
6359   (gnus-configure-windows 'article)
6360   (gnus-eval-in-buffer-window gnus-article-buffer
6361     (widen)
6362     (goto-char (point-max))
6363     (recenter -3)
6364     (when gnus-break-pages
6365       (gnus-narrow-to-page))))
6366
6367 (defun gnus-summary-print-article (&optional filename)
6368   "Generate and print a PostScript image of the article buffer.
6369
6370 If the optional argument FILENAME is nil, send the image to the printer.
6371 If FILENAME is a string, save the PostScript image in a file with that
6372 name.  If FILENAME is a number, prompt the user for the name of the file
6373 to save in."
6374   (interactive (list (ps-print-preprint current-prefix-arg)))
6375   (gnus-summary-select-article)
6376   (gnus-eval-in-buffer-window gnus-article-buffer
6377     (let ((buffer (generate-new-buffer " *print*")))
6378       (unwind-protect
6379           (progn
6380             (copy-to-buffer buffer (point-min) (point-max))
6381             (set-buffer buffer)
6382             (gnus-article-delete-invisible-text)
6383             (run-hooks 'gnus-ps-print-hook)
6384             (ps-print-buffer-with-faces filename))
6385         (kill-buffer buffer)))))
6386
6387 (defun gnus-summary-show-article (&optional arg)
6388   "Force re-fetching of the current article.
6389 If ARG (the prefix) is non-nil, show the raw article without any
6390 article massaging functions being run."
6391   (interactive "P")
6392   (gnus-set-global-variables)
6393   (if (not arg)
6394       ;; Select the article the normal way.
6395       (gnus-summary-select-article nil 'force)
6396     ;; Bind the article treatment functions to nil.
6397     (let ((gnus-have-all-headers t)
6398           gnus-article-display-hook
6399           gnus-article-prepare-hook
6400           gnus-break-pages
6401           gnus-visual)
6402       (gnus-summary-select-article nil 'force)))
6403   (gnus-summary-goto-subject gnus-current-article)
6404                                         ;  (gnus-configure-windows 'article)
6405   (gnus-summary-position-point))
6406
6407 (defun gnus-summary-verbose-headers (&optional arg)
6408   "Toggle permanent full header display.
6409 If ARG is a positive number, turn header display on.
6410 If ARG is a negative number, turn header display off."
6411   (interactive "P")
6412   (gnus-set-global-variables)
6413   (setq gnus-show-all-headers
6414         (cond ((or (not (numberp arg))
6415                    (zerop arg))
6416                (not gnus-show-all-headers))
6417               ((natnump arg)
6418                t)))
6419   (gnus-summary-show-article))
6420
6421 (defun gnus-summary-toggle-header (&optional arg)
6422   "Show the headers if they are hidden, or hide them if they are shown.
6423 If ARG is a positive number, show the entire header.
6424 If ARG is a negative number, hide the unwanted header lines."
6425   (interactive "P")
6426   (gnus-set-global-variables)
6427   (save-excursion
6428     (set-buffer gnus-article-buffer)
6429     (let* ((buffer-read-only nil)
6430            (inhibit-point-motion-hooks t)
6431            (hidden (text-property-any
6432                     (goto-char (point-min)) (search-forward "\n\n")
6433                     'invisible t))
6434            e)
6435       (goto-char (point-min))
6436       (when (search-forward "\n\n" nil t)
6437         (delete-region (point-min) (1- (point))))
6438       (goto-char (point-min))
6439       (save-excursion
6440         (set-buffer gnus-original-article-buffer)
6441         (goto-char (point-min))
6442         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
6443       (insert-buffer-substring gnus-original-article-buffer 1 e)
6444       (let ((article-inhibit-hiding t))
6445         (run-hooks 'gnus-article-display-hook))
6446       (when (or (not hidden) (and (numberp arg) (< arg 0)))
6447         (gnus-article-hide-headers)))))
6448
6449 (defun gnus-summary-show-all-headers ()
6450   "Make all header lines visible."
6451   (interactive)
6452   (gnus-set-global-variables)
6453   (gnus-article-show-all-headers))
6454
6455 (defun gnus-summary-toggle-mime (&optional arg)
6456   "Toggle MIME processing.
6457 If ARG is a positive number, turn MIME processing on."
6458   (interactive "P")
6459   (gnus-set-global-variables)
6460   (setq gnus-show-mime
6461         (if (null arg) (not gnus-show-mime)
6462           (> (prefix-numeric-value arg) 0)))
6463   (gnus-summary-select-article t 'force))
6464
6465 (defun gnus-summary-caesar-message (&optional arg)
6466   "Caesar rotate the current article by 13.
6467 The numerical prefix specifies how many places to rotate each letter
6468 forward."
6469   (interactive "P")
6470   (gnus-set-global-variables)
6471   (gnus-summary-select-article)
6472   (let ((mail-header-separator ""))
6473     (gnus-eval-in-buffer-window gnus-article-buffer
6474       (save-restriction
6475         (widen)
6476         (let ((start (window-start))
6477               buffer-read-only)
6478           (message-caesar-buffer-body arg)
6479           (set-window-start (get-buffer-window (current-buffer)) start))))))
6480
6481 (defun gnus-summary-stop-page-breaking ()
6482   "Stop page breaking in the current article."
6483   (interactive)
6484   (gnus-set-global-variables)
6485   (gnus-summary-select-article)
6486   (gnus-eval-in-buffer-window gnus-article-buffer
6487     (widen)
6488     (when (gnus-visual-p 'page-marker)
6489       (let ((buffer-read-only nil))
6490         (gnus-remove-text-with-property 'gnus-prev)
6491         (gnus-remove-text-with-property 'gnus-next)))))
6492
6493 (defun gnus-summary-move-article (&optional n to-newsgroup 
6494                                             select-method action)
6495   "Move the current article to a different newsgroup.
6496 If N is a positive number, move the N next articles.
6497 If N is a negative number, move the N previous articles.
6498 If N is nil and any articles have been marked with the process mark,
6499 move those articles instead.
6500 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
6501 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
6502 re-spool using this method.
6503
6504 For this function to work, both the current newsgroup and the
6505 newsgroup that you want to move to have to support the `request-move'
6506 and `request-accept' functions."
6507   (interactive "P")
6508   (unless action
6509     (setq action 'move))
6510   (gnus-set-global-variables)
6511   ;; Disable marking as read.
6512   (let (gnus-mark-article-hook)
6513     (save-window-excursion
6514       (gnus-summary-select-article)))
6515   ;; Check whether the source group supports the required functions.
6516   (cond ((and (eq action 'move)
6517               (not (gnus-check-backend-function
6518                     'request-move-article gnus-newsgroup-name)))
6519          (error "The current group does not support article moving"))
6520         ((and (eq action 'crosspost)
6521               (not (gnus-check-backend-function
6522                     'request-replace-article gnus-newsgroup-name)))
6523          (error "The current group does not support article editing")))
6524   (let ((articles (gnus-summary-work-articles n))
6525         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
6526         (names '((move "Move" "Moving")
6527                  (copy "Copy" "Copying")
6528                  (crosspost "Crosspost" "Crossposting")))
6529         (copy-buf (save-excursion
6530                     (nnheader-set-temp-buffer " *copy article*")))
6531         art-group to-method new-xref article to-groups)
6532     (unless (assq action names)
6533       (error "Unknown action %s" action))
6534     ;; Read the newsgroup name.
6535     (when (and (not to-newsgroup)
6536                (not select-method))
6537       (setq to-newsgroup
6538             (gnus-read-move-group-name
6539              (cadr (assq action names))
6540              (symbol-value (intern (format "gnus-current-%s-group" action)))
6541              articles prefix))
6542       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
6543     (setq to-method (or select-method 
6544                         (gnus-group-name-to-method to-newsgroup)))
6545     ;; Check the method we are to move this article to...
6546     (unless (gnus-check-backend-function 
6547              'request-accept-article (car to-method))
6548       (error "%s does not support article copying" (car to-method)))
6549     (unless (gnus-check-server to-method)
6550       (error "Can't open server %s" (car to-method)))
6551     (gnus-message 6 "%s to %s: %s..."
6552                   (caddr (assq action names))
6553                   (or (car select-method) to-newsgroup) articles)
6554     (while articles
6555       (setq article (pop articles))
6556       (setq
6557        art-group
6558        (cond
6559         ;; Move the article.
6560         ((eq action 'move)
6561          (gnus-request-move-article
6562           article                       ; Article to move
6563           gnus-newsgroup-name           ; From newsgroup
6564           (nth 1 (gnus-find-method-for-group
6565                   gnus-newsgroup-name)) ; Server
6566           (list 'gnus-request-accept-article
6567                 to-newsgroup (list 'quote select-method)
6568                 (not articles))         ; Accept form
6569           (not articles)))              ; Only save nov last time
6570         ;; Copy the article.
6571         ((eq action 'copy)
6572          (save-excursion
6573            (set-buffer copy-buf)
6574            (gnus-request-article-this-buffer article gnus-newsgroup-name)
6575            (gnus-request-accept-article
6576             to-newsgroup select-method (not articles))))
6577         ;; Crosspost the article.
6578         ((eq action 'crosspost)
6579          (let ((xref (message-tokenize-header
6580                       (mail-header-xref (gnus-summary-article-header article))
6581                       " ")))
6582            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
6583                                   ":" article))
6584            (unless xref 
6585              (setq xref (list (system-name))))
6586            (setq new-xref
6587                  (concat
6588                   (mapconcat 'identity 
6589                              (delete "Xref:" (delete new-xref xref))
6590                              " ")
6591                   " " new-xref))
6592            (save-excursion
6593              (set-buffer copy-buf)
6594              ;; First put the article in the destination group.
6595              (gnus-request-article-this-buffer article gnus-newsgroup-name)
6596              (setq art-group
6597                    (gnus-request-accept-article
6598                     to-newsgroup select-method (not articles)))
6599              (setq new-xref (concat new-xref " " (car art-group)
6600                                     ":" (cdr art-group)))
6601              ;; Now we have the new Xrefs header, so we insert
6602              ;; it and replace the new article.
6603              (nnheader-replace-header "Xref" new-xref)
6604              (gnus-request-replace-article
6605               (cdr art-group) to-newsgroup (current-buffer))
6606              art-group)))))
6607       (if (not art-group)
6608           (gnus-message 1 "Couldn't %s article %s"
6609                         (cadr (assq action names)) article)
6610         (let* ((entry
6611                 (or
6612                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
6613                  (gnus-gethash
6614                   (gnus-group-prefixed-name
6615                    (car art-group)
6616                    (or select-method 
6617                        (gnus-find-method-for-group to-newsgroup)))
6618                   gnus-newsrc-hashtb)))
6619                (info (nth 2 entry))
6620                (to-group (gnus-info-group info)))
6621           ;; Update the group that has been moved to.
6622           (when (and info
6623                      (memq action '(move copy)))
6624             (unless (member to-group to-groups)
6625               (push to-group to-groups))
6626
6627             (unless (memq article gnus-newsgroup-unreads)
6628               (gnus-info-set-read
6629                info (gnus-add-to-range (gnus-info-read info)
6630                                        (list (cdr art-group)))))
6631
6632             ;; Copy any marks over to the new group.
6633             (let ((marks gnus-article-mark-lists)
6634                   (to-article (cdr art-group)))
6635
6636               ;; See whether the article is to be put in the cache.
6637               (when gnus-use-cache
6638                 (gnus-cache-possibly-enter-article
6639                  to-group to-article
6640                  (let ((header (copy-sequence
6641                                 (gnus-summary-article-header article))))
6642                    (mail-header-set-number header to-article)
6643                    header)
6644                  (memq article gnus-newsgroup-marked)
6645                  (memq article gnus-newsgroup-dormant)
6646                  (memq article gnus-newsgroup-unreads)))
6647
6648               (when (and (equal to-group gnus-newsgroup-name)
6649                          (not (memq article gnus-newsgroup-unreads)))
6650                 ;; Mark this article as read in this group.
6651                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
6652                 (setcdr (gnus-active to-group) to-article)
6653                 (setcdr gnus-newsgroup-active to-article))
6654               
6655               (while marks
6656                 (when (memq article (symbol-value
6657                                      (intern (format "gnus-newsgroup-%s"
6658                                                      (caar marks)))))
6659                   ;; If the other group is the same as this group,
6660                   ;; then we have to add the mark to the list.
6661                   (when (equal to-group gnus-newsgroup-name)
6662                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
6663                          (cons to-article
6664                                (symbol-value
6665                                 (intern (format "gnus-newsgroup-%s"
6666                                                 (caar marks)))))))
6667                   ;; Copy the marks to other group.
6668                   (gnus-add-marked-articles
6669                    to-group (cdar marks) (list to-article) info))
6670                 (setq marks (cdr marks)))))
6671
6672           ;; Update the Xref header in this article to point to
6673           ;; the new crossposted article we have just created.
6674           (when (eq action 'crosspost)
6675             (save-excursion
6676               (set-buffer copy-buf)
6677               (gnus-request-article-this-buffer article gnus-newsgroup-name)
6678               (nnheader-replace-header "Xref" new-xref)
6679               (gnus-request-replace-article
6680                article gnus-newsgroup-name (current-buffer)))))
6681
6682         (gnus-summary-goto-subject article)
6683         (when (eq action 'move)
6684           (gnus-summary-mark-article article gnus-canceled-mark)))
6685       (gnus-summary-remove-process-mark article))
6686     ;; Re-activate all groups that have been moved to.
6687     (while to-groups
6688       (gnus-activate-group (pop to-groups)))
6689     
6690     (gnus-kill-buffer copy-buf)
6691     (gnus-summary-position-point)
6692     (gnus-set-mode-line 'summary)))
6693
6694 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
6695   "Move the current article to a different newsgroup.
6696 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
6697 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
6698 re-spool using this method."
6699   (interactive "P")
6700   (gnus-summary-move-article n to-newsgroup select-method 'copy))
6701
6702 (defun gnus-summary-crosspost-article (&optional n)
6703   "Crosspost the current article to some other group."
6704   (interactive "P")
6705   (gnus-summary-move-article n nil nil 'crosspost))
6706
6707 (defcustom gnus-summary-respool-default-method nil
6708   "Default method for respooling an article.  
6709 If nil, use to the current newsgroup method."
6710   :type 'gnus-select-method-name
6711   :group 'gnus-summary-mail)
6712
6713 (defun gnus-summary-respool-article (&optional n method)
6714   "Respool the current article.
6715 The article will be squeezed through the mail spooling process again,
6716 which means that it will be put in some mail newsgroup or other
6717 depending on `nnmail-split-methods'.
6718 If N is a positive number, respool the N next articles.
6719 If N is a negative number, respool the N previous articles.
6720 If N is nil and any articles have been marked with the process mark,
6721 respool those articles instead.
6722
6723 Respooling can be done both from mail groups and \"real\" newsgroups.
6724 In the former case, the articles in question will be moved from the
6725 current group into whatever groups they are destined to.  In the
6726 latter case, they will be copied into the relevant groups."
6727   (interactive 
6728    (list current-prefix-arg
6729          (let* ((methods (gnus-methods-using 'respool))
6730                 (methname
6731                  (symbol-name (or gnus-summary-respool-default-method
6732                                   (car (gnus-find-method-for-group
6733                                         gnus-newsgroup-name)))))
6734                 (method
6735                  (gnus-completing-read 
6736                   methname "What backend do you want to use when respooling?"
6737                   methods nil t nil 'gnus-mail-method-history))
6738                 ms)
6739            (cond
6740             ((zerop (length (setq ms (gnus-servers-using-backend 
6741                                       (intern method)))))
6742              (list (intern method) ""))
6743             ((= 1 (length ms))
6744              (car ms))
6745             (t
6746              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
6747                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
6748                            ms-alist))))))))
6749   (gnus-set-global-variables)
6750   (unless method
6751     (error "No method given for respooling"))
6752   (if (assoc (symbol-name
6753               (car (gnus-find-method-for-group gnus-newsgroup-name)))
6754              (gnus-methods-using 'respool))
6755       (gnus-summary-move-article n nil method)
6756     (gnus-summary-copy-article n nil method)))
6757
6758 (defun gnus-summary-import-article (file)
6759   "Import a random file into a mail newsgroup."
6760   (interactive "fImport file: ")
6761   (gnus-set-global-variables)
6762   (let ((group gnus-newsgroup-name)
6763         (now (current-time))
6764         atts lines)
6765     (unless (gnus-check-backend-function 'request-accept-article group)
6766       (error "%s does not support article importing" group))
6767     (or (file-readable-p file)
6768         (not (file-regular-p file))
6769         (error "Can't read %s" file))
6770     (save-excursion
6771       (set-buffer (get-buffer-create " *import file*"))
6772       (buffer-disable-undo (current-buffer))
6773       (erase-buffer)
6774       (insert-file-contents file)
6775       (goto-char (point-min))
6776       (unless (nnheader-article-p)
6777         ;; This doesn't look like an article, so we fudge some headers.
6778         (setq atts (file-attributes file)
6779               lines (count-lines (point-min) (point-max)))
6780         (insert "From: " (read-string "From: ") "\n"
6781                 "Subject: " (read-string "Subject: ") "\n"
6782                 "Date: " (timezone-make-date-arpa-standard
6783                           (current-time-string (nth 5 atts))
6784                           (current-time-zone now)
6785                           (current-time-zone now))
6786                 "\n"
6787                 "Message-ID: " (message-make-message-id) "\n"
6788                 "Lines: " (int-to-string lines) "\n"
6789                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
6790       (gnus-request-accept-article group nil t)
6791       (kill-buffer (current-buffer)))))
6792
6793 (defun gnus-summary-article-posted-p ()
6794   "Say whether the current (mail) article is available from `gnus-select-method' as well.
6795 This will be the case if the article has both been mailed and posted."
6796   (interactive)
6797   (let ((id (mail-header-references (gnus-summary-article-header)))
6798         (gnus-override-method
6799          (or gnus-refer-article-method gnus-select-method)))
6800     (if (gnus-request-head id "")
6801         (gnus-message 2 "The current message was found on %s"
6802                       gnus-override-method)
6803       (gnus-message 2 "The current message couldn't be found on %s"
6804                     gnus-override-method)
6805       nil)))
6806
6807 (defun gnus-summary-expire-articles (&optional now)
6808   "Expire all articles that are marked as expirable in the current group."
6809   (interactive)
6810   (gnus-set-global-variables)
6811   (when (gnus-check-backend-function
6812          'request-expire-articles gnus-newsgroup-name)
6813     ;; This backend supports expiry.
6814     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
6815            (expirable (if total
6816                           (progn
6817                             ;; We need to update the info for
6818                             ;; this group for `gnus-list-of-read-articles'
6819                             ;; to give us the right answer.
6820                             (run-hooks 'gnus-exit-group-hook)
6821                             (gnus-summary-update-info)
6822                             (gnus-list-of-read-articles gnus-newsgroup-name))
6823                         (setq gnus-newsgroup-expirable
6824                               (sort gnus-newsgroup-expirable '<))))
6825            (expiry-wait (if now 'immediate
6826                           (gnus-group-find-parameter
6827                            gnus-newsgroup-name 'expiry-wait)))
6828            es)
6829       (when expirable
6830         ;; There are expirable articles in this group, so we run them
6831         ;; through the expiry process.
6832         (gnus-message 6 "Expiring articles...")
6833         ;; The list of articles that weren't expired is returned.
6834         (if expiry-wait
6835             (let ((nnmail-expiry-wait-function nil)
6836                   (nnmail-expiry-wait expiry-wait))
6837               (setq es (gnus-request-expire-articles
6838                         expirable gnus-newsgroup-name)))
6839           (setq es (gnus-request-expire-articles
6840                     expirable gnus-newsgroup-name)))
6841         (unless total
6842           (setq gnus-newsgroup-expirable es))
6843         ;; We go through the old list of expirable, and mark all
6844         ;; really expired articles as nonexistent.
6845         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
6846           (let ((gnus-use-cache nil))
6847             (while expirable
6848               (unless (memq (car expirable) es)
6849                 (when (gnus-data-find (car expirable))
6850                   (gnus-summary-mark-article
6851                    (car expirable) gnus-canceled-mark)))
6852               (setq expirable (cdr expirable)))))
6853         (gnus-message 6 "Expiring articles...done")))))
6854
6855 (defun gnus-summary-expire-articles-now ()
6856   "Expunge all expirable articles in the current group.
6857 This means that *all* articles that are marked as expirable will be
6858 deleted forever, right now."
6859   (interactive)
6860   (gnus-set-global-variables)
6861   (or gnus-expert-user
6862       (gnus-yes-or-no-p
6863        "Are you really, really, really sure you want to delete all these messages? ")
6864       (error "Phew!"))
6865   (gnus-summary-expire-articles t))
6866
6867 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6868 (defun gnus-summary-delete-article (&optional n)
6869   "Delete the N next (mail) articles.
6870 This command actually deletes articles.  This is not a marking
6871 command.  The article will disappear forever from your life, never to
6872 return.
6873 If N is negative, delete backwards.
6874 If N is nil and articles have been marked with the process mark,
6875 delete these instead."
6876   (interactive "P")
6877   (gnus-set-global-variables)
6878   (unless (gnus-check-backend-function 'request-expire-articles
6879                                        gnus-newsgroup-name)
6880     (error "The current newsgroup does not support article deletion."))
6881   ;; Compute the list of articles to delete.
6882   (let ((articles (gnus-summary-work-articles n))
6883         not-deleted)
6884     (if (and gnus-novice-user
6885              (not (gnus-yes-or-no-p
6886                    (format "Do you really want to delete %s forever? "
6887                            (if (> (length articles) 1)
6888                                (format "these %s articles" (length articles))
6889                              "this article")))))
6890         ()
6891       ;; Delete the articles.
6892       (setq not-deleted (gnus-request-expire-articles
6893                          articles gnus-newsgroup-name 'force))
6894       (while articles
6895         (gnus-summary-remove-process-mark (car articles))
6896         ;; The backend might not have been able to delete the article
6897         ;; after all.
6898         (unless (memq (car articles) not-deleted)
6899           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
6900         (setq articles (cdr articles)))
6901       (when not-deleted
6902         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
6903     (gnus-summary-position-point)
6904     (gnus-set-mode-line 'summary)
6905     not-deleted))
6906
6907 (defun gnus-summary-edit-article (&optional force)
6908   "Edit the current article.
6909 This will have permanent effect only in mail groups.
6910 If FORCE is non-nil, allow editing of articles even in read-only
6911 groups." 
6912   (interactive "P")
6913   (save-excursion
6914     (set-buffer gnus-summary-buffer)
6915     (gnus-set-global-variables)
6916     (when (and (not force)
6917                (gnus-group-read-only-p))
6918       (error "The current newsgroup does not support article editing."))
6919     ;; Select article if needed.
6920     (unless (eq (gnus-summary-article-number)
6921                 gnus-current-article)
6922       (gnus-summary-select-article t))
6923     (gnus-article-edit-article
6924      `(lambda ()
6925         (gnus-summary-edit-article-done
6926          ,(or (mail-header-references gnus-current-headers) "")
6927          ,(gnus-group-read-only-p) ,gnus-summary-buffer)))))
6928
6929 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
6930
6931 (defun gnus-summary-edit-article-done (&optional references read-only buffer)
6932   "Make edits to the current article permanent."
6933   (interactive)
6934   ;; Replace the article.
6935   (if (and (not read-only)
6936            (not (gnus-request-replace-article
6937                  (cdr gnus-article-current) (car gnus-article-current)
6938                  (current-buffer))))
6939       (error "Couldn't replace article.")
6940     ;; Update the summary buffer.
6941     (if (and references
6942              (equal (message-tokenize-header references " ")
6943                     (message-tokenize-header
6944                      (or (message-fetch-field "references") "") " ")))
6945         ;; We only have to update this line.
6946         (save-excursion
6947           (save-restriction
6948             (message-narrow-to-head)
6949             (let ((head (buffer-string))
6950                   header)
6951               (nnheader-temp-write nil
6952                 (insert (format "211 %d Article retrieved.\n"
6953                                 (cdr gnus-article-current)))
6954                 (insert head)
6955                 (insert ".\n")
6956                 (let ((nntp-server-buffer (current-buffer)))
6957                   (setq header (car (gnus-get-newsgroup-headers
6958                                      (save-excursion
6959                                        (set-buffer gnus-summary-buffer)
6960                                        gnus-newsgroup-dependencies)
6961                                      t))))
6962                 (save-excursion
6963                   (set-buffer gnus-summary-buffer)
6964                   (gnus-data-set-header
6965                    (gnus-data-find (cdr gnus-article-current))
6966                    header)
6967                   (gnus-summary-update-article-line
6968                    (cdr gnus-article-current) header))))))
6969       ;; Update threads.
6970       (set-buffer (or buffer gnus-summary-buffer))
6971       (gnus-summary-update-article (cdr gnus-article-current)))
6972     ;; Prettify the article buffer again.
6973     (save-excursion
6974       (set-buffer gnus-article-buffer)
6975       (run-hooks 'gnus-article-display-hook)
6976       (set-buffer gnus-original-article-buffer)
6977       (gnus-request-article
6978        (cdr gnus-article-current) (car gnus-article-current) (current-buffer)))
6979     ;; Prettify the summary buffer line.
6980     (when (gnus-visual-p 'summary-highlight 'highlight)
6981       (run-hooks 'gnus-visual-mark-article-hook))))
6982
6983 (defun gnus-summary-edit-wash (key)
6984   "Perform editing command in the article buffer."
6985   (interactive 
6986    (list
6987     (progn
6988       (message "%s" (concat (this-command-keys) "- "))
6989       (read-char))))
6990   (message "")
6991   (gnus-summary-edit-article)
6992   (execute-kbd-macro (concat (this-command-keys) key))
6993   (gnus-article-edit-done))
6994
6995 ;;; Respooling
6996
6997 (defun gnus-summary-respool-query ()
6998   "Query where the respool algorithm would put this article."
6999   (interactive)
7000   (gnus-set-global-variables)
7001   (let (gnus-mark-article-hook)
7002     (gnus-summary-select-article)
7003     (save-excursion
7004       (set-buffer gnus-original-article-buffer)
7005       (save-restriction
7006         (message-narrow-to-head)
7007         (message "This message would go to %s"
7008                  (mapconcat 'car (nnmail-article-group 'identity) ", "))))))
7009
7010 ;; Summary marking commands.
7011
7012 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7013   "Mark articles which has the same subject as read, and then select the next.
7014 If UNMARK is positive, remove any kind of mark.
7015 If UNMARK is negative, tick articles."
7016   (interactive "P")
7017   (gnus-set-global-variables)
7018   (when unmark
7019     (setq unmark (prefix-numeric-value unmark)))
7020   (let ((count
7021          (gnus-summary-mark-same-subject
7022           (gnus-summary-article-subject) unmark)))
7023     ;; Select next unread article.  If auto-select-same mode, should
7024     ;; select the first unread article.
7025     (gnus-summary-next-article t (and gnus-auto-select-same
7026                                       (gnus-summary-article-subject)))
7027     (gnus-message 7 "%d article%s marked as %s"
7028                   count (if (= count 1) " is" "s are")
7029                   (if unmark "unread" "read"))))
7030
7031 (defun gnus-summary-kill-same-subject (&optional unmark)
7032   "Mark articles which has the same subject as read.
7033 If UNMARK is positive, remove any kind of mark.
7034 If UNMARK is negative, tick articles."
7035   (interactive "P")
7036   (gnus-set-global-variables)
7037   (when unmark
7038     (setq unmark (prefix-numeric-value unmark)))
7039   (let ((count
7040          (gnus-summary-mark-same-subject
7041           (gnus-summary-article-subject) unmark)))
7042     ;; If marked as read, go to next unread subject.
7043     (when (null unmark)
7044       ;; Go to next unread subject.
7045       (gnus-summary-next-subject 1 t))
7046     (gnus-message 7 "%d articles are marked as %s"
7047                   count (if unmark "unread" "read"))))
7048
7049 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7050   "Mark articles with same SUBJECT as read, and return marked number.
7051 If optional argument UNMARK is positive, remove any kinds of marks.
7052 If optional argument UNMARK is negative, mark articles as unread instead."
7053   (let ((count 1))
7054     (save-excursion
7055       (cond
7056        ((null unmark)                   ; Mark as read.
7057         (while (and
7058                 (progn
7059                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7060                   (gnus-summary-show-thread) t)
7061                 (gnus-summary-find-subject subject))
7062           (setq count (1+ count))))
7063        ((> unmark 0)                    ; Tick.
7064         (while (and
7065                 (progn
7066                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7067                   (gnus-summary-show-thread) t)
7068                 (gnus-summary-find-subject subject))
7069           (setq count (1+ count))))
7070        (t                               ; Mark as unread.
7071         (while (and
7072                 (progn
7073                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7074                   (gnus-summary-show-thread) t)
7075                 (gnus-summary-find-subject subject))
7076           (setq count (1+ count)))))
7077       (gnus-set-mode-line 'summary)
7078       ;; Return the number of marked articles.
7079       count)))
7080
7081 (defun gnus-summary-mark-as-processable (n &optional unmark)
7082   "Set the process mark on the next N articles.
7083 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7084 the process mark instead.  The difference between N and the actual
7085 number of articles marked is returned."
7086   (interactive "p")
7087   (gnus-set-global-variables)
7088   (let ((backward (< n 0))
7089         (n (abs n)))
7090     (while (and
7091             (> n 0)
7092             (if unmark
7093                 (gnus-summary-remove-process-mark
7094                  (gnus-summary-article-number))
7095               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7096             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7097       (setq n (1- n)))
7098     (when (/= 0 n)
7099       (gnus-message 7 "No more articles"))
7100     (gnus-summary-recenter)
7101     (gnus-summary-position-point)
7102     n))
7103
7104 (defun gnus-summary-unmark-as-processable (n)
7105   "Remove the process mark from the next N articles.
7106 If N is negative, mark backward instead.  The difference between N and
7107 the actual number of articles marked is returned."
7108   (interactive "p")
7109   (gnus-set-global-variables)
7110   (gnus-summary-mark-as-processable n t))
7111
7112 (defun gnus-summary-unmark-all-processable ()
7113   "Remove the process mark from all articles."
7114   (interactive)
7115   (gnus-set-global-variables)
7116   (save-excursion
7117     (while gnus-newsgroup-processable
7118       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7119   (gnus-summary-position-point))
7120
7121 (defun gnus-summary-mark-as-expirable (n)
7122   "Mark N articles forward as expirable.
7123 If N is negative, mark backward instead.  The difference between N and
7124 the actual number of articles marked is returned."
7125   (interactive "p")
7126   (gnus-set-global-variables)
7127   (gnus-summary-mark-forward n gnus-expirable-mark))
7128
7129 (defun gnus-summary-mark-article-as-replied (article)
7130   "Mark ARTICLE replied and update the summary line."
7131   (push article gnus-newsgroup-replied)
7132   (let ((buffer-read-only nil))
7133     (when (gnus-summary-goto-subject article)
7134       (gnus-summary-update-secondary-mark article))))
7135
7136 (defun gnus-summary-set-bookmark (article)
7137   "Set a bookmark in current article."
7138   (interactive (list (gnus-summary-article-number)))
7139   (gnus-set-global-variables)
7140   (when (or (not (get-buffer gnus-article-buffer))
7141             (not gnus-current-article)
7142             (not gnus-article-current)
7143             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7144     (error "No current article selected"))
7145   ;; Remove old bookmark, if one exists.
7146   (let ((old (assq article gnus-newsgroup-bookmarks)))
7147     (when old
7148       (setq gnus-newsgroup-bookmarks
7149             (delq old gnus-newsgroup-bookmarks))))
7150   ;; Set the new bookmark, which is on the form
7151   ;; (article-number . line-number-in-body).
7152   (push
7153    (cons article
7154          (save-excursion
7155            (set-buffer gnus-article-buffer)
7156            (count-lines
7157             (min (point)
7158                  (save-excursion
7159                    (goto-char (point-min))
7160                    (search-forward "\n\n" nil t)
7161                    (point)))
7162             (point))))
7163    gnus-newsgroup-bookmarks)
7164   (gnus-message 6 "A bookmark has been added to the current article."))
7165
7166 (defun gnus-summary-remove-bookmark (article)
7167   "Remove the bookmark from the current article."
7168   (interactive (list (gnus-summary-article-number)))
7169   (gnus-set-global-variables)
7170   ;; Remove old bookmark, if one exists.
7171   (let ((old (assq article gnus-newsgroup-bookmarks)))
7172     (if old
7173         (progn
7174           (setq gnus-newsgroup-bookmarks
7175                 (delq old gnus-newsgroup-bookmarks))
7176           (gnus-message 6 "Removed bookmark."))
7177       (gnus-message 6 "No bookmark in current article."))))
7178
7179 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7180 (defun gnus-summary-mark-as-dormant (n)
7181   "Mark N articles forward as dormant.
7182 If N is negative, mark backward instead.  The difference between N and
7183 the actual number of articles marked is returned."
7184   (interactive "p")
7185   (gnus-set-global-variables)
7186   (gnus-summary-mark-forward n gnus-dormant-mark))
7187
7188 (defun gnus-summary-set-process-mark (article)
7189   "Set the process mark on ARTICLE and update the summary line."
7190   (setq gnus-newsgroup-processable
7191         (cons article
7192               (delq article gnus-newsgroup-processable)))
7193   (when (gnus-summary-goto-subject article)
7194     (gnus-summary-show-thread)
7195     (gnus-summary-update-secondary-mark article)))
7196
7197 (defun gnus-summary-remove-process-mark (article)
7198   "Remove the process mark from ARTICLE and update the summary line."
7199   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7200   (when (gnus-summary-goto-subject article)
7201     (gnus-summary-show-thread)
7202     (gnus-summary-update-secondary-mark article)))
7203
7204 (defun gnus-summary-set-saved-mark (article)
7205   "Set the process mark on ARTICLE and update the summary line."
7206   (push article gnus-newsgroup-saved)
7207   (when (gnus-summary-goto-subject article)
7208     (gnus-summary-update-secondary-mark article)))
7209
7210 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7211   "Mark N articles as read forwards.
7212 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7213 The difference between N and the actual number of articles marked is
7214 returned."
7215   (interactive "p")
7216   (gnus-set-global-variables)
7217   (let ((backward (< n 0))
7218         (gnus-summary-goto-unread
7219          (and gnus-summary-goto-unread
7220               (not (eq gnus-summary-goto-unread 'never))
7221               (not (memq mark (list gnus-unread-mark
7222                                     gnus-ticked-mark gnus-dormant-mark)))))
7223         (n (abs n))
7224         (mark (or mark gnus-del-mark)))
7225     (while (and (> n 0)
7226                 (gnus-summary-mark-article nil mark no-expire)
7227                 (zerop (gnus-summary-next-subject
7228                         (if backward -1 1)
7229                         (and gnus-summary-goto-unread
7230                              (not (eq gnus-summary-goto-unread 'never)))
7231                         t)))
7232       (setq n (1- n)))
7233     (when (/= 0 n)
7234       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7235     (gnus-summary-recenter)
7236     (gnus-summary-position-point)
7237     (gnus-set-mode-line 'summary)
7238     n))
7239
7240 (defun gnus-summary-mark-article-as-read (mark)
7241   "Mark the current article quickly as read with MARK."
7242   (let ((article (gnus-summary-article-number)))
7243     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7244     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7245     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7246     (push (cons article mark) gnus-newsgroup-reads)
7247     ;; Possibly remove from cache, if that is used.
7248     (when gnus-use-cache
7249       (gnus-cache-enter-remove-article article))
7250     ;; Allow the backend to change the mark.
7251     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7252     ;; Check for auto-expiry.
7253     (when (and gnus-newsgroup-auto-expire
7254                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
7255                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
7256                    (= mark gnus-ancient-mark)
7257                    (= mark gnus-read-mark) (= mark gnus-souped-mark)
7258                    (= mark gnus-duplicate-mark)))
7259       (setq mark gnus-expirable-mark)
7260       (push article gnus-newsgroup-expirable))
7261     ;; Set the mark in the buffer.
7262     (gnus-summary-update-mark mark 'unread)
7263     t))
7264
7265 (defun gnus-summary-mark-article-as-unread (mark)
7266   "Mark the current article quickly as unread with MARK."
7267   (let ((article (gnus-summary-article-number)))
7268     (if (< article 0)
7269         (gnus-error 1 "Unmarkable article")
7270       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7271       (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7272       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7273       (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7274       (cond ((= mark gnus-ticked-mark)
7275              (push article gnus-newsgroup-marked))
7276             ((= mark gnus-dormant-mark)
7277              (push article gnus-newsgroup-dormant))
7278             (t
7279              (push article gnus-newsgroup-unreads)))
7280       (setq gnus-newsgroup-reads
7281             (delq (assq article gnus-newsgroup-reads)
7282                   gnus-newsgroup-reads))
7283
7284       ;; See whether the article is to be put in the cache.
7285       (and gnus-use-cache
7286            (vectorp (gnus-summary-article-header article))
7287            (save-excursion
7288              (gnus-cache-possibly-enter-article
7289               gnus-newsgroup-name article
7290               (gnus-summary-article-header article)
7291               (= mark gnus-ticked-mark)
7292               (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7293
7294       ;; Fix the mark.
7295       (gnus-summary-update-mark mark 'unread))
7296     t))
7297
7298 (defun gnus-summary-mark-article (&optional article mark no-expire)
7299   "Mark ARTICLE with MARK.  MARK can be any character.
7300 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
7301 `??' (dormant) and `?E' (expirable).
7302 If MARK is nil, then the default character `?D' is used.
7303 If ARTICLE is nil, then the article on the current line will be
7304 marked."
7305   ;; The mark might be a string.
7306   (when (stringp mark)
7307     (setq mark (aref mark 0)))
7308   ;; If no mark is given, then we check auto-expiring.
7309   (and (not no-expire)
7310        gnus-newsgroup-auto-expire
7311        (or (not mark)
7312            (and (gnus-characterp mark)
7313                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
7314                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
7315                     (= mark gnus-read-mark) (= mark gnus-souped-mark)
7316                     (= mark gnus-duplicate-mark))))
7317        (setq mark gnus-expirable-mark))
7318   (let* ((mark (or mark gnus-del-mark))
7319          (article (or article (gnus-summary-article-number))))
7320     (unless article
7321       (error "No article on current line"))
7322     (if (or (= mark gnus-unread-mark)
7323             (= mark gnus-ticked-mark)
7324             (= mark gnus-dormant-mark))
7325         (gnus-mark-article-as-unread article mark)
7326       (gnus-mark-article-as-read article mark))
7327
7328     ;; See whether the article is to be put in the cache.
7329     (and gnus-use-cache
7330          (not (= mark gnus-canceled-mark))
7331          (vectorp (gnus-summary-article-header article))
7332          (save-excursion
7333            (gnus-cache-possibly-enter-article
7334             gnus-newsgroup-name article
7335             (gnus-summary-article-header article)
7336             (= mark gnus-ticked-mark)
7337             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7338
7339     (when (gnus-summary-goto-subject article nil t)
7340       (let ((buffer-read-only nil))
7341         (gnus-summary-show-thread)
7342         ;; Fix the mark.
7343         (gnus-summary-update-mark mark 'unread)
7344         t))))
7345
7346 (defun gnus-summary-update-secondary-mark (article)
7347   "Update the secondary (read, process, cache) mark."
7348   (gnus-summary-update-mark
7349    (cond ((memq article gnus-newsgroup-processable)
7350           gnus-process-mark)
7351          ((memq article gnus-newsgroup-cached)
7352           gnus-cached-mark)
7353          ((memq article gnus-newsgroup-replied)
7354           gnus-replied-mark)
7355          ((memq article gnus-newsgroup-saved)
7356           gnus-saved-mark)
7357          (t gnus-unread-mark))
7358    'replied)
7359   (when (gnus-visual-p 'summary-highlight 'highlight)
7360     (run-hooks 'gnus-summary-update-hook))
7361   t)
7362
7363 (defun gnus-summary-update-mark (mark type)
7364   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
7365         (buffer-read-only nil))
7366     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
7367     (when (looking-at "\r")
7368       (incf forward))
7369     (when (and forward
7370                (<= (+ forward (point)) (point-max)))
7371       ;; Go to the right position on the line.
7372       (goto-char (+ forward (point)))
7373       ;; Replace the old mark with the new mark.
7374       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
7375       ;; Optionally update the marks by some user rule.
7376       (when (eq type 'unread)
7377         (gnus-data-set-mark
7378          (gnus-data-find (gnus-summary-article-number)) mark)
7379         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
7380
7381 (defun gnus-mark-article-as-read (article &optional mark)
7382   "Enter ARTICLE in the pertinent lists and remove it from others."
7383   ;; Make the article expirable.
7384   (let ((mark (or mark gnus-del-mark)))
7385     (if (= mark gnus-expirable-mark)
7386         (push article gnus-newsgroup-expirable)
7387       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
7388     ;; Remove from unread and marked lists.
7389     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7390     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7391     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7392     (push (cons article mark) gnus-newsgroup-reads)
7393     ;; Possibly remove from cache, if that is used.
7394     (when gnus-use-cache
7395       (gnus-cache-enter-remove-article article))))
7396
7397 (defun gnus-mark-article-as-unread (article &optional mark)
7398   "Enter ARTICLE in the pertinent lists and remove it from others."
7399   (let ((mark (or mark gnus-ticked-mark)))
7400     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
7401           gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
7402           gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
7403           gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7404
7405     ;; Unsuppress duplicates?
7406     (when gnus-suppress-duplicates
7407       (gnus-dup-unsuppress-article article))
7408
7409     (cond ((= mark gnus-ticked-mark)
7410            (push article gnus-newsgroup-marked))
7411           ((= mark gnus-dormant-mark)
7412            (push article gnus-newsgroup-dormant))
7413           (t
7414            (push article gnus-newsgroup-unreads)))
7415     (setq gnus-newsgroup-reads
7416           (delq (assq article gnus-newsgroup-reads)
7417                 gnus-newsgroup-reads))))
7418
7419 (defalias 'gnus-summary-mark-as-unread-forward
7420   'gnus-summary-tick-article-forward)
7421 (make-obsolete 'gnus-summary-mark-as-unread-forward
7422                'gnus-summary-tick-article-forward)
7423 (defun gnus-summary-tick-article-forward (n)
7424   "Tick N articles forwards.
7425 If N is negative, tick backwards instead.
7426 The difference between N and the number of articles ticked is returned."
7427   (interactive "p")
7428   (gnus-summary-mark-forward n gnus-ticked-mark))
7429
7430 (defalias 'gnus-summary-mark-as-unread-backward
7431   'gnus-summary-tick-article-backward)
7432 (make-obsolete 'gnus-summary-mark-as-unread-backward
7433                'gnus-summary-tick-article-backward)
7434 (defun gnus-summary-tick-article-backward (n)
7435   "Tick N articles backwards.
7436 The difference between N and the number of articles ticked is returned."
7437   (interactive "p")
7438   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
7439
7440 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
7441 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
7442 (defun gnus-summary-tick-article (&optional article clear-mark)
7443   "Mark current article as unread.
7444 Optional 1st argument ARTICLE specifies article number to be marked as unread.
7445 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
7446   (interactive)
7447   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
7448                                        gnus-ticked-mark)))
7449
7450 (defun gnus-summary-mark-as-read-forward (n)
7451   "Mark N articles as read forwards.
7452 If N is negative, mark backwards instead.
7453 The difference between N and the actual number of articles marked is
7454 returned."
7455   (interactive "p")
7456   (gnus-summary-mark-forward n gnus-del-mark t))
7457
7458 (defun gnus-summary-mark-as-read-backward (n)
7459   "Mark the N articles as read backwards.
7460 The difference between N and the actual number of articles marked is
7461 returned."
7462   (interactive "p")
7463   (gnus-summary-mark-forward (- n) gnus-del-mark t))
7464
7465 (defun gnus-summary-mark-as-read (&optional article mark)
7466   "Mark current article as read.
7467 ARTICLE specifies the article to be marked as read.
7468 MARK specifies a string to be inserted at the beginning of the line."
7469   (gnus-summary-mark-article article mark))
7470
7471 (defun gnus-summary-clear-mark-forward (n)
7472   "Clear marks from N articles forward.
7473 If N is negative, clear backward instead.
7474 The difference between N and the number of marks cleared is returned."
7475   (interactive "p")
7476   (gnus-summary-mark-forward n gnus-unread-mark))
7477
7478 (defun gnus-summary-clear-mark-backward (n)
7479   "Clear marks from N articles backward.
7480 The difference between N and the number of marks cleared is returned."
7481   (interactive "p")
7482   (gnus-summary-mark-forward (- n) gnus-unread-mark))
7483
7484 (defun gnus-summary-mark-unread-as-read ()
7485   "Intended to be used by `gnus-summary-mark-article-hook'."
7486   (when (memq gnus-current-article gnus-newsgroup-unreads)
7487     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
7488
7489 (defun gnus-summary-mark-read-and-unread-as-read ()
7490   "Intended to be used by `gnus-summary-mark-article-hook'."
7491   (let ((mark (gnus-summary-article-mark)))
7492     (when (or (gnus-unread-mark-p mark)
7493               (gnus-read-mark-p mark))
7494       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
7495
7496 (defun gnus-summary-mark-region-as-read (point mark all)
7497   "Mark all unread articles between point and mark as read.
7498 If given a prefix, mark all articles between point and mark as read,
7499 even ticked and dormant ones."
7500   (interactive "r\nP")
7501   (save-excursion
7502     (let (article)
7503       (goto-char point)
7504       (beginning-of-line)
7505       (while (and
7506               (< (point) mark)
7507               (progn
7508                 (when (or all
7509                           (memq (setq article (gnus-summary-article-number))
7510                                 gnus-newsgroup-unreads))
7511                   (gnus-summary-mark-article article gnus-del-mark))
7512                 t)
7513               (gnus-summary-find-next))))))
7514
7515 (defun gnus-summary-mark-below (score mark)
7516   "Mark articles with score less than SCORE with MARK."
7517   (interactive "P\ncMark: ")
7518   (gnus-set-global-variables)
7519   (setq score (if score
7520                   (prefix-numeric-value score)
7521                 (or gnus-summary-default-score 0)))
7522   (save-excursion
7523     (set-buffer gnus-summary-buffer)
7524     (goto-char (point-min))
7525     (while 
7526         (progn
7527           (and (< (gnus-summary-article-score) score)
7528                (gnus-summary-mark-article nil mark))
7529           (gnus-summary-find-next)))))
7530
7531 (defun gnus-summary-kill-below (&optional score)
7532   "Mark articles with score below SCORE as read."
7533   (interactive "P")
7534   (gnus-set-global-variables)
7535   (gnus-summary-mark-below score gnus-killed-mark))
7536
7537 (defun gnus-summary-clear-above (&optional score)
7538   "Clear all marks from articles with score above SCORE."
7539   (interactive "P")
7540   (gnus-set-global-variables)
7541   (gnus-summary-mark-above score gnus-unread-mark))
7542
7543 (defun gnus-summary-tick-above (&optional score)
7544   "Tick all articles with score above SCORE."
7545   (interactive "P")
7546   (gnus-set-global-variables)
7547   (gnus-summary-mark-above score gnus-ticked-mark))
7548
7549 (defun gnus-summary-mark-above (score mark)
7550   "Mark articles with score over SCORE with MARK."
7551   (interactive "P\ncMark: ")
7552   (gnus-set-global-variables)
7553   (setq score (if score
7554                   (prefix-numeric-value score)
7555                 (or gnus-summary-default-score 0)))
7556   (save-excursion
7557     (set-buffer gnus-summary-buffer)
7558     (goto-char (point-min))
7559     (while (and (progn
7560                   (when (> (gnus-summary-article-score) score)
7561                     (gnus-summary-mark-article nil mark))
7562                   t)
7563                 (gnus-summary-find-next)))))
7564
7565 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7566 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
7567 (defun gnus-summary-limit-include-expunged (&optional no-error)
7568   "Display all the hidden articles that were expunged for low scores."
7569   (interactive)
7570   (gnus-set-global-variables)
7571   (let ((buffer-read-only nil))
7572     (let ((scored gnus-newsgroup-scored)
7573           headers h)
7574       (while scored
7575         (unless (gnus-summary-goto-subject (caar scored))
7576           (and (setq h (gnus-summary-article-header (caar scored)))
7577                (< (cdar scored) gnus-summary-expunge-below)
7578                (push h headers)))
7579         (setq scored (cdr scored)))
7580       (if (not headers)
7581           (when (not no-error)
7582             (error "No expunged articles hidden."))
7583         (goto-char (point-min))
7584         (gnus-summary-prepare-unthreaded (nreverse headers))
7585         (goto-char (point-min))
7586         (gnus-summary-position-point)
7587         t))))
7588
7589 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
7590   "Mark all unread articles in this newsgroup as read.
7591 If prefix argument ALL is non-nil, ticked and dormant articles will 
7592 also be marked as read.
7593 If QUIETLY is non-nil, no questions will be asked.
7594 If TO-HERE is non-nil, it should be a point in the buffer.  All
7595 articles before this point will be marked as read.
7596 Note that this function will only catch up the unread article
7597 in the current summary buffer limitation.
7598 The number of articles marked as read is returned."
7599   (interactive "P")
7600   (gnus-set-global-variables)
7601   (prog1
7602       (save-excursion
7603         (when (or quietly
7604                   (not gnus-interactive-catchup) ;Without confirmation?
7605                   gnus-expert-user
7606                   (gnus-y-or-n-p
7607                    (if all
7608                        "Mark absolutely all articles as read? "
7609                      "Mark all unread articles as read? ")))
7610           (if (and not-mark
7611                    (not gnus-newsgroup-adaptive)
7612                    (not gnus-newsgroup-auto-expire)
7613                    (not gnus-suppress-duplicates))
7614               (progn
7615                 (when all
7616                   (setq gnus-newsgroup-marked nil
7617                         gnus-newsgroup-dormant nil))
7618                 (setq gnus-newsgroup-unreads nil))
7619             ;; We actually mark all articles as canceled, which we
7620             ;; have to do when using auto-expiry or adaptive scoring.
7621             (gnus-summary-show-all-threads)
7622             (when (gnus-summary-first-subject (not all))
7623               (while (and
7624                       (if to-here (< (point) to-here) t)
7625                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
7626                       (gnus-summary-find-next (not all)))))
7627             (gnus-set-mode-line 'summary))
7628           t))
7629     (gnus-summary-position-point)))
7630
7631 (defun gnus-summary-catchup-to-here (&optional all)
7632   "Mark all unticked articles before the current one as read.
7633 If ALL is non-nil, also mark ticked and dormant articles as read."
7634   (interactive "P")
7635   (gnus-set-global-variables)
7636   (save-excursion
7637     (gnus-save-hidden-threads
7638       (let ((beg (point)))
7639         ;; We check that there are unread articles.
7640         (when (or all (gnus-summary-find-prev))
7641           (gnus-summary-catchup all t beg)))))
7642   (gnus-summary-position-point))
7643
7644 (defun gnus-summary-catchup-all (&optional quietly)
7645   "Mark all articles in this newsgroup as read."
7646   (interactive "P")
7647   (gnus-set-global-variables)
7648   (gnus-summary-catchup t quietly))
7649
7650 (defun gnus-summary-catchup-and-exit (&optional all quietly)
7651   "Mark all articles not marked as unread in this newsgroup as read, then exit.
7652 If prefix argument ALL is non-nil, all articles are marked as read."
7653   (interactive "P")
7654   (gnus-set-global-variables)
7655   (when (gnus-summary-catchup all quietly nil 'fast)
7656     ;; Select next newsgroup or exit.
7657     (if (eq gnus-auto-select-next 'quietly)
7658         (gnus-summary-next-group nil)
7659       (gnus-summary-exit))))
7660
7661 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
7662   "Mark all articles in this newsgroup as read, and then exit."
7663   (interactive "P")
7664   (gnus-set-global-variables)
7665   (gnus-summary-catchup-and-exit t quietly))
7666
7667 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
7668 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
7669   "Mark all articles in this group as read and select the next group.
7670 If given a prefix, mark all articles, unread as well as ticked, as
7671 read."
7672   (interactive "P")
7673   (gnus-set-global-variables)
7674   (save-excursion
7675     (gnus-summary-catchup all))
7676   (gnus-summary-next-article t nil nil t))
7677
7678 ;; Thread-based commands.
7679
7680 (defun gnus-summary-articles-in-thread (&optional article)
7681   "Return a list of all articles in the current thread.
7682 If ARTICLE is non-nil, return all articles in the thread that starts
7683 with that article."
7684   (let* ((article (or article (gnus-summary-article-number)))
7685          (data (gnus-data-find-list article))
7686          (top-level (gnus-data-level (car data)))
7687          (top-subject
7688           (cond ((null gnus-thread-operation-ignore-subject)
7689                  (gnus-simplify-subject-re
7690                   (mail-header-subject (gnus-data-header (car data)))))
7691                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
7692                  (gnus-simplify-subject-fuzzy
7693                   (mail-header-subject (gnus-data-header (car data)))))
7694                 (t nil)))
7695          (end-point (save-excursion
7696                       (if (gnus-summary-go-to-next-thread)
7697                           (point) (point-max))))
7698          articles)
7699     (while (and data
7700                 (< (gnus-data-pos (car data)) end-point))
7701       (when (or (not top-subject)
7702                 (string= top-subject
7703                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
7704                              (gnus-simplify-subject-fuzzy
7705                               (mail-header-subject
7706                                (gnus-data-header (car data))))
7707                            (gnus-simplify-subject-re
7708                             (mail-header-subject
7709                              (gnus-data-header (car data)))))))
7710         (push (gnus-data-number (car data)) articles))
7711       (unless (and (setq data (cdr data))
7712                    (> (gnus-data-level (car data)) top-level))
7713         (setq data nil)))
7714     ;; Return the list of articles.
7715     (nreverse articles)))
7716
7717 (defun gnus-summary-rethread-current ()
7718   "Rethread the thread the current article is part of."
7719   (interactive)
7720   (gnus-set-global-variables)
7721   (let* ((gnus-show-threads t)
7722          (article (gnus-summary-article-number))
7723          (id (mail-header-id (gnus-summary-article-header)))
7724          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
7725     (unless id
7726       (error "No article on the current line"))
7727     (gnus-rebuild-thread id)
7728     (gnus-summary-goto-subject article)))
7729
7730 (defun gnus-summary-reparent-thread ()
7731   "Make the current article child of the marked (or previous) article.
7732
7733 Note that the re-threading will only work if `gnus-thread-ignore-subject'
7734 is non-nil or the Subject: of both articles are the same."
7735   (interactive)
7736   (unless (not (gnus-group-read-only-p))
7737     (error "The current newsgroup does not support article editing."))
7738   (unless (<= (length gnus-newsgroup-processable) 1)
7739     (error "No more than one article may be marked."))
7740   (save-window-excursion
7741     (let ((gnus-article-buffer " *reparent*")
7742           (current-article (gnus-summary-article-number))
7743           ;; First grab the marked article, otherwise one line up.
7744           (parent-article (if (not (null gnus-newsgroup-processable))
7745                               (car gnus-newsgroup-processable)
7746                             (save-excursion
7747                               (if (eq (forward-line -1) 0)
7748                                   (gnus-summary-article-number)
7749                                 (error "Beginning of summary buffer."))))))
7750       (unless (not (eq current-article parent-article))
7751         (error "An article may not be self-referential."))
7752       (let ((message-id (mail-header-id 
7753                          (gnus-summary-article-header parent-article))))
7754         (unless (and message-id (not (equal message-id "")))
7755           (error "No message-id in desired parent."))
7756         (gnus-summary-select-article t t nil current-article)
7757         (set-buffer gnus-original-article-buffer)
7758         (let ((buf (format "%s" (buffer-string))))
7759           (nnheader-temp-write nil
7760             (insert buf)
7761             (goto-char (point-min))
7762             (if (search-forward-regexp "^References: " nil t)
7763                 (insert message-id " " )
7764               (insert "References: " message-id "\n"))
7765             (unless (gnus-request-replace-article
7766                      current-article (car gnus-article-current)
7767                      (current-buffer))
7768               (error "Couldn't replace article."))))
7769         (set-buffer gnus-summary-buffer)
7770         (gnus-summary-unmark-all-processable)
7771         (gnus-summary-rethread-current)
7772         (gnus-message 3 "Article %d is now the child of article %d."
7773                       current-article parent-article)))))
7774
7775 (defun gnus-summary-toggle-threads (&optional arg)
7776   "Toggle showing conversation threads.
7777 If ARG is positive number, turn showing conversation threads on."
7778   (interactive "P")
7779   (gnus-set-global-variables)
7780   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
7781     (setq gnus-show-threads
7782           (if (null arg) (not gnus-show-threads)
7783             (> (prefix-numeric-value arg) 0)))
7784     (gnus-summary-prepare)
7785     (gnus-summary-goto-subject current)
7786     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
7787     (gnus-summary-position-point)))
7788
7789 (defun gnus-summary-show-all-threads ()
7790   "Show all threads."
7791   (interactive)
7792   (gnus-set-global-variables)
7793   (save-excursion
7794     (let ((buffer-read-only nil))
7795       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
7796   (gnus-summary-position-point))
7797
7798 (defun gnus-summary-show-thread ()
7799   "Show thread subtrees.
7800 Returns nil if no thread was there to be shown."
7801   (interactive)
7802   (gnus-set-global-variables)
7803   (let ((buffer-read-only nil)
7804         (orig (point))
7805         ;; first goto end then to beg, to have point at beg after let
7806         (end (progn (end-of-line) (point)))
7807         (beg (progn (beginning-of-line) (point))))
7808     (prog1
7809         ;; Any hidden lines here?
7810         (search-forward "\r" end t)
7811       (subst-char-in-region beg end ?\^M ?\n t)
7812       (goto-char orig)
7813       (gnus-summary-position-point))))
7814
7815 (defun gnus-summary-hide-all-threads ()
7816   "Hide all thread subtrees."
7817   (interactive)
7818   (gnus-set-global-variables)
7819   (save-excursion
7820     (goto-char (point-min))
7821     (gnus-summary-hide-thread)
7822     (while (zerop (gnus-summary-next-thread 1 t))
7823       (gnus-summary-hide-thread)))
7824   (gnus-summary-position-point))
7825
7826 (defun gnus-summary-hide-thread ()
7827   "Hide thread subtrees.
7828 Returns nil if no threads were there to be hidden."
7829   (interactive)
7830   (gnus-set-global-variables)
7831   (let ((buffer-read-only nil)
7832         (start (point))
7833         (article (gnus-summary-article-number)))
7834     (goto-char start)
7835     ;; Go forward until either the buffer ends or the subthread
7836     ;; ends.
7837     (when (and (not (eobp))
7838                (or (zerop (gnus-summary-next-thread 1 t))
7839                    (goto-char (point-max))))
7840       (prog1
7841           (if (and (> (point) start)
7842                    (search-backward "\n" start t))
7843               (progn
7844                 (subst-char-in-region start (point) ?\n ?\^M)
7845                 (gnus-summary-goto-subject article))
7846             (goto-char start)
7847             nil)
7848         ;;(gnus-summary-position-point)
7849         ))))
7850
7851 (defun gnus-summary-go-to-next-thread (&optional previous)
7852   "Go to the same level (or less) next thread.
7853 If PREVIOUS is non-nil, go to previous thread instead.
7854 Return the article number moved to, or nil if moving was impossible."
7855   (let ((level (gnus-summary-thread-level))
7856         (way (if previous -1 1))
7857         (beg (point)))
7858     (forward-line way)
7859     (while (and (not (eobp))
7860                 (< level (gnus-summary-thread-level)))
7861       (forward-line way))
7862     (if (eobp)
7863         (progn
7864           (goto-char beg)
7865           nil)
7866       (setq beg (point))
7867       (prog1
7868           (gnus-summary-article-number)
7869         (goto-char beg)))))
7870
7871 (defun gnus-summary-next-thread (n &optional silent)
7872   "Go to the same level next N'th thread.
7873 If N is negative, search backward instead.
7874 Returns the difference between N and the number of skips actually
7875 done.
7876
7877 If SILENT, don't output messages."
7878   (interactive "p")
7879   (gnus-set-global-variables)
7880   (let ((backward (< n 0))
7881         (n (abs n)))
7882     (while (and (> n 0)
7883                 (gnus-summary-go-to-next-thread backward))
7884       (decf n))
7885     (unless silent 
7886       (gnus-summary-position-point))
7887     (when (and (not silent) (/= 0 n))
7888       (gnus-message 7 "No more threads"))
7889     n))
7890
7891 (defun gnus-summary-prev-thread (n)
7892   "Go to the same level previous N'th thread.
7893 Returns the difference between N and the number of skips actually
7894 done."
7895   (interactive "p")
7896   (gnus-set-global-variables)
7897   (gnus-summary-next-thread (- n)))
7898
7899 (defun gnus-summary-go-down-thread ()
7900   "Go down one level in the current thread."
7901   (let ((children (gnus-summary-article-children)))
7902     (when children
7903       (gnus-summary-goto-subject (car children)))))
7904
7905 (defun gnus-summary-go-up-thread ()
7906   "Go up one level in the current thread."
7907   (let ((parent (gnus-summary-article-parent)))
7908     (when parent
7909       (gnus-summary-goto-subject parent))))
7910
7911 (defun gnus-summary-down-thread (n)
7912   "Go down thread N steps.
7913 If N is negative, go up instead.
7914 Returns the difference between N and how many steps down that were
7915 taken."
7916   (interactive "p")
7917   (gnus-set-global-variables)
7918   (let ((up (< n 0))
7919         (n (abs n)))
7920     (while (and (> n 0)
7921                 (if up (gnus-summary-go-up-thread)
7922                   (gnus-summary-go-down-thread)))
7923       (setq n (1- n)))
7924     (gnus-summary-position-point)
7925     (when (/= 0 n)
7926       (gnus-message 7 "Can't go further"))
7927     n))
7928
7929 (defun gnus-summary-up-thread (n)
7930   "Go up thread N steps.
7931 If N is negative, go up instead.
7932 Returns the difference between N and how many steps down that were
7933 taken."
7934   (interactive "p")
7935   (gnus-set-global-variables)
7936   (gnus-summary-down-thread (- n)))
7937
7938 (defun gnus-summary-top-thread ()
7939   "Go to the top of the thread."
7940   (interactive)
7941   (gnus-set-global-variables)
7942   (while (gnus-summary-go-up-thread))
7943   (gnus-summary-article-number))
7944
7945 (defun gnus-summary-kill-thread (&optional unmark)
7946   "Mark articles under current thread as read.
7947 If the prefix argument is positive, remove any kinds of marks.
7948 If the prefix argument is negative, tick articles instead."
7949   (interactive "P")
7950   (gnus-set-global-variables)
7951   (when unmark
7952     (setq unmark (prefix-numeric-value unmark)))
7953   (let ((articles (gnus-summary-articles-in-thread)))
7954     (save-excursion
7955       ;; Expand the thread.
7956       (gnus-summary-show-thread)
7957       ;; Mark all the articles.
7958       (while articles
7959         (gnus-summary-goto-subject (car articles))
7960         (cond ((null unmark)
7961                (gnus-summary-mark-article-as-read gnus-killed-mark))
7962               ((> unmark 0)
7963                (gnus-summary-mark-article-as-unread gnus-unread-mark))
7964               (t
7965                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
7966         (setq articles (cdr articles))))
7967     ;; Hide killed subtrees.
7968     (and (null unmark)
7969          gnus-thread-hide-killed
7970          (gnus-summary-hide-thread))
7971     ;; If marked as read, go to next unread subject.
7972     (when (null unmark)
7973       ;; Go to next unread subject.
7974       (gnus-summary-next-subject 1 t)))
7975   (gnus-set-mode-line 'summary))
7976
7977 ;; Summary sorting commands
7978
7979 (defun gnus-summary-sort-by-number (&optional reverse)
7980   "Sort the summary buffer by article number.
7981 Argument REVERSE means reverse order."
7982   (interactive "P")
7983   (gnus-summary-sort 'number reverse))
7984
7985 (defun gnus-summary-sort-by-author (&optional reverse)
7986   "Sort the summary buffer by author name alphabetically.
7987 If case-fold-search is non-nil, case of letters is ignored.
7988 Argument REVERSE means reverse order."
7989   (interactive "P")
7990   (gnus-summary-sort 'author reverse))
7991
7992 (defun gnus-summary-sort-by-subject (&optional reverse)
7993   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
7994 If case-fold-search is non-nil, case of letters is ignored.
7995 Argument REVERSE means reverse order."
7996   (interactive "P")
7997   (gnus-summary-sort 'subject reverse))
7998
7999 (defun gnus-summary-sort-by-date (&optional reverse)
8000   "Sort the summary buffer by date.
8001 Argument REVERSE means reverse order."
8002   (interactive "P")
8003   (gnus-summary-sort 'date reverse))
8004
8005 (defun gnus-summary-sort-by-score (&optional reverse)
8006   "Sort the summary buffer by score.
8007 Argument REVERSE means reverse order."
8008   (interactive "P")
8009   (gnus-summary-sort 'score reverse))
8010
8011 (defun gnus-summary-sort-by-lines (&optional reverse)
8012   "Sort the summary buffer by article length.
8013 Argument REVERSE means reverse order."
8014   (interactive "P")
8015   (gnus-summary-sort 'lines reverse))
8016
8017 (defun gnus-summary-sort (predicate reverse)
8018   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8019   (gnus-set-global-variables)
8020   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8021          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8022          (gnus-thread-sort-functions
8023           (list
8024            (if (not reverse)
8025                thread
8026              `(lambda (t1 t2)
8027                 (,thread t2 t1)))))
8028          (gnus-article-sort-functions
8029           (list
8030            (if (not reverse)
8031                article
8032              `(lambda (t1 t2)
8033                 (,article t2 t1)))))
8034          (buffer-read-only)
8035          (gnus-summary-prepare-hook nil))
8036     ;; We do the sorting by regenerating the threads.
8037     (gnus-summary-prepare)
8038     ;; Hide subthreads if needed.
8039     (when (and gnus-show-threads gnus-thread-hide-subtree)
8040       (gnus-summary-hide-all-threads))))
8041
8042 ;; Summary saving commands.
8043
8044 (defun gnus-summary-save-article (&optional n not-saved)
8045   "Save the current article using the default saver function.
8046 If N is a positive number, save the N next articles.
8047 If N is a negative number, save the N previous articles.
8048 If N is nil and any articles have been marked with the process mark,
8049 save those articles instead.
8050 The variable `gnus-default-article-saver' specifies the saver function."
8051   (interactive "P")
8052   (gnus-set-global-variables)
8053   (let* ((articles (gnus-summary-work-articles n))
8054          (save-buffer (save-excursion 
8055                         (nnheader-set-temp-buffer " *Gnus Save*")))
8056          (num (length articles))
8057          header article file)
8058     (while articles
8059       (setq header (gnus-summary-article-header
8060                     (setq article (pop articles))))
8061       (if (not (vectorp header))
8062           ;; This is a pseudo-article.
8063           (if (assq 'name header)
8064               (gnus-copy-file (cdr (assq 'name header)))
8065             (gnus-message 1 "Article %d is unsaveable" article))
8066         ;; This is a real article.
8067         (save-window-excursion
8068           (gnus-summary-select-article t nil nil article))
8069         (save-excursion
8070           (set-buffer save-buffer)
8071           (erase-buffer)
8072           (insert-buffer-substring gnus-original-article-buffer))
8073         (setq file (gnus-article-save save-buffer file num))
8074         (gnus-summary-remove-process-mark article)
8075         (unless not-saved
8076           (gnus-summary-set-saved-mark article))))
8077     (gnus-kill-buffer save-buffer)
8078     (gnus-summary-position-point)
8079     (gnus-set-mode-line 'summary)
8080     n))
8081
8082 (defun gnus-summary-pipe-output (&optional arg)
8083   "Pipe the current article to a subprocess.
8084 If N is a positive number, pipe the N next articles.
8085 If N is a negative number, pipe the N previous articles.
8086 If N is nil and any articles have been marked with the process mark,
8087 pipe those articles instead."
8088   (interactive "P")
8089   (gnus-set-global-variables)
8090   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8091     (gnus-summary-save-article arg t))
8092   (gnus-configure-windows 'pipe))
8093
8094 (defun gnus-summary-save-article-mail (&optional arg)
8095   "Append the current article to an mail file.
8096 If N is a positive number, save the N next articles.
8097 If N is a negative number, save the N previous articles.
8098 If N is nil and any articles have been marked with the process mark,
8099 save those articles instead."
8100   (interactive "P")
8101   (gnus-set-global-variables)
8102   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8103     (gnus-summary-save-article arg)))
8104
8105 (defun gnus-summary-save-article-rmail (&optional arg)
8106   "Append the current article to an rmail file.
8107 If N is a positive number, save the N next articles.
8108 If N is a negative number, save the N previous articles.
8109 If N is nil and any articles have been marked with the process mark,
8110 save those articles instead."
8111   (interactive "P")
8112   (gnus-set-global-variables)
8113   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
8114     (gnus-summary-save-article arg)))
8115
8116 (defun gnus-summary-save-article-file (&optional arg)
8117   "Append the current article to a file.
8118 If N is a positive number, save the N next articles.
8119 If N is a negative number, save the N previous articles.
8120 If N is nil and any articles have been marked with the process mark,
8121 save those articles instead."
8122   (interactive "P")
8123   (gnus-set-global-variables)
8124   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8125     (gnus-summary-save-article arg)))
8126
8127 (defun gnus-summary-write-article-file (&optional arg)
8128   "Write the current article to a file, deleting the previous file.
8129 If N is a positive number, save the N next articles.
8130 If N is a negative number, save the N previous articles.
8131 If N is nil and any articles have been marked with the process mark,
8132 save those articles instead."
8133   (interactive "P")
8134   (gnus-set-global-variables)
8135   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8136     (gnus-summary-save-article arg)))
8137
8138 (defun gnus-summary-save-article-body-file (&optional arg)
8139   "Append the current article body to a file.
8140 If N is a positive number, save the N next articles.
8141 If N is a negative number, save the N previous articles.
8142 If N is nil and any articles have been marked with the process mark,
8143 save those articles instead."
8144   (interactive "P")
8145   (gnus-set-global-variables)
8146   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8147     (gnus-summary-save-article arg)))
8148
8149 (defun gnus-summary-pipe-message (program)
8150   "Pipe the current article through PROGRAM."
8151   (interactive "sProgram: ")
8152   (gnus-set-global-variables)
8153   (gnus-summary-select-article)
8154   (let ((mail-header-separator "")
8155         (art-buf (get-buffer gnus-article-buffer)))
8156     (gnus-eval-in-buffer-window gnus-article-buffer
8157       (save-restriction
8158         (widen)
8159         (let ((start (window-start))
8160               buffer-read-only)
8161           (message-pipe-buffer-body program)
8162           (set-window-start (get-buffer-window (current-buffer)) start))))))
8163
8164 (defun gnus-get-split-value (methods)
8165   "Return a value based on the split METHODS."
8166   (let (split-name method result match)
8167     (when methods
8168       (save-excursion
8169         (set-buffer gnus-original-article-buffer)
8170         (save-restriction
8171           (nnheader-narrow-to-headers)
8172           (while methods
8173             (goto-char (point-min))
8174             (setq method (pop methods))
8175             (setq match (car method))
8176             (when (cond
8177                    ((stringp match)
8178                     ;; Regular expression.
8179                     (ignore-errors
8180                       (re-search-forward match nil t)))
8181                    ((gnus-functionp match)
8182                     ;; Function.
8183                     (save-restriction
8184                       (widen)
8185                       (setq result (funcall match gnus-newsgroup-name))))
8186                    ((consp match)
8187                     ;; Form.
8188                     (save-restriction
8189                       (widen)
8190                       (setq result (eval match)))))
8191               (setq split-name (append (cdr method) split-name))
8192               (cond ((stringp result)
8193                      (push (expand-file-name
8194                             result gnus-article-save-directory)
8195                            split-name))
8196                     ((consp result)
8197                      (setq split-name (append result split-name)))))))))
8198     split-name))
8199
8200 (defun gnus-valid-move-group-p (group)
8201   (and (boundp group)
8202        (symbol-name group)
8203        (memq 'respool
8204              (assoc (symbol-name
8205                      (car (gnus-find-method-for-group
8206                            (symbol-name group))))
8207                     gnus-valid-select-methods))))
8208
8209 (defun gnus-read-move-group-name (prompt default articles prefix)
8210   "Read a group name."
8211   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8212          (minibuffer-confirm-incomplete nil) ; XEmacs
8213          (prom
8214           (format "%s %s to:"
8215                   prompt
8216                   (if (> (length articles) 1)
8217                       (format "these %d articles" (length articles))
8218                     "this article")))
8219          (to-newsgroup
8220           (cond
8221            ((null split-name)
8222             (gnus-completing-read default prom
8223                                   gnus-active-hashtb
8224                                   'gnus-valid-move-group-p
8225                                   nil prefix
8226                                   'gnus-group-history))
8227            ((= 1 (length split-name))
8228             (gnus-completing-read (car split-name) prom
8229                                   gnus-active-hashtb
8230                                   'gnus-valid-move-group-p
8231                                   nil nil
8232                                   'gnus-group-history))
8233            (t
8234             (gnus-completing-read nil prom 
8235                                   (mapcar (lambda (el) (list el))
8236                                           (nreverse split-name))
8237                                   nil nil nil
8238                                   'gnus-group-history)))))
8239     (when to-newsgroup
8240       (if (or (string= to-newsgroup "")
8241               (string= to-newsgroup prefix))
8242           (setq to-newsgroup (or default "")))
8243       (or (gnus-active to-newsgroup)
8244           (gnus-activate-group to-newsgroup)
8245           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8246                                      to-newsgroup))
8247               (or (and (gnus-request-create-group 
8248                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8249                        (gnus-activate-group to-newsgroup nil nil
8250                                             (gnus-group-name-to-method
8251                                              to-newsgroup)))
8252                   (error "Couldn't create group %s" to-newsgroup)))
8253           (error "No such group: %s" to-newsgroup)))
8254     to-newsgroup))
8255
8256 ;; Summary extract commands
8257
8258 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
8259   (let ((buffer-read-only nil)
8260         (article (gnus-summary-article-number))
8261         after-article b e)
8262     (unless (gnus-summary-goto-subject article)
8263       (error "No such article: %d" article))
8264     (gnus-summary-position-point)
8265     ;; If all commands are to be bunched up on one line, we collect
8266     ;; them here.
8267     (unless gnus-view-pseudos-separately
8268       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8269             files action)
8270         (while ps
8271           (setq action (cdr (assq 'action (car ps))))
8272           (setq files (list (cdr (assq 'name (car ps)))))
8273           (while (and ps (cdr ps)
8274                       (string= (or action "1")
8275                                (or (cdr (assq 'action (cadr ps))) "2")))
8276             (push (cdr (assq 'name (cadr ps))) files)
8277             (setcdr ps (cddr ps)))
8278           (when files
8279             (when (not (string-match "%s" action))
8280               (push " " files))
8281             (push " " files)
8282             (when (assq 'execute (car ps))
8283               (setcdr (assq 'execute (car ps))
8284                       (funcall (if (string-match "%s" action)
8285                                    'format 'concat)
8286                                action
8287                                (mapconcat
8288                                 (lambda (f)
8289                                   (if (equal f " ")
8290                                       f
8291                                     (gnus-quote-arg-for-sh-or-csh f)))
8292                                 files " ")))))
8293           (setq ps (cdr ps)))))
8294     (if (and gnus-view-pseudos (not not-view))
8295         (while pslist
8296           (when (assq 'execute (car pslist))
8297             (gnus-execute-command (cdr (assq 'execute (car pslist)))
8298                                   (eq gnus-view-pseudos 'not-confirm)))
8299           (setq pslist (cdr pslist)))
8300       (save-excursion
8301         (while pslist
8302           (setq after-article (or (cdr (assq 'article (car pslist)))
8303                                   (gnus-summary-article-number)))
8304           (gnus-summary-goto-subject after-article)
8305           (forward-line 1)
8306           (setq b (point))
8307           (insert "    " (file-name-nondirectory
8308                           (cdr (assq 'name (car pslist))))
8309                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8310           (setq e (point))
8311           (forward-line -1)             ; back to `b'
8312           (gnus-add-text-properties
8313            b (1- e) (list 'gnus-number gnus-reffed-article-number
8314                           gnus-mouse-face-prop gnus-mouse-face))
8315           (gnus-data-enter
8316            after-article gnus-reffed-article-number
8317            gnus-unread-mark b (car pslist) 0 (- e b))
8318           (push gnus-reffed-article-number gnus-newsgroup-unreads)
8319           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
8320           (setq pslist (cdr pslist)))))))
8321
8322 (defun gnus-pseudos< (p1 p2)
8323   (let ((c1 (cdr (assq 'action p1)))
8324         (c2 (cdr (assq 'action p2))))
8325     (and c1 c2 (string< c1 c2))))
8326
8327 (defun gnus-request-pseudo-article (props)
8328   (cond ((assq 'execute props)
8329          (gnus-execute-command (cdr (assq 'execute props)))))
8330   (let ((gnus-current-article (gnus-summary-article-number)))
8331     (run-hooks 'gnus-mark-article-hook)))
8332
8333 (defun gnus-execute-command (command &optional automatic)
8334   (save-excursion
8335     (gnus-article-setup-buffer)
8336     (set-buffer gnus-article-buffer)
8337     (setq buffer-read-only nil)
8338     (let ((command (if automatic command (read-string "Command: " command)))
8339           ;; Just binding this here doesn't help, because there might
8340           ;; be output from the process after exiting the scope of 
8341           ;; this `let'.
8342           ;; (buffer-read-only nil)
8343           )
8344       (erase-buffer)
8345       (insert "$ " command "\n\n")
8346       (if gnus-view-pseudo-asynchronously
8347           (start-process "gnus-execute" nil shell-file-name
8348                          shell-command-switch command)
8349         (call-process shell-file-name nil t nil
8350                       shell-command-switch command)))))
8351
8352 ;; Summary kill commands.
8353
8354 (defun gnus-summary-edit-global-kill (article)
8355   "Edit the \"global\" kill file."
8356   (interactive (list (gnus-summary-article-number)))
8357   (gnus-set-global-variables)
8358   (gnus-group-edit-global-kill article))
8359
8360 (defun gnus-summary-edit-local-kill ()
8361   "Edit a local kill file applied to the current newsgroup."
8362   (interactive)
8363   (gnus-set-global-variables)
8364   (setq gnus-current-headers (gnus-summary-article-header))
8365   (gnus-set-global-variables)
8366   (gnus-group-edit-local-kill
8367    (gnus-summary-article-number) gnus-newsgroup-name))
8368
8369 ;;; Header reading.
8370
8371 (defun gnus-read-header (id &optional header)
8372   "Read the headers of article ID and enter them into the Gnus system."
8373   (let ((group gnus-newsgroup-name)
8374         (gnus-override-method 
8375          (and (gnus-news-group-p gnus-newsgroup-name)
8376               gnus-refer-article-method))
8377         where)
8378     ;; First we check to see whether the header in question is already
8379     ;; fetched.
8380     (if (stringp id)
8381         ;; This is a Message-ID.
8382         (setq header (or header (gnus-id-to-header id)))
8383       ;; This is an article number.
8384       (setq header (or header (gnus-summary-article-header id))))
8385     (if (and header
8386              (not (gnus-summary-article-sparse-p (mail-header-number header))))
8387         ;; We have found the header.
8388         header
8389       ;; We have to really fetch the header to this article.
8390       (save-excursion
8391         (set-buffer nntp-server-buffer)
8392         (when (setq where (gnus-request-head id group))
8393           (nnheader-fold-continuation-lines)
8394           (goto-char (point-max))
8395           (insert ".\n")
8396           (goto-char (point-min))
8397           (insert "211 ")
8398           (princ (cond
8399                   ((numberp id) id)
8400                   ((cdr where) (cdr where))
8401                   (header (mail-header-number header))
8402                   (t gnus-reffed-article-number))
8403                  (current-buffer))
8404           (insert " Article retrieved.\n"))
8405         (if (or (not where)
8406                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
8407             ()                          ; Malformed head.
8408           (unless (gnus-summary-article-sparse-p (mail-header-number header))
8409             (when (and (stringp id)
8410                        (not (string= (gnus-group-real-name group)
8411                                      (car where))))
8412               ;; If we fetched by Message-ID and the article came
8413               ;; from a different group, we fudge some bogus article
8414               ;; numbers for this article.
8415               (mail-header-set-number header gnus-reffed-article-number))
8416             (save-excursion
8417               (set-buffer gnus-summary-buffer)
8418               (decf gnus-reffed-article-number)
8419               (gnus-remove-header (mail-header-number header))
8420               (push header gnus-newsgroup-headers)
8421               (setq gnus-current-headers header)
8422               (push (mail-header-number header) gnus-newsgroup-limit)))
8423           header)))))
8424
8425 (defun gnus-remove-header (number)
8426   "Remove header NUMBER from `gnus-newsgroup-headers'."
8427   (if (and gnus-newsgroup-headers
8428            (= number (mail-header-number (car gnus-newsgroup-headers))))
8429       (pop gnus-newsgroup-headers)
8430     (let ((headers gnus-newsgroup-headers))
8431       (while (and (cdr headers)
8432                   (not (= number (mail-header-number (cadr headers)))))
8433         (pop headers))
8434       (when (cdr headers)
8435         (setcdr headers (cddr headers))))))
8436
8437 ;;;
8438 ;;; summary highlights
8439 ;;;
8440
8441 (defun gnus-highlight-selected-summary ()
8442   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8443   ;; Highlight selected article in summary buffer
8444   (when gnus-summary-selected-face
8445     (save-excursion
8446       (let* ((beg (progn (beginning-of-line) (point)))
8447              (end (progn (end-of-line) (point)))
8448              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
8449              (from (if (get-text-property beg gnus-mouse-face-prop)
8450                        beg
8451                      (or (next-single-property-change 
8452                           beg gnus-mouse-face-prop nil end)
8453                          beg)))
8454              (to
8455               (if (= from end)
8456                   (- from 2)
8457                 (or (next-single-property-change
8458                      from gnus-mouse-face-prop nil end)
8459                     end))))
8460         ;; If no mouse-face prop on line we will have to = from = end,
8461         ;; so we highlight the entire line instead.
8462         (when (= (+ to 2) from)
8463           (setq from beg)
8464           (setq to end))
8465         (if gnus-newsgroup-selected-overlay
8466             ;; Move old overlay.
8467             (gnus-move-overlay
8468              gnus-newsgroup-selected-overlay from to (current-buffer))
8469           ;; Create new overlay.
8470           (gnus-overlay-put
8471            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
8472            'face gnus-summary-selected-face))))))
8473
8474 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
8475 (defun gnus-summary-highlight-line ()
8476   "Highlight current line according to `gnus-summary-highlight'."
8477   (let* ((list gnus-summary-highlight)
8478          (p (point))
8479          (end (progn (end-of-line) (point)))
8480          ;; now find out where the line starts and leave point there.
8481          (beg (progn (beginning-of-line) (point)))
8482          (article (gnus-summary-article-number))
8483          (score (or (cdr (assq (or article gnus-current-article)
8484                                gnus-newsgroup-scored))
8485                     gnus-summary-default-score 0))
8486          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
8487          (inhibit-read-only t))
8488     ;; Eval the cars of the lists until we find a match.
8489     (let ((default gnus-summary-default-score))
8490       (while (and list
8491                   (not (eval (caar list))))
8492         (setq list (cdr list))))
8493     (let ((face (cdar list)))
8494       (unless (eq face (get-text-property beg 'face))
8495         (gnus-put-text-property 
8496          beg end 'face 
8497          (setq face (if (boundp face) (symbol-value face) face)))
8498         (when gnus-summary-highlight-line-function
8499           (funcall gnus-summary-highlight-line-function article face))))
8500     (goto-char p)))
8501
8502 (defun gnus-update-read-articles (group unread)
8503   "Update the list of read articles in GROUP."
8504   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
8505          (entry (gnus-gethash group gnus-newsrc-hashtb))
8506          (info (nth 2 entry))
8507          (prev 1)
8508          (unread (sort (copy-sequence unread) '<))
8509          read)
8510     (if (or (not info) (not active))
8511         ;; There is no info on this group if it was, in fact,
8512         ;; killed.  Gnus stores no information on killed groups, so
8513         ;; there's nothing to be done.
8514         ;; One could store the information somewhere temporarily,
8515         ;; perhaps...  Hmmm...
8516         ()
8517       ;; Remove any negative articles numbers.
8518       (while (and unread (< (car unread) 0))
8519         (setq unread (cdr unread)))
8520       ;; Remove any expired article numbers
8521       (while (and unread (< (car unread) (car active)))
8522         (setq unread (cdr unread)))
8523       ;; Compute the ranges of read articles by looking at the list of
8524       ;; unread articles.
8525       (while unread
8526         (when (/= (car unread) prev)
8527           (push (if (= prev (1- (car unread))) prev
8528                   (cons prev (1- (car unread))))
8529                 read))
8530         (setq prev (1+ (car unread)))
8531         (setq unread (cdr unread)))
8532       (when (<= prev (cdr active))
8533         (push (cons prev (cdr active)) read))
8534       (save-excursion
8535         (set-buffer gnus-group-buffer)
8536         (gnus-undo-register
8537           `(progn
8538              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
8539              (gnus-info-set-read ',info ',(gnus-info-read info))
8540              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
8541              (gnus-group-update-group ,group t))))
8542       ;; Enter this list into the group info.
8543       (gnus-info-set-read
8544        info (if (> (length read) 1) (nreverse read) read))
8545       ;; Set the number of unread articles in gnus-newsrc-hashtb.
8546       (gnus-get-unread-articles-in-group info (gnus-active group))
8547       t)))
8548
8549 (defun gnus-offer-save-summaries ()
8550   "Offer to save all active summary buffers."
8551   (save-excursion
8552     (let ((buflist (buffer-list))
8553           buffers bufname)
8554       ;; Go through all buffers and find all summaries.
8555       (while buflist
8556         (and (setq bufname (buffer-name (car buflist)))
8557              (string-match "Summary" bufname)
8558              (save-excursion
8559                (set-buffer bufname)
8560                ;; We check that this is, indeed, a summary buffer.
8561                (and (eq major-mode 'gnus-summary-mode)
8562                     ;; Also make sure this isn't bogus.
8563                     gnus-newsgroup-prepared
8564                     ;; Also make sure that this isn't a dead summary buffer.
8565                     (not gnus-dead-summary-mode)))
8566              (push bufname buffers))
8567         (setq buflist (cdr buflist)))
8568       ;; Go through all these summary buffers and offer to save them.
8569       (when buffers
8570         (map-y-or-n-p
8571          "Update summary buffer %s? "
8572          (lambda (buf) (switch-to-buffer buf) (gnus-summary-exit))
8573          buffers)))))
8574
8575 (provide 'gnus-sum)
8576
8577 (run-hooks 'gnus-sum-load-hook)
8578
8579 ;;; gnus-sum.el ends here