2003-02-12 Michael Shields <shields@msrl.com>
[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 ?$
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-article-move-hook nil
851   "*A hook called after an article is moved, copied, respooled, or crossposted."
852   :group 'gnus-summary
853   :type 'hook)
854
855 (defcustom gnus-summary-article-delete-hook nil
856   "*A hook called after an article is deleted."
857   :group 'gnus-summary
858   :type 'hook)
859
860 (defcustom gnus-summary-article-expire-hook nil
861   "*A hook called after an article is expired."
862   :group 'gnus-summary
863   :type 'hook)
864
865 (defcustom gnus-summary-display-arrow
866   (and (fboundp 'display-graphic-p)
867        (display-graphic-p))
868   "*If non-nil, display an arrow highlighting the current article."
869   :version "21.1"
870   :group 'gnus-summary
871   :type 'boolean)
872
873 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
874   "Face used for highlighting the current article in the summary buffer."
875   :group 'gnus-summary-visual
876   :type 'face)
877
878 (defvar gnus-tmp-downloaded nil)
879
880 (defcustom gnus-summary-highlight
881   '(((eq mark gnus-canceled-mark)
882      . gnus-summary-cancelled-face)
883     ((and uncached (> score default-high))
884      . gnus-summary-high-undownloaded-face)
885     ((and uncached (< score default-low))
886      . gnus-summary-low-undownloaded-face)
887     (uncached
888      . gnus-summary-normal-undownloaded-face)
889     ((and (> score default-high)
890           (or (eq mark gnus-dormant-mark)
891               (eq mark gnus-ticked-mark)))
892      . gnus-summary-high-ticked-face)
893     ((and (< score default-low)
894           (or (eq mark gnus-dormant-mark)
895               (eq mark gnus-ticked-mark)))
896      . gnus-summary-low-ticked-face)
897     ((or (eq mark gnus-dormant-mark)
898          (eq mark gnus-ticked-mark))
899      . gnus-summary-normal-ticked-face)
900     ((and (> score default-high) (eq mark gnus-ancient-mark))
901      . gnus-summary-high-ancient-face)
902     ((and (< score default-low) (eq mark gnus-ancient-mark))
903      . gnus-summary-low-ancient-face)
904     ((eq mark gnus-ancient-mark)
905      . gnus-summary-normal-ancient-face)
906     ((and (> score default-high) (eq mark gnus-unread-mark))
907      . gnus-summary-high-unread-face)
908     ((and (< score default-low) (eq mark gnus-unread-mark))
909      . gnus-summary-low-unread-face)
910     ((eq mark gnus-unread-mark)
911      . gnus-summary-normal-unread-face)
912     ((> score default-high)
913      . gnus-summary-high-read-face)
914     ((< score default-low)
915      . gnus-summary-low-read-face)
916     (t
917      . gnus-summary-normal-read-face))
918   "*Controls the highlighting of summary buffer lines.
919
920 A list of (FORM . FACE) pairs.  When deciding how a a particular
921 summary line should be displayed, each form is evaluated.  The content
922 of the face field after the first true form is used.  You can change
923 how those summary lines are displayed, by editing the face field.
924
925 You can use the following variables in the FORM field.
926
927 score:        The article's score
928 default:      The default article score.
929 default-high: The default score for high scored articles.
930 default-low:  The default score for low scored articles.
931 below:        The score below which articles are automatically marked as read.
932 mark:         The articles mark."
933   :group 'gnus-summary-visual
934   :type '(repeat (cons (sexp :tag "Form" nil)
935                        face)))
936
937 (defcustom gnus-alter-header-function nil
938   "Function called to allow alteration of article header structures.
939 The function is called with one parameter, the article header vector,
940 which it may alter in any way."
941   :type '(choice (const :tag "None" nil)
942                  function)
943   :group 'gnus-summary)
944
945 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
946   "Variable that says which function should be used to decode a string with encoded words.")
947
948 (defcustom gnus-extra-headers '(To Newsgroups)
949   "*Extra headers to parse."
950   :version "21.1"
951   :group 'gnus-summary
952   :type '(repeat symbol))
953
954 (defcustom gnus-ignored-from-addresses
955   (and user-mail-address (regexp-quote user-mail-address))
956   "*Regexp of From headers that may be suppressed in favor of To headers."
957   :version "21.1"
958   :group 'gnus-summary
959   :type 'regexp)
960
961 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
962   "List of charsets that should be ignored.
963 When these charsets are used in the \"charset\" parameter, the
964 default charset will be used instead."
965   :version "21.1"
966   :type '(repeat symbol)
967   :group 'gnus-charset)
968
969 (gnus-define-group-parameter
970  ignored-charsets
971  :type list
972  :function-document
973  "Return the ignored charsets of GROUP."
974  :variable gnus-group-ignored-charsets-alist
975  :variable-default
976  '(("alt\\.chinese\\.text" iso-8859-1))
977  :variable-document
978  "Alist of regexps (to match group names) and charsets that should be ignored.
979 When these charsets are used in the \"charset\" parameter, the
980 default charset will be used instead."
981  :variable-group gnus-charset
982  :variable-type '(repeat (cons (regexp :tag "Group")
983                                (repeat symbol)))
984  :parameter-type '(choice :tag "Ignored charsets"
985                           :value nil
986                           (repeat (symbol)))
987  :parameter-document       "\
988 List of charsets that should be ignored.
989
990 When these charsets are used in the \"charset\" parameter, the
991 default charset will be used instead.")
992
993 (defcustom gnus-group-highlight-words-alist nil
994   "Alist of group regexps and highlight regexps.
995 This variable uses the same syntax as `gnus-emphasis-alist'."
996   :version "21.1"
997   :type '(repeat (cons (regexp :tag "Group")
998                        (repeat (list (regexp :tag "Highlight regexp")
999                                      (number :tag "Group for entire word" 0)
1000                                      (number :tag "Group for displayed part" 0)
1001                                      (symbol :tag "Face"
1002                                              gnus-emphasis-highlight-words)))))
1003   :group 'gnus-summary-visual)
1004
1005 (defcustom gnus-summary-show-article-charset-alist
1006   nil
1007   "Alist of number and charset.
1008 The article will be shown with the charset corresponding to the
1009 numbered argument.
1010 For example: ((1 . cn-gb-2312) (2 . big5))."
1011   :version "21.1"
1012   :type '(repeat (cons (number :tag "Argument" 1)
1013                        (symbol :tag "Charset")))
1014   :group 'gnus-charset)
1015
1016 (defcustom gnus-preserve-marks t
1017   "Whether marks are preserved when moving, copying and respooling messages."
1018   :version "21.1"
1019   :type 'boolean
1020   :group 'gnus-summary-marks)
1021
1022 (defcustom gnus-alter-articles-to-read-function nil
1023   "Function to be called to alter the list of articles to be selected."
1024   :type '(choice (const nil) function)
1025   :group 'gnus-summary)
1026
1027 (defcustom gnus-orphan-score nil
1028   "*All orphans get this score added.  Set in the score file."
1029   :group 'gnus-score-default
1030   :type '(choice (const nil)
1031                  integer))
1032
1033 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1034   "*A regexp to match MIME parts when saving multiple parts of a message
1035 with gnus-summary-save-parts (X m). This regexp will be used by default
1036 when prompting the user for which type of files to save."
1037   :group 'gnus-summary
1038   :type 'regexp)
1039
1040 (defcustom gnus-read-all-available-headers nil
1041   "Whether Gnus should parse all headers made available to it.
1042 This is mostly relevant for slow backends where the user may
1043 wish to widen the summary buffer to include all headers
1044 that were fetched.  Say, for nnultimate groups."
1045   :group 'gnus-summary
1046   :type '(choice boolean regexp))
1047
1048 (defcustom gnus-summary-muttprint-program "muttprint"
1049   "Command (and optional arguments) used to run Muttprint."
1050   :version "21.3"
1051   :group 'gnus-summary
1052   :type 'string)
1053
1054 (defcustom gnus-article-loose-mime nil
1055   "If non-nil, don't require MIME-Version header.
1056 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1057 supply the MIME-Version header or deliberately strip it From the mail.
1058 Set it to non-nil, Gnus will treat some articles as MIME even if
1059 the MIME-Version header is missed."
1060   :version "21.3"
1061   :type 'boolean
1062   :group 'gnus-article)
1063
1064 (defcustom gnus-article-emulate-mime t
1065   "If non-nil, use MIME emulation for uuencode and the like.
1066 This means that Gnus will search message bodies for text that look
1067 like uuencoded bits, yEncoded bits, and so on, and present that using
1068 the normal Gnus MIME machinery."
1069   :type 'boolean
1070   :group 'gnus-article)
1071
1072 ;;; Internal variables
1073
1074 (defvar gnus-summary-display-cache nil)
1075 (defvar gnus-article-mime-handles nil)
1076 (defvar gnus-article-decoded-p nil)
1077 (defvar gnus-article-charset nil)
1078 (defvar gnus-article-ignored-charsets nil)
1079 (defvar gnus-scores-exclude-files nil)
1080 (defvar gnus-page-broken nil)
1081
1082 (defvar gnus-original-article nil)
1083 (defvar gnus-article-internal-prepare-hook nil)
1084 (defvar gnus-newsgroup-process-stack nil)
1085
1086 (defvar gnus-thread-indent-array nil)
1087 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1088 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1089   "Function called to sort the articles within a thread after it has been gathered together.")
1090
1091 (defvar gnus-summary-save-parts-type-history nil)
1092 (defvar gnus-summary-save-parts-last-directory nil)
1093
1094 ;; Avoid highlighting in kill files.
1095 (defvar gnus-summary-inhibit-highlight nil)
1096 (defvar gnus-newsgroup-selected-overlay nil)
1097 (defvar gnus-inhibit-limiting nil)
1098 (defvar gnus-newsgroup-adaptive-score-file nil)
1099 (defvar gnus-current-score-file nil)
1100 (defvar gnus-current-move-group nil)
1101 (defvar gnus-current-copy-group nil)
1102 (defvar gnus-current-crosspost-group nil)
1103 (defvar gnus-newsgroup-display nil)
1104
1105 (defvar gnus-newsgroup-dependencies nil)
1106 (defvar gnus-newsgroup-adaptive nil)
1107 (defvar gnus-summary-display-article-function nil)
1108 (defvar gnus-summary-highlight-line-function nil
1109   "Function called after highlighting a summary line.")
1110
1111 (defvar gnus-summary-line-format-alist
1112   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1113     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1114     (?s gnus-tmp-subject-or-nil ?s)
1115     (?n gnus-tmp-name ?s)
1116     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1117         ?s)
1118     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1119             gnus-tmp-from) ?s)
1120     (?F gnus-tmp-from ?s)
1121     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1122     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1123     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1124     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1125     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1126     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1127     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1128     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1129     (?L gnus-tmp-lines ?s)
1130     (?O gnus-tmp-downloaded ?c)
1131     (?I gnus-tmp-indentation ?s)
1132     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1133     (?R gnus-tmp-replied ?c)
1134     (?\[ gnus-tmp-opening-bracket ?c)
1135     (?\] gnus-tmp-closing-bracket ?c)
1136     (?\> (make-string gnus-tmp-level ? ) ?s)
1137     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1138     (?i gnus-tmp-score ?d)
1139     (?z gnus-tmp-score-char ?c)
1140     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1141     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1142     (?U gnus-tmp-unread ?c)
1143     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1144         ?s)
1145     (?t (gnus-summary-number-of-articles-in-thread
1146          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1147         ?d)
1148     (?e (gnus-summary-number-of-articles-in-thread
1149          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1150         ?c)
1151     (?u gnus-tmp-user-defined ?s)
1152     (?P (gnus-pick-line-number) ?d)
1153     (?B gnus-tmp-thread-tree-header-string ?s)
1154     (user-date (gnus-user-date
1155                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1156   "An alist of format specifications that can appear in summary lines.
1157 These are paired with what variables they correspond with, along with
1158 the type of the variable (string, integer, character, etc).")
1159
1160 (defvar gnus-summary-dummy-line-format-alist
1161   `((?S gnus-tmp-subject ?s)
1162     (?N gnus-tmp-number ?d)
1163     (?u gnus-tmp-user-defined ?s)))
1164
1165 (defvar gnus-summary-mode-line-format-alist
1166   `((?G gnus-tmp-group-name ?s)
1167     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1168     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1169     (?A gnus-tmp-article-number ?d)
1170     (?Z gnus-tmp-unread-and-unselected ?s)
1171     (?V gnus-version ?s)
1172     (?U gnus-tmp-unread-and-unticked ?d)
1173     (?S gnus-tmp-subject ?s)
1174     (?e gnus-tmp-unselected ?d)
1175     (?u gnus-tmp-user-defined ?s)
1176     (?d (length gnus-newsgroup-dormant) ?d)
1177     (?t (length gnus-newsgroup-marked) ?d)
1178     (?h (length gnus-newsgroup-spam-marked) ?d)
1179     (?r (length gnus-newsgroup-reads) ?d)
1180     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1181     (?E gnus-newsgroup-expunged-tally ?d)
1182     (?s (gnus-current-score-file-nondirectory) ?s)))
1183
1184 (defvar gnus-last-search-regexp nil
1185   "Default regexp for article search command.")
1186
1187 (defvar gnus-last-shell-command nil
1188   "Default shell command on article.")
1189
1190 (defvar gnus-newsgroup-agentized nil
1191   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1192 (defvar gnus-newsgroup-begin nil)
1193 (defvar gnus-newsgroup-end nil)
1194 (defvar gnus-newsgroup-last-rmail nil)
1195 (defvar gnus-newsgroup-last-mail nil)
1196 (defvar gnus-newsgroup-last-folder nil)
1197 (defvar gnus-newsgroup-last-file nil)
1198 (defvar gnus-newsgroup-auto-expire nil)
1199 (defvar gnus-newsgroup-active nil)
1200
1201 (defvar gnus-newsgroup-data nil)
1202 (defvar gnus-newsgroup-data-reverse nil)
1203 (defvar gnus-newsgroup-limit nil)
1204 (defvar gnus-newsgroup-limits nil)
1205
1206 (defvar gnus-newsgroup-unreads nil
1207   "Sorted list of unread articles in the current newsgroup.")
1208
1209 (defvar gnus-newsgroup-unselected nil
1210   "Sorted list of unselected unread articles in the current newsgroup.")
1211
1212 (defvar gnus-newsgroup-reads nil
1213   "Alist of read articles and article marks in the current newsgroup.")
1214
1215 (defvar gnus-newsgroup-expunged-tally nil)
1216
1217 (defvar gnus-newsgroup-marked nil
1218   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1219
1220 (defvar gnus-newsgroup-spam-marked nil
1221   "List of ranges of articles that have been marked as spam.")
1222
1223 (defvar gnus-newsgroup-killed nil
1224   "List of ranges of articles that have been through the scoring process.")
1225
1226 (defvar gnus-newsgroup-cached nil
1227   "Sorted list of articles that come from the article cache.")
1228
1229 (defvar gnus-newsgroup-saved nil
1230   "List of articles that have been saved.")
1231
1232 (defvar gnus-newsgroup-kill-headers nil)
1233
1234 (defvar gnus-newsgroup-replied nil
1235   "List of articles that have been replied to in the current newsgroup.")
1236
1237 (defvar gnus-newsgroup-forwarded nil
1238   "List of articles that have been forwarded in the current newsgroup.")
1239
1240 (defvar gnus-newsgroup-recent nil
1241   "List of articles that have are recent in the current newsgroup.")
1242
1243 (defvar gnus-newsgroup-expirable nil
1244   "Sorted list of articles in the current newsgroup that can be expired.")
1245
1246 (defvar gnus-newsgroup-processable nil
1247   "List of articles in the current newsgroup that can be processed.")
1248
1249 (defvar gnus-newsgroup-downloadable nil
1250   "Sorted list of articles in the current newsgroup that can be processed.")
1251
1252 (defvar gnus-newsgroup-unfetched nil
1253   "Sorted list of articles in the current newsgroup whose headers have not been fetched into the agent.")
1254
1255 (defvar gnus-newsgroup-undownloaded nil
1256   "List of articles in the current newsgroup that haven't been downloaded..")
1257
1258 (defvar gnus-newsgroup-unsendable nil
1259   "List of articles in the current newsgroup that won't be sent.")
1260
1261 (defvar gnus-newsgroup-bookmarks nil
1262   "List of articles in the current newsgroup that have bookmarks.")
1263
1264 (defvar gnus-newsgroup-dormant nil
1265   "Sorted list of dormant articles in the current newsgroup.")
1266
1267 (defvar gnus-newsgroup-unseen nil
1268   "List of unseen articles in the current newsgroup.")
1269
1270 (defvar gnus-newsgroup-seen nil
1271   "Range of seen articles in the current newsgroup.")
1272
1273 (defvar gnus-newsgroup-articles nil
1274   "List of articles in the current newsgroup.")
1275
1276 (defvar gnus-newsgroup-scored nil
1277   "List of scored articles in the current newsgroup.")
1278
1279 (defvar gnus-newsgroup-headers nil
1280   "List of article headers in the current newsgroup.")
1281
1282 (defvar gnus-newsgroup-threads nil)
1283
1284 (defvar gnus-newsgroup-prepared nil
1285   "Whether the current group has been prepared properly.")
1286
1287 (defvar gnus-newsgroup-ancient nil
1288   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1289
1290 (defvar gnus-newsgroup-sparse nil)
1291
1292 (defvar gnus-current-article nil)
1293 (defvar gnus-article-current nil)
1294 (defvar gnus-current-headers nil)
1295 (defvar gnus-have-all-headers nil)
1296 (defvar gnus-last-article nil)
1297 (defvar gnus-newsgroup-history nil)
1298 (defvar gnus-newsgroup-charset nil)
1299 (defvar gnus-newsgroup-ephemeral-charset nil)
1300 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1301
1302 (defvar gnus-article-before-search nil)
1303
1304 (defconst gnus-summary-local-variables
1305   '(gnus-newsgroup-name
1306     gnus-newsgroup-begin gnus-newsgroup-end
1307     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1308     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1309     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1310     gnus-newsgroup-unselected gnus-newsgroup-marked
1311     gnus-newsgroup-spam-marked
1312     gnus-newsgroup-reads gnus-newsgroup-saved
1313     gnus-newsgroup-replied gnus-newsgroup-forwarded
1314     gnus-newsgroup-recent
1315     gnus-newsgroup-expirable
1316     gnus-newsgroup-processable gnus-newsgroup-killed
1317     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1318     gnus-newsgroup-unfetched
1319     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1320     gnus-newsgroup-seen gnus-newsgroup-articles
1321     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1322     gnus-newsgroup-headers gnus-newsgroup-threads
1323     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1324     gnus-current-article gnus-current-headers gnus-have-all-headers
1325     gnus-last-article gnus-article-internal-prepare-hook
1326     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1327     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1328     gnus-thread-expunge-below
1329     gnus-score-alist gnus-current-score-file
1330     (gnus-summary-expunge-below . global)
1331     (gnus-summary-mark-below . global)
1332     (gnus-orphan-score . global)
1333     gnus-newsgroup-active gnus-scores-exclude-files
1334     gnus-newsgroup-history gnus-newsgroup-ancient
1335     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1336     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1337     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1338     (gnus-newsgroup-expunged-tally . 0)
1339     gnus-cache-removable-articles gnus-newsgroup-cached
1340     gnus-newsgroup-data gnus-newsgroup-data-reverse
1341     gnus-newsgroup-limit gnus-newsgroup-limits
1342     gnus-newsgroup-charset gnus-newsgroup-display)
1343   "Variables that are buffer-local to the summary buffers.")
1344
1345 (defvar gnus-newsgroup-variables nil
1346   "A list of variables that have separate values in different newsgroups.
1347 A list of newsgroup (summary buffer) local variables, or cons of
1348 variables and their default values (when the default values are not
1349 nil), that should be made global while the summary buffer is active.
1350 These variables can be used to set variables in the group parameters
1351 while still allowing them to affect operations done in other
1352 buffers. For example:
1353
1354 \(setq gnus-newsgroup-variables
1355      '(message-use-followup-to
1356        (gnus-visible-headers .
1357          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1358 ")
1359
1360 ;; Byte-compiler warning.
1361 ;(eval-when-compile (defvar gnus-article-mode-map))
1362 (eval-when-compile
1363   (let ((features (cons 'gnus-sum features)))
1364     (require 'gnus)
1365     (require 'gnus-agent)
1366     (require 'gnus-art)))
1367
1368 ;; MIME stuff.
1369
1370 (defvar gnus-decode-encoded-word-methods
1371   '(mail-decode-encoded-word-string)
1372   "List of methods used to decode encoded words.
1373
1374 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1375 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1376 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1377 whose names match REGEXP.
1378
1379 For example:
1380 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1381  mail-decode-encoded-word-string
1382  (\"chinese\" . rfc1843-decode-string))")
1383
1384 (defvar gnus-decode-encoded-word-methods-cache nil)
1385
1386 (defun gnus-multi-decode-encoded-word-string (string)
1387   "Apply the functions from `gnus-encoded-word-methods' that match."
1388   (unless (and gnus-decode-encoded-word-methods-cache
1389                (eq gnus-newsgroup-name
1390                    (car gnus-decode-encoded-word-methods-cache)))
1391     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1392     (mapcar (lambda (x)
1393               (if (symbolp x)
1394                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1395                 (if (and gnus-newsgroup-name
1396                          (string-match (car x) gnus-newsgroup-name))
1397                     (nconc gnus-decode-encoded-word-methods-cache
1398                            (list (cdr x))))))
1399             gnus-decode-encoded-word-methods))
1400   (let ((xlist gnus-decode-encoded-word-methods-cache))
1401     (pop xlist)
1402     (while xlist
1403       (setq string (funcall (pop xlist) string))))
1404   string)
1405
1406 ;; Subject simplification.
1407
1408 (defun gnus-simplify-whitespace (str)
1409   "Remove excessive whitespace from STR."
1410   ;; Multiple spaces.
1411   (while (string-match "[ \t][ \t]+" str)
1412     (setq str (concat (substring str 0 (match-beginning 0))
1413                         " "
1414                         (substring str (match-end 0)))))
1415   ;; Leading spaces.
1416   (when (string-match "^[ \t]+" str)
1417     (setq str (substring str (match-end 0))))
1418   ;; Trailing spaces.
1419   (when (string-match "[ \t]+$" str)
1420     (setq str (substring str 0 (match-beginning 0))))
1421   str)
1422
1423 (defun gnus-simplify-all-whitespace (str)
1424   "Remove all whitespace from STR."
1425   (while (string-match "[ \t\n]+" str)
1426     (setq str (replace-match "" nil nil str)))
1427   str)
1428
1429 (defsubst gnus-simplify-subject-re (subject)
1430   "Remove \"Re:\" from subject lines."
1431   (if (string-match message-subject-re-regexp subject)
1432       (substring subject (match-end 0))
1433     subject))
1434
1435 (defun gnus-simplify-subject (subject &optional re-only)
1436   "Remove `Re:' and words in parentheses.
1437 If RE-ONLY is non-nil, strip leading `Re:'s only."
1438   (let ((case-fold-search t))           ;Ignore case.
1439     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1440     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1441       (setq subject (substring subject (match-end 0))))
1442     ;; Remove uninteresting prefixes.
1443     (when (and (not re-only)
1444                gnus-simplify-ignored-prefixes
1445                (string-match gnus-simplify-ignored-prefixes subject))
1446       (setq subject (substring subject (match-end 0))))
1447     ;; Remove words in parentheses from end.
1448     (unless re-only
1449       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1450         (setq subject (substring subject 0 (match-beginning 0)))))
1451     ;; Return subject string.
1452     subject))
1453
1454 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1455 ;; all whitespace.
1456 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1457   (goto-char (point-min))
1458   (while (re-search-forward regexp nil t)
1459     (replace-match (or newtext ""))))
1460
1461 (defun gnus-simplify-buffer-fuzzy ()
1462   "Simplify string in the buffer fuzzily.
1463 The string in the accessible portion of the current buffer is simplified.
1464 It is assumed to be a single-line subject.
1465 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1466 matter is removed.  Additional things can be deleted by setting
1467 `gnus-simplify-subject-fuzzy-regexp'."
1468   (let ((case-fold-search t)
1469         (modified-tick))
1470     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1471
1472     (while (not (eq modified-tick (buffer-modified-tick)))
1473       (setq modified-tick (buffer-modified-tick))
1474       (cond
1475        ((listp gnus-simplify-subject-fuzzy-regexp)
1476         (mapcar 'gnus-simplify-buffer-fuzzy-step
1477                 gnus-simplify-subject-fuzzy-regexp))
1478        (gnus-simplify-subject-fuzzy-regexp
1479         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1480       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1481       (gnus-simplify-buffer-fuzzy-step
1482        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1483       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1484
1485     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1486     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1487     (gnus-simplify-buffer-fuzzy-step " $")
1488     (gnus-simplify-buffer-fuzzy-step "^ +")))
1489
1490 (defun gnus-simplify-subject-fuzzy (subject)
1491   "Simplify a subject string fuzzily.
1492 See `gnus-simplify-buffer-fuzzy' for details."
1493   (save-excursion
1494     (gnus-set-work-buffer)
1495     (let ((case-fold-search t))
1496       ;; Remove uninteresting prefixes.
1497       (when (and gnus-simplify-ignored-prefixes
1498                  (string-match gnus-simplify-ignored-prefixes subject))
1499         (setq subject (substring subject (match-end 0))))
1500       (insert subject)
1501       (inline (gnus-simplify-buffer-fuzzy))
1502       (buffer-string))))
1503
1504 (defsubst gnus-simplify-subject-fully (subject)
1505   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1506   (cond
1507    (gnus-simplify-subject-functions
1508     (gnus-map-function gnus-simplify-subject-functions subject))
1509    ((null gnus-summary-gather-subject-limit)
1510     (gnus-simplify-subject-re subject))
1511    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1512     (gnus-simplify-subject-fuzzy subject))
1513    ((numberp gnus-summary-gather-subject-limit)
1514     (gnus-limit-string (gnus-simplify-subject-re subject)
1515                        gnus-summary-gather-subject-limit))
1516    (t
1517     subject)))
1518
1519 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1520   "Check whether two subjects are equal.
1521 If optional argument simple-first is t, first argument is already
1522 simplified."
1523   (cond
1524    ((null simple-first)
1525     (equal (gnus-simplify-subject-fully s1)
1526            (gnus-simplify-subject-fully s2)))
1527    (t
1528     (equal s1
1529            (gnus-simplify-subject-fully s2)))))
1530
1531 (defun gnus-summary-bubble-group ()
1532   "Increase the score of the current group.
1533 This is a handy function to add to `gnus-summary-exit-hook' to
1534 increase the score of each group you read."
1535   (gnus-group-add-score gnus-newsgroup-name))
1536
1537 \f
1538 ;;;
1539 ;;; Gnus summary mode
1540 ;;;
1541
1542 (put 'gnus-summary-mode 'mode-class 'special)
1543
1544 (defvar gnus-article-commands-menu)
1545
1546 ;; Non-orthogonal keys
1547
1548 (gnus-define-keys gnus-summary-mode-map
1549   " " gnus-summary-next-page
1550   "\177" gnus-summary-prev-page
1551   [delete] gnus-summary-prev-page
1552   [backspace] gnus-summary-prev-page
1553   "\r" gnus-summary-scroll-up
1554   "\M-\r" gnus-summary-scroll-down
1555   "n" gnus-summary-next-unread-article
1556   "p" gnus-summary-prev-unread-article
1557   "N" gnus-summary-next-article
1558   "P" gnus-summary-prev-article
1559   "\M-\C-n" gnus-summary-next-same-subject
1560   "\M-\C-p" gnus-summary-prev-same-subject
1561   "\M-n" gnus-summary-next-unread-subject
1562   "\M-p" gnus-summary-prev-unread-subject
1563   "." gnus-summary-first-unread-article
1564   "," gnus-summary-best-unread-article
1565   "\M-s" gnus-summary-search-article-forward
1566   "\M-r" gnus-summary-search-article-backward
1567   "<" gnus-summary-beginning-of-article
1568   ">" gnus-summary-end-of-article
1569   "j" gnus-summary-goto-article
1570   "^" gnus-summary-refer-parent-article
1571   "\M-^" gnus-summary-refer-article
1572   "u" gnus-summary-tick-article-forward
1573   "!" gnus-summary-tick-article-forward
1574   "U" gnus-summary-tick-article-backward
1575   "d" gnus-summary-mark-as-read-forward
1576   "D" gnus-summary-mark-as-read-backward
1577   "E" gnus-summary-mark-as-expirable
1578   "\M-u" gnus-summary-clear-mark-forward
1579   "\M-U" gnus-summary-clear-mark-backward
1580   "k" gnus-summary-kill-same-subject-and-select
1581   "\C-k" gnus-summary-kill-same-subject
1582   "\M-\C-k" gnus-summary-kill-thread
1583   "\M-\C-l" gnus-summary-lower-thread
1584   "e" gnus-summary-edit-article
1585   "#" gnus-summary-mark-as-processable
1586   "\M-#" gnus-summary-unmark-as-processable
1587   "\M-\C-t" gnus-summary-toggle-threads
1588   "\M-\C-s" gnus-summary-show-thread
1589   "\M-\C-h" gnus-summary-hide-thread
1590   "\M-\C-f" gnus-summary-next-thread
1591   "\M-\C-b" gnus-summary-prev-thread
1592   [(meta down)] gnus-summary-next-thread
1593   [(meta up)] gnus-summary-prev-thread
1594   "\M-\C-u" gnus-summary-up-thread
1595   "\M-\C-d" gnus-summary-down-thread
1596   "&" gnus-summary-execute-command
1597   "c" gnus-summary-catchup-and-exit
1598   "\C-w" gnus-summary-mark-region-as-read
1599   "\C-t" gnus-summary-toggle-truncation
1600   "?" gnus-summary-mark-as-dormant
1601   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1602   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1603   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1604   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1605   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1606   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1607   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1608   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1609   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1610   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1611   "=" gnus-summary-expand-window
1612   "\C-x\C-s" gnus-summary-reselect-current-group
1613   "\M-g" gnus-summary-rescan-group
1614   "w" gnus-summary-stop-page-breaking
1615   "\C-c\C-r" gnus-summary-caesar-message
1616   "f" gnus-summary-followup
1617   "F" gnus-summary-followup-with-original
1618   "C" gnus-summary-cancel-article
1619   "r" gnus-summary-reply
1620   "R" gnus-summary-reply-with-original
1621   "\C-c\C-f" gnus-summary-mail-forward
1622   "o" gnus-summary-save-article
1623   "\C-o" gnus-summary-save-article-mail
1624   "|" gnus-summary-pipe-output
1625   "\M-k" gnus-summary-edit-local-kill
1626   "\M-K" gnus-summary-edit-global-kill
1627   ;; "V" gnus-version
1628   "\C-c\C-d" gnus-summary-describe-group
1629   "q" gnus-summary-exit
1630   "Q" gnus-summary-exit-no-update
1631   "\C-c\C-i" gnus-info-find-node
1632   gnus-mouse-2 gnus-mouse-pick-article
1633   "m" gnus-summary-mail-other-window
1634   "a" gnus-summary-post-news
1635   "i" gnus-summary-news-other-window
1636   "x" gnus-summary-limit-to-unread
1637   "s" gnus-summary-isearch-article
1638   "t" gnus-summary-toggle-header
1639   "g" gnus-summary-show-article
1640   "l" gnus-summary-goto-last-article
1641   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1642   "\C-d" gnus-summary-enter-digest-group
1643   "\M-\C-d" gnus-summary-read-document
1644   "\M-\C-e" gnus-summary-edit-parameters
1645   "\M-\C-a" gnus-summary-customize-parameters
1646   "\C-c\C-b" gnus-bug
1647   "*" gnus-cache-enter-article
1648   "\M-*" gnus-cache-remove-article
1649   "\M-&" gnus-summary-universal-argument
1650   "\C-l" gnus-recenter
1651   "I" gnus-summary-increase-score
1652   "L" gnus-summary-lower-score
1653   "\M-i" gnus-symbolic-argument
1654   "h" gnus-summary-select-article-buffer
1655
1656   "b" gnus-article-view-part
1657   "\M-t" gnus-summary-toggle-display-buttonized
1658
1659   "V" gnus-summary-score-map
1660   "X" gnus-uu-extract-map
1661   "S" gnus-summary-send-map)
1662
1663 ;; Sort of orthogonal keymap
1664 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1665   "t" gnus-summary-tick-article-forward
1666   "!" gnus-summary-tick-article-forward
1667   "d" gnus-summary-mark-as-read-forward
1668   "r" gnus-summary-mark-as-read-forward
1669   "c" gnus-summary-clear-mark-forward
1670   " " gnus-summary-clear-mark-forward
1671   "e" gnus-summary-mark-as-expirable
1672   "x" gnus-summary-mark-as-expirable
1673   "?" gnus-summary-mark-as-dormant
1674   "b" gnus-summary-set-bookmark
1675   "B" gnus-summary-remove-bookmark
1676   "#" gnus-summary-mark-as-processable
1677   "\M-#" gnus-summary-unmark-as-processable
1678   "S" gnus-summary-limit-include-expunged
1679   "C" gnus-summary-catchup
1680   "H" gnus-summary-catchup-to-here
1681   "h" gnus-summary-catchup-from-here
1682   "\C-c" gnus-summary-catchup-all
1683   "k" gnus-summary-kill-same-subject-and-select
1684   "K" gnus-summary-kill-same-subject
1685   "P" gnus-uu-mark-map)
1686
1687 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1688   "c" gnus-summary-clear-above
1689   "u" gnus-summary-tick-above
1690   "m" gnus-summary-mark-above
1691   "k" gnus-summary-kill-below)
1692
1693 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1694   "/" gnus-summary-limit-to-subject
1695   "n" gnus-summary-limit-to-articles
1696   "w" gnus-summary-pop-limit
1697   "s" gnus-summary-limit-to-subject
1698   "a" gnus-summary-limit-to-author
1699   "u" gnus-summary-limit-to-unread
1700   "m" gnus-summary-limit-to-marks
1701   "M" gnus-summary-limit-exclude-marks
1702   "v" gnus-summary-limit-to-score
1703   "*" gnus-summary-limit-include-cached
1704   "D" gnus-summary-limit-include-dormant
1705   "T" gnus-summary-limit-include-thread
1706   "d" gnus-summary-limit-exclude-dormant
1707   "t" gnus-summary-limit-to-age
1708   "." gnus-summary-limit-to-unseen
1709   "x" gnus-summary-limit-to-extra
1710   "p" gnus-summary-limit-to-display-predicate
1711   "E" gnus-summary-limit-include-expunged
1712   "c" gnus-summary-limit-exclude-childless-dormant
1713   "C" gnus-summary-limit-mark-excluded-as-read
1714   "o" gnus-summary-insert-old-articles
1715   "N" gnus-summary-insert-new-articles)
1716
1717 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1718   "n" gnus-summary-next-unread-article
1719   "p" gnus-summary-prev-unread-article
1720   "N" gnus-summary-next-article
1721   "P" gnus-summary-prev-article
1722   "\C-n" gnus-summary-next-same-subject
1723   "\C-p" gnus-summary-prev-same-subject
1724   "\M-n" gnus-summary-next-unread-subject
1725   "\M-p" gnus-summary-prev-unread-subject
1726   "f" gnus-summary-first-unread-article
1727   "b" gnus-summary-best-unread-article
1728   "j" gnus-summary-goto-article
1729   "g" gnus-summary-goto-subject
1730   "l" gnus-summary-goto-last-article
1731   "o" gnus-summary-pop-article)
1732
1733 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1734   "k" gnus-summary-kill-thread
1735   "l" gnus-summary-lower-thread
1736   "i" gnus-summary-raise-thread
1737   "T" gnus-summary-toggle-threads
1738   "t" gnus-summary-rethread-current
1739   "^" gnus-summary-reparent-thread
1740   "s" gnus-summary-show-thread
1741   "S" gnus-summary-show-all-threads
1742   "h" gnus-summary-hide-thread
1743   "H" gnus-summary-hide-all-threads
1744   "n" gnus-summary-next-thread
1745   "p" gnus-summary-prev-thread
1746   "u" gnus-summary-up-thread
1747   "o" gnus-summary-top-thread
1748   "d" gnus-summary-down-thread
1749   "#" gnus-uu-mark-thread
1750   "\M-#" gnus-uu-unmark-thread)
1751
1752 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1753   "g" gnus-summary-prepare
1754   "c" gnus-summary-insert-cached-articles
1755   "d" gnus-summary-insert-dormant-articles)
1756
1757 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1758   "c" gnus-summary-catchup-and-exit
1759   "C" gnus-summary-catchup-all-and-exit
1760   "E" gnus-summary-exit-no-update
1761   "Q" gnus-summary-exit
1762   "Z" gnus-summary-exit
1763   "n" gnus-summary-catchup-and-goto-next-group
1764   "R" gnus-summary-reselect-current-group
1765   "G" gnus-summary-rescan-group
1766   "N" gnus-summary-next-group
1767   "s" gnus-summary-save-newsrc
1768   "P" gnus-summary-prev-group)
1769
1770 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1771   " " gnus-summary-next-page
1772   "n" gnus-summary-next-page
1773   "\177" gnus-summary-prev-page
1774   [delete] gnus-summary-prev-page
1775   "p" gnus-summary-prev-page
1776   "\r" gnus-summary-scroll-up
1777   "\M-\r" gnus-summary-scroll-down
1778   "<" gnus-summary-beginning-of-article
1779   ">" gnus-summary-end-of-article
1780   "b" gnus-summary-beginning-of-article
1781   "e" gnus-summary-end-of-article
1782   "^" gnus-summary-refer-parent-article
1783   "r" gnus-summary-refer-parent-article
1784   "D" gnus-summary-enter-digest-group
1785   "R" gnus-summary-refer-references
1786   "T" gnus-summary-refer-thread
1787   "g" gnus-summary-show-article
1788   "s" gnus-summary-isearch-article
1789   "P" gnus-summary-print-article
1790   "M" gnus-mailing-list-insinuate
1791   "t" gnus-article-babel)
1792
1793 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1794   "b" gnus-article-add-buttons
1795   "B" gnus-article-add-buttons-to-head
1796   "o" gnus-article-treat-overstrike
1797   "e" gnus-article-emphasize
1798   "w" gnus-article-fill-cited-article
1799   "Q" gnus-article-fill-long-lines
1800   "C" gnus-article-capitalize-sentences
1801   "c" gnus-article-remove-cr
1802   "q" gnus-article-de-quoted-unreadable
1803   "6" gnus-article-de-base64-unreadable
1804   "Z" gnus-article-decode-HZ
1805   "h" gnus-article-wash-html
1806   "u" gnus-article-unsplit-urls
1807   "s" gnus-summary-force-verify-and-decrypt
1808   "f" gnus-article-display-x-face
1809   "l" gnus-summary-stop-page-breaking
1810   "r" gnus-summary-caesar-message
1811   "m" gnus-summary-morse-message
1812   "t" gnus-summary-toggle-header
1813   "g" gnus-treat-smiley
1814   "v" gnus-summary-verbose-headers
1815   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1816   "p" gnus-article-verify-x-pgp-sig
1817   "d" gnus-article-treat-dumbquotes)
1818
1819 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1820   ;; mnemonic: deuglif*Y*
1821   "u" gnus-article-outlook-unwrap-lines
1822   "a" gnus-article-outlook-repair-attribution
1823   "c" gnus-article-outlook-rearrange-citation
1824   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1825
1826 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1827   "a" gnus-article-hide
1828   "h" gnus-article-hide-headers
1829   "b" gnus-article-hide-boring-headers
1830   "s" gnus-article-hide-signature
1831   "c" gnus-article-hide-citation
1832   "C" gnus-article-hide-citation-in-followups
1833   "l" gnus-article-hide-list-identifiers
1834   "B" gnus-article-strip-banner
1835   "P" gnus-article-hide-pem
1836   "\C-c" gnus-article-hide-citation-maybe)
1837
1838 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1839   "a" gnus-article-highlight
1840   "h" gnus-article-highlight-headers
1841   "c" gnus-article-highlight-citation
1842   "s" gnus-article-highlight-signature)
1843
1844 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1845   "f" gnus-article-treat-fold-headers
1846   "u" gnus-article-treat-unfold-headers
1847   "n" gnus-article-treat-fold-newsgroups)
1848
1849 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1850   "x" gnus-article-display-x-face
1851   "d" gnus-article-display-face
1852   "s" gnus-treat-smiley
1853   "D" gnus-article-remove-images
1854   "f" gnus-treat-from-picon
1855   "m" gnus-treat-mail-picon
1856   "n" gnus-treat-newsgroups-picon)
1857
1858 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1859   "w" gnus-article-decode-mime-words
1860   "c" gnus-article-decode-charset
1861   "v" gnus-mime-view-all-parts
1862   "b" gnus-article-view-part)
1863
1864 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1865   "z" gnus-article-date-ut
1866   "u" gnus-article-date-ut
1867   "l" gnus-article-date-local
1868   "p" gnus-article-date-english
1869   "e" gnus-article-date-lapsed
1870   "o" gnus-article-date-original
1871   "i" gnus-article-date-iso8601
1872   "s" gnus-article-date-user)
1873
1874 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1875   "t" gnus-article-remove-trailing-blank-lines
1876   "l" gnus-article-strip-leading-blank-lines
1877   "m" gnus-article-strip-multiple-blank-lines
1878   "a" gnus-article-strip-blank-lines
1879   "A" gnus-article-strip-all-blank-lines
1880   "s" gnus-article-strip-leading-space
1881   "e" gnus-article-strip-trailing-space
1882   "w" gnus-article-remove-leading-whitespace)
1883
1884 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1885   "v" gnus-version
1886   "f" gnus-summary-fetch-faq
1887   "d" gnus-summary-describe-group
1888   "h" gnus-summary-describe-briefly
1889   "i" gnus-info-find-node
1890   "c" gnus-group-fetch-charter
1891   "C" gnus-group-fetch-control)
1892
1893 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1894   "e" gnus-summary-expire-articles
1895   "\M-\C-e" gnus-summary-expire-articles-now
1896   "\177" gnus-summary-delete-article
1897   [delete] gnus-summary-delete-article
1898   [backspace] gnus-summary-delete-article
1899   "m" gnus-summary-move-article
1900   "r" gnus-summary-respool-article
1901   "w" gnus-summary-edit-article
1902   "c" gnus-summary-copy-article
1903   "B" gnus-summary-crosspost-article
1904   "q" gnus-summary-respool-query
1905   "t" gnus-summary-respool-trace
1906   "i" gnus-summary-import-article
1907   "I" gnus-summary-create-article
1908   "p" gnus-summary-article-posted-p)
1909
1910 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1911   "o" gnus-summary-save-article
1912   "m" gnus-summary-save-article-mail
1913   "F" gnus-summary-write-article-file
1914   "r" gnus-summary-save-article-rmail
1915   "f" gnus-summary-save-article-file
1916   "b" gnus-summary-save-article-body-file
1917   "h" gnus-summary-save-article-folder
1918   "v" gnus-summary-save-article-vm
1919   "p" gnus-summary-pipe-output
1920   "P" gnus-summary-muttprint
1921   "s" gnus-soup-add-article)
1922
1923 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1924   "b" gnus-summary-display-buttonized
1925   "m" gnus-summary-repair-multipart
1926   "v" gnus-article-view-part
1927   "o" gnus-article-save-part
1928   "c" gnus-article-copy-part
1929   "C" gnus-article-view-part-as-charset
1930   "e" gnus-article-view-part-externally
1931   "E" gnus-article-encrypt-body
1932   "i" gnus-article-inline-part
1933   "|" gnus-article-pipe-part)
1934
1935 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1936   "p" gnus-summary-mark-as-processable
1937   "u" gnus-summary-unmark-as-processable
1938   "U" gnus-summary-unmark-all-processable
1939   "v" gnus-uu-mark-over
1940   "s" gnus-uu-mark-series
1941   "r" gnus-uu-mark-region
1942   "g" gnus-uu-unmark-region
1943   "R" gnus-uu-mark-by-regexp
1944   "G" gnus-uu-unmark-by-regexp
1945   "t" gnus-uu-mark-thread
1946   "T" gnus-uu-unmark-thread
1947   "a" gnus-uu-mark-all
1948   "b" gnus-uu-mark-buffer
1949   "S" gnus-uu-mark-sparse
1950   "k" gnus-summary-kill-process-mark
1951   "y" gnus-summary-yank-process-mark
1952   "w" gnus-summary-save-process-mark
1953   "i" gnus-uu-invert-processable)
1954
1955 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1956   ;;"x" gnus-uu-extract-any
1957   "m" gnus-summary-save-parts
1958   "u" gnus-uu-decode-uu
1959   "U" gnus-uu-decode-uu-and-save
1960   "s" gnus-uu-decode-unshar
1961   "S" gnus-uu-decode-unshar-and-save
1962   "o" gnus-uu-decode-save
1963   "O" gnus-uu-decode-save
1964   "b" gnus-uu-decode-binhex
1965   "B" gnus-uu-decode-binhex
1966   "p" gnus-uu-decode-postscript
1967   "P" gnus-uu-decode-postscript-and-save)
1968
1969 (gnus-define-keys
1970     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
1971   "u" gnus-uu-decode-uu-view
1972   "U" gnus-uu-decode-uu-and-save-view
1973   "s" gnus-uu-decode-unshar-view
1974   "S" gnus-uu-decode-unshar-and-save-view
1975   "o" gnus-uu-decode-save-view
1976   "O" gnus-uu-decode-save-view
1977   "b" gnus-uu-decode-binhex-view
1978   "B" gnus-uu-decode-binhex-view
1979   "p" gnus-uu-decode-postscript-view
1980   "P" gnus-uu-decode-postscript-and-save-view)
1981
1982 (defvar gnus-article-post-menu nil)
1983
1984 (defconst gnus-summary-menu-maxlen 20)
1985
1986 (defun gnus-summary-menu-split (menu)
1987   ;; If we have lots of elements, divide them into groups of 20
1988   ;; and make a pane (or submenu) for each one.
1989   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
1990       (let ((menu menu) sublists next
1991             (i 1))
1992         (while menu
1993           ;; Pull off the next gnus-summary-menu-maxlen elements
1994           ;; and make them the next element of sublist.
1995           (setq next (nthcdr gnus-summary-menu-maxlen menu))
1996           (if next
1997               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
1998                       nil))
1999           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2000                                              (aref (car (last menu)) 0)) menu)
2001                                sublists))
2002           (setq i (1+ i))
2003           (setq menu next))
2004         (nreverse sublists))
2005     ;; Few elements--put them all in one pane.
2006     menu))
2007
2008 (defun gnus-summary-make-menu-bar ()
2009   (gnus-turn-off-edit-menu 'summary)
2010
2011   (unless (boundp 'gnus-summary-misc-menu)
2012
2013     (easy-menu-define
2014       gnus-summary-kill-menu gnus-summary-mode-map ""
2015       (cons
2016        "Score"
2017        (nconc
2018         (list
2019          ["Customize" gnus-score-customize t])
2020         (gnus-make-score-map 'increase)
2021         (gnus-make-score-map 'lower)
2022         '(("Mark"
2023            ["Kill below" gnus-summary-kill-below t]
2024            ["Mark above" gnus-summary-mark-above t]
2025            ["Tick above" gnus-summary-tick-above t]
2026            ["Clear above" gnus-summary-clear-above t])
2027           ["Current score" gnus-summary-current-score t]
2028           ["Set score" gnus-summary-set-score t]
2029           ["Switch current score file..." gnus-score-change-score-file t]
2030           ["Set mark below..." gnus-score-set-mark-below t]
2031           ["Set expunge below..." gnus-score-set-expunge-below t]
2032           ["Edit current score file" gnus-score-edit-current-scores t]
2033           ["Edit score file" gnus-score-edit-file t]
2034           ["Trace score" gnus-score-find-trace t]
2035           ["Find words" gnus-score-find-favourite-words t]
2036           ["Rescore buffer" gnus-summary-rescore t]
2037           ["Increase score..." gnus-summary-increase-score t]
2038           ["Lower score..." gnus-summary-lower-score t]))))
2039
2040     ;; Define both the Article menu in the summary buffer and the
2041     ;; equivalent Commands menu in the article buffer here for
2042     ;; consistency.
2043     (let ((innards
2044            `(("Hide"
2045               ["All" gnus-article-hide t]
2046               ["Headers" gnus-article-hide-headers t]
2047               ["Signature" gnus-article-hide-signature t]
2048               ["Citation" gnus-article-hide-citation t]
2049               ["List identifiers" gnus-article-hide-list-identifiers t]
2050               ["Banner" gnus-article-strip-banner t]
2051               ["Boring headers" gnus-article-hide-boring-headers t])
2052              ("Highlight"
2053               ["All" gnus-article-highlight t]
2054               ["Headers" gnus-article-highlight-headers t]
2055               ["Signature" gnus-article-highlight-signature t]
2056               ["Citation" gnus-article-highlight-citation t])
2057              ("MIME"
2058               ["Words" gnus-article-decode-mime-words t]
2059               ["Charset" gnus-article-decode-charset t]
2060               ["QP" gnus-article-de-quoted-unreadable t]
2061               ["Base64" gnus-article-de-base64-unreadable t]
2062               ["View MIME buttons" gnus-summary-display-buttonized t]
2063               ["View all" gnus-mime-view-all-parts t]
2064               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2065               ["Encrypt body" gnus-article-encrypt-body t]
2066               ["Extract all parts" gnus-summary-save-parts t])
2067              ("Date"
2068               ["Local" gnus-article-date-local t]
2069               ["ISO8601" gnus-article-date-iso8601 t]
2070               ["UT" gnus-article-date-ut t]
2071               ["Original" gnus-article-date-original t]
2072               ["Lapsed" gnus-article-date-lapsed t]
2073               ["User-defined" gnus-article-date-user t])
2074              ("Display"
2075               ["Remove images" gnus-article-remove-images t]
2076               ["Toggle smiley" gnus-treat-smiley t]
2077               ["Show X-Face" gnus-article-display-x-face t]
2078               ["Show picons in From" gnus-treat-from-picon t]
2079               ["Show picons in mail headers" gnus-treat-mail-picon t]
2080               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2081               ("View as different encoding"
2082                ,@(gnus-summary-menu-split
2083                   (mapcar
2084                    (lambda (cs)
2085                      ;; Since easymenu under FSF Emacs doesn't allow lambda
2086                      ;; forms for menu commands, we should provide intern'ed
2087                      ;; function symbols.
2088                      (let ((command (intern (format "\
2089 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2090                        (fset command
2091                              `(lambda ()
2092                                 (interactive)
2093                                 (let ((gnus-summary-show-article-charset-alist
2094                                        '((1 . ,cs))))
2095                                   (gnus-summary-show-article 1))))
2096                        `[,(symbol-name cs) ,command t]))
2097                    (sort (if (fboundp 'coding-system-list)
2098                              (coding-system-list)
2099                            (mapcar 'car mm-mime-mule-charset-alist))
2100                          'string<)))))
2101              ("Washing"
2102               ("Remove Blanks"
2103                ["Leading" gnus-article-strip-leading-blank-lines t]
2104                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2105                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2106                ["All of the above" gnus-article-strip-blank-lines t]
2107                ["All" gnus-article-strip-all-blank-lines t]
2108                ["Leading space" gnus-article-strip-leading-space t]
2109                ["Trailing space" gnus-article-strip-trailing-space t]
2110                ["Leading space in headers"
2111                 gnus-article-remove-leading-whitespace t])
2112               ["Overstrike" gnus-article-treat-overstrike t]
2113               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2114               ["Emphasis" gnus-article-emphasize t]
2115               ["Word wrap" gnus-article-fill-cited-article t]
2116               ["Fill long lines" gnus-article-fill-long-lines t]
2117               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2118               ["CR" gnus-article-remove-cr t]
2119               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2120               ["Base64" gnus-article-de-base64-unreadable t]
2121               ["Rot 13" gnus-summary-caesar-message
2122                ,@(if (featurep 'xemacs) '(t)
2123                    '(:help "\"Caesar rotate\" article by 13"))]
2124               ["Morse decode" gnus-summary-morse-message t]
2125               ["Unix pipe..." gnus-summary-pipe-message t]
2126               ["Add buttons" gnus-article-add-buttons t]
2127               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2128               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2129               ["Verbose header" gnus-summary-verbose-headers t]
2130               ["Toggle header" gnus-summary-toggle-header t]
2131               ["Unfold headers" gnus-article-treat-unfold-headers t]
2132               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2133               ["Html" gnus-article-wash-html t]
2134               ["URLs" gnus-article-unsplit-urls t]
2135               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2136               ["HZ" gnus-article-decode-HZ t]
2137               ("(Outlook) Deuglify"
2138                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2139                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2140                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2141                ["Full (Outlook) deuglify"
2142                 gnus-article-outlook-deuglify-article t])
2143               )
2144              ("Output"
2145               ["Save in default format" gnus-summary-save-article
2146                ,@(if (featurep 'xemacs) '(t)
2147                    '(:help "Save article using default method"))]
2148               ["Save in file" gnus-summary-save-article-file
2149                ,@(if (featurep 'xemacs) '(t)
2150                    '(:help "Save article in file"))]
2151               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2152               ["Save in MH folder" gnus-summary-save-article-folder t]
2153               ["Save in VM folder" gnus-summary-save-article-vm t]
2154               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2155               ["Save body in file" gnus-summary-save-article-body-file t]
2156               ["Pipe through a filter" gnus-summary-pipe-output t]
2157               ["Add to SOUP packet" gnus-soup-add-article t]
2158               ["Print with Muttprint" gnus-summary-muttprint t]
2159               ["Print" gnus-summary-print-article t])
2160              ("Backend"
2161               ["Respool article..." gnus-summary-respool-article t]
2162               ["Move article..." gnus-summary-move-article
2163                (gnus-check-backend-function
2164                 'request-move-article gnus-newsgroup-name)]
2165               ["Copy article..." gnus-summary-copy-article t]
2166               ["Crosspost article..." gnus-summary-crosspost-article
2167                (gnus-check-backend-function
2168                 'request-replace-article gnus-newsgroup-name)]
2169               ["Import file..." gnus-summary-import-article t]
2170               ["Create article..." gnus-summary-create-article t]
2171               ["Check if posted" gnus-summary-article-posted-p t]
2172               ["Edit article" gnus-summary-edit-article
2173                (not (gnus-group-read-only-p))]
2174               ["Delete article" gnus-summary-delete-article
2175                (gnus-check-backend-function
2176                 'request-expire-articles gnus-newsgroup-name)]
2177               ["Query respool" gnus-summary-respool-query t]
2178               ["Trace respool" gnus-summary-respool-trace t]
2179               ["Delete expirable articles" gnus-summary-expire-articles-now
2180                (gnus-check-backend-function
2181                 'request-expire-articles gnus-newsgroup-name)])
2182              ("Extract"
2183               ["Uudecode" gnus-uu-decode-uu
2184                ,@(if (featurep 'xemacs) '(t)
2185                    '(:help "Decode uuencoded article(s)"))]
2186               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2187               ["Unshar" gnus-uu-decode-unshar t]
2188               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2189               ["Save" gnus-uu-decode-save t]
2190               ["Binhex" gnus-uu-decode-binhex t]
2191               ["Postscript" gnus-uu-decode-postscript t]
2192               ["All MIME parts" gnus-summary-save-parts t])
2193              ("Cache"
2194               ["Enter article" gnus-cache-enter-article t]
2195               ["Remove article" gnus-cache-remove-article t])
2196              ["Translate" gnus-article-babel t]
2197              ["Select article buffer" gnus-summary-select-article-buffer t]
2198              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2199              ["Isearch article..." gnus-summary-isearch-article t]
2200              ["Beginning of the article" gnus-summary-beginning-of-article t]
2201              ["End of the article" gnus-summary-end-of-article t]
2202              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2203              ["Fetch referenced articles" gnus-summary-refer-references t]
2204              ["Fetch current thread" gnus-summary-refer-thread t]
2205              ["Fetch article with id..." gnus-summary-refer-article t]
2206              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2207              ["Redisplay" gnus-summary-show-article t]
2208              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2209       (easy-menu-define
2210         gnus-summary-article-menu gnus-summary-mode-map ""
2211         (cons "Article" innards))
2212
2213       (if (not (keymapp gnus-summary-article-menu))
2214           (easy-menu-define
2215             gnus-article-commands-menu gnus-article-mode-map ""
2216             (cons "Commands" innards))
2217         ;; in Emacs, don't share menu.
2218         (setq gnus-article-commands-menu
2219               (copy-keymap gnus-summary-article-menu))
2220         (define-key gnus-article-mode-map [menu-bar commands]
2221           (cons "Commands" gnus-article-commands-menu))))
2222
2223     (easy-menu-define
2224       gnus-summary-thread-menu gnus-summary-mode-map ""
2225       '("Threads"
2226         ["Find all messages in thread" gnus-summary-refer-thread t]
2227         ["Toggle threading" gnus-summary-toggle-threads t]
2228         ["Hide threads" gnus-summary-hide-all-threads t]
2229         ["Show threads" gnus-summary-show-all-threads t]
2230         ["Hide thread" gnus-summary-hide-thread t]
2231         ["Show thread" gnus-summary-show-thread t]
2232         ["Go to next thread" gnus-summary-next-thread t]
2233         ["Go to previous thread" gnus-summary-prev-thread t]
2234         ["Go down thread" gnus-summary-down-thread t]
2235         ["Go up thread" gnus-summary-up-thread t]
2236         ["Top of thread" gnus-summary-top-thread t]
2237         ["Mark thread as read" gnus-summary-kill-thread t]
2238         ["Lower thread score" gnus-summary-lower-thread t]
2239         ["Raise thread score" gnus-summary-raise-thread t]
2240         ["Rethread current" gnus-summary-rethread-current t]))
2241
2242     (easy-menu-define
2243       gnus-summary-post-menu gnus-summary-mode-map ""
2244       `("Post"
2245         ["Send a message (mail or news)" gnus-summary-post-news
2246          ,@(if (featurep 'xemacs) '(t)
2247              '(:help "Post an article"))]
2248         ["Followup" gnus-summary-followup
2249          ,@(if (featurep 'xemacs) '(t)
2250              '(:help "Post followup to this article"))]
2251         ["Followup and yank" gnus-summary-followup-with-original
2252          ,@(if (featurep 'xemacs) '(t)
2253              '(:help "Post followup to this article, quoting its contents"))]
2254         ["Supersede article" gnus-summary-supersede-article t]
2255         ["Cancel article" gnus-summary-cancel-article
2256          ,@(if (featurep 'xemacs) '(t)
2257              '(:help "Cancel an article you posted"))]
2258         ["Reply" gnus-summary-reply t]
2259         ["Reply and yank" gnus-summary-reply-with-original t]
2260         ["Wide reply" gnus-summary-wide-reply t]
2261         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2262          ,@(if (featurep 'xemacs) '(t)
2263              '(:help "Mail a reply, quoting this article"))]
2264         ["Very wide reply" gnus-summary-very-wide-reply t]
2265         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2266          ,@(if (featurep 'xemacs) '(t)
2267              '(:help "Mail a very wide reply, quoting this article"))]
2268         ["Mail forward" gnus-summary-mail-forward t]
2269         ["Post forward" gnus-summary-post-forward t]
2270         ["Digest and mail" gnus-uu-digest-mail-forward t]
2271         ["Digest and post" gnus-uu-digest-post-forward t]
2272         ["Resend message" gnus-summary-resend-message t]
2273         ["Resend message edit" gnus-summary-resend-message-edit t]
2274         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2275         ["Send a mail" gnus-summary-mail-other-window t]
2276         ["Create a local message" gnus-summary-news-other-window t]
2277         ["Uuencode and post" gnus-uu-post-news
2278          ,@(if (featurep 'xemacs) '(t)
2279              '(:help "Post a uuencoded article"))]
2280         ["Followup via news" gnus-summary-followup-to-mail t]
2281         ["Followup via news and yank"
2282          gnus-summary-followup-to-mail-with-original t]
2283         ;;("Draft"
2284         ;;["Send" gnus-summary-send-draft t]
2285         ;;["Send bounced" gnus-resend-bounced-mail t])
2286         ))
2287
2288     (cond
2289      ((not (keymapp gnus-summary-post-menu))
2290       (setq gnus-article-post-menu gnus-summary-post-menu))
2291      ((not gnus-article-post-menu)
2292       ;; Don't share post menu.
2293       (setq gnus-article-post-menu
2294             (copy-keymap gnus-summary-post-menu))))
2295     (define-key gnus-article-mode-map [menu-bar post]
2296       (cons "Post" gnus-article-post-menu))
2297
2298     (easy-menu-define
2299       gnus-summary-misc-menu gnus-summary-mode-map ""
2300       `("Gnus"
2301         ("Mark Read"
2302          ["Mark as read" gnus-summary-mark-as-read-forward t]
2303          ["Mark same subject and select"
2304           gnus-summary-kill-same-subject-and-select t]
2305          ["Mark same subject" gnus-summary-kill-same-subject t]
2306          ["Catchup" gnus-summary-catchup
2307           ,@(if (featurep 'xemacs) '(t)
2308               '(:help "Mark unread articles in this group as read"))]
2309          ["Catchup all" gnus-summary-catchup-all t]
2310          ["Catchup to here" gnus-summary-catchup-to-here t]
2311          ["Catchup from here" gnus-summary-catchup-from-here t]
2312          ["Catchup region" gnus-summary-mark-region-as-read 
2313           (gnus-mark-active-p)]
2314          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2315         ("Mark Various"
2316          ["Tick" gnus-summary-tick-article-forward t]
2317          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2318          ["Remove marks" gnus-summary-clear-mark-forward t]
2319          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2320          ["Set bookmark" gnus-summary-set-bookmark t]
2321          ["Remove bookmark" gnus-summary-remove-bookmark t])
2322         ("Limit to"
2323          ["Marks..." gnus-summary-limit-to-marks t]
2324          ["Subject..." gnus-summary-limit-to-subject t]
2325          ["Author..." gnus-summary-limit-to-author t]
2326          ["Age..." gnus-summary-limit-to-age t]
2327          ["Extra..." gnus-summary-limit-to-extra t]
2328          ["Score..." gnus-summary-limit-to-score t]
2329          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2330          ["Unread" gnus-summary-limit-to-unread t]
2331          ["Unseen" gnus-summary-limit-to-unseen t]
2332          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2333          ["Next articles" gnus-summary-limit-to-articles t]
2334          ["Pop limit" gnus-summary-pop-limit t]
2335          ["Show dormant" gnus-summary-limit-include-dormant t]
2336          ["Hide childless dormant"
2337           gnus-summary-limit-exclude-childless-dormant t]
2338          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2339          ["Hide marked" gnus-summary-limit-exclude-marks t]
2340          ["Show expunged" gnus-summary-limit-include-expunged t])
2341         ("Process Mark"
2342          ["Set mark" gnus-summary-mark-as-processable t]
2343          ["Remove mark" gnus-summary-unmark-as-processable t]
2344          ["Remove all marks" gnus-summary-unmark-all-processable t]
2345          ["Mark above" gnus-uu-mark-over t]
2346          ["Mark series" gnus-uu-mark-series t]
2347          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2348          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2349          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2350          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2351          ["Mark all" gnus-uu-mark-all t]
2352          ["Mark buffer" gnus-uu-mark-buffer t]
2353          ["Mark sparse" gnus-uu-mark-sparse t]
2354          ["Mark thread" gnus-uu-mark-thread t]
2355          ["Unmark thread" gnus-uu-unmark-thread t]
2356          ("Process Mark Sets"
2357           ["Kill" gnus-summary-kill-process-mark t]
2358           ["Yank" gnus-summary-yank-process-mark
2359            gnus-newsgroup-process-stack]
2360           ["Save" gnus-summary-save-process-mark t]
2361           ["Run command on marked..." gnus-summary-universal-argument t]))
2362         ("Scroll article"
2363          ["Page forward" gnus-summary-next-page
2364           ,@(if (featurep 'xemacs) '(t)
2365               '(:help "Show next page of article"))]
2366          ["Page backward" gnus-summary-prev-page
2367           ,@(if (featurep 'xemacs) '(t)
2368               '(:help "Show previous page of article"))]
2369          ["Line forward" gnus-summary-scroll-up t])
2370         ("Move"
2371          ["Next unread article" gnus-summary-next-unread-article t]
2372          ["Previous unread article" gnus-summary-prev-unread-article t]
2373          ["Next article" gnus-summary-next-article t]
2374          ["Previous article" gnus-summary-prev-article t]
2375          ["Next unread subject" gnus-summary-next-unread-subject t]
2376          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2377          ["Next article same subject" gnus-summary-next-same-subject t]
2378          ["Previous article same subject" gnus-summary-prev-same-subject t]
2379          ["First unread article" gnus-summary-first-unread-article t]
2380          ["Best unread article" gnus-summary-best-unread-article t]
2381          ["Go to subject number..." gnus-summary-goto-subject t]
2382          ["Go to article number..." gnus-summary-goto-article t]
2383          ["Go to the last article" gnus-summary-goto-last-article t]
2384          ["Pop article off history" gnus-summary-pop-article t])
2385         ("Sort"
2386          ["Sort by number" gnus-summary-sort-by-number t]
2387          ["Sort by author" gnus-summary-sort-by-author t]
2388          ["Sort by subject" gnus-summary-sort-by-subject t]
2389          ["Sort by date" gnus-summary-sort-by-date t]
2390          ["Sort by score" gnus-summary-sort-by-score t]
2391          ["Sort by lines" gnus-summary-sort-by-lines t]
2392          ["Sort by characters" gnus-summary-sort-by-chars t]
2393          ["Randomize" gnus-summary-sort-by-random t]
2394          ["Original sort" gnus-summary-sort-by-original t])
2395         ("Help"
2396          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2397          ["Describe group" gnus-summary-describe-group t]
2398          ["Fetch charter" gnus-group-fetch-charter
2399           ,@(if (featurep 'xemacs) nil
2400               '(:help "Display the charter of the current group"))]
2401          ["Fetch control message" gnus-group-fetch-control
2402           ,@(if (featurep 'xemacs) nil
2403               '(:help "Display the archived control message for the current group"))]
2404          ["Read manual" gnus-info-find-node t])
2405         ("Modes"
2406          ["Pick and read" gnus-pick-mode t]
2407          ["Binary" gnus-binary-mode t])
2408         ("Regeneration"
2409          ["Regenerate" gnus-summary-prepare t]
2410          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2411          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2412          ["Toggle threading" gnus-summary-toggle-threads t])
2413         ["See old articles" gnus-summary-insert-old-articles t]
2414         ["See new articles" gnus-summary-insert-new-articles t]
2415         ["Filter articles..." gnus-summary-execute-command t]
2416         ["Run command on articles..." gnus-summary-universal-argument t]
2417         ["Search articles forward..." gnus-summary-search-article-forward t]
2418         ["Search articles backward..." gnus-summary-search-article-backward t]
2419         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2420         ["Expand window" gnus-summary-expand-window t]
2421         ["Expire expirable articles" gnus-summary-expire-articles
2422          (gnus-check-backend-function
2423           'request-expire-articles gnus-newsgroup-name)]
2424         ["Edit local kill file" gnus-summary-edit-local-kill t]
2425         ["Edit main kill file" gnus-summary-edit-global-kill t]
2426         ["Edit group parameters" gnus-summary-edit-parameters t]
2427         ["Customize group parameters" gnus-summary-customize-parameters t]
2428         ["Send a bug report" gnus-bug t]
2429         ("Exit"
2430          ["Catchup and exit" gnus-summary-catchup-and-exit
2431           ,@(if (featurep 'xemacs) '(t)
2432               '(:help "Mark unread articles in this group as read, then exit"))]
2433          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2434          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2435          ["Exit group" gnus-summary-exit
2436           ,@(if (featurep 'xemacs) '(t)
2437               '(:help "Exit current group, return to group selection mode"))]
2438          ["Exit group without updating" gnus-summary-exit-no-update t]
2439          ["Exit and goto next group" gnus-summary-next-group t]
2440          ["Exit and goto prev group" gnus-summary-prev-group t]
2441          ["Reselect group" gnus-summary-reselect-current-group t]
2442          ["Rescan group" gnus-summary-rescan-group t]
2443          ["Update dribble" gnus-summary-save-newsrc t])))
2444
2445     (gnus-run-hooks 'gnus-summary-menu-hook)))
2446
2447 (defvar gnus-summary-tool-bar-map nil)
2448
2449 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2450 (defun gnus-summary-make-tool-bar ()
2451   (if (and (fboundp 'tool-bar-add-item-from-menu)
2452            (default-value 'tool-bar-mode)
2453            (not gnus-summary-tool-bar-map))
2454       (setq gnus-summary-tool-bar-map
2455             (let ((tool-bar-map (make-sparse-keymap))
2456                   (load-path (mm-image-load-path)))
2457               (tool-bar-add-item-from-menu
2458                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2459               (tool-bar-add-item-from-menu
2460                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2461               (tool-bar-add-item-from-menu
2462                'gnus-summary-post-news "post" gnus-summary-mode-map)
2463               (tool-bar-add-item-from-menu
2464                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2465               (tool-bar-add-item-from-menu
2466                'gnus-summary-followup "followup" gnus-summary-mode-map)
2467               (tool-bar-add-item-from-menu
2468                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2469               (tool-bar-add-item-from-menu
2470                'gnus-summary-reply "reply" gnus-summary-mode-map)
2471               (tool-bar-add-item-from-menu
2472                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2473               (tool-bar-add-item-from-menu
2474                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2475               (tool-bar-add-item-from-menu
2476                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2477               (tool-bar-add-item-from-menu
2478                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2479               (tool-bar-add-item-from-menu
2480                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2481               (tool-bar-add-item-from-menu
2482                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2483               (tool-bar-add-item-from-menu
2484                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2485               (tool-bar-add-item-from-menu
2486                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2487               tool-bar-map)))
2488   (if gnus-summary-tool-bar-map
2489       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2490
2491 (defun gnus-score-set-default (var value)
2492   "A version of set that updates the GNU Emacs menu-bar."
2493   (set var value)
2494   ;; It is the message that forces the active status to be updated.
2495   (message ""))
2496
2497 (defun gnus-make-score-map (type)
2498   "Make a summary score map of type TYPE."
2499   (if t
2500       nil
2501     (let ((headers '(("author" "from" string)
2502                      ("subject" "subject" string)
2503                      ("article body" "body" string)
2504                      ("article head" "head" string)
2505                      ("xref" "xref" string)
2506                      ("extra header" "extra" string)
2507                      ("lines" "lines" number)
2508                      ("followups to author" "followup" string)))
2509           (types '((number ("less than" <)
2510                            ("greater than" >)
2511                            ("equal" =))
2512                    (string ("substring" s)
2513                            ("exact string" e)
2514                            ("fuzzy string" f)
2515                            ("regexp" r))))
2516           (perms '(("temporary" (current-time-string))
2517                    ("permanent" nil)
2518                    ("immediate" now)))
2519           header)
2520       (list
2521        (apply
2522         'nconc
2523         (list
2524          (if (eq type 'lower)
2525              "Lower score"
2526            "Increase score"))
2527         (let (outh)
2528           (while headers
2529             (setq header (car headers))
2530             (setq outh
2531                   (cons
2532                    (apply
2533                     'nconc
2534                     (list (car header))
2535                     (let ((ts (cdr (assoc (nth 2 header) types)))
2536                           outt)
2537                       (while ts
2538                         (setq outt
2539                               (cons
2540                                (apply
2541                                 'nconc
2542                                 (list (caar ts))
2543                                 (let ((ps perms)
2544                                       outp)
2545                                   (while ps
2546                                     (setq outp
2547                                           (cons
2548                                            (vector
2549                                             (caar ps)
2550                                             (list
2551                                              'gnus-summary-score-entry
2552                                              (nth 1 header)
2553                                              (if (or (string= (nth 1 header)
2554                                                               "head")
2555                                                      (string= (nth 1 header)
2556                                                               "body"))
2557                                                  ""
2558                                                (list 'gnus-summary-header
2559                                                      (nth 1 header)))
2560                                              (list 'quote (nth 1 (car ts)))
2561                                              (list 'gnus-score-delta-default
2562                                                    nil)
2563                                              (nth 1 (car ps))
2564                                              t)
2565                                             t)
2566                                            outp))
2567                                     (setq ps (cdr ps)))
2568                                   (list (nreverse outp))))
2569                                outt))
2570                         (setq ts (cdr ts)))
2571                       (list (nreverse outt))))
2572                    outh))
2573             (setq headers (cdr headers)))
2574           (list (nreverse outh))))))))
2575
2576 \f
2577
2578 (defun gnus-summary-mode (&optional group)
2579   "Major mode for reading articles.
2580
2581 All normal editing commands are switched off.
2582 \\<gnus-summary-mode-map>
2583 Each line in this buffer represents one article.  To read an
2584 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2585 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2586 respectively.
2587
2588 You can also post articles and send mail from this buffer.  To
2589 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2590 of an article, type `\\[gnus-summary-reply]'.
2591
2592 There are approx. one gazillion commands you can execute in this
2593 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2594
2595 The following commands are available:
2596
2597 \\{gnus-summary-mode-map}"
2598   (interactive)
2599   (kill-all-local-variables)
2600   (when (gnus-visual-p 'summary-menu 'menu)
2601     (gnus-summary-make-menu-bar)
2602     (gnus-summary-make-tool-bar))
2603   (gnus-summary-make-local-variables)
2604   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2605     (gnus-summary-make-local-variables))
2606   (gnus-make-thread-indent-array)
2607   (gnus-simplify-mode-line)
2608   (setq major-mode 'gnus-summary-mode)
2609   (setq mode-name "Summary")
2610   (make-local-variable 'minor-mode-alist)
2611   (use-local-map gnus-summary-mode-map)
2612   (buffer-disable-undo)
2613   (setq buffer-read-only t)             ;Disable modification
2614   (setq truncate-lines t)
2615   (setq selective-display t)
2616   (setq selective-display-ellipses t)   ;Display `...'
2617   (gnus-summary-set-display-table)
2618   (gnus-set-default-directory)
2619   (setq gnus-newsgroup-name group)
2620   (make-local-variable 'gnus-summary-line-format)
2621   (make-local-variable 'gnus-summary-line-format-spec)
2622   (make-local-variable 'gnus-summary-dummy-line-format)
2623   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2624   (make-local-variable 'gnus-summary-mark-positions)
2625   (make-local-hook 'pre-command-hook)
2626   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2627   (gnus-run-hooks 'gnus-summary-mode-hook)
2628   (turn-on-gnus-mailing-list-mode)
2629   (mm-enable-multibyte)
2630   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2631   (gnus-update-summary-mark-positions))
2632
2633 (defun gnus-summary-make-local-variables ()
2634   "Make all the local summary buffer variables."
2635   (let (global)
2636     (dolist (local gnus-summary-local-variables)
2637       (if (consp local)
2638           (progn
2639             (if (eq (cdr local) 'global)
2640                 ;; Copy the global value of the variable.
2641                 (setq global (symbol-value (car local)))
2642               ;; Use the value from the list.
2643               (setq global (eval (cdr local))))
2644             (set (make-local-variable (car local)) global))
2645         ;; Simple nil-valued local variable.
2646         (set (make-local-variable local) nil)))))
2647
2648 (defun gnus-summary-clear-local-variables ()
2649   (let ((locals gnus-summary-local-variables))
2650     (while locals
2651       (if (consp (car locals))
2652           (and (vectorp (caar locals))
2653                (set (caar locals) nil))
2654         (and (vectorp (car locals))
2655              (set (car locals) nil)))
2656       (setq locals (cdr locals)))))
2657
2658 ;; Summary data functions.
2659
2660 (defmacro gnus-data-number (data)
2661   `(car ,data))
2662
2663 (defmacro gnus-data-set-number (data number)
2664   `(setcar ,data ,number))
2665
2666 (defmacro gnus-data-mark (data)
2667   `(nth 1 ,data))
2668
2669 (defmacro gnus-data-set-mark (data mark)
2670   `(setcar (nthcdr 1 ,data) ,mark))
2671
2672 (defmacro gnus-data-pos (data)
2673   `(nth 2 ,data))
2674
2675 (defmacro gnus-data-set-pos (data pos)
2676   `(setcar (nthcdr 2 ,data) ,pos))
2677
2678 (defmacro gnus-data-header (data)
2679   `(nth 3 ,data))
2680
2681 (defmacro gnus-data-set-header (data header)
2682   `(setf (nth 3 ,data) ,header))
2683
2684 (defmacro gnus-data-level (data)
2685   `(nth 4 ,data))
2686
2687 (defmacro gnus-data-unread-p (data)
2688   `(= (nth 1 ,data) gnus-unread-mark))
2689
2690 (defmacro gnus-data-read-p (data)
2691   `(/= (nth 1 ,data) gnus-unread-mark))
2692
2693 (defmacro gnus-data-pseudo-p (data)
2694   `(consp (nth 3 ,data)))
2695
2696 (defmacro gnus-data-find (number)
2697   `(assq ,number gnus-newsgroup-data))
2698
2699 (defmacro gnus-data-find-list (number &optional data)
2700   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2701      (memq (assq ,number bdata)
2702            bdata)))
2703
2704 (defmacro gnus-data-make (number mark pos header level)
2705   `(list ,number ,mark ,pos ,header ,level))
2706
2707 (defun gnus-data-enter (after-article number mark pos header level offset)
2708   (let ((data (gnus-data-find-list after-article)))
2709     (unless data
2710       (error "No such article: %d" after-article))
2711     (setcdr data (cons (gnus-data-make number mark pos header level)
2712                        (cdr data)))
2713     (setq gnus-newsgroup-data-reverse nil)
2714     (gnus-data-update-list (cddr data) offset)))
2715
2716 (defun gnus-data-enter-list (after-article list &optional offset)
2717   (when list
2718     (let ((data (and after-article (gnus-data-find-list after-article)))
2719           (ilist list))
2720       (if (not (or data
2721                    after-article))
2722           (let ((odata gnus-newsgroup-data))
2723             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2724             (when offset
2725               (gnus-data-update-list odata offset)))
2726       ;; Find the last element in the list to be spliced into the main
2727         ;; list.
2728         (while (cdr list)
2729           (setq list (cdr list)))
2730         (if (not data)
2731             (progn
2732               (setcdr list gnus-newsgroup-data)
2733               (setq gnus-newsgroup-data ilist)
2734               (when offset
2735                 (gnus-data-update-list (cdr list) offset)))
2736           (setcdr list (cdr data))
2737           (setcdr data ilist)
2738           (when offset
2739             (gnus-data-update-list (cdr list) offset))))
2740       (setq gnus-newsgroup-data-reverse nil))))
2741
2742 (defun gnus-data-remove (article &optional offset)
2743   (let ((data gnus-newsgroup-data))
2744     (if (= (gnus-data-number (car data)) article)
2745         (progn
2746           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2747                 gnus-newsgroup-data-reverse nil)
2748           (when offset
2749             (gnus-data-update-list gnus-newsgroup-data offset)))
2750       (while (cdr data)
2751         (when (= (gnus-data-number (cadr data)) article)
2752           (setcdr data (cddr data))
2753           (when offset
2754             (gnus-data-update-list (cdr data) offset))
2755           (setq data nil
2756                 gnus-newsgroup-data-reverse nil))
2757         (setq data (cdr data))))))
2758
2759 (defmacro gnus-data-list (backward)
2760   `(if ,backward
2761        (or gnus-newsgroup-data-reverse
2762            (setq gnus-newsgroup-data-reverse
2763                  (reverse gnus-newsgroup-data)))
2764      gnus-newsgroup-data))
2765
2766 (defun gnus-data-update-list (data offset)
2767   "Add OFFSET to the POS of all data entries in DATA."
2768   (setq gnus-newsgroup-data-reverse nil)
2769   (while data
2770     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2771     (setq data (cdr data))))
2772
2773 (defun gnus-summary-article-pseudo-p (article)
2774   "Say whether this article is a pseudo article or not."
2775   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2776
2777 (defmacro gnus-summary-article-sparse-p (article)
2778   "Say whether this article is a sparse article or not."
2779   `(memq ,article gnus-newsgroup-sparse))
2780
2781 (defmacro gnus-summary-article-ancient-p (article)
2782   "Say whether this article is a sparse article or not."
2783   `(memq ,article gnus-newsgroup-ancient))
2784
2785 (defun gnus-article-parent-p (number)
2786   "Say whether this article is a parent or not."
2787   (let ((data (gnus-data-find-list number)))
2788     (and (cdr data)              ; There has to be an article after...
2789          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2790             (gnus-data-level (nth 1 data))))))
2791
2792 (defun gnus-article-children (number)
2793   "Return a list of all children to NUMBER."
2794   (let* ((data (gnus-data-find-list number))
2795          (level (gnus-data-level (car data)))
2796          children)
2797     (setq data (cdr data))
2798     (while (and data
2799                 (= (gnus-data-level (car data)) (1+ level)))
2800       (push (gnus-data-number (car data)) children)
2801       (setq data (cdr data)))
2802     children))
2803
2804 (defmacro gnus-summary-skip-intangible ()
2805   "If the current article is intangible, then jump to a different article."
2806   '(let ((to (get-text-property (point) 'gnus-intangible)))
2807      (and to (gnus-summary-goto-subject to))))
2808
2809 (defmacro gnus-summary-article-intangible-p ()
2810   "Say whether this article is intangible or not."
2811   '(get-text-property (point) 'gnus-intangible))
2812
2813 (defun gnus-article-read-p (article)
2814   "Say whether ARTICLE is read or not."
2815   (not (or (memq article gnus-newsgroup-marked)
2816            (memq article gnus-newsgroup-spam-marked)
2817            (memq article gnus-newsgroup-unreads)
2818            (memq article gnus-newsgroup-unselected)
2819            (memq article gnus-newsgroup-dormant))))
2820
2821 ;; Some summary mode macros.
2822
2823 (defmacro gnus-summary-article-number ()
2824   "The article number of the article on the current line.
2825 If there isn't an article number here, then we return the current
2826 article number."
2827   '(progn
2828      (gnus-summary-skip-intangible)
2829      (or (get-text-property (point) 'gnus-number)
2830          (gnus-summary-last-subject))))
2831
2832 (defmacro gnus-summary-article-header (&optional number)
2833   "Return the header of article NUMBER."
2834   `(gnus-data-header (gnus-data-find
2835                       ,(or number '(gnus-summary-article-number)))))
2836
2837 (defmacro gnus-summary-thread-level (&optional number)
2838   "Return the level of thread that starts with article NUMBER."
2839   `(if (and (eq gnus-summary-make-false-root 'dummy)
2840             (get-text-property (point) 'gnus-intangible))
2841        0
2842      (gnus-data-level (gnus-data-find
2843                        ,(or number '(gnus-summary-article-number))))))
2844
2845 (defmacro gnus-summary-article-mark (&optional number)
2846   "Return the mark of article NUMBER."
2847   `(gnus-data-mark (gnus-data-find
2848                     ,(or number '(gnus-summary-article-number)))))
2849
2850 (defmacro gnus-summary-article-pos (&optional number)
2851   "Return the position of the line of article NUMBER."
2852   `(gnus-data-pos (gnus-data-find
2853                    ,(or number '(gnus-summary-article-number)))))
2854
2855 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2856 (defmacro gnus-summary-article-subject (&optional number)
2857   "Return current subject string or nil if nothing."
2858   `(let ((headers
2859           ,(if number
2860                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2861              '(gnus-data-header (assq (gnus-summary-article-number)
2862                                       gnus-newsgroup-data)))))
2863      (and headers
2864           (vectorp headers)
2865           (mail-header-subject headers))))
2866
2867 (defmacro gnus-summary-article-score (&optional number)
2868   "Return current article score."
2869   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2870                   gnus-newsgroup-scored))
2871        gnus-summary-default-score 0))
2872
2873 (defun gnus-summary-article-children (&optional number)
2874   "Return a list of article numbers that are children of article NUMBER."
2875   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2876          (level (gnus-data-level (car data)))
2877          l children)
2878     (while (and (setq data (cdr data))
2879                 (> (setq l (gnus-data-level (car data))) level))
2880       (and (= (1+ level) l)
2881            (push (gnus-data-number (car data))
2882                  children)))
2883     (nreverse children)))
2884
2885 (defun gnus-summary-article-parent (&optional number)
2886   "Return the article number of the parent of article NUMBER."
2887   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2888                                     (gnus-data-list t)))
2889          (level (gnus-data-level (car data))))
2890     (if (zerop level)
2891         ()                              ; This is a root.
2892       ;; We search until we find an article with a level less than
2893       ;; this one.  That function has to be the parent.
2894       (while (and (setq data (cdr data))
2895                   (not (< (gnus-data-level (car data)) level))))
2896       (and data (gnus-data-number (car data))))))
2897
2898 (defun gnus-unread-mark-p (mark)
2899   "Say whether MARK is the unread mark."
2900   (= mark gnus-unread-mark))
2901
2902 (defun gnus-read-mark-p (mark)
2903   "Say whether MARK is one of the marks that mark as read.
2904 This is all marks except unread, ticked, dormant, and expirable."
2905   (not (or (= mark gnus-unread-mark)
2906            (= mark gnus-ticked-mark)
2907            (= mark gnus-spam-mark)
2908            (= mark gnus-dormant-mark)
2909            (= mark gnus-expirable-mark))))
2910
2911 (defmacro gnus-article-mark (number)
2912   "Return the MARK of article NUMBER.
2913 This macro should only be used when computing the mark the \"first\"
2914 time; i.e., when generating the summary lines.  After that,
2915 `gnus-summary-article-mark' should be used to examine the
2916 marks of articles."
2917   `(cond
2918     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2919     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2920     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2921     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2922     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2923     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2924     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2925     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2926            gnus-ancient-mark))))
2927
2928 ;; Saving hidden threads.
2929
2930 (defmacro gnus-save-hidden-threads (&rest forms)
2931   "Save hidden threads, eval FORMS, and restore the hidden threads."
2932   (let ((config (make-symbol "config")))
2933     `(let ((,config (gnus-hidden-threads-configuration)))
2934        (unwind-protect
2935            (save-excursion
2936              ,@forms)
2937          (gnus-restore-hidden-threads-configuration ,config)))))
2938 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2939 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2940
2941 (defun gnus-data-compute-positions ()
2942   "Compute the positions of all articles."
2943   (setq gnus-newsgroup-data-reverse nil)
2944   (let ((data gnus-newsgroup-data))
2945     (save-excursion
2946       (gnus-save-hidden-threads
2947         (gnus-summary-show-all-threads)
2948         (goto-char (point-min))
2949         (while data
2950           (while (get-text-property (point) 'gnus-intangible)
2951             (forward-line 1))
2952           (gnus-data-set-pos (car data) (+ (point) 3))
2953           (setq data (cdr data))
2954           (forward-line 1))))))
2955
2956 (defun gnus-hidden-threads-configuration ()
2957   "Return the current hidden threads configuration."
2958   (save-excursion
2959     (let (config)
2960       (goto-char (point-min))
2961       (while (search-forward "\r" nil t)
2962         (push (1- (point)) config))
2963       config)))
2964
2965 (defun gnus-restore-hidden-threads-configuration (config)
2966   "Restore hidden threads configuration from CONFIG."
2967   (save-excursion
2968     (let (point buffer-read-only)
2969       (while (setq point (pop config))
2970         (when (and (< point (point-max))
2971                    (goto-char point)
2972                    (eq (char-after) ?\n))
2973           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2974
2975 ;; Various summary mode internalish functions.
2976
2977 (defun gnus-mouse-pick-article (e)
2978   (interactive "e")
2979   (mouse-set-point e)
2980   (gnus-summary-next-page nil t))
2981
2982 (defun gnus-summary-set-display-table ()
2983   "Change the display table.
2984 Odd characters have a tendency to mess
2985 up nicely formatted displays - we make all possible glyphs
2986 display only a single character."
2987
2988   ;; We start from the standard display table, if any.
2989   (let ((table (or (copy-sequence standard-display-table)
2990                    (make-display-table)))
2991         (i 32))
2992     ;; Nix out all the control chars...
2993     (while (>= (setq i (1- i)) 0)
2994       (aset table i [??]))
2995    ;; ... but not newline and cr, of course.  (cr is necessary for the
2996     ;; selective display).
2997     (aset table ?\n nil)
2998     (aset table ?\r nil)
2999     ;; We keep TAB as well.
3000     (aset table ?\t nil)
3001     ;; We nix out any glyphs over 126 that are not set already.
3002     (let ((i 256))
3003       (while (>= (setq i (1- i)) 127)
3004         ;; Only modify if the entry is nil.
3005         (unless (aref table i)
3006           (aset table i [??]))))
3007     (setq buffer-display-table table)))
3008
3009 (defun gnus-summary-set-article-display-arrow (pos)
3010   "Update the overlay arrow to point to line at position POS."
3011   (when (and gnus-summary-display-arrow
3012              (boundp 'overlay-arrow-position)
3013              (boundp 'overlay-arrow-string))
3014     (save-excursion
3015       (goto-char pos)
3016       (beginning-of-line)
3017       (unless overlay-arrow-position
3018         (setq overlay-arrow-position (make-marker)))
3019       (setq overlay-arrow-string "=>"
3020             overlay-arrow-position (set-marker overlay-arrow-position
3021                                                (point)
3022                                                (current-buffer))))))
3023
3024 (defun gnus-summary-buffer-name (group)
3025   "Return the summary buffer name of GROUP."
3026   (concat "*Summary " (gnus-group-decoded-name group) "*"))
3027
3028 (defun gnus-summary-setup-buffer (group)
3029   "Initialize summary buffer."
3030   (let ((buffer (gnus-summary-buffer-name group))
3031         (dead-name (concat "*Dead Summary "
3032                            (gnus-group-decoded-name group) "*")))
3033     ;; If a dead summary buffer exists, we kill it.
3034     (when (gnus-buffer-live-p dead-name)
3035       (gnus-kill-buffer dead-name))
3036     (if (get-buffer buffer)
3037         (progn
3038           (set-buffer buffer)
3039           (setq gnus-summary-buffer (current-buffer))
3040           (not gnus-newsgroup-prepared))
3041       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3042       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3043       (gnus-summary-mode group)
3044       (when gnus-carpal
3045         (gnus-carpal-setup-buffer 'summary))
3046       (unless gnus-single-article-buffer
3047         (make-local-variable 'gnus-article-buffer)
3048         (make-local-variable 'gnus-article-current)
3049         (make-local-variable 'gnus-original-article-buffer))
3050       (setq gnus-newsgroup-name group)
3051       ;; Set any local variables in the group parameters.
3052       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3053       t)))
3054
3055 (defun gnus-set-global-variables ()
3056   "Set the global equivalents of the buffer-local variables.
3057 They are set to the latest values they had.  These reflect the summary
3058 buffer that was in action when the last article was fetched."
3059   (when (eq major-mode 'gnus-summary-mode)
3060     (setq gnus-summary-buffer (current-buffer))
3061     (let ((name gnus-newsgroup-name)
3062           (marked gnus-newsgroup-marked)
3063           (spam gnus-newsgroup-spam-marked)
3064           (unread gnus-newsgroup-unreads)
3065           (headers gnus-current-headers)
3066           (data gnus-newsgroup-data)
3067           (summary gnus-summary-buffer)
3068           (article-buffer gnus-article-buffer)
3069           (original gnus-original-article-buffer)
3070           (gac gnus-article-current)
3071           (reffed gnus-reffed-article-number)
3072           (score-file gnus-current-score-file)
3073           (default-charset gnus-newsgroup-charset)
3074           vlist)
3075       (let ((locals gnus-newsgroup-variables))
3076         (while locals
3077           (if (consp (car locals))
3078               (push (eval (caar locals)) vlist)
3079             (push (eval (car locals)) vlist))
3080           (setq locals (cdr locals)))
3081         (setq vlist (nreverse vlist)))
3082       (save-excursion
3083         (set-buffer gnus-group-buffer)
3084         (setq gnus-newsgroup-name name
3085               gnus-newsgroup-marked marked
3086               gnus-newsgroup-spam-marked spam
3087               gnus-newsgroup-unreads unread
3088               gnus-current-headers headers
3089               gnus-newsgroup-data data
3090               gnus-article-current gac
3091               gnus-summary-buffer summary
3092               gnus-article-buffer article-buffer
3093               gnus-original-article-buffer original
3094               gnus-reffed-article-number reffed
3095               gnus-current-score-file score-file
3096               gnus-newsgroup-charset default-charset)
3097         (let ((locals gnus-newsgroup-variables))
3098           (while locals
3099             (if (consp (car locals))
3100                 (set (caar locals) (pop vlist))
3101               (set (car locals) (pop vlist)))
3102             (setq locals (cdr locals))))
3103         ;; The article buffer also has local variables.
3104         (when (gnus-buffer-live-p gnus-article-buffer)
3105           (set-buffer gnus-article-buffer)
3106           (setq gnus-summary-buffer summary))))))
3107
3108 (defun gnus-summary-article-unread-p (article)
3109   "Say whether ARTICLE is unread or not."
3110   (memq article gnus-newsgroup-unreads))
3111
3112 (defun gnus-summary-first-article-p (&optional article)
3113   "Return whether ARTICLE is the first article in the buffer."
3114   (if (not (setq article (or article (gnus-summary-article-number))))
3115       nil
3116     (eq article (caar gnus-newsgroup-data))))
3117
3118 (defun gnus-summary-last-article-p (&optional article)
3119   "Return whether ARTICLE is the last article in the buffer."
3120   (if (not (setq article (or article (gnus-summary-article-number))))
3121       ;; All non-existent numbers are the last article.  :-)
3122       t
3123     (not (cdr (gnus-data-find-list article)))))
3124
3125 (defun gnus-make-thread-indent-array ()
3126   (let ((n 200))
3127     (unless (and gnus-thread-indent-array
3128                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3129       (setq gnus-thread-indent-array (make-vector 201 "")
3130             gnus-thread-indent-array-level gnus-thread-indent-level)
3131       (while (>= n 0)
3132         (aset gnus-thread-indent-array n
3133               (make-string (* n gnus-thread-indent-level) ? ))
3134         (setq n (1- n))))))
3135
3136 (defun gnus-update-summary-mark-positions ()
3137   "Compute where the summary marks are to go."
3138   (save-excursion
3139     (when (gnus-buffer-exists-p gnus-summary-buffer)
3140       (set-buffer gnus-summary-buffer))
3141     (let ((gnus-replied-mark 129)
3142           (gnus-score-below-mark 130)
3143           (gnus-score-over-mark 130)
3144           (gnus-undownloaded-mark 131)
3145           (spec gnus-summary-line-format-spec)
3146           gnus-visual pos)
3147       (save-excursion
3148         (gnus-set-work-buffer)
3149         (let ((gnus-summary-line-format-spec spec)
3150               (gnus-newsgroup-downloadable '(0)))
3151           (gnus-summary-insert-line
3152            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3153            0 nil t 128 t nil "" nil 1)
3154           (goto-char (point-min))
3155           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3156                                              (- (point) (point-min) 1)))))
3157           (goto-char (point-min))
3158           (push (cons 'replied (and (search-forward "\201" nil t)
3159                                     (- (point) (point-min) 1)))
3160                 pos)
3161           (goto-char (point-min))
3162           (push (cons 'score (and (search-forward "\202" nil t)
3163                                   (- (point) (point-min) 1)))
3164                 pos)
3165           (goto-char (point-min))
3166           (push (cons 'download
3167                       (and (search-forward "\203" nil t)
3168                            (- (point) (point-min) 1)))
3169                 pos)))
3170       (setq gnus-summary-mark-positions pos))))
3171
3172 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3173   "Insert a dummy root in the summary buffer."
3174   (beginning-of-line)
3175   (gnus-add-text-properties
3176    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3177    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3178
3179 (defun gnus-summary-extract-address-component (from)
3180   (or (car (funcall gnus-extract-address-components from))
3181       from))
3182
3183 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3184   (let ((mail-parse-charset gnus-newsgroup-charset)
3185         ; Is it really necessary to do this next part for each summary line?
3186         ; Luckily, doesn't seem to slow things down much.
3187         (mail-parse-ignored-charsets
3188          (save-excursion (set-buffer gnus-summary-buffer)
3189                          gnus-newsgroup-ignored-charsets)))
3190     (or
3191      (and gnus-ignored-from-addresses
3192           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3193           (let ((extra-headers (mail-header-extra header))
3194                 to
3195                 newsgroups)
3196             (cond
3197              ((setq to (cdr (assq 'To extra-headers)))
3198               (concat "-> "
3199                       (inline
3200                         (gnus-summary-extract-address-component
3201                          (funcall gnus-decode-encoded-word-function to)))))
3202              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3203               (concat "=> " newsgroups)))))
3204      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3205
3206 (defun gnus-summary-insert-line (gnus-tmp-header
3207                                  gnus-tmp-level gnus-tmp-current
3208                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3209                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3210                                  &optional gnus-tmp-dummy gnus-tmp-score
3211                                  gnus-tmp-process)
3212   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3213          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3214          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3215          (gnus-tmp-score-char
3216           (if (or (null gnus-summary-default-score)
3217                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3218                       gnus-summary-zcore-fuzz))
3219               ?                         ;Whitespace
3220             (if (< gnus-tmp-score gnus-summary-default-score)
3221                 gnus-score-below-mark gnus-score-over-mark)))
3222          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3223          (gnus-tmp-replied
3224           (cond (gnus-tmp-process gnus-process-mark)
3225                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3226                  gnus-cached-mark)
3227                 (gnus-tmp-replied gnus-replied-mark)
3228                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3229                  gnus-forwarded-mark)
3230                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3231                  gnus-saved-mark)
3232                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3233                  gnus-recent-mark)
3234                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3235                  gnus-unseen-mark)
3236                 (t gnus-no-mark)))
3237          (gnus-tmp-downloaded
3238           (cond (undownloaded 
3239                  gnus-undownloaded-mark)
3240                 (gnus-newsgroup-agentized
3241                  gnus-downloaded-mark)
3242                 (t
3243                  gnus-no-mark)))
3244          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3245          (gnus-tmp-name
3246           (cond
3247            ((string-match "<[^>]+> *$" gnus-tmp-from)
3248             (let ((beg (match-beginning 0)))
3249               (or (and (string-match "^\".+\"" gnus-tmp-from)
3250                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3251                   (substring gnus-tmp-from 0 beg))))
3252            ((string-match "(.+)" gnus-tmp-from)
3253             (substring gnus-tmp-from
3254                        (1+ (match-beginning 0)) (1- (match-end 0))))
3255            (t gnus-tmp-from)))
3256          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3257          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3258          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3259          (buffer-read-only nil))
3260     (when (string= gnus-tmp-name "")
3261       (setq gnus-tmp-name gnus-tmp-from))
3262     (unless (numberp gnus-tmp-lines)
3263       (setq gnus-tmp-lines -1))
3264     (if (= gnus-tmp-lines -1)
3265         (setq gnus-tmp-lines "?")
3266       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines))) 
3267       (gnus-put-text-property
3268      (point)
3269      (progn (eval gnus-summary-line-format-spec) (point))
3270        'gnus-number gnus-tmp-number)
3271     (when (gnus-visual-p 'summary-highlight 'highlight)
3272       (forward-line -1)
3273       (gnus-run-hooks 'gnus-summary-update-hook)
3274       (forward-line 1))))
3275
3276 (defun gnus-summary-update-line (&optional dont-update)
3277   "Update summary line after change."
3278   (when (and gnus-summary-default-score
3279              (not gnus-summary-inhibit-highlight))
3280     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3281            (article (gnus-summary-article-number))
3282            (score (gnus-summary-article-score article)))
3283       (unless dont-update
3284         (if (and gnus-summary-mark-below
3285                  (< (gnus-summary-article-score)
3286                     gnus-summary-mark-below))
3287             ;; This article has a low score, so we mark it as read.
3288             (when (memq article gnus-newsgroup-unreads)
3289               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3290           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3291             ;; This article was previously marked as read on account
3292             ;; of a low score, but now it has risen, so we mark it as
3293             ;; unread.
3294             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3295         (gnus-summary-update-mark
3296          (if (or (null gnus-summary-default-score)
3297                  (<= (abs (- score gnus-summary-default-score))
3298                      gnus-summary-zcore-fuzz))
3299              ?                          ;Whitespace
3300            (if (< score gnus-summary-default-score)
3301                gnus-score-below-mark gnus-score-over-mark))
3302          'score))
3303       ;; Do visual highlighting.
3304       (when (gnus-visual-p 'summary-highlight 'highlight)
3305         (gnus-run-hooks 'gnus-summary-update-hook)))))
3306
3307 (defvar gnus-tmp-new-adopts nil)
3308
3309 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3310   "Return the number of articles in THREAD.
3311 This may be 0 in some cases -- if none of the articles in
3312 the thread are to be displayed."
3313   (let* ((number
3314          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3315           (cond
3316            ((not (listp thread))
3317             1)
3318            ((and (consp thread) (cdr thread))
3319             (apply
3320              '+ 1 (mapcar
3321                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3322            ((null thread)
3323             1)
3324            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3325             1)
3326            (t 0))))
3327     (when (and level (zerop level) gnus-tmp-new-adopts)
3328       (incf number
3329             (apply '+ (mapcar
3330                        'gnus-summary-number-of-articles-in-thread
3331                        gnus-tmp-new-adopts))))
3332     (if char
3333         (if (> number 1) gnus-not-empty-thread-mark
3334           gnus-empty-thread-mark)
3335       number)))
3336
3337 (defsubst gnus-summary-line-message-size (head)
3338   "Return pretty-printed version of message size.
3339 This function is intended to be used in
3340 `gnus-summary-line-format-alist', which see."
3341   (let ((c (or (mail-header-chars head) -1)))
3342     (cond ((< c 0) "n/a")               ; chars not available
3343           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3344           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3345           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3346           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3347
3348
3349 (defun gnus-summary-set-local-parameters (group)
3350   "Go through the local params of GROUP and set all variable specs in that list."
3351   (let ((params (gnus-group-find-parameter group))
3352         (vars '(quit-config))           ; Ignore quit-config.
3353         elem)
3354     (while params
3355       (setq elem (car params)
3356             params (cdr params))
3357       (and (consp elem)                 ; Has to be a cons.
3358            (consp (cdr elem))           ; The cdr has to be a list.
3359            (symbolp (car elem))         ; Has to be a symbol in there.
3360            (not (memq (car elem) vars))
3361            (ignore-errors               ; So we set it.
3362              (push (car elem) vars)
3363              (make-local-variable (car elem))
3364              (set (car elem) (eval (nth 1 elem))))))))
3365
3366 (defun gnus-summary-read-group (group &optional show-all no-article
3367                                       kill-buffer no-display backward
3368                                       select-articles)
3369   "Start reading news in newsgroup GROUP.
3370 If SHOW-ALL is non-nil, already read articles are also listed.
3371 If NO-ARTICLE is non-nil, no article is selected initially.
3372 If NO-DISPLAY, don't generate a summary buffer."
3373   (let (result)
3374     (while (and group
3375                 (null (setq result
3376                             (let ((gnus-auto-select-next nil))
3377                               (or (gnus-summary-read-group-1
3378                                    group show-all no-article
3379                                    kill-buffer no-display
3380                                    select-articles)
3381                                   (setq show-all nil
3382                                         select-articles nil)))))
3383                 (eq gnus-auto-select-next 'quietly))
3384       (set-buffer gnus-group-buffer)
3385       ;; The entry function called above goes to the next
3386       ;; group automatically, so we go two groups back
3387       ;; if we are searching for the previous group.
3388       (when backward
3389         (gnus-group-prev-unread-group 2))
3390       (if (not (equal group (gnus-group-group-name)))
3391           (setq group (gnus-group-group-name))
3392         (setq group nil)))
3393     result))
3394
3395 (defun gnus-summary-read-group-1 (group show-all no-article
3396                                         kill-buffer no-display
3397                                         &optional select-articles)
3398   ;; Killed foreign groups can't be entered.
3399   ;;  (when (and (not (gnus-group-native-p group))
3400   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3401   ;;    (error "Dead non-native groups can't be entered"))
3402   (gnus-message 5 "Retrieving newsgroup: %s..."
3403                 (gnus-group-decoded-name group))
3404   (let* ((new-group (gnus-summary-setup-buffer group))
3405          (quit-config (gnus-group-quit-config group))
3406          (did-select (and new-group (gnus-select-newsgroup
3407                                      group show-all select-articles))))
3408     (cond
3409      ;; This summary buffer exists already, so we just select it.
3410      ((not new-group)
3411       (gnus-set-global-variables)
3412       (when kill-buffer
3413         (gnus-kill-or-deaden-summary kill-buffer))
3414       (gnus-configure-windows 'summary 'force)
3415       (gnus-set-mode-line 'summary)
3416       (gnus-summary-position-point)
3417       (message "")
3418       t)
3419      ;; We couldn't select this group.
3420      ((null did-select)
3421       (when (and (eq major-mode 'gnus-summary-mode)
3422                  (not (equal (current-buffer) kill-buffer)))
3423         (kill-buffer (current-buffer))
3424         (if (not quit-config)
3425             (progn
3426               ;; Update the info -- marks might need to be removed,
3427               ;; for instance.
3428               (gnus-summary-update-info)
3429               (set-buffer gnus-group-buffer)
3430               (gnus-group-jump-to-group group)
3431               (gnus-group-next-unread-group 1))
3432           (gnus-handle-ephemeral-exit quit-config)))
3433       (let ((grpinfo (gnus-get-info group)))
3434         (if (null (gnus-info-read grpinfo))
3435             (gnus-message 3 "Group %s contains no messages"
3436                           (gnus-group-decoded-name group))
3437           (gnus-message 3 "Can't select group")))
3438       nil)
3439      ;; The user did a `C-g' while prompting for number of articles,
3440      ;; so we exit this group.
3441      ((eq did-select 'quit)
3442       (and (eq major-mode 'gnus-summary-mode)
3443            (not (equal (current-buffer) kill-buffer))
3444            (kill-buffer (current-buffer)))
3445       (when kill-buffer
3446         (gnus-kill-or-deaden-summary kill-buffer))
3447       (if (not quit-config)
3448           (progn
3449             (set-buffer gnus-group-buffer)
3450             (gnus-group-jump-to-group group)
3451             (gnus-group-next-unread-group 1)
3452             (gnus-configure-windows 'group 'force))
3453         (gnus-handle-ephemeral-exit quit-config))
3454       ;; Finally signal the quit.
3455       (signal 'quit nil))
3456      ;; The group was successfully selected.
3457      (t
3458       (gnus-set-global-variables)
3459       ;; Save the active value in effect when the group was entered.
3460       (setq gnus-newsgroup-active
3461             (gnus-copy-sequence
3462              (gnus-active gnus-newsgroup-name)))
3463       ;; You can change the summary buffer in some way with this hook.
3464       (gnus-run-hooks 'gnus-select-group-hook)
3465       (gnus-update-format-specifications
3466        nil 'summary 'summary-mode 'summary-dummy)
3467       (gnus-update-summary-mark-positions)
3468       ;; Do score processing.
3469       (when gnus-use-scoring
3470         (gnus-possibly-score-headers))
3471       ;; Check whether to fill in the gaps in the threads.
3472       (when gnus-build-sparse-threads
3473         (gnus-build-sparse-threads))
3474       ;; Find the initial limit.
3475       (if gnus-show-threads
3476           (if show-all
3477               (let ((gnus-newsgroup-dormant nil))
3478                 (gnus-summary-initial-limit show-all))
3479             (gnus-summary-initial-limit show-all))
3480         ;; When unthreaded, all articles are always shown.
3481         (setq gnus-newsgroup-limit
3482               (mapcar
3483                (lambda (header) (mail-header-number header))
3484                gnus-newsgroup-headers)))
3485       ;; Generate the summary buffer.
3486       (unless no-display
3487         (gnus-summary-prepare))
3488       (when gnus-use-trees
3489         (gnus-tree-open group)
3490         (setq gnus-summary-highlight-line-function
3491               'gnus-tree-highlight-article))
3492       ;; If the summary buffer is empty, but there are some low-scored
3493       ;; articles or some excluded dormants, we include these in the
3494       ;; buffer.
3495       (when (and (zerop (buffer-size))
3496                  (not no-display))
3497         (cond (gnus-newsgroup-dormant
3498                (gnus-summary-limit-include-dormant))
3499               ((and gnus-newsgroup-scored show-all)
3500                (gnus-summary-limit-include-expunged t))))
3501       ;; Function `gnus-apply-kill-file' must be called in this hook.
3502       (gnus-run-hooks 'gnus-apply-kill-hook)
3503       (if (and (zerop (buffer-size))
3504                (not no-display))
3505           (progn
3506             ;; This newsgroup is empty.
3507             (gnus-summary-catchup-and-exit nil t)
3508             (gnus-message 6 "No unread news")
3509             (when kill-buffer
3510               (gnus-kill-or-deaden-summary kill-buffer))
3511             ;; Return nil from this function.
3512             nil)
3513         ;; Hide conversation thread subtrees.  We cannot do this in
3514         ;; gnus-summary-prepare-hook since kill processing may not
3515         ;; work with hidden articles.
3516         (gnus-summary-maybe-hide-threads)
3517         (when kill-buffer
3518           (gnus-kill-or-deaden-summary kill-buffer))
3519         (gnus-summary-auto-select-subject)
3520         ;; Show first unread article if requested.
3521         (if (and (not no-article)
3522                  (not no-display)
3523                  gnus-newsgroup-unreads
3524                  gnus-auto-select-first)
3525             (progn
3526               (gnus-configure-windows 'summary)
3527               (let ((art (gnus-summary-article-number)))
3528                 (unless (and (not gnus-plugged)
3529                              (or (memq art gnus-newsgroup-undownloaded)
3530                                  (memq art gnus-newsgroup-downloadable)))
3531                   (gnus-summary-goto-article art))))
3532           ;; Don't select any articles.
3533           (gnus-summary-position-point)
3534           (gnus-configure-windows 'summary 'force)
3535           (gnus-set-mode-line 'summary))
3536         (when (get-buffer-window gnus-group-buffer t)
3537           ;; Gotta use windows, because recenter does weird stuff if
3538           ;; the current buffer ain't the displayed window.
3539           (let ((owin (selected-window)))
3540             (select-window (get-buffer-window gnus-group-buffer t))
3541             (when (gnus-group-goto-group group)
3542               (recenter))
3543             (select-window owin)))
3544         ;; Mark this buffer as "prepared".
3545         (setq gnus-newsgroup-prepared t)
3546         (gnus-run-hooks 'gnus-summary-prepared-hook)
3547         (unless (gnus-ephemeral-group-p group)
3548           (gnus-group-update-group group))
3549         t)))))
3550
3551 (defun gnus-summary-auto-select-subject ()
3552   "Select the subject line on initial group entry."
3553   (goto-char (point-min))
3554   (cond
3555    ((eq gnus-auto-select-subject 'best)
3556     (gnus-summary-best-unread-subject))
3557    ((eq gnus-auto-select-subject 'unread)
3558     (gnus-summary-first-unread-subject))
3559    ((eq gnus-auto-select-subject 'unseen)
3560     (gnus-summary-first-unseen-subject))
3561    ((eq gnus-auto-select-subject 'unseen-or-unread)
3562     (gnus-summary-first-unseen-or-unread-subject))
3563    ((eq gnus-auto-select-subject 'first)
3564     ;; Do nothing.
3565     )
3566    ((gnus-functionp gnus-auto-select-subject)
3567     (funcall gnus-auto-select-subject))))
3568
3569 (defun gnus-summary-prepare ()
3570   "Generate the summary buffer."
3571   (interactive)
3572   (let ((buffer-read-only nil))
3573     (erase-buffer)
3574     (setq gnus-newsgroup-data nil
3575           gnus-newsgroup-data-reverse nil)
3576     (gnus-run-hooks 'gnus-summary-generate-hook)
3577     ;; Generate the buffer, either with threads or without.
3578     (when gnus-newsgroup-headers
3579       (gnus-summary-prepare-threads
3580        (if gnus-show-threads
3581            (gnus-sort-gathered-threads
3582             (funcall gnus-summary-thread-gathering-function
3583                      (gnus-sort-threads
3584                       (gnus-cut-threads (gnus-make-threads)))))
3585          ;; Unthreaded display.
3586          (gnus-sort-articles gnus-newsgroup-headers))))
3587     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3588     ;; Call hooks for modifying summary buffer.
3589     (goto-char (point-min))
3590     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3591
3592 (defsubst gnus-general-simplify-subject (subject)
3593   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3594   (setq subject
3595         (cond
3596          ;; Truncate the subject.
3597          (gnus-simplify-subject-functions
3598           (gnus-map-function gnus-simplify-subject-functions subject))
3599          ((numberp gnus-summary-gather-subject-limit)
3600           (setq subject (gnus-simplify-subject-re subject))
3601           (if (> (length subject) gnus-summary-gather-subject-limit)
3602               (substring subject 0 gnus-summary-gather-subject-limit)
3603             subject))
3604          ;; Fuzzily simplify it.
3605          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3606           (gnus-simplify-subject-fuzzy subject))
3607          ;; Just remove the leading "Re:".
3608          (t
3609           (gnus-simplify-subject-re subject))))
3610
3611   (if (and gnus-summary-gather-exclude-subject
3612            (string-match gnus-summary-gather-exclude-subject subject))
3613       nil                         ; This article shouldn't be gathered
3614     subject))
3615
3616 (defun gnus-summary-simplify-subject-query ()
3617   "Query where the respool algorithm would put this article."
3618   (interactive)
3619   (gnus-summary-select-article)
3620   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3621
3622 (defun gnus-gather-threads-by-subject (threads)
3623   "Gather threads by looking at Subject headers."
3624   (if (not gnus-summary-make-false-root)
3625       threads
3626     (let ((hashtb (gnus-make-hashtable 1024))
3627           (prev threads)
3628           (result threads)
3629           subject hthread whole-subject)
3630       (while threads
3631         (setq subject (gnus-general-simplify-subject
3632                        (setq whole-subject (mail-header-subject
3633                                             (caar threads)))))
3634         (when subject
3635           (if (setq hthread (gnus-gethash subject hashtb))
3636               (progn
3637                 ;; We enter a dummy root into the thread, if we
3638                 ;; haven't done that already.
3639                 (unless (stringp (caar hthread))
3640                   (setcar hthread (list whole-subject (car hthread))))
3641                 ;; We add this new gathered thread to this gathered
3642                 ;; thread.
3643                 (setcdr (car hthread)
3644                         (nconc (cdar hthread) (list (car threads))))
3645                 ;; Remove it from the list of threads.
3646                 (setcdr prev (cdr threads))
3647                 (setq threads prev))
3648             ;; Enter this thread into the hash table.
3649             (gnus-sethash subject
3650                           (if gnus-summary-make-false-root-always
3651                               (progn
3652                                 ;; If you want a dummy root above all
3653                                 ;; threads...
3654                                 (setcar threads (list whole-subject
3655                                                       (car threads)))
3656                                 threads)
3657                             threads)
3658                           hashtb)))
3659         (setq prev threads)
3660         (setq threads (cdr threads)))
3661       result)))
3662
3663 (defun gnus-gather-threads-by-references (threads)
3664   "Gather threads by looking at References headers."
3665   (let ((idhashtb (gnus-make-hashtable 1024))
3666         (thhashtb (gnus-make-hashtable 1024))
3667         (prev threads)
3668         (result threads)
3669         ids references id gthread gid entered ref)
3670     (while threads
3671       (when (setq references (mail-header-references (caar threads)))
3672         (setq id (mail-header-id (caar threads))
3673               ids (inline (gnus-split-references references))
3674               entered nil)
3675         (while (setq ref (pop ids))
3676           (setq ids (delete ref ids))
3677           (if (not (setq gid (gnus-gethash ref idhashtb)))
3678               (progn
3679                 (gnus-sethash ref id idhashtb)
3680                 (gnus-sethash id threads thhashtb))
3681             (setq gthread (gnus-gethash gid thhashtb))
3682             (unless entered
3683               ;; We enter a dummy root into the thread, if we
3684               ;; haven't done that already.
3685               (unless (stringp (caar gthread))
3686                 (setcar gthread (list (mail-header-subject (caar gthread))
3687                                       (car gthread))))
3688               ;; We add this new gathered thread to this gathered
3689               ;; thread.
3690               (setcdr (car gthread)
3691                       (nconc (cdar gthread) (list (car threads)))))
3692             ;; Add it into the thread hash table.
3693             (gnus-sethash id gthread thhashtb)
3694             (setq entered t)
3695             ;; Remove it from the list of threads.
3696             (setcdr prev (cdr threads))
3697             (setq threads prev))))
3698       (setq prev threads)
3699       (setq threads (cdr threads)))
3700     result))
3701
3702 (defun gnus-sort-gathered-threads (threads)
3703   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3704   (let ((result threads))
3705     (while threads
3706       (when (stringp (caar threads))
3707         (setcdr (car threads)
3708                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3709       (setq threads (cdr threads)))
3710     result))
3711
3712 (defun gnus-thread-loop-p (root thread)
3713   "Say whether ROOT is in THREAD."
3714   (let ((stack (list thread))
3715         (infloop 0)
3716         th)
3717     (while (setq thread (pop stack))
3718       (setq th (cdr thread))
3719       (while (and th
3720                   (not (eq (caar th) root)))
3721         (pop th))
3722       (if th
3723           ;; We have found a loop.
3724           (let (ref-dep)
3725             (setcdr thread (delq (car th) (cdr thread)))
3726             (if (boundp (setq ref-dep (intern "none"
3727                                               gnus-newsgroup-dependencies)))
3728                 (setcdr (symbol-value ref-dep)
3729                         (nconc (cdr (symbol-value ref-dep))
3730                                (list (car th))))
3731               (set ref-dep (list nil (car th))))
3732             (setq infloop 1
3733                   stack nil))
3734         ;; Push all the subthreads onto the stack.
3735         (push (cdr thread) stack)))
3736     infloop))
3737
3738 (defun gnus-make-threads ()
3739   "Go through the dependency hashtb and find the roots.  Return all threads."
3740   (let (threads)
3741     (while (catch 'infloop
3742              (mapatoms
3743               (lambda (refs)
3744                 ;; Deal with self-referencing References loops.
3745                 (when (and (car (symbol-value refs))
3746                            (not (zerop
3747                                  (apply
3748                                   '+
3749                                   (mapcar
3750                                    (lambda (thread)
3751                                      (gnus-thread-loop-p
3752                                       (car (symbol-value refs)) thread))
3753                                    (cdr (symbol-value refs)))))))
3754                   (setq threads nil)
3755                   (throw 'infloop t))
3756                 (unless (car (symbol-value refs))
3757                   ;; These threads do not refer back to any other
3758                   ;; articles, so they're roots.
3759                   (setq threads (append (cdr (symbol-value refs)) threads))))
3760               gnus-newsgroup-dependencies)))
3761     threads))
3762
3763 ;; Build the thread tree.
3764 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3765   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3766
3767 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3768 if it was already present.
3769
3770 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3771 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3772 Message-IDs will be renamed to a unique Message-ID before being
3773 entered.
3774
3775 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3776   (let* ((id (mail-header-id header))
3777          (id-dep (and id (intern id dependencies)))
3778          parent-id ref ref-dep ref-header replaced)
3779     ;; Enter this `header' in the `dependencies' table.
3780     (cond
3781      ((not id-dep)
3782       (setq header nil))
3783      ;; The first two cases do the normal part: enter a new `header'
3784      ;; in the `dependencies' table.
3785      ((not (boundp id-dep))
3786       (set id-dep (list header)))
3787      ((null (car (symbol-value id-dep)))
3788       (setcar (symbol-value id-dep) header))
3789
3790      ;; From here the `header' was already present in the
3791      ;; `dependencies' table.
3792      (force-new
3793       ;; Overrides an existing entry;
3794       ;; just set the header part of the entry.
3795       (setcar (symbol-value id-dep) header)
3796       (setq replaced t))
3797
3798      ;; Renames the existing `header' to a unique Message-ID.
3799      ((not gnus-summary-ignore-duplicates)
3800       ;; An article with this Message-ID has already been seen.
3801       ;; We rename the Message-ID.
3802       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3803            (list header))
3804       (mail-header-set-id header id))
3805
3806      ;; The last case ignores an existing entry, except it adds any
3807      ;; additional Xrefs (in case the two articles came from different
3808      ;; servers.
3809      ;; Also sets `header' to `nil' meaning that the `dependencies'
3810      ;; table was *not* modified.
3811      (t
3812       (mail-header-set-xref
3813        (car (symbol-value id-dep))
3814        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3815                    "")
3816                (or (mail-header-xref header) "")))
3817       (setq header nil)))
3818
3819     (when (and header (not replaced))
3820       ;; First check that we are not creating a References loop.
3821       (setq parent-id (gnus-parent-id (mail-header-references header)))
3822       (setq ref parent-id)
3823       (while (and ref
3824                   (setq ref-dep (intern-soft ref dependencies))
3825                   (boundp ref-dep)
3826                   (setq ref-header (car (symbol-value ref-dep))))
3827         (if (string= id ref)
3828             ;; Yuk!  This is a reference loop.  Make the article be a
3829             ;; root article.
3830             (progn
3831               (mail-header-set-references (car (symbol-value id-dep)) "none")
3832               (setq ref nil)
3833               (setq parent-id nil))
3834           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3835       (setq ref-dep (intern (or parent-id "none") dependencies))
3836       (if (boundp ref-dep)
3837           (setcdr (symbol-value ref-dep)
3838                   (nconc (cdr (symbol-value ref-dep))
3839                          (list (symbol-value id-dep))))
3840         (set ref-dep (list nil (symbol-value id-dep)))))
3841     header))
3842
3843 (defun gnus-extract-message-id-from-in-reply-to (string)
3844   (if (string-match "<[^>]+>" string)
3845       (substring string (match-beginning 0) (match-end 0))
3846     nil))
3847
3848 (defun gnus-build-sparse-threads ()
3849   (let ((headers gnus-newsgroup-headers)
3850         (mail-parse-charset gnus-newsgroup-charset)
3851         (gnus-summary-ignore-duplicates t)
3852         header references generation relations
3853         subject child end new-child date)
3854     ;; First we create an alist of generations/relations, where
3855     ;; generations is how much we trust the relation, and the relation
3856     ;; is parent/child.
3857     (gnus-message 7 "Making sparse threads...")
3858     (save-excursion
3859       (nnheader-set-temp-buffer " *gnus sparse threads*")
3860       (while (setq header (pop headers))
3861         (when (and (setq references (mail-header-references header))
3862                    (not (string= references "")))
3863           (insert references)
3864           (setq child (mail-header-id header)
3865                 subject (mail-header-subject header)
3866                 date (mail-header-date header)
3867                 generation 0)
3868           (while (search-backward ">" nil t)
3869             (setq end (1+ (point)))
3870             (when (search-backward "<" nil t)
3871               (setq new-child (buffer-substring (point) end))
3872               (push (list (incf generation)
3873                           child (setq child new-child)
3874                           subject date)
3875                     relations)))
3876           (when child
3877             (push (list (1+ generation) child nil subject) relations))
3878           (erase-buffer)))
3879       (kill-buffer (current-buffer)))
3880     ;; Sort over trustworthiness.
3881     (mapcar
3882      (lambda (relation)
3883        (when (gnus-dependencies-add-header
3884               (make-full-mail-header
3885                gnus-reffed-article-number
3886                (nth 3 relation) "" (or (nth 4 relation) "")
3887                (nth 1 relation)
3888                (or (nth 2 relation) "") 0 0 "")
3889               gnus-newsgroup-dependencies nil)
3890          (push gnus-reffed-article-number gnus-newsgroup-limit)
3891          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3892          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3893                gnus-newsgroup-reads)
3894          (decf gnus-reffed-article-number)))
3895      (sort relations 'car-less-than-car))
3896     (gnus-message 7 "Making sparse threads...done")))
3897
3898 (defun gnus-build-old-threads ()
3899   ;; Look at all the articles that refer back to old articles, and
3900   ;; fetch the headers for the articles that aren't there.  This will
3901   ;; build complete threads - if the roots haven't been expired by the
3902   ;; server, that is.
3903   (let ((mail-parse-charset gnus-newsgroup-charset)
3904         id heads)
3905     (mapatoms
3906      (lambda (refs)
3907        (when (not (car (symbol-value refs)))
3908          (setq heads (cdr (symbol-value refs)))
3909          (while heads
3910            (if (memq (mail-header-number (caar heads))
3911                      gnus-newsgroup-dormant)
3912                (setq heads (cdr heads))
3913              (setq id (symbol-name refs))
3914              (while (and (setq id (gnus-build-get-header id))
3915                          (not (car (gnus-id-to-thread id)))))
3916              (setq heads nil)))))
3917      gnus-newsgroup-dependencies)))
3918
3919 ;; This function has to be called with point after the article number
3920 ;; on the beginning of the line.
3921 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3922   (let ((eol (gnus-point-at-eol))
3923         (buffer (current-buffer))
3924         header references in-reply-to)
3925
3926     ;; overview: [num subject from date id refs chars lines misc]
3927     (unwind-protect
3928         (let (x)
3929           (narrow-to-region (point) eol)
3930           (unless (eobp)
3931             (forward-char))
3932
3933           (setq header
3934                 (make-full-mail-header
3935                  number                 ; number
3936                  (condition-case ()     ; subject
3937                      (funcall gnus-decode-encoded-word-function
3938                               (setq x (nnheader-nov-field)))
3939                    (error x))
3940                  (condition-case ()     ; from
3941                      (funcall gnus-decode-encoded-word-function
3942                               (setq x (nnheader-nov-field)))
3943                    (error x))
3944                  (nnheader-nov-field)   ; date
3945                  (nnheader-nov-read-message-id) ; id
3946                  (setq references (nnheader-nov-field)) ; refs
3947                  (nnheader-nov-read-integer) ; chars
3948                  (nnheader-nov-read-integer) ; lines
3949                  (unless (eobp)
3950                    (if (looking-at "Xref: ")
3951                        (goto-char (match-end 0)))
3952                    (nnheader-nov-field)) ; Xref
3953                  (nnheader-nov-parse-extra)))) ; extra
3954
3955       (widen))
3956
3957     (when (and (string= references "")
3958                (setq in-reply-to (mail-header-extra header))
3959                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3960       (mail-header-set-references
3961        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3962
3963     (when gnus-alter-header-function
3964       (funcall gnus-alter-header-function header))
3965     (gnus-dependencies-add-header header dependencies force-new)))
3966
3967 (defun gnus-build-get-header (id)
3968   "Look through the buffer of NOV lines and find the header to ID.
3969 Enter this line into the dependencies hash table, and return
3970 the id of the parent article (if any)."
3971   (let ((deps gnus-newsgroup-dependencies)
3972         found header)
3973     (prog1
3974         (save-excursion
3975           (set-buffer nntp-server-buffer)
3976           (let ((case-fold-search nil))
3977             (goto-char (point-min))
3978             (while (and (not found)
3979                         (search-forward id nil t))
3980               (beginning-of-line)
3981               (setq found (looking-at
3982                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3983                                    (regexp-quote id))))
3984               (or found (beginning-of-line 2)))
3985             (when found
3986               (beginning-of-line)
3987               (and
3988                (setq header (gnus-nov-parse-line
3989                              (read (current-buffer)) deps))
3990                (gnus-parent-id (mail-header-references header))))))
3991       (when header
3992         (let ((number (mail-header-number header)))
3993           (push number gnus-newsgroup-limit)
3994           (push header gnus-newsgroup-headers)
3995           (if (memq number gnus-newsgroup-unselected)
3996               (progn
3997                 (setq gnus-newsgroup-unreads
3998                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
3999                                                number))
4000                 (setq gnus-newsgroup-unselected
4001                       (delq number gnus-newsgroup-unselected)))
4002             (push number gnus-newsgroup-ancient)))))))
4003
4004 (defun gnus-build-all-threads ()
4005   "Read all the headers."
4006   (let ((gnus-summary-ignore-duplicates t)
4007         (mail-parse-charset gnus-newsgroup-charset)
4008         (dependencies gnus-newsgroup-dependencies)
4009         header article)
4010     (save-excursion
4011       (set-buffer nntp-server-buffer)
4012       (let ((case-fold-search nil))
4013         (goto-char (point-min))
4014         (while (not (eobp))
4015           (ignore-errors
4016             (setq article (read (current-buffer))
4017                   header (gnus-nov-parse-line article dependencies)))
4018           (when header
4019             (save-excursion
4020               (set-buffer gnus-summary-buffer)
4021               (push header gnus-newsgroup-headers)
4022               (if (memq (setq article (mail-header-number header))
4023                         gnus-newsgroup-unselected)
4024                   (progn
4025                     (setq gnus-newsgroup-unreads
4026                           (gnus-add-to-sorted-list
4027                            gnus-newsgroup-unreads article))
4028                     (setq gnus-newsgroup-unselected
4029                           (delq article gnus-newsgroup-unselected)))
4030                 (push article gnus-newsgroup-ancient)))
4031             (forward-line 1)))))))
4032
4033 (defun gnus-summary-update-article-line (article header)
4034   "Update the line for ARTICLE using HEADERS."
4035   (let* ((id (mail-header-id header))
4036          (thread (gnus-id-to-thread id)))
4037     (unless thread
4038       (error "Article in no thread"))
4039     ;; Update the thread.
4040     (setcar thread header)
4041     (gnus-summary-goto-subject article)
4042     (let* ((datal (gnus-data-find-list article))
4043            (data (car datal))
4044            (buffer-read-only nil)
4045            (level (gnus-summary-thread-level)))
4046       (gnus-delete-line)
4047       (let ((inserted (- (point)
4048                          (progn
4049                            (gnus-summary-insert-line
4050                             header level nil 
4051                             (memq article gnus-newsgroup-undownloaded)
4052                             (gnus-article-mark article)
4053                             (memq article gnus-newsgroup-replied)
4054                             (memq article gnus-newsgroup-expirable)
4055                             ;; Only insert the Subject string when it's different
4056                             ;; from the previous Subject string.
4057                             (if (and
4058                                  gnus-show-threads
4059                                  (gnus-subject-equal
4060                                   (condition-case ()
4061                                       (mail-header-subject
4062                                        (gnus-data-header
4063                                         (cadr
4064                                          (gnus-data-find-list
4065                                           article
4066                                           (gnus-data-list t)))))
4067                                     ;; Error on the side of excessive subjects.
4068                                     (error ""))
4069                                   (mail-header-subject header)))
4070                                 ""
4071                               (mail-header-subject header))
4072                             nil (cdr (assq article gnus-newsgroup-scored))
4073                             (memq article gnus-newsgroup-processable))
4074                            (point)))))
4075         (when (cdr datal)
4076           (gnus-data-update-list
4077            (cdr datal) 
4078            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4079
4080 (defun gnus-summary-update-article (article &optional iheader)
4081   "Update ARTICLE in the summary buffer."
4082   (set-buffer gnus-summary-buffer)
4083   (let* ((header (gnus-summary-article-header article))
4084          (id (mail-header-id header))
4085          (data (gnus-data-find article))
4086          (thread (gnus-id-to-thread id))
4087          (references (mail-header-references header))
4088          (parent
4089           (gnus-id-to-thread
4090            (or (gnus-parent-id
4091                 (when (and references
4092                            (not (equal "" references)))
4093                   references))
4094                "none")))
4095          (buffer-read-only nil)
4096          (old (car thread)))
4097     (when thread
4098       (unless iheader
4099         (setcar thread nil)
4100         (when parent
4101           (delq thread parent)))
4102       (if (gnus-summary-insert-subject id header)
4103           ;; Set the (possibly) new article number in the data structure.
4104           (gnus-data-set-number data (gnus-id-to-article id))
4105         (setcar thread old)
4106         nil))))
4107
4108 (defun gnus-rebuild-thread (id &optional line)
4109   "Rebuild the thread containing ID.
4110 If LINE, insert the rebuilt thread starting on line LINE."
4111   (let ((buffer-read-only nil)
4112         old-pos current thread data)
4113     (if (not gnus-show-threads)
4114         (setq thread (list (car (gnus-id-to-thread id))))
4115       ;; Get the thread this article is part of.
4116       (setq thread (gnus-remove-thread id)))
4117     (setq old-pos (gnus-point-at-bol))
4118     (setq current (save-excursion
4119                     (and (re-search-backward "[\r\n]" nil t)
4120                          (gnus-summary-article-number))))
4121     ;; If this is a gathered thread, we have to go some re-gathering.
4122     (when (stringp (car thread))
4123       (let ((subject (car thread))
4124             roots thr)
4125         (setq thread (cdr thread))
4126         (while thread
4127           (unless (memq (setq thr (gnus-id-to-thread
4128                                    (gnus-root-id
4129                                     (mail-header-id (caar thread)))))
4130                         roots)
4131             (push thr roots))
4132           (setq thread (cdr thread)))
4133         ;; We now have all (unique) roots.
4134         (if (= (length roots) 1)
4135             ;; All the loose roots are now one solid root.
4136             (setq thread (car roots))
4137           (setq thread (cons subject (gnus-sort-threads roots))))))
4138     (let (threads)
4139       ;; We then insert this thread into the summary buffer.
4140       (when line
4141         (goto-char (point-min))
4142         (forward-line (1- line)))
4143       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4144         (if gnus-show-threads
4145             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4146           (gnus-summary-prepare-unthreaded thread))
4147         (setq data (nreverse gnus-newsgroup-data))
4148         (setq threads gnus-newsgroup-threads))
4149       ;; We splice the new data into the data structure.
4150       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4151       ;;!!! then we want to insert at the beginning of the buffer.
4152       ;;!!! That happens to be true with Gnus now, but that may
4153       ;;!!! change in the future.  Perhaps.
4154       (gnus-data-enter-list
4155        (if line nil current) data (- (point) old-pos))
4156       (setq gnus-newsgroup-threads
4157             (nconc threads gnus-newsgroup-threads))
4158       (gnus-data-compute-positions))))
4159
4160 (defun gnus-number-to-header (number)
4161   "Return the header for article NUMBER."
4162   (let ((headers gnus-newsgroup-headers))
4163     (while (and headers
4164                 (not (= number (mail-header-number (car headers)))))
4165       (pop headers))
4166     (when headers
4167       (car headers))))
4168
4169 (defun gnus-parent-headers (in-headers &optional generation)
4170   "Return the headers of the GENERATIONeth parent of HEADERS."
4171   (unless generation
4172     (setq generation 1))
4173   (let ((parent t)
4174         (headers in-headers)
4175         references)
4176     (while (and parent
4177                 (not (zerop generation))
4178                 (setq references (mail-header-references headers)))
4179       (setq headers (if (and references
4180                              (setq parent (gnus-parent-id references)))
4181                         (car (gnus-id-to-thread parent))
4182                       nil))
4183       (decf generation))
4184     (and (not (eq headers in-headers))
4185          headers)))
4186
4187 (defun gnus-id-to-thread (id)
4188   "Return the (sub-)thread where ID appears."
4189   (gnus-gethash id gnus-newsgroup-dependencies))
4190
4191 (defun gnus-id-to-article (id)
4192   "Return the article number of ID."
4193   (let ((thread (gnus-id-to-thread id)))
4194     (when (and thread
4195                (car thread))
4196       (mail-header-number (car thread)))))
4197
4198 (defun gnus-id-to-header (id)
4199   "Return the article headers of ID."
4200   (car (gnus-id-to-thread id)))
4201
4202 (defun gnus-article-displayed-root-p (article)
4203   "Say whether ARTICLE is a root(ish) article."
4204   (let ((level (gnus-summary-thread-level article))
4205         (refs (mail-header-references  (gnus-summary-article-header article)))
4206         particle)
4207     (cond
4208      ((null level) nil)
4209      ((zerop level) t)
4210      ((null refs) t)
4211      ((null (gnus-parent-id refs)) t)
4212      ((and (= 1 level)
4213            (null (setq particle (gnus-id-to-article
4214                                  (gnus-parent-id refs))))
4215            (null (gnus-summary-thread-level particle)))))))
4216
4217 (defun gnus-root-id (id)
4218   "Return the id of the root of the thread where ID appears."
4219   (let (last-id prev)
4220     (while (and id (setq prev (car (gnus-id-to-thread id))))
4221       (setq last-id id
4222             id (gnus-parent-id (mail-header-references prev))))
4223     last-id))
4224
4225 (defun gnus-articles-in-thread (thread)
4226   "Return the list of articles in THREAD."
4227   (cons (mail-header-number (car thread))
4228         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4229
4230 (defun gnus-remove-thread (id &optional dont-remove)
4231   "Remove the thread that has ID in it."
4232   (let (headers thread last-id)
4233     ;; First go up in this thread until we find the root.
4234     (setq last-id (gnus-root-id id)
4235           headers (message-flatten-list (gnus-id-to-thread last-id)))
4236     ;; We have now found the real root of this thread.  It might have
4237     ;; been gathered into some loose thread, so we have to search
4238     ;; through the threads to find the thread we wanted.
4239     (let ((threads gnus-newsgroup-threads)
4240           sub)
4241       (while threads
4242         (setq sub (car threads))
4243         (if (stringp (car sub))
4244             ;; This is a gathered thread, so we look at the roots
4245             ;; below it to find whether this article is in this
4246             ;; gathered root.
4247             (progn
4248               (setq sub (cdr sub))
4249               (while sub
4250                 (when (member (caar sub) headers)
4251                   (setq thread (car threads)
4252                         threads nil
4253                         sub nil))
4254                 (setq sub (cdr sub))))
4255           ;; It's an ordinary thread, so we check it.
4256           (when (eq (car sub) (car headers))
4257             (setq thread sub
4258                   threads nil)))
4259         (setq threads (cdr threads)))
4260       ;; If this article is in no thread, then it's a root.
4261       (if thread
4262           (unless dont-remove
4263             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4264         (setq thread (gnus-id-to-thread last-id)))
4265       (when thread
4266         (prog1
4267             thread                      ; We return this thread.
4268           (unless dont-remove
4269             (if (stringp (car thread))
4270                 (progn
4271                   ;; If we use dummy roots, then we have to remove the
4272                   ;; dummy root as well.
4273                   (when (eq gnus-summary-make-false-root 'dummy)
4274                     ;; We go to the dummy root by going to
4275                     ;; the first sub-"thread", and then one line up.
4276                     (gnus-summary-goto-article
4277                      (mail-header-number (caadr thread)))
4278                     (forward-line -1)
4279                     (gnus-delete-line)
4280                     (gnus-data-compute-positions))
4281                   (setq thread (cdr thread))
4282                   (while thread
4283                     (gnus-remove-thread-1 (car thread))
4284                     (setq thread (cdr thread))))
4285               (gnus-remove-thread-1 thread))))))))
4286
4287 (defun gnus-remove-thread-1 (thread)
4288   "Remove the thread THREAD recursively."
4289   (let ((number (mail-header-number (pop thread)))
4290         d)
4291     (setq thread (reverse thread))
4292     (while thread
4293       (gnus-remove-thread-1 (pop thread)))
4294     (when (setq d (gnus-data-find number))
4295       (goto-char (gnus-data-pos d))
4296       (gnus-summary-show-thread)
4297       (gnus-data-remove
4298        number
4299        (- (gnus-point-at-bol)
4300           (prog1
4301               (1+ (gnus-point-at-eol))
4302             (gnus-delete-line)))))))
4303
4304 (defun gnus-sort-threads-1 (threads func)
4305   (sort (mapcar (lambda (thread)
4306                   (cons (car thread)
4307                         (and (cdr thread)
4308                              (gnus-sort-threads-1 (cdr thread) func))))
4309                 threads) func))
4310
4311 (defun gnus-sort-threads (threads)
4312   "Sort THREADS."
4313   (if (not gnus-thread-sort-functions)
4314       threads
4315     (gnus-message 8 "Sorting threads...")
4316     (let ((max-lisp-eval-depth 5000))
4317       (prog1 (gnus-sort-threads-1
4318          threads
4319          (gnus-make-sort-function gnus-thread-sort-functions))
4320         (gnus-message 8 "Sorting threads...done")))))
4321
4322 (defun gnus-sort-articles (articles)
4323   "Sort ARTICLES."
4324   (when gnus-article-sort-functions
4325     (gnus-message 7 "Sorting articles...")
4326     (prog1
4327         (setq gnus-newsgroup-headers
4328               (sort articles (gnus-make-sort-function
4329                               gnus-article-sort-functions)))
4330       (gnus-message 7 "Sorting articles...done"))))
4331
4332 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4333 (defmacro gnus-thread-header (thread)
4334   "Return header of first article in THREAD.
4335 Note that THREAD must never, ever be anything else than a variable -
4336 using some other form will lead to serious barfage."
4337   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4338   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4339   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4340         (vector thread) 2))
4341
4342 (defsubst gnus-article-sort-by-number (h1 h2)
4343   "Sort articles by article number."
4344   (< (mail-header-number h1)
4345      (mail-header-number h2)))
4346
4347 (defun gnus-thread-sort-by-number (h1 h2)
4348   "Sort threads by root article number."
4349   (gnus-article-sort-by-number
4350    (gnus-thread-header h1) (gnus-thread-header h2)))
4351
4352 (defsubst gnus-article-sort-by-random (h1 h2)
4353   "Sort articles by article number."
4354   (zerop (random 2)))
4355
4356 (defun gnus-thread-sort-by-random (h1 h2)
4357   "Sort threads by root article number."
4358   (gnus-article-sort-by-random
4359    (gnus-thread-header h1) (gnus-thread-header h2)))
4360
4361 (defsubst gnus-article-sort-by-lines (h1 h2)
4362   "Sort articles by article Lines header."
4363   (< (mail-header-lines h1)
4364      (mail-header-lines h2)))
4365
4366 (defun gnus-thread-sort-by-lines (h1 h2)
4367   "Sort threads by root article Lines header."
4368   (gnus-article-sort-by-lines
4369    (gnus-thread-header h1) (gnus-thread-header h2)))
4370
4371 (defsubst gnus-article-sort-by-chars (h1 h2)
4372   "Sort articles by octet length."
4373   (< (mail-header-chars h1)
4374      (mail-header-chars h2)))
4375
4376 (defun gnus-thread-sort-by-chars (h1 h2)
4377   "Sort threads by root article octet length."
4378   (gnus-article-sort-by-chars
4379    (gnus-thread-header h1) (gnus-thread-header h2)))
4380
4381 (defsubst gnus-article-sort-by-author (h1 h2)
4382   "Sort articles by root author."
4383   (string-lessp
4384    (let ((extract (funcall
4385                    gnus-extract-address-components
4386                    (mail-header-from h1))))
4387      (or (car extract) (cadr extract) ""))
4388    (let ((extract (funcall
4389                    gnus-extract-address-components
4390                    (mail-header-from h2))))
4391      (or (car extract) (cadr extract) ""))))
4392
4393 (defun gnus-thread-sort-by-author (h1 h2)
4394   "Sort threads by root author."
4395   (gnus-article-sort-by-author
4396    (gnus-thread-header h1)  (gnus-thread-header h2)))
4397
4398 (defsubst gnus-article-sort-by-subject (h1 h2)
4399   "Sort articles by root subject."
4400   (string-lessp
4401    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4402    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4403
4404 (defun gnus-thread-sort-by-subject (h1 h2)
4405   "Sort threads by root subject."
4406   (gnus-article-sort-by-subject
4407    (gnus-thread-header h1) (gnus-thread-header h2)))
4408
4409 (defsubst gnus-article-sort-by-date (h1 h2)
4410   "Sort articles by root article date."
4411   (time-less-p
4412    (gnus-date-get-time (mail-header-date h1))
4413    (gnus-date-get-time (mail-header-date h2))))
4414
4415 (defun gnus-thread-sort-by-date (h1 h2)
4416   "Sort threads by root article date."
4417   (gnus-article-sort-by-date
4418    (gnus-thread-header h1) (gnus-thread-header h2)))
4419
4420 (defsubst gnus-article-sort-by-score (h1 h2)
4421   "Sort articles by root article score.
4422 Unscored articles will be counted as having a score of zero."
4423   (> (or (cdr (assq (mail-header-number h1)
4424                     gnus-newsgroup-scored))
4425          gnus-summary-default-score 0)
4426      (or (cdr (assq (mail-header-number h2)
4427                     gnus-newsgroup-scored))
4428          gnus-summary-default-score 0)))
4429
4430 (defun gnus-thread-sort-by-score (h1 h2)
4431   "Sort threads by root article score."
4432   (gnus-article-sort-by-score
4433    (gnus-thread-header h1) (gnus-thread-header h2)))
4434
4435 (defun gnus-thread-sort-by-total-score (h1 h2)
4436   "Sort threads by the sum of all scores in the thread.
4437 Unscored articles will be counted as having a score of zero."
4438   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4439
4440 (defun gnus-thread-total-score (thread)
4441   ;; This function find the total score of THREAD.
4442   (cond
4443    ((null thread)
4444     0)
4445    ((consp thread)
4446     (if (stringp (car thread))
4447         (apply gnus-thread-score-function 0
4448                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4449       (gnus-thread-total-score-1 thread)))
4450    (t
4451     (gnus-thread-total-score-1 (list thread)))))
4452
4453 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4454   "Sort threads such that the thread with the most recently arrived article comes first."
4455   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4456
4457 (defun gnus-thread-highest-number (thread)
4458   "Return the highest article number in THREAD."
4459   (apply 'max (mapcar (lambda (header)
4460                         (mail-header-number header))
4461                       (message-flatten-list thread))))
4462
4463 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4464   "Sort threads such that the thread with the most recently dated article comes first."
4465   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4466
4467 (defun gnus-thread-latest-date (thread)
4468   "Return the highest article date in THREAD."
4469   (let ((previous-time 0))
4470     (apply 'max
4471            (mapcar
4472             (lambda (header)
4473               (setq previous-time
4474                     (time-to-seconds
4475                      (condition-case ()
4476                          (mail-header-parse-date (mail-header-date header))
4477                        (error previous-time)))))
4478             (sort
4479              (message-flatten-list thread)
4480              (lambda (h1 h2)
4481                (< (mail-header-number h1)
4482                   (mail-header-number h2))))))))
4483
4484 (defun gnus-thread-total-score-1 (root)
4485   ;; This function find the total score of the thread below ROOT.
4486   (setq root (car root))
4487   (apply gnus-thread-score-function
4488          (or (append
4489               (mapcar 'gnus-thread-total-score
4490                       (cdr (gnus-id-to-thread (mail-header-id root))))
4491               (when (> (mail-header-number root) 0)
4492                 (list (or (cdr (assq (mail-header-number root)
4493                                      gnus-newsgroup-scored))
4494                           gnus-summary-default-score 0))))
4495              (list gnus-summary-default-score)
4496              '(0))))
4497
4498 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4499 (defvar gnus-tmp-prev-subject nil)
4500 (defvar gnus-tmp-false-parent nil)
4501 (defvar gnus-tmp-root-expunged nil)
4502 (defvar gnus-tmp-dummy-line nil)
4503
4504 (eval-when-compile (defvar gnus-tmp-header))
4505 (defun gnus-extra-header (type &optional header)
4506   "Return the extra header of TYPE."
4507   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4508       ""))
4509
4510 (defvar gnus-tmp-thread-tree-header-string "")
4511
4512 (defcustom gnus-sum-thread-tree-root "> "
4513   "With %B spec, used for the root of a thread.
4514 If nil, use subject instead."
4515   :type 'string
4516   :group 'gnus-thread)
4517 (defcustom gnus-sum-thread-tree-single-indent ""
4518   "With %B spec, used for a thread with just one message.
4519 If nil, use subject instead."
4520   :type 'string
4521   :group 'gnus-thread)
4522 (defcustom gnus-sum-thread-tree-vertical "| "
4523   "With %B spec, used for drawing a vertical line."
4524   :type 'string
4525   :group 'gnus-thread)
4526 (defcustom gnus-sum-thread-tree-indent "  "
4527   "With %B spec, used for indenting."
4528   :type 'string
4529   :group 'gnus-thread)
4530 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4531   "With %B spec, used for a leaf with brothers."
4532   :type 'string
4533   :group 'gnus-thread)
4534 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4535   "With %B spec, used for a leaf without brothers."
4536   :type 'string
4537   :group 'gnus-thread)
4538
4539 (defun gnus-summary-prepare-threads (threads)
4540   "Prepare summary buffer from THREADS and indentation LEVEL.
4541 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4542 or a straight list of headers."
4543   (gnus-message 7 "Generating summary...")
4544
4545   (setq gnus-newsgroup-threads threads)
4546   (beginning-of-line)
4547
4548   (let ((gnus-tmp-level 0)
4549         (default-score (or gnus-summary-default-score 0))
4550         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4551         (building-line-count gnus-summary-display-while-building)
4552         (building-count (integerp gnus-summary-display-while-building))
4553         thread number subject stack state gnus-tmp-gathered beg-match
4554         new-roots gnus-tmp-new-adopts thread-end simp-subject
4555         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4556         gnus-tmp-replied gnus-tmp-subject-or-nil
4557         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4558         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4559         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4560         tree-stack)
4561
4562     (setq gnus-tmp-prev-subject nil
4563           gnus-tmp-thread-tree-header-string "")
4564
4565     (if (vectorp (car threads))
4566         ;; If this is a straight (sic) list of headers, then a
4567         ;; threaded summary display isn't required, so we just create
4568         ;; an unthreaded one.
4569         (gnus-summary-prepare-unthreaded threads)
4570
4571       ;; Do the threaded display.
4572
4573       (if gnus-summary-display-while-building
4574           (switch-to-buffer (buffer-name)))
4575       (while (or threads stack gnus-tmp-new-adopts new-roots)
4576
4577         (if (and (= gnus-tmp-level 0)
4578                  (or (not stack)
4579                      (= (caar stack) 0))
4580                  (not gnus-tmp-false-parent)
4581                  (or gnus-tmp-new-adopts new-roots))
4582             (if gnus-tmp-new-adopts
4583                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4584                       thread (list (car gnus-tmp-new-adopts))
4585                       gnus-tmp-header (caar thread)
4586                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4587               (when new-roots
4588                 (setq thread (list (car new-roots))
4589                       gnus-tmp-header (caar thread)
4590                       new-roots (cdr new-roots))))
4591
4592           (if threads
4593               ;; If there are some threads, we do them before the
4594               ;; threads on the stack.
4595               (setq thread threads
4596                     gnus-tmp-header (caar thread))
4597             ;; There were no current threads, so we pop something off
4598             ;; the stack.
4599             (setq state (car stack)
4600                   gnus-tmp-level (car state)
4601                   tree-stack (cadr state)
4602                   thread (caddr state)
4603                   stack (cdr stack)
4604                   gnus-tmp-header (caar thread))))
4605
4606         (setq gnus-tmp-false-parent nil)
4607         (setq gnus-tmp-root-expunged nil)
4608         (setq thread-end nil)
4609
4610         (if (stringp gnus-tmp-header)
4611             ;; The header is a dummy root.
4612             (cond
4613              ((eq gnus-summary-make-false-root 'adopt)
4614               ;; We let the first article adopt the rest.
4615               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4616                                                (cddar thread)))
4617               (setq gnus-tmp-gathered
4618                     (nconc (mapcar
4619                             (lambda (h) (mail-header-number (car h)))
4620                             (cddar thread))
4621                            gnus-tmp-gathered))
4622               (setq thread (cons (list (caar thread)
4623                                        (cadar thread))
4624                                  (cdr thread)))
4625               (setq gnus-tmp-level -1
4626                     gnus-tmp-false-parent t))
4627              ((eq gnus-summary-make-false-root 'empty)
4628               ;; We print adopted articles with empty subject fields.
4629               (setq gnus-tmp-gathered
4630                     (nconc (mapcar
4631                             (lambda (h) (mail-header-number (car h)))
4632                             (cddar thread))
4633                            gnus-tmp-gathered))
4634               (setq gnus-tmp-level -1))
4635              ((eq gnus-summary-make-false-root 'dummy)
4636               ;; We remember that we probably want to output a dummy
4637               ;; root.
4638               (setq gnus-tmp-dummy-line gnus-tmp-header)
4639               (setq gnus-tmp-prev-subject gnus-tmp-header))
4640              (t
4641               ;; We do not make a root for the gathered
4642               ;; sub-threads at all.
4643               (setq gnus-tmp-level -1)))
4644
4645           (setq number (mail-header-number gnus-tmp-header)
4646                 subject (mail-header-subject gnus-tmp-header)
4647                 simp-subject (gnus-simplify-subject-fully subject))
4648
4649           (cond
4650            ;; If the thread has changed subject, we might want to make
4651            ;; this subthread into a root.
4652            ((and (null gnus-thread-ignore-subject)
4653                  (not (zerop gnus-tmp-level))
4654                  gnus-tmp-prev-subject
4655                  (not (string= gnus-tmp-prev-subject simp-subject)))
4656             (setq new-roots (nconc new-roots (list (car thread)))
4657                   thread-end t
4658                   gnus-tmp-header nil))
4659            ;; If the article lies outside the current limit,
4660            ;; then we do not display it.
4661            ((not (memq number gnus-newsgroup-limit))
4662             (setq gnus-tmp-gathered
4663                   (nconc (mapcar
4664                           (lambda (h) (mail-header-number (car h)))
4665                           (cdar thread))
4666                          gnus-tmp-gathered))
4667             (setq gnus-tmp-new-adopts (if (cdar thread)
4668                                           (append gnus-tmp-new-adopts
4669                                                   (cdar thread))
4670                                         gnus-tmp-new-adopts)
4671                   thread-end t
4672                   gnus-tmp-header nil)
4673             (when (zerop gnus-tmp-level)
4674               (setq gnus-tmp-root-expunged t)))
4675            ;; Perhaps this article is to be marked as read?
4676            ((and gnus-summary-mark-below
4677                  (< (or (cdr (assq number gnus-newsgroup-scored))
4678                         default-score)
4679                     gnus-summary-mark-below)
4680                  ;; Don't touch sparse articles.
4681                  (not (gnus-summary-article-sparse-p number))
4682                  (not (gnus-summary-article-ancient-p number)))
4683             (setq gnus-newsgroup-unreads
4684                   (delq number gnus-newsgroup-unreads))
4685             (if gnus-newsgroup-auto-expire
4686                 (setq gnus-newsgroup-expirable
4687                       (gnus-add-to-sorted-list
4688                        gnus-newsgroup-expirable number))
4689               (push (cons number gnus-low-score-mark)
4690                     gnus-newsgroup-reads))))
4691
4692           (when gnus-tmp-header
4693             ;; We may have an old dummy line to output before this
4694             ;; article.
4695             (when (and gnus-tmp-dummy-line
4696                        (gnus-subject-equal
4697                         gnus-tmp-dummy-line
4698                         (mail-header-subject gnus-tmp-header)))
4699               (gnus-summary-insert-dummy-line
4700                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4701               (setq gnus-tmp-dummy-line nil))
4702
4703             ;; Compute the mark.
4704             (setq gnus-tmp-unread (gnus-article-mark number))
4705
4706             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4707                                   gnus-tmp-header gnus-tmp-level)
4708                   gnus-newsgroup-data)
4709
4710             ;; Actually insert the line.
4711             (setq
4712              gnus-tmp-subject-or-nil
4713              (cond
4714               ((and gnus-thread-ignore-subject
4715                     gnus-tmp-prev-subject
4716                     (not (string= gnus-tmp-prev-subject simp-subject)))
4717                subject)
4718               ((zerop gnus-tmp-level)
4719                (if (and (eq gnus-summary-make-false-root 'empty)
4720                         (memq number gnus-tmp-gathered)
4721                         gnus-tmp-prev-subject
4722                         (string= gnus-tmp-prev-subject simp-subject))
4723                    gnus-summary-same-subject
4724                  subject))
4725               (t gnus-summary-same-subject)))
4726             (if (and (eq gnus-summary-make-false-root 'adopt)
4727                      (= gnus-tmp-level 1)
4728                      (memq number gnus-tmp-gathered))
4729                 (setq gnus-tmp-opening-bracket ?\<
4730                       gnus-tmp-closing-bracket ?\>)
4731               (setq gnus-tmp-opening-bracket ?\[
4732                     gnus-tmp-closing-bracket ?\]))
4733             (setq
4734              gnus-tmp-indentation
4735              (aref gnus-thread-indent-array gnus-tmp-level)
4736              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4737              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4738                                 gnus-summary-default-score 0)
4739              gnus-tmp-score-char
4740              (if (or (null gnus-summary-default-score)
4741                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4742                          gnus-summary-zcore-fuzz))
4743                  ?                      ;Whitespace
4744                (if (< gnus-tmp-score gnus-summary-default-score)
4745                    gnus-score-below-mark gnus-score-over-mark))
4746              gnus-tmp-replied
4747              (cond ((memq number gnus-newsgroup-processable)
4748                     gnus-process-mark)
4749                    ((memq number gnus-newsgroup-cached)
4750                     gnus-cached-mark)
4751                    ((memq number gnus-newsgroup-replied)
4752                     gnus-replied-mark)
4753                    ((memq number gnus-newsgroup-forwarded)
4754                     gnus-forwarded-mark)
4755                    ((memq number gnus-newsgroup-saved)
4756                     gnus-saved-mark)
4757                    ((memq number gnus-newsgroup-recent)
4758                     gnus-recent-mark)
4759                    ((memq number gnus-newsgroup-unseen)
4760                     gnus-unseen-mark)
4761                    (t gnus-no-mark))
4762              gnus-tmp-downloaded
4763              (cond ((memq number gnus-newsgroup-undownloaded) 
4764                     gnus-undownloaded-mark)
4765                    (gnus-newsgroup-agentized
4766                     gnus-downloaded-mark)
4767                    (t
4768                     gnus-no-mark))
4769              gnus-tmp-from (mail-header-from gnus-tmp-header)
4770              gnus-tmp-name
4771              (cond
4772               ((string-match "<[^>]+> *$" gnus-tmp-from)
4773                (setq beg-match (match-beginning 0))
4774                (or (and (string-match "^\".+\"" gnus-tmp-from)
4775                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4776                    (substring gnus-tmp-from 0 beg-match)))
4777               ((string-match "(.+)" gnus-tmp-from)
4778                (substring gnus-tmp-from
4779                           (1+ (match-beginning 0)) (1- (match-end 0))))
4780               (t gnus-tmp-from))
4781              gnus-tmp-thread-tree-header-string
4782              (cond
4783               ((not gnus-show-threads) "")
4784               ((zerop gnus-tmp-level)
4785                (if (cdar thread)
4786                    (or gnus-sum-thread-tree-root subject)
4787                  (or gnus-sum-thread-tree-single-indent subject)))
4788               (t
4789                (concat (apply 'concat
4790                               (mapcar (lambda (item)
4791                                         (if (= item 1)
4792                                             gnus-sum-thread-tree-vertical
4793                                           gnus-sum-thread-tree-indent))
4794                                       (cdr (reverse tree-stack))))
4795                        (if (nth 1 thread)
4796                            gnus-sum-thread-tree-leaf-with-other
4797                          gnus-sum-thread-tree-single-leaf)))))
4798             (when (string= gnus-tmp-name "")
4799               (setq gnus-tmp-name gnus-tmp-from))
4800             (unless (numberp gnus-tmp-lines)
4801               (setq gnus-tmp-lines -1))
4802             (if (= gnus-tmp-lines -1)
4803                 (setq gnus-tmp-lines "?")
4804               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4805               (gnus-put-text-property
4806              (point)
4807              (progn (eval gnus-summary-line-format-spec) (point))
4808                'gnus-number number)
4809             (when gnus-visual-p
4810               (forward-line -1)
4811               (gnus-run-hooks 'gnus-summary-update-hook)
4812               (forward-line 1))
4813
4814             (setq gnus-tmp-prev-subject simp-subject)))
4815
4816         (when (nth 1 thread)
4817           (push (list (max 0 gnus-tmp-level)
4818                       (copy-list tree-stack)
4819                       (nthcdr 1 thread))
4820                 stack))
4821         (push (if (nth 1 thread) 1 0) tree-stack)
4822         (incf gnus-tmp-level)
4823         (setq threads (if thread-end nil (cdar thread)))
4824         (if gnus-summary-display-while-building
4825             (if building-count
4826                 (progn
4827                   ;; use a set frequency
4828                   (setq building-line-count (1- building-line-count))
4829                   (when (= building-line-count 0)
4830                     (sit-for 0)
4831                     (setq building-line-count
4832                           gnus-summary-display-while-building)))
4833               ;; always
4834               (sit-for 0)))
4835         (unless threads
4836           (setq gnus-tmp-level 0)))))
4837   (gnus-message 7 "Generating summary...done"))
4838
4839 (defun gnus-summary-prepare-unthreaded (headers)
4840   "Generate an unthreaded summary buffer based on HEADERS."
4841   (let (header number mark)
4842
4843     (beginning-of-line)
4844
4845     (while headers
4846       ;; We may have to root out some bad articles...
4847       (when (memq (setq number (mail-header-number
4848                                 (setq header (pop headers))))
4849                   gnus-newsgroup-limit)
4850         ;; Mark article as read when it has a low score.
4851         (when (and gnus-summary-mark-below
4852                    (< (or (cdr (assq number gnus-newsgroup-scored))
4853                           gnus-summary-default-score 0)
4854                       gnus-summary-mark-below)
4855                    (not (gnus-summary-article-ancient-p number)))
4856           (setq gnus-newsgroup-unreads
4857                 (delq number gnus-newsgroup-unreads))
4858           (if gnus-newsgroup-auto-expire
4859               (push number gnus-newsgroup-expirable)
4860             (push (cons number gnus-low-score-mark)
4861                   gnus-newsgroup-reads)))
4862
4863         (setq mark (gnus-article-mark number))
4864         (push (gnus-data-make number mark (1+ (point)) header 0)
4865               gnus-newsgroup-data)
4866         (gnus-summary-insert-line
4867          header 0 number
4868          (memq number gnus-newsgroup-undownloaded)
4869          mark (memq number gnus-newsgroup-replied)
4870          (memq number gnus-newsgroup-expirable)
4871          (mail-header-subject header) nil
4872          (cdr (assq number gnus-newsgroup-scored))
4873          (memq number gnus-newsgroup-processable))))))
4874
4875 (defun gnus-summary-remove-list-identifiers ()
4876   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4877   (let ((regexp (if (consp gnus-list-identifiers)
4878                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4879                   gnus-list-identifiers))
4880         changed subject)
4881     (when regexp
4882       (dolist (header gnus-newsgroup-headers)
4883         (setq subject (mail-header-subject header)
4884               changed nil)
4885         (while (string-match
4886                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4887                 subject)
4888           (setq subject
4889                 (concat (substring subject 0 (match-beginning 2))
4890                         (substring subject (match-end 0)))
4891                 changed t))
4892         (when (and changed
4893                    (string-match
4894                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4895           (setq subject
4896                 (concat (substring subject 0 (match-beginning 1))
4897                         (substring subject (match-end 1)))))
4898         (when changed
4899           (mail-header-set-subject header subject))))))
4900
4901 (defun gnus-fetch-headers (articles)
4902   "Fetch headers of ARTICLES."
4903   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4904     (gnus-message 5 "Fetching headers for %s..." name)
4905     (prog1
4906         (if (eq 'nov
4907                 (setq gnus-headers-retrieved-by
4908                       (gnus-retrieve-headers
4909                        articles gnus-newsgroup-name
4910                        ;; We might want to fetch old headers, but
4911                        ;; not if there is only 1 article.
4912                        (and (or (and
4913                                  (not (eq gnus-fetch-old-headers 'some))
4914                                  (not (numberp gnus-fetch-old-headers)))
4915                                 (> (length articles) 1))
4916                             gnus-fetch-old-headers))))
4917             (gnus-get-newsgroup-headers-xover
4918              articles nil nil gnus-newsgroup-name t)
4919           (gnus-get-newsgroup-headers))
4920       (gnus-message 5 "Fetching headers for %s...done" name))))
4921
4922 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4923   "Select newsgroup GROUP.
4924 If READ-ALL is non-nil, all articles in the group are selected.
4925 If SELECT-ARTICLES, only select those articles from GROUP."
4926   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4927          ;;!!! Dirty hack; should be removed.
4928          (gnus-summary-ignore-duplicates
4929           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4930               t
4931             gnus-summary-ignore-duplicates))
4932          (info (nth 2 entry))
4933          articles fetched-articles cached)
4934
4935     (unless (gnus-check-server
4936              (set (make-local-variable 'gnus-current-select-method)
4937                   (gnus-find-method-for-group group)))
4938       (error "Couldn't open server"))
4939
4940     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4941         (gnus-activate-group group)     ; Or we can activate it...
4942         (progn                          ; Or we bug out.
4943           (when (equal major-mode 'gnus-summary-mode)
4944             (gnus-kill-buffer (current-buffer)))
4945           (error "Couldn't activate group %s: %s"
4946                  group (gnus-status-message group))))
4947
4948     (unless (gnus-request-group group t)
4949       (when (equal major-mode 'gnus-summary-mode)
4950         (gnus-kill-buffer (current-buffer)))
4951       (error "Couldn't request group %s: %s"
4952              group (gnus-status-message group)))
4953
4954     (setq gnus-newsgroup-name group
4955           gnus-newsgroup-unselected nil
4956           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4957
4958     (let ((display (gnus-group-find-parameter group 'display)))
4959       (setq gnus-newsgroup-display
4960             (cond
4961              ((not (zerop (or (car-safe read-all) 0)))
4962               ;; The user entered the group with C-u SPC/RET, let's show
4963               ;; all articles.
4964               'gnus-not-ignore)
4965              ((eq display 'all)
4966               'gnus-not-ignore)
4967              ((arrayp display)
4968               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4969              ((numberp display)
4970               ;; The following is probably the "correct" solution, but
4971               ;; it makes Gnus fetch all headers and then limit the
4972               ;; articles (which is slow), so instead we hack the
4973               ;; select-articles parameter instead. -- Simon Josefsson
4974               ;; <jas@kth.se>
4975               ;;
4976               ;; (gnus-byte-compile
4977               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4978               ;;                         display)))))
4979               (setq select-articles
4980                     (gnus-uncompress-range
4981                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4982                              (if (> tmp 0)
4983                                  tmp
4984                                1))
4985                            (cdr (gnus-active group)))))
4986               nil)
4987              (t
4988               nil))))
4989
4990     (gnus-summary-setup-default-charset)
4991
4992     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4993     (when (gnus-virtual-group-p group)
4994       (setq cached gnus-newsgroup-cached))
4995
4996     (setq gnus-newsgroup-unreads
4997           (gnus-sorted-ndifference
4998            (gnus-sorted-ndifference gnus-newsgroup-unreads
4999                                     gnus-newsgroup-marked)
5000            gnus-newsgroup-dormant))
5001
5002     (setq gnus-newsgroup-processable nil)
5003
5004     (gnus-update-read-articles group gnus-newsgroup-unreads)
5005
5006     ;; Adjust and set lists of article marks.
5007     (when info
5008       (gnus-adjust-marked-articles info))
5009     (if (setq articles select-articles)
5010         (setq gnus-newsgroup-unselected
5011               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5012       (setq articles (gnus-articles-to-read group read-all)))
5013
5014     (cond
5015      ((null articles)
5016       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5017       'quit)
5018      ((eq articles 0) nil)
5019      (t
5020       ;; Init the dependencies hash table.
5021       (setq gnus-newsgroup-dependencies
5022             (gnus-make-hashtable (length articles)))
5023       (gnus-set-global-variables)
5024       ;; Retrieve the headers and read them in.
5025
5026       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5027
5028       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5029       (when cached
5030         (setq gnus-newsgroup-cached cached))
5031
5032       ;; Suppress duplicates?
5033       (when gnus-suppress-duplicates
5034         (gnus-dup-suppress-articles))
5035
5036       ;; Set the initial limit.
5037       (setq gnus-newsgroup-limit (copy-sequence articles))
5038       ;; Remove canceled articles from the list of unread articles.
5039       (setq fetched-articles
5040             (mapcar (lambda (headers) (mail-header-number headers))
5041                     gnus-newsgroup-headers))
5042       (setq gnus-newsgroup-articles fetched-articles)
5043       (setq gnus-newsgroup-unreads
5044             (gnus-sorted-nintersection
5045              gnus-newsgroup-unreads fetched-articles))
5046       (gnus-compute-unseen-list)
5047
5048       ;; Removed marked articles that do not exist.
5049       (gnus-update-missing-marks
5050        (gnus-sorted-difference articles fetched-articles))
5051       ;; We might want to build some more threads first.
5052       (when (and gnus-fetch-old-headers
5053                  (eq gnus-headers-retrieved-by 'nov))
5054         (if (eq gnus-fetch-old-headers 'invisible)
5055             (gnus-build-all-threads)
5056           (gnus-build-old-threads)))
5057       ;; Let the Gnus agent mark articles as read.
5058       (when gnus-agent
5059         (gnus-agent-get-undownloaded-list))
5060       ;; Remove list identifiers from subject
5061       (when gnus-list-identifiers
5062         (gnus-summary-remove-list-identifiers))
5063       ;; Check whether auto-expire is to be done in this group.
5064       (setq gnus-newsgroup-auto-expire
5065             (gnus-group-auto-expirable-p group))
5066       ;; Set up the article buffer now, if necessary.
5067       (unless gnus-single-article-buffer
5068         (gnus-article-setup-buffer))
5069       ;; First and last article in this newsgroup.
5070       (when gnus-newsgroup-headers
5071         (setq gnus-newsgroup-begin
5072               (mail-header-number (car gnus-newsgroup-headers))
5073               gnus-newsgroup-end
5074               (mail-header-number
5075                (gnus-last-element gnus-newsgroup-headers))))
5076       ;; GROUP is successfully selected.
5077       (or gnus-newsgroup-headers t)))))
5078
5079 (defun gnus-compute-unseen-list ()
5080   ;; The `seen' marks are treated specially.
5081   (if (not gnus-newsgroup-seen)
5082       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5083     (setq gnus-newsgroup-unseen
5084           (gnus-inverse-list-range-intersection
5085            gnus-newsgroup-articles gnus-newsgroup-seen))))
5086
5087 (defun gnus-summary-display-make-predicate (display)
5088   (require 'gnus-agent)
5089   (when (= (length display) 1)
5090     (setq display (car display)))
5091   (unless gnus-summary-display-cache
5092     (dolist (elem (append '((unread . unread)
5093                             (read . read)
5094                             (unseen . unseen))
5095                           gnus-article-mark-lists))
5096       (push (cons (cdr elem)
5097                   (gnus-byte-compile
5098                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5099             gnus-summary-display-cache)))
5100   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5101         (gnus-category-predicate-cache gnus-summary-display-cache))
5102     (gnus-get-predicate display)))
5103
5104 ;; Uses the dynamically bound `number' variable.
5105 (eval-when-compile
5106   (defvar number))
5107 (defun gnus-article-marked-p (type &optional article)
5108   (let ((article (or article number)))
5109     (cond
5110      ((eq type 'tick)
5111       (memq article gnus-newsgroup-marked))
5112      ((eq type 'spam)
5113       (memq article gnus-newsgroup-spam-marked))
5114      ((eq type 'unsend)
5115       (memq article gnus-newsgroup-unsendable))
5116      ((eq type 'undownload)
5117       (memq article gnus-newsgroup-undownloaded))
5118      ((eq type 'download)
5119       (memq article gnus-newsgroup-downloadable))
5120      ((eq type 'unread)
5121       (memq article gnus-newsgroup-unreads))
5122      ((eq type 'read)
5123       (memq article gnus-newsgroup-reads))
5124      ((eq type 'dormant)
5125       (memq article gnus-newsgroup-dormant) )
5126      ((eq type 'expire)
5127       (memq article gnus-newsgroup-expirable))
5128      ((eq type 'reply)
5129       (memq article gnus-newsgroup-replied))
5130      ((eq type 'killed)
5131       (memq article gnus-newsgroup-killed))
5132      ((eq type 'bookmark)
5133       (assq article gnus-newsgroup-bookmarks))
5134      ((eq type 'score)
5135       (assq article gnus-newsgroup-scored))
5136      ((eq type 'save)
5137       (memq article gnus-newsgroup-saved))
5138      ((eq type 'cache)
5139       (memq article gnus-newsgroup-cached))
5140      ((eq type 'forward)
5141       (memq article gnus-newsgroup-forwarded))
5142      ((eq type 'seen)
5143       (not (memq article gnus-newsgroup-unseen)))
5144      ((eq type 'recent)
5145       (memq article gnus-newsgroup-recent))
5146      (t t))))
5147
5148 (defun gnus-articles-to-read (group &optional read-all)
5149   "Find out what articles the user wants to read."
5150   (let* ((display (gnus-group-find-parameter group 'display))
5151          (articles
5152           ;; Select all articles if `read-all' is non-nil, or if there
5153           ;; are no unread articles.
5154           (if (or read-all
5155                   (and (zerop (length gnus-newsgroup-marked))
5156                        (zerop (length gnus-newsgroup-unreads)))
5157                   ;; Fetch all if the predicate is non-nil.
5158                   gnus-newsgroup-display)
5159               ;; We want to select the headers for all the articles in
5160               ;; the group, so we select either all the active
5161               ;; articles in the group, or (if that's nil), the
5162               ;; articles in the cache.
5163               (or
5164                (gnus-uncompress-range (gnus-active group))
5165                (gnus-cache-articles-in-group group))
5166             ;; Select only the "normal" subset of articles.
5167             (gnus-sorted-nunion
5168              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5169              gnus-newsgroup-unreads)))
5170          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5171          (scored (length scored-list))
5172          (number (length articles))
5173          (marked (+ (length gnus-newsgroup-marked)
5174                     (length gnus-newsgroup-dormant)))
5175          (select
5176           (cond
5177            ((numberp read-all)
5178             read-all)
5179            ((numberp gnus-newsgroup-display)
5180             gnus-newsgroup-display)
5181            (t
5182             (condition-case ()
5183                 (cond
5184                  ((and (or (<= scored marked) (= scored number))
5185                        (numberp gnus-large-newsgroup)
5186                        (> number gnus-large-newsgroup))
5187                   (let* ((cursor-in-echo-area nil)
5188                          (initial (gnus-parameter-large-newsgroup-initial
5189                                    gnus-newsgroup-name))
5190                          (input
5191                           (read-string
5192                            (format
5193                             "How many articles from %s (%s %d): "
5194                             (gnus-limit-string
5195                              (gnus-group-decoded-name gnus-newsgroup-name)
5196                              35)
5197                             (if initial "max" "default")
5198                             number)
5199                            (if initial
5200                                (cons (number-to-string initial)
5201                                      0)))))
5202                     (if (string-match "^[ \t]*$" input) number input)))
5203                  ((and (> scored marked) (< scored number)
5204                        (> (- scored number) 20))
5205                   (let ((input
5206                          (read-string
5207                           (format "%s %s (%d scored, %d total): "
5208                                   "How many articles from"
5209                                   (gnus-group-decoded-name group)
5210                                   scored number))))
5211                     (if (string-match "^[ \t]*$" input)
5212                         number input)))
5213                  (t number))
5214               (quit
5215                (message "Quit getting the articles to read")
5216                nil))))))
5217     (setq select (if (stringp select) (string-to-number select) select))
5218     (if (or (null select) (zerop select))
5219         select
5220       (if (and (not (zerop scored)) (<= (abs select) scored))
5221           (progn
5222             (setq articles (sort scored-list '<))
5223             (setq number (length articles)))
5224         (setq articles (copy-sequence articles)))
5225
5226       (when (< (abs select) number)
5227         (if (< select 0)
5228             ;; Select the N oldest articles.
5229             (setcdr (nthcdr (1- (abs select)) articles) nil)
5230           ;; Select the N most recent articles.
5231           (setq articles (nthcdr (- number select) articles))))
5232       (setq gnus-newsgroup-unselected
5233             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5234       (when gnus-alter-articles-to-read-function
5235         (setq articles
5236               (sort
5237                (funcall gnus-alter-articles-to-read-function
5238                         gnus-newsgroup-name articles)
5239                '<)))
5240       articles)))
5241
5242 (defun gnus-killed-articles (killed articles)
5243   (let (out)
5244     (while articles
5245       (when (inline (gnus-member-of-range (car articles) killed))
5246         (push (car articles) out))
5247       (setq articles (cdr articles)))
5248     out))
5249
5250 (defun gnus-uncompress-marks (marks)
5251   "Uncompress the mark ranges in MARKS."
5252   (let ((uncompressed '(score bookmark))
5253         out)
5254     (while marks
5255       (if (memq (caar marks) uncompressed)
5256           (push (car marks) out)
5257         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5258       (setq marks (cdr marks)))
5259     out))
5260
5261 (defun gnus-article-mark-to-type (mark)
5262   "Return the type of MARK."
5263   (or (cadr (assq mark gnus-article-special-mark-lists))
5264       'list))
5265
5266 (defun gnus-article-unpropagatable-p (mark)
5267   "Return whether MARK should be propagated to backend."
5268   (memq mark gnus-article-unpropagated-mark-lists))
5269
5270 (defun gnus-adjust-marked-articles (info)
5271   "Set all article lists and remove all marks that are no longer valid."
5272   (let* ((marked-lists (gnus-info-marks info))
5273          (active (gnus-active (gnus-info-group info)))
5274          (min (car active))
5275          (max (cdr active))
5276          (types gnus-article-mark-lists)
5277          marks var articles article mark mark-type)
5278
5279     (dolist (marks marked-lists)
5280       (setq mark (car marks)
5281             mark-type (gnus-article-mark-to-type mark)
5282             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5283
5284       ;; We set the variable according to the type of the marks list,
5285       ;; and then adjust the marks to a subset of the active articles.
5286       (cond
5287        ;; Adjust "simple" lists.
5288        ((eq mark-type 'list)
5289         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5290         (when (memq mark '(tick dormant expire reply save))
5291           (while articles
5292             (when (or (< (setq article (pop articles)) min) (> article max))
5293               (set var (delq article (symbol-value var)))))))
5294        ;; Adjust assocs.
5295        ((eq mark-type 'tuple)
5296         (set var (setq articles (cdr marks)))
5297         (when (not (listp (cdr (symbol-value var))))
5298           (set var (list (symbol-value var))))
5299         (when (not (listp (cdr articles)))
5300           (setq articles (list articles)))
5301         (while articles
5302           (when (or (not (consp (setq article (pop articles))))
5303                     (< (car article) min)
5304                     (> (car article) max))
5305             (set var (delq article (symbol-value var))))))
5306        ;; Adjust ranges (sloppily).
5307        ((eq mark-type 'range)
5308         (cond
5309          ((eq mark 'seen)
5310           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5311           ;; It should be (seen (NUM1 . NUM2)).
5312           (when (numberp (cddr marks))
5313             (setcdr marks (list (cdr marks))))
5314           (setq articles (cdr marks))
5315           (while (and articles
5316                       (or (and (consp (car articles))
5317                                (> min (cdar articles)))
5318                           (and (numberp (car articles))
5319                                (> min (car articles)))))
5320             (pop articles))
5321           (set var articles))))))))
5322
5323 (defun gnus-update-missing-marks (missing)
5324   "Go through the list of MISSING articles and remove them from the mark lists."
5325   (when missing
5326     (let (var m)
5327       ;; Go through all types.
5328       (dolist (elem gnus-article-mark-lists)
5329         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5330           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5331           (when (symbol-value var)
5332             ;; This list has articles.  So we delete all missing
5333             ;; articles from it.
5334             (setq m missing)
5335             (while m
5336               (set var (delq (pop m) (symbol-value var))))))))))
5337
5338 (defun gnus-update-marks ()
5339   "Enter the various lists of marked articles into the newsgroup info list."
5340   (let ((types gnus-article-mark-lists)
5341         (info (gnus-get-info gnus-newsgroup-name))
5342         type list newmarked symbol delta-marks)
5343     (when info
5344       ;; Add all marks lists to the list of marks lists.
5345       (while (setq type (pop types))
5346         (setq list (symbol-value
5347                     (setq symbol
5348                           (intern (format "gnus-newsgroup-%s" (car type))))))
5349
5350         (when list
5351           ;; Get rid of the entries of the articles that have the
5352           ;; default score.
5353           (when (and (eq (cdr type) 'score)
5354                      gnus-save-score
5355                      list)
5356             (let* ((arts list)
5357                    (prev (cons nil list))
5358                    (all prev))
5359               (while arts
5360                 (if (or (not (consp (car arts)))
5361                         (= (cdar arts) gnus-summary-default-score))
5362                     (setcdr prev (cdr arts))
5363                   (setq prev arts))
5364                 (setq arts (cdr arts)))
5365               (setq list (cdr all)))))
5366
5367         (when (eq (cdr type) 'seen)
5368           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5369
5370         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5371           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5372
5373         (when (and (gnus-check-backend-function
5374                     'request-set-mark gnus-newsgroup-name)
5375                    (not (gnus-article-unpropagatable-p (cdr type))))
5376           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5377                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5378                  (add (gnus-remove-from-range
5379                        (gnus-copy-sequence list) old)))
5380             (when add
5381               (push (list add 'add (list (cdr type))) delta-marks))
5382             (when del
5383               (push (list del 'del (list (cdr type))) delta-marks))))
5384
5385         (when list
5386           (push (cons (cdr type) list) newmarked)))
5387
5388       (when delta-marks
5389         (unless (gnus-check-group gnus-newsgroup-name)
5390           (error "Can't open server for %s" gnus-newsgroup-name))
5391         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5392
5393       ;; Enter these new marks into the info of the group.
5394       (if (nthcdr 3 info)
5395           (setcar (nthcdr 3 info) newmarked)
5396         ;; Add the marks lists to the end of the info.
5397         (when newmarked
5398           (setcdr (nthcdr 2 info) (list newmarked))))
5399
5400       ;; Cut off the end of the info if there's nothing else there.
5401       (let ((i 5))
5402         (while (and (> i 2)
5403                     (not (nth i info)))
5404           (when (nthcdr (decf i) info)
5405             (setcdr (nthcdr i info) nil)))))))
5406
5407 (defun gnus-set-mode-line (where)
5408   "Set the mode line of the article or summary buffers.
5409 If WHERE is `summary', the summary mode line format will be used."
5410   ;; Is this mode line one we keep updated?
5411   (when (and (memq where gnus-updated-mode-lines)
5412              (symbol-value
5413               (intern (format "gnus-%s-mode-line-format-spec" where))))
5414     (let (mode-string)
5415       (save-excursion
5416         ;; We evaluate this in the summary buffer since these
5417         ;; variables are buffer-local to that buffer.
5418         (set-buffer gnus-summary-buffer)
5419        ;; We bind all these variables that are used in the `eval' form
5420         ;; below.
5421         (let* ((mformat (symbol-value
5422                          (intern
5423                           (format "gnus-%s-mode-line-format-spec" where))))
5424                (gnus-tmp-group-name (gnus-group-decoded-name
5425                                      gnus-newsgroup-name))
5426                (gnus-tmp-article-number (or gnus-current-article 0))
5427                (gnus-tmp-unread gnus-newsgroup-unreads)
5428                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5429                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5430                (gnus-tmp-unread-and-unselected
5431                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5432                             (zerop gnus-tmp-unselected))
5433                        "")
5434                       ((zerop gnus-tmp-unselected)
5435                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5436                       (t (format "{%d(+%d) more}"
5437                                  gnus-tmp-unread-and-unticked
5438                                  gnus-tmp-unselected))))
5439                (gnus-tmp-subject
5440                 (if (and gnus-current-headers
5441                          (vectorp gnus-current-headers))
5442                     (gnus-mode-string-quote
5443                      (mail-header-subject gnus-current-headers))
5444                   ""))
5445                bufname-length max-len
5446                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5447           (setq mode-string (eval mformat))
5448           (setq bufname-length (if (string-match "%b" mode-string)
5449                                    (- (length
5450                                        (buffer-name
5451                                         (if (eq where 'summary)
5452                                             nil
5453                                           (get-buffer gnus-article-buffer))))
5454                                       2)
5455                                  0))
5456           (setq max-len (max 4 (if gnus-mode-non-string-length
5457                                    (- (window-width)
5458                                       gnus-mode-non-string-length
5459                                       bufname-length)
5460                                  (length mode-string))))
5461           ;; We might have to chop a bit of the string off...
5462           (when (> (length mode-string) max-len)
5463             (setq mode-string
5464                   (concat (truncate-string-to-width mode-string (- max-len 3))
5465                           "...")))
5466           ;; Pad the mode string a bit.
5467           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5468       ;; Update the mode line.
5469       (setq mode-line-buffer-identification
5470             (gnus-mode-line-buffer-identification (list mode-string)))
5471       (set-buffer-modified-p t))))
5472
5473 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5474   "Go through the HEADERS list and add all Xrefs to a hash table.
5475 The resulting hash table is returned, or nil if no Xrefs were found."
5476   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5477          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5478          (xref-hashtb (gnus-make-hashtable))
5479          start group entry number xrefs header)
5480     (while headers
5481       (setq header (pop headers))
5482       (when (and (setq xrefs (mail-header-xref header))
5483                  (not (memq (setq number (mail-header-number header))
5484                             unreads)))
5485         (setq start 0)
5486         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5487           (setq start (match-end 0))
5488           (setq group (if prefix
5489                           (concat prefix (substring xrefs (match-beginning 1)
5490                                                     (match-end 1)))
5491                         (substring xrefs (match-beginning 1) (match-end 1))))
5492           (setq number
5493                 (string-to-int (substring xrefs (match-beginning 2)
5494                                           (match-end 2))))
5495           (if (setq entry (gnus-gethash group xref-hashtb))
5496               (setcdr entry (cons number (cdr entry)))
5497             (gnus-sethash group (cons number nil) xref-hashtb)))))
5498     (and start xref-hashtb)))
5499
5500 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5501   "Look through all the headers and mark the Xrefs as read."
5502   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5503         name entry info xref-hashtb idlist method nth4)
5504     (save-excursion
5505       (set-buffer gnus-group-buffer)
5506       (when (setq xref-hashtb
5507                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5508         (mapatoms
5509          (lambda (group)
5510            (unless (string= from-newsgroup (setq name (symbol-name group)))
5511              (setq idlist (symbol-value group))
5512              ;; Dead groups are not updated.
5513              (and (prog1
5514                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5515                             info (nth 2 entry))
5516                     (when (stringp (setq nth4 (gnus-info-method info)))
5517                       (setq nth4 (gnus-server-to-method nth4))))
5518                   ;; Only do the xrefs if the group has the same
5519                   ;; select method as the group we have just read.
5520                   (or (gnus-methods-equal-p
5521                        nth4 (gnus-find-method-for-group from-newsgroup))
5522                       virtual
5523                       (equal nth4 (setq method (gnus-find-method-for-group
5524                                                 from-newsgroup)))
5525                       (and (equal (car nth4) (car method))
5526                            (equal (nth 1 nth4) (nth 1 method))))
5527                   gnus-use-cross-reference
5528                   (or (not (eq gnus-use-cross-reference t))
5529                       virtual
5530                       ;; Only do cross-references on subscribed
5531                       ;; groups, if that is what is wanted.
5532                       (<= (gnus-info-level info) gnus-level-subscribed))
5533                   (gnus-group-make-articles-read name idlist))))
5534          xref-hashtb)))))
5535
5536 (defun gnus-compute-read-articles (group articles)
5537   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5538          (info (nth 2 entry))
5539          (active (gnus-active group))
5540          ninfo)
5541     (when entry
5542       ;; First peel off all invalid article numbers.
5543       (when active
5544         (let ((ids articles)
5545               id first)
5546           (while (setq id (pop ids))
5547             (when (and first (> id (cdr active)))
5548               ;; We'll end up in this situation in one particular
5549               ;; obscure situation.  If you re-scan a group and get
5550               ;; a new article that is cross-posted to a different
5551               ;; group that has not been re-scanned, you might get
5552               ;; crossposted article that has a higher number than
5553               ;; Gnus believes possible.  So we re-activate this
5554               ;; group as well.  This might mean doing the
5555               ;; crossposting thingy will *increase* the number
5556               ;; of articles in some groups.  Tsk, tsk.
5557               (setq active (or (gnus-activate-group group) active)))
5558             (when (or (> id (cdr active))
5559                       (< id (car active)))
5560               (setq articles (delq id articles))))))
5561       ;; If the read list is nil, we init it.
5562       (if (and active
5563                (null (gnus-info-read info))
5564                (> (car active) 1))
5565           (setq ninfo (cons 1 (1- (car active))))
5566         (setq ninfo (gnus-info-read info)))
5567       ;; Then we add the read articles to the range.
5568       (gnus-add-to-range
5569        ninfo (setq articles (sort articles '<))))))
5570
5571 (defun gnus-group-make-articles-read (group articles)
5572   "Update the info of GROUP to say that ARTICLES are read."
5573   (let* ((num 0)
5574          (entry (gnus-gethash group gnus-newsrc-hashtb))
5575          (info (nth 2 entry))
5576          (active (gnus-active group))
5577          range)
5578     (when entry
5579       (setq range (gnus-compute-read-articles group articles))
5580       (save-excursion
5581         (set-buffer gnus-group-buffer)
5582         (gnus-undo-register
5583           `(progn
5584              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5585              (gnus-info-set-read ',info ',(gnus-info-read info))
5586              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5587              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5588              (gnus-group-update-group ,group t))))
5589       ;; Add the read articles to the range.
5590       (gnus-info-set-read info range)
5591       (gnus-request-set-mark group (list (list range 'add '(read))))
5592       ;; Then we have to re-compute how many unread
5593       ;; articles there are in this group.
5594       (when active
5595         (cond
5596          ((not range)
5597           (setq num (- (1+ (cdr active)) (car active))))
5598          ((not (listp (cdr range)))
5599           (setq num (- (cdr active) (- (1+ (cdr range))
5600                                        (car range)))))
5601          (t
5602           (while range
5603             (if (numberp (car range))
5604                 (setq num (1+ num))
5605               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5606             (setq range (cdr range)))
5607           (setq num (- (cdr active) num))))
5608         ;; Update the number of unread articles.
5609         (setcar entry num)
5610         ;; Update the group buffer.
5611         (unless (gnus-ephemeral-group-p group)
5612           (gnus-group-update-group group t))))))
5613
5614 (defvar gnus-newsgroup-none-id 0)
5615
5616 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5617   (let ((cur nntp-server-buffer)
5618         (dependencies
5619          (or dependencies
5620              (save-excursion (set-buffer gnus-summary-buffer)
5621                              gnus-newsgroup-dependencies)))
5622         headers id end ref
5623         (mail-parse-charset gnus-newsgroup-charset)
5624         (mail-parse-ignored-charsets
5625          (save-excursion (condition-case nil
5626                              (set-buffer gnus-summary-buffer)
5627                            (error))
5628                          gnus-newsgroup-ignored-charsets)))
5629     (save-excursion
5630       (set-buffer nntp-server-buffer)
5631       ;; Translate all TAB characters into SPACE characters.
5632       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5633       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5634       (gnus-run-hooks 'gnus-parse-headers-hook)
5635       (let ((case-fold-search t)
5636             in-reply-to header p lines chars)
5637         (goto-char (point-min))
5638         ;; Search to the beginning of the next header.  Error messages
5639         ;; do not begin with 2 or 3.
5640         (while (re-search-forward "^[23][0-9]+ " nil t)
5641           (setq id nil
5642                 ref nil)
5643           ;; This implementation of this function, with nine
5644           ;; search-forwards instead of the one re-search-forward and
5645           ;; a case (which basically was the old function) is actually
5646           ;; about twice as fast, even though it looks messier.  You
5647           ;; can't have everything, I guess.  Speed and elegance
5648           ;; doesn't always go hand in hand.
5649           (setq
5650            header
5651            (vector
5652             ;; Number.
5653             (prog1
5654                 (read cur)
5655               (end-of-line)
5656               (setq p (point))
5657               (narrow-to-region (point)
5658                                 (or (and (search-forward "\n.\n" nil t)
5659                                          (- (point) 2))
5660                                     (point))))
5661             ;; Subject.
5662             (progn
5663               (goto-char p)
5664               (if (search-forward "\nsubject:" nil t)
5665                   (funcall gnus-decode-encoded-word-function
5666                            (nnheader-header-value))
5667                 "(none)"))
5668             ;; From.
5669             (progn
5670               (goto-char p)
5671               (if (search-forward "\nfrom:" nil t)
5672                   (funcall gnus-decode-encoded-word-function
5673                            (nnheader-header-value))
5674                 "(nobody)"))
5675             ;; Date.
5676             (progn
5677               (goto-char p)
5678               (if (search-forward "\ndate:" nil t)
5679                   (nnheader-header-value) ""))
5680             ;; Message-ID.
5681             (progn
5682               (goto-char p)
5683               (setq id (if (re-search-forward
5684                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5685                            ;; We do it this way to make sure the Message-ID
5686                            ;; is (somewhat) syntactically valid.
5687                            (buffer-substring (match-beginning 1)
5688                                              (match-end 1))
5689                          ;; If there was no message-id, we just fake one
5690                          ;; to make subsequent routines simpler.
5691                          (nnheader-generate-fake-message-id))))
5692             ;; References.
5693             (progn
5694               (goto-char p)
5695               (if (search-forward "\nreferences:" nil t)
5696                   (progn
5697                     (setq end (point))
5698                     (prog1
5699                         (nnheader-header-value)
5700                       (setq ref
5701                             (buffer-substring
5702                              (progn
5703                                (end-of-line)
5704                                (search-backward ">" end t)
5705                                (1+ (point)))
5706                              (progn
5707                                (search-backward "<" end t)
5708                                (point))))))
5709                 ;; Get the references from the in-reply-to header if there
5710                 ;; were no references and the in-reply-to header looks
5711                 ;; promising.
5712                 (if (and (search-forward "\nin-reply-to:" nil t)
5713                          (setq in-reply-to (nnheader-header-value))
5714                          (string-match "<[^>]+>" in-reply-to))
5715                     (let (ref2)
5716                       (setq ref (substring in-reply-to (match-beginning 0)
5717                                            (match-end 0)))
5718                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5719                         (setq ref2 (substring in-reply-to (match-beginning 0)
5720                                               (match-end 0)))
5721                         (when (> (length ref2) (length ref))
5722                           (setq ref ref2)))
5723                       ref)
5724                   (setq ref nil))))
5725             ;; Chars.
5726             (progn
5727               (goto-char p)
5728               (if (search-forward "\nchars: " nil t)
5729                   (if (numberp (setq chars (ignore-errors (read cur))))
5730                       chars -1)
5731                 -1))
5732             ;; Lines.
5733             (progn
5734               (goto-char p)
5735               (if (search-forward "\nlines: " nil t)
5736                   (if (numberp (setq lines (ignore-errors (read cur))))
5737                       lines -1)
5738                 -1))
5739             ;; Xref.
5740             (progn
5741               (goto-char p)
5742               (and (search-forward "\nxref:" nil t)
5743                    (nnheader-header-value)))
5744             ;; Extra.
5745             (when gnus-extra-headers
5746               (let ((extra gnus-extra-headers)
5747                     out)
5748                 (while extra
5749                   (goto-char p)
5750                   (when (search-forward
5751                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5752                     (push (cons (car extra) (nnheader-header-value))
5753                           out))
5754                   (pop extra))
5755                 out))))
5756           (when (equal id ref)
5757             (setq ref nil))
5758
5759           (when gnus-alter-header-function
5760             (funcall gnus-alter-header-function header)
5761             (setq id (mail-header-id header)
5762                   ref (gnus-parent-id (mail-header-references header))))
5763
5764           (when (setq header
5765                       (gnus-dependencies-add-header
5766                        header dependencies force-new))
5767             (push header headers))
5768           (goto-char (point-max))
5769           (widen))
5770         (nreverse headers)))))
5771
5772 ;; Goes through the xover lines and returns a list of vectors
5773 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5774                                                   force-new dependencies
5775                                                   group also-fetch-heads)
5776   "Parse the news overview data in the server buffer.
5777 Return a list of headers that match SEQUENCE (see
5778 `nntp-retrieve-headers')."
5779   ;; Get the Xref when the users reads the articles since most/some
5780   ;; NNTP servers do not include Xrefs when using XOVER.
5781   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5782   (let ((mail-parse-charset gnus-newsgroup-charset)
5783         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5784         (cur nntp-server-buffer)
5785         (dependencies (or dependencies gnus-newsgroup-dependencies))
5786         (allp (cond
5787                ((eq gnus-read-all-available-headers t)
5788                 t)
5789                ((stringp gnus-read-all-available-headers)
5790                 (string-match gnus-read-all-available-headers group))
5791                (t
5792                 nil)))
5793         number headers header)
5794     (save-excursion
5795       (set-buffer nntp-server-buffer)
5796       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5797       ;; Allow the user to mangle the headers before parsing them.
5798       (gnus-run-hooks 'gnus-parse-headers-hook)
5799       (goto-char (point-min))
5800       (gnus-parse-without-error
5801         (while (and (or sequence allp)
5802                     (not (eobp)))
5803           (setq number (read cur))
5804           (when (not allp)
5805             (while (and sequence
5806                         (< (car sequence) number))
5807               (setq sequence (cdr sequence))))
5808           (when (and (or allp
5809                          (and sequence
5810                               (eq number (car sequence))))
5811                      (progn
5812                        (setq sequence (cdr sequence))
5813                        (setq header (inline
5814                                       (gnus-nov-parse-line
5815                                        number dependencies force-new)))))
5816             (push header headers))
5817           (forward-line 1)))
5818       ;; A common bug in inn is that if you have posted an article and
5819       ;; then retrieves the active file, it will answer correctly --
5820       ;; the new article is included.  However, a NOV entry for the
5821       ;; article may not have been generated yet, so this may fail.
5822       ;; We work around this problem by retrieving the last few
5823       ;; headers using HEAD.
5824       (if (or (not also-fetch-heads)
5825               (not sequence))
5826           ;; We (probably) got all the headers.
5827           (nreverse headers)
5828         (let ((gnus-nov-is-evil t))
5829           (nconc
5830            (nreverse headers)
5831            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5832              (gnus-get-newsgroup-headers))))))))
5833
5834 (defun gnus-article-get-xrefs ()
5835   "Fill in the Xref value in `gnus-current-headers', if necessary.
5836 This is meant to be called in `gnus-article-internal-prepare-hook'."
5837   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5838                                  gnus-current-headers)))
5839     (or (not gnus-use-cross-reference)
5840         (not headers)
5841         (and (mail-header-xref headers)
5842              (not (string= (mail-header-xref headers) "")))
5843         (let ((case-fold-search t)
5844               xref)
5845           (save-restriction
5846             (nnheader-narrow-to-headers)
5847             (goto-char (point-min))
5848             (when (or (and (not (eobp))
5849                            (eq (downcase (char-after)) ?x)
5850                            (looking-at "Xref:"))
5851                       (search-forward "\nXref:" nil t))
5852               (goto-char (1+ (match-end 0)))
5853               (setq xref (buffer-substring (point)
5854                                            (progn (end-of-line) (point))))
5855               (mail-header-set-xref headers xref)))))))
5856
5857 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5858   "Find article ID and insert the summary line for that article.
5859 OLD-HEADER can either be a header or a line number to insert
5860 the subject line on."
5861   (let* ((line (and (numberp old-header) old-header))
5862          (old-header (and (vectorp old-header) old-header))
5863          (header (cond ((and old-header use-old-header)
5864                         old-header)
5865                        ((and (numberp id)
5866                              (gnus-number-to-header id))
5867                         (gnus-number-to-header id))
5868                        (t
5869                         (gnus-read-header id))))
5870          (number (and (numberp id) id))
5871          d)
5872     (when header
5873       ;; Rebuild the thread that this article is part of and go to the
5874       ;; article we have fetched.
5875       (when (and (not gnus-show-threads)
5876                  old-header)
5877         (when (and number
5878                    (setq d (gnus-data-find (mail-header-number old-header))))
5879           (goto-char (gnus-data-pos d))
5880           (gnus-data-remove
5881            number
5882            (- (gnus-point-at-bol)
5883               (prog1
5884                   (1+ (gnus-point-at-eol))
5885                 (gnus-delete-line))))))
5886       (when old-header
5887         (mail-header-set-number header (mail-header-number old-header)))
5888       (setq gnus-newsgroup-sparse
5889             (delq (setq number (mail-header-number header))
5890                   gnus-newsgroup-sparse))
5891       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5892       (push number gnus-newsgroup-limit)
5893       (gnus-rebuild-thread (mail-header-id header) line)
5894       (gnus-summary-goto-subject number nil t))
5895     (when (and (numberp number)
5896                (> number 0))
5897       ;; We have to update the boundaries even if we can't fetch the
5898       ;; article if ID is a number -- so that the next `P' or `N'
5899       ;; command will fetch the previous (or next) article even
5900       ;; if the one we tried to fetch this time has been canceled.
5901       (when (> number gnus-newsgroup-end)
5902         (setq gnus-newsgroup-end number))
5903       (when (< number gnus-newsgroup-begin)
5904         (setq gnus-newsgroup-begin number))
5905       (setq gnus-newsgroup-unselected
5906             (delq number gnus-newsgroup-unselected)))
5907     ;; Report back a success?
5908     (and header (mail-header-number header))))
5909
5910 ;;; Process/prefix in the summary buffer
5911
5912 (defun gnus-summary-work-articles (n)
5913   "Return a list of articles to be worked upon.
5914 The prefix argument, the list of process marked articles, and the
5915 current article will be taken into consideration."
5916   (save-excursion
5917     (set-buffer gnus-summary-buffer)
5918     (cond
5919      (n
5920       ;; A numerical prefix has been given.
5921       (setq n (prefix-numeric-value n))
5922       (let ((backward (< n 0))
5923             (n (abs (prefix-numeric-value n)))
5924             articles article)
5925         (save-excursion
5926           (while
5927               (and (> n 0)
5928                    (push (setq article (gnus-summary-article-number))
5929                          articles)
5930                    (if backward
5931                        (gnus-summary-find-prev nil article)
5932                      (gnus-summary-find-next nil article)))
5933             (decf n)))
5934         (nreverse articles)))
5935      ((and (gnus-region-active-p) (mark))
5936       (message "region active")
5937       ;; Work on the region between point and mark.
5938       (let ((max (max (point) (mark)))
5939             articles article)
5940         (save-excursion
5941           (goto-char (min (min (point) (mark))))
5942           (while
5943               (and
5944                (push (setq article (gnus-summary-article-number)) articles)
5945                (gnus-summary-find-next nil article)
5946                (< (point) max)))
5947           (nreverse articles))))
5948      (gnus-newsgroup-processable
5949       ;; There are process-marked articles present.
5950       ;; Save current state.
5951       (gnus-summary-save-process-mark)
5952       ;; Return the list.
5953       (reverse gnus-newsgroup-processable))
5954      (t
5955       ;; Just return the current article.
5956       (list (gnus-summary-article-number))))))
5957
5958 (defmacro gnus-summary-iterate (arg &rest forms)
5959   "Iterate over the process/prefixed articles and do FORMS.
5960 ARG is the interactive prefix given to the command.  FORMS will be
5961 executed with point over the summary line of the articles."
5962   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5963     `(let ((,articles (gnus-summary-work-articles ,arg)))
5964        (while ,articles
5965          (gnus-summary-goto-subject (car ,articles))
5966          ,@forms
5967          (pop ,articles)))))
5968
5969 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5970 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5971
5972 (defun gnus-summary-save-process-mark ()
5973   "Push the current set of process marked articles on the stack."
5974   (interactive)
5975   (push (copy-sequence gnus-newsgroup-processable)
5976         gnus-newsgroup-process-stack))
5977
5978 (defun gnus-summary-kill-process-mark ()
5979   "Push the current set of process marked articles on the stack and unmark."
5980   (interactive)
5981   (gnus-summary-save-process-mark)
5982   (gnus-summary-unmark-all-processable))
5983
5984 (defun gnus-summary-yank-process-mark ()
5985   "Pop the last process mark state off the stack and restore it."
5986   (interactive)
5987   (unless gnus-newsgroup-process-stack
5988     (error "Empty mark stack"))
5989   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5990
5991 (defun gnus-summary-process-mark-set (set)
5992   "Make SET into the current process marked articles."
5993   (gnus-summary-unmark-all-processable)
5994   (while set
5995     (gnus-summary-set-process-mark (pop set))))
5996
5997 ;;; Searching and stuff
5998
5999 (defun gnus-summary-search-group (&optional backward use-level)
6000   "Search for next unread newsgroup.
6001 If optional argument BACKWARD is non-nil, search backward instead."
6002   (save-excursion
6003     (set-buffer gnus-group-buffer)
6004     (when (gnus-group-search-forward
6005            backward nil (if use-level (gnus-group-group-level) nil))
6006       (gnus-group-group-name))))
6007
6008 (defun gnus-summary-best-group (&optional exclude-group)
6009   "Find the name of the best unread group.
6010 If EXCLUDE-GROUP, do not go to this group."
6011   (save-excursion
6012     (set-buffer gnus-group-buffer)
6013     (save-excursion
6014       (gnus-group-best-unread-group exclude-group))))
6015
6016 (defun gnus-summary-find-next (&optional unread article backward)
6017   (if backward (gnus-summary-find-prev)
6018     (let* ((dummy (gnus-summary-article-intangible-p))
6019            (article (or article (gnus-summary-article-number)))
6020            (data (gnus-data-find-list article))
6021            result)
6022       (when (and (not dummy)
6023                  (or (not gnus-summary-check-current)
6024                      (not unread)
6025                      (not (gnus-data-unread-p (car data)))))
6026         (setq data (cdr data)))
6027       (when (setq result
6028                   (if unread
6029                       (progn
6030                         (while data
6031                           (unless (memq (gnus-data-number (car data)) 
6032                                         gnus-newsgroup-unfetched)
6033                             (when (gnus-data-unread-p (car data))
6034                               (setq result (car data)
6035                                     data nil)))
6036                           (setq data (cdr data)))
6037                         result)
6038                     (car data)))
6039         (goto-char (gnus-data-pos result))
6040         (gnus-data-number result)))))
6041
6042 (defun gnus-summary-find-prev (&optional unread article)
6043   (let* ((eobp (eobp))
6044          (article (or article (gnus-summary-article-number)))
6045          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6046          result)
6047     (when (and (not eobp)
6048                (or (not gnus-summary-check-current)
6049                    (not unread)
6050                    (not (gnus-data-unread-p (car data)))))
6051       (setq data (cdr data)))
6052     (when (setq result
6053                 (if unread
6054                     (progn
6055                       (while data
6056                         (unless (memq (gnus-data-number (car data)) gnus-newsgroup-unfetched)
6057                           (when (gnus-data-unread-p (car data))
6058                             (setq result (car data)
6059                                   data nil)))
6060                         (setq data (cdr data)))
6061                       result)
6062                   (car data)))
6063       (goto-char (gnus-data-pos result))
6064       (gnus-data-number result))))
6065
6066 (defun gnus-summary-find-subject (subject &optional unread backward article)
6067   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6068          (article (or article (gnus-summary-article-number)))
6069          (articles (gnus-data-list backward))
6070          (arts (gnus-data-find-list article articles))
6071          result)
6072     (when (or (not gnus-summary-check-current)
6073               (not unread)
6074               (not (gnus-data-unread-p (car arts))))
6075       (setq arts (cdr arts)))
6076     (while arts
6077       (and (or (not unread)
6078                (gnus-data-unread-p (car arts)))
6079            (vectorp (gnus-data-header (car arts)))
6080            (gnus-subject-equal
6081             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6082            (setq result (car arts)
6083                  arts nil))
6084       (setq arts (cdr arts)))
6085     (and result
6086          (goto-char (gnus-data-pos result))
6087          (gnus-data-number result))))
6088
6089 (defun gnus-summary-search-forward (&optional unread subject backward)
6090   "Search forward for an article.
6091 If UNREAD, look for unread articles.  If SUBJECT, look for
6092 articles with that subject.  If BACKWARD, search backward instead."
6093   (cond (subject (gnus-summary-find-subject subject unread backward))
6094         (backward (gnus-summary-find-prev unread))
6095         (t (gnus-summary-find-next unread))))
6096
6097 (defun gnus-recenter (&optional n)
6098   "Center point in window and redisplay frame.
6099 Also do horizontal recentering."
6100   (interactive "P")
6101   (when (and gnus-auto-center-summary
6102              (not (eq gnus-auto-center-summary 'vertical)))
6103     (gnus-horizontal-recenter))
6104   (recenter n))
6105
6106 (defun gnus-summary-recenter ()
6107   "Center point in the summary window.
6108 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6109 displayed, no centering will be performed."
6110   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6111   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6112   (interactive)
6113   ;; The user has to want it.
6114   (when gnus-auto-center-summary
6115     (let* ((top (cond ((< (window-height) 4) 0)
6116                       ((< (window-height) 7) 1)
6117                       (t (if (numberp gnus-auto-center-summary)
6118                              gnus-auto-center-summary
6119                            2))))
6120            (height (1- (window-height)))
6121            (bottom (save-excursion (goto-char (point-max))
6122                                    (forward-line (- height))
6123                                    (point)))
6124            (window (get-buffer-window (current-buffer))))
6125       (when (get-buffer-window gnus-article-buffer)
6126         ;; Only do recentering when the article buffer is displayed,
6127         ;; Set the window start to either `bottom', which is the biggest
6128         ;; possible valid number, or the second line from the top,
6129         ;; whichever is the least.
6130         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6131           (if (> bottom top-pos)
6132               ;; Keep the second line from the top visible
6133               (set-window-start window top-pos t)
6134             ;; Try to keep the bottom line visible; if it's partially
6135             ;; obscured, either scroll one more line to make it fully
6136             ;; visible, or revert to using TOP-POS.
6137             (save-excursion
6138               (goto-char (point-max))
6139               (forward-line -1)
6140               (let ((last-line-start (point)))
6141                 (goto-char bottom)
6142                 (set-window-start window (point) t)
6143                 (when (not (pos-visible-in-window-p last-line-start window))
6144                   (forward-line 1)
6145                   (set-window-start window (min (point) top-pos) t)))))))
6146       ;; Do horizontal recentering while we're at it.
6147       (when (and (get-buffer-window (current-buffer) t)
6148                  (not (eq gnus-auto-center-summary 'vertical)))
6149         (let ((selected (selected-window)))
6150           (select-window (get-buffer-window (current-buffer) t))
6151           (gnus-summary-position-point)
6152           (gnus-horizontal-recenter)
6153           (select-window selected))))))
6154
6155 (defun gnus-summary-jump-to-group (newsgroup)
6156   "Move point to NEWSGROUP in group mode buffer."
6157   ;; Keep update point of group mode buffer if visible.
6158   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6159       (save-window-excursion
6160         ;; Take care of tree window mode.
6161         (when (get-buffer-window gnus-group-buffer)
6162           (pop-to-buffer gnus-group-buffer))
6163         (gnus-group-jump-to-group newsgroup))
6164     (save-excursion
6165       ;; Take care of tree window mode.
6166       (if (get-buffer-window gnus-group-buffer)
6167           (pop-to-buffer gnus-group-buffer)
6168         (set-buffer gnus-group-buffer))
6169       (gnus-group-jump-to-group newsgroup))))
6170
6171 ;; This function returns a list of article numbers based on the
6172 ;; difference between the ranges of read articles in this group and
6173 ;; the range of active articles.
6174 (defun gnus-list-of-unread-articles (group)
6175   (let* ((read (gnus-info-read (gnus-get-info group)))
6176          (active (or (gnus-active group) (gnus-activate-group group)))
6177          (last (cdr active))
6178          first nlast unread)
6179     ;; If none are read, then all are unread.
6180     (if (not read)
6181         (setq first (car active))
6182       ;; If the range of read articles is a single range, then the
6183       ;; first unread article is the article after the last read
6184       ;; article.  Sounds logical, doesn't it?
6185       (if (and (not (listp (cdr read)))
6186                (or (< (car read) (car active))
6187                    (progn (setq read (list read))
6188                           nil)))
6189           (setq first (max (car active) (1+ (cdr read))))
6190         ;; `read' is a list of ranges.
6191         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6192                                   (caar read)))
6193                   1)
6194           (setq first (car active)))
6195         (while read
6196           (when first
6197             (while (< first nlast)
6198               (push first unread)
6199               (setq first (1+ first))))
6200           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6201           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6202           (setq read (cdr read)))))
6203     ;; And add the last unread articles.
6204     (while (<= first last)
6205       (push first unread)
6206       (setq first (1+ first)))
6207     ;; Return the list of unread articles.
6208     (delq 0 (nreverse unread))))
6209
6210 (defun gnus-list-of-read-articles (group)
6211   "Return a list of unread, unticked and non-dormant articles."
6212   (let* ((info (gnus-get-info group))
6213          (marked (gnus-info-marks info))
6214          (active (gnus-active group)))
6215     (and info active
6216          (gnus-list-range-difference
6217           (gnus-list-range-difference
6218            (gnus-sorted-complement
6219             (gnus-uncompress-range active)
6220             (gnus-list-of-unread-articles group))
6221            (cdr (assq 'dormant marked)))
6222           (cdr (assq 'tick marked))))))
6223
6224 ;; Various summary commands
6225
6226 (defun gnus-summary-select-article-buffer ()
6227   "Reconfigure windows to show article buffer."
6228   (interactive)
6229   (if (not (gnus-buffer-live-p gnus-article-buffer))
6230       (error "There is no article buffer for this summary buffer")
6231     (gnus-configure-windows 'article)
6232     (select-window (get-buffer-window gnus-article-buffer))))
6233
6234 (defun gnus-summary-universal-argument (arg)
6235   "Perform any operation on all articles that are process/prefixed."
6236   (interactive "P")
6237   (let ((articles (gnus-summary-work-articles arg))
6238         func article)
6239     (if (eq
6240          (setq
6241           func
6242           (key-binding
6243            (read-key-sequence
6244             (substitute-command-keys
6245              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6246          'undefined)
6247         (gnus-error 1 "Undefined key")
6248       (save-excursion
6249         (while articles
6250           (gnus-summary-goto-subject (setq article (pop articles)))
6251           (let (gnus-newsgroup-processable)
6252             (command-execute func))
6253           (gnus-summary-remove-process-mark article)))))
6254   (gnus-summary-position-point))
6255
6256 (defun gnus-summary-toggle-truncation (&optional arg)
6257   "Toggle truncation of summary lines.
6258 With arg, turn line truncation on if arg is positive."
6259   (interactive "P")
6260   (setq truncate-lines
6261         (if (null arg) (not truncate-lines)
6262           (> (prefix-numeric-value arg) 0)))
6263   (redraw-display))
6264
6265 (defun gnus-summary-find-uncancelled ()
6266   "Return the number of an uncancelled article.
6267 The current article is considered, then following articles, then previous
6268 articles.  If all articles are cancelled then return a dummy 0."
6269   (let (found)
6270     (dolist (rev '(nil t))
6271       (unless found      ; don't demand the reverse list if we don't need it
6272         (let ((data (gnus-data-find-list
6273                      (gnus-summary-article-number) (gnus-data-list rev))))
6274           (while (and data (not found))
6275             (if (not (eq gnus-canceled-mark (gnus-data-mark (car data))))
6276                 (setq found (gnus-data-number (car data))))
6277             (setq data (cdr data))))))
6278     (or found 0)))
6279
6280 (defun gnus-summary-reselect-current-group (&optional all rescan)
6281   "Exit and then reselect the current newsgroup.
6282 The prefix argument ALL means to select all articles."
6283   (interactive "P")
6284   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6285     (error "Ephemeral groups can't be reselected"))
6286   (let ((current-subject (gnus-summary-find-uncancelled))
6287         (group gnus-newsgroup-name))
6288     (setq gnus-newsgroup-begin nil)
6289     (gnus-summary-exit)
6290     ;; We have to adjust the point of group mode buffer because
6291     ;; point was moved to the next unread newsgroup by exiting.
6292     (gnus-summary-jump-to-group group)
6293     (when rescan
6294       (save-excursion
6295         (gnus-group-get-new-news-this-group 1)))
6296     (gnus-group-read-group all t)
6297     (gnus-summary-goto-subject current-subject nil t)))
6298
6299 (defun gnus-summary-rescan-group (&optional all)
6300   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6301   (interactive "P")
6302   (gnus-summary-reselect-current-group all t))
6303
6304 (defun gnus-summary-update-info (&optional non-destructive)
6305   (save-excursion
6306     (let ((group gnus-newsgroup-name))
6307       (when group
6308         (when gnus-newsgroup-kill-headers
6309           (setq gnus-newsgroup-killed
6310                 (gnus-compress-sequence
6311                  (gnus-sorted-union
6312                   (gnus-list-range-intersection
6313                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6314                   gnus-newsgroup-unreads)
6315                  t)))
6316         (unless (listp (cdr gnus-newsgroup-killed))
6317           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6318         (let ((headers gnus-newsgroup-headers))
6319           ;; Set the new ranges of read articles.
6320           (save-excursion
6321             (set-buffer gnus-group-buffer)
6322             (gnus-undo-force-boundary))
6323           (gnus-update-read-articles
6324            group (gnus-sorted-union
6325                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6326           ;; Set the current article marks.
6327           (let ((gnus-newsgroup-scored
6328                  (if (and (not gnus-save-score)
6329                           (not non-destructive))
6330                      nil
6331                    gnus-newsgroup-scored)))
6332             (save-excursion
6333               (gnus-update-marks)))
6334           ;; Do the cross-ref thing.
6335           (when gnus-use-cross-reference
6336             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6337           ;; Do not switch windows but change the buffer to work.
6338           (set-buffer gnus-group-buffer)
6339           (unless (gnus-ephemeral-group-p group)
6340             (gnus-group-update-group group)))))))
6341
6342 (defun gnus-summary-save-newsrc (&optional force)
6343   "Save the current number of read/marked articles in the dribble buffer.
6344 The dribble buffer will then be saved.
6345 If FORCE (the prefix), also save the .newsrc file(s)."
6346   (interactive "P")
6347   (gnus-summary-update-info t)
6348   (if force
6349       (gnus-save-newsrc-file)
6350     (gnus-dribble-save)))
6351
6352 (defun gnus-summary-exit (&optional temporary)
6353   "Exit reading current newsgroup, and then return to group selection mode.
6354 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6355   (interactive)
6356   (gnus-set-global-variables)
6357   (when (gnus-buffer-live-p gnus-article-buffer)
6358     (save-excursion
6359       (set-buffer gnus-article-buffer)
6360       (mm-destroy-parts gnus-article-mime-handles)
6361       ;; Set it to nil for safety reason.
6362       (setq gnus-article-mime-handle-alist nil)
6363       (setq gnus-article-mime-handles nil)))
6364   (gnus-kill-save-kill-buffer)
6365   (gnus-async-halt-prefetch)
6366   (let* ((group gnus-newsgroup-name)
6367          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6368          (gnus-group-is-exiting-p t)
6369          (mode major-mode)
6370          (group-point nil)
6371          (buf (current-buffer)))
6372     (unless quit-config
6373       ;; Do adaptive scoring, and possibly save score files.
6374       (when gnus-newsgroup-adaptive
6375         (gnus-score-adaptive))
6376       (when gnus-use-scoring
6377         (gnus-score-save)))
6378     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6379     ;; If we have several article buffers, we kill them at exit.
6380     (unless gnus-single-article-buffer
6381       (gnus-kill-buffer gnus-original-article-buffer)
6382       (setq gnus-article-current nil))
6383     (when gnus-use-cache
6384       (gnus-cache-possibly-remove-articles)
6385       (gnus-cache-save-buffers))
6386     (gnus-async-prefetch-remove-group group)
6387     (when gnus-suppress-duplicates
6388       (gnus-dup-enter-articles))
6389     (when gnus-use-trees
6390       (gnus-tree-close group))
6391     (when gnus-use-cache
6392       (gnus-cache-write-active))
6393     ;; Remove entries for this group.
6394     (nnmail-purge-split-history (gnus-group-real-name group))
6395     ;; Make all changes in this group permanent.
6396     (unless quit-config
6397       (gnus-run-hooks 'gnus-exit-group-hook)
6398       (gnus-summary-update-info))
6399     (gnus-close-group group)
6400     ;; Make sure where we were, and go to next newsgroup.
6401     (set-buffer gnus-group-buffer)
6402     (unless quit-config
6403       (gnus-group-jump-to-group group))
6404     (gnus-run-hooks 'gnus-summary-exit-hook)
6405     (unless (or quit-config
6406                 ;; If this group has disappeared from the summary
6407                 ;; buffer, don't skip forwards.
6408                 (not (string= group (gnus-group-group-name))))
6409       (gnus-group-next-unread-group 1))
6410     (setq group-point (point))
6411     (if temporary
6412         nil                             ;Nothing to do.
6413       ;; If we have several article buffers, we kill them at exit.
6414       (unless gnus-single-article-buffer
6415         (gnus-kill-buffer gnus-article-buffer)
6416         (gnus-kill-buffer gnus-original-article-buffer)
6417         (setq gnus-article-current nil))
6418       (set-buffer buf)
6419       (if (not gnus-kill-summary-on-exit)
6420           (progn
6421             (gnus-deaden-summary)
6422             (setq mode nil))
6423         ;; We set all buffer-local variables to nil.  It is unclear why
6424         ;; this is needed, but if we don't, buffer-local variables are
6425         ;; not garbage-collected, it seems.  This would the lead to en
6426         ;; ever-growing Emacs.
6427         (gnus-summary-clear-local-variables)
6428         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6429           (gnus-summary-clear-local-variables))
6430         (when (get-buffer gnus-article-buffer)
6431           (bury-buffer gnus-article-buffer))
6432         ;; We clear the global counterparts of the buffer-local
6433         ;; variables as well, just to be on the safe side.
6434         (set-buffer gnus-group-buffer)
6435         (gnus-summary-clear-local-variables)
6436         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6437           (gnus-summary-clear-local-variables))
6438         ;; Return to group mode buffer.
6439         (when (eq mode 'gnus-summary-mode)
6440           (gnus-kill-buffer buf)))
6441       (setq gnus-current-select-method gnus-select-method)
6442       (pop-to-buffer gnus-group-buffer)
6443       (if (not quit-config)
6444           (progn
6445             (goto-char group-point)
6446             (gnus-configure-windows 'group 'force))
6447         (gnus-handle-ephemeral-exit quit-config))
6448       ;; Clear the current group name.
6449       (unless quit-config
6450         (setq gnus-newsgroup-name nil)))))
6451
6452 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6453 (defun gnus-summary-exit-no-update (&optional no-questions)
6454   "Quit reading current newsgroup without updating read article info."
6455   (interactive)
6456   (let* ((group gnus-newsgroup-name)
6457          (gnus-group-is-exiting-p t)
6458          (quit-config (gnus-group-quit-config group)))
6459     (when (or no-questions
6460               gnus-expert-user
6461               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6462       (gnus-async-halt-prefetch)
6463       (mapcar 'funcall
6464               (delq 'gnus-summary-expire-articles
6465                     (copy-sequence gnus-summary-prepare-exit-hook)))
6466       (when (gnus-buffer-live-p gnus-article-buffer)
6467         (save-excursion
6468           (set-buffer gnus-article-buffer)
6469           (mm-destroy-parts gnus-article-mime-handles)
6470           ;; Set it to nil for safety reason.
6471           (setq gnus-article-mime-handle-alist nil)
6472           (setq gnus-article-mime-handles nil)))
6473       ;; If we have several article buffers, we kill them at exit.
6474       (unless gnus-single-article-buffer
6475         (gnus-kill-buffer gnus-article-buffer)
6476         (gnus-kill-buffer gnus-original-article-buffer)
6477         (setq gnus-article-current nil))
6478       (if (not gnus-kill-summary-on-exit)
6479           (gnus-deaden-summary)
6480         (gnus-close-group group)
6481         (gnus-summary-clear-local-variables)
6482         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6483           (gnus-summary-clear-local-variables))
6484         (set-buffer gnus-group-buffer)
6485         (gnus-summary-clear-local-variables)
6486         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6487           (gnus-summary-clear-local-variables))
6488         (when (get-buffer gnus-summary-buffer)
6489           (kill-buffer gnus-summary-buffer)))
6490       (unless gnus-single-article-buffer
6491         (setq gnus-article-current nil))
6492       (when gnus-use-trees
6493         (gnus-tree-close group))
6494       (gnus-async-prefetch-remove-group group)
6495       (when (get-buffer gnus-article-buffer)
6496         (bury-buffer gnus-article-buffer))
6497       ;; Return to the group buffer.
6498       (gnus-configure-windows 'group 'force)
6499       ;; Clear the current group name.
6500       (setq gnus-newsgroup-name nil)
6501       (unless (gnus-ephemeral-group-p group)
6502         (gnus-group-update-group group))
6503       (when (equal (gnus-group-group-name) group)
6504         (gnus-group-next-unread-group 1))
6505       (when quit-config
6506         (gnus-handle-ephemeral-exit quit-config)))))
6507
6508 (defun gnus-handle-ephemeral-exit (quit-config)
6509   "Handle movement when leaving an ephemeral group.
6510 The state which existed when entering the ephemeral is reset."
6511   (if (not (buffer-name (car quit-config)))
6512       (gnus-configure-windows 'group 'force)
6513     (set-buffer (car quit-config))
6514     (cond ((eq major-mode 'gnus-summary-mode)
6515            (gnus-set-global-variables))
6516           ((eq major-mode 'gnus-article-mode)
6517            (save-excursion
6518              ;; The `gnus-summary-buffer' variable may point
6519              ;; to the old summary buffer when using a single
6520              ;; article buffer.
6521              (unless (gnus-buffer-live-p gnus-summary-buffer)
6522                (set-buffer gnus-group-buffer))
6523              (set-buffer gnus-summary-buffer)
6524              (gnus-set-global-variables))))
6525     (if (or (eq (cdr quit-config) 'article)
6526             (eq (cdr quit-config) 'pick))
6527         (progn
6528           ;; The current article may be from the ephemeral group
6529           ;; thus it is best that we reload this article
6530           (gnus-summary-show-article)
6531           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6532               (gnus-configure-windows 'pick 'force)
6533             (gnus-configure-windows (cdr quit-config) 'force)))
6534       (gnus-configure-windows (cdr quit-config) 'force))
6535     (when (eq major-mode 'gnus-summary-mode)
6536       (gnus-summary-next-subject 1 nil t)
6537       (gnus-summary-recenter)
6538       (gnus-summary-position-point))))
6539
6540 ;;; Dead summaries.
6541
6542 (defvar gnus-dead-summary-mode-map nil)
6543
6544 (unless gnus-dead-summary-mode-map
6545   (setq gnus-dead-summary-mode-map (make-keymap))
6546   (suppress-keymap gnus-dead-summary-mode-map)
6547   (substitute-key-definition
6548    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6549   (dolist (key '("\C-d" "\r" "\177" [delete]))
6550     (define-key gnus-dead-summary-mode-map
6551       key 'gnus-summary-wake-up-the-dead))
6552   (dolist (key '("q" "Q"))
6553     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6554
6555 (defvar gnus-dead-summary-mode nil
6556   "Minor mode for Gnus summary buffers.")
6557
6558 (defun gnus-dead-summary-mode (&optional arg)
6559   "Minor mode for Gnus summary buffers."
6560   (interactive "P")
6561   (when (eq major-mode 'gnus-summary-mode)
6562     (make-local-variable 'gnus-dead-summary-mode)
6563     (setq gnus-dead-summary-mode
6564           (if (null arg) (not gnus-dead-summary-mode)
6565             (> (prefix-numeric-value arg) 0)))
6566     (when gnus-dead-summary-mode
6567       (gnus-add-minor-mode
6568        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6569
6570 (defun gnus-deaden-summary ()
6571   "Make the current summary buffer into a dead summary buffer."
6572   ;; Kill any previous dead summary buffer.
6573   (when (and gnus-dead-summary
6574              (buffer-name gnus-dead-summary))
6575     (save-excursion
6576       (set-buffer gnus-dead-summary)
6577       (when gnus-dead-summary-mode
6578         (kill-buffer (current-buffer)))))
6579   ;; Make this the current dead summary.
6580   (setq gnus-dead-summary (current-buffer))
6581   (gnus-dead-summary-mode 1)
6582   (let ((name (buffer-name)))
6583     (when (string-match "Summary" name)
6584       (rename-buffer
6585        (concat (substring name 0 (match-beginning 0)) "Dead "
6586                (substring name (match-beginning 0)))
6587        t)
6588       (bury-buffer))))
6589
6590 (defun gnus-kill-or-deaden-summary (buffer)
6591   "Kill or deaden the summary BUFFER."
6592   (save-excursion
6593     (when (and (buffer-name buffer)
6594                (not gnus-single-article-buffer))
6595       (save-excursion
6596         (set-buffer buffer)
6597         (gnus-kill-buffer gnus-article-buffer)
6598         (gnus-kill-buffer gnus-original-article-buffer)))
6599     (cond
6600      ;; Kill the buffer.
6601      (gnus-kill-summary-on-exit
6602       (when (and gnus-use-trees
6603                  (gnus-buffer-exists-p buffer))
6604         (save-excursion
6605           (set-buffer buffer)
6606           (gnus-tree-close gnus-newsgroup-name)))
6607       (gnus-kill-buffer buffer))
6608      ;; Deaden the buffer.
6609      ((gnus-buffer-exists-p buffer)
6610       (save-excursion
6611         (set-buffer buffer)
6612         (gnus-deaden-summary))))))
6613
6614 (defun gnus-summary-wake-up-the-dead (&rest args)
6615   "Wake up the dead summary buffer."
6616   (interactive)
6617   (gnus-dead-summary-mode -1)
6618   (let ((name (buffer-name)))
6619     (when (string-match "Dead " name)
6620       (rename-buffer
6621        (concat (substring name 0 (match-beginning 0))
6622                (substring name (match-end 0)))
6623        t)))
6624   (gnus-message 3 "This dead summary is now alive again"))
6625
6626 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6627 (defun gnus-summary-fetch-faq (&optional faq-dir)
6628   "Fetch the FAQ for the current group.
6629 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6630 in."
6631   (interactive
6632    (list
6633     (when current-prefix-arg
6634       (completing-read
6635        "FAQ dir: " (and (listp gnus-group-faq-directory)
6636                         (mapcar (lambda (file) (list file))
6637                                 gnus-group-faq-directory))))))
6638   (let (gnus-faq-buffer)
6639     (when (setq gnus-faq-buffer
6640                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6641       (gnus-configure-windows 'summary-faq))))
6642
6643 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6644 (defun gnus-summary-describe-group (&optional force)
6645   "Describe the current newsgroup."
6646   (interactive "P")
6647   (gnus-group-describe-group force gnus-newsgroup-name))
6648
6649 (defun gnus-summary-describe-briefly ()
6650   "Describe summary mode commands briefly."
6651   (interactive)
6652   (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")))
6653
6654 ;; Walking around group mode buffer from summary mode.
6655
6656 (defun gnus-summary-next-group (&optional no-article target-group backward)
6657   "Exit current newsgroup and then select next unread newsgroup.
6658 If prefix argument NO-ARTICLE is non-nil, no article is selected
6659 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6660 previous group instead."
6661   (interactive "P")
6662   ;; Stop pre-fetching.
6663   (gnus-async-halt-prefetch)
6664   (let ((current-group gnus-newsgroup-name)
6665         (current-buffer (current-buffer))
6666         entered)
6667     (while (not entered)
6668       ;; Then we find what group we are supposed to enter.
6669       (set-buffer gnus-group-buffer)
6670       (gnus-group-jump-to-group current-group)
6671       (setq target-group
6672             (or target-group
6673                 (if (eq gnus-keep-same-level 'best)
6674                     (gnus-summary-best-group gnus-newsgroup-name)
6675                   (gnus-summary-search-group backward gnus-keep-same-level))))
6676       (if (not target-group)
6677           ;; There are no further groups, so we return to the group
6678           ;; buffer.
6679           (progn
6680             (gnus-message 5 "Returning to the group buffer")
6681             (setq entered t)
6682             (when (gnus-buffer-live-p current-buffer)
6683               (set-buffer current-buffer)
6684               (gnus-summary-exit))
6685             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6686         ;; We try to enter the target group.
6687         (gnus-group-jump-to-group target-group)
6688         (let ((unreads (gnus-group-group-unread)))
6689           (if (and (or (eq t unreads)
6690                        (and unreads (not (zerop unreads))))
6691                    (progn
6692                      ;; Now we semi-exit this group to update Xrefs
6693                      ;; and all variables.  We can't do a real exit,
6694                      ;; because the window conf must remain the same
6695                      ;; in case the user is prompted for info, and we
6696                      ;; don't want the window conf to change before
6697                      ;; that...
6698                      (when (gnus-buffer-live-p current-buffer)
6699                        (set-buffer current-buffer)
6700                        (gnus-summary-exit t))
6701                      (gnus-summary-read-group
6702                       target-group nil no-article
6703                       (and (buffer-name current-buffer) current-buffer)
6704                       nil backward)))
6705               (setq entered t)
6706             (setq current-group target-group
6707                   target-group nil)))))))
6708
6709 (defun gnus-summary-prev-group (&optional no-article)
6710   "Exit current newsgroup and then select previous unread newsgroup.
6711 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6712   (interactive "P")
6713   (gnus-summary-next-group no-article nil t))
6714
6715 ;; Walking around summary lines.
6716
6717 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6718   "Go to the first subject satisfying any non-nil constraint.
6719 If UNREAD is non-nil, the article should be unread.
6720 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6721 If UNSEED is non-nil, the article should be unseen.
6722 Returns the article selected or nil if there are no matching articles."
6723   (interactive "P")
6724   (cond
6725    ;; Empty summary.
6726    ((null gnus-newsgroup-data)
6727     (gnus-message 3 "No articles in the group")
6728     nil)
6729    ;; Pick the first article.
6730    ((not (or unread undownloaded unseen))
6731     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6732     (gnus-data-number (car gnus-newsgroup-data)))
6733    ;; Find the first unread article.
6734    (t
6735     (let ((data gnus-newsgroup-data))
6736       (while (and data
6737                   (let ((num (gnus-data-number (car data))))
6738                     (or (memq num gnus-newsgroup-unfetched)
6739                         (not (or (and unread
6740                                       (memq num gnus-newsgroup-unreads))
6741                                  (and undownloaded
6742                                       (memq num gnus-newsgroup-undownloaded))
6743                                  (and unseen
6744                                       (memq num gnus-newsgroup-unseen)))))))
6745         (setq data (cdr data)))
6746       (prog1 
6747           (if data
6748               (progn
6749                 (goto-char (gnus-data-pos (car data)))
6750                 (gnus-data-number (car data)))
6751             (gnus-message 3 "No more%s articles"
6752                           (let* ((r (when unread " unread"))
6753                                  (d (when undownloaded " undownloaded"))
6754                                  (s (when unseen " unseen"))
6755                                  (l (delq nil (list r d s))))
6756                             (cond ((= 3 (length l))
6757                                    (concat r "," d ", or" s))
6758                                   ((= 2 (length l))
6759                                    (concat (car l) ", or" (cadr l)))
6760                                   ((= 1 (length l))
6761                                    (car l))
6762                                   (t
6763                                    ""))))
6764             nil
6765             )
6766         (gnus-summary-position-point))))))
6767
6768 (defun gnus-summary-next-subject (n &optional unread dont-display)
6769   "Go to next N'th summary line.
6770 If N is negative, go to the previous N'th subject line.
6771 If UNREAD is non-nil, only unread articles are selected.
6772 The difference between N and the actual number of steps taken is
6773 returned."
6774   (interactive "p")
6775   (let ((backward (< n 0))
6776         (n (abs n)))
6777     (while (and (> n 0)
6778                 (if backward
6779                     (gnus-summary-find-prev unread)
6780                   (gnus-summary-find-next unread)))
6781       (unless (zerop (setq n (1- n)))
6782         (gnus-summary-show-thread)))
6783     (when (/= 0 n)
6784       (gnus-message 7 "No more%s articles"
6785                     (if unread " unread" "")))
6786     (unless dont-display
6787       (gnus-summary-recenter)
6788       (gnus-summary-position-point))
6789     n))
6790
6791 (defun gnus-summary-next-unread-subject (n)
6792   "Go to next N'th unread summary line."
6793   (interactive "p")
6794   (gnus-summary-next-subject n t))
6795
6796 (defun gnus-summary-prev-subject (n &optional unread)
6797   "Go to previous N'th summary line.
6798 If optional argument UNREAD is non-nil, only unread article is selected."
6799   (interactive "p")
6800   (gnus-summary-next-subject (- n) unread))
6801
6802 (defun gnus-summary-prev-unread-subject (n)
6803   "Go to previous N'th unread summary line."
6804   (interactive "p")
6805   (gnus-summary-next-subject (- n) t))
6806
6807 (defun gnus-summary-goto-subjects (articles)
6808   "Insert the subject header for ARTICLES in the current buffer."
6809   (save-excursion
6810     (dolist (article articles)
6811       (gnus-summary-goto-subject article t)))
6812   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6813   (gnus-summary-position-point))
6814  
6815 (defun gnus-summary-goto-subject (article &optional force silent)
6816   "Go the subject line of ARTICLE.
6817 If FORCE, also allow jumping to articles not currently shown."
6818   (interactive "nArticle number: ")
6819   (unless (numberp article)
6820     (error "Article %s is not a number" article))
6821   (let ((b (point))
6822         (data (gnus-data-find article)))
6823     ;; We read in the article if we have to.
6824     (and (not data)
6825          force
6826          (gnus-summary-insert-subject
6827           article
6828           (if (or (numberp force) (vectorp force)) force)
6829           t)
6830          (setq data (gnus-data-find article)))
6831     (goto-char b)
6832     (if (not data)
6833         (progn
6834           (unless silent
6835             (gnus-message 3 "Can't find article %d" article))
6836           nil)
6837       (let ((pt (gnus-data-pos data)))
6838         (goto-char pt)
6839         (gnus-summary-set-article-display-arrow pt))
6840       (gnus-summary-position-point)
6841       article)))
6842
6843 ;; Walking around summary lines with displaying articles.
6844
6845 (defun gnus-summary-expand-window (&optional arg)
6846   "Make the summary buffer take up the entire Emacs frame.
6847 Given a prefix, will force an `article' buffer configuration."
6848   (interactive "P")
6849   (if arg
6850       (gnus-configure-windows 'article 'force)
6851     (gnus-configure-windows 'summary 'force)))
6852
6853 (defun gnus-summary-display-article (article &optional all-header)
6854   "Display ARTICLE in article buffer."
6855   (when (gnus-buffer-live-p gnus-article-buffer)
6856     (with-current-buffer gnus-article-buffer
6857       (mm-enable-multibyte)))
6858   (gnus-set-global-variables)
6859   (when (gnus-buffer-live-p gnus-article-buffer)
6860     (with-current-buffer gnus-article-buffer
6861       (setq gnus-article-charset gnus-newsgroup-charset)
6862       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6863       (mm-enable-multibyte)))
6864   (if (null article)
6865       nil
6866     (prog1
6867         (if gnus-summary-display-article-function
6868             (funcall gnus-summary-display-article-function article all-header)
6869           (gnus-article-prepare article all-header))
6870       (gnus-run-hooks 'gnus-select-article-hook)
6871       (when (and gnus-current-article
6872                  (not (zerop gnus-current-article)))
6873         (gnus-summary-goto-subject gnus-current-article))
6874       (gnus-summary-recenter)
6875       (when (and gnus-use-trees gnus-show-threads)
6876         (gnus-possibly-generate-tree article)
6877         (gnus-highlight-selected-tree article))
6878       ;; Successfully display article.
6879       (gnus-article-set-window-start
6880        (cdr (assq article gnus-newsgroup-bookmarks))))))
6881
6882 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6883   "Select the current article.
6884 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6885 non-nil, the article will be re-fetched even if it already present in
6886 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6887 be displayed."
6888   ;; Make sure we are in the summary buffer to work around bbdb bug.
6889   (unless (eq major-mode 'gnus-summary-mode)
6890     (set-buffer gnus-summary-buffer))
6891   (let ((article (or article (gnus-summary-article-number)))
6892         (all-headers (not (not all-headers))) ;Must be t or nil.
6893         gnus-summary-display-article-function)
6894     (and (not pseudo)
6895          (gnus-summary-article-pseudo-p article)
6896          (error "This is a pseudo-article"))
6897     (save-excursion
6898       (set-buffer gnus-summary-buffer)
6899       (if (or (and gnus-single-article-buffer
6900                    (or (null gnus-current-article)
6901                        (null gnus-article-current)
6902                        (null (get-buffer gnus-article-buffer))
6903                        (not (eq article (cdr gnus-article-current)))
6904                        (not (equal (car gnus-article-current)
6905                                    gnus-newsgroup-name))))
6906               (and (not gnus-single-article-buffer)
6907                    (or (null gnus-current-article)
6908                        (not (eq gnus-current-article article))))
6909               force)
6910           ;; The requested article is different from the current article.
6911           (progn
6912             (gnus-summary-display-article article all-headers)
6913             (when (gnus-buffer-live-p gnus-article-buffer)
6914               (with-current-buffer gnus-article-buffer
6915                 (if (not gnus-article-decoded-p) ;; a local variable
6916                     (mm-disable-multibyte))))
6917             (gnus-article-set-window-start
6918              (cdr (assq article gnus-newsgroup-bookmarks)))
6919             article)
6920         'old))))
6921
6922 (defun gnus-summary-force-verify-and-decrypt ()
6923   "Display buttons for signed/encrypted parts and verify/decrypt them."
6924   (interactive)
6925   (let ((mm-verify-option 'known)
6926         (mm-decrypt-option 'known)
6927         (gnus-buttonized-mime-types (append (list "multipart/signed"
6928                                                   "multipart/encrypted")
6929                                             gnus-buttonized-mime-types)))
6930     (gnus-summary-select-article nil 'force)))
6931
6932 (defun gnus-summary-set-current-mark (&optional current-mark)
6933   "Obsolete function."
6934   nil)
6935
6936 (defun gnus-summary-next-article (&optional unread subject backward push)
6937   "Select the next article.
6938 If UNREAD, only unread articles are selected.
6939 If SUBJECT, only articles with SUBJECT are selected.
6940 If BACKWARD, the previous article is selected instead of the next."
6941   (interactive "P")
6942   (cond
6943    ;; Is there such an article?
6944    ((and (gnus-summary-search-forward unread subject backward)
6945          (or (gnus-summary-display-article (gnus-summary-article-number))
6946              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6947     (gnus-summary-position-point))
6948    ;; If not, we try the first unread, if that is wanted.
6949    ((and subject
6950          gnus-auto-select-same
6951          (gnus-summary-first-unread-article))
6952     (gnus-summary-position-point)
6953     (gnus-message 6 "Wrapped"))
6954    ;; Try to get next/previous article not displayed in this group.
6955    ((and gnus-auto-extend-newsgroup
6956          (not unread) (not subject))
6957     (gnus-summary-goto-article
6958      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6959      nil (count-lines (point-min) (point))))
6960    ;; Go to next/previous group.
6961    (t
6962     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6963       (gnus-summary-jump-to-group gnus-newsgroup-name))
6964     (let ((cmd last-command-char)
6965           (point
6966            (save-excursion
6967              (set-buffer gnus-group-buffer)
6968              (point)))
6969           (group
6970            (if (eq gnus-keep-same-level 'best)
6971                (gnus-summary-best-group gnus-newsgroup-name)
6972              (gnus-summary-search-group backward gnus-keep-same-level))))
6973       ;; For some reason, the group window gets selected.  We change
6974       ;; it back.
6975       (select-window (get-buffer-window (current-buffer)))
6976       ;; Select next unread newsgroup automagically.
6977       (cond
6978        ((or (not gnus-auto-select-next)
6979             (not cmd))
6980         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6981        ((or (eq gnus-auto-select-next 'quietly)
6982             (and (eq gnus-auto-select-next 'slightly-quietly)
6983                  push)
6984             (and (eq gnus-auto-select-next 'almost-quietly)
6985                  (gnus-summary-last-article-p)))
6986         ;; Select quietly.
6987         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6988             (gnus-summary-exit)
6989           (gnus-message 7 "No more%s articles (%s)..."
6990                         (if unread " unread" "")
6991                         (if group (concat "selecting " group)
6992                           "exiting"))
6993           (gnus-summary-next-group nil group backward)))
6994        (t
6995         (when (gnus-key-press-event-p last-input-event)
6996           (gnus-summary-walk-group-buffer
6997            gnus-newsgroup-name cmd unread backward point))))))))
6998
6999 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7000   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7001                       (?\C-p (gnus-group-prev-unread-group 1))))
7002         (cursor-in-echo-area t)
7003         keve key group ended prompt)
7004     (save-excursion
7005       (set-buffer gnus-group-buffer)
7006       (goto-char start)
7007       (setq group
7008             (if (eq gnus-keep-same-level 'best)
7009                 (gnus-summary-best-group gnus-newsgroup-name)
7010               (gnus-summary-search-group backward gnus-keep-same-level))))
7011     (while (not ended)
7012       (setq prompt
7013             (format
7014              "No more%s articles%s " (if unread " unread" "")
7015              (if (and group
7016                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7017                  (format " (Type %s for %s [%s])"
7018                          (single-key-description cmd) group
7019                          (car (gnus-gethash group gnus-newsrc-hashtb)))
7020                (format " (Type %s to exit %s)"
7021                        (single-key-description cmd)
7022                        gnus-newsgroup-name))))
7023       ;; Confirm auto selection.
7024       (setq key (car (setq keve (gnus-read-event-char prompt)))
7025             ended t)
7026       (cond
7027        ((assq key keystrokes)
7028         (let ((obuf (current-buffer)))
7029           (switch-to-buffer gnus-group-buffer)
7030           (when group
7031             (gnus-group-jump-to-group group))
7032           (eval (cadr (assq key keystrokes)))
7033           (setq group (gnus-group-group-name))
7034           (switch-to-buffer obuf))
7035         (setq ended nil))
7036        ((equal key cmd)
7037         (if (or (not group)
7038                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7039             (gnus-summary-exit)
7040           (gnus-summary-next-group nil group backward)))
7041        (t
7042         (push (cdr keve) unread-command-events))))))
7043
7044 (defun gnus-summary-next-unread-article ()
7045   "Select unread article after current one."
7046   (interactive)
7047   (gnus-summary-next-article
7048    (or (not (eq gnus-summary-goto-unread 'never))
7049        (gnus-summary-last-article-p (gnus-summary-article-number)))
7050    (and gnus-auto-select-same
7051         (gnus-summary-article-subject))))
7052
7053 (defun gnus-summary-prev-article (&optional unread subject)
7054   "Select the article after the current one.
7055 If UNREAD is non-nil, only unread articles are selected."
7056   (interactive "P")
7057   (gnus-summary-next-article unread subject t))
7058
7059 (defun gnus-summary-prev-unread-article ()
7060   "Select unread article before current one."
7061   (interactive)
7062   (gnus-summary-prev-article
7063    (or (not (eq gnus-summary-goto-unread 'never))
7064        (gnus-summary-first-article-p (gnus-summary-article-number)))
7065    (and gnus-auto-select-same
7066         (gnus-summary-article-subject))))
7067
7068 (defun gnus-summary-next-page (&optional lines circular stop)
7069   "Show next page of the selected article.
7070 If at the end of the current article, select the next article.
7071 LINES says how many lines should be scrolled up.
7072
7073 If CIRCULAR is non-nil, go to the start of the article instead of
7074 selecting the next article when reaching the end of the current
7075 article.
7076
7077 If STOP is non-nil, just stop when reaching the end of the message."
7078   (interactive "P")
7079   (setq gnus-summary-buffer (current-buffer))
7080   (gnus-set-global-variables)
7081   (let ((article (gnus-summary-article-number))
7082         (article-window (get-buffer-window gnus-article-buffer t))
7083         endp)
7084     ;; If the buffer is empty, we have no article.
7085     (unless article
7086       (error "No article to select"))
7087     (gnus-configure-windows 'article)
7088     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7089         (if (and (eq gnus-summary-goto-unread 'never)
7090                  (not (gnus-summary-last-article-p article)))
7091             (gnus-summary-next-article)
7092           (gnus-summary-next-unread-article))
7093       (if (or (null gnus-current-article)
7094               (null gnus-article-current)
7095               (/= article (cdr gnus-article-current))
7096               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7097           ;; Selected subject is different from current article's.
7098           (gnus-summary-display-article article)
7099         (when article-window
7100           (gnus-eval-in-buffer-window gnus-article-buffer
7101             (setq endp (or (gnus-article-next-page lines)
7102                            (gnus-article-only-boring-p))))
7103           (when endp
7104             (cond (stop
7105                    (gnus-message 3 "End of message"))
7106                   (circular
7107                    (gnus-summary-beginning-of-article))
7108                   (lines
7109                    (gnus-message 3 "End of message"))
7110                   ((null lines)
7111                    (if (and (eq gnus-summary-goto-unread 'never)
7112                             (not (gnus-summary-last-article-p article)))
7113                        (gnus-summary-next-article)
7114                      (gnus-summary-next-unread-article))))))))
7115     (gnus-summary-recenter)
7116     (gnus-summary-position-point)))
7117
7118 (defun gnus-summary-prev-page (&optional lines move)
7119   "Show previous page of selected article.
7120 Argument LINES specifies lines to be scrolled down.
7121 If MOVE, move to the previous unread article if point is at
7122 the beginning of the buffer."
7123   (interactive "P")
7124   (let ((article (gnus-summary-article-number))
7125         (article-window (get-buffer-window gnus-article-buffer t))
7126         endp)
7127     (gnus-configure-windows 'article)
7128     (if (or (null gnus-current-article)
7129             (null gnus-article-current)
7130             (/= article (cdr gnus-article-current))
7131             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7132         ;; Selected subject is different from current article's.
7133         (gnus-summary-display-article article)
7134       (gnus-summary-recenter)
7135       (when article-window
7136         (gnus-eval-in-buffer-window gnus-article-buffer
7137           (setq endp (gnus-article-prev-page lines)))
7138         (when (and move endp)
7139           (cond (lines
7140                  (gnus-message 3 "Beginning of message"))
7141                 ((null lines)
7142                  (if (and (eq gnus-summary-goto-unread 'never)
7143                           (not (gnus-summary-first-article-p article)))
7144                      (gnus-summary-prev-article)
7145                    (gnus-summary-prev-unread-article))))))))
7146   (gnus-summary-position-point))
7147
7148 (defun gnus-summary-prev-page-or-article (&optional lines)
7149   "Show previous page of selected article.
7150 Argument LINES specifies lines to be scrolled down.
7151 If at the beginning of the article, go to the next article."
7152   (interactive "P")
7153   (gnus-summary-prev-page lines t))
7154
7155 (defun gnus-summary-scroll-up (lines)
7156   "Scroll up (or down) one line current article.
7157 Argument LINES specifies lines to be scrolled up (or down if negative)."
7158   (interactive "p")
7159   (gnus-configure-windows 'article)
7160   (gnus-summary-show-thread)
7161   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7162     (gnus-eval-in-buffer-window gnus-article-buffer
7163       (cond ((> lines 0)
7164              (when (gnus-article-next-page lines)
7165                (gnus-message 3 "End of message")))
7166             ((< lines 0)
7167              (gnus-article-prev-page (- lines))))))
7168   (gnus-summary-recenter)
7169   (gnus-summary-position-point))
7170
7171 (defun gnus-summary-scroll-down (lines)
7172   "Scroll down (or up) one line current article.
7173 Argument LINES specifies lines to be scrolled down (or up if negative)."
7174   (interactive "p")
7175   (gnus-summary-scroll-up (- lines)))
7176
7177 (defun gnus-summary-next-same-subject ()
7178   "Select next article which has the same subject as current one."
7179   (interactive)
7180   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7181
7182 (defun gnus-summary-prev-same-subject ()
7183   "Select previous article which has the same subject as current one."
7184   (interactive)
7185   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7186
7187 (defun gnus-summary-next-unread-same-subject ()
7188   "Select next unread article which has the same subject as current one."
7189   (interactive)
7190   (gnus-summary-next-article t (gnus-summary-article-subject)))
7191
7192 (defun gnus-summary-prev-unread-same-subject ()
7193   "Select previous unread article which has the same subject as current one."
7194   (interactive)
7195   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7196
7197 (defun gnus-summary-first-unread-article ()
7198   "Select the first unread article.
7199 Return nil if there are no unread articles."
7200   (interactive)
7201   (prog1
7202       (when (gnus-summary-first-subject t)
7203         (gnus-summary-show-thread)
7204         (gnus-summary-first-subject t)
7205         (gnus-summary-display-article (gnus-summary-article-number)))
7206     (gnus-summary-position-point)))
7207
7208 (defun gnus-summary-first-unread-subject ()
7209   "Place the point on the subject line of the first unread article.
7210 Return nil if there are no unread articles."
7211   (interactive)
7212   (prog1
7213       (when (gnus-summary-first-subject t)
7214         (gnus-summary-show-thread)
7215         (gnus-summary-first-subject t))
7216     (gnus-summary-position-point)))
7217
7218 (defun gnus-summary-first-unseen-subject ()
7219   "Place the point on the subject line of the first unseen article.
7220 Return nil if there are no unseen articles."
7221   (interactive)
7222   (prog1
7223       (when (gnus-summary-first-subject nil nil t)
7224         (gnus-summary-show-thread)
7225         (gnus-summary-first-subject nil nil t))
7226     (gnus-summary-position-point)))
7227
7228 (defun gnus-summary-first-unseen-or-unread-subject ()
7229   "Place the point on the subject line of the first unseen article or,
7230 if all article have been seen, on the subject line of the first unread
7231 article."
7232   (interactive)
7233   (prog1
7234       (unless (when (gnus-summary-first-subject nil nil t)
7235                 (gnus-summary-show-thread)
7236                 (gnus-summary-first-subject nil nil t))
7237         (when (gnus-summary-first-subject t)
7238           (gnus-summary-show-thread)
7239           (gnus-summary-first-subject t)))
7240     (gnus-summary-position-point)))
7241
7242 (defun gnus-summary-first-article ()
7243   "Select the first article.
7244 Return nil if there are no articles."
7245   (interactive)
7246   (prog1
7247       (when (gnus-summary-first-subject)
7248         (gnus-summary-show-thread)
7249         (gnus-summary-first-subject)
7250         (gnus-summary-display-article (gnus-summary-article-number)))
7251     (gnus-summary-position-point)))
7252
7253 (defun gnus-summary-best-unread-article (&optional arg)
7254   "Select the unread article with the highest score.
7255 If given a prefix argument, select the next unread article that has a
7256 score higher than the default score."
7257   (interactive "P")
7258   (let ((article (if arg
7259                      (gnus-summary-better-unread-subject)
7260                    (gnus-summary-best-unread-subject))))
7261     (if article
7262         (gnus-summary-goto-article article)
7263       (error "No unread articles"))))
7264
7265 (defun gnus-summary-best-unread-subject ()
7266   "Select the unread subject with the highest score."
7267   (interactive)
7268   (let ((best -1000000)
7269         (data gnus-newsgroup-data)
7270         article score)
7271     (while data
7272       (and (gnus-data-unread-p (car data))
7273            (> (setq score
7274                     (gnus-summary-article-score (gnus-data-number (car data))))
7275               best)
7276            (setq best score
7277                  article (gnus-data-number (car data))))
7278       (setq data (cdr data)))
7279     (when article
7280       (gnus-summary-goto-subject article))
7281     (gnus-summary-position-point)
7282     article))
7283
7284 (defun gnus-summary-better-unread-subject ()
7285   "Select the first unread subject that has a score over the default score."
7286   (interactive)
7287   (let ((data gnus-newsgroup-data)
7288         article score)
7289     (while (and (setq article (gnus-data-number (car data)))
7290                 (or (gnus-data-read-p (car data))
7291                     (not (> (gnus-summary-article-score article)
7292                             gnus-summary-default-score))))
7293       (setq data (cdr data)))
7294     (when article
7295       (gnus-summary-goto-subject article))
7296     (gnus-summary-position-point)
7297     article))
7298
7299 (defun gnus-summary-last-subject ()
7300   "Go to the last displayed subject line in the group."
7301   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7302     (when article
7303       (gnus-summary-goto-subject article))))
7304
7305 (defun gnus-summary-goto-article (article &optional all-headers force)
7306   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7307 If ALL-HEADERS is non-nil, no header lines are hidden.
7308 If FORCE, go to the article even if it isn't displayed.  If FORCE
7309 is a number, it is the line the article is to be displayed on."
7310   (interactive
7311    (list
7312     (completing-read
7313      "Article number or Message-ID: "
7314      (mapcar (lambda (number) (list (int-to-string number)))
7315              gnus-newsgroup-limit))
7316     current-prefix-arg
7317     t))
7318   (prog1
7319       (if (and (stringp article)
7320                (string-match "@" article))
7321           (gnus-summary-refer-article article)
7322         (when (stringp article)
7323           (setq article (string-to-number article)))
7324         (if (gnus-summary-goto-subject article force)
7325             (gnus-summary-display-article article all-headers)
7326           (gnus-message 4 "Couldn't go to article %s" article) nil))
7327     (gnus-summary-position-point)))
7328
7329 (defun gnus-summary-goto-last-article ()
7330   "Go to the previously read article."
7331   (interactive)
7332   (prog1
7333       (when gnus-last-article
7334         (gnus-summary-goto-article gnus-last-article nil t))
7335     (gnus-summary-position-point)))
7336
7337 (defun gnus-summary-pop-article (number)
7338   "Pop one article off the history and go to the previous.
7339 NUMBER articles will be popped off."
7340   (interactive "p")
7341   (let (to)
7342     (setq gnus-newsgroup-history
7343           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7344     (if to
7345         (gnus-summary-goto-article (car to) nil t)
7346       (error "Article history empty")))
7347   (gnus-summary-position-point))
7348
7349 ;; Summary commands and functions for limiting the summary buffer.
7350
7351 (defun gnus-summary-limit-to-articles (n)
7352   "Limit the summary buffer to the next N articles.
7353 If not given a prefix, use the process marked articles instead."
7354   (interactive "P")
7355   (prog1
7356       (let ((articles (gnus-summary-work-articles n)))
7357         (setq gnus-newsgroup-processable nil)
7358         (gnus-summary-limit articles))
7359     (gnus-summary-position-point)))
7360
7361 (defun gnus-summary-pop-limit (&optional total)
7362   "Restore the previous limit.
7363 If given a prefix, remove all limits."
7364   (interactive "P")
7365   (when total
7366     (setq gnus-newsgroup-limits
7367           (list (mapcar (lambda (h) (mail-header-number h))
7368                         gnus-newsgroup-headers))))
7369   (unless gnus-newsgroup-limits
7370     (error "No limit to pop"))
7371   (prog1
7372       (gnus-summary-limit nil 'pop)
7373     (gnus-summary-position-point)))
7374
7375 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7376   "Limit the summary buffer to articles that have subjects that match a regexp.
7377 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7378   (interactive
7379    (list (read-string (if current-prefix-arg
7380                           "Exclude subject (regexp): "
7381                         "Limit to subject (regexp): "))
7382          nil current-prefix-arg))
7383   (unless header
7384     (setq header "subject"))
7385   (when (not (equal "" subject))
7386     (prog1
7387         (let ((articles (gnus-summary-find-matching
7388                          (or header "subject") subject 'all nil nil
7389                          not-matching)))
7390           (unless articles
7391             (error "Found no matches for \"%s\"" subject))
7392           (gnus-summary-limit articles))
7393       (gnus-summary-position-point))))
7394
7395 (defun gnus-summary-limit-to-author (from &optional not-matching)
7396   "Limit the summary buffer to articles that have authors that match a regexp.
7397 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7398   (interactive
7399    (list (read-string (if current-prefix-arg
7400                           "Exclude author (regexp): "
7401                         "Limit to author (regexp): "))
7402          current-prefix-arg))
7403   (gnus-summary-limit-to-subject from "from" not-matching))
7404
7405 (defun gnus-summary-limit-to-age (age &optional younger-p)
7406   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7407 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7408 articles that are younger than AGE days."
7409   (interactive
7410    (let ((younger current-prefix-arg)
7411          (days-got nil)
7412          days)
7413      (while (not days-got)
7414        (setq days (if younger
7415                       (read-string "Limit to articles younger than (in days, older when negative): ")
7416                     (read-string
7417                      "Limit to articles older than (in days, younger when negative): ")))
7418        (when (> (length days) 0)
7419          (setq days (read days)))
7420        (if (numberp days)
7421            (progn
7422              (setq days-got t)
7423              (if (< days 0)
7424                  (progn
7425                    (setq younger (not younger))
7426                    (setq days (* days -1)))))
7427          (message "Please enter a number.")
7428          (sleep-for 1)))
7429      (list days younger)))
7430   (prog1
7431       (let ((data gnus-newsgroup-data)
7432             (cutoff (days-to-time age))
7433             articles d date is-younger)
7434         (while (setq d (pop data))
7435           (when (and (vectorp (gnus-data-header d))
7436                      (setq date (mail-header-date (gnus-data-header d))))
7437             (setq is-younger (time-less-p
7438                               (time-since (condition-case ()
7439                                               (date-to-time date)
7440                                             (error '(0 0))))
7441                               cutoff))
7442             (when (if younger-p
7443                       is-younger
7444                     (not is-younger))
7445               (push (gnus-data-number d) articles))))
7446         (gnus-summary-limit (nreverse articles)))
7447     (gnus-summary-position-point)))
7448
7449 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7450   "Limit the summary buffer to articles that match an 'extra' header."
7451   (interactive
7452    (let ((header
7453           (intern
7454            (gnus-completing-read-with-default
7455             (symbol-name (car gnus-extra-headers))
7456             (if current-prefix-arg
7457                 "Exclude extra header:"
7458               "Limit extra header:")
7459             (mapcar (lambda (x)
7460                       (cons (symbol-name x) x))
7461                     gnus-extra-headers)
7462             nil
7463             t))))
7464      (list header
7465            (read-string (format "%s header %s (regexp): "
7466                                 (if current-prefix-arg "Exclude" "Limit to")
7467                                 header))
7468            current-prefix-arg)))
7469   (when (not (equal "" regexp))
7470     (prog1
7471         (let ((articles (gnus-summary-find-matching
7472                          (cons 'extra header) regexp 'all nil nil
7473                          not-matching)))
7474           (unless articles
7475             (error "Found no matches for \"%s\"" regexp))
7476           (gnus-summary-limit articles))
7477       (gnus-summary-position-point))))
7478
7479 (defun gnus-summary-limit-to-display-predicate ()
7480   "Limit the summary buffer to the predicated in the `display' group parameter."
7481   (interactive)
7482   (unless gnus-newsgroup-display
7483     (error "There is no `display' group parameter"))
7484   (let (articles)
7485     (dolist (number gnus-newsgroup-articles)
7486       (when (funcall gnus-newsgroup-display)
7487         (push number articles)))
7488     (gnus-summary-limit articles))
7489   (gnus-summary-position-point))
7490
7491 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7492 (make-obsolete
7493  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7494
7495 (defun gnus-summary-limit-to-unread (&optional all)
7496   "Limit the summary buffer to articles that are not marked as read.
7497 If ALL is non-nil, limit strictly to unread articles."
7498   (interactive "P")
7499   (if all
7500       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7501     (gnus-summary-limit-to-marks
7502      ;; Concat all the marks that say that an article is read and have
7503      ;; those removed.
7504      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7505            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7506            gnus-low-score-mark gnus-expirable-mark
7507            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7508            gnus-duplicate-mark gnus-souped-mark)
7509      'reverse)))
7510
7511 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7512 (make-obsolete 'gnus-summary-delete-marked-with
7513                'gnus-summary-limit-exclude-marks)
7514
7515 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7516   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7517 If REVERSE, limit the summary buffer to articles that are marked
7518 with MARKS.  MARKS can either be a string of marks or a list of marks.
7519 Returns how many articles were removed."
7520   (interactive "sMarks: ")
7521   (gnus-summary-limit-to-marks marks t))
7522
7523 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7524   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7525 If REVERSE (the prefix), limit the summary buffer to articles that are
7526 not marked with MARKS.  MARKS can either be a string of marks or a
7527 list of marks.
7528 Returns how many articles were removed."
7529   (interactive "sMarks: \nP")
7530   (prog1
7531       (let ((data gnus-newsgroup-data)
7532             (marks (if (listp marks) marks
7533                      (append marks nil))) ; Transform to list.
7534             articles)
7535         (while data
7536           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7537                   (memq (gnus-data-mark (car data)) marks))
7538             (push (gnus-data-number (car data)) articles))
7539           (setq data (cdr data)))
7540         (gnus-summary-limit articles))
7541     (gnus-summary-position-point)))
7542
7543 (defun gnus-summary-limit-to-score (score)
7544   "Limit to articles with score at or above SCORE."
7545   (interactive "NLimit to articles with score of at least: ")
7546   (let ((data gnus-newsgroup-data)
7547         articles)
7548     (while data
7549       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7550                 score)
7551         (push (gnus-data-number (car data)) articles))
7552       (setq data (cdr data)))
7553     (prog1
7554         (gnus-summary-limit articles)
7555       (gnus-summary-position-point))))
7556
7557 (defun gnus-summary-limit-to-unseen ()
7558   "Limit to unseen articles."
7559   (interactive)
7560   (prog1
7561       (gnus-summary-limit gnus-newsgroup-unseen)
7562     (gnus-summary-position-point)))
7563
7564 (defun gnus-summary-limit-include-thread (id)
7565   "Display all the hidden articles that is in the thread with ID in it.
7566 When called interactively, ID is the Message-ID of the current
7567 article."
7568   (interactive (list (mail-header-id (gnus-summary-article-header))))
7569   (let ((articles (gnus-articles-in-thread
7570                    (gnus-id-to-thread (gnus-root-id id)))))
7571     (prog1
7572         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7573       (gnus-summary-limit-include-matching-articles
7574        "subject"
7575        (regexp-quote (gnus-simplify-subject-re
7576                       (mail-header-subject (gnus-id-to-header id)))))
7577       (gnus-summary-position-point))))
7578
7579 (defun gnus-summary-limit-include-matching-articles (header regexp)
7580   "Display all the hidden articles that have HEADERs that match REGEXP."
7581   (interactive (list (read-string "Match on header: ")
7582                      (read-string "Regexp: ")))
7583   (let ((articles (gnus-find-matching-articles header regexp)))
7584     (prog1
7585         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7586       (gnus-summary-position-point))))
7587
7588 (defun gnus-summary-insert-dormant-articles ()
7589   "Insert all the dormat articles for this group into the current buffer."
7590   (interactive)
7591   (let ((gnus-verbose (max 6 gnus-verbose)))
7592     (if (not gnus-newsgroup-dormant)
7593         (gnus-message 3 "No cached articles for this group")
7594       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7595
7596 (defun gnus-summary-limit-include-dormant ()
7597   "Display all the hidden articles that are marked as dormant.
7598 Note that this command only works on a subset of the articles currently
7599 fetched for this group."
7600   (interactive)
7601   (unless gnus-newsgroup-dormant
7602     (error "There are no dormant articles in this group"))
7603   (prog1
7604       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7605     (gnus-summary-position-point)))
7606
7607 (defun gnus-summary-limit-exclude-dormant ()
7608   "Hide all dormant articles."
7609   (interactive)
7610   (prog1
7611       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7612     (gnus-summary-position-point)))
7613
7614 (defun gnus-summary-limit-exclude-childless-dormant ()
7615   "Hide all dormant articles that have no children."
7616   (interactive)
7617   (let ((data (gnus-data-list t))
7618         articles d children)
7619     ;; Find all articles that are either not dormant or have
7620     ;; children.
7621     (while (setq d (pop data))
7622       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7623                 (and (setq children
7624                            (gnus-article-children (gnus-data-number d)))
7625                      (let (found)
7626                        (while children
7627                          (when (memq (car children) articles)
7628                            (setq children nil
7629                                  found t))
7630                          (pop children))
7631                        found)))
7632         (push (gnus-data-number d) articles)))
7633     ;; Do the limiting.
7634     (prog1
7635         (gnus-summary-limit articles)
7636       (gnus-summary-position-point))))
7637
7638 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7639   "Mark all unread excluded articles as read.
7640 If ALL, mark even excluded ticked and dormants as read."
7641   (interactive "P")
7642   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7643   (let ((articles (gnus-sorted-ndifference
7644                    (sort
7645                     (mapcar (lambda (h) (mail-header-number h))
7646                             gnus-newsgroup-headers)
7647                     '<)
7648                    gnus-newsgroup-limit))
7649         article)
7650     (setq gnus-newsgroup-unreads
7651           (gnus-sorted-intersection gnus-newsgroup-unreads
7652                                     gnus-newsgroup-limit))
7653     (if all
7654         (setq gnus-newsgroup-dormant nil
7655               gnus-newsgroup-marked nil
7656               gnus-newsgroup-reads
7657               (nconc
7658                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7659                gnus-newsgroup-reads))
7660       (while (setq article (pop articles))
7661         (unless (or (memq article gnus-newsgroup-dormant)
7662                     (memq article gnus-newsgroup-marked))
7663           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7664
7665 (defun gnus-summary-limit (articles &optional pop)
7666   (if pop
7667       ;; We pop the previous limit off the stack and use that.
7668       (setq articles (car gnus-newsgroup-limits)
7669             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7670     ;; We use the new limit, so we push the old limit on the stack.
7671     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7672   ;; Set the limit.
7673   (setq gnus-newsgroup-limit articles)
7674   (let ((total (length gnus-newsgroup-data))
7675         (data (gnus-data-find-list (gnus-summary-article-number)))
7676         (gnus-summary-mark-below nil)   ; Inhibit this.
7677         found)
7678     ;; This will do all the work of generating the new summary buffer
7679     ;; according to the new limit.
7680     (gnus-summary-prepare)
7681     ;; Hide any threads, possibly.
7682     (gnus-summary-maybe-hide-threads)
7683     ;; Try to return to the article you were at, or one in the
7684     ;; neighborhood.
7685     (when data
7686       ;; We try to find some article after the current one.
7687       (while data
7688         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7689           (setq data nil
7690                 found t))
7691         (setq data (cdr data))))
7692     (unless found
7693       ;; If there is no data, that means that we were after the last
7694       ;; article.  The same goes when we can't find any articles
7695       ;; after the current one.
7696       (goto-char (point-max))
7697       (gnus-summary-find-prev))
7698     (gnus-set-mode-line 'summary)
7699     ;; We return how many articles were removed from the summary
7700     ;; buffer as a result of the new limit.
7701     (- total (length gnus-newsgroup-data))))
7702
7703 (defsubst gnus-invisible-cut-children (threads)
7704   (let ((num 0))
7705     (while threads
7706       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7707         (incf num))
7708       (pop threads))
7709     (< num 2)))
7710
7711 (defsubst gnus-cut-thread (thread)
7712   "Go forwards in the thread until we find an article that we want to display."
7713   (when (or (eq gnus-fetch-old-headers 'some)
7714             (eq gnus-fetch-old-headers 'invisible)
7715             (numberp gnus-fetch-old-headers)
7716             (eq gnus-build-sparse-threads 'some)
7717             (eq gnus-build-sparse-threads 'more))
7718     ;; Deal with old-fetched headers and sparse threads.
7719     (while (and
7720             thread
7721             (or
7722              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7723              (gnus-summary-article-ancient-p
7724               (mail-header-number (car thread))))
7725             (if (or (<= (length (cdr thread)) 1)
7726                     (eq gnus-fetch-old-headers 'invisible))
7727                 (setq gnus-newsgroup-limit
7728                       (delq (mail-header-number (car thread))
7729                             gnus-newsgroup-limit)
7730                       thread (cadr thread))
7731               (when (gnus-invisible-cut-children (cdr thread))
7732                 (let ((th (cdr thread)))
7733                   (while th
7734                     (if (memq (mail-header-number (caar th))
7735                               gnus-newsgroup-limit)
7736                         (setq thread (car th)
7737                               th nil)
7738                       (setq th (cdr th))))))))))
7739   thread)
7740
7741 (defun gnus-cut-threads (threads)
7742   "Cut off all uninteresting articles from the beginning of threads."
7743   (when (or (eq gnus-fetch-old-headers 'some)
7744             (eq gnus-fetch-old-headers 'invisible)
7745             (numberp gnus-fetch-old-headers)
7746             (eq gnus-build-sparse-threads 'some)
7747             (eq gnus-build-sparse-threads 'more))
7748     (let ((th threads))
7749       (while th
7750         (setcar th (gnus-cut-thread (car th)))
7751         (setq th (cdr th)))))
7752   ;; Remove nixed out threads.
7753   (delq nil threads))
7754
7755 (defun gnus-summary-initial-limit (&optional show-if-empty)
7756   "Figure out what the initial limit is supposed to be on group entry.
7757 This entails weeding out unwanted dormants, low-scored articles,
7758 fetch-old-headers verbiage, and so on."
7759   ;; Most groups have nothing to remove.
7760   (if (or gnus-inhibit-limiting
7761           (and (null gnus-newsgroup-dormant)
7762                (eq gnus-newsgroup-display 'gnus-not-ignore)
7763                (not (eq gnus-fetch-old-headers 'some))
7764                (not (numberp gnus-fetch-old-headers))
7765                (not (eq gnus-fetch-old-headers 'invisible))
7766                (null gnus-summary-expunge-below)
7767                (not (eq gnus-build-sparse-threads 'some))
7768                (not (eq gnus-build-sparse-threads 'more))
7769                (null gnus-thread-expunge-below)
7770                (not gnus-use-nocem)))
7771       ()                                ; Do nothing.
7772     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7773     (setq gnus-newsgroup-limit nil)
7774     (mapatoms
7775      (lambda (node)
7776        (unless (car (symbol-value node))
7777          ;; These threads have no parents -- they are roots.
7778          (let ((nodes (cdr (symbol-value node)))
7779                thread)
7780            (while nodes
7781              (if (and gnus-thread-expunge-below
7782                       (< (gnus-thread-total-score (car nodes))
7783                          gnus-thread-expunge-below))
7784                  (gnus-expunge-thread (pop nodes))
7785                (setq thread (pop nodes))
7786                (gnus-summary-limit-children thread))))))
7787      gnus-newsgroup-dependencies)
7788     ;; If this limitation resulted in an empty group, we might
7789     ;; pop the previous limit and use it instead.
7790     (when (and (not gnus-newsgroup-limit)
7791                show-if-empty)
7792       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7793     gnus-newsgroup-limit))
7794
7795 (defun gnus-summary-limit-children (thread)
7796   "Return 1 if this subthread is visible and 0 if it is not."
7797   ;; First we get the number of visible children to this thread.  This
7798   ;; is done by recursing down the thread using this function, so this
7799   ;; will really go down to a leaf article first, before slowly
7800   ;; working its way up towards the root.
7801   (when thread
7802     (let* ((max-lisp-eval-depth 5000)
7803            (children
7804            (if (cdr thread)
7805                (apply '+ (mapcar 'gnus-summary-limit-children
7806                                  (cdr thread)))
7807              0))
7808           (number (mail-header-number (car thread)))
7809           score)
7810       (if (and
7811            (not (memq number gnus-newsgroup-marked))
7812            (or
7813             ;; If this article is dormant and has absolutely no visible
7814             ;; children, then this article isn't visible.
7815             (and (memq number gnus-newsgroup-dormant)
7816                  (zerop children))
7817             ;; If this is "fetch-old-headered" and there is no
7818             ;; visible children, then we don't want this article.
7819             (and (or (eq gnus-fetch-old-headers 'some)
7820                      (numberp gnus-fetch-old-headers))
7821                  (gnus-summary-article-ancient-p number)
7822                  (zerop children))
7823             ;; If this is "fetch-old-headered" and `invisible', then
7824             ;; we don't want this article.
7825             (and (eq gnus-fetch-old-headers 'invisible)
7826                  (gnus-summary-article-ancient-p number))
7827             ;; If this is a sparsely inserted article with no children,
7828             ;; we don't want it.
7829             (and (eq gnus-build-sparse-threads 'some)
7830                  (gnus-summary-article-sparse-p number)
7831                  (zerop children))
7832             ;; If we use expunging, and this article is really
7833             ;; low-scored, then we don't want this article.
7834             (when (and gnus-summary-expunge-below
7835                        (< (setq score
7836                                 (or (cdr (assq number gnus-newsgroup-scored))
7837                                     gnus-summary-default-score))
7838                           gnus-summary-expunge-below))
7839               ;; We increase the expunge-tally here, but that has
7840               ;; nothing to do with the limits, really.
7841               (incf gnus-newsgroup-expunged-tally)
7842               ;; We also mark as read here, if that's wanted.
7843               (when (and gnus-summary-mark-below
7844                          (< score gnus-summary-mark-below))
7845                 (setq gnus-newsgroup-unreads
7846                       (delq number gnus-newsgroup-unreads))
7847                 (if gnus-newsgroup-auto-expire
7848                     (push number gnus-newsgroup-expirable)
7849                   (push (cons number gnus-low-score-mark)
7850                         gnus-newsgroup-reads)))
7851               t)
7852             ;; Do the `display' group parameter.
7853             (and gnus-newsgroup-display
7854                  (not (funcall gnus-newsgroup-display)))
7855             ;; Check NoCeM things.
7856             (if (and gnus-use-nocem
7857                      (gnus-nocem-unwanted-article-p
7858                       (mail-header-id (car thread))))
7859                 (progn
7860                   (setq gnus-newsgroup-unreads
7861                         (delq number gnus-newsgroup-unreads))
7862                   t))))
7863           ;; Nope, invisible article.
7864           0
7865         ;; Ok, this article is to be visible, so we add it to the limit
7866         ;; and return 1.
7867         (push number gnus-newsgroup-limit)
7868         1))))
7869
7870 (defun gnus-expunge-thread (thread)
7871   "Mark all articles in THREAD as read."
7872   (let* ((number (mail-header-number (car thread))))
7873     (incf gnus-newsgroup-expunged-tally)
7874     ;; We also mark as read here, if that's wanted.
7875     (setq gnus-newsgroup-unreads
7876           (delq number gnus-newsgroup-unreads))
7877     (if gnus-newsgroup-auto-expire
7878         (push number gnus-newsgroup-expirable)
7879       (push (cons number gnus-low-score-mark)
7880             gnus-newsgroup-reads)))
7881   ;; Go recursively through all subthreads.
7882   (mapcar 'gnus-expunge-thread (cdr thread)))
7883
7884 ;; Summary article oriented commands
7885
7886 (defun gnus-summary-refer-parent-article (n)
7887   "Refer parent article N times.
7888 If N is negative, go to ancestor -N instead.
7889 The difference between N and the number of articles fetched is returned."
7890   (interactive "p")
7891   (let ((skip 1)
7892         error header ref)
7893     (when (not (natnump n))
7894       (setq skip (abs n)
7895             n 1))
7896     (while (and (> n 0)
7897                 (not error))
7898       (setq header (gnus-summary-article-header))
7899       (if (and (eq (mail-header-number header)
7900                    (cdr gnus-article-current))
7901                (equal gnus-newsgroup-name
7902                       (car gnus-article-current)))
7903           ;; If we try to find the parent of the currently
7904           ;; displayed article, then we take a look at the actual
7905           ;; References header, since this is slightly more
7906           ;; reliable than the References field we got from the
7907           ;; server.
7908           (save-excursion
7909             (set-buffer gnus-original-article-buffer)
7910             (nnheader-narrow-to-headers)
7911             (unless (setq ref (message-fetch-field "references"))
7912               (setq ref (message-fetch-field "in-reply-to")))
7913             (widen))
7914         (setq ref
7915               ;; It's not the current article, so we take a bet on
7916               ;; the value we got from the server.
7917               (mail-header-references header)))
7918       (if (and ref
7919                (not (equal ref "")))
7920           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7921             (gnus-message 1 "Couldn't find parent"))
7922         (gnus-message 1 "No references in article %d"
7923                       (gnus-summary-article-number))
7924         (setq error t))
7925       (decf n))
7926     (gnus-summary-position-point)
7927     n))
7928
7929 (defun gnus-summary-refer-references ()
7930   "Fetch all articles mentioned in the References header.
7931 Return the number of articles fetched."
7932   (interactive)
7933   (let ((ref (mail-header-references (gnus-summary-article-header)))
7934         (current (gnus-summary-article-number))
7935         (n 0))
7936     (if (or (not ref)
7937             (equal ref ""))
7938         (error "No References in the current article")
7939       ;; For each Message-ID in the References header...
7940       (while (string-match "<[^>]*>" ref)
7941         (incf n)
7942         ;; ... fetch that article.
7943         (gnus-summary-refer-article
7944          (prog1 (match-string 0 ref)
7945            (setq ref (substring ref (match-end 0))))))
7946       (gnus-summary-goto-subject current)
7947       (gnus-summary-position-point)
7948       n)))
7949
7950 (defun gnus-summary-refer-thread (&optional limit)
7951   "Fetch all articles in the current thread.
7952 If LIMIT (the numerical prefix), fetch that many old headers instead
7953 of what's specified by the `gnus-refer-thread-limit' variable."
7954   (interactive "P")
7955   (let ((id (mail-header-id (gnus-summary-article-header)))
7956         (limit (if limit (prefix-numeric-value limit)
7957                  gnus-refer-thread-limit)))
7958     (unless (eq gnus-fetch-old-headers 'invisible)
7959       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7960       ;; Retrieve the headers and read them in.
7961       (if (eq (if (numberp limit)
7962                   (gnus-retrieve-headers
7963                    (list (min
7964                           (+ (mail-header-number
7965                               (gnus-summary-article-header))
7966                              limit)
7967                           gnus-newsgroup-end))
7968                    gnus-newsgroup-name (* limit 2))
7969                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
7970                 ;; headers.
7971                 (gnus-retrieve-headers (list gnus-newsgroup-end)
7972                                        gnus-newsgroup-name limit))
7973               'nov)
7974           (gnus-build-all-threads)
7975         (error "Can't fetch thread from backends that don't support NOV"))
7976       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7977     (gnus-summary-limit-include-thread id)))
7978
7979 (defun gnus-summary-refer-article (message-id)
7980   "Fetch an article specified by MESSAGE-ID."
7981   (interactive "sMessage-ID: ")
7982   (when (and (stringp message-id)
7983              (not (zerop (length message-id))))
7984     ;; Construct the correct Message-ID if necessary.
7985     ;; Suggested by tale@pawl.rpi.edu.
7986     (unless (string-match "^<" message-id)
7987       (setq message-id (concat "<" message-id)))
7988     (unless (string-match ">$" message-id)
7989       (setq message-id (concat message-id ">")))
7990     (let* ((header (gnus-id-to-header message-id))
7991            (sparse (and header
7992                         (gnus-summary-article-sparse-p
7993                          (mail-header-number header))
7994                         (memq (mail-header-number header)
7995                               gnus-newsgroup-limit)))
7996            number)
7997       (cond
7998        ;; If the article is present in the buffer we just go to it.
7999        ((and header
8000              (or (not (gnus-summary-article-sparse-p
8001                        (mail-header-number header)))
8002                  sparse))
8003         (prog1
8004             (gnus-summary-goto-article
8005              (mail-header-number header) nil t)
8006           (when sparse
8007             (gnus-summary-update-article (mail-header-number header)))))
8008        (t
8009         ;; We fetch the article.
8010         (catch 'found
8011           (dolist (gnus-override-method (gnus-refer-article-methods))
8012             (gnus-check-server gnus-override-method)
8013             ;; Fetch the header, and display the article.
8014             (when (setq number (gnus-summary-insert-subject message-id))
8015               (gnus-summary-select-article nil nil nil number)
8016               (throw 'found t)))
8017           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8018
8019 (defun gnus-refer-article-methods ()
8020   "Return a list of referable methods."
8021   (cond
8022    ;; No method, so we default to current and native.
8023    ((null gnus-refer-article-method)
8024     (list gnus-current-select-method gnus-select-method))
8025    ;; Current.
8026    ((eq 'current gnus-refer-article-method)
8027     (list gnus-current-select-method))
8028    ;; List of select methods.
8029    ((not (and (symbolp (car gnus-refer-article-method))
8030               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8031     (let (out)
8032       (dolist (method gnus-refer-article-method)
8033         (push (if (eq 'current method)
8034                   gnus-current-select-method
8035                 method)
8036               out))
8037       (nreverse out)))
8038    ;; One single select method.
8039    (t
8040     (list gnus-refer-article-method))))
8041
8042 (defun gnus-summary-edit-parameters ()
8043   "Edit the group parameters of the current group."
8044   (interactive)
8045   (gnus-group-edit-group gnus-newsgroup-name 'params))
8046
8047 (defun gnus-summary-customize-parameters ()
8048   "Customize the group parameters of the current group."
8049   (interactive)
8050   (gnus-group-customize gnus-newsgroup-name))
8051
8052 (defun gnus-summary-enter-digest-group (&optional force)
8053   "Enter an nndoc group based on the current article.
8054 If FORCE, force a digest interpretation.  If not, try
8055 to guess what the document format is."
8056   (interactive "P")
8057   (let ((conf gnus-current-window-configuration))
8058     (save-excursion
8059       (gnus-summary-select-article))
8060     (setq gnus-current-window-configuration conf)
8061     (let* ((name (format "%s-%d"
8062                          (gnus-group-prefixed-name
8063                           gnus-newsgroup-name (list 'nndoc ""))
8064                          (save-excursion
8065                            (set-buffer gnus-summary-buffer)
8066                            gnus-current-article)))
8067            (ogroup gnus-newsgroup-name)
8068            (params (append (gnus-info-params (gnus-get-info ogroup))
8069                            (list (cons 'to-group ogroup))
8070                            (list (cons 'save-article-group ogroup))))
8071            (case-fold-search t)
8072            (buf (current-buffer))
8073            dig to-address)
8074       (save-excursion
8075         (set-buffer gnus-original-article-buffer)
8076         ;; Have the digest group inherit the main mail address of
8077         ;; the parent article.
8078         (when (setq to-address (or (gnus-fetch-field "reply-to")
8079                                    (gnus-fetch-field "from")))
8080           (setq params (append
8081                         (list (cons 'to-address
8082                                     (funcall gnus-decode-encoded-word-function
8083                                              to-address))))))
8084         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8085         (insert-buffer-substring gnus-original-article-buffer)
8086         ;; Remove lines that may lead nndoc to misinterpret the
8087         ;; document type.
8088         (narrow-to-region
8089          (goto-char (point-min))
8090          (or (search-forward "\n\n" nil t) (point)))
8091         (goto-char (point-min))
8092         (delete-matching-lines "^Path:\\|^From ")
8093         (widen))
8094       (unwind-protect
8095           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8096                     (gnus-newsgroup-ephemeral-ignored-charsets
8097                      gnus-newsgroup-ignored-charsets))
8098                 (gnus-group-read-ephemeral-group
8099                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8100                               (nndoc-article-type
8101                                ,(if force 'mbox 'guess)))
8102                  t nil nil nil
8103                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8104                                                         "ADAPT")))))
8105               ;; Make all postings to this group go to the parent group.
8106               (nconc (gnus-info-params (gnus-get-info name))
8107                      params)
8108             ;; Couldn't select this doc group.
8109             (switch-to-buffer buf)
8110             (gnus-set-global-variables)
8111             (gnus-configure-windows 'summary)
8112             (gnus-message 3 "Article couldn't be entered?"))
8113         (kill-buffer dig)))))
8114
8115 (defun gnus-summary-read-document (n)
8116   "Open a new group based on the current article(s).
8117 This will allow you to read digests and other similar
8118 documents as newsgroups.
8119 Obeys the standard process/prefix convention."
8120   (interactive "P")
8121   (let* ((articles (gnus-summary-work-articles n))
8122          (ogroup gnus-newsgroup-name)
8123          (params (append (gnus-info-params (gnus-get-info ogroup))
8124                          (list (cons 'to-group ogroup))))
8125          article group egroup groups vgroup)
8126     (while (setq article (pop articles))
8127       (setq group (format "%s-%d" gnus-newsgroup-name article))
8128       (gnus-summary-remove-process-mark article)
8129       (when (gnus-summary-display-article article)
8130         (save-excursion
8131           (with-temp-buffer
8132             (insert-buffer-substring gnus-original-article-buffer)
8133             ;; Remove some headers that may lead nndoc to make
8134             ;; the wrong guess.
8135             (message-narrow-to-head)
8136             (goto-char (point-min))
8137             (delete-matching-lines "^\\(Path\\):\\|^From ")
8138             (widen)
8139             (if (setq egroup
8140                       (gnus-group-read-ephemeral-group
8141                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8142                                      (nndoc-article-type guess))
8143                        t nil t))
8144                 (progn
8145             ;; Make all postings to this group go to the parent group.
8146                   (nconc (gnus-info-params (gnus-get-info egroup))
8147                          params)
8148                   (push egroup groups))
8149               ;; Couldn't select this doc group.
8150               (gnus-error 3 "Article couldn't be entered"))))))
8151     ;; Now we have selected all the documents.
8152     (cond
8153      ((not groups)
8154       (error "None of the articles could be interpreted as documents"))
8155      ((gnus-group-read-ephemeral-group
8156        (setq vgroup (format
8157                      "nnvirtual:%s-%s" gnus-newsgroup-name
8158                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8159        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8160        t
8161        (cons (current-buffer) 'summary)))
8162      (t
8163       (error "Couldn't select virtual nndoc group")))))
8164
8165 (defun gnus-summary-isearch-article (&optional regexp-p)
8166   "Do incremental search forward on the current article.
8167 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8168   (interactive "P")
8169   (gnus-summary-select-article)
8170   (gnus-configure-windows 'article)
8171   (gnus-eval-in-buffer-window gnus-article-buffer
8172     (save-restriction
8173       (widen)
8174       (isearch-forward regexp-p))))
8175
8176 (defun gnus-summary-search-article-forward (regexp &optional backward)
8177   "Search for an article containing REGEXP forward.
8178 If BACKWARD, search backward instead."
8179   (interactive
8180    (list (read-string
8181           (format "Search article %s (regexp%s): "
8182                   (if current-prefix-arg "backward" "forward")
8183                   (if gnus-last-search-regexp
8184                       (concat ", default " gnus-last-search-regexp)
8185                     "")))
8186          current-prefix-arg))
8187   (if (string-equal regexp "")
8188       (setq regexp (or gnus-last-search-regexp ""))
8189     (setq gnus-last-search-regexp regexp)
8190     (setq gnus-article-before-search gnus-current-article))
8191   ;; Intentionally set gnus-last-article.
8192   (setq gnus-last-article gnus-article-before-search)
8193   (let ((gnus-last-article gnus-last-article))
8194     (if (gnus-summary-search-article regexp backward)
8195         (gnus-summary-show-thread)
8196       (error "Search failed: \"%s\"" regexp))))
8197
8198 (defun gnus-summary-search-article-backward (regexp)
8199   "Search for an article containing REGEXP backward."
8200   (interactive
8201    (list (read-string
8202           (format "Search article backward (regexp%s): "
8203                   (if gnus-last-search-regexp
8204                       (concat ", default " gnus-last-search-regexp)
8205                     "")))))
8206   (gnus-summary-search-article-forward regexp 'backward))
8207
8208 (defun gnus-summary-search-article (regexp &optional backward)
8209   "Search for an article containing REGEXP.
8210 Optional argument BACKWARD means do search for backward.
8211 `gnus-select-article-hook' is not called during the search."
8212   ;; We have to require this here to make sure that the following
8213   ;; dynamic binding isn't shadowed by autoloading.
8214   (require 'gnus-async)
8215   (require 'gnus-art)
8216   (let ((gnus-select-article-hook nil)  ;Disable hook.
8217         (gnus-article-prepare-hook nil)
8218         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8219         (gnus-use-article-prefetch nil)
8220         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8221         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8222         (gnus-visual nil)
8223         (gnus-keep-backlog nil)
8224         (gnus-break-pages nil)
8225         (gnus-summary-display-arrow nil)
8226         (gnus-updated-mode-lines nil)
8227         (gnus-auto-center-summary nil)
8228         (sum (current-buffer))
8229         (gnus-display-mime-function nil)
8230         (found nil)
8231         point)
8232     (gnus-save-hidden-threads
8233       (gnus-summary-select-article)
8234       (set-buffer gnus-article-buffer)
8235       (goto-char (window-point (get-buffer-window (current-buffer))))
8236       (when backward
8237         (forward-line -1))
8238       (while (not found)
8239         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8240         (if (if backward
8241                 (re-search-backward regexp nil t)
8242               (re-search-forward regexp nil t))
8243             ;; We found the regexp.
8244             (progn
8245               (setq found 'found)
8246               (beginning-of-line)
8247               (set-window-start
8248                (get-buffer-window (current-buffer))
8249                (point))
8250               (forward-line 1)
8251               (set-window-point
8252                (get-buffer-window (current-buffer))
8253                (point))
8254               (set-buffer sum)
8255               (setq point (point)))
8256           ;; We didn't find it, so we go to the next article.
8257           (set-buffer sum)
8258           (setq found 'not)
8259           (while (eq found 'not)
8260             (if (not (if backward (gnus-summary-find-prev)
8261                        (gnus-summary-find-next)))
8262                 ;; No more articles.
8263                 (setq found t)
8264               ;; Select the next article and adjust point.
8265               (unless (gnus-summary-article-sparse-p
8266                        (gnus-summary-article-number))
8267                 (setq found nil)
8268                 (gnus-summary-select-article)
8269                 (set-buffer gnus-article-buffer)
8270                 (widen)
8271                 (goto-char (if backward (point-max) (point-min))))))))
8272       (gnus-message 7 ""))
8273     ;; Return whether we found the regexp.
8274     (when (eq found 'found)
8275       (goto-char point)
8276       (gnus-summary-show-thread)
8277       (gnus-summary-goto-subject gnus-current-article)
8278       (gnus-summary-position-point)
8279       t)))
8280
8281 (defun gnus-find-matching-articles (header regexp)
8282   "Return a list of all articles that match REGEXP on HEADER.
8283 This search includes all articles in the current group that Gnus has
8284 fetched headers for, whether they are displayed or not."
8285   (let ((articles nil)
8286         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8287         (case-fold-search t))
8288     (dolist (header gnus-newsgroup-headers)
8289       (when (string-match regexp (funcall func header))
8290         (push (mail-header-number header) articles)))
8291     (nreverse articles)))
8292
8293 (defun gnus-summary-find-matching (header regexp &optional backward unread
8294                                           not-case-fold not-matching)
8295   "Return a list of all articles that match REGEXP on HEADER.
8296 The search stars on the current article and goes forwards unless
8297 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8298 If UNREAD is non-nil, only unread articles will
8299 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8300 in the comparisons. If NOT-MATCHING, return a list of all articles that
8301 not match REGEXP on HEADER."
8302   (let ((case-fold-search (not not-case-fold))
8303         articles d func)
8304     (if (consp header)
8305         (if (eq (car header) 'extra)
8306             (setq func
8307                   `(lambda (h)
8308                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8309                          "")))
8310           (error "%s is an invalid header" header))
8311       (unless (fboundp (intern (concat "mail-header-" header)))
8312         (error "%s is not a valid header" header))
8313       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8314     (dolist (d (if (eq backward 'all)
8315                    gnus-newsgroup-data
8316                  (gnus-data-find-list
8317                   (gnus-summary-article-number)
8318                   (gnus-data-list backward))))
8319       (when (and (or (not unread)       ; We want all articles...
8320                      (gnus-data-unread-p d)) ; Or just unreads.
8321                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8322                  (if not-matching
8323                      (not (string-match
8324                            regexp
8325                            (funcall func (gnus-data-header d))))
8326                    (string-match regexp
8327                                  (funcall func (gnus-data-header d)))))
8328         (push (gnus-data-number d) articles))) ; Success!
8329     (nreverse articles)))
8330
8331 (defun gnus-summary-execute-command (header regexp command &optional backward)
8332   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8333 If HEADER is an empty string (or nil), the match is done on the entire
8334 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8335   (interactive
8336    (list (let ((completion-ignore-case t))
8337            (completing-read
8338             "Header name: "
8339             (mapcar (lambda (header) (list (format "%s" header)))
8340                     (append
8341                      '("Number" "Subject" "From" "Lines" "Date"
8342                        "Message-ID" "Xref" "References" "Body")
8343                      gnus-extra-headers))
8344             nil 'require-match))
8345          (read-string "Regexp: ")
8346          (read-key-sequence "Command: ")
8347          current-prefix-arg))
8348   (when (equal header "Body")
8349     (setq header ""))
8350   ;; Hidden thread subtrees must be searched as well.
8351   (gnus-summary-show-all-threads)
8352   ;; We don't want to change current point nor window configuration.
8353   (save-excursion
8354     (save-window-excursion
8355       (let (gnus-visual
8356             gnus-treat-strip-trailing-blank-lines
8357             gnus-treat-strip-leading-blank-lines
8358             gnus-treat-strip-multiple-blank-lines
8359             gnus-treat-hide-boring-headers
8360             gnus-treat-fold-newsgroups
8361             gnus-article-prepare-hook)
8362         (gnus-message 6 "Executing %s..." (key-description command))
8363         ;; We'd like to execute COMMAND interactively so as to give arguments.
8364         (gnus-execute header regexp
8365                       `(call-interactively ',(key-binding command))
8366                       backward)
8367         (gnus-message 6 "Executing %s...done" (key-description command))))))
8368
8369 (defun gnus-summary-beginning-of-article ()
8370   "Scroll the article back to the beginning."
8371   (interactive)
8372   (gnus-summary-select-article)
8373   (gnus-configure-windows 'article)
8374   (gnus-eval-in-buffer-window gnus-article-buffer
8375     (widen)
8376     (goto-char (point-min))
8377     (when gnus-page-broken
8378       (gnus-narrow-to-page))))
8379
8380 (defun gnus-summary-end-of-article ()
8381   "Scroll to the end of the article."
8382   (interactive)
8383   (gnus-summary-select-article)
8384   (gnus-configure-windows 'article)
8385   (gnus-eval-in-buffer-window gnus-article-buffer
8386     (widen)
8387     (goto-char (point-max))
8388     (recenter -3)
8389     (when gnus-page-broken
8390       (gnus-narrow-to-page))))
8391
8392 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8393   "Truncate to LEN and quote all \"(\"'s in STRING."
8394   (gnus-replace-in-string (if (and len (> (length string) len))
8395                               (substring string 0 len)
8396                             string)
8397                           "[()]" "\\\\\\&"))
8398
8399 (defun gnus-summary-print-article (&optional filename n)
8400   "Generate and print a PostScript image of the N next (mail) articles.
8401
8402 If N is negative, print the N previous articles.  If N is nil and articles
8403 have been marked with the process mark, print these instead.
8404
8405 If the optional first argument FILENAME is nil, send the image to the
8406 printer.  If FILENAME is a string, save the PostScript image in a file with
8407 that name.  If FILENAME is a number, prompt the user for the name of the file
8408 to save in."
8409   (interactive (list (ps-print-preprint current-prefix-arg)))
8410   (dolist (article (gnus-summary-work-articles n))
8411     (gnus-summary-select-article nil nil 'pseudo article)
8412     (gnus-eval-in-buffer-window gnus-article-buffer
8413       (gnus-print-buffer))
8414     (gnus-summary-remove-process-mark article))
8415   (ps-despool filename))
8416
8417 (defun gnus-print-buffer ()
8418   (let ((buffer (generate-new-buffer " *print*")))
8419     (unwind-protect
8420         (progn
8421           (copy-to-buffer buffer (point-min) (point-max))
8422           (set-buffer buffer)
8423           (gnus-article-delete-invisible-text)
8424           (gnus-remove-text-with-property 'gnus-decoration)
8425           (when (gnus-visual-p 'article-highlight 'highlight)
8426             ;; Copy-to-buffer doesn't copy overlay.  So redo
8427             ;; highlight.
8428             (let ((gnus-article-buffer buffer))
8429               (gnus-article-highlight-citation t)
8430               (gnus-article-highlight-signature)))
8431           (let ((ps-left-header
8432                  (list
8433                   (concat "("
8434                           (gnus-summary-print-truncate-and-quote
8435                            (mail-header-subject gnus-current-headers)
8436                            66) ")")
8437                   (concat "("
8438                           (gnus-summary-print-truncate-and-quote
8439                            (mail-header-from gnus-current-headers)
8440                            45) ")")))
8441                 (ps-right-header
8442                  (list
8443                   "/pagenumberstring load"
8444                   (concat "("
8445                           (mail-header-date gnus-current-headers) ")"))))
8446             (gnus-run-hooks 'gnus-ps-print-hook)
8447             (save-excursion
8448               (if window-system
8449                   (ps-spool-buffer-with-faces)
8450                 (ps-spool-buffer)))))
8451       (kill-buffer buffer))))
8452
8453 (defun gnus-summary-show-article (&optional arg)
8454   "Force redisplaying of the current article.
8455 If ARG (the prefix) is a number, show the article with the charset
8456 defined in `gnus-summary-show-article-charset-alist', or the charset
8457 input.
8458 If ARG (the prefix) is non-nil and not a number, show the raw article
8459 without any article massaging functions being run.  Normally, the key strokes
8460 are `C-u g'."
8461   (interactive "P")
8462   (cond
8463    ((numberp arg)
8464     (gnus-summary-show-article t)
8465     (let ((gnus-newsgroup-charset
8466            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8467                (mm-read-coding-system
8468                 "View as charset: " ;; actually it is coding system.
8469                 (save-excursion
8470                   (set-buffer gnus-article-buffer)
8471                   (mm-detect-coding-region (point) (point-max))))))
8472           (gnus-newsgroup-ignored-charsets 'gnus-all))
8473       (gnus-summary-select-article nil 'force)
8474       (let ((deps gnus-newsgroup-dependencies)
8475             head header lines)
8476         (save-excursion
8477           (set-buffer gnus-original-article-buffer)
8478           (save-restriction
8479             (message-narrow-to-head)
8480             (setq head (buffer-string))
8481             (goto-char (point-min))
8482             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8483               (goto-char (point-max))
8484               (widen)
8485               (setq lines (1- (count-lines (point) (point-max))))))
8486           (with-temp-buffer
8487             (insert (format "211 %d Article retrieved.\n"
8488                             (cdr gnus-article-current)))
8489             (insert head)
8490             (if lines (insert (format "Lines: %d\n" lines)))
8491             (insert ".\n")
8492             (let ((nntp-server-buffer (current-buffer)))
8493               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8494         (gnus-data-set-header
8495          (gnus-data-find (cdr gnus-article-current))
8496          header)
8497         (gnus-summary-update-article-line
8498          (cdr gnus-article-current) header)
8499         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8500           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8501    ((not arg)
8502     ;; Select the article the normal way.
8503     (gnus-summary-select-article nil 'force))
8504    (t
8505     ;; We have to require this here to make sure that the following
8506     ;; dynamic binding isn't shadowed by autoloading.
8507     (require 'gnus-async)
8508     (require 'gnus-art)
8509     ;; Bind the article treatment functions to nil.
8510     (let ((gnus-have-all-headers t)
8511           gnus-article-prepare-hook
8512           gnus-article-decode-hook
8513           gnus-display-mime-function
8514           gnus-break-pages)
8515       ;; Destroy any MIME parts.
8516       (when (gnus-buffer-live-p gnus-article-buffer)
8517         (save-excursion
8518           (set-buffer gnus-article-buffer)
8519           (mm-destroy-parts gnus-article-mime-handles)
8520           ;; Set it to nil for safety reason.
8521           (setq gnus-article-mime-handle-alist nil)
8522           (setq gnus-article-mime-handles nil)))
8523       (gnus-summary-select-article nil 'force))))
8524   (gnus-summary-goto-subject gnus-current-article)
8525   (gnus-summary-position-point))
8526
8527 (defun gnus-summary-show-raw-article ()
8528   "Show the raw article without any article massaging functions being run."
8529   (interactive)
8530   (gnus-summary-show-article t))
8531
8532 (defun gnus-summary-verbose-headers (&optional arg)
8533   "Toggle permanent full header display.
8534 If ARG is a positive number, turn header display on.
8535 If ARG is a negative number, turn header display off."
8536   (interactive "P")
8537   (setq gnus-show-all-headers
8538         (cond ((or (not (numberp arg))
8539                    (zerop arg))
8540                (not gnus-show-all-headers))
8541               ((natnump arg)
8542                t)))
8543   (gnus-summary-show-article))
8544
8545 (defun gnus-summary-toggle-header (&optional arg)
8546   "Show the headers if they are hidden, or hide them if they are shown.
8547 If ARG is a positive number, show the entire header.
8548 If ARG is a negative number, hide the unwanted header lines."
8549   (interactive "P")
8550   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8551                      (get-buffer-window gnus-article-buffer t))))
8552     (with-current-buffer gnus-article-buffer
8553       (widen)
8554       (article-narrow-to-head)
8555       (let* ((buffer-read-only nil)
8556              (inhibit-point-motion-hooks t)
8557              (hidden (if (numberp arg)
8558                          (>= arg 0)
8559                        (gnus-article-hidden-text-p 'headers)))
8560              s e)
8561         (delete-region (point-min) (point-max))
8562         (with-current-buffer gnus-original-article-buffer
8563           (goto-char (setq s (point-min)))
8564           (setq e (if (search-forward "\n\n" nil t)
8565                       (1- (point))
8566                     (point-max))))
8567         (insert-buffer-substring gnus-original-article-buffer s e)
8568         (article-decode-encoded-words)
8569         (if hidden
8570             (let ((gnus-treat-hide-headers nil)
8571                   (gnus-treat-hide-boring-headers nil))
8572               (gnus-delete-wash-type 'headers)
8573               (gnus-treat-article 'head))
8574           (gnus-treat-article 'head))
8575         (widen)
8576         (if window
8577             (set-window-start window (goto-char (point-min))))
8578         (setq gnus-page-broken
8579               (when gnus-break-pages
8580                 (gnus-narrow-to-page)
8581                 t))
8582         (gnus-set-mode-line 'article)))))
8583
8584 (defun gnus-summary-show-all-headers ()
8585   "Make all header lines visible."
8586   (interactive)
8587   (gnus-summary-toggle-header 1))
8588
8589 (defun gnus-summary-caesar-message (&optional arg)
8590   "Caesar rotate the current article by 13.
8591 The numerical prefix specifies how many places to rotate each letter
8592 forward."
8593   (interactive "P")
8594   (gnus-summary-select-article)
8595   (let ((mail-header-separator ""))
8596     (gnus-eval-in-buffer-window gnus-article-buffer
8597       (save-restriction
8598         (widen)
8599         (let ((start (window-start))
8600               buffer-read-only)
8601           (message-caesar-buffer-body arg)
8602           (set-window-start (get-buffer-window (current-buffer)) start))))))
8603
8604 (autoload 'unmorse-region "morse"
8605   "Convert morse coded text in region to ordinary ASCII text."
8606   t)
8607
8608 (defun gnus-summary-morse-message (&optional arg)
8609   "Morse decode the current article."
8610   (interactive "P")
8611   (gnus-summary-select-article)
8612   (let ((mail-header-separator ""))
8613     (gnus-eval-in-buffer-window gnus-article-buffer
8614       (save-excursion
8615         (save-restriction
8616           (widen)
8617           (let ((pos (window-start))
8618                 buffer-read-only)
8619             (goto-char (point-min))
8620             (when (message-goto-body)
8621               (gnus-narrow-to-body))
8622             (goto-char (point-min))
8623             (while (re-search-forward "·" (point-max) t)
8624               (replace-match "."))
8625             (unmorse-region (point-min) (point-max))
8626             (widen)
8627             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8628
8629 (defun gnus-summary-stop-page-breaking ()
8630   "Stop page breaking in the current article."
8631   (interactive)
8632   (gnus-summary-select-article)
8633   (gnus-eval-in-buffer-window gnus-article-buffer
8634     (widen)
8635     (when (gnus-visual-p 'page-marker)
8636       (let ((buffer-read-only nil))
8637         (gnus-remove-text-with-property 'gnus-prev)
8638         (gnus-remove-text-with-property 'gnus-next))
8639       (setq gnus-page-broken nil))))
8640
8641 (defun gnus-summary-move-article (&optional n to-newsgroup
8642                                             select-method action)
8643   "Move the current article to a different newsgroup.
8644 If N is a positive number, move the N next articles.
8645 If N is a negative number, move the N previous articles.
8646 If N is nil and any articles have been marked with the process mark,
8647 move those articles instead.
8648 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8649 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8650 re-spool using this method.
8651
8652 When called interactively with TO-NEWSGROUP being nil, the value of
8653 the variable `gnus-move-split-methods' is used for finding a default
8654 for the target newsgroup.
8655
8656 For this function to work, both the current newsgroup and the
8657 newsgroup that you want to move to have to support the `request-move'
8658 and `request-accept' functions.
8659
8660 ACTION can be either `move' (the default), `crosspost' or `copy'."
8661   (interactive "P")
8662   (unless action
8663     (setq action 'move))
8664   ;; Check whether the source group supports the required functions.
8665   (cond ((and (eq action 'move)
8666               (not (gnus-check-backend-function
8667                     'request-move-article gnus-newsgroup-name)))
8668          (error "The current group does not support article moving"))
8669         ((and (eq action 'crosspost)
8670               (not (gnus-check-backend-function
8671                     'request-replace-article gnus-newsgroup-name)))
8672          (error "The current group does not support article editing")))
8673   (let ((articles (gnus-summary-work-articles n))
8674         (prefix (if (gnus-check-backend-function
8675                      'request-move-article gnus-newsgroup-name)
8676                     (gnus-group-real-prefix gnus-newsgroup-name)
8677                   ""))
8678         (names '((move "Move" "Moving")
8679                  (copy "Copy" "Copying")
8680                  (crosspost "Crosspost" "Crossposting")))
8681         (copy-buf (save-excursion
8682                     (nnheader-set-temp-buffer " *copy article*")))
8683         art-group to-method new-xref article to-groups)
8684     (unless (assq action names)
8685       (error "Unknown action %s" action))
8686     ;; Read the newsgroup name.
8687     (when (and (not to-newsgroup)
8688                (not select-method))
8689       (if (and gnus-move-split-methods
8690                (not
8691                 (and (memq gnus-current-article articles)
8692                      (gnus-buffer-live-p gnus-original-article-buffer))))
8693           ;; When `gnus-move-split-methods' is non-nil, we have to
8694           ;; select an article to give `gnus-read-move-group-name' an
8695           ;; opportunity to suggest an appropriate default.  However,
8696           ;; we needn't render or mark the article.
8697           (let ((gnus-display-mime-function nil)
8698                 (gnus-article-prepare-hook nil)
8699                 (gnus-mark-article-hook nil))
8700             (gnus-summary-select-article nil nil nil (car articles))))
8701       (setq to-newsgroup
8702             (gnus-read-move-group-name
8703              (cadr (assq action names))
8704              (symbol-value (intern (format "gnus-current-%s-group" action)))
8705              articles prefix))
8706       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8707     (setq to-method (or select-method
8708                         (gnus-server-to-method
8709                          (gnus-group-method to-newsgroup))))
8710     ;; Check the method we are to move this article to...
8711     (unless (gnus-check-backend-function
8712              'request-accept-article (car to-method))
8713       (error "%s does not support article copying" (car to-method)))
8714     (unless (gnus-check-server to-method)
8715       (error "Can't open server %s" (car to-method)))
8716     (gnus-message 6 "%s to %s: %s..."
8717                   (caddr (assq action names))
8718                   (or (car select-method) to-newsgroup) articles)
8719     (while articles
8720       (setq article (pop articles))
8721       (setq
8722        art-group
8723        (cond
8724         ;; Move the article.
8725         ((eq action 'move)
8726          ;; Remove this article from future suppression.
8727          (gnus-dup-unsuppress-article article)
8728          (gnus-request-move-article
8729           article                       ; Article to move
8730           gnus-newsgroup-name           ; From newsgroup
8731           (nth 1 (gnus-find-method-for-group
8732                   gnus-newsgroup-name)) ; Server
8733           (list 'gnus-request-accept-article
8734                 to-newsgroup (list 'quote select-method)
8735                 (not articles) t)       ; Accept form
8736           (not articles)))              ; Only save nov last time
8737         ;; Copy the article.
8738         ((eq action 'copy)
8739          (save-excursion
8740            (set-buffer copy-buf)
8741            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8742              (gnus-request-accept-article
8743               to-newsgroup select-method (not articles) t))))
8744         ;; Crosspost the article.
8745         ((eq action 'crosspost)
8746          (let ((xref (message-tokenize-header
8747                       (mail-header-xref (gnus-summary-article-header article))
8748                       " ")))
8749            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8750                                   ":" (number-to-string article)))
8751            (unless xref
8752              (setq xref (list (system-name))))
8753            (setq new-xref
8754                  (concat
8755                   (mapconcat 'identity
8756                              (delete "Xref:" (delete new-xref xref))
8757                              " ")
8758                   " " new-xref))
8759            (save-excursion
8760              (set-buffer copy-buf)
8761              ;; First put the article in the destination group.
8762              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8763              (when (consp (setq art-group
8764                                 (gnus-request-accept-article
8765                                  to-newsgroup select-method (not articles))))
8766                (setq new-xref (concat new-xref " " (car art-group)
8767                                       ":"
8768                                       (number-to-string (cdr art-group))))
8769                ;; Now we have the new Xrefs header, so we insert
8770                ;; it and replace the new article.
8771                (nnheader-replace-header "Xref" new-xref)
8772                (gnus-request-replace-article
8773                 (cdr art-group) to-newsgroup (current-buffer))
8774                art-group))))))
8775       (cond
8776        ((not art-group)
8777         (gnus-message 1 "Couldn't %s article %s: %s"
8778                       (cadr (assq action names)) article
8779                       (nnheader-get-report (car to-method))))
8780        ((eq art-group 'junk)
8781         (when (eq action 'move)
8782           (let ((id (mail-header-id (gnus-data-header 
8783                                      (assoc article (gnus-data-list nil))))))
8784             (gnus-summary-mark-article article gnus-canceled-mark)
8785             (gnus-message 4 "Deleted article %s" article)
8786             ;; run the move/copy/crosspost/respool hook
8787             (run-hook-with-args 'gnus-summary-article-delete-hook 
8788                                 action id gnus-newsgroup-name nil
8789                                 select-method))))
8790        (t
8791         (let* ((pto-group (gnus-group-prefixed-name
8792                            (car art-group) to-method))
8793                (entry
8794                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8795                (info (nth 2 entry))
8796                (to-group (gnus-info-group info))
8797                to-marks)
8798           ;; Update the group that has been moved to.
8799           (when (and info
8800                      (memq action '(move copy)))
8801             (unless (member to-group to-groups)
8802               (push to-group to-groups))
8803
8804             (unless (memq article gnus-newsgroup-unreads)
8805               (push 'read to-marks)
8806               (gnus-info-set-read
8807                info (gnus-add-to-range (gnus-info-read info)
8808                                        (list (cdr art-group)))))
8809
8810             ;; See whether the article is to be put in the cache.
8811             (let ((marks gnus-article-mark-lists)
8812                   (to-article (cdr art-group)))
8813
8814               ;; Enter the article into the cache in the new group,
8815               ;; if that is required.
8816               (when gnus-use-cache
8817                 (gnus-cache-possibly-enter-article
8818                  to-group to-article
8819                  (memq article gnus-newsgroup-marked)
8820                  (memq article gnus-newsgroup-dormant)
8821                  (memq article gnus-newsgroup-unreads)))
8822
8823               (when gnus-preserve-marks
8824                 ;; Copy any marks over to the new group.
8825                 (when (and (equal to-group gnus-newsgroup-name)
8826                            (not (memq article gnus-newsgroup-unreads)))
8827                   ;; Mark this article as read in this group.
8828                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8829                   (setcdr (gnus-active to-group) to-article)
8830                   (setcdr gnus-newsgroup-active to-article))
8831
8832                 (while marks
8833                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8834                     (when (memq article (symbol-value
8835                                          (intern (format "gnus-newsgroup-%s"
8836                                                          (caar marks)))))
8837                       (push (cdar marks) to-marks)
8838                       ;; If the other group is the same as this group,
8839                       ;; then we have to add the mark to the list.
8840                       (when (equal to-group gnus-newsgroup-name)
8841                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8842                              (cons to-article
8843                                    (symbol-value
8844                                     (intern (format "gnus-newsgroup-%s"
8845                                                     (caar marks)))))))
8846                       ;; Copy the marks to other group.
8847                       (gnus-add-marked-articles
8848                        to-group (cdar marks) (list to-article) info)))
8849                   (setq marks (cdr marks)))
8850
8851                 (gnus-request-set-mark
8852                  to-group (list (list (list to-article) 'add to-marks))))
8853
8854               (gnus-dribble-enter
8855                (concat "(gnus-group-set-info '"
8856                        (gnus-prin1-to-string (gnus-get-info to-group))
8857                        ")"))))
8858
8859           ;; Update the Xref header in this article to point to
8860           ;; the new crossposted article we have just created.
8861           (when (eq action 'crosspost)
8862             (save-excursion
8863               (set-buffer copy-buf)
8864               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8865               (nnheader-replace-header "Xref" new-xref)
8866               (gnus-request-replace-article
8867                article gnus-newsgroup-name (current-buffer))))
8868
8869           ;; run the move/copy/crosspost/respool hook
8870           (let ((id (mail-header-id (gnus-data-header 
8871                                    (assoc article (gnus-data-list nil))))))
8872           (run-hook-with-args 'gnus-summary-article-move-hook 
8873                               action id gnus-newsgroup-name to-newsgroup
8874                               select-method)))
8875
8876         ;;;!!!Why is this necessary?
8877         (set-buffer gnus-summary-buffer)
8878
8879         (gnus-summary-goto-subject article)
8880         (when (eq action 'move)
8881           (gnus-summary-mark-article article gnus-canceled-mark))))
8882       (gnus-summary-remove-process-mark article))
8883     ;; Re-activate all groups that have been moved to.
8884     (save-excursion
8885       (set-buffer gnus-group-buffer)
8886       (let ((gnus-group-marked to-groups))
8887         (gnus-group-get-new-news-this-group nil t)))
8888
8889     (gnus-kill-buffer copy-buf)
8890     (gnus-summary-position-point)
8891     (gnus-set-mode-line 'summary)))
8892
8893 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8894   "Move the current article to a different newsgroup.
8895 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8896 When called interactively, if TO-NEWSGROUP is nil, use the value of
8897 the variable `gnus-move-split-methods' for finding a default target
8898 newsgroup.
8899 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8900 re-spool using this method."
8901   (interactive "P")
8902   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8903
8904 (defun gnus-summary-crosspost-article (&optional n)
8905   "Crosspost the current article to some other group."
8906   (interactive "P")
8907   (gnus-summary-move-article n nil nil 'crosspost))
8908
8909 (defcustom gnus-summary-respool-default-method nil
8910   "Default method type for respooling an article.
8911 If nil, use to the current newsgroup method."
8912   :type 'symbol
8913   :group 'gnus-summary-mail)
8914
8915 (defcustom gnus-summary-display-while-building nil
8916   "If not-nil, show and update the summary buffer as it's being built.
8917 If the value is t, update the buffer after every line is inserted.  If
8918 the value is an integer (N), update the display every N lines."
8919   :group 'gnus-thread
8920   :type '(choice (const :tag "off" nil)
8921                  number
8922                  (const :tag "frequently" t)))
8923
8924 (defun gnus-summary-respool-article (&optional n method)
8925   "Respool the current article.
8926 The article will be squeezed through the mail spooling process again,
8927 which means that it will be put in some mail newsgroup or other
8928 depending on `nnmail-split-methods'.
8929 If N is a positive number, respool the N next articles.
8930 If N is a negative number, respool the N previous articles.
8931 If N is nil and any articles have been marked with the process mark,
8932 respool those articles instead.
8933
8934 Respooling can be done both from mail groups and \"real\" newsgroups.
8935 In the former case, the articles in question will be moved from the
8936 current group into whatever groups they are destined to.  In the
8937 latter case, they will be copied into the relevant groups."
8938   (interactive
8939    (list current-prefix-arg
8940          (let* ((methods (gnus-methods-using 'respool))
8941                 (methname
8942                  (symbol-name (or gnus-summary-respool-default-method
8943                                   (car (gnus-find-method-for-group
8944                                         gnus-newsgroup-name)))))
8945                 (method
8946                  (gnus-completing-read-with-default
8947                   methname "What backend do you want to use when respooling?"
8948                   methods nil t nil 'gnus-mail-method-history))
8949                 ms)
8950            (cond
8951             ((zerop (length (setq ms (gnus-servers-using-backend
8952                                       (intern method)))))
8953              (list (intern method) ""))
8954             ((= 1 (length ms))
8955              (car ms))
8956             (t
8957              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8958                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8959                            ms-alist))))))))
8960   (unless method
8961     (error "No method given for respooling"))
8962   (if (assoc (symbol-name
8963               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8964              (gnus-methods-using 'respool))
8965       (gnus-summary-move-article n nil method)
8966     (gnus-summary-copy-article n nil method)))
8967
8968 (defun gnus-summary-import-article (file &optional edit)
8969   "Import an arbitrary file into a mail newsgroup."
8970   (interactive "fImport file: \nP")
8971   (let ((group gnus-newsgroup-name)
8972         (now (current-time))
8973         atts lines group-art)
8974     (unless (gnus-check-backend-function 'request-accept-article group)
8975       (error "%s does not support article importing" group))
8976     (or (file-readable-p file)
8977         (not (file-regular-p file))
8978         (error "Can't read %s" file))
8979     (save-excursion
8980       (set-buffer (gnus-get-buffer-create " *import file*"))
8981       (erase-buffer)
8982       (nnheader-insert-file-contents file)
8983       (goto-char (point-min))
8984       (if (nnheader-article-p)
8985           (save-restriction
8986             (goto-char (point-min))
8987             (search-forward "\n\n" nil t)
8988             (narrow-to-region (point-min) (1- (point)))
8989             (goto-char (point-min))
8990             (unless (re-search-forward "^date:" nil t)
8991               (goto-char (point-max))
8992               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8993        ;; This doesn't look like an article, so we fudge some headers.
8994         (setq atts (file-attributes file)
8995               lines (count-lines (point-min) (point-max)))
8996         (insert "From: " (read-string "From: ") "\n"
8997                 "Subject: " (read-string "Subject: ") "\n"
8998                 "Date: " (message-make-date (nth 5 atts)) "\n"
8999                 "Message-ID: " (message-make-message-id) "\n"
9000                 "Lines: " (int-to-string lines) "\n"
9001                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9002       (setq group-art (gnus-request-accept-article group nil t))
9003       (kill-buffer (current-buffer)))
9004     (setq gnus-newsgroup-active (gnus-activate-group group))
9005     (forward-line 1)
9006     (gnus-summary-goto-article (cdr group-art) nil t)
9007     (when edit
9008       (gnus-summary-edit-article))))
9009
9010 (defun gnus-summary-create-article ()
9011   "Create an article in a mail newsgroup."
9012   (interactive)
9013   (let ((group gnus-newsgroup-name)
9014         (now (current-time))
9015         group-art)
9016     (unless (gnus-check-backend-function 'request-accept-article group)
9017       (error "%s does not support article importing" group))
9018     (save-excursion
9019       (set-buffer (gnus-get-buffer-create " *import file*"))
9020       (erase-buffer)
9021       (goto-char (point-min))
9022       ;; This doesn't look like an article, so we fudge some headers.
9023       (insert "From: " (read-string "From: ") "\n"
9024               "Subject: " (read-string "Subject: ") "\n"
9025               "Date: " (message-make-date now) "\n"
9026               "Message-ID: " (message-make-message-id) "\n")
9027       (setq group-art (gnus-request-accept-article group nil t))
9028       (kill-buffer (current-buffer)))
9029     (setq gnus-newsgroup-active (gnus-activate-group group))
9030     (forward-line 1)
9031     (gnus-summary-goto-article (cdr group-art) nil t)
9032     (gnus-summary-edit-article)))
9033
9034 (defun gnus-summary-article-posted-p ()
9035   "Say whether the current (mail) article is available from news as well.
9036 This will be the case if the article has both been mailed and posted."
9037   (interactive)
9038   (let ((id (mail-header-references (gnus-summary-article-header)))
9039         (gnus-override-method (car (gnus-refer-article-methods))))
9040     (if (gnus-request-head id "")
9041         (gnus-message 2 "The current message was found on %s"
9042                       gnus-override-method)
9043       (gnus-message 2 "The current message couldn't be found on %s"
9044                     gnus-override-method)
9045       nil)))
9046
9047 (defun gnus-summary-expire-articles (&optional now)
9048   "Expire all articles that are marked as expirable in the current group."
9049   (interactive)
9050   (when (gnus-check-backend-function
9051          'request-expire-articles gnus-newsgroup-name)
9052     ;; This backend supports expiry.
9053     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9054            (expirable (if total
9055                           (progn
9056                             ;; We need to update the info for
9057                             ;; this group for `gnus-list-of-read-articles'
9058                             ;; to give us the right answer.
9059                             (gnus-run-hooks 'gnus-exit-group-hook)
9060                             (gnus-summary-update-info)
9061                             (gnus-list-of-read-articles gnus-newsgroup-name))
9062                         (setq gnus-newsgroup-expirable
9063                               (sort gnus-newsgroup-expirable '<))))
9064            (expiry-wait (if now 'immediate
9065                           (gnus-group-find-parameter
9066                            gnus-newsgroup-name 'expiry-wait)))
9067            (nnmail-expiry-target
9068             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9069                 nnmail-expiry-target))
9070            es)
9071       (when expirable
9072         ;; There are expirable articles in this group, so we run them
9073         ;; through the expiry process.
9074         (gnus-message 6 "Expiring articles...")
9075         (unless (gnus-check-group gnus-newsgroup-name)
9076           (error "Can't open server for %s" gnus-newsgroup-name))
9077         ;; The list of articles that weren't expired is returned.
9078         (save-excursion
9079           (if expiry-wait
9080               (let ((nnmail-expiry-wait-function nil)
9081                     (nnmail-expiry-wait expiry-wait))
9082                 (setq es (gnus-request-expire-articles
9083                           expirable gnus-newsgroup-name)))
9084             (setq es (gnus-request-expire-articles
9085                       expirable gnus-newsgroup-name)))
9086           (unless total
9087             (setq gnus-newsgroup-expirable es))
9088           ;; We go through the old list of expirable, and mark all
9089           ;; really expired articles as nonexistent.
9090           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9091             (let ((gnus-use-cache nil))
9092               (dolist (article expirable)
9093                 (when (and (not (memq article es))
9094                            (gnus-data-find article))
9095                   (gnus-summary-mark-article article gnus-canceled-mark)
9096                   (let ((id (mail-header-id (gnus-data-header 
9097                                              (assoc article 
9098                                                     (gnus-data-list nil))))))
9099                     (run-hook-with-args 'gnus-summary-article-expire-hook
9100                                         'delete id gnus-newsgroup-name nil
9101                                         nil)))))))
9102         (gnus-message 6 "Expiring articles...done")))))
9103
9104 (defun gnus-summary-expire-articles-now ()
9105   "Expunge all expirable articles in the current group.
9106 This means that *all* articles that are marked as expirable will be
9107 deleted forever, right now."
9108   (interactive)
9109   (or gnus-expert-user
9110       (gnus-yes-or-no-p
9111        "Are you really, really, really sure you want to delete all these messages? ")
9112       (error "Phew!"))
9113   (gnus-summary-expire-articles t))
9114
9115 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9116 (defun gnus-summary-delete-article (&optional n)
9117   "Delete the N next (mail) articles.
9118 This command actually deletes articles.  This is not a marking
9119 command.  The article will disappear forever from your life, never to
9120 return.
9121 If N is negative, delete backwards.
9122 If N is nil and articles have been marked with the process mark,
9123 delete these instead."
9124   (interactive "P")
9125   (unless (gnus-check-backend-function 'request-expire-articles
9126                                        gnus-newsgroup-name)
9127     (error "The current newsgroup does not support article deletion"))
9128   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9129     (error "Couldn't open server"))
9130   ;; Compute the list of articles to delete.
9131   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9132         (nnmail-expiry-target 'delete)
9133         not-deleted)
9134     (if (and gnus-novice-user
9135              (not (gnus-yes-or-no-p
9136                    (format "Do you really want to delete %s forever? "
9137                            (if (> (length articles) 1)
9138                                (format "these %s articles" (length articles))
9139                              "this article")))))
9140         ()
9141       ;; Delete the articles.
9142       (setq not-deleted (gnus-request-expire-articles
9143                          articles gnus-newsgroup-name 'force))
9144       (while articles
9145         (gnus-summary-remove-process-mark (car articles))
9146         ;; The backend might not have been able to delete the article
9147         ;; after all.
9148         (unless (memq (car articles) not-deleted)
9149           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9150         (let* ((article (car articles))
9151                (id (mail-header-id (gnus-data-header 
9152                                     (assoc article (gnus-data-list nil))))))
9153           (run-hook-with-args 'gnus-summary-article-delete-hook
9154                               'delete id gnus-newsgroup-name nil
9155                               nil))
9156         (setq articles (cdr articles)))
9157       (when not-deleted
9158         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9159     (gnus-summary-position-point)
9160     (gnus-set-mode-line 'summary)
9161     not-deleted))
9162
9163 (defun gnus-summary-edit-article (&optional arg)
9164   "Edit the current article.
9165 This will have permanent effect only in mail groups.
9166 If ARG is nil, edit the decoded articles.
9167 If ARG is 1, edit the raw articles.
9168 If ARG is 2, edit the raw articles even in read-only groups.
9169 If ARG is 3, edit the articles with the current handles.
9170 Otherwise, allow editing of articles even in read-only
9171 groups."
9172   (interactive "P")
9173   (let (force raw current-handles)
9174     (cond
9175      ((null arg))
9176      ((eq arg 1)
9177       (setq raw t))
9178      ((eq arg 2)
9179       (setq raw t
9180             force t))
9181      ((eq arg 3)
9182       (setq current-handles
9183             (and (gnus-buffer-live-p gnus-article-buffer)
9184                  (with-current-buffer gnus-article-buffer
9185                    (prog1
9186                        gnus-article-mime-handles
9187                      (setq gnus-article-mime-handles nil))))))
9188      (t
9189       (setq force t)))
9190     (when (and raw (not force)
9191                (member gnus-newsgroup-name '("nndraft:delayed"
9192                                              "nndraft:drafts"
9193                                              "nndraft:queue")))
9194       (error "Can't edit the raw article in group %s"
9195              gnus-newsgroup-name))
9196     (save-excursion
9197       (set-buffer gnus-summary-buffer)
9198       (let ((mail-parse-charset gnus-newsgroup-charset)
9199             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9200         (gnus-set-global-variables)
9201         (when (and (not force)
9202                    (gnus-group-read-only-p))
9203           (error "The current newsgroup does not support article editing"))
9204         (gnus-summary-show-article t)
9205         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9206           (with-current-buffer gnus-article-buffer
9207             (mm-enable-multibyte)))
9208         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9209             (setq raw t))
9210         (gnus-article-edit-article
9211          (if raw 'ignore
9212            `(lambda ()
9213               (let ((mbl mml-buffer-list))
9214                 (setq mml-buffer-list nil)
9215                 (mime-to-mml ,'current-handles)
9216                 (let ((mbl1 mml-buffer-list))
9217                   (setq mml-buffer-list mbl)
9218                   (set (make-local-variable 'mml-buffer-list) mbl1))
9219                 (make-local-hook 'kill-buffer-hook)
9220                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9221          `(lambda (no-highlight)
9222             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9223                   (message-options message-options)
9224                   (message-options-set-recipient)
9225                   (mail-parse-ignored-charsets
9226                    ',gnus-newsgroup-ignored-charsets))
9227               ,(if (not raw) '(progn
9228                                 (mml-to-mime)
9229                                 (mml-destroy-buffers)
9230                                 (remove-hook 'kill-buffer-hook
9231                                              'mml-destroy-buffers t)
9232                                 (kill-local-variable 'mml-buffer-list)))
9233               (gnus-summary-edit-article-done
9234                ,(or (mail-header-references gnus-current-headers) "")
9235                ,(gnus-group-read-only-p)
9236                ,gnus-summary-buffer no-highlight))))))))
9237
9238 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9239
9240 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9241                                                  no-highlight)
9242   "Make edits to the current article permanent."
9243   (interactive)
9244   (save-excursion
9245    ;; The buffer restriction contains the entire article if it exists.
9246     (when (article-goto-body)
9247       (let ((lines (count-lines (point) (point-max)))
9248             (length (- (point-max) (point)))
9249             (case-fold-search t)
9250             (body (copy-marker (point))))
9251         (goto-char (point-min))
9252         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9253           (delete-region (match-beginning 1) (match-end 1))
9254           (insert (number-to-string length)))
9255         (goto-char (point-min))
9256         (when (re-search-forward
9257                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9258           (delete-region (match-beginning 1) (match-end 1))
9259           (insert (number-to-string length)))
9260         (goto-char (point-min))
9261         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9262           (delete-region (match-beginning 1) (match-end 1))
9263           (insert (number-to-string lines))))))
9264   ;; Replace the article.
9265   (let ((buf (current-buffer)))
9266     (with-temp-buffer
9267       (insert-buffer-substring buf)
9268
9269       (if (and (not read-only)
9270                (not (gnus-request-replace-article
9271                      (cdr gnus-article-current) (car gnus-article-current)
9272                      (current-buffer) t)))
9273           (error "Couldn't replace article")
9274         ;; Update the summary buffer.
9275         (if (and references
9276                  (equal (message-tokenize-header references " ")
9277                         (message-tokenize-header
9278                          (or (message-fetch-field "references") "") " ")))
9279             ;; We only have to update this line.
9280             (save-excursion
9281               (save-restriction
9282                 (message-narrow-to-head)
9283                 (let ((head (buffer-string))
9284                       header)
9285                   (with-temp-buffer
9286                     (insert (format "211 %d Article retrieved.\n"
9287                                     (cdr gnus-article-current)))
9288                     (insert head)
9289                     (insert ".\n")
9290                     (let ((nntp-server-buffer (current-buffer)))
9291                       (setq header (car (gnus-get-newsgroup-headers
9292                                          nil t))))
9293                     (save-excursion
9294                       (set-buffer gnus-summary-buffer)
9295                       (gnus-data-set-header
9296                        (gnus-data-find (cdr gnus-article-current))
9297                        header)
9298                       (gnus-summary-update-article-line
9299                        (cdr gnus-article-current) header)
9300                       (if (gnus-summary-goto-subject
9301                            (cdr gnus-article-current) nil t)
9302                           (gnus-summary-update-secondary-mark
9303                            (cdr gnus-article-current))))))))
9304           ;; Update threads.
9305           (set-buffer (or buffer gnus-summary-buffer))
9306           (gnus-summary-update-article (cdr gnus-article-current))
9307           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9308               (gnus-summary-update-secondary-mark
9309                (cdr gnus-article-current))))
9310         ;; Prettify the article buffer again.
9311         (unless no-highlight
9312           (save-excursion
9313             (set-buffer gnus-article-buffer)
9314             ;;;!!! Fix this -- article should be rehighlighted.
9315             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9316             (set-buffer gnus-original-article-buffer)
9317             (gnus-request-article
9318              (cdr gnus-article-current)
9319              (car gnus-article-current) (current-buffer))))
9320         ;; Prettify the summary buffer line.
9321         (when (gnus-visual-p 'summary-highlight 'highlight)
9322           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9323
9324 (defun gnus-summary-edit-wash (key)
9325   "Perform editing command KEY in the article buffer."
9326   (interactive
9327    (list
9328     (progn
9329       (message "%s" (concat (this-command-keys) "- "))
9330       (read-char))))
9331   (message "")
9332   (gnus-summary-edit-article)
9333   (execute-kbd-macro (concat (this-command-keys) key))
9334   (gnus-article-edit-done))
9335
9336 ;;; Respooling
9337
9338 (defun gnus-summary-respool-query (&optional silent trace)
9339   "Query where the respool algorithm would put this article."
9340   (interactive)
9341   (let (gnus-mark-article-hook)
9342     (gnus-summary-select-article)
9343     (save-excursion
9344       (set-buffer gnus-original-article-buffer)
9345       (save-restriction
9346         (message-narrow-to-head)
9347         (let ((groups (nnmail-article-group 'identity trace)))
9348           (unless silent
9349             (if groups
9350                 (message "This message would go to %s"
9351                          (mapconcat 'car groups ", "))
9352               (message "This message would go to no groups"))
9353             groups))))))
9354
9355 (defun gnus-summary-respool-trace ()
9356   "Trace where the respool algorithm would put this article.
9357 Display a buffer showing all fancy splitting patterns which matched."
9358   (interactive)
9359   (gnus-summary-respool-query nil t))
9360
9361 ;; Summary marking commands.
9362
9363 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9364   "Mark articles which has the same subject as read, and then select the next.
9365 If UNMARK is positive, remove any kind of mark.
9366 If UNMARK is negative, tick articles."
9367   (interactive "P")
9368   (when unmark
9369     (setq unmark (prefix-numeric-value unmark)))
9370   (let ((count
9371          (gnus-summary-mark-same-subject
9372           (gnus-summary-article-subject) unmark)))
9373     ;; Select next unread article.  If auto-select-same mode, should
9374     ;; select the first unread article.
9375     (gnus-summary-next-article t (and gnus-auto-select-same
9376                                       (gnus-summary-article-subject)))
9377     (gnus-message 7 "%d article%s marked as %s"
9378                   count (if (= count 1) " is" "s are")
9379                   (if unmark "unread" "read"))))
9380
9381 (defun gnus-summary-kill-same-subject (&optional unmark)
9382   "Mark articles which has the same subject as read.
9383 If UNMARK is positive, remove any kind of mark.
9384 If UNMARK is negative, tick articles."
9385   (interactive "P")
9386   (when unmark
9387     (setq unmark (prefix-numeric-value unmark)))
9388   (let ((count
9389          (gnus-summary-mark-same-subject
9390           (gnus-summary-article-subject) unmark)))
9391     ;; If marked as read, go to next unread subject.
9392     (when (null unmark)
9393       ;; Go to next unread subject.
9394       (gnus-summary-next-subject 1 t))
9395     (gnus-message 7 "%d articles are marked as %s"
9396                   count (if unmark "unread" "read"))))
9397
9398 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9399   "Mark articles with same SUBJECT as read, and return marked number.
9400 If optional argument UNMARK is positive, remove any kinds of marks.
9401 If optional argument UNMARK is negative, mark articles as unread instead."
9402   (let ((count 1))
9403     (save-excursion
9404       (cond
9405        ((null unmark)                   ; Mark as read.
9406         (while (and
9407                 (progn
9408                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9409                   (gnus-summary-show-thread) t)
9410                 (gnus-summary-find-subject subject))
9411           (setq count (1+ count))))
9412        ((> unmark 0)                    ; Tick.
9413         (while (and
9414                 (progn
9415                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9416                   (gnus-summary-show-thread) t)
9417                 (gnus-summary-find-subject subject))
9418           (setq count (1+ count))))
9419        (t                               ; Mark as unread.
9420         (while (and
9421                 (progn
9422                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9423                   (gnus-summary-show-thread) t)
9424                 (gnus-summary-find-subject subject))
9425           (setq count (1+ count)))))
9426       (gnus-set-mode-line 'summary)
9427       ;; Return the number of marked articles.
9428       count)))
9429
9430 (defun gnus-summary-mark-as-processable (n &optional unmark)
9431   "Set the process mark on the next N articles.
9432 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9433 the process mark instead.  The difference between N and the actual
9434 number of articles marked is returned."
9435   (interactive "P")
9436   (if (and (null n) (gnus-region-active-p))
9437       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9438     (setq n (prefix-numeric-value n))
9439     (let ((backward (< n 0))
9440           (n (abs n)))
9441       (while (and
9442               (> n 0)
9443               (if unmark
9444                   (gnus-summary-remove-process-mark
9445                    (gnus-summary-article-number))
9446                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9447               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9448         (setq n (1- n)))
9449       (when (/= 0 n)
9450         (gnus-message 7 "No more articles"))
9451       (gnus-summary-recenter)
9452       (gnus-summary-position-point)
9453       n)))
9454
9455 (defun gnus-summary-unmark-as-processable (n)
9456   "Remove the process mark from the next N articles.
9457 If N is negative, unmark backward instead.  The difference between N and
9458 the actual number of articles unmarked is returned."
9459   (interactive "P")
9460   (gnus-summary-mark-as-processable n t))
9461
9462 (defun gnus-summary-unmark-all-processable ()
9463   "Remove the process mark from all articles."
9464   (interactive)
9465   (save-excursion
9466     (while gnus-newsgroup-processable
9467       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9468   (gnus-summary-position-point))
9469
9470 (defun gnus-summary-add-mark (article type)
9471   "Mark ARTICLE with a mark of TYPE."
9472   (let ((vtype (car (assq type gnus-article-mark-lists)))
9473         var)
9474     (if (not vtype)
9475         (error "No such mark type: %s" type)
9476       (setq var (intern (format "gnus-newsgroup-%s" type)))
9477       (set var (cons article (symbol-value var)))
9478       (if (memq type '(processable cached replied forwarded recent saved))
9479           (gnus-summary-update-secondary-mark article)
9480         ;;; !!! This is bogus.  We should find out what primary
9481         ;;; !!! mark we want to set.
9482         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9483
9484 (defun gnus-summary-mark-as-expirable (n)
9485   "Mark N articles forward as expirable.
9486 If N is negative, mark backward instead.  The difference between N and
9487 the actual number of articles marked is returned."
9488   (interactive "p")
9489   (gnus-summary-mark-forward n gnus-expirable-mark))
9490
9491 (defun gnus-summary-mark-as-spam (n)
9492   "Mark N articles forward as spam.
9493 If N is negative, mark backward instead.  The difference between N and
9494 the actual number of articles marked is returned."
9495   (interactive "p")
9496   (gnus-summary-mark-forward n gnus-spam-mark))
9497
9498 (defun gnus-summary-mark-article-as-replied (article)
9499   "Mark ARTICLE as replied to and update the summary line.
9500 ARTICLE can also be a list of articles."
9501   (interactive (list (gnus-summary-article-number)))
9502   (let ((articles (if (listp article) article (list article))))
9503     (dolist (article articles)
9504       (unless (numberp article)
9505         (error "%s is not a number" article))
9506       (push article gnus-newsgroup-replied)
9507       (let ((buffer-read-only nil))
9508         (when (gnus-summary-goto-subject article nil t)
9509           (gnus-summary-update-secondary-mark article))))))
9510
9511 (defun gnus-summary-mark-article-as-forwarded (article)
9512   "Mark ARTICLE as forwarded and update the summary line.
9513 ARTICLE can also be a list of articles."
9514   (let ((articles (if (listp article) article (list article))))
9515     (dolist (article articles)
9516       (push article gnus-newsgroup-forwarded)
9517       (let ((buffer-read-only nil))
9518         (when (gnus-summary-goto-subject article nil t)
9519           (gnus-summary-update-secondary-mark article))))))
9520
9521 (defun gnus-summary-set-bookmark (article)
9522   "Set a bookmark in current article."
9523   (interactive (list (gnus-summary-article-number)))
9524   (when (or (not (get-buffer gnus-article-buffer))
9525             (not gnus-current-article)
9526             (not gnus-article-current)
9527             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9528     (error "No current article selected"))
9529   ;; Remove old bookmark, if one exists.
9530   (gnus-pull article gnus-newsgroup-bookmarks)
9531   ;; Set the new bookmark, which is on the form
9532   ;; (article-number . line-number-in-body).
9533   (push
9534    (cons article
9535          (save-excursion
9536            (set-buffer gnus-article-buffer)
9537            (count-lines
9538             (min (point)
9539                  (save-excursion
9540                    (article-goto-body)
9541                    (point)))
9542             (point))))
9543    gnus-newsgroup-bookmarks)
9544   (gnus-message 6 "A bookmark has been added to the current article."))
9545
9546 (defun gnus-summary-remove-bookmark (article)
9547   "Remove the bookmark from the current article."
9548   (interactive (list (gnus-summary-article-number)))
9549   ;; Remove old bookmark, if one exists.
9550   (if (not (assq article gnus-newsgroup-bookmarks))
9551       (gnus-message 6 "No bookmark in current article.")
9552     (gnus-pull article gnus-newsgroup-bookmarks)
9553     (gnus-message 6 "Removed bookmark.")))
9554
9555 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9556 (defun gnus-summary-mark-as-dormant (n)
9557   "Mark N articles forward as dormant.
9558 If N is negative, mark backward instead.  The difference between N and
9559 the actual number of articles marked is returned."
9560   (interactive "p")
9561   (gnus-summary-mark-forward n gnus-dormant-mark))
9562
9563 (defun gnus-summary-set-process-mark (article)
9564   "Set the process mark on ARTICLE and update the summary line."
9565   (setq gnus-newsgroup-processable
9566         (cons article
9567               (delq article gnus-newsgroup-processable)))
9568   (when (gnus-summary-goto-subject article)
9569     (gnus-summary-show-thread)
9570     (gnus-summary-goto-subject article)
9571     (gnus-summary-update-secondary-mark article)))
9572
9573 (defun gnus-summary-remove-process-mark (article)
9574   "Remove the process mark from ARTICLE and update the summary line."
9575   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9576   (when (gnus-summary-goto-subject article)
9577     (gnus-summary-show-thread)
9578     (gnus-summary-goto-subject article)
9579     (gnus-summary-update-secondary-mark article)))
9580
9581 (defun gnus-summary-set-saved-mark (article)
9582   "Set the process mark on ARTICLE and update the summary line."
9583   (push article gnus-newsgroup-saved)
9584   (when (gnus-summary-goto-subject article)
9585     (gnus-summary-update-secondary-mark article)))
9586
9587 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9588   "Mark N articles as read forwards.
9589 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9590 The difference between N and the actual number of articles marked is
9591 returned.
9592 If NO-EXPIRE, auto-expiry will be inhibited."
9593   (interactive "p")
9594   (gnus-summary-show-thread)
9595   (let ((backward (< n 0))
9596         (gnus-summary-goto-unread
9597          (and gnus-summary-goto-unread
9598               (not (eq gnus-summary-goto-unread 'never))
9599               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9600                                     gnus-ticked-mark gnus-dormant-mark)))))
9601         (n (abs n))
9602         (mark (or mark gnus-del-mark)))
9603     (while (and (> n 0)
9604                 (gnus-summary-mark-article nil mark no-expire)
9605                 (zerop (gnus-summary-next-subject
9606                         (if backward -1 1)
9607                         (and gnus-summary-goto-unread
9608                              (not (eq gnus-summary-goto-unread 'never)))
9609                         t)))
9610       (setq n (1- n)))
9611     (when (/= 0 n)
9612       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9613     (gnus-summary-recenter)
9614     (gnus-summary-position-point)
9615     (gnus-set-mode-line 'summary)
9616     n))
9617
9618 (defun gnus-summary-mark-article-as-read (mark)
9619   "Mark the current article quickly as read with MARK."
9620   (let ((article (gnus-summary-article-number)))
9621     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9622     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9623     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9624     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9625     (push (cons article mark) gnus-newsgroup-reads)
9626     ;; Possibly remove from cache, if that is used.
9627     (when gnus-use-cache
9628       (gnus-cache-enter-remove-article article))
9629     ;; Allow the backend to change the mark.
9630     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9631     ;; Check for auto-expiry.
9632     (when (and gnus-newsgroup-auto-expire
9633                (memq mark gnus-auto-expirable-marks))
9634       (setq mark gnus-expirable-mark)
9635       ;; Let the backend know about the mark change.
9636       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9637       (push article gnus-newsgroup-expirable))
9638     ;; Set the mark in the buffer.
9639     (gnus-summary-update-mark mark 'unread)
9640     t))
9641
9642 (defun gnus-summary-mark-article-as-unread (mark)
9643   "Mark the current article quickly as unread with MARK."
9644   (let* ((article (gnus-summary-article-number))
9645          (old-mark (gnus-summary-article-mark article)))
9646     ;; Allow the backend to change the mark.
9647     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9648     (if (eq mark old-mark)
9649         t
9650       (if (<= article 0)
9651           (progn
9652             (gnus-error 1 "Can't mark negative article numbers")
9653             nil)
9654         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9655         (setq gnus-newsgroup-spam-marked
9656               (delq article gnus-newsgroup-spam-marked))
9657         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9658         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9659         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9660         (cond ((= mark gnus-ticked-mark)
9661                (setq gnus-newsgroup-marked
9662                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9663                                               article)))
9664               ((= mark gnus-spam-mark)
9665                (setq gnus-newsgroup-spam-marked
9666                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9667                                               article)))
9668               ((= mark gnus-dormant-mark)
9669                (setq gnus-newsgroup-dormant
9670                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9671                                               article)))
9672               (t
9673                (setq gnus-newsgroup-unreads
9674                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9675                                               article))))
9676         (gnus-pull article gnus-newsgroup-reads)
9677
9678         ;; See whether the article is to be put in the cache.
9679         (and gnus-use-cache
9680              (vectorp (gnus-summary-article-header article))
9681              (save-excursion
9682                (gnus-cache-possibly-enter-article
9683                 gnus-newsgroup-name article
9684                 (= mark gnus-ticked-mark)
9685                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9686
9687         ;; Fix the mark.
9688         (gnus-summary-update-mark mark 'unread)
9689         t))))
9690
9691 (defun gnus-summary-mark-article (&optional article mark no-expire)
9692   "Mark ARTICLE with MARK.  MARK can be any character.
9693 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9694 `??' (dormant) and `?E' (expirable).
9695 If MARK is nil, then the default character `?r' is used.
9696 If ARTICLE is nil, then the article on the current line will be
9697 marked.
9698 If NO-EXPIRE, auto-expiry will be inhibited."
9699   ;; The mark might be a string.
9700   (when (stringp mark)
9701     (setq mark (aref mark 0)))
9702   ;; If no mark is given, then we check auto-expiring.
9703   (when (null mark)
9704     (setq mark gnus-del-mark))
9705   (when (and (not no-expire)
9706              gnus-newsgroup-auto-expire
9707              (memq mark gnus-auto-expirable-marks))
9708     (setq mark gnus-expirable-mark))
9709   (let ((article (or article (gnus-summary-article-number)))
9710         (old-mark (gnus-summary-article-mark article)))
9711     ;; Allow the backend to change the mark.
9712     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9713     (if (eq mark old-mark)
9714         t
9715       (unless article
9716         (error "No article on current line"))
9717       (if (not (if (or (= mark gnus-unread-mark)
9718                        (= mark gnus-ticked-mark)
9719                        (= mark gnus-spam-mark)
9720                        (= mark gnus-dormant-mark))
9721                    (gnus-mark-article-as-unread article mark)
9722                  (gnus-mark-article-as-read article mark)))
9723           t
9724         ;; See whether the article is to be put in the cache.
9725         (and gnus-use-cache
9726              (not (= mark gnus-canceled-mark))
9727              (vectorp (gnus-summary-article-header article))
9728              (save-excursion
9729                (gnus-cache-possibly-enter-article
9730                 gnus-newsgroup-name article
9731                 (= mark gnus-ticked-mark)
9732                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9733
9734         (when (gnus-summary-goto-subject article nil t)
9735           (let ((buffer-read-only nil))
9736             (gnus-summary-show-thread)
9737             ;; Fix the mark.
9738             (gnus-summary-update-mark mark 'unread)
9739             t))))))
9740
9741 (defun gnus-summary-update-secondary-mark (article)
9742   "Update the secondary (read, process, cache) mark."
9743   (gnus-summary-update-mark
9744    (cond ((memq article gnus-newsgroup-processable)
9745           gnus-process-mark)
9746          ((memq article gnus-newsgroup-cached)
9747           gnus-cached-mark)
9748          ((memq article gnus-newsgroup-replied)
9749           gnus-replied-mark)
9750          ((memq article gnus-newsgroup-forwarded)
9751           gnus-forwarded-mark)
9752          ((memq article gnus-newsgroup-saved)
9753           gnus-saved-mark)
9754          ((memq article gnus-newsgroup-recent)
9755           gnus-recent-mark)
9756          ((memq article gnus-newsgroup-unseen)
9757           gnus-unseen-mark)
9758          (t gnus-no-mark))
9759    'replied)
9760   (when (gnus-visual-p 'summary-highlight 'highlight)
9761     (gnus-run-hooks 'gnus-summary-update-hook))
9762   t)
9763
9764 (defun gnus-summary-update-download-mark (article)
9765   "Update the secondary (read, process, cache) mark."
9766   (gnus-summary-update-mark
9767    (cond ((memq article gnus-newsgroup-undownloaded) 
9768           gnus-undownloaded-mark)
9769          (gnus-newsgroup-agentized
9770           gnus-downloaded-mark)
9771          (t
9772           gnus-no-mark))
9773    'download)
9774   (gnus-summary-update-line t)
9775   t)
9776
9777 (defun gnus-summary-update-mark (mark type)
9778   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9779         (buffer-read-only nil))
9780     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9781     (when forward
9782       (when (looking-at "\r")
9783         (incf forward))
9784       (when (<= (+ forward (point)) (point-max))
9785         ;; Go to the right position on the line.
9786         (goto-char (+ forward (point)))
9787         ;; Replace the old mark with the new mark.
9788         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9789         ;; Optionally update the marks by some user rule.
9790         (when (eq type 'unread)
9791           (gnus-data-set-mark
9792            (gnus-data-find (gnus-summary-article-number)) mark)
9793           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9794
9795 (defun gnus-mark-article-as-read (article &optional mark)
9796   "Enter ARTICLE in the pertinent lists and remove it from others."
9797   ;; Make the article expirable.
9798   (let ((mark (or mark gnus-del-mark)))
9799     (setq gnus-newsgroup-expirable
9800           (if (= mark gnus-expirable-mark)
9801               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9802             (delq article gnus-newsgroup-expirable)))
9803     ;; Remove from unread and marked lists.
9804     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9805     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9806     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9807     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9808     (push (cons article mark) gnus-newsgroup-reads)
9809     ;; Possibly remove from cache, if that is used.
9810     (when gnus-use-cache
9811       (gnus-cache-enter-remove-article article))
9812     t))
9813
9814 (defun gnus-mark-article-as-unread (article &optional mark)
9815   "Enter ARTICLE in the pertinent lists and remove it from others."
9816   (let ((mark (or mark gnus-ticked-mark)))
9817     (if (<= article 0)
9818         (progn
9819           (gnus-error 1 "Can't mark negative article numbers")
9820           nil)
9821       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9822             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9823             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9824             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9825             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9826
9827       ;; Unsuppress duplicates?
9828       (when gnus-suppress-duplicates
9829         (gnus-dup-unsuppress-article article))
9830
9831       (cond ((= mark gnus-ticked-mark)
9832              (setq gnus-newsgroup-marked
9833                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9834             ((= mark gnus-spam-mark)
9835              (setq gnus-newsgroup-spam-marked
9836                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9837                                             article)))
9838             ((= mark gnus-dormant-mark)
9839              (setq gnus-newsgroup-dormant
9840                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9841             (t
9842              (setq gnus-newsgroup-unreads
9843                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9844       (gnus-pull article gnus-newsgroup-reads)
9845       t)))
9846
9847 (defalias 'gnus-summary-mark-as-unread-forward
9848   'gnus-summary-tick-article-forward)
9849 (make-obsolete 'gnus-summary-mark-as-unread-forward
9850                'gnus-summary-tick-article-forward)
9851 (defun gnus-summary-tick-article-forward (n)
9852   "Tick N articles forwards.
9853 If N is negative, tick backwards instead.
9854 The difference between N and the number of articles ticked is returned."
9855   (interactive "p")
9856   (gnus-summary-mark-forward n gnus-ticked-mark))
9857
9858 (defalias 'gnus-summary-mark-as-unread-backward
9859   'gnus-summary-tick-article-backward)
9860 (make-obsolete 'gnus-summary-mark-as-unread-backward
9861                'gnus-summary-tick-article-backward)
9862 (defun gnus-summary-tick-article-backward (n)
9863   "Tick N articles backwards.
9864 The difference between N and the number of articles ticked is returned."
9865   (interactive "p")
9866   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9867
9868 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9869 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9870 (defun gnus-summary-tick-article (&optional article clear-mark)
9871   "Mark current article as unread.
9872 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9873 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9874   (interactive)
9875   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9876                                        gnus-ticked-mark)))
9877
9878 (defun gnus-summary-mark-as-read-forward (n)
9879   "Mark N articles as read forwards.
9880 If N is negative, mark backwards instead.
9881 The difference between N and the actual number of articles marked is
9882 returned."
9883   (interactive "p")
9884   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9885
9886 (defun gnus-summary-mark-as-read-backward (n)
9887   "Mark the N articles as read backwards.
9888 The difference between N and the actual number of articles marked is
9889 returned."
9890   (interactive "p")
9891   (gnus-summary-mark-forward
9892    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9893
9894 (defun gnus-summary-mark-as-read (&optional article mark)
9895   "Mark current article as read.
9896 ARTICLE specifies the article to be marked as read.
9897 MARK specifies a string to be inserted at the beginning of the line."
9898   (gnus-summary-mark-article article mark))
9899
9900 (defun gnus-summary-clear-mark-forward (n)
9901   "Clear marks from N articles forward.
9902 If N is negative, clear backward instead.
9903 The difference between N and the number of marks cleared is returned."
9904   (interactive "p")
9905   (gnus-summary-mark-forward n gnus-unread-mark))
9906
9907 (defun gnus-summary-clear-mark-backward (n)
9908   "Clear marks from N articles backward.
9909 The difference between N and the number of marks cleared is returned."
9910   (interactive "p")
9911   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9912
9913 (defun gnus-summary-mark-unread-as-read ()
9914   "Intended to be used by `gnus-summary-mark-article-hook'."
9915   (when (memq gnus-current-article gnus-newsgroup-unreads)
9916     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9917
9918 (defun gnus-summary-mark-read-and-unread-as-read ()
9919   "Intended to be used by `gnus-summary-mark-article-hook'."
9920   (let ((mark (gnus-summary-article-mark)))
9921     (when (or (gnus-unread-mark-p mark)
9922               (gnus-read-mark-p mark))
9923       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9924
9925 (defun gnus-summary-mark-unread-as-ticked ()
9926   "Intended to be used by `gnus-summary-mark-article-hook'."
9927   (when (memq gnus-current-article gnus-newsgroup-unreads)
9928     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9929
9930 (defun gnus-summary-mark-region-as-read (point mark all)
9931   "Mark all unread articles between point and mark as read.
9932 If given a prefix, mark all articles between point and mark as read,
9933 even ticked and dormant ones."
9934   (interactive "r\nP")
9935   (save-excursion
9936     (let (article)
9937       (goto-char point)
9938       (beginning-of-line)
9939       (while (and
9940               (< (point) mark)
9941               (progn
9942                 (when (or all
9943                           (memq (setq article (gnus-summary-article-number))
9944                                 gnus-newsgroup-unreads))
9945                   (gnus-summary-mark-article article gnus-del-mark))
9946                 t)
9947               (gnus-summary-find-next))))))
9948
9949 (defun gnus-summary-mark-below (score mark)
9950   "Mark articles with score less than SCORE with MARK."
9951   (interactive "P\ncMark: ")
9952   (setq score (if score
9953                   (prefix-numeric-value score)
9954                 (or gnus-summary-default-score 0)))
9955   (save-excursion
9956     (set-buffer gnus-summary-buffer)
9957     (goto-char (point-min))
9958     (while
9959         (progn
9960           (and (< (gnus-summary-article-score) score)
9961                (gnus-summary-mark-article nil mark))
9962           (gnus-summary-find-next)))))
9963
9964 (defun gnus-summary-kill-below (&optional score)
9965   "Mark articles with score below SCORE as read."
9966   (interactive "P")
9967   (gnus-summary-mark-below score gnus-killed-mark))
9968
9969 (defun gnus-summary-clear-above (&optional score)
9970   "Clear all marks from articles with score above SCORE."
9971   (interactive "P")
9972   (gnus-summary-mark-above score gnus-unread-mark))
9973
9974 (defun gnus-summary-tick-above (&optional score)
9975   "Tick all articles with score above SCORE."
9976   (interactive "P")
9977   (gnus-summary-mark-above score gnus-ticked-mark))
9978
9979 (defun gnus-summary-mark-above (score mark)
9980   "Mark articles with score over SCORE with MARK."
9981   (interactive "P\ncMark: ")
9982   (setq score (if score
9983                   (prefix-numeric-value score)
9984                 (or gnus-summary-default-score 0)))
9985   (save-excursion
9986     (set-buffer gnus-summary-buffer)
9987     (goto-char (point-min))
9988     (while (and (progn
9989                   (when (> (gnus-summary-article-score) score)
9990                     (gnus-summary-mark-article nil mark))
9991                   t)
9992                 (gnus-summary-find-next)))))
9993
9994 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9995 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9996 (defun gnus-summary-limit-include-expunged (&optional no-error)
9997   "Display all the hidden articles that were expunged for low scores."
9998   (interactive)
9999   (let ((buffer-read-only nil))
10000     (let ((scored gnus-newsgroup-scored)
10001           headers h)
10002       (while scored
10003         (unless (gnus-summary-article-header (caar scored))
10004           (and (setq h (gnus-number-to-header (caar scored)))
10005                (< (cdar scored) gnus-summary-expunge-below)
10006                (push h headers)))
10007         (setq scored (cdr scored)))
10008       (if (not headers)
10009           (when (not no-error)
10010             (error "No expunged articles hidden"))
10011         (goto-char (point-min))
10012         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10013         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10014         (mapcar (lambda (x) (push (mail-header-number x)
10015                                   gnus-newsgroup-limit))
10016                 headers)
10017         (gnus-summary-prepare-unthreaded (nreverse headers))
10018         (goto-char (point-min))
10019         (gnus-summary-position-point)
10020         t))))
10021
10022 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10023   "Mark all unread articles in this newsgroup as read.
10024 If prefix argument ALL is non-nil, ticked and dormant articles will
10025 also be marked as read.
10026 If QUIETLY is non-nil, no questions will be asked.
10027 If TO-HERE is non-nil, it should be a point in the buffer.  All
10028 articles before (after, if REVERSE is set) this point will be marked as read.
10029 Note that this function will only catch up the unread article
10030 in the current summary buffer limitation.
10031 The number of articles marked as read is returned."
10032   (interactive "P")
10033   (prog1
10034       (save-excursion
10035         (when (or quietly
10036                   (not gnus-interactive-catchup) ;Without confirmation?
10037                   gnus-expert-user
10038                   (gnus-y-or-n-p
10039                    (if all
10040                        "Mark absolutely all articles as read? "
10041                      "Mark all unread articles as read? ")))
10042           (if (and not-mark
10043                    (not gnus-newsgroup-adaptive)
10044                    (not gnus-newsgroup-auto-expire)
10045                    (not gnus-suppress-duplicates)
10046                    (or (not gnus-use-cache)
10047                        (eq gnus-use-cache 'passive)))
10048               (progn
10049                 (when all
10050                   (setq gnus-newsgroup-marked nil
10051                         gnus-newsgroup-spam-marked nil
10052                         gnus-newsgroup-dormant nil))
10053                 (setq gnus-newsgroup-unreads
10054                       (gnus-sorted-nunion
10055                        (gnus-intersection gnus-newsgroup-unreads
10056                                           gnus-newsgroup-downloadable)
10057                        gnus-newsgroup-unfetched)))
10058             ;; We actually mark all articles as canceled, which we
10059             ;; have to do when using auto-expiry or adaptive scoring.
10060             (gnus-summary-show-all-threads)
10061             (if (and to-here reverse)
10062                 (progn
10063                   (goto-char to-here)
10064                   (while (and
10065                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
10066                           (gnus-summary-find-next (not all)))))
10067               (when (gnus-summary-first-subject (not all))
10068                 (while (and
10069                         (if to-here (< (point) to-here) t)
10070                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10071                         (gnus-summary-find-next (not all))))))
10072             (gnus-set-mode-line 'summary))
10073           t))
10074     (gnus-summary-position-point)))
10075
10076 (defun gnus-summary-catchup-to-here (&optional all)
10077   "Mark all unticked articles before the current one as read.
10078 If ALL is non-nil, also mark ticked and dormant articles as read."
10079   (interactive "P")
10080   (save-excursion
10081     (gnus-save-hidden-threads
10082       (let ((beg (point)))
10083         ;; We check that there are unread articles.
10084         (when (or all (gnus-summary-find-prev))
10085           (gnus-summary-catchup all t beg)))))
10086   (gnus-summary-position-point))
10087
10088 (defun gnus-summary-catchup-from-here (&optional all)
10089   "Mark all unticked articles after the current one as read.
10090 If ALL is non-nil, also mark ticked and dormant articles as read."
10091   (interactive "P")
10092   (save-excursion
10093     (gnus-save-hidden-threads
10094       (let ((beg (point)))
10095         ;; We check that there are unread articles.
10096         (when (or all (gnus-summary-find-next))
10097           (gnus-summary-catchup all t beg nil t)))))
10098
10099   (gnus-summary-position-point))
10100 (defun gnus-summary-catchup-all (&optional quietly)
10101   "Mark all articles in this newsgroup as read.
10102 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10103 instead, which marks only unread articles as read."
10104   (interactive "P")
10105   (gnus-summary-catchup t quietly))
10106
10107 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10108   "Mark all unread articles in this group as read, then exit.
10109 If prefix argument ALL is non-nil, all articles are marked as read.
10110 If QUIETLY is non-nil, no questions will be asked."
10111   (interactive "P")
10112   (when (gnus-summary-catchup all quietly nil 'fast)
10113     ;; Select next newsgroup or exit.
10114     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10115              (eq gnus-auto-select-next 'quietly))
10116         (gnus-summary-next-group nil)
10117       (gnus-summary-exit))))
10118
10119 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10120   "Mark all articles in this newsgroup as read, and then exit.
10121 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10122 instead, which marks only unread articles as read."
10123   (interactive "P")
10124   (gnus-summary-catchup-and-exit t quietly))
10125
10126 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10127   "Mark all articles in this group as read and select the next group.
10128 If given a prefix, mark all articles, unread as well as ticked, as
10129 read."
10130   (interactive "P")
10131   (save-excursion
10132     (gnus-summary-catchup all))
10133   (gnus-summary-next-group))
10134
10135 ;;;
10136 ;;; with article
10137 ;;;
10138
10139 (defmacro gnus-with-article (article &rest forms)
10140   "Select ARTICLE and perform FORMS in the original article buffer.
10141 Then replace the article with the result."
10142   `(progn
10143      ;; We don't want the article to be marked as read.
10144      (let (gnus-mark-article-hook)
10145        (gnus-summary-select-article t t nil ,article))
10146      (set-buffer gnus-original-article-buffer)
10147      ,@forms
10148      (if (not (gnus-check-backend-function
10149                'request-replace-article (car gnus-article-current)))
10150          (gnus-message 5 "Read-only group; not replacing")
10151        (unless (gnus-request-replace-article
10152                 ,article (car gnus-article-current)
10153                 (current-buffer) t)
10154          (error "Couldn't replace article")))
10155      ;; The cache and backlog have to be flushed somewhat.
10156      (when gnus-keep-backlog
10157        (gnus-backlog-remove-article
10158         (car gnus-article-current) (cdr gnus-article-current)))
10159      (when gnus-use-cache
10160        (gnus-cache-update-article
10161         (car gnus-article-current) (cdr gnus-article-current)))))
10162
10163 (put 'gnus-with-article 'lisp-indent-function 1)
10164 (put 'gnus-with-article 'edebug-form-spec '(form body))
10165
10166 ;; Thread-based commands.
10167
10168 (defun gnus-summary-articles-in-thread (&optional article)
10169   "Return a list of all articles in the current thread.
10170 If ARTICLE is non-nil, return all articles in the thread that starts
10171 with that article."
10172   (let* ((article (or article (gnus-summary-article-number)))
10173          (data (gnus-data-find-list article))
10174          (top-level (gnus-data-level (car data)))
10175          (top-subject
10176           (cond ((null gnus-thread-operation-ignore-subject)
10177                  (gnus-simplify-subject-re
10178                   (mail-header-subject (gnus-data-header (car data)))))
10179                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10180                  (gnus-simplify-subject-fuzzy
10181                   (mail-header-subject (gnus-data-header (car data)))))
10182                 (t nil)))
10183          (end-point (save-excursion
10184                       (if (gnus-summary-go-to-next-thread)
10185                           (point) (point-max))))
10186          articles)
10187     (while (and data
10188                 (< (gnus-data-pos (car data)) end-point))
10189       (when (or (not top-subject)
10190                 (string= top-subject
10191                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10192                              (gnus-simplify-subject-fuzzy
10193                               (mail-header-subject
10194                                (gnus-data-header (car data))))
10195                            (gnus-simplify-subject-re
10196                             (mail-header-subject
10197                              (gnus-data-header (car data)))))))
10198         (push (gnus-data-number (car data)) articles))
10199       (unless (and (setq data (cdr data))
10200                    (> (gnus-data-level (car data)) top-level))
10201         (setq data nil)))
10202     ;; Return the list of articles.
10203     (nreverse articles)))
10204
10205 (defun gnus-summary-rethread-current ()
10206   "Rethread the thread the current article is part of."
10207   (interactive)
10208   (let* ((gnus-show-threads t)
10209          (article (gnus-summary-article-number))
10210          (id (mail-header-id (gnus-summary-article-header)))
10211          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10212     (unless id
10213       (error "No article on the current line"))
10214     (gnus-rebuild-thread id)
10215     (gnus-summary-goto-subject article)))
10216
10217 (defun gnus-summary-reparent-thread ()
10218   "Make the current article child of the marked (or previous) article.
10219
10220 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10221 is non-nil or the Subject: of both articles are the same."
10222   (interactive)
10223   (unless (not (gnus-group-read-only-p))
10224     (error "The current newsgroup does not support article editing"))
10225   (unless (<= (length gnus-newsgroup-processable) 1)
10226     (error "No more than one article may be marked"))
10227   (save-window-excursion
10228     (let ((gnus-article-buffer " *reparent*")
10229           (current-article (gnus-summary-article-number))
10230           ;; First grab the marked article, otherwise one line up.
10231           (parent-article (if (not (null gnus-newsgroup-processable))
10232                               (car gnus-newsgroup-processable)
10233                             (save-excursion
10234                               (if (eq (forward-line -1) 0)
10235                                   (gnus-summary-article-number)
10236                                 (error "Beginning of summary buffer"))))))
10237       (unless (not (eq current-article parent-article))
10238         (error "An article may not be self-referential"))
10239       (let ((message-id (mail-header-id
10240                          (gnus-summary-article-header parent-article))))
10241         (unless (and message-id (not (equal message-id "")))
10242           (error "No message-id in desired parent"))
10243         (gnus-with-article current-article
10244           (save-restriction
10245             (goto-char (point-min))
10246             (message-narrow-to-head)
10247             (if (re-search-forward "^References: " nil t)
10248                 (progn
10249                   (re-search-forward "^[^ \t]" nil t)
10250                   (forward-line -1)
10251                   (end-of-line)
10252                   (insert " " message-id))
10253               (insert "References: " message-id "\n"))))
10254         (set-buffer gnus-summary-buffer)
10255         (gnus-summary-unmark-all-processable)
10256         (gnus-summary-update-article current-article)
10257         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10258             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10259         (gnus-summary-rethread-current)
10260         (gnus-message 3 "Article %d is now the child of article %d"
10261                       current-article parent-article)))))
10262
10263 (defun gnus-summary-toggle-threads (&optional arg)
10264   "Toggle showing conversation threads.
10265 If ARG is positive number, turn showing conversation threads on."
10266   (interactive "P")
10267   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10268     (setq gnus-show-threads
10269           (if (null arg) (not gnus-show-threads)
10270             (> (prefix-numeric-value arg) 0)))
10271     (gnus-summary-prepare)
10272     (gnus-summary-goto-subject current)
10273     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10274     (gnus-summary-position-point)))
10275
10276 (defun gnus-summary-show-all-threads ()
10277   "Show all threads."
10278   (interactive)
10279   (save-excursion
10280     (let ((buffer-read-only nil))
10281       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10282   (gnus-summary-position-point))
10283
10284 (defun gnus-summary-show-thread ()
10285   "Show thread subtrees.
10286 Returns nil if no thread was there to be shown."
10287   (interactive)
10288   (let ((buffer-read-only nil)
10289         (orig (point))
10290         ;; first goto end then to beg, to have point at beg after let
10291         (end (progn (end-of-line) (point)))
10292         (beg (progn (beginning-of-line) (point))))
10293     (prog1
10294         ;; Any hidden lines here?
10295         (search-forward "\r" end t)
10296       (subst-char-in-region beg end ?\^M ?\n t)
10297       (goto-char orig)
10298       (gnus-summary-position-point))))
10299
10300 (defun gnus-summary-maybe-hide-threads ()
10301   "If requested, hide the threads that should be hidden."
10302   (when (and gnus-show-threads
10303              gnus-thread-hide-subtree)
10304     (gnus-summary-hide-all-threads
10305      (if (or (consp gnus-thread-hide-subtree)
10306              (gnus-functionp gnus-thread-hide-subtree))
10307          (gnus-make-predicate gnus-thread-hide-subtree)
10308        nil))))
10309
10310 ;;; Hiding predicates.
10311
10312 (defun gnus-article-unread-p (header)
10313   (memq (mail-header-number header) gnus-newsgroup-unreads))
10314
10315 (defun gnus-article-unseen-p (header)
10316   (memq (mail-header-number header) gnus-newsgroup-unseen))
10317
10318 (defun gnus-map-articles (predicate articles)
10319   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10320   (apply 'gnus-or (mapcar predicate
10321                           (mapcar 'gnus-summary-article-header articles))))
10322
10323 (defun gnus-summary-hide-all-threads (&optional predicate)
10324   "Hide all thread subtrees.
10325 If PREDICATE is supplied, threads that satisfy this predicate
10326 will not be hidden."
10327   (interactive)
10328   (save-excursion
10329     (goto-char (point-min))
10330     (let ((end nil))
10331       (while (not end)
10332         (when (or (not predicate)
10333                   (gnus-map-articles
10334                    predicate (gnus-summary-article-children)))
10335             (gnus-summary-hide-thread))
10336         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10337   (gnus-summary-position-point))
10338
10339 (defun gnus-summary-hide-thread ()
10340   "Hide thread subtrees.
10341 If PREDICATE is supplied, threads that satisfy this predicate
10342 will not be hidden.
10343 Returns nil if no threads were there to be hidden."
10344   (interactive)
10345   (let ((buffer-read-only nil)
10346         (start (point))
10347         (article (gnus-summary-article-number)))
10348     (goto-char start)
10349     ;; Go forward until either the buffer ends or the subthread
10350     ;; ends.
10351     (when (and (not (eobp))
10352                (or (zerop (gnus-summary-next-thread 1 t))
10353                    (goto-char (point-max))))
10354       (prog1
10355           (if (and (> (point) start)
10356                    (search-backward "\n" start t))
10357               (progn
10358                 (subst-char-in-region start (point) ?\n ?\^M)
10359                 (gnus-summary-goto-subject article))
10360             (goto-char start)
10361             nil)))))
10362
10363 (defun gnus-summary-go-to-next-thread (&optional previous)
10364   "Go to the same level (or less) next thread.
10365 If PREVIOUS is non-nil, go to previous thread instead.
10366 Return the article number moved to, or nil if moving was impossible."
10367   (let ((level (gnus-summary-thread-level))
10368         (way (if previous -1 1))
10369         (beg (point)))
10370     (forward-line way)
10371     (while (and (not (eobp))
10372                 (< level (gnus-summary-thread-level)))
10373       (forward-line way))
10374     (if (eobp)
10375         (progn
10376           (goto-char beg)
10377           nil)
10378       (setq beg (point))
10379       (prog1
10380           (gnus-summary-article-number)
10381         (goto-char beg)))))
10382
10383 (defun gnus-summary-next-thread (n &optional silent)
10384   "Go to the same level next N'th thread.
10385 If N is negative, search backward instead.
10386 Returns the difference between N and the number of skips actually
10387 done.
10388
10389 If SILENT, don't output messages."
10390   (interactive "p")
10391   (let ((backward (< n 0))
10392         (n (abs n)))
10393     (while (and (> n 0)
10394                 (gnus-summary-go-to-next-thread backward))
10395       (decf n))
10396     (unless silent
10397       (gnus-summary-position-point))
10398     (when (and (not silent) (/= 0 n))
10399       (gnus-message 7 "No more threads"))
10400     n))
10401
10402 (defun gnus-summary-prev-thread (n)
10403   "Go to the same level previous N'th thread.
10404 Returns the difference between N and the number of skips actually
10405 done."
10406   (interactive "p")
10407   (gnus-summary-next-thread (- n)))
10408
10409 (defun gnus-summary-go-down-thread ()
10410   "Go down one level in the current thread."
10411   (let ((children (gnus-summary-article-children)))
10412     (when children
10413       (gnus-summary-goto-subject (car children)))))
10414
10415 (defun gnus-summary-go-up-thread ()
10416   "Go up one level in the current thread."
10417   (let ((parent (gnus-summary-article-parent)))
10418     (when parent
10419       (gnus-summary-goto-subject parent))))
10420
10421 (defun gnus-summary-down-thread (n)
10422   "Go down thread N steps.
10423 If N is negative, go up instead.
10424 Returns the difference between N and how many steps down that were
10425 taken."
10426   (interactive "p")
10427   (let ((up (< n 0))
10428         (n (abs n)))
10429     (while (and (> n 0)
10430                 (if up (gnus-summary-go-up-thread)
10431                   (gnus-summary-go-down-thread)))
10432       (setq n (1- n)))
10433     (gnus-summary-position-point)
10434     (when (/= 0 n)
10435       (gnus-message 7 "Can't go further"))
10436     n))
10437
10438 (defun gnus-summary-up-thread (n)
10439   "Go up thread N steps.
10440 If N is negative, go down instead.
10441 Returns the difference between N and how many steps down that were
10442 taken."
10443   (interactive "p")
10444   (gnus-summary-down-thread (- n)))
10445
10446 (defun gnus-summary-top-thread ()
10447   "Go to the top of the thread."
10448   (interactive)
10449   (while (gnus-summary-go-up-thread))
10450   (gnus-summary-article-number))
10451
10452 (defun gnus-summary-kill-thread (&optional unmark)
10453   "Mark articles under current thread as read.
10454 If the prefix argument is positive, remove any kinds of marks.
10455 If the prefix argument is negative, tick articles instead."
10456   (interactive "P")
10457   (when unmark
10458     (setq unmark (prefix-numeric-value unmark)))
10459   (let ((articles (gnus-summary-articles-in-thread)))
10460     (save-excursion
10461       ;; Expand the thread.
10462       (gnus-summary-show-thread)
10463       ;; Mark all the articles.
10464       (while articles
10465         (gnus-summary-goto-subject (car articles))
10466         (cond ((null unmark)
10467                (gnus-summary-mark-article-as-read gnus-killed-mark))
10468               ((> unmark 0)
10469                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10470               (t
10471                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10472         (setq articles (cdr articles))))
10473     ;; Hide killed subtrees.
10474     (and (null unmark)
10475          gnus-thread-hide-killed
10476          (gnus-summary-hide-thread))
10477     ;; If marked as read, go to next unread subject.
10478     (when (null unmark)
10479       ;; Go to next unread subject.
10480       (gnus-summary-next-subject 1 t)))
10481   (gnus-set-mode-line 'summary))
10482
10483 ;; Summary sorting commands
10484
10485 (defun gnus-summary-sort-by-number (&optional reverse)
10486   "Sort the summary buffer by article number.
10487 Argument REVERSE means reverse order."
10488   (interactive "P")
10489   (gnus-summary-sort 'number reverse))
10490
10491 (defun gnus-summary-sort-by-random (&optional reverse)
10492   "Randomize the order in the summary buffer.
10493 Argument REVERSE means to randomize in reverse order."
10494   (interactive "P")
10495   (gnus-summary-sort 'random reverse))
10496
10497 (defun gnus-summary-sort-by-author (&optional reverse)
10498   "Sort the summary buffer by author name alphabetically.
10499 If `case-fold-search' is non-nil, case of letters is ignored.
10500 Argument REVERSE means reverse order."
10501   (interactive "P")
10502   (gnus-summary-sort 'author reverse))
10503
10504 (defun gnus-summary-sort-by-subject (&optional reverse)
10505   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10506 If `case-fold-search' is non-nil, case of letters is ignored.
10507 Argument REVERSE means reverse order."
10508   (interactive "P")
10509   (gnus-summary-sort 'subject reverse))
10510
10511 (defun gnus-summary-sort-by-date (&optional reverse)
10512   "Sort the summary buffer by date.
10513 Argument REVERSE means reverse order."
10514   (interactive "P")
10515   (gnus-summary-sort 'date reverse))
10516
10517 (defun gnus-summary-sort-by-score (&optional reverse)
10518   "Sort the summary buffer by score.
10519 Argument REVERSE means reverse order."
10520   (interactive "P")
10521   (gnus-summary-sort 'score reverse))
10522
10523 (defun gnus-summary-sort-by-lines (&optional reverse)
10524   "Sort the summary buffer by the number of lines.
10525 Argument REVERSE means reverse order."
10526   (interactive "P")
10527   (gnus-summary-sort 'lines reverse))
10528
10529 (defun gnus-summary-sort-by-chars (&optional reverse)
10530   "Sort the summary buffer by article length.
10531 Argument REVERSE means reverse order."
10532   (interactive "P")
10533   (gnus-summary-sort 'chars reverse))
10534
10535 (defun gnus-summary-sort-by-original (&optional reverse)
10536   "Sort the summary buffer using the default sorting method.
10537 Argument REVERSE means reverse order."
10538   (interactive "P")
10539   (let* ((buffer-read-only)
10540          (gnus-summary-prepare-hook nil))
10541     ;; We do the sorting by regenerating the threads.
10542     (gnus-summary-prepare)
10543     ;; Hide subthreads if needed.
10544     (gnus-summary-maybe-hide-threads)))
10545
10546 (defun gnus-summary-sort (predicate reverse)
10547   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10548   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10549          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10550          (gnus-thread-sort-functions
10551           (if (not reverse)
10552               thread
10553             `(lambda (t1 t2)
10554                (,thread t2 t1))))
10555          (gnus-sort-gathered-threads-function
10556           gnus-thread-sort-functions)
10557          (gnus-article-sort-functions
10558           (if (not reverse)
10559               article
10560             `(lambda (t1 t2)
10561                (,article t2 t1))))
10562          (buffer-read-only)
10563          (gnus-summary-prepare-hook nil))
10564     ;; We do the sorting by regenerating the threads.
10565     (gnus-summary-prepare)
10566     ;; Hide subthreads if needed.
10567     (gnus-summary-maybe-hide-threads)))
10568
10569 ;; Summary saving commands.
10570
10571 (defun gnus-summary-save-article (&optional n not-saved)
10572   "Save the current article using the default saver function.
10573 If N is a positive number, save the N next articles.
10574 If N is a negative number, save the N previous articles.
10575 If N is nil and any articles have been marked with the process mark,
10576 save those articles instead.
10577 The variable `gnus-default-article-saver' specifies the saver function."
10578   (interactive "P")
10579   (let* ((articles (gnus-summary-work-articles n))
10580          (save-buffer (save-excursion
10581                         (nnheader-set-temp-buffer " *Gnus Save*")))
10582          (num (length articles))
10583          header file)
10584     (dolist (article articles)
10585       (setq header (gnus-summary-article-header article))
10586       (if (not (vectorp header))
10587           ;; This is a pseudo-article.
10588           (if (assq 'name header)
10589               (gnus-copy-file (cdr (assq 'name header)))
10590             (gnus-message 1 "Article %d is unsaveable" article))
10591         ;; This is a real article.
10592         (save-window-excursion
10593           (let ((gnus-display-mime-function nil)
10594                 (gnus-article-prepare-hook nil))
10595             (gnus-summary-select-article t nil nil article)))
10596         (save-excursion
10597           (set-buffer save-buffer)
10598           (erase-buffer)
10599           (insert-buffer-substring gnus-original-article-buffer))
10600         (setq file (gnus-article-save save-buffer file num))
10601         (gnus-summary-remove-process-mark article)
10602         (unless not-saved
10603           (gnus-summary-set-saved-mark article))))
10604     (gnus-kill-buffer save-buffer)
10605     (gnus-summary-position-point)
10606     (gnus-set-mode-line 'summary)
10607     n))
10608
10609 (defun gnus-summary-pipe-output (&optional arg headers)
10610   "Pipe the current article to a subprocess.
10611 If N is a positive number, pipe the N next articles.
10612 If N is a negative number, pipe the N previous articles.
10613 If N is nil and any articles have been marked with the process mark,
10614 pipe those articles instead.
10615 If HEADERS (the symbolic prefix), include the headers, too."
10616   (interactive (gnus-interactive "P\ny"))
10617   (require 'gnus-art)
10618   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10619         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10620     (gnus-summary-save-article arg t))
10621   (let ((buffer (get-buffer "*Shell Command Output*")))
10622     (when (and buffer
10623                (not (zerop (buffer-size buffer))))
10624       (gnus-configure-windows 'pipe))))
10625
10626 (defun gnus-summary-save-article-mail (&optional arg)
10627   "Append the current article to an mail file.
10628 If N is a positive number, save the N next articles.
10629 If N is a negative number, save the N previous articles.
10630 If N is nil and any articles have been marked with the process mark,
10631 save those articles instead."
10632   (interactive "P")
10633   (require 'gnus-art)
10634   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10635     (gnus-summary-save-article arg)))
10636
10637 (defun gnus-summary-save-article-rmail (&optional arg)
10638   "Append the current article to an rmail file.
10639 If N is a positive number, save the N next articles.
10640 If N is a negative number, save the N previous articles.
10641 If N is nil and any articles have been marked with the process mark,
10642 save those articles instead."
10643   (interactive "P")
10644   (require 'gnus-art)
10645   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10646     (gnus-summary-save-article arg)))
10647
10648 (defun gnus-summary-save-article-file (&optional arg)
10649   "Append the current article to a file.
10650 If N is a positive number, save the N next articles.
10651 If N is a negative number, save the N previous articles.
10652 If N is nil and any articles have been marked with the process mark,
10653 save those articles instead."
10654   (interactive "P")
10655   (require 'gnus-art)
10656   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10657     (gnus-summary-save-article arg)))
10658
10659 (defun gnus-summary-write-article-file (&optional arg)
10660   "Write the current article to a file, deleting the previous file.
10661 If N is a positive number, save the N next articles.
10662 If N is a negative number, save the N previous articles.
10663 If N is nil and any articles have been marked with the process mark,
10664 save those articles instead."
10665   (interactive "P")
10666   (require 'gnus-art)
10667   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10668     (gnus-summary-save-article arg)))
10669
10670 (defun gnus-summary-save-article-body-file (&optional arg)
10671   "Append the current article body to a file.
10672 If N is a positive number, save the N next articles.
10673 If N is a negative number, save the N previous articles.
10674 If N is nil and any articles have been marked with the process mark,
10675 save those articles instead."
10676   (interactive "P")
10677   (require 'gnus-art)
10678   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10679     (gnus-summary-save-article arg)))
10680
10681 (defun gnus-summary-muttprint (&optional arg)
10682   "Print the current article using Muttprint.
10683 If N is a positive number, save the N next articles.
10684 If N is a negative number, save the N previous articles.
10685 If N is nil and any articles have been marked with the process mark,
10686 save those articles instead."
10687   (interactive "P")
10688   (require 'gnus-art)
10689   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10690     (gnus-summary-save-article arg t)))
10691
10692 (defun gnus-summary-pipe-message (program)
10693   "Pipe the current article through PROGRAM."
10694   (interactive "sProgram: ")
10695   (gnus-summary-select-article)
10696   (let ((mail-header-separator ""))
10697     (gnus-eval-in-buffer-window gnus-article-buffer
10698       (save-restriction
10699         (widen)
10700         (let ((start (window-start))
10701               buffer-read-only)
10702           (message-pipe-buffer-body program)
10703           (set-window-start (get-buffer-window (current-buffer)) start))))))
10704
10705 (defun gnus-get-split-value (methods)
10706   "Return a value based on the split METHODS."
10707   (let (split-name method result match)
10708     (when methods
10709       (save-excursion
10710         (set-buffer gnus-original-article-buffer)
10711         (save-restriction
10712           (nnheader-narrow-to-headers)
10713           (while (and methods (not split-name))
10714             (goto-char (point-min))
10715             (setq method (pop methods))
10716             (setq match (car method))
10717             (when (cond
10718                    ((stringp match)
10719                     ;; Regular expression.
10720                     (ignore-errors
10721                       (re-search-forward match nil t)))
10722                    ((gnus-functionp match)
10723                     ;; Function.
10724                     (save-restriction
10725                       (widen)
10726                       (setq result (funcall match gnus-newsgroup-name))))
10727                    ((consp match)
10728                     ;; Form.
10729                     (save-restriction
10730                       (widen)
10731                       (setq result (eval match)))))
10732               (setq split-name (cdr method))
10733               (cond ((stringp result)
10734                      (push (expand-file-name
10735                             result gnus-article-save-directory)
10736                            split-name))
10737                     ((consp result)
10738                      (setq split-name (append result split-name)))))))))
10739     (nreverse split-name)))
10740
10741 (defun gnus-valid-move-group-p (group)
10742   (and (boundp group)
10743        (symbol-name group)
10744        (symbol-value group)
10745        (gnus-get-function (gnus-find-method-for-group
10746                            (symbol-name group)) 'request-accept-article t)))
10747
10748 (defun gnus-read-move-group-name (prompt default articles prefix)
10749   "Read a group name."
10750   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10751          (minibuffer-confirm-incomplete nil) ; XEmacs
10752          (prom
10753           (format "%s %s to:"
10754                   prompt
10755                   (if (> (length articles) 1)
10756                       (format "these %d articles" (length articles))
10757                     "this article")))
10758          (to-newsgroup
10759           (cond
10760            ((null split-name)
10761             (gnus-completing-read-with-default
10762              default prom
10763              gnus-active-hashtb
10764              'gnus-valid-move-group-p
10765              nil prefix
10766              'gnus-group-history))
10767            ((= 1 (length split-name))
10768             (gnus-completing-read-with-default
10769              (car split-name) prom
10770              gnus-active-hashtb
10771              'gnus-valid-move-group-p
10772              nil nil
10773              'gnus-group-history))
10774            (t
10775             (gnus-completing-read-with-default
10776              nil prom
10777              (mapcar (lambda (el) (list el))
10778                      (nreverse split-name))
10779              nil nil nil
10780              'gnus-group-history))))
10781          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10782     (when to-newsgroup
10783       (if (or (string= to-newsgroup "")
10784               (string= to-newsgroup prefix))
10785           (setq to-newsgroup default))
10786       (unless to-newsgroup
10787         (error "No group name entered"))
10788       (or (gnus-active to-newsgroup)
10789           (gnus-activate-group to-newsgroup nil nil to-method)
10790           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10791                                      to-newsgroup))
10792               (or (and (gnus-request-create-group to-newsgroup to-method)
10793                        (gnus-activate-group
10794                         to-newsgroup nil nil to-method)
10795                        (gnus-subscribe-group to-newsgroup))
10796                   (error "Couldn't create group %s" to-newsgroup)))
10797           (error "No such group: %s" to-newsgroup)))
10798     to-newsgroup))
10799
10800 (defun gnus-summary-save-parts (type dir n &optional reverse)
10801   "Save parts matching TYPE to DIR.
10802 If REVERSE, save parts that do not match TYPE."
10803   (interactive
10804    (list (read-string "Save parts of type: "
10805                       (or (car gnus-summary-save-parts-type-history)
10806                           gnus-summary-save-parts-default-mime)
10807                       'gnus-summary-save-parts-type-history)
10808          (setq gnus-summary-save-parts-last-directory
10809                (read-file-name "Save to directory: "
10810                                gnus-summary-save-parts-last-directory
10811                                nil t))
10812          current-prefix-arg))
10813   (gnus-summary-iterate n
10814     (let ((gnus-display-mime-function nil)
10815           (gnus-inhibit-treatment t))
10816       (gnus-summary-select-article))
10817     (save-excursion
10818       (set-buffer gnus-article-buffer)
10819       (let ((handles (or gnus-article-mime-handles
10820                          (mm-dissect-buffer nil gnus-article-loose-mime)
10821                          (and gnus-article-emulate-mime
10822                               (mm-uu-dissect)))))
10823         (when handles
10824           (gnus-summary-save-parts-1 type dir handles reverse)
10825           (unless gnus-article-mime-handles ;; Don't destroy this case.
10826             (mm-destroy-parts handles)))))))
10827
10828 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10829   (if (stringp (car handle))
10830       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10831               (cdr handle))
10832     (when (if reverse
10833               (not (string-match type (mm-handle-media-type handle)))
10834             (string-match type (mm-handle-media-type handle)))
10835       (let ((file (expand-file-name
10836                    (file-name-nondirectory
10837                     (or
10838                      (mail-content-type-get
10839                       (mm-handle-disposition handle) 'filename)
10840                      (concat gnus-newsgroup-name
10841                              "." (number-to-string
10842                                   (cdr gnus-article-current)))))
10843                    dir)))
10844         (unless (file-exists-p file)
10845           (mm-save-part-to-file handle file))))))
10846
10847 ;; Summary extract commands
10848
10849 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10850   (let ((buffer-read-only nil)
10851         (article (gnus-summary-article-number))
10852         after-article b e)
10853     (unless (gnus-summary-goto-subject article)
10854       (error "No such article: %d" article))
10855     (gnus-summary-position-point)
10856     ;; If all commands are to be bunched up on one line, we collect
10857     ;; them here.
10858     (unless gnus-view-pseudos-separately
10859       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10860             files action)
10861         (while ps
10862           (setq action (cdr (assq 'action (car ps))))
10863           (setq files (list (cdr (assq 'name (car ps)))))
10864           (while (and ps (cdr ps)
10865                       (string= (or action "1")
10866                                (or (cdr (assq 'action (cadr ps))) "2")))
10867             (push (cdr (assq 'name (cadr ps))) files)
10868             (setcdr ps (cddr ps)))
10869           (when files
10870             (when (not (string-match "%s" action))
10871               (push " " files))
10872             (push " " files)
10873             (when (assq 'execute (car ps))
10874               (setcdr (assq 'execute (car ps))
10875                       (funcall (if (string-match "%s" action)
10876                                    'format 'concat)
10877                                action
10878                                (mapconcat
10879                                 (lambda (f)
10880                                   (if (equal f " ")
10881                                       f
10882                                     (mm-quote-arg f)))
10883                                 files " ")))))
10884           (setq ps (cdr ps)))))
10885     (if (and gnus-view-pseudos (not not-view))
10886         (while pslist
10887           (when (assq 'execute (car pslist))
10888             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10889                                   (eq gnus-view-pseudos 'not-confirm)))
10890           (setq pslist (cdr pslist)))
10891       (save-excursion
10892         (while pslist
10893           (setq after-article (or (cdr (assq 'article (car pslist)))
10894                                   (gnus-summary-article-number)))
10895           (gnus-summary-goto-subject after-article)
10896           (forward-line 1)
10897           (setq b (point))
10898           (insert "    " (file-name-nondirectory
10899                           (cdr (assq 'name (car pslist))))
10900                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10901           (setq e (point))
10902           (forward-line -1)             ; back to `b'
10903           (gnus-add-text-properties
10904            b (1- e) (list 'gnus-number gnus-reffed-article-number
10905                           gnus-mouse-face-prop gnus-mouse-face))
10906           (gnus-data-enter
10907            after-article gnus-reffed-article-number
10908            gnus-unread-mark b (car pslist) 0 (- e b))
10909           (setq gnus-newsgroup-unreads
10910                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10911                                          gnus-reffed-article-number))
10912           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10913           (setq pslist (cdr pslist)))))))
10914
10915 (defun gnus-pseudos< (p1 p2)
10916   (let ((c1 (cdr (assq 'action p1)))
10917         (c2 (cdr (assq 'action p2))))
10918     (and c1 c2 (string< c1 c2))))
10919
10920 (defun gnus-request-pseudo-article (props)
10921   (cond ((assq 'execute props)
10922          (gnus-execute-command (cdr (assq 'execute props)))))
10923   (let ((gnus-current-article (gnus-summary-article-number)))
10924     (gnus-run-hooks 'gnus-mark-article-hook)))
10925
10926 (defun gnus-execute-command (command &optional automatic)
10927   (save-excursion
10928     (gnus-article-setup-buffer)
10929     (set-buffer gnus-article-buffer)
10930     (setq buffer-read-only nil)
10931     (let ((command (if automatic command
10932                      (read-string "Command: " (cons command 0)))))
10933       (erase-buffer)
10934       (insert "$ " command "\n\n")
10935       (if gnus-view-pseudo-asynchronously
10936           (start-process "gnus-execute" (current-buffer) shell-file-name
10937                          shell-command-switch command)
10938         (call-process shell-file-name nil t nil
10939                       shell-command-switch command)))))
10940
10941 ;; Summary kill commands.
10942
10943 (defun gnus-summary-edit-global-kill (article)
10944   "Edit the \"global\" kill file."
10945   (interactive (list (gnus-summary-article-number)))
10946   (gnus-group-edit-global-kill article))
10947
10948 (defun gnus-summary-edit-local-kill ()
10949   "Edit a local kill file applied to the current newsgroup."
10950   (interactive)
10951   (setq gnus-current-headers (gnus-summary-article-header))
10952   (gnus-group-edit-local-kill
10953    (gnus-summary-article-number) gnus-newsgroup-name))
10954
10955 ;;; Header reading.
10956
10957 (defun gnus-read-header (id &optional header)
10958   "Read the headers of article ID and enter them into the Gnus system."
10959   (let ((group gnus-newsgroup-name)
10960         (gnus-override-method
10961          (or
10962           gnus-override-method
10963           (and (gnus-news-group-p gnus-newsgroup-name)
10964                (car (gnus-refer-article-methods)))))
10965         where)
10966     ;; First we check to see whether the header in question is already
10967     ;; fetched.
10968     (if (stringp id)
10969         ;; This is a Message-ID.
10970         (setq header (or header (gnus-id-to-header id)))
10971       ;; This is an article number.
10972       (setq header (or header (gnus-summary-article-header id))))
10973     (if (and header
10974              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10975         ;; We have found the header.
10976         header
10977       ;; If this is a sparse article, we have to nix out its
10978       ;; previous entry in the thread hashtb.
10979       (when (and header
10980                  (gnus-summary-article-sparse-p (mail-header-number header)))
10981         (let* ((parent (gnus-parent-id (mail-header-references header)))
10982                (thread (and parent (gnus-id-to-thread parent))))
10983           (when thread
10984             (delq (assq header thread) thread))))
10985       ;; We have to really fetch the header to this article.
10986       (save-excursion
10987         (set-buffer nntp-server-buffer)
10988         (when (setq where (gnus-request-head id group))
10989           (nnheader-fold-continuation-lines)
10990           (goto-char (point-max))
10991           (insert ".\n")
10992           (goto-char (point-min))
10993           (insert "211 ")
10994           (princ (cond
10995                   ((numberp id) id)
10996                   ((cdr where) (cdr where))
10997                   (header (mail-header-number header))
10998                   (t gnus-reffed-article-number))
10999                  (current-buffer))
11000           (insert " Article retrieved.\n"))
11001         (if (or (not where)
11002                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11003             ()                          ; Malformed head.
11004           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11005             (when (and (stringp id)
11006                        (not (string= (gnus-group-real-name group)
11007                                      (car where))))
11008               ;; If we fetched by Message-ID and the article came
11009               ;; from a different group, we fudge some bogus article
11010               ;; numbers for this article.
11011               (mail-header-set-number header gnus-reffed-article-number))
11012             (save-excursion
11013               (set-buffer gnus-summary-buffer)
11014               (decf gnus-reffed-article-number)
11015               (gnus-remove-header (mail-header-number header))
11016               (push header gnus-newsgroup-headers)
11017               (setq gnus-current-headers header)
11018               (push (mail-header-number header) gnus-newsgroup-limit)))
11019           header)))))
11020
11021 (defun gnus-remove-header (number)
11022   "Remove header NUMBER from `gnus-newsgroup-headers'."
11023   (if (and gnus-newsgroup-headers
11024            (= number (mail-header-number (car gnus-newsgroup-headers))))
11025       (pop gnus-newsgroup-headers)
11026     (let ((headers gnus-newsgroup-headers))
11027       (while (and (cdr headers)
11028                   (not (= number (mail-header-number (cadr headers)))))
11029         (pop headers))
11030       (when (cdr headers)
11031         (setcdr headers (cddr headers))))))
11032
11033 ;;;
11034 ;;; summary highlights
11035 ;;;
11036
11037 (defun gnus-highlight-selected-summary ()
11038   "Highlight selected article in summary buffer."
11039   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11040   (when gnus-summary-selected-face
11041     (save-excursion
11042       (let* ((beg (progn (beginning-of-line) (point)))
11043              (end (progn (end-of-line) (point)))
11044              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11045              (from (if (get-text-property beg gnus-mouse-face-prop)
11046                        beg
11047                      (or (next-single-property-change
11048                           beg gnus-mouse-face-prop nil end)
11049                          beg)))
11050              (to
11051               (if (= from end)
11052                   (- from 2)
11053                 (or (next-single-property-change
11054                      from gnus-mouse-face-prop nil end)
11055                     end))))
11056         ;; If no mouse-face prop on line we will have to = from = end,
11057         ;; so we highlight the entire line instead.
11058         (when (= (+ to 2) from)
11059           (setq from beg)
11060           (setq to end))
11061         (if gnus-newsgroup-selected-overlay
11062             ;; Move old overlay.
11063             (gnus-move-overlay
11064              gnus-newsgroup-selected-overlay from to (current-buffer))
11065           ;; Create new overlay.
11066           (gnus-overlay-put
11067            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11068            'face gnus-summary-selected-face))))))
11069
11070 (defvar gnus-summary-highlight-line-cached nil)
11071 (defvar gnus-summary-highlight-line-trigger nil)
11072
11073 (defun gnus-summary-highlight-line-0 ()
11074   (if (and (eq gnus-summary-highlight-line-trigger 
11075                gnus-summary-highlight)
11076            gnus-summary-highlight-line-cached)
11077       gnus-summary-highlight-line-cached
11078     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11079           gnus-summary-highlight-line-cached
11080           (let* ((cond (list 'cond))
11081                  (c cond)
11082                  (list gnus-summary-highlight))
11083             (while list
11084               (setcdr c (cons (list (caar list) (list 'quote (cdar list))) nil))
11085               (setq c (cdr c)
11086                     list (cdr list)))
11087             (gnus-byte-compile (list 'lambda nil cond))))))
11088
11089 (defun gnus-summary-highlight-line ()
11090   "Highlight current line according to `gnus-summary-highlight'."
11091   (let* ((beg (gnus-point-at-bol))
11092          (article (or (gnus-summary-article-number) gnus-current-article))
11093          (score (or (cdr (assq article
11094                                gnus-newsgroup-scored))
11095                     gnus-summary-default-score 0))
11096          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11097          (inhibit-read-only t)
11098          (default gnus-summary-default-score)
11099          (default-high gnus-summary-default-high-score)
11100          (default-low gnus-summary-default-low-score)
11101          (uncached (memq article gnus-newsgroup-undownloaded))
11102          (downloaded (not uncached)))
11103     (let ((face (funcall (gnus-summary-highlight-line-0))))
11104       (unless (eq face (get-text-property beg 'face))
11105         (gnus-put-text-property-excluding-characters-with-faces
11106          beg (gnus-point-at-eol) 'face
11107          (setq face (if (boundp face) (symbol-value face) face)))
11108         (when gnus-summary-highlight-line-function
11109           (funcall gnus-summary-highlight-line-function article face))))))
11110
11111 (defun gnus-update-read-articles (group unread &optional compute)
11112   "Update the list of read articles in GROUP.
11113 UNREAD is a sorted list."
11114   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11115          (entry (gnus-gethash group gnus-newsrc-hashtb))
11116          (info (nth 2 entry))
11117          (prev 1)
11118          read)
11119     (if (or (not info) (not active))
11120         ;; There is no info on this group if it was, in fact,
11121         ;; killed.  Gnus stores no information on killed groups, so
11122         ;; there's nothing to be done.
11123         ;; One could store the information somewhere temporarily,
11124         ;; perhaps...  Hmmm...
11125         ()
11126       ;; Remove any negative articles numbers.
11127       (while (and unread (< (car unread) 0))
11128         (setq unread (cdr unread)))
11129       ;; Remove any expired article numbers
11130       (while (and unread (< (car unread) (car active)))
11131         (setq unread (cdr unread)))
11132       ;; Compute the ranges of read articles by looking at the list of
11133       ;; unread articles.
11134       (while unread
11135         (when (/= (car unread) prev)
11136           (push (if (= prev (1- (car unread))) prev
11137                   (cons prev (1- (car unread))))
11138                 read))
11139         (setq prev (1+ (car unread)))
11140         (setq unread (cdr unread)))
11141       (when (<= prev (cdr active))
11142         (push (cons prev (cdr active)) read))
11143       (setq read (if (> (length read) 1) (nreverse read) read))
11144       (if compute
11145           read
11146         (save-excursion
11147           (let (setmarkundo)
11148             ;; Propagate the read marks to the backend.
11149             (when (gnus-check-backend-function 'request-set-mark group)
11150               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11151                     (add (gnus-remove-from-range read (gnus-info-read info))))
11152                 (when (or add del)
11153                   (unless (gnus-check-group group)
11154                     (error "Can't open server for %s" group))
11155                   (gnus-request-set-mark
11156                    group (delq nil (list (if add (list add 'add '(read)))
11157                                          (if del (list del 'del '(read))))))
11158                   (setq setmarkundo
11159                         `(gnus-request-set-mark
11160                           ,group
11161                           ',(delq nil (list
11162                                        (if del (list del 'add '(read)))
11163                                        (if add (list add 'del '(read))))))))))
11164             (set-buffer gnus-group-buffer)
11165             (gnus-undo-register
11166               `(progn
11167                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11168                  (gnus-info-set-read ',info ',(gnus-info-read info))
11169                  (gnus-get-unread-articles-in-group ',info
11170                                                     (gnus-active ,group))
11171                  (gnus-group-update-group ,group t)
11172                  ,setmarkundo))))
11173         ;; Enter this list into the group info.
11174         (gnus-info-set-read info read)
11175         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11176         (gnus-get-unread-articles-in-group info (gnus-active group))
11177         t))))
11178
11179 (defun gnus-offer-save-summaries ()
11180   "Offer to save all active summary buffers."
11181   (let (buffers)
11182     ;; Go through all buffers and find all summaries.
11183     (dolist (buffer (buffer-list))
11184       (when (and (setq buffer (buffer-name buffer))
11185                  (string-match "Summary" buffer)
11186                  (save-excursion
11187                    (set-buffer buffer)
11188                    ;; We check that this is, indeed, a summary buffer.
11189                    (and (eq major-mode 'gnus-summary-mode)
11190                         ;; Also make sure this isn't bogus.
11191                         gnus-newsgroup-prepared
11192                         ;; Also make sure that this isn't a
11193                         ;; dead summary buffer.
11194                         (not gnus-dead-summary-mode))))
11195         (push buffer buffers)))
11196     ;; Go through all these summary buffers and offer to save them.
11197     (when buffers
11198       (save-excursion
11199         (map-y-or-n-p
11200          "Update summary buffer %s? "
11201          (lambda (buf)
11202            (switch-to-buffer buf)
11203            (gnus-summary-exit))
11204          buffers)))))
11205
11206 (defun gnus-summary-setup-default-charset ()
11207   "Setup newsgroup default charset."
11208   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11209       (setq gnus-newsgroup-charset nil)
11210     (let* ((ignored-charsets
11211             (or gnus-newsgroup-ephemeral-ignored-charsets
11212                 (append
11213                  (and gnus-newsgroup-name
11214                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11215                  gnus-newsgroup-ignored-charsets))))
11216       (setq gnus-newsgroup-charset
11217             (or gnus-newsgroup-ephemeral-charset
11218                 (and gnus-newsgroup-name
11219                      (gnus-parameter-charset gnus-newsgroup-name))
11220                 gnus-default-charset))
11221       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11222            ignored-charsets))))
11223
11224 ;;;
11225 ;;; Mime Commands
11226 ;;;
11227
11228 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11229   "Display the current article buffer fully MIME-buttonized.
11230 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11231 treated as multipart/mixed."
11232   (interactive "P")
11233   (require 'gnus-art)
11234   (let ((gnus-unbuttonized-mime-types nil)
11235         (gnus-mime-display-multipart-as-mixed show-all-parts))
11236     (gnus-summary-show-article)))
11237
11238 (defun gnus-summary-repair-multipart (article)
11239   "Add a Content-Type header to a multipart article without one."
11240   (interactive (list (gnus-summary-article-number)))
11241   (gnus-with-article article
11242     (message-narrow-to-head)
11243     (message-remove-header "Mime-Version")
11244     (goto-char (point-max))
11245     (insert "Mime-Version: 1.0\n")
11246     (widen)
11247     (when (search-forward "\n--" nil t)
11248       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11249         (message-narrow-to-head)
11250         (message-remove-header "Content-Type")
11251         (goto-char (point-max))
11252         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11253                         separator))
11254         (widen))))
11255   (let (gnus-mark-article-hook)
11256     (gnus-summary-select-article t t nil article)))
11257
11258 (defun gnus-summary-toggle-display-buttonized ()
11259   "Toggle the buttonizing of the article buffer."
11260   (interactive)
11261   (require 'gnus-art)
11262   (if (setq gnus-inhibit-mime-unbuttonizing
11263             (not gnus-inhibit-mime-unbuttonizing))
11264       (let ((gnus-unbuttonized-mime-types nil))
11265         (gnus-summary-show-article))
11266     (gnus-summary-show-article)))
11267
11268 ;;;
11269 ;;; Generic summary marking commands
11270 ;;;
11271
11272 (defvar gnus-summary-marking-alist
11273   '((read gnus-del-mark "d")
11274     (unread gnus-unread-mark "u")
11275     (ticked gnus-ticked-mark "!")
11276     (dormant gnus-dormant-mark "?")
11277     (expirable gnus-expirable-mark "e"))
11278   "An alist of names/marks/keystrokes.")
11279
11280 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11281 (defvar gnus-summary-mark-map)
11282
11283 (defun gnus-summary-make-all-marking-commands ()
11284   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11285   (dolist (elem gnus-summary-marking-alist)
11286     (apply 'gnus-summary-make-marking-command elem)))
11287
11288 (defun gnus-summary-make-marking-command (name mark keystroke)
11289   (let ((map (make-sparse-keymap)))
11290     (define-key gnus-summary-generic-mark-map keystroke map)
11291     (dolist (lway `((next "next" next nil "n")
11292                     (next-unread "next unread" next t "N")
11293                     (prev "previous" prev nil "p")
11294                     (prev-unread "previous unread" prev t "P")
11295                     (nomove "" nil nil ,keystroke)))
11296       (let ((func (gnus-summary-make-marking-command-1
11297                    mark (car lway) lway name)))
11298         (setq func (eval func))
11299         (define-key map (nth 4 lway) func)))))
11300
11301 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11302   `(defun ,(intern
11303             (format "gnus-summary-put-mark-as-%s%s"
11304                     name (if (eq way 'nomove)
11305                              ""
11306                            (concat "-" (symbol-name way)))))
11307      (n)
11308      ,(format
11309        "Mark the current article as %s%s.
11310 If N, the prefix, then repeat N times.
11311 If N is negative, move in reverse order.
11312 The difference between N and the actual number of articles marked is
11313 returned."
11314        name (cadr lway))
11315      (interactive "p")
11316      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11317
11318 (defun gnus-summary-generic-mark (n mark move unread)
11319   "Mark N articles with MARK."
11320   (unless (eq major-mode 'gnus-summary-mode)
11321     (error "This command can only be used in the summary buffer"))
11322   (gnus-summary-show-thread)
11323   (let ((nummove
11324          (cond
11325           ((eq move 'next) 1)
11326           ((eq move 'prev) -1)
11327           (t 0))))
11328     (if (zerop nummove)
11329         (setq n 1)
11330       (when (< n 0)
11331         (setq n (abs n)
11332               nummove (* -1 nummove))))
11333     (while (and (> n 0)
11334                 (gnus-summary-mark-article nil mark)
11335                 (zerop (gnus-summary-next-subject nummove unread t)))
11336       (setq n (1- n)))
11337     (when (/= 0 n)
11338       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11339     (gnus-summary-recenter)
11340     (gnus-summary-position-point)
11341     (gnus-set-mode-line 'summary)
11342     n))
11343
11344 (defun gnus-summary-insert-articles (articles)
11345   (when (setq articles
11346               (gnus-sorted-difference articles
11347                                       (mapcar (lambda (h)
11348                                                 (mail-header-number h))
11349                                               gnus-newsgroup-headers)))
11350     (setq gnus-newsgroup-headers
11351           (merge 'list
11352                  gnus-newsgroup-headers
11353                  (gnus-fetch-headers articles)
11354                  'gnus-article-sort-by-number))
11355     ;; Suppress duplicates?
11356     (when gnus-suppress-duplicates
11357       (gnus-dup-suppress-articles))
11358
11359     ;; We might want to build some more threads first.
11360     (when (and gnus-fetch-old-headers
11361                (eq gnus-headers-retrieved-by 'nov))
11362       (if (eq gnus-fetch-old-headers 'invisible)
11363           (gnus-build-all-threads)
11364         (gnus-build-old-threads)))
11365     ;; Let the Gnus agent mark articles as read.
11366     (when gnus-agent
11367       (gnus-agent-get-undownloaded-list))
11368     ;; Remove list identifiers from subject
11369     (when gnus-list-identifiers
11370       (gnus-summary-remove-list-identifiers))
11371     ;; First and last article in this newsgroup.
11372     (when gnus-newsgroup-headers
11373       (setq gnus-newsgroup-begin
11374             (mail-header-number (car gnus-newsgroup-headers))
11375             gnus-newsgroup-end
11376             (mail-header-number
11377              (gnus-last-element gnus-newsgroup-headers))))
11378     (when gnus-use-scoring
11379       (gnus-possibly-score-headers))))
11380
11381 (defun gnus-summary-insert-old-articles (&optional all)
11382   "Insert all old articles in this group.
11383 If ALL is non-nil, already read articles become readable.
11384 If ALL is a number, fetch this number of articles."
11385   (interactive "P")
11386   (prog1
11387       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11388             older len)
11389         (setq older
11390               ;; Some nntp servers lie about their active range.  When
11391               ;; this happens, the active range can be in the millions.
11392               ;; Use a compressed range to avoid creating a huge list.
11393               (gnus-range-difference (list gnus-newsgroup-active) old))
11394         (setq len (gnus-range-length older))
11395         (cond
11396          ((null older) nil)
11397          ((numberp all)
11398           (if (< all len)
11399               (let ((older-range (nreverse older)))
11400                 (setq older nil)
11401
11402                 (while (> all 0)
11403                   (let* ((r (pop older-range))
11404                          (min (if (numberp r) r (car r)))
11405                          (max (if (numberp r) r (cdr r))))
11406                     (while (and (<= min max)
11407                                 (> all 0))
11408                       (push max older)
11409                       (setq all (1- all)
11410                             max (1- max))))))
11411             (setq older (gnus-uncompress-range older))))
11412          (all
11413           (setq older (gnus-uncompress-range older)))
11414          (t
11415           (when (and (numberp gnus-large-newsgroup)
11416                    (> len gnus-large-newsgroup))
11417               (let* ((cursor-in-echo-area nil)
11418                      (initial (gnus-parameter-large-newsgroup-initial
11419                                gnus-newsgroup-name))
11420                      (input
11421                       (read-string
11422                        (format
11423                         "How many articles from %s (%s %d): "
11424                         (gnus-limit-string
11425                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11426                         (if initial "max" "default")
11427                         len)
11428                        (if initial
11429                            (cons (number-to-string initial)
11430                                  0)))))
11431                 (unless (string-match "^[ \t]*$" input)
11432                   (setq all (string-to-number input))
11433                   (if (< all len)
11434                       (let ((older-range (nreverse older)))
11435                         (setq older nil)
11436
11437                         (while (> all 0)
11438                           (let* ((r (pop older-range))
11439                                  (min (if (numberp r) r (car r)))
11440                                  (max (if (numberp r) r (cdr r))))
11441                             (while (and (<= min max)
11442                                         (> all 0))
11443                               (push max older)
11444                               (setq all (1- all)
11445                                     max (1- max))))))))))
11446           (setq older (gnus-uncompress-range older))))
11447         (if (not older)
11448             (message "No old news.")
11449           (gnus-summary-insert-articles older)
11450           (gnus-summary-limit (gnus-sorted-nunion old older))))
11451     (gnus-summary-position-point)))
11452
11453 (defun gnus-summary-insert-new-articles ()
11454   "Insert all new articles in this group."
11455   (interactive)
11456   (prog1
11457       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11458             (old-active gnus-newsgroup-active)
11459             (nnmail-fetched-sources (list t))
11460             i new)
11461         (setq gnus-newsgroup-active
11462               (gnus-activate-group gnus-newsgroup-name 'scan))
11463         (setq i (cdr gnus-newsgroup-active))
11464         (while (> i (cdr old-active))
11465           (push i new)
11466           (decf i))
11467         (if (not new)
11468             (message "No gnus is bad news.")
11469           (gnus-summary-insert-articles new)
11470           (setq gnus-newsgroup-unreads
11471                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11472           (gnus-summary-limit (gnus-sorted-nunion old new))))
11473     (gnus-summary-position-point)))
11474
11475 (gnus-summary-make-all-marking-commands)
11476
11477 (gnus-ems-redefine)
11478
11479 (provide 'gnus-sum)
11480
11481 (run-hooks 'gnus-sum-load-hook)
11482
11483 ;;; gnus-sum.el ends here