Importing Oort Gnus v0.01.
[elisp/gnus.git-] / 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-zcore-fuzz 0
203   "*Fuzziness factor for the zcore in the summary buffer.
204 Articles with scores closer than this to `gnus-summary-default-score'
205 will not be marked."
206   :group 'gnus-summary-format
207   :type 'integer)
208
209 (defcustom gnus-simplify-subject-fuzzy-regexp nil
210   "*Strings to be removed when doing fuzzy matches.
211 This can either be a regular expression or list of regular expressions
212 that will be removed from subject strings if fuzzy subject
213 simplification is selected."
214   :group 'gnus-thread
215   :type '(repeat regexp))
216
217 (defcustom gnus-show-threads t
218   "*If non-nil, display threads in summary mode."
219   :group 'gnus-thread
220   :type 'boolean)
221
222 (defcustom gnus-thread-hide-subtree nil
223   "*If non-nil, hide all threads initially.
224 If threads are hidden, you have to run the command
225 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
226 to expose hidden threads."
227   :group 'gnus-thread
228   :type 'boolean)
229
230 (defcustom gnus-thread-hide-killed t
231   "*If non-nil, hide killed threads automatically."
232   :group 'gnus-thread
233   :type 'boolean)
234
235 (defcustom gnus-thread-ignore-subject t
236   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
237 If nil, articles that have different subjects from their parents will
238 start separate threads."
239   :group 'gnus-thread
240   :type 'boolean)
241
242 (defcustom gnus-thread-operation-ignore-subject t
243   "*If non-nil, subjects will be ignored when doing thread commands.
244 This affects commands like `gnus-summary-kill-thread' and
245 `gnus-summary-lower-thread'.
246
247 If this variable is nil, articles in the same thread with different
248 subjects will not be included in the operation in question.  If this
249 variable is `fuzzy', only articles that have subjects that are fuzzily
250 equal will be included."
251   :group 'gnus-thread
252   :type '(choice (const :tag "off" nil)
253                  (const fuzzy)
254                  (sexp :tag "on" t)))
255
256 (defcustom gnus-thread-indent-level 4
257   "*Number that says how much each sub-thread should be indented."
258   :group 'gnus-thread
259   :type 'integer)
260
261 (defcustom gnus-auto-extend-newsgroup t
262   "*If non-nil, extend newsgroup forward and backward when requested."
263   :group 'gnus-summary-choose
264   :type 'boolean)
265
266 (defcustom gnus-auto-select-first t
267   "*If nil, don't select the first unread article when entering a group.
268 If this variable is `best', select the highest-scored unread article
269 in the group.  If t, select the first unread article.
270
271 This variable can also be a function to place point on a likely
272 subject line.  Useful values include `gnus-summary-first-unread-subject',
273 `gnus-summary-first-unread-article' and
274 `gnus-summary-best-unread-article'.
275
276 If you want to prevent automatic selection of the first unread article
277 in some newsgroups, set the variable to nil in
278 `gnus-select-group-hook'."
279   :group 'gnus-group-select
280   :type '(choice (const :tag "none" nil)
281                  (const best)
282                  (sexp :menu-tag "first" t)
283                  (function-item gnus-summary-first-unread-subject)
284                  (function-item gnus-summary-first-unread-article)
285                  (function-item gnus-summary-best-unread-article)))
286
287 (defcustom gnus-auto-select-next t
288   "*If non-nil, offer to go to the next group from the end of the previous.
289 If the value is t and the next newsgroup is empty, Gnus will exit
290 summary mode and go back to group mode.  If the value is neither nil
291 nor t, Gnus will select the following unread newsgroup.  In
292 particular, if the value is the symbol `quietly', the next unread
293 newsgroup will be selected without any confirmation, and if it is
294 `almost-quietly', the next group will be selected without any
295 confirmation if you are located on the last article in the group.
296 Finally, if this variable is `slightly-quietly', the `Z n' command
297 will go to the next group without confirmation."
298   :group 'gnus-summary-maneuvering
299   :type '(choice (const :tag "off" nil)
300                  (const quietly)
301                  (const almost-quietly)
302                  (const slightly-quietly)
303                  (sexp :menu-tag "on" t)))
304
305 (defcustom gnus-auto-select-same nil
306   "*If non-nil, select the next article with the same subject.
307 If there are no more articles with the same subject, go to
308 the first unread article."
309   :group 'gnus-summary-maneuvering
310   :type 'boolean)
311
312 (defcustom gnus-summary-check-current nil
313   "*If non-nil, consider the current article when moving.
314 The \"unread\" movement commands will stay on the same line if the
315 current article is unread."
316   :group 'gnus-summary-maneuvering
317   :type 'boolean)
318
319 (defcustom gnus-auto-center-summary t
320   "*If non-nil, always center the current summary buffer.
321 In particular, if `vertical' do only vertical recentering.  If non-nil
322 and non-`vertical', do both horizontal and vertical recentering."
323   :group 'gnus-summary-maneuvering
324   :type '(choice (const :tag "none" nil)
325                  (const vertical)
326                  (integer :tag "height")
327                  (sexp :menu-tag "both" t)))
328
329 (defcustom gnus-show-all-headers nil
330   "*If non-nil, don't hide any headers."
331   :group 'gnus-article-hiding
332   :group 'gnus-article-headers
333   :type 'boolean)
334
335 (defcustom gnus-summary-ignore-duplicates nil
336   "*If non-nil, ignore articles with identical Message-ID headers."
337   :group 'gnus-summary
338   :type 'boolean)
339
340 (defcustom gnus-single-article-buffer t
341   "*If non-nil, display all articles in the same buffer.
342 If nil, each group will get its own article buffer."
343   :group 'gnus-article-various
344   :type 'boolean)
345
346 (defcustom gnus-break-pages t
347   "*If non-nil, do page breaking on articles.
348 The page delimiter is specified by the `gnus-page-delimiter'
349 variable."
350   :group 'gnus-article-various
351   :type 'boolean)
352
353 (defcustom gnus-move-split-methods nil
354   "*Variable used to suggest where articles are to be moved to.
355 It uses the same syntax as the `gnus-split-methods' variable.
356 However, whereas `gnus-split-methods' specifies file names as targets,
357 this variable specifies group names."
358   :group 'gnus-summary-mail
359   :type '(repeat (choice (list :value (fun) function)
360                          (cons :value ("" "") regexp (repeat string))
361                          (sexp :value nil))))
362
363 (defcustom gnus-unread-mark ?  ;Whitespace
364   "*Mark used for unread articles."
365   :group 'gnus-summary-marks
366   :type 'character)
367
368 (defcustom gnus-ticked-mark ?!
369   "*Mark used for ticked articles."
370   :group 'gnus-summary-marks
371   :type 'character)
372
373 (defcustom gnus-dormant-mark ??
374   "*Mark used for dormant articles."
375   :group 'gnus-summary-marks
376   :type 'character)
377
378 (defcustom gnus-del-mark ?r
379   "*Mark used for del'd articles."
380   :group 'gnus-summary-marks
381   :type 'character)
382
383 (defcustom gnus-read-mark ?R
384   "*Mark used for read articles."
385   :group 'gnus-summary-marks
386   :type 'character)
387
388 (defcustom gnus-expirable-mark ?E
389   "*Mark used for expirable articles."
390   :group 'gnus-summary-marks
391   :type 'character)
392
393 (defcustom gnus-killed-mark ?K
394   "*Mark used for killed articles."
395   :group 'gnus-summary-marks
396   :type 'character)
397
398 (defcustom gnus-souped-mark ?F
399   "*Mark used for souped articles."
400   :group 'gnus-summary-marks
401   :type 'character)
402
403 (defcustom gnus-kill-file-mark ?X
404   "*Mark used for articles killed by kill files."
405   :group 'gnus-summary-marks
406   :type 'character)
407
408 (defcustom gnus-low-score-mark ?Y
409   "*Mark used for articles with a low score."
410   :group 'gnus-summary-marks
411   :type 'character)
412
413 (defcustom gnus-catchup-mark ?C
414   "*Mark used for articles that are caught up."
415   :group 'gnus-summary-marks
416   :type 'character)
417
418 (defcustom gnus-replied-mark ?A
419   "*Mark used for articles that have been replied to."
420   :group 'gnus-summary-marks
421   :type 'character)
422
423 (defcustom gnus-forwarded-mark ?O
424   "*Mark used for articles that have been forwarded."
425   :group 'gnus-summary-marks
426   :type 'character)
427
428 (defcustom gnus-cached-mark ?*
429   "*Mark used for articles that are in the cache."
430   :group 'gnus-summary-marks
431   :type 'character)
432
433 (defcustom gnus-saved-mark ?S
434   "*Mark used for articles that have been saved to."
435   :group 'gnus-summary-marks
436   :type 'character)
437
438 (defcustom gnus-no-mark ?  ;Whitespace
439   "*Mark used for articles that have no other secondary mark."
440   :group 'gnus-summary-marks
441   :type 'character)
442
443 (defcustom gnus-ancient-mark ?O
444   "*Mark used for ancient articles."
445   :group 'gnus-summary-marks
446   :type 'character)
447
448 (defcustom gnus-sparse-mark ?Q
449   "*Mark used for sparsely reffed articles."
450   :group 'gnus-summary-marks
451   :type 'character)
452
453 (defcustom gnus-canceled-mark ?G
454   "*Mark used for canceled articles."
455   :group 'gnus-summary-marks
456   :type 'character)
457
458 (defcustom gnus-duplicate-mark ?M
459   "*Mark used for duplicate articles."
460   :group 'gnus-summary-marks
461   :type 'character)
462
463 (defcustom gnus-undownloaded-mark ?@
464   "*Mark used for articles that weren't downloaded."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-downloadable-mark ?%
469   "*Mark used for articles that are to be downloaded."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-unsendable-mark ?=
474   "*Mark used for articles that won't be sent."
475   :group 'gnus-summary-marks
476   :type 'character)
477
478 (defcustom gnus-score-over-mark ?+
479   "*Score mark used for articles with high scores."
480   :group 'gnus-summary-marks
481   :type 'character)
482
483 (defcustom gnus-score-below-mark ?-
484   "*Score mark used for articles with low scores."
485   :group 'gnus-summary-marks
486   :type 'character)
487
488 (defcustom gnus-empty-thread-mark ?  ;Whitespace
489   "*There is no thread under the article."
490   :group 'gnus-summary-marks
491   :type 'character)
492
493 (defcustom gnus-not-empty-thread-mark ?=
494   "*There is a thread under the article."
495   :group 'gnus-summary-marks
496   :type 'character)
497
498 (defcustom gnus-view-pseudo-asynchronously nil
499   "*If non-nil, Gnus will view pseudo-articles asynchronously."
500   :group 'gnus-extract-view
501   :type 'boolean)
502
503 (defcustom gnus-auto-expirable-marks
504   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
505         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
506         gnus-souped-mark gnus-duplicate-mark)
507   "*The list of marks converted into expiration if a group is auto-expirable."
508   :version "21.1"
509   :group 'gnus-summary
510   :type '(repeat character))
511
512 (defcustom gnus-inhibit-user-auto-expire t
513   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
514   :version "21.1"
515   :group 'gnus-summary
516   :type 'boolean)
517
518 (defcustom gnus-view-pseudos nil
519   "*If `automatic', pseudo-articles will be viewed automatically.
520 If `not-confirm', pseudos will be viewed automatically, and the user
521 will not be asked to confirm the command."
522   :group 'gnus-extract-view
523   :type '(choice (const :tag "off" nil)
524                  (const automatic)
525                  (const not-confirm)))
526
527 (defcustom gnus-view-pseudos-separately t
528   "*If non-nil, one pseudo-article will be created for each file to be viewed.
529 If nil, all files that use the same viewing command will be given as a
530 list of parameters to that command."
531   :group 'gnus-extract-view
532   :type 'boolean)
533
534 (defcustom gnus-insert-pseudo-articles t
535   "*If non-nil, insert pseudo-articles when decoding articles."
536   :group 'gnus-extract-view
537   :type 'boolean)
538
539 (defcustom gnus-summary-dummy-line-format
540   "  %(:                          :%) %S\n"
541   "*The format specification for the dummy roots in the summary buffer.
542 It works along the same lines as a normal formatting string,
543 with some simple extensions.
544
545 %S  The subject"
546   :group 'gnus-threading
547   :type 'string)
548
549 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
550   "*The format specification for the summary mode line.
551 It works along the same lines as a normal formatting string,
552 with some simple extensions:
553
554 %G  Group name
555 %p  Unprefixed group name
556 %A  Current article number
557 %z  Current article score
558 %V  Gnus version
559 %U  Number of unread articles in the group
560 %e  Number of unselected articles in the group
561 %Z  A string with unread/unselected article counts
562 %g  Shortish group name
563 %S  Subject of the current article
564 %u  User-defined spec
565 %s  Current score file name
566 %d  Number of dormant articles
567 %r  Number of articles that have been marked as read in this session
568 %E  Number of articles expunged by the score files"
569   :group 'gnus-summary-format
570   :type 'string)
571
572 (defcustom gnus-list-identifiers nil
573   "Regexp that matches list identifiers to be removed from subject.
574 This can also be a list of regexps."
575   :version "21.1"
576   :group 'gnus-summary-format
577   :group 'gnus-article-hiding
578   :type '(choice (const :tag "none" nil)
579                  (regexp :value ".*")
580                  (repeat :value (".*") regexp)))
581
582 (defcustom gnus-summary-mark-below 0
583   "*Mark all articles with a score below this variable as read.
584 This variable is local to each summary buffer and usually set by the
585 score file."
586   :group 'gnus-score-default
587   :type 'integer)
588
589 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
590   "*List of functions used for sorting articles in the summary buffer.
591
592 Each function takes two articles and returns non-nil if the first
593 article should be sorted before the other.  If you use more than one
594 function, the primary sort function should be the last.  You should
595 probably always include `gnus-article-sort-by-number' in the list of
596 sorting functions -- preferably first.  Also note that sorting by date
597 is often much slower than sorting by number, and the sorting order is
598 very similar.  (Sorting by date means sorting by the time the message
599 was sent, sorting by number means sorting by arrival time.)
600
601 Ready-made functions include `gnus-article-sort-by-number',
602 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
603 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
604
605 When threading is turned on, the variable `gnus-thread-sort-functions'
606 controls how articles are sorted."
607   :group 'gnus-summary-sort
608   :type '(repeat (choice (function-item gnus-article-sort-by-number)
609                          (function-item gnus-article-sort-by-author)
610                          (function-item gnus-article-sort-by-subject)
611                          (function-item gnus-article-sort-by-date)
612                          (function-item gnus-article-sort-by-score)
613                          (function :tag "other"))))
614
615 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
616   "*List of functions used for sorting threads in the summary buffer.
617 By default, threads are sorted by article number.
618
619 Each function takes two threads and returns non-nil if the first
620 thread should be sorted before the other.  If you use more than one
621 function, the primary sort function should be the last.  You should
622 probably always include `gnus-thread-sort-by-number' in the list of
623 sorting functions -- preferably first.  Also note that sorting by date
624 is often much slower than sorting by number, and the sorting order is
625 very similar.  (Sorting by date means sorting by the time the message
626 was sent, sorting by number means sorting by arrival time.)
627
628 Ready-made functions include `gnus-thread-sort-by-number',
629 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
630 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
631 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
632
633 When threading is turned off, the variable
634 `gnus-article-sort-functions' controls how articles are sorted."
635   :group 'gnus-summary-sort
636   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
637                          (function-item gnus-thread-sort-by-author)
638                          (function-item gnus-thread-sort-by-subject)
639                          (function-item gnus-thread-sort-by-date)
640                          (function-item gnus-thread-sort-by-score)
641                          (function-item gnus-thread-sort-by-total-score)
642                          (function :tag "other"))))
643
644 (defcustom gnus-thread-score-function '+
645   "*Function used for calculating the total score of a thread.
646
647 The function is called with the scores of the article and each
648 subthread and should then return the score of the thread.
649
650 Some functions you can use are `+', `max', or `min'."
651   :group 'gnus-summary-sort
652   :type 'function)
653
654 (defcustom gnus-summary-expunge-below nil
655   "All articles that have a score less than this variable will be expunged.
656 This variable is local to the summary buffers."
657   :group 'gnus-score-default
658   :type '(choice (const :tag "off" nil)
659                  integer))
660
661 (defcustom gnus-thread-expunge-below nil
662   "All threads that have a total score less than this variable will be expunged.
663 See `gnus-thread-score-function' for en explanation of what a
664 \"thread score\" is.
665
666 This variable is local to the summary buffers."
667   :group 'gnus-threading
668   :group 'gnus-score-default
669   :type '(choice (const :tag "off" nil)
670                  integer))
671
672 (defcustom gnus-summary-mode-hook nil
673   "*A hook for Gnus summary mode.
674 This hook is run before any variables are set in the summary buffer."
675   :options '(turn-on-gnus-mailing-list-mode)
676   :group 'gnus-summary-various
677   :type 'hook)
678
679 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
680 (when (featurep 'xemacs)
681   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
682   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
683   (add-hook 'gnus-summary-mode-hook
684             'gnus-xmas-switch-horizontal-scrollbar-off))
685
686 (defcustom gnus-summary-menu-hook nil
687   "*Hook run after the creation of the summary mode menu."
688   :group 'gnus-summary-visual
689   :type 'hook)
690
691 (defcustom gnus-summary-exit-hook nil
692   "*A hook called on exit from the summary buffer.
693 It will be called with point in the group buffer."
694   :group 'gnus-summary-exit
695   :type 'hook)
696
697 (defcustom gnus-summary-prepare-hook nil
698   "*A hook called after the summary buffer has been generated.
699 If you want to modify the summary buffer, you can use this hook."
700   :group 'gnus-summary-various
701   :type 'hook)
702
703 (defcustom gnus-summary-prepared-hook nil
704   "*A hook called as the last thing after the summary buffer has been generated."
705   :group 'gnus-summary-various
706   :type 'hook)
707
708 (defcustom gnus-summary-generate-hook nil
709   "*A hook run just before generating the summary buffer.
710 This hook is commonly used to customize threading variables and the
711 like."
712   :group 'gnus-summary-various
713   :type 'hook)
714
715 (defcustom gnus-select-group-hook nil
716   "*A hook called when a newsgroup is selected.
717
718 If you'd like to simplify subjects like the
719 `gnus-summary-next-same-subject' command does, you can use the
720 following hook:
721
722  (setq gnus-select-group-hook
723       (list
724         (lambda ()
725           (mapcar (lambda (header)
726                      (mail-header-set-subject
727                       header
728                       (gnus-simplify-subject
729                        (mail-header-subject header) 're-only)))
730                   gnus-newsgroup-headers))))"
731   :group 'gnus-group-select
732   :type 'hook)
733
734 (defcustom gnus-select-article-hook nil
735   "*A hook called when an article is selected."
736   :group 'gnus-summary-choose
737   :type 'hook)
738
739 (defcustom gnus-visual-mark-article-hook
740   (list 'gnus-highlight-selected-summary)
741   "*Hook run after selecting an article in the summary buffer.
742 It is meant to be used for highlighting the article in some way.  It
743 is not run if `gnus-visual' is nil."
744   :group 'gnus-summary-visual
745   :type 'hook)
746
747 (defcustom gnus-parse-headers-hook nil
748   "*A hook called before parsing the headers."
749   :group 'gnus-various
750   :type 'hook)
751
752 (defcustom gnus-exit-group-hook nil
753   "*A hook called when exiting summary mode.
754 This hook is not called from the non-updating exit commands like `Q'."
755   :group 'gnus-various
756   :type 'hook)
757
758 (defcustom gnus-summary-update-hook
759   (list 'gnus-summary-highlight-line)
760   "*A hook called when a summary line is changed.
761 The hook will not be called if `gnus-visual' is nil.
762
763 The default function `gnus-summary-highlight-line' will
764 highlight the line according to the `gnus-summary-highlight'
765 variable."
766   :group 'gnus-summary-visual
767   :type 'hook)
768
769 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
770   "*A hook called when an article is selected for the first time.
771 The hook is intended to mark an article as read (or unread)
772 automatically when it is selected."
773   :group 'gnus-summary-choose
774   :type 'hook)
775
776 (defcustom gnus-group-no-more-groups-hook nil
777   "*A hook run when returning to group mode having no more (unread) groups."
778   :group 'gnus-group-select
779   :type 'hook)
780
781 (defcustom gnus-ps-print-hook nil
782   "*A hook run before ps-printing something from Gnus."
783   :group 'gnus-summary
784   :type 'hook)
785
786 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
787   "Face used for highlighting the current article in the summary buffer."
788   :group 'gnus-summary-visual
789   :type 'face)
790
791 (defcustom gnus-summary-highlight
792   '(((= mark gnus-canceled-mark)
793      . gnus-summary-cancelled-face)
794     ((and (> score default)
795           (or (= mark gnus-dormant-mark)
796               (= mark gnus-ticked-mark)))
797      . gnus-summary-high-ticked-face)
798     ((and (< score default)
799           (or (= mark gnus-dormant-mark)
800               (= mark gnus-ticked-mark)))
801      . gnus-summary-low-ticked-face)
802     ((or (= mark gnus-dormant-mark)
803          (= mark gnus-ticked-mark))
804      . gnus-summary-normal-ticked-face)
805     ((and (> score default) (= mark gnus-ancient-mark))
806      . gnus-summary-high-ancient-face)
807     ((and (< score default) (= mark gnus-ancient-mark))
808      . gnus-summary-low-ancient-face)
809     ((= mark gnus-ancient-mark)
810      . gnus-summary-normal-ancient-face)
811     ((and (> score default) (= mark gnus-unread-mark))
812      . gnus-summary-high-unread-face)
813     ((and (< score default) (= mark gnus-unread-mark))
814      . gnus-summary-low-unread-face)
815     ((= mark gnus-unread-mark)
816      . gnus-summary-normal-unread-face)
817     ((and (> score default) (memq mark (list gnus-downloadable-mark
818                                              gnus-undownloaded-mark)))
819      . gnus-summary-high-unread-face)
820     ((and (< score default) (memq mark (list gnus-downloadable-mark
821                                              gnus-undownloaded-mark)))
822      . gnus-summary-low-unread-face)
823     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
824      . gnus-summary-normal-unread-face)
825     ((> score default)
826      . gnus-summary-high-read-face)
827     ((< score default)
828      . gnus-summary-low-read-face)
829     (t
830      . gnus-summary-normal-read-face))
831   "*Controls the highlighting of summary buffer lines.
832
833 A list of (FORM . FACE) pairs.  When deciding how a a particular
834 summary line should be displayed, each form is evaluated.  The content
835 of the face field after the first true form is used.  You can change
836 how those summary lines are displayed, by editing the face field.
837
838 You can use the following variables in the FORM field.
839
840 score:   The articles score
841 default: The default article score.
842 below:   The score below which articles are automatically marked as read.
843 mark:    The articles mark."
844   :group 'gnus-summary-visual
845   :type '(repeat (cons (sexp :tag "Form" nil)
846                        face)))
847
848 (defcustom gnus-alter-header-function nil
849   "Function called to allow alteration of article header structures.
850 The function is called with one parameter, the article header vector,
851 which it may alter in any way.")
852
853 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
854   "Variable that says which function should be used to decode a string with encoded words.")
855
856 (defcustom gnus-extra-headers nil
857   "*Extra headers to parse."
858   :version "21.1"
859   :group 'gnus-summary
860   :type '(repeat symbol))
861
862 (defcustom gnus-ignored-from-addresses
863   (and user-mail-address (regexp-quote user-mail-address))
864   "*Regexp of From headers that may be suppressed in favor of To headers."
865   :version "21.1"
866   :group 'gnus-summary
867   :type 'regexp)
868
869 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
870   "List of charsets that should be ignored.
871 When these charsets are used in the \"charset\" parameter, the
872 default charset will be used instead."
873   :version "21.1"
874   :type '(repeat symbol)
875   :group 'gnus-charset)
876
877 (gnus-define-group-parameter
878  ignored-charsets
879  :type list
880  :function-document
881  "Return the ignored charsets of GROUP."
882  :variable gnus-group-ignored-charsets-alist
883  :variable-default 
884  '(("alt\\.chinese\\.text" iso-8859-1))
885  :variable-document
886  "Alist of regexps (to match group names) and charsets that should be ignored.
887 When these charsets are used in the \"charset\" parameter, the
888 default charset will be used instead."
889  :variable-group gnus-charset
890  :variable-type '(repeat (cons (regexp :tag "Group")
891                                (repeat symbol)))
892  :parameter-type '(choice :tag "Ignored charsets" 
893                           :value nil
894                           (repeat (symbol)))
895  :parameter-document       "\
896 List of charsets that should be ignored.
897
898 When these charsets are used in the \"charset\" parameter, the
899 default charset will be used instead.")
900
901 (defcustom gnus-group-highlight-words-alist nil
902   "Alist of group regexps and highlight regexps.
903 This variable uses the same syntax as `gnus-emphasis-alist'."
904   :version "21.1"
905   :type '(repeat (cons (regexp :tag "Group")
906                        (repeat (list (regexp :tag "Highlight regexp")
907                                      (number :tag "Group for entire word" 0)
908                                      (number :tag "Group for displayed part" 0)
909                                      (symbol :tag "Face"
910                                              gnus-emphasis-highlight-words)))))
911   :group 'gnus-summary-visual)
912
913 (defcustom gnus-summary-show-article-charset-alist
914   nil
915   "Alist of number and charset.
916 The article will be shown with the charset corresponding to the
917 numbered argument.
918 For example: ((1 . cn-gb-2312) (2 . big5))."
919   :version "21.1"
920   :type '(repeat (cons (number :tag "Argument" 1)
921                        (symbol :tag "Charset")))
922   :group 'gnus-charset)
923
924 (defcustom gnus-preserve-marks t
925   "Whether marks are preserved when moving, copying and respooling messages."
926   :version "21.1"
927   :type 'boolean
928   :group 'gnus-summary-marks)
929
930 (defcustom gnus-alter-articles-to-read-function nil
931   "Function to be called to alter the list of articles to be selected."
932   :type '(choice (const nil) function)
933   :group 'gnus-summary)
934
935 (defcustom gnus-orphan-score nil
936   "*All orphans get this score added.  Set in the score file."
937   :group 'gnus-score-default
938   :type '(choice (const nil)
939                  integer))
940
941 (defcustom gnus-summary-save-parts-default-mime "image/.*"
942   "*A regexp to match MIME parts when saving multiple parts of a message
943 with gnus-summary-save-parts (X m). This regexp will be used by default
944 when prompting the user for which type of files to save."
945   :group 'gnus-summary
946   :type 'regexp)
947
948
949 (defcustom gnus-summary-save-parts-default-mime "image/.*"
950   "*A regexp to match MIME parts when saving multiple parts of a message
951 with gnus-summary-save-parts (X m). This regexp will be used by default
952 when prompting the user for which type of files to save."
953   :group 'gnus-summary
954   :type 'regexp)
955
956
957 ;;; Internal variables
958
959 (defvar gnus-article-mime-handles nil)
960 (defvar gnus-article-decoded-p nil)
961 (defvar gnus-article-charset nil)
962 (defvar gnus-article-ignored-charsets nil)
963 (defvar gnus-scores-exclude-files nil)
964 (defvar gnus-page-broken nil)
965 (defvar gnus-inhibit-mime-unbuttonizing nil)
966
967 (defvar gnus-original-article nil)
968 (defvar gnus-article-internal-prepare-hook nil)
969 (defvar gnus-newsgroup-process-stack nil)
970
971 (defvar gnus-thread-indent-array nil)
972 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
973 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
974   "Function called to sort the articles within a thread after it has been gathered together.")
975
976 (defvar gnus-summary-save-parts-type-history nil)
977 (defvar gnus-summary-save-parts-last-directory nil)
978
979 (defvar gnus-summary-save-parts-type-history nil)
980 (defvar gnus-summary-save-parts-last-directory nil)
981
982 ;; Avoid highlighting in kill files.
983 (defvar gnus-summary-inhibit-highlight nil)
984 (defvar gnus-newsgroup-selected-overlay nil)
985 (defvar gnus-inhibit-limiting nil)
986 (defvar gnus-newsgroup-adaptive-score-file nil)
987 (defvar gnus-current-score-file nil)
988 (defvar gnus-current-move-group nil)
989 (defvar gnus-current-copy-group nil)
990 (defvar gnus-current-crosspost-group nil)
991
992 (defvar gnus-newsgroup-dependencies nil)
993 (defvar gnus-newsgroup-adaptive nil)
994 (defvar gnus-summary-display-article-function nil)
995 (defvar gnus-summary-highlight-line-function nil
996   "Function called after highlighting a summary line.")
997
998 (defvar gnus-summary-line-format-alist
999   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1000     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1001     (?s gnus-tmp-subject-or-nil ?s)
1002     (?n gnus-tmp-name ?s)
1003     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1004         ?s)
1005     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1006             gnus-tmp-from) ?s)
1007     (?F gnus-tmp-from ?s)
1008     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1009     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1010     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1011     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1012     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1013     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1014     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1015     (?L gnus-tmp-lines ?s)
1016     (?I gnus-tmp-indentation ?s)
1017     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1018     (?R gnus-tmp-replied ?c)
1019     (?\[ gnus-tmp-opening-bracket ?c)
1020     (?\] gnus-tmp-closing-bracket ?c)
1021     (?\> (make-string gnus-tmp-level ? ) ?s)
1022     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1023     (?i gnus-tmp-score ?d)
1024     (?z gnus-tmp-score-char ?c)
1025     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1026     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1027     (?U gnus-tmp-unread ?c)
1028     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1029     (?t (gnus-summary-number-of-articles-in-thread
1030          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1031         ?d)
1032     (?e (gnus-summary-number-of-articles-in-thread
1033          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1034         ?c)
1035     (?u gnus-tmp-user-defined ?s)
1036     (?P (gnus-pick-line-number) ?d))
1037   "An alist of format specifications that can appear in summary lines.
1038 These are paired with what variables they correspond with, along with
1039 the type of the variable (string, integer, character, etc).")
1040
1041 (defvar gnus-summary-dummy-line-format-alist
1042   `((?S gnus-tmp-subject ?s)
1043     (?N gnus-tmp-number ?d)
1044     (?u gnus-tmp-user-defined ?s)))
1045
1046 (defvar gnus-summary-mode-line-format-alist
1047   `((?G gnus-tmp-group-name ?s)
1048     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1049     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1050     (?A gnus-tmp-article-number ?d)
1051     (?Z gnus-tmp-unread-and-unselected ?s)
1052     (?V gnus-version ?s)
1053     (?U gnus-tmp-unread-and-unticked ?d)
1054     (?S gnus-tmp-subject ?s)
1055     (?e gnus-tmp-unselected ?d)
1056     (?u gnus-tmp-user-defined ?s)
1057     (?d (length gnus-newsgroup-dormant) ?d)
1058     (?t (length gnus-newsgroup-marked) ?d)
1059     (?r (length gnus-newsgroup-reads) ?d)
1060     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1061     (?E gnus-newsgroup-expunged-tally ?d)
1062     (?s (gnus-current-score-file-nondirectory) ?s)))
1063
1064 (defvar gnus-last-search-regexp nil
1065   "Default regexp for article search command.")
1066
1067 (defvar gnus-last-shell-command nil
1068   "Default shell command on article.")
1069
1070 (defvar gnus-newsgroup-begin nil)
1071 (defvar gnus-newsgroup-end nil)
1072 (defvar gnus-newsgroup-last-rmail nil)
1073 (defvar gnus-newsgroup-last-mail nil)
1074 (defvar gnus-newsgroup-last-folder nil)
1075 (defvar gnus-newsgroup-last-file nil)
1076 (defvar gnus-newsgroup-auto-expire nil)
1077 (defvar gnus-newsgroup-active nil)
1078
1079 (defvar gnus-newsgroup-data nil)
1080 (defvar gnus-newsgroup-data-reverse nil)
1081 (defvar gnus-newsgroup-limit nil)
1082 (defvar gnus-newsgroup-limits nil)
1083
1084 (defvar gnus-newsgroup-unreads nil
1085   "List of unread articles in the current newsgroup.")
1086
1087 (defvar gnus-newsgroup-unselected nil
1088   "List of unselected unread articles in the current newsgroup.")
1089
1090 (defvar gnus-newsgroup-reads nil
1091   "Alist of read articles and article marks in the current newsgroup.")
1092
1093 (defvar gnus-newsgroup-expunged-tally nil)
1094
1095 (defvar gnus-newsgroup-marked nil
1096   "List of ticked articles in the current newsgroup (a subset of unread art).")
1097
1098 (defvar gnus-newsgroup-killed nil
1099   "List of ranges of articles that have been through the scoring process.")
1100
1101 (defvar gnus-newsgroup-cached nil
1102   "List of articles that come from the article cache.")
1103
1104 (defvar gnus-newsgroup-saved nil
1105   "List of articles that have been saved.")
1106
1107 (defvar gnus-newsgroup-kill-headers nil)
1108
1109 (defvar gnus-newsgroup-replied nil
1110   "List of articles that have been replied to in the current newsgroup.")
1111
1112 (defvar gnus-newsgroup-forwarded nil
1113   "List of articles that have been forwarded in the current newsgroup.")
1114
1115 (defvar gnus-newsgroup-expirable nil
1116   "List of articles in the current newsgroup that can be expired.")
1117
1118 (defvar gnus-newsgroup-processable nil
1119   "List of articles in the current newsgroup that can be processed.")
1120
1121 (defvar gnus-newsgroup-downloadable nil
1122   "List of articles in the current newsgroup that can be processed.")
1123
1124 (defvar gnus-newsgroup-undownloaded nil
1125   "List of articles in the current newsgroup that haven't been downloaded..")
1126
1127 (defvar gnus-newsgroup-unsendable nil
1128   "List of articles in the current newsgroup that won't be sent.")
1129
1130 (defvar gnus-newsgroup-bookmarks nil
1131   "List of articles in the current newsgroup that have bookmarks.")
1132
1133 (defvar gnus-newsgroup-dormant nil
1134   "List of dormant articles in the current newsgroup.")
1135
1136 (defvar gnus-newsgroup-scored nil
1137   "List of scored articles in the current newsgroup.")
1138
1139 (defvar gnus-newsgroup-headers nil
1140   "List of article headers in the current newsgroup.")
1141
1142 (defvar gnus-newsgroup-threads nil)
1143
1144 (defvar gnus-newsgroup-prepared nil
1145   "Whether the current group has been prepared properly.")
1146
1147 (defvar gnus-newsgroup-ancient nil
1148   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1149
1150 (defvar gnus-newsgroup-sparse nil)
1151
1152 (defvar gnus-current-article nil)
1153 (defvar gnus-article-current nil)
1154 (defvar gnus-current-headers nil)
1155 (defvar gnus-have-all-headers nil)
1156 (defvar gnus-last-article nil)
1157 (defvar gnus-newsgroup-history nil)
1158 (defvar gnus-newsgroup-charset nil)
1159 (defvar gnus-newsgroup-ephemeral-charset nil)
1160 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1161
1162 (defvar gnus-article-before-search nil)
1163
1164 (defconst gnus-summary-local-variables
1165   '(gnus-newsgroup-name
1166     gnus-newsgroup-begin gnus-newsgroup-end
1167     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1168     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1169     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1170     gnus-newsgroup-unselected gnus-newsgroup-marked
1171     gnus-newsgroup-reads gnus-newsgroup-saved
1172     gnus-newsgroup-replied gnus-newsgroup-forwarded
1173     gnus-newsgroup-expirable
1174     gnus-newsgroup-processable gnus-newsgroup-killed
1175     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1176     gnus-newsgroup-unsendable
1177     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1178     gnus-newsgroup-headers gnus-newsgroup-threads
1179     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1180     gnus-current-article gnus-current-headers gnus-have-all-headers
1181     gnus-last-article gnus-article-internal-prepare-hook
1182     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1183     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1184     gnus-thread-expunge-below
1185     gnus-score-alist gnus-current-score-file
1186     (gnus-summary-expunge-below . global)
1187     (gnus-summary-mark-below . global)
1188     (gnus-orphan-score . global)
1189     gnus-newsgroup-active gnus-scores-exclude-files
1190     gnus-newsgroup-history gnus-newsgroup-ancient
1191     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1192     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1193     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1194     (gnus-newsgroup-expunged-tally . 0)
1195     gnus-cache-removable-articles gnus-newsgroup-cached
1196     gnus-newsgroup-data gnus-newsgroup-data-reverse
1197     gnus-newsgroup-limit gnus-newsgroup-limits
1198     gnus-newsgroup-charset)
1199   "Variables that are buffer-local to the summary buffers.")
1200
1201 (defvar gnus-newsgroup-variables nil
1202   "Variables that have separate values in the newsgroups.")
1203
1204 ;; Byte-compiler warning.
1205 (eval-when-compile (defvar gnus-article-mode-map))
1206
1207 ;; MIME stuff.
1208
1209 (defvar gnus-decode-encoded-word-methods
1210   '(mail-decode-encoded-word-string)
1211   "List of methods used to decode encoded words.
1212
1213 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1214 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1215 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1216 whose names match REGEXP.
1217
1218 For example:
1219 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1220  mail-decode-encoded-word-string
1221  (\"chinese\" . rfc1843-decode-string))")
1222
1223 (defvar gnus-decode-encoded-word-methods-cache nil)
1224
1225 (defun gnus-multi-decode-encoded-word-string (string)
1226   "Apply the functions from `gnus-encoded-word-methods' that match."
1227   (unless (and gnus-decode-encoded-word-methods-cache
1228                (eq gnus-newsgroup-name
1229                    (car gnus-decode-encoded-word-methods-cache)))
1230     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1231     (mapcar (lambda (x)
1232               (if (symbolp x)
1233                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1234                 (if (and gnus-newsgroup-name
1235                          (string-match (car x) gnus-newsgroup-name))
1236                     (nconc gnus-decode-encoded-word-methods-cache
1237                            (list (cdr x))))))
1238           gnus-decode-encoded-word-methods))
1239   (let ((xlist gnus-decode-encoded-word-methods-cache))
1240     (pop xlist)
1241     (while xlist
1242       (setq string (funcall (pop xlist) string))))
1243   string)
1244
1245 ;; Subject simplification.
1246
1247 (defun gnus-simplify-whitespace (str)
1248   "Remove excessive whitespace from STR."
1249   (let ((mystr str))
1250     ;; Multiple spaces.
1251     (while (string-match "[ \t][ \t]+" mystr)
1252       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1253                           " "
1254                           (substring mystr (match-end 0)))))
1255     ;; Leading spaces.
1256     (when (string-match "^[ \t]+" mystr)
1257       (setq mystr (substring mystr (match-end 0))))
1258     ;; Trailing spaces.
1259     (when (string-match "[ \t]+$" mystr)
1260       (setq mystr (substring mystr 0 (match-beginning 0))))
1261     mystr))
1262
1263 (defsubst gnus-simplify-subject-re (subject)
1264   "Remove \"Re:\" from subject lines."
1265   (if (string-match message-subject-re-regexp subject)
1266       (substring subject (match-end 0))
1267     subject))
1268
1269 (defun gnus-simplify-subject (subject &optional re-only)
1270   "Remove `Re:' and words in parentheses.
1271 If RE-ONLY is non-nil, strip leading `Re:'s only."
1272   (let ((case-fold-search t))           ;Ignore case.
1273     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1274     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1275       (setq subject (substring subject (match-end 0))))
1276     ;; Remove uninteresting prefixes.
1277     (when (and (not re-only)
1278                gnus-simplify-ignored-prefixes
1279                (string-match gnus-simplify-ignored-prefixes subject))
1280       (setq subject (substring subject (match-end 0))))
1281     ;; Remove words in parentheses from end.
1282     (unless re-only
1283       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1284         (setq subject (substring subject 0 (match-beginning 0)))))
1285     ;; Return subject string.
1286     subject))
1287
1288 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1289 ;; all whitespace.
1290 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1291   (goto-char (point-min))
1292   (while (re-search-forward regexp nil t)
1293     (replace-match (or newtext ""))))
1294
1295 (defun gnus-simplify-buffer-fuzzy ()
1296   "Simplify string in the buffer fuzzily.
1297 The string in the accessible portion of the current buffer is simplified.
1298 It is assumed to be a single-line subject.
1299 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1300 matter is removed.  Additional things can be deleted by setting
1301 `gnus-simplify-subject-fuzzy-regexp'."
1302   (let ((case-fold-search t)
1303         (modified-tick))
1304     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1305
1306     (while (not (eq modified-tick (buffer-modified-tick)))
1307       (setq modified-tick (buffer-modified-tick))
1308       (cond
1309        ((listp gnus-simplify-subject-fuzzy-regexp)
1310         (mapcar 'gnus-simplify-buffer-fuzzy-step
1311                 gnus-simplify-subject-fuzzy-regexp))
1312        (gnus-simplify-subject-fuzzy-regexp
1313         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1314       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1315       (gnus-simplify-buffer-fuzzy-step
1316        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1317       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1318
1319     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1320     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1321     (gnus-simplify-buffer-fuzzy-step " $")
1322     (gnus-simplify-buffer-fuzzy-step "^ +")))
1323
1324 (defun gnus-simplify-subject-fuzzy (subject)
1325   "Simplify a subject string fuzzily.
1326 See `gnus-simplify-buffer-fuzzy' for details."
1327   (save-excursion
1328     (gnus-set-work-buffer)
1329     (let ((case-fold-search t))
1330       ;; Remove uninteresting prefixes.
1331       (when (and gnus-simplify-ignored-prefixes
1332                  (string-match gnus-simplify-ignored-prefixes subject))
1333         (setq subject (substring subject (match-end 0))))
1334       (insert subject)
1335       (inline (gnus-simplify-buffer-fuzzy))
1336       (buffer-string))))
1337
1338 (defsubst gnus-simplify-subject-fully (subject)
1339   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1340   (cond
1341    (gnus-simplify-subject-functions
1342     (gnus-map-function gnus-simplify-subject-functions subject))
1343    ((null gnus-summary-gather-subject-limit)
1344     (gnus-simplify-subject-re subject))
1345    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1346     (gnus-simplify-subject-fuzzy subject))
1347    ((numberp gnus-summary-gather-subject-limit)
1348     (gnus-limit-string (gnus-simplify-subject-re subject)
1349                        gnus-summary-gather-subject-limit))
1350    (t
1351     subject)))
1352
1353 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1354   "Check whether two subjects are equal.
1355 If optional argument simple-first is t, first argument is already
1356 simplified."
1357   (cond
1358    ((null simple-first)
1359     (equal (gnus-simplify-subject-fully s1)
1360            (gnus-simplify-subject-fully s2)))
1361    (t
1362     (equal s1
1363            (gnus-simplify-subject-fully s2)))))
1364
1365 (defun gnus-summary-bubble-group ()
1366   "Increase the score of the current group.
1367 This is a handy function to add to `gnus-summary-exit-hook' to
1368 increase the score of each group you read."
1369   (gnus-group-add-score gnus-newsgroup-name))
1370
1371 \f
1372 ;;;
1373 ;;; Gnus summary mode
1374 ;;;
1375
1376 (put 'gnus-summary-mode 'mode-class 'special)
1377
1378 (defvar gnus-article-commands-menu)
1379
1380 (when t
1381   ;; Non-orthogonal keys
1382
1383   (gnus-define-keys gnus-summary-mode-map
1384     " " gnus-summary-next-page
1385     "\177" gnus-summary-prev-page
1386     [delete] gnus-summary-prev-page
1387     [backspace] gnus-summary-prev-page
1388     "\r" gnus-summary-scroll-up
1389     "\M-\r" gnus-summary-scroll-down
1390     "n" gnus-summary-next-unread-article
1391     "p" gnus-summary-prev-unread-article
1392     "N" gnus-summary-next-article
1393     "P" gnus-summary-prev-article
1394     "\M-\C-n" gnus-summary-next-same-subject
1395     "\M-\C-p" gnus-summary-prev-same-subject
1396     "\M-n" gnus-summary-next-unread-subject
1397     "\M-p" gnus-summary-prev-unread-subject
1398     "." gnus-summary-first-unread-article
1399     "," gnus-summary-best-unread-article
1400     "\M-s" gnus-summary-search-article-forward
1401     "\M-r" gnus-summary-search-article-backward
1402     "<" gnus-summary-beginning-of-article
1403     ">" gnus-summary-end-of-article
1404     "j" gnus-summary-goto-article
1405     "^" gnus-summary-refer-parent-article
1406     "\M-^" gnus-summary-refer-article
1407     "u" gnus-summary-tick-article-forward
1408     "!" gnus-summary-tick-article-forward
1409     "U" gnus-summary-tick-article-backward
1410     "d" gnus-summary-mark-as-read-forward
1411     "D" gnus-summary-mark-as-read-backward
1412     "E" gnus-summary-mark-as-expirable
1413     "\M-u" gnus-summary-clear-mark-forward
1414     "\M-U" gnus-summary-clear-mark-backward
1415     "k" gnus-summary-kill-same-subject-and-select
1416     "\C-k" gnus-summary-kill-same-subject
1417     "\M-\C-k" gnus-summary-kill-thread
1418     "\M-\C-l" gnus-summary-lower-thread
1419     "e" gnus-summary-edit-article
1420     "#" gnus-summary-mark-as-processable
1421     "\M-#" gnus-summary-unmark-as-processable
1422     "\M-\C-t" gnus-summary-toggle-threads
1423     "\M-\C-s" gnus-summary-show-thread
1424     "\M-\C-h" gnus-summary-hide-thread
1425     "\M-\C-f" gnus-summary-next-thread
1426     "\M-\C-b" gnus-summary-prev-thread
1427     [(meta down)] gnus-summary-next-thread
1428     [(meta up)] gnus-summary-prev-thread
1429     "\M-\C-u" gnus-summary-up-thread
1430     "\M-\C-d" gnus-summary-down-thread
1431     "&" gnus-summary-execute-command
1432     "c" gnus-summary-catchup-and-exit
1433     "\C-w" gnus-summary-mark-region-as-read
1434     "\C-t" gnus-summary-toggle-truncation
1435     "?" gnus-summary-mark-as-dormant
1436     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1437     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1438     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1439     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1440     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1441     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1442     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1443     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1444     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1445     "=" gnus-summary-expand-window
1446     "\C-x\C-s" gnus-summary-reselect-current-group
1447     "\M-g" gnus-summary-rescan-group
1448     "w" gnus-summary-stop-page-breaking
1449     "\C-c\C-r" gnus-summary-caesar-message
1450     "f" gnus-summary-followup
1451     "F" gnus-summary-followup-with-original
1452     "C" gnus-summary-cancel-article
1453     "r" gnus-summary-reply
1454     "R" gnus-summary-reply-with-original
1455     "\C-c\C-f" gnus-summary-mail-forward
1456     "o" gnus-summary-save-article
1457     "\C-o" gnus-summary-save-article-mail
1458     "|" gnus-summary-pipe-output
1459     "\M-k" gnus-summary-edit-local-kill
1460     "\M-K" gnus-summary-edit-global-kill
1461     ;; "V" gnus-version
1462     "\C-c\C-d" gnus-summary-describe-group
1463     "q" gnus-summary-exit
1464     "Q" gnus-summary-exit-no-update
1465     "\C-c\C-i" gnus-info-find-node
1466     gnus-mouse-2 gnus-mouse-pick-article
1467     "m" gnus-summary-mail-other-window
1468     "a" gnus-summary-post-news
1469     "x" gnus-summary-limit-to-unread
1470     "s" gnus-summary-isearch-article
1471     "t" gnus-summary-toggle-header
1472     "g" gnus-summary-show-article
1473     "l" gnus-summary-goto-last-article
1474     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1475     "\C-d" gnus-summary-enter-digest-group
1476     "\M-\C-d" gnus-summary-read-document
1477     "\M-\C-e" gnus-summary-edit-parameters
1478     "\M-\C-a" gnus-summary-customize-parameters
1479     "\C-c\C-b" gnus-bug
1480     "*" gnus-cache-enter-article
1481     "\M-*" gnus-cache-remove-article
1482     "\M-&" gnus-summary-universal-argument
1483     "\C-l" gnus-recenter
1484     "I" gnus-summary-increase-score
1485     "L" gnus-summary-lower-score
1486     "\M-i" gnus-symbolic-argument
1487     "h" gnus-summary-select-article-buffer
1488
1489     "b" gnus-article-view-part
1490     "\M-t" gnus-summary-toggle-display-buttonized
1491
1492     "V" gnus-summary-score-map
1493     "X" gnus-uu-extract-map
1494     "S" gnus-summary-send-map)
1495
1496   ;; Sort of orthogonal keymap
1497   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1498     "t" gnus-summary-tick-article-forward
1499     "!" gnus-summary-tick-article-forward
1500     "d" gnus-summary-mark-as-read-forward
1501     "r" gnus-summary-mark-as-read-forward
1502     "c" gnus-summary-clear-mark-forward
1503     " " gnus-summary-clear-mark-forward
1504     "e" gnus-summary-mark-as-expirable
1505     "x" gnus-summary-mark-as-expirable
1506     "?" gnus-summary-mark-as-dormant
1507     "b" gnus-summary-set-bookmark
1508     "B" gnus-summary-remove-bookmark
1509     "#" gnus-summary-mark-as-processable
1510     "\M-#" gnus-summary-unmark-as-processable
1511     "S" gnus-summary-limit-include-expunged
1512     "C" gnus-summary-catchup
1513     "H" gnus-summary-catchup-to-here
1514     "\C-c" gnus-summary-catchup-all
1515     "k" gnus-summary-kill-same-subject-and-select
1516     "K" gnus-summary-kill-same-subject
1517     "P" gnus-uu-mark-map)
1518
1519   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1520     "c" gnus-summary-clear-above
1521     "u" gnus-summary-tick-above
1522     "m" gnus-summary-mark-above
1523     "k" gnus-summary-kill-below)
1524
1525   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1526     "/" gnus-summary-limit-to-subject
1527     "n" gnus-summary-limit-to-articles
1528     "w" gnus-summary-pop-limit
1529     "s" gnus-summary-limit-to-subject
1530     "a" gnus-summary-limit-to-author
1531     "u" gnus-summary-limit-to-unread
1532     "m" gnus-summary-limit-to-marks
1533     "M" gnus-summary-limit-exclude-marks
1534     "v" gnus-summary-limit-to-score
1535     "*" gnus-summary-limit-include-cached
1536     "D" gnus-summary-limit-include-dormant
1537     "T" gnus-summary-limit-include-thread
1538     "d" gnus-summary-limit-exclude-dormant
1539     "t" gnus-summary-limit-to-age
1540     "x" gnus-summary-limit-to-extra
1541     "E" gnus-summary-limit-include-expunged
1542     "c" gnus-summary-limit-exclude-childless-dormant
1543     "C" gnus-summary-limit-mark-excluded-as-read
1544     "o" gnus-summary-insert-old-articles
1545     "N" gnus-summary-insert-new-articles)
1546
1547   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1548     "n" gnus-summary-next-unread-article
1549     "p" gnus-summary-prev-unread-article
1550     "N" gnus-summary-next-article
1551     "P" gnus-summary-prev-article
1552     "\C-n" gnus-summary-next-same-subject
1553     "\C-p" gnus-summary-prev-same-subject
1554     "\M-n" gnus-summary-next-unread-subject
1555     "\M-p" gnus-summary-prev-unread-subject
1556     "f" gnus-summary-first-unread-article
1557     "b" gnus-summary-best-unread-article
1558     "j" gnus-summary-goto-article
1559     "g" gnus-summary-goto-subject
1560     "l" gnus-summary-goto-last-article
1561     "o" gnus-summary-pop-article)
1562
1563   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1564     "k" gnus-summary-kill-thread
1565     "l" gnus-summary-lower-thread
1566     "i" gnus-summary-raise-thread
1567     "T" gnus-summary-toggle-threads
1568     "t" gnus-summary-rethread-current
1569     "^" gnus-summary-reparent-thread
1570     "s" gnus-summary-show-thread
1571     "S" gnus-summary-show-all-threads
1572     "h" gnus-summary-hide-thread
1573     "H" gnus-summary-hide-all-threads
1574     "n" gnus-summary-next-thread
1575     "p" gnus-summary-prev-thread
1576     "u" gnus-summary-up-thread
1577     "o" gnus-summary-top-thread
1578     "d" gnus-summary-down-thread
1579     "#" gnus-uu-mark-thread
1580     "\M-#" gnus-uu-unmark-thread)
1581
1582   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1583     "g" gnus-summary-prepare
1584     "c" gnus-summary-insert-cached-articles)
1585
1586   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1587     "c" gnus-summary-catchup-and-exit
1588     "C" gnus-summary-catchup-all-and-exit
1589     "E" gnus-summary-exit-no-update
1590     "Q" gnus-summary-exit
1591     "Z" gnus-summary-exit
1592     "n" gnus-summary-catchup-and-goto-next-group
1593     "R" gnus-summary-reselect-current-group
1594     "G" gnus-summary-rescan-group
1595     "N" gnus-summary-next-group
1596     "s" gnus-summary-save-newsrc
1597     "P" gnus-summary-prev-group)
1598
1599   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1600     " " gnus-summary-next-page
1601     "n" gnus-summary-next-page
1602     "\177" gnus-summary-prev-page
1603     [delete] gnus-summary-prev-page
1604     "p" gnus-summary-prev-page
1605     "\r" gnus-summary-scroll-up
1606     "\M-\r" gnus-summary-scroll-down
1607     "<" gnus-summary-beginning-of-article
1608     ">" gnus-summary-end-of-article
1609     "b" gnus-summary-beginning-of-article
1610     "e" gnus-summary-end-of-article
1611     "^" gnus-summary-refer-parent-article
1612     "r" gnus-summary-refer-parent-article
1613     "D" gnus-summary-enter-digest-group
1614     "R" gnus-summary-refer-references
1615     "T" gnus-summary-refer-thread
1616     "g" gnus-summary-show-article
1617     "s" gnus-summary-isearch-article
1618     "P" gnus-summary-print-article
1619     "M" gnus-mailing-list-insinuate
1620     "t" gnus-article-babel)
1621
1622   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1623     "b" gnus-article-add-buttons
1624     "B" gnus-article-add-buttons-to-head
1625     "o" gnus-article-treat-overstrike
1626     "e" gnus-article-emphasize
1627     "w" gnus-article-fill-cited-article
1628     "Q" gnus-article-fill-long-lines
1629     "C" gnus-article-capitalize-sentences
1630     "c" gnus-article-remove-cr
1631     "q" gnus-article-de-quoted-unreadable
1632     "6" gnus-article-de-base64-unreadable
1633     "Z" gnus-article-decode-HZ
1634     "h" gnus-article-wash-html
1635     "s" gnus-summary-force-verify-and-decrypt
1636     "f" gnus-article-display-x-face
1637     "l" gnus-summary-stop-page-breaking
1638     "r" gnus-summary-caesar-message
1639     "t" gnus-summary-toggle-header
1640     "v" gnus-summary-verbose-headers
1641     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1642     "p" gnus-article-verify-x-pgp-sig
1643     "d" gnus-article-treat-dumbquotes)
1644
1645   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1646     "a" gnus-article-hide
1647     "h" gnus-article-hide-headers
1648     "b" gnus-article-hide-boring-headers
1649     "s" gnus-article-hide-signature
1650     "c" gnus-article-hide-citation
1651     "C" gnus-article-hide-citation-in-followups
1652     "l" gnus-article-hide-list-identifiers
1653     "p" gnus-article-hide-pgp
1654     "B" gnus-article-strip-banner
1655     "P" gnus-article-hide-pem
1656     "\C-c" gnus-article-hide-citation-maybe)
1657
1658   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1659     "a" gnus-article-highlight
1660     "h" gnus-article-highlight-headers
1661     "c" gnus-article-highlight-citation
1662     "s" gnus-article-highlight-signature)
1663
1664   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1665     "w" gnus-article-decode-mime-words
1666     "c" gnus-article-decode-charset
1667     "v" gnus-mime-view-all-parts
1668     "b" gnus-article-view-part)
1669
1670   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1671     "z" gnus-article-date-ut
1672     "u" gnus-article-date-ut
1673     "l" gnus-article-date-local
1674     "p" gnus-article-date-english
1675     "e" gnus-article-date-lapsed
1676     "o" gnus-article-date-original
1677     "i" gnus-article-date-iso8601
1678     "s" gnus-article-date-user)
1679
1680   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1681     "t" gnus-article-remove-trailing-blank-lines
1682     "l" gnus-article-strip-leading-blank-lines
1683     "m" gnus-article-strip-multiple-blank-lines
1684     "a" gnus-article-strip-blank-lines
1685     "A" gnus-article-strip-all-blank-lines
1686     "s" gnus-article-strip-leading-space
1687     "e" gnus-article-strip-trailing-space
1688     "w" gnus-article-remove-leading-whitespace)
1689
1690   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1691     "v" gnus-version
1692     "f" gnus-summary-fetch-faq
1693     "d" gnus-summary-describe-group
1694     "h" gnus-summary-describe-briefly
1695     "i" gnus-info-find-node)
1696
1697   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1698     "e" gnus-summary-expire-articles
1699     "\M-\C-e" gnus-summary-expire-articles-now
1700     "\177" gnus-summary-delete-article
1701     [delete] gnus-summary-delete-article
1702     [backspace] gnus-summary-delete-article
1703     "m" gnus-summary-move-article
1704     "r" gnus-summary-respool-article
1705     "w" gnus-summary-edit-article
1706     "c" gnus-summary-copy-article
1707     "B" gnus-summary-crosspost-article
1708     "q" gnus-summary-respool-query
1709     "t" gnus-summary-respool-trace
1710     "i" gnus-summary-import-article
1711     "I" gnus-summary-create-article
1712     "p" gnus-summary-article-posted-p)
1713
1714   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1715     "o" gnus-summary-save-article
1716     "m" gnus-summary-save-article-mail
1717     "F" gnus-summary-write-article-file
1718     "r" gnus-summary-save-article-rmail
1719     "f" gnus-summary-save-article-file
1720     "b" gnus-summary-save-article-body-file
1721     "h" gnus-summary-save-article-folder
1722     "v" gnus-summary-save-article-vm
1723     "p" gnus-summary-pipe-output
1724     "s" gnus-soup-add-article)
1725
1726   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1727     "b" gnus-summary-display-buttonized
1728     "m" gnus-summary-repair-multipart
1729     "v" gnus-article-view-part
1730     "o" gnus-article-save-part
1731     "c" gnus-article-copy-part
1732     "C" gnus-article-view-part-as-charset
1733     "e" gnus-article-externalize-part
1734     "E" gnus-article-encrypt-body
1735     "i" gnus-article-inline-part
1736     "|" gnus-article-pipe-part))
1737
1738 (defvar gnus-article-post-menu nil)
1739
1740 (defun gnus-summary-make-menu-bar ()
1741   (gnus-turn-off-edit-menu 'summary)
1742
1743   (unless (boundp 'gnus-summary-misc-menu)
1744
1745     (easy-menu-define
1746      gnus-summary-kill-menu gnus-summary-mode-map ""
1747      (cons
1748       "Score"
1749       (nconc
1750        (list
1751         ["Customize" gnus-score-customize t])
1752        (gnus-make-score-map 'increase)
1753        (gnus-make-score-map 'lower)
1754        '(("Mark"
1755           ["Kill below" gnus-summary-kill-below t]
1756           ["Mark above" gnus-summary-mark-above t]
1757           ["Tick above" gnus-summary-tick-above t]
1758           ["Clear above" gnus-summary-clear-above t])
1759          ["Current score" gnus-summary-current-score t]
1760          ["Set score" gnus-summary-set-score t]
1761          ["Switch current score file..." gnus-score-change-score-file t]
1762          ["Set mark below..." gnus-score-set-mark-below t]
1763          ["Set expunge below..." gnus-score-set-expunge-below t]
1764          ["Edit current score file" gnus-score-edit-current-scores t]
1765          ["Edit score file" gnus-score-edit-file t]
1766          ["Trace score" gnus-score-find-trace t]
1767          ["Find words" gnus-score-find-favourite-words t]
1768          ["Rescore buffer" gnus-summary-rescore t]
1769          ["Increase score..." gnus-summary-increase-score t]
1770          ["Lower score..." gnus-summary-lower-score t]))))
1771
1772     ;; Define both the Article menu in the summary buffer and the equivalent
1773     ;; Commands menu in the article buffer here for consistency.
1774     (let ((innards
1775            `(("Hide"
1776               ["All" gnus-article-hide t]
1777               ["Headers" gnus-article-hide-headers t]
1778               ["Signature" gnus-article-hide-signature t]
1779               ["Citation" gnus-article-hide-citation t]
1780               ["List identifiers" gnus-article-hide-list-identifiers t]
1781               ["PGP" gnus-article-hide-pgp t]
1782               ["Banner" gnus-article-strip-banner t]
1783               ["Boring headers" gnus-article-hide-boring-headers t])
1784              ("Highlight"
1785               ["All" gnus-article-highlight t]
1786               ["Headers" gnus-article-highlight-headers t]
1787               ["Signature" gnus-article-highlight-signature t]
1788               ["Citation" gnus-article-highlight-citation t])
1789              ("MIME"
1790               ["Words" gnus-article-decode-mime-words t]
1791               ["Charset" gnus-article-decode-charset t]
1792               ["QP" gnus-article-de-quoted-unreadable t]
1793               ["Base64" gnus-article-de-base64-unreadable t]
1794               ["View all" gnus-mime-view-all-parts t]
1795               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
1796               ["Encrypt body" gnus-article-encrypt-body t])
1797              ("Date"
1798               ["Local" gnus-article-date-local t]
1799               ["ISO8601" gnus-article-date-iso8601 t]
1800               ["UT" gnus-article-date-ut t]
1801               ["Original" gnus-article-date-original t]
1802               ["Lapsed" gnus-article-date-lapsed t]
1803               ["User-defined" gnus-article-date-user t])
1804              ("Washing"
1805               ("Remove Blanks"
1806                ["Leading" gnus-article-strip-leading-blank-lines t]
1807                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1808                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1809                ["All of the above" gnus-article-strip-blank-lines t]
1810                ["All" gnus-article-strip-all-blank-lines t]
1811                ["Leading space" gnus-article-strip-leading-space t]
1812                ["Trailing space" gnus-article-strip-trailing-space t]
1813                ["Leading space in headers" 
1814                 gnus-article-remove-leading-whitespace t])
1815               ["Overstrike" gnus-article-treat-overstrike t]
1816               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1817               ["Emphasis" gnus-article-emphasize t]
1818               ["Word wrap" gnus-article-fill-cited-article t]
1819               ["Fill long lines" gnus-article-fill-long-lines t]
1820               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1821               ["CR" gnus-article-remove-cr t]
1822               ["Show X-Face" gnus-article-display-x-face t]
1823               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1824               ["Base64" gnus-article-de-base64-unreadable t]
1825               ["Rot 13" gnus-summary-caesar-message
1826                ,@(if (featurep 'xemacs) '(t)
1827                    '(:help "\"Caesar rotate\" article by 13"))]
1828               ["Unix pipe" gnus-summary-pipe-message t]
1829               ["Add buttons" gnus-article-add-buttons t]
1830               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1831               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1832               ["Verbose header" gnus-summary-verbose-headers t]
1833               ["Toggle header" gnus-summary-toggle-header t]
1834               ["Html" gnus-article-wash-html t]
1835               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1836               ["HZ" gnus-article-decode-HZ t])
1837              ("Output"
1838               ["Save in default format" gnus-summary-save-article
1839                ,@(if (featurep 'xemacs) '(t)
1840                    '(:help "Save article using default method"))]
1841               ["Save in file" gnus-summary-save-article-file
1842                ,@(if (featurep 'xemacs) '(t)
1843                    '(:help "Save article in file"))]
1844               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1845               ["Save in MH folder" gnus-summary-save-article-folder t]
1846               ["Save in VM folder" gnus-summary-save-article-vm t]
1847               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1848               ["Save body in file" gnus-summary-save-article-body-file t]
1849               ["Pipe through a filter" gnus-summary-pipe-output t]
1850               ["Add to SOUP packet" gnus-soup-add-article t]
1851               ["Print" gnus-summary-print-article t])
1852              ("Backend"
1853               ["Respool article..." gnus-summary-respool-article t]
1854               ["Move article..." gnus-summary-move-article
1855                (gnus-check-backend-function
1856                 'request-move-article gnus-newsgroup-name)]
1857               ["Copy article..." gnus-summary-copy-article t]
1858               ["Crosspost article..." gnus-summary-crosspost-article
1859                (gnus-check-backend-function
1860                 'request-replace-article gnus-newsgroup-name)]
1861               ["Import file..." gnus-summary-import-article t]
1862               ["Create article..." gnus-summary-create-article t]
1863               ["Check if posted" gnus-summary-article-posted-p t]
1864               ["Edit article" gnus-summary-edit-article
1865                (not (gnus-group-read-only-p))]
1866               ["Delete article" gnus-summary-delete-article
1867                (gnus-check-backend-function
1868                 'request-expire-articles gnus-newsgroup-name)]
1869               ["Query respool" gnus-summary-respool-query t]
1870               ["Trace respool" gnus-summary-respool-trace t]
1871               ["Delete expirable articles" gnus-summary-expire-articles-now
1872                (gnus-check-backend-function
1873                 'request-expire-articles gnus-newsgroup-name)])
1874              ("Extract"
1875               ["Uudecode" gnus-uu-decode-uu
1876                ,@(if (featurep 'xemacs) '(t)
1877                    '(:help "Decode uuencoded article(s)"))]
1878               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1879               ["Unshar" gnus-uu-decode-unshar t]
1880               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1881               ["Save" gnus-uu-decode-save t]
1882               ["Binhex" gnus-uu-decode-binhex t]
1883               ["Postscript" gnus-uu-decode-postscript t])
1884              ("Cache"
1885               ["Enter article" gnus-cache-enter-article t]
1886               ["Remove article" gnus-cache-remove-article t])
1887              ["Translate" gnus-article-babel t]
1888              ["Select article buffer" gnus-summary-select-article-buffer t]
1889              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1890              ["Isearch article..." gnus-summary-isearch-article t]
1891              ["Beginning of the article" gnus-summary-beginning-of-article t]
1892              ["End of the article" gnus-summary-end-of-article t]
1893              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1894              ["Fetch referenced articles" gnus-summary-refer-references t]
1895              ["Fetch current thread" gnus-summary-refer-thread t]
1896              ["Fetch article with id..." gnus-summary-refer-article t]
1897              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1898              ["Redisplay" gnus-summary-show-article t])))
1899       (easy-menu-define
1900        gnus-summary-article-menu gnus-summary-mode-map ""
1901        (cons "Article" innards))
1902
1903       (if (not (keymapp gnus-summary-article-menu))
1904           (easy-menu-define
1905             gnus-article-commands-menu gnus-article-mode-map ""
1906             (cons "Commands" innards))
1907         ;; in Emacs, don't share menu.
1908         (setq gnus-article-commands-menu 
1909               (copy-keymap gnus-summary-article-menu))
1910         (define-key gnus-article-mode-map [menu-bar commands]
1911           (cons "Commands" gnus-article-commands-menu))))
1912
1913     (easy-menu-define
1914      gnus-summary-thread-menu gnus-summary-mode-map ""
1915      '("Threads"
1916        ["Toggle threading" gnus-summary-toggle-threads t]
1917        ["Hide threads" gnus-summary-hide-all-threads t]
1918        ["Show threads" gnus-summary-show-all-threads t]
1919        ["Hide thread" gnus-summary-hide-thread t]
1920        ["Show thread" gnus-summary-show-thread t]
1921        ["Go to next thread" gnus-summary-next-thread t]
1922        ["Go to previous thread" gnus-summary-prev-thread t]
1923        ["Go down thread" gnus-summary-down-thread t]
1924        ["Go up thread" gnus-summary-up-thread t]
1925        ["Top of thread" gnus-summary-top-thread t]
1926        ["Mark thread as read" gnus-summary-kill-thread t]
1927        ["Lower thread score" gnus-summary-lower-thread t]
1928        ["Raise thread score" gnus-summary-raise-thread t]
1929        ["Rethread current" gnus-summary-rethread-current t]))
1930
1931     (easy-menu-define
1932      gnus-summary-post-menu gnus-summary-mode-map ""
1933      `("Post"
1934        ["Post an article" gnus-summary-post-news
1935         ,@(if (featurep 'xemacs) '(t)
1936             '(:help "Post an article"))]
1937        ["Followup" gnus-summary-followup
1938         ,@(if (featurep 'xemacs) '(t)
1939             '(:help "Post followup to this article"))]
1940        ["Followup and yank" gnus-summary-followup-with-original
1941         ,@(if (featurep 'xemacs) '(t)
1942             '(:help "Post followup to this article, quoting its contents"))]
1943        ["Supersede article" gnus-summary-supersede-article t]
1944        ["Cancel article" gnus-summary-cancel-article
1945         ,@(if (featurep 'xemacs) '(t)
1946             '(:help "Cancel an article you posted"))]
1947        ["Reply" gnus-summary-reply t]
1948        ["Reply and yank" gnus-summary-reply-with-original t]
1949        ["Wide reply" gnus-summary-wide-reply t]
1950        ["Wide reply and yank" gnus-summary-wide-reply-with-original
1951         ,@(if (featurep 'xemacs) '(t)
1952             '(:help "Mail a reply, quoting this article"))]
1953        ["Mail forward" gnus-summary-mail-forward t]
1954        ["Post forward" gnus-summary-post-forward t]
1955        ["Digest and mail" gnus-uu-digest-mail-forward t]
1956        ["Digest and post" gnus-uu-digest-post-forward t]
1957        ["Resend message" gnus-summary-resend-message t]
1958        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1959        ["Send a mail" gnus-summary-mail-other-window t]
1960        ["Uuencode and post" gnus-uu-post-news
1961         ,@(if (featurep 'xemacs) '(t)
1962             '(:help "Post a uuencoded article"))]
1963        ["Followup via news" gnus-summary-followup-to-mail t]
1964        ["Followup via news and yank"
1965         gnus-summary-followup-to-mail-with-original t]
1966        ;;("Draft"
1967        ;;["Send" gnus-summary-send-draft t]
1968        ;;["Send bounced" gnus-resend-bounced-mail t])
1969        ))
1970
1971     (cond 
1972      ((not (keymapp gnus-summary-post-menu))
1973       (setq gnus-article-post-menu gnus-summary-post-menu))
1974      ((not gnus-article-post-menu)
1975       ;; Don't share post menu.
1976       (setq gnus-article-post-menu
1977             (copy-keymap gnus-summary-post-menu))))
1978     (define-key gnus-article-mode-map [menu-bar post]
1979       (cons "Post" gnus-article-post-menu))
1980
1981     (easy-menu-define
1982      gnus-summary-misc-menu gnus-summary-mode-map ""
1983      `("Misc"
1984        ("Mark Read"
1985         ["Mark as read" gnus-summary-mark-as-read-forward t]
1986         ["Mark same subject and select"
1987          gnus-summary-kill-same-subject-and-select t]
1988         ["Mark same subject" gnus-summary-kill-same-subject t]
1989         ["Catchup" gnus-summary-catchup
1990          ,@(if (featurep 'xemacs) '(t)
1991              '(:help "Mark unread articles in this group as read"))]
1992         ["Catchup all" gnus-summary-catchup-all t]
1993         ["Catchup to here" gnus-summary-catchup-to-here t]
1994         ["Catchup region" gnus-summary-mark-region-as-read t]
1995         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1996        ("Mark Various"
1997         ["Tick" gnus-summary-tick-article-forward t]
1998         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1999         ["Remove marks" gnus-summary-clear-mark-forward t]
2000         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2001         ["Set bookmark" gnus-summary-set-bookmark t]
2002         ["Remove bookmark" gnus-summary-remove-bookmark t])
2003        ("Mark Limit"
2004         ["Marks..." gnus-summary-limit-to-marks t]
2005         ["Subject..." gnus-summary-limit-to-subject t]
2006         ["Author..." gnus-summary-limit-to-author t]
2007         ["Age..." gnus-summary-limit-to-age t]
2008         ["Extra..." gnus-summary-limit-to-extra t]
2009         ["Score" gnus-summary-limit-to-score t]
2010         ["Unread" gnus-summary-limit-to-unread t]
2011         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2012         ["Articles" gnus-summary-limit-to-articles t]
2013         ["Pop limit" gnus-summary-pop-limit t]
2014         ["Show dormant" gnus-summary-limit-include-dormant t]
2015         ["Hide childless dormant"
2016          gnus-summary-limit-exclude-childless-dormant t]
2017         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2018         ["Hide marked" gnus-summary-limit-exclude-marks t]
2019         ["Show expunged" gnus-summary-limit-include-expunged t])
2020        ("Process Mark"
2021         ["Set mark" gnus-summary-mark-as-processable t]
2022         ["Remove mark" gnus-summary-unmark-as-processable t]
2023         ["Remove all marks" gnus-summary-unmark-all-processable t]
2024         ["Mark above" gnus-uu-mark-over t]
2025         ["Mark series" gnus-uu-mark-series t]
2026         ["Mark region" gnus-uu-mark-region t]
2027         ["Unmark region" gnus-uu-unmark-region t]
2028         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2029         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2030         ["Mark all" gnus-uu-mark-all t]
2031         ["Mark buffer" gnus-uu-mark-buffer t]
2032         ["Mark sparse" gnus-uu-mark-sparse t]
2033         ["Mark thread" gnus-uu-mark-thread t]
2034         ["Unmark thread" gnus-uu-unmark-thread t]
2035         ("Process Mark Sets"
2036          ["Kill" gnus-summary-kill-process-mark t]
2037          ["Yank" gnus-summary-yank-process-mark
2038           gnus-newsgroup-process-stack]
2039          ["Save" gnus-summary-save-process-mark t]))
2040        ("Scroll article"
2041         ["Page forward" gnus-summary-next-page
2042          ,@(if (featurep 'xemacs) '(t)
2043              '(:help "Show next page of article"))]
2044         ["Page backward" gnus-summary-prev-page
2045          ,@(if (featurep 'xemacs) '(t)
2046              '(:help "Show previous page of article"))]
2047         ["Line forward" gnus-summary-scroll-up t])
2048        ("Move"
2049         ["Next unread article" gnus-summary-next-unread-article t]
2050         ["Previous unread article" gnus-summary-prev-unread-article t]
2051         ["Next article" gnus-summary-next-article t]
2052         ["Previous article" gnus-summary-prev-article t]
2053         ["Next unread subject" gnus-summary-next-unread-subject t]
2054         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2055         ["Next article same subject" gnus-summary-next-same-subject t]
2056         ["Previous article same subject" gnus-summary-prev-same-subject t]
2057         ["First unread article" gnus-summary-first-unread-article t]
2058         ["Best unread article" gnus-summary-best-unread-article t]
2059         ["Go to subject number..." gnus-summary-goto-subject t]
2060         ["Go to article number..." gnus-summary-goto-article t]
2061         ["Go to the last article" gnus-summary-goto-last-article t]
2062         ["Pop article off history" gnus-summary-pop-article t])
2063        ("Sort"
2064         ["Sort by number" gnus-summary-sort-by-number t]
2065         ["Sort by author" gnus-summary-sort-by-author t]
2066         ["Sort by subject" gnus-summary-sort-by-subject t]
2067         ["Sort by date" gnus-summary-sort-by-date t]
2068         ["Sort by score" gnus-summary-sort-by-score t]
2069         ["Sort by lines" gnus-summary-sort-by-lines t]
2070         ["Sort by characters" gnus-summary-sort-by-chars t]
2071         ["Original sort" gnus-summary-sort-by-original t])
2072        ("Help"
2073         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2074         ["Describe group" gnus-summary-describe-group t]
2075         ["Read manual" gnus-info-find-node t])
2076        ("Modes"
2077         ["Pick and read" gnus-pick-mode t]
2078         ["Binary" gnus-binary-mode t])
2079        ("Regeneration"
2080         ["Regenerate" gnus-summary-prepare t]
2081         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2082         ["Toggle threading" gnus-summary-toggle-threads t])
2083        ["See old articles" gnus-summary-insert-old-articles t]
2084        ["See new articles" gnus-summary-insert-new-articles t]
2085        ["Filter articles..." gnus-summary-execute-command t]
2086        ["Run command on subjects..." gnus-summary-universal-argument t]
2087        ["Search articles forward..." gnus-summary-search-article-forward t]
2088        ["Search articles backward..." gnus-summary-search-article-backward t]
2089        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2090        ["Expand window" gnus-summary-expand-window t]
2091        ["Expire expirable articles" gnus-summary-expire-articles
2092         (gnus-check-backend-function
2093          'request-expire-articles gnus-newsgroup-name)]
2094        ["Edit local kill file" gnus-summary-edit-local-kill t]
2095        ["Edit main kill file" gnus-summary-edit-global-kill t]
2096        ["Edit group parameters" gnus-summary-edit-parameters t]
2097        ["Customize group parameters" gnus-summary-customize-parameters t]
2098        ["Send a bug report" gnus-bug t]
2099        ("Exit"
2100         ["Catchup and exit" gnus-summary-catchup-and-exit
2101          ,@(if (featurep 'xemacs) '(t)
2102              '(:help "Mark unread articles in this group as read, then exit"))]
2103         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2104         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2105         ["Exit group" gnus-summary-exit
2106          ,@(if (featurep 'xemacs) '(t)
2107              '(:help "Exit current group, return to group selection mode"))]
2108         ["Exit group without updating" gnus-summary-exit-no-update t]
2109         ["Exit and goto next group" gnus-summary-next-group t]
2110         ["Exit and goto prev group" gnus-summary-prev-group t]
2111         ["Reselect group" gnus-summary-reselect-current-group t]
2112         ["Rescan group" gnus-summary-rescan-group t]
2113         ["Update dribble" gnus-summary-save-newsrc t])))
2114
2115     (gnus-run-hooks 'gnus-summary-menu-hook)))
2116
2117 (defvar gnus-summary-tool-bar-map nil)
2118
2119 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2120 (defun gnus-summary-make-tool-bar ()
2121   (if (and (fboundp 'tool-bar-add-item-from-menu)
2122            (default-value 'tool-bar-mode)
2123            (not gnus-summary-tool-bar-map))
2124       (setq gnus-summary-tool-bar-map
2125             (let ((tool-bar-map (make-sparse-keymap))
2126                   (load-path (mm-image-load-path)))
2127               (tool-bar-add-item-from-menu
2128                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2129               (tool-bar-add-item-from-menu
2130                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2131               (tool-bar-add-item-from-menu
2132                'gnus-summary-post-news "post" gnus-summary-mode-map)
2133               (tool-bar-add-item-from-menu
2134                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2135               (tool-bar-add-item-from-menu
2136                'gnus-summary-followup "followup" gnus-summary-mode-map)
2137               (tool-bar-add-item-from-menu
2138                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2139               (tool-bar-add-item-from-menu
2140                'gnus-summary-reply "reply" gnus-summary-mode-map)
2141               (tool-bar-add-item-from-menu
2142                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2143               (tool-bar-add-item-from-menu
2144                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2145               (tool-bar-add-item-from-menu
2146                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2147               (tool-bar-add-item-from-menu
2148                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2149               (tool-bar-add-item-from-menu
2150                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2151               (tool-bar-add-item-from-menu
2152                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2153               (tool-bar-add-item-from-menu
2154                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2155               (tool-bar-add-item-from-menu
2156                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2157               tool-bar-map)))
2158   (if gnus-summary-tool-bar-map
2159       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2160
2161 (defun gnus-score-set-default (var value)
2162   "A version of set that updates the GNU Emacs menu-bar."
2163   (set var value)
2164   ;; It is the message that forces the active status to be updated.
2165   (message ""))
2166
2167 (defun gnus-make-score-map (type)
2168   "Make a summary score map of type TYPE."
2169   (if t
2170       nil
2171     (let ((headers '(("author" "from" string)
2172                      ("subject" "subject" string)
2173                      ("article body" "body" string)
2174                      ("article head" "head" string)
2175                      ("xref" "xref" string)
2176                      ("extra header" "extra" string)
2177                      ("lines" "lines" number)
2178                      ("followups to author" "followup" string)))
2179           (types '((number ("less than" <)
2180                            ("greater than" >)
2181                            ("equal" =))
2182                    (string ("substring" s)
2183                            ("exact string" e)
2184                            ("fuzzy string" f)
2185                            ("regexp" r))))
2186           (perms '(("temporary" (current-time-string))
2187                    ("permanent" nil)
2188                    ("immediate" now)))
2189           header)
2190       (list
2191        (apply
2192         'nconc
2193         (list
2194          (if (eq type 'lower)
2195              "Lower score"
2196            "Increase score"))
2197         (let (outh)
2198           (while headers
2199             (setq header (car headers))
2200             (setq outh
2201                   (cons
2202                    (apply
2203                     'nconc
2204                     (list (car header))
2205                     (let ((ts (cdr (assoc (nth 2 header) types)))
2206                           outt)
2207                       (while ts
2208                         (setq outt
2209                               (cons
2210                                (apply
2211                                 'nconc
2212                                 (list (caar ts))
2213                                 (let ((ps perms)
2214                                       outp)
2215                                   (while ps
2216                                     (setq outp
2217                                           (cons
2218                                            (vector
2219                                             (caar ps)
2220                                             (list
2221                                              'gnus-summary-score-entry
2222                                              (nth 1 header)
2223                                              (if (or (string= (nth 1 header)
2224                                                               "head")
2225                                                      (string= (nth 1 header)
2226                                                               "body"))
2227                                                  ""
2228                                                (list 'gnus-summary-header
2229                                                      (nth 1 header)))
2230                                              (list 'quote (nth 1 (car ts)))
2231                                              (list 'gnus-score-delta-default
2232                                                    nil)
2233                                              (nth 1 (car ps))
2234                                              t)
2235                                             t)
2236                                            outp))
2237                                     (setq ps (cdr ps)))
2238                                   (list (nreverse outp))))
2239                                outt))
2240                         (setq ts (cdr ts)))
2241                       (list (nreverse outt))))
2242                    outh))
2243             (setq headers (cdr headers)))
2244           (list (nreverse outh))))))))
2245
2246 \f
2247
2248 (defun gnus-summary-mode (&optional group)
2249   "Major mode for reading articles.
2250
2251 All normal editing commands are switched off.
2252 \\<gnus-summary-mode-map>
2253 Each line in this buffer represents one article.  To read an
2254 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2255 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2256 respectively.
2257
2258 You can also post articles and send mail from this buffer.  To
2259 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2260 of an article, type `\\[gnus-summary-reply]'.
2261
2262 There are approx. one gazillion commands you can execute in this
2263 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2264
2265 The following commands are available:
2266
2267 \\{gnus-summary-mode-map}"
2268   (interactive)
2269   (kill-all-local-variables)
2270   (when (gnus-visual-p 'summary-menu 'menu)
2271     (gnus-summary-make-menu-bar)
2272     (gnus-summary-make-tool-bar))
2273   (gnus-summary-make-local-variables)
2274   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2275     (gnus-summary-make-local-variables))
2276   (gnus-make-thread-indent-array)
2277   (gnus-simplify-mode-line)
2278   (setq major-mode 'gnus-summary-mode)
2279   (setq mode-name "Summary")
2280   (make-local-variable 'minor-mode-alist)
2281   (use-local-map gnus-summary-mode-map)
2282   (buffer-disable-undo)
2283   (setq buffer-read-only t)             ;Disable modification
2284   (setq truncate-lines t)
2285   (setq selective-display t)
2286   (setq selective-display-ellipses t)   ;Display `...'
2287   (gnus-summary-set-display-table)
2288   (gnus-set-default-directory)
2289   (setq gnus-newsgroup-name group)
2290   (make-local-variable 'gnus-summary-line-format)
2291   (make-local-variable 'gnus-summary-line-format-spec)
2292   (make-local-variable 'gnus-summary-dummy-line-format)
2293   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2294   (make-local-variable 'gnus-summary-mark-positions)
2295   (make-local-hook 'pre-command-hook)
2296   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2297   (gnus-run-hooks 'gnus-summary-mode-hook)
2298   (turn-on-gnus-mailing-list-mode)
2299   (mm-enable-multibyte-mule4)
2300   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2301   (gnus-update-summary-mark-positions))
2302
2303 (defun gnus-summary-make-local-variables ()
2304   "Make all the local summary buffer variables."
2305   (let (global)
2306     (dolist (local gnus-summary-local-variables)
2307       (if (consp local)
2308           (progn
2309             (if (eq (cdr local) 'global)
2310                 ;; Copy the global value of the variable.
2311                 (setq global (symbol-value (car local)))
2312               ;; Use the value from the list.
2313               (setq global (eval (cdr local))))
2314             (set (make-local-variable (car local)) global))
2315         ;; Simple nil-valued local variable.
2316         (set (make-local-variable local) nil)))))
2317
2318 (defun gnus-summary-clear-local-variables ()
2319   (let ((locals gnus-summary-local-variables))
2320     (while locals
2321       (if (consp (car locals))
2322           (and (vectorp (caar locals))
2323                (set (caar locals) nil))
2324         (and (vectorp (car locals))
2325              (set (car locals) nil)))
2326       (setq locals (cdr locals)))))
2327
2328 ;; Summary data functions.
2329
2330 (defmacro gnus-data-number (data)
2331   `(car ,data))
2332
2333 (defmacro gnus-data-set-number (data number)
2334   `(setcar ,data ,number))
2335
2336 (defmacro gnus-data-mark (data)
2337   `(nth 1 ,data))
2338
2339 (defmacro gnus-data-set-mark (data mark)
2340   `(setcar (nthcdr 1 ,data) ,mark))
2341
2342 (defmacro gnus-data-pos (data)
2343   `(nth 2 ,data))
2344
2345 (defmacro gnus-data-set-pos (data pos)
2346   `(setcar (nthcdr 2 ,data) ,pos))
2347
2348 (defmacro gnus-data-header (data)
2349   `(nth 3 ,data))
2350
2351 (defmacro gnus-data-set-header (data header)
2352   `(setf (nth 3 ,data) ,header))
2353
2354 (defmacro gnus-data-level (data)
2355   `(nth 4 ,data))
2356
2357 (defmacro gnus-data-unread-p (data)
2358   `(= (nth 1 ,data) gnus-unread-mark))
2359
2360 (defmacro gnus-data-read-p (data)
2361   `(/= (nth 1 ,data) gnus-unread-mark))
2362
2363 (defmacro gnus-data-pseudo-p (data)
2364   `(consp (nth 3 ,data)))
2365
2366 (defmacro gnus-data-find (number)
2367   `(assq ,number gnus-newsgroup-data))
2368
2369 (defmacro gnus-data-find-list (number &optional data)
2370   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2371      (memq (assq ,number bdata)
2372            bdata)))
2373
2374 (defmacro gnus-data-make (number mark pos header level)
2375   `(list ,number ,mark ,pos ,header ,level))
2376
2377 (defun gnus-data-enter (after-article number mark pos header level offset)
2378   (let ((data (gnus-data-find-list after-article)))
2379     (unless data
2380       (error "No such article: %d" after-article))
2381     (setcdr data (cons (gnus-data-make number mark pos header level)
2382                        (cdr data)))
2383     (setq gnus-newsgroup-data-reverse nil)
2384     (gnus-data-update-list (cddr data) offset)))
2385
2386 (defun gnus-data-enter-list (after-article list &optional offset)
2387   (when list
2388     (let ((data (and after-article (gnus-data-find-list after-article)))
2389           (ilist list))
2390       (if (not (or data
2391                    after-article))
2392           (let ((odata gnus-newsgroup-data))
2393             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2394             (when offset
2395               (gnus-data-update-list odata offset)))
2396         ;; Find the last element in the list to be spliced into the main
2397         ;; list.
2398         (while (cdr list)
2399           (setq list (cdr list)))
2400         (if (not data)
2401             (progn
2402               (setcdr list gnus-newsgroup-data)
2403               (setq gnus-newsgroup-data ilist)
2404               (when offset
2405                 (gnus-data-update-list (cdr list) offset)))
2406           (setcdr list (cdr data))
2407           (setcdr data ilist)
2408           (when offset
2409             (gnus-data-update-list (cdr list) offset))))
2410       (setq gnus-newsgroup-data-reverse nil))))
2411
2412 (defun gnus-data-remove (article &optional offset)
2413   (let ((data gnus-newsgroup-data))
2414     (if (= (gnus-data-number (car data)) article)
2415         (progn
2416           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2417                 gnus-newsgroup-data-reverse nil)
2418           (when offset
2419             (gnus-data-update-list gnus-newsgroup-data offset)))
2420       (while (cdr data)
2421         (when (= (gnus-data-number (cadr data)) article)
2422           (setcdr data (cddr data))
2423           (when offset
2424             (gnus-data-update-list (cdr data) offset))
2425           (setq data nil
2426                 gnus-newsgroup-data-reverse nil))
2427         (setq data (cdr data))))))
2428
2429 (defmacro gnus-data-list (backward)
2430   `(if ,backward
2431        (or gnus-newsgroup-data-reverse
2432            (setq gnus-newsgroup-data-reverse
2433                  (reverse gnus-newsgroup-data)))
2434      gnus-newsgroup-data))
2435
2436 (defun gnus-data-update-list (data offset)
2437   "Add OFFSET to the POS of all data entries in DATA."
2438   (setq gnus-newsgroup-data-reverse nil)
2439   (while data
2440     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2441     (setq data (cdr data))))
2442
2443 (defun gnus-summary-article-pseudo-p (article)
2444   "Say whether this article is a pseudo article or not."
2445   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2446
2447 (defmacro gnus-summary-article-sparse-p (article)
2448   "Say whether this article is a sparse article or not."
2449   `(memq ,article gnus-newsgroup-sparse))
2450
2451 (defmacro gnus-summary-article-ancient-p (article)
2452   "Say whether this article is a sparse article or not."
2453   `(memq ,article gnus-newsgroup-ancient))
2454
2455 (defun gnus-article-parent-p (number)
2456   "Say whether this article is a parent or not."
2457   (let ((data (gnus-data-find-list number)))
2458     (and (cdr data)                     ; There has to be an article after...
2459          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2460             (gnus-data-level (nth 1 data))))))
2461
2462 (defun gnus-article-children (number)
2463   "Return a list of all children to NUMBER."
2464   (let* ((data (gnus-data-find-list number))
2465          (level (gnus-data-level (car data)))
2466          children)
2467     (setq data (cdr data))
2468     (while (and data
2469                 (= (gnus-data-level (car data)) (1+ level)))
2470       (push (gnus-data-number (car data)) children)
2471       (setq data (cdr data)))
2472     children))
2473
2474 (defmacro gnus-summary-skip-intangible ()
2475   "If the current article is intangible, then jump to a different article."
2476   '(let ((to (get-text-property (point) 'gnus-intangible)))
2477      (and to (gnus-summary-goto-subject to))))
2478
2479 (defmacro gnus-summary-article-intangible-p ()
2480   "Say whether this article is intangible or not."
2481   '(get-text-property (point) 'gnus-intangible))
2482
2483 (defun gnus-article-read-p (article)
2484   "Say whether ARTICLE is read or not."
2485   (not (or (memq article gnus-newsgroup-marked)
2486            (memq article gnus-newsgroup-unreads)
2487            (memq article gnus-newsgroup-unselected)
2488            (memq article gnus-newsgroup-dormant))))
2489
2490 ;; Some summary mode macros.
2491
2492 (defmacro gnus-summary-article-number ()
2493   "The article number of the article on the current line.
2494 If there isn's an article number here, then we return the current
2495 article number."
2496   '(progn
2497      (gnus-summary-skip-intangible)
2498      (or (get-text-property (point) 'gnus-number)
2499          (gnus-summary-last-subject))))
2500
2501 (defmacro gnus-summary-article-header (&optional number)
2502   "Return the header of article NUMBER."
2503   `(gnus-data-header (gnus-data-find
2504                       ,(or number '(gnus-summary-article-number)))))
2505
2506 (defmacro gnus-summary-thread-level (&optional number)
2507   "Return the level of thread that starts with article NUMBER."
2508   `(if (and (eq gnus-summary-make-false-root 'dummy)
2509             (get-text-property (point) 'gnus-intangible))
2510        0
2511      (gnus-data-level (gnus-data-find
2512                        ,(or number '(gnus-summary-article-number))))))
2513
2514 (defmacro gnus-summary-article-mark (&optional number)
2515   "Return the mark of article NUMBER."
2516   `(gnus-data-mark (gnus-data-find
2517                     ,(or number '(gnus-summary-article-number)))))
2518
2519 (defmacro gnus-summary-article-pos (&optional number)
2520   "Return the position of the line of article NUMBER."
2521   `(gnus-data-pos (gnus-data-find
2522                    ,(or number '(gnus-summary-article-number)))))
2523
2524 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2525 (defmacro gnus-summary-article-subject (&optional number)
2526   "Return current subject string or nil if nothing."
2527   `(let ((headers
2528           ,(if number
2529                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2530              '(gnus-data-header (assq (gnus-summary-article-number)
2531                                       gnus-newsgroup-data)))))
2532      (and headers
2533           (vectorp headers)
2534           (mail-header-subject headers))))
2535
2536 (defmacro gnus-summary-article-score (&optional number)
2537   "Return current article score."
2538   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2539                   gnus-newsgroup-scored))
2540        gnus-summary-default-score 0))
2541
2542 (defun gnus-summary-article-children (&optional number)
2543   "Return a list of article numbers that are children of article NUMBER."
2544   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2545          (level (gnus-data-level (car data)))
2546          l children)
2547     (while (and (setq data (cdr data))
2548                 (> (setq l (gnus-data-level (car data))) level))
2549       (and (= (1+ level) l)
2550            (push (gnus-data-number (car data))
2551                  children)))
2552     (nreverse children)))
2553
2554 (defun gnus-summary-article-parent (&optional number)
2555   "Return the article number of the parent of article NUMBER."
2556   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2557                                     (gnus-data-list t)))
2558          (level (gnus-data-level (car data))))
2559     (if (zerop level)
2560         ()                              ; This is a root.
2561       ;; We search until we find an article with a level less than
2562       ;; this one.  That function has to be the parent.
2563       (while (and (setq data (cdr data))
2564                   (not (< (gnus-data-level (car data)) level))))
2565       (and data (gnus-data-number (car data))))))
2566
2567 (defun gnus-unread-mark-p (mark)
2568   "Say whether MARK is the unread mark."
2569   (= mark gnus-unread-mark))
2570
2571 (defun gnus-read-mark-p (mark)
2572   "Say whether MARK is one of the marks that mark as read.
2573 This is all marks except unread, ticked, dormant, and expirable."
2574   (not (or (= mark gnus-unread-mark)
2575            (= mark gnus-ticked-mark)
2576            (= mark gnus-dormant-mark)
2577            (= mark gnus-expirable-mark))))
2578
2579 (defmacro gnus-article-mark (number)
2580   "Return the MARK of article NUMBER.
2581 This macro should only be used when computing the mark the \"first\"
2582 time; i.e., when generating the summary lines.  After that,
2583 `gnus-summary-article-mark' should be used to examine the
2584 marks of articles."
2585   `(cond
2586     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2587     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2588     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2589     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2590     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2591     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2592     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2593     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2594            gnus-ancient-mark))))
2595
2596 ;; Saving hidden threads.
2597
2598 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2599 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2600
2601 (defmacro gnus-save-hidden-threads (&rest forms)
2602   "Save hidden threads, eval FORMS, and restore the hidden threads."
2603   (let ((config (make-symbol "config")))
2604     `(let ((,config (gnus-hidden-threads-configuration)))
2605        (unwind-protect
2606            (save-excursion
2607              ,@forms)
2608          (gnus-restore-hidden-threads-configuration ,config)))))
2609
2610 (defun gnus-data-compute-positions ()
2611   "Compute the positions of all articles."
2612   (setq gnus-newsgroup-data-reverse nil)
2613   (let ((data gnus-newsgroup-data))
2614     (save-excursion
2615       (gnus-save-hidden-threads
2616         (gnus-summary-show-all-threads)
2617         (goto-char (point-min))
2618         (while data
2619           (while (get-text-property (point) 'gnus-intangible)
2620             (forward-line 1))
2621           (gnus-data-set-pos (car data) (+ (point) 3))
2622           (setq data (cdr data))
2623           (forward-line 1))))))
2624
2625 (defun gnus-hidden-threads-configuration ()
2626   "Return the current hidden threads configuration."
2627   (save-excursion
2628     (let (config)
2629       (goto-char (point-min))
2630       (while (search-forward "\r" nil t)
2631         (push (1- (point)) config))
2632       config)))
2633
2634 (defun gnus-restore-hidden-threads-configuration (config)
2635   "Restore hidden threads configuration from CONFIG."
2636   (save-excursion
2637     (let (point buffer-read-only)
2638       (while (setq point (pop config))
2639         (when (and (< point (point-max))
2640                    (goto-char point)
2641                    (eq (char-after) ?\n))
2642           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2643
2644 ;; Various summary mode internalish functions.
2645
2646 (defun gnus-mouse-pick-article (e)
2647   (interactive "e")
2648   (mouse-set-point e)
2649   (gnus-summary-next-page nil t))
2650
2651 (defun gnus-summary-set-display-table ()
2652   "Change the display table.
2653 Odd characters have a tendency to mess
2654 up nicely formatted displays - we make all possible glyphs
2655 display only a single character."
2656
2657   ;; We start from the standard display table, if any.
2658   (let ((table (or (copy-sequence standard-display-table)
2659                    (make-display-table)))
2660         (i 32))
2661     ;; Nix out all the control chars...
2662     (while (>= (setq i (1- i)) 0)
2663       (aset table i [??]))
2664     ;; ... but not newline and cr, of course.  (cr is necessary for the
2665     ;; selective display).
2666     (aset table ?\n nil)
2667     (aset table ?\r nil)
2668     ;; We keep TAB as well.
2669     (aset table ?\t nil)
2670     ;; We nix out any glyphs over 126 that are not set already.
2671     (let ((i 256))
2672       (while (>= (setq i (1- i)) 127)
2673         ;; Only modify if the entry is nil.
2674         (unless (aref table i)
2675           (aset table i [??]))))
2676     (setq buffer-display-table table)))
2677
2678 (defun gnus-summary-buffer-name (group)
2679   "Return the summary buffer name of GROUP."
2680   (concat "*Summary " group "*"))
2681
2682 (defun gnus-summary-setup-buffer (group)
2683   "Initialize summary buffer."
2684   (let ((buffer (gnus-summary-buffer-name group)))
2685     (if (get-buffer buffer)
2686         (progn
2687           (set-buffer buffer)
2688           (setq gnus-summary-buffer (current-buffer))
2689           (not gnus-newsgroup-prepared))
2690       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2691       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2692       (gnus-summary-mode group)
2693       (when gnus-carpal
2694         (gnus-carpal-setup-buffer 'summary))
2695       (unless gnus-single-article-buffer
2696         (make-local-variable 'gnus-article-buffer)
2697         (make-local-variable 'gnus-article-current)
2698         (make-local-variable 'gnus-original-article-buffer))
2699       (setq gnus-newsgroup-name group)
2700       ;; Set any local variables in the group parameters.
2701       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2702       t)))
2703
2704 (defun gnus-set-global-variables ()
2705   "Set the global equivalents of the buffer-local variables.
2706 They are set to the latest values they had.  These reflect the summary
2707 buffer that was in action when the last article was fetched."
2708   (when (eq major-mode 'gnus-summary-mode)
2709     (setq gnus-summary-buffer (current-buffer))
2710     (let ((name gnus-newsgroup-name)
2711           (marked gnus-newsgroup-marked)
2712           (unread gnus-newsgroup-unreads)
2713           (headers gnus-current-headers)
2714           (data gnus-newsgroup-data)
2715           (summary gnus-summary-buffer)
2716           (article-buffer gnus-article-buffer)
2717           (original gnus-original-article-buffer)
2718           (gac gnus-article-current)
2719           (reffed gnus-reffed-article-number)
2720           (score-file gnus-current-score-file)
2721           (default-charset gnus-newsgroup-charset)
2722           vlist)
2723       (let ((locals gnus-newsgroup-variables))
2724         (while locals
2725           (if (consp (car locals))
2726               (push (eval (caar locals)) vlist)
2727             (push (eval (car locals)) vlist))
2728           (setq locals (cdr locals)))
2729         (setq vlist (nreverse vlist)))
2730       (save-excursion
2731         (set-buffer gnus-group-buffer)
2732         (setq gnus-newsgroup-name name
2733               gnus-newsgroup-marked marked
2734               gnus-newsgroup-unreads unread
2735               gnus-current-headers headers
2736               gnus-newsgroup-data data
2737               gnus-article-current gac
2738               gnus-summary-buffer summary
2739               gnus-article-buffer article-buffer
2740               gnus-original-article-buffer original
2741               gnus-reffed-article-number reffed
2742               gnus-current-score-file score-file
2743               gnus-newsgroup-charset default-charset)
2744         (let ((locals gnus-newsgroup-variables))
2745           (while locals
2746             (if (consp (car locals))
2747                 (set (caar locals) (pop vlist))
2748               (set (car locals) (pop vlist)))
2749             (setq locals (cdr locals))))
2750         ;; The article buffer also has local variables.
2751         (when (gnus-buffer-live-p gnus-article-buffer)
2752           (set-buffer gnus-article-buffer)
2753           (setq gnus-summary-buffer summary))))))
2754
2755 (defun gnus-summary-article-unread-p (article)
2756   "Say whether ARTICLE is unread or not."
2757   (memq article gnus-newsgroup-unreads))
2758
2759 (defun gnus-summary-first-article-p (&optional article)
2760   "Return whether ARTICLE is the first article in the buffer."
2761   (if (not (setq article (or article (gnus-summary-article-number))))
2762       nil
2763     (eq article (caar gnus-newsgroup-data))))
2764
2765 (defun gnus-summary-last-article-p (&optional article)
2766   "Return whether ARTICLE is the last article in the buffer."
2767   (if (not (setq article (or article (gnus-summary-article-number))))
2768       ;; All non-existent numbers are the last article.  :-)
2769       t
2770     (not (cdr (gnus-data-find-list article)))))
2771
2772 (defun gnus-make-thread-indent-array ()
2773   (let ((n 200))
2774     (unless (and gnus-thread-indent-array
2775                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2776       (setq gnus-thread-indent-array (make-vector 201 "")
2777             gnus-thread-indent-array-level gnus-thread-indent-level)
2778       (while (>= n 0)
2779         (aset gnus-thread-indent-array n
2780               (make-string (* n gnus-thread-indent-level) ? ))
2781         (setq n (1- n))))))
2782
2783 (defun gnus-update-summary-mark-positions ()
2784   "Compute where the summary marks are to go."
2785   (save-excursion
2786     (when (gnus-buffer-exists-p gnus-summary-buffer)
2787       (set-buffer gnus-summary-buffer))
2788     (let ((gnus-replied-mark 129)
2789           (gnus-score-below-mark 130)
2790           (gnus-score-over-mark 130)
2791           (gnus-download-mark 131)
2792           (spec gnus-summary-line-format-spec)
2793           gnus-visual pos)
2794       (save-excursion
2795         (gnus-set-work-buffer)
2796         (let ((gnus-summary-line-format-spec spec)
2797               (gnus-newsgroup-downloadable '((0 . t))))
2798           (gnus-summary-insert-line
2799            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
2800            0 nil 128 t nil "" nil 1)
2801           (goto-char (point-min))
2802           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2803                                              (- (point) 2)))))
2804           (goto-char (point-min))
2805           (push (cons 'replied (and (search-forward "\201" nil t)
2806                                     (- (point) 2)))
2807                 pos)
2808           (goto-char (point-min))
2809           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2810                 pos)
2811           (goto-char (point-min))
2812           (push (cons 'download
2813                       (and (search-forward "\203" nil t) (- (point) 2)))
2814                 pos)))
2815       (setq gnus-summary-mark-positions pos))))
2816
2817 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2818   "Insert a dummy root in the summary buffer."
2819   (beginning-of-line)
2820   (gnus-add-text-properties
2821    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2822    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2823
2824 (defun gnus-summary-from-or-to-or-newsgroups (header)
2825   (let ((to (cdr (assq 'To (mail-header-extra header))))
2826         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2827         (mail-parse-charset gnus-newsgroup-charset)
2828         (mail-parse-ignored-charsets
2829          (save-excursion (set-buffer gnus-summary-buffer)
2830                          gnus-newsgroup-ignored-charsets)))
2831     (cond
2832      ((and to
2833            gnus-ignored-from-addresses
2834            (string-match gnus-ignored-from-addresses
2835                          (mail-header-from header)))
2836       (concat "-> "
2837               (or (car (funcall gnus-extract-address-components
2838                                 (funcall
2839                                  gnus-decode-encoded-word-function to)))
2840                   (funcall gnus-decode-encoded-word-function to))))
2841      ((and newsgroups
2842            gnus-ignored-from-addresses
2843            (string-match gnus-ignored-from-addresses
2844                          (mail-header-from header)))
2845       (concat "=> " newsgroups))
2846      (t
2847       (or (car (funcall gnus-extract-address-components
2848                         (mail-header-from header)))
2849           (mail-header-from header))))))
2850
2851 (defun gnus-summary-insert-line (gnus-tmp-header
2852                                  gnus-tmp-level gnus-tmp-current
2853                                  gnus-tmp-unread gnus-tmp-replied
2854                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2855                                  &optional gnus-tmp-dummy gnus-tmp-score
2856                                  gnus-tmp-process)
2857   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2858          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2859          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2860          (gnus-tmp-score-char
2861           (if (or (null gnus-summary-default-score)
2862                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2863                       gnus-summary-zcore-fuzz))
2864               ?  ;Whitespace
2865             (if (< gnus-tmp-score gnus-summary-default-score)
2866                 gnus-score-below-mark gnus-score-over-mark)))
2867          (gnus-tmp-replied
2868           (cond (gnus-tmp-process gnus-process-mark)
2869                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2870                  gnus-cached-mark)
2871                 (gnus-tmp-replied gnus-replied-mark)
2872                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2873                  gnus-saved-mark)
2874                 (t gnus-no-mark)))
2875          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2876          (gnus-tmp-name
2877           (cond
2878            ((string-match "<[^>]+> *$" gnus-tmp-from)
2879             (let ((beg (match-beginning 0)))
2880               (or (and (string-match "^\".+\"" gnus-tmp-from)
2881                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2882                   (substring gnus-tmp-from 0 beg))))
2883            ((string-match "(.+)" gnus-tmp-from)
2884             (substring gnus-tmp-from
2885                        (1+ (match-beginning 0)) (1- (match-end 0))))
2886            (t gnus-tmp-from)))
2887          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2888          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2889          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2890          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2891          (buffer-read-only nil))
2892     (when (string= gnus-tmp-name "")
2893       (setq gnus-tmp-name gnus-tmp-from))
2894     (unless (numberp gnus-tmp-lines)
2895       (setq gnus-tmp-lines -1))
2896     (when (= gnus-tmp-lines -1)
2897       (setq gnus-tmp-lines "?"))
2898     (gnus-put-text-property
2899      (point)
2900      (progn (eval gnus-summary-line-format-spec) (point))
2901      'gnus-number gnus-tmp-number)
2902     (when (gnus-visual-p 'summary-highlight 'highlight)
2903       (forward-line -1)
2904       (gnus-run-hooks 'gnus-summary-update-hook)
2905       (forward-line 1))))
2906
2907 (defun gnus-summary-update-line (&optional dont-update)
2908   "Update summary line after change."
2909   (when (and gnus-summary-default-score
2910              (not gnus-summary-inhibit-highlight))
2911     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2912            (article (gnus-summary-article-number))
2913            (score (gnus-summary-article-score article)))
2914       (unless dont-update
2915         (if (and gnus-summary-mark-below
2916                  (< (gnus-summary-article-score)
2917                     gnus-summary-mark-below))
2918             ;; This article has a low score, so we mark it as read.
2919             (when (memq article gnus-newsgroup-unreads)
2920               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2921           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2922             ;; This article was previously marked as read on account
2923             ;; of a low score, but now it has risen, so we mark it as
2924             ;; unread.
2925             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2926         (gnus-summary-update-mark
2927          (if (or (null gnus-summary-default-score)
2928                  (<= (abs (- score gnus-summary-default-score))
2929                      gnus-summary-zcore-fuzz))
2930              ?  ;Whitespace
2931            (if (< score gnus-summary-default-score)
2932                gnus-score-below-mark gnus-score-over-mark))
2933          'score))
2934       ;; Do visual highlighting.
2935       (when (gnus-visual-p 'summary-highlight 'highlight)
2936         (gnus-run-hooks 'gnus-summary-update-hook)))))
2937
2938 (defvar gnus-tmp-new-adopts nil)
2939
2940 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2941   "Return the number of articles in THREAD.
2942 This may be 0 in some cases -- if none of the articles in
2943 the thread are to be displayed."
2944   (let* ((number
2945           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2946           (cond
2947            ((not (listp thread))
2948             1)
2949            ((and (consp thread) (cdr thread))
2950             (apply
2951              '+ 1 (mapcar
2952                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2953            ((null thread)
2954             1)
2955            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2956             1)
2957            (t 0))))
2958     (when (and level (zerop level) gnus-tmp-new-adopts)
2959       (incf number
2960             (apply '+ (mapcar
2961                        'gnus-summary-number-of-articles-in-thread
2962                        gnus-tmp-new-adopts))))
2963     (if char
2964         (if (> number 1) gnus-not-empty-thread-mark
2965           gnus-empty-thread-mark)
2966       number)))
2967
2968 (defun gnus-summary-set-local-parameters (group)
2969   "Go through the local params of GROUP and set all variable specs in that list."
2970   (let ((params (gnus-group-find-parameter group))
2971         elem)
2972     (while params
2973       (setq elem (car params)
2974             params (cdr params))
2975       (and (consp elem)                 ; Has to be a cons.
2976            (consp (cdr elem))           ; The cdr has to be a list.
2977            (symbolp (car elem))         ; Has to be a symbol in there.
2978            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2979            (ignore-errors               ; So we set it.
2980              (make-local-variable (car elem))
2981              (set (car elem) (eval (nth 1 elem))))))))
2982
2983 (defun gnus-summary-read-group (group &optional show-all no-article
2984                                       kill-buffer no-display backward
2985                                       select-articles)
2986   "Start reading news in newsgroup GROUP.
2987 If SHOW-ALL is non-nil, already read articles are also listed.
2988 If NO-ARTICLE is non-nil, no article is selected initially.
2989 If NO-DISPLAY, don't generate a summary buffer."
2990   (let (result)
2991     (while (and group
2992                 (null (setq result
2993                             (let ((gnus-auto-select-next nil))
2994                               (or (gnus-summary-read-group-1
2995                                    group show-all no-article
2996                                    kill-buffer no-display
2997                                    select-articles)
2998                                   (setq show-all nil
2999                                         select-articles nil)))))
3000                 (eq gnus-auto-select-next 'quietly))
3001       (set-buffer gnus-group-buffer)
3002       ;; The entry function called above goes to the next
3003       ;; group automatically, so we go two groups back
3004       ;; if we are searching for the previous group.
3005       (when backward
3006         (gnus-group-prev-unread-group 2))
3007       (if (not (equal group (gnus-group-group-name)))
3008           (setq group (gnus-group-group-name))
3009         (setq group nil)))
3010     result))
3011
3012 (defun gnus-summary-read-group-1 (group show-all no-article
3013                                         kill-buffer no-display
3014                                         &optional select-articles)
3015   ;; Killed foreign groups can't be entered.
3016   ;;  (when (and (not (gnus-group-native-p group))
3017   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3018   ;;    (error "Dead non-native groups can't be entered"))
3019   (gnus-message 5 "Retrieving newsgroup: %s..." 
3020                 (gnus-group-decoded-name group))
3021   (let* ((new-group (gnus-summary-setup-buffer group))
3022          (quit-config (gnus-group-quit-config group))
3023          (did-select (and new-group (gnus-select-newsgroup
3024                                      group show-all select-articles))))
3025     (cond
3026      ;; This summary buffer exists already, so we just select it.
3027      ((not new-group)
3028       (gnus-set-global-variables)
3029       (when kill-buffer
3030         (gnus-kill-or-deaden-summary kill-buffer))
3031       (gnus-configure-windows 'summary 'force)
3032       (gnus-set-mode-line 'summary)
3033       (gnus-summary-position-point)
3034       (message "")
3035       t)
3036      ;; We couldn't select this group.
3037      ((null did-select)
3038       (when (and (eq major-mode 'gnus-summary-mode)
3039                  (not (equal (current-buffer) kill-buffer)))
3040         (kill-buffer (current-buffer))
3041         (if (not quit-config)
3042             (progn
3043               ;; Update the info -- marks might need to be removed,
3044               ;; for instance.
3045               (gnus-summary-update-info)
3046               (set-buffer gnus-group-buffer)
3047               (gnus-group-jump-to-group group)
3048               (gnus-group-next-unread-group 1))
3049           (gnus-handle-ephemeral-exit quit-config)))
3050       (let ((grpinfo (gnus-get-info group)))
3051         (if (null (gnus-info-read grpinfo))
3052             (gnus-message 3 "Group %s contains no messages" 
3053                           (gnus-group-decoded-name group))
3054           (gnus-message 3 "Can't select group")))
3055       nil)
3056      ;; The user did a `C-g' while prompting for number of articles,
3057      ;; so we exit this group.
3058      ((eq did-select 'quit)
3059       (and (eq major-mode 'gnus-summary-mode)
3060            (not (equal (current-buffer) kill-buffer))
3061            (kill-buffer (current-buffer)))
3062       (when kill-buffer
3063         (gnus-kill-or-deaden-summary kill-buffer))
3064       (if (not quit-config)
3065           (progn
3066             (set-buffer gnus-group-buffer)
3067             (gnus-group-jump-to-group group)
3068             (gnus-group-next-unread-group 1)
3069             (gnus-configure-windows 'group 'force))
3070         (gnus-handle-ephemeral-exit quit-config))
3071       ;; Finally signal the quit.
3072       (signal 'quit nil))
3073      ;; The group was successfully selected.
3074      (t
3075       (gnus-set-global-variables)
3076       ;; Save the active value in effect when the group was entered.
3077       (setq gnus-newsgroup-active
3078             (gnus-copy-sequence
3079              (gnus-active gnus-newsgroup-name)))
3080       ;; You can change the summary buffer in some way with this hook.
3081       (gnus-run-hooks 'gnus-select-group-hook)
3082       (gnus-update-format-specifications
3083        nil 'summary 'summary-mode 'summary-dummy)
3084       (gnus-update-summary-mark-positions)
3085       ;; Do score processing.
3086       (when gnus-use-scoring
3087         (gnus-possibly-score-headers))
3088       ;; Check whether to fill in the gaps in the threads.
3089       (when gnus-build-sparse-threads
3090         (gnus-build-sparse-threads))
3091       ;; Find the initial limit.
3092       (if gnus-show-threads
3093           (if show-all
3094               (let ((gnus-newsgroup-dormant nil))
3095                 (gnus-summary-initial-limit show-all))
3096             (gnus-summary-initial-limit show-all))
3097         ;; When untreaded, all articles are always shown.
3098         (setq gnus-newsgroup-limit
3099               (mapcar
3100                (lambda (header) (mail-header-number header))
3101                gnus-newsgroup-headers)))
3102       ;; Generate the summary buffer.
3103       (unless no-display
3104         (gnus-summary-prepare))
3105       (when gnus-use-trees
3106         (gnus-tree-open group)
3107         (setq gnus-summary-highlight-line-function
3108               'gnus-tree-highlight-article))
3109       ;; If the summary buffer is empty, but there are some low-scored
3110       ;; articles or some excluded dormants, we include these in the
3111       ;; buffer.
3112       (when (and (zerop (buffer-size))
3113                  (not no-display))
3114         (cond (gnus-newsgroup-dormant
3115                (gnus-summary-limit-include-dormant))
3116               ((and gnus-newsgroup-scored show-all)
3117                (gnus-summary-limit-include-expunged t))))
3118       ;; Function `gnus-apply-kill-file' must be called in this hook.
3119       (gnus-run-hooks 'gnus-apply-kill-hook)
3120       (if (and (zerop (buffer-size))
3121                (not no-display))
3122           (progn
3123             ;; This newsgroup is empty.
3124             (gnus-summary-catchup-and-exit nil t)
3125             (gnus-message 6 "No unread news")
3126             (when kill-buffer
3127               (gnus-kill-or-deaden-summary kill-buffer))
3128             ;; Return nil from this function.
3129             nil)
3130         ;; Hide conversation thread subtrees.  We cannot do this in
3131         ;; gnus-summary-prepare-hook since kill processing may not
3132         ;; work with hidden articles.
3133         (and gnus-show-threads
3134              gnus-thread-hide-subtree
3135              (gnus-summary-hide-all-threads))
3136         (when kill-buffer
3137           (gnus-kill-or-deaden-summary kill-buffer))
3138         ;; Show first unread article if requested.
3139         (if (and (not no-article)
3140                  (not no-display)
3141                  gnus-newsgroup-unreads
3142                  gnus-auto-select-first)
3143             (progn
3144               (gnus-configure-windows 'summary)
3145               (cond
3146                ((eq gnus-auto-select-first 'best)
3147                 (gnus-summary-best-unread-article))
3148                ((eq gnus-auto-select-first t)
3149                 (gnus-summary-first-unread-article))
3150                ((gnus-functionp gnus-auto-select-first)
3151                 (funcall gnus-auto-select-first))))
3152           ;; Don't select any articles, just move point to the first
3153           ;; article in the group.
3154           (goto-char (point-min))
3155           (gnus-summary-position-point)
3156           (gnus-configure-windows 'summary 'force)
3157           (gnus-set-mode-line 'summary))
3158         (when (get-buffer-window gnus-group-buffer t)
3159           ;; Gotta use windows, because recenter does weird stuff if
3160           ;; the current buffer ain't the displayed window.
3161           (let ((owin (selected-window)))
3162             (select-window (get-buffer-window gnus-group-buffer t))
3163             (when (gnus-group-goto-group group)
3164               (recenter))
3165             (select-window owin)))
3166         ;; Mark this buffer as "prepared".
3167         (setq gnus-newsgroup-prepared t)
3168         (gnus-run-hooks 'gnus-summary-prepared-hook)
3169         t)))))
3170
3171 (defun gnus-summary-prepare ()
3172   "Generate the summary buffer."
3173   (interactive)
3174   (let ((buffer-read-only nil))
3175     (erase-buffer)
3176     (setq gnus-newsgroup-data nil
3177           gnus-newsgroup-data-reverse nil)
3178     (gnus-run-hooks 'gnus-summary-generate-hook)
3179     ;; Generate the buffer, either with threads or without.
3180     (when gnus-newsgroup-headers
3181       (gnus-summary-prepare-threads
3182        (if gnus-show-threads
3183            (gnus-sort-gathered-threads
3184             (funcall gnus-summary-thread-gathering-function
3185                      (gnus-sort-threads
3186                       (gnus-cut-threads (gnus-make-threads)))))
3187          ;; Unthreaded display.
3188          (gnus-sort-articles gnus-newsgroup-headers))))
3189     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3190     ;; Call hooks for modifying summary buffer.
3191     (goto-char (point-min))
3192     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3193
3194 (defsubst gnus-general-simplify-subject (subject)
3195   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3196   (setq subject
3197         (cond
3198          ;; Truncate the subject.
3199          (gnus-simplify-subject-functions
3200           (gnus-map-function gnus-simplify-subject-functions subject))
3201          ((numberp gnus-summary-gather-subject-limit)
3202           (setq subject (gnus-simplify-subject-re subject))
3203           (if (> (length subject) gnus-summary-gather-subject-limit)
3204               (substring subject 0 gnus-summary-gather-subject-limit)
3205             subject))
3206          ;; Fuzzily simplify it.
3207          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3208           (gnus-simplify-subject-fuzzy subject))
3209          ;; Just remove the leading "Re:".
3210          (t
3211           (gnus-simplify-subject-re subject))))
3212
3213   (if (and gnus-summary-gather-exclude-subject
3214            (string-match gnus-summary-gather-exclude-subject subject))
3215       nil                               ; This article shouldn't be gathered
3216     subject))
3217
3218 (defun gnus-summary-simplify-subject-query ()
3219   "Query where the respool algorithm would put this article."
3220   (interactive)
3221   (gnus-summary-select-article)
3222   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3223
3224 (defun gnus-gather-threads-by-subject (threads)
3225   "Gather threads by looking at Subject headers."
3226   (if (not gnus-summary-make-false-root)
3227       threads
3228     (let ((hashtb (gnus-make-hashtable 1024))
3229           (prev threads)
3230           (result threads)
3231           subject hthread whole-subject)
3232       (while threads
3233         (setq subject (gnus-general-simplify-subject
3234                        (setq whole-subject (mail-header-subject
3235                                             (caar threads)))))
3236         (when subject
3237           (if (setq hthread (gnus-gethash subject hashtb))
3238               (progn
3239                 ;; We enter a dummy root into the thread, if we
3240                 ;; haven't done that already.
3241                 (unless (stringp (caar hthread))
3242                   (setcar hthread (list whole-subject (car hthread))))
3243                 ;; We add this new gathered thread to this gathered
3244                 ;; thread.
3245                 (setcdr (car hthread)
3246                         (nconc (cdar hthread) (list (car threads))))
3247                 ;; Remove it from the list of threads.
3248                 (setcdr prev (cdr threads))
3249                 (setq threads prev))
3250             ;; Enter this thread into the hash table.
3251             (gnus-sethash subject threads hashtb)))
3252         (setq prev threads)
3253         (setq threads (cdr threads)))
3254       result)))
3255
3256 (defun gnus-gather-threads-by-references (threads)
3257   "Gather threads by looking at References headers."
3258   (let ((idhashtb (gnus-make-hashtable 1024))
3259         (thhashtb (gnus-make-hashtable 1024))
3260         (prev threads)
3261         (result threads)
3262         ids references id gthread gid entered ref)
3263     (while threads
3264       (when (setq references (mail-header-references (caar threads)))
3265         (setq id (mail-header-id (caar threads))
3266               ids (gnus-split-references references)
3267               entered nil)
3268         (while (setq ref (pop ids))
3269           (setq ids (delete ref ids))
3270           (if (not (setq gid (gnus-gethash ref idhashtb)))
3271               (progn
3272                 (gnus-sethash ref id idhashtb)
3273                 (gnus-sethash id threads thhashtb))
3274             (setq gthread (gnus-gethash gid thhashtb))
3275             (unless entered
3276               ;; We enter a dummy root into the thread, if we
3277               ;; haven't done that already.
3278               (unless (stringp (caar gthread))
3279                 (setcar gthread (list (mail-header-subject (caar gthread))
3280                                       (car gthread))))
3281               ;; We add this new gathered thread to this gathered
3282               ;; thread.
3283               (setcdr (car gthread)
3284                       (nconc (cdar gthread) (list (car threads)))))
3285             ;; Add it into the thread hash table.
3286             (gnus-sethash id gthread thhashtb)
3287             (setq entered t)
3288             ;; Remove it from the list of threads.
3289             (setcdr prev (cdr threads))
3290             (setq threads prev))))
3291       (setq prev threads)
3292       (setq threads (cdr threads)))
3293     result))
3294
3295 (defun gnus-sort-gathered-threads (threads)
3296   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3297   (let ((result threads))
3298     (while threads
3299       (when (stringp (caar threads))
3300         (setcdr (car threads)
3301                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3302       (setq threads (cdr threads)))
3303     result))
3304
3305 (defun gnus-thread-loop-p (root thread)
3306   "Say whether ROOT is in THREAD."
3307   (let ((stack (list thread))
3308         (infloop 0)
3309         th)
3310     (while (setq thread (pop stack))
3311       (setq th (cdr thread))
3312       (while (and th
3313                   (not (eq (caar th) root)))
3314         (pop th))
3315       (if th
3316           ;; We have found a loop.
3317           (let (ref-dep)
3318             (setcdr thread (delq (car th) (cdr thread)))
3319             (if (boundp (setq ref-dep (intern "none"
3320                                               gnus-newsgroup-dependencies)))
3321                 (setcdr (symbol-value ref-dep)
3322                         (nconc (cdr (symbol-value ref-dep))
3323                                (list (car th))))
3324               (set ref-dep (list nil (car th))))
3325             (setq infloop 1
3326                   stack nil))
3327         ;; Push all the subthreads onto the stack.
3328         (push (cdr thread) stack)))
3329     infloop))
3330
3331 (defun gnus-make-threads ()
3332   "Go through the dependency hashtb and find the roots.  Return all threads."
3333   (let (threads)
3334     (while (catch 'infloop
3335              (mapatoms
3336               (lambda (refs)
3337                 ;; Deal with self-referencing References loops.
3338                 (when (and (car (symbol-value refs))
3339                            (not (zerop
3340                                  (apply
3341                                   '+
3342                                   (mapcar
3343                                    (lambda (thread)
3344                                      (gnus-thread-loop-p
3345                                       (car (symbol-value refs)) thread))
3346                                    (cdr (symbol-value refs)))))))
3347                   (setq threads nil)
3348                   (throw 'infloop t))
3349                 (unless (car (symbol-value refs))
3350                   ;; These threads do not refer back to any other articles,
3351                   ;; so they're roots.
3352                   (setq threads (append (cdr (symbol-value refs)) threads))))
3353               gnus-newsgroup-dependencies)))
3354     threads))
3355
3356 ;; Build the thread tree.
3357 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3358   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3359
3360 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3361 if it was already present.
3362
3363 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3364 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3365 Message-IDs will be renamed be renamed to a unique Message-ID before
3366 being entered.
3367
3368 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3369   (let* ((id (mail-header-id header))
3370          (id-dep (and id (intern id dependencies)))
3371          ref ref-dep ref-header)
3372     ;; Enter this `header' in the `dependencies' table.
3373     (cond
3374      ((not id-dep)
3375       (setq header nil))
3376      ;; The first two cases do the normal part: enter a new `header'
3377      ;; in the `dependencies' table.
3378      ((not (boundp id-dep))
3379       (set id-dep (list header)))
3380      ((null (car (symbol-value id-dep)))
3381       (setcar (symbol-value id-dep) header))
3382
3383      ;; From here the `header' was already present in the
3384      ;; `dependencies' table.
3385      (force-new
3386       ;; Overrides an existing entry;
3387       ;; just set the header part of the entry.
3388       (setcar (symbol-value id-dep) header))
3389
3390      ;; Renames the existing `header' to a unique Message-ID.
3391      ((not gnus-summary-ignore-duplicates)
3392       ;; An article with this Message-ID has already been seen.
3393       ;; We rename the Message-ID.
3394       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3395            (list header))
3396       (mail-header-set-id header id))
3397
3398      ;; The last case ignores an existing entry, except it adds any
3399      ;; additional Xrefs (in case the two articles came from different
3400      ;; servers.
3401      ;; Also sets `header' to `nil' meaning that the `dependencies'
3402      ;; table was *not* modified.
3403      (t
3404       (mail-header-set-xref
3405        (car (symbol-value id-dep))
3406        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3407                    "")
3408                (or (mail-header-xref header) "")))
3409       (setq header nil)))
3410
3411     (when header
3412       ;; First check if that we are not creating a References loop.
3413       (setq ref (gnus-parent-id (mail-header-references header)))
3414       (while (and ref
3415                   (setq ref-dep (intern-soft ref dependencies))
3416                   (boundp ref-dep)
3417                   (setq ref-header (car (symbol-value ref-dep))))
3418         (if (string= id ref)
3419             ;; Yuk!  This is a reference loop.  Make the article be a
3420             ;; root article.
3421             (progn
3422               (mail-header-set-references (car (symbol-value id-dep)) "none")
3423               (setq ref nil))
3424           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3425       (setq ref (gnus-parent-id (mail-header-references header)))
3426       (setq ref-dep (intern (or ref "none") dependencies))
3427       (if (boundp ref-dep)
3428           (setcdr (symbol-value ref-dep)
3429                   (nconc (cdr (symbol-value ref-dep))
3430                          (list (symbol-value id-dep))))
3431         (set ref-dep (list nil (symbol-value id-dep)))))
3432     header))
3433
3434 (defun gnus-build-sparse-threads ()
3435   (let ((headers gnus-newsgroup-headers)
3436         (mail-parse-charset gnus-newsgroup-charset)
3437         (gnus-summary-ignore-duplicates t)
3438         header references generation relations
3439         subject child end new-child date)
3440     ;; First we create an alist of generations/relations, where
3441     ;; generations is how much we trust the relation, and the relation
3442     ;; is parent/child.
3443     (gnus-message 7 "Making sparse threads...")
3444     (save-excursion
3445       (nnheader-set-temp-buffer " *gnus sparse threads*")
3446       (while (setq header (pop headers))
3447         (when (and (setq references (mail-header-references header))
3448                    (not (string= references "")))
3449           (insert references)
3450           (setq child (mail-header-id header)
3451                 subject (mail-header-subject header)
3452                 date (mail-header-date header)
3453                 generation 0)
3454           (while (search-backward ">" nil t)
3455             (setq end (1+ (point)))
3456             (when (search-backward "<" nil t)
3457               (setq new-child (buffer-substring (point) end))
3458               (push (list (incf generation)
3459                           child (setq child new-child)
3460                           subject date)
3461                     relations)))
3462           (when child
3463             (push (list (1+ generation) child nil subject) relations))
3464           (erase-buffer)))
3465       (kill-buffer (current-buffer)))
3466     ;; Sort over trustworthiness.
3467     (mapcar
3468      (lambda (relation)
3469        (when (gnus-dependencies-add-header
3470               (make-full-mail-header
3471                gnus-reffed-article-number
3472                (nth 3 relation) "" (or (nth 4 relation) "")
3473                (nth 1 relation)
3474                (or (nth 2 relation) "") 0 0 "")
3475               gnus-newsgroup-dependencies nil)
3476          (push gnus-reffed-article-number gnus-newsgroup-limit)
3477          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3478          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3479                gnus-newsgroup-reads)
3480          (decf gnus-reffed-article-number)))
3481      (sort relations 'car-less-than-car))
3482     (gnus-message 7 "Making sparse threads...done")))
3483
3484 (defun gnus-build-old-threads ()
3485   ;; Look at all the articles that refer back to old articles, and
3486   ;; fetch the headers for the articles that aren't there.  This will
3487   ;; build complete threads - if the roots haven't been expired by the
3488   ;; server, that is.
3489   (let ((mail-parse-charset gnus-newsgroup-charset)
3490         id heads)
3491     (mapatoms
3492      (lambda (refs)
3493        (when (not (car (symbol-value refs)))
3494          (setq heads (cdr (symbol-value refs)))
3495          (while heads
3496            (if (memq (mail-header-number (caar heads))
3497                      gnus-newsgroup-dormant)
3498                (setq heads (cdr heads))
3499              (setq id (symbol-name refs))
3500              (while (and (setq id (gnus-build-get-header id))
3501                          (not (car (gnus-id-to-thread id)))))
3502              (setq heads nil)))))
3503      gnus-newsgroup-dependencies)))
3504
3505 ;; This function has to be called with point after the article number
3506 ;; on the beginning of the line.
3507 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3508   (let ((eol (gnus-point-at-eol))
3509         (buffer (current-buffer))
3510         header)
3511
3512     ;; overview: [num subject from date id refs chars lines misc]
3513     (unwind-protect
3514         (progn
3515           (narrow-to-region (point) eol)
3516           (unless (eobp)
3517             (forward-char))
3518
3519           (setq header
3520                 (make-full-mail-header
3521                  number                 ; number
3522                  (funcall gnus-decode-encoded-word-function
3523                           (nnheader-nov-field)) ; subject
3524                  (funcall gnus-decode-encoded-word-function
3525                           (nnheader-nov-field)) ; from
3526                  (nnheader-nov-field)   ; date
3527                  (nnheader-nov-read-message-id) ; id
3528                  (nnheader-nov-field)   ; refs
3529                  (nnheader-nov-read-integer) ; chars
3530                  (nnheader-nov-read-integer) ; lines
3531                  (unless (eobp)
3532                    (if (looking-at "Xref: ")
3533                        (goto-char (match-end 0)))
3534                    (nnheader-nov-field)) ; Xref
3535                  (nnheader-nov-parse-extra)))) ; extra
3536
3537       (widen))
3538
3539     (when gnus-alter-header-function
3540       (funcall gnus-alter-header-function header))
3541     (gnus-dependencies-add-header header dependencies force-new)))
3542
3543 (defun gnus-build-get-header (id)
3544   "Look through the buffer of NOV lines and find the header to ID.
3545 Enter this line into the dependencies hash table, and return
3546 the id of the parent article (if any)."
3547   (let ((deps gnus-newsgroup-dependencies)
3548         found header)
3549     (prog1
3550         (save-excursion
3551           (set-buffer nntp-server-buffer)
3552           (let ((case-fold-search nil))
3553             (goto-char (point-min))
3554             (while (and (not found)
3555                         (search-forward id nil t))
3556               (beginning-of-line)
3557               (setq found (looking-at
3558                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3559                                    (regexp-quote id))))
3560               (or found (beginning-of-line 2)))
3561             (when found
3562               (beginning-of-line)
3563               (and
3564                (setq header (gnus-nov-parse-line
3565                              (read (current-buffer)) deps))
3566                (gnus-parent-id (mail-header-references header))))))
3567       (when header
3568         (let ((number (mail-header-number header)))
3569           (push number gnus-newsgroup-limit)
3570           (push header gnus-newsgroup-headers)
3571           (if (memq number gnus-newsgroup-unselected)
3572               (progn
3573                 (push number gnus-newsgroup-unreads)
3574                 (setq gnus-newsgroup-unselected
3575                       (delq number gnus-newsgroup-unselected)))
3576             (push number gnus-newsgroup-ancient)))))))
3577
3578 (defun gnus-build-all-threads ()
3579   "Read all the headers."
3580   (let ((gnus-summary-ignore-duplicates t)
3581         (mail-parse-charset gnus-newsgroup-charset)
3582         (dependencies gnus-newsgroup-dependencies)
3583         header article)
3584     (save-excursion
3585       (set-buffer nntp-server-buffer)
3586       (let ((case-fold-search nil))
3587         (goto-char (point-min))
3588         (while (not (eobp))
3589           (ignore-errors
3590             (setq article (read (current-buffer))
3591                   header (gnus-nov-parse-line article dependencies)))
3592           (when header
3593             (save-excursion
3594               (set-buffer gnus-summary-buffer)
3595               (push header gnus-newsgroup-headers)
3596               (if (memq (setq article (mail-header-number header))
3597                         gnus-newsgroup-unselected)
3598                   (progn
3599                     (push article gnus-newsgroup-unreads)
3600                     (setq gnus-newsgroup-unselected
3601                           (delq article gnus-newsgroup-unselected)))
3602                 (push article gnus-newsgroup-ancient)))
3603             (forward-line 1)))))))
3604
3605 (defun gnus-summary-update-article-line (article header)
3606   "Update the line for ARTICLE using HEADERS."
3607   (let* ((id (mail-header-id header))
3608          (thread (gnus-id-to-thread id)))
3609     (unless thread
3610       (error "Article in no thread"))
3611     ;; Update the thread.
3612     (setcar thread header)
3613     (gnus-summary-goto-subject article)
3614     (let* ((datal (gnus-data-find-list article))
3615            (data (car datal))
3616            (length (when (cdr datal)
3617                      (- (gnus-data-pos data)
3618                         (gnus-data-pos (cadr datal)))))
3619            (buffer-read-only nil)
3620            (level (gnus-summary-thread-level)))
3621       (gnus-delete-line)
3622       (gnus-summary-insert-line
3623        header level nil (gnus-article-mark article)
3624        (memq article gnus-newsgroup-replied)
3625        (memq article gnus-newsgroup-expirable)
3626        ;; Only insert the Subject string when it's different
3627        ;; from the previous Subject string.
3628        (if (and
3629             gnus-show-threads
3630             (gnus-subject-equal
3631              (condition-case ()
3632                  (mail-header-subject
3633                   (gnus-data-header
3634                    (cadr
3635                     (gnus-data-find-list
3636                      article
3637                      (gnus-data-list t)))))
3638                ;; Error on the side of excessive subjects.
3639                (error ""))
3640              (mail-header-subject header)))
3641            ""
3642          (mail-header-subject header))
3643        nil (cdr (assq article gnus-newsgroup-scored))
3644        (memq article gnus-newsgroup-processable))
3645       (when length
3646         (gnus-data-update-list
3647          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3648
3649 (defun gnus-summary-update-article (article &optional iheader)
3650   "Update ARTICLE in the summary buffer."
3651   (set-buffer gnus-summary-buffer)
3652   (let* ((header (gnus-summary-article-header article))
3653          (id (mail-header-id header))
3654          (data (gnus-data-find article))
3655          (thread (gnus-id-to-thread id))
3656          (references (mail-header-references header))
3657          (parent
3658           (gnus-id-to-thread
3659            (or (gnus-parent-id
3660                 (when (and references
3661                            (not (equal "" references)))
3662                   references))
3663                "none")))
3664          (buffer-read-only nil)
3665          (old (car thread)))
3666     (when thread
3667       (unless iheader
3668         (setcar thread nil)
3669         (when parent
3670           (delq thread parent)))
3671       (if (gnus-summary-insert-subject id header)
3672           ;; Set the (possibly) new article number in the data structure.
3673           (gnus-data-set-number data (gnus-id-to-article id))
3674         (setcar thread old)
3675         nil))))
3676
3677 (defun gnus-rebuild-thread (id &optional line)
3678   "Rebuild the thread containing ID.
3679 If LINE, insert the rebuilt thread starting on line LINE."
3680   (let ((buffer-read-only nil)
3681         old-pos current thread data)
3682     (if (not gnus-show-threads)
3683         (setq thread (list (car (gnus-id-to-thread id))))
3684       ;; Get the thread this article is part of.
3685       (setq thread (gnus-remove-thread id)))
3686     (setq old-pos (gnus-point-at-bol))
3687     (setq current (save-excursion
3688                     (and (zerop (forward-line -1))
3689                          (gnus-summary-article-number))))
3690     ;; If this is a gathered thread, we have to go some re-gathering.
3691     (when (stringp (car thread))
3692       (let ((subject (car thread))
3693             roots thr)
3694         (setq thread (cdr thread))
3695         (while thread
3696           (unless (memq (setq thr (gnus-id-to-thread
3697                                    (gnus-root-id
3698                                     (mail-header-id (caar thread)))))
3699                         roots)
3700             (push thr roots))
3701           (setq thread (cdr thread)))
3702         ;; We now have all (unique) roots.
3703         (if (= (length roots) 1)
3704             ;; All the loose roots are now one solid root.
3705             (setq thread (car roots))
3706           (setq thread (cons subject (gnus-sort-threads roots))))))
3707     (let (threads)
3708       ;; We then insert this thread into the summary buffer.
3709       (when line
3710         (goto-char (point-min))
3711         (forward-line (1- line)))
3712       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3713         (if gnus-show-threads
3714             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3715           (gnus-summary-prepare-unthreaded thread))
3716         (setq data (nreverse gnus-newsgroup-data))
3717         (setq threads gnus-newsgroup-threads))
3718       ;; We splice the new data into the data structure.
3719       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3720       ;;!!! then we want to insert at the beginning of the buffer.
3721       ;;!!! That happens to be true with Gnus now, but that may
3722       ;;!!! change in the future.  Perhaps.
3723       (gnus-data-enter-list
3724        (if line nil current) data (- (point) old-pos))
3725       (setq gnus-newsgroup-threads
3726             (nconc threads gnus-newsgroup-threads))
3727       (gnus-data-compute-positions))))
3728
3729 (defun gnus-number-to-header (number)
3730   "Return the header for article NUMBER."
3731   (let ((headers gnus-newsgroup-headers))
3732     (while (and headers
3733                 (not (= number (mail-header-number (car headers)))))
3734       (pop headers))
3735     (when headers
3736       (car headers))))
3737
3738 (defun gnus-parent-headers (in-headers &optional generation)
3739   "Return the headers of the GENERATIONeth parent of HEADERS."
3740   (unless generation
3741     (setq generation 1))
3742   (let ((parent t)
3743         (headers in-headers)
3744         references)
3745     (while (and parent
3746                 (not (zerop generation))
3747                 (setq references (mail-header-references headers)))
3748       (setq headers (if (and references
3749                              (setq parent (gnus-parent-id references)))
3750                         (car (gnus-id-to-thread parent))
3751                       nil))
3752       (decf generation))
3753     (and (not (eq headers in-headers))
3754          headers)))
3755
3756 (defun gnus-id-to-thread (id)
3757   "Return the (sub-)thread where ID appears."
3758   (gnus-gethash id gnus-newsgroup-dependencies))
3759
3760 (defun gnus-id-to-article (id)
3761   "Return the article number of ID."
3762   (let ((thread (gnus-id-to-thread id)))
3763     (when (and thread
3764                (car thread))
3765       (mail-header-number (car thread)))))
3766
3767 (defun gnus-id-to-header (id)
3768   "Return the article headers of ID."
3769   (car (gnus-id-to-thread id)))
3770
3771 (defun gnus-article-displayed-root-p (article)
3772   "Say whether ARTICLE is a root(ish) article."
3773   (let ((level (gnus-summary-thread-level article))
3774         (refs (mail-header-references  (gnus-summary-article-header article)))
3775         particle)
3776     (cond
3777      ((null level) nil)
3778      ((zerop level) t)
3779      ((null refs) t)
3780      ((null (gnus-parent-id refs)) t)
3781      ((and (= 1 level)
3782            (null (setq particle (gnus-id-to-article
3783                                  (gnus-parent-id refs))))
3784            (null (gnus-summary-thread-level particle)))))))
3785
3786 (defun gnus-root-id (id)
3787   "Return the id of the root of the thread where ID appears."
3788   (let (last-id prev)
3789     (while (and id (setq prev (car (gnus-id-to-thread id))))
3790       (setq last-id id
3791             id (gnus-parent-id (mail-header-references prev))))
3792     last-id))
3793
3794 (defun gnus-articles-in-thread (thread)
3795   "Return the list of articles in THREAD."
3796   (cons (mail-header-number (car thread))
3797         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3798
3799 (defun gnus-remove-thread (id &optional dont-remove)
3800   "Remove the thread that has ID in it."
3801   (let (headers thread last-id)
3802     ;; First go up in this thread until we find the root.
3803     (setq last-id (gnus-root-id id)
3804           headers (message-flatten-list (gnus-id-to-thread last-id)))
3805     ;; We have now found the real root of this thread.  It might have
3806     ;; been gathered into some loose thread, so we have to search
3807     ;; through the threads to find the thread we wanted.
3808     (let ((threads gnus-newsgroup-threads)
3809           sub)
3810       (while threads
3811         (setq sub (car threads))
3812         (if (stringp (car sub))
3813             ;; This is a gathered thread, so we look at the roots
3814             ;; below it to find whether this article is in this
3815             ;; gathered root.
3816             (progn
3817               (setq sub (cdr sub))
3818               (while sub
3819                 (when (member (caar sub) headers)
3820                   (setq thread (car threads)
3821                         threads nil
3822                         sub nil))
3823                 (setq sub (cdr sub))))
3824           ;; It's an ordinary thread, so we check it.
3825           (when (eq (car sub) (car headers))
3826             (setq thread sub
3827                   threads nil)))
3828         (setq threads (cdr threads)))
3829       ;; If this article is in no thread, then it's a root.
3830       (if thread
3831           (unless dont-remove
3832             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3833         (setq thread (gnus-id-to-thread last-id)))
3834       (when thread
3835         (prog1
3836             thread                      ; We return this thread.
3837           (unless dont-remove
3838             (if (stringp (car thread))
3839                 (progn
3840                   ;; If we use dummy roots, then we have to remove the
3841                   ;; dummy root as well.
3842                   (when (eq gnus-summary-make-false-root 'dummy)
3843                     ;; We go to the dummy root by going to
3844                     ;; the first sub-"thread", and then one line up.
3845                     (gnus-summary-goto-article
3846                      (mail-header-number (caadr thread)))
3847                     (forward-line -1)
3848                     (gnus-delete-line)
3849                     (gnus-data-compute-positions))
3850                   (setq thread (cdr thread))
3851                   (while thread
3852                     (gnus-remove-thread-1 (car thread))
3853                     (setq thread (cdr thread))))
3854               (gnus-remove-thread-1 thread))))))))
3855
3856 (defun gnus-remove-thread-1 (thread)
3857   "Remove the thread THREAD recursively."
3858   (let ((number (mail-header-number (pop thread)))
3859         d)
3860     (setq thread (reverse thread))
3861     (while thread
3862       (gnus-remove-thread-1 (pop thread)))
3863     (when (setq d (gnus-data-find number))
3864       (goto-char (gnus-data-pos d))
3865       (gnus-summary-show-thread)
3866       (gnus-data-remove
3867        number
3868        (- (gnus-point-at-bol)
3869           (prog1
3870               (1+ (gnus-point-at-eol))
3871             (gnus-delete-line)))))))
3872
3873 (defun gnus-sort-threads-1 (threads func)
3874   (sort (mapcar (lambda (thread)
3875                   (cons (car thread)
3876                         (and (cdr thread)
3877                              (gnus-sort-threads-1 (cdr thread) func))))
3878                 threads) func))
3879
3880 (defun gnus-sort-threads (threads)
3881   "Sort THREADS."
3882   (if (not gnus-thread-sort-functions)
3883       threads
3884     (gnus-message 8 "Sorting threads...")
3885     (prog1
3886         (gnus-sort-threads-1
3887          threads
3888          (gnus-make-sort-function gnus-thread-sort-functions))
3889       (gnus-message 8 "Sorting threads...done"))))
3890
3891 (defun gnus-sort-articles (articles)
3892   "Sort ARTICLES."
3893   (when gnus-article-sort-functions
3894     (gnus-message 7 "Sorting articles...")
3895     (prog1
3896         (setq gnus-newsgroup-headers
3897               (sort articles (gnus-make-sort-function
3898                               gnus-article-sort-functions)))
3899       (gnus-message 7 "Sorting articles...done"))))
3900
3901 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3902 (defmacro gnus-thread-header (thread)
3903   "Return header of first article in THREAD.
3904 Note that THREAD must never, ever be anything else than a variable -
3905 using some other form will lead to serious barfage."
3906   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3907   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3908   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3909         (vector thread) 2))
3910
3911 (defsubst gnus-article-sort-by-number (h1 h2)
3912   "Sort articles by article number."
3913   (< (mail-header-number h1)
3914      (mail-header-number h2)))
3915
3916 (defun gnus-thread-sort-by-number (h1 h2)
3917   "Sort threads by root article number."
3918   (gnus-article-sort-by-number
3919    (gnus-thread-header h1) (gnus-thread-header h2)))
3920
3921 (defsubst gnus-article-sort-by-lines (h1 h2)
3922   "Sort articles by article Lines header."
3923   (< (mail-header-lines h1)
3924      (mail-header-lines h2)))
3925
3926 (defun gnus-thread-sort-by-lines (h1 h2)
3927   "Sort threads by root article Lines header."
3928   (gnus-article-sort-by-lines
3929    (gnus-thread-header h1) (gnus-thread-header h2)))
3930
3931 (defsubst gnus-article-sort-by-chars (h1 h2)
3932   "Sort articles by octet length."
3933   (< (mail-header-chars h1)
3934      (mail-header-chars h2)))
3935
3936 (defun gnus-thread-sort-by-chars (h1 h2)
3937   "Sort threads by root article octet length."
3938   (gnus-article-sort-by-chars
3939    (gnus-thread-header h1) (gnus-thread-header h2)))
3940
3941 (defsubst gnus-article-sort-by-author (h1 h2)
3942   "Sort articles by root author."
3943   (string-lessp
3944    (let ((extract (funcall
3945                    gnus-extract-address-components
3946                    (mail-header-from h1))))
3947      (or (car extract) (cadr extract) ""))
3948    (let ((extract (funcall
3949                    gnus-extract-address-components
3950                    (mail-header-from h2))))
3951      (or (car extract) (cadr extract) ""))))
3952
3953 (defun gnus-thread-sort-by-author (h1 h2)
3954   "Sort threads by root author."
3955   (gnus-article-sort-by-author
3956    (gnus-thread-header h1)  (gnus-thread-header h2)))
3957
3958 (defsubst gnus-article-sort-by-subject (h1 h2)
3959   "Sort articles by root subject."
3960   (string-lessp
3961    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3962    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3963
3964 (defun gnus-thread-sort-by-subject (h1 h2)
3965   "Sort threads by root subject."
3966   (gnus-article-sort-by-subject
3967    (gnus-thread-header h1) (gnus-thread-header h2)))
3968
3969 (defsubst gnus-article-sort-by-date (h1 h2)
3970   "Sort articles by root article date."
3971   (time-less-p
3972    (gnus-date-get-time (mail-header-date h1))
3973    (gnus-date-get-time (mail-header-date h2))))
3974
3975 (defun gnus-thread-sort-by-date (h1 h2)
3976   "Sort threads by root article date."
3977   (gnus-article-sort-by-date
3978    (gnus-thread-header h1) (gnus-thread-header h2)))
3979
3980 (defsubst gnus-article-sort-by-score (h1 h2)
3981   "Sort articles by root article score.
3982 Unscored articles will be counted as having a score of zero."
3983   (> (or (cdr (assq (mail-header-number h1)
3984                     gnus-newsgroup-scored))
3985          gnus-summary-default-score 0)
3986      (or (cdr (assq (mail-header-number h2)
3987                     gnus-newsgroup-scored))
3988          gnus-summary-default-score 0)))
3989
3990 (defun gnus-thread-sort-by-score (h1 h2)
3991   "Sort threads by root article score."
3992   (gnus-article-sort-by-score
3993    (gnus-thread-header h1) (gnus-thread-header h2)))
3994
3995 (defun gnus-thread-sort-by-total-score (h1 h2)
3996   "Sort threads by the sum of all scores in the thread.
3997 Unscored articles will be counted as having a score of zero."
3998   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3999
4000 (defun gnus-thread-total-score (thread)
4001   ;; This function find the total score of THREAD.
4002   (cond ((null thread)
4003          0)
4004         ((consp thread)
4005          (if (stringp (car thread))
4006              (apply gnus-thread-score-function 0
4007                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4008            (gnus-thread-total-score-1 thread)))
4009         (t
4010          (gnus-thread-total-score-1 (list thread)))))
4011
4012 (defun gnus-thread-total-score-1 (root)
4013   ;; This function find the total score of the thread below ROOT.
4014   (setq root (car root))
4015   (apply gnus-thread-score-function
4016          (or (append
4017               (mapcar 'gnus-thread-total-score
4018                       (cdr (gnus-id-to-thread (mail-header-id root))))
4019               (when (> (mail-header-number root) 0)
4020                 (list (or (cdr (assq (mail-header-number root)
4021                                      gnus-newsgroup-scored))
4022                           gnus-summary-default-score 0))))
4023              (list gnus-summary-default-score)
4024              '(0))))
4025
4026 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4027 (defvar gnus-tmp-prev-subject nil)
4028 (defvar gnus-tmp-false-parent nil)
4029 (defvar gnus-tmp-root-expunged nil)
4030 (defvar gnus-tmp-dummy-line nil)
4031
4032 (eval-when-compile (defvar gnus-tmp-header))
4033 (defun gnus-extra-header (type &optional header)
4034   "Return the extra header of TYPE."
4035   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4036       ""))
4037
4038 (defun gnus-summary-prepare-threads (threads)
4039   "Prepare summary buffer from THREADS and indentation LEVEL.
4040 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4041 or a straight list of headers."
4042   (gnus-message 7 "Generating summary...")
4043
4044   (setq gnus-newsgroup-threads threads)
4045   (beginning-of-line)
4046
4047   (let ((gnus-tmp-level 0)
4048         (default-score (or gnus-summary-default-score 0))
4049         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4050         thread number subject stack state gnus-tmp-gathered beg-match
4051         new-roots gnus-tmp-new-adopts thread-end
4052         gnus-tmp-header gnus-tmp-unread
4053         gnus-tmp-replied gnus-tmp-subject-or-nil
4054         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4055         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4056         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
4057
4058     (setq gnus-tmp-prev-subject nil)
4059
4060     (if (vectorp (car threads))
4061         ;; If this is a straight (sic) list of headers, then a
4062         ;; threaded summary display isn't required, so we just create
4063         ;; an unthreaded one.
4064         (gnus-summary-prepare-unthreaded threads)
4065
4066       ;; Do the threaded display.
4067
4068       (while (or threads stack gnus-tmp-new-adopts new-roots)
4069
4070         (if (and (= gnus-tmp-level 0)
4071                  (or (not stack)
4072                      (= (caar stack) 0))
4073                  (not gnus-tmp-false-parent)
4074                  (or gnus-tmp-new-adopts new-roots))
4075             (if gnus-tmp-new-adopts
4076                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4077                       thread (list (car gnus-tmp-new-adopts))
4078                       gnus-tmp-header (caar thread)
4079                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4080               (when new-roots
4081                 (setq thread (list (car new-roots))
4082                       gnus-tmp-header (caar thread)
4083                       new-roots (cdr new-roots))))
4084
4085           (if threads
4086               ;; If there are some threads, we do them before the
4087               ;; threads on the stack.
4088               (setq thread threads
4089                     gnus-tmp-header (caar thread))
4090             ;; There were no current threads, so we pop something off
4091             ;; the stack.
4092             (setq state (car stack)
4093                   gnus-tmp-level (car state)
4094                   thread (cdr state)
4095                   stack (cdr stack)
4096                   gnus-tmp-header (caar thread))))
4097
4098         (setq gnus-tmp-false-parent nil)
4099         (setq gnus-tmp-root-expunged nil)
4100         (setq thread-end nil)
4101
4102         (if (stringp gnus-tmp-header)
4103             ;; The header is a dummy root.
4104             (cond
4105              ((eq gnus-summary-make-false-root 'adopt)
4106               ;; We let the first article adopt the rest.
4107               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4108                                                (cddar thread)))
4109               (setq gnus-tmp-gathered
4110                     (nconc (mapcar
4111                             (lambda (h) (mail-header-number (car h)))
4112                             (cddar thread))
4113                            gnus-tmp-gathered))
4114               (setq thread (cons (list (caar thread)
4115                                        (cadar thread))
4116                                  (cdr thread)))
4117               (setq gnus-tmp-level -1
4118                     gnus-tmp-false-parent t))
4119              ((eq gnus-summary-make-false-root 'empty)
4120               ;; We print adopted articles with empty subject fields.
4121               (setq gnus-tmp-gathered
4122                     (nconc (mapcar
4123                             (lambda (h) (mail-header-number (car h)))
4124                             (cddar thread))
4125                            gnus-tmp-gathered))
4126               (setq gnus-tmp-level -1))
4127              ((eq gnus-summary-make-false-root 'dummy)
4128               ;; We remember that we probably want to output a dummy
4129               ;; root.
4130               (setq gnus-tmp-dummy-line gnus-tmp-header)
4131               (setq gnus-tmp-prev-subject gnus-tmp-header))
4132              (t
4133               ;; We do not make a root for the gathered
4134               ;; sub-threads at all.
4135               (setq gnus-tmp-level -1)))
4136
4137           (setq number (mail-header-number gnus-tmp-header)
4138                 subject (mail-header-subject gnus-tmp-header))
4139
4140           (cond
4141            ;; If the thread has changed subject, we might want to make
4142            ;; this subthread into a root.
4143            ((and (null gnus-thread-ignore-subject)
4144                  (not (zerop gnus-tmp-level))
4145                  gnus-tmp-prev-subject
4146                  (not (inline
4147                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4148             (setq new-roots (nconc new-roots (list (car thread)))
4149                   thread-end t
4150                   gnus-tmp-header nil))
4151            ;; If the article lies outside the current limit,
4152            ;; then we do not display it.
4153            ((not (memq number gnus-newsgroup-limit))
4154             (setq gnus-tmp-gathered
4155                   (nconc (mapcar
4156                           (lambda (h) (mail-header-number (car h)))
4157                           (cdar thread))
4158                          gnus-tmp-gathered))
4159             (setq gnus-tmp-new-adopts (if (cdar thread)
4160                                           (append gnus-tmp-new-adopts
4161                                                   (cdar thread))
4162                                         gnus-tmp-new-adopts)
4163                   thread-end t
4164                   gnus-tmp-header nil)
4165             (when (zerop gnus-tmp-level)
4166               (setq gnus-tmp-root-expunged t)))
4167            ;; Perhaps this article is to be marked as read?
4168            ((and gnus-summary-mark-below
4169                  (< (or (cdr (assq number gnus-newsgroup-scored))
4170                         default-score)
4171                     gnus-summary-mark-below)
4172                  ;; Don't touch sparse articles.
4173                  (not (gnus-summary-article-sparse-p number))
4174                  (not (gnus-summary-article-ancient-p number)))
4175             (setq gnus-newsgroup-unreads
4176                   (delq number gnus-newsgroup-unreads))
4177             (if gnus-newsgroup-auto-expire
4178                 (push number gnus-newsgroup-expirable)
4179               (push (cons number gnus-low-score-mark)
4180                     gnus-newsgroup-reads))))
4181
4182           (when gnus-tmp-header
4183             ;; We may have an old dummy line to output before this
4184             ;; article.
4185             (when (and gnus-tmp-dummy-line
4186                        (gnus-subject-equal
4187                         gnus-tmp-dummy-line
4188                         (mail-header-subject gnus-tmp-header)))
4189               (gnus-summary-insert-dummy-line
4190                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4191               (setq gnus-tmp-dummy-line nil))
4192
4193             ;; Compute the mark.
4194             (setq gnus-tmp-unread (gnus-article-mark number))
4195
4196             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4197                                   gnus-tmp-header gnus-tmp-level)
4198                   gnus-newsgroup-data)
4199
4200             ;; Actually insert the line.
4201             (setq
4202              gnus-tmp-subject-or-nil
4203              (cond
4204               ((and gnus-thread-ignore-subject
4205                     gnus-tmp-prev-subject
4206                     (not (inline (gnus-subject-equal
4207                                   gnus-tmp-prev-subject subject))))
4208                subject)
4209               ((zerop gnus-tmp-level)
4210                (if (and (eq gnus-summary-make-false-root 'empty)
4211                         (memq number gnus-tmp-gathered)
4212                         gnus-tmp-prev-subject
4213                         (inline (gnus-subject-equal
4214                                  gnus-tmp-prev-subject subject)))
4215                    gnus-summary-same-subject
4216                  subject))
4217               (t gnus-summary-same-subject)))
4218             (if (and (eq gnus-summary-make-false-root 'adopt)
4219                      (= gnus-tmp-level 1)
4220                      (memq number gnus-tmp-gathered))
4221                 (setq gnus-tmp-opening-bracket ?\<
4222                       gnus-tmp-closing-bracket ?\>)
4223               (setq gnus-tmp-opening-bracket ?\[
4224                     gnus-tmp-closing-bracket ?\]))
4225             (setq
4226              gnus-tmp-indentation
4227              (aref gnus-thread-indent-array gnus-tmp-level)
4228              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4229              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4230                                 gnus-summary-default-score 0)
4231              gnus-tmp-score-char
4232              (if (or (null gnus-summary-default-score)
4233                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4234                          gnus-summary-zcore-fuzz))
4235                  ?  ;Whitespace
4236                (if (< gnus-tmp-score gnus-summary-default-score)
4237                    gnus-score-below-mark gnus-score-over-mark))
4238              gnus-tmp-replied
4239              (cond ((memq number gnus-newsgroup-processable)
4240                     gnus-process-mark)
4241                    ((memq number gnus-newsgroup-cached)
4242                     gnus-cached-mark)
4243                    ((memq number gnus-newsgroup-replied)
4244                     gnus-replied-mark)
4245                    ((memq number gnus-newsgroup-forwarded)
4246                     gnus-forwarded-mark)
4247                    ((memq number gnus-newsgroup-saved)
4248                     gnus-saved-mark)
4249                    (t gnus-no-mark))
4250              gnus-tmp-from (mail-header-from gnus-tmp-header)
4251              gnus-tmp-name
4252              (cond
4253               ((string-match "<[^>]+> *$" gnus-tmp-from)
4254                (setq beg-match (match-beginning 0))
4255                (or (and (string-match "^\".+\"" gnus-tmp-from)
4256                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4257                    (substring gnus-tmp-from 0 beg-match)))
4258               ((string-match "(.+)" gnus-tmp-from)
4259                (substring gnus-tmp-from
4260                           (1+ (match-beginning 0)) (1- (match-end 0))))
4261               (t gnus-tmp-from)))
4262             (when (string= gnus-tmp-name "")
4263               (setq gnus-tmp-name gnus-tmp-from))
4264             (unless (numberp gnus-tmp-lines)
4265               (setq gnus-tmp-lines -1))
4266             (when (= gnus-tmp-lines -1)
4267               (setq gnus-tmp-lines "?"))
4268             (gnus-put-text-property
4269              (point)
4270              (progn (eval gnus-summary-line-format-spec) (point))
4271              'gnus-number number)
4272             (when gnus-visual-p
4273               (forward-line -1)
4274               (gnus-run-hooks 'gnus-summary-update-hook)
4275               (forward-line 1))
4276
4277             (setq gnus-tmp-prev-subject subject)))
4278
4279         (when (nth 1 thread)
4280           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4281         (incf gnus-tmp-level)
4282         (setq threads (if thread-end nil (cdar thread)))
4283         (unless threads
4284           (setq gnus-tmp-level 0)))))
4285   (gnus-message 7 "Generating summary...done"))
4286
4287 (defun gnus-summary-prepare-unthreaded (headers)
4288   "Generate an unthreaded summary buffer based on HEADERS."
4289   (let (header number mark)
4290
4291     (beginning-of-line)
4292
4293     (while headers
4294       ;; We may have to root out some bad articles...
4295       (when (memq (setq number (mail-header-number
4296                                 (setq header (pop headers))))
4297                   gnus-newsgroup-limit)
4298         ;; Mark article as read when it has a low score.
4299         (when (and gnus-summary-mark-below
4300                    (< (or (cdr (assq number gnus-newsgroup-scored))
4301                           gnus-summary-default-score 0)
4302                       gnus-summary-mark-below)
4303                    (not (gnus-summary-article-ancient-p number)))
4304           (setq gnus-newsgroup-unreads
4305                 (delq number gnus-newsgroup-unreads))
4306           (if gnus-newsgroup-auto-expire
4307               (push number gnus-newsgroup-expirable)
4308             (push (cons number gnus-low-score-mark)
4309                   gnus-newsgroup-reads)))
4310
4311         (setq mark (gnus-article-mark number))
4312         (push (gnus-data-make number mark (1+ (point)) header 0)
4313               gnus-newsgroup-data)
4314         (gnus-summary-insert-line
4315          header 0 number
4316          mark (memq number gnus-newsgroup-replied)
4317          (memq number gnus-newsgroup-expirable)
4318          (mail-header-subject header) nil
4319          (cdr (assq number gnus-newsgroup-scored))
4320          (memq number gnus-newsgroup-processable))))))
4321
4322 (defun gnus-summary-remove-list-identifiers ()
4323   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4324   (let ((regexp (if (consp gnus-list-identifiers)
4325                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4326                   gnus-list-identifiers))
4327         changed subject)
4328     (when regexp
4329       (dolist (header gnus-newsgroup-headers)
4330         (setq subject (mail-header-subject header)
4331               changed nil)
4332         (while (string-match
4333                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4334                 subject)
4335           (setq subject
4336                 (concat (substring subject 0 (match-beginning 2))
4337                         (substring subject (match-end 0)))
4338                 changed t))
4339         (when (and changed
4340                    (string-match
4341                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4342           (setq subject
4343                 (concat (substring subject 0 (match-beginning 1))
4344                         (substring subject (match-end 1)))))
4345         (when changed
4346           (mail-header-set-subject header subject))))))
4347
4348 (defun gnus-fetch-headers (articles)
4349   "Fetch headers of ARTICLES."
4350   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4351     (gnus-message 5 "Fetching headers for %s..." name)
4352     (prog1
4353         (if (eq 'nov
4354                 (setq gnus-headers-retrieved-by
4355                       (gnus-retrieve-headers
4356                        articles gnus-newsgroup-name
4357                        ;; We might want to fetch old headers, but
4358                        ;; not if there is only 1 article.
4359                        (and (or (and
4360                                  (not (eq gnus-fetch-old-headers 'some))
4361                                  (not (numberp gnus-fetch-old-headers)))
4362                                 (> (length articles) 1))
4363                           gnus-fetch-old-headers))))
4364             (gnus-get-newsgroup-headers-xover
4365              articles nil nil gnus-newsgroup-name t)
4366           (gnus-get-newsgroup-headers))
4367       (gnus-message 5 "Fetching headers for %s...done" name))))
4368
4369 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4370   "Select newsgroup GROUP.
4371 If READ-ALL is non-nil, all articles in the group are selected.
4372 If SELECT-ARTICLES, only select those articles from GROUP."
4373   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4374          ;;!!! Dirty hack; should be removed.
4375          (gnus-summary-ignore-duplicates
4376           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4377               t
4378             gnus-summary-ignore-duplicates))
4379          (info (nth 2 entry))
4380          articles fetched-articles cached)
4381
4382     (unless (gnus-check-server
4383              (setq gnus-current-select-method
4384                    (gnus-find-method-for-group group)))
4385       (error "Couldn't open server"))
4386
4387     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4388         (gnus-activate-group group)     ; Or we can activate it...
4389         (progn                          ; Or we bug out.
4390           (when (equal major-mode 'gnus-summary-mode)
4391             (kill-buffer (current-buffer)))
4392           (error "Couldn't activate group %s: %s"
4393                  group (gnus-status-message group))))
4394
4395     (unless (gnus-request-group group t)
4396       (when (equal major-mode 'gnus-summary-mode)
4397         (kill-buffer (current-buffer)))
4398       (error "Couldn't request group %s: %s"
4399              group (gnus-status-message group)))
4400
4401     (setq gnus-newsgroup-name group)
4402     (setq gnus-newsgroup-unselected nil)
4403     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4404     (gnus-summary-setup-default-charset)
4405
4406     ;; Adjust and set lists of article marks.
4407     (when info
4408       (gnus-adjust-marked-articles info))
4409
4410     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4411     (when (gnus-virtual-group-p group)
4412       (setq cached gnus-newsgroup-cached))
4413
4414     (setq gnus-newsgroup-unreads
4415           (gnus-set-difference
4416            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4417            gnus-newsgroup-dormant))
4418
4419     (setq gnus-newsgroup-processable nil)
4420
4421     (gnus-update-read-articles group gnus-newsgroup-unreads)
4422
4423     (if (setq articles select-articles)
4424         (setq gnus-newsgroup-unselected
4425               (gnus-sorted-intersection
4426                gnus-newsgroup-unreads
4427                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4428       (setq articles (gnus-articles-to-read group read-all)))
4429
4430     (cond
4431      ((null articles)
4432       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4433       'quit)
4434      ((eq articles 0) nil)
4435      (t
4436       ;; Init the dependencies hash table.
4437       (setq gnus-newsgroup-dependencies
4438             (gnus-make-hashtable (length articles)))
4439       (gnus-set-global-variables)
4440       ;; Retrieve the headers and read them in.
4441       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4442
4443       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4444       (when cached
4445         (setq gnus-newsgroup-cached cached))
4446
4447       ;; Suppress duplicates?
4448       (when gnus-suppress-duplicates
4449         (gnus-dup-suppress-articles))
4450
4451       ;; Set the initial limit.
4452       (setq gnus-newsgroup-limit (copy-sequence articles))
4453       ;; Remove canceled articles from the list of unread articles.
4454       (setq gnus-newsgroup-unreads
4455             (gnus-set-sorted-intersection
4456              gnus-newsgroup-unreads
4457              (setq fetched-articles
4458                    (mapcar (lambda (headers) (mail-header-number headers))
4459                            gnus-newsgroup-headers))))
4460       ;; Removed marked articles that do not exist.
4461       (gnus-update-missing-marks
4462        (gnus-sorted-complement fetched-articles articles))
4463       ;; We might want to build some more threads first.
4464       (when (and gnus-fetch-old-headers
4465                  (eq gnus-headers-retrieved-by 'nov))
4466         (if (eq gnus-fetch-old-headers 'invisible)
4467             (gnus-build-all-threads)
4468           (gnus-build-old-threads)))
4469       ;; Let the Gnus agent mark articles as read.
4470       (when gnus-agent
4471         (gnus-agent-get-undownloaded-list))
4472       ;; Remove list identifiers from subject
4473       (when gnus-list-identifiers
4474         (gnus-summary-remove-list-identifiers))
4475       ;; Check whether auto-expire is to be done in this group.
4476       (setq gnus-newsgroup-auto-expire
4477             (gnus-group-auto-expirable-p group))
4478       ;; Set up the article buffer now, if necessary.
4479       (unless gnus-single-article-buffer
4480         (gnus-article-setup-buffer))
4481       ;; First and last article in this newsgroup.
4482       (when gnus-newsgroup-headers
4483         (setq gnus-newsgroup-begin
4484               (mail-header-number (car gnus-newsgroup-headers))
4485               gnus-newsgroup-end
4486               (mail-header-number
4487                (gnus-last-element gnus-newsgroup-headers))))
4488       ;; GROUP is successfully selected.
4489       (or gnus-newsgroup-headers t)))))
4490
4491 (defun gnus-articles-to-read (group &optional read-all)
4492   "Find out what articles the user wants to read."
4493   (let* ((articles
4494           ;; Select all articles if `read-all' is non-nil, or if there
4495           ;; are no unread articles.
4496           (if (or read-all
4497                   (and (zerop (length gnus-newsgroup-marked))
4498                        (zerop (length gnus-newsgroup-unreads)))
4499                   (eq (gnus-group-find-parameter group 'display)
4500                       'all))
4501               (or
4502                (gnus-uncompress-range (gnus-active group))
4503                (gnus-cache-articles-in-group group))
4504             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4505                           (copy-sequence gnus-newsgroup-unreads))
4506                   '<)))
4507          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4508          (scored (length scored-list))
4509          (number (length articles))
4510          (marked (+ (length gnus-newsgroup-marked)
4511                     (length gnus-newsgroup-dormant)))
4512          (select
4513           (cond
4514            ((numberp read-all)
4515             read-all)
4516            (t
4517             (condition-case ()
4518                 (cond
4519                  ((and (or (<= scored marked) (= scored number))
4520                        (numberp gnus-large-newsgroup)
4521                        (> number gnus-large-newsgroup))
4522                   (let ((input
4523                          (read-string
4524                           (format
4525                            "How many articles from %s (default %d): "
4526                            (gnus-limit-string gnus-newsgroup-name 35)
4527                            number))))
4528                     (if (string-match "^[ \t]*$" input) number input)))
4529                  ((and (> scored marked) (< scored number)
4530                        (> (- scored number) 20))
4531                   (let ((input
4532                          (read-string
4533                           (format "%s %s (%d scored, %d total): "
4534                                   "How many articles from"
4535                                   group scored number))))
4536                     (if (string-match "^[ \t]*$" input)
4537                         number input)))
4538                  (t number))
4539               (quit
4540                (message "Quit getting the articles to read")
4541                nil))))))
4542     (setq select (if (stringp select) (string-to-number select) select))
4543     (if (or (null select) (zerop select))
4544         select
4545       (if (and (not (zerop scored)) (<= (abs select) scored))
4546           (progn
4547             (setq articles (sort scored-list '<))
4548             (setq number (length articles)))
4549         (setq articles (copy-sequence articles)))
4550
4551       (when (< (abs select) number)
4552         (if (< select 0)
4553             ;; Select the N oldest articles.
4554             (setcdr (nthcdr (1- (abs select)) articles) nil)
4555           ;; Select the N most recent articles.
4556           (setq articles (nthcdr (- number select) articles))))
4557       (setq gnus-newsgroup-unselected
4558             (gnus-sorted-intersection
4559              gnus-newsgroup-unreads
4560              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4561       (when gnus-alter-articles-to-read-function
4562         (setq gnus-newsgroup-unreads
4563               (sort
4564                (funcall gnus-alter-articles-to-read-function
4565                         gnus-newsgroup-name gnus-newsgroup-unreads)
4566                '<)))
4567       articles)))
4568
4569 (defun gnus-killed-articles (killed articles)
4570   (let (out)
4571     (while articles
4572       (when (inline (gnus-member-of-range (car articles) killed))
4573         (push (car articles) out))
4574       (setq articles (cdr articles)))
4575     out))
4576
4577 (defun gnus-uncompress-marks (marks)
4578   "Uncompress the mark ranges in MARKS."
4579   (let ((uncompressed '(score bookmark))
4580         out)
4581     (while marks
4582       (if (memq (caar marks) uncompressed)
4583           (push (car marks) out)
4584         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4585       (setq marks (cdr marks)))
4586     out))
4587
4588 (defun gnus-adjust-marked-articles (info)
4589   "Set all article lists and remove all marks that are no longer valid."
4590   (let* ((marked-lists (gnus-info-marks info))
4591          (active (gnus-active (gnus-info-group info)))
4592          (min (car active))
4593          (max (cdr active))
4594          (types gnus-article-mark-lists)
4595          (uncompressed '(score bookmark killed))
4596          marks var articles article mark)
4597
4598     (while marked-lists
4599       (setq marks (pop marked-lists))
4600       (set (setq var (intern (format "gnus-newsgroup-%s"
4601                                      (car (rassq (setq mark (car marks))
4602                                                  types)))))
4603            (if (memq (car marks) uncompressed) (cdr marks)
4604              (gnus-uncompress-range (cdr marks))))
4605
4606       (setq articles (symbol-value var))
4607
4608       ;; All articles have to be subsets of the active articles.
4609       (cond
4610        ;; Adjust "simple" lists.
4611        ((memq mark '(tick dormant expire reply save))
4612         (while articles
4613           (when (or (< (setq article (pop articles)) min) (> article max))
4614             (set var (delq article (symbol-value var))))))
4615        ;; Adjust assocs.
4616        ((memq mark uncompressed)
4617         (when (not (listp (cdr (symbol-value var))))
4618           (set var (list (symbol-value var))))
4619         (when (not (listp (cdr articles)))
4620           (setq articles (list articles)))
4621         (while articles
4622           (when (or (not (consp (setq article (pop articles))))
4623                     (< (car article) min)
4624                     (> (car article) max))
4625             (set var (delq article (symbol-value var))))))))))
4626
4627 (defun gnus-update-missing-marks (missing)
4628   "Go through the list of MISSING articles and remove them from the mark lists."
4629   (when missing
4630     (let ((types gnus-article-mark-lists)
4631           var m)
4632       ;; Go through all types.
4633       (while types
4634         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4635         (when (symbol-value var)
4636           ;; This list has articles.  So we delete all missing articles
4637           ;; from it.
4638           (setq m missing)
4639           (while m
4640             (set var (delq (pop m) (symbol-value var)))))))))
4641
4642 (defun gnus-update-marks ()
4643   "Enter the various lists of marked articles into the newsgroup info list."
4644   (let ((types gnus-article-mark-lists)
4645         (info (gnus-get-info gnus-newsgroup-name))
4646         (uncompressed '(score bookmark killed))
4647         type list newmarked symbol delta-marks)
4648     (when info
4649       ;; Add all marks lists to the list of marks lists.
4650       (while (setq type (pop types))
4651         (setq list (symbol-value
4652                     (setq symbol
4653                           (intern (format "gnus-newsgroup-%s"
4654                                           (car type))))))
4655
4656         (when list
4657           ;; Get rid of the entries of the articles that have the
4658           ;; default score.
4659           (when (and (eq (cdr type) 'score)
4660                      gnus-save-score
4661                      list)
4662             (let* ((arts list)
4663                    (prev (cons nil list))
4664                    (all prev))
4665               (while arts
4666                 (if (or (not (consp (car arts)))
4667                         (= (cdar arts) gnus-summary-default-score))
4668                     (setcdr prev (cdr arts))
4669                   (setq prev arts))
4670                 (setq arts (cdr arts)))
4671               (setq list (cdr all)))))
4672
4673         (unless (memq (cdr type) uncompressed)
4674           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4675
4676         (when (gnus-check-backend-function
4677                'request-set-mark gnus-newsgroup-name)
4678           ;; propagate flags to server, with the following exceptions:
4679           ;; uncompressed:s are not proper flags (they are cons cells)
4680           ;; cache is a internal gnus flag
4681           ;; download are local to one gnus installation (well)
4682           ;; unsend are for nndraft groups only
4683           ;; xxx: generality of this?  this suits nnimap anyway
4684           (unless (memq (cdr type) (append '(cache download unsend)
4685                                            uncompressed))
4686             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4687                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4688                    (add (gnus-remove-from-range
4689                          (gnus-copy-sequence list) old)))
4690               (when add
4691                 (push (list add 'add (list (cdr type))) delta-marks))
4692               (when del
4693                 (push (list del 'del (list (cdr type))) delta-marks)))))
4694
4695         (when list
4696           (push (cons (cdr type) list) newmarked)))
4697
4698       (when delta-marks
4699         (unless (gnus-check-group gnus-newsgroup-name)
4700           (error "Can't open server for %s" gnus-newsgroup-name))
4701         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4702
4703       ;; Enter these new marks into the info of the group.
4704       (if (nthcdr 3 info)
4705           (setcar (nthcdr 3 info) newmarked)
4706         ;; Add the marks lists to the end of the info.
4707         (when newmarked
4708           (setcdr (nthcdr 2 info) (list newmarked))))
4709
4710       ;; Cut off the end of the info if there's nothing else there.
4711       (let ((i 5))
4712         (while (and (> i 2)
4713                     (not (nth i info)))
4714           (when (nthcdr (decf i) info)
4715             (setcdr (nthcdr i info) nil)))))))
4716
4717 (defun gnus-set-mode-line (where)
4718   "Set the mode line of the article or summary buffers.
4719 If WHERE is `summary', the summary mode line format will be used."
4720   ;; Is this mode line one we keep updated?
4721   (when (and (memq where gnus-updated-mode-lines)
4722              (symbol-value
4723               (intern (format "gnus-%s-mode-line-format-spec" where))))
4724     (let (mode-string)
4725       (save-excursion
4726         ;; We evaluate this in the summary buffer since these
4727         ;; variables are buffer-local to that buffer.
4728         (set-buffer gnus-summary-buffer)
4729         ;; We bind all these variables that are used in the `eval' form
4730         ;; below.
4731         (let* ((mformat (symbol-value
4732                          (intern
4733                           (format "gnus-%s-mode-line-format-spec" where))))
4734                (gnus-tmp-group-name (gnus-group-decoded-name 
4735                                      gnus-newsgroup-name))
4736                (gnus-tmp-article-number (or gnus-current-article 0))
4737                (gnus-tmp-unread gnus-newsgroup-unreads)
4738                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4739                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4740                (gnus-tmp-unread-and-unselected
4741                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4742                             (zerop gnus-tmp-unselected))
4743                        "")
4744                       ((zerop gnus-tmp-unselected)
4745                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4746                       (t (format "{%d(+%d) more}"
4747                                  gnus-tmp-unread-and-unticked
4748                                  gnus-tmp-unselected))))
4749                (gnus-tmp-subject
4750                 (if (and gnus-current-headers
4751                          (vectorp gnus-current-headers))
4752                     (gnus-mode-string-quote
4753                      (mail-header-subject gnus-current-headers))
4754                   ""))
4755                bufname-length max-len
4756                gnus-tmp-header);; passed as argument to any user-format-funcs
4757           (setq mode-string (eval mformat))
4758           (setq bufname-length (if (string-match "%b" mode-string)
4759                                    (- (length
4760                                        (buffer-name
4761                                         (if (eq where 'summary)
4762                                             nil
4763                                           (get-buffer gnus-article-buffer))))
4764                                       2)
4765                                  0))
4766           (setq max-len (max 4 (if gnus-mode-non-string-length
4767                                    (- (window-width)
4768                                       gnus-mode-non-string-length
4769                                       bufname-length)
4770                                  (length mode-string))))
4771           ;; We might have to chop a bit of the string off...
4772           (when (> (length mode-string) max-len)
4773             (setq mode-string
4774                   (concat (truncate-string-to-width mode-string (- max-len 3))
4775                           "...")))
4776           ;; Pad the mode string a bit.
4777           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4778       ;; Update the mode line.
4779       (setq mode-line-buffer-identification
4780             (gnus-mode-line-buffer-identification (list mode-string)))
4781       (set-buffer-modified-p t))))
4782
4783 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4784   "Go through the HEADERS list and add all Xrefs to a hash table.
4785 The resulting hash table is returned, or nil if no Xrefs were found."
4786   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4787          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4788          (xref-hashtb (gnus-make-hashtable))
4789          start group entry number xrefs header)
4790     (while headers
4791       (setq header (pop headers))
4792       (when (and (setq xrefs (mail-header-xref header))
4793                  (not (memq (setq number (mail-header-number header))
4794                             unreads)))
4795         (setq start 0)
4796         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4797           (setq start (match-end 0))
4798           (setq group (if prefix
4799                           (concat prefix (substring xrefs (match-beginning 1)
4800                                                     (match-end 1)))
4801                         (substring xrefs (match-beginning 1) (match-end 1))))
4802           (setq number
4803                 (string-to-int (substring xrefs (match-beginning 2)
4804                                           (match-end 2))))
4805           (if (setq entry (gnus-gethash group xref-hashtb))
4806               (setcdr entry (cons number (cdr entry)))
4807             (gnus-sethash group (cons number nil) xref-hashtb)))))
4808     (and start xref-hashtb)))
4809
4810 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4811   "Look through all the headers and mark the Xrefs as read."
4812   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4813         name entry info xref-hashtb idlist method nth4)
4814     (save-excursion
4815       (set-buffer gnus-group-buffer)
4816       (when (setq xref-hashtb
4817                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4818         (mapatoms
4819          (lambda (group)
4820            (unless (string= from-newsgroup (setq name (symbol-name group)))
4821              (setq idlist (symbol-value group))
4822              ;; Dead groups are not updated.
4823              (and (prog1
4824                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4825                             info (nth 2 entry))
4826                     (when (stringp (setq nth4 (gnus-info-method info)))
4827                       (setq nth4 (gnus-server-to-method nth4))))
4828                   ;; Only do the xrefs if the group has the same
4829                   ;; select method as the group we have just read.
4830                   (or (gnus-methods-equal-p
4831                        nth4 (gnus-find-method-for-group from-newsgroup))
4832                       virtual
4833                       (equal nth4 (setq method (gnus-find-method-for-group
4834                                                 from-newsgroup)))
4835                       (and (equal (car nth4) (car method))
4836                            (equal (nth 1 nth4) (nth 1 method))))
4837                   gnus-use-cross-reference
4838                   (or (not (eq gnus-use-cross-reference t))
4839                       virtual
4840                       ;; Only do cross-references on subscribed
4841                       ;; groups, if that is what is wanted.
4842                       (<= (gnus-info-level info) gnus-level-subscribed))
4843                   (gnus-group-make-articles-read name idlist))))
4844          xref-hashtb)))))
4845
4846 (defun gnus-compute-read-articles (group articles)
4847   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4848          (info (nth 2 entry))
4849          (active (gnus-active group))
4850          ninfo)
4851     (when entry
4852       ;; First peel off all invalid article numbers.
4853       (when active
4854         (let ((ids articles)
4855               id first)
4856           (while (setq id (pop ids))
4857             (when (and first (> id (cdr active)))
4858               ;; We'll end up in this situation in one particular
4859               ;; obscure situation.  If you re-scan a group and get
4860               ;; a new article that is cross-posted to a different
4861               ;; group that has not been re-scanned, you might get
4862               ;; crossposted article that has a higher number than
4863               ;; Gnus believes possible.  So we re-activate this
4864               ;; group as well.  This might mean doing the
4865               ;; crossposting thingy will *increase* the number
4866               ;; of articles in some groups.  Tsk, tsk.
4867               (setq active (or (gnus-activate-group group) active)))
4868             (when (or (> id (cdr active))
4869                       (< id (car active)))
4870               (setq articles (delq id articles))))))
4871       ;; If the read list is nil, we init it.
4872       (if (and active
4873                (null (gnus-info-read info))
4874                (> (car active) 1))
4875           (setq ninfo (cons 1 (1- (car active))))
4876         (setq ninfo (gnus-info-read info)))
4877       ;; Then we add the read articles to the range.
4878       (gnus-add-to-range
4879        ninfo (setq articles (sort articles '<))))))
4880
4881 (defun gnus-group-make-articles-read (group articles)
4882   "Update the info of GROUP to say that ARTICLES are read."
4883   (let* ((num 0)
4884          (entry (gnus-gethash group gnus-newsrc-hashtb))
4885          (info (nth 2 entry))
4886          (active (gnus-active group))
4887          range)
4888     (when entry
4889       (setq range (gnus-compute-read-articles group articles))
4890       (save-excursion
4891         (set-buffer gnus-group-buffer)
4892         (gnus-undo-register
4893           `(progn
4894              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4895              (gnus-info-set-read ',info ',(gnus-info-read info))
4896              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4897              (gnus-group-update-group ,group t))))
4898       ;; Add the read articles to the range.
4899       (gnus-info-set-read info range)
4900       ;; Then we have to re-compute how many unread
4901       ;; articles there are in this group.
4902       (when active
4903         (cond
4904          ((not range)
4905           (setq num (- (1+ (cdr active)) (car active))))
4906          ((not (listp (cdr range)))
4907           (setq num (- (cdr active) (- (1+ (cdr range))
4908                                        (car range)))))
4909          (t
4910           (while range
4911             (if (numberp (car range))
4912                 (setq num (1+ num))
4913               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4914             (setq range (cdr range)))
4915           (setq num (- (cdr active) num))))
4916         ;; Update the number of unread articles.
4917         (setcar entry num)
4918         ;; Update the group buffer.
4919         (gnus-group-update-group group t)))))
4920
4921 (defvar gnus-newsgroup-none-id 0)
4922
4923 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4924   (let ((cur nntp-server-buffer)
4925         (dependencies
4926          (or dependencies
4927              (save-excursion (set-buffer gnus-summary-buffer)
4928                              gnus-newsgroup-dependencies)))
4929         headers id end ref
4930         (mail-parse-charset gnus-newsgroup-charset)
4931         (mail-parse-ignored-charsets
4932          (save-excursion (condition-case nil
4933                              (set-buffer gnus-summary-buffer)
4934                            (error))
4935                          gnus-newsgroup-ignored-charsets)))
4936     (save-excursion
4937       (set-buffer nntp-server-buffer)
4938       ;; Translate all TAB characters into SPACE characters.
4939       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4940       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4941       (gnus-run-hooks 'gnus-parse-headers-hook)
4942       (let ((case-fold-search t)
4943             in-reply-to header p lines chars)
4944         (goto-char (point-min))
4945         ;; Search to the beginning of the next header.  Error messages
4946         ;; do not begin with 2 or 3.
4947         (while (re-search-forward "^[23][0-9]+ " nil t)
4948           (setq id nil
4949                 ref nil)
4950           ;; This implementation of this function, with nine
4951           ;; search-forwards instead of the one re-search-forward and
4952           ;; a case (which basically was the old function) is actually
4953           ;; about twice as fast, even though it looks messier.  You
4954           ;; can't have everything, I guess.  Speed and elegance
4955           ;; doesn't always go hand in hand.
4956           (setq
4957            header
4958            (vector
4959             ;; Number.
4960             (prog1
4961                 (read cur)
4962               (end-of-line)
4963               (setq p (point))
4964               (narrow-to-region (point)
4965                                 (or (and (search-forward "\n.\n" nil t)
4966                                          (- (point) 2))
4967                                     (point))))
4968             ;; Subject.
4969             (progn
4970               (goto-char p)
4971               (if (search-forward "\nsubject: " nil t)
4972                   (funcall gnus-decode-encoded-word-function
4973                            (nnheader-header-value))
4974                 "(none)"))
4975             ;; From.
4976             (progn
4977               (goto-char p)
4978               (if (or (search-forward "\nfrom: " nil t)
4979                       (search-forward "\nfrom:" nil t))
4980                   (funcall gnus-decode-encoded-word-function
4981                            (nnheader-header-value))
4982                 "(nobody)"))
4983             ;; Date.
4984             (progn
4985               (goto-char p)
4986               (if (search-forward "\ndate: " nil t)
4987                   (nnheader-header-value) ""))
4988             ;; Message-ID.
4989             (progn
4990               (goto-char p)
4991               (setq id (if (re-search-forward
4992                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4993                            ;; We do it this way to make sure the Message-ID
4994                            ;; is (somewhat) syntactically valid.
4995                            (buffer-substring (match-beginning 1)
4996                                              (match-end 1))
4997                          ;; If there was no message-id, we just fake one
4998                          ;; to make subsequent routines simpler.
4999                          (nnheader-generate-fake-message-id))))
5000             ;; References.
5001             (progn
5002               (goto-char p)
5003               (if (search-forward "\nreferences: " nil t)
5004                   (progn
5005                     (setq end (point))
5006                     (prog1
5007                         (nnheader-header-value)
5008                       (setq ref
5009                             (buffer-substring
5010                              (progn
5011                                (end-of-line)
5012                                (search-backward ">" end t)
5013                                (1+ (point)))
5014                              (progn
5015                                (search-backward "<" end t)
5016                                (point))))))
5017                 ;; Get the references from the in-reply-to header if there
5018                 ;; were no references and the in-reply-to header looks
5019                 ;; promising.
5020                 (if (and (search-forward "\nin-reply-to: " nil t)
5021                          (setq in-reply-to (nnheader-header-value))
5022                          (string-match "<[^>]+>" in-reply-to))
5023                     (let (ref2)
5024                       (setq ref (substring in-reply-to (match-beginning 0)
5025                                            (match-end 0)))
5026                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5027                         (setq ref2 (substring in-reply-to (match-beginning 0)
5028                                               (match-end 0)))
5029                         (when (> (length ref2) (length ref))
5030                           (setq ref ref2)))
5031                       ref)
5032                   (setq ref nil))))
5033             ;; Chars.
5034             (progn
5035               (goto-char p)
5036               (if (search-forward "\nchars: " nil t)
5037                   (if (numberp (setq chars (ignore-errors (read cur))))
5038                       chars -1)
5039                 -1))
5040             ;; Lines.
5041             (progn
5042               (goto-char p)
5043               (if (search-forward "\nlines: " nil t)
5044                   (if (numberp (setq lines (ignore-errors (read cur))))
5045                       lines -1)
5046                 -1))
5047             ;; Xref.
5048             (progn
5049               (goto-char p)
5050               (and (search-forward "\nxref: " nil t)
5051                    (nnheader-header-value)))
5052             ;; Extra.
5053             (when gnus-extra-headers
5054               (let ((extra gnus-extra-headers)
5055                     out)
5056                 (while extra
5057                   (goto-char p)
5058                   (when (search-forward
5059                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
5060                     (push (cons (car extra) (nnheader-header-value))
5061                           out))
5062                   (pop extra))
5063                 out))))
5064           (when (equal id ref)
5065             (setq ref nil))
5066
5067           (when gnus-alter-header-function
5068             (funcall gnus-alter-header-function header)
5069             (setq id (mail-header-id header)
5070                   ref (gnus-parent-id (mail-header-references header))))
5071
5072           (when (setq header
5073                       (gnus-dependencies-add-header
5074                        header dependencies force-new))
5075             (push header headers))
5076           (goto-char (point-max))
5077           (widen))
5078         (nreverse headers)))))
5079
5080 ;; Goes through the xover lines and returns a list of vectors
5081 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5082                                                   force-new dependencies
5083                                                   group also-fetch-heads)
5084   "Parse the news overview data in the server buffer.
5085 Return a list of headers that match SEQUENCE (see
5086 `nntp-retrieve-headers')."
5087   ;; Get the Xref when the users reads the articles since most/some
5088   ;; NNTP servers do not include Xrefs when using XOVER.
5089   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5090   (let ((mail-parse-charset gnus-newsgroup-charset)
5091         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5092         (cur nntp-server-buffer)
5093         (dependencies (or dependencies gnus-newsgroup-dependencies))
5094         number headers header)
5095     (save-excursion
5096       (set-buffer nntp-server-buffer)
5097       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5098       ;; Allow the user to mangle the headers before parsing them.
5099       (gnus-run-hooks 'gnus-parse-headers-hook)
5100       (goto-char (point-min))
5101       (while (not (eobp))
5102         (condition-case ()
5103             (while (and sequence (not (eobp)))
5104               (setq number (read cur))
5105               (while (and sequence
5106                           (< (car sequence) number))
5107                 (setq sequence (cdr sequence)))
5108               (and sequence
5109                    (eq number (car sequence))
5110                    (progn
5111                      (setq sequence (cdr sequence))
5112                      (setq header (inline
5113                                     (gnus-nov-parse-line
5114                                      number dependencies force-new))))
5115                    (push header headers))
5116               (forward-line 1))
5117           (error
5118            (gnus-error 4 "Strange nov line (%d)"
5119                        (count-lines (point-min) (point)))))
5120         (forward-line 1))
5121       ;; A common bug in inn is that if you have posted an article and
5122       ;; then retrieves the active file, it will answer correctly --
5123       ;; the new article is included.  However, a NOV entry for the
5124       ;; article may not have been generated yet, so this may fail.
5125       ;; We work around this problem by retrieving the last few
5126       ;; headers using HEAD.
5127       (if (or (not also-fetch-heads)
5128               (not sequence))
5129           ;; We (probably) got all the headers.
5130           (nreverse headers)
5131         (let ((gnus-nov-is-evil t))
5132           (nconc
5133            (nreverse headers)
5134            (when (gnus-retrieve-headers sequence group)
5135              (gnus-get-newsgroup-headers))))))))
5136
5137 (defun gnus-article-get-xrefs ()
5138   "Fill in the Xref value in `gnus-current-headers', if necessary.
5139 This is meant to be called in `gnus-article-internal-prepare-hook'."
5140   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5141                                  gnus-current-headers)))
5142     (or (not gnus-use-cross-reference)
5143         (not headers)
5144         (and (mail-header-xref headers)
5145              (not (string= (mail-header-xref headers) "")))
5146         (let ((case-fold-search t)
5147               xref)
5148           (save-restriction
5149             (nnheader-narrow-to-headers)
5150             (goto-char (point-min))
5151             (when (or (and (not (eobp))
5152                            (eq (downcase (char-after)) ?x)
5153                            (looking-at "Xref:"))
5154                       (search-forward "\nXref:" nil t))
5155               (goto-char (1+ (match-end 0)))
5156               (setq xref (buffer-substring (point)
5157                                            (progn (end-of-line) (point))))
5158               (mail-header-set-xref headers xref)))))))
5159
5160 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5161   "Find article ID and insert the summary line for that article.
5162 OLD-HEADER can either be a header or a line number to insert
5163 the subject line on."
5164   (let* ((line (and (numberp old-header) old-header))
5165          (old-header (and (vectorp old-header) old-header))
5166          (header (cond ((and old-header use-old-header)
5167                         old-header)
5168                        ((and (numberp id)
5169                              (gnus-number-to-header id))
5170                         (gnus-number-to-header id))
5171                        (t
5172                         (gnus-read-header id))))
5173          (number (and (numberp id) id))
5174          d)
5175     (when header
5176       ;; Rebuild the thread that this article is part of and go to the
5177       ;; article we have fetched.
5178       (when (and (not gnus-show-threads)
5179                  old-header)
5180         (when (and number
5181                    (setq d (gnus-data-find (mail-header-number old-header))))
5182           (goto-char (gnus-data-pos d))
5183           (gnus-data-remove
5184            number
5185            (- (gnus-point-at-bol)
5186               (prog1
5187                   (1+ (gnus-point-at-eol))
5188                 (gnus-delete-line))))))
5189       (when old-header
5190         (mail-header-set-number header (mail-header-number old-header)))
5191       (setq gnus-newsgroup-sparse
5192             (delq (setq number (mail-header-number header))
5193                   gnus-newsgroup-sparse))
5194       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5195       (push number gnus-newsgroup-limit)
5196       (gnus-rebuild-thread (mail-header-id header) line)
5197       (gnus-summary-goto-subject number nil t))
5198     (when (and (numberp number)
5199                (> number 0))
5200       ;; We have to update the boundaries even if we can't fetch the
5201       ;; article if ID is a number -- so that the next `P' or `N'
5202       ;; command will fetch the previous (or next) article even
5203       ;; if the one we tried to fetch this time has been canceled.
5204       (when (> number gnus-newsgroup-end)
5205         (setq gnus-newsgroup-end number))
5206       (when (< number gnus-newsgroup-begin)
5207         (setq gnus-newsgroup-begin number))
5208       (setq gnus-newsgroup-unselected
5209             (delq number gnus-newsgroup-unselected)))
5210     ;; Report back a success?
5211     (and header (mail-header-number header))))
5212
5213 ;;; Process/prefix in the summary buffer
5214
5215 (defun gnus-summary-work-articles (n)
5216   "Return a list of articles to be worked upon.
5217 The prefix argument, the list of process marked articles, and the
5218 current article will be taken into consideration."
5219   (save-excursion
5220     (set-buffer gnus-summary-buffer)
5221     (cond
5222      (n
5223       ;; A numerical prefix has been given.
5224       (setq n (prefix-numeric-value n))
5225       (let ((backward (< n 0))
5226             (n (abs (prefix-numeric-value n)))
5227             articles article)
5228         (save-excursion
5229           (while
5230               (and (> n 0)
5231                    (push (setq article (gnus-summary-article-number))
5232                          articles)
5233                    (if backward
5234                        (gnus-summary-find-prev nil article)
5235                      (gnus-summary-find-next nil article)))
5236             (decf n)))
5237         (nreverse articles)))
5238      ((and (gnus-region-active-p) (mark))
5239       (message "region active")
5240       ;; Work on the region between point and mark.
5241       (let ((max (max (point) (mark)))
5242             articles article)
5243         (save-excursion
5244           (goto-char (min (min (point) (mark))))
5245           (while
5246               (and
5247                (push (setq article (gnus-summary-article-number)) articles)
5248                (gnus-summary-find-next nil article)
5249                (< (point) max)))
5250           (nreverse articles))))
5251      (gnus-newsgroup-processable
5252       ;; There are process-marked articles present.
5253       ;; Save current state.
5254       (gnus-summary-save-process-mark)
5255       ;; Return the list.
5256       (reverse gnus-newsgroup-processable))
5257      (t
5258       ;; Just return the current article.
5259       (list (gnus-summary-article-number))))))
5260
5261 (defmacro gnus-summary-iterate (arg &rest forms)
5262   "Iterate over the process/prefixed articles and do FORMS.
5263 ARG is the interactive prefix given to the command.  FORMS will be
5264 executed with point over the summary line of the articles."
5265   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5266     `(let ((,articles (gnus-summary-work-articles ,arg)))
5267        (while ,articles
5268          (gnus-summary-goto-subject (car ,articles))
5269          ,@forms
5270          (pop ,articles)))))
5271
5272 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5273 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5274
5275 (defun gnus-summary-save-process-mark ()
5276   "Push the current set of process marked articles on the stack."
5277   (interactive)
5278   (push (copy-sequence gnus-newsgroup-processable)
5279         gnus-newsgroup-process-stack))
5280
5281 (defun gnus-summary-kill-process-mark ()
5282   "Push the current set of process marked articles on the stack and unmark."
5283   (interactive)
5284   (gnus-summary-save-process-mark)
5285   (gnus-summary-unmark-all-processable))
5286
5287 (defun gnus-summary-yank-process-mark ()
5288   "Pop the last process mark state off the stack and restore it."
5289   (interactive)
5290   (unless gnus-newsgroup-process-stack
5291     (error "Empty mark stack"))
5292   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5293
5294 (defun gnus-summary-process-mark-set (set)
5295   "Make SET into the current process marked articles."
5296   (gnus-summary-unmark-all-processable)
5297   (while set
5298     (gnus-summary-set-process-mark (pop set))))
5299
5300 ;;; Searching and stuff
5301
5302 (defun gnus-summary-search-group (&optional backward use-level)
5303   "Search for next unread newsgroup.
5304 If optional argument BACKWARD is non-nil, search backward instead."
5305   (save-excursion
5306     (set-buffer gnus-group-buffer)
5307     (when (gnus-group-search-forward
5308            backward nil (if use-level (gnus-group-group-level) nil))
5309       (gnus-group-group-name))))
5310
5311 (defun gnus-summary-best-group (&optional exclude-group)
5312   "Find the name of the best unread group.
5313 If EXCLUDE-GROUP, do not go to this group."
5314   (save-excursion
5315     (set-buffer gnus-group-buffer)
5316     (save-excursion
5317       (gnus-group-best-unread-group exclude-group))))
5318
5319 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5320   (if backward (gnus-summary-find-prev)
5321     (let* ((dummy (gnus-summary-article-intangible-p))
5322            (article (or article (gnus-summary-article-number)))
5323            (arts (gnus-data-find-list article))
5324            result)
5325       (when (and (not dummy)
5326                  (or (not gnus-summary-check-current)
5327                      (not unread)
5328                      (not (gnus-data-unread-p (car arts)))))
5329         (setq arts (cdr arts)))
5330       (when (setq result
5331                   (if unread
5332                       (progn
5333                         (while arts
5334                           (when (or (and undownloaded
5335                                          (eq gnus-undownloaded-mark
5336                                              (gnus-data-mark (car arts))))
5337                                     (gnus-data-unread-p (car arts)))
5338                             (setq result (car arts)
5339                                   arts nil))
5340                           (setq arts (cdr arts)))
5341                         result)
5342                     (car arts)))
5343         (goto-char (gnus-data-pos result))
5344         (gnus-data-number result)))))
5345
5346 (defun gnus-summary-find-prev (&optional unread article)
5347   (let* ((eobp (eobp))
5348          (article (or article (gnus-summary-article-number)))
5349          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5350          result)
5351     (when (and (not eobp)
5352                (or (not gnus-summary-check-current)
5353                    (not unread)
5354                    (not (gnus-data-unread-p (car arts)))))
5355       (setq arts (cdr arts)))
5356     (when (setq result
5357                 (if unread
5358                     (progn
5359                       (while arts
5360                         (when (gnus-data-unread-p (car arts))
5361                           (setq result (car arts)
5362                                 arts nil))
5363                         (setq arts (cdr arts)))
5364                       result)
5365                   (car arts)))
5366       (goto-char (gnus-data-pos result))
5367       (gnus-data-number result))))
5368
5369 (defun gnus-summary-find-subject (subject &optional unread backward article)
5370   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5371          (article (or article (gnus-summary-article-number)))
5372          (articles (gnus-data-list backward))
5373          (arts (gnus-data-find-list article articles))
5374          result)
5375     (when (or (not gnus-summary-check-current)
5376               (not unread)
5377               (not (gnus-data-unread-p (car arts))))
5378       (setq arts (cdr arts)))
5379     (while arts
5380       (and (or (not unread)
5381                (gnus-data-unread-p (car arts)))
5382            (vectorp (gnus-data-header (car arts)))
5383            (gnus-subject-equal
5384             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5385            (setq result (car arts)
5386                  arts nil))
5387       (setq arts (cdr arts)))
5388     (and result
5389          (goto-char (gnus-data-pos result))
5390          (gnus-data-number result))))
5391
5392 (defun gnus-summary-search-forward (&optional unread subject backward)
5393   "Search forward for an article.
5394 If UNREAD, look for unread articles.  If SUBJECT, look for
5395 articles with that subject.  If BACKWARD, search backward instead."
5396   (cond (subject (gnus-summary-find-subject subject unread backward))
5397         (backward (gnus-summary-find-prev unread))
5398         (t (gnus-summary-find-next unread))))
5399
5400 (defun gnus-recenter (&optional n)
5401   "Center point in window and redisplay frame.
5402 Also do horizontal recentering."
5403   (interactive "P")
5404   (when (and gnus-auto-center-summary
5405              (not (eq gnus-auto-center-summary 'vertical)))
5406     (gnus-horizontal-recenter))
5407   (recenter n))
5408
5409 (defun gnus-summary-recenter ()
5410   "Center point in the summary window.
5411 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5412 displayed, no centering will be performed."
5413   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5414   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5415   (interactive)
5416   (let* ((top (cond ((< (window-height) 4) 0)
5417                     ((< (window-height) 7) 1)
5418                     (t (if (numberp gnus-auto-center-summary)
5419                            gnus-auto-center-summary
5420                          2))))
5421          (height (1- (window-height)))
5422          (bottom (save-excursion (goto-char (point-max))
5423                                  (forward-line (- height))
5424                                  (point)))
5425          (window (get-buffer-window (current-buffer))))
5426     ;; The user has to want it.
5427     (when gnus-auto-center-summary
5428       (when (get-buffer-window gnus-article-buffer)
5429         ;; Only do recentering when the article buffer is displayed,
5430         ;; Set the window start to either `bottom', which is the biggest
5431         ;; possible valid number, or the second line from the top,
5432         ;; whichever is the least.
5433         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5434           (if (> bottom top-pos)
5435               ;; Keep the second line from the top visible
5436               (set-window-start window top-pos t)
5437             ;; Try to keep the bottom line visible; if it's partially
5438             ;; obscured, either scroll one more line to make it fully
5439             ;; visible, or revert to using TOP-POS.
5440             (save-excursion
5441               (goto-char (point-max))
5442               (forward-line -1)
5443               (let ((last-line-start (point)))
5444                 (goto-char bottom)
5445                 (set-window-start window (point) t)
5446                 (when (not (pos-visible-in-window-p last-line-start window))
5447                   (forward-line 1)
5448                   (set-window-start window (min (point) top-pos) t)))))))
5449       ;; Do horizontal recentering while we're at it.
5450       (when (and (get-buffer-window (current-buffer) t)
5451                  (not (eq gnus-auto-center-summary 'vertical)))
5452         (let ((selected (selected-window)))
5453           (select-window (get-buffer-window (current-buffer) t))
5454           (gnus-summary-position-point)
5455           (gnus-horizontal-recenter)
5456           (select-window selected))))))
5457
5458 (defun gnus-summary-jump-to-group (newsgroup)
5459   "Move point to NEWSGROUP in group mode buffer."
5460   ;; Keep update point of group mode buffer if visible.
5461   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5462       (save-window-excursion
5463         ;; Take care of tree window mode.
5464         (when (get-buffer-window gnus-group-buffer)
5465           (pop-to-buffer gnus-group-buffer))
5466         (gnus-group-jump-to-group newsgroup))
5467     (save-excursion
5468       ;; Take care of tree window mode.
5469       (if (get-buffer-window gnus-group-buffer)
5470           (pop-to-buffer gnus-group-buffer)
5471         (set-buffer gnus-group-buffer))
5472       (gnus-group-jump-to-group newsgroup))))
5473
5474 ;; This function returns a list of article numbers based on the
5475 ;; difference between the ranges of read articles in this group and
5476 ;; the range of active articles.
5477 (defun gnus-list-of-unread-articles (group)
5478   (let* ((read (gnus-info-read (gnus-get-info group)))
5479          (active (or (gnus-active group) (gnus-activate-group group)))
5480          (last (cdr active))
5481          first nlast unread)
5482     ;; If none are read, then all are unread.
5483     (if (not read)
5484         (setq first (car active))
5485       ;; If the range of read articles is a single range, then the
5486       ;; first unread article is the article after the last read
5487       ;; article.  Sounds logical, doesn't it?
5488       (if (and (not (listp (cdr read)))
5489                (or (< (car read) (car active))
5490                    (progn (setq read (list read))
5491                           nil)))
5492           (setq first (max (car active) (1+ (cdr read))))
5493         ;; `read' is a list of ranges.
5494         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5495                                   (caar read)))
5496                   1)
5497           (setq first (car active)))
5498         (while read
5499           (when first
5500             (while (< first nlast)
5501               (push first unread)
5502               (setq first (1+ first))))
5503           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5504           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5505           (setq read (cdr read)))))
5506     ;; And add the last unread articles.
5507     (while (<= first last)
5508       (push first unread)
5509       (setq first (1+ first)))
5510     ;; Return the list of unread articles.
5511     (delq 0 (nreverse unread))))
5512
5513 (defun gnus-list-of-read-articles (group)
5514   "Return a list of unread, unticked and non-dormant articles."
5515   (let* ((info (gnus-get-info group))
5516          (marked (gnus-info-marks info))
5517          (active (gnus-active group)))
5518     (and info active
5519          (gnus-set-difference
5520           (gnus-sorted-complement
5521            (gnus-uncompress-range active)
5522            (gnus-list-of-unread-articles group))
5523           (append
5524            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5525            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5526
5527 ;; Various summary commands
5528
5529 (defun gnus-summary-select-article-buffer ()
5530   "Reconfigure windows to show article buffer."
5531   (interactive)
5532   (if (not (gnus-buffer-live-p gnus-article-buffer))
5533       (error "There is no article buffer for this summary buffer")
5534     (gnus-configure-windows 'article)
5535     (select-window (get-buffer-window gnus-article-buffer))))
5536
5537 (defun gnus-summary-universal-argument (arg)
5538   "Perform any operation on all articles that are process/prefixed."
5539   (interactive "P")
5540   (let ((articles (gnus-summary-work-articles arg))
5541         func article)
5542     (if (eq
5543          (setq
5544           func
5545           (key-binding
5546            (read-key-sequence
5547             (substitute-command-keys
5548              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5549          'undefined)
5550         (gnus-error 1 "Undefined key")
5551       (save-excursion
5552         (while articles
5553           (gnus-summary-goto-subject (setq article (pop articles)))
5554           (let (gnus-newsgroup-processable)
5555             (command-execute func))
5556           (gnus-summary-remove-process-mark article)))))
5557   (gnus-summary-position-point))
5558
5559 (defun gnus-summary-toggle-truncation (&optional arg)
5560   "Toggle truncation of summary lines.
5561 With arg, turn line truncation on iff arg is positive."
5562   (interactive "P")
5563   (setq truncate-lines
5564         (if (null arg) (not truncate-lines)
5565           (> (prefix-numeric-value arg) 0)))
5566   (redraw-display))
5567
5568 (defun gnus-summary-reselect-current-group (&optional all rescan)
5569   "Exit and then reselect the current newsgroup.
5570 The prefix argument ALL means to select all articles."
5571   (interactive "P")
5572   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5573     (error "Ephemeral groups can't be reselected"))
5574   (let ((current-subject (gnus-summary-article-number))
5575         (group gnus-newsgroup-name))
5576     (setq gnus-newsgroup-begin nil)
5577     (gnus-summary-exit)
5578     ;; We have to adjust the point of group mode buffer because
5579     ;; point was moved to the next unread newsgroup by exiting.
5580     (gnus-summary-jump-to-group group)
5581     (when rescan
5582       (save-excursion
5583         (gnus-group-get-new-news-this-group 1)))
5584     (gnus-group-read-group all t)
5585     (gnus-summary-goto-subject current-subject nil t)))
5586
5587 (defun gnus-summary-rescan-group (&optional all)
5588   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5589   (interactive "P")
5590   (gnus-summary-reselect-current-group all t))
5591
5592 (defun gnus-summary-update-info (&optional non-destructive)
5593   (save-excursion
5594     (let ((group gnus-newsgroup-name))
5595       (when group
5596         (when gnus-newsgroup-kill-headers
5597           (setq gnus-newsgroup-killed
5598                 (gnus-compress-sequence
5599                  (nconc
5600                   (gnus-set-sorted-intersection
5601                    (gnus-uncompress-range gnus-newsgroup-killed)
5602                    (setq gnus-newsgroup-unselected
5603                          (sort gnus-newsgroup-unselected '<)))
5604                   (setq gnus-newsgroup-unreads
5605                         (sort gnus-newsgroup-unreads '<)))
5606                  t)))
5607         (unless (listp (cdr gnus-newsgroup-killed))
5608           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5609         (let ((headers gnus-newsgroup-headers))
5610           ;; Set the new ranges of read articles.
5611           (save-excursion
5612             (set-buffer gnus-group-buffer)
5613             (gnus-undo-force-boundary))
5614           (gnus-update-read-articles
5615            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5616           ;; Set the current article marks.
5617           (let ((gnus-newsgroup-scored
5618                  (if (and (not gnus-save-score)
5619                           (not non-destructive))
5620                      nil
5621                    gnus-newsgroup-scored)))
5622             (save-excursion
5623               (gnus-update-marks)))
5624           ;; Do the cross-ref thing.
5625           (when gnus-use-cross-reference
5626             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5627           ;; Do not switch windows but change the buffer to work.
5628           (set-buffer gnus-group-buffer)
5629           (unless (gnus-ephemeral-group-p group)
5630             (gnus-group-update-group group)))))))
5631
5632 (defun gnus-summary-save-newsrc (&optional force)
5633   "Save the current number of read/marked articles in the dribble buffer.
5634 The dribble buffer will then be saved.
5635 If FORCE (the prefix), also save the .newsrc file(s)."
5636   (interactive "P")
5637   (gnus-summary-update-info t)
5638   (if force
5639       (gnus-save-newsrc-file)
5640     (gnus-dribble-save)))
5641
5642 (defun gnus-summary-exit (&optional temporary)
5643   "Exit reading current newsgroup, and then return to group selection mode.
5644 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5645   (interactive)
5646   (gnus-set-global-variables)
5647   (when (gnus-buffer-live-p gnus-article-buffer)
5648     (save-excursion
5649       (set-buffer gnus-article-buffer)
5650       (mm-destroy-parts gnus-article-mime-handles)
5651       ;; Set it to nil for safety reason.
5652       (setq gnus-article-mime-handle-alist nil)
5653       (setq gnus-article-mime-handles nil)))
5654   (gnus-kill-save-kill-buffer)
5655   (gnus-async-halt-prefetch)
5656   (let* ((group gnus-newsgroup-name)
5657          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5658          (mode major-mode)
5659          (group-point nil)
5660          (buf (current-buffer)))
5661     (unless quit-config
5662       ;; Do adaptive scoring, and possibly save score files.
5663       (when gnus-newsgroup-adaptive
5664         (gnus-score-adaptive))
5665       (when gnus-use-scoring
5666         (gnus-score-save)))
5667     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5668     ;; If we have several article buffers, we kill them at exit.
5669     (unless gnus-single-article-buffer
5670       (gnus-kill-buffer gnus-original-article-buffer)
5671       (setq gnus-article-current nil))
5672     (when gnus-use-cache
5673       (gnus-cache-possibly-remove-articles)
5674       (gnus-cache-save-buffers))
5675     (gnus-async-prefetch-remove-group group)
5676     (when gnus-suppress-duplicates
5677       (gnus-dup-enter-articles))
5678     (when gnus-use-trees
5679       (gnus-tree-close group))
5680     (when gnus-use-cache
5681       (gnus-cache-write-active))
5682     ;; Remove entries for this group.
5683     (nnmail-purge-split-history (gnus-group-real-name group))
5684     ;; Make all changes in this group permanent.
5685     (unless quit-config
5686       (gnus-run-hooks 'gnus-exit-group-hook)
5687       (gnus-summary-update-info))
5688     (gnus-close-group group)
5689     ;; Make sure where we were, and go to next newsgroup.
5690     (set-buffer gnus-group-buffer)
5691     (unless quit-config
5692       (gnus-group-jump-to-group group))
5693     (gnus-run-hooks 'gnus-summary-exit-hook)
5694     (unless (or quit-config
5695                 ;; If this group has disappeared from the summary
5696                 ;; buffer, don't skip forwards.
5697                 (not (string= group (gnus-group-group-name))))
5698       (gnus-group-next-unread-group 1))
5699     (setq group-point (point))
5700     (if temporary
5701         nil                             ;Nothing to do.
5702       ;; If we have several article buffers, we kill them at exit.
5703       (unless gnus-single-article-buffer
5704         (gnus-kill-buffer gnus-article-buffer)
5705         (gnus-kill-buffer gnus-original-article-buffer)
5706         (setq gnus-article-current nil))
5707       (set-buffer buf)
5708       (if (not gnus-kill-summary-on-exit)
5709           (progn
5710             (gnus-deaden-summary)
5711             (setq mode nil))
5712         ;; We set all buffer-local variables to nil.  It is unclear why
5713         ;; this is needed, but if we don't, buffer-local variables are
5714         ;; not garbage-collected, it seems.  This would the lead to en
5715         ;; ever-growing Emacs.
5716         (gnus-summary-clear-local-variables)
5717         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5718           (gnus-summary-clear-local-variables))
5719         (when (get-buffer gnus-article-buffer)
5720           (bury-buffer gnus-article-buffer))
5721         ;; We clear the global counterparts of the buffer-local
5722         ;; variables as well, just to be on the safe side.
5723         (set-buffer gnus-group-buffer)
5724         (gnus-summary-clear-local-variables)
5725         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5726           (gnus-summary-clear-local-variables)))
5727       (setq gnus-current-select-method gnus-select-method)
5728       (pop-to-buffer gnus-group-buffer)
5729       (if (not quit-config)
5730           (progn
5731             (goto-char group-point)
5732             (gnus-configure-windows 'group 'force))
5733         (gnus-handle-ephemeral-exit quit-config))
5734       ;; Return to group mode buffer.
5735       (when (eq mode 'gnus-summary-mode)
5736         (gnus-kill-buffer buf))
5737       ;; Clear the current group name.
5738       (unless quit-config
5739         (setq gnus-newsgroup-name nil)))))
5740
5741 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5742 (defun gnus-summary-exit-no-update (&optional no-questions)
5743   "Quit reading current newsgroup without updating read article info."
5744   (interactive)
5745   (let* ((group gnus-newsgroup-name)
5746          (quit-config (gnus-group-quit-config group)))
5747     (when (or no-questions
5748               gnus-expert-user
5749               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5750       (gnus-async-halt-prefetch)
5751       (mapcar 'funcall
5752               (delq 'gnus-summary-expire-articles
5753                     (copy-sequence gnus-summary-prepare-exit-hook)))
5754       (when (gnus-buffer-live-p gnus-article-buffer)
5755         (save-excursion
5756           (set-buffer gnus-article-buffer)
5757           (mm-destroy-parts gnus-article-mime-handles)
5758           ;; Set it to nil for safety reason.
5759           (setq gnus-article-mime-handle-alist nil)
5760           (setq gnus-article-mime-handles nil)))
5761       ;; If we have several article buffers, we kill them at exit.
5762       (unless gnus-single-article-buffer
5763         (gnus-kill-buffer gnus-article-buffer)
5764         (gnus-kill-buffer gnus-original-article-buffer)
5765         (setq gnus-article-current nil))
5766       (if (not gnus-kill-summary-on-exit)
5767           (gnus-deaden-summary)
5768         (gnus-close-group group)
5769         (gnus-summary-clear-local-variables)
5770         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5771           (gnus-summary-clear-local-variables))
5772         (set-buffer gnus-group-buffer)
5773         (gnus-summary-clear-local-variables)
5774         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5775           (gnus-summary-clear-local-variables))
5776         (when (get-buffer gnus-summary-buffer)
5777           (kill-buffer gnus-summary-buffer)))
5778       (unless gnus-single-article-buffer
5779         (setq gnus-article-current nil))
5780       (when gnus-use-trees
5781         (gnus-tree-close group))
5782       (gnus-async-prefetch-remove-group group)
5783       (when (get-buffer gnus-article-buffer)
5784         (bury-buffer gnus-article-buffer))
5785       ;; Return to the group buffer.
5786       (gnus-configure-windows 'group 'force)
5787       ;; Clear the current group name.
5788       (setq gnus-newsgroup-name nil)
5789       (when (equal (gnus-group-group-name) group)
5790         (gnus-group-next-unread-group 1))
5791       (when quit-config
5792         (gnus-handle-ephemeral-exit quit-config)))))
5793
5794 (defun gnus-handle-ephemeral-exit (quit-config)
5795   "Handle movement when leaving an ephemeral group.
5796 The state which existed when entering the ephemeral is reset."
5797   (if (not (buffer-name (car quit-config)))
5798       (gnus-configure-windows 'group 'force)
5799     (set-buffer (car quit-config))
5800     (cond ((eq major-mode 'gnus-summary-mode)
5801            (gnus-set-global-variables))
5802           ((eq major-mode 'gnus-article-mode)
5803            (save-excursion
5804              ;; The `gnus-summary-buffer' variable may point
5805              ;; to the old summary buffer when using a single
5806              ;; article buffer.
5807              (unless (gnus-buffer-live-p gnus-summary-buffer)
5808                (set-buffer gnus-group-buffer))
5809              (set-buffer gnus-summary-buffer)
5810              (gnus-set-global-variables))))
5811     (if (or (eq (cdr quit-config) 'article)
5812             (eq (cdr quit-config) 'pick))
5813         (progn
5814           ;; The current article may be from the ephemeral group
5815           ;; thus it is best that we reload this article
5816           (gnus-summary-show-article)
5817           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5818               (gnus-configure-windows 'pick 'force)
5819             (gnus-configure-windows (cdr quit-config) 'force)))
5820       (gnus-configure-windows (cdr quit-config) 'force))
5821     (when (eq major-mode 'gnus-summary-mode)
5822       (gnus-summary-next-subject 1 nil t)
5823       (gnus-summary-recenter)
5824       (gnus-summary-position-point))))
5825
5826 ;;; Dead summaries.
5827
5828 (defvar gnus-dead-summary-mode-map nil)
5829
5830 (unless gnus-dead-summary-mode-map
5831   (setq gnus-dead-summary-mode-map (make-keymap))
5832   (suppress-keymap gnus-dead-summary-mode-map)
5833   (substitute-key-definition
5834    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5835   (let ((keys '("\C-d" "\r" "\177" [delete])))
5836     (while keys
5837       (define-key gnus-dead-summary-mode-map
5838         (pop keys) 'gnus-summary-wake-up-the-dead))))
5839
5840 (defvar gnus-dead-summary-mode nil
5841   "Minor mode for Gnus summary buffers.")
5842
5843 (defun gnus-dead-summary-mode (&optional arg)
5844   "Minor mode for Gnus summary buffers."
5845   (interactive "P")
5846   (when (eq major-mode 'gnus-summary-mode)
5847     (make-local-variable 'gnus-dead-summary-mode)
5848     (setq gnus-dead-summary-mode
5849           (if (null arg) (not gnus-dead-summary-mode)
5850             (> (prefix-numeric-value arg) 0)))
5851     (when gnus-dead-summary-mode
5852       (gnus-add-minor-mode
5853        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5854
5855 (defun gnus-deaden-summary ()
5856   "Make the current summary buffer into a dead summary buffer."
5857   ;; Kill any previous dead summary buffer.
5858   (when (and gnus-dead-summary
5859              (buffer-name gnus-dead-summary))
5860     (save-excursion
5861       (set-buffer gnus-dead-summary)
5862       (when gnus-dead-summary-mode
5863         (kill-buffer (current-buffer)))))
5864   ;; Make this the current dead summary.
5865   (setq gnus-dead-summary (current-buffer))
5866   (gnus-dead-summary-mode 1)
5867   (let ((name (buffer-name)))
5868     (when (string-match "Summary" name)
5869       (rename-buffer
5870        (concat (substring name 0 (match-beginning 0)) "Dead "
5871                (substring name (match-beginning 0)))
5872        t)
5873       (bury-buffer))))
5874
5875 (defun gnus-kill-or-deaden-summary (buffer)
5876   "Kill or deaden the summary BUFFER."
5877   (save-excursion
5878     (when (and (buffer-name buffer)
5879                (not gnus-single-article-buffer))
5880       (save-excursion
5881         (set-buffer buffer)
5882         (gnus-kill-buffer gnus-article-buffer)
5883         (gnus-kill-buffer gnus-original-article-buffer)))
5884     (cond (gnus-kill-summary-on-exit
5885            (when (and gnus-use-trees
5886                       (gnus-buffer-exists-p buffer))
5887              (save-excursion
5888                (set-buffer buffer)
5889                (gnus-tree-close gnus-newsgroup-name)))
5890            (gnus-kill-buffer buffer))
5891           ((gnus-buffer-exists-p buffer)
5892            (save-excursion
5893              (set-buffer buffer)
5894              (gnus-deaden-summary))))))
5895
5896 (defun gnus-summary-wake-up-the-dead (&rest args)
5897   "Wake up the dead summary buffer."
5898   (interactive)
5899   (gnus-dead-summary-mode -1)
5900   (let ((name (buffer-name)))
5901     (when (string-match "Dead " name)
5902       (rename-buffer
5903        (concat (substring name 0 (match-beginning 0))
5904                (substring name (match-end 0)))
5905        t)))
5906   (gnus-message 3 "This dead summary is now alive again"))
5907
5908 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5909 (defun gnus-summary-fetch-faq (&optional faq-dir)
5910   "Fetch the FAQ for the current group.
5911 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5912 in."
5913   (interactive
5914    (list
5915     (when current-prefix-arg
5916       (completing-read
5917        "Faq dir: " (and (listp gnus-group-faq-directory)
5918                         (mapcar (lambda (file) (list file))
5919                                 gnus-group-faq-directory))))))
5920   (let (gnus-faq-buffer)
5921     (when (setq gnus-faq-buffer
5922                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5923       (gnus-configure-windows 'summary-faq))))
5924
5925 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5926 (defun gnus-summary-describe-group (&optional force)
5927   "Describe the current newsgroup."
5928   (interactive "P")
5929   (gnus-group-describe-group force gnus-newsgroup-name))
5930
5931 (defun gnus-summary-describe-briefly ()
5932   "Describe summary mode commands briefly."
5933   (interactive)
5934   (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")))
5935
5936 ;; Walking around group mode buffer from summary mode.
5937
5938 (defun gnus-summary-next-group (&optional no-article target-group backward)
5939   "Exit current newsgroup and then select next unread newsgroup.
5940 If prefix argument NO-ARTICLE is non-nil, no article is selected
5941 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5942 previous group instead."
5943   (interactive "P")
5944   ;; Stop pre-fetching.
5945   (gnus-async-halt-prefetch)
5946   (let ((current-group gnus-newsgroup-name)
5947         (current-buffer (current-buffer))
5948         entered)
5949     ;; First we semi-exit this group to update Xrefs and all variables.
5950     ;; We can't do a real exit, because the window conf must remain
5951     ;; the same in case the user is prompted for info, and we don't
5952     ;; want the window conf to change before that...
5953     (gnus-summary-exit t)
5954     (while (not entered)
5955       ;; Then we find what group we are supposed to enter.
5956       (set-buffer gnus-group-buffer)
5957       (gnus-group-jump-to-group current-group)
5958       (setq target-group
5959             (or target-group
5960                 (if (eq gnus-keep-same-level 'best)
5961                     (gnus-summary-best-group gnus-newsgroup-name)
5962                   (gnus-summary-search-group backward gnus-keep-same-level))))
5963       (if (not target-group)
5964           ;; There are no further groups, so we return to the group
5965           ;; buffer.
5966           (progn
5967             (gnus-message 5 "Returning to the group buffer")
5968             (setq entered t)
5969             (when (gnus-buffer-live-p current-buffer)
5970               (set-buffer current-buffer)
5971               (gnus-summary-exit))
5972             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5973         ;; We try to enter the target group.
5974         (gnus-group-jump-to-group target-group)
5975         (let ((unreads (gnus-group-group-unread)))
5976           (if (and (or (eq t unreads)
5977                        (and unreads (not (zerop unreads))))
5978                    (gnus-summary-read-group
5979                     target-group nil no-article
5980                     (and (buffer-name current-buffer) current-buffer)
5981                     nil backward))
5982               (setq entered t)
5983             (setq current-group target-group
5984                   target-group nil)))))))
5985
5986 (defun gnus-summary-prev-group (&optional no-article)
5987   "Exit current newsgroup and then select previous unread newsgroup.
5988 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5989   (interactive "P")
5990   (gnus-summary-next-group no-article nil t))
5991
5992 ;; Walking around summary lines.
5993
5994 (defun gnus-summary-first-subject (&optional unread undownloaded)
5995   "Go to the first unread subject.
5996 If UNREAD is non-nil, go to the first unread article.
5997 Returns the article selected or nil if there are no unread articles."
5998   (interactive "P")
5999   (prog1
6000       (cond
6001        ;; Empty summary.
6002        ((null gnus-newsgroup-data)
6003         (gnus-message 3 "No articles in the group")
6004         nil)
6005        ;; Pick the first article.
6006        ((not unread)
6007         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6008         (gnus-data-number (car gnus-newsgroup-data)))
6009        ;; No unread articles.
6010        ((null gnus-newsgroup-unreads)
6011         (gnus-message 3 "No more unread articles")
6012         nil)
6013        ;; Find the first unread article.
6014        (t
6015         (let ((data gnus-newsgroup-data))
6016           (while (and data
6017                       (and (not (and undownloaded
6018                                      (eq gnus-undownloaded-mark
6019                                          (gnus-data-mark (car data)))))
6020                            (not (gnus-data-unread-p (car data)))))
6021             (setq data (cdr data)))
6022           (when data
6023             (goto-char (gnus-data-pos (car data)))
6024             (gnus-data-number (car data))))))
6025     (gnus-summary-position-point)))
6026
6027 (defun gnus-summary-next-subject (n &optional unread dont-display)
6028   "Go to next N'th summary line.
6029 If N is negative, go to the previous N'th subject line.
6030 If UNREAD is non-nil, only unread articles are selected.
6031 The difference between N and the actual number of steps taken is
6032 returned."
6033   (interactive "p")
6034   (let ((backward (< n 0))
6035         (n (abs n)))
6036     (while (and (> n 0)
6037                 (if backward
6038                     (gnus-summary-find-prev unread)
6039                   (gnus-summary-find-next unread)))
6040       (unless (zerop (setq n (1- n)))
6041         (gnus-summary-show-thread)))
6042     (when (/= 0 n)
6043       (gnus-message 7 "No more%s articles"
6044                     (if unread " unread" "")))
6045     (unless dont-display
6046       (gnus-summary-recenter)
6047       (gnus-summary-position-point))
6048     n))
6049
6050 (defun gnus-summary-next-unread-subject (n)
6051   "Go to next N'th unread summary line."
6052   (interactive "p")
6053   (gnus-summary-next-subject n t))
6054
6055 (defun gnus-summary-prev-subject (n &optional unread)
6056   "Go to previous N'th summary line.
6057 If optional argument UNREAD is non-nil, only unread article is selected."
6058   (interactive "p")
6059   (gnus-summary-next-subject (- n) unread))
6060
6061 (defun gnus-summary-prev-unread-subject (n)
6062   "Go to previous N'th unread summary line."
6063   (interactive "p")
6064   (gnus-summary-next-subject (- n) t))
6065
6066 (defun gnus-summary-goto-subject (article &optional force silent)
6067   "Go the subject line of ARTICLE.
6068 If FORCE, also allow jumping to articles not currently shown."
6069   (interactive "nArticle number: ")
6070   (let ((b (point))
6071         (data (gnus-data-find article)))
6072     ;; We read in the article if we have to.
6073     (and (not data)
6074          force
6075          (gnus-summary-insert-subject
6076           article
6077           (if (or (numberp force) (vectorp force)) force)
6078           t)
6079          (setq data (gnus-data-find article)))
6080     (goto-char b)
6081     (if (not data)
6082         (progn
6083           (unless silent
6084             (gnus-message 3 "Can't find article %d" article))
6085           nil)
6086       (goto-char (gnus-data-pos data))
6087       (gnus-summary-position-point)
6088       article)))
6089
6090 ;; Walking around summary lines with displaying articles.
6091
6092 (defun gnus-summary-expand-window (&optional arg)
6093   "Make the summary buffer take up the entire Emacs frame.
6094 Given a prefix, will force an `article' buffer configuration."
6095   (interactive "P")
6096   (if arg
6097       (gnus-configure-windows 'article 'force)
6098     (gnus-configure-windows 'summary 'force)))
6099
6100 (defun gnus-summary-display-article (article &optional all-header)
6101   "Display ARTICLE in article buffer."
6102   (when (gnus-buffer-live-p gnus-article-buffer)
6103     (with-current-buffer gnus-article-buffer
6104       (mm-enable-multibyte-mule4)))
6105   (gnus-set-global-variables)
6106   (when (gnus-buffer-live-p gnus-article-buffer)
6107     (with-current-buffer gnus-article-buffer
6108       (setq gnus-article-charset gnus-newsgroup-charset)
6109       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6110       (mm-enable-multibyte-mule4)))
6111   (if (null article)
6112       nil
6113     (prog1
6114         (if gnus-summary-display-article-function
6115             (funcall gnus-summary-display-article-function article all-header)
6116           (gnus-article-prepare article all-header))
6117       (gnus-run-hooks 'gnus-select-article-hook)
6118       (when (and gnus-current-article
6119                  (not (zerop gnus-current-article)))
6120         (gnus-summary-goto-subject gnus-current-article))
6121       (gnus-summary-recenter)
6122       (when (and gnus-use-trees gnus-show-threads)
6123         (gnus-possibly-generate-tree article)
6124         (gnus-highlight-selected-tree article))
6125       ;; Successfully display article.
6126       (gnus-article-set-window-start
6127        (cdr (assq article gnus-newsgroup-bookmarks))))))
6128
6129 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6130   "Select the current article.
6131 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6132 non-nil, the article will be re-fetched even if it already present in
6133 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6134 be displayed."
6135   ;; Make sure we are in the summary buffer to work around bbdb bug.
6136   (unless (eq major-mode 'gnus-summary-mode)
6137     (set-buffer gnus-summary-buffer))
6138   (let ((article (or article (gnus-summary-article-number)))
6139         (all-headers (not (not all-headers))) ;Must be T or NIL.
6140         gnus-summary-display-article-function)
6141     (and (not pseudo)
6142          (gnus-summary-article-pseudo-p article)
6143          (error "This is a pseudo-article"))
6144     (save-excursion
6145       (set-buffer gnus-summary-buffer)
6146       (if (or (and gnus-single-article-buffer
6147                    (or (null gnus-current-article)
6148                        (null gnus-article-current)
6149                        (null (get-buffer gnus-article-buffer))
6150                        (not (eq article (cdr gnus-article-current)))
6151                        (not (equal (car gnus-article-current)
6152                                    gnus-newsgroup-name))))
6153               (and (not gnus-single-article-buffer)
6154                    (or (null gnus-current-article)
6155                        (not (eq gnus-current-article article))))
6156               force)
6157           ;; The requested article is different from the current article.
6158           (progn
6159             (gnus-summary-display-article article all-headers)
6160             (when (gnus-buffer-live-p gnus-article-buffer)
6161                (with-current-buffer gnus-article-buffer
6162                 (if (not gnus-article-decoded-p) ;; a local variable
6163                     (mm-disable-multibyte-mule4))))
6164             (when (or all-headers gnus-show-all-headers)
6165               (gnus-article-show-all-headers))
6166             (gnus-article-set-window-start
6167              (cdr (assq article gnus-newsgroup-bookmarks)))
6168             article)
6169         (when (or all-headers gnus-show-all-headers)
6170           (gnus-article-show-all-headers))
6171         'old))))
6172
6173 (defun gnus-summary-force-verify-and-decrypt ()
6174   (interactive)
6175   (let ((mm-verify-option 'known)
6176         (mm-decrypt-option 'known))
6177     (gnus-summary-select-article nil 'force)))
6178
6179 (defun gnus-summary-set-current-mark (&optional current-mark)
6180   "Obsolete function."
6181   nil)
6182
6183 (defun gnus-summary-next-article (&optional unread subject backward push)
6184   "Select the next article.
6185 If UNREAD, only unread articles are selected.
6186 If SUBJECT, only articles with SUBJECT are selected.
6187 If BACKWARD, the previous article is selected instead of the next."
6188   (interactive "P")
6189   (cond
6190    ;; Is there such an article?
6191    ((and (gnus-summary-search-forward unread subject backward)
6192          (or (gnus-summary-display-article (gnus-summary-article-number))
6193              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6194     (gnus-summary-position-point))
6195    ;; If not, we try the first unread, if that is wanted.
6196    ((and subject
6197          gnus-auto-select-same
6198          (gnus-summary-first-unread-article))
6199     (gnus-summary-position-point)
6200     (gnus-message 6 "Wrapped"))
6201    ;; Try to get next/previous article not displayed in this group.
6202    ((and gnus-auto-extend-newsgroup
6203          (not unread) (not subject))
6204     (gnus-summary-goto-article
6205      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6206      nil (count-lines (point-min) (point))))
6207    ;; Go to next/previous group.
6208    (t
6209     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6210       (gnus-summary-jump-to-group gnus-newsgroup-name))
6211     (let ((cmd last-command-char)
6212           (point
6213            (save-excursion
6214              (set-buffer gnus-group-buffer)
6215              (point)))
6216           (group
6217            (if (eq gnus-keep-same-level 'best)
6218                (gnus-summary-best-group gnus-newsgroup-name)
6219              (gnus-summary-search-group backward gnus-keep-same-level))))
6220       ;; For some reason, the group window gets selected.  We change
6221       ;; it back.
6222       (select-window (get-buffer-window (current-buffer)))
6223       ;; Select next unread newsgroup automagically.
6224       (cond
6225        ((or (not gnus-auto-select-next)
6226             (not cmd))
6227         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6228        ((or (eq gnus-auto-select-next 'quietly)
6229             (and (eq gnus-auto-select-next 'slightly-quietly)
6230                  push)
6231             (and (eq gnus-auto-select-next 'almost-quietly)
6232                  (gnus-summary-last-article-p)))
6233         ;; Select quietly.
6234         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6235             (gnus-summary-exit)
6236           (gnus-message 7 "No more%s articles (%s)..."
6237                         (if unread " unread" "")
6238                         (if group (concat "selecting " group)
6239                           "exiting"))
6240           (gnus-summary-next-group nil group backward)))
6241        (t
6242         (when (gnus-key-press-event-p last-input-event)
6243           (gnus-summary-walk-group-buffer
6244            gnus-newsgroup-name cmd unread backward point))))))))
6245
6246 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6247   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6248                       (?\C-p (gnus-group-prev-unread-group 1))))
6249         (cursor-in-echo-area t)
6250         keve key group ended)
6251     (save-excursion
6252       (set-buffer gnus-group-buffer)
6253       (goto-char start)
6254       (setq group
6255             (if (eq gnus-keep-same-level 'best)
6256                 (gnus-summary-best-group gnus-newsgroup-name)
6257               (gnus-summary-search-group backward gnus-keep-same-level))))
6258     (while (not ended)
6259       (gnus-message
6260        5 "No more%s articles%s" (if unread " unread" "")
6261        (if (and group
6262                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6263            (format " (Type %s for %s [%s])"
6264                    (single-key-description cmd) group
6265                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6266          (format " (Type %s to exit %s)"
6267                  (single-key-description cmd)
6268                  gnus-newsgroup-name)))
6269       ;; Confirm auto selection.
6270       (setq key (car (setq keve (gnus-read-event-char))))
6271       (setq ended t)
6272       (cond
6273        ((assq key keystrokes)
6274         (let ((obuf (current-buffer)))
6275           (switch-to-buffer gnus-group-buffer)
6276           (when group
6277             (gnus-group-jump-to-group group))
6278           (eval (cadr (assq key keystrokes)))
6279           (setq group (gnus-group-group-name))
6280           (switch-to-buffer obuf))
6281         (setq ended nil))
6282        ((equal key cmd)
6283         (if (or (not group)
6284                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6285             (gnus-summary-exit)
6286           (gnus-summary-next-group nil group backward)))
6287        (t
6288         (push (cdr keve) unread-command-events))))))
6289
6290 (defun gnus-summary-next-unread-article ()
6291   "Select unread article after current one."
6292   (interactive)
6293   (gnus-summary-next-article
6294    (or (not (eq gnus-summary-goto-unread 'never))
6295        (gnus-summary-last-article-p (gnus-summary-article-number)))
6296    (and gnus-auto-select-same
6297         (gnus-summary-article-subject))))
6298
6299 (defun gnus-summary-prev-article (&optional unread subject)
6300   "Select the article after the current one.
6301 If UNREAD is non-nil, only unread articles are selected."
6302   (interactive "P")
6303   (gnus-summary-next-article unread subject t))
6304
6305 (defun gnus-summary-prev-unread-article ()
6306   "Select unread article before current one."
6307   (interactive)
6308   (gnus-summary-prev-article
6309    (or (not (eq gnus-summary-goto-unread 'never))
6310        (gnus-summary-first-article-p (gnus-summary-article-number)))
6311    (and gnus-auto-select-same
6312         (gnus-summary-article-subject))))
6313
6314 (defun gnus-summary-next-page (&optional lines circular)
6315   "Show next page of the selected article.
6316 If at the end of the current article, select the next article.
6317 LINES says how many lines should be scrolled up.
6318
6319 If CIRCULAR is non-nil, go to the start of the article instead of
6320 selecting the next article when reaching the end of the current
6321 article."
6322   (interactive "P")
6323   (setq gnus-summary-buffer (current-buffer))
6324   (gnus-set-global-variables)
6325   (let ((article (gnus-summary-article-number))
6326         (article-window (get-buffer-window gnus-article-buffer t))
6327         endp)
6328     ;; If the buffer is empty, we have no article.
6329     (unless article
6330       (error "No article to select"))
6331     (gnus-configure-windows 'article)
6332     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6333         (if (and (eq gnus-summary-goto-unread 'never)
6334                  (not (gnus-summary-last-article-p article)))
6335             (gnus-summary-next-article)
6336           (gnus-summary-next-unread-article))
6337       (if (or (null gnus-current-article)
6338               (null gnus-article-current)
6339               (/= article (cdr gnus-article-current))
6340               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6341           ;; Selected subject is different from current article's.
6342           (gnus-summary-display-article article)
6343         (when article-window
6344           (gnus-eval-in-buffer-window gnus-article-buffer
6345             (setq endp (gnus-article-next-page lines)))
6346           (when endp
6347             (cond (circular
6348                    (gnus-summary-beginning-of-article))
6349                   (lines
6350                    (gnus-message 3 "End of message"))
6351                   ((null lines)
6352                    (if (and (eq gnus-summary-goto-unread 'never)
6353                             (not (gnus-summary-last-article-p article)))
6354                        (gnus-summary-next-article)
6355                      (gnus-summary-next-unread-article))))))))
6356     (gnus-summary-recenter)
6357     (gnus-summary-position-point)))
6358
6359 (defun gnus-summary-prev-page (&optional lines move)
6360   "Show previous page of selected article.
6361 Argument LINES specifies lines to be scrolled down.
6362 If MOVE, move to the previous unread article if point is at
6363 the beginning of the buffer."
6364   (interactive "P")
6365   (let ((article (gnus-summary-article-number))
6366         (article-window (get-buffer-window gnus-article-buffer t))
6367         endp)
6368     (gnus-configure-windows 'article)
6369     (if (or (null gnus-current-article)
6370             (null gnus-article-current)
6371             (/= article (cdr gnus-article-current))
6372             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6373         ;; Selected subject is different from current article's.
6374         (gnus-summary-display-article article)
6375       (gnus-summary-recenter)
6376       (when article-window
6377         (gnus-eval-in-buffer-window gnus-article-buffer
6378           (setq endp (gnus-article-prev-page lines)))
6379         (when (and move endp)
6380           (cond (lines
6381                  (gnus-message 3 "Beginning of message"))
6382                 ((null lines)
6383                  (if (and (eq gnus-summary-goto-unread 'never)
6384                           (not (gnus-summary-first-article-p article)))
6385                      (gnus-summary-prev-article)
6386                    (gnus-summary-prev-unread-article))))))))
6387   (gnus-summary-position-point))
6388
6389 (defun gnus-summary-prev-page-or-article (&optional lines)
6390   "Show previous page of selected article.
6391 Argument LINES specifies lines to be scrolled down.
6392 If at the beginning of the article, go to the next article."
6393   (interactive "P")
6394   (gnus-summary-prev-page lines t))
6395
6396 (defun gnus-summary-scroll-up (lines)
6397   "Scroll up (or down) one line current article.
6398 Argument LINES specifies lines to be scrolled up (or down if negative)."
6399   (interactive "p")
6400   (gnus-configure-windows 'article)
6401   (gnus-summary-show-thread)
6402   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6403     (gnus-eval-in-buffer-window gnus-article-buffer
6404       (cond ((> lines 0)
6405              (when (gnus-article-next-page lines)
6406                (gnus-message 3 "End of message")))
6407             ((< lines 0)
6408              (gnus-article-prev-page (- lines))))))
6409   (gnus-summary-recenter)
6410   (gnus-summary-position-point))
6411
6412 (defun gnus-summary-scroll-down (lines)
6413   "Scroll down (or up) one line current article.
6414 Argument LINES specifies lines to be scrolled down (or up if negative)."
6415   (interactive "p")
6416   (gnus-summary-scroll-up (- lines)))
6417
6418 (defun gnus-summary-next-same-subject ()
6419   "Select next article which has the same subject as current one."
6420   (interactive)
6421   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6422
6423 (defun gnus-summary-prev-same-subject ()
6424   "Select previous article which has the same subject as current one."
6425   (interactive)
6426   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6427
6428 (defun gnus-summary-next-unread-same-subject ()
6429   "Select next unread article which has the same subject as current one."
6430   (interactive)
6431   (gnus-summary-next-article t (gnus-summary-article-subject)))
6432
6433 (defun gnus-summary-prev-unread-same-subject ()
6434   "Select previous unread article which has the same subject as current one."
6435   (interactive)
6436   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6437
6438 (defun gnus-summary-first-unread-article ()
6439   "Select the first unread article.
6440 Return nil if there are no unread articles."
6441   (interactive)
6442   (prog1
6443       (when (gnus-summary-first-subject t)
6444         (gnus-summary-show-thread)
6445         (gnus-summary-first-subject t)
6446         (gnus-summary-display-article (gnus-summary-article-number)))
6447     (gnus-summary-position-point)))
6448
6449 (defun gnus-summary-first-unread-subject ()
6450   "Place the point on the subject line of the first unread article.
6451 Return nil if there are no unread articles."
6452   (interactive)
6453   (prog1
6454       (when (gnus-summary-first-subject t)
6455         (gnus-summary-show-thread)
6456         (gnus-summary-first-subject t))
6457     (gnus-summary-position-point)))
6458
6459 (defun gnus-summary-first-article ()
6460   "Select the first article.
6461 Return nil if there are no articles."
6462   (interactive)
6463   (prog1
6464       (when (gnus-summary-first-subject)
6465         (gnus-summary-show-thread)
6466         (gnus-summary-first-subject)
6467         (gnus-summary-display-article (gnus-summary-article-number)))
6468     (gnus-summary-position-point)))
6469
6470 (defun gnus-summary-best-unread-article ()
6471   "Select the unread article with the highest score."
6472   (interactive)
6473   (let ((best -1000000)
6474         (data gnus-newsgroup-data)
6475         article score)
6476     (while data
6477       (and (gnus-data-unread-p (car data))
6478            (> (setq score
6479                     (gnus-summary-article-score (gnus-data-number (car data))))
6480               best)
6481            (setq best score
6482                  article (gnus-data-number (car data))))
6483       (setq data (cdr data)))
6484     (prog1
6485         (if article
6486             (gnus-summary-goto-article article)
6487           (error "No unread articles"))
6488       (gnus-summary-position-point))))
6489
6490 (defun gnus-summary-last-subject ()
6491   "Go to the last displayed subject line in the group."
6492   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6493     (when article
6494       (gnus-summary-goto-subject article))))
6495
6496 (defun gnus-summary-goto-article (article &optional all-headers force)
6497   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6498 If ALL-HEADERS is non-nil, no header lines are hidden.
6499 If FORCE, go to the article even if it isn't displayed.  If FORCE
6500 is a number, it is the line the article is to be displayed on."
6501   (interactive
6502    (list
6503     (completing-read
6504      "Article number or Message-ID: "
6505      (mapcar (lambda (number) (list (int-to-string number)))
6506              gnus-newsgroup-limit))
6507     current-prefix-arg
6508     t))
6509   (prog1
6510       (if (and (stringp article)
6511                (string-match "@" article))
6512           (gnus-summary-refer-article article)
6513         (when (stringp article)
6514           (setq article (string-to-number article)))
6515         (if (gnus-summary-goto-subject article force)
6516             (gnus-summary-display-article article all-headers)
6517           (gnus-message 4 "Couldn't go to article %s" article) nil))
6518     (gnus-summary-position-point)))
6519
6520 (defun gnus-summary-goto-last-article ()
6521   "Go to the previously read article."
6522   (interactive)
6523   (prog1
6524       (when gnus-last-article
6525         (gnus-summary-goto-article gnus-last-article nil t))
6526     (gnus-summary-position-point)))
6527
6528 (defun gnus-summary-pop-article (number)
6529   "Pop one article off the history and go to the previous.
6530 NUMBER articles will be popped off."
6531   (interactive "p")
6532   (let (to)
6533     (setq gnus-newsgroup-history
6534           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6535     (if to
6536         (gnus-summary-goto-article (car to) nil t)
6537       (error "Article history empty")))
6538   (gnus-summary-position-point))
6539
6540 ;; Summary commands and functions for limiting the summary buffer.
6541
6542 (defun gnus-summary-limit-to-articles (n)
6543   "Limit the summary buffer to the next N articles.
6544 If not given a prefix, use the process marked articles instead."
6545   (interactive "P")
6546   (prog1
6547       (let ((articles (gnus-summary-work-articles n)))
6548         (setq gnus-newsgroup-processable nil)
6549         (gnus-summary-limit articles))
6550     (gnus-summary-position-point)))
6551
6552 (defun gnus-summary-pop-limit (&optional total)
6553   "Restore the previous limit.
6554 If given a prefix, remove all limits."
6555   (interactive "P")
6556   (when total
6557     (setq gnus-newsgroup-limits
6558           (list (mapcar (lambda (h) (mail-header-number h))
6559                         gnus-newsgroup-headers))))
6560   (unless gnus-newsgroup-limits
6561     (error "No limit to pop"))
6562   (prog1
6563       (gnus-summary-limit nil 'pop)
6564     (gnus-summary-position-point)))
6565
6566 (defun gnus-summary-limit-to-subject (subject &optional header)
6567   "Limit the summary buffer to articles that have subjects that match a regexp."
6568   (interactive "sLimit to subject (regexp): ")
6569   (unless header
6570     (setq header "subject"))
6571   (when (not (equal "" subject))
6572     (prog1
6573         (let ((articles (gnus-summary-find-matching
6574                          (or header "subject") subject 'all)))
6575           (unless articles
6576             (error "Found no matches for \"%s\"" subject))
6577           (gnus-summary-limit articles))
6578       (gnus-summary-position-point))))
6579
6580 (defun gnus-summary-limit-to-author (from)
6581   "Limit the summary buffer to articles that have authors that match a regexp."
6582   (interactive "sLimit to author (regexp): ")
6583   (gnus-summary-limit-to-subject from "from"))
6584
6585 (defun gnus-summary-limit-to-age (age &optional younger-p)
6586   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6587 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6588 articles that are younger than AGE days."
6589   (interactive
6590    (let ((younger current-prefix-arg)
6591          (days-got nil)
6592          days)
6593      (while (not days-got)
6594        (setq days (if younger
6595                       (read-string "Limit to articles within (in days): ")
6596                     (read-string "Limit to articles older than (in days): ")))
6597        (when (> (length days) 0)
6598          (setq days (read days)))
6599        (if (numberp days)
6600            (setq days-got t)
6601          (message "Please enter a number.")
6602          (sleep-for 1)))
6603      (list days younger)))
6604   (prog1
6605       (let ((data gnus-newsgroup-data)
6606             (cutoff (days-to-time age))
6607             articles d date is-younger)
6608         (while (setq d (pop data))
6609           (when (and (vectorp (gnus-data-header d))
6610                      (setq date (mail-header-date (gnus-data-header d))))
6611             (setq is-younger (time-less-p
6612                               (time-since (condition-case ()
6613                                               (date-to-time date)
6614                                             (error '(0 0))))
6615                               cutoff))
6616             (when (if younger-p
6617                       is-younger
6618                     (not is-younger))
6619               (push (gnus-data-number d) articles))))
6620         (gnus-summary-limit (nreverse articles)))
6621     (gnus-summary-position-point)))
6622
6623 (defun gnus-summary-limit-to-extra (header regexp)
6624   "Limit the summary buffer to articles that match an 'extra' header."
6625   (interactive
6626    (let ((header
6627           (intern
6628            (gnus-completing-read
6629             (symbol-name (car gnus-extra-headers))
6630             "Limit extra header:"
6631             (mapcar (lambda (x)
6632                       (cons (symbol-name x) x))
6633                     gnus-extra-headers)
6634             nil
6635             t))))
6636      (list header
6637            (read-string (format "Limit to header %s (regexp): " header)))))
6638   (when (not (equal "" regexp))
6639     (prog1
6640         (let ((articles (gnus-summary-find-matching
6641                          (cons 'extra header) regexp 'all)))
6642           (unless articles
6643             (error "Found no matches for \"%s\"" regexp))
6644           (gnus-summary-limit articles))
6645       (gnus-summary-position-point))))
6646
6647 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6648 (make-obsolete
6649  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6650
6651 (defun gnus-summary-limit-to-unread (&optional all)
6652   "Limit the summary buffer to articles that are not marked as read.
6653 If ALL is non-nil, limit strictly to unread articles."
6654   (interactive "P")
6655   (if all
6656       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6657     (gnus-summary-limit-to-marks
6658      ;; Concat all the marks that say that an article is read and have
6659      ;; those removed.
6660      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6661            gnus-killed-mark gnus-kill-file-mark
6662            gnus-low-score-mark gnus-expirable-mark
6663            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6664            gnus-duplicate-mark gnus-souped-mark)
6665      'reverse)))
6666
6667 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6668 (make-obsolete 'gnus-summary-delete-marked-with
6669                'gnus-summary-limit-exlude-marks)
6670
6671 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6672   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6673 If REVERSE, limit the summary buffer to articles that are marked
6674 with MARKS.  MARKS can either be a string of marks or a list of marks.
6675 Returns how many articles were removed."
6676   (interactive "sMarks: ")
6677   (gnus-summary-limit-to-marks marks t))
6678
6679 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6680   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6681 If REVERSE (the prefix), limit the summary buffer to articles that are
6682 not marked with MARKS.  MARKS can either be a string of marks or a
6683 list of marks.
6684 Returns how many articles were removed."
6685   (interactive "sMarks: \nP")
6686   (prog1
6687       (let ((data gnus-newsgroup-data)
6688             (marks (if (listp marks) marks
6689                      (append marks nil))) ; Transform to list.
6690             articles)
6691         (while data
6692           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6693                   (memq (gnus-data-mark (car data)) marks))
6694             (push (gnus-data-number (car data)) articles))
6695           (setq data (cdr data)))
6696         (gnus-summary-limit articles))
6697     (gnus-summary-position-point)))
6698
6699 (defun gnus-summary-limit-to-score (&optional score)
6700   "Limit to articles with score at or above SCORE."
6701   (interactive "P")
6702   (setq score (if score
6703                   (prefix-numeric-value score)
6704                 (or gnus-summary-default-score 0)))
6705   (let ((data gnus-newsgroup-data)
6706         articles)
6707     (while data
6708       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6709                 score)
6710         (push (gnus-data-number (car data)) articles))
6711       (setq data (cdr data)))
6712     (prog1
6713         (gnus-summary-limit articles)
6714       (gnus-summary-position-point))))
6715
6716 (defun gnus-summary-limit-include-thread (id)
6717   "Display all the hidden articles that in the current thread."
6718   (interactive (list (mail-header-id (gnus-summary-article-header))))
6719   (let ((articles (gnus-articles-in-thread
6720                    (gnus-id-to-thread (gnus-root-id id)))))
6721     (prog1
6722         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6723       (gnus-summary-position-point))))
6724
6725 (defun gnus-summary-limit-include-dormant ()
6726   "Display all the hidden articles that are marked as dormant.
6727 Note that this command only works on a subset of the articles currently
6728 fetched for this group."
6729   (interactive)
6730   (unless gnus-newsgroup-dormant
6731     (error "There are no dormant articles in this group"))
6732   (prog1
6733       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6734     (gnus-summary-position-point)))
6735
6736 (defun gnus-summary-limit-exclude-dormant ()
6737   "Hide all dormant articles."
6738   (interactive)
6739   (prog1
6740       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6741     (gnus-summary-position-point)))
6742
6743 (defun gnus-summary-limit-exclude-childless-dormant ()
6744   "Hide all dormant articles that have no children."
6745   (interactive)
6746   (let ((data (gnus-data-list t))
6747         articles d children)
6748     ;; Find all articles that are either not dormant or have
6749     ;; children.
6750     (while (setq d (pop data))
6751       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6752                 (and (setq children
6753                            (gnus-article-children (gnus-data-number d)))
6754                      (let (found)
6755                        (while children
6756                          (when (memq (car children) articles)
6757                            (setq children nil
6758                                  found t))
6759                          (pop children))
6760                        found)))
6761         (push (gnus-data-number d) articles)))
6762     ;; Do the limiting.
6763     (prog1
6764         (gnus-summary-limit articles)
6765       (gnus-summary-position-point))))
6766
6767 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6768   "Mark all unread excluded articles as read.
6769 If ALL, mark even excluded ticked and dormants as read."
6770   (interactive "P")
6771   (let ((articles (gnus-sorted-complement
6772                    (sort
6773                     (mapcar (lambda (h) (mail-header-number h))
6774                             gnus-newsgroup-headers)
6775                     '<)
6776                    (sort gnus-newsgroup-limit '<)))
6777         article)
6778     (setq gnus-newsgroup-unreads
6779           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6780     (if all
6781         (setq gnus-newsgroup-dormant nil
6782               gnus-newsgroup-marked nil
6783               gnus-newsgroup-reads
6784               (nconc
6785                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6786                gnus-newsgroup-reads))
6787       (while (setq article (pop articles))
6788         (unless (or (memq article gnus-newsgroup-dormant)
6789                     (memq article gnus-newsgroup-marked))
6790           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6791
6792 (defun gnus-summary-limit (articles &optional pop)
6793   (if pop
6794       ;; We pop the previous limit off the stack and use that.
6795       (setq articles (car gnus-newsgroup-limits)
6796             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6797     ;; We use the new limit, so we push the old limit on the stack.
6798     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6799   ;; Set the limit.
6800   (setq gnus-newsgroup-limit articles)
6801   (let ((total (length gnus-newsgroup-data))
6802         (data (gnus-data-find-list (gnus-summary-article-number)))
6803         (gnus-summary-mark-below nil)   ; Inhibit this.
6804         found)
6805     ;; This will do all the work of generating the new summary buffer
6806     ;; according to the new limit.
6807     (gnus-summary-prepare)
6808     ;; Hide any threads, possibly.
6809     (and gnus-show-threads
6810          gnus-thread-hide-subtree
6811          (gnus-summary-hide-all-threads))
6812     ;; Try to return to the article you were at, or one in the
6813     ;; neighborhood.
6814     (when data
6815       ;; We try to find some article after the current one.
6816       (while data
6817         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6818           (setq data nil
6819                 found t))
6820         (setq data (cdr data))))
6821     (unless found
6822       ;; If there is no data, that means that we were after the last
6823       ;; article.  The same goes when we can't find any articles
6824       ;; after the current one.
6825       (goto-char (point-max))
6826       (gnus-summary-find-prev))
6827     (gnus-set-mode-line 'summary)
6828     ;; We return how many articles were removed from the summary
6829     ;; buffer as a result of the new limit.
6830     (- total (length gnus-newsgroup-data))))
6831
6832 (defsubst gnus-invisible-cut-children (threads)
6833   (let ((num 0))
6834     (while threads
6835       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6836         (incf num))
6837       (pop threads))
6838     (< num 2)))
6839
6840 (defsubst gnus-cut-thread (thread)
6841   "Go forwards in the thread until we find an article that we want to display."
6842   (when (or (eq gnus-fetch-old-headers 'some)
6843             (eq gnus-fetch-old-headers 'invisible)
6844             (numberp gnus-fetch-old-headers)
6845             (eq gnus-build-sparse-threads 'some)
6846             (eq gnus-build-sparse-threads 'more))
6847     ;; Deal with old-fetched headers and sparse threads.
6848     (while (and
6849             thread
6850             (or
6851              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6852              (gnus-summary-article-ancient-p
6853               (mail-header-number (car thread))))
6854             (if (or (<= (length (cdr thread)) 1)
6855                     (eq gnus-fetch-old-headers 'invisible))
6856                 (setq gnus-newsgroup-limit
6857                       (delq (mail-header-number (car thread))
6858                             gnus-newsgroup-limit)
6859                       thread (cadr thread))
6860               (when (gnus-invisible-cut-children (cdr thread))
6861                 (let ((th (cdr thread)))
6862                   (while th
6863                     (if (memq (mail-header-number (caar th))
6864                               gnus-newsgroup-limit)
6865                         (setq thread (car th)
6866                               th nil)
6867                       (setq th (cdr th))))))))))
6868   thread)
6869
6870 (defun gnus-cut-threads (threads)
6871   "Cut off all uninteresting articles from the beginning of threads."
6872   (when (or (eq gnus-fetch-old-headers 'some)
6873             (eq gnus-fetch-old-headers 'invisible)
6874             (numberp gnus-fetch-old-headers)
6875             (eq gnus-build-sparse-threads 'some)
6876             (eq gnus-build-sparse-threads 'more))
6877     (let ((th threads))
6878       (while th
6879         (setcar th (gnus-cut-thread (car th)))
6880         (setq th (cdr th)))))
6881   ;; Remove nixed out threads.
6882   (delq nil threads))
6883
6884 (defun gnus-summary-initial-limit (&optional show-if-empty)
6885   "Figure out what the initial limit is supposed to be on group entry.
6886 This entails weeding out unwanted dormants, low-scored articles,
6887 fetch-old-headers verbiage, and so on."
6888   ;; Most groups have nothing to remove.
6889   (if (or gnus-inhibit-limiting
6890           (and (null gnus-newsgroup-dormant)
6891                (not (eq gnus-fetch-old-headers 'some))
6892                (not (numberp gnus-fetch-old-headers))
6893                (not (eq gnus-fetch-old-headers 'invisible))
6894                (null gnus-summary-expunge-below)
6895                (not (eq gnus-build-sparse-threads 'some))
6896                (not (eq gnus-build-sparse-threads 'more))
6897                (null gnus-thread-expunge-below)
6898                (not gnus-use-nocem)))
6899       ()                                ; Do nothing.
6900     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6901     (setq gnus-newsgroup-limit nil)
6902     (mapatoms
6903      (lambda (node)
6904        (unless (car (symbol-value node))
6905          ;; These threads have no parents -- they are roots.
6906          (let ((nodes (cdr (symbol-value node)))
6907                thread)
6908            (while nodes
6909              (if (and gnus-thread-expunge-below
6910                       (< (gnus-thread-total-score (car nodes))
6911                          gnus-thread-expunge-below))
6912                  (gnus-expunge-thread (pop nodes))
6913                (setq thread (pop nodes))
6914                (gnus-summary-limit-children thread))))))
6915      gnus-newsgroup-dependencies)
6916     ;; If this limitation resulted in an empty group, we might
6917     ;; pop the previous limit and use it instead.
6918     (when (and (not gnus-newsgroup-limit)
6919                show-if-empty)
6920       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6921     gnus-newsgroup-limit))
6922
6923 (defun gnus-summary-limit-children (thread)
6924   "Return 1 if this subthread is visible and 0 if it is not."
6925   ;; First we get the number of visible children to this thread.  This
6926   ;; is done by recursing down the thread using this function, so this
6927   ;; will really go down to a leaf article first, before slowly
6928   ;; working its way up towards the root.
6929   (when thread
6930     (let ((children
6931            (if (cdr thread)
6932                (apply '+ (mapcar 'gnus-summary-limit-children
6933                                  (cdr thread)))
6934              0))
6935           (number (mail-header-number (car thread)))
6936           score)
6937       (if (and
6938            (not (memq number gnus-newsgroup-marked))
6939            (or
6940             ;; If this article is dormant and has absolutely no visible
6941             ;; children, then this article isn't visible.
6942             (and (memq number gnus-newsgroup-dormant)
6943                  (zerop children))
6944             ;; If this is "fetch-old-headered" and there is no
6945             ;; visible children, then we don't want this article.
6946             (and (or (eq gnus-fetch-old-headers 'some)
6947                      (numberp gnus-fetch-old-headers))
6948                  (gnus-summary-article-ancient-p number)
6949                  (zerop children))
6950             ;; If this is "fetch-old-headered" and `invisible', then
6951             ;; we don't want this article.
6952             (and (eq gnus-fetch-old-headers 'invisible)
6953                  (gnus-summary-article-ancient-p number))
6954             ;; If this is a sparsely inserted article with no children,
6955             ;; we don't want it.
6956             (and (eq gnus-build-sparse-threads 'some)
6957                  (gnus-summary-article-sparse-p number)
6958                  (zerop children))
6959             ;; If we use expunging, and this article is really
6960             ;; low-scored, then we don't want this article.
6961             (when (and gnus-summary-expunge-below
6962                        (< (setq score
6963                                 (or (cdr (assq number gnus-newsgroup-scored))
6964                                     gnus-summary-default-score))
6965                           gnus-summary-expunge-below))
6966               ;; We increase the expunge-tally here, but that has
6967               ;; nothing to do with the limits, really.
6968               (incf gnus-newsgroup-expunged-tally)
6969               ;; We also mark as read here, if that's wanted.
6970               (when (and gnus-summary-mark-below
6971                          (< score gnus-summary-mark-below))
6972                 (setq gnus-newsgroup-unreads
6973                       (delq number gnus-newsgroup-unreads))
6974                 (if gnus-newsgroup-auto-expire
6975                     (push number gnus-newsgroup-expirable)
6976                   (push (cons number gnus-low-score-mark)
6977                         gnus-newsgroup-reads)))
6978               t)
6979             ;; Check NoCeM things.
6980             (if (and gnus-use-nocem
6981                      (gnus-nocem-unwanted-article-p
6982                       (mail-header-id (car thread))))
6983                 (progn
6984                   (setq gnus-newsgroup-unreads
6985                         (delq number gnus-newsgroup-unreads))
6986                   t))))
6987           ;; Nope, invisible article.
6988           0
6989         ;; Ok, this article is to be visible, so we add it to the limit
6990         ;; and return 1.
6991         (push number gnus-newsgroup-limit)
6992         1))))
6993
6994 (defun gnus-expunge-thread (thread)
6995   "Mark all articles in THREAD as read."
6996   (let* ((number (mail-header-number (car thread))))
6997     (incf gnus-newsgroup-expunged-tally)
6998     ;; We also mark as read here, if that's wanted.
6999     (setq gnus-newsgroup-unreads
7000           (delq number gnus-newsgroup-unreads))
7001     (if gnus-newsgroup-auto-expire
7002         (push number gnus-newsgroup-expirable)
7003       (push (cons number gnus-low-score-mark)
7004             gnus-newsgroup-reads)))
7005   ;; Go recursively through all subthreads.
7006   (mapcar 'gnus-expunge-thread (cdr thread)))
7007
7008 ;; Summary article oriented commands
7009
7010 (defun gnus-summary-refer-parent-article (n)
7011   "Refer parent article N times.
7012 If N is negative, go to ancestor -N instead.
7013 The difference between N and the number of articles fetched is returned."
7014   (interactive "p")
7015   (let ((skip 1)
7016         error header ref)
7017     (when (not (natnump n))
7018       (setq skip (abs n)
7019             n 1))
7020     (while (and (> n 0)
7021                 (not error))
7022       (setq header (gnus-summary-article-header))
7023       (if (and (eq (mail-header-number header)
7024                    (cdr gnus-article-current))
7025                (equal gnus-newsgroup-name
7026                       (car gnus-article-current)))
7027           ;; If we try to find the parent of the currently
7028           ;; displayed article, then we take a look at the actual
7029           ;; References header, since this is slightly more
7030           ;; reliable than the References field we got from the
7031           ;; server.
7032           (save-excursion
7033             (set-buffer gnus-original-article-buffer)
7034             (nnheader-narrow-to-headers)
7035             (unless (setq ref (message-fetch-field "references"))
7036               (setq ref (message-fetch-field "in-reply-to")))
7037             (widen))
7038         (setq ref
7039               ;; It's not the current article, so we take a bet on
7040               ;; the value we got from the server.
7041               (mail-header-references header)))
7042       (if (and ref
7043                (not (equal ref "")))
7044           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7045             (gnus-message 1 "Couldn't find parent"))
7046         (gnus-message 1 "No references in article %d"
7047                       (gnus-summary-article-number))
7048         (setq error t))
7049       (decf n))
7050     (gnus-summary-position-point)
7051     n))
7052
7053 (defun gnus-summary-refer-references ()
7054   "Fetch all articles mentioned in the References header.
7055 Return the number of articles fetched."
7056   (interactive)
7057   (let ((ref (mail-header-references (gnus-summary-article-header)))
7058         (current (gnus-summary-article-number))
7059         (n 0))
7060     (if (or (not ref)
7061             (equal ref ""))
7062         (error "No References in the current article")
7063       ;; For each Message-ID in the References header...
7064       (while (string-match "<[^>]*>" ref)
7065         (incf n)
7066         ;; ... fetch that article.
7067         (gnus-summary-refer-article
7068          (prog1 (match-string 0 ref)
7069            (setq ref (substring ref (match-end 0))))))
7070       (gnus-summary-goto-subject current)
7071       (gnus-summary-position-point)
7072       n)))
7073
7074 (defun gnus-summary-refer-thread (&optional limit)
7075   "Fetch all articles in the current thread.
7076 If LIMIT (the numerical prefix), fetch that many old headers instead
7077 of what's specified by the `gnus-refer-thread-limit' variable."
7078   (interactive "P")
7079   (let ((id (mail-header-id (gnus-summary-article-header)))
7080         (limit (if limit (prefix-numeric-value limit)
7081                  gnus-refer-thread-limit)))
7082     ;; We want to fetch LIMIT *old* headers, but we also have to
7083     ;; re-fetch all the headers in the current buffer, because many of
7084     ;; them may be undisplayed.  So we adjust LIMIT.
7085     (when (numberp limit)
7086       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7087     (unless (eq gnus-fetch-old-headers 'invisible)
7088       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7089       ;; Retrieve the headers and read them in.
7090       (if (eq (gnus-retrieve-headers
7091                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7092               'nov)
7093           (gnus-build-all-threads)
7094         (error "Can't fetch thread from backends that don't support NOV"))
7095       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7096     (gnus-summary-limit-include-thread id)))
7097
7098 (defun gnus-summary-refer-article (message-id)
7099   "Fetch an article specified by MESSAGE-ID."
7100   (interactive "sMessage-ID: ")
7101   (when (and (stringp message-id)
7102              (not (zerop (length message-id))))
7103     ;; Construct the correct Message-ID if necessary.
7104     ;; Suggested by tale@pawl.rpi.edu.
7105     (unless (string-match "^<" message-id)
7106       (setq message-id (concat "<" message-id)))
7107     (unless (string-match ">$" message-id)
7108       (setq message-id (concat message-id ">")))
7109     (let* ((header (gnus-id-to-header message-id))
7110            (sparse (and header
7111                         (gnus-summary-article-sparse-p
7112                          (mail-header-number header))
7113                         (memq (mail-header-number header)
7114                               gnus-newsgroup-limit)))
7115            number)
7116       (cond
7117        ;; If the article is present in the buffer we just go to it.
7118        ((and header
7119              (or (not (gnus-summary-article-sparse-p
7120                        (mail-header-number header)))
7121                  sparse))
7122         (prog1
7123             (gnus-summary-goto-article
7124              (mail-header-number header) nil t)
7125           (when sparse
7126             (gnus-summary-update-article (mail-header-number header)))))
7127        (t
7128         ;; We fetch the article.
7129         (catch 'found
7130           (dolist (gnus-override-method (gnus-refer-article-methods))
7131             (gnus-check-server gnus-override-method)
7132             ;; Fetch the header, and display the article.
7133             (when (setq number (gnus-summary-insert-subject message-id))
7134               (gnus-summary-select-article nil nil nil number)
7135               (throw 'found t)))
7136           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7137
7138 (defun gnus-refer-article-methods ()
7139   "Return a list of referrable methods."
7140   (cond
7141    ;; No method, so we default to current and native.
7142    ((null gnus-refer-article-method)
7143     (list gnus-current-select-method gnus-select-method))
7144    ;; Current.
7145    ((eq 'current gnus-refer-article-method)
7146     (list gnus-current-select-method))
7147    ;; List of select methods.
7148    ((not (and (symbolp (car gnus-refer-article-method))
7149               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7150     (let (out)
7151       (dolist (method gnus-refer-article-method)
7152         (push (if (eq 'current method)
7153                   gnus-current-select-method
7154                 method)
7155               out))
7156       (nreverse out)))
7157    ;; One single select method.
7158    (t
7159     (list gnus-refer-article-method))))
7160
7161 (defun gnus-summary-edit-parameters ()
7162   "Edit the group parameters of the current group."
7163   (interactive)
7164   (gnus-group-edit-group gnus-newsgroup-name 'params))
7165
7166 (defun gnus-summary-customize-parameters ()
7167   "Customize the group parameters of the current group."
7168   (interactive)
7169   (gnus-group-customize gnus-newsgroup-name))
7170
7171 (defun gnus-summary-enter-digest-group (&optional force)
7172   "Enter an nndoc group based on the current article.
7173 If FORCE, force a digest interpretation.  If not, try
7174 to guess what the document format is."
7175   (interactive "P")
7176   (let ((conf gnus-current-window-configuration))
7177     (save-excursion
7178       (gnus-summary-select-article))
7179     (setq gnus-current-window-configuration conf)
7180     (let* ((name (format "%s-%d"
7181                          (gnus-group-prefixed-name
7182                           gnus-newsgroup-name (list 'nndoc ""))
7183                          (save-excursion
7184                            (set-buffer gnus-summary-buffer)
7185                            gnus-current-article)))
7186            (ogroup gnus-newsgroup-name)
7187            (params (append (gnus-info-params (gnus-get-info ogroup))
7188                            (list (cons 'to-group ogroup))
7189                            (list (cons 'save-article-group ogroup))))
7190            (case-fold-search t)
7191            (buf (current-buffer))
7192            dig to-address)
7193       (save-excursion
7194         (set-buffer gnus-original-article-buffer)
7195         ;; Have the digest group inherit the main mail address of
7196         ;; the parent article.
7197         (when (setq to-address (or (message-fetch-field "reply-to")
7198                                    (message-fetch-field "from")))
7199           (setq params (append
7200                         (list (cons 'to-address
7201                                     (funcall gnus-decode-encoded-word-function
7202                                              to-address))))))
7203         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7204         (insert-buffer-substring gnus-original-article-buffer)
7205         ;; Remove lines that may lead nndoc to misinterpret the
7206         ;; document type.
7207         (narrow-to-region
7208          (goto-char (point-min))
7209          (or (search-forward "\n\n" nil t) (point)))
7210         (goto-char (point-min))
7211         (delete-matching-lines "^Path:\\|^From ")
7212         (widen))
7213       (unwind-protect
7214           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7215                     (gnus-newsgroup-ephemeral-ignored-charsets
7216                      gnus-newsgroup-ignored-charsets))
7217                 (gnus-group-read-ephemeral-group
7218                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7219                               (nndoc-article-type
7220                                ,(if force 'mbox 'guess))) t))
7221               ;; Make all postings to this group go to the parent group.
7222               (nconc (gnus-info-params (gnus-get-info name))
7223                      params)
7224             ;; Couldn't select this doc group.
7225             (switch-to-buffer buf)
7226             (gnus-set-global-variables)
7227             (gnus-configure-windows 'summary)
7228             (gnus-message 3 "Article couldn't be entered?"))
7229         (kill-buffer dig)))))
7230
7231 (defun gnus-summary-read-document (n)
7232   "Open a new group based on the current article(s).
7233 This will allow you to read digests and other similar
7234 documents as newsgroups.
7235 Obeys the standard process/prefix convention."
7236   (interactive "P")
7237   (let* ((articles (gnus-summary-work-articles n))
7238          (ogroup gnus-newsgroup-name)
7239          (params (append (gnus-info-params (gnus-get-info ogroup))
7240                          (list (cons 'to-group ogroup))))
7241          article group egroup groups vgroup)
7242     (while (setq article (pop articles))
7243       (setq group (format "%s-%d" gnus-newsgroup-name article))
7244       (gnus-summary-remove-process-mark article)
7245       (when (gnus-summary-display-article article)
7246         (save-excursion
7247           (with-temp-buffer
7248             (insert-buffer-substring gnus-original-article-buffer)
7249             ;; Remove some headers that may lead nndoc to make
7250             ;; the wrong guess.
7251             (message-narrow-to-head)
7252             (goto-char (point-min))
7253             (delete-matching-lines "^\\(Path\\):\\|^From ")
7254             (widen)
7255             (if (setq egroup
7256                       (gnus-group-read-ephemeral-group
7257                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7258                                      (nndoc-article-type guess))
7259                        t nil t))
7260                 (progn
7261                   ;; Make all postings to this group go to the parent group.
7262                   (nconc (gnus-info-params (gnus-get-info egroup))
7263                          params)
7264                   (push egroup groups))
7265               ;; Couldn't select this doc group.
7266               (gnus-error 3 "Article couldn't be entered"))))))
7267     ;; Now we have selected all the documents.
7268     (cond
7269      ((not groups)
7270       (error "None of the articles could be interpreted as documents"))
7271      ((gnus-group-read-ephemeral-group
7272        (setq vgroup (format
7273                      "nnvirtual:%s-%s" gnus-newsgroup-name
7274                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7275        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7276        t
7277        (cons (current-buffer) 'summary)))
7278      (t
7279       (error "Couldn't select virtual nndoc group")))))
7280
7281 (defun gnus-summary-isearch-article (&optional regexp-p)
7282   "Do incremental search forward on the current article.
7283 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7284   (interactive "P")
7285   (gnus-summary-select-article)
7286   (gnus-configure-windows 'article)
7287   (gnus-eval-in-buffer-window gnus-article-buffer
7288     (save-restriction
7289       (widen)
7290       (isearch-forward regexp-p))))
7291
7292 (defun gnus-summary-search-article-forward (regexp &optional backward)
7293   "Search for an article containing REGEXP forward.
7294 If BACKWARD, search backward instead."
7295   (interactive
7296    (list (read-string
7297           (format "Search article %s (regexp%s): "
7298                   (if current-prefix-arg "backward" "forward")
7299                   (if gnus-last-search-regexp
7300                       (concat ", default " gnus-last-search-regexp)
7301                     "")))
7302          current-prefix-arg))
7303   (if (string-equal regexp "")
7304       (setq regexp (or gnus-last-search-regexp ""))
7305     (setq gnus-last-search-regexp regexp)
7306     (setq gnus-article-before-search gnus-current-article))
7307   ;; Intentionally set gnus-last-article.
7308   (setq gnus-last-article gnus-article-before-search)
7309   (let ((gnus-last-article gnus-last-article))
7310     (if (gnus-summary-search-article regexp backward)
7311         (gnus-summary-show-thread)
7312       (error "Search failed: \"%s\"" regexp))))
7313
7314 (defun gnus-summary-search-article-backward (regexp)
7315   "Search for an article containing REGEXP backward."
7316   (interactive
7317    (list (read-string
7318           (format "Search article backward (regexp%s): "
7319                   (if gnus-last-search-regexp
7320                       (concat ", default " gnus-last-search-regexp)
7321                     "")))))
7322   (gnus-summary-search-article-forward regexp 'backward))
7323
7324 (defun gnus-summary-search-article (regexp &optional backward)
7325   "Search for an article containing REGEXP.
7326 Optional argument BACKWARD means do search for backward.
7327 `gnus-select-article-hook' is not called during the search."
7328   ;; We have to require this here to make sure that the following
7329   ;; dynamic binding isn't shadowed by autoloading.
7330   (require 'gnus-async)
7331   (require 'gnus-art)
7332   (let ((gnus-select-article-hook nil)  ;Disable hook.
7333         (gnus-article-prepare-hook nil)
7334         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7335         (gnus-use-article-prefetch nil)
7336         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7337         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7338         (sum (current-buffer))
7339         (gnus-display-mime-function nil)
7340         (found nil)
7341         point)
7342     (gnus-save-hidden-threads
7343       (gnus-summary-select-article)
7344       (set-buffer gnus-article-buffer)
7345       (goto-char (window-point (get-buffer-window (current-buffer))))
7346       (when backward
7347         (forward-line -1))
7348       (while (not found)
7349         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7350         (if (if backward
7351                 (re-search-backward regexp nil t)
7352               (re-search-forward regexp nil t))
7353             ;; We found the regexp.
7354             (progn
7355               (setq found 'found)
7356               (beginning-of-line)
7357               (set-window-start
7358                (get-buffer-window (current-buffer))
7359                (point))
7360               (forward-line 1)
7361               (set-window-point
7362                (get-buffer-window (current-buffer))
7363                (point))
7364               (set-buffer sum)
7365               (setq point (point)))
7366           ;; We didn't find it, so we go to the next article.
7367           (set-buffer sum)
7368           (setq found 'not)
7369           (while (eq found 'not)
7370             (if (not (if backward (gnus-summary-find-prev)
7371                        (gnus-summary-find-next)))
7372                 ;; No more articles.
7373                 (setq found t)
7374               ;; Select the next article and adjust point.
7375               (unless (gnus-summary-article-sparse-p
7376                        (gnus-summary-article-number))
7377                 (setq found nil)
7378                 (gnus-summary-select-article)
7379                 (set-buffer gnus-article-buffer)
7380                 (widen)
7381                 (goto-char (if backward (point-max) (point-min))))))))
7382       (gnus-message 7 ""))
7383     ;; Return whether we found the regexp.
7384     (when (eq found 'found)
7385       (goto-char point)
7386       (gnus-summary-show-thread)
7387       (gnus-summary-goto-subject gnus-current-article)
7388       (gnus-summary-position-point)
7389       t)))
7390
7391 (defun gnus-summary-find-matching (header regexp &optional backward unread
7392                                           not-case-fold)
7393   "Return a list of all articles that match REGEXP on HEADER.
7394 The search stars on the current article and goes forwards unless
7395 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7396 If UNREAD is non-nil, only unread articles will
7397 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7398 in the comparisons."
7399   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7400                 (gnus-data-find-list
7401                  (gnus-summary-article-number) (gnus-data-list backward))))
7402         (case-fold-search (not not-case-fold))
7403         articles d func)
7404     (if (consp header)
7405         (if (eq (car header) 'extra)
7406             (setq func
7407                   `(lambda (h)
7408                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7409                          "")))
7410           (error "%s is an invalid header" header))
7411       (unless (fboundp (intern (concat "mail-header-" header)))
7412         (error "%s is not a valid header" header))
7413       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7414     (while data
7415       (setq d (car data))
7416       (and (or (not unread)             ; We want all articles...
7417                (gnus-data-unread-p d))  ; Or just unreads.
7418            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7419            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7420            (push (gnus-data-number d) articles)) ; Success!
7421       (setq data (cdr data)))
7422     (nreverse articles)))
7423
7424 (defun gnus-summary-execute-command (header regexp command &optional backward)
7425   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7426 If HEADER is an empty string (or nil), the match is done on the entire
7427 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7428   (interactive
7429    (list (let ((completion-ignore-case t))
7430            (completing-read
7431             "Header name: "
7432             (mapcar (lambda (string) (list string))
7433                     '("Number" "Subject" "From" "Lines" "Date"
7434                       "Message-ID" "Xref" "References" "Body"))
7435             nil 'require-match))
7436          (read-string "Regexp: ")
7437          (read-key-sequence "Command: ")
7438          current-prefix-arg))
7439   (when (equal header "Body")
7440     (setq header ""))
7441   ;; Hidden thread subtrees must be searched as well.
7442   (gnus-summary-show-all-threads)
7443   ;; We don't want to change current point nor window configuration.
7444   (save-excursion
7445     (save-window-excursion
7446       (gnus-message 6 "Executing %s..." (key-description command))
7447       ;; We'd like to execute COMMAND interactively so as to give arguments.
7448       (gnus-execute header regexp
7449                     `(call-interactively ',(key-binding command))
7450                     backward)
7451       (gnus-message 6 "Executing %s...done" (key-description command)))))
7452
7453 (defun gnus-summary-beginning-of-article ()
7454   "Scroll the article back to the beginning."
7455   (interactive)
7456   (gnus-summary-select-article)
7457   (gnus-configure-windows 'article)
7458   (gnus-eval-in-buffer-window gnus-article-buffer
7459     (widen)
7460     (goto-char (point-min))
7461     (when gnus-page-broken
7462       (gnus-narrow-to-page))))
7463
7464 (defun gnus-summary-end-of-article ()
7465   "Scroll to the end of the article."
7466   (interactive)
7467   (gnus-summary-select-article)
7468   (gnus-configure-windows 'article)
7469   (gnus-eval-in-buffer-window gnus-article-buffer
7470     (widen)
7471     (goto-char (point-max))
7472     (recenter -3)
7473     (when gnus-page-broken
7474       (gnus-narrow-to-page))))
7475
7476 (defun gnus-summary-print-article (&optional filename n)
7477   "Generate and print a PostScript image of the N next (mail) articles.
7478
7479 If N is negative, print the N previous articles.  If N is nil and articles
7480 have been marked with the process mark, print these instead.
7481
7482 If the optional first argument FILENAME is nil, send the image to the
7483 printer.  If FILENAME is a string, save the PostScript image in a file with
7484 that name.  If FILENAME is a number, prompt the user for the name of the file
7485 to save in."
7486   (interactive (list (ps-print-preprint current-prefix-arg)))
7487   (dolist (article (gnus-summary-work-articles n))
7488     (gnus-summary-select-article nil nil 'pseudo article)
7489     (gnus-eval-in-buffer-window gnus-article-buffer
7490       (let ((buffer (generate-new-buffer " *print*")))
7491         (unwind-protect
7492             (progn
7493               (copy-to-buffer buffer (point-min) (point-max))
7494               (set-buffer buffer)
7495               (gnus-article-delete-invisible-text)
7496               (when (gnus-visual-p 'article-highlight 'highlight)
7497                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7498                 ;; highlight.
7499                 (let ((gnus-article-buffer buffer))
7500                   (gnus-article-highlight-citation t)
7501                   (gnus-article-highlight-signature)))
7502               (let ((ps-left-header
7503                      (list
7504                       (concat "("
7505                               (mail-header-subject gnus-current-headers) ")")
7506                       (concat "("
7507                               (mail-header-from gnus-current-headers) ")")))
7508                     (ps-right-header
7509                      (list
7510                       "/pagenumberstring load"
7511                       (concat "("
7512                               (mail-header-date gnus-current-headers) ")"))))
7513                 (gnus-run-hooks 'gnus-ps-print-hook)
7514                 (save-excursion
7515                   (if window-system
7516                       (ps-spool-buffer-with-faces)
7517                     (ps-spool-buffer)))))
7518           (kill-buffer buffer))))
7519     (gnus-summary-remove-process-mark article))
7520   (ps-despool filename))
7521
7522 (defun gnus-summary-show-article (&optional arg)
7523   "Force re-fetching of the current article.
7524 If ARG (the prefix) is a number, show the article with the charset
7525 defined in `gnus-summary-show-article-charset-alist', or the charset
7526 inputed.
7527 If ARG (the prefix) is non-nil and not a number, show the raw article
7528 without any article massaging functions being run."
7529   (interactive "P")
7530   (cond
7531    ((numberp arg)
7532     (let ((gnus-newsgroup-charset
7533            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7534                (read-coding-system "Charset: ")))
7535           (gnus-newsgroup-ignored-charsets 'gnus-all))
7536       (gnus-summary-select-article nil 'force)
7537       (let ((deps gnus-newsgroup-dependencies)
7538             head header)
7539         (save-excursion
7540           (set-buffer gnus-original-article-buffer)
7541           (save-restriction
7542             (message-narrow-to-head)
7543             (setq head (buffer-string)))
7544           (with-temp-buffer
7545             (insert (format "211 %d Article retrieved.\n"
7546                             (cdr gnus-article-current)))
7547             (insert head)
7548             (insert ".\n")
7549             (let ((nntp-server-buffer (current-buffer)))
7550               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7551         (gnus-data-set-header
7552          (gnus-data-find (cdr gnus-article-current))
7553          header)
7554         (gnus-summary-update-article-line
7555          (cdr gnus-article-current) header))))
7556    ((not arg)
7557     ;; Select the article the normal way.
7558     (gnus-summary-select-article nil 'force))
7559    (t
7560     ;; We have to require this here to make sure that the following
7561     ;; dynamic binding isn't shadowed by autoloading.
7562     (require 'gnus-async)
7563     (require 'gnus-art)
7564     ;; Bind the article treatment functions to nil.
7565     (let ((gnus-have-all-headers t)
7566           gnus-article-prepare-hook
7567           gnus-article-decode-hook
7568           gnus-display-mime-function
7569           gnus-break-pages)
7570       ;; Destroy any MIME parts.
7571       (when (gnus-buffer-live-p gnus-article-buffer)
7572         (save-excursion
7573           (set-buffer gnus-article-buffer)
7574           (mm-destroy-parts gnus-article-mime-handles)
7575           ;; Set it to nil for safety reason.
7576           (setq gnus-article-mime-handle-alist nil)
7577           (setq gnus-article-mime-handles nil)))
7578       (gnus-summary-select-article nil 'force))))
7579   (gnus-summary-goto-subject gnus-current-article)
7580   (gnus-summary-position-point))
7581
7582 (defun gnus-summary-verbose-headers (&optional arg)
7583   "Toggle permanent full header display.
7584 If ARG is a positive number, turn header display on.
7585 If ARG is a negative number, turn header display off."
7586   (interactive "P")
7587   (setq gnus-show-all-headers
7588         (cond ((or (not (numberp arg))
7589                    (zerop arg))
7590                (not gnus-show-all-headers))
7591               ((natnump arg)
7592                t)))
7593   (gnus-summary-show-article))
7594
7595 (defun gnus-summary-toggle-header (&optional arg)
7596   "Show the headers if they are hidden, or hide them if they are shown.
7597 If ARG is a positive number, show the entire header.
7598 If ARG is a negative number, hide the unwanted header lines."
7599   (interactive "P")
7600   (save-excursion
7601     (set-buffer gnus-article-buffer)
7602     (save-restriction
7603       (let* ((buffer-read-only nil)
7604              (inhibit-point-motion-hooks t)
7605              hidden e)
7606         (setq hidden
7607               (if (numberp arg)
7608                   (>= arg 0)
7609                 (save-restriction
7610                   (article-narrow-to-head)
7611                   (gnus-article-hidden-text-p 'headers))))
7612         (goto-char (point-min))
7613         (when (search-forward "\n\n" nil t)
7614           (delete-region (point-min) (1- (point))))
7615         (goto-char (point-min))
7616         (save-excursion
7617           (set-buffer gnus-original-article-buffer)
7618           (goto-char (point-min))
7619           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7620         (insert-buffer-substring gnus-original-article-buffer 1 e)
7621         (save-restriction
7622           (narrow-to-region (point-min) (point))
7623           (article-decode-encoded-words)
7624           (if  hidden
7625               (let ((gnus-treat-hide-headers nil)
7626                     (gnus-treat-hide-boring-headers nil))
7627                 (setq gnus-article-wash-types
7628                       (delq 'headers gnus-article-wash-types))
7629                 (gnus-treat-article 'head))
7630             (gnus-treat-article 'head)))
7631         (gnus-set-mode-line 'article)))))
7632
7633 (defun gnus-summary-show-all-headers ()
7634   "Make all header lines visible."
7635   (interactive)
7636   (gnus-summary-toggle-header 1))
7637
7638 (defun gnus-summary-caesar-message (&optional arg)
7639   "Caesar rotate the current article by 13.
7640 The numerical prefix specifies how many places to rotate each letter
7641 forward."
7642   (interactive "P")
7643   (gnus-summary-select-article)
7644   (let ((mail-header-separator ""))
7645     (gnus-eval-in-buffer-window gnus-article-buffer
7646       (save-restriction
7647         (widen)
7648         (let ((start (window-start))
7649               buffer-read-only)
7650           (message-caesar-buffer-body arg)
7651           (set-window-start (get-buffer-window (current-buffer)) start))))))
7652
7653 (defun gnus-summary-stop-page-breaking ()
7654   "Stop page breaking in the current article."
7655   (interactive)
7656   (gnus-summary-select-article)
7657   (gnus-eval-in-buffer-window gnus-article-buffer
7658     (widen)
7659     (when (gnus-visual-p 'page-marker)
7660       (let ((buffer-read-only nil))
7661         (gnus-remove-text-with-property 'gnus-prev)
7662         (gnus-remove-text-with-property 'gnus-next))
7663       (setq gnus-page-broken nil))))
7664
7665 (defun gnus-summary-move-article (&optional n to-newsgroup
7666                                             select-method action)
7667   "Move the current article to a different newsgroup.
7668 If N is a positive number, move the N next articles.
7669 If N is a negative number, move the N previous articles.
7670 If N is nil and any articles have been marked with the process mark,
7671 move those articles instead.
7672 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7673 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7674 re-spool using this method.
7675
7676 For this function to work, both the current newsgroup and the
7677 newsgroup that you want to move to have to support the `request-move'
7678 and `request-accept' functions.
7679
7680 ACTION can be either `move' (the default), `crosspost' or `copy'."
7681   (interactive "P")
7682   (unless action
7683     (setq action 'move))
7684   ;; Disable marking as read.
7685   (let (gnus-mark-article-hook)
7686     (save-window-excursion
7687       (gnus-summary-select-article)))
7688   ;; Check whether the source group supports the required functions.
7689   (cond ((and (eq action 'move)
7690               (not (gnus-check-backend-function
7691                     'request-move-article gnus-newsgroup-name)))
7692          (error "The current group does not support article moving"))
7693         ((and (eq action 'crosspost)
7694               (not (gnus-check-backend-function
7695                     'request-replace-article gnus-newsgroup-name)))
7696          (error "The current group does not support article editing")))
7697   (let ((articles (gnus-summary-work-articles n))
7698         (prefix (if (gnus-check-backend-function
7699                     'request-move-article gnus-newsgroup-name)
7700                     (gnus-group-real-prefix gnus-newsgroup-name)
7701                   ""))
7702         (names '((move "Move" "Moving")
7703                  (copy "Copy" "Copying")
7704                  (crosspost "Crosspost" "Crossposting")))
7705         (copy-buf (save-excursion
7706                     (nnheader-set-temp-buffer " *copy article*")))
7707         art-group to-method new-xref article to-groups)
7708     (unless (assq action names)
7709       (error "Unknown action %s" action))
7710     ;; Read the newsgroup name.
7711     (when (and (not to-newsgroup)
7712                (not select-method))
7713       (setq to-newsgroup
7714             (gnus-read-move-group-name
7715              (cadr (assq action names))
7716              (symbol-value (intern (format "gnus-current-%s-group" action)))
7717              articles prefix))
7718       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7719     (setq to-method (or select-method
7720                         (gnus-server-to-method
7721                          (gnus-group-method to-newsgroup))))
7722     ;; Check the method we are to move this article to...
7723     (unless (gnus-check-backend-function
7724              'request-accept-article (car to-method))
7725       (error "%s does not support article copying" (car to-method)))
7726     (unless (gnus-check-server to-method)
7727       (error "Can't open server %s" (car to-method)))
7728     (gnus-message 6 "%s to %s: %s..."
7729                   (caddr (assq action names))
7730                   (or (car select-method) to-newsgroup) articles)
7731     (while articles
7732       (setq article (pop articles))
7733       (setq
7734        art-group
7735        (cond
7736         ;; Move the article.
7737         ((eq action 'move)
7738          ;; Remove this article from future suppression.
7739          (gnus-dup-unsuppress-article article)
7740          (gnus-request-move-article
7741           article                       ; Article to move
7742           gnus-newsgroup-name           ; From newsgroup
7743           (nth 1 (gnus-find-method-for-group
7744                   gnus-newsgroup-name)) ; Server
7745           (list 'gnus-request-accept-article
7746                 to-newsgroup (list 'quote select-method)
7747                 (not articles) t)       ; Accept form
7748           (not articles)))              ; Only save nov last time
7749         ;; Copy the article.
7750         ((eq action 'copy)
7751          (save-excursion
7752            (set-buffer copy-buf)
7753            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7754              (gnus-request-accept-article
7755               to-newsgroup select-method (not articles) t))))
7756         ;; Crosspost the article.
7757         ((eq action 'crosspost)
7758          (let ((xref (message-tokenize-header
7759                       (mail-header-xref (gnus-summary-article-header article))
7760                       " ")))
7761            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7762                                   ":" article))
7763            (unless xref
7764              (setq xref (list (system-name))))
7765            (setq new-xref
7766                  (concat
7767                   (mapconcat 'identity
7768                              (delete "Xref:" (delete new-xref xref))
7769                              " ")
7770                   " " new-xref))
7771            (save-excursion
7772              (set-buffer copy-buf)
7773              ;; First put the article in the destination group.
7774              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7775              (when (consp (setq art-group
7776                                 (gnus-request-accept-article
7777                                  to-newsgroup select-method (not articles))))
7778                (setq new-xref (concat new-xref " " (car art-group)
7779                                       ":" (cdr art-group)))
7780                ;; Now we have the new Xrefs header, so we insert
7781                ;; it and replace the new article.
7782                (nnheader-replace-header "Xref" new-xref)
7783                (gnus-request-replace-article
7784                 (cdr art-group) to-newsgroup (current-buffer))
7785                art-group))))))
7786       (cond
7787        ((not art-group)
7788         (gnus-message 1 "Couldn't %s article %s: %s"
7789                       (cadr (assq action names)) article
7790                       (nnheader-get-report (car to-method))))
7791        ((eq art-group 'junk)
7792         (when (eq action 'move)
7793           (gnus-summary-mark-article article gnus-canceled-mark)
7794           (gnus-message 4 "Deleted article %s" article)))
7795        (t
7796         (let* ((pto-group (gnus-group-prefixed-name
7797                            (car art-group) to-method))
7798                (entry
7799                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7800                (info (nth 2 entry))
7801                (to-group (gnus-info-group info))
7802                to-marks)
7803           ;; Update the group that has been moved to.
7804           (when (and info
7805                      (memq action '(move copy)))
7806             (unless (member to-group to-groups)
7807               (push to-group to-groups))
7808
7809             (unless (memq article gnus-newsgroup-unreads)
7810               (push 'read to-marks)
7811               (gnus-info-set-read
7812                info (gnus-add-to-range (gnus-info-read info)
7813                                        (list (cdr art-group)))))
7814
7815             ;; See whether the article is to be put in the cache.
7816             (let ((marks gnus-article-mark-lists)
7817                   (to-article (cdr art-group)))
7818
7819               ;; Enter the article into the cache in the new group,
7820               ;; if that is required.
7821               (when gnus-use-cache
7822                 (gnus-cache-possibly-enter-article
7823                  to-group to-article
7824                  (memq article gnus-newsgroup-marked)
7825                  (memq article gnus-newsgroup-dormant)
7826                  (memq article gnus-newsgroup-unreads)))
7827
7828               (when gnus-preserve-marks
7829                 ;; Copy any marks over to the new group.
7830                 (when (and (equal to-group gnus-newsgroup-name)
7831                            (not (memq article gnus-newsgroup-unreads)))
7832                   ;; Mark this article as read in this group.
7833                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7834                   (setcdr (gnus-active to-group) to-article)
7835                   (setcdr gnus-newsgroup-active to-article))
7836
7837                 (while marks
7838                   (when (memq article (symbol-value
7839                                        (intern (format "gnus-newsgroup-%s"
7840                                                        (caar marks)))))
7841                     (push (cdar marks) to-marks)
7842                     ;; If the other group is the same as this group,
7843                     ;; then we have to add the mark to the list.
7844                     (when (equal to-group gnus-newsgroup-name)
7845                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7846                            (cons to-article
7847                                  (symbol-value
7848                                   (intern (format "gnus-newsgroup-%s"
7849                                                   (caar marks)))))))
7850                     ;; Copy the marks to other group.
7851                     (gnus-add-marked-articles
7852                      to-group (cdar marks) (list to-article) info))
7853                   (setq marks (cdr marks)))
7854
7855                 (gnus-request-set-mark to-group (list (list (list to-article)
7856                                                             'set
7857                                                             to-marks))))
7858
7859               (gnus-dribble-enter
7860                (concat "(gnus-group-set-info '"
7861                        (gnus-prin1-to-string (gnus-get-info to-group))
7862                        ")"))))
7863
7864           ;; Update the Xref header in this article to point to
7865           ;; the new crossposted article we have just created.
7866           (when (eq action 'crosspost)
7867             (save-excursion
7868               (set-buffer copy-buf)
7869               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7870               (nnheader-replace-header "Xref" new-xref)
7871               (gnus-request-replace-article
7872                article gnus-newsgroup-name (current-buffer)))))
7873
7874         ;;;!!!Why is this necessary?
7875         (set-buffer gnus-summary-buffer)
7876
7877         (gnus-summary-goto-subject article)
7878         (when (eq action 'move)
7879           (gnus-summary-mark-article article gnus-canceled-mark))))
7880       (gnus-summary-remove-process-mark article))
7881     ;; Re-activate all groups that have been moved to.
7882     (while to-groups
7883       (save-excursion
7884         (set-buffer gnus-group-buffer)
7885         (when (gnus-group-goto-group (car to-groups) t)
7886           (gnus-group-get-new-news-this-group 1 t))
7887         (pop to-groups)))
7888
7889     (gnus-kill-buffer copy-buf)
7890     (gnus-summary-position-point)
7891     (gnus-set-mode-line 'summary)))
7892
7893 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7894   "Move the current article to a different newsgroup.
7895 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7896 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7897 re-spool using this method."
7898   (interactive "P")
7899   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7900
7901 (defun gnus-summary-crosspost-article (&optional n)
7902   "Crosspost the current article to some other group."
7903   (interactive "P")
7904   (gnus-summary-move-article n nil nil 'crosspost))
7905
7906 (defcustom gnus-summary-respool-default-method nil
7907   "Default method for respooling an article.
7908 If nil, use to the current newsgroup method."
7909   :type '(choice (gnus-select-method :value (nnml ""))
7910                  (const nil))
7911   :group 'gnus-summary-mail)
7912
7913 (defun gnus-summary-respool-article (&optional n method)
7914   "Respool the current article.
7915 The article will be squeezed through the mail spooling process again,
7916 which means that it will be put in some mail newsgroup or other
7917 depending on `nnmail-split-methods'.
7918 If N is a positive number, respool the N next articles.
7919 If N is a negative number, respool the N previous articles.
7920 If N is nil and any articles have been marked with the process mark,
7921 respool those articles instead.
7922
7923 Respooling can be done both from mail groups and \"real\" newsgroups.
7924 In the former case, the articles in question will be moved from the
7925 current group into whatever groups they are destined to.  In the
7926 latter case, they will be copied into the relevant groups."
7927   (interactive
7928    (list current-prefix-arg
7929          (let* ((methods (gnus-methods-using 'respool))
7930                 (methname
7931                  (symbol-name (or gnus-summary-respool-default-method
7932                                   (car (gnus-find-method-for-group
7933                                         gnus-newsgroup-name)))))
7934                 (method
7935                  (gnus-completing-read
7936                   methname "What backend do you want to use when respooling?"
7937                   methods nil t nil 'gnus-mail-method-history))
7938                 ms)
7939            (cond
7940             ((zerop (length (setq ms (gnus-servers-using-backend
7941                                       (intern method)))))
7942              (list (intern method) ""))
7943             ((= 1 (length ms))
7944              (car ms))
7945             (t
7946              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7947                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7948                            ms-alist))))))))
7949   (unless method
7950     (error "No method given for respooling"))
7951   (if (assoc (symbol-name
7952               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7953              (gnus-methods-using 'respool))
7954       (gnus-summary-move-article n nil method)
7955     (gnus-summary-copy-article n nil method)))
7956
7957 (defun gnus-summary-import-article (file &optional edit)
7958   "Import an arbitrary file into a mail newsgroup."
7959   (interactive "fImport file: \nP")
7960   (let ((group gnus-newsgroup-name)
7961         (now (current-time))
7962         atts lines group-art)
7963     (unless (gnus-check-backend-function 'request-accept-article group)
7964       (error "%s does not support article importing" group))
7965     (or (file-readable-p file)
7966         (not (file-regular-p file))
7967         (error "Can't read %s" file))
7968     (save-excursion
7969       (set-buffer (gnus-get-buffer-create " *import file*"))
7970       (erase-buffer)
7971       (nnheader-insert-file-contents file)
7972       (goto-char (point-min))
7973       (unless (nnheader-article-p)
7974         ;; This doesn't look like an article, so we fudge some headers.
7975         (setq atts (file-attributes file)
7976               lines (count-lines (point-min) (point-max)))
7977         (insert "From: " (read-string "From: ") "\n"
7978                 "Subject: " (read-string "Subject: ") "\n"
7979                 "Date: " (message-make-date (nth 5 atts)) "\n"
7980                 "Message-ID: " (message-make-message-id) "\n"
7981                 "Lines: " (int-to-string lines) "\n"
7982                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7983       (setq group-art (gnus-request-accept-article group nil t))
7984       (kill-buffer (current-buffer)))
7985     (setq gnus-newsgroup-active (gnus-activate-group group))
7986     (forward-line 1)
7987     (gnus-summary-goto-article (cdr group-art) nil t)
7988     (when edit
7989       (gnus-summary-edit-article))))
7990
7991 (defun gnus-summary-create-article ()
7992   "Create an article in a mail newsgroup."
7993   (interactive)
7994   (let ((group gnus-newsgroup-name)
7995         (now (current-time))
7996         group-art)
7997     (unless (gnus-check-backend-function 'request-accept-article group)
7998       (error "%s does not support article importing" group))
7999     (save-excursion
8000       (set-buffer (gnus-get-buffer-create " *import file*"))
8001       (erase-buffer)
8002       (goto-char (point-min))
8003       ;; This doesn't look like an article, so we fudge some headers.
8004       (insert "From: " (read-string "From: ") "\n"
8005               "Subject: " (read-string "Subject: ") "\n"
8006               "Date: " (message-make-date now) "\n"
8007               "Message-ID: " (message-make-message-id) "\n")
8008       (setq group-art (gnus-request-accept-article group nil t))
8009       (kill-buffer (current-buffer)))
8010     (setq gnus-newsgroup-active (gnus-activate-group group))
8011     (forward-line 1)
8012     (gnus-summary-goto-article (cdr group-art) nil t)
8013     (gnus-summary-edit-article)))
8014
8015 (defun gnus-summary-article-posted-p ()
8016   "Say whether the current (mail) article is available from news as well.
8017 This will be the case if the article has both been mailed and posted."
8018   (interactive)
8019   (let ((id (mail-header-references (gnus-summary-article-header)))
8020         (gnus-override-method (car (gnus-refer-article-methods))))
8021     (if (gnus-request-head id "")
8022         (gnus-message 2 "The current message was found on %s"
8023                       gnus-override-method)
8024       (gnus-message 2 "The current message couldn't be found on %s"
8025                     gnus-override-method)
8026       nil)))
8027
8028 (defun gnus-summary-expire-articles (&optional now)
8029   "Expire all articles that are marked as expirable in the current group."
8030   (interactive)
8031   (when (gnus-check-backend-function
8032          'request-expire-articles gnus-newsgroup-name)
8033     ;; This backend supports expiry.
8034     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8035            (expirable (if total
8036                           (progn
8037                             ;; We need to update the info for
8038                             ;; this group for `gnus-list-of-read-articles'
8039                             ;; to give us the right answer.
8040                             (gnus-run-hooks 'gnus-exit-group-hook)
8041                             (gnus-summary-update-info)
8042                             (gnus-list-of-read-articles gnus-newsgroup-name))
8043                         (setq gnus-newsgroup-expirable
8044                               (sort gnus-newsgroup-expirable '<))))
8045            (expiry-wait (if now 'immediate
8046                           (gnus-group-find-parameter
8047                            gnus-newsgroup-name 'expiry-wait)))
8048            (nnmail-expiry-target
8049             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8050                 nnmail-expiry-target))
8051            es)
8052       (when expirable
8053         ;; There are expirable articles in this group, so we run them
8054         ;; through the expiry process.
8055         (gnus-message 6 "Expiring articles...")
8056         (unless (gnus-check-group gnus-newsgroup-name)
8057           (error "Can't open server for %s" gnus-newsgroup-name))
8058         ;; The list of articles that weren't expired is returned.
8059         (save-excursion
8060           (if expiry-wait
8061               (let ((nnmail-expiry-wait-function nil)
8062                     (nnmail-expiry-wait expiry-wait))
8063                 (setq es (gnus-request-expire-articles
8064                           expirable gnus-newsgroup-name)))
8065             (setq es (gnus-request-expire-articles
8066                       expirable gnus-newsgroup-name)))
8067           (unless total
8068             (setq gnus-newsgroup-expirable es))
8069           ;; We go through the old list of expirable, and mark all
8070           ;; really expired articles as nonexistent.
8071           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8072             (let ((gnus-use-cache nil))
8073               (while expirable
8074                 (unless (memq (car expirable) es)
8075                   (when (gnus-data-find (car expirable))
8076                     (gnus-summary-mark-article
8077                      (car expirable) gnus-canceled-mark)))
8078                 (setq expirable (cdr expirable))))))
8079         (gnus-message 6 "Expiring articles...done")))))
8080
8081 (defun gnus-summary-expire-articles-now ()
8082   "Expunge all expirable articles in the current group.
8083 This means that *all* articles that are marked as expirable will be
8084 deleted forever, right now."
8085   (interactive)
8086   (or gnus-expert-user
8087       (gnus-yes-or-no-p
8088        "Are you really, really, really sure you want to delete all these messages? ")
8089       (error "Phew!"))
8090   (gnus-summary-expire-articles t))
8091
8092 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8093 (defun gnus-summary-delete-article (&optional n)
8094   "Delete the N next (mail) articles.
8095 This command actually deletes articles.  This is not a marking
8096 command.  The article will disappear forever from your life, never to
8097 return.
8098 If N is negative, delete backwards.
8099 If N is nil and articles have been marked with the process mark,
8100 delete these instead."
8101   (interactive "P")
8102   (unless (gnus-check-backend-function 'request-expire-articles
8103                                        gnus-newsgroup-name)
8104     (error "The current newsgroup does not support article deletion"))
8105   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8106     (error "Couldn't open server"))
8107   ;; Compute the list of articles to delete.
8108   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8109         not-deleted)
8110     (if (and gnus-novice-user
8111              (not (gnus-yes-or-no-p
8112                    (format "Do you really want to delete %s forever? "
8113                            (if (> (length articles) 1)
8114                                (format "these %s articles" (length articles))
8115                              "this article")))))
8116         ()
8117       ;; Delete the articles.
8118       (setq not-deleted (gnus-request-expire-articles
8119                          articles gnus-newsgroup-name 'force))
8120       (while articles
8121         (gnus-summary-remove-process-mark (car articles))
8122         ;; The backend might not have been able to delete the article
8123         ;; after all.
8124         (unless (memq (car articles) not-deleted)
8125           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8126         (setq articles (cdr articles)))
8127       (when not-deleted
8128         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8129     (gnus-summary-position-point)
8130     (gnus-set-mode-line 'summary)
8131     not-deleted))
8132
8133 (defun gnus-summary-edit-article (&optional arg)
8134   "Edit the current article.
8135 This will have permanent effect only in mail groups.
8136 If ARG is nil, edit the decoded articles.
8137 If ARG is 1, edit the raw articles.
8138 If ARG is 2, edit the raw articles even in read-only groups.
8139 If ARG is 3, edit the articles with the current handles.
8140 Otherwise, allow editing of articles even in read-only
8141 groups."
8142   (interactive "P")
8143   (let (force raw current-handles)
8144     (cond
8145      ((null arg))
8146      ((eq arg 1) (setq raw t))
8147      ((eq arg 2) (setq raw t
8148                        force t))
8149      ((eq arg 3) (setq current-handles
8150                        (and (gnus-buffer-live-p gnus-article-buffer)
8151                             (with-current-buffer gnus-article-buffer
8152                               (prog1
8153                                   gnus-article-mime-handles
8154                                   (setq gnus-article-mime-handles nil))))))
8155      (t (setq force t)))
8156     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8157         (error "Can't edit the raw article in group nndraft:drafts."))
8158     (save-excursion
8159       (set-buffer gnus-summary-buffer)
8160       (let ((mail-parse-charset gnus-newsgroup-charset)
8161             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8162         (gnus-set-global-variables)
8163         (when (and (not force)
8164                    (gnus-group-read-only-p))
8165           (error "The current newsgroup does not support article editing"))
8166         (gnus-summary-show-article t)
8167         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8168           (with-current-buffer gnus-article-buffer
8169             (mm-enable-multibyte-mule4)))
8170         (if (equal gnus-newsgroup-name "nndraft:drafts")
8171             (setq raw t))
8172         (gnus-article-edit-article
8173          (if raw 'ignore
8174            `(lambda ()
8175               (let ((mbl mml-buffer-list))
8176                 (setq mml-buffer-list nil)
8177                 (mime-to-mml ,'current-handles)
8178                 (make-local-hook 'kill-buffer-hook)
8179                 (let ((mbl1 mml-buffer-list))
8180                   (setq mml-buffer-list mbl)
8181                   (set (make-local-variable 'mml-buffer-list) mbl1))
8182                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8183          `(lambda (no-highlight)
8184             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8185                   (message-options message-options)
8186                   (message-options-set-recipient)
8187                   (mail-parse-ignored-charsets
8188                    ',gnus-newsgroup-ignored-charsets))
8189               ,(if (not raw) '(progn
8190                                 (mml-to-mime)
8191                                 (mml-destroy-buffers)
8192                                 (remove-hook 'kill-buffer-hook
8193                                              'mml-destroy-buffers t)
8194                                 (kill-local-variable 'mml-buffer-list)))
8195               (gnus-summary-edit-article-done
8196                ,(or (mail-header-references gnus-current-headers) "")
8197                ,(gnus-group-read-only-p)
8198                ,gnus-summary-buffer no-highlight))))))))
8199
8200 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8201
8202 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8203                                                  no-highlight)
8204   "Make edits to the current article permanent."
8205   (interactive)
8206   (save-excursion
8207     ;; The buffer restriction contains the entire article if it exists.
8208     (when (article-goto-body)
8209       (let ((lines (count-lines (point) (point-max)))
8210             (length (- (point-max) (point)))
8211             (case-fold-search t)
8212             (body (copy-marker (point))))
8213         (goto-char (point-min))
8214         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8215           (delete-region (match-beginning 1) (match-end 1))
8216           (insert (number-to-string length)))
8217         (goto-char (point-min))
8218         (when (re-search-forward
8219                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8220           (delete-region (match-beginning 1) (match-end 1))
8221           (insert (number-to-string length)))
8222         (goto-char (point-min))
8223         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8224           (delete-region (match-beginning 1) (match-end 1))
8225           (insert (number-to-string lines))))))
8226   ;; Replace the article.
8227   (let ((buf (current-buffer)))
8228     (with-temp-buffer
8229       (insert-buffer-substring buf)
8230
8231       (if (and (not read-only)
8232                (not (gnus-request-replace-article
8233                      (cdr gnus-article-current) (car gnus-article-current)
8234                      (current-buffer) t)))
8235           (error "Couldn't replace article")
8236         ;; Update the summary buffer.
8237         (if (and references
8238                  (equal (message-tokenize-header references " ")
8239                         (message-tokenize-header
8240                          (or (message-fetch-field "references") "") " ")))
8241             ;; We only have to update this line.
8242             (save-excursion
8243               (save-restriction
8244                 (message-narrow-to-head)
8245                 (let ((head (buffer-string))
8246                       header)
8247                   (with-temp-buffer
8248                     (insert (format "211 %d Article retrieved.\n"
8249                                     (cdr gnus-article-current)))
8250                     (insert head)
8251                     (insert ".\n")
8252                     (let ((nntp-server-buffer (current-buffer)))
8253                       (setq header (car (gnus-get-newsgroup-headers
8254                                          (save-excursion
8255                                            (set-buffer gnus-summary-buffer)
8256                                            gnus-newsgroup-dependencies)
8257                                          t))))
8258                     (save-excursion
8259                       (set-buffer gnus-summary-buffer)
8260                       (gnus-data-set-header
8261                        (gnus-data-find (cdr gnus-article-current))
8262                        header)
8263                       (gnus-summary-update-article-line
8264                        (cdr gnus-article-current) header))))))
8265           ;; Update threads.
8266           (set-buffer (or buffer gnus-summary-buffer))
8267           (gnus-summary-update-article (cdr gnus-article-current)))
8268         ;; Prettify the article buffer again.
8269         (unless no-highlight
8270           (save-excursion
8271             (set-buffer gnus-article-buffer)
8272             ;;;!!! Fix this -- article should be rehighlighted.
8273             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8274             (set-buffer gnus-original-article-buffer)
8275             (gnus-request-article
8276              (cdr gnus-article-current)
8277              (car gnus-article-current) (current-buffer))))
8278         ;; Prettify the summary buffer line.
8279         (when (gnus-visual-p 'summary-highlight 'highlight)
8280           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8281
8282 (defun gnus-summary-edit-wash (key)
8283   "Perform editing command KEY in the article buffer."
8284   (interactive
8285    (list
8286     (progn
8287       (message "%s" (concat (this-command-keys) "- "))
8288       (read-char))))
8289   (message "")
8290   (gnus-summary-edit-article)
8291   (execute-kbd-macro (concat (this-command-keys) key))
8292   (gnus-article-edit-done))
8293
8294 ;;; Respooling
8295
8296 (defun gnus-summary-respool-query (&optional silent trace)
8297   "Query where the respool algorithm would put this article."
8298   (interactive)
8299   (let (gnus-mark-article-hook)
8300     (gnus-summary-select-article)
8301     (save-excursion
8302       (set-buffer gnus-original-article-buffer)
8303       (save-restriction
8304         (message-narrow-to-head)
8305         (let ((groups (nnmail-article-group 'identity trace)))
8306           (unless silent
8307             (if groups
8308                 (message "This message would go to %s"
8309                          (mapconcat 'car groups ", "))
8310               (message "This message would go to no groups"))
8311             groups))))))
8312
8313 (defun gnus-summary-respool-trace ()
8314   "Trace where the respool algorithm would put this article.
8315 Display a buffer showing all fancy splitting patterns which matched."
8316   (interactive)
8317   (gnus-summary-respool-query nil t))
8318
8319 ;; Summary marking commands.
8320
8321 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8322   "Mark articles which has the same subject as read, and then select the next.
8323 If UNMARK is positive, remove any kind of mark.
8324 If UNMARK is negative, tick articles."
8325   (interactive "P")
8326   (when unmark
8327     (setq unmark (prefix-numeric-value unmark)))
8328   (let ((count
8329          (gnus-summary-mark-same-subject
8330           (gnus-summary-article-subject) unmark)))
8331     ;; Select next unread article.  If auto-select-same mode, should
8332     ;; select the first unread article.
8333     (gnus-summary-next-article t (and gnus-auto-select-same
8334                                       (gnus-summary-article-subject)))
8335     (gnus-message 7 "%d article%s marked as %s"
8336                   count (if (= count 1) " is" "s are")
8337                   (if unmark "unread" "read"))))
8338
8339 (defun gnus-summary-kill-same-subject (&optional unmark)
8340   "Mark articles which has the same subject as read.
8341 If UNMARK is positive, remove any kind of mark.
8342 If UNMARK is negative, tick articles."
8343   (interactive "P")
8344   (when unmark
8345     (setq unmark (prefix-numeric-value unmark)))
8346   (let ((count
8347          (gnus-summary-mark-same-subject
8348           (gnus-summary-article-subject) unmark)))
8349     ;; If marked as read, go to next unread subject.
8350     (when (null unmark)
8351       ;; Go to next unread subject.
8352       (gnus-summary-next-subject 1 t))
8353     (gnus-message 7 "%d articles are marked as %s"
8354                   count (if unmark "unread" "read"))))
8355
8356 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8357   "Mark articles with same SUBJECT as read, and return marked number.
8358 If optional argument UNMARK is positive, remove any kinds of marks.
8359 If optional argument UNMARK is negative, mark articles as unread instead."
8360   (let ((count 1))
8361     (save-excursion
8362       (cond
8363        ((null unmark)                   ; Mark as read.
8364         (while (and
8365                 (progn
8366                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8367                   (gnus-summary-show-thread) t)
8368                 (gnus-summary-find-subject subject))
8369           (setq count (1+ count))))
8370        ((> unmark 0)                    ; Tick.
8371         (while (and
8372                 (progn
8373                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8374                   (gnus-summary-show-thread) t)
8375                 (gnus-summary-find-subject subject))
8376           (setq count (1+ count))))
8377        (t                               ; Mark as unread.
8378         (while (and
8379                 (progn
8380                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8381                   (gnus-summary-show-thread) t)
8382                 (gnus-summary-find-subject subject))
8383           (setq count (1+ count)))))
8384       (gnus-set-mode-line 'summary)
8385       ;; Return the number of marked articles.
8386       count)))
8387
8388 (defun gnus-summary-mark-as-processable (n &optional unmark)
8389   "Set the process mark on the next N articles.
8390 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8391 the process mark instead.  The difference between N and the actual
8392 number of articles marked is returned."
8393   (interactive "P")
8394   (if (and (null n) (gnus-region-active-p))
8395       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8396     (setq n (prefix-numeric-value n))
8397     (let ((backward (< n 0))
8398           (n (abs n)))
8399       (while (and
8400               (> n 0)
8401               (if unmark
8402                 (gnus-summary-remove-process-mark
8403                  (gnus-summary-article-number))
8404                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8405               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8406         (setq n (1- n)))
8407       (when (/= 0 n)
8408         (gnus-message 7 "No more articles"))
8409       (gnus-summary-recenter)
8410       (gnus-summary-position-point)
8411       n)))
8412
8413 (defun gnus-summary-unmark-as-processable (n)
8414   "Remove the process mark from the next N articles.
8415 If N is negative, unmark backward instead.  The difference between N and
8416 the actual number of articles unmarked is returned."
8417   (interactive "P")
8418   (gnus-summary-mark-as-processable n t))
8419
8420 (defun gnus-summary-unmark-all-processable ()
8421   "Remove the process mark from all articles."
8422   (interactive)
8423   (save-excursion
8424     (while gnus-newsgroup-processable
8425       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8426   (gnus-summary-position-point))
8427
8428 (defun gnus-summary-add-mark (article type)
8429   "Mark ARTICLE with a mark of TYPE."
8430   (let ((vtype (car (assq type gnus-article-mark-lists)))
8431         var)
8432     (if (not vtype)
8433         (error "No such mark type: %s" type)
8434       (setq var (intern (format "gnus-newsgroup-%s" type)))
8435       (set var (cons article (symbol-value var)))
8436       (if (memq type '(processable cached replied forwarded saved))
8437           (gnus-summary-update-secondary-mark article)
8438         ;;; !!! This is bobus.  We should find out what primary
8439         ;;; !!! mark we want to set.
8440         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8441
8442 (defun gnus-summary-mark-as-expirable (n)
8443   "Mark N articles forward as expirable.
8444 If N is negative, mark backward instead.  The difference between N and
8445 the actual number of articles marked is returned."
8446   (interactive "p")
8447   (gnus-summary-mark-forward n gnus-expirable-mark))
8448
8449 (defun gnus-summary-mark-article-as-replied (article)
8450   "Mark ARTICLE as replied to and update the summary line.
8451 ARTICLE can also be a list of articles."
8452   (interactive (list (gnus-summary-article-number)))
8453   (let ((articles (if (listp article) article (list article))))
8454     (dolist (article articles)
8455       (push article gnus-newsgroup-replied)
8456       (let ((buffer-read-only nil))
8457         (when (gnus-summary-goto-subject article nil t)
8458           (gnus-summary-update-secondary-mark article))))))
8459
8460 (defun gnus-summary-mark-article-as-forwarded (article)
8461   "Mark ARTICLE as forwarded and update the summary line.
8462 ARTICLE can also be a list of articles."
8463   (let ((articles (if (listp article) article (list article))))
8464     (dolist (article articles)
8465       (push article gnus-newsgroup-forwarded)
8466       (let ((buffer-read-only nil))
8467         (when (gnus-summary-goto-subject article nil t)
8468           (gnus-summary-update-secondary-mark article))))))
8469
8470 (defun gnus-summary-set-bookmark (article)
8471   "Set a bookmark in current article."
8472   (interactive (list (gnus-summary-article-number)))
8473   (when (or (not (get-buffer gnus-article-buffer))
8474             (not gnus-current-article)
8475             (not gnus-article-current)
8476             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8477     (error "No current article selected"))
8478   ;; Remove old bookmark, if one exists.
8479   (let ((old (assq article gnus-newsgroup-bookmarks)))
8480     (when old
8481       (setq gnus-newsgroup-bookmarks
8482             (delq old gnus-newsgroup-bookmarks))))
8483   ;; Set the new bookmark, which is on the form
8484   ;; (article-number . line-number-in-body).
8485   (push
8486    (cons article
8487          (save-excursion
8488            (set-buffer gnus-article-buffer)
8489            (count-lines
8490             (min (point)
8491                  (save-excursion
8492                    (goto-char (point-min))
8493                    (search-forward "\n\n" nil t)
8494                    (point)))
8495             (point))))
8496    gnus-newsgroup-bookmarks)
8497   (gnus-message 6 "A bookmark has been added to the current article."))
8498
8499 (defun gnus-summary-remove-bookmark (article)
8500   "Remove the bookmark from the current article."
8501   (interactive (list (gnus-summary-article-number)))
8502   ;; Remove old bookmark, if one exists.
8503   (let ((old (assq article gnus-newsgroup-bookmarks)))
8504     (if old
8505         (progn
8506           (setq gnus-newsgroup-bookmarks
8507                 (delq old gnus-newsgroup-bookmarks))
8508           (gnus-message 6 "Removed bookmark."))
8509       (gnus-message 6 "No bookmark in current article."))))
8510
8511 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8512 (defun gnus-summary-mark-as-dormant (n)
8513   "Mark N articles forward as dormant.
8514 If N is negative, mark backward instead.  The difference between N and
8515 the actual number of articles marked is returned."
8516   (interactive "p")
8517   (gnus-summary-mark-forward n gnus-dormant-mark))
8518
8519 (defun gnus-summary-set-process-mark (article)
8520   "Set the process mark on ARTICLE and update the summary line."
8521   (setq gnus-newsgroup-processable
8522         (cons article
8523               (delq article gnus-newsgroup-processable)))
8524   (when (gnus-summary-goto-subject article)
8525     (gnus-summary-show-thread)
8526     (gnus-summary-goto-subject article)
8527     (gnus-summary-update-secondary-mark article)))
8528
8529 (defun gnus-summary-remove-process-mark (article)
8530   "Remove the process mark from ARTICLE and update the summary line."
8531   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8532   (when (gnus-summary-goto-subject article)
8533     (gnus-summary-show-thread)
8534     (gnus-summary-goto-subject article)
8535     (gnus-summary-update-secondary-mark article)))
8536
8537 (defun gnus-summary-set-saved-mark (article)
8538   "Set the process mark on ARTICLE and update the summary line."
8539   (push article gnus-newsgroup-saved)
8540   (when (gnus-summary-goto-subject article)
8541     (gnus-summary-update-secondary-mark article)))
8542
8543 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8544   "Mark N articles as read forwards.
8545 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8546 The difference between N and the actual number of articles marked is
8547 returned.
8548 Iff NO-EXPIRE, auto-expiry will be inhibited."
8549   (interactive "p")
8550   (gnus-summary-show-thread)
8551   (let ((backward (< n 0))
8552         (gnus-summary-goto-unread
8553          (and gnus-summary-goto-unread
8554               (not (eq gnus-summary-goto-unread 'never))
8555               (not (memq mark (list gnus-unread-mark
8556                                     gnus-ticked-mark gnus-dormant-mark)))))
8557         (n (abs n))
8558         (mark (or mark gnus-del-mark)))
8559     (while (and (> n 0)
8560                 (gnus-summary-mark-article nil mark no-expire)
8561                 (zerop (gnus-summary-next-subject
8562                         (if backward -1 1)
8563                         (and gnus-summary-goto-unread
8564                              (not (eq gnus-summary-goto-unread 'never)))
8565                         t)))
8566       (setq n (1- n)))
8567     (when (/= 0 n)
8568       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8569     (gnus-summary-recenter)
8570     (gnus-summary-position-point)
8571     (gnus-set-mode-line 'summary)
8572     n))
8573
8574 (defun gnus-summary-mark-article-as-read (mark)
8575   "Mark the current article quickly as read with MARK."
8576   (let ((article (gnus-summary-article-number)))
8577     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8578     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8579     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8580     (push (cons article mark) gnus-newsgroup-reads)
8581     ;; Possibly remove from cache, if that is used.
8582     (when gnus-use-cache
8583       (gnus-cache-enter-remove-article article))
8584     ;; Allow the backend to change the mark.
8585     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8586     ;; Check for auto-expiry.
8587     (when (and gnus-newsgroup-auto-expire
8588                (memq mark gnus-auto-expirable-marks))
8589       (setq mark gnus-expirable-mark)
8590       ;; Let the backend know about the mark change.
8591       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8592       (push article gnus-newsgroup-expirable))
8593     ;; Set the mark in the buffer.
8594     (gnus-summary-update-mark mark 'unread)
8595     t))
8596
8597 (defun gnus-summary-mark-article-as-unread (mark)
8598   "Mark the current article quickly as unread with MARK."
8599   (let* ((article (gnus-summary-article-number))
8600          (old-mark (gnus-summary-article-mark article)))
8601     ;; Allow the backend to change the mark.
8602     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8603     (if (eq mark old-mark)
8604         t
8605       (if (<= article 0)
8606           (progn
8607             (gnus-error 1 "Can't mark negative article numbers")
8608             nil)
8609         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8610         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8611         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8612         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8613         (cond ((= mark gnus-ticked-mark)
8614                (push article gnus-newsgroup-marked))
8615               ((= mark gnus-dormant-mark)
8616                (push article gnus-newsgroup-dormant))
8617               (t
8618                (push article gnus-newsgroup-unreads)))
8619         (gnus-pull article gnus-newsgroup-reads)
8620
8621         ;; See whether the article is to be put in the cache.
8622         (and gnus-use-cache
8623              (vectorp (gnus-summary-article-header article))
8624              (save-excursion
8625                (gnus-cache-possibly-enter-article
8626                 gnus-newsgroup-name article
8627                 (= mark gnus-ticked-mark)
8628                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8629
8630         ;; Fix the mark.
8631         (gnus-summary-update-mark mark 'unread)
8632         t))))
8633
8634 (defun gnus-summary-mark-article (&optional article mark no-expire)
8635   "Mark ARTICLE with MARK.  MARK can be any character.
8636 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8637 `??' (dormant) and `?E' (expirable).
8638 If MARK is nil, then the default character `?r' is used.
8639 If ARTICLE is nil, then the article on the current line will be
8640 marked.
8641 Iff NO-EXPIRE, auto-expiry will be inhibited."
8642   ;; The mark might be a string.
8643   (when (stringp mark)
8644     (setq mark (aref mark 0)))
8645   ;; If no mark is given, then we check auto-expiring.
8646   (when (null mark)
8647     (setq mark gnus-del-mark))
8648   (when (and (not no-expire)
8649              gnus-newsgroup-auto-expire
8650              (memq mark gnus-auto-expirable-marks))
8651     (setq mark gnus-expirable-mark))
8652   (let ((article (or article (gnus-summary-article-number)))
8653         (old-mark (gnus-summary-article-mark article)))
8654     ;; Allow the backend to change the mark.
8655     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8656     (if (eq mark old-mark)
8657         t
8658       (unless article
8659         (error "No article on current line"))
8660       (if (not (if (or (= mark gnus-unread-mark)
8661                        (= mark gnus-ticked-mark)
8662                        (= mark gnus-dormant-mark))
8663                    (gnus-mark-article-as-unread article mark)
8664                  (gnus-mark-article-as-read article mark)))
8665           t
8666         ;; See whether the article is to be put in the cache.
8667         (and gnus-use-cache
8668              (not (= mark gnus-canceled-mark))
8669              (vectorp (gnus-summary-article-header article))
8670              (save-excursion
8671                (gnus-cache-possibly-enter-article
8672                 gnus-newsgroup-name article
8673                 (= mark gnus-ticked-mark)
8674                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8675
8676         (when (gnus-summary-goto-subject article nil t)
8677           (let ((buffer-read-only nil))
8678             (gnus-summary-show-thread)
8679             ;; Fix the mark.
8680             (gnus-summary-update-mark mark 'unread)
8681             t))))))
8682
8683 (defun gnus-summary-update-secondary-mark (article)
8684   "Update the secondary (read, process, cache) mark."
8685   (gnus-summary-update-mark
8686    (cond ((memq article gnus-newsgroup-processable)
8687           gnus-process-mark)
8688          ((memq article gnus-newsgroup-cached)
8689           gnus-cached-mark)
8690          ((memq article gnus-newsgroup-replied)
8691           gnus-replied-mark)
8692          ((memq article gnus-newsgroup-forwarded)
8693           gnus-forwarded-mark)
8694          ((memq article gnus-newsgroup-saved)
8695           gnus-saved-mark)
8696          (t gnus-no-mark))
8697    'replied)
8698   (when (gnus-visual-p 'summary-highlight 'highlight)
8699     (gnus-run-hooks 'gnus-summary-update-hook))
8700   t)
8701
8702 (defun gnus-summary-update-mark (mark type)
8703   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8704         (buffer-read-only nil))
8705     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8706     (when forward
8707       (when (looking-at "\r")
8708         (incf forward))
8709       (when (<= (+ forward (point)) (point-max))
8710         ;; Go to the right position on the line.
8711         (goto-char (+ forward (point)))
8712         ;; Replace the old mark with the new mark.
8713         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8714         ;; Optionally update the marks by some user rule.
8715         (when (eq type 'unread)
8716           (gnus-data-set-mark
8717            (gnus-data-find (gnus-summary-article-number)) mark)
8718           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8719
8720 (defun gnus-mark-article-as-read (article &optional mark)
8721   "Enter ARTICLE in the pertinent lists and remove it from others."
8722   ;; Make the article expirable.
8723   (let ((mark (or mark gnus-del-mark)))
8724     (if (= mark gnus-expirable-mark)
8725         (push article gnus-newsgroup-expirable)
8726       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8727     ;; Remove from unread and marked lists.
8728     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8729     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8730     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8731     (push (cons article mark) gnus-newsgroup-reads)
8732     ;; Possibly remove from cache, if that is used.
8733     (when gnus-use-cache
8734       (gnus-cache-enter-remove-article article))
8735     t))
8736
8737 (defun gnus-mark-article-as-unread (article &optional mark)
8738   "Enter ARTICLE in the pertinent lists and remove it from others."
8739   (let ((mark (or mark gnus-ticked-mark)))
8740     (if (<= article 0)
8741         (progn
8742           (gnus-error 1 "Can't mark negative article numbers")
8743           nil)
8744       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8745             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8746             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8747             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8748
8749       ;; Unsuppress duplicates?
8750       (when gnus-suppress-duplicates
8751         (gnus-dup-unsuppress-article article))
8752
8753       (cond ((= mark gnus-ticked-mark)
8754              (push article gnus-newsgroup-marked))
8755             ((= mark gnus-dormant-mark)
8756              (push article gnus-newsgroup-dormant))
8757             (t
8758              (push article gnus-newsgroup-unreads)))
8759       (gnus-pull article gnus-newsgroup-reads)
8760       t)))
8761
8762 (defalias 'gnus-summary-mark-as-unread-forward
8763   'gnus-summary-tick-article-forward)
8764 (make-obsolete 'gnus-summary-mark-as-unread-forward
8765                'gnus-summary-tick-article-forward)
8766 (defun gnus-summary-tick-article-forward (n)
8767   "Tick N articles forwards.
8768 If N is negative, tick backwards instead.
8769 The difference between N and the number of articles ticked is returned."
8770   (interactive "p")
8771   (gnus-summary-mark-forward n gnus-ticked-mark))
8772
8773 (defalias 'gnus-summary-mark-as-unread-backward
8774   'gnus-summary-tick-article-backward)
8775 (make-obsolete 'gnus-summary-mark-as-unread-backward
8776                'gnus-summary-tick-article-backward)
8777 (defun gnus-summary-tick-article-backward (n)
8778   "Tick N articles backwards.
8779 The difference between N and the number of articles ticked is returned."
8780   (interactive "p")
8781   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8782
8783 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8784 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8785 (defun gnus-summary-tick-article (&optional article clear-mark)
8786   "Mark current article as unread.
8787 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8788 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8789   (interactive)
8790   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8791                                        gnus-ticked-mark)))
8792
8793 (defun gnus-summary-mark-as-read-forward (n)
8794   "Mark N articles as read forwards.
8795 If N is negative, mark backwards instead.
8796 The difference between N and the actual number of articles marked is
8797 returned."
8798   (interactive "p")
8799   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8800
8801 (defun gnus-summary-mark-as-read-backward (n)
8802   "Mark the N articles as read backwards.
8803 The difference between N and the actual number of articles marked is
8804 returned."
8805   (interactive "p")
8806   (gnus-summary-mark-forward
8807    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8808
8809 (defun gnus-summary-mark-as-read (&optional article mark)
8810   "Mark current article as read.
8811 ARTICLE specifies the article to be marked as read.
8812 MARK specifies a string to be inserted at the beginning of the line."
8813   (gnus-summary-mark-article article mark))
8814
8815 (defun gnus-summary-clear-mark-forward (n)
8816   "Clear marks from N articles forward.
8817 If N is negative, clear backward instead.
8818 The difference between N and the number of marks cleared is returned."
8819   (interactive "p")
8820   (gnus-summary-mark-forward n gnus-unread-mark))
8821
8822 (defun gnus-summary-clear-mark-backward (n)
8823   "Clear marks from N articles backward.
8824 The difference between N and the number of marks cleared is returned."
8825   (interactive "p")
8826   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8827
8828 (defun gnus-summary-mark-unread-as-read ()
8829   "Intended to be used by `gnus-summary-mark-article-hook'."
8830   (when (memq gnus-current-article gnus-newsgroup-unreads)
8831     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8832
8833 (defun gnus-summary-mark-read-and-unread-as-read ()
8834   "Intended to be used by `gnus-summary-mark-article-hook'."
8835   (let ((mark (gnus-summary-article-mark)))
8836     (when (or (gnus-unread-mark-p mark)
8837               (gnus-read-mark-p mark))
8838       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8839
8840 (defun gnus-summary-mark-unread-as-ticked ()
8841    "Intended to be used by `gnus-summary-mark-article-hook'."
8842   (when (memq gnus-current-article gnus-newsgroup-unreads)
8843     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
8844
8845 (defun gnus-summary-mark-region-as-read (point mark all)
8846   "Mark all unread articles between point and mark as read.
8847 If given a prefix, mark all articles between point and mark as read,
8848 even ticked and dormant ones."
8849   (interactive "r\nP")
8850   (save-excursion
8851     (let (article)
8852       (goto-char point)
8853       (beginning-of-line)
8854       (while (and
8855               (< (point) mark)
8856               (progn
8857                 (when (or all
8858                           (memq (setq article (gnus-summary-article-number))
8859                                 gnus-newsgroup-unreads))
8860                   (gnus-summary-mark-article article gnus-del-mark))
8861                 t)
8862               (gnus-summary-find-next))))))
8863
8864 (defun gnus-summary-mark-below (score mark)
8865   "Mark articles with score less than SCORE with MARK."
8866   (interactive "P\ncMark: ")
8867   (setq score (if score
8868                   (prefix-numeric-value score)
8869                 (or gnus-summary-default-score 0)))
8870   (save-excursion
8871     (set-buffer gnus-summary-buffer)
8872     (goto-char (point-min))
8873     (while
8874         (progn
8875           (and (< (gnus-summary-article-score) score)
8876                (gnus-summary-mark-article nil mark))
8877           (gnus-summary-find-next)))))
8878
8879 (defun gnus-summary-kill-below (&optional score)
8880   "Mark articles with score below SCORE as read."
8881   (interactive "P")
8882   (gnus-summary-mark-below score gnus-killed-mark))
8883
8884 (defun gnus-summary-clear-above (&optional score)
8885   "Clear all marks from articles with score above SCORE."
8886   (interactive "P")
8887   (gnus-summary-mark-above score gnus-unread-mark))
8888
8889 (defun gnus-summary-tick-above (&optional score)
8890   "Tick all articles with score above SCORE."
8891   (interactive "P")
8892   (gnus-summary-mark-above score gnus-ticked-mark))
8893
8894 (defun gnus-summary-mark-above (score mark)
8895   "Mark articles with score over SCORE with MARK."
8896   (interactive "P\ncMark: ")
8897   (setq score (if score
8898                   (prefix-numeric-value score)
8899                 (or gnus-summary-default-score 0)))
8900   (save-excursion
8901     (set-buffer gnus-summary-buffer)
8902     (goto-char (point-min))
8903     (while (and (progn
8904                   (when (> (gnus-summary-article-score) score)
8905                     (gnus-summary-mark-article nil mark))
8906                   t)
8907                 (gnus-summary-find-next)))))
8908
8909 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8910 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8911 (defun gnus-summary-limit-include-expunged (&optional no-error)
8912   "Display all the hidden articles that were expunged for low scores."
8913   (interactive)
8914   (let ((buffer-read-only nil))
8915     (let ((scored gnus-newsgroup-scored)
8916           headers h)
8917       (while scored
8918         (unless (gnus-summary-article-header (caar scored))
8919           (and (setq h (gnus-number-to-header (caar scored)))
8920                (< (cdar scored) gnus-summary-expunge-below)
8921                (push h headers)))
8922         (setq scored (cdr scored)))
8923       (if (not headers)
8924           (when (not no-error)
8925             (error "No expunged articles hidden"))
8926         (goto-char (point-min))
8927         (push gnus-newsgroup-limit gnus-newsgroup-limits)
8928         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
8929         (mapcar (lambda (x) (push (mail-header-number x) 
8930                                   gnus-newsgroup-limit))
8931                 headers)
8932         (gnus-summary-prepare-unthreaded (nreverse headers))
8933         (goto-char (point-min))
8934         (gnus-summary-position-point)
8935         t))))
8936
8937 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8938   "Mark all unread articles in this newsgroup as read.
8939 If prefix argument ALL is non-nil, ticked and dormant articles will
8940 also be marked as read.
8941 If QUIETLY is non-nil, no questions will be asked.
8942 If TO-HERE is non-nil, it should be a point in the buffer.  All
8943 articles before this point will be marked as read.
8944 Note that this function will only catch up the unread article
8945 in the current summary buffer limitation.
8946 The number of articles marked as read is returned."
8947   (interactive "P")
8948   (prog1
8949       (save-excursion
8950         (when (or quietly
8951                   (not gnus-interactive-catchup) ;Without confirmation?
8952                   gnus-expert-user
8953                   (gnus-y-or-n-p
8954                    (if all
8955                        "Mark absolutely all articles as read? "
8956                      "Mark all unread articles as read? ")))
8957           (if (and not-mark
8958                    (not gnus-newsgroup-adaptive)
8959                    (not gnus-newsgroup-auto-expire)
8960                    (not gnus-suppress-duplicates)
8961                    (or (not gnus-use-cache)
8962                        (eq gnus-use-cache 'passive)))
8963               (progn
8964                 (when all
8965                   (setq gnus-newsgroup-marked nil
8966                         gnus-newsgroup-dormant nil))
8967                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8968             ;; We actually mark all articles as canceled, which we
8969             ;; have to do when using auto-expiry or adaptive scoring.
8970             (gnus-summary-show-all-threads)
8971             (when (gnus-summary-first-subject (not all) t)
8972               (while (and
8973                       (if to-here (< (point) to-here) t)
8974                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8975                       (gnus-summary-find-next (not all) nil nil t))))
8976             (gnus-set-mode-line 'summary))
8977           t))
8978     (gnus-summary-position-point)))
8979
8980 (defun gnus-summary-catchup-to-here (&optional all)
8981   "Mark all unticked articles before the current one as read.
8982 If ALL is non-nil, also mark ticked and dormant articles as read."
8983   (interactive "P")
8984   (save-excursion
8985     (gnus-save-hidden-threads
8986       (let ((beg (point)))
8987         ;; We check that there are unread articles.
8988         (when (or all (gnus-summary-find-prev))
8989           (gnus-summary-catchup all t beg)))))
8990   (gnus-summary-position-point))
8991
8992 (defun gnus-summary-catchup-all (&optional quietly)
8993   "Mark all articles in this newsgroup as read."
8994   (interactive "P")
8995   (gnus-summary-catchup t quietly))
8996
8997 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8998   "Mark all unread articles in this group as read, then exit.
8999 If prefix argument ALL is non-nil, all articles are marked as read.
9000 If QUIETLY is non-nil, no questions will be asked."
9001   (interactive "P")
9002   (when (gnus-summary-catchup all quietly nil 'fast)
9003     ;; Select next newsgroup or exit.
9004     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9005              (eq gnus-auto-select-next 'quietly))
9006         (gnus-summary-next-group nil)
9007       (gnus-summary-exit))))
9008
9009 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9010   "Mark all articles in this newsgroup as read, and then exit."
9011   (interactive "P")
9012   (gnus-summary-catchup-and-exit t quietly))
9013
9014 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9015   "Mark all articles in this group as read and select the next group.
9016 If given a prefix, mark all articles, unread as well as ticked, as
9017 read."
9018   (interactive "P")
9019   (save-excursion
9020     (gnus-summary-catchup all))
9021   (gnus-summary-next-group))
9022
9023 ;;;
9024 ;;; with article
9025 ;;;
9026
9027 (defmacro gnus-with-article (article &rest forms)
9028   "Select ARTICLE and perform FORMS in the original article buffer.
9029 Then replace the article with the result."
9030   `(progn
9031      ;; We don't want the article to be marked as read.
9032      (let (gnus-mark-article-hook)
9033        (gnus-summary-select-article t t nil ,article))
9034      (set-buffer gnus-original-article-buffer)
9035      ,@forms
9036      (if (not (gnus-check-backend-function
9037                'request-replace-article (car gnus-article-current)))
9038          (gnus-message 5 "Read-only group; not replacing")
9039        (unless (gnus-request-replace-article
9040                 ,article (car gnus-article-current)
9041                 (current-buffer) t)
9042          (error "Couldn't replace article")))
9043      ;; The cache and backlog have to be flushed somewhat.
9044      (when gnus-keep-backlog
9045        (gnus-backlog-remove-article
9046         (car gnus-article-current) (cdr gnus-article-current)))
9047      (when gnus-use-cache
9048        (gnus-cache-update-article
9049         (car gnus-article-current) (cdr gnus-article-current)))))
9050
9051 (put 'gnus-with-article 'lisp-indent-function 1)
9052 (put 'gnus-with-article 'edebug-form-spec '(form body))
9053
9054 ;; Thread-based commands.
9055
9056 (defun gnus-summary-articles-in-thread (&optional article)
9057   "Return a list of all articles in the current thread.
9058 If ARTICLE is non-nil, return all articles in the thread that starts
9059 with that article."
9060   (let* ((article (or article (gnus-summary-article-number)))
9061          (data (gnus-data-find-list article))
9062          (top-level (gnus-data-level (car data)))
9063          (top-subject
9064           (cond ((null gnus-thread-operation-ignore-subject)
9065                  (gnus-simplify-subject-re
9066                   (mail-header-subject (gnus-data-header (car data)))))
9067                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9068                  (gnus-simplify-subject-fuzzy
9069                   (mail-header-subject (gnus-data-header (car data)))))
9070                 (t nil)))
9071          (end-point (save-excursion
9072                       (if (gnus-summary-go-to-next-thread)
9073                           (point) (point-max))))
9074          articles)
9075     (while (and data
9076                 (< (gnus-data-pos (car data)) end-point))
9077       (when (or (not top-subject)
9078                 (string= top-subject
9079                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9080                              (gnus-simplify-subject-fuzzy
9081                               (mail-header-subject
9082                                (gnus-data-header (car data))))
9083                            (gnus-simplify-subject-re
9084                             (mail-header-subject
9085                              (gnus-data-header (car data)))))))
9086         (push (gnus-data-number (car data)) articles))
9087       (unless (and (setq data (cdr data))
9088                    (> (gnus-data-level (car data)) top-level))
9089         (setq data nil)))
9090     ;; Return the list of articles.
9091     (nreverse articles)))
9092
9093 (defun gnus-summary-rethread-current ()
9094   "Rethread the thread the current article is part of."
9095   (interactive)
9096   (let* ((gnus-show-threads t)
9097          (article (gnus-summary-article-number))
9098          (id (mail-header-id (gnus-summary-article-header)))
9099          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9100     (unless id
9101       (error "No article on the current line"))
9102     (gnus-rebuild-thread id)
9103     (gnus-summary-goto-subject article)))
9104
9105 (defun gnus-summary-reparent-thread ()
9106   "Make the current article child of the marked (or previous) article.
9107
9108 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9109 is non-nil or the Subject: of both articles are the same."
9110   (interactive)
9111   (unless (not (gnus-group-read-only-p))
9112     (error "The current newsgroup does not support article editing"))
9113   (unless (<= (length gnus-newsgroup-processable) 1)
9114     (error "No more than one article may be marked"))
9115   (save-window-excursion
9116     (let ((gnus-article-buffer " *reparent*")
9117           (current-article (gnus-summary-article-number))
9118           ;; First grab the marked article, otherwise one line up.
9119           (parent-article (if (not (null gnus-newsgroup-processable))
9120                               (car gnus-newsgroup-processable)
9121                             (save-excursion
9122                               (if (eq (forward-line -1) 0)
9123                                   (gnus-summary-article-number)
9124                                 (error "Beginning of summary buffer"))))))
9125       (unless (not (eq current-article parent-article))
9126         (error "An article may not be self-referential"))
9127       (let ((message-id (mail-header-id
9128                          (gnus-summary-article-header parent-article))))
9129         (unless (and message-id (not (equal message-id "")))
9130           (error "No message-id in desired parent"))
9131         (gnus-with-article current-article
9132           (save-restriction
9133             (goto-char (point-min))
9134             (message-narrow-to-head)
9135             (if (re-search-forward "^References: " nil t)
9136                 (progn
9137                   (re-search-forward "^[^ \t]" nil t)
9138                   (forward-line -1)
9139                   (end-of-line)
9140                   (insert " " message-id))
9141               (insert "References: " message-id "\n"))))
9142         (set-buffer gnus-summary-buffer)
9143         (gnus-summary-unmark-all-processable)
9144         (gnus-summary-update-article current-article)
9145         (gnus-summary-rethread-current)
9146         (gnus-message 3 "Article %d is now the child of article %d"
9147                       current-article parent-article)))))
9148
9149 (defun gnus-summary-toggle-threads (&optional arg)
9150   "Toggle showing conversation threads.
9151 If ARG is positive number, turn showing conversation threads on."
9152   (interactive "P")
9153   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9154     (setq gnus-show-threads
9155           (if (null arg) (not gnus-show-threads)
9156             (> (prefix-numeric-value arg) 0)))
9157     (gnus-summary-prepare)
9158     (gnus-summary-goto-subject current)
9159     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9160     (gnus-summary-position-point)))
9161
9162 (defun gnus-summary-show-all-threads ()
9163   "Show all threads."
9164   (interactive)
9165   (save-excursion
9166     (let ((buffer-read-only nil))
9167       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9168   (gnus-summary-position-point))
9169
9170 (defun gnus-summary-show-thread ()
9171   "Show thread subtrees.
9172 Returns nil if no thread was there to be shown."
9173   (interactive)
9174   (let ((buffer-read-only nil)
9175         (orig (point))
9176         ;; first goto end then to beg, to have point at beg after let
9177         (end (progn (end-of-line) (point)))
9178         (beg (progn (beginning-of-line) (point))))
9179     (prog1
9180         ;; Any hidden lines here?
9181         (search-forward "\r" end t)
9182       (subst-char-in-region beg end ?\^M ?\n t)
9183       (goto-char orig)
9184       (gnus-summary-position-point))))
9185
9186 (defun gnus-summary-hide-all-threads ()
9187   "Hide all thread subtrees."
9188   (interactive)
9189   (save-excursion
9190     (goto-char (point-min))
9191     (gnus-summary-hide-thread)
9192     (while (zerop (gnus-summary-next-thread 1 t))
9193       (gnus-summary-hide-thread)))
9194   (gnus-summary-position-point))
9195
9196 (defun gnus-summary-hide-thread ()
9197   "Hide thread subtrees.
9198 Returns nil if no threads were there to be hidden."
9199   (interactive)
9200   (let ((buffer-read-only nil)
9201         (start (point))
9202         (article (gnus-summary-article-number)))
9203     (goto-char start)
9204     ;; Go forward until either the buffer ends or the subthread
9205     ;; ends.
9206     (when (and (not (eobp))
9207                (or (zerop (gnus-summary-next-thread 1 t))
9208                    (goto-char (point-max))))
9209       (prog1
9210           (if (and (> (point) start)
9211                    (search-backward "\n" start t))
9212               (progn
9213                 (subst-char-in-region start (point) ?\n ?\^M)
9214                 (gnus-summary-goto-subject article))
9215             (goto-char start)
9216             nil)))))
9217
9218 (defun gnus-summary-go-to-next-thread (&optional previous)
9219   "Go to the same level (or less) next thread.
9220 If PREVIOUS is non-nil, go to previous thread instead.
9221 Return the article number moved to, or nil if moving was impossible."
9222   (let ((level (gnus-summary-thread-level))
9223         (way (if previous -1 1))
9224         (beg (point)))
9225     (forward-line way)
9226     (while (and (not (eobp))
9227                 (< level (gnus-summary-thread-level)))
9228       (forward-line way))
9229     (if (eobp)
9230         (progn
9231           (goto-char beg)
9232           nil)
9233       (setq beg (point))
9234       (prog1
9235           (gnus-summary-article-number)
9236         (goto-char beg)))))
9237
9238 (defun gnus-summary-next-thread (n &optional silent)
9239   "Go to the same level next N'th thread.
9240 If N is negative, search backward instead.
9241 Returns the difference between N and the number of skips actually
9242 done.
9243
9244 If SILENT, don't output messages."
9245   (interactive "p")
9246   (let ((backward (< n 0))
9247         (n (abs n)))
9248     (while (and (> n 0)
9249                 (gnus-summary-go-to-next-thread backward))
9250       (decf n))
9251     (unless silent
9252       (gnus-summary-position-point))
9253     (when (and (not silent) (/= 0 n))
9254       (gnus-message 7 "No more threads"))
9255     n))
9256
9257 (defun gnus-summary-prev-thread (n)
9258   "Go to the same level previous N'th thread.
9259 Returns the difference between N and the number of skips actually
9260 done."
9261   (interactive "p")
9262   (gnus-summary-next-thread (- n)))
9263
9264 (defun gnus-summary-go-down-thread ()
9265   "Go down one level in the current thread."
9266   (let ((children (gnus-summary-article-children)))
9267     (when children
9268       (gnus-summary-goto-subject (car children)))))
9269
9270 (defun gnus-summary-go-up-thread ()
9271   "Go up one level in the current thread."
9272   (let ((parent (gnus-summary-article-parent)))
9273     (when parent
9274       (gnus-summary-goto-subject parent))))
9275
9276 (defun gnus-summary-down-thread (n)
9277   "Go down thread N steps.
9278 If N is negative, go up instead.
9279 Returns the difference between N and how many steps down that were
9280 taken."
9281   (interactive "p")
9282   (let ((up (< n 0))
9283         (n (abs n)))
9284     (while (and (> n 0)
9285                 (if up (gnus-summary-go-up-thread)
9286                   (gnus-summary-go-down-thread)))
9287       (setq n (1- n)))
9288     (gnus-summary-position-point)
9289     (when (/= 0 n)
9290       (gnus-message 7 "Can't go further"))
9291     n))
9292
9293 (defun gnus-summary-up-thread (n)
9294   "Go up thread N steps.
9295 If N is negative, go down instead.
9296 Returns the difference between N and how many steps down that were
9297 taken."
9298   (interactive "p")
9299   (gnus-summary-down-thread (- n)))
9300
9301 (defun gnus-summary-top-thread ()
9302   "Go to the top of the thread."
9303   (interactive)
9304   (while (gnus-summary-go-up-thread))
9305   (gnus-summary-article-number))
9306
9307 (defun gnus-summary-kill-thread (&optional unmark)
9308   "Mark articles under current thread as read.
9309 If the prefix argument is positive, remove any kinds of marks.
9310 If the prefix argument is negative, tick articles instead."
9311   (interactive "P")
9312   (when unmark
9313     (setq unmark (prefix-numeric-value unmark)))
9314   (let ((articles (gnus-summary-articles-in-thread)))
9315     (save-excursion
9316       ;; Expand the thread.
9317       (gnus-summary-show-thread)
9318       ;; Mark all the articles.
9319       (while articles
9320         (gnus-summary-goto-subject (car articles))
9321         (cond ((null unmark)
9322                (gnus-summary-mark-article-as-read gnus-killed-mark))
9323               ((> unmark 0)
9324                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9325               (t
9326                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9327         (setq articles (cdr articles))))
9328     ;; Hide killed subtrees.
9329     (and (null unmark)
9330          gnus-thread-hide-killed
9331          (gnus-summary-hide-thread))
9332     ;; If marked as read, go to next unread subject.
9333     (when (null unmark)
9334       ;; Go to next unread subject.
9335       (gnus-summary-next-subject 1 t)))
9336   (gnus-set-mode-line 'summary))
9337
9338 ;; Summary sorting commands
9339
9340 (defun gnus-summary-sort-by-number (&optional reverse)
9341   "Sort the summary buffer by article number.
9342 Argument REVERSE means reverse order."
9343   (interactive "P")
9344   (gnus-summary-sort 'number reverse))
9345
9346 (defun gnus-summary-sort-by-author (&optional reverse)
9347   "Sort the summary buffer by author name alphabetically.
9348 If `case-fold-search' is non-nil, case of letters is ignored.
9349 Argument REVERSE means reverse order."
9350   (interactive "P")
9351   (gnus-summary-sort 'author reverse))
9352
9353 (defun gnus-summary-sort-by-subject (&optional reverse)
9354   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9355 If `case-fold-search' is non-nil, case of letters is ignored.
9356 Argument REVERSE means reverse order."
9357   (interactive "P")
9358   (gnus-summary-sort 'subject reverse))
9359
9360 (defun gnus-summary-sort-by-date (&optional reverse)
9361   "Sort the summary buffer by date.
9362 Argument REVERSE means reverse order."
9363   (interactive "P")
9364   (gnus-summary-sort 'date reverse))
9365
9366 (defun gnus-summary-sort-by-score (&optional reverse)
9367   "Sort the summary buffer by score.
9368 Argument REVERSE means reverse order."
9369   (interactive "P")
9370   (gnus-summary-sort 'score reverse))
9371
9372 (defun gnus-summary-sort-by-lines (&optional reverse)
9373   "Sort the summary buffer by the number of lines.
9374 Argument REVERSE means reverse order."
9375   (interactive "P")
9376   (gnus-summary-sort 'lines reverse))
9377
9378 (defun gnus-summary-sort-by-chars (&optional reverse)
9379   "Sort the summary buffer by article length.
9380 Argument REVERSE means reverse order."
9381   (interactive "P")
9382   (gnus-summary-sort 'chars reverse))
9383
9384 (defun gnus-summary-sort-by-original (&optional reverse)
9385   "Sort the summary buffer using the default sorting method.
9386 Argument REVERSE means reverse order."
9387   (interactive "P")
9388   (let* ((buffer-read-only)
9389          (gnus-summary-prepare-hook nil))
9390     ;; We do the sorting by regenerating the threads.
9391     (gnus-summary-prepare)
9392     ;; Hide subthreads if needed.
9393     (when (and gnus-show-threads gnus-thread-hide-subtree)
9394       (gnus-summary-hide-all-threads))))
9395
9396 (defun gnus-summary-sort (predicate reverse)
9397   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9398   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9399          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9400          (gnus-thread-sort-functions
9401           (if (not reverse)
9402               thread
9403             `(lambda (t1 t2)
9404                (,thread t2 t1))))
9405          (gnus-sort-gathered-threads-function
9406           gnus-thread-sort-functions)
9407          (gnus-article-sort-functions
9408           (if (not reverse)
9409               article
9410             `(lambda (t1 t2)
9411                (,article t2 t1))))
9412          (buffer-read-only)
9413          (gnus-summary-prepare-hook nil))
9414     ;; We do the sorting by regenerating the threads.
9415     (gnus-summary-prepare)
9416     ;; Hide subthreads if needed.
9417     (when (and gnus-show-threads gnus-thread-hide-subtree)
9418       (gnus-summary-hide-all-threads))))
9419
9420 ;; Summary saving commands.
9421
9422 (defun gnus-summary-save-article (&optional n not-saved)
9423   "Save the current article using the default saver function.
9424 If N is a positive number, save the N next articles.
9425 If N is a negative number, save the N previous articles.
9426 If N is nil and any articles have been marked with the process mark,
9427 save those articles instead.
9428 The variable `gnus-default-article-saver' specifies the saver function."
9429   (interactive "P")
9430   (let* ((articles (gnus-summary-work-articles n))
9431          (save-buffer (save-excursion
9432                         (nnheader-set-temp-buffer " *Gnus Save*")))
9433          (num (length articles))
9434          header file)
9435     (dolist (article articles)
9436       (setq header (gnus-summary-article-header article))
9437       (if (not (vectorp header))
9438           ;; This is a pseudo-article.
9439           (if (assq 'name header)
9440               (gnus-copy-file (cdr (assq 'name header)))
9441             (gnus-message 1 "Article %d is unsaveable" article))
9442         ;; This is a real article.
9443         (save-window-excursion
9444           (gnus-summary-select-article t nil nil article))
9445         (save-excursion
9446           (set-buffer save-buffer)
9447           (erase-buffer)
9448           (insert-buffer-substring gnus-original-article-buffer))
9449         (setq file (gnus-article-save save-buffer file num))
9450         (gnus-summary-remove-process-mark article)
9451         (unless not-saved
9452           (gnus-summary-set-saved-mark article))))
9453     (gnus-kill-buffer save-buffer)
9454     (gnus-summary-position-point)
9455     (gnus-set-mode-line 'summary)
9456     n))
9457
9458 (defun gnus-summary-pipe-output (&optional arg)
9459   "Pipe the current article to a subprocess.
9460 If N is a positive number, pipe the N next articles.
9461 If N is a negative number, pipe the N previous articles.
9462 If N is nil and any articles have been marked with the process mark,
9463 pipe those articles instead."
9464   (interactive "P")
9465   (require 'gnus-art)
9466   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9467     (gnus-summary-save-article arg t))
9468   (gnus-configure-windows 'pipe))
9469
9470 (defun gnus-summary-save-article-mail (&optional arg)
9471   "Append the current article to an mail file.
9472 If N is a positive number, save the N next articles.
9473 If N is a negative number, save the N previous articles.
9474 If N is nil and any articles have been marked with the process mark,
9475 save those articles instead."
9476   (interactive "P")
9477   (require 'gnus-art)
9478   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9479     (gnus-summary-save-article arg)))
9480
9481 (defun gnus-summary-save-article-rmail (&optional arg)
9482   "Append the current article to an rmail file.
9483 If N is a positive number, save the N next articles.
9484 If N is a negative number, save the N previous articles.
9485 If N is nil and any articles have been marked with the process mark,
9486 save those articles instead."
9487   (interactive "P")
9488   (require 'gnus-art)
9489   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9490     (gnus-summary-save-article arg)))
9491
9492 (defun gnus-summary-save-article-file (&optional arg)
9493   "Append the current article to a file.
9494 If N is a positive number, save the N next articles.
9495 If N is a negative number, save the N previous articles.
9496 If N is nil and any articles have been marked with the process mark,
9497 save those articles instead."
9498   (interactive "P")
9499   (require 'gnus-art)
9500   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9501     (gnus-summary-save-article arg)))
9502
9503 (defun gnus-summary-write-article-file (&optional arg)
9504   "Write the current article to a file, deleting the previous file.
9505 If N is a positive number, save the N next articles.
9506 If N is a negative number, save the N previous articles.
9507 If N is nil and any articles have been marked with the process mark,
9508 save those articles instead."
9509   (interactive "P")
9510   (require 'gnus-art)
9511   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9512     (gnus-summary-save-article arg)))
9513
9514 (defun gnus-summary-save-article-body-file (&optional arg)
9515   "Append the current article body to a file.
9516 If N is a positive number, save the N next articles.
9517 If N is a negative number, save the N previous articles.
9518 If N is nil and any articles have been marked with the process mark,
9519 save those articles instead."
9520   (interactive "P")
9521   (require 'gnus-art)
9522   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9523     (gnus-summary-save-article arg)))
9524
9525 (defun gnus-summary-pipe-message (program)
9526   "Pipe the current article through PROGRAM."
9527   (interactive "sProgram: ")
9528   (gnus-summary-select-article)
9529   (let ((mail-header-separator ""))
9530     (gnus-eval-in-buffer-window gnus-article-buffer
9531       (save-restriction
9532         (widen)
9533         (let ((start (window-start))
9534               buffer-read-only)
9535           (message-pipe-buffer-body program)
9536           (set-window-start (get-buffer-window (current-buffer)) start))))))
9537
9538 (defun gnus-get-split-value (methods)
9539   "Return a value based on the split METHODS."
9540   (let (split-name method result match)
9541     (when methods
9542       (save-excursion
9543         (set-buffer gnus-original-article-buffer)
9544         (save-restriction
9545           (nnheader-narrow-to-headers)
9546           (while (and methods (not split-name))
9547             (goto-char (point-min))
9548             (setq method (pop methods))
9549             (setq match (car method))
9550             (when (cond
9551                    ((stringp match)
9552                     ;; Regular expression.
9553                     (ignore-errors
9554                       (re-search-forward match nil t)))
9555                    ((gnus-functionp match)
9556                     ;; Function.
9557                     (save-restriction
9558                       (widen)
9559                       (setq result (funcall match gnus-newsgroup-name))))
9560                    ((consp match)
9561                     ;; Form.
9562                     (save-restriction
9563                       (widen)
9564                       (setq result (eval match)))))
9565               (setq split-name (cdr method))
9566               (cond ((stringp result)
9567                      (push (expand-file-name
9568                             result gnus-article-save-directory)
9569                            split-name))
9570                     ((consp result)
9571                      (setq split-name (append result split-name)))))))))
9572     (nreverse split-name)))
9573
9574 (defun gnus-valid-move-group-p (group)
9575   (and (boundp group)
9576        (symbol-name group)
9577        (symbol-value group)
9578        (gnus-get-function (gnus-find-method-for-group
9579                            (symbol-name group)) 'request-accept-article t)))
9580
9581 (defun gnus-read-move-group-name (prompt default articles prefix)
9582   "Read a group name."
9583   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9584          (minibuffer-confirm-incomplete nil) ; XEmacs
9585          (prom
9586           (format "%s %s to:"
9587                   prompt
9588                   (if (> (length articles) 1)
9589                       (format "these %d articles" (length articles))
9590                     "this article")))
9591          (to-newsgroup
9592           (cond
9593            ((null split-name)
9594             (gnus-completing-read default prom
9595                                   gnus-active-hashtb
9596                                   'gnus-valid-move-group-p
9597                                   nil prefix
9598                                   'gnus-group-history))
9599            ((= 1 (length split-name))
9600             (gnus-completing-read (car split-name) prom
9601                                   gnus-active-hashtb
9602                                   'gnus-valid-move-group-p
9603                                   nil nil
9604                                   'gnus-group-history))
9605            (t
9606             (gnus-completing-read nil prom
9607                                   (mapcar (lambda (el) (list el))
9608                                           (nreverse split-name))
9609                                   nil nil nil
9610                                   'gnus-group-history))))
9611          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9612     (when to-newsgroup
9613       (if (or (string= to-newsgroup "")
9614               (string= to-newsgroup prefix))
9615           (setq to-newsgroup default))
9616       (unless to-newsgroup
9617         (error "No group name entered"))
9618       (or (gnus-active to-newsgroup)
9619           (gnus-activate-group to-newsgroup nil nil to-method)
9620           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9621                                      to-newsgroup))
9622               (or (and (gnus-request-create-group to-newsgroup to-method)
9623                        (gnus-activate-group
9624                         to-newsgroup nil nil to-method)
9625                        (gnus-subscribe-group to-newsgroup))
9626                   (error "Couldn't create group %s" to-newsgroup)))
9627           (error "No such group: %s" to-newsgroup)))
9628     to-newsgroup))
9629
9630 (defun gnus-summary-save-parts (type dir n &optional reverse)
9631   "Save parts matching TYPE to DIR.
9632 If REVERSE, save parts that do not match TYPE."
9633   (interactive
9634    (list (read-string "Save parts of type: "
9635                       (or (car gnus-summary-save-parts-type-history)
9636                           gnus-summary-save-parts-default-mime)
9637                       'gnus-summary-save-parts-type-history)
9638          (setq gnus-summary-save-parts-last-directory
9639                (read-file-name "Save to directory: "
9640                                gnus-summary-save-parts-last-directory
9641                                nil t))
9642          current-prefix-arg))
9643   (gnus-summary-iterate n
9644     (let ((gnus-display-mime-function nil)
9645           (gnus-inhibit-treatment t))
9646       (gnus-summary-select-article))
9647     (save-excursion
9648       (set-buffer gnus-article-buffer)
9649       (let ((handles (or gnus-article-mime-handles
9650                          (mm-dissect-buffer) (mm-uu-dissect))))
9651         (when handles
9652           (gnus-summary-save-parts-1 type dir handles reverse)
9653           (unless gnus-article-mime-handles ;; Don't destroy this case.
9654             (mm-destroy-parts handles)))))))
9655
9656 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9657   (if (stringp (car handle))
9658       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9659               (cdr handle))
9660     (when (if reverse
9661               (not (string-match type (mm-handle-media-type handle)))
9662             (string-match type (mm-handle-media-type handle)))
9663       (let ((file (expand-file-name
9664                    (file-name-nondirectory
9665                     (or
9666                      (mail-content-type-get
9667                       (mm-handle-disposition handle) 'filename)
9668                      (concat gnus-newsgroup-name
9669                              "." (number-to-string
9670                                   (cdr gnus-article-current)))))
9671                    dir)))
9672         (unless (file-exists-p file)
9673           (mm-save-part-to-file handle file))))))
9674
9675 ;; Summary extract commands
9676
9677 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9678   (let ((buffer-read-only nil)
9679         (article (gnus-summary-article-number))
9680         after-article b e)
9681     (unless (gnus-summary-goto-subject article)
9682       (error "No such article: %d" article))
9683     (gnus-summary-position-point)
9684     ;; If all commands are to be bunched up on one line, we collect
9685     ;; them here.
9686     (unless gnus-view-pseudos-separately
9687       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9688             files action)
9689         (while ps
9690           (setq action (cdr (assq 'action (car ps))))
9691           (setq files (list (cdr (assq 'name (car ps)))))
9692           (while (and ps (cdr ps)
9693                       (string= (or action "1")
9694                                (or (cdr (assq 'action (cadr ps))) "2")))
9695             (push (cdr (assq 'name (cadr ps))) files)
9696             (setcdr ps (cddr ps)))
9697           (when files
9698             (when (not (string-match "%s" action))
9699               (push " " files))
9700             (push " " files)
9701             (when (assq 'execute (car ps))
9702               (setcdr (assq 'execute (car ps))
9703                       (funcall (if (string-match "%s" action)
9704                                    'format 'concat)
9705                                action
9706                                (mapconcat
9707                                 (lambda (f)
9708                                   (if (equal f " ")
9709                                       f
9710                                     (mm-quote-arg f)))
9711                                 files " ")))))
9712           (setq ps (cdr ps)))))
9713     (if (and gnus-view-pseudos (not not-view))
9714         (while pslist
9715           (when (assq 'execute (car pslist))
9716             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9717                                   (eq gnus-view-pseudos 'not-confirm)))
9718           (setq pslist (cdr pslist)))
9719       (save-excursion
9720         (while pslist
9721           (setq after-article (or (cdr (assq 'article (car pslist)))
9722                                   (gnus-summary-article-number)))
9723           (gnus-summary-goto-subject after-article)
9724           (forward-line 1)
9725           (setq b (point))
9726           (insert "    " (file-name-nondirectory
9727                           (cdr (assq 'name (car pslist))))
9728                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9729           (setq e (point))
9730           (forward-line -1)             ; back to `b'
9731           (gnus-add-text-properties
9732            b (1- e) (list 'gnus-number gnus-reffed-article-number
9733                           gnus-mouse-face-prop gnus-mouse-face))
9734           (gnus-data-enter
9735            after-article gnus-reffed-article-number
9736            gnus-unread-mark b (car pslist) 0 (- e b))
9737           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9738           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9739           (setq pslist (cdr pslist)))))))
9740
9741 (defun gnus-pseudos< (p1 p2)
9742   (let ((c1 (cdr (assq 'action p1)))
9743         (c2 (cdr (assq 'action p2))))
9744     (and c1 c2 (string< c1 c2))))
9745
9746 (defun gnus-request-pseudo-article (props)
9747   (cond ((assq 'execute props)
9748          (gnus-execute-command (cdr (assq 'execute props)))))
9749   (let ((gnus-current-article (gnus-summary-article-number)))
9750     (gnus-run-hooks 'gnus-mark-article-hook)))
9751
9752 (defun gnus-execute-command (command &optional automatic)
9753   (save-excursion
9754     (gnus-article-setup-buffer)
9755     (set-buffer gnus-article-buffer)
9756     (setq buffer-read-only nil)
9757     (let ((command (if automatic command
9758                      (read-string "Command: " (cons command 0)))))
9759       (erase-buffer)
9760       (insert "$ " command "\n\n")
9761       (if gnus-view-pseudo-asynchronously
9762           (start-process "gnus-execute" (current-buffer) shell-file-name
9763                          shell-command-switch command)
9764         (call-process shell-file-name nil t nil
9765                       shell-command-switch command)))))
9766
9767 ;; Summary kill commands.
9768
9769 (defun gnus-summary-edit-global-kill (article)
9770   "Edit the \"global\" kill file."
9771   (interactive (list (gnus-summary-article-number)))
9772   (gnus-group-edit-global-kill article))
9773
9774 (defun gnus-summary-edit-local-kill ()
9775   "Edit a local kill file applied to the current newsgroup."
9776   (interactive)
9777   (setq gnus-current-headers (gnus-summary-article-header))
9778   (gnus-group-edit-local-kill
9779    (gnus-summary-article-number) gnus-newsgroup-name))
9780
9781 ;;; Header reading.
9782
9783 (defun gnus-read-header (id &optional header)
9784   "Read the headers of article ID and enter them into the Gnus system."
9785   (let ((group gnus-newsgroup-name)
9786         (gnus-override-method
9787          (or
9788           gnus-override-method
9789           (and (gnus-news-group-p gnus-newsgroup-name)
9790                (car (gnus-refer-article-methods)))))
9791         where)
9792     ;; First we check to see whether the header in question is already
9793     ;; fetched.
9794     (if (stringp id)
9795         ;; This is a Message-ID.
9796         (setq header (or header (gnus-id-to-header id)))
9797       ;; This is an article number.
9798       (setq header (or header (gnus-summary-article-header id))))
9799     (if (and header
9800              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9801         ;; We have found the header.
9802         header
9803       ;; If this is a sparse article, we have to nix out its
9804       ;; previous entry in the thread hashtb.
9805       (when (and header
9806                  (gnus-summary-article-sparse-p (mail-header-number header)))
9807         (let* ((parent (gnus-parent-id (mail-header-references header)))
9808                (thread (and parent (gnus-id-to-thread parent))))
9809           (when thread
9810             (delq (assq header thread) thread))))
9811       ;; We have to really fetch the header to this article.
9812       (save-excursion
9813         (set-buffer nntp-server-buffer)
9814         (when (setq where (gnus-request-head id group))
9815           (nnheader-fold-continuation-lines)
9816           (goto-char (point-max))
9817           (insert ".\n")
9818           (goto-char (point-min))
9819           (insert "211 ")
9820           (princ (cond
9821                   ((numberp id) id)
9822                   ((cdr where) (cdr where))
9823                   (header (mail-header-number header))
9824                   (t gnus-reffed-article-number))
9825                  (current-buffer))
9826           (insert " Article retrieved.\n"))
9827         (if (or (not where)
9828                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9829             ()                          ; Malformed head.
9830           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9831             (when (and (stringp id)
9832                        (not (string= (gnus-group-real-name group)
9833                                      (car where))))
9834               ;; If we fetched by Message-ID and the article came
9835               ;; from a different group, we fudge some bogus article
9836               ;; numbers for this article.
9837               (mail-header-set-number header gnus-reffed-article-number))
9838             (save-excursion
9839               (set-buffer gnus-summary-buffer)
9840               (decf gnus-reffed-article-number)
9841               (gnus-remove-header (mail-header-number header))
9842               (push header gnus-newsgroup-headers)
9843               (setq gnus-current-headers header)
9844               (push (mail-header-number header) gnus-newsgroup-limit)))
9845           header)))))
9846
9847 (defun gnus-remove-header (number)
9848   "Remove header NUMBER from `gnus-newsgroup-headers'."
9849   (if (and gnus-newsgroup-headers
9850            (= number (mail-header-number (car gnus-newsgroup-headers))))
9851       (pop gnus-newsgroup-headers)
9852     (let ((headers gnus-newsgroup-headers))
9853       (while (and (cdr headers)
9854                   (not (= number (mail-header-number (cadr headers)))))
9855         (pop headers))
9856       (when (cdr headers)
9857         (setcdr headers (cddr headers))))))
9858
9859 ;;;
9860 ;;; summary highlights
9861 ;;;
9862
9863 (defun gnus-highlight-selected-summary ()
9864   "Highlight selected article in summary buffer."
9865   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9866   (when gnus-summary-selected-face
9867     (save-excursion
9868       (let* ((beg (progn (beginning-of-line) (point)))
9869              (end (progn (end-of-line) (point)))
9870              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9871              (from (if (get-text-property beg gnus-mouse-face-prop)
9872                        beg
9873                      (or (next-single-property-change
9874                           beg gnus-mouse-face-prop nil end)
9875                          beg)))
9876              (to
9877               (if (= from end)
9878                   (- from 2)
9879                 (or (next-single-property-change
9880                      from gnus-mouse-face-prop nil end)
9881                     end))))
9882         ;; If no mouse-face prop on line we will have to = from = end,
9883         ;; so we highlight the entire line instead.
9884         (when (= (+ to 2) from)
9885           (setq from beg)
9886           (setq to end))
9887         (if gnus-newsgroup-selected-overlay
9888             ;; Move old overlay.
9889             (gnus-move-overlay
9890              gnus-newsgroup-selected-overlay from to (current-buffer))
9891           ;; Create new overlay.
9892           (gnus-overlay-put
9893            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9894            'face gnus-summary-selected-face))))))
9895
9896 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9897 (defun gnus-summary-highlight-line ()
9898   "Highlight current line according to `gnus-summary-highlight'."
9899   (let* ((list gnus-summary-highlight)
9900          (p (point))
9901          (end (progn (end-of-line) (point)))
9902          ;; now find out where the line starts and leave point there.
9903          (beg (progn (beginning-of-line) (point)))
9904          (article (gnus-summary-article-number))
9905          (score (or (cdr (assq (or article gnus-current-article)
9906                                gnus-newsgroup-scored))
9907                     gnus-summary-default-score 0))
9908          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9909          (inhibit-read-only t))
9910     ;; Eval the cars of the lists until we find a match.
9911     (let ((default gnus-summary-default-score))
9912       (while (and list
9913                   (not (eval (caar list))))
9914         (setq list (cdr list))))
9915     (let ((face (cdar list)))
9916       (unless (eq face (get-text-property beg 'face))
9917         (gnus-put-text-property-excluding-characters-with-faces
9918          beg end 'face
9919          (setq face (if (boundp face) (symbol-value face) face)))
9920         (when gnus-summary-highlight-line-function
9921           (funcall gnus-summary-highlight-line-function article face))))
9922     (goto-char p)))
9923
9924 (defun gnus-update-read-articles (group unread &optional compute)
9925   "Update the list of read articles in GROUP."
9926   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9927          (entry (gnus-gethash group gnus-newsrc-hashtb))
9928          (info (nth 2 entry))
9929          (prev 1)
9930          (unread (sort (copy-sequence unread) '<))
9931          read)
9932     (if (or (not info) (not active))
9933         ;; There is no info on this group if it was, in fact,
9934         ;; killed.  Gnus stores no information on killed groups, so
9935         ;; there's nothing to be done.
9936         ;; One could store the information somewhere temporarily,
9937         ;; perhaps...  Hmmm...
9938         ()
9939       ;; Remove any negative articles numbers.
9940       (while (and unread (< (car unread) 0))
9941         (setq unread (cdr unread)))
9942       ;; Remove any expired article numbers
9943       (while (and unread (< (car unread) (car active)))
9944         (setq unread (cdr unread)))
9945       ;; Compute the ranges of read articles by looking at the list of
9946       ;; unread articles.
9947       (while unread
9948         (when (/= (car unread) prev)
9949           (push (if (= prev (1- (car unread))) prev
9950                   (cons prev (1- (car unread))))
9951                 read))
9952         (setq prev (1+ (car unread)))
9953         (setq unread (cdr unread)))
9954       (when (<= prev (cdr active))
9955         (push (cons prev (cdr active)) read))
9956       (setq read (if (> (length read) 1) (nreverse read) read))
9957       (if compute
9958           read
9959         (save-excursion
9960           (let (setmarkundo)
9961             ;; Propagate the read marks to the backend.
9962             (when (gnus-check-backend-function 'request-set-mark group)
9963               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9964                     (add (gnus-remove-from-range read (gnus-info-read info))))
9965                 (when (or add del)
9966                   (unless (gnus-check-group group)
9967                     (error "Can't open server for %s" group))
9968                   (gnus-request-set-mark
9969                    group (delq nil (list (if add (list add 'add '(read)))
9970                                          (if del (list del 'del '(read))))))
9971                   (setq setmarkundo
9972                         `(gnus-request-set-mark
9973                           ,group
9974                           ',(delq nil (list
9975                                        (if del (list del 'add '(read)))
9976                                        (if add (list add 'del '(read))))))))))
9977             (set-buffer gnus-group-buffer)
9978             (gnus-undo-register
9979               `(progn
9980                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9981                  (gnus-info-set-read ',info ',(gnus-info-read info))
9982                  (gnus-get-unread-articles-in-group ',info
9983                                                     (gnus-active ,group))
9984                  (gnus-group-update-group ,group t)
9985                  ,setmarkundo))))
9986         ;; Enter this list into the group info.
9987         (gnus-info-set-read info read)
9988         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9989         (gnus-get-unread-articles-in-group info (gnus-active group))
9990         t))))
9991
9992 (defun gnus-offer-save-summaries ()
9993   "Offer to save all active summary buffers."
9994   (save-excursion
9995     (let ((buflist (buffer-list))
9996           buffers bufname)
9997       ;; Go through all buffers and find all summaries.
9998       (while buflist
9999         (and (setq bufname (buffer-name (car buflist)))
10000              (string-match "Summary" bufname)
10001              (save-excursion
10002                (set-buffer bufname)
10003                ;; We check that this is, indeed, a summary buffer.
10004                (and (eq major-mode 'gnus-summary-mode)
10005                     ;; Also make sure this isn't bogus.
10006                     gnus-newsgroup-prepared
10007                     ;; Also make sure that this isn't a dead summary buffer.
10008                     (not gnus-dead-summary-mode)))
10009              (push bufname buffers))
10010         (setq buflist (cdr buflist)))
10011       ;; Go through all these summary buffers and offer to save them.
10012       (when buffers
10013         (map-y-or-n-p
10014          "Update summary buffer %s? "
10015          (lambda (buf)
10016            (switch-to-buffer buf)
10017            (gnus-summary-exit))
10018          buffers)))))
10019
10020 (defun gnus-summary-setup-default-charset ()
10021   "Setup newsgroup default charset."
10022   (if (equal gnus-newsgroup-name "nndraft:drafts")
10023       (setq gnus-newsgroup-charset nil)
10024     (let* ((ignored-charsets
10025             (or gnus-newsgroup-ephemeral-ignored-charsets
10026                 (append
10027                  (and gnus-newsgroup-name
10028                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10029                  gnus-newsgroup-ignored-charsets))))
10030       (setq gnus-newsgroup-charset
10031             (or gnus-newsgroup-ephemeral-charset
10032                 (and gnus-newsgroup-name
10033                      (gnus-parameter-charset gnus-newsgroup-name))
10034                 gnus-default-charset))
10035       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10036            ignored-charsets))))
10037
10038 ;;;
10039 ;;; Mime Commands
10040 ;;;
10041
10042 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10043   "Display the current article buffer fully MIME-buttonized.
10044 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10045 treated as multipart/mixed."
10046   (interactive "P")
10047   (require 'gnus-art)
10048   (let ((gnus-unbuttonized-mime-types nil)
10049         (gnus-mime-display-multipart-as-mixed show-all-parts))
10050     (gnus-summary-show-article)))
10051
10052 (defun gnus-summary-repair-multipart (article)
10053   "Add a Content-Type header to a multipart article without one."
10054   (interactive (list (gnus-summary-article-number)))
10055   (gnus-with-article article
10056     (message-narrow-to-head)
10057     (message-remove-header "Mime-Version")
10058     (goto-char (point-max))
10059     (insert "Mime-Version: 1.0\n")
10060     (widen)
10061     (when (search-forward "\n--" nil t)
10062       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10063         (message-narrow-to-head)
10064         (message-remove-header "Content-Type")
10065         (goto-char (point-max))
10066         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10067                         separator))
10068         (widen))))
10069   (let (gnus-mark-article-hook)
10070     (gnus-summary-select-article t t nil article)))
10071
10072 (defun gnus-summary-toggle-display-buttonized ()
10073   "Toggle the buttonizing of the article buffer."
10074   (interactive)
10075   (require 'gnus-art)
10076   (if (setq gnus-inhibit-mime-unbuttonizing
10077             (not gnus-inhibit-mime-unbuttonizing))
10078       (let ((gnus-unbuttonized-mime-types nil))
10079         (gnus-summary-show-article))
10080     (gnus-summary-show-article)))
10081
10082 ;;;
10083 ;;; Generic summary marking commands
10084 ;;;
10085
10086 (defvar gnus-summary-marking-alist
10087   '((read gnus-del-mark "d")
10088     (unread gnus-unread-mark "u")
10089     (ticked gnus-ticked-mark "!")
10090     (dormant gnus-dormant-mark "?")
10091     (expirable gnus-expirable-mark "e"))
10092   "An alist of names/marks/keystrokes.")
10093
10094 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10095 (defvar gnus-summary-mark-map)
10096
10097 (defun gnus-summary-make-all-marking-commands ()
10098   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10099   (dolist (elem gnus-summary-marking-alist)
10100     (apply 'gnus-summary-make-marking-command elem)))
10101
10102 (defun gnus-summary-make-marking-command (name mark keystroke)
10103   (let ((map (make-sparse-keymap)))
10104     (define-key gnus-summary-generic-mark-map keystroke map)
10105     (dolist (lway `((next "next" next nil "n")
10106                     (next-unread "next unread" next t "N")
10107                     (prev "previous" prev nil "p")
10108                     (prev-unread "previous unread" prev t "P")
10109                     (nomove "" nil nil ,keystroke)))
10110       (let ((func (gnus-summary-make-marking-command-1
10111                    mark (car lway) lway name)))
10112         (setq func (eval func))
10113         (define-key map (nth 4 lway) func)))))
10114
10115 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10116   `(defun ,(intern
10117             (format "gnus-summary-put-mark-as-%s%s"
10118                     name (if (eq way 'nomove)
10119                              ""
10120                            (concat "-" (symbol-name way)))))
10121      (n)
10122      ,(format
10123        "Mark the current article as %s%s.
10124 If N, the prefix, then repeat N times.
10125 If N is negative, move in reverse order.
10126 The difference between N and the actual number of articles marked is
10127 returned."
10128        name (cadr lway))
10129      (interactive "p")
10130      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10131
10132 (defun gnus-summary-generic-mark (n mark move unread)
10133   "Mark N articles with MARK."
10134   (unless (eq major-mode 'gnus-summary-mode)
10135     (error "This command can only be used in the summary buffer"))
10136   (gnus-summary-show-thread)
10137   (let ((nummove
10138          (cond
10139           ((eq move 'next) 1)
10140           ((eq move 'prev) -1)
10141           (t 0))))
10142     (if (zerop nummove)
10143         (setq n 1)
10144       (when (< n 0)
10145         (setq n (abs n)
10146               nummove (* -1 nummove))))
10147     (while (and (> n 0)
10148                 (gnus-summary-mark-article nil mark)
10149                 (zerop (gnus-summary-next-subject nummove unread t)))
10150       (setq n (1- n)))
10151     (when (/= 0 n)
10152       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10153     (gnus-summary-recenter)
10154     (gnus-summary-position-point)
10155     (gnus-set-mode-line 'summary)
10156     n))
10157
10158 (defun gnus-summary-insert-articles (articles)
10159   (when (setq articles
10160               (gnus-set-difference articles
10161                                    (mapcar (lambda (h) (mail-header-number h))
10162                                            gnus-newsgroup-headers)))
10163     (setq gnus-newsgroup-headers 
10164           (merge 'list
10165                  gnus-newsgroup-headers
10166                  (gnus-fetch-headers articles)
10167                  'gnus-article-sort-by-number))
10168     ;; Suppress duplicates?
10169     (when gnus-suppress-duplicates
10170       (gnus-dup-suppress-articles))
10171     
10172     ;; We might want to build some more threads first.
10173     (when (and gnus-fetch-old-headers
10174                (eq gnus-headers-retrieved-by 'nov))
10175       (if (eq gnus-fetch-old-headers 'invisible)
10176         (gnus-build-all-threads)
10177         (gnus-build-old-threads)))
10178     ;; Let the Gnus agent mark articles as read.
10179     (when gnus-agent
10180       (gnus-agent-get-undownloaded-list))
10181     ;; Remove list identifiers from subject
10182     (when gnus-list-identifiers
10183       (gnus-summary-remove-list-identifiers))
10184     ;; First and last article in this newsgroup.
10185     (when gnus-newsgroup-headers
10186       (setq gnus-newsgroup-begin
10187             (mail-header-number (car gnus-newsgroup-headers))
10188             gnus-newsgroup-end
10189             (mail-header-number
10190              (gnus-last-element gnus-newsgroup-headers))))
10191     (when gnus-use-scoring
10192       (gnus-possibly-score-headers))))
10193
10194 (defun gnus-summary-insert-old-articles (&optional all)
10195   "Insert all old articles in this group.
10196 If ALL is non-nil, already read articles become readable.
10197 If ALL is a number, fetch this number of articles."
10198   (interactive "P")
10199   (prog1
10200       (let ((old (mapcar 'car gnus-newsgroup-data))
10201             (i (car gnus-newsgroup-active))
10202             older len)
10203         (while (<= i (cdr gnus-newsgroup-active))
10204           (or (memq i old) (push i older))
10205           (incf i))
10206         (setq len (length older))
10207         (cond 
10208          ((null older) nil)
10209          ((numberp all) 
10210           (if (< all len)
10211               (setq older (subseq older 0 all))))
10212          (all nil)
10213          (t
10214           (if (and (numberp gnus-large-newsgroup)
10215                    (> len gnus-large-newsgroup))
10216               (let ((input
10217                      (read-string
10218                       (format
10219                        "How many articles from %s (default %d): "
10220                        (gnus-limit-string 
10221                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10222                        len))))
10223                 (unless (string-match "^[ \t]*$" input) 
10224                   (setq all (string-to-number input))
10225                   (if (< all len)
10226                       (setq older (subseq older 0 all))))))))
10227         (if (not older)
10228             (message "No old news.")
10229           (gnus-summary-insert-articles older)
10230           (gnus-summary-limit (gnus-union older old))))
10231     (gnus-summary-position-point)))
10232
10233 (defun gnus-summary-insert-new-articles ()
10234   "Insert all new articles in this group."
10235   (interactive)
10236   (prog1
10237       (let ((old (mapcar 'car gnus-newsgroup-data))
10238             (old-active gnus-newsgroup-active)
10239             (nnmail-fetched-sources (list t))
10240             i new)
10241         (setq gnus-newsgroup-active 
10242               (gnus-activate-group gnus-newsgroup-name 'scan))
10243         (setq i (1+ (cdr old-active)))
10244         (while (<= i (cdr gnus-newsgroup-active))
10245           (push i new)
10246           (incf i))
10247         (if (not new)
10248             (message "No gnus is bad news.")
10249           (setq new (nreverse new))
10250           (gnus-summary-insert-articles new)
10251           (setq gnus-newsgroup-unreads
10252                 (append gnus-newsgroup-unreads new))
10253           (gnus-summary-limit (gnus-union old new))))
10254     (gnus-summary-position-point)))
10255
10256 (gnus-summary-make-all-marking-commands)
10257
10258 (gnus-ems-redefine)
10259
10260 (provide 'gnus-sum)
10261
10262 (run-hooks 'gnus-sum-load-hook)
10263
10264 ;;; gnus-sum.el ends here