(Mail Source Specifiers): Fix. Added documentation for IMAP mail-source
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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 (eval-when-compile (require 'cl))
29
30 (require 'gnus)
31 (require 'gnus-group)
32 (require 'gnus-spec)
33 (require 'gnus-range)
34 (require 'gnus-int)
35 (require 'gnus-undo)
36 (require 'gnus-util)
37 (require 'mm-decode)
38 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
39
40 (defcustom gnus-kill-summary-on-exit t
41   "*If non-nil, kill the summary buffer when you exit from it.
42 If nil, the summary will become a \"*Dead Summary*\" buffer, and
43 it will be killed sometime later."
44   :group 'gnus-summary-exit
45   :type 'boolean)
46
47 (defcustom gnus-fetch-old-headers nil
48   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
49 If an unread article in the group refers to an older, already read (or
50 just marked as read) article, the old article will not normally be
51 displayed in the Summary buffer.  If this variable is non-nil, Gnus
52 will attempt to grab the headers to the old articles, and thereby
53 build complete threads.  If it has the value `some', only enough
54 headers to connect otherwise loose threads will be displayed.  This
55 variable can also be a number.  In that case, no more than that number
56 of old headers will be fetched.  If it has the value `invisible', all
57 old headers will be fetched, but none will be displayed.
58
59 The server has to support NOV for any of this to work."
60   :group 'gnus-thread
61   :type '(choice (const :tag "off" nil)
62                  (const some)
63                  number
64                  (sexp :menu-tag "other" t)))
65
66 (defcustom gnus-refer-thread-limit 200
67   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
68 If t, fetch all the available old headers."
69   :group 'gnus-thread
70   :type '(choice number
71                  (sexp :menu-tag "other" t)))
72
73 (defcustom gnus-summary-make-false-root 'adopt
74   "*nil means that Gnus won't gather loose threads.
75 If the root of a thread has expired or been read in a previous
76 session, the information necessary to build a complete thread has been
77 lost.  Instead of having many small sub-threads from this original thread
78 scattered all over the summary buffer, Gnus can gather them.
79
80 If non-nil, Gnus will try to gather all loose sub-threads from an
81 original thread into one large thread.
82
83 If this variable is non-nil, it should be one of `none', `adopt',
84 `dummy' or `empty'.
85
86 If this variable is `none', Gnus will not make a false root, but just
87 present the sub-threads after another.
88 If this variable is `dummy', Gnus will create a dummy root that will
89 have all the sub-threads as children.
90 If this variable is `adopt', Gnus will make one of the \"children\"
91 the parent and mark all the step-children as such.
92 If this variable is `empty', the \"children\" are printed with empty
93 subject fields.  (Or rather, they will be printed with a string
94 given by the `gnus-summary-same-subject' variable.)"
95   :group 'gnus-thread
96   :type '(choice (const :tag "off" nil)
97                  (const none)
98                  (const dummy)
99                  (const adopt)
100                  (const empty)))
101
102 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
103   "*A regexp to match subjects to be excluded from loose thread gathering.
104 As loose thread gathering is done on subjects only, that means that
105 there can be many false gatherings performed.  By rooting out certain
106 common subjects, gathering might become saner."
107   :group 'gnus-thread
108   :type 'regexp)
109
110 (defcustom gnus-summary-gather-subject-limit nil
111   "*Maximum length of subject comparisons when gathering loose threads.
112 Use nil to compare full subjects.  Setting this variable to a low
113 number will help gather threads that have been corrupted by
114 newsreaders chopping off subject lines, but it might also mean that
115 unrelated articles that have subject that happen to begin with the
116 same few characters will be incorrectly gathered.
117
118 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
119 comparing subjects."
120   :group 'gnus-thread
121   :type '(choice (const :tag "off" nil)
122                  (const fuzzy)
123                  (sexp :menu-tag "on" t)))
124
125 (defcustom gnus-simplify-subject-functions nil
126   "List of functions taking a string argument that simplify subjects.
127 The functions are applied recursively.
128
129 Useful functions to put in this list include: `gnus-simplify-subject-re',
130 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
131   :group 'gnus-thread
132   :type '(repeat function))
133
134 (defcustom gnus-simplify-ignored-prefixes nil
135   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
136   :group 'gnus-thread
137   :type '(choice (const :tag "off" nil)
138                  regexp))
139
140 (defcustom gnus-build-sparse-threads nil
141   "*If non-nil, fill in the gaps in threads.
142 If `some', only fill in the gaps that are needed to tie loose threads
143 together.  If `more', fill in all leaf nodes that Gnus can find.  If
144 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
145   :group 'gnus-thread
146   :type '(choice (const :tag "off" nil)
147                  (const some)
148                  (const more)
149                  (sexp :menu-tag "all" t)))
150
151 (defcustom gnus-summary-thread-gathering-function
152   'gnus-gather-threads-by-subject
153   "*Function used for gathering loose threads.
154 There are two pre-defined functions: `gnus-gather-threads-by-subject',
155 which only takes Subjects into consideration; and
156 `gnus-gather-threads-by-references', which compared the References
157 headers of the articles to find matches."
158   :group 'gnus-thread
159   :type '(radio (function-item gnus-gather-threads-by-subject)
160                 (function-item gnus-gather-threads-by-references)
161                 (function :tag "other")))
162
163 (defcustom gnus-summary-same-subject ""
164   "*String indicating that the current article has the same subject as the previous.
165 This variable will only be used if the value of
166 `gnus-summary-make-false-root' is `empty'."
167   :group 'gnus-summary-format
168   :type 'string)
169
170 (defcustom gnus-summary-goto-unread t
171   "*If t, marking commands will go to the next unread article.
172 If `never', commands that usually go to the next unread article, will
173 go to the next article, whether it is read or not.
174 If nil, only the marking commands will go to the next (un)read article."
175   :group 'gnus-summary-marks
176   :link '(custom-manual "(gnus)Setting Marks")
177   :type '(choice (const :tag "off" nil)
178                  (const never)
179                  (sexp :menu-tag "on" t)))
180
181 (defcustom gnus-summary-default-score 0
182   "*Default article score level.
183 All scores generated by the score files will be added to this score.
184 If this variable is nil, scoring will be disabled."
185   :group 'gnus-score-default
186   :type '(choice (const :tag "disable")
187                  integer))
188
189 (defcustom gnus-summary-zcore-fuzz 0
190   "*Fuzziness factor for the zcore in the summary buffer.
191 Articles with scores closer than this to `gnus-summary-default-score'
192 will not be marked."
193   :group 'gnus-summary-format
194   :type 'integer)
195
196 (defcustom gnus-simplify-subject-fuzzy-regexp nil
197   "*Strings to be removed when doing fuzzy matches.
198 This can either be a regular expression or list of regular expressions
199 that will be removed from subject strings if fuzzy subject
200 simplification is selected."
201   :group 'gnus-thread
202   :type '(repeat regexp))
203
204 (defcustom gnus-show-threads t
205   "*If non-nil, display threads in summary mode."
206   :group 'gnus-thread
207   :type 'boolean)
208
209 (defcustom gnus-thread-hide-subtree nil
210   "*If non-nil, hide all threads initially.
211 If threads are hidden, you have to run the command
212 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
213 to expose hidden threads."
214   :group 'gnus-thread
215   :type 'boolean)
216
217 (defcustom gnus-thread-hide-killed t
218   "*If non-nil, hide killed threads automatically."
219   :group 'gnus-thread
220   :type 'boolean)
221
222 (defcustom gnus-thread-ignore-subject t
223   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
224 If nil, articles that have different subjects from their parents will
225 start separate threads."
226   :group 'gnus-thread
227   :type 'boolean)
228
229 (defcustom gnus-thread-operation-ignore-subject t
230   "*If non-nil, subjects will be ignored when doing thread commands.
231 This affects commands like `gnus-summary-kill-thread' and
232 `gnus-summary-lower-thread'.
233
234 If this variable is nil, articles in the same thread with different
235 subjects will not be included in the operation in question.  If this
236 variable is `fuzzy', only articles that have subjects that are fuzzily
237 equal will be included."
238   :group 'gnus-thread
239   :type '(choice (const :tag "off" nil)
240                  (const fuzzy)
241                  (sexp :tag "on" t)))
242
243 (defcustom gnus-thread-indent-level 4
244   "*Number that says how much each sub-thread should be indented."
245   :group 'gnus-thread
246   :type 'integer)
247
248 (defcustom gnus-auto-extend-newsgroup t
249   "*If non-nil, extend newsgroup forward and backward when requested."
250   :group 'gnus-summary-choose
251   :type 'boolean)
252
253 (defcustom gnus-auto-select-first t
254   "*If nil, don't select the first unread article when entering a group.
255 If this variable is `best', select the highest-scored unread article
256 in the group.  If t, select the first unread article.
257
258 This variable can also be a function to place point on a likely
259 subject line.  Useful values include `gnus-summary-first-unread-subject',
260 `gnus-summary-first-unread-article' and
261 `gnus-summary-best-unread-article'.
262
263 If you want to prevent automatic selection of the first unread article
264 in some newsgroups, set the variable to nil in
265 `gnus-select-group-hook'."
266   :group 'gnus-group-select
267   :type '(choice (const :tag "none" nil)
268                  (const best)
269                  (sexp :menu-tag "first" t)
270                  (function-item gnus-summary-first-unread-subject)
271                  (function-item gnus-summary-first-unread-article)
272                  (function-item gnus-summary-best-unread-article)))
273
274 (defcustom gnus-auto-select-next t
275   "*If non-nil, offer to go to the next group from the end of the previous.
276 If the value is t and the next newsgroup is empty, Gnus will exit
277 summary mode and go back to group mode.  If the value is neither nil
278 nor t, Gnus will select the following unread newsgroup.  In
279 particular, if the value is the symbol `quietly', the next unread
280 newsgroup will be selected without any confirmation, and if it is
281 `almost-quietly', the next group will be selected without any
282 confirmation if you are located on the last article in the group.
283 Finally, if this variable is `slightly-quietly', the `Z n' command
284 will go to the next group without confirmation."
285   :group 'gnus-summary-maneuvering
286   :type '(choice (const :tag "off" nil)
287                  (const quietly)
288                  (const almost-quietly)
289                  (const slightly-quietly)
290                  (sexp :menu-tag "on" t)))
291
292 (defcustom gnus-auto-select-same nil
293   "*If non-nil, select the next article with the same subject.
294 If there are no more articles with the same subject, go to
295 the first unread article."
296   :group 'gnus-summary-maneuvering
297   :type 'boolean)
298
299 (defcustom gnus-summary-check-current nil
300   "*If non-nil, consider the current article when moving.
301 The \"unread\" movement commands will stay on the same line if the
302 current article is unread."
303   :group 'gnus-summary-maneuvering
304   :type 'boolean)
305
306 (defcustom gnus-auto-center-summary t
307   "*If non-nil, always center the current summary buffer.
308 In particular, if `vertical' do only vertical recentering.  If non-nil
309 and non-`vertical', do both horizontal and vertical recentering."
310   :group 'gnus-summary-maneuvering
311   :type '(choice (const :tag "none" nil)
312                  (const vertical)
313                  (integer :tag "height")
314                  (sexp :menu-tag "both" t)))
315
316 (defcustom gnus-show-all-headers nil
317   "*If non-nil, don't hide any headers."
318   :group 'gnus-article-hiding
319   :group 'gnus-article-headers
320   :type 'boolean)
321
322 (defcustom gnus-summary-ignore-duplicates nil
323   "*If non-nil, ignore articles with identical Message-ID headers."
324   :group 'gnus-summary
325   :type 'boolean)
326
327 (defcustom gnus-single-article-buffer t
328   "*If non-nil, display all articles in the same buffer.
329 If nil, each group will get its own article buffer."
330   :group 'gnus-article-various
331   :type 'boolean)
332
333 (defcustom gnus-break-pages t
334   "*If non-nil, do page breaking on articles.
335 The page delimiter is specified by the `gnus-page-delimiter'
336 variable."
337   :group 'gnus-article-various
338   :type 'boolean)
339
340 (defcustom gnus-move-split-methods nil
341   "*Variable used to suggest where articles are to be moved to.
342 It uses the same syntax as the `gnus-split-methods' variable."
343   :group 'gnus-summary-mail
344   :type '(repeat (choice (list :value (fun) function)
345                          (cons :value ("" "") regexp (repeat string))
346                          (sexp :value nil))))
347
348 (defcustom gnus-unread-mark ? ;Whitespace
349   "*Mark used for unread articles."
350   :group 'gnus-summary-marks
351   :type 'character)
352
353 (defcustom gnus-ticked-mark ?!
354   "*Mark used for ticked articles."
355   :group 'gnus-summary-marks
356   :type 'character)
357
358 (defcustom gnus-dormant-mark ??
359   "*Mark used for dormant articles."
360   :group 'gnus-summary-marks
361   :type 'character)
362
363 (defcustom gnus-del-mark ?r
364   "*Mark used for del'd articles."
365   :group 'gnus-summary-marks
366   :type 'character)
367
368 (defcustom gnus-read-mark ?R
369   "*Mark used for read articles."
370   :group 'gnus-summary-marks
371   :type 'character)
372
373 (defcustom gnus-expirable-mark ?E
374   "*Mark used for expirable articles."
375   :group 'gnus-summary-marks
376   :type 'character)
377
378 (defcustom gnus-killed-mark ?K
379   "*Mark used for killed articles."
380   :group 'gnus-summary-marks
381   :type 'character)
382
383 (defcustom gnus-souped-mark ?F
384   "*Mark used for killed articles."
385   :group 'gnus-summary-marks
386   :type 'character)
387
388 (defcustom gnus-kill-file-mark ?X
389   "*Mark used for articles killed by kill files."
390   :group 'gnus-summary-marks
391   :type 'character)
392
393 (defcustom gnus-low-score-mark ?Y
394   "*Mark used for articles with a low score."
395   :group 'gnus-summary-marks
396   :type 'character)
397
398 (defcustom gnus-catchup-mark ?C
399   "*Mark used for articles that are caught up."
400   :group 'gnus-summary-marks
401   :type 'character)
402
403 (defcustom gnus-replied-mark ?A
404   "*Mark used for articles that have been replied to."
405   :group 'gnus-summary-marks
406   :type 'character)
407
408 (defcustom gnus-cached-mark ?*
409   "*Mark used for articles that are in the cache."
410   :group 'gnus-summary-marks
411   :type 'character)
412
413 (defcustom gnus-saved-mark ?S
414   "*Mark used for articles that have been saved to."
415   :group 'gnus-summary-marks
416   :type 'character)
417
418 (defcustom gnus-ancient-mark ?O
419   "*Mark used for ancient articles."
420   :group 'gnus-summary-marks
421   :type 'character)
422
423 (defcustom gnus-sparse-mark ?Q
424   "*Mark used for sparsely reffed articles."
425   :group 'gnus-summary-marks
426   :type 'character)
427
428 (defcustom gnus-canceled-mark ?G
429   "*Mark used for canceled articles."
430   :group 'gnus-summary-marks
431   :type 'character)
432
433 (defcustom gnus-duplicate-mark ?M
434   "*Mark used for duplicate articles."
435   :group 'gnus-summary-marks
436   :type 'character)
437
438 (defcustom gnus-undownloaded-mark ?@
439   "*Mark used for articles that weren't downloaded."
440   :group 'gnus-summary-marks
441   :type 'character)
442
443 (defcustom gnus-downloadable-mark ?%
444   "*Mark used for articles that are to be downloaded."
445   :group 'gnus-summary-marks
446   :type 'character)
447
448 (defcustom gnus-unsendable-mark ?=
449   "*Mark used for articles that won't be sent."
450   :group 'gnus-summary-marks
451   :type 'character)
452
453 (defcustom gnus-score-over-mark ?+
454   "*Score mark used for articles with high scores."
455   :group 'gnus-summary-marks
456   :type 'character)
457
458 (defcustom gnus-score-below-mark ?-
459   "*Score mark used for articles with low scores."
460   :group 'gnus-summary-marks
461   :type 'character)
462
463 (defcustom gnus-empty-thread-mark ? ;Whitespace
464   "*There is no thread under the article."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-not-empty-thread-mark ?=
469   "*There is a thread under the article."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-view-pseudo-asynchronously nil
474   "*If non-nil, Gnus will view pseudo-articles asynchronously."
475   :group 'gnus-extract-view
476   :type 'boolean)
477
478 (defcustom gnus-auto-expirable-marks
479   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
480         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
481         gnus-souped-mark gnus-duplicate-mark)
482   "*The list of marks converted into expiration if a group is auto-expirable."
483   :group 'gnus-summary
484   :type '(repeat character))
485
486 (defcustom gnus-inhibit-user-auto-expire t
487   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
488   :group 'gnus-summary
489   :type 'boolean)
490
491 (defcustom gnus-view-pseudos nil
492   "*If `automatic', pseudo-articles will be viewed automatically.
493 If `not-confirm', pseudos will be viewed automatically, and the user
494 will not be asked to confirm the command."
495   :group 'gnus-extract-view
496   :type '(choice (const :tag "off" nil)
497                  (const automatic)
498                  (const not-confirm)))
499
500 (defcustom gnus-view-pseudos-separately t
501   "*If non-nil, one pseudo-article will be created for each file to be viewed.
502 If nil, all files that use the same viewing command will be given as a
503 list of parameters to that command."
504   :group 'gnus-extract-view
505   :type 'boolean)
506
507 (defcustom gnus-insert-pseudo-articles t
508   "*If non-nil, insert pseudo-articles when decoding articles."
509   :group 'gnus-extract-view
510   :type 'boolean)
511
512 (defcustom gnus-summary-dummy-line-format
513   "  %(:                          :%) %S\n"
514   "*The format specification for the dummy roots in the summary buffer.
515 It works along the same lines as a normal formatting string,
516 with some simple extensions.
517
518 %S  The subject"
519   :group 'gnus-threading
520   :type 'string)
521
522 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
523   "*The format specification for the summary mode line.
524 It works along the same lines as a normal formatting string,
525 with some simple extensions:
526
527 %G  Group name
528 %p  Unprefixed group name
529 %A  Current article number
530 %z  Current article score
531 %V  Gnus version
532 %U  Number of unread articles in the group
533 %e  Number of unselected articles in the group
534 %Z  A string with unread/unselected article counts
535 %g  Shortish group name
536 %S  Subject of the current article
537 %u  User-defined spec
538 %s  Current score file name
539 %d  Number of dormant articles
540 %r  Number of articles that have been marked as read in this session
541 %E  Number of articles expunged by the score files"
542   :group 'gnus-summary-format
543   :type 'string)
544
545 (defcustom gnus-list-identifiers nil
546   "Regexp that matches list identifiers to be removed from subject.
547 This can also be a list of regexps."
548   :group 'gnus-summary-format
549   :group 'gnus-article-hiding
550   :type '(choice (const :tag "none" nil)
551                  (regexp :value ".*")
552                  (repeat :value (".*") regexp)))
553
554 (defcustom gnus-summary-mark-below 0
555   "*Mark all articles with a score below this variable as read.
556 This variable is local to each summary buffer and usually set by the
557 score file."
558   :group 'gnus-score-default
559   :type 'integer)
560
561 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
562   "*List of functions used for sorting articles in the summary buffer.
563 This variable is only used when not using a threaded display."
564   :group 'gnus-summary-sort
565   :type '(repeat (choice (function-item gnus-article-sort-by-number)
566                          (function-item gnus-article-sort-by-author)
567                          (function-item gnus-article-sort-by-subject)
568                          (function-item gnus-article-sort-by-date)
569                          (function-item gnus-article-sort-by-score)
570                          (function :tag "other"))))
571
572 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
573   "*List of functions used for sorting threads in the summary buffer.
574 By default, threads are sorted by article number.
575
576 Each function takes two threads and return non-nil if the first thread
577 should be sorted before the other.  If you use more than one function,
578 the primary sort function should be the last.  You should probably
579 always include `gnus-thread-sort-by-number' in the list of sorting
580 functions -- preferably first.
581
582 Ready-made functions include `gnus-thread-sort-by-number',
583 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
584 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
585 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
586   :group 'gnus-summary-sort
587   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
588                          (function-item gnus-thread-sort-by-author)
589                          (function-item gnus-thread-sort-by-subject)
590                          (function-item gnus-thread-sort-by-date)
591                          (function-item gnus-thread-sort-by-score)
592                          (function-item gnus-thread-sort-by-total-score)
593                          (function :tag "other"))))
594
595 (defcustom gnus-thread-score-function '+
596   "*Function used for calculating the total score of a thread.
597
598 The function is called with the scores of the article and each
599 subthread and should then return the score of the thread.
600
601 Some functions you can use are `+', `max', or `min'."
602   :group 'gnus-summary-sort
603   :type 'function)
604
605 (defcustom gnus-summary-expunge-below nil
606   "All articles that have a score less than this variable will be expunged.
607 This variable is local to the summary buffers."
608   :group 'gnus-score-default
609   :type '(choice (const :tag "off" nil)
610                  integer))
611
612 (defcustom gnus-thread-expunge-below nil
613   "All threads that have a total score less than this variable will be expunged.
614 See `gnus-thread-score-function' for en explanation of what a
615 \"thread score\" is.
616
617 This variable is local to the summary buffers."
618   :group 'gnus-threading
619   :group 'gnus-score-default
620   :type '(choice (const :tag "off" nil)
621                  integer))
622
623 (defcustom gnus-summary-mode-hook nil
624   "*A hook for Gnus summary mode.
625 This hook is run before any variables are set in the summary buffer."
626   :group 'gnus-summary-various
627   :type 'hook)
628
629 (defcustom gnus-summary-menu-hook nil
630   "*Hook run after the creation of the summary mode menu."
631   :group 'gnus-summary-visual
632   :type 'hook)
633
634 (defcustom gnus-summary-exit-hook nil
635   "*A hook called on exit from the summary buffer.
636 It will be called with point in the group buffer."
637   :group 'gnus-summary-exit
638   :type 'hook)
639
640 (defcustom gnus-summary-prepare-hook nil
641   "*A hook called after the summary buffer has been generated.
642 If you want to modify the summary buffer, you can use this hook."
643   :group 'gnus-summary-various
644   :type 'hook)
645
646 (defcustom gnus-summary-prepared-hook nil
647   "*A hook called as the last thing after the summary buffer has been generated."
648   :group 'gnus-summary-various
649   :type 'hook)
650
651 (defcustom gnus-summary-generate-hook nil
652   "*A hook run just before generating the summary buffer.
653 This hook is commonly used to customize threading variables and the
654 like."
655   :group 'gnus-summary-various
656   :type 'hook)
657
658 (defcustom gnus-select-group-hook nil
659   "*A hook called when a newsgroup is selected.
660
661 If you'd like to simplify subjects like the
662 `gnus-summary-next-same-subject' command does, you can use the
663 following hook:
664
665  (setq gnus-select-group-hook
666       (list
667         (lambda ()
668           (mapcar (lambda (header)
669                      (mail-header-set-subject
670                       header
671                       (gnus-simplify-subject
672                        (mail-header-subject header) 're-only)))
673                   gnus-newsgroup-headers))))"
674   :group 'gnus-group-select
675   :type 'hook)
676
677 (defcustom gnus-select-article-hook nil
678   "*A hook called when an article is selected."
679   :group 'gnus-summary-choose
680   :type 'hook)
681
682 (defcustom gnus-visual-mark-article-hook
683   (list 'gnus-highlight-selected-summary)
684   "*Hook run after selecting an article in the summary buffer.
685 It is meant to be used for highlighting the article in some way.  It
686 is not run if `gnus-visual' is nil."
687   :group 'gnus-summary-visual
688   :type 'hook)
689
690 (defcustom gnus-parse-headers-hook nil
691   "*A hook called before parsing the headers."
692   :group 'gnus-various
693   :type 'hook)
694
695 (defcustom gnus-exit-group-hook nil
696   "*A hook called when exiting (not quitting) summary mode."
697   :group 'gnus-various
698   :type 'hook)
699
700 (defcustom gnus-summary-update-hook
701   (list 'gnus-summary-highlight-line)
702   "*A hook called when a summary line is changed.
703 The hook will not be called if `gnus-visual' is nil.
704
705 The default function `gnus-summary-highlight-line' will
706 highlight the line according to the `gnus-summary-highlight'
707 variable."
708   :group 'gnus-summary-visual
709   :type 'hook)
710
711 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
712   "*A hook called when an article is selected for the first time.
713 The hook is intended to mark an article as read (or unread)
714 automatically when it is selected."
715   :group 'gnus-summary-choose
716   :type 'hook)
717
718 (defcustom gnus-group-no-more-groups-hook nil
719   "*A hook run when returning to group mode having no more (unread) groups."
720   :group 'gnus-group-select
721   :type 'hook)
722
723 (defcustom gnus-ps-print-hook nil
724   "*A hook run before ps-printing something from Gnus."
725   :group 'gnus-summary
726   :type 'hook)
727
728 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
729   "Face used for highlighting the current article in the summary buffer."
730   :group 'gnus-summary-visual
731   :type 'face)
732
733 (defcustom gnus-summary-highlight
734   '(((= mark gnus-canceled-mark)
735      . gnus-summary-cancelled-face)
736     ((and (> score default)
737           (or (= mark gnus-dormant-mark)
738               (= mark gnus-ticked-mark)))
739      . gnus-summary-high-ticked-face)
740     ((and (< score default)
741           (or (= mark gnus-dormant-mark)
742               (= mark gnus-ticked-mark)))
743      . gnus-summary-low-ticked-face)
744     ((or (= mark gnus-dormant-mark)
745          (= mark gnus-ticked-mark))
746      . gnus-summary-normal-ticked-face)
747     ((and (> score default) (= mark gnus-ancient-mark))
748      . gnus-summary-high-ancient-face)
749     ((and (< score default) (= mark gnus-ancient-mark))
750      . gnus-summary-low-ancient-face)
751     ((= mark gnus-ancient-mark)
752      . gnus-summary-normal-ancient-face)
753     ((and (> score default) (= mark gnus-unread-mark))
754      . gnus-summary-high-unread-face)
755     ((and (< score default) (= mark gnus-unread-mark))
756      . gnus-summary-low-unread-face)
757     ((= mark gnus-unread-mark)
758      . gnus-summary-normal-unread-face)
759     ((and (> score default) (memq mark (list gnus-downloadable-mark
760                                              gnus-undownloaded-mark)))
761      . gnus-summary-high-unread-face)
762     ((and (< score default) (memq mark (list gnus-downloadable-mark
763                                              gnus-undownloaded-mark)))
764      . gnus-summary-low-unread-face)
765     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
766      . gnus-summary-normal-unread-face)
767     ((> score default)
768      . gnus-summary-high-read-face)
769     ((< score default)
770      . gnus-summary-low-read-face)
771     (t
772      . gnus-summary-normal-read-face))
773   "*Controls the highlighting of summary buffer lines.
774
775 A list of (FORM . FACE) pairs.  When deciding how a a particular
776 summary line should be displayed, each form is evaluated.  The content
777 of the face field after the first true form is used.  You can change
778 how those summary lines are displayed, by editing the face field.
779
780 You can use the following variables in the FORM field.
781
782 score:   The articles score
783 default: The default article score.
784 below:   The score below which articles are automatically marked as read.
785 mark:    The articles mark."
786   :group 'gnus-summary-visual
787   :type '(repeat (cons (sexp :tag "Form" nil)
788                        face)))
789
790 (defcustom gnus-alter-header-function nil
791   "Function called to allow alteration of article header structures.
792 The function is called with one parameter, the article header vector,
793 which it may alter in any way.")
794
795 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
796   "Variable that says which function should be used to decode a string with encoded words.")
797
798 (defcustom gnus-extra-headers nil
799   "*Extra headers to parse."
800   :group 'gnus-summary
801   :type '(repeat symbol))
802
803 (defcustom gnus-ignored-from-addresses
804   (and user-mail-address (regexp-quote user-mail-address))
805   "*Regexp of From headers that may be suppressed in favor of To headers."
806   :group 'gnus-summary
807   :type 'regexp)
808
809 (defcustom gnus-group-charset-alist
810   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
811     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
812     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
813     ("^relcom\\>" koi8-r)
814     ("^fido7\\>" koi8-r)
815     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
816     ("^israel\\>" iso-8859-1)
817     ("^han\\>" euc-kr)
818     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
819     (".*" iso-8859-1))
820   "Alist of regexps (to match group names) and default charsets to be used when reading."
821   :type '(repeat (list (regexp :tag "Group")
822                        (symbol :tag "Charset")))
823   :group 'gnus-charset)
824
825 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
826   "List of charsets that should be ignored.
827 When these charsets are used in the \"charset\" parameter, the
828 default charset will be used instead."
829   :type '(repeat symbol)
830   :group 'gnus-charset)
831
832 (defcustom gnus-group-ignored-charsets-alist 
833   '(("alt\\.chinese\\.text" iso-8859-1))
834   "Alist of regexps (to match group names) and charsets that should be ignored.
835 When these charsets are used in the \"charset\" parameter, the
836 default charset will be used instead."
837   :type '(repeat (cons (regexp :tag "Group")
838                        (repeat symbol)))
839   :group 'gnus-charset)
840
841 (defcustom gnus-group-highlight-words-alist nil
842   "Alist of group regexps and highlight regexps.
843 This variable uses the same syntax as `gnus-emphasis-alist'."
844   :type '(repeat (cons (regexp :tag "Group")
845                        (repeat (list (regexp :tag "Highlight regexp")
846                                      (number :tag "Group for entire word" 0)
847                                      (number :tag "Group for displayed part" 0)
848                                      (symbol :tag "Face" 
849                                              gnus-emphasis-highlight-words)))))
850   :group 'gnus-summary-visual)
851
852 ;;; Internal variables
853
854 (defvar gnus-article-mime-handles nil)
855 (defvar gnus-article-decoded-p nil)
856 (defvar gnus-scores-exclude-files nil)
857 (defvar gnus-page-broken nil)
858 (defvar gnus-inhibit-mime-unbuttonizing nil)
859
860 (defvar gnus-original-article nil)
861 (defvar gnus-article-internal-prepare-hook nil)
862 (defvar gnus-newsgroup-process-stack nil)
863
864 (defvar gnus-thread-indent-array nil)
865 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
866 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
867   "Function called to sort the articles within a thread after it has 
868 been gathered together.")
869
870 ;; Avoid highlighting in kill files.
871 (defvar gnus-summary-inhibit-highlight nil)
872 (defvar gnus-newsgroup-selected-overlay nil)
873 (defvar gnus-inhibit-limiting nil)
874 (defvar gnus-newsgroup-adaptive-score-file nil)
875 (defvar gnus-current-score-file nil)
876 (defvar gnus-current-move-group nil)
877 (defvar gnus-current-copy-group nil)
878 (defvar gnus-current-crosspost-group nil)
879
880 (defvar gnus-newsgroup-dependencies nil)
881 (defvar gnus-newsgroup-adaptive nil)
882 (defvar gnus-summary-display-article-function nil)
883 (defvar gnus-summary-highlight-line-function nil
884   "Function called after highlighting a summary line.")
885
886 (defvar gnus-summary-line-format-alist
887   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
888     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
889     (?s gnus-tmp-subject-or-nil ?s)
890     (?n gnus-tmp-name ?s)
891     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
892         ?s)
893     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
894             gnus-tmp-from) ?s)
895     (?F gnus-tmp-from ?s)
896     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
897     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
898     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
899     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
900     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
901     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
902     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
903     (?L gnus-tmp-lines ?d)
904     (?I gnus-tmp-indentation ?s)
905     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
906     (?R gnus-tmp-replied ?c)
907     (?\[ gnus-tmp-opening-bracket ?c)
908     (?\] gnus-tmp-closing-bracket ?c)
909     (?\> (make-string gnus-tmp-level ? ) ?s)
910     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
911     (?i gnus-tmp-score ?d)
912     (?z gnus-tmp-score-char ?c)
913     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
914     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
915     (?U gnus-tmp-unread ?c)
916     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
917     (?t (gnus-summary-number-of-articles-in-thread
918          (and (boundp 'thread) (car thread)) gnus-tmp-level)
919         ?d)
920     (?e (gnus-summary-number-of-articles-in-thread
921          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
922         ?c)
923     (?u gnus-tmp-user-defined ?s)
924     (?P (gnus-pick-line-number) ?d))
925   "An alist of format specifications that can appear in summary lines,
926 and what variables they correspond with, along with the type of the
927 variable (string, integer, character, etc).")
928
929 (defvar gnus-summary-dummy-line-format-alist
930   `((?S gnus-tmp-subject ?s)
931     (?N gnus-tmp-number ?d)
932     (?u gnus-tmp-user-defined ?s)))
933
934 (defvar gnus-summary-mode-line-format-alist
935   `((?G gnus-tmp-group-name ?s)
936     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
937     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
938     (?A gnus-tmp-article-number ?d)
939     (?Z gnus-tmp-unread-and-unselected ?s)
940     (?V gnus-version ?s)
941     (?U gnus-tmp-unread-and-unticked ?d)
942     (?S gnus-tmp-subject ?s)
943     (?e gnus-tmp-unselected ?d)
944     (?u gnus-tmp-user-defined ?s)
945     (?d (length gnus-newsgroup-dormant) ?d)
946     (?t (length gnus-newsgroup-marked) ?d)
947     (?r (length gnus-newsgroup-reads) ?d)
948     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
949     (?E gnus-newsgroup-expunged-tally ?d)
950     (?s (gnus-current-score-file-nondirectory) ?s)))
951
952 (defvar gnus-last-search-regexp nil
953   "Default regexp for article search command.")
954
955 (defvar gnus-last-shell-command nil
956   "Default shell command on article.")
957
958 (defvar gnus-newsgroup-begin nil)
959 (defvar gnus-newsgroup-end nil)
960 (defvar gnus-newsgroup-last-rmail nil)
961 (defvar gnus-newsgroup-last-mail nil)
962 (defvar gnus-newsgroup-last-folder nil)
963 (defvar gnus-newsgroup-last-file nil)
964 (defvar gnus-newsgroup-auto-expire nil)
965 (defvar gnus-newsgroup-active nil)
966
967 (defvar gnus-newsgroup-data nil)
968 (defvar gnus-newsgroup-data-reverse nil)
969 (defvar gnus-newsgroup-limit nil)
970 (defvar gnus-newsgroup-limits nil)
971
972 (defvar gnus-newsgroup-unreads nil
973   "List of unread articles in the current newsgroup.")
974
975 (defvar gnus-newsgroup-unselected nil
976   "List of unselected unread articles in the current newsgroup.")
977
978 (defvar gnus-newsgroup-reads nil
979   "Alist of read articles and article marks in the current newsgroup.")
980
981 (defvar gnus-newsgroup-expunged-tally nil)
982
983 (defvar gnus-newsgroup-marked nil
984   "List of ticked articles in the current newsgroup (a subset of unread art).")
985
986 (defvar gnus-newsgroup-killed nil
987   "List of ranges of articles that have been through the scoring process.")
988
989 (defvar gnus-newsgroup-cached nil
990   "List of articles that come from the article cache.")
991
992 (defvar gnus-newsgroup-saved nil
993   "List of articles that have been saved.")
994
995 (defvar gnus-newsgroup-kill-headers nil)
996
997 (defvar gnus-newsgroup-replied nil
998   "List of articles that have been replied to in the current newsgroup.")
999
1000 (defvar gnus-newsgroup-expirable nil
1001   "List of articles in the current newsgroup that can be expired.")
1002
1003 (defvar gnus-newsgroup-processable nil
1004   "List of articles in the current newsgroup that can be processed.")
1005
1006 (defvar gnus-newsgroup-downloadable nil
1007   "List of articles in the current newsgroup that can be processed.")
1008
1009 (defvar gnus-newsgroup-undownloaded nil
1010   "List of articles in the current newsgroup that haven't been downloaded..")
1011
1012 (defvar gnus-newsgroup-unsendable nil
1013   "List of articles in the current newsgroup that won't be sent.")
1014
1015 (defvar gnus-newsgroup-bookmarks nil
1016   "List of articles in the current newsgroup that have bookmarks.")
1017
1018 (defvar gnus-newsgroup-dormant nil
1019   "List of dormant articles in the current newsgroup.")
1020
1021 (defvar gnus-newsgroup-scored nil
1022   "List of scored articles in the current newsgroup.")
1023
1024 (defvar gnus-newsgroup-headers nil
1025   "List of article headers in the current newsgroup.")
1026
1027 (defvar gnus-newsgroup-threads nil)
1028
1029 (defvar gnus-newsgroup-prepared nil
1030   "Whether the current group has been prepared properly.")
1031
1032 (defvar gnus-newsgroup-ancient nil
1033   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1034
1035 (defvar gnus-newsgroup-sparse nil)
1036
1037 (defvar gnus-current-article nil)
1038 (defvar gnus-article-current nil)
1039 (defvar gnus-current-headers nil)
1040 (defvar gnus-have-all-headers nil)
1041 (defvar gnus-last-article nil)
1042 (defvar gnus-newsgroup-history nil)
1043 (defvar gnus-newsgroup-charset nil)
1044 (defvar gnus-newsgroup-ephemeral-charset nil)
1045 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1046
1047 (defconst gnus-summary-local-variables
1048   '(gnus-newsgroup-name
1049     gnus-newsgroup-begin gnus-newsgroup-end
1050     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1051     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1052     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1053     gnus-newsgroup-unselected gnus-newsgroup-marked
1054     gnus-newsgroup-reads gnus-newsgroup-saved
1055     gnus-newsgroup-replied gnus-newsgroup-expirable
1056     gnus-newsgroup-processable gnus-newsgroup-killed
1057     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1058     gnus-newsgroup-unsendable
1059     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1060     gnus-newsgroup-headers gnus-newsgroup-threads
1061     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1062     gnus-current-article gnus-current-headers gnus-have-all-headers
1063     gnus-last-article gnus-article-internal-prepare-hook
1064     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1065     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1066     gnus-thread-expunge-below
1067     gnus-score-alist gnus-current-score-file
1068     (gnus-summary-expunge-below . global)
1069     (gnus-summary-mark-below . global)
1070     gnus-newsgroup-active gnus-scores-exclude-files
1071     gnus-newsgroup-history gnus-newsgroup-ancient
1072     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1073     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1074     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1075     (gnus-newsgroup-expunged-tally . 0)
1076     gnus-cache-removable-articles gnus-newsgroup-cached
1077     gnus-newsgroup-data gnus-newsgroup-data-reverse
1078     gnus-newsgroup-limit gnus-newsgroup-limits
1079     gnus-newsgroup-charset)
1080   "Variables that are buffer-local to the summary buffers.")
1081
1082 ;; Byte-compiler warning.
1083 (defvar gnus-article-mode-map)
1084
1085 ;; MIME stuff.
1086
1087 (defvar gnus-decode-encoded-word-methods
1088   '(mail-decode-encoded-word-string)
1089   "List of methods used to decode encoded words.
1090
1091 This variable is a list of FUNCTION or (REGEXP . FUNCTION). If item is
1092 FUNCTION, FUNCTION will be apply to all newsgroups. If item is a
1093 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1094 whose names match REGEXP.
1095
1096 For example:
1097 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1098  mail-decode-encoded-word-string
1099  (\"chinese\" . rfc1843-decode-string))
1100 ")
1101
1102 (defvar gnus-decode-encoded-word-methods-cache nil)
1103
1104 (defun gnus-multi-decode-encoded-word-string (string)
1105   "Apply the functions from `gnus-encoded-word-methods' that match."
1106   (unless (and gnus-decode-encoded-word-methods-cache
1107                (eq gnus-newsgroup-name
1108                    (car gnus-decode-encoded-word-methods-cache)))
1109     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1110     (mapc '(lambda (x)
1111              (if (symbolp x)
1112                  (nconc gnus-decode-encoded-word-methods-cache (list x))
1113                (if (and gnus-newsgroup-name
1114                         (string-match (car x) gnus-newsgroup-name))
1115                    (nconc gnus-decode-encoded-word-methods-cache
1116                           (list (cdr x))))))
1117           gnus-decode-encoded-word-methods))
1118   (let ((xlist gnus-decode-encoded-word-methods-cache))
1119     (pop xlist)
1120     (while xlist
1121       (setq string (funcall (pop xlist) string))))
1122   string)
1123
1124 ;; Subject simplification.
1125
1126 (defun gnus-simplify-whitespace (str)
1127   "Remove excessive whitespace."
1128   (let ((mystr str))
1129     ;; Multiple spaces.
1130     (while (string-match "[ \t][ \t]+" mystr)
1131       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1132                           " "
1133                           (substring mystr (match-end 0)))))
1134     ;; Leading spaces.
1135     (when (string-match "^[ \t]+" mystr)
1136       (setq mystr (substring mystr (match-end 0))))
1137     ;; Trailing spaces.
1138     (when (string-match "[ \t]+$" mystr)
1139       (setq mystr (substring mystr 0 (match-beginning 0))))
1140     mystr))
1141
1142 (defsubst gnus-simplify-subject-re (subject)
1143   "Remove \"Re:\" from subject lines."
1144   (if (string-match "^[Rr][Ee]: *" subject)
1145       (substring subject (match-end 0))
1146     subject))
1147
1148 (defun gnus-simplify-subject (subject &optional re-only)
1149   "Remove `Re:' and words in parentheses.
1150 If RE-ONLY is non-nil, strip leading `Re:'s only."
1151   (let ((case-fold-search t))           ;Ignore case.
1152     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1153     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1154       (setq subject (substring subject (match-end 0))))
1155     ;; Remove uninteresting prefixes.
1156     (when (and (not re-only)
1157                gnus-simplify-ignored-prefixes
1158                (string-match gnus-simplify-ignored-prefixes subject))
1159       (setq subject (substring subject (match-end 0))))
1160     ;; Remove words in parentheses from end.
1161     (unless re-only
1162       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1163         (setq subject (substring subject 0 (match-beginning 0)))))
1164     ;; Return subject string.
1165     subject))
1166
1167 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1168 ;; all whitespace.
1169 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1170   (goto-char (point-min))
1171   (while (re-search-forward regexp nil t)
1172       (replace-match (or newtext ""))))
1173
1174 (defun gnus-simplify-buffer-fuzzy ()
1175   "Simplify string in the buffer fuzzily.
1176 The string in the accessible portion of the current buffer is simplified.
1177 It is assumed to be a single-line subject.
1178 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1179 matter is removed.  Additional things can be deleted by setting
1180 gnus-simplify-subject-fuzzy-regexp."
1181   (let ((case-fold-search t)
1182         (modified-tick))
1183     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1184
1185     (while (not (eq modified-tick (buffer-modified-tick)))
1186       (setq modified-tick (buffer-modified-tick))
1187       (cond
1188        ((listp gnus-simplify-subject-fuzzy-regexp)
1189         (mapcar 'gnus-simplify-buffer-fuzzy-step
1190                 gnus-simplify-subject-fuzzy-regexp))
1191        (gnus-simplify-subject-fuzzy-regexp
1192         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1193       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1194       (gnus-simplify-buffer-fuzzy-step
1195        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1196       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1197
1198     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1199     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1200     (gnus-simplify-buffer-fuzzy-step " $")
1201     (gnus-simplify-buffer-fuzzy-step "^ +")))
1202
1203 (defun gnus-simplify-subject-fuzzy (subject)
1204   "Simplify a subject string fuzzily.
1205 See `gnus-simplify-buffer-fuzzy' for details."
1206   (save-excursion
1207     (gnus-set-work-buffer)
1208     (let ((case-fold-search t))
1209       ;; Remove uninteresting prefixes.
1210       (when (and gnus-simplify-ignored-prefixes
1211                  (string-match gnus-simplify-ignored-prefixes subject))
1212         (setq subject (substring subject (match-end 0))))
1213       (insert subject)
1214       (inline (gnus-simplify-buffer-fuzzy))
1215       (buffer-string))))
1216
1217 (defsubst gnus-simplify-subject-fully (subject)
1218   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1219   (cond
1220    (gnus-simplify-subject-functions
1221     (gnus-map-function gnus-simplify-subject-functions subject))
1222    ((null gnus-summary-gather-subject-limit)
1223     (gnus-simplify-subject-re subject))
1224    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1225     (gnus-simplify-subject-fuzzy subject))
1226    ((numberp gnus-summary-gather-subject-limit)
1227     (gnus-limit-string (gnus-simplify-subject-re subject)
1228                        gnus-summary-gather-subject-limit))
1229    (t
1230     subject)))
1231
1232 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1233   "Check whether two subjects are equal.
1234 If optional argument simple-first is t, first argument is already
1235 simplified."
1236   (cond
1237    ((null simple-first)
1238     (equal (gnus-simplify-subject-fully s1)
1239            (gnus-simplify-subject-fully s2)))
1240    (t
1241     (equal s1
1242            (gnus-simplify-subject-fully s2)))))
1243
1244 (defun gnus-summary-bubble-group ()
1245   "Increase the score of the current group.
1246 This is a handy function to add to `gnus-summary-exit-hook' to
1247 increase the score of each group you read."
1248   (gnus-group-add-score gnus-newsgroup-name))
1249
1250 \f
1251 ;;;
1252 ;;; Gnus summary mode
1253 ;;;
1254
1255 (put 'gnus-summary-mode 'mode-class 'special)
1256
1257 (when t
1258   ;; Non-orthogonal keys
1259
1260   (gnus-define-keys gnus-summary-mode-map
1261     " " gnus-summary-next-page
1262     "\177" gnus-summary-prev-page
1263     [delete] gnus-summary-prev-page
1264     [backspace] gnus-summary-prev-page
1265     "\r" gnus-summary-scroll-up
1266     "\M-\r" gnus-summary-scroll-down
1267     "n" gnus-summary-next-unread-article
1268     "p" gnus-summary-prev-unread-article
1269     "N" gnus-summary-next-article
1270     "P" gnus-summary-prev-article
1271     "\M-\C-n" gnus-summary-next-same-subject
1272     "\M-\C-p" gnus-summary-prev-same-subject
1273     "\M-n" gnus-summary-next-unread-subject
1274     "\M-p" gnus-summary-prev-unread-subject
1275     "." gnus-summary-first-unread-article
1276     "," gnus-summary-best-unread-article
1277     "\M-s" gnus-summary-search-article-forward
1278     "\M-r" gnus-summary-search-article-backward
1279     "<" gnus-summary-beginning-of-article
1280     ">" gnus-summary-end-of-article
1281     "j" gnus-summary-goto-article
1282     "^" gnus-summary-refer-parent-article
1283     "\M-^" gnus-summary-refer-article
1284     "u" gnus-summary-tick-article-forward
1285     "!" gnus-summary-tick-article-forward
1286     "U" gnus-summary-tick-article-backward
1287     "d" gnus-summary-mark-as-read-forward
1288     "D" gnus-summary-mark-as-read-backward
1289     "E" gnus-summary-mark-as-expirable
1290     "\M-u" gnus-summary-clear-mark-forward
1291     "\M-U" gnus-summary-clear-mark-backward
1292     "k" gnus-summary-kill-same-subject-and-select
1293     "\C-k" gnus-summary-kill-same-subject
1294     "\M-\C-k" gnus-summary-kill-thread
1295     "\M-\C-l" gnus-summary-lower-thread
1296     "e" gnus-summary-edit-article
1297     "#" gnus-summary-mark-as-processable
1298     "\M-#" gnus-summary-unmark-as-processable
1299     "\M-\C-t" gnus-summary-toggle-threads
1300     "\M-\C-s" gnus-summary-show-thread
1301     "\M-\C-h" gnus-summary-hide-thread
1302     "\M-\C-f" gnus-summary-next-thread
1303     "\M-\C-b" gnus-summary-prev-thread
1304     "\M-\C-u" gnus-summary-up-thread
1305     "\M-\C-d" gnus-summary-down-thread
1306     "&" gnus-summary-execute-command
1307     "c" gnus-summary-catchup-and-exit
1308     "\C-w" gnus-summary-mark-region-as-read
1309     "\C-t" gnus-summary-toggle-truncation
1310     "?" gnus-summary-mark-as-dormant
1311     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1312     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1313     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1314     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1315     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1316     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1317     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1318     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1319     "=" gnus-summary-expand-window
1320     "\C-x\C-s" gnus-summary-reselect-current-group
1321     "\M-g" gnus-summary-rescan-group
1322     "w" gnus-summary-stop-page-breaking
1323     "\C-c\C-r" gnus-summary-caesar-message
1324     "f" gnus-summary-followup
1325     "F" gnus-summary-followup-with-original
1326     "C" gnus-summary-cancel-article
1327     "r" gnus-summary-reply
1328     "R" gnus-summary-reply-with-original
1329     "\C-c\C-f" gnus-summary-mail-forward
1330     "o" gnus-summary-save-article
1331     "\C-o" gnus-summary-save-article-mail
1332     "|" gnus-summary-pipe-output
1333     "\M-k" gnus-summary-edit-local-kill
1334     "\M-K" gnus-summary-edit-global-kill
1335     ;; "V" gnus-version
1336     "\C-c\C-d" gnus-summary-describe-group
1337     "q" gnus-summary-exit
1338     "Q" gnus-summary-exit-no-update
1339     "\C-c\C-i" gnus-info-find-node
1340     gnus-mouse-2 gnus-mouse-pick-article
1341     "m" gnus-summary-mail-other-window
1342     "a" gnus-summary-post-news
1343     "x" gnus-summary-limit-to-unread
1344     "s" gnus-summary-isearch-article
1345     "t" gnus-summary-toggle-header
1346     "g" gnus-summary-show-article
1347     "l" gnus-summary-goto-last-article
1348     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1349     "\C-d" gnus-summary-enter-digest-group
1350     "\M-\C-d" gnus-summary-read-document
1351     "\M-\C-e" gnus-summary-edit-parameters
1352     "\M-\C-g" gnus-summary-customize-parameters
1353     "\C-c\C-b" gnus-bug
1354     "*" gnus-cache-enter-article
1355     "\M-*" gnus-cache-remove-article
1356     "\M-&" gnus-summary-universal-argument
1357     "\C-l" gnus-recenter
1358     "I" gnus-summary-increase-score
1359     "L" gnus-summary-lower-score
1360     "\M-i" gnus-symbolic-argument
1361     "h" gnus-summary-select-article-buffer
1362
1363     "b" gnus-article-view-part
1364     "\M-t" gnus-summary-toggle-display-buttonized
1365
1366     "V" gnus-summary-score-map
1367     "X" gnus-uu-extract-map
1368     "S" gnus-summary-send-map)
1369
1370   ;; Sort of orthogonal keymap
1371   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1372     "t" gnus-summary-tick-article-forward
1373     "!" gnus-summary-tick-article-forward
1374     "d" gnus-summary-mark-as-read-forward
1375     "r" gnus-summary-mark-as-read-forward
1376     "c" gnus-summary-clear-mark-forward
1377     " " gnus-summary-clear-mark-forward
1378     "e" gnus-summary-mark-as-expirable
1379     "x" gnus-summary-mark-as-expirable
1380     "?" gnus-summary-mark-as-dormant
1381     "b" gnus-summary-set-bookmark
1382     "B" gnus-summary-remove-bookmark
1383     "#" gnus-summary-mark-as-processable
1384     "\M-#" gnus-summary-unmark-as-processable
1385     "S" gnus-summary-limit-include-expunged
1386     "C" gnus-summary-catchup
1387     "H" gnus-summary-catchup-to-here
1388     "\C-c" gnus-summary-catchup-all
1389     "k" gnus-summary-kill-same-subject-and-select
1390     "K" gnus-summary-kill-same-subject
1391     "P" gnus-uu-mark-map)
1392
1393   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1394     "c" gnus-summary-clear-above
1395     "u" gnus-summary-tick-above
1396     "m" gnus-summary-mark-above
1397     "k" gnus-summary-kill-below)
1398
1399   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1400     "/" gnus-summary-limit-to-subject
1401     "n" gnus-summary-limit-to-articles
1402     "w" gnus-summary-pop-limit
1403     "s" gnus-summary-limit-to-subject
1404     "a" gnus-summary-limit-to-author
1405     "u" gnus-summary-limit-to-unread
1406     "m" gnus-summary-limit-to-marks
1407     "M" gnus-summary-limit-exclude-marks
1408     "v" gnus-summary-limit-to-score
1409     "*" gnus-summary-limit-include-cached
1410     "D" gnus-summary-limit-include-dormant
1411     "T" gnus-summary-limit-include-thread
1412     "d" gnus-summary-limit-exclude-dormant
1413     "t" gnus-summary-limit-to-age
1414     "x" gnus-summary-limit-to-extra 
1415     "E" gnus-summary-limit-include-expunged
1416     "c" gnus-summary-limit-exclude-childless-dormant
1417     "C" gnus-summary-limit-mark-excluded-as-read)
1418
1419   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1420     "n" gnus-summary-next-unread-article
1421     "p" gnus-summary-prev-unread-article
1422     "N" gnus-summary-next-article
1423     "P" gnus-summary-prev-article
1424     "\C-n" gnus-summary-next-same-subject
1425     "\C-p" gnus-summary-prev-same-subject
1426     "\M-n" gnus-summary-next-unread-subject
1427     "\M-p" gnus-summary-prev-unread-subject
1428     "f" gnus-summary-first-unread-article
1429     "b" gnus-summary-best-unread-article
1430     "j" gnus-summary-goto-article
1431     "g" gnus-summary-goto-subject
1432     "l" gnus-summary-goto-last-article
1433     "o" gnus-summary-pop-article)
1434
1435   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1436     "k" gnus-summary-kill-thread
1437     "l" gnus-summary-lower-thread
1438     "i" gnus-summary-raise-thread
1439     "T" gnus-summary-toggle-threads
1440     "t" gnus-summary-rethread-current
1441     "^" gnus-summary-reparent-thread
1442     "s" gnus-summary-show-thread
1443     "S" gnus-summary-show-all-threads
1444     "h" gnus-summary-hide-thread
1445     "H" gnus-summary-hide-all-threads
1446     "n" gnus-summary-next-thread
1447     "p" gnus-summary-prev-thread
1448     "u" gnus-summary-up-thread
1449     "o" gnus-summary-top-thread
1450     "d" gnus-summary-down-thread
1451     "#" gnus-uu-mark-thread
1452     "\M-#" gnus-uu-unmark-thread)
1453
1454   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1455     "g" gnus-summary-prepare
1456     "c" gnus-summary-insert-cached-articles)
1457
1458   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1459     "c" gnus-summary-catchup-and-exit
1460     "C" gnus-summary-catchup-all-and-exit
1461     "E" gnus-summary-exit-no-update
1462     "Q" gnus-summary-exit
1463     "Z" gnus-summary-exit
1464     "n" gnus-summary-catchup-and-goto-next-group
1465     "R" gnus-summary-reselect-current-group
1466     "G" gnus-summary-rescan-group
1467     "N" gnus-summary-next-group
1468     "s" gnus-summary-save-newsrc
1469     "P" gnus-summary-prev-group)
1470
1471   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1472     " " gnus-summary-next-page
1473     "n" gnus-summary-next-page
1474     "\177" gnus-summary-prev-page
1475     [delete] gnus-summary-prev-page
1476     "p" gnus-summary-prev-page
1477     "\r" gnus-summary-scroll-up
1478     "\M-\r" gnus-summary-scroll-down
1479     "<" gnus-summary-beginning-of-article
1480     ">" gnus-summary-end-of-article
1481     "b" gnus-summary-beginning-of-article
1482     "e" gnus-summary-end-of-article
1483     "^" gnus-summary-refer-parent-article
1484     "r" gnus-summary-refer-parent-article
1485     "D" gnus-summary-enter-digest-group
1486     "R" gnus-summary-refer-references
1487     "T" gnus-summary-refer-thread
1488     "g" gnus-summary-show-article
1489     "s" gnus-summary-isearch-article
1490     "P" gnus-summary-print-article
1491     "t" gnus-article-babel)
1492
1493   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1494     "b" gnus-article-add-buttons
1495     "B" gnus-article-add-buttons-to-head
1496     "o" gnus-article-treat-overstrike
1497     "e" gnus-article-emphasize
1498     "w" gnus-article-fill-cited-article
1499     "Q" gnus-article-fill-long-lines
1500     "C" gnus-article-capitalize-sentences
1501     "c" gnus-article-remove-cr
1502     "q" gnus-article-de-quoted-unreadable
1503     "f" gnus-article-display-x-face
1504     "l" gnus-summary-stop-page-breaking
1505     "r" gnus-summary-caesar-message
1506     "t" gnus-article-hide-headers
1507     "v" gnus-summary-verbose-headers
1508     "H" gnus-article-strip-headers-in-body
1509     "d" gnus-article-treat-dumbquotes)
1510
1511   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1512     "a" gnus-article-hide
1513     "h" gnus-article-hide-headers
1514     "b" gnus-article-hide-boring-headers
1515     "s" gnus-article-hide-signature
1516     "c" gnus-article-hide-citation
1517     "C" gnus-article-hide-citation-in-followups
1518     "l" gnus-article-hide-list-identifiers
1519     "p" gnus-article-hide-pgp
1520     "B" gnus-article-strip-banner
1521     "P" gnus-article-hide-pem
1522     "\C-c" gnus-article-hide-citation-maybe)
1523
1524   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1525     "a" gnus-article-highlight
1526     "h" gnus-article-highlight-headers
1527     "c" gnus-article-highlight-citation
1528     "s" gnus-article-highlight-signature)
1529
1530   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1531     "w" gnus-article-decode-mime-words
1532     "c" gnus-article-decode-charset
1533     "v" gnus-mime-view-all-parts
1534     "b" gnus-article-view-part)
1535
1536   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1537     "z" gnus-article-date-ut
1538     "u" gnus-article-date-ut
1539     "l" gnus-article-date-local
1540     "e" gnus-article-date-lapsed
1541     "o" gnus-article-date-original
1542     "i" gnus-article-date-iso8601
1543     "s" gnus-article-date-user)
1544
1545   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1546     "t" gnus-article-remove-trailing-blank-lines
1547     "l" gnus-article-strip-leading-blank-lines
1548     "m" gnus-article-strip-multiple-blank-lines
1549     "a" gnus-article-strip-blank-lines
1550     "A" gnus-article-strip-all-blank-lines
1551     "s" gnus-article-strip-leading-space
1552     "e" gnus-article-strip-trailing-space)
1553
1554   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1555     "v" gnus-version
1556     "f" gnus-summary-fetch-faq
1557     "d" gnus-summary-describe-group
1558     "h" gnus-summary-describe-briefly
1559     "i" gnus-info-find-node)
1560
1561   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1562     "e" gnus-summary-expire-articles
1563     "\M-\C-e" gnus-summary-expire-articles-now
1564     "\177" gnus-summary-delete-article
1565     [delete] gnus-summary-delete-article
1566     [backspace] gnus-summary-delete-article
1567     "m" gnus-summary-move-article
1568     "r" gnus-summary-respool-article
1569     "w" gnus-summary-edit-article
1570     "c" gnus-summary-copy-article
1571     "B" gnus-summary-crosspost-article
1572     "q" gnus-summary-respool-query
1573     "t" gnus-summary-respool-trace
1574     "i" gnus-summary-import-article
1575     "p" gnus-summary-article-posted-p)
1576
1577   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1578     "o" gnus-summary-save-article
1579     "m" gnus-summary-save-article-mail
1580     "F" gnus-summary-write-article-file
1581     "r" gnus-summary-save-article-rmail
1582     "f" gnus-summary-save-article-file
1583     "b" gnus-summary-save-article-body-file
1584     "h" gnus-summary-save-article-folder
1585     "v" gnus-summary-save-article-vm
1586     "p" gnus-summary-pipe-output
1587     "s" gnus-soup-add-article)
1588
1589   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1590     "b" gnus-summary-display-buttonized
1591     "m" gnus-summary-repair-multipart
1592     "v" gnus-article-view-part
1593     "o" gnus-article-save-part
1594     "c" gnus-article-copy-part
1595     "e" gnus-article-externalize-part
1596     "i" gnus-article-inline-part
1597     "|" gnus-article-pipe-part)
1598   )
1599
1600 (defun gnus-summary-make-menu-bar ()
1601   (gnus-turn-off-edit-menu 'summary)
1602
1603   (unless (boundp 'gnus-summary-misc-menu)
1604
1605     (easy-menu-define
1606      gnus-summary-kill-menu gnus-summary-mode-map ""
1607      (cons
1608       "Score"
1609       (nconc
1610        (list
1611         ["Enter score..." gnus-summary-score-entry t]
1612         ["Customize" gnus-score-customize t])
1613        (gnus-make-score-map 'increase)
1614        (gnus-make-score-map 'lower)
1615        '(("Mark"
1616           ["Kill below" gnus-summary-kill-below t]
1617           ["Mark above" gnus-summary-mark-above t]
1618           ["Tick above" gnus-summary-tick-above t]
1619           ["Clear above" gnus-summary-clear-above t])
1620          ["Current score" gnus-summary-current-score t]
1621          ["Set score" gnus-summary-set-score t]
1622          ["Switch current score file..." gnus-score-change-score-file t]
1623          ["Set mark below..." gnus-score-set-mark-below t]
1624          ["Set expunge below..." gnus-score-set-expunge-below t]
1625          ["Edit current score file" gnus-score-edit-current-scores t]
1626          ["Edit score file" gnus-score-edit-file t]
1627          ["Trace score" gnus-score-find-trace t]
1628          ["Find words" gnus-score-find-favourite-words t]
1629          ["Rescore buffer" gnus-summary-rescore t]
1630          ["Increase score..." gnus-summary-increase-score t]
1631          ["Lower score..." gnus-summary-lower-score t]))))
1632
1633     ;; Define both the Article menu in the summary buffer and the equivalent
1634     ;; Commands menu in the article buffer here for consistency.
1635     (let ((innards
1636            '(("Hide"
1637               ["All" gnus-article-hide t]
1638               ["Headers" gnus-article-hide-headers t]
1639               ["Signature" gnus-article-hide-signature t]
1640               ["Citation" gnus-article-hide-citation t]
1641               ["List identifiers" gnus-article-hide-list-identifiers t]
1642               ["PGP" gnus-article-hide-pgp t]
1643               ["Banner" gnus-article-strip-banner t]
1644               ["Boring headers" gnus-article-hide-boring-headers t])
1645              ("Highlight"
1646               ["All" gnus-article-highlight t]
1647               ["Headers" gnus-article-highlight-headers t]
1648               ["Signature" gnus-article-highlight-signature t]
1649               ["Citation" gnus-article-highlight-citation t])
1650              ("MIME"
1651               ["Words" gnus-article-decode-mime-words t]
1652               ["Charset" gnus-article-decode-charset t]
1653               ["QP" gnus-article-de-quoted-unreadable t]
1654               ["View all" gnus-mime-view-all-parts t])
1655              ("Date"
1656               ["Local" gnus-article-date-local t]
1657               ["ISO8601" gnus-article-date-iso8601 t]
1658               ["UT" gnus-article-date-ut t]
1659               ["Original" gnus-article-date-original t]
1660               ["Lapsed" gnus-article-date-lapsed t]
1661               ["User-defined" gnus-article-date-user t])
1662              ("Washing"
1663               ("Remove Blanks"
1664                ["Leading" gnus-article-strip-leading-blank-lines t]
1665                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1666                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1667                ["All of the above" gnus-article-strip-blank-lines t]
1668                ["All" gnus-article-strip-all-blank-lines t]
1669                ["Leading space" gnus-article-strip-leading-space t]
1670                ["Trailing space" gnus-article-strip-trailing-space t])
1671               ["Overstrike" gnus-article-treat-overstrike t]
1672               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1673               ["Emphasis" gnus-article-emphasize t]
1674               ["Word wrap" gnus-article-fill-cited-article t]
1675               ["Fill long lines" gnus-article-fill-long-lines t]
1676               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1677               ["CR" gnus-article-remove-cr t]
1678               ["Show X-Face" gnus-article-display-x-face t]
1679               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1680               ["Rot 13" gnus-summary-caesar-message t]
1681               ["Unix pipe" gnus-summary-pipe-message t]
1682               ["Add buttons" gnus-article-add-buttons t]
1683               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1684               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1685               ["Verbose header" gnus-summary-verbose-headers t]
1686               ["Toggle header" gnus-summary-toggle-header t])
1687              ("Output"
1688               ["Save in default format" gnus-summary-save-article t]
1689               ["Save in file" gnus-summary-save-article-file t]
1690               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1691               ["Save in MH folder" gnus-summary-save-article-folder t]
1692               ["Save in VM folder" gnus-summary-save-article-vm t]
1693               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1694               ["Save body in file" gnus-summary-save-article-body-file t]
1695               ["Pipe through a filter" gnus-summary-pipe-output t]
1696               ["Add to SOUP packet" gnus-soup-add-article t]
1697               ["Print" gnus-summary-print-article t])
1698              ("Backend"
1699               ["Respool article..." gnus-summary-respool-article t]
1700               ["Move article..." gnus-summary-move-article
1701                (gnus-check-backend-function
1702                 'request-move-article gnus-newsgroup-name)]
1703               ["Copy article..." gnus-summary-copy-article t]
1704               ["Crosspost article..." gnus-summary-crosspost-article
1705                (gnus-check-backend-function
1706                 'request-replace-article gnus-newsgroup-name)]
1707               ["Import file..." gnus-summary-import-article t]
1708               ["Check if posted" gnus-summary-article-posted-p t]
1709               ["Edit article" gnus-summary-edit-article
1710                (not (gnus-group-read-only-p))]
1711               ["Delete article" gnus-summary-delete-article
1712                (gnus-check-backend-function
1713                 'request-expire-articles gnus-newsgroup-name)]
1714               ["Query respool" gnus-summary-respool-query t]
1715               ["Trace respool" gnus-summary-respool-trace t]
1716               ["Delete expirable articles" gnus-summary-expire-articles-now
1717                (gnus-check-backend-function
1718                 'request-expire-articles gnus-newsgroup-name)])
1719              ("Extract"
1720               ["Uudecode" gnus-uu-decode-uu t]
1721               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1722               ["Unshar" gnus-uu-decode-unshar t]
1723               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1724               ["Save" gnus-uu-decode-save t]
1725               ["Binhex" gnus-uu-decode-binhex t]
1726               ["Postscript" gnus-uu-decode-postscript t])
1727              ("Cache"
1728               ["Enter article" gnus-cache-enter-article t]
1729               ["Remove article" gnus-cache-remove-article t])
1730              ["Translate" gnus-article-babel t]
1731              ["Select article buffer" gnus-summary-select-article-buffer t]
1732              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1733              ["Isearch article..." gnus-summary-isearch-article t]
1734              ["Beginning of the article" gnus-summary-beginning-of-article t]
1735              ["End of the article" gnus-summary-end-of-article t]
1736              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1737              ["Fetch referenced articles" gnus-summary-refer-references t]
1738              ["Fetch current thread" gnus-summary-refer-thread t]
1739              ["Fetch article with id..." gnus-summary-refer-article t]
1740              ["Redisplay" gnus-summary-show-article t])))
1741       (easy-menu-define
1742        gnus-summary-article-menu gnus-summary-mode-map ""
1743        (cons "Article" innards))
1744
1745       (easy-menu-define
1746        gnus-article-commands-menu gnus-article-mode-map ""
1747        (cons "Commands" innards)))
1748
1749     (easy-menu-define
1750      gnus-summary-thread-menu gnus-summary-mode-map ""
1751      '("Threads"
1752        ["Toggle threading" gnus-summary-toggle-threads t]
1753        ["Hide threads" gnus-summary-hide-all-threads t]
1754        ["Show threads" gnus-summary-show-all-threads t]
1755        ["Hide thread" gnus-summary-hide-thread t]
1756        ["Show thread" gnus-summary-show-thread t]
1757        ["Go to next thread" gnus-summary-next-thread t]
1758        ["Go to previous thread" gnus-summary-prev-thread t]
1759        ["Go down thread" gnus-summary-down-thread t]
1760        ["Go up thread" gnus-summary-up-thread t]
1761        ["Top of thread" gnus-summary-top-thread t]
1762        ["Mark thread as read" gnus-summary-kill-thread t]
1763        ["Lower thread score" gnus-summary-lower-thread t]
1764        ["Raise thread score" gnus-summary-raise-thread t]
1765        ["Rethread current" gnus-summary-rethread-current t]
1766        ))
1767
1768     (easy-menu-define
1769      gnus-summary-post-menu gnus-summary-mode-map ""
1770      '("Post"
1771        ["Post an article" gnus-summary-post-news t]
1772        ["Followup" gnus-summary-followup t]
1773        ["Followup and yank" gnus-summary-followup-with-original t]
1774        ["Supersede article" gnus-summary-supersede-article t]
1775        ["Cancel article" gnus-summary-cancel-article t]
1776        ["Reply" gnus-summary-reply t]
1777        ["Reply and yank" gnus-summary-reply-with-original t]
1778        ["Wide reply" gnus-summary-wide-reply t]
1779        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1780        ["Mail forward" gnus-summary-mail-forward t]
1781        ["Post forward" gnus-summary-post-forward t]
1782        ["Digest and mail" gnus-uu-digest-mail-forward t]
1783        ["Digest and post" gnus-uu-digest-post-forward t]
1784        ["Resend message" gnus-summary-resend-message t]
1785        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1786        ["Send a mail" gnus-summary-mail-other-window t]
1787        ["Uuencode and post" gnus-uu-post-news t]
1788        ["Followup via news" gnus-summary-followup-to-mail t]
1789        ["Followup via news and yank"
1790         gnus-summary-followup-to-mail-with-original t]
1791        ;;("Draft"
1792        ;;["Send" gnus-summary-send-draft t]
1793        ;;["Send bounced" gnus-resend-bounced-mail t])
1794        ))
1795
1796     (easy-menu-define
1797      gnus-summary-misc-menu gnus-summary-mode-map ""
1798      '("Misc"
1799        ("Mark Read"
1800         ["Mark as read" gnus-summary-mark-as-read-forward t]
1801         ["Mark same subject and select"
1802          gnus-summary-kill-same-subject-and-select t]
1803         ["Mark same subject" gnus-summary-kill-same-subject t]
1804         ["Catchup" gnus-summary-catchup t]
1805         ["Catchup all" gnus-summary-catchup-all t]
1806         ["Catchup to here" gnus-summary-catchup-to-here t]
1807         ["Catchup region" gnus-summary-mark-region-as-read t]
1808         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1809        ("Mark Various"
1810         ["Tick" gnus-summary-tick-article-forward t]
1811         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1812         ["Remove marks" gnus-summary-clear-mark-forward t]
1813         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1814         ["Set bookmark" gnus-summary-set-bookmark t]
1815         ["Remove bookmark" gnus-summary-remove-bookmark t])
1816        ("Mark Limit"
1817         ["Marks..." gnus-summary-limit-to-marks t]
1818         ["Subject..." gnus-summary-limit-to-subject t]
1819         ["Author..." gnus-summary-limit-to-author t]
1820         ["Age..." gnus-summary-limit-to-age t]
1821         ["Extra..." gnus-summary-limit-to-extra t]
1822         ["Score" gnus-summary-limit-to-score t]
1823         ["Unread" gnus-summary-limit-to-unread t]
1824         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1825         ["Articles" gnus-summary-limit-to-articles t]
1826         ["Pop limit" gnus-summary-pop-limit t]
1827         ["Show dormant" gnus-summary-limit-include-dormant t]
1828         ["Hide childless dormant"
1829          gnus-summary-limit-exclude-childless-dormant t]
1830         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1831         ["Hide marked" gnus-summary-limit-exclude-marks t]
1832         ["Show expunged" gnus-summary-show-all-expunged t])
1833        ("Process Mark"
1834         ["Set mark" gnus-summary-mark-as-processable t]
1835         ["Remove mark" gnus-summary-unmark-as-processable t]
1836         ["Remove all marks" gnus-summary-unmark-all-processable t]
1837         ["Mark above" gnus-uu-mark-over t]
1838         ["Mark series" gnus-uu-mark-series t]
1839         ["Mark region" gnus-uu-mark-region t]
1840         ["Unmark region" gnus-uu-unmark-region t]
1841         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1842         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1843         ["Mark all" gnus-uu-mark-all t]
1844         ["Mark buffer" gnus-uu-mark-buffer t]
1845         ["Mark sparse" gnus-uu-mark-sparse t]
1846         ["Mark thread" gnus-uu-mark-thread t]
1847         ["Unmark thread" gnus-uu-unmark-thread t]
1848         ("Process Mark Sets"
1849          ["Kill" gnus-summary-kill-process-mark t]
1850          ["Yank" gnus-summary-yank-process-mark
1851           gnus-newsgroup-process-stack]
1852          ["Save" gnus-summary-save-process-mark t]))
1853        ("Scroll article"
1854         ["Page forward" gnus-summary-next-page t]
1855         ["Page backward" gnus-summary-prev-page t]
1856         ["Line forward" gnus-summary-scroll-up t])
1857        ("Move"
1858         ["Next unread article" gnus-summary-next-unread-article t]
1859         ["Previous unread article" gnus-summary-prev-unread-article t]
1860         ["Next article" gnus-summary-next-article t]
1861         ["Previous article" gnus-summary-prev-article t]
1862         ["Next unread subject" gnus-summary-next-unread-subject t]
1863         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1864         ["Next article same subject" gnus-summary-next-same-subject t]
1865         ["Previous article same subject" gnus-summary-prev-same-subject t]
1866         ["First unread article" gnus-summary-first-unread-article t]
1867         ["Best unread article" gnus-summary-best-unread-article t]
1868         ["Go to subject number..." gnus-summary-goto-subject t]
1869         ["Go to article number..." gnus-summary-goto-article t]
1870         ["Go to the last article" gnus-summary-goto-last-article t]
1871         ["Pop article off history" gnus-summary-pop-article t])
1872        ("Sort"
1873         ["Sort by number" gnus-summary-sort-by-number t]
1874         ["Sort by author" gnus-summary-sort-by-author t]
1875         ["Sort by subject" gnus-summary-sort-by-subject t]
1876         ["Sort by date" gnus-summary-sort-by-date t]
1877         ["Sort by score" gnus-summary-sort-by-score t]
1878         ["Sort by lines" gnus-summary-sort-by-lines t]
1879         ["Sort by characters" gnus-summary-sort-by-chars t])
1880        ("Help"
1881         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1882         ["Describe group" gnus-summary-describe-group t]
1883         ["Read manual" gnus-info-find-node t])
1884        ("Modes"
1885         ["Pick and read" gnus-pick-mode t]
1886         ["Binary" gnus-binary-mode t])
1887        ("Regeneration"
1888         ["Regenerate" gnus-summary-prepare t]
1889         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1890         ["Toggle threading" gnus-summary-toggle-threads t])
1891        ["Filter articles..." gnus-summary-execute-command t]
1892        ["Run command on subjects..." gnus-summary-universal-argument t]
1893        ["Search articles forward..." gnus-summary-search-article-forward t]
1894        ["Search articles backward..." gnus-summary-search-article-backward t]
1895        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1896        ["Expand window" gnus-summary-expand-window t]
1897        ["Expire expirable articles" gnus-summary-expire-articles
1898         (gnus-check-backend-function
1899          'request-expire-articles gnus-newsgroup-name)]
1900        ["Edit local kill file" gnus-summary-edit-local-kill t]
1901        ["Edit main kill file" gnus-summary-edit-global-kill t]
1902        ["Edit group parameters" gnus-summary-edit-parameters t]
1903        ["Customize group parameters" gnus-summary-customize-parameters t]
1904        ["Send a bug report" gnus-bug t]
1905        ("Exit"
1906         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1907         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1908         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1909         ["Exit group" gnus-summary-exit t]
1910         ["Exit group without updating" gnus-summary-exit-no-update t]
1911         ["Exit and goto next group" gnus-summary-next-group t]
1912         ["Exit and goto prev group" gnus-summary-prev-group t]
1913         ["Reselect group" gnus-summary-reselect-current-group t]
1914         ["Rescan group" gnus-summary-rescan-group t]
1915         ["Update dribble" gnus-summary-save-newsrc t])))
1916
1917     (gnus-run-hooks 'gnus-summary-menu-hook)))
1918
1919 (defun gnus-score-set-default (var value)
1920   "A version of set that updates the GNU Emacs menu-bar."
1921   (set var value)
1922   ;; It is the message that forces the active status to be updated.
1923   (message ""))
1924
1925 (defun gnus-make-score-map (type)
1926   "Make a summary score map of type TYPE."
1927   (if t
1928       nil
1929     (let ((headers '(("author" "from" string)
1930                      ("subject" "subject" string)
1931                      ("article body" "body" string)
1932                      ("article head" "head" string)
1933                      ("xref" "xref" string)
1934                      ("extra header" "extra" string)
1935                      ("lines" "lines" number)
1936                      ("followups to author" "followup" string)))
1937           (types '((number ("less than" <)
1938                            ("greater than" >)
1939                            ("equal" =))
1940                    (string ("substring" s)
1941                            ("exact string" e)
1942                            ("fuzzy string" f)
1943                            ("regexp" r))))
1944           (perms '(("temporary" (current-time-string))
1945                    ("permanent" nil)
1946                    ("immediate" now)))
1947           header)
1948       (list
1949        (apply
1950         'nconc
1951         (list
1952          (if (eq type 'lower)
1953              "Lower score"
1954            "Increase score"))
1955         (let (outh)
1956           (while headers
1957             (setq header (car headers))
1958             (setq outh
1959                   (cons
1960                    (apply
1961                     'nconc
1962                     (list (car header))
1963                     (let ((ts (cdr (assoc (nth 2 header) types)))
1964                           outt)
1965                       (while ts
1966                         (setq outt
1967                               (cons
1968                                (apply
1969                                 'nconc
1970                                 (list (caar ts))
1971                                 (let ((ps perms)
1972                                       outp)
1973                                   (while ps
1974                                     (setq outp
1975                                           (cons
1976                                            (vector
1977                                             (caar ps)
1978                                             (list
1979                                              'gnus-summary-score-entry
1980                                              (nth 1 header)
1981                                              (if (or (string= (nth 1 header)
1982                                                               "head")
1983                                                      (string= (nth 1 header)
1984                                                               "body"))
1985                                                  ""
1986                                                (list 'gnus-summary-header
1987                                                      (nth 1 header)))
1988                                              (list 'quote (nth 1 (car ts)))
1989                                              (list 'gnus-score-default nil)
1990                                              (nth 1 (car ps))
1991                                              t)
1992                                             t)
1993                                            outp))
1994                                     (setq ps (cdr ps)))
1995                                   (list (nreverse outp))))
1996                                outt))
1997                         (setq ts (cdr ts)))
1998                       (list (nreverse outt))))
1999                    outh))
2000             (setq headers (cdr headers)))
2001           (list (nreverse outh))))))))
2002
2003 \f
2004
2005 (defun gnus-summary-mode (&optional group)
2006   "Major mode for reading articles.
2007
2008 All normal editing commands are switched off.
2009 \\<gnus-summary-mode-map>
2010 Each line in this buffer represents one article.  To read an
2011 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2012 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2013 respectively.
2014
2015 You can also post articles and send mail from this buffer.  To
2016 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2017 of an article, type `\\[gnus-summary-reply]'.
2018
2019 There are approx. one gazillion commands you can execute in this
2020 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2021
2022 The following commands are available:
2023
2024 \\{gnus-summary-mode-map}"
2025   (interactive)
2026   (when (gnus-visual-p 'summary-menu 'menu)
2027     (gnus-summary-make-menu-bar))
2028   (kill-all-local-variables)
2029   (gnus-summary-make-local-variables)
2030   (gnus-make-thread-indent-array)
2031   (gnus-simplify-mode-line)
2032   (setq major-mode 'gnus-summary-mode)
2033   (setq mode-name "Summary")
2034   (make-local-variable 'minor-mode-alist)
2035   (use-local-map gnus-summary-mode-map)
2036   (buffer-disable-undo)
2037   (setq buffer-read-only t)             ;Disable modification
2038   (setq truncate-lines t)
2039   (setq selective-display t)
2040   (setq selective-display-ellipses t)   ;Display `...'
2041   (gnus-summary-set-display-table)
2042   (gnus-set-default-directory)
2043   (setq gnus-newsgroup-name group)
2044   (make-local-variable 'gnus-summary-line-format)
2045   (make-local-variable 'gnus-summary-line-format-spec)
2046   (make-local-variable 'gnus-summary-dummy-line-format)
2047   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2048   (make-local-variable 'gnus-summary-mark-positions)
2049   (make-local-hook 'pre-command-hook)
2050   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2051   (gnus-run-hooks 'gnus-summary-mode-hook)
2052   (mm-enable-multibyte)
2053   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2054   (gnus-update-summary-mark-positions))
2055
2056 (defun gnus-summary-make-local-variables ()
2057   "Make all the local summary buffer variables."
2058   (let (global)
2059     (dolist (local gnus-summary-local-variables)
2060       (if (consp local)
2061           (progn
2062             (if (eq (cdr local) 'global)
2063                 ;; Copy the global value of the variable.
2064                 (setq global (symbol-value (car local)))
2065               ;; Use the value from the list.
2066               (setq global (eval (cdr local))))
2067             (set (make-local-variable (car local)) global))
2068         ;; Simple nil-valued local variable.
2069         (set (make-local-variable local) nil)))))
2070
2071 (defun gnus-summary-clear-local-variables ()
2072   (let ((locals gnus-summary-local-variables))
2073     (while locals
2074       (if (consp (car locals))
2075           (and (vectorp (caar locals))
2076                (set (caar locals) nil))
2077         (and (vectorp (car locals))
2078              (set (car locals) nil)))
2079       (setq locals (cdr locals)))))
2080
2081 ;; Summary data functions.
2082
2083 (defmacro gnus-data-number (data)
2084   `(car ,data))
2085
2086 (defmacro gnus-data-set-number (data number)
2087   `(setcar ,data ,number))
2088
2089 (defmacro gnus-data-mark (data)
2090   `(nth 1 ,data))
2091
2092 (defmacro gnus-data-set-mark (data mark)
2093   `(setcar (nthcdr 1 ,data) ,mark))
2094
2095 (defmacro gnus-data-pos (data)
2096   `(nth 2 ,data))
2097
2098 (defmacro gnus-data-set-pos (data pos)
2099   `(setcar (nthcdr 2 ,data) ,pos))
2100
2101 (defmacro gnus-data-header (data)
2102   `(nth 3 ,data))
2103
2104 (defmacro gnus-data-set-header (data header)
2105   `(setf (nth 3 ,data) ,header))
2106
2107 (defmacro gnus-data-level (data)
2108   `(nth 4 ,data))
2109
2110 (defmacro gnus-data-unread-p (data)
2111   `(= (nth 1 ,data) gnus-unread-mark))
2112
2113 (defmacro gnus-data-read-p (data)
2114   `(/= (nth 1 ,data) gnus-unread-mark))
2115
2116 (defmacro gnus-data-pseudo-p (data)
2117   `(consp (nth 3 ,data)))
2118
2119 (defmacro gnus-data-find (number)
2120   `(assq ,number gnus-newsgroup-data))
2121
2122 (defmacro gnus-data-find-list (number &optional data)
2123   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2124      (memq (assq ,number bdata)
2125            bdata)))
2126
2127 (defmacro gnus-data-make (number mark pos header level)
2128   `(list ,number ,mark ,pos ,header ,level))
2129
2130 (defun gnus-data-enter (after-article number mark pos header level offset)
2131   (let ((data (gnus-data-find-list after-article)))
2132     (unless data
2133       (error "No such article: %d" after-article))
2134     (setcdr data (cons (gnus-data-make number mark pos header level)
2135                        (cdr data)))
2136     (setq gnus-newsgroup-data-reverse nil)
2137     (gnus-data-update-list (cddr data) offset)))
2138
2139 (defun gnus-data-enter-list (after-article list &optional offset)
2140   (when list
2141     (let ((data (and after-article (gnus-data-find-list after-article)))
2142           (ilist list))
2143       (if (not (or data
2144                    after-article))
2145           (let ((odata gnus-newsgroup-data))
2146             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2147             (when offset
2148               (gnus-data-update-list odata offset)))
2149         ;; Find the last element in the list to be spliced into the main
2150         ;; list.
2151         (while (cdr list)
2152           (setq list (cdr list)))
2153         (if (not data)
2154             (progn
2155               (setcdr list gnus-newsgroup-data)
2156               (setq gnus-newsgroup-data ilist)
2157               (when offset
2158                 (gnus-data-update-list (cdr list) offset)))
2159           (setcdr list (cdr data))
2160           (setcdr data ilist)
2161           (when offset
2162             (gnus-data-update-list (cdr list) offset))))
2163       (setq gnus-newsgroup-data-reverse nil))))
2164
2165 (defun gnus-data-remove (article &optional offset)
2166   (let ((data gnus-newsgroup-data))
2167     (if (= (gnus-data-number (car data)) article)
2168         (progn
2169           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2170                 gnus-newsgroup-data-reverse nil)
2171           (when offset
2172             (gnus-data-update-list gnus-newsgroup-data offset)))
2173       (while (cdr data)
2174         (when (= (gnus-data-number (cadr data)) article)
2175           (setcdr data (cddr data))
2176           (when offset
2177             (gnus-data-update-list (cdr data) offset))
2178           (setq data nil
2179                 gnus-newsgroup-data-reverse nil))
2180         (setq data (cdr data))))))
2181
2182 (defmacro gnus-data-list (backward)
2183   `(if ,backward
2184        (or gnus-newsgroup-data-reverse
2185            (setq gnus-newsgroup-data-reverse
2186                  (reverse gnus-newsgroup-data)))
2187      gnus-newsgroup-data))
2188
2189 (defun gnus-data-update-list (data offset)
2190   "Add OFFSET to the POS of all data entries in DATA."
2191   (setq gnus-newsgroup-data-reverse nil)
2192   (while data
2193     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2194     (setq data (cdr data))))
2195
2196 (defun gnus-summary-article-pseudo-p (article)
2197   "Say whether this article is a pseudo article or not."
2198   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2199
2200 (defmacro gnus-summary-article-sparse-p (article)
2201   "Say whether this article is a sparse article or not."
2202   `(memq ,article gnus-newsgroup-sparse))
2203
2204 (defmacro gnus-summary-article-ancient-p (article)
2205   "Say whether this article is a sparse article or not."
2206   `(memq ,article gnus-newsgroup-ancient))
2207
2208 (defun gnus-article-parent-p (number)
2209   "Say whether this article is a parent or not."
2210   (let ((data (gnus-data-find-list number)))
2211     (and (cdr data)                     ; There has to be an article after...
2212          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2213             (gnus-data-level (nth 1 data))))))
2214
2215 (defun gnus-article-children (number)
2216   "Return a list of all children to NUMBER."
2217   (let* ((data (gnus-data-find-list number))
2218          (level (gnus-data-level (car data)))
2219          children)
2220     (setq data (cdr data))
2221     (while (and data
2222                 (= (gnus-data-level (car data)) (1+ level)))
2223       (push (gnus-data-number (car data)) children)
2224       (setq data (cdr data)))
2225     children))
2226
2227 (defmacro gnus-summary-skip-intangible ()
2228   "If the current article is intangible, then jump to a different article."
2229   '(let ((to (get-text-property (point) 'gnus-intangible)))
2230      (and to (gnus-summary-goto-subject to))))
2231
2232 (defmacro gnus-summary-article-intangible-p ()
2233   "Say whether this article is intangible or not."
2234   '(get-text-property (point) 'gnus-intangible))
2235
2236 (defun gnus-article-read-p (article)
2237   "Say whether ARTICLE is read or not."
2238   (not (or (memq article gnus-newsgroup-marked)
2239            (memq article gnus-newsgroup-unreads)
2240            (memq article gnus-newsgroup-unselected)
2241            (memq article gnus-newsgroup-dormant))))
2242
2243 ;; Some summary mode macros.
2244
2245 (defmacro gnus-summary-article-number ()
2246   "The article number of the article on the current line.
2247 If there isn's an article number here, then we return the current
2248 article number."
2249   '(progn
2250      (gnus-summary-skip-intangible)
2251      (or (get-text-property (point) 'gnus-number)
2252          (gnus-summary-last-subject))))
2253
2254 (defmacro gnus-summary-article-header (&optional number)
2255   "Return the header of article NUMBER."
2256   `(gnus-data-header (gnus-data-find
2257                       ,(or number '(gnus-summary-article-number)))))
2258
2259 (defmacro gnus-summary-thread-level (&optional number)
2260   "Return the level of thread that starts with article NUMBER."
2261   `(if (and (eq gnus-summary-make-false-root 'dummy)
2262             (get-text-property (point) 'gnus-intangible))
2263        0
2264      (gnus-data-level (gnus-data-find
2265                        ,(or number '(gnus-summary-article-number))))))
2266
2267 (defmacro gnus-summary-article-mark (&optional number)
2268   "Return the mark of article NUMBER."
2269   `(gnus-data-mark (gnus-data-find
2270                     ,(or number '(gnus-summary-article-number)))))
2271
2272 (defmacro gnus-summary-article-pos (&optional number)
2273   "Return the position of the line of article NUMBER."
2274   `(gnus-data-pos (gnus-data-find
2275                    ,(or number '(gnus-summary-article-number)))))
2276
2277 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2278 (defmacro gnus-summary-article-subject (&optional number)
2279   "Return current subject string or nil if nothing."
2280   `(let ((headers
2281           ,(if number
2282                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2283              '(gnus-data-header (assq (gnus-summary-article-number)
2284                                       gnus-newsgroup-data)))))
2285      (and headers
2286           (vectorp headers)
2287           (mail-header-subject headers))))
2288
2289 (defmacro gnus-summary-article-score (&optional number)
2290   "Return current article score."
2291   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2292                   gnus-newsgroup-scored))
2293        gnus-summary-default-score 0))
2294
2295 (defun gnus-summary-article-children (&optional number)
2296   "Return a list of article numbers that are children of article NUMBER."
2297   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2298          (level (gnus-data-level (car data)))
2299          l children)
2300     (while (and (setq data (cdr data))
2301                 (> (setq l (gnus-data-level (car data))) level))
2302       (and (= (1+ level) l)
2303            (push (gnus-data-number (car data))
2304                  children)))
2305     (nreverse children)))
2306
2307 (defun gnus-summary-article-parent (&optional number)
2308   "Return the article number of the parent of article NUMBER."
2309   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2310                                     (gnus-data-list t)))
2311          (level (gnus-data-level (car data))))
2312     (if (zerop level)
2313         ()                              ; This is a root.
2314       ;; We search until we find an article with a level less than
2315       ;; this one.  That function has to be the parent.
2316       (while (and (setq data (cdr data))
2317                   (not (< (gnus-data-level (car data)) level))))
2318       (and data (gnus-data-number (car data))))))
2319
2320 (defun gnus-unread-mark-p (mark)
2321   "Say whether MARK is the unread mark."
2322   (= mark gnus-unread-mark))
2323
2324 (defun gnus-read-mark-p (mark)
2325   "Say whether MARK is one of the marks that mark as read.
2326 This is all marks except unread, ticked, dormant, and expirable."
2327   (not (or (= mark gnus-unread-mark)
2328            (= mark gnus-ticked-mark)
2329            (= mark gnus-dormant-mark)
2330            (= mark gnus-expirable-mark))))
2331
2332 (defmacro gnus-article-mark (number)
2333   "Return the MARK of article NUMBER.
2334 This macro should only be used when computing the mark the \"first\"
2335 time; i.e., when generating the summary lines.  After that,
2336 `gnus-summary-article-mark' should be used to examine the
2337 marks of articles."
2338   `(cond
2339     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2340     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2341     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2342     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2343     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2344     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2345     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2346     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2347            gnus-ancient-mark))))
2348
2349 ;; Saving hidden threads.
2350
2351 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2352 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2353
2354 (defmacro gnus-save-hidden-threads (&rest forms)
2355   "Save hidden threads, eval FORMS, and restore the hidden threads."
2356   (let ((config (make-symbol "config")))
2357     `(let ((,config (gnus-hidden-threads-configuration)))
2358        (unwind-protect
2359            (save-excursion
2360              ,@forms)
2361          (gnus-restore-hidden-threads-configuration ,config)))))
2362
2363 (defun gnus-data-compute-positions ()
2364   "Compute the positions of all articles."
2365   (setq gnus-newsgroup-data-reverse nil)
2366   (let ((data gnus-newsgroup-data))
2367     (save-excursion
2368       (gnus-save-hidden-threads
2369         (gnus-summary-show-all-threads)
2370         (goto-char (point-min))
2371         (while data
2372           (while (get-text-property (point) 'gnus-intangible)
2373             (forward-line 1))
2374           (gnus-data-set-pos (car data) (+ (point) 3))
2375           (setq data (cdr data))
2376           (forward-line 1))))))
2377
2378 (defun gnus-hidden-threads-configuration ()
2379   "Return the current hidden threads configuration."
2380   (save-excursion
2381     (let (config)
2382       (goto-char (point-min))
2383       (while (search-forward "\r" nil t)
2384         (push (1- (point)) config))
2385       config)))
2386
2387 (defun gnus-restore-hidden-threads-configuration (config)
2388   "Restore hidden threads configuration from CONFIG."
2389   (let (point buffer-read-only)
2390     (while (setq point (pop config))
2391       (when (and (< point (point-max))
2392                  (goto-char point)
2393                  (eq (char-after) ?\n))
2394         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2395
2396 ;; Various summary mode internalish functions.
2397
2398 (defun gnus-mouse-pick-article (e)
2399   (interactive "e")
2400   (mouse-set-point e)
2401   (gnus-summary-next-page nil t))
2402
2403 (defun gnus-summary-set-display-table ()
2404   ;; Change the display table.  Odd characters have a tendency to mess
2405   ;; up nicely formatted displays - we make all possible glyphs
2406   ;; display only a single character.
2407
2408   ;; We start from the standard display table, if any.
2409   (let ((table (or (copy-sequence standard-display-table)
2410                    (make-display-table)))
2411         (i 32))
2412     ;; Nix out all the control chars...
2413     (while (>= (setq i (1- i)) 0)
2414       (aset table i [??]))
2415     ;; ... but not newline and cr, of course.  (cr is necessary for the
2416     ;; selective display).
2417     (aset table ?\n nil)
2418     (aset table ?\r nil)
2419     ;; We keep TAB as well.
2420     (aset table ?\t nil)
2421     ;; We nix out any glyphs over 126 that are not set already.
2422     (let ((i 256))
2423       (while (>= (setq i (1- i)) 127)
2424         ;; Only modify if the entry is nil.
2425         (unless (aref table i)
2426           (aset table i [??]))))
2427     (setq buffer-display-table table)))
2428
2429 (defun gnus-summary-setup-buffer (group)
2430   "Initialize summary buffer."
2431   (let ((buffer (concat "*Summary " group "*")))
2432     (if (get-buffer buffer)
2433         (progn
2434           (set-buffer buffer)
2435           (setq gnus-summary-buffer (current-buffer))
2436           (not gnus-newsgroup-prepared))
2437       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2438       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2439       (gnus-summary-mode group)
2440       (when gnus-carpal
2441         (gnus-carpal-setup-buffer 'summary))
2442       (unless gnus-single-article-buffer
2443         (make-local-variable 'gnus-article-buffer)
2444         (make-local-variable 'gnus-article-current)
2445         (make-local-variable 'gnus-original-article-buffer))
2446       (setq gnus-newsgroup-name group)
2447       t)))
2448
2449 (defun gnus-set-global-variables ()
2450   ;; Set the global equivalents of the summary buffer-local variables
2451   ;; to the latest values they had.  These reflect the summary buffer
2452   ;; that was in action when the last article was fetched.
2453   (when (eq major-mode 'gnus-summary-mode)
2454     (setq gnus-summary-buffer (current-buffer))
2455     (let ((name gnus-newsgroup-name)
2456           (marked gnus-newsgroup-marked)
2457           (unread gnus-newsgroup-unreads)
2458           (headers gnus-current-headers)
2459           (data gnus-newsgroup-data)
2460           (summary gnus-summary-buffer)
2461           (article-buffer gnus-article-buffer)
2462           (original gnus-original-article-buffer)
2463           (gac gnus-article-current)
2464           (reffed gnus-reffed-article-number)
2465           (score-file gnus-current-score-file)
2466           (default-charset gnus-newsgroup-charset))
2467       (save-excursion
2468         (set-buffer gnus-group-buffer)
2469         (setq gnus-newsgroup-name name
2470               gnus-newsgroup-marked marked
2471               gnus-newsgroup-unreads unread
2472               gnus-current-headers headers
2473               gnus-newsgroup-data data
2474               gnus-article-current gac
2475               gnus-summary-buffer summary
2476               gnus-article-buffer article-buffer
2477               gnus-original-article-buffer original
2478               gnus-reffed-article-number reffed
2479               gnus-current-score-file score-file
2480               gnus-newsgroup-charset default-charset)
2481         ;; The article buffer also has local variables.
2482         (when (gnus-buffer-live-p gnus-article-buffer)
2483           (set-buffer gnus-article-buffer)
2484           (setq gnus-summary-buffer summary))))))
2485
2486 (defun gnus-summary-article-unread-p (article)
2487   "Say whether ARTICLE is unread or not."
2488   (memq article gnus-newsgroup-unreads))
2489
2490 (defun gnus-summary-first-article-p (&optional article)
2491   "Return whether ARTICLE is the first article in the buffer."
2492   (if (not (setq article (or article (gnus-summary-article-number))))
2493       nil
2494     (eq article (caar gnus-newsgroup-data))))
2495
2496 (defun gnus-summary-last-article-p (&optional article)
2497   "Return whether ARTICLE is the last article in the buffer."
2498   (if (not (setq article (or article (gnus-summary-article-number))))
2499       ;; All non-existent numbers are the last article.  :-)
2500       t
2501     (not (cdr (gnus-data-find-list article)))))
2502
2503 (defun gnus-make-thread-indent-array ()
2504   (let ((n 200))
2505     (unless (and gnus-thread-indent-array
2506                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2507       (setq gnus-thread-indent-array (make-vector 201 "")
2508             gnus-thread-indent-array-level gnus-thread-indent-level)
2509       (while (>= n 0)
2510         (aset gnus-thread-indent-array n
2511               (make-string (* n gnus-thread-indent-level) ? ))
2512         (setq n (1- n))))))
2513
2514 (defun gnus-update-summary-mark-positions ()
2515   "Compute where the summary marks are to go."
2516   (save-excursion
2517     (when (gnus-buffer-exists-p gnus-summary-buffer)
2518       (set-buffer gnus-summary-buffer))
2519     (let ((gnus-replied-mark 129)
2520           (gnus-score-below-mark 130)
2521           (gnus-score-over-mark 130)
2522           (gnus-download-mark 131)
2523           (spec gnus-summary-line-format-spec)
2524           gnus-visual pos)
2525       (save-excursion
2526         (gnus-set-work-buffer)
2527         (let ((gnus-summary-line-format-spec spec)
2528               (gnus-newsgroup-downloadable '((0 . t))))
2529           (gnus-summary-insert-line
2530            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2531           (goto-char (point-min))
2532           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2533                                              (- (point) 2)))))
2534           (goto-char (point-min))
2535           (push (cons 'replied (and (search-forward "\201" nil t)
2536                                     (- (point) 2)))
2537                 pos)
2538           (goto-char (point-min))
2539           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2540                 pos)
2541           (goto-char (point-min))
2542           (push (cons 'download
2543                       (and (search-forward "\203" nil t) (- (point) 2)))
2544                 pos)))
2545       (setq gnus-summary-mark-positions pos))))
2546
2547 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2548   "Insert a dummy root in the summary buffer."
2549   (beginning-of-line)
2550   (gnus-add-text-properties
2551    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2552    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2553
2554 (defun gnus-summary-from-or-to-or-newsgroups (header)
2555   (let ((to (cdr (assq 'To (mail-header-extra header))))
2556         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2557         (mail-parse-charset gnus-newsgroup-charset)
2558         (mail-parse-ignored-charsets 
2559          (save-excursion (set-buffer gnus-summary-buffer)
2560                          gnus-newsgroup-ignored-charsets)))
2561     (cond
2562      ((and to
2563            gnus-ignored-from-addresses
2564            (string-match gnus-ignored-from-addresses
2565                          (mail-header-from header)))
2566       (concat "-> "
2567               (or (car (funcall gnus-extract-address-components
2568                                 (funcall
2569                                  gnus-decode-encoded-word-function to)))
2570                   (funcall gnus-decode-encoded-word-function to))))
2571      ((and newsgroups
2572            gnus-ignored-from-addresses
2573            (string-match gnus-ignored-from-addresses
2574                          (mail-header-from header)))
2575       (concat "=> " newsgroups))
2576      (t
2577       (or (car (funcall gnus-extract-address-components
2578                         (mail-header-from header)))
2579           (mail-header-from header))))))
2580
2581 (defun gnus-summary-insert-line (gnus-tmp-header
2582                                  gnus-tmp-level gnus-tmp-current
2583                                  gnus-tmp-unread gnus-tmp-replied
2584                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2585                                  &optional gnus-tmp-dummy gnus-tmp-score
2586                                  gnus-tmp-process)
2587   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2588          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2589          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2590          (gnus-tmp-score-char
2591           (if (or (null gnus-summary-default-score)
2592                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2593                       gnus-summary-zcore-fuzz))
2594               ? ;Whitespace
2595             (if (< gnus-tmp-score gnus-summary-default-score)
2596                 gnus-score-below-mark gnus-score-over-mark)))
2597          (gnus-tmp-replied
2598           (cond (gnus-tmp-process gnus-process-mark)
2599                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2600                  gnus-cached-mark)
2601                 (gnus-tmp-replied gnus-replied-mark)
2602                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2603                  gnus-saved-mark)
2604                 (t gnus-unread-mark)))
2605          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2606          (gnus-tmp-name
2607           (cond
2608            ((string-match "<[^>]+> *$" gnus-tmp-from)
2609             (let ((beg (match-beginning 0)))
2610               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2611                        (substring gnus-tmp-from (1+ (match-beginning 0))
2612                                   (1- (match-end 0))))
2613                   (substring gnus-tmp-from 0 beg))))
2614            ((string-match "(.+)" gnus-tmp-from)
2615             (substring gnus-tmp-from
2616                        (1+ (match-beginning 0)) (1- (match-end 0))))
2617            (t gnus-tmp-from)))
2618          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2619          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2620          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2621          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2622          (buffer-read-only nil))
2623     (when (string= gnus-tmp-name "")
2624       (setq gnus-tmp-name gnus-tmp-from))
2625     (unless (numberp gnus-tmp-lines)
2626       (setq gnus-tmp-lines 0))
2627     (gnus-put-text-property
2628      (point)
2629      (progn (eval gnus-summary-line-format-spec) (point))
2630      'gnus-number gnus-tmp-number)
2631     (when (gnus-visual-p 'summary-highlight 'highlight)
2632       (forward-line -1)
2633       (gnus-run-hooks 'gnus-summary-update-hook)
2634       (forward-line 1))))
2635
2636 (defun gnus-summary-update-line (&optional dont-update)
2637   ;; Update summary line after change.
2638   (when (and gnus-summary-default-score
2639              (not gnus-summary-inhibit-highlight))
2640     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2641            (article (gnus-summary-article-number))
2642            (score (gnus-summary-article-score article)))
2643       (unless dont-update
2644         (if (and gnus-summary-mark-below
2645                  (< (gnus-summary-article-score)
2646                     gnus-summary-mark-below))
2647             ;; This article has a low score, so we mark it as read.
2648             (when (memq article gnus-newsgroup-unreads)
2649               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2650           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2651             ;; This article was previously marked as read on account
2652             ;; of a low score, but now it has risen, so we mark it as
2653             ;; unread.
2654             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2655         (gnus-summary-update-mark
2656          (if (or (null gnus-summary-default-score)
2657                  (<= (abs (- score gnus-summary-default-score))
2658                      gnus-summary-zcore-fuzz))
2659              ? ;Whitespace
2660            (if (< score gnus-summary-default-score)
2661                gnus-score-below-mark gnus-score-over-mark))
2662          'score))
2663       ;; Do visual highlighting.
2664       (when (gnus-visual-p 'summary-highlight 'highlight)
2665         (gnus-run-hooks 'gnus-summary-update-hook)))))
2666
2667 (defvar gnus-tmp-new-adopts nil)
2668
2669 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2670   "Return the number of articles in THREAD.
2671 This may be 0 in some cases -- if none of the articles in
2672 the thread are to be displayed."
2673   (let* ((number
2674           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2675           (cond
2676            ((not (listp thread))
2677             1)
2678            ((and (consp thread) (cdr thread))
2679             (apply
2680              '+ 1 (mapcar
2681                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2682            ((null thread)
2683             1)
2684            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2685             1)
2686            (t 0))))
2687     (when (and level (zerop level) gnus-tmp-new-adopts)
2688       (incf number
2689             (apply '+ (mapcar
2690                        'gnus-summary-number-of-articles-in-thread
2691                        gnus-tmp-new-adopts))))
2692     (if char
2693         (if (> number 1) gnus-not-empty-thread-mark
2694           gnus-empty-thread-mark)
2695       number)))
2696
2697 (defun gnus-summary-set-local-parameters (group)
2698   "Go through the local params of GROUP and set all variable specs in that list."
2699   (let ((params (gnus-group-find-parameter group))
2700         elem)
2701     (while params
2702       (setq elem (car params)
2703             params (cdr params))
2704       (and (consp elem)                 ; Has to be a cons.
2705            (consp (cdr elem))           ; The cdr has to be a list.
2706            (symbolp (car elem))         ; Has to be a symbol in there.
2707            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2708            (ignore-errors               ; So we set it.
2709              (make-local-variable (car elem))
2710              (set (car elem) (eval (nth 1 elem))))))))
2711
2712 (defun gnus-summary-read-group (group &optional show-all no-article
2713                                       kill-buffer no-display backward
2714                                       select-articles)
2715   "Start reading news in newsgroup GROUP.
2716 If SHOW-ALL is non-nil, already read articles are also listed.
2717 If NO-ARTICLE is non-nil, no article is selected initially.
2718 If NO-DISPLAY, don't generate a summary buffer."
2719   (let (result)
2720     (while (and group
2721                 (null (setq result
2722                             (let ((gnus-auto-select-next nil))
2723                               (or (gnus-summary-read-group-1
2724                                    group show-all no-article
2725                                    kill-buffer no-display
2726                                    select-articles)
2727                                   (setq show-all nil
2728                                         select-articles nil)))))
2729                 (eq gnus-auto-select-next 'quietly))
2730       (set-buffer gnus-group-buffer)
2731       ;; The entry function called above goes to the next
2732       ;; group automatically, so we go two groups back
2733       ;; if we are searching for the previous group.
2734       (when backward
2735         (gnus-group-prev-unread-group 2))
2736       (if (not (equal group (gnus-group-group-name)))
2737           (setq group (gnus-group-group-name))
2738         (setq group nil)))
2739     result))
2740
2741 (defun gnus-summary-read-group-1 (group show-all no-article
2742                                         kill-buffer no-display
2743                                         &optional select-articles)
2744   ;; Killed foreign groups can't be entered.
2745   (when (and (not (gnus-group-native-p group))
2746              (not (gnus-gethash group gnus-newsrc-hashtb)))
2747     (error "Dead non-native groups can't be entered"))
2748   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2749   (let* ((new-group (gnus-summary-setup-buffer group))
2750          (quit-config (gnus-group-quit-config group))
2751          (did-select (and new-group (gnus-select-newsgroup
2752                                      group show-all select-articles))))
2753     (cond
2754      ;; This summary buffer exists already, so we just select it.
2755      ((not new-group)
2756       (gnus-set-global-variables)
2757       (when kill-buffer
2758         (gnus-kill-or-deaden-summary kill-buffer))
2759       (gnus-configure-windows 'summary 'force)
2760       (gnus-set-mode-line 'summary)
2761       (gnus-summary-position-point)
2762       (message "")
2763       t)
2764      ;; We couldn't select this group.
2765      ((null did-select)
2766       (when (and (eq major-mode 'gnus-summary-mode)
2767                  (not (equal (current-buffer) kill-buffer)))
2768         (kill-buffer (current-buffer))
2769         (if (not quit-config)
2770             (progn
2771               ;; Update the info -- marks might need to be removed,
2772               ;; for instance.
2773               (gnus-summary-update-info)
2774               (set-buffer gnus-group-buffer)
2775               (gnus-group-jump-to-group group)
2776               (gnus-group-next-unread-group 1))
2777           (gnus-handle-ephemeral-exit quit-config)))
2778       (gnus-message 3 "Can't select group")
2779       nil)
2780      ;; The user did a `C-g' while prompting for number of articles,
2781      ;; so we exit this group.
2782      ((eq did-select 'quit)
2783       (and (eq major-mode 'gnus-summary-mode)
2784            (not (equal (current-buffer) kill-buffer))
2785            (kill-buffer (current-buffer)))
2786       (when kill-buffer
2787         (gnus-kill-or-deaden-summary kill-buffer))
2788       (if (not quit-config)
2789           (progn
2790             (set-buffer gnus-group-buffer)
2791             (gnus-group-jump-to-group group)
2792             (gnus-group-next-unread-group 1)
2793             (gnus-configure-windows 'group 'force))
2794         (gnus-handle-ephemeral-exit quit-config))
2795       ;; Finally signal the quit.
2796       (signal 'quit nil))
2797      ;; The group was successfully selected.
2798      (t
2799       (gnus-set-global-variables)
2800       ;; Save the active value in effect when the group was entered.
2801       (setq gnus-newsgroup-active
2802             (gnus-copy-sequence
2803              (gnus-active gnus-newsgroup-name)))
2804       ;; You can change the summary buffer in some way with this hook.
2805       (gnus-run-hooks 'gnus-select-group-hook)
2806       ;; Set any local variables in the group parameters.
2807       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2808       (gnus-update-format-specifications
2809        nil 'summary 'summary-mode 'summary-dummy)
2810       (gnus-update-summary-mark-positions)
2811       ;; Do score processing.
2812       (when gnus-use-scoring
2813         (gnus-possibly-score-headers))
2814       ;; Check whether to fill in the gaps in the threads.
2815       (when gnus-build-sparse-threads
2816         (gnus-build-sparse-threads))
2817       ;; Find the initial limit.
2818       (if gnus-show-threads
2819           (if show-all
2820               (let ((gnus-newsgroup-dormant nil))
2821                 (gnus-summary-initial-limit show-all))
2822             (gnus-summary-initial-limit show-all))
2823         ;; When untreaded, all articles are always shown.
2824         (setq gnus-newsgroup-limit
2825               (mapcar
2826                (lambda (header) (mail-header-number header))
2827                gnus-newsgroup-headers)))
2828       ;; Generate the summary buffer.
2829       (unless no-display
2830         (gnus-summary-prepare))
2831       (when gnus-use-trees
2832         (gnus-tree-open group)
2833         (setq gnus-summary-highlight-line-function
2834               'gnus-tree-highlight-article))
2835       ;; If the summary buffer is empty, but there are some low-scored
2836       ;; articles or some excluded dormants, we include these in the
2837       ;; buffer.
2838       (when (and (zerop (buffer-size))
2839                  (not no-display))
2840         (cond (gnus-newsgroup-dormant
2841                (gnus-summary-limit-include-dormant))
2842               ((and gnus-newsgroup-scored show-all)
2843                (gnus-summary-limit-include-expunged t))))
2844       ;; Function `gnus-apply-kill-file' must be called in this hook.
2845       (gnus-run-hooks 'gnus-apply-kill-hook)
2846       (if (and (zerop (buffer-size))
2847                (not no-display))
2848           (progn
2849             ;; This newsgroup is empty.
2850             (gnus-summary-catchup-and-exit nil t)
2851             (gnus-message 6 "No unread news")
2852             (when kill-buffer
2853               (gnus-kill-or-deaden-summary kill-buffer))
2854             ;; Return nil from this function.
2855             nil)
2856         ;; Hide conversation thread subtrees.  We cannot do this in
2857         ;; gnus-summary-prepare-hook since kill processing may not
2858         ;; work with hidden articles.
2859         (and gnus-show-threads
2860              gnus-thread-hide-subtree
2861              (gnus-summary-hide-all-threads))
2862         (when kill-buffer
2863           (gnus-kill-or-deaden-summary kill-buffer))
2864         ;; Show first unread article if requested.
2865         (if (and (not no-article)
2866                  (not no-display)
2867                  gnus-newsgroup-unreads
2868                  gnus-auto-select-first)
2869             (progn
2870               (gnus-configure-windows 'summary)
2871               (cond
2872                ((eq gnus-auto-select-first 'best)
2873                 (gnus-summary-best-unread-article))
2874                ((eq gnus-auto-select-first t)
2875                 (gnus-summary-first-unread-article))
2876                ((gnus-functionp gnus-auto-select-first)
2877                 (funcall gnus-auto-select-first))))
2878           ;; Don't select any articles, just move point to the first
2879           ;; article in the group.
2880           (goto-char (point-min))
2881           (gnus-summary-position-point)
2882           (gnus-configure-windows 'summary 'force)
2883           (gnus-set-mode-line 'summary))
2884         (when (get-buffer-window gnus-group-buffer t)
2885           ;; Gotta use windows, because recenter does weird stuff if
2886           ;; the current buffer ain't the displayed window.
2887           (let ((owin (selected-window)))
2888             (select-window (get-buffer-window gnus-group-buffer t))
2889             (when (gnus-group-goto-group group)
2890               (recenter))
2891             (select-window owin)))
2892         ;; Mark this buffer as "prepared".
2893         (setq gnus-newsgroup-prepared t)
2894         (gnus-run-hooks 'gnus-summary-prepared-hook)
2895         t)))))
2896
2897 (defun gnus-summary-prepare ()
2898   "Generate the summary buffer."
2899   (interactive)
2900   (let ((buffer-read-only nil))
2901     (erase-buffer)
2902     (setq gnus-newsgroup-data nil
2903           gnus-newsgroup-data-reverse nil)
2904     (gnus-run-hooks 'gnus-summary-generate-hook)
2905     ;; Generate the buffer, either with threads or without.
2906     (when gnus-newsgroup-headers
2907       (gnus-summary-prepare-threads
2908        (if gnus-show-threads
2909            (gnus-sort-gathered-threads
2910             (funcall gnus-summary-thread-gathering-function
2911                      (gnus-sort-threads
2912                       (gnus-cut-threads (gnus-make-threads)))))
2913          ;; Unthreaded display.
2914          (gnus-sort-articles gnus-newsgroup-headers))))
2915     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2916     ;; Call hooks for modifying summary buffer.
2917     (goto-char (point-min))
2918     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2919
2920 (defsubst gnus-general-simplify-subject (subject)
2921   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2922   (setq subject
2923         (cond
2924          ;; Truncate the subject.
2925          (gnus-simplify-subject-functions
2926           (gnus-map-function gnus-simplify-subject-functions subject))
2927          ((numberp gnus-summary-gather-subject-limit)
2928           (setq subject (gnus-simplify-subject-re subject))
2929           (if (> (length subject) gnus-summary-gather-subject-limit)
2930               (substring subject 0 gnus-summary-gather-subject-limit)
2931             subject))
2932          ;; Fuzzily simplify it.
2933          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2934           (gnus-simplify-subject-fuzzy subject))
2935          ;; Just remove the leading "Re:".
2936          (t
2937           (gnus-simplify-subject-re subject))))
2938
2939   (if (and gnus-summary-gather-exclude-subject
2940            (string-match gnus-summary-gather-exclude-subject subject))
2941       nil                               ; This article shouldn't be gathered
2942     subject))
2943
2944 (defun gnus-summary-simplify-subject-query ()
2945   "Query where the respool algorithm would put this article."
2946   (interactive)
2947   (gnus-summary-select-article)
2948   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2949
2950 (defun gnus-gather-threads-by-subject (threads)
2951   "Gather threads by looking at Subject headers."
2952   (if (not gnus-summary-make-false-root)
2953       threads
2954     (let ((hashtb (gnus-make-hashtable 1024))
2955           (prev threads)
2956           (result threads)
2957           subject hthread whole-subject)
2958       (while threads
2959         (setq subject (gnus-general-simplify-subject
2960                        (setq whole-subject (mail-header-subject
2961                                             (caar threads)))))
2962         (when subject
2963           (if (setq hthread (gnus-gethash subject hashtb))
2964               (progn
2965                 ;; We enter a dummy root into the thread, if we
2966                 ;; haven't done that already.
2967                 (unless (stringp (caar hthread))
2968                   (setcar hthread (list whole-subject (car hthread))))
2969                 ;; We add this new gathered thread to this gathered
2970                 ;; thread.
2971                 (setcdr (car hthread)
2972                         (nconc (cdar hthread) (list (car threads))))
2973                 ;; Remove it from the list of threads.
2974                 (setcdr prev (cdr threads))
2975                 (setq threads prev))
2976             ;; Enter this thread into the hash table.
2977             (gnus-sethash subject threads hashtb)))
2978         (setq prev threads)
2979         (setq threads (cdr threads)))
2980       result)))
2981
2982 (defun gnus-gather-threads-by-references (threads)
2983   "Gather threads by looking at References headers."
2984   (let ((idhashtb (gnus-make-hashtable 1024))
2985         (thhashtb (gnus-make-hashtable 1024))
2986         (prev threads)
2987         (result threads)
2988         ids references id gthread gid entered ref)
2989     (while threads
2990       (when (setq references (mail-header-references (caar threads)))
2991         (setq id (mail-header-id (caar threads))
2992               ids (gnus-split-references references)
2993               entered nil)
2994         (while (setq ref (pop ids))
2995           (setq ids (delete ref ids))
2996           (if (not (setq gid (gnus-gethash ref idhashtb)))
2997               (progn
2998                 (gnus-sethash ref id idhashtb)
2999                 (gnus-sethash id threads thhashtb))
3000             (setq gthread (gnus-gethash gid thhashtb))
3001             (unless entered
3002               ;; We enter a dummy root into the thread, if we
3003               ;; haven't done that already.
3004               (unless (stringp (caar gthread))
3005                 (setcar gthread (list (mail-header-subject (caar gthread))
3006                                       (car gthread))))
3007               ;; We add this new gathered thread to this gathered
3008               ;; thread.
3009               (setcdr (car gthread)
3010                       (nconc (cdar gthread) (list (car threads)))))
3011             ;; Add it into the thread hash table.
3012             (gnus-sethash id gthread thhashtb)
3013             (setq entered t)
3014             ;; Remove it from the list of threads.
3015             (setcdr prev (cdr threads))
3016             (setq threads prev))))
3017       (setq prev threads)
3018       (setq threads (cdr threads)))
3019     result))
3020
3021 (defun gnus-sort-gathered-threads (threads)
3022   "Sort subtreads inside each gathered thread by article number."
3023   (let ((result threads))
3024     (while threads
3025       (when (stringp (caar threads))
3026         (setcdr (car threads)
3027                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3028       (setq threads (cdr threads)))
3029     result))
3030
3031 (defun gnus-thread-loop-p (root thread)
3032   "Say whether ROOT is in THREAD."
3033   (let ((stack (list thread))
3034         (infloop 0)
3035         th)
3036     (while (setq thread (pop stack))
3037       (setq th (cdr thread))
3038       (while (and th
3039                   (not (eq (caar th) root)))
3040         (pop th))
3041       (if th
3042           ;; We have found a loop.
3043           (let (ref-dep)
3044             (setcdr thread (delq (car th) (cdr thread)))
3045             (if (boundp (setq ref-dep (intern "none"
3046                                               gnus-newsgroup-dependencies)))
3047                 (setcdr (symbol-value ref-dep)
3048                         (nconc (cdr (symbol-value ref-dep))
3049                                (list (car th))))
3050               (set ref-dep (list nil (car th))))
3051             (setq infloop 1
3052                   stack nil))
3053         ;; Push all the subthreads onto the stack.
3054         (push (cdr thread) stack)))
3055     infloop))
3056
3057 (defun gnus-make-threads ()
3058   "Go through the dependency hashtb and find the roots.  Return all threads."
3059   (let (threads)
3060     (while (catch 'infloop
3061              (mapatoms
3062               (lambda (refs)
3063                 ;; Deal with self-referencing References loops.
3064                 (when (and (car (symbol-value refs))
3065                            (not (zerop
3066                                  (apply
3067                                   '+
3068                                   (mapcar
3069                                    (lambda (thread)
3070                                      (gnus-thread-loop-p
3071                                       (car (symbol-value refs)) thread))
3072                                    (cdr (symbol-value refs)))))))
3073                   (setq threads nil)
3074                   (throw 'infloop t))
3075                 (unless (car (symbol-value refs))
3076                   ;; These threads do not refer back to any other articles,
3077                   ;; so they're roots.
3078                   (setq threads (append (cdr (symbol-value refs)) threads))))
3079               gnus-newsgroup-dependencies)))
3080     threads))
3081
3082 ;; Build the thread tree.
3083 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3084   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3085
3086 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3087 if it was already present.
3088
3089 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3090 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3091 Message-IDs will be renamed be renamed to a unique Message-ID before
3092 being entered.
3093
3094 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3095   (let* ((id (mail-header-id header))
3096          (id-dep (and id (intern id dependencies)))
3097          ref ref-dep ref-header)
3098     ;; Enter this `header' in the `dependencies' table.
3099     (cond
3100      ((not id-dep)
3101       (setq header nil))
3102      ;; The first two cases do the normal part: enter a new `header'
3103      ;; in the `dependencies' table.
3104      ((not (boundp id-dep))
3105       (set id-dep (list header)))
3106      ((null (car (symbol-value id-dep)))
3107       (setcar (symbol-value id-dep) header))
3108
3109      ;; From here the `header' was already present in the
3110      ;; `dependencies' table.
3111      (force-new
3112       ;; Overrides an existing entry;
3113       ;; just set the header part of the entry.
3114       (setcar (symbol-value id-dep) header))
3115
3116      ;; Renames the existing `header' to a unique Message-ID.
3117      ((not gnus-summary-ignore-duplicates)
3118       ;; An article with this Message-ID has already been seen.
3119       ;; We rename the Message-ID.
3120       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3121            (list header))
3122       (mail-header-set-id header id))
3123
3124      ;; The last case ignores an existing entry, except it adds any
3125      ;; additional Xrefs (in case the two articles came from different
3126      ;; servers.
3127      ;; Also sets `header' to `nil' meaning that the `dependencies'
3128      ;; table was *not* modified.
3129      (t
3130       (mail-header-set-xref
3131        (car (symbol-value id-dep))
3132        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3133                    "")
3134                (or (mail-header-xref header) "")))
3135       (setq header nil)))
3136
3137     (when header
3138       ;; First check if that we are not creating a References loop.
3139       (setq ref (gnus-parent-id (mail-header-references header)))
3140       (while (and ref
3141                   (setq ref-dep (intern-soft ref dependencies))
3142                   (boundp ref-dep)
3143                   (setq ref-header (car (symbol-value ref-dep))))
3144         (if (string= id ref)
3145             ;; Yuk!  This is a reference loop.  Make the article be a
3146             ;; root article.
3147             (progn
3148               (mail-header-set-references (car (symbol-value id-dep)) "none")
3149               (setq ref nil))
3150           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3151       (setq ref (gnus-parent-id (mail-header-references header)))
3152       (setq ref-dep (intern (or ref "none") dependencies))
3153       (if (boundp ref-dep)
3154           (setcdr (symbol-value ref-dep)
3155                   (nconc (cdr (symbol-value ref-dep))
3156                          (list (symbol-value id-dep))))
3157         (set ref-dep (list nil (symbol-value id-dep)))))
3158     header))
3159
3160 (defun gnus-build-sparse-threads ()
3161   (let ((headers gnus-newsgroup-headers)
3162         (gnus-summary-ignore-duplicates t)
3163         header references generation relations
3164         subject child end new-child date)
3165     ;; First we create an alist of generations/relations, where
3166     ;; generations is how much we trust the relation, and the relation
3167     ;; is parent/child.
3168     (gnus-message 7 "Making sparse threads...")
3169     (save-excursion
3170       (nnheader-set-temp-buffer " *gnus sparse threads*")
3171       (while (setq header (pop headers))
3172         (when (and (setq references (mail-header-references header))
3173                    (not (string= references "")))
3174           (insert references)
3175           (setq child (mail-header-id header)
3176                 subject (mail-header-subject header)
3177                 date (mail-header-date header)
3178                 generation 0)
3179           (while (search-backward ">" nil t)
3180             (setq end (1+ (point)))
3181             (when (search-backward "<" nil t)
3182               (setq new-child (buffer-substring (point) end))
3183               (push (list (incf generation)
3184                           child (setq child new-child)
3185                           subject date)
3186                     relations)))
3187           (when child
3188             (push (list (1+ generation) child nil subject) relations))
3189           (erase-buffer)))
3190       (kill-buffer (current-buffer)))
3191     ;; Sort over trustworthiness.
3192     (mapcar
3193      (lambda (relation)
3194        (when (gnus-dependencies-add-header
3195               (make-full-mail-header
3196                gnus-reffed-article-number
3197                (nth 3 relation) "" (or (nth 4 relation) "")
3198                (nth 1 relation)
3199                (or (nth 2 relation) "") 0 0 "")
3200               gnus-newsgroup-dependencies nil)
3201          (push gnus-reffed-article-number gnus-newsgroup-limit)
3202          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3203          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3204                gnus-newsgroup-reads)
3205          (decf gnus-reffed-article-number)))
3206      (sort relations 'car-less-than-car))
3207     (gnus-message 7 "Making sparse threads...done")))
3208
3209 (defun gnus-build-old-threads ()
3210   ;; Look at all the articles that refer back to old articles, and
3211   ;; fetch the headers for the articles that aren't there.  This will
3212   ;; build complete threads - if the roots haven't been expired by the
3213   ;; server, that is.
3214   (let (id heads)
3215     (mapatoms
3216      (lambda (refs)
3217        (when (not (car (symbol-value refs)))
3218          (setq heads (cdr (symbol-value refs)))
3219          (while heads
3220            (if (memq (mail-header-number (caar heads))
3221                      gnus-newsgroup-dormant)
3222                (setq heads (cdr heads))
3223              (setq id (symbol-name refs))
3224              (while (and (setq id (gnus-build-get-header id))
3225                          (not (car (gnus-id-to-thread id)))))
3226              (setq heads nil)))))
3227      gnus-newsgroup-dependencies)))
3228
3229 ;; This function has to be called with point after the article number
3230 ;; on the beginning of the line.
3231 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3232   (let ((eol (gnus-point-at-eol))
3233         (buffer (current-buffer))
3234         header)
3235
3236     ;; overview: [num subject from date id refs chars lines misc]
3237     (unwind-protect
3238         (progn
3239           (narrow-to-region (point) eol)
3240           (unless (eobp)
3241             (forward-char))
3242
3243           (setq header
3244                 (make-full-mail-header
3245                  number                         ; number
3246                  (funcall gnus-decode-encoded-word-function
3247                           (nnheader-nov-field)) ; subject
3248                  (funcall gnus-decode-encoded-word-function
3249                           (nnheader-nov-field)) ; from
3250                  (nnheader-nov-field)           ; date
3251                  (nnheader-nov-read-message-id) ; id
3252                  (nnheader-nov-field)           ; refs
3253                  (nnheader-nov-read-integer)    ; chars
3254                  (nnheader-nov-read-integer)    ; lines
3255                  (unless (eobp)
3256                    (nnheader-nov-field))                ; misc
3257                  (nnheader-nov-parse-extra))))  ; extra
3258
3259       (widen))
3260
3261     (when gnus-alter-header-function
3262       (funcall gnus-alter-header-function header))
3263     (gnus-dependencies-add-header header dependencies force-new)))
3264
3265 (defun gnus-build-get-header (id)
3266   ;; Look through the buffer of NOV lines and find the header to
3267   ;; ID.  Enter this line into the dependencies hash table, and return
3268   ;; the id of the parent article (if any).
3269   (let ((deps gnus-newsgroup-dependencies)
3270         found header)
3271     (prog1
3272         (save-excursion
3273           (set-buffer nntp-server-buffer)
3274           (let ((case-fold-search nil))
3275             (goto-char (point-min))
3276             (while (and (not found)
3277                         (search-forward id nil t))
3278               (beginning-of-line)
3279               (setq found (looking-at
3280                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3281                                    (regexp-quote id))))
3282               (or found (beginning-of-line 2)))
3283             (when found
3284               (beginning-of-line)
3285               (and
3286                (setq header (gnus-nov-parse-line
3287                              (read (current-buffer)) deps))
3288                (gnus-parent-id (mail-header-references header))))))
3289       (when header
3290         (let ((number (mail-header-number header)))
3291           (push number gnus-newsgroup-limit)
3292           (push header gnus-newsgroup-headers)
3293           (if (memq number gnus-newsgroup-unselected)
3294               (progn
3295                 (push number gnus-newsgroup-unreads)
3296                 (setq gnus-newsgroup-unselected
3297                       (delq number gnus-newsgroup-unselected)))
3298             (push number gnus-newsgroup-ancient)))))))
3299
3300 (defun gnus-build-all-threads ()
3301   "Read all the headers."
3302   (let ((gnus-summary-ignore-duplicates t)
3303         (dependencies gnus-newsgroup-dependencies)
3304         header article)
3305     (save-excursion
3306       (set-buffer nntp-server-buffer)
3307       (let ((case-fold-search nil))
3308         (goto-char (point-min))
3309         (while (not (eobp))
3310           (ignore-errors
3311             (setq article (read (current-buffer))
3312                   header (gnus-nov-parse-line
3313                           article dependencies)))
3314           (when header
3315             (save-excursion
3316               (set-buffer gnus-summary-buffer)
3317               (push header gnus-newsgroup-headers)
3318               (if (memq (setq article (mail-header-number header))
3319                         gnus-newsgroup-unselected)
3320                   (progn
3321                     (push article gnus-newsgroup-unreads)
3322                     (setq gnus-newsgroup-unselected
3323                           (delq article gnus-newsgroup-unselected)))
3324                 (push article gnus-newsgroup-ancient)))
3325             (forward-line 1)))))))
3326
3327 (defun gnus-summary-update-article-line (article header)
3328   "Update the line for ARTICLE using HEADERS."
3329   (let* ((id (mail-header-id header))
3330          (thread (gnus-id-to-thread id)))
3331     (unless thread
3332       (error "Article in no thread"))
3333     ;; Update the thread.
3334     (setcar thread header)
3335     (gnus-summary-goto-subject article)
3336     (let* ((datal (gnus-data-find-list article))
3337            (data (car datal))
3338            (length (when (cdr datal)
3339                      (- (gnus-data-pos data)
3340                         (gnus-data-pos (cadr datal)))))
3341            (buffer-read-only nil)
3342            (level (gnus-summary-thread-level)))
3343       (gnus-delete-line)
3344       (gnus-summary-insert-line
3345        header level nil (gnus-article-mark article)
3346        (memq article gnus-newsgroup-replied)
3347        (memq article gnus-newsgroup-expirable)
3348        ;; Only insert the Subject string when it's different
3349        ;; from the previous Subject string.
3350        (if (gnus-subject-equal
3351             (condition-case ()
3352                 (mail-header-subject
3353                  (gnus-data-header
3354                   (cadr
3355                    (gnus-data-find-list
3356                     article
3357                     (gnus-data-list t)))))
3358               ;; Error on the side of excessive subjects.
3359               (error ""))
3360             (mail-header-subject header))
3361            ""
3362          (mail-header-subject header))
3363        nil (cdr (assq article gnus-newsgroup-scored))
3364        (memq article gnus-newsgroup-processable))
3365       (when length
3366         (gnus-data-update-list
3367          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3368
3369 (defun gnus-summary-update-article (article &optional iheader)
3370   "Update ARTICLE in the summary buffer."
3371   (set-buffer gnus-summary-buffer)
3372   (let* ((header (gnus-summary-article-header article))
3373          (id (mail-header-id header))
3374          (data (gnus-data-find article))
3375          (thread (gnus-id-to-thread id))
3376          (references (mail-header-references header))
3377          (parent
3378           (gnus-id-to-thread
3379            (or (gnus-parent-id
3380                 (when (and references
3381                            (not (equal "" references)))
3382                   references))
3383                "none")))
3384          (buffer-read-only nil)
3385          (old (car thread)))
3386     (when thread
3387       (unless iheader
3388         (setcar thread nil)
3389         (when parent
3390           (delq thread parent)))
3391       (if (gnus-summary-insert-subject id header)
3392           ;; Set the (possibly) new article number in the data structure.
3393           (gnus-data-set-number data (gnus-id-to-article id))
3394         (setcar thread old)
3395         nil))))
3396
3397 (defun gnus-rebuild-thread (id &optional line)
3398   "Rebuild the thread containing ID.
3399 If LINE, insert the rebuilt thread starting on line LINE."
3400   (let ((buffer-read-only nil)
3401         old-pos current thread data)
3402     (if (not gnus-show-threads)
3403         (setq thread (list (car (gnus-id-to-thread id))))
3404       ;; Get the thread this article is part of.
3405       (setq thread (gnus-remove-thread id)))
3406     (setq old-pos (gnus-point-at-bol))
3407     (setq current (save-excursion
3408                     (and (zerop (forward-line -1))
3409                          (gnus-summary-article-number))))
3410     ;; If this is a gathered thread, we have to go some re-gathering.
3411     (when (stringp (car thread))
3412       (let ((subject (car thread))
3413             roots thr)
3414         (setq thread (cdr thread))
3415         (while thread
3416           (unless (memq (setq thr (gnus-id-to-thread
3417                                    (gnus-root-id
3418                                     (mail-header-id (caar thread)))))
3419                         roots)
3420             (push thr roots))
3421           (setq thread (cdr thread)))
3422         ;; We now have all (unique) roots.
3423         (if (= (length roots) 1)
3424             ;; All the loose roots are now one solid root.
3425             (setq thread (car roots))
3426           (setq thread (cons subject (gnus-sort-threads roots))))))
3427     (let (threads)
3428       ;; We then insert this thread into the summary buffer.
3429       (when line
3430         (goto-char (point-min))
3431         (forward-line (1- line)))
3432       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3433         (if gnus-show-threads
3434             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3435           (gnus-summary-prepare-unthreaded thread))
3436         (setq data (nreverse gnus-newsgroup-data))
3437         (setq threads gnus-newsgroup-threads))
3438       ;; We splice the new data into the data structure.
3439       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3440       ;;!!! then we want to insert at the beginning of the buffer.
3441       ;;!!! That happens to be true with Gnus now, but that may
3442       ;;!!! change in the future.  Perhaps.
3443       (gnus-data-enter-list
3444        (if line nil current) data (- (point) old-pos))
3445       (setq gnus-newsgroup-threads
3446             (nconc threads gnus-newsgroup-threads))
3447       (gnus-data-compute-positions))))
3448
3449 (defun gnus-number-to-header (number)
3450   "Return the header for article NUMBER."
3451   (let ((headers gnus-newsgroup-headers))
3452     (while (and headers
3453                 (not (= number (mail-header-number (car headers)))))
3454       (pop headers))
3455     (when headers
3456       (car headers))))
3457
3458 (defun gnus-parent-headers (in-headers &optional generation)
3459   "Return the headers of the GENERATIONeth parent of HEADERS."
3460   (unless generation
3461     (setq generation 1))
3462   (let ((parent t)
3463         (headers in-headers)
3464         references)
3465     (while (and parent
3466                 (not (zerop generation))
3467                 (setq references (mail-header-references headers)))
3468       (setq headers (if (and references
3469                              (setq parent (gnus-parent-id references)))
3470                         (car (gnus-id-to-thread parent))
3471                       nil))
3472       (decf generation))
3473     (and (not (eq headers in-headers))
3474          headers)))
3475
3476 (defun gnus-id-to-thread (id)
3477   "Return the (sub-)thread where ID appears."
3478   (gnus-gethash id gnus-newsgroup-dependencies))
3479
3480 (defun gnus-id-to-article (id)
3481   "Return the article number of ID."
3482   (let ((thread (gnus-id-to-thread id)))
3483     (when (and thread
3484                (car thread))
3485       (mail-header-number (car thread)))))
3486
3487 (defun gnus-id-to-header (id)
3488   "Return the article headers of ID."
3489   (car (gnus-id-to-thread id)))
3490
3491 (defun gnus-article-displayed-root-p (article)
3492   "Say whether ARTICLE is a root(ish) article."
3493   (let ((level (gnus-summary-thread-level article))
3494         (refs (mail-header-references  (gnus-summary-article-header article)))
3495         particle)
3496     (cond
3497      ((null level) nil)
3498      ((zerop level) t)
3499      ((null refs) t)
3500      ((null (gnus-parent-id refs)) t)
3501      ((and (= 1 level)
3502            (null (setq particle (gnus-id-to-article
3503                                  (gnus-parent-id refs))))
3504            (null (gnus-summary-thread-level particle)))))))
3505
3506 (defun gnus-root-id (id)
3507   "Return the id of the root of the thread where ID appears."
3508   (let (last-id prev)
3509     (while (and id (setq prev (car (gnus-id-to-thread id))))
3510       (setq last-id id
3511             id (gnus-parent-id (mail-header-references prev))))
3512     last-id))
3513
3514 (defun gnus-articles-in-thread (thread)
3515   "Return the list of articles in THREAD."
3516   (cons (mail-header-number (car thread))
3517         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3518
3519 (defun gnus-remove-thread (id &optional dont-remove)
3520   "Remove the thread that has ID in it."
3521   (let (headers thread last-id)
3522     ;; First go up in this thread until we find the root.
3523     (setq last-id (gnus-root-id id)
3524           headers (message-flatten-list (gnus-id-to-thread last-id)))
3525     ;; We have now found the real root of this thread.  It might have
3526     ;; been gathered into some loose thread, so we have to search
3527     ;; through the threads to find the thread we wanted.
3528     (let ((threads gnus-newsgroup-threads)
3529           sub)
3530       (while threads
3531         (setq sub (car threads))
3532         (if (stringp (car sub))
3533             ;; This is a gathered thread, so we look at the roots
3534             ;; below it to find whether this article is in this
3535             ;; gathered root.
3536             (progn
3537               (setq sub (cdr sub))
3538               (while sub
3539                 (when (member (caar sub) headers)
3540                   (setq thread (car threads)
3541                         threads nil
3542                         sub nil))
3543                 (setq sub (cdr sub))))
3544           ;; It's an ordinary thread, so we check it.
3545           (when (eq (car sub) (car headers))
3546             (setq thread sub
3547                   threads nil)))
3548         (setq threads (cdr threads)))
3549       ;; If this article is in no thread, then it's a root.
3550       (if thread
3551           (unless dont-remove
3552             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3553         (setq thread (gnus-id-to-thread last-id)))
3554       (when thread
3555         (prog1
3556             thread                      ; We return this thread.
3557           (unless dont-remove
3558             (if (stringp (car thread))
3559                 (progn
3560                   ;; If we use dummy roots, then we have to remove the
3561                   ;; dummy root as well.
3562                   (when (eq gnus-summary-make-false-root 'dummy)
3563                     ;; We go to the dummy root by going to
3564                     ;; the first sub-"thread", and then one line up.
3565                     (gnus-summary-goto-article
3566                      (mail-header-number (caadr thread)))
3567                     (forward-line -1)
3568                     (gnus-delete-line)
3569                     (gnus-data-compute-positions))
3570                   (setq thread (cdr thread))
3571                   (while thread
3572                     (gnus-remove-thread-1 (car thread))
3573                     (setq thread (cdr thread))))
3574               (gnus-summary-show-all-threads)
3575               (gnus-remove-thread-1 thread))))))))
3576
3577 (defun gnus-remove-thread-1 (thread)
3578   "Remove the thread THREAD recursively."
3579   (let ((number (mail-header-number (pop thread)))
3580         d)
3581     (setq thread (reverse thread))
3582     (while thread
3583       (gnus-remove-thread-1 (pop thread)))
3584     (when (setq d (gnus-data-find number))
3585       (goto-char (gnus-data-pos d))
3586       (gnus-data-remove
3587        number
3588        (- (gnus-point-at-bol)
3589           (prog1
3590               (1+ (gnus-point-at-eol))
3591             (gnus-delete-line)))))))
3592
3593 (defun gnus-sort-threads (threads)
3594   "Sort THREADS."
3595   (if (not gnus-thread-sort-functions)
3596       threads
3597     (gnus-message 8 "Sorting threads...")
3598     (prog1
3599         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3600       (gnus-message 8 "Sorting threads...done"))))
3601
3602 (defun gnus-sort-articles (articles)
3603   "Sort ARTICLES."
3604   (when gnus-article-sort-functions
3605     (gnus-message 7 "Sorting articles...")
3606     (prog1
3607         (setq gnus-newsgroup-headers
3608               (sort articles (gnus-make-sort-function
3609                               gnus-article-sort-functions)))
3610       (gnus-message 7 "Sorting articles...done"))))
3611
3612 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3613 (defmacro gnus-thread-header (thread)
3614   ;; Return header of first article in THREAD.
3615   ;; Note that THREAD must never, ever be anything else than a variable -
3616   ;; using some other form will lead to serious barfage.
3617   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3618   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3619   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3620         (vector thread) 2))
3621
3622 (defsubst gnus-article-sort-by-number (h1 h2)
3623   "Sort articles by article number."
3624   (< (mail-header-number h1)
3625      (mail-header-number h2)))
3626
3627 (defun gnus-thread-sort-by-number (h1 h2)
3628   "Sort threads by root article number."
3629   (gnus-article-sort-by-number
3630    (gnus-thread-header h1) (gnus-thread-header h2)))
3631
3632 (defsubst gnus-article-sort-by-lines (h1 h2)
3633   "Sort articles by article Lines header."
3634   (< (mail-header-lines h1)
3635      (mail-header-lines h2)))
3636
3637 (defun gnus-thread-sort-by-lines (h1 h2)
3638   "Sort threads by root article Lines header."
3639   (gnus-article-sort-by-lines
3640    (gnus-thread-header h1) (gnus-thread-header h2)))
3641
3642 (defsubst gnus-article-sort-by-chars (h1 h2)
3643   "Sort articles by octet length."
3644   (< (mail-header-chars h1)
3645      (mail-header-chars h2)))
3646
3647 (defun gnus-thread-sort-by-chars (h1 h2)
3648   "Sort threads by root article octet length."
3649   (gnus-article-sort-by-chars
3650    (gnus-thread-header h1) (gnus-thread-header h2)))
3651
3652 (defsubst gnus-article-sort-by-author (h1 h2)
3653   "Sort articles by root author."
3654   (string-lessp
3655    (let ((extract (funcall
3656                    gnus-extract-address-components
3657                    (mail-header-from h1))))
3658      (or (car extract) (cadr extract) ""))
3659    (let ((extract (funcall
3660                    gnus-extract-address-components
3661                    (mail-header-from h2))))
3662      (or (car extract) (cadr extract) ""))))
3663
3664 (defun gnus-thread-sort-by-author (h1 h2)
3665   "Sort threads by root author."
3666   (gnus-article-sort-by-author
3667    (gnus-thread-header h1)  (gnus-thread-header h2)))
3668
3669 (defsubst gnus-article-sort-by-subject (h1 h2)
3670   "Sort articles by root subject."
3671   (string-lessp
3672    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3673    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3674
3675 (defun gnus-thread-sort-by-subject (h1 h2)
3676   "Sort threads by root subject."
3677   (gnus-article-sort-by-subject
3678    (gnus-thread-header h1) (gnus-thread-header h2)))
3679
3680 (defsubst gnus-article-sort-by-date (h1 h2)
3681   "Sort articles by root article date."
3682   (time-less-p
3683    (gnus-date-get-time (mail-header-date h1))
3684    (gnus-date-get-time (mail-header-date h2))))
3685
3686 (defun gnus-thread-sort-by-date (h1 h2)
3687   "Sort threads by root article date."
3688   (gnus-article-sort-by-date
3689    (gnus-thread-header h1) (gnus-thread-header h2)))
3690
3691 (defsubst gnus-article-sort-by-score (h1 h2)
3692   "Sort articles by root article score.
3693 Unscored articles will be counted as having a score of zero."
3694   (> (or (cdr (assq (mail-header-number h1)
3695                     gnus-newsgroup-scored))
3696          gnus-summary-default-score 0)
3697      (or (cdr (assq (mail-header-number h2)
3698                     gnus-newsgroup-scored))
3699          gnus-summary-default-score 0)))
3700
3701 (defun gnus-thread-sort-by-score (h1 h2)
3702   "Sort threads by root article score."
3703   (gnus-article-sort-by-score
3704    (gnus-thread-header h1) (gnus-thread-header h2)))
3705
3706 (defun gnus-thread-sort-by-total-score (h1 h2)
3707   "Sort threads by the sum of all scores in the thread.
3708 Unscored articles will be counted as having a score of zero."
3709   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3710
3711 (defun gnus-thread-total-score (thread)
3712   ;; This function find the total score of THREAD.
3713   (cond ((null thread)
3714          0)
3715         ((consp thread)
3716          (if (stringp (car thread))
3717              (apply gnus-thread-score-function 0
3718                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3719            (gnus-thread-total-score-1 thread)))
3720         (t
3721          (gnus-thread-total-score-1 (list thread)))))
3722
3723 (defun gnus-thread-total-score-1 (root)
3724   ;; This function find the total score of the thread below ROOT.
3725   (setq root (car root))
3726   (apply gnus-thread-score-function
3727          (or (append
3728               (mapcar 'gnus-thread-total-score
3729                       (cdr (gnus-id-to-thread (mail-header-id root))))
3730               (when (> (mail-header-number root) 0)
3731                 (list (or (cdr (assq (mail-header-number root)
3732                                      gnus-newsgroup-scored))
3733                           gnus-summary-default-score 0))))
3734              (list gnus-summary-default-score)
3735              '(0))))
3736
3737 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3738 (defvar gnus-tmp-prev-subject nil)
3739 (defvar gnus-tmp-false-parent nil)
3740 (defvar gnus-tmp-root-expunged nil)
3741 (defvar gnus-tmp-dummy-line nil)
3742
3743 (defvar gnus-tmp-header)
3744 (defun gnus-extra-header (type &optional header)
3745   "Return the extra header of TYPE."
3746   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3747       ""))
3748
3749 (defun gnus-summary-prepare-threads (threads)
3750   "Prepare summary buffer from THREADS and indentation LEVEL.
3751 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3752 or a straight list of headers."
3753   (gnus-message 7 "Generating summary...")
3754
3755   (setq gnus-newsgroup-threads threads)
3756   (beginning-of-line)
3757
3758   (let ((gnus-tmp-level 0)
3759         (default-score (or gnus-summary-default-score 0))
3760         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3761         thread number subject stack state gnus-tmp-gathered beg-match
3762         new-roots gnus-tmp-new-adopts thread-end
3763         gnus-tmp-header gnus-tmp-unread
3764         gnus-tmp-replied gnus-tmp-subject-or-nil
3765         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3766         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3767         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3768
3769     (setq gnus-tmp-prev-subject nil)
3770
3771     (if (vectorp (car threads))
3772         ;; If this is a straight (sic) list of headers, then a
3773         ;; threaded summary display isn't required, so we just create
3774         ;; an unthreaded one.
3775         (gnus-summary-prepare-unthreaded threads)
3776
3777       ;; Do the threaded display.
3778
3779       (while (or threads stack gnus-tmp-new-adopts new-roots)
3780
3781         (if (and (= gnus-tmp-level 0)
3782                  (or (not stack)
3783                      (= (caar stack) 0))
3784                  (not gnus-tmp-false-parent)
3785                  (or gnus-tmp-new-adopts new-roots))
3786             (if gnus-tmp-new-adopts
3787                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3788                       thread (list (car gnus-tmp-new-adopts))
3789                       gnus-tmp-header (caar thread)
3790                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3791               (when new-roots
3792                 (setq thread (list (car new-roots))
3793                       gnus-tmp-header (caar thread)
3794                       new-roots (cdr new-roots))))
3795
3796           (if threads
3797               ;; If there are some threads, we do them before the
3798               ;; threads on the stack.
3799               (setq thread threads
3800                     gnus-tmp-header (caar thread))
3801             ;; There were no current threads, so we pop something off
3802             ;; the stack.
3803             (setq state (car stack)
3804                   gnus-tmp-level (car state)
3805                   thread (cdr state)
3806                   stack (cdr stack)
3807                   gnus-tmp-header (caar thread))))
3808
3809         (setq gnus-tmp-false-parent nil)
3810         (setq gnus-tmp-root-expunged nil)
3811         (setq thread-end nil)
3812
3813         (if (stringp gnus-tmp-header)
3814             ;; The header is a dummy root.
3815             (cond
3816              ((eq gnus-summary-make-false-root 'adopt)
3817               ;; We let the first article adopt the rest.
3818               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3819                                                (cddar thread)))
3820               (setq gnus-tmp-gathered
3821                     (nconc (mapcar
3822                             (lambda (h) (mail-header-number (car h)))
3823                             (cddar thread))
3824                            gnus-tmp-gathered))
3825               (setq thread (cons (list (caar thread)
3826                                        (cadar thread))
3827                                  (cdr thread)))
3828               (setq gnus-tmp-level -1
3829                     gnus-tmp-false-parent t))
3830              ((eq gnus-summary-make-false-root 'empty)
3831               ;; We print adopted articles with empty subject fields.
3832               (setq gnus-tmp-gathered
3833                     (nconc (mapcar
3834                             (lambda (h) (mail-header-number (car h)))
3835                             (cddar thread))
3836                            gnus-tmp-gathered))
3837               (setq gnus-tmp-level -1))
3838              ((eq gnus-summary-make-false-root 'dummy)
3839               ;; We remember that we probably want to output a dummy
3840               ;; root.
3841               (setq gnus-tmp-dummy-line gnus-tmp-header)
3842               (setq gnus-tmp-prev-subject gnus-tmp-header))
3843              (t
3844               ;; We do not make a root for the gathered
3845               ;; sub-threads at all.
3846               (setq gnus-tmp-level -1)))
3847
3848           (setq number (mail-header-number gnus-tmp-header)
3849                 subject (mail-header-subject gnus-tmp-header))
3850
3851           (cond
3852            ;; If the thread has changed subject, we might want to make
3853            ;; this subthread into a root.
3854            ((and (null gnus-thread-ignore-subject)
3855                  (not (zerop gnus-tmp-level))
3856                  gnus-tmp-prev-subject
3857                  (not (inline
3858                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3859             (setq new-roots (nconc new-roots (list (car thread)))
3860                   thread-end t
3861                   gnus-tmp-header nil))
3862            ;; If the article lies outside the current limit,
3863            ;; then we do not display it.
3864            ((not (memq number gnus-newsgroup-limit))
3865             (setq gnus-tmp-gathered
3866                   (nconc (mapcar
3867                           (lambda (h) (mail-header-number (car h)))
3868                           (cdar thread))
3869                          gnus-tmp-gathered))
3870             (setq gnus-tmp-new-adopts (if (cdar thread)
3871                                           (append gnus-tmp-new-adopts
3872                                                   (cdar thread))
3873                                         gnus-tmp-new-adopts)
3874                   thread-end t
3875                   gnus-tmp-header nil)
3876             (when (zerop gnus-tmp-level)
3877               (setq gnus-tmp-root-expunged t)))
3878            ;; Perhaps this article is to be marked as read?
3879            ((and gnus-summary-mark-below
3880                  (< (or (cdr (assq number gnus-newsgroup-scored))
3881                         default-score)
3882                     gnus-summary-mark-below)
3883                  ;; Don't touch sparse articles.
3884                  (not (gnus-summary-article-sparse-p number))
3885                  (not (gnus-summary-article-ancient-p number)))
3886             (setq gnus-newsgroup-unreads
3887                   (delq number gnus-newsgroup-unreads))
3888             (if gnus-newsgroup-auto-expire
3889                 (push number gnus-newsgroup-expirable)
3890               (push (cons number gnus-low-score-mark)
3891                     gnus-newsgroup-reads))))
3892
3893           (when gnus-tmp-header
3894             ;; We may have an old dummy line to output before this
3895             ;; article.
3896             (when (and gnus-tmp-dummy-line
3897                        (gnus-subject-equal
3898                         gnus-tmp-dummy-line
3899                         (mail-header-subject gnus-tmp-header)))
3900               (gnus-summary-insert-dummy-line
3901                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3902               (setq gnus-tmp-dummy-line nil))
3903
3904             ;; Compute the mark.
3905             (setq gnus-tmp-unread (gnus-article-mark number))
3906
3907             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3908                                   gnus-tmp-header gnus-tmp-level)
3909                   gnus-newsgroup-data)
3910
3911             ;; Actually insert the line.
3912             (setq
3913              gnus-tmp-subject-or-nil
3914              (cond
3915               ((and gnus-thread-ignore-subject
3916                     gnus-tmp-prev-subject
3917                     (not (inline (gnus-subject-equal
3918                                   gnus-tmp-prev-subject subject))))
3919                subject)
3920               ((zerop gnus-tmp-level)
3921                (if (and (eq gnus-summary-make-false-root 'empty)
3922                         (memq number gnus-tmp-gathered)
3923                         gnus-tmp-prev-subject
3924                         (inline (gnus-subject-equal
3925                                  gnus-tmp-prev-subject subject)))
3926                    gnus-summary-same-subject
3927                  subject))
3928               (t gnus-summary-same-subject)))
3929             (if (and (eq gnus-summary-make-false-root 'adopt)
3930                      (= gnus-tmp-level 1)
3931                      (memq number gnus-tmp-gathered))
3932                 (setq gnus-tmp-opening-bracket ?\<
3933                       gnus-tmp-closing-bracket ?\>)
3934               (setq gnus-tmp-opening-bracket ?\[
3935                     gnus-tmp-closing-bracket ?\]))
3936             (setq
3937              gnus-tmp-indentation
3938              (aref gnus-thread-indent-array gnus-tmp-level)
3939              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3940              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3941                                 gnus-summary-default-score 0)
3942              gnus-tmp-score-char
3943              (if (or (null gnus-summary-default-score)
3944                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3945                          gnus-summary-zcore-fuzz))
3946                  ? ;Whitespace
3947                (if (< gnus-tmp-score gnus-summary-default-score)
3948                    gnus-score-below-mark gnus-score-over-mark))
3949              gnus-tmp-replied
3950              (cond ((memq number gnus-newsgroup-processable)
3951                     gnus-process-mark)
3952                    ((memq number gnus-newsgroup-cached)
3953                     gnus-cached-mark)
3954                    ((memq number gnus-newsgroup-replied)
3955                     gnus-replied-mark)
3956                    ((memq number gnus-newsgroup-saved)
3957                     gnus-saved-mark)
3958                    (t gnus-unread-mark))
3959              gnus-tmp-from (mail-header-from gnus-tmp-header)
3960              gnus-tmp-name
3961              (cond
3962               ((string-match "<[^>]+> *$" gnus-tmp-from)
3963                (setq beg-match (match-beginning 0))
3964                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3965                         (substring gnus-tmp-from (1+ (match-beginning 0))
3966                                    (1- (match-end 0))))
3967                    (substring gnus-tmp-from 0 beg-match)))
3968               ((string-match "(.+)" gnus-tmp-from)
3969                (substring gnus-tmp-from
3970                           (1+ (match-beginning 0)) (1- (match-end 0))))
3971               (t gnus-tmp-from)))
3972             (when (string= gnus-tmp-name "")
3973               (setq gnus-tmp-name gnus-tmp-from))
3974             (unless (numberp gnus-tmp-lines)
3975               (setq gnus-tmp-lines 0))
3976             (gnus-put-text-property
3977              (point)
3978              (progn (eval gnus-summary-line-format-spec) (point))
3979              'gnus-number number)
3980             (when gnus-visual-p
3981               (forward-line -1)
3982               (gnus-run-hooks 'gnus-summary-update-hook)
3983               (forward-line 1))
3984
3985             (setq gnus-tmp-prev-subject subject)))
3986
3987         (when (nth 1 thread)
3988           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3989         (incf gnus-tmp-level)
3990         (setq threads (if thread-end nil (cdar thread)))
3991         (unless threads
3992           (setq gnus-tmp-level 0)))))
3993   (gnus-message 7 "Generating summary...done"))
3994
3995 (defun gnus-summary-prepare-unthreaded (headers)
3996   "Generate an unthreaded summary buffer based on HEADERS."
3997   (let (header number mark)
3998
3999     (beginning-of-line)
4000
4001     (while headers
4002       ;; We may have to root out some bad articles...
4003       (when (memq (setq number (mail-header-number
4004                                 (setq header (pop headers))))
4005                   gnus-newsgroup-limit)
4006         ;; Mark article as read when it has a low score.
4007         (when (and gnus-summary-mark-below
4008                    (< (or (cdr (assq number gnus-newsgroup-scored))
4009                           gnus-summary-default-score 0)
4010                       gnus-summary-mark-below)
4011                    (not (gnus-summary-article-ancient-p number)))
4012           (setq gnus-newsgroup-unreads
4013                 (delq number gnus-newsgroup-unreads))
4014           (if gnus-newsgroup-auto-expire
4015               (push number gnus-newsgroup-expirable)
4016             (push (cons number gnus-low-score-mark)
4017                   gnus-newsgroup-reads)))
4018
4019         (setq mark (gnus-article-mark number))
4020         (push (gnus-data-make number mark (1+ (point)) header 0)
4021               gnus-newsgroup-data)
4022         (gnus-summary-insert-line
4023          header 0 number
4024          mark (memq number gnus-newsgroup-replied)
4025          (memq number gnus-newsgroup-expirable)
4026          (mail-header-subject header) nil
4027          (cdr (assq number gnus-newsgroup-scored))
4028          (memq number gnus-newsgroup-processable))))))
4029
4030 (defun gnus-summary-remove-list-identifiers ()
4031   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4032   (let ((regexp (if (stringp gnus-list-identifiers)
4033                     gnus-list-identifiers
4034                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4035     (dolist (header gnus-newsgroup-headers)
4036       (when (string-match (concat "\\(Re: +\\)?\\(" regexp " *\\)")
4037                           (mail-header-subject header))
4038         (mail-header-set-subject
4039          header (concat (substring (mail-header-subject header)
4040                                    0 (match-beginning 2))
4041                         (substring (mail-header-subject header)
4042                                    (match-end 2))))))))
4043
4044 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4045   "Select newsgroup GROUP.
4046 If READ-ALL is non-nil, all articles in the group are selected.
4047 If SELECT-ARTICLES, only select those articles from GROUP."
4048   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4049          ;;!!! Dirty hack; should be removed.
4050          (gnus-summary-ignore-duplicates
4051           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4052               t
4053             gnus-summary-ignore-duplicates))
4054          (info (nth 2 entry))
4055          articles fetched-articles cached)
4056
4057     (unless (gnus-check-server
4058              (setq gnus-current-select-method
4059                    (gnus-find-method-for-group group)))
4060       (error "Couldn't open server"))
4061
4062     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4063         (gnus-activate-group group)     ; Or we can activate it...
4064         (progn                          ; Or we bug out.
4065           (when (equal major-mode 'gnus-summary-mode)
4066             (kill-buffer (current-buffer)))
4067           (error "Couldn't request group %s: %s"
4068                  group (gnus-status-message group))))
4069
4070     (unless (gnus-request-group group t)
4071       (when (equal major-mode 'gnus-summary-mode)
4072         (kill-buffer (current-buffer)))
4073       (error "Couldn't request group %s: %s"
4074              group (gnus-status-message group)))
4075
4076     (setq gnus-newsgroup-name group)
4077     (setq gnus-newsgroup-unselected nil)
4078     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4079     (gnus-summary-setup-default-charset)
4080
4081     ;; Adjust and set lists of article marks.
4082     (when info
4083       (gnus-adjust-marked-articles info))
4084
4085     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4086     (when (gnus-virtual-group-p group)
4087       (setq cached gnus-newsgroup-cached))
4088
4089     (setq gnus-newsgroup-unreads
4090           (gnus-set-difference
4091            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4092            gnus-newsgroup-dormant))
4093
4094     (setq gnus-newsgroup-processable nil)
4095
4096     (gnus-update-read-articles group gnus-newsgroup-unreads)
4097
4098     (if (setq articles select-articles)
4099         (setq gnus-newsgroup-unselected
4100               (gnus-sorted-intersection
4101                gnus-newsgroup-unreads
4102                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4103       (setq articles (gnus-articles-to-read group read-all)))
4104
4105     (cond
4106      ((null articles)
4107       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4108       'quit)
4109      ((eq articles 0) nil)
4110      (t
4111       ;; Init the dependencies hash table.
4112       (setq gnus-newsgroup-dependencies
4113             (gnus-make-hashtable (length articles)))
4114       (gnus-set-global-variables)
4115       ;; Retrieve the headers and read them in.
4116       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4117       (setq gnus-newsgroup-headers
4118             (if (eq 'nov
4119                     (setq gnus-headers-retrieved-by
4120                           (gnus-retrieve-headers
4121                            articles gnus-newsgroup-name
4122                            ;; We might want to fetch old headers, but
4123                            ;; not if there is only 1 article.
4124                            (and (or (and
4125                                      (not (eq gnus-fetch-old-headers 'some))
4126                                      (not (numberp gnus-fetch-old-headers)))
4127                                     (> (length articles) 1))
4128                                 gnus-fetch-old-headers))))
4129                 (gnus-get-newsgroup-headers-xover
4130                  articles nil nil gnus-newsgroup-name t)
4131               (gnus-get-newsgroup-headers)))
4132       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4133
4134       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4135       (when cached
4136         (setq gnus-newsgroup-cached cached))
4137
4138       ;; Suppress duplicates?
4139       (when gnus-suppress-duplicates
4140         (gnus-dup-suppress-articles))
4141
4142       ;; Set the initial limit.
4143       (setq gnus-newsgroup-limit (copy-sequence articles))
4144       ;; Remove canceled articles from the list of unread articles.
4145       (setq gnus-newsgroup-unreads
4146             (gnus-set-sorted-intersection
4147              gnus-newsgroup-unreads
4148              (setq fetched-articles
4149                    (mapcar (lambda (headers) (mail-header-number headers))
4150                            gnus-newsgroup-headers))))
4151       ;; Removed marked articles that do not exist.
4152       (gnus-update-missing-marks
4153        (gnus-sorted-complement fetched-articles articles))
4154       ;; We might want to build some more threads first.
4155       (when (and gnus-fetch-old-headers
4156                  (eq gnus-headers-retrieved-by 'nov))
4157         (if (eq gnus-fetch-old-headers 'invisible)
4158             (gnus-build-all-threads)
4159           (gnus-build-old-threads)))
4160       ;; Let the Gnus agent mark articles as read.
4161       (when gnus-agent
4162         (gnus-agent-get-undownloaded-list))
4163       ;; Remove list identifiers from subject
4164       (when gnus-list-identifiers
4165         (gnus-summary-remove-list-identifiers))
4166       ;; Check whether auto-expire is to be done in this group.
4167       (setq gnus-newsgroup-auto-expire
4168             (gnus-group-auto-expirable-p group))
4169       ;; Set up the article buffer now, if necessary.
4170       (unless gnus-single-article-buffer
4171         (gnus-article-setup-buffer))
4172       ;; First and last article in this newsgroup.
4173       (when gnus-newsgroup-headers
4174         (setq gnus-newsgroup-begin
4175               (mail-header-number (car gnus-newsgroup-headers))
4176               gnus-newsgroup-end
4177               (mail-header-number
4178                (gnus-last-element gnus-newsgroup-headers))))
4179       ;; GROUP is successfully selected.
4180       (or gnus-newsgroup-headers t)))))
4181
4182 (defun gnus-articles-to-read (group &optional read-all)
4183   ;; Find out what articles the user wants to read.
4184   (let* ((articles
4185           ;; Select all articles if `read-all' is non-nil, or if there
4186           ;; are no unread articles.
4187           (if (or read-all
4188                   (and (zerop (length gnus-newsgroup-marked))
4189                        (zerop (length gnus-newsgroup-unreads)))
4190                   (eq (gnus-group-find-parameter group 'display)
4191                       'all))
4192               (or
4193                (gnus-uncompress-range (gnus-active group))
4194                (gnus-cache-articles-in-group group))
4195             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4196                           (copy-sequence gnus-newsgroup-unreads))
4197                   '<)))
4198          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4199          (scored (length scored-list))
4200          (number (length articles))
4201          (marked (+ (length gnus-newsgroup-marked)
4202                     (length gnus-newsgroup-dormant)))
4203          (select
4204           (cond
4205            ((numberp read-all)
4206             read-all)
4207            (t
4208             (condition-case ()
4209                 (cond
4210                  ((and (or (<= scored marked) (= scored number))
4211                        (numberp gnus-large-newsgroup)
4212                        (> number gnus-large-newsgroup))
4213                   (let ((input
4214                          (read-string
4215                           (format
4216                            "How many articles from %s (default %d): "
4217                            (gnus-limit-string gnus-newsgroup-name 35)
4218                            number))))
4219                     (if (string-match "^[ \t]*$" input) number input)))
4220                  ((and (> scored marked) (< scored number)
4221                        (> (- scored number) 20))
4222                   (let ((input
4223                          (read-string
4224                           (format "%s %s (%d scored, %d total): "
4225                                   "How many articles from"
4226                                   group scored number))))
4227                     (if (string-match "^[ \t]*$" input)
4228                         number input)))
4229                  (t number))
4230               (quit nil))))))
4231     (setq select (if (stringp select) (string-to-number select) select))
4232     (if (or (null select) (zerop select))
4233         select
4234       (if (and (not (zerop scored)) (<= (abs select) scored))
4235           (progn
4236             (setq articles (sort scored-list '<))
4237             (setq number (length articles)))
4238         (setq articles (copy-sequence articles)))
4239
4240       (when (< (abs select) number)
4241         (if (< select 0)
4242             ;; Select the N oldest articles.
4243             (setcdr (nthcdr (1- (abs select)) articles) nil)
4244           ;; Select the N most recent articles.
4245           (setq articles (nthcdr (- number select) articles))))
4246       (setq gnus-newsgroup-unselected
4247             (gnus-sorted-intersection
4248              gnus-newsgroup-unreads
4249              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4250       articles)))
4251
4252 (defun gnus-killed-articles (killed articles)
4253   (let (out)
4254     (while articles
4255       (when (inline (gnus-member-of-range (car articles) killed))
4256         (push (car articles) out))
4257       (setq articles (cdr articles)))
4258     out))
4259
4260 (defun gnus-uncompress-marks (marks)
4261   "Uncompress the mark ranges in MARKS."
4262   (let ((uncompressed '(score bookmark))
4263         out)
4264     (while marks
4265       (if (memq (caar marks) uncompressed)
4266           (push (car marks) out)
4267         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4268       (setq marks (cdr marks)))
4269     out))
4270
4271 (defun gnus-adjust-marked-articles (info)
4272   "Set all article lists and remove all marks that are no longer valid."
4273   (let* ((marked-lists (gnus-info-marks info))
4274          (active (gnus-active (gnus-info-group info)))
4275          (min (car active))
4276          (max (cdr active))
4277          (types gnus-article-mark-lists)
4278          (uncompressed '(score bookmark killed))
4279          marks var articles article mark)
4280
4281     (while marked-lists
4282       (setq marks (pop marked-lists))
4283       (set (setq var (intern (format "gnus-newsgroup-%s"
4284                                      (car (rassq (setq mark (car marks))
4285                                                  types)))))
4286            (if (memq (car marks) uncompressed) (cdr marks)
4287              (gnus-uncompress-range (cdr marks))))
4288
4289       (setq articles (symbol-value var))
4290
4291       ;; All articles have to be subsets of the active articles.
4292       (cond
4293        ;; Adjust "simple" lists.
4294        ((memq mark '(tick dormant expire reply save))
4295         (while articles
4296           (when (or (< (setq article (pop articles)) min) (> article max))
4297             (set var (delq article (symbol-value var))))))
4298        ;; Adjust assocs.
4299        ((memq mark uncompressed)
4300         (when (not (listp (cdr (symbol-value var))))
4301           (set var (list (symbol-value var))))
4302         (when (not (listp (cdr articles)))
4303           (setq articles (list articles)))
4304         (while articles
4305           (when (or (not (consp (setq article (pop articles))))
4306                     (< (car article) min)
4307                     (> (car article) max))
4308             (set var (delq article (symbol-value var))))))))))
4309
4310 (defun gnus-update-missing-marks (missing)
4311   "Go through the list of MISSING articles and remove them from the mark lists."
4312   (when missing
4313     (let ((types gnus-article-mark-lists)
4314           var m)
4315       ;; Go through all types.
4316       (while types
4317         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4318         (when (symbol-value var)
4319           ;; This list has articles.  So we delete all missing articles
4320           ;; from it.
4321           (setq m missing)
4322           (while m
4323             (set var (delq (pop m) (symbol-value var)))))))))
4324
4325 (defun gnus-update-marks ()
4326   "Enter the various lists of marked articles into the newsgroup info list."
4327   (let ((types gnus-article-mark-lists)
4328         (info (gnus-get-info gnus-newsgroup-name))
4329         (uncompressed '(score bookmark killed))
4330         type list newmarked symbol delta-marks)
4331     (when info
4332       ;; Add all marks lists to the list of marks lists.
4333       (while (setq type (pop types))
4334         (setq list (symbol-value
4335                           (setq symbol
4336                                 (intern (format "gnus-newsgroup-%s"
4337                                                 (car type))))))
4338
4339         (when list
4340           ;; Get rid of the entries of the articles that have the
4341           ;; default score.
4342           (when (and (eq (cdr type) 'score)
4343                      gnus-save-score
4344                      list)
4345             (let* ((arts list)
4346                    (prev (cons nil list))
4347                    (all prev))
4348               (while arts
4349                 (if (or (not (consp (car arts)))
4350                         (= (cdar arts) gnus-summary-default-score))
4351                     (setcdr prev (cdr arts))
4352                   (setq prev arts))
4353                 (setq arts (cdr arts)))
4354               (setq list (cdr all)))))
4355
4356        (or (memq (cdr type) uncompressed)
4357            (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4358        
4359        (when (gnus-check-backend-function 'request-set-mark
4360                                           gnus-newsgroup-name)
4361          ;; uncompressed:s are not proper flags (they are cons cells)
4362          ;; cache is a internal gnus flag
4363          (unless (memq (cdr type) (cons 'cache uncompressed))
4364            (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4365                   (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4366                   (add (gnus-remove-from-range (gnus-copy-sequence list) old)))
4367              (if add
4368                  (push (list add 'add (list (cdr type))) delta-marks))
4369              (if del
4370                  (push (list del 'del (list (cdr type))) delta-marks)))))
4371           
4372         (when list
4373          (push (cons (cdr type) list) newmarked)))
4374
4375       (when delta-marks
4376         (unless (gnus-check-group gnus-newsgroup-name)
4377           (error "Can't open server for %s" gnus-newsgroup-name))
4378         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4379           
4380       ;; Enter these new marks into the info of the group.
4381       (if (nthcdr 3 info)
4382           (setcar (nthcdr 3 info) newmarked)
4383         ;; Add the marks lists to the end of the info.
4384         (when newmarked
4385           (setcdr (nthcdr 2 info) (list newmarked))))
4386
4387       ;; Cut off the end of the info if there's nothing else there.
4388       (let ((i 5))
4389         (while (and (> i 2)
4390                     (not (nth i info)))
4391           (when (nthcdr (decf i) info)
4392             (setcdr (nthcdr i info) nil)))))))
4393
4394 (defun gnus-set-mode-line (where)
4395   "This function sets the mode line of the article or summary buffers.
4396 If WHERE is `summary', the summary mode line format will be used."
4397   ;; Is this mode line one we keep updated?
4398   (when (and (memq where gnus-updated-mode-lines)
4399              (symbol-value
4400               (intern (format "gnus-%s-mode-line-format-spec" where))))
4401     (let (mode-string)
4402       (save-excursion
4403         ;; We evaluate this in the summary buffer since these
4404         ;; variables are buffer-local to that buffer.
4405         (set-buffer gnus-summary-buffer)
4406         ;; We bind all these variables that are used in the `eval' form
4407         ;; below.
4408         (let* ((mformat (symbol-value
4409                          (intern
4410                           (format "gnus-%s-mode-line-format-spec" where))))
4411                (gnus-tmp-group-name gnus-newsgroup-name)
4412                (gnus-tmp-article-number (or gnus-current-article 0))
4413                (gnus-tmp-unread gnus-newsgroup-unreads)
4414                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4415                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4416                (gnus-tmp-unread-and-unselected
4417                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4418                             (zerop gnus-tmp-unselected))
4419                        "")
4420                       ((zerop gnus-tmp-unselected)
4421                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4422                       (t (format "{%d(+%d) more}"
4423                                  gnus-tmp-unread-and-unticked
4424                                  gnus-tmp-unselected))))
4425                (gnus-tmp-subject
4426                 (if (and gnus-current-headers
4427                          (vectorp gnus-current-headers))
4428                     (gnus-mode-string-quote
4429                      (mail-header-subject gnus-current-headers))
4430                   ""))
4431                bufname-length max-len
4432                gnus-tmp-header);; passed as argument to any user-format-funcs
4433           (setq mode-string (eval mformat))
4434           (setq bufname-length (if (string-match "%b" mode-string)
4435                                    (- (length
4436                                        (buffer-name
4437                                         (if (eq where 'summary)
4438                                             nil
4439                                           (get-buffer gnus-article-buffer))))
4440                                       2)
4441                                  0))
4442           (setq max-len (max 4 (if gnus-mode-non-string-length
4443                                    (- (window-width)
4444                                       gnus-mode-non-string-length
4445                                       bufname-length)
4446                                  (length mode-string))))
4447           ;; We might have to chop a bit of the string off...
4448           (when (> (length mode-string) max-len)
4449             (setq mode-string
4450                   (concat (truncate-string-to-width mode-string (- max-len 3))
4451                           "...")))
4452           ;; Pad the mode string a bit.
4453           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4454       ;; Update the mode line.
4455       (setq mode-line-buffer-identification
4456             (gnus-mode-line-buffer-identification (list mode-string)))
4457       (set-buffer-modified-p t))))
4458
4459 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4460   "Go through the HEADERS list and add all Xrefs to a hash table.
4461 The resulting hash table is returned, or nil if no Xrefs were found."
4462   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4463          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4464          (xref-hashtb (gnus-make-hashtable))
4465          start group entry number xrefs header)
4466     (while headers
4467       (setq header (pop headers))
4468       (when (and (setq xrefs (mail-header-xref header))
4469                  (not (memq (setq number (mail-header-number header))
4470                             unreads)))
4471         (setq start 0)
4472         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4473           (setq start (match-end 0))
4474           (setq group (if prefix
4475                           (concat prefix (substring xrefs (match-beginning 1)
4476                                                     (match-end 1)))
4477                         (substring xrefs (match-beginning 1) (match-end 1))))
4478           (setq number
4479                 (string-to-int (substring xrefs (match-beginning 2)
4480                                           (match-end 2))))
4481           (if (setq entry (gnus-gethash group xref-hashtb))
4482               (setcdr entry (cons number (cdr entry)))
4483             (gnus-sethash group (cons number nil) xref-hashtb)))))
4484     (and start xref-hashtb)))
4485
4486 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4487   "Look through all the headers and mark the Xrefs as read."
4488   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4489         name entry info xref-hashtb idlist method nth4)
4490     (save-excursion
4491       (set-buffer gnus-group-buffer)
4492       (when (setq xref-hashtb
4493                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4494         (mapatoms
4495          (lambda (group)
4496            (unless (string= from-newsgroup (setq name (symbol-name group)))
4497              (setq idlist (symbol-value group))
4498              ;; Dead groups are not updated.
4499              (and (prog1
4500                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4501                             info (nth 2 entry))
4502                     (when (stringp (setq nth4 (gnus-info-method info)))
4503                       (setq nth4 (gnus-server-to-method nth4))))
4504                   ;; Only do the xrefs if the group has the same
4505                   ;; select method as the group we have just read.
4506                   (or (gnus-methods-equal-p
4507                        nth4 (gnus-find-method-for-group from-newsgroup))
4508                       virtual
4509                       (equal nth4 (setq method (gnus-find-method-for-group
4510                                                 from-newsgroup)))
4511                       (and (equal (car nth4) (car method))
4512                            (equal (nth 1 nth4) (nth 1 method))))
4513                   gnus-use-cross-reference
4514                   (or (not (eq gnus-use-cross-reference t))
4515                       virtual
4516                       ;; Only do cross-references on subscribed
4517                       ;; groups, if that is what is wanted.
4518                       (<= (gnus-info-level info) gnus-level-subscribed))
4519                   (gnus-group-make-articles-read name idlist))))
4520          xref-hashtb)))))
4521
4522 (defun gnus-compute-read-articles (group articles)
4523   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4524          (info (nth 2 entry))
4525          (active (gnus-active group))
4526          ninfo)
4527     (when entry
4528       ;; First peel off all invalid article numbers.
4529       (when active
4530         (let ((ids articles)
4531               id first)
4532           (while (setq id (pop ids))
4533             (when (and first (> id (cdr active)))
4534               ;; We'll end up in this situation in one particular
4535               ;; obscure situation.  If you re-scan a group and get
4536               ;; a new article that is cross-posted to a different
4537               ;; group that has not been re-scanned, you might get
4538               ;; crossposted article that has a higher number than
4539               ;; Gnus believes possible.  So we re-activate this
4540               ;; group as well.  This might mean doing the
4541               ;; crossposting thingy will *increase* the number
4542               ;; of articles in some groups.  Tsk, tsk.
4543               (setq active (or (gnus-activate-group group) active)))
4544             (when (or (> id (cdr active))
4545                       (< id (car active)))
4546               (setq articles (delq id articles))))))
4547       ;; If the read list is nil, we init it.
4548       (if (and active
4549                (null (gnus-info-read info))
4550                (> (car active) 1))
4551           (setq ninfo (cons 1 (1- (car active))))
4552         (setq ninfo (gnus-info-read info)))
4553       ;; Then we add the read articles to the range.
4554       (gnus-add-to-range
4555        ninfo (setq articles (sort articles '<))))))
4556
4557 (defun gnus-group-make-articles-read (group articles)
4558   "Update the info of GROUP to say that ARTICLES are read."
4559   (let* ((num 0)
4560          (entry (gnus-gethash group gnus-newsrc-hashtb))
4561          (info (nth 2 entry))
4562          (active (gnus-active group))
4563          range)
4564     (when entry
4565       (setq range (gnus-compute-read-articles group articles))
4566       (save-excursion
4567         (set-buffer gnus-group-buffer)
4568         (gnus-undo-register
4569           `(progn
4570              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4571              (gnus-info-set-read ',info ',(gnus-info-read info))
4572              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4573              (gnus-group-update-group ,group t))))
4574       ;; Add the read articles to the range.
4575       (gnus-info-set-read info range)
4576       ;; Then we have to re-compute how many unread
4577       ;; articles there are in this group.
4578       (when active
4579         (cond
4580          ((not range)
4581           (setq num (- (1+ (cdr active)) (car active))))
4582          ((not (listp (cdr range)))
4583           (setq num (- (cdr active) (- (1+ (cdr range))
4584                                        (car range)))))
4585          (t
4586           (while range
4587             (if (numberp (car range))
4588                 (setq num (1+ num))
4589               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4590             (setq range (cdr range)))
4591           (setq num (- (cdr active) num))))
4592         ;; Update the number of unread articles.
4593         (setcar entry num)
4594         ;; Update the group buffer.
4595         (gnus-group-update-group group t)))))
4596
4597 (defvar gnus-newsgroup-none-id 0)
4598
4599 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4600   (let ((cur nntp-server-buffer)
4601         (dependencies
4602          (or dependencies
4603              (save-excursion (set-buffer gnus-summary-buffer)
4604                              gnus-newsgroup-dependencies)))
4605         headers id end ref
4606         (mail-parse-charset gnus-newsgroup-charset)
4607         (mail-parse-ignored-charsets 
4608              (save-excursion (condition-case nil
4609                                  (set-buffer gnus-summary-buffer)
4610                                (error))
4611                              gnus-newsgroup-ignored-charsets)))
4612     (save-excursion
4613       (set-buffer nntp-server-buffer)
4614       ;; Translate all TAB characters into SPACE characters.
4615       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4616       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4617       (gnus-run-hooks 'gnus-parse-headers-hook)
4618       (let ((case-fold-search t)
4619             in-reply-to header p lines chars)
4620         (goto-char (point-min))
4621         ;; Search to the beginning of the next header.  Error messages
4622         ;; do not begin with 2 or 3.
4623         (while (re-search-forward "^[23][0-9]+ " nil t)
4624           (setq id nil
4625                 ref nil)
4626           ;; This implementation of this function, with nine
4627           ;; search-forwards instead of the one re-search-forward and
4628           ;; a case (which basically was the old function) is actually
4629           ;; about twice as fast, even though it looks messier.  You
4630           ;; can't have everything, I guess.  Speed and elegance
4631           ;; doesn't always go hand in hand.
4632           (setq
4633            header
4634            (vector
4635             ;; Number.
4636             (prog1
4637                 (read cur)
4638               (end-of-line)
4639               (setq p (point))
4640               (narrow-to-region (point)
4641                                 (or (and (search-forward "\n.\n" nil t)
4642                                          (- (point) 2))
4643                                     (point))))
4644             ;; Subject.
4645             (progn
4646               (goto-char p)
4647               (if (search-forward "\nsubject: " nil t)
4648                   (funcall gnus-decode-encoded-word-function
4649                            (nnheader-header-value))
4650                 "(none)"))
4651             ;; From.
4652             (progn
4653               (goto-char p)
4654               (if (search-forward "\nfrom: " nil t)
4655                   (funcall gnus-decode-encoded-word-function
4656                            (nnheader-header-value))
4657                 "(nobody)"))
4658             ;; Date.
4659             (progn
4660               (goto-char p)
4661               (if (search-forward "\ndate: " nil t)
4662                   (nnheader-header-value) ""))
4663             ;; Message-ID.
4664             (progn
4665               (goto-char p)
4666               (setq id (if (re-search-forward
4667                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4668                            ;; We do it this way to make sure the Message-ID
4669                            ;; is (somewhat) syntactically valid.
4670                            (buffer-substring (match-beginning 1)
4671                                              (match-end 1))
4672                          ;; If there was no message-id, we just fake one
4673                          ;; to make subsequent routines simpler.
4674                          (nnheader-generate-fake-message-id))))
4675             ;; References.
4676             (progn
4677               (goto-char p)
4678               (if (search-forward "\nreferences: " nil t)
4679                   (progn
4680                     (setq end (point))
4681                     (prog1
4682                         (nnheader-header-value)
4683                       (setq ref
4684                             (buffer-substring
4685                              (progn
4686                                (end-of-line)
4687                                (search-backward ">" end t)
4688                                (1+ (point)))
4689                              (progn
4690                                (search-backward "<" end t)
4691                                (point))))))
4692                 ;; Get the references from the in-reply-to header if there
4693                 ;; were no references and the in-reply-to header looks
4694                 ;; promising.
4695                 (if (and (search-forward "\nin-reply-to: " nil t)
4696                          (setq in-reply-to (nnheader-header-value))
4697                          (string-match "<[^>]+>" in-reply-to))
4698                     (let (ref2)
4699                       (setq ref (substring in-reply-to (match-beginning 0)
4700                                            (match-end 0)))
4701                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4702                         (setq ref2 (substring in-reply-to (match-beginning 0)
4703                                               (match-end 0)))
4704                         (when (> (length ref2) (length ref))
4705                           (setq ref ref2)))
4706                       ref)
4707                   (setq ref nil))))
4708             ;; Chars.
4709             (progn
4710               (goto-char p)
4711               (if (search-forward "\nchars: " nil t)
4712                   (if (numberp (setq chars (ignore-errors (read cur))))
4713                       chars 0)
4714                 0))
4715             ;; Lines.
4716             (progn
4717               (goto-char p)
4718               (if (search-forward "\nlines: " nil t)
4719                   (if (numberp (setq lines (ignore-errors (read cur))))
4720                       lines 0)
4721                 0))
4722             ;; Xref.
4723             (progn
4724               (goto-char p)
4725               (and (search-forward "\nxref: " nil t)
4726                    (nnheader-header-value)))
4727             ;; Extra.
4728             (when gnus-extra-headers
4729               (let ((extra gnus-extra-headers)
4730                     out)
4731                 (while extra
4732                   (goto-char p)
4733                   (when (search-forward
4734                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4735                     (push (cons (car extra) (nnheader-header-value))
4736                           out))
4737                   (pop extra))
4738                 out))))
4739           (when (equal id ref)
4740             (setq ref nil))
4741
4742           (when gnus-alter-header-function
4743             (funcall gnus-alter-header-function header)
4744             (setq id (mail-header-id header)
4745                   ref (gnus-parent-id (mail-header-references header))))
4746
4747           (when (setq header
4748                       (gnus-dependencies-add-header
4749                        header dependencies force-new))
4750             (push header headers))
4751           (goto-char (point-max))
4752           (widen))
4753         (nreverse headers)))))
4754
4755 ;; Goes through the xover lines and returns a list of vectors
4756 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4757                                                   force-new dependencies
4758                                                   group also-fetch-heads)
4759   "Parse the news overview data in the server buffer, and return a
4760 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4761   ;; Get the Xref when the users reads the articles since most/some
4762   ;; NNTP servers do not include Xrefs when using XOVER.
4763   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4764   (let ((mail-parse-charset gnus-newsgroup-charset)
4765         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4766         (cur nntp-server-buffer)
4767         (dependencies (or dependencies gnus-newsgroup-dependencies))
4768         number headers header)
4769     (save-excursion
4770       (set-buffer nntp-server-buffer)
4771       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4772       ;; Allow the user to mangle the headers before parsing them.
4773       (gnus-run-hooks 'gnus-parse-headers-hook)
4774       (goto-char (point-min))
4775       (while (not (eobp))
4776         (condition-case ()
4777             (while (and sequence (not (eobp)))
4778               (setq number (read cur))
4779               (while (and sequence
4780                           (< (car sequence) number))
4781                 (setq sequence (cdr sequence)))
4782               (and sequence
4783                    (eq number (car sequence))
4784                    (progn
4785                      (setq sequence (cdr sequence))
4786                      (setq header (inline
4787                                     (gnus-nov-parse-line
4788                                      number dependencies force-new))))
4789                    (push header headers))
4790               (forward-line 1))
4791           (error
4792            (gnus-error 4 "Strange nov line (%d)"
4793                        (count-lines (point-min) (point)))))
4794         (forward-line 1))
4795       ;; A common bug in inn is that if you have posted an article and
4796       ;; then retrieves the active file, it will answer correctly --
4797       ;; the new article is included.  However, a NOV entry for the
4798       ;; article may not have been generated yet, so this may fail.
4799       ;; We work around this problem by retrieving the last few
4800       ;; headers using HEAD.
4801       (if (or (not also-fetch-heads)
4802               (not sequence))
4803           ;; We (probably) got all the headers.
4804           (nreverse headers)
4805         (let ((gnus-nov-is-evil t))
4806           (nconc
4807            (nreverse headers)
4808            (when (gnus-retrieve-headers sequence group)
4809              (gnus-get-newsgroup-headers))))))))
4810
4811 (defun gnus-article-get-xrefs ()
4812   "Fill in the Xref value in `gnus-current-headers', if necessary.
4813 This is meant to be called in `gnus-article-internal-prepare-hook'."
4814   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4815                                  gnus-current-headers)))
4816     (or (not gnus-use-cross-reference)
4817         (not headers)
4818         (and (mail-header-xref headers)
4819              (not (string= (mail-header-xref headers) "")))
4820         (let ((case-fold-search t)
4821               xref)
4822           (save-restriction
4823             (nnheader-narrow-to-headers)
4824             (goto-char (point-min))
4825             (when (or (and (not (eobp))
4826                            (eq (downcase (char-after)) ?x)
4827                            (looking-at "Xref:"))
4828                       (search-forward "\nXref:" nil t))
4829               (goto-char (1+ (match-end 0)))
4830               (setq xref (buffer-substring (point)
4831                                            (progn (end-of-line) (point))))
4832               (mail-header-set-xref headers xref)))))))
4833
4834 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4835   "Find article ID and insert the summary line for that article.
4836 OLD-HEADER can either be a header or a line number to insert
4837 the subject line on."
4838   (let* ((line (and (numberp old-header) old-header))
4839          (old-header (and (vectorp old-header) old-header))
4840          (header (cond ((and old-header use-old-header)
4841                         old-header)
4842                        ((and (numberp id)
4843                              (gnus-number-to-header id))
4844                         (gnus-number-to-header id))
4845                        (t
4846                         (gnus-read-header id))))
4847          (number (and (numberp id) id))
4848          d)
4849     (when header
4850       ;; Rebuild the thread that this article is part of and go to the
4851       ;; article we have fetched.
4852       (when (and (not gnus-show-threads)
4853                  old-header)
4854         (when (and number
4855                    (setq d (gnus-data-find (mail-header-number old-header))))
4856           (goto-char (gnus-data-pos d))
4857           (gnus-data-remove
4858            number
4859            (- (gnus-point-at-bol)
4860               (prog1
4861                   (1+ (gnus-point-at-eol))
4862                 (gnus-delete-line))))))
4863       (when old-header
4864         (mail-header-set-number header (mail-header-number old-header)))
4865       (setq gnus-newsgroup-sparse
4866             (delq (setq number (mail-header-number header))
4867                   gnus-newsgroup-sparse))
4868       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4869       (push number gnus-newsgroup-limit)
4870       (gnus-rebuild-thread (mail-header-id header) line)
4871       (gnus-summary-goto-subject number nil t))
4872     (when (and (numberp number)
4873                (> number 0))
4874       ;; We have to update the boundaries even if we can't fetch the
4875       ;; article if ID is a number -- so that the next `P' or `N'
4876       ;; command will fetch the previous (or next) article even
4877       ;; if the one we tried to fetch this time has been canceled.
4878       (when (> number gnus-newsgroup-end)
4879         (setq gnus-newsgroup-end number))
4880       (when (< number gnus-newsgroup-begin)
4881         (setq gnus-newsgroup-begin number))
4882       (setq gnus-newsgroup-unselected
4883             (delq number gnus-newsgroup-unselected)))
4884     ;; Report back a success?
4885     (and header (mail-header-number header))))
4886
4887 ;;; Process/prefix in the summary buffer
4888
4889 (defun gnus-summary-work-articles (n)
4890   "Return a list of articles to be worked upon.
4891 The prefix argument, the list of process marked articles, and the
4892 current article will be taken into consideration."
4893   (save-excursion
4894     (set-buffer gnus-summary-buffer)
4895     (cond
4896      (n
4897       ;; A numerical prefix has been given.
4898       (setq n (prefix-numeric-value n))
4899       (let ((backward (< n 0))
4900             (n (abs (prefix-numeric-value n)))
4901             articles article)
4902         (save-excursion
4903           (while
4904               (and (> n 0)
4905                    (push (setq article (gnus-summary-article-number))
4906                          articles)
4907                    (if backward
4908                        (gnus-summary-find-prev nil article)
4909                      (gnus-summary-find-next nil article)))
4910             (decf n)))
4911         (nreverse articles)))
4912      ((and (gnus-region-active-p) (mark))
4913       (message "region active")
4914       ;; Work on the region between point and mark.
4915       (let ((max (max (point) (mark)))
4916             articles article)
4917         (save-excursion
4918           (goto-char (min (min (point) (mark))))
4919           (while
4920               (and
4921                (push (setq article (gnus-summary-article-number)) articles)
4922                (gnus-summary-find-next nil article)
4923                (< (point) max)))
4924           (nreverse articles))))
4925      (gnus-newsgroup-processable
4926       ;; There are process-marked articles present.
4927       ;; Save current state.
4928       (gnus-summary-save-process-mark)
4929       ;; Return the list.
4930       (reverse gnus-newsgroup-processable))
4931      (t
4932       ;; Just return the current article.
4933       (list (gnus-summary-article-number))))))
4934
4935 (defmacro gnus-summary-iterate (arg &rest forms)
4936   "Iterate over the process/prefixed articles and do FORMS.
4937 ARG is the interactive prefix given to the command.  FORMS will be
4938 executed with point over the summary line of the articles."
4939   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4940     `(let ((,articles (gnus-summary-work-articles ,arg)))
4941        (while ,articles
4942          (gnus-summary-goto-subject (car ,articles))
4943          ,@forms
4944          (pop ,articles)))))
4945
4946 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4947 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4948
4949 (defun gnus-summary-save-process-mark ()
4950   "Push the current set of process marked articles on the stack."
4951   (interactive)
4952   (push (copy-sequence gnus-newsgroup-processable)
4953         gnus-newsgroup-process-stack))
4954
4955 (defun gnus-summary-kill-process-mark ()
4956   "Push the current set of process marked articles on the stack and unmark."
4957   (interactive)
4958   (gnus-summary-save-process-mark)
4959   (gnus-summary-unmark-all-processable))
4960
4961 (defun gnus-summary-yank-process-mark ()
4962   "Pop the last process mark state off the stack and restore it."
4963   (interactive)
4964   (unless gnus-newsgroup-process-stack
4965     (error "Empty mark stack"))
4966   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4967
4968 (defun gnus-summary-process-mark-set (set)
4969   "Make SET into the current process marked articles."
4970   (gnus-summary-unmark-all-processable)
4971   (while set
4972     (gnus-summary-set-process-mark (pop set))))
4973
4974 ;;; Searching and stuff
4975
4976 (defun gnus-summary-search-group (&optional backward use-level)
4977   "Search for next unread newsgroup.
4978 If optional argument BACKWARD is non-nil, search backward instead."
4979   (save-excursion
4980     (set-buffer gnus-group-buffer)
4981     (when (gnus-group-search-forward
4982            backward nil (if use-level (gnus-group-group-level) nil))
4983       (gnus-group-group-name))))
4984
4985 (defun gnus-summary-best-group (&optional exclude-group)
4986   "Find the name of the best unread group.
4987 If EXCLUDE-GROUP, do not go to this group."
4988   (save-excursion
4989     (set-buffer gnus-group-buffer)
4990     (save-excursion
4991       (gnus-group-best-unread-group exclude-group))))
4992
4993 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
4994   (if backward (gnus-summary-find-prev)
4995     (let* ((dummy (gnus-summary-article-intangible-p))
4996            (article (or article (gnus-summary-article-number)))
4997            (arts (gnus-data-find-list article))
4998            result)
4999       (when (and (not dummy)
5000                  (or (not gnus-summary-check-current)
5001                      (not unread)
5002                      (not (gnus-data-unread-p (car arts)))))
5003         (setq arts (cdr arts)))
5004       (when (setq result
5005                   (if unread
5006                       (progn
5007                         (while arts
5008                           (when (or (and undownloaded
5009                                          (eq gnus-undownloaded-mark
5010                                              (gnus-data-mark (car arts))))
5011                                     (gnus-data-unread-p (car arts)))
5012                             (setq result (car arts)
5013                                   arts nil))
5014                           (setq arts (cdr arts)))
5015                         result)
5016                     (car arts)))
5017         (goto-char (gnus-data-pos result))
5018         (gnus-data-number result)))))
5019
5020 (defun gnus-summary-find-prev (&optional unread article)
5021   (let* ((eobp (eobp))
5022          (article (or article (gnus-summary-article-number)))
5023          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5024          result)
5025     (when (and (not eobp)
5026                (or (not gnus-summary-check-current)
5027                    (not unread)
5028                    (not (gnus-data-unread-p (car arts)))))
5029       (setq arts (cdr arts)))
5030     (when (setq result
5031                 (if unread
5032                     (progn
5033                       (while arts
5034                         (when (gnus-data-unread-p (car arts))
5035                           (setq result (car arts)
5036                                 arts nil))
5037                         (setq arts (cdr arts)))
5038                       result)
5039                   (car arts)))
5040       (goto-char (gnus-data-pos result))
5041       (gnus-data-number result))))
5042
5043 (defun gnus-summary-find-subject (subject &optional unread backward article)
5044   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5045          (article (or article (gnus-summary-article-number)))
5046          (articles (gnus-data-list backward))
5047          (arts (gnus-data-find-list article articles))
5048          result)
5049     (when (or (not gnus-summary-check-current)
5050               (not unread)
5051               (not (gnus-data-unread-p (car arts))))
5052       (setq arts (cdr arts)))
5053     (while arts
5054       (and (or (not unread)
5055                (gnus-data-unread-p (car arts)))
5056            (vectorp (gnus-data-header (car arts)))
5057            (gnus-subject-equal
5058             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5059            (setq result (car arts)
5060                  arts nil))
5061       (setq arts (cdr arts)))
5062     (and result
5063          (goto-char (gnus-data-pos result))
5064          (gnus-data-number result))))
5065
5066 (defun gnus-summary-search-forward (&optional unread subject backward)
5067   "Search forward for an article.
5068 If UNREAD, look for unread articles.  If SUBJECT, look for
5069 articles with that subject.  If BACKWARD, search backward instead."
5070   (cond (subject (gnus-summary-find-subject subject unread backward))
5071         (backward (gnus-summary-find-prev unread))
5072         (t (gnus-summary-find-next unread))))
5073
5074 (defun gnus-recenter (&optional n)
5075   "Center point in window and redisplay frame.
5076 Also do horizontal recentering."
5077   (interactive "P")
5078   (when (and gnus-auto-center-summary
5079              (not (eq gnus-auto-center-summary 'vertical)))
5080     (gnus-horizontal-recenter))
5081   (recenter n))
5082
5083 (defun gnus-summary-recenter ()
5084   "Center point in the summary window.
5085 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5086 displayed, no centering will be performed."
5087   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5088   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5089   (let* ((top (cond ((< (window-height) 4) 0)
5090                     ((< (window-height) 7) 1)
5091                     (t (if (numberp gnus-auto-center-summary)
5092                            gnus-auto-center-summary
5093                          2))))
5094          (height (1- (window-height)))
5095          (bottom (save-excursion (goto-char (point-max))
5096                                  (forward-line (- height))
5097                                  (point)))
5098          (window (get-buffer-window (current-buffer))))
5099     ;; The user has to want it.
5100     (when gnus-auto-center-summary
5101       (when (get-buffer-window gnus-article-buffer)
5102         ;; Only do recentering when the article buffer is displayed,
5103         ;; Set the window start to either `bottom', which is the biggest
5104         ;; possible valid number, or the second line from the top,
5105         ;; whichever is the least.
5106         (set-window-start
5107          window (min bottom (save-excursion
5108                               (forward-line (- top)) (point)))))
5109       ;; Do horizontal recentering while we're at it.
5110       (when (and (get-buffer-window (current-buffer) t)
5111                  (not (eq gnus-auto-center-summary 'vertical)))
5112         (let ((selected (selected-window)))
5113           (select-window (get-buffer-window (current-buffer) t))
5114           (gnus-summary-position-point)
5115           (gnus-horizontal-recenter)
5116           (select-window selected))))))
5117
5118 (defun gnus-summary-jump-to-group (newsgroup)
5119   "Move point to NEWSGROUP in group mode buffer."
5120   ;; Keep update point of group mode buffer if visible.
5121   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5122       (save-window-excursion
5123         ;; Take care of tree window mode.
5124         (when (get-buffer-window gnus-group-buffer)
5125           (pop-to-buffer gnus-group-buffer))
5126         (gnus-group-jump-to-group newsgroup))
5127     (save-excursion
5128       ;; Take care of tree window mode.
5129       (if (get-buffer-window gnus-group-buffer)
5130           (pop-to-buffer gnus-group-buffer)
5131         (set-buffer gnus-group-buffer))
5132       (gnus-group-jump-to-group newsgroup))))
5133
5134 ;; This function returns a list of article numbers based on the
5135 ;; difference between the ranges of read articles in this group and
5136 ;; the range of active articles.
5137 (defun gnus-list-of-unread-articles (group)
5138   (let* ((read (gnus-info-read (gnus-get-info group)))
5139          (active (or (gnus-active group) (gnus-activate-group group)))
5140          (last (cdr active))
5141          first nlast unread)
5142     ;; If none are read, then all are unread.
5143     (if (not read)
5144         (setq first (car active))
5145       ;; If the range of read articles is a single range, then the
5146       ;; first unread article is the article after the last read
5147       ;; article.  Sounds logical, doesn't it?
5148       (if (not (listp (cdr read)))
5149           (setq first (max (car active) (1+ (cdr read))))
5150         ;; `read' is a list of ranges.
5151         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5152                                   (caar read)))
5153                   1)
5154           (setq first (car active)))
5155         (while read
5156           (when first
5157             (while (< first nlast)
5158               (push first unread)
5159               (setq first (1+ first))))
5160           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5161           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5162           (setq read (cdr read)))))
5163     ;; And add the last unread articles.
5164     (while (<= first last)
5165       (push first unread)
5166       (setq first (1+ first)))
5167     ;; Return the list of unread articles.
5168     (delq 0 (nreverse unread))))
5169
5170 (defun gnus-list-of-read-articles (group)
5171   "Return a list of unread, unticked and non-dormant articles."
5172   (let* ((info (gnus-get-info group))
5173          (marked (gnus-info-marks info))
5174          (active (gnus-active group)))
5175     (and info active
5176          (gnus-set-difference
5177           (gnus-sorted-complement
5178            (gnus-uncompress-range active)
5179            (gnus-list-of-unread-articles group))
5180           (append
5181            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5182            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5183
5184 ;; Various summary commands
5185
5186 (defun gnus-summary-select-article-buffer ()
5187   "Reconfigure windows to show article buffer."
5188   (interactive)
5189   (if (not (gnus-buffer-live-p gnus-article-buffer))
5190       (error "There is no article buffer for this summary buffer")
5191     (gnus-configure-windows 'article)
5192     (select-window (get-buffer-window gnus-article-buffer))))
5193
5194 (defun gnus-summary-universal-argument (arg)
5195   "Perform any operation on all articles that are process/prefixed."
5196   (interactive "P")
5197   (let ((articles (gnus-summary-work-articles arg))
5198         func article)
5199     (if (eq
5200          (setq
5201           func
5202           (key-binding
5203            (read-key-sequence
5204             (substitute-command-keys
5205              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5206              ))))
5207          'undefined)
5208         (gnus-error 1 "Undefined key")
5209       (save-excursion
5210         (while articles
5211           (gnus-summary-goto-subject (setq article (pop articles)))
5212           (let (gnus-newsgroup-processable)
5213             (command-execute func))
5214           (gnus-summary-remove-process-mark article)))))
5215   (gnus-summary-position-point))
5216
5217 (defun gnus-summary-toggle-truncation (&optional arg)
5218   "Toggle truncation of summary lines.
5219 With arg, turn line truncation on iff arg is positive."
5220   (interactive "P")
5221   (setq truncate-lines
5222         (if (null arg) (not truncate-lines)
5223           (> (prefix-numeric-value arg) 0)))
5224   (redraw-display))
5225
5226 (defun gnus-summary-reselect-current-group (&optional all rescan)
5227   "Exit and then reselect the current newsgroup.
5228 The prefix argument ALL means to select all articles."
5229   (interactive "P")
5230   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5231     (error "Ephemeral groups can't be reselected"))
5232   (let ((current-subject (gnus-summary-article-number))
5233         (group gnus-newsgroup-name))
5234     (setq gnus-newsgroup-begin nil)
5235     (gnus-summary-exit)
5236     ;; We have to adjust the point of group mode buffer because
5237     ;; point was moved to the next unread newsgroup by exiting.
5238     (gnus-summary-jump-to-group group)
5239     (when rescan
5240       (save-excursion
5241         (gnus-group-get-new-news-this-group 1)))
5242     (gnus-group-read-group all t)
5243     (gnus-summary-goto-subject current-subject nil t)))
5244
5245 (defun gnus-summary-rescan-group (&optional all)
5246   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5247   (interactive "P")
5248   (gnus-summary-reselect-current-group all t))
5249
5250 (defun gnus-summary-update-info (&optional non-destructive)
5251   (save-excursion
5252     (let ((group gnus-newsgroup-name))
5253       (when group
5254         (when gnus-newsgroup-kill-headers
5255           (setq gnus-newsgroup-killed
5256                 (gnus-compress-sequence
5257                  (nconc
5258                   (gnus-set-sorted-intersection
5259                    (gnus-uncompress-range gnus-newsgroup-killed)
5260                    (setq gnus-newsgroup-unselected
5261                          (sort gnus-newsgroup-unselected '<)))
5262                   (setq gnus-newsgroup-unreads
5263                         (sort gnus-newsgroup-unreads '<)))
5264                  t)))
5265         (unless (listp (cdr gnus-newsgroup-killed))
5266           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5267         (let ((headers gnus-newsgroup-headers))
5268           ;; Set the new ranges of read articles.
5269           (save-excursion
5270             (set-buffer gnus-group-buffer)
5271             (gnus-undo-force-boundary))
5272           (gnus-update-read-articles
5273            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5274           ;; Set the current article marks.
5275           (let ((gnus-newsgroup-scored
5276                  (if (and (not gnus-save-score)
5277                           (not non-destructive))
5278                      nil
5279                    gnus-newsgroup-scored)))
5280             (save-excursion
5281               (gnus-update-marks)))
5282           ;; Do the cross-ref thing.
5283           (when gnus-use-cross-reference
5284             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5285           ;; Do not switch windows but change the buffer to work.
5286           (set-buffer gnus-group-buffer)
5287           (unless (gnus-ephemeral-group-p group)
5288             (gnus-group-update-group group)))))))
5289
5290 (defun gnus-summary-save-newsrc (&optional force)
5291   "Save the current number of read/marked articles in the dribble buffer.
5292 The dribble buffer will then be saved.
5293 If FORCE (the prefix), also save the .newsrc file(s)."
5294   (interactive "P")
5295   (gnus-summary-update-info t)
5296   (if force
5297       (gnus-save-newsrc-file)
5298     (gnus-dribble-save)))
5299
5300 (defun gnus-summary-exit (&optional temporary)
5301   "Exit reading current newsgroup, and then return to group selection mode.
5302 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5303   (interactive)
5304   (gnus-set-global-variables)
5305   (when (gnus-buffer-live-p gnus-article-buffer)
5306     (save-excursion
5307       (set-buffer gnus-article-buffer)
5308       (mm-destroy-parts gnus-article-mime-handles)))
5309   (gnus-kill-save-kill-buffer)
5310   (gnus-async-halt-prefetch)
5311   (let* ((group gnus-newsgroup-name)
5312          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5313          (mode major-mode)
5314          (group-point nil)
5315          (buf (current-buffer)))
5316     (unless quit-config
5317       ;; Do adaptive scoring, and possibly save score files.
5318       (when gnus-newsgroup-adaptive
5319         (gnus-score-adaptive))
5320       (when gnus-use-scoring
5321         (gnus-score-save)))
5322     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5323     ;; If we have several article buffers, we kill them at exit.
5324     (unless gnus-single-article-buffer
5325       (gnus-kill-buffer gnus-original-article-buffer)
5326       (setq gnus-article-current nil))
5327     (when gnus-use-cache
5328       (gnus-cache-possibly-remove-articles)
5329       (gnus-cache-save-buffers))
5330     (gnus-async-prefetch-remove-group group)
5331     (when gnus-suppress-duplicates
5332       (gnus-dup-enter-articles))
5333     (when gnus-use-trees
5334       (gnus-tree-close group))
5335     ;; Remove entries for this group.
5336     (nnmail-purge-split-history (gnus-group-real-name group))
5337     ;; Make all changes in this group permanent.
5338     (unless quit-config
5339       (gnus-run-hooks 'gnus-exit-group-hook)
5340       (gnus-summary-update-info))
5341     (gnus-close-group group)
5342     ;; Make sure where we were, and go to next newsgroup.
5343     (set-buffer gnus-group-buffer)
5344     (unless quit-config
5345       (gnus-group-jump-to-group group))
5346     (gnus-run-hooks 'gnus-summary-exit-hook)
5347     (unless (or quit-config
5348                 ;; If this group has disappeared from the summary
5349                 ;; buffer, don't skip forwards.
5350                 (not (string= group (gnus-group-group-name))))
5351       (gnus-group-next-unread-group 1))
5352     (setq group-point (point))
5353     (if temporary
5354         nil                             ;Nothing to do.
5355       ;; If we have several article buffers, we kill them at exit.
5356       (unless gnus-single-article-buffer
5357         (gnus-kill-buffer gnus-article-buffer)
5358         (gnus-kill-buffer gnus-original-article-buffer)
5359         (setq gnus-article-current nil))
5360       (set-buffer buf)
5361       (if (not gnus-kill-summary-on-exit)
5362           (gnus-deaden-summary)
5363         ;; We set all buffer-local variables to nil.  It is unclear why
5364         ;; this is needed, but if we don't, buffer-local variables are
5365         ;; not garbage-collected, it seems.  This would the lead to en
5366         ;; ever-growing Emacs.
5367         (gnus-summary-clear-local-variables)
5368         (when (get-buffer gnus-article-buffer)
5369           (bury-buffer gnus-article-buffer))
5370         ;; We clear the global counterparts of the buffer-local
5371         ;; variables as well, just to be on the safe side.
5372         (set-buffer gnus-group-buffer)
5373         (gnus-summary-clear-local-variables)
5374         ;; Return to group mode buffer.
5375         (when (eq mode 'gnus-summary-mode)
5376           (gnus-kill-buffer buf)))
5377       (setq gnus-current-select-method gnus-select-method)
5378       (pop-to-buffer gnus-group-buffer)
5379       (if (not quit-config)
5380           (progn
5381             (goto-char group-point)
5382             (gnus-configure-windows 'group 'force))
5383         (gnus-handle-ephemeral-exit quit-config))
5384       ;; Clear the current group name.
5385       (unless quit-config
5386         (setq gnus-newsgroup-name nil)))))
5387
5388 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5389 (defun gnus-summary-exit-no-update (&optional no-questions)
5390   "Quit reading current newsgroup without updating read article info."
5391   (interactive)
5392   (let* ((group gnus-newsgroup-name)
5393          (quit-config (gnus-group-quit-config group)))
5394     (when (or no-questions
5395               gnus-expert-user
5396               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5397       (gnus-async-halt-prefetch)
5398       (mapcar 'funcall
5399               (delq 'gnus-summary-expire-articles
5400                     (copy-list gnus-summary-prepare-exit-hook)))
5401       (when (gnus-buffer-live-p gnus-article-buffer)
5402         (save-excursion
5403           (set-buffer gnus-article-buffer)
5404           (mm-destroy-parts gnus-article-mime-handles)))
5405       ;; If we have several article buffers, we kill them at exit.
5406       (unless gnus-single-article-buffer
5407         (gnus-kill-buffer gnus-article-buffer)
5408         (gnus-kill-buffer gnus-original-article-buffer)
5409         (setq gnus-article-current nil))
5410       (if (not gnus-kill-summary-on-exit)
5411           (gnus-deaden-summary)
5412         (gnus-close-group group)
5413         (gnus-summary-clear-local-variables)
5414         (set-buffer gnus-group-buffer)
5415         (gnus-summary-clear-local-variables)
5416         (when (get-buffer gnus-summary-buffer)
5417           (kill-buffer gnus-summary-buffer)))
5418       (unless gnus-single-article-buffer
5419         (setq gnus-article-current nil))
5420       (when gnus-use-trees
5421         (gnus-tree-close group))
5422       (gnus-async-prefetch-remove-group group)
5423       (when (get-buffer gnus-article-buffer)
5424         (bury-buffer gnus-article-buffer))
5425       ;; Return to the group buffer.
5426       (gnus-configure-windows 'group 'force)
5427       ;; Clear the current group name.
5428       (setq gnus-newsgroup-name nil)
5429       (when (equal (gnus-group-group-name) group)
5430         (gnus-group-next-unread-group 1))
5431       (when quit-config
5432         (gnus-handle-ephemeral-exit quit-config)))))
5433
5434 (defun gnus-handle-ephemeral-exit (quit-config)
5435   "Handle movement when leaving an ephemeral group.
5436 The state which existed when entering the ephemeral is reset."
5437   (if (not (buffer-name (car quit-config)))
5438       (gnus-configure-windows 'group 'force)
5439     (set-buffer (car quit-config))
5440     (cond ((eq major-mode 'gnus-summary-mode)
5441            (gnus-set-global-variables))
5442           ((eq major-mode 'gnus-article-mode)
5443            (save-excursion
5444              ;; The `gnus-summary-buffer' variable may point
5445              ;; to the old summary buffer when using a single
5446              ;; article buffer.
5447              (unless (gnus-buffer-live-p gnus-summary-buffer)
5448                (set-buffer gnus-group-buffer))
5449              (set-buffer gnus-summary-buffer)
5450              (gnus-set-global-variables))))
5451     (if (or (eq (cdr quit-config) 'article)
5452             (eq (cdr quit-config) 'pick))
5453         (progn
5454           ;; The current article may be from the ephemeral group
5455           ;; thus it is best that we reload this article
5456           (gnus-summary-show-article)
5457           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5458               (gnus-configure-windows 'pick 'force)
5459             (gnus-configure-windows (cdr quit-config) 'force)))
5460       (gnus-configure-windows (cdr quit-config) 'force))
5461     (when (eq major-mode 'gnus-summary-mode)
5462       (gnus-summary-next-subject 1 nil t)
5463       (gnus-summary-recenter)
5464       (gnus-summary-position-point))))
5465
5466 ;;; Dead summaries.
5467
5468 (defvar gnus-dead-summary-mode-map nil)
5469
5470 (unless gnus-dead-summary-mode-map
5471   (setq gnus-dead-summary-mode-map (make-keymap))
5472   (suppress-keymap gnus-dead-summary-mode-map)
5473   (substitute-key-definition
5474    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5475   (let ((keys '("\C-d" "\r" "\177" [delete])))
5476     (while keys
5477       (define-key gnus-dead-summary-mode-map
5478         (pop keys) 'gnus-summary-wake-up-the-dead))))
5479
5480 (defvar gnus-dead-summary-mode nil
5481   "Minor mode for Gnus summary buffers.")
5482
5483 (defun gnus-dead-summary-mode (&optional arg)
5484   "Minor mode for Gnus summary buffers."
5485   (interactive "P")
5486   (when (eq major-mode 'gnus-summary-mode)
5487     (make-local-variable 'gnus-dead-summary-mode)
5488     (setq gnus-dead-summary-mode
5489           (if (null arg) (not gnus-dead-summary-mode)
5490             (> (prefix-numeric-value arg) 0)))
5491     (when gnus-dead-summary-mode
5492       (gnus-add-minor-mode
5493        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5494
5495 (defun gnus-deaden-summary ()
5496   "Make the current summary buffer into a dead summary buffer."
5497   ;; Kill any previous dead summary buffer.
5498   (when (and gnus-dead-summary
5499              (buffer-name gnus-dead-summary))
5500     (save-excursion
5501       (set-buffer gnus-dead-summary)
5502       (when gnus-dead-summary-mode
5503         (kill-buffer (current-buffer)))))
5504   ;; Make this the current dead summary.
5505   (setq gnus-dead-summary (current-buffer))
5506   (gnus-dead-summary-mode 1)
5507   (let ((name (buffer-name)))
5508     (when (string-match "Summary" name)
5509       (rename-buffer
5510        (concat (substring name 0 (match-beginning 0)) "Dead "
5511                (substring name (match-beginning 0)))
5512        t))))
5513
5514 (defun gnus-kill-or-deaden-summary (buffer)
5515   "Kill or deaden the summary BUFFER."
5516   (save-excursion
5517     (when (and (buffer-name buffer)
5518                (not gnus-single-article-buffer))
5519       (save-excursion
5520         (set-buffer buffer)
5521         (gnus-kill-buffer gnus-article-buffer)
5522         (gnus-kill-buffer gnus-original-article-buffer)))
5523     (cond (gnus-kill-summary-on-exit
5524            (when (and gnus-use-trees
5525                       (gnus-buffer-exists-p buffer))
5526              (save-excursion
5527                (set-buffer buffer)
5528                (gnus-tree-close gnus-newsgroup-name)))
5529            (gnus-kill-buffer buffer))
5530           ((gnus-buffer-exists-p buffer)
5531            (save-excursion
5532              (set-buffer buffer)
5533              (gnus-deaden-summary))))))
5534
5535 (defun gnus-summary-wake-up-the-dead (&rest args)
5536   "Wake up the dead summary buffer."
5537   (interactive)
5538   (gnus-dead-summary-mode -1)
5539   (let ((name (buffer-name)))
5540     (when (string-match "Dead " name)
5541       (rename-buffer
5542        (concat (substring name 0 (match-beginning 0))
5543                (substring name (match-end 0)))
5544        t)))
5545   (gnus-message 3 "This dead summary is now alive again"))
5546
5547 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5548 (defun gnus-summary-fetch-faq (&optional faq-dir)
5549   "Fetch the FAQ for the current group.
5550 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5551 in."
5552   (interactive
5553    (list
5554     (when current-prefix-arg
5555       (completing-read
5556        "Faq dir: " (and (listp gnus-group-faq-directory)
5557                         (mapcar (lambda (file) (list file))
5558                                 gnus-group-faq-directory))))))
5559   (let (gnus-faq-buffer)
5560     (when (setq gnus-faq-buffer
5561                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5562       (gnus-configure-windows 'summary-faq))))
5563
5564 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5565 (defun gnus-summary-describe-group (&optional force)
5566   "Describe the current newsgroup."
5567   (interactive "P")
5568   (gnus-group-describe-group force gnus-newsgroup-name))
5569
5570 (defun gnus-summary-describe-briefly ()
5571   "Describe summary mode commands briefly."
5572   (interactive)
5573   (gnus-message 6 (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")))
5574
5575 ;; Walking around group mode buffer from summary mode.
5576
5577 (defun gnus-summary-next-group (&optional no-article target-group backward)
5578   "Exit current newsgroup and then select next unread newsgroup.
5579 If prefix argument NO-ARTICLE is non-nil, no article is selected
5580 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5581 previous group instead."
5582   (interactive "P")
5583   ;; Stop pre-fetching.
5584   (gnus-async-halt-prefetch)
5585   (let ((current-group gnus-newsgroup-name)
5586         (current-buffer (current-buffer))
5587         entered)
5588     ;; First we semi-exit this group to update Xrefs and all variables.
5589     ;; We can't do a real exit, because the window conf must remain
5590     ;; the same in case the user is prompted for info, and we don't
5591     ;; want the window conf to change before that...
5592     (gnus-summary-exit t)
5593     (while (not entered)
5594       ;; Then we find what group we are supposed to enter.
5595       (set-buffer gnus-group-buffer)
5596       (gnus-group-jump-to-group current-group)
5597       (setq target-group
5598             (or target-group
5599                 (if (eq gnus-keep-same-level 'best)
5600                     (gnus-summary-best-group gnus-newsgroup-name)
5601                   (gnus-summary-search-group backward gnus-keep-same-level))))
5602       (if (not target-group)
5603           ;; There are no further groups, so we return to the group
5604           ;; buffer.
5605           (progn
5606             (gnus-message 5 "Returning to the group buffer")
5607             (setq entered t)
5608             (when (gnus-buffer-live-p current-buffer)
5609               (set-buffer current-buffer)
5610               (gnus-summary-exit))
5611             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5612         ;; We try to enter the target group.
5613         (gnus-group-jump-to-group target-group)
5614         (let ((unreads (gnus-group-group-unread)))
5615           (if (and (or (eq t unreads)
5616                        (and unreads (not (zerop unreads))))
5617                    (gnus-summary-read-group
5618                     target-group nil no-article
5619                     (and (buffer-name current-buffer) current-buffer)
5620                     nil backward))
5621               (setq entered t)
5622             (setq current-group target-group
5623                   target-group nil)))))))
5624
5625 (defun gnus-summary-prev-group (&optional no-article)
5626   "Exit current newsgroup and then select previous unread newsgroup.
5627 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5628   (interactive "P")
5629   (gnus-summary-next-group no-article nil t))
5630
5631 ;; Walking around summary lines.
5632
5633 (defun gnus-summary-first-subject (&optional unread undownloaded)
5634   "Go to the first unread subject.
5635 If UNREAD is non-nil, go to the first unread article.
5636 Returns the article selected or nil if there are no unread articles."
5637   (interactive "P")
5638   (prog1
5639       (cond
5640        ;; Empty summary.
5641        ((null gnus-newsgroup-data)
5642         (gnus-message 3 "No articles in the group")
5643         nil)
5644        ;; Pick the first article.
5645        ((not unread)
5646         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5647         (gnus-data-number (car gnus-newsgroup-data)))
5648        ;; No unread articles.
5649        ((null gnus-newsgroup-unreads)
5650         (gnus-message 3 "No more unread articles")
5651         nil)
5652        ;; Find the first unread article.
5653        (t
5654         (let ((data gnus-newsgroup-data))
5655           (while (and data
5656                       (and (not (and undownloaded
5657                                      (eq gnus-undownloaded-mark
5658                                          (gnus-data-mark (car data)))))
5659                            (not (gnus-data-unread-p (car data)))))
5660             (setq data (cdr data)))
5661           (when data
5662             (goto-char (gnus-data-pos (car data)))
5663             (gnus-data-number (car data))))))
5664     (gnus-summary-position-point)))
5665
5666 (defun gnus-summary-next-subject (n &optional unread dont-display)
5667   "Go to next N'th summary line.
5668 If N is negative, go to the previous N'th subject line.
5669 If UNREAD is non-nil, only unread articles are selected.
5670 The difference between N and the actual number of steps taken is
5671 returned."
5672   (interactive "p")
5673   (let ((backward (< n 0))
5674         (n (abs n)))
5675     (while (and (> n 0)
5676                 (if backward
5677                     (gnus-summary-find-prev unread)
5678                   (gnus-summary-find-next unread)))
5679       (gnus-summary-show-thread)
5680       (setq n (1- n)))
5681     (when (/= 0 n)
5682       (gnus-message 7 "No more%s articles"
5683                     (if unread " unread" "")))
5684     (unless dont-display
5685       (gnus-summary-recenter)
5686       (gnus-summary-position-point))
5687     n))
5688
5689 (defun gnus-summary-next-unread-subject (n)
5690   "Go to next N'th unread summary line."
5691   (interactive "p")
5692   (gnus-summary-next-subject n t))
5693
5694 (defun gnus-summary-prev-subject (n &optional unread)
5695   "Go to previous N'th summary line.
5696 If optional argument UNREAD is non-nil, only unread article is selected."
5697   (interactive "p")
5698   (gnus-summary-next-subject (- n) unread))
5699
5700 (defun gnus-summary-prev-unread-subject (n)
5701   "Go to previous N'th unread summary line."
5702   (interactive "p")
5703   (gnus-summary-next-subject (- n) t))
5704
5705 (defun gnus-summary-goto-subject (article &optional force silent)
5706   "Go the subject line of ARTICLE.
5707 If FORCE, also allow jumping to articles not currently shown."
5708   (interactive "nArticle number: ")
5709   (let ((b (point))
5710         (data (gnus-data-find article)))
5711     ;; We read in the article if we have to.
5712     (and (not data)
5713          force
5714          (gnus-summary-insert-subject
5715           article
5716           (if (or (numberp force) (vectorp force)) force)
5717           t)
5718          (setq data (gnus-data-find article)))
5719     (goto-char b)
5720     (if (not data)
5721         (progn
5722           (unless silent
5723             (gnus-message 3 "Can't find article %d" article))
5724           nil)
5725       (goto-char (gnus-data-pos data))
5726       (gnus-summary-position-point)
5727       article)))
5728
5729 ;; Walking around summary lines with displaying articles.
5730
5731 (defun gnus-summary-expand-window (&optional arg)
5732   "Make the summary buffer take up the entire Emacs frame.
5733 Given a prefix, will force an `article' buffer configuration."
5734   (interactive "P")
5735   (if arg
5736       (gnus-configure-windows 'article 'force)
5737     (gnus-configure-windows 'summary 'force)))
5738
5739 (defun gnus-summary-display-article (article &optional all-header)
5740   "Display ARTICLE in article buffer."
5741   (gnus-set-global-variables)
5742   (if (null article)
5743       nil
5744     (prog1
5745         (if gnus-summary-display-article-function
5746             (funcall gnus-summary-display-article-function article all-header)
5747           (gnus-article-prepare article all-header))
5748       (gnus-run-hooks 'gnus-select-article-hook)
5749       (when (and gnus-current-article
5750                  (not (zerop gnus-current-article)))
5751         (gnus-summary-goto-subject gnus-current-article))
5752       (gnus-summary-recenter)
5753       (when (and gnus-use-trees gnus-show-threads)
5754         (gnus-possibly-generate-tree article)
5755         (gnus-highlight-selected-tree article))
5756       ;; Successfully display article.
5757       (gnus-article-set-window-start
5758        (cdr (assq article gnus-newsgroup-bookmarks))))))
5759
5760 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5761   "Select the current article.
5762 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5763 non-nil, the article will be re-fetched even if it already present in
5764 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5765 be displayed."
5766   ;; Make sure we are in the summary buffer to work around bbdb bug.
5767   (unless (eq major-mode 'gnus-summary-mode)
5768     (set-buffer gnus-summary-buffer))
5769   (let ((article (or article (gnus-summary-article-number)))
5770         (all-headers (not (not all-headers))) ;Must be T or NIL.
5771         gnus-summary-display-article-function
5772         did)
5773     (and (not pseudo)
5774          (gnus-summary-article-pseudo-p article)
5775          (error "This is a pseudo-article"))
5776     (prog1
5777         (save-excursion
5778           (set-buffer gnus-summary-buffer)
5779           (if (or (and gnus-single-article-buffer
5780                        (or (null gnus-current-article)
5781                            (null gnus-article-current)
5782                            (null (get-buffer gnus-article-buffer))
5783                            (not (eq article (cdr gnus-article-current)))
5784                            (not (equal (car gnus-article-current)
5785                                        gnus-newsgroup-name))))
5786                   (and (not gnus-single-article-buffer)
5787                        (or (null gnus-current-article)
5788                            (not (eq gnus-current-article article))))
5789                   force)
5790               ;; The requested article is different from the current article.
5791               (prog1
5792                   (gnus-summary-display-article article all-headers)
5793                 (setq did article)
5794                 (when (or all-headers gnus-show-all-headers)
5795                   (gnus-article-show-all-headers)))
5796             (when (or all-headers gnus-show-all-headers)
5797               (gnus-article-show-all-headers))
5798             'old))
5799       (when did
5800         (gnus-article-set-window-start
5801          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5802
5803 (defun gnus-summary-set-current-mark (&optional current-mark)
5804   "Obsolete function."
5805   nil)
5806
5807 (defun gnus-summary-next-article (&optional unread subject backward push)
5808   "Select the next article.
5809 If UNREAD, only unread articles are selected.
5810 If SUBJECT, only articles with SUBJECT are selected.
5811 If BACKWARD, the previous article is selected instead of the next."
5812   (interactive "P")
5813   (cond
5814    ;; Is there such an article?
5815    ((and (gnus-summary-search-forward unread subject backward)
5816          (or (gnus-summary-display-article (gnus-summary-article-number))
5817              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5818     (gnus-summary-position-point))
5819    ;; If not, we try the first unread, if that is wanted.
5820    ((and subject
5821          gnus-auto-select-same
5822          (gnus-summary-first-unread-article))
5823     (gnus-summary-position-point)
5824     (gnus-message 6 "Wrapped"))
5825    ;; Try to get next/previous article not displayed in this group.
5826    ((and gnus-auto-extend-newsgroup
5827          (not unread) (not subject))
5828     (gnus-summary-goto-article
5829      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5830      nil (count-lines (point-min) (point))))
5831    ;; Go to next/previous group.
5832    (t
5833     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5834       (gnus-summary-jump-to-group gnus-newsgroup-name))
5835     (let ((cmd last-command-char)
5836           (point
5837            (save-excursion
5838              (set-buffer gnus-group-buffer)
5839              (point)))
5840           (group
5841            (if (eq gnus-keep-same-level 'best)
5842                (gnus-summary-best-group gnus-newsgroup-name)
5843              (gnus-summary-search-group backward gnus-keep-same-level))))
5844       ;; For some reason, the group window gets selected.  We change
5845       ;; it back.
5846       (select-window (get-buffer-window (current-buffer)))
5847       ;; Select next unread newsgroup automagically.
5848       (cond
5849        ((or (not gnus-auto-select-next)
5850             (not cmd))
5851         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5852        ((or (eq gnus-auto-select-next 'quietly)
5853             (and (eq gnus-auto-select-next 'slightly-quietly)
5854                  push)
5855             (and (eq gnus-auto-select-next 'almost-quietly)
5856                  (gnus-summary-last-article-p)))
5857         ;; Select quietly.
5858         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5859             (gnus-summary-exit)
5860           (gnus-message 7 "No more%s articles (%s)..."
5861                         (if unread " unread" "")
5862                         (if group (concat "selecting " group)
5863                           "exiting"))
5864           (gnus-summary-next-group nil group backward)))
5865        (t
5866         (when (gnus-key-press-event-p last-input-event)
5867           (gnus-summary-walk-group-buffer
5868            gnus-newsgroup-name cmd unread backward point))))))))
5869
5870 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5871   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5872                       (?\C-p (gnus-group-prev-unread-group 1))))
5873         (cursor-in-echo-area t)
5874         keve key group ended)
5875     (save-excursion
5876       (set-buffer gnus-group-buffer)
5877       (goto-char start)
5878       (setq group
5879             (if (eq gnus-keep-same-level 'best)
5880                 (gnus-summary-best-group gnus-newsgroup-name)
5881               (gnus-summary-search-group backward gnus-keep-same-level))))
5882     (while (not ended)
5883       (gnus-message
5884        5 "No more%s articles%s" (if unread " unread" "")
5885        (if (and group
5886                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5887            (format " (Type %s for %s [%s])"
5888                    (single-key-description cmd) group
5889                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5890          (format " (Type %s to exit %s)"
5891                  (single-key-description cmd)
5892                  gnus-newsgroup-name)))
5893       ;; Confirm auto selection.
5894       (setq key (car (setq keve (gnus-read-event-char))))
5895       (setq ended t)
5896       (cond
5897        ((assq key keystrokes)
5898         (let ((obuf (current-buffer)))
5899           (switch-to-buffer gnus-group-buffer)
5900           (when group
5901             (gnus-group-jump-to-group group))
5902           (eval (cadr (assq key keystrokes)))
5903           (setq group (gnus-group-group-name))
5904           (switch-to-buffer obuf))
5905         (setq ended nil))
5906        ((equal key cmd)
5907         (if (or (not group)
5908                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5909             (gnus-summary-exit)
5910           (gnus-summary-next-group nil group backward)))
5911        (t
5912         (push (cdr keve) unread-command-events))))))
5913
5914 (defun gnus-summary-next-unread-article ()
5915   "Select unread article after current one."
5916   (interactive)
5917   (gnus-summary-next-article
5918    (or (not (eq gnus-summary-goto-unread 'never))
5919        (gnus-summary-last-article-p (gnus-summary-article-number)))
5920    (and gnus-auto-select-same
5921         (gnus-summary-article-subject))))
5922
5923 (defun gnus-summary-prev-article (&optional unread subject)
5924   "Select the article after the current one.
5925 If UNREAD is non-nil, only unread articles are selected."
5926   (interactive "P")
5927   (gnus-summary-next-article unread subject t))
5928
5929 (defun gnus-summary-prev-unread-article ()
5930   "Select unread article before current one."
5931   (interactive)
5932   (gnus-summary-prev-article
5933    (or (not (eq gnus-summary-goto-unread 'never))
5934        (gnus-summary-first-article-p (gnus-summary-article-number)))
5935    (and gnus-auto-select-same
5936         (gnus-summary-article-subject))))
5937
5938 (defun gnus-summary-next-page (&optional lines circular)
5939   "Show next page of the selected article.
5940 If at the end of the current article, select the next article.
5941 LINES says how many lines should be scrolled up.
5942
5943 If CIRCULAR is non-nil, go to the start of the article instead of
5944 selecting the next article when reaching the end of the current
5945 article."
5946   (interactive "P")
5947   (setq gnus-summary-buffer (current-buffer))
5948   (gnus-set-global-variables)
5949   (let ((article (gnus-summary-article-number))
5950         (article-window (get-buffer-window gnus-article-buffer t))
5951         endp)
5952     ;; If the buffer is empty, we have no article.
5953     (unless article
5954       (error "No article to select"))
5955     (gnus-configure-windows 'article)
5956     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5957         (if (and (eq gnus-summary-goto-unread 'never)
5958                  (not (gnus-summary-last-article-p article)))
5959             (gnus-summary-next-article)
5960           (gnus-summary-next-unread-article))
5961       (if (or (null gnus-current-article)
5962               (null gnus-article-current)
5963               (/= article (cdr gnus-article-current))
5964               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5965           ;; Selected subject is different from current article's.
5966           (gnus-summary-display-article article)
5967         (when article-window
5968           (gnus-eval-in-buffer-window gnus-article-buffer
5969             (setq endp (gnus-article-next-page lines)))
5970           (when endp
5971             (cond (circular
5972                    (gnus-summary-beginning-of-article))
5973                   (lines
5974                    (gnus-message 3 "End of message"))
5975                   ((null lines)
5976                    (if (and (eq gnus-summary-goto-unread 'never)
5977                             (not (gnus-summary-last-article-p article)))
5978                        (gnus-summary-next-article)
5979                      (gnus-summary-next-unread-article))))))))
5980     (gnus-summary-recenter)
5981     (gnus-summary-position-point)))
5982
5983 (defun gnus-summary-prev-page (&optional lines move)
5984   "Show previous page of selected article.
5985 Argument LINES specifies lines to be scrolled down.
5986 If MOVE, move to the previous unread article if point is at
5987 the beginning of the buffer."
5988   (interactive "P")
5989   (let ((article (gnus-summary-article-number))
5990         (article-window (get-buffer-window gnus-article-buffer t))
5991         endp)
5992     (gnus-configure-windows 'article)
5993     (if (or (null gnus-current-article)
5994             (null gnus-article-current)
5995             (/= article (cdr gnus-article-current))
5996             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5997         ;; Selected subject is different from current article's.
5998         (gnus-summary-display-article article)
5999       (gnus-summary-recenter)
6000       (when article-window
6001         (gnus-eval-in-buffer-window gnus-article-buffer
6002           (setq endp (gnus-article-prev-page lines)))
6003         (when (and move endp)
6004           (cond (lines
6005                  (gnus-message 3 "Beginning of message"))
6006                 ((null lines)
6007                  (if (and (eq gnus-summary-goto-unread 'never)
6008                           (not (gnus-summary-first-article-p article)))
6009                      (gnus-summary-prev-article)
6010                    (gnus-summary-prev-unread-article))))))))
6011   (gnus-summary-position-point))
6012
6013 (defun gnus-summary-prev-page-or-article (&optional lines)
6014   "Show previous page of selected article.
6015 Argument LINES specifies lines to be scrolled down.
6016 If at the beginning of the article, go to the next article."
6017   (interactive "P")
6018   (gnus-summary-prev-page lines t))
6019
6020 (defun gnus-summary-scroll-up (lines)
6021   "Scroll up (or down) one line current article.
6022 Argument LINES specifies lines to be scrolled up (or down if negative)."
6023   (interactive "p")
6024   (gnus-configure-windows 'article)
6025   (gnus-summary-show-thread)
6026   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6027     (gnus-eval-in-buffer-window gnus-article-buffer
6028       (cond ((> lines 0)
6029              (when (gnus-article-next-page lines)
6030                (gnus-message 3 "End of message")))
6031             ((< lines 0)
6032              (gnus-article-prev-page (- lines))))))
6033   (gnus-summary-recenter)
6034   (gnus-summary-position-point))
6035
6036 (defun gnus-summary-scroll-down (lines)
6037   "Scroll down (or up) one line current article.
6038 Argument LINES specifies lines to be scrolled down (or up if negative)."
6039   (interactive "p")
6040   (gnus-summary-scroll-up (- lines)))
6041
6042 (defun gnus-summary-next-same-subject ()
6043   "Select next article which has the same subject as current one."
6044   (interactive)
6045   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6046
6047 (defun gnus-summary-prev-same-subject ()
6048   "Select previous article which has the same subject as current one."
6049   (interactive)
6050   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6051
6052 (defun gnus-summary-next-unread-same-subject ()
6053   "Select next unread article which has the same subject as current one."
6054   (interactive)
6055   (gnus-summary-next-article t (gnus-summary-article-subject)))
6056
6057 (defun gnus-summary-prev-unread-same-subject ()
6058   "Select previous unread article which has the same subject as current one."
6059   (interactive)
6060   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6061
6062 (defun gnus-summary-first-unread-article ()
6063   "Select the first unread article.
6064 Return nil if there are no unread articles."
6065   (interactive)
6066   (prog1
6067       (when (gnus-summary-first-subject t)
6068         (gnus-summary-show-thread)
6069         (gnus-summary-first-subject t)
6070         (gnus-summary-display-article (gnus-summary-article-number)))
6071     (gnus-summary-position-point)))
6072
6073 (defun gnus-summary-first-unread-subject ()
6074   "Place the point on the subject line of the first unread article.
6075 Return nil if there are no unread articles."
6076   (interactive)
6077   (prog1
6078       (when (gnus-summary-first-subject t)
6079         (gnus-summary-show-thread)
6080         (gnus-summary-first-subject t))
6081     (gnus-summary-position-point)))
6082
6083 (defun gnus-summary-first-article ()
6084   "Select the first article.
6085 Return nil if there are no articles."
6086   (interactive)
6087   (prog1
6088       (when (gnus-summary-first-subject)
6089         (gnus-summary-show-thread)
6090         (gnus-summary-first-subject)
6091         (gnus-summary-display-article (gnus-summary-article-number)))
6092     (gnus-summary-position-point)))
6093
6094 (defun gnus-summary-best-unread-article ()
6095   "Select the unread article with the highest score."
6096   (interactive)
6097   (let ((best -1000000)
6098         (data gnus-newsgroup-data)
6099         article score)
6100     (while data
6101       (and (gnus-data-unread-p (car data))
6102            (> (setq score
6103                     (gnus-summary-article-score (gnus-data-number (car data))))
6104               best)
6105            (setq best score
6106                  article (gnus-data-number (car data))))
6107       (setq data (cdr data)))
6108     (prog1
6109         (if article
6110             (gnus-summary-goto-article article)
6111           (error "No unread articles"))
6112       (gnus-summary-position-point))))
6113
6114 (defun gnus-summary-last-subject ()
6115   "Go to the last displayed subject line in the group."
6116   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6117     (when article
6118       (gnus-summary-goto-subject article))))
6119
6120 (defun gnus-summary-goto-article (article &optional all-headers force)
6121   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6122 If ALL-HEADERS is non-nil, no header lines are hidden.
6123 If FORCE, go to the article even if it isn't displayed.  If FORCE
6124 is a number, it is the line the article is to be displayed on."
6125   (interactive
6126    (list
6127     (completing-read
6128      "Article number or Message-ID: "
6129      (mapcar (lambda (number) (list (int-to-string number)))
6130              gnus-newsgroup-limit))
6131     current-prefix-arg
6132     t))
6133   (prog1
6134       (if (and (stringp article)
6135                (string-match "@" article))
6136           (gnus-summary-refer-article article)
6137         (when (stringp article)
6138           (setq article (string-to-number article)))
6139         (if (gnus-summary-goto-subject article force)
6140             (gnus-summary-display-article article all-headers)
6141           (gnus-message 4 "Couldn't go to article %s" article) nil))
6142     (gnus-summary-position-point)))
6143
6144 (defun gnus-summary-goto-last-article ()
6145   "Go to the previously read article."
6146   (interactive)
6147   (prog1
6148       (when gnus-last-article
6149         (gnus-summary-goto-article gnus-last-article nil t))
6150     (gnus-summary-position-point)))
6151
6152 (defun gnus-summary-pop-article (number)
6153   "Pop one article off the history and go to the previous.
6154 NUMBER articles will be popped off."
6155   (interactive "p")
6156   (let (to)
6157     (setq gnus-newsgroup-history
6158           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6159     (if to
6160         (gnus-summary-goto-article (car to) nil t)
6161       (error "Article history empty")))
6162   (gnus-summary-position-point))
6163
6164 ;; Summary commands and functions for limiting the summary buffer.
6165
6166 (defun gnus-summary-limit-to-articles (n)
6167   "Limit the summary buffer to the next N articles.
6168 If not given a prefix, use the process marked articles instead."
6169   (interactive "P")
6170   (prog1
6171       (let ((articles (gnus-summary-work-articles n)))
6172         (setq gnus-newsgroup-processable nil)
6173         (gnus-summary-limit articles))
6174     (gnus-summary-position-point)))
6175
6176 (defun gnus-summary-pop-limit (&optional total)
6177   "Restore the previous limit.
6178 If given a prefix, remove all limits."
6179   (interactive "P")
6180   (when total
6181     (setq gnus-newsgroup-limits
6182           (list (mapcar (lambda (h) (mail-header-number h))
6183                         gnus-newsgroup-headers))))
6184   (unless gnus-newsgroup-limits
6185     (error "No limit to pop"))
6186   (prog1
6187       (gnus-summary-limit nil 'pop)
6188     (gnus-summary-position-point)))
6189
6190 (defun gnus-summary-limit-to-subject (subject &optional header)
6191   "Limit the summary buffer to articles that have subjects that match a regexp."
6192   (interactive "sLimit to subject (regexp): ")
6193   (unless header
6194     (setq header "subject"))
6195   (when (not (equal "" subject))
6196     (prog1
6197         (let ((articles (gnus-summary-find-matching
6198                          (or header "subject") subject 'all)))
6199           (unless articles
6200             (error "Found no matches for \"%s\"" subject))
6201           (gnus-summary-limit articles))
6202       (gnus-summary-position-point))))
6203
6204 (defun gnus-summary-limit-to-author (from)
6205   "Limit the summary buffer to articles that have authors that match a regexp."
6206   (interactive "sLimit to author (regexp): ")
6207   (gnus-summary-limit-to-subject from "from"))
6208
6209 (defun gnus-summary-limit-to-age (age &optional younger-p)
6210   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6211 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6212 articles that are younger than AGE days."
6213   (interactive
6214    (let ((younger current-prefix-arg)
6215          (days-got nil)
6216          days)
6217      (while (not days-got)
6218        (setq days (if younger
6219                       (read-string "Limit to articles within (in days): ")
6220                     (read-string "Limit to articles old than (in days): ")))
6221        (when (> (length days) 0)
6222          (setq days (read days)))
6223        (if (numberp days)
6224            (setq days-got t)
6225          (message "Please enter a number.")
6226          (sleep-for 1)))
6227      (list days younger)))
6228   (prog1
6229       (let ((data gnus-newsgroup-data)
6230             (cutoff (days-to-time age))
6231             articles d date is-younger)
6232         (while (setq d (pop data))
6233           (when (and (vectorp (gnus-data-header d))
6234                      (setq date (mail-header-date (gnus-data-header d))))
6235             (setq is-younger (time-less-p
6236                               (time-since (condition-case ()
6237                                               (date-to-time date)
6238                                             (error '(0 0))))
6239                               cutoff))
6240             (when (if younger-p
6241                       is-younger
6242                     (not is-younger))
6243               (push (gnus-data-number d) articles))))
6244         (gnus-summary-limit (nreverse articles)))
6245     (gnus-summary-position-point)))
6246
6247 (defun gnus-summary-limit-to-extra (header regexp)
6248   "Limit the summary buffer to articles that match an 'extra' header."
6249   (interactive
6250    (let ((header
6251           (intern
6252            (gnus-completing-read
6253             (symbol-name (car gnus-extra-headers))      
6254             "Limit extra header:"       
6255             (mapcar (lambda (x) 
6256                       (cons (symbol-name x) x))
6257                     gnus-extra-headers)
6258             nil                 
6259             t))))
6260      (list header
6261            (read-string (format "Limit to header %s (regexp): " header)))))
6262   (when (not (equal "" regexp))
6263     (prog1
6264         (let ((articles (gnus-summary-find-matching
6265                          (cons 'extra header) regexp 'all)))
6266           (unless articles
6267             (error "Found no matches for \"%s\"" regexp))
6268           (gnus-summary-limit articles))
6269       (gnus-summary-position-point))))
6270
6271 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6272 (make-obsolete
6273  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6274
6275 (defun gnus-summary-limit-to-unread (&optional all)
6276   "Limit the summary buffer to articles that are not marked as read.
6277 If ALL is non-nil, limit strictly to unread articles."
6278   (interactive "P")
6279   (if all
6280       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6281     (gnus-summary-limit-to-marks
6282      ;; Concat all the marks that say that an article is read and have
6283      ;; those removed.
6284      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6285            gnus-killed-mark gnus-kill-file-mark
6286            gnus-low-score-mark gnus-expirable-mark
6287            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6288            gnus-duplicate-mark gnus-souped-mark)
6289      'reverse)))
6290
6291 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6292 (make-obsolete 'gnus-summary-delete-marked-with
6293                'gnus-summary-limit-exlude-marks)
6294
6295 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6296   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6297 If REVERSE, limit the summary buffer to articles that are marked
6298 with MARKS.  MARKS can either be a string of marks or a list of marks.
6299 Returns how many articles were removed."
6300   (interactive "sMarks: ")
6301   (gnus-summary-limit-to-marks marks t))
6302
6303 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6304   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6305 If REVERSE (the prefix), limit the summary buffer to articles that are
6306 not marked with MARKS.  MARKS can either be a string of marks or a
6307 list of marks.
6308 Returns how many articles were removed."
6309   (interactive "sMarks: \nP")
6310   (prog1
6311       (let ((data gnus-newsgroup-data)
6312             (marks (if (listp marks) marks
6313                      (append marks nil))) ; Transform to list.
6314             articles)
6315         (while data
6316           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6317                   (memq (gnus-data-mark (car data)) marks))
6318             (push (gnus-data-number (car data)) articles))
6319           (setq data (cdr data)))
6320         (gnus-summary-limit articles))
6321     (gnus-summary-position-point)))
6322
6323 (defun gnus-summary-limit-to-score (&optional score)
6324   "Limit to articles with score at or above SCORE."
6325   (interactive "P")
6326   (setq score (if score
6327                   (prefix-numeric-value score)
6328                 (or gnus-summary-default-score 0)))
6329   (let ((data gnus-newsgroup-data)
6330         articles)
6331     (while data
6332       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6333                 score)
6334         (push (gnus-data-number (car data)) articles))
6335       (setq data (cdr data)))
6336     (prog1
6337         (gnus-summary-limit articles)
6338       (gnus-summary-position-point))))
6339
6340 (defun gnus-summary-limit-include-thread (id)
6341   "Display all the hidden articles that in the current thread."
6342   (interactive (list (mail-header-id (gnus-summary-article-header))))
6343   (let ((articles (gnus-articles-in-thread
6344                    (gnus-id-to-thread (gnus-root-id id)))))
6345     (prog1
6346         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6347       (gnus-summary-position-point))))
6348
6349 (defun gnus-summary-limit-include-dormant ()
6350   "Display all the hidden articles that are marked as dormant.
6351 Note that this command only works on a subset of the articles currently
6352 fetched for this group."
6353   (interactive)
6354   (unless gnus-newsgroup-dormant
6355     (error "There are no dormant articles in this group"))
6356   (prog1
6357       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6358     (gnus-summary-position-point)))
6359
6360 (defun gnus-summary-limit-exclude-dormant ()
6361   "Hide all dormant articles."
6362   (interactive)
6363   (prog1
6364       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6365     (gnus-summary-position-point)))
6366
6367 (defun gnus-summary-limit-exclude-childless-dormant ()
6368   "Hide all dormant articles that have no children."
6369   (interactive)
6370   (let ((data (gnus-data-list t))
6371         articles d children)
6372     ;; Find all articles that are either not dormant or have
6373     ;; children.
6374     (while (setq d (pop data))
6375       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6376                 (and (setq children
6377                            (gnus-article-children (gnus-data-number d)))
6378                      (let (found)
6379                        (while children
6380                          (when (memq (car children) articles)
6381                            (setq children nil
6382                                  found t))
6383                          (pop children))
6384                        found)))
6385         (push (gnus-data-number d) articles)))
6386     ;; Do the limiting.
6387     (prog1
6388         (gnus-summary-limit articles)
6389       (gnus-summary-position-point))))
6390
6391 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6392   "Mark all unread excluded articles as read.
6393 If ALL, mark even excluded ticked and dormants as read."
6394   (interactive "P")
6395   (let ((articles (gnus-sorted-complement
6396                    (sort
6397                     (mapcar (lambda (h) (mail-header-number h))
6398                             gnus-newsgroup-headers)
6399                     '<)
6400                    (sort gnus-newsgroup-limit '<)))
6401         article)
6402     (setq gnus-newsgroup-unreads
6403           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6404     (if all
6405         (setq gnus-newsgroup-dormant nil
6406               gnus-newsgroup-marked nil
6407               gnus-newsgroup-reads
6408               (nconc
6409                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6410                gnus-newsgroup-reads))
6411       (while (setq article (pop articles))
6412         (unless (or (memq article gnus-newsgroup-dormant)
6413                     (memq article gnus-newsgroup-marked))
6414           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6415
6416 (defun gnus-summary-limit (articles &optional pop)
6417   (if pop
6418       ;; We pop the previous limit off the stack and use that.
6419       (setq articles (car gnus-newsgroup-limits)
6420             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6421     ;; We use the new limit, so we push the old limit on the stack.
6422     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6423   ;; Set the limit.
6424   (setq gnus-newsgroup-limit articles)
6425   (let ((total (length gnus-newsgroup-data))
6426         (data (gnus-data-find-list (gnus-summary-article-number)))
6427         (gnus-summary-mark-below nil)   ; Inhibit this.
6428         found)
6429     ;; This will do all the work of generating the new summary buffer
6430     ;; according to the new limit.
6431     (gnus-summary-prepare)
6432     ;; Hide any threads, possibly.
6433     (and gnus-show-threads
6434          gnus-thread-hide-subtree
6435          (gnus-summary-hide-all-threads))
6436     ;; Try to return to the article you were at, or one in the
6437     ;; neighborhood.
6438     (when data
6439       ;; We try to find some article after the current one.
6440       (while data
6441         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6442           (setq data nil
6443                 found t))
6444         (setq data (cdr data))))
6445     (unless found
6446       ;; If there is no data, that means that we were after the last
6447       ;; article.  The same goes when we can't find any articles
6448       ;; after the current one.
6449       (goto-char (point-max))
6450       (gnus-summary-find-prev))
6451     (gnus-set-mode-line 'summary)
6452     ;; We return how many articles were removed from the summary
6453     ;; buffer as a result of the new limit.
6454     (- total (length gnus-newsgroup-data))))
6455
6456 (defsubst gnus-invisible-cut-children (threads)
6457   (let ((num 0))
6458     (while threads
6459       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6460         (incf num))
6461       (pop threads))
6462     (< num 2)))
6463
6464 (defsubst gnus-cut-thread (thread)
6465   "Go forwards in the thread until we find an article that we want to display."
6466   (when (or (eq gnus-fetch-old-headers 'some)
6467             (eq gnus-fetch-old-headers 'invisible)
6468             (numberp gnus-fetch-old-headers)
6469             (eq gnus-build-sparse-threads 'some)
6470             (eq gnus-build-sparse-threads 'more))
6471     ;; Deal with old-fetched headers and sparse threads.
6472     (while (and
6473             thread
6474             (or
6475              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6476              (gnus-summary-article-ancient-p
6477               (mail-header-number (car thread))))
6478             (if (or (<= (length (cdr thread)) 1)
6479                     (eq gnus-fetch-old-headers 'invisible))
6480                 (setq gnus-newsgroup-limit
6481                       (delq (mail-header-number (car thread))
6482                             gnus-newsgroup-limit)
6483                       thread (cadr thread))
6484               (when (gnus-invisible-cut-children (cdr thread))
6485                 (let ((th (cdr thread)))
6486                   (while th
6487                     (if (memq (mail-header-number (caar th))
6488                               gnus-newsgroup-limit)
6489                         (setq thread (car th)
6490                               th nil)
6491                       (setq th (cdr th))))))))))
6492   thread)
6493
6494 (defun gnus-cut-threads (threads)
6495   "Cut off all uninteresting articles from the beginning of threads."
6496   (when (or (eq gnus-fetch-old-headers 'some)
6497             (eq gnus-fetch-old-headers 'invisible)
6498             (numberp gnus-fetch-old-headers)
6499             (eq gnus-build-sparse-threads 'some)
6500             (eq gnus-build-sparse-threads 'more))
6501     (let ((th threads))
6502       (while th
6503         (setcar th (gnus-cut-thread (car th)))
6504         (setq th (cdr th)))))
6505   ;; Remove nixed out threads.
6506   (delq nil threads))
6507
6508 (defun gnus-summary-initial-limit (&optional show-if-empty)
6509   "Figure out what the initial limit is supposed to be on group entry.
6510 This entails weeding out unwanted dormants, low-scored articles,
6511 fetch-old-headers verbiage, and so on."
6512   ;; Most groups have nothing to remove.
6513   (if (or gnus-inhibit-limiting
6514           (and (null gnus-newsgroup-dormant)
6515                (not (eq gnus-fetch-old-headers 'some))
6516                (not (numberp gnus-fetch-old-headers))
6517                (not (eq gnus-fetch-old-headers 'invisible))
6518                (null gnus-summary-expunge-below)
6519                (not (eq gnus-build-sparse-threads 'some))
6520                (not (eq gnus-build-sparse-threads 'more))
6521                (null gnus-thread-expunge-below)
6522                (not gnus-use-nocem)))
6523       ()                                ; Do nothing.
6524     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6525     (setq gnus-newsgroup-limit nil)
6526     (mapatoms
6527      (lambda (node)
6528        (unless (car (symbol-value node))
6529          ;; These threads have no parents -- they are roots.
6530          (let ((nodes (cdr (symbol-value node)))
6531                thread)
6532            (while nodes
6533              (if (and gnus-thread-expunge-below
6534                       (< (gnus-thread-total-score (car nodes))
6535                          gnus-thread-expunge-below))
6536                  (gnus-expunge-thread (pop nodes))
6537                (setq thread (pop nodes))
6538                (gnus-summary-limit-children thread))))))
6539      gnus-newsgroup-dependencies)
6540     ;; If this limitation resulted in an empty group, we might
6541     ;; pop the previous limit and use it instead.
6542     (when (and (not gnus-newsgroup-limit)
6543                show-if-empty)
6544       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6545     gnus-newsgroup-limit))
6546
6547 (defun gnus-summary-limit-children (thread)
6548   "Return 1 if this subthread is visible and 0 if it is not."
6549   ;; First we get the number of visible children to this thread.  This
6550   ;; is done by recursing down the thread using this function, so this
6551   ;; will really go down to a leaf article first, before slowly
6552   ;; working its way up towards the root.
6553   (when thread
6554     (let ((children
6555            (if (cdr thread)
6556                (apply '+ (mapcar 'gnus-summary-limit-children
6557                                  (cdr thread)))
6558              0))
6559           (number (mail-header-number (car thread)))
6560           score)
6561       (if (and
6562            (not (memq number gnus-newsgroup-marked))
6563            (or
6564             ;; If this article is dormant and has absolutely no visible
6565             ;; children, then this article isn't visible.
6566             (and (memq number gnus-newsgroup-dormant)
6567                  (zerop children))
6568             ;; If this is "fetch-old-headered" and there is no
6569             ;; visible children, then we don't want this article.
6570             (and (or (eq gnus-fetch-old-headers 'some)
6571                      (numberp gnus-fetch-old-headers))
6572                  (gnus-summary-article-ancient-p number)
6573                  (zerop children))
6574             ;; If this is "fetch-old-headered" and `invisible', then
6575             ;; we don't want this article.
6576             (and (eq gnus-fetch-old-headers 'invisible)
6577                  (gnus-summary-article-ancient-p number))
6578             ;; If this is a sparsely inserted article with no children,
6579             ;; we don't want it.
6580             (and (eq gnus-build-sparse-threads 'some)
6581                  (gnus-summary-article-sparse-p number)
6582                  (zerop children))
6583             ;; If we use expunging, and this article is really
6584             ;; low-scored, then we don't want this article.
6585             (when (and gnus-summary-expunge-below
6586                        (< (setq score
6587                                 (or (cdr (assq number gnus-newsgroup-scored))
6588                                     gnus-summary-default-score))
6589                           gnus-summary-expunge-below))
6590               ;; We increase the expunge-tally here, but that has
6591               ;; nothing to do with the limits, really.
6592               (incf gnus-newsgroup-expunged-tally)
6593               ;; We also mark as read here, if that's wanted.
6594               (when (and gnus-summary-mark-below
6595                          (< score gnus-summary-mark-below))
6596                 (setq gnus-newsgroup-unreads
6597                       (delq number gnus-newsgroup-unreads))
6598                 (if gnus-newsgroup-auto-expire
6599                     (push number gnus-newsgroup-expirable)
6600                   (push (cons number gnus-low-score-mark)
6601                         gnus-newsgroup-reads)))
6602               t)
6603             ;; Check NoCeM things.
6604             (if (and gnus-use-nocem
6605                      (gnus-nocem-unwanted-article-p
6606                       (mail-header-id (car thread))))
6607                 (progn
6608                   (setq gnus-newsgroup-unreads
6609                         (delq number gnus-newsgroup-unreads))
6610                   t))))
6611           ;; Nope, invisible article.
6612           0
6613         ;; Ok, this article is to be visible, so we add it to the limit
6614         ;; and return 1.
6615         (push number gnus-newsgroup-limit)
6616         1))))
6617
6618 (defun gnus-expunge-thread (thread)
6619   "Mark all articles in THREAD as read."
6620   (let* ((number (mail-header-number (car thread))))
6621     (incf gnus-newsgroup-expunged-tally)
6622     ;; We also mark as read here, if that's wanted.
6623     (setq gnus-newsgroup-unreads
6624           (delq number gnus-newsgroup-unreads))
6625     (if gnus-newsgroup-auto-expire
6626         (push number gnus-newsgroup-expirable)
6627       (push (cons number gnus-low-score-mark)
6628             gnus-newsgroup-reads)))
6629   ;; Go recursively through all subthreads.
6630   (mapcar 'gnus-expunge-thread (cdr thread)))
6631
6632 ;; Summary article oriented commands
6633
6634 (defun gnus-summary-refer-parent-article (n)
6635   "Refer parent article N times.
6636 If N is negative, go to ancestor -N instead.
6637 The difference between N and the number of articles fetched is returned."
6638   (interactive "p")
6639   (let ((skip 1)
6640         error header ref)
6641     (when (not (natnump n))
6642       (setq skip (abs n)
6643             n 1))
6644     (while (and (> n 0)
6645                 (not error))
6646       (setq header (gnus-summary-article-header))
6647       (if (and (eq (mail-header-number header)
6648                    (cdr gnus-article-current))
6649                (equal gnus-newsgroup-name
6650                       (car gnus-article-current)))
6651           ;; If we try to find the parent of the currently
6652           ;; displayed article, then we take a look at the actual
6653           ;; References header, since this is slightly more
6654           ;; reliable than the References field we got from the
6655           ;; server.
6656           (save-excursion
6657             (set-buffer gnus-original-article-buffer)
6658             (nnheader-narrow-to-headers)
6659             (unless (setq ref (message-fetch-field "references"))
6660               (setq ref (message-fetch-field "in-reply-to")))
6661             (widen))
6662         (setq ref
6663               ;; It's not the current article, so we take a bet on
6664               ;; the value we got from the server.
6665               (mail-header-references header)))
6666       (if (and ref
6667                (not (equal ref "")))
6668           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6669             (gnus-message 1 "Couldn't find parent"))
6670         (gnus-message 1 "No references in article %d"
6671                       (gnus-summary-article-number))
6672         (setq error t))
6673       (decf n))
6674     (gnus-summary-position-point)
6675     n))
6676
6677 (defun gnus-summary-refer-references ()
6678   "Fetch all articles mentioned in the References header.
6679 Return the number of articles fetched."
6680   (interactive)
6681   (let ((ref (mail-header-references (gnus-summary-article-header)))
6682         (current (gnus-summary-article-number))
6683         (n 0))
6684     (if (or (not ref)
6685             (equal ref ""))
6686         (error "No References in the current article")
6687       ;; For each Message-ID in the References header...
6688       (while (string-match "<[^>]*>" ref)
6689         (incf n)
6690         ;; ... fetch that article.
6691         (gnus-summary-refer-article
6692          (prog1 (match-string 0 ref)
6693            (setq ref (substring ref (match-end 0))))))
6694       (gnus-summary-goto-subject current)
6695       (gnus-summary-position-point)
6696       n)))
6697
6698 (defun gnus-summary-refer-thread (&optional limit)
6699   "Fetch all articles in the current thread.
6700 If LIMIT (the numerical prefix), fetch that many old headers instead
6701 of what's specified by the `gnus-refer-thread-limit' variable."
6702   (interactive "P")
6703   (let ((id (mail-header-id (gnus-summary-article-header)))
6704         (limit (if limit (prefix-numeric-value limit)
6705                  gnus-refer-thread-limit)))
6706     ;; We want to fetch LIMIT *old* headers, but we also have to
6707     ;; re-fetch all the headers in the current buffer, because many of
6708     ;; them may be undisplayed.  So we adjust LIMIT.
6709     (when (numberp limit)
6710       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6711     (unless (eq gnus-fetch-old-headers 'invisible)
6712       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6713       ;; Retrieve the headers and read them in.
6714       (if (eq (gnus-retrieve-headers
6715                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6716               'nov)
6717           (gnus-build-all-threads)
6718         (error "Can't fetch thread from backends that don't support NOV"))
6719       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6720     (gnus-summary-limit-include-thread id)))
6721
6722 (defun gnus-summary-refer-article (message-id &optional arg)
6723   "Fetch an article specified by MESSAGE-ID.
6724 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6725 or `gnus-select-method', no matter what backend the article comes from."
6726   (interactive "sMessage-ID: \nP")
6727   (when (and (stringp message-id)
6728              (not (zerop (length message-id))))
6729     ;; Construct the correct Message-ID if necessary.
6730     ;; Suggested by tale@pawl.rpi.edu.
6731     (unless (string-match "^<" message-id)
6732       (setq message-id (concat "<" message-id)))
6733     (unless (string-match ">$" message-id)
6734       (setq message-id (concat message-id ">")))
6735     (let* ((header (gnus-id-to-header message-id))
6736            (sparse (and header
6737                         (gnus-summary-article-sparse-p
6738                          (mail-header-number header))
6739                         (memq (mail-header-number header)
6740                               gnus-newsgroup-limit))))
6741       (cond
6742        ;; If the article is present in the buffer we just go to it.
6743        ((and header
6744              (or (not (gnus-summary-article-sparse-p
6745                        (mail-header-number header)))
6746                  sparse))
6747         (prog1
6748             (gnus-summary-goto-article
6749              (mail-header-number header) nil t)
6750           (when sparse
6751             (gnus-summary-update-article (mail-header-number header)))))
6752        (t
6753         ;; We fetch the article
6754         (let ((gnus-override-method
6755                (cond ((gnus-news-group-p gnus-newsgroup-name)
6756                       gnus-refer-article-method)
6757                      (arg
6758                       (or gnus-refer-article-method gnus-select-method))
6759                      (t nil)))
6760               number)
6761           ;; Start the special refer-article method, if necessary.
6762           (when (and gnus-refer-article-method
6763                      (gnus-news-group-p gnus-newsgroup-name))
6764             (gnus-check-server gnus-refer-article-method))
6765           ;; Fetch the header, and display the article.
6766           (if (setq number (gnus-summary-insert-subject message-id))
6767               (gnus-summary-select-article nil nil nil number)
6768             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6769
6770 (defun gnus-summary-edit-parameters ()
6771   "Edit the group parameters of the current group."
6772   (interactive)
6773   (gnus-group-edit-group gnus-newsgroup-name 'params))
6774
6775 (defun gnus-summary-customize-parameters ()
6776   "Customize the group parameters of the current group."
6777   (interactive)
6778   (gnus-group-customize gnus-newsgroup-name))
6779
6780 (defun gnus-summary-enter-digest-group (&optional force)
6781   "Enter an nndoc group based on the current article.
6782 If FORCE, force a digest interpretation.  If not, try
6783 to guess what the document format is."
6784   (interactive "P")
6785   (let ((conf gnus-current-window-configuration))
6786     (save-excursion
6787       (gnus-summary-select-article))
6788     (setq gnus-current-window-configuration conf)
6789     (let* ((name (format "%s-%d"
6790                          (gnus-group-prefixed-name
6791                           gnus-newsgroup-name (list 'nndoc ""))
6792                          (save-excursion
6793                            (set-buffer gnus-summary-buffer)
6794                            gnus-current-article)))
6795            (ogroup gnus-newsgroup-name)
6796            (params (append (gnus-info-params (gnus-get-info ogroup))
6797                            (list (cons 'to-group ogroup))
6798                            (list (cons 'save-article-group ogroup))))
6799            (case-fold-search t)
6800            (buf (current-buffer))
6801            dig)
6802       (save-excursion
6803         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6804         (insert-buffer-substring gnus-original-article-buffer)
6805         ;; Remove lines that may lead nndoc to misinterpret the
6806         ;; document type.
6807         (narrow-to-region
6808          (goto-char (point-min))
6809          (or (search-forward "\n\n" nil t) (point)))
6810         (goto-char (point-min))
6811         (delete-matching-lines "^Path:\\|^From ")
6812         (widen))
6813       (unwind-protect
6814           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6815                     (gnus-newsgroup-ephemeral-ignored-charsets
6816                      gnus-newsgroup-ignored-charsets))
6817                 (gnus-group-read-ephemeral-group
6818                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6819                               (nndoc-article-type
6820                                ,(if force 'mbox 'guess))) t))
6821               ;; Make all postings to this group go to the parent group.
6822               (nconc (gnus-info-params (gnus-get-info name))
6823                      params)
6824             ;; Couldn't select this doc group.
6825             (switch-to-buffer buf)
6826             (gnus-set-global-variables)
6827             (gnus-configure-windows 'summary)
6828             (gnus-message 3 "Article couldn't be entered?"))
6829         (kill-buffer dig)))))
6830
6831 (defun gnus-summary-read-document (n)
6832   "Open a new group based on the current article(s).
6833 This will allow you to read digests and other similar
6834 documents as newsgroups.
6835 Obeys the standard process/prefix convention."
6836   (interactive "P")
6837   (let* ((articles (gnus-summary-work-articles n))
6838          (ogroup gnus-newsgroup-name)
6839          (params (append (gnus-info-params (gnus-get-info ogroup))
6840                          (list (cons 'to-group ogroup))))
6841          article group egroup groups vgroup)
6842     (while (setq article (pop articles))
6843       (setq group (format "%s-%d" gnus-newsgroup-name article))
6844       (gnus-summary-remove-process-mark article)
6845       (when (gnus-summary-display-article article)
6846         (save-excursion
6847           (with-temp-buffer
6848             (insert-buffer-substring gnus-original-article-buffer)
6849             ;; Remove some headers that may lead nndoc to make
6850             ;; the wrong guess.
6851             (message-narrow-to-head)
6852             (goto-char (point-min))
6853             (delete-matching-lines "^\\(Path\\):\\|^From ")
6854             (widen)
6855             (if (setq egroup
6856                       (gnus-group-read-ephemeral-group
6857                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6858                                      (nndoc-article-type guess))
6859                        t nil t))
6860                 (progn
6861                   ;; Make all postings to this group go to the parent group.
6862                   (nconc (gnus-info-params (gnus-get-info egroup))
6863                          params)
6864                   (push egroup groups))
6865               ;; Couldn't select this doc group.
6866               (gnus-error 3 "Article couldn't be entered"))))))
6867     ;; Now we have selected all the documents.
6868     (cond
6869      ((not groups)
6870       (error "None of the articles could be interpreted as documents"))
6871      ((gnus-group-read-ephemeral-group
6872        (setq vgroup (format
6873                      "nnvirtual:%s-%s" gnus-newsgroup-name
6874                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6875        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6876        t
6877        (cons (current-buffer) 'summary)))
6878      (t
6879       (error "Couldn't select virtual nndoc group")))))
6880
6881 (defun gnus-summary-isearch-article (&optional regexp-p)
6882   "Do incremental search forward on the current article.
6883 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6884   (interactive "P")
6885   (gnus-summary-select-article)
6886   (gnus-configure-windows 'article)
6887   (gnus-eval-in-buffer-window gnus-article-buffer
6888     (save-restriction
6889       (widen)
6890       (isearch-forward regexp-p))))
6891
6892 (defun gnus-summary-search-article-forward (regexp &optional backward)
6893   "Search for an article containing REGEXP forward.
6894 If BACKWARD, search backward instead."
6895   (interactive
6896    (list (read-string
6897           (format "Search article %s (regexp%s): "
6898                   (if current-prefix-arg "backward" "forward")
6899                   (if gnus-last-search-regexp
6900                       (concat ", default " gnus-last-search-regexp)
6901                     "")))
6902          current-prefix-arg))
6903   (if (string-equal regexp "")
6904       (setq regexp (or gnus-last-search-regexp ""))
6905     (setq gnus-last-search-regexp regexp))
6906   (if (gnus-summary-search-article regexp backward)
6907       (gnus-summary-show-thread)
6908     (error "Search failed: \"%s\"" regexp)))
6909
6910 (defun gnus-summary-search-article-backward (regexp)
6911   "Search for an article containing REGEXP backward."
6912   (interactive
6913    (list (read-string
6914           (format "Search article backward (regexp%s): "
6915                   (if gnus-last-search-regexp
6916                       (concat ", default " gnus-last-search-regexp)
6917                     "")))))
6918   (gnus-summary-search-article-forward regexp 'backward))
6919
6920 (defun gnus-summary-search-article (regexp &optional backward)
6921   "Search for an article containing REGEXP.
6922 Optional argument BACKWARD means do search for backward.
6923 `gnus-select-article-hook' is not called during the search."
6924   ;; We have to require this here to make sure that the following
6925   ;; dynamic binding isn't shadowed by autoloading.
6926   (require 'gnus-async)
6927   (require 'gnus-art)
6928   (let ((gnus-select-article-hook nil)  ;Disable hook.
6929         (gnus-article-prepare-hook nil)
6930         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6931         (gnus-use-article-prefetch nil)
6932         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6933         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6934         (sum (current-buffer))
6935         (gnus-display-mime-function nil)
6936         (found nil)
6937         point)
6938     (gnus-save-hidden-threads
6939       (gnus-summary-select-article)
6940       (set-buffer gnus-article-buffer)
6941       (when backward
6942         (forward-line -1))
6943       (while (not found)
6944         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6945         (if (if backward
6946                 (re-search-backward regexp nil t)
6947               (re-search-forward regexp nil t))
6948             ;; We found the regexp.
6949             (progn
6950               (setq found 'found)
6951               (beginning-of-line)
6952               (set-window-start
6953                (get-buffer-window (current-buffer))
6954                (point))
6955               (forward-line 1)
6956               (set-window-point
6957                (get-buffer-window (current-buffer))
6958                (point))
6959               (set-buffer sum)
6960               (setq point (point)))
6961           ;; We didn't find it, so we go to the next article.
6962           (set-buffer sum)
6963           (setq found 'not)
6964           (while (eq found 'not)
6965             (if (not (if backward (gnus-summary-find-prev)
6966                        (gnus-summary-find-next)))
6967                 ;; No more articles.
6968                 (setq found t)
6969               ;; Select the next article and adjust point.
6970               (unless (gnus-summary-article-sparse-p
6971                        (gnus-summary-article-number))
6972                 (setq found nil)
6973                 (gnus-summary-select-article)
6974                 (set-buffer gnus-article-buffer)
6975                 (widen)
6976                 (goto-char (if backward (point-max) (point-min))))))))
6977       (gnus-message 7 ""))
6978     ;; Return whether we found the regexp.
6979     (when (eq found 'found)
6980       (goto-char point)
6981       (gnus-summary-show-thread)
6982       (gnus-summary-goto-subject gnus-current-article)
6983       (gnus-summary-position-point)
6984       t)))
6985
6986 (defun gnus-summary-find-matching (header regexp &optional backward unread
6987                                           not-case-fold)
6988   "Return a list of all articles that match REGEXP on HEADER.
6989 The search stars on the current article and goes forwards unless
6990 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6991 If UNREAD is non-nil, only unread articles will
6992 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6993 in the comparisons."
6994   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6995                 (gnus-data-find-list
6996                  (gnus-summary-article-number) (gnus-data-list backward))))
6997         (case-fold-search (not not-case-fold))
6998         articles d func)
6999     (if (consp header)
7000         (if (eq (car header) 'extra)
7001             (setq func
7002                   `(lambda (h)
7003                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7004                          "")))
7005           (error "%s is an invalid header" header))
7006       (unless (fboundp (intern (concat "mail-header-" header)))
7007         (error "%s is not a valid header" header))
7008       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7009     (while data
7010       (setq d (car data))
7011       (and (or (not unread)             ; We want all articles...
7012                (gnus-data-unread-p d))  ; Or just unreads.
7013            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7014            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7015            (push (gnus-data-number d) articles)) ; Success!
7016       (setq data (cdr data)))
7017     (nreverse articles)))
7018
7019 (defun gnus-summary-execute-command (header regexp command &optional backward)
7020   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7021 If HEADER is an empty string (or nil), the match is done on the entire
7022 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7023   (interactive
7024    (list (let ((completion-ignore-case t))
7025            (completing-read
7026             "Header name: "
7027             (mapcar (lambda (string) (list string))
7028                     '("Number" "Subject" "From" "Lines" "Date"
7029                       "Message-ID" "Xref" "References" "Body"))
7030             nil 'require-match))
7031          (read-string "Regexp: ")
7032          (read-key-sequence "Command: ")
7033          current-prefix-arg))
7034   (when (equal header "Body")
7035     (setq header ""))
7036   ;; Hidden thread subtrees must be searched as well.
7037   (gnus-summary-show-all-threads)
7038   ;; We don't want to change current point nor window configuration.
7039   (save-excursion
7040     (save-window-excursion
7041       (gnus-message 6 "Executing %s..." (key-description command))
7042       ;; We'd like to execute COMMAND interactively so as to give arguments.
7043       (gnus-execute header regexp
7044                     `(call-interactively ',(key-binding command))
7045                     backward)
7046       (gnus-message 6 "Executing %s...done" (key-description command)))))
7047
7048 (defun gnus-summary-beginning-of-article ()
7049   "Scroll the article back to the beginning."
7050   (interactive)
7051   (gnus-summary-select-article)
7052   (gnus-configure-windows 'article)
7053   (gnus-eval-in-buffer-window gnus-article-buffer
7054     (widen)
7055     (goto-char (point-min))
7056     (when gnus-page-broken
7057       (gnus-narrow-to-page))))
7058
7059 (defun gnus-summary-end-of-article ()
7060   "Scroll to the end of the article."
7061   (interactive)
7062   (gnus-summary-select-article)
7063   (gnus-configure-windows 'article)
7064   (gnus-eval-in-buffer-window gnus-article-buffer
7065     (widen)
7066     (goto-char (point-max))
7067     (recenter -3)
7068     (when gnus-page-broken
7069       (gnus-narrow-to-page))))
7070
7071 (defun gnus-summary-print-article (&optional filename n)
7072   "Generate and print a PostScript image of the N next (mail) articles.
7073
7074 If N is negative, print the N previous articles.  If N is nil and articles
7075 have been marked with the process mark, print these instead.
7076
7077 If the optional first argument FILENAME is nil, send the image to the
7078 printer.  If FILENAME is a string, save the PostScript image in a file with
7079 that name.  If FILENAME is a number, prompt the user for the name of the file
7080 to save in."
7081   (interactive (list (ps-print-preprint current-prefix-arg)
7082                      current-prefix-arg))
7083   (dolist (article (gnus-summary-work-articles n))
7084     (gnus-summary-select-article nil nil 'pseudo article)
7085     (gnus-eval-in-buffer-window gnus-article-buffer
7086       (let ((buffer (generate-new-buffer " *print*")))
7087         (unwind-protect
7088             (progn
7089               (copy-to-buffer buffer (point-min) (point-max))
7090               (set-buffer buffer)
7091               (gnus-article-delete-invisible-text)
7092               (let ((ps-left-header
7093                      (list
7094                       (concat "("
7095                               (mail-header-subject gnus-current-headers) ")")
7096                       (concat "("
7097                               (mail-header-from gnus-current-headers) ")")))
7098                     (ps-right-header
7099                      (list
7100                       "/pagenumberstring load"
7101                       (concat "("
7102                               (mail-header-date gnus-current-headers) ")"))))
7103                 (gnus-run-hooks 'gnus-ps-print-hook)
7104                 (save-excursion
7105                   (ps-print-buffer-with-faces filename))))
7106           (kill-buffer buffer))))))
7107
7108 (defun gnus-summary-show-article (&optional arg)
7109   "Force re-fetching of the current article.
7110 If ARG (the prefix) is non-nil, show the raw article without any
7111 article massaging functions being run."
7112   (interactive "P")
7113   (if (not arg)
7114       ;; Select the article the normal way.
7115       (gnus-summary-select-article nil 'force)
7116     ;; We have to require this here to make sure that the following
7117     ;; dynamic binding isn't shadowed by autoloading.
7118     (require 'gnus-async)
7119     (require 'gnus-art)
7120     ;; Bind the article treatment functions to nil.
7121     (let ((gnus-have-all-headers t)
7122           gnus-article-prepare-hook
7123           gnus-article-decode-hook
7124           gnus-display-mime-function
7125           gnus-break-pages
7126           gnus-visual)
7127       ;; Destroy any MIME parts.
7128       (when (gnus-buffer-live-p gnus-article-buffer)
7129         (save-excursion
7130           (set-buffer gnus-article-buffer)
7131           (mm-destroy-parts gnus-article-mime-handles)))
7132       (gnus-summary-select-article nil 'force)))
7133   (gnus-summary-goto-subject gnus-current-article)
7134   (gnus-summary-position-point))
7135
7136 (defun gnus-summary-verbose-headers (&optional arg)
7137   "Toggle permanent full header display.
7138 If ARG is a positive number, turn header display on.
7139 If ARG is a negative number, turn header display off."
7140   (interactive "P")
7141   (setq gnus-show-all-headers
7142         (cond ((or (not (numberp arg))
7143                    (zerop arg))
7144                (not gnus-show-all-headers))
7145               ((natnump arg)
7146                t)))
7147   (gnus-summary-show-article))
7148
7149 (defun gnus-summary-toggle-header (&optional arg)
7150   "Show the headers if they are hidden, or hide them if they are shown.
7151 If ARG is a positive number, show the entire header.
7152 If ARG is a negative number, hide the unwanted header lines."
7153   (interactive "P")
7154   (save-excursion
7155     (set-buffer gnus-article-buffer)
7156     (save-restriction
7157       (let* ((buffer-read-only nil)
7158              (inhibit-point-motion-hooks t)
7159              hidden e)
7160         (setq hidden
7161               (if (numberp arg)
7162                   (>= arg 0)
7163                 (save-restriction 
7164                   (article-narrow-to-head)
7165                   (gnus-article-hidden-text-p 'headers))))
7166         (goto-char (point-min))
7167         (when (search-forward "\n\n" nil t)
7168           (delete-region (point-min) (1- (point))))
7169         (goto-char (point-min))
7170         (save-excursion
7171           (set-buffer gnus-original-article-buffer)
7172           (goto-char (point-min))
7173           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7174         (insert-buffer-substring gnus-original-article-buffer 1 e)
7175         (save-restriction
7176           (narrow-to-region (point-min) (point))
7177           (article-decode-encoded-words)
7178           (if  hidden
7179               (let ((gnus-treat-hide-headers nil)
7180                     (gnus-treat-hide-boring-headers nil))
7181                 (gnus-treat-article 'head))
7182             (gnus-treat-article 'head)))))))
7183
7184 (defun gnus-summary-show-all-headers ()
7185   "Make all header lines visible."
7186   (interactive)
7187   (gnus-article-show-all-headers))
7188
7189 (defun gnus-summary-caesar-message (&optional arg)
7190   "Caesar rotate the current article by 13.
7191 The numerical prefix specifies how many places to rotate each letter
7192 forward."
7193   (interactive "P")
7194   (gnus-summary-select-article)
7195   (let ((mail-header-separator ""))
7196     (gnus-eval-in-buffer-window gnus-article-buffer
7197       (save-restriction
7198         (widen)
7199         (let ((start (window-start))
7200               buffer-read-only)
7201           (message-caesar-buffer-body arg)
7202           (set-window-start (get-buffer-window (current-buffer)) start))))))
7203
7204 (defun gnus-summary-stop-page-breaking ()
7205   "Stop page breaking in the current article."
7206   (interactive)
7207   (gnus-summary-select-article)
7208   (gnus-eval-in-buffer-window gnus-article-buffer
7209     (widen)
7210     (when (gnus-visual-p 'page-marker)
7211       (let ((buffer-read-only nil))
7212         (gnus-remove-text-with-property 'gnus-prev)
7213         (gnus-remove-text-with-property 'gnus-next))
7214       (setq gnus-page-broken nil))))
7215
7216 (defun gnus-summary-move-article (&optional n to-newsgroup
7217                                             select-method action)
7218   "Move the current article to a different newsgroup.
7219 If N is a positive number, move the N next articles.
7220 If N is a negative number, move the N previous articles.
7221 If N is nil and any articles have been marked with the process mark,
7222 move those articles instead.
7223 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7224 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7225 re-spool using this method.
7226
7227 For this function to work, both the current newsgroup and the
7228 newsgroup that you want to move to have to support the `request-move'
7229 and `request-accept' functions."
7230   (interactive "P")
7231   (unless action
7232     (setq action 'move))
7233   ;; Disable marking as read.
7234   (let (gnus-mark-article-hook)
7235     (save-window-excursion
7236       (gnus-summary-select-article)))
7237   ;; Check whether the source group supports the required functions.
7238   (cond ((and (eq action 'move)
7239               (not (gnus-check-backend-function
7240                     'request-move-article gnus-newsgroup-name)))
7241          (error "The current group does not support article moving"))
7242         ((and (eq action 'crosspost)
7243               (not (gnus-check-backend-function
7244                     'request-replace-article gnus-newsgroup-name)))
7245          (error "The current group does not support article editing")))
7246   (let ((articles (gnus-summary-work-articles n))
7247         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7248         (names '((move "Move" "Moving")
7249                  (copy "Copy" "Copying")
7250                  (crosspost "Crosspost" "Crossposting")))
7251         (copy-buf (save-excursion
7252                     (nnheader-set-temp-buffer " *copy article*")))
7253         art-group to-method new-xref article to-groups)
7254     (unless (assq action names)
7255       (error "Unknown action %s" action))
7256     ;; Read the newsgroup name.
7257     (when (and (not to-newsgroup)
7258                (not select-method))
7259       (setq to-newsgroup
7260             (gnus-read-move-group-name
7261              (cadr (assq action names))
7262              (symbol-value (intern (format "gnus-current-%s-group" action)))
7263              articles prefix))
7264       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7265     (setq to-method (or select-method
7266                         (gnus-group-name-to-method to-newsgroup)))
7267     ;; Check the method we are to move this article to...
7268     (unless (gnus-check-backend-function
7269              'request-accept-article (car to-method))
7270       (error "%s does not support article copying" (car to-method)))
7271     (unless (gnus-check-server to-method)
7272       (error "Can't open server %s" (car to-method)))
7273     (gnus-message 6 "%s to %s: %s..."
7274                   (caddr (assq action names))
7275                   (or (car select-method) to-newsgroup) articles)
7276     (while articles
7277       (setq article (pop articles))
7278       (setq
7279        art-group
7280        (cond
7281         ;; Move the article.
7282         ((eq action 'move)
7283          ;; Remove this article from future suppression.
7284          (gnus-dup-unsuppress-article article)
7285          (gnus-request-move-article
7286           article                       ; Article to move
7287           gnus-newsgroup-name           ; From newsgroup
7288           (nth 1 (gnus-find-method-for-group
7289                   gnus-newsgroup-name)) ; Server
7290           (list 'gnus-request-accept-article
7291                 to-newsgroup (list 'quote select-method)
7292                 (not articles) t)               ; Accept form
7293           (not articles)))              ; Only save nov last time
7294         ;; Copy the article.
7295         ((eq action 'copy)
7296          (save-excursion
7297            (set-buffer copy-buf)
7298            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7299              (gnus-request-accept-article
7300               to-newsgroup select-method (not articles) t))))
7301         ;; Crosspost the article.
7302         ((eq action 'crosspost)
7303          (let ((xref (message-tokenize-header
7304                       (mail-header-xref (gnus-summary-article-header article))
7305                       " ")))
7306            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7307                                   ":" article))
7308            (unless xref
7309              (setq xref (list (system-name))))
7310            (setq new-xref
7311                  (concat
7312                   (mapconcat 'identity
7313                              (delete "Xref:" (delete new-xref xref))
7314                              " ")
7315                   " " new-xref))
7316            (save-excursion
7317              (set-buffer copy-buf)
7318              ;; First put the article in the destination group.
7319              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7320              (when (consp (setq art-group
7321                                 (gnus-request-accept-article
7322                                  to-newsgroup select-method (not articles))))
7323                (setq new-xref (concat new-xref " " (car art-group)
7324                                       ":" (cdr art-group)))
7325                ;; Now we have the new Xrefs header, so we insert
7326                ;; it and replace the new article.
7327                (nnheader-replace-header "Xref" new-xref)
7328                (gnus-request-replace-article
7329                 (cdr art-group) to-newsgroup (current-buffer))
7330                art-group))))))
7331       (cond
7332        ((not art-group)
7333        (gnus-message 1 "Couldn't %s article %s: %s"
7334                      (cadr (assq action names)) article
7335                      (nnheader-get-report (car to-method))))
7336        ((and (eq art-group 'junk)
7337              (eq action 'move))
7338         (gnus-summary-mark-article article gnus-canceled-mark)
7339         (gnus-message 4 "Deleted article %s" article))
7340        (t
7341         (let* ((pto-group (gnus-group-prefixed-name
7342                            (car art-group) to-method))
7343                (entry
7344                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7345                (info (nth 2 entry))
7346                (to-group (gnus-info-group info))
7347                to-marks)
7348           ;; Update the group that has been moved to.
7349           (when (and info
7350                      (memq action '(move copy)))
7351             (unless (member to-group to-groups)
7352               (push to-group to-groups))
7353
7354             (unless (memq article gnus-newsgroup-unreads)
7355               (push 'read to-marks)
7356               (gnus-info-set-read
7357                info (gnus-add-to-range (gnus-info-read info)
7358                                        (list (cdr art-group)))))
7359
7360             ;; Copy any marks over to the new group.
7361             (let ((marks gnus-article-mark-lists)
7362                   (to-article (cdr art-group)))
7363
7364               ;; See whether the article is to be put in the cache.
7365               (when gnus-use-cache
7366                 (gnus-cache-possibly-enter-article
7367                  to-group to-article
7368                  (memq article gnus-newsgroup-marked)
7369                  (memq article gnus-newsgroup-dormant)
7370                  (memq article gnus-newsgroup-unreads)))
7371
7372               (when (and (equal to-group gnus-newsgroup-name)
7373                          (not (memq article gnus-newsgroup-unreads)))
7374                 ;; Mark this article as read in this group.
7375                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7376                 (setcdr (gnus-active to-group) to-article)
7377                 (setcdr gnus-newsgroup-active to-article))
7378
7379               (while marks
7380                 (when (memq article (symbol-value
7381                                      (intern (format "gnus-newsgroup-%s"
7382                                                      (caar marks)))))
7383                   (push (cdar marks) to-marks)
7384                   ;; If the other group is the same as this group,
7385                   ;; then we have to add the mark to the list.
7386                   (when (equal to-group gnus-newsgroup-name)
7387                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7388                          (cons to-article
7389                                (symbol-value
7390                                 (intern (format "gnus-newsgroup-%s"
7391                                                 (caar marks)))))))
7392                   ;; Copy the marks to other group.
7393                   (gnus-add-marked-articles
7394                    to-group (cdar marks) (list to-article) info))
7395                 (setq marks (cdr marks)))
7396
7397               (gnus-request-set-mark to-group (list (list (list to-article)
7398                                                           'set
7399                                                           to-marks)))
7400
7401               (gnus-dribble-enter
7402                (concat "(gnus-group-set-info '"
7403                        (gnus-prin1-to-string (gnus-get-info to-group))
7404                        ")"))))
7405
7406           ;; Update the Xref header in this article to point to
7407           ;; the new crossposted article we have just created.
7408           (when (eq action 'crosspost)
7409             (save-excursion
7410               (set-buffer copy-buf)
7411               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7412               (nnheader-replace-header "Xref" new-xref)
7413               (gnus-request-replace-article
7414                article gnus-newsgroup-name (current-buffer)))))
7415
7416         ;;;!!!Why is this necessary?
7417         (set-buffer gnus-summary-buffer)
7418
7419         (gnus-summary-goto-subject article)
7420         (when (eq action 'move)
7421           (gnus-summary-mark-article article gnus-canceled-mark))))
7422       (gnus-summary-remove-process-mark article))
7423     ;; Re-activate all groups that have been moved to.
7424     (while to-groups
7425       (save-excursion
7426         (set-buffer gnus-group-buffer)
7427         (when (gnus-group-goto-group (car to-groups) t)
7428           (gnus-group-get-new-news-this-group 1 t))
7429         (pop to-groups)))
7430
7431     (gnus-kill-buffer copy-buf)
7432     (gnus-summary-position-point)
7433     (gnus-set-mode-line 'summary)))
7434
7435 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7436   "Move the current article to a different newsgroup.
7437 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7438 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7439 re-spool using this method."
7440   (interactive "P")
7441   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7442
7443 (defun gnus-summary-crosspost-article (&optional n)
7444   "Crosspost the current article to some other group."
7445   (interactive "P")
7446   (gnus-summary-move-article n nil nil 'crosspost))
7447
7448 (defcustom gnus-summary-respool-default-method nil
7449   "Default method for respooling an article.
7450 If nil, use to the current newsgroup method."
7451   :type '(choice (gnus-select-method :value (nnml ""))
7452                  (const nil))
7453   :group 'gnus-summary-mail)
7454
7455 (defun gnus-summary-respool-article (&optional n method)
7456   "Respool the current article.
7457 The article will be squeezed through the mail spooling process again,
7458 which means that it will be put in some mail newsgroup or other
7459 depending on `nnmail-split-methods'.
7460 If N is a positive number, respool the N next articles.
7461 If N is a negative number, respool the N previous articles.
7462 If N is nil and any articles have been marked with the process mark,
7463 respool those articles instead.
7464
7465 Respooling can be done both from mail groups and \"real\" newsgroups.
7466 In the former case, the articles in question will be moved from the
7467 current group into whatever groups they are destined to.  In the
7468 latter case, they will be copied into the relevant groups."
7469   (interactive
7470    (list current-prefix-arg
7471          (let* ((methods (gnus-methods-using 'respool))
7472                 (methname
7473                  (symbol-name (or gnus-summary-respool-default-method
7474                                   (car (gnus-find-method-for-group
7475                                         gnus-newsgroup-name)))))
7476                 (method
7477                  (gnus-completing-read
7478                   methname "What backend do you want to use when respooling?"
7479                   methods nil t nil 'gnus-mail-method-history))
7480                 ms)
7481            (cond
7482             ((zerop (length (setq ms (gnus-servers-using-backend
7483                                       (intern method)))))
7484              (list (intern method) ""))
7485             ((= 1 (length ms))
7486              (car ms))
7487             (t
7488              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7489                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7490                            ms-alist))))))))
7491   (unless method
7492     (error "No method given for respooling"))
7493   (if (assoc (symbol-name
7494               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7495              (gnus-methods-using 'respool))
7496       (gnus-summary-move-article n nil method)
7497     (gnus-summary-copy-article n nil method)))
7498
7499 (defun gnus-summary-import-article (file)
7500   "Import an arbitrary file into a mail newsgroup."
7501   (interactive "fImport file: ")
7502   (let ((group gnus-newsgroup-name)
7503         (now (current-time))
7504         atts lines)
7505     (unless (gnus-check-backend-function 'request-accept-article group)
7506       (error "%s does not support article importing" group))
7507     (or (file-readable-p file)
7508         (not (file-regular-p file))
7509         (error "Can't read %s" file))
7510     (save-excursion
7511       (set-buffer (gnus-get-buffer-create " *import file*"))
7512       (erase-buffer)
7513       (insert-file-contents file)
7514       (goto-char (point-min))
7515       (unless (nnheader-article-p)
7516         ;; This doesn't look like an article, so we fudge some headers.
7517         (setq atts (file-attributes file)
7518               lines (count-lines (point-min) (point-max)))
7519         (insert "From: " (read-string "From: ") "\n"
7520                 "Subject: " (read-string "Subject: ") "\n"
7521                 "Date: " (message-make-date (nth 5 atts))
7522                 "\n"
7523                 "Message-ID: " (message-make-message-id) "\n"
7524                 "Lines: " (int-to-string lines) "\n"
7525                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7526       (gnus-request-accept-article group nil t)
7527       (kill-buffer (current-buffer)))))
7528
7529 (defun gnus-summary-article-posted-p ()
7530   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7531 This will be the case if the article has both been mailed and posted."
7532   (interactive)
7533   (let ((id (mail-header-references (gnus-summary-article-header)))
7534         (gnus-override-method
7535          (or gnus-refer-article-method gnus-select-method)))
7536     (if (gnus-request-head id "")
7537         (gnus-message 2 "The current message was found on %s"
7538                       gnus-override-method)
7539       (gnus-message 2 "The current message couldn't be found on %s"
7540                     gnus-override-method)
7541       nil)))
7542
7543 (defun gnus-summary-expire-articles (&optional now)
7544   "Expire all articles that are marked as expirable in the current group."
7545   (interactive)
7546   (when (gnus-check-backend-function
7547          'request-expire-articles gnus-newsgroup-name)
7548     ;; This backend supports expiry.
7549     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7550            (expirable (if total
7551                           (progn
7552                             ;; We need to update the info for
7553                             ;; this group for `gnus-list-of-read-articles'
7554                             ;; to give us the right answer.
7555                             (gnus-run-hooks 'gnus-exit-group-hook)
7556                             (gnus-summary-update-info)
7557                             (gnus-list-of-read-articles gnus-newsgroup-name))
7558                         (setq gnus-newsgroup-expirable
7559                               (sort gnus-newsgroup-expirable '<))))
7560            (expiry-wait (if now 'immediate
7561                           (gnus-group-find-parameter
7562                            gnus-newsgroup-name 'expiry-wait)))
7563            es)
7564       (when expirable
7565         ;; There are expirable articles in this group, so we run them
7566         ;; through the expiry process.
7567         (gnus-message 6 "Expiring articles...")
7568         (unless (gnus-check-group gnus-newsgroup-name)
7569           (error "Can't open server for %s" gnus-newsgroup-name))
7570         ;; The list of articles that weren't expired is returned.
7571         (save-excursion
7572           (if expiry-wait
7573               (let ((nnmail-expiry-wait-function nil)
7574                     (nnmail-expiry-wait expiry-wait))
7575                 (setq es (gnus-request-expire-articles
7576                           expirable gnus-newsgroup-name)))
7577             (setq es (gnus-request-expire-articles
7578                       expirable gnus-newsgroup-name))))
7579         (unless total
7580           (setq gnus-newsgroup-expirable es))
7581         ;; We go through the old list of expirable, and mark all
7582         ;; really expired articles as nonexistent.
7583         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7584           (let ((gnus-use-cache nil))
7585             (while expirable
7586               (unless (memq (car expirable) es)
7587                 (when (gnus-data-find (car expirable))
7588                   (gnus-summary-mark-article
7589                    (car expirable) gnus-canceled-mark)))
7590               (setq expirable (cdr expirable)))))
7591         (gnus-message 6 "Expiring articles...done")))))
7592
7593 (defun gnus-summary-expire-articles-now ()
7594   "Expunge all expirable articles in the current group.
7595 This means that *all* articles that are marked as expirable will be
7596 deleted forever, right now."
7597   (interactive)
7598   (or gnus-expert-user
7599       (gnus-yes-or-no-p
7600        "Are you really, really, really sure you want to delete all these messages? ")
7601       (error "Phew!"))
7602   (gnus-summary-expire-articles t))
7603
7604 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7605 (defun gnus-summary-delete-article (&optional n)
7606   "Delete the N next (mail) articles.
7607 This command actually deletes articles.  This is not a marking
7608 command.  The article will disappear forever from your life, never to
7609 return.
7610 If N is negative, delete backwards.
7611 If N is nil and articles have been marked with the process mark,
7612 delete these instead."
7613   (interactive "P")
7614   (unless (gnus-check-backend-function 'request-expire-articles
7615                                        gnus-newsgroup-name)
7616     (error "The current newsgroup does not support article deletion"))
7617   ;; Compute the list of articles to delete.
7618   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7619         not-deleted)
7620     (if (and gnus-novice-user
7621              (not (gnus-yes-or-no-p
7622                    (format "Do you really want to delete %s forever? "
7623                            (if (> (length articles) 1)
7624                                (format "these %s articles" (length articles))
7625                              "this article")))))
7626         ()
7627       ;; Delete the articles.
7628       (setq not-deleted (gnus-request-expire-articles
7629                          articles gnus-newsgroup-name 'force))
7630       (while articles
7631         (gnus-summary-remove-process-mark (car articles))
7632         ;; The backend might not have been able to delete the article
7633         ;; after all.
7634         (unless (memq (car articles) not-deleted)
7635           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7636         (setq articles (cdr articles)))
7637       (when not-deleted
7638         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7639     (gnus-summary-position-point)
7640     (gnus-set-mode-line 'summary)
7641     not-deleted))
7642
7643 (defun gnus-summary-edit-article (&optional force)
7644   "Edit the current article.
7645 This will have permanent effect only in mail groups.
7646 If FORCE is non-nil, allow editing of articles even in read-only
7647 groups."
7648   (interactive "P")
7649   (save-excursion
7650     (set-buffer gnus-summary-buffer)
7651     (let ((mail-parse-charset gnus-newsgroup-charset)
7652           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7653       (gnus-set-global-variables)
7654       (when (and (not force)
7655                  (gnus-group-read-only-p))
7656         (error "The current newsgroup does not support article editing"))
7657       (gnus-summary-show-article t)
7658       (gnus-article-edit-article
7659        'mime-to-mml
7660        `(lambda (no-highlight)
7661           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7662                 (mail-parse-ignored-charsets 
7663                  ',gnus-newsgroup-ignored-charsets))
7664             (mml-to-mime)
7665             (gnus-summary-edit-article-done
7666              ,(or (mail-header-references gnus-current-headers) "")
7667              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7668
7669 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7670
7671 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7672                                                  no-highlight)
7673   "Make edits to the current article permanent."
7674   (interactive)
7675   ;; Replace the article.
7676   (let ((buf (current-buffer)))
7677     (with-temp-buffer
7678       (insert-buffer-substring buf)
7679       (if (and (not read-only)
7680                (not (gnus-request-replace-article
7681                      (cdr gnus-article-current) (car gnus-article-current)
7682                      (current-buffer) t)))
7683           (error "Couldn't replace article")
7684         ;; Update the summary buffer.
7685         (if (and references
7686                  (equal (message-tokenize-header references " ")
7687                         (message-tokenize-header
7688                          (or (message-fetch-field "references") "") " ")))
7689             ;; We only have to update this line.
7690             (save-excursion
7691               (save-restriction
7692                 (message-narrow-to-head)
7693                 (let ((head (buffer-string))
7694                       header)
7695                   (with-temp-buffer
7696                     (insert (format "211 %d Article retrieved.\n"
7697                                     (cdr gnus-article-current)))
7698                     (insert head)
7699                     (insert ".\n")
7700                     (let ((nntp-server-buffer (current-buffer)))
7701                       (setq header (car (gnus-get-newsgroup-headers
7702                                          (save-excursion
7703                                            (set-buffer gnus-summary-buffer)
7704                                            gnus-newsgroup-dependencies)
7705                                          t))))
7706                     (save-excursion
7707                       (set-buffer gnus-summary-buffer)
7708                       (gnus-data-set-header
7709                        (gnus-data-find (cdr gnus-article-current))
7710                        header)
7711                       (gnus-summary-update-article-line
7712                        (cdr gnus-article-current) header))))))
7713           ;; Update threads.
7714           (set-buffer (or buffer gnus-summary-buffer))
7715           (gnus-summary-update-article (cdr gnus-article-current)))
7716         ;; Prettify the article buffer again.
7717         (unless no-highlight
7718           (save-excursion
7719             (set-buffer gnus-article-buffer)
7720             ;;;!!! Fix this -- article should be rehighlighted.
7721             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7722             (set-buffer gnus-original-article-buffer)
7723             (gnus-request-article
7724              (cdr gnus-article-current)
7725              (car gnus-article-current) (current-buffer))))
7726         ;; Prettify the summary buffer line.
7727         (when (gnus-visual-p 'summary-highlight 'highlight)
7728           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7729
7730 (defun gnus-summary-edit-wash (key)
7731   "Perform editing command KEY in the article buffer."
7732   (interactive
7733    (list
7734     (progn
7735       (message "%s" (concat (this-command-keys) "- "))
7736       (read-char))))
7737   (message "")
7738   (gnus-summary-edit-article)
7739   (execute-kbd-macro (concat (this-command-keys) key))
7740   (gnus-article-edit-done))
7741
7742 ;;; Respooling
7743
7744 (defun gnus-summary-respool-query (&optional silent trace)
7745   "Query where the respool algorithm would put this article."
7746   (interactive)
7747   (let (gnus-mark-article-hook)
7748     (gnus-summary-select-article)
7749     (save-excursion
7750       (set-buffer gnus-original-article-buffer)
7751       (save-restriction
7752         (message-narrow-to-head)
7753         (let ((groups (nnmail-article-group 'identity trace)))
7754           (unless silent
7755             (if groups
7756                 (message "This message would go to %s"
7757                          (mapconcat 'car groups ", "))
7758               (message "This message would go to no groups"))
7759             groups))))))
7760
7761 (defun gnus-summary-respool-trace ()
7762   "Trace where the respool algorithm would put this article.
7763 Display a buffer showing all fancy splitting patterns which matched."
7764   (interactive)
7765   (gnus-summary-respool-query nil t))
7766
7767 ;; Summary marking commands.
7768
7769 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7770   "Mark articles which has the same subject as read, and then select the next.
7771 If UNMARK is positive, remove any kind of mark.
7772 If UNMARK is negative, tick articles."
7773   (interactive "P")
7774   (when unmark
7775     (setq unmark (prefix-numeric-value unmark)))
7776   (let ((count
7777          (gnus-summary-mark-same-subject
7778           (gnus-summary-article-subject) unmark)))
7779     ;; Select next unread article.  If auto-select-same mode, should
7780     ;; select the first unread article.
7781     (gnus-summary-next-article t (and gnus-auto-select-same
7782                                       (gnus-summary-article-subject)))
7783     (gnus-message 7 "%d article%s marked as %s"
7784                   count (if (= count 1) " is" "s are")
7785                   (if unmark "unread" "read"))))
7786
7787 (defun gnus-summary-kill-same-subject (&optional unmark)
7788   "Mark articles which has the same subject as read.
7789 If UNMARK is positive, remove any kind of mark.
7790 If UNMARK is negative, tick articles."
7791   (interactive "P")
7792   (when unmark
7793     (setq unmark (prefix-numeric-value unmark)))
7794   (let ((count
7795          (gnus-summary-mark-same-subject
7796           (gnus-summary-article-subject) unmark)))
7797     ;; If marked as read, go to next unread subject.
7798     (when (null unmark)
7799       ;; Go to next unread subject.
7800       (gnus-summary-next-subject 1 t))
7801     (gnus-message 7 "%d articles are marked as %s"
7802                   count (if unmark "unread" "read"))))
7803
7804 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7805   "Mark articles with same SUBJECT as read, and return marked number.
7806 If optional argument UNMARK is positive, remove any kinds of marks.
7807 If optional argument UNMARK is negative, mark articles as unread instead."
7808   (let ((count 1))
7809     (save-excursion
7810       (cond
7811        ((null unmark)                   ; Mark as read.
7812         (while (and
7813                 (progn
7814                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7815                   (gnus-summary-show-thread) t)
7816                 (gnus-summary-find-subject subject))
7817           (setq count (1+ count))))
7818        ((> unmark 0)                    ; Tick.
7819         (while (and
7820                 (progn
7821                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7822                   (gnus-summary-show-thread) t)
7823                 (gnus-summary-find-subject subject))
7824           (setq count (1+ count))))
7825        (t                               ; Mark as unread.
7826         (while (and
7827                 (progn
7828                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7829                   (gnus-summary-show-thread) t)
7830                 (gnus-summary-find-subject subject))
7831           (setq count (1+ count)))))
7832       (gnus-set-mode-line 'summary)
7833       ;; Return the number of marked articles.
7834       count)))
7835
7836 (defun gnus-summary-mark-as-processable (n &optional unmark)
7837   "Set the process mark on the next N articles.
7838 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7839 the process mark instead.  The difference between N and the actual
7840 number of articles marked is returned."
7841   (interactive "p")
7842   (let ((backward (< n 0))
7843         (n (abs n)))
7844     (while (and
7845             (> n 0)
7846             (if unmark
7847                 (gnus-summary-remove-process-mark
7848                  (gnus-summary-article-number))
7849               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7850             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7851       (setq n (1- n)))
7852     (when (/= 0 n)
7853       (gnus-message 7 "No more articles"))
7854     (gnus-summary-recenter)
7855     (gnus-summary-position-point)
7856     n))
7857
7858 (defun gnus-summary-unmark-as-processable (n)
7859   "Remove the process mark from the next N articles.
7860 If N is negative, unmark backward instead.  The difference between N and
7861 the actual number of articles unmarked is returned."
7862   (interactive "p")
7863   (gnus-summary-mark-as-processable n t))
7864
7865 (defun gnus-summary-unmark-all-processable ()
7866   "Remove the process mark from all articles."
7867   (interactive)
7868   (save-excursion
7869     (while gnus-newsgroup-processable
7870       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7871   (gnus-summary-position-point))
7872
7873 (defun gnus-summary-mark-as-expirable (n)
7874   "Mark N articles forward as expirable.
7875 If N is negative, mark backward instead.  The difference between N and
7876 the actual number of articles marked is returned."
7877   (interactive "p")
7878   (gnus-summary-mark-forward n gnus-expirable-mark))
7879
7880 (defun gnus-summary-mark-article-as-replied (article)
7881   "Mark ARTICLE replied and update the summary line."
7882   (push article gnus-newsgroup-replied)
7883   (let ((buffer-read-only nil))
7884     (when (gnus-summary-goto-subject article nil t)
7885       (gnus-summary-update-secondary-mark article))))
7886
7887 (defun gnus-summary-set-bookmark (article)
7888   "Set a bookmark in current article."
7889   (interactive (list (gnus-summary-article-number)))
7890   (when (or (not (get-buffer gnus-article-buffer))
7891             (not gnus-current-article)
7892             (not gnus-article-current)
7893             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7894     (error "No current article selected"))
7895   ;; Remove old bookmark, if one exists.
7896   (let ((old (assq article gnus-newsgroup-bookmarks)))
7897     (when old
7898       (setq gnus-newsgroup-bookmarks
7899             (delq old gnus-newsgroup-bookmarks))))
7900   ;; Set the new bookmark, which is on the form
7901   ;; (article-number . line-number-in-body).
7902   (push
7903    (cons article
7904          (save-excursion
7905            (set-buffer gnus-article-buffer)
7906            (count-lines
7907             (min (point)
7908                  (save-excursion
7909                    (goto-char (point-min))
7910                    (search-forward "\n\n" nil t)
7911                    (point)))
7912             (point))))
7913    gnus-newsgroup-bookmarks)
7914   (gnus-message 6 "A bookmark has been added to the current article."))
7915
7916 (defun gnus-summary-remove-bookmark (article)
7917   "Remove the bookmark from the current article."
7918   (interactive (list (gnus-summary-article-number)))
7919   ;; Remove old bookmark, if one exists.
7920   (let ((old (assq article gnus-newsgroup-bookmarks)))
7921     (if old
7922         (progn
7923           (setq gnus-newsgroup-bookmarks
7924                 (delq old gnus-newsgroup-bookmarks))
7925           (gnus-message 6 "Removed bookmark."))
7926       (gnus-message 6 "No bookmark in current article."))))
7927
7928 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7929 (defun gnus-summary-mark-as-dormant (n)
7930   "Mark N articles forward as dormant.
7931 If N is negative, mark backward instead.  The difference between N and
7932 the actual number of articles marked is returned."
7933   (interactive "p")
7934   (gnus-summary-mark-forward n gnus-dormant-mark))
7935
7936 (defun gnus-summary-set-process-mark (article)
7937   "Set the process mark on ARTICLE and update the summary line."
7938   (setq gnus-newsgroup-processable
7939         (cons article
7940               (delq article gnus-newsgroup-processable)))
7941   (when (gnus-summary-goto-subject article)
7942     (gnus-summary-show-thread)
7943     (gnus-summary-goto-subject article)
7944     (gnus-summary-update-secondary-mark article)))
7945
7946 (defun gnus-summary-remove-process-mark (article)
7947   "Remove the process mark from ARTICLE and update the summary line."
7948   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7949   (when (gnus-summary-goto-subject article)
7950     (gnus-summary-show-thread)
7951     (gnus-summary-goto-subject article)
7952     (gnus-summary-update-secondary-mark article)))
7953
7954 (defun gnus-summary-set-saved-mark (article)
7955   "Set the process mark on ARTICLE and update the summary line."
7956   (push article gnus-newsgroup-saved)
7957   (when (gnus-summary-goto-subject article)
7958     (gnus-summary-update-secondary-mark article)))
7959
7960 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7961   "Mark N articles as read forwards.
7962 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7963 The difference between N and the actual number of articles marked is
7964 returned."
7965   (interactive "p")
7966   (gnus-summary-show-thread)
7967   (let ((backward (< n 0))
7968         (gnus-summary-goto-unread
7969          (and gnus-summary-goto-unread
7970               (not (eq gnus-summary-goto-unread 'never))
7971               (not (memq mark (list gnus-unread-mark
7972                                     gnus-ticked-mark gnus-dormant-mark)))))
7973         (n (abs n))
7974         (mark (or mark gnus-del-mark)))
7975     (while (and (> n 0)
7976                 (gnus-summary-mark-article nil mark no-expire)
7977                 (zerop (gnus-summary-next-subject
7978                         (if backward -1 1)
7979                         (and gnus-summary-goto-unread
7980                              (not (eq gnus-summary-goto-unread 'never)))
7981                         t)))
7982       (setq n (1- n)))
7983     (when (/= 0 n)
7984       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7985     (gnus-summary-recenter)
7986     (gnus-summary-position-point)
7987     (gnus-set-mode-line 'summary)
7988     n))
7989
7990 (defun gnus-summary-mark-article-as-read (mark)
7991   "Mark the current article quickly as read with MARK."
7992   (let ((article (gnus-summary-article-number)))
7993     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7994     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7995     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7996     (push (cons article mark) gnus-newsgroup-reads)
7997     ;; Possibly remove from cache, if that is used.
7998     (when gnus-use-cache
7999       (gnus-cache-enter-remove-article article))
8000     ;; Allow the backend to change the mark.
8001     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8002     ;; Check for auto-expiry.
8003     (when (and gnus-newsgroup-auto-expire
8004                (memq mark gnus-auto-expirable-marks))
8005       (setq mark gnus-expirable-mark)
8006       ;; Let the backend know about the mark change.
8007       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8008       (push article gnus-newsgroup-expirable))
8009     ;; Set the mark in the buffer.
8010     (gnus-summary-update-mark mark 'unread)
8011     t))
8012
8013 (defun gnus-summary-mark-article-as-unread (mark)
8014   "Mark the current article quickly as unread with MARK."
8015   (let* ((article (gnus-summary-article-number))
8016          (old-mark (gnus-summary-article-mark article)))
8017     ;; Allow the backend to change the mark.
8018     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8019     (if (eq mark old-mark)
8020         t
8021       (if (<= article 0)
8022           (progn
8023             (gnus-error 1 "Can't mark negative article numbers")
8024             nil)
8025         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8026         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8027         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8028         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8029         (cond ((= mark gnus-ticked-mark)
8030                (push article gnus-newsgroup-marked))
8031               ((= mark gnus-dormant-mark)
8032                (push article gnus-newsgroup-dormant))
8033               (t
8034                (push article gnus-newsgroup-unreads)))
8035         (gnus-pull article gnus-newsgroup-reads)
8036
8037         ;; See whether the article is to be put in the cache.
8038         (and gnus-use-cache
8039              (vectorp (gnus-summary-article-header article))
8040              (save-excursion
8041                (gnus-cache-possibly-enter-article
8042                 gnus-newsgroup-name article
8043                 (= mark gnus-ticked-mark)
8044                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8045
8046         ;; Fix the mark.
8047         (gnus-summary-update-mark mark 'unread)
8048         t))))
8049
8050 (defun gnus-summary-mark-article (&optional article mark no-expire)
8051   "Mark ARTICLE with MARK.  MARK can be any character.
8052 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8053 `??' (dormant) and `?E' (expirable).
8054 If MARK is nil, then the default character `?r' is used.
8055 If ARTICLE is nil, then the article on the current line will be
8056 marked."
8057   ;; The mark might be a string.
8058   (when (stringp mark)
8059     (setq mark (aref mark 0)))
8060   ;; If no mark is given, then we check auto-expiring.
8061   (when (null mark)
8062     (setq mark gnus-del-mark))
8063   (when (and (not no-expire)
8064              gnus-newsgroup-auto-expire
8065              (memq mark gnus-auto-expirable-marks))
8066     (setq mark gnus-expirable-mark))
8067   (let ((article (or article (gnus-summary-article-number)))
8068         (old-mark (gnus-summary-article-mark article)))
8069     ;; Allow the backend to change the mark.
8070     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8071     (if (eq mark old-mark)
8072         t
8073       (unless article
8074         (error "No article on current line"))
8075       (if (not (if (or (= mark gnus-unread-mark)
8076                        (= mark gnus-ticked-mark)
8077                        (= mark gnus-dormant-mark))
8078                    (gnus-mark-article-as-unread article mark)
8079                  (gnus-mark-article-as-read article mark)))
8080           t
8081         ;; See whether the article is to be put in the cache.
8082         (and gnus-use-cache
8083              (not (= mark gnus-canceled-mark))
8084              (vectorp (gnus-summary-article-header article))
8085              (save-excursion
8086                (gnus-cache-possibly-enter-article
8087                 gnus-newsgroup-name article
8088                 (= mark gnus-ticked-mark)
8089                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8090
8091         (when (gnus-summary-goto-subject article nil t)
8092           (let ((buffer-read-only nil))
8093             (gnus-summary-show-thread)
8094             ;; Fix the mark.
8095             (gnus-summary-update-mark mark 'unread)
8096             t))))))
8097
8098 (defun gnus-summary-update-secondary-mark (article)
8099   "Update the secondary (read, process, cache) mark."
8100   (gnus-summary-update-mark
8101    (cond ((memq article gnus-newsgroup-processable)
8102           gnus-process-mark)
8103          ((memq article gnus-newsgroup-cached)
8104           gnus-cached-mark)
8105          ((memq article gnus-newsgroup-replied)
8106           gnus-replied-mark)
8107          ((memq article gnus-newsgroup-saved)
8108           gnus-saved-mark)
8109          (t gnus-unread-mark))
8110    'replied)
8111   (when (gnus-visual-p 'summary-highlight 'highlight)
8112     (gnus-run-hooks 'gnus-summary-update-hook))
8113   t)
8114
8115 (defun gnus-summary-update-mark (mark type)
8116   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8117         (buffer-read-only nil))
8118     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8119     (when forward
8120       (when (looking-at "\r")
8121         (incf forward))
8122       (when (<= (+ forward (point)) (point-max))
8123         ;; Go to the right position on the line.
8124         (goto-char (+ forward (point)))
8125         ;; Replace the old mark with the new mark.
8126         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8127         ;; Optionally update the marks by some user rule.
8128         (when (eq type 'unread)
8129           (gnus-data-set-mark
8130            (gnus-data-find (gnus-summary-article-number)) mark)
8131           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8132
8133 (defun gnus-mark-article-as-read (article &optional mark)
8134   "Enter ARTICLE in the pertinent lists and remove it from others."
8135   ;; Make the article expirable.
8136   (let ((mark (or mark gnus-del-mark)))
8137     (if (= mark gnus-expirable-mark)
8138         (push article gnus-newsgroup-expirable)
8139       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8140     ;; Remove from unread and marked lists.
8141     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8142     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8143     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8144     (push (cons article mark) gnus-newsgroup-reads)
8145     ;; Possibly remove from cache, if that is used.
8146     (when gnus-use-cache
8147       (gnus-cache-enter-remove-article article))
8148     t))
8149
8150 (defun gnus-mark-article-as-unread (article &optional mark)
8151   "Enter ARTICLE in the pertinent lists and remove it from others."
8152   (let ((mark (or mark gnus-ticked-mark)))
8153     (if (<= article 0)
8154         (progn
8155           (gnus-error 1 "Can't mark negative article numbers")
8156           nil)
8157       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8158             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8159             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8160             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8161
8162       ;; Unsuppress duplicates?
8163       (when gnus-suppress-duplicates
8164         (gnus-dup-unsuppress-article article))
8165
8166       (cond ((= mark gnus-ticked-mark)
8167              (push article gnus-newsgroup-marked))
8168             ((= mark gnus-dormant-mark)
8169              (push article gnus-newsgroup-dormant))
8170             (t
8171              (push article gnus-newsgroup-unreads)))
8172       (gnus-pull article gnus-newsgroup-reads)
8173       t)))
8174
8175 (defalias 'gnus-summary-mark-as-unread-forward
8176   'gnus-summary-tick-article-forward)
8177 (make-obsolete 'gnus-summary-mark-as-unread-forward
8178                'gnus-summary-tick-article-forward)
8179 (defun gnus-summary-tick-article-forward (n)
8180   "Tick N articles forwards.
8181 If N is negative, tick backwards instead.
8182 The difference between N and the number of articles ticked is returned."
8183   (interactive "p")
8184   (gnus-summary-mark-forward n gnus-ticked-mark))
8185
8186 (defalias 'gnus-summary-mark-as-unread-backward
8187   'gnus-summary-tick-article-backward)
8188 (make-obsolete 'gnus-summary-mark-as-unread-backward
8189                'gnus-summary-tick-article-backward)
8190 (defun gnus-summary-tick-article-backward (n)
8191   "Tick N articles backwards.
8192 The difference between N and the number of articles ticked is returned."
8193   (interactive "p")
8194   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8195
8196 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8197 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8198 (defun gnus-summary-tick-article (&optional article clear-mark)
8199   "Mark current article as unread.
8200 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8201 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8202   (interactive)
8203   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8204                                        gnus-ticked-mark)))
8205
8206 (defun gnus-summary-mark-as-read-forward (n)
8207   "Mark N articles as read forwards.
8208 If N is negative, mark backwards instead.
8209 The difference between N and the actual number of articles marked is
8210 returned."
8211   (interactive "p")
8212   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8213
8214 (defun gnus-summary-mark-as-read-backward (n)
8215   "Mark the N articles as read backwards.
8216 The difference between N and the actual number of articles marked is
8217 returned."
8218   (interactive "p")
8219   (gnus-summary-mark-forward
8220    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8221
8222 (defun gnus-summary-mark-as-read (&optional article mark)
8223   "Mark current article as read.
8224 ARTICLE specifies the article to be marked as read.
8225 MARK specifies a string to be inserted at the beginning of the line."
8226   (gnus-summary-mark-article article mark))
8227
8228 (defun gnus-summary-clear-mark-forward (n)
8229   "Clear marks from N articles forward.
8230 If N is negative, clear backward instead.
8231 The difference between N and the number of marks cleared is returned."
8232   (interactive "p")
8233   (gnus-summary-mark-forward n gnus-unread-mark))
8234
8235 (defun gnus-summary-clear-mark-backward (n)
8236   "Clear marks from N articles backward.
8237 The difference between N and the number of marks cleared is returned."
8238   (interactive "p")
8239   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8240
8241 (defun gnus-summary-mark-unread-as-read ()
8242   "Intended to be used by `gnus-summary-mark-article-hook'."
8243   (when (memq gnus-current-article gnus-newsgroup-unreads)
8244     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8245
8246 (defun gnus-summary-mark-read-and-unread-as-read ()
8247   "Intended to be used by `gnus-summary-mark-article-hook'."
8248   (let ((mark (gnus-summary-article-mark)))
8249     (when (or (gnus-unread-mark-p mark)
8250               (gnus-read-mark-p mark))
8251       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8252
8253 (defun gnus-summary-mark-region-as-read (point mark all)
8254   "Mark all unread articles between point and mark as read.
8255 If given a prefix, mark all articles between point and mark as read,
8256 even ticked and dormant ones."
8257   (interactive "r\nP")
8258   (save-excursion
8259     (let (article)
8260       (goto-char point)
8261       (beginning-of-line)
8262       (while (and
8263               (< (point) mark)
8264               (progn
8265                 (when (or all
8266                           (memq (setq article (gnus-summary-article-number))
8267                                 gnus-newsgroup-unreads))
8268                   (gnus-summary-mark-article article gnus-del-mark))
8269                 t)
8270               (gnus-summary-find-next))))))
8271
8272 (defun gnus-summary-mark-below (score mark)
8273   "Mark articles with score less than SCORE with MARK."
8274   (interactive "P\ncMark: ")
8275   (setq score (if score
8276                   (prefix-numeric-value score)
8277                 (or gnus-summary-default-score 0)))
8278   (save-excursion
8279     (set-buffer gnus-summary-buffer)
8280     (goto-char (point-min))
8281     (while
8282         (progn
8283           (and (< (gnus-summary-article-score) score)
8284                (gnus-summary-mark-article nil mark))
8285           (gnus-summary-find-next)))))
8286
8287 (defun gnus-summary-kill-below (&optional score)
8288   "Mark articles with score below SCORE as read."
8289   (interactive "P")
8290   (gnus-summary-mark-below score gnus-killed-mark))
8291
8292 (defun gnus-summary-clear-above (&optional score)
8293   "Clear all marks from articles with score above SCORE."
8294   (interactive "P")
8295   (gnus-summary-mark-above score gnus-unread-mark))
8296
8297 (defun gnus-summary-tick-above (&optional score)
8298   "Tick all articles with score above SCORE."
8299   (interactive "P")
8300   (gnus-summary-mark-above score gnus-ticked-mark))
8301
8302 (defun gnus-summary-mark-above (score mark)
8303   "Mark articles with score over SCORE with MARK."
8304   (interactive "P\ncMark: ")
8305   (setq score (if score
8306                   (prefix-numeric-value score)
8307                 (or gnus-summary-default-score 0)))
8308   (save-excursion
8309     (set-buffer gnus-summary-buffer)
8310     (goto-char (point-min))
8311     (while (and (progn
8312                   (when (> (gnus-summary-article-score) score)
8313                     (gnus-summary-mark-article nil mark))
8314                   t)
8315                 (gnus-summary-find-next)))))
8316
8317 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8318 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8319 (defun gnus-summary-limit-include-expunged (&optional no-error)
8320   "Display all the hidden articles that were expunged for low scores."
8321   (interactive)
8322   (let ((buffer-read-only nil))
8323     (let ((scored gnus-newsgroup-scored)
8324           headers h)
8325       (while scored
8326         (unless (gnus-summary-goto-subject (caar scored))
8327           (and (setq h (gnus-summary-article-header (caar scored)))
8328                (< (cdar scored) gnus-summary-expunge-below)
8329                (push h headers)))
8330         (setq scored (cdr scored)))
8331       (if (not headers)
8332           (when (not no-error)
8333             (error "No expunged articles hidden"))
8334         (goto-char (point-min))
8335         (gnus-summary-prepare-unthreaded (nreverse headers))
8336         (goto-char (point-min))
8337         (gnus-summary-position-point)
8338         t))))
8339
8340 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8341   "Mark all unread articles in this newsgroup as read.
8342 If prefix argument ALL is non-nil, ticked and dormant articles will
8343 also be marked as read.
8344 If QUIETLY is non-nil, no questions will be asked.
8345 If TO-HERE is non-nil, it should be a point in the buffer.  All
8346 articles before this point will be marked as read.
8347 Note that this function will only catch up the unread article
8348 in the current summary buffer limitation.
8349 The number of articles marked as read is returned."
8350   (interactive "P")
8351   (prog1
8352       (save-excursion
8353         (when (or quietly
8354                   (not gnus-interactive-catchup) ;Without confirmation?
8355                   gnus-expert-user
8356                   (gnus-y-or-n-p
8357                    (if all
8358                        "Mark absolutely all articles as read? "
8359                      "Mark all unread articles as read? ")))
8360           (if (and not-mark
8361                    (not gnus-newsgroup-adaptive)
8362                    (not gnus-newsgroup-auto-expire)
8363                    (not gnus-suppress-duplicates)
8364                    (or (not gnus-use-cache)
8365                        (eq gnus-use-cache 'passive)))
8366               (progn
8367                 (when all
8368                   (setq gnus-newsgroup-marked nil
8369                         gnus-newsgroup-dormant nil))
8370                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8371             ;; We actually mark all articles as canceled, which we
8372             ;; have to do when using auto-expiry or adaptive scoring.
8373             (gnus-summary-show-all-threads)
8374             (when (gnus-summary-first-subject (not all) t)
8375               (while (and
8376                       (if to-here (< (point) to-here) t)
8377                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8378                       (gnus-summary-find-next (not all) nil nil t))))
8379             (gnus-set-mode-line 'summary))
8380           t))
8381     (gnus-summary-position-point)))
8382
8383 (defun gnus-summary-catchup-to-here (&optional all)
8384   "Mark all unticked articles before the current one as read.
8385 If ALL is non-nil, also mark ticked and dormant articles as read."
8386   (interactive "P")
8387   (save-excursion
8388     (gnus-save-hidden-threads
8389       (let ((beg (point)))
8390         ;; We check that there are unread articles.
8391         (when (or all (gnus-summary-find-prev))
8392           (gnus-summary-catchup all t beg)))))
8393   (gnus-summary-position-point))
8394
8395 (defun gnus-summary-catchup-all (&optional quietly)
8396   "Mark all articles in this newsgroup as read."
8397   (interactive "P")
8398   (gnus-summary-catchup t quietly))
8399
8400 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8401   "Mark all unread articles in this group as read, then exit.
8402 If prefix argument ALL is non-nil, all articles are marked as read."
8403   (interactive "P")
8404   (when (gnus-summary-catchup all quietly nil 'fast)
8405     ;; Select next newsgroup or exit.
8406     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8407              (eq gnus-auto-select-next 'quietly))
8408         (gnus-summary-next-group nil)
8409       (gnus-summary-exit))))
8410
8411 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8412   "Mark all articles in this newsgroup as read, and then exit."
8413   (interactive "P")
8414   (gnus-summary-catchup-and-exit t quietly))
8415
8416 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8417 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8418   "Mark all articles in this group as read and select the next group.
8419 If given a prefix, mark all articles, unread as well as ticked, as
8420 read."
8421   (interactive "P")
8422   (save-excursion
8423     (gnus-summary-catchup all))
8424   (gnus-summary-next-group t nil nil))
8425
8426 ;; Thread-based commands.
8427
8428 (defun gnus-summary-articles-in-thread (&optional article)
8429   "Return a list of all articles in the current thread.
8430 If ARTICLE is non-nil, return all articles in the thread that starts
8431 with that article."
8432   (let* ((article (or article (gnus-summary-article-number)))
8433          (data (gnus-data-find-list article))
8434          (top-level (gnus-data-level (car data)))
8435          (top-subject
8436           (cond ((null gnus-thread-operation-ignore-subject)
8437                  (gnus-simplify-subject-re
8438                   (mail-header-subject (gnus-data-header (car data)))))
8439                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8440                  (gnus-simplify-subject-fuzzy
8441                   (mail-header-subject (gnus-data-header (car data)))))
8442                 (t nil)))
8443          (end-point (save-excursion
8444                       (if (gnus-summary-go-to-next-thread)
8445                           (point) (point-max))))
8446          articles)
8447     (while (and data
8448                 (< (gnus-data-pos (car data)) end-point))
8449       (when (or (not top-subject)
8450                 (string= top-subject
8451                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8452                              (gnus-simplify-subject-fuzzy
8453                               (mail-header-subject
8454                                (gnus-data-header (car data))))
8455                            (gnus-simplify-subject-re
8456                             (mail-header-subject
8457                              (gnus-data-header (car data)))))))
8458         (push (gnus-data-number (car data)) articles))
8459       (unless (and (setq data (cdr data))
8460                    (> (gnus-data-level (car data)) top-level))
8461         (setq data nil)))
8462     ;; Return the list of articles.
8463     (nreverse articles)))
8464
8465 (defun gnus-summary-rethread-current ()
8466   "Rethread the thread the current article is part of."
8467   (interactive)
8468   (let* ((gnus-show-threads t)
8469          (article (gnus-summary-article-number))
8470          (id (mail-header-id (gnus-summary-article-header)))
8471          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8472     (unless id
8473       (error "No article on the current line"))
8474     (gnus-rebuild-thread id)
8475     (gnus-summary-goto-subject article)))
8476
8477 (defun gnus-summary-reparent-thread ()
8478   "Make the current article child of the marked (or previous) article.
8479
8480 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8481 is non-nil or the Subject: of both articles are the same."
8482   (interactive)
8483   (unless (not (gnus-group-read-only-p))
8484     (error "The current newsgroup does not support article editing"))
8485   (unless (<= (length gnus-newsgroup-processable) 1)
8486     (error "No more than one article may be marked"))
8487   (save-window-excursion
8488     (let ((gnus-article-buffer " *reparent*")
8489           (current-article (gnus-summary-article-number))
8490           ;; First grab the marked article, otherwise one line up.
8491           (parent-article (if (not (null gnus-newsgroup-processable))
8492                               (car gnus-newsgroup-processable)
8493                             (save-excursion
8494                               (if (eq (forward-line -1) 0)
8495                                   (gnus-summary-article-number)
8496                                 (error "Beginning of summary buffer"))))))
8497       (unless (not (eq current-article parent-article))
8498         (error "An article may not be self-referential"))
8499       (let ((message-id (mail-header-id
8500                          (gnus-summary-article-header parent-article))))
8501         (unless (and message-id (not (equal message-id "")))
8502           (error "No message-id in desired parent"))
8503         (gnus-with-article current-article
8504           (goto-char (point-min))
8505           (if (re-search-forward "^References: " nil t)
8506               (progn
8507                 (re-search-forward "^[^ \t]" nil t)
8508                 (forward-line -1)
8509                 (end-of-line)
8510                 (insert " " message-id))
8511             (insert "References: " message-id "\n")))
8512         (set-buffer gnus-summary-buffer)
8513         (gnus-summary-unmark-all-processable)
8514         (gnus-summary-update-article current-article)
8515         (gnus-summary-rethread-current)
8516         (gnus-message 3 "Article %d is now the child of article %d"
8517                       current-article parent-article)))))
8518
8519 (defun gnus-summary-toggle-threads (&optional arg)
8520   "Toggle showing conversation threads.
8521 If ARG is positive number, turn showing conversation threads on."
8522   (interactive "P")
8523   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8524     (setq gnus-show-threads
8525           (if (null arg) (not gnus-show-threads)
8526             (> (prefix-numeric-value arg) 0)))
8527     (gnus-summary-prepare)
8528     (gnus-summary-goto-subject current)
8529     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8530     (gnus-summary-position-point)))
8531
8532 (defun gnus-summary-show-all-threads ()
8533   "Show all threads."
8534   (interactive)
8535   (save-excursion
8536     (let ((buffer-read-only nil))
8537       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8538   (gnus-summary-position-point))
8539
8540 (defun gnus-summary-show-thread ()
8541   "Show thread subtrees.
8542 Returns nil if no thread was there to be shown."
8543   (interactive)
8544   (let ((buffer-read-only nil)
8545         (orig (point))
8546         ;; first goto end then to beg, to have point at beg after let
8547         (end (progn (end-of-line) (point)))
8548         (beg (progn (beginning-of-line) (point))))
8549     (prog1
8550         ;; Any hidden lines here?
8551         (search-forward "\r" end t)
8552       (subst-char-in-region beg end ?\^M ?\n t)
8553       (goto-char orig)
8554       (gnus-summary-position-point))))
8555
8556 (defun gnus-summary-hide-all-threads ()
8557   "Hide all thread subtrees."
8558   (interactive)
8559   (save-excursion
8560     (goto-char (point-min))
8561     (gnus-summary-hide-thread)
8562     (while (zerop (gnus-summary-next-thread 1 t))
8563       (gnus-summary-hide-thread)))
8564   (gnus-summary-position-point))
8565
8566 (defun gnus-summary-hide-thread ()
8567   "Hide thread subtrees.
8568 Returns nil if no threads were there to be hidden."
8569   (interactive)
8570   (let ((buffer-read-only nil)
8571         (start (point))
8572         (article (gnus-summary-article-number)))
8573     (goto-char start)
8574     ;; Go forward until either the buffer ends or the subthread
8575     ;; ends.
8576     (when (and (not (eobp))
8577                (or (zerop (gnus-summary-next-thread 1 t))
8578                    (goto-char (point-max))))
8579       (prog1
8580           (if (and (> (point) start)
8581                    (search-backward "\n" start t))
8582               (progn
8583                 (subst-char-in-region start (point) ?\n ?\^M)
8584                 (gnus-summary-goto-subject article))
8585             (goto-char start)
8586             nil)
8587         ;;(gnus-summary-position-point)
8588         ))))
8589
8590 (defun gnus-summary-go-to-next-thread (&optional previous)
8591   "Go to the same level (or less) next thread.
8592 If PREVIOUS is non-nil, go to previous thread instead.
8593 Return the article number moved to, or nil if moving was impossible."
8594   (let ((level (gnus-summary-thread-level))
8595         (way (if previous -1 1))
8596         (beg (point)))
8597     (forward-line way)
8598     (while (and (not (eobp))
8599                 (< level (gnus-summary-thread-level)))
8600       (forward-line way))
8601     (if (eobp)
8602         (progn
8603           (goto-char beg)
8604           nil)
8605       (setq beg (point))
8606       (prog1
8607           (gnus-summary-article-number)
8608         (goto-char beg)))))
8609
8610 (defun gnus-summary-next-thread (n &optional silent)
8611   "Go to the same level next N'th thread.
8612 If N is negative, search backward instead.
8613 Returns the difference between N and the number of skips actually
8614 done.
8615
8616 If SILENT, don't output messages."
8617   (interactive "p")
8618   (let ((backward (< n 0))
8619         (n (abs n)))
8620     (while (and (> n 0)
8621                 (gnus-summary-go-to-next-thread backward))
8622       (decf n))
8623     (unless silent
8624       (gnus-summary-position-point))
8625     (when (and (not silent) (/= 0 n))
8626       (gnus-message 7 "No more threads"))
8627     n))
8628
8629 (defun gnus-summary-prev-thread (n)
8630   "Go to the same level previous N'th thread.
8631 Returns the difference between N and the number of skips actually
8632 done."
8633   (interactive "p")
8634   (gnus-summary-next-thread (- n)))
8635
8636 (defun gnus-summary-go-down-thread ()
8637   "Go down one level in the current thread."
8638   (let ((children (gnus-summary-article-children)))
8639     (when children
8640       (gnus-summary-goto-subject (car children)))))
8641
8642 (defun gnus-summary-go-up-thread ()
8643   "Go up one level in the current thread."
8644   (let ((parent (gnus-summary-article-parent)))
8645     (when parent
8646       (gnus-summary-goto-subject parent))))
8647
8648 (defun gnus-summary-down-thread (n)
8649   "Go down thread N steps.
8650 If N is negative, go up instead.
8651 Returns the difference between N and how many steps down that were
8652 taken."
8653   (interactive "p")
8654   (let ((up (< n 0))
8655         (n (abs n)))
8656     (while (and (> n 0)
8657                 (if up (gnus-summary-go-up-thread)
8658                   (gnus-summary-go-down-thread)))
8659       (setq n (1- n)))
8660     (gnus-summary-position-point)
8661     (when (/= 0 n)
8662       (gnus-message 7 "Can't go further"))
8663     n))
8664
8665 (defun gnus-summary-up-thread (n)
8666   "Go up thread N steps.
8667 If N is negative, go up instead.
8668 Returns the difference between N and how many steps down that were
8669 taken."
8670   (interactive "p")
8671   (gnus-summary-down-thread (- n)))
8672
8673 (defun gnus-summary-top-thread ()
8674   "Go to the top of the thread."
8675   (interactive)
8676   (while (gnus-summary-go-up-thread))
8677   (gnus-summary-article-number))
8678
8679 (defun gnus-summary-kill-thread (&optional unmark)
8680   "Mark articles under current thread as read.
8681 If the prefix argument is positive, remove any kinds of marks.
8682 If the prefix argument is negative, tick articles instead."
8683   (interactive "P")
8684   (when unmark
8685     (setq unmark (prefix-numeric-value unmark)))
8686   (let ((articles (gnus-summary-articles-in-thread)))
8687     (save-excursion
8688       ;; Expand the thread.
8689       (gnus-summary-show-thread)
8690       ;; Mark all the articles.
8691       (while articles
8692         (gnus-summary-goto-subject (car articles))
8693         (cond ((null unmark)
8694                (gnus-summary-mark-article-as-read gnus-killed-mark))
8695               ((> unmark 0)
8696                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8697               (t
8698                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8699         (setq articles (cdr articles))))
8700     ;; Hide killed subtrees.
8701     (and (null unmark)
8702          gnus-thread-hide-killed
8703          (gnus-summary-hide-thread))
8704     ;; If marked as read, go to next unread subject.
8705     (when (null unmark)
8706       ;; Go to next unread subject.
8707       (gnus-summary-next-subject 1 t)))
8708   (gnus-set-mode-line 'summary))
8709
8710 ;; Summary sorting commands
8711
8712 (defun gnus-summary-sort-by-number (&optional reverse)
8713   "Sort the summary buffer by article number.
8714 Argument REVERSE means reverse order."
8715   (interactive "P")
8716   (gnus-summary-sort 'number reverse))
8717
8718 (defun gnus-summary-sort-by-author (&optional reverse)
8719   "Sort the summary buffer by author name alphabetically.
8720 If case-fold-search is non-nil, case of letters is ignored.
8721 Argument REVERSE means reverse order."
8722   (interactive "P")
8723   (gnus-summary-sort 'author reverse))
8724
8725 (defun gnus-summary-sort-by-subject (&optional reverse)
8726   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8727 If case-fold-search is non-nil, case of letters is ignored.
8728 Argument REVERSE means reverse order."
8729   (interactive "P")
8730   (gnus-summary-sort 'subject reverse))
8731
8732 (defun gnus-summary-sort-by-date (&optional reverse)
8733   "Sort the summary buffer by date.
8734 Argument REVERSE means reverse order."
8735   (interactive "P")
8736   (gnus-summary-sort 'date reverse))
8737
8738 (defun gnus-summary-sort-by-score (&optional reverse)
8739   "Sort the summary buffer by score.
8740 Argument REVERSE means reverse order."
8741   (interactive "P")
8742   (gnus-summary-sort 'score reverse))
8743
8744 (defun gnus-summary-sort-by-lines (&optional reverse)
8745   "Sort the summary buffer by the number of lines.
8746 Argument REVERSE means reverse order."
8747   (interactive "P")
8748   (gnus-summary-sort 'lines reverse))
8749
8750 (defun gnus-summary-sort-by-chars (&optional reverse)
8751   "Sort the summary buffer by article length.
8752 Argument REVERSE means reverse order."
8753   (interactive "P")
8754   (gnus-summary-sort 'chars reverse))   
8755
8756 (defun gnus-summary-sort (predicate reverse)
8757   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8758   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8759          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8760          (gnus-thread-sort-functions
8761           (if (not reverse)
8762               thread
8763             `(lambda (t1 t2)
8764                (,thread t2 t1))))
8765          (gnus-article-sort-functions
8766           (if (not reverse)
8767               article
8768             `(lambda (t1 t2)
8769                (,article t2 t1))))
8770          (buffer-read-only)
8771          (gnus-summary-prepare-hook nil))
8772     ;; We do the sorting by regenerating the threads.
8773     (gnus-summary-prepare)
8774     ;; Hide subthreads if needed.
8775     (when (and gnus-show-threads gnus-thread-hide-subtree)
8776       (gnus-summary-hide-all-threads))))
8777
8778 ;; Summary saving commands.
8779
8780 (defun gnus-summary-save-article (&optional n not-saved)
8781   "Save the current article using the default saver function.
8782 If N is a positive number, save the N next articles.
8783 If N is a negative number, save the N previous articles.
8784 If N is nil and any articles have been marked with the process mark,
8785 save those articles instead.
8786 The variable `gnus-default-article-saver' specifies the saver function."
8787   (interactive "P")
8788   (let* ((articles (gnus-summary-work-articles n))
8789          (save-buffer (save-excursion
8790                         (nnheader-set-temp-buffer " *Gnus Save*")))
8791          (num (length articles))
8792          header file)
8793     (dolist (article articles)
8794       (setq header (gnus-summary-article-header article))
8795       (if (not (vectorp header))
8796           ;; This is a pseudo-article.
8797           (if (assq 'name header)
8798               (gnus-copy-file (cdr (assq 'name header)))
8799             (gnus-message 1 "Article %d is unsaveable" article))
8800         ;; This is a real article.
8801         (save-window-excursion
8802           (gnus-summary-select-article t nil nil article))
8803         (save-excursion
8804           (set-buffer save-buffer)
8805           (erase-buffer)
8806           (insert-buffer-substring gnus-original-article-buffer))
8807         (setq file (gnus-article-save save-buffer file num))
8808         (gnus-summary-remove-process-mark article)
8809         (unless not-saved
8810           (gnus-summary-set-saved-mark article))))
8811     (gnus-kill-buffer save-buffer)
8812     (gnus-summary-position-point)
8813     (gnus-set-mode-line 'summary)
8814     n))
8815
8816 (defun gnus-summary-pipe-output (&optional arg)
8817   "Pipe the current article to a subprocess.
8818 If N is a positive number, pipe the N next articles.
8819 If N is a negative number, pipe the N previous articles.
8820 If N is nil and any articles have been marked with the process mark,
8821 pipe those articles instead."
8822   (interactive "P")
8823   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8824     (gnus-summary-save-article arg t))
8825   (gnus-configure-windows 'pipe))
8826
8827 (defun gnus-summary-save-article-mail (&optional arg)
8828   "Append the current article to an mail file.
8829 If N is a positive number, save the N next articles.
8830 If N is a negative number, save the N previous articles.
8831 If N is nil and any articles have been marked with the process mark,
8832 save those articles instead."
8833   (interactive "P")
8834   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8835     (gnus-summary-save-article arg)))
8836
8837 (defun gnus-summary-save-article-rmail (&optional arg)
8838   "Append the current article to an rmail file.
8839 If N is a positive number, save the N next articles.
8840 If N is a negative number, save the N previous articles.
8841 If N is nil and any articles have been marked with the process mark,
8842 save those articles instead."
8843   (interactive "P")
8844   (let ((gnus-default-article-saver 'rmail-output-to-rmail-file))
8845     (gnus-summary-save-article arg)))
8846
8847 (defun gnus-summary-save-article-file (&optional arg)
8848   "Append the current article to a file.
8849 If N is a positive number, save the N next articles.
8850 If N is a negative number, save the N previous articles.
8851 If N is nil and any articles have been marked with the process mark,
8852 save those articles instead."
8853   (interactive "P")
8854   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8855     (gnus-summary-save-article arg)))
8856
8857 (defun gnus-summary-write-article-file (&optional arg)
8858   "Write the current article to a file, deleting the previous file.
8859 If N is a positive number, save the N next articles.
8860 If N is a negative number, save the N previous articles.
8861 If N is nil and any articles have been marked with the process mark,
8862 save those articles instead."
8863   (interactive "P")
8864   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8865     (gnus-summary-save-article arg)))
8866
8867 (defun gnus-summary-save-article-body-file (&optional arg)
8868   "Append the current article body to a file.
8869 If N is a positive number, save the N next articles.
8870 If N is a negative number, save the N previous articles.
8871 If N is nil and any articles have been marked with the process mark,
8872 save those articles instead."
8873   (interactive "P")
8874   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8875     (gnus-summary-save-article arg)))
8876
8877 (defun gnus-summary-pipe-message (program)
8878   "Pipe the current article through PROGRAM."
8879   (interactive "sProgram: ")
8880   (gnus-summary-select-article)
8881   (let ((mail-header-separator ""))
8882     (gnus-eval-in-buffer-window gnus-article-buffer
8883       (save-restriction
8884         (widen)
8885         (let ((start (window-start))
8886               buffer-read-only)
8887           (message-pipe-buffer-body program)
8888           (set-window-start (get-buffer-window (current-buffer)) start))))))
8889
8890 (defun gnus-get-split-value (methods)
8891   "Return a value based on the split METHODS."
8892   (let (split-name method result match)
8893     (when methods
8894       (save-excursion
8895         (set-buffer gnus-original-article-buffer)
8896         (save-restriction
8897           (nnheader-narrow-to-headers)
8898           (while methods
8899             (goto-char (point-min))
8900             (setq method (pop methods))
8901             (setq match (car method))
8902             (when (cond
8903                    ((stringp match)
8904                     ;; Regular expression.
8905                     (ignore-errors
8906                       (re-search-forward match nil t)))
8907                    ((gnus-functionp match)
8908                     ;; Function.
8909                     (save-restriction
8910                       (widen)
8911                       (setq result (funcall match gnus-newsgroup-name))))
8912                    ((consp match)
8913                     ;; Form.
8914                     (save-restriction
8915                       (widen)
8916                       (setq result (eval match)))))
8917               (setq split-name (append (cdr method) split-name))
8918               (cond ((stringp result)
8919                      (push (expand-file-name
8920                             result gnus-article-save-directory)
8921                            split-name))
8922                     ((consp result)
8923                      (setq split-name (append result split-name)))))))))
8924     (nreverse split-name)))
8925
8926 (defun gnus-valid-move-group-p (group)
8927   (and (boundp group)
8928        (symbol-name group)
8929        (symbol-value group)
8930        (gnus-get-function (gnus-find-method-for-group
8931                            (symbol-name group)) 'request-accept-article t)))
8932
8933 (defun gnus-read-move-group-name (prompt default articles prefix)
8934   "Read a group name."
8935   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8936          (minibuffer-confirm-incomplete nil) ; XEmacs
8937          (prom
8938           (format "%s %s to:"
8939                   prompt
8940                   (if (> (length articles) 1)
8941                       (format "these %d articles" (length articles))
8942                     "this article")))
8943          (to-newsgroup
8944           (cond
8945            ((null split-name)
8946             (gnus-completing-read default prom
8947                                   gnus-active-hashtb
8948                                   'gnus-valid-move-group-p
8949                                   nil prefix
8950                                   'gnus-group-history))
8951            ((= 1 (length split-name))
8952             (gnus-completing-read (car split-name) prom
8953                                   gnus-active-hashtb
8954                                   'gnus-valid-move-group-p
8955                                   nil nil
8956                                   'gnus-group-history))
8957            (t
8958             (gnus-completing-read nil prom
8959                                   (mapcar (lambda (el) (list el))
8960                                           (nreverse split-name))
8961                                   nil nil nil
8962                                   'gnus-group-history)))))
8963     (when to-newsgroup
8964       (if (or (string= to-newsgroup "")
8965               (string= to-newsgroup prefix))
8966           (setq to-newsgroup default))
8967       (unless to-newsgroup
8968         (error "No group name entered"))
8969       (or (gnus-active to-newsgroup)
8970           (gnus-activate-group to-newsgroup)
8971           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8972                                      to-newsgroup))
8973               (or (and (gnus-request-create-group
8974                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8975                        (gnus-activate-group
8976                         to-newsgroup nil nil
8977                         (gnus-group-name-to-method to-newsgroup))
8978                        (gnus-subscribe-group to-newsgroup))
8979                   (error "Couldn't create group %s" to-newsgroup)))
8980           (error "No such group: %s" to-newsgroup)))
8981     to-newsgroup))
8982
8983 (defun gnus-summary-save-parts (type dir n reverse)
8984   "Save parts matching TYPE to DIR.
8985 If REVERSE, save parts that do not match TYPE."
8986   (interactive
8987    (list (read-string "Save parts of type: " "image/.*")
8988          (read-file-name "Save to directory: " t nil t)
8989          current-prefix-arg))
8990   (gnus-summary-iterate n
8991     (let ((gnus-display-mime-function nil)
8992           (gnus-inhibit-treatment t))
8993       (gnus-summary-select-article))
8994     (save-excursion
8995       (set-buffer gnus-article-buffer)
8996       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
8997         (when handles
8998           (gnus-summary-save-parts-1 type dir handles reverse)
8999           (mm-destroy-parts handles))))))
9000
9001 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9002   (if (stringp (car handle))
9003       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9004               (cdr handle))
9005     (when (if reverse
9006               (not (string-match type (mm-handle-media-type handle)))
9007             (string-match type (mm-handle-media-type handle)))
9008       (let ((file (expand-file-name
9009                    (file-name-nondirectory
9010                     (or
9011                      (mail-content-type-get
9012                       (mm-handle-disposition handle) 'filename)
9013                      (concat gnus-newsgroup-name "." gnus-current-article)))
9014                    dir)))
9015         (unless (file-exists-p file)
9016           (mm-save-part-to-file handle file))))))
9017
9018 ;; Summary extract commands
9019
9020 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9021   (let ((buffer-read-only nil)
9022         (article (gnus-summary-article-number))
9023         after-article b e)
9024     (unless (gnus-summary-goto-subject article)
9025       (error "No such article: %d" article))
9026     (gnus-summary-position-point)
9027     ;; If all commands are to be bunched up on one line, we collect
9028     ;; them here.
9029     (unless gnus-view-pseudos-separately
9030       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9031             files action)
9032         (while ps
9033           (setq action (cdr (assq 'action (car ps))))
9034           (setq files (list (cdr (assq 'name (car ps)))))
9035           (while (and ps (cdr ps)
9036                       (string= (or action "1")
9037                                (or (cdr (assq 'action (cadr ps))) "2")))
9038             (push (cdr (assq 'name (cadr ps))) files)
9039             (setcdr ps (cddr ps)))
9040           (when files
9041             (when (not (string-match "%s" action))
9042               (push " " files))
9043             (push " " files)
9044             (when (assq 'execute (car ps))
9045               (setcdr (assq 'execute (car ps))
9046                       (funcall (if (string-match "%s" action)
9047                                    'format 'concat)
9048                                action
9049                                (mapconcat
9050                                 (lambda (f)
9051                                   (if (equal f " ")
9052                                       f
9053                                     (mm-quote-arg f)))
9054                                 files " ")))))
9055           (setq ps (cdr ps)))))
9056     (if (and gnus-view-pseudos (not not-view))
9057         (while pslist
9058           (when (assq 'execute (car pslist))
9059             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9060                                   (eq gnus-view-pseudos 'not-confirm)))
9061           (setq pslist (cdr pslist)))
9062       (save-excursion
9063         (while pslist
9064           (setq after-article (or (cdr (assq 'article (car pslist)))
9065                                   (gnus-summary-article-number)))
9066           (gnus-summary-goto-subject after-article)
9067           (forward-line 1)
9068           (setq b (point))
9069           (insert "    " (file-name-nondirectory
9070                           (cdr (assq 'name (car pslist))))
9071                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9072           (setq e (point))
9073           (forward-line -1)             ; back to `b'
9074           (gnus-add-text-properties
9075            b (1- e) (list 'gnus-number gnus-reffed-article-number
9076                           gnus-mouse-face-prop gnus-mouse-face))
9077           (gnus-data-enter
9078            after-article gnus-reffed-article-number
9079            gnus-unread-mark b (car pslist) 0 (- e b))
9080           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9081           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9082           (setq pslist (cdr pslist)))))))
9083
9084 (defun gnus-pseudos< (p1 p2)
9085   (let ((c1 (cdr (assq 'action p1)))
9086         (c2 (cdr (assq 'action p2))))
9087     (and c1 c2 (string< c1 c2))))
9088
9089 (defun gnus-request-pseudo-article (props)
9090   (cond ((assq 'execute props)
9091          (gnus-execute-command (cdr (assq 'execute props)))))
9092   (let ((gnus-current-article (gnus-summary-article-number)))
9093     (gnus-run-hooks 'gnus-mark-article-hook)))
9094
9095 (defun gnus-execute-command (command &optional automatic)
9096   (save-excursion
9097     (gnus-article-setup-buffer)
9098     (set-buffer gnus-article-buffer)
9099     (setq buffer-read-only nil)
9100     (let ((command (if automatic command
9101                      (read-string "Command: " (cons command 0)))))
9102       (erase-buffer)
9103       (insert "$ " command "\n\n")
9104       (if gnus-view-pseudo-asynchronously
9105           (start-process "gnus-execute" (current-buffer) shell-file-name
9106                          shell-command-switch command)
9107         (call-process shell-file-name nil t nil
9108                       shell-command-switch command)))))
9109
9110 ;; Summary kill commands.
9111
9112 (defun gnus-summary-edit-global-kill (article)
9113   "Edit the \"global\" kill file."
9114   (interactive (list (gnus-summary-article-number)))
9115   (gnus-group-edit-global-kill article))
9116
9117 (defun gnus-summary-edit-local-kill ()
9118   "Edit a local kill file applied to the current newsgroup."
9119   (interactive)
9120   (setq gnus-current-headers (gnus-summary-article-header))
9121   (gnus-group-edit-local-kill
9122    (gnus-summary-article-number) gnus-newsgroup-name))
9123
9124 ;;; Header reading.
9125
9126 (defun gnus-read-header (id &optional header)
9127   "Read the headers of article ID and enter them into the Gnus system."
9128   (let ((group gnus-newsgroup-name)
9129         (gnus-override-method
9130          (and (gnus-news-group-p gnus-newsgroup-name)
9131               gnus-refer-article-method))
9132         where)
9133     ;; First we check to see whether the header in question is already
9134     ;; fetched.
9135     (if (stringp id)
9136         ;; This is a Message-ID.
9137         (setq header (or header (gnus-id-to-header id)))
9138       ;; This is an article number.
9139       (setq header (or header (gnus-summary-article-header id))))
9140     (if (and header
9141              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9142         ;; We have found the header.
9143         header
9144       ;; If this is a sparse article, we have to nix out its
9145       ;; previous entry in the thread hashtb.
9146       (when (and header
9147                  (gnus-summary-article-sparse-p (mail-header-number header)))
9148         (let* ((parent (gnus-parent-id (mail-header-references header)))
9149                (thread (and parent (gnus-id-to-thread parent))))
9150           (when thread
9151             (delq (assq header thread) thread))))
9152       ;; We have to really fetch the header to this article.
9153       (save-excursion
9154         (set-buffer nntp-server-buffer)
9155         (when (setq where (gnus-request-head id group))
9156           (nnheader-fold-continuation-lines)
9157           (goto-char (point-max))
9158           (insert ".\n")
9159           (goto-char (point-min))
9160           (insert "211 ")
9161           (princ (cond
9162                   ((numberp id) id)
9163                   ((cdr where) (cdr where))
9164                   (header (mail-header-number header))
9165                   (t gnus-reffed-article-number))
9166                  (current-buffer))
9167           (insert " Article retrieved.\n"))
9168         (if (or (not where)
9169                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9170             ()                          ; Malformed head.
9171           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9172             (when (and (stringp id)
9173                        (not (string= (gnus-group-real-name group)
9174                                      (car where))))
9175               ;; If we fetched by Message-ID and the article came
9176               ;; from a different group, we fudge some bogus article
9177               ;; numbers for this article.
9178               (mail-header-set-number header gnus-reffed-article-number))
9179             (save-excursion
9180               (set-buffer gnus-summary-buffer)
9181               (decf gnus-reffed-article-number)
9182               (gnus-remove-header (mail-header-number header))
9183               (push header gnus-newsgroup-headers)
9184               (setq gnus-current-headers header)
9185               (push (mail-header-number header) gnus-newsgroup-limit)))
9186           header)))))
9187
9188 (defun gnus-remove-header (number)
9189   "Remove header NUMBER from `gnus-newsgroup-headers'."
9190   (if (and gnus-newsgroup-headers
9191            (= number (mail-header-number (car gnus-newsgroup-headers))))
9192       (pop gnus-newsgroup-headers)
9193     (let ((headers gnus-newsgroup-headers))
9194       (while (and (cdr headers)
9195                   (not (= number (mail-header-number (cadr headers)))))
9196         (pop headers))
9197       (when (cdr headers)
9198         (setcdr headers (cddr headers))))))
9199
9200 ;;;
9201 ;;; summary highlights
9202 ;;;
9203
9204 (defun gnus-highlight-selected-summary ()
9205   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9206   ;; Highlight selected article in summary buffer
9207   (when gnus-summary-selected-face
9208     (save-excursion
9209       (let* ((beg (progn (beginning-of-line) (point)))
9210              (end (progn (end-of-line) (point)))
9211              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9212              (from (if (get-text-property beg gnus-mouse-face-prop)
9213                        beg
9214                      (or (next-single-property-change
9215                           beg gnus-mouse-face-prop nil end)
9216                          beg)))
9217              (to
9218               (if (= from end)
9219                   (- from 2)
9220                 (or (next-single-property-change
9221                      from gnus-mouse-face-prop nil end)
9222                     end))))
9223         ;; If no mouse-face prop on line we will have to = from = end,
9224         ;; so we highlight the entire line instead.
9225         (when (= (+ to 2) from)
9226           (setq from beg)
9227           (setq to end))
9228         (if gnus-newsgroup-selected-overlay
9229             ;; Move old overlay.
9230             (gnus-move-overlay
9231              gnus-newsgroup-selected-overlay from to (current-buffer))
9232           ;; Create new overlay.
9233           (gnus-overlay-put
9234            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9235            'face gnus-summary-selected-face))))))
9236
9237 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9238 (defun gnus-summary-highlight-line ()
9239   "Highlight current line according to `gnus-summary-highlight'."
9240   (let* ((list gnus-summary-highlight)
9241          (p (point))
9242          (end (progn (end-of-line) (point)))
9243          ;; now find out where the line starts and leave point there.
9244          (beg (progn (beginning-of-line) (point)))
9245          (article (gnus-summary-article-number))
9246          (score (or (cdr (assq (or article gnus-current-article)
9247                                gnus-newsgroup-scored))
9248                     gnus-summary-default-score 0))
9249          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9250          (inhibit-read-only t))
9251     ;; Eval the cars of the lists until we find a match.
9252     (let ((default gnus-summary-default-score))
9253       (while (and list
9254                   (not (eval (caar list))))
9255         (setq list (cdr list))))
9256     (let ((face (cdar list)))
9257       (unless (eq face (get-text-property beg 'face))
9258         (gnus-put-text-property-excluding-characters-with-faces
9259          beg end 'face
9260          (setq face (if (boundp face) (symbol-value face) face)))
9261         (when gnus-summary-highlight-line-function
9262           (funcall gnus-summary-highlight-line-function article face))))
9263     (goto-char p)))
9264
9265 (defun gnus-update-read-articles (group unread &optional compute)
9266   "Update the list of read articles in GROUP."
9267   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9268          (entry (gnus-gethash group gnus-newsrc-hashtb))
9269          (info (nth 2 entry))
9270          (prev 1)
9271          (unread (sort (copy-sequence unread) '<))
9272          read)
9273     (if (or (not info) (not active))
9274         ;; There is no info on this group if it was, in fact,
9275         ;; killed.  Gnus stores no information on killed groups, so
9276         ;; there's nothing to be done.
9277         ;; One could store the information somewhere temporarily,
9278         ;; perhaps...  Hmmm...
9279         ()
9280       ;; Remove any negative articles numbers.
9281       (while (and unread (< (car unread) 0))
9282         (setq unread (cdr unread)))
9283       ;; Remove any expired article numbers
9284       (while (and unread (< (car unread) (car active)))
9285         (setq unread (cdr unread)))
9286       ;; Compute the ranges of read articles by looking at the list of
9287       ;; unread articles.
9288       (while unread
9289         (when (/= (car unread) prev)
9290           (push (if (= prev (1- (car unread))) prev
9291                   (cons prev (1- (car unread))))
9292                 read))
9293         (setq prev (1+ (car unread)))
9294         (setq unread (cdr unread)))
9295       (when (<= prev (cdr active))
9296         (push (cons prev (cdr active)) read))
9297       (setq read (if (> (length read) 1) (nreverse read) read))
9298       (if compute
9299           read
9300         (save-excursion
9301           (set-buffer gnus-group-buffer)
9302           (gnus-undo-register
9303             `(progn
9304                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9305                (gnus-info-set-read ',info ',(gnus-info-read info))
9306                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9307                (gnus-group-update-group ,group t))))
9308        ;; Propagate the read marks to the backend.
9309        (if (gnus-check-backend-function 'request-set-mark group)
9310            (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9311                  (add (gnus-remove-from-range read (gnus-info-read info))))
9312              (when (or add del)
9313                (unless (gnus-check-group group)
9314                  (error "Can't open server for %s" group))
9315                (gnus-request-set-mark
9316                 group (delq nil (list (if add (list add 'add '(read)))
9317                                       (if del (list del 'del '(read)))))))))
9318         ;; Enter this list into the group info.
9319         (gnus-info-set-read info read)
9320         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9321         (gnus-get-unread-articles-in-group info (gnus-active group))
9322         t))))
9323
9324 (defun gnus-offer-save-summaries ()
9325   "Offer to save all active summary buffers."
9326   (save-excursion
9327     (let ((buflist (buffer-list))
9328           buffers bufname)
9329       ;; Go through all buffers and find all summaries.
9330       (while buflist
9331         (and (setq bufname (buffer-name (car buflist)))
9332              (string-match "Summary" bufname)
9333              (save-excursion
9334                (set-buffer bufname)
9335                ;; We check that this is, indeed, a summary buffer.
9336                (and (eq major-mode 'gnus-summary-mode)
9337                     ;; Also make sure this isn't bogus.
9338                     gnus-newsgroup-prepared
9339                     ;; Also make sure that this isn't a dead summary buffer.
9340                     (not gnus-dead-summary-mode)))
9341              (push bufname buffers))
9342         (setq buflist (cdr buflist)))
9343       ;; Go through all these summary buffers and offer to save them.
9344       (when buffers
9345         (map-y-or-n-p
9346          "Update summary buffer %s? "
9347          (lambda (buf)
9348            (switch-to-buffer buf)
9349            (gnus-summary-exit))
9350          buffers)))))
9351
9352 (defun gnus-summary-setup-default-charset ()
9353   "Setup newsgroup default charset."
9354   (if (equal gnus-newsgroup-name "nndraft:drafts")
9355       (setq gnus-newsgroup-charset nil)
9356   (let* ((name (and gnus-newsgroup-name
9357                    (gnus-group-real-name gnus-newsgroup-name)))
9358          (ignored-charsets 
9359           (or gnus-newsgroup-ephemeral-ignored-charsets
9360               (append
9361                (and gnus-newsgroup-name
9362                     (or (gnus-group-find-parameter gnus-newsgroup-name
9363                                                    'ignored-charsets t)
9364                         (let ((alist gnus-group-ignored-charsets-alist)
9365                            elem (charsets nil))
9366                           (while (setq elem (pop alist))
9367                             (when (and name
9368                                        (string-match (car elem) name))
9369                               (setq alist nil
9370                                     charsets (cdr elem))))
9371                           charsets))))
9372               gnus-newsgroup-ignored-charsets)))
9373     (setq gnus-newsgroup-charset
9374           (or gnus-newsgroup-ephemeral-charset
9375               (and gnus-newsgroup-name
9376                    (or (gnus-group-find-parameter gnus-newsgroup-name
9377                                                   'charset)
9378                        (let ((alist gnus-group-charset-alist)
9379                              elem (charset nil))
9380                          (while (setq elem (pop alist))
9381                            (when (and name
9382                                       (string-match (car elem) name))
9383                              (setq alist nil
9384                                    charset (cadr elem))))
9385                          charset)))
9386               gnus-default-charset))
9387     (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9388          ignored-charsets))))
9389
9390 ;;;
9391 ;;; Mime Commands
9392 ;;;
9393
9394 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9395   "Display the current article buffer fully MIME-buttonized.
9396 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9397 treated as multipart/mixed."
9398   (interactive "P")
9399   (require 'gnus-art)
9400   (let ((gnus-unbuttonized-mime-types nil)
9401         (gnus-mime-display-multipart-as-mixed show-all-parts))
9402     (gnus-summary-show-article)))
9403
9404 (defun gnus-summary-repair-multipart (article)
9405   "Add a Content-Type header to a multipart article without one."
9406   (interactive (list (gnus-summary-article-number)))
9407   (gnus-with-article article
9408     (message-narrow-to-head)
9409     (goto-char (point-max))
9410     (widen)
9411     (when (search-forward "\n--" nil t)
9412       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9413         (message-narrow-to-head)
9414         (message-remove-header "Mime-Version")
9415         (message-remove-header "Content-Type")
9416         (goto-char (point-max))
9417         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9418                         separator))
9419         (insert "Mime-Version: 1.0\n")
9420         (widen))))
9421   (let (gnus-mark-article-hook)
9422     (gnus-summary-select-article t t nil article)))
9423
9424 (defun gnus-summary-toggle-display-buttonized ()
9425   "Toggle the buttonizing of the article buffer."
9426   (interactive)
9427   (require 'gnus-art)
9428   (if (setq gnus-inhibit-mime-unbuttonizing
9429             (not gnus-inhibit-mime-unbuttonizing))
9430       (let ((gnus-unbuttonized-mime-types nil))
9431         (gnus-summary-show-article))
9432     (gnus-summary-show-article)))
9433
9434 ;;;
9435 ;;; with article
9436 ;;;
9437
9438 (defmacro gnus-with-article (article &rest forms)
9439   "Select ARTICLE and perform FORMS in the original article buffer.
9440 Then replace the article with the result."
9441   `(progn
9442      ;; We don't want the article to be marked as read.
9443      (let (gnus-mark-article-hook)
9444        (gnus-summary-select-article t t nil ,article))
9445      (set-buffer gnus-original-article-buffer)
9446      ,@forms
9447      (if (not (gnus-check-backend-function
9448                'request-replace-article (car gnus-article-current)))
9449          (gnus-message 5 "Read-only group; not replacing")
9450        (unless (gnus-request-replace-article
9451                 ,article (car gnus-article-current)
9452                 (current-buffer) t)
9453          (error "Couldn't replace article")))
9454      ;; The cache and backlog have to be flushed somewhat.
9455      (when gnus-keep-backlog
9456        (gnus-backlog-remove-article
9457         (car gnus-article-current) (cdr gnus-article-current)))
9458      (when gnus-use-cache
9459        (gnus-cache-update-article
9460         (car gnus-article-current) (cdr gnus-article-current)))))
9461
9462 (put 'gnus-with-article 'lisp-indent-function 1)
9463 (put 'gnus-with-article 'edebug-form-spec '(form body))
9464
9465 ;;;
9466 ;;; Generic summary marking commands
9467 ;;;
9468
9469 (defvar gnus-summary-marking-alist
9470   '((read gnus-del-mark "d")
9471     (unread gnus-unread-mark "u")
9472     (ticked gnus-ticked-mark "!")
9473     (dormant gnus-dormant-mark "?")
9474     (expirable gnus-expirable-mark "e"))
9475   "An alist of names/marks/keystrokes.")
9476
9477 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9478 (defvar gnus-summary-mark-map)
9479
9480 (defun gnus-summary-make-all-marking-commands ()
9481   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9482   (dolist (elem gnus-summary-marking-alist)
9483     (apply 'gnus-summary-make-marking-command elem)))
9484
9485 (defun gnus-summary-make-marking-command (name mark keystroke)
9486   (let ((map (make-sparse-keymap)))
9487     (define-key gnus-summary-generic-mark-map keystroke map)
9488     (dolist (lway `((next "next" next nil "n")
9489                     (next-unread "next unread" next t "N")
9490                     (prev "previous" prev nil "p")
9491                     (prev-unread "previous unread" prev t "P")
9492                     (nomove "" nil nil ,keystroke)))
9493       (let ((func (gnus-summary-make-marking-command-1
9494                    mark (car lway) lway name)))
9495         (setq func (eval func))
9496         (define-key map (nth 4 lway) func)))))
9497       
9498 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9499   `(defun ,(intern
9500             (format "gnus-summary-put-mark-as-%s%s"
9501                     name (if (eq way 'nomove)
9502                              ""
9503                            (concat "-" (symbol-name way)))))
9504      (n)
9505      ,(format
9506        "Mark the current article as %s%s.
9507 If N, the prefix, then repeat N times.
9508 If N is negative, move in reverse order.
9509 The difference between N and the actual number of articles marked is
9510 returned."
9511        name (cadr lway))
9512      (interactive "p")
9513      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9514     
9515 (defun gnus-summary-generic-mark (n mark move unread)
9516   "Mark N articles with MARK."
9517   (unless (eq major-mode 'gnus-summary-mode)
9518     (error "This command can only be used in the summary buffer"))
9519   (gnus-summary-show-thread)
9520   (let ((nummove
9521          (cond
9522           ((eq move 'next) 1)
9523           ((eq move 'prev) -1)
9524           (t 0))))
9525     (if (zerop nummove)
9526         (setq n 1)
9527       (when (< n 0)
9528         (setq n (abs n)
9529               nummove (* -1 nummove))))
9530     (while (and (> n 0)
9531                 (gnus-summary-mark-article nil mark)
9532                 (zerop (gnus-summary-next-subject nummove unread t)))
9533       (setq n (1- n)))
9534     (when (/= 0 n)
9535       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9536     (gnus-summary-recenter)
9537     (gnus-summary-position-point)
9538     (gnus-set-mode-line 'summary)
9539     n))
9540
9541 (gnus-summary-make-all-marking-commands)
9542
9543 (gnus-ems-redefine)
9544
9545 (provide 'gnus-sum)
9546
9547 (run-hooks 'gnus-sum-load-hook)
9548
9549 ;;; gnus-sum.el ends here