(gnus-articles-to-read): Don't just apply
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 (require 'nnoo)
40
41 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
42 (autoload 'gnus-cache-write-active "gnus-cache")
43 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
44 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
45 (autoload 'mm-uu-dissect "mm-uu")
46 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
47   "Deuglify broken Outlook (Express) articles and redisplay."
48   t)
49 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
50 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
51 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
52
53 (defcustom gnus-kill-summary-on-exit t
54   "*If non-nil, kill the summary buffer when you exit from it.
55 If nil, the summary will become a \"*Dead Summary*\" buffer, and
56 it will be killed sometime later."
57   :group 'gnus-summary-exit
58   :type 'boolean)
59
60 (defcustom gnus-fetch-old-headers nil
61   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
62 If an unread article in the group refers to an older, already read (or
63 just marked as read) article, the old article will not normally be
64 displayed in the Summary buffer.  If this variable is non-nil, Gnus
65 will attempt to grab the headers to the old articles, and thereby
66 build complete threads.  If it has the value `some', only enough
67 headers to connect otherwise loose threads will be displayed.  This
68 variable can also be a number.  In that case, no more than that number
69 of old headers will be fetched.  If it has the value `invisible', all
70 old headers will be fetched, but none will be displayed.
71
72 The server has to support NOV for any of this to work."
73   :group 'gnus-thread
74   :type '(choice (const :tag "off" nil)
75                  (const some)
76                  number
77                  (sexp :menu-tag "other" t)))
78
79 (defcustom gnus-refer-thread-limit 200
80   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
81 If t, fetch all the available old headers."
82   :group 'gnus-thread
83   :type '(choice number
84                  (sexp :menu-tag "other" t)))
85
86 (defcustom gnus-summary-make-false-root 'adopt
87   "*nil means that Gnus won't gather loose threads.
88 If the root of a thread has expired or been read in a previous
89 session, the information necessary to build a complete thread has been
90 lost.  Instead of having many small sub-threads from this original thread
91 scattered all over the summary buffer, Gnus can gather them.
92
93 If non-nil, Gnus will try to gather all loose sub-threads from an
94 original thread into one large thread.
95
96 If this variable is non-nil, it should be one of `none', `adopt',
97 `dummy' or `empty'.
98
99 If this variable is `none', Gnus will not make a false root, but just
100 present the sub-threads after another.
101 If this variable is `dummy', Gnus will create a dummy root that will
102 have all the sub-threads as children.
103 If this variable is `adopt', Gnus will make one of the \"children\"
104 the parent and mark all the step-children as such.
105 If this variable is `empty', the \"children\" are printed with empty
106 subject fields.  (Or rather, they will be printed with a string
107 given by the `gnus-summary-same-subject' variable.)"
108   :group 'gnus-thread
109   :type '(choice (const :tag "off" nil)
110                  (const none)
111                  (const dummy)
112                  (const adopt)
113                  (const empty)))
114
115 (defcustom gnus-summary-make-false-root-always nil
116   "Always make a false dummy root."
117   :group 'gnus-thread
118   :type 'boolean)
119
120 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
121   "*A regexp to match subjects to be excluded from loose thread gathering.
122 As loose thread gathering is done on subjects only, that means that
123 there can be many false gatherings performed.  By rooting out certain
124 common subjects, gathering might become saner."
125   :group 'gnus-thread
126   :type 'regexp)
127
128 (defcustom gnus-summary-gather-subject-limit nil
129   "*Maximum length of subject comparisons when gathering loose threads.
130 Use nil to compare full subjects.  Setting this variable to a low
131 number will help gather threads that have been corrupted by
132 newsreaders chopping off subject lines, but it might also mean that
133 unrelated articles that have subject that happen to begin with the
134 same few characters will be incorrectly gathered.
135
136 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
137 comparing subjects."
138   :group 'gnus-thread
139   :type '(choice (const :tag "off" nil)
140                  (const fuzzy)
141                  (sexp :menu-tag "on" t)))
142
143 (defcustom gnus-simplify-subject-functions nil
144   "List of functions taking a string argument that simplify subjects.
145 The functions are applied recursively.
146
147 Useful functions to put in this list include:
148 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
149 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
150   :group 'gnus-thread
151   :type '(repeat function))
152
153 (defcustom gnus-simplify-ignored-prefixes nil
154   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
155   :group 'gnus-thread
156   :type '(choice (const :tag "off" nil)
157                  regexp))
158
159 (defcustom gnus-build-sparse-threads nil
160   "*If non-nil, fill in the gaps in threads.
161 If `some', only fill in the gaps that are needed to tie loose threads
162 together.  If `more', fill in all leaf nodes that Gnus can find.  If
163 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
164   :group 'gnus-thread
165   :type '(choice (const :tag "off" nil)
166                  (const some)
167                  (const more)
168                  (sexp :menu-tag "all" t)))
169
170 (defcustom gnus-summary-thread-gathering-function
171   'gnus-gather-threads-by-subject
172   "*Function used for gathering loose threads.
173 There are two pre-defined functions: `gnus-gather-threads-by-subject',
174 which only takes Subjects into consideration; and
175 `gnus-gather-threads-by-references', which compared the References
176 headers of the articles to find matches."
177   :group 'gnus-thread
178   :type '(radio (function-item gnus-gather-threads-by-subject)
179                 (function-item gnus-gather-threads-by-references)
180                 (function :tag "other")))
181
182 (defcustom gnus-summary-same-subject ""
183   "*String indicating that the current article has the same subject as the previous.
184 This variable will only be used if the value of
185 `gnus-summary-make-false-root' is `empty'."
186   :group 'gnus-summary-format
187   :type 'string)
188
189 (defcustom gnus-summary-goto-unread t
190   "*If t, many commands will go to the next unread article.
191 This applies to marking commands as well as other commands that
192 \"naturally\" select the next article, like, for instance, `SPC' at
193 the end of an article.
194
195 If nil, the marking commands do NOT go to the next unread article
196 \(they go to the next article instead).  If `never', commands that
197 usually go to the next unread article, will go to the next article,
198 whether it is read or not."
199   :group 'gnus-summary-marks
200   :link '(custom-manual "(gnus)Setting Marks")
201   :type '(choice (const :tag "off" nil)
202                  (const never)
203                  (sexp :menu-tag "on" t)))
204
205 (defcustom gnus-summary-default-score 0
206   "*Default article score level.
207 All scores generated by the score files will be added to this score.
208 If this variable is nil, scoring will be disabled."
209   :group 'gnus-score-default
210   :type '(choice (const :tag "disable")
211                  integer))
212
213 (defcustom gnus-summary-default-high-score 0
214   "*Default threshold for a high scored article.
215 An article will be highlighted as high scored if its score is greater
216 than this score."
217   :group 'gnus-score-default
218   :type 'integer)
219
220 (defcustom gnus-summary-default-low-score 0
221   "*Default threshold for a low scored article.
222 An article will be highlighted as low scored if its score is smaller
223 than this score."
224   :group 'gnus-score-default
225   :type 'integer)
226
227 (defcustom gnus-summary-zcore-fuzz 0
228   "*Fuzziness factor for the zcore in the summary buffer.
229 Articles with scores closer than this to `gnus-summary-default-score'
230 will not be marked."
231   :group 'gnus-summary-format
232   :type 'integer)
233
234 (defcustom gnus-simplify-subject-fuzzy-regexp nil
235   "*Strings to be removed when doing fuzzy matches.
236 This can either be a regular expression or list of regular expressions
237 that will be removed from subject strings if fuzzy subject
238 simplification is selected."
239   :group 'gnus-thread
240   :type '(repeat regexp))
241
242 (defcustom gnus-show-threads t
243   "*If non-nil, display threads in summary mode."
244   :group 'gnus-thread
245   :type 'boolean)
246
247 (defcustom gnus-thread-hide-subtree nil
248   "*If non-nil, hide all threads initially.
249 This can be a predicate specifier which says which threads to hide.
250 If threads are hidden, you have to run the command
251 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
252 to expose hidden threads."
253   :group 'gnus-thread
254   :type 'boolean)
255
256 (defcustom gnus-thread-hide-killed t
257   "*If non-nil, hide killed threads automatically."
258   :group 'gnus-thread
259   :type 'boolean)
260
261 (defcustom gnus-thread-ignore-subject t
262   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
263 If nil, articles that have different subjects from their parents will
264 start separate threads."
265   :group 'gnus-thread
266   :type 'boolean)
267
268 (defcustom gnus-thread-operation-ignore-subject t
269   "*If non-nil, subjects will be ignored when doing thread commands.
270 This affects commands like `gnus-summary-kill-thread' and
271 `gnus-summary-lower-thread'.
272
273 If this variable is nil, articles in the same thread with different
274 subjects will not be included in the operation in question.  If this
275 variable is `fuzzy', only articles that have subjects that are fuzzily
276 equal will be included."
277   :group 'gnus-thread
278   :type '(choice (const :tag "off" nil)
279                  (const fuzzy)
280                  (sexp :tag "on" t)))
281
282 (defcustom gnus-thread-indent-level 4
283   "*Number that says how much each sub-thread should be indented."
284   :group 'gnus-thread
285   :type 'integer)
286
287 (defcustom gnus-auto-extend-newsgroup t
288   "*If non-nil, extend newsgroup forward and backward when requested."
289   :group 'gnus-summary-choose
290   :type 'boolean)
291
292 (defcustom gnus-auto-select-first t
293   "*If non-nil, select the article under point.
294 Which article this is is controlled by the `gnus-auto-select-subject'
295 variable.
296
297 If you want to prevent automatic selection of articles in some
298 newsgroups, set the variable to nil in `gnus-select-group-hook'."
299   :group 'gnus-group-select
300   :type '(choice (const :tag "none" nil)
301                  (sexp :menu-tag "first" t)))
302
303 (defcustom gnus-auto-select-subject 'unread
304   "*Says what subject to place under point when entering a group.
305
306 This variable can either be the symbols `first' (place point on the
307 first subject), `unread' (place point on the subject line of the first
308 unread article), `best' (place point on the subject line of the
309 higest-scored article), `unseen' (place point on the subject line of
310 the first unseen article), 'unseen-or-unread' (place point on the subject
311 line of the first unseen article or, if all article have been seen, on the
312 subject line of the first unread article), or a function to be called to
313 place point on some subject line."
314   :group 'gnus-group-select
315   :type '(choice (const best)
316                  (const unread)
317                  (const first)
318                  (const unseen)
319                  (const unseen-or-unread)))
320
321 (defcustom gnus-auto-select-next t
322   "*If non-nil, offer to go to the next group from the end of the previous.
323 If the value is t and the next newsgroup is empty, Gnus will exit
324 summary mode and go back to group mode.  If the value is neither nil
325 nor t, Gnus will select the following unread newsgroup.  In
326 particular, if the value is the symbol `quietly', the next unread
327 newsgroup will be selected without any confirmation, and if it is
328 `almost-quietly', the next group will be selected without any
329 confirmation if you are located on the last article in the group.
330 Finally, if this variable is `slightly-quietly', the `Z n' command
331 will go to the next group without confirmation."
332   :group 'gnus-summary-maneuvering
333   :type '(choice (const :tag "off" nil)
334                  (const quietly)
335                  (const almost-quietly)
336                  (const slightly-quietly)
337                  (sexp :menu-tag "on" t)))
338
339 (defcustom gnus-auto-select-same nil
340   "*If non-nil, select the next article with the same subject.
341 If there are no more articles with the same subject, go to
342 the first unread article."
343   :group 'gnus-summary-maneuvering
344   :type 'boolean)
345
346 (defcustom gnus-summary-check-current nil
347   "*If non-nil, consider the current article when moving.
348 The \"unread\" movement commands will stay on the same line if the
349 current article is unread."
350   :group 'gnus-summary-maneuvering
351   :type 'boolean)
352
353 (defcustom gnus-auto-center-summary t
354   "*If non-nil, always center the current summary buffer.
355 In particular, if `vertical' do only vertical recentering.  If non-nil
356 and non-`vertical', do both horizontal and vertical recentering."
357   :group 'gnus-summary-maneuvering
358   :type '(choice (const :tag "none" nil)
359                  (const vertical)
360                  (integer :tag "height")
361                  (sexp :menu-tag "both" t)))
362
363 (defcustom gnus-show-all-headers nil
364   "*If non-nil, don't hide any headers."
365   :group 'gnus-article-hiding
366   :group 'gnus-article-headers
367   :type 'boolean)
368
369 (defcustom gnus-summary-ignore-duplicates nil
370   "*If non-nil, ignore articles with identical Message-ID headers."
371   :group 'gnus-summary
372   :type 'boolean)
373
374 (defcustom gnus-single-article-buffer t
375   "*If non-nil, display all articles in the same buffer.
376 If nil, each group will get its own article buffer."
377   :group 'gnus-article-various
378   :type 'boolean)
379
380 (defcustom gnus-break-pages t
381   "*If non-nil, do page breaking on articles.
382 The page delimiter is specified by the `gnus-page-delimiter'
383 variable."
384   :group 'gnus-article-various
385   :type 'boolean)
386
387 (defcustom gnus-move-split-methods nil
388   "*Variable used to suggest where articles are to be moved to.
389 It uses the same syntax as the `gnus-split-methods' variable.
390 However, whereas `gnus-split-methods' specifies file names as targets,
391 this variable specifies group names."
392   :group 'gnus-summary-mail
393   :type '(repeat (choice (list :value (fun) function)
394                          (cons :value ("" "") regexp (repeat string))
395                          (sexp :value nil))))
396
397 (defcustom gnus-unread-mark ?           ;Whitespace
398   "*Mark used for unread articles."
399   :group 'gnus-summary-marks
400   :type 'character)
401
402 (defcustom gnus-ticked-mark ?!
403   "*Mark used for ticked articles."
404   :group 'gnus-summary-marks
405   :type 'character)
406
407 (defcustom gnus-dormant-mark ??
408   "*Mark used for dormant articles."
409   :group 'gnus-summary-marks
410   :type 'character)
411
412 (defcustom gnus-del-mark ?r
413   "*Mark used for del'd articles."
414   :group 'gnus-summary-marks
415   :type 'character)
416
417 (defcustom gnus-read-mark ?R
418   "*Mark used for read articles."
419   :group 'gnus-summary-marks
420   :type 'character)
421
422 (defcustom gnus-expirable-mark ?E
423   "*Mark used for expirable articles."
424   :group 'gnus-summary-marks
425   :type 'character)
426
427 (defcustom gnus-killed-mark ?K
428   "*Mark used for killed articles."
429   :group 'gnus-summary-marks
430   :type 'character)
431
432 (defcustom gnus-spam-mark ?H
433   "*Mark used for spam articles."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-souped-mark ?F
438   "*Mark used for souped articles."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-kill-file-mark ?X
443   "*Mark used for articles killed by kill files."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-low-score-mark ?Y
448   "*Mark used for articles with a low score."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-catchup-mark ?C
453   "*Mark used for articles that are caught up."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-replied-mark ?A
458   "*Mark used for articles that have been replied to."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-forwarded-mark ?F
463   "*Mark used for articles that have been forwarded."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-recent-mark ?N
468   "*Mark used for articles that are recent."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-cached-mark ?*
473   "*Mark used for articles that are in the cache."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-saved-mark ?S
478   "*Mark used for articles that have been saved."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-unseen-mark ?.
483   "*Mark used for articles that haven't been seen."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-no-mark ?               ;Whitespace
488   "*Mark used for articles that have no other secondary mark."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-ancient-mark ?O
493   "*Mark used for ancient articles."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-sparse-mark ?Q
498   "*Mark used for sparsely reffed articles."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-canceled-mark ?G
503   "*Mark used for canceled articles."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-duplicate-mark ?M
508   "*Mark used for duplicate articles."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-undownloaded-mark ?-
513   "*Mark used for articles that weren't downloaded."
514   :group 'gnus-summary-marks
515   :type 'character)
516
517 (defcustom gnus-downloaded-mark ?+
518   "*Mark used for articles that were downloaded."
519   :group 'gnus-summary-marks
520   :type 'character)
521
522 (defcustom gnus-downloadable-mark ?%
523   "*Mark used for articles that are to be downloaded."
524   :group 'gnus-summary-marks
525   :type 'character)
526
527 (defcustom gnus-unsendable-mark ?=
528   "*Mark used for articles that won't be sent."
529   :group 'gnus-summary-marks
530   :type 'character)
531
532 (defcustom gnus-score-over-mark ?+
533   "*Score mark used for articles with high scores."
534   :group 'gnus-summary-marks
535   :type 'character)
536
537 (defcustom gnus-score-below-mark ?-
538   "*Score mark used for articles with low scores."
539   :group 'gnus-summary-marks
540   :type 'character)
541
542 (defcustom gnus-empty-thread-mark ?     ;Whitespace
543   "*There is no thread under the article."
544   :group 'gnus-summary-marks
545   :type 'character)
546
547 (defcustom gnus-not-empty-thread-mark ?=
548   "*There is a thread under the article."
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-view-pseudo-asynchronously nil
553   "*If non-nil, Gnus will view pseudo-articles asynchronously."
554   :group 'gnus-extract-view
555   :type 'boolean)
556
557 (defcustom gnus-auto-expirable-marks
558   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
559         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
560         gnus-souped-mark gnus-duplicate-mark)
561   "*The list of marks converted into expiration if a group is auto-expirable."
562   :version "21.1"
563   :group 'gnus-summary
564   :type '(repeat character))
565
566 (defcustom gnus-inhibit-user-auto-expire t
567   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
568   :version "21.1"
569   :group 'gnus-summary
570   :type 'boolean)
571
572 (defcustom gnus-view-pseudos nil
573   "*If `automatic', pseudo-articles will be viewed automatically.
574 If `not-confirm', pseudos will be viewed automatically, and the user
575 will not be asked to confirm the command."
576   :group 'gnus-extract-view
577   :type '(choice (const :tag "off" nil)
578                  (const automatic)
579                  (const not-confirm)))
580
581 (defcustom gnus-view-pseudos-separately t
582   "*If non-nil, one pseudo-article will be created for each file to be viewed.
583 If nil, all files that use the same viewing command will be given as a
584 list of parameters to that command."
585   :group 'gnus-extract-view
586   :type 'boolean)
587
588 (defcustom gnus-insert-pseudo-articles t
589   "*If non-nil, insert pseudo-articles when decoding articles."
590   :group 'gnus-extract-view
591   :type 'boolean)
592
593 (defcustom gnus-summary-dummy-line-format
594   "   %(:                             :%) %S\n"
595   "*The format specification for the dummy roots in the summary buffer.
596 It works along the same lines as a normal formatting string,
597 with some simple extensions.
598
599 %S  The subject
600
601 General format specifiers can also be used.
602 See `(gnus)Formatting Variables'."
603   :link '(custom-manual "(gnus)Formatting Variables")
604   :group 'gnus-threading
605   :type 'string)
606
607 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
608   "*The format specification for the summary mode line.
609 It works along the same lines as a normal formatting string,
610 with some simple extensions:
611
612 %G  Group name
613 %p  Unprefixed group name
614 %A  Current article number
615 %z  Current article score
616 %V  Gnus version
617 %U  Number of unread articles in the group
618 %e  Number of unselected articles in the group
619 %Z  A string with unread/unselected article counts
620 %g  Shortish group name
621 %S  Subject of the current article
622 %u  User-defined spec
623 %s  Current score file name
624 %d  Number of dormant articles
625 %r  Number of articles that have been marked as read in this session
626 %E  Number of articles expunged by the score files"
627   :group 'gnus-summary-format
628   :type 'string)
629
630 (defcustom gnus-list-identifiers nil
631   "Regexp that matches list identifiers to be removed from subject.
632 This can also be a list of regexps."
633   :version "21.1"
634   :group 'gnus-summary-format
635   :group 'gnus-article-hiding
636   :type '(choice (const :tag "none" nil)
637                  (regexp :value ".*")
638                  (repeat :value (".*") regexp)))
639
640 (defcustom gnus-summary-mark-below 0
641   "*Mark all articles with a score below this variable as read.
642 This variable is local to each summary buffer and usually set by the
643 score file."
644   :group 'gnus-score-default
645   :type 'integer)
646
647 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
648   "*List of functions used for sorting articles in the summary buffer.
649
650 Each function takes two articles and returns non-nil if the first
651 article should be sorted before the other.  If you use more than one
652 function, the primary sort function should be the last.  You should
653 probably always include `gnus-article-sort-by-number' in the list of
654 sorting functions -- preferably first.  Also note that sorting by date
655 is often much slower than sorting by number, and the sorting order is
656 very similar.  (Sorting by date means sorting by the time the message
657 was sent, sorting by number means sorting by arrival time.)
658
659 Ready-made functions include `gnus-article-sort-by-number',
660 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
661 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
662 and `gnus-article-sort-by-score'.
663
664 When threading is turned on, the variable `gnus-thread-sort-functions'
665 controls how articles are sorted."
666   :group 'gnus-summary-sort
667   :type '(repeat (choice (function-item gnus-article-sort-by-number)
668                          (function-item gnus-article-sort-by-author)
669                          (function-item gnus-article-sort-by-subject)
670                          (function-item gnus-article-sort-by-date)
671                          (function-item gnus-article-sort-by-score)
672                          (function-item gnus-article-sort-by-random)
673                          (function :tag "other"))))
674
675 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
676   "*List of functions used for sorting threads in the summary buffer.
677 By default, threads are sorted by article number.
678
679 Each function takes two threads and returns non-nil if the first
680 thread should be sorted before the other.  If you use more than one
681 function, the primary sort function should be the last.  You should
682 probably always include `gnus-thread-sort-by-number' in the list of
683 sorting functions -- preferably first.  Also note that sorting by date
684 is often much slower than sorting by number, and the sorting order is
685 very similar.  (Sorting by date means sorting by the time the message
686 was sent, sorting by number means sorting by arrival time.)
687
688 Ready-made functions include `gnus-thread-sort-by-number',
689 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
690 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
691 `gnus-thread-sort-by-most-recent-number',
692 `gnus-thread-sort-by-most-recent-date',
693 `gnus-thread-sort-by-random', and
694 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
695
696 When threading is turned off, the variable
697 `gnus-article-sort-functions' controls how articles are sorted."
698   :group 'gnus-summary-sort
699   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
700                          (function-item gnus-thread-sort-by-author)
701                          (function-item gnus-thread-sort-by-subject)
702                          (function-item gnus-thread-sort-by-date)
703                          (function-item gnus-thread-sort-by-score)
704                          (function-item gnus-thread-sort-by-total-score)
705                          (function-item gnus-thread-sort-by-random)
706                          (function :tag "other"))))
707
708 (defcustom gnus-thread-score-function '+
709   "*Function used for calculating the total score of a thread.
710
711 The function is called with the scores of the article and each
712 subthread and should then return the score of the thread.
713
714 Some functions you can use are `+', `max', or `min'."
715   :group 'gnus-summary-sort
716   :type 'function)
717
718 (defcustom gnus-summary-expunge-below nil
719   "All articles that have a score less than this variable will be expunged.
720 This variable is local to the summary buffers."
721   :group 'gnus-score-default
722   :type '(choice (const :tag "off" nil)
723                  integer))
724
725 (defcustom gnus-thread-expunge-below nil
726   "All threads that have a total score less than this variable will be expunged.
727 See `gnus-thread-score-function' for en explanation of what a
728 \"thread score\" is.
729
730 This variable is local to the summary buffers."
731   :group 'gnus-threading
732   :group 'gnus-score-default
733   :type '(choice (const :tag "off" nil)
734                  integer))
735
736 (defcustom gnus-summary-mode-hook nil
737   "*A hook for Gnus summary mode.
738 This hook is run before any variables are set in the summary buffer."
739   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
740   :group 'gnus-summary-various
741   :type 'hook)
742
743 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
744 (when (featurep 'xemacs)
745   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
746   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
747   (add-hook 'gnus-summary-mode-hook
748             'gnus-xmas-switch-horizontal-scrollbar-off))
749
750 (defcustom gnus-summary-menu-hook nil
751   "*Hook run after the creation of the summary mode menu."
752   :group 'gnus-summary-visual
753   :type 'hook)
754
755 (defcustom gnus-summary-exit-hook nil
756   "*A hook called on exit from the summary buffer.
757 It will be called with point in the group buffer."
758   :group 'gnus-summary-exit
759   :type 'hook)
760
761 (defcustom gnus-summary-prepare-hook nil
762   "*A hook called after the summary buffer has been generated.
763 If you want to modify the summary buffer, you can use this hook."
764   :group 'gnus-summary-various
765   :type 'hook)
766
767 (defcustom gnus-summary-prepared-hook nil
768   "*A hook called as the last thing after the summary buffer has been generated."
769   :group 'gnus-summary-various
770   :type 'hook)
771
772 (defcustom gnus-summary-generate-hook nil
773   "*A hook run just before generating the summary buffer.
774 This hook is commonly used to customize threading variables and the
775 like."
776   :group 'gnus-summary-various
777   :type 'hook)
778
779 (defcustom gnus-select-group-hook nil
780   "*A hook called when a newsgroup is selected.
781
782 If you'd like to simplify subjects like the
783 `gnus-summary-next-same-subject' command does, you can use the
784 following hook:
785
786  (add-hook gnus-select-group-hook
787            (lambda ()
788              (mapcar (lambda (header)
789                        (mail-header-set-subject
790                         header
791                         (gnus-simplify-subject
792                          (mail-header-subject header) 're-only)))
793                      gnus-newsgroup-headers)))"
794   :group 'gnus-group-select
795   :type 'hook)
796
797 (defcustom gnus-select-article-hook nil
798   "*A hook called when an article is selected."
799   :group 'gnus-summary-choose
800   :options '(gnus-agent-fetch-selected-article)
801   :type 'hook)
802
803 (defcustom gnus-visual-mark-article-hook
804   (list 'gnus-highlight-selected-summary)
805   "*Hook run after selecting an article in the summary buffer.
806 It is meant to be used for highlighting the article in some way.  It
807 is not run if `gnus-visual' is nil."
808   :group 'gnus-summary-visual
809   :type 'hook)
810
811 (defcustom gnus-parse-headers-hook nil
812   "*A hook called before parsing the headers."
813   :group 'gnus-various
814   :type 'hook)
815
816 (defcustom gnus-exit-group-hook nil
817   "*A hook called when exiting summary mode.
818 This hook is not called from the non-updating exit commands like `Q'."
819   :group 'gnus-various
820   :type 'hook)
821
822 (defcustom gnus-summary-update-hook
823   (list 'gnus-summary-highlight-line)
824   "*A hook called when a summary line is changed.
825 The hook will not be called if `gnus-visual' is nil.
826
827 The default function `gnus-summary-highlight-line' will
828 highlight the line according to the `gnus-summary-highlight'
829 variable."
830   :group 'gnus-summary-visual
831   :type 'hook)
832
833 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
834   "*A hook called when an article is selected for the first time.
835 The hook is intended to mark an article as read (or unread)
836 automatically when it is selected."
837   :group 'gnus-summary-choose
838   :type 'hook)
839
840 (defcustom gnus-group-no-more-groups-hook nil
841   "*A hook run when returning to group mode having no more (unread) groups."
842   :group 'gnus-group-select
843   :type 'hook)
844
845 (defcustom gnus-ps-print-hook nil
846   "*A hook run before ps-printing something from Gnus."
847   :group 'gnus-summary
848   :type 'hook)
849
850 (defcustom gnus-summary-display-arrow
851   (and (fboundp 'display-graphic-p)
852        (display-graphic-p))
853   "*If non-nil, display an arrow highlighting the current article."
854   :version "21.1"
855   :group 'gnus-summary
856   :type 'boolean)
857
858 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
859   "Face used for highlighting the current article in the summary buffer."
860   :group 'gnus-summary-visual
861   :type 'face)
862
863 (defvar gnus-tmp-downloaded nil)
864
865 (defcustom gnus-summary-highlight
866   '(((eq mark gnus-canceled-mark)
867      . gnus-summary-cancelled-face)
868     ((and uncached (> score default-high))
869      . gnus-summary-high-undownloaded-face)
870     ((and uncached (< score default-low))
871      . gnus-summary-low-undownloaded-face)
872     (uncached
873      . gnus-summary-normal-undownloaded-face)
874     ((and (> score default-high)
875           (or (eq mark gnus-dormant-mark)
876               (eq mark gnus-ticked-mark)))
877      . gnus-summary-high-ticked-face)
878     ((and (< score default-low)
879           (or (eq mark gnus-dormant-mark)
880               (eq mark gnus-ticked-mark)))
881      . gnus-summary-low-ticked-face)
882     ((or (eq mark gnus-dormant-mark)
883          (eq mark gnus-ticked-mark))
884      . gnus-summary-normal-ticked-face)
885     ((and (> score default-high) (eq mark gnus-ancient-mark))
886      . gnus-summary-high-ancient-face)
887     ((and (< score default-low) (eq mark gnus-ancient-mark))
888      . gnus-summary-low-ancient-face)
889     ((eq mark gnus-ancient-mark)
890      . gnus-summary-normal-ancient-face)
891     ((and (> score default-high) (eq mark gnus-unread-mark))
892      . gnus-summary-high-unread-face)
893     ((and (< score default-low) (eq mark gnus-unread-mark))
894      . gnus-summary-low-unread-face)
895     ((eq mark gnus-unread-mark)
896      . gnus-summary-normal-unread-face)
897     ((> score default-high)
898      . gnus-summary-high-read-face)
899     ((< score default-low)
900      . gnus-summary-low-read-face)
901     (t
902      . gnus-summary-normal-read-face))
903   "*Controls the highlighting of summary buffer lines.
904
905 A list of (FORM . FACE) pairs.  When deciding how a a particular
906 summary line should be displayed, each form is evaluated.  The content
907 of the face field after the first true form is used.  You can change
908 how those summary lines are displayed, by editing the face field.
909
910 You can use the following variables in the FORM field.
911
912 score:        The article's score
913 default:      The default article score.
914 default-high: The default score for high scored articles.
915 default-low:  The default score for low scored articles.
916 below:        The score below which articles are automatically marked as read.
917 mark:         The articles mark."
918   :group 'gnus-summary-visual
919   :type '(repeat (cons (sexp :tag "Form" nil)
920                        face)))
921
922 (defcustom gnus-alter-header-function nil
923   "Function called to allow alteration of article header structures.
924 The function is called with one parameter, the article header vector,
925 which it may alter in any way.")
926
927 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
928   "Variable that says which function should be used to decode a string with encoded words.")
929
930 (defcustom gnus-extra-headers '(To Newsgroups)
931   "*Extra headers to parse."
932   :version "21.1"
933   :group 'gnus-summary
934   :type '(repeat symbol))
935
936 (defcustom gnus-ignored-from-addresses
937   (and user-mail-address (regexp-quote user-mail-address))
938   "*Regexp of From headers that may be suppressed in favor of To headers."
939   :version "21.1"
940   :group 'gnus-summary
941   :type 'regexp)
942
943 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
944   "List of charsets that should be ignored.
945 When these charsets are used in the \"charset\" parameter, the
946 default charset will be used instead."
947   :version "21.1"
948   :type '(repeat symbol)
949   :group 'gnus-charset)
950
951 (gnus-define-group-parameter
952  ignored-charsets
953  :type list
954  :function-document
955  "Return the ignored charsets of GROUP."
956  :variable gnus-group-ignored-charsets-alist
957  :variable-default
958  '(("alt\\.chinese\\.text" iso-8859-1))
959  :variable-document
960  "Alist of regexps (to match group names) and charsets that should be ignored.
961 When these charsets are used in the \"charset\" parameter, the
962 default charset will be used instead."
963  :variable-group gnus-charset
964  :variable-type '(repeat (cons (regexp :tag "Group")
965                                (repeat symbol)))
966  :parameter-type '(choice :tag "Ignored charsets"
967                           :value nil
968                           (repeat (symbol)))
969  :parameter-document       "\
970 List of charsets that should be ignored.
971
972 When these charsets are used in the \"charset\" parameter, the
973 default charset will be used instead.")
974
975 (defcustom gnus-group-highlight-words-alist nil
976   "Alist of group regexps and highlight regexps.
977 This variable uses the same syntax as `gnus-emphasis-alist'."
978   :version "21.1"
979   :type '(repeat (cons (regexp :tag "Group")
980                        (repeat (list (regexp :tag "Highlight regexp")
981                                      (number :tag "Group for entire word" 0)
982                                      (number :tag "Group for displayed part" 0)
983                                      (symbol :tag "Face"
984                                              gnus-emphasis-highlight-words)))))
985   :group 'gnus-summary-visual)
986
987 (defcustom gnus-summary-show-article-charset-alist
988   nil
989   "Alist of number and charset.
990 The article will be shown with the charset corresponding to the
991 numbered argument.
992 For example: ((1 . cn-gb-2312) (2 . big5))."
993   :version "21.1"
994   :type '(repeat (cons (number :tag "Argument" 1)
995                        (symbol :tag "Charset")))
996   :group 'gnus-charset)
997
998 (defcustom gnus-preserve-marks t
999   "Whether marks are preserved when moving, copying and respooling messages."
1000   :version "21.1"
1001   :type 'boolean
1002   :group 'gnus-summary-marks)
1003
1004 (defcustom gnus-alter-articles-to-read-function nil
1005   "Function to be called to alter the list of articles to be selected."
1006   :type '(choice (const nil) function)
1007   :group 'gnus-summary)
1008
1009 (defcustom gnus-orphan-score nil
1010   "*All orphans get this score added.  Set in the score file."
1011   :group 'gnus-score-default
1012   :type '(choice (const nil)
1013                  integer))
1014
1015 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1016   "*A regexp to match MIME parts when saving multiple parts of a message
1017 with gnus-summary-save-parts (X m). This regexp will be used by default
1018 when prompting the user for which type of files to save."
1019   :group 'gnus-summary
1020   :type 'regexp)
1021
1022 (defcustom gnus-read-all-available-headers nil
1023   "Whether Gnus should parse all headers made available to it.
1024 This is mostly relevant for slow backends where the user may
1025 wish to widen the summary buffer to include all headers
1026 that were fetched.  Say, for nnultimate groups."
1027   :group 'gnus-summary
1028   :type '(choice boolean regexp))
1029
1030 (defcustom gnus-summary-muttprint-program "muttprint"
1031   "Command (and optional arguments) used to run Muttprint."
1032   :version "21.3"
1033   :group 'gnus-summary
1034   :type 'string)
1035
1036 (defcustom gnus-article-loose-mime nil
1037   "If non-nil, don't require MIME-Version header.
1038 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1039 supply the MIME-Version header or deliberately strip it From the mail.
1040 Set it to non-nil, Gnus will treat some articles as MIME even if
1041 the MIME-Version header is missed."
1042   :version "21.3"
1043   :type 'boolean
1044   :group 'gnus-article)
1045
1046 (defcustom gnus-article-emulate-mime t
1047   "If non-nil, use MIME emulation for uuencode and the like.
1048 This means that Gnus will search message bodies for text that look
1049 like uuencoded bits, yEncoded bits, and so on, and present that using
1050 the normal Gnus MIME machinery."
1051   :type 'boolean
1052   :group 'gnus-article)
1053
1054 ;;; Internal variables
1055
1056 (defvar gnus-summary-display-cache nil)
1057 (defvar gnus-article-mime-handles nil)
1058 (defvar gnus-article-decoded-p nil)
1059 (defvar gnus-article-charset nil)
1060 (defvar gnus-article-ignored-charsets nil)
1061 (defvar gnus-scores-exclude-files nil)
1062 (defvar gnus-page-broken nil)
1063
1064 (defvar gnus-original-article nil)
1065 (defvar gnus-article-internal-prepare-hook nil)
1066 (defvar gnus-newsgroup-process-stack nil)
1067
1068 (defvar gnus-thread-indent-array nil)
1069 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1070 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1071   "Function called to sort the articles within a thread after it has been gathered together.")
1072
1073 (defvar gnus-summary-save-parts-type-history nil)
1074 (defvar gnus-summary-save-parts-last-directory nil)
1075
1076 ;; Avoid highlighting in kill files.
1077 (defvar gnus-summary-inhibit-highlight nil)
1078 (defvar gnus-newsgroup-selected-overlay nil)
1079 (defvar gnus-inhibit-limiting nil)
1080 (defvar gnus-newsgroup-adaptive-score-file nil)
1081 (defvar gnus-current-score-file nil)
1082 (defvar gnus-current-move-group nil)
1083 (defvar gnus-current-copy-group nil)
1084 (defvar gnus-current-crosspost-group nil)
1085 (defvar gnus-newsgroup-display nil)
1086
1087 (defvar gnus-newsgroup-dependencies nil)
1088 (defvar gnus-newsgroup-adaptive nil)
1089 (defvar gnus-summary-display-article-function nil)
1090 (defvar gnus-summary-highlight-line-function nil
1091   "Function called after highlighting a summary line.")
1092
1093 (defvar gnus-summary-line-format-alist
1094   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1095     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1096     (?s gnus-tmp-subject-or-nil ?s)
1097     (?n gnus-tmp-name ?s)
1098     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1099         ?s)
1100     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1101             gnus-tmp-from) ?s)
1102     (?F gnus-tmp-from ?s)
1103     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1104     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1105     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1106     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1107     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1108     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1109     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1110     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1111     (?L gnus-tmp-lines ?s)
1112     (?O gnus-tmp-downloaded ?c)
1113     (?I gnus-tmp-indentation ?s)
1114     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1115     (?R gnus-tmp-replied ?c)
1116     (?\[ gnus-tmp-opening-bracket ?c)
1117     (?\] gnus-tmp-closing-bracket ?c)
1118     (?\> (make-string gnus-tmp-level ? ) ?s)
1119     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1120     (?i gnus-tmp-score ?d)
1121     (?z gnus-tmp-score-char ?c)
1122     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1123     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1124     (?U gnus-tmp-unread ?c)
1125     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1126         ?s)
1127     (?t (gnus-summary-number-of-articles-in-thread
1128          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1129         ?d)
1130     (?e (gnus-summary-number-of-articles-in-thread
1131          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1132         ?c)
1133     (?u gnus-tmp-user-defined ?s)
1134     (?P (gnus-pick-line-number) ?d)
1135     (?B gnus-tmp-thread-tree-header-string ?s)
1136     (user-date (gnus-user-date
1137                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1138   "An alist of format specifications that can appear in summary lines.
1139 These are paired with what variables they correspond with, along with
1140 the type of the variable (string, integer, character, etc).")
1141
1142 (defvar gnus-summary-dummy-line-format-alist
1143   `((?S gnus-tmp-subject ?s)
1144     (?N gnus-tmp-number ?d)
1145     (?u gnus-tmp-user-defined ?s)))
1146
1147 (defvar gnus-summary-mode-line-format-alist
1148   `((?G gnus-tmp-group-name ?s)
1149     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1150     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1151     (?A gnus-tmp-article-number ?d)
1152     (?Z gnus-tmp-unread-and-unselected ?s)
1153     (?V gnus-version ?s)
1154     (?U gnus-tmp-unread-and-unticked ?d)
1155     (?S gnus-tmp-subject ?s)
1156     (?e gnus-tmp-unselected ?d)
1157     (?u gnus-tmp-user-defined ?s)
1158     (?d (length gnus-newsgroup-dormant) ?d)
1159     (?t (length gnus-newsgroup-marked) ?d)
1160     (?h (length gnus-newsgroup-spam-marked) ?d)
1161     (?r (length gnus-newsgroup-reads) ?d)
1162     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1163     (?E gnus-newsgroup-expunged-tally ?d)
1164     (?s (gnus-current-score-file-nondirectory) ?s)))
1165
1166 (defvar gnus-last-search-regexp nil
1167   "Default regexp for article search command.")
1168
1169 (defvar gnus-last-shell-command nil
1170   "Default shell command on article.")
1171
1172 (defvar gnus-newsgroup-agentized nil
1173   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1174 (defvar gnus-newsgroup-begin nil)
1175 (defvar gnus-newsgroup-end nil)
1176 (defvar gnus-newsgroup-last-rmail nil)
1177 (defvar gnus-newsgroup-last-mail nil)
1178 (defvar gnus-newsgroup-last-folder nil)
1179 (defvar gnus-newsgroup-last-file nil)
1180 (defvar gnus-newsgroup-auto-expire nil)
1181 (defvar gnus-newsgroup-active nil)
1182
1183 (defvar gnus-newsgroup-data nil)
1184 (defvar gnus-newsgroup-data-reverse nil)
1185 (defvar gnus-newsgroup-limit nil)
1186 (defvar gnus-newsgroup-limits nil)
1187
1188 (defvar gnus-newsgroup-unreads nil
1189   "Sorted list of unread articles in the current newsgroup.")
1190
1191 (defvar gnus-newsgroup-unselected nil
1192   "Sorted list of unselected unread articles in the current newsgroup.")
1193
1194 (defvar gnus-newsgroup-reads nil
1195   "Alist of read articles and article marks in the current newsgroup.")
1196
1197 (defvar gnus-newsgroup-expunged-tally nil)
1198
1199 (defvar gnus-newsgroup-marked nil
1200   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1201
1202 (defvar gnus-newsgroup-spam-marked nil
1203   "List of ranges of articles that have been marked as spam.")
1204
1205 (defvar gnus-newsgroup-killed nil
1206   "List of ranges of articles that have been through the scoring process.")
1207
1208 (defvar gnus-newsgroup-cached nil
1209   "Sorted list of articles that come from the article cache.")
1210
1211 (defvar gnus-newsgroup-saved nil
1212   "List of articles that have been saved.")
1213
1214 (defvar gnus-newsgroup-kill-headers nil)
1215
1216 (defvar gnus-newsgroup-replied nil
1217   "List of articles that have been replied to in the current newsgroup.")
1218
1219 (defvar gnus-newsgroup-forwarded nil
1220   "List of articles that have been forwarded in the current newsgroup.")
1221
1222 (defvar gnus-newsgroup-recent nil
1223   "List of articles that have are recent in the current newsgroup.")
1224
1225 (defvar gnus-newsgroup-expirable nil
1226   "Sorted list of articles in the current newsgroup that can be expired.")
1227
1228 (defvar gnus-newsgroup-processable nil
1229   "List of articles in the current newsgroup that can be processed.")
1230
1231 (defvar gnus-newsgroup-downloadable nil
1232   "Sorted list of articles in the current newsgroup that can be processed.")
1233
1234 (defvar gnus-newsgroup-undownloaded nil
1235   "List of articles in the current newsgroup that haven't been downloaded..")
1236
1237 (defvar gnus-newsgroup-unsendable nil
1238   "List of articles in the current newsgroup that won't be sent.")
1239
1240 (defvar gnus-newsgroup-bookmarks nil
1241   "List of articles in the current newsgroup that have bookmarks.")
1242
1243 (defvar gnus-newsgroup-dormant nil
1244   "Sorted list of dormant articles in the current newsgroup.")
1245
1246 (defvar gnus-newsgroup-unseen nil
1247   "List of unseen articles in the current newsgroup.")
1248
1249 (defvar gnus-newsgroup-seen nil
1250   "Range of seen articles in the current newsgroup.")
1251
1252 (defvar gnus-newsgroup-articles nil
1253   "List of articles in the current newsgroup.")
1254
1255 (defvar gnus-newsgroup-scored nil
1256   "List of scored articles in the current newsgroup.")
1257
1258 (defvar gnus-newsgroup-headers nil
1259   "List of article headers in the current newsgroup.")
1260
1261 (defvar gnus-newsgroup-threads nil)
1262
1263 (defvar gnus-newsgroup-prepared nil
1264   "Whether the current group has been prepared properly.")
1265
1266 (defvar gnus-newsgroup-ancient nil
1267   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1268
1269 (defvar gnus-newsgroup-sparse nil)
1270
1271 (defvar gnus-current-article nil)
1272 (defvar gnus-article-current nil)
1273 (defvar gnus-current-headers nil)
1274 (defvar gnus-have-all-headers nil)
1275 (defvar gnus-last-article nil)
1276 (defvar gnus-newsgroup-history nil)
1277 (defvar gnus-newsgroup-charset nil)
1278 (defvar gnus-newsgroup-ephemeral-charset nil)
1279 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1280
1281 (defvar gnus-article-before-search nil)
1282
1283 (defconst gnus-summary-local-variables
1284   '(gnus-newsgroup-name
1285     gnus-newsgroup-begin gnus-newsgroup-end
1286     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1287     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1288     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1289     gnus-newsgroup-unselected gnus-newsgroup-marked
1290     gnus-newsgroup-spam-marked
1291     gnus-newsgroup-reads gnus-newsgroup-saved
1292     gnus-newsgroup-replied gnus-newsgroup-forwarded
1293     gnus-newsgroup-recent
1294     gnus-newsgroup-expirable
1295     gnus-newsgroup-processable gnus-newsgroup-killed
1296     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1297     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1298     gnus-newsgroup-seen gnus-newsgroup-articles
1299     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1300     gnus-newsgroup-headers gnus-newsgroup-threads
1301     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1302     gnus-current-article gnus-current-headers gnus-have-all-headers
1303     gnus-last-article gnus-article-internal-prepare-hook
1304     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1305     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1306     gnus-thread-expunge-below
1307     gnus-score-alist gnus-current-score-file
1308     (gnus-summary-expunge-below . global)
1309     (gnus-summary-mark-below . global)
1310     (gnus-orphan-score . global)
1311     gnus-newsgroup-active gnus-scores-exclude-files
1312     gnus-newsgroup-history gnus-newsgroup-ancient
1313     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1314     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1315     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1316     (gnus-newsgroup-expunged-tally . 0)
1317     gnus-cache-removable-articles gnus-newsgroup-cached
1318     gnus-newsgroup-data gnus-newsgroup-data-reverse
1319     gnus-newsgroup-limit gnus-newsgroup-limits
1320     gnus-newsgroup-charset gnus-newsgroup-display)
1321   "Variables that are buffer-local to the summary buffers.")
1322
1323 (defvar gnus-newsgroup-variables nil
1324   "A list of variables that have separate values in different newsgroups.
1325 A list of newsgroup (summary buffer) local variables, or cons of
1326 variables and their default values (when the default values are not
1327 nil), that should be made global while the summary buffer is active.
1328 These variables can be used to set variables in the group parameters
1329 while still allowing them to affect operations done in other
1330 buffers. For example:
1331
1332 \(setq gnus-newsgroup-variables
1333      '(message-use-followup-to
1334        (gnus-visible-headers .
1335          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1336 ")
1337
1338 ;; Byte-compiler warning.
1339 ;(eval-when-compile (defvar gnus-article-mode-map))
1340 (eval-when-compile
1341   (let ((features (cons 'gnus-sum features)))
1342     (require 'gnus)
1343     (require 'gnus-agent)
1344     (require 'gnus-art)))
1345
1346 ;; MIME stuff.
1347
1348 (defvar gnus-decode-encoded-word-methods
1349   '(mail-decode-encoded-word-string)
1350   "List of methods used to decode encoded words.
1351
1352 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1353 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1354 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1355 whose names match REGEXP.
1356
1357 For example:
1358 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1359  mail-decode-encoded-word-string
1360  (\"chinese\" . rfc1843-decode-string))")
1361
1362 (defvar gnus-decode-encoded-word-methods-cache nil)
1363
1364 (defun gnus-multi-decode-encoded-word-string (string)
1365   "Apply the functions from `gnus-encoded-word-methods' that match."
1366   (unless (and gnus-decode-encoded-word-methods-cache
1367                (eq gnus-newsgroup-name
1368                    (car gnus-decode-encoded-word-methods-cache)))
1369     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1370     (mapcar (lambda (x)
1371               (if (symbolp x)
1372                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1373                 (if (and gnus-newsgroup-name
1374                          (string-match (car x) gnus-newsgroup-name))
1375                     (nconc gnus-decode-encoded-word-methods-cache
1376                            (list (cdr x))))))
1377             gnus-decode-encoded-word-methods))
1378   (let ((xlist gnus-decode-encoded-word-methods-cache))
1379     (pop xlist)
1380     (while xlist
1381       (setq string (funcall (pop xlist) string))))
1382   string)
1383
1384 ;; Subject simplification.
1385
1386 (defun gnus-simplify-whitespace (str)
1387   "Remove excessive whitespace from STR."
1388   (let ((mystr str))
1389     ;; Multiple spaces.
1390     (while (string-match "[ \t][ \t]+" mystr)
1391       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1392                           " "
1393                           (substring mystr (match-end 0)))))
1394     ;; Leading spaces.
1395     (when (string-match "^[ \t]+" mystr)
1396       (setq mystr (substring mystr (match-end 0))))
1397     ;; Trailing spaces.
1398     (when (string-match "[ \t]+$" mystr)
1399       (setq mystr (substring mystr 0 (match-beginning 0))))
1400     mystr))
1401
1402 (defun gnus-simplify-all-whitespace (str)
1403   "Remove all whitespace from STR."
1404   (let ((mystr str))
1405     (while (string-match "[ \t\n]+" mystr)
1406       (setq mystr (replace-match "" nil nil mystr)))
1407     mystr))
1408
1409 (defsubst gnus-simplify-subject-re (subject)
1410   "Remove \"Re:\" from subject lines."
1411   (if (string-match message-subject-re-regexp subject)
1412       (substring subject (match-end 0))
1413     subject))
1414
1415 (defun gnus-simplify-subject (subject &optional re-only)
1416   "Remove `Re:' and words in parentheses.
1417 If RE-ONLY is non-nil, strip leading `Re:'s only."
1418   (let ((case-fold-search t))           ;Ignore case.
1419     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1420     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1421       (setq subject (substring subject (match-end 0))))
1422     ;; Remove uninteresting prefixes.
1423     (when (and (not re-only)
1424                gnus-simplify-ignored-prefixes
1425                (string-match gnus-simplify-ignored-prefixes subject))
1426       (setq subject (substring subject (match-end 0))))
1427     ;; Remove words in parentheses from end.
1428     (unless re-only
1429       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1430         (setq subject (substring subject 0 (match-beginning 0)))))
1431     ;; Return subject string.
1432     subject))
1433
1434 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1435 ;; all whitespace.
1436 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1437   (goto-char (point-min))
1438   (while (re-search-forward regexp nil t)
1439     (replace-match (or newtext ""))))
1440
1441 (defun gnus-simplify-buffer-fuzzy ()
1442   "Simplify string in the buffer fuzzily.
1443 The string in the accessible portion of the current buffer is simplified.
1444 It is assumed to be a single-line subject.
1445 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1446 matter is removed.  Additional things can be deleted by setting
1447 `gnus-simplify-subject-fuzzy-regexp'."
1448   (let ((case-fold-search t)
1449         (modified-tick))
1450     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1451
1452     (while (not (eq modified-tick (buffer-modified-tick)))
1453       (setq modified-tick (buffer-modified-tick))
1454       (cond
1455        ((listp gnus-simplify-subject-fuzzy-regexp)
1456         (mapcar 'gnus-simplify-buffer-fuzzy-step
1457                 gnus-simplify-subject-fuzzy-regexp))
1458        (gnus-simplify-subject-fuzzy-regexp
1459         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1460       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1461       (gnus-simplify-buffer-fuzzy-step
1462        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1463       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1464
1465     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1466     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1467     (gnus-simplify-buffer-fuzzy-step " $")
1468     (gnus-simplify-buffer-fuzzy-step "^ +")))
1469
1470 (defun gnus-simplify-subject-fuzzy (subject)
1471   "Simplify a subject string fuzzily.
1472 See `gnus-simplify-buffer-fuzzy' for details."
1473   (save-excursion
1474     (gnus-set-work-buffer)
1475     (let ((case-fold-search t))
1476       ;; Remove uninteresting prefixes.
1477       (when (and gnus-simplify-ignored-prefixes
1478                  (string-match gnus-simplify-ignored-prefixes subject))
1479         (setq subject (substring subject (match-end 0))))
1480       (insert subject)
1481       (inline (gnus-simplify-buffer-fuzzy))
1482       (buffer-string))))
1483
1484 (defsubst gnus-simplify-subject-fully (subject)
1485   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1486   (cond
1487    (gnus-simplify-subject-functions
1488     (gnus-map-function gnus-simplify-subject-functions subject))
1489    ((null gnus-summary-gather-subject-limit)
1490     (gnus-simplify-subject-re subject))
1491    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1492     (gnus-simplify-subject-fuzzy subject))
1493    ((numberp gnus-summary-gather-subject-limit)
1494     (gnus-limit-string (gnus-simplify-subject-re subject)
1495                        gnus-summary-gather-subject-limit))
1496    (t
1497     subject)))
1498
1499 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1500   "Check whether two subjects are equal.
1501 If optional argument simple-first is t, first argument is already
1502 simplified."
1503   (cond
1504    ((null simple-first)
1505     (equal (gnus-simplify-subject-fully s1)
1506            (gnus-simplify-subject-fully s2)))
1507    (t
1508     (equal s1
1509            (gnus-simplify-subject-fully s2)))))
1510
1511 (defun gnus-summary-bubble-group ()
1512   "Increase the score of the current group.
1513 This is a handy function to add to `gnus-summary-exit-hook' to
1514 increase the score of each group you read."
1515   (gnus-group-add-score gnus-newsgroup-name))
1516
1517 \f
1518 ;;;
1519 ;;; Gnus summary mode
1520 ;;;
1521
1522 (put 'gnus-summary-mode 'mode-class 'special)
1523
1524 (defvar gnus-article-commands-menu)
1525
1526 (when t
1527   ;; Non-orthogonal keys
1528
1529   (gnus-define-keys gnus-summary-mode-map
1530     " " gnus-summary-next-page
1531     "\177" gnus-summary-prev-page
1532     [delete] gnus-summary-prev-page
1533     [backspace] gnus-summary-prev-page
1534     "\r" gnus-summary-scroll-up
1535     "\M-\r" gnus-summary-scroll-down
1536     "n" gnus-summary-next-unread-article
1537     "p" gnus-summary-prev-unread-article
1538     "N" gnus-summary-next-article
1539     "P" gnus-summary-prev-article
1540     "\M-\C-n" gnus-summary-next-same-subject
1541     "\M-\C-p" gnus-summary-prev-same-subject
1542     "\M-n" gnus-summary-next-unread-subject
1543     "\M-p" gnus-summary-prev-unread-subject
1544     "." gnus-summary-first-unread-article
1545     "," gnus-summary-best-unread-article
1546     "\M-s" gnus-summary-search-article-forward
1547     "\M-r" gnus-summary-search-article-backward
1548     "<" gnus-summary-beginning-of-article
1549     ">" gnus-summary-end-of-article
1550     "j" gnus-summary-goto-article
1551     "^" gnus-summary-refer-parent-article
1552     "\M-^" gnus-summary-refer-article
1553     "u" gnus-summary-tick-article-forward
1554     "!" gnus-summary-tick-article-forward
1555     "U" gnus-summary-tick-article-backward
1556     "d" gnus-summary-mark-as-read-forward
1557     "D" gnus-summary-mark-as-read-backward
1558     "E" gnus-summary-mark-as-expirable
1559     "\M-u" gnus-summary-clear-mark-forward
1560     "\M-U" gnus-summary-clear-mark-backward
1561     "k" gnus-summary-kill-same-subject-and-select
1562     "\C-k" gnus-summary-kill-same-subject
1563     "\M-\C-k" gnus-summary-kill-thread
1564     "\M-\C-l" gnus-summary-lower-thread
1565     "e" gnus-summary-edit-article
1566     "#" gnus-summary-mark-as-processable
1567     "\M-#" gnus-summary-unmark-as-processable
1568     "\M-\C-t" gnus-summary-toggle-threads
1569     "\M-\C-s" gnus-summary-show-thread
1570     "\M-\C-h" gnus-summary-hide-thread
1571     "\M-\C-f" gnus-summary-next-thread
1572     "\M-\C-b" gnus-summary-prev-thread
1573     [(meta down)] gnus-summary-next-thread
1574     [(meta up)] gnus-summary-prev-thread
1575     "\M-\C-u" gnus-summary-up-thread
1576     "\M-\C-d" gnus-summary-down-thread
1577     "&" gnus-summary-execute-command
1578     "c" gnus-summary-catchup-and-exit
1579     "\C-w" gnus-summary-mark-region-as-read
1580     "\C-t" gnus-summary-toggle-truncation
1581     "?" gnus-summary-mark-as-dormant
1582     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1583     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1584     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1585     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1586     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1587     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1588     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1589     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1590     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1591     "\C-c\C-s\C-r" gnus-summary-sort-by-random
1592     "=" gnus-summary-expand-window
1593     "\C-x\C-s" gnus-summary-reselect-current-group
1594     "\M-g" gnus-summary-rescan-group
1595     "w" gnus-summary-stop-page-breaking
1596     "\C-c\C-r" gnus-summary-caesar-message
1597     "f" gnus-summary-followup
1598     "F" gnus-summary-followup-with-original
1599     "C" gnus-summary-cancel-article
1600     "r" gnus-summary-reply
1601     "R" gnus-summary-reply-with-original
1602     "\C-c\C-f" gnus-summary-mail-forward
1603     "o" gnus-summary-save-article
1604     "\C-o" gnus-summary-save-article-mail
1605     "|" gnus-summary-pipe-output
1606     "\M-k" gnus-summary-edit-local-kill
1607     "\M-K" gnus-summary-edit-global-kill
1608     ;; "V" gnus-version
1609     "\C-c\C-d" gnus-summary-describe-group
1610     "q" gnus-summary-exit
1611     "Q" gnus-summary-exit-no-update
1612     "\C-c\C-i" gnus-info-find-node
1613     gnus-mouse-2 gnus-mouse-pick-article
1614     "m" gnus-summary-mail-other-window
1615     "a" gnus-summary-post-news
1616     "i" gnus-summary-news-other-window
1617     "x" gnus-summary-limit-to-unread
1618     "s" gnus-summary-isearch-article
1619     "t" gnus-summary-toggle-header
1620     "g" gnus-summary-show-article
1621     "l" gnus-summary-goto-last-article
1622     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1623     "\C-d" gnus-summary-enter-digest-group
1624     "\M-\C-d" gnus-summary-read-document
1625     "\M-\C-e" gnus-summary-edit-parameters
1626     "\M-\C-a" gnus-summary-customize-parameters
1627     "\C-c\C-b" gnus-bug
1628     "*" gnus-cache-enter-article
1629     "\M-*" gnus-cache-remove-article
1630     "\M-&" gnus-summary-universal-argument
1631     "\C-l" gnus-recenter
1632     "I" gnus-summary-increase-score
1633     "L" gnus-summary-lower-score
1634     "\M-i" gnus-symbolic-argument
1635     "h" gnus-summary-select-article-buffer
1636
1637     "b" gnus-article-view-part
1638     "\M-t" gnus-summary-toggle-display-buttonized
1639
1640     "V" gnus-summary-score-map
1641     "X" gnus-uu-extract-map
1642     "S" gnus-summary-send-map)
1643
1644   ;; Sort of orthogonal keymap
1645   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1646     "t" gnus-summary-tick-article-forward
1647     "!" gnus-summary-tick-article-forward
1648     "d" gnus-summary-mark-as-read-forward
1649     "r" gnus-summary-mark-as-read-forward
1650     "c" gnus-summary-clear-mark-forward
1651     " " gnus-summary-clear-mark-forward
1652     "e" gnus-summary-mark-as-expirable
1653     "x" gnus-summary-mark-as-expirable
1654     "?" gnus-summary-mark-as-dormant
1655     "b" gnus-summary-set-bookmark
1656     "B" gnus-summary-remove-bookmark
1657     "#" gnus-summary-mark-as-processable
1658     "\M-#" gnus-summary-unmark-as-processable
1659     "S" gnus-summary-limit-include-expunged
1660     "C" gnus-summary-catchup
1661     "H" gnus-summary-catchup-to-here
1662     "h" gnus-summary-catchup-from-here
1663     "\C-c" gnus-summary-catchup-all
1664     "k" gnus-summary-kill-same-subject-and-select
1665     "K" gnus-summary-kill-same-subject
1666     "P" gnus-uu-mark-map)
1667
1668   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1669     "c" gnus-summary-clear-above
1670     "u" gnus-summary-tick-above
1671     "m" gnus-summary-mark-above
1672     "k" gnus-summary-kill-below)
1673
1674   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1675     "/" gnus-summary-limit-to-subject
1676     "n" gnus-summary-limit-to-articles
1677     "w" gnus-summary-pop-limit
1678     "s" gnus-summary-limit-to-subject
1679     "a" gnus-summary-limit-to-author
1680     "u" gnus-summary-limit-to-unread
1681     "m" gnus-summary-limit-to-marks
1682     "M" gnus-summary-limit-exclude-marks
1683     "v" gnus-summary-limit-to-score
1684     "*" gnus-summary-limit-include-cached
1685     "D" gnus-summary-limit-include-dormant
1686     "T" gnus-summary-limit-include-thread
1687     "d" gnus-summary-limit-exclude-dormant
1688     "t" gnus-summary-limit-to-age
1689     "." gnus-summary-limit-to-unseen
1690     "x" gnus-summary-limit-to-extra
1691     "p" gnus-summary-limit-to-display-predicate
1692     "E" gnus-summary-limit-include-expunged
1693     "c" gnus-summary-limit-exclude-childless-dormant
1694     "C" gnus-summary-limit-mark-excluded-as-read
1695     "o" gnus-summary-insert-old-articles
1696     "N" gnus-summary-insert-new-articles)
1697
1698   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1699     "n" gnus-summary-next-unread-article
1700     "p" gnus-summary-prev-unread-article
1701     "N" gnus-summary-next-article
1702     "P" gnus-summary-prev-article
1703     "\C-n" gnus-summary-next-same-subject
1704     "\C-p" gnus-summary-prev-same-subject
1705     "\M-n" gnus-summary-next-unread-subject
1706     "\M-p" gnus-summary-prev-unread-subject
1707     "f" gnus-summary-first-unread-article
1708     "b" gnus-summary-best-unread-article
1709     "j" gnus-summary-goto-article
1710     "g" gnus-summary-goto-subject
1711     "l" gnus-summary-goto-last-article
1712     "o" gnus-summary-pop-article)
1713
1714   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1715     "k" gnus-summary-kill-thread
1716     "l" gnus-summary-lower-thread
1717     "i" gnus-summary-raise-thread
1718     "T" gnus-summary-toggle-threads
1719     "t" gnus-summary-rethread-current
1720     "^" gnus-summary-reparent-thread
1721     "s" gnus-summary-show-thread
1722     "S" gnus-summary-show-all-threads
1723     "h" gnus-summary-hide-thread
1724     "H" gnus-summary-hide-all-threads
1725     "n" gnus-summary-next-thread
1726     "p" gnus-summary-prev-thread
1727     "u" gnus-summary-up-thread
1728     "o" gnus-summary-top-thread
1729     "d" gnus-summary-down-thread
1730     "#" gnus-uu-mark-thread
1731     "\M-#" gnus-uu-unmark-thread)
1732
1733   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1734     "g" gnus-summary-prepare
1735     "c" gnus-summary-insert-cached-articles
1736     "d" gnus-summary-insert-dormant-articles)
1737
1738   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1739     "c" gnus-summary-catchup-and-exit
1740     "C" gnus-summary-catchup-all-and-exit
1741     "E" gnus-summary-exit-no-update
1742     "Q" gnus-summary-exit
1743     "Z" gnus-summary-exit
1744     "n" gnus-summary-catchup-and-goto-next-group
1745     "R" gnus-summary-reselect-current-group
1746     "G" gnus-summary-rescan-group
1747     "N" gnus-summary-next-group
1748     "s" gnus-summary-save-newsrc
1749     "P" gnus-summary-prev-group)
1750
1751   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1752     " " gnus-summary-next-page
1753     "n" gnus-summary-next-page
1754     "\177" gnus-summary-prev-page
1755     [delete] gnus-summary-prev-page
1756     "p" gnus-summary-prev-page
1757     "\r" gnus-summary-scroll-up
1758     "\M-\r" gnus-summary-scroll-down
1759     "<" gnus-summary-beginning-of-article
1760     ">" gnus-summary-end-of-article
1761     "b" gnus-summary-beginning-of-article
1762     "e" gnus-summary-end-of-article
1763     "^" gnus-summary-refer-parent-article
1764     "r" gnus-summary-refer-parent-article
1765     "D" gnus-summary-enter-digest-group
1766     "R" gnus-summary-refer-references
1767     "T" gnus-summary-refer-thread
1768     "g" gnus-summary-show-article
1769     "s" gnus-summary-isearch-article
1770     "P" gnus-summary-print-article
1771     "M" gnus-mailing-list-insinuate
1772     "t" gnus-article-babel)
1773
1774   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1775     "b" gnus-article-add-buttons
1776     "B" gnus-article-add-buttons-to-head
1777     "o" gnus-article-treat-overstrike
1778     "e" gnus-article-emphasize
1779     "w" gnus-article-fill-cited-article
1780     "Q" gnus-article-fill-long-lines
1781     "C" gnus-article-capitalize-sentences
1782     "c" gnus-article-remove-cr
1783     "q" gnus-article-de-quoted-unreadable
1784     "6" gnus-article-de-base64-unreadable
1785     "Z" gnus-article-decode-HZ
1786     "h" gnus-article-wash-html
1787     "u" gnus-article-unsplit-urls
1788     "s" gnus-summary-force-verify-and-decrypt
1789     "f" gnus-article-display-x-face
1790     "l" gnus-summary-stop-page-breaking
1791     "r" gnus-summary-caesar-message
1792     "m" gnus-summary-morse-message
1793     "t" gnus-summary-toggle-header
1794     "g" gnus-treat-smiley
1795     "v" gnus-summary-verbose-headers
1796     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1797     "p" gnus-article-verify-x-pgp-sig
1798     "d" gnus-article-treat-dumbquotes)
1799
1800   (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1801     ;; mnemonic: deuglif*Y*
1802     "u" gnus-article-outlook-unwrap-lines
1803     "a" gnus-article-outlook-repair-attribution
1804     "c" gnus-article-outlook-rearrange-citation
1805     "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1806
1807   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1808     "a" gnus-article-hide
1809     "h" gnus-article-hide-headers
1810     "b" gnus-article-hide-boring-headers
1811     "s" gnus-article-hide-signature
1812     "c" gnus-article-hide-citation
1813     "C" gnus-article-hide-citation-in-followups
1814     "l" gnus-article-hide-list-identifiers
1815     "p" gnus-article-hide-pgp
1816     "B" gnus-article-strip-banner
1817     "P" gnus-article-hide-pem
1818     "\C-c" gnus-article-hide-citation-maybe)
1819
1820   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1821     "a" gnus-article-highlight
1822     "h" gnus-article-highlight-headers
1823     "c" gnus-article-highlight-citation
1824     "s" gnus-article-highlight-signature)
1825
1826   (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1827     "f" gnus-article-treat-fold-headers
1828     "u" gnus-article-treat-unfold-headers
1829     "n" gnus-article-treat-fold-newsgroups)
1830
1831   (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1832     "x" gnus-article-display-x-face
1833     "d" gnus-article-display-face
1834     "s" gnus-treat-smiley
1835     "D" gnus-article-remove-images
1836     "f" gnus-treat-from-picon
1837     "m" gnus-treat-mail-picon
1838     "n" gnus-treat-newsgroups-picon)
1839
1840   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1841     "w" gnus-article-decode-mime-words
1842     "c" gnus-article-decode-charset
1843     "v" gnus-mime-view-all-parts
1844     "b" gnus-article-view-part)
1845
1846   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1847     "z" gnus-article-date-ut
1848     "u" gnus-article-date-ut
1849     "l" gnus-article-date-local
1850     "p" gnus-article-date-english
1851     "e" gnus-article-date-lapsed
1852     "o" gnus-article-date-original
1853     "i" gnus-article-date-iso8601
1854     "s" gnus-article-date-user)
1855
1856   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1857     "t" gnus-article-remove-trailing-blank-lines
1858     "l" gnus-article-strip-leading-blank-lines
1859     "m" gnus-article-strip-multiple-blank-lines
1860     "a" gnus-article-strip-blank-lines
1861     "A" gnus-article-strip-all-blank-lines
1862     "s" gnus-article-strip-leading-space
1863     "e" gnus-article-strip-trailing-space
1864     "w" gnus-article-remove-leading-whitespace)
1865
1866   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1867     "v" gnus-version
1868     "f" gnus-summary-fetch-faq
1869     "d" gnus-summary-describe-group
1870     "h" gnus-summary-describe-briefly
1871     "i" gnus-info-find-node
1872     "c" gnus-group-fetch-charter
1873     "C" gnus-group-fetch-control)
1874
1875   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1876     "e" gnus-summary-expire-articles
1877     "\M-\C-e" gnus-summary-expire-articles-now
1878     "\177" gnus-summary-delete-article
1879     [delete] gnus-summary-delete-article
1880     [backspace] gnus-summary-delete-article
1881     "m" gnus-summary-move-article
1882     "r" gnus-summary-respool-article
1883     "w" gnus-summary-edit-article
1884     "c" gnus-summary-copy-article
1885     "B" gnus-summary-crosspost-article
1886     "q" gnus-summary-respool-query
1887     "t" gnus-summary-respool-trace
1888     "i" gnus-summary-import-article
1889     "I" gnus-summary-create-article
1890     "p" gnus-summary-article-posted-p)
1891
1892   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1893     "o" gnus-summary-save-article
1894     "m" gnus-summary-save-article-mail
1895     "F" gnus-summary-write-article-file
1896     "r" gnus-summary-save-article-rmail
1897     "f" gnus-summary-save-article-file
1898     "b" gnus-summary-save-article-body-file
1899     "h" gnus-summary-save-article-folder
1900     "v" gnus-summary-save-article-vm
1901     "p" gnus-summary-pipe-output
1902     "P" gnus-summary-muttprint
1903     "s" gnus-soup-add-article)
1904
1905   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1906     "b" gnus-summary-display-buttonized
1907     "m" gnus-summary-repair-multipart
1908     "v" gnus-article-view-part
1909     "o" gnus-article-save-part
1910     "c" gnus-article-copy-part
1911     "C" gnus-article-view-part-as-charset
1912     "e" gnus-article-view-part-externally
1913     "E" gnus-article-encrypt-body
1914     "i" gnus-article-inline-part
1915     "|" gnus-article-pipe-part)
1916
1917   (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1918     "p" gnus-summary-mark-as-processable
1919     "u" gnus-summary-unmark-as-processable
1920     "U" gnus-summary-unmark-all-processable
1921     "v" gnus-uu-mark-over
1922     "s" gnus-uu-mark-series
1923     "r" gnus-uu-mark-region
1924     "g" gnus-uu-unmark-region
1925     "R" gnus-uu-mark-by-regexp
1926     "G" gnus-uu-unmark-by-regexp
1927     "t" gnus-uu-mark-thread
1928     "T" gnus-uu-unmark-thread
1929     "a" gnus-uu-mark-all
1930     "b" gnus-uu-mark-buffer
1931     "S" gnus-uu-mark-sparse
1932     "k" gnus-summary-kill-process-mark
1933     "y" gnus-summary-yank-process-mark
1934     "w" gnus-summary-save-process-mark
1935     "i" gnus-uu-invert-processable)
1936
1937   (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1938     ;;"x" gnus-uu-extract-any
1939     "m" gnus-summary-save-parts
1940     "u" gnus-uu-decode-uu
1941     "U" gnus-uu-decode-uu-and-save
1942     "s" gnus-uu-decode-unshar
1943     "S" gnus-uu-decode-unshar-and-save
1944     "o" gnus-uu-decode-save
1945     "O" gnus-uu-decode-save
1946     "b" gnus-uu-decode-binhex
1947     "B" gnus-uu-decode-binhex
1948     "p" gnus-uu-decode-postscript
1949     "P" gnus-uu-decode-postscript-and-save)
1950
1951   (gnus-define-keys
1952       (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
1953     "u" gnus-uu-decode-uu-view
1954     "U" gnus-uu-decode-uu-and-save-view
1955     "s" gnus-uu-decode-unshar-view
1956     "S" gnus-uu-decode-unshar-and-save-view
1957     "o" gnus-uu-decode-save-view
1958     "O" gnus-uu-decode-save-view
1959     "b" gnus-uu-decode-binhex-view
1960     "B" gnus-uu-decode-binhex-view
1961     "p" gnus-uu-decode-postscript-view
1962     "P" gnus-uu-decode-postscript-and-save-view))
1963
1964 (defvar gnus-article-post-menu nil)
1965
1966 (defconst gnus-summary-menu-maxlen 20)
1967
1968 (defun gnus-summary-menu-split (menu)
1969   ;; If we have lots of elements, divide them into groups of 20
1970   ;; and make a pane (or submenu) for each one.
1971   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
1972       (let ((menu menu) sublists next
1973             (i 1))
1974         (while menu
1975           ;; Pull off the next gnus-summary-menu-maxlen elements
1976           ;; and make them the next element of sublist.
1977           (setq next (nthcdr gnus-summary-menu-maxlen menu))
1978           (if next
1979               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
1980                       nil))
1981           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
1982                                              (aref (car (last menu)) 0)) menu)
1983                                sublists))
1984           (setq i (1+ i))
1985           (setq menu next))
1986         (nreverse sublists))
1987     ;; Few elements--put them all in one pane.
1988     menu))
1989
1990 (defun gnus-summary-make-menu-bar ()
1991   (gnus-turn-off-edit-menu 'summary)
1992
1993   (unless (boundp 'gnus-summary-misc-menu)
1994
1995     (easy-menu-define
1996       gnus-summary-kill-menu gnus-summary-mode-map ""
1997       (cons
1998        "Score"
1999        (nconc
2000         (list
2001          ["Customize" gnus-score-customize t])
2002         (gnus-make-score-map 'increase)
2003         (gnus-make-score-map 'lower)
2004         '(("Mark"
2005            ["Kill below" gnus-summary-kill-below t]
2006            ["Mark above" gnus-summary-mark-above t]
2007            ["Tick above" gnus-summary-tick-above t]
2008            ["Clear above" gnus-summary-clear-above t])
2009           ["Current score" gnus-summary-current-score t]
2010           ["Set score" gnus-summary-set-score t]
2011           ["Switch current score file..." gnus-score-change-score-file t]
2012           ["Set mark below..." gnus-score-set-mark-below t]
2013           ["Set expunge below..." gnus-score-set-expunge-below t]
2014           ["Edit current score file" gnus-score-edit-current-scores t]
2015           ["Edit score file" gnus-score-edit-file t]
2016           ["Trace score" gnus-score-find-trace t]
2017           ["Find words" gnus-score-find-favourite-words t]
2018           ["Rescore buffer" gnus-summary-rescore t]
2019           ["Increase score..." gnus-summary-increase-score t]
2020           ["Lower score..." gnus-summary-lower-score t]))))
2021
2022     ;; Define both the Article menu in the summary buffer and the
2023     ;; equivalent Commands menu in the article buffer here for
2024     ;; consistency.
2025     (let ((innards
2026            `(("Hide"
2027               ["All" gnus-article-hide t]
2028               ["Headers" gnus-article-hide-headers t]
2029               ["Signature" gnus-article-hide-signature t]
2030               ["Citation" gnus-article-hide-citation t]
2031               ["List identifiers" gnus-article-hide-list-identifiers t]
2032               ["PGP" gnus-article-hide-pgp t]
2033               ["Banner" gnus-article-strip-banner t]
2034               ["Boring headers" gnus-article-hide-boring-headers t])
2035              ("Highlight"
2036               ["All" gnus-article-highlight t]
2037               ["Headers" gnus-article-highlight-headers t]
2038               ["Signature" gnus-article-highlight-signature t]
2039               ["Citation" gnus-article-highlight-citation t])
2040              ("MIME"
2041               ["Words" gnus-article-decode-mime-words t]
2042               ["Charset" gnus-article-decode-charset t]
2043               ["QP" gnus-article-de-quoted-unreadable t]
2044               ["Base64" gnus-article-de-base64-unreadable t]
2045               ["View MIME buttons" gnus-summary-display-buttonized t]
2046               ["View all" gnus-mime-view-all-parts t]
2047               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2048               ["Encrypt body" gnus-article-encrypt-body t]
2049               ["Extract all parts" gnus-summary-save-parts t])
2050              ("Date"
2051               ["Local" gnus-article-date-local t]
2052               ["ISO8601" gnus-article-date-iso8601 t]
2053               ["UT" gnus-article-date-ut t]
2054               ["Original" gnus-article-date-original t]
2055               ["Lapsed" gnus-article-date-lapsed t]
2056               ["User-defined" gnus-article-date-user t])
2057              ("Display"
2058               ["Remove images" gnus-article-remove-images t]
2059               ["Toggle smiley" gnus-treat-smiley t]
2060               ["Show X-Face" gnus-article-display-x-face t]
2061               ["Show picons in From" gnus-treat-from-picon t]
2062               ["Show picons in mail headers" gnus-treat-mail-picon t]
2063               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2064               ("View as different encoding"
2065                ,@(gnus-summary-menu-split
2066                   (mapcar
2067                    (lambda (cs)
2068                      ;; Since easymenu under FSF Emacs doesn't allow lambda
2069                      ;; forms for menu commands, we should provide intern'ed
2070                      ;; function symbols.
2071                      (let ((command (intern (format "\
2072 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2073                        (fset command
2074                              `(lambda ()
2075                                 (interactive)
2076                                 (let ((gnus-summary-show-article-charset-alist
2077                                        '((1 . ,cs))))
2078                                   (gnus-summary-show-article 1))))
2079                        `[,(symbol-name cs) ,command t]))
2080                    (sort (if (fboundp 'coding-system-list)
2081                              (coding-system-list)
2082                            (mapcar 'car mm-mime-mule-charset-alist))
2083                          'string<)))))
2084              ("Washing"
2085               ("Remove Blanks"
2086                ["Leading" gnus-article-strip-leading-blank-lines t]
2087                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2088                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2089                ["All of the above" gnus-article-strip-blank-lines t]
2090                ["All" gnus-article-strip-all-blank-lines t]
2091                ["Leading space" gnus-article-strip-leading-space t]
2092                ["Trailing space" gnus-article-strip-trailing-space t]
2093                ["Leading space in headers"
2094                 gnus-article-remove-leading-whitespace t])
2095               ["Overstrike" gnus-article-treat-overstrike t]
2096               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2097               ["Emphasis" gnus-article-emphasize t]
2098               ["Word wrap" gnus-article-fill-cited-article t]
2099               ["Fill long lines" gnus-article-fill-long-lines t]
2100               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2101               ["CR" gnus-article-remove-cr t]
2102               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2103               ["Base64" gnus-article-de-base64-unreadable t]
2104               ["Rot 13" gnus-summary-caesar-message
2105                ,@(if (featurep 'xemacs) '(t)
2106                    '(:help "\"Caesar rotate\" article by 13"))]
2107               ["Morse decode" gnus-summary-morse-message t]
2108               ["Unix pipe..." gnus-summary-pipe-message t]
2109               ["Add buttons" gnus-article-add-buttons t]
2110               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2111               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2112               ["Verbose header" gnus-summary-verbose-headers t]
2113               ["Toggle header" gnus-summary-toggle-header t]
2114               ["Unfold headers" gnus-article-treat-unfold-headers t]
2115               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2116               ["Html" gnus-article-wash-html t]
2117               ["URLs" gnus-article-unsplit-urls t]
2118               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2119               ["HZ" gnus-article-decode-HZ t]
2120               ("(Outlook) Deuglify"
2121                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2122                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2123                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2124                ["Full (Outlook) deuglify"
2125                 gnus-article-outlook-deuglify-article t])
2126               )
2127              ("Output"
2128               ["Save in default format" gnus-summary-save-article
2129                ,@(if (featurep 'xemacs) '(t)
2130                    '(:help "Save article using default method"))]
2131               ["Save in file" gnus-summary-save-article-file
2132                ,@(if (featurep 'xemacs) '(t)
2133                    '(:help "Save article in file"))]
2134               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2135               ["Save in MH folder" gnus-summary-save-article-folder t]
2136               ["Save in VM folder" gnus-summary-save-article-vm t]
2137               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2138               ["Save body in file" gnus-summary-save-article-body-file t]
2139               ["Pipe through a filter" gnus-summary-pipe-output t]
2140               ["Add to SOUP packet" gnus-soup-add-article t]
2141               ["Print with Muttprint" gnus-summary-muttprint t]
2142               ["Print" gnus-summary-print-article t])
2143              ("Backend"
2144               ["Respool article..." gnus-summary-respool-article t]
2145               ["Move article..." gnus-summary-move-article
2146                (gnus-check-backend-function
2147                 'request-move-article gnus-newsgroup-name)]
2148               ["Copy article..." gnus-summary-copy-article t]
2149               ["Crosspost article..." gnus-summary-crosspost-article
2150                (gnus-check-backend-function
2151                 'request-replace-article gnus-newsgroup-name)]
2152               ["Import file..." gnus-summary-import-article t]
2153               ["Create article..." gnus-summary-create-article t]
2154               ["Check if posted" gnus-summary-article-posted-p t]
2155               ["Edit article" gnus-summary-edit-article
2156                (not (gnus-group-read-only-p))]
2157               ["Delete article" gnus-summary-delete-article
2158                (gnus-check-backend-function
2159                 'request-expire-articles gnus-newsgroup-name)]
2160               ["Query respool" gnus-summary-respool-query t]
2161               ["Trace respool" gnus-summary-respool-trace t]
2162               ["Delete expirable articles" gnus-summary-expire-articles-now
2163                (gnus-check-backend-function
2164                 'request-expire-articles gnus-newsgroup-name)])
2165              ("Extract"
2166               ["Uudecode" gnus-uu-decode-uu
2167                ,@(if (featurep 'xemacs) '(t)
2168                    '(:help "Decode uuencoded article(s)"))]
2169               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2170               ["Unshar" gnus-uu-decode-unshar t]
2171               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2172               ["Save" gnus-uu-decode-save t]
2173               ["Binhex" gnus-uu-decode-binhex t]
2174               ["Postscript" gnus-uu-decode-postscript t]
2175               ["All MIME parts" gnus-summary-save-parts t])
2176              ("Cache"
2177               ["Enter article" gnus-cache-enter-article t]
2178               ["Remove article" gnus-cache-remove-article t])
2179              ["Translate" gnus-article-babel t]
2180              ["Select article buffer" gnus-summary-select-article-buffer t]
2181              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2182              ["Isearch article..." gnus-summary-isearch-article t]
2183              ["Beginning of the article" gnus-summary-beginning-of-article t]
2184              ["End of the article" gnus-summary-end-of-article t]
2185              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2186              ["Fetch referenced articles" gnus-summary-refer-references t]
2187              ["Fetch current thread" gnus-summary-refer-thread t]
2188              ["Fetch article with id..." gnus-summary-refer-article t]
2189              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2190              ["Redisplay" gnus-summary-show-article t]
2191              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2192       (easy-menu-define
2193         gnus-summary-article-menu gnus-summary-mode-map ""
2194         (cons "Article" innards))
2195
2196       (if (not (keymapp gnus-summary-article-menu))
2197           (easy-menu-define
2198             gnus-article-commands-menu gnus-article-mode-map ""
2199             (cons "Commands" innards))
2200         ;; in Emacs, don't share menu.
2201         (setq gnus-article-commands-menu
2202               (copy-keymap gnus-summary-article-menu))
2203         (define-key gnus-article-mode-map [menu-bar commands]
2204           (cons "Commands" gnus-article-commands-menu))))
2205
2206     (easy-menu-define
2207       gnus-summary-thread-menu gnus-summary-mode-map ""
2208       '("Threads"
2209         ["Find all messages in thread" gnus-summary-refer-thread t]
2210         ["Toggle threading" gnus-summary-toggle-threads t]
2211         ["Hide threads" gnus-summary-hide-all-threads t]
2212         ["Show threads" gnus-summary-show-all-threads t]
2213         ["Hide thread" gnus-summary-hide-thread t]
2214         ["Show thread" gnus-summary-show-thread t]
2215         ["Go to next thread" gnus-summary-next-thread t]
2216         ["Go to previous thread" gnus-summary-prev-thread t]
2217         ["Go down thread" gnus-summary-down-thread t]
2218         ["Go up thread" gnus-summary-up-thread t]
2219         ["Top of thread" gnus-summary-top-thread t]
2220         ["Mark thread as read" gnus-summary-kill-thread t]
2221         ["Lower thread score" gnus-summary-lower-thread t]
2222         ["Raise thread score" gnus-summary-raise-thread t]
2223         ["Rethread current" gnus-summary-rethread-current t]))
2224
2225     (easy-menu-define
2226       gnus-summary-post-menu gnus-summary-mode-map ""
2227       `("Post"
2228         ["Send a message (mail or news)" gnus-summary-post-news
2229          ,@(if (featurep 'xemacs) '(t)
2230              '(:help "Post an article"))]
2231         ["Followup" gnus-summary-followup
2232          ,@(if (featurep 'xemacs) '(t)
2233              '(:help "Post followup to this article"))]
2234         ["Followup and yank" gnus-summary-followup-with-original
2235          ,@(if (featurep 'xemacs) '(t)
2236              '(:help "Post followup to this article, quoting its contents"))]
2237         ["Supersede article" gnus-summary-supersede-article t]
2238         ["Cancel article" gnus-summary-cancel-article
2239          ,@(if (featurep 'xemacs) '(t)
2240              '(:help "Cancel an article you posted"))]
2241         ["Reply" gnus-summary-reply t]
2242         ["Reply and yank" gnus-summary-reply-with-original t]
2243         ["Wide reply" gnus-summary-wide-reply t]
2244         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2245          ,@(if (featurep 'xemacs) '(t)
2246              '(:help "Mail a reply, quoting this article"))]
2247         ["Very wide reply" gnus-summary-very-wide-reply t]
2248         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2249          ,@(if (featurep 'xemacs) '(t)
2250              '(:help "Mail a very wide reply, quoting this article"))]
2251         ["Mail forward" gnus-summary-mail-forward t]
2252         ["Post forward" gnus-summary-post-forward t]
2253         ["Digest and mail" gnus-uu-digest-mail-forward t]
2254         ["Digest and post" gnus-uu-digest-post-forward t]
2255         ["Resend message" gnus-summary-resend-message t]
2256         ["Resend message edit" gnus-summary-resend-message-edit t]
2257         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2258         ["Send a mail" gnus-summary-mail-other-window t]
2259         ["Create a local message" gnus-summary-news-other-window t]
2260         ["Uuencode and post" gnus-uu-post-news
2261          ,@(if (featurep 'xemacs) '(t)
2262              '(:help "Post a uuencoded article"))]
2263         ["Followup via news" gnus-summary-followup-to-mail t]
2264         ["Followup via news and yank"
2265          gnus-summary-followup-to-mail-with-original t]
2266         ;;("Draft"
2267         ;;["Send" gnus-summary-send-draft t]
2268         ;;["Send bounced" gnus-resend-bounced-mail t])
2269         ))
2270
2271     (cond
2272      ((not (keymapp gnus-summary-post-menu))
2273       (setq gnus-article-post-menu gnus-summary-post-menu))
2274      ((not gnus-article-post-menu)
2275       ;; Don't share post menu.
2276       (setq gnus-article-post-menu
2277             (copy-keymap gnus-summary-post-menu))))
2278     (define-key gnus-article-mode-map [menu-bar post]
2279       (cons "Post" gnus-article-post-menu))
2280
2281     (easy-menu-define
2282       gnus-summary-misc-menu gnus-summary-mode-map ""
2283       `("Gnus"
2284         ("Mark Read"
2285          ["Mark as read" gnus-summary-mark-as-read-forward t]
2286          ["Mark same subject and select"
2287           gnus-summary-kill-same-subject-and-select t]
2288          ["Mark same subject" gnus-summary-kill-same-subject t]
2289          ["Catchup" gnus-summary-catchup
2290           ,@(if (featurep 'xemacs) '(t)
2291               '(:help "Mark unread articles in this group as read"))]
2292          ["Catchup all" gnus-summary-catchup-all t]
2293          ["Catchup to here" gnus-summary-catchup-to-here t]
2294          ["Catchup from here" gnus-summary-catchup-from-here t]
2295          ["Catchup region" gnus-summary-mark-region-as-read 
2296           (gnus-mark-active-p)]
2297          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2298         ("Mark Various"
2299          ["Tick" gnus-summary-tick-article-forward t]
2300          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2301          ["Remove marks" gnus-summary-clear-mark-forward t]
2302          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2303          ["Set bookmark" gnus-summary-set-bookmark t]
2304          ["Remove bookmark" gnus-summary-remove-bookmark t])
2305         ("Limit to"
2306          ["Marks..." gnus-summary-limit-to-marks t]
2307          ["Subject..." gnus-summary-limit-to-subject t]
2308          ["Author..." gnus-summary-limit-to-author t]
2309          ["Age..." gnus-summary-limit-to-age t]
2310          ["Extra..." gnus-summary-limit-to-extra t]
2311          ["Score..." gnus-summary-limit-to-score t]
2312          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2313          ["Unread" gnus-summary-limit-to-unread t]
2314          ["Unseen" gnus-summary-limit-to-unseen t]
2315          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2316          ["Next articles" gnus-summary-limit-to-articles t]
2317          ["Pop limit" gnus-summary-pop-limit t]
2318          ["Show dormant" gnus-summary-limit-include-dormant t]
2319          ["Hide childless dormant"
2320           gnus-summary-limit-exclude-childless-dormant t]
2321          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2322          ["Hide marked" gnus-summary-limit-exclude-marks t]
2323          ["Show expunged" gnus-summary-limit-include-expunged t])
2324         ("Process Mark"
2325          ["Set mark" gnus-summary-mark-as-processable t]
2326          ["Remove mark" gnus-summary-unmark-as-processable t]
2327          ["Remove all marks" gnus-summary-unmark-all-processable t]
2328          ["Mark above" gnus-uu-mark-over t]
2329          ["Mark series" gnus-uu-mark-series t]
2330          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2331          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2332          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2333          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2334          ["Mark all" gnus-uu-mark-all t]
2335          ["Mark buffer" gnus-uu-mark-buffer t]
2336          ["Mark sparse" gnus-uu-mark-sparse t]
2337          ["Mark thread" gnus-uu-mark-thread t]
2338          ["Unmark thread" gnus-uu-unmark-thread t]
2339          ("Process Mark Sets"
2340           ["Kill" gnus-summary-kill-process-mark t]
2341           ["Yank" gnus-summary-yank-process-mark
2342            gnus-newsgroup-process-stack]
2343           ["Save" gnus-summary-save-process-mark t]))
2344         ("Scroll article"
2345          ["Page forward" gnus-summary-next-page
2346           ,@(if (featurep 'xemacs) '(t)
2347               '(:help "Show next page of article"))]
2348          ["Page backward" gnus-summary-prev-page
2349           ,@(if (featurep 'xemacs) '(t)
2350               '(:help "Show previous page of article"))]
2351          ["Line forward" gnus-summary-scroll-up t])
2352         ("Move"
2353          ["Next unread article" gnus-summary-next-unread-article t]
2354          ["Previous unread article" gnus-summary-prev-unread-article t]
2355          ["Next article" gnus-summary-next-article t]
2356          ["Previous article" gnus-summary-prev-article t]
2357          ["Next unread subject" gnus-summary-next-unread-subject t]
2358          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2359          ["Next article same subject" gnus-summary-next-same-subject t]
2360          ["Previous article same subject" gnus-summary-prev-same-subject t]
2361          ["First unread article" gnus-summary-first-unread-article t]
2362          ["Best unread article" gnus-summary-best-unread-article t]
2363          ["Go to subject number..." gnus-summary-goto-subject t]
2364          ["Go to article number..." gnus-summary-goto-article t]
2365          ["Go to the last article" gnus-summary-goto-last-article t]
2366          ["Pop article off history" gnus-summary-pop-article t])
2367         ("Sort"
2368          ["Sort by number" gnus-summary-sort-by-number t]
2369          ["Sort by author" gnus-summary-sort-by-author t]
2370          ["Sort by subject" gnus-summary-sort-by-subject t]
2371          ["Sort by date" gnus-summary-sort-by-date t]
2372          ["Sort by score" gnus-summary-sort-by-score t]
2373          ["Sort by lines" gnus-summary-sort-by-lines t]
2374          ["Sort by characters" gnus-summary-sort-by-chars t]
2375          ["Randomize" gnus-summary-sort-by-random t]
2376          ["Original sort" gnus-summary-sort-by-original t])
2377         ("Help"
2378          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2379          ["Describe group" gnus-summary-describe-group t]
2380          ["Fetch charter" gnus-group-fetch-charter
2381           ,@(if (featurep 'xemacs) nil
2382               '(:help "Display the charter of the current group"))]
2383          ["Fetch control message" gnus-group-fetch-control
2384           ,@(if (featurep 'xemacs) nil
2385               '(:help "Display the archived control message for the current group"))]
2386          ["Read manual" gnus-info-find-node t])
2387         ("Modes"
2388          ["Pick and read" gnus-pick-mode t]
2389          ["Binary" gnus-binary-mode t])
2390         ("Regeneration"
2391          ["Regenerate" gnus-summary-prepare t]
2392          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2393          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2394          ["Toggle threading" gnus-summary-toggle-threads t])
2395         ["See old articles" gnus-summary-insert-old-articles t]
2396         ["See new articles" gnus-summary-insert-new-articles t]
2397         ["Filter articles..." gnus-summary-execute-command t]
2398         ["Run command on subjects..." gnus-summary-universal-argument t]
2399         ["Search articles forward..." gnus-summary-search-article-forward t]
2400         ["Search articles backward..." gnus-summary-search-article-backward t]
2401         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2402         ["Expand window" gnus-summary-expand-window t]
2403         ["Expire expirable articles" gnus-summary-expire-articles
2404          (gnus-check-backend-function
2405           'request-expire-articles gnus-newsgroup-name)]
2406         ["Edit local kill file" gnus-summary-edit-local-kill t]
2407         ["Edit main kill file" gnus-summary-edit-global-kill t]
2408         ["Edit group parameters" gnus-summary-edit-parameters t]
2409         ["Customize group parameters" gnus-summary-customize-parameters t]
2410         ["Send a bug report" gnus-bug t]
2411         ("Exit"
2412          ["Catchup and exit" gnus-summary-catchup-and-exit
2413           ,@(if (featurep 'xemacs) '(t)
2414               '(:help "Mark unread articles in this group as read, then exit"))]
2415          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2416          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2417          ["Exit group" gnus-summary-exit
2418           ,@(if (featurep 'xemacs) '(t)
2419               '(:help "Exit current group, return to group selection mode"))]
2420          ["Exit group without updating" gnus-summary-exit-no-update t]
2421          ["Exit and goto next group" gnus-summary-next-group t]
2422          ["Exit and goto prev group" gnus-summary-prev-group t]
2423          ["Reselect group" gnus-summary-reselect-current-group t]
2424          ["Rescan group" gnus-summary-rescan-group t]
2425          ["Update dribble" gnus-summary-save-newsrc t])))
2426
2427     (gnus-run-hooks 'gnus-summary-menu-hook)))
2428
2429 (defvar gnus-summary-tool-bar-map nil)
2430
2431 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2432 (defun gnus-summary-make-tool-bar ()
2433   (if (and (fboundp 'tool-bar-add-item-from-menu)
2434            (default-value 'tool-bar-mode)
2435            (not gnus-summary-tool-bar-map))
2436       (setq gnus-summary-tool-bar-map
2437             (let ((tool-bar-map (make-sparse-keymap))
2438                   (load-path (mm-image-load-path)))
2439               (tool-bar-add-item-from-menu
2440                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2441               (tool-bar-add-item-from-menu
2442                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2443               (tool-bar-add-item-from-menu
2444                'gnus-summary-post-news "post" gnus-summary-mode-map)
2445               (tool-bar-add-item-from-menu
2446                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2447               (tool-bar-add-item-from-menu
2448                'gnus-summary-followup "followup" gnus-summary-mode-map)
2449               (tool-bar-add-item-from-menu
2450                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2451               (tool-bar-add-item-from-menu
2452                'gnus-summary-reply "reply" gnus-summary-mode-map)
2453               (tool-bar-add-item-from-menu
2454                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2455               (tool-bar-add-item-from-menu
2456                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2457               (tool-bar-add-item-from-menu
2458                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2459               (tool-bar-add-item-from-menu
2460                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2461               (tool-bar-add-item-from-menu
2462                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2463               (tool-bar-add-item-from-menu
2464                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2465               (tool-bar-add-item-from-menu
2466                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2467               (tool-bar-add-item-from-menu
2468                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2469               tool-bar-map)))
2470   (if gnus-summary-tool-bar-map
2471       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2472
2473 (defun gnus-score-set-default (var value)
2474   "A version of set that updates the GNU Emacs menu-bar."
2475   (set var value)
2476   ;; It is the message that forces the active status to be updated.
2477   (message ""))
2478
2479 (defun gnus-make-score-map (type)
2480   "Make a summary score map of type TYPE."
2481   (if t
2482       nil
2483     (let ((headers '(("author" "from" string)
2484                      ("subject" "subject" string)
2485                      ("article body" "body" string)
2486                      ("article head" "head" string)
2487                      ("xref" "xref" string)
2488                      ("extra header" "extra" string)
2489                      ("lines" "lines" number)
2490                      ("followups to author" "followup" string)))
2491           (types '((number ("less than" <)
2492                            ("greater than" >)
2493                            ("equal" =))
2494                    (string ("substring" s)
2495                            ("exact string" e)
2496                            ("fuzzy string" f)
2497                            ("regexp" r))))
2498           (perms '(("temporary" (current-time-string))
2499                    ("permanent" nil)
2500                    ("immediate" now)))
2501           header)
2502       (list
2503        (apply
2504         'nconc
2505         (list
2506          (if (eq type 'lower)
2507              "Lower score"
2508            "Increase score"))
2509         (let (outh)
2510           (while headers
2511             (setq header (car headers))
2512             (setq outh
2513                   (cons
2514                    (apply
2515                     'nconc
2516                     (list (car header))
2517                     (let ((ts (cdr (assoc (nth 2 header) types)))
2518                           outt)
2519                       (while ts
2520                         (setq outt
2521                               (cons
2522                                (apply
2523                                 'nconc
2524                                 (list (caar ts))
2525                                 (let ((ps perms)
2526                                       outp)
2527                                   (while ps
2528                                     (setq outp
2529                                           (cons
2530                                            (vector
2531                                             (caar ps)
2532                                             (list
2533                                              'gnus-summary-score-entry
2534                                              (nth 1 header)
2535                                              (if (or (string= (nth 1 header)
2536                                                               "head")
2537                                                      (string= (nth 1 header)
2538                                                               "body"))
2539                                                  ""
2540                                                (list 'gnus-summary-header
2541                                                      (nth 1 header)))
2542                                              (list 'quote (nth 1 (car ts)))
2543                                              (list 'gnus-score-delta-default
2544                                                    nil)
2545                                              (nth 1 (car ps))
2546                                              t)
2547                                             t)
2548                                            outp))
2549                                     (setq ps (cdr ps)))
2550                                   (list (nreverse outp))))
2551                                outt))
2552                         (setq ts (cdr ts)))
2553                       (list (nreverse outt))))
2554                    outh))
2555             (setq headers (cdr headers)))
2556           (list (nreverse outh))))))))
2557
2558 \f
2559
2560 (defun gnus-summary-mode (&optional group)
2561   "Major mode for reading articles.
2562
2563 All normal editing commands are switched off.
2564 \\<gnus-summary-mode-map>
2565 Each line in this buffer represents one article.  To read an
2566 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2567 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2568 respectively.
2569
2570 You can also post articles and send mail from this buffer.  To
2571 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2572 of an article, type `\\[gnus-summary-reply]'.
2573
2574 There are approx. one gazillion commands you can execute in this
2575 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2576
2577 The following commands are available:
2578
2579 \\{gnus-summary-mode-map}"
2580   (interactive)
2581   (kill-all-local-variables)
2582   (when (gnus-visual-p 'summary-menu 'menu)
2583     (gnus-summary-make-menu-bar)
2584     (gnus-summary-make-tool-bar))
2585   (gnus-summary-make-local-variables)
2586   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2587     (gnus-summary-make-local-variables))
2588   (gnus-make-thread-indent-array)
2589   (gnus-simplify-mode-line)
2590   (setq major-mode 'gnus-summary-mode)
2591   (setq mode-name "Summary")
2592   (make-local-variable 'minor-mode-alist)
2593   (use-local-map gnus-summary-mode-map)
2594   (buffer-disable-undo)
2595   (setq buffer-read-only t)             ;Disable modification
2596   (setq truncate-lines t)
2597   (setq selective-display t)
2598   (setq selective-display-ellipses t)   ;Display `...'
2599   (gnus-summary-set-display-table)
2600   (gnus-set-default-directory)
2601   (setq gnus-newsgroup-name group)
2602   (make-local-variable 'gnus-summary-line-format)
2603   (make-local-variable 'gnus-summary-line-format-spec)
2604   (make-local-variable 'gnus-summary-dummy-line-format)
2605   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2606   (make-local-variable 'gnus-summary-mark-positions)
2607   (make-local-hook 'pre-command-hook)
2608   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2609   (gnus-run-hooks 'gnus-summary-mode-hook)
2610   (turn-on-gnus-mailing-list-mode)
2611   (mm-enable-multibyte)
2612   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2613   (gnus-update-summary-mark-positions))
2614
2615 (defun gnus-summary-make-local-variables ()
2616   "Make all the local summary buffer variables."
2617   (let (global)
2618     (dolist (local gnus-summary-local-variables)
2619       (if (consp local)
2620           (progn
2621             (if (eq (cdr local) 'global)
2622                 ;; Copy the global value of the variable.
2623                 (setq global (symbol-value (car local)))
2624               ;; Use the value from the list.
2625               (setq global (eval (cdr local))))
2626             (set (make-local-variable (car local)) global))
2627         ;; Simple nil-valued local variable.
2628         (set (make-local-variable local) nil)))))
2629
2630 (defun gnus-summary-clear-local-variables ()
2631   (let ((locals gnus-summary-local-variables))
2632     (while locals
2633       (if (consp (car locals))
2634           (and (vectorp (caar locals))
2635                (set (caar locals) nil))
2636         (and (vectorp (car locals))
2637              (set (car locals) nil)))
2638       (setq locals (cdr locals)))))
2639
2640 ;; Summary data functions.
2641
2642 (defmacro gnus-data-number (data)
2643   `(car ,data))
2644
2645 (defmacro gnus-data-set-number (data number)
2646   `(setcar ,data ,number))
2647
2648 (defmacro gnus-data-mark (data)
2649   `(nth 1 ,data))
2650
2651 (defmacro gnus-data-set-mark (data mark)
2652   `(setcar (nthcdr 1 ,data) ,mark))
2653
2654 (defmacro gnus-data-pos (data)
2655   `(nth 2 ,data))
2656
2657 (defmacro gnus-data-set-pos (data pos)
2658   `(setcar (nthcdr 2 ,data) ,pos))
2659
2660 (defmacro gnus-data-header (data)
2661   `(nth 3 ,data))
2662
2663 (defmacro gnus-data-set-header (data header)
2664   `(setf (nth 3 ,data) ,header))
2665
2666 (defmacro gnus-data-level (data)
2667   `(nth 4 ,data))
2668
2669 (defmacro gnus-data-unread-p (data)
2670   `(= (nth 1 ,data) gnus-unread-mark))
2671
2672 (defmacro gnus-data-read-p (data)
2673   `(/= (nth 1 ,data) gnus-unread-mark))
2674
2675 (defmacro gnus-data-pseudo-p (data)
2676   `(consp (nth 3 ,data)))
2677
2678 (defmacro gnus-data-find (number)
2679   `(assq ,number gnus-newsgroup-data))
2680
2681 (defmacro gnus-data-find-list (number &optional data)
2682   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2683      (memq (assq ,number bdata)
2684            bdata)))
2685
2686 (defmacro gnus-data-make (number mark pos header level)
2687   `(list ,number ,mark ,pos ,header ,level))
2688
2689 (defun gnus-data-enter (after-article number mark pos header level offset)
2690   (let ((data (gnus-data-find-list after-article)))
2691     (unless data
2692       (error "No such article: %d" after-article))
2693     (setcdr data (cons (gnus-data-make number mark pos header level)
2694                        (cdr data)))
2695     (setq gnus-newsgroup-data-reverse nil)
2696     (gnus-data-update-list (cddr data) offset)))
2697
2698 (defun gnus-data-enter-list (after-article list &optional offset)
2699   (when list
2700     (let ((data (and after-article (gnus-data-find-list after-article)))
2701           (ilist list))
2702       (if (not (or data
2703                    after-article))
2704           (let ((odata gnus-newsgroup-data))
2705             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2706             (when offset
2707               (gnus-data-update-list odata offset)))
2708       ;; Find the last element in the list to be spliced into the main
2709         ;; list.
2710         (while (cdr list)
2711           (setq list (cdr list)))
2712         (if (not data)
2713             (progn
2714               (setcdr list gnus-newsgroup-data)
2715               (setq gnus-newsgroup-data ilist)
2716               (when offset
2717                 (gnus-data-update-list (cdr list) offset)))
2718           (setcdr list (cdr data))
2719           (setcdr data ilist)
2720           (when offset
2721             (gnus-data-update-list (cdr list) offset))))
2722       (setq gnus-newsgroup-data-reverse nil))))
2723
2724 (defun gnus-data-remove (article &optional offset)
2725   (let ((data gnus-newsgroup-data))
2726     (if (= (gnus-data-number (car data)) article)
2727         (progn
2728           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2729                 gnus-newsgroup-data-reverse nil)
2730           (when offset
2731             (gnus-data-update-list gnus-newsgroup-data offset)))
2732       (while (cdr data)
2733         (when (= (gnus-data-number (cadr data)) article)
2734           (setcdr data (cddr data))
2735           (when offset
2736             (gnus-data-update-list (cdr data) offset))
2737           (setq data nil
2738                 gnus-newsgroup-data-reverse nil))
2739         (setq data (cdr data))))))
2740
2741 (defmacro gnus-data-list (backward)
2742   `(if ,backward
2743        (or gnus-newsgroup-data-reverse
2744            (setq gnus-newsgroup-data-reverse
2745                  (reverse gnus-newsgroup-data)))
2746      gnus-newsgroup-data))
2747
2748 (defun gnus-data-update-list (data offset)
2749   "Add OFFSET to the POS of all data entries in DATA."
2750   (setq gnus-newsgroup-data-reverse nil)
2751   (while data
2752     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2753     (setq data (cdr data))))
2754
2755 (defun gnus-summary-article-pseudo-p (article)
2756   "Say whether this article is a pseudo article or not."
2757   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2758
2759 (defmacro gnus-summary-article-sparse-p (article)
2760   "Say whether this article is a sparse article or not."
2761   `(memq ,article gnus-newsgroup-sparse))
2762
2763 (defmacro gnus-summary-article-ancient-p (article)
2764   "Say whether this article is a sparse article or not."
2765   `(memq ,article gnus-newsgroup-ancient))
2766
2767 (defun gnus-article-parent-p (number)
2768   "Say whether this article is a parent or not."
2769   (let ((data (gnus-data-find-list number)))
2770     (and (cdr data)              ; There has to be an article after...
2771          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2772             (gnus-data-level (nth 1 data))))))
2773
2774 (defun gnus-article-children (number)
2775   "Return a list of all children to NUMBER."
2776   (let* ((data (gnus-data-find-list number))
2777          (level (gnus-data-level (car data)))
2778          children)
2779     (setq data (cdr data))
2780     (while (and data
2781                 (= (gnus-data-level (car data)) (1+ level)))
2782       (push (gnus-data-number (car data)) children)
2783       (setq data (cdr data)))
2784     children))
2785
2786 (defmacro gnus-summary-skip-intangible ()
2787   "If the current article is intangible, then jump to a different article."
2788   '(let ((to (get-text-property (point) 'gnus-intangible)))
2789      (and to (gnus-summary-goto-subject to))))
2790
2791 (defmacro gnus-summary-article-intangible-p ()
2792   "Say whether this article is intangible or not."
2793   '(get-text-property (point) 'gnus-intangible))
2794
2795 (defun gnus-article-read-p (article)
2796   "Say whether ARTICLE is read or not."
2797   (not (or (memq article gnus-newsgroup-marked)
2798            (memq article gnus-newsgroup-spam-marked)
2799            (memq article gnus-newsgroup-unreads)
2800            (memq article gnus-newsgroup-unselected)
2801            (memq article gnus-newsgroup-dormant))))
2802
2803 ;; Some summary mode macros.
2804
2805 (defmacro gnus-summary-article-number ()
2806   "The article number of the article on the current line.
2807 If there isn't an article number here, then we return the current
2808 article number."
2809   '(progn
2810      (gnus-summary-skip-intangible)
2811      (or (get-text-property (point) 'gnus-number)
2812          (gnus-summary-last-subject))))
2813
2814 (defmacro gnus-summary-article-header (&optional number)
2815   "Return the header of article NUMBER."
2816   `(gnus-data-header (gnus-data-find
2817                       ,(or number '(gnus-summary-article-number)))))
2818
2819 (defmacro gnus-summary-thread-level (&optional number)
2820   "Return the level of thread that starts with article NUMBER."
2821   `(if (and (eq gnus-summary-make-false-root 'dummy)
2822             (get-text-property (point) 'gnus-intangible))
2823        0
2824      (gnus-data-level (gnus-data-find
2825                        ,(or number '(gnus-summary-article-number))))))
2826
2827 (defmacro gnus-summary-article-mark (&optional number)
2828   "Return the mark of article NUMBER."
2829   `(gnus-data-mark (gnus-data-find
2830                     ,(or number '(gnus-summary-article-number)))))
2831
2832 (defmacro gnus-summary-article-pos (&optional number)
2833   "Return the position of the line of article NUMBER."
2834   `(gnus-data-pos (gnus-data-find
2835                    ,(or number '(gnus-summary-article-number)))))
2836
2837 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2838 (defmacro gnus-summary-article-subject (&optional number)
2839   "Return current subject string or nil if nothing."
2840   `(let ((headers
2841           ,(if number
2842                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2843              '(gnus-data-header (assq (gnus-summary-article-number)
2844                                       gnus-newsgroup-data)))))
2845      (and headers
2846           (vectorp headers)
2847           (mail-header-subject headers))))
2848
2849 (defmacro gnus-summary-article-score (&optional number)
2850   "Return current article score."
2851   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2852                   gnus-newsgroup-scored))
2853        gnus-summary-default-score 0))
2854
2855 (defun gnus-summary-article-children (&optional number)
2856   "Return a list of article numbers that are children of article NUMBER."
2857   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2858          (level (gnus-data-level (car data)))
2859          l children)
2860     (while (and (setq data (cdr data))
2861                 (> (setq l (gnus-data-level (car data))) level))
2862       (and (= (1+ level) l)
2863            (push (gnus-data-number (car data))
2864                  children)))
2865     (nreverse children)))
2866
2867 (defun gnus-summary-article-parent (&optional number)
2868   "Return the article number of the parent of article NUMBER."
2869   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2870                                     (gnus-data-list t)))
2871          (level (gnus-data-level (car data))))
2872     (if (zerop level)
2873         ()                              ; This is a root.
2874       ;; We search until we find an article with a level less than
2875       ;; this one.  That function has to be the parent.
2876       (while (and (setq data (cdr data))
2877                   (not (< (gnus-data-level (car data)) level))))
2878       (and data (gnus-data-number (car data))))))
2879
2880 (defun gnus-unread-mark-p (mark)
2881   "Say whether MARK is the unread mark."
2882   (= mark gnus-unread-mark))
2883
2884 (defun gnus-read-mark-p (mark)
2885   "Say whether MARK is one of the marks that mark as read.
2886 This is all marks except unread, ticked, dormant, and expirable."
2887   (not (or (= mark gnus-unread-mark)
2888            (= mark gnus-ticked-mark)
2889            (= mark gnus-spam-mark)
2890            (= mark gnus-dormant-mark)
2891            (= mark gnus-expirable-mark))))
2892
2893 (defmacro gnus-article-mark (number)
2894   "Return the MARK of article NUMBER.
2895 This macro should only be used when computing the mark the \"first\"
2896 time; i.e., when generating the summary lines.  After that,
2897 `gnus-summary-article-mark' should be used to examine the
2898 marks of articles."
2899   `(cond
2900     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2901     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2902     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2903     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2904     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2905     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2906     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2907     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2908            gnus-ancient-mark))))
2909
2910 ;; Saving hidden threads.
2911
2912 (defmacro gnus-save-hidden-threads (&rest forms)
2913   "Save hidden threads, eval FORMS, and restore the hidden threads."
2914   (let ((config (make-symbol "config")))
2915     `(let ((,config (gnus-hidden-threads-configuration)))
2916        (unwind-protect
2917            (save-excursion
2918              ,@forms)
2919          (gnus-restore-hidden-threads-configuration ,config)))))
2920 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2921 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2922
2923 (defun gnus-data-compute-positions ()
2924   "Compute the positions of all articles."
2925   (setq gnus-newsgroup-data-reverse nil)
2926   (let ((data gnus-newsgroup-data))
2927     (save-excursion
2928       (gnus-save-hidden-threads
2929         (gnus-summary-show-all-threads)
2930         (goto-char (point-min))
2931         (while data
2932           (while (get-text-property (point) 'gnus-intangible)
2933             (forward-line 1))
2934           (gnus-data-set-pos (car data) (+ (point) 3))
2935           (setq data (cdr data))
2936           (forward-line 1))))))
2937
2938 (defun gnus-hidden-threads-configuration ()
2939   "Return the current hidden threads configuration."
2940   (save-excursion
2941     (let (config)
2942       (goto-char (point-min))
2943       (while (search-forward "\r" nil t)
2944         (push (1- (point)) config))
2945       config)))
2946
2947 (defun gnus-restore-hidden-threads-configuration (config)
2948   "Restore hidden threads configuration from CONFIG."
2949   (save-excursion
2950     (let (point buffer-read-only)
2951       (while (setq point (pop config))
2952         (when (and (< point (point-max))
2953                    (goto-char point)
2954                    (eq (char-after) ?\n))
2955           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2956
2957 ;; Various summary mode internalish functions.
2958
2959 (defun gnus-mouse-pick-article (e)
2960   (interactive "e")
2961   (mouse-set-point e)
2962   (gnus-summary-next-page nil t))
2963
2964 (defun gnus-summary-set-display-table ()
2965   "Change the display table.
2966 Odd characters have a tendency to mess
2967 up nicely formatted displays - we make all possible glyphs
2968 display only a single character."
2969
2970   ;; We start from the standard display table, if any.
2971   (let ((table (or (copy-sequence standard-display-table)
2972                    (make-display-table)))
2973         (i 32))
2974     ;; Nix out all the control chars...
2975     (while (>= (setq i (1- i)) 0)
2976       (aset table i [??]))
2977    ;; ... but not newline and cr, of course.  (cr is necessary for the
2978     ;; selective display).
2979     (aset table ?\n nil)
2980     (aset table ?\r nil)
2981     ;; We keep TAB as well.
2982     (aset table ?\t nil)
2983     ;; We nix out any glyphs over 126 that are not set already.
2984     (let ((i 256))
2985       (while (>= (setq i (1- i)) 127)
2986         ;; Only modify if the entry is nil.
2987         (unless (aref table i)
2988           (aset table i [??]))))
2989     (setq buffer-display-table table)))
2990
2991 (defun gnus-summary-set-article-display-arrow (pos)
2992   "Update the overlay arrow to point to line at position POS."
2993   (when (and gnus-summary-display-arrow
2994              (boundp 'overlay-arrow-position)
2995              (boundp 'overlay-arrow-string))
2996     (save-excursion
2997       (goto-char pos)
2998       (beginning-of-line)
2999       (unless overlay-arrow-position
3000         (setq overlay-arrow-position (make-marker)))
3001       (setq overlay-arrow-string "=>"
3002             overlay-arrow-position (set-marker overlay-arrow-position
3003                                                (point)
3004                                                (current-buffer))))))
3005
3006 (defun gnus-summary-buffer-name (group)
3007   "Return the summary buffer name of GROUP."
3008   (concat "*Summary " (gnus-group-decoded-name group) "*"))
3009
3010 (defun gnus-summary-setup-buffer (group)
3011   "Initialize summary buffer."
3012   (let ((buffer (gnus-summary-buffer-name group))
3013         (dead-name (concat "*Dead Summary "
3014                            (gnus-group-decoded-name group) "*")))
3015     ;; If a dead summary buffer exists, we kill it.
3016     (when (gnus-buffer-live-p dead-name)
3017       (gnus-kill-buffer dead-name))
3018     (if (get-buffer buffer)
3019         (progn
3020           (set-buffer buffer)
3021           (setq gnus-summary-buffer (current-buffer))
3022           (not gnus-newsgroup-prepared))
3023       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3024       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3025       (gnus-summary-mode group)
3026       (when gnus-carpal
3027         (gnus-carpal-setup-buffer 'summary))
3028       (unless gnus-single-article-buffer
3029         (make-local-variable 'gnus-article-buffer)
3030         (make-local-variable 'gnus-article-current)
3031         (make-local-variable 'gnus-original-article-buffer))
3032       (setq gnus-newsgroup-name group)
3033       ;; Set any local variables in the group parameters.
3034       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3035       t)))
3036
3037 (defun gnus-set-global-variables ()
3038   "Set the global equivalents of the buffer-local variables.
3039 They are set to the latest values they had.  These reflect the summary
3040 buffer that was in action when the last article was fetched."
3041   (when (eq major-mode 'gnus-summary-mode)
3042     (setq gnus-summary-buffer (current-buffer))
3043     (let ((name gnus-newsgroup-name)
3044           (marked gnus-newsgroup-marked)
3045           (spam gnus-newsgroup-spam-marked)
3046           (unread gnus-newsgroup-unreads)
3047           (headers gnus-current-headers)
3048           (data gnus-newsgroup-data)
3049           (summary gnus-summary-buffer)
3050           (article-buffer gnus-article-buffer)
3051           (original gnus-original-article-buffer)
3052           (gac gnus-article-current)
3053           (reffed gnus-reffed-article-number)
3054           (score-file gnus-current-score-file)
3055           (default-charset gnus-newsgroup-charset)
3056           vlist)
3057       (let ((locals gnus-newsgroup-variables))
3058         (while locals
3059           (if (consp (car locals))
3060               (push (eval (caar locals)) vlist)
3061             (push (eval (car locals)) vlist))
3062           (setq locals (cdr locals)))
3063         (setq vlist (nreverse vlist)))
3064       (save-excursion
3065         (set-buffer gnus-group-buffer)
3066         (setq gnus-newsgroup-name name
3067               gnus-newsgroup-marked marked
3068               gnus-newsgroup-spam-marked spam
3069               gnus-newsgroup-unreads unread
3070               gnus-current-headers headers
3071               gnus-newsgroup-data data
3072               gnus-article-current gac
3073               gnus-summary-buffer summary
3074               gnus-article-buffer article-buffer
3075               gnus-original-article-buffer original
3076               gnus-reffed-article-number reffed
3077               gnus-current-score-file score-file
3078               gnus-newsgroup-charset default-charset)
3079         (let ((locals gnus-newsgroup-variables))
3080           (while locals
3081             (if (consp (car locals))
3082                 (set (caar locals) (pop vlist))
3083               (set (car locals) (pop vlist)))
3084             (setq locals (cdr locals))))
3085         ;; The article buffer also has local variables.
3086         (when (gnus-buffer-live-p gnus-article-buffer)
3087           (set-buffer gnus-article-buffer)
3088           (setq gnus-summary-buffer summary))))))
3089
3090 (defun gnus-summary-article-unread-p (article)
3091   "Say whether ARTICLE is unread or not."
3092   (memq article gnus-newsgroup-unreads))
3093
3094 (defun gnus-summary-first-article-p (&optional article)
3095   "Return whether ARTICLE is the first article in the buffer."
3096   (if (not (setq article (or article (gnus-summary-article-number))))
3097       nil
3098     (eq article (caar gnus-newsgroup-data))))
3099
3100 (defun gnus-summary-last-article-p (&optional article)
3101   "Return whether ARTICLE is the last article in the buffer."
3102   (if (not (setq article (or article (gnus-summary-article-number))))
3103       ;; All non-existent numbers are the last article.  :-)
3104       t
3105     (not (cdr (gnus-data-find-list article)))))
3106
3107 (defun gnus-make-thread-indent-array ()
3108   (let ((n 200))
3109     (unless (and gnus-thread-indent-array
3110                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3111       (setq gnus-thread-indent-array (make-vector 201 "")
3112             gnus-thread-indent-array-level gnus-thread-indent-level)
3113       (while (>= n 0)
3114         (aset gnus-thread-indent-array n
3115               (make-string (* n gnus-thread-indent-level) ? ))
3116         (setq n (1- n))))))
3117
3118 (defun gnus-update-summary-mark-positions ()
3119   "Compute where the summary marks are to go."
3120   (save-excursion
3121     (when (gnus-buffer-exists-p gnus-summary-buffer)
3122       (set-buffer gnus-summary-buffer))
3123     (let ((gnus-replied-mark 129)
3124           (gnus-score-below-mark 130)
3125           (gnus-score-over-mark 130)
3126           (gnus-undownloaded-mark 131)
3127           (spec gnus-summary-line-format-spec)
3128           gnus-visual pos)
3129       (save-excursion
3130         (gnus-set-work-buffer)
3131         (let ((gnus-summary-line-format-spec spec)
3132               (gnus-newsgroup-downloadable '(0)))
3133           (gnus-summary-insert-line
3134            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3135            0 nil t 128 t nil "" nil 1)
3136           (goto-char (point-min))
3137           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3138                                              (- (point) (point-min) 1)))))
3139           (goto-char (point-min))
3140           (push (cons 'replied (and (search-forward "\201" nil t)
3141                                     (- (point) (point-min) 1)))
3142                 pos)
3143           (goto-char (point-min))
3144           (push (cons 'score (and (search-forward "\202" nil t)
3145                                   (- (point) (point-min) 1)))
3146                 pos)
3147           (goto-char (point-min))
3148           (push (cons 'download
3149                       (and (search-forward "\203" nil t)
3150                            (- (point) (point-min) 1)))
3151                 pos)))
3152       (setq gnus-summary-mark-positions pos))))
3153
3154 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3155   "Insert a dummy root in the summary buffer."
3156   (beginning-of-line)
3157   (gnus-add-text-properties
3158    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3159    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3160
3161 (defun gnus-summary-extract-address-component (from)
3162   (or (car (funcall gnus-extract-address-components from))
3163       from))
3164
3165 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3166   (let ((mail-parse-charset gnus-newsgroup-charset)
3167         ; Is it really necessary to do this next part for each summary line?
3168         ; Luckily, doesn't seem to slow things down much.
3169         (mail-parse-ignored-charsets
3170          (save-excursion (set-buffer gnus-summary-buffer)
3171                          gnus-newsgroup-ignored-charsets)))
3172     (or
3173      (and gnus-ignored-from-addresses
3174           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3175           (let ((extra-headers (mail-header-extra header))
3176                 to
3177                 newsgroups)
3178             (cond
3179              ((setq to (cdr (assq 'To extra-headers)))
3180               (concat "-> "
3181                       (inline
3182                         (gnus-summary-extract-address-component
3183                          (funcall gnus-decode-encoded-word-function to)))))
3184              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3185               (concat "=> " newsgroups)))))
3186      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3187
3188 (defun gnus-summary-insert-line (gnus-tmp-header
3189                                  gnus-tmp-level gnus-tmp-current
3190                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3191                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3192                                  &optional gnus-tmp-dummy gnus-tmp-score
3193                                  gnus-tmp-process)
3194   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3195          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3196          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3197          (gnus-tmp-score-char
3198           (if (or (null gnus-summary-default-score)
3199                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3200                       gnus-summary-zcore-fuzz))
3201               ?                         ;Whitespace
3202             (if (< gnus-tmp-score gnus-summary-default-score)
3203                 gnus-score-below-mark gnus-score-over-mark)))
3204          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3205          (gnus-tmp-replied
3206           (cond (gnus-tmp-process gnus-process-mark)
3207                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3208                  gnus-cached-mark)
3209                 (gnus-tmp-replied gnus-replied-mark)
3210                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3211                  gnus-forwarded-mark)
3212                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3213                  gnus-saved-mark)
3214                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3215                  gnus-recent-mark)
3216                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3217                  gnus-unseen-mark)
3218                 (t gnus-no-mark)))
3219          (gnus-tmp-downloaded
3220           (cond (undownloaded 
3221                  gnus-undownloaded-mark)
3222                 (gnus-newsgroup-agentized
3223                  gnus-downloaded-mark)
3224                 (t
3225                  gnus-no-mark)))
3226          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3227          (gnus-tmp-name
3228           (cond
3229            ((string-match "<[^>]+> *$" gnus-tmp-from)
3230             (let ((beg (match-beginning 0)))
3231               (or (and (string-match "^\".+\"" gnus-tmp-from)
3232                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3233                   (substring gnus-tmp-from 0 beg))))
3234            ((string-match "(.+)" gnus-tmp-from)
3235             (substring gnus-tmp-from
3236                        (1+ (match-beginning 0)) (1- (match-end 0))))
3237            (t gnus-tmp-from)))
3238          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3239          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3240          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3241          (buffer-read-only nil))
3242     (when (string= gnus-tmp-name "")
3243       (setq gnus-tmp-name gnus-tmp-from))
3244     (unless (numberp gnus-tmp-lines)
3245       (setq gnus-tmp-lines -1))
3246     (if (= gnus-tmp-lines -1)
3247         (setq gnus-tmp-lines "?")
3248       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3249     (gnus-put-text-property
3250      (point)
3251      (progn (eval gnus-summary-line-format-spec) (point))
3252      'gnus-number gnus-tmp-number)
3253     (when (gnus-visual-p 'summary-highlight 'highlight)
3254       (forward-line -1)
3255       (gnus-run-hooks 'gnus-summary-update-hook)
3256       (forward-line 1))))
3257
3258 (defun gnus-summary-update-line (&optional dont-update)
3259   "Update summary line after change."
3260   (when (and gnus-summary-default-score
3261              (not gnus-summary-inhibit-highlight))
3262     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3263            (article (gnus-summary-article-number))
3264            (score (gnus-summary-article-score article)))
3265       (unless dont-update
3266         (if (and gnus-summary-mark-below
3267                  (< (gnus-summary-article-score)
3268                     gnus-summary-mark-below))
3269             ;; This article has a low score, so we mark it as read.
3270             (when (memq article gnus-newsgroup-unreads)
3271               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3272           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3273             ;; This article was previously marked as read on account
3274             ;; of a low score, but now it has risen, so we mark it as
3275             ;; unread.
3276             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3277         (gnus-summary-update-mark
3278          (if (or (null gnus-summary-default-score)
3279                  (<= (abs (- score gnus-summary-default-score))
3280                      gnus-summary-zcore-fuzz))
3281              ?                          ;Whitespace
3282            (if (< score gnus-summary-default-score)
3283                gnus-score-below-mark gnus-score-over-mark))
3284          'score))
3285       ;; Do visual highlighting.
3286       (when (gnus-visual-p 'summary-highlight 'highlight)
3287         (gnus-run-hooks 'gnus-summary-update-hook)))))
3288
3289 (defvar gnus-tmp-new-adopts nil)
3290
3291 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3292   "Return the number of articles in THREAD.
3293 This may be 0 in some cases -- if none of the articles in
3294 the thread are to be displayed."
3295   (let* ((number
3296          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3297           (cond
3298            ((not (listp thread))
3299             1)
3300            ((and (consp thread) (cdr thread))
3301             (apply
3302              '+ 1 (mapcar
3303                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3304            ((null thread)
3305             1)
3306            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3307             1)
3308            (t 0))))
3309     (when (and level (zerop level) gnus-tmp-new-adopts)
3310       (incf number
3311             (apply '+ (mapcar
3312                        'gnus-summary-number-of-articles-in-thread
3313                        gnus-tmp-new-adopts))))
3314     (if char
3315         (if (> number 1) gnus-not-empty-thread-mark
3316           gnus-empty-thread-mark)
3317       number)))
3318
3319 (defsubst gnus-summary-line-message-size (head)
3320   "Return pretty-printed version of message size.
3321 This function is intended to be used in
3322 `gnus-summary-line-format-alist', which see."
3323   (let ((c (or (mail-header-chars head) -1)))
3324     (cond ((< c 0) "n/a")               ; chars not available
3325           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3326           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3327           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3328           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3329
3330
3331 (defun gnus-summary-set-local-parameters (group)
3332   "Go through the local params of GROUP and set all variable specs in that list."
3333   (let ((params (gnus-group-find-parameter group))
3334         (vars '(quit-config))           ; Ignore quit-config.
3335         elem)
3336     (while params
3337       (setq elem (car params)
3338             params (cdr params))
3339       (and (consp elem)                 ; Has to be a cons.
3340            (consp (cdr elem))           ; The cdr has to be a list.
3341            (symbolp (car elem))         ; Has to be a symbol in there.
3342            (not (memq (car elem) vars))
3343            (ignore-errors               ; So we set it.
3344              (push (car elem) vars)
3345              (make-local-variable (car elem))
3346              (set (car elem) (eval (nth 1 elem))))))))
3347
3348 (defun gnus-summary-read-group (group &optional show-all no-article
3349                                       kill-buffer no-display backward
3350                                       select-articles)
3351   "Start reading news in newsgroup GROUP.
3352 If SHOW-ALL is non-nil, already read articles are also listed.
3353 If NO-ARTICLE is non-nil, no article is selected initially.
3354 If NO-DISPLAY, don't generate a summary buffer."
3355   (let (result)
3356     (while (and group
3357                 (null (setq result
3358                             (let ((gnus-auto-select-next nil))
3359                               (or (gnus-summary-read-group-1
3360                                    group show-all no-article
3361                                    kill-buffer no-display
3362                                    select-articles)
3363                                   (setq show-all nil
3364                                         select-articles nil)))))
3365                 (eq gnus-auto-select-next 'quietly))
3366       (set-buffer gnus-group-buffer)
3367       ;; The entry function called above goes to the next
3368       ;; group automatically, so we go two groups back
3369       ;; if we are searching for the previous group.
3370       (when backward
3371         (gnus-group-prev-unread-group 2))
3372       (if (not (equal group (gnus-group-group-name)))
3373           (setq group (gnus-group-group-name))
3374         (setq group nil)))
3375     result))
3376
3377 (defun gnus-summary-read-group-1 (group show-all no-article
3378                                         kill-buffer no-display
3379                                         &optional select-articles)
3380   ;; Killed foreign groups can't be entered.
3381   ;;  (when (and (not (gnus-group-native-p group))
3382   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3383   ;;    (error "Dead non-native groups can't be entered"))
3384   (gnus-message 5 "Retrieving newsgroup: %s..."
3385                 (gnus-group-decoded-name group))
3386   (let* ((new-group (gnus-summary-setup-buffer group))
3387          (quit-config (gnus-group-quit-config group))
3388          (did-select (and new-group (gnus-select-newsgroup
3389                                      group show-all select-articles))))
3390     (cond
3391      ;; This summary buffer exists already, so we just select it.
3392      ((not new-group)
3393       (gnus-set-global-variables)
3394       (when kill-buffer
3395         (gnus-kill-or-deaden-summary kill-buffer))
3396       (gnus-configure-windows 'summary 'force)
3397       (gnus-set-mode-line 'summary)
3398       (gnus-summary-position-point)
3399       (message "")
3400       t)
3401      ;; We couldn't select this group.
3402      ((null did-select)
3403       (when (and (eq major-mode 'gnus-summary-mode)
3404                  (not (equal (current-buffer) kill-buffer)))
3405         (kill-buffer (current-buffer))
3406         (if (not quit-config)
3407             (progn
3408               ;; Update the info -- marks might need to be removed,
3409               ;; for instance.
3410               (gnus-summary-update-info)
3411               (set-buffer gnus-group-buffer)
3412               (gnus-group-jump-to-group group)
3413               (gnus-group-next-unread-group 1))
3414           (gnus-handle-ephemeral-exit quit-config)))
3415       (let ((grpinfo (gnus-get-info group)))
3416         (if (null (gnus-info-read grpinfo))
3417             (gnus-message 3 "Group %s contains no messages"
3418                           (gnus-group-decoded-name group))
3419           (gnus-message 3 "Can't select group")))
3420       nil)
3421      ;; The user did a `C-g' while prompting for number of articles,
3422      ;; so we exit this group.
3423      ((eq did-select 'quit)
3424       (and (eq major-mode 'gnus-summary-mode)
3425            (not (equal (current-buffer) kill-buffer))
3426            (kill-buffer (current-buffer)))
3427       (when kill-buffer
3428         (gnus-kill-or-deaden-summary kill-buffer))
3429       (if (not quit-config)
3430           (progn
3431             (set-buffer gnus-group-buffer)
3432             (gnus-group-jump-to-group group)
3433             (gnus-group-next-unread-group 1)
3434             (gnus-configure-windows 'group 'force))
3435         (gnus-handle-ephemeral-exit quit-config))
3436       ;; Finally signal the quit.
3437       (signal 'quit nil))
3438      ;; The group was successfully selected.
3439      (t
3440       (gnus-set-global-variables)
3441       ;; Save the active value in effect when the group was entered.
3442       (setq gnus-newsgroup-active
3443             (gnus-copy-sequence
3444              (gnus-active gnus-newsgroup-name)))
3445       ;; You can change the summary buffer in some way with this hook.
3446       (gnus-run-hooks 'gnus-select-group-hook)
3447       (gnus-update-format-specifications
3448        nil 'summary 'summary-mode 'summary-dummy)
3449       (gnus-update-summary-mark-positions)
3450       ;; Do score processing.
3451       (when gnus-use-scoring
3452         (gnus-possibly-score-headers))
3453       ;; Check whether to fill in the gaps in the threads.
3454       (when gnus-build-sparse-threads
3455         (gnus-build-sparse-threads))
3456       ;; Find the initial limit.
3457       (if gnus-show-threads
3458           (if show-all
3459               (let ((gnus-newsgroup-dormant nil))
3460                 (gnus-summary-initial-limit show-all))
3461             (gnus-summary-initial-limit show-all))
3462         ;; When unthreaded, all articles are always shown.
3463         (setq gnus-newsgroup-limit
3464               (mapcar
3465                (lambda (header) (mail-header-number header))
3466                gnus-newsgroup-headers)))
3467       ;; Generate the summary buffer.
3468       (unless no-display
3469         (gnus-summary-prepare))
3470       (when gnus-use-trees
3471         (gnus-tree-open group)
3472         (setq gnus-summary-highlight-line-function
3473               'gnus-tree-highlight-article))
3474       ;; If the summary buffer is empty, but there are some low-scored
3475       ;; articles or some excluded dormants, we include these in the
3476       ;; buffer.
3477       (when (and (zerop (buffer-size))
3478                  (not no-display))
3479         (cond (gnus-newsgroup-dormant
3480                (gnus-summary-limit-include-dormant))
3481               ((and gnus-newsgroup-scored show-all)
3482                (gnus-summary-limit-include-expunged t))))
3483       ;; Function `gnus-apply-kill-file' must be called in this hook.
3484       (gnus-run-hooks 'gnus-apply-kill-hook)
3485       (if (and (zerop (buffer-size))
3486                (not no-display))
3487           (progn
3488             ;; This newsgroup is empty.
3489             (gnus-summary-catchup-and-exit nil t)
3490             (gnus-message 6 "No unread news")
3491             (when kill-buffer
3492               (gnus-kill-or-deaden-summary kill-buffer))
3493             ;; Return nil from this function.
3494             nil)
3495         ;; Hide conversation thread subtrees.  We cannot do this in
3496         ;; gnus-summary-prepare-hook since kill processing may not
3497         ;; work with hidden articles.
3498         (gnus-summary-maybe-hide-threads)
3499         (when kill-buffer
3500           (gnus-kill-or-deaden-summary kill-buffer))
3501         (gnus-summary-auto-select-subject)
3502         ;; Show first unread article if requested.
3503         (if (and (not no-article)
3504                  (not no-display)
3505                  gnus-newsgroup-unreads
3506                  gnus-auto-select-first)
3507             (progn
3508               (gnus-configure-windows 'summary)
3509               (let ((art (gnus-summary-article-number)))
3510                 (unless (and (not gnus-plugged)
3511                              (or (memq art gnus-newsgroup-undownloaded)
3512                                  (memq art gnus-newsgroup-downloadable)))
3513                   (gnus-summary-goto-article art))))
3514           ;; Don't select any articles.
3515           (gnus-summary-position-point)
3516           (gnus-configure-windows 'summary 'force)
3517           (gnus-set-mode-line 'summary))
3518         (when (get-buffer-window gnus-group-buffer t)
3519           ;; Gotta use windows, because recenter does weird stuff if
3520           ;; the current buffer ain't the displayed window.
3521           (let ((owin (selected-window)))
3522             (select-window (get-buffer-window gnus-group-buffer t))
3523             (when (gnus-group-goto-group group)
3524               (recenter))
3525             (select-window owin)))
3526         ;; Mark this buffer as "prepared".
3527         (setq gnus-newsgroup-prepared t)
3528         (gnus-run-hooks 'gnus-summary-prepared-hook)
3529         (unless (gnus-ephemeral-group-p group)
3530           (gnus-group-update-group group))
3531         t)))))
3532
3533 (defun gnus-summary-auto-select-subject ()
3534   "Select the subject line on initial group entry."
3535   (goto-char (point-min))
3536   (cond
3537    ((eq gnus-auto-select-subject 'best)
3538     (gnus-summary-best-unread-subject))
3539    ((eq gnus-auto-select-subject 'unread)
3540     (gnus-summary-first-unread-subject))
3541    ((eq gnus-auto-select-subject 'unseen)
3542     (gnus-summary-first-unseen-subject))
3543    ((eq gnus-auto-select-subject 'unseen-or-unread)
3544     (gnus-summary-first-unseen-or-unread-subject))
3545    ((eq gnus-auto-select-subject 'first)
3546     ;; Do nothing.
3547     )
3548    ((gnus-functionp gnus-auto-select-subject)
3549     (funcall gnus-auto-select-subject))))
3550
3551 (defun gnus-summary-prepare ()
3552   "Generate the summary buffer."
3553   (interactive)
3554   (let ((buffer-read-only nil))
3555     (erase-buffer)
3556     (setq gnus-newsgroup-data nil
3557           gnus-newsgroup-data-reverse nil)
3558     (gnus-run-hooks 'gnus-summary-generate-hook)
3559     ;; Generate the buffer, either with threads or without.
3560     (when gnus-newsgroup-headers
3561       (gnus-summary-prepare-threads
3562        (if gnus-show-threads
3563            (gnus-sort-gathered-threads
3564             (funcall gnus-summary-thread-gathering-function
3565                      (gnus-sort-threads
3566                       (gnus-cut-threads (gnus-make-threads)))))
3567          ;; Unthreaded display.
3568          (gnus-sort-articles gnus-newsgroup-headers))))
3569     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3570     ;; Call hooks for modifying summary buffer.
3571     (goto-char (point-min))
3572     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3573
3574 (defsubst gnus-general-simplify-subject (subject)
3575   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3576   (setq subject
3577         (cond
3578          ;; Truncate the subject.
3579          (gnus-simplify-subject-functions
3580           (gnus-map-function gnus-simplify-subject-functions subject))
3581          ((numberp gnus-summary-gather-subject-limit)
3582           (setq subject (gnus-simplify-subject-re subject))
3583           (if (> (length subject) gnus-summary-gather-subject-limit)
3584               (substring subject 0 gnus-summary-gather-subject-limit)
3585             subject))
3586          ;; Fuzzily simplify it.
3587          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3588           (gnus-simplify-subject-fuzzy subject))
3589          ;; Just remove the leading "Re:".
3590          (t
3591           (gnus-simplify-subject-re subject))))
3592
3593   (if (and gnus-summary-gather-exclude-subject
3594            (string-match gnus-summary-gather-exclude-subject subject))
3595       nil                         ; This article shouldn't be gathered
3596     subject))
3597
3598 (defun gnus-summary-simplify-subject-query ()
3599   "Query where the respool algorithm would put this article."
3600   (interactive)
3601   (gnus-summary-select-article)
3602   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3603
3604 (defun gnus-gather-threads-by-subject (threads)
3605   "Gather threads by looking at Subject headers."
3606   (if (not gnus-summary-make-false-root)
3607       threads
3608     (let ((hashtb (gnus-make-hashtable 1024))
3609           (prev threads)
3610           (result threads)
3611           subject hthread whole-subject)
3612       (while threads
3613         (setq subject (gnus-general-simplify-subject
3614                        (setq whole-subject (mail-header-subject
3615                                             (caar threads)))))
3616         (when subject
3617           (if (setq hthread (gnus-gethash subject hashtb))
3618               (progn
3619                 ;; We enter a dummy root into the thread, if we
3620                 ;; haven't done that already.
3621                 (unless (stringp (caar hthread))
3622                   (setcar hthread (list whole-subject (car hthread))))
3623                 ;; We add this new gathered thread to this gathered
3624                 ;; thread.
3625                 (setcdr (car hthread)
3626                         (nconc (cdar hthread) (list (car threads))))
3627                 ;; Remove it from the list of threads.
3628                 (setcdr prev (cdr threads))
3629                 (setq threads prev))
3630             ;; Enter this thread into the hash table.
3631             (gnus-sethash subject
3632                           (if gnus-summary-make-false-root-always
3633                               (progn
3634                                 ;; If you want a dummy root above all
3635                                 ;; threads...
3636                                 (setcar threads (list whole-subject
3637                                                       (car threads)))
3638                                 threads)
3639                             threads)
3640                           hashtb)))
3641         (setq prev threads)
3642         (setq threads (cdr threads)))
3643       result)))
3644
3645 (defun gnus-gather-threads-by-references (threads)
3646   "Gather threads by looking at References headers."
3647   (let ((idhashtb (gnus-make-hashtable 1024))
3648         (thhashtb (gnus-make-hashtable 1024))
3649         (prev threads)
3650         (result threads)
3651         ids references id gthread gid entered ref)
3652     (while threads
3653       (when (setq references (mail-header-references (caar threads)))
3654         (setq id (mail-header-id (caar threads))
3655               ids (inline (gnus-split-references references))
3656               entered nil)
3657         (while (setq ref (pop ids))
3658           (setq ids (delete ref ids))
3659           (if (not (setq gid (gnus-gethash ref idhashtb)))
3660               (progn
3661                 (gnus-sethash ref id idhashtb)
3662                 (gnus-sethash id threads thhashtb))
3663             (setq gthread (gnus-gethash gid thhashtb))
3664             (unless entered
3665               ;; We enter a dummy root into the thread, if we
3666               ;; haven't done that already.
3667               (unless (stringp (caar gthread))
3668                 (setcar gthread (list (mail-header-subject (caar gthread))
3669                                       (car gthread))))
3670               ;; We add this new gathered thread to this gathered
3671               ;; thread.
3672               (setcdr (car gthread)
3673                       (nconc (cdar gthread) (list (car threads)))))
3674             ;; Add it into the thread hash table.
3675             (gnus-sethash id gthread thhashtb)
3676             (setq entered t)
3677             ;; Remove it from the list of threads.
3678             (setcdr prev (cdr threads))
3679             (setq threads prev))))
3680       (setq prev threads)
3681       (setq threads (cdr threads)))
3682     result))
3683
3684 (defun gnus-sort-gathered-threads (threads)
3685   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3686   (let ((result threads))
3687     (while threads
3688       (when (stringp (caar threads))
3689         (setcdr (car threads)
3690                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3691       (setq threads (cdr threads)))
3692     result))
3693
3694 (defun gnus-thread-loop-p (root thread)
3695   "Say whether ROOT is in THREAD."
3696   (let ((stack (list thread))
3697         (infloop 0)
3698         th)
3699     (while (setq thread (pop stack))
3700       (setq th (cdr thread))
3701       (while (and th
3702                   (not (eq (caar th) root)))
3703         (pop th))
3704       (if th
3705           ;; We have found a loop.
3706           (let (ref-dep)
3707             (setcdr thread (delq (car th) (cdr thread)))
3708             (if (boundp (setq ref-dep (intern "none"
3709                                               gnus-newsgroup-dependencies)))
3710                 (setcdr (symbol-value ref-dep)
3711                         (nconc (cdr (symbol-value ref-dep))
3712                                (list (car th))))
3713               (set ref-dep (list nil (car th))))
3714             (setq infloop 1
3715                   stack nil))
3716         ;; Push all the subthreads onto the stack.
3717         (push (cdr thread) stack)))
3718     infloop))
3719
3720 (defun gnus-make-threads ()
3721   "Go through the dependency hashtb and find the roots.  Return all threads."
3722   (let (threads)
3723     (while (catch 'infloop
3724              (mapatoms
3725               (lambda (refs)
3726                 ;; Deal with self-referencing References loops.
3727                 (when (and (car (symbol-value refs))
3728                            (not (zerop
3729                                  (apply
3730                                   '+
3731                                   (mapcar
3732                                    (lambda (thread)
3733                                      (gnus-thread-loop-p
3734                                       (car (symbol-value refs)) thread))
3735                                    (cdr (symbol-value refs)))))))
3736                   (setq threads nil)
3737                   (throw 'infloop t))
3738                 (unless (car (symbol-value refs))
3739                   ;; These threads do not refer back to any other
3740                   ;; articles, so they're roots.
3741                   (setq threads (append (cdr (symbol-value refs)) threads))))
3742               gnus-newsgroup-dependencies)))
3743     threads))
3744
3745 ;; Build the thread tree.
3746 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3747   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3748
3749 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3750 if it was already present.
3751
3752 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3753 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3754 Message-IDs will be renamed to a unique Message-ID before being
3755 entered.
3756
3757 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3758   (let* ((id (mail-header-id header))
3759          (id-dep (and id (intern id dependencies)))
3760          parent-id ref ref-dep ref-header replaced)
3761     ;; Enter this `header' in the `dependencies' table.
3762     (cond
3763      ((not id-dep)
3764       (setq header nil))
3765      ;; The first two cases do the normal part: enter a new `header'
3766      ;; in the `dependencies' table.
3767      ((not (boundp id-dep))
3768       (set id-dep (list header)))
3769      ((null (car (symbol-value id-dep)))
3770       (setcar (symbol-value id-dep) header))
3771
3772      ;; From here the `header' was already present in the
3773      ;; `dependencies' table.
3774      (force-new
3775       ;; Overrides an existing entry;
3776       ;; just set the header part of the entry.
3777       (setcar (symbol-value id-dep) header)
3778       (setq replaced t))
3779
3780      ;; Renames the existing `header' to a unique Message-ID.
3781      ((not gnus-summary-ignore-duplicates)
3782       ;; An article with this Message-ID has already been seen.
3783       ;; We rename the Message-ID.
3784       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3785            (list header))
3786       (mail-header-set-id header id))
3787
3788      ;; The last case ignores an existing entry, except it adds any
3789      ;; additional Xrefs (in case the two articles came from different
3790      ;; servers.
3791      ;; Also sets `header' to `nil' meaning that the `dependencies'
3792      ;; table was *not* modified.
3793      (t
3794       (mail-header-set-xref
3795        (car (symbol-value id-dep))
3796        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3797                    "")
3798                (or (mail-header-xref header) "")))
3799       (setq header nil)))
3800
3801     (when (and header (not replaced))
3802       ;; First check that we are not creating a References loop.
3803       (setq parent-id (gnus-parent-id (mail-header-references header)))
3804       (setq ref parent-id)
3805       (while (and ref
3806                   (setq ref-dep (intern-soft ref dependencies))
3807                   (boundp ref-dep)
3808                   (setq ref-header (car (symbol-value ref-dep))))
3809         (if (string= id ref)
3810             ;; Yuk!  This is a reference loop.  Make the article be a
3811             ;; root article.
3812             (progn
3813               (mail-header-set-references (car (symbol-value id-dep)) "none")
3814               (setq ref nil)
3815               (setq parent-id nil))
3816           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3817       (setq ref-dep (intern (or parent-id "none") dependencies))
3818       (if (boundp ref-dep)
3819           (setcdr (symbol-value ref-dep)
3820                   (nconc (cdr (symbol-value ref-dep))
3821                          (list (symbol-value id-dep))))
3822         (set ref-dep (list nil (symbol-value id-dep)))))
3823     header))
3824
3825 (defun gnus-extract-message-id-from-in-reply-to (string)
3826   (if (string-match "<[^>]+>" string)
3827       (substring string (match-beginning 0) (match-end 0))
3828     nil))
3829
3830 (defun gnus-build-sparse-threads ()
3831   (let ((headers gnus-newsgroup-headers)
3832         (mail-parse-charset gnus-newsgroup-charset)
3833         (gnus-summary-ignore-duplicates t)
3834         header references generation relations
3835         subject child end new-child date)
3836     ;; First we create an alist of generations/relations, where
3837     ;; generations is how much we trust the relation, and the relation
3838     ;; is parent/child.
3839     (gnus-message 7 "Making sparse threads...")
3840     (save-excursion
3841       (nnheader-set-temp-buffer " *gnus sparse threads*")
3842       (while (setq header (pop headers))
3843         (when (and (setq references (mail-header-references header))
3844                    (not (string= references "")))
3845           (insert references)
3846           (setq child (mail-header-id header)
3847                 subject (mail-header-subject header)
3848                 date (mail-header-date header)
3849                 generation 0)
3850           (while (search-backward ">" nil t)
3851             (setq end (1+ (point)))
3852             (when (search-backward "<" nil t)
3853               (setq new-child (buffer-substring (point) end))
3854               (push (list (incf generation)
3855                           child (setq child new-child)
3856                           subject date)
3857                     relations)))
3858           (when child
3859             (push (list (1+ generation) child nil subject) relations))
3860           (erase-buffer)))
3861       (kill-buffer (current-buffer)))
3862     ;; Sort over trustworthiness.
3863     (mapcar
3864      (lambda (relation)
3865        (when (gnus-dependencies-add-header
3866               (make-full-mail-header
3867                gnus-reffed-article-number
3868                (nth 3 relation) "" (or (nth 4 relation) "")
3869                (nth 1 relation)
3870                (or (nth 2 relation) "") 0 0 "")
3871               gnus-newsgroup-dependencies nil)
3872          (push gnus-reffed-article-number gnus-newsgroup-limit)
3873          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3874          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3875                gnus-newsgroup-reads)
3876          (decf gnus-reffed-article-number)))
3877      (sort relations 'car-less-than-car))
3878     (gnus-message 7 "Making sparse threads...done")))
3879
3880 (defun gnus-build-old-threads ()
3881   ;; Look at all the articles that refer back to old articles, and
3882   ;; fetch the headers for the articles that aren't there.  This will
3883   ;; build complete threads - if the roots haven't been expired by the
3884   ;; server, that is.
3885   (let ((mail-parse-charset gnus-newsgroup-charset)
3886         id heads)
3887     (mapatoms
3888      (lambda (refs)
3889        (when (not (car (symbol-value refs)))
3890          (setq heads (cdr (symbol-value refs)))
3891          (while heads
3892            (if (memq (mail-header-number (caar heads))
3893                      gnus-newsgroup-dormant)
3894                (setq heads (cdr heads))
3895              (setq id (symbol-name refs))
3896              (while (and (setq id (gnus-build-get-header id))
3897                          (not (car (gnus-id-to-thread id)))))
3898              (setq heads nil)))))
3899      gnus-newsgroup-dependencies)))
3900
3901 ;; This function has to be called with point after the article number
3902 ;; on the beginning of the line.
3903 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3904   (let ((eol (gnus-point-at-eol))
3905         (buffer (current-buffer))
3906         header references in-reply-to)
3907
3908     ;; overview: [num subject from date id refs chars lines misc]
3909     (unwind-protect
3910         (let (x)
3911           (narrow-to-region (point) eol)
3912           (unless (eobp)
3913             (forward-char))
3914
3915           (setq header
3916                 (make-full-mail-header
3917                  number                 ; number
3918                  (condition-case ()     ; subject
3919                      (funcall gnus-decode-encoded-word-function
3920                               (setq x (nnheader-nov-field)))
3921                    (error x))
3922                  (condition-case ()     ; from
3923                      (funcall gnus-decode-encoded-word-function
3924                               (setq x (nnheader-nov-field)))
3925                    (error x))
3926                  (nnheader-nov-field)   ; date
3927                  (nnheader-nov-read-message-id) ; id
3928                  (setq references (nnheader-nov-field)) ; refs
3929                  (nnheader-nov-read-integer) ; chars
3930                  (nnheader-nov-read-integer) ; lines
3931                  (unless (eobp)
3932                    (if (looking-at "Xref: ")
3933                        (goto-char (match-end 0)))
3934                    (nnheader-nov-field)) ; Xref
3935                  (nnheader-nov-parse-extra)))) ; extra
3936
3937       (widen))
3938
3939     (when (and (string= references "")
3940                (setq in-reply-to (mail-header-extra header))
3941                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3942       (mail-header-set-references
3943        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3944
3945     (when gnus-alter-header-function
3946       (funcall gnus-alter-header-function header))
3947     (gnus-dependencies-add-header header dependencies force-new)))
3948
3949 (defun gnus-build-get-header (id)
3950   "Look through the buffer of NOV lines and find the header to ID.
3951 Enter this line into the dependencies hash table, and return
3952 the id of the parent article (if any)."
3953   (let ((deps gnus-newsgroup-dependencies)
3954         found header)
3955     (prog1
3956         (save-excursion
3957           (set-buffer nntp-server-buffer)
3958           (let ((case-fold-search nil))
3959             (goto-char (point-min))
3960             (while (and (not found)
3961                         (search-forward id nil t))
3962               (beginning-of-line)
3963               (setq found (looking-at
3964                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3965                                    (regexp-quote id))))
3966               (or found (beginning-of-line 2)))
3967             (when found
3968               (beginning-of-line)
3969               (and
3970                (setq header (gnus-nov-parse-line
3971                              (read (current-buffer)) deps))
3972                (gnus-parent-id (mail-header-references header))))))
3973       (when header
3974         (let ((number (mail-header-number header)))
3975           (push number gnus-newsgroup-limit)
3976           (push header gnus-newsgroup-headers)
3977           (if (memq number gnus-newsgroup-unselected)
3978               (progn
3979                 (setq gnus-newsgroup-unreads
3980                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
3981                                                number))
3982                 (setq gnus-newsgroup-unselected
3983                       (delq number gnus-newsgroup-unselected)))
3984             (push number gnus-newsgroup-ancient)))))))
3985
3986 (defun gnus-build-all-threads ()
3987   "Read all the headers."
3988   (let ((gnus-summary-ignore-duplicates t)
3989         (mail-parse-charset gnus-newsgroup-charset)
3990         (dependencies gnus-newsgroup-dependencies)
3991         header article)
3992     (save-excursion
3993       (set-buffer nntp-server-buffer)
3994       (let ((case-fold-search nil))
3995         (goto-char (point-min))
3996         (while (not (eobp))
3997           (ignore-errors
3998             (setq article (read (current-buffer))
3999                   header (gnus-nov-parse-line article dependencies)))
4000           (when header
4001             (save-excursion
4002               (set-buffer gnus-summary-buffer)
4003               (push header gnus-newsgroup-headers)
4004               (if (memq (setq article (mail-header-number header))
4005                         gnus-newsgroup-unselected)
4006                   (progn
4007                     (setq gnus-newsgroup-unreads
4008                           (gnus-add-to-sorted-list
4009                            gnus-newsgroup-unreads article))
4010                     (setq gnus-newsgroup-unselected
4011                           (delq article gnus-newsgroup-unselected)))
4012                 (push article gnus-newsgroup-ancient)))
4013             (forward-line 1)))))))
4014
4015 (defun gnus-summary-update-article-line (article header)
4016   "Update the line for ARTICLE using HEADERS."
4017   (let* ((id (mail-header-id header))
4018          (thread (gnus-id-to-thread id)))
4019     (unless thread
4020       (error "Article in no thread"))
4021     ;; Update the thread.
4022     (setcar thread header)
4023     (gnus-summary-goto-subject article)
4024     (let* ((datal (gnus-data-find-list article))
4025            (data (car datal))
4026            (length (when (cdr datal)
4027                      (- (gnus-data-pos data)
4028                         (gnus-data-pos (cadr datal)))))
4029            (buffer-read-only nil)
4030            (level (gnus-summary-thread-level)))
4031       (gnus-delete-line)
4032       (gnus-summary-insert-line
4033        header level nil 
4034        (memq article gnus-newsgroup-undownloaded)
4035        (gnus-article-mark article)
4036        (memq article gnus-newsgroup-replied)
4037        (memq article gnus-newsgroup-expirable)
4038        ;; Only insert the Subject string when it's different
4039        ;; from the previous Subject string.
4040        (if (and
4041             gnus-show-threads
4042             (gnus-subject-equal
4043              (condition-case ()
4044                  (mail-header-subject
4045                   (gnus-data-header
4046                    (cadr
4047                     (gnus-data-find-list
4048                      article
4049                      (gnus-data-list t)))))
4050                ;; Error on the side of excessive subjects.
4051                (error ""))
4052              (mail-header-subject header)))
4053            ""
4054          (mail-header-subject header))
4055        nil (cdr (assq article gnus-newsgroup-scored))
4056        (memq article gnus-newsgroup-processable))
4057       (when length
4058         (gnus-data-update-list
4059          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
4060
4061 (defun gnus-summary-update-article (article &optional iheader)
4062   "Update ARTICLE in the summary buffer."
4063   (set-buffer gnus-summary-buffer)
4064   (let* ((header (gnus-summary-article-header article))
4065          (id (mail-header-id header))
4066          (data (gnus-data-find article))
4067          (thread (gnus-id-to-thread id))
4068          (references (mail-header-references header))
4069          (parent
4070           (gnus-id-to-thread
4071            (or (gnus-parent-id
4072                 (when (and references
4073                            (not (equal "" references)))
4074                   references))
4075                "none")))
4076          (buffer-read-only nil)
4077          (old (car thread)))
4078     (when thread
4079       (unless iheader
4080         (setcar thread nil)
4081         (when parent
4082           (delq thread parent)))
4083       (if (gnus-summary-insert-subject id header)
4084           ;; Set the (possibly) new article number in the data structure.
4085           (gnus-data-set-number data (gnus-id-to-article id))
4086         (setcar thread old)
4087         nil))))
4088
4089 (defun gnus-rebuild-thread (id &optional line)
4090   "Rebuild the thread containing ID.
4091 If LINE, insert the rebuilt thread starting on line LINE."
4092   (let ((buffer-read-only nil)
4093         old-pos current thread data)
4094     (if (not gnus-show-threads)
4095         (setq thread (list (car (gnus-id-to-thread id))))
4096       ;; Get the thread this article is part of.
4097       (setq thread (gnus-remove-thread id)))
4098     (setq old-pos (gnus-point-at-bol))
4099     (setq current (save-excursion
4100                     (and (re-search-backward "[\r\n]" nil t)
4101                          (gnus-summary-article-number))))
4102     ;; If this is a gathered thread, we have to go some re-gathering.
4103     (when (stringp (car thread))
4104       (let ((subject (car thread))
4105             roots thr)
4106         (setq thread (cdr thread))
4107         (while thread
4108           (unless (memq (setq thr (gnus-id-to-thread
4109                                    (gnus-root-id
4110                                     (mail-header-id (caar thread)))))
4111                         roots)
4112             (push thr roots))
4113           (setq thread (cdr thread)))
4114         ;; We now have all (unique) roots.
4115         (if (= (length roots) 1)
4116             ;; All the loose roots are now one solid root.
4117             (setq thread (car roots))
4118           (setq thread (cons subject (gnus-sort-threads roots))))))
4119     (let (threads)
4120       ;; We then insert this thread into the summary buffer.
4121       (when line
4122         (goto-char (point-min))
4123         (forward-line (1- line)))
4124       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4125         (if gnus-show-threads
4126             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4127           (gnus-summary-prepare-unthreaded thread))
4128         (setq data (nreverse gnus-newsgroup-data))
4129         (setq threads gnus-newsgroup-threads))
4130       ;; We splice the new data into the data structure.
4131       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4132       ;;!!! then we want to insert at the beginning of the buffer.
4133       ;;!!! That happens to be true with Gnus now, but that may
4134       ;;!!! change in the future.  Perhaps.
4135       (gnus-data-enter-list
4136        (if line nil current) data (- (point) old-pos))
4137       (setq gnus-newsgroup-threads
4138             (nconc threads gnus-newsgroup-threads))
4139       (gnus-data-compute-positions))))
4140
4141 (defun gnus-number-to-header (number)
4142   "Return the header for article NUMBER."
4143   (let ((headers gnus-newsgroup-headers))
4144     (while (and headers
4145                 (not (= number (mail-header-number (car headers)))))
4146       (pop headers))
4147     (when headers
4148       (car headers))))
4149
4150 (defun gnus-parent-headers (in-headers &optional generation)
4151   "Return the headers of the GENERATIONeth parent of HEADERS."
4152   (unless generation
4153     (setq generation 1))
4154   (let ((parent t)
4155         (headers in-headers)
4156         references)
4157     (while (and parent
4158                 (not (zerop generation))
4159                 (setq references (mail-header-references headers)))
4160       (setq headers (if (and references
4161                              (setq parent (gnus-parent-id references)))
4162                         (car (gnus-id-to-thread parent))
4163                       nil))
4164       (decf generation))
4165     (and (not (eq headers in-headers))
4166          headers)))
4167
4168 (defun gnus-id-to-thread (id)
4169   "Return the (sub-)thread where ID appears."
4170   (gnus-gethash id gnus-newsgroup-dependencies))
4171
4172 (defun gnus-id-to-article (id)
4173   "Return the article number of ID."
4174   (let ((thread (gnus-id-to-thread id)))
4175     (when (and thread
4176                (car thread))
4177       (mail-header-number (car thread)))))
4178
4179 (defun gnus-id-to-header (id)
4180   "Return the article headers of ID."
4181   (car (gnus-id-to-thread id)))
4182
4183 (defun gnus-article-displayed-root-p (article)
4184   "Say whether ARTICLE is a root(ish) article."
4185   (let ((level (gnus-summary-thread-level article))
4186         (refs (mail-header-references  (gnus-summary-article-header article)))
4187         particle)
4188     (cond
4189      ((null level) nil)
4190      ((zerop level) t)
4191      ((null refs) t)
4192      ((null (gnus-parent-id refs)) t)
4193      ((and (= 1 level)
4194            (null (setq particle (gnus-id-to-article
4195                                  (gnus-parent-id refs))))
4196            (null (gnus-summary-thread-level particle)))))))
4197
4198 (defun gnus-root-id (id)
4199   "Return the id of the root of the thread where ID appears."
4200   (let (last-id prev)
4201     (while (and id (setq prev (car (gnus-id-to-thread id))))
4202       (setq last-id id
4203             id (gnus-parent-id (mail-header-references prev))))
4204     last-id))
4205
4206 (defun gnus-articles-in-thread (thread)
4207   "Return the list of articles in THREAD."
4208   (cons (mail-header-number (car thread))
4209         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4210
4211 (defun gnus-remove-thread (id &optional dont-remove)
4212   "Remove the thread that has ID in it."
4213   (let (headers thread last-id)
4214     ;; First go up in this thread until we find the root.
4215     (setq last-id (gnus-root-id id)
4216           headers (message-flatten-list (gnus-id-to-thread last-id)))
4217     ;; We have now found the real root of this thread.  It might have
4218     ;; been gathered into some loose thread, so we have to search
4219     ;; through the threads to find the thread we wanted.
4220     (let ((threads gnus-newsgroup-threads)
4221           sub)
4222       (while threads
4223         (setq sub (car threads))
4224         (if (stringp (car sub))
4225             ;; This is a gathered thread, so we look at the roots
4226             ;; below it to find whether this article is in this
4227             ;; gathered root.
4228             (progn
4229               (setq sub (cdr sub))
4230               (while sub
4231                 (when (member (caar sub) headers)
4232                   (setq thread (car threads)
4233                         threads nil
4234                         sub nil))
4235                 (setq sub (cdr sub))))
4236           ;; It's an ordinary thread, so we check it.
4237           (when (eq (car sub) (car headers))
4238             (setq thread sub
4239                   threads nil)))
4240         (setq threads (cdr threads)))
4241       ;; If this article is in no thread, then it's a root.
4242       (if thread
4243           (unless dont-remove
4244             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4245         (setq thread (gnus-id-to-thread last-id)))
4246       (when thread
4247         (prog1
4248             thread                      ; We return this thread.
4249           (unless dont-remove
4250             (if (stringp (car thread))
4251                 (progn
4252                   ;; If we use dummy roots, then we have to remove the
4253                   ;; dummy root as well.
4254                   (when (eq gnus-summary-make-false-root 'dummy)
4255                     ;; We go to the dummy root by going to
4256                     ;; the first sub-"thread", and then one line up.
4257                     (gnus-summary-goto-article
4258                      (mail-header-number (caadr thread)))
4259                     (forward-line -1)
4260                     (gnus-delete-line)
4261                     (gnus-data-compute-positions))
4262                   (setq thread (cdr thread))
4263                   (while thread
4264                     (gnus-remove-thread-1 (car thread))
4265                     (setq thread (cdr thread))))
4266               (gnus-remove-thread-1 thread))))))))
4267
4268 (defun gnus-remove-thread-1 (thread)
4269   "Remove the thread THREAD recursively."
4270   (let ((number (mail-header-number (pop thread)))
4271         d)
4272     (setq thread (reverse thread))
4273     (while thread
4274       (gnus-remove-thread-1 (pop thread)))
4275     (when (setq d (gnus-data-find number))
4276       (goto-char (gnus-data-pos d))
4277       (gnus-summary-show-thread)
4278       (gnus-data-remove
4279        number
4280        (- (gnus-point-at-bol)
4281           (prog1
4282               (1+ (gnus-point-at-eol))
4283             (gnus-delete-line)))))))
4284
4285 (defun gnus-sort-threads-1 (threads func)
4286   (sort (mapcar (lambda (thread)
4287                   (cons (car thread)
4288                         (and (cdr thread)
4289                              (gnus-sort-threads-1 (cdr thread) func))))
4290                 threads) func))
4291
4292 (defun gnus-sort-threads (threads)
4293   "Sort THREADS."
4294   (if (not gnus-thread-sort-functions)
4295       threads
4296     (gnus-message 8 "Sorting threads...")
4297     (let ((max-lisp-eval-depth 5000))
4298       (prog1 (gnus-sort-threads-1
4299          threads
4300          (gnus-make-sort-function gnus-thread-sort-functions))
4301         (gnus-message 8 "Sorting threads...done")))))
4302
4303 (defun gnus-sort-articles (articles)
4304   "Sort ARTICLES."
4305   (when gnus-article-sort-functions
4306     (gnus-message 7 "Sorting articles...")
4307     (prog1
4308         (setq gnus-newsgroup-headers
4309               (sort articles (gnus-make-sort-function
4310                               gnus-article-sort-functions)))
4311       (gnus-message 7 "Sorting articles...done"))))
4312
4313 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4314 (defmacro gnus-thread-header (thread)
4315   "Return header of first article in THREAD.
4316 Note that THREAD must never, ever be anything else than a variable -
4317 using some other form will lead to serious barfage."
4318   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4319   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4320   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4321         (vector thread) 2))
4322
4323 (defsubst gnus-article-sort-by-number (h1 h2)
4324   "Sort articles by article number."
4325   (< (mail-header-number h1)
4326      (mail-header-number h2)))
4327
4328 (defun gnus-thread-sort-by-number (h1 h2)
4329   "Sort threads by root article number."
4330   (gnus-article-sort-by-number
4331    (gnus-thread-header h1) (gnus-thread-header h2)))
4332
4333 (defsubst gnus-article-sort-by-random (h1 h2)
4334   "Sort articles by article number."
4335   (zerop (random 2)))
4336
4337 (defun gnus-thread-sort-by-random (h1 h2)
4338   "Sort threads by root article number."
4339   (gnus-article-sort-by-random
4340    (gnus-thread-header h1) (gnus-thread-header h2)))
4341
4342 (defsubst gnus-article-sort-by-lines (h1 h2)
4343   "Sort articles by article Lines header."
4344   (< (mail-header-lines h1)
4345      (mail-header-lines h2)))
4346
4347 (defun gnus-thread-sort-by-lines (h1 h2)
4348   "Sort threads by root article Lines header."
4349   (gnus-article-sort-by-lines
4350    (gnus-thread-header h1) (gnus-thread-header h2)))
4351
4352 (defsubst gnus-article-sort-by-chars (h1 h2)
4353   "Sort articles by octet length."
4354   (< (mail-header-chars h1)
4355      (mail-header-chars h2)))
4356
4357 (defun gnus-thread-sort-by-chars (h1 h2)
4358   "Sort threads by root article octet length."
4359   (gnus-article-sort-by-chars
4360    (gnus-thread-header h1) (gnus-thread-header h2)))
4361
4362 (defsubst gnus-article-sort-by-author (h1 h2)
4363   "Sort articles by root author."
4364   (string-lessp
4365    (let ((extract (funcall
4366                    gnus-extract-address-components
4367                    (mail-header-from h1))))
4368      (or (car extract) (cadr extract) ""))
4369    (let ((extract (funcall
4370                    gnus-extract-address-components
4371                    (mail-header-from h2))))
4372      (or (car extract) (cadr extract) ""))))
4373
4374 (defun gnus-thread-sort-by-author (h1 h2)
4375   "Sort threads by root author."
4376   (gnus-article-sort-by-author
4377    (gnus-thread-header h1)  (gnus-thread-header h2)))
4378
4379 (defsubst gnus-article-sort-by-subject (h1 h2)
4380   "Sort articles by root subject."
4381   (string-lessp
4382    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4383    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4384
4385 (defun gnus-thread-sort-by-subject (h1 h2)
4386   "Sort threads by root subject."
4387   (gnus-article-sort-by-subject
4388    (gnus-thread-header h1) (gnus-thread-header h2)))
4389
4390 (defsubst gnus-article-sort-by-date (h1 h2)
4391   "Sort articles by root article date."
4392   (time-less-p
4393    (gnus-date-get-time (mail-header-date h1))
4394    (gnus-date-get-time (mail-header-date h2))))
4395
4396 (defun gnus-thread-sort-by-date (h1 h2)
4397   "Sort threads by root article date."
4398   (gnus-article-sort-by-date
4399    (gnus-thread-header h1) (gnus-thread-header h2)))
4400
4401 (defsubst gnus-article-sort-by-score (h1 h2)
4402   "Sort articles by root article score.
4403 Unscored articles will be counted as having a score of zero."
4404   (> (or (cdr (assq (mail-header-number h1)
4405                     gnus-newsgroup-scored))
4406          gnus-summary-default-score 0)
4407      (or (cdr (assq (mail-header-number h2)
4408                     gnus-newsgroup-scored))
4409          gnus-summary-default-score 0)))
4410
4411 (defun gnus-thread-sort-by-score (h1 h2)
4412   "Sort threads by root article score."
4413   (gnus-article-sort-by-score
4414    (gnus-thread-header h1) (gnus-thread-header h2)))
4415
4416 (defun gnus-thread-sort-by-total-score (h1 h2)
4417   "Sort threads by the sum of all scores in the thread.
4418 Unscored articles will be counted as having a score of zero."
4419   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4420
4421 (defun gnus-thread-total-score (thread)
4422   ;; This function find the total score of THREAD.
4423   (cond
4424    ((null thread)
4425     0)
4426    ((consp thread)
4427     (if (stringp (car thread))
4428         (apply gnus-thread-score-function 0
4429                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4430       (gnus-thread-total-score-1 thread)))
4431    (t
4432     (gnus-thread-total-score-1 (list thread)))))
4433
4434 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4435   "Sort threads such that the thread with the most recently arrived article comes first."
4436   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4437
4438 (defun gnus-thread-highest-number (thread)
4439   "Return the highest article number in THREAD."
4440   (apply 'max (mapcar (lambda (header)
4441                         (mail-header-number header))
4442                       (message-flatten-list thread))))
4443
4444 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4445   "Sort threads such that the thread with the most recently dated article comes first."
4446   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4447
4448 (defun gnus-thread-latest-date (thread)
4449   "Return the highest article date in THREAD."
4450   (let ((previous-time 0))
4451     (apply 'max
4452            (mapcar
4453             (lambda (header)
4454               (setq previous-time
4455                     (time-to-seconds
4456                      (condition-case ()
4457                          (mail-header-parse-date (mail-header-date header))
4458                        (error previous-time)))))
4459             (sort
4460              (message-flatten-list thread)
4461              (lambda (h1 h2)
4462                (< (mail-header-number h1)
4463                   (mail-header-number h2))))))))
4464
4465 (defun gnus-thread-total-score-1 (root)
4466   ;; This function find the total score of the thread below ROOT.
4467   (setq root (car root))
4468   (apply gnus-thread-score-function
4469          (or (append
4470               (mapcar 'gnus-thread-total-score
4471                       (cdr (gnus-id-to-thread (mail-header-id root))))
4472               (when (> (mail-header-number root) 0)
4473                 (list (or (cdr (assq (mail-header-number root)
4474                                      gnus-newsgroup-scored))
4475                           gnus-summary-default-score 0))))
4476              (list gnus-summary-default-score)
4477              '(0))))
4478
4479 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4480 (defvar gnus-tmp-prev-subject nil)
4481 (defvar gnus-tmp-false-parent nil)
4482 (defvar gnus-tmp-root-expunged nil)
4483 (defvar gnus-tmp-dummy-line nil)
4484
4485 (eval-when-compile (defvar gnus-tmp-header))
4486 (defun gnus-extra-header (type &optional header)
4487   "Return the extra header of TYPE."
4488   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4489       ""))
4490
4491 (defvar gnus-tmp-thread-tree-header-string "")
4492
4493 (defcustom gnus-sum-thread-tree-root "> "
4494   "With %B spec, used for the root of a thread.
4495 If nil, use subject instead."
4496   :type 'string
4497   :group 'gnus-thread)
4498 (defcustom gnus-sum-thread-tree-single-indent ""
4499   "With %B spec, used for a thread with just one message.
4500 If nil, use subject instead."
4501   :type 'string
4502   :group 'gnus-thread)
4503 (defcustom gnus-sum-thread-tree-vertical "| "
4504   "With %B spec, used for drawing a vertical line."
4505   :type 'string
4506   :group 'gnus-thread)
4507 (defcustom gnus-sum-thread-tree-indent "  "
4508   "With %B spec, used for indenting."
4509   :type 'string
4510   :group 'gnus-thread)
4511 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4512   "With %B spec, used for a leaf with brothers."
4513   :type 'string
4514   :group 'gnus-thread)
4515 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4516   "With %B spec, used for a leaf without brothers."
4517   :type 'string
4518   :group 'gnus-thread)
4519
4520 (defun gnus-summary-prepare-threads (threads)
4521   "Prepare summary buffer from THREADS and indentation LEVEL.
4522 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4523 or a straight list of headers."
4524   (gnus-message 7 "Generating summary...")
4525
4526   (setq gnus-newsgroup-threads threads)
4527   (beginning-of-line)
4528
4529   (let ((gnus-tmp-level 0)
4530         (default-score (or gnus-summary-default-score 0))
4531         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4532         (building-line-count gnus-summary-display-while-building)
4533         (building-count (integerp gnus-summary-display-while-building))
4534         thread number subject stack state gnus-tmp-gathered beg-match
4535         new-roots gnus-tmp-new-adopts thread-end simp-subject
4536         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4537         gnus-tmp-replied gnus-tmp-subject-or-nil
4538         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4539         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4540         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4541         tree-stack)
4542
4543     (setq gnus-tmp-prev-subject nil)
4544
4545     (if (vectorp (car threads))
4546         ;; If this is a straight (sic) list of headers, then a
4547         ;; threaded summary display isn't required, so we just create
4548         ;; an unthreaded one.
4549         (gnus-summary-prepare-unthreaded threads)
4550
4551       ;; Do the threaded display.
4552
4553       (if gnus-summary-display-while-building
4554           (switch-to-buffer (buffer-name)))
4555       (while (or threads stack gnus-tmp-new-adopts new-roots)
4556
4557         (if (and (= gnus-tmp-level 0)
4558                  (or (not stack)
4559                      (= (caar stack) 0))
4560                  (not gnus-tmp-false-parent)
4561                  (or gnus-tmp-new-adopts new-roots))
4562             (if gnus-tmp-new-adopts
4563                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4564                       thread (list (car gnus-tmp-new-adopts))
4565                       gnus-tmp-header (caar thread)
4566                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4567               (when new-roots
4568                 (setq thread (list (car new-roots))
4569                       gnus-tmp-header (caar thread)
4570                       new-roots (cdr new-roots))))
4571
4572           (if threads
4573               ;; If there are some threads, we do them before the
4574               ;; threads on the stack.
4575               (setq thread threads
4576                     gnus-tmp-header (caar thread))
4577             ;; There were no current threads, so we pop something off
4578             ;; the stack.
4579             (setq state (car stack)
4580                   gnus-tmp-level (car state)
4581                   tree-stack (cadr state)
4582                   thread (caddr state)
4583                   stack (cdr stack)
4584                   gnus-tmp-header (caar thread))))
4585
4586         (setq gnus-tmp-false-parent nil)
4587         (setq gnus-tmp-root-expunged nil)
4588         (setq thread-end nil)
4589
4590         (if (stringp gnus-tmp-header)
4591             ;; The header is a dummy root.
4592             (cond
4593              ((eq gnus-summary-make-false-root 'adopt)
4594               ;; We let the first article adopt the rest.
4595               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4596                                                (cddar thread)))
4597               (setq gnus-tmp-gathered
4598                     (nconc (mapcar
4599                             (lambda (h) (mail-header-number (car h)))
4600                             (cddar thread))
4601                            gnus-tmp-gathered))
4602               (setq thread (cons (list (caar thread)
4603                                        (cadar thread))
4604                                  (cdr thread)))
4605               (setq gnus-tmp-level -1
4606                     gnus-tmp-false-parent t))
4607              ((eq gnus-summary-make-false-root 'empty)
4608               ;; We print adopted articles with empty subject fields.
4609               (setq gnus-tmp-gathered
4610                     (nconc (mapcar
4611                             (lambda (h) (mail-header-number (car h)))
4612                             (cddar thread))
4613                            gnus-tmp-gathered))
4614               (setq gnus-tmp-level -1))
4615              ((eq gnus-summary-make-false-root 'dummy)
4616               ;; We remember that we probably want to output a dummy
4617               ;; root.
4618               (setq gnus-tmp-dummy-line gnus-tmp-header)
4619               (setq gnus-tmp-prev-subject gnus-tmp-header))
4620              (t
4621               ;; We do not make a root for the gathered
4622               ;; sub-threads at all.
4623               (setq gnus-tmp-level -1)))
4624
4625           (setq number (mail-header-number gnus-tmp-header)
4626                 subject (mail-header-subject gnus-tmp-header)
4627                 simp-subject (gnus-simplify-subject-fully subject))
4628
4629           (cond
4630            ;; If the thread has changed subject, we might want to make
4631            ;; this subthread into a root.
4632            ((and (null gnus-thread-ignore-subject)
4633                  (not (zerop gnus-tmp-level))
4634                  gnus-tmp-prev-subject
4635                  (not (string= gnus-tmp-prev-subject simp-subject)))
4636             (setq new-roots (nconc new-roots (list (car thread)))
4637                   thread-end t
4638                   gnus-tmp-header nil))
4639            ;; If the article lies outside the current limit,
4640            ;; then we do not display it.
4641            ((not (memq number gnus-newsgroup-limit))
4642             (setq gnus-tmp-gathered
4643                   (nconc (mapcar
4644                           (lambda (h) (mail-header-number (car h)))
4645                           (cdar thread))
4646                          gnus-tmp-gathered))
4647             (setq gnus-tmp-new-adopts (if (cdar thread)
4648                                           (append gnus-tmp-new-adopts
4649                                                   (cdar thread))
4650                                         gnus-tmp-new-adopts)
4651                   thread-end t
4652                   gnus-tmp-header nil)
4653             (when (zerop gnus-tmp-level)
4654               (setq gnus-tmp-root-expunged t)))
4655            ;; Perhaps this article is to be marked as read?
4656            ((and gnus-summary-mark-below
4657                  (< (or (cdr (assq number gnus-newsgroup-scored))
4658                         default-score)
4659                     gnus-summary-mark-below)
4660                  ;; Don't touch sparse articles.
4661                  (not (gnus-summary-article-sparse-p number))
4662                  (not (gnus-summary-article-ancient-p number)))
4663             (setq gnus-newsgroup-unreads
4664                   (delq number gnus-newsgroup-unreads))
4665             (if gnus-newsgroup-auto-expire
4666                 (setq gnus-newsgroup-expirable
4667                       (gnus-add-to-sorted-list
4668                        gnus-newsgroup-expirable number))
4669               (push (cons number gnus-low-score-mark)
4670                     gnus-newsgroup-reads))))
4671
4672           (when gnus-tmp-header
4673             ;; We may have an old dummy line to output before this
4674             ;; article.
4675             (when (and gnus-tmp-dummy-line
4676                        (gnus-subject-equal
4677                         gnus-tmp-dummy-line
4678                         (mail-header-subject gnus-tmp-header)))
4679               (gnus-summary-insert-dummy-line
4680                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4681               (setq gnus-tmp-dummy-line nil))
4682
4683             ;; Compute the mark.
4684             (setq gnus-tmp-unread (gnus-article-mark number))
4685
4686             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4687                                   gnus-tmp-header gnus-tmp-level)
4688                   gnus-newsgroup-data)
4689
4690             ;; Actually insert the line.
4691             (setq
4692              gnus-tmp-subject-or-nil
4693              (cond
4694               ((and gnus-thread-ignore-subject
4695                     gnus-tmp-prev-subject
4696                     (not (string= gnus-tmp-prev-subject simp-subject)))
4697                subject)
4698               ((zerop gnus-tmp-level)
4699                (if (and (eq gnus-summary-make-false-root 'empty)
4700                         (memq number gnus-tmp-gathered)
4701                         gnus-tmp-prev-subject
4702                         (string= gnus-tmp-prev-subject simp-subject))
4703                    gnus-summary-same-subject
4704                  subject))
4705               (t gnus-summary-same-subject)))
4706             (if (and (eq gnus-summary-make-false-root 'adopt)
4707                      (= gnus-tmp-level 1)
4708                      (memq number gnus-tmp-gathered))
4709                 (setq gnus-tmp-opening-bracket ?\<
4710                       gnus-tmp-closing-bracket ?\>)
4711               (setq gnus-tmp-opening-bracket ?\[
4712                     gnus-tmp-closing-bracket ?\]))
4713             (setq
4714              gnus-tmp-indentation
4715              (aref gnus-thread-indent-array gnus-tmp-level)
4716              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4717              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4718                                 gnus-summary-default-score 0)
4719              gnus-tmp-score-char
4720              (if (or (null gnus-summary-default-score)
4721                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4722                          gnus-summary-zcore-fuzz))
4723                  ?                      ;Whitespace
4724                (if (< gnus-tmp-score gnus-summary-default-score)
4725                    gnus-score-below-mark gnus-score-over-mark))
4726              gnus-tmp-replied
4727              (cond ((memq number gnus-newsgroup-processable)
4728                     gnus-process-mark)
4729                    ((memq number gnus-newsgroup-cached)
4730                     gnus-cached-mark)
4731                    ((memq number gnus-newsgroup-replied)
4732                     gnus-replied-mark)
4733                    ((memq number gnus-newsgroup-forwarded)
4734                     gnus-forwarded-mark)
4735                    ((memq number gnus-newsgroup-saved)
4736                     gnus-saved-mark)
4737                    ((memq number gnus-newsgroup-recent)
4738                     gnus-recent-mark)
4739                    ((memq number gnus-newsgroup-unseen)
4740                     gnus-unseen-mark)
4741                    (t gnus-no-mark))
4742              gnus-tmp-downloaded
4743              (cond ((memq number gnus-newsgroup-undownloaded) 
4744                     gnus-undownloaded-mark)
4745                    (gnus-newsgroup-agentized
4746                     gnus-downloaded-mark)
4747                    (t
4748                     gnus-no-mark))
4749              gnus-tmp-from (mail-header-from gnus-tmp-header)
4750              gnus-tmp-name
4751              (cond
4752               ((string-match "<[^>]+> *$" gnus-tmp-from)
4753                (setq beg-match (match-beginning 0))
4754                (or (and (string-match "^\".+\"" gnus-tmp-from)
4755                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4756                    (substring gnus-tmp-from 0 beg-match)))
4757               ((string-match "(.+)" gnus-tmp-from)
4758                (substring gnus-tmp-from
4759                           (1+ (match-beginning 0)) (1- (match-end 0))))
4760               (t gnus-tmp-from))
4761              gnus-tmp-thread-tree-header-string
4762              (cond
4763               ((not gnus-show-threads) "")
4764               ((zerop gnus-tmp-level)
4765                (if (cdar thread)
4766                    (or gnus-sum-thread-tree-root subject)
4767                  (or gnus-sum-thread-tree-single-indent subject)))
4768               (t
4769                (concat (apply 'concat
4770                               (mapcar (lambda (item)
4771                                         (if (= item 1)
4772                                             gnus-sum-thread-tree-vertical
4773                                           gnus-sum-thread-tree-indent))
4774                                       (cdr (reverse tree-stack))))
4775                        (if (nth 1 thread)
4776                            gnus-sum-thread-tree-leaf-with-other
4777                          gnus-sum-thread-tree-single-leaf)))))
4778             (when (string= gnus-tmp-name "")
4779               (setq gnus-tmp-name gnus-tmp-from))
4780             (unless (numberp gnus-tmp-lines)
4781               (setq gnus-tmp-lines -1))
4782             (if (= gnus-tmp-lines -1)
4783                 (setq gnus-tmp-lines "?")
4784               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4785             (gnus-put-text-property
4786              (point)
4787              (progn (eval gnus-summary-line-format-spec) (point))
4788              'gnus-number number)
4789             (when gnus-visual-p
4790               (forward-line -1)
4791               (gnus-run-hooks 'gnus-summary-update-hook)
4792               (forward-line 1))
4793
4794             (setq gnus-tmp-prev-subject simp-subject)))
4795
4796         (when (nth 1 thread)
4797           (push (list (max 0 gnus-tmp-level)
4798                       (copy-list tree-stack)
4799                       (nthcdr 1 thread))
4800                 stack))
4801         (push (if (nth 1 thread) 1 0) tree-stack)
4802         (incf gnus-tmp-level)
4803         (setq threads (if thread-end nil (cdar thread)))
4804         (if gnus-summary-display-while-building
4805             (if building-count
4806                 (progn
4807                   ;; use a set frequency
4808                   (setq building-line-count (1- building-line-count))
4809                   (when (= building-line-count 0)
4810                     (sit-for 0)
4811                     (setq building-line-count
4812                           gnus-summary-display-while-building)))
4813               ;; always
4814               (sit-for 0)))
4815         (unless threads
4816           (setq gnus-tmp-level 0)))))
4817   (gnus-message 7 "Generating summary...done"))
4818
4819 (defun gnus-summary-prepare-unthreaded (headers)
4820   "Generate an unthreaded summary buffer based on HEADERS."
4821   (let (header number mark)
4822
4823     (beginning-of-line)
4824
4825     (while headers
4826       ;; We may have to root out some bad articles...
4827       (when (memq (setq number (mail-header-number
4828                                 (setq header (pop headers))))
4829                   gnus-newsgroup-limit)
4830         ;; Mark article as read when it has a low score.
4831         (when (and gnus-summary-mark-below
4832                    (< (or (cdr (assq number gnus-newsgroup-scored))
4833                           gnus-summary-default-score 0)
4834                       gnus-summary-mark-below)
4835                    (not (gnus-summary-article-ancient-p number)))
4836           (setq gnus-newsgroup-unreads
4837                 (delq number gnus-newsgroup-unreads))
4838           (if gnus-newsgroup-auto-expire
4839               (push number gnus-newsgroup-expirable)
4840             (push (cons number gnus-low-score-mark)
4841                   gnus-newsgroup-reads)))
4842
4843         (setq mark (gnus-article-mark number))
4844         (push (gnus-data-make number mark (1+ (point)) header 0)
4845               gnus-newsgroup-data)
4846         (gnus-summary-insert-line
4847          header 0 number
4848          (memq number gnus-newsgroup-undownloaded)
4849          mark (memq number gnus-newsgroup-replied)
4850          (memq number gnus-newsgroup-expirable)
4851          (mail-header-subject header) nil
4852          (cdr (assq number gnus-newsgroup-scored))
4853          (memq number gnus-newsgroup-processable))))))
4854
4855 (defun gnus-summary-remove-list-identifiers ()
4856   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4857   (let ((regexp (if (consp gnus-list-identifiers)
4858                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4859                   gnus-list-identifiers))
4860         changed subject)
4861     (when regexp
4862       (dolist (header gnus-newsgroup-headers)
4863         (setq subject (mail-header-subject header)
4864               changed nil)
4865         (while (string-match
4866                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4867                 subject)
4868           (setq subject
4869                 (concat (substring subject 0 (match-beginning 2))
4870                         (substring subject (match-end 0)))
4871                 changed t))
4872         (when (and changed
4873                    (string-match
4874                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4875           (setq subject
4876                 (concat (substring subject 0 (match-beginning 1))
4877                         (substring subject (match-end 1)))))
4878         (when changed
4879           (mail-header-set-subject header subject))))))
4880
4881 (defun gnus-fetch-headers (articles)
4882   "Fetch headers of ARTICLES."
4883   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4884     (gnus-message 5 "Fetching headers for %s..." name)
4885     (prog1
4886         (if (eq 'nov
4887                 (setq gnus-headers-retrieved-by
4888                       (gnus-retrieve-headers
4889                        articles gnus-newsgroup-name
4890                        ;; We might want to fetch old headers, but
4891                        ;; not if there is only 1 article.
4892                        (and (or (and
4893                                  (not (eq gnus-fetch-old-headers 'some))
4894                                  (not (numberp gnus-fetch-old-headers)))
4895                                 (> (length articles) 1))
4896                             gnus-fetch-old-headers))))
4897             (gnus-get-newsgroup-headers-xover
4898              articles nil nil gnus-newsgroup-name t)
4899           (gnus-get-newsgroup-headers))
4900       (gnus-message 5 "Fetching headers for %s...done" name))))
4901
4902 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4903   "Select newsgroup GROUP.
4904 If READ-ALL is non-nil, all articles in the group are selected.
4905 If SELECT-ARTICLES, only select those articles from GROUP."
4906   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4907          ;;!!! Dirty hack; should be removed.
4908          (gnus-summary-ignore-duplicates
4909           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4910               t
4911             gnus-summary-ignore-duplicates))
4912          (info (nth 2 entry))
4913          articles fetched-articles cached)
4914
4915     (unless (gnus-check-server
4916              (set (make-local-variable 'gnus-current-select-method)
4917                   (gnus-find-method-for-group group)))
4918       (error "Couldn't open server"))
4919
4920     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4921         (gnus-activate-group group)     ; Or we can activate it...
4922         (progn                          ; Or we bug out.
4923           (when (equal major-mode 'gnus-summary-mode)
4924             (kill-buffer (current-buffer)))
4925           (error "Couldn't activate group %s: %s"
4926                  group (gnus-status-message group))))
4927
4928     (unless (gnus-request-group group t)
4929       (when (equal major-mode 'gnus-summary-mode)
4930         (kill-buffer (current-buffer)))
4931       (error "Couldn't request group %s: %s"
4932              group (gnus-status-message group)))
4933
4934     (setq gnus-newsgroup-name group
4935           gnus-newsgroup-unselected nil
4936           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4937
4938     (let ((display (gnus-group-find-parameter group 'display)))
4939       (setq gnus-newsgroup-display
4940             (cond
4941              ((not (zerop (or (car-safe read-all) 0)))
4942               ;; The user entered the group with C-u SPC/RET, let's show
4943               ;; all articles.
4944               'gnus-not-ignore)
4945              ((eq display 'all)
4946               'gnus-not-ignore)
4947              ((arrayp display)
4948               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4949              ((numberp display)
4950               ;; The following is probably the "correct" solution, but
4951               ;; it makes Gnus fetch all headers and then limit the
4952               ;; articles (which is slow), so instead we hack the
4953               ;; select-articles parameter instead. -- Simon Josefsson
4954               ;; <jas@kth.se>
4955               ;;
4956               ;; (gnus-byte-compile
4957               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4958               ;;                         display)))))
4959               (setq select-articles
4960                     (gnus-uncompress-range
4961                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4962                              (if (> tmp 0)
4963                                  tmp
4964                                1))
4965                            (cdr (gnus-active group)))))
4966               nil)
4967              (t
4968               nil))))
4969
4970     (gnus-summary-setup-default-charset)
4971
4972     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4973     (when (gnus-virtual-group-p group)
4974       (setq cached gnus-newsgroup-cached))
4975
4976     (setq gnus-newsgroup-unreads
4977           (gnus-sorted-ndifference
4978            (gnus-sorted-ndifference gnus-newsgroup-unreads
4979                                     gnus-newsgroup-marked)
4980            gnus-newsgroup-dormant))
4981
4982     (setq gnus-newsgroup-processable nil)
4983
4984     (gnus-update-read-articles group gnus-newsgroup-unreads)
4985
4986     ;; Adjust and set lists of article marks.
4987     (when info
4988       (gnus-adjust-marked-articles info))
4989     (if (setq articles select-articles)
4990         (setq gnus-newsgroup-unselected
4991               (gnus-sorted-difference gnus-newsgroup-unreads articles))
4992       (setq articles (gnus-articles-to-read group read-all)))
4993
4994     (cond
4995      ((null articles)
4996       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4997       'quit)
4998      ((eq articles 0) nil)
4999      (t
5000       ;; Init the dependencies hash table.
5001       (setq gnus-newsgroup-dependencies
5002             (gnus-make-hashtable (length articles)))
5003       (gnus-set-global-variables)
5004       ;; Retrieve the headers and read them in.
5005
5006       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5007
5008       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5009       (when cached
5010         (setq gnus-newsgroup-cached cached))
5011
5012       ;; Suppress duplicates?
5013       (when gnus-suppress-duplicates
5014         (gnus-dup-suppress-articles))
5015
5016       ;; Set the initial limit.
5017       (setq gnus-newsgroup-limit (copy-sequence articles))
5018       ;; Remove canceled articles from the list of unread articles.
5019       (setq fetched-articles
5020             (mapcar (lambda (headers) (mail-header-number headers))
5021                     gnus-newsgroup-headers))
5022       (setq gnus-newsgroup-articles fetched-articles)
5023       (setq gnus-newsgroup-unreads
5024             (gnus-sorted-nintersection
5025              gnus-newsgroup-unreads fetched-articles))
5026       (gnus-compute-unseen-list)
5027
5028       ;; Removed marked articles that do not exist.
5029       (gnus-update-missing-marks
5030        (gnus-sorted-difference articles fetched-articles))
5031       ;; We might want to build some more threads first.
5032       (when (and gnus-fetch-old-headers
5033                  (eq gnus-headers-retrieved-by 'nov))
5034         (if (eq gnus-fetch-old-headers 'invisible)
5035             (gnus-build-all-threads)
5036           (gnus-build-old-threads)))
5037       ;; Let the Gnus agent mark articles as read.
5038       (when gnus-agent
5039         (gnus-agent-get-undownloaded-list))
5040       ;; Remove list identifiers from subject
5041       (when gnus-list-identifiers
5042         (gnus-summary-remove-list-identifiers))
5043       ;; Check whether auto-expire is to be done in this group.
5044       (setq gnus-newsgroup-auto-expire
5045             (gnus-group-auto-expirable-p group))
5046       ;; Set up the article buffer now, if necessary.
5047       (unless gnus-single-article-buffer
5048         (gnus-article-setup-buffer))
5049       ;; First and last article in this newsgroup.
5050       (when gnus-newsgroup-headers
5051         (setq gnus-newsgroup-begin
5052               (mail-header-number (car gnus-newsgroup-headers))
5053               gnus-newsgroup-end
5054               (mail-header-number
5055                (gnus-last-element gnus-newsgroup-headers))))
5056       ;; GROUP is successfully selected.
5057       (or gnus-newsgroup-headers t)))))
5058
5059 (defun gnus-compute-unseen-list ()
5060   ;; The `seen' marks are treated specially.
5061   (if (not gnus-newsgroup-seen)
5062       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5063     (setq gnus-newsgroup-unseen
5064           (gnus-inverse-list-range-intersection
5065            gnus-newsgroup-articles gnus-newsgroup-seen))))
5066
5067 (defun gnus-summary-display-make-predicate (display)
5068   (require 'gnus-agent)
5069   (when (= (length display) 1)
5070     (setq display (car display)))
5071   (unless gnus-summary-display-cache
5072     (dolist (elem (append '((unread . unread)
5073                             (read . read)
5074                             (unseen . unseen))
5075                           gnus-article-mark-lists))
5076       (push (cons (cdr elem)
5077                   (gnus-byte-compile
5078                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5079             gnus-summary-display-cache)))
5080   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5081         (gnus-category-predicate-cache gnus-summary-display-cache))
5082     (gnus-get-predicate display)))
5083
5084 ;; Uses the dynamically bound `number' variable.
5085 (eval-when-compile
5086   (defvar number))
5087 (defun gnus-article-marked-p (type &optional article)
5088   (let ((article (or article number)))
5089     (cond
5090      ((eq type 'tick)
5091       (memq article gnus-newsgroup-marked))
5092      ((eq type 'spam)
5093       (memq article gnus-newsgroup-spam-marked))
5094      ((eq type 'unsend)
5095       (memq article gnus-newsgroup-unsendable))
5096      ((eq type 'undownload)
5097       (memq article gnus-newsgroup-undownloaded))
5098      ((eq type 'download)
5099       (memq article gnus-newsgroup-downloadable))
5100      ((eq type 'unread)
5101       (memq article gnus-newsgroup-unreads))
5102      ((eq type 'read)
5103       (memq article gnus-newsgroup-reads))
5104      ((eq type 'dormant)
5105       (memq article gnus-newsgroup-dormant) )
5106      ((eq type 'expire)
5107       (memq article gnus-newsgroup-expirable))
5108      ((eq type 'reply)
5109       (memq article gnus-newsgroup-replied))
5110      ((eq type 'killed)
5111       (memq article gnus-newsgroup-killed))
5112      ((eq type 'bookmark)
5113       (assq article gnus-newsgroup-bookmarks))
5114      ((eq type 'score)
5115       (assq article gnus-newsgroup-scored))
5116      ((eq type 'save)
5117       (memq article gnus-newsgroup-saved))
5118      ((eq type 'cache)
5119       (memq article gnus-newsgroup-cached))
5120      ((eq type 'forward)
5121       (memq article gnus-newsgroup-forwarded))
5122      ((eq type 'seen)
5123       (not (memq article gnus-newsgroup-unseen)))
5124      ((eq type 'recent)
5125       (memq article gnus-newsgroup-recent))
5126      (t t))))
5127
5128 (defun gnus-articles-to-read (group &optional read-all)
5129   "Find out what articles the user wants to read."
5130   (let* ((display (gnus-group-find-parameter group 'display))
5131          (articles
5132           ;; Select all articles if `read-all' is non-nil, or if there
5133           ;; are no unread articles.
5134           (if (or read-all
5135                   (and (zerop (length gnus-newsgroup-marked))
5136                        (zerop (length gnus-newsgroup-unreads)))
5137                   ;; Fetch all if the predicate is non-nil.
5138                   gnus-newsgroup-display)
5139               ;; We want to select the headers for all the articles in
5140               ;; the group, so we select either all the active
5141               ;; articles in the group, or (if that's nil), the
5142               ;; articles in the cache.
5143               (or
5144                (gnus-uncompress-range (gnus-active group))
5145                (gnus-cache-articles-in-group group))
5146             ;; Select only the "normal" subset of articles.
5147             (gnus-sorted-nunion
5148              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5149              gnus-newsgroup-unreads)))
5150          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5151          (scored (length scored-list))
5152          (number (length articles))
5153          (marked (+ (length gnus-newsgroup-marked)
5154                     (length gnus-newsgroup-dormant)))
5155          (select
5156           (cond
5157            ((numberp read-all)
5158             read-all)
5159            ((numberp gnus-newsgroup-display)
5160             gnus-newsgroup-display)
5161            (t
5162             (condition-case ()
5163                 (cond
5164                  ((and (or (<= scored marked) (= scored number))
5165                        (numberp gnus-large-newsgroup)
5166                        (> number gnus-large-newsgroup))
5167                   (let* ((cursor-in-echo-area nil)
5168                          (initial (gnus-parameter-large-newsgroup-initial
5169                                    gnus-newsgroup-name))
5170                          (input
5171                           (read-string
5172                            (format
5173                             "How many articles from %s (%s %d): "
5174                             (gnus-limit-string
5175                              (gnus-group-decoded-name gnus-newsgroup-name)
5176                              35)
5177                             (if initial "max" "default")
5178                             number)
5179                            (if initial
5180                                (cons (number-to-string initial)
5181                                      0)))))
5182                     (if (string-match "^[ \t]*$" input) number input)))
5183                  ((and (> scored marked) (< scored number)
5184                        (> (- scored number) 20))
5185                   (let ((input
5186                          (read-string
5187                           (format "%s %s (%d scored, %d total): "
5188                                   "How many articles from"
5189                                   (gnus-group-decoded-name group)
5190                                   scored number))))
5191                     (if (string-match "^[ \t]*$" input)
5192                         number input)))
5193                  (t number))
5194               (quit
5195                (message "Quit getting the articles to read")
5196                nil))))))
5197     (setq select (if (stringp select) (string-to-number select) select))
5198     (if (or (null select) (zerop select))
5199         select
5200       (if (and (not (zerop scored)) (<= (abs select) scored))
5201           (progn
5202             (setq articles (sort scored-list '<))
5203             (setq number (length articles)))
5204         (setq articles (copy-sequence articles)))
5205
5206       (when (< (abs select) number)
5207         (if (< select 0)
5208             ;; Select the N oldest articles.
5209             (setcdr (nthcdr (1- (abs select)) articles) nil)
5210           ;; Select the N most recent articles.
5211           (setq articles (nthcdr (- number select) articles))))
5212       (setq gnus-newsgroup-unselected
5213             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5214       (when gnus-alter-articles-to-read-function
5215         (setq articles
5216               (sort
5217                (funcall gnus-alter-articles-to-read-function
5218                         gnus-newsgroup-name articles)
5219                '<)))
5220       articles)))
5221
5222 (defun gnus-killed-articles (killed articles)
5223   (let (out)
5224     (while articles
5225       (when (inline (gnus-member-of-range (car articles) killed))
5226         (push (car articles) out))
5227       (setq articles (cdr articles)))
5228     out))
5229
5230 (defun gnus-uncompress-marks (marks)
5231   "Uncompress the mark ranges in MARKS."
5232   (let ((uncompressed '(score bookmark))
5233         out)
5234     (while marks
5235       (if (memq (caar marks) uncompressed)
5236           (push (car marks) out)
5237         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5238       (setq marks (cdr marks)))
5239     out))
5240
5241 (defun gnus-article-mark-to-type (mark)
5242   "Return the type of MARK."
5243   (or (cadr (assq mark gnus-article-special-mark-lists))
5244       'list))
5245
5246 (defun gnus-article-unpropagatable-p (mark)
5247   "Return whether MARK should be propagated to backend."
5248   (memq mark gnus-article-unpropagated-mark-lists))
5249
5250 (defun gnus-adjust-marked-articles (info)
5251   "Set all article lists and remove all marks that are no longer valid."
5252   (let* ((marked-lists (gnus-info-marks info))
5253          (active (gnus-active (gnus-info-group info)))
5254          (min (car active))
5255          (max (cdr active))
5256          (types gnus-article-mark-lists)
5257          marks var articles article mark mark-type)
5258
5259     (dolist (marks marked-lists)
5260       (setq mark (car marks)
5261             mark-type (gnus-article-mark-to-type mark)
5262             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5263
5264       ;; We set the variable according to the type of the marks list,
5265       ;; and then adjust the marks to a subset of the active articles.
5266       (cond
5267        ;; Adjust "simple" lists.
5268        ((eq mark-type 'list)
5269         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5270         (when (memq mark '(tick dormant expire reply save))
5271           (while articles
5272             (when (or (< (setq article (pop articles)) min) (> article max))
5273               (set var (delq article (symbol-value var)))))))
5274        ;; Adjust assocs.
5275        ((eq mark-type 'tuple)
5276         (set var (setq articles (cdr marks)))
5277         (when (not (listp (cdr (symbol-value var))))
5278           (set var (list (symbol-value var))))
5279         (when (not (listp (cdr articles)))
5280           (setq articles (list articles)))
5281         (while articles
5282           (when (or (not (consp (setq article (pop articles))))
5283                     (< (car article) min)
5284                     (> (car article) max))
5285             (set var (delq article (symbol-value var))))))
5286        ;; Adjust ranges (sloppily).
5287        ((eq mark-type 'range)
5288         (cond
5289          ((eq mark 'seen)
5290           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5291           ;; It should be (seen (NUM1 . NUM2)).
5292           (when (numberp (cddr marks))
5293             (setcdr marks (list (cdr marks))))
5294           (setq articles (cdr marks))
5295           (while (and articles
5296                       (or (and (consp (car articles))
5297                                (> min (cdar articles)))
5298                           (and (numberp (car articles))
5299                                (> min (car articles)))))
5300             (pop articles))
5301           (set var articles))))))))
5302
5303 (defun gnus-update-missing-marks (missing)
5304   "Go through the list of MISSING articles and remove them from the mark lists."
5305   (when missing
5306     (let (var m)
5307       ;; Go through all types.
5308       (dolist (elem gnus-article-mark-lists)
5309         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5310           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5311           (when (symbol-value var)
5312             ;; This list has articles.  So we delete all missing
5313             ;; articles from it.
5314             (setq m missing)
5315             (while m
5316               (set var (delq (pop m) (symbol-value var))))))))))
5317
5318 (defun gnus-update-marks ()
5319   "Enter the various lists of marked articles into the newsgroup info list."
5320   (let ((types gnus-article-mark-lists)
5321         (info (gnus-get-info gnus-newsgroup-name))
5322         type list newmarked symbol delta-marks)
5323     (when info
5324       ;; Add all marks lists to the list of marks lists.
5325       (while (setq type (pop types))
5326         (setq list (symbol-value
5327                     (setq symbol
5328                           (intern (format "gnus-newsgroup-%s" (car type))))))
5329
5330         (when list
5331           ;; Get rid of the entries of the articles that have the
5332           ;; default score.
5333           (when (and (eq (cdr type) 'score)
5334                      gnus-save-score
5335                      list)
5336             (let* ((arts list)
5337                    (prev (cons nil list))
5338                    (all prev))
5339               (while arts
5340                 (if (or (not (consp (car arts)))
5341                         (= (cdar arts) gnus-summary-default-score))
5342                     (setcdr prev (cdr arts))
5343                   (setq prev arts))
5344                 (setq arts (cdr arts)))
5345               (setq list (cdr all)))))
5346
5347         (when (eq (cdr type) 'seen)
5348           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5349
5350         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5351           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5352
5353         (when (and (gnus-check-backend-function
5354                     'request-set-mark gnus-newsgroup-name)
5355                    (not (gnus-article-unpropagatable-p (cdr type))))
5356           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5357                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5358                  (add (gnus-remove-from-range
5359                        (gnus-copy-sequence list) old)))
5360             (when add
5361               (push (list add 'add (list (cdr type))) delta-marks))
5362             (when del
5363               (push (list del 'del (list (cdr type))) delta-marks))))
5364
5365         (when list
5366           (push (cons (cdr type) list) newmarked)))
5367
5368       (when delta-marks
5369         (unless (gnus-check-group gnus-newsgroup-name)
5370           (error "Can't open server for %s" gnus-newsgroup-name))
5371         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5372
5373       ;; Enter these new marks into the info of the group.
5374       (if (nthcdr 3 info)
5375           (setcar (nthcdr 3 info) newmarked)
5376         ;; Add the marks lists to the end of the info.
5377         (when newmarked
5378           (setcdr (nthcdr 2 info) (list newmarked))))
5379
5380       ;; Cut off the end of the info if there's nothing else there.
5381       (let ((i 5))
5382         (while (and (> i 2)
5383                     (not (nth i info)))
5384           (when (nthcdr (decf i) info)
5385             (setcdr (nthcdr i info) nil)))))))
5386
5387 (defun gnus-set-mode-line (where)
5388   "Set the mode line of the article or summary buffers.
5389 If WHERE is `summary', the summary mode line format will be used."
5390   ;; Is this mode line one we keep updated?
5391   (when (and (memq where gnus-updated-mode-lines)
5392              (symbol-value
5393               (intern (format "gnus-%s-mode-line-format-spec" where))))
5394     (let (mode-string)
5395       (save-excursion
5396         ;; We evaluate this in the summary buffer since these
5397         ;; variables are buffer-local to that buffer.
5398         (set-buffer gnus-summary-buffer)
5399        ;; We bind all these variables that are used in the `eval' form
5400         ;; below.
5401         (let* ((mformat (symbol-value
5402                          (intern
5403                           (format "gnus-%s-mode-line-format-spec" where))))
5404                (gnus-tmp-group-name (gnus-group-decoded-name
5405                                      gnus-newsgroup-name))
5406                (gnus-tmp-article-number (or gnus-current-article 0))
5407                (gnus-tmp-unread gnus-newsgroup-unreads)
5408                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5409                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5410                (gnus-tmp-unread-and-unselected
5411                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5412                             (zerop gnus-tmp-unselected))
5413                        "")
5414                       ((zerop gnus-tmp-unselected)
5415                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5416                       (t (format "{%d(+%d) more}"
5417                                  gnus-tmp-unread-and-unticked
5418                                  gnus-tmp-unselected))))
5419                (gnus-tmp-subject
5420                 (if (and gnus-current-headers
5421                          (vectorp gnus-current-headers))
5422                     (gnus-mode-string-quote
5423                      (mail-header-subject gnus-current-headers))
5424                   ""))
5425                bufname-length max-len
5426                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5427           (setq mode-string (eval mformat))
5428           (setq bufname-length (if (string-match "%b" mode-string)
5429                                    (- (length
5430                                        (buffer-name
5431                                         (if (eq where 'summary)
5432                                             nil
5433                                           (get-buffer gnus-article-buffer))))
5434                                       2)
5435                                  0))
5436           (setq max-len (max 4 (if gnus-mode-non-string-length
5437                                    (- (window-width)
5438                                       gnus-mode-non-string-length
5439                                       bufname-length)
5440                                  (length mode-string))))
5441           ;; We might have to chop a bit of the string off...
5442           (when (> (length mode-string) max-len)
5443             (setq mode-string
5444                   (concat (truncate-string-to-width mode-string (- max-len 3))
5445                           "...")))
5446           ;; Pad the mode string a bit.
5447           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5448       ;; Update the mode line.
5449       (setq mode-line-buffer-identification
5450             (gnus-mode-line-buffer-identification (list mode-string)))
5451       (set-buffer-modified-p t))))
5452
5453 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5454   "Go through the HEADERS list and add all Xrefs to a hash table.
5455 The resulting hash table is returned, or nil if no Xrefs were found."
5456   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5457          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5458          (xref-hashtb (gnus-make-hashtable))
5459          start group entry number xrefs header)
5460     (while headers
5461       (setq header (pop headers))
5462       (when (and (setq xrefs (mail-header-xref header))
5463                  (not (memq (setq number (mail-header-number header))
5464                             unreads)))
5465         (setq start 0)
5466         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5467           (setq start (match-end 0))
5468           (setq group (if prefix
5469                           (concat prefix (substring xrefs (match-beginning 1)
5470                                                     (match-end 1)))
5471                         (substring xrefs (match-beginning 1) (match-end 1))))
5472           (setq number
5473                 (string-to-int (substring xrefs (match-beginning 2)
5474                                           (match-end 2))))
5475           (if (setq entry (gnus-gethash group xref-hashtb))
5476               (setcdr entry (cons number (cdr entry)))
5477             (gnus-sethash group (cons number nil) xref-hashtb)))))
5478     (and start xref-hashtb)))
5479
5480 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5481   "Look through all the headers and mark the Xrefs as read."
5482   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5483         name entry info xref-hashtb idlist method nth4)
5484     (save-excursion
5485       (set-buffer gnus-group-buffer)
5486       (when (setq xref-hashtb
5487                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5488         (mapatoms
5489          (lambda (group)
5490            (unless (string= from-newsgroup (setq name (symbol-name group)))
5491              (setq idlist (symbol-value group))
5492              ;; Dead groups are not updated.
5493              (and (prog1
5494                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5495                             info (nth 2 entry))
5496                     (when (stringp (setq nth4 (gnus-info-method info)))
5497                       (setq nth4 (gnus-server-to-method nth4))))
5498                   ;; Only do the xrefs if the group has the same
5499                   ;; select method as the group we have just read.
5500                   (or (gnus-methods-equal-p
5501                        nth4 (gnus-find-method-for-group from-newsgroup))
5502                       virtual
5503                       (equal nth4 (setq method (gnus-find-method-for-group
5504                                                 from-newsgroup)))
5505                       (and (equal (car nth4) (car method))
5506                            (equal (nth 1 nth4) (nth 1 method))))
5507                   gnus-use-cross-reference
5508                   (or (not (eq gnus-use-cross-reference t))
5509                       virtual
5510                       ;; Only do cross-references on subscribed
5511                       ;; groups, if that is what is wanted.
5512                       (<= (gnus-info-level info) gnus-level-subscribed))
5513                   (gnus-group-make-articles-read name idlist))))
5514          xref-hashtb)))))
5515
5516 (defun gnus-compute-read-articles (group articles)
5517   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5518          (info (nth 2 entry))
5519          (active (gnus-active group))
5520          ninfo)
5521     (when entry
5522       ;; First peel off all invalid article numbers.
5523       (when active
5524         (let ((ids articles)
5525               id first)
5526           (while (setq id (pop ids))
5527             (when (and first (> id (cdr active)))
5528               ;; We'll end up in this situation in one particular
5529               ;; obscure situation.  If you re-scan a group and get
5530               ;; a new article that is cross-posted to a different
5531               ;; group that has not been re-scanned, you might get
5532               ;; crossposted article that has a higher number than
5533               ;; Gnus believes possible.  So we re-activate this
5534               ;; group as well.  This might mean doing the
5535               ;; crossposting thingy will *increase* the number
5536               ;; of articles in some groups.  Tsk, tsk.
5537               (setq active (or (gnus-activate-group group) active)))
5538             (when (or (> id (cdr active))
5539                       (< id (car active)))
5540               (setq articles (delq id articles))))))
5541       ;; If the read list is nil, we init it.
5542       (if (and active
5543                (null (gnus-info-read info))
5544                (> (car active) 1))
5545           (setq ninfo (cons 1 (1- (car active))))
5546         (setq ninfo (gnus-info-read info)))
5547       ;; Then we add the read articles to the range.
5548       (gnus-add-to-range
5549        ninfo (setq articles (sort articles '<))))))
5550
5551 (defun gnus-group-make-articles-read (group articles)
5552   "Update the info of GROUP to say that ARTICLES are read."
5553   (let* ((num 0)
5554          (entry (gnus-gethash group gnus-newsrc-hashtb))
5555          (info (nth 2 entry))
5556          (active (gnus-active group))
5557          range)
5558     (when entry
5559       (setq range (gnus-compute-read-articles group articles))
5560       (save-excursion
5561         (set-buffer gnus-group-buffer)
5562         (gnus-undo-register
5563           `(progn
5564              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5565              (gnus-info-set-read ',info ',(gnus-info-read info))
5566              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5567              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5568              (gnus-group-update-group ,group t))))
5569       ;; Add the read articles to the range.
5570       (gnus-info-set-read info range)
5571       (gnus-request-set-mark group (list (list range 'add '(read))))
5572       ;; Then we have to re-compute how many unread
5573       ;; articles there are in this group.
5574       (when active
5575         (cond
5576          ((not range)
5577           (setq num (- (1+ (cdr active)) (car active))))
5578          ((not (listp (cdr range)))
5579           (setq num (- (cdr active) (- (1+ (cdr range))
5580                                        (car range)))))
5581          (t
5582           (while range
5583             (if (numberp (car range))
5584                 (setq num (1+ num))
5585               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5586             (setq range (cdr range)))
5587           (setq num (- (cdr active) num))))
5588         ;; Update the number of unread articles.
5589         (setcar entry num)
5590         ;; Update the group buffer.
5591         (unless (gnus-ephemeral-group-p group)
5592           (gnus-group-update-group group t))))))
5593
5594 (defvar gnus-newsgroup-none-id 0)
5595
5596 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5597   (let ((cur nntp-server-buffer)
5598         (dependencies
5599          (or dependencies
5600              (save-excursion (set-buffer gnus-summary-buffer)
5601                              gnus-newsgroup-dependencies)))
5602         headers id end ref
5603         (mail-parse-charset gnus-newsgroup-charset)
5604         (mail-parse-ignored-charsets
5605          (save-excursion (condition-case nil
5606                              (set-buffer gnus-summary-buffer)
5607                            (error))
5608                          gnus-newsgroup-ignored-charsets)))
5609     (save-excursion
5610       (set-buffer nntp-server-buffer)
5611       ;; Translate all TAB characters into SPACE characters.
5612       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5613       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5614       (gnus-run-hooks 'gnus-parse-headers-hook)
5615       (let ((case-fold-search t)
5616             in-reply-to header p lines chars)
5617         (goto-char (point-min))
5618         ;; Search to the beginning of the next header.  Error messages
5619         ;; do not begin with 2 or 3.
5620         (while (re-search-forward "^[23][0-9]+ " nil t)
5621           (setq id nil
5622                 ref nil)
5623           ;; This implementation of this function, with nine
5624           ;; search-forwards instead of the one re-search-forward and
5625           ;; a case (which basically was the old function) is actually
5626           ;; about twice as fast, even though it looks messier.  You
5627           ;; can't have everything, I guess.  Speed and elegance
5628           ;; doesn't always go hand in hand.
5629           (setq
5630            header
5631            (vector
5632             ;; Number.
5633             (prog1
5634                 (read cur)
5635               (end-of-line)
5636               (setq p (point))
5637               (narrow-to-region (point)
5638                                 (or (and (search-forward "\n.\n" nil t)
5639                                          (- (point) 2))
5640                                     (point))))
5641             ;; Subject.
5642             (progn
5643               (goto-char p)
5644               (if (search-forward "\nsubject:" nil t)
5645                   (funcall gnus-decode-encoded-word-function
5646                            (nnheader-header-value))
5647                 "(none)"))
5648             ;; From.
5649             (progn
5650               (goto-char p)
5651               (if (search-forward "\nfrom:" nil t)
5652                   (funcall gnus-decode-encoded-word-function
5653                            (nnheader-header-value))
5654                 "(nobody)"))
5655             ;; Date.
5656             (progn
5657               (goto-char p)
5658               (if (search-forward "\ndate:" nil t)
5659                   (nnheader-header-value) ""))
5660             ;; Message-ID.
5661             (progn
5662               (goto-char p)
5663               (setq id (if (re-search-forward
5664                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5665                            ;; We do it this way to make sure the Message-ID
5666                            ;; is (somewhat) syntactically valid.
5667                            (buffer-substring (match-beginning 1)
5668                                              (match-end 1))
5669                          ;; If there was no message-id, we just fake one
5670                          ;; to make subsequent routines simpler.
5671                          (nnheader-generate-fake-message-id))))
5672             ;; References.
5673             (progn
5674               (goto-char p)
5675               (if (search-forward "\nreferences:" nil t)
5676                   (progn
5677                     (setq end (point))
5678                     (prog1
5679                         (nnheader-header-value)
5680                       (setq ref
5681                             (buffer-substring
5682                              (progn
5683                                (end-of-line)
5684                                (search-backward ">" end t)
5685                                (1+ (point)))
5686                              (progn
5687                                (search-backward "<" end t)
5688                                (point))))))
5689                 ;; Get the references from the in-reply-to header if there
5690                 ;; were no references and the in-reply-to header looks
5691                 ;; promising.
5692                 (if (and (search-forward "\nin-reply-to:" nil t)
5693                          (setq in-reply-to (nnheader-header-value))
5694                          (string-match "<[^>]+>" in-reply-to))
5695                     (let (ref2)
5696                       (setq ref (substring in-reply-to (match-beginning 0)
5697                                            (match-end 0)))
5698                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5699                         (setq ref2 (substring in-reply-to (match-beginning 0)
5700                                               (match-end 0)))
5701                         (when (> (length ref2) (length ref))
5702                           (setq ref ref2)))
5703                       ref)
5704                   (setq ref nil))))
5705             ;; Chars.
5706             (progn
5707               (goto-char p)
5708               (if (search-forward "\nchars: " nil t)
5709                   (if (numberp (setq chars (ignore-errors (read cur))))
5710                       chars -1)
5711                 -1))
5712             ;; Lines.
5713             (progn
5714               (goto-char p)
5715               (if (search-forward "\nlines: " nil t)
5716                   (if (numberp (setq lines (ignore-errors (read cur))))
5717                       lines -1)
5718                 -1))
5719             ;; Xref.
5720             (progn
5721               (goto-char p)
5722               (and (search-forward "\nxref:" nil t)
5723                    (nnheader-header-value)))
5724             ;; Extra.
5725             (when gnus-extra-headers
5726               (let ((extra gnus-extra-headers)
5727                     out)
5728                 (while extra
5729                   (goto-char p)
5730                   (when (search-forward
5731                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5732                     (push (cons (car extra) (nnheader-header-value))
5733                           out))
5734                   (pop extra))
5735                 out))))
5736           (when (equal id ref)
5737             (setq ref nil))
5738
5739           (when gnus-alter-header-function
5740             (funcall gnus-alter-header-function header)
5741             (setq id (mail-header-id header)
5742                   ref (gnus-parent-id (mail-header-references header))))
5743
5744           (when (setq header
5745                       (gnus-dependencies-add-header
5746                        header dependencies force-new))
5747             (push header headers))
5748           (goto-char (point-max))
5749           (widen))
5750         (nreverse headers)))))
5751
5752 ;; Goes through the xover lines and returns a list of vectors
5753 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5754                                                   force-new dependencies
5755                                                   group also-fetch-heads)
5756   "Parse the news overview data in the server buffer.
5757 Return a list of headers that match SEQUENCE (see
5758 `nntp-retrieve-headers')."
5759   ;; Get the Xref when the users reads the articles since most/some
5760   ;; NNTP servers do not include Xrefs when using XOVER.
5761   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5762   (let ((mail-parse-charset gnus-newsgroup-charset)
5763         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5764         (cur nntp-server-buffer)
5765         (dependencies (or dependencies gnus-newsgroup-dependencies))
5766         (allp (cond
5767                ((eq gnus-read-all-available-headers t)
5768                 t)
5769                ((stringp gnus-read-all-available-headers)
5770                 (string-match gnus-read-all-available-headers group))
5771                (t
5772                 nil)))
5773         number headers header)
5774     (save-excursion
5775       (set-buffer nntp-server-buffer)
5776       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5777       ;; Allow the user to mangle the headers before parsing them.
5778       (gnus-run-hooks 'gnus-parse-headers-hook)
5779       (goto-char (point-min))
5780       (gnus-parse-without-error
5781         (while (and (or sequence allp)
5782                     (not (eobp)))
5783           (setq number (read cur))
5784           (when (not allp)
5785             (while (and sequence
5786                         (< (car sequence) number))
5787               (setq sequence (cdr sequence))))
5788           (when (and (or allp
5789                          (and sequence
5790                               (eq number (car sequence))))
5791                      (progn
5792                        (setq sequence (cdr sequence))
5793                        (setq header (inline
5794                                       (gnus-nov-parse-line
5795                                        number dependencies force-new)))))
5796             (push header headers))
5797           (forward-line 1)))
5798       ;; A common bug in inn is that if you have posted an article and
5799       ;; then retrieves the active file, it will answer correctly --
5800       ;; the new article is included.  However, a NOV entry for the
5801       ;; article may not have been generated yet, so this may fail.
5802       ;; We work around this problem by retrieving the last few
5803       ;; headers using HEAD.
5804       (if (or (not also-fetch-heads)
5805               (not sequence))
5806           ;; We (probably) got all the headers.
5807           (nreverse headers)
5808         (let ((gnus-nov-is-evil t))
5809           (nconc
5810            (nreverse headers)
5811            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5812              (gnus-get-newsgroup-headers))))))))
5813
5814 (defun gnus-article-get-xrefs ()
5815   "Fill in the Xref value in `gnus-current-headers', if necessary.
5816 This is meant to be called in `gnus-article-internal-prepare-hook'."
5817   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5818                                  gnus-current-headers)))
5819     (or (not gnus-use-cross-reference)
5820         (not headers)
5821         (and (mail-header-xref headers)
5822              (not (string= (mail-header-xref headers) "")))
5823         (let ((case-fold-search t)
5824               xref)
5825           (save-restriction
5826             (nnheader-narrow-to-headers)
5827             (goto-char (point-min))
5828             (when (or (and (not (eobp))
5829                            (eq (downcase (char-after)) ?x)
5830                            (looking-at "Xref:"))
5831                       (search-forward "\nXref:" nil t))
5832               (goto-char (1+ (match-end 0)))
5833               (setq xref (buffer-substring (point)
5834                                            (progn (end-of-line) (point))))
5835               (mail-header-set-xref headers xref)))))))
5836
5837 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5838   "Find article ID and insert the summary line for that article.
5839 OLD-HEADER can either be a header or a line number to insert
5840 the subject line on."
5841   (let* ((line (and (numberp old-header) old-header))
5842          (old-header (and (vectorp old-header) old-header))
5843          (header (cond ((and old-header use-old-header)
5844                         old-header)
5845                        ((and (numberp id)
5846                              (gnus-number-to-header id))
5847                         (gnus-number-to-header id))
5848                        (t
5849                         (gnus-read-header id))))
5850          (number (and (numberp id) id))
5851          d)
5852     (when header
5853       ;; Rebuild the thread that this article is part of and go to the
5854       ;; article we have fetched.
5855       (when (and (not gnus-show-threads)
5856                  old-header)
5857         (when (and number
5858                    (setq d (gnus-data-find (mail-header-number old-header))))
5859           (goto-char (gnus-data-pos d))
5860           (gnus-data-remove
5861            number
5862            (- (gnus-point-at-bol)
5863               (prog1
5864                   (1+ (gnus-point-at-eol))
5865                 (gnus-delete-line))))))
5866       (when old-header
5867         (mail-header-set-number header (mail-header-number old-header)))
5868       (setq gnus-newsgroup-sparse
5869             (delq (setq number (mail-header-number header))
5870                   gnus-newsgroup-sparse))
5871       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5872       (push number gnus-newsgroup-limit)
5873       (gnus-rebuild-thread (mail-header-id header) line)
5874       (gnus-summary-goto-subject number nil t))
5875     (when (and (numberp number)
5876                (> number 0))
5877       ;; We have to update the boundaries even if we can't fetch the
5878       ;; article if ID is a number -- so that the next `P' or `N'
5879       ;; command will fetch the previous (or next) article even
5880       ;; if the one we tried to fetch this time has been canceled.
5881       (when (> number gnus-newsgroup-end)
5882         (setq gnus-newsgroup-end number))
5883       (when (< number gnus-newsgroup-begin)
5884         (setq gnus-newsgroup-begin number))
5885       (setq gnus-newsgroup-unselected
5886             (delq number gnus-newsgroup-unselected)))
5887     ;; Report back a success?
5888     (and header (mail-header-number header))))
5889
5890 ;;; Process/prefix in the summary buffer
5891
5892 (defun gnus-summary-work-articles (n)
5893   "Return a list of articles to be worked upon.
5894 The prefix argument, the list of process marked articles, and the
5895 current article will be taken into consideration."
5896   (save-excursion
5897     (set-buffer gnus-summary-buffer)
5898     (cond
5899      (n
5900       ;; A numerical prefix has been given.
5901       (setq n (prefix-numeric-value n))
5902       (let ((backward (< n 0))
5903             (n (abs (prefix-numeric-value n)))
5904             articles article)
5905         (save-excursion
5906           (while
5907               (and (> n 0)
5908                    (push (setq article (gnus-summary-article-number))
5909                          articles)
5910                    (if backward
5911                        (gnus-summary-find-prev nil article)
5912                      (gnus-summary-find-next nil article)))
5913             (decf n)))
5914         (nreverse articles)))
5915      ((and (gnus-region-active-p) (mark))
5916       (message "region active")
5917       ;; Work on the region between point and mark.
5918       (let ((max (max (point) (mark)))
5919             articles article)
5920         (save-excursion
5921           (goto-char (min (min (point) (mark))))
5922           (while
5923               (and
5924                (push (setq article (gnus-summary-article-number)) articles)
5925                (gnus-summary-find-next nil article)
5926                (< (point) max)))
5927           (nreverse articles))))
5928      (gnus-newsgroup-processable
5929       ;; There are process-marked articles present.
5930       ;; Save current state.
5931       (gnus-summary-save-process-mark)
5932       ;; Return the list.
5933       (reverse gnus-newsgroup-processable))
5934      (t
5935       ;; Just return the current article.
5936       (list (gnus-summary-article-number))))))
5937
5938 (defmacro gnus-summary-iterate (arg &rest forms)
5939   "Iterate over the process/prefixed articles and do FORMS.
5940 ARG is the interactive prefix given to the command.  FORMS will be
5941 executed with point over the summary line of the articles."
5942   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5943     `(let ((,articles (gnus-summary-work-articles ,arg)))
5944        (while ,articles
5945          (gnus-summary-goto-subject (car ,articles))
5946          ,@forms
5947          (pop ,articles)))))
5948
5949 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5950 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5951
5952 (defun gnus-summary-save-process-mark ()
5953   "Push the current set of process marked articles on the stack."
5954   (interactive)
5955   (push (copy-sequence gnus-newsgroup-processable)
5956         gnus-newsgroup-process-stack))
5957
5958 (defun gnus-summary-kill-process-mark ()
5959   "Push the current set of process marked articles on the stack and unmark."
5960   (interactive)
5961   (gnus-summary-save-process-mark)
5962   (gnus-summary-unmark-all-processable))
5963
5964 (defun gnus-summary-yank-process-mark ()
5965   "Pop the last process mark state off the stack and restore it."
5966   (interactive)
5967   (unless gnus-newsgroup-process-stack
5968     (error "Empty mark stack"))
5969   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5970
5971 (defun gnus-summary-process-mark-set (set)
5972   "Make SET into the current process marked articles."
5973   (gnus-summary-unmark-all-processable)
5974   (while set
5975     (gnus-summary-set-process-mark (pop set))))
5976
5977 ;;; Searching and stuff
5978
5979 (defun gnus-summary-search-group (&optional backward use-level)
5980   "Search for next unread newsgroup.
5981 If optional argument BACKWARD is non-nil, search backward instead."
5982   (save-excursion
5983     (set-buffer gnus-group-buffer)
5984     (when (gnus-group-search-forward
5985            backward nil (if use-level (gnus-group-group-level) nil))
5986       (gnus-group-group-name))))
5987
5988 (defun gnus-summary-best-group (&optional exclude-group)
5989   "Find the name of the best unread group.
5990 If EXCLUDE-GROUP, do not go to this group."
5991   (save-excursion
5992     (set-buffer gnus-group-buffer)
5993     (save-excursion
5994       (gnus-group-best-unread-group exclude-group))))
5995
5996 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5997   (if backward (gnus-summary-find-prev)
5998     (let* ((dummy (gnus-summary-article-intangible-p))
5999            (article (or article (gnus-summary-article-number)))
6000            (arts (gnus-data-find-list article))
6001            result)
6002       (when (and (not dummy)
6003                  (or (not gnus-summary-check-current)
6004                      (not unread)
6005                      (not (gnus-data-unread-p (car arts)))))
6006         (setq arts (cdr arts)))
6007       (when (setq result
6008                   (if unread
6009                       (progn
6010                         (while arts
6011                           (when (or (and undownloaded
6012                                          (memq (car arts) gnus-newsgroup-undownloaded))
6013                                     (gnus-data-unread-p (car arts)))
6014                             (setq result (car arts)
6015                                   arts nil))
6016                           (setq arts (cdr arts)))
6017                         result)
6018                     (car arts)))
6019         (goto-char (gnus-data-pos result))
6020         (gnus-data-number result)))))
6021
6022 (defun gnus-summary-find-prev (&optional unread article)
6023   (let* ((eobp (eobp))
6024          (article (or article (gnus-summary-article-number)))
6025          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
6026          result)
6027     (when (and (not eobp)
6028                (or (not gnus-summary-check-current)
6029                    (not unread)
6030                    (not (gnus-data-unread-p (car arts)))))
6031       (setq arts (cdr arts)))
6032     (when (setq result
6033                 (if unread
6034                     (progn
6035                       (while arts
6036                         (when (gnus-data-unread-p (car arts))
6037                           (setq result (car arts)
6038                                 arts nil))
6039                         (setq arts (cdr arts)))
6040                       result)
6041                   (car arts)))
6042       (goto-char (gnus-data-pos result))
6043       (gnus-data-number result))))
6044
6045 (defun gnus-summary-find-subject (subject &optional unread backward article)
6046   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6047          (article (or article (gnus-summary-article-number)))
6048          (articles (gnus-data-list backward))
6049          (arts (gnus-data-find-list article articles))
6050          result)
6051     (when (or (not gnus-summary-check-current)
6052               (not unread)
6053               (not (gnus-data-unread-p (car arts))))
6054       (setq arts (cdr arts)))
6055     (while arts
6056       (and (or (not unread)
6057                (gnus-data-unread-p (car arts)))
6058            (vectorp (gnus-data-header (car arts)))
6059            (gnus-subject-equal
6060             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6061            (setq result (car arts)
6062                  arts nil))
6063       (setq arts (cdr arts)))
6064     (and result
6065          (goto-char (gnus-data-pos result))
6066          (gnus-data-number result))))
6067
6068 (defun gnus-summary-search-forward (&optional unread subject backward)
6069   "Search forward for an article.
6070 If UNREAD, look for unread articles.  If SUBJECT, look for
6071 articles with that subject.  If BACKWARD, search backward instead."
6072   (cond (subject (gnus-summary-find-subject subject unread backward))
6073         (backward (gnus-summary-find-prev unread))
6074         (t (gnus-summary-find-next unread))))
6075
6076 (defun gnus-recenter (&optional n)
6077   "Center point in window and redisplay frame.
6078 Also do horizontal recentering."
6079   (interactive "P")
6080   (when (and gnus-auto-center-summary
6081              (not (eq gnus-auto-center-summary 'vertical)))
6082     (gnus-horizontal-recenter))
6083   (recenter n))
6084
6085 (defun gnus-summary-recenter ()
6086   "Center point in the summary window.
6087 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6088 displayed, no centering will be performed."
6089   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6090 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6091   (interactive)
6092   (let* ((top (cond ((< (window-height) 4) 0)
6093                     ((< (window-height) 7) 1)
6094                     (t (if (numberp gnus-auto-center-summary)
6095                            gnus-auto-center-summary
6096                          2))))
6097          (height (1- (window-height)))
6098          (bottom (save-excursion (goto-char (point-max))
6099                                  (forward-line (- height))
6100                                  (point)))
6101          (window (get-buffer-window (current-buffer))))
6102     ;; The user has to want it.
6103     (when gnus-auto-center-summary
6104       (when (get-buffer-window gnus-article-buffer)
6105         ;; Only do recentering when the article buffer is displayed,
6106       ;; Set the window start to either `bottom', which is the biggest
6107         ;; possible valid number, or the second line from the top,
6108         ;; whichever is the least.
6109         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6110           (if (> bottom top-pos)
6111               ;; Keep the second line from the top visible
6112               (set-window-start window top-pos t)
6113             ;; Try to keep the bottom line visible; if it's partially
6114             ;; obscured, either scroll one more line to make it fully
6115             ;; visible, or revert to using TOP-POS.
6116             (save-excursion
6117               (goto-char (point-max))
6118               (forward-line -1)
6119               (let ((last-line-start (point)))
6120                 (goto-char bottom)
6121                 (set-window-start window (point) t)
6122                 (when (not (pos-visible-in-window-p last-line-start window))
6123                   (forward-line 1)
6124                   (set-window-start window (min (point) top-pos) t)))))))
6125       ;; Do horizontal recentering while we're at it.
6126       (when (and (get-buffer-window (current-buffer) t)
6127                  (not (eq gnus-auto-center-summary 'vertical)))
6128         (let ((selected (selected-window)))
6129           (select-window (get-buffer-window (current-buffer) t))
6130           (gnus-summary-position-point)
6131           (gnus-horizontal-recenter)
6132           (select-window selected))))))
6133
6134 (defun gnus-summary-jump-to-group (newsgroup)
6135   "Move point to NEWSGROUP in group mode buffer."
6136   ;; Keep update point of group mode buffer if visible.
6137   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6138       (save-window-excursion
6139         ;; Take care of tree window mode.
6140         (when (get-buffer-window gnus-group-buffer)
6141           (pop-to-buffer gnus-group-buffer))
6142         (gnus-group-jump-to-group newsgroup))
6143     (save-excursion
6144       ;; Take care of tree window mode.
6145       (if (get-buffer-window gnus-group-buffer)
6146           (pop-to-buffer gnus-group-buffer)
6147         (set-buffer gnus-group-buffer))
6148       (gnus-group-jump-to-group newsgroup))))
6149
6150 ;; This function returns a list of article numbers based on the
6151 ;; difference between the ranges of read articles in this group and
6152 ;; the range of active articles.
6153 (defun gnus-list-of-unread-articles (group)
6154   (let* ((read (gnus-info-read (gnus-get-info group)))
6155          (active (or (gnus-active group) (gnus-activate-group group)))
6156          (last (cdr active))
6157          first nlast unread)
6158     ;; If none are read, then all are unread.
6159     (if (not read)
6160         (setq first (car active))
6161       ;; If the range of read articles is a single range, then the
6162       ;; first unread article is the article after the last read
6163       ;; article.  Sounds logical, doesn't it?
6164       (if (and (not (listp (cdr read)))
6165                (or (< (car read) (car active))
6166                    (progn (setq read (list read))
6167                           nil)))
6168           (setq first (max (car active) (1+ (cdr read))))
6169         ;; `read' is a list of ranges.
6170         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6171                                   (caar read)))
6172                   1)
6173           (setq first (car active)))
6174         (while read
6175           (when first
6176             (while (< first nlast)
6177               (push first unread)
6178               (setq first (1+ first))))
6179           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6180           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6181           (setq read (cdr read)))))
6182     ;; And add the last unread articles.
6183     (while (<= first last)
6184       (push first unread)
6185       (setq first (1+ first)))
6186     ;; Return the list of unread articles.
6187     (delq 0 (nreverse unread))))
6188
6189 (defun gnus-list-of-read-articles (group)
6190   "Return a list of unread, unticked and non-dormant articles."
6191   (let* ((info (gnus-get-info group))
6192          (marked (gnus-info-marks info))
6193          (active (gnus-active group)))
6194     (and info active
6195          (gnus-list-range-difference
6196           (gnus-list-range-difference
6197            (gnus-sorted-complement
6198             (gnus-uncompress-range active)
6199             (gnus-list-of-unread-articles group))
6200            (cdr (assq 'dormant marked)))
6201           (cdr (assq 'tick marked))))))
6202
6203 ;; Various summary commands
6204
6205 (defun gnus-summary-select-article-buffer ()
6206   "Reconfigure windows to show article buffer."
6207   (interactive)
6208   (if (not (gnus-buffer-live-p gnus-article-buffer))
6209       (error "There is no article buffer for this summary buffer")
6210     (gnus-configure-windows 'article)
6211     (select-window (get-buffer-window gnus-article-buffer))))
6212
6213 (defun gnus-summary-universal-argument (arg)
6214   "Perform any operation on all articles that are process/prefixed."
6215   (interactive "P")
6216   (let ((articles (gnus-summary-work-articles arg))
6217         func article)
6218     (if (eq
6219          (setq
6220           func
6221           (key-binding
6222            (read-key-sequence
6223             (substitute-command-keys
6224              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6225          'undefined)
6226         (gnus-error 1 "Undefined key")
6227       (save-excursion
6228         (while articles
6229           (gnus-summary-goto-subject (setq article (pop articles)))
6230           (let (gnus-newsgroup-processable)
6231             (command-execute func))
6232           (gnus-summary-remove-process-mark article)))))
6233   (gnus-summary-position-point))
6234
6235 (defun gnus-summary-toggle-truncation (&optional arg)
6236   "Toggle truncation of summary lines.
6237 With arg, turn line truncation on if arg is positive."
6238   (interactive "P")
6239   (setq truncate-lines
6240         (if (null arg) (not truncate-lines)
6241           (> (prefix-numeric-value arg) 0)))
6242   (redraw-display))
6243
6244 (defun gnus-summary-find-uncancelled ()
6245   "Return the number of an uncancelled article.
6246 The current article is considered, then following articles, then previous
6247 articles.  If all articles are cancelled then return a dummy 0."
6248   (let (found)
6249     (dolist (rev '(nil t))
6250       (unless found      ; don't demand the reverse list if we don't need it
6251         (let ((data (gnus-data-find-list
6252                      (gnus-summary-article-number) (gnus-data-list rev))))
6253           (while (and data (not found))
6254             (if (not (eq gnus-canceled-mark (gnus-data-mark (car data))))
6255                 (setq found (gnus-data-number (car data))))
6256             (setq data (cdr data))))))
6257     (or found 0)))
6258
6259 (defun gnus-summary-reselect-current-group (&optional all rescan)
6260   "Exit and then reselect the current newsgroup.
6261 The prefix argument ALL means to select all articles."
6262   (interactive "P")
6263   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6264     (error "Ephemeral groups can't be reselected"))
6265   (let ((current-subject (gnus-summary-find-uncancelled))
6266         (group gnus-newsgroup-name))
6267     (setq gnus-newsgroup-begin nil)
6268     (gnus-summary-exit)
6269     ;; We have to adjust the point of group mode buffer because
6270     ;; point was moved to the next unread newsgroup by exiting.
6271     (gnus-summary-jump-to-group group)
6272     (when rescan
6273       (save-excursion
6274         (gnus-group-get-new-news-this-group 1)))
6275     (gnus-group-read-group all t)
6276     (gnus-summary-goto-subject current-subject nil t)))
6277
6278 (defun gnus-summary-rescan-group (&optional all)
6279   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6280   (interactive "P")
6281   (gnus-summary-reselect-current-group all t))
6282
6283 (defun gnus-summary-update-info (&optional non-destructive)
6284   (save-excursion
6285     (let ((group gnus-newsgroup-name))
6286       (when group
6287         (when gnus-newsgroup-kill-headers
6288           (setq gnus-newsgroup-killed
6289                 (gnus-compress-sequence
6290                  (gnus-sorted-union
6291                   (gnus-list-range-intersection
6292                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6293                   gnus-newsgroup-unreads)
6294                  t)))
6295         (unless (listp (cdr gnus-newsgroup-killed))
6296           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6297         (let ((headers gnus-newsgroup-headers))
6298           ;; Set the new ranges of read articles.
6299           (save-excursion
6300             (set-buffer gnus-group-buffer)
6301             (gnus-undo-force-boundary))
6302           (gnus-update-read-articles
6303            group (gnus-sorted-union
6304                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6305           ;; Set the current article marks.
6306           (let ((gnus-newsgroup-scored
6307                  (if (and (not gnus-save-score)
6308                           (not non-destructive))
6309                      nil
6310                    gnus-newsgroup-scored)))
6311             (save-excursion
6312               (gnus-update-marks)))
6313           ;; Do the cross-ref thing.
6314           (when gnus-use-cross-reference
6315             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6316           ;; Do not switch windows but change the buffer to work.
6317           (set-buffer gnus-group-buffer)
6318           (unless (gnus-ephemeral-group-p group)
6319             (gnus-group-update-group group)))))))
6320
6321 (defun gnus-summary-save-newsrc (&optional force)
6322   "Save the current number of read/marked articles in the dribble buffer.
6323 The dribble buffer will then be saved.
6324 If FORCE (the prefix), also save the .newsrc file(s)."
6325   (interactive "P")
6326   (gnus-summary-update-info t)
6327   (if force
6328       (gnus-save-newsrc-file)
6329     (gnus-dribble-save)))
6330
6331 (defun gnus-summary-exit (&optional temporary)
6332   "Exit reading current newsgroup, and then return to group selection mode.
6333 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6334   (interactive)
6335   (gnus-set-global-variables)
6336   (when (gnus-buffer-live-p gnus-article-buffer)
6337     (save-excursion
6338       (set-buffer gnus-article-buffer)
6339       (mm-destroy-parts gnus-article-mime-handles)
6340       ;; Set it to nil for safety reason.
6341       (setq gnus-article-mime-handle-alist nil)
6342       (setq gnus-article-mime-handles nil)))
6343   (gnus-kill-save-kill-buffer)
6344   (gnus-async-halt-prefetch)
6345   (let* ((group gnus-newsgroup-name)
6346          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6347          (gnus-group-is-exiting-p t)
6348          (mode major-mode)
6349          (group-point nil)
6350          (buf (current-buffer)))
6351     (unless quit-config
6352       ;; Do adaptive scoring, and possibly save score files.
6353       (when gnus-newsgroup-adaptive
6354         (gnus-score-adaptive))
6355       (when gnus-use-scoring
6356         (gnus-score-save)))
6357     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6358     ;; If we have several article buffers, we kill them at exit.
6359     (unless gnus-single-article-buffer
6360       (gnus-kill-buffer gnus-original-article-buffer)
6361       (setq gnus-article-current nil))
6362     (when gnus-use-cache
6363       (gnus-cache-possibly-remove-articles)
6364       (gnus-cache-save-buffers))
6365     (gnus-async-prefetch-remove-group group)
6366     (when gnus-suppress-duplicates
6367       (gnus-dup-enter-articles))
6368     (when gnus-use-trees
6369       (gnus-tree-close group))
6370     (when gnus-use-cache
6371       (gnus-cache-write-active))
6372     ;; Remove entries for this group.
6373     (nnmail-purge-split-history (gnus-group-real-name group))
6374     ;; Make all changes in this group permanent.
6375     (unless quit-config
6376       (gnus-run-hooks 'gnus-exit-group-hook)
6377       (gnus-summary-update-info))
6378     (gnus-close-group group)
6379     ;; Make sure where we were, and go to next newsgroup.
6380     (set-buffer gnus-group-buffer)
6381     (unless quit-config
6382       (gnus-group-jump-to-group group))
6383     (gnus-run-hooks 'gnus-summary-exit-hook)
6384     (unless (or quit-config
6385                 ;; If this group has disappeared from the summary
6386                 ;; buffer, don't skip forwards.
6387                 (not (string= group (gnus-group-group-name))))
6388       (gnus-group-next-unread-group 1))
6389     (setq group-point (point))
6390     (if temporary
6391         nil                             ;Nothing to do.
6392       ;; If we have several article buffers, we kill them at exit.
6393       (unless gnus-single-article-buffer
6394         (gnus-kill-buffer gnus-article-buffer)
6395         (gnus-kill-buffer gnus-original-article-buffer)
6396         (setq gnus-article-current nil))
6397       (set-buffer buf)
6398       (if (not gnus-kill-summary-on-exit)
6399           (progn
6400             (gnus-deaden-summary)
6401             (setq mode nil))
6402         ;; We set all buffer-local variables to nil.  It is unclear why
6403         ;; this is needed, but if we don't, buffer-local variables are
6404         ;; not garbage-collected, it seems.  This would the lead to en
6405         ;; ever-growing Emacs.
6406         (gnus-summary-clear-local-variables)
6407         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6408           (gnus-summary-clear-local-variables))
6409         (when (get-buffer gnus-article-buffer)
6410           (bury-buffer gnus-article-buffer))
6411         ;; We clear the global counterparts of the buffer-local
6412         ;; variables as well, just to be on the safe side.
6413         (set-buffer gnus-group-buffer)
6414         (gnus-summary-clear-local-variables)
6415         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6416           (gnus-summary-clear-local-variables))
6417         ;; Return to group mode buffer.
6418         (when (eq mode 'gnus-summary-mode)
6419           (gnus-kill-buffer buf)))
6420       (setq gnus-current-select-method gnus-select-method)
6421       (pop-to-buffer gnus-group-buffer)
6422       (if (not quit-config)
6423           (progn
6424             (goto-char group-point)
6425             (gnus-configure-windows 'group 'force))
6426         (gnus-handle-ephemeral-exit quit-config))
6427       ;; Clear the current group name.
6428       (unless quit-config
6429         (setq gnus-newsgroup-name nil)))))
6430
6431 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6432 (defun gnus-summary-exit-no-update (&optional no-questions)
6433   "Quit reading current newsgroup without updating read article info."
6434   (interactive)
6435   (let* ((group gnus-newsgroup-name)
6436          (gnus-group-is-exiting-p t)
6437          (quit-config (gnus-group-quit-config group)))
6438     (when (or no-questions
6439               gnus-expert-user
6440               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6441       (gnus-async-halt-prefetch)
6442       (mapcar 'funcall
6443               (delq 'gnus-summary-expire-articles
6444                     (copy-sequence gnus-summary-prepare-exit-hook)))
6445       (when (gnus-buffer-live-p gnus-article-buffer)
6446         (save-excursion
6447           (set-buffer gnus-article-buffer)
6448           (mm-destroy-parts gnus-article-mime-handles)
6449           ;; Set it to nil for safety reason.
6450           (setq gnus-article-mime-handle-alist nil)
6451           (setq gnus-article-mime-handles nil)))
6452       ;; If we have several article buffers, we kill them at exit.
6453       (unless gnus-single-article-buffer
6454         (gnus-kill-buffer gnus-article-buffer)
6455         (gnus-kill-buffer gnus-original-article-buffer)
6456         (setq gnus-article-current nil))
6457       (if (not gnus-kill-summary-on-exit)
6458           (gnus-deaden-summary)
6459         (gnus-close-group group)
6460         (gnus-summary-clear-local-variables)
6461         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6462           (gnus-summary-clear-local-variables))
6463         (set-buffer gnus-group-buffer)
6464         (gnus-summary-clear-local-variables)
6465         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6466           (gnus-summary-clear-local-variables))
6467         (when (get-buffer gnus-summary-buffer)
6468           (kill-buffer gnus-summary-buffer)))
6469       (unless gnus-single-article-buffer
6470         (setq gnus-article-current nil))
6471       (when gnus-use-trees
6472         (gnus-tree-close group))
6473       (gnus-async-prefetch-remove-group group)
6474       (when (get-buffer gnus-article-buffer)
6475         (bury-buffer gnus-article-buffer))
6476       ;; Return to the group buffer.
6477       (gnus-configure-windows 'group 'force)
6478       ;; Clear the current group name.
6479       (setq gnus-newsgroup-name nil)
6480       (unless (gnus-ephemeral-group-p group)
6481         (gnus-group-update-group group))
6482       (when (equal (gnus-group-group-name) group)
6483         (gnus-group-next-unread-group 1))
6484       (when quit-config
6485         (gnus-handle-ephemeral-exit quit-config)))))
6486
6487 (defun gnus-handle-ephemeral-exit (quit-config)
6488   "Handle movement when leaving an ephemeral group.
6489 The state which existed when entering the ephemeral is reset."
6490   (if (not (buffer-name (car quit-config)))
6491       (gnus-configure-windows 'group 'force)
6492     (set-buffer (car quit-config))
6493     (cond ((eq major-mode 'gnus-summary-mode)
6494            (gnus-set-global-variables))
6495           ((eq major-mode 'gnus-article-mode)
6496            (save-excursion
6497              ;; The `gnus-summary-buffer' variable may point
6498              ;; to the old summary buffer when using a single
6499              ;; article buffer.
6500              (unless (gnus-buffer-live-p gnus-summary-buffer)
6501                (set-buffer gnus-group-buffer))
6502              (set-buffer gnus-summary-buffer)
6503              (gnus-set-global-variables))))
6504     (if (or (eq (cdr quit-config) 'article)
6505             (eq (cdr quit-config) 'pick))
6506         (progn
6507           ;; The current article may be from the ephemeral group
6508           ;; thus it is best that we reload this article
6509           (gnus-summary-show-article)
6510           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6511               (gnus-configure-windows 'pick 'force)
6512             (gnus-configure-windows (cdr quit-config) 'force)))
6513       (gnus-configure-windows (cdr quit-config) 'force))
6514     (when (eq major-mode 'gnus-summary-mode)
6515       (gnus-summary-next-subject 1 nil t)
6516       (gnus-summary-recenter)
6517       (gnus-summary-position-point))))
6518
6519 ;;; Dead summaries.
6520
6521 (defvar gnus-dead-summary-mode-map nil)
6522
6523 (unless gnus-dead-summary-mode-map
6524   (setq gnus-dead-summary-mode-map (make-keymap))
6525   (suppress-keymap gnus-dead-summary-mode-map)
6526   (substitute-key-definition
6527    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6528   (dolist (key '("\C-d" "\r" "\177" [delete]))
6529     (define-key gnus-dead-summary-mode-map
6530       key 'gnus-summary-wake-up-the-dead))
6531   (dolist (key '("q" "Q"))
6532     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6533
6534 (defvar gnus-dead-summary-mode nil
6535   "Minor mode for Gnus summary buffers.")
6536
6537 (defun gnus-dead-summary-mode (&optional arg)
6538   "Minor mode for Gnus summary buffers."
6539   (interactive "P")
6540   (when (eq major-mode 'gnus-summary-mode)
6541     (make-local-variable 'gnus-dead-summary-mode)
6542     (setq gnus-dead-summary-mode
6543           (if (null arg) (not gnus-dead-summary-mode)
6544             (> (prefix-numeric-value arg) 0)))
6545     (when gnus-dead-summary-mode
6546       (gnus-add-minor-mode
6547        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6548
6549 (defun gnus-deaden-summary ()
6550   "Make the current summary buffer into a dead summary buffer."
6551   ;; Kill any previous dead summary buffer.
6552   (when (and gnus-dead-summary
6553              (buffer-name gnus-dead-summary))
6554     (save-excursion
6555       (set-buffer gnus-dead-summary)
6556       (when gnus-dead-summary-mode
6557         (kill-buffer (current-buffer)))))
6558   ;; Make this the current dead summary.
6559   (setq gnus-dead-summary (current-buffer))
6560   (gnus-dead-summary-mode 1)
6561   (let ((name (buffer-name)))
6562     (when (string-match "Summary" name)
6563       (rename-buffer
6564        (concat (substring name 0 (match-beginning 0)) "Dead "
6565                (substring name (match-beginning 0)))
6566        t)
6567       (bury-buffer))))
6568
6569 (defun gnus-kill-or-deaden-summary (buffer)
6570   "Kill or deaden the summary BUFFER."
6571   (save-excursion
6572     (when (and (buffer-name buffer)
6573                (not gnus-single-article-buffer))
6574       (save-excursion
6575         (set-buffer buffer)
6576         (gnus-kill-buffer gnus-article-buffer)
6577         (gnus-kill-buffer gnus-original-article-buffer)))
6578     (cond
6579      ;; Kill the buffer.
6580      (gnus-kill-summary-on-exit
6581       (when (and gnus-use-trees
6582                  (gnus-buffer-exists-p buffer))
6583         (save-excursion
6584           (set-buffer buffer)
6585           (gnus-tree-close gnus-newsgroup-name)))
6586       (gnus-kill-buffer buffer))
6587      ;; Deaden the buffer.
6588      ((gnus-buffer-exists-p buffer)
6589       (save-excursion
6590         (set-buffer buffer)
6591         (gnus-deaden-summary))))))
6592
6593 (defun gnus-summary-wake-up-the-dead (&rest args)
6594   "Wake up the dead summary buffer."
6595   (interactive)
6596   (gnus-dead-summary-mode -1)
6597   (let ((name (buffer-name)))
6598     (when (string-match "Dead " name)
6599       (rename-buffer
6600        (concat (substring name 0 (match-beginning 0))
6601                (substring name (match-end 0)))
6602        t)))
6603   (gnus-message 3 "This dead summary is now alive again"))
6604
6605 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6606 (defun gnus-summary-fetch-faq (&optional faq-dir)
6607   "Fetch the FAQ for the current group.
6608 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6609 in."
6610   (interactive
6611    (list
6612     (when current-prefix-arg
6613       (completing-read
6614        "FAQ dir: " (and (listp gnus-group-faq-directory)
6615                         (mapcar (lambda (file) (list file))
6616                                 gnus-group-faq-directory))))))
6617   (let (gnus-faq-buffer)
6618     (when (setq gnus-faq-buffer
6619                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6620       (gnus-configure-windows 'summary-faq))))
6621
6622 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6623 (defun gnus-summary-describe-group (&optional force)
6624   "Describe the current newsgroup."
6625   (interactive "P")
6626   (gnus-group-describe-group force gnus-newsgroup-name))
6627
6628 (defun gnus-summary-describe-briefly ()
6629   "Describe summary mode commands briefly."
6630   (interactive)
6631   (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")))
6632
6633 ;; Walking around group mode buffer from summary mode.
6634
6635 (defun gnus-summary-next-group (&optional no-article target-group backward)
6636   "Exit current newsgroup and then select next unread newsgroup.
6637 If prefix argument NO-ARTICLE is non-nil, no article is selected
6638 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6639 previous group instead."
6640   (interactive "P")
6641   ;; Stop pre-fetching.
6642   (gnus-async-halt-prefetch)
6643   (let ((current-group gnus-newsgroup-name)
6644         (current-buffer (current-buffer))
6645         entered)
6646     (while (not entered)
6647       ;; Then we find what group we are supposed to enter.
6648       (set-buffer gnus-group-buffer)
6649       (gnus-group-jump-to-group current-group)
6650       (setq target-group
6651             (or target-group
6652                 (if (eq gnus-keep-same-level 'best)
6653                     (gnus-summary-best-group gnus-newsgroup-name)
6654                   (gnus-summary-search-group backward gnus-keep-same-level))))
6655       (if (not target-group)
6656           ;; There are no further groups, so we return to the group
6657           ;; buffer.
6658           (progn
6659             (gnus-message 5 "Returning to the group buffer")
6660             (setq entered t)
6661             (when (gnus-buffer-live-p current-buffer)
6662               (set-buffer current-buffer)
6663               (gnus-summary-exit))
6664             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6665         ;; We try to enter the target group.
6666         (gnus-group-jump-to-group target-group)
6667         (let ((unreads (gnus-group-group-unread)))
6668           (if (and (or (eq t unreads)
6669                        (and unreads (not (zerop unreads))))
6670                    (progn
6671                      ;; Now we semi-exit this group to update Xrefs
6672                      ;; and all variables.  We can't do a real exit,
6673                      ;; because the window conf must remain the same
6674                      ;; in case the user is prompted for info, and we
6675                      ;; don't want the window conf to change before
6676                      ;; that...
6677                      (when (gnus-buffer-live-p current-buffer)
6678                        (set-buffer current-buffer)
6679                        (gnus-summary-exit t))
6680                      (gnus-summary-read-group
6681                       target-group nil no-article
6682                       (and (buffer-name current-buffer) current-buffer)
6683                       nil backward)))
6684               (setq entered t)
6685             (setq current-group target-group
6686                   target-group nil)))))))
6687
6688 (defun gnus-summary-prev-group (&optional no-article)
6689   "Exit current newsgroup and then select previous unread newsgroup.
6690 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6691   (interactive "P")
6692   (gnus-summary-next-group no-article nil t))
6693
6694 ;; Walking around summary lines.
6695
6696 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6697   "Go to the first unread subject.
6698 If UNREAD is non-nil, go to the first unread article.
6699 Returns the article selected or nil if there are no unread articles."
6700   (interactive "P")
6701   (prog1
6702       (cond
6703        ;; Empty summary.
6704        ((null gnus-newsgroup-data)
6705         (gnus-message 3 "No articles in the group")
6706         nil)
6707        ;; Pick the first article.
6708        ((not unread)
6709         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6710         (gnus-data-number (car gnus-newsgroup-data)))
6711        ;; No unread articles.
6712        ((null gnus-newsgroup-unreads)
6713         (gnus-message 3 "No more unread articles")
6714         nil)
6715        ;; Find the first unread article.
6716        (t
6717         (let ((data gnus-newsgroup-data))
6718           (while (and data
6719                       (and (not (and undownloaded
6720                                      (memq (car data)
6721                                            gnus-newsgroup-undownloaded)))
6722                            (if unseen
6723                                (or (not (memq
6724                                          (gnus-data-number (car data))
6725                                          gnus-newsgroup-unseen))
6726                                    (not (gnus-data-unread-p (car data))))
6727                              (not (gnus-data-unread-p (car data))))))
6728             (setq data (cdr data)))
6729           (when data
6730             (goto-char (gnus-data-pos (car data)))
6731             (gnus-data-number (car data))))))
6732     (gnus-summary-position-point)))
6733
6734 (defun gnus-summary-next-subject (n &optional unread dont-display)
6735   "Go to next N'th summary line.
6736 If N is negative, go to the previous N'th subject line.
6737 If UNREAD is non-nil, only unread articles are selected.
6738 The difference between N and the actual number of steps taken is
6739 returned."
6740   (interactive "p")
6741   (let ((backward (< n 0))
6742         (n (abs n)))
6743     (while (and (> n 0)
6744                 (if backward
6745                     (gnus-summary-find-prev unread)
6746                   (gnus-summary-find-next unread)))
6747       (unless (zerop (setq n (1- n)))
6748         (gnus-summary-show-thread)))
6749     (when (/= 0 n)
6750       (gnus-message 7 "No more%s articles"
6751                     (if unread " unread" "")))
6752     (unless dont-display
6753       (gnus-summary-recenter)
6754       (gnus-summary-position-point))
6755     n))
6756
6757 (defun gnus-summary-next-unread-subject (n)
6758   "Go to next N'th unread summary line."
6759   (interactive "p")
6760   (gnus-summary-next-subject n t))
6761
6762 (defun gnus-summary-prev-subject (n &optional unread)
6763   "Go to previous N'th summary line.
6764 If optional argument UNREAD is non-nil, only unread article is selected."
6765   (interactive "p")
6766   (gnus-summary-next-subject (- n) unread))
6767
6768 (defun gnus-summary-prev-unread-subject (n)
6769   "Go to previous N'th unread summary line."
6770   (interactive "p")
6771   (gnus-summary-next-subject (- n) t))
6772
6773 (defun gnus-summary-goto-subjects (articles)
6774   "Insert the subject header for ARTICLES in the current buffer."
6775   (save-excursion
6776     (dolist (article articles)
6777       (gnus-summary-goto-subject article t)))
6778   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6779   (gnus-summary-position-point))
6780   
6781 (defun gnus-summary-goto-subject (article &optional force silent)
6782   "Go the subject line of ARTICLE.
6783 If FORCE, also allow jumping to articles not currently shown."
6784   (interactive "nArticle number: ")
6785   (unless (numberp article)
6786     (error "Article %s is not a number" article))
6787   (let ((b (point))
6788         (data (gnus-data-find article)))
6789     ;; We read in the article if we have to.
6790     (and (not data)
6791          force
6792          (gnus-summary-insert-subject
6793           article
6794           (if (or (numberp force) (vectorp force)) force)
6795           t)
6796          (setq data (gnus-data-find article)))
6797     (goto-char b)
6798     (if (not data)
6799         (progn
6800           (unless silent
6801             (gnus-message 3 "Can't find article %d" article))
6802           nil)
6803       (let ((pt (gnus-data-pos data)))
6804         (goto-char pt)
6805         (gnus-summary-set-article-display-arrow pt))
6806       (gnus-summary-position-point)
6807       article)))
6808
6809 ;; Walking around summary lines with displaying articles.
6810
6811 (defun gnus-summary-expand-window (&optional arg)
6812   "Make the summary buffer take up the entire Emacs frame.
6813 Given a prefix, will force an `article' buffer configuration."
6814   (interactive "P")
6815   (if arg
6816       (gnus-configure-windows 'article 'force)
6817     (gnus-configure-windows 'summary 'force)))
6818
6819 (defun gnus-summary-display-article (article &optional all-header)
6820   "Display ARTICLE in article buffer."
6821   (when (gnus-buffer-live-p gnus-article-buffer)
6822     (with-current-buffer gnus-article-buffer
6823       (mm-enable-multibyte)))
6824   (gnus-set-global-variables)
6825   (when (gnus-buffer-live-p gnus-article-buffer)
6826     (with-current-buffer gnus-article-buffer
6827       (setq gnus-article-charset gnus-newsgroup-charset)
6828       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6829       (mm-enable-multibyte)))
6830   (if (null article)
6831       nil
6832     (prog1
6833         (if gnus-summary-display-article-function
6834             (funcall gnus-summary-display-article-function article all-header)
6835           (gnus-article-prepare article all-header))
6836       (gnus-run-hooks 'gnus-select-article-hook)
6837       (when (and gnus-current-article
6838                  (not (zerop gnus-current-article)))
6839         (gnus-summary-goto-subject gnus-current-article))
6840       (gnus-summary-recenter)
6841       (when (and gnus-use-trees gnus-show-threads)
6842         (gnus-possibly-generate-tree article)
6843         (gnus-highlight-selected-tree article))
6844       ;; Successfully display article.
6845       (gnus-article-set-window-start
6846        (cdr (assq article gnus-newsgroup-bookmarks))))))
6847
6848 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6849   "Select the current article.
6850 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6851 non-nil, the article will be re-fetched even if it already present in
6852 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6853 be displayed."
6854   ;; Make sure we are in the summary buffer to work around bbdb bug.
6855   (unless (eq major-mode 'gnus-summary-mode)
6856     (set-buffer gnus-summary-buffer))
6857   (let ((article (or article (gnus-summary-article-number)))
6858         (all-headers (not (not all-headers))) ;Must be t or nil.
6859         gnus-summary-display-article-function)
6860     (and (not pseudo)
6861          (gnus-summary-article-pseudo-p article)
6862          (error "This is a pseudo-article"))
6863     (save-excursion
6864       (set-buffer gnus-summary-buffer)
6865       (if (or (and gnus-single-article-buffer
6866                    (or (null gnus-current-article)
6867                        (null gnus-article-current)
6868                        (null (get-buffer gnus-article-buffer))
6869                        (not (eq article (cdr gnus-article-current)))
6870                        (not (equal (car gnus-article-current)
6871                                    gnus-newsgroup-name))))
6872               (and (not gnus-single-article-buffer)
6873                    (or (null gnus-current-article)
6874                        (not (eq gnus-current-article article))))
6875               force)
6876           ;; The requested article is different from the current article.
6877           (progn
6878             (gnus-summary-display-article article all-headers)
6879             (when (gnus-buffer-live-p gnus-article-buffer)
6880               (with-current-buffer gnus-article-buffer
6881                 (if (not gnus-article-decoded-p) ;; a local variable
6882                     (mm-disable-multibyte))))
6883             (gnus-article-set-window-start
6884              (cdr (assq article gnus-newsgroup-bookmarks)))
6885             article)
6886         'old))))
6887
6888 (defun gnus-summary-force-verify-and-decrypt ()
6889   (interactive)
6890   (let ((mm-verify-option 'known)
6891         (mm-decrypt-option 'known)
6892         (gnus-buttonized-mime-types (append (list "multipart/signed"
6893                                                   "multipart/encrypted")
6894                                             gnus-buttonized-mime-types)))
6895     (gnus-summary-select-article nil 'force)))
6896
6897 (defun gnus-summary-set-current-mark (&optional current-mark)
6898   "Obsolete function."
6899   nil)
6900
6901 (defun gnus-summary-next-article (&optional unread subject backward push)
6902   "Select the next article.
6903 If UNREAD, only unread articles are selected.
6904 If SUBJECT, only articles with SUBJECT are selected.
6905 If BACKWARD, the previous article is selected instead of the next."
6906   (interactive "P")
6907   (cond
6908    ;; Is there such an article?
6909    ((and (gnus-summary-search-forward unread subject backward)
6910          (or (gnus-summary-display-article (gnus-summary-article-number))
6911              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6912     (gnus-summary-position-point))
6913    ;; If not, we try the first unread, if that is wanted.
6914    ((and subject
6915          gnus-auto-select-same
6916          (gnus-summary-first-unread-article))
6917     (gnus-summary-position-point)
6918     (gnus-message 6 "Wrapped"))
6919    ;; Try to get next/previous article not displayed in this group.
6920    ((and gnus-auto-extend-newsgroup
6921          (not unread) (not subject))
6922     (gnus-summary-goto-article
6923      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6924      nil (count-lines (point-min) (point))))
6925    ;; Go to next/previous group.
6926    (t
6927     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6928       (gnus-summary-jump-to-group gnus-newsgroup-name))
6929     (let ((cmd last-command-char)
6930           (point
6931            (save-excursion
6932              (set-buffer gnus-group-buffer)
6933              (point)))
6934           (group
6935            (if (eq gnus-keep-same-level 'best)
6936                (gnus-summary-best-group gnus-newsgroup-name)
6937              (gnus-summary-search-group backward gnus-keep-same-level))))
6938       ;; For some reason, the group window gets selected.  We change
6939       ;; it back.
6940       (select-window (get-buffer-window (current-buffer)))
6941       ;; Select next unread newsgroup automagically.
6942       (cond
6943        ((or (not gnus-auto-select-next)
6944             (not cmd))
6945         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6946        ((or (eq gnus-auto-select-next 'quietly)
6947             (and (eq gnus-auto-select-next 'slightly-quietly)
6948                  push)
6949             (and (eq gnus-auto-select-next 'almost-quietly)
6950                  (gnus-summary-last-article-p)))
6951         ;; Select quietly.
6952         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6953             (gnus-summary-exit)
6954           (gnus-message 7 "No more%s articles (%s)..."
6955                         (if unread " unread" "")
6956                         (if group (concat "selecting " group)
6957                           "exiting"))
6958           (gnus-summary-next-group nil group backward)))
6959        (t
6960         (when (gnus-key-press-event-p last-input-event)
6961           (gnus-summary-walk-group-buffer
6962            gnus-newsgroup-name cmd unread backward point))))))))
6963
6964 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6965   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6966                       (?\C-p (gnus-group-prev-unread-group 1))))
6967         (cursor-in-echo-area t)
6968         keve key group ended prompt)
6969     (save-excursion
6970       (set-buffer gnus-group-buffer)
6971       (goto-char start)
6972       (setq group
6973             (if (eq gnus-keep-same-level 'best)
6974                 (gnus-summary-best-group gnus-newsgroup-name)
6975               (gnus-summary-search-group backward gnus-keep-same-level))))
6976     (while (not ended)
6977       (setq prompt
6978             (format
6979              "No more%s articles%s " (if unread " unread" "")
6980              (if (and group
6981                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6982                  (format " (Type %s for %s [%s])"
6983                          (single-key-description cmd) group
6984                          (car (gnus-gethash group gnus-newsrc-hashtb)))
6985                (format " (Type %s to exit %s)"
6986                        (single-key-description cmd)
6987                        gnus-newsgroup-name))))
6988       ;; Confirm auto selection.
6989       (setq key (car (setq keve (gnus-read-event-char prompt)))
6990             ended t)
6991       (cond
6992        ((assq key keystrokes)
6993         (let ((obuf (current-buffer)))
6994           (switch-to-buffer gnus-group-buffer)
6995           (when group
6996             (gnus-group-jump-to-group group))
6997           (eval (cadr (assq key keystrokes)))
6998           (setq group (gnus-group-group-name))
6999           (switch-to-buffer obuf))
7000         (setq ended nil))
7001        ((equal key cmd)
7002         (if (or (not group)
7003                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7004             (gnus-summary-exit)
7005           (gnus-summary-next-group nil group backward)))
7006        (t
7007         (push (cdr keve) unread-command-events))))))
7008
7009 (defun gnus-summary-next-unread-article ()
7010   "Select unread article after current one."
7011   (interactive)
7012   (gnus-summary-next-article
7013    (or (not (eq gnus-summary-goto-unread 'never))
7014        (gnus-summary-last-article-p (gnus-summary-article-number)))
7015    (and gnus-auto-select-same
7016         (gnus-summary-article-subject))))
7017
7018 (defun gnus-summary-prev-article (&optional unread subject)
7019   "Select the article after the current one.
7020 If UNREAD is non-nil, only unread articles are selected."
7021   (interactive "P")
7022   (gnus-summary-next-article unread subject t))
7023
7024 (defun gnus-summary-prev-unread-article ()
7025   "Select unread article before current one."
7026   (interactive)
7027   (gnus-summary-prev-article
7028    (or (not (eq gnus-summary-goto-unread 'never))
7029        (gnus-summary-first-article-p (gnus-summary-article-number)))
7030    (and gnus-auto-select-same
7031         (gnus-summary-article-subject))))
7032
7033 (defun gnus-summary-next-page (&optional lines circular stop)
7034   "Show next page of the selected article.
7035 If at the end of the current article, select the next article.
7036 LINES says how many lines should be scrolled up.
7037
7038 If CIRCULAR is non-nil, go to the start of the article instead of
7039 selecting the next article when reaching the end of the current
7040 article.
7041
7042 If STOP is non-nil, just stop when reaching the end of the message."
7043   (interactive "P")
7044   (setq gnus-summary-buffer (current-buffer))
7045   (gnus-set-global-variables)
7046   (let ((article (gnus-summary-article-number))
7047         (article-window (get-buffer-window gnus-article-buffer t))
7048         endp)
7049     ;; If the buffer is empty, we have no article.
7050     (unless article
7051       (error "No article to select"))
7052     (gnus-configure-windows 'article)
7053     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7054         (if (and (eq gnus-summary-goto-unread 'never)
7055                  (not (gnus-summary-last-article-p article)))
7056             (gnus-summary-next-article)
7057           (gnus-summary-next-unread-article))
7058       (if (or (null gnus-current-article)
7059               (null gnus-article-current)
7060               (/= article (cdr gnus-article-current))
7061               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7062           ;; Selected subject is different from current article's.
7063           (gnus-summary-display-article article)
7064         (when article-window
7065           (gnus-eval-in-buffer-window gnus-article-buffer
7066             (setq endp (gnus-article-next-page lines)))
7067           (when endp
7068             (cond (stop
7069                    (gnus-message 3 "End of message"))
7070                   (circular
7071                    (gnus-summary-beginning-of-article))
7072                   (lines
7073                    (gnus-message 3 "End of message"))
7074                   ((null lines)
7075                    (if (and (eq gnus-summary-goto-unread 'never)
7076                             (not (gnus-summary-last-article-p article)))
7077                        (gnus-summary-next-article)
7078                      (gnus-summary-next-unread-article))))))))
7079     (gnus-summary-recenter)
7080     (gnus-summary-position-point)))
7081
7082 (defun gnus-summary-prev-page (&optional lines move)
7083   "Show previous page of selected article.
7084 Argument LINES specifies lines to be scrolled down.
7085 If MOVE, move to the previous unread article if point is at
7086 the beginning of the buffer."
7087   (interactive "P")
7088   (let ((article (gnus-summary-article-number))
7089         (article-window (get-buffer-window gnus-article-buffer t))
7090         endp)
7091     (gnus-configure-windows 'article)
7092     (if (or (null gnus-current-article)
7093             (null gnus-article-current)
7094             (/= article (cdr gnus-article-current))
7095             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7096         ;; Selected subject is different from current article's.
7097         (gnus-summary-display-article article)
7098       (gnus-summary-recenter)
7099       (when article-window
7100         (gnus-eval-in-buffer-window gnus-article-buffer
7101           (setq endp (gnus-article-prev-page lines)))
7102         (when (and move endp)
7103           (cond (lines
7104                  (gnus-message 3 "Beginning of message"))
7105                 ((null lines)
7106                  (if (and (eq gnus-summary-goto-unread 'never)
7107                           (not (gnus-summary-first-article-p article)))
7108                      (gnus-summary-prev-article)
7109                    (gnus-summary-prev-unread-article))))))))
7110   (gnus-summary-position-point))
7111
7112 (defun gnus-summary-prev-page-or-article (&optional lines)
7113   "Show previous page of selected article.
7114 Argument LINES specifies lines to be scrolled down.
7115 If at the beginning of the article, go to the next article."
7116   (interactive "P")
7117   (gnus-summary-prev-page lines t))
7118
7119 (defun gnus-summary-scroll-up (lines)
7120   "Scroll up (or down) one line current article.
7121 Argument LINES specifies lines to be scrolled up (or down if negative)."
7122   (interactive "p")
7123   (gnus-configure-windows 'article)
7124   (gnus-summary-show-thread)
7125   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7126     (gnus-eval-in-buffer-window gnus-article-buffer
7127       (cond ((> lines 0)
7128              (when (gnus-article-next-page lines)
7129                (gnus-message 3 "End of message")))
7130             ((< lines 0)
7131              (gnus-article-prev-page (- lines))))))
7132   (gnus-summary-recenter)
7133   (gnus-summary-position-point))
7134
7135 (defun gnus-summary-scroll-down (lines)
7136   "Scroll down (or up) one line current article.
7137 Argument LINES specifies lines to be scrolled down (or up if negative)."
7138   (interactive "p")
7139   (gnus-summary-scroll-up (- lines)))
7140
7141 (defun gnus-summary-next-same-subject ()
7142   "Select next article which has the same subject as current one."
7143   (interactive)
7144   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7145
7146 (defun gnus-summary-prev-same-subject ()
7147   "Select previous article which has the same subject as current one."
7148   (interactive)
7149   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7150
7151 (defun gnus-summary-next-unread-same-subject ()
7152   "Select next unread article which has the same subject as current one."
7153   (interactive)
7154   (gnus-summary-next-article t (gnus-summary-article-subject)))
7155
7156 (defun gnus-summary-prev-unread-same-subject ()
7157   "Select previous unread article which has the same subject as current one."
7158   (interactive)
7159   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7160
7161 (defun gnus-summary-first-unread-article ()
7162   "Select the first unread article.
7163 Return nil if there are no unread articles."
7164   (interactive)
7165   (prog1
7166       (when (gnus-summary-first-subject t)
7167         (gnus-summary-show-thread)
7168         (gnus-summary-first-subject t)
7169         (gnus-summary-display-article (gnus-summary-article-number)))
7170     (gnus-summary-position-point)))
7171
7172 (defun gnus-summary-first-unread-subject ()
7173   "Place the point on the subject line of the first unread article.
7174 Return nil if there are no unread articles."
7175   (interactive)
7176   (prog1
7177       (when (gnus-summary-first-subject t)
7178         (gnus-summary-show-thread)
7179         (gnus-summary-first-subject t))
7180     (gnus-summary-position-point)))
7181
7182 (defun gnus-summary-first-unseen-subject ()
7183   "Place the point on the subject line of the first unseen article.
7184 Return nil if there are no unseen articles."
7185   (interactive)
7186   (prog1
7187       (when (gnus-summary-first-subject t t t)
7188         (gnus-summary-show-thread)
7189         (gnus-summary-first-subject t t t))
7190     (gnus-summary-position-point)))
7191
7192 (defun gnus-summary-first-unseen-or-unread-subject ()
7193   "Place the point on the subject line of the first unseen article.
7194 Return nil if there are no unseen articles."
7195   (interactive)
7196   (prog1
7197       (unless (when (gnus-summary-first-subject t t t)
7198                 (gnus-summary-show-thread)
7199                 (gnus-summary-first-subject t t t))
7200         (when (gnus-summary-first-subject t)
7201           (gnus-summary-show-thread)
7202           (gnus-summary-first-subject t)))
7203     (gnus-summary-position-point)))
7204
7205 (defun gnus-summary-first-article ()
7206   "Select the first article.
7207 Return nil if there are no articles."
7208   (interactive)
7209   (prog1
7210       (when (gnus-summary-first-subject)
7211         (gnus-summary-show-thread)
7212         (gnus-summary-first-subject)
7213         (gnus-summary-display-article (gnus-summary-article-number)))
7214     (gnus-summary-position-point)))
7215
7216 (defun gnus-summary-best-unread-article (&optional arg)
7217   "Select the unread article with the highest score.
7218 If given a prefix argument, select the next unread article that has a
7219 score higher than the default score."
7220   (interactive "P")
7221   (let ((article (if arg
7222                      (gnus-summary-better-unread-subject)
7223                    (gnus-summary-best-unread-subject))))
7224     (if article
7225         (gnus-summary-goto-article article)
7226       (error "No unread articles"))))
7227
7228 (defun gnus-summary-best-unread-subject ()
7229   "Select the unread subject with the highest score."
7230   (interactive)
7231   (let ((best -1000000)
7232         (data gnus-newsgroup-data)
7233         article score)
7234     (while data
7235       (and (gnus-data-unread-p (car data))
7236            (> (setq score
7237                     (gnus-summary-article-score (gnus-data-number (car data))))
7238               best)
7239            (setq best score
7240                  article (gnus-data-number (car data))))
7241       (setq data (cdr data)))
7242     (when article
7243       (gnus-summary-goto-subject article))
7244     (gnus-summary-position-point)
7245     article))
7246
7247 (defun gnus-summary-better-unread-subject ()
7248   "Select the first unread subject that has a score over the default score."
7249   (interactive)
7250   (let ((data gnus-newsgroup-data)
7251         article score)
7252     (while (and (setq article (gnus-data-number (car data)))
7253                 (or (gnus-data-read-p (car data))
7254                     (not (> (gnus-summary-article-score article)
7255                             gnus-summary-default-score))))
7256       (setq data (cdr data)))
7257     (when article
7258       (gnus-summary-goto-subject article))
7259     (gnus-summary-position-point)
7260     article))
7261
7262 (defun gnus-summary-last-subject ()
7263   "Go to the last displayed subject line in the group."
7264   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7265     (when article
7266       (gnus-summary-goto-subject article))))
7267
7268 (defun gnus-summary-goto-article (article &optional all-headers force)
7269   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7270 If ALL-HEADERS is non-nil, no header lines are hidden.
7271 If FORCE, go to the article even if it isn't displayed.  If FORCE
7272 is a number, it is the line the article is to be displayed on."
7273   (interactive
7274    (list
7275     (completing-read
7276      "Article number or Message-ID: "
7277      (mapcar (lambda (number) (list (int-to-string number)))
7278              gnus-newsgroup-limit))
7279     current-prefix-arg
7280     t))
7281   (prog1
7282       (if (and (stringp article)
7283                (string-match "@" article))
7284           (gnus-summary-refer-article article)
7285         (when (stringp article)
7286           (setq article (string-to-number article)))
7287         (if (gnus-summary-goto-subject article force)
7288             (gnus-summary-display-article article all-headers)
7289           (gnus-message 4 "Couldn't go to article %s" article) nil))
7290     (gnus-summary-position-point)))
7291
7292 (defun gnus-summary-goto-last-article ()
7293   "Go to the previously read article."
7294   (interactive)
7295   (prog1
7296       (when gnus-last-article
7297         (gnus-summary-goto-article gnus-last-article nil t))
7298     (gnus-summary-position-point)))
7299
7300 (defun gnus-summary-pop-article (number)
7301   "Pop one article off the history and go to the previous.
7302 NUMBER articles will be popped off."
7303   (interactive "p")
7304   (let (to)
7305     (setq gnus-newsgroup-history
7306           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7307     (if to
7308         (gnus-summary-goto-article (car to) nil t)
7309       (error "Article history empty")))
7310   (gnus-summary-position-point))
7311
7312 ;; Summary commands and functions for limiting the summary buffer.
7313
7314 (defun gnus-summary-limit-to-articles (n)
7315   "Limit the summary buffer to the next N articles.
7316 If not given a prefix, use the process marked articles instead."
7317   (interactive "P")
7318   (prog1
7319       (let ((articles (gnus-summary-work-articles n)))
7320         (setq gnus-newsgroup-processable nil)
7321         (gnus-summary-limit articles))
7322     (gnus-summary-position-point)))
7323
7324 (defun gnus-summary-pop-limit (&optional total)
7325   "Restore the previous limit.
7326 If given a prefix, remove all limits."
7327   (interactive "P")
7328   (when total
7329     (setq gnus-newsgroup-limits
7330           (list (mapcar (lambda (h) (mail-header-number h))
7331                         gnus-newsgroup-headers))))
7332   (unless gnus-newsgroup-limits
7333     (error "No limit to pop"))
7334   (prog1
7335       (gnus-summary-limit nil 'pop)
7336     (gnus-summary-position-point)))
7337
7338 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7339   "Limit the summary buffer to articles that have subjects that match a regexp.
7340 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7341   (interactive
7342    (list (read-string (if current-prefix-arg
7343                           "Exclude subject (regexp): "
7344                         "Limit to subject (regexp): "))
7345          nil current-prefix-arg))
7346   (unless header
7347     (setq header "subject"))
7348   (when (not (equal "" subject))
7349     (prog1
7350         (let ((articles (gnus-summary-find-matching
7351                          (or header "subject") subject 'all nil nil
7352                          not-matching)))
7353           (unless articles
7354             (error "Found no matches for \"%s\"" subject))
7355           (gnus-summary-limit articles))
7356       (gnus-summary-position-point))))
7357
7358 (defun gnus-summary-limit-to-author (from &optional not-matching)
7359   "Limit the summary buffer to articles that have authors that match a regexp.
7360 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7361   (interactive
7362    (list (read-string (if current-prefix-arg
7363                           "Exclude author (regexp): "
7364                         "Limit to author (regexp): "))
7365          current-prefix-arg))
7366   (gnus-summary-limit-to-subject from "from" not-matching))
7367
7368 (defun gnus-summary-limit-to-age (age &optional younger-p)
7369   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7370 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7371 articles that are younger than AGE days."
7372   (interactive
7373    (let ((younger current-prefix-arg)
7374          (days-got nil)
7375          days)
7376      (while (not days-got)
7377        (setq days (if younger
7378                       (read-string "Limit to articles younger than (in days, older when negative): ")
7379                     (read-string
7380                      "Limit to articles older than (in days, younger when negative): ")))
7381        (when (> (length days) 0)
7382          (setq days (read days)))
7383        (if (numberp days)
7384            (progn
7385              (setq days-got t)
7386              (if (< days 0)
7387                  (progn
7388                    (setq younger (not younger))
7389                    (setq days (* days -1)))))
7390          (message "Please enter a number.")
7391          (sleep-for 1)))
7392      (list days younger)))
7393   (prog1
7394       (let ((data gnus-newsgroup-data)
7395             (cutoff (days-to-time age))
7396             articles d date is-younger)
7397         (while (setq d (pop data))
7398           (when (and (vectorp (gnus-data-header d))
7399                      (setq date (mail-header-date (gnus-data-header d))))
7400             (setq is-younger (time-less-p
7401                               (time-since (condition-case ()
7402                                               (date-to-time date)
7403                                             (error '(0 0))))
7404                               cutoff))
7405             (when (if younger-p
7406                       is-younger
7407                     (not is-younger))
7408               (push (gnus-data-number d) articles))))
7409         (gnus-summary-limit (nreverse articles)))
7410     (gnus-summary-position-point)))
7411
7412 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7413   "Limit the summary buffer to articles that match an 'extra' header."
7414   (interactive
7415    (let ((header
7416           (intern
7417            (gnus-completing-read-with-default
7418             (symbol-name (car gnus-extra-headers))
7419             (if current-prefix-arg
7420                 "Exclude extra header:"
7421               "Limit extra header:")
7422             (mapcar (lambda (x)
7423                       (cons (symbol-name x) x))
7424                     gnus-extra-headers)
7425             nil
7426             t))))
7427      (list header
7428            (read-string (format "%s header %s (regexp): "
7429                                 (if current-prefix-arg "Exclude" "Limit to")
7430                                 header))
7431            current-prefix-arg)))
7432   (when (not (equal "" regexp))
7433     (prog1
7434         (let ((articles (gnus-summary-find-matching
7435                          (cons 'extra header) regexp 'all nil nil
7436                          not-matching)))
7437           (unless articles
7438             (error "Found no matches for \"%s\"" regexp))
7439           (gnus-summary-limit articles))
7440       (gnus-summary-position-point))))
7441
7442 (defun gnus-summary-limit-to-display-predicate ()
7443   "Limit the summary buffer to the predicated in the `display' group parameter."
7444   (interactive)
7445   (unless gnus-newsgroup-display
7446     (error "There is no `display' group parameter"))
7447   (let (articles)
7448     (dolist (number gnus-newsgroup-articles)
7449       (when (funcall gnus-newsgroup-display)
7450         (push number articles)))
7451     (gnus-summary-limit articles))
7452   (gnus-summary-position-point))
7453
7454 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7455 (make-obsolete
7456  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7457
7458 (defun gnus-summary-limit-to-unread (&optional all)
7459   "Limit the summary buffer to articles that are not marked as read.
7460 If ALL is non-nil, limit strictly to unread articles."
7461   (interactive "P")
7462   (if all
7463       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7464     (gnus-summary-limit-to-marks
7465      ;; Concat all the marks that say that an article is read and have
7466      ;; those removed.
7467      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7468            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7469            gnus-low-score-mark gnus-expirable-mark
7470            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7471            gnus-duplicate-mark gnus-souped-mark)
7472      'reverse)))
7473
7474 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7475 (make-obsolete 'gnus-summary-delete-marked-with
7476                'gnus-summary-limit-exclude-marks)
7477
7478 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7479   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7480 If REVERSE, limit the summary buffer to articles that are marked
7481 with MARKS.  MARKS can either be a string of marks or a list of marks.
7482 Returns how many articles were removed."
7483   (interactive "sMarks: ")
7484   (gnus-summary-limit-to-marks marks t))
7485
7486 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7487   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7488 If REVERSE (the prefix), limit the summary buffer to articles that are
7489 not marked with MARKS.  MARKS can either be a string of marks or a
7490 list of marks.
7491 Returns how many articles were removed."
7492   (interactive "sMarks: \nP")
7493   (prog1
7494       (let ((data gnus-newsgroup-data)
7495             (marks (if (listp marks) marks
7496                      (append marks nil))) ; Transform to list.
7497             articles)
7498         (while data
7499           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7500                   (memq (gnus-data-mark (car data)) marks))
7501             (push (gnus-data-number (car data)) articles))
7502           (setq data (cdr data)))
7503         (gnus-summary-limit articles))
7504     (gnus-summary-position-point)))
7505
7506 (defun gnus-summary-limit-to-score (score)
7507   "Limit to articles with score at or above SCORE."
7508   (interactive "NLimit to articles with score of at least: ")
7509   (let ((data gnus-newsgroup-data)
7510         articles)
7511     (while data
7512       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7513                 score)
7514         (push (gnus-data-number (car data)) articles))
7515       (setq data (cdr data)))
7516     (prog1
7517         (gnus-summary-limit articles)
7518       (gnus-summary-position-point))))
7519
7520 (defun gnus-summary-limit-to-unseen ()
7521   "Limit to unseen articles."
7522   (interactive)
7523   (prog1
7524       (gnus-summary-limit gnus-newsgroup-unseen)
7525     (gnus-summary-position-point)))
7526
7527 (defun gnus-summary-limit-include-thread (id)
7528   "Display all the hidden articles that is in the thread with ID in it.
7529 When called interactively, ID is the Message-ID of the current
7530 article."
7531   (interactive (list (mail-header-id (gnus-summary-article-header))))
7532   (let ((articles (gnus-articles-in-thread
7533                    (gnus-id-to-thread (gnus-root-id id)))))
7534     (prog1
7535         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7536       (gnus-summary-limit-include-matching-articles
7537        "subject"
7538        (regexp-quote (gnus-simplify-subject-re
7539                       (mail-header-subject (gnus-id-to-header id)))))
7540       (gnus-summary-position-point))))
7541
7542 (defun gnus-summary-limit-include-matching-articles (header regexp)
7543   "Display all the hidden articles that have HEADERs that match REGEXP."
7544   (interactive (list (read-string "Match on header: ")
7545                      (read-string "Regexp: ")))
7546   (let ((articles (gnus-find-matching-articles header regexp)))
7547     (prog1
7548         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7549       (gnus-summary-position-point))))
7550
7551 (defun gnus-summary-insert-dormant-articles ()
7552   "Insert all the dormat articles for this group into the current buffer."
7553   (interactive)
7554   (let ((gnus-verbose (max 6 gnus-verbose)))
7555     (if (not gnus-newsgroup-dormant)
7556         (gnus-message 3 "No cached articles for this group")
7557       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7558
7559 (defun gnus-summary-limit-include-dormant ()
7560   "Display all the hidden articles that are marked as dormant.
7561 Note that this command only works on a subset of the articles currently
7562 fetched for this group."
7563   (interactive)
7564   (unless gnus-newsgroup-dormant
7565     (error "There are no dormant articles in this group"))
7566   (prog1
7567       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7568     (gnus-summary-position-point)))
7569
7570 (defun gnus-summary-limit-exclude-dormant ()
7571   "Hide all dormant articles."
7572   (interactive)
7573   (prog1
7574       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7575     (gnus-summary-position-point)))
7576
7577 (defun gnus-summary-limit-exclude-childless-dormant ()
7578   "Hide all dormant articles that have no children."
7579   (interactive)
7580   (let ((data (gnus-data-list t))
7581         articles d children)
7582     ;; Find all articles that are either not dormant or have
7583     ;; children.
7584     (while (setq d (pop data))
7585       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7586                 (and (setq children
7587                            (gnus-article-children (gnus-data-number d)))
7588                      (let (found)
7589                        (while children
7590                          (when (memq (car children) articles)
7591                            (setq children nil
7592                                  found t))
7593                          (pop children))
7594                        found)))
7595         (push (gnus-data-number d) articles)))
7596     ;; Do the limiting.
7597     (prog1
7598         (gnus-summary-limit articles)
7599       (gnus-summary-position-point))))
7600
7601 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7602   "Mark all unread excluded articles as read.
7603 If ALL, mark even excluded ticked and dormants as read."
7604   (interactive "P")
7605   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7606   (let ((articles (gnus-sorted-ndifference
7607                    (sort
7608                     (mapcar (lambda (h) (mail-header-number h))
7609                             gnus-newsgroup-headers)
7610                     '<)
7611                    gnus-newsgroup-limit))
7612         article)
7613     (setq gnus-newsgroup-unreads
7614           (gnus-sorted-intersection gnus-newsgroup-unreads
7615                                     gnus-newsgroup-limit))
7616     (if all
7617         (setq gnus-newsgroup-dormant nil
7618               gnus-newsgroup-marked nil
7619               gnus-newsgroup-reads
7620               (nconc
7621                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7622                gnus-newsgroup-reads))
7623       (while (setq article (pop articles))
7624         (unless (or (memq article gnus-newsgroup-dormant)
7625                     (memq article gnus-newsgroup-marked))
7626           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7627
7628 (defun gnus-summary-limit (articles &optional pop)
7629   (if pop
7630       ;; We pop the previous limit off the stack and use that.
7631       (setq articles (car gnus-newsgroup-limits)
7632             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7633     ;; We use the new limit, so we push the old limit on the stack.
7634     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7635   ;; Set the limit.
7636   (setq gnus-newsgroup-limit articles)
7637   (let ((total (length gnus-newsgroup-data))
7638         (data (gnus-data-find-list (gnus-summary-article-number)))
7639         (gnus-summary-mark-below nil)   ; Inhibit this.
7640         found)
7641     ;; This will do all the work of generating the new summary buffer
7642     ;; according to the new limit.
7643     (gnus-summary-prepare)
7644     ;; Hide any threads, possibly.
7645     (gnus-summary-maybe-hide-threads)
7646     ;; Try to return to the article you were at, or one in the
7647     ;; neighborhood.
7648     (when data
7649       ;; We try to find some article after the current one.
7650       (while data
7651         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7652           (setq data nil
7653                 found t))
7654         (setq data (cdr data))))
7655     (unless found
7656       ;; If there is no data, that means that we were after the last
7657       ;; article.  The same goes when we can't find any articles
7658       ;; after the current one.
7659       (goto-char (point-max))
7660       (gnus-summary-find-prev))
7661     (gnus-set-mode-line 'summary)
7662     ;; We return how many articles were removed from the summary
7663     ;; buffer as a result of the new limit.
7664     (- total (length gnus-newsgroup-data))))
7665
7666 (defsubst gnus-invisible-cut-children (threads)
7667   (let ((num 0))
7668     (while threads
7669       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7670         (incf num))
7671       (pop threads))
7672     (< num 2)))
7673
7674 (defsubst gnus-cut-thread (thread)
7675   "Go forwards in the thread until we find an article that we want to display."
7676   (when (or (eq gnus-fetch-old-headers 'some)
7677             (eq gnus-fetch-old-headers 'invisible)
7678             (numberp gnus-fetch-old-headers)
7679             (eq gnus-build-sparse-threads 'some)
7680             (eq gnus-build-sparse-threads 'more))
7681     ;; Deal with old-fetched headers and sparse threads.
7682     (while (and
7683             thread
7684             (or
7685              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7686              (gnus-summary-article-ancient-p
7687               (mail-header-number (car thread))))
7688             (if (or (<= (length (cdr thread)) 1)
7689                     (eq gnus-fetch-old-headers 'invisible))
7690                 (setq gnus-newsgroup-limit
7691                       (delq (mail-header-number (car thread))
7692                             gnus-newsgroup-limit)
7693                       thread (cadr thread))
7694               (when (gnus-invisible-cut-children (cdr thread))
7695                 (let ((th (cdr thread)))
7696                   (while th
7697                     (if (memq (mail-header-number (caar th))
7698                               gnus-newsgroup-limit)
7699                         (setq thread (car th)
7700                               th nil)
7701                       (setq th (cdr th))))))))))
7702   thread)
7703
7704 (defun gnus-cut-threads (threads)
7705   "Cut off all uninteresting articles from the beginning of threads."
7706   (when (or (eq gnus-fetch-old-headers 'some)
7707             (eq gnus-fetch-old-headers 'invisible)
7708             (numberp gnus-fetch-old-headers)
7709             (eq gnus-build-sparse-threads 'some)
7710             (eq gnus-build-sparse-threads 'more))
7711     (let ((th threads))
7712       (while th
7713         (setcar th (gnus-cut-thread (car th)))
7714         (setq th (cdr th)))))
7715   ;; Remove nixed out threads.
7716   (delq nil threads))
7717
7718 (defun gnus-summary-initial-limit (&optional show-if-empty)
7719   "Figure out what the initial limit is supposed to be on group entry.
7720 This entails weeding out unwanted dormants, low-scored articles,
7721 fetch-old-headers verbiage, and so on."
7722   ;; Most groups have nothing to remove.
7723   (if (or gnus-inhibit-limiting
7724           (and (null gnus-newsgroup-dormant)
7725                (eq gnus-newsgroup-display 'gnus-not-ignore)
7726                (not (eq gnus-fetch-old-headers 'some))
7727                (not (numberp gnus-fetch-old-headers))
7728                (not (eq gnus-fetch-old-headers 'invisible))
7729                (null gnus-summary-expunge-below)
7730                (not (eq gnus-build-sparse-threads 'some))
7731                (not (eq gnus-build-sparse-threads 'more))
7732                (null gnus-thread-expunge-below)
7733                (not gnus-use-nocem)))
7734       ()                                ; Do nothing.
7735     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7736     (setq gnus-newsgroup-limit nil)
7737     (mapatoms
7738      (lambda (node)
7739        (unless (car (symbol-value node))
7740          ;; These threads have no parents -- they are roots.
7741          (let ((nodes (cdr (symbol-value node)))
7742                thread)
7743            (while nodes
7744              (if (and gnus-thread-expunge-below
7745                       (< (gnus-thread-total-score (car nodes))
7746                          gnus-thread-expunge-below))
7747                  (gnus-expunge-thread (pop nodes))
7748                (setq thread (pop nodes))
7749                (gnus-summary-limit-children thread))))))
7750      gnus-newsgroup-dependencies)
7751     ;; If this limitation resulted in an empty group, we might
7752     ;; pop the previous limit and use it instead.
7753     (when (and (not gnus-newsgroup-limit)
7754                show-if-empty)
7755       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7756     gnus-newsgroup-limit))
7757
7758 (defun gnus-summary-limit-children (thread)
7759   "Return 1 if this subthread is visible and 0 if it is not."
7760   ;; First we get the number of visible children to this thread.  This
7761   ;; is done by recursing down the thread using this function, so this
7762   ;; will really go down to a leaf article first, before slowly
7763   ;; working its way up towards the root.
7764   (when thread
7765     (let* ((max-lisp-eval-depth 5000)
7766            (children
7767            (if (cdr thread)
7768                (apply '+ (mapcar 'gnus-summary-limit-children
7769                                  (cdr thread)))
7770              0))
7771           (number (mail-header-number (car thread)))
7772           score)
7773       (if (and
7774            (not (memq number gnus-newsgroup-marked))
7775            (or
7776             ;; If this article is dormant and has absolutely no visible
7777             ;; children, then this article isn't visible.
7778             (and (memq number gnus-newsgroup-dormant)
7779                  (zerop children))
7780             ;; If this is "fetch-old-headered" and there is no
7781             ;; visible children, then we don't want this article.
7782             (and (or (eq gnus-fetch-old-headers 'some)
7783                      (numberp gnus-fetch-old-headers))
7784                  (gnus-summary-article-ancient-p number)
7785                  (zerop children))
7786             ;; If this is "fetch-old-headered" and `invisible', then
7787             ;; we don't want this article.
7788             (and (eq gnus-fetch-old-headers 'invisible)
7789                  (gnus-summary-article-ancient-p number))
7790             ;; If this is a sparsely inserted article with no children,
7791             ;; we don't want it.
7792             (and (eq gnus-build-sparse-threads 'some)
7793                  (gnus-summary-article-sparse-p number)
7794                  (zerop children))
7795             ;; If we use expunging, and this article is really
7796             ;; low-scored, then we don't want this article.
7797             (when (and gnus-summary-expunge-below
7798                        (< (setq score
7799                                 (or (cdr (assq number gnus-newsgroup-scored))
7800                                     gnus-summary-default-score))
7801                           gnus-summary-expunge-below))
7802               ;; We increase the expunge-tally here, but that has
7803               ;; nothing to do with the limits, really.
7804               (incf gnus-newsgroup-expunged-tally)
7805               ;; We also mark as read here, if that's wanted.
7806               (when (and gnus-summary-mark-below
7807                          (< score gnus-summary-mark-below))
7808                 (setq gnus-newsgroup-unreads
7809                       (delq number gnus-newsgroup-unreads))
7810                 (if gnus-newsgroup-auto-expire
7811                     (push number gnus-newsgroup-expirable)
7812                   (push (cons number gnus-low-score-mark)
7813                         gnus-newsgroup-reads)))
7814               t)
7815             ;; Do the `display' group parameter.
7816             (and gnus-newsgroup-display
7817                  (not (funcall gnus-newsgroup-display)))
7818             ;; Check NoCeM things.
7819             (if (and gnus-use-nocem
7820                      (gnus-nocem-unwanted-article-p
7821                       (mail-header-id (car thread))))
7822                 (progn
7823                   (setq gnus-newsgroup-unreads
7824                         (delq number gnus-newsgroup-unreads))
7825                   t))))
7826           ;; Nope, invisible article.
7827           0
7828         ;; Ok, this article is to be visible, so we add it to the limit
7829         ;; and return 1.
7830         (push number gnus-newsgroup-limit)
7831         1))))
7832
7833 (defun gnus-expunge-thread (thread)
7834   "Mark all articles in THREAD as read."
7835   (let* ((number (mail-header-number (car thread))))
7836     (incf gnus-newsgroup-expunged-tally)
7837     ;; We also mark as read here, if that's wanted.
7838     (setq gnus-newsgroup-unreads
7839           (delq number gnus-newsgroup-unreads))
7840     (if gnus-newsgroup-auto-expire
7841         (push number gnus-newsgroup-expirable)
7842       (push (cons number gnus-low-score-mark)
7843             gnus-newsgroup-reads)))
7844   ;; Go recursively through all subthreads.
7845   (mapcar 'gnus-expunge-thread (cdr thread)))
7846
7847 ;; Summary article oriented commands
7848
7849 (defun gnus-summary-refer-parent-article (n)
7850   "Refer parent article N times.
7851 If N is negative, go to ancestor -N instead.
7852 The difference between N and the number of articles fetched is returned."
7853   (interactive "p")
7854   (let ((skip 1)
7855         error header ref)
7856     (when (not (natnump n))
7857       (setq skip (abs n)
7858             n 1))
7859     (while (and (> n 0)
7860                 (not error))
7861       (setq header (gnus-summary-article-header))
7862       (if (and (eq (mail-header-number header)
7863                    (cdr gnus-article-current))
7864                (equal gnus-newsgroup-name
7865                       (car gnus-article-current)))
7866           ;; If we try to find the parent of the currently
7867           ;; displayed article, then we take a look at the actual
7868           ;; References header, since this is slightly more
7869           ;; reliable than the References field we got from the
7870           ;; server.
7871           (save-excursion
7872             (set-buffer gnus-original-article-buffer)
7873             (nnheader-narrow-to-headers)
7874             (unless (setq ref (message-fetch-field "references"))
7875               (setq ref (message-fetch-field "in-reply-to")))
7876             (widen))
7877         (setq ref
7878               ;; It's not the current article, so we take a bet on
7879               ;; the value we got from the server.
7880               (mail-header-references header)))
7881       (if (and ref
7882                (not (equal ref "")))
7883           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7884             (gnus-message 1 "Couldn't find parent"))
7885         (gnus-message 1 "No references in article %d"
7886                       (gnus-summary-article-number))
7887         (setq error t))
7888       (decf n))
7889     (gnus-summary-position-point)
7890     n))
7891
7892 (defun gnus-summary-refer-references ()
7893   "Fetch all articles mentioned in the References header.
7894 Return the number of articles fetched."
7895   (interactive)
7896   (let ((ref (mail-header-references (gnus-summary-article-header)))
7897         (current (gnus-summary-article-number))
7898         (n 0))
7899     (if (or (not ref)
7900             (equal ref ""))
7901         (error "No References in the current article")
7902       ;; For each Message-ID in the References header...
7903       (while (string-match "<[^>]*>" ref)
7904         (incf n)
7905         ;; ... fetch that article.
7906         (gnus-summary-refer-article
7907          (prog1 (match-string 0 ref)
7908            (setq ref (substring ref (match-end 0))))))
7909       (gnus-summary-goto-subject current)
7910       (gnus-summary-position-point)
7911       n)))
7912
7913 (defun gnus-summary-refer-thread (&optional limit)
7914   "Fetch all articles in the current thread.
7915 If LIMIT (the numerical prefix), fetch that many old headers instead
7916 of what's specified by the `gnus-refer-thread-limit' variable."
7917   (interactive "P")
7918   (let ((id (mail-header-id (gnus-summary-article-header)))
7919         (limit (if limit (prefix-numeric-value limit)
7920                  gnus-refer-thread-limit)))
7921     ;; We want to fetch LIMIT *old* headers, but we also have to
7922     ;; re-fetch all the headers in the current buffer, because many of
7923     ;; them may be undisplayed.  So we adjust LIMIT.
7924     (when (numberp limit)
7925       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7926     (unless (eq gnus-fetch-old-headers 'invisible)
7927       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7928       ;; Retrieve the headers and read them in.
7929       (if (eq (gnus-retrieve-headers
7930                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7931               'nov)
7932           (gnus-build-all-threads)
7933         (error "Can't fetch thread from backends that don't support NOV"))
7934       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7935     (gnus-summary-limit-include-thread id)))
7936
7937 (defun gnus-summary-refer-article (message-id)
7938   "Fetch an article specified by MESSAGE-ID."
7939   (interactive "sMessage-ID: ")
7940   (when (and (stringp message-id)
7941              (not (zerop (length message-id))))
7942     ;; Construct the correct Message-ID if necessary.
7943     ;; Suggested by tale@pawl.rpi.edu.
7944     (unless (string-match "^<" message-id)
7945       (setq message-id (concat "<" message-id)))
7946     (unless (string-match ">$" message-id)
7947       (setq message-id (concat message-id ">")))
7948     (let* ((header (gnus-id-to-header message-id))
7949            (sparse (and header
7950                         (gnus-summary-article-sparse-p
7951                          (mail-header-number header))
7952                         (memq (mail-header-number header)
7953                               gnus-newsgroup-limit)))
7954            number)
7955       (cond
7956        ;; If the article is present in the buffer we just go to it.
7957        ((and header
7958              (or (not (gnus-summary-article-sparse-p
7959                        (mail-header-number header)))
7960                  sparse))
7961         (prog1
7962             (gnus-summary-goto-article
7963              (mail-header-number header) nil t)
7964           (when sparse
7965             (gnus-summary-update-article (mail-header-number header)))))
7966        (t
7967         ;; We fetch the article.
7968         (catch 'found
7969           (dolist (gnus-override-method (gnus-refer-article-methods))
7970             (gnus-check-server gnus-override-method)
7971             ;; Fetch the header, and display the article.
7972             (when (setq number (gnus-summary-insert-subject message-id))
7973               (gnus-summary-select-article nil nil nil number)
7974               (throw 'found t)))
7975           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7976
7977 (defun gnus-refer-article-methods ()
7978   "Return a list of referable methods."
7979   (cond
7980    ;; No method, so we default to current and native.
7981    ((null gnus-refer-article-method)
7982     (list gnus-current-select-method gnus-select-method))
7983    ;; Current.
7984    ((eq 'current gnus-refer-article-method)
7985     (list gnus-current-select-method))
7986    ;; List of select methods.
7987    ((not (and (symbolp (car gnus-refer-article-method))
7988               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7989     (let (out)
7990       (dolist (method gnus-refer-article-method)
7991         (push (if (eq 'current method)
7992                   gnus-current-select-method
7993                 method)
7994               out))
7995       (nreverse out)))
7996    ;; One single select method.
7997    (t
7998     (list gnus-refer-article-method))))
7999
8000 (defun gnus-summary-edit-parameters ()
8001   "Edit the group parameters of the current group."
8002   (interactive)
8003   (gnus-group-edit-group gnus-newsgroup-name 'params))
8004
8005 (defun gnus-summary-customize-parameters ()
8006   "Customize the group parameters of the current group."
8007   (interactive)
8008   (gnus-group-customize gnus-newsgroup-name))
8009
8010 (defun gnus-summary-enter-digest-group (&optional force)
8011   "Enter an nndoc group based on the current article.
8012 If FORCE, force a digest interpretation.  If not, try
8013 to guess what the document format is."
8014   (interactive "P")
8015   (let ((conf gnus-current-window-configuration))
8016     (save-excursion
8017       (gnus-summary-select-article))
8018     (setq gnus-current-window-configuration conf)
8019     (let* ((name (format "%s-%d"
8020                          (gnus-group-prefixed-name
8021                           gnus-newsgroup-name (list 'nndoc ""))
8022                          (save-excursion
8023                            (set-buffer gnus-summary-buffer)
8024                            gnus-current-article)))
8025            (ogroup gnus-newsgroup-name)
8026            (params (append (gnus-info-params (gnus-get-info ogroup))
8027                            (list (cons 'to-group ogroup))
8028                            (list (cons 'save-article-group ogroup))))
8029            (case-fold-search t)
8030            (buf (current-buffer))
8031            dig to-address)
8032       (save-excursion
8033         (set-buffer gnus-original-article-buffer)
8034         ;; Have the digest group inherit the main mail address of
8035         ;; the parent article.
8036         (when (setq to-address (or (gnus-fetch-field "reply-to")
8037                                    (gnus-fetch-field "from")))
8038           (setq params (append
8039                         (list (cons 'to-address
8040                                     (funcall gnus-decode-encoded-word-function
8041                                              to-address))))))
8042         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8043         (insert-buffer-substring gnus-original-article-buffer)
8044         ;; Remove lines that may lead nndoc to misinterpret the
8045         ;; document type.
8046         (narrow-to-region
8047          (goto-char (point-min))
8048          (or (search-forward "\n\n" nil t) (point)))
8049         (goto-char (point-min))
8050         (delete-matching-lines "^Path:\\|^From ")
8051         (widen))
8052       (unwind-protect
8053           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8054                     (gnus-newsgroup-ephemeral-ignored-charsets
8055                      gnus-newsgroup-ignored-charsets))
8056                 (gnus-group-read-ephemeral-group
8057                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8058                               (nndoc-article-type
8059                                ,(if force 'mbox 'guess)))
8060                  t nil nil nil
8061                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8062                                                         "ADAPT")))))
8063               ;; Make all postings to this group go to the parent group.
8064               (nconc (gnus-info-params (gnus-get-info name))
8065                      params)
8066             ;; Couldn't select this doc group.
8067             (switch-to-buffer buf)
8068             (gnus-set-global-variables)
8069             (gnus-configure-windows 'summary)
8070             (gnus-message 3 "Article couldn't be entered?"))
8071         (kill-buffer dig)))))
8072
8073 (defun gnus-summary-read-document (n)
8074   "Open a new group based on the current article(s).
8075 This will allow you to read digests and other similar
8076 documents as newsgroups.
8077 Obeys the standard process/prefix convention."
8078   (interactive "P")
8079   (let* ((articles (gnus-summary-work-articles n))
8080          (ogroup gnus-newsgroup-name)
8081          (params (append (gnus-info-params (gnus-get-info ogroup))
8082                          (list (cons 'to-group ogroup))))
8083          article group egroup groups vgroup)
8084     (while (setq article (pop articles))
8085       (setq group (format "%s-%d" gnus-newsgroup-name article))
8086       (gnus-summary-remove-process-mark article)
8087       (when (gnus-summary-display-article article)
8088         (save-excursion
8089           (with-temp-buffer
8090             (insert-buffer-substring gnus-original-article-buffer)
8091             ;; Remove some headers that may lead nndoc to make
8092             ;; the wrong guess.
8093             (message-narrow-to-head)
8094             (goto-char (point-min))
8095             (delete-matching-lines "^\\(Path\\):\\|^From ")
8096             (widen)
8097             (if (setq egroup
8098                       (gnus-group-read-ephemeral-group
8099                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8100                                      (nndoc-article-type guess))
8101                        t nil t))
8102                 (progn
8103             ;; Make all postings to this group go to the parent group.
8104                   (nconc (gnus-info-params (gnus-get-info egroup))
8105                          params)
8106                   (push egroup groups))
8107               ;; Couldn't select this doc group.
8108               (gnus-error 3 "Article couldn't be entered"))))))
8109     ;; Now we have selected all the documents.
8110     (cond
8111      ((not groups)
8112       (error "None of the articles could be interpreted as documents"))
8113      ((gnus-group-read-ephemeral-group
8114        (setq vgroup (format
8115                      "nnvirtual:%s-%s" gnus-newsgroup-name
8116                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8117        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8118        t
8119        (cons (current-buffer) 'summary)))
8120      (t
8121       (error "Couldn't select virtual nndoc group")))))
8122
8123 (defun gnus-summary-isearch-article (&optional regexp-p)
8124   "Do incremental search forward on the current article.
8125 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8126   (interactive "P")
8127   (gnus-summary-select-article)
8128   (gnus-configure-windows 'article)
8129   (gnus-eval-in-buffer-window gnus-article-buffer
8130     (save-restriction
8131       (widen)
8132       (isearch-forward regexp-p))))
8133
8134 (defun gnus-summary-search-article-forward (regexp &optional backward)
8135   "Search for an article containing REGEXP forward.
8136 If BACKWARD, search backward instead."
8137   (interactive
8138    (list (read-string
8139           (format "Search article %s (regexp%s): "
8140                   (if current-prefix-arg "backward" "forward")
8141                   (if gnus-last-search-regexp
8142                       (concat ", default " gnus-last-search-regexp)
8143                     "")))
8144          current-prefix-arg))
8145   (if (string-equal regexp "")
8146       (setq regexp (or gnus-last-search-regexp ""))
8147     (setq gnus-last-search-regexp regexp)
8148     (setq gnus-article-before-search gnus-current-article))
8149   ;; Intentionally set gnus-last-article.
8150   (setq gnus-last-article gnus-article-before-search)
8151   (let ((gnus-last-article gnus-last-article))
8152     (if (gnus-summary-search-article regexp backward)
8153         (gnus-summary-show-thread)
8154       (error "Search failed: \"%s\"" regexp))))
8155
8156 (defun gnus-summary-search-article-backward (regexp)
8157   "Search for an article containing REGEXP backward."
8158   (interactive
8159    (list (read-string
8160           (format "Search article backward (regexp%s): "
8161                   (if gnus-last-search-regexp
8162                       (concat ", default " gnus-last-search-regexp)
8163                     "")))))
8164   (gnus-summary-search-article-forward regexp 'backward))
8165
8166 (defun gnus-summary-search-article (regexp &optional backward)
8167   "Search for an article containing REGEXP.
8168 Optional argument BACKWARD means do search for backward.
8169 `gnus-select-article-hook' is not called during the search."
8170   ;; We have to require this here to make sure that the following
8171   ;; dynamic binding isn't shadowed by autoloading.
8172   (require 'gnus-async)
8173   (require 'gnus-art)
8174   (let ((gnus-select-article-hook nil)  ;Disable hook.
8175         (gnus-article-prepare-hook nil)
8176         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8177         (gnus-use-article-prefetch nil)
8178         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8179         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8180         (sum (current-buffer))
8181         (gnus-display-mime-function nil)
8182         (found nil)
8183         point)
8184     (gnus-save-hidden-threads
8185       (gnus-summary-select-article)
8186       (set-buffer gnus-article-buffer)
8187       (goto-char (window-point (get-buffer-window (current-buffer))))
8188       (when backward
8189         (forward-line -1))
8190       (while (not found)
8191         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8192         (if (if backward
8193                 (re-search-backward regexp nil t)
8194               (re-search-forward regexp nil t))
8195             ;; We found the regexp.
8196             (progn
8197               (setq found 'found)
8198               (beginning-of-line)
8199               (set-window-start
8200                (get-buffer-window (current-buffer))
8201                (point))
8202               (forward-line 1)
8203               (set-window-point
8204                (get-buffer-window (current-buffer))
8205                (point))
8206               (set-buffer sum)
8207               (setq point (point)))
8208           ;; We didn't find it, so we go to the next article.
8209           (set-buffer sum)
8210           (setq found 'not)
8211           (while (eq found 'not)
8212             (if (not (if backward (gnus-summary-find-prev)
8213                        (gnus-summary-find-next)))
8214                 ;; No more articles.
8215                 (setq found t)
8216               ;; Select the next article and adjust point.
8217               (unless (gnus-summary-article-sparse-p
8218                        (gnus-summary-article-number))
8219                 (setq found nil)
8220                 (gnus-summary-select-article)
8221                 (set-buffer gnus-article-buffer)
8222                 (widen)
8223                 (goto-char (if backward (point-max) (point-min))))))))
8224       (gnus-message 7 ""))
8225     ;; Return whether we found the regexp.
8226     (when (eq found 'found)
8227       (goto-char point)
8228       (gnus-summary-show-thread)
8229       (gnus-summary-goto-subject gnus-current-article)
8230       (gnus-summary-position-point)
8231       t)))
8232
8233 (defun gnus-find-matching-articles (header regexp)
8234   "Return a list of all articles that match REGEXP on HEADER.
8235 This search includes all articles in the current group that Gnus has
8236 fetched headers for, whether they are displayed or not."
8237   (let ((articles nil)
8238         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8239         (case-fold-search t))
8240     (dolist (header gnus-newsgroup-headers)
8241       (when (string-match regexp (funcall func header))
8242         (push (mail-header-number header) articles)))
8243     (nreverse articles)))
8244
8245 (defun gnus-summary-find-matching (header regexp &optional backward unread
8246                                           not-case-fold not-matching)
8247   "Return a list of all articles that match REGEXP on HEADER.
8248 The search stars on the current article and goes forwards unless
8249 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8250 If UNREAD is non-nil, only unread articles will
8251 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8252 in the comparisons. If NOT-MATCHING, return a list of all articles that
8253 not match REGEXP on HEADER."
8254   (let ((case-fold-search (not not-case-fold))
8255         articles d func)
8256     (if (consp header)
8257         (if (eq (car header) 'extra)
8258             (setq func
8259                   `(lambda (h)
8260                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8261                          "")))
8262           (error "%s is an invalid header" header))
8263       (unless (fboundp (intern (concat "mail-header-" header)))
8264         (error "%s is not a valid header" header))
8265       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8266     (dolist (d (if (eq backward 'all)
8267                    gnus-newsgroup-data
8268                  (gnus-data-find-list
8269                   (gnus-summary-article-number)
8270                   (gnus-data-list backward))))
8271       (when (and (or (not unread)       ; We want all articles...
8272                      (gnus-data-unread-p d)) ; Or just unreads.
8273                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8274                  (if not-matching
8275                      (not (string-match
8276                            regexp
8277                            (funcall func (gnus-data-header d))))
8278                    (string-match regexp
8279                                  (funcall func (gnus-data-header d)))))
8280         (push (gnus-data-number d) articles))) ; Success!
8281     (nreverse articles)))
8282
8283 (defun gnus-summary-execute-command (header regexp command &optional backward)
8284   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8285 If HEADER is an empty string (or nil), the match is done on the entire
8286 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8287   (interactive
8288    (list (let ((completion-ignore-case t))
8289            (completing-read
8290             "Header name: "
8291             (mapcar (lambda (header) (list (format "%s" header)))
8292                     (append
8293                      '("Number" "Subject" "From" "Lines" "Date"
8294                        "Message-ID" "Xref" "References" "Body")
8295                      gnus-extra-headers))
8296             nil 'require-match))
8297          (read-string "Regexp: ")
8298          (read-key-sequence "Command: ")
8299          current-prefix-arg))
8300   (when (equal header "Body")
8301     (setq header ""))
8302   ;; Hidden thread subtrees must be searched as well.
8303   (gnus-summary-show-all-threads)
8304   ;; We don't want to change current point nor window configuration.
8305   (save-excursion
8306     (save-window-excursion
8307       (let (gnus-visual
8308             gnus-treat-strip-trailing-blank-lines
8309             gnus-treat-strip-leading-blank-lines
8310             gnus-treat-strip-multiple-blank-lines
8311             gnus-treat-hide-boring-headers
8312             gnus-treat-fold-newsgroups
8313             gnus-article-prepare-hook)
8314         (gnus-message 6 "Executing %s..." (key-description command))
8315         ;; We'd like to execute COMMAND interactively so as to give arguments.
8316         (gnus-execute header regexp
8317                       `(call-interactively ',(key-binding command))
8318                       backward)
8319         (gnus-message 6 "Executing %s...done" (key-description command))))))
8320
8321 (defun gnus-summary-beginning-of-article ()
8322   "Scroll the article back to the beginning."
8323   (interactive)
8324   (gnus-summary-select-article)
8325   (gnus-configure-windows 'article)
8326   (gnus-eval-in-buffer-window gnus-article-buffer
8327     (widen)
8328     (goto-char (point-min))
8329     (when gnus-page-broken
8330       (gnus-narrow-to-page))))
8331
8332 (defun gnus-summary-end-of-article ()
8333   "Scroll to the end of the article."
8334   (interactive)
8335   (gnus-summary-select-article)
8336   (gnus-configure-windows 'article)
8337   (gnus-eval-in-buffer-window gnus-article-buffer
8338     (widen)
8339     (goto-char (point-max))
8340     (recenter -3)
8341     (when gnus-page-broken
8342       (gnus-narrow-to-page))))
8343
8344 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8345   "Truncate to LEN and quote all \"(\"'s in STRING."
8346   (gnus-replace-in-string (if (and len (> (length string) len))
8347                               (substring string 0 len)
8348                             string)
8349                           "[()]" "\\\\\\&"))
8350
8351 (defun gnus-summary-print-article (&optional filename n)
8352   "Generate and print a PostScript image of the N next (mail) articles.
8353
8354 If N is negative, print the N previous articles.  If N is nil and articles
8355 have been marked with the process mark, print these instead.
8356
8357 If the optional first argument FILENAME is nil, send the image to the
8358 printer.  If FILENAME is a string, save the PostScript image in a file with
8359 that name.  If FILENAME is a number, prompt the user for the name of the file
8360 to save in."
8361   (interactive (list (ps-print-preprint current-prefix-arg)))
8362   (dolist (article (gnus-summary-work-articles n))
8363     (gnus-summary-select-article nil nil 'pseudo article)
8364     (gnus-eval-in-buffer-window gnus-article-buffer
8365       (gnus-print-buffer))
8366     (gnus-summary-remove-process-mark article))
8367   (ps-despool filename))
8368
8369 (defun gnus-print-buffer ()
8370   (let ((buffer (generate-new-buffer " *print*")))
8371     (unwind-protect
8372         (progn
8373           (copy-to-buffer buffer (point-min) (point-max))
8374           (set-buffer buffer)
8375           (gnus-article-delete-invisible-text)
8376           (gnus-remove-text-with-property 'gnus-decoration)
8377           (when (gnus-visual-p 'article-highlight 'highlight)
8378             ;; Copy-to-buffer doesn't copy overlay.  So redo
8379             ;; highlight.
8380             (let ((gnus-article-buffer buffer))
8381               (gnus-article-highlight-citation t)
8382               (gnus-article-highlight-signature)))
8383           (let ((ps-left-header
8384                  (list
8385                   (concat "("
8386                           (gnus-summary-print-truncate-and-quote
8387                            (mail-header-subject gnus-current-headers)
8388                            66) ")")
8389                   (concat "("
8390                           (gnus-summary-print-truncate-and-quote
8391                            (mail-header-from gnus-current-headers)
8392                            45) ")")))
8393                 (ps-right-header
8394                  (list
8395                   "/pagenumberstring load"
8396                   (concat "("
8397                           (mail-header-date gnus-current-headers) ")"))))
8398             (gnus-run-hooks 'gnus-ps-print-hook)
8399             (save-excursion
8400               (if window-system
8401                   (ps-spool-buffer-with-faces)
8402                 (ps-spool-buffer)))))
8403       (kill-buffer buffer))))
8404
8405 (defun gnus-summary-show-article (&optional arg)
8406   "Force redisplaying of the current article.
8407 If ARG (the prefix) is a number, show the article with the charset
8408 defined in `gnus-summary-show-article-charset-alist', or the charset
8409 input.
8410 If ARG (the prefix) is non-nil and not a number, show the raw article
8411 without any article massaging functions being run.  Normally, the key strokes
8412 are `C-u g'."
8413   (interactive "P")
8414   (cond
8415    ((numberp arg)
8416     (gnus-summary-show-article t)
8417     (let ((gnus-newsgroup-charset
8418            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8419                (mm-read-coding-system
8420                 "View as charset: " ;; actually it is coding system.
8421                 (save-excursion
8422                   (set-buffer gnus-article-buffer)
8423                   (mm-detect-coding-region (point) (point-max))))))
8424           (gnus-newsgroup-ignored-charsets 'gnus-all))
8425       (gnus-summary-select-article nil 'force)
8426       (let ((deps gnus-newsgroup-dependencies)
8427             head header lines)
8428         (save-excursion
8429           (set-buffer gnus-original-article-buffer)
8430           (save-restriction
8431             (message-narrow-to-head)
8432             (setq head (buffer-string))
8433             (goto-char (point-min))
8434             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8435               (goto-char (point-max))
8436               (widen)
8437               (setq lines (1- (count-lines (point) (point-max))))))
8438           (with-temp-buffer
8439             (insert (format "211 %d Article retrieved.\n"
8440                             (cdr gnus-article-current)))
8441             (insert head)
8442             (if lines (insert (format "Lines: %d\n" lines)))
8443             (insert ".\n")
8444             (let ((nntp-server-buffer (current-buffer)))
8445               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8446         (gnus-data-set-header
8447          (gnus-data-find (cdr gnus-article-current))
8448          header)
8449         (gnus-summary-update-article-line
8450          (cdr gnus-article-current) header)
8451         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8452           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8453    ((not arg)
8454     ;; Select the article the normal way.
8455     (gnus-summary-select-article nil 'force))
8456    (t
8457     ;; We have to require this here to make sure that the following
8458     ;; dynamic binding isn't shadowed by autoloading.
8459     (require 'gnus-async)
8460     (require 'gnus-art)
8461     ;; Bind the article treatment functions to nil.
8462     (let ((gnus-have-all-headers t)
8463           gnus-article-prepare-hook
8464           gnus-article-decode-hook
8465           gnus-display-mime-function
8466           gnus-break-pages)
8467       ;; Destroy any MIME parts.
8468       (when (gnus-buffer-live-p gnus-article-buffer)
8469         (save-excursion
8470           (set-buffer gnus-article-buffer)
8471           (mm-destroy-parts gnus-article-mime-handles)
8472           ;; Set it to nil for safety reason.
8473           (setq gnus-article-mime-handle-alist nil)
8474           (setq gnus-article-mime-handles nil)))
8475       (gnus-summary-select-article nil 'force))))
8476   (gnus-summary-goto-subject gnus-current-article)
8477   (gnus-summary-position-point))
8478
8479 (defun gnus-summary-show-raw-article ()
8480   "Show the raw article without any article massaging functions being run."
8481   (interactive)
8482   (gnus-summary-show-article t))
8483
8484 (defun gnus-summary-verbose-headers (&optional arg)
8485   "Toggle permanent full header display.
8486 If ARG is a positive number, turn header display on.
8487 If ARG is a negative number, turn header display off."
8488   (interactive "P")
8489   (setq gnus-show-all-headers
8490         (cond ((or (not (numberp arg))
8491                    (zerop arg))
8492                (not gnus-show-all-headers))
8493               ((natnump arg)
8494                t)))
8495   (gnus-summary-show-article))
8496
8497 (defun gnus-summary-toggle-header (&optional arg)
8498   "Show the headers if they are hidden, or hide them if they are shown.
8499 If ARG is a positive number, show the entire header.
8500 If ARG is a negative number, hide the unwanted header lines."
8501   (interactive "P")
8502   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8503                      (get-buffer-window gnus-article-buffer t))))
8504     (with-current-buffer gnus-article-buffer
8505       (widen)
8506       (article-narrow-to-head)
8507       (let* ((buffer-read-only nil)
8508              (inhibit-point-motion-hooks t)
8509              (hidden (if (numberp arg)
8510                          (>= arg 0)
8511                        (gnus-article-hidden-text-p 'headers)))
8512              s e)
8513         (delete-region (point-min) (point-max))
8514         (with-current-buffer gnus-original-article-buffer
8515           (goto-char (setq s (point-min)))
8516           (setq e (if (search-forward "\n\n" nil t)
8517                       (1- (point))
8518                     (point-max))))
8519         (insert-buffer-substring gnus-original-article-buffer s e)
8520         (article-decode-encoded-words)
8521         (if hidden
8522             (let ((gnus-treat-hide-headers nil)
8523                   (gnus-treat-hide-boring-headers nil))
8524               (gnus-delete-wash-type 'headers)
8525               (gnus-treat-article 'head))
8526           (gnus-treat-article 'head))
8527         (widen)
8528         (if window
8529             (set-window-start window (goto-char (point-min))))
8530         (setq gnus-page-broken
8531               (when gnus-break-pages
8532                 (gnus-narrow-to-page)
8533                 t))
8534         (gnus-set-mode-line 'article)))))
8535
8536 (defun gnus-summary-show-all-headers ()
8537   "Make all header lines visible."
8538   (interactive)
8539   (gnus-summary-toggle-header 1))
8540
8541 (defun gnus-summary-caesar-message (&optional arg)
8542   "Caesar rotate the current article by 13.
8543 The numerical prefix specifies how many places to rotate each letter
8544 forward."
8545   (interactive "P")
8546   (gnus-summary-select-article)
8547   (let ((mail-header-separator ""))
8548     (gnus-eval-in-buffer-window gnus-article-buffer
8549       (save-restriction
8550         (widen)
8551         (let ((start (window-start))
8552               buffer-read-only)
8553           (message-caesar-buffer-body arg)
8554           (set-window-start (get-buffer-window (current-buffer)) start))))))
8555
8556 (autoload 'unmorse-region "morse"
8557   "Convert morse coded text in region to ordinary ASCII text."
8558   t)
8559
8560 (defun gnus-summary-morse-message (&optional arg)
8561   "Morse decode the current article."
8562   (interactive "P")
8563   (gnus-summary-select-article)
8564   (let ((mail-header-separator ""))
8565     (gnus-eval-in-buffer-window gnus-article-buffer
8566       (save-excursion
8567         (save-restriction
8568           (widen)
8569           (let ((pos (window-start))
8570                 buffer-read-only)
8571             (goto-char (point-min))
8572             (when (message-goto-body)
8573               (gnus-narrow-to-body))
8574             (goto-char (point-min))
8575             (while (re-search-forward "·" (point-max) t)
8576               (replace-match "."))
8577             (unmorse-region (point-min) (point-max))
8578             (widen)
8579             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8580
8581 (defun gnus-summary-stop-page-breaking ()
8582   "Stop page breaking in the current article."
8583   (interactive)
8584   (gnus-summary-select-article)
8585   (gnus-eval-in-buffer-window gnus-article-buffer
8586     (widen)
8587     (when (gnus-visual-p 'page-marker)
8588       (let ((buffer-read-only nil))
8589         (gnus-remove-text-with-property 'gnus-prev)
8590         (gnus-remove-text-with-property 'gnus-next))
8591       (setq gnus-page-broken nil))))
8592
8593 (defun gnus-summary-move-article (&optional n to-newsgroup
8594                                             select-method action)
8595   "Move the current article to a different newsgroup.
8596 If N is a positive number, move the N next articles.
8597 If N is a negative number, move the N previous articles.
8598 If N is nil and any articles have been marked with the process mark,
8599 move those articles instead.
8600 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8601 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8602 re-spool using this method.
8603
8604 When called interactively with TO-NEWSGROUP being nil, the value of
8605 the variable `gnus-move-split-methods' is used for finding a default
8606 for the target newsgroup.
8607
8608 For this function to work, both the current newsgroup and the
8609 newsgroup that you want to move to have to support the `request-move'
8610 and `request-accept' functions.
8611
8612 ACTION can be either `move' (the default), `crosspost' or `copy'."
8613   (interactive "P")
8614   (unless action
8615     (setq action 'move))
8616   ;; Check whether the source group supports the required functions.
8617   (cond ((and (eq action 'move)
8618               (not (gnus-check-backend-function
8619                     'request-move-article gnus-newsgroup-name)))
8620          (error "The current group does not support article moving"))
8621         ((and (eq action 'crosspost)
8622               (not (gnus-check-backend-function
8623                     'request-replace-article gnus-newsgroup-name)))
8624          (error "The current group does not support article editing")))
8625   (let ((articles (gnus-summary-work-articles n))
8626         (prefix (if (gnus-check-backend-function
8627                      'request-move-article gnus-newsgroup-name)
8628                     (gnus-group-real-prefix gnus-newsgroup-name)
8629                   ""))
8630         (names '((move "Move" "Moving")
8631                  (copy "Copy" "Copying")
8632                  (crosspost "Crosspost" "Crossposting")))
8633         (copy-buf (save-excursion
8634                     (nnheader-set-temp-buffer " *copy article*")))
8635         art-group to-method new-xref article to-groups)
8636     (unless (assq action names)
8637       (error "Unknown action %s" action))
8638     ;; Read the newsgroup name.
8639     (when (and (not to-newsgroup)
8640                (not select-method))
8641       (if (and gnus-move-split-methods
8642                (not
8643                 (and (memq gnus-current-article articles)
8644                      (gnus-buffer-live-p gnus-original-article-buffer))))
8645           ;; When `gnus-move-split-methods' is non-nil, we have to
8646           ;; select an article to give `gnus-read-move-group-name' an
8647           ;; opportunity to suggest an appropriate default.  However,
8648           ;; we needn't render or mark the article.
8649           (let ((gnus-display-mime-function nil)
8650                 (gnus-article-prepare-hook nil)
8651                 (gnus-mark-article-hook nil))
8652             (gnus-summary-select-article nil nil nil (car articles))))
8653       (setq to-newsgroup
8654             (gnus-read-move-group-name
8655              (cadr (assq action names))
8656              (symbol-value (intern (format "gnus-current-%s-group" action)))
8657              articles prefix))
8658       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8659     (setq to-method (or select-method
8660                         (gnus-server-to-method
8661                          (gnus-group-method to-newsgroup))))
8662     ;; Check the method we are to move this article to...
8663     (unless (gnus-check-backend-function
8664              'request-accept-article (car to-method))
8665       (error "%s does not support article copying" (car to-method)))
8666     (unless (gnus-check-server to-method)
8667       (error "Can't open server %s" (car to-method)))
8668     (gnus-message 6 "%s to %s: %s..."
8669                   (caddr (assq action names))
8670                   (or (car select-method) to-newsgroup) articles)
8671     (while articles
8672       (setq article (pop articles))
8673       (setq
8674        art-group
8675        (cond
8676         ;; Move the article.
8677         ((eq action 'move)
8678          ;; Remove this article from future suppression.
8679          (gnus-dup-unsuppress-article article)
8680          (gnus-request-move-article
8681           article                       ; Article to move
8682           gnus-newsgroup-name           ; From newsgroup
8683           (nth 1 (gnus-find-method-for-group
8684                   gnus-newsgroup-name)) ; Server
8685           (list 'gnus-request-accept-article
8686                 to-newsgroup (list 'quote select-method)
8687                 (not articles) t)       ; Accept form
8688           (not articles)))              ; Only save nov last time
8689         ;; Copy the article.
8690         ((eq action 'copy)
8691          (save-excursion
8692            (set-buffer copy-buf)
8693            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8694              (gnus-request-accept-article
8695               to-newsgroup select-method (not articles) t))))
8696         ;; Crosspost the article.
8697         ((eq action 'crosspost)
8698          (let ((xref (message-tokenize-header
8699                       (mail-header-xref (gnus-summary-article-header article))
8700                       " ")))
8701            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8702                                   ":" (number-to-string article)))
8703            (unless xref
8704              (setq xref (list (system-name))))
8705            (setq new-xref
8706                  (concat
8707                   (mapconcat 'identity
8708                              (delete "Xref:" (delete new-xref xref))
8709                              " ")
8710                   " " new-xref))
8711            (save-excursion
8712              (set-buffer copy-buf)
8713              ;; First put the article in the destination group.
8714              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8715              (when (consp (setq art-group
8716                                 (gnus-request-accept-article
8717                                  to-newsgroup select-method (not articles))))
8718                (setq new-xref (concat new-xref " " (car art-group)
8719                                       ":"
8720                                       (number-to-string (cdr art-group))))
8721                ;; Now we have the new Xrefs header, so we insert
8722                ;; it and replace the new article.
8723                (nnheader-replace-header "Xref" new-xref)
8724                (gnus-request-replace-article
8725                 (cdr art-group) to-newsgroup (current-buffer))
8726                art-group))))))
8727       (cond
8728        ((not art-group)
8729         (gnus-message 1 "Couldn't %s article %s: %s"
8730                       (cadr (assq action names)) article
8731                       (nnheader-get-report (car to-method))))
8732        ((eq art-group 'junk)
8733         (when (eq action 'move)
8734           (gnus-summary-mark-article article gnus-canceled-mark)
8735           (gnus-message 4 "Deleted article %s" article)))
8736        (t
8737         (let* ((pto-group (gnus-group-prefixed-name
8738                            (car art-group) to-method))
8739                (entry
8740                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8741                (info (nth 2 entry))
8742                (to-group (gnus-info-group info))
8743                to-marks)
8744           ;; Update the group that has been moved to.
8745           (when (and info
8746                      (memq action '(move copy)))
8747             (unless (member to-group to-groups)
8748               (push to-group to-groups))
8749
8750             (unless (memq article gnus-newsgroup-unreads)
8751               (push 'read to-marks)
8752               (gnus-info-set-read
8753                info (gnus-add-to-range (gnus-info-read info)
8754                                        (list (cdr art-group)))))
8755
8756             ;; See whether the article is to be put in the cache.
8757             (let ((marks gnus-article-mark-lists)
8758                   (to-article (cdr art-group)))
8759
8760               ;; Enter the article into the cache in the new group,
8761               ;; if that is required.
8762               (when gnus-use-cache
8763                 (gnus-cache-possibly-enter-article
8764                  to-group to-article
8765                  (memq article gnus-newsgroup-marked)
8766                  (memq article gnus-newsgroup-dormant)
8767                  (memq article gnus-newsgroup-unreads)))
8768
8769               (when gnus-preserve-marks
8770                 ;; Copy any marks over to the new group.
8771                 (when (and (equal to-group gnus-newsgroup-name)
8772                            (not (memq article gnus-newsgroup-unreads)))
8773                   ;; Mark this article as read in this group.
8774                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8775                   (setcdr (gnus-active to-group) to-article)
8776                   (setcdr gnus-newsgroup-active to-article))
8777
8778                 (while marks
8779                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8780                     (when (memq article (symbol-value
8781                                          (intern (format "gnus-newsgroup-%s"
8782                                                          (caar marks)))))
8783                       (push (cdar marks) to-marks)
8784                       ;; If the other group is the same as this group,
8785                       ;; then we have to add the mark to the list.
8786                       (when (equal to-group gnus-newsgroup-name)
8787                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8788                              (cons to-article
8789                                    (symbol-value
8790                                     (intern (format "gnus-newsgroup-%s"
8791                                                     (caar marks)))))))
8792                       ;; Copy the marks to other group.
8793                       (gnus-add-marked-articles
8794                        to-group (cdar marks) (list to-article) info)))
8795                   (setq marks (cdr marks)))
8796
8797                 (gnus-request-set-mark
8798                  to-group (list (list (list to-article) 'add to-marks))))
8799
8800               (gnus-dribble-enter
8801                (concat "(gnus-group-set-info '"
8802                        (gnus-prin1-to-string (gnus-get-info to-group))
8803                        ")"))))
8804
8805           ;; Update the Xref header in this article to point to
8806           ;; the new crossposted article we have just created.
8807           (when (eq action 'crosspost)
8808             (save-excursion
8809               (set-buffer copy-buf)
8810               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8811               (nnheader-replace-header "Xref" new-xref)
8812               (gnus-request-replace-article
8813                article gnus-newsgroup-name (current-buffer)))))
8814
8815         ;;;!!!Why is this necessary?
8816         (set-buffer gnus-summary-buffer)
8817
8818         (gnus-summary-goto-subject article)
8819         (when (eq action 'move)
8820           (gnus-summary-mark-article article gnus-canceled-mark))))
8821       (gnus-summary-remove-process-mark article))
8822     ;; Re-activate all groups that have been moved to.
8823     (save-excursion
8824       (set-buffer gnus-group-buffer)
8825       (let ((gnus-group-marked to-groups))
8826         (gnus-group-get-new-news-this-group nil t)))
8827
8828     (gnus-kill-buffer copy-buf)
8829     (gnus-summary-position-point)
8830     (gnus-set-mode-line 'summary)))
8831
8832 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8833   "Move the current article to a different newsgroup.
8834 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8835 When called interactively, if TO-NEWSGROUP is nil, use the value of
8836 the variable `gnus-move-split-methods' for finding a default target
8837 newsgroup.
8838 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8839 re-spool using this method."
8840   (interactive "P")
8841   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8842
8843 (defun gnus-summary-crosspost-article (&optional n)
8844   "Crosspost the current article to some other group."
8845   (interactive "P")
8846   (gnus-summary-move-article n nil nil 'crosspost))
8847
8848 (defcustom gnus-summary-respool-default-method nil
8849   "Default method type for respooling an article.
8850 If nil, use to the current newsgroup method."
8851   :type 'symbol
8852   :group 'gnus-summary-mail)
8853
8854 (defcustom gnus-summary-display-while-building nil
8855   "If not-nil, show and update the summary buffer as it's being built.
8856 If the value is t, update the buffer after every line is inserted.  If
8857 the value is an integer (N), update the display every N lines."
8858   :group 'gnus-thread
8859   :type '(choice (const :tag "off" nil)
8860                  number
8861                  (const :tag "frequently" t)))
8862
8863 (defun gnus-summary-respool-article (&optional n method)
8864   "Respool the current article.
8865 The article will be squeezed through the mail spooling process again,
8866 which means that it will be put in some mail newsgroup or other
8867 depending on `nnmail-split-methods'.
8868 If N is a positive number, respool the N next articles.
8869 If N is a negative number, respool the N previous articles.
8870 If N is nil and any articles have been marked with the process mark,
8871 respool those articles instead.
8872
8873 Respooling can be done both from mail groups and \"real\" newsgroups.
8874 In the former case, the articles in question will be moved from the
8875 current group into whatever groups they are destined to.  In the
8876 latter case, they will be copied into the relevant groups."
8877   (interactive
8878    (list current-prefix-arg
8879          (let* ((methods (gnus-methods-using 'respool))
8880                 (methname
8881                  (symbol-name (or gnus-summary-respool-default-method
8882                                   (car (gnus-find-method-for-group
8883                                         gnus-newsgroup-name)))))
8884                 (method
8885                  (gnus-completing-read-with-default
8886                   methname "What backend do you want to use when respooling?"
8887                   methods nil t nil 'gnus-mail-method-history))
8888                 ms)
8889            (cond
8890             ((zerop (length (setq ms (gnus-servers-using-backend
8891                                       (intern method)))))
8892              (list (intern method) ""))
8893             ((= 1 (length ms))
8894              (car ms))
8895             (t
8896              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8897                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8898                            ms-alist))))))))
8899   (unless method
8900     (error "No method given for respooling"))
8901   (if (assoc (symbol-name
8902               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8903              (gnus-methods-using 'respool))
8904       (gnus-summary-move-article n nil method)
8905     (gnus-summary-copy-article n nil method)))
8906
8907 (defun gnus-summary-import-article (file &optional edit)
8908   "Import an arbitrary file into a mail newsgroup."
8909   (interactive "fImport file: \nP")
8910   (let ((group gnus-newsgroup-name)
8911         (now (current-time))
8912         atts lines group-art)
8913     (unless (gnus-check-backend-function 'request-accept-article group)
8914       (error "%s does not support article importing" group))
8915     (or (file-readable-p file)
8916         (not (file-regular-p file))
8917         (error "Can't read %s" file))
8918     (save-excursion
8919       (set-buffer (gnus-get-buffer-create " *import file*"))
8920       (erase-buffer)
8921       (nnheader-insert-file-contents file)
8922       (goto-char (point-min))
8923       (if (nnheader-article-p)
8924           (save-restriction
8925             (goto-char (point-min))
8926             (search-forward "\n\n" nil t)
8927             (narrow-to-region (point-min) (1- (point)))
8928             (goto-char (point-min))
8929             (unless (re-search-forward "^date:" nil t)
8930               (goto-char (point-max))
8931               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8932        ;; This doesn't look like an article, so we fudge some headers.
8933         (setq atts (file-attributes file)
8934               lines (count-lines (point-min) (point-max)))
8935         (insert "From: " (read-string "From: ") "\n"
8936                 "Subject: " (read-string "Subject: ") "\n"
8937                 "Date: " (message-make-date (nth 5 atts)) "\n"
8938                 "Message-ID: " (message-make-message-id) "\n"
8939                 "Lines: " (int-to-string lines) "\n"
8940                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8941       (setq group-art (gnus-request-accept-article group nil t))
8942       (kill-buffer (current-buffer)))
8943     (setq gnus-newsgroup-active (gnus-activate-group group))
8944     (forward-line 1)
8945     (gnus-summary-goto-article (cdr group-art) nil t)
8946     (when edit
8947       (gnus-summary-edit-article))))
8948
8949 (defun gnus-summary-create-article ()
8950   "Create an article in a mail newsgroup."
8951   (interactive)
8952   (let ((group gnus-newsgroup-name)
8953         (now (current-time))
8954         group-art)
8955     (unless (gnus-check-backend-function 'request-accept-article group)
8956       (error "%s does not support article importing" group))
8957     (save-excursion
8958       (set-buffer (gnus-get-buffer-create " *import file*"))
8959       (erase-buffer)
8960       (goto-char (point-min))
8961       ;; This doesn't look like an article, so we fudge some headers.
8962       (insert "From: " (read-string "From: ") "\n"
8963               "Subject: " (read-string "Subject: ") "\n"
8964               "Date: " (message-make-date now) "\n"
8965               "Message-ID: " (message-make-message-id) "\n")
8966       (setq group-art (gnus-request-accept-article group nil t))
8967       (kill-buffer (current-buffer)))
8968     (setq gnus-newsgroup-active (gnus-activate-group group))
8969     (forward-line 1)
8970     (gnus-summary-goto-article (cdr group-art) nil t)
8971     (gnus-summary-edit-article)))
8972
8973 (defun gnus-summary-article-posted-p ()
8974   "Say whether the current (mail) article is available from news as well.
8975 This will be the case if the article has both been mailed and posted."
8976   (interactive)
8977   (let ((id (mail-header-references (gnus-summary-article-header)))
8978         (gnus-override-method (car (gnus-refer-article-methods))))
8979     (if (gnus-request-head id "")
8980         (gnus-message 2 "The current message was found on %s"
8981                       gnus-override-method)
8982       (gnus-message 2 "The current message couldn't be found on %s"
8983                     gnus-override-method)
8984       nil)))
8985
8986 (defun gnus-summary-expire-articles (&optional now)
8987   "Expire all articles that are marked as expirable in the current group."
8988   (interactive)
8989   (when (gnus-check-backend-function
8990          'request-expire-articles gnus-newsgroup-name)
8991     ;; This backend supports expiry.
8992     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8993            (expirable (if total
8994                           (progn
8995                             ;; We need to update the info for
8996                             ;; this group for `gnus-list-of-read-articles'
8997                             ;; to give us the right answer.
8998                             (gnus-run-hooks 'gnus-exit-group-hook)
8999                             (gnus-summary-update-info)
9000                             (gnus-list-of-read-articles gnus-newsgroup-name))
9001                         (setq gnus-newsgroup-expirable
9002                               (sort gnus-newsgroup-expirable '<))))
9003            (expiry-wait (if now 'immediate
9004                           (gnus-group-find-parameter
9005                            gnus-newsgroup-name 'expiry-wait)))
9006            (nnmail-expiry-target
9007             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9008                 nnmail-expiry-target))
9009            es)
9010       (when expirable
9011         ;; There are expirable articles in this group, so we run them
9012         ;; through the expiry process.
9013         (gnus-message 6 "Expiring articles...")
9014         (unless (gnus-check-group gnus-newsgroup-name)
9015           (error "Can't open server for %s" gnus-newsgroup-name))
9016         ;; The list of articles that weren't expired is returned.
9017         (save-excursion
9018           (if expiry-wait
9019               (let ((nnmail-expiry-wait-function nil)
9020                     (nnmail-expiry-wait expiry-wait))
9021                 (setq es (gnus-request-expire-articles
9022                           expirable gnus-newsgroup-name)))
9023             (setq es (gnus-request-expire-articles
9024                       expirable gnus-newsgroup-name)))
9025           (unless total
9026             (setq gnus-newsgroup-expirable es))
9027           ;; We go through the old list of expirable, and mark all
9028           ;; really expired articles as nonexistent.
9029           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9030             (let ((gnus-use-cache nil))
9031               (dolist (article expirable)
9032                 (when (and (not (memq article es))
9033                            (gnus-data-find article))
9034                   (gnus-summary-mark-article article gnus-canceled-mark))))))
9035         (gnus-message 6 "Expiring articles...done")))))
9036
9037 (defun gnus-summary-expire-articles-now ()
9038   "Expunge all expirable articles in the current group.
9039 This means that *all* articles that are marked as expirable will be
9040 deleted forever, right now."
9041   (interactive)
9042   (or gnus-expert-user
9043       (gnus-yes-or-no-p
9044        "Are you really, really, really sure you want to delete all these messages? ")
9045       (error "Phew!"))
9046   (gnus-summary-expire-articles t))
9047
9048 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9049 (defun gnus-summary-delete-article (&optional n)
9050   "Delete the N next (mail) articles.
9051 This command actually deletes articles.  This is not a marking
9052 command.  The article will disappear forever from your life, never to
9053 return.
9054 If N is negative, delete backwards.
9055 If N is nil and articles have been marked with the process mark,
9056 delete these instead."
9057   (interactive "P")
9058   (unless (gnus-check-backend-function 'request-expire-articles
9059                                        gnus-newsgroup-name)
9060     (error "The current newsgroup does not support article deletion"))
9061   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9062     (error "Couldn't open server"))
9063   ;; Compute the list of articles to delete.
9064   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9065         (nnmail-expiry-target 'delete)
9066         not-deleted)
9067     (if (and gnus-novice-user
9068              (not (gnus-yes-or-no-p
9069                    (format "Do you really want to delete %s forever? "
9070                            (if (> (length articles) 1)
9071                                (format "these %s articles" (length articles))
9072                              "this article")))))
9073         ()
9074       ;; Delete the articles.
9075       (setq not-deleted (gnus-request-expire-articles
9076                          articles gnus-newsgroup-name 'force))
9077       (while articles
9078         (gnus-summary-remove-process-mark (car articles))
9079         ;; The backend might not have been able to delete the article
9080         ;; after all.
9081         (unless (memq (car articles) not-deleted)
9082           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9083         (setq articles (cdr articles)))
9084       (when not-deleted
9085         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9086     (gnus-summary-position-point)
9087     (gnus-set-mode-line 'summary)
9088     not-deleted))
9089
9090 (defun gnus-summary-edit-article (&optional arg)
9091   "Edit the current article.
9092 This will have permanent effect only in mail groups.
9093 If ARG is nil, edit the decoded articles.
9094 If ARG is 1, edit the raw articles.
9095 If ARG is 2, edit the raw articles even in read-only groups.
9096 If ARG is 3, edit the articles with the current handles.
9097 Otherwise, allow editing of articles even in read-only
9098 groups."
9099   (interactive "P")
9100   (let (force raw current-handles)
9101     (cond
9102      ((null arg))
9103      ((eq arg 1)
9104       (setq raw t))
9105      ((eq arg 2)
9106       (setq raw t
9107             force t))
9108      ((eq arg 3)
9109       (setq current-handles
9110             (and (gnus-buffer-live-p gnus-article-buffer)
9111                  (with-current-buffer gnus-article-buffer
9112                    (prog1
9113                        gnus-article-mime-handles
9114                      (setq gnus-article-mime-handles nil))))))
9115      (t
9116       (setq force t)))
9117     (when (and raw (not force)
9118                (member gnus-newsgroup-name '("nndraft:delayed"
9119                                              "nndraft:drafts"
9120                                              "nndraft:queue")))
9121       (error "Can't edit the raw article in group %s"
9122              gnus-newsgroup-name))
9123     (save-excursion
9124       (set-buffer gnus-summary-buffer)
9125       (let ((mail-parse-charset gnus-newsgroup-charset)
9126             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9127         (gnus-set-global-variables)
9128         (when (and (not force)
9129                    (gnus-group-read-only-p))
9130           (error "The current newsgroup does not support article editing"))
9131         (gnus-summary-show-article t)
9132         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9133           (with-current-buffer gnus-article-buffer
9134             (mm-enable-multibyte)))
9135         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9136             (setq raw t))
9137         (gnus-article-edit-article
9138          (if raw 'ignore
9139            `(lambda ()
9140               (let ((mbl mml-buffer-list))
9141                 (setq mml-buffer-list nil)
9142                 (mime-to-mml ,'current-handles)
9143                 (let ((mbl1 mml-buffer-list))
9144                   (setq mml-buffer-list mbl)
9145                   (set (make-local-variable 'mml-buffer-list) mbl1))
9146                 (make-local-hook 'kill-buffer-hook)
9147                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9148          `(lambda (no-highlight)
9149             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9150                   (message-options message-options)
9151                   (message-options-set-recipient)
9152                   (mail-parse-ignored-charsets
9153                    ',gnus-newsgroup-ignored-charsets))
9154               ,(if (not raw) '(progn
9155                                 (mml-to-mime)
9156                                 (mml-destroy-buffers)
9157                                 (remove-hook 'kill-buffer-hook
9158                                              'mml-destroy-buffers t)
9159                                 (kill-local-variable 'mml-buffer-list)))
9160               (gnus-summary-edit-article-done
9161                ,(or (mail-header-references gnus-current-headers) "")
9162                ,(gnus-group-read-only-p)
9163                ,gnus-summary-buffer no-highlight))))))))
9164
9165 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9166
9167 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9168                                                  no-highlight)
9169   "Make edits to the current article permanent."
9170   (interactive)
9171   (save-excursion
9172    ;; The buffer restriction contains the entire article if it exists.
9173     (when (article-goto-body)
9174       (let ((lines (count-lines (point) (point-max)))
9175             (length (- (point-max) (point)))
9176             (case-fold-search t)
9177             (body (copy-marker (point))))
9178         (goto-char (point-min))
9179         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9180           (delete-region (match-beginning 1) (match-end 1))
9181           (insert (number-to-string length)))
9182         (goto-char (point-min))
9183         (when (re-search-forward
9184                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9185           (delete-region (match-beginning 1) (match-end 1))
9186           (insert (number-to-string length)))
9187         (goto-char (point-min))
9188         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9189           (delete-region (match-beginning 1) (match-end 1))
9190           (insert (number-to-string lines))))))
9191   ;; Replace the article.
9192   (let ((buf (current-buffer)))
9193     (with-temp-buffer
9194       (insert-buffer-substring buf)
9195
9196       (if (and (not read-only)
9197                (not (gnus-request-replace-article
9198                      (cdr gnus-article-current) (car gnus-article-current)
9199                      (current-buffer) t)))
9200           (error "Couldn't replace article")
9201         ;; Update the summary buffer.
9202         (if (and references
9203                  (equal (message-tokenize-header references " ")
9204                         (message-tokenize-header
9205                          (or (message-fetch-field "references") "") " ")))
9206             ;; We only have to update this line.
9207             (save-excursion
9208               (save-restriction
9209                 (message-narrow-to-head)
9210                 (let ((head (buffer-string))
9211                       header)
9212                   (with-temp-buffer
9213                     (insert (format "211 %d Article retrieved.\n"
9214                                     (cdr gnus-article-current)))
9215                     (insert head)
9216                     (insert ".\n")
9217                     (let ((nntp-server-buffer (current-buffer)))
9218                       (setq header (car (gnus-get-newsgroup-headers
9219                                          nil t))))
9220                     (save-excursion
9221                       (set-buffer gnus-summary-buffer)
9222                       (gnus-data-set-header
9223                        (gnus-data-find (cdr gnus-article-current))
9224                        header)
9225                       (gnus-summary-update-article-line
9226                        (cdr gnus-article-current) header)
9227                       (if (gnus-summary-goto-subject
9228                            (cdr gnus-article-current) nil t)
9229                           (gnus-summary-update-secondary-mark
9230                            (cdr gnus-article-current))))))))
9231           ;; Update threads.
9232           (set-buffer (or buffer gnus-summary-buffer))
9233           (gnus-summary-update-article (cdr gnus-article-current))
9234           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9235               (gnus-summary-update-secondary-mark
9236                (cdr gnus-article-current))))
9237         ;; Prettify the article buffer again.
9238         (unless no-highlight
9239           (save-excursion
9240             (set-buffer gnus-article-buffer)
9241             ;;;!!! Fix this -- article should be rehighlighted.
9242             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9243             (set-buffer gnus-original-article-buffer)
9244             (gnus-request-article
9245              (cdr gnus-article-current)
9246              (car gnus-article-current) (current-buffer))))
9247         ;; Prettify the summary buffer line.
9248         (when (gnus-visual-p 'summary-highlight 'highlight)
9249           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9250
9251 (defun gnus-summary-edit-wash (key)
9252   "Perform editing command KEY in the article buffer."
9253   (interactive
9254    (list
9255     (progn
9256       (message "%s" (concat (this-command-keys) "- "))
9257       (read-char))))
9258   (message "")
9259   (gnus-summary-edit-article)
9260   (execute-kbd-macro (concat (this-command-keys) key))
9261   (gnus-article-edit-done))
9262
9263 ;;; Respooling
9264
9265 (defun gnus-summary-respool-query (&optional silent trace)
9266   "Query where the respool algorithm would put this article."
9267   (interactive)
9268   (let (gnus-mark-article-hook)
9269     (gnus-summary-select-article)
9270     (save-excursion
9271       (set-buffer gnus-original-article-buffer)
9272       (save-restriction
9273         (message-narrow-to-head)
9274         (let ((groups (nnmail-article-group 'identity trace)))
9275           (unless silent
9276             (if groups
9277                 (message "This message would go to %s"
9278                          (mapconcat 'car groups ", "))
9279               (message "This message would go to no groups"))
9280             groups))))))
9281
9282 (defun gnus-summary-respool-trace ()
9283   "Trace where the respool algorithm would put this article.
9284 Display a buffer showing all fancy splitting patterns which matched."
9285   (interactive)
9286   (gnus-summary-respool-query nil t))
9287
9288 ;; Summary marking commands.
9289
9290 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9291   "Mark articles which has the same subject as read, and then select the next.
9292 If UNMARK is positive, remove any kind of mark.
9293 If UNMARK is negative, tick articles."
9294   (interactive "P")
9295   (when unmark
9296     (setq unmark (prefix-numeric-value unmark)))
9297   (let ((count
9298          (gnus-summary-mark-same-subject
9299           (gnus-summary-article-subject) unmark)))
9300     ;; Select next unread article.  If auto-select-same mode, should
9301     ;; select the first unread article.
9302     (gnus-summary-next-article t (and gnus-auto-select-same
9303                                       (gnus-summary-article-subject)))
9304     (gnus-message 7 "%d article%s marked as %s"
9305                   count (if (= count 1) " is" "s are")
9306                   (if unmark "unread" "read"))))
9307
9308 (defun gnus-summary-kill-same-subject (&optional unmark)
9309   "Mark articles which has the same subject as read.
9310 If UNMARK is positive, remove any kind of mark.
9311 If UNMARK is negative, tick articles."
9312   (interactive "P")
9313   (when unmark
9314     (setq unmark (prefix-numeric-value unmark)))
9315   (let ((count
9316          (gnus-summary-mark-same-subject
9317           (gnus-summary-article-subject) unmark)))
9318     ;; If marked as read, go to next unread subject.
9319     (when (null unmark)
9320       ;; Go to next unread subject.
9321       (gnus-summary-next-subject 1 t))
9322     (gnus-message 7 "%d articles are marked as %s"
9323                   count (if unmark "unread" "read"))))
9324
9325 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9326   "Mark articles with same SUBJECT as read, and return marked number.
9327 If optional argument UNMARK is positive, remove any kinds of marks.
9328 If optional argument UNMARK is negative, mark articles as unread instead."
9329   (let ((count 1))
9330     (save-excursion
9331       (cond
9332        ((null unmark)                   ; Mark as read.
9333         (while (and
9334                 (progn
9335                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9336                   (gnus-summary-show-thread) t)
9337                 (gnus-summary-find-subject subject))
9338           (setq count (1+ count))))
9339        ((> unmark 0)                    ; Tick.
9340         (while (and
9341                 (progn
9342                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9343                   (gnus-summary-show-thread) t)
9344                 (gnus-summary-find-subject subject))
9345           (setq count (1+ count))))
9346        (t                               ; Mark as unread.
9347         (while (and
9348                 (progn
9349                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9350                   (gnus-summary-show-thread) t)
9351                 (gnus-summary-find-subject subject))
9352           (setq count (1+ count)))))
9353       (gnus-set-mode-line 'summary)
9354       ;; Return the number of marked articles.
9355       count)))
9356
9357 (defun gnus-summary-mark-as-processable (n &optional unmark)
9358   "Set the process mark on the next N articles.
9359 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9360 the process mark instead.  The difference between N and the actual
9361 number of articles marked is returned."
9362   (interactive "P")
9363   (if (and (null n) (gnus-region-active-p))
9364       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9365     (setq n (prefix-numeric-value n))
9366     (let ((backward (< n 0))
9367           (n (abs n)))
9368       (while (and
9369               (> n 0)
9370               (if unmark
9371                   (gnus-summary-remove-process-mark
9372                    (gnus-summary-article-number))
9373                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9374               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9375         (setq n (1- n)))
9376       (when (/= 0 n)
9377         (gnus-message 7 "No more articles"))
9378       (gnus-summary-recenter)
9379       (gnus-summary-position-point)
9380       n)))
9381
9382 (defun gnus-summary-unmark-as-processable (n)
9383   "Remove the process mark from the next N articles.
9384 If N is negative, unmark backward instead.  The difference between N and
9385 the actual number of articles unmarked is returned."
9386   (interactive "P")
9387   (gnus-summary-mark-as-processable n t))
9388
9389 (defun gnus-summary-unmark-all-processable ()
9390   "Remove the process mark from all articles."
9391   (interactive)
9392   (save-excursion
9393     (while gnus-newsgroup-processable
9394       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9395   (gnus-summary-position-point))
9396
9397 (defun gnus-summary-add-mark (article type)
9398   "Mark ARTICLE with a mark of TYPE."
9399   (let ((vtype (car (assq type gnus-article-mark-lists)))
9400         var)
9401     (if (not vtype)
9402         (error "No such mark type: %s" type)
9403       (setq var (intern (format "gnus-newsgroup-%s" type)))
9404       (set var (cons article (symbol-value var)))
9405       (if (memq type '(processable cached replied forwarded recent saved))
9406           (gnus-summary-update-secondary-mark article)
9407         ;;; !!! This is bogus.  We should find out what primary
9408         ;;; !!! mark we want to set.
9409         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9410
9411 (defun gnus-summary-mark-as-expirable (n)
9412   "Mark N articles forward as expirable.
9413 If N is negative, mark backward instead.  The difference between N and
9414 the actual number of articles marked is returned."
9415   (interactive "p")
9416   (gnus-summary-mark-forward n gnus-expirable-mark))
9417
9418 (defun gnus-summary-mark-as-spam (n)
9419   "Mark N articles forward as spam.
9420 If N is negative, mark backward instead.  The difference between N and
9421 the actual number of articles marked is returned."
9422   (interactive "p")
9423   (gnus-summary-mark-forward n gnus-spam-mark))
9424
9425 (defun gnus-summary-mark-article-as-replied (article)
9426   "Mark ARTICLE as replied to and update the summary line.
9427 ARTICLE can also be a list of articles."
9428   (interactive (list (gnus-summary-article-number)))
9429   (let ((articles (if (listp article) article (list article))))
9430     (dolist (article articles)
9431       (push article gnus-newsgroup-replied)
9432       (let ((buffer-read-only nil))
9433         (when (gnus-summary-goto-subject article nil t)
9434           (gnus-summary-update-secondary-mark article))))))
9435
9436 (defun gnus-summary-mark-article-as-forwarded (article)
9437   "Mark ARTICLE as forwarded and update the summary line.
9438 ARTICLE can also be a list of articles."
9439   (let ((articles (if (listp article) article (list article))))
9440     (dolist (article articles)
9441       (push article gnus-newsgroup-forwarded)
9442       (let ((buffer-read-only nil))
9443         (when (gnus-summary-goto-subject article nil t)
9444           (gnus-summary-update-secondary-mark article))))))
9445
9446 (defun gnus-summary-set-bookmark (article)
9447   "Set a bookmark in current article."
9448   (interactive (list (gnus-summary-article-number)))
9449   (when (or (not (get-buffer gnus-article-buffer))
9450             (not gnus-current-article)
9451             (not gnus-article-current)
9452             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9453     (error "No current article selected"))
9454   ;; Remove old bookmark, if one exists.
9455   (gnus-pull article gnus-newsgroup-bookmarks)
9456   ;; Set the new bookmark, which is on the form
9457   ;; (article-number . line-number-in-body).
9458   (push
9459    (cons article
9460          (save-excursion
9461            (set-buffer gnus-article-buffer)
9462            (count-lines
9463             (min (point)
9464                  (save-excursion
9465                    (article-goto-body)
9466                    (point)))
9467             (point))))
9468    gnus-newsgroup-bookmarks)
9469   (gnus-message 6 "A bookmark has been added to the current article."))
9470
9471 (defun gnus-summary-remove-bookmark (article)
9472   "Remove the bookmark from the current article."
9473   (interactive (list (gnus-summary-article-number)))
9474   ;; Remove old bookmark, if one exists.
9475   (if (not (assq article gnus-newsgroup-bookmarks))
9476       (gnus-message 6 "No bookmark in current article.")
9477     (gnus-pull article gnus-newsgroup-bookmarks)
9478     (gnus-message 6 "Removed bookmark.")))
9479
9480 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9481 (defun gnus-summary-mark-as-dormant (n)
9482   "Mark N articles forward as dormant.
9483 If N is negative, mark backward instead.  The difference between N and
9484 the actual number of articles marked is returned."
9485   (interactive "p")
9486   (gnus-summary-mark-forward n gnus-dormant-mark))
9487
9488 (defun gnus-summary-set-process-mark (article)
9489   "Set the process mark on ARTICLE and update the summary line."
9490   (setq gnus-newsgroup-processable
9491         (cons article
9492               (delq article gnus-newsgroup-processable)))
9493   (when (gnus-summary-goto-subject article)
9494     (gnus-summary-show-thread)
9495     (gnus-summary-goto-subject article)
9496     (gnus-summary-update-secondary-mark article)))
9497
9498 (defun gnus-summary-remove-process-mark (article)
9499   "Remove the process mark from ARTICLE and update the summary line."
9500   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9501   (when (gnus-summary-goto-subject article)
9502     (gnus-summary-show-thread)
9503     (gnus-summary-goto-subject article)
9504     (gnus-summary-update-secondary-mark article)))
9505
9506 (defun gnus-summary-set-saved-mark (article)
9507   "Set the process mark on ARTICLE and update the summary line."
9508   (push article gnus-newsgroup-saved)
9509   (when (gnus-summary-goto-subject article)
9510     (gnus-summary-update-secondary-mark article)))
9511
9512 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9513   "Mark N articles as read forwards.
9514 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9515 The difference between N and the actual number of articles marked is
9516 returned.
9517 If NO-EXPIRE, auto-expiry will be inhibited."
9518   (interactive "p")
9519   (gnus-summary-show-thread)
9520   (let ((backward (< n 0))
9521         (gnus-summary-goto-unread
9522          (and gnus-summary-goto-unread
9523               (not (eq gnus-summary-goto-unread 'never))
9524               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9525                                     gnus-ticked-mark gnus-dormant-mark)))))
9526         (n (abs n))
9527         (mark (or mark gnus-del-mark)))
9528     (while (and (> n 0)
9529                 (gnus-summary-mark-article nil mark no-expire)
9530                 (zerop (gnus-summary-next-subject
9531                         (if backward -1 1)
9532                         (and gnus-summary-goto-unread
9533                              (not (eq gnus-summary-goto-unread 'never)))
9534                         t)))
9535       (setq n (1- n)))
9536     (when (/= 0 n)
9537       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9538     (gnus-summary-recenter)
9539     (gnus-summary-position-point)
9540     (gnus-set-mode-line 'summary)
9541     n))
9542
9543 (defun gnus-summary-mark-article-as-read (mark)
9544   "Mark the current article quickly as read with MARK."
9545   (let ((article (gnus-summary-article-number)))
9546     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9547     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9548     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9549     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9550     (push (cons article mark) gnus-newsgroup-reads)
9551     ;; Possibly remove from cache, if that is used.
9552     (when gnus-use-cache
9553       (gnus-cache-enter-remove-article article))
9554     ;; Allow the backend to change the mark.
9555     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9556     ;; Check for auto-expiry.
9557     (when (and gnus-newsgroup-auto-expire
9558                (memq mark gnus-auto-expirable-marks))
9559       (setq mark gnus-expirable-mark)
9560       ;; Let the backend know about the mark change.
9561       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9562       (push article gnus-newsgroup-expirable))
9563     ;; Set the mark in the buffer.
9564     (gnus-summary-update-mark mark 'unread)
9565     t))
9566
9567 (defun gnus-summary-mark-article-as-unread (mark)
9568   "Mark the current article quickly as unread with MARK."
9569   (let* ((article (gnus-summary-article-number))
9570          (old-mark (gnus-summary-article-mark article)))
9571     ;; Allow the backend to change the mark.
9572     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9573     (if (eq mark old-mark)
9574         t
9575       (if (<= article 0)
9576           (progn
9577             (gnus-error 1 "Can't mark negative article numbers")
9578             nil)
9579         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9580         (setq gnus-newsgroup-spam-marked
9581               (delq article gnus-newsgroup-spam-marked))
9582         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9583         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9584         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9585         (cond ((= mark gnus-ticked-mark)
9586                (setq gnus-newsgroup-marked
9587                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9588                                               article)))
9589               ((= mark gnus-spam-mark)
9590                (setq gnus-newsgroup-spam-marked
9591                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9592                                               article)))
9593               ((= mark gnus-dormant-mark)
9594                (setq gnus-newsgroup-dormant
9595                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9596                                               article)))
9597               (t
9598                (setq gnus-newsgroup-unreads
9599                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9600                                               article))))
9601         (gnus-pull article gnus-newsgroup-reads)
9602
9603         ;; See whether the article is to be put in the cache.
9604         (and gnus-use-cache
9605              (vectorp (gnus-summary-article-header article))
9606              (save-excursion
9607                (gnus-cache-possibly-enter-article
9608                 gnus-newsgroup-name article
9609                 (= mark gnus-ticked-mark)
9610                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9611
9612         ;; Fix the mark.
9613         (gnus-summary-update-mark mark 'unread)
9614         t))))
9615
9616 (defun gnus-summary-mark-article (&optional article mark no-expire)
9617   "Mark ARTICLE with MARK.  MARK can be any character.
9618 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9619 `??' (dormant) and `?E' (expirable).
9620 If MARK is nil, then the default character `?r' is used.
9621 If ARTICLE is nil, then the article on the current line will be
9622 marked.
9623 If NO-EXPIRE, auto-expiry will be inhibited."
9624   ;; The mark might be a string.
9625   (when (stringp mark)
9626     (setq mark (aref mark 0)))
9627   ;; If no mark is given, then we check auto-expiring.
9628   (when (null mark)
9629     (setq mark gnus-del-mark))
9630   (when (and (not no-expire)
9631              gnus-newsgroup-auto-expire
9632              (memq mark gnus-auto-expirable-marks))
9633     (setq mark gnus-expirable-mark))
9634   (let ((article (or article (gnus-summary-article-number)))
9635         (old-mark (gnus-summary-article-mark article)))
9636     ;; Allow the backend to change the mark.
9637     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9638     (if (eq mark old-mark)
9639         t
9640       (unless article
9641         (error "No article on current line"))
9642       (if (not (if (or (= mark gnus-unread-mark)
9643                        (= mark gnus-ticked-mark)
9644                        (= mark gnus-spam-mark)
9645                        (= mark gnus-dormant-mark))
9646                    (gnus-mark-article-as-unread article mark)
9647                  (gnus-mark-article-as-read article mark)))
9648           t
9649         ;; See whether the article is to be put in the cache.
9650         (and gnus-use-cache
9651              (not (= mark gnus-canceled-mark))
9652              (vectorp (gnus-summary-article-header article))
9653              (save-excursion
9654                (gnus-cache-possibly-enter-article
9655                 gnus-newsgroup-name article
9656                 (= mark gnus-ticked-mark)
9657                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9658
9659         (when (gnus-summary-goto-subject article nil t)
9660           (let ((buffer-read-only nil))
9661             (gnus-summary-show-thread)
9662             ;; Fix the mark.
9663             (gnus-summary-update-mark mark 'unread)
9664             t))))))
9665
9666 (defun gnus-summary-update-secondary-mark (article)
9667   "Update the secondary (read, process, cache) mark."
9668   (gnus-summary-update-mark
9669    (cond ((memq article gnus-newsgroup-processable)
9670           gnus-process-mark)
9671          ((memq article gnus-newsgroup-cached)
9672           gnus-cached-mark)
9673          ((memq article gnus-newsgroup-replied)
9674           gnus-replied-mark)
9675          ((memq article gnus-newsgroup-forwarded)
9676           gnus-forwarded-mark)
9677          ((memq article gnus-newsgroup-saved)
9678           gnus-saved-mark)
9679          ((memq article gnus-newsgroup-recent)
9680           gnus-recent-mark)
9681          ((memq article gnus-newsgroup-unseen)
9682           gnus-unseen-mark)
9683          (t gnus-no-mark))
9684    'replied)
9685   (when (gnus-visual-p 'summary-highlight 'highlight)
9686     (gnus-run-hooks 'gnus-summary-update-hook))
9687   t)
9688
9689 (defun gnus-summary-update-download-mark (article)
9690   "Update the secondary (read, process, cache) mark."
9691   (gnus-summary-update-mark
9692    (cond ((memq article gnus-newsgroup-undownloaded) 
9693           gnus-undownloaded-mark)
9694          (gnus-newsgroup-agentized
9695           gnus-downloaded-mark)
9696          (t
9697           gnus-no-mark))
9698    'download)
9699   (gnus-summary-update-line t)
9700   t)
9701
9702 (defun gnus-summary-update-mark (mark type)
9703   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9704         (buffer-read-only nil))
9705     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9706     (when forward
9707       (when (looking-at "\r")
9708         (incf forward))
9709       (when (<= (+ forward (point)) (point-max))
9710         ;; Go to the right position on the line.
9711         (goto-char (+ forward (point)))
9712         ;; Replace the old mark with the new mark.
9713         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9714         ;; Optionally update the marks by some user rule.
9715         (when (eq type 'unread)
9716           (gnus-data-set-mark
9717            (gnus-data-find (gnus-summary-article-number)) mark)
9718           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9719
9720 (defun gnus-mark-article-as-read (article &optional mark)
9721   "Enter ARTICLE in the pertinent lists and remove it from others."
9722   ;; Make the article expirable.
9723   (let ((mark (or mark gnus-del-mark)))
9724     (setq gnus-newsgroup-expirable
9725           (if (= mark gnus-expirable-mark)
9726               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9727             (delq article gnus-newsgroup-expirable)))
9728     ;; Remove from unread and marked lists.
9729     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9730     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9731     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9732     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9733     (push (cons article mark) gnus-newsgroup-reads)
9734     ;; Possibly remove from cache, if that is used.
9735     (when gnus-use-cache
9736       (gnus-cache-enter-remove-article article))
9737     t))
9738
9739 (defun gnus-mark-article-as-unread (article &optional mark)
9740   "Enter ARTICLE in the pertinent lists and remove it from others."
9741   (let ((mark (or mark gnus-ticked-mark)))
9742     (if (<= article 0)
9743         (progn
9744           (gnus-error 1 "Can't mark negative article numbers")
9745           nil)
9746       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9747             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9748             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9749             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9750             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9751
9752       ;; Unsuppress duplicates?
9753       (when gnus-suppress-duplicates
9754         (gnus-dup-unsuppress-article article))
9755
9756       (cond ((= mark gnus-ticked-mark)
9757              (setq gnus-newsgroup-marked
9758                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9759             ((= mark gnus-spam-mark)
9760              (setq gnus-newsgroup-spam-marked
9761                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9762                                             article)))
9763             ((= mark gnus-dormant-mark)
9764              (setq gnus-newsgroup-dormant
9765                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9766             (t
9767              (setq gnus-newsgroup-unreads
9768                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9769       (gnus-pull article gnus-newsgroup-reads)
9770       t)))
9771
9772 (defalias 'gnus-summary-mark-as-unread-forward
9773   'gnus-summary-tick-article-forward)
9774 (make-obsolete 'gnus-summary-mark-as-unread-forward
9775                'gnus-summary-tick-article-forward)
9776 (defun gnus-summary-tick-article-forward (n)
9777   "Tick N articles forwards.
9778 If N is negative, tick backwards instead.
9779 The difference between N and the number of articles ticked is returned."
9780   (interactive "p")
9781   (gnus-summary-mark-forward n gnus-ticked-mark))
9782
9783 (defalias 'gnus-summary-mark-as-unread-backward
9784   'gnus-summary-tick-article-backward)
9785 (make-obsolete 'gnus-summary-mark-as-unread-backward
9786                'gnus-summary-tick-article-backward)
9787 (defun gnus-summary-tick-article-backward (n)
9788   "Tick N articles backwards.
9789 The difference between N and the number of articles ticked is returned."
9790   (interactive "p")
9791   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9792
9793 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9794 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9795 (defun gnus-summary-tick-article (&optional article clear-mark)
9796   "Mark current article as unread.
9797 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9798 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9799   (interactive)
9800   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9801                                        gnus-ticked-mark)))
9802
9803 (defun gnus-summary-mark-as-read-forward (n)
9804   "Mark N articles as read forwards.
9805 If N is negative, mark backwards instead.
9806 The difference between N and the actual number of articles marked is
9807 returned."
9808   (interactive "p")
9809   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9810
9811 (defun gnus-summary-mark-as-read-backward (n)
9812   "Mark the N articles as read backwards.
9813 The difference between N and the actual number of articles marked is
9814 returned."
9815   (interactive "p")
9816   (gnus-summary-mark-forward
9817    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9818
9819 (defun gnus-summary-mark-as-read (&optional article mark)
9820   "Mark current article as read.
9821 ARTICLE specifies the article to be marked as read.
9822 MARK specifies a string to be inserted at the beginning of the line."
9823   (gnus-summary-mark-article article mark))
9824
9825 (defun gnus-summary-clear-mark-forward (n)
9826   "Clear marks from N articles forward.
9827 If N is negative, clear backward instead.
9828 The difference between N and the number of marks cleared is returned."
9829   (interactive "p")
9830   (gnus-summary-mark-forward n gnus-unread-mark))
9831
9832 (defun gnus-summary-clear-mark-backward (n)
9833   "Clear marks from N articles backward.
9834 The difference between N and the number of marks cleared is returned."
9835   (interactive "p")
9836   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9837
9838 (defun gnus-summary-mark-unread-as-read ()
9839   "Intended to be used by `gnus-summary-mark-article-hook'."
9840   (when (memq gnus-current-article gnus-newsgroup-unreads)
9841     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9842
9843 (defun gnus-summary-mark-read-and-unread-as-read ()
9844   "Intended to be used by `gnus-summary-mark-article-hook'."
9845   (let ((mark (gnus-summary-article-mark)))
9846     (when (or (gnus-unread-mark-p mark)
9847               (gnus-read-mark-p mark))
9848       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9849
9850 (defun gnus-summary-mark-unread-as-ticked ()
9851   "Intended to be used by `gnus-summary-mark-article-hook'."
9852   (when (memq gnus-current-article gnus-newsgroup-unreads)
9853     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9854
9855 (defun gnus-summary-mark-region-as-read (point mark all)
9856   "Mark all unread articles between point and mark as read.
9857 If given a prefix, mark all articles between point and mark as read,
9858 even ticked and dormant ones."
9859   (interactive "r\nP")
9860   (save-excursion
9861     (let (article)
9862       (goto-char point)
9863       (beginning-of-line)
9864       (while (and
9865               (< (point) mark)
9866               (progn
9867                 (when (or all
9868                           (memq (setq article (gnus-summary-article-number))
9869                                 gnus-newsgroup-unreads))
9870                   (gnus-summary-mark-article article gnus-del-mark))
9871                 t)
9872               (gnus-summary-find-next))))))
9873
9874 (defun gnus-summary-mark-below (score mark)
9875   "Mark articles with score less than SCORE with MARK."
9876   (interactive "P\ncMark: ")
9877   (setq score (if score
9878                   (prefix-numeric-value score)
9879                 (or gnus-summary-default-score 0)))
9880   (save-excursion
9881     (set-buffer gnus-summary-buffer)
9882     (goto-char (point-min))
9883     (while
9884         (progn
9885           (and (< (gnus-summary-article-score) score)
9886                (gnus-summary-mark-article nil mark))
9887           (gnus-summary-find-next)))))
9888
9889 (defun gnus-summary-kill-below (&optional score)
9890   "Mark articles with score below SCORE as read."
9891   (interactive "P")
9892   (gnus-summary-mark-below score gnus-killed-mark))
9893
9894 (defun gnus-summary-clear-above (&optional score)
9895   "Clear all marks from articles with score above SCORE."
9896   (interactive "P")
9897   (gnus-summary-mark-above score gnus-unread-mark))
9898
9899 (defun gnus-summary-tick-above (&optional score)
9900   "Tick all articles with score above SCORE."
9901   (interactive "P")
9902   (gnus-summary-mark-above score gnus-ticked-mark))
9903
9904 (defun gnus-summary-mark-above (score mark)
9905   "Mark articles with score over SCORE with MARK."
9906   (interactive "P\ncMark: ")
9907   (setq score (if score
9908                   (prefix-numeric-value score)
9909                 (or gnus-summary-default-score 0)))
9910   (save-excursion
9911     (set-buffer gnus-summary-buffer)
9912     (goto-char (point-min))
9913     (while (and (progn
9914                   (when (> (gnus-summary-article-score) score)
9915                     (gnus-summary-mark-article nil mark))
9916                   t)
9917                 (gnus-summary-find-next)))))
9918
9919 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9920 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9921 (defun gnus-summary-limit-include-expunged (&optional no-error)
9922   "Display all the hidden articles that were expunged for low scores."
9923   (interactive)
9924   (let ((buffer-read-only nil))
9925     (let ((scored gnus-newsgroup-scored)
9926           headers h)
9927       (while scored
9928         (unless (gnus-summary-article-header (caar scored))
9929           (and (setq h (gnus-number-to-header (caar scored)))
9930                (< (cdar scored) gnus-summary-expunge-below)
9931                (push h headers)))
9932         (setq scored (cdr scored)))
9933       (if (not headers)
9934           (when (not no-error)
9935             (error "No expunged articles hidden"))
9936         (goto-char (point-min))
9937         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9938         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9939         (mapcar (lambda (x) (push (mail-header-number x)
9940                                   gnus-newsgroup-limit))
9941                 headers)
9942         (gnus-summary-prepare-unthreaded (nreverse headers))
9943         (goto-char (point-min))
9944         (gnus-summary-position-point)
9945         t))))
9946
9947 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9948   "Mark all unread articles in this newsgroup as read.
9949 If prefix argument ALL is non-nil, ticked and dormant articles will
9950 also be marked as read.
9951 If QUIETLY is non-nil, no questions will be asked.
9952 If TO-HERE is non-nil, it should be a point in the buffer.  All
9953 articles before (after, if REVERSE is set) this point will be marked as read.
9954 Note that this function will only catch up the unread article
9955 in the current summary buffer limitation.
9956 The number of articles marked as read is returned."
9957   (interactive "P")
9958   (prog1
9959       (save-excursion
9960         (when (or quietly
9961                   (not gnus-interactive-catchup) ;Without confirmation?
9962                   gnus-expert-user
9963                   (gnus-y-or-n-p
9964                    (if all
9965                        "Mark absolutely all articles as read? "
9966                      "Mark all unread articles as read? ")))
9967           (if (and not-mark
9968                    (not gnus-newsgroup-adaptive)
9969                    (not gnus-newsgroup-auto-expire)
9970                    (not gnus-suppress-duplicates)
9971                    (or (not gnus-use-cache)
9972                        (eq gnus-use-cache 'passive)))
9973               (progn
9974                 (when all
9975                   (setq gnus-newsgroup-marked nil
9976                         gnus-newsgroup-spam-marked nil
9977                         gnus-newsgroup-dormant nil))
9978                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9979             ;; We actually mark all articles as canceled, which we
9980             ;; have to do when using auto-expiry or adaptive scoring.
9981             (gnus-summary-show-all-threads)
9982             (if (and to-here reverse)
9983                 (progn
9984                   (goto-char to-here)
9985                   (while (and
9986                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9987                           (gnus-summary-find-next (not all) nil nil t))))
9988               (when (gnus-summary-first-subject (not all) t)
9989                 (while (and
9990                         (if to-here (< (point) to-here) t)
9991                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9992                         (gnus-summary-find-next (not all) nil nil t)))))
9993             (gnus-set-mode-line 'summary))
9994           t))
9995     (gnus-summary-position-point)))
9996
9997 (defun gnus-summary-catchup-to-here (&optional all)
9998   "Mark all unticked articles before the current one as read.
9999 If ALL is non-nil, also mark ticked and dormant articles as read."
10000   (interactive "P")
10001   (save-excursion
10002     (gnus-save-hidden-threads
10003       (let ((beg (point)))
10004         ;; We check that there are unread articles.
10005         (when (or all (gnus-summary-find-prev))
10006           (gnus-summary-catchup all t beg)))))
10007   (gnus-summary-position-point))
10008
10009 (defun gnus-summary-catchup-from-here (&optional all)
10010   "Mark all unticked articles after the current one as read.
10011 If ALL is non-nil, also mark ticked and dormant articles as read."
10012   (interactive "P")
10013   (save-excursion
10014     (gnus-save-hidden-threads
10015       (let ((beg (point)))
10016         ;; We check that there are unread articles.
10017         (when (or all (gnus-summary-find-next))
10018           (gnus-summary-catchup all t beg nil t)))))
10019
10020   (gnus-summary-position-point))
10021 (defun gnus-summary-catchup-all (&optional quietly)
10022   "Mark all articles in this newsgroup as read.
10023 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10024 instead, which marks only unread articles as read."
10025   (interactive "P")
10026   (gnus-summary-catchup t quietly))
10027
10028 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10029   "Mark all unread articles in this group as read, then exit.
10030 If prefix argument ALL is non-nil, all articles are marked as read.
10031 If QUIETLY is non-nil, no questions will be asked."
10032   (interactive "P")
10033   (when (gnus-summary-catchup all quietly nil 'fast)
10034     ;; Select next newsgroup or exit.
10035     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10036              (eq gnus-auto-select-next 'quietly))
10037         (gnus-summary-next-group nil)
10038       (gnus-summary-exit))))
10039
10040 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10041   "Mark all articles in this newsgroup as read, and then exit.
10042 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10043 instead, which marks only unread articles as read."
10044   (interactive "P")
10045   (gnus-summary-catchup-and-exit t quietly))
10046
10047 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10048   "Mark all articles in this group as read and select the next group.
10049 If given a prefix, mark all articles, unread as well as ticked, as
10050 read."
10051   (interactive "P")
10052   (save-excursion
10053     (gnus-summary-catchup all))
10054   (gnus-summary-next-group))
10055
10056 ;;;
10057 ;;; with article
10058 ;;;
10059
10060 (defmacro gnus-with-article (article &rest forms)
10061   "Select ARTICLE and perform FORMS in the original article buffer.
10062 Then replace the article with the result."
10063   `(progn
10064      ;; We don't want the article to be marked as read.
10065      (let (gnus-mark-article-hook)
10066        (gnus-summary-select-article t t nil ,article))
10067      (set-buffer gnus-original-article-buffer)
10068      ,@forms
10069      (if (not (gnus-check-backend-function
10070                'request-replace-article (car gnus-article-current)))
10071          (gnus-message 5 "Read-only group; not replacing")
10072        (unless (gnus-request-replace-article
10073                 ,article (car gnus-article-current)
10074                 (current-buffer) t)
10075          (error "Couldn't replace article")))
10076      ;; The cache and backlog have to be flushed somewhat.
10077      (when gnus-keep-backlog
10078        (gnus-backlog-remove-article
10079         (car gnus-article-current) (cdr gnus-article-current)))
10080      (when gnus-use-cache
10081        (gnus-cache-update-article
10082         (car gnus-article-current) (cdr gnus-article-current)))))
10083
10084 (put 'gnus-with-article 'lisp-indent-function 1)
10085 (put 'gnus-with-article 'edebug-form-spec '(form body))
10086
10087 ;; Thread-based commands.
10088
10089 (defun gnus-summary-articles-in-thread (&optional article)
10090   "Return a list of all articles in the current thread.
10091 If ARTICLE is non-nil, return all articles in the thread that starts
10092 with that article."
10093   (let* ((article (or article (gnus-summary-article-number)))
10094          (data (gnus-data-find-list article))
10095          (top-level (gnus-data-level (car data)))
10096          (top-subject
10097           (cond ((null gnus-thread-operation-ignore-subject)
10098                  (gnus-simplify-subject-re
10099                   (mail-header-subject (gnus-data-header (car data)))))
10100                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10101                  (gnus-simplify-subject-fuzzy
10102                   (mail-header-subject (gnus-data-header (car data)))))
10103                 (t nil)))
10104          (end-point (save-excursion
10105                       (if (gnus-summary-go-to-next-thread)
10106                           (point) (point-max))))
10107          articles)
10108     (while (and data
10109                 (< (gnus-data-pos (car data)) end-point))
10110       (when (or (not top-subject)
10111                 (string= top-subject
10112                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10113                              (gnus-simplify-subject-fuzzy
10114                               (mail-header-subject
10115                                (gnus-data-header (car data))))
10116                            (gnus-simplify-subject-re
10117                             (mail-header-subject
10118                              (gnus-data-header (car data)))))))
10119         (push (gnus-data-number (car data)) articles))
10120       (unless (and (setq data (cdr data))
10121                    (> (gnus-data-level (car data)) top-level))
10122         (setq data nil)))
10123     ;; Return the list of articles.
10124     (nreverse articles)))
10125
10126 (defun gnus-summary-rethread-current ()
10127   "Rethread the thread the current article is part of."
10128   (interactive)
10129   (let* ((gnus-show-threads t)
10130          (article (gnus-summary-article-number))
10131          (id (mail-header-id (gnus-summary-article-header)))
10132          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10133     (unless id
10134       (error "No article on the current line"))
10135     (gnus-rebuild-thread id)
10136     (gnus-summary-goto-subject article)))
10137
10138 (defun gnus-summary-reparent-thread ()
10139   "Make the current article child of the marked (or previous) article.
10140
10141 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10142 is non-nil or the Subject: of both articles are the same."
10143   (interactive)
10144   (unless (not (gnus-group-read-only-p))
10145     (error "The current newsgroup does not support article editing"))
10146   (unless (<= (length gnus-newsgroup-processable) 1)
10147     (error "No more than one article may be marked"))
10148   (save-window-excursion
10149     (let ((gnus-article-buffer " *reparent*")
10150           (current-article (gnus-summary-article-number))
10151           ;; First grab the marked article, otherwise one line up.
10152           (parent-article (if (not (null gnus-newsgroup-processable))
10153                               (car gnus-newsgroup-processable)
10154                             (save-excursion
10155                               (if (eq (forward-line -1) 0)
10156                                   (gnus-summary-article-number)
10157                                 (error "Beginning of summary buffer"))))))
10158       (unless (not (eq current-article parent-article))
10159         (error "An article may not be self-referential"))
10160       (let ((message-id (mail-header-id
10161                          (gnus-summary-article-header parent-article))))
10162         (unless (and message-id (not (equal message-id "")))
10163           (error "No message-id in desired parent"))
10164         (gnus-with-article current-article
10165           (save-restriction
10166             (goto-char (point-min))
10167             (message-narrow-to-head)
10168             (if (re-search-forward "^References: " nil t)
10169                 (progn
10170                   (re-search-forward "^[^ \t]" nil t)
10171                   (forward-line -1)
10172                   (end-of-line)
10173                   (insert " " message-id))
10174               (insert "References: " message-id "\n"))))
10175         (set-buffer gnus-summary-buffer)
10176         (gnus-summary-unmark-all-processable)
10177         (gnus-summary-update-article current-article)
10178         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10179             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10180         (gnus-summary-rethread-current)
10181         (gnus-message 3 "Article %d is now the child of article %d"
10182                       current-article parent-article)))))
10183
10184 (defun gnus-summary-toggle-threads (&optional arg)
10185   "Toggle showing conversation threads.
10186 If ARG is positive number, turn showing conversation threads on."
10187   (interactive "P")
10188   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10189     (setq gnus-show-threads
10190           (if (null arg) (not gnus-show-threads)
10191             (> (prefix-numeric-value arg) 0)))
10192     (gnus-summary-prepare)
10193     (gnus-summary-goto-subject current)
10194     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10195     (gnus-summary-position-point)))
10196
10197 (defun gnus-summary-show-all-threads ()
10198   "Show all threads."
10199   (interactive)
10200   (save-excursion
10201     (let ((buffer-read-only nil))
10202       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10203   (gnus-summary-position-point))
10204
10205 (defun gnus-summary-show-thread ()
10206   "Show thread subtrees.
10207 Returns nil if no thread was there to be shown."
10208   (interactive)
10209   (let ((buffer-read-only nil)
10210         (orig (point))
10211         ;; first goto end then to beg, to have point at beg after let
10212         (end (progn (end-of-line) (point)))
10213         (beg (progn (beginning-of-line) (point))))
10214     (prog1
10215         ;; Any hidden lines here?
10216         (search-forward "\r" end t)
10217       (subst-char-in-region beg end ?\^M ?\n t)
10218       (goto-char orig)
10219       (gnus-summary-position-point))))
10220
10221 (defun gnus-summary-maybe-hide-threads ()
10222   "If requested, hide the threads that should be hidden."
10223   (when (and gnus-show-threads
10224              gnus-thread-hide-subtree)
10225     (gnus-summary-hide-all-threads
10226      (if (or (consp gnus-thread-hide-subtree)
10227              (gnus-functionp gnus-thread-hide-subtree))
10228          (gnus-make-predicate gnus-thread-hide-subtree)
10229        nil))))
10230
10231 ;;; Hiding predicates.
10232
10233 (defun gnus-article-unread-p (header)
10234   (memq (mail-header-number header) gnus-newsgroup-unreads))
10235
10236 (defun gnus-article-unseen-p (header)
10237   (memq (mail-header-number header) gnus-newsgroup-unseen))
10238
10239 (defun gnus-map-articles (predicate articles)
10240   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10241   (apply 'gnus-or (mapcar predicate
10242                           (mapcar 'gnus-summary-article-header articles))))
10243
10244 (defun gnus-summary-hide-all-threads (&optional predicate)
10245   "Hide all thread subtrees.
10246 If PREDICATE is supplied, threads that satisfy this predicate
10247 will not be hidden."
10248   (interactive)
10249   (save-excursion
10250     (goto-char (point-min))
10251     (let ((end nil))
10252       (while (not end)
10253         (when (or (not predicate)
10254                   (gnus-map-articles
10255                    predicate (gnus-summary-article-children)))
10256             (gnus-summary-hide-thread))
10257         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10258   (gnus-summary-position-point))
10259
10260 (defun gnus-summary-hide-thread ()
10261   "Hide thread subtrees.
10262 If PREDICATE is supplied, threads that satisfy this predicate
10263 will not be hidden.
10264 Returns nil if no threads were there to be hidden."
10265   (interactive)
10266   (let ((buffer-read-only nil)
10267         (start (point))
10268         (article (gnus-summary-article-number)))
10269     (goto-char start)
10270     ;; Go forward until either the buffer ends or the subthread
10271     ;; ends.
10272     (when (and (not (eobp))
10273                (or (zerop (gnus-summary-next-thread 1 t))
10274                    (goto-char (point-max))))
10275       (prog1
10276           (if (and (> (point) start)
10277                    (search-backward "\n" start t))
10278               (progn
10279                 (subst-char-in-region start (point) ?\n ?\^M)
10280                 (gnus-summary-goto-subject article))
10281             (goto-char start)
10282             nil)))))
10283
10284 (defun gnus-summary-go-to-next-thread (&optional previous)
10285   "Go to the same level (or less) next thread.
10286 If PREVIOUS is non-nil, go to previous thread instead.
10287 Return the article number moved to, or nil if moving was impossible."
10288   (let ((level (gnus-summary-thread-level))
10289         (way (if previous -1 1))
10290         (beg (point)))
10291     (forward-line way)
10292     (while (and (not (eobp))
10293                 (< level (gnus-summary-thread-level)))
10294       (forward-line way))
10295     (if (eobp)
10296         (progn
10297           (goto-char beg)
10298           nil)
10299       (setq beg (point))
10300       (prog1
10301           (gnus-summary-article-number)
10302         (goto-char beg)))))
10303
10304 (defun gnus-summary-next-thread (n &optional silent)
10305   "Go to the same level next N'th thread.
10306 If N is negative, search backward instead.
10307 Returns the difference between N and the number of skips actually
10308 done.
10309
10310 If SILENT, don't output messages."
10311   (interactive "p")
10312   (let ((backward (< n 0))
10313         (n (abs n)))
10314     (while (and (> n 0)
10315                 (gnus-summary-go-to-next-thread backward))
10316       (decf n))
10317     (unless silent
10318       (gnus-summary-position-point))
10319     (when (and (not silent) (/= 0 n))
10320       (gnus-message 7 "No more threads"))
10321     n))
10322
10323 (defun gnus-summary-prev-thread (n)
10324   "Go to the same level previous N'th thread.
10325 Returns the difference between N and the number of skips actually
10326 done."
10327   (interactive "p")
10328   (gnus-summary-next-thread (- n)))
10329
10330 (defun gnus-summary-go-down-thread ()
10331   "Go down one level in the current thread."
10332   (let ((children (gnus-summary-article-children)))
10333     (when children
10334       (gnus-summary-goto-subject (car children)))))
10335
10336 (defun gnus-summary-go-up-thread ()
10337   "Go up one level in the current thread."
10338   (let ((parent (gnus-summary-article-parent)))
10339     (when parent
10340       (gnus-summary-goto-subject parent))))
10341
10342 (defun gnus-summary-down-thread (n)
10343   "Go down thread N steps.
10344 If N is negative, go up instead.
10345 Returns the difference between N and how many steps down that were
10346 taken."
10347   (interactive "p")
10348   (let ((up (< n 0))
10349         (n (abs n)))
10350     (while (and (> n 0)
10351                 (if up (gnus-summary-go-up-thread)
10352                   (gnus-summary-go-down-thread)))
10353       (setq n (1- n)))
10354     (gnus-summary-position-point)
10355     (when (/= 0 n)
10356       (gnus-message 7 "Can't go further"))
10357     n))
10358
10359 (defun gnus-summary-up-thread (n)
10360   "Go up thread N steps.
10361 If N is negative, go down instead.
10362 Returns the difference between N and how many steps down that were
10363 taken."
10364   (interactive "p")
10365   (gnus-summary-down-thread (- n)))
10366
10367 (defun gnus-summary-top-thread ()
10368   "Go to the top of the thread."
10369   (interactive)
10370   (while (gnus-summary-go-up-thread))
10371   (gnus-summary-article-number))
10372
10373 (defun gnus-summary-kill-thread (&optional unmark)
10374   "Mark articles under current thread as read.
10375 If the prefix argument is positive, remove any kinds of marks.
10376 If the prefix argument is negative, tick articles instead."
10377   (interactive "P")
10378   (when unmark
10379     (setq unmark (prefix-numeric-value unmark)))
10380   (let ((articles (gnus-summary-articles-in-thread)))
10381     (save-excursion
10382       ;; Expand the thread.
10383       (gnus-summary-show-thread)
10384       ;; Mark all the articles.
10385       (while articles
10386         (gnus-summary-goto-subject (car articles))
10387         (cond ((null unmark)
10388                (gnus-summary-mark-article-as-read gnus-killed-mark))
10389               ((> unmark 0)
10390                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10391               (t
10392                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10393         (setq articles (cdr articles))))
10394     ;; Hide killed subtrees.
10395     (and (null unmark)
10396          gnus-thread-hide-killed
10397          (gnus-summary-hide-thread))
10398     ;; If marked as read, go to next unread subject.
10399     (when (null unmark)
10400       ;; Go to next unread subject.
10401       (gnus-summary-next-subject 1 t)))
10402   (gnus-set-mode-line 'summary))
10403
10404 ;; Summary sorting commands
10405
10406 (defun gnus-summary-sort-by-number (&optional reverse)
10407   "Sort the summary buffer by article number.
10408 Argument REVERSE means reverse order."
10409   (interactive "P")
10410   (gnus-summary-sort 'number reverse))
10411
10412 (defun gnus-summary-sort-by-random (&optional reverse)
10413   "Randomize the order in the summary buffer.
10414 Argument REVERSE means to randomize in reverse order."
10415   (interactive "P")
10416   (gnus-summary-sort 'random reverse))
10417
10418 (defun gnus-summary-sort-by-author (&optional reverse)
10419   "Sort the summary buffer by author name alphabetically.
10420 If `case-fold-search' is non-nil, case of letters is ignored.
10421 Argument REVERSE means reverse order."
10422   (interactive "P")
10423   (gnus-summary-sort 'author reverse))
10424
10425 (defun gnus-summary-sort-by-subject (&optional reverse)
10426   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10427 If `case-fold-search' is non-nil, case of letters is ignored.
10428 Argument REVERSE means reverse order."
10429   (interactive "P")
10430   (gnus-summary-sort 'subject reverse))
10431
10432 (defun gnus-summary-sort-by-date (&optional reverse)
10433   "Sort the summary buffer by date.
10434 Argument REVERSE means reverse order."
10435   (interactive "P")
10436   (gnus-summary-sort 'date reverse))
10437
10438 (defun gnus-summary-sort-by-score (&optional reverse)
10439   "Sort the summary buffer by score.
10440 Argument REVERSE means reverse order."
10441   (interactive "P")
10442   (gnus-summary-sort 'score reverse))
10443
10444 (defun gnus-summary-sort-by-lines (&optional reverse)
10445   "Sort the summary buffer by the number of lines.
10446 Argument REVERSE means reverse order."
10447   (interactive "P")
10448   (gnus-summary-sort 'lines reverse))
10449
10450 (defun gnus-summary-sort-by-chars (&optional reverse)
10451   "Sort the summary buffer by article length.
10452 Argument REVERSE means reverse order."
10453   (interactive "P")
10454   (gnus-summary-sort 'chars reverse))
10455
10456 (defun gnus-summary-sort-by-original (&optional reverse)
10457   "Sort the summary buffer using the default sorting method.
10458 Argument REVERSE means reverse order."
10459   (interactive "P")
10460   (let* ((buffer-read-only)
10461          (gnus-summary-prepare-hook nil))
10462     ;; We do the sorting by regenerating the threads.
10463     (gnus-summary-prepare)
10464     ;; Hide subthreads if needed.
10465     (gnus-summary-maybe-hide-threads)))
10466
10467 (defun gnus-summary-sort (predicate reverse)
10468   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10469   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10470          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10471          (gnus-thread-sort-functions
10472           (if (not reverse)
10473               thread
10474             `(lambda (t1 t2)
10475                (,thread t2 t1))))
10476          (gnus-sort-gathered-threads-function
10477           gnus-thread-sort-functions)
10478          (gnus-article-sort-functions
10479           (if (not reverse)
10480               article
10481             `(lambda (t1 t2)
10482                (,article t2 t1))))
10483          (buffer-read-only)
10484          (gnus-summary-prepare-hook nil))
10485     ;; We do the sorting by regenerating the threads.
10486     (gnus-summary-prepare)
10487     ;; Hide subthreads if needed.
10488     (gnus-summary-maybe-hide-threads)))
10489
10490 ;; Summary saving commands.
10491
10492 (defun gnus-summary-save-article (&optional n not-saved)
10493   "Save the current article using the default saver function.
10494 If N is a positive number, save the N next articles.
10495 If N is a negative number, save the N previous articles.
10496 If N is nil and any articles have been marked with the process mark,
10497 save those articles instead.
10498 The variable `gnus-default-article-saver' specifies the saver function."
10499   (interactive "P")
10500   (let* ((articles (gnus-summary-work-articles n))
10501          (save-buffer (save-excursion
10502                         (nnheader-set-temp-buffer " *Gnus Save*")))
10503          (num (length articles))
10504          header file)
10505     (dolist (article articles)
10506       (setq header (gnus-summary-article-header article))
10507       (if (not (vectorp header))
10508           ;; This is a pseudo-article.
10509           (if (assq 'name header)
10510               (gnus-copy-file (cdr (assq 'name header)))
10511             (gnus-message 1 "Article %d is unsaveable" article))
10512         ;; This is a real article.
10513         (save-window-excursion
10514           (let ((gnus-display-mime-function nil)
10515                 (gnus-article-prepare-hook nil))
10516             (gnus-summary-select-article t nil nil article)))
10517         (save-excursion
10518           (set-buffer save-buffer)
10519           (erase-buffer)
10520           (insert-buffer-substring gnus-original-article-buffer))
10521         (setq file (gnus-article-save save-buffer file num))
10522         (gnus-summary-remove-process-mark article)
10523         (unless not-saved
10524           (gnus-summary-set-saved-mark article))))
10525     (gnus-kill-buffer save-buffer)
10526     (gnus-summary-position-point)
10527     (gnus-set-mode-line 'summary)
10528     n))
10529
10530 (defun gnus-summary-pipe-output (&optional arg headers)
10531   "Pipe the current article to a subprocess.
10532 If N is a positive number, pipe the N next articles.
10533 If N is a negative number, pipe the N previous articles.
10534 If N is nil and any articles have been marked with the process mark,
10535 pipe those articles instead.
10536 If HEADERS (the symbolic prefix), include the headers, too."
10537   (interactive (gnus-interactive "P\ny"))
10538   (require 'gnus-art)
10539   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10540         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10541     (gnus-summary-save-article arg t))
10542   (let ((buffer (get-buffer "*Shell Command Output*")))
10543     (when (and buffer
10544                (not (zerop (buffer-size buffer))))
10545       (gnus-configure-windows 'pipe))))
10546
10547 (defun gnus-summary-save-article-mail (&optional arg)
10548   "Append the current article to an mail file.
10549 If N is a positive number, save the N next articles.
10550 If N is a negative number, save the N previous articles.
10551 If N is nil and any articles have been marked with the process mark,
10552 save those articles instead."
10553   (interactive "P")
10554   (require 'gnus-art)
10555   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10556     (gnus-summary-save-article arg)))
10557
10558 (defun gnus-summary-save-article-rmail (&optional arg)
10559   "Append the current article to an rmail file.
10560 If N is a positive number, save the N next articles.
10561 If N is a negative number, save the N previous articles.
10562 If N is nil and any articles have been marked with the process mark,
10563 save those articles instead."
10564   (interactive "P")
10565   (require 'gnus-art)
10566   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10567     (gnus-summary-save-article arg)))
10568
10569 (defun gnus-summary-save-article-file (&optional arg)
10570   "Append the current article to a file.
10571 If N is a positive number, save the N next articles.
10572 If N is a negative number, save the N previous articles.
10573 If N is nil and any articles have been marked with the process mark,
10574 save those articles instead."
10575   (interactive "P")
10576   (require 'gnus-art)
10577   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10578     (gnus-summary-save-article arg)))
10579
10580 (defun gnus-summary-write-article-file (&optional arg)
10581   "Write the current article to a file, deleting the previous file.
10582 If N is a positive number, save the N next articles.
10583 If N is a negative number, save the N previous articles.
10584 If N is nil and any articles have been marked with the process mark,
10585 save those articles instead."
10586   (interactive "P")
10587   (require 'gnus-art)
10588   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10589     (gnus-summary-save-article arg)))
10590
10591 (defun gnus-summary-save-article-body-file (&optional arg)
10592   "Append the current article body to a file.
10593 If N is a positive number, save the N next articles.
10594 If N is a negative number, save the N previous articles.
10595 If N is nil and any articles have been marked with the process mark,
10596 save those articles instead."
10597   (interactive "P")
10598   (require 'gnus-art)
10599   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10600     (gnus-summary-save-article arg)))
10601
10602 (defun gnus-summary-muttprint (&optional arg)
10603   "Print the current article using Muttprint.
10604 If N is a positive number, save the N next articles.
10605 If N is a negative number, save the N previous articles.
10606 If N is nil and any articles have been marked with the process mark,
10607 save those articles instead."
10608   (interactive "P")
10609   (require 'gnus-art)
10610   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10611     (gnus-summary-save-article arg t)))
10612
10613 (defun gnus-summary-pipe-message (program)
10614   "Pipe the current article through PROGRAM."
10615   (interactive "sProgram: ")
10616   (gnus-summary-select-article)
10617   (let ((mail-header-separator ""))
10618     (gnus-eval-in-buffer-window gnus-article-buffer
10619       (save-restriction
10620         (widen)
10621         (let ((start (window-start))
10622               buffer-read-only)
10623           (message-pipe-buffer-body program)
10624           (set-window-start (get-buffer-window (current-buffer)) start))))))
10625
10626 (defun gnus-get-split-value (methods)
10627   "Return a value based on the split METHODS."
10628   (let (split-name method result match)
10629     (when methods
10630       (save-excursion
10631         (set-buffer gnus-original-article-buffer)
10632         (save-restriction
10633           (nnheader-narrow-to-headers)
10634           (while (and methods (not split-name))
10635             (goto-char (point-min))
10636             (setq method (pop methods))
10637             (setq match (car method))
10638             (when (cond
10639                    ((stringp match)
10640                     ;; Regular expression.
10641                     (ignore-errors
10642                       (re-search-forward match nil t)))
10643                    ((gnus-functionp match)
10644                     ;; Function.
10645                     (save-restriction
10646                       (widen)
10647                       (setq result (funcall match gnus-newsgroup-name))))
10648                    ((consp match)
10649                     ;; Form.
10650                     (save-restriction
10651                       (widen)
10652                       (setq result (eval match)))))
10653               (setq split-name (cdr method))
10654               (cond ((stringp result)
10655                      (push (expand-file-name
10656                             result gnus-article-save-directory)
10657                            split-name))
10658                     ((consp result)
10659                      (setq split-name (append result split-name)))))))))
10660     (nreverse split-name)))
10661
10662 (defun gnus-valid-move-group-p (group)
10663   (and (boundp group)
10664        (symbol-name group)
10665        (symbol-value group)
10666        (gnus-get-function (gnus-find-method-for-group
10667                            (symbol-name group)) 'request-accept-article t)))
10668
10669 (defun gnus-read-move-group-name (prompt default articles prefix)
10670   "Read a group name."
10671   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10672          (minibuffer-confirm-incomplete nil) ; XEmacs
10673          (prom
10674           (format "%s %s to:"
10675                   prompt
10676                   (if (> (length articles) 1)
10677                       (format "these %d articles" (length articles))
10678                     "this article")))
10679          (to-newsgroup
10680           (cond
10681            ((null split-name)
10682             (gnus-completing-read-with-default
10683              default prom
10684              gnus-active-hashtb
10685              'gnus-valid-move-group-p
10686              nil prefix
10687              'gnus-group-history))
10688            ((= 1 (length split-name))
10689             (gnus-completing-read-with-default
10690              (car split-name) prom
10691              gnus-active-hashtb
10692              'gnus-valid-move-group-p
10693              nil nil
10694              'gnus-group-history))
10695            (t
10696             (gnus-completing-read-with-default
10697              nil prom
10698              (mapcar (lambda (el) (list el))
10699                      (nreverse split-name))
10700              nil nil nil
10701              'gnus-group-history))))
10702          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10703     (when to-newsgroup
10704       (if (or (string= to-newsgroup "")
10705               (string= to-newsgroup prefix))
10706           (setq to-newsgroup default))
10707       (unless to-newsgroup
10708         (error "No group name entered"))
10709       (or (gnus-active to-newsgroup)
10710           (gnus-activate-group to-newsgroup nil nil to-method)
10711           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10712                                      to-newsgroup))
10713               (or (and (gnus-request-create-group to-newsgroup to-method)
10714                        (gnus-activate-group
10715                         to-newsgroup nil nil to-method)
10716                        (gnus-subscribe-group to-newsgroup))
10717                   (error "Couldn't create group %s" to-newsgroup)))
10718           (error "No such group: %s" to-newsgroup)))
10719     to-newsgroup))
10720
10721 (defun gnus-summary-save-parts (type dir n &optional reverse)
10722   "Save parts matching TYPE to DIR.
10723 If REVERSE, save parts that do not match TYPE."
10724   (interactive
10725    (list (read-string "Save parts of type: "
10726                       (or (car gnus-summary-save-parts-type-history)
10727                           gnus-summary-save-parts-default-mime)
10728                       'gnus-summary-save-parts-type-history)
10729          (setq gnus-summary-save-parts-last-directory
10730                (read-file-name "Save to directory: "
10731                                gnus-summary-save-parts-last-directory
10732                                nil t))
10733          current-prefix-arg))
10734   (gnus-summary-iterate n
10735     (let ((gnus-display-mime-function nil)
10736           (gnus-inhibit-treatment t))
10737       (gnus-summary-select-article))
10738     (save-excursion
10739       (set-buffer gnus-article-buffer)
10740       (let ((handles (or gnus-article-mime-handles
10741                          (mm-dissect-buffer nil gnus-article-loose-mime)
10742                          (and gnus-article-emulate-mime
10743                               (mm-uu-dissect)))))
10744         (when handles
10745           (gnus-summary-save-parts-1 type dir handles reverse)
10746           (unless gnus-article-mime-handles ;; Don't destroy this case.
10747             (mm-destroy-parts handles)))))))
10748
10749 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10750   (if (stringp (car handle))
10751       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10752               (cdr handle))
10753     (when (if reverse
10754               (not (string-match type (mm-handle-media-type handle)))
10755             (string-match type (mm-handle-media-type handle)))
10756       (let ((file (expand-file-name
10757                    (file-name-nondirectory
10758                     (or
10759                      (mail-content-type-get
10760                       (mm-handle-disposition handle) 'filename)
10761                      (concat gnus-newsgroup-name
10762                              "." (number-to-string
10763                                   (cdr gnus-article-current)))))
10764                    dir)))
10765         (unless (file-exists-p file)
10766           (mm-save-part-to-file handle file))))))
10767
10768 ;; Summary extract commands
10769
10770 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10771   (let ((buffer-read-only nil)
10772         (article (gnus-summary-article-number))
10773         after-article b e)
10774     (unless (gnus-summary-goto-subject article)
10775       (error "No such article: %d" article))
10776     (gnus-summary-position-point)
10777     ;; If all commands are to be bunched up on one line, we collect
10778     ;; them here.
10779     (unless gnus-view-pseudos-separately
10780       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10781             files action)
10782         (while ps
10783           (setq action (cdr (assq 'action (car ps))))
10784           (setq files (list (cdr (assq 'name (car ps)))))
10785           (while (and ps (cdr ps)
10786                       (string= (or action "1")
10787                                (or (cdr (assq 'action (cadr ps))) "2")))
10788             (push (cdr (assq 'name (cadr ps))) files)
10789             (setcdr ps (cddr ps)))
10790           (when files
10791             (when (not (string-match "%s" action))
10792               (push " " files))
10793             (push " " files)
10794             (when (assq 'execute (car ps))
10795               (setcdr (assq 'execute (car ps))
10796                       (funcall (if (string-match "%s" action)
10797                                    'format 'concat)
10798                                action
10799                                (mapconcat
10800                                 (lambda (f)
10801                                   (if (equal f " ")
10802                                       f
10803                                     (mm-quote-arg f)))
10804                                 files " ")))))
10805           (setq ps (cdr ps)))))
10806     (if (and gnus-view-pseudos (not not-view))
10807         (while pslist
10808           (when (assq 'execute (car pslist))
10809             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10810                                   (eq gnus-view-pseudos 'not-confirm)))
10811           (setq pslist (cdr pslist)))
10812       (save-excursion
10813         (while pslist
10814           (setq after-article (or (cdr (assq 'article (car pslist)))
10815                                   (gnus-summary-article-number)))
10816           (gnus-summary-goto-subject after-article)
10817           (forward-line 1)
10818           (setq b (point))
10819           (insert "    " (file-name-nondirectory
10820                           (cdr (assq 'name (car pslist))))
10821                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10822           (setq e (point))
10823           (forward-line -1)             ; back to `b'
10824           (gnus-add-text-properties
10825            b (1- e) (list 'gnus-number gnus-reffed-article-number
10826                           gnus-mouse-face-prop gnus-mouse-face))
10827           (gnus-data-enter
10828            after-article gnus-reffed-article-number
10829            gnus-unread-mark b (car pslist) 0 (- e b))
10830           (setq gnus-newsgroup-unreads
10831                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10832                                          gnus-reffed-article-number))
10833           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10834           (setq pslist (cdr pslist)))))))
10835
10836 (defun gnus-pseudos< (p1 p2)
10837   (let ((c1 (cdr (assq 'action p1)))
10838         (c2 (cdr (assq 'action p2))))
10839     (and c1 c2 (string< c1 c2))))
10840
10841 (defun gnus-request-pseudo-article (props)
10842   (cond ((assq 'execute props)
10843          (gnus-execute-command (cdr (assq 'execute props)))))
10844   (let ((gnus-current-article (gnus-summary-article-number)))
10845     (gnus-run-hooks 'gnus-mark-article-hook)))
10846
10847 (defun gnus-execute-command (command &optional automatic)
10848   (save-excursion
10849     (gnus-article-setup-buffer)
10850     (set-buffer gnus-article-buffer)
10851     (setq buffer-read-only nil)
10852     (let ((command (if automatic command
10853                      (read-string "Command: " (cons command 0)))))
10854       (erase-buffer)
10855       (insert "$ " command "\n\n")
10856       (if gnus-view-pseudo-asynchronously
10857           (start-process "gnus-execute" (current-buffer) shell-file-name
10858                          shell-command-switch command)
10859         (call-process shell-file-name nil t nil
10860                       shell-command-switch command)))))
10861
10862 ;; Summary kill commands.
10863
10864 (defun gnus-summary-edit-global-kill (article)
10865   "Edit the \"global\" kill file."
10866   (interactive (list (gnus-summary-article-number)))
10867   (gnus-group-edit-global-kill article))
10868
10869 (defun gnus-summary-edit-local-kill ()
10870   "Edit a local kill file applied to the current newsgroup."
10871   (interactive)
10872   (setq gnus-current-headers (gnus-summary-article-header))
10873   (gnus-group-edit-local-kill
10874    (gnus-summary-article-number) gnus-newsgroup-name))
10875
10876 ;;; Header reading.
10877
10878 (defun gnus-read-header (id &optional header)
10879   "Read the headers of article ID and enter them into the Gnus system."
10880   (let ((group gnus-newsgroup-name)
10881         (gnus-override-method
10882          (or
10883           gnus-override-method
10884           (and (gnus-news-group-p gnus-newsgroup-name)
10885                (car (gnus-refer-article-methods)))))
10886         where)
10887     ;; First we check to see whether the header in question is already
10888     ;; fetched.
10889     (if (stringp id)
10890         ;; This is a Message-ID.
10891         (setq header (or header (gnus-id-to-header id)))
10892       ;; This is an article number.
10893       (setq header (or header (gnus-summary-article-header id))))
10894     (if (and header
10895              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10896         ;; We have found the header.
10897         header
10898       ;; If this is a sparse article, we have to nix out its
10899       ;; previous entry in the thread hashtb.
10900       (when (and header
10901                  (gnus-summary-article-sparse-p (mail-header-number header)))
10902         (let* ((parent (gnus-parent-id (mail-header-references header)))
10903                (thread (and parent (gnus-id-to-thread parent))))
10904           (when thread
10905             (delq (assq header thread) thread))))
10906       ;; We have to really fetch the header to this article.
10907       (save-excursion
10908         (set-buffer nntp-server-buffer)
10909         (when (setq where (gnus-request-head id group))
10910           (nnheader-fold-continuation-lines)
10911           (goto-char (point-max))
10912           (insert ".\n")
10913           (goto-char (point-min))
10914           (insert "211 ")
10915           (princ (cond
10916                   ((numberp id) id)
10917                   ((cdr where) (cdr where))
10918                   (header (mail-header-number header))
10919                   (t gnus-reffed-article-number))
10920                  (current-buffer))
10921           (insert " Article retrieved.\n"))
10922         (if (or (not where)
10923                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10924             ()                          ; Malformed head.
10925           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10926             (when (and (stringp id)
10927                        (not (string= (gnus-group-real-name group)
10928                                      (car where))))
10929               ;; If we fetched by Message-ID and the article came
10930               ;; from a different group, we fudge some bogus article
10931               ;; numbers for this article.
10932               (mail-header-set-number header gnus-reffed-article-number))
10933             (save-excursion
10934               (set-buffer gnus-summary-buffer)
10935               (decf gnus-reffed-article-number)
10936               (gnus-remove-header (mail-header-number header))
10937               (push header gnus-newsgroup-headers)
10938               (setq gnus-current-headers header)
10939               (push (mail-header-number header) gnus-newsgroup-limit)))
10940           header)))))
10941
10942 (defun gnus-remove-header (number)
10943   "Remove header NUMBER from `gnus-newsgroup-headers'."
10944   (if (and gnus-newsgroup-headers
10945            (= number (mail-header-number (car gnus-newsgroup-headers))))
10946       (pop gnus-newsgroup-headers)
10947     (let ((headers gnus-newsgroup-headers))
10948       (while (and (cdr headers)
10949                   (not (= number (mail-header-number (cadr headers)))))
10950         (pop headers))
10951       (when (cdr headers)
10952         (setcdr headers (cddr headers))))))
10953
10954 ;;;
10955 ;;; summary highlights
10956 ;;;
10957
10958 (defun gnus-highlight-selected-summary ()
10959   "Highlight selected article in summary buffer."
10960   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10961   (when gnus-summary-selected-face
10962     (save-excursion
10963       (let* ((beg (progn (beginning-of-line) (point)))
10964              (end (progn (end-of-line) (point)))
10965              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10966              (from (if (get-text-property beg gnus-mouse-face-prop)
10967                        beg
10968                      (or (next-single-property-change
10969                           beg gnus-mouse-face-prop nil end)
10970                          beg)))
10971              (to
10972               (if (= from end)
10973                   (- from 2)
10974                 (or (next-single-property-change
10975                      from gnus-mouse-face-prop nil end)
10976                     end))))
10977         ;; If no mouse-face prop on line we will have to = from = end,
10978         ;; so we highlight the entire line instead.
10979         (when (= (+ to 2) from)
10980           (setq from beg)
10981           (setq to end))
10982         (if gnus-newsgroup-selected-overlay
10983             ;; Move old overlay.
10984             (gnus-move-overlay
10985              gnus-newsgroup-selected-overlay from to (current-buffer))
10986           ;; Create new overlay.
10987           (gnus-overlay-put
10988            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10989            'face gnus-summary-selected-face))))))
10990
10991 (defvar gnus-summary-highlight-line-cached nil)
10992 (defvar gnus-summary-highlight-line-trigger nil)
10993
10994 (defun gnus-summary-highlight-line-0 ()
10995   (if (and (eq gnus-summary-highlight-line-trigger 
10996                gnus-summary-highlight)
10997            gnus-summary-highlight-line-cached)
10998       gnus-summary-highlight-line-cached
10999     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11000           gnus-summary-highlight-line-cached
11001           (let* ((cond (list 'cond))
11002                  (c cond)
11003                  (list gnus-summary-highlight))
11004             (while list
11005               (setcdr c (cons (list (caar list) (list 'quote (cdar list))) nil))
11006               (setq c (cdr c)
11007                     list (cdr list)))
11008             (gnus-byte-compile (list 'lambda nil cond))))))
11009
11010 (defun gnus-summary-highlight-line ()
11011   "Highlight current line according to `gnus-summary-highlight'."
11012   (let* ((beg (gnus-point-at-bol))
11013          (article (or (gnus-summary-article-number) gnus-current-article))
11014          (score (or (cdr (assq article
11015                                gnus-newsgroup-scored))
11016                     gnus-summary-default-score 0))
11017          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11018          (inhibit-read-only t)
11019          (default gnus-summary-default-score)
11020          (default-high gnus-summary-default-high-score)
11021          (default-low gnus-summary-default-low-score)
11022          (uncached (memq article gnus-newsgroup-undownloaded))
11023          (downloaded (not uncached)))
11024     (let ((face (funcall (gnus-summary-highlight-line-0))))
11025       (unless (eq face (get-text-property beg 'face))
11026         (gnus-put-text-property-excluding-characters-with-faces
11027          beg (gnus-point-at-eol) 'face
11028          (setq face (if (boundp face) (symbol-value face) face)))
11029         (when gnus-summary-highlight-line-function
11030           (funcall gnus-summary-highlight-line-function article face))))))
11031
11032 (defun gnus-update-read-articles (group unread &optional compute)
11033   "Update the list of read articles in GROUP.
11034 UNREAD is a sorted list."
11035   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11036          (entry (gnus-gethash group gnus-newsrc-hashtb))
11037          (info (nth 2 entry))
11038          (prev 1)
11039          read)
11040     (if (or (not info) (not active))
11041         ;; There is no info on this group if it was, in fact,
11042         ;; killed.  Gnus stores no information on killed groups, so
11043         ;; there's nothing to be done.
11044         ;; One could store the information somewhere temporarily,
11045         ;; perhaps...  Hmmm...
11046         ()
11047       ;; Remove any negative articles numbers.
11048       (while (and unread (< (car unread) 0))
11049         (setq unread (cdr unread)))
11050       ;; Remove any expired article numbers
11051       (while (and unread (< (car unread) (car active)))
11052         (setq unread (cdr unread)))
11053       ;; Compute the ranges of read articles by looking at the list of
11054       ;; unread articles.
11055       (while unread
11056         (when (/= (car unread) prev)
11057           (push (if (= prev (1- (car unread))) prev
11058                   (cons prev (1- (car unread))))
11059                 read))
11060         (setq prev (1+ (car unread)))
11061         (setq unread (cdr unread)))
11062       (when (<= prev (cdr active))
11063         (push (cons prev (cdr active)) read))
11064       (setq read (if (> (length read) 1) (nreverse read) read))
11065       (if compute
11066           read
11067         (save-excursion
11068           (let (setmarkundo)
11069             ;; Propagate the read marks to the backend.
11070             (when (gnus-check-backend-function 'request-set-mark group)
11071               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11072                     (add (gnus-remove-from-range read (gnus-info-read info))))
11073                 (when (or add del)
11074                   (unless (gnus-check-group group)
11075                     (error "Can't open server for %s" group))
11076                   (gnus-request-set-mark
11077                    group (delq nil (list (if add (list add 'add '(read)))
11078                                          (if del (list del 'del '(read))))))
11079                   (setq setmarkundo
11080                         `(gnus-request-set-mark
11081                           ,group
11082                           ',(delq nil (list
11083                                        (if del (list del 'add '(read)))
11084                                        (if add (list add 'del '(read))))))))))
11085             (set-buffer gnus-group-buffer)
11086             (gnus-undo-register
11087               `(progn
11088                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11089                  (gnus-info-set-read ',info ',(gnus-info-read info))
11090                  (gnus-get-unread-articles-in-group ',info
11091                                                     (gnus-active ,group))
11092                  (gnus-group-update-group ,group t)
11093                  ,setmarkundo))))
11094         ;; Enter this list into the group info.
11095         (gnus-info-set-read info read)
11096         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11097         (gnus-get-unread-articles-in-group info (gnus-active group))
11098         t))))
11099
11100 (defun gnus-offer-save-summaries ()
11101   "Offer to save all active summary buffers."
11102   (let (buffers)
11103     ;; Go through all buffers and find all summaries.
11104     (dolist (buffer (buffer-list))
11105       (when (and (setq buffer (buffer-name buffer))
11106                  (string-match "Summary" buffer)
11107                  (save-excursion
11108                    (set-buffer buffer)
11109                    ;; We check that this is, indeed, a summary buffer.
11110                    (and (eq major-mode 'gnus-summary-mode)
11111                         ;; Also make sure this isn't bogus.
11112                         gnus-newsgroup-prepared
11113                         ;; Also make sure that this isn't a
11114                         ;; dead summary buffer.
11115                         (not gnus-dead-summary-mode))))
11116         (push buffer buffers)))
11117     ;; Go through all these summary buffers and offer to save them.
11118     (when buffers
11119       (save-excursion
11120         (map-y-or-n-p
11121          "Update summary buffer %s? "
11122          (lambda (buf)
11123            (switch-to-buffer buf)
11124            (gnus-summary-exit))
11125          buffers)))))
11126
11127 (defun gnus-summary-setup-default-charset ()
11128   "Setup newsgroup default charset."
11129   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11130       (setq gnus-newsgroup-charset nil)
11131     (let* ((ignored-charsets
11132             (or gnus-newsgroup-ephemeral-ignored-charsets
11133                 (append
11134                  (and gnus-newsgroup-name
11135                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11136                  gnus-newsgroup-ignored-charsets))))
11137       (setq gnus-newsgroup-charset
11138             (or gnus-newsgroup-ephemeral-charset
11139                 (and gnus-newsgroup-name
11140                      (gnus-parameter-charset gnus-newsgroup-name))
11141                 gnus-default-charset))
11142       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11143            ignored-charsets))))
11144
11145 ;;;
11146 ;;; Mime Commands
11147 ;;;
11148
11149 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11150   "Display the current article buffer fully MIME-buttonized.
11151 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11152 treated as multipart/mixed."
11153   (interactive "P")
11154   (require 'gnus-art)
11155   (let ((gnus-unbuttonized-mime-types nil)
11156         (gnus-mime-display-multipart-as-mixed show-all-parts))
11157     (gnus-summary-show-article)))
11158
11159 (defun gnus-summary-repair-multipart (article)
11160   "Add a Content-Type header to a multipart article without one."
11161   (interactive (list (gnus-summary-article-number)))
11162   (gnus-with-article article
11163     (message-narrow-to-head)
11164     (message-remove-header "Mime-Version")
11165     (goto-char (point-max))
11166     (insert "Mime-Version: 1.0\n")
11167     (widen)
11168     (when (search-forward "\n--" nil t)
11169       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11170         (message-narrow-to-head)
11171         (message-remove-header "Content-Type")
11172         (goto-char (point-max))
11173         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11174                         separator))
11175         (widen))))
11176   (let (gnus-mark-article-hook)
11177     (gnus-summary-select-article t t nil article)))
11178
11179 (defun gnus-summary-toggle-display-buttonized ()
11180   "Toggle the buttonizing of the article buffer."
11181   (interactive)
11182   (require 'gnus-art)
11183   (if (setq gnus-inhibit-mime-unbuttonizing
11184             (not gnus-inhibit-mime-unbuttonizing))
11185       (let ((gnus-unbuttonized-mime-types nil))
11186         (gnus-summary-show-article))
11187     (gnus-summary-show-article)))
11188
11189 ;;;
11190 ;;; Generic summary marking commands
11191 ;;;
11192
11193 (defvar gnus-summary-marking-alist
11194   '((read gnus-del-mark "d")
11195     (unread gnus-unread-mark "u")
11196     (ticked gnus-ticked-mark "!")
11197     (dormant gnus-dormant-mark "?")
11198     (expirable gnus-expirable-mark "e"))
11199   "An alist of names/marks/keystrokes.")
11200
11201 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11202 (defvar gnus-summary-mark-map)
11203
11204 (defun gnus-summary-make-all-marking-commands ()
11205   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11206   (dolist (elem gnus-summary-marking-alist)
11207     (apply 'gnus-summary-make-marking-command elem)))
11208
11209 (defun gnus-summary-make-marking-command (name mark keystroke)
11210   (let ((map (make-sparse-keymap)))
11211     (define-key gnus-summary-generic-mark-map keystroke map)
11212     (dolist (lway `((next "next" next nil "n")
11213                     (next-unread "next unread" next t "N")
11214                     (prev "previous" prev nil "p")
11215                     (prev-unread "previous unread" prev t "P")
11216                     (nomove "" nil nil ,keystroke)))
11217       (let ((func (gnus-summary-make-marking-command-1
11218                    mark (car lway) lway name)))
11219         (setq func (eval func))
11220         (define-key map (nth 4 lway) func)))))
11221
11222 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11223   `(defun ,(intern
11224             (format "gnus-summary-put-mark-as-%s%s"
11225                     name (if (eq way 'nomove)
11226                              ""
11227                            (concat "-" (symbol-name way)))))
11228      (n)
11229      ,(format
11230        "Mark the current article as %s%s.
11231 If N, the prefix, then repeat N times.
11232 If N is negative, move in reverse order.
11233 The difference between N and the actual number of articles marked is
11234 returned."
11235        name (cadr lway))
11236      (interactive "p")
11237      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11238
11239 (defun gnus-summary-generic-mark (n mark move unread)
11240   "Mark N articles with MARK."
11241   (unless (eq major-mode 'gnus-summary-mode)
11242     (error "This command can only be used in the summary buffer"))
11243   (gnus-summary-show-thread)
11244   (let ((nummove
11245          (cond
11246           ((eq move 'next) 1)
11247           ((eq move 'prev) -1)
11248           (t 0))))
11249     (if (zerop nummove)
11250         (setq n 1)
11251       (when (< n 0)
11252         (setq n (abs n)
11253               nummove (* -1 nummove))))
11254     (while (and (> n 0)
11255                 (gnus-summary-mark-article nil mark)
11256                 (zerop (gnus-summary-next-subject nummove unread t)))
11257       (setq n (1- n)))
11258     (when (/= 0 n)
11259       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11260     (gnus-summary-recenter)
11261     (gnus-summary-position-point)
11262     (gnus-set-mode-line 'summary)
11263     n))
11264
11265 (defun gnus-summary-insert-articles (articles)
11266   (when (setq articles
11267               (gnus-sorted-difference articles
11268                                       (mapcar (lambda (h)
11269                                                 (mail-header-number h))
11270                                               gnus-newsgroup-headers)))
11271     (setq gnus-newsgroup-headers
11272           (merge 'list
11273                  gnus-newsgroup-headers
11274                  (gnus-fetch-headers articles)
11275                  'gnus-article-sort-by-number))
11276     ;; Suppress duplicates?
11277     (when gnus-suppress-duplicates
11278       (gnus-dup-suppress-articles))
11279
11280     ;; We might want to build some more threads first.
11281     (when (and gnus-fetch-old-headers
11282                (eq gnus-headers-retrieved-by 'nov))
11283       (if (eq gnus-fetch-old-headers 'invisible)
11284           (gnus-build-all-threads)
11285         (gnus-build-old-threads)))
11286     ;; Let the Gnus agent mark articles as read.
11287     (when gnus-agent
11288       (gnus-agent-get-undownloaded-list))
11289     ;; Remove list identifiers from subject
11290     (when gnus-list-identifiers
11291       (gnus-summary-remove-list-identifiers))
11292     ;; First and last article in this newsgroup.
11293     (when gnus-newsgroup-headers
11294       (setq gnus-newsgroup-begin
11295             (mail-header-number (car gnus-newsgroup-headers))
11296             gnus-newsgroup-end
11297             (mail-header-number
11298              (gnus-last-element gnus-newsgroup-headers))))
11299     (when gnus-use-scoring
11300       (gnus-possibly-score-headers))))
11301
11302 (defun gnus-summary-insert-old-articles (&optional all)
11303   "Insert all old articles in this group.
11304 If ALL is non-nil, already read articles become readable.
11305 If ALL is a number, fetch this number of articles."
11306   (interactive "P")
11307   (prog1
11308       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11309             older len)
11310         (setq older
11311               ;; Some nntp servers lie about their active range.  When
11312               ;; this happens, the active range can be in the millions.
11313               ;; Use a compressed range to avoid creating a huge list.
11314               (gnus-range-difference (list gnus-newsgroup-active) old))
11315         (setq len (gnus-range-length older))
11316         (cond
11317          ((null older) nil)
11318          ((numberp all)
11319           (if (< all len)
11320               (let ((older-range (nreverse older)))
11321                 (setq older nil)
11322
11323                 (while (> all 0)
11324                   (let* ((r (pop older-range))
11325                          (min (if (numberp r) r (car r)))
11326                          (max (if (numberp r) r (cdr r))))
11327                     (while (and (<= min max)
11328                                 (> all 0))
11329                       (push max older)
11330                       (setq all (1- all)
11331                             max (1- max))))))
11332             (setq older (gnus-uncompress-range older))))
11333          (all
11334           (setq older (gnus-uncompress-range older)))
11335          (t
11336           (when (and (numberp gnus-large-newsgroup)
11337                    (> len gnus-large-newsgroup))
11338               (let* ((cursor-in-echo-area nil)
11339                      (initial (gnus-parameter-large-newsgroup-initial
11340                                gnus-newsgroup-name))
11341                      (input
11342                       (read-string
11343                        (format
11344                         "How many articles from %s (%s %d): "
11345                         (gnus-limit-string
11346                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11347                         (if initial "max" "default")
11348                         len)
11349                        (if initial
11350                            (cons (number-to-string initial)
11351                                  0)))))
11352                 (unless (string-match "^[ \t]*$" input)
11353                   (setq all (string-to-number input))
11354                   (if (< all len)
11355                       (let ((older-range (nreverse older)))
11356                         (setq older nil)
11357
11358                         (while (> all 0)
11359                           (let* ((r (pop older-range))
11360                                  (min (if (numberp r) r (car r)))
11361                                  (max (if (numberp r) r (cdr r))))
11362                             (while (and (<= min max)
11363                                         (> all 0))
11364                               (push max older)
11365                               (setq all (1- all)
11366                                     max (1- max))))))))))
11367           (setq older (gnus-uncompress-range older))))
11368         (if (not older)
11369             (message "No old news.")
11370           (gnus-summary-insert-articles older)
11371           (gnus-summary-limit (gnus-sorted-nunion old older))))
11372     (gnus-summary-position-point)))
11373
11374 (defun gnus-summary-insert-new-articles ()
11375   "Insert all new articles in this group."
11376   (interactive)
11377   (prog1
11378       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11379             (old-active gnus-newsgroup-active)
11380             (nnmail-fetched-sources (list t))
11381             i new)
11382         (setq gnus-newsgroup-active
11383               (gnus-activate-group gnus-newsgroup-name 'scan))
11384         (setq i (cdr gnus-newsgroup-active))
11385         (while (> i (cdr old-active))
11386           (push i new)
11387           (decf i))
11388         (if (not new)
11389             (message "No gnus is bad news.")
11390           (gnus-summary-insert-articles new)
11391           (setq gnus-newsgroup-unreads
11392                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11393           (gnus-summary-limit (gnus-sorted-nunion old new))))
11394     (gnus-summary-position-point)))
11395
11396 (gnus-summary-make-all-marking-commands)
11397
11398 (gnus-ems-redefine)
11399
11400 (provide 'gnus-sum)
11401
11402 (run-hooks 'gnus-sum-load-hook)
11403
11404 ;;; gnus-sum.el ends here