6adbb3f26a3cdebbc1a2ec5c102274912aa68dc4
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
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 (require 'nnoo)
40
41 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
42 (autoload 'gnus-cache-write-active "gnus-cache")
43 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
44 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
45 (autoload 'mm-uu-dissect "mm-uu")
46 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
47   "Deuglify broken Outlook (Express) articles and redisplay."
48   t)
49 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
50 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
51 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
52
53 (defcustom gnus-kill-summary-on-exit t
54   "*If non-nil, kill the summary buffer when you exit from it.
55 If nil, the summary will become a \"*Dead Summary*\" buffer, and
56 it will be killed sometime later."
57   :group 'gnus-summary-exit
58   :type 'boolean)
59
60 (defcustom gnus-fetch-old-headers nil
61   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
62 If an unread article in the group refers to an older, already read (or
63 just marked as read) article, the old article will not normally be
64 displayed in the Summary buffer.  If this variable is non-nil, Gnus
65 will attempt to grab the headers to the old articles, and thereby
66 build complete threads.  If it has the value `some', only enough
67 headers to connect otherwise loose threads will be displayed.  This
68 variable can also be a number.  In that case, no more than that number
69 of old headers will be fetched.  If it has the value `invisible', all
70 old headers will be fetched, but none will be displayed.
71
72 The server has to support NOV for any of this to work."
73   :group 'gnus-thread
74   :type '(choice (const :tag "off" nil)
75                  (const some)
76                  number
77                  (sexp :menu-tag "other" t)))
78
79 (defcustom gnus-refer-thread-limit 200
80   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
81 If t, fetch all the available old headers."
82   :group 'gnus-thread
83   :type '(choice number
84                  (sexp :menu-tag "other" t)))
85
86 (defcustom gnus-summary-make-false-root 'adopt
87   "*nil means that Gnus won't gather loose threads.
88 If the root of a thread has expired or been read in a previous
89 session, the information necessary to build a complete thread has been
90 lost.  Instead of having many small sub-threads from this original thread
91 scattered all over the summary buffer, Gnus can gather them.
92
93 If non-nil, Gnus will try to gather all loose sub-threads from an
94 original thread into one large thread.
95
96 If this variable is non-nil, it should be one of `none', `adopt',
97 `dummy' or `empty'.
98
99 If this variable is `none', Gnus will not make a false root, but just
100 present the sub-threads after another.
101 If this variable is `dummy', Gnus will create a dummy root that will
102 have all the sub-threads as children.
103 If this variable is `adopt', Gnus will make one of the \"children\"
104 the parent and mark all the step-children as such.
105 If this variable is `empty', the \"children\" are printed with empty
106 subject fields.  (Or rather, they will be printed with a string
107 given by the `gnus-summary-same-subject' variable.)"
108   :group 'gnus-thread
109   :type '(choice (const :tag "off" nil)
110                  (const none)
111                  (const dummy)
112                  (const adopt)
113                  (const empty)))
114
115 (defcustom gnus-summary-make-false-root-always nil
116   "Always make a false dummy root."
117   :group 'gnus-thread
118   :type 'boolean)
119
120 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
121   "*A regexp to match subjects to be excluded from loose thread gathering.
122 As loose thread gathering is done on subjects only, that means that
123 there can be many false gatherings performed.  By rooting out certain
124 common subjects, gathering might become saner."
125   :group 'gnus-thread
126   :type 'regexp)
127
128 (defcustom gnus-summary-gather-subject-limit nil
129   "*Maximum length of subject comparisons when gathering loose threads.
130 Use nil to compare full subjects.  Setting this variable to a low
131 number will help gather threads that have been corrupted by
132 newsreaders chopping off subject lines, but it might also mean that
133 unrelated articles that have subject that happen to begin with the
134 same few characters will be incorrectly gathered.
135
136 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
137 comparing subjects."
138   :group 'gnus-thread
139   :type '(choice (const :tag "off" nil)
140                  (const fuzzy)
141                  (sexp :menu-tag "on" t)))
142
143 (defcustom gnus-simplify-subject-functions nil
144   "List of functions taking a string argument that simplify subjects.
145 The functions are applied recursively.
146
147 Useful functions to put in this list include:
148 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
149 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
150   :group 'gnus-thread
151   :type '(repeat function))
152
153 (defcustom gnus-simplify-ignored-prefixes nil
154   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
155   :group 'gnus-thread
156   :type '(choice (const :tag "off" nil)
157                  regexp))
158
159 (defcustom gnus-build-sparse-threads nil
160   "*If non-nil, fill in the gaps in threads.
161 If `some', only fill in the gaps that are needed to tie loose threads
162 together.  If `more', fill in all leaf nodes that Gnus can find.  If
163 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
164   :group 'gnus-thread
165   :type '(choice (const :tag "off" nil)
166                  (const some)
167                  (const more)
168                  (sexp :menu-tag "all" t)))
169
170 (defcustom gnus-summary-thread-gathering-function
171   'gnus-gather-threads-by-subject
172   "*Function used for gathering loose threads.
173 There are two pre-defined functions: `gnus-gather-threads-by-subject',
174 which only takes Subjects into consideration; and
175 `gnus-gather-threads-by-references', which compared the References
176 headers of the articles to find matches."
177   :group 'gnus-thread
178   :type '(radio (function-item gnus-gather-threads-by-subject)
179                 (function-item gnus-gather-threads-by-references)
180                 (function :tag "other")))
181
182 (defcustom gnus-summary-same-subject ""
183   "*String indicating that the current article has the same subject as the previous.
184 This variable will only be used if the value of
185 `gnus-summary-make-false-root' is `empty'."
186   :group 'gnus-summary-format
187   :type 'string)
188
189 (defcustom gnus-summary-goto-unread t
190   "*If t, many commands will go to the next unread article.
191 This applies to marking commands as well as other commands that
192 \"naturally\" select the next article, like, for instance, `SPC' at
193 the end of an article.
194
195 If nil, the marking commands do NOT go to the next unread article
196 \(they go to the next article instead).  If `never', commands that
197 usually go to the next unread article, will go to the next article,
198 whether it is read or not."
199   :group 'gnus-summary-marks
200   :link '(custom-manual "(gnus)Setting Marks")
201   :type '(choice (const :tag "off" nil)
202                  (const never)
203                  (sexp :menu-tag "on" t)))
204
205 (defcustom gnus-summary-default-score 0
206   "*Default article score level.
207 All scores generated by the score files will be added to this score.
208 If this variable is nil, scoring will be disabled."
209   :group 'gnus-score-default
210   :type '(choice (const :tag "disable")
211                  integer))
212
213 (defcustom gnus-summary-default-high-score 0
214   "*Default threshold for a high scored article.
215 An article will be highlighted as high scored if its score is greater
216 than this score."
217   :group 'gnus-score-default
218   :type 'integer)
219
220 (defcustom gnus-summary-default-low-score 0
221   "*Default threshold for a low scored article.
222 An article will be highlighted as low scored if its score is smaller
223 than this score."
224   :group 'gnus-score-default
225   :type 'integer)
226
227 (defcustom gnus-summary-zcore-fuzz 0
228   "*Fuzziness factor for the zcore in the summary buffer.
229 Articles with scores closer than this to `gnus-summary-default-score'
230 will not be marked."
231   :group 'gnus-summary-format
232   :type 'integer)
233
234 (defcustom gnus-simplify-subject-fuzzy-regexp nil
235   "*Strings to be removed when doing fuzzy matches.
236 This can either be a regular expression or list of regular expressions
237 that will be removed from subject strings if fuzzy subject
238 simplification is selected."
239   :group 'gnus-thread
240   :type '(repeat regexp))
241
242 (defcustom gnus-show-threads t
243   "*If non-nil, display threads in summary mode."
244   :group 'gnus-thread
245   :type 'boolean)
246
247 (defcustom gnus-thread-hide-subtree nil
248   "*If non-nil, hide all threads initially.
249 This can be a predicate specifier which says which threads to hide.
250 If threads are hidden, you have to run the command
251 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
252 to expose hidden threads."
253   :group 'gnus-thread
254   :type 'boolean)
255
256 (defcustom gnus-thread-hide-killed t
257   "*If non-nil, hide killed threads automatically."
258   :group 'gnus-thread
259   :type 'boolean)
260
261 (defcustom gnus-thread-ignore-subject t
262   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
263 If nil, articles that have different subjects from their parents will
264 start separate threads."
265   :group 'gnus-thread
266   :type 'boolean)
267
268 (defcustom gnus-thread-operation-ignore-subject t
269   "*If non-nil, subjects will be ignored when doing thread commands.
270 This affects commands like `gnus-summary-kill-thread' and
271 `gnus-summary-lower-thread'.
272
273 If this variable is nil, articles in the same thread with different
274 subjects will not be included in the operation in question.  If this
275 variable is `fuzzy', only articles that have subjects that are fuzzily
276 equal will be included."
277   :group 'gnus-thread
278   :type '(choice (const :tag "off" nil)
279                  (const fuzzy)
280                  (sexp :tag "on" t)))
281
282 (defcustom gnus-thread-indent-level 4
283   "*Number that says how much each sub-thread should be indented."
284   :group 'gnus-thread
285   :type 'integer)
286
287 (defcustom gnus-auto-extend-newsgroup t
288   "*If non-nil, extend newsgroup forward and backward when requested."
289   :group 'gnus-summary-choose
290   :type 'boolean)
291
292 (defcustom gnus-auto-select-first t
293   "*If non-nil, select the article under point.
294 Which article this is is controlled by the `gnus-auto-select-subject'
295 variable.
296
297 If you want to prevent automatic selection of articles in some
298 newsgroups, set the variable to nil in `gnus-select-group-hook'."
299   :group 'gnus-group-select
300   :type '(choice (const :tag "none" nil)
301                  (sexp :menu-tag "first" t)))
302
303 (defcustom gnus-auto-select-subject 'unread
304   "*Says what subject to place under point when entering a group.
305
306 This variable can either be the symbols `first' (place point on the
307 first subject), `unread' (place point on the subject line of the first
308 unread article), `best' (place point on the subject line of the
309 higest-scored article), `unseen' (place point on the subject line of
310 the first unseen article), 'unseen-or-unread' (place point on the subject
311 line of the first unseen article or, if all article have been seen, on the
312 subject line of the first unread article), or a function to be called to
313 place point on some subject line."
314   :group 'gnus-group-select
315   :type '(choice (const best)
316                  (const unread)
317                  (const first)
318                  (const unseen)
319                  (const unseen-or-unread)))
320
321 (defcustom gnus-auto-select-next t
322   "*If non-nil, offer to go to the next group from the end of the previous.
323 If the value is t and the next newsgroup is empty, Gnus will exit
324 summary mode and go back to group mode.  If the value is neither nil
325 nor t, Gnus will select the following unread newsgroup.  In
326 particular, if the value is the symbol `quietly', the next unread
327 newsgroup will be selected without any confirmation, and if it is
328 `almost-quietly', the next group will be selected without any
329 confirmation if you are located on the last article in the group.
330 Finally, if this variable is `slightly-quietly', the `Z n' command
331 will go to the next group without confirmation."
332   :group 'gnus-summary-maneuvering
333   :type '(choice (const :tag "off" nil)
334                  (const quietly)
335                  (const almost-quietly)
336                  (const slightly-quietly)
337                  (sexp :menu-tag "on" t)))
338
339 (defcustom gnus-auto-select-same nil
340   "*If non-nil, select the next article with the same subject.
341 If there are no more articles with the same subject, go to
342 the first unread article."
343   :group 'gnus-summary-maneuvering
344   :type 'boolean)
345
346 (defcustom gnus-summary-check-current nil
347   "*If non-nil, consider the current article when moving.
348 The \"unread\" movement commands will stay on the same line if the
349 current article is unread."
350   :group 'gnus-summary-maneuvering
351   :type 'boolean)
352
353 (defcustom gnus-auto-center-summary t
354   "*If non-nil, always center the current summary buffer.
355 In particular, if `vertical' do only vertical recentering.  If non-nil
356 and non-`vertical', do both horizontal and vertical recentering."
357   :group 'gnus-summary-maneuvering
358   :type '(choice (const :tag "none" nil)
359                  (const vertical)
360                  (integer :tag "height")
361                  (sexp :menu-tag "both" t)))
362
363 (defcustom gnus-show-all-headers nil
364   "*If non-nil, don't hide any headers."
365   :group 'gnus-article-hiding
366   :group 'gnus-article-headers
367   :type 'boolean)
368
369 (defcustom gnus-summary-ignore-duplicates nil
370   "*If non-nil, ignore articles with identical Message-ID headers."
371   :group 'gnus-summary
372   :type 'boolean)
373
374 (defcustom gnus-single-article-buffer t
375   "*If non-nil, display all articles in the same buffer.
376 If nil, each group will get its own article buffer."
377   :group 'gnus-article-various
378   :type 'boolean)
379
380 (defcustom gnus-break-pages t
381   "*If non-nil, do page breaking on articles.
382 The page delimiter is specified by the `gnus-page-delimiter'
383 variable."
384   :group 'gnus-article-various
385   :type 'boolean)
386
387 (defcustom gnus-move-split-methods nil
388   "*Variable used to suggest where articles are to be moved to.
389 It uses the same syntax as the `gnus-split-methods' variable.
390 However, whereas `gnus-split-methods' specifies file names as targets,
391 this variable specifies group names."
392   :group 'gnus-summary-mail
393   :type '(repeat (choice (list :value (fun) function)
394                          (cons :value ("" "") regexp (repeat string))
395                          (sexp :value nil))))
396
397 (defcustom gnus-unread-mark ?           ;Whitespace
398   "*Mark used for unread articles."
399   :group 'gnus-summary-marks
400   :type 'character)
401
402 (defcustom gnus-ticked-mark ?!
403   "*Mark used for ticked articles."
404   :group 'gnus-summary-marks
405   :type 'character)
406
407 (defcustom gnus-dormant-mark ??
408   "*Mark used for dormant articles."
409   :group 'gnus-summary-marks
410   :type 'character)
411
412 (defcustom gnus-del-mark ?r
413   "*Mark used for del'd articles."
414   :group 'gnus-summary-marks
415   :type 'character)
416
417 (defcustom gnus-read-mark ?R
418   "*Mark used for read articles."
419   :group 'gnus-summary-marks
420   :type 'character)
421
422 (defcustom gnus-expirable-mark ?E
423   "*Mark used for expirable articles."
424   :group 'gnus-summary-marks
425   :type 'character)
426
427 (defcustom gnus-killed-mark ?K
428   "*Mark used for killed articles."
429   :group 'gnus-summary-marks
430   :type 'character)
431
432 (defcustom gnus-spam-mark ?H
433   "*Mark used for spam articles."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-souped-mark ?F
438   "*Mark used for souped articles."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-kill-file-mark ?X
443   "*Mark used for articles killed by kill files."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-low-score-mark ?Y
448   "*Mark used for articles with a low score."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-catchup-mark ?C
453   "*Mark used for articles that are caught up."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-replied-mark ?A
458   "*Mark used for articles that have been replied to."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-forwarded-mark ?F
463   "*Mark used for articles that have been forwarded."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-recent-mark ?N
468   "*Mark used for articles that are recent."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-cached-mark ?*
473   "*Mark used for articles that are in the cache."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-saved-mark ?S
478   "*Mark used for articles that have been saved."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-unseen-mark ?.
483   "*Mark used for articles that haven't been seen."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-no-mark ?               ;Whitespace
488   "*Mark used for articles that have no other secondary mark."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-ancient-mark ?O
493   "*Mark used for ancient articles."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-sparse-mark ?Q
498   "*Mark used for sparsely reffed articles."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-canceled-mark ?G
503   "*Mark used for canceled articles."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-duplicate-mark ?M
508   "*Mark used for duplicate articles."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-undownloaded-mark ?-
513   "*Mark used for articles that weren't downloaded."
514   :group 'gnus-summary-marks
515   :type 'character)
516
517 (defcustom gnus-downloaded-mark ?+
518   "*Mark used for articles that were downloaded."
519   :group 'gnus-summary-marks
520   :type 'character)
521
522 (defcustom gnus-downloadable-mark ?%
523   "*Mark used for articles that are to be downloaded."
524   :group 'gnus-summary-marks
525   :type 'character)
526
527 (defcustom gnus-unsendable-mark ?=
528   "*Mark used for articles that won't be sent."
529   :group 'gnus-summary-marks
530   :type 'character)
531
532 (defcustom gnus-score-over-mark ?+
533   "*Score mark used for articles with high scores."
534   :group 'gnus-summary-marks
535   :type 'character)
536
537 (defcustom gnus-score-below-mark ?-
538   "*Score mark used for articles with low scores."
539   :group 'gnus-summary-marks
540   :type 'character)
541
542 (defcustom gnus-empty-thread-mark ?     ;Whitespace
543   "*There is no thread under the article."
544   :group 'gnus-summary-marks
545   :type 'character)
546
547 (defcustom gnus-not-empty-thread-mark ?=
548   "*There is a thread under the article."
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-view-pseudo-asynchronously nil
553   "*If non-nil, Gnus will view pseudo-articles asynchronously."
554   :group 'gnus-extract-view
555   :type 'boolean)
556
557 (defcustom gnus-auto-expirable-marks
558   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
559         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
560         gnus-souped-mark gnus-duplicate-mark)
561   "*The list of marks converted into expiration if a group is auto-expirable."
562   :version "21.1"
563   :group 'gnus-summary
564   :type '(repeat character))
565
566 (defcustom gnus-inhibit-user-auto-expire t
567   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
568   :version "21.1"
569   :group 'gnus-summary
570   :type 'boolean)
571
572 (defcustom gnus-view-pseudos nil
573   "*If `automatic', pseudo-articles will be viewed automatically.
574 If `not-confirm', pseudos will be viewed automatically, and the user
575 will not be asked to confirm the command."
576   :group 'gnus-extract-view
577   :type '(choice (const :tag "off" nil)
578                  (const automatic)
579                  (const not-confirm)))
580
581 (defcustom gnus-view-pseudos-separately t
582   "*If non-nil, one pseudo-article will be created for each file to be viewed.
583 If nil, all files that use the same viewing command will be given as a
584 list of parameters to that command."
585   :group 'gnus-extract-view
586   :type 'boolean)
587
588 (defcustom gnus-insert-pseudo-articles t
589   "*If non-nil, insert pseudo-articles when decoding articles."
590   :group 'gnus-extract-view
591   :type 'boolean)
592
593 (defcustom gnus-summary-dummy-line-format
594   "   %(:                             :%) %S\n"
595   "*The format specification for the dummy roots in the summary buffer.
596 It works along the same lines as a normal formatting string,
597 with some simple extensions.
598
599 %S  The subject
600
601 General format specifiers can also be used.
602 See `(gnus)Formatting Variables'."
603   :link '(custom-manual "(gnus)Formatting Variables")
604   :group 'gnus-threading
605   :type 'string)
606
607 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
608   "*The format specification for the summary mode line.
609 It works along the same lines as a normal formatting string,
610 with some simple extensions:
611
612 %G  Group name
613 %p  Unprefixed group name
614 %A  Current article number
615 %z  Current article score
616 %V  Gnus version
617 %U  Number of unread articles in the group
618 %e  Number of unselected articles in the group
619 %Z  A string with unread/unselected article counts
620 %g  Shortish group name
621 %S  Subject of the current article
622 %u  User-defined spec
623 %s  Current score file name
624 %d  Number of dormant articles
625 %r  Number of articles that have been marked as read in this session
626 %E  Number of articles expunged by the score files"
627   :group 'gnus-summary-format
628   :type 'string)
629
630 (defcustom gnus-list-identifiers nil
631   "Regexp that matches list identifiers to be removed from subject.
632 This can also be a list of regexps."
633   :version "21.1"
634   :group 'gnus-summary-format
635   :group 'gnus-article-hiding
636   :type '(choice (const :tag "none" nil)
637                  (regexp :value ".*")
638                  (repeat :value (".*") regexp)))
639
640 (defcustom gnus-summary-mark-below 0
641   "*Mark all articles with a score below this variable as read.
642 This variable is local to each summary buffer and usually set by the
643 score file."
644   :group 'gnus-score-default
645   :type 'integer)
646
647 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
648   "*List of functions used for sorting articles in the summary buffer.
649
650 Each function takes two articles and returns non-nil if the first
651 article should be sorted before the other.  If you use more than one
652 function, the primary sort function should be the last.  You should
653 probably always include `gnus-article-sort-by-number' in the list of
654 sorting functions -- preferably first.  Also note that sorting by date
655 is often much slower than sorting by number, and the sorting order is
656 very similar.  (Sorting by date means sorting by the time the message
657 was sent, sorting by number means sorting by arrival time.)
658
659 Ready-made functions include `gnus-article-sort-by-number',
660 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
661 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
662 and `gnus-article-sort-by-score'.
663
664 When threading is turned on, the variable `gnus-thread-sort-functions'
665 controls how articles are sorted."
666   :group 'gnus-summary-sort
667   :type '(repeat (choice (function-item gnus-article-sort-by-number)
668                          (function-item gnus-article-sort-by-author)
669                          (function-item gnus-article-sort-by-subject)
670                          (function-item gnus-article-sort-by-date)
671                          (function-item gnus-article-sort-by-score)
672                          (function-item gnus-article-sort-by-random)
673                          (function :tag "other"))))
674
675 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
676   "*List of functions used for sorting threads in the summary buffer.
677 By default, threads are sorted by article number.
678
679 Each function takes two threads and returns non-nil if the first
680 thread should be sorted before the other.  If you use more than one
681 function, the primary sort function should be the last.  You should
682 probably always include `gnus-thread-sort-by-number' in the list of
683 sorting functions -- preferably first.  Also note that sorting by date
684 is often much slower than sorting by number, and the sorting order is
685 very similar.  (Sorting by date means sorting by the time the message
686 was sent, sorting by number means sorting by arrival time.)
687
688 Ready-made functions include `gnus-thread-sort-by-number',
689 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
690 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
691 `gnus-thread-sort-by-most-recent-number',
692 `gnus-thread-sort-by-most-recent-date',
693 `gnus-thread-sort-by-random', and
694 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
695
696 When threading is turned off, the variable
697 `gnus-article-sort-functions' controls how articles are sorted."
698   :group 'gnus-summary-sort
699   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
700                          (function-item gnus-thread-sort-by-author)
701                          (function-item gnus-thread-sort-by-subject)
702                          (function-item gnus-thread-sort-by-date)
703                          (function-item gnus-thread-sort-by-score)
704                          (function-item gnus-thread-sort-by-total-score)
705                          (function-item gnus-thread-sort-by-random)
706                          (function :tag "other"))))
707
708 (defcustom gnus-thread-score-function '+
709   "*Function used for calculating the total score of a thread.
710
711 The function is called with the scores of the article and each
712 subthread and should then return the score of the thread.
713
714 Some functions you can use are `+', `max', or `min'."
715   :group 'gnus-summary-sort
716   :type 'function)
717
718 (defcustom gnus-summary-expunge-below nil
719   "All articles that have a score less than this variable will be expunged.
720 This variable is local to the summary buffers."
721   :group 'gnus-score-default
722   :type '(choice (const :tag "off" nil)
723                  integer))
724
725 (defcustom gnus-thread-expunge-below nil
726   "All threads that have a total score less than this variable will be expunged.
727 See `gnus-thread-score-function' for en explanation of what a
728 \"thread score\" is.
729
730 This variable is local to the summary buffers."
731   :group 'gnus-threading
732   :group 'gnus-score-default
733   :type '(choice (const :tag "off" nil)
734                  integer))
735
736 (defcustom gnus-summary-mode-hook nil
737   "*A hook for Gnus summary mode.
738 This hook is run before any variables are set in the summary buffer."
739   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
740   :group 'gnus-summary-various
741   :type 'hook)
742
743 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
744 (when (featurep 'xemacs)
745   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
746   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
747   (add-hook 'gnus-summary-mode-hook
748             'gnus-xmas-switch-horizontal-scrollbar-off))
749
750 (defcustom gnus-summary-menu-hook nil
751   "*Hook run after the creation of the summary mode menu."
752   :group 'gnus-summary-visual
753   :type 'hook)
754
755 (defcustom gnus-summary-exit-hook nil
756   "*A hook called on exit from the summary buffer.
757 It will be called with point in the group buffer."
758   :group 'gnus-summary-exit
759   :type 'hook)
760
761 (defcustom gnus-summary-prepare-hook nil
762   "*A hook called after the summary buffer has been generated.
763 If you want to modify the summary buffer, you can use this hook."
764   :group 'gnus-summary-various
765   :type 'hook)
766
767 (defcustom gnus-summary-prepared-hook nil
768   "*A hook called as the last thing after the summary buffer has been generated."
769   :group 'gnus-summary-various
770   :type 'hook)
771
772 (defcustom gnus-summary-generate-hook nil
773   "*A hook run just before generating the summary buffer.
774 This hook is commonly used to customize threading variables and the
775 like."
776   :group 'gnus-summary-various
777   :type 'hook)
778
779 (defcustom gnus-select-group-hook nil
780   "*A hook called when a newsgroup is selected.
781
782 If you'd like to simplify subjects like the
783 `gnus-summary-next-same-subject' command does, you can use the
784 following hook:
785
786  (add-hook gnus-select-group-hook
787            (lambda ()
788              (mapcar (lambda (header)
789                        (mail-header-set-subject
790                         header
791                         (gnus-simplify-subject
792                          (mail-header-subject header) 're-only)))
793                      gnus-newsgroup-headers)))"
794   :group 'gnus-group-select
795   :type 'hook)
796
797 (defcustom gnus-select-article-hook nil
798   "*A hook called when an article is selected."
799   :group 'gnus-summary-choose
800   :options '(gnus-agent-fetch-selected-article)
801   :type 'hook)
802
803 (defcustom gnus-visual-mark-article-hook
804   (list 'gnus-highlight-selected-summary)
805   "*Hook run after selecting an article in the summary buffer.
806 It is meant to be used for highlighting the article in some way.  It
807 is not run if `gnus-visual' is nil."
808   :group 'gnus-summary-visual
809   :type 'hook)
810
811 (defcustom gnus-parse-headers-hook nil
812   "*A hook called before parsing the headers."
813   :group 'gnus-various
814   :type 'hook)
815
816 (defcustom gnus-exit-group-hook nil
817   "*A hook called when exiting summary mode.
818 This hook is not called from the non-updating exit commands like `Q'."
819   :group 'gnus-various
820   :type 'hook)
821
822 (defcustom gnus-summary-update-hook
823   (list 'gnus-summary-highlight-line)
824   "*A hook called when a summary line is changed.
825 The hook will not be called if `gnus-visual' is nil.
826
827 The default function `gnus-summary-highlight-line' will
828 highlight the line according to the `gnus-summary-highlight'
829 variable."
830   :group 'gnus-summary-visual
831   :type 'hook)
832
833 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
834   "*A hook called when an article is selected for the first time.
835 The hook is intended to mark an article as read (or unread)
836 automatically when it is selected."
837   :group 'gnus-summary-choose
838   :type 'hook)
839
840 (defcustom gnus-group-no-more-groups-hook nil
841   "*A hook run when returning to group mode having no more (unread) groups."
842   :group 'gnus-group-select
843   :type 'hook)
844
845 (defcustom gnus-ps-print-hook nil
846   "*A hook run before ps-printing something from Gnus."
847   :group 'gnus-summary
848   :type 'hook)
849
850 (defcustom gnus-summary-display-arrow
851   (and (fboundp 'display-graphic-p)
852        (display-graphic-p))
853   "*If non-nil, display an arrow highlighting the current article."
854   :version "21.1"
855   :group 'gnus-summary
856   :type 'boolean)
857
858 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
859   "Face used for highlighting the current article in the summary buffer."
860   :group 'gnus-summary-visual
861   :type 'face)
862
863 (defvar gnus-tmp-downloaded nil)
864
865 (defcustom gnus-summary-highlight
866   '(((eq mark gnus-canceled-mark)
867      . gnus-summary-cancelled-face)
868     ((and uncached (> score default-high))
869      . gnus-summary-high-undownloaded-face)
870     ((and uncached (< score default-low))
871      . gnus-summary-low-undownloaded-face)
872     (uncached
873      . gnus-summary-normal-undownloaded-face)
874     ((and (> score default-high)
875           (or (eq mark gnus-dormant-mark)
876               (eq mark gnus-ticked-mark)))
877      . gnus-summary-high-ticked-face)
878     ((and (< score default-low)
879           (or (eq mark gnus-dormant-mark)
880               (eq mark gnus-ticked-mark)))
881      . gnus-summary-low-ticked-face)
882     ((or (eq mark gnus-dormant-mark)
883          (eq mark gnus-ticked-mark))
884      . gnus-summary-normal-ticked-face)
885     ((and (> score default-high) (eq mark gnus-ancient-mark))
886      . gnus-summary-high-ancient-face)
887     ((and (< score default-low) (eq mark gnus-ancient-mark))
888      . gnus-summary-low-ancient-face)
889     ((eq mark gnus-ancient-mark)
890      . gnus-summary-normal-ancient-face)
891     ((and (> score default-high) (eq mark gnus-unread-mark))
892      . gnus-summary-high-unread-face)
893     ((and (< score default-low) (eq mark gnus-unread-mark))
894      . gnus-summary-low-unread-face)
895     ((eq mark gnus-unread-mark)
896      . gnus-summary-normal-unread-face)
897     ((> score default-high)
898      . gnus-summary-high-read-face)
899     ((< score default-low)
900      . gnus-summary-low-read-face)
901     (t
902      . gnus-summary-normal-read-face))
903   "*Controls the highlighting of summary buffer lines.
904
905 A list of (FORM . FACE) pairs.  When deciding how a a particular
906 summary line should be displayed, each form is evaluated.  The content
907 of the face field after the first true form is used.  You can change
908 how those summary lines are displayed, by editing the face field.
909
910 You can use the following variables in the FORM field.
911
912 score:        The article's score
913 default:      The default article score.
914 default-high: The default score for high scored articles.
915 default-low:  The default score for low scored articles.
916 below:        The score below which articles are automatically marked as read.
917 mark:         The articles mark."
918   :group 'gnus-summary-visual
919   :type '(repeat (cons (sexp :tag "Form" nil)
920                        face)))
921
922 (defcustom gnus-alter-header-function nil
923   "Function called to allow alteration of article header structures.
924 The function is called with one parameter, the article header vector,
925 which it may alter in any way."
926   :type '(choice (const :tag "None" nil)
927                  function)
928   :group 'gnus-summary)
929
930 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
931   "Variable that says which function should be used to decode a string with encoded words.")
932
933 (defcustom gnus-extra-headers '(To Newsgroups)
934   "*Extra headers to parse."
935   :version "21.1"
936   :group 'gnus-summary
937   :type '(repeat symbol))
938
939 (defcustom gnus-ignored-from-addresses
940   (and user-mail-address (regexp-quote user-mail-address))
941   "*Regexp of From headers that may be suppressed in favor of To headers."
942   :version "21.1"
943   :group 'gnus-summary
944   :type 'regexp)
945
946 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
947   "List of charsets that should be ignored.
948 When these charsets are used in the \"charset\" parameter, the
949 default charset will be used instead."
950   :version "21.1"
951   :type '(repeat symbol)
952   :group 'gnus-charset)
953
954 (gnus-define-group-parameter
955  ignored-charsets
956  :type list
957  :function-document
958  "Return the ignored charsets of GROUP."
959  :variable gnus-group-ignored-charsets-alist
960  :variable-default
961  '(("alt\\.chinese\\.text" iso-8859-1))
962  :variable-document
963  "Alist of regexps (to match group names) and charsets that should be ignored.
964 When these charsets are used in the \"charset\" parameter, the
965 default charset will be used instead."
966  :variable-group gnus-charset
967  :variable-type '(repeat (cons (regexp :tag "Group")
968                                (repeat symbol)))
969  :parameter-type '(choice :tag "Ignored charsets"
970                           :value nil
971                           (repeat (symbol)))
972  :parameter-document       "\
973 List of charsets that should be ignored.
974
975 When these charsets are used in the \"charset\" parameter, the
976 default charset will be used instead.")
977
978 (defcustom gnus-group-highlight-words-alist nil
979   "Alist of group regexps and highlight regexps.
980 This variable uses the same syntax as `gnus-emphasis-alist'."
981   :version "21.1"
982   :type '(repeat (cons (regexp :tag "Group")
983                        (repeat (list (regexp :tag "Highlight regexp")
984                                      (number :tag "Group for entire word" 0)
985                                      (number :tag "Group for displayed part" 0)
986                                      (symbol :tag "Face"
987                                              gnus-emphasis-highlight-words)))))
988   :group 'gnus-summary-visual)
989
990 (defcustom gnus-summary-show-article-charset-alist
991   nil
992   "Alist of number and charset.
993 The article will be shown with the charset corresponding to the
994 numbered argument.
995 For example: ((1 . cn-gb-2312) (2 . big5))."
996   :version "21.1"
997   :type '(repeat (cons (number :tag "Argument" 1)
998                        (symbol :tag "Charset")))
999   :group 'gnus-charset)
1000
1001 (defcustom gnus-preserve-marks t
1002   "Whether marks are preserved when moving, copying and respooling messages."
1003   :version "21.1"
1004   :type 'boolean
1005   :group 'gnus-summary-marks)
1006
1007 (defcustom gnus-alter-articles-to-read-function nil
1008   "Function to be called to alter the list of articles to be selected."
1009   :type '(choice (const nil) function)
1010   :group 'gnus-summary)
1011
1012 (defcustom gnus-orphan-score nil
1013   "*All orphans get this score added.  Set in the score file."
1014   :group 'gnus-score-default
1015   :type '(choice (const nil)
1016                  integer))
1017
1018 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1019   "*A regexp to match MIME parts when saving multiple parts of a message
1020 with gnus-summary-save-parts (X m). This regexp will be used by default
1021 when prompting the user for which type of files to save."
1022   :group 'gnus-summary
1023   :type 'regexp)
1024
1025 (defcustom gnus-read-all-available-headers nil
1026   "Whether Gnus should parse all headers made available to it.
1027 This is mostly relevant for slow backends where the user may
1028 wish to widen the summary buffer to include all headers
1029 that were fetched.  Say, for nnultimate groups."
1030   :group 'gnus-summary
1031   :type '(choice boolean regexp))
1032
1033 (defcustom gnus-summary-muttprint-program "muttprint"
1034   "Command (and optional arguments) used to run Muttprint."
1035   :version "21.3"
1036   :group 'gnus-summary
1037   :type 'string)
1038
1039 (defcustom gnus-article-loose-mime nil
1040   "If non-nil, don't require MIME-Version header.
1041 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1042 supply the MIME-Version header or deliberately strip it From the mail.
1043 Set it to non-nil, Gnus will treat some articles as MIME even if
1044 the MIME-Version header is missed."
1045   :version "21.3"
1046   :type 'boolean
1047   :group 'gnus-article)
1048
1049 (defcustom gnus-article-emulate-mime t
1050   "If non-nil, use MIME emulation for uuencode and the like.
1051 This means that Gnus will search message bodies for text that look
1052 like uuencoded bits, yEncoded bits, and so on, and present that using
1053 the normal Gnus MIME machinery."
1054   :type 'boolean
1055   :group 'gnus-article)
1056
1057 ;;; Internal variables
1058
1059 (defvar gnus-summary-display-cache nil)
1060 (defvar gnus-article-mime-handles nil)
1061 (defvar gnus-article-decoded-p nil)
1062 (defvar gnus-article-charset nil)
1063 (defvar gnus-article-ignored-charsets nil)
1064 (defvar gnus-scores-exclude-files nil)
1065 (defvar gnus-page-broken nil)
1066
1067 (defvar gnus-original-article nil)
1068 (defvar gnus-article-internal-prepare-hook nil)
1069 (defvar gnus-newsgroup-process-stack nil)
1070
1071 (defvar gnus-thread-indent-array nil)
1072 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1073 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1074   "Function called to sort the articles within a thread after it has been gathered together.")
1075
1076 (defvar gnus-summary-save-parts-type-history nil)
1077 (defvar gnus-summary-save-parts-last-directory nil)
1078
1079 ;; Avoid highlighting in kill files.
1080 (defvar gnus-summary-inhibit-highlight nil)
1081 (defvar gnus-newsgroup-selected-overlay nil)
1082 (defvar gnus-inhibit-limiting nil)
1083 (defvar gnus-newsgroup-adaptive-score-file nil)
1084 (defvar gnus-current-score-file nil)
1085 (defvar gnus-current-move-group nil)
1086 (defvar gnus-current-copy-group nil)
1087 (defvar gnus-current-crosspost-group nil)
1088 (defvar gnus-newsgroup-display nil)
1089
1090 (defvar gnus-newsgroup-dependencies nil)
1091 (defvar gnus-newsgroup-adaptive nil)
1092 (defvar gnus-summary-display-article-function nil)
1093 (defvar gnus-summary-highlight-line-function nil
1094   "Function called after highlighting a summary line.")
1095
1096 (defvar gnus-summary-line-format-alist
1097   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1098     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1099     (?s gnus-tmp-subject-or-nil ?s)
1100     (?n gnus-tmp-name ?s)
1101     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1102         ?s)
1103     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1104             gnus-tmp-from) ?s)
1105     (?F gnus-tmp-from ?s)
1106     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1107     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1108     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1109     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1110     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1111     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1112     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1113     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1114     (?L gnus-tmp-lines ?s)
1115     (?O gnus-tmp-downloaded ?c)
1116     (?I gnus-tmp-indentation ?s)
1117     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1118     (?R gnus-tmp-replied ?c)
1119     (?\[ gnus-tmp-opening-bracket ?c)
1120     (?\] gnus-tmp-closing-bracket ?c)
1121     (?\> (make-string gnus-tmp-level ? ) ?s)
1122     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1123     (?i gnus-tmp-score ?d)
1124     (?z gnus-tmp-score-char ?c)
1125     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1126     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1127     (?U gnus-tmp-unread ?c)
1128     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1129         ?s)
1130     (?t (gnus-summary-number-of-articles-in-thread
1131          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1132         ?d)
1133     (?e (gnus-summary-number-of-articles-in-thread
1134          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1135         ?c)
1136     (?u gnus-tmp-user-defined ?s)
1137     (?P (gnus-pick-line-number) ?d)
1138     (?B gnus-tmp-thread-tree-header-string ?s)
1139     (user-date (gnus-user-date
1140                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1141   "An alist of format specifications that can appear in summary lines.
1142 These are paired with what variables they correspond with, along with
1143 the type of the variable (string, integer, character, etc).")
1144
1145 (defvar gnus-summary-dummy-line-format-alist
1146   `((?S gnus-tmp-subject ?s)
1147     (?N gnus-tmp-number ?d)
1148     (?u gnus-tmp-user-defined ?s)))
1149
1150 (defvar gnus-summary-mode-line-format-alist
1151   `((?G gnus-tmp-group-name ?s)
1152     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1153     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1154     (?A gnus-tmp-article-number ?d)
1155     (?Z gnus-tmp-unread-and-unselected ?s)
1156     (?V gnus-version ?s)
1157     (?U gnus-tmp-unread-and-unticked ?d)
1158     (?S gnus-tmp-subject ?s)
1159     (?e gnus-tmp-unselected ?d)
1160     (?u gnus-tmp-user-defined ?s)
1161     (?d (length gnus-newsgroup-dormant) ?d)
1162     (?t (length gnus-newsgroup-marked) ?d)
1163     (?h (length gnus-newsgroup-spam-marked) ?d)
1164     (?r (length gnus-newsgroup-reads) ?d)
1165     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1166     (?E gnus-newsgroup-expunged-tally ?d)
1167     (?s (gnus-current-score-file-nondirectory) ?s)))
1168
1169 (defvar gnus-last-search-regexp nil
1170   "Default regexp for article search command.")
1171
1172 (defvar gnus-last-shell-command nil
1173   "Default shell command on article.")
1174
1175 (defvar gnus-newsgroup-agentized nil
1176   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1177 (defvar gnus-newsgroup-begin nil)
1178 (defvar gnus-newsgroup-end nil)
1179 (defvar gnus-newsgroup-last-rmail nil)
1180 (defvar gnus-newsgroup-last-mail nil)
1181 (defvar gnus-newsgroup-last-folder nil)
1182 (defvar gnus-newsgroup-last-file nil)
1183 (defvar gnus-newsgroup-auto-expire nil)
1184 (defvar gnus-newsgroup-active nil)
1185
1186 (defvar gnus-newsgroup-data nil)
1187 (defvar gnus-newsgroup-data-reverse nil)
1188 (defvar gnus-newsgroup-limit nil)
1189 (defvar gnus-newsgroup-limits nil)
1190
1191 (defvar gnus-newsgroup-unreads nil
1192   "Sorted list of unread articles in the current newsgroup.")
1193
1194 (defvar gnus-newsgroup-unselected nil
1195   "Sorted list of unselected unread articles in the current newsgroup.")
1196
1197 (defvar gnus-newsgroup-reads nil
1198   "Alist of read articles and article marks in the current newsgroup.")
1199
1200 (defvar gnus-newsgroup-expunged-tally nil)
1201
1202 (defvar gnus-newsgroup-marked nil
1203   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1204
1205 (defvar gnus-newsgroup-spam-marked nil
1206   "List of ranges of articles that have been marked as spam.")
1207
1208 (defvar gnus-newsgroup-killed nil
1209   "List of ranges of articles that have been through the scoring process.")
1210
1211 (defvar gnus-newsgroup-cached nil
1212   "Sorted list of articles that come from the article cache.")
1213
1214 (defvar gnus-newsgroup-saved nil
1215   "List of articles that have been saved.")
1216
1217 (defvar gnus-newsgroup-kill-headers nil)
1218
1219 (defvar gnus-newsgroup-replied nil
1220   "List of articles that have been replied to in the current newsgroup.")
1221
1222 (defvar gnus-newsgroup-forwarded nil
1223   "List of articles that have been forwarded in the current newsgroup.")
1224
1225 (defvar gnus-newsgroup-recent nil
1226   "List of articles that have are recent in the current newsgroup.")
1227
1228 (defvar gnus-newsgroup-expirable nil
1229   "Sorted list of articles in the current newsgroup that can be expired.")
1230
1231 (defvar gnus-newsgroup-processable nil
1232   "List of articles in the current newsgroup that can be processed.")
1233
1234 (defvar gnus-newsgroup-downloadable nil
1235   "Sorted list of articles in the current newsgroup that can be processed.")
1236
1237 (defvar gnus-newsgroup-unfetched nil
1238   "Sorted list of articles in the current newsgroup whose headers have not been fetched into the agent.")
1239
1240 (defvar gnus-newsgroup-undownloaded nil
1241   "List of articles in the current newsgroup that haven't been downloaded..")
1242
1243 (defvar gnus-newsgroup-unsendable nil
1244   "List of articles in the current newsgroup that won't be sent.")
1245
1246 (defvar gnus-newsgroup-bookmarks nil
1247   "List of articles in the current newsgroup that have bookmarks.")
1248
1249 (defvar gnus-newsgroup-dormant nil
1250   "Sorted list of dormant articles in the current newsgroup.")
1251
1252 (defvar gnus-newsgroup-unseen nil
1253   "List of unseen articles in the current newsgroup.")
1254
1255 (defvar gnus-newsgroup-seen nil
1256   "Range of seen articles in the current newsgroup.")
1257
1258 (defvar gnus-newsgroup-articles nil
1259   "List of articles in the current newsgroup.")
1260
1261 (defvar gnus-newsgroup-scored nil
1262   "List of scored articles in the current newsgroup.")
1263
1264 (defvar gnus-newsgroup-headers nil
1265   "List of article headers in the current newsgroup.")
1266
1267 (defvar gnus-newsgroup-threads nil)
1268
1269 (defvar gnus-newsgroup-prepared nil
1270   "Whether the current group has been prepared properly.")
1271
1272 (defvar gnus-newsgroup-ancient nil
1273   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1274
1275 (defvar gnus-newsgroup-sparse nil)
1276
1277 (defvar gnus-current-article nil)
1278 (defvar gnus-article-current nil)
1279 (defvar gnus-current-headers nil)
1280 (defvar gnus-have-all-headers nil)
1281 (defvar gnus-last-article nil)
1282 (defvar gnus-newsgroup-history nil)
1283 (defvar gnus-newsgroup-charset nil)
1284 (defvar gnus-newsgroup-ephemeral-charset nil)
1285 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1286
1287 (defvar gnus-article-before-search nil)
1288
1289 (defconst gnus-summary-local-variables
1290   '(gnus-newsgroup-name
1291     gnus-newsgroup-begin gnus-newsgroup-end
1292     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1293     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1294     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1295     gnus-newsgroup-unselected gnus-newsgroup-marked
1296     gnus-newsgroup-spam-marked
1297     gnus-newsgroup-reads gnus-newsgroup-saved
1298     gnus-newsgroup-replied gnus-newsgroup-forwarded
1299     gnus-newsgroup-recent
1300     gnus-newsgroup-expirable
1301     gnus-newsgroup-processable gnus-newsgroup-killed
1302     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1303     gnus-newsgroup-unfetched
1304     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1305     gnus-newsgroup-seen gnus-newsgroup-articles
1306     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1307     gnus-newsgroup-headers gnus-newsgroup-threads
1308     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1309     gnus-current-article gnus-current-headers gnus-have-all-headers
1310     gnus-last-article gnus-article-internal-prepare-hook
1311     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1312     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1313     gnus-thread-expunge-below
1314     gnus-score-alist gnus-current-score-file
1315     (gnus-summary-expunge-below . global)
1316     (gnus-summary-mark-below . global)
1317     (gnus-orphan-score . global)
1318     gnus-newsgroup-active gnus-scores-exclude-files
1319     gnus-newsgroup-history gnus-newsgroup-ancient
1320     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1321     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1322     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1323     (gnus-newsgroup-expunged-tally . 0)
1324     gnus-cache-removable-articles gnus-newsgroup-cached
1325     gnus-newsgroup-data gnus-newsgroup-data-reverse
1326     gnus-newsgroup-limit gnus-newsgroup-limits
1327     gnus-newsgroup-charset gnus-newsgroup-display)
1328   "Variables that are buffer-local to the summary buffers.")
1329
1330 (defvar gnus-newsgroup-variables nil
1331   "A list of variables that have separate values in different newsgroups.
1332 A list of newsgroup (summary buffer) local variables, or cons of
1333 variables and their default values (when the default values are not
1334 nil), that should be made global while the summary buffer is active.
1335 These variables can be used to set variables in the group parameters
1336 while still allowing them to affect operations done in other
1337 buffers. For example:
1338
1339 \(setq gnus-newsgroup-variables
1340      '(message-use-followup-to
1341        (gnus-visible-headers .
1342          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1343 ")
1344
1345 ;; Byte-compiler warning.
1346 ;(eval-when-compile (defvar gnus-article-mode-map))
1347 (eval-when-compile
1348   (let ((features (cons 'gnus-sum features)))
1349     (require 'gnus)
1350     (require 'gnus-agent)
1351     (require 'gnus-art)))
1352
1353 ;; MIME stuff.
1354
1355 (defvar gnus-decode-encoded-word-methods
1356   '(mail-decode-encoded-word-string)
1357   "List of methods used to decode encoded words.
1358
1359 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1360 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1361 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1362 whose names match REGEXP.
1363
1364 For example:
1365 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1366  mail-decode-encoded-word-string
1367  (\"chinese\" . rfc1843-decode-string))")
1368
1369 (defvar gnus-decode-encoded-word-methods-cache nil)
1370
1371 (defun gnus-multi-decode-encoded-word-string (string)
1372   "Apply the functions from `gnus-encoded-word-methods' that match."
1373   (unless (and gnus-decode-encoded-word-methods-cache
1374                (eq gnus-newsgroup-name
1375                    (car gnus-decode-encoded-word-methods-cache)))
1376     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1377     (mapcar (lambda (x)
1378               (if (symbolp x)
1379                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1380                 (if (and gnus-newsgroup-name
1381                          (string-match (car x) gnus-newsgroup-name))
1382                     (nconc gnus-decode-encoded-word-methods-cache
1383                            (list (cdr x))))))
1384             gnus-decode-encoded-word-methods))
1385   (let ((xlist gnus-decode-encoded-word-methods-cache))
1386     (pop xlist)
1387     (while xlist
1388       (setq string (funcall (pop xlist) string))))
1389   string)
1390
1391 ;; Subject simplification.
1392
1393 (defun gnus-simplify-whitespace (str)
1394   "Remove excessive whitespace from STR."
1395   (let ((mystr str))
1396     ;; Multiple spaces.
1397     (while (string-match "[ \t][ \t]+" mystr)
1398       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1399                           " "
1400                           (substring mystr (match-end 0)))))
1401     ;; Leading spaces.
1402     (when (string-match "^[ \t]+" mystr)
1403       (setq mystr (substring mystr (match-end 0))))
1404     ;; Trailing spaces.
1405     (when (string-match "[ \t]+$" mystr)
1406       (setq mystr (substring mystr 0 (match-beginning 0))))
1407     mystr))
1408
1409 (defun gnus-simplify-all-whitespace (str)
1410   "Remove all whitespace from STR."
1411   (let ((mystr str))
1412     (while (string-match "[ \t\n]+" mystr)
1413       (setq mystr (replace-match "" nil nil mystr)))
1414     mystr))
1415
1416 (defsubst gnus-simplify-subject-re (subject)
1417   "Remove \"Re:\" from subject lines."
1418   (if (string-match message-subject-re-regexp subject)
1419       (substring subject (match-end 0))
1420     subject))
1421
1422 (defun gnus-simplify-subject (subject &optional re-only)
1423   "Remove `Re:' and words in parentheses.
1424 If RE-ONLY is non-nil, strip leading `Re:'s only."
1425   (let ((case-fold-search t))           ;Ignore case.
1426     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1427     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1428       (setq subject (substring subject (match-end 0))))
1429     ;; Remove uninteresting prefixes.
1430     (when (and (not re-only)
1431                gnus-simplify-ignored-prefixes
1432                (string-match gnus-simplify-ignored-prefixes subject))
1433       (setq subject (substring subject (match-end 0))))
1434     ;; Remove words in parentheses from end.
1435     (unless re-only
1436       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1437         (setq subject (substring subject 0 (match-beginning 0)))))
1438     ;; Return subject string.
1439     subject))
1440
1441 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1442 ;; all whitespace.
1443 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1444   (goto-char (point-min))
1445   (while (re-search-forward regexp nil t)
1446     (replace-match (or newtext ""))))
1447
1448 (defun gnus-simplify-buffer-fuzzy ()
1449   "Simplify string in the buffer fuzzily.
1450 The string in the accessible portion of the current buffer is simplified.
1451 It is assumed to be a single-line subject.
1452 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1453 matter is removed.  Additional things can be deleted by setting
1454 `gnus-simplify-subject-fuzzy-regexp'."
1455   (let ((case-fold-search t)
1456         (modified-tick))
1457     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1458
1459     (while (not (eq modified-tick (buffer-modified-tick)))
1460       (setq modified-tick (buffer-modified-tick))
1461       (cond
1462        ((listp gnus-simplify-subject-fuzzy-regexp)
1463         (mapcar 'gnus-simplify-buffer-fuzzy-step
1464                 gnus-simplify-subject-fuzzy-regexp))
1465        (gnus-simplify-subject-fuzzy-regexp
1466         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1467       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1468       (gnus-simplify-buffer-fuzzy-step
1469        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1470       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1471
1472     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1473     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1474     (gnus-simplify-buffer-fuzzy-step " $")
1475     (gnus-simplify-buffer-fuzzy-step "^ +")))
1476
1477 (defun gnus-simplify-subject-fuzzy (subject)
1478   "Simplify a subject string fuzzily.
1479 See `gnus-simplify-buffer-fuzzy' for details."
1480   (save-excursion
1481     (gnus-set-work-buffer)
1482     (let ((case-fold-search t))
1483       ;; Remove uninteresting prefixes.
1484       (when (and gnus-simplify-ignored-prefixes
1485                  (string-match gnus-simplify-ignored-prefixes subject))
1486         (setq subject (substring subject (match-end 0))))
1487       (insert subject)
1488       (inline (gnus-simplify-buffer-fuzzy))
1489       (buffer-string))))
1490
1491 (defsubst gnus-simplify-subject-fully (subject)
1492   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1493   (cond
1494    (gnus-simplify-subject-functions
1495     (gnus-map-function gnus-simplify-subject-functions subject))
1496    ((null gnus-summary-gather-subject-limit)
1497     (gnus-simplify-subject-re subject))
1498    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1499     (gnus-simplify-subject-fuzzy subject))
1500    ((numberp gnus-summary-gather-subject-limit)
1501     (gnus-limit-string (gnus-simplify-subject-re subject)
1502                        gnus-summary-gather-subject-limit))
1503    (t
1504     subject)))
1505
1506 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1507   "Check whether two subjects are equal.
1508 If optional argument simple-first is t, first argument is already
1509 simplified."
1510   (cond
1511    ((null simple-first)
1512     (equal (gnus-simplify-subject-fully s1)
1513            (gnus-simplify-subject-fully s2)))
1514    (t
1515     (equal s1
1516            (gnus-simplify-subject-fully s2)))))
1517
1518 (defun gnus-summary-bubble-group ()
1519   "Increase the score of the current group.
1520 This is a handy function to add to `gnus-summary-exit-hook' to
1521 increase the score of each group you read."
1522   (gnus-group-add-score gnus-newsgroup-name))
1523
1524 \f
1525 ;;;
1526 ;;; Gnus summary mode
1527 ;;;
1528
1529 (put 'gnus-summary-mode 'mode-class 'special)
1530
1531 (defvar gnus-article-commands-menu)
1532
1533 ;; Non-orthogonal keys
1534
1535 (gnus-define-keys gnus-summary-mode-map
1536   " " gnus-summary-next-page
1537   "\177" gnus-summary-prev-page
1538   [delete] gnus-summary-prev-page
1539   [backspace] gnus-summary-prev-page
1540   "\r" gnus-summary-scroll-up
1541   "\M-\r" gnus-summary-scroll-down
1542   "n" gnus-summary-next-unread-article
1543   "p" gnus-summary-prev-unread-article
1544   "N" gnus-summary-next-article
1545   "P" gnus-summary-prev-article
1546   "\M-\C-n" gnus-summary-next-same-subject
1547   "\M-\C-p" gnus-summary-prev-same-subject
1548   "\M-n" gnus-summary-next-unread-subject
1549   "\M-p" gnus-summary-prev-unread-subject
1550   "." gnus-summary-first-unread-article
1551   "," gnus-summary-best-unread-article
1552   "\M-s" gnus-summary-search-article-forward
1553   "\M-r" gnus-summary-search-article-backward
1554   "<" gnus-summary-beginning-of-article
1555   ">" gnus-summary-end-of-article
1556   "j" gnus-summary-goto-article
1557   "^" gnus-summary-refer-parent-article
1558   "\M-^" gnus-summary-refer-article
1559   "u" gnus-summary-tick-article-forward
1560   "!" gnus-summary-tick-article-forward
1561   "U" gnus-summary-tick-article-backward
1562   "d" gnus-summary-mark-as-read-forward
1563   "D" gnus-summary-mark-as-read-backward
1564   "E" gnus-summary-mark-as-expirable
1565   "\M-u" gnus-summary-clear-mark-forward
1566   "\M-U" gnus-summary-clear-mark-backward
1567   "k" gnus-summary-kill-same-subject-and-select
1568   "\C-k" gnus-summary-kill-same-subject
1569   "\M-\C-k" gnus-summary-kill-thread
1570   "\M-\C-l" gnus-summary-lower-thread
1571   "e" gnus-summary-edit-article
1572   "#" gnus-summary-mark-as-processable
1573   "\M-#" gnus-summary-unmark-as-processable
1574   "\M-\C-t" gnus-summary-toggle-threads
1575   "\M-\C-s" gnus-summary-show-thread
1576   "\M-\C-h" gnus-summary-hide-thread
1577   "\M-\C-f" gnus-summary-next-thread
1578   "\M-\C-b" gnus-summary-prev-thread
1579   [(meta down)] gnus-summary-next-thread
1580   [(meta up)] gnus-summary-prev-thread
1581   "\M-\C-u" gnus-summary-up-thread
1582   "\M-\C-d" gnus-summary-down-thread
1583   "&" gnus-summary-execute-command
1584   "c" gnus-summary-catchup-and-exit
1585   "\C-w" gnus-summary-mark-region-as-read
1586   "\C-t" gnus-summary-toggle-truncation
1587   "?" gnus-summary-mark-as-dormant
1588   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1589   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1590   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1591   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1592   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1593   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1594   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1595   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1596   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1597   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1598   "=" gnus-summary-expand-window
1599   "\C-x\C-s" gnus-summary-reselect-current-group
1600   "\M-g" gnus-summary-rescan-group
1601   "w" gnus-summary-stop-page-breaking
1602   "\C-c\C-r" gnus-summary-caesar-message
1603   "f" gnus-summary-followup
1604   "F" gnus-summary-followup-with-original
1605   "C" gnus-summary-cancel-article
1606   "r" gnus-summary-reply
1607   "R" gnus-summary-reply-with-original
1608   "\C-c\C-f" gnus-summary-mail-forward
1609   "o" gnus-summary-save-article
1610   "\C-o" gnus-summary-save-article-mail
1611   "|" gnus-summary-pipe-output
1612   "\M-k" gnus-summary-edit-local-kill
1613   "\M-K" gnus-summary-edit-global-kill
1614   ;; "V" gnus-version
1615   "\C-c\C-d" gnus-summary-describe-group
1616   "q" gnus-summary-exit
1617   "Q" gnus-summary-exit-no-update
1618   "\C-c\C-i" gnus-info-find-node
1619   gnus-mouse-2 gnus-mouse-pick-article
1620   "m" gnus-summary-mail-other-window
1621   "a" gnus-summary-post-news
1622   "i" gnus-summary-news-other-window
1623   "x" gnus-summary-limit-to-unread
1624   "s" gnus-summary-isearch-article
1625   "t" gnus-summary-toggle-header
1626   "g" gnus-summary-show-article
1627   "l" gnus-summary-goto-last-article
1628   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1629   "\C-d" gnus-summary-enter-digest-group
1630   "\M-\C-d" gnus-summary-read-document
1631   "\M-\C-e" gnus-summary-edit-parameters
1632   "\M-\C-a" gnus-summary-customize-parameters
1633   "\C-c\C-b" gnus-bug
1634   "*" gnus-cache-enter-article
1635   "\M-*" gnus-cache-remove-article
1636   "\M-&" gnus-summary-universal-argument
1637   "\C-l" gnus-recenter
1638   "I" gnus-summary-increase-score
1639   "L" gnus-summary-lower-score
1640   "\M-i" gnus-symbolic-argument
1641   "h" gnus-summary-select-article-buffer
1642
1643   "b" gnus-article-view-part
1644   "\M-t" gnus-summary-toggle-display-buttonized
1645
1646   "V" gnus-summary-score-map
1647   "X" gnus-uu-extract-map
1648   "S" gnus-summary-send-map)
1649
1650 ;; Sort of orthogonal keymap
1651 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1652   "t" gnus-summary-tick-article-forward
1653   "!" gnus-summary-tick-article-forward
1654   "d" gnus-summary-mark-as-read-forward
1655   "r" gnus-summary-mark-as-read-forward
1656   "c" gnus-summary-clear-mark-forward
1657   " " gnus-summary-clear-mark-forward
1658   "e" gnus-summary-mark-as-expirable
1659   "x" gnus-summary-mark-as-expirable
1660   "?" gnus-summary-mark-as-dormant
1661   "b" gnus-summary-set-bookmark
1662   "B" gnus-summary-remove-bookmark
1663   "#" gnus-summary-mark-as-processable
1664   "\M-#" gnus-summary-unmark-as-processable
1665   "S" gnus-summary-limit-include-expunged
1666   "C" gnus-summary-catchup
1667   "H" gnus-summary-catchup-to-here
1668   "h" gnus-summary-catchup-from-here
1669   "\C-c" gnus-summary-catchup-all
1670   "k" gnus-summary-kill-same-subject-and-select
1671   "K" gnus-summary-kill-same-subject
1672   "P" gnus-uu-mark-map)
1673
1674 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1675   "c" gnus-summary-clear-above
1676   "u" gnus-summary-tick-above
1677   "m" gnus-summary-mark-above
1678   "k" gnus-summary-kill-below)
1679
1680 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1681   "/" gnus-summary-limit-to-subject
1682   "n" gnus-summary-limit-to-articles
1683   "w" gnus-summary-pop-limit
1684   "s" gnus-summary-limit-to-subject
1685   "a" gnus-summary-limit-to-author
1686   "u" gnus-summary-limit-to-unread
1687   "m" gnus-summary-limit-to-marks
1688   "M" gnus-summary-limit-exclude-marks
1689   "v" gnus-summary-limit-to-score
1690   "*" gnus-summary-limit-include-cached
1691   "D" gnus-summary-limit-include-dormant
1692   "T" gnus-summary-limit-include-thread
1693   "d" gnus-summary-limit-exclude-dormant
1694   "t" gnus-summary-limit-to-age
1695   "." gnus-summary-limit-to-unseen
1696   "x" gnus-summary-limit-to-extra
1697   "p" gnus-summary-limit-to-display-predicate
1698   "E" gnus-summary-limit-include-expunged
1699   "c" gnus-summary-limit-exclude-childless-dormant
1700   "C" gnus-summary-limit-mark-excluded-as-read
1701   "o" gnus-summary-insert-old-articles
1702   "N" gnus-summary-insert-new-articles)
1703
1704 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1705   "n" gnus-summary-next-unread-article
1706   "p" gnus-summary-prev-unread-article
1707   "N" gnus-summary-next-article
1708   "P" gnus-summary-prev-article
1709   "\C-n" gnus-summary-next-same-subject
1710   "\C-p" gnus-summary-prev-same-subject
1711   "\M-n" gnus-summary-next-unread-subject
1712   "\M-p" gnus-summary-prev-unread-subject
1713   "f" gnus-summary-first-unread-article
1714   "b" gnus-summary-best-unread-article
1715   "j" gnus-summary-goto-article
1716   "g" gnus-summary-goto-subject
1717   "l" gnus-summary-goto-last-article
1718   "o" gnus-summary-pop-article)
1719
1720 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1721   "k" gnus-summary-kill-thread
1722   "l" gnus-summary-lower-thread
1723   "i" gnus-summary-raise-thread
1724   "T" gnus-summary-toggle-threads
1725   "t" gnus-summary-rethread-current
1726   "^" gnus-summary-reparent-thread
1727   "s" gnus-summary-show-thread
1728   "S" gnus-summary-show-all-threads
1729   "h" gnus-summary-hide-thread
1730   "H" gnus-summary-hide-all-threads
1731   "n" gnus-summary-next-thread
1732   "p" gnus-summary-prev-thread
1733   "u" gnus-summary-up-thread
1734   "o" gnus-summary-top-thread
1735   "d" gnus-summary-down-thread
1736   "#" gnus-uu-mark-thread
1737   "\M-#" gnus-uu-unmark-thread)
1738
1739 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1740   "g" gnus-summary-prepare
1741   "c" gnus-summary-insert-cached-articles
1742   "d" gnus-summary-insert-dormant-articles)
1743
1744 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1745   "c" gnus-summary-catchup-and-exit
1746   "C" gnus-summary-catchup-all-and-exit
1747   "E" gnus-summary-exit-no-update
1748   "Q" gnus-summary-exit
1749   "Z" gnus-summary-exit
1750   "n" gnus-summary-catchup-and-goto-next-group
1751   "R" gnus-summary-reselect-current-group
1752   "G" gnus-summary-rescan-group
1753   "N" gnus-summary-next-group
1754   "s" gnus-summary-save-newsrc
1755   "P" gnus-summary-prev-group)
1756
1757 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1758   " " gnus-summary-next-page
1759   "n" gnus-summary-next-page
1760   "\177" gnus-summary-prev-page
1761   [delete] gnus-summary-prev-page
1762   "p" gnus-summary-prev-page
1763   "\r" gnus-summary-scroll-up
1764   "\M-\r" gnus-summary-scroll-down
1765   "<" gnus-summary-beginning-of-article
1766   ">" gnus-summary-end-of-article
1767   "b" gnus-summary-beginning-of-article
1768   "e" gnus-summary-end-of-article
1769   "^" gnus-summary-refer-parent-article
1770   "r" gnus-summary-refer-parent-article
1771   "D" gnus-summary-enter-digest-group
1772   "R" gnus-summary-refer-references
1773   "T" gnus-summary-refer-thread
1774   "g" gnus-summary-show-article
1775   "s" gnus-summary-isearch-article
1776   "P" gnus-summary-print-article
1777   "M" gnus-mailing-list-insinuate
1778   "t" gnus-article-babel)
1779
1780 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1781   "b" gnus-article-add-buttons
1782   "B" gnus-article-add-buttons-to-head
1783   "o" gnus-article-treat-overstrike
1784   "e" gnus-article-emphasize
1785   "w" gnus-article-fill-cited-article
1786   "Q" gnus-article-fill-long-lines
1787   "C" gnus-article-capitalize-sentences
1788   "c" gnus-article-remove-cr
1789   "q" gnus-article-de-quoted-unreadable
1790   "6" gnus-article-de-base64-unreadable
1791   "Z" gnus-article-decode-HZ
1792   "h" gnus-article-wash-html
1793   "u" gnus-article-unsplit-urls
1794   "s" gnus-summary-force-verify-and-decrypt
1795   "f" gnus-article-display-x-face
1796   "l" gnus-summary-stop-page-breaking
1797   "r" gnus-summary-caesar-message
1798   "m" gnus-summary-morse-message
1799   "t" gnus-summary-toggle-header
1800   "g" gnus-treat-smiley
1801   "v" gnus-summary-verbose-headers
1802   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1803   "p" gnus-article-verify-x-pgp-sig
1804   "d" gnus-article-treat-dumbquotes)
1805
1806 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1807   ;; mnemonic: deuglif*Y*
1808   "u" gnus-article-outlook-unwrap-lines
1809   "a" gnus-article-outlook-repair-attribution
1810   "c" gnus-article-outlook-rearrange-citation
1811   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1812
1813 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1814   "a" gnus-article-hide
1815   "h" gnus-article-hide-headers
1816   "b" gnus-article-hide-boring-headers
1817   "s" gnus-article-hide-signature
1818   "c" gnus-article-hide-citation
1819   "C" gnus-article-hide-citation-in-followups
1820   "l" gnus-article-hide-list-identifiers
1821   "B" gnus-article-strip-banner
1822   "P" gnus-article-hide-pem
1823   "\C-c" gnus-article-hide-citation-maybe)
1824
1825 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1826   "a" gnus-article-highlight
1827   "h" gnus-article-highlight-headers
1828   "c" gnus-article-highlight-citation
1829   "s" gnus-article-highlight-signature)
1830
1831 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1832   "f" gnus-article-treat-fold-headers
1833   "u" gnus-article-treat-unfold-headers
1834   "n" gnus-article-treat-fold-newsgroups)
1835
1836 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1837   "x" gnus-article-display-x-face
1838   "d" gnus-article-display-face
1839   "s" gnus-treat-smiley
1840   "D" gnus-article-remove-images
1841   "f" gnus-treat-from-picon
1842   "m" gnus-treat-mail-picon
1843   "n" gnus-treat-newsgroups-picon)
1844
1845 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1846   "w" gnus-article-decode-mime-words
1847   "c" gnus-article-decode-charset
1848   "v" gnus-mime-view-all-parts
1849   "b" gnus-article-view-part)
1850
1851 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1852   "z" gnus-article-date-ut
1853   "u" gnus-article-date-ut
1854   "l" gnus-article-date-local
1855   "p" gnus-article-date-english
1856   "e" gnus-article-date-lapsed
1857   "o" gnus-article-date-original
1858   "i" gnus-article-date-iso8601
1859   "s" gnus-article-date-user)
1860
1861 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1862   "t" gnus-article-remove-trailing-blank-lines
1863   "l" gnus-article-strip-leading-blank-lines
1864   "m" gnus-article-strip-multiple-blank-lines
1865   "a" gnus-article-strip-blank-lines
1866   "A" gnus-article-strip-all-blank-lines
1867   "s" gnus-article-strip-leading-space
1868   "e" gnus-article-strip-trailing-space
1869   "w" gnus-article-remove-leading-whitespace)
1870
1871 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1872   "v" gnus-version
1873   "f" gnus-summary-fetch-faq
1874   "d" gnus-summary-describe-group
1875   "h" gnus-summary-describe-briefly
1876   "i" gnus-info-find-node
1877   "c" gnus-group-fetch-charter
1878   "C" gnus-group-fetch-control)
1879
1880 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1881   "e" gnus-summary-expire-articles
1882   "\M-\C-e" gnus-summary-expire-articles-now
1883   "\177" gnus-summary-delete-article
1884   [delete] gnus-summary-delete-article
1885   [backspace] gnus-summary-delete-article
1886   "m" gnus-summary-move-article
1887   "r" gnus-summary-respool-article
1888   "w" gnus-summary-edit-article
1889   "c" gnus-summary-copy-article
1890   "B" gnus-summary-crosspost-article
1891   "q" gnus-summary-respool-query
1892   "t" gnus-summary-respool-trace
1893   "i" gnus-summary-import-article
1894   "I" gnus-summary-create-article
1895   "p" gnus-summary-article-posted-p)
1896
1897 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1898   "o" gnus-summary-save-article
1899   "m" gnus-summary-save-article-mail
1900   "F" gnus-summary-write-article-file
1901   "r" gnus-summary-save-article-rmail
1902   "f" gnus-summary-save-article-file
1903   "b" gnus-summary-save-article-body-file
1904   "h" gnus-summary-save-article-folder
1905   "v" gnus-summary-save-article-vm
1906   "p" gnus-summary-pipe-output
1907   "P" gnus-summary-muttprint
1908   "s" gnus-soup-add-article)
1909
1910 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1911   "b" gnus-summary-display-buttonized
1912   "m" gnus-summary-repair-multipart
1913   "v" gnus-article-view-part
1914   "o" gnus-article-save-part
1915   "c" gnus-article-copy-part
1916   "C" gnus-article-view-part-as-charset
1917   "e" gnus-article-view-part-externally
1918   "E" gnus-article-encrypt-body
1919   "i" gnus-article-inline-part
1920   "|" gnus-article-pipe-part)
1921
1922 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1923   "p" gnus-summary-mark-as-processable
1924   "u" gnus-summary-unmark-as-processable
1925   "U" gnus-summary-unmark-all-processable
1926   "v" gnus-uu-mark-over
1927   "s" gnus-uu-mark-series
1928   "r" gnus-uu-mark-region
1929   "g" gnus-uu-unmark-region
1930   "R" gnus-uu-mark-by-regexp
1931   "G" gnus-uu-unmark-by-regexp
1932   "t" gnus-uu-mark-thread
1933   "T" gnus-uu-unmark-thread
1934   "a" gnus-uu-mark-all
1935   "b" gnus-uu-mark-buffer
1936   "S" gnus-uu-mark-sparse
1937   "k" gnus-summary-kill-process-mark
1938   "y" gnus-summary-yank-process-mark
1939   "w" gnus-summary-save-process-mark
1940   "i" gnus-uu-invert-processable)
1941
1942 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1943   ;;"x" gnus-uu-extract-any
1944   "m" gnus-summary-save-parts
1945   "u" gnus-uu-decode-uu
1946   "U" gnus-uu-decode-uu-and-save
1947   "s" gnus-uu-decode-unshar
1948   "S" gnus-uu-decode-unshar-and-save
1949   "o" gnus-uu-decode-save
1950   "O" gnus-uu-decode-save
1951   "b" gnus-uu-decode-binhex
1952   "B" gnus-uu-decode-binhex
1953   "p" gnus-uu-decode-postscript
1954   "P" gnus-uu-decode-postscript-and-save)
1955
1956 (gnus-define-keys
1957     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
1958   "u" gnus-uu-decode-uu-view
1959   "U" gnus-uu-decode-uu-and-save-view
1960   "s" gnus-uu-decode-unshar-view
1961   "S" gnus-uu-decode-unshar-and-save-view
1962   "o" gnus-uu-decode-save-view
1963   "O" gnus-uu-decode-save-view
1964   "b" gnus-uu-decode-binhex-view
1965   "B" gnus-uu-decode-binhex-view
1966   "p" gnus-uu-decode-postscript-view
1967   "P" gnus-uu-decode-postscript-and-save-view)
1968
1969 (defvar gnus-article-post-menu nil)
1970
1971 (defconst gnus-summary-menu-maxlen 20)
1972
1973 (defun gnus-summary-menu-split (menu)
1974   ;; If we have lots of elements, divide them into groups of 20
1975   ;; and make a pane (or submenu) for each one.
1976   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
1977       (let ((menu menu) sublists next
1978             (i 1))
1979         (while menu
1980           ;; Pull off the next gnus-summary-menu-maxlen elements
1981           ;; and make them the next element of sublist.
1982           (setq next (nthcdr gnus-summary-menu-maxlen menu))
1983           (if next
1984               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
1985                       nil))
1986           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
1987                                              (aref (car (last menu)) 0)) menu)
1988                                sublists))
1989           (setq i (1+ i))
1990           (setq menu next))
1991         (nreverse sublists))
1992     ;; Few elements--put them all in one pane.
1993     menu))
1994
1995 (defun gnus-summary-make-menu-bar ()
1996   (gnus-turn-off-edit-menu 'summary)
1997
1998   (unless (boundp 'gnus-summary-misc-menu)
1999
2000     (easy-menu-define
2001       gnus-summary-kill-menu gnus-summary-mode-map ""
2002       (cons
2003        "Score"
2004        (nconc
2005         (list
2006          ["Customize" gnus-score-customize t])
2007         (gnus-make-score-map 'increase)
2008         (gnus-make-score-map 'lower)
2009         '(("Mark"
2010            ["Kill below" gnus-summary-kill-below t]
2011            ["Mark above" gnus-summary-mark-above t]
2012            ["Tick above" gnus-summary-tick-above t]
2013            ["Clear above" gnus-summary-clear-above t])
2014           ["Current score" gnus-summary-current-score t]
2015           ["Set score" gnus-summary-set-score t]
2016           ["Switch current score file..." gnus-score-change-score-file t]
2017           ["Set mark below..." gnus-score-set-mark-below t]
2018           ["Set expunge below..." gnus-score-set-expunge-below t]
2019           ["Edit current score file" gnus-score-edit-current-scores t]
2020           ["Edit score file" gnus-score-edit-file t]
2021           ["Trace score" gnus-score-find-trace t]
2022           ["Find words" gnus-score-find-favourite-words t]
2023           ["Rescore buffer" gnus-summary-rescore t]
2024           ["Increase score..." gnus-summary-increase-score t]
2025           ["Lower score..." gnus-summary-lower-score t]))))
2026
2027     ;; Define both the Article menu in the summary buffer and the
2028     ;; equivalent Commands menu in the article buffer here for
2029     ;; consistency.
2030     (let ((innards
2031            `(("Hide"
2032               ["All" gnus-article-hide t]
2033               ["Headers" gnus-article-hide-headers t]
2034               ["Signature" gnus-article-hide-signature t]
2035               ["Citation" gnus-article-hide-citation t]
2036               ["List identifiers" gnus-article-hide-list-identifiers t]
2037               ["Banner" gnus-article-strip-banner t]
2038               ["Boring headers" gnus-article-hide-boring-headers t])
2039              ("Highlight"
2040               ["All" gnus-article-highlight t]
2041               ["Headers" gnus-article-highlight-headers t]
2042               ["Signature" gnus-article-highlight-signature t]
2043               ["Citation" gnus-article-highlight-citation t])
2044              ("MIME"
2045               ["Words" gnus-article-decode-mime-words t]
2046               ["Charset" gnus-article-decode-charset t]
2047               ["QP" gnus-article-de-quoted-unreadable t]
2048               ["Base64" gnus-article-de-base64-unreadable t]
2049               ["View MIME buttons" gnus-summary-display-buttonized t]
2050               ["View all" gnus-mime-view-all-parts t]
2051               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2052               ["Encrypt body" gnus-article-encrypt-body t]
2053               ["Extract all parts" gnus-summary-save-parts t])
2054              ("Date"
2055               ["Local" gnus-article-date-local t]
2056               ["ISO8601" gnus-article-date-iso8601 t]
2057               ["UT" gnus-article-date-ut t]
2058               ["Original" gnus-article-date-original t]
2059               ["Lapsed" gnus-article-date-lapsed t]
2060               ["User-defined" gnus-article-date-user t])
2061              ("Display"
2062               ["Remove images" gnus-article-remove-images t]
2063               ["Toggle smiley" gnus-treat-smiley t]
2064               ["Show X-Face" gnus-article-display-x-face t]
2065               ["Show picons in From" gnus-treat-from-picon t]
2066               ["Show picons in mail headers" gnus-treat-mail-picon t]
2067               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2068               ("View as different encoding"
2069                ,@(gnus-summary-menu-split
2070                   (mapcar
2071                    (lambda (cs)
2072                      ;; Since easymenu under FSF Emacs doesn't allow lambda
2073                      ;; forms for menu commands, we should provide intern'ed
2074                      ;; function symbols.
2075                      (let ((command (intern (format "\
2076 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2077                        (fset command
2078                              `(lambda ()
2079                                 (interactive)
2080                                 (let ((gnus-summary-show-article-charset-alist
2081                                        '((1 . ,cs))))
2082                                   (gnus-summary-show-article 1))))
2083                        `[,(symbol-name cs) ,command t]))
2084                    (sort (if (fboundp 'coding-system-list)
2085                              (coding-system-list)
2086                            (mapcar 'car mm-mime-mule-charset-alist))
2087                          'string<)))))
2088              ("Washing"
2089               ("Remove Blanks"
2090                ["Leading" gnus-article-strip-leading-blank-lines t]
2091                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2092                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2093                ["All of the above" gnus-article-strip-blank-lines t]
2094                ["All" gnus-article-strip-all-blank-lines t]
2095                ["Leading space" gnus-article-strip-leading-space t]
2096                ["Trailing space" gnus-article-strip-trailing-space t]
2097                ["Leading space in headers"
2098                 gnus-article-remove-leading-whitespace t])
2099               ["Overstrike" gnus-article-treat-overstrike t]
2100               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2101               ["Emphasis" gnus-article-emphasize t]
2102               ["Word wrap" gnus-article-fill-cited-article t]
2103               ["Fill long lines" gnus-article-fill-long-lines t]
2104               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2105               ["CR" gnus-article-remove-cr t]
2106               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2107               ["Base64" gnus-article-de-base64-unreadable t]
2108               ["Rot 13" gnus-summary-caesar-message
2109                ,@(if (featurep 'xemacs) '(t)
2110                    '(:help "\"Caesar rotate\" article by 13"))]
2111               ["Morse decode" gnus-summary-morse-message t]
2112               ["Unix pipe..." gnus-summary-pipe-message t]
2113               ["Add buttons" gnus-article-add-buttons t]
2114               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2115               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2116               ["Verbose header" gnus-summary-verbose-headers t]
2117               ["Toggle header" gnus-summary-toggle-header t]
2118               ["Unfold headers" gnus-article-treat-unfold-headers t]
2119               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2120               ["Html" gnus-article-wash-html t]
2121               ["URLs" gnus-article-unsplit-urls t]
2122               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2123               ["HZ" gnus-article-decode-HZ t]
2124               ("(Outlook) Deuglify"
2125                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2126                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2127                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2128                ["Full (Outlook) deuglify"
2129                 gnus-article-outlook-deuglify-article t])
2130               )
2131              ("Output"
2132               ["Save in default format" gnus-summary-save-article
2133                ,@(if (featurep 'xemacs) '(t)
2134                    '(:help "Save article using default method"))]
2135               ["Save in file" gnus-summary-save-article-file
2136                ,@(if (featurep 'xemacs) '(t)
2137                    '(:help "Save article in file"))]
2138               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2139               ["Save in MH folder" gnus-summary-save-article-folder t]
2140               ["Save in VM folder" gnus-summary-save-article-vm t]
2141               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2142               ["Save body in file" gnus-summary-save-article-body-file t]
2143               ["Pipe through a filter" gnus-summary-pipe-output t]
2144               ["Add to SOUP packet" gnus-soup-add-article t]
2145               ["Print with Muttprint" gnus-summary-muttprint t]
2146               ["Print" gnus-summary-print-article t])
2147              ("Backend"
2148               ["Respool article..." gnus-summary-respool-article t]
2149               ["Move article..." gnus-summary-move-article
2150                (gnus-check-backend-function
2151                 'request-move-article gnus-newsgroup-name)]
2152               ["Copy article..." gnus-summary-copy-article t]
2153               ["Crosspost article..." gnus-summary-crosspost-article
2154                (gnus-check-backend-function
2155                 'request-replace-article gnus-newsgroup-name)]
2156               ["Import file..." gnus-summary-import-article t]
2157               ["Create article..." gnus-summary-create-article t]
2158               ["Check if posted" gnus-summary-article-posted-p t]
2159               ["Edit article" gnus-summary-edit-article
2160                (not (gnus-group-read-only-p))]
2161               ["Delete article" gnus-summary-delete-article
2162                (gnus-check-backend-function
2163                 'request-expire-articles gnus-newsgroup-name)]
2164               ["Query respool" gnus-summary-respool-query t]
2165               ["Trace respool" gnus-summary-respool-trace t]
2166               ["Delete expirable articles" gnus-summary-expire-articles-now
2167                (gnus-check-backend-function
2168                 'request-expire-articles gnus-newsgroup-name)])
2169              ("Extract"
2170               ["Uudecode" gnus-uu-decode-uu
2171                ,@(if (featurep 'xemacs) '(t)
2172                    '(:help "Decode uuencoded article(s)"))]
2173               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2174               ["Unshar" gnus-uu-decode-unshar t]
2175               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2176               ["Save" gnus-uu-decode-save t]
2177               ["Binhex" gnus-uu-decode-binhex t]
2178               ["Postscript" gnus-uu-decode-postscript t]
2179               ["All MIME parts" gnus-summary-save-parts t])
2180              ("Cache"
2181               ["Enter article" gnus-cache-enter-article t]
2182               ["Remove article" gnus-cache-remove-article t])
2183              ["Translate" gnus-article-babel t]
2184              ["Select article buffer" gnus-summary-select-article-buffer t]
2185              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2186              ["Isearch article..." gnus-summary-isearch-article t]
2187              ["Beginning of the article" gnus-summary-beginning-of-article t]
2188              ["End of the article" gnus-summary-end-of-article t]
2189              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2190              ["Fetch referenced articles" gnus-summary-refer-references t]
2191              ["Fetch current thread" gnus-summary-refer-thread t]
2192              ["Fetch article with id..." gnus-summary-refer-article t]
2193              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2194              ["Redisplay" gnus-summary-show-article t]
2195              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2196       (easy-menu-define
2197         gnus-summary-article-menu gnus-summary-mode-map ""
2198         (cons "Article" innards))
2199
2200       (if (not (keymapp gnus-summary-article-menu))
2201           (easy-menu-define
2202             gnus-article-commands-menu gnus-article-mode-map ""
2203             (cons "Commands" innards))
2204         ;; in Emacs, don't share menu.
2205         (setq gnus-article-commands-menu
2206               (copy-keymap gnus-summary-article-menu))
2207         (define-key gnus-article-mode-map [menu-bar commands]
2208           (cons "Commands" gnus-article-commands-menu))))
2209
2210     (easy-menu-define
2211       gnus-summary-thread-menu gnus-summary-mode-map ""
2212       '("Threads"
2213         ["Find all messages in thread" gnus-summary-refer-thread t]
2214         ["Toggle threading" gnus-summary-toggle-threads t]
2215         ["Hide threads" gnus-summary-hide-all-threads t]
2216         ["Show threads" gnus-summary-show-all-threads t]
2217         ["Hide thread" gnus-summary-hide-thread t]
2218         ["Show thread" gnus-summary-show-thread t]
2219         ["Go to next thread" gnus-summary-next-thread t]
2220         ["Go to previous thread" gnus-summary-prev-thread t]
2221         ["Go down thread" gnus-summary-down-thread t]
2222         ["Go up thread" gnus-summary-up-thread t]
2223         ["Top of thread" gnus-summary-top-thread t]
2224         ["Mark thread as read" gnus-summary-kill-thread t]
2225         ["Lower thread score" gnus-summary-lower-thread t]
2226         ["Raise thread score" gnus-summary-raise-thread t]
2227         ["Rethread current" gnus-summary-rethread-current t]))
2228
2229     (easy-menu-define
2230       gnus-summary-post-menu gnus-summary-mode-map ""
2231       `("Post"
2232         ["Send a message (mail or news)" gnus-summary-post-news
2233          ,@(if (featurep 'xemacs) '(t)
2234              '(:help "Post an article"))]
2235         ["Followup" gnus-summary-followup
2236          ,@(if (featurep 'xemacs) '(t)
2237              '(:help "Post followup to this article"))]
2238         ["Followup and yank" gnus-summary-followup-with-original
2239          ,@(if (featurep 'xemacs) '(t)
2240              '(:help "Post followup to this article, quoting its contents"))]
2241         ["Supersede article" gnus-summary-supersede-article t]
2242         ["Cancel article" gnus-summary-cancel-article
2243          ,@(if (featurep 'xemacs) '(t)
2244              '(:help "Cancel an article you posted"))]
2245         ["Reply" gnus-summary-reply t]
2246         ["Reply and yank" gnus-summary-reply-with-original t]
2247         ["Wide reply" gnus-summary-wide-reply t]
2248         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2249          ,@(if (featurep 'xemacs) '(t)
2250              '(:help "Mail a reply, quoting this article"))]
2251         ["Very wide reply" gnus-summary-very-wide-reply t]
2252         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2253          ,@(if (featurep 'xemacs) '(t)
2254              '(:help "Mail a very wide reply, quoting this article"))]
2255         ["Mail forward" gnus-summary-mail-forward t]
2256         ["Post forward" gnus-summary-post-forward t]
2257         ["Digest and mail" gnus-uu-digest-mail-forward t]
2258         ["Digest and post" gnus-uu-digest-post-forward t]
2259         ["Resend message" gnus-summary-resend-message t]
2260         ["Resend message edit" gnus-summary-resend-message-edit t]
2261         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2262         ["Send a mail" gnus-summary-mail-other-window t]
2263         ["Create a local message" gnus-summary-news-other-window t]
2264         ["Uuencode and post" gnus-uu-post-news
2265          ,@(if (featurep 'xemacs) '(t)
2266              '(:help "Post a uuencoded article"))]
2267         ["Followup via news" gnus-summary-followup-to-mail t]
2268         ["Followup via news and yank"
2269          gnus-summary-followup-to-mail-with-original t]
2270         ;;("Draft"
2271         ;;["Send" gnus-summary-send-draft t]
2272         ;;["Send bounced" gnus-resend-bounced-mail t])
2273         ))
2274
2275     (cond
2276      ((not (keymapp gnus-summary-post-menu))
2277       (setq gnus-article-post-menu gnus-summary-post-menu))
2278      ((not gnus-article-post-menu)
2279       ;; Don't share post menu.
2280       (setq gnus-article-post-menu
2281             (copy-keymap gnus-summary-post-menu))))
2282     (define-key gnus-article-mode-map [menu-bar post]
2283       (cons "Post" gnus-article-post-menu))
2284
2285     (easy-menu-define
2286       gnus-summary-misc-menu gnus-summary-mode-map ""
2287       `("Gnus"
2288         ("Mark Read"
2289          ["Mark as read" gnus-summary-mark-as-read-forward t]
2290          ["Mark same subject and select"
2291           gnus-summary-kill-same-subject-and-select t]
2292          ["Mark same subject" gnus-summary-kill-same-subject t]
2293          ["Catchup" gnus-summary-catchup
2294           ,@(if (featurep 'xemacs) '(t)
2295               '(:help "Mark unread articles in this group as read"))]
2296          ["Catchup all" gnus-summary-catchup-all t]
2297          ["Catchup to here" gnus-summary-catchup-to-here t]
2298          ["Catchup from here" gnus-summary-catchup-from-here t]
2299          ["Catchup region" gnus-summary-mark-region-as-read 
2300           (gnus-mark-active-p)]
2301          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2302         ("Mark Various"
2303          ["Tick" gnus-summary-tick-article-forward t]
2304          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2305          ["Remove marks" gnus-summary-clear-mark-forward t]
2306          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2307          ["Set bookmark" gnus-summary-set-bookmark t]
2308          ["Remove bookmark" gnus-summary-remove-bookmark t])
2309         ("Limit to"
2310          ["Marks..." gnus-summary-limit-to-marks t]
2311          ["Subject..." gnus-summary-limit-to-subject t]
2312          ["Author..." gnus-summary-limit-to-author t]
2313          ["Age..." gnus-summary-limit-to-age t]
2314          ["Extra..." gnus-summary-limit-to-extra t]
2315          ["Score..." gnus-summary-limit-to-score t]
2316          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2317          ["Unread" gnus-summary-limit-to-unread t]
2318          ["Unseen" gnus-summary-limit-to-unseen t]
2319          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2320          ["Next articles" gnus-summary-limit-to-articles t]
2321          ["Pop limit" gnus-summary-pop-limit t]
2322          ["Show dormant" gnus-summary-limit-include-dormant t]
2323          ["Hide childless dormant"
2324           gnus-summary-limit-exclude-childless-dormant t]
2325          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2326          ["Hide marked" gnus-summary-limit-exclude-marks t]
2327          ["Show expunged" gnus-summary-limit-include-expunged t])
2328         ("Process Mark"
2329          ["Set mark" gnus-summary-mark-as-processable t]
2330          ["Remove mark" gnus-summary-unmark-as-processable t]
2331          ["Remove all marks" gnus-summary-unmark-all-processable t]
2332          ["Mark above" gnus-uu-mark-over t]
2333          ["Mark series" gnus-uu-mark-series t]
2334          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2335          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2336          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2337          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2338          ["Mark all" gnus-uu-mark-all t]
2339          ["Mark buffer" gnus-uu-mark-buffer t]
2340          ["Mark sparse" gnus-uu-mark-sparse t]
2341          ["Mark thread" gnus-uu-mark-thread t]
2342          ["Unmark thread" gnus-uu-unmark-thread t]
2343          ("Process Mark Sets"
2344           ["Kill" gnus-summary-kill-process-mark t]
2345           ["Yank" gnus-summary-yank-process-mark
2346            gnus-newsgroup-process-stack]
2347           ["Save" gnus-summary-save-process-mark t]))
2348         ("Scroll article"
2349          ["Page forward" gnus-summary-next-page
2350           ,@(if (featurep 'xemacs) '(t)
2351               '(:help "Show next page of article"))]
2352          ["Page backward" gnus-summary-prev-page
2353           ,@(if (featurep 'xemacs) '(t)
2354               '(:help "Show previous page of article"))]
2355          ["Line forward" gnus-summary-scroll-up t])
2356         ("Move"
2357          ["Next unread article" gnus-summary-next-unread-article t]
2358          ["Previous unread article" gnus-summary-prev-unread-article t]
2359          ["Next article" gnus-summary-next-article t]
2360          ["Previous article" gnus-summary-prev-article t]
2361          ["Next unread subject" gnus-summary-next-unread-subject t]
2362          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2363          ["Next article same subject" gnus-summary-next-same-subject t]
2364          ["Previous article same subject" gnus-summary-prev-same-subject t]
2365          ["First unread article" gnus-summary-first-unread-article t]
2366          ["Best unread article" gnus-summary-best-unread-article t]
2367          ["Go to subject number..." gnus-summary-goto-subject t]
2368          ["Go to article number..." gnus-summary-goto-article t]
2369          ["Go to the last article" gnus-summary-goto-last-article t]
2370          ["Pop article off history" gnus-summary-pop-article t])
2371         ("Sort"
2372          ["Sort by number" gnus-summary-sort-by-number t]
2373          ["Sort by author" gnus-summary-sort-by-author t]
2374          ["Sort by subject" gnus-summary-sort-by-subject t]
2375          ["Sort by date" gnus-summary-sort-by-date t]
2376          ["Sort by score" gnus-summary-sort-by-score t]
2377          ["Sort by lines" gnus-summary-sort-by-lines t]
2378          ["Sort by characters" gnus-summary-sort-by-chars t]
2379          ["Randomize" gnus-summary-sort-by-random t]
2380          ["Original sort" gnus-summary-sort-by-original t])
2381         ("Help"
2382          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2383          ["Describe group" gnus-summary-describe-group t]
2384          ["Fetch charter" gnus-group-fetch-charter
2385           ,@(if (featurep 'xemacs) nil
2386               '(:help "Display the charter of the current group"))]
2387          ["Fetch control message" gnus-group-fetch-control
2388           ,@(if (featurep 'xemacs) nil
2389               '(:help "Display the archived control message for the current group"))]
2390          ["Read manual" gnus-info-find-node t])
2391         ("Modes"
2392          ["Pick and read" gnus-pick-mode t]
2393          ["Binary" gnus-binary-mode t])
2394         ("Regeneration"
2395          ["Regenerate" gnus-summary-prepare t]
2396          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2397          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2398          ["Toggle threading" gnus-summary-toggle-threads t])
2399         ["See old articles" gnus-summary-insert-old-articles t]
2400         ["See new articles" gnus-summary-insert-new-articles t]
2401         ["Filter articles..." gnus-summary-execute-command t]
2402         ["Run command on subjects..." gnus-summary-universal-argument t]
2403         ["Search articles forward..." gnus-summary-search-article-forward t]
2404         ["Search articles backward..." gnus-summary-search-article-backward t]
2405         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2406         ["Expand window" gnus-summary-expand-window t]
2407         ["Expire expirable articles" gnus-summary-expire-articles
2408          (gnus-check-backend-function
2409           'request-expire-articles gnus-newsgroup-name)]
2410         ["Edit local kill file" gnus-summary-edit-local-kill t]
2411         ["Edit main kill file" gnus-summary-edit-global-kill t]
2412         ["Edit group parameters" gnus-summary-edit-parameters t]
2413         ["Customize group parameters" gnus-summary-customize-parameters t]
2414         ["Send a bug report" gnus-bug t]
2415         ("Exit"
2416          ["Catchup and exit" gnus-summary-catchup-and-exit
2417           ,@(if (featurep 'xemacs) '(t)
2418               '(:help "Mark unread articles in this group as read, then exit"))]
2419          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2420          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2421          ["Exit group" gnus-summary-exit
2422           ,@(if (featurep 'xemacs) '(t)
2423               '(:help "Exit current group, return to group selection mode"))]
2424          ["Exit group without updating" gnus-summary-exit-no-update t]
2425          ["Exit and goto next group" gnus-summary-next-group t]
2426          ["Exit and goto prev group" gnus-summary-prev-group t]
2427          ["Reselect group" gnus-summary-reselect-current-group t]
2428          ["Rescan group" gnus-summary-rescan-group t]
2429          ["Update dribble" gnus-summary-save-newsrc t])))
2430
2431     (gnus-run-hooks 'gnus-summary-menu-hook)))
2432
2433 (defvar gnus-summary-tool-bar-map nil)
2434
2435 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2436 (defun gnus-summary-make-tool-bar ()
2437   (if (and (fboundp 'tool-bar-add-item-from-menu)
2438            (default-value 'tool-bar-mode)
2439            (not gnus-summary-tool-bar-map))
2440       (setq gnus-summary-tool-bar-map
2441             (let ((tool-bar-map (make-sparse-keymap))
2442                   (load-path (mm-image-load-path)))
2443               (tool-bar-add-item-from-menu
2444                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2445               (tool-bar-add-item-from-menu
2446                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2447               (tool-bar-add-item-from-menu
2448                'gnus-summary-post-news "post" gnus-summary-mode-map)
2449               (tool-bar-add-item-from-menu
2450                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2451               (tool-bar-add-item-from-menu
2452                'gnus-summary-followup "followup" gnus-summary-mode-map)
2453               (tool-bar-add-item-from-menu
2454                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2455               (tool-bar-add-item-from-menu
2456                'gnus-summary-reply "reply" gnus-summary-mode-map)
2457               (tool-bar-add-item-from-menu
2458                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2459               (tool-bar-add-item-from-menu
2460                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2461               (tool-bar-add-item-from-menu
2462                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2463               (tool-bar-add-item-from-menu
2464                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2465               (tool-bar-add-item-from-menu
2466                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2467               (tool-bar-add-item-from-menu
2468                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2469               (tool-bar-add-item-from-menu
2470                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2471               (tool-bar-add-item-from-menu
2472                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2473               tool-bar-map)))
2474   (if gnus-summary-tool-bar-map
2475       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2476
2477 (defun gnus-score-set-default (var value)
2478   "A version of set that updates the GNU Emacs menu-bar."
2479   (set var value)
2480   ;; It is the message that forces the active status to be updated.
2481   (message ""))
2482
2483 (defun gnus-make-score-map (type)
2484   "Make a summary score map of type TYPE."
2485   (if t
2486       nil
2487     (let ((headers '(("author" "from" string)
2488                      ("subject" "subject" string)
2489                      ("article body" "body" string)
2490                      ("article head" "head" string)
2491                      ("xref" "xref" string)
2492                      ("extra header" "extra" string)
2493                      ("lines" "lines" number)
2494                      ("followups to author" "followup" string)))
2495           (types '((number ("less than" <)
2496                            ("greater than" >)
2497                            ("equal" =))
2498                    (string ("substring" s)
2499                            ("exact string" e)
2500                            ("fuzzy string" f)
2501                            ("regexp" r))))
2502           (perms '(("temporary" (current-time-string))
2503                    ("permanent" nil)
2504                    ("immediate" now)))
2505           header)
2506       (list
2507        (apply
2508         'nconc
2509         (list
2510          (if (eq type 'lower)
2511              "Lower score"
2512            "Increase score"))
2513         (let (outh)
2514           (while headers
2515             (setq header (car headers))
2516             (setq outh
2517                   (cons
2518                    (apply
2519                     'nconc
2520                     (list (car header))
2521                     (let ((ts (cdr (assoc (nth 2 header) types)))
2522                           outt)
2523                       (while ts
2524                         (setq outt
2525                               (cons
2526                                (apply
2527                                 'nconc
2528                                 (list (caar ts))
2529                                 (let ((ps perms)
2530                                       outp)
2531                                   (while ps
2532                                     (setq outp
2533                                           (cons
2534                                            (vector
2535                                             (caar ps)
2536                                             (list
2537                                              'gnus-summary-score-entry
2538                                              (nth 1 header)
2539                                              (if (or (string= (nth 1 header)
2540                                                               "head")
2541                                                      (string= (nth 1 header)
2542                                                               "body"))
2543                                                  ""
2544                                                (list 'gnus-summary-header
2545                                                      (nth 1 header)))
2546                                              (list 'quote (nth 1 (car ts)))
2547                                              (list 'gnus-score-delta-default
2548                                                    nil)
2549                                              (nth 1 (car ps))
2550                                              t)
2551                                             t)
2552                                            outp))
2553                                     (setq ps (cdr ps)))
2554                                   (list (nreverse outp))))
2555                                outt))
2556                         (setq ts (cdr ts)))
2557                       (list (nreverse outt))))
2558                    outh))
2559             (setq headers (cdr headers)))
2560           (list (nreverse outh))))))))
2561
2562 \f
2563
2564 (defun gnus-summary-mode (&optional group)
2565   "Major mode for reading articles.
2566
2567 All normal editing commands are switched off.
2568 \\<gnus-summary-mode-map>
2569 Each line in this buffer represents one article.  To read an
2570 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2571 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2572 respectively.
2573
2574 You can also post articles and send mail from this buffer.  To
2575 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2576 of an article, type `\\[gnus-summary-reply]'.
2577
2578 There are approx. one gazillion commands you can execute in this
2579 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2580
2581 The following commands are available:
2582
2583 \\{gnus-summary-mode-map}"
2584   (interactive)
2585   (kill-all-local-variables)
2586   (when (gnus-visual-p 'summary-menu 'menu)
2587     (gnus-summary-make-menu-bar)
2588     (gnus-summary-make-tool-bar))
2589   (gnus-summary-make-local-variables)
2590   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2591     (gnus-summary-make-local-variables))
2592   (gnus-make-thread-indent-array)
2593   (gnus-simplify-mode-line)
2594   (setq major-mode 'gnus-summary-mode)
2595   (setq mode-name "Summary")
2596   (make-local-variable 'minor-mode-alist)
2597   (use-local-map gnus-summary-mode-map)
2598   (buffer-disable-undo)
2599   (setq buffer-read-only t)             ;Disable modification
2600   (setq truncate-lines t)
2601   (setq selective-display t)
2602   (setq selective-display-ellipses t)   ;Display `...'
2603   (gnus-summary-set-display-table)
2604   (gnus-set-default-directory)
2605   (setq gnus-newsgroup-name group)
2606   (make-local-variable 'gnus-summary-line-format)
2607   (make-local-variable 'gnus-summary-line-format-spec)
2608   (make-local-variable 'gnus-summary-dummy-line-format)
2609   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2610   (make-local-variable 'gnus-summary-mark-positions)
2611   (make-local-hook 'pre-command-hook)
2612   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2613   (gnus-run-hooks 'gnus-summary-mode-hook)
2614   (turn-on-gnus-mailing-list-mode)
2615   (mm-enable-multibyte)
2616   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2617   (gnus-update-summary-mark-positions))
2618
2619 (defun gnus-summary-make-local-variables ()
2620   "Make all the local summary buffer variables."
2621   (let (global)
2622     (dolist (local gnus-summary-local-variables)
2623       (if (consp local)
2624           (progn
2625             (if (eq (cdr local) 'global)
2626                 ;; Copy the global value of the variable.
2627                 (setq global (symbol-value (car local)))
2628               ;; Use the value from the list.
2629               (setq global (eval (cdr local))))
2630             (set (make-local-variable (car local)) global))
2631         ;; Simple nil-valued local variable.
2632         (set (make-local-variable local) nil)))))
2633
2634 (defun gnus-summary-clear-local-variables ()
2635   (let ((locals gnus-summary-local-variables))
2636     (while locals
2637       (if (consp (car locals))
2638           (and (vectorp (caar locals))
2639                (set (caar locals) nil))
2640         (and (vectorp (car locals))
2641              (set (car locals) nil)))
2642       (setq locals (cdr locals)))))
2643
2644 ;; Summary data functions.
2645
2646 (defmacro gnus-data-number (data)
2647   `(car ,data))
2648
2649 (defmacro gnus-data-set-number (data number)
2650   `(setcar ,data ,number))
2651
2652 (defmacro gnus-data-mark (data)
2653   `(nth 1 ,data))
2654
2655 (defmacro gnus-data-set-mark (data mark)
2656   `(setcar (nthcdr 1 ,data) ,mark))
2657
2658 (defmacro gnus-data-pos (data)
2659   `(nth 2 ,data))
2660
2661 (defmacro gnus-data-set-pos (data pos)
2662   `(setcar (nthcdr 2 ,data) ,pos))
2663
2664 (defmacro gnus-data-header (data)
2665   `(nth 3 ,data))
2666
2667 (defmacro gnus-data-set-header (data header)
2668   `(setf (nth 3 ,data) ,header))
2669
2670 (defmacro gnus-data-level (data)
2671   `(nth 4 ,data))
2672
2673 (defmacro gnus-data-unread-p (data)
2674   `(= (nth 1 ,data) gnus-unread-mark))
2675
2676 (defmacro gnus-data-read-p (data)
2677   `(/= (nth 1 ,data) gnus-unread-mark))
2678
2679 (defmacro gnus-data-pseudo-p (data)
2680   `(consp (nth 3 ,data)))
2681
2682 (defmacro gnus-data-find (number)
2683   `(assq ,number gnus-newsgroup-data))
2684
2685 (defmacro gnus-data-find-list (number &optional data)
2686   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2687      (memq (assq ,number bdata)
2688            bdata)))
2689
2690 (defmacro gnus-data-make (number mark pos header level)
2691   `(list ,number ,mark ,pos ,header ,level))
2692
2693 (defun gnus-data-enter (after-article number mark pos header level offset)
2694   (let ((data (gnus-data-find-list after-article)))
2695     (unless data
2696       (error "No such article: %d" after-article))
2697     (setcdr data (cons (gnus-data-make number mark pos header level)
2698                        (cdr data)))
2699     (setq gnus-newsgroup-data-reverse nil)
2700     (gnus-data-update-list (cddr data) offset)))
2701
2702 (defun gnus-data-enter-list (after-article list &optional offset)
2703   (when list
2704     (let ((data (and after-article (gnus-data-find-list after-article)))
2705           (ilist list))
2706       (if (not (or data
2707                    after-article))
2708           (let ((odata gnus-newsgroup-data))
2709             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2710             (when offset
2711               (gnus-data-update-list odata offset)))
2712       ;; Find the last element in the list to be spliced into the main
2713         ;; list.
2714         (while (cdr list)
2715           (setq list (cdr list)))
2716         (if (not data)
2717             (progn
2718               (setcdr list gnus-newsgroup-data)
2719               (setq gnus-newsgroup-data ilist)
2720               (when offset
2721                 (gnus-data-update-list (cdr list) offset)))
2722           (setcdr list (cdr data))
2723           (setcdr data ilist)
2724           (when offset
2725             (gnus-data-update-list (cdr list) offset))))
2726       (setq gnus-newsgroup-data-reverse nil))))
2727
2728 (defun gnus-data-remove (article &optional offset)
2729   (let ((data gnus-newsgroup-data))
2730     (if (= (gnus-data-number (car data)) article)
2731         (progn
2732           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2733                 gnus-newsgroup-data-reverse nil)
2734           (when offset
2735             (gnus-data-update-list gnus-newsgroup-data offset)))
2736       (while (cdr data)
2737         (when (= (gnus-data-number (cadr data)) article)
2738           (setcdr data (cddr data))
2739           (when offset
2740             (gnus-data-update-list (cdr data) offset))
2741           (setq data nil
2742                 gnus-newsgroup-data-reverse nil))
2743         (setq data (cdr data))))))
2744
2745 (defmacro gnus-data-list (backward)
2746   `(if ,backward
2747        (or gnus-newsgroup-data-reverse
2748            (setq gnus-newsgroup-data-reverse
2749                  (reverse gnus-newsgroup-data)))
2750      gnus-newsgroup-data))
2751
2752 (defun gnus-data-update-list (data offset)
2753   "Add OFFSET to the POS of all data entries in DATA."
2754   (setq gnus-newsgroup-data-reverse nil)
2755   (while data
2756     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2757     (setq data (cdr data))))
2758
2759 (defun gnus-summary-article-pseudo-p (article)
2760   "Say whether this article is a pseudo article or not."
2761   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2762
2763 (defmacro gnus-summary-article-sparse-p (article)
2764   "Say whether this article is a sparse article or not."
2765   `(memq ,article gnus-newsgroup-sparse))
2766
2767 (defmacro gnus-summary-article-ancient-p (article)
2768   "Say whether this article is a sparse article or not."
2769   `(memq ,article gnus-newsgroup-ancient))
2770
2771 (defun gnus-article-parent-p (number)
2772   "Say whether this article is a parent or not."
2773   (let ((data (gnus-data-find-list number)))
2774     (and (cdr data)              ; There has to be an article after...
2775          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2776             (gnus-data-level (nth 1 data))))))
2777
2778 (defun gnus-article-children (number)
2779   "Return a list of all children to NUMBER."
2780   (let* ((data (gnus-data-find-list number))
2781          (level (gnus-data-level (car data)))
2782          children)
2783     (setq data (cdr data))
2784     (while (and data
2785                 (= (gnus-data-level (car data)) (1+ level)))
2786       (push (gnus-data-number (car data)) children)
2787       (setq data (cdr data)))
2788     children))
2789
2790 (defmacro gnus-summary-skip-intangible ()
2791   "If the current article is intangible, then jump to a different article."
2792   '(let ((to (get-text-property (point) 'gnus-intangible)))
2793      (and to (gnus-summary-goto-subject to))))
2794
2795 (defmacro gnus-summary-article-intangible-p ()
2796   "Say whether this article is intangible or not."
2797   '(get-text-property (point) 'gnus-intangible))
2798
2799 (defun gnus-article-read-p (article)
2800   "Say whether ARTICLE is read or not."
2801   (not (or (memq article gnus-newsgroup-marked)
2802            (memq article gnus-newsgroup-spam-marked)
2803            (memq article gnus-newsgroup-unreads)
2804            (memq article gnus-newsgroup-unselected)
2805            (memq article gnus-newsgroup-dormant))))
2806
2807 ;; Some summary mode macros.
2808
2809 (defmacro gnus-summary-article-number ()
2810   "The article number of the article on the current line.
2811 If there isn't an article number here, then we return the current
2812 article number."
2813   '(progn
2814      (gnus-summary-skip-intangible)
2815      (or (get-text-property (point) 'gnus-number)
2816          (gnus-summary-last-subject))))
2817
2818 (defmacro gnus-summary-article-header (&optional number)
2819   "Return the header of article NUMBER."
2820   `(gnus-data-header (gnus-data-find
2821                       ,(or number '(gnus-summary-article-number)))))
2822
2823 (defmacro gnus-summary-thread-level (&optional number)
2824   "Return the level of thread that starts with article NUMBER."
2825   `(if (and (eq gnus-summary-make-false-root 'dummy)
2826             (get-text-property (point) 'gnus-intangible))
2827        0
2828      (gnus-data-level (gnus-data-find
2829                        ,(or number '(gnus-summary-article-number))))))
2830
2831 (defmacro gnus-summary-article-mark (&optional number)
2832   "Return the mark of article NUMBER."
2833   `(gnus-data-mark (gnus-data-find
2834                     ,(or number '(gnus-summary-article-number)))))
2835
2836 (defmacro gnus-summary-article-pos (&optional number)
2837   "Return the position of the line of article NUMBER."
2838   `(gnus-data-pos (gnus-data-find
2839                    ,(or number '(gnus-summary-article-number)))))
2840
2841 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2842 (defmacro gnus-summary-article-subject (&optional number)
2843   "Return current subject string or nil if nothing."
2844   `(let ((headers
2845           ,(if number
2846                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2847              '(gnus-data-header (assq (gnus-summary-article-number)
2848                                       gnus-newsgroup-data)))))
2849      (and headers
2850           (vectorp headers)
2851           (mail-header-subject headers))))
2852
2853 (defmacro gnus-summary-article-score (&optional number)
2854   "Return current article score."
2855   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2856                   gnus-newsgroup-scored))
2857        gnus-summary-default-score 0))
2858
2859 (defun gnus-summary-article-children (&optional number)
2860   "Return a list of article numbers that are children of article NUMBER."
2861   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2862          (level (gnus-data-level (car data)))
2863          l children)
2864     (while (and (setq data (cdr data))
2865                 (> (setq l (gnus-data-level (car data))) level))
2866       (and (= (1+ level) l)
2867            (push (gnus-data-number (car data))
2868                  children)))
2869     (nreverse children)))
2870
2871 (defun gnus-summary-article-parent (&optional number)
2872   "Return the article number of the parent of article NUMBER."
2873   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2874                                     (gnus-data-list t)))
2875          (level (gnus-data-level (car data))))
2876     (if (zerop level)
2877         ()                              ; This is a root.
2878       ;; We search until we find an article with a level less than
2879       ;; this one.  That function has to be the parent.
2880       (while (and (setq data (cdr data))
2881                   (not (< (gnus-data-level (car data)) level))))
2882       (and data (gnus-data-number (car data))))))
2883
2884 (defun gnus-unread-mark-p (mark)
2885   "Say whether MARK is the unread mark."
2886   (= mark gnus-unread-mark))
2887
2888 (defun gnus-read-mark-p (mark)
2889   "Say whether MARK is one of the marks that mark as read.
2890 This is all marks except unread, ticked, dormant, and expirable."
2891   (not (or (= mark gnus-unread-mark)
2892            (= mark gnus-ticked-mark)
2893            (= mark gnus-spam-mark)
2894            (= mark gnus-dormant-mark)
2895            (= mark gnus-expirable-mark))))
2896
2897 (defmacro gnus-article-mark (number)
2898   "Return the MARK of article NUMBER.
2899 This macro should only be used when computing the mark the \"first\"
2900 time; i.e., when generating the summary lines.  After that,
2901 `gnus-summary-article-mark' should be used to examine the
2902 marks of articles."
2903   `(cond
2904     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2905     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2906     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2907     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2908     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2909     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2910     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2911     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2912            gnus-ancient-mark))))
2913
2914 ;; Saving hidden threads.
2915
2916 (defmacro gnus-save-hidden-threads (&rest forms)
2917   "Save hidden threads, eval FORMS, and restore the hidden threads."
2918   (let ((config (make-symbol "config")))
2919     `(let ((,config (gnus-hidden-threads-configuration)))
2920        (unwind-protect
2921            (save-excursion
2922              ,@forms)
2923          (gnus-restore-hidden-threads-configuration ,config)))))
2924 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2925 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2926
2927 (defun gnus-data-compute-positions ()
2928   "Compute the positions of all articles."
2929   (setq gnus-newsgroup-data-reverse nil)
2930   (let ((data gnus-newsgroup-data))
2931     (save-excursion
2932       (gnus-save-hidden-threads
2933         (gnus-summary-show-all-threads)
2934         (goto-char (point-min))
2935         (while data
2936           (while (get-text-property (point) 'gnus-intangible)
2937             (forward-line 1))
2938           (gnus-data-set-pos (car data) (+ (point) 3))
2939           (setq data (cdr data))
2940           (forward-line 1))))))
2941
2942 (defun gnus-hidden-threads-configuration ()
2943   "Return the current hidden threads configuration."
2944   (save-excursion
2945     (let (config)
2946       (goto-char (point-min))
2947       (while (search-forward "\r" nil t)
2948         (push (1- (point)) config))
2949       config)))
2950
2951 (defun gnus-restore-hidden-threads-configuration (config)
2952   "Restore hidden threads configuration from CONFIG."
2953   (save-excursion
2954     (let (point buffer-read-only)
2955       (while (setq point (pop config))
2956         (when (and (< point (point-max))
2957                    (goto-char point)
2958                    (eq (char-after) ?\n))
2959           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2960
2961 ;; Various summary mode internalish functions.
2962
2963 (defun gnus-mouse-pick-article (e)
2964   (interactive "e")
2965   (mouse-set-point e)
2966   (gnus-summary-next-page nil t))
2967
2968 (defun gnus-summary-set-display-table ()
2969   "Change the display table.
2970 Odd characters have a tendency to mess
2971 up nicely formatted displays - we make all possible glyphs
2972 display only a single character."
2973
2974   ;; We start from the standard display table, if any.
2975   (let ((table (or (copy-sequence standard-display-table)
2976                    (make-display-table)))
2977         (i 32))
2978     ;; Nix out all the control chars...
2979     (while (>= (setq i (1- i)) 0)
2980       (aset table i [??]))
2981    ;; ... but not newline and cr, of course.  (cr is necessary for the
2982     ;; selective display).
2983     (aset table ?\n nil)
2984     (aset table ?\r nil)
2985     ;; We keep TAB as well.
2986     (aset table ?\t nil)
2987     ;; We nix out any glyphs over 126 that are not set already.
2988     (let ((i 256))
2989       (while (>= (setq i (1- i)) 127)
2990         ;; Only modify if the entry is nil.
2991         (unless (aref table i)
2992           (aset table i [??]))))
2993     (setq buffer-display-table table)))
2994
2995 (defun gnus-summary-set-article-display-arrow (pos)
2996   "Update the overlay arrow to point to line at position POS."
2997   (when (and gnus-summary-display-arrow
2998              (boundp 'overlay-arrow-position)
2999              (boundp 'overlay-arrow-string))
3000     (save-excursion
3001       (goto-char pos)
3002       (beginning-of-line)
3003       (unless overlay-arrow-position
3004         (setq overlay-arrow-position (make-marker)))
3005       (setq overlay-arrow-string "=>"
3006             overlay-arrow-position (set-marker overlay-arrow-position
3007                                                (point)
3008                                                (current-buffer))))))
3009
3010 (defun gnus-summary-buffer-name (group)
3011   "Return the summary buffer name of GROUP."
3012   (concat "*Summary " (gnus-group-decoded-name group) "*"))
3013
3014 (defun gnus-summary-setup-buffer (group)
3015   "Initialize summary buffer."
3016   (let ((buffer (gnus-summary-buffer-name group))
3017         (dead-name (concat "*Dead Summary "
3018                            (gnus-group-decoded-name group) "*")))
3019     ;; If a dead summary buffer exists, we kill it.
3020     (when (gnus-buffer-live-p dead-name)
3021       (gnus-kill-buffer dead-name))
3022     (if (get-buffer buffer)
3023         (progn
3024           (set-buffer buffer)
3025           (setq gnus-summary-buffer (current-buffer))
3026           (not gnus-newsgroup-prepared))
3027       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3028       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3029       (gnus-summary-mode group)
3030       (when gnus-carpal
3031         (gnus-carpal-setup-buffer 'summary))
3032       (unless gnus-single-article-buffer
3033         (make-local-variable 'gnus-article-buffer)
3034         (make-local-variable 'gnus-article-current)
3035         (make-local-variable 'gnus-original-article-buffer))
3036       (setq gnus-newsgroup-name group)
3037       ;; Set any local variables in the group parameters.
3038       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3039       t)))
3040
3041 (defun gnus-set-global-variables ()
3042   "Set the global equivalents of the buffer-local variables.
3043 They are set to the latest values they had.  These reflect the summary
3044 buffer that was in action when the last article was fetched."
3045   (when (eq major-mode 'gnus-summary-mode)
3046     (setq gnus-summary-buffer (current-buffer))
3047     (let ((name gnus-newsgroup-name)
3048           (marked gnus-newsgroup-marked)
3049           (spam gnus-newsgroup-spam-marked)
3050           (unread gnus-newsgroup-unreads)
3051           (headers gnus-current-headers)
3052           (data gnus-newsgroup-data)
3053           (summary gnus-summary-buffer)
3054           (article-buffer gnus-article-buffer)
3055           (original gnus-original-article-buffer)
3056           (gac gnus-article-current)
3057           (reffed gnus-reffed-article-number)
3058           (score-file gnus-current-score-file)
3059           (default-charset gnus-newsgroup-charset)
3060           vlist)
3061       (let ((locals gnus-newsgroup-variables))
3062         (while locals
3063           (if (consp (car locals))
3064               (push (eval (caar locals)) vlist)
3065             (push (eval (car locals)) vlist))
3066           (setq locals (cdr locals)))
3067         (setq vlist (nreverse vlist)))
3068       (save-excursion
3069         (set-buffer gnus-group-buffer)
3070         (setq gnus-newsgroup-name name
3071               gnus-newsgroup-marked marked
3072               gnus-newsgroup-spam-marked spam
3073               gnus-newsgroup-unreads unread
3074               gnus-current-headers headers
3075               gnus-newsgroup-data data
3076               gnus-article-current gac
3077               gnus-summary-buffer summary
3078               gnus-article-buffer article-buffer
3079               gnus-original-article-buffer original
3080               gnus-reffed-article-number reffed
3081               gnus-current-score-file score-file
3082               gnus-newsgroup-charset default-charset)
3083         (let ((locals gnus-newsgroup-variables))
3084           (while locals
3085             (if (consp (car locals))
3086                 (set (caar locals) (pop vlist))
3087               (set (car locals) (pop vlist)))
3088             (setq locals (cdr locals))))
3089         ;; The article buffer also has local variables.
3090         (when (gnus-buffer-live-p gnus-article-buffer)
3091           (set-buffer gnus-article-buffer)
3092           (setq gnus-summary-buffer summary))))))
3093
3094 (defun gnus-summary-article-unread-p (article)
3095   "Say whether ARTICLE is unread or not."
3096   (memq article gnus-newsgroup-unreads))
3097
3098 (defun gnus-summary-first-article-p (&optional article)
3099   "Return whether ARTICLE is the first article in the buffer."
3100   (if (not (setq article (or article (gnus-summary-article-number))))
3101       nil
3102     (eq article (caar gnus-newsgroup-data))))
3103
3104 (defun gnus-summary-last-article-p (&optional article)
3105   "Return whether ARTICLE is the last article in the buffer."
3106   (if (not (setq article (or article (gnus-summary-article-number))))
3107       ;; All non-existent numbers are the last article.  :-)
3108       t
3109     (not (cdr (gnus-data-find-list article)))))
3110
3111 (defun gnus-make-thread-indent-array ()
3112   (let ((n 200))
3113     (unless (and gnus-thread-indent-array
3114                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3115       (setq gnus-thread-indent-array (make-vector 201 "")
3116             gnus-thread-indent-array-level gnus-thread-indent-level)
3117       (while (>= n 0)
3118         (aset gnus-thread-indent-array n
3119               (make-string (* n gnus-thread-indent-level) ? ))
3120         (setq n (1- n))))))
3121
3122 (defun gnus-update-summary-mark-positions ()
3123   "Compute where the summary marks are to go."
3124   (save-excursion
3125     (when (gnus-buffer-exists-p gnus-summary-buffer)
3126       (set-buffer gnus-summary-buffer))
3127     (let ((gnus-replied-mark 129)
3128           (gnus-score-below-mark 130)
3129           (gnus-score-over-mark 130)
3130           (gnus-undownloaded-mark 131)
3131           (spec gnus-summary-line-format-spec)
3132           gnus-visual pos)
3133       (save-excursion
3134         (gnus-set-work-buffer)
3135         (let ((gnus-summary-line-format-spec spec)
3136               (gnus-newsgroup-downloadable '(0)))
3137           (gnus-summary-insert-line
3138            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3139            0 nil t 128 t nil "" nil 1)
3140           (goto-char (point-min))
3141           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3142                                              (- (point) (point-min) 1)))))
3143           (goto-char (point-min))
3144           (push (cons 'replied (and (search-forward "\201" nil t)
3145                                     (- (point) (point-min) 1)))
3146                 pos)
3147           (goto-char (point-min))
3148           (push (cons 'score (and (search-forward "\202" nil t)
3149                                   (- (point) (point-min) 1)))
3150                 pos)
3151           (goto-char (point-min))
3152           (push (cons 'download
3153                       (and (search-forward "\203" nil t)
3154                            (- (point) (point-min) 1)))
3155                 pos)))
3156       (setq gnus-summary-mark-positions pos))))
3157
3158 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3159   "Insert a dummy root in the summary buffer."
3160   (beginning-of-line)
3161   (gnus-add-text-properties
3162    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3163    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3164
3165 (defun gnus-summary-extract-address-component (from)
3166   (or (car (funcall gnus-extract-address-components from))
3167       from))
3168
3169 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3170   (let ((mail-parse-charset gnus-newsgroup-charset)
3171         ; Is it really necessary to do this next part for each summary line?
3172         ; Luckily, doesn't seem to slow things down much.
3173         (mail-parse-ignored-charsets
3174          (save-excursion (set-buffer gnus-summary-buffer)
3175                          gnus-newsgroup-ignored-charsets)))
3176     (or
3177      (and gnus-ignored-from-addresses
3178           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3179           (let ((extra-headers (mail-header-extra header))
3180                 to
3181                 newsgroups)
3182             (cond
3183              ((setq to (cdr (assq 'To extra-headers)))
3184               (concat "-> "
3185                       (inline
3186                         (gnus-summary-extract-address-component
3187                          (funcall gnus-decode-encoded-word-function to)))))
3188              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3189               (concat "=> " newsgroups)))))
3190      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3191
3192 (defun gnus-summary-insert-line (gnus-tmp-header
3193                                  gnus-tmp-level gnus-tmp-current
3194                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3195                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3196                                  &optional gnus-tmp-dummy gnus-tmp-score
3197                                  gnus-tmp-process)
3198   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3199          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3200          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3201          (gnus-tmp-score-char
3202           (if (or (null gnus-summary-default-score)
3203                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3204                       gnus-summary-zcore-fuzz))
3205               ?                         ;Whitespace
3206             (if (< gnus-tmp-score gnus-summary-default-score)
3207                 gnus-score-below-mark gnus-score-over-mark)))
3208          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3209          (gnus-tmp-replied
3210           (cond (gnus-tmp-process gnus-process-mark)
3211                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3212                  gnus-cached-mark)
3213                 (gnus-tmp-replied gnus-replied-mark)
3214                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3215                  gnus-forwarded-mark)
3216                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3217                  gnus-saved-mark)
3218                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3219                  gnus-recent-mark)
3220                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3221                  gnus-unseen-mark)
3222                 (t gnus-no-mark)))
3223          (gnus-tmp-downloaded
3224           (cond (undownloaded 
3225                  gnus-undownloaded-mark)
3226                 (gnus-newsgroup-agentized
3227                  gnus-downloaded-mark)
3228                 (t
3229                  gnus-no-mark)))
3230          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3231          (gnus-tmp-name
3232           (cond
3233            ((string-match "<[^>]+> *$" gnus-tmp-from)
3234             (let ((beg (match-beginning 0)))
3235               (or (and (string-match "^\".+\"" gnus-tmp-from)
3236                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3237                   (substring gnus-tmp-from 0 beg))))
3238            ((string-match "(.+)" gnus-tmp-from)
3239             (substring gnus-tmp-from
3240                        (1+ (match-beginning 0)) (1- (match-end 0))))
3241            (t gnus-tmp-from)))
3242          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3243          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3244          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3245          (buffer-read-only nil))
3246     (when (string= gnus-tmp-name "")
3247       (setq gnus-tmp-name gnus-tmp-from))
3248     (unless (numberp gnus-tmp-lines)
3249       (setq gnus-tmp-lines -1))
3250     (if (= gnus-tmp-lines -1)
3251         (setq gnus-tmp-lines "?")
3252       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines))) 
3253       (gnus-put-text-property
3254      (point)
3255      (progn (eval gnus-summary-line-format-spec) (point))
3256        'gnus-number gnus-tmp-number)
3257     (when (gnus-visual-p 'summary-highlight 'highlight)
3258       (forward-line -1)
3259       (gnus-run-hooks 'gnus-summary-update-hook)
3260       (forward-line 1))))
3261
3262 (defun gnus-summary-update-line (&optional dont-update)
3263   "Update summary line after change."
3264   (when (and gnus-summary-default-score
3265              (not gnus-summary-inhibit-highlight))
3266     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3267            (article (gnus-summary-article-number))
3268            (score (gnus-summary-article-score article)))
3269       (unless dont-update
3270         (if (and gnus-summary-mark-below
3271                  (< (gnus-summary-article-score)
3272                     gnus-summary-mark-below))
3273             ;; This article has a low score, so we mark it as read.
3274             (when (memq article gnus-newsgroup-unreads)
3275               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3276           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3277             ;; This article was previously marked as read on account
3278             ;; of a low score, but now it has risen, so we mark it as
3279             ;; unread.
3280             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3281         (gnus-summary-update-mark
3282          (if (or (null gnus-summary-default-score)
3283                  (<= (abs (- score gnus-summary-default-score))
3284                      gnus-summary-zcore-fuzz))
3285              ?                          ;Whitespace
3286            (if (< score gnus-summary-default-score)
3287                gnus-score-below-mark gnus-score-over-mark))
3288          'score))
3289       ;; Do visual highlighting.
3290       (when (gnus-visual-p 'summary-highlight 'highlight)
3291         (gnus-run-hooks 'gnus-summary-update-hook)))))
3292
3293 (defvar gnus-tmp-new-adopts nil)
3294
3295 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3296   "Return the number of articles in THREAD.
3297 This may be 0 in some cases -- if none of the articles in
3298 the thread are to be displayed."
3299   (let* ((number
3300          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3301           (cond
3302            ((not (listp thread))
3303             1)
3304            ((and (consp thread) (cdr thread))
3305             (apply
3306              '+ 1 (mapcar
3307                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3308            ((null thread)
3309             1)
3310            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3311             1)
3312            (t 0))))
3313     (when (and level (zerop level) gnus-tmp-new-adopts)
3314       (incf number
3315             (apply '+ (mapcar
3316                        'gnus-summary-number-of-articles-in-thread
3317                        gnus-tmp-new-adopts))))
3318     (if char
3319         (if (> number 1) gnus-not-empty-thread-mark
3320           gnus-empty-thread-mark)
3321       number)))
3322
3323 (defsubst gnus-summary-line-message-size (head)
3324   "Return pretty-printed version of message size.
3325 This function is intended to be used in
3326 `gnus-summary-line-format-alist', which see."
3327   (let ((c (or (mail-header-chars head) -1)))
3328     (cond ((< c 0) "n/a")               ; chars not available
3329           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3330           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3331           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3332           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3333
3334
3335 (defun gnus-summary-set-local-parameters (group)
3336   "Go through the local params of GROUP and set all variable specs in that list."
3337   (let ((params (gnus-group-find-parameter group))
3338         (vars '(quit-config))           ; Ignore quit-config.
3339         elem)
3340     (while params
3341       (setq elem (car params)
3342             params (cdr params))
3343       (and (consp elem)                 ; Has to be a cons.
3344            (consp (cdr elem))           ; The cdr has to be a list.
3345            (symbolp (car elem))         ; Has to be a symbol in there.
3346            (not (memq (car elem) vars))
3347            (ignore-errors               ; So we set it.
3348              (push (car elem) vars)
3349              (make-local-variable (car elem))
3350              (set (car elem) (eval (nth 1 elem))))))))
3351
3352 (defun gnus-summary-read-group (group &optional show-all no-article
3353                                       kill-buffer no-display backward
3354                                       select-articles)
3355   "Start reading news in newsgroup GROUP.
3356 If SHOW-ALL is non-nil, already read articles are also listed.
3357 If NO-ARTICLE is non-nil, no article is selected initially.
3358 If NO-DISPLAY, don't generate a summary buffer."
3359   (let (result)
3360     (while (and group
3361                 (null (setq result
3362                             (let ((gnus-auto-select-next nil))
3363                               (or (gnus-summary-read-group-1
3364                                    group show-all no-article
3365                                    kill-buffer no-display
3366                                    select-articles)
3367                                   (setq show-all nil
3368                                         select-articles nil)))))
3369                 (eq gnus-auto-select-next 'quietly))
3370       (set-buffer gnus-group-buffer)
3371       ;; The entry function called above goes to the next
3372       ;; group automatically, so we go two groups back
3373       ;; if we are searching for the previous group.
3374       (when backward
3375         (gnus-group-prev-unread-group 2))
3376       (if (not (equal group (gnus-group-group-name)))
3377           (setq group (gnus-group-group-name))
3378         (setq group nil)))
3379     result))
3380
3381 (defun gnus-summary-read-group-1 (group show-all no-article
3382                                         kill-buffer no-display
3383                                         &optional select-articles)
3384   ;; Killed foreign groups can't be entered.
3385   ;;  (when (and (not (gnus-group-native-p group))
3386   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3387   ;;    (error "Dead non-native groups can't be entered"))
3388   (gnus-message 5 "Retrieving newsgroup: %s..."
3389                 (gnus-group-decoded-name group))
3390   (let* ((new-group (gnus-summary-setup-buffer group))
3391          (quit-config (gnus-group-quit-config group))
3392          (did-select (and new-group (gnus-select-newsgroup
3393                                      group show-all select-articles))))
3394     (cond
3395      ;; This summary buffer exists already, so we just select it.
3396      ((not new-group)
3397       (gnus-set-global-variables)
3398       (when kill-buffer
3399         (gnus-kill-or-deaden-summary kill-buffer))
3400       (gnus-configure-windows 'summary 'force)
3401       (gnus-set-mode-line 'summary)
3402       (gnus-summary-position-point)
3403       (message "")
3404       t)
3405      ;; We couldn't select this group.
3406      ((null did-select)
3407       (when (and (eq major-mode 'gnus-summary-mode)
3408                  (not (equal (current-buffer) kill-buffer)))
3409         (kill-buffer (current-buffer))
3410         (if (not quit-config)
3411             (progn
3412               ;; Update the info -- marks might need to be removed,
3413               ;; for instance.
3414               (gnus-summary-update-info)
3415               (set-buffer gnus-group-buffer)
3416               (gnus-group-jump-to-group group)
3417               (gnus-group-next-unread-group 1))
3418           (gnus-handle-ephemeral-exit quit-config)))
3419       (let ((grpinfo (gnus-get-info group)))
3420         (if (null (gnus-info-read grpinfo))
3421             (gnus-message 3 "Group %s contains no messages"
3422                           (gnus-group-decoded-name group))
3423           (gnus-message 3 "Can't select group")))
3424       nil)
3425      ;; The user did a `C-g' while prompting for number of articles,
3426      ;; so we exit this group.
3427      ((eq did-select 'quit)
3428       (and (eq major-mode 'gnus-summary-mode)
3429            (not (equal (current-buffer) kill-buffer))
3430            (kill-buffer (current-buffer)))
3431       (when kill-buffer
3432         (gnus-kill-or-deaden-summary kill-buffer))
3433       (if (not quit-config)
3434           (progn
3435             (set-buffer gnus-group-buffer)
3436             (gnus-group-jump-to-group group)
3437             (gnus-group-next-unread-group 1)
3438             (gnus-configure-windows 'group 'force))
3439         (gnus-handle-ephemeral-exit quit-config))
3440       ;; Finally signal the quit.
3441       (signal 'quit nil))
3442      ;; The group was successfully selected.
3443      (t
3444       (gnus-set-global-variables)
3445       ;; Save the active value in effect when the group was entered.
3446       (setq gnus-newsgroup-active
3447             (gnus-copy-sequence
3448              (gnus-active gnus-newsgroup-name)))
3449       ;; You can change the summary buffer in some way with this hook.
3450       (gnus-run-hooks 'gnus-select-group-hook)
3451       (gnus-update-format-specifications
3452        nil 'summary 'summary-mode 'summary-dummy)
3453       (gnus-update-summary-mark-positions)
3454       ;; Do score processing.
3455       (when gnus-use-scoring
3456         (gnus-possibly-score-headers))
3457       ;; Check whether to fill in the gaps in the threads.
3458       (when gnus-build-sparse-threads
3459         (gnus-build-sparse-threads))
3460       ;; Find the initial limit.
3461       (if gnus-show-threads
3462           (if show-all
3463               (let ((gnus-newsgroup-dormant nil))
3464                 (gnus-summary-initial-limit show-all))
3465             (gnus-summary-initial-limit show-all))
3466         ;; When unthreaded, all articles are always shown.
3467         (setq gnus-newsgroup-limit
3468               (mapcar
3469                (lambda (header) (mail-header-number header))
3470                gnus-newsgroup-headers)))
3471       ;; Generate the summary buffer.
3472       (unless no-display
3473         (gnus-summary-prepare))
3474       (when gnus-use-trees
3475         (gnus-tree-open group)
3476         (setq gnus-summary-highlight-line-function
3477               'gnus-tree-highlight-article))
3478       ;; If the summary buffer is empty, but there are some low-scored
3479       ;; articles or some excluded dormants, we include these in the
3480       ;; buffer.
3481       (when (and (zerop (buffer-size))
3482                  (not no-display))
3483         (cond (gnus-newsgroup-dormant
3484                (gnus-summary-limit-include-dormant))
3485               ((and gnus-newsgroup-scored show-all)
3486                (gnus-summary-limit-include-expunged t))))
3487       ;; Function `gnus-apply-kill-file' must be called in this hook.
3488       (gnus-run-hooks 'gnus-apply-kill-hook)
3489       (if (and (zerop (buffer-size))
3490                (not no-display))
3491           (progn
3492             ;; This newsgroup is empty.
3493             (gnus-summary-catchup-and-exit nil t)
3494             (gnus-message 6 "No unread news")
3495             (when kill-buffer
3496               (gnus-kill-or-deaden-summary kill-buffer))
3497             ;; Return nil from this function.
3498             nil)
3499         ;; Hide conversation thread subtrees.  We cannot do this in
3500         ;; gnus-summary-prepare-hook since kill processing may not
3501         ;; work with hidden articles.
3502         (gnus-summary-maybe-hide-threads)
3503         (when kill-buffer
3504           (gnus-kill-or-deaden-summary kill-buffer))
3505         (gnus-summary-auto-select-subject)
3506         ;; Show first unread article if requested.
3507         (if (and (not no-article)
3508                  (not no-display)
3509                  gnus-newsgroup-unreads
3510                  gnus-auto-select-first)
3511             (progn
3512               (gnus-configure-windows 'summary)
3513               (let ((art (gnus-summary-article-number)))
3514                 (unless (and (not gnus-plugged)
3515                              (or (memq art gnus-newsgroup-undownloaded)
3516                                  (memq art gnus-newsgroup-downloadable)))
3517                   (gnus-summary-goto-article art))))
3518           ;; Don't select any articles.
3519           (gnus-summary-position-point)
3520           (gnus-configure-windows 'summary 'force)
3521           (gnus-set-mode-line 'summary))
3522         (when (get-buffer-window gnus-group-buffer t)
3523           ;; Gotta use windows, because recenter does weird stuff if
3524           ;; the current buffer ain't the displayed window.
3525           (let ((owin (selected-window)))
3526             (select-window (get-buffer-window gnus-group-buffer t))
3527             (when (gnus-group-goto-group group)
3528               (recenter))
3529             (select-window owin)))
3530         ;; Mark this buffer as "prepared".
3531         (setq gnus-newsgroup-prepared t)
3532         (gnus-run-hooks 'gnus-summary-prepared-hook)
3533         (unless (gnus-ephemeral-group-p group)
3534           (gnus-group-update-group group))
3535         t)))))
3536
3537 (defun gnus-summary-auto-select-subject ()
3538   "Select the subject line on initial group entry."
3539   (goto-char (point-min))
3540   (cond
3541    ((eq gnus-auto-select-subject 'best)
3542     (gnus-summary-best-unread-subject))
3543    ((eq gnus-auto-select-subject 'unread)
3544     (gnus-summary-first-unread-subject))
3545    ((eq gnus-auto-select-subject 'unseen)
3546     (gnus-summary-first-unseen-subject))
3547    ((eq gnus-auto-select-subject 'unseen-or-unread)
3548     (gnus-summary-first-unseen-or-unread-subject))
3549    ((eq gnus-auto-select-subject 'first)
3550     ;; Do nothing.
3551     )
3552    ((gnus-functionp gnus-auto-select-subject)
3553     (funcall gnus-auto-select-subject))))
3554
3555 (defun gnus-summary-prepare ()
3556   "Generate the summary buffer."
3557   (interactive)
3558   (let ((buffer-read-only nil))
3559     (erase-buffer)
3560     (setq gnus-newsgroup-data nil
3561           gnus-newsgroup-data-reverse nil)
3562     (gnus-run-hooks 'gnus-summary-generate-hook)
3563     ;; Generate the buffer, either with threads or without.
3564     (when gnus-newsgroup-headers
3565       (gnus-summary-prepare-threads
3566        (if gnus-show-threads
3567            (gnus-sort-gathered-threads
3568             (funcall gnus-summary-thread-gathering-function
3569                      (gnus-sort-threads
3570                       (gnus-cut-threads (gnus-make-threads)))))
3571          ;; Unthreaded display.
3572          (gnus-sort-articles gnus-newsgroup-headers))))
3573     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3574     ;; Call hooks for modifying summary buffer.
3575     (goto-char (point-min))
3576     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3577
3578 (defsubst gnus-general-simplify-subject (subject)
3579   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3580   (setq subject
3581         (cond
3582          ;; Truncate the subject.
3583          (gnus-simplify-subject-functions
3584           (gnus-map-function gnus-simplify-subject-functions subject))
3585          ((numberp gnus-summary-gather-subject-limit)
3586           (setq subject (gnus-simplify-subject-re subject))
3587           (if (> (length subject) gnus-summary-gather-subject-limit)
3588               (substring subject 0 gnus-summary-gather-subject-limit)
3589             subject))
3590          ;; Fuzzily simplify it.
3591          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3592           (gnus-simplify-subject-fuzzy subject))
3593          ;; Just remove the leading "Re:".
3594          (t
3595           (gnus-simplify-subject-re subject))))
3596
3597   (if (and gnus-summary-gather-exclude-subject
3598            (string-match gnus-summary-gather-exclude-subject subject))
3599       nil                         ; This article shouldn't be gathered
3600     subject))
3601
3602 (defun gnus-summary-simplify-subject-query ()
3603   "Query where the respool algorithm would put this article."
3604   (interactive)
3605   (gnus-summary-select-article)
3606   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3607
3608 (defun gnus-gather-threads-by-subject (threads)
3609   "Gather threads by looking at Subject headers."
3610   (if (not gnus-summary-make-false-root)
3611       threads
3612     (let ((hashtb (gnus-make-hashtable 1024))
3613           (prev threads)
3614           (result threads)
3615           subject hthread whole-subject)
3616       (while threads
3617         (setq subject (gnus-general-simplify-subject
3618                        (setq whole-subject (mail-header-subject
3619                                             (caar threads)))))
3620         (when subject
3621           (if (setq hthread (gnus-gethash subject hashtb))
3622               (progn
3623                 ;; We enter a dummy root into the thread, if we
3624                 ;; haven't done that already.
3625                 (unless (stringp (caar hthread))
3626                   (setcar hthread (list whole-subject (car hthread))))
3627                 ;; We add this new gathered thread to this gathered
3628                 ;; thread.
3629                 (setcdr (car hthread)
3630                         (nconc (cdar hthread) (list (car threads))))
3631                 ;; Remove it from the list of threads.
3632                 (setcdr prev (cdr threads))
3633                 (setq threads prev))
3634             ;; Enter this thread into the hash table.
3635             (gnus-sethash subject
3636                           (if gnus-summary-make-false-root-always
3637                               (progn
3638                                 ;; If you want a dummy root above all
3639                                 ;; threads...
3640                                 (setcar threads (list whole-subject
3641                                                       (car threads)))
3642                                 threads)
3643                             threads)
3644                           hashtb)))
3645         (setq prev threads)
3646         (setq threads (cdr threads)))
3647       result)))
3648
3649 (defun gnus-gather-threads-by-references (threads)
3650   "Gather threads by looking at References headers."
3651   (let ((idhashtb (gnus-make-hashtable 1024))
3652         (thhashtb (gnus-make-hashtable 1024))
3653         (prev threads)
3654         (result threads)
3655         ids references id gthread gid entered ref)
3656     (while threads
3657       (when (setq references (mail-header-references (caar threads)))
3658         (setq id (mail-header-id (caar threads))
3659               ids (inline (gnus-split-references references))
3660               entered nil)
3661         (while (setq ref (pop ids))
3662           (setq ids (delete ref ids))
3663           (if (not (setq gid (gnus-gethash ref idhashtb)))
3664               (progn
3665                 (gnus-sethash ref id idhashtb)
3666                 (gnus-sethash id threads thhashtb))
3667             (setq gthread (gnus-gethash gid thhashtb))
3668             (unless entered
3669               ;; We enter a dummy root into the thread, if we
3670               ;; haven't done that already.
3671               (unless (stringp (caar gthread))
3672                 (setcar gthread (list (mail-header-subject (caar gthread))
3673                                       (car gthread))))
3674               ;; We add this new gathered thread to this gathered
3675               ;; thread.
3676               (setcdr (car gthread)
3677                       (nconc (cdar gthread) (list (car threads)))))
3678             ;; Add it into the thread hash table.
3679             (gnus-sethash id gthread thhashtb)
3680             (setq entered t)
3681             ;; Remove it from the list of threads.
3682             (setcdr prev (cdr threads))
3683             (setq threads prev))))
3684       (setq prev threads)
3685       (setq threads (cdr threads)))
3686     result))
3687
3688 (defun gnus-sort-gathered-threads (threads)
3689   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3690   (let ((result threads))
3691     (while threads
3692       (when (stringp (caar threads))
3693         (setcdr (car threads)
3694                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3695       (setq threads (cdr threads)))
3696     result))
3697
3698 (defun gnus-thread-loop-p (root thread)
3699   "Say whether ROOT is in THREAD."
3700   (let ((stack (list thread))
3701         (infloop 0)
3702         th)
3703     (while (setq thread (pop stack))
3704       (setq th (cdr thread))
3705       (while (and th
3706                   (not (eq (caar th) root)))
3707         (pop th))
3708       (if th
3709           ;; We have found a loop.
3710           (let (ref-dep)
3711             (setcdr thread (delq (car th) (cdr thread)))
3712             (if (boundp (setq ref-dep (intern "none"
3713                                               gnus-newsgroup-dependencies)))
3714                 (setcdr (symbol-value ref-dep)
3715                         (nconc (cdr (symbol-value ref-dep))
3716                                (list (car th))))
3717               (set ref-dep (list nil (car th))))
3718             (setq infloop 1
3719                   stack nil))
3720         ;; Push all the subthreads onto the stack.
3721         (push (cdr thread) stack)))
3722     infloop))
3723
3724 (defun gnus-make-threads ()
3725   "Go through the dependency hashtb and find the roots.  Return all threads."
3726   (let (threads)
3727     (while (catch 'infloop
3728              (mapatoms
3729               (lambda (refs)
3730                 ;; Deal with self-referencing References loops.
3731                 (when (and (car (symbol-value refs))
3732                            (not (zerop
3733                                  (apply
3734                                   '+
3735                                   (mapcar
3736                                    (lambda (thread)
3737                                      (gnus-thread-loop-p
3738                                       (car (symbol-value refs)) thread))
3739                                    (cdr (symbol-value refs)))))))
3740                   (setq threads nil)
3741                   (throw 'infloop t))
3742                 (unless (car (symbol-value refs))
3743                   ;; These threads do not refer back to any other
3744                   ;; articles, so they're roots.
3745                   (setq threads (append (cdr (symbol-value refs)) threads))))
3746               gnus-newsgroup-dependencies)))
3747     threads))
3748
3749 ;; Build the thread tree.
3750 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3751   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3752
3753 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3754 if it was already present.
3755
3756 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3757 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3758 Message-IDs will be renamed to a unique Message-ID before being
3759 entered.
3760
3761 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3762   (let* ((id (mail-header-id header))
3763          (id-dep (and id (intern id dependencies)))
3764          parent-id ref ref-dep ref-header replaced)
3765     ;; Enter this `header' in the `dependencies' table.
3766     (cond
3767      ((not id-dep)
3768       (setq header nil))
3769      ;; The first two cases do the normal part: enter a new `header'
3770      ;; in the `dependencies' table.
3771      ((not (boundp id-dep))
3772       (set id-dep (list header)))
3773      ((null (car (symbol-value id-dep)))
3774       (setcar (symbol-value id-dep) header))
3775
3776      ;; From here the `header' was already present in the
3777      ;; `dependencies' table.
3778      (force-new
3779       ;; Overrides an existing entry;
3780       ;; just set the header part of the entry.
3781       (setcar (symbol-value id-dep) header)
3782       (setq replaced t))
3783
3784      ;; Renames the existing `header' to a unique Message-ID.
3785      ((not gnus-summary-ignore-duplicates)
3786       ;; An article with this Message-ID has already been seen.
3787       ;; We rename the Message-ID.
3788       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3789            (list header))
3790       (mail-header-set-id header id))
3791
3792      ;; The last case ignores an existing entry, except it adds any
3793      ;; additional Xrefs (in case the two articles came from different
3794      ;; servers.
3795      ;; Also sets `header' to `nil' meaning that the `dependencies'
3796      ;; table was *not* modified.
3797      (t
3798       (mail-header-set-xref
3799        (car (symbol-value id-dep))
3800        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3801                    "")
3802                (or (mail-header-xref header) "")))
3803       (setq header nil)))
3804
3805     (when (and header (not replaced))
3806       ;; First check that we are not creating a References loop.
3807       (setq parent-id (gnus-parent-id (mail-header-references header)))
3808       (setq ref parent-id)
3809       (while (and ref
3810                   (setq ref-dep (intern-soft ref dependencies))
3811                   (boundp ref-dep)
3812                   (setq ref-header (car (symbol-value ref-dep))))
3813         (if (string= id ref)
3814             ;; Yuk!  This is a reference loop.  Make the article be a
3815             ;; root article.
3816             (progn
3817               (mail-header-set-references (car (symbol-value id-dep)) "none")
3818               (setq ref nil)
3819               (setq parent-id nil))
3820           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3821       (setq ref-dep (intern (or parent-id "none") dependencies))
3822       (if (boundp ref-dep)
3823           (setcdr (symbol-value ref-dep)
3824                   (nconc (cdr (symbol-value ref-dep))
3825                          (list (symbol-value id-dep))))
3826         (set ref-dep (list nil (symbol-value id-dep)))))
3827     header))
3828
3829 (defun gnus-extract-message-id-from-in-reply-to (string)
3830   (if (string-match "<[^>]+>" string)
3831       (substring string (match-beginning 0) (match-end 0))
3832     nil))
3833
3834 (defun gnus-build-sparse-threads ()
3835   (let ((headers gnus-newsgroup-headers)
3836         (mail-parse-charset gnus-newsgroup-charset)
3837         (gnus-summary-ignore-duplicates t)
3838         header references generation relations
3839         subject child end new-child date)
3840     ;; First we create an alist of generations/relations, where
3841     ;; generations is how much we trust the relation, and the relation
3842     ;; is parent/child.
3843     (gnus-message 7 "Making sparse threads...")
3844     (save-excursion
3845       (nnheader-set-temp-buffer " *gnus sparse threads*")
3846       (while (setq header (pop headers))
3847         (when (and (setq references (mail-header-references header))
3848                    (not (string= references "")))
3849           (insert references)
3850           (setq child (mail-header-id header)
3851                 subject (mail-header-subject header)
3852                 date (mail-header-date header)
3853                 generation 0)
3854           (while (search-backward ">" nil t)
3855             (setq end (1+ (point)))
3856             (when (search-backward "<" nil t)
3857               (setq new-child (buffer-substring (point) end))
3858               (push (list (incf generation)
3859                           child (setq child new-child)
3860                           subject date)
3861                     relations)))
3862           (when child
3863             (push (list (1+ generation) child nil subject) relations))
3864           (erase-buffer)))
3865       (kill-buffer (current-buffer)))
3866     ;; Sort over trustworthiness.
3867     (mapcar
3868      (lambda (relation)
3869        (when (gnus-dependencies-add-header
3870               (make-full-mail-header
3871                gnus-reffed-article-number
3872                (nth 3 relation) "" (or (nth 4 relation) "")
3873                (nth 1 relation)
3874                (or (nth 2 relation) "") 0 0 "")
3875               gnus-newsgroup-dependencies nil)
3876          (push gnus-reffed-article-number gnus-newsgroup-limit)
3877          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3878          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3879                gnus-newsgroup-reads)
3880          (decf gnus-reffed-article-number)))
3881      (sort relations 'car-less-than-car))
3882     (gnus-message 7 "Making sparse threads...done")))
3883
3884 (defun gnus-build-old-threads ()
3885   ;; Look at all the articles that refer back to old articles, and
3886   ;; fetch the headers for the articles that aren't there.  This will
3887   ;; build complete threads - if the roots haven't been expired by the
3888   ;; server, that is.
3889   (let ((mail-parse-charset gnus-newsgroup-charset)
3890         id heads)
3891     (mapatoms
3892      (lambda (refs)
3893        (when (not (car (symbol-value refs)))
3894          (setq heads (cdr (symbol-value refs)))
3895          (while heads
3896            (if (memq (mail-header-number (caar heads))
3897                      gnus-newsgroup-dormant)
3898                (setq heads (cdr heads))
3899              (setq id (symbol-name refs))
3900              (while (and (setq id (gnus-build-get-header id))
3901                          (not (car (gnus-id-to-thread id)))))
3902              (setq heads nil)))))
3903      gnus-newsgroup-dependencies)))
3904
3905 ;; This function has to be called with point after the article number
3906 ;; on the beginning of the line.
3907 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3908   (let ((eol (gnus-point-at-eol))
3909         (buffer (current-buffer))
3910         header references in-reply-to)
3911
3912     ;; overview: [num subject from date id refs chars lines misc]
3913     (unwind-protect
3914         (let (x)
3915           (narrow-to-region (point) eol)
3916           (unless (eobp)
3917             (forward-char))
3918
3919           (setq header
3920                 (make-full-mail-header
3921                  number                 ; number
3922                  (condition-case ()     ; subject
3923                      (funcall gnus-decode-encoded-word-function
3924                               (setq x (nnheader-nov-field)))
3925                    (error x))
3926                  (condition-case ()     ; from
3927                      (funcall gnus-decode-encoded-word-function
3928                               (setq x (nnheader-nov-field)))
3929                    (error x))
3930                  (nnheader-nov-field)   ; date
3931                  (nnheader-nov-read-message-id) ; id
3932                  (setq references (nnheader-nov-field)) ; refs
3933                  (nnheader-nov-read-integer) ; chars
3934                  (nnheader-nov-read-integer) ; lines
3935                  (unless (eobp)
3936                    (if (looking-at "Xref: ")
3937                        (goto-char (match-end 0)))
3938                    (nnheader-nov-field)) ; Xref
3939                  (nnheader-nov-parse-extra)))) ; extra
3940
3941       (widen))
3942
3943     (when (and (string= references "")
3944                (setq in-reply-to (mail-header-extra header))
3945                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3946       (mail-header-set-references
3947        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3948
3949     (when gnus-alter-header-function
3950       (funcall gnus-alter-header-function header))
3951     (gnus-dependencies-add-header header dependencies force-new)))
3952
3953 (defun gnus-build-get-header (id)
3954   "Look through the buffer of NOV lines and find the header to ID.
3955 Enter this line into the dependencies hash table, and return
3956 the id of the parent article (if any)."
3957   (let ((deps gnus-newsgroup-dependencies)
3958         found header)
3959     (prog1
3960         (save-excursion
3961           (set-buffer nntp-server-buffer)
3962           (let ((case-fold-search nil))
3963             (goto-char (point-min))
3964             (while (and (not found)
3965                         (search-forward id nil t))
3966               (beginning-of-line)
3967               (setq found (looking-at
3968                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3969                                    (regexp-quote id))))
3970               (or found (beginning-of-line 2)))
3971             (when found
3972               (beginning-of-line)
3973               (and
3974                (setq header (gnus-nov-parse-line
3975                              (read (current-buffer)) deps))
3976                (gnus-parent-id (mail-header-references header))))))
3977       (when header
3978         (let ((number (mail-header-number header)))
3979           (push number gnus-newsgroup-limit)
3980           (push header gnus-newsgroup-headers)
3981           (if (memq number gnus-newsgroup-unselected)
3982               (progn
3983                 (setq gnus-newsgroup-unreads
3984                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
3985                                                number))
3986                 (setq gnus-newsgroup-unselected
3987                       (delq number gnus-newsgroup-unselected)))
3988             (push number gnus-newsgroup-ancient)))))))
3989
3990 (defun gnus-build-all-threads ()
3991   "Read all the headers."
3992   (let ((gnus-summary-ignore-duplicates t)
3993         (mail-parse-charset gnus-newsgroup-charset)
3994         (dependencies gnus-newsgroup-dependencies)
3995         header article)
3996     (save-excursion
3997       (set-buffer nntp-server-buffer)
3998       (let ((case-fold-search nil))
3999         (goto-char (point-min))
4000         (while (not (eobp))
4001           (ignore-errors
4002             (setq article (read (current-buffer))
4003                   header (gnus-nov-parse-line article dependencies)))
4004           (when header
4005             (save-excursion
4006               (set-buffer gnus-summary-buffer)
4007               (push header gnus-newsgroup-headers)
4008               (if (memq (setq article (mail-header-number header))
4009                         gnus-newsgroup-unselected)
4010                   (progn
4011                     (setq gnus-newsgroup-unreads
4012                           (gnus-add-to-sorted-list
4013                            gnus-newsgroup-unreads article))
4014                     (setq gnus-newsgroup-unselected
4015                           (delq article gnus-newsgroup-unselected)))
4016                 (push article gnus-newsgroup-ancient)))
4017             (forward-line 1)))))))
4018
4019 (defun gnus-summary-update-article-line (article header)
4020   "Update the line for ARTICLE using HEADERS."
4021   (let* ((id (mail-header-id header))
4022          (thread (gnus-id-to-thread id)))
4023     (unless thread
4024       (error "Article in no thread"))
4025     ;; Update the thread.
4026     (setcar thread header)
4027     (gnus-summary-goto-subject article)
4028     (let* ((datal (gnus-data-find-list article))
4029            (data (car datal))
4030            (buffer-read-only nil)
4031            (level (gnus-summary-thread-level)))
4032       (gnus-delete-line)
4033       (let ((inserted (- (point)
4034                          (progn
4035                            (gnus-summary-insert-line
4036                             header level nil 
4037                             (memq article gnus-newsgroup-undownloaded)
4038                             (gnus-article-mark article)
4039                             (memq article gnus-newsgroup-replied)
4040                             (memq article gnus-newsgroup-expirable)
4041                             ;; Only insert the Subject string when it's different
4042                             ;; from the previous Subject string.
4043                             (if (and
4044                                  gnus-show-threads
4045                                  (gnus-subject-equal
4046                                   (condition-case ()
4047                                       (mail-header-subject
4048                                        (gnus-data-header
4049                                         (cadr
4050                                          (gnus-data-find-list
4051                                           article
4052                                           (gnus-data-list t)))))
4053                                     ;; Error on the side of excessive subjects.
4054                                     (error ""))
4055                                   (mail-header-subject header)))
4056                                 ""
4057                               (mail-header-subject header))
4058                             nil (cdr (assq article gnus-newsgroup-scored))
4059                             (memq article gnus-newsgroup-processable))
4060                            (point)))))
4061         (when (cdr datal)
4062           (gnus-data-update-list
4063            (cdr datal) 
4064            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4065
4066 (defun gnus-summary-update-article (article &optional iheader)
4067   "Update ARTICLE in the summary buffer."
4068   (set-buffer gnus-summary-buffer)
4069   (let* ((header (gnus-summary-article-header article))
4070          (id (mail-header-id header))
4071          (data (gnus-data-find article))
4072          (thread (gnus-id-to-thread id))
4073          (references (mail-header-references header))
4074          (parent
4075           (gnus-id-to-thread
4076            (or (gnus-parent-id
4077                 (when (and references
4078                            (not (equal "" references)))
4079                   references))
4080                "none")))
4081          (buffer-read-only nil)
4082          (old (car thread)))
4083     (when thread
4084       (unless iheader
4085         (setcar thread nil)
4086         (when parent
4087           (delq thread parent)))
4088       (if (gnus-summary-insert-subject id header)
4089           ;; Set the (possibly) new article number in the data structure.
4090           (gnus-data-set-number data (gnus-id-to-article id))
4091         (setcar thread old)
4092         nil))))
4093
4094 (defun gnus-rebuild-thread (id &optional line)
4095   "Rebuild the thread containing ID.
4096 If LINE, insert the rebuilt thread starting on line LINE."
4097   (let ((buffer-read-only nil)
4098         old-pos current thread data)
4099     (if (not gnus-show-threads)
4100         (setq thread (list (car (gnus-id-to-thread id))))
4101       ;; Get the thread this article is part of.
4102       (setq thread (gnus-remove-thread id)))
4103     (setq old-pos (gnus-point-at-bol))
4104     (setq current (save-excursion
4105                     (and (re-search-backward "[\r\n]" nil t)
4106                          (gnus-summary-article-number))))
4107     ;; If this is a gathered thread, we have to go some re-gathering.
4108     (when (stringp (car thread))
4109       (let ((subject (car thread))
4110             roots thr)
4111         (setq thread (cdr thread))
4112         (while thread
4113           (unless (memq (setq thr (gnus-id-to-thread
4114                                    (gnus-root-id
4115                                     (mail-header-id (caar thread)))))
4116                         roots)
4117             (push thr roots))
4118           (setq thread (cdr thread)))
4119         ;; We now have all (unique) roots.
4120         (if (= (length roots) 1)
4121             ;; All the loose roots are now one solid root.
4122             (setq thread (car roots))
4123           (setq thread (cons subject (gnus-sort-threads roots))))))
4124     (let (threads)
4125       ;; We then insert this thread into the summary buffer.
4126       (when line
4127         (goto-char (point-min))
4128         (forward-line (1- line)))
4129       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4130         (if gnus-show-threads
4131             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4132           (gnus-summary-prepare-unthreaded thread))
4133         (setq data (nreverse gnus-newsgroup-data))
4134         (setq threads gnus-newsgroup-threads))
4135       ;; We splice the new data into the data structure.
4136       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4137       ;;!!! then we want to insert at the beginning of the buffer.
4138       ;;!!! That happens to be true with Gnus now, but that may
4139       ;;!!! change in the future.  Perhaps.
4140       (gnus-data-enter-list
4141        (if line nil current) data (- (point) old-pos))
4142       (setq gnus-newsgroup-threads
4143             (nconc threads gnus-newsgroup-threads))
4144       (gnus-data-compute-positions))))
4145
4146 (defun gnus-number-to-header (number)
4147   "Return the header for article NUMBER."
4148   (let ((headers gnus-newsgroup-headers))
4149     (while (and headers
4150                 (not (= number (mail-header-number (car headers)))))
4151       (pop headers))
4152     (when headers
4153       (car headers))))
4154
4155 (defun gnus-parent-headers (in-headers &optional generation)
4156   "Return the headers of the GENERATIONeth parent of HEADERS."
4157   (unless generation
4158     (setq generation 1))
4159   (let ((parent t)
4160         (headers in-headers)
4161         references)
4162     (while (and parent
4163                 (not (zerop generation))
4164                 (setq references (mail-header-references headers)))
4165       (setq headers (if (and references
4166                              (setq parent (gnus-parent-id references)))
4167                         (car (gnus-id-to-thread parent))
4168                       nil))
4169       (decf generation))
4170     (and (not (eq headers in-headers))
4171          headers)))
4172
4173 (defun gnus-id-to-thread (id)
4174   "Return the (sub-)thread where ID appears."
4175   (gnus-gethash id gnus-newsgroup-dependencies))
4176
4177 (defun gnus-id-to-article (id)
4178   "Return the article number of ID."
4179   (let ((thread (gnus-id-to-thread id)))
4180     (when (and thread
4181                (car thread))
4182       (mail-header-number (car thread)))))
4183
4184 (defun gnus-id-to-header (id)
4185   "Return the article headers of ID."
4186   (car (gnus-id-to-thread id)))
4187
4188 (defun gnus-article-displayed-root-p (article)
4189   "Say whether ARTICLE is a root(ish) article."
4190   (let ((level (gnus-summary-thread-level article))
4191         (refs (mail-header-references  (gnus-summary-article-header article)))
4192         particle)
4193     (cond
4194      ((null level) nil)
4195      ((zerop level) t)
4196      ((null refs) t)
4197      ((null (gnus-parent-id refs)) t)
4198      ((and (= 1 level)
4199            (null (setq particle (gnus-id-to-article
4200                                  (gnus-parent-id refs))))
4201            (null (gnus-summary-thread-level particle)))))))
4202
4203 (defun gnus-root-id (id)
4204   "Return the id of the root of the thread where ID appears."
4205   (let (last-id prev)
4206     (while (and id (setq prev (car (gnus-id-to-thread id))))
4207       (setq last-id id
4208             id (gnus-parent-id (mail-header-references prev))))
4209     last-id))
4210
4211 (defun gnus-articles-in-thread (thread)
4212   "Return the list of articles in THREAD."
4213   (cons (mail-header-number (car thread))
4214         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4215
4216 (defun gnus-remove-thread (id &optional dont-remove)
4217   "Remove the thread that has ID in it."
4218   (let (headers thread last-id)
4219     ;; First go up in this thread until we find the root.
4220     (setq last-id (gnus-root-id id)
4221           headers (message-flatten-list (gnus-id-to-thread last-id)))
4222     ;; We have now found the real root of this thread.  It might have
4223     ;; been gathered into some loose thread, so we have to search
4224     ;; through the threads to find the thread we wanted.
4225     (let ((threads gnus-newsgroup-threads)
4226           sub)
4227       (while threads
4228         (setq sub (car threads))
4229         (if (stringp (car sub))
4230             ;; This is a gathered thread, so we look at the roots
4231             ;; below it to find whether this article is in this
4232             ;; gathered root.
4233             (progn
4234               (setq sub (cdr sub))
4235               (while sub
4236                 (when (member (caar sub) headers)
4237                   (setq thread (car threads)
4238                         threads nil
4239                         sub nil))
4240                 (setq sub (cdr sub))))
4241           ;; It's an ordinary thread, so we check it.
4242           (when (eq (car sub) (car headers))
4243             (setq thread sub
4244                   threads nil)))
4245         (setq threads (cdr threads)))
4246       ;; If this article is in no thread, then it's a root.
4247       (if thread
4248           (unless dont-remove
4249             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4250         (setq thread (gnus-id-to-thread last-id)))
4251       (when thread
4252         (prog1
4253             thread                      ; We return this thread.
4254           (unless dont-remove
4255             (if (stringp (car thread))
4256                 (progn
4257                   ;; If we use dummy roots, then we have to remove the
4258                   ;; dummy root as well.
4259                   (when (eq gnus-summary-make-false-root 'dummy)
4260                     ;; We go to the dummy root by going to
4261                     ;; the first sub-"thread", and then one line up.
4262                     (gnus-summary-goto-article
4263                      (mail-header-number (caadr thread)))
4264                     (forward-line -1)
4265                     (gnus-delete-line)
4266                     (gnus-data-compute-positions))
4267                   (setq thread (cdr thread))
4268                   (while thread
4269                     (gnus-remove-thread-1 (car thread))
4270                     (setq thread (cdr thread))))
4271               (gnus-remove-thread-1 thread))))))))
4272
4273 (defun gnus-remove-thread-1 (thread)
4274   "Remove the thread THREAD recursively."
4275   (let ((number (mail-header-number (pop thread)))
4276         d)
4277     (setq thread (reverse thread))
4278     (while thread
4279       (gnus-remove-thread-1 (pop thread)))
4280     (when (setq d (gnus-data-find number))
4281       (goto-char (gnus-data-pos d))
4282       (gnus-summary-show-thread)
4283       (gnus-data-remove
4284        number
4285        (- (gnus-point-at-bol)
4286           (prog1
4287               (1+ (gnus-point-at-eol))
4288             (gnus-delete-line)))))))
4289
4290 (defun gnus-sort-threads-1 (threads func)
4291   (sort (mapcar (lambda (thread)
4292                   (cons (car thread)
4293                         (and (cdr thread)
4294                              (gnus-sort-threads-1 (cdr thread) func))))
4295                 threads) func))
4296
4297 (defun gnus-sort-threads (threads)
4298   "Sort THREADS."
4299   (if (not gnus-thread-sort-functions)
4300       threads
4301     (gnus-message 8 "Sorting threads...")
4302     (let ((max-lisp-eval-depth 5000))
4303       (prog1 (gnus-sort-threads-1
4304          threads
4305          (gnus-make-sort-function gnus-thread-sort-functions))
4306         (gnus-message 8 "Sorting threads...done")))))
4307
4308 (defun gnus-sort-articles (articles)
4309   "Sort ARTICLES."
4310   (when gnus-article-sort-functions
4311     (gnus-message 7 "Sorting articles...")
4312     (prog1
4313         (setq gnus-newsgroup-headers
4314               (sort articles (gnus-make-sort-function
4315                               gnus-article-sort-functions)))
4316       (gnus-message 7 "Sorting articles...done"))))
4317
4318 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4319 (defmacro gnus-thread-header (thread)
4320   "Return header of first article in THREAD.
4321 Note that THREAD must never, ever be anything else than a variable -
4322 using some other form will lead to serious barfage."
4323   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4324   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4325   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4326         (vector thread) 2))
4327
4328 (defsubst gnus-article-sort-by-number (h1 h2)
4329   "Sort articles by article number."
4330   (< (mail-header-number h1)
4331      (mail-header-number h2)))
4332
4333 (defun gnus-thread-sort-by-number (h1 h2)
4334   "Sort threads by root article number."
4335   (gnus-article-sort-by-number
4336    (gnus-thread-header h1) (gnus-thread-header h2)))
4337
4338 (defsubst gnus-article-sort-by-random (h1 h2)
4339   "Sort articles by article number."
4340   (zerop (random 2)))
4341
4342 (defun gnus-thread-sort-by-random (h1 h2)
4343   "Sort threads by root article number."
4344   (gnus-article-sort-by-random
4345    (gnus-thread-header h1) (gnus-thread-header h2)))
4346
4347 (defsubst gnus-article-sort-by-lines (h1 h2)
4348   "Sort articles by article Lines header."
4349   (< (mail-header-lines h1)
4350      (mail-header-lines h2)))
4351
4352 (defun gnus-thread-sort-by-lines (h1 h2)
4353   "Sort threads by root article Lines header."
4354   (gnus-article-sort-by-lines
4355    (gnus-thread-header h1) (gnus-thread-header h2)))
4356
4357 (defsubst gnus-article-sort-by-chars (h1 h2)
4358   "Sort articles by octet length."
4359   (< (mail-header-chars h1)
4360      (mail-header-chars h2)))
4361
4362 (defun gnus-thread-sort-by-chars (h1 h2)
4363   "Sort threads by root article octet length."
4364   (gnus-article-sort-by-chars
4365    (gnus-thread-header h1) (gnus-thread-header h2)))
4366
4367 (defsubst gnus-article-sort-by-author (h1 h2)
4368   "Sort articles by root author."
4369   (string-lessp
4370    (let ((extract (funcall
4371                    gnus-extract-address-components
4372                    (mail-header-from h1))))
4373      (or (car extract) (cadr extract) ""))
4374    (let ((extract (funcall
4375                    gnus-extract-address-components
4376                    (mail-header-from h2))))
4377      (or (car extract) (cadr extract) ""))))
4378
4379 (defun gnus-thread-sort-by-author (h1 h2)
4380   "Sort threads by root author."
4381   (gnus-article-sort-by-author
4382    (gnus-thread-header h1)  (gnus-thread-header h2)))
4383
4384 (defsubst gnus-article-sort-by-subject (h1 h2)
4385   "Sort articles by root subject."
4386   (string-lessp
4387    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4388    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4389
4390 (defun gnus-thread-sort-by-subject (h1 h2)
4391   "Sort threads by root subject."
4392   (gnus-article-sort-by-subject
4393    (gnus-thread-header h1) (gnus-thread-header h2)))
4394
4395 (defsubst gnus-article-sort-by-date (h1 h2)
4396   "Sort articles by root article date."
4397   (time-less-p
4398    (gnus-date-get-time (mail-header-date h1))
4399    (gnus-date-get-time (mail-header-date h2))))
4400
4401 (defun gnus-thread-sort-by-date (h1 h2)
4402   "Sort threads by root article date."
4403   (gnus-article-sort-by-date
4404    (gnus-thread-header h1) (gnus-thread-header h2)))
4405
4406 (defsubst gnus-article-sort-by-score (h1 h2)
4407   "Sort articles by root article score.
4408 Unscored articles will be counted as having a score of zero."
4409   (> (or (cdr (assq (mail-header-number h1)
4410                     gnus-newsgroup-scored))
4411          gnus-summary-default-score 0)
4412      (or (cdr (assq (mail-header-number h2)
4413                     gnus-newsgroup-scored))
4414          gnus-summary-default-score 0)))
4415
4416 (defun gnus-thread-sort-by-score (h1 h2)
4417   "Sort threads by root article score."
4418   (gnus-article-sort-by-score
4419    (gnus-thread-header h1) (gnus-thread-header h2)))
4420
4421 (defun gnus-thread-sort-by-total-score (h1 h2)
4422   "Sort threads by the sum of all scores in the thread.
4423 Unscored articles will be counted as having a score of zero."
4424   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4425
4426 (defun gnus-thread-total-score (thread)
4427   ;; This function find the total score of THREAD.
4428   (cond
4429    ((null thread)
4430     0)
4431    ((consp thread)
4432     (if (stringp (car thread))
4433         (apply gnus-thread-score-function 0
4434                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4435       (gnus-thread-total-score-1 thread)))
4436    (t
4437     (gnus-thread-total-score-1 (list thread)))))
4438
4439 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4440   "Sort threads such that the thread with the most recently arrived article comes first."
4441   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4442
4443 (defun gnus-thread-highest-number (thread)
4444   "Return the highest article number in THREAD."
4445   (apply 'max (mapcar (lambda (header)
4446                         (mail-header-number header))
4447                       (message-flatten-list thread))))
4448
4449 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4450   "Sort threads such that the thread with the most recently dated article comes first."
4451   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4452
4453 (defun gnus-thread-latest-date (thread)
4454   "Return the highest article date in THREAD."
4455   (let ((previous-time 0))
4456     (apply 'max
4457            (mapcar
4458             (lambda (header)
4459               (setq previous-time
4460                     (time-to-seconds
4461                      (condition-case ()
4462                          (mail-header-parse-date (mail-header-date header))
4463                        (error previous-time)))))
4464             (sort
4465              (message-flatten-list thread)
4466              (lambda (h1 h2)
4467                (< (mail-header-number h1)
4468                   (mail-header-number h2))))))))
4469
4470 (defun gnus-thread-total-score-1 (root)
4471   ;; This function find the total score of the thread below ROOT.
4472   (setq root (car root))
4473   (apply gnus-thread-score-function
4474          (or (append
4475               (mapcar 'gnus-thread-total-score
4476                       (cdr (gnus-id-to-thread (mail-header-id root))))
4477               (when (> (mail-header-number root) 0)
4478                 (list (or (cdr (assq (mail-header-number root)
4479                                      gnus-newsgroup-scored))
4480                           gnus-summary-default-score 0))))
4481              (list gnus-summary-default-score)
4482              '(0))))
4483
4484 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4485 (defvar gnus-tmp-prev-subject nil)
4486 (defvar gnus-tmp-false-parent nil)
4487 (defvar gnus-tmp-root-expunged nil)
4488 (defvar gnus-tmp-dummy-line nil)
4489
4490 (eval-when-compile (defvar gnus-tmp-header))
4491 (defun gnus-extra-header (type &optional header)
4492   "Return the extra header of TYPE."
4493   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4494       ""))
4495
4496 (defvar gnus-tmp-thread-tree-header-string "")
4497
4498 (defcustom gnus-sum-thread-tree-root "> "
4499   "With %B spec, used for the root of a thread.
4500 If nil, use subject instead."
4501   :type 'string
4502   :group 'gnus-thread)
4503 (defcustom gnus-sum-thread-tree-single-indent ""
4504   "With %B spec, used for a thread with just one message.
4505 If nil, use subject instead."
4506   :type 'string
4507   :group 'gnus-thread)
4508 (defcustom gnus-sum-thread-tree-vertical "| "
4509   "With %B spec, used for drawing a vertical line."
4510   :type 'string
4511   :group 'gnus-thread)
4512 (defcustom gnus-sum-thread-tree-indent "  "
4513   "With %B spec, used for indenting."
4514   :type 'string
4515   :group 'gnus-thread)
4516 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4517   "With %B spec, used for a leaf with brothers."
4518   :type 'string
4519   :group 'gnus-thread)
4520 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4521   "With %B spec, used for a leaf without brothers."
4522   :type 'string
4523   :group 'gnus-thread)
4524
4525 (defun gnus-summary-prepare-threads (threads)
4526   "Prepare summary buffer from THREADS and indentation LEVEL.
4527 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4528 or a straight list of headers."
4529   (gnus-message 7 "Generating summary...")
4530
4531   (setq gnus-newsgroup-threads threads)
4532   (beginning-of-line)
4533
4534   (let ((gnus-tmp-level 0)
4535         (default-score (or gnus-summary-default-score 0))
4536         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4537         (building-line-count gnus-summary-display-while-building)
4538         (building-count (integerp gnus-summary-display-while-building))
4539         thread number subject stack state gnus-tmp-gathered beg-match
4540         new-roots gnus-tmp-new-adopts thread-end simp-subject
4541         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4542         gnus-tmp-replied gnus-tmp-subject-or-nil
4543         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4544         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4545         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4546         tree-stack)
4547
4548     (setq gnus-tmp-prev-subject nil)
4549
4550     (if (vectorp (car threads))
4551         ;; If this is a straight (sic) list of headers, then a
4552         ;; threaded summary display isn't required, so we just create
4553         ;; an unthreaded one.
4554         (gnus-summary-prepare-unthreaded threads)
4555
4556       ;; Do the threaded display.
4557
4558       (if gnus-summary-display-while-building
4559           (switch-to-buffer (buffer-name)))
4560       (while (or threads stack gnus-tmp-new-adopts new-roots)
4561
4562         (if (and (= gnus-tmp-level 0)
4563                  (or (not stack)
4564                      (= (caar stack) 0))
4565                  (not gnus-tmp-false-parent)
4566                  (or gnus-tmp-new-adopts new-roots))
4567             (if gnus-tmp-new-adopts
4568                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4569                       thread (list (car gnus-tmp-new-adopts))
4570                       gnus-tmp-header (caar thread)
4571                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4572               (when new-roots
4573                 (setq thread (list (car new-roots))
4574                       gnus-tmp-header (caar thread)
4575                       new-roots (cdr new-roots))))
4576
4577           (if threads
4578               ;; If there are some threads, we do them before the
4579               ;; threads on the stack.
4580               (setq thread threads
4581                     gnus-tmp-header (caar thread))
4582             ;; There were no current threads, so we pop something off
4583             ;; the stack.
4584             (setq state (car stack)
4585                   gnus-tmp-level (car state)
4586                   tree-stack (cadr state)
4587                   thread (caddr state)
4588                   stack (cdr stack)
4589                   gnus-tmp-header (caar thread))))
4590
4591         (setq gnus-tmp-false-parent nil)
4592         (setq gnus-tmp-root-expunged nil)
4593         (setq thread-end nil)
4594
4595         (if (stringp gnus-tmp-header)
4596             ;; The header is a dummy root.
4597             (cond
4598              ((eq gnus-summary-make-false-root 'adopt)
4599               ;; We let the first article adopt the rest.
4600               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4601                                                (cddar thread)))
4602               (setq gnus-tmp-gathered
4603                     (nconc (mapcar
4604                             (lambda (h) (mail-header-number (car h)))
4605                             (cddar thread))
4606                            gnus-tmp-gathered))
4607               (setq thread (cons (list (caar thread)
4608                                        (cadar thread))
4609                                  (cdr thread)))
4610               (setq gnus-tmp-level -1
4611                     gnus-tmp-false-parent t))
4612              ((eq gnus-summary-make-false-root 'empty)
4613               ;; We print adopted articles with empty subject fields.
4614               (setq gnus-tmp-gathered
4615                     (nconc (mapcar
4616                             (lambda (h) (mail-header-number (car h)))
4617                             (cddar thread))
4618                            gnus-tmp-gathered))
4619               (setq gnus-tmp-level -1))
4620              ((eq gnus-summary-make-false-root 'dummy)
4621               ;; We remember that we probably want to output a dummy
4622               ;; root.
4623               (setq gnus-tmp-dummy-line gnus-tmp-header)
4624               (setq gnus-tmp-prev-subject gnus-tmp-header))
4625              (t
4626               ;; We do not make a root for the gathered
4627               ;; sub-threads at all.
4628               (setq gnus-tmp-level -1)))
4629
4630           (setq number (mail-header-number gnus-tmp-header)
4631                 subject (mail-header-subject gnus-tmp-header)
4632                 simp-subject (gnus-simplify-subject-fully subject))
4633
4634           (cond
4635            ;; If the thread has changed subject, we might want to make
4636            ;; this subthread into a root.
4637            ((and (null gnus-thread-ignore-subject)
4638                  (not (zerop gnus-tmp-level))
4639                  gnus-tmp-prev-subject
4640                  (not (string= gnus-tmp-prev-subject simp-subject)))
4641             (setq new-roots (nconc new-roots (list (car thread)))
4642                   thread-end t
4643                   gnus-tmp-header nil))
4644            ;; If the article lies outside the current limit,
4645            ;; then we do not display it.
4646            ((not (memq number gnus-newsgroup-limit))
4647             (setq gnus-tmp-gathered
4648                   (nconc (mapcar
4649                           (lambda (h) (mail-header-number (car h)))
4650                           (cdar thread))
4651                          gnus-tmp-gathered))
4652             (setq gnus-tmp-new-adopts (if (cdar thread)
4653                                           (append gnus-tmp-new-adopts
4654                                                   (cdar thread))
4655                                         gnus-tmp-new-adopts)
4656                   thread-end t
4657                   gnus-tmp-header nil)
4658             (when (zerop gnus-tmp-level)
4659               (setq gnus-tmp-root-expunged t)))
4660            ;; Perhaps this article is to be marked as read?
4661            ((and gnus-summary-mark-below
4662                  (< (or (cdr (assq number gnus-newsgroup-scored))
4663                         default-score)
4664                     gnus-summary-mark-below)
4665                  ;; Don't touch sparse articles.
4666                  (not (gnus-summary-article-sparse-p number))
4667                  (not (gnus-summary-article-ancient-p number)))
4668             (setq gnus-newsgroup-unreads
4669                   (delq number gnus-newsgroup-unreads))
4670             (if gnus-newsgroup-auto-expire
4671                 (setq gnus-newsgroup-expirable
4672                       (gnus-add-to-sorted-list
4673                        gnus-newsgroup-expirable number))
4674               (push (cons number gnus-low-score-mark)
4675                     gnus-newsgroup-reads))))
4676
4677           (when gnus-tmp-header
4678             ;; We may have an old dummy line to output before this
4679             ;; article.
4680             (when (and gnus-tmp-dummy-line
4681                        (gnus-subject-equal
4682                         gnus-tmp-dummy-line
4683                         (mail-header-subject gnus-tmp-header)))
4684               (gnus-summary-insert-dummy-line
4685                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4686               (setq gnus-tmp-dummy-line nil))
4687
4688             ;; Compute the mark.
4689             (setq gnus-tmp-unread (gnus-article-mark number))
4690
4691             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4692                                   gnus-tmp-header gnus-tmp-level)
4693                   gnus-newsgroup-data)
4694
4695             ;; Actually insert the line.
4696             (setq
4697              gnus-tmp-subject-or-nil
4698              (cond
4699               ((and gnus-thread-ignore-subject
4700                     gnus-tmp-prev-subject
4701                     (not (string= gnus-tmp-prev-subject simp-subject)))
4702                subject)
4703               ((zerop gnus-tmp-level)
4704                (if (and (eq gnus-summary-make-false-root 'empty)
4705                         (memq number gnus-tmp-gathered)
4706                         gnus-tmp-prev-subject
4707                         (string= gnus-tmp-prev-subject simp-subject))
4708                    gnus-summary-same-subject
4709                  subject))
4710               (t gnus-summary-same-subject)))
4711             (if (and (eq gnus-summary-make-false-root 'adopt)
4712                      (= gnus-tmp-level 1)
4713                      (memq number gnus-tmp-gathered))
4714                 (setq gnus-tmp-opening-bracket ?\<
4715                       gnus-tmp-closing-bracket ?\>)
4716               (setq gnus-tmp-opening-bracket ?\[
4717                     gnus-tmp-closing-bracket ?\]))
4718             (setq
4719              gnus-tmp-indentation
4720              (aref gnus-thread-indent-array gnus-tmp-level)
4721              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4722              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4723                                 gnus-summary-default-score 0)
4724              gnus-tmp-score-char
4725              (if (or (null gnus-summary-default-score)
4726                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4727                          gnus-summary-zcore-fuzz))
4728                  ?                      ;Whitespace
4729                (if (< gnus-tmp-score gnus-summary-default-score)
4730                    gnus-score-below-mark gnus-score-over-mark))
4731              gnus-tmp-replied
4732              (cond ((memq number gnus-newsgroup-processable)
4733                     gnus-process-mark)
4734                    ((memq number gnus-newsgroup-cached)
4735                     gnus-cached-mark)
4736                    ((memq number gnus-newsgroup-replied)
4737                     gnus-replied-mark)
4738                    ((memq number gnus-newsgroup-forwarded)
4739                     gnus-forwarded-mark)
4740                    ((memq number gnus-newsgroup-saved)
4741                     gnus-saved-mark)
4742                    ((memq number gnus-newsgroup-recent)
4743                     gnus-recent-mark)
4744                    ((memq number gnus-newsgroup-unseen)
4745                     gnus-unseen-mark)
4746                    (t gnus-no-mark))
4747              gnus-tmp-downloaded
4748              (cond ((memq number gnus-newsgroup-undownloaded) 
4749                     gnus-undownloaded-mark)
4750                    (gnus-newsgroup-agentized
4751                     gnus-downloaded-mark)
4752                    (t
4753                     gnus-no-mark))
4754              gnus-tmp-from (mail-header-from gnus-tmp-header)
4755              gnus-tmp-name
4756              (cond
4757               ((string-match "<[^>]+> *$" gnus-tmp-from)
4758                (setq beg-match (match-beginning 0))
4759                (or (and (string-match "^\".+\"" gnus-tmp-from)
4760                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4761                    (substring gnus-tmp-from 0 beg-match)))
4762               ((string-match "(.+)" gnus-tmp-from)
4763                (substring gnus-tmp-from
4764                           (1+ (match-beginning 0)) (1- (match-end 0))))
4765               (t gnus-tmp-from))
4766              gnus-tmp-thread-tree-header-string
4767              (cond
4768               ((not gnus-show-threads) "")
4769               ((zerop gnus-tmp-level)
4770                (if (cdar thread)
4771                    (or gnus-sum-thread-tree-root subject)
4772                  (or gnus-sum-thread-tree-single-indent subject)))
4773               (t
4774                (concat (apply 'concat
4775                               (mapcar (lambda (item)
4776                                         (if (= item 1)
4777                                             gnus-sum-thread-tree-vertical
4778                                           gnus-sum-thread-tree-indent))
4779                                       (cdr (reverse tree-stack))))
4780                        (if (nth 1 thread)
4781                            gnus-sum-thread-tree-leaf-with-other
4782                          gnus-sum-thread-tree-single-leaf)))))
4783             (when (string= gnus-tmp-name "")
4784               (setq gnus-tmp-name gnus-tmp-from))
4785             (unless (numberp gnus-tmp-lines)
4786               (setq gnus-tmp-lines -1))
4787             (if (= gnus-tmp-lines -1)
4788                 (setq gnus-tmp-lines "?")
4789               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4790               (gnus-put-text-property
4791              (point)
4792              (progn (eval gnus-summary-line-format-spec) (point))
4793                'gnus-number number)
4794             (when gnus-visual-p
4795               (forward-line -1)
4796               (gnus-run-hooks 'gnus-summary-update-hook)
4797               (forward-line 1))
4798
4799             (setq gnus-tmp-prev-subject simp-subject)))
4800
4801         (when (nth 1 thread)
4802           (push (list (max 0 gnus-tmp-level)
4803                       (copy-list tree-stack)
4804                       (nthcdr 1 thread))
4805                 stack))
4806         (push (if (nth 1 thread) 1 0) tree-stack)
4807         (incf gnus-tmp-level)
4808         (setq threads (if thread-end nil (cdar thread)))
4809         (if gnus-summary-display-while-building
4810             (if building-count
4811                 (progn
4812                   ;; use a set frequency
4813                   (setq building-line-count (1- building-line-count))
4814                   (when (= building-line-count 0)
4815                     (sit-for 0)
4816                     (setq building-line-count
4817                           gnus-summary-display-while-building)))
4818               ;; always
4819               (sit-for 0)))
4820         (unless threads
4821           (setq gnus-tmp-level 0)))))
4822   (gnus-message 7 "Generating summary...done"))
4823
4824 (defun gnus-summary-prepare-unthreaded (headers)
4825   "Generate an unthreaded summary buffer based on HEADERS."
4826   (let (header number mark)
4827
4828     (beginning-of-line)
4829
4830     (while headers
4831       ;; We may have to root out some bad articles...
4832       (when (memq (setq number (mail-header-number
4833                                 (setq header (pop headers))))
4834                   gnus-newsgroup-limit)
4835         ;; Mark article as read when it has a low score.
4836         (when (and gnus-summary-mark-below
4837                    (< (or (cdr (assq number gnus-newsgroup-scored))
4838                           gnus-summary-default-score 0)
4839                       gnus-summary-mark-below)
4840                    (not (gnus-summary-article-ancient-p number)))
4841           (setq gnus-newsgroup-unreads
4842                 (delq number gnus-newsgroup-unreads))
4843           (if gnus-newsgroup-auto-expire
4844               (push number gnus-newsgroup-expirable)
4845             (push (cons number gnus-low-score-mark)
4846                   gnus-newsgroup-reads)))
4847
4848         (setq mark (gnus-article-mark number))
4849         (push (gnus-data-make number mark (1+ (point)) header 0)
4850               gnus-newsgroup-data)
4851         (gnus-summary-insert-line
4852          header 0 number
4853          (memq number gnus-newsgroup-undownloaded)
4854          mark (memq number gnus-newsgroup-replied)
4855          (memq number gnus-newsgroup-expirable)
4856          (mail-header-subject header) nil
4857          (cdr (assq number gnus-newsgroup-scored))
4858          (memq number gnus-newsgroup-processable))))))
4859
4860 (defun gnus-summary-remove-list-identifiers ()
4861   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4862   (let ((regexp (if (consp gnus-list-identifiers)
4863                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4864                   gnus-list-identifiers))
4865         changed subject)
4866     (when regexp
4867       (dolist (header gnus-newsgroup-headers)
4868         (setq subject (mail-header-subject header)
4869               changed nil)
4870         (while (string-match
4871                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4872                 subject)
4873           (setq subject
4874                 (concat (substring subject 0 (match-beginning 2))
4875                         (substring subject (match-end 0)))
4876                 changed t))
4877         (when (and changed
4878                    (string-match
4879                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4880           (setq subject
4881                 (concat (substring subject 0 (match-beginning 1))
4882                         (substring subject (match-end 1)))))
4883         (when changed
4884           (mail-header-set-subject header subject))))))
4885
4886 (defun gnus-fetch-headers (articles)
4887   "Fetch headers of ARTICLES."
4888   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4889     (gnus-message 5 "Fetching headers for %s..." name)
4890     (prog1
4891         (if (eq 'nov
4892                 (setq gnus-headers-retrieved-by
4893                       (gnus-retrieve-headers
4894                        articles gnus-newsgroup-name
4895                        ;; We might want to fetch old headers, but
4896                        ;; not if there is only 1 article.
4897                        (and (or (and
4898                                  (not (eq gnus-fetch-old-headers 'some))
4899                                  (not (numberp gnus-fetch-old-headers)))
4900                                 (> (length articles) 1))
4901                             gnus-fetch-old-headers))))
4902             (gnus-get-newsgroup-headers-xover
4903              articles nil nil gnus-newsgroup-name t)
4904           (gnus-get-newsgroup-headers))
4905       (gnus-message 5 "Fetching headers for %s...done" name))))
4906
4907 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4908   "Select newsgroup GROUP.
4909 If READ-ALL is non-nil, all articles in the group are selected.
4910 If SELECT-ARTICLES, only select those articles from GROUP."
4911   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4912          ;;!!! Dirty hack; should be removed.
4913          (gnus-summary-ignore-duplicates
4914           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4915               t
4916             gnus-summary-ignore-duplicates))
4917          (info (nth 2 entry))
4918          articles fetched-articles cached)
4919
4920     (unless (gnus-check-server
4921              (set (make-local-variable 'gnus-current-select-method)
4922                   (gnus-find-method-for-group group)))
4923       (error "Couldn't open server"))
4924
4925     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4926         (gnus-activate-group group)     ; Or we can activate it...
4927         (progn                          ; Or we bug out.
4928           (when (equal major-mode 'gnus-summary-mode)
4929             (kill-buffer (current-buffer)))
4930           (error "Couldn't activate group %s: %s"
4931                  group (gnus-status-message group))))
4932
4933     (unless (gnus-request-group group t)
4934       (when (equal major-mode 'gnus-summary-mode)
4935         (kill-buffer (current-buffer)))
4936       (error "Couldn't request group %s: %s"
4937              group (gnus-status-message group)))
4938
4939     (setq gnus-newsgroup-name group
4940           gnus-newsgroup-unselected nil
4941           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4942
4943     (let ((display (gnus-group-find-parameter group 'display)))
4944       (setq gnus-newsgroup-display
4945             (cond
4946              ((not (zerop (or (car-safe read-all) 0)))
4947               ;; The user entered the group with C-u SPC/RET, let's show
4948               ;; all articles.
4949               'gnus-not-ignore)
4950              ((eq display 'all)
4951               'gnus-not-ignore)
4952              ((arrayp display)
4953               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4954              ((numberp display)
4955               ;; The following is probably the "correct" solution, but
4956               ;; it makes Gnus fetch all headers and then limit the
4957               ;; articles (which is slow), so instead we hack the
4958               ;; select-articles parameter instead. -- Simon Josefsson
4959               ;; <jas@kth.se>
4960               ;;
4961               ;; (gnus-byte-compile
4962               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4963               ;;                         display)))))
4964               (setq select-articles
4965                     (gnus-uncompress-range
4966                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4967                              (if (> tmp 0)
4968                                  tmp
4969                                1))
4970                            (cdr (gnus-active group)))))
4971               nil)
4972              (t
4973               nil))))
4974
4975     (gnus-summary-setup-default-charset)
4976
4977     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4978     (when (gnus-virtual-group-p group)
4979       (setq cached gnus-newsgroup-cached))
4980
4981     (setq gnus-newsgroup-unreads
4982           (gnus-sorted-ndifference
4983            (gnus-sorted-ndifference gnus-newsgroup-unreads
4984                                     gnus-newsgroup-marked)
4985            gnus-newsgroup-dormant))
4986
4987     (setq gnus-newsgroup-processable nil)
4988
4989     (gnus-update-read-articles group gnus-newsgroup-unreads)
4990
4991     ;; Adjust and set lists of article marks.
4992     (when info
4993       (gnus-adjust-marked-articles info))
4994     (if (setq articles select-articles)
4995         (setq gnus-newsgroup-unselected
4996               (gnus-sorted-difference gnus-newsgroup-unreads articles))
4997       (setq articles (gnus-articles-to-read group read-all)))
4998
4999     (cond
5000      ((null articles)
5001       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5002       'quit)
5003      ((eq articles 0) nil)
5004      (t
5005       ;; Init the dependencies hash table.
5006       (setq gnus-newsgroup-dependencies
5007             (gnus-make-hashtable (length articles)))
5008       (gnus-set-global-variables)
5009       ;; Retrieve the headers and read them in.
5010
5011       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5012
5013       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5014       (when cached
5015         (setq gnus-newsgroup-cached cached))
5016
5017       ;; Suppress duplicates?
5018       (when gnus-suppress-duplicates
5019         (gnus-dup-suppress-articles))
5020
5021       ;; Set the initial limit.
5022       (setq gnus-newsgroup-limit (copy-sequence articles))
5023       ;; Remove canceled articles from the list of unread articles.
5024       (setq fetched-articles
5025             (mapcar (lambda (headers) (mail-header-number headers))
5026                     gnus-newsgroup-headers))
5027       (setq gnus-newsgroup-articles fetched-articles)
5028       (setq gnus-newsgroup-unreads
5029             (gnus-sorted-nintersection
5030              gnus-newsgroup-unreads fetched-articles))
5031       (gnus-compute-unseen-list)
5032
5033       ;; Removed marked articles that do not exist.
5034       (gnus-update-missing-marks
5035        (gnus-sorted-difference articles fetched-articles))
5036       ;; We might want to build some more threads first.
5037       (when (and gnus-fetch-old-headers
5038                  (eq gnus-headers-retrieved-by 'nov))
5039         (if (eq gnus-fetch-old-headers 'invisible)
5040             (gnus-build-all-threads)
5041           (gnus-build-old-threads)))
5042       ;; Let the Gnus agent mark articles as read.
5043       (when gnus-agent
5044         (gnus-agent-get-undownloaded-list))
5045       ;; Remove list identifiers from subject
5046       (when gnus-list-identifiers
5047         (gnus-summary-remove-list-identifiers))
5048       ;; Check whether auto-expire is to be done in this group.
5049       (setq gnus-newsgroup-auto-expire
5050             (gnus-group-auto-expirable-p group))
5051       ;; Set up the article buffer now, if necessary.
5052       (unless gnus-single-article-buffer
5053         (gnus-article-setup-buffer))
5054       ;; First and last article in this newsgroup.
5055       (when gnus-newsgroup-headers
5056         (setq gnus-newsgroup-begin
5057               (mail-header-number (car gnus-newsgroup-headers))
5058               gnus-newsgroup-end
5059               (mail-header-number
5060                (gnus-last-element gnus-newsgroup-headers))))
5061       ;; GROUP is successfully selected.
5062       (or gnus-newsgroup-headers t)))))
5063
5064 (defun gnus-compute-unseen-list ()
5065   ;; The `seen' marks are treated specially.
5066   (if (not gnus-newsgroup-seen)
5067       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5068     (setq gnus-newsgroup-unseen
5069           (gnus-inverse-list-range-intersection
5070            gnus-newsgroup-articles gnus-newsgroup-seen))))
5071
5072 (defun gnus-summary-display-make-predicate (display)
5073   (require 'gnus-agent)
5074   (when (= (length display) 1)
5075     (setq display (car display)))
5076   (unless gnus-summary-display-cache
5077     (dolist (elem (append '((unread . unread)
5078                             (read . read)
5079                             (unseen . unseen))
5080                           gnus-article-mark-lists))
5081       (push (cons (cdr elem)
5082                   (gnus-byte-compile
5083                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5084             gnus-summary-display-cache)))
5085   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5086         (gnus-category-predicate-cache gnus-summary-display-cache))
5087     (gnus-get-predicate display)))
5088
5089 ;; Uses the dynamically bound `number' variable.
5090 (eval-when-compile
5091   (defvar number))
5092 (defun gnus-article-marked-p (type &optional article)
5093   (let ((article (or article number)))
5094     (cond
5095      ((eq type 'tick)
5096       (memq article gnus-newsgroup-marked))
5097      ((eq type 'spam)
5098       (memq article gnus-newsgroup-spam-marked))
5099      ((eq type 'unsend)
5100       (memq article gnus-newsgroup-unsendable))
5101      ((eq type 'undownload)
5102       (memq article gnus-newsgroup-undownloaded))
5103      ((eq type 'download)
5104       (memq article gnus-newsgroup-downloadable))
5105      ((eq type 'unread)
5106       (memq article gnus-newsgroup-unreads))
5107      ((eq type 'read)
5108       (memq article gnus-newsgroup-reads))
5109      ((eq type 'dormant)
5110       (memq article gnus-newsgroup-dormant) )
5111      ((eq type 'expire)
5112       (memq article gnus-newsgroup-expirable))
5113      ((eq type 'reply)
5114       (memq article gnus-newsgroup-replied))
5115      ((eq type 'killed)
5116       (memq article gnus-newsgroup-killed))
5117      ((eq type 'bookmark)
5118       (assq article gnus-newsgroup-bookmarks))
5119      ((eq type 'score)
5120       (assq article gnus-newsgroup-scored))
5121      ((eq type 'save)
5122       (memq article gnus-newsgroup-saved))
5123      ((eq type 'cache)
5124       (memq article gnus-newsgroup-cached))
5125      ((eq type 'forward)
5126       (memq article gnus-newsgroup-forwarded))
5127      ((eq type 'seen)
5128       (not (memq article gnus-newsgroup-unseen)))
5129      ((eq type 'recent)
5130       (memq article gnus-newsgroup-recent))
5131      (t t))))
5132
5133 (defun gnus-articles-to-read (group &optional read-all)
5134   "Find out what articles the user wants to read."
5135   (let* ((display (gnus-group-find-parameter group 'display))
5136          (articles
5137           ;; Select all articles if `read-all' is non-nil, or if there
5138           ;; are no unread articles.
5139           (if (or read-all
5140                   (and (zerop (length gnus-newsgroup-marked))
5141                        (zerop (length gnus-newsgroup-unreads)))
5142                   ;; Fetch all if the predicate is non-nil.
5143                   gnus-newsgroup-display)
5144               ;; We want to select the headers for all the articles in
5145               ;; the group, so we select either all the active
5146               ;; articles in the group, or (if that's nil), the
5147               ;; articles in the cache.
5148               (or
5149                (gnus-uncompress-range (gnus-active group))
5150                (gnus-cache-articles-in-group group))
5151             ;; Select only the "normal" subset of articles.
5152             (gnus-sorted-nunion
5153              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5154              gnus-newsgroup-unreads)))
5155          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5156          (scored (length scored-list))
5157          (number (length articles))
5158          (marked (+ (length gnus-newsgroup-marked)
5159                     (length gnus-newsgroup-dormant)))
5160          (select
5161           (cond
5162            ((numberp read-all)
5163             read-all)
5164            ((numberp gnus-newsgroup-display)
5165             gnus-newsgroup-display)
5166            (t
5167             (condition-case ()
5168                 (cond
5169                  ((and (or (<= scored marked) (= scored number))
5170                        (numberp gnus-large-newsgroup)
5171                        (> number gnus-large-newsgroup))
5172                   (let* ((cursor-in-echo-area nil)
5173                          (initial (gnus-parameter-large-newsgroup-initial
5174                                    gnus-newsgroup-name))
5175                          (input
5176                           (read-string
5177                            (format
5178                             "How many articles from %s (%s %d): "
5179                             (gnus-limit-string
5180                              (gnus-group-decoded-name gnus-newsgroup-name)
5181                              35)
5182                             (if initial "max" "default")
5183                             number)
5184                            (if initial
5185                                (cons (number-to-string initial)
5186                                      0)))))
5187                     (if (string-match "^[ \t]*$" input) number input)))
5188                  ((and (> scored marked) (< scored number)
5189                        (> (- scored number) 20))
5190                   (let ((input
5191                          (read-string
5192                           (format "%s %s (%d scored, %d total): "
5193                                   "How many articles from"
5194                                   (gnus-group-decoded-name group)
5195                                   scored number))))
5196                     (if (string-match "^[ \t]*$" input)
5197                         number input)))
5198                  (t number))
5199               (quit
5200                (message "Quit getting the articles to read")
5201                nil))))))
5202     (setq select (if (stringp select) (string-to-number select) select))
5203     (if (or (null select) (zerop select))
5204         select
5205       (if (and (not (zerop scored)) (<= (abs select) scored))
5206           (progn
5207             (setq articles (sort scored-list '<))
5208             (setq number (length articles)))
5209         (setq articles (copy-sequence articles)))
5210
5211       (when (< (abs select) number)
5212         (if (< select 0)
5213             ;; Select the N oldest articles.
5214             (setcdr (nthcdr (1- (abs select)) articles) nil)
5215           ;; Select the N most recent articles.
5216           (setq articles (nthcdr (- number select) articles))))
5217       (setq gnus-newsgroup-unselected
5218             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5219       (when gnus-alter-articles-to-read-function
5220         (setq articles
5221               (sort
5222                (funcall gnus-alter-articles-to-read-function
5223                         gnus-newsgroup-name articles)
5224                '<)))
5225       articles)))
5226
5227 (defun gnus-killed-articles (killed articles)
5228   (let (out)
5229     (while articles
5230       (when (inline (gnus-member-of-range (car articles) killed))
5231         (push (car articles) out))
5232       (setq articles (cdr articles)))
5233     out))
5234
5235 (defun gnus-uncompress-marks (marks)
5236   "Uncompress the mark ranges in MARKS."
5237   (let ((uncompressed '(score bookmark))
5238         out)
5239     (while marks
5240       (if (memq (caar marks) uncompressed)
5241           (push (car marks) out)
5242         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5243       (setq marks (cdr marks)))
5244     out))
5245
5246 (defun gnus-article-mark-to-type (mark)
5247   "Return the type of MARK."
5248   (or (cadr (assq mark gnus-article-special-mark-lists))
5249       'list))
5250
5251 (defun gnus-article-unpropagatable-p (mark)
5252   "Return whether MARK should be propagated to backend."
5253   (memq mark gnus-article-unpropagated-mark-lists))
5254
5255 (defun gnus-adjust-marked-articles (info)
5256   "Set all article lists and remove all marks that are no longer valid."
5257   (let* ((marked-lists (gnus-info-marks info))
5258          (active (gnus-active (gnus-info-group info)))
5259          (min (car active))
5260          (max (cdr active))
5261          (types gnus-article-mark-lists)
5262          marks var articles article mark mark-type)
5263
5264     (dolist (marks marked-lists)
5265       (setq mark (car marks)
5266             mark-type (gnus-article-mark-to-type mark)
5267             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5268
5269       ;; We set the variable according to the type of the marks list,
5270       ;; and then adjust the marks to a subset of the active articles.
5271       (cond
5272        ;; Adjust "simple" lists.
5273        ((eq mark-type 'list)
5274         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5275         (when (memq mark '(tick dormant expire reply save))
5276           (while articles
5277             (when (or (< (setq article (pop articles)) min) (> article max))
5278               (set var (delq article (symbol-value var)))))))
5279        ;; Adjust assocs.
5280        ((eq mark-type 'tuple)
5281         (set var (setq articles (cdr marks)))
5282         (when (not (listp (cdr (symbol-value var))))
5283           (set var (list (symbol-value var))))
5284         (when (not (listp (cdr articles)))
5285           (setq articles (list articles)))
5286         (while articles
5287           (when (or (not (consp (setq article (pop articles))))
5288                     (< (car article) min)
5289                     (> (car article) max))
5290             (set var (delq article (symbol-value var))))))
5291        ;; Adjust ranges (sloppily).
5292        ((eq mark-type 'range)
5293         (cond
5294          ((eq mark 'seen)
5295           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5296           ;; It should be (seen (NUM1 . NUM2)).
5297           (when (numberp (cddr marks))
5298             (setcdr marks (list (cdr marks))))
5299           (setq articles (cdr marks))
5300           (while (and articles
5301                       (or (and (consp (car articles))
5302                                (> min (cdar articles)))
5303                           (and (numberp (car articles))
5304                                (> min (car articles)))))
5305             (pop articles))
5306           (set var articles))))))))
5307
5308 (defun gnus-update-missing-marks (missing)
5309   "Go through the list of MISSING articles and remove them from the mark lists."
5310   (when missing
5311     (let (var m)
5312       ;; Go through all types.
5313       (dolist (elem gnus-article-mark-lists)
5314         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5315           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5316           (when (symbol-value var)
5317             ;; This list has articles.  So we delete all missing
5318             ;; articles from it.
5319             (setq m missing)
5320             (while m
5321               (set var (delq (pop m) (symbol-value var))))))))))
5322
5323 (defun gnus-update-marks ()
5324   "Enter the various lists of marked articles into the newsgroup info list."
5325   (let ((types gnus-article-mark-lists)
5326         (info (gnus-get-info gnus-newsgroup-name))
5327         type list newmarked symbol delta-marks)
5328     (when info
5329       ;; Add all marks lists to the list of marks lists.
5330       (while (setq type (pop types))
5331         (setq list (symbol-value
5332                     (setq symbol
5333                           (intern (format "gnus-newsgroup-%s" (car type))))))
5334
5335         (when list
5336           ;; Get rid of the entries of the articles that have the
5337           ;; default score.
5338           (when (and (eq (cdr type) 'score)
5339                      gnus-save-score
5340                      list)
5341             (let* ((arts list)
5342                    (prev (cons nil list))
5343                    (all prev))
5344               (while arts
5345                 (if (or (not (consp (car arts)))
5346                         (= (cdar arts) gnus-summary-default-score))
5347                     (setcdr prev (cdr arts))
5348                   (setq prev arts))
5349                 (setq arts (cdr arts)))
5350               (setq list (cdr all)))))
5351
5352         (when (eq (cdr type) 'seen)
5353           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5354
5355         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5356           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5357
5358         (when (and (gnus-check-backend-function
5359                     'request-set-mark gnus-newsgroup-name)
5360                    (not (gnus-article-unpropagatable-p (cdr type))))
5361           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5362                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5363                  (add (gnus-remove-from-range
5364                        (gnus-copy-sequence list) old)))
5365             (when add
5366               (push (list add 'add (list (cdr type))) delta-marks))
5367             (when del
5368               (push (list del 'del (list (cdr type))) delta-marks))))
5369
5370         (when list
5371           (push (cons (cdr type) list) newmarked)))
5372
5373       (when delta-marks
5374         (unless (gnus-check-group gnus-newsgroup-name)
5375           (error "Can't open server for %s" gnus-newsgroup-name))
5376         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5377
5378       ;; Enter these new marks into the info of the group.
5379       (if (nthcdr 3 info)
5380           (setcar (nthcdr 3 info) newmarked)
5381         ;; Add the marks lists to the end of the info.
5382         (when newmarked
5383           (setcdr (nthcdr 2 info) (list newmarked))))
5384
5385       ;; Cut off the end of the info if there's nothing else there.
5386       (let ((i 5))
5387         (while (and (> i 2)
5388                     (not (nth i info)))
5389           (when (nthcdr (decf i) info)
5390             (setcdr (nthcdr i info) nil)))))))
5391
5392 (defun gnus-set-mode-line (where)
5393   "Set the mode line of the article or summary buffers.
5394 If WHERE is `summary', the summary mode line format will be used."
5395   ;; Is this mode line one we keep updated?
5396   (when (and (memq where gnus-updated-mode-lines)
5397              (symbol-value
5398               (intern (format "gnus-%s-mode-line-format-spec" where))))
5399     (let (mode-string)
5400       (save-excursion
5401         ;; We evaluate this in the summary buffer since these
5402         ;; variables are buffer-local to that buffer.
5403         (set-buffer gnus-summary-buffer)
5404        ;; We bind all these variables that are used in the `eval' form
5405         ;; below.
5406         (let* ((mformat (symbol-value
5407                          (intern
5408                           (format "gnus-%s-mode-line-format-spec" where))))
5409                (gnus-tmp-group-name (gnus-group-decoded-name
5410                                      gnus-newsgroup-name))
5411                (gnus-tmp-article-number (or gnus-current-article 0))
5412                (gnus-tmp-unread gnus-newsgroup-unreads)
5413                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5414                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5415                (gnus-tmp-unread-and-unselected
5416                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5417                             (zerop gnus-tmp-unselected))
5418                        "")
5419                       ((zerop gnus-tmp-unselected)
5420                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5421                       (t (format "{%d(+%d) more}"
5422                                  gnus-tmp-unread-and-unticked
5423                                  gnus-tmp-unselected))))
5424                (gnus-tmp-subject
5425                 (if (and gnus-current-headers
5426                          (vectorp gnus-current-headers))
5427                     (gnus-mode-string-quote
5428                      (mail-header-subject gnus-current-headers))
5429                   ""))
5430                bufname-length max-len
5431                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5432           (setq mode-string (eval mformat))
5433           (setq bufname-length (if (string-match "%b" mode-string)
5434                                    (- (length
5435                                        (buffer-name
5436                                         (if (eq where 'summary)
5437                                             nil
5438                                           (get-buffer gnus-article-buffer))))
5439                                       2)
5440                                  0))
5441           (setq max-len (max 4 (if gnus-mode-non-string-length
5442                                    (- (window-width)
5443                                       gnus-mode-non-string-length
5444                                       bufname-length)
5445                                  (length mode-string))))
5446           ;; We might have to chop a bit of the string off...
5447           (when (> (length mode-string) max-len)
5448             (setq mode-string
5449                   (concat (truncate-string-to-width mode-string (- max-len 3))
5450                           "...")))
5451           ;; Pad the mode string a bit.
5452           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5453       ;; Update the mode line.
5454       (setq mode-line-buffer-identification
5455             (gnus-mode-line-buffer-identification (list mode-string)))
5456       (set-buffer-modified-p t))))
5457
5458 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5459   "Go through the HEADERS list and add all Xrefs to a hash table.
5460 The resulting hash table is returned, or nil if no Xrefs were found."
5461   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5462          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5463          (xref-hashtb (gnus-make-hashtable))
5464          start group entry number xrefs header)
5465     (while headers
5466       (setq header (pop headers))
5467       (when (and (setq xrefs (mail-header-xref header))
5468                  (not (memq (setq number (mail-header-number header))
5469                             unreads)))
5470         (setq start 0)
5471         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5472           (setq start (match-end 0))
5473           (setq group (if prefix
5474                           (concat prefix (substring xrefs (match-beginning 1)
5475                                                     (match-end 1)))
5476                         (substring xrefs (match-beginning 1) (match-end 1))))
5477           (setq number
5478                 (string-to-int (substring xrefs (match-beginning 2)
5479                                           (match-end 2))))
5480           (if (setq entry (gnus-gethash group xref-hashtb))
5481               (setcdr entry (cons number (cdr entry)))
5482             (gnus-sethash group (cons number nil) xref-hashtb)))))
5483     (and start xref-hashtb)))
5484
5485 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5486   "Look through all the headers and mark the Xrefs as read."
5487   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5488         name entry info xref-hashtb idlist method nth4)
5489     (save-excursion
5490       (set-buffer gnus-group-buffer)
5491       (when (setq xref-hashtb
5492                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5493         (mapatoms
5494          (lambda (group)
5495            (unless (string= from-newsgroup (setq name (symbol-name group)))
5496              (setq idlist (symbol-value group))
5497              ;; Dead groups are not updated.
5498              (and (prog1
5499                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5500                             info (nth 2 entry))
5501                     (when (stringp (setq nth4 (gnus-info-method info)))
5502                       (setq nth4 (gnus-server-to-method nth4))))
5503                   ;; Only do the xrefs if the group has the same
5504                   ;; select method as the group we have just read.
5505                   (or (gnus-methods-equal-p
5506                        nth4 (gnus-find-method-for-group from-newsgroup))
5507                       virtual
5508                       (equal nth4 (setq method (gnus-find-method-for-group
5509                                                 from-newsgroup)))
5510                       (and (equal (car nth4) (car method))
5511                            (equal (nth 1 nth4) (nth 1 method))))
5512                   gnus-use-cross-reference
5513                   (or (not (eq gnus-use-cross-reference t))
5514                       virtual
5515                       ;; Only do cross-references on subscribed
5516                       ;; groups, if that is what is wanted.
5517                       (<= (gnus-info-level info) gnus-level-subscribed))
5518                   (gnus-group-make-articles-read name idlist))))
5519          xref-hashtb)))))
5520
5521 (defun gnus-compute-read-articles (group articles)
5522   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5523          (info (nth 2 entry))
5524          (active (gnus-active group))
5525          ninfo)
5526     (when entry
5527       ;; First peel off all invalid article numbers.
5528       (when active
5529         (let ((ids articles)
5530               id first)
5531           (while (setq id (pop ids))
5532             (when (and first (> id (cdr active)))
5533               ;; We'll end up in this situation in one particular
5534               ;; obscure situation.  If you re-scan a group and get
5535               ;; a new article that is cross-posted to a different
5536               ;; group that has not been re-scanned, you might get
5537               ;; crossposted article that has a higher number than
5538               ;; Gnus believes possible.  So we re-activate this
5539               ;; group as well.  This might mean doing the
5540               ;; crossposting thingy will *increase* the number
5541               ;; of articles in some groups.  Tsk, tsk.
5542               (setq active (or (gnus-activate-group group) active)))
5543             (when (or (> id (cdr active))
5544                       (< id (car active)))
5545               (setq articles (delq id articles))))))
5546       ;; If the read list is nil, we init it.
5547       (if (and active
5548                (null (gnus-info-read info))
5549                (> (car active) 1))
5550           (setq ninfo (cons 1 (1- (car active))))
5551         (setq ninfo (gnus-info-read info)))
5552       ;; Then we add the read articles to the range.
5553       (gnus-add-to-range
5554        ninfo (setq articles (sort articles '<))))))
5555
5556 (defun gnus-group-make-articles-read (group articles)
5557   "Update the info of GROUP to say that ARTICLES are read."
5558   (let* ((num 0)
5559          (entry (gnus-gethash group gnus-newsrc-hashtb))
5560          (info (nth 2 entry))
5561          (active (gnus-active group))
5562          range)
5563     (when entry
5564       (setq range (gnus-compute-read-articles group articles))
5565       (save-excursion
5566         (set-buffer gnus-group-buffer)
5567         (gnus-undo-register
5568           `(progn
5569              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5570              (gnus-info-set-read ',info ',(gnus-info-read info))
5571              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5572              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5573              (gnus-group-update-group ,group t))))
5574       ;; Add the read articles to the range.
5575       (gnus-info-set-read info range)
5576       (gnus-request-set-mark group (list (list range 'add '(read))))
5577       ;; Then we have to re-compute how many unread
5578       ;; articles there are in this group.
5579       (when active
5580         (cond
5581          ((not range)
5582           (setq num (- (1+ (cdr active)) (car active))))
5583          ((not (listp (cdr range)))
5584           (setq num (- (cdr active) (- (1+ (cdr range))
5585                                        (car range)))))
5586          (t
5587           (while range
5588             (if (numberp (car range))
5589                 (setq num (1+ num))
5590               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5591             (setq range (cdr range)))
5592           (setq num (- (cdr active) num))))
5593         ;; Update the number of unread articles.
5594         (setcar entry num)
5595         ;; Update the group buffer.
5596         (unless (gnus-ephemeral-group-p group)
5597           (gnus-group-update-group group t))))))
5598
5599 (defvar gnus-newsgroup-none-id 0)
5600
5601 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5602   (let ((cur nntp-server-buffer)
5603         (dependencies
5604          (or dependencies
5605              (save-excursion (set-buffer gnus-summary-buffer)
5606                              gnus-newsgroup-dependencies)))
5607         headers id end ref
5608         (mail-parse-charset gnus-newsgroup-charset)
5609         (mail-parse-ignored-charsets
5610          (save-excursion (condition-case nil
5611                              (set-buffer gnus-summary-buffer)
5612                            (error))
5613                          gnus-newsgroup-ignored-charsets)))
5614     (save-excursion
5615       (set-buffer nntp-server-buffer)
5616       ;; Translate all TAB characters into SPACE characters.
5617       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5618       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5619       (gnus-run-hooks 'gnus-parse-headers-hook)
5620       (let ((case-fold-search t)
5621             in-reply-to header p lines chars)
5622         (goto-char (point-min))
5623         ;; Search to the beginning of the next header.  Error messages
5624         ;; do not begin with 2 or 3.
5625         (while (re-search-forward "^[23][0-9]+ " nil t)
5626           (setq id nil
5627                 ref nil)
5628           ;; This implementation of this function, with nine
5629           ;; search-forwards instead of the one re-search-forward and
5630           ;; a case (which basically was the old function) is actually
5631           ;; about twice as fast, even though it looks messier.  You
5632           ;; can't have everything, I guess.  Speed and elegance
5633           ;; doesn't always go hand in hand.
5634           (setq
5635            header
5636            (vector
5637             ;; Number.
5638             (prog1
5639                 (read cur)
5640               (end-of-line)
5641               (setq p (point))
5642               (narrow-to-region (point)
5643                                 (or (and (search-forward "\n.\n" nil t)
5644                                          (- (point) 2))
5645                                     (point))))
5646             ;; Subject.
5647             (progn
5648               (goto-char p)
5649               (if (search-forward "\nsubject:" nil t)
5650                   (funcall gnus-decode-encoded-word-function
5651                            (nnheader-header-value))
5652                 "(none)"))
5653             ;; From.
5654             (progn
5655               (goto-char p)
5656               (if (search-forward "\nfrom:" nil t)
5657                   (funcall gnus-decode-encoded-word-function
5658                            (nnheader-header-value))
5659                 "(nobody)"))
5660             ;; Date.
5661             (progn
5662               (goto-char p)
5663               (if (search-forward "\ndate:" nil t)
5664                   (nnheader-header-value) ""))
5665             ;; Message-ID.
5666             (progn
5667               (goto-char p)
5668               (setq id (if (re-search-forward
5669                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5670                            ;; We do it this way to make sure the Message-ID
5671                            ;; is (somewhat) syntactically valid.
5672                            (buffer-substring (match-beginning 1)
5673                                              (match-end 1))
5674                          ;; If there was no message-id, we just fake one
5675                          ;; to make subsequent routines simpler.
5676                          (nnheader-generate-fake-message-id))))
5677             ;; References.
5678             (progn
5679               (goto-char p)
5680               (if (search-forward "\nreferences:" nil t)
5681                   (progn
5682                     (setq end (point))
5683                     (prog1
5684                         (nnheader-header-value)
5685                       (setq ref
5686                             (buffer-substring
5687                              (progn
5688                                (end-of-line)
5689                                (search-backward ">" end t)
5690                                (1+ (point)))
5691                              (progn
5692                                (search-backward "<" end t)
5693                                (point))))))
5694                 ;; Get the references from the in-reply-to header if there
5695                 ;; were no references and the in-reply-to header looks
5696                 ;; promising.
5697                 (if (and (search-forward "\nin-reply-to:" nil t)
5698                          (setq in-reply-to (nnheader-header-value))
5699                          (string-match "<[^>]+>" in-reply-to))
5700                     (let (ref2)
5701                       (setq ref (substring in-reply-to (match-beginning 0)
5702                                            (match-end 0)))
5703                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5704                         (setq ref2 (substring in-reply-to (match-beginning 0)
5705                                               (match-end 0)))
5706                         (when (> (length ref2) (length ref))
5707                           (setq ref ref2)))
5708                       ref)
5709                   (setq ref nil))))
5710             ;; Chars.
5711             (progn
5712               (goto-char p)
5713               (if (search-forward "\nchars: " nil t)
5714                   (if (numberp (setq chars (ignore-errors (read cur))))
5715                       chars -1)
5716                 -1))
5717             ;; Lines.
5718             (progn
5719               (goto-char p)
5720               (if (search-forward "\nlines: " nil t)
5721                   (if (numberp (setq lines (ignore-errors (read cur))))
5722                       lines -1)
5723                 -1))
5724             ;; Xref.
5725             (progn
5726               (goto-char p)
5727               (and (search-forward "\nxref:" nil t)
5728                    (nnheader-header-value)))
5729             ;; Extra.
5730             (when gnus-extra-headers
5731               (let ((extra gnus-extra-headers)
5732                     out)
5733                 (while extra
5734                   (goto-char p)
5735                   (when (search-forward
5736                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5737                     (push (cons (car extra) (nnheader-header-value))
5738                           out))
5739                   (pop extra))
5740                 out))))
5741           (when (equal id ref)
5742             (setq ref nil))
5743
5744           (when gnus-alter-header-function
5745             (funcall gnus-alter-header-function header)
5746             (setq id (mail-header-id header)
5747                   ref (gnus-parent-id (mail-header-references header))))
5748
5749           (when (setq header
5750                       (gnus-dependencies-add-header
5751                        header dependencies force-new))
5752             (push header headers))
5753           (goto-char (point-max))
5754           (widen))
5755         (nreverse headers)))))
5756
5757 ;; Goes through the xover lines and returns a list of vectors
5758 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5759                                                   force-new dependencies
5760                                                   group also-fetch-heads)
5761   "Parse the news overview data in the server buffer.
5762 Return a list of headers that match SEQUENCE (see
5763 `nntp-retrieve-headers')."
5764   ;; Get the Xref when the users reads the articles since most/some
5765   ;; NNTP servers do not include Xrefs when using XOVER.
5766   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5767   (let ((mail-parse-charset gnus-newsgroup-charset)
5768         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5769         (cur nntp-server-buffer)
5770         (dependencies (or dependencies gnus-newsgroup-dependencies))
5771         (allp (cond
5772                ((eq gnus-read-all-available-headers t)
5773                 t)
5774                ((stringp gnus-read-all-available-headers)
5775                 (string-match gnus-read-all-available-headers group))
5776                (t
5777                 nil)))
5778         number headers header)
5779     (save-excursion
5780       (set-buffer nntp-server-buffer)
5781       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5782       ;; Allow the user to mangle the headers before parsing them.
5783       (gnus-run-hooks 'gnus-parse-headers-hook)
5784       (goto-char (point-min))
5785       (gnus-parse-without-error
5786         (while (and (or sequence allp)
5787                     (not (eobp)))
5788           (setq number (read cur))
5789           (when (not allp)
5790             (while (and sequence
5791                         (< (car sequence) number))
5792               (setq sequence (cdr sequence))))
5793           (when (and (or allp
5794                          (and sequence
5795                               (eq number (car sequence))))
5796                      (progn
5797                        (setq sequence (cdr sequence))
5798                        (setq header (inline
5799                                       (gnus-nov-parse-line
5800                                        number dependencies force-new)))))
5801             (push header headers))
5802           (forward-line 1)))
5803       ;; A common bug in inn is that if you have posted an article and
5804       ;; then retrieves the active file, it will answer correctly --
5805       ;; the new article is included.  However, a NOV entry for the
5806       ;; article may not have been generated yet, so this may fail.
5807       ;; We work around this problem by retrieving the last few
5808       ;; headers using HEAD.
5809       (if (or (not also-fetch-heads)
5810               (not sequence))
5811           ;; We (probably) got all the headers.
5812           (nreverse headers)
5813         (let ((gnus-nov-is-evil t))
5814           (nconc
5815            (nreverse headers)
5816            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5817              (gnus-get-newsgroup-headers))))))))
5818
5819 (defun gnus-article-get-xrefs ()
5820   "Fill in the Xref value in `gnus-current-headers', if necessary.
5821 This is meant to be called in `gnus-article-internal-prepare-hook'."
5822   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5823                                  gnus-current-headers)))
5824     (or (not gnus-use-cross-reference)
5825         (not headers)
5826         (and (mail-header-xref headers)
5827              (not (string= (mail-header-xref headers) "")))
5828         (let ((case-fold-search t)
5829               xref)
5830           (save-restriction
5831             (nnheader-narrow-to-headers)
5832             (goto-char (point-min))
5833             (when (or (and (not (eobp))
5834                            (eq (downcase (char-after)) ?x)
5835                            (looking-at "Xref:"))
5836                       (search-forward "\nXref:" nil t))
5837               (goto-char (1+ (match-end 0)))
5838               (setq xref (buffer-substring (point)
5839                                            (progn (end-of-line) (point))))
5840               (mail-header-set-xref headers xref)))))))
5841
5842 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5843   "Find article ID and insert the summary line for that article.
5844 OLD-HEADER can either be a header or a line number to insert
5845 the subject line on."
5846   (let* ((line (and (numberp old-header) old-header))
5847          (old-header (and (vectorp old-header) old-header))
5848          (header (cond ((and old-header use-old-header)
5849                         old-header)
5850                        ((and (numberp id)
5851                              (gnus-number-to-header id))
5852                         (gnus-number-to-header id))
5853                        (t
5854                         (gnus-read-header id))))
5855          (number (and (numberp id) id))
5856          d)
5857     (when header
5858       ;; Rebuild the thread that this article is part of and go to the
5859       ;; article we have fetched.
5860       (when (and (not gnus-show-threads)
5861                  old-header)
5862         (when (and number
5863                    (setq d (gnus-data-find (mail-header-number old-header))))
5864           (goto-char (gnus-data-pos d))
5865           (gnus-data-remove
5866            number
5867            (- (gnus-point-at-bol)
5868               (prog1
5869                   (1+ (gnus-point-at-eol))
5870                 (gnus-delete-line))))))
5871       (when old-header
5872         (mail-header-set-number header (mail-header-number old-header)))
5873       (setq gnus-newsgroup-sparse
5874             (delq (setq number (mail-header-number header))
5875                   gnus-newsgroup-sparse))
5876       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5877       (push number gnus-newsgroup-limit)
5878       (gnus-rebuild-thread (mail-header-id header) line)
5879       (gnus-summary-goto-subject number nil t))
5880     (when (and (numberp number)
5881                (> number 0))
5882       ;; We have to update the boundaries even if we can't fetch the
5883       ;; article if ID is a number -- so that the next `P' or `N'
5884       ;; command will fetch the previous (or next) article even
5885       ;; if the one we tried to fetch this time has been canceled.
5886       (when (> number gnus-newsgroup-end)
5887         (setq gnus-newsgroup-end number))
5888       (when (< number gnus-newsgroup-begin)
5889         (setq gnus-newsgroup-begin number))
5890       (setq gnus-newsgroup-unselected
5891             (delq number gnus-newsgroup-unselected)))
5892     ;; Report back a success?
5893     (and header (mail-header-number header))))
5894
5895 ;;; Process/prefix in the summary buffer
5896
5897 (defun gnus-summary-work-articles (n)
5898   "Return a list of articles to be worked upon.
5899 The prefix argument, the list of process marked articles, and the
5900 current article will be taken into consideration."
5901   (save-excursion
5902     (set-buffer gnus-summary-buffer)
5903     (cond
5904      (n
5905       ;; A numerical prefix has been given.
5906       (setq n (prefix-numeric-value n))
5907       (let ((backward (< n 0))
5908             (n (abs (prefix-numeric-value n)))
5909             articles article)
5910         (save-excursion
5911           (while
5912               (and (> n 0)
5913                    (push (setq article (gnus-summary-article-number))
5914                          articles)
5915                    (if backward
5916                        (gnus-summary-find-prev nil article)
5917                      (gnus-summary-find-next nil article)))
5918             (decf n)))
5919         (nreverse articles)))
5920      ((and (gnus-region-active-p) (mark))
5921       (message "region active")
5922       ;; Work on the region between point and mark.
5923       (let ((max (max (point) (mark)))
5924             articles article)
5925         (save-excursion
5926           (goto-char (min (min (point) (mark))))
5927           (while
5928               (and
5929                (push (setq article (gnus-summary-article-number)) articles)
5930                (gnus-summary-find-next nil article)
5931                (< (point) max)))
5932           (nreverse articles))))
5933      (gnus-newsgroup-processable
5934       ;; There are process-marked articles present.
5935       ;; Save current state.
5936       (gnus-summary-save-process-mark)
5937       ;; Return the list.
5938       (reverse gnus-newsgroup-processable))
5939      (t
5940       ;; Just return the current article.
5941       (list (gnus-summary-article-number))))))
5942
5943 (defmacro gnus-summary-iterate (arg &rest forms)
5944   "Iterate over the process/prefixed articles and do FORMS.
5945 ARG is the interactive prefix given to the command.  FORMS will be
5946 executed with point over the summary line of the articles."
5947   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5948     `(let ((,articles (gnus-summary-work-articles ,arg)))
5949        (while ,articles
5950          (gnus-summary-goto-subject (car ,articles))
5951          ,@forms
5952          (pop ,articles)))))
5953
5954 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5955 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5956
5957 (defun gnus-summary-save-process-mark ()
5958   "Push the current set of process marked articles on the stack."
5959   (interactive)
5960   (push (copy-sequence gnus-newsgroup-processable)
5961         gnus-newsgroup-process-stack))
5962
5963 (defun gnus-summary-kill-process-mark ()
5964   "Push the current set of process marked articles on the stack and unmark."
5965   (interactive)
5966   (gnus-summary-save-process-mark)
5967   (gnus-summary-unmark-all-processable))
5968
5969 (defun gnus-summary-yank-process-mark ()
5970   "Pop the last process mark state off the stack and restore it."
5971   (interactive)
5972   (unless gnus-newsgroup-process-stack
5973     (error "Empty mark stack"))
5974   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5975
5976 (defun gnus-summary-process-mark-set (set)
5977   "Make SET into the current process marked articles."
5978   (gnus-summary-unmark-all-processable)
5979   (while set
5980     (gnus-summary-set-process-mark (pop set))))
5981
5982 ;;; Searching and stuff
5983
5984 (defun gnus-summary-search-group (&optional backward use-level)
5985   "Search for next unread newsgroup.
5986 If optional argument BACKWARD is non-nil, search backward instead."
5987   (save-excursion
5988     (set-buffer gnus-group-buffer)
5989     (when (gnus-group-search-forward
5990            backward nil (if use-level (gnus-group-group-level) nil))
5991       (gnus-group-group-name))))
5992
5993 (defun gnus-summary-best-group (&optional exclude-group)
5994   "Find the name of the best unread group.
5995 If EXCLUDE-GROUP, do not go to this group."
5996   (save-excursion
5997     (set-buffer gnus-group-buffer)
5998     (save-excursion
5999       (gnus-group-best-unread-group exclude-group))))
6000
6001 (defun gnus-summary-find-next (&optional unread article backward)
6002   (if backward (gnus-summary-find-prev)
6003     (let* ((dummy (gnus-summary-article-intangible-p))
6004            (article (or article (gnus-summary-article-number)))
6005            (data (gnus-data-find-list article))
6006            result)
6007       (when (and (not dummy)
6008                  (or (not gnus-summary-check-current)
6009                      (not unread)
6010                      (not (gnus-data-unread-p (car data)))))
6011         (setq data (cdr data)))
6012       (when (setq result
6013                   (if unread
6014                       (progn
6015                         (while data
6016                           (unless (memq (gnus-data-number (car data)) 
6017                                         gnus-newsgroup-unfetched)
6018                             (when (gnus-data-unread-p (car data))
6019                               (setq result (car data)
6020                                     data nil)))
6021                           (setq data (cdr data)))
6022                         result)
6023                     (car data)))
6024         (goto-char (gnus-data-pos result))
6025         (gnus-data-number result)))))
6026
6027 (defun gnus-summary-find-prev (&optional unread article)
6028   (let* ((eobp (eobp))
6029          (article (or article (gnus-summary-article-number)))
6030          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6031          result)
6032     (when (and (not eobp)
6033                (or (not gnus-summary-check-current)
6034                    (not unread)
6035                    (not (gnus-data-unread-p (car data)))))
6036       (setq data (cdr data)))
6037     (when (setq result
6038                 (if unread
6039                     (progn
6040                       (while data
6041                         (unless (memq (gnus-data-number (car data)) gnus-newsgroup-unfetched)
6042                           (when (gnus-data-unread-p (car data))
6043                             (setq result (car data)
6044                                   data nil)))
6045                         (setq data (cdr data)))
6046                       result)
6047                   (car data)))
6048       (goto-char (gnus-data-pos result))
6049       (gnus-data-number result))))
6050
6051 (defun gnus-summary-find-subject (subject &optional unread backward article)
6052   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6053          (article (or article (gnus-summary-article-number)))
6054          (articles (gnus-data-list backward))
6055          (arts (gnus-data-find-list article articles))
6056          result)
6057     (when (or (not gnus-summary-check-current)
6058               (not unread)
6059               (not (gnus-data-unread-p (car arts))))
6060       (setq arts (cdr arts)))
6061     (while arts
6062       (and (or (not unread)
6063                (gnus-data-unread-p (car arts)))
6064            (vectorp (gnus-data-header (car arts)))
6065            (gnus-subject-equal
6066             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6067            (setq result (car arts)
6068                  arts nil))
6069       (setq arts (cdr arts)))
6070     (and result
6071          (goto-char (gnus-data-pos result))
6072          (gnus-data-number result))))
6073
6074 (defun gnus-summary-search-forward (&optional unread subject backward)
6075   "Search forward for an article.
6076 If UNREAD, look for unread articles.  If SUBJECT, look for
6077 articles with that subject.  If BACKWARD, search backward instead."
6078   (cond (subject (gnus-summary-find-subject subject unread backward))
6079         (backward (gnus-summary-find-prev unread))
6080         (t (gnus-summary-find-next unread))))
6081
6082 (defun gnus-recenter (&optional n)
6083   "Center point in window and redisplay frame.
6084 Also do horizontal recentering."
6085   (interactive "P")
6086   (when (and gnus-auto-center-summary
6087              (not (eq gnus-auto-center-summary 'vertical)))
6088     (gnus-horizontal-recenter))
6089   (recenter n))
6090
6091 (defun gnus-summary-recenter ()
6092   "Center point in the summary window.
6093 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6094 displayed, no centering will be performed."
6095   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6096 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6097   (interactive)
6098   (let* ((top (cond ((< (window-height) 4) 0)
6099                     ((< (window-height) 7) 1)
6100                     (t (if (numberp gnus-auto-center-summary)
6101                            gnus-auto-center-summary
6102                          2))))
6103          (height (1- (window-height)))
6104          (bottom (save-excursion (goto-char (point-max))
6105                                  (forward-line (- height))
6106                                  (point)))
6107          (window (get-buffer-window (current-buffer))))
6108     ;; The user has to want it.
6109     (when gnus-auto-center-summary
6110       (when (get-buffer-window gnus-article-buffer)
6111         ;; Only do recentering when the article buffer is displayed,
6112       ;; Set the window start to either `bottom', which is the biggest
6113         ;; possible valid number, or the second line from the top,
6114         ;; whichever is the least.
6115         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6116           (if (> bottom top-pos)
6117               ;; Keep the second line from the top visible
6118               (set-window-start window top-pos t)
6119             ;; Try to keep the bottom line visible; if it's partially
6120             ;; obscured, either scroll one more line to make it fully
6121             ;; visible, or revert to using TOP-POS.
6122             (save-excursion
6123               (goto-char (point-max))
6124               (forward-line -1)
6125               (let ((last-line-start (point)))
6126                 (goto-char bottom)
6127                 (set-window-start window (point) t)
6128                 (when (not (pos-visible-in-window-p last-line-start window))
6129                   (forward-line 1)
6130                   (set-window-start window (min (point) top-pos) t)))))))
6131       ;; Do horizontal recentering while we're at it.
6132       (when (and (get-buffer-window (current-buffer) t)
6133                  (not (eq gnus-auto-center-summary 'vertical)))
6134         (let ((selected (selected-window)))
6135           (select-window (get-buffer-window (current-buffer) t))
6136           (gnus-summary-position-point)
6137           (gnus-horizontal-recenter)
6138           (select-window selected))))))
6139
6140 (defun gnus-summary-jump-to-group (newsgroup)
6141   "Move point to NEWSGROUP in group mode buffer."
6142   ;; Keep update point of group mode buffer if visible.
6143   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6144       (save-window-excursion
6145         ;; Take care of tree window mode.
6146         (when (get-buffer-window gnus-group-buffer)
6147           (pop-to-buffer gnus-group-buffer))
6148         (gnus-group-jump-to-group newsgroup))
6149     (save-excursion
6150       ;; Take care of tree window mode.
6151       (if (get-buffer-window gnus-group-buffer)
6152           (pop-to-buffer gnus-group-buffer)
6153         (set-buffer gnus-group-buffer))
6154       (gnus-group-jump-to-group newsgroup))))
6155
6156 ;; This function returns a list of article numbers based on the
6157 ;; difference between the ranges of read articles in this group and
6158 ;; the range of active articles.
6159 (defun gnus-list-of-unread-articles (group)
6160   (let* ((read (gnus-info-read (gnus-get-info group)))
6161          (active (or (gnus-active group) (gnus-activate-group group)))
6162          (last (cdr active))
6163          first nlast unread)
6164     ;; If none are read, then all are unread.
6165     (if (not read)
6166         (setq first (car active))
6167       ;; If the range of read articles is a single range, then the
6168       ;; first unread article is the article after the last read
6169       ;; article.  Sounds logical, doesn't it?
6170       (if (and (not (listp (cdr read)))
6171                (or (< (car read) (car active))
6172                    (progn (setq read (list read))
6173                           nil)))
6174           (setq first (max (car active) (1+ (cdr read))))
6175         ;; `read' is a list of ranges.
6176         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6177                                   (caar read)))
6178                   1)
6179           (setq first (car active)))
6180         (while read
6181           (when first
6182             (while (< first nlast)
6183               (push first unread)
6184               (setq first (1+ first))))
6185           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6186           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6187           (setq read (cdr read)))))
6188     ;; And add the last unread articles.
6189     (while (<= first last)
6190       (push first unread)
6191       (setq first (1+ first)))
6192     ;; Return the list of unread articles.
6193     (delq 0 (nreverse unread))))
6194
6195 (defun gnus-list-of-read-articles (group)
6196   "Return a list of unread, unticked and non-dormant articles."
6197   (let* ((info (gnus-get-info group))
6198          (marked (gnus-info-marks info))
6199          (active (gnus-active group)))
6200     (and info active
6201          (gnus-list-range-difference
6202           (gnus-list-range-difference
6203            (gnus-sorted-complement
6204             (gnus-uncompress-range active)
6205             (gnus-list-of-unread-articles group))
6206            (cdr (assq 'dormant marked)))
6207           (cdr (assq 'tick marked))))))
6208
6209 ;; Various summary commands
6210
6211 (defun gnus-summary-select-article-buffer ()
6212   "Reconfigure windows to show article buffer."
6213   (interactive)
6214   (if (not (gnus-buffer-live-p gnus-article-buffer))
6215       (error "There is no article buffer for this summary buffer")
6216     (gnus-configure-windows 'article)
6217     (select-window (get-buffer-window gnus-article-buffer))))
6218
6219 (defun gnus-summary-universal-argument (arg)
6220   "Perform any operation on all articles that are process/prefixed."
6221   (interactive "P")
6222   (let ((articles (gnus-summary-work-articles arg))
6223         func article)
6224     (if (eq
6225          (setq
6226           func
6227           (key-binding
6228            (read-key-sequence
6229             (substitute-command-keys
6230              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6231          'undefined)
6232         (gnus-error 1 "Undefined key")
6233       (save-excursion
6234         (while articles
6235           (gnus-summary-goto-subject (setq article (pop articles)))
6236           (let (gnus-newsgroup-processable)
6237             (command-execute func))
6238           (gnus-summary-remove-process-mark article)))))
6239   (gnus-summary-position-point))
6240
6241 (defun gnus-summary-toggle-truncation (&optional arg)
6242   "Toggle truncation of summary lines.
6243 With arg, turn line truncation on if arg is positive."
6244   (interactive "P")
6245   (setq truncate-lines
6246         (if (null arg) (not truncate-lines)
6247           (> (prefix-numeric-value arg) 0)))
6248   (redraw-display))
6249
6250 (defun gnus-summary-find-uncancelled ()
6251   "Return the number of an uncancelled article.
6252 The current article is considered, then following articles, then previous
6253 articles.  If all articles are cancelled then return a dummy 0."
6254   (let (found)
6255     (dolist (rev '(nil t))
6256       (unless found      ; don't demand the reverse list if we don't need it
6257         (let ((data (gnus-data-find-list
6258                      (gnus-summary-article-number) (gnus-data-list rev))))
6259           (while (and data (not found))
6260             (if (not (eq gnus-canceled-mark (gnus-data-mark (car data))))
6261                 (setq found (gnus-data-number (car data))))
6262             (setq data (cdr data))))))
6263     (or found 0)))
6264
6265 (defun gnus-summary-reselect-current-group (&optional all rescan)
6266   "Exit and then reselect the current newsgroup.
6267 The prefix argument ALL means to select all articles."
6268   (interactive "P")
6269   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6270     (error "Ephemeral groups can't be reselected"))
6271   (let ((current-subject (gnus-summary-find-uncancelled))
6272         (group gnus-newsgroup-name))
6273     (setq gnus-newsgroup-begin nil)
6274     (gnus-summary-exit)
6275     ;; We have to adjust the point of group mode buffer because
6276     ;; point was moved to the next unread newsgroup by exiting.
6277     (gnus-summary-jump-to-group group)
6278     (when rescan
6279       (save-excursion
6280         (gnus-group-get-new-news-this-group 1)))
6281     (gnus-group-read-group all t)
6282     (gnus-summary-goto-subject current-subject nil t)))
6283
6284 (defun gnus-summary-rescan-group (&optional all)
6285   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6286   (interactive "P")
6287   (gnus-summary-reselect-current-group all t))
6288
6289 (defun gnus-summary-update-info (&optional non-destructive)
6290   (save-excursion
6291     (let ((group gnus-newsgroup-name))
6292       (when group
6293         (when gnus-newsgroup-kill-headers
6294           (setq gnus-newsgroup-killed
6295                 (gnus-compress-sequence
6296                  (gnus-sorted-union
6297                   (gnus-list-range-intersection
6298                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6299                   gnus-newsgroup-unreads)
6300                  t)))
6301         (unless (listp (cdr gnus-newsgroup-killed))
6302           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6303         (let ((headers gnus-newsgroup-headers))
6304           ;; Set the new ranges of read articles.
6305           (save-excursion
6306             (set-buffer gnus-group-buffer)
6307             (gnus-undo-force-boundary))
6308           (gnus-update-read-articles
6309            group (gnus-sorted-union
6310                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6311           ;; Set the current article marks.
6312           (let ((gnus-newsgroup-scored
6313                  (if (and (not gnus-save-score)
6314                           (not non-destructive))
6315                      nil
6316                    gnus-newsgroup-scored)))
6317             (save-excursion
6318               (gnus-update-marks)))
6319           ;; Do the cross-ref thing.
6320           (when gnus-use-cross-reference
6321             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6322           ;; Do not switch windows but change the buffer to work.
6323           (set-buffer gnus-group-buffer)
6324           (unless (gnus-ephemeral-group-p group)
6325             (gnus-group-update-group group)))))))
6326
6327 (defun gnus-summary-save-newsrc (&optional force)
6328   "Save the current number of read/marked articles in the dribble buffer.
6329 The dribble buffer will then be saved.
6330 If FORCE (the prefix), also save the .newsrc file(s)."
6331   (interactive "P")
6332   (gnus-summary-update-info t)
6333   (if force
6334       (gnus-save-newsrc-file)
6335     (gnus-dribble-save)))
6336
6337 (defun gnus-summary-exit (&optional temporary)
6338   "Exit reading current newsgroup, and then return to group selection mode.
6339 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6340   (interactive)
6341   (gnus-set-global-variables)
6342   (when (gnus-buffer-live-p gnus-article-buffer)
6343     (save-excursion
6344       (set-buffer gnus-article-buffer)
6345       (mm-destroy-parts gnus-article-mime-handles)
6346       ;; Set it to nil for safety reason.
6347       (setq gnus-article-mime-handle-alist nil)
6348       (setq gnus-article-mime-handles nil)))
6349   (gnus-kill-save-kill-buffer)
6350   (gnus-async-halt-prefetch)
6351   (let* ((group gnus-newsgroup-name)
6352          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6353          (gnus-group-is-exiting-p t)
6354          (mode major-mode)
6355          (group-point nil)
6356          (buf (current-buffer)))
6357     (unless quit-config
6358       ;; Do adaptive scoring, and possibly save score files.
6359       (when gnus-newsgroup-adaptive
6360         (gnus-score-adaptive))
6361       (when gnus-use-scoring
6362         (gnus-score-save)))
6363     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6364     ;; If we have several article buffers, we kill them at exit.
6365     (unless gnus-single-article-buffer
6366       (gnus-kill-buffer gnus-original-article-buffer)
6367       (setq gnus-article-current nil))
6368     (when gnus-use-cache
6369       (gnus-cache-possibly-remove-articles)
6370       (gnus-cache-save-buffers))
6371     (gnus-async-prefetch-remove-group group)
6372     (when gnus-suppress-duplicates
6373       (gnus-dup-enter-articles))
6374     (when gnus-use-trees
6375       (gnus-tree-close group))
6376     (when gnus-use-cache
6377       (gnus-cache-write-active))
6378     ;; Remove entries for this group.
6379     (nnmail-purge-split-history (gnus-group-real-name group))
6380     ;; Make all changes in this group permanent.
6381     (unless quit-config
6382       (gnus-run-hooks 'gnus-exit-group-hook)
6383       (gnus-summary-update-info))
6384     (gnus-close-group group)
6385     ;; Make sure where we were, and go to next newsgroup.
6386     (set-buffer gnus-group-buffer)
6387     (unless quit-config
6388       (gnus-group-jump-to-group group))
6389     (gnus-run-hooks 'gnus-summary-exit-hook)
6390     (unless (or quit-config
6391                 ;; If this group has disappeared from the summary
6392                 ;; buffer, don't skip forwards.
6393                 (not (string= group (gnus-group-group-name))))
6394       (gnus-group-next-unread-group 1))
6395     (setq group-point (point))
6396     (if temporary
6397         nil                             ;Nothing to do.
6398       ;; If we have several article buffers, we kill them at exit.
6399       (unless gnus-single-article-buffer
6400         (gnus-kill-buffer gnus-article-buffer)
6401         (gnus-kill-buffer gnus-original-article-buffer)
6402         (setq gnus-article-current nil))
6403       (set-buffer buf)
6404       (if (not gnus-kill-summary-on-exit)
6405           (progn
6406             (gnus-deaden-summary)
6407             (setq mode nil))
6408         ;; We set all buffer-local variables to nil.  It is unclear why
6409         ;; this is needed, but if we don't, buffer-local variables are
6410         ;; not garbage-collected, it seems.  This would the lead to en
6411         ;; ever-growing Emacs.
6412         (gnus-summary-clear-local-variables)
6413         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6414           (gnus-summary-clear-local-variables))
6415         (when (get-buffer gnus-article-buffer)
6416           (bury-buffer gnus-article-buffer))
6417         ;; We clear the global counterparts of the buffer-local
6418         ;; variables as well, just to be on the safe side.
6419         (set-buffer gnus-group-buffer)
6420         (gnus-summary-clear-local-variables)
6421         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6422           (gnus-summary-clear-local-variables))
6423         ;; Return to group mode buffer.
6424         (when (eq mode 'gnus-summary-mode)
6425           (gnus-kill-buffer buf)))
6426       (setq gnus-current-select-method gnus-select-method)
6427       (pop-to-buffer gnus-group-buffer)
6428       (if (not quit-config)
6429           (progn
6430             (goto-char group-point)
6431             (gnus-configure-windows 'group 'force))
6432         (gnus-handle-ephemeral-exit quit-config))
6433       ;; Clear the current group name.
6434       (unless quit-config
6435         (setq gnus-newsgroup-name nil)))))
6436
6437 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6438 (defun gnus-summary-exit-no-update (&optional no-questions)
6439   "Quit reading current newsgroup without updating read article info."
6440   (interactive)
6441   (let* ((group gnus-newsgroup-name)
6442          (gnus-group-is-exiting-p t)
6443          (quit-config (gnus-group-quit-config group)))
6444     (when (or no-questions
6445               gnus-expert-user
6446               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6447       (gnus-async-halt-prefetch)
6448       (mapcar 'funcall
6449               (delq 'gnus-summary-expire-articles
6450                     (copy-sequence gnus-summary-prepare-exit-hook)))
6451       (when (gnus-buffer-live-p gnus-article-buffer)
6452         (save-excursion
6453           (set-buffer gnus-article-buffer)
6454           (mm-destroy-parts gnus-article-mime-handles)
6455           ;; Set it to nil for safety reason.
6456           (setq gnus-article-mime-handle-alist nil)
6457           (setq gnus-article-mime-handles nil)))
6458       ;; If we have several article buffers, we kill them at exit.
6459       (unless gnus-single-article-buffer
6460         (gnus-kill-buffer gnus-article-buffer)
6461         (gnus-kill-buffer gnus-original-article-buffer)
6462         (setq gnus-article-current nil))
6463       (if (not gnus-kill-summary-on-exit)
6464           (gnus-deaden-summary)
6465         (gnus-close-group group)
6466         (gnus-summary-clear-local-variables)
6467         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6468           (gnus-summary-clear-local-variables))
6469         (set-buffer gnus-group-buffer)
6470         (gnus-summary-clear-local-variables)
6471         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6472           (gnus-summary-clear-local-variables))
6473         (when (get-buffer gnus-summary-buffer)
6474           (kill-buffer gnus-summary-buffer)))
6475       (unless gnus-single-article-buffer
6476         (setq gnus-article-current nil))
6477       (when gnus-use-trees
6478         (gnus-tree-close group))
6479       (gnus-async-prefetch-remove-group group)
6480       (when (get-buffer gnus-article-buffer)
6481         (bury-buffer gnus-article-buffer))
6482       ;; Return to the group buffer.
6483       (gnus-configure-windows 'group 'force)
6484       ;; Clear the current group name.
6485       (setq gnus-newsgroup-name nil)
6486       (unless (gnus-ephemeral-group-p group)
6487         (gnus-group-update-group group))
6488       (when (equal (gnus-group-group-name) group)
6489         (gnus-group-next-unread-group 1))
6490       (when quit-config
6491         (gnus-handle-ephemeral-exit quit-config)))))
6492
6493 (defun gnus-handle-ephemeral-exit (quit-config)
6494   "Handle movement when leaving an ephemeral group.
6495 The state which existed when entering the ephemeral is reset."
6496   (if (not (buffer-name (car quit-config)))
6497       (gnus-configure-windows 'group 'force)
6498     (set-buffer (car quit-config))
6499     (cond ((eq major-mode 'gnus-summary-mode)
6500            (gnus-set-global-variables))
6501           ((eq major-mode 'gnus-article-mode)
6502            (save-excursion
6503              ;; The `gnus-summary-buffer' variable may point
6504              ;; to the old summary buffer when using a single
6505              ;; article buffer.
6506              (unless (gnus-buffer-live-p gnus-summary-buffer)
6507                (set-buffer gnus-group-buffer))
6508              (set-buffer gnus-summary-buffer)
6509              (gnus-set-global-variables))))
6510     (if (or (eq (cdr quit-config) 'article)
6511             (eq (cdr quit-config) 'pick))
6512         (progn
6513           ;; The current article may be from the ephemeral group
6514           ;; thus it is best that we reload this article
6515           (gnus-summary-show-article)
6516           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6517               (gnus-configure-windows 'pick 'force)
6518             (gnus-configure-windows (cdr quit-config) 'force)))
6519       (gnus-configure-windows (cdr quit-config) 'force))
6520     (when (eq major-mode 'gnus-summary-mode)
6521       (gnus-summary-next-subject 1 nil t)
6522       (gnus-summary-recenter)
6523       (gnus-summary-position-point))))
6524
6525 ;;; Dead summaries.
6526
6527 (defvar gnus-dead-summary-mode-map nil)
6528
6529 (unless gnus-dead-summary-mode-map
6530   (setq gnus-dead-summary-mode-map (make-keymap))
6531   (suppress-keymap gnus-dead-summary-mode-map)
6532   (substitute-key-definition
6533    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6534   (dolist (key '("\C-d" "\r" "\177" [delete]))
6535     (define-key gnus-dead-summary-mode-map
6536       key 'gnus-summary-wake-up-the-dead))
6537   (dolist (key '("q" "Q"))
6538     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6539
6540 (defvar gnus-dead-summary-mode nil
6541   "Minor mode for Gnus summary buffers.")
6542
6543 (defun gnus-dead-summary-mode (&optional arg)
6544   "Minor mode for Gnus summary buffers."
6545   (interactive "P")
6546   (when (eq major-mode 'gnus-summary-mode)
6547     (make-local-variable 'gnus-dead-summary-mode)
6548     (setq gnus-dead-summary-mode
6549           (if (null arg) (not gnus-dead-summary-mode)
6550             (> (prefix-numeric-value arg) 0)))
6551     (when gnus-dead-summary-mode
6552       (gnus-add-minor-mode
6553        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6554
6555 (defun gnus-deaden-summary ()
6556   "Make the current summary buffer into a dead summary buffer."
6557   ;; Kill any previous dead summary buffer.
6558   (when (and gnus-dead-summary
6559              (buffer-name gnus-dead-summary))
6560     (save-excursion
6561       (set-buffer gnus-dead-summary)
6562       (when gnus-dead-summary-mode
6563         (kill-buffer (current-buffer)))))
6564   ;; Make this the current dead summary.
6565   (setq gnus-dead-summary (current-buffer))
6566   (gnus-dead-summary-mode 1)
6567   (let ((name (buffer-name)))
6568     (when (string-match "Summary" name)
6569       (rename-buffer
6570        (concat (substring name 0 (match-beginning 0)) "Dead "
6571                (substring name (match-beginning 0)))
6572        t)
6573       (bury-buffer))))
6574
6575 (defun gnus-kill-or-deaden-summary (buffer)
6576   "Kill or deaden the summary BUFFER."
6577   (save-excursion
6578     (when (and (buffer-name buffer)
6579                (not gnus-single-article-buffer))
6580       (save-excursion
6581         (set-buffer buffer)
6582         (gnus-kill-buffer gnus-article-buffer)
6583         (gnus-kill-buffer gnus-original-article-buffer)))
6584     (cond
6585      ;; Kill the buffer.
6586      (gnus-kill-summary-on-exit
6587       (when (and gnus-use-trees
6588                  (gnus-buffer-exists-p buffer))
6589         (save-excursion
6590           (set-buffer buffer)
6591           (gnus-tree-close gnus-newsgroup-name)))
6592       (gnus-kill-buffer buffer))
6593      ;; Deaden the buffer.
6594      ((gnus-buffer-exists-p buffer)
6595       (save-excursion
6596         (set-buffer buffer)
6597         (gnus-deaden-summary))))))
6598
6599 (defun gnus-summary-wake-up-the-dead (&rest args)
6600   "Wake up the dead summary buffer."
6601   (interactive)
6602   (gnus-dead-summary-mode -1)
6603   (let ((name (buffer-name)))
6604     (when (string-match "Dead " name)
6605       (rename-buffer
6606        (concat (substring name 0 (match-beginning 0))
6607                (substring name (match-end 0)))
6608        t)))
6609   (gnus-message 3 "This dead summary is now alive again"))
6610
6611 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6612 (defun gnus-summary-fetch-faq (&optional faq-dir)
6613   "Fetch the FAQ for the current group.
6614 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6615 in."
6616   (interactive
6617    (list
6618     (when current-prefix-arg
6619       (completing-read
6620        "FAQ dir: " (and (listp gnus-group-faq-directory)
6621                         (mapcar (lambda (file) (list file))
6622                                 gnus-group-faq-directory))))))
6623   (let (gnus-faq-buffer)
6624     (when (setq gnus-faq-buffer
6625                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6626       (gnus-configure-windows 'summary-faq))))
6627
6628 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6629 (defun gnus-summary-describe-group (&optional force)
6630   "Describe the current newsgroup."
6631   (interactive "P")
6632   (gnus-group-describe-group force gnus-newsgroup-name))
6633
6634 (defun gnus-summary-describe-briefly ()
6635   "Describe summary mode commands briefly."
6636   (interactive)
6637   (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")))
6638
6639 ;; Walking around group mode buffer from summary mode.
6640
6641 (defun gnus-summary-next-group (&optional no-article target-group backward)
6642   "Exit current newsgroup and then select next unread newsgroup.
6643 If prefix argument NO-ARTICLE is non-nil, no article is selected
6644 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6645 previous group instead."
6646   (interactive "P")
6647   ;; Stop pre-fetching.
6648   (gnus-async-halt-prefetch)
6649   (let ((current-group gnus-newsgroup-name)
6650         (current-buffer (current-buffer))
6651         entered)
6652     (while (not entered)
6653       ;; Then we find what group we are supposed to enter.
6654       (set-buffer gnus-group-buffer)
6655       (gnus-group-jump-to-group current-group)
6656       (setq target-group
6657             (or target-group
6658                 (if (eq gnus-keep-same-level 'best)
6659                     (gnus-summary-best-group gnus-newsgroup-name)
6660                   (gnus-summary-search-group backward gnus-keep-same-level))))
6661       (if (not target-group)
6662           ;; There are no further groups, so we return to the group
6663           ;; buffer.
6664           (progn
6665             (gnus-message 5 "Returning to the group buffer")
6666             (setq entered t)
6667             (when (gnus-buffer-live-p current-buffer)
6668               (set-buffer current-buffer)
6669               (gnus-summary-exit))
6670             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6671         ;; We try to enter the target group.
6672         (gnus-group-jump-to-group target-group)
6673         (let ((unreads (gnus-group-group-unread)))
6674           (if (and (or (eq t unreads)
6675                        (and unreads (not (zerop unreads))))
6676                    (progn
6677                      ;; Now we semi-exit this group to update Xrefs
6678                      ;; and all variables.  We can't do a real exit,
6679                      ;; because the window conf must remain the same
6680                      ;; in case the user is prompted for info, and we
6681                      ;; don't want the window conf to change before
6682                      ;; that...
6683                      (when (gnus-buffer-live-p current-buffer)
6684                        (set-buffer current-buffer)
6685                        (gnus-summary-exit t))
6686                      (gnus-summary-read-group
6687                       target-group nil no-article
6688                       (and (buffer-name current-buffer) current-buffer)
6689                       nil backward)))
6690               (setq entered t)
6691             (setq current-group target-group
6692                   target-group nil)))))))
6693
6694 (defun gnus-summary-prev-group (&optional no-article)
6695   "Exit current newsgroup and then select previous unread newsgroup.
6696 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6697   (interactive "P")
6698   (gnus-summary-next-group no-article nil t))
6699
6700 ;; Walking around summary lines.
6701
6702 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6703   "Go to the first subject satisfying any non-nil constraint.
6704 If UNREAD is non-nil, the article should be unread.
6705 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6706 If UNSEED is non-nil, the article should be unseen.
6707 Returns the article selected or nil if there are no matching articles."
6708   (interactive "P")
6709   (cond
6710    ;; Empty summary.
6711    ((null gnus-newsgroup-data)
6712     (gnus-message 3 "No articles in the group")
6713     nil)
6714    ;; Pick the first article.
6715    ((not (or unread undownloaded unseen))
6716     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6717     (gnus-data-number (car gnus-newsgroup-data)))
6718    ;; Find the first unread article.
6719    (t
6720     (let ((data gnus-newsgroup-data)
6721           (gnus-newsgroup-unreads gnus-newsgroup-unreads)
6722           (gnus-newsgroup-undownloaded gnus-newsgroup-undownloaded)
6723           (gnus-newsgroup-unseen gnus-newsgroup-unseen)
6724           (gnus-newsgroup-unfetched gnus-newsgroup-unfetched))
6725       (while (and data
6726                   (not (let ((num (gnus-data-number (car data)))
6727                              (matched nil))
6728                          (while (> num (or (car gnus-newsgroup-unfetched)
6729                                            (1+ num)))
6730                            (pop gnus-newsgroup-unfetched))
6731                          (unless (eq num (car gnus-newsgroup-unfetched))
6732                            (when unread
6733                              (while (> num (or (car gnus-newsgroup-unreads)
6734                                                (1+ num)))
6735                                (pop gnus-newsgroup-unreads))
6736                              (setq matched (eq num (car gnus-newsgroup-unreads))))
6737                            (unless matched
6738                              (when undownloaded
6739                                (while (> num (or (car gnus-newsgroup-undownloaded)
6740                                                  (1+ num)))
6741                                  (pop gnus-newsgroup-undownloaded))
6742                                (setq matched (eq num (car gnus-newsgroup-undownloaded))))
6743                              (unless matched
6744                                (when unseen
6745                                  (while (> num (or (car gnus-newsgroup-unseen)
6746                                                    (1+ num)))
6747                                    (pop gnus-newsgroup-unseen))
6748                                  (setq matched (eq num (car gnus-newsgroup-unseen)))))))
6749                          matched)))
6750         (setq data (cdr data)))
6751       (prog1 
6752           (if data
6753               (progn
6754                 (goto-char (gnus-data-pos (car data)))
6755                 (gnus-data-number (car data)))
6756             (gnus-message 3 "No more%s articles"
6757                           (let* ((r (when unread " unread"))
6758                                  (d (when undownloaded " undownloaded"))
6759                                  (s (when unseen " unseen"))
6760                                  (l (delq nil (list r d s))))
6761                             (cond ((= 3 (length l))
6762                                    (concat r "," d ", or" s))
6763                                   ((= 2 (length l))
6764                                    (concat (car l) ", or" (cadr l)))
6765                                   ((= 1 (length l))
6766                                    (car l))
6767                                   (t
6768                                    ""))))
6769             nil
6770             )
6771         (gnus-summary-position-point))))))
6772
6773 (defun gnus-summary-next-subject (n &optional unread dont-display)
6774   "Go to next N'th summary line.
6775 If N is negative, go to the previous N'th subject line.
6776 If UNREAD is non-nil, only unread articles are selected.
6777 The difference between N and the actual number of steps taken is
6778 returned."
6779   (interactive "p")
6780   (let ((backward (< n 0))
6781         (n (abs n)))
6782     (while (and (> n 0)
6783                 (if backward
6784                     (gnus-summary-find-prev unread)
6785                   (gnus-summary-find-next unread)))
6786       (unless (zerop (setq n (1- n)))
6787         (gnus-summary-show-thread)))
6788     (when (/= 0 n)
6789       (gnus-message 7 "No more%s articles"
6790                     (if unread " unread" "")))
6791     (unless dont-display
6792       (gnus-summary-recenter)
6793       (gnus-summary-position-point))
6794     n))
6795
6796 (defun gnus-summary-next-unread-subject (n)
6797   "Go to next N'th unread summary line."
6798   (interactive "p")
6799   (gnus-summary-next-subject n t))
6800
6801 (defun gnus-summary-prev-subject (n &optional unread)
6802   "Go to previous N'th summary line.
6803 If optional argument UNREAD is non-nil, only unread article is selected."
6804   (interactive "p")
6805   (gnus-summary-next-subject (- n) unread))
6806
6807 (defun gnus-summary-prev-unread-subject (n)
6808   "Go to previous N'th unread summary line."
6809   (interactive "p")
6810   (gnus-summary-next-subject (- n) t))
6811
6812 (defun gnus-summary-goto-subjects (articles)
6813   "Insert the subject header for ARTICLES in the current buffer."
6814   (save-excursion
6815     (dolist (article articles)
6816       (gnus-summary-goto-subject article t)))
6817   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6818   (gnus-summary-position-point))
6819  
6820 (defun gnus-summary-goto-subject (article &optional force silent)
6821   "Go the subject line of ARTICLE.
6822 If FORCE, also allow jumping to articles not currently shown."
6823   (interactive "nArticle number: ")
6824   (unless (numberp article)
6825     (error "Article %s is not a number" article))
6826   (let ((b (point))
6827         (data (gnus-data-find article)))
6828     ;; We read in the article if we have to.
6829     (and (not data)
6830          force
6831          (gnus-summary-insert-subject
6832           article
6833           (if (or (numberp force) (vectorp force)) force)
6834           t)
6835          (setq data (gnus-data-find article)))
6836     (goto-char b)
6837     (if (not data)
6838         (progn
6839           (unless silent
6840             (gnus-message 3 "Can't find article %d" article))
6841           nil)
6842       (let ((pt (gnus-data-pos data)))
6843         (goto-char pt)
6844         (gnus-summary-set-article-display-arrow pt))
6845       (gnus-summary-position-point)
6846       article)))
6847
6848 ;; Walking around summary lines with displaying articles.
6849
6850 (defun gnus-summary-expand-window (&optional arg)
6851   "Make the summary buffer take up the entire Emacs frame.
6852 Given a prefix, will force an `article' buffer configuration."
6853   (interactive "P")
6854   (if arg
6855       (gnus-configure-windows 'article 'force)
6856     (gnus-configure-windows 'summary 'force)))
6857
6858 (defun gnus-summary-display-article (article &optional all-header)
6859   "Display ARTICLE in article buffer."
6860   (when (gnus-buffer-live-p gnus-article-buffer)
6861     (with-current-buffer gnus-article-buffer
6862       (mm-enable-multibyte)))
6863   (gnus-set-global-variables)
6864   (when (gnus-buffer-live-p gnus-article-buffer)
6865     (with-current-buffer gnus-article-buffer
6866       (setq gnus-article-charset gnus-newsgroup-charset)
6867       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6868       (mm-enable-multibyte)))
6869   (if (null article)
6870       nil
6871     (prog1
6872         (if gnus-summary-display-article-function
6873             (funcall gnus-summary-display-article-function article all-header)
6874           (gnus-article-prepare article all-header))
6875       (gnus-run-hooks 'gnus-select-article-hook)
6876       (when (and gnus-current-article
6877                  (not (zerop gnus-current-article)))
6878         (gnus-summary-goto-subject gnus-current-article))
6879       (gnus-summary-recenter)
6880       (when (and gnus-use-trees gnus-show-threads)
6881         (gnus-possibly-generate-tree article)
6882         (gnus-highlight-selected-tree article))
6883       ;; Successfully display article.
6884       (gnus-article-set-window-start
6885        (cdr (assq article gnus-newsgroup-bookmarks))))))
6886
6887 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6888   "Select the current article.
6889 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6890 non-nil, the article will be re-fetched even if it already present in
6891 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6892 be displayed."
6893   ;; Make sure we are in the summary buffer to work around bbdb bug.
6894   (unless (eq major-mode 'gnus-summary-mode)
6895     (set-buffer gnus-summary-buffer))
6896   (let ((article (or article (gnus-summary-article-number)))
6897         (all-headers (not (not all-headers))) ;Must be t or nil.
6898         gnus-summary-display-article-function)
6899     (and (not pseudo)
6900          (gnus-summary-article-pseudo-p article)
6901          (error "This is a pseudo-article"))
6902     (save-excursion
6903       (set-buffer gnus-summary-buffer)
6904       (if (or (and gnus-single-article-buffer
6905                    (or (null gnus-current-article)
6906                        (null gnus-article-current)
6907                        (null (get-buffer gnus-article-buffer))
6908                        (not (eq article (cdr gnus-article-current)))
6909                        (not (equal (car gnus-article-current)
6910                                    gnus-newsgroup-name))))
6911               (and (not gnus-single-article-buffer)
6912                    (or (null gnus-current-article)
6913                        (not (eq gnus-current-article article))))
6914               force)
6915           ;; The requested article is different from the current article.
6916           (progn
6917             (gnus-summary-display-article article all-headers)
6918             (when (gnus-buffer-live-p gnus-article-buffer)
6919               (with-current-buffer gnus-article-buffer
6920                 (if (not gnus-article-decoded-p) ;; a local variable
6921                     (mm-disable-multibyte))))
6922             (gnus-article-set-window-start
6923              (cdr (assq article gnus-newsgroup-bookmarks)))
6924             article)
6925         'old))))
6926
6927 (defun gnus-summary-force-verify-and-decrypt ()
6928   (interactive)
6929   (let ((mm-verify-option 'known)
6930         (mm-decrypt-option 'known)
6931         (gnus-buttonized-mime-types (append (list "multipart/signed"
6932                                                   "multipart/encrypted")
6933                                             gnus-buttonized-mime-types)))
6934     (gnus-summary-select-article nil 'force)))
6935
6936 (defun gnus-summary-set-current-mark (&optional current-mark)
6937   "Obsolete function."
6938   nil)
6939
6940 (defun gnus-summary-next-article (&optional unread subject backward push)
6941   "Select the next article.
6942 If UNREAD, only unread articles are selected.
6943 If SUBJECT, only articles with SUBJECT are selected.
6944 If BACKWARD, the previous article is selected instead of the next."
6945   (interactive "P")
6946   (cond
6947    ;; Is there such an article?
6948    ((and (gnus-summary-search-forward unread subject backward)
6949          (or (gnus-summary-display-article (gnus-summary-article-number))
6950              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6951     (gnus-summary-position-point))
6952    ;; If not, we try the first unread, if that is wanted.
6953    ((and subject
6954          gnus-auto-select-same
6955          (gnus-summary-first-unread-article))
6956     (gnus-summary-position-point)
6957     (gnus-message 6 "Wrapped"))
6958    ;; Try to get next/previous article not displayed in this group.
6959    ((and gnus-auto-extend-newsgroup
6960          (not unread) (not subject))
6961     (gnus-summary-goto-article
6962      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6963      nil (count-lines (point-min) (point))))
6964    ;; Go to next/previous group.
6965    (t
6966     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6967       (gnus-summary-jump-to-group gnus-newsgroup-name))
6968     (let ((cmd last-command-char)
6969           (point
6970            (save-excursion
6971              (set-buffer gnus-group-buffer)
6972              (point)))
6973           (group
6974            (if (eq gnus-keep-same-level 'best)
6975                (gnus-summary-best-group gnus-newsgroup-name)
6976              (gnus-summary-search-group backward gnus-keep-same-level))))
6977       ;; For some reason, the group window gets selected.  We change
6978       ;; it back.
6979       (select-window (get-buffer-window (current-buffer)))
6980       ;; Select next unread newsgroup automagically.
6981       (cond
6982        ((or (not gnus-auto-select-next)
6983             (not cmd))
6984         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6985        ((or (eq gnus-auto-select-next 'quietly)
6986             (and (eq gnus-auto-select-next 'slightly-quietly)
6987                  push)
6988             (and (eq gnus-auto-select-next 'almost-quietly)
6989                  (gnus-summary-last-article-p)))
6990         ;; Select quietly.
6991         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6992             (gnus-summary-exit)
6993           (gnus-message 7 "No more%s articles (%s)..."
6994                         (if unread " unread" "")
6995                         (if group (concat "selecting " group)
6996                           "exiting"))
6997           (gnus-summary-next-group nil group backward)))
6998        (t
6999         (when (gnus-key-press-event-p last-input-event)
7000           (gnus-summary-walk-group-buffer
7001            gnus-newsgroup-name cmd unread backward point))))))))
7002
7003 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7004   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7005                       (?\C-p (gnus-group-prev-unread-group 1))))
7006         (cursor-in-echo-area t)
7007         keve key group ended prompt)
7008     (save-excursion
7009       (set-buffer gnus-group-buffer)
7010       (goto-char start)
7011       (setq group
7012             (if (eq gnus-keep-same-level 'best)
7013                 (gnus-summary-best-group gnus-newsgroup-name)
7014               (gnus-summary-search-group backward gnus-keep-same-level))))
7015     (while (not ended)
7016       (setq prompt
7017             (format
7018              "No more%s articles%s " (if unread " unread" "")
7019              (if (and group
7020                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7021                  (format " (Type %s for %s [%s])"
7022                          (single-key-description cmd) group
7023                          (car (gnus-gethash group gnus-newsrc-hashtb)))
7024                (format " (Type %s to exit %s)"
7025                        (single-key-description cmd)
7026                        gnus-newsgroup-name))))
7027       ;; Confirm auto selection.
7028       (setq key (car (setq keve (gnus-read-event-char prompt)))
7029             ended t)
7030       (cond
7031        ((assq key keystrokes)
7032         (let ((obuf (current-buffer)))
7033           (switch-to-buffer gnus-group-buffer)
7034           (when group
7035             (gnus-group-jump-to-group group))
7036           (eval (cadr (assq key keystrokes)))
7037           (setq group (gnus-group-group-name))
7038           (switch-to-buffer obuf))
7039         (setq ended nil))
7040        ((equal key cmd)
7041         (if (or (not group)
7042                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7043             (gnus-summary-exit)
7044           (gnus-summary-next-group nil group backward)))
7045        (t
7046         (push (cdr keve) unread-command-events))))))
7047
7048 (defun gnus-summary-next-unread-article ()
7049   "Select unread article after current one."
7050   (interactive)
7051   (gnus-summary-next-article
7052    (or (not (eq gnus-summary-goto-unread 'never))
7053        (gnus-summary-last-article-p (gnus-summary-article-number)))
7054    (and gnus-auto-select-same
7055         (gnus-summary-article-subject))))
7056
7057 (defun gnus-summary-prev-article (&optional unread subject)
7058   "Select the article after the current one.
7059 If UNREAD is non-nil, only unread articles are selected."
7060   (interactive "P")
7061   (gnus-summary-next-article unread subject t))
7062
7063 (defun gnus-summary-prev-unread-article ()
7064   "Select unread article before current one."
7065   (interactive)
7066   (gnus-summary-prev-article
7067    (or (not (eq gnus-summary-goto-unread 'never))
7068        (gnus-summary-first-article-p (gnus-summary-article-number)))
7069    (and gnus-auto-select-same
7070         (gnus-summary-article-subject))))
7071
7072 (defun gnus-summary-next-page (&optional lines circular stop)
7073   "Show next page of the selected article.
7074 If at the end of the current article, select the next article.
7075 LINES says how many lines should be scrolled up.
7076
7077 If CIRCULAR is non-nil, go to the start of the article instead of
7078 selecting the next article when reaching the end of the current
7079 article.
7080
7081 If STOP is non-nil, just stop when reaching the end of the message."
7082   (interactive "P")
7083   (setq gnus-summary-buffer (current-buffer))
7084   (gnus-set-global-variables)
7085   (let ((article (gnus-summary-article-number))
7086         (article-window (get-buffer-window gnus-article-buffer t))
7087         endp)
7088     ;; If the buffer is empty, we have no article.
7089     (unless article
7090       (error "No article to select"))
7091     (gnus-configure-windows 'article)
7092     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7093         (if (and (eq gnus-summary-goto-unread 'never)
7094                  (not (gnus-summary-last-article-p article)))
7095             (gnus-summary-next-article)
7096           (gnus-summary-next-unread-article))
7097       (if (or (null gnus-current-article)
7098               (null gnus-article-current)
7099               (/= article (cdr gnus-article-current))
7100               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7101           ;; Selected subject is different from current article's.
7102           (gnus-summary-display-article article)
7103         (when article-window
7104           (gnus-eval-in-buffer-window gnus-article-buffer
7105             (setq endp (gnus-article-next-page lines)))
7106           (when endp
7107             (cond (stop
7108                    (gnus-message 3 "End of message"))
7109                   (circular
7110                    (gnus-summary-beginning-of-article))
7111                   (lines
7112                    (gnus-message 3 "End of message"))
7113                   ((null lines)
7114                    (if (and (eq gnus-summary-goto-unread 'never)
7115                             (not (gnus-summary-last-article-p article)))
7116                        (gnus-summary-next-article)
7117                      (gnus-summary-next-unread-article))))))))
7118     (gnus-summary-recenter)
7119     (gnus-summary-position-point)))
7120
7121 (defun gnus-summary-prev-page (&optional lines move)
7122   "Show previous page of selected article.
7123 Argument LINES specifies lines to be scrolled down.
7124 If MOVE, move to the previous unread article if point is at
7125 the beginning of the buffer."
7126   (interactive "P")
7127   (let ((article (gnus-summary-article-number))
7128         (article-window (get-buffer-window gnus-article-buffer t))
7129         endp)
7130     (gnus-configure-windows 'article)
7131     (if (or (null gnus-current-article)
7132             (null gnus-article-current)
7133             (/= article (cdr gnus-article-current))
7134             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7135         ;; Selected subject is different from current article's.
7136         (gnus-summary-display-article article)
7137       (gnus-summary-recenter)
7138       (when article-window
7139         (gnus-eval-in-buffer-window gnus-article-buffer
7140           (setq endp (gnus-article-prev-page lines)))
7141         (when (and move endp)
7142           (cond (lines
7143                  (gnus-message 3 "Beginning of message"))
7144                 ((null lines)
7145                  (if (and (eq gnus-summary-goto-unread 'never)
7146                           (not (gnus-summary-first-article-p article)))
7147                      (gnus-summary-prev-article)
7148                    (gnus-summary-prev-unread-article))))))))
7149   (gnus-summary-position-point))
7150
7151 (defun gnus-summary-prev-page-or-article (&optional lines)
7152   "Show previous page of selected article.
7153 Argument LINES specifies lines to be scrolled down.
7154 If at the beginning of the article, go to the next article."
7155   (interactive "P")
7156   (gnus-summary-prev-page lines t))
7157
7158 (defun gnus-summary-scroll-up (lines)
7159   "Scroll up (or down) one line current article.
7160 Argument LINES specifies lines to be scrolled up (or down if negative)."
7161   (interactive "p")
7162   (gnus-configure-windows 'article)
7163   (gnus-summary-show-thread)
7164   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7165     (gnus-eval-in-buffer-window gnus-article-buffer
7166       (cond ((> lines 0)
7167              (when (gnus-article-next-page lines)
7168                (gnus-message 3 "End of message")))
7169             ((< lines 0)
7170              (gnus-article-prev-page (- lines))))))
7171   (gnus-summary-recenter)
7172   (gnus-summary-position-point))
7173
7174 (defun gnus-summary-scroll-down (lines)
7175   "Scroll down (or up) one line current article.
7176 Argument LINES specifies lines to be scrolled down (or up if negative)."
7177   (interactive "p")
7178   (gnus-summary-scroll-up (- lines)))
7179
7180 (defun gnus-summary-next-same-subject ()
7181   "Select next article which has the same subject as current one."
7182   (interactive)
7183   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7184
7185 (defun gnus-summary-prev-same-subject ()
7186   "Select previous article which has the same subject as current one."
7187   (interactive)
7188   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7189
7190 (defun gnus-summary-next-unread-same-subject ()
7191   "Select next unread article which has the same subject as current one."
7192   (interactive)
7193   (gnus-summary-next-article t (gnus-summary-article-subject)))
7194
7195 (defun gnus-summary-prev-unread-same-subject ()
7196   "Select previous unread article which has the same subject as current one."
7197   (interactive)
7198   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7199
7200 (defun gnus-summary-first-unread-article ()
7201   "Select the first unread article.
7202 Return nil if there are no unread articles."
7203   (interactive)
7204   (prog1
7205       (when (gnus-summary-first-subject t)
7206         (gnus-summary-show-thread)
7207         (gnus-summary-first-subject t)
7208         (gnus-summary-display-article (gnus-summary-article-number)))
7209     (gnus-summary-position-point)))
7210
7211 (defun gnus-summary-first-unread-subject ()
7212   "Place the point on the subject line of the first unread article.
7213 Return nil if there are no unread articles."
7214   (interactive)
7215   (prog1
7216       (when (gnus-summary-first-subject t)
7217         (gnus-summary-show-thread)
7218         (gnus-summary-first-subject t))
7219     (gnus-summary-position-point)))
7220
7221 (defun gnus-summary-first-unseen-subject ()
7222   "Place the point on the subject line of the first unseen article.
7223 Return nil if there are no unseen articles."
7224   (interactive)
7225   (prog1
7226       (when (gnus-summary-first-subject nil nil t)
7227         (gnus-summary-show-thread)
7228         (gnus-summary-first-subject nil nil t))
7229     (gnus-summary-position-point)))
7230
7231 (defun gnus-summary-first-unseen-or-unread-subject ()
7232   "Place the point on the subject line of the first unseen article.
7233 Return nil if there are no unseen articles."
7234   (interactive)
7235   (prog1
7236       (unless (when (gnus-summary-first-subject t nil t)
7237                 (gnus-summary-show-thread)
7238                 (gnus-summary-first-subject t nil t))
7239         (when (gnus-summary-first-subject t)
7240           (gnus-summary-show-thread)
7241           (gnus-summary-first-subject t)))
7242     (gnus-summary-position-point)))
7243
7244 (defun gnus-summary-first-article ()
7245   "Select the first article.
7246 Return nil if there are no articles."
7247   (interactive)
7248   (prog1
7249       (when (gnus-summary-first-subject)
7250         (gnus-summary-show-thread)
7251         (gnus-summary-first-subject)
7252         (gnus-summary-display-article (gnus-summary-article-number)))
7253     (gnus-summary-position-point)))
7254
7255 (defun gnus-summary-best-unread-article (&optional arg)
7256   "Select the unread article with the highest score.
7257 If given a prefix argument, select the next unread article that has a
7258 score higher than the default score."
7259   (interactive "P")
7260   (let ((article (if arg
7261                      (gnus-summary-better-unread-subject)
7262                    (gnus-summary-best-unread-subject))))
7263     (if article
7264         (gnus-summary-goto-article article)
7265       (error "No unread articles"))))
7266
7267 (defun gnus-summary-best-unread-subject ()
7268   "Select the unread subject with the highest score."
7269   (interactive)
7270   (let ((best -1000000)
7271         (data gnus-newsgroup-data)
7272         article score)
7273     (while data
7274       (and (gnus-data-unread-p (car data))
7275            (> (setq score
7276                     (gnus-summary-article-score (gnus-data-number (car data))))
7277               best)
7278            (setq best score
7279                  article (gnus-data-number (car data))))
7280       (setq data (cdr data)))
7281     (when article
7282       (gnus-summary-goto-subject article))
7283     (gnus-summary-position-point)
7284     article))
7285
7286 (defun gnus-summary-better-unread-subject ()
7287   "Select the first unread subject that has a score over the default score."
7288   (interactive)
7289   (let ((data gnus-newsgroup-data)
7290         article score)
7291     (while (and (setq article (gnus-data-number (car data)))
7292                 (or (gnus-data-read-p (car data))
7293                     (not (> (gnus-summary-article-score article)
7294                             gnus-summary-default-score))))
7295       (setq data (cdr data)))
7296     (when article
7297       (gnus-summary-goto-subject article))
7298     (gnus-summary-position-point)
7299     article))
7300
7301 (defun gnus-summary-last-subject ()
7302   "Go to the last displayed subject line in the group."
7303   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7304     (when article
7305       (gnus-summary-goto-subject article))))
7306
7307 (defun gnus-summary-goto-article (article &optional all-headers force)
7308   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7309 If ALL-HEADERS is non-nil, no header lines are hidden.
7310 If FORCE, go to the article even if it isn't displayed.  If FORCE
7311 is a number, it is the line the article is to be displayed on."
7312   (interactive
7313    (list
7314     (completing-read
7315      "Article number or Message-ID: "
7316      (mapcar (lambda (number) (list (int-to-string number)))
7317              gnus-newsgroup-limit))
7318     current-prefix-arg
7319     t))
7320   (prog1
7321       (if (and (stringp article)
7322                (string-match "@" article))
7323           (gnus-summary-refer-article article)
7324         (when (stringp article)
7325           (setq article (string-to-number article)))
7326         (if (gnus-summary-goto-subject article force)
7327             (gnus-summary-display-article article all-headers)
7328           (gnus-message 4 "Couldn't go to article %s" article) nil))
7329     (gnus-summary-position-point)))
7330
7331 (defun gnus-summary-goto-last-article ()
7332   "Go to the previously read article."
7333   (interactive)
7334   (prog1
7335       (when gnus-last-article
7336         (gnus-summary-goto-article gnus-last-article nil t))
7337     (gnus-summary-position-point)))
7338
7339 (defun gnus-summary-pop-article (number)
7340   "Pop one article off the history and go to the previous.
7341 NUMBER articles will be popped off."
7342   (interactive "p")
7343   (let (to)
7344     (setq gnus-newsgroup-history
7345           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7346     (if to
7347         (gnus-summary-goto-article (car to) nil t)
7348       (error "Article history empty")))
7349   (gnus-summary-position-point))
7350
7351 ;; Summary commands and functions for limiting the summary buffer.
7352
7353 (defun gnus-summary-limit-to-articles (n)
7354   "Limit the summary buffer to the next N articles.
7355 If not given a prefix, use the process marked articles instead."
7356   (interactive "P")
7357   (prog1
7358       (let ((articles (gnus-summary-work-articles n)))
7359         (setq gnus-newsgroup-processable nil)
7360         (gnus-summary-limit articles))
7361     (gnus-summary-position-point)))
7362
7363 (defun gnus-summary-pop-limit (&optional total)
7364   "Restore the previous limit.
7365 If given a prefix, remove all limits."
7366   (interactive "P")
7367   (when total
7368     (setq gnus-newsgroup-limits
7369           (list (mapcar (lambda (h) (mail-header-number h))
7370                         gnus-newsgroup-headers))))
7371   (unless gnus-newsgroup-limits
7372     (error "No limit to pop"))
7373   (prog1
7374       (gnus-summary-limit nil 'pop)
7375     (gnus-summary-position-point)))
7376
7377 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7378   "Limit the summary buffer to articles that have subjects that match a regexp.
7379 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7380   (interactive
7381    (list (read-string (if current-prefix-arg
7382                           "Exclude subject (regexp): "
7383                         "Limit to subject (regexp): "))
7384          nil current-prefix-arg))
7385   (unless header
7386     (setq header "subject"))
7387   (when (not (equal "" subject))
7388     (prog1
7389         (let ((articles (gnus-summary-find-matching
7390                          (or header "subject") subject 'all nil nil
7391                          not-matching)))
7392           (unless articles
7393             (error "Found no matches for \"%s\"" subject))
7394           (gnus-summary-limit articles))
7395       (gnus-summary-position-point))))
7396
7397 (defun gnus-summary-limit-to-author (from &optional not-matching)
7398   "Limit the summary buffer to articles that have authors that match a regexp.
7399 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7400   (interactive
7401    (list (read-string (if current-prefix-arg
7402                           "Exclude author (regexp): "
7403                         "Limit to author (regexp): "))
7404          current-prefix-arg))
7405   (gnus-summary-limit-to-subject from "from" not-matching))
7406
7407 (defun gnus-summary-limit-to-age (age &optional younger-p)
7408   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7409 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7410 articles that are younger than AGE days."
7411   (interactive
7412    (let ((younger current-prefix-arg)
7413          (days-got nil)
7414          days)
7415      (while (not days-got)
7416        (setq days (if younger
7417                       (read-string "Limit to articles younger than (in days, older when negative): ")
7418                     (read-string
7419                      "Limit to articles older than (in days, younger when negative): ")))
7420        (when (> (length days) 0)
7421          (setq days (read days)))
7422        (if (numberp days)
7423            (progn
7424              (setq days-got t)
7425              (if (< days 0)
7426                  (progn
7427                    (setq younger (not younger))
7428                    (setq days (* days -1)))))
7429          (message "Please enter a number.")
7430          (sleep-for 1)))
7431      (list days younger)))
7432   (prog1
7433       (let ((data gnus-newsgroup-data)
7434             (cutoff (days-to-time age))
7435             articles d date is-younger)
7436         (while (setq d (pop data))
7437           (when (and (vectorp (gnus-data-header d))
7438                      (setq date (mail-header-date (gnus-data-header d))))
7439             (setq is-younger (time-less-p
7440                               (time-since (condition-case ()
7441                                               (date-to-time date)
7442                                             (error '(0 0))))
7443                               cutoff))
7444             (when (if younger-p
7445                       is-younger
7446                     (not is-younger))
7447               (push (gnus-data-number d) articles))))
7448         (gnus-summary-limit (nreverse articles)))
7449     (gnus-summary-position-point)))
7450
7451 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7452   "Limit the summary buffer to articles that match an 'extra' header."
7453   (interactive
7454    (let ((header
7455           (intern
7456            (gnus-completing-read-with-default
7457             (symbol-name (car gnus-extra-headers))
7458             (if current-prefix-arg
7459                 "Exclude extra header:"
7460               "Limit extra header:")
7461             (mapcar (lambda (x)
7462                       (cons (symbol-name x) x))
7463                     gnus-extra-headers)
7464             nil
7465             t))))
7466      (list header
7467            (read-string (format "%s header %s (regexp): "
7468                                 (if current-prefix-arg "Exclude" "Limit to")
7469                                 header))
7470            current-prefix-arg)))
7471   (when (not (equal "" regexp))
7472     (prog1
7473         (let ((articles (gnus-summary-find-matching
7474                          (cons 'extra header) regexp 'all nil nil
7475                          not-matching)))
7476           (unless articles
7477             (error "Found no matches for \"%s\"" regexp))
7478           (gnus-summary-limit articles))
7479       (gnus-summary-position-point))))
7480
7481 (defun gnus-summary-limit-to-display-predicate ()
7482   "Limit the summary buffer to the predicated in the `display' group parameter."
7483   (interactive)
7484   (unless gnus-newsgroup-display
7485     (error "There is no `display' group parameter"))
7486   (let (articles)
7487     (dolist (number gnus-newsgroup-articles)
7488       (when (funcall gnus-newsgroup-display)
7489         (push number articles)))
7490     (gnus-summary-limit articles))
7491   (gnus-summary-position-point))
7492
7493 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7494 (make-obsolete
7495  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7496
7497 (defun gnus-summary-limit-to-unread (&optional all)
7498   "Limit the summary buffer to articles that are not marked as read.
7499 If ALL is non-nil, limit strictly to unread articles."
7500   (interactive "P")
7501   (if all
7502       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7503     (gnus-summary-limit-to-marks
7504      ;; Concat all the marks that say that an article is read and have
7505      ;; those removed.
7506      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7507            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7508            gnus-low-score-mark gnus-expirable-mark
7509            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7510            gnus-duplicate-mark gnus-souped-mark)
7511      'reverse)))
7512
7513 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7514 (make-obsolete 'gnus-summary-delete-marked-with
7515                'gnus-summary-limit-exclude-marks)
7516
7517 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7518   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7519 If REVERSE, limit the summary buffer to articles that are marked
7520 with MARKS.  MARKS can either be a string of marks or a list of marks.
7521 Returns how many articles were removed."
7522   (interactive "sMarks: ")
7523   (gnus-summary-limit-to-marks marks t))
7524
7525 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7526   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7527 If REVERSE (the prefix), limit the summary buffer to articles that are
7528 not marked with MARKS.  MARKS can either be a string of marks or a
7529 list of marks.
7530 Returns how many articles were removed."
7531   (interactive "sMarks: \nP")
7532   (prog1
7533       (let ((data gnus-newsgroup-data)
7534             (marks (if (listp marks) marks
7535                      (append marks nil))) ; Transform to list.
7536             articles)
7537         (while data
7538           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7539                   (memq (gnus-data-mark (car data)) marks))
7540             (push (gnus-data-number (car data)) articles))
7541           (setq data (cdr data)))
7542         (gnus-summary-limit articles))
7543     (gnus-summary-position-point)))
7544
7545 (defun gnus-summary-limit-to-score (score)
7546   "Limit to articles with score at or above SCORE."
7547   (interactive "NLimit to articles with score of at least: ")
7548   (let ((data gnus-newsgroup-data)
7549         articles)
7550     (while data
7551       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7552                 score)
7553         (push (gnus-data-number (car data)) articles))
7554       (setq data (cdr data)))
7555     (prog1
7556         (gnus-summary-limit articles)
7557       (gnus-summary-position-point))))
7558
7559 (defun gnus-summary-limit-to-unseen ()
7560   "Limit to unseen articles."
7561   (interactive)
7562   (prog1
7563       (gnus-summary-limit gnus-newsgroup-unseen)
7564     (gnus-summary-position-point)))
7565
7566 (defun gnus-summary-limit-include-thread (id)
7567   "Display all the hidden articles that is in the thread with ID in it.
7568 When called interactively, ID is the Message-ID of the current
7569 article."
7570   (interactive (list (mail-header-id (gnus-summary-article-header))))
7571   (let ((articles (gnus-articles-in-thread
7572                    (gnus-id-to-thread (gnus-root-id id)))))
7573     (prog1
7574         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7575       (gnus-summary-limit-include-matching-articles
7576        "subject"
7577        (regexp-quote (gnus-simplify-subject-re
7578                       (mail-header-subject (gnus-id-to-header id)))))
7579       (gnus-summary-position-point))))
7580
7581 (defun gnus-summary-limit-include-matching-articles (header regexp)
7582   "Display all the hidden articles that have HEADERs that match REGEXP."
7583   (interactive (list (read-string "Match on header: ")
7584                      (read-string "Regexp: ")))
7585   (let ((articles (gnus-find-matching-articles header regexp)))
7586     (prog1
7587         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7588       (gnus-summary-position-point))))
7589
7590 (defun gnus-summary-insert-dormant-articles ()
7591   "Insert all the dormat articles for this group into the current buffer."
7592   (interactive)
7593   (let ((gnus-verbose (max 6 gnus-verbose)))
7594     (if (not gnus-newsgroup-dormant)
7595         (gnus-message 3 "No cached articles for this group")
7596       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7597
7598 (defun gnus-summary-limit-include-dormant ()
7599   "Display all the hidden articles that are marked as dormant.
7600 Note that this command only works on a subset of the articles currently
7601 fetched for this group."
7602   (interactive)
7603   (unless gnus-newsgroup-dormant
7604     (error "There are no dormant articles in this group"))
7605   (prog1
7606       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7607     (gnus-summary-position-point)))
7608
7609 (defun gnus-summary-limit-exclude-dormant ()
7610   "Hide all dormant articles."
7611   (interactive)
7612   (prog1
7613       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7614     (gnus-summary-position-point)))
7615
7616 (defun gnus-summary-limit-exclude-childless-dormant ()
7617   "Hide all dormant articles that have no children."
7618   (interactive)
7619   (let ((data (gnus-data-list t))
7620         articles d children)
7621     ;; Find all articles that are either not dormant or have
7622     ;; children.
7623     (while (setq d (pop data))
7624       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7625                 (and (setq children
7626                            (gnus-article-children (gnus-data-number d)))
7627                      (let (found)
7628                        (while children
7629                          (when (memq (car children) articles)
7630                            (setq children nil
7631                                  found t))
7632                          (pop children))
7633                        found)))
7634         (push (gnus-data-number d) articles)))
7635     ;; Do the limiting.
7636     (prog1
7637         (gnus-summary-limit articles)
7638       (gnus-summary-position-point))))
7639
7640 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7641   "Mark all unread excluded articles as read.
7642 If ALL, mark even excluded ticked and dormants as read."
7643   (interactive "P")
7644   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7645   (let ((articles (gnus-sorted-ndifference
7646                    (sort
7647                     (mapcar (lambda (h) (mail-header-number h))
7648                             gnus-newsgroup-headers)
7649                     '<)
7650                    gnus-newsgroup-limit))
7651         article)
7652     (setq gnus-newsgroup-unreads
7653           (gnus-sorted-intersection gnus-newsgroup-unreads
7654                                     gnus-newsgroup-limit))
7655     (if all
7656         (setq gnus-newsgroup-dormant nil
7657               gnus-newsgroup-marked nil
7658               gnus-newsgroup-reads
7659               (nconc
7660                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7661                gnus-newsgroup-reads))
7662       (while (setq article (pop articles))
7663         (unless (or (memq article gnus-newsgroup-dormant)
7664                     (memq article gnus-newsgroup-marked))
7665           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7666
7667 (defun gnus-summary-limit (articles &optional pop)
7668   (if pop
7669       ;; We pop the previous limit off the stack and use that.
7670       (setq articles (car gnus-newsgroup-limits)
7671             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7672     ;; We use the new limit, so we push the old limit on the stack.
7673     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7674   ;; Set the limit.
7675   (setq gnus-newsgroup-limit articles)
7676   (let ((total (length gnus-newsgroup-data))
7677         (data (gnus-data-find-list (gnus-summary-article-number)))
7678         (gnus-summary-mark-below nil)   ; Inhibit this.
7679         found)
7680     ;; This will do all the work of generating the new summary buffer
7681     ;; according to the new limit.
7682     (gnus-summary-prepare)
7683     ;; Hide any threads, possibly.
7684     (gnus-summary-maybe-hide-threads)
7685     ;; Try to return to the article you were at, or one in the
7686     ;; neighborhood.
7687     (when data
7688       ;; We try to find some article after the current one.
7689       (while data
7690         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7691           (setq data nil
7692                 found t))
7693         (setq data (cdr data))))
7694     (unless found
7695       ;; If there is no data, that means that we were after the last
7696       ;; article.  The same goes when we can't find any articles
7697       ;; after the current one.
7698       (goto-char (point-max))
7699       (gnus-summary-find-prev))
7700     (gnus-set-mode-line 'summary)
7701     ;; We return how many articles were removed from the summary
7702     ;; buffer as a result of the new limit.
7703     (- total (length gnus-newsgroup-data))))
7704
7705 (defsubst gnus-invisible-cut-children (threads)
7706   (let ((num 0))
7707     (while threads
7708       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7709         (incf num))
7710       (pop threads))
7711     (< num 2)))
7712
7713 (defsubst gnus-cut-thread (thread)
7714   "Go forwards in the thread until we find an article that we want to display."
7715   (when (or (eq gnus-fetch-old-headers 'some)
7716             (eq gnus-fetch-old-headers 'invisible)
7717             (numberp gnus-fetch-old-headers)
7718             (eq gnus-build-sparse-threads 'some)
7719             (eq gnus-build-sparse-threads 'more))
7720     ;; Deal with old-fetched headers and sparse threads.
7721     (while (and
7722             thread
7723             (or
7724              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7725              (gnus-summary-article-ancient-p
7726               (mail-header-number (car thread))))
7727             (if (or (<= (length (cdr thread)) 1)
7728                     (eq gnus-fetch-old-headers 'invisible))
7729                 (setq gnus-newsgroup-limit
7730                       (delq (mail-header-number (car thread))
7731                             gnus-newsgroup-limit)
7732                       thread (cadr thread))
7733               (when (gnus-invisible-cut-children (cdr thread))
7734                 (let ((th (cdr thread)))
7735                   (while th
7736                     (if (memq (mail-header-number (caar th))
7737                               gnus-newsgroup-limit)
7738                         (setq thread (car th)
7739                               th nil)
7740                       (setq th (cdr th))))))))))
7741   thread)
7742
7743 (defun gnus-cut-threads (threads)
7744   "Cut off all uninteresting articles from the beginning of threads."
7745   (when (or (eq gnus-fetch-old-headers 'some)
7746             (eq gnus-fetch-old-headers 'invisible)
7747             (numberp gnus-fetch-old-headers)
7748             (eq gnus-build-sparse-threads 'some)
7749             (eq gnus-build-sparse-threads 'more))
7750     (let ((th threads))
7751       (while th
7752         (setcar th (gnus-cut-thread (car th)))
7753         (setq th (cdr th)))))
7754   ;; Remove nixed out threads.
7755   (delq nil threads))
7756
7757 (defun gnus-summary-initial-limit (&optional show-if-empty)
7758   "Figure out what the initial limit is supposed to be on group entry.
7759 This entails weeding out unwanted dormants, low-scored articles,
7760 fetch-old-headers verbiage, and so on."
7761   ;; Most groups have nothing to remove.
7762   (if (or gnus-inhibit-limiting
7763           (and (null gnus-newsgroup-dormant)
7764                (eq gnus-newsgroup-display 'gnus-not-ignore)
7765                (not (eq gnus-fetch-old-headers 'some))
7766                (not (numberp gnus-fetch-old-headers))
7767                (not (eq gnus-fetch-old-headers 'invisible))
7768                (null gnus-summary-expunge-below)
7769                (not (eq gnus-build-sparse-threads 'some))
7770                (not (eq gnus-build-sparse-threads 'more))
7771                (null gnus-thread-expunge-below)
7772                (not gnus-use-nocem)))
7773       ()                                ; Do nothing.
7774     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7775     (setq gnus-newsgroup-limit nil)
7776     (mapatoms
7777      (lambda (node)
7778        (unless (car (symbol-value node))
7779          ;; These threads have no parents -- they are roots.
7780          (let ((nodes (cdr (symbol-value node)))
7781                thread)
7782            (while nodes
7783              (if (and gnus-thread-expunge-below
7784                       (< (gnus-thread-total-score (car nodes))
7785                          gnus-thread-expunge-below))
7786                  (gnus-expunge-thread (pop nodes))
7787                (setq thread (pop nodes))
7788                (gnus-summary-limit-children thread))))))
7789      gnus-newsgroup-dependencies)
7790     ;; If this limitation resulted in an empty group, we might
7791     ;; pop the previous limit and use it instead.
7792     (when (and (not gnus-newsgroup-limit)
7793                show-if-empty)
7794       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7795     gnus-newsgroup-limit))
7796
7797 (defun gnus-summary-limit-children (thread)
7798   "Return 1 if this subthread is visible and 0 if it is not."
7799   ;; First we get the number of visible children to this thread.  This
7800   ;; is done by recursing down the thread using this function, so this
7801   ;; will really go down to a leaf article first, before slowly
7802   ;; working its way up towards the root.
7803   (when thread
7804     (let* ((max-lisp-eval-depth 5000)
7805            (children
7806            (if (cdr thread)
7807                (apply '+ (mapcar 'gnus-summary-limit-children
7808                                  (cdr thread)))
7809              0))
7810           (number (mail-header-number (car thread)))
7811           score)
7812       (if (and
7813            (not (memq number gnus-newsgroup-marked))
7814            (or
7815             ;; If this article is dormant and has absolutely no visible
7816             ;; children, then this article isn't visible.
7817             (and (memq number gnus-newsgroup-dormant)
7818                  (zerop children))
7819             ;; If this is "fetch-old-headered" and there is no
7820             ;; visible children, then we don't want this article.
7821             (and (or (eq gnus-fetch-old-headers 'some)
7822                      (numberp gnus-fetch-old-headers))
7823                  (gnus-summary-article-ancient-p number)
7824                  (zerop children))
7825             ;; If this is "fetch-old-headered" and `invisible', then
7826             ;; we don't want this article.
7827             (and (eq gnus-fetch-old-headers 'invisible)
7828                  (gnus-summary-article-ancient-p number))
7829             ;; If this is a sparsely inserted article with no children,
7830             ;; we don't want it.
7831             (and (eq gnus-build-sparse-threads 'some)
7832                  (gnus-summary-article-sparse-p number)
7833                  (zerop children))
7834             ;; If we use expunging, and this article is really
7835             ;; low-scored, then we don't want this article.
7836             (when (and gnus-summary-expunge-below
7837                        (< (setq score
7838                                 (or (cdr (assq number gnus-newsgroup-scored))
7839                                     gnus-summary-default-score))
7840                           gnus-summary-expunge-below))
7841               ;; We increase the expunge-tally here, but that has
7842               ;; nothing to do with the limits, really.
7843               (incf gnus-newsgroup-expunged-tally)
7844               ;; We also mark as read here, if that's wanted.
7845               (when (and gnus-summary-mark-below
7846                          (< score gnus-summary-mark-below))
7847                 (setq gnus-newsgroup-unreads
7848                       (delq number gnus-newsgroup-unreads))
7849                 (if gnus-newsgroup-auto-expire
7850                     (push number gnus-newsgroup-expirable)
7851                   (push (cons number gnus-low-score-mark)
7852                         gnus-newsgroup-reads)))
7853               t)
7854             ;; Do the `display' group parameter.
7855             (and gnus-newsgroup-display
7856                  (not (funcall gnus-newsgroup-display)))
7857             ;; Check NoCeM things.
7858             (if (and gnus-use-nocem
7859                      (gnus-nocem-unwanted-article-p
7860                       (mail-header-id (car thread))))
7861                 (progn
7862                   (setq gnus-newsgroup-unreads
7863                         (delq number gnus-newsgroup-unreads))
7864                   t))))
7865           ;; Nope, invisible article.
7866           0
7867         ;; Ok, this article is to be visible, so we add it to the limit
7868         ;; and return 1.
7869         (push number gnus-newsgroup-limit)
7870         1))))
7871
7872 (defun gnus-expunge-thread (thread)
7873   "Mark all articles in THREAD as read."
7874   (let* ((number (mail-header-number (car thread))))
7875     (incf gnus-newsgroup-expunged-tally)
7876     ;; We also mark as read here, if that's wanted.
7877     (setq gnus-newsgroup-unreads
7878           (delq number gnus-newsgroup-unreads))
7879     (if gnus-newsgroup-auto-expire
7880         (push number gnus-newsgroup-expirable)
7881       (push (cons number gnus-low-score-mark)
7882             gnus-newsgroup-reads)))
7883   ;; Go recursively through all subthreads.
7884   (mapcar 'gnus-expunge-thread (cdr thread)))
7885
7886 ;; Summary article oriented commands
7887
7888 (defun gnus-summary-refer-parent-article (n)
7889   "Refer parent article N times.
7890 If N is negative, go to ancestor -N instead.
7891 The difference between N and the number of articles fetched is returned."
7892   (interactive "p")
7893   (let ((skip 1)
7894         error header ref)
7895     (when (not (natnump n))
7896       (setq skip (abs n)
7897             n 1))
7898     (while (and (> n 0)
7899                 (not error))
7900       (setq header (gnus-summary-article-header))
7901       (if (and (eq (mail-header-number header)
7902                    (cdr gnus-article-current))
7903                (equal gnus-newsgroup-name
7904                       (car gnus-article-current)))
7905           ;; If we try to find the parent of the currently
7906           ;; displayed article, then we take a look at the actual
7907           ;; References header, since this is slightly more
7908           ;; reliable than the References field we got from the
7909           ;; server.
7910           (save-excursion
7911             (set-buffer gnus-original-article-buffer)
7912             (nnheader-narrow-to-headers)
7913             (unless (setq ref (message-fetch-field "references"))
7914               (setq ref (message-fetch-field "in-reply-to")))
7915             (widen))
7916         (setq ref
7917               ;; It's not the current article, so we take a bet on
7918               ;; the value we got from the server.
7919               (mail-header-references header)))
7920       (if (and ref
7921                (not (equal ref "")))
7922           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7923             (gnus-message 1 "Couldn't find parent"))
7924         (gnus-message 1 "No references in article %d"
7925                       (gnus-summary-article-number))
7926         (setq error t))
7927       (decf n))
7928     (gnus-summary-position-point)
7929     n))
7930
7931 (defun gnus-summary-refer-references ()
7932   "Fetch all articles mentioned in the References header.
7933 Return the number of articles fetched."
7934   (interactive)
7935   (let ((ref (mail-header-references (gnus-summary-article-header)))
7936         (current (gnus-summary-article-number))
7937         (n 0))
7938     (if (or (not ref)
7939             (equal ref ""))
7940         (error "No References in the current article")
7941       ;; For each Message-ID in the References header...
7942       (while (string-match "<[^>]*>" ref)
7943         (incf n)
7944         ;; ... fetch that article.
7945         (gnus-summary-refer-article
7946          (prog1 (match-string 0 ref)
7947            (setq ref (substring ref (match-end 0))))))
7948       (gnus-summary-goto-subject current)
7949       (gnus-summary-position-point)
7950       n)))
7951
7952 (defun gnus-summary-refer-thread (&optional limit)
7953   "Fetch all articles in the current thread.
7954 If LIMIT (the numerical prefix), fetch that many old headers instead
7955 of what's specified by the `gnus-refer-thread-limit' variable."
7956   (interactive "P")
7957   (let ((id (mail-header-id (gnus-summary-article-header)))
7958         (limit (if limit (prefix-numeric-value limit)
7959                  gnus-refer-thread-limit)))
7960     ;; We want to fetch LIMIT *old* headers, but we also have to
7961     ;; re-fetch all the headers in the current buffer, because many of
7962     ;; them may be undisplayed.  So we adjust LIMIT.
7963     (when (numberp limit)
7964       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7965     (unless (eq gnus-fetch-old-headers 'invisible)
7966       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7967       ;; Retrieve the headers and read them in.
7968       (if (eq (gnus-retrieve-headers
7969                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7970               'nov)
7971           (gnus-build-all-threads)
7972         (error "Can't fetch thread from backends that don't support NOV"))
7973       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7974     (gnus-summary-limit-include-thread id)))
7975
7976 (defun gnus-summary-refer-article (message-id)
7977   "Fetch an article specified by MESSAGE-ID."
7978   (interactive "sMessage-ID: ")
7979   (when (and (stringp message-id)
7980              (not (zerop (length message-id))))
7981     ;; Construct the correct Message-ID if necessary.
7982     ;; Suggested by tale@pawl.rpi.edu.
7983     (unless (string-match "^<" message-id)
7984       (setq message-id (concat "<" message-id)))
7985     (unless (string-match ">$" message-id)
7986       (setq message-id (concat message-id ">")))
7987     (let* ((header (gnus-id-to-header message-id))
7988            (sparse (and header
7989                         (gnus-summary-article-sparse-p
7990                          (mail-header-number header))
7991                         (memq (mail-header-number header)
7992                               gnus-newsgroup-limit)))
7993            number)
7994       (cond
7995        ;; If the article is present in the buffer we just go to it.
7996        ((and header
7997              (or (not (gnus-summary-article-sparse-p
7998                        (mail-header-number header)))
7999                  sparse))
8000         (prog1
8001             (gnus-summary-goto-article
8002              (mail-header-number header) nil t)
8003           (when sparse
8004             (gnus-summary-update-article (mail-header-number header)))))
8005        (t
8006         ;; We fetch the article.
8007         (catch 'found
8008           (dolist (gnus-override-method (gnus-refer-article-methods))
8009             (gnus-check-server gnus-override-method)
8010             ;; Fetch the header, and display the article.
8011             (when (setq number (gnus-summary-insert-subject message-id))
8012               (gnus-summary-select-article nil nil nil number)
8013               (throw 'found t)))
8014           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8015
8016 (defun gnus-refer-article-methods ()
8017   "Return a list of referable methods."
8018   (cond
8019    ;; No method, so we default to current and native.
8020    ((null gnus-refer-article-method)
8021     (list gnus-current-select-method gnus-select-method))
8022    ;; Current.
8023    ((eq 'current gnus-refer-article-method)
8024     (list gnus-current-select-method))
8025    ;; List of select methods.
8026    ((not (and (symbolp (car gnus-refer-article-method))
8027               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8028     (let (out)
8029       (dolist (method gnus-refer-article-method)
8030         (push (if (eq 'current method)
8031                   gnus-current-select-method
8032                 method)
8033               out))
8034       (nreverse out)))
8035    ;; One single select method.
8036    (t
8037     (list gnus-refer-article-method))))
8038
8039 (defun gnus-summary-edit-parameters ()
8040   "Edit the group parameters of the current group."
8041   (interactive)
8042   (gnus-group-edit-group gnus-newsgroup-name 'params))
8043
8044 (defun gnus-summary-customize-parameters ()
8045   "Customize the group parameters of the current group."
8046   (interactive)
8047   (gnus-group-customize gnus-newsgroup-name))
8048
8049 (defun gnus-summary-enter-digest-group (&optional force)
8050   "Enter an nndoc group based on the current article.
8051 If FORCE, force a digest interpretation.  If not, try
8052 to guess what the document format is."
8053   (interactive "P")
8054   (let ((conf gnus-current-window-configuration))
8055     (save-excursion
8056       (gnus-summary-select-article))
8057     (setq gnus-current-window-configuration conf)
8058     (let* ((name (format "%s-%d"
8059                          (gnus-group-prefixed-name
8060                           gnus-newsgroup-name (list 'nndoc ""))
8061                          (save-excursion
8062                            (set-buffer gnus-summary-buffer)
8063                            gnus-current-article)))
8064            (ogroup gnus-newsgroup-name)
8065            (params (append (gnus-info-params (gnus-get-info ogroup))
8066                            (list (cons 'to-group ogroup))
8067                            (list (cons 'save-article-group ogroup))))
8068            (case-fold-search t)
8069            (buf (current-buffer))
8070            dig to-address)
8071       (save-excursion
8072         (set-buffer gnus-original-article-buffer)
8073         ;; Have the digest group inherit the main mail address of
8074         ;; the parent article.
8075         (when (setq to-address (or (gnus-fetch-field "reply-to")
8076                                    (gnus-fetch-field "from")))
8077           (setq params (append
8078                         (list (cons 'to-address
8079                                     (funcall gnus-decode-encoded-word-function
8080                                              to-address))))))
8081         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8082         (insert-buffer-substring gnus-original-article-buffer)
8083         ;; Remove lines that may lead nndoc to misinterpret the
8084         ;; document type.
8085         (narrow-to-region
8086          (goto-char (point-min))
8087          (or (search-forward "\n\n" nil t) (point)))
8088         (goto-char (point-min))
8089         (delete-matching-lines "^Path:\\|^From ")
8090         (widen))
8091       (unwind-protect
8092           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8093                     (gnus-newsgroup-ephemeral-ignored-charsets
8094                      gnus-newsgroup-ignored-charsets))
8095                 (gnus-group-read-ephemeral-group
8096                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8097                               (nndoc-article-type
8098                                ,(if force 'mbox 'guess)))
8099                  t nil nil nil
8100                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8101                                                         "ADAPT")))))
8102               ;; Make all postings to this group go to the parent group.
8103               (nconc (gnus-info-params (gnus-get-info name))
8104                      params)
8105             ;; Couldn't select this doc group.
8106             (switch-to-buffer buf)
8107             (gnus-set-global-variables)
8108             (gnus-configure-windows 'summary)
8109             (gnus-message 3 "Article couldn't be entered?"))
8110         (kill-buffer dig)))))
8111
8112 (defun gnus-summary-read-document (n)
8113   "Open a new group based on the current article(s).
8114 This will allow you to read digests and other similar
8115 documents as newsgroups.
8116 Obeys the standard process/prefix convention."
8117   (interactive "P")
8118   (let* ((articles (gnus-summary-work-articles n))
8119          (ogroup gnus-newsgroup-name)
8120          (params (append (gnus-info-params (gnus-get-info ogroup))
8121                          (list (cons 'to-group ogroup))))
8122          article group egroup groups vgroup)
8123     (while (setq article (pop articles))
8124       (setq group (format "%s-%d" gnus-newsgroup-name article))
8125       (gnus-summary-remove-process-mark article)
8126       (when (gnus-summary-display-article article)
8127         (save-excursion
8128           (with-temp-buffer
8129             (insert-buffer-substring gnus-original-article-buffer)
8130             ;; Remove some headers that may lead nndoc to make
8131             ;; the wrong guess.
8132             (message-narrow-to-head)
8133             (goto-char (point-min))
8134             (delete-matching-lines "^\\(Path\\):\\|^From ")
8135             (widen)
8136             (if (setq egroup
8137                       (gnus-group-read-ephemeral-group
8138                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8139                                      (nndoc-article-type guess))
8140                        t nil t))
8141                 (progn
8142             ;; Make all postings to this group go to the parent group.
8143                   (nconc (gnus-info-params (gnus-get-info egroup))
8144                          params)
8145                   (push egroup groups))
8146               ;; Couldn't select this doc group.
8147               (gnus-error 3 "Article couldn't be entered"))))))
8148     ;; Now we have selected all the documents.
8149     (cond
8150      ((not groups)
8151       (error "None of the articles could be interpreted as documents"))
8152      ((gnus-group-read-ephemeral-group
8153        (setq vgroup (format
8154                      "nnvirtual:%s-%s" gnus-newsgroup-name
8155                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8156        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8157        t
8158        (cons (current-buffer) 'summary)))
8159      (t
8160       (error "Couldn't select virtual nndoc group")))))
8161
8162 (defun gnus-summary-isearch-article (&optional regexp-p)
8163   "Do incremental search forward on the current article.
8164 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8165   (interactive "P")
8166   (gnus-summary-select-article)
8167   (gnus-configure-windows 'article)
8168   (gnus-eval-in-buffer-window gnus-article-buffer
8169     (save-restriction
8170       (widen)
8171       (isearch-forward regexp-p))))
8172
8173 (defun gnus-summary-search-article-forward (regexp &optional backward)
8174   "Search for an article containing REGEXP forward.
8175 If BACKWARD, search backward instead."
8176   (interactive
8177    (list (read-string
8178           (format "Search article %s (regexp%s): "
8179                   (if current-prefix-arg "backward" "forward")
8180                   (if gnus-last-search-regexp
8181                       (concat ", default " gnus-last-search-regexp)
8182                     "")))
8183          current-prefix-arg))
8184   (if (string-equal regexp "")
8185       (setq regexp (or gnus-last-search-regexp ""))
8186     (setq gnus-last-search-regexp regexp)
8187     (setq gnus-article-before-search gnus-current-article))
8188   ;; Intentionally set gnus-last-article.
8189   (setq gnus-last-article gnus-article-before-search)
8190   (let ((gnus-last-article gnus-last-article))
8191     (if (gnus-summary-search-article regexp backward)
8192         (gnus-summary-show-thread)
8193       (error "Search failed: \"%s\"" regexp))))
8194
8195 (defun gnus-summary-search-article-backward (regexp)
8196   "Search for an article containing REGEXP backward."
8197   (interactive
8198    (list (read-string
8199           (format "Search article backward (regexp%s): "
8200                   (if gnus-last-search-regexp
8201                       (concat ", default " gnus-last-search-regexp)
8202                     "")))))
8203   (gnus-summary-search-article-forward regexp 'backward))
8204
8205 (defun gnus-summary-search-article (regexp &optional backward)
8206   "Search for an article containing REGEXP.
8207 Optional argument BACKWARD means do search for backward.
8208 `gnus-select-article-hook' is not called during the search."
8209   ;; We have to require this here to make sure that the following
8210   ;; dynamic binding isn't shadowed by autoloading.
8211   (require 'gnus-async)
8212   (require 'gnus-art)
8213   (let ((gnus-select-article-hook nil)  ;Disable hook.
8214         (gnus-article-prepare-hook nil)
8215         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8216         (gnus-use-article-prefetch nil)
8217         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8218         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8219         (sum (current-buffer))
8220         (gnus-display-mime-function nil)
8221         (found nil)
8222         point)
8223     (gnus-save-hidden-threads
8224       (gnus-summary-select-article)
8225       (set-buffer gnus-article-buffer)
8226       (goto-char (window-point (get-buffer-window (current-buffer))))
8227       (when backward
8228         (forward-line -1))
8229       (while (not found)
8230         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8231         (if (if backward
8232                 (re-search-backward regexp nil t)
8233               (re-search-forward regexp nil t))
8234             ;; We found the regexp.
8235             (progn
8236               (setq found 'found)
8237               (beginning-of-line)
8238               (set-window-start
8239                (get-buffer-window (current-buffer))
8240                (point))
8241               (forward-line 1)
8242               (set-window-point
8243                (get-buffer-window (current-buffer))
8244                (point))
8245               (set-buffer sum)
8246               (setq point (point)))
8247           ;; We didn't find it, so we go to the next article.
8248           (set-buffer sum)
8249           (setq found 'not)
8250           (while (eq found 'not)
8251             (if (not (if backward (gnus-summary-find-prev)
8252                        (gnus-summary-find-next)))
8253                 ;; No more articles.
8254                 (setq found t)
8255               ;; Select the next article and adjust point.
8256               (unless (gnus-summary-article-sparse-p
8257                        (gnus-summary-article-number))
8258                 (setq found nil)
8259                 (gnus-summary-select-article)
8260                 (set-buffer gnus-article-buffer)
8261                 (widen)
8262                 (goto-char (if backward (point-max) (point-min))))))))
8263       (gnus-message 7 ""))
8264     ;; Return whether we found the regexp.
8265     (when (eq found 'found)
8266       (goto-char point)
8267       (gnus-summary-show-thread)
8268       (gnus-summary-goto-subject gnus-current-article)
8269       (gnus-summary-position-point)
8270       t)))
8271
8272 (defun gnus-find-matching-articles (header regexp)
8273   "Return a list of all articles that match REGEXP on HEADER.
8274 This search includes all articles in the current group that Gnus has
8275 fetched headers for, whether they are displayed or not."
8276   (let ((articles nil)
8277         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8278         (case-fold-search t))
8279     (dolist (header gnus-newsgroup-headers)
8280       (when (string-match regexp (funcall func header))
8281         (push (mail-header-number header) articles)))
8282     (nreverse articles)))
8283
8284 (defun gnus-summary-find-matching (header regexp &optional backward unread
8285                                           not-case-fold not-matching)
8286   "Return a list of all articles that match REGEXP on HEADER.
8287 The search stars on the current article and goes forwards unless
8288 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8289 If UNREAD is non-nil, only unread articles will
8290 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8291 in the comparisons. If NOT-MATCHING, return a list of all articles that
8292 not match REGEXP on HEADER."
8293   (let ((case-fold-search (not not-case-fold))
8294         articles d func)
8295     (if (consp header)
8296         (if (eq (car header) 'extra)
8297             (setq func
8298                   `(lambda (h)
8299                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8300                          "")))
8301           (error "%s is an invalid header" header))
8302       (unless (fboundp (intern (concat "mail-header-" header)))
8303         (error "%s is not a valid header" header))
8304       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8305     (dolist (d (if (eq backward 'all)
8306                    gnus-newsgroup-data
8307                  (gnus-data-find-list
8308                   (gnus-summary-article-number)
8309                   (gnus-data-list backward))))
8310       (when (and (or (not unread)       ; We want all articles...
8311                      (gnus-data-unread-p d)) ; Or just unreads.
8312                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8313                  (if not-matching
8314                      (not (string-match
8315                            regexp
8316                            (funcall func (gnus-data-header d))))
8317                    (string-match regexp
8318                                  (funcall func (gnus-data-header d)))))
8319         (push (gnus-data-number d) articles))) ; Success!
8320     (nreverse articles)))
8321
8322 (defun gnus-summary-execute-command (header regexp command &optional backward)
8323   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8324 If HEADER is an empty string (or nil), the match is done on the entire
8325 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8326   (interactive
8327    (list (let ((completion-ignore-case t))
8328            (completing-read
8329             "Header name: "
8330             (mapcar (lambda (header) (list (format "%s" header)))
8331                     (append
8332                      '("Number" "Subject" "From" "Lines" "Date"
8333                        "Message-ID" "Xref" "References" "Body")
8334                      gnus-extra-headers))
8335             nil 'require-match))
8336          (read-string "Regexp: ")
8337          (read-key-sequence "Command: ")
8338          current-prefix-arg))
8339   (when (equal header "Body")
8340     (setq header ""))
8341   ;; Hidden thread subtrees must be searched as well.
8342   (gnus-summary-show-all-threads)
8343   ;; We don't want to change current point nor window configuration.
8344   (save-excursion
8345     (save-window-excursion
8346       (let (gnus-visual
8347             gnus-treat-strip-trailing-blank-lines
8348             gnus-treat-strip-leading-blank-lines
8349             gnus-treat-strip-multiple-blank-lines
8350             gnus-treat-hide-boring-headers
8351             gnus-treat-fold-newsgroups
8352             gnus-article-prepare-hook)
8353         (gnus-message 6 "Executing %s..." (key-description command))
8354         ;; We'd like to execute COMMAND interactively so as to give arguments.
8355         (gnus-execute header regexp
8356                       `(call-interactively ',(key-binding command))
8357                       backward)
8358         (gnus-message 6 "Executing %s...done" (key-description command))))))
8359
8360 (defun gnus-summary-beginning-of-article ()
8361   "Scroll the article back to the beginning."
8362   (interactive)
8363   (gnus-summary-select-article)
8364   (gnus-configure-windows 'article)
8365   (gnus-eval-in-buffer-window gnus-article-buffer
8366     (widen)
8367     (goto-char (point-min))
8368     (when gnus-page-broken
8369       (gnus-narrow-to-page))))
8370
8371 (defun gnus-summary-end-of-article ()
8372   "Scroll to the end of the article."
8373   (interactive)
8374   (gnus-summary-select-article)
8375   (gnus-configure-windows 'article)
8376   (gnus-eval-in-buffer-window gnus-article-buffer
8377     (widen)
8378     (goto-char (point-max))
8379     (recenter -3)
8380     (when gnus-page-broken
8381       (gnus-narrow-to-page))))
8382
8383 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8384   "Truncate to LEN and quote all \"(\"'s in STRING."
8385   (gnus-replace-in-string (if (and len (> (length string) len))
8386                               (substring string 0 len)
8387                             string)
8388                           "[()]" "\\\\\\&"))
8389
8390 (defun gnus-summary-print-article (&optional filename n)
8391   "Generate and print a PostScript image of the N next (mail) articles.
8392
8393 If N is negative, print the N previous articles.  If N is nil and articles
8394 have been marked with the process mark, print these instead.
8395
8396 If the optional first argument FILENAME is nil, send the image to the
8397 printer.  If FILENAME is a string, save the PostScript image in a file with
8398 that name.  If FILENAME is a number, prompt the user for the name of the file
8399 to save in."
8400   (interactive (list (ps-print-preprint current-prefix-arg)))
8401   (dolist (article (gnus-summary-work-articles n))
8402     (gnus-summary-select-article nil nil 'pseudo article)
8403     (gnus-eval-in-buffer-window gnus-article-buffer
8404       (gnus-print-buffer))
8405     (gnus-summary-remove-process-mark article))
8406   (ps-despool filename))
8407
8408 (defun gnus-print-buffer ()
8409   (let ((buffer (generate-new-buffer " *print*")))
8410     (unwind-protect
8411         (progn
8412           (copy-to-buffer buffer (point-min) (point-max))
8413           (set-buffer buffer)
8414           (gnus-article-delete-invisible-text)
8415           (gnus-remove-text-with-property 'gnus-decoration)
8416           (when (gnus-visual-p 'article-highlight 'highlight)
8417             ;; Copy-to-buffer doesn't copy overlay.  So redo
8418             ;; highlight.
8419             (let ((gnus-article-buffer buffer))
8420               (gnus-article-highlight-citation t)
8421               (gnus-article-highlight-signature)))
8422           (let ((ps-left-header
8423                  (list
8424                   (concat "("
8425                           (gnus-summary-print-truncate-and-quote
8426                            (mail-header-subject gnus-current-headers)
8427                            66) ")")
8428                   (concat "("
8429                           (gnus-summary-print-truncate-and-quote
8430                            (mail-header-from gnus-current-headers)
8431                            45) ")")))
8432                 (ps-right-header
8433                  (list
8434                   "/pagenumberstring load"
8435                   (concat "("
8436                           (mail-header-date gnus-current-headers) ")"))))
8437             (gnus-run-hooks 'gnus-ps-print-hook)
8438             (save-excursion
8439               (if window-system
8440                   (ps-spool-buffer-with-faces)
8441                 (ps-spool-buffer)))))
8442       (kill-buffer buffer))))
8443
8444 (defun gnus-summary-show-article (&optional arg)
8445   "Force redisplaying of the current article.
8446 If ARG (the prefix) is a number, show the article with the charset
8447 defined in `gnus-summary-show-article-charset-alist', or the charset
8448 input.
8449 If ARG (the prefix) is non-nil and not a number, show the raw article
8450 without any article massaging functions being run.  Normally, the key strokes
8451 are `C-u g'."
8452   (interactive "P")
8453   (cond
8454    ((numberp arg)
8455     (gnus-summary-show-article t)
8456     (let ((gnus-newsgroup-charset
8457            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8458                (mm-read-coding-system
8459                 "View as charset: " ;; actually it is coding system.
8460                 (save-excursion
8461                   (set-buffer gnus-article-buffer)
8462                   (mm-detect-coding-region (point) (point-max))))))
8463           (gnus-newsgroup-ignored-charsets 'gnus-all))
8464       (gnus-summary-select-article nil 'force)
8465       (let ((deps gnus-newsgroup-dependencies)
8466             head header lines)
8467         (save-excursion
8468           (set-buffer gnus-original-article-buffer)
8469           (save-restriction
8470             (message-narrow-to-head)
8471             (setq head (buffer-string))
8472             (goto-char (point-min))
8473             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8474               (goto-char (point-max))
8475               (widen)
8476               (setq lines (1- (count-lines (point) (point-max))))))
8477           (with-temp-buffer
8478             (insert (format "211 %d Article retrieved.\n"
8479                             (cdr gnus-article-current)))
8480             (insert head)
8481             (if lines (insert (format "Lines: %d\n" lines)))
8482             (insert ".\n")
8483             (let ((nntp-server-buffer (current-buffer)))
8484               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8485         (gnus-data-set-header
8486          (gnus-data-find (cdr gnus-article-current))
8487          header)
8488         (gnus-summary-update-article-line
8489          (cdr gnus-article-current) header)
8490         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8491           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8492    ((not arg)
8493     ;; Select the article the normal way.
8494     (gnus-summary-select-article nil 'force))
8495    (t
8496     ;; We have to require this here to make sure that the following
8497     ;; dynamic binding isn't shadowed by autoloading.
8498     (require 'gnus-async)
8499     (require 'gnus-art)
8500     ;; Bind the article treatment functions to nil.
8501     (let ((gnus-have-all-headers t)
8502           gnus-article-prepare-hook
8503           gnus-article-decode-hook
8504           gnus-display-mime-function
8505           gnus-break-pages)
8506       ;; Destroy any MIME parts.
8507       (when (gnus-buffer-live-p gnus-article-buffer)
8508         (save-excursion
8509           (set-buffer gnus-article-buffer)
8510           (mm-destroy-parts gnus-article-mime-handles)
8511           ;; Set it to nil for safety reason.
8512           (setq gnus-article-mime-handle-alist nil)
8513           (setq gnus-article-mime-handles nil)))
8514       (gnus-summary-select-article nil 'force))))
8515   (gnus-summary-goto-subject gnus-current-article)
8516   (gnus-summary-position-point))
8517
8518 (defun gnus-summary-show-raw-article ()
8519   "Show the raw article without any article massaging functions being run."
8520   (interactive)
8521   (gnus-summary-show-article t))
8522
8523 (defun gnus-summary-verbose-headers (&optional arg)
8524   "Toggle permanent full header display.
8525 If ARG is a positive number, turn header display on.
8526 If ARG is a negative number, turn header display off."
8527   (interactive "P")
8528   (setq gnus-show-all-headers
8529         (cond ((or (not (numberp arg))
8530                    (zerop arg))
8531                (not gnus-show-all-headers))
8532               ((natnump arg)
8533                t)))
8534   (gnus-summary-show-article))
8535
8536 (defun gnus-summary-toggle-header (&optional arg)
8537   "Show the headers if they are hidden, or hide them if they are shown.
8538 If ARG is a positive number, show the entire header.
8539 If ARG is a negative number, hide the unwanted header lines."
8540   (interactive "P")
8541   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8542                      (get-buffer-window gnus-article-buffer t))))
8543     (with-current-buffer gnus-article-buffer
8544       (widen)
8545       (article-narrow-to-head)
8546       (let* ((buffer-read-only nil)
8547              (inhibit-point-motion-hooks t)
8548              (hidden (if (numberp arg)
8549                          (>= arg 0)
8550                        (gnus-article-hidden-text-p 'headers)))
8551              s e)
8552         (delete-region (point-min) (point-max))
8553         (with-current-buffer gnus-original-article-buffer
8554           (goto-char (setq s (point-min)))
8555           (setq e (if (search-forward "\n\n" nil t)
8556                       (1- (point))
8557                     (point-max))))
8558         (insert-buffer-substring gnus-original-article-buffer s e)
8559         (article-decode-encoded-words)
8560         (if hidden
8561             (let ((gnus-treat-hide-headers nil)
8562                   (gnus-treat-hide-boring-headers nil))
8563               (gnus-delete-wash-type 'headers)
8564               (gnus-treat-article 'head))
8565           (gnus-treat-article 'head))
8566         (widen)
8567         (if window
8568             (set-window-start window (goto-char (point-min))))
8569         (setq gnus-page-broken
8570               (when gnus-break-pages
8571                 (gnus-narrow-to-page)
8572                 t))
8573         (gnus-set-mode-line 'article)))))
8574
8575 (defun gnus-summary-show-all-headers ()
8576   "Make all header lines visible."
8577   (interactive)
8578   (gnus-summary-toggle-header 1))
8579
8580 (defun gnus-summary-caesar-message (&optional arg)
8581   "Caesar rotate the current article by 13.
8582 The numerical prefix specifies how many places to rotate each letter
8583 forward."
8584   (interactive "P")
8585   (gnus-summary-select-article)
8586   (let ((mail-header-separator ""))
8587     (gnus-eval-in-buffer-window gnus-article-buffer
8588       (save-restriction
8589         (widen)
8590         (let ((start (window-start))
8591               buffer-read-only)
8592           (message-caesar-buffer-body arg)
8593           (set-window-start (get-buffer-window (current-buffer)) start))))))
8594
8595 (autoload 'unmorse-region "morse"
8596   "Convert morse coded text in region to ordinary ASCII text."
8597   t)
8598
8599 (defun gnus-summary-morse-message (&optional arg)
8600   "Morse decode the current article."
8601   (interactive "P")
8602   (gnus-summary-select-article)
8603   (let ((mail-header-separator ""))
8604     (gnus-eval-in-buffer-window gnus-article-buffer
8605       (save-excursion
8606         (save-restriction
8607           (widen)
8608           (let ((pos (window-start))
8609                 buffer-read-only)
8610             (goto-char (point-min))
8611             (when (message-goto-body)
8612               (gnus-narrow-to-body))
8613             (goto-char (point-min))
8614             (while (re-search-forward "·" (point-max) t)
8615               (replace-match "."))
8616             (unmorse-region (point-min) (point-max))
8617             (widen)
8618             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8619
8620 (defun gnus-summary-stop-page-breaking ()
8621   "Stop page breaking in the current article."
8622   (interactive)
8623   (gnus-summary-select-article)
8624   (gnus-eval-in-buffer-window gnus-article-buffer
8625     (widen)
8626     (when (gnus-visual-p 'page-marker)
8627       (let ((buffer-read-only nil))
8628         (gnus-remove-text-with-property 'gnus-prev)
8629         (gnus-remove-text-with-property 'gnus-next))
8630       (setq gnus-page-broken nil))))
8631
8632 (defun gnus-summary-move-article (&optional n to-newsgroup
8633                                             select-method action)
8634   "Move the current article to a different newsgroup.
8635 If N is a positive number, move the N next articles.
8636 If N is a negative number, move the N previous articles.
8637 If N is nil and any articles have been marked with the process mark,
8638 move those articles instead.
8639 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8640 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8641 re-spool using this method.
8642
8643 When called interactively with TO-NEWSGROUP being nil, the value of
8644 the variable `gnus-move-split-methods' is used for finding a default
8645 for the target newsgroup.
8646
8647 For this function to work, both the current newsgroup and the
8648 newsgroup that you want to move to have to support the `request-move'
8649 and `request-accept' functions.
8650
8651 ACTION can be either `move' (the default), `crosspost' or `copy'."
8652   (interactive "P")
8653   (unless action
8654     (setq action 'move))
8655   ;; Check whether the source group supports the required functions.
8656   (cond ((and (eq action 'move)
8657               (not (gnus-check-backend-function
8658                     'request-move-article gnus-newsgroup-name)))
8659          (error "The current group does not support article moving"))
8660         ((and (eq action 'crosspost)
8661               (not (gnus-check-backend-function
8662                     'request-replace-article gnus-newsgroup-name)))
8663          (error "The current group does not support article editing")))
8664   (let ((articles (gnus-summary-work-articles n))
8665         (prefix (if (gnus-check-backend-function
8666                      'request-move-article gnus-newsgroup-name)
8667                     (gnus-group-real-prefix gnus-newsgroup-name)
8668                   ""))
8669         (names '((move "Move" "Moving")
8670                  (copy "Copy" "Copying")
8671                  (crosspost "Crosspost" "Crossposting")))
8672         (copy-buf (save-excursion
8673                     (nnheader-set-temp-buffer " *copy article*")))
8674         art-group to-method new-xref article to-groups)
8675     (unless (assq action names)
8676       (error "Unknown action %s" action))
8677     ;; Read the newsgroup name.
8678     (when (and (not to-newsgroup)
8679                (not select-method))
8680       (if (and gnus-move-split-methods
8681                (not
8682                 (and (memq gnus-current-article articles)
8683                      (gnus-buffer-live-p gnus-original-article-buffer))))
8684           ;; When `gnus-move-split-methods' is non-nil, we have to
8685           ;; select an article to give `gnus-read-move-group-name' an
8686           ;; opportunity to suggest an appropriate default.  However,
8687           ;; we needn't render or mark the article.
8688           (let ((gnus-display-mime-function nil)
8689                 (gnus-article-prepare-hook nil)
8690                 (gnus-mark-article-hook nil))
8691             (gnus-summary-select-article nil nil nil (car articles))))
8692       (setq to-newsgroup
8693             (gnus-read-move-group-name
8694              (cadr (assq action names))
8695              (symbol-value (intern (format "gnus-current-%s-group" action)))
8696              articles prefix))
8697       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8698     (setq to-method (or select-method
8699                         (gnus-server-to-method
8700                          (gnus-group-method to-newsgroup))))
8701     ;; Check the method we are to move this article to...
8702     (unless (gnus-check-backend-function
8703              'request-accept-article (car to-method))
8704       (error "%s does not support article copying" (car to-method)))
8705     (unless (gnus-check-server to-method)
8706       (error "Can't open server %s" (car to-method)))
8707     (gnus-message 6 "%s to %s: %s..."
8708                   (caddr (assq action names))
8709                   (or (car select-method) to-newsgroup) articles)
8710     (while articles
8711       (setq article (pop articles))
8712       (setq
8713        art-group
8714        (cond
8715         ;; Move the article.
8716         ((eq action 'move)
8717          ;; Remove this article from future suppression.
8718          (gnus-dup-unsuppress-article article)
8719          (gnus-request-move-article
8720           article                       ; Article to move
8721           gnus-newsgroup-name           ; From newsgroup
8722           (nth 1 (gnus-find-method-for-group
8723                   gnus-newsgroup-name)) ; Server
8724           (list 'gnus-request-accept-article
8725                 to-newsgroup (list 'quote select-method)
8726                 (not articles) t)       ; Accept form
8727           (not articles)))              ; Only save nov last time
8728         ;; Copy the article.
8729         ((eq action 'copy)
8730          (save-excursion
8731            (set-buffer copy-buf)
8732            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8733              (gnus-request-accept-article
8734               to-newsgroup select-method (not articles) t))))
8735         ;; Crosspost the article.
8736         ((eq action 'crosspost)
8737          (let ((xref (message-tokenize-header
8738                       (mail-header-xref (gnus-summary-article-header article))
8739                       " ")))
8740            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8741                                   ":" (number-to-string article)))
8742            (unless xref
8743              (setq xref (list (system-name))))
8744            (setq new-xref
8745                  (concat
8746                   (mapconcat 'identity
8747                              (delete "Xref:" (delete new-xref xref))
8748                              " ")
8749                   " " new-xref))
8750            (save-excursion
8751              (set-buffer copy-buf)
8752              ;; First put the article in the destination group.
8753              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8754              (when (consp (setq art-group
8755                                 (gnus-request-accept-article
8756                                  to-newsgroup select-method (not articles))))
8757                (setq new-xref (concat new-xref " " (car art-group)
8758                                       ":"
8759                                       (number-to-string (cdr art-group))))
8760                ;; Now we have the new Xrefs header, so we insert
8761                ;; it and replace the new article.
8762                (nnheader-replace-header "Xref" new-xref)
8763                (gnus-request-replace-article
8764                 (cdr art-group) to-newsgroup (current-buffer))
8765                art-group))))))
8766       (cond
8767        ((not art-group)
8768         (gnus-message 1 "Couldn't %s article %s: %s"
8769                       (cadr (assq action names)) article
8770                       (nnheader-get-report (car to-method))))
8771        ((eq art-group 'junk)
8772         (when (eq action 'move)
8773           (gnus-summary-mark-article article gnus-canceled-mark)
8774           (gnus-message 4 "Deleted article %s" article)))
8775        (t
8776         (let* ((pto-group (gnus-group-prefixed-name
8777                            (car art-group) to-method))
8778                (entry
8779                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8780                (info (nth 2 entry))
8781                (to-group (gnus-info-group info))
8782                to-marks)
8783           ;; Update the group that has been moved to.
8784           (when (and info
8785                      (memq action '(move copy)))
8786             (unless (member to-group to-groups)
8787               (push to-group to-groups))
8788
8789             (unless (memq article gnus-newsgroup-unreads)
8790               (push 'read to-marks)
8791               (gnus-info-set-read
8792                info (gnus-add-to-range (gnus-info-read info)
8793                                        (list (cdr art-group)))))
8794
8795             ;; See whether the article is to be put in the cache.
8796             (let ((marks gnus-article-mark-lists)
8797                   (to-article (cdr art-group)))
8798
8799               ;; Enter the article into the cache in the new group,
8800               ;; if that is required.
8801               (when gnus-use-cache
8802                 (gnus-cache-possibly-enter-article
8803                  to-group to-article
8804                  (memq article gnus-newsgroup-marked)
8805                  (memq article gnus-newsgroup-dormant)
8806                  (memq article gnus-newsgroup-unreads)))
8807
8808               (when gnus-preserve-marks
8809                 ;; Copy any marks over to the new group.
8810                 (when (and (equal to-group gnus-newsgroup-name)
8811                            (not (memq article gnus-newsgroup-unreads)))
8812                   ;; Mark this article as read in this group.
8813                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8814                   (setcdr (gnus-active to-group) to-article)
8815                   (setcdr gnus-newsgroup-active to-article))
8816
8817                 (while marks
8818                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8819                     (when (memq article (symbol-value
8820                                          (intern (format "gnus-newsgroup-%s"
8821                                                          (caar marks)))))
8822                       (push (cdar marks) to-marks)
8823                       ;; If the other group is the same as this group,
8824                       ;; then we have to add the mark to the list.
8825                       (when (equal to-group gnus-newsgroup-name)
8826                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8827                              (cons to-article
8828                                    (symbol-value
8829                                     (intern (format "gnus-newsgroup-%s"
8830                                                     (caar marks)))))))
8831                       ;; Copy the marks to other group.
8832                       (gnus-add-marked-articles
8833                        to-group (cdar marks) (list to-article) info)))
8834                   (setq marks (cdr marks)))
8835
8836                 (gnus-request-set-mark
8837                  to-group (list (list (list to-article) 'add to-marks))))
8838
8839               (gnus-dribble-enter
8840                (concat "(gnus-group-set-info '"
8841                        (gnus-prin1-to-string (gnus-get-info to-group))
8842                        ")"))))
8843
8844           ;; Update the Xref header in this article to point to
8845           ;; the new crossposted article we have just created.
8846           (when (eq action 'crosspost)
8847             (save-excursion
8848               (set-buffer copy-buf)
8849               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8850               (nnheader-replace-header "Xref" new-xref)
8851               (gnus-request-replace-article
8852                article gnus-newsgroup-name (current-buffer)))))
8853
8854         ;;;!!!Why is this necessary?
8855         (set-buffer gnus-summary-buffer)
8856
8857         (gnus-summary-goto-subject article)
8858         (when (eq action 'move)
8859           (gnus-summary-mark-article article gnus-canceled-mark))))
8860       (gnus-summary-remove-process-mark article))
8861     ;; Re-activate all groups that have been moved to.
8862     (save-excursion
8863       (set-buffer gnus-group-buffer)
8864       (let ((gnus-group-marked to-groups))
8865         (gnus-group-get-new-news-this-group nil t)))
8866
8867     (gnus-kill-buffer copy-buf)
8868     (gnus-summary-position-point)
8869     (gnus-set-mode-line 'summary)))
8870
8871 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8872   "Move the current article to a different newsgroup.
8873 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8874 When called interactively, if TO-NEWSGROUP is nil, use the value of
8875 the variable `gnus-move-split-methods' for finding a default target
8876 newsgroup.
8877 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8878 re-spool using this method."
8879   (interactive "P")
8880   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8881
8882 (defun gnus-summary-crosspost-article (&optional n)
8883   "Crosspost the current article to some other group."
8884   (interactive "P")
8885   (gnus-summary-move-article n nil nil 'crosspost))
8886
8887 (defcustom gnus-summary-respool-default-method nil
8888   "Default method type for respooling an article.
8889 If nil, use to the current newsgroup method."
8890   :type 'symbol
8891   :group 'gnus-summary-mail)
8892
8893 (defcustom gnus-summary-display-while-building nil
8894   "If not-nil, show and update the summary buffer as it's being built.
8895 If the value is t, update the buffer after every line is inserted.  If
8896 the value is an integer (N), update the display every N lines."
8897   :group 'gnus-thread
8898   :type '(choice (const :tag "off" nil)
8899                  number
8900                  (const :tag "frequently" t)))
8901
8902 (defun gnus-summary-respool-article (&optional n method)
8903   "Respool the current article.
8904 The article will be squeezed through the mail spooling process again,
8905 which means that it will be put in some mail newsgroup or other
8906 depending on `nnmail-split-methods'.
8907 If N is a positive number, respool the N next articles.
8908 If N is a negative number, respool the N previous articles.
8909 If N is nil and any articles have been marked with the process mark,
8910 respool those articles instead.
8911
8912 Respooling can be done both from mail groups and \"real\" newsgroups.
8913 In the former case, the articles in question will be moved from the
8914 current group into whatever groups they are destined to.  In the
8915 latter case, they will be copied into the relevant groups."
8916   (interactive
8917    (list current-prefix-arg
8918          (let* ((methods (gnus-methods-using 'respool))
8919                 (methname
8920                  (symbol-name (or gnus-summary-respool-default-method
8921                                   (car (gnus-find-method-for-group
8922                                         gnus-newsgroup-name)))))
8923                 (method
8924                  (gnus-completing-read-with-default
8925                   methname "What backend do you want to use when respooling?"
8926                   methods nil t nil 'gnus-mail-method-history))
8927                 ms)
8928            (cond
8929             ((zerop (length (setq ms (gnus-servers-using-backend
8930                                       (intern method)))))
8931              (list (intern method) ""))
8932             ((= 1 (length ms))
8933              (car ms))
8934             (t
8935              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8936                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8937                            ms-alist))))))))
8938   (unless method
8939     (error "No method given for respooling"))
8940   (if (assoc (symbol-name
8941               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8942              (gnus-methods-using 'respool))
8943       (gnus-summary-move-article n nil method)
8944     (gnus-summary-copy-article n nil method)))
8945
8946 (defun gnus-summary-import-article (file &optional edit)
8947   "Import an arbitrary file into a mail newsgroup."
8948   (interactive "fImport file: \nP")
8949   (let ((group gnus-newsgroup-name)
8950         (now (current-time))
8951         atts lines group-art)
8952     (unless (gnus-check-backend-function 'request-accept-article group)
8953       (error "%s does not support article importing" group))
8954     (or (file-readable-p file)
8955         (not (file-regular-p file))
8956         (error "Can't read %s" file))
8957     (save-excursion
8958       (set-buffer (gnus-get-buffer-create " *import file*"))
8959       (erase-buffer)
8960       (nnheader-insert-file-contents file)
8961       (goto-char (point-min))
8962       (if (nnheader-article-p)
8963           (save-restriction
8964             (goto-char (point-min))
8965             (search-forward "\n\n" nil t)
8966             (narrow-to-region (point-min) (1- (point)))
8967             (goto-char (point-min))
8968             (unless (re-search-forward "^date:" nil t)
8969               (goto-char (point-max))
8970               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8971        ;; This doesn't look like an article, so we fudge some headers.
8972         (setq atts (file-attributes file)
8973               lines (count-lines (point-min) (point-max)))
8974         (insert "From: " (read-string "From: ") "\n"
8975                 "Subject: " (read-string "Subject: ") "\n"
8976                 "Date: " (message-make-date (nth 5 atts)) "\n"
8977                 "Message-ID: " (message-make-message-id) "\n"
8978                 "Lines: " (int-to-string lines) "\n"
8979                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8980       (setq group-art (gnus-request-accept-article group nil t))
8981       (kill-buffer (current-buffer)))
8982     (setq gnus-newsgroup-active (gnus-activate-group group))
8983     (forward-line 1)
8984     (gnus-summary-goto-article (cdr group-art) nil t)
8985     (when edit
8986       (gnus-summary-edit-article))))
8987
8988 (defun gnus-summary-create-article ()
8989   "Create an article in a mail newsgroup."
8990   (interactive)
8991   (let ((group gnus-newsgroup-name)
8992         (now (current-time))
8993         group-art)
8994     (unless (gnus-check-backend-function 'request-accept-article group)
8995       (error "%s does not support article importing" group))
8996     (save-excursion
8997       (set-buffer (gnus-get-buffer-create " *import file*"))
8998       (erase-buffer)
8999       (goto-char (point-min))
9000       ;; This doesn't look like an article, so we fudge some headers.
9001       (insert "From: " (read-string "From: ") "\n"
9002               "Subject: " (read-string "Subject: ") "\n"
9003               "Date: " (message-make-date now) "\n"
9004               "Message-ID: " (message-make-message-id) "\n")
9005       (setq group-art (gnus-request-accept-article group nil t))
9006       (kill-buffer (current-buffer)))
9007     (setq gnus-newsgroup-active (gnus-activate-group group))
9008     (forward-line 1)
9009     (gnus-summary-goto-article (cdr group-art) nil t)
9010     (gnus-summary-edit-article)))
9011
9012 (defun gnus-summary-article-posted-p ()
9013   "Say whether the current (mail) article is available from news as well.
9014 This will be the case if the article has both been mailed and posted."
9015   (interactive)
9016   (let ((id (mail-header-references (gnus-summary-article-header)))
9017         (gnus-override-method (car (gnus-refer-article-methods))))
9018     (if (gnus-request-head id "")
9019         (gnus-message 2 "The current message was found on %s"
9020                       gnus-override-method)
9021       (gnus-message 2 "The current message couldn't be found on %s"
9022                     gnus-override-method)
9023       nil)))
9024
9025 (defun gnus-summary-expire-articles (&optional now)
9026   "Expire all articles that are marked as expirable in the current group."
9027   (interactive)
9028   (when (gnus-check-backend-function
9029          'request-expire-articles gnus-newsgroup-name)
9030     ;; This backend supports expiry.
9031     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9032            (expirable (if total
9033                           (progn
9034                             ;; We need to update the info for
9035                             ;; this group for `gnus-list-of-read-articles'
9036                             ;; to give us the right answer.
9037                             (gnus-run-hooks 'gnus-exit-group-hook)
9038                             (gnus-summary-update-info)
9039                             (gnus-list-of-read-articles gnus-newsgroup-name))
9040                         (setq gnus-newsgroup-expirable
9041                               (sort gnus-newsgroup-expirable '<))))
9042            (expiry-wait (if now 'immediate
9043                           (gnus-group-find-parameter
9044                            gnus-newsgroup-name 'expiry-wait)))
9045            (nnmail-expiry-target
9046             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9047                 nnmail-expiry-target))
9048            es)
9049       (when expirable
9050         ;; There are expirable articles in this group, so we run them
9051         ;; through the expiry process.
9052         (gnus-message 6 "Expiring articles...")
9053         (unless (gnus-check-group gnus-newsgroup-name)
9054           (error "Can't open server for %s" gnus-newsgroup-name))
9055         ;; The list of articles that weren't expired is returned.
9056         (save-excursion
9057           (if expiry-wait
9058               (let ((nnmail-expiry-wait-function nil)
9059                     (nnmail-expiry-wait expiry-wait))
9060                 (setq es (gnus-request-expire-articles
9061                           expirable gnus-newsgroup-name)))
9062             (setq es (gnus-request-expire-articles
9063                       expirable gnus-newsgroup-name)))
9064           (unless total
9065             (setq gnus-newsgroup-expirable es))
9066           ;; We go through the old list of expirable, and mark all
9067           ;; really expired articles as nonexistent.
9068           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9069             (let ((gnus-use-cache nil))
9070               (dolist (article expirable)
9071                 (when (and (not (memq article es))
9072                            (gnus-data-find article))
9073                   (gnus-summary-mark-article article gnus-canceled-mark))))))
9074         (gnus-message 6 "Expiring articles...done")))))
9075
9076 (defun gnus-summary-expire-articles-now ()
9077   "Expunge all expirable articles in the current group.
9078 This means that *all* articles that are marked as expirable will be
9079 deleted forever, right now."
9080   (interactive)
9081   (or gnus-expert-user
9082       (gnus-yes-or-no-p
9083        "Are you really, really, really sure you want to delete all these messages? ")
9084       (error "Phew!"))
9085   (gnus-summary-expire-articles t))
9086
9087 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9088 (defun gnus-summary-delete-article (&optional n)
9089   "Delete the N next (mail) articles.
9090 This command actually deletes articles.  This is not a marking
9091 command.  The article will disappear forever from your life, never to
9092 return.
9093 If N is negative, delete backwards.
9094 If N is nil and articles have been marked with the process mark,
9095 delete these instead."
9096   (interactive "P")
9097   (unless (gnus-check-backend-function 'request-expire-articles
9098                                        gnus-newsgroup-name)
9099     (error "The current newsgroup does not support article deletion"))
9100   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9101     (error "Couldn't open server"))
9102   ;; Compute the list of articles to delete.
9103   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9104         (nnmail-expiry-target 'delete)
9105         not-deleted)
9106     (if (and gnus-novice-user
9107              (not (gnus-yes-or-no-p
9108                    (format "Do you really want to delete %s forever? "
9109                            (if (> (length articles) 1)
9110                                (format "these %s articles" (length articles))
9111                              "this article")))))
9112         ()
9113       ;; Delete the articles.
9114       (setq not-deleted (gnus-request-expire-articles
9115                          articles gnus-newsgroup-name 'force))
9116       (while articles
9117         (gnus-summary-remove-process-mark (car articles))
9118         ;; The backend might not have been able to delete the article
9119         ;; after all.
9120         (unless (memq (car articles) not-deleted)
9121           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9122         (setq articles (cdr articles)))
9123       (when not-deleted
9124         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9125     (gnus-summary-position-point)
9126     (gnus-set-mode-line 'summary)
9127     not-deleted))
9128
9129 (defun gnus-summary-edit-article (&optional arg)
9130   "Edit the current article.
9131 This will have permanent effect only in mail groups.
9132 If ARG is nil, edit the decoded articles.
9133 If ARG is 1, edit the raw articles.
9134 If ARG is 2, edit the raw articles even in read-only groups.
9135 If ARG is 3, edit the articles with the current handles.
9136 Otherwise, allow editing of articles even in read-only
9137 groups."
9138   (interactive "P")
9139   (let (force raw current-handles)
9140     (cond
9141      ((null arg))
9142      ((eq arg 1)
9143       (setq raw t))
9144      ((eq arg 2)
9145       (setq raw t
9146             force t))
9147      ((eq arg 3)
9148       (setq current-handles
9149             (and (gnus-buffer-live-p gnus-article-buffer)
9150                  (with-current-buffer gnus-article-buffer
9151                    (prog1
9152                        gnus-article-mime-handles
9153                      (setq gnus-article-mime-handles nil))))))
9154      (t
9155       (setq force t)))
9156     (when (and raw (not force)
9157                (member gnus-newsgroup-name '("nndraft:delayed"
9158                                              "nndraft:drafts"
9159                                              "nndraft:queue")))
9160       (error "Can't edit the raw article in group %s"
9161              gnus-newsgroup-name))
9162     (save-excursion
9163       (set-buffer gnus-summary-buffer)
9164       (let ((mail-parse-charset gnus-newsgroup-charset)
9165             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9166         (gnus-set-global-variables)
9167         (when (and (not force)
9168                    (gnus-group-read-only-p))
9169           (error "The current newsgroup does not support article editing"))
9170         (gnus-summary-show-article t)
9171         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9172           (with-current-buffer gnus-article-buffer
9173             (mm-enable-multibyte)))
9174         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9175             (setq raw t))
9176         (gnus-article-edit-article
9177          (if raw 'ignore
9178            `(lambda ()
9179               (let ((mbl mml-buffer-list))
9180                 (setq mml-buffer-list nil)
9181                 (mime-to-mml ,'current-handles)
9182                 (let ((mbl1 mml-buffer-list))
9183                   (setq mml-buffer-list mbl)
9184                   (set (make-local-variable 'mml-buffer-list) mbl1))
9185                 (make-local-hook 'kill-buffer-hook)
9186                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9187          `(lambda (no-highlight)
9188             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9189                   (message-options message-options)
9190                   (message-options-set-recipient)
9191                   (mail-parse-ignored-charsets
9192                    ',gnus-newsgroup-ignored-charsets))
9193               ,(if (not raw) '(progn
9194                                 (mml-to-mime)
9195                                 (mml-destroy-buffers)
9196                                 (remove-hook 'kill-buffer-hook
9197                                              'mml-destroy-buffers t)
9198                                 (kill-local-variable 'mml-buffer-list)))
9199               (gnus-summary-edit-article-done
9200                ,(or (mail-header-references gnus-current-headers) "")
9201                ,(gnus-group-read-only-p)
9202                ,gnus-summary-buffer no-highlight))))))))
9203
9204 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9205
9206 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9207                                                  no-highlight)
9208   "Make edits to the current article permanent."
9209   (interactive)
9210   (save-excursion
9211    ;; The buffer restriction contains the entire article if it exists.
9212     (when (article-goto-body)
9213       (let ((lines (count-lines (point) (point-max)))
9214             (length (- (point-max) (point)))
9215             (case-fold-search t)
9216             (body (copy-marker (point))))
9217         (goto-char (point-min))
9218         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9219           (delete-region (match-beginning 1) (match-end 1))
9220           (insert (number-to-string length)))
9221         (goto-char (point-min))
9222         (when (re-search-forward
9223                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9224           (delete-region (match-beginning 1) (match-end 1))
9225           (insert (number-to-string length)))
9226         (goto-char (point-min))
9227         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9228           (delete-region (match-beginning 1) (match-end 1))
9229           (insert (number-to-string lines))))))
9230   ;; Replace the article.
9231   (let ((buf (current-buffer)))
9232     (with-temp-buffer
9233       (insert-buffer-substring buf)
9234
9235       (if (and (not read-only)
9236                (not (gnus-request-replace-article
9237                      (cdr gnus-article-current) (car gnus-article-current)
9238                      (current-buffer) t)))
9239           (error "Couldn't replace article")
9240         ;; Update the summary buffer.
9241         (if (and references
9242                  (equal (message-tokenize-header references " ")
9243                         (message-tokenize-header
9244                          (or (message-fetch-field "references") "") " ")))
9245             ;; We only have to update this line.
9246             (save-excursion
9247               (save-restriction
9248                 (message-narrow-to-head)
9249                 (let ((head (buffer-string))
9250                       header)
9251                   (with-temp-buffer
9252                     (insert (format "211 %d Article retrieved.\n"
9253                                     (cdr gnus-article-current)))
9254                     (insert head)
9255                     (insert ".\n")
9256                     (let ((nntp-server-buffer (current-buffer)))
9257                       (setq header (car (gnus-get-newsgroup-headers
9258                                          nil t))))
9259                     (save-excursion
9260                       (set-buffer gnus-summary-buffer)
9261                       (gnus-data-set-header
9262                        (gnus-data-find (cdr gnus-article-current))
9263                        header)
9264                       (gnus-summary-update-article-line
9265                        (cdr gnus-article-current) header)
9266                       (if (gnus-summary-goto-subject
9267                            (cdr gnus-article-current) nil t)
9268                           (gnus-summary-update-secondary-mark
9269                            (cdr gnus-article-current))))))))
9270           ;; Update threads.
9271           (set-buffer (or buffer gnus-summary-buffer))
9272           (gnus-summary-update-article (cdr gnus-article-current))
9273           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9274               (gnus-summary-update-secondary-mark
9275                (cdr gnus-article-current))))
9276         ;; Prettify the article buffer again.
9277         (unless no-highlight
9278           (save-excursion
9279             (set-buffer gnus-article-buffer)
9280             ;;;!!! Fix this -- article should be rehighlighted.
9281             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9282             (set-buffer gnus-original-article-buffer)
9283             (gnus-request-article
9284              (cdr gnus-article-current)
9285              (car gnus-article-current) (current-buffer))))
9286         ;; Prettify the summary buffer line.
9287         (when (gnus-visual-p 'summary-highlight 'highlight)
9288           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9289
9290 (defun gnus-summary-edit-wash (key)
9291   "Perform editing command KEY in the article buffer."
9292   (interactive
9293    (list
9294     (progn
9295       (message "%s" (concat (this-command-keys) "- "))
9296       (read-char))))
9297   (message "")
9298   (gnus-summary-edit-article)
9299   (execute-kbd-macro (concat (this-command-keys) key))
9300   (gnus-article-edit-done))
9301
9302 ;;; Respooling
9303
9304 (defun gnus-summary-respool-query (&optional silent trace)
9305   "Query where the respool algorithm would put this article."
9306   (interactive)
9307   (let (gnus-mark-article-hook)
9308     (gnus-summary-select-article)
9309     (save-excursion
9310       (set-buffer gnus-original-article-buffer)
9311       (save-restriction
9312         (message-narrow-to-head)
9313         (let ((groups (nnmail-article-group 'identity trace)))
9314           (unless silent
9315             (if groups
9316                 (message "This message would go to %s"
9317                          (mapconcat 'car groups ", "))
9318               (message "This message would go to no groups"))
9319             groups))))))
9320
9321 (defun gnus-summary-respool-trace ()
9322   "Trace where the respool algorithm would put this article.
9323 Display a buffer showing all fancy splitting patterns which matched."
9324   (interactive)
9325   (gnus-summary-respool-query nil t))
9326
9327 ;; Summary marking commands.
9328
9329 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9330   "Mark articles which has the same subject as read, and then select the next.
9331 If UNMARK is positive, remove any kind of mark.
9332 If UNMARK is negative, tick articles."
9333   (interactive "P")
9334   (when unmark
9335     (setq unmark (prefix-numeric-value unmark)))
9336   (let ((count
9337          (gnus-summary-mark-same-subject
9338           (gnus-summary-article-subject) unmark)))
9339     ;; Select next unread article.  If auto-select-same mode, should
9340     ;; select the first unread article.
9341     (gnus-summary-next-article t (and gnus-auto-select-same
9342                                       (gnus-summary-article-subject)))
9343     (gnus-message 7 "%d article%s marked as %s"
9344                   count (if (= count 1) " is" "s are")
9345                   (if unmark "unread" "read"))))
9346
9347 (defun gnus-summary-kill-same-subject (&optional unmark)
9348   "Mark articles which has the same subject as read.
9349 If UNMARK is positive, remove any kind of mark.
9350 If UNMARK is negative, tick articles."
9351   (interactive "P")
9352   (when unmark
9353     (setq unmark (prefix-numeric-value unmark)))
9354   (let ((count
9355          (gnus-summary-mark-same-subject
9356           (gnus-summary-article-subject) unmark)))
9357     ;; If marked as read, go to next unread subject.
9358     (when (null unmark)
9359       ;; Go to next unread subject.
9360       (gnus-summary-next-subject 1 t))
9361     (gnus-message 7 "%d articles are marked as %s"
9362                   count (if unmark "unread" "read"))))
9363
9364 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9365   "Mark articles with same SUBJECT as read, and return marked number.
9366 If optional argument UNMARK is positive, remove any kinds of marks.
9367 If optional argument UNMARK is negative, mark articles as unread instead."
9368   (let ((count 1))
9369     (save-excursion
9370       (cond
9371        ((null unmark)                   ; Mark as read.
9372         (while (and
9373                 (progn
9374                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9375                   (gnus-summary-show-thread) t)
9376                 (gnus-summary-find-subject subject))
9377           (setq count (1+ count))))
9378        ((> unmark 0)                    ; Tick.
9379         (while (and
9380                 (progn
9381                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9382                   (gnus-summary-show-thread) t)
9383                 (gnus-summary-find-subject subject))
9384           (setq count (1+ count))))
9385        (t                               ; Mark as unread.
9386         (while (and
9387                 (progn
9388                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9389                   (gnus-summary-show-thread) t)
9390                 (gnus-summary-find-subject subject))
9391           (setq count (1+ count)))))
9392       (gnus-set-mode-line 'summary)
9393       ;; Return the number of marked articles.
9394       count)))
9395
9396 (defun gnus-summary-mark-as-processable (n &optional unmark)
9397   "Set the process mark on the next N articles.
9398 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9399 the process mark instead.  The difference between N and the actual
9400 number of articles marked is returned."
9401   (interactive "P")
9402   (if (and (null n) (gnus-region-active-p))
9403       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9404     (setq n (prefix-numeric-value n))
9405     (let ((backward (< n 0))
9406           (n (abs n)))
9407       (while (and
9408               (> n 0)
9409               (if unmark
9410                   (gnus-summary-remove-process-mark
9411                    (gnus-summary-article-number))
9412                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9413               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9414         (setq n (1- n)))
9415       (when (/= 0 n)
9416         (gnus-message 7 "No more articles"))
9417       (gnus-summary-recenter)
9418       (gnus-summary-position-point)
9419       n)))
9420
9421 (defun gnus-summary-unmark-as-processable (n)
9422   "Remove the process mark from the next N articles.
9423 If N is negative, unmark backward instead.  The difference between N and
9424 the actual number of articles unmarked is returned."
9425   (interactive "P")
9426   (gnus-summary-mark-as-processable n t))
9427
9428 (defun gnus-summary-unmark-all-processable ()
9429   "Remove the process mark from all articles."
9430   (interactive)
9431   (save-excursion
9432     (while gnus-newsgroup-processable
9433       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9434   (gnus-summary-position-point))
9435
9436 (defun gnus-summary-add-mark (article type)
9437   "Mark ARTICLE with a mark of TYPE."
9438   (let ((vtype (car (assq type gnus-article-mark-lists)))
9439         var)
9440     (if (not vtype)
9441         (error "No such mark type: %s" type)
9442       (setq var (intern (format "gnus-newsgroup-%s" type)))
9443       (set var (cons article (symbol-value var)))
9444       (if (memq type '(processable cached replied forwarded recent saved))
9445           (gnus-summary-update-secondary-mark article)
9446         ;;; !!! This is bogus.  We should find out what primary
9447         ;;; !!! mark we want to set.
9448         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9449
9450 (defun gnus-summary-mark-as-expirable (n)
9451   "Mark N articles forward as expirable.
9452 If N is negative, mark backward instead.  The difference between N and
9453 the actual number of articles marked is returned."
9454   (interactive "p")
9455   (gnus-summary-mark-forward n gnus-expirable-mark))
9456
9457 (defun gnus-summary-mark-as-spam (n)
9458   "Mark N articles forward as spam.
9459 If N is negative, mark backward instead.  The difference between N and
9460 the actual number of articles marked is returned."
9461   (interactive "p")
9462   (gnus-summary-mark-forward n gnus-spam-mark))
9463
9464 (defun gnus-summary-mark-article-as-replied (article)
9465   "Mark ARTICLE as replied to and update the summary line.
9466 ARTICLE can also be a list of articles."
9467   (interactive (list (gnus-summary-article-number)))
9468   (let ((articles (if (listp article) article (list article))))
9469     (dolist (article articles)
9470       (unless (numberp article)
9471         (error "%s is not a number" article))
9472       (push article gnus-newsgroup-replied)
9473       (let ((buffer-read-only nil))
9474         (when (gnus-summary-goto-subject article nil t)
9475           (gnus-summary-update-secondary-mark article))))))
9476
9477 (defun gnus-summary-mark-article-as-forwarded (article)
9478   "Mark ARTICLE as forwarded and update the summary line.
9479 ARTICLE can also be a list of articles."
9480   (let ((articles (if (listp article) article (list article))))
9481     (dolist (article articles)
9482       (push article gnus-newsgroup-forwarded)
9483       (let ((buffer-read-only nil))
9484         (when (gnus-summary-goto-subject article nil t)
9485           (gnus-summary-update-secondary-mark article))))))
9486
9487 (defun gnus-summary-set-bookmark (article)
9488   "Set a bookmark in current article."
9489   (interactive (list (gnus-summary-article-number)))
9490   (when (or (not (get-buffer gnus-article-buffer))
9491             (not gnus-current-article)
9492             (not gnus-article-current)
9493             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9494     (error "No current article selected"))
9495   ;; Remove old bookmark, if one exists.
9496   (gnus-pull article gnus-newsgroup-bookmarks)
9497   ;; Set the new bookmark, which is on the form
9498   ;; (article-number . line-number-in-body).
9499   (push
9500    (cons article
9501          (save-excursion
9502            (set-buffer gnus-article-buffer)
9503            (count-lines
9504             (min (point)
9505                  (save-excursion
9506                    (article-goto-body)
9507                    (point)))
9508             (point))))
9509    gnus-newsgroup-bookmarks)
9510   (gnus-message 6 "A bookmark has been added to the current article."))
9511
9512 (defun gnus-summary-remove-bookmark (article)
9513   "Remove the bookmark from the current article."
9514   (interactive (list (gnus-summary-article-number)))
9515   ;; Remove old bookmark, if one exists.
9516   (if (not (assq article gnus-newsgroup-bookmarks))
9517       (gnus-message 6 "No bookmark in current article.")
9518     (gnus-pull article gnus-newsgroup-bookmarks)
9519     (gnus-message 6 "Removed bookmark.")))
9520
9521 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9522 (defun gnus-summary-mark-as-dormant (n)
9523   "Mark N articles forward as dormant.
9524 If N is negative, mark backward instead.  The difference between N and
9525 the actual number of articles marked is returned."
9526   (interactive "p")
9527   (gnus-summary-mark-forward n gnus-dormant-mark))
9528
9529 (defun gnus-summary-set-process-mark (article)
9530   "Set the process mark on ARTICLE and update the summary line."
9531   (setq gnus-newsgroup-processable
9532         (cons article
9533               (delq article gnus-newsgroup-processable)))
9534   (when (gnus-summary-goto-subject article)
9535     (gnus-summary-show-thread)
9536     (gnus-summary-goto-subject article)
9537     (gnus-summary-update-secondary-mark article)))
9538
9539 (defun gnus-summary-remove-process-mark (article)
9540   "Remove the process mark from ARTICLE and update the summary line."
9541   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9542   (when (gnus-summary-goto-subject article)
9543     (gnus-summary-show-thread)
9544     (gnus-summary-goto-subject article)
9545     (gnus-summary-update-secondary-mark article)))
9546
9547 (defun gnus-summary-set-saved-mark (article)
9548   "Set the process mark on ARTICLE and update the summary line."
9549   (push article gnus-newsgroup-saved)
9550   (when (gnus-summary-goto-subject article)
9551     (gnus-summary-update-secondary-mark article)))
9552
9553 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9554   "Mark N articles as read forwards.
9555 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9556 The difference between N and the actual number of articles marked is
9557 returned.
9558 If NO-EXPIRE, auto-expiry will be inhibited."
9559   (interactive "p")
9560   (gnus-summary-show-thread)
9561   (let ((backward (< n 0))
9562         (gnus-summary-goto-unread
9563          (and gnus-summary-goto-unread
9564               (not (eq gnus-summary-goto-unread 'never))
9565               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9566                                     gnus-ticked-mark gnus-dormant-mark)))))
9567         (n (abs n))
9568         (mark (or mark gnus-del-mark)))
9569     (while (and (> n 0)
9570                 (gnus-summary-mark-article nil mark no-expire)
9571                 (zerop (gnus-summary-next-subject
9572                         (if backward -1 1)
9573                         (and gnus-summary-goto-unread
9574                              (not (eq gnus-summary-goto-unread 'never)))
9575                         t)))
9576       (setq n (1- n)))
9577     (when (/= 0 n)
9578       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9579     (gnus-summary-recenter)
9580     (gnus-summary-position-point)
9581     (gnus-set-mode-line 'summary)
9582     n))
9583
9584 (defun gnus-summary-mark-article-as-read (mark)
9585   "Mark the current article quickly as read with MARK."
9586   (let ((article (gnus-summary-article-number)))
9587     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9588     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9589     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9590     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9591     (push (cons article mark) gnus-newsgroup-reads)
9592     ;; Possibly remove from cache, if that is used.
9593     (when gnus-use-cache
9594       (gnus-cache-enter-remove-article article))
9595     ;; Allow the backend to change the mark.
9596     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9597     ;; Check for auto-expiry.
9598     (when (and gnus-newsgroup-auto-expire
9599                (memq mark gnus-auto-expirable-marks))
9600       (setq mark gnus-expirable-mark)
9601       ;; Let the backend know about the mark change.
9602       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9603       (push article gnus-newsgroup-expirable))
9604     ;; Set the mark in the buffer.
9605     (gnus-summary-update-mark mark 'unread)
9606     t))
9607
9608 (defun gnus-summary-mark-article-as-unread (mark)
9609   "Mark the current article quickly as unread with MARK."
9610   (let* ((article (gnus-summary-article-number))
9611          (old-mark (gnus-summary-article-mark article)))
9612     ;; Allow the backend to change the mark.
9613     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9614     (if (eq mark old-mark)
9615         t
9616       (if (<= article 0)
9617           (progn
9618             (gnus-error 1 "Can't mark negative article numbers")
9619             nil)
9620         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9621         (setq gnus-newsgroup-spam-marked
9622               (delq article gnus-newsgroup-spam-marked))
9623         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9624         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9625         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9626         (cond ((= mark gnus-ticked-mark)
9627                (setq gnus-newsgroup-marked
9628                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9629                                               article)))
9630               ((= mark gnus-spam-mark)
9631                (setq gnus-newsgroup-spam-marked
9632                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9633                                               article)))
9634               ((= mark gnus-dormant-mark)
9635                (setq gnus-newsgroup-dormant
9636                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9637                                               article)))
9638               (t
9639                (setq gnus-newsgroup-unreads
9640                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9641                                               article))))
9642         (gnus-pull article gnus-newsgroup-reads)
9643
9644         ;; See whether the article is to be put in the cache.
9645         (and gnus-use-cache
9646              (vectorp (gnus-summary-article-header article))
9647              (save-excursion
9648                (gnus-cache-possibly-enter-article
9649                 gnus-newsgroup-name article
9650                 (= mark gnus-ticked-mark)
9651                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9652
9653         ;; Fix the mark.
9654         (gnus-summary-update-mark mark 'unread)
9655         t))))
9656
9657 (defun gnus-summary-mark-article (&optional article mark no-expire)
9658   "Mark ARTICLE with MARK.  MARK can be any character.
9659 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9660 `??' (dormant) and `?E' (expirable).
9661 If MARK is nil, then the default character `?r' is used.
9662 If ARTICLE is nil, then the article on the current line will be
9663 marked.
9664 If NO-EXPIRE, auto-expiry will be inhibited."
9665   ;; The mark might be a string.
9666   (when (stringp mark)
9667     (setq mark (aref mark 0)))
9668   ;; If no mark is given, then we check auto-expiring.
9669   (when (null mark)
9670     (setq mark gnus-del-mark))
9671   (when (and (not no-expire)
9672              gnus-newsgroup-auto-expire
9673              (memq mark gnus-auto-expirable-marks))
9674     (setq mark gnus-expirable-mark))
9675   (let ((article (or article (gnus-summary-article-number)))
9676         (old-mark (gnus-summary-article-mark article)))
9677     ;; Allow the backend to change the mark.
9678     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9679     (if (eq mark old-mark)
9680         t
9681       (unless article
9682         (error "No article on current line"))
9683       (if (not (if (or (= mark gnus-unread-mark)
9684                        (= mark gnus-ticked-mark)
9685                        (= mark gnus-spam-mark)
9686                        (= mark gnus-dormant-mark))
9687                    (gnus-mark-article-as-unread article mark)
9688                  (gnus-mark-article-as-read article mark)))
9689           t
9690         ;; See whether the article is to be put in the cache.
9691         (and gnus-use-cache
9692              (not (= mark gnus-canceled-mark))
9693              (vectorp (gnus-summary-article-header article))
9694              (save-excursion
9695                (gnus-cache-possibly-enter-article
9696                 gnus-newsgroup-name article
9697                 (= mark gnus-ticked-mark)
9698                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9699
9700         (when (gnus-summary-goto-subject article nil t)
9701           (let ((buffer-read-only nil))
9702             (gnus-summary-show-thread)
9703             ;; Fix the mark.
9704             (gnus-summary-update-mark mark 'unread)
9705             t))))))
9706
9707 (defun gnus-summary-update-secondary-mark (article)
9708   "Update the secondary (read, process, cache) mark."
9709   (gnus-summary-update-mark
9710    (cond ((memq article gnus-newsgroup-processable)
9711           gnus-process-mark)
9712          ((memq article gnus-newsgroup-cached)
9713           gnus-cached-mark)
9714          ((memq article gnus-newsgroup-replied)
9715           gnus-replied-mark)
9716          ((memq article gnus-newsgroup-forwarded)
9717           gnus-forwarded-mark)
9718          ((memq article gnus-newsgroup-saved)
9719           gnus-saved-mark)
9720          ((memq article gnus-newsgroup-recent)
9721           gnus-recent-mark)
9722          ((memq article gnus-newsgroup-unseen)
9723           gnus-unseen-mark)
9724          (t gnus-no-mark))
9725    'replied)
9726   (when (gnus-visual-p 'summary-highlight 'highlight)
9727     (gnus-run-hooks 'gnus-summary-update-hook))
9728   t)
9729
9730 (defun gnus-summary-update-download-mark (article)
9731   "Update the secondary (read, process, cache) mark."
9732   (gnus-summary-update-mark
9733    (cond ((memq article gnus-newsgroup-undownloaded) 
9734           gnus-undownloaded-mark)
9735          (gnus-newsgroup-agentized
9736           gnus-downloaded-mark)
9737          (t
9738           gnus-no-mark))
9739    'download)
9740   (gnus-summary-update-line t)
9741   t)
9742
9743 (defun gnus-summary-update-mark (mark type)
9744   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9745         (buffer-read-only nil))
9746     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9747     (when forward
9748       (when (looking-at "\r")
9749         (incf forward))
9750       (when (<= (+ forward (point)) (point-max))
9751         ;; Go to the right position on the line.
9752         (goto-char (+ forward (point)))
9753         ;; Replace the old mark with the new mark.
9754         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9755         ;; Optionally update the marks by some user rule.
9756         (when (eq type 'unread)
9757           (gnus-data-set-mark
9758            (gnus-data-find (gnus-summary-article-number)) mark)
9759           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9760
9761 (defun gnus-mark-article-as-read (article &optional mark)
9762   "Enter ARTICLE in the pertinent lists and remove it from others."
9763   ;; Make the article expirable.
9764   (let ((mark (or mark gnus-del-mark)))
9765     (setq gnus-newsgroup-expirable
9766           (if (= mark gnus-expirable-mark)
9767               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9768             (delq article gnus-newsgroup-expirable)))
9769     ;; Remove from unread and marked lists.
9770     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9771     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9772     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9773     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9774     (push (cons article mark) gnus-newsgroup-reads)
9775     ;; Possibly remove from cache, if that is used.
9776     (when gnus-use-cache
9777       (gnus-cache-enter-remove-article article))
9778     t))
9779
9780 (defun gnus-mark-article-as-unread (article &optional mark)
9781   "Enter ARTICLE in the pertinent lists and remove it from others."
9782   (let ((mark (or mark gnus-ticked-mark)))
9783     (if (<= article 0)
9784         (progn
9785           (gnus-error 1 "Can't mark negative article numbers")
9786           nil)
9787       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9788             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9789             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9790             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9791             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9792
9793       ;; Unsuppress duplicates?
9794       (when gnus-suppress-duplicates
9795         (gnus-dup-unsuppress-article article))
9796
9797       (cond ((= mark gnus-ticked-mark)
9798              (setq gnus-newsgroup-marked
9799                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9800             ((= mark gnus-spam-mark)
9801              (setq gnus-newsgroup-spam-marked
9802                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9803                                             article)))
9804             ((= mark gnus-dormant-mark)
9805              (setq gnus-newsgroup-dormant
9806                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9807             (t
9808              (setq gnus-newsgroup-unreads
9809                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9810       (gnus-pull article gnus-newsgroup-reads)
9811       t)))
9812
9813 (defalias 'gnus-summary-mark-as-unread-forward
9814   'gnus-summary-tick-article-forward)
9815 (make-obsolete 'gnus-summary-mark-as-unread-forward
9816                'gnus-summary-tick-article-forward)
9817 (defun gnus-summary-tick-article-forward (n)
9818   "Tick N articles forwards.
9819 If N is negative, tick backwards instead.
9820 The difference between N and the number of articles ticked is returned."
9821   (interactive "p")
9822   (gnus-summary-mark-forward n gnus-ticked-mark))
9823
9824 (defalias 'gnus-summary-mark-as-unread-backward
9825   'gnus-summary-tick-article-backward)
9826 (make-obsolete 'gnus-summary-mark-as-unread-backward
9827                'gnus-summary-tick-article-backward)
9828 (defun gnus-summary-tick-article-backward (n)
9829   "Tick N articles backwards.
9830 The difference between N and the number of articles ticked is returned."
9831   (interactive "p")
9832   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9833
9834 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9835 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9836 (defun gnus-summary-tick-article (&optional article clear-mark)
9837   "Mark current article as unread.
9838 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9839 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9840   (interactive)
9841   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9842                                        gnus-ticked-mark)))
9843
9844 (defun gnus-summary-mark-as-read-forward (n)
9845   "Mark N articles as read forwards.
9846 If N is negative, mark backwards instead.
9847 The difference between N and the actual number of articles marked is
9848 returned."
9849   (interactive "p")
9850   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9851
9852 (defun gnus-summary-mark-as-read-backward (n)
9853   "Mark the N articles as read backwards.
9854 The difference between N and the actual number of articles marked is
9855 returned."
9856   (interactive "p")
9857   (gnus-summary-mark-forward
9858    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9859
9860 (defun gnus-summary-mark-as-read (&optional article mark)
9861   "Mark current article as read.
9862 ARTICLE specifies the article to be marked as read.
9863 MARK specifies a string to be inserted at the beginning of the line."
9864   (gnus-summary-mark-article article mark))
9865
9866 (defun gnus-summary-clear-mark-forward (n)
9867   "Clear marks from N articles forward.
9868 If N is negative, clear backward instead.
9869 The difference between N and the number of marks cleared is returned."
9870   (interactive "p")
9871   (gnus-summary-mark-forward n gnus-unread-mark))
9872
9873 (defun gnus-summary-clear-mark-backward (n)
9874   "Clear marks from N articles backward.
9875 The difference between N and the number of marks cleared is returned."
9876   (interactive "p")
9877   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9878
9879 (defun gnus-summary-mark-unread-as-read ()
9880   "Intended to be used by `gnus-summary-mark-article-hook'."
9881   (when (memq gnus-current-article gnus-newsgroup-unreads)
9882     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9883
9884 (defun gnus-summary-mark-read-and-unread-as-read ()
9885   "Intended to be used by `gnus-summary-mark-article-hook'."
9886   (let ((mark (gnus-summary-article-mark)))
9887     (when (or (gnus-unread-mark-p mark)
9888               (gnus-read-mark-p mark))
9889       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9890
9891 (defun gnus-summary-mark-unread-as-ticked ()
9892   "Intended to be used by `gnus-summary-mark-article-hook'."
9893   (when (memq gnus-current-article gnus-newsgroup-unreads)
9894     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9895
9896 (defun gnus-summary-mark-region-as-read (point mark all)
9897   "Mark all unread articles between point and mark as read.
9898 If given a prefix, mark all articles between point and mark as read,
9899 even ticked and dormant ones."
9900   (interactive "r\nP")
9901   (save-excursion
9902     (let (article)
9903       (goto-char point)
9904       (beginning-of-line)
9905       (while (and
9906               (< (point) mark)
9907               (progn
9908                 (when (or all
9909                           (memq (setq article (gnus-summary-article-number))
9910                                 gnus-newsgroup-unreads))
9911                   (gnus-summary-mark-article article gnus-del-mark))
9912                 t)
9913               (gnus-summary-find-next))))))
9914
9915 (defun gnus-summary-mark-below (score mark)
9916   "Mark articles with score less than SCORE with MARK."
9917   (interactive "P\ncMark: ")
9918   (setq score (if score
9919                   (prefix-numeric-value score)
9920                 (or gnus-summary-default-score 0)))
9921   (save-excursion
9922     (set-buffer gnus-summary-buffer)
9923     (goto-char (point-min))
9924     (while
9925         (progn
9926           (and (< (gnus-summary-article-score) score)
9927                (gnus-summary-mark-article nil mark))
9928           (gnus-summary-find-next)))))
9929
9930 (defun gnus-summary-kill-below (&optional score)
9931   "Mark articles with score below SCORE as read."
9932   (interactive "P")
9933   (gnus-summary-mark-below score gnus-killed-mark))
9934
9935 (defun gnus-summary-clear-above (&optional score)
9936   "Clear all marks from articles with score above SCORE."
9937   (interactive "P")
9938   (gnus-summary-mark-above score gnus-unread-mark))
9939
9940 (defun gnus-summary-tick-above (&optional score)
9941   "Tick all articles with score above SCORE."
9942   (interactive "P")
9943   (gnus-summary-mark-above score gnus-ticked-mark))
9944
9945 (defun gnus-summary-mark-above (score mark)
9946   "Mark articles with score over SCORE with MARK."
9947   (interactive "P\ncMark: ")
9948   (setq score (if score
9949                   (prefix-numeric-value score)
9950                 (or gnus-summary-default-score 0)))
9951   (save-excursion
9952     (set-buffer gnus-summary-buffer)
9953     (goto-char (point-min))
9954     (while (and (progn
9955                   (when (> (gnus-summary-article-score) score)
9956                     (gnus-summary-mark-article nil mark))
9957                   t)
9958                 (gnus-summary-find-next)))))
9959
9960 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9961 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9962 (defun gnus-summary-limit-include-expunged (&optional no-error)
9963   "Display all the hidden articles that were expunged for low scores."
9964   (interactive)
9965   (let ((buffer-read-only nil))
9966     (let ((scored gnus-newsgroup-scored)
9967           headers h)
9968       (while scored
9969         (unless (gnus-summary-article-header (caar scored))
9970           (and (setq h (gnus-number-to-header (caar scored)))
9971                (< (cdar scored) gnus-summary-expunge-below)
9972                (push h headers)))
9973         (setq scored (cdr scored)))
9974       (if (not headers)
9975           (when (not no-error)
9976             (error "No expunged articles hidden"))
9977         (goto-char (point-min))
9978         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9979         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9980         (mapcar (lambda (x) (push (mail-header-number x)
9981                                   gnus-newsgroup-limit))
9982                 headers)
9983         (gnus-summary-prepare-unthreaded (nreverse headers))
9984         (goto-char (point-min))
9985         (gnus-summary-position-point)
9986         t))))
9987
9988 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9989   "Mark all unread articles in this newsgroup as read.
9990 If prefix argument ALL is non-nil, ticked and dormant articles will
9991 also be marked as read.
9992 If QUIETLY is non-nil, no questions will be asked.
9993 If TO-HERE is non-nil, it should be a point in the buffer.  All
9994 articles before (after, if REVERSE is set) this point will be marked as read.
9995 Note that this function will only catch up the unread article
9996 in the current summary buffer limitation.
9997 The number of articles marked as read is returned."
9998   (interactive "P")
9999   (prog1
10000       (save-excursion
10001         (when (or quietly
10002                   (not gnus-interactive-catchup) ;Without confirmation?
10003                   gnus-expert-user
10004                   (gnus-y-or-n-p
10005                    (if all
10006                        "Mark absolutely all articles as read? "
10007                      "Mark all unread articles as read? ")))
10008           (if (and not-mark
10009                    (not gnus-newsgroup-adaptive)
10010                    (not gnus-newsgroup-auto-expire)
10011                    (not gnus-suppress-duplicates)
10012                    (or (not gnus-use-cache)
10013                        (eq gnus-use-cache 'passive)))
10014               (progn
10015                 (when all
10016                   (setq gnus-newsgroup-marked nil
10017                         gnus-newsgroup-spam-marked nil
10018                         gnus-newsgroup-dormant nil))
10019                 (setq gnus-newsgroup-unreads
10020                       (gnus-sorted-nunion
10021                        (gnus-intersection gnus-newsgroup-unreads
10022                                           gnus-newsgroup-downloadable)
10023                        gnus-newsgroup-unfetched)))
10024             ;; We actually mark all articles as canceled, which we
10025             ;; have to do when using auto-expiry or adaptive scoring.
10026             (gnus-summary-show-all-threads)
10027             (if (and to-here reverse)
10028                 (progn
10029                   (goto-char to-here)
10030                   (while (and
10031                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
10032                           (gnus-summary-find-next (not all)))))
10033               (when (gnus-summary-first-subject (not all))
10034                 (while (and
10035                         (if to-here (< (point) to-here) t)
10036                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10037                         (gnus-summary-find-next (not all))))))
10038             (gnus-set-mode-line 'summary))
10039           t))
10040     (gnus-summary-position-point)))
10041
10042 (defun gnus-summary-catchup-to-here (&optional all)
10043   "Mark all unticked articles before the current one as read.
10044 If ALL is non-nil, also mark ticked and dormant articles as read."
10045   (interactive "P")
10046   (save-excursion
10047     (gnus-save-hidden-threads
10048       (let ((beg (point)))
10049         ;; We check that there are unread articles.
10050         (when (or all (gnus-summary-find-prev))
10051           (gnus-summary-catchup all t beg)))))
10052   (gnus-summary-position-point))
10053
10054 (defun gnus-summary-catchup-from-here (&optional all)
10055   "Mark all unticked articles after the current one as read.
10056 If ALL is non-nil, also mark ticked and dormant articles as read."
10057   (interactive "P")
10058   (save-excursion
10059     (gnus-save-hidden-threads
10060       (let ((beg (point)))
10061         ;; We check that there are unread articles.
10062         (when (or all (gnus-summary-find-next))
10063           (gnus-summary-catchup all t beg nil t)))))
10064
10065   (gnus-summary-position-point))
10066 (defun gnus-summary-catchup-all (&optional quietly)
10067   "Mark all articles in this newsgroup as read.
10068 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10069 instead, which marks only unread articles as read."
10070   (interactive "P")
10071   (gnus-summary-catchup t quietly))
10072
10073 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10074   "Mark all unread articles in this group as read, then exit.
10075 If prefix argument ALL is non-nil, all articles are marked as read.
10076 If QUIETLY is non-nil, no questions will be asked."
10077   (interactive "P")
10078   (when (gnus-summary-catchup all quietly nil 'fast)
10079     ;; Select next newsgroup or exit.
10080     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10081              (eq gnus-auto-select-next 'quietly))
10082         (gnus-summary-next-group nil)
10083       (gnus-summary-exit))))
10084
10085 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10086   "Mark all articles in this newsgroup as read, and then exit.
10087 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10088 instead, which marks only unread articles as read."
10089   (interactive "P")
10090   (gnus-summary-catchup-and-exit t quietly))
10091
10092 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10093   "Mark all articles in this group as read and select the next group.
10094 If given a prefix, mark all articles, unread as well as ticked, as
10095 read."
10096   (interactive "P")
10097   (save-excursion
10098     (gnus-summary-catchup all))
10099   (gnus-summary-next-group))
10100
10101 ;;;
10102 ;;; with article
10103 ;;;
10104
10105 (defmacro gnus-with-article (article &rest forms)
10106   "Select ARTICLE and perform FORMS in the original article buffer.
10107 Then replace the article with the result."
10108   `(progn
10109      ;; We don't want the article to be marked as read.
10110      (let (gnus-mark-article-hook)
10111        (gnus-summary-select-article t t nil ,article))
10112      (set-buffer gnus-original-article-buffer)
10113      ,@forms
10114      (if (not (gnus-check-backend-function
10115                'request-replace-article (car gnus-article-current)))
10116          (gnus-message 5 "Read-only group; not replacing")
10117        (unless (gnus-request-replace-article
10118                 ,article (car gnus-article-current)
10119                 (current-buffer) t)
10120          (error "Couldn't replace article")))
10121      ;; The cache and backlog have to be flushed somewhat.
10122      (when gnus-keep-backlog
10123        (gnus-backlog-remove-article
10124         (car gnus-article-current) (cdr gnus-article-current)))
10125      (when gnus-use-cache
10126        (gnus-cache-update-article
10127         (car gnus-article-current) (cdr gnus-article-current)))))
10128
10129 (put 'gnus-with-article 'lisp-indent-function 1)
10130 (put 'gnus-with-article 'edebug-form-spec '(form body))
10131
10132 ;; Thread-based commands.
10133
10134 (defun gnus-summary-articles-in-thread (&optional article)
10135   "Return a list of all articles in the current thread.
10136 If ARTICLE is non-nil, return all articles in the thread that starts
10137 with that article."
10138   (let* ((article (or article (gnus-summary-article-number)))
10139          (data (gnus-data-find-list article))
10140          (top-level (gnus-data-level (car data)))
10141          (top-subject
10142           (cond ((null gnus-thread-operation-ignore-subject)
10143                  (gnus-simplify-subject-re
10144                   (mail-header-subject (gnus-data-header (car data)))))
10145                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10146                  (gnus-simplify-subject-fuzzy
10147                   (mail-header-subject (gnus-data-header (car data)))))
10148                 (t nil)))
10149          (end-point (save-excursion
10150                       (if (gnus-summary-go-to-next-thread)
10151                           (point) (point-max))))
10152          articles)
10153     (while (and data
10154                 (< (gnus-data-pos (car data)) end-point))
10155       (when (or (not top-subject)
10156                 (string= top-subject
10157                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10158                              (gnus-simplify-subject-fuzzy
10159                               (mail-header-subject
10160                                (gnus-data-header (car data))))
10161                            (gnus-simplify-subject-re
10162                             (mail-header-subject
10163                              (gnus-data-header (car data)))))))
10164         (push (gnus-data-number (car data)) articles))
10165       (unless (and (setq data (cdr data))
10166                    (> (gnus-data-level (car data)) top-level))
10167         (setq data nil)))
10168     ;; Return the list of articles.
10169     (nreverse articles)))
10170
10171 (defun gnus-summary-rethread-current ()
10172   "Rethread the thread the current article is part of."
10173   (interactive)
10174   (let* ((gnus-show-threads t)
10175          (article (gnus-summary-article-number))
10176          (id (mail-header-id (gnus-summary-article-header)))
10177          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10178     (unless id
10179       (error "No article on the current line"))
10180     (gnus-rebuild-thread id)
10181     (gnus-summary-goto-subject article)))
10182
10183 (defun gnus-summary-reparent-thread ()
10184   "Make the current article child of the marked (or previous) article.
10185
10186 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10187 is non-nil or the Subject: of both articles are the same."
10188   (interactive)
10189   (unless (not (gnus-group-read-only-p))
10190     (error "The current newsgroup does not support article editing"))
10191   (unless (<= (length gnus-newsgroup-processable) 1)
10192     (error "No more than one article may be marked"))
10193   (save-window-excursion
10194     (let ((gnus-article-buffer " *reparent*")
10195           (current-article (gnus-summary-article-number))
10196           ;; First grab the marked article, otherwise one line up.
10197           (parent-article (if (not (null gnus-newsgroup-processable))
10198                               (car gnus-newsgroup-processable)
10199                             (save-excursion
10200                               (if (eq (forward-line -1) 0)
10201                                   (gnus-summary-article-number)
10202                                 (error "Beginning of summary buffer"))))))
10203       (unless (not (eq current-article parent-article))
10204         (error "An article may not be self-referential"))
10205       (let ((message-id (mail-header-id
10206                          (gnus-summary-article-header parent-article))))
10207         (unless (and message-id (not (equal message-id "")))
10208           (error "No message-id in desired parent"))
10209         (gnus-with-article current-article
10210           (save-restriction
10211             (goto-char (point-min))
10212             (message-narrow-to-head)
10213             (if (re-search-forward "^References: " nil t)
10214                 (progn
10215                   (re-search-forward "^[^ \t]" nil t)
10216                   (forward-line -1)
10217                   (end-of-line)
10218                   (insert " " message-id))
10219               (insert "References: " message-id "\n"))))
10220         (set-buffer gnus-summary-buffer)
10221         (gnus-summary-unmark-all-processable)
10222         (gnus-summary-update-article current-article)
10223         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10224             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10225         (gnus-summary-rethread-current)
10226         (gnus-message 3 "Article %d is now the child of article %d"
10227                       current-article parent-article)))))
10228
10229 (defun gnus-summary-toggle-threads (&optional arg)
10230   "Toggle showing conversation threads.
10231 If ARG is positive number, turn showing conversation threads on."
10232   (interactive "P")
10233   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10234     (setq gnus-show-threads
10235           (if (null arg) (not gnus-show-threads)
10236             (> (prefix-numeric-value arg) 0)))
10237     (gnus-summary-prepare)
10238     (gnus-summary-goto-subject current)
10239     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10240     (gnus-summary-position-point)))
10241
10242 (defun gnus-summary-show-all-threads ()
10243   "Show all threads."
10244   (interactive)
10245   (save-excursion
10246     (let ((buffer-read-only nil))
10247       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10248   (gnus-summary-position-point))
10249
10250 (defun gnus-summary-show-thread ()
10251   "Show thread subtrees.
10252 Returns nil if no thread was there to be shown."
10253   (interactive)
10254   (let ((buffer-read-only nil)
10255         (orig (point))
10256         ;; first goto end then to beg, to have point at beg after let
10257         (end (progn (end-of-line) (point)))
10258         (beg (progn (beginning-of-line) (point))))
10259     (prog1
10260         ;; Any hidden lines here?
10261         (search-forward "\r" end t)
10262       (subst-char-in-region beg end ?\^M ?\n t)
10263       (goto-char orig)
10264       (gnus-summary-position-point))))
10265
10266 (defun gnus-summary-maybe-hide-threads ()
10267   "If requested, hide the threads that should be hidden."
10268   (when (and gnus-show-threads
10269              gnus-thread-hide-subtree)
10270     (gnus-summary-hide-all-threads
10271      (if (or (consp gnus-thread-hide-subtree)
10272              (gnus-functionp gnus-thread-hide-subtree))
10273          (gnus-make-predicate gnus-thread-hide-subtree)
10274        nil))))
10275
10276 ;;; Hiding predicates.
10277
10278 (defun gnus-article-unread-p (header)
10279   (memq (mail-header-number header) gnus-newsgroup-unreads))
10280
10281 (defun gnus-article-unseen-p (header)
10282   (memq (mail-header-number header) gnus-newsgroup-unseen))
10283
10284 (defun gnus-map-articles (predicate articles)
10285   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10286   (apply 'gnus-or (mapcar predicate
10287                           (mapcar 'gnus-summary-article-header articles))))
10288
10289 (defun gnus-summary-hide-all-threads (&optional predicate)
10290   "Hide all thread subtrees.
10291 If PREDICATE is supplied, threads that satisfy this predicate
10292 will not be hidden."
10293   (interactive)
10294   (save-excursion
10295     (goto-char (point-min))
10296     (let ((end nil))
10297       (while (not end)
10298         (when (or (not predicate)
10299                   (gnus-map-articles
10300                    predicate (gnus-summary-article-children)))
10301             (gnus-summary-hide-thread))
10302         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10303   (gnus-summary-position-point))
10304
10305 (defun gnus-summary-hide-thread ()
10306   "Hide thread subtrees.
10307 If PREDICATE is supplied, threads that satisfy this predicate
10308 will not be hidden.
10309 Returns nil if no threads were there to be hidden."
10310   (interactive)
10311   (let ((buffer-read-only nil)
10312         (start (point))
10313         (article (gnus-summary-article-number)))
10314     (goto-char start)
10315     ;; Go forward until either the buffer ends or the subthread
10316     ;; ends.
10317     (when (and (not (eobp))
10318                (or (zerop (gnus-summary-next-thread 1 t))
10319                    (goto-char (point-max))))
10320       (prog1
10321           (if (and (> (point) start)
10322                    (search-backward "\n" start t))
10323               (progn
10324                 (subst-char-in-region start (point) ?\n ?\^M)
10325                 (gnus-summary-goto-subject article))
10326             (goto-char start)
10327             nil)))))
10328
10329 (defun gnus-summary-go-to-next-thread (&optional previous)
10330   "Go to the same level (or less) next thread.
10331 If PREVIOUS is non-nil, go to previous thread instead.
10332 Return the article number moved to, or nil if moving was impossible."
10333   (let ((level (gnus-summary-thread-level))
10334         (way (if previous -1 1))
10335         (beg (point)))
10336     (forward-line way)
10337     (while (and (not (eobp))
10338                 (< level (gnus-summary-thread-level)))
10339       (forward-line way))
10340     (if (eobp)
10341         (progn
10342           (goto-char beg)
10343           nil)
10344       (setq beg (point))
10345       (prog1
10346           (gnus-summary-article-number)
10347         (goto-char beg)))))
10348
10349 (defun gnus-summary-next-thread (n &optional silent)
10350   "Go to the same level next N'th thread.
10351 If N is negative, search backward instead.
10352 Returns the difference between N and the number of skips actually
10353 done.
10354
10355 If SILENT, don't output messages."
10356   (interactive "p")
10357   (let ((backward (< n 0))
10358         (n (abs n)))
10359     (while (and (> n 0)
10360                 (gnus-summary-go-to-next-thread backward))
10361       (decf n))
10362     (unless silent
10363       (gnus-summary-position-point))
10364     (when (and (not silent) (/= 0 n))
10365       (gnus-message 7 "No more threads"))
10366     n))
10367
10368 (defun gnus-summary-prev-thread (n)
10369   "Go to the same level previous N'th thread.
10370 Returns the difference between N and the number of skips actually
10371 done."
10372   (interactive "p")
10373   (gnus-summary-next-thread (- n)))
10374
10375 (defun gnus-summary-go-down-thread ()
10376   "Go down one level in the current thread."
10377   (let ((children (gnus-summary-article-children)))
10378     (when children
10379       (gnus-summary-goto-subject (car children)))))
10380
10381 (defun gnus-summary-go-up-thread ()
10382   "Go up one level in the current thread."
10383   (let ((parent (gnus-summary-article-parent)))
10384     (when parent
10385       (gnus-summary-goto-subject parent))))
10386
10387 (defun gnus-summary-down-thread (n)
10388   "Go down thread N steps.
10389 If N is negative, go up instead.
10390 Returns the difference between N and how many steps down that were
10391 taken."
10392   (interactive "p")
10393   (let ((up (< n 0))
10394         (n (abs n)))
10395     (while (and (> n 0)
10396                 (if up (gnus-summary-go-up-thread)
10397                   (gnus-summary-go-down-thread)))
10398       (setq n (1- n)))
10399     (gnus-summary-position-point)
10400     (when (/= 0 n)
10401       (gnus-message 7 "Can't go further"))
10402     n))
10403
10404 (defun gnus-summary-up-thread (n)
10405   "Go up thread N steps.
10406 If N is negative, go down instead.
10407 Returns the difference between N and how many steps down that were
10408 taken."
10409   (interactive "p")
10410   (gnus-summary-down-thread (- n)))
10411
10412 (defun gnus-summary-top-thread ()
10413   "Go to the top of the thread."
10414   (interactive)
10415   (while (gnus-summary-go-up-thread))
10416   (gnus-summary-article-number))
10417
10418 (defun gnus-summary-kill-thread (&optional unmark)
10419   "Mark articles under current thread as read.
10420 If the prefix argument is positive, remove any kinds of marks.
10421 If the prefix argument is negative, tick articles instead."
10422   (interactive "P")
10423   (when unmark
10424     (setq unmark (prefix-numeric-value unmark)))
10425   (let ((articles (gnus-summary-articles-in-thread)))
10426     (save-excursion
10427       ;; Expand the thread.
10428       (gnus-summary-show-thread)
10429       ;; Mark all the articles.
10430       (while articles
10431         (gnus-summary-goto-subject (car articles))
10432         (cond ((null unmark)
10433                (gnus-summary-mark-article-as-read gnus-killed-mark))
10434               ((> unmark 0)
10435                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10436               (t
10437                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10438         (setq articles (cdr articles))))
10439     ;; Hide killed subtrees.
10440     (and (null unmark)
10441          gnus-thread-hide-killed
10442          (gnus-summary-hide-thread))
10443     ;; If marked as read, go to next unread subject.
10444     (when (null unmark)
10445       ;; Go to next unread subject.
10446       (gnus-summary-next-subject 1 t)))
10447   (gnus-set-mode-line 'summary))
10448
10449 ;; Summary sorting commands
10450
10451 (defun gnus-summary-sort-by-number (&optional reverse)
10452   "Sort the summary buffer by article number.
10453 Argument REVERSE means reverse order."
10454   (interactive "P")
10455   (gnus-summary-sort 'number reverse))
10456
10457 (defun gnus-summary-sort-by-random (&optional reverse)
10458   "Randomize the order in the summary buffer.
10459 Argument REVERSE means to randomize in reverse order."
10460   (interactive "P")
10461   (gnus-summary-sort 'random reverse))
10462
10463 (defun gnus-summary-sort-by-author (&optional reverse)
10464   "Sort the summary buffer by author name alphabetically.
10465 If `case-fold-search' is non-nil, case of letters is ignored.
10466 Argument REVERSE means reverse order."
10467   (interactive "P")
10468   (gnus-summary-sort 'author reverse))
10469
10470 (defun gnus-summary-sort-by-subject (&optional reverse)
10471   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10472 If `case-fold-search' is non-nil, case of letters is ignored.
10473 Argument REVERSE means reverse order."
10474   (interactive "P")
10475   (gnus-summary-sort 'subject reverse))
10476
10477 (defun gnus-summary-sort-by-date (&optional reverse)
10478   "Sort the summary buffer by date.
10479 Argument REVERSE means reverse order."
10480   (interactive "P")
10481   (gnus-summary-sort 'date reverse))
10482
10483 (defun gnus-summary-sort-by-score (&optional reverse)
10484   "Sort the summary buffer by score.
10485 Argument REVERSE means reverse order."
10486   (interactive "P")
10487   (gnus-summary-sort 'score reverse))
10488
10489 (defun gnus-summary-sort-by-lines (&optional reverse)
10490   "Sort the summary buffer by the number of lines.
10491 Argument REVERSE means reverse order."
10492   (interactive "P")
10493   (gnus-summary-sort 'lines reverse))
10494
10495 (defun gnus-summary-sort-by-chars (&optional reverse)
10496   "Sort the summary buffer by article length.
10497 Argument REVERSE means reverse order."
10498   (interactive "P")
10499   (gnus-summary-sort 'chars reverse))
10500
10501 (defun gnus-summary-sort-by-original (&optional reverse)
10502   "Sort the summary buffer using the default sorting method.
10503 Argument REVERSE means reverse order."
10504   (interactive "P")
10505   (let* ((buffer-read-only)
10506          (gnus-summary-prepare-hook nil))
10507     ;; We do the sorting by regenerating the threads.
10508     (gnus-summary-prepare)
10509     ;; Hide subthreads if needed.
10510     (gnus-summary-maybe-hide-threads)))
10511
10512 (defun gnus-summary-sort (predicate reverse)
10513   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10514   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10515          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10516          (gnus-thread-sort-functions
10517           (if (not reverse)
10518               thread
10519             `(lambda (t1 t2)
10520                (,thread t2 t1))))
10521          (gnus-sort-gathered-threads-function
10522           gnus-thread-sort-functions)
10523          (gnus-article-sort-functions
10524           (if (not reverse)
10525               article
10526             `(lambda (t1 t2)
10527                (,article t2 t1))))
10528          (buffer-read-only)
10529          (gnus-summary-prepare-hook nil))
10530     ;; We do the sorting by regenerating the threads.
10531     (gnus-summary-prepare)
10532     ;; Hide subthreads if needed.
10533     (gnus-summary-maybe-hide-threads)))
10534
10535 ;; Summary saving commands.
10536
10537 (defun gnus-summary-save-article (&optional n not-saved)
10538   "Save the current article using the default saver function.
10539 If N is a positive number, save the N next articles.
10540 If N is a negative number, save the N previous articles.
10541 If N is nil and any articles have been marked with the process mark,
10542 save those articles instead.
10543 The variable `gnus-default-article-saver' specifies the saver function."
10544   (interactive "P")
10545   (let* ((articles (gnus-summary-work-articles n))
10546          (save-buffer (save-excursion
10547                         (nnheader-set-temp-buffer " *Gnus Save*")))
10548          (num (length articles))
10549          header file)
10550     (dolist (article articles)
10551       (setq header (gnus-summary-article-header article))
10552       (if (not (vectorp header))
10553           ;; This is a pseudo-article.
10554           (if (assq 'name header)
10555               (gnus-copy-file (cdr (assq 'name header)))
10556             (gnus-message 1 "Article %d is unsaveable" article))
10557         ;; This is a real article.
10558         (save-window-excursion
10559           (let ((gnus-display-mime-function nil)
10560                 (gnus-article-prepare-hook nil))
10561             (gnus-summary-select-article t nil nil article)))
10562         (save-excursion
10563           (set-buffer save-buffer)
10564           (erase-buffer)
10565           (insert-buffer-substring gnus-original-article-buffer))
10566         (setq file (gnus-article-save save-buffer file num))
10567         (gnus-summary-remove-process-mark article)
10568         (unless not-saved
10569           (gnus-summary-set-saved-mark article))))
10570     (gnus-kill-buffer save-buffer)
10571     (gnus-summary-position-point)
10572     (gnus-set-mode-line 'summary)
10573     n))
10574
10575 (defun gnus-summary-pipe-output (&optional arg headers)
10576   "Pipe the current article to a subprocess.
10577 If N is a positive number, pipe the N next articles.
10578 If N is a negative number, pipe the N previous articles.
10579 If N is nil and any articles have been marked with the process mark,
10580 pipe those articles instead.
10581 If HEADERS (the symbolic prefix), include the headers, too."
10582   (interactive (gnus-interactive "P\ny"))
10583   (require 'gnus-art)
10584   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10585         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10586     (gnus-summary-save-article arg t))
10587   (let ((buffer (get-buffer "*Shell Command Output*")))
10588     (when (and buffer
10589                (not (zerop (buffer-size buffer))))
10590       (gnus-configure-windows 'pipe))))
10591
10592 (defun gnus-summary-save-article-mail (&optional arg)
10593   "Append the current article to an mail file.
10594 If N is a positive number, save the N next articles.
10595 If N is a negative number, save the N previous articles.
10596 If N is nil and any articles have been marked with the process mark,
10597 save those articles instead."
10598   (interactive "P")
10599   (require 'gnus-art)
10600   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10601     (gnus-summary-save-article arg)))
10602
10603 (defun gnus-summary-save-article-rmail (&optional arg)
10604   "Append the current article to an rmail file.
10605 If N is a positive number, save the N next articles.
10606 If N is a negative number, save the N previous articles.
10607 If N is nil and any articles have been marked with the process mark,
10608 save those articles instead."
10609   (interactive "P")
10610   (require 'gnus-art)
10611   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10612     (gnus-summary-save-article arg)))
10613
10614 (defun gnus-summary-save-article-file (&optional arg)
10615   "Append the current article to a file.
10616 If N is a positive number, save the N next articles.
10617 If N is a negative number, save the N previous articles.
10618 If N is nil and any articles have been marked with the process mark,
10619 save those articles instead."
10620   (interactive "P")
10621   (require 'gnus-art)
10622   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10623     (gnus-summary-save-article arg)))
10624
10625 (defun gnus-summary-write-article-file (&optional arg)
10626   "Write the current article to a file, deleting the previous file.
10627 If N is a positive number, save the N next articles.
10628 If N is a negative number, save the N previous articles.
10629 If N is nil and any articles have been marked with the process mark,
10630 save those articles instead."
10631   (interactive "P")
10632   (require 'gnus-art)
10633   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10634     (gnus-summary-save-article arg)))
10635
10636 (defun gnus-summary-save-article-body-file (&optional arg)
10637   "Append the current article body to a file.
10638 If N is a positive number, save the N next articles.
10639 If N is a negative number, save the N previous articles.
10640 If N is nil and any articles have been marked with the process mark,
10641 save those articles instead."
10642   (interactive "P")
10643   (require 'gnus-art)
10644   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10645     (gnus-summary-save-article arg)))
10646
10647 (defun gnus-summary-muttprint (&optional arg)
10648   "Print the current article using Muttprint.
10649 If N is a positive number, save the N next articles.
10650 If N is a negative number, save the N previous articles.
10651 If N is nil and any articles have been marked with the process mark,
10652 save those articles instead."
10653   (interactive "P")
10654   (require 'gnus-art)
10655   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10656     (gnus-summary-save-article arg t)))
10657
10658 (defun gnus-summary-pipe-message (program)
10659   "Pipe the current article through PROGRAM."
10660   (interactive "sProgram: ")
10661   (gnus-summary-select-article)
10662   (let ((mail-header-separator ""))
10663     (gnus-eval-in-buffer-window gnus-article-buffer
10664       (save-restriction
10665         (widen)
10666         (let ((start (window-start))
10667               buffer-read-only)
10668           (message-pipe-buffer-body program)
10669           (set-window-start (get-buffer-window (current-buffer)) start))))))
10670
10671 (defun gnus-get-split-value (methods)
10672   "Return a value based on the split METHODS."
10673   (let (split-name method result match)
10674     (when methods
10675       (save-excursion
10676         (set-buffer gnus-original-article-buffer)
10677         (save-restriction
10678           (nnheader-narrow-to-headers)
10679           (while (and methods (not split-name))
10680             (goto-char (point-min))
10681             (setq method (pop methods))
10682             (setq match (car method))
10683             (when (cond
10684                    ((stringp match)
10685                     ;; Regular expression.
10686                     (ignore-errors
10687                       (re-search-forward match nil t)))
10688                    ((gnus-functionp match)
10689                     ;; Function.
10690                     (save-restriction
10691                       (widen)
10692                       (setq result (funcall match gnus-newsgroup-name))))
10693                    ((consp match)
10694                     ;; Form.
10695                     (save-restriction
10696                       (widen)
10697                       (setq result (eval match)))))
10698               (setq split-name (cdr method))
10699               (cond ((stringp result)
10700                      (push (expand-file-name
10701                             result gnus-article-save-directory)
10702                            split-name))
10703                     ((consp result)
10704                      (setq split-name (append result split-name)))))))))
10705     (nreverse split-name)))
10706
10707 (defun gnus-valid-move-group-p (group)
10708   (and (boundp group)
10709        (symbol-name group)
10710        (symbol-value group)
10711        (gnus-get-function (gnus-find-method-for-group
10712                            (symbol-name group)) 'request-accept-article t)))
10713
10714 (defun gnus-read-move-group-name (prompt default articles prefix)
10715   "Read a group name."
10716   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10717          (minibuffer-confirm-incomplete nil) ; XEmacs
10718          (prom
10719           (format "%s %s to:"
10720                   prompt
10721                   (if (> (length articles) 1)
10722                       (format "these %d articles" (length articles))
10723                     "this article")))
10724          (to-newsgroup
10725           (cond
10726            ((null split-name)
10727             (gnus-completing-read-with-default
10728              default prom
10729              gnus-active-hashtb
10730              'gnus-valid-move-group-p
10731              nil prefix
10732              'gnus-group-history))
10733            ((= 1 (length split-name))
10734             (gnus-completing-read-with-default
10735              (car split-name) prom
10736              gnus-active-hashtb
10737              'gnus-valid-move-group-p
10738              nil nil
10739              'gnus-group-history))
10740            (t
10741             (gnus-completing-read-with-default
10742              nil prom
10743              (mapcar (lambda (el) (list el))
10744                      (nreverse split-name))
10745              nil nil nil
10746              'gnus-group-history))))
10747          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10748     (when to-newsgroup
10749       (if (or (string= to-newsgroup "")
10750               (string= to-newsgroup prefix))
10751           (setq to-newsgroup default))
10752       (unless to-newsgroup
10753         (error "No group name entered"))
10754       (or (gnus-active to-newsgroup)
10755           (gnus-activate-group to-newsgroup nil nil to-method)
10756           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10757                                      to-newsgroup))
10758               (or (and (gnus-request-create-group to-newsgroup to-method)
10759                        (gnus-activate-group
10760                         to-newsgroup nil nil to-method)
10761                        (gnus-subscribe-group to-newsgroup))
10762                   (error "Couldn't create group %s" to-newsgroup)))
10763           (error "No such group: %s" to-newsgroup)))
10764     to-newsgroup))
10765
10766 (defun gnus-summary-save-parts (type dir n &optional reverse)
10767   "Save parts matching TYPE to DIR.
10768 If REVERSE, save parts that do not match TYPE."
10769   (interactive
10770    (list (read-string "Save parts of type: "
10771                       (or (car gnus-summary-save-parts-type-history)
10772                           gnus-summary-save-parts-default-mime)
10773                       'gnus-summary-save-parts-type-history)
10774          (setq gnus-summary-save-parts-last-directory
10775                (read-file-name "Save to directory: "
10776                                gnus-summary-save-parts-last-directory
10777                                nil t))
10778          current-prefix-arg))
10779   (gnus-summary-iterate n
10780     (let ((gnus-display-mime-function nil)
10781           (gnus-inhibit-treatment t))
10782       (gnus-summary-select-article))
10783     (save-excursion
10784       (set-buffer gnus-article-buffer)
10785       (let ((handles (or gnus-article-mime-handles
10786                          (mm-dissect-buffer nil gnus-article-loose-mime)
10787                          (and gnus-article-emulate-mime
10788                               (mm-uu-dissect)))))
10789         (when handles
10790           (gnus-summary-save-parts-1 type dir handles reverse)
10791           (unless gnus-article-mime-handles ;; Don't destroy this case.
10792             (mm-destroy-parts handles)))))))
10793
10794 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10795   (if (stringp (car handle))
10796       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10797               (cdr handle))
10798     (when (if reverse
10799               (not (string-match type (mm-handle-media-type handle)))
10800             (string-match type (mm-handle-media-type handle)))
10801       (let ((file (expand-file-name
10802                    (file-name-nondirectory
10803                     (or
10804                      (mail-content-type-get
10805                       (mm-handle-disposition handle) 'filename)
10806                      (concat gnus-newsgroup-name
10807                              "." (number-to-string
10808                                   (cdr gnus-article-current)))))
10809                    dir)))
10810         (unless (file-exists-p file)
10811           (mm-save-part-to-file handle file))))))
10812
10813 ;; Summary extract commands
10814
10815 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10816   (let ((buffer-read-only nil)
10817         (article (gnus-summary-article-number))
10818         after-article b e)
10819     (unless (gnus-summary-goto-subject article)
10820       (error "No such article: %d" article))
10821     (gnus-summary-position-point)
10822     ;; If all commands are to be bunched up on one line, we collect
10823     ;; them here.
10824     (unless gnus-view-pseudos-separately
10825       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10826             files action)
10827         (while ps
10828           (setq action (cdr (assq 'action (car ps))))
10829           (setq files (list (cdr (assq 'name (car ps)))))
10830           (while (and ps (cdr ps)
10831                       (string= (or action "1")
10832                                (or (cdr (assq 'action (cadr ps))) "2")))
10833             (push (cdr (assq 'name (cadr ps))) files)
10834             (setcdr ps (cddr ps)))
10835           (when files
10836             (when (not (string-match "%s" action))
10837               (push " " files))
10838             (push " " files)
10839             (when (assq 'execute (car ps))
10840               (setcdr (assq 'execute (car ps))
10841                       (funcall (if (string-match "%s" action)
10842                                    'format 'concat)
10843                                action
10844                                (mapconcat
10845                                 (lambda (f)
10846                                   (if (equal f " ")
10847                                       f
10848                                     (mm-quote-arg f)))
10849                                 files " ")))))
10850           (setq ps (cdr ps)))))
10851     (if (and gnus-view-pseudos (not not-view))
10852         (while pslist
10853           (when (assq 'execute (car pslist))
10854             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10855                                   (eq gnus-view-pseudos 'not-confirm)))
10856           (setq pslist (cdr pslist)))
10857       (save-excursion
10858         (while pslist
10859           (setq after-article (or (cdr (assq 'article (car pslist)))
10860                                   (gnus-summary-article-number)))
10861           (gnus-summary-goto-subject after-article)
10862           (forward-line 1)
10863           (setq b (point))
10864           (insert "    " (file-name-nondirectory
10865                           (cdr (assq 'name (car pslist))))
10866                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10867           (setq e (point))
10868           (forward-line -1)             ; back to `b'
10869           (gnus-add-text-properties
10870            b (1- e) (list 'gnus-number gnus-reffed-article-number
10871                           gnus-mouse-face-prop gnus-mouse-face))
10872           (gnus-data-enter
10873            after-article gnus-reffed-article-number
10874            gnus-unread-mark b (car pslist) 0 (- e b))
10875           (setq gnus-newsgroup-unreads
10876                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10877                                          gnus-reffed-article-number))
10878           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10879           (setq pslist (cdr pslist)))))))
10880
10881 (defun gnus-pseudos< (p1 p2)
10882   (let ((c1 (cdr (assq 'action p1)))
10883         (c2 (cdr (assq 'action p2))))
10884     (and c1 c2 (string< c1 c2))))
10885
10886 (defun gnus-request-pseudo-article (props)
10887   (cond ((assq 'execute props)
10888          (gnus-execute-command (cdr (assq 'execute props)))))
10889   (let ((gnus-current-article (gnus-summary-article-number)))
10890     (gnus-run-hooks 'gnus-mark-article-hook)))
10891
10892 (defun gnus-execute-command (command &optional automatic)
10893   (save-excursion
10894     (gnus-article-setup-buffer)
10895     (set-buffer gnus-article-buffer)
10896     (setq buffer-read-only nil)
10897     (let ((command (if automatic command
10898                      (read-string "Command: " (cons command 0)))))
10899       (erase-buffer)
10900       (insert "$ " command "\n\n")
10901       (if gnus-view-pseudo-asynchronously
10902           (start-process "gnus-execute" (current-buffer) shell-file-name
10903                          shell-command-switch command)
10904         (call-process shell-file-name nil t nil
10905                       shell-command-switch command)))))
10906
10907 ;; Summary kill commands.
10908
10909 (defun gnus-summary-edit-global-kill (article)
10910   "Edit the \"global\" kill file."
10911   (interactive (list (gnus-summary-article-number)))
10912   (gnus-group-edit-global-kill article))
10913
10914 (defun gnus-summary-edit-local-kill ()
10915   "Edit a local kill file applied to the current newsgroup."
10916   (interactive)
10917   (setq gnus-current-headers (gnus-summary-article-header))
10918   (gnus-group-edit-local-kill
10919    (gnus-summary-article-number) gnus-newsgroup-name))
10920
10921 ;;; Header reading.
10922
10923 (defun gnus-read-header (id &optional header)
10924   "Read the headers of article ID and enter them into the Gnus system."
10925   (let ((group gnus-newsgroup-name)
10926         (gnus-override-method
10927          (or
10928           gnus-override-method
10929           (and (gnus-news-group-p gnus-newsgroup-name)
10930                (car (gnus-refer-article-methods)))))
10931         where)
10932     ;; First we check to see whether the header in question is already
10933     ;; fetched.
10934     (if (stringp id)
10935         ;; This is a Message-ID.
10936         (setq header (or header (gnus-id-to-header id)))
10937       ;; This is an article number.
10938       (setq header (or header (gnus-summary-article-header id))))
10939     (if (and header
10940              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10941         ;; We have found the header.
10942         header
10943       ;; If this is a sparse article, we have to nix out its
10944       ;; previous entry in the thread hashtb.
10945       (when (and header
10946                  (gnus-summary-article-sparse-p (mail-header-number header)))
10947         (let* ((parent (gnus-parent-id (mail-header-references header)))
10948                (thread (and parent (gnus-id-to-thread parent))))
10949           (when thread
10950             (delq (assq header thread) thread))))
10951       ;; We have to really fetch the header to this article.
10952       (save-excursion
10953         (set-buffer nntp-server-buffer)
10954         (when (setq where (gnus-request-head id group))
10955           (nnheader-fold-continuation-lines)
10956           (goto-char (point-max))
10957           (insert ".\n")
10958           (goto-char (point-min))
10959           (insert "211 ")
10960           (princ (cond
10961                   ((numberp id) id)
10962                   ((cdr where) (cdr where))
10963                   (header (mail-header-number header))
10964                   (t gnus-reffed-article-number))
10965                  (current-buffer))
10966           (insert " Article retrieved.\n"))
10967         (if (or (not where)
10968                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10969             ()                          ; Malformed head.
10970           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10971             (when (and (stringp id)
10972                        (not (string= (gnus-group-real-name group)
10973                                      (car where))))
10974               ;; If we fetched by Message-ID and the article came
10975               ;; from a different group, we fudge some bogus article
10976               ;; numbers for this article.
10977               (mail-header-set-number header gnus-reffed-article-number))
10978             (save-excursion
10979               (set-buffer gnus-summary-buffer)
10980               (decf gnus-reffed-article-number)
10981               (gnus-remove-header (mail-header-number header))
10982               (push header gnus-newsgroup-headers)
10983               (setq gnus-current-headers header)
10984               (push (mail-header-number header) gnus-newsgroup-limit)))
10985           header)))))
10986
10987 (defun gnus-remove-header (number)
10988   "Remove header NUMBER from `gnus-newsgroup-headers'."
10989   (if (and gnus-newsgroup-headers
10990            (= number (mail-header-number (car gnus-newsgroup-headers))))
10991       (pop gnus-newsgroup-headers)
10992     (let ((headers gnus-newsgroup-headers))
10993       (while (and (cdr headers)
10994                   (not (= number (mail-header-number (cadr headers)))))
10995         (pop headers))
10996       (when (cdr headers)
10997         (setcdr headers (cddr headers))))))
10998
10999 ;;;
11000 ;;; summary highlights
11001 ;;;
11002
11003 (defun gnus-highlight-selected-summary ()
11004   "Highlight selected article in summary buffer."
11005   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11006   (when gnus-summary-selected-face
11007     (save-excursion
11008       (let* ((beg (progn (beginning-of-line) (point)))
11009              (end (progn (end-of-line) (point)))
11010              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11011              (from (if (get-text-property beg gnus-mouse-face-prop)
11012                        beg
11013                      (or (next-single-property-change
11014                           beg gnus-mouse-face-prop nil end)
11015                          beg)))
11016              (to
11017               (if (= from end)
11018                   (- from 2)
11019                 (or (next-single-property-change
11020                      from gnus-mouse-face-prop nil end)
11021                     end))))
11022         ;; If no mouse-face prop on line we will have to = from = end,
11023         ;; so we highlight the entire line instead.
11024         (when (= (+ to 2) from)
11025           (setq from beg)
11026           (setq to end))
11027         (if gnus-newsgroup-selected-overlay
11028             ;; Move old overlay.
11029             (gnus-move-overlay
11030              gnus-newsgroup-selected-overlay from to (current-buffer))
11031           ;; Create new overlay.
11032           (gnus-overlay-put
11033            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11034            'face gnus-summary-selected-face))))))
11035
11036 (defvar gnus-summary-highlight-line-cached nil)
11037 (defvar gnus-summary-highlight-line-trigger nil)
11038
11039 (defun gnus-summary-highlight-line-0 ()
11040   (if (and (eq gnus-summary-highlight-line-trigger 
11041                gnus-summary-highlight)
11042            gnus-summary-highlight-line-cached)
11043       gnus-summary-highlight-line-cached
11044     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11045           gnus-summary-highlight-line-cached
11046           (let* ((cond (list 'cond))
11047                  (c cond)
11048                  (list gnus-summary-highlight))
11049             (while list
11050               (setcdr c (cons (list (caar list) (list 'quote (cdar list))) nil))
11051               (setq c (cdr c)
11052                     list (cdr list)))
11053             (gnus-byte-compile (list 'lambda nil cond))))))
11054
11055 (defun gnus-summary-highlight-line ()
11056   "Highlight current line according to `gnus-summary-highlight'."
11057   (let* ((beg (gnus-point-at-bol))
11058          (article (or (gnus-summary-article-number) gnus-current-article))
11059          (score (or (cdr (assq article
11060                                gnus-newsgroup-scored))
11061                     gnus-summary-default-score 0))
11062          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11063          (inhibit-read-only t)
11064          (default gnus-summary-default-score)
11065          (default-high gnus-summary-default-high-score)
11066          (default-low gnus-summary-default-low-score)
11067          (uncached (memq article gnus-newsgroup-undownloaded))
11068          (downloaded (not uncached)))
11069     (let ((face (funcall (gnus-summary-highlight-line-0))))
11070       (unless (eq face (get-text-property beg 'face))
11071         (gnus-put-text-property-excluding-characters-with-faces
11072          beg (gnus-point-at-eol) 'face
11073          (setq face (if (boundp face) (symbol-value face) face)))
11074         (when gnus-summary-highlight-line-function
11075           (funcall gnus-summary-highlight-line-function article face))))))
11076
11077 (defun gnus-update-read-articles (group unread &optional compute)
11078   "Update the list of read articles in GROUP.
11079 UNREAD is a sorted list."
11080   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11081          (entry (gnus-gethash group gnus-newsrc-hashtb))
11082          (info (nth 2 entry))
11083          (prev 1)
11084          read)
11085     (if (or (not info) (not active))
11086         ;; There is no info on this group if it was, in fact,
11087         ;; killed.  Gnus stores no information on killed groups, so
11088         ;; there's nothing to be done.
11089         ;; One could store the information somewhere temporarily,
11090         ;; perhaps...  Hmmm...
11091         ()
11092       ;; Remove any negative articles numbers.
11093       (while (and unread (< (car unread) 0))
11094         (setq unread (cdr unread)))
11095       ;; Remove any expired article numbers
11096       (while (and unread (< (car unread) (car active)))
11097         (setq unread (cdr unread)))
11098       ;; Compute the ranges of read articles by looking at the list of
11099       ;; unread articles.
11100       (while unread
11101         (when (/= (car unread) prev)
11102           (push (if (= prev (1- (car unread))) prev
11103                   (cons prev (1- (car unread))))
11104                 read))
11105         (setq prev (1+ (car unread)))
11106         (setq unread (cdr unread)))
11107       (when (<= prev (cdr active))
11108         (push (cons prev (cdr active)) read))
11109       (setq read (if (> (length read) 1) (nreverse read) read))
11110       (if compute
11111           read
11112         (save-excursion
11113           (let (setmarkundo)
11114             ;; Propagate the read marks to the backend.
11115             (when (gnus-check-backend-function 'request-set-mark group)
11116               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11117                     (add (gnus-remove-from-range read (gnus-info-read info))))
11118                 (when (or add del)
11119                   (unless (gnus-check-group group)
11120                     (error "Can't open server for %s" group))
11121                   (gnus-request-set-mark
11122                    group (delq nil (list (if add (list add 'add '(read)))
11123                                          (if del (list del 'del '(read))))))
11124                   (setq setmarkundo
11125                         `(gnus-request-set-mark
11126                           ,group
11127                           ',(delq nil (list
11128                                        (if del (list del 'add '(read)))
11129                                        (if add (list add 'del '(read))))))))))
11130             (set-buffer gnus-group-buffer)
11131             (gnus-undo-register
11132               `(progn
11133                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11134                  (gnus-info-set-read ',info ',(gnus-info-read info))
11135                  (gnus-get-unread-articles-in-group ',info
11136                                                     (gnus-active ,group))
11137                  (gnus-group-update-group ,group t)
11138                  ,setmarkundo))))
11139         ;; Enter this list into the group info.
11140         (gnus-info-set-read info read)
11141         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11142         (gnus-get-unread-articles-in-group info (gnus-active group))
11143         t))))
11144
11145 (defun gnus-offer-save-summaries ()
11146   "Offer to save all active summary buffers."
11147   (let (buffers)
11148     ;; Go through all buffers and find all summaries.
11149     (dolist (buffer (buffer-list))
11150       (when (and (setq buffer (buffer-name buffer))
11151                  (string-match "Summary" buffer)
11152                  (save-excursion
11153                    (set-buffer buffer)
11154                    ;; We check that this is, indeed, a summary buffer.
11155                    (and (eq major-mode 'gnus-summary-mode)
11156                         ;; Also make sure this isn't bogus.
11157                         gnus-newsgroup-prepared
11158                         ;; Also make sure that this isn't a
11159                         ;; dead summary buffer.
11160                         (not gnus-dead-summary-mode))))
11161         (push buffer buffers)))
11162     ;; Go through all these summary buffers and offer to save them.
11163     (when buffers
11164       (save-excursion
11165         (map-y-or-n-p
11166          "Update summary buffer %s? "
11167          (lambda (buf)
11168            (switch-to-buffer buf)
11169            (gnus-summary-exit))
11170          buffers)))))
11171
11172 (defun gnus-summary-setup-default-charset ()
11173   "Setup newsgroup default charset."
11174   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11175       (setq gnus-newsgroup-charset nil)
11176     (let* ((ignored-charsets
11177             (or gnus-newsgroup-ephemeral-ignored-charsets
11178                 (append
11179                  (and gnus-newsgroup-name
11180                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11181                  gnus-newsgroup-ignored-charsets))))
11182       (setq gnus-newsgroup-charset
11183             (or gnus-newsgroup-ephemeral-charset
11184                 (and gnus-newsgroup-name
11185                      (gnus-parameter-charset gnus-newsgroup-name))
11186                 gnus-default-charset))
11187       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11188            ignored-charsets))))
11189
11190 ;;;
11191 ;;; Mime Commands
11192 ;;;
11193
11194 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11195   "Display the current article buffer fully MIME-buttonized.
11196 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11197 treated as multipart/mixed."
11198   (interactive "P")
11199   (require 'gnus-art)
11200   (let ((gnus-unbuttonized-mime-types nil)
11201         (gnus-mime-display-multipart-as-mixed show-all-parts))
11202     (gnus-summary-show-article)))
11203
11204 (defun gnus-summary-repair-multipart (article)
11205   "Add a Content-Type header to a multipart article without one."
11206   (interactive (list (gnus-summary-article-number)))
11207   (gnus-with-article article
11208     (message-narrow-to-head)
11209     (message-remove-header "Mime-Version")
11210     (goto-char (point-max))
11211     (insert "Mime-Version: 1.0\n")
11212     (widen)
11213     (when (search-forward "\n--" nil t)
11214       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11215         (message-narrow-to-head)
11216         (message-remove-header "Content-Type")
11217         (goto-char (point-max))
11218         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11219                         separator))
11220         (widen))))
11221   (let (gnus-mark-article-hook)
11222     (gnus-summary-select-article t t nil article)))
11223
11224 (defun gnus-summary-toggle-display-buttonized ()
11225   "Toggle the buttonizing of the article buffer."
11226   (interactive)
11227   (require 'gnus-art)
11228   (if (setq gnus-inhibit-mime-unbuttonizing
11229             (not gnus-inhibit-mime-unbuttonizing))
11230       (let ((gnus-unbuttonized-mime-types nil))
11231         (gnus-summary-show-article))
11232     (gnus-summary-show-article)))
11233
11234 ;;;
11235 ;;; Generic summary marking commands
11236 ;;;
11237
11238 (defvar gnus-summary-marking-alist
11239   '((read gnus-del-mark "d")
11240     (unread gnus-unread-mark "u")
11241     (ticked gnus-ticked-mark "!")
11242     (dormant gnus-dormant-mark "?")
11243     (expirable gnus-expirable-mark "e"))
11244   "An alist of names/marks/keystrokes.")
11245
11246 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11247 (defvar gnus-summary-mark-map)
11248
11249 (defun gnus-summary-make-all-marking-commands ()
11250   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11251   (dolist (elem gnus-summary-marking-alist)
11252     (apply 'gnus-summary-make-marking-command elem)))
11253
11254 (defun gnus-summary-make-marking-command (name mark keystroke)
11255   (let ((map (make-sparse-keymap)))
11256     (define-key gnus-summary-generic-mark-map keystroke map)
11257     (dolist (lway `((next "next" next nil "n")
11258                     (next-unread "next unread" next t "N")
11259                     (prev "previous" prev nil "p")
11260                     (prev-unread "previous unread" prev t "P")
11261                     (nomove "" nil nil ,keystroke)))
11262       (let ((func (gnus-summary-make-marking-command-1
11263                    mark (car lway) lway name)))
11264         (setq func (eval func))
11265         (define-key map (nth 4 lway) func)))))
11266
11267 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11268   `(defun ,(intern
11269             (format "gnus-summary-put-mark-as-%s%s"
11270                     name (if (eq way 'nomove)
11271                              ""
11272                            (concat "-" (symbol-name way)))))
11273      (n)
11274      ,(format
11275        "Mark the current article as %s%s.
11276 If N, the prefix, then repeat N times.
11277 If N is negative, move in reverse order.
11278 The difference between N and the actual number of articles marked is
11279 returned."
11280        name (cadr lway))
11281      (interactive "p")
11282      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11283
11284 (defun gnus-summary-generic-mark (n mark move unread)
11285   "Mark N articles with MARK."
11286   (unless (eq major-mode 'gnus-summary-mode)
11287     (error "This command can only be used in the summary buffer"))
11288   (gnus-summary-show-thread)
11289   (let ((nummove
11290          (cond
11291           ((eq move 'next) 1)
11292           ((eq move 'prev) -1)
11293           (t 0))))
11294     (if (zerop nummove)
11295         (setq n 1)
11296       (when (< n 0)
11297         (setq n (abs n)
11298               nummove (* -1 nummove))))
11299     (while (and (> n 0)
11300                 (gnus-summary-mark-article nil mark)
11301                 (zerop (gnus-summary-next-subject nummove unread t)))
11302       (setq n (1- n)))
11303     (when (/= 0 n)
11304       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11305     (gnus-summary-recenter)
11306     (gnus-summary-position-point)
11307     (gnus-set-mode-line 'summary)
11308     n))
11309
11310 (defun gnus-summary-insert-articles (articles)
11311   (when (setq articles
11312               (gnus-sorted-difference articles
11313                                       (mapcar (lambda (h)
11314                                                 (mail-header-number h))
11315                                               gnus-newsgroup-headers)))
11316     (setq gnus-newsgroup-headers
11317           (merge 'list
11318                  gnus-newsgroup-headers
11319                  (gnus-fetch-headers articles)
11320                  'gnus-article-sort-by-number))
11321     ;; Suppress duplicates?
11322     (when gnus-suppress-duplicates
11323       (gnus-dup-suppress-articles))
11324
11325     ;; We might want to build some more threads first.
11326     (when (and gnus-fetch-old-headers
11327                (eq gnus-headers-retrieved-by 'nov))
11328       (if (eq gnus-fetch-old-headers 'invisible)
11329           (gnus-build-all-threads)
11330         (gnus-build-old-threads)))
11331     ;; Let the Gnus agent mark articles as read.
11332     (when gnus-agent
11333       (gnus-agent-get-undownloaded-list))
11334     ;; Remove list identifiers from subject
11335     (when gnus-list-identifiers
11336       (gnus-summary-remove-list-identifiers))
11337     ;; First and last article in this newsgroup.
11338     (when gnus-newsgroup-headers
11339       (setq gnus-newsgroup-begin
11340             (mail-header-number (car gnus-newsgroup-headers))
11341             gnus-newsgroup-end
11342             (mail-header-number
11343              (gnus-last-element gnus-newsgroup-headers))))
11344     (when gnus-use-scoring
11345       (gnus-possibly-score-headers))))
11346
11347 (defun gnus-summary-insert-old-articles (&optional all)
11348   "Insert all old articles in this group.
11349 If ALL is non-nil, already read articles become readable.
11350 If ALL is a number, fetch this number of articles."
11351   (interactive "P")
11352   (prog1
11353       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11354             older len)
11355         (setq older
11356               ;; Some nntp servers lie about their active range.  When
11357               ;; this happens, the active range can be in the millions.
11358               ;; Use a compressed range to avoid creating a huge list.
11359               (gnus-range-difference (list gnus-newsgroup-active) old))
11360         (setq len (gnus-range-length older))
11361         (cond
11362          ((null older) nil)
11363          ((numberp all)
11364           (if (< all len)
11365               (let ((older-range (nreverse older)))
11366                 (setq older nil)
11367
11368                 (while (> all 0)
11369                   (let* ((r (pop older-range))
11370                          (min (if (numberp r) r (car r)))
11371                          (max (if (numberp r) r (cdr r))))
11372                     (while (and (<= min max)
11373                                 (> all 0))
11374                       (push max older)
11375                       (setq all (1- all)
11376                             max (1- max))))))
11377             (setq older (gnus-uncompress-range older))))
11378          (all
11379           (setq older (gnus-uncompress-range older)))
11380          (t
11381           (when (and (numberp gnus-large-newsgroup)
11382                    (> len gnus-large-newsgroup))
11383               (let* ((cursor-in-echo-area nil)
11384                      (initial (gnus-parameter-large-newsgroup-initial
11385                                gnus-newsgroup-name))
11386                      (input
11387                       (read-string
11388                        (format
11389                         "How many articles from %s (%s %d): "
11390                         (gnus-limit-string
11391                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11392                         (if initial "max" "default")
11393                         len)
11394                        (if initial
11395                            (cons (number-to-string initial)
11396                                  0)))))
11397                 (unless (string-match "^[ \t]*$" input)
11398                   (setq all (string-to-number input))
11399                   (if (< all len)
11400                       (let ((older-range (nreverse older)))
11401                         (setq older nil)
11402
11403                         (while (> all 0)
11404                           (let* ((r (pop older-range))
11405                                  (min (if (numberp r) r (car r)))
11406                                  (max (if (numberp r) r (cdr r))))
11407                             (while (and (<= min max)
11408                                         (> all 0))
11409                               (push max older)
11410                               (setq all (1- all)
11411                                     max (1- max))))))))))
11412           (setq older (gnus-uncompress-range older))))
11413         (if (not older)
11414             (message "No old news.")
11415           (gnus-summary-insert-articles older)
11416           (gnus-summary-limit (gnus-sorted-nunion old older))))
11417     (gnus-summary-position-point)))
11418
11419 (defun gnus-summary-insert-new-articles ()
11420   "Insert all new articles in this group."
11421   (interactive)
11422   (prog1
11423       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11424             (old-active gnus-newsgroup-active)
11425             (nnmail-fetched-sources (list t))
11426             i new)
11427         (setq gnus-newsgroup-active
11428               (gnus-activate-group gnus-newsgroup-name 'scan))
11429         (setq i (cdr gnus-newsgroup-active))
11430         (while (> i (cdr old-active))
11431           (push i new)
11432           (decf i))
11433         (if (not new)
11434             (message "No gnus is bad news.")
11435           (gnus-summary-insert-articles new)
11436           (setq gnus-newsgroup-unreads
11437                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11438           (gnus-summary-limit (gnus-sorted-nunion old new))))
11439     (gnus-summary-position-point)))
11440
11441 (gnus-summary-make-all-marking-commands)
11442
11443 (gnus-ems-redefine)
11444
11445 (provide 'gnus-sum)
11446
11447 (run-hooks 'gnus-sum-load-hook)
11448
11449 ;;; gnus-sum.el ends here