(gnus-summary-from-or-to-or-newsgroups): Do not decode extra header field.
[elisp/gnus.git-] / 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 'mcharset)
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
40
41 (defcustom gnus-kill-summary-on-exit t
42   "*If non-nil, kill the summary buffer when you exit from it.
43 If nil, the summary will become a \"*Dead Summary*\" buffer, and
44 it will be killed sometime later."
45   :group 'gnus-summary-exit
46   :type 'boolean)
47
48 (defcustom gnus-fetch-old-headers nil
49   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
50 If an unread article in the group refers to an older, already read (or
51 just marked as read) article, the old article will not normally be
52 displayed in the Summary buffer.  If this variable is non-nil, Gnus
53 will attempt to grab the headers to the old articles, and thereby
54 build complete threads.  If it has the value `some', only enough
55 headers to connect otherwise loose threads will be displayed.  This
56 variable can also be a number.  In that case, no more than that number
57 of old headers will be fetched.  If it has the value `invisible', all
58 old headers will be fetched, but none will be displayed.
59
60 The server has to support NOV for any of this to work."
61   :group 'gnus-thread
62   :type '(choice (const :tag "off" nil)
63                  (const some)
64                  number
65                  (sexp :menu-tag "other" t)))
66
67 (defcustom gnus-refer-thread-limit 200
68   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
69 If t, fetch all the available old headers."
70   :group 'gnus-thread
71   :type '(choice number
72                  (sexp :menu-tag "other" t)))
73
74 (defcustom gnus-summary-make-false-root 'adopt
75   "*nil means that Gnus won't gather loose threads.
76 If the root of a thread has expired or been read in a previous
77 session, the information necessary to build a complete thread has been
78 lost.  Instead of having many small sub-threads from this original thread
79 scattered all over the summary buffer, Gnus can gather them.
80
81 If non-nil, Gnus will try to gather all loose sub-threads from an
82 original thread into one large thread.
83
84 If this variable is non-nil, it should be one of `none', `adopt',
85 `dummy' or `empty'.
86
87 If this variable is `none', Gnus will not make a false root, but just
88 present the sub-threads after another.
89 If this variable is `dummy', Gnus will create a dummy root that will
90 have all the sub-threads as children.
91 If this variable is `adopt', Gnus will make one of the \"children\"
92 the parent and mark all the step-children as such.
93 If this variable is `empty', the \"children\" are printed with empty
94 subject fields.  (Or rather, they will be printed with a string
95 given by the `gnus-summary-same-subject' variable.)"
96   :group 'gnus-thread
97   :type '(choice (const :tag "off" nil)
98                  (const none)
99                  (const dummy)
100                  (const adopt)
101                  (const empty)))
102
103 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
104   "*A regexp to match subjects to be excluded from loose thread gathering.
105 As loose thread gathering is done on subjects only, that means that
106 there can be many false gatherings performed.  By rooting out certain
107 common subjects, gathering might become saner."
108   :group 'gnus-thread
109   :type 'regexp)
110
111 (defcustom gnus-summary-gather-subject-limit nil
112   "*Maximum length of subject comparisons when gathering loose threads.
113 Use nil to compare full subjects.  Setting this variable to a low
114 number will help gather threads that have been corrupted by
115 newsreaders chopping off subject lines, but it might also mean that
116 unrelated articles that have subject that happen to begin with the
117 same few characters will be incorrectly gathered.
118
119 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
120 comparing subjects."
121   :group 'gnus-thread
122   :type '(choice (const :tag "off" nil)
123                  (const fuzzy)
124                  (sexp :menu-tag "on" t)))
125
126 (defcustom gnus-simplify-subject-functions nil
127   "List of functions taking a string argument that simplify subjects.
128 The functions are applied recursively.
129
130 Useful functions to put in this list include: `gnus-simplify-subject-re',
131 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
132   :group 'gnus-thread
133   :type '(repeat function))
134
135 (defcustom gnus-simplify-ignored-prefixes nil
136   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
137   :group 'gnus-thread
138   :type '(choice (const :tag "off" nil)
139                  regexp))
140
141 (defcustom gnus-build-sparse-threads nil
142   "*If non-nil, fill in the gaps in threads.
143 If `some', only fill in the gaps that are needed to tie loose threads
144 together.  If `more', fill in all leaf nodes that Gnus can find.  If
145 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
146   :group 'gnus-thread
147   :type '(choice (const :tag "off" nil)
148                  (const some)
149                  (const more)
150                  (sexp :menu-tag "all" t)))
151
152 (defcustom gnus-summary-thread-gathering-function
153   'gnus-gather-threads-by-subject
154   "*Function used for gathering loose threads.
155 There are two pre-defined functions: `gnus-gather-threads-by-subject',
156 which only takes Subjects into consideration; and
157 `gnus-gather-threads-by-references', which compared the References
158 headers of the articles to find matches."
159   :group 'gnus-thread
160   :type '(radio (function-item gnus-gather-threads-by-subject)
161                 (function-item gnus-gather-threads-by-references)
162                 (function :tag "other")))
163
164 (defcustom gnus-summary-same-subject ""
165   "*String indicating that the current article has the same subject as the previous.
166 This variable will only be used if the value of
167 `gnus-summary-make-false-root' is `empty'."
168   :group 'gnus-summary-format
169   :type 'string)
170
171 (defcustom gnus-summary-goto-unread t
172   "*If t, marking commands will go to the next unread article.
173 If `never', commands that usually go to the next unread article, will
174 go to the next article, whether it is read or not.
175 If nil, only the marking commands will go to the next (un)read article."
176   :group 'gnus-summary-marks
177   :link '(custom-manual "(gnus)Setting Marks")
178   :type '(choice (const :tag "off" nil)
179                  (const never)
180                  (sexp :menu-tag "on" t)))
181
182 (defcustom gnus-summary-default-score 0
183   "*Default article score level.
184 All scores generated by the score files will be added to this score.
185 If this variable is nil, scoring will be disabled."
186   :group 'gnus-score-default
187   :type '(choice (const :tag "disable")
188                  integer))
189
190 (defcustom gnus-summary-zcore-fuzz 0
191   "*Fuzziness factor for the zcore in the summary buffer.
192 Articles with scores closer than this to `gnus-summary-default-score'
193 will not be marked."
194   :group 'gnus-summary-format
195   :type 'integer)
196
197 (defcustom gnus-simplify-subject-fuzzy-regexp nil
198   "*Strings to be removed when doing fuzzy matches.
199 This can either be a regular expression or list of regular expressions
200 that will be removed from subject strings if fuzzy subject
201 simplification is selected."
202   :group 'gnus-thread
203   :type '(repeat regexp))
204
205 (defcustom gnus-show-threads t
206   "*If non-nil, display threads in summary mode."
207   :group 'gnus-thread
208   :type 'boolean)
209
210 (defcustom gnus-thread-hide-subtree nil
211   "*If non-nil, hide all threads initially.
212 If threads are hidden, you have to run the command
213 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
214 to expose hidden threads."
215   :group 'gnus-thread
216   :type 'boolean)
217
218 (defcustom gnus-thread-hide-killed t
219   "*If non-nil, hide killed threads automatically."
220   :group 'gnus-thread
221   :type 'boolean)
222
223 (defcustom gnus-thread-ignore-subject t
224   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
225 If nil, articles that have different subjects from their parents will
226 start separate threads."
227   :group 'gnus-thread
228   :type 'boolean)
229
230 (defcustom gnus-thread-operation-ignore-subject t
231   "*If non-nil, subjects will be ignored when doing thread commands.
232 This affects commands like `gnus-summary-kill-thread' and
233 `gnus-summary-lower-thread'.
234
235 If this variable is nil, articles in the same thread with different
236 subjects will not be included in the operation in question.  If this
237 variable is `fuzzy', only articles that have subjects that are fuzzily
238 equal will be included."
239   :group 'gnus-thread
240   :type '(choice (const :tag "off" nil)
241                  (const fuzzy)
242                  (sexp :tag "on" t)))
243
244 (defcustom gnus-thread-indent-level 4
245   "*Number that says how much each sub-thread should be indented."
246   :group 'gnus-thread
247   :type 'integer)
248
249 (defcustom gnus-auto-extend-newsgroup t
250   "*If non-nil, extend newsgroup forward and backward when requested."
251   :group 'gnus-summary-choose
252   :type 'boolean)
253
254 (defcustom gnus-auto-select-first t
255   "*If nil, don't select the first unread article when entering a group.
256 If this variable is `best', select the highest-scored unread article
257 in the group.  If t, select the first unread article.
258
259 This variable can also be a function to place point on a likely
260 subject line.  Useful values include `gnus-summary-first-unread-subject',
261 `gnus-summary-first-unread-article' and
262 `gnus-summary-best-unread-article'.
263
264 If you want to prevent automatic selection of the first unread article
265 in some newsgroups, set the variable to nil in
266 `gnus-select-group-hook'."
267   :group 'gnus-group-select
268   :type '(choice (const :tag "none" nil)
269                  (const best)
270                  (sexp :menu-tag "first" t)
271                  (function-item gnus-summary-first-unread-subject)
272                  (function-item gnus-summary-first-unread-article)
273                  (function-item gnus-summary-best-unread-article)))
274
275 (defcustom gnus-auto-select-next t
276   "*If non-nil, offer to go to the next group from the end of the previous.
277 If the value is t and the next newsgroup is empty, Gnus will exit
278 summary mode and go back to group mode.  If the value is neither nil
279 nor t, Gnus will select the following unread newsgroup.  In
280 particular, if the value is the symbol `quietly', the next unread
281 newsgroup will be selected without any confirmation, and if it is
282 `almost-quietly', the next group will be selected without any
283 confirmation if you are located on the last article in the group.
284 Finally, if this variable is `slightly-quietly', the `Z n' command
285 will go to the next group without confirmation."
286   :group 'gnus-summary-maneuvering
287   :type '(choice (const :tag "off" nil)
288                  (const quietly)
289                  (const almost-quietly)
290                  (const slightly-quietly)
291                  (sexp :menu-tag "on" t)))
292
293 (defcustom gnus-auto-select-same nil
294   "*If non-nil, select the next article with the same subject.
295 If there are no more articles with the same subject, go to
296 the first unread article."
297   :group 'gnus-summary-maneuvering
298   :type 'boolean)
299
300 (defcustom gnus-summary-check-current nil
301   "*If non-nil, consider the current article when moving.
302 The \"unread\" movement commands will stay on the same line if the
303 current article is unread."
304   :group 'gnus-summary-maneuvering
305   :type 'boolean)
306
307 (defcustom gnus-auto-center-summary t
308   "*If non-nil, always center the current summary buffer.
309 In particular, if `vertical' do only vertical recentering.  If non-nil
310 and non-`vertical', do both horizontal and vertical recentering."
311   :group 'gnus-summary-maneuvering
312   :type '(choice (const :tag "none" nil)
313                  (const vertical)
314                  (integer :tag "height")
315                  (sexp :menu-tag "both" t)))
316
317 (defcustom gnus-show-all-headers nil
318   "*If non-nil, don't hide any headers."
319   :group 'gnus-article-hiding
320   :group 'gnus-article-headers
321   :type 'boolean)
322
323 (defcustom gnus-summary-ignore-duplicates nil
324   "*If non-nil, ignore articles with identical Message-ID headers."
325   :group 'gnus-summary
326   :type 'boolean)
327
328 (defcustom gnus-single-article-buffer t
329   "*If non-nil, display all articles in the same buffer.
330 If nil, each group will get its own article buffer."
331   :group 'gnus-article-various
332   :type 'boolean)
333
334 (defcustom gnus-break-pages t
335   "*If non-nil, do page breaking on articles.
336 The page delimiter is specified by the `gnus-page-delimiter'
337 variable."
338   :group 'gnus-article-various
339   :type 'boolean)
340
341 (defcustom gnus-move-split-methods nil
342   "*Variable used to suggest where articles are to be moved to.
343 It uses the same syntax as the `gnus-split-methods' variable."
344   :group 'gnus-summary-mail
345   :type '(repeat (choice (list :value (fun) function)
346                          (cons :value ("" "") regexp (repeat string))
347                          (sexp :value nil))))
348
349 (defcustom gnus-unread-mark ? ;Whitespace
350   "*Mark used for unread articles."
351   :group 'gnus-summary-marks
352   :type 'character)
353
354 (defcustom gnus-ticked-mark ?!
355   "*Mark used for ticked articles."
356   :group 'gnus-summary-marks
357   :type 'character)
358
359 (defcustom gnus-dormant-mark ??
360   "*Mark used for dormant articles."
361   :group 'gnus-summary-marks
362   :type 'character)
363
364 (defcustom gnus-del-mark ?r
365   "*Mark used for del'd articles."
366   :group 'gnus-summary-marks
367   :type 'character)
368
369 (defcustom gnus-read-mark ?R
370   "*Mark used for read articles."
371   :group 'gnus-summary-marks
372   :type 'character)
373
374 (defcustom gnus-expirable-mark ?E
375   "*Mark used for expirable articles."
376   :group 'gnus-summary-marks
377   :type 'character)
378
379 (defcustom gnus-killed-mark ?K
380   "*Mark used for killed articles."
381   :group 'gnus-summary-marks
382   :type 'character)
383
384 (defcustom gnus-souped-mark ?F
385   "*Mark used for killed articles."
386   :group 'gnus-summary-marks
387   :type 'character)
388
389 (defcustom gnus-kill-file-mark ?X
390   "*Mark used for articles killed by kill files."
391   :group 'gnus-summary-marks
392   :type 'character)
393
394 (defcustom gnus-low-score-mark ?Y
395   "*Mark used for articles with a low score."
396   :group 'gnus-summary-marks
397   :type 'character)
398
399 (defcustom gnus-catchup-mark ?C
400   "*Mark used for articles that are caught up."
401   :group 'gnus-summary-marks
402   :type 'character)
403
404 (defcustom gnus-replied-mark ?A
405   "*Mark used for articles that have been replied to."
406   :group 'gnus-summary-marks
407   :type 'character)
408
409 (defcustom gnus-cached-mark ?*
410   "*Mark used for articles that are in the cache."
411   :group 'gnus-summary-marks
412   :type 'character)
413
414 (defcustom gnus-saved-mark ?S
415   "*Mark used for articles that have been saved to."
416   :group 'gnus-summary-marks
417   :type 'character)
418
419 (defcustom gnus-ancient-mark ?O
420   "*Mark used for ancient articles."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-sparse-mark ?Q
425   "*Mark used for sparsely reffed articles."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-canceled-mark ?G
430   "*Mark used for canceled articles."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-duplicate-mark ?M
435   "*Mark used for duplicate articles."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-undownloaded-mark ?@
440   "*Mark used for articles that weren't downloaded."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-downloadable-mark ?%
445   "*Mark used for articles that are to be downloaded."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-unsendable-mark ?=
450   "*Mark used for articles that won't be sent."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-score-over-mark ?+
455   "*Score mark used for articles with high scores."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-score-below-mark ?-
460   "*Score mark used for articles with low scores."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-empty-thread-mark ? ;Whitespace
465   "*There is no thread under the article."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-not-empty-thread-mark ?=
470   "*There is a thread under the article."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-view-pseudo-asynchronously nil
475   "*If non-nil, Gnus will view pseudo-articles asynchronously."
476   :group 'gnus-extract-view
477   :type 'boolean)
478
479 (defcustom gnus-auto-expirable-marks
480   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
481         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
482         gnus-souped-mark gnus-duplicate-mark)
483   "*The list of marks converted into expiration if a group is auto-expirable."
484   :group 'gnus-summary
485   :type '(repeat character))
486
487 (defcustom gnus-inhibit-user-auto-expire t
488   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
489   :group 'gnus-summary
490   :type 'boolean)
491
492 (defcustom gnus-view-pseudos nil
493   "*If `automatic', pseudo-articles will be viewed automatically.
494 If `not-confirm', pseudos will be viewed automatically, and the user
495 will not be asked to confirm the command."
496   :group 'gnus-extract-view
497   :type '(choice (const :tag "off" nil)
498                  (const automatic)
499                  (const not-confirm)))
500
501 (defcustom gnus-view-pseudos-separately t
502   "*If non-nil, one pseudo-article will be created for each file to be viewed.
503 If nil, all files that use the same viewing command will be given as a
504 list of parameters to that command."
505   :group 'gnus-extract-view
506   :type 'boolean)
507
508 (defcustom gnus-insert-pseudo-articles t
509   "*If non-nil, insert pseudo-articles when decoding articles."
510   :group 'gnus-extract-view
511   :type 'boolean)
512
513 (defcustom gnus-summary-dummy-line-format
514   "  %(:                          :%) %S\n"
515   "*The format specification for the dummy roots in the summary buffer.
516 It works along the same lines as a normal formatting string,
517 with some simple extensions.
518
519 %S  The subject"
520   :group 'gnus-threading
521   :type 'string)
522
523 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
524   "*The format specification for the summary mode line.
525 It works along the same lines as a normal formatting string,
526 with some simple extensions:
527
528 %G  Group name
529 %p  Unprefixed group name
530 %A  Current article number
531 %z  Current article score
532 %V  Gnus version
533 %U  Number of unread articles in the group
534 %e  Number of unselected articles in the group
535 %Z  A string with unread/unselected article counts
536 %g  Shortish group name
537 %S  Subject of the current article
538 %u  User-defined spec
539 %s  Current score file name
540 %d  Number of dormant articles
541 %r  Number of articles that have been marked as read in this session
542 %E  Number of articles expunged by the score files"
543   :group 'gnus-summary-format
544   :type 'string)
545
546 (defcustom gnus-list-identifiers nil
547   "Regexp that matches list identifiers to be removed from subject.
548 This can also be a list of regexps."
549   :group 'gnus-summary-format
550   :group 'gnus-article-hiding
551   :type '(choice (const :tag "none" nil)
552                  (regexp :value ".*")
553                  (repeat :value (".*") regexp)))
554
555 (defcustom gnus-summary-mark-below 0
556   "*Mark all articles with a score below this variable as read.
557 This variable is local to each summary buffer and usually set by the
558 score file."
559   :group 'gnus-score-default
560   :type 'integer)
561
562 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
563   "*List of functions used for sorting articles in the summary buffer.
564 This variable is only used when not using a threaded display."
565   :group 'gnus-summary-sort
566   :type '(repeat (choice (function-item gnus-article-sort-by-number)
567                          (function-item gnus-article-sort-by-author)
568                          (function-item gnus-article-sort-by-subject)
569                          (function-item gnus-article-sort-by-date)
570                          (function-item gnus-article-sort-by-score)
571                          (function :tag "other"))))
572
573 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
574   "*List of functions used for sorting threads in the summary buffer.
575 By default, threads are sorted by article number.
576
577 Each function takes two threads and return non-nil if the first thread
578 should be sorted before the other.  If you use more than one function,
579 the primary sort function should be the last.  You should probably
580 always include `gnus-thread-sort-by-number' in the list of sorting
581 functions -- preferably first.
582
583 Ready-made functions include `gnus-thread-sort-by-number',
584 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
585 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
586 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
587   :group 'gnus-summary-sort
588   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
589                          (function-item gnus-thread-sort-by-author)
590                          (function-item gnus-thread-sort-by-subject)
591                          (function-item gnus-thread-sort-by-date)
592                          (function-item gnus-thread-sort-by-score)
593                          (function-item gnus-thread-sort-by-total-score)
594                          (function :tag "other"))))
595
596 (defcustom gnus-thread-score-function '+
597   "*Function used for calculating the total score of a thread.
598
599 The function is called with the scores of the article and each
600 subthread and should then return the score of the thread.
601
602 Some functions you can use are `+', `max', or `min'."
603   :group 'gnus-summary-sort
604   :type 'function)
605
606 (defcustom gnus-summary-expunge-below nil
607   "All articles that have a score less than this variable will be expunged.
608 This variable is local to the summary buffers."
609   :group 'gnus-score-default
610   :type '(choice (const :tag "off" nil)
611                  integer))
612
613 (defcustom gnus-thread-expunge-below nil
614   "All threads that have a total score less than this variable will be expunged.
615 See `gnus-thread-score-function' for en explanation of what a
616 \"thread score\" is.
617
618 This variable is local to the summary buffers."
619   :group 'gnus-threading
620   :group 'gnus-score-default
621   :type '(choice (const :tag "off" nil)
622                  integer))
623
624 (defcustom gnus-summary-mode-hook nil
625   "*A hook for Gnus summary mode.
626 This hook is run before any variables are set in the summary buffer."
627   :group 'gnus-summary-various
628   :type 'hook)
629
630 (defcustom gnus-summary-menu-hook nil
631   "*Hook run after the creation of the summary mode menu."
632   :group 'gnus-summary-visual
633   :type 'hook)
634
635 (defcustom gnus-summary-exit-hook nil
636   "*A hook called on exit from the summary buffer.
637 It will be called with point in the group buffer."
638   :group 'gnus-summary-exit
639   :type 'hook)
640
641 (defcustom gnus-summary-prepare-hook nil
642   "*A hook called after the summary buffer has been generated.
643 If you want to modify the summary buffer, you can use this hook."
644   :group 'gnus-summary-various
645   :type 'hook)
646
647 (defcustom gnus-summary-prepared-hook nil
648   "*A hook called as the last thing after the summary buffer has been generated."
649   :group 'gnus-summary-various
650   :type 'hook)
651
652 (defcustom gnus-summary-generate-hook nil
653   "*A hook run just before generating the summary buffer.
654 This hook is commonly used to customize threading variables and the
655 like."
656   :group 'gnus-summary-various
657   :type 'hook)
658
659 (defcustom gnus-select-group-hook nil
660   "*A hook called when a newsgroup is selected.
661
662 If you'd like to simplify subjects like the
663 `gnus-summary-next-same-subject' command does, you can use the
664 following hook:
665
666  (setq gnus-select-group-hook
667       (list
668         (lambda ()
669           (mapcar (lambda (header)
670                      (mail-header-set-subject
671                       header
672                       (gnus-simplify-subject
673                        (mail-header-subject header) 're-only)))
674                   gnus-newsgroup-headers))))"
675   :group 'gnus-group-select
676   :type 'hook)
677
678 (defcustom gnus-select-article-hook nil
679   "*A hook called when an article is selected."
680   :group 'gnus-summary-choose
681   :type 'hook)
682
683 (defcustom gnus-visual-mark-article-hook
684   (list 'gnus-highlight-selected-summary)
685   "*Hook run after selecting an article in the summary buffer.
686 It is meant to be used for highlighting the article in some way.  It
687 is not run if `gnus-visual' is nil."
688   :group 'gnus-summary-visual
689   :type 'hook)
690
691 (defcustom gnus-parse-headers-hook nil
692   "*A hook called before parsing the headers."
693   :group 'gnus-various
694   :type 'hook)
695
696 (defcustom gnus-exit-group-hook nil
697   "*A hook called when exiting (not quitting) summary mode."
698   :group 'gnus-various
699   :type 'hook)
700
701 (defcustom gnus-summary-update-hook
702   (list 'gnus-summary-highlight-line)
703   "*A hook called when a summary line is changed.
704 The hook will not be called if `gnus-visual' is nil.
705
706 The default function `gnus-summary-highlight-line' will
707 highlight the line according to the `gnus-summary-highlight'
708 variable."
709   :group 'gnus-summary-visual
710   :type 'hook)
711
712 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
713   "*A hook called when an article is selected for the first time.
714 The hook is intended to mark an article as read (or unread)
715 automatically when it is selected."
716   :group 'gnus-summary-choose
717   :type 'hook)
718
719 (defcustom gnus-group-no-more-groups-hook nil
720   "*A hook run when returning to group mode having no more (unread) groups."
721   :group 'gnus-group-select
722   :type 'hook)
723
724 (defcustom gnus-ps-print-hook nil
725   "*A hook run before ps-printing something from Gnus."
726   :group 'gnus-summary
727   :type 'hook)
728
729 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
730   "Face used for highlighting the current article in the summary buffer."
731   :group 'gnus-summary-visual
732   :type 'face)
733
734 (defcustom gnus-summary-highlight
735   '(((= mark gnus-canceled-mark)
736      . gnus-summary-cancelled-face)
737     ((and (> score default)
738           (or (= mark gnus-dormant-mark)
739               (= mark gnus-ticked-mark)))
740      . gnus-summary-high-ticked-face)
741     ((and (< score default)
742           (or (= mark gnus-dormant-mark)
743               (= mark gnus-ticked-mark)))
744      . gnus-summary-low-ticked-face)
745     ((or (= mark gnus-dormant-mark)
746          (= mark gnus-ticked-mark))
747      . gnus-summary-normal-ticked-face)
748     ((and (> score default) (= mark gnus-ancient-mark))
749      . gnus-summary-high-ancient-face)
750     ((and (< score default) (= mark gnus-ancient-mark))
751      . gnus-summary-low-ancient-face)
752     ((= mark gnus-ancient-mark)
753      . gnus-summary-normal-ancient-face)
754     ((and (> score default) (= mark gnus-unread-mark))
755      . gnus-summary-high-unread-face)
756     ((and (< score default) (= mark gnus-unread-mark))
757      . gnus-summary-low-unread-face)
758     ((= mark gnus-unread-mark)
759      . gnus-summary-normal-unread-face)
760     ((and (> score default) (memq mark (list gnus-downloadable-mark
761                                              gnus-undownloaded-mark)))
762      . gnus-summary-high-unread-face)
763     ((and (< score default) (memq mark (list gnus-downloadable-mark
764                                              gnus-undownloaded-mark)))
765      . gnus-summary-low-unread-face)
766     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
767      . gnus-summary-normal-unread-face)
768     ((> score default)
769      . gnus-summary-high-read-face)
770     ((< score default)
771      . gnus-summary-low-read-face)
772     (t
773      . gnus-summary-normal-read-face))
774   "*Controls the highlighting of summary buffer lines.
775
776 A list of (FORM . FACE) pairs.  When deciding how a a particular
777 summary line should be displayed, each form is evaluated.  The content
778 of the face field after the first true form is used.  You can change
779 how those summary lines are displayed, by editing the face field.
780
781 You can use the following variables in the FORM field.
782
783 score:   The articles score
784 default: The default article score.
785 below:   The score below which articles are automatically marked as read.
786 mark:    The articles mark."
787   :group 'gnus-summary-visual
788   :type '(repeat (cons (sexp :tag "Form" nil)
789                        face)))
790
791 (defcustom gnus-alter-header-function nil
792   "Function called to allow alteration of article header structures.
793 The function is called with one parameter, the article header vector,
794 which it may alter in any way.")
795
796 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
797   "Variable that says which function should be used to decode a string with encoded words.")
798
799 (defcustom gnus-extra-headers nil
800   "*Extra headers to parse."
801   :group 'gnus-summary
802   :type '(repeat symbol))
803
804 (defcustom gnus-ignored-from-addresses
805   (and user-mail-address (regexp-quote user-mail-address))
806   "*Regexp of From headers that may be suppressed in favor of To headers."
807   :group 'gnus-summary
808   :type 'regexp)
809
810 (defcustom gnus-group-charset-alist
811   `(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
812     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
813     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
814     ("^relcom\\>" koi8-r)
815     ("^fido7\\>" koi8-r)
816     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
817     ("^israel\\>" iso-8859-1)
818     ("^han\\>" euc-kr)
819     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
820     (".*" ,default-mime-charset))
821   "Alist of regexps (to match group names) and default charsets to be used when reading."
822   :type '(repeat (list (regexp :tag "Group")
823                        (symbol :tag "Charset")))
824   :group 'gnus-charset)
825
826 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
827   "List of charsets that should be ignored.
828 When these charsets are used in the \"charset\" parameter, the
829 default charset will be used instead."
830   :type '(repeat symbol)
831   :group 'gnus-charset)
832
833 (defcustom gnus-group-ignored-charsets-alist 
834   '(("alt\\.chinese\\.text" iso-8859-1))
835   "Alist of regexps (to match group names) and charsets that should be ignored.
836 When these charsets are used in the \"charset\" parameter, the
837 default charset will be used instead."
838   :type '(repeat (cons (regexp :tag "Group")
839                        (repeat symbol)))
840   :group 'gnus-charset)
841
842 (defcustom gnus-group-highlight-words-alist nil
843   "Alist of group regexps and highlight regexps.
844 This variable uses the same syntax as `gnus-emphasis-alist'."
845   :type '(repeat (cons (regexp :tag "Group")
846                        (repeat (list (regexp :tag "Highlight regexp")
847                                      (number :tag "Group for entire word" 0)
848                                      (number :tag "Group for displayed part" 0)
849                                      (symbol :tag "Face" 
850                                              gnus-emphasis-highlight-words)))))
851   :group 'gnus-summary-visual)
852
853 (defcustom gnus-summary-show-article-charset-alist
854   nil
855   "Alist of number and charset.
856 The article will be shown with the charset corresponding to the
857 numbered argument.
858 For example: ((1 . cn-gb-2312) (2 . big5))."
859   :type '(repeat (cons (number :tag "Argument" 1)
860                        (symbol :tag "Charset")))
861   :group 'gnus-charset)
862
863
864 ;;; Internal variables
865
866 (defvar gnus-article-mime-handles nil)
867 (defvar gnus-article-decoded-p nil)
868 (defvar gnus-scores-exclude-files nil)
869 (defvar gnus-page-broken nil)
870 (defvar gnus-inhibit-mime-unbuttonizing nil)
871
872 (defvar gnus-original-article nil)
873 (defvar gnus-article-internal-prepare-hook nil)
874 (defvar gnus-newsgroup-process-stack nil)
875
876 (defvar gnus-thread-indent-array nil)
877 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
878 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
879   "Function called to sort the articles within a thread after it has 
880 been gathered together.")
881
882 ;; Avoid highlighting in kill files.
883 (defvar gnus-summary-inhibit-highlight nil)
884 (defvar gnus-newsgroup-selected-overlay nil)
885 (defvar gnus-inhibit-limiting nil)
886 (defvar gnus-newsgroup-adaptive-score-file nil)
887 (defvar gnus-current-score-file nil)
888 (defvar gnus-current-move-group nil)
889 (defvar gnus-current-copy-group nil)
890 (defvar gnus-current-crosspost-group nil)
891
892 (defvar gnus-newsgroup-dependencies nil)
893 (defvar gnus-newsgroup-adaptive nil)
894 (defvar gnus-summary-display-article-function nil)
895 (defvar gnus-summary-highlight-line-function nil
896   "Function called after highlighting a summary line.")
897
898 (defvar gnus-summary-line-format-alist
899   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
900     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
901     (?s gnus-tmp-subject-or-nil ?s)
902     (?n gnus-tmp-name ?s)
903     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
904         ?s)
905     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
906             gnus-tmp-from) ?s)
907     (?F gnus-tmp-from ?s)
908     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
909     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
910     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
911     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
912     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
913     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
914     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
915     (?L gnus-tmp-lines ?d)
916     (?I gnus-tmp-indentation ?s)
917     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
918     (?R gnus-tmp-replied ?c)
919     (?\[ gnus-tmp-opening-bracket ?c)
920     (?\] gnus-tmp-closing-bracket ?c)
921     (?\> (make-string gnus-tmp-level ? ) ?s)
922     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
923     (?i gnus-tmp-score ?d)
924     (?z gnus-tmp-score-char ?c)
925     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
926     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
927     (?U gnus-tmp-unread ?c)
928     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
929     (?t (gnus-summary-number-of-articles-in-thread
930          (and (boundp 'thread) (car thread)) gnus-tmp-level)
931         ?d)
932     (?e (gnus-summary-number-of-articles-in-thread
933          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
934         ?c)
935     (?u gnus-tmp-user-defined ?s)
936     (?P (gnus-pick-line-number) ?d))
937   "An alist of format specifications that can appear in summary lines,
938 and what variables they correspond with, along with the type of the
939 variable (string, integer, character, etc).")
940
941 (defvar gnus-summary-dummy-line-format-alist
942   `((?S gnus-tmp-subject ?s)
943     (?N gnus-tmp-number ?d)
944     (?u gnus-tmp-user-defined ?s)))
945
946 (defvar gnus-summary-mode-line-format-alist
947   `((?G gnus-tmp-group-name ?s)
948     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
949     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
950     (?A gnus-tmp-article-number ?d)
951     (?Z gnus-tmp-unread-and-unselected ?s)
952     (?V gnus-version ?s)
953     (?U gnus-tmp-unread-and-unticked ?d)
954     (?S gnus-tmp-subject ?s)
955     (?e gnus-tmp-unselected ?d)
956     (?u gnus-tmp-user-defined ?s)
957     (?d (length gnus-newsgroup-dormant) ?d)
958     (?t (length gnus-newsgroup-marked) ?d)
959     (?r (length gnus-newsgroup-reads) ?d)
960     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
961     (?E gnus-newsgroup-expunged-tally ?d)
962     (?s (gnus-current-score-file-nondirectory) ?s)))
963
964 (defvar gnus-last-search-regexp nil
965   "Default regexp for article search command.")
966
967 (defvar gnus-last-shell-command nil
968   "Default shell command on article.")
969
970 (defvar gnus-newsgroup-begin nil)
971 (defvar gnus-newsgroup-end nil)
972 (defvar gnus-newsgroup-last-rmail nil)
973 (defvar gnus-newsgroup-last-mail nil)
974 (defvar gnus-newsgroup-last-folder nil)
975 (defvar gnus-newsgroup-last-file nil)
976 (defvar gnus-newsgroup-auto-expire nil)
977 (defvar gnus-newsgroup-active nil)
978
979 (defvar gnus-newsgroup-data nil)
980 (defvar gnus-newsgroup-data-reverse nil)
981 (defvar gnus-newsgroup-limit nil)
982 (defvar gnus-newsgroup-limits nil)
983
984 (defvar gnus-newsgroup-unreads nil
985   "List of unread articles in the current newsgroup.")
986
987 (defvar gnus-newsgroup-unselected nil
988   "List of unselected unread articles in the current newsgroup.")
989
990 (defvar gnus-newsgroup-reads nil
991   "Alist of read articles and article marks in the current newsgroup.")
992
993 (defvar gnus-newsgroup-expunged-tally nil)
994
995 (defvar gnus-newsgroup-marked nil
996   "List of ticked articles in the current newsgroup (a subset of unread art).")
997
998 (defvar gnus-newsgroup-killed nil
999   "List of ranges of articles that have been through the scoring process.")
1000
1001 (defvar gnus-newsgroup-cached nil
1002   "List of articles that come from the article cache.")
1003
1004 (defvar gnus-newsgroup-saved nil
1005   "List of articles that have been saved.")
1006
1007 (defvar gnus-newsgroup-kill-headers nil)
1008
1009 (defvar gnus-newsgroup-replied nil
1010   "List of articles that have been replied to in the current newsgroup.")
1011
1012 (defvar gnus-newsgroup-expirable nil
1013   "List of articles in the current newsgroup that can be expired.")
1014
1015 (defvar gnus-newsgroup-processable nil
1016   "List of articles in the current newsgroup that can be processed.")
1017
1018 (defvar gnus-newsgroup-downloadable nil
1019   "List of articles in the current newsgroup that can be processed.")
1020
1021 (defvar gnus-newsgroup-undownloaded nil
1022   "List of articles in the current newsgroup that haven't been downloaded..")
1023
1024 (defvar gnus-newsgroup-unsendable nil
1025   "List of articles in the current newsgroup that won't be sent.")
1026
1027 (defvar gnus-newsgroup-bookmarks nil
1028   "List of articles in the current newsgroup that have bookmarks.")
1029
1030 (defvar gnus-newsgroup-dormant nil
1031   "List of dormant articles in the current newsgroup.")
1032
1033 (defvar gnus-newsgroup-scored nil
1034   "List of scored articles in the current newsgroup.")
1035
1036 (defvar gnus-newsgroup-headers nil
1037   "List of article headers in the current newsgroup.")
1038
1039 (defvar gnus-newsgroup-threads nil)
1040
1041 (defvar gnus-newsgroup-prepared nil
1042   "Whether the current group has been prepared properly.")
1043
1044 (defvar gnus-newsgroup-ancient nil
1045   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1046
1047 (defvar gnus-newsgroup-sparse nil)
1048
1049 (defvar gnus-current-article nil)
1050 (defvar gnus-article-current nil)
1051 (defvar gnus-current-headers nil)
1052 (defvar gnus-have-all-headers nil)
1053 (defvar gnus-last-article nil)
1054 (defvar gnus-newsgroup-history nil)
1055 (defvar gnus-newsgroup-charset nil)
1056 (defvar gnus-newsgroup-ephemeral-charset nil)
1057 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1058
1059 (defconst gnus-summary-local-variables
1060   '(gnus-newsgroup-name
1061     gnus-newsgroup-begin gnus-newsgroup-end
1062     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1063     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1064     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1065     gnus-newsgroup-unselected gnus-newsgroup-marked
1066     gnus-newsgroup-reads gnus-newsgroup-saved
1067     gnus-newsgroup-replied gnus-newsgroup-expirable
1068     gnus-newsgroup-processable gnus-newsgroup-killed
1069     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1070     gnus-newsgroup-unsendable
1071     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1072     gnus-newsgroup-headers gnus-newsgroup-threads
1073     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1074     gnus-current-article gnus-current-headers gnus-have-all-headers
1075     gnus-last-article gnus-article-internal-prepare-hook
1076     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1077     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1078     gnus-thread-expunge-below
1079     gnus-score-alist gnus-current-score-file
1080     (gnus-summary-expunge-below . global)
1081     (gnus-summary-mark-below . global)
1082     gnus-newsgroup-active gnus-scores-exclude-files
1083     gnus-newsgroup-history gnus-newsgroup-ancient
1084     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1085     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1086     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1087     (gnus-newsgroup-expunged-tally . 0)
1088     gnus-cache-removable-articles gnus-newsgroup-cached
1089     gnus-newsgroup-data gnus-newsgroup-data-reverse
1090     gnus-newsgroup-limit gnus-newsgroup-limits
1091     gnus-newsgroup-charset)
1092   "Variables that are buffer-local to the summary buffers.")
1093
1094 ;; Byte-compiler warning.
1095 (defvar gnus-article-mode-map)
1096
1097 ;; MIME stuff.
1098
1099 (defvar gnus-decode-encoded-word-methods
1100   '(mail-decode-encoded-word-string)
1101   "List of methods used to decode encoded words.
1102
1103 This variable is a list of FUNCTION or (REGEXP . FUNCTION). If item is
1104 FUNCTION, FUNCTION will be apply to all newsgroups. If item is a
1105 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1106 whose names match REGEXP.
1107
1108 For example:
1109 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1110  mail-decode-encoded-word-string
1111  (\"chinese\" . rfc1843-decode-string))
1112 ")
1113
1114 (defvar gnus-decode-encoded-word-methods-cache nil)
1115
1116 (defun gnus-multi-decode-encoded-word-string (string)
1117   "Apply the functions from `gnus-encoded-word-methods' that match."
1118   (unless (and gnus-decode-encoded-word-methods-cache
1119                (eq gnus-newsgroup-name
1120                    (car gnus-decode-encoded-word-methods-cache)))
1121     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1122     (mapc '(lambda (x)
1123              (if (symbolp x)
1124                  (nconc gnus-decode-encoded-word-methods-cache (list x))
1125                (if (and gnus-newsgroup-name
1126                         (string-match (car x) gnus-newsgroup-name))
1127                    (nconc gnus-decode-encoded-word-methods-cache
1128                           (list (cdr x))))))
1129           gnus-decode-encoded-word-methods))
1130   (let ((xlist gnus-decode-encoded-word-methods-cache))
1131     (pop xlist)
1132     (while xlist
1133       (setq string (funcall (pop xlist) string))))
1134   string)
1135
1136 ;; Subject simplification.
1137
1138 (defun gnus-simplify-whitespace (str)
1139   "Remove excessive whitespace."
1140   (let ((mystr str))
1141     ;; Multiple spaces.
1142     (while (string-match "[ \t][ \t]+" mystr)
1143       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1144                           " "
1145                           (substring mystr (match-end 0)))))
1146     ;; Leading spaces.
1147     (when (string-match "^[ \t]+" mystr)
1148       (setq mystr (substring mystr (match-end 0))))
1149     ;; Trailing spaces.
1150     (when (string-match "[ \t]+$" mystr)
1151       (setq mystr (substring mystr 0 (match-beginning 0))))
1152     mystr))
1153
1154 (defsubst gnus-simplify-subject-re (subject)
1155   "Remove \"Re:\" from subject lines."
1156   (if (string-match "^[Rr][Ee]: *" subject)
1157       (substring subject (match-end 0))
1158     subject))
1159
1160 (defun gnus-simplify-subject (subject &optional re-only)
1161   "Remove `Re:' and words in parentheses.
1162 If RE-ONLY is non-nil, strip leading `Re:'s only."
1163   (let ((case-fold-search t))           ;Ignore case.
1164     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1165     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1166       (setq subject (substring subject (match-end 0))))
1167     ;; Remove uninteresting prefixes.
1168     (when (and (not re-only)
1169                gnus-simplify-ignored-prefixes
1170                (string-match gnus-simplify-ignored-prefixes subject))
1171       (setq subject (substring subject (match-end 0))))
1172     ;; Remove words in parentheses from end.
1173     (unless re-only
1174       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1175         (setq subject (substring subject 0 (match-beginning 0)))))
1176     ;; Return subject string.
1177     subject))
1178
1179 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1180 ;; all whitespace.
1181 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1182   (goto-char (point-min))
1183   (while (re-search-forward regexp nil t)
1184       (replace-match (or newtext ""))))
1185
1186 (defun gnus-simplify-buffer-fuzzy ()
1187   "Simplify string in the buffer fuzzily.
1188 The string in the accessible portion of the current buffer is simplified.
1189 It is assumed to be a single-line subject.
1190 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1191 matter is removed.  Additional things can be deleted by setting
1192 gnus-simplify-subject-fuzzy-regexp."
1193   (let ((case-fold-search t)
1194         (modified-tick))
1195     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1196
1197     (while (not (eq modified-tick (buffer-modified-tick)))
1198       (setq modified-tick (buffer-modified-tick))
1199       (cond
1200        ((listp gnus-simplify-subject-fuzzy-regexp)
1201         (mapcar 'gnus-simplify-buffer-fuzzy-step
1202                 gnus-simplify-subject-fuzzy-regexp))
1203        (gnus-simplify-subject-fuzzy-regexp
1204         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1205       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1206       (gnus-simplify-buffer-fuzzy-step
1207        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1208       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1209
1210     (goto-char (point-min))
1211     (while (re-search-forward " +" nil t)
1212       (delete-region (match-beginning 0) (match-end 0))
1213       (or (bolp) (eobp)
1214           (> (char-after) 127) (> (char-before) 127)
1215           (insert " ")))
1216     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1217     (gnus-simplify-buffer-fuzzy-step " $")
1218     (gnus-simplify-buffer-fuzzy-step "^ +")))
1219
1220 (defun gnus-simplify-subject-fuzzy (subject)
1221   "Simplify a subject string fuzzily.
1222 See `gnus-simplify-buffer-fuzzy' for details."
1223   (let ((fuzzy-regexp gnus-simplify-subject-fuzzy-regexp)) ; Group parameter.
1224     ;; Remove uninteresting prefixes.
1225     (when (and gnus-simplify-ignored-prefixes
1226                (let ((case-fold-search t))
1227                  (string-match gnus-simplify-ignored-prefixes subject)))
1228       (setq subject (substring subject (match-end 0))))
1229     (save-excursion
1230       (gnus-set-work-buffer)
1231       (let ((case-fold-search t)
1232             (gnus-simplify-subject-fuzzy-regexp fuzzy-regexp))
1233         (insert subject)
1234         (inline (gnus-simplify-buffer-fuzzy))
1235         (buffer-string)))))
1236
1237 (defsubst gnus-simplify-subject-fully (subject)
1238   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1239   (cond
1240    (gnus-simplify-subject-functions
1241     (gnus-map-function gnus-simplify-subject-functions subject))
1242    ((null gnus-summary-gather-subject-limit)
1243     (gnus-simplify-subject-re subject))
1244    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1245     (gnus-simplify-subject-fuzzy subject))
1246    ((numberp gnus-summary-gather-subject-limit)
1247     (gnus-limit-string (gnus-simplify-subject-re subject)
1248                        gnus-summary-gather-subject-limit))
1249    (t
1250     subject)))
1251
1252 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1253   "Check whether two subjects are equal.
1254 If optional argument simple-first is t, first argument is already
1255 simplified."
1256   (cond
1257    ((null simple-first)
1258     (equal (gnus-simplify-subject-fully s1)
1259            (gnus-simplify-subject-fully s2)))
1260    (t
1261     (equal s1
1262            (gnus-simplify-subject-fully s2)))))
1263
1264 (defun gnus-summary-bubble-group ()
1265   "Increase the score of the current group.
1266 This is a handy function to add to `gnus-summary-exit-hook' to
1267 increase the score of each group you read."
1268   (gnus-group-add-score gnus-newsgroup-name))
1269
1270 \f
1271 ;;;
1272 ;;; Gnus summary mode
1273 ;;;
1274
1275 (put 'gnus-summary-mode 'mode-class 'special)
1276
1277 (when t
1278   ;; Non-orthogonal keys
1279
1280   (gnus-define-keys gnus-summary-mode-map
1281     " " gnus-summary-next-page
1282     "\177" gnus-summary-prev-page
1283     [delete] gnus-summary-prev-page
1284     [backspace] gnus-summary-prev-page
1285     "\r" gnus-summary-scroll-up
1286     "\M-\r" gnus-summary-scroll-down
1287     "n" gnus-summary-next-unread-article
1288     "p" gnus-summary-prev-unread-article
1289     "N" gnus-summary-next-article
1290     "P" gnus-summary-prev-article
1291     "\M-\C-n" gnus-summary-next-same-subject
1292     "\M-\C-p" gnus-summary-prev-same-subject
1293     "\M-n" gnus-summary-next-unread-subject
1294     "\M-p" gnus-summary-prev-unread-subject
1295     "." gnus-summary-first-unread-article
1296     "," gnus-summary-best-unread-article
1297     "\M-s" gnus-summary-search-article-forward
1298     "\M-r" gnus-summary-search-article-backward
1299     "<" gnus-summary-beginning-of-article
1300     ">" gnus-summary-end-of-article
1301     "j" gnus-summary-goto-article
1302     "^" gnus-summary-refer-parent-article
1303     "\M-^" gnus-summary-refer-article
1304     "u" gnus-summary-tick-article-forward
1305     "!" gnus-summary-tick-article-forward
1306     "U" gnus-summary-tick-article-backward
1307     "d" gnus-summary-mark-as-read-forward
1308     "D" gnus-summary-mark-as-read-backward
1309     "E" gnus-summary-mark-as-expirable
1310     "\M-u" gnus-summary-clear-mark-forward
1311     "\M-U" gnus-summary-clear-mark-backward
1312     "k" gnus-summary-kill-same-subject-and-select
1313     "\C-k" gnus-summary-kill-same-subject
1314     "\M-\C-k" gnus-summary-kill-thread
1315     "\M-\C-l" gnus-summary-lower-thread
1316     "e" gnus-summary-edit-article
1317     "#" gnus-summary-mark-as-processable
1318     "\M-#" gnus-summary-unmark-as-processable
1319     "\M-\C-t" gnus-summary-toggle-threads
1320     "\M-\C-s" gnus-summary-show-thread
1321     "\M-\C-h" gnus-summary-hide-thread
1322     "\M-\C-f" gnus-summary-next-thread
1323     "\M-\C-b" gnus-summary-prev-thread
1324     "\M-\C-u" gnus-summary-up-thread
1325     "\M-\C-d" gnus-summary-down-thread
1326     "&" gnus-summary-execute-command
1327     "c" gnus-summary-catchup-and-exit
1328     "\C-w" gnus-summary-mark-region-as-read
1329     "\C-t" gnus-summary-toggle-truncation
1330     "?" gnus-summary-mark-as-dormant
1331     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1332     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1333     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1334     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1335     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1336     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1337     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1338     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1339     "=" gnus-summary-expand-window
1340     "\C-x\C-s" gnus-summary-reselect-current-group
1341     "\M-g" gnus-summary-rescan-group
1342     "w" gnus-summary-stop-page-breaking
1343     "\C-c\C-r" gnus-summary-caesar-message
1344     "f" gnus-summary-followup
1345     "F" gnus-summary-followup-with-original
1346     "C" gnus-summary-cancel-article
1347     "r" gnus-summary-reply
1348     "R" gnus-summary-reply-with-original
1349     "\C-c\C-f" gnus-summary-mail-forward
1350     "o" gnus-summary-save-article
1351     "\C-o" gnus-summary-save-article-mail
1352     "|" gnus-summary-pipe-output
1353     "\M-k" gnus-summary-edit-local-kill
1354     "\M-K" gnus-summary-edit-global-kill
1355     ;; "V" gnus-version
1356     "\C-c\C-d" gnus-summary-describe-group
1357     "q" gnus-summary-exit
1358     "Q" gnus-summary-exit-no-update
1359     "\C-c\C-i" gnus-info-find-node
1360     gnus-mouse-2 gnus-mouse-pick-article
1361     "m" gnus-summary-mail-other-window
1362     "a" gnus-summary-post-news
1363     "x" gnus-summary-limit-to-unread
1364     "s" gnus-summary-isearch-article
1365     "t" gnus-summary-toggle-header
1366     "g" gnus-summary-show-article
1367     "l" gnus-summary-goto-last-article
1368     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1369     "\C-d" gnus-summary-enter-digest-group
1370     "\M-\C-d" gnus-summary-read-document
1371     "\M-\C-e" gnus-summary-edit-parameters
1372     "\M-\C-g" gnus-summary-customize-parameters
1373     "\C-c\C-b" gnus-bug
1374     "*" gnus-cache-enter-article
1375     "\M-*" gnus-cache-remove-article
1376     "\M-&" gnus-summary-universal-argument
1377     "\C-l" gnus-recenter
1378     "I" gnus-summary-increase-score
1379     "L" gnus-summary-lower-score
1380     "\M-i" gnus-symbolic-argument
1381     "h" gnus-summary-select-article-buffer
1382
1383     "b" gnus-article-view-part
1384     "\M-t" gnus-summary-toggle-display-buttonized
1385
1386     "V" gnus-summary-score-map
1387     "X" gnus-uu-extract-map
1388     "S" gnus-summary-send-map)
1389
1390   ;; Sort of orthogonal keymap
1391   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1392     "t" gnus-summary-tick-article-forward
1393     "!" gnus-summary-tick-article-forward
1394     "d" gnus-summary-mark-as-read-forward
1395     "r" gnus-summary-mark-as-read-forward
1396     "c" gnus-summary-clear-mark-forward
1397     " " gnus-summary-clear-mark-forward
1398     "e" gnus-summary-mark-as-expirable
1399     "x" gnus-summary-mark-as-expirable
1400     "?" gnus-summary-mark-as-dormant
1401     "b" gnus-summary-set-bookmark
1402     "B" gnus-summary-remove-bookmark
1403     "#" gnus-summary-mark-as-processable
1404     "\M-#" gnus-summary-unmark-as-processable
1405     "S" gnus-summary-limit-include-expunged
1406     "C" gnus-summary-catchup
1407     "H" gnus-summary-catchup-to-here
1408     "\C-c" gnus-summary-catchup-all
1409     "k" gnus-summary-kill-same-subject-and-select
1410     "K" gnus-summary-kill-same-subject
1411     "P" gnus-uu-mark-map)
1412
1413   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1414     "c" gnus-summary-clear-above
1415     "u" gnus-summary-tick-above
1416     "m" gnus-summary-mark-above
1417     "k" gnus-summary-kill-below)
1418
1419   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1420     "/" gnus-summary-limit-to-subject
1421     "n" gnus-summary-limit-to-articles
1422     "w" gnus-summary-pop-limit
1423     "s" gnus-summary-limit-to-subject
1424     "a" gnus-summary-limit-to-author
1425     "u" gnus-summary-limit-to-unread
1426     "m" gnus-summary-limit-to-marks
1427     "M" gnus-summary-limit-exclude-marks
1428     "v" gnus-summary-limit-to-score
1429     "*" gnus-summary-limit-include-cached
1430     "D" gnus-summary-limit-include-dormant
1431     "T" gnus-summary-limit-include-thread
1432     "d" gnus-summary-limit-exclude-dormant
1433     "t" gnus-summary-limit-to-age
1434     "x" gnus-summary-limit-to-extra 
1435     "E" gnus-summary-limit-include-expunged
1436     "c" gnus-summary-limit-exclude-childless-dormant
1437     "C" gnus-summary-limit-mark-excluded-as-read)
1438
1439   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1440     "n" gnus-summary-next-unread-article
1441     "p" gnus-summary-prev-unread-article
1442     "N" gnus-summary-next-article
1443     "P" gnus-summary-prev-article
1444     "\C-n" gnus-summary-next-same-subject
1445     "\C-p" gnus-summary-prev-same-subject
1446     "\M-n" gnus-summary-next-unread-subject
1447     "\M-p" gnus-summary-prev-unread-subject
1448     "f" gnus-summary-first-unread-article
1449     "b" gnus-summary-best-unread-article
1450     "j" gnus-summary-goto-article
1451     "g" gnus-summary-goto-subject
1452     "l" gnus-summary-goto-last-article
1453     "o" gnus-summary-pop-article)
1454
1455   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1456     "k" gnus-summary-kill-thread
1457     "l" gnus-summary-lower-thread
1458     "i" gnus-summary-raise-thread
1459     "T" gnus-summary-toggle-threads
1460     "t" gnus-summary-rethread-current
1461     "^" gnus-summary-reparent-thread
1462     "s" gnus-summary-show-thread
1463     "S" gnus-summary-show-all-threads
1464     "h" gnus-summary-hide-thread
1465     "H" gnus-summary-hide-all-threads
1466     "n" gnus-summary-next-thread
1467     "p" gnus-summary-prev-thread
1468     "u" gnus-summary-up-thread
1469     "o" gnus-summary-top-thread
1470     "d" gnus-summary-down-thread
1471     "#" gnus-uu-mark-thread
1472     "\M-#" gnus-uu-unmark-thread)
1473
1474   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1475     "g" gnus-summary-prepare
1476     "c" gnus-summary-insert-cached-articles)
1477
1478   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1479     "c" gnus-summary-catchup-and-exit
1480     "C" gnus-summary-catchup-all-and-exit
1481     "E" gnus-summary-exit-no-update
1482     "Q" gnus-summary-exit
1483     "Z" gnus-summary-exit
1484     "n" gnus-summary-catchup-and-goto-next-group
1485     "R" gnus-summary-reselect-current-group
1486     "G" gnus-summary-rescan-group
1487     "N" gnus-summary-next-group
1488     "s" gnus-summary-save-newsrc
1489     "P" gnus-summary-prev-group)
1490
1491   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1492     " " gnus-summary-next-page
1493     "n" gnus-summary-next-page
1494     "\177" gnus-summary-prev-page
1495     [delete] gnus-summary-prev-page
1496     "p" gnus-summary-prev-page
1497     "\r" gnus-summary-scroll-up
1498     "\M-\r" gnus-summary-scroll-down
1499     "<" gnus-summary-beginning-of-article
1500     ">" gnus-summary-end-of-article
1501     "b" gnus-summary-beginning-of-article
1502     "e" gnus-summary-end-of-article
1503     "^" gnus-summary-refer-parent-article
1504     "r" gnus-summary-refer-parent-article
1505     "D" gnus-summary-enter-digest-group
1506     "R" gnus-summary-refer-references
1507     "T" gnus-summary-refer-thread
1508     "g" gnus-summary-show-article
1509     "s" gnus-summary-isearch-article
1510     "P" gnus-summary-print-article
1511     "t" gnus-article-babel)
1512
1513   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1514     "b" gnus-article-add-buttons
1515     "B" gnus-article-add-buttons-to-head
1516     "o" gnus-article-treat-overstrike
1517     "e" gnus-article-emphasize
1518     "w" gnus-article-fill-cited-article
1519     "Q" gnus-article-fill-long-lines
1520     "C" gnus-article-capitalize-sentences
1521     "c" gnus-article-remove-cr
1522     "q" gnus-article-de-quoted-unreadable
1523     "f" gnus-article-display-x-face
1524     "l" gnus-summary-stop-page-breaking
1525     "r" gnus-summary-caesar-message
1526     "t" gnus-article-hide-headers
1527     "v" gnus-summary-verbose-headers
1528     "H" gnus-article-strip-headers-in-body
1529     "d" gnus-article-treat-dumbquotes)
1530
1531   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1532     "a" gnus-article-hide
1533     "h" gnus-article-hide-headers
1534     "b" gnus-article-hide-boring-headers
1535     "s" gnus-article-hide-signature
1536     "c" gnus-article-hide-citation
1537     "C" gnus-article-hide-citation-in-followups
1538     "l" gnus-article-hide-list-identifiers
1539     "p" gnus-article-hide-pgp
1540     "B" gnus-article-strip-banner
1541     "P" gnus-article-hide-pem
1542     "\C-c" gnus-article-hide-citation-maybe)
1543
1544   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1545     "a" gnus-article-highlight
1546     "h" gnus-article-highlight-headers
1547     "c" gnus-article-highlight-citation
1548     "s" gnus-article-highlight-signature)
1549
1550   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1551     "w" gnus-article-decode-mime-words
1552     "c" gnus-article-decode-charset
1553     "v" gnus-mime-view-all-parts
1554     "b" gnus-article-view-part)
1555
1556   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1557     "z" gnus-article-date-ut
1558     "u" gnus-article-date-ut
1559     "l" gnus-article-date-local
1560     "e" gnus-article-date-lapsed
1561     "o" gnus-article-date-original
1562     "i" gnus-article-date-iso8601
1563     "s" gnus-article-date-user)
1564
1565   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1566     "t" gnus-article-remove-trailing-blank-lines
1567     "l" gnus-article-strip-leading-blank-lines
1568     "m" gnus-article-strip-multiple-blank-lines
1569     "a" gnus-article-strip-blank-lines
1570     "A" gnus-article-strip-all-blank-lines
1571     "s" gnus-article-strip-leading-space
1572     "e" gnus-article-strip-trailing-space)
1573
1574   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1575     "v" gnus-version
1576     "f" gnus-summary-fetch-faq
1577     "d" gnus-summary-describe-group
1578     "h" gnus-summary-describe-briefly
1579     "i" gnus-info-find-node)
1580
1581   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1582     "e" gnus-summary-expire-articles
1583     "\M-\C-e" gnus-summary-expire-articles-now
1584     "\177" gnus-summary-delete-article
1585     [delete] gnus-summary-delete-article
1586     [backspace] gnus-summary-delete-article
1587     "m" gnus-summary-move-article
1588     "r" gnus-summary-respool-article
1589     "w" gnus-summary-edit-article
1590     "c" gnus-summary-copy-article
1591     "B" gnus-summary-crosspost-article
1592     "q" gnus-summary-respool-query
1593     "t" gnus-summary-respool-trace
1594     "i" gnus-summary-import-article
1595     "p" gnus-summary-article-posted-p)
1596
1597   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1598     "o" gnus-summary-save-article
1599     "m" gnus-summary-save-article-mail
1600     "F" gnus-summary-write-article-file
1601     "r" gnus-summary-save-article-rmail
1602     "f" gnus-summary-save-article-file
1603     "b" gnus-summary-save-article-body-file
1604     "h" gnus-summary-save-article-folder
1605     "v" gnus-summary-save-article-vm
1606     "p" gnus-summary-pipe-output
1607     "s" gnus-soup-add-article)
1608
1609   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1610     "b" gnus-summary-display-buttonized
1611     "m" gnus-summary-repair-multipart
1612     "v" gnus-article-view-part
1613     "o" gnus-article-save-part
1614     "c" gnus-article-copy-part
1615     "e" gnus-article-externalize-part
1616     "i" gnus-article-inline-part
1617     "|" gnus-article-pipe-part)
1618   )
1619
1620 (defun gnus-summary-make-menu-bar ()
1621   (gnus-turn-off-edit-menu 'summary)
1622
1623   (unless (boundp 'gnus-summary-misc-menu)
1624
1625     (easy-menu-define
1626      gnus-summary-kill-menu gnus-summary-mode-map ""
1627      (cons
1628       "Score"
1629       (nconc
1630        (list
1631         ["Enter score..." gnus-summary-score-entry t]
1632         ["Customize" gnus-score-customize t])
1633        (gnus-make-score-map 'increase)
1634        (gnus-make-score-map 'lower)
1635        '(("Mark"
1636           ["Kill below" gnus-summary-kill-below t]
1637           ["Mark above" gnus-summary-mark-above t]
1638           ["Tick above" gnus-summary-tick-above t]
1639           ["Clear above" gnus-summary-clear-above t])
1640          ["Current score" gnus-summary-current-score t]
1641          ["Set score" gnus-summary-set-score t]
1642          ["Switch current score file..." gnus-score-change-score-file t]
1643          ["Set mark below..." gnus-score-set-mark-below t]
1644          ["Set expunge below..." gnus-score-set-expunge-below t]
1645          ["Edit current score file" gnus-score-edit-current-scores t]
1646          ["Edit score file" gnus-score-edit-file t]
1647          ["Trace score" gnus-score-find-trace t]
1648          ["Find words" gnus-score-find-favourite-words t]
1649          ["Rescore buffer" gnus-summary-rescore t]
1650          ["Increase score..." gnus-summary-increase-score t]
1651          ["Lower score..." gnus-summary-lower-score t]))))
1652
1653     ;; Define both the Article menu in the summary buffer and the equivalent
1654     ;; Commands menu in the article buffer here for consistency.
1655     (let ((innards
1656            '(("Hide"
1657               ["All" gnus-article-hide t]
1658               ["Headers" gnus-article-hide-headers t]
1659               ["Signature" gnus-article-hide-signature t]
1660               ["Citation" gnus-article-hide-citation t]
1661               ["List identifiers" gnus-article-hide-list-identifiers t]
1662               ["PGP" gnus-article-hide-pgp t]
1663               ["Banner" gnus-article-strip-banner t]
1664               ["Boring headers" gnus-article-hide-boring-headers t])
1665              ("Highlight"
1666               ["All" gnus-article-highlight t]
1667               ["Headers" gnus-article-highlight-headers t]
1668               ["Signature" gnus-article-highlight-signature t]
1669               ["Citation" gnus-article-highlight-citation t])
1670              ("MIME"
1671               ["Words" gnus-article-decode-mime-words t]
1672               ["Charset" gnus-article-decode-charset t]
1673               ["QP" gnus-article-de-quoted-unreadable t]
1674               ["View all" gnus-mime-view-all-parts t])
1675              ("Date"
1676               ["Local" gnus-article-date-local t]
1677               ["ISO8601" gnus-article-date-iso8601 t]
1678               ["UT" gnus-article-date-ut t]
1679               ["Original" gnus-article-date-original t]
1680               ["Lapsed" gnus-article-date-lapsed t]
1681               ["User-defined" gnus-article-date-user t])
1682              ("Washing"
1683               ("Remove Blanks"
1684                ["Leading" gnus-article-strip-leading-blank-lines t]
1685                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1686                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1687                ["All of the above" gnus-article-strip-blank-lines t]
1688                ["All" gnus-article-strip-all-blank-lines t]
1689                ["Leading space" gnus-article-strip-leading-space t]
1690                ["Trailing space" gnus-article-strip-trailing-space t])
1691               ["Overstrike" gnus-article-treat-overstrike t]
1692               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1693               ["Emphasis" gnus-article-emphasize t]
1694               ["Word wrap" gnus-article-fill-cited-article t]
1695               ["Fill long lines" gnus-article-fill-long-lines t]
1696               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1697               ["CR" gnus-article-remove-cr t]
1698               ["Show X-Face" gnus-article-display-x-face t]
1699               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1700               ["Rot 13" gnus-summary-caesar-message t]
1701               ["Unix pipe" gnus-summary-pipe-message t]
1702               ["Add buttons" gnus-article-add-buttons t]
1703               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1704               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1705               ["Verbose header" gnus-summary-verbose-headers t]
1706               ["Toggle header" gnus-summary-toggle-header t])
1707              ("Output"
1708               ["Save in default format" gnus-summary-save-article t]
1709               ["Save in file" gnus-summary-save-article-file t]
1710               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1711               ["Save in MH folder" gnus-summary-save-article-folder t]
1712               ["Save in VM folder" gnus-summary-save-article-vm t]
1713               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1714               ["Save body in file" gnus-summary-save-article-body-file t]
1715               ["Pipe through a filter" gnus-summary-pipe-output t]
1716               ["Add to SOUP packet" gnus-soup-add-article t]
1717               ["Print" gnus-summary-print-article t])
1718              ("Backend"
1719               ["Respool article..." gnus-summary-respool-article t]
1720               ["Move article..." gnus-summary-move-article
1721                (gnus-check-backend-function
1722                 'request-move-article gnus-newsgroup-name)]
1723               ["Copy article..." gnus-summary-copy-article t]
1724               ["Crosspost article..." gnus-summary-crosspost-article
1725                (gnus-check-backend-function
1726                 'request-replace-article gnus-newsgroup-name)]
1727               ["Import file..." gnus-summary-import-article t]
1728               ["Check if posted" gnus-summary-article-posted-p t]
1729               ["Edit article" gnus-summary-edit-article
1730                (not (gnus-group-read-only-p))]
1731               ["Delete article" gnus-summary-delete-article
1732                (gnus-check-backend-function
1733                 'request-expire-articles gnus-newsgroup-name)]
1734               ["Query respool" gnus-summary-respool-query t]
1735               ["Trace respool" gnus-summary-respool-trace t]
1736               ["Delete expirable articles" gnus-summary-expire-articles-now
1737                (gnus-check-backend-function
1738                 'request-expire-articles gnus-newsgroup-name)])
1739              ("Extract"
1740               ["Uudecode" gnus-uu-decode-uu t]
1741               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1742               ["Unshar" gnus-uu-decode-unshar t]
1743               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1744               ["Save" gnus-uu-decode-save t]
1745               ["Binhex" gnus-uu-decode-binhex t]
1746               ["Postscript" gnus-uu-decode-postscript t])
1747              ("Cache"
1748               ["Enter article" gnus-cache-enter-article t]
1749               ["Remove article" gnus-cache-remove-article t])
1750              ["Translate" gnus-article-babel t]
1751              ["Select article buffer" gnus-summary-select-article-buffer t]
1752              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1753              ["Isearch article..." gnus-summary-isearch-article t]
1754              ["Beginning of the article" gnus-summary-beginning-of-article t]
1755              ["End of the article" gnus-summary-end-of-article t]
1756              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1757              ["Fetch referenced articles" gnus-summary-refer-references t]
1758              ["Fetch current thread" gnus-summary-refer-thread t]
1759              ["Fetch article with id..." gnus-summary-refer-article t]
1760              ["Redisplay" gnus-summary-show-article t])))
1761       (easy-menu-define
1762        gnus-summary-article-menu gnus-summary-mode-map ""
1763        (cons "Article" innards))
1764
1765       (easy-menu-define
1766        gnus-article-commands-menu gnus-article-mode-map ""
1767        (cons "Commands" innards)))
1768
1769     (easy-menu-define
1770      gnus-summary-thread-menu gnus-summary-mode-map ""
1771      '("Threads"
1772        ["Toggle threading" gnus-summary-toggle-threads t]
1773        ["Hide threads" gnus-summary-hide-all-threads t]
1774        ["Show threads" gnus-summary-show-all-threads t]
1775        ["Hide thread" gnus-summary-hide-thread t]
1776        ["Show thread" gnus-summary-show-thread t]
1777        ["Go to next thread" gnus-summary-next-thread t]
1778        ["Go to previous thread" gnus-summary-prev-thread t]
1779        ["Go down thread" gnus-summary-down-thread t]
1780        ["Go up thread" gnus-summary-up-thread t]
1781        ["Top of thread" gnus-summary-top-thread t]
1782        ["Mark thread as read" gnus-summary-kill-thread t]
1783        ["Lower thread score" gnus-summary-lower-thread t]
1784        ["Raise thread score" gnus-summary-raise-thread t]
1785        ["Rethread current" gnus-summary-rethread-current t]
1786        ))
1787
1788     (easy-menu-define
1789      gnus-summary-post-menu gnus-summary-mode-map ""
1790      '("Post"
1791        ["Post an article" gnus-summary-post-news t]
1792        ["Followup" gnus-summary-followup t]
1793        ["Followup and yank" gnus-summary-followup-with-original t]
1794        ["Supersede article" gnus-summary-supersede-article t]
1795        ["Cancel article" gnus-summary-cancel-article t]
1796        ["Reply" gnus-summary-reply t]
1797        ["Reply and yank" gnus-summary-reply-with-original t]
1798        ["Wide reply" gnus-summary-wide-reply t]
1799        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1800        ["Mail forward" gnus-summary-mail-forward t]
1801        ["Post forward" gnus-summary-post-forward t]
1802        ["Digest and mail" gnus-uu-digest-mail-forward t]
1803        ["Digest and post" gnus-uu-digest-post-forward t]
1804        ["Resend message" gnus-summary-resend-message t]
1805        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1806        ["Send a mail" gnus-summary-mail-other-window t]
1807        ["Uuencode and post" gnus-uu-post-news t]
1808        ["Followup via news" gnus-summary-followup-to-mail t]
1809        ["Followup via news and yank"
1810         gnus-summary-followup-to-mail-with-original t]
1811        ;;("Draft"
1812        ;;["Send" gnus-summary-send-draft t]
1813        ;;["Send bounced" gnus-resend-bounced-mail t])
1814        ))
1815
1816     (easy-menu-define
1817      gnus-summary-misc-menu gnus-summary-mode-map ""
1818      '("Misc"
1819        ("Mark Read"
1820         ["Mark as read" gnus-summary-mark-as-read-forward t]
1821         ["Mark same subject and select"
1822          gnus-summary-kill-same-subject-and-select t]
1823         ["Mark same subject" gnus-summary-kill-same-subject t]
1824         ["Catchup" gnus-summary-catchup t]
1825         ["Catchup all" gnus-summary-catchup-all t]
1826         ["Catchup to here" gnus-summary-catchup-to-here t]
1827         ["Catchup region" gnus-summary-mark-region-as-read t]
1828         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1829        ("Mark Various"
1830         ["Tick" gnus-summary-tick-article-forward t]
1831         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1832         ["Remove marks" gnus-summary-clear-mark-forward t]
1833         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1834         ["Set bookmark" gnus-summary-set-bookmark t]
1835         ["Remove bookmark" gnus-summary-remove-bookmark t])
1836        ("Mark Limit"
1837         ["Marks..." gnus-summary-limit-to-marks t]
1838         ["Subject..." gnus-summary-limit-to-subject t]
1839         ["Author..." gnus-summary-limit-to-author t]
1840         ["Age..." gnus-summary-limit-to-age t]
1841         ["Extra..." gnus-summary-limit-to-extra t]
1842         ["Score" gnus-summary-limit-to-score t]
1843         ["Unread" gnus-summary-limit-to-unread t]
1844         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1845         ["Articles" gnus-summary-limit-to-articles t]
1846         ["Pop limit" gnus-summary-pop-limit t]
1847         ["Show dormant" gnus-summary-limit-include-dormant t]
1848         ["Hide childless dormant"
1849          gnus-summary-limit-exclude-childless-dormant t]
1850         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1851         ["Hide marked" gnus-summary-limit-exclude-marks t]
1852         ["Show expunged" gnus-summary-show-all-expunged t])
1853        ("Process Mark"
1854         ["Set mark" gnus-summary-mark-as-processable t]
1855         ["Remove mark" gnus-summary-unmark-as-processable t]
1856         ["Remove all marks" gnus-summary-unmark-all-processable t]
1857         ["Mark above" gnus-uu-mark-over t]
1858         ["Mark series" gnus-uu-mark-series t]
1859         ["Mark region" gnus-uu-mark-region t]
1860         ["Unmark region" gnus-uu-unmark-region t]
1861         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1862         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1863         ["Mark all" gnus-uu-mark-all t]
1864         ["Mark buffer" gnus-uu-mark-buffer t]
1865         ["Mark sparse" gnus-uu-mark-sparse t]
1866         ["Mark thread" gnus-uu-mark-thread t]
1867         ["Unmark thread" gnus-uu-unmark-thread t]
1868         ("Process Mark Sets"
1869          ["Kill" gnus-summary-kill-process-mark t]
1870          ["Yank" gnus-summary-yank-process-mark
1871           gnus-newsgroup-process-stack]
1872          ["Save" gnus-summary-save-process-mark t]))
1873        ("Scroll article"
1874         ["Page forward" gnus-summary-next-page t]
1875         ["Page backward" gnus-summary-prev-page t]
1876         ["Line forward" gnus-summary-scroll-up t])
1877        ("Move"
1878         ["Next unread article" gnus-summary-next-unread-article t]
1879         ["Previous unread article" gnus-summary-prev-unread-article t]
1880         ["Next article" gnus-summary-next-article t]
1881         ["Previous article" gnus-summary-prev-article t]
1882         ["Next unread subject" gnus-summary-next-unread-subject t]
1883         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1884         ["Next article same subject" gnus-summary-next-same-subject t]
1885         ["Previous article same subject" gnus-summary-prev-same-subject t]
1886         ["First unread article" gnus-summary-first-unread-article t]
1887         ["Best unread article" gnus-summary-best-unread-article t]
1888         ["Go to subject number..." gnus-summary-goto-subject t]
1889         ["Go to article number..." gnus-summary-goto-article t]
1890         ["Go to the last article" gnus-summary-goto-last-article t]
1891         ["Pop article off history" gnus-summary-pop-article t])
1892        ("Sort"
1893         ["Sort by number" gnus-summary-sort-by-number t]
1894         ["Sort by author" gnus-summary-sort-by-author t]
1895         ["Sort by subject" gnus-summary-sort-by-subject t]
1896         ["Sort by date" gnus-summary-sort-by-date t]
1897         ["Sort by score" gnus-summary-sort-by-score t]
1898         ["Sort by lines" gnus-summary-sort-by-lines t]
1899         ["Sort by characters" gnus-summary-sort-by-chars t])
1900        ("Help"
1901         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1902         ["Describe group" gnus-summary-describe-group t]
1903         ["Read manual" gnus-info-find-node t])
1904        ("Modes"
1905         ["Pick and read" gnus-pick-mode t]
1906         ["Binary" gnus-binary-mode t])
1907        ("Regeneration"
1908         ["Regenerate" gnus-summary-prepare t]
1909         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1910         ["Toggle threading" gnus-summary-toggle-threads t])
1911        ["Filter articles..." gnus-summary-execute-command t]
1912        ["Run command on subjects..." gnus-summary-universal-argument t]
1913        ["Search articles forward..." gnus-summary-search-article-forward t]
1914        ["Search articles backward..." gnus-summary-search-article-backward t]
1915        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1916        ["Expand window" gnus-summary-expand-window t]
1917        ["Expire expirable articles" gnus-summary-expire-articles
1918         (gnus-check-backend-function
1919          'request-expire-articles gnus-newsgroup-name)]
1920        ["Edit local kill file" gnus-summary-edit-local-kill t]
1921        ["Edit main kill file" gnus-summary-edit-global-kill t]
1922        ["Edit group parameters" gnus-summary-edit-parameters t]
1923        ["Customize group parameters" gnus-summary-customize-parameters t]
1924        ["Send a bug report" gnus-bug t]
1925        ("Exit"
1926         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1927         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1928         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1929         ["Exit group" gnus-summary-exit t]
1930         ["Exit group without updating" gnus-summary-exit-no-update t]
1931         ["Exit and goto next group" gnus-summary-next-group t]
1932         ["Exit and goto prev group" gnus-summary-prev-group t]
1933         ["Reselect group" gnus-summary-reselect-current-group t]
1934         ["Rescan group" gnus-summary-rescan-group t]
1935         ["Update dribble" gnus-summary-save-newsrc t])))
1936
1937     (gnus-run-hooks 'gnus-summary-menu-hook)))
1938
1939 (defun gnus-score-set-default (var value)
1940   "A version of set that updates the GNU Emacs menu-bar."
1941   (set var value)
1942   ;; It is the message that forces the active status to be updated.
1943   (message ""))
1944
1945 (defun gnus-make-score-map (type)
1946   "Make a summary score map of type TYPE."
1947   (if t
1948       nil
1949     (let ((headers '(("author" "from" string)
1950                      ("subject" "subject" string)
1951                      ("article body" "body" string)
1952                      ("article head" "head" string)
1953                      ("xref" "xref" string)
1954                      ("extra header" "extra" string)
1955                      ("lines" "lines" number)
1956                      ("followups to author" "followup" string)))
1957           (types '((number ("less than" <)
1958                            ("greater than" >)
1959                            ("equal" =))
1960                    (string ("substring" s)
1961                            ("exact string" e)
1962                            ("fuzzy string" f)
1963                            ("regexp" r))))
1964           (perms '(("temporary" (current-time-string))
1965                    ("permanent" nil)
1966                    ("immediate" now)))
1967           header)
1968       (list
1969        (apply
1970         'nconc
1971         (list
1972          (if (eq type 'lower)
1973              "Lower score"
1974            "Increase score"))
1975         (let (outh)
1976           (while headers
1977             (setq header (car headers))
1978             (setq outh
1979                   (cons
1980                    (apply
1981                     'nconc
1982                     (list (car header))
1983                     (let ((ts (cdr (assoc (nth 2 header) types)))
1984                           outt)
1985                       (while ts
1986                         (setq outt
1987                               (cons
1988                                (apply
1989                                 'nconc
1990                                 (list (caar ts))
1991                                 (let ((ps perms)
1992                                       outp)
1993                                   (while ps
1994                                     (setq outp
1995                                           (cons
1996                                            (vector
1997                                             (caar ps)
1998                                             (list
1999                                              'gnus-summary-score-entry
2000                                              (nth 1 header)
2001                                              (if (or (string= (nth 1 header)
2002                                                               "head")
2003                                                      (string= (nth 1 header)
2004                                                               "body"))
2005                                                  ""
2006                                                (list 'gnus-summary-header
2007                                                      (nth 1 header)))
2008                                              (list 'quote (nth 1 (car ts)))
2009                                              (list 'gnus-score-default nil)
2010                                              (nth 1 (car ps))
2011                                              t)
2012                                             t)
2013                                            outp))
2014                                     (setq ps (cdr ps)))
2015                                   (list (nreverse outp))))
2016                                outt))
2017                         (setq ts (cdr ts)))
2018                       (list (nreverse outt))))
2019                    outh))
2020             (setq headers (cdr headers)))
2021           (list (nreverse outh))))))))
2022
2023 \f
2024
2025 (defun gnus-summary-mode (&optional group)
2026   "Major mode for reading articles.
2027
2028 All normal editing commands are switched off.
2029 \\<gnus-summary-mode-map>
2030 Each line in this buffer represents one article.  To read an
2031 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2032 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2033 respectively.
2034
2035 You can also post articles and send mail from this buffer.  To
2036 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2037 of an article, type `\\[gnus-summary-reply]'.
2038
2039 There are approx. one gazillion commands you can execute in this
2040 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2041
2042 The following commands are available:
2043
2044 \\{gnus-summary-mode-map}"
2045   (interactive)
2046   (when (gnus-visual-p 'summary-menu 'menu)
2047     (gnus-summary-make-menu-bar))
2048   (kill-all-local-variables)
2049   (gnus-summary-make-local-variables)
2050   (gnus-make-thread-indent-array)
2051   (gnus-simplify-mode-line)
2052   (setq major-mode 'gnus-summary-mode)
2053   (setq mode-name "Summary")
2054   (make-local-variable 'minor-mode-alist)
2055   (use-local-map gnus-summary-mode-map)
2056   (buffer-disable-undo)
2057   (setq buffer-read-only t)             ;Disable modification
2058   (setq truncate-lines t)
2059   (setq selective-display t)
2060   (setq selective-display-ellipses t)   ;Display `...'
2061   (gnus-summary-set-display-table)
2062   (gnus-set-default-directory)
2063   (setq gnus-newsgroup-name group)
2064   (make-local-variable 'gnus-summary-line-format)
2065   (make-local-variable 'gnus-summary-line-format-spec)
2066   (make-local-variable 'gnus-summary-dummy-line-format)
2067   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2068   (make-local-variable 'gnus-summary-mark-positions)
2069   (make-local-hook 'pre-command-hook)
2070   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2071   (gnus-run-hooks 'gnus-summary-mode-hook)
2072   (mm-enable-multibyte)
2073   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2074   (gnus-update-summary-mark-positions))
2075
2076 (defun gnus-summary-make-local-variables ()
2077   "Make all the local summary buffer variables."
2078   (let (global)
2079     (dolist (local gnus-summary-local-variables)
2080       (if (consp local)
2081           (progn
2082             (if (eq (cdr local) 'global)
2083                 ;; Copy the global value of the variable.
2084                 (setq global (symbol-value (car local)))
2085               ;; Use the value from the list.
2086               (setq global (eval (cdr local))))
2087             (set (make-local-variable (car local)) global))
2088         ;; Simple nil-valued local variable.
2089         (set (make-local-variable local) nil)))))
2090
2091 (defun gnus-summary-clear-local-variables ()
2092   (let ((locals gnus-summary-local-variables))
2093     (while locals
2094       (if (consp (car locals))
2095           (and (vectorp (caar locals))
2096                (set (caar locals) nil))
2097         (and (vectorp (car locals))
2098              (set (car locals) nil)))
2099       (setq locals (cdr locals)))))
2100
2101 ;; Summary data functions.
2102
2103 (defmacro gnus-data-number (data)
2104   `(car ,data))
2105
2106 (defmacro gnus-data-set-number (data number)
2107   `(setcar ,data ,number))
2108
2109 (defmacro gnus-data-mark (data)
2110   `(nth 1 ,data))
2111
2112 (defmacro gnus-data-set-mark (data mark)
2113   `(setcar (nthcdr 1 ,data) ,mark))
2114
2115 (defmacro gnus-data-pos (data)
2116   `(nth 2 ,data))
2117
2118 (defmacro gnus-data-set-pos (data pos)
2119   `(setcar (nthcdr 2 ,data) ,pos))
2120
2121 (defmacro gnus-data-header (data)
2122   `(nth 3 ,data))
2123
2124 (defmacro gnus-data-set-header (data header)
2125   `(setf (nth 3 ,data) ,header))
2126
2127 (defmacro gnus-data-level (data)
2128   `(nth 4 ,data))
2129
2130 (defmacro gnus-data-unread-p (data)
2131   `(= (nth 1 ,data) gnus-unread-mark))
2132
2133 (defmacro gnus-data-read-p (data)
2134   `(/= (nth 1 ,data) gnus-unread-mark))
2135
2136 (defmacro gnus-data-pseudo-p (data)
2137   `(consp (nth 3 ,data)))
2138
2139 (defmacro gnus-data-find (number)
2140   `(assq ,number gnus-newsgroup-data))
2141
2142 (defmacro gnus-data-find-list (number &optional data)
2143   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2144      (memq (assq ,number bdata)
2145            bdata)))
2146
2147 (defmacro gnus-data-make (number mark pos header level)
2148   `(list ,number ,mark ,pos ,header ,level))
2149
2150 (defun gnus-data-enter (after-article number mark pos header level offset)
2151   (let ((data (gnus-data-find-list after-article)))
2152     (unless data
2153       (error "No such article: %d" after-article))
2154     (setcdr data (cons (gnus-data-make number mark pos header level)
2155                        (cdr data)))
2156     (setq gnus-newsgroup-data-reverse nil)
2157     (gnus-data-update-list (cddr data) offset)))
2158
2159 (defun gnus-data-enter-list (after-article list &optional offset)
2160   (when list
2161     (let ((data (and after-article (gnus-data-find-list after-article)))
2162           (ilist list))
2163       (if (not (or data
2164                    after-article))
2165           (let ((odata gnus-newsgroup-data))
2166             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2167             (when offset
2168               (gnus-data-update-list odata offset)))
2169         ;; Find the last element in the list to be spliced into the main
2170         ;; list.
2171         (while (cdr list)
2172           (setq list (cdr list)))
2173         (if (not data)
2174             (progn
2175               (setcdr list gnus-newsgroup-data)
2176               (setq gnus-newsgroup-data ilist)
2177               (when offset
2178                 (gnus-data-update-list (cdr list) offset)))
2179           (setcdr list (cdr data))
2180           (setcdr data ilist)
2181           (when offset
2182             (gnus-data-update-list (cdr list) offset))))
2183       (setq gnus-newsgroup-data-reverse nil))))
2184
2185 (defun gnus-data-remove (article &optional offset)
2186   (let ((data gnus-newsgroup-data))
2187     (if (= (gnus-data-number (car data)) article)
2188         (progn
2189           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2190                 gnus-newsgroup-data-reverse nil)
2191           (when offset
2192             (gnus-data-update-list gnus-newsgroup-data offset)))
2193       (while (cdr data)
2194         (when (= (gnus-data-number (cadr data)) article)
2195           (setcdr data (cddr data))
2196           (when offset
2197             (gnus-data-update-list (cdr data) offset))
2198           (setq data nil
2199                 gnus-newsgroup-data-reverse nil))
2200         (setq data (cdr data))))))
2201
2202 (defmacro gnus-data-list (backward)
2203   `(if ,backward
2204        (or gnus-newsgroup-data-reverse
2205            (setq gnus-newsgroup-data-reverse
2206                  (reverse gnus-newsgroup-data)))
2207      gnus-newsgroup-data))
2208
2209 (defun gnus-data-update-list (data offset)
2210   "Add OFFSET to the POS of all data entries in DATA."
2211   (setq gnus-newsgroup-data-reverse nil)
2212   (while data
2213     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2214     (setq data (cdr data))))
2215
2216 (defun gnus-summary-article-pseudo-p (article)
2217   "Say whether this article is a pseudo article or not."
2218   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2219
2220 (defmacro gnus-summary-article-sparse-p (article)
2221   "Say whether this article is a sparse article or not."
2222   `(memq ,article gnus-newsgroup-sparse))
2223
2224 (defmacro gnus-summary-article-ancient-p (article)
2225   "Say whether this article is a sparse article or not."
2226   `(memq ,article gnus-newsgroup-ancient))
2227
2228 (defun gnus-article-parent-p (number)
2229   "Say whether this article is a parent or not."
2230   (let ((data (gnus-data-find-list number)))
2231     (and (cdr data)                     ; There has to be an article after...
2232          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2233             (gnus-data-level (nth 1 data))))))
2234
2235 (defun gnus-article-children (number)
2236   "Return a list of all children to NUMBER."
2237   (let* ((data (gnus-data-find-list number))
2238          (level (gnus-data-level (car data)))
2239          children)
2240     (setq data (cdr data))
2241     (while (and data
2242                 (= (gnus-data-level (car data)) (1+ level)))
2243       (push (gnus-data-number (car data)) children)
2244       (setq data (cdr data)))
2245     children))
2246
2247 (defmacro gnus-summary-skip-intangible ()
2248   "If the current article is intangible, then jump to a different article."
2249   '(let ((to (get-text-property (point) 'gnus-intangible)))
2250      (and to (gnus-summary-goto-subject to))))
2251
2252 (defmacro gnus-summary-article-intangible-p ()
2253   "Say whether this article is intangible or not."
2254   '(get-text-property (point) 'gnus-intangible))
2255
2256 (defun gnus-article-read-p (article)
2257   "Say whether ARTICLE is read or not."
2258   (not (or (memq article gnus-newsgroup-marked)
2259            (memq article gnus-newsgroup-unreads)
2260            (memq article gnus-newsgroup-unselected)
2261            (memq article gnus-newsgroup-dormant))))
2262
2263 ;; Some summary mode macros.
2264
2265 (defmacro gnus-summary-article-number ()
2266   "The article number of the article on the current line.
2267 If there isn's an article number here, then we return the current
2268 article number."
2269   '(progn
2270      (gnus-summary-skip-intangible)
2271      (or (get-text-property (point) 'gnus-number)
2272          (gnus-summary-last-subject))))
2273
2274 (defmacro gnus-summary-article-header (&optional number)
2275   "Return the header of article NUMBER."
2276   `(gnus-data-header (gnus-data-find
2277                       ,(or number '(gnus-summary-article-number)))))
2278
2279 (defmacro gnus-summary-thread-level (&optional number)
2280   "Return the level of thread that starts with article NUMBER."
2281   `(if (and (eq gnus-summary-make-false-root 'dummy)
2282             (get-text-property (point) 'gnus-intangible))
2283        0
2284      (gnus-data-level (gnus-data-find
2285                        ,(or number '(gnus-summary-article-number))))))
2286
2287 (defmacro gnus-summary-article-mark (&optional number)
2288   "Return the mark of article NUMBER."
2289   `(gnus-data-mark (gnus-data-find
2290                     ,(or number '(gnus-summary-article-number)))))
2291
2292 (defmacro gnus-summary-article-pos (&optional number)
2293   "Return the position of the line of article NUMBER."
2294   `(gnus-data-pos (gnus-data-find
2295                    ,(or number '(gnus-summary-article-number)))))
2296
2297 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2298 (defmacro gnus-summary-article-subject (&optional number)
2299   "Return current subject string or nil if nothing."
2300   `(let ((headers
2301           ,(if number
2302                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2303              '(gnus-data-header (assq (gnus-summary-article-number)
2304                                       gnus-newsgroup-data)))))
2305      (and headers
2306           (vectorp headers)
2307           (mail-header-subject headers))))
2308
2309 (defmacro gnus-summary-article-score (&optional number)
2310   "Return current article score."
2311   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2312                   gnus-newsgroup-scored))
2313        gnus-summary-default-score 0))
2314
2315 (defun gnus-summary-article-children (&optional number)
2316   "Return a list of article numbers that are children of article NUMBER."
2317   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2318          (level (gnus-data-level (car data)))
2319          l children)
2320     (while (and (setq data (cdr data))
2321                 (> (setq l (gnus-data-level (car data))) level))
2322       (and (= (1+ level) l)
2323            (push (gnus-data-number (car data))
2324                  children)))
2325     (nreverse children)))
2326
2327 (defun gnus-summary-article-parent (&optional number)
2328   "Return the article number of the parent of article NUMBER."
2329   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2330                                     (gnus-data-list t)))
2331          (level (gnus-data-level (car data))))
2332     (if (zerop level)
2333         ()                              ; This is a root.
2334       ;; We search until we find an article with a level less than
2335       ;; this one.  That function has to be the parent.
2336       (while (and (setq data (cdr data))
2337                   (not (< (gnus-data-level (car data)) level))))
2338       (and data (gnus-data-number (car data))))))
2339
2340 (defun gnus-unread-mark-p (mark)
2341   "Say whether MARK is the unread mark."
2342   (= mark gnus-unread-mark))
2343
2344 (defun gnus-read-mark-p (mark)
2345   "Say whether MARK is one of the marks that mark as read.
2346 This is all marks except unread, ticked, dormant, and expirable."
2347   (not (or (= mark gnus-unread-mark)
2348            (= mark gnus-ticked-mark)
2349            (= mark gnus-dormant-mark)
2350            (= mark gnus-expirable-mark))))
2351
2352 (defmacro gnus-article-mark (number)
2353   "Return the MARK of article NUMBER.
2354 This macro should only be used when computing the mark the \"first\"
2355 time; i.e., when generating the summary lines.  After that,
2356 `gnus-summary-article-mark' should be used to examine the
2357 marks of articles."
2358   `(cond
2359     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2360     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2361     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2362     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2363     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2364     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2365     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2366     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2367            gnus-ancient-mark))))
2368
2369 ;; Saving hidden threads.
2370
2371 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2372 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2373
2374 (defmacro gnus-save-hidden-threads (&rest forms)
2375   "Save hidden threads, eval FORMS, and restore the hidden threads."
2376   (let ((config (make-symbol "config")))
2377     `(let ((,config (gnus-hidden-threads-configuration)))
2378        (unwind-protect
2379            (save-excursion
2380              ,@forms)
2381          (gnus-restore-hidden-threads-configuration ,config)))))
2382
2383 (defun gnus-data-compute-positions ()
2384   "Compute the positions of all articles."
2385   (setq gnus-newsgroup-data-reverse nil)
2386   (let ((data gnus-newsgroup-data))
2387     (save-excursion
2388       (gnus-save-hidden-threads
2389         (gnus-summary-show-all-threads)
2390         (goto-char (point-min))
2391         (while data
2392           (while (get-text-property (point) 'gnus-intangible)
2393             (forward-line 1))
2394           (gnus-data-set-pos (car data) (+ (point) 3))
2395           (setq data (cdr data))
2396           (forward-line 1))))))
2397
2398 (defun gnus-hidden-threads-configuration ()
2399   "Return the current hidden threads configuration."
2400   (save-excursion
2401     (let (config)
2402       (goto-char (point-min))
2403       (while (search-forward "\r" nil t)
2404         (push (1- (point)) config))
2405       config)))
2406
2407 (defun gnus-restore-hidden-threads-configuration (config)
2408   "Restore hidden threads configuration from CONFIG."
2409   (let (point buffer-read-only)
2410     (while (setq point (pop config))
2411       (when (and (< point (point-max))
2412                  (goto-char point)
2413                  (eq (char-after) ?\n))
2414         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2415
2416 ;; Various summary mode internalish functions.
2417
2418 (defun gnus-mouse-pick-article (e)
2419   (interactive "e")
2420   (mouse-set-point e)
2421   (gnus-summary-next-page nil t))
2422
2423 (defun gnus-summary-set-display-table ()
2424   ;; Change the display table.  Odd characters have a tendency to mess
2425   ;; up nicely formatted displays - we make all possible glyphs
2426   ;; display only a single character.
2427
2428   ;; We start from the standard display table, if any.
2429   (let ((table (or (copy-sequence standard-display-table)
2430                    (make-display-table)))
2431         (i 32))
2432     ;; Nix out all the control chars...
2433     (while (>= (setq i (1- i)) 0)
2434       (aset table i [??]))
2435     ;; ... but not newline and cr, of course.  (cr is necessary for the
2436     ;; selective display).
2437     (aset table ?\n nil)
2438     (aset table ?\r nil)
2439     ;; We keep TAB as well.
2440     (aset table ?\t nil)
2441     ;; We nix out any glyphs over 126 that are not set already.
2442     (let ((i 256))
2443       (while (>= (setq i (1- i)) 127)
2444         ;; Only modify if the entry is nil.
2445         (unless (aref table i)
2446           (aset table i [??]))))
2447     (setq buffer-display-table table)))
2448
2449 (defun gnus-summary-setup-buffer (group)
2450   "Initialize summary buffer."
2451   (let ((buffer (concat "*Summary " group "*")))
2452     (if (get-buffer buffer)
2453         (progn
2454           (set-buffer buffer)
2455           (setq gnus-summary-buffer (current-buffer))
2456           (not gnus-newsgroup-prepared))
2457       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2458       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2459       (gnus-summary-mode group)
2460       (when gnus-carpal
2461         (gnus-carpal-setup-buffer 'summary))
2462       (unless gnus-single-article-buffer
2463         (make-local-variable 'gnus-article-buffer)
2464         (make-local-variable 'gnus-article-current)
2465         (make-local-variable 'gnus-original-article-buffer))
2466       (setq gnus-newsgroup-name group)
2467       t)))
2468
2469 (defun gnus-set-global-variables ()
2470   ;; Set the global equivalents of the summary buffer-local variables
2471   ;; to the latest values they had.  These reflect the summary buffer
2472   ;; that was in action when the last article was fetched.
2473   (when (eq major-mode 'gnus-summary-mode)
2474     (setq gnus-summary-buffer (current-buffer))
2475     (let ((name gnus-newsgroup-name)
2476           (marked gnus-newsgroup-marked)
2477           (unread gnus-newsgroup-unreads)
2478           (headers gnus-current-headers)
2479           (data gnus-newsgroup-data)
2480           (summary gnus-summary-buffer)
2481           (article-buffer gnus-article-buffer)
2482           (original gnus-original-article-buffer)
2483           (gac gnus-article-current)
2484           (reffed gnus-reffed-article-number)
2485           (score-file gnus-current-score-file)
2486           (default-charset gnus-newsgroup-charset))
2487       (save-excursion
2488         (set-buffer gnus-group-buffer)
2489         (setq gnus-newsgroup-name name
2490               gnus-newsgroup-marked marked
2491               gnus-newsgroup-unreads unread
2492               gnus-current-headers headers
2493               gnus-newsgroup-data data
2494               gnus-article-current gac
2495               gnus-summary-buffer summary
2496               gnus-article-buffer article-buffer
2497               gnus-original-article-buffer original
2498               gnus-reffed-article-number reffed
2499               gnus-current-score-file score-file
2500               gnus-newsgroup-charset default-charset)
2501         ;; The article buffer also has local variables.
2502         (when (gnus-buffer-live-p gnus-article-buffer)
2503           (set-buffer gnus-article-buffer)
2504           (setq gnus-summary-buffer summary))))))
2505
2506 (defun gnus-summary-article-unread-p (article)
2507   "Say whether ARTICLE is unread or not."
2508   (memq article gnus-newsgroup-unreads))
2509
2510 (defun gnus-summary-first-article-p (&optional article)
2511   "Return whether ARTICLE is the first article in the buffer."
2512   (if (not (setq article (or article (gnus-summary-article-number))))
2513       nil
2514     (eq article (caar gnus-newsgroup-data))))
2515
2516 (defun gnus-summary-last-article-p (&optional article)
2517   "Return whether ARTICLE is the last article in the buffer."
2518   (if (not (setq article (or article (gnus-summary-article-number))))
2519       ;; All non-existent numbers are the last article.  :-)
2520       t
2521     (not (cdr (gnus-data-find-list article)))))
2522
2523 (defun gnus-make-thread-indent-array ()
2524   (let ((n 200))
2525     (unless (and gnus-thread-indent-array
2526                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2527       (setq gnus-thread-indent-array (make-vector 201 "")
2528             gnus-thread-indent-array-level gnus-thread-indent-level)
2529       (while (>= n 0)
2530         (aset gnus-thread-indent-array n
2531               (make-string (* n gnus-thread-indent-level) ? ))
2532         (setq n (1- n))))))
2533
2534 (defun gnus-update-summary-mark-positions ()
2535   "Compute where the summary marks are to go."
2536   (save-excursion
2537     (when (gnus-buffer-exists-p gnus-summary-buffer)
2538       (set-buffer gnus-summary-buffer))
2539     (let ((gnus-replied-mark 129)
2540           (gnus-score-below-mark 130)
2541           (gnus-score-over-mark 130)
2542           (gnus-download-mark 131)
2543           (spec gnus-summary-line-format-spec)
2544           gnus-visual pos)
2545       (save-excursion
2546         (gnus-set-work-buffer)
2547         (let ((gnus-summary-line-format-spec spec)
2548               (gnus-newsgroup-downloadable '((0 . t))))
2549           (gnus-summary-insert-line
2550            (make-full-mail-header 0 "" "" "" "" "" 0 0 "" nil)
2551            0 nil 128 t nil "" nil 1)
2552           (goto-char (point-min))
2553           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2554                                              (- (point) 2)))))
2555           (goto-char (point-min))
2556           (push (cons 'replied (and (search-forward "\201" nil t)
2557                                     (- (point) 2)))
2558                 pos)
2559           (goto-char (point-min))
2560           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2561                 pos)
2562           (goto-char (point-min))
2563           (push (cons 'download
2564                       (and (search-forward "\203" nil t) (- (point) 2)))
2565                 pos)))
2566       (setq gnus-summary-mark-positions pos))))
2567
2568 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2569   "Insert a dummy root in the summary buffer."
2570   (beginning-of-line)
2571   (gnus-add-text-properties
2572    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2573    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2574
2575 (defun gnus-summary-from-or-to-or-newsgroups (header)
2576   (let ((to (cdr (assq 'To (mail-header-extra header))))
2577         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2578         (mail-parse-charset gnus-newsgroup-charset)
2579         (mail-parse-ignored-charsets 
2580          (save-excursion (set-buffer gnus-summary-buffer)
2581                          gnus-newsgroup-ignored-charsets)))
2582     (cond
2583      ((and to
2584            gnus-ignored-from-addresses
2585            (string-match gnus-ignored-from-addresses
2586                          (mail-header-from header)))
2587       (concat "-> "
2588               (or (car (funcall gnus-extract-address-components to))
2589                   to)))
2590      ((and newsgroups
2591            gnus-ignored-from-addresses
2592            (string-match gnus-ignored-from-addresses
2593                          (mail-header-from header)))
2594       (concat "=> " newsgroups))
2595      (t
2596       (or (car (funcall gnus-extract-address-components
2597                         (mail-header-from header)))
2598           (mail-header-from header))))))
2599
2600 (defun gnus-summary-insert-line (gnus-tmp-header
2601                                  gnus-tmp-level gnus-tmp-current
2602                                  gnus-tmp-unread gnus-tmp-replied
2603                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2604                                  &optional gnus-tmp-dummy gnus-tmp-score
2605                                  gnus-tmp-process)
2606   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2607          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2608          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2609          (gnus-tmp-score-char
2610           (if (or (null gnus-summary-default-score)
2611                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2612                       gnus-summary-zcore-fuzz))
2613               ? ;Whitespace
2614             (if (< gnus-tmp-score gnus-summary-default-score)
2615                 gnus-score-below-mark gnus-score-over-mark)))
2616          (gnus-tmp-replied
2617           (cond (gnus-tmp-process gnus-process-mark)
2618                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2619                  gnus-cached-mark)
2620                 (gnus-tmp-replied gnus-replied-mark)
2621                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2622                  gnus-saved-mark)
2623                 (t gnus-unread-mark)))
2624          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2625          (gnus-tmp-name
2626           (cond
2627            ((string-match "<[^>]+> *$" gnus-tmp-from)
2628             (let ((beg (match-beginning 0)))
2629               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2630                        (substring gnus-tmp-from (1+ (match-beginning 0))
2631                                   (1- (match-end 0))))
2632                   (substring gnus-tmp-from 0 beg))))
2633            ((string-match "(.+)" gnus-tmp-from)
2634             (substring gnus-tmp-from
2635                        (1+ (match-beginning 0)) (1- (match-end 0))))
2636            (t gnus-tmp-from)))
2637          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2638          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2639          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2640          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2641          (buffer-read-only nil))
2642     (when (string= gnus-tmp-name "")
2643       (setq gnus-tmp-name gnus-tmp-from))
2644     (unless (numberp gnus-tmp-lines)
2645       (setq gnus-tmp-lines 0))
2646     (gnus-put-text-property
2647      (point)
2648      (progn (eval gnus-summary-line-format-spec) (point))
2649      'gnus-number gnus-tmp-number)
2650     (when (gnus-visual-p 'summary-highlight 'highlight)
2651       (forward-line -1)
2652       (gnus-run-hooks 'gnus-summary-update-hook)
2653       (forward-line 1))))
2654
2655 (defun gnus-summary-update-line (&optional dont-update)
2656   ;; Update summary line after change.
2657   (when (and gnus-summary-default-score
2658              (not gnus-summary-inhibit-highlight))
2659     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2660            (article (gnus-summary-article-number))
2661            (score (gnus-summary-article-score article)))
2662       (unless dont-update
2663         (if (and gnus-summary-mark-below
2664                  (< (gnus-summary-article-score)
2665                     gnus-summary-mark-below))
2666             ;; This article has a low score, so we mark it as read.
2667             (when (memq article gnus-newsgroup-unreads)
2668               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2669           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2670             ;; This article was previously marked as read on account
2671             ;; of a low score, but now it has risen, so we mark it as
2672             ;; unread.
2673             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2674         (gnus-summary-update-mark
2675          (if (or (null gnus-summary-default-score)
2676                  (<= (abs (- score gnus-summary-default-score))
2677                      gnus-summary-zcore-fuzz))
2678              ? ;Whitespace
2679            (if (< score gnus-summary-default-score)
2680                gnus-score-below-mark gnus-score-over-mark))
2681          'score))
2682       ;; Do visual highlighting.
2683       (when (gnus-visual-p 'summary-highlight 'highlight)
2684         (gnus-run-hooks 'gnus-summary-update-hook)))))
2685
2686 (defvar gnus-tmp-new-adopts nil)
2687
2688 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2689   "Return the number of articles in THREAD.
2690 This may be 0 in some cases -- if none of the articles in
2691 the thread are to be displayed."
2692   (let* ((number
2693           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2694           (cond
2695            ((not (listp thread))
2696             1)
2697            ((and (consp thread) (cdr thread))
2698             (apply
2699              '+ 1 (mapcar
2700                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2701            ((null thread)
2702             1)
2703            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2704             1)
2705            (t 0))))
2706     (when (and level (zerop level) gnus-tmp-new-adopts)
2707       (incf number
2708             (apply '+ (mapcar
2709                        'gnus-summary-number-of-articles-in-thread
2710                        gnus-tmp-new-adopts))))
2711     (if char
2712         (if (> number 1) gnus-not-empty-thread-mark
2713           gnus-empty-thread-mark)
2714       number)))
2715
2716 (defun gnus-summary-set-local-parameters (group)
2717   "Go through the local params of GROUP and set all variable specs in that list."
2718   (let ((params (gnus-group-find-parameter group))
2719         elem)
2720     (while params
2721       (setq elem (car params)
2722             params (cdr params))
2723       (and (consp elem)                 ; Has to be a cons.
2724            (consp (cdr elem))           ; The cdr has to be a list.
2725            (symbolp (car elem))         ; Has to be a symbol in there.
2726            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2727            (ignore-errors               ; So we set it.
2728              (make-local-variable (car elem))
2729              (set (car elem) (eval (nth 1 elem))))))))
2730
2731 (defun gnus-summary-read-group (group &optional show-all no-article
2732                                       kill-buffer no-display backward
2733                                       select-articles)
2734   "Start reading news in newsgroup GROUP.
2735 If SHOW-ALL is non-nil, already read articles are also listed.
2736 If NO-ARTICLE is non-nil, no article is selected initially.
2737 If NO-DISPLAY, don't generate a summary buffer."
2738   (let (result)
2739     (while (and group
2740                 (null (setq result
2741                             (let ((gnus-auto-select-next nil))
2742                               (or (gnus-summary-read-group-1
2743                                    group show-all no-article
2744                                    kill-buffer no-display
2745                                    select-articles)
2746                                   (setq show-all nil
2747                                         select-articles nil)))))
2748                 (eq gnus-auto-select-next 'quietly))
2749       (set-buffer gnus-group-buffer)
2750       ;; The entry function called above goes to the next
2751       ;; group automatically, so we go two groups back
2752       ;; if we are searching for the previous group.
2753       (when backward
2754         (gnus-group-prev-unread-group 2))
2755       (if (not (equal group (gnus-group-group-name)))
2756           (setq group (gnus-group-group-name))
2757         (setq group nil)))
2758     result))
2759
2760 (defun gnus-summary-read-group-1 (group show-all no-article
2761                                         kill-buffer no-display
2762                                         &optional select-articles)
2763   ;; Killed foreign groups can't be entered.
2764   (when (and (not (gnus-group-native-p group))
2765              (not (gnus-gethash group gnus-newsrc-hashtb)))
2766     (error "Dead non-native groups can't be entered"))
2767   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2768   (let* ((new-group (gnus-summary-setup-buffer group))
2769          (quit-config (gnus-group-quit-config group))
2770          (did-select (and new-group (gnus-select-newsgroup
2771                                      group show-all select-articles))))
2772     (cond
2773      ;; This summary buffer exists already, so we just select it.
2774      ((not new-group)
2775       (gnus-set-global-variables)
2776       (when kill-buffer
2777         (gnus-kill-or-deaden-summary kill-buffer))
2778       (gnus-configure-windows 'summary 'force)
2779       (gnus-set-mode-line 'summary)
2780       (gnus-summary-position-point)
2781       (message "")
2782       t)
2783      ;; We couldn't select this group.
2784      ((null did-select)
2785       (when (and (eq major-mode 'gnus-summary-mode)
2786                  (not (equal (current-buffer) kill-buffer)))
2787         (kill-buffer (current-buffer))
2788         (if (not quit-config)
2789             (progn
2790               ;; Update the info -- marks might need to be removed,
2791               ;; for instance.
2792               (gnus-summary-update-info)
2793               (set-buffer gnus-group-buffer)
2794               (gnus-group-jump-to-group group)
2795               (gnus-group-next-unread-group 1))
2796           (gnus-handle-ephemeral-exit quit-config)))
2797       (gnus-message 3 "Can't select group")
2798       nil)
2799      ;; The user did a `C-g' while prompting for number of articles,
2800      ;; so we exit this group.
2801      ((eq did-select 'quit)
2802       (and (eq major-mode 'gnus-summary-mode)
2803            (not (equal (current-buffer) kill-buffer))
2804            (kill-buffer (current-buffer)))
2805       (when kill-buffer
2806         (gnus-kill-or-deaden-summary kill-buffer))
2807       (if (not quit-config)
2808           (progn
2809             (set-buffer gnus-group-buffer)
2810             (gnus-group-jump-to-group group)
2811             (gnus-group-next-unread-group 1)
2812             (gnus-configure-windows 'group 'force))
2813         (gnus-handle-ephemeral-exit quit-config))
2814       ;; Finally signal the quit.
2815       (signal 'quit nil))
2816      ;; The group was successfully selected.
2817      (t
2818       (gnus-set-global-variables)
2819       ;; Save the active value in effect when the group was entered.
2820       (setq gnus-newsgroup-active
2821             (gnus-copy-sequence
2822              (gnus-active gnus-newsgroup-name)))
2823       ;; You can change the summary buffer in some way with this hook.
2824       (gnus-run-hooks 'gnus-select-group-hook)
2825       ;; Set any local variables in the group parameters.
2826       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2827       (gnus-update-format-specifications
2828        nil 'summary 'summary-mode 'summary-dummy)
2829       (gnus-update-summary-mark-positions)
2830       ;; Do score processing.
2831       (when gnus-use-scoring
2832         (gnus-possibly-score-headers))
2833       ;; Check whether to fill in the gaps in the threads.
2834       (when gnus-build-sparse-threads
2835         (gnus-build-sparse-threads))
2836       ;; Find the initial limit.
2837       (if gnus-show-threads
2838           (if show-all
2839               (let ((gnus-newsgroup-dormant nil))
2840                 (gnus-summary-initial-limit show-all))
2841             (gnus-summary-initial-limit show-all))
2842         ;; When untreaded, all articles are always shown.
2843         (setq gnus-newsgroup-limit
2844               (mapcar
2845                (lambda (header) (mail-header-number header))
2846                gnus-newsgroup-headers)))
2847       ;; Generate the summary buffer.
2848       (unless no-display
2849         (gnus-summary-prepare))
2850       (when gnus-use-trees
2851         (gnus-tree-open group)
2852         (setq gnus-summary-highlight-line-function
2853               'gnus-tree-highlight-article))
2854       ;; If the summary buffer is empty, but there are some low-scored
2855       ;; articles or some excluded dormants, we include these in the
2856       ;; buffer.
2857       (when (and (zerop (buffer-size))
2858                  (not no-display))
2859         (cond (gnus-newsgroup-dormant
2860                (gnus-summary-limit-include-dormant))
2861               ((and gnus-newsgroup-scored show-all)
2862                (gnus-summary-limit-include-expunged t))))
2863       ;; Function `gnus-apply-kill-file' must be called in this hook.
2864       (gnus-run-hooks 'gnus-apply-kill-hook)
2865       (if (and (zerop (buffer-size))
2866                (not no-display))
2867           (progn
2868             ;; This newsgroup is empty.
2869             (gnus-summary-catchup-and-exit nil t)
2870             (gnus-message 6 "No unread news")
2871             (when kill-buffer
2872               (gnus-kill-or-deaden-summary kill-buffer))
2873             ;; Return nil from this function.
2874             nil)
2875         ;; Hide conversation thread subtrees.  We cannot do this in
2876         ;; gnus-summary-prepare-hook since kill processing may not
2877         ;; work with hidden articles.
2878         (and gnus-show-threads
2879              gnus-thread-hide-subtree
2880              (gnus-summary-hide-all-threads))
2881         (when kill-buffer
2882           (gnus-kill-or-deaden-summary kill-buffer))
2883         ;; Show first unread article if requested.
2884         (if (and (not no-article)
2885                  (not no-display)
2886                  gnus-newsgroup-unreads
2887                  gnus-auto-select-first)
2888             (progn
2889               (gnus-configure-windows 'summary)
2890               (cond
2891                ((eq gnus-auto-select-first 'best)
2892                 (gnus-summary-best-unread-article))
2893                ((eq gnus-auto-select-first t)
2894                 (gnus-summary-first-unread-article))
2895                ((gnus-functionp gnus-auto-select-first)
2896                 (funcall gnus-auto-select-first))))
2897           ;; Don't select any articles, just move point to the first
2898           ;; article in the group.
2899           (goto-char (point-min))
2900           (gnus-summary-position-point)
2901           (gnus-configure-windows 'summary 'force)
2902           (gnus-set-mode-line 'summary))
2903         (when (get-buffer-window gnus-group-buffer t)
2904           ;; Gotta use windows, because recenter does weird stuff if
2905           ;; the current buffer ain't the displayed window.
2906           (let ((owin (selected-window)))
2907             (select-window (get-buffer-window gnus-group-buffer t))
2908             (when (gnus-group-goto-group group)
2909               (recenter))
2910             (select-window owin)))
2911         ;; Mark this buffer as "prepared".
2912         (setq gnus-newsgroup-prepared t)
2913         (gnus-run-hooks 'gnus-summary-prepared-hook)
2914         t)))))
2915
2916 (defun gnus-summary-prepare ()
2917   "Generate the summary buffer."
2918   (interactive)
2919   (let ((buffer-read-only nil))
2920     (erase-buffer)
2921     (setq gnus-newsgroup-data nil
2922           gnus-newsgroup-data-reverse nil)
2923     (gnus-run-hooks 'gnus-summary-generate-hook)
2924     ;; Generate the buffer, either with threads or without.
2925     (when gnus-newsgroup-headers
2926       (gnus-summary-prepare-threads
2927        (if gnus-show-threads
2928            (gnus-sort-gathered-threads
2929             (funcall gnus-summary-thread-gathering-function
2930                      (gnus-sort-threads
2931                       (gnus-cut-threads (gnus-make-threads)))))
2932          ;; Unthreaded display.
2933          (gnus-sort-articles gnus-newsgroup-headers))))
2934     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2935     ;; Call hooks for modifying summary buffer.
2936     (goto-char (point-min))
2937     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2938
2939 (defsubst gnus-general-simplify-subject (subject)
2940   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2941   (setq subject
2942         (cond
2943          ;; Truncate the subject.
2944          (gnus-simplify-subject-functions
2945           (gnus-map-function gnus-simplify-subject-functions subject))
2946          ((numberp gnus-summary-gather-subject-limit)
2947           (setq subject (gnus-simplify-subject-re subject))
2948           (if (> (length subject) gnus-summary-gather-subject-limit)
2949               (substring subject 0 gnus-summary-gather-subject-limit)
2950             subject))
2951          ;; Fuzzily simplify it.
2952          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2953           (gnus-simplify-subject-fuzzy subject))
2954          ;; Just remove the leading "Re:".
2955          (t
2956           (gnus-simplify-subject-re subject))))
2957
2958   (if (and gnus-summary-gather-exclude-subject
2959            (string-match gnus-summary-gather-exclude-subject subject))
2960       nil                               ; This article shouldn't be gathered
2961     subject))
2962
2963 (defun gnus-summary-simplify-subject-query ()
2964   "Query where the respool algorithm would put this article."
2965   (interactive)
2966   (gnus-summary-select-article)
2967   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2968
2969 (defun gnus-gather-threads-by-subject (threads)
2970   "Gather threads by looking at Subject headers."
2971   (if (not gnus-summary-make-false-root)
2972       threads
2973     (let ((hashtb (gnus-make-hashtable 1024))
2974           (prev threads)
2975           (result threads)
2976           subject hthread whole-subject)
2977       (while threads
2978         (setq subject (gnus-general-simplify-subject
2979                        (setq whole-subject (mail-header-subject
2980                                             (caar threads)))))
2981         (when subject
2982           (if (setq hthread (gnus-gethash subject hashtb))
2983               (progn
2984                 ;; We enter a dummy root into the thread, if we
2985                 ;; haven't done that already.
2986                 (unless (stringp (caar hthread))
2987                   (setcar hthread (list whole-subject (car hthread))))
2988                 ;; We add this new gathered thread to this gathered
2989                 ;; thread.
2990                 (setcdr (car hthread)
2991                         (nconc (cdar hthread) (list (car threads))))
2992                 ;; Remove it from the list of threads.
2993                 (setcdr prev (cdr threads))
2994                 (setq threads prev))
2995             ;; Enter this thread into the hash table.
2996             (gnus-sethash subject threads hashtb)))
2997         (setq prev threads)
2998         (setq threads (cdr threads)))
2999       result)))
3000
3001 (defun gnus-gather-threads-by-references (threads)
3002   "Gather threads by looking at References headers."
3003   (let ((idhashtb (gnus-make-hashtable 1024))
3004         (thhashtb (gnus-make-hashtable 1024))
3005         (prev threads)
3006         (result threads)
3007         ids references id gthread gid entered ref)
3008     (while threads
3009       (when (setq references (mail-header-references (caar threads)))
3010         (setq id (mail-header-id (caar threads))
3011               ids (gnus-split-references references)
3012               entered nil)
3013         (while (setq ref (pop ids))
3014           (setq ids (delete ref ids))
3015           (if (not (setq gid (gnus-gethash ref idhashtb)))
3016               (progn
3017                 (gnus-sethash ref id idhashtb)
3018                 (gnus-sethash id threads thhashtb))
3019             (setq gthread (gnus-gethash gid thhashtb))
3020             (unless entered
3021               ;; We enter a dummy root into the thread, if we
3022               ;; haven't done that already.
3023               (unless (stringp (caar gthread))
3024                 (setcar gthread (list (mail-header-subject (caar gthread))
3025                                       (car gthread))))
3026               ;; We add this new gathered thread to this gathered
3027               ;; thread.
3028               (setcdr (car gthread)
3029                       (nconc (cdar gthread) (list (car threads)))))
3030             ;; Add it into the thread hash table.
3031             (gnus-sethash id gthread thhashtb)
3032             (setq entered t)
3033             ;; Remove it from the list of threads.
3034             (setcdr prev (cdr threads))
3035             (setq threads prev))))
3036       (setq prev threads)
3037       (setq threads (cdr threads)))
3038     result))
3039
3040 (defun gnus-sort-gathered-threads (threads)
3041   "Sort subtreads inside each gathered thread by article number."
3042   (let ((result threads))
3043     (while threads
3044       (when (stringp (caar threads))
3045         (setcdr (car threads)
3046                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3047       (setq threads (cdr threads)))
3048     result))
3049
3050 (defun gnus-thread-loop-p (root thread)
3051   "Say whether ROOT is in THREAD."
3052   (let ((stack (list thread))
3053         (infloop 0)
3054         th)
3055     (while (setq thread (pop stack))
3056       (setq th (cdr thread))
3057       (while (and th
3058                   (not (eq (caar th) root)))
3059         (pop th))
3060       (if th
3061           ;; We have found a loop.
3062           (let (ref-dep)
3063             (setcdr thread (delq (car th) (cdr thread)))
3064             (if (boundp (setq ref-dep (intern "none"
3065                                               gnus-newsgroup-dependencies)))
3066                 (setcdr (symbol-value ref-dep)
3067                         (nconc (cdr (symbol-value ref-dep))
3068                                (list (car th))))
3069               (set ref-dep (list nil (car th))))
3070             (setq infloop 1
3071                   stack nil))
3072         ;; Push all the subthreads onto the stack.
3073         (push (cdr thread) stack)))
3074     infloop))
3075
3076 (defun gnus-make-threads ()
3077   "Go through the dependency hashtb and find the roots.  Return all threads."
3078   (let (threads)
3079     (while (catch 'infloop
3080              (mapatoms
3081               (lambda (refs)
3082                 ;; Deal with self-referencing References loops.
3083                 (when (and (car (symbol-value refs))
3084                            (not (zerop
3085                                  (apply
3086                                   '+
3087                                   (mapcar
3088                                    (lambda (thread)
3089                                      (gnus-thread-loop-p
3090                                       (car (symbol-value refs)) thread))
3091                                    (cdr (symbol-value refs)))))))
3092                   (setq threads nil)
3093                   (throw 'infloop t))
3094                 (unless (car (symbol-value refs))
3095                   ;; These threads do not refer back to any other articles,
3096                   ;; so they're roots.
3097                   (setq threads (append (cdr (symbol-value refs)) threads))))
3098               gnus-newsgroup-dependencies)))
3099     threads))
3100
3101 ;; Build the thread tree.
3102 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3103   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3104
3105 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3106 if it was already present.
3107
3108 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3109 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3110 Message-IDs will be renamed be renamed to a unique Message-ID before
3111 being entered.
3112
3113 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3114   (let* ((id (mail-header-id header))
3115          (id-dep (and id (intern id dependencies)))
3116          ref ref-dep ref-header)
3117     ;; Enter this `header' in the `dependencies' table.
3118     (cond
3119      ((not id-dep)
3120       (setq header nil))
3121      ;; The first two cases do the normal part: enter a new `header'
3122      ;; in the `dependencies' table.
3123      ((not (boundp id-dep))
3124       (set id-dep (list header)))
3125      ((null (car (symbol-value id-dep)))
3126       (setcar (symbol-value id-dep) header))
3127
3128      ;; From here the `header' was already present in the
3129      ;; `dependencies' table.
3130      (force-new
3131       ;; Overrides an existing entry;
3132       ;; just set the header part of the entry.
3133       (setcar (symbol-value id-dep) header))
3134
3135      ;; Renames the existing `header' to a unique Message-ID.
3136      ((not gnus-summary-ignore-duplicates)
3137       ;; An article with this Message-ID has already been seen.
3138       ;; We rename the Message-ID.
3139       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3140            (list header))
3141       (mail-header-set-id header id))
3142
3143      ;; The last case ignores an existing entry, except it adds any
3144      ;; additional Xrefs (in case the two articles came from different
3145      ;; servers.
3146      ;; Also sets `header' to `nil' meaning that the `dependencies'
3147      ;; table was *not* modified.
3148      (t
3149       (mail-header-set-xref
3150        (car (symbol-value id-dep))
3151        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3152                    "")
3153                (or (mail-header-xref header) "")))
3154       (setq header nil)))
3155
3156     (when header
3157       ;; First check if that we are not creating a References loop.
3158       (setq ref (gnus-parent-id (mail-header-references header)))
3159       (while (and ref
3160                   (setq ref-dep (intern-soft ref dependencies))
3161                   (boundp ref-dep)
3162                   (setq ref-header (car (symbol-value ref-dep))))
3163         (if (string= id ref)
3164             ;; Yuk!  This is a reference loop.  Make the article be a
3165             ;; root article.
3166             (progn
3167               (mail-header-set-references (car (symbol-value id-dep)) "none")
3168               (setq ref nil))
3169           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3170       (setq ref (gnus-parent-id (mail-header-references header)))
3171       (setq ref-dep (intern (or ref "none") dependencies))
3172       (if (boundp ref-dep)
3173           (setcdr (symbol-value ref-dep)
3174                   (nconc (cdr (symbol-value ref-dep))
3175                          (list (symbol-value id-dep))))
3176         (set ref-dep (list nil (symbol-value id-dep)))))
3177     header))
3178
3179 (defun gnus-build-sparse-threads ()
3180   (let ((headers gnus-newsgroup-headers)
3181         (mail-parse-charset gnus-newsgroup-charset)
3182         (gnus-summary-ignore-duplicates t)
3183         header references generation relations
3184         subject child end new-child date)
3185     ;; First we create an alist of generations/relations, where
3186     ;; generations is how much we trust the relation, and the relation
3187     ;; is parent/child.
3188     (gnus-message 7 "Making sparse threads...")
3189     (save-excursion
3190       (nnheader-set-temp-buffer " *gnus sparse threads*")
3191       (while (setq header (pop headers))
3192         (when (and (setq references (mail-header-references header))
3193                    (not (string= references "")))
3194           (insert references)
3195           (setq child (mail-header-id header)
3196                 subject (mail-header-subject header)
3197                 date (mail-header-date header)
3198                 generation 0)
3199           (while (search-backward ">" nil t)
3200             (setq end (1+ (point)))
3201             (when (search-backward "<" nil t)
3202               (setq new-child (buffer-substring (point) end))
3203               (push (list (incf generation)
3204                           child (setq child new-child)
3205                           subject date)
3206                     relations)))
3207           (when child
3208             (push (list (1+ generation) child nil subject) relations))
3209           (erase-buffer)))
3210       (kill-buffer (current-buffer)))
3211     ;; Sort over trustworthiness.
3212     (mapcar
3213      (lambda (relation)
3214        (when (gnus-dependencies-add-header
3215               (make-full-mail-header
3216                gnus-reffed-article-number
3217                (nth 3 relation) "" (or (nth 4 relation) "")
3218                (nth 1 relation)
3219                (or (nth 2 relation) "") 0 0 "")
3220               gnus-newsgroup-dependencies nil)
3221          (push gnus-reffed-article-number gnus-newsgroup-limit)
3222          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3223          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3224                gnus-newsgroup-reads)
3225          (decf gnus-reffed-article-number)))
3226      (sort relations 'car-less-than-car))
3227     (gnus-message 7 "Making sparse threads...done")))
3228
3229 (defun gnus-build-old-threads ()
3230   ;; Look at all the articles that refer back to old articles, and
3231   ;; fetch the headers for the articles that aren't there.  This will
3232   ;; build complete threads - if the roots haven't been expired by the
3233   ;; server, that is.
3234   (let ((mail-parse-charset gnus-newsgroup-charset)
3235         id heads)
3236     (mapatoms
3237      (lambda (refs)
3238        (when (not (car (symbol-value refs)))
3239          (setq heads (cdr (symbol-value refs)))
3240          (while heads
3241            (if (memq (mail-header-number (caar heads))
3242                      gnus-newsgroup-dormant)
3243                (setq heads (cdr heads))
3244              (setq id (symbol-name refs))
3245              (while (and (setq id (gnus-build-get-header id))
3246                          (not (car (gnus-id-to-thread id)))))
3247              (setq heads nil)))))
3248      gnus-newsgroup-dependencies)))
3249
3250 ;; This function has to be called with point after the article number
3251 ;; on the beginning of the line.
3252 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3253   (let ((eol (gnus-point-at-eol))
3254         (buffer (current-buffer))
3255         header)
3256
3257     ;; overview: [num subject from date id refs chars lines misc]
3258     (unwind-protect
3259         (progn
3260           (narrow-to-region (point) eol)
3261           (unless (eobp)
3262             (forward-char))
3263
3264           (setq header
3265                 (make-full-mail-header
3266                  number                         ; number
3267                  (nnheader-nov-field)           ; subject
3268                  (nnheader-nov-field)           ; from
3269                  (nnheader-nov-field)           ; date
3270                  (nnheader-nov-read-message-id) ; id
3271                  (nnheader-nov-field)           ; refs
3272                  (nnheader-nov-read-integer)    ; chars
3273                  (nnheader-nov-read-integer)    ; lines
3274                  (unless (eobp)
3275                    (nnheader-nov-field))        ; misc
3276                  (nnheader-nov-parse-extra))))  ; extra
3277
3278       (widen))
3279
3280     (when gnus-alter-header-function
3281       (funcall gnus-alter-header-function header))
3282     (gnus-dependencies-add-header header dependencies force-new)))
3283
3284 (defun gnus-build-get-header (id)
3285   ;; Look through the buffer of NOV lines and find the header to
3286   ;; ID.  Enter this line into the dependencies hash table, and return
3287   ;; the id of the parent article (if any).
3288   (let ((deps gnus-newsgroup-dependencies)
3289         found header)
3290     (prog1
3291         (save-excursion
3292           (set-buffer nntp-server-buffer)
3293           (let ((case-fold-search nil))
3294             (goto-char (point-min))
3295             (while (and (not found)
3296                         (search-forward id nil t))
3297               (beginning-of-line)
3298               (setq found (looking-at
3299                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3300                                    (regexp-quote id))))
3301               (or found (beginning-of-line 2)))
3302             (when found
3303               (beginning-of-line)
3304               (and
3305                (setq header (gnus-nov-parse-line
3306                              (read (current-buffer)) deps))
3307                (gnus-parent-id (mail-header-references header))))))
3308       (when header
3309         (let ((number (mail-header-number header)))
3310           (push number gnus-newsgroup-limit)
3311           (push header gnus-newsgroup-headers)
3312           (if (memq number gnus-newsgroup-unselected)
3313               (progn
3314                 (push number gnus-newsgroup-unreads)
3315                 (setq gnus-newsgroup-unselected
3316                       (delq number gnus-newsgroup-unselected)))
3317             (push number gnus-newsgroup-ancient)))))))
3318
3319 (defun gnus-build-all-threads ()
3320   "Read all the headers."
3321   (let ((gnus-summary-ignore-duplicates t)
3322         (mail-parse-charset gnus-newsgroup-charset)
3323         (dependencies gnus-newsgroup-dependencies)
3324         header article)
3325     (save-excursion
3326       (set-buffer nntp-server-buffer)
3327       (let ((case-fold-search nil))
3328         (goto-char (point-min))
3329         (while (not (eobp))
3330           (ignore-errors
3331             (setq article (read (current-buffer))
3332                   header (gnus-nov-parse-line article dependencies)))
3333           (when header
3334             (save-excursion
3335               (set-buffer gnus-summary-buffer)
3336               (push header gnus-newsgroup-headers)
3337               (if (memq (setq article (mail-header-number header))
3338                         gnus-newsgroup-unselected)
3339                   (progn
3340                     (push article gnus-newsgroup-unreads)
3341                     (setq gnus-newsgroup-unselected
3342                           (delq article gnus-newsgroup-unselected)))
3343                 (push article gnus-newsgroup-ancient)))
3344             (forward-line 1)))))))
3345
3346 (defun gnus-summary-update-article-line (article header)
3347   "Update the line for ARTICLE using HEADERS."
3348   (let* ((id (mail-header-id header))
3349          (thread (gnus-id-to-thread id)))
3350     (unless thread
3351       (error "Article in no thread"))
3352     ;; Update the thread.
3353     (setcar thread header)
3354     (gnus-summary-goto-subject article)
3355     (let* ((datal (gnus-data-find-list article))
3356            (data (car datal))
3357            (length (when (cdr datal)
3358                      (- (gnus-data-pos data)
3359                         (gnus-data-pos (cadr datal)))))
3360            (buffer-read-only nil)
3361            (level (gnus-summary-thread-level)))
3362       (gnus-delete-line)
3363       (gnus-summary-insert-line
3364        header level nil (gnus-article-mark article)
3365        (memq article gnus-newsgroup-replied)
3366        (memq article gnus-newsgroup-expirable)
3367        ;; Only insert the Subject string when it's different
3368        ;; from the previous Subject string.
3369        (if (gnus-subject-equal
3370             (condition-case ()
3371                 (mail-header-subject
3372                  (gnus-data-header
3373                   (cadr
3374                    (gnus-data-find-list
3375                     article
3376                     (gnus-data-list t)))))
3377               ;; Error on the side of excessive subjects.
3378               (error ""))
3379             (mail-header-subject header))
3380            ""
3381          (mail-header-subject header))
3382        nil (cdr (assq article gnus-newsgroup-scored))
3383        (memq article gnus-newsgroup-processable))
3384       (when length
3385         (gnus-data-update-list
3386          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3387
3388 (defun gnus-summary-update-article (article &optional iheader)
3389   "Update ARTICLE in the summary buffer."
3390   (set-buffer gnus-summary-buffer)
3391   (let* ((header (gnus-summary-article-header article))
3392          (id (mail-header-id header))
3393          (data (gnus-data-find article))
3394          (thread (gnus-id-to-thread id))
3395          (references (mail-header-references header))
3396          (parent
3397           (gnus-id-to-thread
3398            (or (gnus-parent-id
3399                 (when (and references
3400                            (not (equal "" references)))
3401                   references))
3402                "none")))
3403          (buffer-read-only nil)
3404          (old (car thread)))
3405     (when thread
3406       (unless iheader
3407         (setcar thread nil)
3408         (when parent
3409           (delq thread parent)))
3410       (if (gnus-summary-insert-subject id header)
3411           ;; Set the (possibly) new article number in the data structure.
3412           (gnus-data-set-number data (gnus-id-to-article id))
3413         (setcar thread old)
3414         nil))))
3415
3416 (defun gnus-rebuild-thread (id &optional line)
3417   "Rebuild the thread containing ID.
3418 If LINE, insert the rebuilt thread starting on line LINE."
3419   (let ((buffer-read-only nil)
3420         old-pos current thread data)
3421     (if (not gnus-show-threads)
3422         (setq thread (list (car (gnus-id-to-thread id))))
3423       ;; Get the thread this article is part of.
3424       (setq thread (gnus-remove-thread id)))
3425     (setq old-pos (gnus-point-at-bol))
3426     (setq current (save-excursion
3427                     (and (zerop (forward-line -1))
3428                          (gnus-summary-article-number))))
3429     ;; If this is a gathered thread, we have to go some re-gathering.
3430     (when (stringp (car thread))
3431       (let ((subject (car thread))
3432             roots thr)
3433         (setq thread (cdr thread))
3434         (while thread
3435           (unless (memq (setq thr (gnus-id-to-thread
3436                                    (gnus-root-id
3437                                     (mail-header-id (caar thread)))))
3438                         roots)
3439             (push thr roots))
3440           (setq thread (cdr thread)))
3441         ;; We now have all (unique) roots.
3442         (if (= (length roots) 1)
3443             ;; All the loose roots are now one solid root.
3444             (setq thread (car roots))
3445           (setq thread (cons subject (gnus-sort-threads roots))))))
3446     (let (threads)
3447       ;; We then insert this thread into the summary buffer.
3448       (when line
3449         (goto-char (point-min))
3450         (forward-line (1- line)))
3451       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3452         (if gnus-show-threads
3453             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3454           (gnus-summary-prepare-unthreaded thread))
3455         (setq data (nreverse gnus-newsgroup-data))
3456         (setq threads gnus-newsgroup-threads))
3457       ;; We splice the new data into the data structure.
3458       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3459       ;;!!! then we want to insert at the beginning of the buffer.
3460       ;;!!! That happens to be true with Gnus now, but that may
3461       ;;!!! change in the future.  Perhaps.
3462       (gnus-data-enter-list
3463        (if line nil current) data (- (point) old-pos))
3464       (setq gnus-newsgroup-threads
3465             (nconc threads gnus-newsgroup-threads))
3466       (gnus-data-compute-positions))))
3467
3468 (defun gnus-number-to-header (number)
3469   "Return the header for article NUMBER."
3470   (let ((headers gnus-newsgroup-headers))
3471     (while (and headers
3472                 (not (= number (mail-header-number (car headers)))))
3473       (pop headers))
3474     (when headers
3475       (car headers))))
3476
3477 (defun gnus-parent-headers (in-headers &optional generation)
3478   "Return the headers of the GENERATIONeth parent of HEADERS."
3479   (unless generation
3480     (setq generation 1))
3481   (let ((parent t)
3482         (headers in-headers)
3483         references)
3484     (while (and parent
3485                 (not (zerop generation))
3486                 (setq references (mail-header-references headers)))
3487       (setq headers (if (and references
3488                              (setq parent (gnus-parent-id references)))
3489                         (car (gnus-id-to-thread parent))
3490                       nil))
3491       (decf generation))
3492     (and (not (eq headers in-headers))
3493          headers)))
3494
3495 (defun gnus-id-to-thread (id)
3496   "Return the (sub-)thread where ID appears."
3497   (gnus-gethash id gnus-newsgroup-dependencies))
3498
3499 (defun gnus-id-to-article (id)
3500   "Return the article number of ID."
3501   (let ((thread (gnus-id-to-thread id)))
3502     (when (and thread
3503                (car thread))
3504       (mail-header-number (car thread)))))
3505
3506 (defun gnus-id-to-header (id)
3507   "Return the article headers of ID."
3508   (car (gnus-id-to-thread id)))
3509
3510 (defun gnus-article-displayed-root-p (article)
3511   "Say whether ARTICLE is a root(ish) article."
3512   (let ((level (gnus-summary-thread-level article))
3513         (refs (mail-header-references  (gnus-summary-article-header article)))
3514         particle)
3515     (cond
3516      ((null level) nil)
3517      ((zerop level) t)
3518      ((null refs) t)
3519      ((null (gnus-parent-id refs)) t)
3520      ((and (= 1 level)
3521            (null (setq particle (gnus-id-to-article
3522                                  (gnus-parent-id refs))))
3523            (null (gnus-summary-thread-level particle)))))))
3524
3525 (defun gnus-root-id (id)
3526   "Return the id of the root of the thread where ID appears."
3527   (let (last-id prev)
3528     (while (and id (setq prev (car (gnus-id-to-thread id))))
3529       (setq last-id id
3530             id (gnus-parent-id (mail-header-references prev))))
3531     last-id))
3532
3533 (defun gnus-articles-in-thread (thread)
3534   "Return the list of articles in THREAD."
3535   (cons (mail-header-number (car thread))
3536         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3537
3538 (defun gnus-remove-thread (id &optional dont-remove)
3539   "Remove the thread that has ID in it."
3540   (let (headers thread last-id)
3541     ;; First go up in this thread until we find the root.
3542     (setq last-id (gnus-root-id id)
3543           headers (message-flatten-list (gnus-id-to-thread last-id)))
3544     ;; We have now found the real root of this thread.  It might have
3545     ;; been gathered into some loose thread, so we have to search
3546     ;; through the threads to find the thread we wanted.
3547     (let ((threads gnus-newsgroup-threads)
3548           sub)
3549       (while threads
3550         (setq sub (car threads))
3551         (if (stringp (car sub))
3552             ;; This is a gathered thread, so we look at the roots
3553             ;; below it to find whether this article is in this
3554             ;; gathered root.
3555             (progn
3556               (setq sub (cdr sub))
3557               (while sub
3558                 (when (member (caar sub) headers)
3559                   (setq thread (car threads)
3560                         threads nil
3561                         sub nil))
3562                 (setq sub (cdr sub))))
3563           ;; It's an ordinary thread, so we check it.
3564           (when (eq (car sub) (car headers))
3565             (setq thread sub
3566                   threads nil)))
3567         (setq threads (cdr threads)))
3568       ;; If this article is in no thread, then it's a root.
3569       (if thread
3570           (unless dont-remove
3571             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3572         (setq thread (gnus-id-to-thread last-id)))
3573       (when thread
3574         (prog1
3575             thread                      ; We return this thread.
3576           (unless dont-remove
3577             (if (stringp (car thread))
3578                 (progn
3579                   ;; If we use dummy roots, then we have to remove the
3580                   ;; dummy root as well.
3581                   (when (eq gnus-summary-make-false-root 'dummy)
3582                     ;; We go to the dummy root by going to
3583                     ;; the first sub-"thread", and then one line up.
3584                     (gnus-summary-goto-article
3585                      (mail-header-number (caadr thread)))
3586                     (forward-line -1)
3587                     (gnus-delete-line)
3588                     (gnus-data-compute-positions))
3589                   (setq thread (cdr thread))
3590                   (while thread
3591                     (gnus-remove-thread-1 (car thread))
3592                     (setq thread (cdr thread))))
3593               (gnus-summary-show-all-threads)
3594               (gnus-remove-thread-1 thread))))))))
3595
3596 (defun gnus-remove-thread-1 (thread)
3597   "Remove the thread THREAD recursively."
3598   (let ((number (mail-header-number (pop thread)))
3599         d)
3600     (setq thread (reverse thread))
3601     (while thread
3602       (gnus-remove-thread-1 (pop thread)))
3603     (when (setq d (gnus-data-find number))
3604       (goto-char (gnus-data-pos d))
3605       (gnus-data-remove
3606        number
3607        (- (gnus-point-at-bol)
3608           (prog1
3609               (1+ (gnus-point-at-eol))
3610             (gnus-delete-line)))))))
3611
3612 (defun gnus-sort-threads (threads)
3613   "Sort THREADS."
3614   (if (not gnus-thread-sort-functions)
3615       threads
3616     (gnus-message 8 "Sorting threads...")
3617     (prog1
3618         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3619       (gnus-message 8 "Sorting threads...done"))))
3620
3621 (defun gnus-sort-articles (articles)
3622   "Sort ARTICLES."
3623   (when gnus-article-sort-functions
3624     (gnus-message 7 "Sorting articles...")
3625     (prog1
3626         (setq gnus-newsgroup-headers
3627               (sort articles (gnus-make-sort-function
3628                               gnus-article-sort-functions)))
3629       (gnus-message 7 "Sorting articles...done"))))
3630
3631 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3632 (defmacro gnus-thread-header (thread)
3633   ;; Return header of first article in THREAD.
3634   ;; Note that THREAD must never, ever be anything else than a variable -
3635   ;; using some other form will lead to serious barfage.
3636   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3637   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3638   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3639         (vector thread) 2))
3640
3641 (defsubst gnus-article-sort-by-number (h1 h2)
3642   "Sort articles by article number."
3643   (< (mail-header-number h1)
3644      (mail-header-number h2)))
3645
3646 (defun gnus-thread-sort-by-number (h1 h2)
3647   "Sort threads by root article number."
3648   (gnus-article-sort-by-number
3649    (gnus-thread-header h1) (gnus-thread-header h2)))
3650
3651 (defsubst gnus-article-sort-by-lines (h1 h2)
3652   "Sort articles by article Lines header."
3653   (< (mail-header-lines h1)
3654      (mail-header-lines h2)))
3655
3656 (defun gnus-thread-sort-by-lines (h1 h2)
3657   "Sort threads by root article Lines header."
3658   (gnus-article-sort-by-lines
3659    (gnus-thread-header h1) (gnus-thread-header h2)))
3660
3661 (defsubst gnus-article-sort-by-chars (h1 h2)
3662   "Sort articles by octet length."
3663   (< (mail-header-chars h1)
3664      (mail-header-chars h2)))
3665
3666 (defun gnus-thread-sort-by-chars (h1 h2)
3667   "Sort threads by root article octet length."
3668   (gnus-article-sort-by-chars
3669    (gnus-thread-header h1) (gnus-thread-header h2)))
3670
3671 (defsubst gnus-article-sort-by-author (h1 h2)
3672   "Sort articles by root author."
3673   (string-lessp
3674    (let ((extract (funcall
3675                    gnus-extract-address-components
3676                    (mail-header-from h1))))
3677      (or (car extract) (cadr extract) ""))
3678    (let ((extract (funcall
3679                    gnus-extract-address-components
3680                    (mail-header-from h2))))
3681      (or (car extract) (cadr extract) ""))))
3682
3683 (defun gnus-thread-sort-by-author (h1 h2)
3684   "Sort threads by root author."
3685   (gnus-article-sort-by-author
3686    (gnus-thread-header h1)  (gnus-thread-header h2)))
3687
3688 (defsubst gnus-article-sort-by-subject (h1 h2)
3689   "Sort articles by root subject."
3690   (string-lessp
3691    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3692    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3693
3694 (defun gnus-thread-sort-by-subject (h1 h2)
3695   "Sort threads by root subject."
3696   (gnus-article-sort-by-subject
3697    (gnus-thread-header h1) (gnus-thread-header h2)))
3698
3699 (defsubst gnus-article-sort-by-date (h1 h2)
3700   "Sort articles by root article date."
3701   (time-less-p
3702    (gnus-date-get-time (mail-header-date h1))
3703    (gnus-date-get-time (mail-header-date h2))))
3704
3705 (defun gnus-thread-sort-by-date (h1 h2)
3706   "Sort threads by root article date."
3707   (gnus-article-sort-by-date
3708    (gnus-thread-header h1) (gnus-thread-header h2)))
3709
3710 (defsubst gnus-article-sort-by-score (h1 h2)
3711   "Sort articles by root article score.
3712 Unscored articles will be counted as having a score of zero."
3713   (> (or (cdr (assq (mail-header-number h1)
3714                     gnus-newsgroup-scored))
3715          gnus-summary-default-score 0)
3716      (or (cdr (assq (mail-header-number h2)
3717                     gnus-newsgroup-scored))
3718          gnus-summary-default-score 0)))
3719
3720 (defun gnus-thread-sort-by-score (h1 h2)
3721   "Sort threads by root article score."
3722   (gnus-article-sort-by-score
3723    (gnus-thread-header h1) (gnus-thread-header h2)))
3724
3725 (defun gnus-thread-sort-by-total-score (h1 h2)
3726   "Sort threads by the sum of all scores in the thread.
3727 Unscored articles will be counted as having a score of zero."
3728   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3729
3730 (defun gnus-thread-total-score (thread)
3731   ;; This function find the total score of THREAD.
3732   (cond ((null thread)
3733          0)
3734         ((consp thread)
3735          (if (stringp (car thread))
3736              (apply gnus-thread-score-function 0
3737                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3738            (gnus-thread-total-score-1 thread)))
3739         (t
3740          (gnus-thread-total-score-1 (list thread)))))
3741
3742 (defun gnus-thread-total-score-1 (root)
3743   ;; This function find the total score of the thread below ROOT.
3744   (setq root (car root))
3745   (apply gnus-thread-score-function
3746          (or (append
3747               (mapcar 'gnus-thread-total-score
3748                       (cdr (gnus-id-to-thread (mail-header-id root))))
3749               (when (> (mail-header-number root) 0)
3750                 (list (or (cdr (assq (mail-header-number root)
3751                                      gnus-newsgroup-scored))
3752                           gnus-summary-default-score 0))))
3753              (list gnus-summary-default-score)
3754              '(0))))
3755
3756 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3757 (defvar gnus-tmp-prev-subject nil)
3758 (defvar gnus-tmp-false-parent nil)
3759 (defvar gnus-tmp-root-expunged nil)
3760 (defvar gnus-tmp-dummy-line nil)
3761
3762 (defvar gnus-tmp-header)
3763 (defun gnus-extra-header (type &optional header)
3764   "Return the extra header of TYPE."
3765   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3766       ""))
3767
3768 (defun gnus-summary-prepare-threads (threads)
3769   "Prepare summary buffer from THREADS and indentation LEVEL.
3770 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3771 or a straight list of headers."
3772   (gnus-message 7 "Generating summary...")
3773
3774   (setq gnus-newsgroup-threads threads)
3775   (beginning-of-line)
3776
3777   (let ((gnus-tmp-level 0)
3778         (default-score (or gnus-summary-default-score 0))
3779         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3780         thread number subject stack state gnus-tmp-gathered beg-match
3781         new-roots gnus-tmp-new-adopts thread-end
3782         gnus-tmp-header gnus-tmp-unread
3783         gnus-tmp-replied gnus-tmp-subject-or-nil
3784         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3785         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3786         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3787
3788     (setq gnus-tmp-prev-subject nil)
3789
3790     (if (vectorp (car threads))
3791         ;; If this is a straight (sic) list of headers, then a
3792         ;; threaded summary display isn't required, so we just create
3793         ;; an unthreaded one.
3794         (gnus-summary-prepare-unthreaded threads)
3795
3796       ;; Do the threaded display.
3797
3798       (while (or threads stack gnus-tmp-new-adopts new-roots)
3799
3800         (if (and (= gnus-tmp-level 0)
3801                  (or (not stack)
3802                      (= (caar stack) 0))
3803                  (not gnus-tmp-false-parent)
3804                  (or gnus-tmp-new-adopts new-roots))
3805             (if gnus-tmp-new-adopts
3806                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3807                       thread (list (car gnus-tmp-new-adopts))
3808                       gnus-tmp-header (caar thread)
3809                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3810               (when new-roots
3811                 (setq thread (list (car new-roots))
3812                       gnus-tmp-header (caar thread)
3813                       new-roots (cdr new-roots))))
3814
3815           (if threads
3816               ;; If there are some threads, we do them before the
3817               ;; threads on the stack.
3818               (setq thread threads
3819                     gnus-tmp-header (caar thread))
3820             ;; There were no current threads, so we pop something off
3821             ;; the stack.
3822             (setq state (car stack)
3823                   gnus-tmp-level (car state)
3824                   thread (cdr state)
3825                   stack (cdr stack)
3826                   gnus-tmp-header (caar thread))))
3827
3828         (setq gnus-tmp-false-parent nil)
3829         (setq gnus-tmp-root-expunged nil)
3830         (setq thread-end nil)
3831
3832         (if (stringp gnus-tmp-header)
3833             ;; The header is a dummy root.
3834             (cond
3835              ((eq gnus-summary-make-false-root 'adopt)
3836               ;; We let the first article adopt the rest.
3837               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3838                                                (cddar thread)))
3839               (setq gnus-tmp-gathered
3840                     (nconc (mapcar
3841                             (lambda (h) (mail-header-number (car h)))
3842                             (cddar thread))
3843                            gnus-tmp-gathered))
3844               (setq thread (cons (list (caar thread)
3845                                        (cadar thread))
3846                                  (cdr thread)))
3847               (setq gnus-tmp-level -1
3848                     gnus-tmp-false-parent t))
3849              ((eq gnus-summary-make-false-root 'empty)
3850               ;; We print adopted articles with empty subject fields.
3851               (setq gnus-tmp-gathered
3852                     (nconc (mapcar
3853                             (lambda (h) (mail-header-number (car h)))
3854                             (cddar thread))
3855                            gnus-tmp-gathered))
3856               (setq gnus-tmp-level -1))
3857              ((eq gnus-summary-make-false-root 'dummy)
3858               ;; We remember that we probably want to output a dummy
3859               ;; root.
3860               (setq gnus-tmp-dummy-line gnus-tmp-header)
3861               (setq gnus-tmp-prev-subject gnus-tmp-header))
3862              (t
3863               ;; We do not make a root for the gathered
3864               ;; sub-threads at all.
3865               (setq gnus-tmp-level -1)))
3866
3867           (setq number (mail-header-number gnus-tmp-header)
3868                 subject (mail-header-subject gnus-tmp-header))
3869
3870           (cond
3871            ;; If the thread has changed subject, we might want to make
3872            ;; this subthread into a root.
3873            ((and (null gnus-thread-ignore-subject)
3874                  (not (zerop gnus-tmp-level))
3875                  gnus-tmp-prev-subject
3876                  (not (inline
3877                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3878             (setq new-roots (nconc new-roots (list (car thread)))
3879                   thread-end t
3880                   gnus-tmp-header nil))
3881            ;; If the article lies outside the current limit,
3882            ;; then we do not display it.
3883            ((not (memq number gnus-newsgroup-limit))
3884             (setq gnus-tmp-gathered
3885                   (nconc (mapcar
3886                           (lambda (h) (mail-header-number (car h)))
3887                           (cdar thread))
3888                          gnus-tmp-gathered))
3889             (setq gnus-tmp-new-adopts (if (cdar thread)
3890                                           (append gnus-tmp-new-adopts
3891                                                   (cdar thread))
3892                                         gnus-tmp-new-adopts)
3893                   thread-end t
3894                   gnus-tmp-header nil)
3895             (when (zerop gnus-tmp-level)
3896               (setq gnus-tmp-root-expunged t)))
3897            ;; Perhaps this article is to be marked as read?
3898            ((and gnus-summary-mark-below
3899                  (< (or (cdr (assq number gnus-newsgroup-scored))
3900                         default-score)
3901                     gnus-summary-mark-below)
3902                  ;; Don't touch sparse articles.
3903                  (not (gnus-summary-article-sparse-p number))
3904                  (not (gnus-summary-article-ancient-p number)))
3905             (setq gnus-newsgroup-unreads
3906                   (delq number gnus-newsgroup-unreads))
3907             (if gnus-newsgroup-auto-expire
3908                 (push number gnus-newsgroup-expirable)
3909               (push (cons number gnus-low-score-mark)
3910                     gnus-newsgroup-reads))))
3911
3912           (when gnus-tmp-header
3913             ;; We may have an old dummy line to output before this
3914             ;; article.
3915             (when (and gnus-tmp-dummy-line
3916                        (gnus-subject-equal
3917                         gnus-tmp-dummy-line
3918                         (mail-header-subject gnus-tmp-header)))
3919               (gnus-summary-insert-dummy-line
3920                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3921               (setq gnus-tmp-dummy-line nil))
3922
3923             ;; Compute the mark.
3924             (setq gnus-tmp-unread (gnus-article-mark number))
3925
3926             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3927                                   gnus-tmp-header gnus-tmp-level)
3928                   gnus-newsgroup-data)
3929
3930             ;; Actually insert the line.
3931             (setq
3932              gnus-tmp-subject-or-nil
3933              (cond
3934               ((and gnus-thread-ignore-subject
3935                     gnus-tmp-prev-subject
3936                     (not (inline (gnus-subject-equal
3937                                   gnus-tmp-prev-subject subject))))
3938                subject)
3939               ((zerop gnus-tmp-level)
3940                (if (and (eq gnus-summary-make-false-root 'empty)
3941                         (memq number gnus-tmp-gathered)
3942                         gnus-tmp-prev-subject
3943                         (inline (gnus-subject-equal
3944                                  gnus-tmp-prev-subject subject)))
3945                    gnus-summary-same-subject
3946                  subject))
3947               (t gnus-summary-same-subject)))
3948             (if (and (eq gnus-summary-make-false-root 'adopt)
3949                      (= gnus-tmp-level 1)
3950                      (memq number gnus-tmp-gathered))
3951                 (setq gnus-tmp-opening-bracket ?\<
3952                       gnus-tmp-closing-bracket ?\>)
3953               (setq gnus-tmp-opening-bracket ?\[
3954                     gnus-tmp-closing-bracket ?\]))
3955             (setq
3956              gnus-tmp-indentation
3957              (aref gnus-thread-indent-array gnus-tmp-level)
3958              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3959              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3960                                 gnus-summary-default-score 0)
3961              gnus-tmp-score-char
3962              (if (or (null gnus-summary-default-score)
3963                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3964                          gnus-summary-zcore-fuzz))
3965                  ? ;Whitespace
3966                (if (< gnus-tmp-score gnus-summary-default-score)
3967                    gnus-score-below-mark gnus-score-over-mark))
3968              gnus-tmp-replied
3969              (cond ((memq number gnus-newsgroup-processable)
3970                     gnus-process-mark)
3971                    ((memq number gnus-newsgroup-cached)
3972                     gnus-cached-mark)
3973                    ((memq number gnus-newsgroup-replied)
3974                     gnus-replied-mark)
3975                    ((memq number gnus-newsgroup-saved)
3976                     gnus-saved-mark)
3977                    (t gnus-unread-mark))
3978              gnus-tmp-from (mail-header-from gnus-tmp-header)
3979              gnus-tmp-name
3980              (cond
3981               ((string-match "<[^>]+> *$" gnus-tmp-from)
3982                (setq beg-match (match-beginning 0))
3983                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3984                         (substring gnus-tmp-from (1+ (match-beginning 0))
3985                                    (1- (match-end 0))))
3986                    (substring gnus-tmp-from 0 beg-match)))
3987               ((string-match "(.+)" gnus-tmp-from)
3988                (substring gnus-tmp-from
3989                           (1+ (match-beginning 0)) (1- (match-end 0))))
3990               (t gnus-tmp-from)))
3991             (when (string= gnus-tmp-name "")
3992               (setq gnus-tmp-name gnus-tmp-from))
3993             (unless (numberp gnus-tmp-lines)
3994               (setq gnus-tmp-lines 0))
3995             (gnus-put-text-property
3996              (point)
3997              (progn (eval gnus-summary-line-format-spec) (point))
3998              'gnus-number number)
3999             (when gnus-visual-p
4000               (forward-line -1)
4001               (gnus-run-hooks 'gnus-summary-update-hook)
4002               (forward-line 1))
4003
4004             (setq gnus-tmp-prev-subject subject)))
4005
4006         (when (nth 1 thread)
4007           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4008         (incf gnus-tmp-level)
4009         (setq threads (if thread-end nil (cdar thread)))
4010         (unless threads
4011           (setq gnus-tmp-level 0)))))
4012   (gnus-message 7 "Generating summary...done"))
4013
4014 (defun gnus-summary-prepare-unthreaded (headers)
4015   "Generate an unthreaded summary buffer based on HEADERS."
4016   (let (header number mark)
4017
4018     (beginning-of-line)
4019
4020     (while headers
4021       ;; We may have to root out some bad articles...
4022       (when (memq (setq number (mail-header-number
4023                                 (setq header (pop headers))))
4024                   gnus-newsgroup-limit)
4025         ;; Mark article as read when it has a low score.
4026         (when (and gnus-summary-mark-below
4027                    (< (or (cdr (assq number gnus-newsgroup-scored))
4028                           gnus-summary-default-score 0)
4029                       gnus-summary-mark-below)
4030                    (not (gnus-summary-article-ancient-p number)))
4031           (setq gnus-newsgroup-unreads
4032                 (delq number gnus-newsgroup-unreads))
4033           (if gnus-newsgroup-auto-expire
4034               (push number gnus-newsgroup-expirable)
4035             (push (cons number gnus-low-score-mark)
4036                   gnus-newsgroup-reads)))
4037
4038         (setq mark (gnus-article-mark number))
4039         (push (gnus-data-make number mark (1+ (point)) header 0)
4040               gnus-newsgroup-data)
4041         (gnus-summary-insert-line
4042          header 0 number
4043          mark (memq number gnus-newsgroup-replied)
4044          (memq number gnus-newsgroup-expirable)
4045          (mail-header-subject header) nil
4046          (cdr (assq number gnus-newsgroup-scored))
4047          (memq number gnus-newsgroup-processable))))))
4048
4049 (defun gnus-summary-remove-list-identifiers ()
4050   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4051   (let ((regexp (if (stringp gnus-list-identifiers)
4052                     gnus-list-identifiers
4053                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4054     (dolist (header gnus-newsgroup-headers)
4055       (when (string-match (concat "\\(Re: +\\)?\\(" regexp " *\\)")
4056                           (mail-header-subject header))
4057         (mail-header-set-subject
4058          header (concat (substring (mail-header-subject header)
4059                                    0 (match-beginning 2))
4060                         (substring (mail-header-subject header)
4061                                    (match-end 2))))))))
4062
4063 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4064   "Select newsgroup GROUP.
4065 If READ-ALL is non-nil, all articles in the group are selected.
4066 If SELECT-ARTICLES, only select those articles from GROUP."
4067   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4068          ;;!!! Dirty hack; should be removed.
4069          (gnus-summary-ignore-duplicates
4070           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4071               t
4072             gnus-summary-ignore-duplicates))
4073          (info (nth 2 entry))
4074          articles fetched-articles cached)
4075
4076     (unless (gnus-check-server
4077              (setq gnus-current-select-method
4078                    (gnus-find-method-for-group group)))
4079       (error "Couldn't open server"))
4080
4081     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4082         (gnus-activate-group group)     ; Or we can activate it...
4083         (progn                          ; Or we bug out.
4084           (when (equal major-mode 'gnus-summary-mode)
4085             (kill-buffer (current-buffer)))
4086           (error "Couldn't request group %s: %s"
4087                  group (gnus-status-message group))))
4088
4089     (unless (gnus-request-group group t)
4090       (when (equal major-mode 'gnus-summary-mode)
4091         (kill-buffer (current-buffer)))
4092       (error "Couldn't request group %s: %s"
4093              group (gnus-status-message group)))
4094
4095     (setq gnus-newsgroup-name group)
4096     (setq gnus-newsgroup-unselected nil)
4097     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4098     (gnus-summary-setup-default-charset)
4099
4100     ;; Adjust and set lists of article marks.
4101     (when info
4102       (gnus-adjust-marked-articles info))
4103
4104     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4105     (when (gnus-virtual-group-p group)
4106       (setq cached gnus-newsgroup-cached))
4107
4108     (setq gnus-newsgroup-unreads
4109           (gnus-set-difference
4110            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4111            gnus-newsgroup-dormant))
4112
4113     (setq gnus-newsgroup-processable nil)
4114
4115     (gnus-update-read-articles group gnus-newsgroup-unreads)
4116
4117     (if (setq articles select-articles)
4118         (setq gnus-newsgroup-unselected
4119               (gnus-sorted-intersection
4120                gnus-newsgroup-unreads
4121                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4122       (setq articles (gnus-articles-to-read group read-all)))
4123
4124     (cond
4125      ((null articles)
4126       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4127       'quit)
4128      ((eq articles 0) nil)
4129      (t
4130       ;; Init the dependencies hash table.
4131       (setq gnus-newsgroup-dependencies
4132             (gnus-make-hashtable (length articles)))
4133       (gnus-set-global-variables)
4134       ;; Retrieve the headers and read them in.
4135       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4136       (setq gnus-newsgroup-headers
4137             (if (eq 'nov
4138                     (setq gnus-headers-retrieved-by
4139                           (gnus-retrieve-headers
4140                            articles gnus-newsgroup-name
4141                            ;; We might want to fetch old headers, but
4142                            ;; not if there is only 1 article.
4143                            (and (or (and
4144                                      (not (eq gnus-fetch-old-headers 'some))
4145                                      (not (numberp gnus-fetch-old-headers)))
4146                                     (> (length articles) 1))
4147                                 gnus-fetch-old-headers))))
4148                 (gnus-get-newsgroup-headers-xover
4149                  articles nil nil gnus-newsgroup-name t)
4150               (gnus-get-newsgroup-headers)))
4151       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4152
4153       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4154       (when cached
4155         (setq gnus-newsgroup-cached cached))
4156
4157       ;; Suppress duplicates?
4158       (when gnus-suppress-duplicates
4159         (gnus-dup-suppress-articles))
4160
4161       ;; Set the initial limit.
4162       (setq gnus-newsgroup-limit (copy-sequence articles))
4163       ;; Remove canceled articles from the list of unread articles.
4164       (setq gnus-newsgroup-unreads
4165             (gnus-set-sorted-intersection
4166              gnus-newsgroup-unreads
4167              (setq fetched-articles
4168                    (mapcar (lambda (headers) (mail-header-number headers))
4169                            gnus-newsgroup-headers))))
4170       ;; Removed marked articles that do not exist.
4171       (gnus-update-missing-marks
4172        (gnus-sorted-complement fetched-articles articles))
4173       ;; We might want to build some more threads first.
4174       (when (and gnus-fetch-old-headers
4175                  (eq gnus-headers-retrieved-by 'nov))
4176         (if (eq gnus-fetch-old-headers 'invisible)
4177             (gnus-build-all-threads)
4178           (gnus-build-old-threads)))
4179       ;; Let the Gnus agent mark articles as read.
4180       (when gnus-agent
4181         (gnus-agent-get-undownloaded-list))
4182       ;; Remove list identifiers from subject
4183       (when gnus-list-identifiers
4184         (gnus-summary-remove-list-identifiers))
4185       ;; Check whether auto-expire is to be done in this group.
4186       (setq gnus-newsgroup-auto-expire
4187             (gnus-group-auto-expirable-p group))
4188       ;; Set up the article buffer now, if necessary.
4189       (unless gnus-single-article-buffer
4190         (gnus-article-setup-buffer))
4191       ;; First and last article in this newsgroup.
4192       (when gnus-newsgroup-headers
4193         (setq gnus-newsgroup-begin
4194               (mail-header-number (car gnus-newsgroup-headers))
4195               gnus-newsgroup-end
4196               (mail-header-number
4197                (gnus-last-element gnus-newsgroup-headers))))
4198       ;; GROUP is successfully selected.
4199       (or gnus-newsgroup-headers t)))))
4200
4201 (defun gnus-articles-to-read (group &optional read-all)
4202   ;; Find out what articles the user wants to read.
4203   (let* ((articles
4204           ;; Select all articles if `read-all' is non-nil, or if there
4205           ;; are no unread articles.
4206           (if (or read-all
4207                   (and (zerop (length gnus-newsgroup-marked))
4208                        (zerop (length gnus-newsgroup-unreads)))
4209                   (eq (gnus-group-find-parameter group 'display)
4210                       'all))
4211               (or
4212                (gnus-uncompress-range (gnus-active group))
4213                (gnus-cache-articles-in-group group))
4214             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4215                           (copy-sequence gnus-newsgroup-unreads))
4216                   '<)))
4217          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4218          (scored (length scored-list))
4219          (number (length articles))
4220          (marked (+ (length gnus-newsgroup-marked)
4221                     (length gnus-newsgroup-dormant)))
4222          (select
4223           (cond
4224            ((numberp read-all)
4225             read-all)
4226            (t
4227             (condition-case ()
4228                 (cond
4229                  ((and (or (<= scored marked) (= scored number))
4230                        (numberp gnus-large-newsgroup)
4231                        (> number gnus-large-newsgroup))
4232                   (let ((input
4233                          (read-string
4234                           (format
4235                            "How many articles from %s (default %d): "
4236                            (gnus-limit-string gnus-newsgroup-name 35)
4237                            number))))
4238                     (if (string-match "^[ \t]*$" input) number input)))
4239                  ((and (> scored marked) (< scored number)
4240                        (> (- scored number) 20))
4241                   (let ((input
4242                          (read-string
4243                           (format "%s %s (%d scored, %d total): "
4244                                   "How many articles from"
4245                                   group scored number))))
4246                     (if (string-match "^[ \t]*$" input)
4247                         number input)))
4248                  (t number))
4249               (quit nil))))))
4250     (setq select (if (stringp select) (string-to-number select) select))
4251     (if (or (null select) (zerop select))
4252         select
4253       (if (and (not (zerop scored)) (<= (abs select) scored))
4254           (progn
4255             (setq articles (sort scored-list '<))
4256             (setq number (length articles)))
4257         (setq articles (copy-sequence articles)))
4258
4259       (when (< (abs select) number)
4260         (if (< select 0)
4261             ;; Select the N oldest articles.
4262             (setcdr (nthcdr (1- (abs select)) articles) nil)
4263           ;; Select the N most recent articles.
4264           (setq articles (nthcdr (- number select) articles))))
4265       (setq gnus-newsgroup-unselected
4266             (gnus-sorted-intersection
4267              gnus-newsgroup-unreads
4268              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4269       articles)))
4270
4271 (defun gnus-killed-articles (killed articles)
4272   (let (out)
4273     (while articles
4274       (when (inline (gnus-member-of-range (car articles) killed))
4275         (push (car articles) out))
4276       (setq articles (cdr articles)))
4277     out))
4278
4279 (defun gnus-uncompress-marks (marks)
4280   "Uncompress the mark ranges in MARKS."
4281   (let ((uncompressed '(score bookmark))
4282         out)
4283     (while marks
4284       (if (memq (caar marks) uncompressed)
4285           (push (car marks) out)
4286         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4287       (setq marks (cdr marks)))
4288     out))
4289
4290 (defun gnus-adjust-marked-articles (info)
4291   "Set all article lists and remove all marks that are no longer valid."
4292   (let* ((marked-lists (gnus-info-marks info))
4293          (active (gnus-active (gnus-info-group info)))
4294          (min (car active))
4295          (max (cdr active))
4296          (types gnus-article-mark-lists)
4297          (uncompressed '(score bookmark killed))
4298          marks var articles article mark)
4299
4300     (while marked-lists
4301       (setq marks (pop marked-lists))
4302       (set (setq var (intern (format "gnus-newsgroup-%s"
4303                                      (car (rassq (setq mark (car marks))
4304                                                  types)))))
4305            (if (memq (car marks) uncompressed) (cdr marks)
4306              (gnus-uncompress-range (cdr marks))))
4307
4308       (setq articles (symbol-value var))
4309
4310       ;; All articles have to be subsets of the active articles.
4311       (cond
4312        ;; Adjust "simple" lists.
4313        ((memq mark '(tick dormant expire reply save))
4314         (while articles
4315           (when (or (< (setq article (pop articles)) min) (> article max))
4316             (set var (delq article (symbol-value var))))))
4317        ;; Adjust assocs.
4318        ((memq mark uncompressed)
4319         (when (not (listp (cdr (symbol-value var))))
4320           (set var (list (symbol-value var))))
4321         (when (not (listp (cdr articles)))
4322           (setq articles (list articles)))
4323         (while articles
4324           (when (or (not (consp (setq article (pop articles))))
4325                     (< (car article) min)
4326                     (> (car article) max))
4327             (set var (delq article (symbol-value var))))))))))
4328
4329 (defun gnus-update-missing-marks (missing)
4330   "Go through the list of MISSING articles and remove them from the mark lists."
4331   (when missing
4332     (let ((types gnus-article-mark-lists)
4333           var m)
4334       ;; Go through all types.
4335       (while types
4336         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4337         (when (symbol-value var)
4338           ;; This list has articles.  So we delete all missing articles
4339           ;; from it.
4340           (setq m missing)
4341           (while m
4342             (set var (delq (pop m) (symbol-value var)))))))))
4343
4344 (defun gnus-update-marks ()
4345   "Enter the various lists of marked articles into the newsgroup info list."
4346   (let ((types gnus-article-mark-lists)
4347         (info (gnus-get-info gnus-newsgroup-name))
4348         (uncompressed '(score bookmark killed))
4349         type list newmarked symbol delta-marks)
4350     (when info
4351       ;; Add all marks lists to the list of marks lists.
4352       (while (setq type (pop types))
4353         (setq list (symbol-value
4354                           (setq symbol
4355                                 (intern (format "gnus-newsgroup-%s"
4356                                                 (car type))))))
4357
4358         (when list
4359           ;; Get rid of the entries of the articles that have the
4360           ;; default score.
4361           (when (and (eq (cdr type) 'score)
4362                      gnus-save-score
4363                      list)
4364             (let* ((arts list)
4365                    (prev (cons nil list))
4366                    (all prev))
4367               (while arts
4368                 (if (or (not (consp (car arts)))
4369                         (= (cdar arts) gnus-summary-default-score))
4370                     (setcdr prev (cdr arts))
4371                   (setq prev arts))
4372                 (setq arts (cdr arts)))
4373               (setq list (cdr all)))))
4374
4375        (or (memq (cdr type) uncompressed)
4376            (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4377        
4378        (when (gnus-check-backend-function 'request-set-mark
4379                                           gnus-newsgroup-name)
4380          ;; uncompressed:s are not proper flags (they are cons cells)
4381          ;; cache is a internal gnus flag
4382          (unless (memq (cdr type) (cons 'cache uncompressed))
4383            (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4384                   (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4385                   (add (gnus-remove-from-range (gnus-copy-sequence list) old)))
4386              (if add
4387                  (push (list add 'add (list (cdr type))) delta-marks))
4388              (if del
4389                  (push (list del 'del (list (cdr type))) delta-marks)))))
4390           
4391         (when list
4392          (push (cons (cdr type) list) newmarked)))
4393
4394       (when delta-marks
4395         (unless (gnus-check-group gnus-newsgroup-name)
4396           (error "Can't open server for %s" gnus-newsgroup-name))
4397         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4398           
4399       ;; Enter these new marks into the info of the group.
4400       (if (nthcdr 3 info)
4401           (setcar (nthcdr 3 info) newmarked)
4402         ;; Add the marks lists to the end of the info.
4403         (when newmarked
4404           (setcdr (nthcdr 2 info) (list newmarked))))
4405
4406       ;; Cut off the end of the info if there's nothing else there.
4407       (let ((i 5))
4408         (while (and (> i 2)
4409                     (not (nth i info)))
4410           (when (nthcdr (decf i) info)
4411             (setcdr (nthcdr i info) nil)))))))
4412
4413 (defun gnus-set-mode-line (where)
4414   "This function sets the mode line of the article or summary buffers.
4415 If WHERE is `summary', the summary mode line format will be used."
4416   ;; Is this mode line one we keep updated?
4417   (when (and (memq where gnus-updated-mode-lines)
4418              (symbol-value
4419               (intern (format "gnus-%s-mode-line-format-spec" where))))
4420     (let (mode-string)
4421       (save-excursion
4422         ;; We evaluate this in the summary buffer since these
4423         ;; variables are buffer-local to that buffer.
4424         (set-buffer gnus-summary-buffer)
4425         ;; We bind all these variables that are used in the `eval' form
4426         ;; below.
4427         (let* ((mformat (symbol-value
4428                          (intern
4429                           (format "gnus-%s-mode-line-format-spec" where))))
4430                (gnus-tmp-group-name gnus-newsgroup-name)
4431                (gnus-tmp-article-number (or gnus-current-article 0))
4432                (gnus-tmp-unread gnus-newsgroup-unreads)
4433                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4434                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4435                (gnus-tmp-unread-and-unselected
4436                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4437                             (zerop gnus-tmp-unselected))
4438                        "")
4439                       ((zerop gnus-tmp-unselected)
4440                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4441                       (t (format "{%d(+%d) more}"
4442                                  gnus-tmp-unread-and-unticked
4443                                  gnus-tmp-unselected))))
4444                (gnus-tmp-subject
4445                 (if (and gnus-current-headers
4446                          (vectorp gnus-current-headers))
4447                     (gnus-mode-string-quote
4448                      (mail-header-subject gnus-current-headers))
4449                   ""))
4450                bufname-length max-len
4451                gnus-tmp-header);; passed as argument to any user-format-funcs
4452           (setq mode-string (eval mformat))
4453           (setq bufname-length (if (string-match "%b" mode-string)
4454                                    (- (length
4455                                        (buffer-name
4456                                         (if (eq where 'summary)
4457                                             nil
4458                                           (get-buffer gnus-article-buffer))))
4459                                       2)
4460                                  0))
4461           (setq max-len (max 4 (if gnus-mode-non-string-length
4462                                    (- (window-width)
4463                                       gnus-mode-non-string-length
4464                                       bufname-length)
4465                                  (length mode-string))))
4466           ;; We might have to chop a bit of the string off...
4467           (when (> (length mode-string) max-len)
4468             (setq mode-string
4469                   (concat (truncate-string-to-width mode-string (- max-len 3))
4470                           "...")))
4471           ;; Pad the mode string a bit.
4472           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4473       ;; Update the mode line.
4474       (setq mode-line-buffer-identification
4475             (gnus-mode-line-buffer-identification (list mode-string)))
4476       (set-buffer-modified-p t))))
4477
4478 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4479   "Go through the HEADERS list and add all Xrefs to a hash table.
4480 The resulting hash table is returned, or nil if no Xrefs were found."
4481   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4482          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4483          (xref-hashtb (gnus-make-hashtable))
4484          start group entry number xrefs header)
4485     (while headers
4486       (setq header (pop headers))
4487       (when (and (setq xrefs (mail-header-xref header))
4488                  (not (memq (setq number (mail-header-number header))
4489                             unreads)))
4490         (setq start 0)
4491         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4492           (setq start (match-end 0))
4493           (setq group (if prefix
4494                           (concat prefix (substring xrefs (match-beginning 1)
4495                                                     (match-end 1)))
4496                         (substring xrefs (match-beginning 1) (match-end 1))))
4497           (setq number
4498                 (string-to-int (substring xrefs (match-beginning 2)
4499                                           (match-end 2))))
4500           (if (setq entry (gnus-gethash group xref-hashtb))
4501               (setcdr entry (cons number (cdr entry)))
4502             (gnus-sethash group (cons number nil) xref-hashtb)))))
4503     (and start xref-hashtb)))
4504
4505 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4506   "Look through all the headers and mark the Xrefs as read."
4507   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4508         name entry info xref-hashtb idlist method nth4)
4509     (save-excursion
4510       (set-buffer gnus-group-buffer)
4511       (when (setq xref-hashtb
4512                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4513         (mapatoms
4514          (lambda (group)
4515            (unless (string= from-newsgroup (setq name (symbol-name group)))
4516              (setq idlist (symbol-value group))
4517              ;; Dead groups are not updated.
4518              (and (prog1
4519                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4520                             info (nth 2 entry))
4521                     (when (stringp (setq nth4 (gnus-info-method info)))
4522                       (setq nth4 (gnus-server-to-method nth4))))
4523                   ;; Only do the xrefs if the group has the same
4524                   ;; select method as the group we have just read.
4525                   (or (gnus-methods-equal-p
4526                        nth4 (gnus-find-method-for-group from-newsgroup))
4527                       virtual
4528                       (equal nth4 (setq method (gnus-find-method-for-group
4529                                                 from-newsgroup)))
4530                       (and (equal (car nth4) (car method))
4531                            (equal (nth 1 nth4) (nth 1 method))))
4532                   gnus-use-cross-reference
4533                   (or (not (eq gnus-use-cross-reference t))
4534                       virtual
4535                       ;; Only do cross-references on subscribed
4536                       ;; groups, if that is what is wanted.
4537                       (<= (gnus-info-level info) gnus-level-subscribed))
4538                   (gnus-group-make-articles-read name idlist))))
4539          xref-hashtb)))))
4540
4541 (defun gnus-compute-read-articles (group articles)
4542   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4543          (info (nth 2 entry))
4544          (active (gnus-active group))
4545          ninfo)
4546     (when entry
4547       ;; First peel off all invalid article numbers.
4548       (when active
4549         (let ((ids articles)
4550               id first)
4551           (while (setq id (pop ids))
4552             (when (and first (> id (cdr active)))
4553               ;; We'll end up in this situation in one particular
4554               ;; obscure situation.  If you re-scan a group and get
4555               ;; a new article that is cross-posted to a different
4556               ;; group that has not been re-scanned, you might get
4557               ;; crossposted article that has a higher number than
4558               ;; Gnus believes possible.  So we re-activate this
4559               ;; group as well.  This might mean doing the
4560               ;; crossposting thingy will *increase* the number
4561               ;; of articles in some groups.  Tsk, tsk.
4562               (setq active (or (gnus-activate-group group) active)))
4563             (when (or (> id (cdr active))
4564                       (< id (car active)))
4565               (setq articles (delq id articles))))))
4566       ;; If the read list is nil, we init it.
4567       (if (and active
4568                (null (gnus-info-read info))
4569                (> (car active) 1))
4570           (setq ninfo (cons 1 (1- (car active))))
4571         (setq ninfo (gnus-info-read info)))
4572       ;; Then we add the read articles to the range.
4573       (gnus-add-to-range
4574        ninfo (setq articles (sort articles '<))))))
4575
4576 (defun gnus-group-make-articles-read (group articles)
4577   "Update the info of GROUP to say that ARTICLES are read."
4578   (let* ((num 0)
4579          (entry (gnus-gethash group gnus-newsrc-hashtb))
4580          (info (nth 2 entry))
4581          (active (gnus-active group))
4582          range)
4583     (when entry
4584       (setq range (gnus-compute-read-articles group articles))
4585       (save-excursion
4586         (set-buffer gnus-group-buffer)
4587         (gnus-undo-register
4588           `(progn
4589              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4590              (gnus-info-set-read ',info ',(gnus-info-read info))
4591              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4592              (gnus-group-update-group ,group t))))
4593       ;; Add the read articles to the range.
4594       (gnus-info-set-read info range)
4595       ;; Then we have to re-compute how many unread
4596       ;; articles there are in this group.
4597       (when active
4598         (cond
4599          ((not range)
4600           (setq num (- (1+ (cdr active)) (car active))))
4601          ((not (listp (cdr range)))
4602           (setq num (- (cdr active) (- (1+ (cdr range))
4603                                        (car range)))))
4604          (t
4605           (while range
4606             (if (numberp (car range))
4607                 (setq num (1+ num))
4608               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4609             (setq range (cdr range)))
4610           (setq num (- (cdr active) num))))
4611         ;; Update the number of unread articles.
4612         (setcar entry num)
4613         ;; Update the group buffer.
4614         (gnus-group-update-group group t)))))
4615
4616 (defvar gnus-newsgroup-none-id 0)
4617
4618 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4619   (let ((cur nntp-server-buffer)
4620         (dependencies
4621          (or dependencies
4622              (save-excursion (set-buffer gnus-summary-buffer)
4623                              gnus-newsgroup-dependencies)))
4624         headers id end ref
4625         (mail-parse-charset gnus-newsgroup-charset)
4626         (mail-parse-ignored-charsets 
4627              (save-excursion (condition-case nil
4628                                  (set-buffer gnus-summary-buffer)
4629                                (error))
4630                              gnus-newsgroup-ignored-charsets)))
4631     (save-excursion
4632       (set-buffer nntp-server-buffer)
4633       ;; Translate all TAB characters into SPACE characters.
4634       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4635       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4636       (gnus-run-hooks 'gnus-parse-headers-hook)
4637       (let ((case-fold-search t)
4638             in-reply-to header p lines chars)
4639         (goto-char (point-min))
4640         ;; Search to the beginning of the next header.  Error messages
4641         ;; do not begin with 2 or 3.
4642         (while (re-search-forward "^[23][0-9]+ " nil t)
4643           (setq id nil
4644                 ref nil)
4645           ;; This implementation of this function, with nine
4646           ;; search-forwards instead of the one re-search-forward and
4647           ;; a case (which basically was the old function) is actually
4648           ;; about twice as fast, even though it looks messier.  You
4649           ;; can't have everything, I guess.  Speed and elegance
4650           ;; doesn't always go hand in hand.
4651           (setq
4652            header
4653            (make-full-mail-header
4654             ;; Number.
4655             (prog1
4656                 (read cur)
4657               (end-of-line)
4658               (setq p (point))
4659               (narrow-to-region (point)
4660                                 (or (and (search-forward "\n.\n" nil t)
4661                                          (- (point) 2))
4662                                     (point))))
4663             ;; Subject.
4664             (progn
4665               (goto-char p)
4666               (if (search-forward "\nsubject: " nil t)
4667                   (nnheader-header-value)
4668                 "(none)"))
4669             ;; From.
4670             (progn
4671               (goto-char p)
4672               (if (search-forward "\nfrom: " nil t)
4673                   (nnheader-header-value)
4674                 "(nobody)"))
4675             ;; Date.
4676             (progn
4677               (goto-char p)
4678               (if (search-forward "\ndate: " nil t)
4679                   (nnheader-header-value) ""))
4680             ;; Message-ID.
4681             (progn
4682               (goto-char p)
4683               (setq id (if (re-search-forward
4684                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4685                            ;; We do it this way to make sure the Message-ID
4686                            ;; is (somewhat) syntactically valid.
4687                            (buffer-substring (match-beginning 1)
4688                                              (match-end 1))
4689                          ;; If there was no message-id, we just fake one
4690                          ;; to make subsequent routines simpler.
4691                          (nnheader-generate-fake-message-id))))
4692             ;; References.
4693             (progn
4694               (goto-char p)
4695               (if (search-forward "\nreferences: " nil t)
4696                   (progn
4697                     (setq end (point))
4698                     (prog1
4699                         (nnheader-header-value)
4700                       (setq ref
4701                             (buffer-substring
4702                              (progn
4703                                (end-of-line)
4704                                (search-backward ">" end t)
4705                                (1+ (point)))
4706                              (progn
4707                                (search-backward "<" end t)
4708                                (point))))))
4709                 ;; Get the references from the in-reply-to header if there
4710                 ;; were no references and the in-reply-to header looks
4711                 ;; promising.
4712                 (if (and (search-forward "\nin-reply-to: " nil t)
4713                          (setq in-reply-to (nnheader-header-value))
4714                          (string-match "<[^>]+>" in-reply-to))
4715                     (let (ref2)
4716                       (setq ref (substring in-reply-to (match-beginning 0)
4717                                            (match-end 0)))
4718                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4719                         (setq ref2 (substring in-reply-to (match-beginning 0)
4720                                               (match-end 0)))
4721                         (when (> (length ref2) (length ref))
4722                           (setq ref ref2)))
4723                       ref)
4724                   (setq ref nil))))
4725             ;; Chars.
4726             (progn
4727               (goto-char p)
4728               (if (search-forward "\nchars: " nil t)
4729                   (if (numberp (setq chars (ignore-errors (read cur))))
4730                       chars 0)
4731                 0))
4732             ;; Lines.
4733             (progn
4734               (goto-char p)
4735               (if (search-forward "\nlines: " nil t)
4736                   (if (numberp (setq lines (ignore-errors (read cur))))
4737                       lines 0)
4738                 0))
4739             ;; Xref.
4740             (progn
4741               (goto-char p)
4742               (and (search-forward "\nxref: " nil t)
4743                    (nnheader-header-value)))
4744             ;; Extra.
4745             (when gnus-extra-headers
4746               (let ((extra gnus-extra-headers)
4747                     out)
4748                 (while extra
4749                   (goto-char p)
4750                   (when (search-forward
4751                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4752                     (push (cons (car extra) (nnheader-header-value))
4753                           out))
4754                   (pop extra))
4755                 out))))
4756           (when (equal id ref)
4757             (setq ref nil))
4758
4759           (when gnus-alter-header-function
4760             (funcall gnus-alter-header-function header)
4761             (setq id (mail-header-id header)
4762                   ref (gnus-parent-id (mail-header-references header))))
4763
4764           (when (setq header
4765                       (gnus-dependencies-add-header
4766                        header dependencies force-new))
4767             (push header headers))
4768           (goto-char (point-max))
4769           (widen))
4770         (nreverse headers)))))
4771
4772 ;; Goes through the xover lines and returns a list of vectors
4773 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4774                                                   force-new dependencies
4775                                                   group also-fetch-heads)
4776   "Parse the news overview data in the server buffer, and return a
4777 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4778   ;; Get the Xref when the users reads the articles since most/some
4779   ;; NNTP servers do not include Xrefs when using XOVER.
4780   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4781   (let ((mail-parse-charset gnus-newsgroup-charset)
4782         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4783         (cur nntp-server-buffer)
4784         (dependencies (or dependencies gnus-newsgroup-dependencies))
4785         number headers header)
4786     (save-excursion
4787       (set-buffer nntp-server-buffer)
4788       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4789       ;; Allow the user to mangle the headers before parsing them.
4790       (gnus-run-hooks 'gnus-parse-headers-hook)
4791       (goto-char (point-min))
4792       (while (not (eobp))
4793         (condition-case ()
4794             (while (and sequence (not (eobp)))
4795               (setq number (read cur))
4796               (while (and sequence
4797                           (< (car sequence) number))
4798                 (setq sequence (cdr sequence)))
4799               (and sequence
4800                    (eq number (car sequence))
4801                    (progn
4802                      (setq sequence (cdr sequence))
4803                      (setq header (inline
4804                                     (gnus-nov-parse-line
4805                                      number dependencies force-new))))
4806                    (push header headers))
4807               (forward-line 1))
4808           (error
4809            (gnus-error 4 "Strange nov line (%d)"
4810                        (count-lines (point-min) (point)))))
4811         (forward-line 1))
4812       ;; A common bug in inn is that if you have posted an article and
4813       ;; then retrieves the active file, it will answer correctly --
4814       ;; the new article is included.  However, a NOV entry for the
4815       ;; article may not have been generated yet, so this may fail.
4816       ;; We work around this problem by retrieving the last few
4817       ;; headers using HEAD.
4818       (if (or (not also-fetch-heads)
4819               (not sequence))
4820           ;; We (probably) got all the headers.
4821           (nreverse headers)
4822         (let ((gnus-nov-is-evil t))
4823           (nconc
4824            (nreverse headers)
4825            (when (gnus-retrieve-headers sequence group)
4826              (gnus-get-newsgroup-headers))))))))
4827
4828 (defun gnus-article-get-xrefs ()
4829   "Fill in the Xref value in `gnus-current-headers', if necessary.
4830 This is meant to be called in `gnus-article-internal-prepare-hook'."
4831   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4832                                  gnus-current-headers)))
4833     (or (not gnus-use-cross-reference)
4834         (not headers)
4835         (and (mail-header-xref headers)
4836              (not (string= (mail-header-xref headers) "")))
4837         (let ((case-fold-search t)
4838               xref)
4839           (save-restriction
4840             (nnheader-narrow-to-headers)
4841             (goto-char (point-min))
4842             (when (or (and (not (eobp))
4843                            (eq (downcase (char-after)) ?x)
4844                            (looking-at "Xref:"))
4845                       (search-forward "\nXref:" nil t))
4846               (goto-char (1+ (match-end 0)))
4847               (setq xref (buffer-substring (point)
4848                                            (progn (end-of-line) (point))))
4849               (mail-header-set-xref headers xref)))))))
4850
4851 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4852   "Find article ID and insert the summary line for that article.
4853 OLD-HEADER can either be a header or a line number to insert
4854 the subject line on."
4855   (let* ((line (and (numberp old-header) old-header))
4856          (old-header (and (vectorp old-header) old-header))
4857          (header (cond ((and old-header use-old-header)
4858                         old-header)
4859                        ((and (numberp id)
4860                              (gnus-number-to-header id))
4861                         (gnus-number-to-header id))
4862                        (t
4863                         (gnus-read-header id))))
4864          (number (and (numberp id) id))
4865          d)
4866     (when header
4867       ;; Rebuild the thread that this article is part of and go to the
4868       ;; article we have fetched.
4869       (when (and (not gnus-show-threads)
4870                  old-header)
4871         (when (and number
4872                    (setq d (gnus-data-find (mail-header-number old-header))))
4873           (goto-char (gnus-data-pos d))
4874           (gnus-data-remove
4875            number
4876            (- (gnus-point-at-bol)
4877               (prog1
4878                   (1+ (gnus-point-at-eol))
4879                 (gnus-delete-line))))))
4880       (when old-header
4881         (mail-header-set-number header (mail-header-number old-header)))
4882       (setq gnus-newsgroup-sparse
4883             (delq (setq number (mail-header-number header))
4884                   gnus-newsgroup-sparse))
4885       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4886       (push number gnus-newsgroup-limit)
4887       (gnus-rebuild-thread (mail-header-id header) line)
4888       (gnus-summary-goto-subject number nil t))
4889     (when (and (numberp number)
4890                (> number 0))
4891       ;; We have to update the boundaries even if we can't fetch the
4892       ;; article if ID is a number -- so that the next `P' or `N'
4893       ;; command will fetch the previous (or next) article even
4894       ;; if the one we tried to fetch this time has been canceled.
4895       (when (> number gnus-newsgroup-end)
4896         (setq gnus-newsgroup-end number))
4897       (when (< number gnus-newsgroup-begin)
4898         (setq gnus-newsgroup-begin number))
4899       (setq gnus-newsgroup-unselected
4900             (delq number gnus-newsgroup-unselected)))
4901     ;; Report back a success?
4902     (and header (mail-header-number header))))
4903
4904 ;;; Process/prefix in the summary buffer
4905
4906 (defun gnus-summary-work-articles (n)
4907   "Return a list of articles to be worked upon.
4908 The prefix argument, the list of process marked articles, and the
4909 current article will be taken into consideration."
4910   (save-excursion
4911     (set-buffer gnus-summary-buffer)
4912     (cond
4913      (n
4914       ;; A numerical prefix has been given.
4915       (setq n (prefix-numeric-value n))
4916       (let ((backward (< n 0))
4917             (n (abs (prefix-numeric-value n)))
4918             articles article)
4919         (save-excursion
4920           (while
4921               (and (> n 0)
4922                    (push (setq article (gnus-summary-article-number))
4923                          articles)
4924                    (if backward
4925                        (gnus-summary-find-prev nil article)
4926                      (gnus-summary-find-next nil article)))
4927             (decf n)))
4928         (nreverse articles)))
4929      ((and (gnus-region-active-p) (mark))
4930       (message "region active")
4931       ;; Work on the region between point and mark.
4932       (let ((max (max (point) (mark)))
4933             articles article)
4934         (save-excursion
4935           (goto-char (min (min (point) (mark))))
4936           (while
4937               (and
4938                (push (setq article (gnus-summary-article-number)) articles)
4939                (gnus-summary-find-next nil article)
4940                (< (point) max)))
4941           (nreverse articles))))
4942      (gnus-newsgroup-processable
4943       ;; There are process-marked articles present.
4944       ;; Save current state.
4945       (gnus-summary-save-process-mark)
4946       ;; Return the list.
4947       (reverse gnus-newsgroup-processable))
4948      (t
4949       ;; Just return the current article.
4950       (list (gnus-summary-article-number))))))
4951
4952 (defmacro gnus-summary-iterate (arg &rest forms)
4953   "Iterate over the process/prefixed articles and do FORMS.
4954 ARG is the interactive prefix given to the command.  FORMS will be
4955 executed with point over the summary line of the articles."
4956   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4957     `(let ((,articles (gnus-summary-work-articles ,arg)))
4958        (while ,articles
4959          (gnus-summary-goto-subject (car ,articles))
4960          ,@forms
4961          (pop ,articles)))))
4962
4963 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4964 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4965
4966 (defun gnus-summary-save-process-mark ()
4967   "Push the current set of process marked articles on the stack."
4968   (interactive)
4969   (push (copy-sequence gnus-newsgroup-processable)
4970         gnus-newsgroup-process-stack))
4971
4972 (defun gnus-summary-kill-process-mark ()
4973   "Push the current set of process marked articles on the stack and unmark."
4974   (interactive)
4975   (gnus-summary-save-process-mark)
4976   (gnus-summary-unmark-all-processable))
4977
4978 (defun gnus-summary-yank-process-mark ()
4979   "Pop the last process mark state off the stack and restore it."
4980   (interactive)
4981   (unless gnus-newsgroup-process-stack
4982     (error "Empty mark stack"))
4983   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4984
4985 (defun gnus-summary-process-mark-set (set)
4986   "Make SET into the current process marked articles."
4987   (gnus-summary-unmark-all-processable)
4988   (while set
4989     (gnus-summary-set-process-mark (pop set))))
4990
4991 ;;; Searching and stuff
4992
4993 (defun gnus-summary-search-group (&optional backward use-level)
4994   "Search for next unread newsgroup.
4995 If optional argument BACKWARD is non-nil, search backward instead."
4996   (save-excursion
4997     (set-buffer gnus-group-buffer)
4998     (when (gnus-group-search-forward
4999            backward nil (if use-level (gnus-group-group-level) nil))
5000       (gnus-group-group-name))))
5001
5002 (defun gnus-summary-best-group (&optional exclude-group)
5003   "Find the name of the best unread group.
5004 If EXCLUDE-GROUP, do not go to this group."
5005   (save-excursion
5006     (set-buffer gnus-group-buffer)
5007     (save-excursion
5008       (gnus-group-best-unread-group exclude-group))))
5009
5010 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5011   (if backward (gnus-summary-find-prev)
5012     (let* ((dummy (gnus-summary-article-intangible-p))
5013            (article (or article (gnus-summary-article-number)))
5014            (arts (gnus-data-find-list article))
5015            result)
5016       (when (and (not dummy)
5017                  (or (not gnus-summary-check-current)
5018                      (not unread)
5019                      (not (gnus-data-unread-p (car arts)))))
5020         (setq arts (cdr arts)))
5021       (when (setq result
5022                   (if unread
5023                       (progn
5024                         (while arts
5025                           (when (or (and undownloaded
5026                                          (eq gnus-undownloaded-mark
5027                                              (gnus-data-mark (car arts))))
5028                                     (gnus-data-unread-p (car arts)))
5029                             (setq result (car arts)
5030                                   arts nil))
5031                           (setq arts (cdr arts)))
5032                         result)
5033                     (car arts)))
5034         (goto-char (gnus-data-pos result))
5035         (gnus-data-number result)))))
5036
5037 (defun gnus-summary-find-prev (&optional unread article)
5038   (let* ((eobp (eobp))
5039          (article (or article (gnus-summary-article-number)))
5040          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5041          result)
5042     (when (and (not eobp)
5043                (or (not gnus-summary-check-current)
5044                    (not unread)
5045                    (not (gnus-data-unread-p (car arts)))))
5046       (setq arts (cdr arts)))
5047     (when (setq result
5048                 (if unread
5049                     (progn
5050                       (while arts
5051                         (when (gnus-data-unread-p (car arts))
5052                           (setq result (car arts)
5053                                 arts nil))
5054                         (setq arts (cdr arts)))
5055                       result)
5056                   (car arts)))
5057       (goto-char (gnus-data-pos result))
5058       (gnus-data-number result))))
5059
5060 (defun gnus-summary-find-subject (subject &optional unread backward article)
5061   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5062          (article (or article (gnus-summary-article-number)))
5063          (articles (gnus-data-list backward))
5064          (arts (gnus-data-find-list article articles))
5065          result)
5066     (when (or (not gnus-summary-check-current)
5067               (not unread)
5068               (not (gnus-data-unread-p (car arts))))
5069       (setq arts (cdr arts)))
5070     (while arts
5071       (and (or (not unread)
5072                (gnus-data-unread-p (car arts)))
5073            (vectorp (gnus-data-header (car arts)))
5074            (gnus-subject-equal
5075             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5076            (setq result (car arts)
5077                  arts nil))
5078       (setq arts (cdr arts)))
5079     (and result
5080          (goto-char (gnus-data-pos result))
5081          (gnus-data-number result))))
5082
5083 (defun gnus-summary-search-forward (&optional unread subject backward)
5084   "Search forward for an article.
5085 If UNREAD, look for unread articles.  If SUBJECT, look for
5086 articles with that subject.  If BACKWARD, search backward instead."
5087   (cond (subject (gnus-summary-find-subject subject unread backward))
5088         (backward (gnus-summary-find-prev unread))
5089         (t (gnus-summary-find-next unread))))
5090
5091 (defun gnus-recenter (&optional n)
5092   "Center point in window and redisplay frame.
5093 Also do horizontal recentering."
5094   (interactive "P")
5095   (when (and gnus-auto-center-summary
5096              (not (eq gnus-auto-center-summary 'vertical)))
5097     (gnus-horizontal-recenter))
5098   (recenter n))
5099
5100 (defun gnus-summary-recenter ()
5101   "Center point in the summary window.
5102 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5103 displayed, no centering will be performed."
5104   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5105   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5106   (let* ((top (cond ((< (window-height) 4) 0)
5107                     ((< (window-height) 7) 1)
5108                     (t (if (numberp gnus-auto-center-summary)
5109                            gnus-auto-center-summary
5110                          2))))
5111          (height (1- (window-height)))
5112          (bottom (save-excursion (goto-char (point-max))
5113                                  (forward-line (- height))
5114                                  (point)))
5115          (window (get-buffer-window (current-buffer))))
5116     ;; The user has to want it.
5117     (when gnus-auto-center-summary
5118       (when (get-buffer-window gnus-article-buffer)
5119         ;; Only do recentering when the article buffer is displayed,
5120         ;; Set the window start to either `bottom', which is the biggest
5121         ;; possible valid number, or the second line from the top,
5122         ;; whichever is the least.
5123         (set-window-start
5124          window (min bottom (save-excursion
5125                               (forward-line (- top)) (point)))
5126          t))
5127       ;; Do horizontal recentering while we're at it.
5128       (when (and (get-buffer-window (current-buffer) t)
5129                  (not (eq gnus-auto-center-summary 'vertical)))
5130         (let ((selected (selected-window)))
5131           (select-window (get-buffer-window (current-buffer) t))
5132           (gnus-summary-position-point)
5133           (gnus-horizontal-recenter)
5134           (select-window selected))))))
5135
5136 (defun gnus-summary-jump-to-group (newsgroup)
5137   "Move point to NEWSGROUP in group mode buffer."
5138   ;; Keep update point of group mode buffer if visible.
5139   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5140       (save-window-excursion
5141         ;; Take care of tree window mode.
5142         (when (get-buffer-window gnus-group-buffer)
5143           (pop-to-buffer gnus-group-buffer))
5144         (gnus-group-jump-to-group newsgroup))
5145     (save-excursion
5146       ;; Take care of tree window mode.
5147       (if (get-buffer-window gnus-group-buffer)
5148           (pop-to-buffer gnus-group-buffer)
5149         (set-buffer gnus-group-buffer))
5150       (gnus-group-jump-to-group newsgroup))))
5151
5152 ;; This function returns a list of article numbers based on the
5153 ;; difference between the ranges of read articles in this group and
5154 ;; the range of active articles.
5155 (defun gnus-list-of-unread-articles (group)
5156   (let* ((read (gnus-info-read (gnus-get-info group)))
5157          (active (or (gnus-active group) (gnus-activate-group group)))
5158          (last (cdr active))
5159          first nlast unread)
5160     ;; If none are read, then all are unread.
5161     (if (not read)
5162         (setq first (car active))
5163       ;; If the range of read articles is a single range, then the
5164       ;; first unread article is the article after the last read
5165       ;; article.  Sounds logical, doesn't it?
5166       (if (not (listp (cdr read)))
5167           (setq first (max (car active) (1+ (cdr read))))
5168         ;; `read' is a list of ranges.
5169         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5170                                   (caar read)))
5171                   1)
5172           (setq first (car active)))
5173         (while read
5174           (when first
5175             (while (< first nlast)
5176               (push first unread)
5177               (setq first (1+ first))))
5178           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5179           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5180           (setq read (cdr read)))))
5181     ;; And add the last unread articles.
5182     (while (<= first last)
5183       (push first unread)
5184       (setq first (1+ first)))
5185     ;; Return the list of unread articles.
5186     (delq 0 (nreverse unread))))
5187
5188 (defun gnus-list-of-read-articles (group)
5189   "Return a list of unread, unticked and non-dormant articles."
5190   (let* ((info (gnus-get-info group))
5191          (marked (gnus-info-marks info))
5192          (active (gnus-active group)))
5193     (and info active
5194          (gnus-set-difference
5195           (gnus-sorted-complement
5196            (gnus-uncompress-range active)
5197            (gnus-list-of-unread-articles group))
5198           (append
5199            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5200            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5201
5202 ;; Various summary commands
5203
5204 (defun gnus-summary-select-article-buffer ()
5205   "Reconfigure windows to show article buffer."
5206   (interactive)
5207   (if (not (gnus-buffer-live-p gnus-article-buffer))
5208       (error "There is no article buffer for this summary buffer")
5209     (gnus-configure-windows 'article)
5210     (select-window (get-buffer-window gnus-article-buffer))))
5211
5212 (defun gnus-summary-universal-argument (arg)
5213   "Perform any operation on all articles that are process/prefixed."
5214   (interactive "P")
5215   (let ((articles (gnus-summary-work-articles arg))
5216         func article)
5217     (if (eq
5218          (setq
5219           func
5220           (key-binding
5221            (read-key-sequence
5222             (substitute-command-keys
5223              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5224              ))))
5225          'undefined)
5226         (gnus-error 1 "Undefined key")
5227       (save-excursion
5228         (while articles
5229           (gnus-summary-goto-subject (setq article (pop articles)))
5230           (let (gnus-newsgroup-processable)
5231             (command-execute func))
5232           (gnus-summary-remove-process-mark article)))))
5233   (gnus-summary-position-point))
5234
5235 (defun gnus-summary-toggle-truncation (&optional arg)
5236   "Toggle truncation of summary lines.
5237 With arg, turn line truncation on iff arg is positive."
5238   (interactive "P")
5239   (setq truncate-lines
5240         (if (null arg) (not truncate-lines)
5241           (> (prefix-numeric-value arg) 0)))
5242   (redraw-display))
5243
5244 (defun gnus-summary-reselect-current-group (&optional all rescan)
5245   "Exit and then reselect the current newsgroup.
5246 The prefix argument ALL means to select all articles."
5247   (interactive "P")
5248   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5249     (error "Ephemeral groups can't be reselected"))
5250   (let ((current-subject (gnus-summary-article-number))
5251         (group gnus-newsgroup-name))
5252     (setq gnus-newsgroup-begin nil)
5253     (gnus-summary-exit)
5254     ;; We have to adjust the point of group mode buffer because
5255     ;; point was moved to the next unread newsgroup by exiting.
5256     (gnus-summary-jump-to-group group)
5257     (when rescan
5258       (save-excursion
5259         (gnus-group-get-new-news-this-group 1)))
5260     (gnus-group-read-group all t)
5261     (gnus-summary-goto-subject current-subject nil t)))
5262
5263 (defun gnus-summary-rescan-group (&optional all)
5264   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5265   (interactive "P")
5266   (gnus-summary-reselect-current-group all t))
5267
5268 (defun gnus-summary-update-info (&optional non-destructive)
5269   (save-excursion
5270     (let ((group gnus-newsgroup-name))
5271       (when group
5272         (when gnus-newsgroup-kill-headers
5273           (setq gnus-newsgroup-killed
5274                 (gnus-compress-sequence
5275                  (nconc
5276                   (gnus-set-sorted-intersection
5277                    (gnus-uncompress-range gnus-newsgroup-killed)
5278                    (setq gnus-newsgroup-unselected
5279                          (sort gnus-newsgroup-unselected '<)))
5280                   (setq gnus-newsgroup-unreads
5281                         (sort gnus-newsgroup-unreads '<)))
5282                  t)))
5283         (unless (listp (cdr gnus-newsgroup-killed))
5284           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5285         (let ((headers gnus-newsgroup-headers))
5286           ;; Set the new ranges of read articles.
5287           (save-excursion
5288             (set-buffer gnus-group-buffer)
5289             (gnus-undo-force-boundary))
5290           (gnus-update-read-articles
5291            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5292           ;; Set the current article marks.
5293           (let ((gnus-newsgroup-scored
5294                  (if (and (not gnus-save-score)
5295                           (not non-destructive))
5296                      nil
5297                    gnus-newsgroup-scored)))
5298             (save-excursion
5299               (gnus-update-marks)))
5300           ;; Do the cross-ref thing.
5301           (when gnus-use-cross-reference
5302             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5303           ;; Do not switch windows but change the buffer to work.
5304           (set-buffer gnus-group-buffer)
5305           (unless (gnus-ephemeral-group-p group)
5306             (gnus-group-update-group group)))))))
5307
5308 (defun gnus-summary-save-newsrc (&optional force)
5309   "Save the current number of read/marked articles in the dribble buffer.
5310 The dribble buffer will then be saved.
5311 If FORCE (the prefix), also save the .newsrc file(s)."
5312   (interactive "P")
5313   (gnus-summary-update-info t)
5314   (if force
5315       (gnus-save-newsrc-file)
5316     (gnus-dribble-save)))
5317
5318 (defun gnus-summary-exit (&optional temporary)
5319   "Exit reading current newsgroup, and then return to group selection mode.
5320 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5321   (interactive)
5322   (gnus-set-global-variables)
5323   (when (gnus-buffer-live-p gnus-article-buffer)
5324     (save-excursion
5325       (set-buffer gnus-article-buffer)
5326       (mm-destroy-parts gnus-article-mime-handles)))
5327   (gnus-kill-save-kill-buffer)
5328   (gnus-async-halt-prefetch)
5329   (let* ((group gnus-newsgroup-name)
5330          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5331          (mode major-mode)
5332          (group-point nil)
5333          (buf (current-buffer)))
5334     (unless quit-config
5335       ;; Do adaptive scoring, and possibly save score files.
5336       (when gnus-newsgroup-adaptive
5337         (gnus-score-adaptive))
5338       (when gnus-use-scoring
5339         (gnus-score-save)))
5340     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5341     ;; If we have several article buffers, we kill them at exit.
5342     (unless gnus-single-article-buffer
5343       (gnus-kill-buffer gnus-original-article-buffer)
5344       (setq gnus-article-current nil))
5345     (when gnus-use-cache
5346       (gnus-cache-possibly-remove-articles)
5347       (gnus-cache-save-buffers))
5348     (gnus-async-prefetch-remove-group group)
5349     (when gnus-suppress-duplicates
5350       (gnus-dup-enter-articles))
5351     (when gnus-use-trees
5352       (gnus-tree-close group))
5353     ;; Remove entries for this group.
5354     (nnmail-purge-split-history (gnus-group-real-name group))
5355     ;; Make all changes in this group permanent.
5356     (unless quit-config
5357       (gnus-run-hooks 'gnus-exit-group-hook)
5358       (gnus-summary-update-info))
5359     (gnus-close-group group)
5360     ;; Make sure where we were, and go to next newsgroup.
5361     (set-buffer gnus-group-buffer)
5362     (unless quit-config
5363       (gnus-group-jump-to-group group))
5364     (gnus-run-hooks 'gnus-summary-exit-hook)
5365     (unless (or quit-config
5366                 ;; If this group has disappeared from the summary
5367                 ;; buffer, don't skip forwards.
5368                 (not (string= group (gnus-group-group-name))))
5369       (gnus-group-next-unread-group 1))
5370     (setq group-point (point))
5371     (if temporary
5372         nil                             ;Nothing to do.
5373       ;; If we have several article buffers, we kill them at exit.
5374       (unless gnus-single-article-buffer
5375         (gnus-kill-buffer gnus-article-buffer)
5376         (gnus-kill-buffer gnus-original-article-buffer)
5377         (setq gnus-article-current nil))
5378       (set-buffer buf)
5379       (if (not gnus-kill-summary-on-exit)
5380           (gnus-deaden-summary)
5381         ;; We set all buffer-local variables to nil.  It is unclear why
5382         ;; this is needed, but if we don't, buffer-local variables are
5383         ;; not garbage-collected, it seems.  This would the lead to en
5384         ;; ever-growing Emacs.
5385         (gnus-summary-clear-local-variables)
5386         (when (get-buffer gnus-article-buffer)
5387           (bury-buffer gnus-article-buffer))
5388         ;; We clear the global counterparts of the buffer-local
5389         ;; variables as well, just to be on the safe side.
5390         (set-buffer gnus-group-buffer)
5391         (gnus-summary-clear-local-variables)
5392         ;; Return to group mode buffer.
5393         (when (eq mode 'gnus-summary-mode)
5394           (gnus-kill-buffer buf)))
5395       (setq gnus-current-select-method gnus-select-method)
5396       (pop-to-buffer gnus-group-buffer)
5397       (if (not quit-config)
5398           (progn
5399             (goto-char group-point)
5400             (gnus-configure-windows 'group 'force))
5401         (gnus-handle-ephemeral-exit quit-config))
5402       ;; Clear the current group name.
5403       (unless quit-config
5404         (setq gnus-newsgroup-name nil)))))
5405
5406 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5407 (defun gnus-summary-exit-no-update (&optional no-questions)
5408   "Quit reading current newsgroup without updating read article info."
5409   (interactive)
5410   (let* ((group gnus-newsgroup-name)
5411          (quit-config (gnus-group-quit-config group)))
5412     (when (or no-questions
5413               gnus-expert-user
5414               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5415       (gnus-async-halt-prefetch)
5416       (mapcar 'funcall
5417               (delq 'gnus-summary-expire-articles
5418                     (copy-list gnus-summary-prepare-exit-hook)))
5419       (when (gnus-buffer-live-p gnus-article-buffer)
5420         (save-excursion
5421           (set-buffer gnus-article-buffer)
5422           (mm-destroy-parts gnus-article-mime-handles)))
5423       ;; If we have several article buffers, we kill them at exit.
5424       (unless gnus-single-article-buffer
5425         (gnus-kill-buffer gnus-article-buffer)
5426         (gnus-kill-buffer gnus-original-article-buffer)
5427         (setq gnus-article-current nil))
5428       (if (not gnus-kill-summary-on-exit)
5429           (gnus-deaden-summary)
5430         (gnus-close-group group)
5431         (gnus-summary-clear-local-variables)
5432         (set-buffer gnus-group-buffer)
5433         (gnus-summary-clear-local-variables)
5434         (when (get-buffer gnus-summary-buffer)
5435           (kill-buffer gnus-summary-buffer)))
5436       (unless gnus-single-article-buffer
5437         (setq gnus-article-current nil))
5438       (when gnus-use-trees
5439         (gnus-tree-close group))
5440       (gnus-async-prefetch-remove-group group)
5441       (when (get-buffer gnus-article-buffer)
5442         (bury-buffer gnus-article-buffer))
5443       ;; Return to the group buffer.
5444       (gnus-configure-windows 'group 'force)
5445       ;; Clear the current group name.
5446       (setq gnus-newsgroup-name nil)
5447       (when (equal (gnus-group-group-name) group)
5448         (gnus-group-next-unread-group 1))
5449       (when quit-config
5450         (gnus-handle-ephemeral-exit quit-config)))))
5451
5452 (defun gnus-handle-ephemeral-exit (quit-config)
5453   "Handle movement when leaving an ephemeral group.
5454 The state which existed when entering the ephemeral is reset."
5455   (if (not (buffer-name (car quit-config)))
5456       (gnus-configure-windows 'group 'force)
5457     (set-buffer (car quit-config))
5458     (cond ((eq major-mode 'gnus-summary-mode)
5459            (gnus-set-global-variables))
5460           ((eq major-mode 'gnus-article-mode)
5461            (save-excursion
5462              ;; The `gnus-summary-buffer' variable may point
5463              ;; to the old summary buffer when using a single
5464              ;; article buffer.
5465              (unless (gnus-buffer-live-p gnus-summary-buffer)
5466                (set-buffer gnus-group-buffer))
5467              (set-buffer gnus-summary-buffer)
5468              (gnus-set-global-variables))))
5469     (if (or (eq (cdr quit-config) 'article)
5470             (eq (cdr quit-config) 'pick))
5471         (progn
5472           ;; The current article may be from the ephemeral group
5473           ;; thus it is best that we reload this article
5474           (gnus-summary-show-article)
5475           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5476               (gnus-configure-windows 'pick 'force)
5477             (gnus-configure-windows (cdr quit-config) 'force)))
5478       (gnus-configure-windows (cdr quit-config) 'force))
5479     (when (eq major-mode 'gnus-summary-mode)
5480       (gnus-summary-next-subject 1 nil t)
5481       (gnus-summary-recenter)
5482       (gnus-summary-position-point))))
5483
5484 ;;; Dead summaries.
5485
5486 (defvar gnus-dead-summary-mode-map nil)
5487
5488 (unless gnus-dead-summary-mode-map
5489   (setq gnus-dead-summary-mode-map (make-keymap))
5490   (suppress-keymap gnus-dead-summary-mode-map)
5491   (substitute-key-definition
5492    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5493   (let ((keys '("\C-d" "\r" "\177" [delete])))
5494     (while keys
5495       (define-key gnus-dead-summary-mode-map
5496         (pop keys) 'gnus-summary-wake-up-the-dead))))
5497
5498 (defvar gnus-dead-summary-mode nil
5499   "Minor mode for Gnus summary buffers.")
5500
5501 (defun gnus-dead-summary-mode (&optional arg)
5502   "Minor mode for Gnus summary buffers."
5503   (interactive "P")
5504   (when (eq major-mode 'gnus-summary-mode)
5505     (make-local-variable 'gnus-dead-summary-mode)
5506     (setq gnus-dead-summary-mode
5507           (if (null arg) (not gnus-dead-summary-mode)
5508             (> (prefix-numeric-value arg) 0)))
5509     (when gnus-dead-summary-mode
5510       (gnus-add-minor-mode
5511        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5512
5513 (defun gnus-deaden-summary ()
5514   "Make the current summary buffer into a dead summary buffer."
5515   ;; Kill any previous dead summary buffer.
5516   (when (and gnus-dead-summary
5517              (buffer-name gnus-dead-summary))
5518     (save-excursion
5519       (set-buffer gnus-dead-summary)
5520       (when gnus-dead-summary-mode
5521         (kill-buffer (current-buffer)))))
5522   ;; Make this the current dead summary.
5523   (setq gnus-dead-summary (current-buffer))
5524   (gnus-dead-summary-mode 1)
5525   (let ((name (buffer-name)))
5526     (when (string-match "Summary" name)
5527       (rename-buffer
5528        (concat (substring name 0 (match-beginning 0)) "Dead "
5529                (substring name (match-beginning 0)))
5530        t))))
5531
5532 (defun gnus-kill-or-deaden-summary (buffer)
5533   "Kill or deaden the summary BUFFER."
5534   (save-excursion
5535     (when (and (buffer-name buffer)
5536                (not gnus-single-article-buffer))
5537       (save-excursion
5538         (set-buffer buffer)
5539         (gnus-kill-buffer gnus-article-buffer)
5540         (gnus-kill-buffer gnus-original-article-buffer)))
5541     (cond (gnus-kill-summary-on-exit
5542            (when (and gnus-use-trees
5543                       (gnus-buffer-exists-p buffer))
5544              (save-excursion
5545                (set-buffer buffer)
5546                (gnus-tree-close gnus-newsgroup-name)))
5547            (gnus-kill-buffer buffer))
5548           ((gnus-buffer-exists-p buffer)
5549            (save-excursion
5550              (set-buffer buffer)
5551              (gnus-deaden-summary))))))
5552
5553 (defun gnus-summary-wake-up-the-dead (&rest args)
5554   "Wake up the dead summary buffer."
5555   (interactive)
5556   (gnus-dead-summary-mode -1)
5557   (let ((name (buffer-name)))
5558     (when (string-match "Dead " name)
5559       (rename-buffer
5560        (concat (substring name 0 (match-beginning 0))
5561                (substring name (match-end 0)))
5562        t)))
5563   (gnus-message 3 "This dead summary is now alive again"))
5564
5565 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5566 (defun gnus-summary-fetch-faq (&optional faq-dir)
5567   "Fetch the FAQ for the current group.
5568 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5569 in."
5570   (interactive
5571    (list
5572     (when current-prefix-arg
5573       (completing-read
5574        "Faq dir: " (and (listp gnus-group-faq-directory)
5575                         (mapcar (lambda (file) (list file))
5576                                 gnus-group-faq-directory))))))
5577   (let (gnus-faq-buffer)
5578     (when (setq gnus-faq-buffer
5579                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5580       (gnus-configure-windows 'summary-faq))))
5581
5582 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5583 (defun gnus-summary-describe-group (&optional force)
5584   "Describe the current newsgroup."
5585   (interactive "P")
5586   (gnus-group-describe-group force gnus-newsgroup-name))
5587
5588 (defun gnus-summary-describe-briefly ()
5589   "Describe summary mode commands briefly."
5590   (interactive)
5591   (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")))
5592
5593 ;; Walking around group mode buffer from summary mode.
5594
5595 (defun gnus-summary-next-group (&optional no-article target-group backward)
5596   "Exit current newsgroup and then select next unread newsgroup.
5597 If prefix argument NO-ARTICLE is non-nil, no article is selected
5598 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5599 previous group instead."
5600   (interactive "P")
5601   ;; Stop pre-fetching.
5602   (gnus-async-halt-prefetch)
5603   (let ((current-group gnus-newsgroup-name)
5604         (current-buffer (current-buffer))
5605         entered)
5606     ;; First we semi-exit this group to update Xrefs and all variables.
5607     ;; We can't do a real exit, because the window conf must remain
5608     ;; the same in case the user is prompted for info, and we don't
5609     ;; want the window conf to change before that...
5610     (gnus-summary-exit t)
5611     (while (not entered)
5612       ;; Then we find what group we are supposed to enter.
5613       (set-buffer gnus-group-buffer)
5614       (gnus-group-jump-to-group current-group)
5615       (setq target-group
5616             (or target-group
5617                 (if (eq gnus-keep-same-level 'best)
5618                     (gnus-summary-best-group gnus-newsgroup-name)
5619                   (gnus-summary-search-group backward gnus-keep-same-level))))
5620       (if (not target-group)
5621           ;; There are no further groups, so we return to the group
5622           ;; buffer.
5623           (progn
5624             (gnus-message 5 "Returning to the group buffer")
5625             (setq entered t)
5626             (when (gnus-buffer-live-p current-buffer)
5627               (set-buffer current-buffer)
5628               (gnus-summary-exit))
5629             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5630         ;; We try to enter the target group.
5631         (gnus-group-jump-to-group target-group)
5632         (let ((unreads (gnus-group-group-unread)))
5633           (if (and (or (eq t unreads)
5634                        (and unreads (not (zerop unreads))))
5635                    (gnus-summary-read-group
5636                     target-group nil no-article
5637                     (and (buffer-name current-buffer) current-buffer)
5638                     nil backward))
5639               (setq entered t)
5640             (setq current-group target-group
5641                   target-group nil)))))))
5642
5643 (defun gnus-summary-prev-group (&optional no-article)
5644   "Exit current newsgroup and then select previous unread newsgroup.
5645 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5646   (interactive "P")
5647   (gnus-summary-next-group no-article nil t))
5648
5649 ;; Walking around summary lines.
5650
5651 (defun gnus-summary-first-subject (&optional unread undownloaded)
5652   "Go to the first unread subject.
5653 If UNREAD is non-nil, go to the first unread article.
5654 Returns the article selected or nil if there are no unread articles."
5655   (interactive "P")
5656   (prog1
5657       (cond
5658        ;; Empty summary.
5659        ((null gnus-newsgroup-data)
5660         (gnus-message 3 "No articles in the group")
5661         nil)
5662        ;; Pick the first article.
5663        ((not unread)
5664         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5665         (gnus-data-number (car gnus-newsgroup-data)))
5666        ;; No unread articles.
5667        ((null gnus-newsgroup-unreads)
5668         (gnus-message 3 "No more unread articles")
5669         nil)
5670        ;; Find the first unread article.
5671        (t
5672         (let ((data gnus-newsgroup-data))
5673           (while (and data
5674                       (and (not (and undownloaded
5675                                      (eq gnus-undownloaded-mark
5676                                          (gnus-data-mark (car data)))))
5677                            (not (gnus-data-unread-p (car data)))))
5678             (setq data (cdr data)))
5679           (when data
5680             (goto-char (gnus-data-pos (car data)))
5681             (gnus-data-number (car data))))))
5682     (gnus-summary-position-point)))
5683
5684 (defun gnus-summary-next-subject (n &optional unread dont-display)
5685   "Go to next N'th summary line.
5686 If N is negative, go to the previous N'th subject line.
5687 If UNREAD is non-nil, only unread articles are selected.
5688 The difference between N and the actual number of steps taken is
5689 returned."
5690   (interactive "p")
5691   (let ((backward (< n 0))
5692         (n (abs n)))
5693     (while (and (> n 0)
5694                 (if backward
5695                     (gnus-summary-find-prev unread)
5696                   (gnus-summary-find-next unread)))
5697       (gnus-summary-show-thread)
5698       (setq n (1- n)))
5699     (when (/= 0 n)
5700       (gnus-message 7 "No more%s articles"
5701                     (if unread " unread" "")))
5702     (unless dont-display
5703       (gnus-summary-recenter)
5704       (gnus-summary-position-point))
5705     n))
5706
5707 (defun gnus-summary-next-unread-subject (n)
5708   "Go to next N'th unread summary line."
5709   (interactive "p")
5710   (gnus-summary-next-subject n t))
5711
5712 (defun gnus-summary-prev-subject (n &optional unread)
5713   "Go to previous N'th summary line.
5714 If optional argument UNREAD is non-nil, only unread article is selected."
5715   (interactive "p")
5716   (gnus-summary-next-subject (- n) unread))
5717
5718 (defun gnus-summary-prev-unread-subject (n)
5719   "Go to previous N'th unread summary line."
5720   (interactive "p")
5721   (gnus-summary-next-subject (- n) t))
5722
5723 (defun gnus-summary-goto-subject (article &optional force silent)
5724   "Go the subject line of ARTICLE.
5725 If FORCE, also allow jumping to articles not currently shown."
5726   (interactive "nArticle number: ")
5727   (let ((b (point))
5728         (data (gnus-data-find article)))
5729     ;; We read in the article if we have to.
5730     (and (not data)
5731          force
5732          (gnus-summary-insert-subject
5733           article
5734           (if (or (numberp force) (vectorp force)) force)
5735           t)
5736          (setq data (gnus-data-find article)))
5737     (goto-char b)
5738     (if (not data)
5739         (progn
5740           (unless silent
5741             (gnus-message 3 "Can't find article %d" article))
5742           nil)
5743       (goto-char (gnus-data-pos data))
5744       (gnus-summary-position-point)
5745       article)))
5746
5747 ;; Walking around summary lines with displaying articles.
5748
5749 (defun gnus-summary-expand-window (&optional arg)
5750   "Make the summary buffer take up the entire Emacs frame.
5751 Given a prefix, will force an `article' buffer configuration."
5752   (interactive "P")
5753   (if arg
5754       (gnus-configure-windows 'article 'force)
5755     (gnus-configure-windows 'summary 'force)))
5756
5757 (defun gnus-summary-display-article (article &optional all-header)
5758   "Display ARTICLE in article buffer."
5759   (gnus-set-global-variables)
5760   (if (null article)
5761       nil
5762     (prog1
5763         (if gnus-summary-display-article-function
5764             (funcall gnus-summary-display-article-function article all-header)
5765           (gnus-article-prepare article all-header))
5766       (gnus-run-hooks 'gnus-select-article-hook)
5767       (when (and gnus-current-article
5768                  (not (zerop gnus-current-article)))
5769         (gnus-summary-goto-subject gnus-current-article))
5770       (gnus-summary-recenter)
5771       (when (and gnus-use-trees gnus-show-threads)
5772         (gnus-possibly-generate-tree article)
5773         (gnus-highlight-selected-tree article))
5774       ;; Successfully display article.
5775       (gnus-article-set-window-start
5776        (cdr (assq article gnus-newsgroup-bookmarks))))))
5777
5778 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5779   "Select the current article.
5780 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5781 non-nil, the article will be re-fetched even if it already present in
5782 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5783 be displayed."
5784   ;; Make sure we are in the summary buffer to work around bbdb bug.
5785   (unless (eq major-mode 'gnus-summary-mode)
5786     (set-buffer gnus-summary-buffer))
5787   (let ((article (or article (gnus-summary-article-number)))
5788         (all-headers (not (not all-headers))) ;Must be T or NIL.
5789         gnus-summary-display-article-function
5790         did)
5791     (and (not pseudo)
5792          (gnus-summary-article-pseudo-p article)
5793          (error "This is a pseudo-article"))
5794     (prog1
5795         (save-excursion
5796           (set-buffer gnus-summary-buffer)
5797           (if (or (and gnus-single-article-buffer
5798                        (or (null gnus-current-article)
5799                            (null gnus-article-current)
5800                            (null (get-buffer gnus-article-buffer))
5801                            (not (eq article (cdr gnus-article-current)))
5802                            (not (equal (car gnus-article-current)
5803                                        gnus-newsgroup-name))))
5804                   (and (not gnus-single-article-buffer)
5805                        (or (null gnus-current-article)
5806                            (not (eq gnus-current-article article))))
5807                   force)
5808               ;; The requested article is different from the current article.
5809               (prog1
5810                   (gnus-summary-display-article article all-headers)
5811                 (setq did article)
5812                 (when (or all-headers gnus-show-all-headers)
5813                   (gnus-article-show-all-headers)))
5814             (when (or all-headers gnus-show-all-headers)
5815               (gnus-article-show-all-headers))
5816             'old))
5817       (when did
5818         (gnus-article-set-window-start
5819          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5820
5821 (defun gnus-summary-set-current-mark (&optional current-mark)
5822   "Obsolete function."
5823   nil)
5824
5825 (defun gnus-summary-next-article (&optional unread subject backward push)
5826   "Select the next article.
5827 If UNREAD, only unread articles are selected.
5828 If SUBJECT, only articles with SUBJECT are selected.
5829 If BACKWARD, the previous article is selected instead of the next."
5830   (interactive "P")
5831   (cond
5832    ;; Is there such an article?
5833    ((and (gnus-summary-search-forward unread subject backward)
5834          (or (gnus-summary-display-article (gnus-summary-article-number))
5835              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5836     (gnus-summary-position-point))
5837    ;; If not, we try the first unread, if that is wanted.
5838    ((and subject
5839          gnus-auto-select-same
5840          (gnus-summary-first-unread-article))
5841     (gnus-summary-position-point)
5842     (gnus-message 6 "Wrapped"))
5843    ;; Try to get next/previous article not displayed in this group.
5844    ((and gnus-auto-extend-newsgroup
5845          (not unread) (not subject))
5846     (gnus-summary-goto-article
5847      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5848      nil (count-lines (point-min) (point))))
5849    ;; Go to next/previous group.
5850    (t
5851     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5852       (gnus-summary-jump-to-group gnus-newsgroup-name))
5853     (let ((cmd last-command-char)
5854           (point
5855            (save-excursion
5856              (set-buffer gnus-group-buffer)
5857              (point)))
5858           (group
5859            (if (eq gnus-keep-same-level 'best)
5860                (gnus-summary-best-group gnus-newsgroup-name)
5861              (gnus-summary-search-group backward gnus-keep-same-level))))
5862       ;; For some reason, the group window gets selected.  We change
5863       ;; it back.
5864       (select-window (get-buffer-window (current-buffer)))
5865       ;; Select next unread newsgroup automagically.
5866       (cond
5867        ((or (not gnus-auto-select-next)
5868             (not cmd))
5869         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5870        ((or (eq gnus-auto-select-next 'quietly)
5871             (and (eq gnus-auto-select-next 'slightly-quietly)
5872                  push)
5873             (and (eq gnus-auto-select-next 'almost-quietly)
5874                  (gnus-summary-last-article-p)))
5875         ;; Select quietly.
5876         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5877             (gnus-summary-exit)
5878           (gnus-message 7 "No more%s articles (%s)..."
5879                         (if unread " unread" "")
5880                         (if group (concat "selecting " group)
5881                           "exiting"))
5882           (gnus-summary-next-group nil group backward)))
5883        (t
5884         (when (gnus-key-press-event-p last-input-event)
5885           (gnus-summary-walk-group-buffer
5886            gnus-newsgroup-name cmd unread backward point))))))))
5887
5888 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5889   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5890                       (?\C-p (gnus-group-prev-unread-group 1))))
5891         (cursor-in-echo-area t)
5892         keve key group ended)
5893     (save-excursion
5894       (set-buffer gnus-group-buffer)
5895       (goto-char start)
5896       (setq group
5897             (if (eq gnus-keep-same-level 'best)
5898                 (gnus-summary-best-group gnus-newsgroup-name)
5899               (gnus-summary-search-group backward gnus-keep-same-level))))
5900     (while (not ended)
5901       (gnus-message
5902        5 "No more%s articles%s" (if unread " unread" "")
5903        (if (and group
5904                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5905            (format " (Type %s for %s [%s])"
5906                    (single-key-description cmd) group
5907                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5908          (format " (Type %s to exit %s)"
5909                  (single-key-description cmd)
5910                  gnus-newsgroup-name)))
5911       ;; Confirm auto selection.
5912       (setq key (car (setq keve (gnus-read-event-char))))
5913       (setq ended t)
5914       (cond
5915        ((assq key keystrokes)
5916         (let ((obuf (current-buffer)))
5917           (switch-to-buffer gnus-group-buffer)
5918           (when group
5919             (gnus-group-jump-to-group group))
5920           (eval (cadr (assq key keystrokes)))
5921           (setq group (gnus-group-group-name))
5922           (switch-to-buffer obuf))
5923         (setq ended nil))
5924        ((equal key cmd)
5925         (if (or (not group)
5926                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5927             (gnus-summary-exit)
5928           (gnus-summary-next-group nil group backward)))
5929        (t
5930         (push (cdr keve) unread-command-events))))))
5931
5932 (defun gnus-summary-next-unread-article ()
5933   "Select unread article after current one."
5934   (interactive)
5935   (gnus-summary-next-article
5936    (or (not (eq gnus-summary-goto-unread 'never))
5937        (gnus-summary-last-article-p (gnus-summary-article-number)))
5938    (and gnus-auto-select-same
5939         (gnus-summary-article-subject))))
5940
5941 (defun gnus-summary-prev-article (&optional unread subject)
5942   "Select the article after the current one.
5943 If UNREAD is non-nil, only unread articles are selected."
5944   (interactive "P")
5945   (gnus-summary-next-article unread subject t))
5946
5947 (defun gnus-summary-prev-unread-article ()
5948   "Select unread article before current one."
5949   (interactive)
5950   (gnus-summary-prev-article
5951    (or (not (eq gnus-summary-goto-unread 'never))
5952        (gnus-summary-first-article-p (gnus-summary-article-number)))
5953    (and gnus-auto-select-same
5954         (gnus-summary-article-subject))))
5955
5956 (defun gnus-summary-next-page (&optional lines circular)
5957   "Show next page of the selected article.
5958 If at the end of the current article, select the next article.
5959 LINES says how many lines should be scrolled up.
5960
5961 If CIRCULAR is non-nil, go to the start of the article instead of
5962 selecting the next article when reaching the end of the current
5963 article."
5964   (interactive "P")
5965   (setq gnus-summary-buffer (current-buffer))
5966   (gnus-set-global-variables)
5967   (let ((article (gnus-summary-article-number))
5968         (article-window (get-buffer-window gnus-article-buffer t))
5969         endp)
5970     ;; If the buffer is empty, we have no article.
5971     (unless article
5972       (error "No article to select"))
5973     (gnus-configure-windows 'article)
5974     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5975         (if (and (eq gnus-summary-goto-unread 'never)
5976                  (not (gnus-summary-last-article-p article)))
5977             (gnus-summary-next-article)
5978           (gnus-summary-next-unread-article))
5979       (if (or (null gnus-current-article)
5980               (null gnus-article-current)
5981               (/= article (cdr gnus-article-current))
5982               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5983           ;; Selected subject is different from current article's.
5984           (gnus-summary-display-article article)
5985         (when article-window
5986           (gnus-eval-in-buffer-window gnus-article-buffer
5987             (setq endp (gnus-article-next-page lines)))
5988           (when endp
5989             (cond (circular
5990                    (gnus-summary-beginning-of-article))
5991                   (lines
5992                    (gnus-message 3 "End of message"))
5993                   ((null lines)
5994                    (if (and (eq gnus-summary-goto-unread 'never)
5995                             (not (gnus-summary-last-article-p article)))
5996                        (gnus-summary-next-article)
5997                      (gnus-summary-next-unread-article))))))))
5998     (gnus-summary-recenter)
5999     (gnus-summary-position-point)))
6000
6001 (defun gnus-summary-prev-page (&optional lines move)
6002   "Show previous page of selected article.
6003 Argument LINES specifies lines to be scrolled down.
6004 If MOVE, move to the previous unread article if point is at
6005 the beginning of the buffer."
6006   (interactive "P")
6007   (let ((article (gnus-summary-article-number))
6008         (article-window (get-buffer-window gnus-article-buffer t))
6009         endp)
6010     (gnus-configure-windows 'article)
6011     (if (or (null gnus-current-article)
6012             (null gnus-article-current)
6013             (/= article (cdr gnus-article-current))
6014             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6015         ;; Selected subject is different from current article's.
6016         (gnus-summary-display-article article)
6017       (gnus-summary-recenter)
6018       (when article-window
6019         (gnus-eval-in-buffer-window gnus-article-buffer
6020           (setq endp (gnus-article-prev-page lines)))
6021         (when (and move endp)
6022           (cond (lines
6023                  (gnus-message 3 "Beginning of message"))
6024                 ((null lines)
6025                  (if (and (eq gnus-summary-goto-unread 'never)
6026                           (not (gnus-summary-first-article-p article)))
6027                      (gnus-summary-prev-article)
6028                    (gnus-summary-prev-unread-article))))))))
6029   (gnus-summary-position-point))
6030
6031 (defun gnus-summary-prev-page-or-article (&optional lines)
6032   "Show previous page of selected article.
6033 Argument LINES specifies lines to be scrolled down.
6034 If at the beginning of the article, go to the next article."
6035   (interactive "P")
6036   (gnus-summary-prev-page lines t))
6037
6038 (defun gnus-summary-scroll-up (lines)
6039   "Scroll up (or down) one line current article.
6040 Argument LINES specifies lines to be scrolled up (or down if negative)."
6041   (interactive "p")
6042   (gnus-configure-windows 'article)
6043   (gnus-summary-show-thread)
6044   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6045     (gnus-eval-in-buffer-window gnus-article-buffer
6046       (cond ((> lines 0)
6047              (when (gnus-article-next-page lines)
6048                (gnus-message 3 "End of message")))
6049             ((< lines 0)
6050              (gnus-article-prev-page (- lines))))))
6051   (gnus-summary-recenter)
6052   (gnus-summary-position-point))
6053
6054 (defun gnus-summary-scroll-down (lines)
6055   "Scroll down (or up) one line current article.
6056 Argument LINES specifies lines to be scrolled down (or up if negative)."
6057   (interactive "p")
6058   (gnus-summary-scroll-up (- lines)))
6059
6060 (defun gnus-summary-next-same-subject ()
6061   "Select next article which has the same subject as current one."
6062   (interactive)
6063   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6064
6065 (defun gnus-summary-prev-same-subject ()
6066   "Select previous article which has the same subject as current one."
6067   (interactive)
6068   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6069
6070 (defun gnus-summary-next-unread-same-subject ()
6071   "Select next unread article which has the same subject as current one."
6072   (interactive)
6073   (gnus-summary-next-article t (gnus-summary-article-subject)))
6074
6075 (defun gnus-summary-prev-unread-same-subject ()
6076   "Select previous unread article which has the same subject as current one."
6077   (interactive)
6078   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6079
6080 (defun gnus-summary-first-unread-article ()
6081   "Select the first unread article.
6082 Return nil if there are no unread articles."
6083   (interactive)
6084   (prog1
6085       (when (gnus-summary-first-subject t)
6086         (gnus-summary-show-thread)
6087         (gnus-summary-first-subject t)
6088         (gnus-summary-display-article (gnus-summary-article-number)))
6089     (gnus-summary-position-point)))
6090
6091 (defun gnus-summary-first-unread-subject ()
6092   "Place the point on the subject line of the first unread article.
6093 Return nil if there are no unread articles."
6094   (interactive)
6095   (prog1
6096       (when (gnus-summary-first-subject t)
6097         (gnus-summary-show-thread)
6098         (gnus-summary-first-subject t))
6099     (gnus-summary-position-point)))
6100
6101 (defun gnus-summary-first-article ()
6102   "Select the first article.
6103 Return nil if there are no articles."
6104   (interactive)
6105   (prog1
6106       (when (gnus-summary-first-subject)
6107         (gnus-summary-show-thread)
6108         (gnus-summary-first-subject)
6109         (gnus-summary-display-article (gnus-summary-article-number)))
6110     (gnus-summary-position-point)))
6111
6112 (defun gnus-summary-best-unread-article ()
6113   "Select the unread article with the highest score."
6114   (interactive)
6115   (let ((best -1000000)
6116         (data gnus-newsgroup-data)
6117         article score)
6118     (while data
6119       (and (gnus-data-unread-p (car data))
6120            (> (setq score
6121                     (gnus-summary-article-score (gnus-data-number (car data))))
6122               best)
6123            (setq best score
6124                  article (gnus-data-number (car data))))
6125       (setq data (cdr data)))
6126     (prog1
6127         (if article
6128             (gnus-summary-goto-article article)
6129           (error "No unread articles"))
6130       (gnus-summary-position-point))))
6131
6132 (defun gnus-summary-last-subject ()
6133   "Go to the last displayed subject line in the group."
6134   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6135     (when article
6136       (gnus-summary-goto-subject article))))
6137
6138 (defun gnus-summary-goto-article (article &optional all-headers force)
6139   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6140 If ALL-HEADERS is non-nil, no header lines are hidden.
6141 If FORCE, go to the article even if it isn't displayed.  If FORCE
6142 is a number, it is the line the article is to be displayed on."
6143   (interactive
6144    (list
6145     (completing-read
6146      "Article number or Message-ID: "
6147      (mapcar (lambda (number) (list (int-to-string number)))
6148              gnus-newsgroup-limit))
6149     current-prefix-arg
6150     t))
6151   (prog1
6152       (if (and (stringp article)
6153                (string-match "@" article))
6154           (gnus-summary-refer-article article)
6155         (when (stringp article)
6156           (setq article (string-to-number article)))
6157         (if (gnus-summary-goto-subject article force)
6158             (gnus-summary-display-article article all-headers)
6159           (gnus-message 4 "Couldn't go to article %s" article) nil))
6160     (gnus-summary-position-point)))
6161
6162 (defun gnus-summary-goto-last-article ()
6163   "Go to the previously read article."
6164   (interactive)
6165   (prog1
6166       (when gnus-last-article
6167         (gnus-summary-goto-article gnus-last-article nil t))
6168     (gnus-summary-position-point)))
6169
6170 (defun gnus-summary-pop-article (number)
6171   "Pop one article off the history and go to the previous.
6172 NUMBER articles will be popped off."
6173   (interactive "p")
6174   (let (to)
6175     (setq gnus-newsgroup-history
6176           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6177     (if to
6178         (gnus-summary-goto-article (car to) nil t)
6179       (error "Article history empty")))
6180   (gnus-summary-position-point))
6181
6182 ;; Summary commands and functions for limiting the summary buffer.
6183
6184 (defun gnus-summary-limit-to-articles (n)
6185   "Limit the summary buffer to the next N articles.
6186 If not given a prefix, use the process marked articles instead."
6187   (interactive "P")
6188   (prog1
6189       (let ((articles (gnus-summary-work-articles n)))
6190         (setq gnus-newsgroup-processable nil)
6191         (gnus-summary-limit articles))
6192     (gnus-summary-position-point)))
6193
6194 (defun gnus-summary-pop-limit (&optional total)
6195   "Restore the previous limit.
6196 If given a prefix, remove all limits."
6197   (interactive "P")
6198   (when total
6199     (setq gnus-newsgroup-limits
6200           (list (mapcar (lambda (h) (mail-header-number h))
6201                         gnus-newsgroup-headers))))
6202   (unless gnus-newsgroup-limits
6203     (error "No limit to pop"))
6204   (prog1
6205       (gnus-summary-limit nil 'pop)
6206     (gnus-summary-position-point)))
6207
6208 (defun gnus-summary-limit-to-subject (subject &optional header)
6209   "Limit the summary buffer to articles that have subjects that match a regexp."
6210   (interactive "sLimit to subject (regexp): ")
6211   (unless header
6212     (setq header "subject"))
6213   (when (not (equal "" subject))
6214     (prog1
6215         (let ((articles (gnus-summary-find-matching
6216                          (or header "subject") subject 'all)))
6217           (unless articles
6218             (error "Found no matches for \"%s\"" subject))
6219           (gnus-summary-limit articles))
6220       (gnus-summary-position-point))))
6221
6222 (defun gnus-summary-limit-to-author (from)
6223   "Limit the summary buffer to articles that have authors that match a regexp."
6224   (interactive "sLimit to author (regexp): ")
6225   (gnus-summary-limit-to-subject from "from"))
6226
6227 (defun gnus-summary-limit-to-age (age &optional younger-p)
6228   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6229 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6230 articles that are younger than AGE days."
6231   (interactive
6232    (let ((younger current-prefix-arg)
6233          (days-got nil)
6234          days)
6235      (while (not days-got)
6236        (setq days (if younger
6237                       (read-string "Limit to articles within (in days): ")
6238                     (read-string "Limit to articles old than (in days): ")))
6239        (when (> (length days) 0)
6240          (setq days (read days)))
6241        (if (numberp days)
6242            (setq days-got t)
6243          (message "Please enter a number.")
6244          (sleep-for 1)))
6245      (list days younger)))
6246   (prog1
6247       (let ((data gnus-newsgroup-data)
6248             (cutoff (days-to-time age))
6249             articles d date is-younger)
6250         (while (setq d (pop data))
6251           (when (and (vectorp (gnus-data-header d))
6252                      (setq date (mail-header-date (gnus-data-header d))))
6253             (setq is-younger (time-less-p
6254                               (time-since (condition-case ()
6255                                               (date-to-time date)
6256                                             (error '(0 0))))
6257                               cutoff))
6258             (when (if younger-p
6259                       is-younger
6260                     (not is-younger))
6261               (push (gnus-data-number d) articles))))
6262         (gnus-summary-limit (nreverse articles)))
6263     (gnus-summary-position-point)))
6264
6265 (defun gnus-summary-limit-to-extra (header regexp)
6266   "Limit the summary buffer to articles that match an 'extra' header."
6267   (interactive
6268    (let ((header
6269           (intern
6270            (gnus-completing-read
6271             (symbol-name (car gnus-extra-headers))      
6272             "Limit extra header:"       
6273             (mapcar (lambda (x) 
6274                       (cons (symbol-name x) x))
6275                     gnus-extra-headers)
6276             nil                 
6277             t))))
6278      (list header
6279            (read-string (format "Limit to header %s (regexp): " header)))))
6280   (when (not (equal "" regexp))
6281     (prog1
6282         (let ((articles (gnus-summary-find-matching
6283                          (cons 'extra header) regexp 'all)))
6284           (unless articles
6285             (error "Found no matches for \"%s\"" regexp))
6286           (gnus-summary-limit articles))
6287       (gnus-summary-position-point))))
6288
6289 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6290 (make-obsolete
6291  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6292
6293 (defun gnus-summary-limit-to-unread (&optional all)
6294   "Limit the summary buffer to articles that are not marked as read.
6295 If ALL is non-nil, limit strictly to unread articles."
6296   (interactive "P")
6297   (if all
6298       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6299     (gnus-summary-limit-to-marks
6300      ;; Concat all the marks that say that an article is read and have
6301      ;; those removed.
6302      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6303            gnus-killed-mark gnus-kill-file-mark
6304            gnus-low-score-mark gnus-expirable-mark
6305            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6306            gnus-duplicate-mark gnus-souped-mark)
6307      'reverse)))
6308
6309 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6310 (make-obsolete 'gnus-summary-delete-marked-with
6311                'gnus-summary-limit-exlude-marks)
6312
6313 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6314   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6315 If REVERSE, limit the summary buffer to articles that are marked
6316 with MARKS.  MARKS can either be a string of marks or a list of marks.
6317 Returns how many articles were removed."
6318   (interactive "sMarks: ")
6319   (gnus-summary-limit-to-marks marks t))
6320
6321 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6322   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6323 If REVERSE (the prefix), limit the summary buffer to articles that are
6324 not marked with MARKS.  MARKS can either be a string of marks or a
6325 list of marks.
6326 Returns how many articles were removed."
6327   (interactive "sMarks: \nP")
6328   (prog1
6329       (let ((data gnus-newsgroup-data)
6330             (marks (if (listp marks) marks
6331                      (append marks nil))) ; Transform to list.
6332             articles)
6333         (while data
6334           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6335                   (memq (gnus-data-mark (car data)) marks))
6336             (push (gnus-data-number (car data)) articles))
6337           (setq data (cdr data)))
6338         (gnus-summary-limit articles))
6339     (gnus-summary-position-point)))
6340
6341 (defun gnus-summary-limit-to-score (&optional score)
6342   "Limit to articles with score at or above SCORE."
6343   (interactive "P")
6344   (setq score (if score
6345                   (prefix-numeric-value score)
6346                 (or gnus-summary-default-score 0)))
6347   (let ((data gnus-newsgroup-data)
6348         articles)
6349     (while data
6350       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6351                 score)
6352         (push (gnus-data-number (car data)) articles))
6353       (setq data (cdr data)))
6354     (prog1
6355         (gnus-summary-limit articles)
6356       (gnus-summary-position-point))))
6357
6358 (defun gnus-summary-limit-include-thread (id)
6359   "Display all the hidden articles that in the current thread."
6360   (interactive (list (mail-header-id (gnus-summary-article-header))))
6361   (let ((articles (gnus-articles-in-thread
6362                    (gnus-id-to-thread (gnus-root-id id)))))
6363     (prog1
6364         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6365       (gnus-summary-position-point))))
6366
6367 (defun gnus-summary-limit-include-dormant ()
6368   "Display all the hidden articles that are marked as dormant.
6369 Note that this command only works on a subset of the articles currently
6370 fetched for this group."
6371   (interactive)
6372   (unless gnus-newsgroup-dormant
6373     (error "There are no dormant articles in this group"))
6374   (prog1
6375       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6376     (gnus-summary-position-point)))
6377
6378 (defun gnus-summary-limit-exclude-dormant ()
6379   "Hide all dormant articles."
6380   (interactive)
6381   (prog1
6382       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6383     (gnus-summary-position-point)))
6384
6385 (defun gnus-summary-limit-exclude-childless-dormant ()
6386   "Hide all dormant articles that have no children."
6387   (interactive)
6388   (let ((data (gnus-data-list t))
6389         articles d children)
6390     ;; Find all articles that are either not dormant or have
6391     ;; children.
6392     (while (setq d (pop data))
6393       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6394                 (and (setq children
6395                            (gnus-article-children (gnus-data-number d)))
6396                      (let (found)
6397                        (while children
6398                          (when (memq (car children) articles)
6399                            (setq children nil
6400                                  found t))
6401                          (pop children))
6402                        found)))
6403         (push (gnus-data-number d) articles)))
6404     ;; Do the limiting.
6405     (prog1
6406         (gnus-summary-limit articles)
6407       (gnus-summary-position-point))))
6408
6409 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6410   "Mark all unread excluded articles as read.
6411 If ALL, mark even excluded ticked and dormants as read."
6412   (interactive "P")
6413   (let ((articles (gnus-sorted-complement
6414                    (sort
6415                     (mapcar (lambda (h) (mail-header-number h))
6416                             gnus-newsgroup-headers)
6417                     '<)
6418                    (sort gnus-newsgroup-limit '<)))
6419         article)
6420     (setq gnus-newsgroup-unreads
6421           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6422     (if all
6423         (setq gnus-newsgroup-dormant nil
6424               gnus-newsgroup-marked nil
6425               gnus-newsgroup-reads
6426               (nconc
6427                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6428                gnus-newsgroup-reads))
6429       (while (setq article (pop articles))
6430         (unless (or (memq article gnus-newsgroup-dormant)
6431                     (memq article gnus-newsgroup-marked))
6432           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6433
6434 (defun gnus-summary-limit (articles &optional pop)
6435   (if pop
6436       ;; We pop the previous limit off the stack and use that.
6437       (setq articles (car gnus-newsgroup-limits)
6438             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6439     ;; We use the new limit, so we push the old limit on the stack.
6440     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6441   ;; Set the limit.
6442   (setq gnus-newsgroup-limit articles)
6443   (let ((total (length gnus-newsgroup-data))
6444         (data (gnus-data-find-list (gnus-summary-article-number)))
6445         (gnus-summary-mark-below nil)   ; Inhibit this.
6446         found)
6447     ;; This will do all the work of generating the new summary buffer
6448     ;; according to the new limit.
6449     (gnus-summary-prepare)
6450     ;; Hide any threads, possibly.
6451     (and gnus-show-threads
6452          gnus-thread-hide-subtree
6453          (gnus-summary-hide-all-threads))
6454     ;; Try to return to the article you were at, or one in the
6455     ;; neighborhood.
6456     (when data
6457       ;; We try to find some article after the current one.
6458       (while data
6459         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6460           (setq data nil
6461                 found t))
6462         (setq data (cdr data))))
6463     (unless found
6464       ;; If there is no data, that means that we were after the last
6465       ;; article.  The same goes when we can't find any articles
6466       ;; after the current one.
6467       (goto-char (point-max))
6468       (gnus-summary-find-prev))
6469     (gnus-set-mode-line 'summary)
6470     ;; We return how many articles were removed from the summary
6471     ;; buffer as a result of the new limit.
6472     (- total (length gnus-newsgroup-data))))
6473
6474 (defsubst gnus-invisible-cut-children (threads)
6475   (let ((num 0))
6476     (while threads
6477       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6478         (incf num))
6479       (pop threads))
6480     (< num 2)))
6481
6482 (defsubst gnus-cut-thread (thread)
6483   "Go forwards in the thread until we find an article that we want to display."
6484   (when (or (eq gnus-fetch-old-headers 'some)
6485             (eq gnus-fetch-old-headers 'invisible)
6486             (numberp gnus-fetch-old-headers)
6487             (eq gnus-build-sparse-threads 'some)
6488             (eq gnus-build-sparse-threads 'more))
6489     ;; Deal with old-fetched headers and sparse threads.
6490     (while (and
6491             thread
6492             (or
6493              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6494              (gnus-summary-article-ancient-p
6495               (mail-header-number (car thread))))
6496             (if (or (<= (length (cdr thread)) 1)
6497                     (eq gnus-fetch-old-headers 'invisible))
6498                 (setq gnus-newsgroup-limit
6499                       (delq (mail-header-number (car thread))
6500                             gnus-newsgroup-limit)
6501                       thread (cadr thread))
6502               (when (gnus-invisible-cut-children (cdr thread))
6503                 (let ((th (cdr thread)))
6504                   (while th
6505                     (if (memq (mail-header-number (caar th))
6506                               gnus-newsgroup-limit)
6507                         (setq thread (car th)
6508                               th nil)
6509                       (setq th (cdr th))))))))))
6510   thread)
6511
6512 (defun gnus-cut-threads (threads)
6513   "Cut off all uninteresting articles from the beginning of threads."
6514   (when (or (eq gnus-fetch-old-headers 'some)
6515             (eq gnus-fetch-old-headers 'invisible)
6516             (numberp gnus-fetch-old-headers)
6517             (eq gnus-build-sparse-threads 'some)
6518             (eq gnus-build-sparse-threads 'more))
6519     (let ((th threads))
6520       (while th
6521         (setcar th (gnus-cut-thread (car th)))
6522         (setq th (cdr th)))))
6523   ;; Remove nixed out threads.
6524   (delq nil threads))
6525
6526 (defun gnus-summary-initial-limit (&optional show-if-empty)
6527   "Figure out what the initial limit is supposed to be on group entry.
6528 This entails weeding out unwanted dormants, low-scored articles,
6529 fetch-old-headers verbiage, and so on."
6530   ;; Most groups have nothing to remove.
6531   (if (or gnus-inhibit-limiting
6532           (and (null gnus-newsgroup-dormant)
6533                (not (eq gnus-fetch-old-headers 'some))
6534                (not (numberp gnus-fetch-old-headers))
6535                (not (eq gnus-fetch-old-headers 'invisible))
6536                (null gnus-summary-expunge-below)
6537                (not (eq gnus-build-sparse-threads 'some))
6538                (not (eq gnus-build-sparse-threads 'more))
6539                (null gnus-thread-expunge-below)
6540                (not gnus-use-nocem)))
6541       ()                                ; Do nothing.
6542     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6543     (setq gnus-newsgroup-limit nil)
6544     (mapatoms
6545      (lambda (node)
6546        (unless (car (symbol-value node))
6547          ;; These threads have no parents -- they are roots.
6548          (let ((nodes (cdr (symbol-value node)))
6549                thread)
6550            (while nodes
6551              (if (and gnus-thread-expunge-below
6552                       (< (gnus-thread-total-score (car nodes))
6553                          gnus-thread-expunge-below))
6554                  (gnus-expunge-thread (pop nodes))
6555                (setq thread (pop nodes))
6556                (gnus-summary-limit-children thread))))))
6557      gnus-newsgroup-dependencies)
6558     ;; If this limitation resulted in an empty group, we might
6559     ;; pop the previous limit and use it instead.
6560     (when (and (not gnus-newsgroup-limit)
6561                show-if-empty)
6562       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6563     gnus-newsgroup-limit))
6564
6565 (defun gnus-summary-limit-children (thread)
6566   "Return 1 if this subthread is visible and 0 if it is not."
6567   ;; First we get the number of visible children to this thread.  This
6568   ;; is done by recursing down the thread using this function, so this
6569   ;; will really go down to a leaf article first, before slowly
6570   ;; working its way up towards the root.
6571   (when thread
6572     (let ((children
6573            (if (cdr thread)
6574                (apply '+ (mapcar 'gnus-summary-limit-children
6575                                  (cdr thread)))
6576              0))
6577           (number (mail-header-number (car thread)))
6578           score)
6579       (if (and
6580            (not (memq number gnus-newsgroup-marked))
6581            (or
6582             ;; If this article is dormant and has absolutely no visible
6583             ;; children, then this article isn't visible.
6584             (and (memq number gnus-newsgroup-dormant)
6585                  (zerop children))
6586             ;; If this is "fetch-old-headered" and there is no
6587             ;; visible children, then we don't want this article.
6588             (and (or (eq gnus-fetch-old-headers 'some)
6589                      (numberp gnus-fetch-old-headers))
6590                  (gnus-summary-article-ancient-p number)
6591                  (zerop children))
6592             ;; If this is "fetch-old-headered" and `invisible', then
6593             ;; we don't want this article.
6594             (and (eq gnus-fetch-old-headers 'invisible)
6595                  (gnus-summary-article-ancient-p number))
6596             ;; If this is a sparsely inserted article with no children,
6597             ;; we don't want it.
6598             (and (eq gnus-build-sparse-threads 'some)
6599                  (gnus-summary-article-sparse-p number)
6600                  (zerop children))
6601             ;; If we use expunging, and this article is really
6602             ;; low-scored, then we don't want this article.
6603             (when (and gnus-summary-expunge-below
6604                        (< (setq score
6605                                 (or (cdr (assq number gnus-newsgroup-scored))
6606                                     gnus-summary-default-score))
6607                           gnus-summary-expunge-below))
6608               ;; We increase the expunge-tally here, but that has
6609               ;; nothing to do with the limits, really.
6610               (incf gnus-newsgroup-expunged-tally)
6611               ;; We also mark as read here, if that's wanted.
6612               (when (and gnus-summary-mark-below
6613                          (< score gnus-summary-mark-below))
6614                 (setq gnus-newsgroup-unreads
6615                       (delq number gnus-newsgroup-unreads))
6616                 (if gnus-newsgroup-auto-expire
6617                     (push number gnus-newsgroup-expirable)
6618                   (push (cons number gnus-low-score-mark)
6619                         gnus-newsgroup-reads)))
6620               t)
6621             ;; Check NoCeM things.
6622             (if (and gnus-use-nocem
6623                      (gnus-nocem-unwanted-article-p
6624                       (mail-header-id (car thread))))
6625                 (progn
6626                   (setq gnus-newsgroup-unreads
6627                         (delq number gnus-newsgroup-unreads))
6628                   t))))
6629           ;; Nope, invisible article.
6630           0
6631         ;; Ok, this article is to be visible, so we add it to the limit
6632         ;; and return 1.
6633         (push number gnus-newsgroup-limit)
6634         1))))
6635
6636 (defun gnus-expunge-thread (thread)
6637   "Mark all articles in THREAD as read."
6638   (let* ((number (mail-header-number (car thread))))
6639     (incf gnus-newsgroup-expunged-tally)
6640     ;; We also mark as read here, if that's wanted.
6641     (setq gnus-newsgroup-unreads
6642           (delq number gnus-newsgroup-unreads))
6643     (if gnus-newsgroup-auto-expire
6644         (push number gnus-newsgroup-expirable)
6645       (push (cons number gnus-low-score-mark)
6646             gnus-newsgroup-reads)))
6647   ;; Go recursively through all subthreads.
6648   (mapcar 'gnus-expunge-thread (cdr thread)))
6649
6650 ;; Summary article oriented commands
6651
6652 (defun gnus-summary-refer-parent-article (n)
6653   "Refer parent article N times.
6654 If N is negative, go to ancestor -N instead.
6655 The difference between N and the number of articles fetched is returned."
6656   (interactive "p")
6657   (let ((skip 1)
6658         error header ref)
6659     (when (not (natnump n))
6660       (setq skip (abs n)
6661             n 1))
6662     (while (and (> n 0)
6663                 (not error))
6664       (setq header (gnus-summary-article-header))
6665       (if (and (eq (mail-header-number header)
6666                    (cdr gnus-article-current))
6667                (equal gnus-newsgroup-name
6668                       (car gnus-article-current)))
6669           ;; If we try to find the parent of the currently
6670           ;; displayed article, then we take a look at the actual
6671           ;; References header, since this is slightly more
6672           ;; reliable than the References field we got from the
6673           ;; server.
6674           (save-excursion
6675             (set-buffer gnus-original-article-buffer)
6676             (nnheader-narrow-to-headers)
6677             (unless (setq ref (message-fetch-field "references"))
6678               (setq ref (message-fetch-field "in-reply-to")))
6679             (widen))
6680         (setq ref
6681               ;; It's not the current article, so we take a bet on
6682               ;; the value we got from the server.
6683               (mail-header-references header)))
6684       (if (and ref
6685                (not (equal ref "")))
6686           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6687             (gnus-message 1 "Couldn't find parent"))
6688         (gnus-message 1 "No references in article %d"
6689                       (gnus-summary-article-number))
6690         (setq error t))
6691       (decf n))
6692     (gnus-summary-position-point)
6693     n))
6694
6695 (defun gnus-summary-refer-references ()
6696   "Fetch all articles mentioned in the References header.
6697 Return the number of articles fetched."
6698   (interactive)
6699   (let ((ref (mail-header-references (gnus-summary-article-header)))
6700         (current (gnus-summary-article-number))
6701         (n 0))
6702     (if (or (not ref)
6703             (equal ref ""))
6704         (error "No References in the current article")
6705       ;; For each Message-ID in the References header...
6706       (while (string-match "<[^>]*>" ref)
6707         (incf n)
6708         ;; ... fetch that article.
6709         (gnus-summary-refer-article
6710          (prog1 (match-string 0 ref)
6711            (setq ref (substring ref (match-end 0))))))
6712       (gnus-summary-goto-subject current)
6713       (gnus-summary-position-point)
6714       n)))
6715
6716 (defun gnus-summary-refer-thread (&optional limit)
6717   "Fetch all articles in the current thread.
6718 If LIMIT (the numerical prefix), fetch that many old headers instead
6719 of what's specified by the `gnus-refer-thread-limit' variable."
6720   (interactive "P")
6721   (let ((id (mail-header-id (gnus-summary-article-header)))
6722         (limit (if limit (prefix-numeric-value limit)
6723                  gnus-refer-thread-limit)))
6724     ;; We want to fetch LIMIT *old* headers, but we also have to
6725     ;; re-fetch all the headers in the current buffer, because many of
6726     ;; them may be undisplayed.  So we adjust LIMIT.
6727     (when (numberp limit)
6728       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6729     (unless (eq gnus-fetch-old-headers 'invisible)
6730       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6731       ;; Retrieve the headers and read them in.
6732       (if (eq (gnus-retrieve-headers
6733                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6734               'nov)
6735           (gnus-build-all-threads)
6736         (error "Can't fetch thread from backends that don't support NOV"))
6737       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6738     (gnus-summary-limit-include-thread id)))
6739
6740 (defun gnus-summary-refer-article (message-id)
6741   "Fetch an article specified by MESSAGE-ID."
6742   (interactive "sMessage-ID: ")
6743   (when (and (stringp message-id)
6744              (not (zerop (length message-id))))
6745     ;; Construct the correct Message-ID if necessary.
6746     ;; Suggested by tale@pawl.rpi.edu.
6747     (unless (string-match "^<" message-id)
6748       (setq message-id (concat "<" message-id)))
6749     (unless (string-match ">$" message-id)
6750       (setq message-id (concat message-id ">")))
6751     (let* ((header (gnus-id-to-header message-id))
6752            (sparse (and header
6753                         (gnus-summary-article-sparse-p
6754                          (mail-header-number header))
6755                         (memq (mail-header-number header)
6756                               gnus-newsgroup-limit)))
6757            number)
6758       (cond
6759        ;; If the article is present in the buffer we just go to it.
6760        ((and header
6761              (or (not (gnus-summary-article-sparse-p
6762                        (mail-header-number header)))
6763                  sparse))
6764         (prog1
6765             (gnus-summary-goto-article
6766              (mail-header-number header) nil t)
6767           (when sparse
6768             (gnus-summary-update-article (mail-header-number header)))))
6769        (t
6770         ;; We fetch the article.
6771         (catch 'found
6772           (dolist (gnus-override-method
6773                    (cond ((null gnus-refer-article-method)
6774                           (list 'current gnus-select-method))
6775                          ((consp (car gnus-refer-article-method))
6776                           gnus-refer-article-method)
6777                          (t
6778                           (list gnus-refer-article-method))))
6779             (when (eq 'current gnus-override-method)
6780               (setq gnus-override-method gnus-current-select-method))
6781             (gnus-check-server gnus-override-method)
6782             ;; Fetch the header, and display the article.
6783             (when (setq number (gnus-summary-insert-subject message-id))
6784               (gnus-summary-select-article nil nil nil number)
6785               (throw 'found t)))
6786           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6787
6788 (defun gnus-summary-edit-parameters ()
6789   "Edit the group parameters of the current group."
6790   (interactive)
6791   (gnus-group-edit-group gnus-newsgroup-name 'params))
6792
6793 (defun gnus-summary-customize-parameters ()
6794   "Customize the group parameters of the current group."
6795   (interactive)
6796   (gnus-group-customize gnus-newsgroup-name))
6797
6798 (defun gnus-summary-enter-digest-group (&optional force)
6799   "Enter an nndoc group based on the current article.
6800 If FORCE, force a digest interpretation.  If not, try
6801 to guess what the document format is."
6802   (interactive "P")
6803   (let ((conf gnus-current-window-configuration))
6804     (save-excursion
6805       (gnus-summary-select-article))
6806     (setq gnus-current-window-configuration conf)
6807     (let* ((name (format "%s-%d"
6808                          (gnus-group-prefixed-name
6809                           gnus-newsgroup-name (list 'nndoc ""))
6810                          (save-excursion
6811                            (set-buffer gnus-summary-buffer)
6812                            gnus-current-article)))
6813            (ogroup gnus-newsgroup-name)
6814            (params (append (gnus-info-params (gnus-get-info ogroup))
6815                            (list (cons 'to-group ogroup))
6816                            (list (cons 'save-article-group ogroup))))
6817            (case-fold-search t)
6818            (buf (current-buffer))
6819            dig to-address)
6820       (save-excursion
6821         (set-buffer gnus-original-article-buffer)
6822         ;; Have the digest group inherit the main mail address of
6823         ;; the parent article.
6824         (when (setq to-address (or (message-fetch-field "reply-to")
6825                                    (message-fetch-field "from")))
6826           (setq params (append (list (cons 'to-address to-address)))))
6827         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6828         (insert-buffer-substring gnus-original-article-buffer)
6829         ;; Remove lines that may lead nndoc to misinterpret the
6830         ;; document type.
6831         (narrow-to-region
6832          (goto-char (point-min))
6833          (or (search-forward "\n\n" nil t) (point)))
6834         (goto-char (point-min))
6835         (delete-matching-lines "^Path:\\|^From ")
6836         (widen))
6837       (unwind-protect
6838           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6839                     (gnus-newsgroup-ephemeral-ignored-charsets
6840                      gnus-newsgroup-ignored-charsets))
6841                 (gnus-group-read-ephemeral-group
6842                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6843                               (nndoc-article-type
6844                                ,(if force 'mbox 'guess))) t))
6845               ;; Make all postings to this group go to the parent group.
6846               (nconc (gnus-info-params (gnus-get-info name))
6847                      params)
6848             ;; Couldn't select this doc group.
6849             (switch-to-buffer buf)
6850             (gnus-set-global-variables)
6851             (gnus-configure-windows 'summary)
6852             (gnus-message 3 "Article couldn't be entered?"))
6853         (kill-buffer dig)))))
6854
6855 (defun gnus-summary-read-document (n)
6856   "Open a new group based on the current article(s).
6857 This will allow you to read digests and other similar
6858 documents as newsgroups.
6859 Obeys the standard process/prefix convention."
6860   (interactive "P")
6861   (let* ((articles (gnus-summary-work-articles n))
6862          (ogroup gnus-newsgroup-name)
6863          (params (append (gnus-info-params (gnus-get-info ogroup))
6864                          (list (cons 'to-group ogroup))))
6865          article group egroup groups vgroup)
6866     (while (setq article (pop articles))
6867       (setq group (format "%s-%d" gnus-newsgroup-name article))
6868       (gnus-summary-remove-process-mark article)
6869       (when (gnus-summary-display-article article)
6870         (save-excursion
6871           (with-temp-buffer
6872             (insert-buffer-substring gnus-original-article-buffer)
6873             ;; Remove some headers that may lead nndoc to make
6874             ;; the wrong guess.
6875             (message-narrow-to-head)
6876             (goto-char (point-min))
6877             (delete-matching-lines "^\\(Path\\):\\|^From ")
6878             (widen)
6879             (if (setq egroup
6880                       (gnus-group-read-ephemeral-group
6881                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6882                                      (nndoc-article-type guess))
6883                        t nil t))
6884                 (progn
6885                   ;; Make all postings to this group go to the parent group.
6886                   (nconc (gnus-info-params (gnus-get-info egroup))
6887                          params)
6888                   (push egroup groups))
6889               ;; Couldn't select this doc group.
6890               (gnus-error 3 "Article couldn't be entered"))))))
6891     ;; Now we have selected all the documents.
6892     (cond
6893      ((not groups)
6894       (error "None of the articles could be interpreted as documents"))
6895      ((gnus-group-read-ephemeral-group
6896        (setq vgroup (format
6897                      "nnvirtual:%s-%s" gnus-newsgroup-name
6898                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6899        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6900        t
6901        (cons (current-buffer) 'summary)))
6902      (t
6903       (error "Couldn't select virtual nndoc group")))))
6904
6905 (defun gnus-summary-isearch-article (&optional regexp-p)
6906   "Do incremental search forward on the current article.
6907 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6908   (interactive "P")
6909   (gnus-summary-select-article)
6910   (gnus-configure-windows 'article)
6911   (gnus-eval-in-buffer-window gnus-article-buffer
6912     (save-restriction
6913       (widen)
6914       (isearch-forward regexp-p))))
6915
6916 (defun gnus-summary-search-article-forward (regexp &optional backward)
6917   "Search for an article containing REGEXP forward.
6918 If BACKWARD, search backward instead."
6919   (interactive
6920    (list (read-string
6921           (format "Search article %s (regexp%s): "
6922                   (if current-prefix-arg "backward" "forward")
6923                   (if gnus-last-search-regexp
6924                       (concat ", default " gnus-last-search-regexp)
6925                     "")))
6926          current-prefix-arg))
6927   (if (string-equal regexp "")
6928       (setq regexp (or gnus-last-search-regexp ""))
6929     (setq gnus-last-search-regexp regexp))
6930   (if (gnus-summary-search-article regexp backward)
6931       (gnus-summary-show-thread)
6932     (error "Search failed: \"%s\"" regexp)))
6933
6934 (defun gnus-summary-search-article-backward (regexp)
6935   "Search for an article containing REGEXP backward."
6936   (interactive
6937    (list (read-string
6938           (format "Search article backward (regexp%s): "
6939                   (if gnus-last-search-regexp
6940                       (concat ", default " gnus-last-search-regexp)
6941                     "")))))
6942   (gnus-summary-search-article-forward regexp 'backward))
6943
6944 (defun gnus-summary-search-article (regexp &optional backward)
6945   "Search for an article containing REGEXP.
6946 Optional argument BACKWARD means do search for backward.
6947 `gnus-select-article-hook' is not called during the search."
6948   ;; We have to require this here to make sure that the following
6949   ;; dynamic binding isn't shadowed by autoloading.
6950   (require 'gnus-async)
6951   (require 'gnus-art)
6952   (let ((gnus-select-article-hook nil)  ;Disable hook.
6953         (gnus-article-prepare-hook nil)
6954         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6955         (gnus-use-article-prefetch nil)
6956         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6957         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6958         (sum (current-buffer))
6959         (gnus-display-mime-function nil)
6960         (found nil)
6961         point)
6962     (gnus-save-hidden-threads
6963       (gnus-summary-select-article)
6964       (set-buffer gnus-article-buffer)
6965       (when backward
6966         (forward-line -1))
6967       (while (not found)
6968         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6969         (if (if backward
6970                 (re-search-backward regexp nil t)
6971               (re-search-forward regexp nil t))
6972             ;; We found the regexp.
6973             (progn
6974               (setq found 'found)
6975               (beginning-of-line)
6976               (set-window-start
6977                (get-buffer-window (current-buffer))
6978                (point))
6979               (forward-line 1)
6980               (set-window-point
6981                (get-buffer-window (current-buffer))
6982                (point))
6983               (set-buffer sum)
6984               (setq point (point)))
6985           ;; We didn't find it, so we go to the next article.
6986           (set-buffer sum)
6987           (setq found 'not)
6988           (while (eq found 'not)
6989             (if (not (if backward (gnus-summary-find-prev)
6990                        (gnus-summary-find-next)))
6991                 ;; No more articles.
6992                 (setq found t)
6993               ;; Select the next article and adjust point.
6994               (unless (gnus-summary-article-sparse-p
6995                        (gnus-summary-article-number))
6996                 (setq found nil)
6997                 (gnus-summary-select-article)
6998                 (set-buffer gnus-article-buffer)
6999                 (widen)
7000                 (goto-char (if backward (point-max) (point-min))))))))
7001       (gnus-message 7 ""))
7002     ;; Return whether we found the regexp.
7003     (when (eq found 'found)
7004       (goto-char point)
7005       (gnus-summary-show-thread)
7006       (gnus-summary-goto-subject gnus-current-article)
7007       (gnus-summary-position-point)
7008       t)))
7009
7010 (defun gnus-summary-find-matching (header regexp &optional backward unread
7011                                           not-case-fold)
7012   "Return a list of all articles that match REGEXP on HEADER.
7013 The search stars on the current article and goes forwards unless
7014 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7015 If UNREAD is non-nil, only unread articles will
7016 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7017 in the comparisons."
7018   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7019                 (gnus-data-find-list
7020                  (gnus-summary-article-number) (gnus-data-list backward))))
7021         (case-fold-search (not not-case-fold))
7022         articles d func)
7023     (if (consp header)
7024         (if (eq (car header) 'extra)
7025             (setq func
7026                   `(lambda (h)
7027                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7028                          "")))
7029           (error "%s is an invalid header" header))
7030       (unless (fboundp (intern (concat "mail-header-" header)))
7031         (error "%s is not a valid header" header))
7032       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7033     (while data
7034       (setq d (car data))
7035       (and (or (not unread)             ; We want all articles...
7036                (gnus-data-unread-p d))  ; Or just unreads.
7037            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7038            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7039            (push (gnus-data-number d) articles)) ; Success!
7040       (setq data (cdr data)))
7041     (nreverse articles)))
7042
7043 (defun gnus-summary-execute-command (header regexp command &optional backward)
7044   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7045 If HEADER is an empty string (or nil), the match is done on the entire
7046 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7047   (interactive
7048    (list (let ((completion-ignore-case t))
7049            (completing-read
7050             "Header name: "
7051             (mapcar (lambda (string) (list string))
7052                     '("Number" "Subject" "From" "Lines" "Date"
7053                       "Message-ID" "Xref" "References" "Body"))
7054             nil 'require-match))
7055          (read-string "Regexp: ")
7056          (read-key-sequence "Command: ")
7057          current-prefix-arg))
7058   (when (equal header "Body")
7059     (setq header ""))
7060   ;; Hidden thread subtrees must be searched as well.
7061   (gnus-summary-show-all-threads)
7062   ;; We don't want to change current point nor window configuration.
7063   (save-excursion
7064     (save-window-excursion
7065       (gnus-message 6 "Executing %s..." (key-description command))
7066       ;; We'd like to execute COMMAND interactively so as to give arguments.
7067       (gnus-execute header regexp
7068                     `(call-interactively ',(key-binding command))
7069                     backward)
7070       (gnus-message 6 "Executing %s...done" (key-description command)))))
7071
7072 (defun gnus-summary-beginning-of-article ()
7073   "Scroll the article back to the beginning."
7074   (interactive)
7075   (gnus-summary-select-article)
7076   (gnus-configure-windows 'article)
7077   (gnus-eval-in-buffer-window gnus-article-buffer
7078     (widen)
7079     (goto-char (point-min))
7080     (when gnus-page-broken
7081       (gnus-narrow-to-page))))
7082
7083 (defun gnus-summary-end-of-article ()
7084   "Scroll to the end of the article."
7085   (interactive)
7086   (gnus-summary-select-article)
7087   (gnus-configure-windows 'article)
7088   (gnus-eval-in-buffer-window gnus-article-buffer
7089     (widen)
7090     (goto-char (point-max))
7091     (recenter -3)
7092     (when gnus-page-broken
7093       (gnus-narrow-to-page))))
7094
7095 (defun gnus-summary-print-article (&optional filename n)
7096   "Generate and print a PostScript image of the N next (mail) articles.
7097
7098 If N is negative, print the N previous articles.  If N is nil and articles
7099 have been marked with the process mark, print these instead.
7100
7101 If the optional first argument FILENAME is nil, send the image to the
7102 printer.  If FILENAME is a string, save the PostScript image in a file with
7103 that name.  If FILENAME is a number, prompt the user for the name of the file
7104 to save in."
7105   (interactive (list (ps-print-preprint current-prefix-arg)
7106                      current-prefix-arg))
7107   (dolist (article (gnus-summary-work-articles n))
7108     (gnus-summary-select-article nil nil 'pseudo article)
7109     (gnus-eval-in-buffer-window gnus-article-buffer
7110       (let ((buffer (generate-new-buffer " *print*")))
7111         (unwind-protect
7112             (progn
7113               (copy-to-buffer buffer (point-min) (point-max))
7114               (set-buffer buffer)
7115               (gnus-article-delete-invisible-text)
7116               (let ((ps-left-header
7117                      (list
7118                       (concat "("
7119                               (mail-header-subject gnus-current-headers) ")")
7120                       (concat "("
7121                               (mail-header-from gnus-current-headers) ")")))
7122                     (ps-right-header
7123                      (list
7124                       "/pagenumberstring load"
7125                       (concat "("
7126                               (mail-header-date gnus-current-headers) ")"))))
7127                 (gnus-run-hooks 'gnus-ps-print-hook)
7128                 (save-excursion
7129                   (ps-print-buffer-with-faces filename))))
7130           (kill-buffer buffer))))))
7131
7132 (defun gnus-summary-show-article (&optional arg)
7133   "Force re-fetching of the current article.
7134 If ARG (the prefix) is a number, show the article with the charset 
7135 defined in `gnus-summary-show-article-charset-alist', or the charset
7136 inputed.
7137 If ARG (the prefix) is non-nil and not a number, show the raw article 
7138 without any article massaging functions being run."
7139   (interactive "P")
7140   (cond 
7141    ((numberp arg)
7142     (let ((gnus-newsgroup-charset 
7143            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7144                (read-coding-system "Charset: ")))
7145           (gnus-newsgroup-ignored-charsets 'gnus-all))
7146       (gnus-summary-select-article nil 'force)))
7147    ((not arg)
7148     ;; Select the article the normal way.
7149     (gnus-summary-select-article nil 'force))
7150    (t
7151     ;; We have to require this here to make sure that the following
7152     ;; dynamic binding isn't shadowed by autoloading.
7153     (require 'gnus-async)
7154     (require 'gnus-art)
7155     ;; Bind the article treatment functions to nil.
7156     (let ((gnus-have-all-headers t)
7157           gnus-article-prepare-hook
7158           gnus-article-decode-hook
7159           gnus-display-mime-function
7160           gnus-break-pages)
7161       ;; Destroy any MIME parts.
7162       (when (gnus-buffer-live-p gnus-article-buffer)
7163         (save-excursion
7164           (set-buffer gnus-article-buffer)
7165           (mm-destroy-parts gnus-article-mime-handles)))
7166       (gnus-summary-select-article nil 'force))))
7167   (gnus-summary-goto-subject gnus-current-article)
7168   (gnus-summary-position-point))
7169
7170 (defun gnus-summary-verbose-headers (&optional arg)
7171   "Toggle permanent full header display.
7172 If ARG is a positive number, turn header display on.
7173 If ARG is a negative number, turn header display off."
7174   (interactive "P")
7175   (setq gnus-show-all-headers
7176         (cond ((or (not (numberp arg))
7177                    (zerop arg))
7178                (not gnus-show-all-headers))
7179               ((natnump arg)
7180                t)))
7181   (gnus-summary-show-article))
7182
7183 (defun gnus-summary-toggle-header (&optional arg)
7184   "Show the headers if they are hidden, or hide them if they are shown.
7185 If ARG is a positive number, show the entire header.
7186 If ARG is a negative number, hide the unwanted header lines."
7187   (interactive "P")
7188   (save-excursion
7189     (set-buffer gnus-article-buffer)
7190     (save-restriction
7191       (let* ((buffer-read-only nil)
7192              (inhibit-point-motion-hooks t)
7193              hidden e)
7194         (setq hidden
7195               (if (numberp arg)
7196                   (>= arg 0)
7197                 (save-restriction 
7198                   (article-narrow-to-head)
7199                   (gnus-article-hidden-text-p 'headers))))
7200         (goto-char (point-min))
7201         (when (search-forward "\n\n" nil t)
7202           (delete-region (point-min) (1- (point))))
7203         (goto-char (point-min))
7204         (save-excursion
7205           (set-buffer gnus-original-article-buffer)
7206           (goto-char (point-min))
7207           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7208         (insert-buffer-substring gnus-original-article-buffer 1 e)
7209         (save-restriction
7210           (narrow-to-region (point-min) (point))
7211           (article-decode-encoded-words)
7212           (if  hidden
7213               (let ((gnus-treat-hide-headers nil)
7214                     (gnus-treat-hide-boring-headers nil))
7215                 (gnus-treat-article 'head))
7216             (gnus-treat-article 'head)))))))
7217
7218 (defun gnus-summary-show-all-headers ()
7219   "Make all header lines visible."
7220   (interactive)
7221   (gnus-article-show-all-headers))
7222
7223 (defun gnus-summary-caesar-message (&optional arg)
7224   "Caesar rotate the current article by 13.
7225 The numerical prefix specifies how many places to rotate each letter
7226 forward."
7227   (interactive "P")
7228   (gnus-summary-select-article)
7229   (let ((mail-header-separator ""))
7230     (gnus-eval-in-buffer-window gnus-article-buffer
7231       (save-restriction
7232         (widen)
7233         (let ((start (window-start))
7234               buffer-read-only)
7235           (message-caesar-buffer-body arg)
7236           (set-window-start (get-buffer-window (current-buffer)) start))))))
7237
7238 (defun gnus-summary-stop-page-breaking ()
7239   "Stop page breaking in the current article."
7240   (interactive)
7241   (gnus-summary-select-article)
7242   (gnus-eval-in-buffer-window gnus-article-buffer
7243     (widen)
7244     (when (gnus-visual-p 'page-marker)
7245       (let ((buffer-read-only nil))
7246         (gnus-remove-text-with-property 'gnus-prev)
7247         (gnus-remove-text-with-property 'gnus-next))
7248       (setq gnus-page-broken nil))))
7249
7250 (defun gnus-summary-move-article (&optional n to-newsgroup
7251                                             select-method action)
7252   "Move the current article to a different newsgroup.
7253 If N is a positive number, move the N next articles.
7254 If N is a negative number, move the N previous articles.
7255 If N is nil and any articles have been marked with the process mark,
7256 move those articles instead.
7257 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7258 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7259 re-spool using this method.
7260
7261 For this function to work, both the current newsgroup and the
7262 newsgroup that you want to move to have to support the `request-move'
7263 and `request-accept' functions."
7264   (interactive "P")
7265   (unless action
7266     (setq action 'move))
7267   ;; Disable marking as read.
7268   (let (gnus-mark-article-hook)
7269     (save-window-excursion
7270       (gnus-summary-select-article)))
7271   ;; Check whether the source group supports the required functions.
7272   (cond ((and (eq action 'move)
7273               (not (gnus-check-backend-function
7274                     'request-move-article gnus-newsgroup-name)))
7275          (error "The current group does not support article moving"))
7276         ((and (eq action 'crosspost)
7277               (not (gnus-check-backend-function
7278                     'request-replace-article gnus-newsgroup-name)))
7279          (error "The current group does not support article editing")))
7280   (let ((articles (gnus-summary-work-articles n))
7281         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7282         (names '((move "Move" "Moving")
7283                  (copy "Copy" "Copying")
7284                  (crosspost "Crosspost" "Crossposting")))
7285         (copy-buf (save-excursion
7286                     (nnheader-set-temp-buffer " *copy article*")))
7287         art-group to-method new-xref article to-groups)
7288     (unless (assq action names)
7289       (error "Unknown action %s" action))
7290     ;; Read the newsgroup name.
7291     (when (and (not to-newsgroup)
7292                (not select-method))
7293       (setq to-newsgroup
7294             (gnus-read-move-group-name
7295              (cadr (assq action names))
7296              (symbol-value (intern (format "gnus-current-%s-group" action)))
7297              articles prefix))
7298       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7299     (setq to-method (or select-method
7300                         (gnus-group-name-to-method to-newsgroup)))
7301     ;; Check the method we are to move this article to...
7302     (unless (gnus-check-backend-function
7303              'request-accept-article (car to-method))
7304       (error "%s does not support article copying" (car to-method)))
7305     (unless (gnus-check-server to-method)
7306       (error "Can't open server %s" (car to-method)))
7307     (gnus-message 6 "%s to %s: %s..."
7308                   (caddr (assq action names))
7309                   (or (car select-method) to-newsgroup) articles)
7310     (while articles
7311       (setq article (pop articles))
7312       (setq
7313        art-group
7314        (cond
7315         ;; Move the article.
7316         ((eq action 'move)
7317          ;; Remove this article from future suppression.
7318          (gnus-dup-unsuppress-article article)
7319          (gnus-request-move-article
7320           article                       ; Article to move
7321           gnus-newsgroup-name           ; From newsgroup
7322           (nth 1 (gnus-find-method-for-group
7323                   gnus-newsgroup-name)) ; Server
7324           (list 'gnus-request-accept-article
7325                 to-newsgroup (list 'quote select-method)
7326                 (not articles) t)               ; Accept form
7327           (not articles)))              ; Only save nov last time
7328         ;; Copy the article.
7329         ((eq action 'copy)
7330          (save-excursion
7331            (set-buffer copy-buf)
7332            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7333              (gnus-request-accept-article
7334               to-newsgroup select-method (not articles) t))))
7335         ;; Crosspost the article.
7336         ((eq action 'crosspost)
7337          (let ((xref (message-tokenize-header
7338                       (mail-header-xref (gnus-summary-article-header article))
7339                       " ")))
7340            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7341                                   ":" article))
7342            (unless xref
7343              (setq xref (list (system-name))))
7344            (setq new-xref
7345                  (concat
7346                   (mapconcat 'identity
7347                              (delete "Xref:" (delete new-xref xref))
7348                              " ")
7349                   " " new-xref))
7350            (save-excursion
7351              (set-buffer copy-buf)
7352              ;; First put the article in the destination group.
7353              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7354              (when (consp (setq art-group
7355                                 (gnus-request-accept-article
7356                                  to-newsgroup select-method (not articles))))
7357                (setq new-xref (concat new-xref " " (car art-group)
7358                                       ":" (cdr art-group)))
7359                ;; Now we have the new Xrefs header, so we insert
7360                ;; it and replace the new article.
7361                (nnheader-replace-header "Xref" new-xref)
7362                (gnus-request-replace-article
7363                 (cdr art-group) to-newsgroup (current-buffer))
7364                art-group))))))
7365       (cond
7366        ((not art-group)
7367        (gnus-message 1 "Couldn't %s article %s: %s"
7368                      (cadr (assq action names)) article
7369                      (nnheader-get-report (car to-method))))
7370        ((and (eq art-group 'junk)
7371              (eq action 'move))
7372         (gnus-summary-mark-article article gnus-canceled-mark)
7373         (gnus-message 4 "Deleted article %s" article))
7374        (t
7375         (let* ((pto-group (gnus-group-prefixed-name
7376                            (car art-group) to-method))
7377                (entry
7378                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7379                (info (nth 2 entry))
7380                (to-group (gnus-info-group info))
7381                to-marks)
7382           ;; Update the group that has been moved to.
7383           (when (and info
7384                      (memq action '(move copy)))
7385             (unless (member to-group to-groups)
7386               (push to-group to-groups))
7387
7388             (unless (memq article gnus-newsgroup-unreads)
7389               (push 'read to-marks)
7390               (gnus-info-set-read
7391                info (gnus-add-to-range (gnus-info-read info)
7392                                        (list (cdr art-group)))))
7393
7394             ;; Copy any marks over to the new group.
7395             (let ((marks gnus-article-mark-lists)
7396                   (to-article (cdr art-group)))
7397
7398               ;; See whether the article is to be put in the cache.
7399               (when gnus-use-cache
7400                 (gnus-cache-possibly-enter-article
7401                  to-group to-article
7402                  (memq article gnus-newsgroup-marked)
7403                  (memq article gnus-newsgroup-dormant)
7404                  (memq article gnus-newsgroup-unreads)))
7405
7406               (when (and (equal to-group gnus-newsgroup-name)
7407                          (not (memq article gnus-newsgroup-unreads)))
7408                 ;; Mark this article as read in this group.
7409                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7410                 (setcdr (gnus-active to-group) to-article)
7411                 (setcdr gnus-newsgroup-active to-article))
7412
7413               (while marks
7414                 (when (memq article (symbol-value
7415                                      (intern (format "gnus-newsgroup-%s"
7416                                                      (caar marks)))))
7417                   (push (cdar marks) to-marks)
7418                   ;; If the other group is the same as this group,
7419                   ;; then we have to add the mark to the list.
7420                   (when (equal to-group gnus-newsgroup-name)
7421                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7422                          (cons to-article
7423                                (symbol-value
7424                                 (intern (format "gnus-newsgroup-%s"
7425                                                 (caar marks)))))))
7426                   ;; Copy the marks to other group.
7427                   (gnus-add-marked-articles
7428                    to-group (cdar marks) (list to-article) info))
7429                 (setq marks (cdr marks)))
7430
7431               (gnus-request-set-mark to-group (list (list (list to-article)
7432                                                           'set
7433                                                           to-marks)))
7434
7435               (gnus-dribble-enter
7436                (concat "(gnus-group-set-info '"
7437                        (gnus-prin1-to-string (gnus-get-info to-group))
7438                        ")"))))
7439
7440           ;; Update the Xref header in this article to point to
7441           ;; the new crossposted article we have just created.
7442           (when (eq action 'crosspost)
7443             (save-excursion
7444               (set-buffer copy-buf)
7445               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7446               (nnheader-replace-header "Xref" new-xref)
7447               (gnus-request-replace-article
7448                article gnus-newsgroup-name (current-buffer)))))
7449
7450         ;;;!!!Why is this necessary?
7451         (set-buffer gnus-summary-buffer)
7452
7453         (gnus-summary-goto-subject article)
7454         (when (eq action 'move)
7455           (gnus-summary-mark-article article gnus-canceled-mark))))
7456       (gnus-summary-remove-process-mark article))
7457     ;; Re-activate all groups that have been moved to.
7458     (while to-groups
7459       (save-excursion
7460         (set-buffer gnus-group-buffer)
7461         (when (gnus-group-goto-group (car to-groups) t)
7462           (gnus-group-get-new-news-this-group 1 t))
7463         (pop to-groups)))
7464
7465     (gnus-kill-buffer copy-buf)
7466     (gnus-summary-position-point)
7467     (gnus-set-mode-line 'summary)))
7468
7469 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7470   "Move the current article to a different newsgroup.
7471 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7472 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7473 re-spool using this method."
7474   (interactive "P")
7475   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7476
7477 (defun gnus-summary-crosspost-article (&optional n)
7478   "Crosspost the current article to some other group."
7479   (interactive "P")
7480   (gnus-summary-move-article n nil nil 'crosspost))
7481
7482 (defcustom gnus-summary-respool-default-method nil
7483   "Default method for respooling an article.
7484 If nil, use to the current newsgroup method."
7485   :type '(choice (gnus-select-method :value (nnml ""))
7486                  (const nil))
7487   :group 'gnus-summary-mail)
7488
7489 (defun gnus-summary-respool-article (&optional n method)
7490   "Respool the current article.
7491 The article will be squeezed through the mail spooling process again,
7492 which means that it will be put in some mail newsgroup or other
7493 depending on `nnmail-split-methods'.
7494 If N is a positive number, respool the N next articles.
7495 If N is a negative number, respool the N previous articles.
7496 If N is nil and any articles have been marked with the process mark,
7497 respool those articles instead.
7498
7499 Respooling can be done both from mail groups and \"real\" newsgroups.
7500 In the former case, the articles in question will be moved from the
7501 current group into whatever groups they are destined to.  In the
7502 latter case, they will be copied into the relevant groups."
7503   (interactive
7504    (list current-prefix-arg
7505          (let* ((methods (gnus-methods-using 'respool))
7506                 (methname
7507                  (symbol-name (or gnus-summary-respool-default-method
7508                                   (car (gnus-find-method-for-group
7509                                         gnus-newsgroup-name)))))
7510                 (method
7511                  (gnus-completing-read
7512                   methname "What backend do you want to use when respooling?"
7513                   methods nil t nil 'gnus-mail-method-history))
7514                 ms)
7515            (cond
7516             ((zerop (length (setq ms (gnus-servers-using-backend
7517                                       (intern method)))))
7518              (list (intern method) ""))
7519             ((= 1 (length ms))
7520              (car ms))
7521             (t
7522              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7523                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7524                            ms-alist))))))))
7525   (unless method
7526     (error "No method given for respooling"))
7527   (if (assoc (symbol-name
7528               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7529              (gnus-methods-using 'respool))
7530       (gnus-summary-move-article n nil method)
7531     (gnus-summary-copy-article n nil method)))
7532
7533 (defun gnus-summary-import-article (file)
7534   "Import an arbitrary file into a mail newsgroup."
7535   (interactive "fImport file: ")
7536   (let ((group gnus-newsgroup-name)
7537         (now (current-time))
7538         atts lines)
7539     (unless (gnus-check-backend-function 'request-accept-article group)
7540       (error "%s does not support article importing" group))
7541     (or (file-readable-p file)
7542         (not (file-regular-p file))
7543         (error "Can't read %s" file))
7544     (save-excursion
7545       (set-buffer (gnus-get-buffer-create " *import file*"))
7546       (erase-buffer)
7547       (insert-file-contents file)
7548       (goto-char (point-min))
7549       (unless (nnheader-article-p)
7550         ;; This doesn't look like an article, so we fudge some headers.
7551         (setq atts (file-attributes file)
7552               lines (count-lines (point-min) (point-max)))
7553         (insert "From: " (read-string "From: ") "\n"
7554                 "Subject: " (read-string "Subject: ") "\n"
7555                 "Date: " (message-make-date (nth 5 atts))
7556                 "\n"
7557                 "Message-ID: " (message-make-message-id) "\n"
7558                 "Lines: " (int-to-string lines) "\n"
7559                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7560       (gnus-request-accept-article group nil t)
7561       (kill-buffer (current-buffer)))))
7562
7563 (defun gnus-summary-article-posted-p ()
7564   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7565 This will be the case if the article has both been mailed and posted."
7566   (interactive)
7567   (let ((id (mail-header-references (gnus-summary-article-header)))
7568         (gnus-override-method
7569          (or gnus-refer-article-method gnus-select-method)))
7570     (if (gnus-request-head id "")
7571         (gnus-message 2 "The current message was found on %s"
7572                       gnus-override-method)
7573       (gnus-message 2 "The current message couldn't be found on %s"
7574                     gnus-override-method)
7575       nil)))
7576
7577 (defun gnus-summary-expire-articles (&optional now)
7578   "Expire all articles that are marked as expirable in the current group."
7579   (interactive)
7580   (when (gnus-check-backend-function
7581          'request-expire-articles gnus-newsgroup-name)
7582     ;; This backend supports expiry.
7583     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7584            (expirable (if total
7585                           (progn
7586                             ;; We need to update the info for
7587                             ;; this group for `gnus-list-of-read-articles'
7588                             ;; to give us the right answer.
7589                             (gnus-run-hooks 'gnus-exit-group-hook)
7590                             (gnus-summary-update-info)
7591                             (gnus-list-of-read-articles gnus-newsgroup-name))
7592                         (setq gnus-newsgroup-expirable
7593                               (sort gnus-newsgroup-expirable '<))))
7594            (expiry-wait (if now 'immediate
7595                           (gnus-group-find-parameter
7596                            gnus-newsgroup-name 'expiry-wait)))
7597            es)
7598       (when expirable
7599         ;; There are expirable articles in this group, so we run them
7600         ;; through the expiry process.
7601         (gnus-message 6 "Expiring articles...")
7602         (unless (gnus-check-group gnus-newsgroup-name)
7603           (error "Can't open server for %s" gnus-newsgroup-name))
7604         ;; The list of articles that weren't expired is returned.
7605         (save-excursion
7606           (if expiry-wait
7607               (let ((nnmail-expiry-wait-function nil)
7608                     (nnmail-expiry-wait expiry-wait))
7609                 (setq es (gnus-request-expire-articles
7610                           expirable gnus-newsgroup-name)))
7611             (setq es (gnus-request-expire-articles
7612                       expirable gnus-newsgroup-name))))
7613         (unless total
7614           (setq gnus-newsgroup-expirable es))
7615         ;; We go through the old list of expirable, and mark all
7616         ;; really expired articles as nonexistent.
7617         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7618           (let ((gnus-use-cache nil))
7619             (while expirable
7620               (unless (memq (car expirable) es)
7621                 (when (gnus-data-find (car expirable))
7622                   (gnus-summary-mark-article
7623                    (car expirable) gnus-canceled-mark)))
7624               (setq expirable (cdr expirable)))))
7625         (gnus-message 6 "Expiring articles...done")))))
7626
7627 (defun gnus-summary-expire-articles-now ()
7628   "Expunge all expirable articles in the current group.
7629 This means that *all* articles that are marked as expirable will be
7630 deleted forever, right now."
7631   (interactive)
7632   (or gnus-expert-user
7633       (gnus-yes-or-no-p
7634        "Are you really, really, really sure you want to delete all these messages? ")
7635       (error "Phew!"))
7636   (gnus-summary-expire-articles t))
7637
7638 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7639 (defun gnus-summary-delete-article (&optional n)
7640   "Delete the N next (mail) articles.
7641 This command actually deletes articles.  This is not a marking
7642 command.  The article will disappear forever from your life, never to
7643 return.
7644 If N is negative, delete backwards.
7645 If N is nil and articles have been marked with the process mark,
7646 delete these instead."
7647   (interactive "P")
7648   (unless (gnus-check-backend-function 'request-expire-articles
7649                                        gnus-newsgroup-name)
7650     (error "The current newsgroup does not support article deletion"))
7651   ;; Compute the list of articles to delete.
7652   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7653         not-deleted)
7654     (if (and gnus-novice-user
7655              (not (gnus-yes-or-no-p
7656                    (format "Do you really want to delete %s forever? "
7657                            (if (> (length articles) 1)
7658                                (format "these %s articles" (length articles))
7659                              "this article")))))
7660         ()
7661       ;; Delete the articles.
7662       (setq not-deleted (gnus-request-expire-articles
7663                          articles gnus-newsgroup-name 'force))
7664       (while articles
7665         (gnus-summary-remove-process-mark (car articles))
7666         ;; The backend might not have been able to delete the article
7667         ;; after all.
7668         (unless (memq (car articles) not-deleted)
7669           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7670         (setq articles (cdr articles)))
7671       (when not-deleted
7672         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7673     (gnus-summary-position-point)
7674     (gnus-set-mode-line 'summary)
7675     not-deleted))
7676
7677 (defun gnus-summary-edit-article (&optional force)
7678   "Edit the current article.
7679 This will have permanent effect only in mail groups.
7680 If FORCE is non-nil, allow editing of articles even in read-only
7681 groups."
7682   (interactive "P")
7683   (save-excursion
7684     (set-buffer gnus-summary-buffer)
7685     (let ((mail-parse-charset gnus-newsgroup-charset)
7686           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7687       (gnus-set-global-variables)
7688       (when (and (not force)
7689                  (gnus-group-read-only-p))
7690         (error "The current newsgroup does not support article editing"))
7691       (gnus-summary-show-article t)
7692       (gnus-article-edit-article
7693        'mime-to-mml
7694        `(lambda (no-highlight)
7695           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7696                 (mail-parse-ignored-charsets 
7697                  ',gnus-newsgroup-ignored-charsets))
7698             (mml-to-mime)
7699             (gnus-summary-edit-article-done
7700              ,(or (mail-header-references gnus-current-headers) "")
7701              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7702
7703 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7704
7705 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7706                                                  no-highlight)
7707   "Make edits to the current article permanent."
7708   (interactive)
7709   ;; Replace the article.
7710   (let ((buf (current-buffer)))
7711     (with-temp-buffer
7712       (insert-buffer-substring buf)
7713       (if (and (not read-only)
7714                (not (gnus-request-replace-article
7715                      (cdr gnus-article-current) (car gnus-article-current)
7716                      (current-buffer) t)))
7717           (error "Couldn't replace article")
7718         ;; Update the summary buffer.
7719         (if (and references
7720                  (equal (message-tokenize-header references " ")
7721                         (message-tokenize-header
7722                          (or (message-fetch-field "references") "") " ")))
7723             ;; We only have to update this line.
7724             (save-excursion
7725               (save-restriction
7726                 (message-narrow-to-head)
7727                 (let ((head (buffer-string))
7728                       header)
7729                   (with-temp-buffer
7730                     (insert (format "211 %d Article retrieved.\n"
7731                                     (cdr gnus-article-current)))
7732                     (insert head)
7733                     (insert ".\n")
7734                     (let ((nntp-server-buffer (current-buffer)))
7735                       (setq header (car (gnus-get-newsgroup-headers
7736                                          (save-excursion
7737                                            (set-buffer gnus-summary-buffer)
7738                                            gnus-newsgroup-dependencies)
7739                                          t))))
7740                     (save-excursion
7741                       (set-buffer gnus-summary-buffer)
7742                       (gnus-data-set-header
7743                        (gnus-data-find (cdr gnus-article-current))
7744                        header)
7745                       (gnus-summary-update-article-line
7746                        (cdr gnus-article-current) header))))))
7747           ;; Update threads.
7748           (set-buffer (or buffer gnus-summary-buffer))
7749           (gnus-summary-update-article (cdr gnus-article-current)))
7750         ;; Prettify the article buffer again.
7751         (unless no-highlight
7752           (save-excursion
7753             (set-buffer gnus-article-buffer)
7754             ;;;!!! Fix this -- article should be rehighlighted.
7755             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7756             (set-buffer gnus-original-article-buffer)
7757             (gnus-request-article
7758              (cdr gnus-article-current)
7759              (car gnus-article-current) (current-buffer))))
7760         ;; Prettify the summary buffer line.
7761         (when (gnus-visual-p 'summary-highlight 'highlight)
7762           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7763
7764 (defun gnus-summary-edit-wash (key)
7765   "Perform editing command KEY in the article buffer."
7766   (interactive
7767    (list
7768     (progn
7769       (message "%s" (concat (this-command-keys) "- "))
7770       (read-char))))
7771   (message "")
7772   (gnus-summary-edit-article)
7773   (execute-kbd-macro (concat (this-command-keys) key))
7774   (gnus-article-edit-done))
7775
7776 ;;; Respooling
7777
7778 (defun gnus-summary-respool-query (&optional silent trace)
7779   "Query where the respool algorithm would put this article."
7780   (interactive)
7781   (let (gnus-mark-article-hook)
7782     (gnus-summary-select-article)
7783     (save-excursion
7784       (set-buffer gnus-original-article-buffer)
7785       (save-restriction
7786         (message-narrow-to-head)
7787         (let ((groups (nnmail-article-group 'identity trace)))
7788           (unless silent
7789             (if groups
7790                 (message "This message would go to %s"
7791                          (mapconcat 'car groups ", "))
7792               (message "This message would go to no groups"))
7793             groups))))))
7794
7795 (defun gnus-summary-respool-trace ()
7796   "Trace where the respool algorithm would put this article.
7797 Display a buffer showing all fancy splitting patterns which matched."
7798   (interactive)
7799   (gnus-summary-respool-query nil t))
7800
7801 ;; Summary marking commands.
7802
7803 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7804   "Mark articles which has the same subject as read, and then select the next.
7805 If UNMARK is positive, remove any kind of mark.
7806 If UNMARK is negative, tick articles."
7807   (interactive "P")
7808   (when unmark
7809     (setq unmark (prefix-numeric-value unmark)))
7810   (let ((count
7811          (gnus-summary-mark-same-subject
7812           (gnus-summary-article-subject) unmark)))
7813     ;; Select next unread article.  If auto-select-same mode, should
7814     ;; select the first unread article.
7815     (gnus-summary-next-article t (and gnus-auto-select-same
7816                                       (gnus-summary-article-subject)))
7817     (gnus-message 7 "%d article%s marked as %s"
7818                   count (if (= count 1) " is" "s are")
7819                   (if unmark "unread" "read"))))
7820
7821 (defun gnus-summary-kill-same-subject (&optional unmark)
7822   "Mark articles which has the same subject as read.
7823 If UNMARK is positive, remove any kind of mark.
7824 If UNMARK is negative, tick articles."
7825   (interactive "P")
7826   (when unmark
7827     (setq unmark (prefix-numeric-value unmark)))
7828   (let ((count
7829          (gnus-summary-mark-same-subject
7830           (gnus-summary-article-subject) unmark)))
7831     ;; If marked as read, go to next unread subject.
7832     (when (null unmark)
7833       ;; Go to next unread subject.
7834       (gnus-summary-next-subject 1 t))
7835     (gnus-message 7 "%d articles are marked as %s"
7836                   count (if unmark "unread" "read"))))
7837
7838 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7839   "Mark articles with same SUBJECT as read, and return marked number.
7840 If optional argument UNMARK is positive, remove any kinds of marks.
7841 If optional argument UNMARK is negative, mark articles as unread instead."
7842   (let ((count 1))
7843     (save-excursion
7844       (cond
7845        ((null unmark)                   ; Mark as read.
7846         (while (and
7847                 (progn
7848                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7849                   (gnus-summary-show-thread) t)
7850                 (gnus-summary-find-subject subject))
7851           (setq count (1+ count))))
7852        ((> unmark 0)                    ; Tick.
7853         (while (and
7854                 (progn
7855                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7856                   (gnus-summary-show-thread) t)
7857                 (gnus-summary-find-subject subject))
7858           (setq count (1+ count))))
7859        (t                               ; Mark as unread.
7860         (while (and
7861                 (progn
7862                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7863                   (gnus-summary-show-thread) t)
7864                 (gnus-summary-find-subject subject))
7865           (setq count (1+ count)))))
7866       (gnus-set-mode-line 'summary)
7867       ;; Return the number of marked articles.
7868       count)))
7869
7870 (defun gnus-summary-mark-as-processable (n &optional unmark)
7871   "Set the process mark on the next N articles.
7872 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7873 the process mark instead.  The difference between N and the actual
7874 number of articles marked is returned."
7875   (interactive "p")
7876   (let ((backward (< n 0))
7877         (n (abs n)))
7878     (while (and
7879             (> n 0)
7880             (if unmark
7881                 (gnus-summary-remove-process-mark
7882                  (gnus-summary-article-number))
7883               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7884             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7885       (setq n (1- n)))
7886     (when (/= 0 n)
7887       (gnus-message 7 "No more articles"))
7888     (gnus-summary-recenter)
7889     (gnus-summary-position-point)
7890     n))
7891
7892 (defun gnus-summary-unmark-as-processable (n)
7893   "Remove the process mark from the next N articles.
7894 If N is negative, unmark backward instead.  The difference between N and
7895 the actual number of articles unmarked is returned."
7896   (interactive "p")
7897   (gnus-summary-mark-as-processable n t))
7898
7899 (defun gnus-summary-unmark-all-processable ()
7900   "Remove the process mark from all articles."
7901   (interactive)
7902   (save-excursion
7903     (while gnus-newsgroup-processable
7904       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7905   (gnus-summary-position-point))
7906
7907 (defun gnus-summary-mark-as-expirable (n)
7908   "Mark N articles forward as expirable.
7909 If N is negative, mark backward instead.  The difference between N and
7910 the actual number of articles marked is returned."
7911   (interactive "p")
7912   (gnus-summary-mark-forward n gnus-expirable-mark))
7913
7914 (defun gnus-summary-mark-article-as-replied (article)
7915   "Mark ARTICLE replied and update the summary line."
7916   (push article gnus-newsgroup-replied)
7917   (let ((buffer-read-only nil))
7918     (when (gnus-summary-goto-subject article nil t)
7919       (gnus-summary-update-secondary-mark article))))
7920
7921 (defun gnus-summary-set-bookmark (article)
7922   "Set a bookmark in current article."
7923   (interactive (list (gnus-summary-article-number)))
7924   (when (or (not (get-buffer gnus-article-buffer))
7925             (not gnus-current-article)
7926             (not gnus-article-current)
7927             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7928     (error "No current article selected"))
7929   ;; Remove old bookmark, if one exists.
7930   (let ((old (assq article gnus-newsgroup-bookmarks)))
7931     (when old
7932       (setq gnus-newsgroup-bookmarks
7933             (delq old gnus-newsgroup-bookmarks))))
7934   ;; Set the new bookmark, which is on the form
7935   ;; (article-number . line-number-in-body).
7936   (push
7937    (cons article
7938          (save-excursion
7939            (set-buffer gnus-article-buffer)
7940            (count-lines
7941             (min (point)
7942                  (save-excursion
7943                    (goto-char (point-min))
7944                    (search-forward "\n\n" nil t)
7945                    (point)))
7946             (point))))
7947    gnus-newsgroup-bookmarks)
7948   (gnus-message 6 "A bookmark has been added to the current article."))
7949
7950 (defun gnus-summary-remove-bookmark (article)
7951   "Remove the bookmark from the current article."
7952   (interactive (list (gnus-summary-article-number)))
7953   ;; Remove old bookmark, if one exists.
7954   (let ((old (assq article gnus-newsgroup-bookmarks)))
7955     (if old
7956         (progn
7957           (setq gnus-newsgroup-bookmarks
7958                 (delq old gnus-newsgroup-bookmarks))
7959           (gnus-message 6 "Removed bookmark."))
7960       (gnus-message 6 "No bookmark in current article."))))
7961
7962 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7963 (defun gnus-summary-mark-as-dormant (n)
7964   "Mark N articles forward as dormant.
7965 If N is negative, mark backward instead.  The difference between N and
7966 the actual number of articles marked is returned."
7967   (interactive "p")
7968   (gnus-summary-mark-forward n gnus-dormant-mark))
7969
7970 (defun gnus-summary-set-process-mark (article)
7971   "Set the process mark on ARTICLE and update the summary line."
7972   (setq gnus-newsgroup-processable
7973         (cons article
7974               (delq article gnus-newsgroup-processable)))
7975   (when (gnus-summary-goto-subject article)
7976     (gnus-summary-show-thread)
7977     (gnus-summary-goto-subject article)
7978     (gnus-summary-update-secondary-mark article)))
7979
7980 (defun gnus-summary-remove-process-mark (article)
7981   "Remove the process mark from ARTICLE and update the summary line."
7982   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7983   (when (gnus-summary-goto-subject article)
7984     (gnus-summary-show-thread)
7985     (gnus-summary-goto-subject article)
7986     (gnus-summary-update-secondary-mark article)))
7987
7988 (defun gnus-summary-set-saved-mark (article)
7989   "Set the process mark on ARTICLE and update the summary line."
7990   (push article gnus-newsgroup-saved)
7991   (when (gnus-summary-goto-subject article)
7992     (gnus-summary-update-secondary-mark article)))
7993
7994 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7995   "Mark N articles as read forwards.
7996 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7997 The difference between N and the actual number of articles marked is
7998 returned."
7999   (interactive "p")
8000   (gnus-summary-show-thread)
8001   (let ((backward (< n 0))
8002         (gnus-summary-goto-unread
8003          (and gnus-summary-goto-unread
8004               (not (eq gnus-summary-goto-unread 'never))
8005               (not (memq mark (list gnus-unread-mark
8006                                     gnus-ticked-mark gnus-dormant-mark)))))
8007         (n (abs n))
8008         (mark (or mark gnus-del-mark)))
8009     (while (and (> n 0)
8010                 (gnus-summary-mark-article nil mark no-expire)
8011                 (zerop (gnus-summary-next-subject
8012                         (if backward -1 1)
8013                         (and gnus-summary-goto-unread
8014                              (not (eq gnus-summary-goto-unread 'never)))
8015                         t)))
8016       (setq n (1- n)))
8017     (when (/= 0 n)
8018       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8019     (gnus-summary-recenter)
8020     (gnus-summary-position-point)
8021     (gnus-set-mode-line 'summary)
8022     n))
8023
8024 (defun gnus-summary-mark-article-as-read (mark)
8025   "Mark the current article quickly as read with MARK."
8026   (let ((article (gnus-summary-article-number)))
8027     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8028     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8029     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8030     (push (cons article mark) gnus-newsgroup-reads)
8031     ;; Possibly remove from cache, if that is used.
8032     (when gnus-use-cache
8033       (gnus-cache-enter-remove-article article))
8034     ;; Allow the backend to change the mark.
8035     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8036     ;; Check for auto-expiry.
8037     (when (and gnus-newsgroup-auto-expire
8038                (memq mark gnus-auto-expirable-marks))
8039       (setq mark gnus-expirable-mark)
8040       ;; Let the backend know about the mark change.
8041       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8042       (push article gnus-newsgroup-expirable))
8043     ;; Set the mark in the buffer.
8044     (gnus-summary-update-mark mark 'unread)
8045     t))
8046
8047 (defun gnus-summary-mark-article-as-unread (mark)
8048   "Mark the current article quickly as unread with MARK."
8049   (let* ((article (gnus-summary-article-number))
8050          (old-mark (gnus-summary-article-mark article)))
8051     ;; Allow the backend to change the mark.
8052     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8053     (if (eq mark old-mark)
8054         t
8055       (if (<= article 0)
8056           (progn
8057             (gnus-error 1 "Can't mark negative article numbers")
8058             nil)
8059         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8060         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8061         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8062         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8063         (cond ((= mark gnus-ticked-mark)
8064                (push article gnus-newsgroup-marked))
8065               ((= mark gnus-dormant-mark)
8066                (push article gnus-newsgroup-dormant))
8067               (t
8068                (push article gnus-newsgroup-unreads)))
8069         (gnus-pull article gnus-newsgroup-reads)
8070
8071         ;; See whether the article is to be put in the cache.
8072         (and gnus-use-cache
8073              (vectorp (gnus-summary-article-header article))
8074              (save-excursion
8075                (gnus-cache-possibly-enter-article
8076                 gnus-newsgroup-name article
8077                 (= mark gnus-ticked-mark)
8078                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8079
8080         ;; Fix the mark.
8081         (gnus-summary-update-mark mark 'unread)
8082         t))))
8083
8084 (defun gnus-summary-mark-article (&optional article mark no-expire)
8085   "Mark ARTICLE with MARK.  MARK can be any character.
8086 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8087 `??' (dormant) and `?E' (expirable).
8088 If MARK is nil, then the default character `?r' is used.
8089 If ARTICLE is nil, then the article on the current line will be
8090 marked."
8091   ;; The mark might be a string.
8092   (when (stringp mark)
8093     (setq mark (aref mark 0)))
8094   ;; If no mark is given, then we check auto-expiring.
8095   (when (null mark)
8096     (setq mark gnus-del-mark))
8097   (when (and (not no-expire)
8098              gnus-newsgroup-auto-expire
8099              (memq mark gnus-auto-expirable-marks))
8100     (setq mark gnus-expirable-mark))
8101   (let ((article (or article (gnus-summary-article-number)))
8102         (old-mark (gnus-summary-article-mark article)))
8103     ;; Allow the backend to change the mark.
8104     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8105     (if (eq mark old-mark)
8106         t
8107       (unless article
8108         (error "No article on current line"))
8109       (if (not (if (or (= mark gnus-unread-mark)
8110                        (= mark gnus-ticked-mark)
8111                        (= mark gnus-dormant-mark))
8112                    (gnus-mark-article-as-unread article mark)
8113                  (gnus-mark-article-as-read article mark)))
8114           t
8115         ;; See whether the article is to be put in the cache.
8116         (and gnus-use-cache
8117              (not (= mark gnus-canceled-mark))
8118              (vectorp (gnus-summary-article-header article))
8119              (save-excursion
8120                (gnus-cache-possibly-enter-article
8121                 gnus-newsgroup-name article
8122                 (= mark gnus-ticked-mark)
8123                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8124
8125         (when (gnus-summary-goto-subject article nil t)
8126           (let ((buffer-read-only nil))
8127             (gnus-summary-show-thread)
8128             ;; Fix the mark.
8129             (gnus-summary-update-mark mark 'unread)
8130             t))))))
8131
8132 (defun gnus-summary-update-secondary-mark (article)
8133   "Update the secondary (read, process, cache) mark."
8134   (gnus-summary-update-mark
8135    (cond ((memq article gnus-newsgroup-processable)
8136           gnus-process-mark)
8137          ((memq article gnus-newsgroup-cached)
8138           gnus-cached-mark)
8139          ((memq article gnus-newsgroup-replied)
8140           gnus-replied-mark)
8141          ((memq article gnus-newsgroup-saved)
8142           gnus-saved-mark)
8143          (t gnus-unread-mark))
8144    'replied)
8145   (when (gnus-visual-p 'summary-highlight 'highlight)
8146     (gnus-run-hooks 'gnus-summary-update-hook))
8147   t)
8148
8149 (defun gnus-summary-update-mark (mark type)
8150   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8151         (buffer-read-only nil))
8152     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8153     (when forward
8154       (when (looking-at "\r")
8155         (incf forward))
8156       (when (<= (+ forward (point)) (point-max))
8157         ;; Go to the right position on the line.
8158         (goto-char (+ forward (point)))
8159         ;; Replace the old mark with the new mark.
8160         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8161         ;; Optionally update the marks by some user rule.
8162         (when (eq type 'unread)
8163           (gnus-data-set-mark
8164            (gnus-data-find (gnus-summary-article-number)) mark)
8165           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8166
8167 (defun gnus-mark-article-as-read (article &optional mark)
8168   "Enter ARTICLE in the pertinent lists and remove it from others."
8169   ;; Make the article expirable.
8170   (let ((mark (or mark gnus-del-mark)))
8171     (if (= mark gnus-expirable-mark)
8172         (push article gnus-newsgroup-expirable)
8173       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8174     ;; Remove from unread and marked lists.
8175     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8176     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8177     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8178     (push (cons article mark) gnus-newsgroup-reads)
8179     ;; Possibly remove from cache, if that is used.
8180     (when gnus-use-cache
8181       (gnus-cache-enter-remove-article article))
8182     t))
8183
8184 (defun gnus-mark-article-as-unread (article &optional mark)
8185   "Enter ARTICLE in the pertinent lists and remove it from others."
8186   (let ((mark (or mark gnus-ticked-mark)))
8187     (if (<= article 0)
8188         (progn
8189           (gnus-error 1 "Can't mark negative article numbers")
8190           nil)
8191       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8192             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8193             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8194             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8195
8196       ;; Unsuppress duplicates?
8197       (when gnus-suppress-duplicates
8198         (gnus-dup-unsuppress-article article))
8199
8200       (cond ((= mark gnus-ticked-mark)
8201              (push article gnus-newsgroup-marked))
8202             ((= mark gnus-dormant-mark)
8203              (push article gnus-newsgroup-dormant))
8204             (t
8205              (push article gnus-newsgroup-unreads)))
8206       (gnus-pull article gnus-newsgroup-reads)
8207       t)))
8208
8209 (defalias 'gnus-summary-mark-as-unread-forward
8210   'gnus-summary-tick-article-forward)
8211 (make-obsolete 'gnus-summary-mark-as-unread-forward
8212                'gnus-summary-tick-article-forward)
8213 (defun gnus-summary-tick-article-forward (n)
8214   "Tick N articles forwards.
8215 If N is negative, tick backwards instead.
8216 The difference between N and the number of articles ticked is returned."
8217   (interactive "p")
8218   (gnus-summary-mark-forward n gnus-ticked-mark))
8219
8220 (defalias 'gnus-summary-mark-as-unread-backward
8221   'gnus-summary-tick-article-backward)
8222 (make-obsolete 'gnus-summary-mark-as-unread-backward
8223                'gnus-summary-tick-article-backward)
8224 (defun gnus-summary-tick-article-backward (n)
8225   "Tick N articles backwards.
8226 The difference between N and the number of articles ticked is returned."
8227   (interactive "p")
8228   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8229
8230 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8231 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8232 (defun gnus-summary-tick-article (&optional article clear-mark)
8233   "Mark current article as unread.
8234 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8235 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8236   (interactive)
8237   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8238                                        gnus-ticked-mark)))
8239
8240 (defun gnus-summary-mark-as-read-forward (n)
8241   "Mark N articles as read forwards.
8242 If N is negative, mark backwards instead.
8243 The difference between N and the actual number of articles marked is
8244 returned."
8245   (interactive "p")
8246   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8247
8248 (defun gnus-summary-mark-as-read-backward (n)
8249   "Mark the N articles as read backwards.
8250 The difference between N and the actual number of articles marked is
8251 returned."
8252   (interactive "p")
8253   (gnus-summary-mark-forward
8254    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8255
8256 (defun gnus-summary-mark-as-read (&optional article mark)
8257   "Mark current article as read.
8258 ARTICLE specifies the article to be marked as read.
8259 MARK specifies a string to be inserted at the beginning of the line."
8260   (gnus-summary-mark-article article mark))
8261
8262 (defun gnus-summary-clear-mark-forward (n)
8263   "Clear marks from N articles forward.
8264 If N is negative, clear backward instead.
8265 The difference between N and the number of marks cleared is returned."
8266   (interactive "p")
8267   (gnus-summary-mark-forward n gnus-unread-mark))
8268
8269 (defun gnus-summary-clear-mark-backward (n)
8270   "Clear marks from N articles backward.
8271 The difference between N and the number of marks cleared is returned."
8272   (interactive "p")
8273   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8274
8275 (defun gnus-summary-mark-unread-as-read ()
8276   "Intended to be used by `gnus-summary-mark-article-hook'."
8277   (when (memq gnus-current-article gnus-newsgroup-unreads)
8278     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8279
8280 (defun gnus-summary-mark-read-and-unread-as-read ()
8281   "Intended to be used by `gnus-summary-mark-article-hook'."
8282   (let ((mark (gnus-summary-article-mark)))
8283     (when (or (gnus-unread-mark-p mark)
8284               (gnus-read-mark-p mark))
8285       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8286
8287 (defun gnus-summary-mark-region-as-read (point mark all)
8288   "Mark all unread articles between point and mark as read.
8289 If given a prefix, mark all articles between point and mark as read,
8290 even ticked and dormant ones."
8291   (interactive "r\nP")
8292   (save-excursion
8293     (let (article)
8294       (goto-char point)
8295       (beginning-of-line)
8296       (while (and
8297               (< (point) mark)
8298               (progn
8299                 (when (or all
8300                           (memq (setq article (gnus-summary-article-number))
8301                                 gnus-newsgroup-unreads))
8302                   (gnus-summary-mark-article article gnus-del-mark))
8303                 t)
8304               (gnus-summary-find-next))))))
8305
8306 (defun gnus-summary-mark-below (score mark)
8307   "Mark articles with score less than SCORE with MARK."
8308   (interactive "P\ncMark: ")
8309   (setq score (if score
8310                   (prefix-numeric-value score)
8311                 (or gnus-summary-default-score 0)))
8312   (save-excursion
8313     (set-buffer gnus-summary-buffer)
8314     (goto-char (point-min))
8315     (while
8316         (progn
8317           (and (< (gnus-summary-article-score) score)
8318                (gnus-summary-mark-article nil mark))
8319           (gnus-summary-find-next)))))
8320
8321 (defun gnus-summary-kill-below (&optional score)
8322   "Mark articles with score below SCORE as read."
8323   (interactive "P")
8324   (gnus-summary-mark-below score gnus-killed-mark))
8325
8326 (defun gnus-summary-clear-above (&optional score)
8327   "Clear all marks from articles with score above SCORE."
8328   (interactive "P")
8329   (gnus-summary-mark-above score gnus-unread-mark))
8330
8331 (defun gnus-summary-tick-above (&optional score)
8332   "Tick all articles with score above SCORE."
8333   (interactive "P")
8334   (gnus-summary-mark-above score gnus-ticked-mark))
8335
8336 (defun gnus-summary-mark-above (score mark)
8337   "Mark articles with score over SCORE with MARK."
8338   (interactive "P\ncMark: ")
8339   (setq score (if score
8340                   (prefix-numeric-value score)
8341                 (or gnus-summary-default-score 0)))
8342   (save-excursion
8343     (set-buffer gnus-summary-buffer)
8344     (goto-char (point-min))
8345     (while (and (progn
8346                   (when (> (gnus-summary-article-score) score)
8347                     (gnus-summary-mark-article nil mark))
8348                   t)
8349                 (gnus-summary-find-next)))))
8350
8351 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8352 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8353 (defun gnus-summary-limit-include-expunged (&optional no-error)
8354   "Display all the hidden articles that were expunged for low scores."
8355   (interactive)
8356   (let ((buffer-read-only nil))
8357     (let ((scored gnus-newsgroup-scored)
8358           headers h)
8359       (while scored
8360         (unless (gnus-summary-goto-subject (caar scored))
8361           (and (setq h (gnus-summary-article-header (caar scored)))
8362                (< (cdar scored) gnus-summary-expunge-below)
8363                (push h headers)))
8364         (setq scored (cdr scored)))
8365       (if (not headers)
8366           (when (not no-error)
8367             (error "No expunged articles hidden"))
8368         (goto-char (point-min))
8369         (gnus-summary-prepare-unthreaded (nreverse headers))
8370         (goto-char (point-min))
8371         (gnus-summary-position-point)
8372         t))))
8373
8374 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8375   "Mark all unread articles in this newsgroup as read.
8376 If prefix argument ALL is non-nil, ticked and dormant articles will
8377 also be marked as read.
8378 If QUIETLY is non-nil, no questions will be asked.
8379 If TO-HERE is non-nil, it should be a point in the buffer.  All
8380 articles before this point will be marked as read.
8381 Note that this function will only catch up the unread article
8382 in the current summary buffer limitation.
8383 The number of articles marked as read is returned."
8384   (interactive "P")
8385   (prog1
8386       (save-excursion
8387         (when (or quietly
8388                   (not gnus-interactive-catchup) ;Without confirmation?
8389                   gnus-expert-user
8390                   (gnus-y-or-n-p
8391                    (if all
8392                        "Mark absolutely all articles as read? "
8393                      "Mark all unread articles as read? ")))
8394           (if (and not-mark
8395                    (not gnus-newsgroup-adaptive)
8396                    (not gnus-newsgroup-auto-expire)
8397                    (not gnus-suppress-duplicates)
8398                    (or (not gnus-use-cache)
8399                        (eq gnus-use-cache 'passive)))
8400               (progn
8401                 (when all
8402                   (setq gnus-newsgroup-marked nil
8403                         gnus-newsgroup-dormant nil))
8404                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8405             ;; We actually mark all articles as canceled, which we
8406             ;; have to do when using auto-expiry or adaptive scoring.
8407             (gnus-summary-show-all-threads)
8408             (when (gnus-summary-first-subject (not all) t)
8409               (while (and
8410                       (if to-here (< (point) to-here) t)
8411                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8412                       (gnus-summary-find-next (not all) nil nil t))))
8413             (gnus-set-mode-line 'summary))
8414           t))
8415     (gnus-summary-position-point)))
8416
8417 (defun gnus-summary-catchup-to-here (&optional all)
8418   "Mark all unticked articles before the current one as read.
8419 If ALL is non-nil, also mark ticked and dormant articles as read."
8420   (interactive "P")
8421   (save-excursion
8422     (gnus-save-hidden-threads
8423       (let ((beg (point)))
8424         ;; We check that there are unread articles.
8425         (when (or all (gnus-summary-find-prev))
8426           (gnus-summary-catchup all t beg)))))
8427   (gnus-summary-position-point))
8428
8429 (defun gnus-summary-catchup-all (&optional quietly)
8430   "Mark all articles in this newsgroup as read."
8431   (interactive "P")
8432   (gnus-summary-catchup t quietly))
8433
8434 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8435   "Mark all unread articles in this group as read, then exit.
8436 If prefix argument ALL is non-nil, all articles are marked as read."
8437   (interactive "P")
8438   (when (gnus-summary-catchup all quietly nil 'fast)
8439     ;; Select next newsgroup or exit.
8440     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8441              (eq gnus-auto-select-next 'quietly))
8442         (gnus-summary-next-group nil)
8443       (gnus-summary-exit))))
8444
8445 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8446   "Mark all articles in this newsgroup as read, and then exit."
8447   (interactive "P")
8448   (gnus-summary-catchup-and-exit t quietly))
8449
8450 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8451   "Mark all articles in this group as read and select the next group.
8452 If given a prefix, mark all articles, unread as well as ticked, as
8453 read."
8454   (interactive "P")
8455   (save-excursion
8456     (gnus-summary-catchup all))
8457   (gnus-summary-next-group))
8458
8459 ;; Thread-based commands.
8460
8461 (defun gnus-summary-articles-in-thread (&optional article)
8462   "Return a list of all articles in the current thread.
8463 If ARTICLE is non-nil, return all articles in the thread that starts
8464 with that article."
8465   (let* ((article (or article (gnus-summary-article-number)))
8466          (data (gnus-data-find-list article))
8467          (top-level (gnus-data-level (car data)))
8468          (top-subject
8469           (cond ((null gnus-thread-operation-ignore-subject)
8470                  (gnus-simplify-subject-re
8471                   (mail-header-subject (gnus-data-header (car data)))))
8472                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8473                  (gnus-simplify-subject-fuzzy
8474                   (mail-header-subject (gnus-data-header (car data)))))
8475                 (t nil)))
8476          (end-point (save-excursion
8477                       (if (gnus-summary-go-to-next-thread)
8478                           (point) (point-max))))
8479          articles)
8480     (while (and data
8481                 (< (gnus-data-pos (car data)) end-point))
8482       (when (or (not top-subject)
8483                 (string= top-subject
8484                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8485                              (gnus-simplify-subject-fuzzy
8486                               (mail-header-subject
8487                                (gnus-data-header (car data))))
8488                            (gnus-simplify-subject-re
8489                             (mail-header-subject
8490                              (gnus-data-header (car data)))))))
8491         (push (gnus-data-number (car data)) articles))
8492       (unless (and (setq data (cdr data))
8493                    (> (gnus-data-level (car data)) top-level))
8494         (setq data nil)))
8495     ;; Return the list of articles.
8496     (nreverse articles)))
8497
8498 (defun gnus-summary-rethread-current ()
8499   "Rethread the thread the current article is part of."
8500   (interactive)
8501   (let* ((gnus-show-threads t)
8502          (article (gnus-summary-article-number))
8503          (id (mail-header-id (gnus-summary-article-header)))
8504          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8505     (unless id
8506       (error "No article on the current line"))
8507     (gnus-rebuild-thread id)
8508     (gnus-summary-goto-subject article)))
8509
8510 (defun gnus-summary-reparent-thread ()
8511   "Make the current article child of the marked (or previous) article.
8512
8513 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8514 is non-nil or the Subject: of both articles are the same."
8515   (interactive)
8516   (unless (not (gnus-group-read-only-p))
8517     (error "The current newsgroup does not support article editing"))
8518   (unless (<= (length gnus-newsgroup-processable) 1)
8519     (error "No more than one article may be marked"))
8520   (save-window-excursion
8521     (let ((gnus-article-buffer " *reparent*")
8522           (current-article (gnus-summary-article-number))
8523           ;; First grab the marked article, otherwise one line up.
8524           (parent-article (if (not (null gnus-newsgroup-processable))
8525                               (car gnus-newsgroup-processable)
8526                             (save-excursion
8527                               (if (eq (forward-line -1) 0)
8528                                   (gnus-summary-article-number)
8529                                 (error "Beginning of summary buffer"))))))
8530       (unless (not (eq current-article parent-article))
8531         (error "An article may not be self-referential"))
8532       (let ((message-id (mail-header-id
8533                          (gnus-summary-article-header parent-article))))
8534         (unless (and message-id (not (equal message-id "")))
8535           (error "No message-id in desired parent"))
8536         (gnus-with-article current-article
8537           (goto-char (point-min))
8538           (if (re-search-forward "^References: " nil t)
8539               (progn
8540                 (re-search-forward "^[^ \t]" nil t)
8541                 (forward-line -1)
8542                 (end-of-line)
8543                 (insert " " message-id))
8544             (insert "References: " message-id "\n")))
8545         (set-buffer gnus-summary-buffer)
8546         (gnus-summary-unmark-all-processable)
8547         (gnus-summary-update-article current-article)
8548         (gnus-summary-rethread-current)
8549         (gnus-message 3 "Article %d is now the child of article %d"
8550                       current-article parent-article)))))
8551
8552 (defun gnus-summary-toggle-threads (&optional arg)
8553   "Toggle showing conversation threads.
8554 If ARG is positive number, turn showing conversation threads on."
8555   (interactive "P")
8556   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8557     (setq gnus-show-threads
8558           (if (null arg) (not gnus-show-threads)
8559             (> (prefix-numeric-value arg) 0)))
8560     (gnus-summary-prepare)
8561     (gnus-summary-goto-subject current)
8562     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8563     (gnus-summary-position-point)))
8564
8565 (defun gnus-summary-show-all-threads ()
8566   "Show all threads."
8567   (interactive)
8568   (save-excursion
8569     (let ((buffer-read-only nil))
8570       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8571   (gnus-summary-position-point))
8572
8573 (defun gnus-summary-show-thread ()
8574   "Show thread subtrees.
8575 Returns nil if no thread was there to be shown."
8576   (interactive)
8577   (let ((buffer-read-only nil)
8578         (orig (point))
8579         ;; first goto end then to beg, to have point at beg after let
8580         (end (progn (end-of-line) (point)))
8581         (beg (progn (beginning-of-line) (point))))
8582     (prog1
8583         ;; Any hidden lines here?
8584         (search-forward "\r" end t)
8585       (subst-char-in-region beg end ?\^M ?\n t)
8586       (goto-char orig)
8587       (gnus-summary-position-point))))
8588
8589 (defun gnus-summary-hide-all-threads ()
8590   "Hide all thread subtrees."
8591   (interactive)
8592   (save-excursion
8593     (goto-char (point-min))
8594     (gnus-summary-hide-thread)
8595     (while (zerop (gnus-summary-next-thread 1 t))
8596       (gnus-summary-hide-thread)))
8597   (gnus-summary-position-point))
8598
8599 (defun gnus-summary-hide-thread ()
8600   "Hide thread subtrees.
8601 Returns nil if no threads were there to be hidden."
8602   (interactive)
8603   (let ((buffer-read-only nil)
8604         (start (point))
8605         (article (gnus-summary-article-number)))
8606     (goto-char start)
8607     ;; Go forward until either the buffer ends or the subthread
8608     ;; ends.
8609     (when (and (not (eobp))
8610                (or (zerop (gnus-summary-next-thread 1 t))
8611                    (goto-char (point-max))))
8612       (prog1
8613           (if (and (> (point) start)
8614                    (search-backward "\n" start t))
8615               (progn
8616                 (subst-char-in-region start (point) ?\n ?\^M)
8617                 (gnus-summary-goto-subject article))
8618             (goto-char start)
8619             nil)
8620         ;;(gnus-summary-position-point)
8621         ))))
8622
8623 (defun gnus-summary-go-to-next-thread (&optional previous)
8624   "Go to the same level (or less) next thread.
8625 If PREVIOUS is non-nil, go to previous thread instead.
8626 Return the article number moved to, or nil if moving was impossible."
8627   (let ((level (gnus-summary-thread-level))
8628         (way (if previous -1 1))
8629         (beg (point)))
8630     (forward-line way)
8631     (while (and (not (eobp))
8632                 (< level (gnus-summary-thread-level)))
8633       (forward-line way))
8634     (if (eobp)
8635         (progn
8636           (goto-char beg)
8637           nil)
8638       (setq beg (point))
8639       (prog1
8640           (gnus-summary-article-number)
8641         (goto-char beg)))))
8642
8643 (defun gnus-summary-next-thread (n &optional silent)
8644   "Go to the same level next N'th thread.
8645 If N is negative, search backward instead.
8646 Returns the difference between N and the number of skips actually
8647 done.
8648
8649 If SILENT, don't output messages."
8650   (interactive "p")
8651   (let ((backward (< n 0))
8652         (n (abs n)))
8653     (while (and (> n 0)
8654                 (gnus-summary-go-to-next-thread backward))
8655       (decf n))
8656     (unless silent
8657       (gnus-summary-position-point))
8658     (when (and (not silent) (/= 0 n))
8659       (gnus-message 7 "No more threads"))
8660     n))
8661
8662 (defun gnus-summary-prev-thread (n)
8663   "Go to the same level previous N'th thread.
8664 Returns the difference between N and the number of skips actually
8665 done."
8666   (interactive "p")
8667   (gnus-summary-next-thread (- n)))
8668
8669 (defun gnus-summary-go-down-thread ()
8670   "Go down one level in the current thread."
8671   (let ((children (gnus-summary-article-children)))
8672     (when children
8673       (gnus-summary-goto-subject (car children)))))
8674
8675 (defun gnus-summary-go-up-thread ()
8676   "Go up one level in the current thread."
8677   (let ((parent (gnus-summary-article-parent)))
8678     (when parent
8679       (gnus-summary-goto-subject parent))))
8680
8681 (defun gnus-summary-down-thread (n)
8682   "Go down thread N steps.
8683 If N is negative, go up instead.
8684 Returns the difference between N and how many steps down that were
8685 taken."
8686   (interactive "p")
8687   (let ((up (< n 0))
8688         (n (abs n)))
8689     (while (and (> n 0)
8690                 (if up (gnus-summary-go-up-thread)
8691                   (gnus-summary-go-down-thread)))
8692       (setq n (1- n)))
8693     (gnus-summary-position-point)
8694     (when (/= 0 n)
8695       (gnus-message 7 "Can't go further"))
8696     n))
8697
8698 (defun gnus-summary-up-thread (n)
8699   "Go up thread N steps.
8700 If N is negative, go up instead.
8701 Returns the difference between N and how many steps down that were
8702 taken."
8703   (interactive "p")
8704   (gnus-summary-down-thread (- n)))
8705
8706 (defun gnus-summary-top-thread ()
8707   "Go to the top of the thread."
8708   (interactive)
8709   (while (gnus-summary-go-up-thread))
8710   (gnus-summary-article-number))
8711
8712 (defun gnus-summary-kill-thread (&optional unmark)
8713   "Mark articles under current thread as read.
8714 If the prefix argument is positive, remove any kinds of marks.
8715 If the prefix argument is negative, tick articles instead."
8716   (interactive "P")
8717   (when unmark
8718     (setq unmark (prefix-numeric-value unmark)))
8719   (let ((articles (gnus-summary-articles-in-thread)))
8720     (save-excursion
8721       ;; Expand the thread.
8722       (gnus-summary-show-thread)
8723       ;; Mark all the articles.
8724       (while articles
8725         (gnus-summary-goto-subject (car articles))
8726         (cond ((null unmark)
8727                (gnus-summary-mark-article-as-read gnus-killed-mark))
8728               ((> unmark 0)
8729                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8730               (t
8731                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8732         (setq articles (cdr articles))))
8733     ;; Hide killed subtrees.
8734     (and (null unmark)
8735          gnus-thread-hide-killed
8736          (gnus-summary-hide-thread))
8737     ;; If marked as read, go to next unread subject.
8738     (when (null unmark)
8739       ;; Go to next unread subject.
8740       (gnus-summary-next-subject 1 t)))
8741   (gnus-set-mode-line 'summary))
8742
8743 ;; Summary sorting commands
8744
8745 (defun gnus-summary-sort-by-number (&optional reverse)
8746   "Sort the summary buffer by article number.
8747 Argument REVERSE means reverse order."
8748   (interactive "P")
8749   (gnus-summary-sort 'number reverse))
8750
8751 (defun gnus-summary-sort-by-author (&optional reverse)
8752   "Sort the summary buffer by author name alphabetically.
8753 If case-fold-search is non-nil, case of letters is ignored.
8754 Argument REVERSE means reverse order."
8755   (interactive "P")
8756   (gnus-summary-sort 'author reverse))
8757
8758 (defun gnus-summary-sort-by-subject (&optional reverse)
8759   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8760 If case-fold-search is non-nil, case of letters is ignored.
8761 Argument REVERSE means reverse order."
8762   (interactive "P")
8763   (gnus-summary-sort 'subject reverse))
8764
8765 (defun gnus-summary-sort-by-date (&optional reverse)
8766   "Sort the summary buffer by date.
8767 Argument REVERSE means reverse order."
8768   (interactive "P")
8769   (gnus-summary-sort 'date reverse))
8770
8771 (defun gnus-summary-sort-by-score (&optional reverse)
8772   "Sort the summary buffer by score.
8773 Argument REVERSE means reverse order."
8774   (interactive "P")
8775   (gnus-summary-sort 'score reverse))
8776
8777 (defun gnus-summary-sort-by-lines (&optional reverse)
8778   "Sort the summary buffer by the number of lines.
8779 Argument REVERSE means reverse order."
8780   (interactive "P")
8781   (gnus-summary-sort 'lines reverse))
8782
8783 (defun gnus-summary-sort-by-chars (&optional reverse)
8784   "Sort the summary buffer by article length.
8785 Argument REVERSE means reverse order."
8786   (interactive "P")
8787   (gnus-summary-sort 'chars reverse))   
8788
8789 (defun gnus-summary-sort (predicate reverse)
8790   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8791   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8792          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8793          (gnus-thread-sort-functions
8794           (if (not reverse)
8795               thread
8796             `(lambda (t1 t2)
8797                (,thread t2 t1))))
8798          (gnus-article-sort-functions
8799           (if (not reverse)
8800               article
8801             `(lambda (t1 t2)
8802                (,article t2 t1))))
8803          (buffer-read-only)
8804          (gnus-summary-prepare-hook nil))
8805     ;; We do the sorting by regenerating the threads.
8806     (gnus-summary-prepare)
8807     ;; Hide subthreads if needed.
8808     (when (and gnus-show-threads gnus-thread-hide-subtree)
8809       (gnus-summary-hide-all-threads))))
8810
8811 ;; Summary saving commands.
8812
8813 (defun gnus-summary-save-article (&optional n not-saved)
8814   "Save the current article using the default saver function.
8815 If N is a positive number, save the N next articles.
8816 If N is a negative number, save the N previous articles.
8817 If N is nil and any articles have been marked with the process mark,
8818 save those articles instead.
8819 The variable `gnus-default-article-saver' specifies the saver function."
8820   (interactive "P")
8821   (let* ((articles (gnus-summary-work-articles n))
8822          (save-buffer (save-excursion
8823                         (nnheader-set-temp-buffer " *Gnus Save*")))
8824          (num (length articles))
8825          header file)
8826     (dolist (article articles)
8827       (setq header (gnus-summary-article-header article))
8828       (if (not (vectorp header))
8829           ;; This is a pseudo-article.
8830           (if (assq 'name header)
8831               (gnus-copy-file (cdr (assq 'name header)))
8832             (gnus-message 1 "Article %d is unsaveable" article))
8833         ;; This is a real article.
8834         (save-window-excursion
8835           (gnus-summary-select-article t nil nil article))
8836         (save-excursion
8837           (set-buffer save-buffer)
8838           (erase-buffer)
8839           (insert-buffer-substring gnus-original-article-buffer))
8840         (setq file (gnus-article-save save-buffer file num))
8841         (gnus-summary-remove-process-mark article)
8842         (unless not-saved
8843           (gnus-summary-set-saved-mark article))))
8844     (gnus-kill-buffer save-buffer)
8845     (gnus-summary-position-point)
8846     (gnus-set-mode-line 'summary)
8847     n))
8848
8849 (defun gnus-summary-pipe-output (&optional arg)
8850   "Pipe the current article to a subprocess.
8851 If N is a positive number, pipe the N next articles.
8852 If N is a negative number, pipe the N previous articles.
8853 If N is nil and any articles have been marked with the process mark,
8854 pipe those articles instead."
8855   (interactive "P")
8856   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8857     (gnus-summary-save-article arg t))
8858   (gnus-configure-windows 'pipe))
8859
8860 (defun gnus-summary-save-article-mail (&optional arg)
8861   "Append the current article to an mail file.
8862 If N is a positive number, save the N next articles.
8863 If N is a negative number, save the N previous articles.
8864 If N is nil and any articles have been marked with the process mark,
8865 save those articles instead."
8866   (interactive "P")
8867   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8868     (gnus-summary-save-article arg)))
8869
8870 (defun gnus-summary-save-article-rmail (&optional arg)
8871   "Append the current article to an rmail file.
8872 If N is a positive number, save the N next articles.
8873 If N is a negative number, save the N previous articles.
8874 If N is nil and any articles have been marked with the process mark,
8875 save those articles instead."
8876   (interactive "P")
8877   (let ((gnus-default-article-saver 'rmail-output-to-rmail-file))
8878     (gnus-summary-save-article arg)))
8879
8880 (defun gnus-summary-save-article-file (&optional arg)
8881   "Append the current article to a file.
8882 If N is a positive number, save the N next articles.
8883 If N is a negative number, save the N previous articles.
8884 If N is nil and any articles have been marked with the process mark,
8885 save those articles instead."
8886   (interactive "P")
8887   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8888     (gnus-summary-save-article arg)))
8889
8890 (defun gnus-summary-write-article-file (&optional arg)
8891   "Write the current article to a file, deleting the previous file.
8892 If N is a positive number, save the N next articles.
8893 If N is a negative number, save the N previous articles.
8894 If N is nil and any articles have been marked with the process mark,
8895 save those articles instead."
8896   (interactive "P")
8897   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8898     (gnus-summary-save-article arg)))
8899
8900 (defun gnus-summary-save-article-body-file (&optional arg)
8901   "Append the current article body to a file.
8902 If N is a positive number, save the N next articles.
8903 If N is a negative number, save the N previous articles.
8904 If N is nil and any articles have been marked with the process mark,
8905 save those articles instead."
8906   (interactive "P")
8907   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8908     (gnus-summary-save-article arg)))
8909
8910 (defun gnus-summary-pipe-message (program)
8911   "Pipe the current article through PROGRAM."
8912   (interactive "sProgram: ")
8913   (gnus-summary-select-article)
8914   (let ((mail-header-separator ""))
8915     (gnus-eval-in-buffer-window gnus-article-buffer
8916       (save-restriction
8917         (widen)
8918         (let ((start (window-start))
8919               buffer-read-only)
8920           (message-pipe-buffer-body program)
8921           (set-window-start (get-buffer-window (current-buffer)) start))))))
8922
8923 (defun gnus-get-split-value (methods)
8924   "Return a value based on the split METHODS."
8925   (let (split-name method result match)
8926     (when methods
8927       (save-excursion
8928         (set-buffer gnus-original-article-buffer)
8929         (save-restriction
8930           (nnheader-narrow-to-headers)
8931           (while methods
8932             (goto-char (point-min))
8933             (setq method (pop methods))
8934             (setq match (car method))
8935             (when (cond
8936                    ((stringp match)
8937                     ;; Regular expression.
8938                     (ignore-errors
8939                       (re-search-forward match nil t)))
8940                    ((gnus-functionp match)
8941                     ;; Function.
8942                     (save-restriction
8943                       (widen)
8944                       (setq result (funcall match gnus-newsgroup-name))))
8945                    ((consp match)
8946                     ;; Form.
8947                     (save-restriction
8948                       (widen)
8949                       (setq result (eval match)))))
8950               (setq split-name (append (cdr method) split-name))
8951               (cond ((stringp result)
8952                      (push (expand-file-name
8953                             result gnus-article-save-directory)
8954                            split-name))
8955                     ((consp result)
8956                      (setq split-name (append result split-name)))))))))
8957     (nreverse split-name)))
8958
8959 (defun gnus-valid-move-group-p (group)
8960   (and (boundp group)
8961        (symbol-name group)
8962        (symbol-value group)
8963        (gnus-get-function (gnus-find-method-for-group
8964                            (symbol-name group)) 'request-accept-article t)))
8965
8966 (defun gnus-read-move-group-name (prompt default articles prefix)
8967   "Read a group name."
8968   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8969          (minibuffer-confirm-incomplete nil) ; XEmacs
8970          (prom
8971           (format "%s %s to:"
8972                   prompt
8973                   (if (> (length articles) 1)
8974                       (format "these %d articles" (length articles))
8975                     "this article")))
8976          (to-newsgroup
8977           (cond
8978            ((null split-name)
8979             (gnus-completing-read default prom
8980                                   gnus-active-hashtb
8981                                   'gnus-valid-move-group-p
8982                                   nil prefix
8983                                   'gnus-group-history))
8984            ((= 1 (length split-name))
8985             (gnus-completing-read (car split-name) prom
8986                                   gnus-active-hashtb
8987                                   'gnus-valid-move-group-p
8988                                   nil nil
8989                                   'gnus-group-history))
8990            (t
8991             (gnus-completing-read nil prom
8992                                   (mapcar (lambda (el) (list el))
8993                                           (nreverse split-name))
8994                                   nil nil nil
8995                                   'gnus-group-history)))))
8996     (when to-newsgroup
8997       (if (or (string= to-newsgroup "")
8998               (string= to-newsgroup prefix))
8999           (setq to-newsgroup default))
9000       (unless to-newsgroup
9001         (error "No group name entered"))
9002       (or (gnus-active to-newsgroup)
9003           (gnus-activate-group to-newsgroup)
9004           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9005                                      to-newsgroup))
9006               (or (and (gnus-request-create-group
9007                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
9008                        (gnus-activate-group
9009                         to-newsgroup nil nil
9010                         (gnus-group-name-to-method to-newsgroup))
9011                        (gnus-subscribe-group to-newsgroup))
9012                   (error "Couldn't create group %s" to-newsgroup)))
9013           (error "No such group: %s" to-newsgroup)))
9014     to-newsgroup))
9015
9016 (defun gnus-summary-save-parts (type dir n reverse)
9017   "Save parts matching TYPE to DIR.
9018 If REVERSE, save parts that do not match TYPE."
9019   (interactive
9020    (list (read-string "Save parts of type: " "image/.*")
9021          (read-file-name "Save to directory: " t nil t)
9022          current-prefix-arg))
9023   (gnus-summary-iterate n
9024     (let ((gnus-display-mime-function nil)
9025           (gnus-inhibit-treatment t))
9026       (gnus-summary-select-article))
9027     (save-excursion
9028       (set-buffer gnus-article-buffer)
9029       (let ((handles (or (mm-dissect-buffer gnus-current-headers)
9030                          (mm-uu-dissect))))
9031         (when handles
9032           (gnus-summary-save-parts-1 type dir handles reverse)
9033           (mm-destroy-parts handles))))))
9034
9035 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9036   (if (stringp (car handle))
9037       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9038               (cdr handle))
9039     (when (if reverse
9040               (not (string-match type (mm-handle-media-type handle)))
9041             (string-match type (mm-handle-media-type handle)))
9042       (let ((file (expand-file-name
9043                    (file-name-nondirectory
9044                     (or
9045                      (mail-content-type-get
9046                       (mm-handle-disposition handle) 'filename)
9047                      (concat gnus-newsgroup-name "." gnus-current-article)))
9048                    dir)))
9049         (unless (file-exists-p file)
9050           (mm-save-part-to-file handle file))))))
9051
9052 ;; Summary extract commands
9053
9054 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9055   (let ((buffer-read-only nil)
9056         (article (gnus-summary-article-number))
9057         after-article b e)
9058     (unless (gnus-summary-goto-subject article)
9059       (error "No such article: %d" article))
9060     (gnus-summary-position-point)
9061     ;; If all commands are to be bunched up on one line, we collect
9062     ;; them here.
9063     (unless gnus-view-pseudos-separately
9064       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9065             files action)
9066         (while ps
9067           (setq action (cdr (assq 'action (car ps))))
9068           (setq files (list (cdr (assq 'name (car ps)))))
9069           (while (and ps (cdr ps)
9070                       (string= (or action "1")
9071                                (or (cdr (assq 'action (cadr ps))) "2")))
9072             (push (cdr (assq 'name (cadr ps))) files)
9073             (setcdr ps (cddr ps)))
9074           (when files
9075             (when (not (string-match "%s" action))
9076               (push " " files))
9077             (push " " files)
9078             (when (assq 'execute (car ps))
9079               (setcdr (assq 'execute (car ps))
9080                       (funcall (if (string-match "%s" action)
9081                                    'format 'concat)
9082                                action
9083                                (mapconcat
9084                                 (lambda (f)
9085                                   (if (equal f " ")
9086                                       f
9087                                     (mm-quote-arg f)))
9088                                 files " ")))))
9089           (setq ps (cdr ps)))))
9090     (if (and gnus-view-pseudos (not not-view))
9091         (while pslist
9092           (when (assq 'execute (car pslist))
9093             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9094                                   (eq gnus-view-pseudos 'not-confirm)))
9095           (setq pslist (cdr pslist)))
9096       (save-excursion
9097         (while pslist
9098           (setq after-article (or (cdr (assq 'article (car pslist)))
9099                                   (gnus-summary-article-number)))
9100           (gnus-summary-goto-subject after-article)
9101           (forward-line 1)
9102           (setq b (point))
9103           (insert "    " (file-name-nondirectory
9104                           (cdr (assq 'name (car pslist))))
9105                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9106           (setq e (point))
9107           (forward-line -1)             ; back to `b'
9108           (gnus-add-text-properties
9109            b (1- e) (list 'gnus-number gnus-reffed-article-number
9110                           gnus-mouse-face-prop gnus-mouse-face))
9111           (gnus-data-enter
9112            after-article gnus-reffed-article-number
9113            gnus-unread-mark b (car pslist) 0 (- e b))
9114           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9115           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9116           (setq pslist (cdr pslist)))))))
9117
9118 (defun gnus-pseudos< (p1 p2)
9119   (let ((c1 (cdr (assq 'action p1)))
9120         (c2 (cdr (assq 'action p2))))
9121     (and c1 c2 (string< c1 c2))))
9122
9123 (defun gnus-request-pseudo-article (props)
9124   (cond ((assq 'execute props)
9125          (gnus-execute-command (cdr (assq 'execute props)))))
9126   (let ((gnus-current-article (gnus-summary-article-number)))
9127     (gnus-run-hooks 'gnus-mark-article-hook)))
9128
9129 (defun gnus-execute-command (command &optional automatic)
9130   (save-excursion
9131     (gnus-article-setup-buffer)
9132     (set-buffer gnus-article-buffer)
9133     (setq buffer-read-only nil)
9134     (let ((command (if automatic command
9135                      (read-string "Command: " (cons command 0)))))
9136       (erase-buffer)
9137       (insert "$ " command "\n\n")
9138       (if gnus-view-pseudo-asynchronously
9139           (start-process "gnus-execute" (current-buffer) shell-file-name
9140                          shell-command-switch command)
9141         (call-process shell-file-name nil t nil
9142                       shell-command-switch command)))))
9143
9144 ;; Summary kill commands.
9145
9146 (defun gnus-summary-edit-global-kill (article)
9147   "Edit the \"global\" kill file."
9148   (interactive (list (gnus-summary-article-number)))
9149   (gnus-group-edit-global-kill article))
9150
9151 (defun gnus-summary-edit-local-kill ()
9152   "Edit a local kill file applied to the current newsgroup."
9153   (interactive)
9154   (setq gnus-current-headers (gnus-summary-article-header))
9155   (gnus-group-edit-local-kill
9156    (gnus-summary-article-number) gnus-newsgroup-name))
9157
9158 ;;; Header reading.
9159
9160 (defun gnus-read-header (id &optional header)
9161   "Read the headers of article ID and enter them into the Gnus system."
9162   (let ((group gnus-newsgroup-name)
9163         (gnus-override-method
9164          (and (gnus-news-group-p gnus-newsgroup-name)
9165               gnus-refer-article-method))
9166         where)
9167     ;; First we check to see whether the header in question is already
9168     ;; fetched.
9169     (if (stringp id)
9170         ;; This is a Message-ID.
9171         (setq header (or header (gnus-id-to-header id)))
9172       ;; This is an article number.
9173       (setq header (or header (gnus-summary-article-header id))))
9174     (if (and header
9175              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9176         ;; We have found the header.
9177         header
9178       ;; If this is a sparse article, we have to nix out its
9179       ;; previous entry in the thread hashtb.
9180       (when (and header
9181                  (gnus-summary-article-sparse-p (mail-header-number header)))
9182         (let* ((parent (gnus-parent-id (mail-header-references header)))
9183                (thread (and parent (gnus-id-to-thread parent))))
9184           (when thread
9185             (delq (assq header thread) thread))))
9186       ;; We have to really fetch the header to this article.
9187       (save-excursion
9188         (set-buffer nntp-server-buffer)
9189         (when (setq where (gnus-request-head id group))
9190           (nnheader-fold-continuation-lines)
9191           (goto-char (point-max))
9192           (insert ".\n")
9193           (goto-char (point-min))
9194           (insert "211 ")
9195           (princ (cond
9196                   ((numberp id) id)
9197                   ((cdr where) (cdr where))
9198                   (header (mail-header-number header))
9199                   (t gnus-reffed-article-number))
9200                  (current-buffer))
9201           (insert " Article retrieved.\n"))
9202         (if (or (not where)
9203                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9204             ()                          ; Malformed head.
9205           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9206             (when (and (stringp id)
9207                        (not (string= (gnus-group-real-name group)
9208                                      (car where))))
9209               ;; If we fetched by Message-ID and the article came
9210               ;; from a different group, we fudge some bogus article
9211               ;; numbers for this article.
9212               (mail-header-set-number header gnus-reffed-article-number))
9213             (save-excursion
9214               (set-buffer gnus-summary-buffer)
9215               (decf gnus-reffed-article-number)
9216               (gnus-remove-header (mail-header-number header))
9217               (push header gnus-newsgroup-headers)
9218               (setq gnus-current-headers header)
9219               (push (mail-header-number header) gnus-newsgroup-limit)))
9220           header)))))
9221
9222 (defun gnus-remove-header (number)
9223   "Remove header NUMBER from `gnus-newsgroup-headers'."
9224   (if (and gnus-newsgroup-headers
9225            (= number (mail-header-number (car gnus-newsgroup-headers))))
9226       (pop gnus-newsgroup-headers)
9227     (let ((headers gnus-newsgroup-headers))
9228       (while (and (cdr headers)
9229                   (not (= number (mail-header-number (cadr headers)))))
9230         (pop headers))
9231       (when (cdr headers)
9232         (setcdr headers (cddr headers))))))
9233
9234 ;;;
9235 ;;; summary highlights
9236 ;;;
9237
9238 (defun gnus-highlight-selected-summary ()
9239   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9240   ;; Highlight selected article in summary buffer
9241   (when gnus-summary-selected-face
9242     (save-excursion
9243       (let* ((beg (progn (beginning-of-line) (point)))
9244              (end (progn (end-of-line) (point)))
9245              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9246              (from (if (get-text-property beg gnus-mouse-face-prop)
9247                        beg
9248                      (or (next-single-property-change
9249                           beg gnus-mouse-face-prop nil end)
9250                          beg)))
9251              (to
9252               (if (= from end)
9253                   (- from 2)
9254                 (or (next-single-property-change
9255                      from gnus-mouse-face-prop nil end)
9256                     end))))
9257         ;; If no mouse-face prop on line we will have to = from = end,
9258         ;; so we highlight the entire line instead.
9259         (when (= (+ to 2) from)
9260           (setq from beg)
9261           (setq to end))
9262         (if gnus-newsgroup-selected-overlay
9263             ;; Move old overlay.
9264             (gnus-move-overlay
9265              gnus-newsgroup-selected-overlay from to (current-buffer))
9266           ;; Create new overlay.
9267           (gnus-overlay-put
9268            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9269            'face gnus-summary-selected-face))))))
9270
9271 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9272 (defun gnus-summary-highlight-line ()
9273   "Highlight current line according to `gnus-summary-highlight'."
9274   (let* ((list gnus-summary-highlight)
9275          (p (point))
9276          (end (progn (end-of-line) (point)))
9277          ;; now find out where the line starts and leave point there.
9278          (beg (progn (beginning-of-line) (point)))
9279          (article (gnus-summary-article-number))
9280          (score (or (cdr (assq (or article gnus-current-article)
9281                                gnus-newsgroup-scored))
9282                     gnus-summary-default-score 0))
9283          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9284          (inhibit-read-only t))
9285     ;; Eval the cars of the lists until we find a match.
9286     (let ((default gnus-summary-default-score))
9287       (while (and list
9288                   (not (eval (caar list))))
9289         (setq list (cdr list))))
9290     (let ((face (cdar list)))
9291       (unless (eq face (get-text-property beg 'face))
9292         (gnus-put-text-property-excluding-characters-with-faces
9293          beg end 'face
9294          (setq face (if (boundp face) (symbol-value face) face)))
9295         (when gnus-summary-highlight-line-function
9296           (funcall gnus-summary-highlight-line-function article face))))
9297     (goto-char p)))
9298
9299 (defun gnus-update-read-articles (group unread &optional compute)
9300   "Update the list of read articles in GROUP."
9301   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9302          (entry (gnus-gethash group gnus-newsrc-hashtb))
9303          (info (nth 2 entry))
9304          (prev 1)
9305          (unread (sort (copy-sequence unread) '<))
9306          read)
9307     (if (or (not info) (not active))
9308         ;; There is no info on this group if it was, in fact,
9309         ;; killed.  Gnus stores no information on killed groups, so
9310         ;; there's nothing to be done.
9311         ;; One could store the information somewhere temporarily,
9312         ;; perhaps...  Hmmm...
9313         ()
9314       ;; Remove any negative articles numbers.
9315       (while (and unread (< (car unread) 0))
9316         (setq unread (cdr unread)))
9317       ;; Remove any expired article numbers
9318       (while (and unread (< (car unread) (car active)))
9319         (setq unread (cdr unread)))
9320       ;; Compute the ranges of read articles by looking at the list of
9321       ;; unread articles.
9322       (while unread
9323         (when (/= (car unread) prev)
9324           (push (if (= prev (1- (car unread))) prev
9325                   (cons prev (1- (car unread))))
9326                 read))
9327         (setq prev (1+ (car unread)))
9328         (setq unread (cdr unread)))
9329       (when (<= prev (cdr active))
9330         (push (cons prev (cdr active)) read))
9331       (setq read (if (> (length read) 1) (nreverse read) read))
9332       (if compute
9333           read
9334         (save-excursion
9335           (set-buffer gnus-group-buffer)
9336           (gnus-undo-register
9337             `(progn
9338                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9339                (gnus-info-set-read ',info ',(gnus-info-read info))
9340                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9341                (gnus-group-update-group ,group t))))
9342        ;; Propagate the read marks to the backend.
9343        (if (gnus-check-backend-function 'request-set-mark group)
9344            (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9345                  (add (gnus-remove-from-range read (gnus-info-read info))))
9346              (when (or add del)
9347                (unless (gnus-check-group group)
9348                  (error "Can't open server for %s" group))
9349                (gnus-request-set-mark
9350                 group (delq nil (list (if add (list add 'add '(read)))
9351                                       (if del (list del 'del '(read)))))))))
9352         ;; Enter this list into the group info.
9353         (gnus-info-set-read info read)
9354         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9355         (gnus-get-unread-articles-in-group info (gnus-active group))
9356         t))))
9357
9358 (defun gnus-offer-save-summaries ()
9359   "Offer to save all active summary buffers."
9360   (save-excursion
9361     (let ((buflist (buffer-list))
9362           buffers bufname)
9363       ;; Go through all buffers and find all summaries.
9364       (while buflist
9365         (and (setq bufname (buffer-name (car buflist)))
9366              (string-match "Summary" bufname)
9367              (save-excursion
9368                (set-buffer bufname)
9369                ;; We check that this is, indeed, a summary buffer.
9370                (and (eq major-mode 'gnus-summary-mode)
9371                     ;; Also make sure this isn't bogus.
9372                     gnus-newsgroup-prepared
9373                     ;; Also make sure that this isn't a dead summary buffer.
9374                     (not gnus-dead-summary-mode)))
9375              (push bufname buffers))
9376         (setq buflist (cdr buflist)))
9377       ;; Go through all these summary buffers and offer to save them.
9378       (when buffers
9379         (map-y-or-n-p
9380          "Update summary buffer %s? "
9381          (lambda (buf)
9382            (switch-to-buffer buf)
9383            (gnus-summary-exit))
9384          buffers)))))
9385
9386 (defun gnus-summary-setup-default-charset ()
9387   "Setup newsgroup default charset."
9388   (if (equal gnus-newsgroup-name "nndraft:drafts")
9389       (setq gnus-newsgroup-charset nil)
9390   (let* ((name (and gnus-newsgroup-name
9391                    (gnus-group-real-name gnus-newsgroup-name)))
9392          (ignored-charsets 
9393           (or gnus-newsgroup-ephemeral-ignored-charsets
9394               (append
9395                (and gnus-newsgroup-name
9396                     (or (gnus-group-find-parameter gnus-newsgroup-name
9397                                                    'ignored-charsets t)
9398                         (let ((alist gnus-group-ignored-charsets-alist)
9399                            elem (charsets nil))
9400                           (while (setq elem (pop alist))
9401                             (when (and name
9402                                        (string-match (car elem) name))
9403                               (setq alist nil
9404                                     charsets (cdr elem))))
9405                           charsets))))
9406               gnus-newsgroup-ignored-charsets)))
9407     (setq gnus-newsgroup-charset
9408           (or gnus-newsgroup-ephemeral-charset
9409               (and gnus-newsgroup-name
9410                    (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9411                        (let ((alist gnus-group-charset-alist)
9412                              elem charset)
9413                          (while (setq elem (pop alist))
9414                            (when (and name
9415                                       (string-match (car elem) name))
9416                              (setq alist nil
9417                                    charset (cadr elem))))
9418                          charset)))
9419               gnus-default-charset))
9420     (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9421          ignored-charsets))))
9422
9423 ;;;
9424 ;;; Mime Commands
9425 ;;;
9426
9427 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9428   "Display the current article buffer fully MIME-buttonized.
9429 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9430 treated as multipart/mixed."
9431   (interactive "P")
9432   (require 'gnus-art)
9433   (let ((gnus-unbuttonized-mime-types nil)
9434         (gnus-mime-display-multipart-as-mixed show-all-parts))
9435     (gnus-summary-show-article)))
9436
9437 (defun gnus-summary-repair-multipart (article)
9438   "Add a Content-Type header to a multipart article without one."
9439   (interactive (list (gnus-summary-article-number)))
9440   (gnus-with-article article
9441     (message-narrow-to-head)
9442     (goto-char (point-max))
9443     (widen)
9444     (when (search-forward "\n--" nil t)
9445       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9446         (message-narrow-to-head)
9447         (message-remove-header "Mime-Version")
9448         (message-remove-header "Content-Type")
9449         (goto-char (point-max))
9450         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9451                         separator))
9452         (insert "Mime-Version: 1.0\n")
9453         (widen))))
9454   (let (gnus-mark-article-hook)
9455     (gnus-summary-select-article t t nil article)))
9456
9457 (defun gnus-summary-toggle-display-buttonized ()
9458   "Toggle the buttonizing of the article buffer."
9459   (interactive)
9460   (require 'gnus-art)
9461   (if (setq gnus-inhibit-mime-unbuttonizing
9462             (not gnus-inhibit-mime-unbuttonizing))
9463       (let ((gnus-unbuttonized-mime-types nil))
9464         (gnus-summary-show-article))
9465     (gnus-summary-show-article)))
9466
9467 ;;;
9468 ;;; with article
9469 ;;;
9470
9471 (defmacro gnus-with-article (article &rest forms)
9472   "Select ARTICLE and perform FORMS in the original article buffer.
9473 Then replace the article with the result."
9474   `(progn
9475      ;; We don't want the article to be marked as read.
9476      (let (gnus-mark-article-hook)
9477        (gnus-summary-select-article t t nil ,article))
9478      (set-buffer gnus-original-article-buffer)
9479      ,@forms
9480      (if (not (gnus-check-backend-function
9481                'request-replace-article (car gnus-article-current)))
9482          (gnus-message 5 "Read-only group; not replacing")
9483        (unless (gnus-request-replace-article
9484                 ,article (car gnus-article-current)
9485                 (current-buffer) t)
9486          (error "Couldn't replace article")))
9487      ;; The cache and backlog have to be flushed somewhat.
9488      (when gnus-keep-backlog
9489        (gnus-backlog-remove-article
9490         (car gnus-article-current) (cdr gnus-article-current)))
9491      (when gnus-use-cache
9492        (gnus-cache-update-article
9493         (car gnus-article-current) (cdr gnus-article-current)))))
9494
9495 (put 'gnus-with-article 'lisp-indent-function 1)
9496 (put 'gnus-with-article 'edebug-form-spec '(form body))
9497
9498 ;;;
9499 ;;; Generic summary marking commands
9500 ;;;
9501
9502 (defvar gnus-summary-marking-alist
9503   '((read gnus-del-mark "d")
9504     (unread gnus-unread-mark "u")
9505     (ticked gnus-ticked-mark "!")
9506     (dormant gnus-dormant-mark "?")
9507     (expirable gnus-expirable-mark "e"))
9508   "An alist of names/marks/keystrokes.")
9509
9510 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9511 (defvar gnus-summary-mark-map)
9512
9513 (defun gnus-summary-make-all-marking-commands ()
9514   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9515   (dolist (elem gnus-summary-marking-alist)
9516     (apply 'gnus-summary-make-marking-command elem)))
9517
9518 (defun gnus-summary-make-marking-command (name mark keystroke)
9519   (let ((map (make-sparse-keymap)))
9520     (define-key gnus-summary-generic-mark-map keystroke map)
9521     (dolist (lway `((next "next" next nil "n")
9522                     (next-unread "next unread" next t "N")
9523                     (prev "previous" prev nil "p")
9524                     (prev-unread "previous unread" prev t "P")
9525                     (nomove "" nil nil ,keystroke)))
9526       (let ((func (gnus-summary-make-marking-command-1
9527                    mark (car lway) lway name)))
9528         (setq func (eval func))
9529         (define-key map (nth 4 lway) func)))))
9530       
9531 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9532   `(defun ,(intern
9533             (format "gnus-summary-put-mark-as-%s%s"
9534                     name (if (eq way 'nomove)
9535                              ""
9536                            (concat "-" (symbol-name way)))))
9537      (n)
9538      ,(format
9539        "Mark the current article as %s%s.
9540 If N, the prefix, then repeat N times.
9541 If N is negative, move in reverse order.
9542 The difference between N and the actual number of articles marked is
9543 returned."
9544        name (cadr lway))
9545      (interactive "p")
9546      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9547     
9548 (defun gnus-summary-generic-mark (n mark move unread)
9549   "Mark N articles with MARK."
9550   (unless (eq major-mode 'gnus-summary-mode)
9551     (error "This command can only be used in the summary buffer"))
9552   (gnus-summary-show-thread)
9553   (let ((nummove
9554          (cond
9555           ((eq move 'next) 1)
9556           ((eq move 'prev) -1)
9557           (t 0))))
9558     (if (zerop nummove)
9559         (setq n 1)
9560       (when (< n 0)
9561         (setq n (abs n)
9562               nummove (* -1 nummove))))
9563     (while (and (> n 0)
9564                 (gnus-summary-mark-article nil mark)
9565                 (zerop (gnus-summary-next-subject nummove unread t)))
9566       (setq n (1- n)))
9567     (when (/= 0 n)
9568       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9569     (gnus-summary-recenter)
9570     (gnus-summary-position-point)
9571     (gnus-set-mode-line 'summary)
9572     n))
9573
9574 (gnus-summary-make-all-marking-commands)
9575
9576 (gnus-ems-redefine)
9577
9578 (provide 'gnus-sum)
9579
9580 (run-hooks 'gnus-sum-load-hook)
9581
9582 ;;; gnus-sum.el ends here