2001-08-12 Simon Josefsson <jas@extundo.com>
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
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 ;; Recursive :-(.
40 ;; (require 'gnus-art)
41 (require 'nnoo)
42 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
43 (autoload 'gnus-cache-write-active "gnus-cache")
44 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
45 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
46 (autoload 'mm-uu-dissect "mm-uu")
47
48 (defcustom gnus-kill-summary-on-exit t
49   "*If non-nil, kill the summary buffer when you exit from it.
50 If nil, the summary will become a \"*Dead Summary*\" buffer, and
51 it will be killed sometime later."
52   :group 'gnus-summary-exit
53   :type 'boolean)
54
55 (defcustom gnus-fetch-old-headers nil
56   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
57 If an unread article in the group refers to an older, already read (or
58 just marked as read) article, the old article will not normally be
59 displayed in the Summary buffer.  If this variable is non-nil, Gnus
60 will attempt to grab the headers to the old articles, and thereby
61 build complete threads.  If it has the value `some', only enough
62 headers to connect otherwise loose threads will be displayed.  This
63 variable can also be a number.  In that case, no more than that number
64 of old headers will be fetched.  If it has the value `invisible', all
65 old headers will be fetched, but none will be displayed.
66
67 The server has to support NOV for any of this to work."
68   :group 'gnus-thread
69   :type '(choice (const :tag "off" nil)
70                  (const some)
71                  number
72                  (sexp :menu-tag "other" t)))
73
74 (defcustom gnus-refer-thread-limit 200
75   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
76 If t, fetch all the available old headers."
77   :group 'gnus-thread
78   :type '(choice number
79                  (sexp :menu-tag "other" t)))
80
81 (defcustom gnus-summary-make-false-root 'adopt
82   "*nil means that Gnus won't gather loose threads.
83 If the root of a thread has expired or been read in a previous
84 session, the information necessary to build a complete thread has been
85 lost.  Instead of having many small sub-threads from this original thread
86 scattered all over the summary buffer, Gnus can gather them.
87
88 If non-nil, Gnus will try to gather all loose sub-threads from an
89 original thread into one large thread.
90
91 If this variable is non-nil, it should be one of `none', `adopt',
92 `dummy' or `empty'.
93
94 If this variable is `none', Gnus will not make a false root, but just
95 present the sub-threads after another.
96 If this variable is `dummy', Gnus will create a dummy root that will
97 have all the sub-threads as children.
98 If this variable is `adopt', Gnus will make one of the \"children\"
99 the parent and mark all the step-children as such.
100 If this variable is `empty', the \"children\" are printed with empty
101 subject fields.  (Or rather, they will be printed with a string
102 given by the `gnus-summary-same-subject' variable.)"
103   :group 'gnus-thread
104   :type '(choice (const :tag "off" nil)
105                  (const none)
106                  (const dummy)
107                  (const adopt)
108                  (const empty)))
109
110 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
111   "*A regexp to match subjects to be excluded from loose thread gathering.
112 As loose thread gathering is done on subjects only, that means that
113 there can be many false gatherings performed.  By rooting out certain
114 common subjects, gathering might become saner."
115   :group 'gnus-thread
116   :type 'regexp)
117
118 (defcustom gnus-summary-gather-subject-limit nil
119   "*Maximum length of subject comparisons when gathering loose threads.
120 Use nil to compare full subjects.  Setting this variable to a low
121 number will help gather threads that have been corrupted by
122 newsreaders chopping off subject lines, but it might also mean that
123 unrelated articles that have subject that happen to begin with the
124 same few characters will be incorrectly gathered.
125
126 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
127 comparing subjects."
128   :group 'gnus-thread
129   :type '(choice (const :tag "off" nil)
130                  (const fuzzy)
131                  (sexp :menu-tag "on" t)))
132
133 (defcustom gnus-simplify-subject-functions nil
134   "List of functions taking a string argument that simplify subjects.
135 The functions are applied recursively.
136
137 Useful functions to put in this list include: `gnus-simplify-subject-re',
138 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
139   :group 'gnus-thread
140   :type '(repeat function))
141
142 (defcustom gnus-simplify-ignored-prefixes nil
143   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
144   :group 'gnus-thread
145   :type '(choice (const :tag "off" nil)
146                  regexp))
147
148 (defcustom gnus-build-sparse-threads nil
149   "*If non-nil, fill in the gaps in threads.
150 If `some', only fill in the gaps that are needed to tie loose threads
151 together.  If `more', fill in all leaf nodes that Gnus can find.  If
152 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
153   :group 'gnus-thread
154   :type '(choice (const :tag "off" nil)
155                  (const some)
156                  (const more)
157                  (sexp :menu-tag "all" t)))
158
159 (defcustom gnus-summary-thread-gathering-function
160   'gnus-gather-threads-by-subject
161   "*Function used for gathering loose threads.
162 There are two pre-defined functions: `gnus-gather-threads-by-subject',
163 which only takes Subjects into consideration; and
164 `gnus-gather-threads-by-references', which compared the References
165 headers of the articles to find matches."
166   :group 'gnus-thread
167   :type '(radio (function-item gnus-gather-threads-by-subject)
168                 (function-item gnus-gather-threads-by-references)
169                 (function :tag "other")))
170
171 (defcustom gnus-summary-same-subject ""
172   "*String indicating that the current article has the same subject as the previous.
173 This variable will only be used if the value of
174 `gnus-summary-make-false-root' is `empty'."
175   :group 'gnus-summary-format
176   :type 'string)
177
178 (defcustom gnus-summary-goto-unread t
179   "*If t, many commands will go to the next unread article.
180 This applies to marking commands as well as other commands that
181 \"naturally\" select the next article, like, for instance, `SPC' at
182 the end of an article.
183
184 If nil, the marking commands do NOT go to the next unread article
185 (they go to the next article instead).  If `never', commands that
186 usually go to the next unread article, will go to the next article,
187 whether it is read or not."
188   :group 'gnus-summary-marks
189   :link '(custom-manual "(gnus)Setting Marks")
190   :type '(choice (const :tag "off" nil)
191                  (const never)
192                  (sexp :menu-tag "on" t)))
193
194 (defcustom gnus-summary-default-score 0
195   "*Default article score level.
196 All scores generated by the score files will be added to this score.
197 If this variable is nil, scoring will be disabled."
198   :group 'gnus-score-default
199   :type '(choice (const :tag "disable")
200                  integer))
201
202 (defcustom gnus-summary-default-high-score 0
203   "*Default threshold for a high scored article.
204 An article will be highlighted as high scored if its score is greater
205 than this score."
206   :group 'gnus-score-default
207   :type 'integer)
208
209 (defcustom gnus-summary-default-low-score 0
210   "*Default threshold for a low scored article.
211 An article will be highlighted as low scored if its score is smaller
212 than this score."
213   :group 'gnus-score-default
214   :type 'integer)
215
216 (defcustom gnus-summary-zcore-fuzz 0
217   "*Fuzziness factor for the zcore in the summary buffer.
218 Articles with scores closer than this to `gnus-summary-default-score'
219 will not be marked."
220   :group 'gnus-summary-format
221   :type 'integer)
222
223 (defcustom gnus-simplify-subject-fuzzy-regexp nil
224   "*Strings to be removed when doing fuzzy matches.
225 This can either be a regular expression or list of regular expressions
226 that will be removed from subject strings if fuzzy subject
227 simplification is selected."
228   :group 'gnus-thread
229   :type '(repeat regexp))
230
231 (defcustom gnus-show-threads t
232   "*If non-nil, display threads in summary mode."
233   :group 'gnus-thread
234   :type 'boolean)
235
236 (defcustom gnus-thread-hide-subtree nil
237   "*If non-nil, hide all threads initially.
238 If threads are hidden, you have to run the command
239 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
240 to expose hidden threads."
241   :group 'gnus-thread
242   :type 'boolean)
243
244 (defcustom gnus-thread-hide-killed t
245   "*If non-nil, hide killed threads automatically."
246   :group 'gnus-thread
247   :type 'boolean)
248
249 (defcustom gnus-thread-ignore-subject t
250   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
251 If nil, articles that have different subjects from their parents will
252 start separate threads."
253   :group 'gnus-thread
254   :type 'boolean)
255
256 (defcustom gnus-thread-operation-ignore-subject t
257   "*If non-nil, subjects will be ignored when doing thread commands.
258 This affects commands like `gnus-summary-kill-thread' and
259 `gnus-summary-lower-thread'.
260
261 If this variable is nil, articles in the same thread with different
262 subjects will not be included in the operation in question.  If this
263 variable is `fuzzy', only articles that have subjects that are fuzzily
264 equal will be included."
265   :group 'gnus-thread
266   :type '(choice (const :tag "off" nil)
267                  (const fuzzy)
268                  (sexp :tag "on" t)))
269
270 (defcustom gnus-thread-indent-level 4
271   "*Number that says how much each sub-thread should be indented."
272   :group 'gnus-thread
273   :type 'integer)
274
275 (defcustom gnus-auto-extend-newsgroup t
276   "*If non-nil, extend newsgroup forward and backward when requested."
277   :group 'gnus-summary-choose
278   :type 'boolean)
279
280 (defcustom gnus-auto-select-first t
281   "*If nil, don't select the first unread article when entering a group.
282 If this variable is `best', select the highest-scored unread article
283 in the group.  If t, select the first unread article.
284
285 This variable can also be a function to place point on a likely
286 subject line.  Useful values include `gnus-summary-first-unread-subject',
287 `gnus-summary-first-unread-article' and
288 `gnus-summary-best-unread-article'.
289
290 If you want to prevent automatic selection of the first unread article
291 in some newsgroups, set the variable to nil in
292 `gnus-select-group-hook'."
293   :group 'gnus-group-select
294   :type '(choice (const :tag "none" nil)
295                  (const best)
296                  (sexp :menu-tag "first" t)
297                  (function-item gnus-summary-first-unread-subject)
298                  (function-item gnus-summary-first-unread-article)
299                  (function-item gnus-summary-best-unread-article)))
300
301 (defcustom gnus-auto-select-next t
302   "*If non-nil, offer to go to the next group from the end of the previous.
303 If the value is t and the next newsgroup is empty, Gnus will exit
304 summary mode and go back to group mode.  If the value is neither nil
305 nor t, Gnus will select the following unread newsgroup.  In
306 particular, if the value is the symbol `quietly', the next unread
307 newsgroup will be selected without any confirmation, and if it is
308 `almost-quietly', the next group will be selected without any
309 confirmation if you are located on the last article in the group.
310 Finally, if this variable is `slightly-quietly', the `Z n' command
311 will go to the next group without confirmation."
312   :group 'gnus-summary-maneuvering
313   :type '(choice (const :tag "off" nil)
314                  (const quietly)
315                  (const almost-quietly)
316                  (const slightly-quietly)
317                  (sexp :menu-tag "on" t)))
318
319 (defcustom gnus-auto-select-same nil
320   "*If non-nil, select the next article with the same subject.
321 If there are no more articles with the same subject, go to
322 the first unread article."
323   :group 'gnus-summary-maneuvering
324   :type 'boolean)
325
326 (defcustom gnus-summary-check-current nil
327   "*If non-nil, consider the current article when moving.
328 The \"unread\" movement commands will stay on the same line if the
329 current article is unread."
330   :group 'gnus-summary-maneuvering
331   :type 'boolean)
332
333 (defcustom gnus-auto-center-summary t
334   "*If non-nil, always center the current summary buffer.
335 In particular, if `vertical' do only vertical recentering.  If non-nil
336 and non-`vertical', do both horizontal and vertical recentering."
337   :group 'gnus-summary-maneuvering
338   :type '(choice (const :tag "none" nil)
339                  (const vertical)
340                  (integer :tag "height")
341                  (sexp :menu-tag "both" t)))
342
343 (defcustom gnus-show-all-headers nil
344   "*If non-nil, don't hide any headers."
345   :group 'gnus-article-hiding
346   :group 'gnus-article-headers
347   :type 'boolean)
348
349 (defcustom gnus-summary-ignore-duplicates nil
350   "*If non-nil, ignore articles with identical Message-ID headers."
351   :group 'gnus-summary
352   :type 'boolean)
353
354 (defcustom gnus-single-article-buffer t
355   "*If non-nil, display all articles in the same buffer.
356 If nil, each group will get its own article buffer."
357   :group 'gnus-article-various
358   :type 'boolean)
359
360 (defcustom gnus-break-pages t
361   "*If non-nil, do page breaking on articles.
362 The page delimiter is specified by the `gnus-page-delimiter'
363 variable."
364   :group 'gnus-article-various
365   :type 'boolean)
366
367 (defcustom gnus-move-split-methods nil
368   "*Variable used to suggest where articles are to be moved to.
369 It uses the same syntax as the `gnus-split-methods' variable.
370 However, whereas `gnus-split-methods' specifies file names as targets,
371 this variable specifies group names."
372   :group 'gnus-summary-mail
373   :type '(repeat (choice (list :value (fun) function)
374                          (cons :value ("" "") regexp (repeat string))
375                          (sexp :value nil))))
376
377 (defcustom gnus-unread-mark ?           ;Whitespace
378   "*Mark used for unread articles."
379   :group 'gnus-summary-marks
380   :type 'character)
381
382 (defcustom gnus-ticked-mark ?!
383   "*Mark used for ticked articles."
384   :group 'gnus-summary-marks
385   :type 'character)
386
387 (defcustom gnus-dormant-mark ??
388   "*Mark used for dormant articles."
389   :group 'gnus-summary-marks
390   :type 'character)
391
392 (defcustom gnus-del-mark ?r
393   "*Mark used for del'd articles."
394   :group 'gnus-summary-marks
395   :type 'character)
396
397 (defcustom gnus-read-mark ?R
398   "*Mark used for read articles."
399   :group 'gnus-summary-marks
400   :type 'character)
401
402 (defcustom gnus-expirable-mark ?E
403   "*Mark used for expirable articles."
404   :group 'gnus-summary-marks
405   :type 'character)
406
407 (defcustom gnus-killed-mark ?K
408   "*Mark used for killed articles."
409   :group 'gnus-summary-marks
410   :type 'character)
411
412 (defcustom gnus-souped-mark ?F
413   "*Mark used for souped articles."
414   :group 'gnus-summary-marks
415   :type 'character)
416
417 (defcustom gnus-kill-file-mark ?X
418   "*Mark used for articles killed by kill files."
419   :group 'gnus-summary-marks
420   :type 'character)
421
422 (defcustom gnus-low-score-mark ?Y
423   "*Mark used for articles with a low score."
424   :group 'gnus-summary-marks
425   :type 'character)
426
427 (defcustom gnus-catchup-mark ?C
428   "*Mark used for articles that are caught up."
429   :group 'gnus-summary-marks
430   :type 'character)
431
432 (defcustom gnus-replied-mark ?A
433   "*Mark used for articles that have been replied to."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-forwarded-mark ?O
438   "*Mark used for articles that have been forwarded."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-recent-mark ?N
443   "*Mark used for articles that are recent."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-cached-mark ?*
448   "*Mark used for articles that are in the cache."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-saved-mark ?S
453   "*Mark used for articles that have been saved to."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-no-mark ?               ;Whitespace
458   "*Mark used for articles that have no other secondary mark."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-ancient-mark ?O
463   "*Mark used for ancient articles."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-sparse-mark ?Q
468   "*Mark used for sparsely reffed articles."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-canceled-mark ?G
473   "*Mark used for canceled articles."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-duplicate-mark ?M
478   "*Mark used for duplicate articles."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-undownloaded-mark ?@
483   "*Mark used for articles that weren't downloaded."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-downloadable-mark ?%
488   "*Mark used for articles that are to be downloaded."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-unsendable-mark ?=
493   "*Mark used for articles that won't be sent."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-score-over-mark ?+
498   "*Score mark used for articles with high scores."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-score-below-mark ?-
503   "*Score mark used for articles with low scores."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-empty-thread-mark ?     ;Whitespace
508   "*There is no thread under the article."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-not-empty-thread-mark ?=
513   "*There is a thread under the article."
514   :group 'gnus-summary-marks
515   :type 'character)
516
517 (defcustom gnus-view-pseudo-asynchronously nil
518   "*If non-nil, Gnus will view pseudo-articles asynchronously."
519   :group 'gnus-extract-view
520   :type 'boolean)
521
522 (defcustom gnus-auto-expirable-marks
523   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
524         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
525         gnus-souped-mark gnus-duplicate-mark)
526   "*The list of marks converted into expiration if a group is auto-expirable."
527   :version "21.1"
528   :group 'gnus-summary
529   :type '(repeat character))
530
531 (defcustom gnus-inhibit-user-auto-expire t
532   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
533   :version "21.1"
534   :group 'gnus-summary
535   :type 'boolean)
536
537 (defcustom gnus-view-pseudos nil
538   "*If `automatic', pseudo-articles will be viewed automatically.
539 If `not-confirm', pseudos will be viewed automatically, and the user
540 will not be asked to confirm the command."
541   :group 'gnus-extract-view
542   :type '(choice (const :tag "off" nil)
543                  (const automatic)
544                  (const not-confirm)))
545
546 (defcustom gnus-view-pseudos-separately t
547   "*If non-nil, one pseudo-article will be created for each file to be viewed.
548 If nil, all files that use the same viewing command will be given as a
549 list of parameters to that command."
550   :group 'gnus-extract-view
551   :type 'boolean)
552
553 (defcustom gnus-insert-pseudo-articles t
554   "*If non-nil, insert pseudo-articles when decoding articles."
555   :group 'gnus-extract-view
556   :type 'boolean)
557
558 (defcustom gnus-summary-dummy-line-format
559   "  %(:                          :%) %S\n"
560   "*The format specification for the dummy roots in the summary buffer.
561 It works along the same lines as a normal formatting string,
562 with some simple extensions.
563
564 %S  The subject"
565   :group 'gnus-threading
566   :type 'string)
567
568 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
569   "*The format specification for the summary mode line.
570 It works along the same lines as a normal formatting string,
571 with some simple extensions:
572
573 %G  Group name
574 %p  Unprefixed group name
575 %A  Current article number
576 %z  Current article score
577 %V  Gnus version
578 %U  Number of unread articles in the group
579 %e  Number of unselected articles in the group
580 %Z  A string with unread/unselected article counts
581 %g  Shortish group name
582 %S  Subject of the current article
583 %u  User-defined spec
584 %s  Current score file name
585 %d  Number of dormant articles
586 %r  Number of articles that have been marked as read in this session
587 %E  Number of articles expunged by the score files"
588   :group 'gnus-summary-format
589   :type 'string)
590
591 (defcustom gnus-list-identifiers nil
592   "Regexp that matches list identifiers to be removed from subject.
593 This can also be a list of regexps."
594   :version "21.1"
595   :group 'gnus-summary-format
596   :group 'gnus-article-hiding
597   :type '(choice (const :tag "none" nil)
598                  (regexp :value ".*")
599                  (repeat :value (".*") regexp)))
600
601 (defcustom gnus-summary-mark-below 0
602   "*Mark all articles with a score below this variable as read.
603 This variable is local to each summary buffer and usually set by the
604 score file."
605   :group 'gnus-score-default
606   :type 'integer)
607
608 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
609   "*List of functions used for sorting articles in the summary buffer.
610
611 Each function takes two articles and returns non-nil if the first
612 article should be sorted before the other.  If you use more than one
613 function, the primary sort function should be the last.  You should
614 probably always include `gnus-article-sort-by-number' in the list of
615 sorting functions -- preferably first.  Also note that sorting by date
616 is often much slower than sorting by number, and the sorting order is
617 very similar.  (Sorting by date means sorting by the time the message
618 was sent, sorting by number means sorting by arrival time.)
619
620 Ready-made functions include `gnus-article-sort-by-number',
621 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
622 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
623
624 When threading is turned on, the variable `gnus-thread-sort-functions'
625 controls how articles are sorted."
626   :group 'gnus-summary-sort
627   :type '(repeat (choice (function-item gnus-article-sort-by-number)
628                          (function-item gnus-article-sort-by-author)
629                          (function-item gnus-article-sort-by-subject)
630                          (function-item gnus-article-sort-by-date)
631                          (function-item gnus-article-sort-by-score)
632                          (function :tag "other"))))
633
634 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
635   "*List of functions used for sorting threads in the summary buffer.
636 By default, threads are sorted by article number.
637
638 Each function takes two threads and returns non-nil if the first
639 thread should be sorted before the other.  If you use more than one
640 function, the primary sort function should be the last.  You should
641 probably always include `gnus-thread-sort-by-number' in the list of
642 sorting functions -- preferably first.  Also note that sorting by date
643 is often much slower than sorting by number, and the sorting order is
644 very similar.  (Sorting by date means sorting by the time the message
645 was sent, sorting by number means sorting by arrival time.)
646
647 Ready-made functions include `gnus-thread-sort-by-number',
648 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
649 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
650 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
651
652 When threading is turned off, the variable
653 `gnus-article-sort-functions' controls how articles are sorted."
654   :group 'gnus-summary-sort
655   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
656                          (function-item gnus-thread-sort-by-author)
657                          (function-item gnus-thread-sort-by-subject)
658                          (function-item gnus-thread-sort-by-date)
659                          (function-item gnus-thread-sort-by-score)
660                          (function-item gnus-thread-sort-by-total-score)
661                          (function :tag "other"))))
662
663 (defcustom gnus-thread-score-function '+
664   "*Function used for calculating the total score of a thread.
665
666 The function is called with the scores of the article and each
667 subthread and should then return the score of the thread.
668
669 Some functions you can use are `+', `max', or `min'."
670   :group 'gnus-summary-sort
671   :type 'function)
672
673 (defcustom gnus-summary-expunge-below nil
674   "All articles that have a score less than this variable will be expunged.
675 This variable is local to the summary buffers."
676   :group 'gnus-score-default
677   :type '(choice (const :tag "off" nil)
678                  integer))
679
680 (defcustom gnus-thread-expunge-below nil
681   "All threads that have a total score less than this variable will be expunged.
682 See `gnus-thread-score-function' for en explanation of what a
683 \"thread score\" is.
684
685 This variable is local to the summary buffers."
686   :group 'gnus-threading
687   :group 'gnus-score-default
688   :type '(choice (const :tag "off" nil)
689                  integer))
690
691 (defcustom gnus-summary-mode-hook nil
692   "*A hook for Gnus summary mode.
693 This hook is run before any variables are set in the summary buffer."
694   :options '(turn-on-gnus-mailing-list-mode)
695   :group 'gnus-summary-various
696   :type 'hook)
697
698 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
699 (when (featurep 'xemacs)
700   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
701   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
702   (add-hook 'gnus-summary-mode-hook
703             'gnus-xmas-switch-horizontal-scrollbar-off))
704
705 (defcustom gnus-summary-menu-hook nil
706   "*Hook run after the creation of the summary mode menu."
707   :group 'gnus-summary-visual
708   :type 'hook)
709
710 (defcustom gnus-summary-exit-hook nil
711   "*A hook called on exit from the summary buffer.
712 It will be called with point in the group buffer."
713   :group 'gnus-summary-exit
714   :type 'hook)
715
716 (defcustom gnus-summary-prepare-hook nil
717   "*A hook called after the summary buffer has been generated.
718 If you want to modify the summary buffer, you can use this hook."
719   :group 'gnus-summary-various
720   :type 'hook)
721
722 (defcustom gnus-summary-prepared-hook nil
723   "*A hook called as the last thing after the summary buffer has been generated."
724   :group 'gnus-summary-various
725   :type 'hook)
726
727 (defcustom gnus-summary-generate-hook nil
728   "*A hook run just before generating the summary buffer.
729 This hook is commonly used to customize threading variables and the
730 like."
731   :group 'gnus-summary-various
732   :type 'hook)
733
734 (defcustom gnus-select-group-hook nil
735   "*A hook called when a newsgroup is selected.
736
737 If you'd like to simplify subjects like the
738 `gnus-summary-next-same-subject' command does, you can use the
739 following hook:
740
741  (setq gnus-select-group-hook
742       (list
743         (lambda ()
744           (mapcar (lambda (header)
745                      (mail-header-set-subject
746                       header
747                       (gnus-simplify-subject
748                        (mail-header-subject header) 're-only)))
749                   gnus-newsgroup-headers))))"
750   :group 'gnus-group-select
751   :type 'hook)
752
753 (defcustom gnus-select-article-hook nil
754   "*A hook called when an article is selected."
755   :group 'gnus-summary-choose
756   :type 'hook)
757
758 (defcustom gnus-visual-mark-article-hook
759   (list 'gnus-highlight-selected-summary)
760   "*Hook run after selecting an article in the summary buffer.
761 It is meant to be used for highlighting the article in some way.  It
762 is not run if `gnus-visual' is nil."
763   :group 'gnus-summary-visual
764   :type 'hook)
765
766 (defcustom gnus-parse-headers-hook nil
767   "*A hook called before parsing the headers."
768   :group 'gnus-various
769   :type 'hook)
770
771 (defcustom gnus-exit-group-hook nil
772   "*A hook called when exiting summary mode.
773 This hook is not called from the non-updating exit commands like `Q'."
774   :group 'gnus-various
775   :type 'hook)
776
777 (defcustom gnus-summary-update-hook
778   (list 'gnus-summary-highlight-line)
779   "*A hook called when a summary line is changed.
780 The hook will not be called if `gnus-visual' is nil.
781
782 The default function `gnus-summary-highlight-line' will
783 highlight the line according to the `gnus-summary-highlight'
784 variable."
785   :group 'gnus-summary-visual
786   :type 'hook)
787
788 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
789   "*A hook called when an article is selected for the first time.
790 The hook is intended to mark an article as read (or unread)
791 automatically when it is selected."
792   :group 'gnus-summary-choose
793   :type 'hook)
794
795 (defcustom gnus-group-no-more-groups-hook nil
796   "*A hook run when returning to group mode having no more (unread) groups."
797   :group 'gnus-group-select
798   :type 'hook)
799
800 (defcustom gnus-ps-print-hook nil
801   "*A hook run before ps-printing something from Gnus."
802   :group 'gnus-summary
803   :type 'hook)
804
805 (defcustom gnus-summary-display-arrow
806   (and (fboundp 'display-graphic-p)
807        (display-graphic-p))
808   "*If non-nil, display an arrow highlighting the current article."
809   :version "21.1"
810   :group 'gnus-summary
811   :type 'boolean)
812
813 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
814   "Face used for highlighting the current article in the summary buffer."
815   :group 'gnus-summary-visual
816   :type 'face)
817
818 (defcustom gnus-summary-highlight
819   '(((= mark gnus-canceled-mark)
820      . gnus-summary-cancelled-face)
821     ((and (> score default-high)
822           (or (= mark gnus-dormant-mark)
823               (= mark gnus-ticked-mark)))
824      . gnus-summary-high-ticked-face)
825     ((and (< score default-low)
826           (or (= mark gnus-dormant-mark)
827               (= mark gnus-ticked-mark)))
828      . gnus-summary-low-ticked-face)
829     ((or (= mark gnus-dormant-mark)
830          (= mark gnus-ticked-mark))
831      . gnus-summary-normal-ticked-face)
832     ((and (> score default-high) (= mark gnus-ancient-mark))
833      . gnus-summary-high-ancient-face)
834     ((and (< score default-low) (= mark gnus-ancient-mark))
835      . gnus-summary-low-ancient-face)
836     ((= mark gnus-ancient-mark)
837      . gnus-summary-normal-ancient-face)
838     ((and (> score default-high) (= mark gnus-unread-mark))
839      . gnus-summary-high-unread-face)
840     ((and (< score default-low) (= mark gnus-unread-mark))
841      . gnus-summary-low-unread-face)
842     ((= mark gnus-unread-mark)
843      . gnus-summary-normal-unread-face)
844     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
845                                                   gnus-undownloaded-mark)))
846      . gnus-summary-high-unread-face)
847     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
848                                                  gnus-undownloaded-mark)))
849      . gnus-summary-low-unread-face)
850     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
851           (memq article gnus-newsgroup-unreads))
852      . gnus-summary-normal-unread-face)
853     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
854      . gnus-summary-normal-read-face)
855     ((> score default-high)
856      . gnus-summary-high-read-face)
857     ((< score default-low)
858      . gnus-summary-low-read-face)
859     (t
860      . gnus-summary-normal-read-face))
861   "*Controls the highlighting of summary buffer lines.
862
863 A list of (FORM . FACE) pairs.  When deciding how a a particular
864 summary line should be displayed, each form is evaluated.  The content
865 of the face field after the first true form is used.  You can change
866 how those summary lines are displayed, by editing the face field.
867
868 You can use the following variables in the FORM field.
869
870 score:        The article's score
871 default:      The default article score.
872 default-high: The default score for high scored articles.
873 default-low:  The default score for low scored articles.
874 below:        The score below which articles are automatically marked as read.
875 mark:         The articles mark."
876   :group 'gnus-summary-visual
877   :type '(repeat (cons (sexp :tag "Form" nil)
878                        face)))
879
880 (defcustom gnus-alter-header-function nil
881   "Function called to allow alteration of article header structures.
882 The function is called with one parameter, the article header vector,
883 which it may alter in any way.")
884
885 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
886   "Variable that says which function should be used to decode a string with encoded words.")
887
888 (defcustom gnus-extra-headers nil
889   "*Extra headers to parse."
890   :version "21.1"
891   :group 'gnus-summary
892   :type '(repeat symbol))
893
894 (defcustom gnus-ignored-from-addresses
895   (and user-mail-address (regexp-quote user-mail-address))
896   "*Regexp of From headers that may be suppressed in favor of To headers."
897   :version "21.1"
898   :group 'gnus-summary
899   :type 'regexp)
900
901 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
902   "List of charsets that should be ignored.
903 When these charsets are used in the \"charset\" parameter, the
904 default charset will be used instead."
905   :version "21.1"
906   :type '(repeat symbol)
907   :group 'gnus-charset)
908
909 (gnus-define-group-parameter
910  ignored-charsets
911  :type list
912  :function-document
913  "Return the ignored charsets of GROUP."
914  :variable gnus-group-ignored-charsets-alist
915  :variable-default
916  '(("alt\\.chinese\\.text" iso-8859-1))
917  :variable-document
918  "Alist of regexps (to match group names) and charsets that should be ignored.
919 When these charsets are used in the \"charset\" parameter, the
920 default charset will be used instead."
921  :variable-group gnus-charset
922  :variable-type '(repeat (cons (regexp :tag "Group")
923                                (repeat symbol)))
924  :parameter-type '(choice :tag "Ignored charsets"
925                           :value nil
926                           (repeat (symbol)))
927  :parameter-document       "\
928 List of charsets that should be ignored.
929
930 When these charsets are used in the \"charset\" parameter, the
931 default charset will be used instead.")
932
933 (defcustom gnus-group-highlight-words-alist nil
934   "Alist of group regexps and highlight regexps.
935 This variable uses the same syntax as `gnus-emphasis-alist'."
936   :version "21.1"
937   :type '(repeat (cons (regexp :tag "Group")
938                        (repeat (list (regexp :tag "Highlight regexp")
939                                      (number :tag "Group for entire word" 0)
940                                      (number :tag "Group for displayed part" 0)
941                                      (symbol :tag "Face"
942                                              gnus-emphasis-highlight-words)))))
943   :group 'gnus-summary-visual)
944
945 (defcustom gnus-summary-show-article-charset-alist
946   nil
947   "Alist of number and charset.
948 The article will be shown with the charset corresponding to the
949 numbered argument.
950 For example: ((1 . cn-gb-2312) (2 . big5))."
951   :version "21.1"
952   :type '(repeat (cons (number :tag "Argument" 1)
953                        (symbol :tag "Charset")))
954   :group 'gnus-charset)
955
956 (defcustom gnus-preserve-marks t
957   "Whether marks are preserved when moving, copying and respooling messages."
958   :version "21.1"
959   :type 'boolean
960   :group 'gnus-summary-marks)
961
962 (defcustom gnus-alter-articles-to-read-function nil
963   "Function to be called to alter the list of articles to be selected."
964   :type '(choice (const nil) function)
965   :group 'gnus-summary)
966
967 (defcustom gnus-orphan-score nil
968   "*All orphans get this score added.  Set in the score file."
969   :group 'gnus-score-default
970   :type '(choice (const nil)
971                  integer))
972
973 (defcustom gnus-summary-save-parts-default-mime "image/.*"
974   "*A regexp to match MIME parts when saving multiple parts of a message
975 with gnus-summary-save-parts (X m). This regexp will be used by default
976 when prompting the user for which type of files to save."
977   :group 'gnus-summary
978   :type 'regexp)
979
980
981 (defcustom gnus-summary-save-parts-default-mime "image/.*"
982   "*A regexp to match MIME parts when saving multiple parts of a message
983 with gnus-summary-save-parts (X m). This regexp will be used by default
984 when prompting the user for which type of files to save."
985   :group 'gnus-summary
986   :type 'regexp)
987
988 (defcustom gnus-read-all-available-headers nil
989   "Whether Gnus should parse all headers made available to it.
990 This is mostly relevant for slow backends where the user may
991 wish to widen the summary buffer to include all headers
992 that were fetched.  Say, for nnultimate groups."
993   :group 'gnus-summary
994   :type '(choice boolean regexp))
995
996 ;;; Internal variables
997
998 (defvar gnus-article-mime-handles nil)
999 (defvar gnus-article-decoded-p nil)
1000 (defvar gnus-article-charset nil)
1001 (defvar gnus-article-ignored-charsets nil)
1002 (defvar gnus-scores-exclude-files nil)
1003 (defvar gnus-page-broken nil)
1004 (defvar gnus-inhibit-mime-unbuttonizing nil)
1005
1006 (defvar gnus-original-article nil)
1007 (defvar gnus-article-internal-prepare-hook nil)
1008 (defvar gnus-newsgroup-process-stack nil)
1009
1010 (defvar gnus-thread-indent-array nil)
1011 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1012 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1013   "Function called to sort the articles within a thread after it has been gathered together.")
1014
1015 (defvar gnus-summary-save-parts-type-history nil)
1016 (defvar gnus-summary-save-parts-last-directory nil)
1017
1018 (defvar gnus-summary-save-parts-type-history nil)
1019 (defvar gnus-summary-save-parts-last-directory nil)
1020
1021 ;; Avoid highlighting in kill files.
1022 (defvar gnus-summary-inhibit-highlight nil)
1023 (defvar gnus-newsgroup-selected-overlay nil)
1024 (defvar gnus-inhibit-limiting nil)
1025 (defvar gnus-newsgroup-adaptive-score-file nil)
1026 (defvar gnus-current-score-file nil)
1027 (defvar gnus-current-move-group nil)
1028 (defvar gnus-current-copy-group nil)
1029 (defvar gnus-current-crosspost-group nil)
1030
1031 (defvar gnus-newsgroup-dependencies nil)
1032 (defvar gnus-newsgroup-adaptive nil)
1033 (defvar gnus-summary-display-article-function nil)
1034 (defvar gnus-summary-highlight-line-function nil
1035   "Function called after highlighting a summary line.")
1036
1037 (defvar gnus-summary-line-format-alist
1038   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1039     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1040     (?s gnus-tmp-subject-or-nil ?s)
1041     (?n gnus-tmp-name ?s)
1042     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1043         ?s)
1044     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1045             gnus-tmp-from) ?s)
1046     (?F gnus-tmp-from ?s)
1047     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1048     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1049     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1050     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1051     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1052     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1053     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1054     (?L gnus-tmp-lines ?s)
1055     (?I gnus-tmp-indentation ?s)
1056     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1057     (?R gnus-tmp-replied ?c)
1058     (?\[ gnus-tmp-opening-bracket ?c)
1059     (?\] gnus-tmp-closing-bracket ?c)
1060     (?\> (make-string gnus-tmp-level ? ) ?s)
1061     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1062     (?i gnus-tmp-score ?d)
1063     (?z gnus-tmp-score-char ?c)
1064     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1065     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1066     (?U gnus-tmp-unread ?c)
1067     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1068     (?t (gnus-summary-number-of-articles-in-thread
1069          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1070         ?d)
1071     (?e (gnus-summary-number-of-articles-in-thread
1072          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1073         ?c)
1074     (?u gnus-tmp-user-defined ?s)
1075     (?P (gnus-pick-line-number) ?d)
1076     (?B gnus-tmp-thread-tree-header-string ?s))
1077   "An alist of format specifications that can appear in summary lines.
1078 These are paired with what variables they correspond with, along with
1079 the type of the variable (string, integer, character, etc).")
1080
1081 (defvar gnus-summary-dummy-line-format-alist
1082   `((?S gnus-tmp-subject ?s)
1083     (?N gnus-tmp-number ?d)
1084     (?u gnus-tmp-user-defined ?s)))
1085
1086 (defvar gnus-summary-mode-line-format-alist
1087   `((?G gnus-tmp-group-name ?s)
1088     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1089     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1090     (?A gnus-tmp-article-number ?d)
1091     (?Z gnus-tmp-unread-and-unselected ?s)
1092     (?V gnus-version ?s)
1093     (?U gnus-tmp-unread-and-unticked ?d)
1094     (?S gnus-tmp-subject ?s)
1095     (?e gnus-tmp-unselected ?d)
1096     (?u gnus-tmp-user-defined ?s)
1097     (?d (length gnus-newsgroup-dormant) ?d)
1098     (?t (length gnus-newsgroup-marked) ?d)
1099     (?r (length gnus-newsgroup-reads) ?d)
1100     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1101     (?E gnus-newsgroup-expunged-tally ?d)
1102     (?s (gnus-current-score-file-nondirectory) ?s)))
1103
1104 (defvar gnus-last-search-regexp nil
1105   "Default regexp for article search command.")
1106
1107 (defvar gnus-last-shell-command nil
1108   "Default shell command on article.")
1109
1110 (defvar gnus-newsgroup-begin nil)
1111 (defvar gnus-newsgroup-end nil)
1112 (defvar gnus-newsgroup-last-rmail nil)
1113 (defvar gnus-newsgroup-last-mail nil)
1114 (defvar gnus-newsgroup-last-folder nil)
1115 (defvar gnus-newsgroup-last-file nil)
1116 (defvar gnus-newsgroup-auto-expire nil)
1117 (defvar gnus-newsgroup-active nil)
1118
1119 (defvar gnus-newsgroup-data nil)
1120 (defvar gnus-newsgroup-data-reverse nil)
1121 (defvar gnus-newsgroup-limit nil)
1122 (defvar gnus-newsgroup-limits nil)
1123
1124 (defvar gnus-newsgroup-unreads nil
1125   "List of unread articles in the current newsgroup.")
1126
1127 (defvar gnus-newsgroup-unselected nil
1128   "List of unselected unread articles in the current newsgroup.")
1129
1130 (defvar gnus-newsgroup-reads nil
1131   "Alist of read articles and article marks in the current newsgroup.")
1132
1133 (defvar gnus-newsgroup-expunged-tally nil)
1134
1135 (defvar gnus-newsgroup-marked nil
1136   "List of ticked articles in the current newsgroup (a subset of unread art).")
1137
1138 (defvar gnus-newsgroup-killed nil
1139   "List of ranges of articles that have been through the scoring process.")
1140
1141 (defvar gnus-newsgroup-cached nil
1142   "List of articles that come from the article cache.")
1143
1144 (defvar gnus-newsgroup-saved nil
1145   "List of articles that have been saved.")
1146
1147 (defvar gnus-newsgroup-kill-headers nil)
1148
1149 (defvar gnus-newsgroup-replied nil
1150   "List of articles that have been replied to in the current newsgroup.")
1151
1152 (defvar gnus-newsgroup-forwarded nil
1153   "List of articles that have been forwarded in the current newsgroup.")
1154
1155 (defvar gnus-newsgroup-recent nil
1156   "List of articles that have are recent in the current newsgroup.")
1157
1158 (defvar gnus-newsgroup-expirable nil
1159   "List of articles in the current newsgroup that can be expired.")
1160
1161 (defvar gnus-newsgroup-processable nil
1162   "List of articles in the current newsgroup that can be processed.")
1163
1164 (defvar gnus-newsgroup-downloadable nil
1165   "List of articles in the current newsgroup that can be processed.")
1166
1167 (defvar gnus-newsgroup-undownloaded nil
1168   "List of articles in the current newsgroup that haven't been downloaded..")
1169
1170 (defvar gnus-newsgroup-unsendable nil
1171   "List of articles in the current newsgroup that won't be sent.")
1172
1173 (defvar gnus-newsgroup-bookmarks nil
1174   "List of articles in the current newsgroup that have bookmarks.")
1175
1176 (defvar gnus-newsgroup-dormant nil
1177   "List of dormant articles in the current newsgroup.")
1178
1179 (defvar gnus-newsgroup-scored nil
1180   "List of scored articles in the current newsgroup.")
1181
1182 (defvar gnus-newsgroup-headers nil
1183   "List of article headers in the current newsgroup.")
1184
1185 (defvar gnus-newsgroup-threads nil)
1186
1187 (defvar gnus-newsgroup-prepared nil
1188   "Whether the current group has been prepared properly.")
1189
1190 (defvar gnus-newsgroup-ancient nil
1191   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1192
1193 (defvar gnus-newsgroup-sparse nil)
1194
1195 (defvar gnus-current-article nil)
1196 (defvar gnus-article-current nil)
1197 (defvar gnus-current-headers nil)
1198 (defvar gnus-have-all-headers nil)
1199 (defvar gnus-last-article nil)
1200 (defvar gnus-newsgroup-history nil)
1201 (defvar gnus-newsgroup-charset nil)
1202 (defvar gnus-newsgroup-ephemeral-charset nil)
1203 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1204
1205 (defvar gnus-article-before-search nil)
1206
1207 (defconst gnus-summary-local-variables
1208   '(gnus-newsgroup-name
1209     gnus-newsgroup-begin gnus-newsgroup-end
1210     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1211     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1212     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1213     gnus-newsgroup-unselected gnus-newsgroup-marked
1214     gnus-newsgroup-reads gnus-newsgroup-saved
1215     gnus-newsgroup-replied gnus-newsgroup-forwarded
1216     gnus-newsgroup-recent
1217     gnus-newsgroup-expirable
1218     gnus-newsgroup-processable gnus-newsgroup-killed
1219     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1220     gnus-newsgroup-unsendable
1221     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1222     gnus-newsgroup-headers gnus-newsgroup-threads
1223     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1224     gnus-current-article gnus-current-headers gnus-have-all-headers
1225     gnus-last-article gnus-article-internal-prepare-hook
1226     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1227     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1228     gnus-thread-expunge-below
1229     gnus-score-alist gnus-current-score-file
1230     (gnus-summary-expunge-below . global)
1231     (gnus-summary-mark-below . global)
1232     (gnus-orphan-score . global)
1233     gnus-newsgroup-active gnus-scores-exclude-files
1234     gnus-newsgroup-history gnus-newsgroup-ancient
1235     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1236     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1237     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1238     (gnus-newsgroup-expunged-tally . 0)
1239     gnus-cache-removable-articles gnus-newsgroup-cached
1240     gnus-newsgroup-data gnus-newsgroup-data-reverse
1241     gnus-newsgroup-limit gnus-newsgroup-limits
1242     gnus-newsgroup-charset)
1243   "Variables that are buffer-local to the summary buffers.")
1244
1245 (defvar gnus-newsgroup-variables nil
1246   "Variables that have separate values in the newsgroups.")
1247
1248 ;; Byte-compiler warning.
1249 (eval-when-compile (defvar gnus-article-mode-map))
1250
1251 ;; MIME stuff.
1252
1253 (defvar gnus-decode-encoded-word-methods
1254   '(mail-decode-encoded-word-string)
1255   "List of methods used to decode encoded words.
1256
1257 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1258 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1259 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1260 whose names match REGEXP.
1261
1262 For example:
1263 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1264  mail-decode-encoded-word-string
1265  (\"chinese\" . rfc1843-decode-string))")
1266
1267 (defvar gnus-decode-encoded-word-methods-cache nil)
1268
1269 (defun gnus-multi-decode-encoded-word-string (string)
1270   "Apply the functions from `gnus-encoded-word-methods' that match."
1271   (unless (and gnus-decode-encoded-word-methods-cache
1272                (eq gnus-newsgroup-name
1273                    (car gnus-decode-encoded-word-methods-cache)))
1274     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1275     (mapcar (lambda (x)
1276               (if (symbolp x)
1277                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1278                 (if (and gnus-newsgroup-name
1279                          (string-match (car x) gnus-newsgroup-name))
1280                     (nconc gnus-decode-encoded-word-methods-cache
1281                            (list (cdr x))))))
1282             gnus-decode-encoded-word-methods))
1283   (let ((xlist gnus-decode-encoded-word-methods-cache))
1284     (pop xlist)
1285     (while xlist
1286       (setq string (funcall (pop xlist) string))))
1287   string)
1288
1289 ;; Subject simplification.
1290
1291 (defun gnus-simplify-whitespace (str)
1292   "Remove excessive whitespace from STR."
1293   (let ((mystr str))
1294     ;; Multiple spaces.
1295     (while (string-match "[ \t][ \t]+" mystr)
1296       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1297                           " "
1298                           (substring mystr (match-end 0)))))
1299     ;; Leading spaces.
1300     (when (string-match "^[ \t]+" mystr)
1301       (setq mystr (substring mystr (match-end 0))))
1302     ;; Trailing spaces.
1303     (when (string-match "[ \t]+$" mystr)
1304       (setq mystr (substring mystr 0 (match-beginning 0))))
1305     mystr))
1306
1307 (defsubst gnus-simplify-subject-re (subject)
1308   "Remove \"Re:\" from subject lines."
1309   (if (string-match message-subject-re-regexp subject)
1310       (substring subject (match-end 0))
1311     subject))
1312
1313 (defun gnus-simplify-subject (subject &optional re-only)
1314   "Remove `Re:' and words in parentheses.
1315 If RE-ONLY is non-nil, strip leading `Re:'s only."
1316   (let ((case-fold-search t))           ;Ignore case.
1317     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1318     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1319       (setq subject (substring subject (match-end 0))))
1320     ;; Remove uninteresting prefixes.
1321     (when (and (not re-only)
1322                gnus-simplify-ignored-prefixes
1323                (string-match gnus-simplify-ignored-prefixes subject))
1324       (setq subject (substring subject (match-end 0))))
1325     ;; Remove words in parentheses from end.
1326     (unless re-only
1327       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1328         (setq subject (substring subject 0 (match-beginning 0)))))
1329     ;; Return subject string.
1330     subject))
1331
1332 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1333 ;; all whitespace.
1334 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1335   (goto-char (point-min))
1336   (while (re-search-forward regexp nil t)
1337     (replace-match (or newtext ""))))
1338
1339 (defun gnus-simplify-buffer-fuzzy ()
1340   "Simplify string in the buffer fuzzily.
1341 The string in the accessible portion of the current buffer is simplified.
1342 It is assumed to be a single-line subject.
1343 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1344 matter is removed.  Additional things can be deleted by setting
1345 `gnus-simplify-subject-fuzzy-regexp'."
1346   (let ((case-fold-search t)
1347         (modified-tick))
1348     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1349
1350     (while (not (eq modified-tick (buffer-modified-tick)))
1351       (setq modified-tick (buffer-modified-tick))
1352       (cond
1353        ((listp gnus-simplify-subject-fuzzy-regexp)
1354         (mapcar 'gnus-simplify-buffer-fuzzy-step
1355                 gnus-simplify-subject-fuzzy-regexp))
1356        (gnus-simplify-subject-fuzzy-regexp
1357         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1358       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1359       (gnus-simplify-buffer-fuzzy-step
1360        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1361       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1362
1363     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1364     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1365     (gnus-simplify-buffer-fuzzy-step " $")
1366     (gnus-simplify-buffer-fuzzy-step "^ +")))
1367
1368 (defun gnus-simplify-subject-fuzzy (subject)
1369   "Simplify a subject string fuzzily.
1370 See `gnus-simplify-buffer-fuzzy' for details."
1371   (save-excursion
1372     (gnus-set-work-buffer)
1373     (let ((case-fold-search t))
1374       ;; Remove uninteresting prefixes.
1375       (when (and gnus-simplify-ignored-prefixes
1376                  (string-match gnus-simplify-ignored-prefixes subject))
1377         (setq subject (substring subject (match-end 0))))
1378       (insert subject)
1379       (inline (gnus-simplify-buffer-fuzzy))
1380       (buffer-string))))
1381
1382 (defsubst gnus-simplify-subject-fully (subject)
1383   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1384   (cond
1385    (gnus-simplify-subject-functions
1386     (gnus-map-function gnus-simplify-subject-functions subject))
1387    ((null gnus-summary-gather-subject-limit)
1388     (gnus-simplify-subject-re subject))
1389    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1390     (gnus-simplify-subject-fuzzy subject))
1391    ((numberp gnus-summary-gather-subject-limit)
1392     (gnus-limit-string (gnus-simplify-subject-re subject)
1393                        gnus-summary-gather-subject-limit))
1394    (t
1395     subject)))
1396
1397 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1398   "Check whether two subjects are equal.
1399 If optional argument simple-first is t, first argument is already
1400 simplified."
1401   (cond
1402    ((null simple-first)
1403     (equal (gnus-simplify-subject-fully s1)
1404            (gnus-simplify-subject-fully s2)))
1405    (t
1406     (equal s1
1407            (gnus-simplify-subject-fully s2)))))
1408
1409 (defun gnus-summary-bubble-group ()
1410   "Increase the score of the current group.
1411 This is a handy function to add to `gnus-summary-exit-hook' to
1412 increase the score of each group you read."
1413   (gnus-group-add-score gnus-newsgroup-name))
1414
1415 \f
1416 ;;;
1417 ;;; Gnus summary mode
1418 ;;;
1419
1420 (put 'gnus-summary-mode 'mode-class 'special)
1421
1422 (defvar gnus-article-commands-menu)
1423
1424 (when t
1425   ;; Non-orthogonal keys
1426
1427   (gnus-define-keys gnus-summary-mode-map
1428     " " gnus-summary-next-page
1429     "\177" gnus-summary-prev-page
1430     [delete] gnus-summary-prev-page
1431     [backspace] gnus-summary-prev-page
1432     "\r" gnus-summary-scroll-up
1433     "\M-\r" gnus-summary-scroll-down
1434     "n" gnus-summary-next-unread-article
1435     "p" gnus-summary-prev-unread-article
1436     "N" gnus-summary-next-article
1437     "P" gnus-summary-prev-article
1438     "\M-\C-n" gnus-summary-next-same-subject
1439     "\M-\C-p" gnus-summary-prev-same-subject
1440     "\M-n" gnus-summary-next-unread-subject
1441     "\M-p" gnus-summary-prev-unread-subject
1442     "." gnus-summary-first-unread-article
1443     "," gnus-summary-best-unread-article
1444     "\M-s" gnus-summary-search-article-forward
1445     "\M-r" gnus-summary-search-article-backward
1446     "<" gnus-summary-beginning-of-article
1447     ">" gnus-summary-end-of-article
1448     "j" gnus-summary-goto-article
1449     "^" gnus-summary-refer-parent-article
1450     "\M-^" gnus-summary-refer-article
1451     "u" gnus-summary-tick-article-forward
1452     "!" gnus-summary-tick-article-forward
1453     "U" gnus-summary-tick-article-backward
1454     "d" gnus-summary-mark-as-read-forward
1455     "D" gnus-summary-mark-as-read-backward
1456     "E" gnus-summary-mark-as-expirable
1457     "\M-u" gnus-summary-clear-mark-forward
1458     "\M-U" gnus-summary-clear-mark-backward
1459     "k" gnus-summary-kill-same-subject-and-select
1460     "\C-k" gnus-summary-kill-same-subject
1461     "\M-\C-k" gnus-summary-kill-thread
1462     "\M-\C-l" gnus-summary-lower-thread
1463     "e" gnus-summary-edit-article
1464     "#" gnus-summary-mark-as-processable
1465     "\M-#" gnus-summary-unmark-as-processable
1466     "\M-\C-t" gnus-summary-toggle-threads
1467     "\M-\C-s" gnus-summary-show-thread
1468     "\M-\C-h" gnus-summary-hide-thread
1469     "\M-\C-f" gnus-summary-next-thread
1470     "\M-\C-b" gnus-summary-prev-thread
1471     [(meta down)] gnus-summary-next-thread
1472     [(meta up)] gnus-summary-prev-thread
1473     "\M-\C-u" gnus-summary-up-thread
1474     "\M-\C-d" gnus-summary-down-thread
1475     "&" gnus-summary-execute-command
1476     "c" gnus-summary-catchup-and-exit
1477     "\C-w" gnus-summary-mark-region-as-read
1478     "\C-t" gnus-summary-toggle-truncation
1479     "?" gnus-summary-mark-as-dormant
1480     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1481     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1482     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1483     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1484     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1485     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1486     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1487     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1488     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1489     "=" gnus-summary-expand-window
1490     "\C-x\C-s" gnus-summary-reselect-current-group
1491     "\M-g" gnus-summary-rescan-group
1492     "w" gnus-summary-stop-page-breaking
1493     "\C-c\C-r" gnus-summary-caesar-message
1494     "f" gnus-summary-followup
1495     "F" gnus-summary-followup-with-original
1496     "C" gnus-summary-cancel-article
1497     "r" gnus-summary-reply
1498     "R" gnus-summary-reply-with-original
1499     "\C-c\C-f" gnus-summary-mail-forward
1500     "o" gnus-summary-save-article
1501     "\C-o" gnus-summary-save-article-mail
1502     "|" gnus-summary-pipe-output
1503     "\M-k" gnus-summary-edit-local-kill
1504     "\M-K" gnus-summary-edit-global-kill
1505     ;; "V" gnus-version
1506     "\C-c\C-d" gnus-summary-describe-group
1507     "q" gnus-summary-exit
1508     "Q" gnus-summary-exit-no-update
1509     "\C-c\C-i" gnus-info-find-node
1510     gnus-mouse-2 gnus-mouse-pick-article
1511     "m" gnus-summary-mail-other-window
1512     "a" gnus-summary-post-news
1513     "x" gnus-summary-limit-to-unread
1514     "s" gnus-summary-isearch-article
1515     "t" gnus-summary-toggle-header
1516     "g" gnus-summary-show-article
1517     "l" gnus-summary-goto-last-article
1518     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1519     "\C-d" gnus-summary-enter-digest-group
1520     "\M-\C-d" gnus-summary-read-document
1521     "\M-\C-e" gnus-summary-edit-parameters
1522     "\M-\C-a" gnus-summary-customize-parameters
1523     "\C-c\C-b" gnus-bug
1524     "*" gnus-cache-enter-article
1525     "\M-*" gnus-cache-remove-article
1526     "\M-&" gnus-summary-universal-argument
1527     "\C-l" gnus-recenter
1528     "I" gnus-summary-increase-score
1529     "L" gnus-summary-lower-score
1530     "\M-i" gnus-symbolic-argument
1531     "h" gnus-summary-select-article-buffer
1532
1533     "b" gnus-article-view-part
1534     "\M-t" gnus-summary-toggle-display-buttonized
1535
1536     "V" gnus-summary-score-map
1537     "X" gnus-uu-extract-map
1538     "S" gnus-summary-send-map)
1539
1540   ;; Sort of orthogonal keymap
1541   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1542     "t" gnus-summary-tick-article-forward
1543     "!" gnus-summary-tick-article-forward
1544     "d" gnus-summary-mark-as-read-forward
1545     "r" gnus-summary-mark-as-read-forward
1546     "c" gnus-summary-clear-mark-forward
1547     " " gnus-summary-clear-mark-forward
1548     "e" gnus-summary-mark-as-expirable
1549     "x" gnus-summary-mark-as-expirable
1550     "?" gnus-summary-mark-as-dormant
1551     "b" gnus-summary-set-bookmark
1552     "B" gnus-summary-remove-bookmark
1553     "#" gnus-summary-mark-as-processable
1554     "\M-#" gnus-summary-unmark-as-processable
1555     "S" gnus-summary-limit-include-expunged
1556     "C" gnus-summary-catchup
1557     "H" gnus-summary-catchup-to-here
1558     "h" gnus-summary-catchup-from-here
1559     "\C-c" gnus-summary-catchup-all
1560     "k" gnus-summary-kill-same-subject-and-select
1561     "K" gnus-summary-kill-same-subject
1562     "P" gnus-uu-mark-map)
1563
1564   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1565     "c" gnus-summary-clear-above
1566     "u" gnus-summary-tick-above
1567     "m" gnus-summary-mark-above
1568     "k" gnus-summary-kill-below)
1569
1570   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1571     "/" gnus-summary-limit-to-subject
1572     "n" gnus-summary-limit-to-articles
1573     "w" gnus-summary-pop-limit
1574     "s" gnus-summary-limit-to-subject
1575     "a" gnus-summary-limit-to-author
1576     "u" gnus-summary-limit-to-unread
1577     "m" gnus-summary-limit-to-marks
1578     "M" gnus-summary-limit-exclude-marks
1579     "v" gnus-summary-limit-to-score
1580     "*" gnus-summary-limit-include-cached
1581     "D" gnus-summary-limit-include-dormant
1582     "T" gnus-summary-limit-include-thread
1583     "d" gnus-summary-limit-exclude-dormant
1584     "t" gnus-summary-limit-to-age
1585     "x" gnus-summary-limit-to-extra
1586     "E" gnus-summary-limit-include-expunged
1587     "c" gnus-summary-limit-exclude-childless-dormant
1588     "C" gnus-summary-limit-mark-excluded-as-read
1589     "o" gnus-summary-insert-old-articles
1590     "N" gnus-summary-insert-new-articles)
1591
1592   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1593     "n" gnus-summary-next-unread-article
1594     "p" gnus-summary-prev-unread-article
1595     "N" gnus-summary-next-article
1596     "P" gnus-summary-prev-article
1597     "\C-n" gnus-summary-next-same-subject
1598     "\C-p" gnus-summary-prev-same-subject
1599     "\M-n" gnus-summary-next-unread-subject
1600     "\M-p" gnus-summary-prev-unread-subject
1601     "f" gnus-summary-first-unread-article
1602     "b" gnus-summary-best-unread-article
1603     "j" gnus-summary-goto-article
1604     "g" gnus-summary-goto-subject
1605     "l" gnus-summary-goto-last-article
1606     "o" gnus-summary-pop-article)
1607
1608   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1609     "k" gnus-summary-kill-thread
1610     "l" gnus-summary-lower-thread
1611     "i" gnus-summary-raise-thread
1612     "T" gnus-summary-toggle-threads
1613     "t" gnus-summary-rethread-current
1614     "^" gnus-summary-reparent-thread
1615     "s" gnus-summary-show-thread
1616     "S" gnus-summary-show-all-threads
1617     "h" gnus-summary-hide-thread
1618     "H" gnus-summary-hide-all-threads
1619     "n" gnus-summary-next-thread
1620     "p" gnus-summary-prev-thread
1621     "u" gnus-summary-up-thread
1622     "o" gnus-summary-top-thread
1623     "d" gnus-summary-down-thread
1624     "#" gnus-uu-mark-thread
1625     "\M-#" gnus-uu-unmark-thread)
1626
1627   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1628     "g" gnus-summary-prepare
1629     "c" gnus-summary-insert-cached-articles)
1630
1631   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1632     "c" gnus-summary-catchup-and-exit
1633     "C" gnus-summary-catchup-all-and-exit
1634     "E" gnus-summary-exit-no-update
1635     "Q" gnus-summary-exit
1636     "Z" gnus-summary-exit
1637     "n" gnus-summary-catchup-and-goto-next-group
1638     "R" gnus-summary-reselect-current-group
1639     "G" gnus-summary-rescan-group
1640     "N" gnus-summary-next-group
1641     "s" gnus-summary-save-newsrc
1642     "P" gnus-summary-prev-group)
1643
1644   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1645     " " gnus-summary-next-page
1646     "n" gnus-summary-next-page
1647     "\177" gnus-summary-prev-page
1648     [delete] gnus-summary-prev-page
1649     "p" gnus-summary-prev-page
1650     "\r" gnus-summary-scroll-up
1651     "\M-\r" gnus-summary-scroll-down
1652     "<" gnus-summary-beginning-of-article
1653     ">" gnus-summary-end-of-article
1654     "b" gnus-summary-beginning-of-article
1655     "e" gnus-summary-end-of-article
1656     "^" gnus-summary-refer-parent-article
1657     "r" gnus-summary-refer-parent-article
1658     "D" gnus-summary-enter-digest-group
1659     "R" gnus-summary-refer-references
1660     "T" gnus-summary-refer-thread
1661     "g" gnus-summary-show-article
1662     "s" gnus-summary-isearch-article
1663     "P" gnus-summary-print-article
1664     "M" gnus-mailing-list-insinuate
1665     "t" gnus-article-babel)
1666
1667   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1668     "b" gnus-article-add-buttons
1669     "B" gnus-article-add-buttons-to-head
1670     "o" gnus-article-treat-overstrike
1671     "e" gnus-article-emphasize
1672     "w" gnus-article-fill-cited-article
1673     "Q" gnus-article-fill-long-lines
1674     "C" gnus-article-capitalize-sentences
1675     "c" gnus-article-remove-cr
1676     "q" gnus-article-de-quoted-unreadable
1677     "6" gnus-article-de-base64-unreadable
1678     "Z" gnus-article-decode-HZ
1679     "h" gnus-article-wash-html
1680     "s" gnus-summary-force-verify-and-decrypt
1681     "f" gnus-article-display-x-face
1682     "l" gnus-summary-stop-page-breaking
1683     "r" gnus-summary-caesar-message
1684     "t" gnus-summary-toggle-header
1685     "v" gnus-summary-verbose-headers
1686     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1687     "p" gnus-article-verify-x-pgp-sig
1688     "d" gnus-article-treat-dumbquotes)
1689
1690   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1691     "a" gnus-article-hide
1692     "h" gnus-article-hide-headers
1693     "b" gnus-article-hide-boring-headers
1694     "s" gnus-article-hide-signature
1695     "c" gnus-article-hide-citation
1696     "C" gnus-article-hide-citation-in-followups
1697     "l" gnus-article-hide-list-identifiers
1698     "p" gnus-article-hide-pgp
1699     "B" gnus-article-strip-banner
1700     "P" gnus-article-hide-pem
1701     "\C-c" gnus-article-hide-citation-maybe)
1702
1703   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1704     "a" gnus-article-highlight
1705     "h" gnus-article-highlight-headers
1706     "c" gnus-article-highlight-citation
1707     "s" gnus-article-highlight-signature)
1708
1709   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1710     "w" gnus-article-decode-mime-words
1711     "c" gnus-article-decode-charset
1712     "v" gnus-mime-view-all-parts
1713     "b" gnus-article-view-part)
1714
1715   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1716     "z" gnus-article-date-ut
1717     "u" gnus-article-date-ut
1718     "l" gnus-article-date-local
1719     "p" gnus-article-date-english
1720     "e" gnus-article-date-lapsed
1721     "o" gnus-article-date-original
1722     "i" gnus-article-date-iso8601
1723     "s" gnus-article-date-user)
1724
1725   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1726     "t" gnus-article-remove-trailing-blank-lines
1727     "l" gnus-article-strip-leading-blank-lines
1728     "m" gnus-article-strip-multiple-blank-lines
1729     "a" gnus-article-strip-blank-lines
1730     "A" gnus-article-strip-all-blank-lines
1731     "s" gnus-article-strip-leading-space
1732     "e" gnus-article-strip-trailing-space
1733     "w" gnus-article-remove-leading-whitespace)
1734
1735   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1736     "v" gnus-version
1737     "f" gnus-summary-fetch-faq
1738     "d" gnus-summary-describe-group
1739     "h" gnus-summary-describe-briefly
1740     "i" gnus-info-find-node)
1741
1742   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1743     "e" gnus-summary-expire-articles
1744     "\M-\C-e" gnus-summary-expire-articles-now
1745     "\177" gnus-summary-delete-article
1746     [delete] gnus-summary-delete-article
1747     [backspace] gnus-summary-delete-article
1748     "m" gnus-summary-move-article
1749     "r" gnus-summary-respool-article
1750     "w" gnus-summary-edit-article
1751     "c" gnus-summary-copy-article
1752     "B" gnus-summary-crosspost-article
1753     "q" gnus-summary-respool-query
1754     "t" gnus-summary-respool-trace
1755     "i" gnus-summary-import-article
1756     "I" gnus-summary-create-article
1757     "p" gnus-summary-article-posted-p)
1758
1759   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1760     "o" gnus-summary-save-article
1761     "m" gnus-summary-save-article-mail
1762     "F" gnus-summary-write-article-file
1763     "r" gnus-summary-save-article-rmail
1764     "f" gnus-summary-save-article-file
1765     "b" gnus-summary-save-article-body-file
1766     "h" gnus-summary-save-article-folder
1767     "v" gnus-summary-save-article-vm
1768     "p" gnus-summary-pipe-output
1769     "s" gnus-soup-add-article)
1770
1771   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1772     "b" gnus-summary-display-buttonized
1773     "m" gnus-summary-repair-multipart
1774     "v" gnus-article-view-part
1775     "o" gnus-article-save-part
1776     "c" gnus-article-copy-part
1777     "C" gnus-article-view-part-as-charset
1778     "e" gnus-article-externalize-part
1779     "E" gnus-article-encrypt-body
1780     "i" gnus-article-inline-part
1781     "|" gnus-article-pipe-part))
1782
1783 (defvar gnus-article-post-menu nil)
1784
1785 (defun gnus-summary-make-menu-bar ()
1786   (gnus-turn-off-edit-menu 'summary)
1787
1788   (unless (boundp 'gnus-summary-misc-menu)
1789
1790     (easy-menu-define
1791       gnus-summary-kill-menu gnus-summary-mode-map ""
1792       (cons
1793        "Score"
1794        (nconc
1795         (list
1796          ["Customize" gnus-score-customize t])
1797         (gnus-make-score-map 'increase)
1798         (gnus-make-score-map 'lower)
1799         '(("Mark"
1800            ["Kill below" gnus-summary-kill-below t]
1801            ["Mark above" gnus-summary-mark-above t]
1802            ["Tick above" gnus-summary-tick-above t]
1803            ["Clear above" gnus-summary-clear-above t])
1804           ["Current score" gnus-summary-current-score t]
1805           ["Set score" gnus-summary-set-score t]
1806           ["Switch current score file..." gnus-score-change-score-file t]
1807           ["Set mark below..." gnus-score-set-mark-below t]
1808           ["Set expunge below..." gnus-score-set-expunge-below t]
1809           ["Edit current score file" gnus-score-edit-current-scores t]
1810           ["Edit score file" gnus-score-edit-file t]
1811           ["Trace score" gnus-score-find-trace t]
1812           ["Find words" gnus-score-find-favourite-words t]
1813           ["Rescore buffer" gnus-summary-rescore t]
1814           ["Increase score..." gnus-summary-increase-score t]
1815           ["Lower score..." gnus-summary-lower-score t]))))
1816
1817 ;; Define both the Article menu in the summary buffer and the equivalent
1818     ;; Commands menu in the article buffer here for consistency.
1819     (let ((innards
1820            `(("Hide"
1821               ["All" gnus-article-hide t]
1822               ["Headers" gnus-article-hide-headers t]
1823               ["Signature" gnus-article-hide-signature t]
1824               ["Citation" gnus-article-hide-citation t]
1825               ["List identifiers" gnus-article-hide-list-identifiers t]
1826               ["PGP" gnus-article-hide-pgp t]
1827               ["Banner" gnus-article-strip-banner t]
1828               ["Boring headers" gnus-article-hide-boring-headers t])
1829              ("Highlight"
1830               ["All" gnus-article-highlight t]
1831               ["Headers" gnus-article-highlight-headers t]
1832               ["Signature" gnus-article-highlight-signature t]
1833               ["Citation" gnus-article-highlight-citation t])
1834              ("MIME"
1835               ["Words" gnus-article-decode-mime-words t]
1836               ["Charset" gnus-article-decode-charset t]
1837               ["QP" gnus-article-de-quoted-unreadable t]
1838               ["Base64" gnus-article-de-base64-unreadable t]
1839               ["View all" gnus-mime-view-all-parts t]
1840               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
1841               ["Encrypt body" gnus-article-encrypt-body t])
1842              ("Date"
1843               ["Local" gnus-article-date-local t]
1844               ["ISO8601" gnus-article-date-iso8601 t]
1845               ["UT" gnus-article-date-ut t]
1846               ["Original" gnus-article-date-original t]
1847               ["Lapsed" gnus-article-date-lapsed t]
1848               ["User-defined" gnus-article-date-user t])
1849              ("Washing"
1850               ("Remove Blanks"
1851                ["Leading" gnus-article-strip-leading-blank-lines t]
1852                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1853                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1854                ["All of the above" gnus-article-strip-blank-lines t]
1855                ["All" gnus-article-strip-all-blank-lines t]
1856                ["Leading space" gnus-article-strip-leading-space t]
1857                ["Trailing space" gnus-article-strip-trailing-space t]
1858                ["Leading space in headers"
1859                 gnus-article-remove-leading-whitespace t])
1860               ["Overstrike" gnus-article-treat-overstrike t]
1861               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1862               ["Emphasis" gnus-article-emphasize t]
1863               ["Word wrap" gnus-article-fill-cited-article t]
1864               ["Fill long lines" gnus-article-fill-long-lines t]
1865               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1866               ["CR" gnus-article-remove-cr t]
1867               ["Show X-Face" gnus-article-display-x-face t]
1868               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1869               ["Base64" gnus-article-de-base64-unreadable t]
1870               ["Rot 13" gnus-summary-caesar-message
1871                ,@(if (featurep 'xemacs) '(t)
1872                    '(:help "\"Caesar rotate\" article by 13"))]
1873               ["Unix pipe" gnus-summary-pipe-message t]
1874               ["Add buttons" gnus-article-add-buttons t]
1875               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1876               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1877               ["Verbose header" gnus-summary-verbose-headers t]
1878               ["Toggle header" gnus-summary-toggle-header t]
1879               ["Html" gnus-article-wash-html t]
1880               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1881               ["HZ" gnus-article-decode-HZ t])
1882              ("Output"
1883               ["Save in default format" gnus-summary-save-article
1884                ,@(if (featurep 'xemacs) '(t)
1885                    '(:help "Save article using default method"))]
1886               ["Save in file" gnus-summary-save-article-file
1887                ,@(if (featurep 'xemacs) '(t)
1888                    '(:help "Save article in file"))]
1889               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1890               ["Save in MH folder" gnus-summary-save-article-folder t]
1891               ["Save in VM folder" gnus-summary-save-article-vm t]
1892               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1893               ["Save body in file" gnus-summary-save-article-body-file t]
1894               ["Pipe through a filter" gnus-summary-pipe-output t]
1895               ["Add to SOUP packet" gnus-soup-add-article t]
1896               ["Print" gnus-summary-print-article t])
1897              ("Backend"
1898               ["Respool article..." gnus-summary-respool-article t]
1899               ["Move article..." gnus-summary-move-article
1900                (gnus-check-backend-function
1901                 'request-move-article gnus-newsgroup-name)]
1902               ["Copy article..." gnus-summary-copy-article t]
1903               ["Crosspost article..." gnus-summary-crosspost-article
1904                (gnus-check-backend-function
1905                 'request-replace-article gnus-newsgroup-name)]
1906               ["Import file..." gnus-summary-import-article t]
1907               ["Create article..." gnus-summary-create-article t]
1908               ["Check if posted" gnus-summary-article-posted-p t]
1909               ["Edit article" gnus-summary-edit-article
1910                (not (gnus-group-read-only-p))]
1911               ["Delete article" gnus-summary-delete-article
1912                (gnus-check-backend-function
1913                 'request-expire-articles gnus-newsgroup-name)]
1914               ["Query respool" gnus-summary-respool-query t]
1915               ["Trace respool" gnus-summary-respool-trace t]
1916               ["Delete expirable articles" gnus-summary-expire-articles-now
1917                (gnus-check-backend-function
1918                 'request-expire-articles gnus-newsgroup-name)])
1919              ("Extract"
1920               ["Uudecode" gnus-uu-decode-uu
1921                ,@(if (featurep 'xemacs) '(t)
1922                    '(:help "Decode uuencoded article(s)"))]
1923               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1924               ["Unshar" gnus-uu-decode-unshar t]
1925               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1926               ["Save" gnus-uu-decode-save t]
1927               ["Binhex" gnus-uu-decode-binhex t]
1928               ["Postscript" gnus-uu-decode-postscript t])
1929              ("Cache"
1930               ["Enter article" gnus-cache-enter-article t]
1931               ["Remove article" gnus-cache-remove-article t])
1932              ["Translate" gnus-article-babel t]
1933              ["Select article buffer" gnus-summary-select-article-buffer t]
1934              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1935              ["Isearch article..." gnus-summary-isearch-article t]
1936              ["Beginning of the article" gnus-summary-beginning-of-article t]
1937              ["End of the article" gnus-summary-end-of-article t]
1938              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1939              ["Fetch referenced articles" gnus-summary-refer-references t]
1940              ["Fetch current thread" gnus-summary-refer-thread t]
1941              ["Fetch article with id..." gnus-summary-refer-article t]
1942              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1943              ["Redisplay" gnus-summary-show-article t]
1944              ["Raw article" gnus-summary-show-raw-article t])))
1945       (easy-menu-define
1946         gnus-summary-article-menu gnus-summary-mode-map ""
1947         (cons "Article" innards))
1948
1949       (if (not (keymapp gnus-summary-article-menu))
1950           (easy-menu-define
1951             gnus-article-commands-menu gnus-article-mode-map ""
1952             (cons "Commands" innards))
1953         ;; in Emacs, don't share menu.
1954         (setq gnus-article-commands-menu
1955               (copy-keymap gnus-summary-article-menu))
1956         (define-key gnus-article-mode-map [menu-bar commands]
1957           (cons "Commands" gnus-article-commands-menu))))
1958
1959     (easy-menu-define
1960       gnus-summary-thread-menu gnus-summary-mode-map ""
1961       '("Threads"
1962         ["Toggle threading" gnus-summary-toggle-threads t]
1963         ["Hide threads" gnus-summary-hide-all-threads t]
1964         ["Show threads" gnus-summary-show-all-threads t]
1965         ["Hide thread" gnus-summary-hide-thread t]
1966         ["Show thread" gnus-summary-show-thread t]
1967         ["Go to next thread" gnus-summary-next-thread t]
1968         ["Go to previous thread" gnus-summary-prev-thread t]
1969         ["Go down thread" gnus-summary-down-thread t]
1970         ["Go up thread" gnus-summary-up-thread t]
1971         ["Top of thread" gnus-summary-top-thread t]
1972         ["Mark thread as read" gnus-summary-kill-thread t]
1973         ["Lower thread score" gnus-summary-lower-thread t]
1974         ["Raise thread score" gnus-summary-raise-thread t]
1975         ["Rethread current" gnus-summary-rethread-current t]))
1976
1977     (easy-menu-define
1978       gnus-summary-post-menu gnus-summary-mode-map ""
1979       `("Post"
1980         ["Post an article" gnus-summary-post-news
1981          ,@(if (featurep 'xemacs) '(t)
1982              '(:help "Post an article"))]
1983         ["Followup" gnus-summary-followup
1984          ,@(if (featurep 'xemacs) '(t)
1985              '(:help "Post followup to this article"))]
1986         ["Followup and yank" gnus-summary-followup-with-original
1987          ,@(if (featurep 'xemacs) '(t)
1988              '(:help "Post followup to this article, quoting its contents"))]
1989         ["Supersede article" gnus-summary-supersede-article t]
1990         ["Cancel article" gnus-summary-cancel-article
1991          ,@(if (featurep 'xemacs) '(t)
1992              '(:help "Cancel an article you posted"))]
1993         ["Reply" gnus-summary-reply t]
1994         ["Reply and yank" gnus-summary-reply-with-original t]
1995         ["Wide reply" gnus-summary-wide-reply t]
1996         ["Wide reply and yank" gnus-summary-wide-reply-with-original
1997          ,@(if (featurep 'xemacs) '(t)
1998              '(:help "Mail a reply, quoting this article"))]
1999         ["Mail forward" gnus-summary-mail-forward t]
2000         ["Post forward" gnus-summary-post-forward t]
2001         ["Digest and mail" gnus-uu-digest-mail-forward t]
2002         ["Digest and post" gnus-uu-digest-post-forward t]
2003         ["Resend message" gnus-summary-resend-message t]
2004         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2005         ["Send a mail" gnus-summary-mail-other-window t]
2006         ["Uuencode and post" gnus-uu-post-news
2007          ,@(if (featurep 'xemacs) '(t)
2008              '(:help "Post a uuencoded article"))]
2009         ["Followup via news" gnus-summary-followup-to-mail t]
2010         ["Followup via news and yank"
2011          gnus-summary-followup-to-mail-with-original t]
2012         ;;("Draft"
2013         ;;["Send" gnus-summary-send-draft t]
2014         ;;["Send bounced" gnus-resend-bounced-mail t])
2015         ))
2016
2017     (cond
2018      ((not (keymapp gnus-summary-post-menu))
2019       (setq gnus-article-post-menu gnus-summary-post-menu))
2020      ((not gnus-article-post-menu)
2021       ;; Don't share post menu.
2022       (setq gnus-article-post-menu
2023             (copy-keymap gnus-summary-post-menu))))
2024     (define-key gnus-article-mode-map [menu-bar post]
2025       (cons "Post" gnus-article-post-menu))
2026
2027     (easy-menu-define
2028       gnus-summary-misc-menu gnus-summary-mode-map ""
2029       `("Gnus"
2030         ("Mark Read"
2031          ["Mark as read" gnus-summary-mark-as-read-forward t]
2032          ["Mark same subject and select"
2033           gnus-summary-kill-same-subject-and-select t]
2034          ["Mark same subject" gnus-summary-kill-same-subject t]
2035          ["Catchup" gnus-summary-catchup
2036           ,@(if (featurep 'xemacs) '(t)
2037               '(:help "Mark unread articles in this group as read"))]
2038          ["Catchup all" gnus-summary-catchup-all t]
2039          ["Catchup to here" gnus-summary-catchup-to-here t]
2040          ["Catchup from here" gnus-summary-catchup-from-here t]
2041          ["Catchup region" gnus-summary-mark-region-as-read t]
2042          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2043         ("Mark Various"
2044          ["Tick" gnus-summary-tick-article-forward t]
2045          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2046          ["Remove marks" gnus-summary-clear-mark-forward t]
2047          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2048          ["Set bookmark" gnus-summary-set-bookmark t]
2049          ["Remove bookmark" gnus-summary-remove-bookmark t])
2050         ("Mark Limit"
2051          ["Marks..." gnus-summary-limit-to-marks t]
2052          ["Subject..." gnus-summary-limit-to-subject t]
2053          ["Author..." gnus-summary-limit-to-author t]
2054          ["Age..." gnus-summary-limit-to-age t]
2055          ["Extra..." gnus-summary-limit-to-extra t]
2056          ["Score" gnus-summary-limit-to-score t]
2057          ["Unread" gnus-summary-limit-to-unread t]
2058          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2059          ["Articles" gnus-summary-limit-to-articles t]
2060          ["Pop limit" gnus-summary-pop-limit t]
2061          ["Show dormant" gnus-summary-limit-include-dormant t]
2062          ["Hide childless dormant"
2063           gnus-summary-limit-exclude-childless-dormant t]
2064          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2065          ["Hide marked" gnus-summary-limit-exclude-marks t]
2066          ["Show expunged" gnus-summary-limit-include-expunged t])
2067         ("Process Mark"
2068          ["Set mark" gnus-summary-mark-as-processable t]
2069          ["Remove mark" gnus-summary-unmark-as-processable t]
2070          ["Remove all marks" gnus-summary-unmark-all-processable t]
2071          ["Mark above" gnus-uu-mark-over t]
2072          ["Mark series" gnus-uu-mark-series t]
2073          ["Mark region" gnus-uu-mark-region t]
2074          ["Unmark region" gnus-uu-unmark-region t]
2075          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2076          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2077          ["Mark all" gnus-uu-mark-all t]
2078          ["Mark buffer" gnus-uu-mark-buffer t]
2079          ["Mark sparse" gnus-uu-mark-sparse t]
2080          ["Mark thread" gnus-uu-mark-thread t]
2081          ["Unmark thread" gnus-uu-unmark-thread t]
2082          ("Process Mark Sets"
2083           ["Kill" gnus-summary-kill-process-mark t]
2084           ["Yank" gnus-summary-yank-process-mark
2085            gnus-newsgroup-process-stack]
2086           ["Save" gnus-summary-save-process-mark t]))
2087         ("Scroll article"
2088          ["Page forward" gnus-summary-next-page
2089           ,@(if (featurep 'xemacs) '(t)
2090               '(:help "Show next page of article"))]
2091          ["Page backward" gnus-summary-prev-page
2092           ,@(if (featurep 'xemacs) '(t)
2093               '(:help "Show previous page of article"))]
2094          ["Line forward" gnus-summary-scroll-up t])
2095         ("Move"
2096          ["Next unread article" gnus-summary-next-unread-article t]
2097          ["Previous unread article" gnus-summary-prev-unread-article t]
2098          ["Next article" gnus-summary-next-article t]
2099          ["Previous article" gnus-summary-prev-article t]
2100          ["Next unread subject" gnus-summary-next-unread-subject t]
2101          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2102          ["Next article same subject" gnus-summary-next-same-subject t]
2103          ["Previous article same subject" gnus-summary-prev-same-subject t]
2104          ["First unread article" gnus-summary-first-unread-article t]
2105          ["Best unread article" gnus-summary-best-unread-article t]
2106          ["Go to subject number..." gnus-summary-goto-subject t]
2107          ["Go to article number..." gnus-summary-goto-article t]
2108          ["Go to the last article" gnus-summary-goto-last-article t]
2109          ["Pop article off history" gnus-summary-pop-article t])
2110         ("Sort"
2111          ["Sort by number" gnus-summary-sort-by-number t]
2112          ["Sort by author" gnus-summary-sort-by-author t]
2113          ["Sort by subject" gnus-summary-sort-by-subject t]
2114          ["Sort by date" gnus-summary-sort-by-date t]
2115          ["Sort by score" gnus-summary-sort-by-score t]
2116          ["Sort by lines" gnus-summary-sort-by-lines t]
2117          ["Sort by characters" gnus-summary-sort-by-chars t]
2118          ["Original sort" gnus-summary-sort-by-original t])
2119         ("Help"
2120          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2121          ["Describe group" gnus-summary-describe-group t]
2122          ["Read manual" gnus-info-find-node t])
2123         ("Modes"
2124          ["Pick and read" gnus-pick-mode t]
2125          ["Binary" gnus-binary-mode t])
2126         ("Regeneration"
2127          ["Regenerate" gnus-summary-prepare t]
2128          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2129          ["Toggle threading" gnus-summary-toggle-threads t])
2130         ["See old articles" gnus-summary-insert-old-articles t]
2131         ["See new articles" gnus-summary-insert-new-articles t]
2132         ["Filter articles..." gnus-summary-execute-command t]
2133         ["Run command on subjects..." gnus-summary-universal-argument t]
2134         ["Search articles forward..." gnus-summary-search-article-forward t]
2135         ["Search articles backward..." gnus-summary-search-article-backward t]
2136         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2137         ["Expand window" gnus-summary-expand-window t]
2138         ["Expire expirable articles" gnus-summary-expire-articles
2139          (gnus-check-backend-function
2140           'request-expire-articles gnus-newsgroup-name)]
2141         ["Edit local kill file" gnus-summary-edit-local-kill t]
2142         ["Edit main kill file" gnus-summary-edit-global-kill t]
2143         ["Edit group parameters" gnus-summary-edit-parameters t]
2144         ["Customize group parameters" gnus-summary-customize-parameters t]
2145         ["Send a bug report" gnus-bug t]
2146         ("Exit"
2147          ["Catchup and exit" gnus-summary-catchup-and-exit
2148           ,@(if (featurep 'xemacs) '(t)
2149               '(:help "Mark unread articles in this group as read, then exit"))]
2150          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2151          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2152          ["Exit group" gnus-summary-exit
2153           ,@(if (featurep 'xemacs) '(t)
2154               '(:help "Exit current group, return to group selection mode"))]
2155          ["Exit group without updating" gnus-summary-exit-no-update t]
2156          ["Exit and goto next group" gnus-summary-next-group t]
2157          ["Exit and goto prev group" gnus-summary-prev-group t]
2158          ["Reselect group" gnus-summary-reselect-current-group t]
2159          ["Rescan group" gnus-summary-rescan-group t]
2160          ["Update dribble" gnus-summary-save-newsrc t])))
2161
2162     (gnus-run-hooks 'gnus-summary-menu-hook)))
2163
2164 (defvar gnus-summary-tool-bar-map nil)
2165
2166 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2167 (defun gnus-summary-make-tool-bar ()
2168   (if (and (fboundp 'tool-bar-add-item-from-menu)
2169            (default-value 'tool-bar-mode)
2170            (not gnus-summary-tool-bar-map))
2171       (setq gnus-summary-tool-bar-map
2172             (let ((tool-bar-map (make-sparse-keymap))
2173                   (load-path (mm-image-load-path)))
2174               (tool-bar-add-item-from-menu
2175                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2176               (tool-bar-add-item-from-menu
2177                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2178               (tool-bar-add-item-from-menu
2179                'gnus-summary-post-news "post" gnus-summary-mode-map)
2180               (tool-bar-add-item-from-menu
2181                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2182               (tool-bar-add-item-from-menu
2183                'gnus-summary-followup "followup" gnus-summary-mode-map)
2184               (tool-bar-add-item-from-menu
2185                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2186               (tool-bar-add-item-from-menu
2187                'gnus-summary-reply "reply" gnus-summary-mode-map)
2188               (tool-bar-add-item-from-menu
2189                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2190               (tool-bar-add-item-from-menu
2191                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2192               (tool-bar-add-item-from-menu
2193                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2194               (tool-bar-add-item-from-menu
2195                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2196               (tool-bar-add-item-from-menu
2197                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2198               (tool-bar-add-item-from-menu
2199                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2200               (tool-bar-add-item-from-menu
2201                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2202               (tool-bar-add-item-from-menu
2203                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2204               tool-bar-map)))
2205   (if gnus-summary-tool-bar-map
2206       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2207
2208 (defun gnus-score-set-default (var value)
2209   "A version of set that updates the GNU Emacs menu-bar."
2210   (set var value)
2211   ;; It is the message that forces the active status to be updated.
2212   (message ""))
2213
2214 (defun gnus-make-score-map (type)
2215   "Make a summary score map of type TYPE."
2216   (if t
2217       nil
2218     (let ((headers '(("author" "from" string)
2219                      ("subject" "subject" string)
2220                      ("article body" "body" string)
2221                      ("article head" "head" string)
2222                      ("xref" "xref" string)
2223                      ("extra header" "extra" string)
2224                      ("lines" "lines" number)
2225                      ("followups to author" "followup" string)))
2226           (types '((number ("less than" <)
2227                            ("greater than" >)
2228                            ("equal" =))
2229                    (string ("substring" s)
2230                            ("exact string" e)
2231                            ("fuzzy string" f)
2232                            ("regexp" r))))
2233           (perms '(("temporary" (current-time-string))
2234                    ("permanent" nil)
2235                    ("immediate" now)))
2236           header)
2237       (list
2238        (apply
2239         'nconc
2240         (list
2241          (if (eq type 'lower)
2242              "Lower score"
2243            "Increase score"))
2244         (let (outh)
2245           (while headers
2246             (setq header (car headers))
2247             (setq outh
2248                   (cons
2249                    (apply
2250                     'nconc
2251                     (list (car header))
2252                     (let ((ts (cdr (assoc (nth 2 header) types)))
2253                           outt)
2254                       (while ts
2255                         (setq outt
2256                               (cons
2257                                (apply
2258                                 'nconc
2259                                 (list (caar ts))
2260                                 (let ((ps perms)
2261                                       outp)
2262                                   (while ps
2263                                     (setq outp
2264                                           (cons
2265                                            (vector
2266                                             (caar ps)
2267                                             (list
2268                                              'gnus-summary-score-entry
2269                                              (nth 1 header)
2270                                              (if (or (string= (nth 1 header)
2271                                                               "head")
2272                                                      (string= (nth 1 header)
2273                                                               "body"))
2274                                                  ""
2275                                                (list 'gnus-summary-header
2276                                                      (nth 1 header)))
2277                                              (list 'quote (nth 1 (car ts)))
2278                                              (list 'gnus-score-delta-default
2279                                                    nil)
2280                                              (nth 1 (car ps))
2281                                              t)
2282                                             t)
2283                                            outp))
2284                                     (setq ps (cdr ps)))
2285                                   (list (nreverse outp))))
2286                                outt))
2287                         (setq ts (cdr ts)))
2288                       (list (nreverse outt))))
2289                    outh))
2290             (setq headers (cdr headers)))
2291           (list (nreverse outh))))))))
2292
2293 \f
2294
2295 (defun gnus-summary-mode (&optional group)
2296   "Major mode for reading articles.
2297
2298 All normal editing commands are switched off.
2299 \\<gnus-summary-mode-map>
2300 Each line in this buffer represents one article.  To read an
2301 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2302 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2303 respectively.
2304
2305 You can also post articles and send mail from this buffer.  To
2306 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2307 of an article, type `\\[gnus-summary-reply]'.
2308
2309 There are approx. one gazillion commands you can execute in this
2310 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2311
2312 The following commands are available:
2313
2314 \\{gnus-summary-mode-map}"
2315   (interactive)
2316   (kill-all-local-variables)
2317   (when (gnus-visual-p 'summary-menu 'menu)
2318     (gnus-summary-make-menu-bar)
2319     (gnus-summary-make-tool-bar))
2320   (gnus-summary-make-local-variables)
2321   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2322     (gnus-summary-make-local-variables))
2323   (gnus-make-thread-indent-array)
2324   (gnus-simplify-mode-line)
2325   (setq major-mode 'gnus-summary-mode)
2326   (setq mode-name "Summary")
2327   (make-local-variable 'minor-mode-alist)
2328   (use-local-map gnus-summary-mode-map)
2329   (buffer-disable-undo)
2330   (setq buffer-read-only t)             ;Disable modification
2331   (setq truncate-lines t)
2332   (setq selective-display t)
2333   (setq selective-display-ellipses t)   ;Display `...'
2334   (gnus-summary-set-display-table)
2335   (gnus-set-default-directory)
2336   (setq gnus-newsgroup-name group)
2337   (make-local-variable 'gnus-summary-line-format)
2338   (make-local-variable 'gnus-summary-line-format-spec)
2339   (make-local-variable 'gnus-summary-dummy-line-format)
2340   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2341   (make-local-variable 'gnus-summary-mark-positions)
2342   (make-local-hook 'pre-command-hook)
2343   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2344   (gnus-run-hooks 'gnus-summary-mode-hook)
2345   (turn-on-gnus-mailing-list-mode)
2346   (mm-enable-multibyte-mule4)
2347   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2348   (gnus-update-summary-mark-positions))
2349
2350 (defun gnus-summary-make-local-variables ()
2351   "Make all the local summary buffer variables."
2352   (let (global)
2353     (dolist (local gnus-summary-local-variables)
2354       (if (consp local)
2355           (progn
2356             (if (eq (cdr local) 'global)
2357                 ;; Copy the global value of the variable.
2358                 (setq global (symbol-value (car local)))
2359               ;; Use the value from the list.
2360               (setq global (eval (cdr local))))
2361             (set (make-local-variable (car local)) global))
2362         ;; Simple nil-valued local variable.
2363         (set (make-local-variable local) nil)))))
2364
2365 (defun gnus-summary-clear-local-variables ()
2366   (let ((locals gnus-summary-local-variables))
2367     (while locals
2368       (if (consp (car locals))
2369           (and (vectorp (caar locals))
2370                (set (caar locals) nil))
2371         (and (vectorp (car locals))
2372              (set (car locals) nil)))
2373       (setq locals (cdr locals)))))
2374
2375 ;; Summary data functions.
2376
2377 (defmacro gnus-data-number (data)
2378   `(car ,data))
2379
2380 (defmacro gnus-data-set-number (data number)
2381   `(setcar ,data ,number))
2382
2383 (defmacro gnus-data-mark (data)
2384   `(nth 1 ,data))
2385
2386 (defmacro gnus-data-set-mark (data mark)
2387   `(setcar (nthcdr 1 ,data) ,mark))
2388
2389 (defmacro gnus-data-pos (data)
2390   `(nth 2 ,data))
2391
2392 (defmacro gnus-data-set-pos (data pos)
2393   `(setcar (nthcdr 2 ,data) ,pos))
2394
2395 (defmacro gnus-data-header (data)
2396   `(nth 3 ,data))
2397
2398 (defmacro gnus-data-set-header (data header)
2399   `(setf (nth 3 ,data) ,header))
2400
2401 (defmacro gnus-data-level (data)
2402   `(nth 4 ,data))
2403
2404 (defmacro gnus-data-unread-p (data)
2405   `(= (nth 1 ,data) gnus-unread-mark))
2406
2407 (defmacro gnus-data-read-p (data)
2408   `(/= (nth 1 ,data) gnus-unread-mark))
2409
2410 (defmacro gnus-data-pseudo-p (data)
2411   `(consp (nth 3 ,data)))
2412
2413 (defmacro gnus-data-find (number)
2414   `(assq ,number gnus-newsgroup-data))
2415
2416 (defmacro gnus-data-find-list (number &optional data)
2417   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2418      (memq (assq ,number bdata)
2419            bdata)))
2420
2421 (defmacro gnus-data-make (number mark pos header level)
2422   `(list ,number ,mark ,pos ,header ,level))
2423
2424 (defun gnus-data-enter (after-article number mark pos header level offset)
2425   (let ((data (gnus-data-find-list after-article)))
2426     (unless data
2427       (error "No such article: %d" after-article))
2428     (setcdr data (cons (gnus-data-make number mark pos header level)
2429                        (cdr data)))
2430     (setq gnus-newsgroup-data-reverse nil)
2431     (gnus-data-update-list (cddr data) offset)))
2432
2433 (defun gnus-data-enter-list (after-article list &optional offset)
2434   (when list
2435     (let ((data (and after-article (gnus-data-find-list after-article)))
2436           (ilist list))
2437       (if (not (or data
2438                    after-article))
2439           (let ((odata gnus-newsgroup-data))
2440             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2441             (when offset
2442               (gnus-data-update-list odata offset)))
2443       ;; Find the last element in the list to be spliced into the main
2444         ;; list.
2445         (while (cdr list)
2446           (setq list (cdr list)))
2447         (if (not data)
2448             (progn
2449               (setcdr list gnus-newsgroup-data)
2450               (setq gnus-newsgroup-data ilist)
2451               (when offset
2452                 (gnus-data-update-list (cdr list) offset)))
2453           (setcdr list (cdr data))
2454           (setcdr data ilist)
2455           (when offset
2456             (gnus-data-update-list (cdr list) offset))))
2457       (setq gnus-newsgroup-data-reverse nil))))
2458
2459 (defun gnus-data-remove (article &optional offset)
2460   (let ((data gnus-newsgroup-data))
2461     (if (= (gnus-data-number (car data)) article)
2462         (progn
2463           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2464                 gnus-newsgroup-data-reverse nil)
2465           (when offset
2466             (gnus-data-update-list gnus-newsgroup-data offset)))
2467       (while (cdr data)
2468         (when (= (gnus-data-number (cadr data)) article)
2469           (setcdr data (cddr data))
2470           (when offset
2471             (gnus-data-update-list (cdr data) offset))
2472           (setq data nil
2473                 gnus-newsgroup-data-reverse nil))
2474         (setq data (cdr data))))))
2475
2476 (defmacro gnus-data-list (backward)
2477   `(if ,backward
2478        (or gnus-newsgroup-data-reverse
2479            (setq gnus-newsgroup-data-reverse
2480                  (reverse gnus-newsgroup-data)))
2481      gnus-newsgroup-data))
2482
2483 (defun gnus-data-update-list (data offset)
2484   "Add OFFSET to the POS of all data entries in DATA."
2485   (setq gnus-newsgroup-data-reverse nil)
2486   (while data
2487     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2488     (setq data (cdr data))))
2489
2490 (defun gnus-summary-article-pseudo-p (article)
2491   "Say whether this article is a pseudo article or not."
2492   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2493
2494 (defmacro gnus-summary-article-sparse-p (article)
2495   "Say whether this article is a sparse article or not."
2496   `(memq ,article gnus-newsgroup-sparse))
2497
2498 (defmacro gnus-summary-article-ancient-p (article)
2499   "Say whether this article is a sparse article or not."
2500   `(memq ,article gnus-newsgroup-ancient))
2501
2502 (defun gnus-article-parent-p (number)
2503   "Say whether this article is a parent or not."
2504   (let ((data (gnus-data-find-list number)))
2505     (and (cdr data)              ; There has to be an article after...
2506          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2507             (gnus-data-level (nth 1 data))))))
2508
2509 (defun gnus-article-children (number)
2510   "Return a list of all children to NUMBER."
2511   (let* ((data (gnus-data-find-list number))
2512          (level (gnus-data-level (car data)))
2513          children)
2514     (setq data (cdr data))
2515     (while (and data
2516                 (= (gnus-data-level (car data)) (1+ level)))
2517       (push (gnus-data-number (car data)) children)
2518       (setq data (cdr data)))
2519     children))
2520
2521 (defmacro gnus-summary-skip-intangible ()
2522   "If the current article is intangible, then jump to a different article."
2523   '(let ((to (get-text-property (point) 'gnus-intangible)))
2524      (and to (gnus-summary-goto-subject to))))
2525
2526 (defmacro gnus-summary-article-intangible-p ()
2527   "Say whether this article is intangible or not."
2528   '(get-text-property (point) 'gnus-intangible))
2529
2530 (defun gnus-article-read-p (article)
2531   "Say whether ARTICLE is read or not."
2532   (not (or (memq article gnus-newsgroup-marked)
2533            (memq article gnus-newsgroup-unreads)
2534            (memq article gnus-newsgroup-unselected)
2535            (memq article gnus-newsgroup-dormant))))
2536
2537 ;; Some summary mode macros.
2538
2539 (defmacro gnus-summary-article-number ()
2540   "The article number of the article on the current line.
2541 If there isn's an article number here, then we return the current
2542 article number."
2543   '(progn
2544      (gnus-summary-skip-intangible)
2545      (or (get-text-property (point) 'gnus-number)
2546          (gnus-summary-last-subject))))
2547
2548 (defmacro gnus-summary-article-header (&optional number)
2549   "Return the header of article NUMBER."
2550   `(gnus-data-header (gnus-data-find
2551                       ,(or number '(gnus-summary-article-number)))))
2552
2553 (defmacro gnus-summary-thread-level (&optional number)
2554   "Return the level of thread that starts with article NUMBER."
2555   `(if (and (eq gnus-summary-make-false-root 'dummy)
2556             (get-text-property (point) 'gnus-intangible))
2557        0
2558      (gnus-data-level (gnus-data-find
2559                        ,(or number '(gnus-summary-article-number))))))
2560
2561 (defmacro gnus-summary-article-mark (&optional number)
2562   "Return the mark of article NUMBER."
2563   `(gnus-data-mark (gnus-data-find
2564                     ,(or number '(gnus-summary-article-number)))))
2565
2566 (defmacro gnus-summary-article-pos (&optional number)
2567   "Return the position of the line of article NUMBER."
2568   `(gnus-data-pos (gnus-data-find
2569                    ,(or number '(gnus-summary-article-number)))))
2570
2571 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2572 (defmacro gnus-summary-article-subject (&optional number)
2573   "Return current subject string or nil if nothing."
2574   `(let ((headers
2575           ,(if number
2576                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2577              '(gnus-data-header (assq (gnus-summary-article-number)
2578                                       gnus-newsgroup-data)))))
2579      (and headers
2580           (vectorp headers)
2581           (mail-header-subject headers))))
2582
2583 (defmacro gnus-summary-article-score (&optional number)
2584   "Return current article score."
2585   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2586                   gnus-newsgroup-scored))
2587        gnus-summary-default-score 0))
2588
2589 (defun gnus-summary-article-children (&optional number)
2590   "Return a list of article numbers that are children of article NUMBER."
2591   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2592          (level (gnus-data-level (car data)))
2593          l children)
2594     (while (and (setq data (cdr data))
2595                 (> (setq l (gnus-data-level (car data))) level))
2596       (and (= (1+ level) l)
2597            (push (gnus-data-number (car data))
2598                  children)))
2599     (nreverse children)))
2600
2601 (defun gnus-summary-article-parent (&optional number)
2602   "Return the article number of the parent of article NUMBER."
2603   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2604                                     (gnus-data-list t)))
2605          (level (gnus-data-level (car data))))
2606     (if (zerop level)
2607         ()                              ; This is a root.
2608       ;; We search until we find an article with a level less than
2609       ;; this one.  That function has to be the parent.
2610       (while (and (setq data (cdr data))
2611                   (not (< (gnus-data-level (car data)) level))))
2612       (and data (gnus-data-number (car data))))))
2613
2614 (defun gnus-unread-mark-p (mark)
2615   "Say whether MARK is the unread mark."
2616   (= mark gnus-unread-mark))
2617
2618 (defun gnus-read-mark-p (mark)
2619   "Say whether MARK is one of the marks that mark as read.
2620 This is all marks except unread, ticked, dormant, and expirable."
2621   (not (or (= mark gnus-unread-mark)
2622            (= mark gnus-ticked-mark)
2623            (= mark gnus-dormant-mark)
2624            (= mark gnus-expirable-mark))))
2625
2626 (defmacro gnus-article-mark (number)
2627   "Return the MARK of article NUMBER.
2628 This macro should only be used when computing the mark the \"first\"
2629 time; i.e., when generating the summary lines.  After that,
2630 `gnus-summary-article-mark' should be used to examine the
2631 marks of articles."
2632   `(cond
2633     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2634     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2635     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2636     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2637     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2638     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2639     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2640     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2641            gnus-ancient-mark))))
2642
2643 ;; Saving hidden threads.
2644
2645 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2646 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2647
2648 (defmacro gnus-save-hidden-threads (&rest forms)
2649   "Save hidden threads, eval FORMS, and restore the hidden threads."
2650   (let ((config (make-symbol "config")))
2651     `(let ((,config (gnus-hidden-threads-configuration)))
2652        (unwind-protect
2653            (save-excursion
2654              ,@forms)
2655          (gnus-restore-hidden-threads-configuration ,config)))))
2656
2657 (defun gnus-data-compute-positions ()
2658   "Compute the positions of all articles."
2659   (setq gnus-newsgroup-data-reverse nil)
2660   (let ((data gnus-newsgroup-data))
2661     (save-excursion
2662       (gnus-save-hidden-threads
2663         (gnus-summary-show-all-threads)
2664         (goto-char (point-min))
2665         (while data
2666           (while (get-text-property (point) 'gnus-intangible)
2667             (forward-line 1))
2668           (gnus-data-set-pos (car data) (+ (point) 3))
2669           (setq data (cdr data))
2670           (forward-line 1))))))
2671
2672 (defun gnus-hidden-threads-configuration ()
2673   "Return the current hidden threads configuration."
2674   (save-excursion
2675     (let (config)
2676       (goto-char (point-min))
2677       (while (search-forward "\r" nil t)
2678         (push (1- (point)) config))
2679       config)))
2680
2681 (defun gnus-restore-hidden-threads-configuration (config)
2682   "Restore hidden threads configuration from CONFIG."
2683   (save-excursion
2684     (let (point buffer-read-only)
2685       (while (setq point (pop config))
2686         (when (and (< point (point-max))
2687                    (goto-char point)
2688                    (eq (char-after) ?\n))
2689           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2690
2691 ;; Various summary mode internalish functions.
2692
2693 (defun gnus-mouse-pick-article (e)
2694   (interactive "e")
2695   (mouse-set-point e)
2696   (gnus-summary-next-page nil t))
2697
2698 (defun gnus-summary-set-display-table ()
2699   "Change the display table.
2700 Odd characters have a tendency to mess
2701 up nicely formatted displays - we make all possible glyphs
2702 display only a single character."
2703
2704   ;; We start from the standard display table, if any.
2705   (let ((table (or (copy-sequence standard-display-table)
2706                    (make-display-table)))
2707         (i 32))
2708     ;; Nix out all the control chars...
2709     (while (>= (setq i (1- i)) 0)
2710       (aset table i [??]))
2711    ;; ... but not newline and cr, of course.  (cr is necessary for the
2712     ;; selective display).
2713     (aset table ?\n nil)
2714     (aset table ?\r nil)
2715     ;; We keep TAB as well.
2716     (aset table ?\t nil)
2717     ;; We nix out any glyphs over 126 that are not set already.
2718     (let ((i 256))
2719       (while (>= (setq i (1- i)) 127)
2720         ;; Only modify if the entry is nil.
2721         (unless (aref table i)
2722           (aset table i [??]))))
2723     (setq buffer-display-table table)))
2724
2725 (defun gnus-summary-set-article-display-arrow (pos)
2726   "Update the overlay arrow to point to line at position POS."
2727   (when (and gnus-summary-display-arrow
2728              (boundp 'overlay-arrow-position)
2729              (boundp 'overlay-arrow-string))
2730     (save-excursion
2731       (goto-char pos)
2732       (beginning-of-line)
2733       (unless overlay-arrow-position
2734         (setq overlay-arrow-position (make-marker)))
2735       (setq overlay-arrow-string "=>"
2736             overlay-arrow-position (set-marker overlay-arrow-position
2737                                                (point)
2738                                                (current-buffer))))))
2739
2740 (defun gnus-summary-buffer-name (group)
2741   "Return the summary buffer name of GROUP."
2742   (concat "*Summary " group "*"))
2743
2744 (defun gnus-summary-setup-buffer (group)
2745   "Initialize summary buffer."
2746   (let ((buffer (gnus-summary-buffer-name group)))
2747     (if (get-buffer buffer)
2748         (progn
2749           (set-buffer buffer)
2750           (setq gnus-summary-buffer (current-buffer))
2751           (not gnus-newsgroup-prepared))
2752       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2753       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2754       (gnus-summary-mode group)
2755       (when gnus-carpal
2756         (gnus-carpal-setup-buffer 'summary))
2757       (unless gnus-single-article-buffer
2758         (make-local-variable 'gnus-article-buffer)
2759         (make-local-variable 'gnus-article-current)
2760         (make-local-variable 'gnus-original-article-buffer))
2761       (setq gnus-newsgroup-name group)
2762       ;; Set any local variables in the group parameters.
2763       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2764       t)))
2765
2766 (defun gnus-set-global-variables ()
2767   "Set the global equivalents of the buffer-local variables.
2768 They are set to the latest values they had.  These reflect the summary
2769 buffer that was in action when the last article was fetched."
2770   (when (eq major-mode 'gnus-summary-mode)
2771     (setq gnus-summary-buffer (current-buffer))
2772     (let ((name gnus-newsgroup-name)
2773           (marked gnus-newsgroup-marked)
2774           (unread gnus-newsgroup-unreads)
2775           (headers gnus-current-headers)
2776           (data gnus-newsgroup-data)
2777           (summary gnus-summary-buffer)
2778           (article-buffer gnus-article-buffer)
2779           (original gnus-original-article-buffer)
2780           (gac gnus-article-current)
2781           (reffed gnus-reffed-article-number)
2782           (score-file gnus-current-score-file)
2783           (default-charset gnus-newsgroup-charset)
2784           vlist)
2785       (let ((locals gnus-newsgroup-variables))
2786         (while locals
2787           (if (consp (car locals))
2788               (push (eval (caar locals)) vlist)
2789             (push (eval (car locals)) vlist))
2790           (setq locals (cdr locals)))
2791         (setq vlist (nreverse vlist)))
2792       (save-excursion
2793         (set-buffer gnus-group-buffer)
2794         (setq gnus-newsgroup-name name
2795               gnus-newsgroup-marked marked
2796               gnus-newsgroup-unreads unread
2797               gnus-current-headers headers
2798               gnus-newsgroup-data data
2799               gnus-article-current gac
2800               gnus-summary-buffer summary
2801               gnus-article-buffer article-buffer
2802               gnus-original-article-buffer original
2803               gnus-reffed-article-number reffed
2804               gnus-current-score-file score-file
2805               gnus-newsgroup-charset default-charset)
2806         (let ((locals gnus-newsgroup-variables))
2807           (while locals
2808             (if (consp (car locals))
2809                 (set (caar locals) (pop vlist))
2810               (set (car locals) (pop vlist)))
2811             (setq locals (cdr locals))))
2812         ;; The article buffer also has local variables.
2813         (when (gnus-buffer-live-p gnus-article-buffer)
2814           (set-buffer gnus-article-buffer)
2815           (setq gnus-summary-buffer summary))))))
2816
2817 (defun gnus-summary-article-unread-p (article)
2818   "Say whether ARTICLE is unread or not."
2819   (memq article gnus-newsgroup-unreads))
2820
2821 (defun gnus-summary-first-article-p (&optional article)
2822   "Return whether ARTICLE is the first article in the buffer."
2823   (if (not (setq article (or article (gnus-summary-article-number))))
2824       nil
2825     (eq article (caar gnus-newsgroup-data))))
2826
2827 (defun gnus-summary-last-article-p (&optional article)
2828   "Return whether ARTICLE is the last article in the buffer."
2829   (if (not (setq article (or article (gnus-summary-article-number))))
2830       ;; All non-existent numbers are the last article.  :-)
2831       t
2832     (not (cdr (gnus-data-find-list article)))))
2833
2834 (defun gnus-make-thread-indent-array ()
2835   (let ((n 200))
2836     (unless (and gnus-thread-indent-array
2837                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2838       (setq gnus-thread-indent-array (make-vector 201 "")
2839             gnus-thread-indent-array-level gnus-thread-indent-level)
2840       (while (>= n 0)
2841         (aset gnus-thread-indent-array n
2842               (make-string (* n gnus-thread-indent-level) ? ))
2843         (setq n (1- n))))))
2844
2845 (defun gnus-update-summary-mark-positions ()
2846   "Compute where the summary marks are to go."
2847   (save-excursion
2848     (when (gnus-buffer-exists-p gnus-summary-buffer)
2849       (set-buffer gnus-summary-buffer))
2850     (let ((gnus-replied-mark 129)
2851           (gnus-score-below-mark 130)
2852           (gnus-score-over-mark 130)
2853           (gnus-download-mark 131)
2854           (spec gnus-summary-line-format-spec)
2855           gnus-visual pos)
2856       (save-excursion
2857         (gnus-set-work-buffer)
2858         (let ((gnus-summary-line-format-spec spec)
2859               (gnus-newsgroup-downloadable '((0 . t))))
2860           (gnus-summary-insert-line
2861            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
2862            0 nil 128 t nil "" nil 1)
2863           (goto-char (point-min))
2864           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2865                                              (- (point) 2)))))
2866           (goto-char (point-min))
2867           (push (cons 'replied (and (search-forward "\201" nil t)
2868                                     (- (point) 2)))
2869                 pos)
2870           (goto-char (point-min))
2871           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2872                 pos)
2873           (goto-char (point-min))
2874           (push (cons 'download
2875                       (and (search-forward "\203" nil t) (- (point) 2)))
2876                 pos)))
2877       (setq gnus-summary-mark-positions pos))))
2878
2879 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2880   "Insert a dummy root in the summary buffer."
2881   (beginning-of-line)
2882   (gnus-add-text-properties
2883    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2884    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2885
2886 (defun gnus-summary-from-or-to-or-newsgroups (header)
2887   (let ((to (cdr (assq 'To (mail-header-extra header))))
2888         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2889         (mail-parse-charset gnus-newsgroup-charset)
2890         (mail-parse-ignored-charsets
2891          (save-excursion (set-buffer gnus-summary-buffer)
2892                          gnus-newsgroup-ignored-charsets)))
2893     (cond
2894      ((and to
2895            gnus-ignored-from-addresses
2896            (string-match gnus-ignored-from-addresses
2897                          (mail-header-from header)))
2898       (concat "-> "
2899               (or (car (funcall gnus-extract-address-components
2900                                 (funcall
2901                                  gnus-decode-encoded-word-function to)))
2902                   (funcall gnus-decode-encoded-word-function to))))
2903      ((and newsgroups
2904            gnus-ignored-from-addresses
2905            (string-match gnus-ignored-from-addresses
2906                          (mail-header-from header)))
2907       (concat "=> " newsgroups))
2908      (t
2909       (or (car (funcall gnus-extract-address-components
2910                         (mail-header-from header)))
2911           (mail-header-from header))))))
2912
2913 (defun gnus-summary-insert-line (gnus-tmp-header
2914                                  gnus-tmp-level gnus-tmp-current
2915                                  gnus-tmp-unread gnus-tmp-replied
2916                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2917                                  &optional gnus-tmp-dummy gnus-tmp-score
2918                                  gnus-tmp-process)
2919   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2920          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2921          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2922          (gnus-tmp-score-char
2923           (if (or (null gnus-summary-default-score)
2924                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2925                       gnus-summary-zcore-fuzz))
2926               ?                         ;Whitespace
2927             (if (< gnus-tmp-score gnus-summary-default-score)
2928                 gnus-score-below-mark gnus-score-over-mark)))
2929          (gnus-tmp-replied
2930           (cond (gnus-tmp-process gnus-process-mark)
2931                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2932                  gnus-cached-mark)
2933                 (gnus-tmp-replied gnus-replied-mark)
2934                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2935                  gnus-saved-mark)
2936                 (t gnus-no-mark)))
2937          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2938          (gnus-tmp-name
2939           (cond
2940            ((string-match "<[^>]+> *$" gnus-tmp-from)
2941             (let ((beg (match-beginning 0)))
2942               (or (and (string-match "^\".+\"" gnus-tmp-from)
2943                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2944                   (substring gnus-tmp-from 0 beg))))
2945            ((string-match "(.+)" gnus-tmp-from)
2946             (substring gnus-tmp-from
2947                        (1+ (match-beginning 0)) (1- (match-end 0))))
2948            (t gnus-tmp-from)))
2949          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2950          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2951          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2952          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2953          (buffer-read-only nil))
2954     (when (string= gnus-tmp-name "")
2955       (setq gnus-tmp-name gnus-tmp-from))
2956     (unless (numberp gnus-tmp-lines)
2957       (setq gnus-tmp-lines -1))
2958     (when (= gnus-tmp-lines -1)
2959       (setq gnus-tmp-lines "?"))
2960     (gnus-put-text-property
2961      (point)
2962      (progn (eval gnus-summary-line-format-spec) (point))
2963      'gnus-number gnus-tmp-number)
2964     (when (gnus-visual-p 'summary-highlight 'highlight)
2965       (forward-line -1)
2966       (gnus-run-hooks 'gnus-summary-update-hook)
2967       (forward-line 1))))
2968
2969 (defun gnus-summary-update-line (&optional dont-update)
2970   "Update summary line after change."
2971   (when (and gnus-summary-default-score
2972              (not gnus-summary-inhibit-highlight))
2973     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2974            (article (gnus-summary-article-number))
2975            (score (gnus-summary-article-score article)))
2976       (unless dont-update
2977         (if (and gnus-summary-mark-below
2978                  (< (gnus-summary-article-score)
2979                     gnus-summary-mark-below))
2980             ;; This article has a low score, so we mark it as read.
2981             (when (memq article gnus-newsgroup-unreads)
2982               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2983           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2984             ;; This article was previously marked as read on account
2985             ;; of a low score, but now it has risen, so we mark it as
2986             ;; unread.
2987             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2988         (gnus-summary-update-mark
2989          (if (or (null gnus-summary-default-score)
2990                  (<= (abs (- score gnus-summary-default-score))
2991                      gnus-summary-zcore-fuzz))
2992              ?                          ;Whitespace
2993            (if (< score gnus-summary-default-score)
2994                gnus-score-below-mark gnus-score-over-mark))
2995          'score))
2996       ;; Do visual highlighting.
2997       (when (gnus-visual-p 'summary-highlight 'highlight)
2998         (gnus-run-hooks 'gnus-summary-update-hook)))))
2999
3000 (defvar gnus-tmp-new-adopts nil)
3001
3002 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3003   "Return the number of articles in THREAD.
3004 This may be 0 in some cases -- if none of the articles in
3005 the thread are to be displayed."
3006   (let* ((number
3007          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3008           (cond
3009            ((not (listp thread))
3010             1)
3011            ((and (consp thread) (cdr thread))
3012             (apply
3013              '+ 1 (mapcar
3014                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3015            ((null thread)
3016             1)
3017            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3018             1)
3019            (t 0))))
3020     (when (and level (zerop level) gnus-tmp-new-adopts)
3021       (incf number
3022             (apply '+ (mapcar
3023                        'gnus-summary-number-of-articles-in-thread
3024                        gnus-tmp-new-adopts))))
3025     (if char
3026         (if (> number 1) gnus-not-empty-thread-mark
3027           gnus-empty-thread-mark)
3028       number)))
3029
3030 (defun gnus-summary-set-local-parameters (group)
3031   "Go through the local params of GROUP and set all variable specs in that list."
3032   (let ((params (gnus-group-find-parameter group))
3033         (vars '(quit-config))           ; Ignore quit-config.
3034         elem)
3035     (while params
3036       (setq elem (car params)
3037             params (cdr params))
3038       (and (consp elem)                 ; Has to be a cons.
3039            (consp (cdr elem))           ; The cdr has to be a list.
3040            (symbolp (car elem))         ; Has to be a symbol in there.
3041            (not (memq (car elem) vars))
3042            (ignore-errors               ; So we set it.
3043              (push (car elem) vars)
3044              (make-local-variable (car elem))
3045              (set (car elem) (eval (nth 1 elem))))))))
3046
3047 (defun gnus-summary-read-group (group &optional show-all no-article
3048                                       kill-buffer no-display backward
3049                                       select-articles)
3050   "Start reading news in newsgroup GROUP.
3051 If SHOW-ALL is non-nil, already read articles are also listed.
3052 If NO-ARTICLE is non-nil, no article is selected initially.
3053 If NO-DISPLAY, don't generate a summary buffer."
3054   (let (result)
3055     (while (and group
3056                 (null (setq result
3057                             (let ((gnus-auto-select-next nil))
3058                               (or (gnus-summary-read-group-1
3059                                    group show-all no-article
3060                                    kill-buffer no-display
3061                                    select-articles)
3062                                   (setq show-all nil
3063                                         select-articles nil)))))
3064                 (eq gnus-auto-select-next 'quietly))
3065       (set-buffer gnus-group-buffer)
3066       ;; The entry function called above goes to the next
3067       ;; group automatically, so we go two groups back
3068       ;; if we are searching for the previous group.
3069       (when backward
3070         (gnus-group-prev-unread-group 2))
3071       (if (not (equal group (gnus-group-group-name)))
3072           (setq group (gnus-group-group-name))
3073         (setq group nil)))
3074     result))
3075
3076 (defun gnus-summary-read-group-1 (group show-all no-article
3077                                         kill-buffer no-display
3078                                         &optional select-articles)
3079   ;; Killed foreign groups can't be entered.
3080   ;;  (when (and (not (gnus-group-native-p group))
3081   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3082   ;;    (error "Dead non-native groups can't be entered"))
3083   (gnus-message 5 "Retrieving newsgroup: %s..."
3084                 (gnus-group-decoded-name group))
3085   (let* ((new-group (gnus-summary-setup-buffer group))
3086          (quit-config (gnus-group-quit-config group))
3087          (did-select (and new-group (gnus-select-newsgroup
3088                                      group show-all select-articles))))
3089     (cond
3090      ;; This summary buffer exists already, so we just select it.
3091      ((not new-group)
3092       (gnus-set-global-variables)
3093       (when kill-buffer
3094         (gnus-kill-or-deaden-summary kill-buffer))
3095       (gnus-configure-windows 'summary 'force)
3096       (gnus-set-mode-line 'summary)
3097       (gnus-summary-position-point)
3098       (message "")
3099       t)
3100      ;; We couldn't select this group.
3101      ((null did-select)
3102       (when (and (eq major-mode 'gnus-summary-mode)
3103                  (not (equal (current-buffer) kill-buffer)))
3104         (kill-buffer (current-buffer))
3105         (if (not quit-config)
3106             (progn
3107               ;; Update the info -- marks might need to be removed,
3108               ;; for instance.
3109               (gnus-summary-update-info)
3110               (set-buffer gnus-group-buffer)
3111               (gnus-group-jump-to-group group)
3112               (gnus-group-next-unread-group 1))
3113           (gnus-handle-ephemeral-exit quit-config)))
3114       (let ((grpinfo (gnus-get-info group)))
3115         (if (null (gnus-info-read grpinfo))
3116             (gnus-message 3 "Group %s contains no messages"
3117                           (gnus-group-decoded-name group))
3118           (gnus-message 3 "Can't select group")))
3119       nil)
3120      ;; The user did a `C-g' while prompting for number of articles,
3121      ;; so we exit this group.
3122      ((eq did-select 'quit)
3123       (and (eq major-mode 'gnus-summary-mode)
3124            (not (equal (current-buffer) kill-buffer))
3125            (kill-buffer (current-buffer)))
3126       (when kill-buffer
3127         (gnus-kill-or-deaden-summary kill-buffer))
3128       (if (not quit-config)
3129           (progn
3130             (set-buffer gnus-group-buffer)
3131             (gnus-group-jump-to-group group)
3132             (gnus-group-next-unread-group 1)
3133             (gnus-configure-windows 'group 'force))
3134         (gnus-handle-ephemeral-exit quit-config))
3135       ;; Finally signal the quit.
3136       (signal 'quit nil))
3137      ;; The group was successfully selected.
3138      (t
3139       (gnus-set-global-variables)
3140       ;; Save the active value in effect when the group was entered.
3141       (setq gnus-newsgroup-active
3142             (gnus-copy-sequence
3143              (gnus-active gnus-newsgroup-name)))
3144       ;; You can change the summary buffer in some way with this hook.
3145       (gnus-run-hooks 'gnus-select-group-hook)
3146       (gnus-update-format-specifications
3147        nil 'summary 'summary-mode 'summary-dummy)
3148       (gnus-update-summary-mark-positions)
3149       ;; Do score processing.
3150       (when gnus-use-scoring
3151         (gnus-possibly-score-headers))
3152       ;; Check whether to fill in the gaps in the threads.
3153       (when gnus-build-sparse-threads
3154         (gnus-build-sparse-threads))
3155       ;; Find the initial limit.
3156       (if gnus-show-threads
3157           (if show-all
3158               (let ((gnus-newsgroup-dormant nil))
3159                 (gnus-summary-initial-limit show-all))
3160             (gnus-summary-initial-limit show-all))
3161         ;; When untreaded, all articles are always shown.
3162         (setq gnus-newsgroup-limit
3163               (mapcar
3164                (lambda (header) (mail-header-number header))
3165                gnus-newsgroup-headers)))
3166       ;; Generate the summary buffer.
3167       (unless no-display
3168         (gnus-summary-prepare))
3169       (when gnus-use-trees
3170         (gnus-tree-open group)
3171         (setq gnus-summary-highlight-line-function
3172               'gnus-tree-highlight-article))
3173       ;; If the summary buffer is empty, but there are some low-scored
3174       ;; articles or some excluded dormants, we include these in the
3175       ;; buffer.
3176       (when (and (zerop (buffer-size))
3177                  (not no-display))
3178         (cond (gnus-newsgroup-dormant
3179                (gnus-summary-limit-include-dormant))
3180               ((and gnus-newsgroup-scored show-all)
3181                (gnus-summary-limit-include-expunged t))))
3182       ;; Function `gnus-apply-kill-file' must be called in this hook.
3183       (gnus-run-hooks 'gnus-apply-kill-hook)
3184       (if (and (zerop (buffer-size))
3185                (not no-display))
3186           (progn
3187             ;; This newsgroup is empty.
3188             (gnus-summary-catchup-and-exit nil t)
3189             (gnus-message 6 "No unread news")
3190             (when kill-buffer
3191               (gnus-kill-or-deaden-summary kill-buffer))
3192             ;; Return nil from this function.
3193             nil)
3194         ;; Hide conversation thread subtrees.  We cannot do this in
3195         ;; gnus-summary-prepare-hook since kill processing may not
3196         ;; work with hidden articles.
3197         (and gnus-show-threads
3198              gnus-thread-hide-subtree
3199              (gnus-summary-hide-all-threads))
3200         (when kill-buffer
3201           (gnus-kill-or-deaden-summary kill-buffer))
3202         ;; Show first unread article if requested.
3203         (if (and (not no-article)
3204                  (not no-display)
3205                  gnus-newsgroup-unreads
3206                  gnus-auto-select-first)
3207             (progn
3208               (gnus-configure-windows 'summary)
3209               (cond
3210                ((eq gnus-auto-select-first 'best)
3211                 (gnus-summary-best-unread-article))
3212                ((eq gnus-auto-select-first t)
3213                 (gnus-summary-first-unread-article))
3214                ((gnus-functionp gnus-auto-select-first)
3215                 (funcall gnus-auto-select-first))))
3216           ;; Don't select any articles, just move point to the first
3217           ;; article in the group.
3218           (goto-char (point-min))
3219           (gnus-summary-position-point)
3220           (gnus-configure-windows 'summary 'force)
3221           (gnus-set-mode-line 'summary))
3222         (when (get-buffer-window gnus-group-buffer t)
3223           ;; Gotta use windows, because recenter does weird stuff if
3224           ;; the current buffer ain't the displayed window.
3225           (let ((owin (selected-window)))
3226             (select-window (get-buffer-window gnus-group-buffer t))
3227             (when (gnus-group-goto-group group)
3228               (recenter))
3229             (select-window owin)))
3230         ;; Mark this buffer as "prepared".
3231         (setq gnus-newsgroup-prepared t)
3232         (gnus-run-hooks 'gnus-summary-prepared-hook)
3233         t)))))
3234
3235 (defun gnus-summary-prepare ()
3236   "Generate the summary buffer."
3237   (interactive)
3238   (let ((buffer-read-only nil))
3239     (erase-buffer)
3240     (setq gnus-newsgroup-data nil
3241           gnus-newsgroup-data-reverse nil)
3242     (gnus-run-hooks 'gnus-summary-generate-hook)
3243     ;; Generate the buffer, either with threads or without.
3244     (when gnus-newsgroup-headers
3245       (gnus-summary-prepare-threads
3246        (if gnus-show-threads
3247            (gnus-sort-gathered-threads
3248             (funcall gnus-summary-thread-gathering-function
3249                      (gnus-sort-threads
3250                       (gnus-cut-threads (gnus-make-threads)))))
3251          ;; Unthreaded display.
3252          (gnus-sort-articles gnus-newsgroup-headers))))
3253     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3254     ;; Call hooks for modifying summary buffer.
3255     (goto-char (point-min))
3256     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3257
3258 (defsubst gnus-general-simplify-subject (subject)
3259   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3260   (setq subject
3261         (cond
3262          ;; Truncate the subject.
3263          (gnus-simplify-subject-functions
3264           (gnus-map-function gnus-simplify-subject-functions subject))
3265          ((numberp gnus-summary-gather-subject-limit)
3266           (setq subject (gnus-simplify-subject-re subject))
3267           (if (> (length subject) gnus-summary-gather-subject-limit)
3268               (substring subject 0 gnus-summary-gather-subject-limit)
3269             subject))
3270          ;; Fuzzily simplify it.
3271          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3272           (gnus-simplify-subject-fuzzy subject))
3273          ;; Just remove the leading "Re:".
3274          (t
3275           (gnus-simplify-subject-re subject))))
3276
3277   (if (and gnus-summary-gather-exclude-subject
3278            (string-match gnus-summary-gather-exclude-subject subject))
3279       nil                         ; This article shouldn't be gathered
3280     subject))
3281
3282 (defun gnus-summary-simplify-subject-query ()
3283   "Query where the respool algorithm would put this article."
3284   (interactive)
3285   (gnus-summary-select-article)
3286   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3287
3288 (defun gnus-gather-threads-by-subject (threads)
3289   "Gather threads by looking at Subject headers."
3290   (if (not gnus-summary-make-false-root)
3291       threads
3292     (let ((hashtb (gnus-make-hashtable 1024))
3293           (prev threads)
3294           (result threads)
3295           subject hthread whole-subject)
3296       (while threads
3297         (setq subject (gnus-general-simplify-subject
3298                        (setq whole-subject (mail-header-subject
3299                                             (caar threads)))))
3300         (when subject
3301           (if (setq hthread (gnus-gethash subject hashtb))
3302               (progn
3303                 ;; We enter a dummy root into the thread, if we
3304                 ;; haven't done that already.
3305                 (unless (stringp (caar hthread))
3306                   (setcar hthread (list whole-subject (car hthread))))
3307                 ;; We add this new gathered thread to this gathered
3308                 ;; thread.
3309                 (setcdr (car hthread)
3310                         (nconc (cdar hthread) (list (car threads))))
3311                 ;; Remove it from the list of threads.
3312                 (setcdr prev (cdr threads))
3313                 (setq threads prev))
3314             ;; Enter this thread into the hash table.
3315             (gnus-sethash subject threads hashtb)))
3316         (setq prev threads)
3317         (setq threads (cdr threads)))
3318       result)))
3319
3320 (defun gnus-gather-threads-by-references (threads)
3321   "Gather threads by looking at References headers."
3322   (let ((idhashtb (gnus-make-hashtable 1024))
3323         (thhashtb (gnus-make-hashtable 1024))
3324         (prev threads)
3325         (result threads)
3326         ids references id gthread gid entered ref)
3327     (while threads
3328       (when (setq references (mail-header-references (caar threads)))
3329         (setq id (mail-header-id (caar threads))
3330               ids (gnus-split-references references)
3331               entered nil)
3332         (while (setq ref (pop ids))
3333           (setq ids (delete ref ids))
3334           (if (not (setq gid (gnus-gethash ref idhashtb)))
3335               (progn
3336                 (gnus-sethash ref id idhashtb)
3337                 (gnus-sethash id threads thhashtb))
3338             (setq gthread (gnus-gethash gid thhashtb))
3339             (unless entered
3340               ;; We enter a dummy root into the thread, if we
3341               ;; haven't done that already.
3342               (unless (stringp (caar gthread))
3343                 (setcar gthread (list (mail-header-subject (caar gthread))
3344                                       (car gthread))))
3345               ;; We add this new gathered thread to this gathered
3346               ;; thread.
3347               (setcdr (car gthread)
3348                       (nconc (cdar gthread) (list (car threads)))))
3349             ;; Add it into the thread hash table.
3350             (gnus-sethash id gthread thhashtb)
3351             (setq entered t)
3352             ;; Remove it from the list of threads.
3353             (setcdr prev (cdr threads))
3354             (setq threads prev))))
3355       (setq prev threads)
3356       (setq threads (cdr threads)))
3357     result))
3358
3359 (defun gnus-sort-gathered-threads (threads)
3360   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3361   (let ((result threads))
3362     (while threads
3363       (when (stringp (caar threads))
3364         (setcdr (car threads)
3365                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3366       (setq threads (cdr threads)))
3367     result))
3368
3369 (defun gnus-thread-loop-p (root thread)
3370   "Say whether ROOT is in THREAD."
3371   (let ((stack (list thread))
3372         (infloop 0)
3373         th)
3374     (while (setq thread (pop stack))
3375       (setq th (cdr thread))
3376       (while (and th
3377                   (not (eq (caar th) root)))
3378         (pop th))
3379       (if th
3380           ;; We have found a loop.
3381           (let (ref-dep)
3382             (setcdr thread (delq (car th) (cdr thread)))
3383             (if (boundp (setq ref-dep (intern "none"
3384                                               gnus-newsgroup-dependencies)))
3385                 (setcdr (symbol-value ref-dep)
3386                         (nconc (cdr (symbol-value ref-dep))
3387                                (list (car th))))
3388               (set ref-dep (list nil (car th))))
3389             (setq infloop 1
3390                   stack nil))
3391         ;; Push all the subthreads onto the stack.
3392         (push (cdr thread) stack)))
3393     infloop))
3394
3395 (defun gnus-make-threads ()
3396   "Go through the dependency hashtb and find the roots.  Return all threads."
3397   (let (threads)
3398     (while (catch 'infloop
3399              (mapatoms
3400               (lambda (refs)
3401                 ;; Deal with self-referencing References loops.
3402                 (when (and (car (symbol-value refs))
3403                            (not (zerop
3404                                  (apply
3405                                   '+
3406                                   (mapcar
3407                                    (lambda (thread)
3408                                      (gnus-thread-loop-p
3409                                       (car (symbol-value refs)) thread))
3410                                    (cdr (symbol-value refs)))))))
3411                   (setq threads nil)
3412                   (throw 'infloop t))
3413                 (unless (car (symbol-value refs))
3414              ;; These threads do not refer back to any other articles,
3415                   ;; so they're roots.
3416                   (setq threads (append (cdr (symbol-value refs)) threads))))
3417               gnus-newsgroup-dependencies)))
3418     threads))
3419
3420 ;; Build the thread tree.
3421 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3422   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3423
3424 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3425 if it was already present.
3426
3427 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3428 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3429 Message-IDs will be renamed be renamed to a unique Message-ID before
3430 being entered.
3431
3432 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3433   (let* ((id (mail-header-id header))
3434          (id-dep (and id (intern id dependencies)))
3435          ref ref-dep ref-header)
3436     ;; Enter this `header' in the `dependencies' table.
3437     (cond
3438      ((not id-dep)
3439       (setq header nil))
3440      ;; The first two cases do the normal part: enter a new `header'
3441      ;; in the `dependencies' table.
3442      ((not (boundp id-dep))
3443       (set id-dep (list header)))
3444      ((null (car (symbol-value id-dep)))
3445       (setcar (symbol-value id-dep) header))
3446
3447      ;; From here the `header' was already present in the
3448      ;; `dependencies' table.
3449      (force-new
3450       ;; Overrides an existing entry;
3451       ;; just set the header part of the entry.
3452       (setcar (symbol-value id-dep) header))
3453
3454      ;; Renames the existing `header' to a unique Message-ID.
3455      ((not gnus-summary-ignore-duplicates)
3456       ;; An article with this Message-ID has already been seen.
3457       ;; We rename the Message-ID.
3458       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3459            (list header))
3460       (mail-header-set-id header id))
3461
3462      ;; The last case ignores an existing entry, except it adds any
3463      ;; additional Xrefs (in case the two articles came from different
3464      ;; servers.
3465      ;; Also sets `header' to `nil' meaning that the `dependencies'
3466      ;; table was *not* modified.
3467      (t
3468       (mail-header-set-xref
3469        (car (symbol-value id-dep))
3470        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3471                    "")
3472                (or (mail-header-xref header) "")))
3473       (setq header nil)))
3474
3475     (when header
3476       ;; First check if that we are not creating a References loop.
3477       (setq ref (gnus-parent-id (mail-header-references header)))
3478       (while (and ref
3479                   (setq ref-dep (intern-soft ref dependencies))
3480                   (boundp ref-dep)
3481                   (setq ref-header (car (symbol-value ref-dep))))
3482         (if (string= id ref)
3483             ;; Yuk!  This is a reference loop.  Make the article be a
3484             ;; root article.
3485             (progn
3486               (mail-header-set-references (car (symbol-value id-dep)) "none")
3487               (setq ref nil))
3488           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3489       (setq ref (gnus-parent-id (mail-header-references header)))
3490       (setq ref-dep (intern (or ref "none") dependencies))
3491       (if (boundp ref-dep)
3492           (setcdr (symbol-value ref-dep)
3493                   (nconc (cdr (symbol-value ref-dep))
3494                          (list (symbol-value id-dep))))
3495         (set ref-dep (list nil (symbol-value id-dep)))))
3496     header))
3497
3498 (defun gnus-build-sparse-threads ()
3499   (let ((headers gnus-newsgroup-headers)
3500         (mail-parse-charset gnus-newsgroup-charset)
3501         (gnus-summary-ignore-duplicates t)
3502         header references generation relations
3503         subject child end new-child date)
3504     ;; First we create an alist of generations/relations, where
3505     ;; generations is how much we trust the relation, and the relation
3506     ;; is parent/child.
3507     (gnus-message 7 "Making sparse threads...")
3508     (save-excursion
3509       (nnheader-set-temp-buffer " *gnus sparse threads*")
3510       (while (setq header (pop headers))
3511         (when (and (setq references (mail-header-references header))
3512                    (not (string= references "")))
3513           (insert references)
3514           (setq child (mail-header-id header)
3515                 subject (mail-header-subject header)
3516                 date (mail-header-date header)
3517                 generation 0)
3518           (while (search-backward ">" nil t)
3519             (setq end (1+ (point)))
3520             (when (search-backward "<" nil t)
3521               (setq new-child (buffer-substring (point) end))
3522               (push (list (incf generation)
3523                           child (setq child new-child)
3524                           subject date)
3525                     relations)))
3526           (when child
3527             (push (list (1+ generation) child nil subject) relations))
3528           (erase-buffer)))
3529       (kill-buffer (current-buffer)))
3530     ;; Sort over trustworthiness.
3531     (mapcar
3532      (lambda (relation)
3533        (when (gnus-dependencies-add-header
3534               (make-full-mail-header
3535                gnus-reffed-article-number
3536                (nth 3 relation) "" (or (nth 4 relation) "")
3537                (nth 1 relation)
3538                (or (nth 2 relation) "") 0 0 "")
3539               gnus-newsgroup-dependencies nil)
3540          (push gnus-reffed-article-number gnus-newsgroup-limit)
3541          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3542          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3543                gnus-newsgroup-reads)
3544          (decf gnus-reffed-article-number)))
3545      (sort relations 'car-less-than-car))
3546     (gnus-message 7 "Making sparse threads...done")))
3547
3548 (defun gnus-build-old-threads ()
3549   ;; Look at all the articles that refer back to old articles, and
3550   ;; fetch the headers for the articles that aren't there.  This will
3551   ;; build complete threads - if the roots haven't been expired by the
3552   ;; server, that is.
3553   (let ((mail-parse-charset gnus-newsgroup-charset)
3554         id heads)
3555     (mapatoms
3556      (lambda (refs)
3557        (when (not (car (symbol-value refs)))
3558          (setq heads (cdr (symbol-value refs)))
3559          (while heads
3560            (if (memq (mail-header-number (caar heads))
3561                      gnus-newsgroup-dormant)
3562                (setq heads (cdr heads))
3563              (setq id (symbol-name refs))
3564              (while (and (setq id (gnus-build-get-header id))
3565                          (not (car (gnus-id-to-thread id)))))
3566              (setq heads nil)))))
3567      gnus-newsgroup-dependencies)))
3568
3569 ;; This function has to be called with point after the article number
3570 ;; on the beginning of the line.
3571 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3572   (let ((eol (gnus-point-at-eol))
3573         (buffer (current-buffer))
3574         header)
3575
3576     ;; overview: [num subject from date id refs chars lines misc]
3577     (unwind-protect
3578         (progn
3579           (narrow-to-region (point) eol)
3580           (unless (eobp)
3581             (forward-char))
3582
3583           (setq header
3584                 (make-full-mail-header
3585                  number                 ; number
3586                  (funcall gnus-decode-encoded-word-function
3587                           (nnheader-nov-field)) ; subject
3588                  (funcall gnus-decode-encoded-word-function
3589                           (nnheader-nov-field)) ; from
3590                  (nnheader-nov-field)   ; date
3591                  (nnheader-nov-read-message-id) ; id
3592                  (nnheader-nov-field)   ; refs
3593                  (nnheader-nov-read-integer) ; chars
3594                  (nnheader-nov-read-integer) ; lines
3595                  (unless (eobp)
3596                    (if (looking-at "Xref: ")
3597                        (goto-char (match-end 0)))
3598                    (nnheader-nov-field)) ; Xref
3599                  (nnheader-nov-parse-extra)))) ; extra
3600
3601       (widen))
3602
3603     (when gnus-alter-header-function
3604       (funcall gnus-alter-header-function header))
3605     (gnus-dependencies-add-header header dependencies force-new)))
3606
3607 (defun gnus-build-get-header (id)
3608   "Look through the buffer of NOV lines and find the header to ID.
3609 Enter this line into the dependencies hash table, and return
3610 the id of the parent article (if any)."
3611   (let ((deps gnus-newsgroup-dependencies)
3612         found header)
3613     (prog1
3614         (save-excursion
3615           (set-buffer nntp-server-buffer)
3616           (let ((case-fold-search nil))
3617             (goto-char (point-min))
3618             (while (and (not found)
3619                         (search-forward id nil t))
3620               (beginning-of-line)
3621               (setq found (looking-at
3622                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3623                                    (regexp-quote id))))
3624               (or found (beginning-of-line 2)))
3625             (when found
3626               (beginning-of-line)
3627               (and
3628                (setq header (gnus-nov-parse-line
3629                              (read (current-buffer)) deps))
3630                (gnus-parent-id (mail-header-references header))))))
3631       (when header
3632         (let ((number (mail-header-number header)))
3633           (push number gnus-newsgroup-limit)
3634           (push header gnus-newsgroup-headers)
3635           (if (memq number gnus-newsgroup-unselected)
3636               (progn
3637                 (push number gnus-newsgroup-unreads)
3638                 (setq gnus-newsgroup-unselected
3639                       (delq number gnus-newsgroup-unselected)))
3640             (push number gnus-newsgroup-ancient)))))))
3641
3642 (defun gnus-build-all-threads ()
3643   "Read all the headers."
3644   (let ((gnus-summary-ignore-duplicates t)
3645         (mail-parse-charset gnus-newsgroup-charset)
3646         (dependencies gnus-newsgroup-dependencies)
3647         header article)
3648     (save-excursion
3649       (set-buffer nntp-server-buffer)
3650       (let ((case-fold-search nil))
3651         (goto-char (point-min))
3652         (while (not (eobp))
3653           (ignore-errors
3654             (setq article (read (current-buffer))
3655                   header (gnus-nov-parse-line article dependencies)))
3656           (when header
3657             (save-excursion
3658               (set-buffer gnus-summary-buffer)
3659               (push header gnus-newsgroup-headers)
3660               (if (memq (setq article (mail-header-number header))
3661                         gnus-newsgroup-unselected)
3662                   (progn
3663                     (push article gnus-newsgroup-unreads)
3664                     (setq gnus-newsgroup-unselected
3665                           (delq article gnus-newsgroup-unselected)))
3666                 (push article gnus-newsgroup-ancient)))
3667             (forward-line 1)))))))
3668
3669 (defun gnus-summary-update-article-line (article header)
3670   "Update the line for ARTICLE using HEADERS."
3671   (let* ((id (mail-header-id header))
3672          (thread (gnus-id-to-thread id)))
3673     (unless thread
3674       (error "Article in no thread"))
3675     ;; Update the thread.
3676     (setcar thread header)
3677     (gnus-summary-goto-subject article)
3678     (let* ((datal (gnus-data-find-list article))
3679            (data (car datal))
3680            (length (when (cdr datal)
3681                      (- (gnus-data-pos data)
3682                         (gnus-data-pos (cadr datal)))))
3683            (buffer-read-only nil)
3684            (level (gnus-summary-thread-level)))
3685       (gnus-delete-line)
3686       (gnus-summary-insert-line
3687        header level nil (gnus-article-mark article)
3688        (memq article gnus-newsgroup-replied)
3689        (memq article gnus-newsgroup-expirable)
3690        ;; Only insert the Subject string when it's different
3691        ;; from the previous Subject string.
3692        (if (and
3693             gnus-show-threads
3694             (gnus-subject-equal
3695              (condition-case ()
3696                  (mail-header-subject
3697                   (gnus-data-header
3698                    (cadr
3699                     (gnus-data-find-list
3700                      article
3701                      (gnus-data-list t)))))
3702                ;; Error on the side of excessive subjects.
3703                (error ""))
3704              (mail-header-subject header)))
3705            ""
3706          (mail-header-subject header))
3707        nil (cdr (assq article gnus-newsgroup-scored))
3708        (memq article gnus-newsgroup-processable))
3709       (when length
3710         (gnus-data-update-list
3711          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3712
3713 (defun gnus-summary-update-article (article &optional iheader)
3714   "Update ARTICLE in the summary buffer."
3715   (set-buffer gnus-summary-buffer)
3716   (let* ((header (gnus-summary-article-header article))
3717          (id (mail-header-id header))
3718          (data (gnus-data-find article))
3719          (thread (gnus-id-to-thread id))
3720          (references (mail-header-references header))
3721          (parent
3722           (gnus-id-to-thread
3723            (or (gnus-parent-id
3724                 (when (and references
3725                            (not (equal "" references)))
3726                   references))
3727                "none")))
3728          (buffer-read-only nil)
3729          (old (car thread)))
3730     (when thread
3731       (unless iheader
3732         (setcar thread nil)
3733         (when parent
3734           (delq thread parent)))
3735       (if (gnus-summary-insert-subject id header)
3736        ;; Set the (possibly) new article number in the data structure.
3737           (gnus-data-set-number data (gnus-id-to-article id))
3738         (setcar thread old)
3739         nil))))
3740
3741 (defun gnus-rebuild-thread (id &optional line)
3742   "Rebuild the thread containing ID.
3743 If LINE, insert the rebuilt thread starting on line LINE."
3744   (let ((buffer-read-only nil)
3745         old-pos current thread data)
3746     (if (not gnus-show-threads)
3747         (setq thread (list (car (gnus-id-to-thread id))))
3748       ;; Get the thread this article is part of.
3749       (setq thread (gnus-remove-thread id)))
3750     (setq old-pos (gnus-point-at-bol))
3751     (setq current (save-excursion
3752                     (and (re-search-backward "[\r\n]" nil t)
3753                          (gnus-summary-article-number))))
3754     ;; If this is a gathered thread, we have to go some re-gathering.
3755     (when (stringp (car thread))
3756       (let ((subject (car thread))
3757             roots thr)
3758         (setq thread (cdr thread))
3759         (while thread
3760           (unless (memq (setq thr (gnus-id-to-thread
3761                                    (gnus-root-id
3762                                     (mail-header-id (caar thread)))))
3763                         roots)
3764             (push thr roots))
3765           (setq thread (cdr thread)))
3766         ;; We now have all (unique) roots.
3767         (if (= (length roots) 1)
3768             ;; All the loose roots are now one solid root.
3769             (setq thread (car roots))
3770           (setq thread (cons subject (gnus-sort-threads roots))))))
3771     (let (threads)
3772       ;; We then insert this thread into the summary buffer.
3773       (when line
3774         (goto-char (point-min))
3775         (forward-line (1- line)))
3776       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3777         (if gnus-show-threads
3778             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3779           (gnus-summary-prepare-unthreaded thread))
3780         (setq data (nreverse gnus-newsgroup-data))
3781         (setq threads gnus-newsgroup-threads))
3782       ;; We splice the new data into the data structure.
3783       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3784       ;;!!! then we want to insert at the beginning of the buffer.
3785       ;;!!! That happens to be true with Gnus now, but that may
3786       ;;!!! change in the future.  Perhaps.
3787       (gnus-data-enter-list
3788        (if line nil current) data (- (point) old-pos))
3789       (setq gnus-newsgroup-threads
3790             (nconc threads gnus-newsgroup-threads))
3791       (gnus-data-compute-positions))))
3792
3793 (defun gnus-number-to-header (number)
3794   "Return the header for article NUMBER."
3795   (let ((headers gnus-newsgroup-headers))
3796     (while (and headers
3797                 (not (= number (mail-header-number (car headers)))))
3798       (pop headers))
3799     (when headers
3800       (car headers))))
3801
3802 (defun gnus-parent-headers (in-headers &optional generation)
3803   "Return the headers of the GENERATIONeth parent of HEADERS."
3804   (unless generation
3805     (setq generation 1))
3806   (let ((parent t)
3807         (headers in-headers)
3808         references)
3809     (while (and parent
3810                 (not (zerop generation))
3811                 (setq references (mail-header-references headers)))
3812       (setq headers (if (and references
3813                              (setq parent (gnus-parent-id references)))
3814                         (car (gnus-id-to-thread parent))
3815                       nil))
3816       (decf generation))
3817     (and (not (eq headers in-headers))
3818          headers)))
3819
3820 (defun gnus-id-to-thread (id)
3821   "Return the (sub-)thread where ID appears."
3822   (gnus-gethash id gnus-newsgroup-dependencies))
3823
3824 (defun gnus-id-to-article (id)
3825   "Return the article number of ID."
3826   (let ((thread (gnus-id-to-thread id)))
3827     (when (and thread
3828                (car thread))
3829       (mail-header-number (car thread)))))
3830
3831 (defun gnus-id-to-header (id)
3832   "Return the article headers of ID."
3833   (car (gnus-id-to-thread id)))
3834
3835 (defun gnus-article-displayed-root-p (article)
3836   "Say whether ARTICLE is a root(ish) article."
3837   (let ((level (gnus-summary-thread-level article))
3838         (refs (mail-header-references  (gnus-summary-article-header article)))
3839         particle)
3840     (cond
3841      ((null level) nil)
3842      ((zerop level) t)
3843      ((null refs) t)
3844      ((null (gnus-parent-id refs)) t)
3845      ((and (= 1 level)
3846            (null (setq particle (gnus-id-to-article
3847                                  (gnus-parent-id refs))))
3848            (null (gnus-summary-thread-level particle)))))))
3849
3850 (defun gnus-root-id (id)
3851   "Return the id of the root of the thread where ID appears."
3852   (let (last-id prev)
3853     (while (and id (setq prev (car (gnus-id-to-thread id))))
3854       (setq last-id id
3855             id (gnus-parent-id (mail-header-references prev))))
3856     last-id))
3857
3858 (defun gnus-articles-in-thread (thread)
3859   "Return the list of articles in THREAD."
3860   (cons (mail-header-number (car thread))
3861         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3862
3863 (defun gnus-remove-thread (id &optional dont-remove)
3864   "Remove the thread that has ID in it."
3865   (let (headers thread last-id)
3866     ;; First go up in this thread until we find the root.
3867     (setq last-id (gnus-root-id id)
3868           headers (message-flatten-list (gnus-id-to-thread last-id)))
3869     ;; We have now found the real root of this thread.  It might have
3870     ;; been gathered into some loose thread, so we have to search
3871     ;; through the threads to find the thread we wanted.
3872     (let ((threads gnus-newsgroup-threads)
3873           sub)
3874       (while threads
3875         (setq sub (car threads))
3876         (if (stringp (car sub))
3877             ;; This is a gathered thread, so we look at the roots
3878             ;; below it to find whether this article is in this
3879             ;; gathered root.
3880             (progn
3881               (setq sub (cdr sub))
3882               (while sub
3883                 (when (member (caar sub) headers)
3884                   (setq thread (car threads)
3885                         threads nil
3886                         sub nil))
3887                 (setq sub (cdr sub))))
3888           ;; It's an ordinary thread, so we check it.
3889           (when (eq (car sub) (car headers))
3890             (setq thread sub
3891                   threads nil)))
3892         (setq threads (cdr threads)))
3893       ;; If this article is in no thread, then it's a root.
3894       (if thread
3895           (unless dont-remove
3896             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3897         (setq thread (gnus-id-to-thread last-id)))
3898       (when thread
3899         (prog1
3900             thread                      ; We return this thread.
3901           (unless dont-remove
3902             (if (stringp (car thread))
3903                 (progn
3904                   ;; If we use dummy roots, then we have to remove the
3905                   ;; dummy root as well.
3906                   (when (eq gnus-summary-make-false-root 'dummy)
3907                     ;; We go to the dummy root by going to
3908                     ;; the first sub-"thread", and then one line up.
3909                     (gnus-summary-goto-article
3910                      (mail-header-number (caadr thread)))
3911                     (forward-line -1)
3912                     (gnus-delete-line)
3913                     (gnus-data-compute-positions))
3914                   (setq thread (cdr thread))
3915                   (while thread
3916                     (gnus-remove-thread-1 (car thread))
3917                     (setq thread (cdr thread))))
3918               (gnus-remove-thread-1 thread))))))))
3919
3920 (defun gnus-remove-thread-1 (thread)
3921   "Remove the thread THREAD recursively."
3922   (let ((number (mail-header-number (pop thread)))
3923         d)
3924     (setq thread (reverse thread))
3925     (while thread
3926       (gnus-remove-thread-1 (pop thread)))
3927     (when (setq d (gnus-data-find number))
3928       (goto-char (gnus-data-pos d))
3929       (gnus-summary-show-thread)
3930       (gnus-data-remove
3931        number
3932        (- (gnus-point-at-bol)
3933           (prog1
3934               (1+ (gnus-point-at-eol))
3935             (gnus-delete-line)))))))
3936
3937 (defun gnus-sort-threads-1 (threads func)
3938   (sort (mapcar (lambda (thread)
3939                   (cons (car thread)
3940                         (and (cdr thread)
3941                              (gnus-sort-threads-1 (cdr thread) func))))
3942                 threads) func))
3943
3944 (defun gnus-sort-threads (threads)
3945   "Sort THREADS."
3946   (if (not gnus-thread-sort-functions)
3947       threads
3948     (gnus-message 8 "Sorting threads...")
3949     (prog1
3950         (gnus-sort-threads-1
3951          threads
3952          (gnus-make-sort-function gnus-thread-sort-functions))
3953       (gnus-message 8 "Sorting threads...done"))))
3954
3955 (defun gnus-sort-articles (articles)
3956   "Sort ARTICLES."
3957   (when gnus-article-sort-functions
3958     (gnus-message 7 "Sorting articles...")
3959     (prog1
3960         (setq gnus-newsgroup-headers
3961               (sort articles (gnus-make-sort-function
3962                               gnus-article-sort-functions)))
3963       (gnus-message 7 "Sorting articles...done"))))
3964
3965 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3966 (defmacro gnus-thread-header (thread)
3967   "Return header of first article in THREAD.
3968 Note that THREAD must never, ever be anything else than a variable -
3969 using some other form will lead to serious barfage."
3970   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3971   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3972   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3973         (vector thread) 2))
3974
3975 (defsubst gnus-article-sort-by-number (h1 h2)
3976   "Sort articles by article number."
3977   (< (mail-header-number h1)
3978      (mail-header-number h2)))
3979
3980 (defun gnus-thread-sort-by-number (h1 h2)
3981   "Sort threads by root article number."
3982   (gnus-article-sort-by-number
3983    (gnus-thread-header h1) (gnus-thread-header h2)))
3984
3985 (defsubst gnus-article-sort-by-lines (h1 h2)
3986   "Sort articles by article Lines header."
3987   (< (mail-header-lines h1)
3988      (mail-header-lines h2)))
3989
3990 (defun gnus-thread-sort-by-lines (h1 h2)
3991   "Sort threads by root article Lines header."
3992   (gnus-article-sort-by-lines
3993    (gnus-thread-header h1) (gnus-thread-header h2)))
3994
3995 (defsubst gnus-article-sort-by-chars (h1 h2)
3996   "Sort articles by octet length."
3997   (< (mail-header-chars h1)
3998      (mail-header-chars h2)))
3999
4000 (defun gnus-thread-sort-by-chars (h1 h2)
4001   "Sort threads by root article octet length."
4002   (gnus-article-sort-by-chars
4003    (gnus-thread-header h1) (gnus-thread-header h2)))
4004
4005 (defsubst gnus-article-sort-by-author (h1 h2)
4006   "Sort articles by root author."
4007   (string-lessp
4008    (let ((extract (funcall
4009                    gnus-extract-address-components
4010                    (mail-header-from h1))))
4011      (or (car extract) (cadr extract) ""))
4012    (let ((extract (funcall
4013                    gnus-extract-address-components
4014                    (mail-header-from h2))))
4015      (or (car extract) (cadr extract) ""))))
4016
4017 (defun gnus-thread-sort-by-author (h1 h2)
4018   "Sort threads by root author."
4019   (gnus-article-sort-by-author
4020    (gnus-thread-header h1)  (gnus-thread-header h2)))
4021
4022 (defsubst gnus-article-sort-by-subject (h1 h2)
4023   "Sort articles by root subject."
4024   (string-lessp
4025    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4026    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4027
4028 (defun gnus-thread-sort-by-subject (h1 h2)
4029   "Sort threads by root subject."
4030   (gnus-article-sort-by-subject
4031    (gnus-thread-header h1) (gnus-thread-header h2)))
4032
4033 (defsubst gnus-article-sort-by-date (h1 h2)
4034   "Sort articles by root article date."
4035   (time-less-p
4036    (gnus-date-get-time (mail-header-date h1))
4037    (gnus-date-get-time (mail-header-date h2))))
4038
4039 (defun gnus-thread-sort-by-date (h1 h2)
4040   "Sort threads by root article date."
4041   (gnus-article-sort-by-date
4042    (gnus-thread-header h1) (gnus-thread-header h2)))
4043
4044 (defsubst gnus-article-sort-by-score (h1 h2)
4045   "Sort articles by root article score.
4046 Unscored articles will be counted as having a score of zero."
4047   (> (or (cdr (assq (mail-header-number h1)
4048                     gnus-newsgroup-scored))
4049          gnus-summary-default-score 0)
4050      (or (cdr (assq (mail-header-number h2)
4051                     gnus-newsgroup-scored))
4052          gnus-summary-default-score 0)))
4053
4054 (defun gnus-thread-sort-by-score (h1 h2)
4055   "Sort threads by root article score."
4056   (gnus-article-sort-by-score
4057    (gnus-thread-header h1) (gnus-thread-header h2)))
4058
4059 (defun gnus-thread-sort-by-total-score (h1 h2)
4060   "Sort threads by the sum of all scores in the thread.
4061 Unscored articles will be counted as having a score of zero."
4062   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4063
4064 (defun gnus-thread-total-score (thread)
4065   ;; This function find the total score of THREAD.
4066   (cond ((null thread)
4067          0)
4068         ((consp thread)
4069          (if (stringp (car thread))
4070              (apply gnus-thread-score-function 0
4071                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4072            (gnus-thread-total-score-1 thread)))
4073         (t
4074          (gnus-thread-total-score-1 (list thread)))))
4075
4076 (defun gnus-thread-total-score-1 (root)
4077   ;; This function find the total score of the thread below ROOT.
4078   (setq root (car root))
4079   (apply gnus-thread-score-function
4080          (or (append
4081               (mapcar 'gnus-thread-total-score
4082                       (cdr (gnus-id-to-thread (mail-header-id root))))
4083               (when (> (mail-header-number root) 0)
4084                 (list (or (cdr (assq (mail-header-number root)
4085                                      gnus-newsgroup-scored))
4086                           gnus-summary-default-score 0))))
4087              (list gnus-summary-default-score)
4088              '(0))))
4089
4090 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4091 (defvar gnus-tmp-prev-subject nil)
4092 (defvar gnus-tmp-false-parent nil)
4093 (defvar gnus-tmp-root-expunged nil)
4094 (defvar gnus-tmp-dummy-line nil)
4095
4096 (eval-when-compile (defvar gnus-tmp-header))
4097 (defun gnus-extra-header (type &optional header)
4098   "Return the extra header of TYPE."
4099   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4100       ""))
4101
4102 (defvar gnus-tmp-thread-tree-header-string "")
4103
4104 (defvar gnus-sum-thread-tree-root "> "
4105   "With %B spec, used for the root of a thread.
4106 If nil, use subject instead.")
4107 (defvar gnus-sum-thread-tree-single-indent ""
4108   "With %B spec, used for a thread with just one message.
4109 If nil, use subject instead.")
4110 (defvar gnus-sum-thread-tree-vertical "| "
4111   "With %B spec, used for drawing a vertical line.")
4112 (defvar gnus-sum-thread-tree-indent "  "
4113   "With %B spec, used for indenting.")
4114 (defvar gnus-sum-thread-tree-leaf-with-other "+-> "
4115   "With %B spec, used for a leaf with brothers.")
4116 (defvar gnus-sum-thread-tree-single-leaf "\\-> "
4117   "With %B spec, used for a leaf without brothers.")
4118
4119 (defun gnus-summary-prepare-threads (threads)
4120   "Prepare summary buffer from THREADS and indentation LEVEL.
4121 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4122 or a straight list of headers."
4123   (gnus-message 7 "Generating summary...")
4124
4125   (setq gnus-newsgroup-threads threads)
4126   (beginning-of-line)
4127
4128   (let ((gnus-tmp-level 0)
4129         (default-score (or gnus-summary-default-score 0))
4130         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4131         thread number subject stack state gnus-tmp-gathered beg-match
4132         new-roots gnus-tmp-new-adopts thread-end
4133         gnus-tmp-header gnus-tmp-unread
4134         gnus-tmp-replied gnus-tmp-subject-or-nil
4135         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4136         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4137         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4138         tree-stack)
4139
4140     (setq gnus-tmp-prev-subject nil)
4141
4142     (if (vectorp (car threads))
4143         ;; If this is a straight (sic) list of headers, then a
4144         ;; threaded summary display isn't required, so we just create
4145         ;; an unthreaded one.
4146         (gnus-summary-prepare-unthreaded threads)
4147
4148       ;; Do the threaded display.
4149
4150       (while (or threads stack gnus-tmp-new-adopts new-roots)
4151
4152         (if (and (= gnus-tmp-level 0)
4153                  (or (not stack)
4154                      (= (caar stack) 0))
4155                  (not gnus-tmp-false-parent)
4156                  (or gnus-tmp-new-adopts new-roots))
4157             (if gnus-tmp-new-adopts
4158                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4159                       thread (list (car gnus-tmp-new-adopts))
4160                       gnus-tmp-header (caar thread)
4161                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4162               (when new-roots
4163                 (setq thread (list (car new-roots))
4164                       gnus-tmp-header (caar thread)
4165                       new-roots (cdr new-roots))))
4166
4167           (if threads
4168               ;; If there are some threads, we do them before the
4169               ;; threads on the stack.
4170               (setq thread threads
4171                     gnus-tmp-header (caar thread))
4172             ;; There were no current threads, so we pop something off
4173             ;; the stack.
4174             (setq state (car stack)
4175                   gnus-tmp-level (car state)
4176                   tree-stack (cadr state)
4177                   thread (caddr state)
4178                   stack (cdr stack)
4179                   gnus-tmp-header (caar thread))))
4180
4181         (setq gnus-tmp-false-parent nil)
4182         (setq gnus-tmp-root-expunged nil)
4183         (setq thread-end nil)
4184
4185         (if (stringp gnus-tmp-header)
4186             ;; The header is a dummy root.
4187             (cond
4188              ((eq gnus-summary-make-false-root 'adopt)
4189               ;; We let the first article adopt the rest.
4190               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4191                                                (cddar thread)))
4192               (setq gnus-tmp-gathered
4193                     (nconc (mapcar
4194                             (lambda (h) (mail-header-number (car h)))
4195                             (cddar thread))
4196                            gnus-tmp-gathered))
4197               (setq thread (cons (list (caar thread)
4198                                        (cadar thread))
4199                                  (cdr thread)))
4200               (setq gnus-tmp-level -1
4201                     gnus-tmp-false-parent t))
4202              ((eq gnus-summary-make-false-root 'empty)
4203               ;; We print adopted articles with empty subject fields.
4204               (setq gnus-tmp-gathered
4205                     (nconc (mapcar
4206                             (lambda (h) (mail-header-number (car h)))
4207                             (cddar thread))
4208                            gnus-tmp-gathered))
4209               (setq gnus-tmp-level -1))
4210              ((eq gnus-summary-make-false-root 'dummy)
4211               ;; We remember that we probably want to output a dummy
4212               ;; root.
4213               (setq gnus-tmp-dummy-line gnus-tmp-header)
4214               (setq gnus-tmp-prev-subject gnus-tmp-header))
4215              (t
4216               ;; We do not make a root for the gathered
4217               ;; sub-threads at all.
4218               (setq gnus-tmp-level -1)))
4219
4220           (setq number (mail-header-number gnus-tmp-header)
4221                 subject (mail-header-subject gnus-tmp-header))
4222
4223           (cond
4224            ;; If the thread has changed subject, we might want to make
4225            ;; this subthread into a root.
4226            ((and (null gnus-thread-ignore-subject)
4227                  (not (zerop gnus-tmp-level))
4228                  gnus-tmp-prev-subject
4229                  (not (inline
4230                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4231             (setq new-roots (nconc new-roots (list (car thread)))
4232                   thread-end t
4233                   gnus-tmp-header nil))
4234            ;; If the article lies outside the current limit,
4235            ;; then we do not display it.
4236            ((not (memq number gnus-newsgroup-limit))
4237             (setq gnus-tmp-gathered
4238                   (nconc (mapcar
4239                           (lambda (h) (mail-header-number (car h)))
4240                           (cdar thread))
4241                          gnus-tmp-gathered))
4242             (setq gnus-tmp-new-adopts (if (cdar thread)
4243                                           (append gnus-tmp-new-adopts
4244                                                   (cdar thread))
4245                                         gnus-tmp-new-adopts)
4246                   thread-end t
4247                   gnus-tmp-header nil)
4248             (when (zerop gnus-tmp-level)
4249               (setq gnus-tmp-root-expunged t)))
4250            ;; Perhaps this article is to be marked as read?
4251            ((and gnus-summary-mark-below
4252                  (< (or (cdr (assq number gnus-newsgroup-scored))
4253                         default-score)
4254                     gnus-summary-mark-below)
4255                  ;; Don't touch sparse articles.
4256                  (not (gnus-summary-article-sparse-p number))
4257                  (not (gnus-summary-article-ancient-p number)))
4258             (setq gnus-newsgroup-unreads
4259                   (delq number gnus-newsgroup-unreads))
4260             (if gnus-newsgroup-auto-expire
4261                 (push number gnus-newsgroup-expirable)
4262               (push (cons number gnus-low-score-mark)
4263                     gnus-newsgroup-reads))))
4264
4265           (when gnus-tmp-header
4266             ;; We may have an old dummy line to output before this
4267             ;; article.
4268             (when (and gnus-tmp-dummy-line
4269                        (gnus-subject-equal
4270                         gnus-tmp-dummy-line
4271                         (mail-header-subject gnus-tmp-header)))
4272               (gnus-summary-insert-dummy-line
4273                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4274               (setq gnus-tmp-dummy-line nil))
4275
4276             ;; Compute the mark.
4277             (setq gnus-tmp-unread (gnus-article-mark number))
4278
4279             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4280                                   gnus-tmp-header gnus-tmp-level)
4281                   gnus-newsgroup-data)
4282
4283             ;; Actually insert the line.
4284             (setq
4285              gnus-tmp-subject-or-nil
4286              (cond
4287               ((and gnus-thread-ignore-subject
4288                     gnus-tmp-prev-subject
4289                     (not (inline (gnus-subject-equal
4290                                   gnus-tmp-prev-subject subject))))
4291                subject)
4292               ((zerop gnus-tmp-level)
4293                (if (and (eq gnus-summary-make-false-root 'empty)
4294                         (memq number gnus-tmp-gathered)
4295                         gnus-tmp-prev-subject
4296                         (inline (gnus-subject-equal
4297                                  gnus-tmp-prev-subject subject)))
4298                    gnus-summary-same-subject
4299                  subject))
4300               (t gnus-summary-same-subject)))
4301             (if (and (eq gnus-summary-make-false-root 'adopt)
4302                      (= gnus-tmp-level 1)
4303                      (memq number gnus-tmp-gathered))
4304                 (setq gnus-tmp-opening-bracket ?\<
4305                       gnus-tmp-closing-bracket ?\>)
4306               (setq gnus-tmp-opening-bracket ?\[
4307                     gnus-tmp-closing-bracket ?\]))
4308             (setq
4309              gnus-tmp-indentation
4310              (aref gnus-thread-indent-array gnus-tmp-level)
4311              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4312              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4313                                 gnus-summary-default-score 0)
4314              gnus-tmp-score-char
4315              (if (or (null gnus-summary-default-score)
4316                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4317                          gnus-summary-zcore-fuzz))
4318                  ?                      ;Whitespace
4319                (if (< gnus-tmp-score gnus-summary-default-score)
4320                    gnus-score-below-mark gnus-score-over-mark))
4321              gnus-tmp-replied
4322              (cond ((memq number gnus-newsgroup-processable)
4323                     gnus-process-mark)
4324                    ((memq number gnus-newsgroup-cached)
4325                     gnus-cached-mark)
4326                    ((memq number gnus-newsgroup-replied)
4327                     gnus-replied-mark)
4328                    ((memq number gnus-newsgroup-forwarded)
4329                     gnus-forwarded-mark)
4330                    ((memq number gnus-newsgroup-saved)
4331                     gnus-saved-mark)
4332                    ((memq number gnus-newsgroup-recent)
4333                     gnus-recent-mark)
4334                    (t gnus-no-mark))
4335              gnus-tmp-from (mail-header-from gnus-tmp-header)
4336              gnus-tmp-name
4337              (cond
4338               ((string-match "<[^>]+> *$" gnus-tmp-from)
4339                (setq beg-match (match-beginning 0))
4340                (or (and (string-match "^\".+\"" gnus-tmp-from)
4341                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4342                    (substring gnus-tmp-from 0 beg-match)))
4343               ((string-match "(.+)" gnus-tmp-from)
4344                (substring gnus-tmp-from
4345                           (1+ (match-beginning 0)) (1- (match-end 0))))
4346               (t gnus-tmp-from))
4347              gnus-tmp-thread-tree-header-string
4348              (cond 
4349               ((not gnus-show-threads) "")
4350               ((zerop gnus-tmp-level)
4351                (if (cdar thread) 
4352                    (or gnus-sum-thread-tree-root subject)
4353                  (or gnus-sum-thread-tree-single-indent subject)))
4354               (t
4355                (concat (apply 'concat
4356                               (mapcar (lambda (item) 
4357                                         (if (= item 1) 
4358                                             gnus-sum-thread-tree-vertical
4359                                           gnus-sum-thread-tree-indent))
4360                                       (cdr (reverse tree-stack))))
4361                        (if (nth 1 thread) 
4362                            gnus-sum-thread-tree-leaf-with-other
4363                          gnus-sum-thread-tree-single-leaf)))))
4364             (when (string= gnus-tmp-name "")
4365               (setq gnus-tmp-name gnus-tmp-from))
4366             (unless (numberp gnus-tmp-lines)
4367               (setq gnus-tmp-lines -1))
4368             (when (= gnus-tmp-lines -1)
4369               (setq gnus-tmp-lines "?"))
4370             (gnus-put-text-property
4371              (point)
4372              (progn (eval gnus-summary-line-format-spec) (point))
4373              'gnus-number number)
4374             (when gnus-visual-p
4375               (forward-line -1)
4376               (gnus-run-hooks 'gnus-summary-update-hook)
4377               (forward-line 1))
4378
4379             (setq gnus-tmp-prev-subject subject)))
4380
4381         (when (nth 1 thread)
4382           (push (list (max 0 gnus-tmp-level)
4383                       (copy-list tree-stack)
4384                       (nthcdr 1 thread))
4385                 stack))
4386         (push (if (nth 1 thread) 1 0) tree-stack)
4387         (incf gnus-tmp-level)
4388         (setq threads (if thread-end nil (cdar thread)))
4389         (unless threads
4390           (setq gnus-tmp-level 0)))))
4391   (gnus-message 7 "Generating summary...done"))
4392
4393 (defun gnus-summary-prepare-unthreaded (headers)
4394   "Generate an unthreaded summary buffer based on HEADERS."
4395   (let (header number mark)
4396
4397     (beginning-of-line)
4398
4399     (while headers
4400       ;; We may have to root out some bad articles...
4401       (when (memq (setq number (mail-header-number
4402                                 (setq header (pop headers))))
4403                   gnus-newsgroup-limit)
4404         ;; Mark article as read when it has a low score.
4405         (when (and gnus-summary-mark-below
4406                    (< (or (cdr (assq number gnus-newsgroup-scored))
4407                           gnus-summary-default-score 0)
4408                       gnus-summary-mark-below)
4409                    (not (gnus-summary-article-ancient-p number)))
4410           (setq gnus-newsgroup-unreads
4411                 (delq number gnus-newsgroup-unreads))
4412           (if gnus-newsgroup-auto-expire
4413               (push number gnus-newsgroup-expirable)
4414             (push (cons number gnus-low-score-mark)
4415                   gnus-newsgroup-reads)))
4416
4417         (setq mark (gnus-article-mark number))
4418         (push (gnus-data-make number mark (1+ (point)) header 0)
4419               gnus-newsgroup-data)
4420         (gnus-summary-insert-line
4421          header 0 number
4422          mark (memq number gnus-newsgroup-replied)
4423          (memq number gnus-newsgroup-expirable)
4424          (mail-header-subject header) nil
4425          (cdr (assq number gnus-newsgroup-scored))
4426          (memq number gnus-newsgroup-processable))))))
4427
4428 (defun gnus-summary-remove-list-identifiers ()
4429   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4430   (let ((regexp (if (consp gnus-list-identifiers)
4431                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4432                   gnus-list-identifiers))
4433         changed subject)
4434     (when regexp
4435       (dolist (header gnus-newsgroup-headers)
4436         (setq subject (mail-header-subject header)
4437               changed nil)
4438         (while (string-match
4439                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4440                 subject)
4441           (setq subject
4442                 (concat (substring subject 0 (match-beginning 2))
4443                         (substring subject (match-end 0)))
4444                 changed t))
4445         (when (and changed
4446                    (string-match
4447                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4448           (setq subject
4449                 (concat (substring subject 0 (match-beginning 1))
4450                         (substring subject (match-end 1)))))
4451         (when changed
4452           (mail-header-set-subject header subject))))))
4453
4454 (defun gnus-fetch-headers (articles)
4455   "Fetch headers of ARTICLES."
4456   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4457     (gnus-message 5 "Fetching headers for %s..." name)
4458     (prog1
4459         (if (eq 'nov
4460                 (setq gnus-headers-retrieved-by
4461                       (gnus-retrieve-headers
4462                        articles gnus-newsgroup-name
4463                        ;; We might want to fetch old headers, but
4464                        ;; not if there is only 1 article.
4465                        (and (or (and
4466                                  (not (eq gnus-fetch-old-headers 'some))
4467                                  (not (numberp gnus-fetch-old-headers)))
4468                                 (> (length articles) 1))
4469                             gnus-fetch-old-headers))))
4470             (gnus-get-newsgroup-headers-xover
4471              articles nil nil gnus-newsgroup-name t)
4472           (gnus-get-newsgroup-headers))
4473       (gnus-message 5 "Fetching headers for %s...done" name))))
4474
4475 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4476   "Select newsgroup GROUP.
4477 If READ-ALL is non-nil, all articles in the group are selected.
4478 If SELECT-ARTICLES, only select those articles from GROUP."
4479   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4480          ;;!!! Dirty hack; should be removed.
4481          (gnus-summary-ignore-duplicates
4482           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4483               t
4484             gnus-summary-ignore-duplicates))
4485          (info (nth 2 entry))
4486          articles fetched-articles cached)
4487
4488     (unless (gnus-check-server
4489              (setq gnus-current-select-method
4490                    (gnus-find-method-for-group group)))
4491       (error "Couldn't open server"))
4492
4493     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4494         (gnus-activate-group group)     ; Or we can activate it...
4495         (progn                          ; Or we bug out.
4496           (when (equal major-mode 'gnus-summary-mode)
4497             (kill-buffer (current-buffer)))
4498           (error "Couldn't activate group %s: %s"
4499                  group (gnus-status-message group))))
4500
4501     (unless (gnus-request-group group t)
4502       (when (equal major-mode 'gnus-summary-mode)
4503         (kill-buffer (current-buffer)))
4504       (error "Couldn't request group %s: %s"
4505              group (gnus-status-message group)))
4506
4507     (setq gnus-newsgroup-name group)
4508     (setq gnus-newsgroup-unselected nil)
4509     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4510     (gnus-summary-setup-default-charset)
4511
4512     ;; Adjust and set lists of article marks.
4513     (when info
4514       (gnus-adjust-marked-articles info))
4515
4516 ;; Kludge to avoid having cached articles nixed out in virtual groups.
4517     (when (gnus-virtual-group-p group)
4518       (setq cached gnus-newsgroup-cached))
4519
4520     (setq gnus-newsgroup-unreads
4521           (gnus-set-difference
4522            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4523            gnus-newsgroup-dormant))
4524
4525     (setq gnus-newsgroup-processable nil)
4526
4527     (gnus-update-read-articles group gnus-newsgroup-unreads)
4528
4529     (if (setq articles select-articles)
4530         (setq gnus-newsgroup-unselected
4531               (gnus-sorted-intersection
4532                gnus-newsgroup-unreads
4533                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4534       (setq articles (gnus-articles-to-read group read-all)))
4535
4536     (cond
4537      ((null articles)
4538 ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4539       'quit)
4540      ((eq articles 0) nil)
4541      (t
4542       ;; Init the dependencies hash table.
4543       (setq gnus-newsgroup-dependencies
4544             (gnus-make-hashtable (length articles)))
4545       (gnus-set-global-variables)
4546       ;; Retrieve the headers and read them in.
4547       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4548
4549 ;; Kludge to avoid having cached articles nixed out in virtual groups.
4550       (when cached
4551         (setq gnus-newsgroup-cached cached))
4552
4553       ;; Suppress duplicates?
4554       (when gnus-suppress-duplicates
4555         (gnus-dup-suppress-articles))
4556
4557       ;; Set the initial limit.
4558       (setq gnus-newsgroup-limit (copy-sequence articles))
4559       ;; Remove canceled articles from the list of unread articles.
4560       (setq gnus-newsgroup-unreads
4561             (gnus-set-sorted-intersection
4562              gnus-newsgroup-unreads
4563              (setq fetched-articles
4564                    (mapcar (lambda (headers) (mail-header-number headers))
4565                            gnus-newsgroup-headers))))
4566       ;; Removed marked articles that do not exist.
4567       (gnus-update-missing-marks
4568        (gnus-sorted-complement fetched-articles articles))
4569       ;; We might want to build some more threads first.
4570       (when (and gnus-fetch-old-headers
4571                  (eq gnus-headers-retrieved-by 'nov))
4572         (if (eq gnus-fetch-old-headers 'invisible)
4573             (gnus-build-all-threads)
4574           (gnus-build-old-threads)))
4575       ;; Let the Gnus agent mark articles as read.
4576       (when gnus-agent
4577         (gnus-agent-get-undownloaded-list))
4578       ;; Remove list identifiers from subject
4579       (when gnus-list-identifiers
4580         (gnus-summary-remove-list-identifiers))
4581       ;; Check whether auto-expire is to be done in this group.
4582       (setq gnus-newsgroup-auto-expire
4583             (gnus-group-auto-expirable-p group))
4584       ;; Set up the article buffer now, if necessary.
4585       (unless gnus-single-article-buffer
4586         (gnus-article-setup-buffer))
4587       ;; First and last article in this newsgroup.
4588       (when gnus-newsgroup-headers
4589         (setq gnus-newsgroup-begin
4590               (mail-header-number (car gnus-newsgroup-headers))
4591               gnus-newsgroup-end
4592               (mail-header-number
4593                (gnus-last-element gnus-newsgroup-headers))))
4594       ;; GROUP is successfully selected.
4595       (or gnus-newsgroup-headers t)))))
4596
4597 (defun gnus-articles-to-read (group &optional read-all)
4598   "Find out what articles the user wants to read."
4599   (let* ((articles
4600           ;; Select all articles if `read-all' is non-nil, or if there
4601           ;; are no unread articles.
4602           (if (or read-all
4603                   (and (zerop (length gnus-newsgroup-marked))
4604                        (zerop (length gnus-newsgroup-unreads)))
4605                   (eq (gnus-group-find-parameter group 'display)
4606                       'all))
4607               (or
4608                (gnus-uncompress-range (gnus-active group))
4609                (gnus-cache-articles-in-group group))
4610             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4611                           (copy-sequence gnus-newsgroup-unreads))
4612                   '<)))
4613          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4614          (scored (length scored-list))
4615          (number (length articles))
4616          (marked (+ (length gnus-newsgroup-marked)
4617                     (length gnus-newsgroup-dormant)))
4618          (select
4619           (cond
4620            ((numberp read-all)
4621             read-all)
4622            (t
4623             (condition-case ()
4624                 (cond
4625                  ((and (or (<= scored marked) (= scored number))
4626                        (numberp gnus-large-newsgroup)
4627                        (> number gnus-large-newsgroup))
4628                   (let ((input
4629                          (read-string
4630                           (format
4631                            "How many articles from %s (default %d): "
4632                            (gnus-limit-string
4633                             (gnus-group-decoded-name gnus-newsgroup-name)
4634                             35)
4635                            number))))
4636                     (if (string-match "^[ \t]*$" input) number input)))
4637                  ((and (> scored marked) (< scored number)
4638                        (> (- scored number) 20))
4639                   (let ((input
4640                          (read-string
4641                           (format "%s %s (%d scored, %d total): "
4642                                   "How many articles from"
4643                                   (gnus-group-decoded-name group)
4644                                   scored number))))
4645                     (if (string-match "^[ \t]*$" input)
4646                         number input)))
4647                  (t number))
4648               (quit
4649                (message "Quit getting the articles to read")
4650                nil))))))
4651     (setq select (if (stringp select) (string-to-number select) select))
4652     (if (or (null select) (zerop select))
4653         select
4654       (if (and (not (zerop scored)) (<= (abs select) scored))
4655           (progn
4656             (setq articles (sort scored-list '<))
4657             (setq number (length articles)))
4658         (setq articles (copy-sequence articles)))
4659
4660       (when (< (abs select) number)
4661         (if (< select 0)
4662             ;; Select the N oldest articles.
4663             (setcdr (nthcdr (1- (abs select)) articles) nil)
4664           ;; Select the N most recent articles.
4665           (setq articles (nthcdr (- number select) articles))))
4666       (setq gnus-newsgroup-unselected
4667             (gnus-sorted-intersection
4668              gnus-newsgroup-unreads
4669              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4670       (when gnus-alter-articles-to-read-function
4671         (setq gnus-newsgroup-unreads
4672               (sort
4673                (funcall gnus-alter-articles-to-read-function
4674                         gnus-newsgroup-name gnus-newsgroup-unreads)
4675                '<)))
4676       articles)))
4677
4678 (defun gnus-killed-articles (killed articles)
4679   (let (out)
4680     (while articles
4681       (when (inline (gnus-member-of-range (car articles) killed))
4682         (push (car articles) out))
4683       (setq articles (cdr articles)))
4684     out))
4685
4686 (defun gnus-uncompress-marks (marks)
4687   "Uncompress the mark ranges in MARKS."
4688   (let ((uncompressed '(score bookmark))
4689         out)
4690     (while marks
4691       (if (memq (caar marks) uncompressed)
4692           (push (car marks) out)
4693         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4694       (setq marks (cdr marks)))
4695     out))
4696
4697 (defun gnus-adjust-marked-articles (info)
4698   "Set all article lists and remove all marks that are no longer valid."
4699   (let* ((marked-lists (gnus-info-marks info))
4700          (active (gnus-active (gnus-info-group info)))
4701          (min (car active))
4702          (max (cdr active))
4703          (types gnus-article-mark-lists)
4704          (uncompressed '(score bookmark killed))
4705          marks var articles article mark)
4706
4707     (while marked-lists
4708       (setq marks (pop marked-lists))
4709       (set (setq var (intern (format "gnus-newsgroup-%s"
4710                                      (car (rassq (setq mark (car marks))
4711                                                  types)))))
4712            (if (memq (car marks) uncompressed) (cdr marks)
4713              (gnus-uncompress-range (cdr marks))))
4714
4715       (setq articles (symbol-value var))
4716
4717       ;; All articles have to be subsets of the active articles.
4718       (cond
4719        ;; Adjust "simple" lists.
4720        ((memq mark '(tick dormant expire reply save))
4721         (while articles
4722           (when (or (< (setq article (pop articles)) min) (> article max))
4723             (set var (delq article (symbol-value var))))))
4724        ;; Adjust assocs.
4725        ((memq mark uncompressed)
4726         (when (not (listp (cdr (symbol-value var))))
4727           (set var (list (symbol-value var))))
4728         (when (not (listp (cdr articles)))
4729           (setq articles (list articles)))
4730         (while articles
4731           (when (or (not (consp (setq article (pop articles))))
4732                     (< (car article) min)
4733                     (> (car article) max))
4734             (set var (delq article (symbol-value var))))))))))
4735
4736 (defun gnus-update-missing-marks (missing)
4737   "Go through the list of MISSING articles and remove them from the mark lists."
4738   (when missing
4739     (let ((types gnus-article-mark-lists)
4740           var m)
4741       ;; Go through all types.
4742       (while types
4743         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4744         (when (symbol-value var)
4745          ;; This list has articles.  So we delete all missing articles
4746           ;; from it.
4747           (setq m missing)
4748           (while m
4749             (set var (delq (pop m) (symbol-value var)))))))))
4750
4751 (defun gnus-update-marks ()
4752   "Enter the various lists of marked articles into the newsgroup info list."
4753   (let ((types gnus-article-mark-lists)
4754         (info (gnus-get-info gnus-newsgroup-name))
4755         (uncompressed '(score bookmark killed))
4756         type list newmarked symbol delta-marks)
4757     (when info
4758       ;; Add all marks lists to the list of marks lists.
4759       (while (setq type (pop types))
4760         (setq list (symbol-value
4761                     (setq symbol
4762                           (intern (format "gnus-newsgroup-%s"
4763                                           (car type))))))
4764
4765         (when list
4766           ;; Get rid of the entries of the articles that have the
4767           ;; default score.
4768           (when (and (eq (cdr type) 'score)
4769                      gnus-save-score
4770                      list)
4771             (let* ((arts list)
4772                    (prev (cons nil list))
4773                    (all prev))
4774               (while arts
4775                 (if (or (not (consp (car arts)))
4776                         (= (cdar arts) gnus-summary-default-score))
4777                     (setcdr prev (cdr arts))
4778                   (setq prev arts))
4779                 (setq arts (cdr arts)))
4780               (setq list (cdr all)))))
4781
4782         (unless (memq (cdr type) uncompressed)
4783           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4784
4785         (when (gnus-check-backend-function
4786                'request-set-mark gnus-newsgroup-name)
4787           ;; propagate flags to server, with the following exceptions:
4788           ;; uncompressed:s are not proper flags (they are cons cells)
4789           ;; cache is a internal gnus flag
4790           ;; download are local to one gnus installation (well)
4791           ;; unsend are for nndraft groups only
4792           ;; xxx: generality of this?  this suits nnimap anyway
4793           (unless (memq (cdr type) (append '(cache download unsend)
4794                                            uncompressed))
4795             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4796                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4797                    (add (gnus-remove-from-range
4798                          (gnus-copy-sequence list) old)))
4799               (when add
4800                 (push (list add 'add (list (cdr type))) delta-marks))
4801               (when del
4802                 (push (list del 'del (list (cdr type))) delta-marks)))))
4803
4804         (when list
4805           (push (cons (cdr type) list) newmarked)))
4806
4807       (when delta-marks
4808         (unless (gnus-check-group gnus-newsgroup-name)
4809           (error "Can't open server for %s" gnus-newsgroup-name))
4810         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4811
4812       ;; Enter these new marks into the info of the group.
4813       (if (nthcdr 3 info)
4814           (setcar (nthcdr 3 info) newmarked)
4815         ;; Add the marks lists to the end of the info.
4816         (when newmarked
4817           (setcdr (nthcdr 2 info) (list newmarked))))
4818
4819       ;; Cut off the end of the info if there's nothing else there.
4820       (let ((i 5))
4821         (while (and (> i 2)
4822                     (not (nth i info)))
4823           (when (nthcdr (decf i) info)
4824             (setcdr (nthcdr i info) nil)))))))
4825
4826 (defun gnus-set-mode-line (where)
4827   "Set the mode line of the article or summary buffers.
4828 If WHERE is `summary', the summary mode line format will be used."
4829   ;; Is this mode line one we keep updated?
4830   (when (and (memq where gnus-updated-mode-lines)
4831              (symbol-value
4832               (intern (format "gnus-%s-mode-line-format-spec" where))))
4833     (let (mode-string)
4834       (save-excursion
4835         ;; We evaluate this in the summary buffer since these
4836         ;; variables are buffer-local to that buffer.
4837         (set-buffer gnus-summary-buffer)
4838        ;; We bind all these variables that are used in the `eval' form
4839         ;; below.
4840         (let* ((mformat (symbol-value
4841                          (intern
4842                           (format "gnus-%s-mode-line-format-spec" where))))
4843                (gnus-tmp-group-name (gnus-group-decoded-name
4844                                      gnus-newsgroup-name))
4845                (gnus-tmp-article-number (or gnus-current-article 0))
4846                (gnus-tmp-unread gnus-newsgroup-unreads)
4847                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4848                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4849                (gnus-tmp-unread-and-unselected
4850                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4851                             (zerop gnus-tmp-unselected))
4852                        "")
4853                       ((zerop gnus-tmp-unselected)
4854                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4855                       (t (format "{%d(+%d) more}"
4856                                  gnus-tmp-unread-and-unticked
4857                                  gnus-tmp-unselected))))
4858                (gnus-tmp-subject
4859                 (if (and gnus-current-headers
4860                          (vectorp gnus-current-headers))
4861                     (gnus-mode-string-quote
4862                      (mail-header-subject gnus-current-headers))
4863                   ""))
4864                bufname-length max-len
4865                gnus-tmp-header) ;; passed as argument to any user-format-funcs
4866           (setq mode-string (eval mformat))
4867           (setq bufname-length (if (string-match "%b" mode-string)
4868                                    (- (length
4869                                        (buffer-name
4870                                         (if (eq where 'summary)
4871                                             nil
4872                                           (get-buffer gnus-article-buffer))))
4873                                       2)
4874                                  0))
4875           (setq max-len (max 4 (if gnus-mode-non-string-length
4876                                    (- (window-width)
4877                                       gnus-mode-non-string-length
4878                                       bufname-length)
4879                                  (length mode-string))))
4880           ;; We might have to chop a bit of the string off...
4881           (when (> (length mode-string) max-len)
4882             (setq mode-string
4883                   (concat (truncate-string-to-width mode-string (- max-len 3))
4884                           "...")))
4885           ;; Pad the mode string a bit.
4886           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4887       ;; Update the mode line.
4888       (setq mode-line-buffer-identification
4889             (gnus-mode-line-buffer-identification (list mode-string)))
4890       (set-buffer-modified-p t))))
4891
4892 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4893   "Go through the HEADERS list and add all Xrefs to a hash table.
4894 The resulting hash table is returned, or nil if no Xrefs were found."
4895   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4896          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4897          (xref-hashtb (gnus-make-hashtable))
4898          start group entry number xrefs header)
4899     (while headers
4900       (setq header (pop headers))
4901       (when (and (setq xrefs (mail-header-xref header))
4902                  (not (memq (setq number (mail-header-number header))
4903                             unreads)))
4904         (setq start 0)
4905         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4906           (setq start (match-end 0))
4907           (setq group (if prefix
4908                           (concat prefix (substring xrefs (match-beginning 1)
4909                                                     (match-end 1)))
4910                         (substring xrefs (match-beginning 1) (match-end 1))))
4911           (setq number
4912                 (string-to-int (substring xrefs (match-beginning 2)
4913                                           (match-end 2))))
4914           (if (setq entry (gnus-gethash group xref-hashtb))
4915               (setcdr entry (cons number (cdr entry)))
4916             (gnus-sethash group (cons number nil) xref-hashtb)))))
4917     (and start xref-hashtb)))
4918
4919 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4920   "Look through all the headers and mark the Xrefs as read."
4921   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4922         name entry info xref-hashtb idlist method nth4)
4923     (save-excursion
4924       (set-buffer gnus-group-buffer)
4925       (when (setq xref-hashtb
4926                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4927         (mapatoms
4928          (lambda (group)
4929            (unless (string= from-newsgroup (setq name (symbol-name group)))
4930              (setq idlist (symbol-value group))
4931              ;; Dead groups are not updated.
4932              (and (prog1
4933                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4934                             info (nth 2 entry))
4935                     (when (stringp (setq nth4 (gnus-info-method info)))
4936                       (setq nth4 (gnus-server-to-method nth4))))
4937                   ;; Only do the xrefs if the group has the same
4938                   ;; select method as the group we have just read.
4939                   (or (gnus-methods-equal-p
4940                        nth4 (gnus-find-method-for-group from-newsgroup))
4941                       virtual
4942                       (equal nth4 (setq method (gnus-find-method-for-group
4943                                                 from-newsgroup)))
4944                       (and (equal (car nth4) (car method))
4945                            (equal (nth 1 nth4) (nth 1 method))))
4946                   gnus-use-cross-reference
4947                   (or (not (eq gnus-use-cross-reference t))
4948                       virtual
4949                       ;; Only do cross-references on subscribed
4950                       ;; groups, if that is what is wanted.
4951                       (<= (gnus-info-level info) gnus-level-subscribed))
4952                   (gnus-group-make-articles-read name idlist))))
4953          xref-hashtb)))))
4954
4955 (defun gnus-compute-read-articles (group articles)
4956   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4957          (info (nth 2 entry))
4958          (active (gnus-active group))
4959          ninfo)
4960     (when entry
4961       ;; First peel off all invalid article numbers.
4962       (when active
4963         (let ((ids articles)
4964               id first)
4965           (while (setq id (pop ids))
4966             (when (and first (> id (cdr active)))
4967               ;; We'll end up in this situation in one particular
4968               ;; obscure situation.  If you re-scan a group and get
4969               ;; a new article that is cross-posted to a different
4970               ;; group that has not been re-scanned, you might get
4971               ;; crossposted article that has a higher number than
4972               ;; Gnus believes possible.  So we re-activate this
4973               ;; group as well.  This might mean doing the
4974               ;; crossposting thingy will *increase* the number
4975               ;; of articles in some groups.  Tsk, tsk.
4976               (setq active (or (gnus-activate-group group) active)))
4977             (when (or (> id (cdr active))
4978                       (< id (car active)))
4979               (setq articles (delq id articles))))))
4980       ;; If the read list is nil, we init it.
4981       (if (and active
4982                (null (gnus-info-read info))
4983                (> (car active) 1))
4984           (setq ninfo (cons 1 (1- (car active))))
4985         (setq ninfo (gnus-info-read info)))
4986       ;; Then we add the read articles to the range.
4987       (gnus-add-to-range
4988        ninfo (setq articles (sort articles '<))))))
4989
4990 (defun gnus-group-make-articles-read (group articles)
4991   "Update the info of GROUP to say that ARTICLES are read."
4992   (let* ((num 0)
4993          (entry (gnus-gethash group gnus-newsrc-hashtb))
4994          (info (nth 2 entry))
4995          (active (gnus-active group))
4996          range)
4997     (when entry
4998       (setq range (gnus-compute-read-articles group articles))
4999       (save-excursion
5000         (set-buffer gnus-group-buffer)
5001         (gnus-undo-register
5002           `(progn
5003              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5004              (gnus-info-set-read ',info ',(gnus-info-read info))
5005              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5006              (gnus-group-update-group ,group t))))
5007       ;; Add the read articles to the range.
5008       (gnus-info-set-read info range)
5009       ;; Then we have to re-compute how many unread
5010       ;; articles there are in this group.
5011       (when active
5012         (cond
5013          ((not range)
5014           (setq num (- (1+ (cdr active)) (car active))))
5015          ((not (listp (cdr range)))
5016           (setq num (- (cdr active) (- (1+ (cdr range))
5017                                        (car range)))))
5018          (t
5019           (while range
5020             (if (numberp (car range))
5021                 (setq num (1+ num))
5022               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5023             (setq range (cdr range)))
5024           (setq num (- (cdr active) num))))
5025         ;; Update the number of unread articles.
5026         (setcar entry num)
5027         ;; Update the group buffer.
5028         (gnus-group-update-group group t)))))
5029
5030 (defvar gnus-newsgroup-none-id 0)
5031
5032 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5033   (let ((cur nntp-server-buffer)
5034         (dependencies
5035          (or dependencies
5036              (save-excursion (set-buffer gnus-summary-buffer)
5037                              gnus-newsgroup-dependencies)))
5038         headers id end ref
5039         (mail-parse-charset gnus-newsgroup-charset)
5040         (mail-parse-ignored-charsets
5041          (save-excursion (condition-case nil
5042                              (set-buffer gnus-summary-buffer)
5043                            (error))
5044                          gnus-newsgroup-ignored-charsets)))
5045     (save-excursion
5046       (set-buffer nntp-server-buffer)
5047       ;; Translate all TAB characters into SPACE characters.
5048       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5049       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5050       (gnus-run-hooks 'gnus-parse-headers-hook)
5051       (let ((case-fold-search t)
5052             in-reply-to header p lines chars)
5053         (goto-char (point-min))
5054         ;; Search to the beginning of the next header.  Error messages
5055         ;; do not begin with 2 or 3.
5056         (while (re-search-forward "^[23][0-9]+ " nil t)
5057           (setq id nil
5058                 ref nil)
5059           ;; This implementation of this function, with nine
5060           ;; search-forwards instead of the one re-search-forward and
5061           ;; a case (which basically was the old function) is actually
5062           ;; about twice as fast, even though it looks messier.  You
5063           ;; can't have everything, I guess.  Speed and elegance
5064           ;; doesn't always go hand in hand.
5065           (setq
5066            header
5067            (vector
5068             ;; Number.
5069             (prog1
5070                 (read cur)
5071               (end-of-line)
5072               (setq p (point))
5073               (narrow-to-region (point)
5074                                 (or (and (search-forward "\n.\n" nil t)
5075                                          (- (point) 2))
5076                                     (point))))
5077             ;; Subject.
5078             (progn
5079               (goto-char p)
5080               (if (search-forward "\nsubject:" nil t)
5081                   (funcall gnus-decode-encoded-word-function
5082                            (nnheader-header-value))
5083                 "(none)"))
5084             ;; From.
5085             (progn
5086               (goto-char p)
5087               (if (search-forward "\nfrom:" nil t)
5088                   (funcall gnus-decode-encoded-word-function
5089                            (nnheader-header-value))
5090                 "(nobody)"))
5091             ;; Date.
5092             (progn
5093               (goto-char p)
5094               (if (search-forward "\ndate:" nil t)
5095                   (nnheader-header-value) ""))
5096             ;; Message-ID.
5097             (progn
5098               (goto-char p)
5099               (setq id (if (re-search-forward
5100                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5101                       ;; We do it this way to make sure the Message-ID
5102                            ;; is (somewhat) syntactically valid.
5103                            (buffer-substring (match-beginning 1)
5104                                              (match-end 1))
5105                        ;; If there was no message-id, we just fake one
5106                          ;; to make subsequent routines simpler.
5107                          (nnheader-generate-fake-message-id))))
5108             ;; References.
5109             (progn
5110               (goto-char p)
5111               (if (search-forward "\nreferences:" nil t)
5112                   (progn
5113                     (setq end (point))
5114                     (prog1
5115                         (nnheader-header-value)
5116                       (setq ref
5117                             (buffer-substring
5118                              (progn
5119                                (end-of-line)
5120                                (search-backward ">" end t)
5121                                (1+ (point)))
5122                              (progn
5123                                (search-backward "<" end t)
5124                                (point))))))
5125             ;; Get the references from the in-reply-to header if there
5126                 ;; were no references and the in-reply-to header looks
5127                 ;; promising.
5128                 (if (and (search-forward "\nin-reply-to:" nil t)
5129                          (setq in-reply-to (nnheader-header-value))
5130                          (string-match "<[^>]+>" in-reply-to))
5131                     (let (ref2)
5132                       (setq ref (substring in-reply-to (match-beginning 0)
5133                                            (match-end 0)))
5134                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5135                         (setq ref2 (substring in-reply-to (match-beginning 0)
5136                                               (match-end 0)))
5137                         (when (> (length ref2) (length ref))
5138                           (setq ref ref2)))
5139                       ref)
5140                   (setq ref nil))))
5141             ;; Chars.
5142             (progn
5143               (goto-char p)
5144               (if (search-forward "\nchars: " nil t)
5145                   (if (numberp (setq chars (ignore-errors (read cur))))
5146                       chars -1)
5147                 -1))
5148             ;; Lines.
5149             (progn
5150               (goto-char p)
5151               (if (search-forward "\nlines: " nil t)
5152                   (if (numberp (setq lines (ignore-errors (read cur))))
5153                       lines -1)
5154                 -1))
5155             ;; Xref.
5156             (progn
5157               (goto-char p)
5158               (and (search-forward "\nxref:" nil t)
5159                    (nnheader-header-value)))
5160             ;; Extra.
5161             (when gnus-extra-headers
5162               (let ((extra gnus-extra-headers)
5163                     out)
5164                 (while extra
5165                   (goto-char p)
5166                   (when (search-forward
5167                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5168                     (push (cons (car extra) (nnheader-header-value))
5169                           out))
5170                   (pop extra))
5171                 out))))
5172           (when (equal id ref)
5173             (setq ref nil))
5174
5175           (when gnus-alter-header-function
5176             (funcall gnus-alter-header-function header)
5177             (setq id (mail-header-id header)
5178                   ref (gnus-parent-id (mail-header-references header))))
5179
5180           (when (setq header
5181                       (gnus-dependencies-add-header
5182                        header dependencies force-new))
5183             (push header headers))
5184           (goto-char (point-max))
5185           (widen))
5186         (nreverse headers)))))
5187
5188 ;; Goes through the xover lines and returns a list of vectors
5189 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5190                                                   force-new dependencies
5191                                                   group also-fetch-heads)
5192   "Parse the news overview data in the server buffer.
5193 Return a list of headers that match SEQUENCE (see
5194 `nntp-retrieve-headers')."
5195   ;; Get the Xref when the users reads the articles since most/some
5196   ;; NNTP servers do not include Xrefs when using XOVER.
5197   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5198   (let ((mail-parse-charset gnus-newsgroup-charset)
5199         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5200         (cur nntp-server-buffer)
5201         (dependencies (or dependencies gnus-newsgroup-dependencies))
5202         (allp (cond
5203                ((eq gnus-read-all-available-headers t)
5204                 t)
5205                ((stringp gnus-read-all-available-headers)
5206                 (string-match gnus-read-all-available-headers group))
5207                (t
5208                 nil)))
5209         number headers header)
5210     (save-excursion
5211       (set-buffer nntp-server-buffer)
5212       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5213       ;; Allow the user to mangle the headers before parsing them.
5214       (gnus-run-hooks 'gnus-parse-headers-hook)
5215       (goto-char (point-min))
5216       (while (not (eobp))
5217         (condition-case ()
5218             (while (and (or sequence allp)
5219                         (not (eobp)))
5220               (setq number (read cur))
5221               (when (not allp)
5222                 (while (and sequence
5223                             (< (car sequence) number))
5224                   (setq sequence (cdr sequence))))
5225               (when (and (or allp
5226                              (and sequence
5227                                   (eq number (car sequence))))
5228                          (progn
5229                            (setq sequence (cdr sequence))
5230                            (setq header (inline
5231                                           (gnus-nov-parse-line
5232                                            number dependencies force-new)))))
5233                 (push header headers))
5234               (forward-line 1))
5235           (error
5236            (gnus-error 4 "Strange nov line (%d)"
5237                        (count-lines (point-min) (point)))))
5238         (forward-line 1))
5239       ;; A common bug in inn is that if you have posted an article and
5240       ;; then retrieves the active file, it will answer correctly --
5241       ;; the new article is included.  However, a NOV entry for the
5242       ;; article may not have been generated yet, so this may fail.
5243       ;; We work around this problem by retrieving the last few
5244       ;; headers using HEAD.
5245       (if (or (not also-fetch-heads)
5246               (not sequence))
5247           ;; We (probably) got all the headers.
5248           (nreverse headers)
5249         (let ((gnus-nov-is-evil t))
5250           (nconc
5251            (nreverse headers)
5252            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5253              (gnus-get-newsgroup-headers))))))))
5254
5255 (defun gnus-article-get-xrefs ()
5256   "Fill in the Xref value in `gnus-current-headers', if necessary.
5257 This is meant to be called in `gnus-article-internal-prepare-hook'."
5258   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5259                                  gnus-current-headers)))
5260     (or (not gnus-use-cross-reference)
5261         (not headers)
5262         (and (mail-header-xref headers)
5263              (not (string= (mail-header-xref headers) "")))
5264         (let ((case-fold-search t)
5265               xref)
5266           (save-restriction
5267             (nnheader-narrow-to-headers)
5268             (goto-char (point-min))
5269             (when (or (and (not (eobp))
5270                            (eq (downcase (char-after)) ?x)
5271                            (looking-at "Xref:"))
5272                       (search-forward "\nXref:" nil t))
5273               (goto-char (1+ (match-end 0)))
5274               (setq xref (buffer-substring (point)
5275                                            (progn (end-of-line) (point))))
5276               (mail-header-set-xref headers xref)))))))
5277
5278 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5279   "Find article ID and insert the summary line for that article.
5280 OLD-HEADER can either be a header or a line number to insert
5281 the subject line on."
5282   (let* ((line (and (numberp old-header) old-header))
5283          (old-header (and (vectorp old-header) old-header))
5284          (header (cond ((and old-header use-old-header)
5285                         old-header)
5286                        ((and (numberp id)
5287                              (gnus-number-to-header id))
5288                         (gnus-number-to-header id))
5289                        (t
5290                         (gnus-read-header id))))
5291          (number (and (numberp id) id))
5292          d)
5293     (when header
5294       ;; Rebuild the thread that this article is part of and go to the
5295       ;; article we have fetched.
5296       (when (and (not gnus-show-threads)
5297                  old-header)
5298         (when (and number
5299                    (setq d (gnus-data-find (mail-header-number old-header))))
5300           (goto-char (gnus-data-pos d))
5301           (gnus-data-remove
5302            number
5303            (- (gnus-point-at-bol)
5304               (prog1
5305                   (1+ (gnus-point-at-eol))
5306                 (gnus-delete-line))))))
5307       (when old-header
5308         (mail-header-set-number header (mail-header-number old-header)))
5309       (setq gnus-newsgroup-sparse
5310             (delq (setq number (mail-header-number header))
5311                   gnus-newsgroup-sparse))
5312       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5313       (push number gnus-newsgroup-limit)
5314       (gnus-rebuild-thread (mail-header-id header) line)
5315       (gnus-summary-goto-subject number nil t))
5316     (when (and (numberp number)
5317                (> number 0))
5318       ;; We have to update the boundaries even if we can't fetch the
5319       ;; article if ID is a number -- so that the next `P' or `N'
5320       ;; command will fetch the previous (or next) article even
5321       ;; if the one we tried to fetch this time has been canceled.
5322       (when (> number gnus-newsgroup-end)
5323         (setq gnus-newsgroup-end number))
5324       (when (< number gnus-newsgroup-begin)
5325         (setq gnus-newsgroup-begin number))
5326       (setq gnus-newsgroup-unselected
5327             (delq number gnus-newsgroup-unselected)))
5328     ;; Report back a success?
5329     (and header (mail-header-number header))))
5330
5331 ;;; Process/prefix in the summary buffer
5332
5333 (defun gnus-summary-work-articles (n)
5334   "Return a list of articles to be worked upon.
5335 The prefix argument, the list of process marked articles, and the
5336 current article will be taken into consideration."
5337   (save-excursion
5338     (set-buffer gnus-summary-buffer)
5339     (cond
5340      (n
5341       ;; A numerical prefix has been given.
5342       (setq n (prefix-numeric-value n))
5343       (let ((backward (< n 0))
5344             (n (abs (prefix-numeric-value n)))
5345             articles article)
5346         (save-excursion
5347           (while
5348               (and (> n 0)
5349                    (push (setq article (gnus-summary-article-number))
5350                          articles)
5351                    (if backward
5352                        (gnus-summary-find-prev nil article)
5353                      (gnus-summary-find-next nil article)))
5354             (decf n)))
5355         (nreverse articles)))
5356      ((and (gnus-region-active-p) (mark))
5357       (message "region active")
5358       ;; Work on the region between point and mark.
5359       (let ((max (max (point) (mark)))
5360             articles article)
5361         (save-excursion
5362           (goto-char (min (min (point) (mark))))
5363           (while
5364               (and
5365                (push (setq article (gnus-summary-article-number)) articles)
5366                (gnus-summary-find-next nil article)
5367                (< (point) max)))
5368           (nreverse articles))))
5369      (gnus-newsgroup-processable
5370       ;; There are process-marked articles present.
5371       ;; Save current state.
5372       (gnus-summary-save-process-mark)
5373       ;; Return the list.
5374       (reverse gnus-newsgroup-processable))
5375      (t
5376       ;; Just return the current article.
5377       (list (gnus-summary-article-number))))))
5378
5379 (defmacro gnus-summary-iterate (arg &rest forms)
5380   "Iterate over the process/prefixed articles and do FORMS.
5381 ARG is the interactive prefix given to the command.  FORMS will be
5382 executed with point over the summary line of the articles."
5383   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5384     `(let ((,articles (gnus-summary-work-articles ,arg)))
5385        (while ,articles
5386          (gnus-summary-goto-subject (car ,articles))
5387          ,@forms
5388          (pop ,articles)))))
5389
5390 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5391 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5392
5393 (defun gnus-summary-save-process-mark ()
5394   "Push the current set of process marked articles on the stack."
5395   (interactive)
5396   (push (copy-sequence gnus-newsgroup-processable)
5397         gnus-newsgroup-process-stack))
5398
5399 (defun gnus-summary-kill-process-mark ()
5400   "Push the current set of process marked articles on the stack and unmark."
5401   (interactive)
5402   (gnus-summary-save-process-mark)
5403   (gnus-summary-unmark-all-processable))
5404
5405 (defun gnus-summary-yank-process-mark ()
5406   "Pop the last process mark state off the stack and restore it."
5407   (interactive)
5408   (unless gnus-newsgroup-process-stack
5409     (error "Empty mark stack"))
5410   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5411
5412 (defun gnus-summary-process-mark-set (set)
5413   "Make SET into the current process marked articles."
5414   (gnus-summary-unmark-all-processable)
5415   (while set
5416     (gnus-summary-set-process-mark (pop set))))
5417
5418 ;;; Searching and stuff
5419
5420 (defun gnus-summary-search-group (&optional backward use-level)
5421   "Search for next unread newsgroup.
5422 If optional argument BACKWARD is non-nil, search backward instead."
5423   (save-excursion
5424     (set-buffer gnus-group-buffer)
5425     (when (gnus-group-search-forward
5426            backward nil (if use-level (gnus-group-group-level) nil))
5427       (gnus-group-group-name))))
5428
5429 (defun gnus-summary-best-group (&optional exclude-group)
5430   "Find the name of the best unread group.
5431 If EXCLUDE-GROUP, do not go to this group."
5432   (save-excursion
5433     (set-buffer gnus-group-buffer)
5434     (save-excursion
5435       (gnus-group-best-unread-group exclude-group))))
5436
5437 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5438   (if backward (gnus-summary-find-prev)
5439     (let* ((dummy (gnus-summary-article-intangible-p))
5440            (article (or article (gnus-summary-article-number)))
5441            (arts (gnus-data-find-list article))
5442            result)
5443       (when (and (not dummy)
5444                  (or (not gnus-summary-check-current)
5445                      (not unread)
5446                      (not (gnus-data-unread-p (car arts)))))
5447         (setq arts (cdr arts)))
5448       (when (setq result
5449                   (if unread
5450                       (progn
5451                         (while arts
5452                           (when (or (and undownloaded
5453                                          (eq gnus-undownloaded-mark
5454                                              (gnus-data-mark (car arts))))
5455                                     (gnus-data-unread-p (car arts)))
5456                             (setq result (car arts)
5457                                   arts nil))
5458                           (setq arts (cdr arts)))
5459                         result)
5460                     (car arts)))
5461         (goto-char (gnus-data-pos result))
5462         (gnus-data-number result)))))
5463
5464 (defun gnus-summary-find-prev (&optional unread article)
5465   (let* ((eobp (eobp))
5466          (article (or article (gnus-summary-article-number)))
5467          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5468          result)
5469     (when (and (not eobp)
5470                (or (not gnus-summary-check-current)
5471                    (not unread)
5472                    (not (gnus-data-unread-p (car arts)))))
5473       (setq arts (cdr arts)))
5474     (when (setq result
5475                 (if unread
5476                     (progn
5477                       (while arts
5478                         (when (gnus-data-unread-p (car arts))
5479                           (setq result (car arts)
5480                                 arts nil))
5481                         (setq arts (cdr arts)))
5482                       result)
5483                   (car arts)))
5484       (goto-char (gnus-data-pos result))
5485       (gnus-data-number result))))
5486
5487 (defun gnus-summary-find-subject (subject &optional unread backward article)
5488   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5489          (article (or article (gnus-summary-article-number)))
5490          (articles (gnus-data-list backward))
5491          (arts (gnus-data-find-list article articles))
5492          result)
5493     (when (or (not gnus-summary-check-current)
5494               (not unread)
5495               (not (gnus-data-unread-p (car arts))))
5496       (setq arts (cdr arts)))
5497     (while arts
5498       (and (or (not unread)
5499                (gnus-data-unread-p (car arts)))
5500            (vectorp (gnus-data-header (car arts)))
5501            (gnus-subject-equal
5502             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5503            (setq result (car arts)
5504                  arts nil))
5505       (setq arts (cdr arts)))
5506     (and result
5507          (goto-char (gnus-data-pos result))
5508          (gnus-data-number result))))
5509
5510 (defun gnus-summary-search-forward (&optional unread subject backward)
5511   "Search forward for an article.
5512 If UNREAD, look for unread articles.  If SUBJECT, look for
5513 articles with that subject.  If BACKWARD, search backward instead."
5514   (cond (subject (gnus-summary-find-subject subject unread backward))
5515         (backward (gnus-summary-find-prev unread))
5516         (t (gnus-summary-find-next unread))))
5517
5518 (defun gnus-recenter (&optional n)
5519   "Center point in window and redisplay frame.
5520 Also do horizontal recentering."
5521   (interactive "P")
5522   (when (and gnus-auto-center-summary
5523              (not (eq gnus-auto-center-summary 'vertical)))
5524     (gnus-horizontal-recenter))
5525   (recenter n))
5526
5527 (defun gnus-summary-recenter ()
5528   "Center point in the summary window.
5529 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5530 displayed, no centering will be performed."
5531   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5532 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5533   (interactive)
5534   (let* ((top (cond ((< (window-height) 4) 0)
5535                     ((< (window-height) 7) 1)
5536                     (t (if (numberp gnus-auto-center-summary)
5537                            gnus-auto-center-summary
5538                          2))))
5539          (height (1- (window-height)))
5540          (bottom (save-excursion (goto-char (point-max))
5541                                  (forward-line (- height))
5542                                  (point)))
5543          (window (get-buffer-window (current-buffer))))
5544     ;; The user has to want it.
5545     (when gnus-auto-center-summary
5546       (when (get-buffer-window gnus-article-buffer)
5547         ;; Only do recentering when the article buffer is displayed,
5548       ;; Set the window start to either `bottom', which is the biggest
5549         ;; possible valid number, or the second line from the top,
5550         ;; whichever is the least.
5551         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5552           (if (> bottom top-pos)
5553               ;; Keep the second line from the top visible
5554               (set-window-start window top-pos t)
5555             ;; Try to keep the bottom line visible; if it's partially
5556             ;; obscured, either scroll one more line to make it fully
5557             ;; visible, or revert to using TOP-POS.
5558             (save-excursion
5559               (goto-char (point-max))
5560               (forward-line -1)
5561               (let ((last-line-start (point)))
5562                 (goto-char bottom)
5563                 (set-window-start window (point) t)
5564                 (when (not (pos-visible-in-window-p last-line-start window))
5565                   (forward-line 1)
5566                   (set-window-start window (min (point) top-pos) t)))))))
5567       ;; Do horizontal recentering while we're at it.
5568       (when (and (get-buffer-window (current-buffer) t)
5569                  (not (eq gnus-auto-center-summary 'vertical)))
5570         (let ((selected (selected-window)))
5571           (select-window (get-buffer-window (current-buffer) t))
5572           (gnus-summary-position-point)
5573           (gnus-horizontal-recenter)
5574           (select-window selected))))))
5575
5576 (defun gnus-summary-jump-to-group (newsgroup)
5577   "Move point to NEWSGROUP in group mode buffer."
5578   ;; Keep update point of group mode buffer if visible.
5579   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5580       (save-window-excursion
5581         ;; Take care of tree window mode.
5582         (when (get-buffer-window gnus-group-buffer)
5583           (pop-to-buffer gnus-group-buffer))
5584         (gnus-group-jump-to-group newsgroup))
5585     (save-excursion
5586       ;; Take care of tree window mode.
5587       (if (get-buffer-window gnus-group-buffer)
5588           (pop-to-buffer gnus-group-buffer)
5589         (set-buffer gnus-group-buffer))
5590       (gnus-group-jump-to-group newsgroup))))
5591
5592 ;; This function returns a list of article numbers based on the
5593 ;; difference between the ranges of read articles in this group and
5594 ;; the range of active articles.
5595 (defun gnus-list-of-unread-articles (group)
5596   (let* ((read (gnus-info-read (gnus-get-info group)))
5597          (active (or (gnus-active group) (gnus-activate-group group)))
5598          (last (cdr active))
5599          first nlast unread)
5600     ;; If none are read, then all are unread.
5601     (if (not read)
5602         (setq first (car active))
5603       ;; If the range of read articles is a single range, then the
5604       ;; first unread article is the article after the last read
5605       ;; article.  Sounds logical, doesn't it?
5606       (if (and (not (listp (cdr read)))
5607                (or (< (car read) (car active))
5608                    (progn (setq read (list read))
5609                           nil)))
5610           (setq first (max (car active) (1+ (cdr read))))
5611         ;; `read' is a list of ranges.
5612         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5613                                   (caar read)))
5614                   1)
5615           (setq first (car active)))
5616         (while read
5617           (when first
5618             (while (< first nlast)
5619               (push first unread)
5620               (setq first (1+ first))))
5621           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5622           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5623           (setq read (cdr read)))))
5624     ;; And add the last unread articles.
5625     (while (<= first last)
5626       (push first unread)
5627       (setq first (1+ first)))
5628     ;; Return the list of unread articles.
5629     (delq 0 (nreverse unread))))
5630
5631 (defun gnus-list-of-read-articles (group)
5632   "Return a list of unread, unticked and non-dormant articles."
5633   (let* ((info (gnus-get-info group))
5634          (marked (gnus-info-marks info))
5635          (active (gnus-active group)))
5636     (and info active
5637          (gnus-set-difference
5638           (gnus-sorted-complement
5639            (gnus-uncompress-range active)
5640            (gnus-list-of-unread-articles group))
5641           (append
5642            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5643            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5644
5645 ;; Various summary commands
5646
5647 (defun gnus-summary-select-article-buffer ()
5648   "Reconfigure windows to show article buffer."
5649   (interactive)
5650   (if (not (gnus-buffer-live-p gnus-article-buffer))
5651       (error "There is no article buffer for this summary buffer")
5652     (gnus-configure-windows 'article)
5653     (select-window (get-buffer-window gnus-article-buffer))))
5654
5655 (defun gnus-summary-universal-argument (arg)
5656   "Perform any operation on all articles that are process/prefixed."
5657   (interactive "P")
5658   (let ((articles (gnus-summary-work-articles arg))
5659         func article)
5660     (if (eq
5661          (setq
5662           func
5663           (key-binding
5664            (read-key-sequence
5665             (substitute-command-keys
5666              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5667          'undefined)
5668         (gnus-error 1 "Undefined key")
5669       (save-excursion
5670         (while articles
5671           (gnus-summary-goto-subject (setq article (pop articles)))
5672           (let (gnus-newsgroup-processable)
5673             (command-execute func))
5674           (gnus-summary-remove-process-mark article)))))
5675   (gnus-summary-position-point))
5676
5677 (defun gnus-summary-toggle-truncation (&optional arg)
5678   "Toggle truncation of summary lines.
5679 With arg, turn line truncation on iff arg is positive."
5680   (interactive "P")
5681   (setq truncate-lines
5682         (if (null arg) (not truncate-lines)
5683           (> (prefix-numeric-value arg) 0)))
5684   (redraw-display))
5685
5686 (defun gnus-summary-reselect-current-group (&optional all rescan)
5687   "Exit and then reselect the current newsgroup.
5688 The prefix argument ALL means to select all articles."
5689   (interactive "P")
5690   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5691     (error "Ephemeral groups can't be reselected"))
5692   (let ((current-subject (gnus-summary-article-number))
5693         (group gnus-newsgroup-name))
5694     (setq gnus-newsgroup-begin nil)
5695     (gnus-summary-exit)
5696     ;; We have to adjust the point of group mode buffer because
5697     ;; point was moved to the next unread newsgroup by exiting.
5698     (gnus-summary-jump-to-group group)
5699     (when rescan
5700       (save-excursion
5701         (gnus-group-get-new-news-this-group 1)))
5702     (gnus-group-read-group all t)
5703     (gnus-summary-goto-subject current-subject nil t)))
5704
5705 (defun gnus-summary-rescan-group (&optional all)
5706   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5707   (interactive "P")
5708   (gnus-summary-reselect-current-group all t))
5709
5710 (defun gnus-summary-update-info (&optional non-destructive)
5711   (save-excursion
5712     (let ((group gnus-newsgroup-name))
5713       (when group
5714         (when gnus-newsgroup-kill-headers
5715           (setq gnus-newsgroup-killed
5716                 (gnus-compress-sequence
5717                  (nconc
5718                   (gnus-set-sorted-intersection
5719                    (gnus-uncompress-range gnus-newsgroup-killed)
5720                    (setq gnus-newsgroup-unselected
5721                          (sort gnus-newsgroup-unselected '<)))
5722                   (setq gnus-newsgroup-unreads
5723                         (sort gnus-newsgroup-unreads '<)))
5724                  t)))
5725         (unless (listp (cdr gnus-newsgroup-killed))
5726           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5727         (let ((headers gnus-newsgroup-headers))
5728           ;; Set the new ranges of read articles.
5729           (save-excursion
5730             (set-buffer gnus-group-buffer)
5731             (gnus-undo-force-boundary))
5732           (gnus-update-read-articles
5733            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5734           ;; Set the current article marks.
5735           (let ((gnus-newsgroup-scored
5736                  (if (and (not gnus-save-score)
5737                           (not non-destructive))
5738                      nil
5739                    gnus-newsgroup-scored)))
5740             (save-excursion
5741               (gnus-update-marks)))
5742           ;; Do the cross-ref thing.
5743           (when gnus-use-cross-reference
5744             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5745           ;; Do not switch windows but change the buffer to work.
5746           (set-buffer gnus-group-buffer)
5747           (unless (gnus-ephemeral-group-p group)
5748             (gnus-group-update-group group)))))))
5749
5750 (defun gnus-summary-save-newsrc (&optional force)
5751   "Save the current number of read/marked articles in the dribble buffer.
5752 The dribble buffer will then be saved.
5753 If FORCE (the prefix), also save the .newsrc file(s)."
5754   (interactive "P")
5755   (gnus-summary-update-info t)
5756   (if force
5757       (gnus-save-newsrc-file)
5758     (gnus-dribble-save)))
5759
5760 (defun gnus-summary-exit (&optional temporary)
5761   "Exit reading current newsgroup, and then return to group selection mode.
5762 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5763   (interactive)
5764   (gnus-set-global-variables)
5765   (when (gnus-buffer-live-p gnus-article-buffer)
5766     (save-excursion
5767       (set-buffer gnus-article-buffer)
5768       (mm-destroy-parts gnus-article-mime-handles)
5769       ;; Set it to nil for safety reason.
5770       (setq gnus-article-mime-handle-alist nil)
5771       (setq gnus-article-mime-handles nil)))
5772   (gnus-kill-save-kill-buffer)
5773   (gnus-async-halt-prefetch)
5774   (let* ((group gnus-newsgroup-name)
5775          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5776          (mode major-mode)
5777          (group-point nil)
5778          (buf (current-buffer)))
5779     (unless quit-config
5780       ;; Do adaptive scoring, and possibly save score files.
5781       (when gnus-newsgroup-adaptive
5782         (gnus-score-adaptive))
5783       (when gnus-use-scoring
5784         (gnus-score-save)))
5785     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5786     ;; If we have several article buffers, we kill them at exit.
5787     (unless gnus-single-article-buffer
5788       (gnus-kill-buffer gnus-original-article-buffer)
5789       (setq gnus-article-current nil))
5790     (when gnus-use-cache
5791       (gnus-cache-possibly-remove-articles)
5792       (gnus-cache-save-buffers))
5793     (gnus-async-prefetch-remove-group group)
5794     (when gnus-suppress-duplicates
5795       (gnus-dup-enter-articles))
5796     (when gnus-use-trees
5797       (gnus-tree-close group))
5798     (when gnus-use-cache
5799       (gnus-cache-write-active))
5800     ;; Remove entries for this group.
5801     (nnmail-purge-split-history (gnus-group-real-name group))
5802     ;; Make all changes in this group permanent.
5803     (unless quit-config
5804       (gnus-run-hooks 'gnus-exit-group-hook)
5805       (gnus-summary-update-info))
5806     (gnus-close-group group)
5807     ;; Make sure where we were, and go to next newsgroup.
5808     (set-buffer gnus-group-buffer)
5809     (unless quit-config
5810       (gnus-group-jump-to-group group))
5811     (gnus-run-hooks 'gnus-summary-exit-hook)
5812     (unless (or quit-config
5813                 ;; If this group has disappeared from the summary
5814                 ;; buffer, don't skip forwards.
5815                 (not (string= group (gnus-group-group-name))))
5816       (gnus-group-next-unread-group 1))
5817     (setq group-point (point))
5818     (if temporary
5819         nil                             ;Nothing to do.
5820       ;; If we have several article buffers, we kill them at exit.
5821       (unless gnus-single-article-buffer
5822         (gnus-kill-buffer gnus-article-buffer)
5823         (gnus-kill-buffer gnus-original-article-buffer)
5824         (setq gnus-article-current nil))
5825       (set-buffer buf)
5826       (if (not gnus-kill-summary-on-exit)
5827           (progn
5828             (gnus-deaden-summary)
5829             (setq mode nil))
5830        ;; We set all buffer-local variables to nil.  It is unclear why
5831         ;; this is needed, but if we don't, buffer-local variables are
5832         ;; not garbage-collected, it seems.  This would the lead to en
5833         ;; ever-growing Emacs.
5834         (gnus-summary-clear-local-variables)
5835         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5836           (gnus-summary-clear-local-variables))
5837         (when (get-buffer gnus-article-buffer)
5838           (bury-buffer gnus-article-buffer))
5839         ;; We clear the global counterparts of the buffer-local
5840         ;; variables as well, just to be on the safe side.
5841         (set-buffer gnus-group-buffer)
5842         (gnus-summary-clear-local-variables)
5843         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5844           (gnus-summary-clear-local-variables)))
5845       (setq gnus-current-select-method gnus-select-method)
5846       (pop-to-buffer gnus-group-buffer)
5847       (if (not quit-config)
5848           (progn
5849             (goto-char group-point)
5850             (gnus-configure-windows 'group 'force))
5851         (gnus-handle-ephemeral-exit quit-config))
5852       ;; Return to group mode buffer.
5853       (when (eq mode 'gnus-summary-mode)
5854         (gnus-kill-buffer buf))
5855       ;; Clear the current group name.
5856       (unless quit-config
5857         (setq gnus-newsgroup-name nil)))))
5858
5859 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5860 (defun gnus-summary-exit-no-update (&optional no-questions)
5861   "Quit reading current newsgroup without updating read article info."
5862   (interactive)
5863   (let* ((group gnus-newsgroup-name)
5864          (quit-config (gnus-group-quit-config group)))
5865     (when (or no-questions
5866               gnus-expert-user
5867               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5868       (gnus-async-halt-prefetch)
5869       (mapcar 'funcall
5870               (delq 'gnus-summary-expire-articles
5871                     (copy-sequence gnus-summary-prepare-exit-hook)))
5872       (when (gnus-buffer-live-p gnus-article-buffer)
5873         (save-excursion
5874           (set-buffer gnus-article-buffer)
5875           (mm-destroy-parts gnus-article-mime-handles)
5876           ;; Set it to nil for safety reason.
5877           (setq gnus-article-mime-handle-alist nil)
5878           (setq gnus-article-mime-handles nil)))
5879       ;; If we have several article buffers, we kill them at exit.
5880       (unless gnus-single-article-buffer
5881         (gnus-kill-buffer gnus-article-buffer)
5882         (gnus-kill-buffer gnus-original-article-buffer)
5883         (setq gnus-article-current nil))
5884       (if (not gnus-kill-summary-on-exit)
5885           (gnus-deaden-summary)
5886         (gnus-close-group group)
5887         (gnus-summary-clear-local-variables)
5888         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5889           (gnus-summary-clear-local-variables))
5890         (set-buffer gnus-group-buffer)
5891         (gnus-summary-clear-local-variables)
5892         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5893           (gnus-summary-clear-local-variables))
5894         (when (get-buffer gnus-summary-buffer)
5895           (kill-buffer gnus-summary-buffer)))
5896       (unless gnus-single-article-buffer
5897         (setq gnus-article-current nil))
5898       (when gnus-use-trees
5899         (gnus-tree-close group))
5900       (gnus-async-prefetch-remove-group group)
5901       (when (get-buffer gnus-article-buffer)
5902         (bury-buffer gnus-article-buffer))
5903       ;; Return to the group buffer.
5904       (gnus-configure-windows 'group 'force)
5905       ;; Clear the current group name.
5906       (setq gnus-newsgroup-name nil)
5907       (when (equal (gnus-group-group-name) group)
5908         (gnus-group-next-unread-group 1))
5909       (when quit-config
5910         (gnus-handle-ephemeral-exit quit-config)))))
5911
5912 (defun gnus-handle-ephemeral-exit (quit-config)
5913   "Handle movement when leaving an ephemeral group.
5914 The state which existed when entering the ephemeral is reset."
5915   (if (not (buffer-name (car quit-config)))
5916       (gnus-configure-windows 'group 'force)
5917     (set-buffer (car quit-config))
5918     (cond ((eq major-mode 'gnus-summary-mode)
5919            (gnus-set-global-variables))
5920           ((eq major-mode 'gnus-article-mode)
5921            (save-excursion
5922              ;; The `gnus-summary-buffer' variable may point
5923              ;; to the old summary buffer when using a single
5924              ;; article buffer.
5925              (unless (gnus-buffer-live-p gnus-summary-buffer)
5926                (set-buffer gnus-group-buffer))
5927              (set-buffer gnus-summary-buffer)
5928              (gnus-set-global-variables))))
5929     (if (or (eq (cdr quit-config) 'article)
5930             (eq (cdr quit-config) 'pick))
5931         (progn
5932           ;; The current article may be from the ephemeral group
5933           ;; thus it is best that we reload this article
5934           (gnus-summary-show-article)
5935           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5936               (gnus-configure-windows 'pick 'force)
5937             (gnus-configure-windows (cdr quit-config) 'force)))
5938       (gnus-configure-windows (cdr quit-config) 'force))
5939     (when (eq major-mode 'gnus-summary-mode)
5940       (gnus-summary-next-subject 1 nil t)
5941       (gnus-summary-recenter)
5942       (gnus-summary-position-point))))
5943
5944 ;;; Dead summaries.
5945
5946 (defvar gnus-dead-summary-mode-map nil)
5947
5948 (unless gnus-dead-summary-mode-map
5949   (setq gnus-dead-summary-mode-map (make-keymap))
5950   (suppress-keymap gnus-dead-summary-mode-map)
5951   (substitute-key-definition
5952    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5953   (let ((keys '("\C-d" "\r" "\177" [delete])))
5954     (while keys
5955       (define-key gnus-dead-summary-mode-map
5956         (pop keys) 'gnus-summary-wake-up-the-dead))))
5957
5958 (defvar gnus-dead-summary-mode nil
5959   "Minor mode for Gnus summary buffers.")
5960
5961 (defun gnus-dead-summary-mode (&optional arg)
5962   "Minor mode for Gnus summary buffers."
5963   (interactive "P")
5964   (when (eq major-mode 'gnus-summary-mode)
5965     (make-local-variable 'gnus-dead-summary-mode)
5966     (setq gnus-dead-summary-mode
5967           (if (null arg) (not gnus-dead-summary-mode)
5968             (> (prefix-numeric-value arg) 0)))
5969     (when gnus-dead-summary-mode
5970       (gnus-add-minor-mode
5971        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5972
5973 (defun gnus-deaden-summary ()
5974   "Make the current summary buffer into a dead summary buffer."
5975   ;; Kill any previous dead summary buffer.
5976   (when (and gnus-dead-summary
5977              (buffer-name gnus-dead-summary))
5978     (save-excursion
5979       (set-buffer gnus-dead-summary)
5980       (when gnus-dead-summary-mode
5981         (kill-buffer (current-buffer)))))
5982   ;; Make this the current dead summary.
5983   (setq gnus-dead-summary (current-buffer))
5984   (gnus-dead-summary-mode 1)
5985   (let ((name (buffer-name)))
5986     (when (string-match "Summary" name)
5987       (rename-buffer
5988        (concat (substring name 0 (match-beginning 0)) "Dead "
5989                (substring name (match-beginning 0)))
5990        t)
5991       (bury-buffer))))
5992
5993 (defun gnus-kill-or-deaden-summary (buffer)
5994   "Kill or deaden the summary BUFFER."
5995   (save-excursion
5996     (when (and (buffer-name buffer)
5997                (not gnus-single-article-buffer))
5998       (save-excursion
5999         (set-buffer buffer)
6000         (gnus-kill-buffer gnus-article-buffer)
6001         (gnus-kill-buffer gnus-original-article-buffer)))
6002     (cond (gnus-kill-summary-on-exit
6003            (when (and gnus-use-trees
6004                       (gnus-buffer-exists-p buffer))
6005              (save-excursion
6006                (set-buffer buffer)
6007                (gnus-tree-close gnus-newsgroup-name)))
6008            (gnus-kill-buffer buffer))
6009           ((gnus-buffer-exists-p buffer)
6010            (save-excursion
6011              (set-buffer buffer)
6012              (gnus-deaden-summary))))))
6013
6014 (defun gnus-summary-wake-up-the-dead (&rest args)
6015   "Wake up the dead summary buffer."
6016   (interactive)
6017   (gnus-dead-summary-mode -1)
6018   (let ((name (buffer-name)))
6019     (when (string-match "Dead " name)
6020       (rename-buffer
6021        (concat (substring name 0 (match-beginning 0))
6022                (substring name (match-end 0)))
6023        t)))
6024   (gnus-message 3 "This dead summary is now alive again"))
6025
6026 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6027 (defun gnus-summary-fetch-faq (&optional faq-dir)
6028   "Fetch the FAQ for the current group.
6029 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6030 in."
6031   (interactive
6032    (list
6033     (when current-prefix-arg
6034       (completing-read
6035        "Faq dir: " (and (listp gnus-group-faq-directory)
6036                         (mapcar (lambda (file) (list file))
6037                                 gnus-group-faq-directory))))))
6038   (let (gnus-faq-buffer)
6039     (when (setq gnus-faq-buffer
6040                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6041       (gnus-configure-windows 'summary-faq))))
6042
6043 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6044 (defun gnus-summary-describe-group (&optional force)
6045   "Describe the current newsgroup."
6046   (interactive "P")
6047   (gnus-group-describe-group force gnus-newsgroup-name))
6048
6049 (defun gnus-summary-describe-briefly ()
6050   "Describe summary mode commands briefly."
6051   (interactive)
6052   (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")))
6053
6054 ;; Walking around group mode buffer from summary mode.
6055
6056 (defun gnus-summary-next-group (&optional no-article target-group backward)
6057   "Exit current newsgroup and then select next unread newsgroup.
6058 If prefix argument NO-ARTICLE is non-nil, no article is selected
6059 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6060 previous group instead."
6061   (interactive "P")
6062   ;; Stop pre-fetching.
6063   (gnus-async-halt-prefetch)
6064   (let ((current-group gnus-newsgroup-name)
6065         (current-buffer (current-buffer))
6066         entered)
6067    ;; First we semi-exit this group to update Xrefs and all variables.
6068     ;; We can't do a real exit, because the window conf must remain
6069     ;; the same in case the user is prompted for info, and we don't
6070     ;; want the window conf to change before that...
6071     (gnus-summary-exit t)
6072     (while (not entered)
6073       ;; Then we find what group we are supposed to enter.
6074       (set-buffer gnus-group-buffer)
6075       (gnus-group-jump-to-group current-group)
6076       (setq target-group
6077             (or target-group
6078                 (if (eq gnus-keep-same-level 'best)
6079                     (gnus-summary-best-group gnus-newsgroup-name)
6080                   (gnus-summary-search-group backward gnus-keep-same-level))))
6081       (if (not target-group)
6082           ;; There are no further groups, so we return to the group
6083           ;; buffer.
6084           (progn
6085             (gnus-message 5 "Returning to the group buffer")
6086             (setq entered t)
6087             (when (gnus-buffer-live-p current-buffer)
6088               (set-buffer current-buffer)
6089               (gnus-summary-exit))
6090             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6091         ;; We try to enter the target group.
6092         (gnus-group-jump-to-group target-group)
6093         (let ((unreads (gnus-group-group-unread)))
6094           (if (and (or (eq t unreads)
6095                        (and unreads (not (zerop unreads))))
6096                    (gnus-summary-read-group
6097                     target-group nil no-article
6098                     (and (buffer-name current-buffer) current-buffer)
6099                     nil backward))
6100               (setq entered t)
6101             (setq current-group target-group
6102                   target-group nil)))))))
6103
6104 (defun gnus-summary-prev-group (&optional no-article)
6105   "Exit current newsgroup and then select previous unread newsgroup.
6106 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6107   (interactive "P")
6108   (gnus-summary-next-group no-article nil t))
6109
6110 ;; Walking around summary lines.
6111
6112 (defun gnus-summary-first-subject (&optional unread undownloaded)
6113   "Go to the first unread subject.
6114 If UNREAD is non-nil, go to the first unread article.
6115 Returns the article selected or nil if there are no unread articles."
6116   (interactive "P")
6117   (prog1
6118       (cond
6119        ;; Empty summary.
6120        ((null gnus-newsgroup-data)
6121         (gnus-message 3 "No articles in the group")
6122         nil)
6123        ;; Pick the first article.
6124        ((not unread)
6125         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6126         (gnus-data-number (car gnus-newsgroup-data)))
6127        ;; No unread articles.
6128        ((null gnus-newsgroup-unreads)
6129         (gnus-message 3 "No more unread articles")
6130         nil)
6131        ;; Find the first unread article.
6132        (t
6133         (let ((data gnus-newsgroup-data))
6134           (while (and data
6135                       (and (not (and undownloaded
6136                                      (eq gnus-undownloaded-mark
6137                                          (gnus-data-mark (car data)))))
6138                            (not (gnus-data-unread-p (car data)))))
6139             (setq data (cdr data)))
6140           (when data
6141             (goto-char (gnus-data-pos (car data)))
6142             (gnus-data-number (car data))))))
6143     (gnus-summary-position-point)))
6144
6145 (defun gnus-summary-next-subject (n &optional unread dont-display)
6146   "Go to next N'th summary line.
6147 If N is negative, go to the previous N'th subject line.
6148 If UNREAD is non-nil, only unread articles are selected.
6149 The difference between N and the actual number of steps taken is
6150 returned."
6151   (interactive "p")
6152   (let ((backward (< n 0))
6153         (n (abs n)))
6154     (while (and (> n 0)
6155                 (if backward
6156                     (gnus-summary-find-prev unread)
6157                   (gnus-summary-find-next unread)))
6158       (unless (zerop (setq n (1- n)))
6159         (gnus-summary-show-thread)))
6160     (when (/= 0 n)
6161       (gnus-message 7 "No more%s articles"
6162                     (if unread " unread" "")))
6163     (unless dont-display
6164       (gnus-summary-recenter)
6165       (gnus-summary-position-point))
6166     n))
6167
6168 (defun gnus-summary-next-unread-subject (n)
6169   "Go to next N'th unread summary line."
6170   (interactive "p")
6171   (gnus-summary-next-subject n t))
6172
6173 (defun gnus-summary-prev-subject (n &optional unread)
6174   "Go to previous N'th summary line.
6175 If optional argument UNREAD is non-nil, only unread article is selected."
6176   (interactive "p")
6177   (gnus-summary-next-subject (- n) unread))
6178
6179 (defun gnus-summary-prev-unread-subject (n)
6180   "Go to previous N'th unread summary line."
6181   (interactive "p")
6182   (gnus-summary-next-subject (- n) t))
6183
6184 (defun gnus-summary-goto-subject (article &optional force silent)
6185   "Go the subject line of ARTICLE.
6186 If FORCE, also allow jumping to articles not currently shown."
6187   (interactive "nArticle number: ")
6188   (let ((b (point))
6189         (data (gnus-data-find article)))
6190     ;; We read in the article if we have to.
6191     (and (not data)
6192          force
6193          (gnus-summary-insert-subject
6194           article
6195           (if (or (numberp force) (vectorp force)) force)
6196           t)
6197          (setq data (gnus-data-find article)))
6198     (goto-char b)
6199     (if (not data)
6200         (progn
6201           (unless silent
6202             (gnus-message 3 "Can't find article %d" article))
6203           nil)
6204       (let ((pt (gnus-data-pos data)))
6205         (goto-char pt)
6206         (gnus-summary-set-article-display-arrow pt))
6207       (gnus-summary-position-point)
6208       article)))
6209
6210 ;; Walking around summary lines with displaying articles.
6211
6212 (defun gnus-summary-expand-window (&optional arg)
6213   "Make the summary buffer take up the entire Emacs frame.
6214 Given a prefix, will force an `article' buffer configuration."
6215   (interactive "P")
6216   (if arg
6217       (gnus-configure-windows 'article 'force)
6218     (gnus-configure-windows 'summary 'force)))
6219
6220 (defun gnus-summary-display-article (article &optional all-header)
6221   "Display ARTICLE in article buffer."
6222   (when (gnus-buffer-live-p gnus-article-buffer)
6223     (with-current-buffer gnus-article-buffer
6224       (mm-enable-multibyte-mule4)))
6225   (gnus-set-global-variables)
6226   (when (gnus-buffer-live-p gnus-article-buffer)
6227     (with-current-buffer gnus-article-buffer
6228       (setq gnus-article-charset gnus-newsgroup-charset)
6229       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6230       (mm-enable-multibyte-mule4)))
6231   (if (null article)
6232       nil
6233     (prog1
6234         (if gnus-summary-display-article-function
6235             (funcall gnus-summary-display-article-function article all-header)
6236           (gnus-article-prepare article all-header))
6237       (gnus-run-hooks 'gnus-select-article-hook)
6238       (when (and gnus-current-article
6239                  (not (zerop gnus-current-article)))
6240         (gnus-summary-goto-subject gnus-current-article))
6241       (gnus-summary-recenter)
6242       (when (and gnus-use-trees gnus-show-threads)
6243         (gnus-possibly-generate-tree article)
6244         (gnus-highlight-selected-tree article))
6245       ;; Successfully display article.
6246       (gnus-article-set-window-start
6247        (cdr (assq article gnus-newsgroup-bookmarks))))))
6248
6249 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6250   "Select the current article.
6251 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6252 non-nil, the article will be re-fetched even if it already present in
6253 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6254 be displayed."
6255   ;; Make sure we are in the summary buffer to work around bbdb bug.
6256   (unless (eq major-mode 'gnus-summary-mode)
6257     (set-buffer gnus-summary-buffer))
6258   (let ((article (or article (gnus-summary-article-number)))
6259         (all-headers (not (not all-headers))) ;Must be T or NIL.
6260         gnus-summary-display-article-function)
6261     (and (not pseudo)
6262          (gnus-summary-article-pseudo-p article)
6263          (error "This is a pseudo-article"))
6264     (save-excursion
6265       (set-buffer gnus-summary-buffer)
6266       (if (or (and gnus-single-article-buffer
6267                    (or (null gnus-current-article)
6268                        (null gnus-article-current)
6269                        (null (get-buffer gnus-article-buffer))
6270                        (not (eq article (cdr gnus-article-current)))
6271                        (not (equal (car gnus-article-current)
6272                                    gnus-newsgroup-name))))
6273               (and (not gnus-single-article-buffer)
6274                    (or (null gnus-current-article)
6275                        (not (eq gnus-current-article article))))
6276               force)
6277        ;; The requested article is different from the current article.
6278           (progn
6279             (gnus-summary-display-article article all-headers)
6280             (when (gnus-buffer-live-p gnus-article-buffer)
6281               (with-current-buffer gnus-article-buffer
6282                 (if (not gnus-article-decoded-p) ;; a local variable
6283                     (mm-disable-multibyte-mule4))))
6284             (when (or all-headers gnus-show-all-headers)
6285               (gnus-article-show-all-headers))
6286             (gnus-article-set-window-start
6287              (cdr (assq article gnus-newsgroup-bookmarks)))
6288             article)
6289         (when (or all-headers gnus-show-all-headers)
6290           (gnus-article-show-all-headers))
6291         'old))))
6292
6293 (defun gnus-summary-force-verify-and-decrypt ()
6294   (interactive)
6295   (let ((mm-verify-option 'known)
6296         (mm-decrypt-option 'known))
6297     (gnus-summary-select-article nil 'force)))
6298
6299 (defun gnus-summary-set-current-mark (&optional current-mark)
6300   "Obsolete function."
6301   nil)
6302
6303 (defun gnus-summary-next-article (&optional unread subject backward push)
6304   "Select the next article.
6305 If UNREAD, only unread articles are selected.
6306 If SUBJECT, only articles with SUBJECT are selected.
6307 If BACKWARD, the previous article is selected instead of the next."
6308   (interactive "P")
6309   (cond
6310    ;; Is there such an article?
6311    ((and (gnus-summary-search-forward unread subject backward)
6312          (or (gnus-summary-display-article (gnus-summary-article-number))
6313              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6314     (gnus-summary-position-point))
6315    ;; If not, we try the first unread, if that is wanted.
6316    ((and subject
6317          gnus-auto-select-same
6318          (gnus-summary-first-unread-article))
6319     (gnus-summary-position-point)
6320     (gnus-message 6 "Wrapped"))
6321    ;; Try to get next/previous article not displayed in this group.
6322    ((and gnus-auto-extend-newsgroup
6323          (not unread) (not subject))
6324     (gnus-summary-goto-article
6325      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6326      nil (count-lines (point-min) (point))))
6327    ;; Go to next/previous group.
6328    (t
6329     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6330       (gnus-summary-jump-to-group gnus-newsgroup-name))
6331     (let ((cmd last-command-char)
6332           (point
6333            (save-excursion
6334              (set-buffer gnus-group-buffer)
6335              (point)))
6336           (group
6337            (if (eq gnus-keep-same-level 'best)
6338                (gnus-summary-best-group gnus-newsgroup-name)
6339              (gnus-summary-search-group backward gnus-keep-same-level))))
6340       ;; For some reason, the group window gets selected.  We change
6341       ;; it back.
6342       (select-window (get-buffer-window (current-buffer)))
6343       ;; Select next unread newsgroup automagically.
6344       (cond
6345        ((or (not gnus-auto-select-next)
6346             (not cmd))
6347         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6348        ((or (eq gnus-auto-select-next 'quietly)
6349             (and (eq gnus-auto-select-next 'slightly-quietly)
6350                  push)
6351             (and (eq gnus-auto-select-next 'almost-quietly)
6352                  (gnus-summary-last-article-p)))
6353         ;; Select quietly.
6354         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6355             (gnus-summary-exit)
6356           (gnus-message 7 "No more%s articles (%s)..."
6357                         (if unread " unread" "")
6358                         (if group (concat "selecting " group)
6359                           "exiting"))
6360           (gnus-summary-next-group nil group backward)))
6361        (t
6362         (when (gnus-key-press-event-p last-input-event)
6363           (gnus-summary-walk-group-buffer
6364            gnus-newsgroup-name cmd unread backward point))))))))
6365
6366 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6367   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6368                       (?\C-p (gnus-group-prev-unread-group 1))))
6369         (cursor-in-echo-area t)
6370         keve key group ended)
6371     (save-excursion
6372       (set-buffer gnus-group-buffer)
6373       (goto-char start)
6374       (setq group
6375             (if (eq gnus-keep-same-level 'best)
6376                 (gnus-summary-best-group gnus-newsgroup-name)
6377               (gnus-summary-search-group backward gnus-keep-same-level))))
6378     (while (not ended)
6379       (gnus-message
6380        5 "No more%s articles%s" (if unread " unread" "")
6381        (if (and group
6382                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6383            (format " (Type %s for %s [%s])"
6384                    (single-key-description cmd) group
6385                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6386          (format " (Type %s to exit %s)"
6387                  (single-key-description cmd)
6388                  gnus-newsgroup-name)))
6389       ;; Confirm auto selection.
6390       (setq key (car (setq keve (gnus-read-event-char))))
6391       (setq ended t)
6392       (cond
6393        ((assq key keystrokes)
6394         (let ((obuf (current-buffer)))
6395           (switch-to-buffer gnus-group-buffer)
6396           (when group
6397             (gnus-group-jump-to-group group))
6398           (eval (cadr (assq key keystrokes)))
6399           (setq group (gnus-group-group-name))
6400           (switch-to-buffer obuf))
6401         (setq ended nil))
6402        ((equal key cmd)
6403         (if (or (not group)
6404                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6405             (gnus-summary-exit)
6406           (gnus-summary-next-group nil group backward)))
6407        (t
6408         (push (cdr keve) unread-command-events))))))
6409
6410 (defun gnus-summary-next-unread-article ()
6411   "Select unread article after current one."
6412   (interactive)
6413   (gnus-summary-next-article
6414    (or (not (eq gnus-summary-goto-unread 'never))
6415        (gnus-summary-last-article-p (gnus-summary-article-number)))
6416    (and gnus-auto-select-same
6417         (gnus-summary-article-subject))))
6418
6419 (defun gnus-summary-prev-article (&optional unread subject)
6420   "Select the article after the current one.
6421 If UNREAD is non-nil, only unread articles are selected."
6422   (interactive "P")
6423   (gnus-summary-next-article unread subject t))
6424
6425 (defun gnus-summary-prev-unread-article ()
6426   "Select unread article before current one."
6427   (interactive)
6428   (gnus-summary-prev-article
6429    (or (not (eq gnus-summary-goto-unread 'never))
6430        (gnus-summary-first-article-p (gnus-summary-article-number)))
6431    (and gnus-auto-select-same
6432         (gnus-summary-article-subject))))
6433
6434 (defun gnus-summary-next-page (&optional lines circular)
6435   "Show next page of the selected article.
6436 If at the end of the current article, select the next article.
6437 LINES says how many lines should be scrolled up.
6438
6439 If CIRCULAR is non-nil, go to the start of the article instead of
6440 selecting the next article when reaching the end of the current
6441 article."
6442   (interactive "P")
6443   (setq gnus-summary-buffer (current-buffer))
6444   (gnus-set-global-variables)
6445   (let ((article (gnus-summary-article-number))
6446         (article-window (get-buffer-window gnus-article-buffer t))
6447         endp)
6448     ;; If the buffer is empty, we have no article.
6449     (unless article
6450       (error "No article to select"))
6451     (gnus-configure-windows 'article)
6452     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6453         (if (and (eq gnus-summary-goto-unread 'never)
6454                  (not (gnus-summary-last-article-p article)))
6455             (gnus-summary-next-article)
6456           (gnus-summary-next-unread-article))
6457       (if (or (null gnus-current-article)
6458               (null gnus-article-current)
6459               (/= article (cdr gnus-article-current))
6460               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6461           ;; Selected subject is different from current article's.
6462           (gnus-summary-display-article article)
6463         (when article-window
6464           (gnus-eval-in-buffer-window gnus-article-buffer
6465             (setq endp (gnus-article-next-page lines)))
6466           (when endp
6467             (cond (circular
6468                    (gnus-summary-beginning-of-article))
6469                   (lines
6470                    (gnus-message 3 "End of message"))
6471                   ((null lines)
6472                    (if (and (eq gnus-summary-goto-unread 'never)
6473                             (not (gnus-summary-last-article-p article)))
6474                        (gnus-summary-next-article)
6475                      (gnus-summary-next-unread-article))))))))
6476     (gnus-summary-recenter)
6477     (gnus-summary-position-point)))
6478
6479 (defun gnus-summary-prev-page (&optional lines move)
6480   "Show previous page of selected article.
6481 Argument LINES specifies lines to be scrolled down.
6482 If MOVE, move to the previous unread article if point is at
6483 the beginning of the buffer."
6484   (interactive "P")
6485   (let ((article (gnus-summary-article-number))
6486         (article-window (get-buffer-window gnus-article-buffer t))
6487         endp)
6488     (gnus-configure-windows 'article)
6489     (if (or (null gnus-current-article)
6490             (null gnus-article-current)
6491             (/= article (cdr gnus-article-current))
6492             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6493         ;; Selected subject is different from current article's.
6494         (gnus-summary-display-article article)
6495       (gnus-summary-recenter)
6496       (when article-window
6497         (gnus-eval-in-buffer-window gnus-article-buffer
6498           (setq endp (gnus-article-prev-page lines)))
6499         (when (and move endp)
6500           (cond (lines
6501                  (gnus-message 3 "Beginning of message"))
6502                 ((null lines)
6503                  (if (and (eq gnus-summary-goto-unread 'never)
6504                           (not (gnus-summary-first-article-p article)))
6505                      (gnus-summary-prev-article)
6506                    (gnus-summary-prev-unread-article))))))))
6507   (gnus-summary-position-point))
6508
6509 (defun gnus-summary-prev-page-or-article (&optional lines)
6510   "Show previous page of selected article.
6511 Argument LINES specifies lines to be scrolled down.
6512 If at the beginning of the article, go to the next article."
6513   (interactive "P")
6514   (gnus-summary-prev-page lines t))
6515
6516 (defun gnus-summary-scroll-up (lines)
6517   "Scroll up (or down) one line current article.
6518 Argument LINES specifies lines to be scrolled up (or down if negative)."
6519   (interactive "p")
6520   (gnus-configure-windows 'article)
6521   (gnus-summary-show-thread)
6522   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6523     (gnus-eval-in-buffer-window gnus-article-buffer
6524       (cond ((> lines 0)
6525              (when (gnus-article-next-page lines)
6526                (gnus-message 3 "End of message")))
6527             ((< lines 0)
6528              (gnus-article-prev-page (- lines))))))
6529   (gnus-summary-recenter)
6530   (gnus-summary-position-point))
6531
6532 (defun gnus-summary-scroll-down (lines)
6533   "Scroll down (or up) one line current article.
6534 Argument LINES specifies lines to be scrolled down (or up if negative)."
6535   (interactive "p")
6536   (gnus-summary-scroll-up (- lines)))
6537
6538 (defun gnus-summary-next-same-subject ()
6539   "Select next article which has the same subject as current one."
6540   (interactive)
6541   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6542
6543 (defun gnus-summary-prev-same-subject ()
6544   "Select previous article which has the same subject as current one."
6545   (interactive)
6546   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6547
6548 (defun gnus-summary-next-unread-same-subject ()
6549   "Select next unread article which has the same subject as current one."
6550   (interactive)
6551   (gnus-summary-next-article t (gnus-summary-article-subject)))
6552
6553 (defun gnus-summary-prev-unread-same-subject ()
6554   "Select previous unread article which has the same subject as current one."
6555   (interactive)
6556   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6557
6558 (defun gnus-summary-first-unread-article ()
6559   "Select the first unread article.
6560 Return nil if there are no unread articles."
6561   (interactive)
6562   (prog1
6563       (when (gnus-summary-first-subject t)
6564         (gnus-summary-show-thread)
6565         (gnus-summary-first-subject t)
6566         (gnus-summary-display-article (gnus-summary-article-number)))
6567     (gnus-summary-position-point)))
6568
6569 (defun gnus-summary-first-unread-subject ()
6570   "Place the point on the subject line of the first unread article.
6571 Return nil if there are no unread articles."
6572   (interactive)
6573   (prog1
6574       (when (gnus-summary-first-subject t)
6575         (gnus-summary-show-thread)
6576         (gnus-summary-first-subject t))
6577     (gnus-summary-position-point)))
6578
6579 (defun gnus-summary-first-article ()
6580   "Select the first article.
6581 Return nil if there are no articles."
6582   (interactive)
6583   (prog1
6584       (when (gnus-summary-first-subject)
6585         (gnus-summary-show-thread)
6586         (gnus-summary-first-subject)
6587         (gnus-summary-display-article (gnus-summary-article-number)))
6588     (gnus-summary-position-point)))
6589
6590 (defun gnus-summary-best-unread-article ()
6591   "Select the unread article with the highest score."
6592   (interactive)
6593   (let ((best -1000000)
6594         (data gnus-newsgroup-data)
6595         article score)
6596     (while data
6597       (and (gnus-data-unread-p (car data))
6598            (> (setq score
6599                     (gnus-summary-article-score (gnus-data-number (car data))))
6600               best)
6601            (setq best score
6602                  article (gnus-data-number (car data))))
6603       (setq data (cdr data)))
6604     (prog1
6605         (if article
6606             (gnus-summary-goto-article article)
6607           (error "No unread articles"))
6608       (gnus-summary-position-point))))
6609
6610 (defun gnus-summary-last-subject ()
6611   "Go to the last displayed subject line in the group."
6612   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6613     (when article
6614       (gnus-summary-goto-subject article))))
6615
6616 (defun gnus-summary-goto-article (article &optional all-headers force)
6617   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6618 If ALL-HEADERS is non-nil, no header lines are hidden.
6619 If FORCE, go to the article even if it isn't displayed.  If FORCE
6620 is a number, it is the line the article is to be displayed on."
6621   (interactive
6622    (list
6623     (completing-read
6624      "Article number or Message-ID: "
6625      (mapcar (lambda (number) (list (int-to-string number)))
6626              gnus-newsgroup-limit))
6627     current-prefix-arg
6628     t))
6629   (prog1
6630       (if (and (stringp article)
6631                (string-match "@" article))
6632           (gnus-summary-refer-article article)
6633         (when (stringp article)
6634           (setq article (string-to-number article)))
6635         (if (gnus-summary-goto-subject article force)
6636             (gnus-summary-display-article article all-headers)
6637           (gnus-message 4 "Couldn't go to article %s" article) nil))
6638     (gnus-summary-position-point)))
6639
6640 (defun gnus-summary-goto-last-article ()
6641   "Go to the previously read article."
6642   (interactive)
6643   (prog1
6644       (when gnus-last-article
6645         (gnus-summary-goto-article gnus-last-article nil t))
6646     (gnus-summary-position-point)))
6647
6648 (defun gnus-summary-pop-article (number)
6649   "Pop one article off the history and go to the previous.
6650 NUMBER articles will be popped off."
6651   (interactive "p")
6652   (let (to)
6653     (setq gnus-newsgroup-history
6654           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6655     (if to
6656         (gnus-summary-goto-article (car to) nil t)
6657       (error "Article history empty")))
6658   (gnus-summary-position-point))
6659
6660 ;; Summary commands and functions for limiting the summary buffer.
6661
6662 (defun gnus-summary-limit-to-articles (n)
6663   "Limit the summary buffer to the next N articles.
6664 If not given a prefix, use the process marked articles instead."
6665   (interactive "P")
6666   (prog1
6667       (let ((articles (gnus-summary-work-articles n)))
6668         (setq gnus-newsgroup-processable nil)
6669         (gnus-summary-limit articles))
6670     (gnus-summary-position-point)))
6671
6672 (defun gnus-summary-pop-limit (&optional total)
6673   "Restore the previous limit.
6674 If given a prefix, remove all limits."
6675   (interactive "P")
6676   (when total
6677     (setq gnus-newsgroup-limits
6678           (list (mapcar (lambda (h) (mail-header-number h))
6679                         gnus-newsgroup-headers))))
6680   (unless gnus-newsgroup-limits
6681     (error "No limit to pop"))
6682   (prog1
6683       (gnus-summary-limit nil 'pop)
6684     (gnus-summary-position-point)))
6685
6686 (defun gnus-summary-limit-to-subject (subject &optional header)
6687   "Limit the summary buffer to articles that have subjects that match a regexp."
6688   (interactive "sLimit to subject (regexp): ")
6689   (unless header
6690     (setq header "subject"))
6691   (when (not (equal "" subject))
6692     (prog1
6693         (let ((articles (gnus-summary-find-matching
6694                          (or header "subject") subject 'all)))
6695           (unless articles
6696             (error "Found no matches for \"%s\"" subject))
6697           (gnus-summary-limit articles))
6698       (gnus-summary-position-point))))
6699
6700 (defun gnus-summary-limit-to-author (from)
6701   "Limit the summary buffer to articles that have authors that match a regexp."
6702   (interactive "sLimit to author (regexp): ")
6703   (gnus-summary-limit-to-subject from "from"))
6704
6705 (defun gnus-summary-limit-to-age (age &optional younger-p)
6706   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6707 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6708 articles that are younger than AGE days."
6709   (interactive
6710    (let ((younger current-prefix-arg)
6711          (days-got nil)
6712          days)
6713      (while (not days-got)
6714        (setq days (if younger
6715                       (read-string "Limit to articles within (in days): ")
6716                     (read-string "Limit to articles older than (in days): ")))
6717        (when (> (length days) 0)
6718          (setq days (read days)))
6719        (if (numberp days)
6720            (setq days-got t)
6721          (message "Please enter a number.")
6722          (sleep-for 1)))
6723      (list days younger)))
6724   (prog1
6725       (let ((data gnus-newsgroup-data)
6726             (cutoff (days-to-time age))
6727             articles d date is-younger)
6728         (while (setq d (pop data))
6729           (when (and (vectorp (gnus-data-header d))
6730                      (setq date (mail-header-date (gnus-data-header d))))
6731             (setq is-younger (time-less-p
6732                               (time-since (condition-case ()
6733                                               (date-to-time date)
6734                                             (error '(0 0))))
6735                               cutoff))
6736             (when (if younger-p
6737                       is-younger
6738                     (not is-younger))
6739               (push (gnus-data-number d) articles))))
6740         (gnus-summary-limit (nreverse articles)))
6741     (gnus-summary-position-point)))
6742
6743 (defun gnus-summary-limit-to-extra (header regexp)
6744   "Limit the summary buffer to articles that match an 'extra' header."
6745   (interactive
6746    (let ((header
6747           (intern
6748            (gnus-completing-read
6749             (symbol-name (car gnus-extra-headers))
6750             "Limit extra header:"
6751             (mapcar (lambda (x)
6752                       (cons (symbol-name x) x))
6753                     gnus-extra-headers)
6754             nil
6755             t))))
6756      (list header
6757            (read-string (format "Limit to header %s (regexp): " header)))))
6758   (when (not (equal "" regexp))
6759     (prog1
6760         (let ((articles (gnus-summary-find-matching
6761                          (cons 'extra header) regexp 'all)))
6762           (unless articles
6763             (error "Found no matches for \"%s\"" regexp))
6764           (gnus-summary-limit articles))
6765       (gnus-summary-position-point))))
6766
6767 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6768 (make-obsolete
6769  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6770
6771 (defun gnus-summary-limit-to-unread (&optional all)
6772   "Limit the summary buffer to articles that are not marked as read.
6773 If ALL is non-nil, limit strictly to unread articles."
6774   (interactive "P")
6775   (if all
6776       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6777     (gnus-summary-limit-to-marks
6778      ;; Concat all the marks that say that an article is read and have
6779      ;; those removed.
6780      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6781            gnus-killed-mark gnus-kill-file-mark
6782            gnus-low-score-mark gnus-expirable-mark
6783            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6784            gnus-duplicate-mark gnus-souped-mark)
6785      'reverse)))
6786
6787 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6788 (make-obsolete 'gnus-summary-delete-marked-with
6789                'gnus-summary-limit-exlude-marks)
6790
6791 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6792   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6793 If REVERSE, limit the summary buffer to articles that are marked
6794 with MARKS.  MARKS can either be a string of marks or a list of marks.
6795 Returns how many articles were removed."
6796   (interactive "sMarks: ")
6797   (gnus-summary-limit-to-marks marks t))
6798
6799 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6800   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6801 If REVERSE (the prefix), limit the summary buffer to articles that are
6802 not marked with MARKS.  MARKS can either be a string of marks or a
6803 list of marks.
6804 Returns how many articles were removed."
6805   (interactive "sMarks: \nP")
6806   (prog1
6807       (let ((data gnus-newsgroup-data)
6808             (marks (if (listp marks) marks
6809                      (append marks nil))) ; Transform to list.
6810             articles)
6811         (while data
6812           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6813                   (memq (gnus-data-mark (car data)) marks))
6814             (push (gnus-data-number (car data)) articles))
6815           (setq data (cdr data)))
6816         (gnus-summary-limit articles))
6817     (gnus-summary-position-point)))
6818
6819 (defun gnus-summary-limit-to-score (&optional score)
6820   "Limit to articles with score at or above SCORE."
6821   (interactive "P")
6822   (setq score (if score
6823                   (prefix-numeric-value score)
6824                 (or gnus-summary-default-score 0)))
6825   (let ((data gnus-newsgroup-data)
6826         articles)
6827     (while data
6828       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6829                 score)
6830         (push (gnus-data-number (car data)) articles))
6831       (setq data (cdr data)))
6832     (prog1
6833         (gnus-summary-limit articles)
6834       (gnus-summary-position-point))))
6835
6836 (defun gnus-summary-limit-include-thread (id)
6837   "Display all the hidden articles that is in the thread with ID in it.
6838 When called interactively, ID is the Message-ID of the current
6839 article."
6840   (interactive (list (mail-header-id (gnus-summary-article-header))))
6841   (let ((articles (gnus-articles-in-thread
6842                    (gnus-id-to-thread (gnus-root-id id)))))
6843     (prog1
6844         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6845       (gnus-summary-limit-include-matching-articles
6846        "subject"
6847        (regexp-quote (gnus-simplify-subject-re
6848                       (mail-header-subject (gnus-id-to-header id)))))
6849       (gnus-summary-position-point))))
6850
6851 (defun gnus-summary-limit-include-matching-articles (header regexp)
6852   "Display all the hidden articles that have HEADERs that match REGEXP."
6853   (interactive (list (read-string "Match on header: ")
6854                      (read-string "Regexp: ")))
6855   (let ((articles (gnus-find-matching-articles header regexp)))
6856     (prog1
6857         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6858       (gnus-summary-position-point))))
6859
6860 (defun gnus-summary-limit-include-dormant ()
6861   "Display all the hidden articles that are marked as dormant.
6862 Note that this command only works on a subset of the articles currently
6863 fetched for this group."
6864   (interactive)
6865   (unless gnus-newsgroup-dormant
6866     (error "There are no dormant articles in this group"))
6867   (prog1
6868       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6869     (gnus-summary-position-point)))
6870
6871 (defun gnus-summary-limit-exclude-dormant ()
6872   "Hide all dormant articles."
6873   (interactive)
6874   (prog1
6875       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6876     (gnus-summary-position-point)))
6877
6878 (defun gnus-summary-limit-exclude-childless-dormant ()
6879   "Hide all dormant articles that have no children."
6880   (interactive)
6881   (let ((data (gnus-data-list t))
6882         articles d children)
6883     ;; Find all articles that are either not dormant or have
6884     ;; children.
6885     (while (setq d (pop data))
6886       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6887                 (and (setq children
6888                            (gnus-article-children (gnus-data-number d)))
6889                      (let (found)
6890                        (while children
6891                          (when (memq (car children) articles)
6892                            (setq children nil
6893                                  found t))
6894                          (pop children))
6895                        found)))
6896         (push (gnus-data-number d) articles)))
6897     ;; Do the limiting.
6898     (prog1
6899         (gnus-summary-limit articles)
6900       (gnus-summary-position-point))))
6901
6902 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6903   "Mark all unread excluded articles as read.
6904 If ALL, mark even excluded ticked and dormants as read."
6905   (interactive "P")
6906   (let ((articles (gnus-sorted-complement
6907                    (sort
6908                     (mapcar (lambda (h) (mail-header-number h))
6909                             gnus-newsgroup-headers)
6910                     '<)
6911                    (sort gnus-newsgroup-limit '<)))
6912         article)
6913     (setq gnus-newsgroup-unreads
6914           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6915     (if all
6916         (setq gnus-newsgroup-dormant nil
6917               gnus-newsgroup-marked nil
6918               gnus-newsgroup-reads
6919               (nconc
6920                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6921                gnus-newsgroup-reads))
6922       (while (setq article (pop articles))
6923         (unless (or (memq article gnus-newsgroup-dormant)
6924                     (memq article gnus-newsgroup-marked))
6925           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6926
6927 (defun gnus-summary-limit (articles &optional pop)
6928   (if pop
6929       ;; We pop the previous limit off the stack and use that.
6930       (setq articles (car gnus-newsgroup-limits)
6931             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6932     ;; We use the new limit, so we push the old limit on the stack.
6933     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6934   ;; Set the limit.
6935   (setq gnus-newsgroup-limit articles)
6936   (let ((total (length gnus-newsgroup-data))
6937         (data (gnus-data-find-list (gnus-summary-article-number)))
6938         (gnus-summary-mark-below nil)   ; Inhibit this.
6939         found)
6940     ;; This will do all the work of generating the new summary buffer
6941     ;; according to the new limit.
6942     (gnus-summary-prepare)
6943     ;; Hide any threads, possibly.
6944     (and gnus-show-threads
6945          gnus-thread-hide-subtree
6946          (gnus-summary-hide-all-threads))
6947     ;; Try to return to the article you were at, or one in the
6948     ;; neighborhood.
6949     (when data
6950       ;; We try to find some article after the current one.
6951       (while data
6952         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6953           (setq data nil
6954                 found t))
6955         (setq data (cdr data))))
6956     (unless found
6957       ;; If there is no data, that means that we were after the last
6958       ;; article.  The same goes when we can't find any articles
6959       ;; after the current one.
6960       (goto-char (point-max))
6961       (gnus-summary-find-prev))
6962     (gnus-set-mode-line 'summary)
6963     ;; We return how many articles were removed from the summary
6964     ;; buffer as a result of the new limit.
6965     (- total (length gnus-newsgroup-data))))
6966
6967 (defsubst gnus-invisible-cut-children (threads)
6968   (let ((num 0))
6969     (while threads
6970       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6971         (incf num))
6972       (pop threads))
6973     (< num 2)))
6974
6975 (defsubst gnus-cut-thread (thread)
6976   "Go forwards in the thread until we find an article that we want to display."
6977   (when (or (eq gnus-fetch-old-headers 'some)
6978             (eq gnus-fetch-old-headers 'invisible)
6979             (numberp gnus-fetch-old-headers)
6980             (eq gnus-build-sparse-threads 'some)
6981             (eq gnus-build-sparse-threads 'more))
6982     ;; Deal with old-fetched headers and sparse threads.
6983     (while (and
6984             thread
6985             (or
6986              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6987              (gnus-summary-article-ancient-p
6988               (mail-header-number (car thread))))
6989             (if (or (<= (length (cdr thread)) 1)
6990                     (eq gnus-fetch-old-headers 'invisible))
6991                 (setq gnus-newsgroup-limit
6992                       (delq (mail-header-number (car thread))
6993                             gnus-newsgroup-limit)
6994                       thread (cadr thread))
6995               (when (gnus-invisible-cut-children (cdr thread))
6996                 (let ((th (cdr thread)))
6997                   (while th
6998                     (if (memq (mail-header-number (caar th))
6999                               gnus-newsgroup-limit)
7000                         (setq thread (car th)
7001                               th nil)
7002                       (setq th (cdr th))))))))))
7003   thread)
7004
7005 (defun gnus-cut-threads (threads)
7006   "Cut off all uninteresting articles from the beginning of threads."
7007   (when (or (eq gnus-fetch-old-headers 'some)
7008             (eq gnus-fetch-old-headers 'invisible)
7009             (numberp gnus-fetch-old-headers)
7010             (eq gnus-build-sparse-threads 'some)
7011             (eq gnus-build-sparse-threads 'more))
7012     (let ((th threads))
7013       (while th
7014         (setcar th (gnus-cut-thread (car th)))
7015         (setq th (cdr th)))))
7016   ;; Remove nixed out threads.
7017   (delq nil threads))
7018
7019 (defun gnus-summary-initial-limit (&optional show-if-empty)
7020   "Figure out what the initial limit is supposed to be on group entry.
7021 This entails weeding out unwanted dormants, low-scored articles,
7022 fetch-old-headers verbiage, and so on."
7023   ;; Most groups have nothing to remove.
7024   (if (or gnus-inhibit-limiting
7025           (and (null gnus-newsgroup-dormant)
7026                (not (eq gnus-fetch-old-headers 'some))
7027                (not (numberp gnus-fetch-old-headers))
7028                (not (eq gnus-fetch-old-headers 'invisible))
7029                (null gnus-summary-expunge-below)
7030                (not (eq gnus-build-sparse-threads 'some))
7031                (not (eq gnus-build-sparse-threads 'more))
7032                (null gnus-thread-expunge-below)
7033                (not gnus-use-nocem)))
7034       ()                                ; Do nothing.
7035     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7036     (setq gnus-newsgroup-limit nil)
7037     (mapatoms
7038      (lambda (node)
7039        (unless (car (symbol-value node))
7040          ;; These threads have no parents -- they are roots.
7041          (let ((nodes (cdr (symbol-value node)))
7042                thread)
7043            (while nodes
7044              (if (and gnus-thread-expunge-below
7045                       (< (gnus-thread-total-score (car nodes))
7046                          gnus-thread-expunge-below))
7047                  (gnus-expunge-thread (pop nodes))
7048                (setq thread (pop nodes))
7049                (gnus-summary-limit-children thread))))))
7050      gnus-newsgroup-dependencies)
7051     ;; If this limitation resulted in an empty group, we might
7052     ;; pop the previous limit and use it instead.
7053     (when (and (not gnus-newsgroup-limit)
7054                show-if-empty)
7055       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7056     gnus-newsgroup-limit))
7057
7058 (defun gnus-summary-limit-children (thread)
7059   "Return 1 if this subthread is visible and 0 if it is not."
7060   ;; First we get the number of visible children to this thread.  This
7061   ;; is done by recursing down the thread using this function, so this
7062   ;; will really go down to a leaf article first, before slowly
7063   ;; working its way up towards the root.
7064   (when thread
7065     (let ((children
7066            (if (cdr thread)
7067                (apply '+ (mapcar 'gnus-summary-limit-children
7068                                  (cdr thread)))
7069              0))
7070           (number (mail-header-number (car thread)))
7071           score)
7072       (if (and
7073            (not (memq number gnus-newsgroup-marked))
7074            (or
7075            ;; If this article is dormant and has absolutely no visible
7076             ;; children, then this article isn't visible.
7077             (and (memq number gnus-newsgroup-dormant)
7078                  (zerop children))
7079             ;; If this is "fetch-old-headered" and there is no
7080             ;; visible children, then we don't want this article.
7081             (and (or (eq gnus-fetch-old-headers 'some)
7082                      (numberp gnus-fetch-old-headers))
7083                  (gnus-summary-article-ancient-p number)
7084                  (zerop children))
7085             ;; If this is "fetch-old-headered" and `invisible', then
7086             ;; we don't want this article.
7087             (and (eq gnus-fetch-old-headers 'invisible)
7088                  (gnus-summary-article-ancient-p number))
7089            ;; If this is a sparsely inserted article with no children,
7090             ;; we don't want it.
7091             (and (eq gnus-build-sparse-threads 'some)
7092                  (gnus-summary-article-sparse-p number)
7093                  (zerop children))
7094             ;; If we use expunging, and this article is really
7095             ;; low-scored, then we don't want this article.
7096             (when (and gnus-summary-expunge-below
7097                        (< (setq score
7098                                 (or (cdr (assq number gnus-newsgroup-scored))
7099                                     gnus-summary-default-score))
7100                           gnus-summary-expunge-below))
7101               ;; We increase the expunge-tally here, but that has
7102               ;; nothing to do with the limits, really.
7103               (incf gnus-newsgroup-expunged-tally)
7104               ;; We also mark as read here, if that's wanted.
7105               (when (and gnus-summary-mark-below
7106                          (< score gnus-summary-mark-below))
7107                 (setq gnus-newsgroup-unreads
7108                       (delq number gnus-newsgroup-unreads))
7109                 (if gnus-newsgroup-auto-expire
7110                     (push number gnus-newsgroup-expirable)
7111                   (push (cons number gnus-low-score-mark)
7112                         gnus-newsgroup-reads)))
7113               t)
7114             ;; Check NoCeM things.
7115             (if (and gnus-use-nocem
7116                      (gnus-nocem-unwanted-article-p
7117                       (mail-header-id (car thread))))
7118                 (progn
7119                   (setq gnus-newsgroup-unreads
7120                         (delq number gnus-newsgroup-unreads))
7121                   t))))
7122           ;; Nope, invisible article.
7123           0
7124        ;; Ok, this article is to be visible, so we add it to the limit
7125         ;; and return 1.
7126         (push number gnus-newsgroup-limit)
7127         1))))
7128
7129 (defun gnus-expunge-thread (thread)
7130   "Mark all articles in THREAD as read."
7131   (let* ((number (mail-header-number (car thread))))
7132     (incf gnus-newsgroup-expunged-tally)
7133     ;; We also mark as read here, if that's wanted.
7134     (setq gnus-newsgroup-unreads
7135           (delq number gnus-newsgroup-unreads))
7136     (if gnus-newsgroup-auto-expire
7137         (push number gnus-newsgroup-expirable)
7138       (push (cons number gnus-low-score-mark)
7139             gnus-newsgroup-reads)))
7140   ;; Go recursively through all subthreads.
7141   (mapcar 'gnus-expunge-thread (cdr thread)))
7142
7143 ;; Summary article oriented commands
7144
7145 (defun gnus-summary-refer-parent-article (n)
7146   "Refer parent article N times.
7147 If N is negative, go to ancestor -N instead.
7148 The difference between N and the number of articles fetched is returned."
7149   (interactive "p")
7150   (let ((skip 1)
7151         error header ref)
7152     (when (not (natnump n))
7153       (setq skip (abs n)
7154             n 1))
7155     (while (and (> n 0)
7156                 (not error))
7157       (setq header (gnus-summary-article-header))
7158       (if (and (eq (mail-header-number header)
7159                    (cdr gnus-article-current))
7160                (equal gnus-newsgroup-name
7161                       (car gnus-article-current)))
7162           ;; If we try to find the parent of the currently
7163           ;; displayed article, then we take a look at the actual
7164           ;; References header, since this is slightly more
7165           ;; reliable than the References field we got from the
7166           ;; server.
7167           (save-excursion
7168             (set-buffer gnus-original-article-buffer)
7169             (nnheader-narrow-to-headers)
7170             (unless (setq ref (message-fetch-field "references"))
7171               (setq ref (message-fetch-field "in-reply-to")))
7172             (widen))
7173         (setq ref
7174               ;; It's not the current article, so we take a bet on
7175               ;; the value we got from the server.
7176               (mail-header-references header)))
7177       (if (and ref
7178                (not (equal ref "")))
7179           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7180             (gnus-message 1 "Couldn't find parent"))
7181         (gnus-message 1 "No references in article %d"
7182                       (gnus-summary-article-number))
7183         (setq error t))
7184       (decf n))
7185     (gnus-summary-position-point)
7186     n))
7187
7188 (defun gnus-summary-refer-references ()
7189   "Fetch all articles mentioned in the References header.
7190 Return the number of articles fetched."
7191   (interactive)
7192   (let ((ref (mail-header-references (gnus-summary-article-header)))
7193         (current (gnus-summary-article-number))
7194         (n 0))
7195     (if (or (not ref)
7196             (equal ref ""))
7197         (error "No References in the current article")
7198       ;; For each Message-ID in the References header...
7199       (while (string-match "<[^>]*>" ref)
7200         (incf n)
7201         ;; ... fetch that article.
7202         (gnus-summary-refer-article
7203          (prog1 (match-string 0 ref)
7204            (setq ref (substring ref (match-end 0))))))
7205       (gnus-summary-goto-subject current)
7206       (gnus-summary-position-point)
7207       n)))
7208
7209 (defun gnus-summary-refer-thread (&optional limit)
7210   "Fetch all articles in the current thread.
7211 If LIMIT (the numerical prefix), fetch that many old headers instead
7212 of what's specified by the `gnus-refer-thread-limit' variable."
7213   (interactive "P")
7214   (let ((id (mail-header-id (gnus-summary-article-header)))
7215         (limit (if limit (prefix-numeric-value limit)
7216                  gnus-refer-thread-limit)))
7217     ;; We want to fetch LIMIT *old* headers, but we also have to
7218     ;; re-fetch all the headers in the current buffer, because many of
7219     ;; them may be undisplayed.  So we adjust LIMIT.
7220     (when (numberp limit)
7221       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7222     (unless (eq gnus-fetch-old-headers 'invisible)
7223       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7224       ;; Retrieve the headers and read them in.
7225       (if (eq (gnus-retrieve-headers
7226                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7227               'nov)
7228           (gnus-build-all-threads)
7229         (error "Can't fetch thread from backends that don't support NOV"))
7230       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7231     (gnus-summary-limit-include-thread id)))
7232
7233 (defun gnus-summary-refer-article (message-id)
7234   "Fetch an article specified by MESSAGE-ID."
7235   (interactive "sMessage-ID: ")
7236   (when (and (stringp message-id)
7237              (not (zerop (length message-id))))
7238     ;; Construct the correct Message-ID if necessary.
7239     ;; Suggested by tale@pawl.rpi.edu.
7240     (unless (string-match "^<" message-id)
7241       (setq message-id (concat "<" message-id)))
7242     (unless (string-match ">$" message-id)
7243       (setq message-id (concat message-id ">")))
7244     (let* ((header (gnus-id-to-header message-id))
7245            (sparse (and header
7246                         (gnus-summary-article-sparse-p
7247                          (mail-header-number header))
7248                         (memq (mail-header-number header)
7249                               gnus-newsgroup-limit)))
7250            number)
7251       (cond
7252        ;; If the article is present in the buffer we just go to it.
7253        ((and header
7254              (or (not (gnus-summary-article-sparse-p
7255                        (mail-header-number header)))
7256                  sparse))
7257         (prog1
7258             (gnus-summary-goto-article
7259              (mail-header-number header) nil t)
7260           (when sparse
7261             (gnus-summary-update-article (mail-header-number header)))))
7262        (t
7263         ;; We fetch the article.
7264         (catch 'found
7265           (dolist (gnus-override-method (gnus-refer-article-methods))
7266             (gnus-check-server gnus-override-method)
7267             ;; Fetch the header, and display the article.
7268             (when (setq number (gnus-summary-insert-subject message-id))
7269               (gnus-summary-select-article nil nil nil number)
7270               (throw 'found t)))
7271           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7272
7273 (defun gnus-refer-article-methods ()
7274   "Return a list of referrable methods."
7275   (cond
7276    ;; No method, so we default to current and native.
7277    ((null gnus-refer-article-method)
7278     (list gnus-current-select-method gnus-select-method))
7279    ;; Current.
7280    ((eq 'current gnus-refer-article-method)
7281     (list gnus-current-select-method))
7282    ;; List of select methods.
7283    ((not (and (symbolp (car gnus-refer-article-method))
7284               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7285     (let (out)
7286       (dolist (method gnus-refer-article-method)
7287         (push (if (eq 'current method)
7288                   gnus-current-select-method
7289                 method)
7290               out))
7291       (nreverse out)))
7292    ;; One single select method.
7293    (t
7294     (list gnus-refer-article-method))))
7295
7296 (defun gnus-summary-edit-parameters ()
7297   "Edit the group parameters of the current group."
7298   (interactive)
7299   (gnus-group-edit-group gnus-newsgroup-name 'params))
7300
7301 (defun gnus-summary-customize-parameters ()
7302   "Customize the group parameters of the current group."
7303   (interactive)
7304   (gnus-group-customize gnus-newsgroup-name))
7305
7306 (defun gnus-summary-enter-digest-group (&optional force)
7307   "Enter an nndoc group based on the current article.
7308 If FORCE, force a digest interpretation.  If not, try
7309 to guess what the document format is."
7310   (interactive "P")
7311   (let ((conf gnus-current-window-configuration))
7312     (save-excursion
7313       (gnus-summary-select-article))
7314     (setq gnus-current-window-configuration conf)
7315     (let* ((name (format "%s-%d"
7316                          (gnus-group-prefixed-name
7317                           gnus-newsgroup-name (list 'nndoc ""))
7318                          (save-excursion
7319                            (set-buffer gnus-summary-buffer)
7320                            gnus-current-article)))
7321            (ogroup gnus-newsgroup-name)
7322            (params (append (gnus-info-params (gnus-get-info ogroup))
7323                            (list (cons 'to-group ogroup))
7324                            (list (cons 'save-article-group ogroup))))
7325            (case-fold-search t)
7326            (buf (current-buffer))
7327            dig to-address)
7328       (save-excursion
7329         (set-buffer gnus-original-article-buffer)
7330         ;; Have the digest group inherit the main mail address of
7331         ;; the parent article.
7332         (when (setq to-address (or (message-fetch-field "reply-to")
7333                                    (message-fetch-field "from")))
7334           (setq params (append
7335                         (list (cons 'to-address
7336                                     (funcall gnus-decode-encoded-word-function
7337                                              to-address))))))
7338         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7339         (insert-buffer-substring gnus-original-article-buffer)
7340         ;; Remove lines that may lead nndoc to misinterpret the
7341         ;; document type.
7342         (narrow-to-region
7343          (goto-char (point-min))
7344          (or (search-forward "\n\n" nil t) (point)))
7345         (goto-char (point-min))
7346         (delete-matching-lines "^Path:\\|^From ")
7347         (widen))
7348       (unwind-protect
7349           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7350                     (gnus-newsgroup-ephemeral-ignored-charsets
7351                      gnus-newsgroup-ignored-charsets))
7352                 (gnus-group-read-ephemeral-group
7353                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7354                               (nndoc-article-type
7355                                ,(if force 'mbox 'guess))) t))
7356             ;; Make all postings to this group go to the parent group.
7357               (nconc (gnus-info-params (gnus-get-info name))
7358                      params)
7359             ;; Couldn't select this doc group.
7360             (switch-to-buffer buf)
7361             (gnus-set-global-variables)
7362             (gnus-configure-windows 'summary)
7363             (gnus-message 3 "Article couldn't be entered?"))
7364         (kill-buffer dig)))))
7365
7366 (defun gnus-summary-read-document (n)
7367   "Open a new group based on the current article(s).
7368 This will allow you to read digests and other similar
7369 documents as newsgroups.
7370 Obeys the standard process/prefix convention."
7371   (interactive "P")
7372   (let* ((articles (gnus-summary-work-articles n))
7373          (ogroup gnus-newsgroup-name)
7374          (params (append (gnus-info-params (gnus-get-info ogroup))
7375                          (list (cons 'to-group ogroup))))
7376          article group egroup groups vgroup)
7377     (while (setq article (pop articles))
7378       (setq group (format "%s-%d" gnus-newsgroup-name article))
7379       (gnus-summary-remove-process-mark article)
7380       (when (gnus-summary-display-article article)
7381         (save-excursion
7382           (with-temp-buffer
7383             (insert-buffer-substring gnus-original-article-buffer)
7384             ;; Remove some headers that may lead nndoc to make
7385             ;; the wrong guess.
7386             (message-narrow-to-head)
7387             (goto-char (point-min))
7388             (delete-matching-lines "^\\(Path\\):\\|^From ")
7389             (widen)
7390             (if (setq egroup
7391                       (gnus-group-read-ephemeral-group
7392                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7393                                      (nndoc-article-type guess))
7394                        t nil t))
7395                 (progn
7396             ;; Make all postings to this group go to the parent group.
7397                   (nconc (gnus-info-params (gnus-get-info egroup))
7398                          params)
7399                   (push egroup groups))
7400               ;; Couldn't select this doc group.
7401               (gnus-error 3 "Article couldn't be entered"))))))
7402     ;; Now we have selected all the documents.
7403     (cond
7404      ((not groups)
7405       (error "None of the articles could be interpreted as documents"))
7406      ((gnus-group-read-ephemeral-group
7407        (setq vgroup (format
7408                      "nnvirtual:%s-%s" gnus-newsgroup-name
7409                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7410        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7411        t
7412        (cons (current-buffer) 'summary)))
7413      (t
7414       (error "Couldn't select virtual nndoc group")))))
7415
7416 (defun gnus-summary-isearch-article (&optional regexp-p)
7417   "Do incremental search forward on the current article.
7418 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7419   (interactive "P")
7420   (gnus-summary-select-article)
7421   (gnus-configure-windows 'article)
7422   (gnus-eval-in-buffer-window gnus-article-buffer
7423     (save-restriction
7424       (widen)
7425       (isearch-forward regexp-p))))
7426
7427 (defun gnus-summary-search-article-forward (regexp &optional backward)
7428   "Search for an article containing REGEXP forward.
7429 If BACKWARD, search backward instead."
7430   (interactive
7431    (list (read-string
7432           (format "Search article %s (regexp%s): "
7433                   (if current-prefix-arg "backward" "forward")
7434                   (if gnus-last-search-regexp
7435                       (concat ", default " gnus-last-search-regexp)
7436                     "")))
7437          current-prefix-arg))
7438   (if (string-equal regexp "")
7439       (setq regexp (or gnus-last-search-regexp ""))
7440     (setq gnus-last-search-regexp regexp)
7441     (setq gnus-article-before-search gnus-current-article))
7442   ;; Intentionally set gnus-last-article.
7443   (setq gnus-last-article gnus-article-before-search)
7444   (let ((gnus-last-article gnus-last-article))
7445     (if (gnus-summary-search-article regexp backward)
7446         (gnus-summary-show-thread)
7447       (error "Search failed: \"%s\"" regexp))))
7448
7449 (defun gnus-summary-search-article-backward (regexp)
7450   "Search for an article containing REGEXP backward."
7451   (interactive
7452    (list (read-string
7453           (format "Search article backward (regexp%s): "
7454                   (if gnus-last-search-regexp
7455                       (concat ", default " gnus-last-search-regexp)
7456                     "")))))
7457   (gnus-summary-search-article-forward regexp 'backward))
7458
7459 (defun gnus-summary-search-article (regexp &optional backward)
7460   "Search for an article containing REGEXP.
7461 Optional argument BACKWARD means do search for backward.
7462 `gnus-select-article-hook' is not called during the search."
7463   ;; We have to require this here to make sure that the following
7464   ;; dynamic binding isn't shadowed by autoloading.
7465   (require 'gnus-async)
7466   (require 'gnus-art)
7467   (let ((gnus-select-article-hook nil)  ;Disable hook.
7468         (gnus-article-prepare-hook nil)
7469         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7470         (gnus-use-article-prefetch nil)
7471         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7472         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7473         (sum (current-buffer))
7474         (gnus-display-mime-function nil)
7475         (found nil)
7476         point)
7477     (gnus-save-hidden-threads
7478       (gnus-summary-select-article)
7479       (set-buffer gnus-article-buffer)
7480       (goto-char (window-point (get-buffer-window (current-buffer))))
7481       (when backward
7482         (forward-line -1))
7483       (while (not found)
7484         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7485         (if (if backward
7486                 (re-search-backward regexp nil t)
7487               (re-search-forward regexp nil t))
7488             ;; We found the regexp.
7489             (progn
7490               (setq found 'found)
7491               (beginning-of-line)
7492               (set-window-start
7493                (get-buffer-window (current-buffer))
7494                (point))
7495               (forward-line 1)
7496               (set-window-point
7497                (get-buffer-window (current-buffer))
7498                (point))
7499               (set-buffer sum)
7500               (setq point (point)))
7501           ;; We didn't find it, so we go to the next article.
7502           (set-buffer sum)
7503           (setq found 'not)
7504           (while (eq found 'not)
7505             (if (not (if backward (gnus-summary-find-prev)
7506                        (gnus-summary-find-next)))
7507                 ;; No more articles.
7508                 (setq found t)
7509               ;; Select the next article and adjust point.
7510               (unless (gnus-summary-article-sparse-p
7511                        (gnus-summary-article-number))
7512                 (setq found nil)
7513                 (gnus-summary-select-article)
7514                 (set-buffer gnus-article-buffer)
7515                 (widen)
7516                 (goto-char (if backward (point-max) (point-min))))))))
7517       (gnus-message 7 ""))
7518     ;; Return whether we found the regexp.
7519     (when (eq found 'found)
7520       (goto-char point)
7521       (gnus-summary-show-thread)
7522       (gnus-summary-goto-subject gnus-current-article)
7523       (gnus-summary-position-point)
7524       t)))
7525
7526 (defun gnus-find-matching-articles (header regexp)
7527   "Return a list of all articles that match REGEXP on HEADER.
7528 This search includes all articles in the current group that Gnus has
7529 fetched headers for, whether they are displayed or not."
7530   (let ((articles nil)
7531         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7532         (case-fold-search t))
7533     (dolist (header gnus-newsgroup-headers)
7534       (when (string-match regexp (funcall func header))
7535         (push (mail-header-number header) articles)))
7536     (nreverse articles)))
7537
7538 (defun gnus-summary-find-matching (header regexp &optional backward unread
7539                                           not-case-fold)
7540   "Return a list of all articles that match REGEXP on HEADER.
7541 The search stars on the current article and goes forwards unless
7542 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7543 If UNREAD is non-nil, only unread articles will
7544 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7545 in the comparisons."
7546   (let ((case-fold-search (not not-case-fold))
7547         articles d func)
7548     (if (consp header)
7549         (if (eq (car header) 'extra)
7550             (setq func
7551                   `(lambda (h)
7552                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7553                          "")))
7554           (error "%s is an invalid header" header))
7555       (unless (fboundp (intern (concat "mail-header-" header)))
7556         (error "%s is not a valid header" header))
7557       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7558     (dolist (d (if (eq backward 'all)
7559                    gnus-newsgroup-data
7560                  (gnus-data-find-list
7561                   (gnus-summary-article-number)
7562                   (gnus-data-list backward))))
7563       (when (and (or (not unread)       ; We want all articles...
7564                      (gnus-data-unread-p d)) ; Or just unreads.
7565                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7566                  (string-match regexp
7567                                (funcall func (gnus-data-header d)))) ; Match.
7568         (push (gnus-data-number d) articles))) ; Success!
7569     (nreverse articles)))
7570
7571 (defun gnus-summary-execute-command (header regexp command &optional backward)
7572   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7573 If HEADER is an empty string (or nil), the match is done on the entire
7574 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7575   (interactive
7576    (list (let ((completion-ignore-case t))
7577            (completing-read
7578             "Header name: "
7579             (mapcar (lambda (header) (list (format "%s" header)))
7580                     (append
7581                      '("Number" "Subject" "From" "Lines" "Date"
7582                        "Message-ID" "Xref" "References" "Body")
7583                      gnus-extra-headers))
7584             nil 'require-match))
7585          (read-string "Regexp: ")
7586          (read-key-sequence "Command: ")
7587          current-prefix-arg))
7588   (when (equal header "Body")
7589     (setq header ""))
7590   ;; Hidden thread subtrees must be searched as well.
7591   (gnus-summary-show-all-threads)
7592   ;; We don't want to change current point nor window configuration.
7593   (save-excursion
7594     (save-window-excursion
7595       (gnus-message 6 "Executing %s..." (key-description command))
7596 ;; We'd like to execute COMMAND interactively so as to give arguments.
7597       (gnus-execute header regexp
7598                     `(call-interactively ',(key-binding command))
7599                     backward)
7600       (gnus-message 6 "Executing %s...done" (key-description command)))))
7601
7602 (defun gnus-summary-beginning-of-article ()
7603   "Scroll the article back to the beginning."
7604   (interactive)
7605   (gnus-summary-select-article)
7606   (gnus-configure-windows 'article)
7607   (gnus-eval-in-buffer-window gnus-article-buffer
7608     (widen)
7609     (goto-char (point-min))
7610     (when gnus-page-broken
7611       (gnus-narrow-to-page))))
7612
7613 (defun gnus-summary-end-of-article ()
7614   "Scroll to the end of the article."
7615   (interactive)
7616   (gnus-summary-select-article)
7617   (gnus-configure-windows 'article)
7618   (gnus-eval-in-buffer-window gnus-article-buffer
7619     (widen)
7620     (goto-char (point-max))
7621     (recenter -3)
7622     (when gnus-page-broken
7623       (gnus-narrow-to-page))))
7624
7625 (defun gnus-summary-print-article (&optional filename n)
7626   "Generate and print a PostScript image of the N next (mail) articles.
7627
7628 If N is negative, print the N previous articles.  If N is nil and articles
7629 have been marked with the process mark, print these instead.
7630
7631 If the optional first argument FILENAME is nil, send the image to the
7632 printer.  If FILENAME is a string, save the PostScript image in a file with
7633 that name.  If FILENAME is a number, prompt the user for the name of the file
7634 to save in."
7635   (interactive (list (ps-print-preprint current-prefix-arg)))
7636   (dolist (article (gnus-summary-work-articles n))
7637     (gnus-summary-select-article nil nil 'pseudo article)
7638     (gnus-eval-in-buffer-window gnus-article-buffer
7639       (let ((buffer (generate-new-buffer " *print*")))
7640         (unwind-protect
7641             (progn
7642               (copy-to-buffer buffer (point-min) (point-max))
7643               (set-buffer buffer)
7644               (gnus-article-delete-invisible-text)
7645               (when (gnus-visual-p 'article-highlight 'highlight)
7646                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7647                 ;; highlight.
7648                 (let ((gnus-article-buffer buffer))
7649                   (gnus-article-highlight-citation t)
7650                   (gnus-article-highlight-signature)))
7651               (let ((ps-left-header
7652                      (list
7653                       (concat "("
7654                               (mail-header-subject gnus-current-headers) ")")
7655                       (concat "("
7656                               (mail-header-from gnus-current-headers) ")")))
7657                     (ps-right-header
7658                      (list
7659                       "/pagenumberstring load"
7660                       (concat "("
7661                               (mail-header-date gnus-current-headers) ")"))))
7662                 (gnus-run-hooks 'gnus-ps-print-hook)
7663                 (save-excursion
7664                   (if window-system
7665                       (ps-spool-buffer-with-faces)
7666                     (ps-spool-buffer)))))
7667           (kill-buffer buffer))))
7668     (gnus-summary-remove-process-mark article))
7669   (ps-despool filename))
7670
7671 (defun gnus-summary-show-article (&optional arg)
7672   "Force re-fetching of the current article.
7673 If ARG (the prefix) is a number, show the article with the charset
7674 defined in `gnus-summary-show-article-charset-alist', or the charset
7675 inputed.
7676 If ARG (the prefix) is non-nil and not a number, show the raw article
7677 without any article massaging functions being run."
7678   (interactive "P")
7679   (cond
7680    ((numberp arg)
7681     (let ((gnus-newsgroup-charset
7682            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7683                (mm-read-coding-system "Charset: ")))
7684           (gnus-newsgroup-ignored-charsets 'gnus-all))
7685       (gnus-summary-select-article nil 'force)
7686       (let ((deps gnus-newsgroup-dependencies)
7687             head header)
7688         (save-excursion
7689           (set-buffer gnus-original-article-buffer)
7690           (save-restriction
7691             (message-narrow-to-head)
7692             (setq head (buffer-string)))
7693           (with-temp-buffer
7694             (insert (format "211 %d Article retrieved.\n"
7695                             (cdr gnus-article-current)))
7696             (insert head)
7697             (insert ".\n")
7698             (let ((nntp-server-buffer (current-buffer)))
7699               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7700         (gnus-data-set-header
7701          (gnus-data-find (cdr gnus-article-current))
7702          header)
7703         (gnus-summary-update-article-line
7704          (cdr gnus-article-current) header)
7705         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
7706             (gnus-summary-update-secondary-mark
7707              (cdr gnus-article-current))))))
7708    ((not arg)
7709     ;; Select the article the normal way.
7710     (gnus-summary-select-article nil 'force))
7711    (t
7712     ;; We have to require this here to make sure that the following
7713     ;; dynamic binding isn't shadowed by autoloading.
7714     (require 'gnus-async)
7715     (require 'gnus-art)
7716     ;; Bind the article treatment functions to nil.
7717     (let ((gnus-have-all-headers t)
7718           gnus-article-prepare-hook
7719           gnus-article-decode-hook
7720           gnus-display-mime-function
7721           gnus-break-pages)
7722       ;; Destroy any MIME parts.
7723       (when (gnus-buffer-live-p gnus-article-buffer)
7724         (save-excursion
7725           (set-buffer gnus-article-buffer)
7726           (mm-destroy-parts gnus-article-mime-handles)
7727           ;; Set it to nil for safety reason.
7728           (setq gnus-article-mime-handle-alist nil)
7729           (setq gnus-article-mime-handles nil)))
7730       (gnus-summary-select-article nil 'force))))
7731   (gnus-summary-goto-subject gnus-current-article)
7732   (gnus-summary-position-point))
7733
7734 (defun gnus-summary-show-raw-article ()
7735   "Show the raw article without any article massaging functions being run."
7736   (interactive)
7737   (gnus-summary-show-article t))
7738
7739 (defun gnus-summary-verbose-headers (&optional arg)
7740   "Toggle permanent full header display.
7741 If ARG is a positive number, turn header display on.
7742 If ARG is a negative number, turn header display off."
7743   (interactive "P")
7744   (setq gnus-show-all-headers
7745         (cond ((or (not (numberp arg))
7746                    (zerop arg))
7747                (not gnus-show-all-headers))
7748               ((natnump arg)
7749                t)))
7750   (gnus-summary-show-article))
7751
7752 (defun gnus-summary-toggle-header (&optional arg)
7753   "Show the headers if they are hidden, or hide them if they are shown.
7754 If ARG is a positive number, show the entire header.
7755 If ARG is a negative number, hide the unwanted header lines."
7756   (interactive "P")
7757   (save-excursion
7758     (set-buffer gnus-article-buffer)
7759     (save-restriction
7760       (let* ((buffer-read-only nil)
7761              (inhibit-point-motion-hooks t)
7762              hidden e)
7763         (setq hidden
7764               (if (numberp arg)
7765                   (>= arg 0)
7766                 (save-restriction
7767                   (article-narrow-to-head)
7768                   (gnus-article-hidden-text-p 'headers))))
7769         (goto-char (point-min))
7770         (when (search-forward "\n\n" nil t)
7771           (delete-region (point-min) (1- (point))))
7772         (goto-char (point-min))
7773         (save-excursion
7774           (set-buffer gnus-original-article-buffer)
7775           (goto-char (point-min))
7776           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7777         (insert-buffer-substring gnus-original-article-buffer 1 e)
7778         (save-restriction
7779           (narrow-to-region (point-min) (point))
7780           (article-decode-encoded-words)
7781           (if  hidden
7782               (let ((gnus-treat-hide-headers nil)
7783                     (gnus-treat-hide-boring-headers nil))
7784                 (setq gnus-article-wash-types
7785                       (delq 'headers gnus-article-wash-types))
7786                 (gnus-treat-article 'head))
7787             (gnus-treat-article 'head)))
7788         (gnus-set-mode-line 'article)))))
7789
7790 (defun gnus-summary-show-all-headers ()
7791   "Make all header lines visible."
7792   (interactive)
7793   (gnus-summary-toggle-header 1))
7794
7795 (defun gnus-summary-caesar-message (&optional arg)
7796   "Caesar rotate the current article by 13.
7797 The numerical prefix specifies how many places to rotate each letter
7798 forward."
7799   (interactive "P")
7800   (gnus-summary-select-article)
7801   (let ((mail-header-separator ""))
7802     (gnus-eval-in-buffer-window gnus-article-buffer
7803       (save-restriction
7804         (widen)
7805         (let ((start (window-start))
7806               buffer-read-only)
7807           (message-caesar-buffer-body arg)
7808           (set-window-start (get-buffer-window (current-buffer)) start))))))
7809
7810 (defun gnus-summary-stop-page-breaking ()
7811   "Stop page breaking in the current article."
7812   (interactive)
7813   (gnus-summary-select-article)
7814   (gnus-eval-in-buffer-window gnus-article-buffer
7815     (widen)
7816     (when (gnus-visual-p 'page-marker)
7817       (let ((buffer-read-only nil))
7818         (gnus-remove-text-with-property 'gnus-prev)
7819         (gnus-remove-text-with-property 'gnus-next))
7820       (setq gnus-page-broken nil))))
7821
7822 (defun gnus-summary-move-article (&optional n to-newsgroup
7823                                             select-method action)
7824   "Move the current article to a different newsgroup.
7825 If N is a positive number, move the N next articles.
7826 If N is a negative number, move the N previous articles.
7827 If N is nil and any articles have been marked with the process mark,
7828 move those articles instead.
7829 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7830 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7831 re-spool using this method.
7832
7833 For this function to work, both the current newsgroup and the
7834 newsgroup that you want to move to have to support the `request-move'
7835 and `request-accept' functions.
7836
7837 ACTION can be either `move' (the default), `crosspost' or `copy'."
7838   (interactive "P")
7839   (unless action
7840     (setq action 'move))
7841   ;; Disable marking as read.
7842   (let (gnus-mark-article-hook)
7843     (save-window-excursion
7844       (gnus-summary-select-article)))
7845   ;; Check whether the source group supports the required functions.
7846   (cond ((and (eq action 'move)
7847               (not (gnus-check-backend-function
7848                     'request-move-article gnus-newsgroup-name)))
7849          (error "The current group does not support article moving"))
7850         ((and (eq action 'crosspost)
7851               (not (gnus-check-backend-function
7852                     'request-replace-article gnus-newsgroup-name)))
7853          (error "The current group does not support article editing")))
7854   (let ((articles (gnus-summary-work-articles n))
7855         (prefix (if (gnus-check-backend-function
7856                      'request-move-article gnus-newsgroup-name)
7857                     (gnus-group-real-prefix gnus-newsgroup-name)
7858                   ""))
7859         (names '((move "Move" "Moving")
7860                  (copy "Copy" "Copying")
7861                  (crosspost "Crosspost" "Crossposting")))
7862         (copy-buf (save-excursion
7863                     (nnheader-set-temp-buffer " *copy article*")))
7864         art-group to-method new-xref article to-groups)
7865     (unless (assq action names)
7866       (error "Unknown action %s" action))
7867     ;; Read the newsgroup name.
7868     (when (and (not to-newsgroup)
7869                (not select-method))
7870       (setq to-newsgroup
7871             (gnus-read-move-group-name
7872              (cadr (assq action names))
7873              (symbol-value (intern (format "gnus-current-%s-group" action)))
7874              articles prefix))
7875       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7876     (setq to-method (or select-method
7877                         (gnus-server-to-method
7878                          (gnus-group-method to-newsgroup))))
7879     ;; Check the method we are to move this article to...
7880     (unless (gnus-check-backend-function
7881              'request-accept-article (car to-method))
7882       (error "%s does not support article copying" (car to-method)))
7883     (unless (gnus-check-server to-method)
7884       (error "Can't open server %s" (car to-method)))
7885     (gnus-message 6 "%s to %s: %s..."
7886                   (caddr (assq action names))
7887                   (or (car select-method) to-newsgroup) articles)
7888     (while articles
7889       (setq article (pop articles))
7890       (setq
7891        art-group
7892        (cond
7893         ;; Move the article.
7894         ((eq action 'move)
7895          ;; Remove this article from future suppression.
7896          (gnus-dup-unsuppress-article article)
7897          (gnus-request-move-article
7898           article                       ; Article to move
7899           gnus-newsgroup-name           ; From newsgroup
7900           (nth 1 (gnus-find-method-for-group
7901                   gnus-newsgroup-name)) ; Server
7902           (list 'gnus-request-accept-article
7903                 to-newsgroup (list 'quote select-method)
7904                 (not articles) t)       ; Accept form
7905           (not articles)))              ; Only save nov last time
7906         ;; Copy the article.
7907         ((eq action 'copy)
7908          (save-excursion
7909            (set-buffer copy-buf)
7910            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7911              (gnus-request-accept-article
7912               to-newsgroup select-method (not articles) t))))
7913         ;; Crosspost the article.
7914         ((eq action 'crosspost)
7915          (let ((xref (message-tokenize-header
7916                       (mail-header-xref (gnus-summary-article-header article))
7917                       " ")))
7918            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7919                                   ":" article))
7920            (unless xref
7921              (setq xref (list (system-name))))
7922            (setq new-xref
7923                  (concat
7924                   (mapconcat 'identity
7925                              (delete "Xref:" (delete new-xref xref))
7926                              " ")
7927                   " " new-xref))
7928            (save-excursion
7929              (set-buffer copy-buf)
7930              ;; First put the article in the destination group.
7931              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7932              (when (consp (setq art-group
7933                                 (gnus-request-accept-article
7934                                  to-newsgroup select-method (not articles))))
7935                (setq new-xref (concat new-xref " " (car art-group)
7936                                       ":" (cdr art-group)))
7937                ;; Now we have the new Xrefs header, so we insert
7938                ;; it and replace the new article.
7939                (nnheader-replace-header "Xref" new-xref)
7940                (gnus-request-replace-article
7941                 (cdr art-group) to-newsgroup (current-buffer))
7942                art-group))))))
7943       (cond
7944        ((not art-group)
7945         (gnus-message 1 "Couldn't %s article %s: %s"
7946                       (cadr (assq action names)) article
7947                       (nnheader-get-report (car to-method))))
7948        ((eq art-group 'junk)
7949         (when (eq action 'move)
7950           (gnus-summary-mark-article article gnus-canceled-mark)
7951           (gnus-message 4 "Deleted article %s" article)))
7952        (t
7953         (let* ((pto-group (gnus-group-prefixed-name
7954                            (car art-group) to-method))
7955                (entry
7956                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7957                (info (nth 2 entry))
7958                (to-group (gnus-info-group info))
7959                to-marks)
7960           ;; Update the group that has been moved to.
7961           (when (and info
7962                      (memq action '(move copy)))
7963             (unless (member to-group to-groups)
7964               (push to-group to-groups))
7965
7966             (unless (memq article gnus-newsgroup-unreads)
7967               (push 'read to-marks)
7968               (gnus-info-set-read
7969                info (gnus-add-to-range (gnus-info-read info)
7970                                        (list (cdr art-group)))))
7971
7972             ;; See whether the article is to be put in the cache.
7973             (let ((marks gnus-article-mark-lists)
7974                   (to-article (cdr art-group)))
7975
7976               ;; Enter the article into the cache in the new group,
7977               ;; if that is required.
7978               (when gnus-use-cache
7979                 (gnus-cache-possibly-enter-article
7980                  to-group to-article
7981                  (memq article gnus-newsgroup-marked)
7982                  (memq article gnus-newsgroup-dormant)
7983                  (memq article gnus-newsgroup-unreads)))
7984
7985               (when gnus-preserve-marks
7986                 ;; Copy any marks over to the new group.
7987                 (when (and (equal to-group gnus-newsgroup-name)
7988                            (not (memq article gnus-newsgroup-unreads)))
7989                   ;; Mark this article as read in this group.
7990                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7991                   (setcdr (gnus-active to-group) to-article)
7992                   (setcdr gnus-newsgroup-active to-article))
7993
7994                 (while marks
7995                   (when (memq article (symbol-value
7996                                        (intern (format "gnus-newsgroup-%s"
7997                                                        (caar marks)))))
7998                     (push (cdar marks) to-marks)
7999                     ;; If the other group is the same as this group,
8000                     ;; then we have to add the mark to the list.
8001                     (when (equal to-group gnus-newsgroup-name)
8002                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8003                            (cons to-article
8004                                  (symbol-value
8005                                   (intern (format "gnus-newsgroup-%s"
8006                                                   (caar marks)))))))
8007                     ;; Copy the marks to other group.
8008                     (gnus-add-marked-articles
8009                      to-group (cdar marks) (list to-article) info))
8010                   (setq marks (cdr marks)))
8011
8012                 (gnus-request-set-mark to-group (list (list (list to-article)
8013                                                             'set
8014                                                             to-marks))))
8015
8016               (gnus-dribble-enter
8017                (concat "(gnus-group-set-info '"
8018                        (gnus-prin1-to-string (gnus-get-info to-group))
8019                        ")"))))
8020
8021           ;; Update the Xref header in this article to point to
8022           ;; the new crossposted article we have just created.
8023           (when (eq action 'crosspost)
8024             (save-excursion
8025               (set-buffer copy-buf)
8026               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8027               (nnheader-replace-header "Xref" new-xref)
8028               (gnus-request-replace-article
8029                article gnus-newsgroup-name (current-buffer)))))
8030
8031         ;;;!!!Why is this necessary?
8032         (set-buffer gnus-summary-buffer)
8033
8034         (gnus-summary-goto-subject article)
8035         (when (eq action 'move)
8036           (gnus-summary-mark-article article gnus-canceled-mark))))
8037       (gnus-summary-remove-process-mark article))
8038     ;; Re-activate all groups that have been moved to.
8039     (while to-groups
8040       (save-excursion
8041         (set-buffer gnus-group-buffer)
8042         (when (gnus-group-goto-group (car to-groups) t)
8043           (gnus-group-get-new-news-this-group 1 t))
8044         (pop to-groups)))
8045
8046     (gnus-kill-buffer copy-buf)
8047     (gnus-summary-position-point)
8048     (gnus-set-mode-line 'summary)))
8049
8050 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8051   "Move the current article to a different newsgroup.
8052 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8053 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8054 re-spool using this method."
8055   (interactive "P")
8056   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8057
8058 (defun gnus-summary-crosspost-article (&optional n)
8059   "Crosspost the current article to some other group."
8060   (interactive "P")
8061   (gnus-summary-move-article n nil nil 'crosspost))
8062
8063 (defcustom gnus-summary-respool-default-method nil
8064   "Default method for respooling an article.
8065 If nil, use to the current newsgroup method."
8066   :type '(choice (gnus-select-method :value (nnml ""))
8067                  (const nil))
8068   :group 'gnus-summary-mail)
8069
8070 (defun gnus-summary-respool-article (&optional n method)
8071   "Respool the current article.
8072 The article will be squeezed through the mail spooling process again,
8073 which means that it will be put in some mail newsgroup or other
8074 depending on `nnmail-split-methods'.
8075 If N is a positive number, respool the N next articles.
8076 If N is a negative number, respool the N previous articles.
8077 If N is nil and any articles have been marked with the process mark,
8078 respool those articles instead.
8079
8080 Respooling can be done both from mail groups and \"real\" newsgroups.
8081 In the former case, the articles in question will be moved from the
8082 current group into whatever groups they are destined to.  In the
8083 latter case, they will be copied into the relevant groups."
8084   (interactive
8085    (list current-prefix-arg
8086          (let* ((methods (gnus-methods-using 'respool))
8087                 (methname
8088                  (symbol-name (or gnus-summary-respool-default-method
8089                                   (car (gnus-find-method-for-group
8090                                         gnus-newsgroup-name)))))
8091                 (method
8092                  (gnus-completing-read
8093                   methname "What backend do you want to use when respooling?"
8094                   methods nil t nil 'gnus-mail-method-history))
8095                 ms)
8096            (cond
8097             ((zerop (length (setq ms (gnus-servers-using-backend
8098                                       (intern method)))))
8099              (list (intern method) ""))
8100             ((= 1 (length ms))
8101              (car ms))
8102             (t
8103              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8104                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8105                            ms-alist))))))))
8106   (unless method
8107     (error "No method given for respooling"))
8108   (if (assoc (symbol-name
8109               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8110              (gnus-methods-using 'respool))
8111       (gnus-summary-move-article n nil method)
8112     (gnus-summary-copy-article n nil method)))
8113
8114 (defun gnus-summary-import-article (file &optional edit)
8115   "Import an arbitrary file into a mail newsgroup."
8116   (interactive "fImport file: \nP")
8117   (let ((group gnus-newsgroup-name)
8118         (now (current-time))
8119         atts lines group-art)
8120     (unless (gnus-check-backend-function 'request-accept-article group)
8121       (error "%s does not support article importing" group))
8122     (or (file-readable-p file)
8123         (not (file-regular-p file))
8124         (error "Can't read %s" file))
8125     (save-excursion
8126       (set-buffer (gnus-get-buffer-create " *import file*"))
8127       (erase-buffer)
8128       (nnheader-insert-file-contents file)
8129       (goto-char (point-min))
8130       (if (nnheader-article-p)
8131           (save-restriction
8132             (goto-char (point-min))
8133             (search-forward "\n\n" nil t)
8134             (narrow-to-region (point-min) (1- (point)))
8135             (goto-char (point-min))
8136             (unless (re-search-forward "^date:" nil t)
8137               (goto-char (point-max))
8138               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8139        ;; This doesn't look like an article, so we fudge some headers.
8140         (setq atts (file-attributes file)
8141               lines (count-lines (point-min) (point-max)))
8142         (insert "From: " (read-string "From: ") "\n"
8143                 "Subject: " (read-string "Subject: ") "\n"
8144                 "Date: " (message-make-date (nth 5 atts)) "\n"
8145                 "Message-ID: " (message-make-message-id) "\n"
8146                 "Lines: " (int-to-string lines) "\n"
8147                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8148       (setq group-art (gnus-request-accept-article group nil t))
8149       (kill-buffer (current-buffer)))
8150     (setq gnus-newsgroup-active (gnus-activate-group group))
8151     (forward-line 1)
8152     (gnus-summary-goto-article (cdr group-art) nil t)
8153     (when edit
8154       (gnus-summary-edit-article))))
8155
8156 (defun gnus-summary-create-article ()
8157   "Create an article in a mail newsgroup."
8158   (interactive)
8159   (let ((group gnus-newsgroup-name)
8160         (now (current-time))
8161         group-art)
8162     (unless (gnus-check-backend-function 'request-accept-article group)
8163       (error "%s does not support article importing" group))
8164     (save-excursion
8165       (set-buffer (gnus-get-buffer-create " *import file*"))
8166       (erase-buffer)
8167       (goto-char (point-min))
8168       ;; This doesn't look like an article, so we fudge some headers.
8169       (insert "From: " (read-string "From: ") "\n"
8170               "Subject: " (read-string "Subject: ") "\n"
8171               "Date: " (message-make-date now) "\n"
8172               "Message-ID: " (message-make-message-id) "\n")
8173       (setq group-art (gnus-request-accept-article group nil t))
8174       (kill-buffer (current-buffer)))
8175     (setq gnus-newsgroup-active (gnus-activate-group group))
8176     (forward-line 1)
8177     (gnus-summary-goto-article (cdr group-art) nil t)
8178     (gnus-summary-edit-article)))
8179
8180 (defun gnus-summary-article-posted-p ()
8181   "Say whether the current (mail) article is available from news as well.
8182 This will be the case if the article has both been mailed and posted."
8183   (interactive)
8184   (let ((id (mail-header-references (gnus-summary-article-header)))
8185         (gnus-override-method (car (gnus-refer-article-methods))))
8186     (if (gnus-request-head id "")
8187         (gnus-message 2 "The current message was found on %s"
8188                       gnus-override-method)
8189       (gnus-message 2 "The current message couldn't be found on %s"
8190                     gnus-override-method)
8191       nil)))
8192
8193 (defun gnus-summary-expire-articles (&optional now)
8194   "Expire all articles that are marked as expirable in the current group."
8195   (interactive)
8196   (when (gnus-check-backend-function
8197          'request-expire-articles gnus-newsgroup-name)
8198     ;; This backend supports expiry.
8199     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8200            (expirable (if total
8201                           (progn
8202                             ;; We need to update the info for
8203                         ;; this group for `gnus-list-of-read-articles'
8204                             ;; to give us the right answer.
8205                             (gnus-run-hooks 'gnus-exit-group-hook)
8206                             (gnus-summary-update-info)
8207                             (gnus-list-of-read-articles gnus-newsgroup-name))
8208                         (setq gnus-newsgroup-expirable
8209                               (sort gnus-newsgroup-expirable '<))))
8210            (expiry-wait (if now 'immediate
8211                           (gnus-group-find-parameter
8212                            gnus-newsgroup-name 'expiry-wait)))
8213            (nnmail-expiry-target
8214             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8215                 nnmail-expiry-target))
8216            es)
8217       (when expirable
8218         ;; There are expirable articles in this group, so we run them
8219         ;; through the expiry process.
8220         (gnus-message 6 "Expiring articles...")
8221         (unless (gnus-check-group gnus-newsgroup-name)
8222           (error "Can't open server for %s" gnus-newsgroup-name))
8223         ;; The list of articles that weren't expired is returned.
8224         (save-excursion
8225           (if expiry-wait
8226               (let ((nnmail-expiry-wait-function nil)
8227                     (nnmail-expiry-wait expiry-wait))
8228                 (setq es (gnus-request-expire-articles
8229                           expirable gnus-newsgroup-name)))
8230             (setq es (gnus-request-expire-articles
8231                       expirable gnus-newsgroup-name)))
8232           (unless total
8233             (setq gnus-newsgroup-expirable es))
8234           ;; We go through the old list of expirable, and mark all
8235           ;; really expired articles as nonexistent.
8236           (unless (eq es expirable) ;If nothing was expired, we don't mark.
8237             (let ((gnus-use-cache nil))
8238               (while expirable
8239                 (unless (memq (car expirable) es)
8240                   (when (gnus-data-find (car expirable))
8241                     (gnus-summary-mark-article
8242                      (car expirable) gnus-canceled-mark)))
8243                 (setq expirable (cdr expirable))))))
8244         (gnus-message 6 "Expiring articles...done")))))
8245
8246 (defun gnus-summary-expire-articles-now ()
8247   "Expunge all expirable articles in the current group.
8248 This means that *all* articles that are marked as expirable will be
8249 deleted forever, right now."
8250   (interactive)
8251   (or gnus-expert-user
8252       (gnus-yes-or-no-p
8253        "Are you really, really, really sure you want to delete all these messages? ")
8254       (error "Phew!"))
8255   (gnus-summary-expire-articles t))
8256
8257 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8258 (defun gnus-summary-delete-article (&optional n)
8259   "Delete the N next (mail) articles.
8260 This command actually deletes articles.  This is not a marking
8261 command.  The article will disappear forever from your life, never to
8262 return.
8263 If N is negative, delete backwards.
8264 If N is nil and articles have been marked with the process mark,
8265 delete these instead."
8266   (interactive "P")
8267   (unless (gnus-check-backend-function 'request-expire-articles
8268                                        gnus-newsgroup-name)
8269     (error "The current newsgroup does not support article deletion"))
8270   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8271     (error "Couldn't open server"))
8272   ;; Compute the list of articles to delete.
8273   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8274         not-deleted)
8275     (if (and gnus-novice-user
8276              (not (gnus-yes-or-no-p
8277                    (format "Do you really want to delete %s forever? "
8278                            (if (> (length articles) 1)
8279                                (format "these %s articles" (length articles))
8280                              "this article")))))
8281         ()
8282       ;; Delete the articles.
8283       (setq not-deleted (gnus-request-expire-articles
8284                          articles gnus-newsgroup-name 'force))
8285       (while articles
8286         (gnus-summary-remove-process-mark (car articles))
8287         ;; The backend might not have been able to delete the article
8288         ;; after all.
8289         (unless (memq (car articles) not-deleted)
8290           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8291         (setq articles (cdr articles)))
8292       (when not-deleted
8293         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8294     (gnus-summary-position-point)
8295     (gnus-set-mode-line 'summary)
8296     not-deleted))
8297
8298 (defun gnus-summary-edit-article (&optional arg)
8299   "Edit the current article.
8300 This will have permanent effect only in mail groups.
8301 If ARG is nil, edit the decoded articles.
8302 If ARG is 1, edit the raw articles.
8303 If ARG is 2, edit the raw articles even in read-only groups.
8304 If ARG is 3, edit the articles with the current handles.
8305 Otherwise, allow editing of articles even in read-only
8306 groups."
8307   (interactive "P")
8308   (let (force raw current-handles)
8309     (cond
8310      ((null arg))
8311      ((eq arg 1) (setq raw t))
8312      ((eq arg 2) (setq raw t
8313                        force t))
8314      ((eq arg 3) (setq current-handles
8315                        (and (gnus-buffer-live-p gnus-article-buffer)
8316                             (with-current-buffer gnus-article-buffer
8317                               (prog1
8318                                   gnus-article-mime-handles
8319                                 (setq gnus-article-mime-handles nil))))))
8320      (t (setq force t)))
8321     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8322         (error "Can't edit the raw article in group nndraft:drafts"))
8323     (save-excursion
8324       (set-buffer gnus-summary-buffer)
8325       (let ((mail-parse-charset gnus-newsgroup-charset)
8326             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8327         (gnus-set-global-variables)
8328         (when (and (not force)
8329                    (gnus-group-read-only-p))
8330           (error "The current newsgroup does not support article editing"))
8331         (gnus-summary-show-article t)
8332         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8333           (with-current-buffer gnus-article-buffer
8334             (mm-enable-multibyte-mule4)))
8335         (if (equal gnus-newsgroup-name "nndraft:drafts")
8336             (setq raw t))
8337         (gnus-article-edit-article
8338          (if raw 'ignore
8339            `(lambda ()
8340               (let ((mbl mml-buffer-list))
8341                 (setq mml-buffer-list nil)
8342                 (mime-to-mml ,'current-handles)
8343                 (let ((mbl1 mml-buffer-list))
8344                   (setq mml-buffer-list mbl)
8345                   (set (make-local-variable 'mml-buffer-list) mbl1))
8346                 (make-local-hook 'kill-buffer-hook)
8347                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8348          `(lambda (no-highlight)
8349             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8350                   (message-options message-options)
8351                   (message-options-set-recipient)
8352                   (mail-parse-ignored-charsets
8353                    ',gnus-newsgroup-ignored-charsets))
8354               ,(if (not raw) '(progn
8355                                 (mml-to-mime)
8356                                 (mml-destroy-buffers)
8357                                 (remove-hook 'kill-buffer-hook
8358                                              'mml-destroy-buffers t)
8359                                 (kill-local-variable 'mml-buffer-list)))
8360               (gnus-summary-edit-article-done
8361                ,(or (mail-header-references gnus-current-headers) "")
8362                ,(gnus-group-read-only-p)
8363                ,gnus-summary-buffer no-highlight))))))))
8364
8365 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8366
8367 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8368                                                  no-highlight)
8369   "Make edits to the current article permanent."
8370   (interactive)
8371   (save-excursion
8372    ;; The buffer restriction contains the entire article if it exists.
8373     (when (article-goto-body)
8374       (let ((lines (count-lines (point) (point-max)))
8375             (length (- (point-max) (point)))
8376             (case-fold-search t)
8377             (body (copy-marker (point))))
8378         (goto-char (point-min))
8379         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8380           (delete-region (match-beginning 1) (match-end 1))
8381           (insert (number-to-string length)))
8382         (goto-char (point-min))
8383         (when (re-search-forward
8384                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8385           (delete-region (match-beginning 1) (match-end 1))
8386           (insert (number-to-string length)))
8387         (goto-char (point-min))
8388         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8389           (delete-region (match-beginning 1) (match-end 1))
8390           (insert (number-to-string lines))))))
8391   ;; Replace the article.
8392   (let ((buf (current-buffer)))
8393     (with-temp-buffer
8394       (insert-buffer-substring buf)
8395
8396       (if (and (not read-only)
8397                (not (gnus-request-replace-article
8398                      (cdr gnus-article-current) (car gnus-article-current)
8399                      (current-buffer) t)))
8400           (error "Couldn't replace article")
8401         ;; Update the summary buffer.
8402         (if (and references
8403                  (equal (message-tokenize-header references " ")
8404                         (message-tokenize-header
8405                          (or (message-fetch-field "references") "") " ")))
8406             ;; We only have to update this line.
8407             (save-excursion
8408               (save-restriction
8409                 (message-narrow-to-head)
8410                 (let ((head (buffer-string))
8411                       header)
8412                   (with-temp-buffer
8413                     (insert (format "211 %d Article retrieved.\n"
8414                                     (cdr gnus-article-current)))
8415                     (insert head)
8416                     (insert ".\n")
8417                     (let ((nntp-server-buffer (current-buffer)))
8418                       (setq header (car (gnus-get-newsgroup-headers
8419                                          (save-excursion
8420                                            (set-buffer gnus-summary-buffer)
8421                                            gnus-newsgroup-dependencies)
8422                                          t))))
8423                     (save-excursion
8424                       (set-buffer gnus-summary-buffer)
8425                       (gnus-data-set-header
8426                        (gnus-data-find (cdr gnus-article-current))
8427                        header)
8428                       (gnus-summary-update-article-line
8429                        (cdr gnus-article-current) header)
8430                       (if (gnus-summary-goto-subject
8431                            (cdr gnus-article-current) nil t)
8432                           (gnus-summary-update-secondary-mark
8433                            (cdr gnus-article-current))))))))
8434           ;; Update threads.
8435           (set-buffer (or buffer gnus-summary-buffer))
8436           (gnus-summary-update-article (cdr gnus-article-current))
8437           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8438               (gnus-summary-update-secondary-mark
8439                (cdr gnus-article-current))))
8440         ;; Prettify the article buffer again.
8441         (unless no-highlight
8442           (save-excursion
8443             (set-buffer gnus-article-buffer)
8444             ;;;!!! Fix this -- article should be rehighlighted.
8445             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8446             (set-buffer gnus-original-article-buffer)
8447             (gnus-request-article
8448              (cdr gnus-article-current)
8449              (car gnus-article-current) (current-buffer))))
8450         ;; Prettify the summary buffer line.
8451         (when (gnus-visual-p 'summary-highlight 'highlight)
8452           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8453
8454 (defun gnus-summary-edit-wash (key)
8455   "Perform editing command KEY in the article buffer."
8456   (interactive
8457    (list
8458     (progn
8459       (message "%s" (concat (this-command-keys) "- "))
8460       (read-char))))
8461   (message "")
8462   (gnus-summary-edit-article)
8463   (execute-kbd-macro (concat (this-command-keys) key))
8464   (gnus-article-edit-done))
8465
8466 ;;; Respooling
8467
8468 (defun gnus-summary-respool-query (&optional silent trace)
8469   "Query where the respool algorithm would put this article."
8470   (interactive)
8471   (let (gnus-mark-article-hook)
8472     (gnus-summary-select-article)
8473     (save-excursion
8474       (set-buffer gnus-original-article-buffer)
8475       (save-restriction
8476         (message-narrow-to-head)
8477         (let ((groups (nnmail-article-group 'identity trace)))
8478           (unless silent
8479             (if groups
8480                 (message "This message would go to %s"
8481                          (mapconcat 'car groups ", "))
8482               (message "This message would go to no groups"))
8483             groups))))))
8484
8485 (defun gnus-summary-respool-trace ()
8486   "Trace where the respool algorithm would put this article.
8487 Display a buffer showing all fancy splitting patterns which matched."
8488   (interactive)
8489   (gnus-summary-respool-query nil t))
8490
8491 ;; Summary marking commands.
8492
8493 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8494   "Mark articles which has the same subject as read, and then select the next.
8495 If UNMARK is positive, remove any kind of mark.
8496 If UNMARK is negative, tick articles."
8497   (interactive "P")
8498   (when unmark
8499     (setq unmark (prefix-numeric-value unmark)))
8500   (let ((count
8501          (gnus-summary-mark-same-subject
8502           (gnus-summary-article-subject) unmark)))
8503     ;; Select next unread article.  If auto-select-same mode, should
8504     ;; select the first unread article.
8505     (gnus-summary-next-article t (and gnus-auto-select-same
8506                                       (gnus-summary-article-subject)))
8507     (gnus-message 7 "%d article%s marked as %s"
8508                   count (if (= count 1) " is" "s are")
8509                   (if unmark "unread" "read"))))
8510
8511 (defun gnus-summary-kill-same-subject (&optional unmark)
8512   "Mark articles which has the same subject as read.
8513 If UNMARK is positive, remove any kind of mark.
8514 If UNMARK is negative, tick articles."
8515   (interactive "P")
8516   (when unmark
8517     (setq unmark (prefix-numeric-value unmark)))
8518   (let ((count
8519          (gnus-summary-mark-same-subject
8520           (gnus-summary-article-subject) unmark)))
8521     ;; If marked as read, go to next unread subject.
8522     (when (null unmark)
8523       ;; Go to next unread subject.
8524       (gnus-summary-next-subject 1 t))
8525     (gnus-message 7 "%d articles are marked as %s"
8526                   count (if unmark "unread" "read"))))
8527
8528 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8529   "Mark articles with same SUBJECT as read, and return marked number.
8530 If optional argument UNMARK is positive, remove any kinds of marks.
8531 If optional argument UNMARK is negative, mark articles as unread instead."
8532   (let ((count 1))
8533     (save-excursion
8534       (cond
8535        ((null unmark)                   ; Mark as read.
8536         (while (and
8537                 (progn
8538                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8539                   (gnus-summary-show-thread) t)
8540                 (gnus-summary-find-subject subject))
8541           (setq count (1+ count))))
8542        ((> unmark 0)                    ; Tick.
8543         (while (and
8544                 (progn
8545                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8546                   (gnus-summary-show-thread) t)
8547                 (gnus-summary-find-subject subject))
8548           (setq count (1+ count))))
8549        (t                               ; Mark as unread.
8550         (while (and
8551                 (progn
8552                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8553                   (gnus-summary-show-thread) t)
8554                 (gnus-summary-find-subject subject))
8555           (setq count (1+ count)))))
8556       (gnus-set-mode-line 'summary)
8557       ;; Return the number of marked articles.
8558       count)))
8559
8560 (defun gnus-summary-mark-as-processable (n &optional unmark)
8561   "Set the process mark on the next N articles.
8562 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8563 the process mark instead.  The difference between N and the actual
8564 number of articles marked is returned."
8565   (interactive "P")
8566   (if (and (null n) (gnus-region-active-p))
8567       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8568     (setq n (prefix-numeric-value n))
8569     (let ((backward (< n 0))
8570           (n (abs n)))
8571       (while (and
8572               (> n 0)
8573               (if unmark
8574                   (gnus-summary-remove-process-mark
8575                    (gnus-summary-article-number))
8576                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8577               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8578         (setq n (1- n)))
8579       (when (/= 0 n)
8580         (gnus-message 7 "No more articles"))
8581       (gnus-summary-recenter)
8582       (gnus-summary-position-point)
8583       n)))
8584
8585 (defun gnus-summary-unmark-as-processable (n)
8586   "Remove the process mark from the next N articles.
8587 If N is negative, unmark backward instead.  The difference between N and
8588 the actual number of articles unmarked is returned."
8589   (interactive "P")
8590   (gnus-summary-mark-as-processable n t))
8591
8592 (defun gnus-summary-unmark-all-processable ()
8593   "Remove the process mark from all articles."
8594   (interactive)
8595   (save-excursion
8596     (while gnus-newsgroup-processable
8597       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8598   (gnus-summary-position-point))
8599
8600 (defun gnus-summary-add-mark (article type)
8601   "Mark ARTICLE with a mark of TYPE."
8602   (let ((vtype (car (assq type gnus-article-mark-lists)))
8603         var)
8604     (if (not vtype)
8605         (error "No such mark type: %s" type)
8606       (setq var (intern (format "gnus-newsgroup-%s" type)))
8607       (set var (cons article (symbol-value var)))
8608       (if (memq type '(processable cached replied forwarded recent saved))
8609           (gnus-summary-update-secondary-mark article)
8610         ;;; !!! This is bogus.  We should find out what primary
8611         ;;; !!! mark we want to set.
8612         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8613
8614 (defun gnus-summary-mark-as-expirable (n)
8615   "Mark N articles forward as expirable.
8616 If N is negative, mark backward instead.  The difference between N and
8617 the actual number of articles marked is returned."
8618   (interactive "p")
8619   (gnus-summary-mark-forward n gnus-expirable-mark))
8620
8621 (defun gnus-summary-mark-article-as-replied (article)
8622   "Mark ARTICLE as replied to and update the summary line.
8623 ARTICLE can also be a list of articles."
8624   (interactive (list (gnus-summary-article-number)))
8625   (let ((articles (if (listp article) article (list article))))
8626     (dolist (article articles)
8627       (push article gnus-newsgroup-replied)
8628       (let ((buffer-read-only nil))
8629         (when (gnus-summary-goto-subject article nil t)
8630           (gnus-summary-update-secondary-mark article))))))
8631
8632 (defun gnus-summary-mark-article-as-forwarded (article)
8633   "Mark ARTICLE as forwarded and update the summary line.
8634 ARTICLE can also be a list of articles."
8635   (let ((articles (if (listp article) article (list article))))
8636     (dolist (article articles)
8637       (push article gnus-newsgroup-forwarded)
8638       (let ((buffer-read-only nil))
8639         (when (gnus-summary-goto-subject article nil t)
8640           (gnus-summary-update-secondary-mark article))))))
8641
8642 (defun gnus-summary-set-bookmark (article)
8643   "Set a bookmark in current article."
8644   (interactive (list (gnus-summary-article-number)))
8645   (when (or (not (get-buffer gnus-article-buffer))
8646             (not gnus-current-article)
8647             (not gnus-article-current)
8648             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8649     (error "No current article selected"))
8650   ;; Remove old bookmark, if one exists.
8651   (let ((old (assq article gnus-newsgroup-bookmarks)))
8652     (when old
8653       (setq gnus-newsgroup-bookmarks
8654             (delq old gnus-newsgroup-bookmarks))))
8655   ;; Set the new bookmark, which is on the form
8656   ;; (article-number . line-number-in-body).
8657   (push
8658    (cons article
8659          (save-excursion
8660            (set-buffer gnus-article-buffer)
8661            (count-lines
8662             (min (point)
8663                  (save-excursion
8664                    (goto-char (point-min))
8665                    (search-forward "\n\n" nil t)
8666                    (point)))
8667             (point))))
8668    gnus-newsgroup-bookmarks)
8669   (gnus-message 6 "A bookmark has been added to the current article."))
8670
8671 (defun gnus-summary-remove-bookmark (article)
8672   "Remove the bookmark from the current article."
8673   (interactive (list (gnus-summary-article-number)))
8674   ;; Remove old bookmark, if one exists.
8675   (let ((old (assq article gnus-newsgroup-bookmarks)))
8676     (if old
8677         (progn
8678           (setq gnus-newsgroup-bookmarks
8679                 (delq old gnus-newsgroup-bookmarks))
8680           (gnus-message 6 "Removed bookmark."))
8681       (gnus-message 6 "No bookmark in current article."))))
8682
8683 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8684 (defun gnus-summary-mark-as-dormant (n)
8685   "Mark N articles forward as dormant.
8686 If N is negative, mark backward instead.  The difference between N and
8687 the actual number of articles marked is returned."
8688   (interactive "p")
8689   (gnus-summary-mark-forward n gnus-dormant-mark))
8690
8691 (defun gnus-summary-set-process-mark (article)
8692   "Set the process mark on ARTICLE and update the summary line."
8693   (setq gnus-newsgroup-processable
8694         (cons article
8695               (delq article gnus-newsgroup-processable)))
8696   (when (gnus-summary-goto-subject article)
8697     (gnus-summary-show-thread)
8698     (gnus-summary-goto-subject article)
8699     (gnus-summary-update-secondary-mark article)))
8700
8701 (defun gnus-summary-remove-process-mark (article)
8702   "Remove the process mark from ARTICLE and update the summary line."
8703   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8704   (when (gnus-summary-goto-subject article)
8705     (gnus-summary-show-thread)
8706     (gnus-summary-goto-subject article)
8707     (gnus-summary-update-secondary-mark article)))
8708
8709 (defun gnus-summary-set-saved-mark (article)
8710   "Set the process mark on ARTICLE and update the summary line."
8711   (push article gnus-newsgroup-saved)
8712   (when (gnus-summary-goto-subject article)
8713     (gnus-summary-update-secondary-mark article)))
8714
8715 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8716   "Mark N articles as read forwards.
8717 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8718 The difference between N and the actual number of articles marked is
8719 returned.
8720 Iff NO-EXPIRE, auto-expiry will be inhibited."
8721   (interactive "p")
8722   (gnus-summary-show-thread)
8723   (let ((backward (< n 0))
8724         (gnus-summary-goto-unread
8725          (and gnus-summary-goto-unread
8726               (not (eq gnus-summary-goto-unread 'never))
8727               (not (memq mark (list gnus-unread-mark
8728                                     gnus-ticked-mark gnus-dormant-mark)))))
8729         (n (abs n))
8730         (mark (or mark gnus-del-mark)))
8731     (while (and (> n 0)
8732                 (gnus-summary-mark-article nil mark no-expire)
8733                 (zerop (gnus-summary-next-subject
8734                         (if backward -1 1)
8735                         (and gnus-summary-goto-unread
8736                              (not (eq gnus-summary-goto-unread 'never)))
8737                         t)))
8738       (setq n (1- n)))
8739     (when (/= 0 n)
8740       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8741     (gnus-summary-recenter)
8742     (gnus-summary-position-point)
8743     (gnus-set-mode-line 'summary)
8744     n))
8745
8746 (defun gnus-summary-mark-article-as-read (mark)
8747   "Mark the current article quickly as read with MARK."
8748   (let ((article (gnus-summary-article-number)))
8749     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8750     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8751     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8752     (push (cons article mark) gnus-newsgroup-reads)
8753     ;; Possibly remove from cache, if that is used.
8754     (when gnus-use-cache
8755       (gnus-cache-enter-remove-article article))
8756     ;; Allow the backend to change the mark.
8757     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8758     ;; Check for auto-expiry.
8759     (when (and gnus-newsgroup-auto-expire
8760                (memq mark gnus-auto-expirable-marks))
8761       (setq mark gnus-expirable-mark)
8762       ;; Let the backend know about the mark change.
8763       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8764       (push article gnus-newsgroup-expirable))
8765     ;; Set the mark in the buffer.
8766     (gnus-summary-update-mark mark 'unread)
8767     t))
8768
8769 (defun gnus-summary-mark-article-as-unread (mark)
8770   "Mark the current article quickly as unread with MARK."
8771   (let* ((article (gnus-summary-article-number))
8772          (old-mark (gnus-summary-article-mark article)))
8773     ;; Allow the backend to change the mark.
8774     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8775     (if (eq mark old-mark)
8776         t
8777       (if (<= article 0)
8778           (progn
8779             (gnus-error 1 "Can't mark negative article numbers")
8780             nil)
8781         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8782         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8783         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8784         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8785         (cond ((= mark gnus-ticked-mark)
8786                (push article gnus-newsgroup-marked))
8787               ((= mark gnus-dormant-mark)
8788                (push article gnus-newsgroup-dormant))
8789               (t
8790                (push article gnus-newsgroup-unreads)))
8791         (gnus-pull article gnus-newsgroup-reads)
8792
8793         ;; See whether the article is to be put in the cache.
8794         (and gnus-use-cache
8795              (vectorp (gnus-summary-article-header article))
8796              (save-excursion
8797                (gnus-cache-possibly-enter-article
8798                 gnus-newsgroup-name article
8799                 (= mark gnus-ticked-mark)
8800                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8801
8802         ;; Fix the mark.
8803         (gnus-summary-update-mark mark 'unread)
8804         t))))
8805
8806 (defun gnus-summary-mark-article (&optional article mark no-expire)
8807   "Mark ARTICLE with MARK.  MARK can be any character.
8808 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8809 `??' (dormant) and `?E' (expirable).
8810 If MARK is nil, then the default character `?r' is used.
8811 If ARTICLE is nil, then the article on the current line will be
8812 marked.
8813 Iff NO-EXPIRE, auto-expiry will be inhibited."
8814   ;; The mark might be a string.
8815   (when (stringp mark)
8816     (setq mark (aref mark 0)))
8817   ;; If no mark is given, then we check auto-expiring.
8818   (when (null mark)
8819     (setq mark gnus-del-mark))
8820   (when (and (not no-expire)
8821              gnus-newsgroup-auto-expire
8822              (memq mark gnus-auto-expirable-marks))
8823     (setq mark gnus-expirable-mark))
8824   (let ((article (or article (gnus-summary-article-number)))
8825         (old-mark (gnus-summary-article-mark article)))
8826     ;; Allow the backend to change the mark.
8827     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8828     (if (eq mark old-mark)
8829         t
8830       (unless article
8831         (error "No article on current line"))
8832       (if (not (if (or (= mark gnus-unread-mark)
8833                        (= mark gnus-ticked-mark)
8834                        (= mark gnus-dormant-mark))
8835                    (gnus-mark-article-as-unread article mark)
8836                  (gnus-mark-article-as-read article mark)))
8837           t
8838         ;; See whether the article is to be put in the cache.
8839         (and gnus-use-cache
8840              (not (= mark gnus-canceled-mark))
8841              (vectorp (gnus-summary-article-header article))
8842              (save-excursion
8843                (gnus-cache-possibly-enter-article
8844                 gnus-newsgroup-name article
8845                 (= mark gnus-ticked-mark)
8846                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8847
8848         (when (gnus-summary-goto-subject article nil t)
8849           (let ((buffer-read-only nil))
8850             (gnus-summary-show-thread)
8851             ;; Fix the mark.
8852             (gnus-summary-update-mark mark 'unread)
8853             t))))))
8854
8855 (defun gnus-summary-update-secondary-mark (article)
8856   "Update the secondary (read, process, cache) mark."
8857   (gnus-summary-update-mark
8858    (cond ((memq article gnus-newsgroup-processable)
8859           gnus-process-mark)
8860          ((memq article gnus-newsgroup-cached)
8861           gnus-cached-mark)
8862          ((memq article gnus-newsgroup-replied)
8863           gnus-replied-mark)
8864          ((memq article gnus-newsgroup-forwarded)
8865           gnus-forwarded-mark)
8866          ((memq article gnus-newsgroup-saved)
8867           gnus-saved-mark)
8868          ((memq article gnus-newsgroup-recent)
8869           gnus-recent-mark)
8870          (t gnus-no-mark))
8871    'replied)
8872   (when (gnus-visual-p 'summary-highlight 'highlight)
8873     (gnus-run-hooks 'gnus-summary-update-hook))
8874   t)
8875
8876 (defun gnus-summary-update-mark (mark type)
8877   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8878         (buffer-read-only nil))
8879     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8880     (when forward
8881       (when (looking-at "\r")
8882         (incf forward))
8883       (when (<= (+ forward (point)) (point-max))
8884         ;; Go to the right position on the line.
8885         (goto-char (+ forward (point)))
8886         ;; Replace the old mark with the new mark.
8887         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8888         ;; Optionally update the marks by some user rule.
8889         (when (eq type 'unread)
8890           (gnus-data-set-mark
8891            (gnus-data-find (gnus-summary-article-number)) mark)
8892           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8893
8894 (defun gnus-mark-article-as-read (article &optional mark)
8895   "Enter ARTICLE in the pertinent lists and remove it from others."
8896   ;; Make the article expirable.
8897   (let ((mark (or mark gnus-del-mark)))
8898     (if (= mark gnus-expirable-mark)
8899         (push article gnus-newsgroup-expirable)
8900       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8901     ;; Remove from unread and marked lists.
8902     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8903     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8904     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8905     (push (cons article mark) gnus-newsgroup-reads)
8906     ;; Possibly remove from cache, if that is used.
8907     (when gnus-use-cache
8908       (gnus-cache-enter-remove-article article))
8909     t))
8910
8911 (defun gnus-mark-article-as-unread (article &optional mark)
8912   "Enter ARTICLE in the pertinent lists and remove it from others."
8913   (let ((mark (or mark gnus-ticked-mark)))
8914     (if (<= article 0)
8915         (progn
8916           (gnus-error 1 "Can't mark negative article numbers")
8917           nil)
8918       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8919             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8920             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8921             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8922
8923       ;; Unsuppress duplicates?
8924       (when gnus-suppress-duplicates
8925         (gnus-dup-unsuppress-article article))
8926
8927       (cond ((= mark gnus-ticked-mark)
8928              (push article gnus-newsgroup-marked))
8929             ((= mark gnus-dormant-mark)
8930              (push article gnus-newsgroup-dormant))
8931             (t
8932              (push article gnus-newsgroup-unreads)))
8933       (gnus-pull article gnus-newsgroup-reads)
8934       t)))
8935
8936 (defalias 'gnus-summary-mark-as-unread-forward
8937   'gnus-summary-tick-article-forward)
8938 (make-obsolete 'gnus-summary-mark-as-unread-forward
8939                'gnus-summary-tick-article-forward)
8940 (defun gnus-summary-tick-article-forward (n)
8941   "Tick N articles forwards.
8942 If N is negative, tick backwards instead.
8943 The difference between N and the number of articles ticked is returned."
8944   (interactive "p")
8945   (gnus-summary-mark-forward n gnus-ticked-mark))
8946
8947 (defalias 'gnus-summary-mark-as-unread-backward
8948   'gnus-summary-tick-article-backward)
8949 (make-obsolete 'gnus-summary-mark-as-unread-backward
8950                'gnus-summary-tick-article-backward)
8951 (defun gnus-summary-tick-article-backward (n)
8952   "Tick N articles backwards.
8953 The difference between N and the number of articles ticked is returned."
8954   (interactive "p")
8955   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8956
8957 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8958 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8959 (defun gnus-summary-tick-article (&optional article clear-mark)
8960   "Mark current article as unread.
8961 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8962 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8963   (interactive)
8964   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8965                                        gnus-ticked-mark)))
8966
8967 (defun gnus-summary-mark-as-read-forward (n)
8968   "Mark N articles as read forwards.
8969 If N is negative, mark backwards instead.
8970 The difference between N and the actual number of articles marked is
8971 returned."
8972   (interactive "p")
8973   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8974
8975 (defun gnus-summary-mark-as-read-backward (n)
8976   "Mark the N articles as read backwards.
8977 The difference between N and the actual number of articles marked is
8978 returned."
8979   (interactive "p")
8980   (gnus-summary-mark-forward
8981    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8982
8983 (defun gnus-summary-mark-as-read (&optional article mark)
8984   "Mark current article as read.
8985 ARTICLE specifies the article to be marked as read.
8986 MARK specifies a string to be inserted at the beginning of the line."
8987   (gnus-summary-mark-article article mark))
8988
8989 (defun gnus-summary-clear-mark-forward (n)
8990   "Clear marks from N articles forward.
8991 If N is negative, clear backward instead.
8992 The difference between N and the number of marks cleared is returned."
8993   (interactive "p")
8994   (gnus-summary-mark-forward n gnus-unread-mark))
8995
8996 (defun gnus-summary-clear-mark-backward (n)
8997   "Clear marks from N articles backward.
8998 The difference between N and the number of marks cleared is returned."
8999   (interactive "p")
9000   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9001
9002 (defun gnus-summary-mark-unread-as-read ()
9003   "Intended to be used by `gnus-summary-mark-article-hook'."
9004   (when (memq gnus-current-article gnus-newsgroup-unreads)
9005     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9006
9007 (defun gnus-summary-mark-read-and-unread-as-read ()
9008   "Intended to be used by `gnus-summary-mark-article-hook'."
9009   (let ((mark (gnus-summary-article-mark)))
9010     (when (or (gnus-unread-mark-p mark)
9011               (gnus-read-mark-p mark))
9012       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9013
9014 (defun gnus-summary-mark-unread-as-ticked ()
9015   "Intended to be used by `gnus-summary-mark-article-hook'."
9016   (when (memq gnus-current-article gnus-newsgroup-unreads)
9017     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9018
9019 (defun gnus-summary-mark-region-as-read (point mark all)
9020   "Mark all unread articles between point and mark as read.
9021 If given a prefix, mark all articles between point and mark as read,
9022 even ticked and dormant ones."
9023   (interactive "r\nP")
9024   (save-excursion
9025     (let (article)
9026       (goto-char point)
9027       (beginning-of-line)
9028       (while (and
9029               (< (point) mark)
9030               (progn
9031                 (when (or all
9032                           (memq (setq article (gnus-summary-article-number))
9033                                 gnus-newsgroup-unreads))
9034                   (gnus-summary-mark-article article gnus-del-mark))
9035                 t)
9036               (gnus-summary-find-next))))))
9037
9038 (defun gnus-summary-mark-below (score mark)
9039   "Mark articles with score less than SCORE with MARK."
9040   (interactive "P\ncMark: ")
9041   (setq score (if score
9042                   (prefix-numeric-value score)
9043                 (or gnus-summary-default-score 0)))
9044   (save-excursion
9045     (set-buffer gnus-summary-buffer)
9046     (goto-char (point-min))
9047     (while
9048         (progn
9049           (and (< (gnus-summary-article-score) score)
9050                (gnus-summary-mark-article nil mark))
9051           (gnus-summary-find-next)))))
9052
9053 (defun gnus-summary-kill-below (&optional score)
9054   "Mark articles with score below SCORE as read."
9055   (interactive "P")
9056   (gnus-summary-mark-below score gnus-killed-mark))
9057
9058 (defun gnus-summary-clear-above (&optional score)
9059   "Clear all marks from articles with score above SCORE."
9060   (interactive "P")
9061   (gnus-summary-mark-above score gnus-unread-mark))
9062
9063 (defun gnus-summary-tick-above (&optional score)
9064   "Tick all articles with score above SCORE."
9065   (interactive "P")
9066   (gnus-summary-mark-above score gnus-ticked-mark))
9067
9068 (defun gnus-summary-mark-above (score mark)
9069   "Mark articles with score over SCORE with MARK."
9070   (interactive "P\ncMark: ")
9071   (setq score (if score
9072                   (prefix-numeric-value score)
9073                 (or gnus-summary-default-score 0)))
9074   (save-excursion
9075     (set-buffer gnus-summary-buffer)
9076     (goto-char (point-min))
9077     (while (and (progn
9078                   (when (> (gnus-summary-article-score) score)
9079                     (gnus-summary-mark-article nil mark))
9080                   t)
9081                 (gnus-summary-find-next)))))
9082
9083 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9084 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9085 (defun gnus-summary-limit-include-expunged (&optional no-error)
9086   "Display all the hidden articles that were expunged for low scores."
9087   (interactive)
9088   (let ((buffer-read-only nil))
9089     (let ((scored gnus-newsgroup-scored)
9090           headers h)
9091       (while scored
9092         (unless (gnus-summary-article-header (caar scored))
9093           (and (setq h (gnus-number-to-header (caar scored)))
9094                (< (cdar scored) gnus-summary-expunge-below)
9095                (push h headers)))
9096         (setq scored (cdr scored)))
9097       (if (not headers)
9098           (when (not no-error)
9099             (error "No expunged articles hidden"))
9100         (goto-char (point-min))
9101         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9102         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9103         (mapcar (lambda (x) (push (mail-header-number x)
9104                                   gnus-newsgroup-limit))
9105                 headers)
9106         (gnus-summary-prepare-unthreaded (nreverse headers))
9107         (goto-char (point-min))
9108         (gnus-summary-position-point)
9109         t))))
9110
9111 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9112   "Mark all unread articles in this newsgroup as read.
9113 If prefix argument ALL is non-nil, ticked and dormant articles will
9114 also be marked as read.
9115 If QUIETLY is non-nil, no questions will be asked.
9116 If TO-HERE is non-nil, it should be a point in the buffer.  All
9117 articles before (after, if REVERSE is set) this point will be marked as read.
9118 Note that this function will only catch up the unread article
9119 in the current summary buffer limitation.
9120 The number of articles marked as read is returned."
9121   (interactive "P")
9122   (prog1
9123       (save-excursion
9124         (when (or quietly
9125                   (not gnus-interactive-catchup) ;Without confirmation?
9126                   gnus-expert-user
9127                   (gnus-y-or-n-p
9128                    (if all
9129                        "Mark absolutely all articles as read? "
9130                      "Mark all unread articles as read? ")))
9131           (if (and not-mark
9132                    (not gnus-newsgroup-adaptive)
9133                    (not gnus-newsgroup-auto-expire)
9134                    (not gnus-suppress-duplicates)
9135                    (or (not gnus-use-cache)
9136                        (eq gnus-use-cache 'passive)))
9137               (progn
9138                 (when all
9139                   (setq gnus-newsgroup-marked nil
9140                         gnus-newsgroup-dormant nil))
9141                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9142             ;; We actually mark all articles as canceled, which we
9143             ;; have to do when using auto-expiry or adaptive scoring.
9144             (gnus-summary-show-all-threads)
9145             (if (and to-here reverse)
9146                 (progn
9147                   (goto-char to-here)
9148                   (while (and
9149                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9150                           (gnus-summary-find-next (not all) nil nil t))))
9151               (when (gnus-summary-first-subject (not all) t)
9152                 (while (and
9153                         (if to-here (< (point) to-here) t)
9154                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9155                         (gnus-summary-find-next (not all) nil nil t)))))
9156             (gnus-set-mode-line 'summary))
9157           t))
9158     (gnus-summary-position-point)))
9159
9160 (defun gnus-summary-catchup-to-here (&optional all)
9161   "Mark all unticked articles before the current one as read.
9162 If ALL is non-nil, also mark ticked and dormant articles as read."
9163   (interactive "P")
9164   (save-excursion
9165     (gnus-save-hidden-threads
9166       (let ((beg (point)))
9167         ;; We check that there are unread articles.
9168         (when (or all (gnus-summary-find-prev))
9169           (gnus-summary-catchup all t beg)))))
9170   (gnus-summary-position-point))
9171
9172 (defun gnus-summary-catchup-from-here (&optional all)
9173   "Mark all unticked articles after the current one as read.
9174 If ALL is non-nil, also mark ticked and dormant articles as read."
9175   (interactive "P")
9176   (save-excursion
9177     (gnus-save-hidden-threads
9178       (let ((beg (point)))
9179         ;; We check that there are unread articles.
9180         (when (or all (gnus-summary-find-next))
9181           (gnus-summary-catchup all t beg nil t)))))
9182
9183   (gnus-summary-position-point))
9184 (defun gnus-summary-catchup-all (&optional quietly)
9185   "Mark all articles in this newsgroup as read."
9186   (interactive "P")
9187   (gnus-summary-catchup t quietly))
9188
9189 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9190   "Mark all unread articles in this group as read, then exit.
9191 If prefix argument ALL is non-nil, all articles are marked as read.
9192 If QUIETLY is non-nil, no questions will be asked."
9193   (interactive "P")
9194   (when (gnus-summary-catchup all quietly nil 'fast)
9195     ;; Select next newsgroup or exit.
9196     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9197              (eq gnus-auto-select-next 'quietly))
9198         (gnus-summary-next-group nil)
9199       (gnus-summary-exit))))
9200
9201 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9202   "Mark all articles in this newsgroup as read, and then exit."
9203   (interactive "P")
9204   (gnus-summary-catchup-and-exit t quietly))
9205
9206 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9207   "Mark all articles in this group as read and select the next group.
9208 If given a prefix, mark all articles, unread as well as ticked, as
9209 read."
9210   (interactive "P")
9211   (save-excursion
9212     (gnus-summary-catchup all))
9213   (gnus-summary-next-group))
9214
9215 ;;;
9216 ;;; with article
9217 ;;;
9218
9219 (defmacro gnus-with-article (article &rest forms)
9220   "Select ARTICLE and perform FORMS in the original article buffer.
9221 Then replace the article with the result."
9222   `(progn
9223      ;; We don't want the article to be marked as read.
9224      (let (gnus-mark-article-hook)
9225        (gnus-summary-select-article t t nil ,article))
9226      (set-buffer gnus-original-article-buffer)
9227      ,@forms
9228      (if (not (gnus-check-backend-function
9229                'request-replace-article (car gnus-article-current)))
9230          (gnus-message 5 "Read-only group; not replacing")
9231        (unless (gnus-request-replace-article
9232                 ,article (car gnus-article-current)
9233                 (current-buffer) t)
9234          (error "Couldn't replace article")))
9235      ;; The cache and backlog have to be flushed somewhat.
9236      (when gnus-keep-backlog
9237        (gnus-backlog-remove-article
9238         (car gnus-article-current) (cdr gnus-article-current)))
9239      (when gnus-use-cache
9240        (gnus-cache-update-article
9241         (car gnus-article-current) (cdr gnus-article-current)))))
9242
9243 (put 'gnus-with-article 'lisp-indent-function 1)
9244 (put 'gnus-with-article 'edebug-form-spec '(form body))
9245
9246 ;; Thread-based commands.
9247
9248 (defun gnus-summary-articles-in-thread (&optional article)
9249   "Return a list of all articles in the current thread.
9250 If ARTICLE is non-nil, return all articles in the thread that starts
9251 with that article."
9252   (let* ((article (or article (gnus-summary-article-number)))
9253          (data (gnus-data-find-list article))
9254          (top-level (gnus-data-level (car data)))
9255          (top-subject
9256           (cond ((null gnus-thread-operation-ignore-subject)
9257                  (gnus-simplify-subject-re
9258                   (mail-header-subject (gnus-data-header (car data)))))
9259                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9260                  (gnus-simplify-subject-fuzzy
9261                   (mail-header-subject (gnus-data-header (car data)))))
9262                 (t nil)))
9263          (end-point (save-excursion
9264                       (if (gnus-summary-go-to-next-thread)
9265                           (point) (point-max))))
9266          articles)
9267     (while (and data
9268                 (< (gnus-data-pos (car data)) end-point))
9269       (when (or (not top-subject)
9270                 (string= top-subject
9271                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9272                              (gnus-simplify-subject-fuzzy
9273                               (mail-header-subject
9274                                (gnus-data-header (car data))))
9275                            (gnus-simplify-subject-re
9276                             (mail-header-subject
9277                              (gnus-data-header (car data)))))))
9278         (push (gnus-data-number (car data)) articles))
9279       (unless (and (setq data (cdr data))
9280                    (> (gnus-data-level (car data)) top-level))
9281         (setq data nil)))
9282     ;; Return the list of articles.
9283     (nreverse articles)))
9284
9285 (defun gnus-summary-rethread-current ()
9286   "Rethread the thread the current article is part of."
9287   (interactive)
9288   (let* ((gnus-show-threads t)
9289          (article (gnus-summary-article-number))
9290          (id (mail-header-id (gnus-summary-article-header)))
9291          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9292     (unless id
9293       (error "No article on the current line"))
9294     (gnus-rebuild-thread id)
9295     (gnus-summary-goto-subject article)))
9296
9297 (defun gnus-summary-reparent-thread ()
9298   "Make the current article child of the marked (or previous) article.
9299
9300 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9301 is non-nil or the Subject: of both articles are the same."
9302   (interactive)
9303   (unless (not (gnus-group-read-only-p))
9304     (error "The current newsgroup does not support article editing"))
9305   (unless (<= (length gnus-newsgroup-processable) 1)
9306     (error "No more than one article may be marked"))
9307   (save-window-excursion
9308     (let ((gnus-article-buffer " *reparent*")
9309           (current-article (gnus-summary-article-number))
9310           ;; First grab the marked article, otherwise one line up.
9311           (parent-article (if (not (null gnus-newsgroup-processable))
9312                               (car gnus-newsgroup-processable)
9313                             (save-excursion
9314                               (if (eq (forward-line -1) 0)
9315                                   (gnus-summary-article-number)
9316                                 (error "Beginning of summary buffer"))))))
9317       (unless (not (eq current-article parent-article))
9318         (error "An article may not be self-referential"))
9319       (let ((message-id (mail-header-id
9320                          (gnus-summary-article-header parent-article))))
9321         (unless (and message-id (not (equal message-id "")))
9322           (error "No message-id in desired parent"))
9323         (gnus-with-article current-article
9324           (save-restriction
9325             (goto-char (point-min))
9326             (message-narrow-to-head)
9327             (if (re-search-forward "^References: " nil t)
9328                 (progn
9329                   (re-search-forward "^[^ \t]" nil t)
9330                   (forward-line -1)
9331                   (end-of-line)
9332                   (insert " " message-id))
9333               (insert "References: " message-id "\n"))))
9334         (set-buffer gnus-summary-buffer)
9335         (gnus-summary-unmark-all-processable)
9336         (gnus-summary-update-article current-article)
9337         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9338             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9339         (gnus-summary-rethread-current)
9340         (gnus-message 3 "Article %d is now the child of article %d"
9341                       current-article parent-article)))))
9342
9343 (defun gnus-summary-toggle-threads (&optional arg)
9344   "Toggle showing conversation threads.
9345 If ARG is positive number, turn showing conversation threads on."
9346   (interactive "P")
9347   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9348     (setq gnus-show-threads
9349           (if (null arg) (not gnus-show-threads)
9350             (> (prefix-numeric-value arg) 0)))
9351     (gnus-summary-prepare)
9352     (gnus-summary-goto-subject current)
9353     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9354     (gnus-summary-position-point)))
9355
9356 (defun gnus-summary-show-all-threads ()
9357   "Show all threads."
9358   (interactive)
9359   (save-excursion
9360     (let ((buffer-read-only nil))
9361       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9362   (gnus-summary-position-point))
9363
9364 (defun gnus-summary-show-thread ()
9365   "Show thread subtrees.
9366 Returns nil if no thread was there to be shown."
9367   (interactive)
9368   (let ((buffer-read-only nil)
9369         (orig (point))
9370         ;; first goto end then to beg, to have point at beg after let
9371         (end (progn (end-of-line) (point)))
9372         (beg (progn (beginning-of-line) (point))))
9373     (prog1
9374         ;; Any hidden lines here?
9375         (search-forward "\r" end t)
9376       (subst-char-in-region beg end ?\^M ?\n t)
9377       (goto-char orig)
9378       (gnus-summary-position-point))))
9379
9380 (defun gnus-summary-hide-all-threads ()
9381   "Hide all thread subtrees."
9382   (interactive)
9383   (save-excursion
9384     (goto-char (point-min))
9385     (gnus-summary-hide-thread)
9386     (while (zerop (gnus-summary-next-thread 1 t))
9387       (gnus-summary-hide-thread)))
9388   (gnus-summary-position-point))
9389
9390 (defun gnus-summary-hide-thread ()
9391   "Hide thread subtrees.
9392 Returns nil if no threads were there to be hidden."
9393   (interactive)
9394   (let ((buffer-read-only nil)
9395         (start (point))
9396         (article (gnus-summary-article-number)))
9397     (goto-char start)
9398     ;; Go forward until either the buffer ends or the subthread
9399     ;; ends.
9400     (when (and (not (eobp))
9401                (or (zerop (gnus-summary-next-thread 1 t))
9402                    (goto-char (point-max))))
9403       (prog1
9404           (if (and (> (point) start)
9405                    (search-backward "\n" start t))
9406               (progn
9407                 (subst-char-in-region start (point) ?\n ?\^M)
9408                 (gnus-summary-goto-subject article))
9409             (goto-char start)
9410             nil)))))
9411
9412 (defun gnus-summary-go-to-next-thread (&optional previous)
9413   "Go to the same level (or less) next thread.
9414 If PREVIOUS is non-nil, go to previous thread instead.
9415 Return the article number moved to, or nil if moving was impossible."
9416   (let ((level (gnus-summary-thread-level))
9417         (way (if previous -1 1))
9418         (beg (point)))
9419     (forward-line way)
9420     (while (and (not (eobp))
9421                 (< level (gnus-summary-thread-level)))
9422       (forward-line way))
9423     (if (eobp)
9424         (progn
9425           (goto-char beg)
9426           nil)
9427       (setq beg (point))
9428       (prog1
9429           (gnus-summary-article-number)
9430         (goto-char beg)))))
9431
9432 (defun gnus-summary-next-thread (n &optional silent)
9433   "Go to the same level next N'th thread.
9434 If N is negative, search backward instead.
9435 Returns the difference between N and the number of skips actually
9436 done.
9437
9438 If SILENT, don't output messages."
9439   (interactive "p")
9440   (let ((backward (< n 0))
9441         (n (abs n)))
9442     (while (and (> n 0)
9443                 (gnus-summary-go-to-next-thread backward))
9444       (decf n))
9445     (unless silent
9446       (gnus-summary-position-point))
9447     (when (and (not silent) (/= 0 n))
9448       (gnus-message 7 "No more threads"))
9449     n))
9450
9451 (defun gnus-summary-prev-thread (n)
9452   "Go to the same level previous N'th thread.
9453 Returns the difference between N and the number of skips actually
9454 done."
9455   (interactive "p")
9456   (gnus-summary-next-thread (- n)))
9457
9458 (defun gnus-summary-go-down-thread ()
9459   "Go down one level in the current thread."
9460   (let ((children (gnus-summary-article-children)))
9461     (when children
9462       (gnus-summary-goto-subject (car children)))))
9463
9464 (defun gnus-summary-go-up-thread ()
9465   "Go up one level in the current thread."
9466   (let ((parent (gnus-summary-article-parent)))
9467     (when parent
9468       (gnus-summary-goto-subject parent))))
9469
9470 (defun gnus-summary-down-thread (n)
9471   "Go down thread N steps.
9472 If N is negative, go up instead.
9473 Returns the difference between N and how many steps down that were
9474 taken."
9475   (interactive "p")
9476   (let ((up (< n 0))
9477         (n (abs n)))
9478     (while (and (> n 0)
9479                 (if up (gnus-summary-go-up-thread)
9480                   (gnus-summary-go-down-thread)))
9481       (setq n (1- n)))
9482     (gnus-summary-position-point)
9483     (when (/= 0 n)
9484       (gnus-message 7 "Can't go further"))
9485     n))
9486
9487 (defun gnus-summary-up-thread (n)
9488   "Go up thread N steps.
9489 If N is negative, go down instead.
9490 Returns the difference between N and how many steps down that were
9491 taken."
9492   (interactive "p")
9493   (gnus-summary-down-thread (- n)))
9494
9495 (defun gnus-summary-top-thread ()
9496   "Go to the top of the thread."
9497   (interactive)
9498   (while (gnus-summary-go-up-thread))
9499   (gnus-summary-article-number))
9500
9501 (defun gnus-summary-kill-thread (&optional unmark)
9502   "Mark articles under current thread as read.
9503 If the prefix argument is positive, remove any kinds of marks.
9504 If the prefix argument is negative, tick articles instead."
9505   (interactive "P")
9506   (when unmark
9507     (setq unmark (prefix-numeric-value unmark)))
9508   (let ((articles (gnus-summary-articles-in-thread)))
9509     (save-excursion
9510       ;; Expand the thread.
9511       (gnus-summary-show-thread)
9512       ;; Mark all the articles.
9513       (while articles
9514         (gnus-summary-goto-subject (car articles))
9515         (cond ((null unmark)
9516                (gnus-summary-mark-article-as-read gnus-killed-mark))
9517               ((> unmark 0)
9518                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9519               (t
9520                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9521         (setq articles (cdr articles))))
9522     ;; Hide killed subtrees.
9523     (and (null unmark)
9524          gnus-thread-hide-killed
9525          (gnus-summary-hide-thread))
9526     ;; If marked as read, go to next unread subject.
9527     (when (null unmark)
9528       ;; Go to next unread subject.
9529       (gnus-summary-next-subject 1 t)))
9530   (gnus-set-mode-line 'summary))
9531
9532 ;; Summary sorting commands
9533
9534 (defun gnus-summary-sort-by-number (&optional reverse)
9535   "Sort the summary buffer by article number.
9536 Argument REVERSE means reverse order."
9537   (interactive "P")
9538   (gnus-summary-sort 'number reverse))
9539
9540 (defun gnus-summary-sort-by-author (&optional reverse)
9541   "Sort the summary buffer by author name alphabetically.
9542 If `case-fold-search' is non-nil, case of letters is ignored.
9543 Argument REVERSE means reverse order."
9544   (interactive "P")
9545   (gnus-summary-sort 'author reverse))
9546
9547 (defun gnus-summary-sort-by-subject (&optional reverse)
9548   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9549 If `case-fold-search' is non-nil, case of letters is ignored.
9550 Argument REVERSE means reverse order."
9551   (interactive "P")
9552   (gnus-summary-sort 'subject reverse))
9553
9554 (defun gnus-summary-sort-by-date (&optional reverse)
9555   "Sort the summary buffer by date.
9556 Argument REVERSE means reverse order."
9557   (interactive "P")
9558   (gnus-summary-sort 'date reverse))
9559
9560 (defun gnus-summary-sort-by-score (&optional reverse)
9561   "Sort the summary buffer by score.
9562 Argument REVERSE means reverse order."
9563   (interactive "P")
9564   (gnus-summary-sort 'score reverse))
9565
9566 (defun gnus-summary-sort-by-lines (&optional reverse)
9567   "Sort the summary buffer by the number of lines.
9568 Argument REVERSE means reverse order."
9569   (interactive "P")
9570   (gnus-summary-sort 'lines reverse))
9571
9572 (defun gnus-summary-sort-by-chars (&optional reverse)
9573   "Sort the summary buffer by article length.
9574 Argument REVERSE means reverse order."
9575   (interactive "P")
9576   (gnus-summary-sort 'chars reverse))
9577
9578 (defun gnus-summary-sort-by-original (&optional reverse)
9579   "Sort the summary buffer using the default sorting method.
9580 Argument REVERSE means reverse order."
9581   (interactive "P")
9582   (let* ((buffer-read-only)
9583          (gnus-summary-prepare-hook nil))
9584     ;; We do the sorting by regenerating the threads.
9585     (gnus-summary-prepare)
9586     ;; Hide subthreads if needed.
9587     (when (and gnus-show-threads gnus-thread-hide-subtree)
9588       (gnus-summary-hide-all-threads))))
9589
9590 (defun gnus-summary-sort (predicate reverse)
9591   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9592   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9593          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9594          (gnus-thread-sort-functions
9595           (if (not reverse)
9596               thread
9597             `(lambda (t1 t2)
9598                (,thread t2 t1))))
9599          (gnus-sort-gathered-threads-function
9600           gnus-thread-sort-functions)
9601          (gnus-article-sort-functions
9602           (if (not reverse)
9603               article
9604             `(lambda (t1 t2)
9605                (,article t2 t1))))
9606          (buffer-read-only)
9607          (gnus-summary-prepare-hook nil))
9608     ;; We do the sorting by regenerating the threads.
9609     (gnus-summary-prepare)
9610     ;; Hide subthreads if needed.
9611     (when (and gnus-show-threads gnus-thread-hide-subtree)
9612       (gnus-summary-hide-all-threads))))
9613
9614 ;; Summary saving commands.
9615
9616 (defun gnus-summary-save-article (&optional n not-saved)
9617   "Save the current article using the default saver function.
9618 If N is a positive number, save the N next articles.
9619 If N is a negative number, save the N previous articles.
9620 If N is nil and any articles have been marked with the process mark,
9621 save those articles instead.
9622 The variable `gnus-default-article-saver' specifies the saver function."
9623   (interactive "P")
9624   (let* ((articles (gnus-summary-work-articles n))
9625          (save-buffer (save-excursion
9626                         (nnheader-set-temp-buffer " *Gnus Save*")))
9627          (num (length articles))
9628          header file)
9629     (dolist (article articles)
9630       (setq header (gnus-summary-article-header article))
9631       (if (not (vectorp header))
9632           ;; This is a pseudo-article.
9633           (if (assq 'name header)
9634               (gnus-copy-file (cdr (assq 'name header)))
9635             (gnus-message 1 "Article %d is unsaveable" article))
9636         ;; This is a real article.
9637         (save-window-excursion
9638           (gnus-summary-select-article t nil nil article))
9639         (save-excursion
9640           (set-buffer save-buffer)
9641           (erase-buffer)
9642           (insert-buffer-substring gnus-original-article-buffer))
9643         (setq file (gnus-article-save save-buffer file num))
9644         (gnus-summary-remove-process-mark article)
9645         (unless not-saved
9646           (gnus-summary-set-saved-mark article))))
9647     (gnus-kill-buffer save-buffer)
9648     (gnus-summary-position-point)
9649     (gnus-set-mode-line 'summary)
9650     n))
9651
9652 (defun gnus-summary-pipe-output (&optional arg)
9653   "Pipe the current article to a subprocess.
9654 If N is a positive number, pipe the N next articles.
9655 If N is a negative number, pipe the N previous articles.
9656 If N is nil and any articles have been marked with the process mark,
9657 pipe those articles instead."
9658   (interactive "P")
9659   (require 'gnus-art)
9660   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9661     (gnus-summary-save-article arg t))
9662   (let ((buffer (get-buffer "*Shell Command Output*")))
9663     (if (and buffer
9664              (with-current-buffer buffer (> (point-max) (point-min))))
9665         (gnus-configure-windows 'pipe))))
9666
9667 (defun gnus-summary-save-article-mail (&optional arg)
9668   "Append the current article to an mail file.
9669 If N is a positive number, save the N next articles.
9670 If N is a negative number, save the N previous articles.
9671 If N is nil and any articles have been marked with the process mark,
9672 save those articles instead."
9673   (interactive "P")
9674   (require 'gnus-art)
9675   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9676     (gnus-summary-save-article arg)))
9677
9678 (defun gnus-summary-save-article-rmail (&optional arg)
9679   "Append the current article to an rmail file.
9680 If N is a positive number, save the N next articles.
9681 If N is a negative number, save the N previous articles.
9682 If N is nil and any articles have been marked with the process mark,
9683 save those articles instead."
9684   (interactive "P")
9685   (require 'gnus-art)
9686   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9687     (gnus-summary-save-article arg)))
9688
9689 (defun gnus-summary-save-article-file (&optional arg)
9690   "Append the current article to a file.
9691 If N is a positive number, save the N next articles.
9692 If N is a negative number, save the N previous articles.
9693 If N is nil and any articles have been marked with the process mark,
9694 save those articles instead."
9695   (interactive "P")
9696   (require 'gnus-art)
9697   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9698     (gnus-summary-save-article arg)))
9699
9700 (defun gnus-summary-write-article-file (&optional arg)
9701   "Write the current article to a file, deleting the previous file.
9702 If N is a positive number, save the N next articles.
9703 If N is a negative number, save the N previous articles.
9704 If N is nil and any articles have been marked with the process mark,
9705 save those articles instead."
9706   (interactive "P")
9707   (require 'gnus-art)
9708   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9709     (gnus-summary-save-article arg)))
9710
9711 (defun gnus-summary-save-article-body-file (&optional arg)
9712   "Append the current article body to a file.
9713 If N is a positive number, save the N next articles.
9714 If N is a negative number, save the N previous articles.
9715 If N is nil and any articles have been marked with the process mark,
9716 save those articles instead."
9717   (interactive "P")
9718   (require 'gnus-art)
9719   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9720     (gnus-summary-save-article arg)))
9721
9722 (defun gnus-summary-pipe-message (program)
9723   "Pipe the current article through PROGRAM."
9724   (interactive "sProgram: ")
9725   (gnus-summary-select-article)
9726   (let ((mail-header-separator ""))
9727     (gnus-eval-in-buffer-window gnus-article-buffer
9728       (save-restriction
9729         (widen)
9730         (let ((start (window-start))
9731               buffer-read-only)
9732           (message-pipe-buffer-body program)
9733           (set-window-start (get-buffer-window (current-buffer)) start))))))
9734
9735 (defun gnus-get-split-value (methods)
9736   "Return a value based on the split METHODS."
9737   (let (split-name method result match)
9738     (when methods
9739       (save-excursion
9740         (set-buffer gnus-original-article-buffer)
9741         (save-restriction
9742           (nnheader-narrow-to-headers)
9743           (while (and methods (not split-name))
9744             (goto-char (point-min))
9745             (setq method (pop methods))
9746             (setq match (car method))
9747             (when (cond
9748                    ((stringp match)
9749                     ;; Regular expression.
9750                     (ignore-errors
9751                       (re-search-forward match nil t)))
9752                    ((gnus-functionp match)
9753                     ;; Function.
9754                     (save-restriction
9755                       (widen)
9756                       (setq result (funcall match gnus-newsgroup-name))))
9757                    ((consp match)
9758                     ;; Form.
9759                     (save-restriction
9760                       (widen)
9761                       (setq result (eval match)))))
9762               (setq split-name (cdr method))
9763               (cond ((stringp result)
9764                      (push (expand-file-name
9765                             result gnus-article-save-directory)
9766                            split-name))
9767                     ((consp result)
9768                      (setq split-name (append result split-name)))))))))
9769     (nreverse split-name)))
9770
9771 (defun gnus-valid-move-group-p (group)
9772   (and (boundp group)
9773        (symbol-name group)
9774        (symbol-value group)
9775        (gnus-get-function (gnus-find-method-for-group
9776                            (symbol-name group)) 'request-accept-article t)))
9777
9778 (defun gnus-read-move-group-name (prompt default articles prefix)
9779   "Read a group name."
9780   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9781          (minibuffer-confirm-incomplete nil) ; XEmacs
9782          (prom
9783           (format "%s %s to:"
9784                   prompt
9785                   (if (> (length articles) 1)
9786                       (format "these %d articles" (length articles))
9787                     "this article")))
9788          (to-newsgroup
9789           (cond
9790            ((null split-name)
9791             (gnus-completing-read default prom
9792                                   gnus-active-hashtb
9793                                   'gnus-valid-move-group-p
9794                                   nil prefix
9795                                   'gnus-group-history))
9796            ((= 1 (length split-name))
9797             (gnus-completing-read (car split-name) prom
9798                                   gnus-active-hashtb
9799                                   'gnus-valid-move-group-p
9800                                   nil nil
9801                                   'gnus-group-history))
9802            (t
9803             (gnus-completing-read nil prom
9804                                   (mapcar (lambda (el) (list el))
9805                                           (nreverse split-name))
9806                                   nil nil nil
9807                                   'gnus-group-history))))
9808          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9809     (when to-newsgroup
9810       (if (or (string= to-newsgroup "")
9811               (string= to-newsgroup prefix))
9812           (setq to-newsgroup default))
9813       (unless to-newsgroup
9814         (error "No group name entered"))
9815       (or (gnus-active to-newsgroup)
9816           (gnus-activate-group to-newsgroup nil nil to-method)
9817           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9818                                      to-newsgroup))
9819               (or (and (gnus-request-create-group to-newsgroup to-method)
9820                        (gnus-activate-group
9821                         to-newsgroup nil nil to-method)
9822                        (gnus-subscribe-group to-newsgroup))
9823                   (error "Couldn't create group %s" to-newsgroup)))
9824           (error "No such group: %s" to-newsgroup)))
9825     to-newsgroup))
9826
9827 (defun gnus-summary-save-parts (type dir n &optional reverse)
9828   "Save parts matching TYPE to DIR.
9829 If REVERSE, save parts that do not match TYPE."
9830   (interactive
9831    (list (read-string "Save parts of type: "
9832                       (or (car gnus-summary-save-parts-type-history)
9833                           gnus-summary-save-parts-default-mime)
9834                       'gnus-summary-save-parts-type-history)
9835          (setq gnus-summary-save-parts-last-directory
9836                (read-file-name "Save to directory: "
9837                                gnus-summary-save-parts-last-directory
9838                                nil t))
9839          current-prefix-arg))
9840   (gnus-summary-iterate n
9841     (let ((gnus-display-mime-function nil)
9842           (gnus-inhibit-treatment t))
9843       (gnus-summary-select-article))
9844     (save-excursion
9845       (set-buffer gnus-article-buffer)
9846       (let ((handles (or gnus-article-mime-handles
9847                          (mm-dissect-buffer) (mm-uu-dissect))))
9848         (when handles
9849           (gnus-summary-save-parts-1 type dir handles reverse)
9850           (unless gnus-article-mime-handles ;; Don't destroy this case.
9851             (mm-destroy-parts handles)))))))
9852
9853 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9854   (if (stringp (car handle))
9855       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9856               (cdr handle))
9857     (when (if reverse
9858               (not (string-match type (mm-handle-media-type handle)))
9859             (string-match type (mm-handle-media-type handle)))
9860       (let ((file (expand-file-name
9861                    (file-name-nondirectory
9862                     (or
9863                      (mail-content-type-get
9864                       (mm-handle-disposition handle) 'filename)
9865                      (concat gnus-newsgroup-name
9866                              "." (number-to-string
9867                                   (cdr gnus-article-current)))))
9868                    dir)))
9869         (unless (file-exists-p file)
9870           (mm-save-part-to-file handle file))))))
9871
9872 ;; Summary extract commands
9873
9874 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9875   (let ((buffer-read-only nil)
9876         (article (gnus-summary-article-number))
9877         after-article b e)
9878     (unless (gnus-summary-goto-subject article)
9879       (error "No such article: %d" article))
9880     (gnus-summary-position-point)
9881     ;; If all commands are to be bunched up on one line, we collect
9882     ;; them here.
9883     (unless gnus-view-pseudos-separately
9884       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9885             files action)
9886         (while ps
9887           (setq action (cdr (assq 'action (car ps))))
9888           (setq files (list (cdr (assq 'name (car ps)))))
9889           (while (and ps (cdr ps)
9890                       (string= (or action "1")
9891                                (or (cdr (assq 'action (cadr ps))) "2")))
9892             (push (cdr (assq 'name (cadr ps))) files)
9893             (setcdr ps (cddr ps)))
9894           (when files
9895             (when (not (string-match "%s" action))
9896               (push " " files))
9897             (push " " files)
9898             (when (assq 'execute (car ps))
9899               (setcdr (assq 'execute (car ps))
9900                       (funcall (if (string-match "%s" action)
9901                                    'format 'concat)
9902                                action
9903                                (mapconcat
9904                                 (lambda (f)
9905                                   (if (equal f " ")
9906                                       f
9907                                     (mm-quote-arg f)))
9908                                 files " ")))))
9909           (setq ps (cdr ps)))))
9910     (if (and gnus-view-pseudos (not not-view))
9911         (while pslist
9912           (when (assq 'execute (car pslist))
9913             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9914                                   (eq gnus-view-pseudos 'not-confirm)))
9915           (setq pslist (cdr pslist)))
9916       (save-excursion
9917         (while pslist
9918           (setq after-article (or (cdr (assq 'article (car pslist)))
9919                                   (gnus-summary-article-number)))
9920           (gnus-summary-goto-subject after-article)
9921           (forward-line 1)
9922           (setq b (point))
9923           (insert "    " (file-name-nondirectory
9924                           (cdr (assq 'name (car pslist))))
9925                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9926           (setq e (point))
9927           (forward-line -1)             ; back to `b'
9928           (gnus-add-text-properties
9929            b (1- e) (list 'gnus-number gnus-reffed-article-number
9930                           gnus-mouse-face-prop gnus-mouse-face))
9931           (gnus-data-enter
9932            after-article gnus-reffed-article-number
9933            gnus-unread-mark b (car pslist) 0 (- e b))
9934           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9935           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9936           (setq pslist (cdr pslist)))))))
9937
9938 (defun gnus-pseudos< (p1 p2)
9939   (let ((c1 (cdr (assq 'action p1)))
9940         (c2 (cdr (assq 'action p2))))
9941     (and c1 c2 (string< c1 c2))))
9942
9943 (defun gnus-request-pseudo-article (props)
9944   (cond ((assq 'execute props)
9945          (gnus-execute-command (cdr (assq 'execute props)))))
9946   (let ((gnus-current-article (gnus-summary-article-number)))
9947     (gnus-run-hooks 'gnus-mark-article-hook)))
9948
9949 (defun gnus-execute-command (command &optional automatic)
9950   (save-excursion
9951     (gnus-article-setup-buffer)
9952     (set-buffer gnus-article-buffer)
9953     (setq buffer-read-only nil)
9954     (let ((command (if automatic command
9955                      (read-string "Command: " (cons command 0)))))
9956       (erase-buffer)
9957       (insert "$ " command "\n\n")
9958       (if gnus-view-pseudo-asynchronously
9959           (start-process "gnus-execute" (current-buffer) shell-file-name
9960                          shell-command-switch command)
9961         (call-process shell-file-name nil t nil
9962                       shell-command-switch command)))))
9963
9964 ;; Summary kill commands.
9965
9966 (defun gnus-summary-edit-global-kill (article)
9967   "Edit the \"global\" kill file."
9968   (interactive (list (gnus-summary-article-number)))
9969   (gnus-group-edit-global-kill article))
9970
9971 (defun gnus-summary-edit-local-kill ()
9972   "Edit a local kill file applied to the current newsgroup."
9973   (interactive)
9974   (setq gnus-current-headers (gnus-summary-article-header))
9975   (gnus-group-edit-local-kill
9976    (gnus-summary-article-number) gnus-newsgroup-name))
9977
9978 ;;; Header reading.
9979
9980 (defun gnus-read-header (id &optional header)
9981   "Read the headers of article ID and enter them into the Gnus system."
9982   (let ((group gnus-newsgroup-name)
9983         (gnus-override-method
9984          (or
9985           gnus-override-method
9986           (and (gnus-news-group-p gnus-newsgroup-name)
9987                (car (gnus-refer-article-methods)))))
9988         where)
9989     ;; First we check to see whether the header in question is already
9990     ;; fetched.
9991     (if (stringp id)
9992         ;; This is a Message-ID.
9993         (setq header (or header (gnus-id-to-header id)))
9994       ;; This is an article number.
9995       (setq header (or header (gnus-summary-article-header id))))
9996     (if (and header
9997              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9998         ;; We have found the header.
9999         header
10000       ;; If this is a sparse article, we have to nix out its
10001       ;; previous entry in the thread hashtb.
10002       (when (and header
10003                  (gnus-summary-article-sparse-p (mail-header-number header)))
10004         (let* ((parent (gnus-parent-id (mail-header-references header)))
10005                (thread (and parent (gnus-id-to-thread parent))))
10006           (when thread
10007             (delq (assq header thread) thread))))
10008       ;; We have to really fetch the header to this article.
10009       (save-excursion
10010         (set-buffer nntp-server-buffer)
10011         (when (setq where (gnus-request-head id group))
10012           (nnheader-fold-continuation-lines)
10013           (goto-char (point-max))
10014           (insert ".\n")
10015           (goto-char (point-min))
10016           (insert "211 ")
10017           (princ (cond
10018                   ((numberp id) id)
10019                   ((cdr where) (cdr where))
10020                   (header (mail-header-number header))
10021                   (t gnus-reffed-article-number))
10022                  (current-buffer))
10023           (insert " Article retrieved.\n"))
10024         (if (or (not where)
10025                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10026             ()                          ; Malformed head.
10027           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10028             (when (and (stringp id)
10029                        (not (string= (gnus-group-real-name group)
10030                                      (car where))))
10031               ;; If we fetched by Message-ID and the article came
10032               ;; from a different group, we fudge some bogus article
10033               ;; numbers for this article.
10034               (mail-header-set-number header gnus-reffed-article-number))
10035             (save-excursion
10036               (set-buffer gnus-summary-buffer)
10037               (decf gnus-reffed-article-number)
10038               (gnus-remove-header (mail-header-number header))
10039               (push header gnus-newsgroup-headers)
10040               (setq gnus-current-headers header)
10041               (push (mail-header-number header) gnus-newsgroup-limit)))
10042           header)))))
10043
10044 (defun gnus-remove-header (number)
10045   "Remove header NUMBER from `gnus-newsgroup-headers'."
10046   (if (and gnus-newsgroup-headers
10047            (= number (mail-header-number (car gnus-newsgroup-headers))))
10048       (pop gnus-newsgroup-headers)
10049     (let ((headers gnus-newsgroup-headers))
10050       (while (and (cdr headers)
10051                   (not (= number (mail-header-number (cadr headers)))))
10052         (pop headers))
10053       (when (cdr headers)
10054         (setcdr headers (cddr headers))))))
10055
10056 ;;;
10057 ;;; summary highlights
10058 ;;;
10059
10060 (defun gnus-highlight-selected-summary ()
10061   "Highlight selected article in summary buffer."
10062   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10063   (when gnus-summary-selected-face
10064     (save-excursion
10065       (let* ((beg (progn (beginning-of-line) (point)))
10066              (end (progn (end-of-line) (point)))
10067              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10068              (from (if (get-text-property beg gnus-mouse-face-prop)
10069                        beg
10070                      (or (next-single-property-change
10071                           beg gnus-mouse-face-prop nil end)
10072                          beg)))
10073              (to
10074               (if (= from end)
10075                   (- from 2)
10076                 (or (next-single-property-change
10077                      from gnus-mouse-face-prop nil end)
10078                     end))))
10079         ;; If no mouse-face prop on line we will have to = from = end,
10080         ;; so we highlight the entire line instead.
10081         (when (= (+ to 2) from)
10082           (setq from beg)
10083           (setq to end))
10084         (if gnus-newsgroup-selected-overlay
10085             ;; Move old overlay.
10086             (gnus-move-overlay
10087              gnus-newsgroup-selected-overlay from to (current-buffer))
10088           ;; Create new overlay.
10089           (gnus-overlay-put
10090            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10091            'face gnus-summary-selected-face))))))
10092
10093 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10094 (defun gnus-summary-highlight-line ()
10095   "Highlight current line according to `gnus-summary-highlight'."
10096   (let* ((list gnus-summary-highlight)
10097          (p (point))
10098          (end (progn (end-of-line) (point)))
10099          ;; now find out where the line starts and leave point there.
10100          (beg (progn (beginning-of-line) (point)))
10101          (article (gnus-summary-article-number))
10102          (score (or (cdr (assq (or article gnus-current-article)
10103                                gnus-newsgroup-scored))
10104                     gnus-summary-default-score 0))
10105          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10106          (inhibit-read-only t))
10107     ;; Eval the cars of the lists until we find a match.
10108     (let ((default gnus-summary-default-score)
10109           (default-high gnus-summary-default-high-score)
10110           (default-low gnus-summary-default-low-score))
10111       (while (and list
10112                   (not (eval (caar list))))
10113         (setq list (cdr list))))
10114     (let ((face (cdar list)))
10115       (unless (eq face (get-text-property beg 'face))
10116         (gnus-put-text-property-excluding-characters-with-faces
10117          beg end 'face
10118          (setq face (if (boundp face) (symbol-value face) face)))
10119         (when gnus-summary-highlight-line-function
10120           (funcall gnus-summary-highlight-line-function article face))))
10121     (goto-char p)))
10122
10123 (defun gnus-update-read-articles (group unread &optional compute)
10124   "Update the list of read articles in GROUP."
10125   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10126          (entry (gnus-gethash group gnus-newsrc-hashtb))
10127          (info (nth 2 entry))
10128          (prev 1)
10129          (unread (sort (copy-sequence unread) '<))
10130          read)
10131     (if (or (not info) (not active))
10132         ;; There is no info on this group if it was, in fact,
10133         ;; killed.  Gnus stores no information on killed groups, so
10134         ;; there's nothing to be done.
10135         ;; One could store the information somewhere temporarily,
10136         ;; perhaps...  Hmmm...
10137         ()
10138       ;; Remove any negative articles numbers.
10139       (while (and unread (< (car unread) 0))
10140         (setq unread (cdr unread)))
10141       ;; Remove any expired article numbers
10142       (while (and unread (< (car unread) (car active)))
10143         (setq unread (cdr unread)))
10144       ;; Compute the ranges of read articles by looking at the list of
10145       ;; unread articles.
10146       (while unread
10147         (when (/= (car unread) prev)
10148           (push (if (= prev (1- (car unread))) prev
10149                   (cons prev (1- (car unread))))
10150                 read))
10151         (setq prev (1+ (car unread)))
10152         (setq unread (cdr unread)))
10153       (when (<= prev (cdr active))
10154         (push (cons prev (cdr active)) read))
10155       (setq read (if (> (length read) 1) (nreverse read) read))
10156       (if compute
10157           read
10158         (save-excursion
10159           (let (setmarkundo)
10160             ;; Propagate the read marks to the backend.
10161             (when (gnus-check-backend-function 'request-set-mark group)
10162               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10163                     (add (gnus-remove-from-range read (gnus-info-read info))))
10164                 (when (or add del)
10165                   (unless (gnus-check-group group)
10166                     (error "Can't open server for %s" group))
10167                   (gnus-request-set-mark
10168                    group (delq nil (list (if add (list add 'add '(read)))
10169                                          (if del (list del 'del '(read))))))
10170                   (setq setmarkundo
10171                         `(gnus-request-set-mark
10172                           ,group
10173                           ',(delq nil (list
10174                                        (if del (list del 'add '(read)))
10175                                        (if add (list add 'del '(read))))))))))
10176             (set-buffer gnus-group-buffer)
10177             (gnus-undo-register
10178               `(progn
10179                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10180                  (gnus-info-set-read ',info ',(gnus-info-read info))
10181                  (gnus-get-unread-articles-in-group ',info
10182                                                     (gnus-active ,group))
10183                  (gnus-group-update-group ,group t)
10184                  ,setmarkundo))))
10185         ;; Enter this list into the group info.
10186         (gnus-info-set-read info read)
10187         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10188         (gnus-get-unread-articles-in-group info (gnus-active group))
10189         t))))
10190
10191 (defun gnus-offer-save-summaries ()
10192   "Offer to save all active summary buffers."
10193   (let (buffers)
10194     ;; Go through all buffers and find all summaries.
10195     (dolist (buffer (buffer-list))
10196       (when (and (setq buffer (buffer-name buffer))
10197                  (string-match "Summary" buffer)
10198                  (save-excursion
10199                    (set-buffer buffer)
10200                    ;; We check that this is, indeed, a summary buffer.
10201                    (and (eq major-mode 'gnus-summary-mode)
10202                         ;; Also make sure this isn't bogus.
10203                         gnus-newsgroup-prepared
10204                         ;; Also make sure that this isn't a
10205                         ;; dead summary buffer.
10206                         (not gnus-dead-summary-mode))))
10207         (push buffer buffers)))
10208     ;; Go through all these summary buffers and offer to save them.
10209     (when buffers
10210       (save-excursion
10211         (map-y-or-n-p
10212          "Update summary buffer %s? "
10213          (lambda (buf)
10214            (switch-to-buffer buf)
10215            (gnus-summary-exit))
10216          buffers)))))
10217
10218 (defun gnus-summary-setup-default-charset ()
10219   "Setup newsgroup default charset."
10220   (if (equal gnus-newsgroup-name "nndraft:drafts")
10221       (setq gnus-newsgroup-charset nil)
10222     (let* ((ignored-charsets
10223             (or gnus-newsgroup-ephemeral-ignored-charsets
10224                 (append
10225                  (and gnus-newsgroup-name
10226                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10227                  gnus-newsgroup-ignored-charsets))))
10228       (setq gnus-newsgroup-charset
10229             (or gnus-newsgroup-ephemeral-charset
10230                 (and gnus-newsgroup-name
10231                      (gnus-parameter-charset gnus-newsgroup-name))
10232                 gnus-default-charset))
10233       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10234            ignored-charsets))))
10235
10236 ;;;
10237 ;;; Mime Commands
10238 ;;;
10239
10240 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10241   "Display the current article buffer fully MIME-buttonized.
10242 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10243 treated as multipart/mixed."
10244   (interactive "P")
10245   (require 'gnus-art)
10246   (let ((gnus-unbuttonized-mime-types nil)
10247         (gnus-mime-display-multipart-as-mixed show-all-parts))
10248     (gnus-summary-show-article)))
10249
10250 (defun gnus-summary-repair-multipart (article)
10251   "Add a Content-Type header to a multipart article without one."
10252   (interactive (list (gnus-summary-article-number)))
10253   (gnus-with-article article
10254     (message-narrow-to-head)
10255     (message-remove-header "Mime-Version")
10256     (goto-char (point-max))
10257     (insert "Mime-Version: 1.0\n")
10258     (widen)
10259     (when (search-forward "\n--" nil t)
10260       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10261         (message-narrow-to-head)
10262         (message-remove-header "Content-Type")
10263         (goto-char (point-max))
10264         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10265                         separator))
10266         (widen))))
10267   (let (gnus-mark-article-hook)
10268     (gnus-summary-select-article t t nil article)))
10269
10270 (defun gnus-summary-toggle-display-buttonized ()
10271   "Toggle the buttonizing of the article buffer."
10272   (interactive)
10273   (require 'gnus-art)
10274   (if (setq gnus-inhibit-mime-unbuttonizing
10275             (not gnus-inhibit-mime-unbuttonizing))
10276       (let ((gnus-unbuttonized-mime-types nil))
10277         (gnus-summary-show-article))
10278     (gnus-summary-show-article)))
10279
10280 ;;;
10281 ;;; Generic summary marking commands
10282 ;;;
10283
10284 (defvar gnus-summary-marking-alist
10285   '((read gnus-del-mark "d")
10286     (unread gnus-unread-mark "u")
10287     (ticked gnus-ticked-mark "!")
10288     (dormant gnus-dormant-mark "?")
10289     (expirable gnus-expirable-mark "e"))
10290   "An alist of names/marks/keystrokes.")
10291
10292 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10293 (defvar gnus-summary-mark-map)
10294
10295 (defun gnus-summary-make-all-marking-commands ()
10296   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10297   (dolist (elem gnus-summary-marking-alist)
10298     (apply 'gnus-summary-make-marking-command elem)))
10299
10300 (defun gnus-summary-make-marking-command (name mark keystroke)
10301   (let ((map (make-sparse-keymap)))
10302     (define-key gnus-summary-generic-mark-map keystroke map)
10303     (dolist (lway `((next "next" next nil "n")
10304                     (next-unread "next unread" next t "N")
10305                     (prev "previous" prev nil "p")
10306                     (prev-unread "previous unread" prev t "P")
10307                     (nomove "" nil nil ,keystroke)))
10308       (let ((func (gnus-summary-make-marking-command-1
10309                    mark (car lway) lway name)))
10310         (setq func (eval func))
10311         (define-key map (nth 4 lway) func)))))
10312
10313 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10314   `(defun ,(intern
10315             (format "gnus-summary-put-mark-as-%s%s"
10316                     name (if (eq way 'nomove)
10317                              ""
10318                            (concat "-" (symbol-name way)))))
10319      (n)
10320      ,(format
10321        "Mark the current article as %s%s.
10322 If N, the prefix, then repeat N times.
10323 If N is negative, move in reverse order.
10324 The difference between N and the actual number of articles marked is
10325 returned."
10326        name (cadr lway))
10327      (interactive "p")
10328      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10329
10330 (defun gnus-summary-generic-mark (n mark move unread)
10331   "Mark N articles with MARK."
10332   (unless (eq major-mode 'gnus-summary-mode)
10333     (error "This command can only be used in the summary buffer"))
10334   (gnus-summary-show-thread)
10335   (let ((nummove
10336          (cond
10337           ((eq move 'next) 1)
10338           ((eq move 'prev) -1)
10339           (t 0))))
10340     (if (zerop nummove)
10341         (setq n 1)
10342       (when (< n 0)
10343         (setq n (abs n)
10344               nummove (* -1 nummove))))
10345     (while (and (> n 0)
10346                 (gnus-summary-mark-article nil mark)
10347                 (zerop (gnus-summary-next-subject nummove unread t)))
10348       (setq n (1- n)))
10349     (when (/= 0 n)
10350       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10351     (gnus-summary-recenter)
10352     (gnus-summary-position-point)
10353     (gnus-set-mode-line 'summary)
10354     n))
10355
10356 (defun gnus-summary-insert-articles (articles)
10357   (when (setq articles
10358               (gnus-set-difference articles
10359                                    (mapcar (lambda (h) (mail-header-number h))
10360                                            gnus-newsgroup-headers)))
10361     (setq gnus-newsgroup-headers
10362           (merge 'list
10363                  gnus-newsgroup-headers
10364                  (gnus-fetch-headers articles)
10365                  'gnus-article-sort-by-number))
10366     ;; Suppress duplicates?
10367     (when gnus-suppress-duplicates
10368       (gnus-dup-suppress-articles))
10369
10370     ;; We might want to build some more threads first.
10371     (when (and gnus-fetch-old-headers
10372                (eq gnus-headers-retrieved-by 'nov))
10373       (if (eq gnus-fetch-old-headers 'invisible)
10374           (gnus-build-all-threads)
10375         (gnus-build-old-threads)))
10376     ;; Let the Gnus agent mark articles as read.
10377     (when gnus-agent
10378       (gnus-agent-get-undownloaded-list))
10379     ;; Remove list identifiers from subject
10380     (when gnus-list-identifiers
10381       (gnus-summary-remove-list-identifiers))
10382     ;; First and last article in this newsgroup.
10383     (when gnus-newsgroup-headers
10384       (setq gnus-newsgroup-begin
10385             (mail-header-number (car gnus-newsgroup-headers))
10386             gnus-newsgroup-end
10387             (mail-header-number
10388              (gnus-last-element gnus-newsgroup-headers))))
10389     (when gnus-use-scoring
10390       (gnus-possibly-score-headers))))
10391
10392 (defun gnus-summary-insert-old-articles (&optional all)
10393   "Insert all old articles in this group.
10394 If ALL is non-nil, already read articles become readable.
10395 If ALL is a number, fetch this number of articles."
10396   (interactive "P")
10397   (prog1
10398       (let ((old (mapcar 'car gnus-newsgroup-data))
10399             (i (car gnus-newsgroup-active))
10400             older len)
10401         (while (<= i (cdr gnus-newsgroup-active))
10402           (or (memq i old) (push i older))
10403           (incf i))
10404         (setq len (length older))
10405         (cond
10406          ((null older) nil)
10407          ((numberp all)
10408           (if (< all len)
10409               (setq older (subseq older 0 all))))
10410          (all nil)
10411          (t
10412           (if (and (numberp gnus-large-newsgroup)
10413                    (> len gnus-large-newsgroup))
10414               (let ((input
10415                      (read-string
10416                       (format
10417                        "How many articles from %s (default %d): "
10418                        (gnus-limit-string
10419                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10420                        len))))
10421                 (unless (string-match "^[ \t]*$" input)
10422                   (setq all (string-to-number input))
10423                   (if (< all len)
10424                       (setq older (subseq older 0 all))))))))
10425         (if (not older)
10426             (message "No old news.")
10427           (gnus-summary-insert-articles older)
10428           (gnus-summary-limit (gnus-union older old))))
10429     (gnus-summary-position-point)))
10430
10431 (defun gnus-summary-insert-new-articles ()
10432   "Insert all new articles in this group."
10433   (interactive)
10434   (prog1
10435       (let ((old (mapcar 'car gnus-newsgroup-data))
10436             (old-active gnus-newsgroup-active)
10437             (nnmail-fetched-sources (list t))
10438             i new)
10439         (setq gnus-newsgroup-active
10440               (gnus-activate-group gnus-newsgroup-name 'scan))
10441         (setq i (1+ (cdr old-active)))
10442         (while (<= i (cdr gnus-newsgroup-active))
10443           (push i new)
10444           (incf i))
10445         (if (not new)
10446             (message "No gnus is bad news.")
10447           (setq new (nreverse new))
10448           (gnus-summary-insert-articles new)
10449           (setq gnus-newsgroup-unreads
10450                 (append gnus-newsgroup-unreads new))
10451           (gnus-summary-limit (gnus-union old new))))
10452     (gnus-summary-position-point)))
10453
10454 (gnus-summary-make-all-marking-commands)
10455
10456 (gnus-ems-redefine)
10457
10458 (provide 'gnus-sum)
10459
10460 (run-hooks 'gnus-sum-load-hook)
10461
10462 ;;; gnus-sum.el ends here