* deuglify.el (gnus-outlook-deuglify-attrib-verb-regexp): Added
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
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-outlook-unwrap-lines "deuglify" nil t)
50 (autoload 'gnus-outlook-repair-attribution "deuglify" nil t)
51 (autoload 'gnus-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     "k" gnus-article-outlook-deuglify-article) ;; mnemonic: outloo*k*
1800
1801   (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1802     ;; mnemonic: deuglif*Y*
1803     "u" gnus-outlook-unwrap-lines
1804     "a" gnus-outlook-repair-attribution
1805     "c" gnus-outlook-rearrange-citation
1806     "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1807
1808   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1809     "a" gnus-article-hide
1810     "h" gnus-article-hide-headers
1811     "b" gnus-article-hide-boring-headers
1812     "s" gnus-article-hide-signature
1813     "c" gnus-article-hide-citation
1814     "C" gnus-article-hide-citation-in-followups
1815     "l" gnus-article-hide-list-identifiers
1816     "p" gnus-article-hide-pgp
1817     "B" gnus-article-strip-banner
1818     "P" gnus-article-hide-pem
1819     "\C-c" gnus-article-hide-citation-maybe)
1820
1821   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1822     "a" gnus-article-highlight
1823     "h" gnus-article-highlight-headers
1824     "c" gnus-article-highlight-citation
1825     "s" gnus-article-highlight-signature)
1826
1827   (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1828     "f" gnus-article-treat-fold-headers
1829     "u" gnus-article-treat-unfold-headers
1830     "n" gnus-article-treat-fold-newsgroups)
1831
1832   (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1833     "x" gnus-article-display-x-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-outlook-unwrap-lines t]
2122                ["Repair attribution" gnus-outlook-repair-attribution t]
2123                ["Rearrange citation" gnus-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 t]
2296          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2297         ("Mark Various"
2298          ["Tick" gnus-summary-tick-article-forward t]
2299          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2300          ["Remove marks" gnus-summary-clear-mark-forward t]
2301          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2302          ["Set bookmark" gnus-summary-set-bookmark t]
2303          ["Remove bookmark" gnus-summary-remove-bookmark t])
2304         ("Limit to"
2305          ["Marks..." gnus-summary-limit-to-marks t]
2306          ["Subject..." gnus-summary-limit-to-subject t]
2307          ["Author..." gnus-summary-limit-to-author t]
2308          ["Age..." gnus-summary-limit-to-age t]
2309          ["Extra..." gnus-summary-limit-to-extra t]
2310          ["Score..." gnus-summary-limit-to-score t]
2311          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2312          ["Unread" gnus-summary-limit-to-unread t]
2313          ["Unseen" gnus-summary-limit-to-unseen t]
2314          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2315          ["Next articles" gnus-summary-limit-to-articles t]
2316          ["Pop limit" gnus-summary-pop-limit t]
2317          ["Show dormant" gnus-summary-limit-include-dormant t]
2318          ["Hide childless dormant"
2319           gnus-summary-limit-exclude-childless-dormant t]
2320          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2321          ["Hide marked" gnus-summary-limit-exclude-marks t]
2322          ["Show expunged" gnus-summary-limit-include-expunged t])
2323         ("Process Mark"
2324          ["Set mark" gnus-summary-mark-as-processable t]
2325          ["Remove mark" gnus-summary-unmark-as-processable t]
2326          ["Remove all marks" gnus-summary-unmark-all-processable t]
2327          ["Mark above" gnus-uu-mark-over t]
2328          ["Mark series" gnus-uu-mark-series t]
2329          ["Mark region" gnus-uu-mark-region t]
2330          ["Unmark region" gnus-uu-unmark-region t]
2331          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2332          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2333          ["Mark all" gnus-uu-mark-all t]
2334          ["Mark buffer" gnus-uu-mark-buffer t]
2335          ["Mark sparse" gnus-uu-mark-sparse t]
2336          ["Mark thread" gnus-uu-mark-thread t]
2337          ["Unmark thread" gnus-uu-unmark-thread t]
2338          ("Process Mark Sets"
2339           ["Kill" gnus-summary-kill-process-mark t]
2340           ["Yank" gnus-summary-yank-process-mark
2341            gnus-newsgroup-process-stack]
2342           ["Save" gnus-summary-save-process-mark t]))
2343         ("Scroll article"
2344          ["Page forward" gnus-summary-next-page
2345           ,@(if (featurep 'xemacs) '(t)
2346               '(:help "Show next page of article"))]
2347          ["Page backward" gnus-summary-prev-page
2348           ,@(if (featurep 'xemacs) '(t)
2349               '(:help "Show previous page of article"))]
2350          ["Line forward" gnus-summary-scroll-up t])
2351         ("Move"
2352          ["Next unread article" gnus-summary-next-unread-article t]
2353          ["Previous unread article" gnus-summary-prev-unread-article t]
2354          ["Next article" gnus-summary-next-article t]
2355          ["Previous article" gnus-summary-prev-article t]
2356          ["Next unread subject" gnus-summary-next-unread-subject t]
2357          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2358          ["Next article same subject" gnus-summary-next-same-subject t]
2359          ["Previous article same subject" gnus-summary-prev-same-subject t]
2360          ["First unread article" gnus-summary-first-unread-article t]
2361          ["Best unread article" gnus-summary-best-unread-article t]
2362          ["Go to subject number..." gnus-summary-goto-subject t]
2363          ["Go to article number..." gnus-summary-goto-article t]
2364          ["Go to the last article" gnus-summary-goto-last-article t]
2365          ["Pop article off history" gnus-summary-pop-article t])
2366         ("Sort"
2367          ["Sort by number" gnus-summary-sort-by-number t]
2368          ["Sort by author" gnus-summary-sort-by-author t]
2369          ["Sort by subject" gnus-summary-sort-by-subject t]
2370          ["Sort by date" gnus-summary-sort-by-date t]
2371          ["Sort by score" gnus-summary-sort-by-score t]
2372          ["Sort by lines" gnus-summary-sort-by-lines t]
2373          ["Sort by characters" gnus-summary-sort-by-chars t]
2374          ["Randomize" gnus-summary-sort-by-random t]
2375          ["Original sort" gnus-summary-sort-by-original t])
2376         ("Help"
2377          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2378          ["Describe group" gnus-summary-describe-group t]
2379          ["Fetch charter" gnus-group-fetch-charter
2380           ,@(if (featurep 'xemacs) nil
2381               '(:help "Display the charter of the current group"))]
2382          ["Fetch control message" gnus-group-fetch-control
2383           ,@(if (featurep 'xemacs) nil
2384               '(:help "Display the archived control message for the current group"))]
2385          ["Read manual" gnus-info-find-node t])
2386         ("Modes"
2387          ["Pick and read" gnus-pick-mode t]
2388          ["Binary" gnus-binary-mode t])
2389         ("Regeneration"
2390          ["Regenerate" gnus-summary-prepare t]
2391          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2392          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2393          ["Toggle threading" gnus-summary-toggle-threads t])
2394         ["See old articles" gnus-summary-insert-old-articles t]
2395         ["See new articles" gnus-summary-insert-new-articles t]
2396         ["Filter articles..." gnus-summary-execute-command t]
2397         ["Run command on subjects..." gnus-summary-universal-argument t]
2398         ["Search articles forward..." gnus-summary-search-article-forward t]
2399         ["Search articles backward..." gnus-summary-search-article-backward t]
2400         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2401         ["Expand window" gnus-summary-expand-window t]
2402         ["Expire expirable articles" gnus-summary-expire-articles
2403          (gnus-check-backend-function
2404           'request-expire-articles gnus-newsgroup-name)]
2405         ["Edit local kill file" gnus-summary-edit-local-kill t]
2406         ["Edit main kill file" gnus-summary-edit-global-kill t]
2407         ["Edit group parameters" gnus-summary-edit-parameters t]
2408         ["Customize group parameters" gnus-summary-customize-parameters t]
2409         ["Send a bug report" gnus-bug t]
2410         ("Exit"
2411          ["Catchup and exit" gnus-summary-catchup-and-exit
2412           ,@(if (featurep 'xemacs) '(t)
2413               '(:help "Mark unread articles in this group as read, then exit"))]
2414          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2415          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2416          ["Exit group" gnus-summary-exit
2417           ,@(if (featurep 'xemacs) '(t)
2418               '(:help "Exit current group, return to group selection mode"))]
2419          ["Exit group without updating" gnus-summary-exit-no-update t]
2420          ["Exit and goto next group" gnus-summary-next-group t]
2421          ["Exit and goto prev group" gnus-summary-prev-group t]
2422          ["Reselect group" gnus-summary-reselect-current-group t]
2423          ["Rescan group" gnus-summary-rescan-group t]
2424          ["Update dribble" gnus-summary-save-newsrc t])))
2425
2426     (gnus-run-hooks 'gnus-summary-menu-hook)))
2427
2428 (defvar gnus-summary-tool-bar-map nil)
2429
2430 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2431 (defun gnus-summary-make-tool-bar ()
2432   (if (and (fboundp 'tool-bar-add-item-from-menu)
2433            (default-value 'tool-bar-mode)
2434            (not gnus-summary-tool-bar-map))
2435       (setq gnus-summary-tool-bar-map
2436             (let ((tool-bar-map (make-sparse-keymap))
2437                   (load-path (mm-image-load-path)))
2438               (tool-bar-add-item-from-menu
2439                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2440               (tool-bar-add-item-from-menu
2441                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2442               (tool-bar-add-item-from-menu
2443                'gnus-summary-post-news "post" gnus-summary-mode-map)
2444               (tool-bar-add-item-from-menu
2445                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2446               (tool-bar-add-item-from-menu
2447                'gnus-summary-followup "followup" gnus-summary-mode-map)
2448               (tool-bar-add-item-from-menu
2449                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2450               (tool-bar-add-item-from-menu
2451                'gnus-summary-reply "reply" gnus-summary-mode-map)
2452               (tool-bar-add-item-from-menu
2453                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2454               (tool-bar-add-item-from-menu
2455                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2456               (tool-bar-add-item-from-menu
2457                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2458               (tool-bar-add-item-from-menu
2459                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2460               (tool-bar-add-item-from-menu
2461                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2462               (tool-bar-add-item-from-menu
2463                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2464               (tool-bar-add-item-from-menu
2465                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2466               (tool-bar-add-item-from-menu
2467                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2468               tool-bar-map)))
2469   (if gnus-summary-tool-bar-map
2470       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2471
2472 (defun gnus-score-set-default (var value)
2473   "A version of set that updates the GNU Emacs menu-bar."
2474   (set var value)
2475   ;; It is the message that forces the active status to be updated.
2476   (message ""))
2477
2478 (defun gnus-make-score-map (type)
2479   "Make a summary score map of type TYPE."
2480   (if t
2481       nil
2482     (let ((headers '(("author" "from" string)
2483                      ("subject" "subject" string)
2484                      ("article body" "body" string)
2485                      ("article head" "head" string)
2486                      ("xref" "xref" string)
2487                      ("extra header" "extra" string)
2488                      ("lines" "lines" number)
2489                      ("followups to author" "followup" string)))
2490           (types '((number ("less than" <)
2491                            ("greater than" >)
2492                            ("equal" =))
2493                    (string ("substring" s)
2494                            ("exact string" e)
2495                            ("fuzzy string" f)
2496                            ("regexp" r))))
2497           (perms '(("temporary" (current-time-string))
2498                    ("permanent" nil)
2499                    ("immediate" now)))
2500           header)
2501       (list
2502        (apply
2503         'nconc
2504         (list
2505          (if (eq type 'lower)
2506              "Lower score"
2507            "Increase score"))
2508         (let (outh)
2509           (while headers
2510             (setq header (car headers))
2511             (setq outh
2512                   (cons
2513                    (apply
2514                     'nconc
2515                     (list (car header))
2516                     (let ((ts (cdr (assoc (nth 2 header) types)))
2517                           outt)
2518                       (while ts
2519                         (setq outt
2520                               (cons
2521                                (apply
2522                                 'nconc
2523                                 (list (caar ts))
2524                                 (let ((ps perms)
2525                                       outp)
2526                                   (while ps
2527                                     (setq outp
2528                                           (cons
2529                                            (vector
2530                                             (caar ps)
2531                                             (list
2532                                              'gnus-summary-score-entry
2533                                              (nth 1 header)
2534                                              (if (or (string= (nth 1 header)
2535                                                               "head")
2536                                                      (string= (nth 1 header)
2537                                                               "body"))
2538                                                  ""
2539                                                (list 'gnus-summary-header
2540                                                      (nth 1 header)))
2541                                              (list 'quote (nth 1 (car ts)))
2542                                              (list 'gnus-score-delta-default
2543                                                    nil)
2544                                              (nth 1 (car ps))
2545                                              t)
2546                                             t)
2547                                            outp))
2548                                     (setq ps (cdr ps)))
2549                                   (list (nreverse outp))))
2550                                outt))
2551                         (setq ts (cdr ts)))
2552                       (list (nreverse outt))))
2553                    outh))
2554             (setq headers (cdr headers)))
2555           (list (nreverse outh))))))))
2556
2557 \f
2558
2559 (defun gnus-summary-mode (&optional group)
2560   "Major mode for reading articles.
2561
2562 All normal editing commands are switched off.
2563 \\<gnus-summary-mode-map>
2564 Each line in this buffer represents one article.  To read an
2565 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2566 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2567 respectively.
2568
2569 You can also post articles and send mail from this buffer.  To
2570 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2571 of an article, type `\\[gnus-summary-reply]'.
2572
2573 There are approx. one gazillion commands you can execute in this
2574 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2575
2576 The following commands are available:
2577
2578 \\{gnus-summary-mode-map}"
2579   (interactive)
2580   (kill-all-local-variables)
2581   (when (gnus-visual-p 'summary-menu 'menu)
2582     (gnus-summary-make-menu-bar)
2583     (gnus-summary-make-tool-bar))
2584   (gnus-summary-make-local-variables)
2585   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2586     (gnus-summary-make-local-variables))
2587   (gnus-make-thread-indent-array)
2588   (gnus-simplify-mode-line)
2589   (setq major-mode 'gnus-summary-mode)
2590   (setq mode-name "Summary")
2591   (make-local-variable 'minor-mode-alist)
2592   (use-local-map gnus-summary-mode-map)
2593   (buffer-disable-undo)
2594   (setq buffer-read-only t)             ;Disable modification
2595   (setq truncate-lines t)
2596   (setq selective-display t)
2597   (setq selective-display-ellipses t)   ;Display `...'
2598   (gnus-summary-set-display-table)
2599   (gnus-set-default-directory)
2600   (setq gnus-newsgroup-name group)
2601   (make-local-variable 'gnus-summary-line-format)
2602   (make-local-variable 'gnus-summary-line-format-spec)
2603   (make-local-variable 'gnus-summary-dummy-line-format)
2604   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2605   (make-local-variable 'gnus-summary-mark-positions)
2606   (make-local-hook 'pre-command-hook)
2607   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2608   (gnus-run-hooks 'gnus-summary-mode-hook)
2609   (turn-on-gnus-mailing-list-mode)
2610   (mm-enable-multibyte)
2611   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2612   (gnus-update-summary-mark-positions))
2613
2614 (defun gnus-summary-make-local-variables ()
2615   "Make all the local summary buffer variables."
2616   (let (global)
2617     (dolist (local gnus-summary-local-variables)
2618       (if (consp local)
2619           (progn
2620             (if (eq (cdr local) 'global)
2621                 ;; Copy the global value of the variable.
2622                 (setq global (symbol-value (car local)))
2623               ;; Use the value from the list.
2624               (setq global (eval (cdr local))))
2625             (set (make-local-variable (car local)) global))
2626         ;; Simple nil-valued local variable.
2627         (set (make-local-variable local) nil)))))
2628
2629 (defun gnus-summary-clear-local-variables ()
2630   (let ((locals gnus-summary-local-variables))
2631     (while locals
2632       (if (consp (car locals))
2633           (and (vectorp (caar locals))
2634                (set (caar locals) nil))
2635         (and (vectorp (car locals))
2636              (set (car locals) nil)))
2637       (setq locals (cdr locals)))))
2638
2639 ;; Summary data functions.
2640
2641 (defmacro gnus-data-number (data)
2642   `(car ,data))
2643
2644 (defmacro gnus-data-set-number (data number)
2645   `(setcar ,data ,number))
2646
2647 (defmacro gnus-data-mark (data)
2648   `(nth 1 ,data))
2649
2650 (defmacro gnus-data-set-mark (data mark)
2651   `(setcar (nthcdr 1 ,data) ,mark))
2652
2653 (defmacro gnus-data-pos (data)
2654   `(nth 2 ,data))
2655
2656 (defmacro gnus-data-set-pos (data pos)
2657   `(setcar (nthcdr 2 ,data) ,pos))
2658
2659 (defmacro gnus-data-header (data)
2660   `(nth 3 ,data))
2661
2662 (defmacro gnus-data-set-header (data header)
2663   `(setf (nth 3 ,data) ,header))
2664
2665 (defmacro gnus-data-level (data)
2666   `(nth 4 ,data))
2667
2668 (defmacro gnus-data-unread-p (data)
2669   `(= (nth 1 ,data) gnus-unread-mark))
2670
2671 (defmacro gnus-data-read-p (data)
2672   `(/= (nth 1 ,data) gnus-unread-mark))
2673
2674 (defmacro gnus-data-pseudo-p (data)
2675   `(consp (nth 3 ,data)))
2676
2677 (defmacro gnus-data-find (number)
2678   `(assq ,number gnus-newsgroup-data))
2679
2680 (defmacro gnus-data-find-list (number &optional data)
2681   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2682      (memq (assq ,number bdata)
2683            bdata)))
2684
2685 (defmacro gnus-data-make (number mark pos header level)
2686   `(list ,number ,mark ,pos ,header ,level))
2687
2688 (defun gnus-data-enter (after-article number mark pos header level offset)
2689   (let ((data (gnus-data-find-list after-article)))
2690     (unless data
2691       (error "No such article: %d" after-article))
2692     (setcdr data (cons (gnus-data-make number mark pos header level)
2693                        (cdr data)))
2694     (setq gnus-newsgroup-data-reverse nil)
2695     (gnus-data-update-list (cddr data) offset)))
2696
2697 (defun gnus-data-enter-list (after-article list &optional offset)
2698   (when list
2699     (let ((data (and after-article (gnus-data-find-list after-article)))
2700           (ilist list))
2701       (if (not (or data
2702                    after-article))
2703           (let ((odata gnus-newsgroup-data))
2704             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2705             (when offset
2706               (gnus-data-update-list odata offset)))
2707       ;; Find the last element in the list to be spliced into the main
2708         ;; list.
2709         (while (cdr list)
2710           (setq list (cdr list)))
2711         (if (not data)
2712             (progn
2713               (setcdr list gnus-newsgroup-data)
2714               (setq gnus-newsgroup-data ilist)
2715               (when offset
2716                 (gnus-data-update-list (cdr list) offset)))
2717           (setcdr list (cdr data))
2718           (setcdr data ilist)
2719           (when offset
2720             (gnus-data-update-list (cdr list) offset))))
2721       (setq gnus-newsgroup-data-reverse nil))))
2722
2723 (defun gnus-data-remove (article &optional offset)
2724   (let ((data gnus-newsgroup-data))
2725     (if (= (gnus-data-number (car data)) article)
2726         (progn
2727           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2728                 gnus-newsgroup-data-reverse nil)
2729           (when offset
2730             (gnus-data-update-list gnus-newsgroup-data offset)))
2731       (while (cdr data)
2732         (when (= (gnus-data-number (cadr data)) article)
2733           (setcdr data (cddr data))
2734           (when offset
2735             (gnus-data-update-list (cdr data) offset))
2736           (setq data nil
2737                 gnus-newsgroup-data-reverse nil))
2738         (setq data (cdr data))))))
2739
2740 (defmacro gnus-data-list (backward)
2741   `(if ,backward
2742        (or gnus-newsgroup-data-reverse
2743            (setq gnus-newsgroup-data-reverse
2744                  (reverse gnus-newsgroup-data)))
2745      gnus-newsgroup-data))
2746
2747 (defun gnus-data-update-list (data offset)
2748   "Add OFFSET to the POS of all data entries in DATA."
2749   (setq gnus-newsgroup-data-reverse nil)
2750   (while data
2751     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2752     (setq data (cdr data))))
2753
2754 (defun gnus-summary-article-pseudo-p (article)
2755   "Say whether this article is a pseudo article or not."
2756   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2757
2758 (defmacro gnus-summary-article-sparse-p (article)
2759   "Say whether this article is a sparse article or not."
2760   `(memq ,article gnus-newsgroup-sparse))
2761
2762 (defmacro gnus-summary-article-ancient-p (article)
2763   "Say whether this article is a sparse article or not."
2764   `(memq ,article gnus-newsgroup-ancient))
2765
2766 (defun gnus-article-parent-p (number)
2767   "Say whether this article is a parent or not."
2768   (let ((data (gnus-data-find-list number)))
2769     (and (cdr data)              ; There has to be an article after...
2770          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2771             (gnus-data-level (nth 1 data))))))
2772
2773 (defun gnus-article-children (number)
2774   "Return a list of all children to NUMBER."
2775   (let* ((data (gnus-data-find-list number))
2776          (level (gnus-data-level (car data)))
2777          children)
2778     (setq data (cdr data))
2779     (while (and data
2780                 (= (gnus-data-level (car data)) (1+ level)))
2781       (push (gnus-data-number (car data)) children)
2782       (setq data (cdr data)))
2783     children))
2784
2785 (defmacro gnus-summary-skip-intangible ()
2786   "If the current article is intangible, then jump to a different article."
2787   '(let ((to (get-text-property (point) 'gnus-intangible)))
2788      (and to (gnus-summary-goto-subject to))))
2789
2790 (defmacro gnus-summary-article-intangible-p ()
2791   "Say whether this article is intangible or not."
2792   '(get-text-property (point) 'gnus-intangible))
2793
2794 (defun gnus-article-read-p (article)
2795   "Say whether ARTICLE is read or not."
2796   (not (or (memq article gnus-newsgroup-marked)
2797            (memq article gnus-newsgroup-spam-marked)
2798            (memq article gnus-newsgroup-unreads)
2799            (memq article gnus-newsgroup-unselected)
2800            (memq article gnus-newsgroup-dormant))))
2801
2802 ;; Some summary mode macros.
2803
2804 (defmacro gnus-summary-article-number ()
2805   "The article number of the article on the current line.
2806 If there isn't an article number here, then we return the current
2807 article number."
2808   '(progn
2809      (gnus-summary-skip-intangible)
2810      (or (get-text-property (point) 'gnus-number)
2811          (gnus-summary-last-subject))))
2812
2813 (defmacro gnus-summary-article-header (&optional number)
2814   "Return the header of article NUMBER."
2815   `(gnus-data-header (gnus-data-find
2816                       ,(or number '(gnus-summary-article-number)))))
2817
2818 (defmacro gnus-summary-thread-level (&optional number)
2819   "Return the level of thread that starts with article NUMBER."
2820   `(if (and (eq gnus-summary-make-false-root 'dummy)
2821             (get-text-property (point) 'gnus-intangible))
2822        0
2823      (gnus-data-level (gnus-data-find
2824                        ,(or number '(gnus-summary-article-number))))))
2825
2826 (defmacro gnus-summary-article-mark (&optional number)
2827   "Return the mark of article NUMBER."
2828   `(gnus-data-mark (gnus-data-find
2829                     ,(or number '(gnus-summary-article-number)))))
2830
2831 (defmacro gnus-summary-article-pos (&optional number)
2832   "Return the position of the line of article NUMBER."
2833   `(gnus-data-pos (gnus-data-find
2834                    ,(or number '(gnus-summary-article-number)))))
2835
2836 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2837 (defmacro gnus-summary-article-subject (&optional number)
2838   "Return current subject string or nil if nothing."
2839   `(let ((headers
2840           ,(if number
2841                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2842              '(gnus-data-header (assq (gnus-summary-article-number)
2843                                       gnus-newsgroup-data)))))
2844      (and headers
2845           (vectorp headers)
2846           (mail-header-subject headers))))
2847
2848 (defmacro gnus-summary-article-score (&optional number)
2849   "Return current article score."
2850   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2851                   gnus-newsgroup-scored))
2852        gnus-summary-default-score 0))
2853
2854 (defun gnus-summary-article-children (&optional number)
2855   "Return a list of article numbers that are children of article NUMBER."
2856   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2857          (level (gnus-data-level (car data)))
2858          l children)
2859     (while (and (setq data (cdr data))
2860                 (> (setq l (gnus-data-level (car data))) level))
2861       (and (= (1+ level) l)
2862            (push (gnus-data-number (car data))
2863                  children)))
2864     (nreverse children)))
2865
2866 (defun gnus-summary-article-parent (&optional number)
2867   "Return the article number of the parent of article NUMBER."
2868   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2869                                     (gnus-data-list t)))
2870          (level (gnus-data-level (car data))))
2871     (if (zerop level)
2872         ()                              ; This is a root.
2873       ;; We search until we find an article with a level less than
2874       ;; this one.  That function has to be the parent.
2875       (while (and (setq data (cdr data))
2876                   (not (< (gnus-data-level (car data)) level))))
2877       (and data (gnus-data-number (car data))))))
2878
2879 (defun gnus-unread-mark-p (mark)
2880   "Say whether MARK is the unread mark."
2881   (= mark gnus-unread-mark))
2882
2883 (defun gnus-read-mark-p (mark)
2884   "Say whether MARK is one of the marks that mark as read.
2885 This is all marks except unread, ticked, dormant, and expirable."
2886   (not (or (= mark gnus-unread-mark)
2887            (= mark gnus-ticked-mark)
2888            (= mark gnus-spam-mark)
2889            (= mark gnus-dormant-mark)
2890            (= mark gnus-expirable-mark))))
2891
2892 (defmacro gnus-article-mark (number)
2893   "Return the MARK of article NUMBER.
2894 This macro should only be used when computing the mark the \"first\"
2895 time; i.e., when generating the summary lines.  After that,
2896 `gnus-summary-article-mark' should be used to examine the
2897 marks of articles."
2898   `(cond
2899     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2900     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2901     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2902     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2903     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2904     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2905     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2906     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2907            gnus-ancient-mark))))
2908
2909 ;; Saving hidden threads.
2910
2911 (defmacro gnus-save-hidden-threads (&rest forms)
2912   "Save hidden threads, eval FORMS, and restore the hidden threads."
2913   (let ((config (make-symbol "config")))
2914     `(let ((,config (gnus-hidden-threads-configuration)))
2915        (unwind-protect
2916            (save-excursion
2917              ,@forms)
2918          (gnus-restore-hidden-threads-configuration ,config)))))
2919 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2920 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2921
2922 (defun gnus-data-compute-positions ()
2923   "Compute the positions of all articles."
2924   (setq gnus-newsgroup-data-reverse nil)
2925   (let ((data gnus-newsgroup-data))
2926     (save-excursion
2927       (gnus-save-hidden-threads
2928         (gnus-summary-show-all-threads)
2929         (goto-char (point-min))
2930         (while data
2931           (while (get-text-property (point) 'gnus-intangible)
2932             (forward-line 1))
2933           (gnus-data-set-pos (car data) (+ (point) 3))
2934           (setq data (cdr data))
2935           (forward-line 1))))))
2936
2937 (defun gnus-hidden-threads-configuration ()
2938   "Return the current hidden threads configuration."
2939   (save-excursion
2940     (let (config)
2941       (goto-char (point-min))
2942       (while (search-forward "\r" nil t)
2943         (push (1- (point)) config))
2944       config)))
2945
2946 (defun gnus-restore-hidden-threads-configuration (config)
2947   "Restore hidden threads configuration from CONFIG."
2948   (save-excursion
2949     (let (point buffer-read-only)
2950       (while (setq point (pop config))
2951         (when (and (< point (point-max))
2952                    (goto-char point)
2953                    (eq (char-after) ?\n))
2954           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2955
2956 ;; Various summary mode internalish functions.
2957
2958 (defun gnus-mouse-pick-article (e)
2959   (interactive "e")
2960   (mouse-set-point e)
2961   (gnus-summary-next-page nil t))
2962
2963 (defun gnus-summary-set-display-table ()
2964   "Change the display table.
2965 Odd characters have a tendency to mess
2966 up nicely formatted displays - we make all possible glyphs
2967 display only a single character."
2968
2969   ;; We start from the standard display table, if any.
2970   (let ((table (or (copy-sequence standard-display-table)
2971                    (make-display-table)))
2972         (i 32))
2973     ;; Nix out all the control chars...
2974     (while (>= (setq i (1- i)) 0)
2975       (aset table i [??]))
2976    ;; ... but not newline and cr, of course.  (cr is necessary for the
2977     ;; selective display).
2978     (aset table ?\n nil)
2979     (aset table ?\r nil)
2980     ;; We keep TAB as well.
2981     (aset table ?\t nil)
2982     ;; We nix out any glyphs over 126 that are not set already.
2983     (let ((i 256))
2984       (while (>= (setq i (1- i)) 127)
2985         ;; Only modify if the entry is nil.
2986         (unless (aref table i)
2987           (aset table i [??]))))
2988     (setq buffer-display-table table)))
2989
2990 (defun gnus-summary-set-article-display-arrow (pos)
2991   "Update the overlay arrow to point to line at position POS."
2992   (when (and gnus-summary-display-arrow
2993              (boundp 'overlay-arrow-position)
2994              (boundp 'overlay-arrow-string))
2995     (save-excursion
2996       (goto-char pos)
2997       (beginning-of-line)
2998       (unless overlay-arrow-position
2999         (setq overlay-arrow-position (make-marker)))
3000       (setq overlay-arrow-string "=>"
3001             overlay-arrow-position (set-marker overlay-arrow-position
3002                                                (point)
3003                                                (current-buffer))))))
3004
3005 (defun gnus-summary-buffer-name (group)
3006   "Return the summary buffer name of GROUP."
3007   (concat "*Summary " (gnus-group-decoded-name group) "*"))
3008
3009 (defun gnus-summary-setup-buffer (group)
3010   "Initialize summary buffer."
3011   (let ((buffer (gnus-summary-buffer-name group))
3012         (dead-name (concat "*Dead Summary "
3013                            (gnus-group-decoded-name group) "*")))
3014     ;; If a dead summary buffer exists, we kill it.
3015     (when (gnus-buffer-live-p dead-name)
3016       (gnus-kill-buffer dead-name))
3017     (if (get-buffer buffer)
3018         (progn
3019           (set-buffer buffer)
3020           (setq gnus-summary-buffer (current-buffer))
3021           (not gnus-newsgroup-prepared))
3022       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3023       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3024       (gnus-summary-mode group)
3025       (when gnus-carpal
3026         (gnus-carpal-setup-buffer 'summary))
3027       (unless gnus-single-article-buffer
3028         (make-local-variable 'gnus-article-buffer)
3029         (make-local-variable 'gnus-article-current)
3030         (make-local-variable 'gnus-original-article-buffer))
3031       (setq gnus-newsgroup-name group)
3032       ;; Set any local variables in the group parameters.
3033       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3034       t)))
3035
3036 (defun gnus-set-global-variables ()
3037   "Set the global equivalents of the buffer-local variables.
3038 They are set to the latest values they had.  These reflect the summary
3039 buffer that was in action when the last article was fetched."
3040   (when (eq major-mode 'gnus-summary-mode)
3041     (setq gnus-summary-buffer (current-buffer))
3042     (let ((name gnus-newsgroup-name)
3043           (marked gnus-newsgroup-marked)
3044           (spam gnus-newsgroup-spam-marked)
3045           (unread gnus-newsgroup-unreads)
3046           (headers gnus-current-headers)
3047           (data gnus-newsgroup-data)
3048           (summary gnus-summary-buffer)
3049           (article-buffer gnus-article-buffer)
3050           (original gnus-original-article-buffer)
3051           (gac gnus-article-current)
3052           (reffed gnus-reffed-article-number)
3053           (score-file gnus-current-score-file)
3054           (default-charset gnus-newsgroup-charset)
3055           vlist)
3056       (let ((locals gnus-newsgroup-variables))
3057         (while locals
3058           (if (consp (car locals))
3059               (push (eval (caar locals)) vlist)
3060             (push (eval (car locals)) vlist))
3061           (setq locals (cdr locals)))
3062         (setq vlist (nreverse vlist)))
3063       (save-excursion
3064         (set-buffer gnus-group-buffer)
3065         (setq gnus-newsgroup-name name
3066               gnus-newsgroup-marked marked
3067               gnus-newsgroup-spam-marked spam
3068               gnus-newsgroup-unreads unread
3069               gnus-current-headers headers
3070               gnus-newsgroup-data data
3071               gnus-article-current gac
3072               gnus-summary-buffer summary
3073               gnus-article-buffer article-buffer
3074               gnus-original-article-buffer original
3075               gnus-reffed-article-number reffed
3076               gnus-current-score-file score-file
3077               gnus-newsgroup-charset default-charset)
3078         (let ((locals gnus-newsgroup-variables))
3079           (while locals
3080             (if (consp (car locals))
3081                 (set (caar locals) (pop vlist))
3082               (set (car locals) (pop vlist)))
3083             (setq locals (cdr locals))))
3084         ;; The article buffer also has local variables.
3085         (when (gnus-buffer-live-p gnus-article-buffer)
3086           (set-buffer gnus-article-buffer)
3087           (setq gnus-summary-buffer summary))))))
3088
3089 (defun gnus-summary-article-unread-p (article)
3090   "Say whether ARTICLE is unread or not."
3091   (memq article gnus-newsgroup-unreads))
3092
3093 (defun gnus-summary-first-article-p (&optional article)
3094   "Return whether ARTICLE is the first article in the buffer."
3095   (if (not (setq article (or article (gnus-summary-article-number))))
3096       nil
3097     (eq article (caar gnus-newsgroup-data))))
3098
3099 (defun gnus-summary-last-article-p (&optional article)
3100   "Return whether ARTICLE is the last article in the buffer."
3101   (if (not (setq article (or article (gnus-summary-article-number))))
3102       ;; All non-existent numbers are the last article.  :-)
3103       t
3104     (not (cdr (gnus-data-find-list article)))))
3105
3106 (defun gnus-make-thread-indent-array ()
3107   (let ((n 200))
3108     (unless (and gnus-thread-indent-array
3109                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3110       (setq gnus-thread-indent-array (make-vector 201 "")
3111             gnus-thread-indent-array-level gnus-thread-indent-level)
3112       (while (>= n 0)
3113         (aset gnus-thread-indent-array n
3114               (make-string (* n gnus-thread-indent-level) ? ))
3115         (setq n (1- n))))))
3116
3117 (defun gnus-update-summary-mark-positions ()
3118   "Compute where the summary marks are to go."
3119   (save-excursion
3120     (when (gnus-buffer-exists-p gnus-summary-buffer)
3121       (set-buffer gnus-summary-buffer))
3122     (let ((gnus-replied-mark 129)
3123           (gnus-score-below-mark 130)
3124           (gnus-score-over-mark 130)
3125           (gnus-undownloaded-mark 131)
3126           (spec gnus-summary-line-format-spec)
3127           gnus-visual pos)
3128       (save-excursion
3129         (gnus-set-work-buffer)
3130         (let ((gnus-summary-line-format-spec spec)
3131               (gnus-newsgroup-downloadable '(0)))
3132           (gnus-summary-insert-line
3133            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3134            0 nil t 128 t nil "" nil 1)
3135           (goto-char (point-min))
3136           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3137                                              (- (point) (point-min) 1)))))
3138           (goto-char (point-min))
3139           (push (cons 'replied (and (search-forward "\201" nil t)
3140                                     (- (point) (point-min) 1)))
3141                 pos)
3142           (goto-char (point-min))
3143           (push (cons 'score (and (search-forward "\202" nil t)
3144                                   (- (point) (point-min) 1)))
3145                 pos)
3146           (goto-char (point-min))
3147           (push (cons 'download
3148                       (and (search-forward "\203" nil t)
3149                            (- (point) (point-min) 1)))
3150                 pos)))
3151       (setq gnus-summary-mark-positions pos))))
3152
3153 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3154   "Insert a dummy root in the summary buffer."
3155   (beginning-of-line)
3156   (gnus-add-text-properties
3157    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3158    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3159
3160 (defun gnus-summary-extract-address-component (from)
3161   (or (car (funcall gnus-extract-address-components from))
3162       from))
3163
3164 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3165   (let ((mail-parse-charset gnus-newsgroup-charset)
3166         ; Is it really necessary to do this next part for each summary line?
3167         ; Luckily, doesn't seem to slow things down much.
3168         (mail-parse-ignored-charsets
3169          (save-excursion (set-buffer gnus-summary-buffer)
3170                          gnus-newsgroup-ignored-charsets)))
3171     (or
3172      (and gnus-ignored-from-addresses
3173           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3174           (let ((extra-headers (mail-header-extra header))
3175                 to
3176                 newsgroups)
3177             (cond
3178              ((setq to (cdr (assq 'To extra-headers)))
3179               (concat "-> "
3180                       (inline
3181                         (gnus-summary-extract-address-component
3182                          (funcall gnus-decode-encoded-word-function to)))))
3183              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3184               (concat "=> " newsgroups)))))
3185      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3186
3187 (defun gnus-summary-insert-line (gnus-tmp-header
3188                                  gnus-tmp-level gnus-tmp-current
3189                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3190                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3191                                  &optional gnus-tmp-dummy gnus-tmp-score
3192                                  gnus-tmp-process)
3193   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3194          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3195          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3196          (gnus-tmp-score-char
3197           (if (or (null gnus-summary-default-score)
3198                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3199                       gnus-summary-zcore-fuzz))
3200               ?                         ;Whitespace
3201             (if (< gnus-tmp-score gnus-summary-default-score)
3202                 gnus-score-below-mark gnus-score-over-mark)))
3203          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3204          (gnus-tmp-replied
3205           (cond (gnus-tmp-process gnus-process-mark)
3206                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3207                  gnus-cached-mark)
3208                 (gnus-tmp-replied gnus-replied-mark)
3209                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3210                  gnus-forwarded-mark)
3211                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3212                  gnus-saved-mark)
3213                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3214                  gnus-recent-mark)
3215                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3216                  gnus-unseen-mark)
3217                 (t gnus-no-mark)))
3218          (gnus-tmp-downloaded
3219           (cond (undownloaded 
3220                  gnus-undownloaded-mark)
3221                 (gnus-newsgroup-agentized
3222                  gnus-downloaded-mark)
3223                 (t
3224                  gnus-no-mark)))
3225          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3226          (gnus-tmp-name
3227           (cond
3228            ((string-match "<[^>]+> *$" gnus-tmp-from)
3229             (let ((beg (match-beginning 0)))
3230               (or (and (string-match "^\".+\"" gnus-tmp-from)
3231                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3232                   (substring gnus-tmp-from 0 beg))))
3233            ((string-match "(.+)" gnus-tmp-from)
3234             (substring gnus-tmp-from
3235                        (1+ (match-beginning 0)) (1- (match-end 0))))
3236            (t gnus-tmp-from)))
3237          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3238          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3239          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3240          (buffer-read-only nil))
3241     (when (string= gnus-tmp-name "")
3242       (setq gnus-tmp-name gnus-tmp-from))
3243     (unless (numberp gnus-tmp-lines)
3244       (setq gnus-tmp-lines -1))
3245     (if (= gnus-tmp-lines -1)
3246         (setq gnus-tmp-lines "?")
3247       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3248     (gnus-put-text-property
3249      (point)
3250      (progn (eval gnus-summary-line-format-spec) (point))
3251      'gnus-number gnus-tmp-number)
3252     (when (gnus-visual-p 'summary-highlight 'highlight)
3253       (forward-line -1)
3254       (gnus-run-hooks 'gnus-summary-update-hook)
3255       (forward-line 1))))
3256
3257 (defun gnus-summary-update-line (&optional dont-update)
3258   "Update summary line after change."
3259   (when (and gnus-summary-default-score
3260              (not gnus-summary-inhibit-highlight))
3261     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3262            (article (gnus-summary-article-number))
3263            (score (gnus-summary-article-score article)))
3264       (unless dont-update
3265         (if (and gnus-summary-mark-below
3266                  (< (gnus-summary-article-score)
3267                     gnus-summary-mark-below))
3268             ;; This article has a low score, so we mark it as read.
3269             (when (memq article gnus-newsgroup-unreads)
3270               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3271           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3272             ;; This article was previously marked as read on account
3273             ;; of a low score, but now it has risen, so we mark it as
3274             ;; unread.
3275             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3276         (gnus-summary-update-mark
3277          (if (or (null gnus-summary-default-score)
3278                  (<= (abs (- score gnus-summary-default-score))
3279                      gnus-summary-zcore-fuzz))
3280              ?                          ;Whitespace
3281            (if (< score gnus-summary-default-score)
3282                gnus-score-below-mark gnus-score-over-mark))
3283          'score))
3284       ;; Do visual highlighting.
3285       (when (gnus-visual-p 'summary-highlight 'highlight)
3286         (gnus-run-hooks 'gnus-summary-update-hook)))))
3287
3288 (defvar gnus-tmp-new-adopts nil)
3289
3290 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3291   "Return the number of articles in THREAD.
3292 This may be 0 in some cases -- if none of the articles in
3293 the thread are to be displayed."
3294   (let* ((number
3295          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3296           (cond
3297            ((not (listp thread))
3298             1)
3299            ((and (consp thread) (cdr thread))
3300             (apply
3301              '+ 1 (mapcar
3302                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3303            ((null thread)
3304             1)
3305            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3306             1)
3307            (t 0))))
3308     (when (and level (zerop level) gnus-tmp-new-adopts)
3309       (incf number
3310             (apply '+ (mapcar
3311                        'gnus-summary-number-of-articles-in-thread
3312                        gnus-tmp-new-adopts))))
3313     (if char
3314         (if (> number 1) gnus-not-empty-thread-mark
3315           gnus-empty-thread-mark)
3316       number)))
3317
3318 (defsubst gnus-summary-line-message-size (head)
3319   "Return pretty-printed version of message size.
3320 This function is intended to be used in
3321 `gnus-summary-line-format-alist', which see."
3322   (let ((c (or (mail-header-chars head) -1)))
3323     (cond ((< c 0) "n/a")               ; chars not available
3324           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3325           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3326           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3327           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3328
3329
3330 (defun gnus-summary-set-local-parameters (group)
3331   "Go through the local params of GROUP and set all variable specs in that list."
3332   (let ((params (gnus-group-find-parameter group))
3333         (vars '(quit-config))           ; Ignore quit-config.
3334         elem)
3335     (while params
3336       (setq elem (car params)
3337             params (cdr params))
3338       (and (consp elem)                 ; Has to be a cons.
3339            (consp (cdr elem))           ; The cdr has to be a list.
3340            (symbolp (car elem))         ; Has to be a symbol in there.
3341            (not (memq (car elem) vars))
3342            (ignore-errors               ; So we set it.
3343              (push (car elem) vars)
3344              (make-local-variable (car elem))
3345              (set (car elem) (eval (nth 1 elem))))))))
3346
3347 (defun gnus-summary-read-group (group &optional show-all no-article
3348                                       kill-buffer no-display backward
3349                                       select-articles)
3350   "Start reading news in newsgroup GROUP.
3351 If SHOW-ALL is non-nil, already read articles are also listed.
3352 If NO-ARTICLE is non-nil, no article is selected initially.
3353 If NO-DISPLAY, don't generate a summary buffer."
3354   (let (result)
3355     (while (and group
3356                 (null (setq result
3357                             (let ((gnus-auto-select-next nil))
3358                               (or (gnus-summary-read-group-1
3359                                    group show-all no-article
3360                                    kill-buffer no-display
3361                                    select-articles)
3362                                   (setq show-all nil
3363                                         select-articles nil)))))
3364                 (eq gnus-auto-select-next 'quietly))
3365       (set-buffer gnus-group-buffer)
3366       ;; The entry function called above goes to the next
3367       ;; group automatically, so we go two groups back
3368       ;; if we are searching for the previous group.
3369       (when backward
3370         (gnus-group-prev-unread-group 2))
3371       (if (not (equal group (gnus-group-group-name)))
3372           (setq group (gnus-group-group-name))
3373         (setq group nil)))
3374     result))
3375
3376 (defun gnus-summary-read-group-1 (group show-all no-article
3377                                         kill-buffer no-display
3378                                         &optional select-articles)
3379   ;; Killed foreign groups can't be entered.
3380   ;;  (when (and (not (gnus-group-native-p group))
3381   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3382   ;;    (error "Dead non-native groups can't be entered"))
3383   (gnus-message 5 "Retrieving newsgroup: %s..."
3384                 (gnus-group-decoded-name group))
3385   (let* ((new-group (gnus-summary-setup-buffer group))
3386          (quit-config (gnus-group-quit-config group))
3387          (did-select (and new-group (gnus-select-newsgroup
3388                                      group show-all select-articles))))
3389     (cond
3390      ;; This summary buffer exists already, so we just select it.
3391      ((not new-group)
3392       (gnus-set-global-variables)
3393       (when kill-buffer
3394         (gnus-kill-or-deaden-summary kill-buffer))
3395       (gnus-configure-windows 'summary 'force)
3396       (gnus-set-mode-line 'summary)
3397       (gnus-summary-position-point)
3398       (message "")
3399       t)
3400      ;; We couldn't select this group.
3401      ((null did-select)
3402       (when (and (eq major-mode 'gnus-summary-mode)
3403                  (not (equal (current-buffer) kill-buffer)))
3404         (kill-buffer (current-buffer))
3405         (if (not quit-config)
3406             (progn
3407               ;; Update the info -- marks might need to be removed,
3408               ;; for instance.
3409               (gnus-summary-update-info)
3410               (set-buffer gnus-group-buffer)
3411               (gnus-group-jump-to-group group)
3412               (gnus-group-next-unread-group 1))
3413           (gnus-handle-ephemeral-exit quit-config)))
3414       (let ((grpinfo (gnus-get-info group)))
3415         (if (null (gnus-info-read grpinfo))
3416             (gnus-message 3 "Group %s contains no messages"
3417                           (gnus-group-decoded-name group))
3418           (gnus-message 3 "Can't select group")))
3419       nil)
3420      ;; The user did a `C-g' while prompting for number of articles,
3421      ;; so we exit this group.
3422      ((eq did-select 'quit)
3423       (and (eq major-mode 'gnus-summary-mode)
3424            (not (equal (current-buffer) kill-buffer))
3425            (kill-buffer (current-buffer)))
3426       (when kill-buffer
3427         (gnus-kill-or-deaden-summary kill-buffer))
3428       (if (not quit-config)
3429           (progn
3430             (set-buffer gnus-group-buffer)
3431             (gnus-group-jump-to-group group)
3432             (gnus-group-next-unread-group 1)
3433             (gnus-configure-windows 'group 'force))
3434         (gnus-handle-ephemeral-exit quit-config))
3435       ;; Finally signal the quit.
3436       (signal 'quit nil))
3437      ;; The group was successfully selected.
3438      (t
3439       (gnus-set-global-variables)
3440       ;; Save the active value in effect when the group was entered.
3441       (setq gnus-newsgroup-active
3442             (gnus-copy-sequence
3443              (gnus-active gnus-newsgroup-name)))
3444       ;; You can change the summary buffer in some way with this hook.
3445       (gnus-run-hooks 'gnus-select-group-hook)
3446       (gnus-update-format-specifications
3447        nil 'summary 'summary-mode 'summary-dummy)
3448       (gnus-update-summary-mark-positions)
3449       ;; Do score processing.
3450       (when gnus-use-scoring
3451         (gnus-possibly-score-headers))
3452       ;; Check whether to fill in the gaps in the threads.
3453       (when gnus-build-sparse-threads
3454         (gnus-build-sparse-threads))
3455       ;; Find the initial limit.
3456       (if gnus-show-threads
3457           (if show-all
3458               (let ((gnus-newsgroup-dormant nil))
3459                 (gnus-summary-initial-limit show-all))
3460             (gnus-summary-initial-limit show-all))
3461         ;; When unthreaded, all articles are always shown.
3462         (setq gnus-newsgroup-limit
3463               (mapcar
3464                (lambda (header) (mail-header-number header))
3465                gnus-newsgroup-headers)))
3466       ;; Generate the summary buffer.
3467       (unless no-display
3468         (gnus-summary-prepare))
3469       (when gnus-use-trees
3470         (gnus-tree-open group)
3471         (setq gnus-summary-highlight-line-function
3472               'gnus-tree-highlight-article))
3473       ;; If the summary buffer is empty, but there are some low-scored
3474       ;; articles or some excluded dormants, we include these in the
3475       ;; buffer.
3476       (when (and (zerop (buffer-size))
3477                  (not no-display))
3478         (cond (gnus-newsgroup-dormant
3479                (gnus-summary-limit-include-dormant))
3480               ((and gnus-newsgroup-scored show-all)
3481                (gnus-summary-limit-include-expunged t))))
3482       ;; Function `gnus-apply-kill-file' must be called in this hook.
3483       (gnus-run-hooks 'gnus-apply-kill-hook)
3484       (if (and (zerop (buffer-size))
3485                (not no-display))
3486           (progn
3487             ;; This newsgroup is empty.
3488             (gnus-summary-catchup-and-exit nil t)
3489             (gnus-message 6 "No unread news")
3490             (when kill-buffer
3491               (gnus-kill-or-deaden-summary kill-buffer))
3492             ;; Return nil from this function.
3493             nil)
3494         ;; Hide conversation thread subtrees.  We cannot do this in
3495         ;; gnus-summary-prepare-hook since kill processing may not
3496         ;; work with hidden articles.
3497         (gnus-summary-maybe-hide-threads)
3498         (when kill-buffer
3499           (gnus-kill-or-deaden-summary kill-buffer))
3500         (gnus-summary-auto-select-subject)
3501         ;; Show first unread article if requested.
3502         (if (and (not no-article)
3503                  (not no-display)
3504                  gnus-newsgroup-unreads
3505                  gnus-auto-select-first)
3506             (progn
3507               (gnus-configure-windows 'summary)
3508               (let ((art (gnus-summary-article-number)))
3509                 (unless (and (not gnus-plugged)
3510                              (or (memq art gnus-newsgroup-undownloaded)
3511                                  (memq art gnus-newsgroup-downloadable)))
3512                   (gnus-summary-goto-article art))))
3513           ;; Don't select any articles.
3514           (gnus-summary-position-point)
3515           (gnus-configure-windows 'summary 'force)
3516           (gnus-set-mode-line 'summary))
3517         (when (get-buffer-window gnus-group-buffer t)
3518           ;; Gotta use windows, because recenter does weird stuff if
3519           ;; the current buffer ain't the displayed window.
3520           (let ((owin (selected-window)))
3521             (select-window (get-buffer-window gnus-group-buffer t))
3522             (when (gnus-group-goto-group group)
3523               (recenter))
3524             (select-window owin)))
3525         ;; Mark this buffer as "prepared".
3526         (setq gnus-newsgroup-prepared t)
3527         (gnus-run-hooks 'gnus-summary-prepared-hook)
3528         (unless (gnus-ephemeral-group-p group)
3529           (gnus-group-update-group group))
3530         t)))))
3531
3532 (defun gnus-summary-auto-select-subject ()
3533   "Select the subject line on initial group entry."
3534   (goto-char (point-min))
3535   (cond
3536    ((eq gnus-auto-select-subject 'best)
3537     (gnus-summary-best-unread-subject))
3538    ((eq gnus-auto-select-subject 'unread)
3539     (gnus-summary-first-unread-subject))
3540    ((eq gnus-auto-select-subject 'unseen)
3541     (gnus-summary-first-unseen-subject))
3542    ((eq gnus-auto-select-subject 'unseen-or-unread)
3543     (gnus-summary-first-unseen-or-unread-subject))
3544    ((eq gnus-auto-select-subject 'first)
3545     ;; Do nothing.
3546     )
3547    ((gnus-functionp gnus-auto-select-subject)
3548     (funcall gnus-auto-select-subject))))
3549
3550 (defun gnus-summary-prepare ()
3551   "Generate the summary buffer."
3552   (interactive)
3553   (let ((buffer-read-only nil))
3554     (erase-buffer)
3555     (setq gnus-newsgroup-data nil
3556           gnus-newsgroup-data-reverse nil)
3557     (gnus-run-hooks 'gnus-summary-generate-hook)
3558     ;; Generate the buffer, either with threads or without.
3559     (when gnus-newsgroup-headers
3560       (gnus-summary-prepare-threads
3561        (if gnus-show-threads
3562            (gnus-sort-gathered-threads
3563             (funcall gnus-summary-thread-gathering-function
3564                      (gnus-sort-threads
3565                       (gnus-cut-threads (gnus-make-threads)))))
3566          ;; Unthreaded display.
3567          (gnus-sort-articles gnus-newsgroup-headers))))
3568     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3569     ;; Call hooks for modifying summary buffer.
3570     (goto-char (point-min))
3571     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3572
3573 (defsubst gnus-general-simplify-subject (subject)
3574   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3575   (setq subject
3576         (cond
3577          ;; Truncate the subject.
3578          (gnus-simplify-subject-functions
3579           (gnus-map-function gnus-simplify-subject-functions subject))
3580          ((numberp gnus-summary-gather-subject-limit)
3581           (setq subject (gnus-simplify-subject-re subject))
3582           (if (> (length subject) gnus-summary-gather-subject-limit)
3583               (substring subject 0 gnus-summary-gather-subject-limit)
3584             subject))
3585          ;; Fuzzily simplify it.
3586          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3587           (gnus-simplify-subject-fuzzy subject))
3588          ;; Just remove the leading "Re:".
3589          (t
3590           (gnus-simplify-subject-re subject))))
3591
3592   (if (and gnus-summary-gather-exclude-subject
3593            (string-match gnus-summary-gather-exclude-subject subject))
3594       nil                         ; This article shouldn't be gathered
3595     subject))
3596
3597 (defun gnus-summary-simplify-subject-query ()
3598   "Query where the respool algorithm would put this article."
3599   (interactive)
3600   (gnus-summary-select-article)
3601   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3602
3603 (defun gnus-gather-threads-by-subject (threads)
3604   "Gather threads by looking at Subject headers."
3605   (if (not gnus-summary-make-false-root)
3606       threads
3607     (let ((hashtb (gnus-make-hashtable 1024))
3608           (prev threads)
3609           (result threads)
3610           subject hthread whole-subject)
3611       (while threads
3612         (setq subject (gnus-general-simplify-subject
3613                        (setq whole-subject (mail-header-subject
3614                                             (caar threads)))))
3615         (when subject
3616           (if (setq hthread (gnus-gethash subject hashtb))
3617               (progn
3618                 ;; We enter a dummy root into the thread, if we
3619                 ;; haven't done that already.
3620                 (unless (stringp (caar hthread))
3621                   (setcar hthread (list whole-subject (car hthread))))
3622                 ;; We add this new gathered thread to this gathered
3623                 ;; thread.
3624                 (setcdr (car hthread)
3625                         (nconc (cdar hthread) (list (car threads))))
3626                 ;; Remove it from the list of threads.
3627                 (setcdr prev (cdr threads))
3628                 (setq threads prev))
3629             ;; Enter this thread into the hash table.
3630             (gnus-sethash subject
3631                           (if gnus-summary-make-false-root-always
3632                               (progn
3633                                 ;; If you want a dummy root above all
3634                                 ;; threads...
3635                                 (setcar threads (list whole-subject
3636                                                       (car threads)))
3637                                 threads)
3638                             threads)
3639                           hashtb)))
3640         (setq prev threads)
3641         (setq threads (cdr threads)))
3642       result)))
3643
3644 (defun gnus-gather-threads-by-references (threads)
3645   "Gather threads by looking at References headers."
3646   (let ((idhashtb (gnus-make-hashtable 1024))
3647         (thhashtb (gnus-make-hashtable 1024))
3648         (prev threads)
3649         (result threads)
3650         ids references id gthread gid entered ref)
3651     (while threads
3652       (when (setq references (mail-header-references (caar threads)))
3653         (setq id (mail-header-id (caar threads))
3654               ids (inline (gnus-split-references references))
3655               entered nil)
3656         (while (setq ref (pop ids))
3657           (setq ids (delete ref ids))
3658           (if (not (setq gid (gnus-gethash ref idhashtb)))
3659               (progn
3660                 (gnus-sethash ref id idhashtb)
3661                 (gnus-sethash id threads thhashtb))
3662             (setq gthread (gnus-gethash gid thhashtb))
3663             (unless entered
3664               ;; We enter a dummy root into the thread, if we
3665               ;; haven't done that already.
3666               (unless (stringp (caar gthread))
3667                 (setcar gthread (list (mail-header-subject (caar gthread))
3668                                       (car gthread))))
3669               ;; We add this new gathered thread to this gathered
3670               ;; thread.
3671               (setcdr (car gthread)
3672                       (nconc (cdar gthread) (list (car threads)))))
3673             ;; Add it into the thread hash table.
3674             (gnus-sethash id gthread thhashtb)
3675             (setq entered t)
3676             ;; Remove it from the list of threads.
3677             (setcdr prev (cdr threads))
3678             (setq threads prev))))
3679       (setq prev threads)
3680       (setq threads (cdr threads)))
3681     result))
3682
3683 (defun gnus-sort-gathered-threads (threads)
3684   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3685   (let ((result threads))
3686     (while threads
3687       (when (stringp (caar threads))
3688         (setcdr (car threads)
3689                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3690       (setq threads (cdr threads)))
3691     result))
3692
3693 (defun gnus-thread-loop-p (root thread)
3694   "Say whether ROOT is in THREAD."
3695   (let ((stack (list thread))
3696         (infloop 0)
3697         th)
3698     (while (setq thread (pop stack))
3699       (setq th (cdr thread))
3700       (while (and th
3701                   (not (eq (caar th) root)))
3702         (pop th))
3703       (if th
3704           ;; We have found a loop.
3705           (let (ref-dep)
3706             (setcdr thread (delq (car th) (cdr thread)))
3707             (if (boundp (setq ref-dep (intern "none"
3708                                               gnus-newsgroup-dependencies)))
3709                 (setcdr (symbol-value ref-dep)
3710                         (nconc (cdr (symbol-value ref-dep))
3711                                (list (car th))))
3712               (set ref-dep (list nil (car th))))
3713             (setq infloop 1
3714                   stack nil))
3715         ;; Push all the subthreads onto the stack.
3716         (push (cdr thread) stack)))
3717     infloop))
3718
3719 (defun gnus-make-threads ()
3720   "Go through the dependency hashtb and find the roots.  Return all threads."
3721   (let (threads)
3722     (while (catch 'infloop
3723              (mapatoms
3724               (lambda (refs)
3725                 ;; Deal with self-referencing References loops.
3726                 (when (and (car (symbol-value refs))
3727                            (not (zerop
3728                                  (apply
3729                                   '+
3730                                   (mapcar
3731                                    (lambda (thread)
3732                                      (gnus-thread-loop-p
3733                                       (car (symbol-value refs)) thread))
3734                                    (cdr (symbol-value refs)))))))
3735                   (setq threads nil)
3736                   (throw 'infloop t))
3737                 (unless (car (symbol-value refs))
3738                   ;; These threads do not refer back to any other
3739                   ;; articles, so they're roots.
3740                   (setq threads (append (cdr (symbol-value refs)) threads))))
3741               gnus-newsgroup-dependencies)))
3742     threads))
3743
3744 ;; Build the thread tree.
3745 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3746   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3747
3748 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3749 if it was already present.
3750
3751 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3752 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3753 Message-IDs will be renamed to a unique Message-ID before being
3754 entered.
3755
3756 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3757   (let* ((id (mail-header-id header))
3758          (id-dep (and id (intern id dependencies)))
3759          parent-id ref ref-dep ref-header replaced)
3760     ;; Enter this `header' in the `dependencies' table.
3761     (cond
3762      ((not id-dep)
3763       (setq header nil))
3764      ;; The first two cases do the normal part: enter a new `header'
3765      ;; in the `dependencies' table.
3766      ((not (boundp id-dep))
3767       (set id-dep (list header)))
3768      ((null (car (symbol-value id-dep)))
3769       (setcar (symbol-value id-dep) header))
3770
3771      ;; From here the `header' was already present in the
3772      ;; `dependencies' table.
3773      (force-new
3774       ;; Overrides an existing entry;
3775       ;; just set the header part of the entry.
3776       (setcar (symbol-value id-dep) header)
3777       (setq replaced t))
3778
3779      ;; Renames the existing `header' to a unique Message-ID.
3780      ((not gnus-summary-ignore-duplicates)
3781       ;; An article with this Message-ID has already been seen.
3782       ;; We rename the Message-ID.
3783       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3784            (list header))
3785       (mail-header-set-id header id))
3786
3787      ;; The last case ignores an existing entry, except it adds any
3788      ;; additional Xrefs (in case the two articles came from different
3789      ;; servers.
3790      ;; Also sets `header' to `nil' meaning that the `dependencies'
3791      ;; table was *not* modified.
3792      (t
3793       (mail-header-set-xref
3794        (car (symbol-value id-dep))
3795        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3796                    "")
3797                (or (mail-header-xref header) "")))
3798       (setq header nil)))
3799
3800     (when (and header (not replaced))
3801       ;; First check that we are not creating a References loop.
3802       (setq parent-id (gnus-parent-id (mail-header-references header)))
3803       (setq ref parent-id)
3804       (while (and ref
3805                   (setq ref-dep (intern-soft ref dependencies))
3806                   (boundp ref-dep)
3807                   (setq ref-header (car (symbol-value ref-dep))))
3808         (if (string= id ref)
3809             ;; Yuk!  This is a reference loop.  Make the article be a
3810             ;; root article.
3811             (progn
3812               (mail-header-set-references (car (symbol-value id-dep)) "none")
3813               (setq ref nil)
3814               (setq parent-id nil))
3815           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3816       (setq ref-dep (intern (or parent-id "none") dependencies))
3817       (if (boundp ref-dep)
3818           (setcdr (symbol-value ref-dep)
3819                   (nconc (cdr (symbol-value ref-dep))
3820                          (list (symbol-value id-dep))))
3821         (set ref-dep (list nil (symbol-value id-dep)))))
3822     header))
3823
3824 (defun gnus-extract-message-id-from-in-reply-to (string)
3825   (if (string-match "<[^>]+>" string)
3826       (substring string (match-beginning 0) (match-end 0))
3827     nil))
3828
3829 (defun gnus-build-sparse-threads ()
3830   (let ((headers gnus-newsgroup-headers)
3831         (mail-parse-charset gnus-newsgroup-charset)
3832         (gnus-summary-ignore-duplicates t)
3833         header references generation relations
3834         subject child end new-child date)
3835     ;; First we create an alist of generations/relations, where
3836     ;; generations is how much we trust the relation, and the relation
3837     ;; is parent/child.
3838     (gnus-message 7 "Making sparse threads...")
3839     (save-excursion
3840       (nnheader-set-temp-buffer " *gnus sparse threads*")
3841       (while (setq header (pop headers))
3842         (when (and (setq references (mail-header-references header))
3843                    (not (string= references "")))
3844           (insert references)
3845           (setq child (mail-header-id header)
3846                 subject (mail-header-subject header)
3847                 date (mail-header-date header)
3848                 generation 0)
3849           (while (search-backward ">" nil t)
3850             (setq end (1+ (point)))
3851             (when (search-backward "<" nil t)
3852               (setq new-child (buffer-substring (point) end))
3853               (push (list (incf generation)
3854                           child (setq child new-child)
3855                           subject date)
3856                     relations)))
3857           (when child
3858             (push (list (1+ generation) child nil subject) relations))
3859           (erase-buffer)))
3860       (kill-buffer (current-buffer)))
3861     ;; Sort over trustworthiness.
3862     (mapcar
3863      (lambda (relation)
3864        (when (gnus-dependencies-add-header
3865               (make-full-mail-header
3866                gnus-reffed-article-number
3867                (nth 3 relation) "" (or (nth 4 relation) "")
3868                (nth 1 relation)
3869                (or (nth 2 relation) "") 0 0 "")
3870               gnus-newsgroup-dependencies nil)
3871          (push gnus-reffed-article-number gnus-newsgroup-limit)
3872          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3873          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3874                gnus-newsgroup-reads)
3875          (decf gnus-reffed-article-number)))
3876      (sort relations 'car-less-than-car))
3877     (gnus-message 7 "Making sparse threads...done")))
3878
3879 (defun gnus-build-old-threads ()
3880   ;; Look at all the articles that refer back to old articles, and
3881   ;; fetch the headers for the articles that aren't there.  This will
3882   ;; build complete threads - if the roots haven't been expired by the
3883   ;; server, that is.
3884   (let ((mail-parse-charset gnus-newsgroup-charset)
3885         id heads)
3886     (mapatoms
3887      (lambda (refs)
3888        (when (not (car (symbol-value refs)))
3889          (setq heads (cdr (symbol-value refs)))
3890          (while heads
3891            (if (memq (mail-header-number (caar heads))
3892                      gnus-newsgroup-dormant)
3893                (setq heads (cdr heads))
3894              (setq id (symbol-name refs))
3895              (while (and (setq id (gnus-build-get-header id))
3896                          (not (car (gnus-id-to-thread id)))))
3897              (setq heads nil)))))
3898      gnus-newsgroup-dependencies)))
3899
3900 ;; This function has to be called with point after the article number
3901 ;; on the beginning of the line.
3902 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3903   (let ((eol (gnus-point-at-eol))
3904         (buffer (current-buffer))
3905         header references in-reply-to)
3906
3907     ;; overview: [num subject from date id refs chars lines misc]
3908     (unwind-protect
3909         (let (x)
3910           (narrow-to-region (point) eol)
3911           (unless (eobp)
3912             (forward-char))
3913
3914           (setq header
3915                 (make-full-mail-header
3916                  number                 ; number
3917                  (condition-case ()     ; subject
3918                      (funcall gnus-decode-encoded-word-function
3919                               (setq x (nnheader-nov-field)))
3920                    (error x))
3921                  (condition-case ()     ; from
3922                      (funcall gnus-decode-encoded-word-function
3923                               (setq x (nnheader-nov-field)))
3924                    (error x))
3925                  (nnheader-nov-field)   ; date
3926                  (nnheader-nov-read-message-id) ; id
3927                  (setq references (nnheader-nov-field)) ; refs
3928                  (nnheader-nov-read-integer) ; chars
3929                  (nnheader-nov-read-integer) ; lines
3930                  (unless (eobp)
3931                    (if (looking-at "Xref: ")
3932                        (goto-char (match-end 0)))
3933                    (nnheader-nov-field)) ; Xref
3934                  (nnheader-nov-parse-extra)))) ; extra
3935
3936       (widen))
3937
3938     (when (and (string= references "")
3939                (setq in-reply-to (mail-header-extra header))
3940                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3941       (mail-header-set-references
3942        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3943
3944     (when gnus-alter-header-function
3945       (funcall gnus-alter-header-function header))
3946     (gnus-dependencies-add-header header dependencies force-new)))
3947
3948 (defun gnus-build-get-header (id)
3949   "Look through the buffer of NOV lines and find the header to ID.
3950 Enter this line into the dependencies hash table, and return
3951 the id of the parent article (if any)."
3952   (let ((deps gnus-newsgroup-dependencies)
3953         found header)
3954     (prog1
3955         (save-excursion
3956           (set-buffer nntp-server-buffer)
3957           (let ((case-fold-search nil))
3958             (goto-char (point-min))
3959             (while (and (not found)
3960                         (search-forward id nil t))
3961               (beginning-of-line)
3962               (setq found (looking-at
3963                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3964                                    (regexp-quote id))))
3965               (or found (beginning-of-line 2)))
3966             (when found
3967               (beginning-of-line)
3968               (and
3969                (setq header (gnus-nov-parse-line
3970                              (read (current-buffer)) deps))
3971                (gnus-parent-id (mail-header-references header))))))
3972       (when header
3973         (let ((number (mail-header-number header)))
3974           (push number gnus-newsgroup-limit)
3975           (push header gnus-newsgroup-headers)
3976           (if (memq number gnus-newsgroup-unselected)
3977               (progn
3978                 (setq gnus-newsgroup-unreads
3979                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
3980                                                number))
3981                 (setq gnus-newsgroup-unselected
3982                       (delq number gnus-newsgroup-unselected)))
3983             (push number gnus-newsgroup-ancient)))))))
3984
3985 (defun gnus-build-all-threads ()
3986   "Read all the headers."
3987   (let ((gnus-summary-ignore-duplicates t)
3988         (mail-parse-charset gnus-newsgroup-charset)
3989         (dependencies gnus-newsgroup-dependencies)
3990         header article)
3991     (save-excursion
3992       (set-buffer nntp-server-buffer)
3993       (let ((case-fold-search nil))
3994         (goto-char (point-min))
3995         (while (not (eobp))
3996           (ignore-errors
3997             (setq article (read (current-buffer))
3998                   header (gnus-nov-parse-line article dependencies)))
3999           (when header
4000             (save-excursion
4001               (set-buffer gnus-summary-buffer)
4002               (push header gnus-newsgroup-headers)
4003               (if (memq (setq article (mail-header-number header))
4004                         gnus-newsgroup-unselected)
4005                   (progn
4006                     (setq gnus-newsgroup-unreads
4007                           (gnus-add-to-sorted-list
4008                            gnus-newsgroup-unreads article))
4009                     (setq gnus-newsgroup-unselected
4010                           (delq article gnus-newsgroup-unselected)))
4011                 (push article gnus-newsgroup-ancient)))
4012             (forward-line 1)))))))
4013
4014 (defun gnus-summary-update-article-line (article header)
4015   "Update the line for ARTICLE using HEADERS."
4016   (let* ((id (mail-header-id header))
4017          (thread (gnus-id-to-thread id)))
4018     (unless thread
4019       (error "Article in no thread"))
4020     ;; Update the thread.
4021     (setcar thread header)
4022     (gnus-summary-goto-subject article)
4023     (let* ((datal (gnus-data-find-list article))
4024            (data (car datal))
4025            (length (when (cdr datal)
4026                      (- (gnus-data-pos data)
4027                         (gnus-data-pos (cadr datal)))))
4028            (buffer-read-only nil)
4029            (level (gnus-summary-thread-level)))
4030       (gnus-delete-line)
4031       (gnus-summary-insert-line
4032        header level nil 
4033        (memq article gnus-newsgroup-undownloaded)
4034        (gnus-article-mark article)
4035        (memq article gnus-newsgroup-replied)
4036        (memq article gnus-newsgroup-expirable)
4037        ;; Only insert the Subject string when it's different
4038        ;; from the previous Subject string.
4039        (if (and
4040             gnus-show-threads
4041             (gnus-subject-equal
4042              (condition-case ()
4043                  (mail-header-subject
4044                   (gnus-data-header
4045                    (cadr
4046                     (gnus-data-find-list
4047                      article
4048                      (gnus-data-list t)))))
4049                ;; Error on the side of excessive subjects.
4050                (error ""))
4051              (mail-header-subject header)))
4052            ""
4053          (mail-header-subject header))
4054        nil (cdr (assq article gnus-newsgroup-scored))
4055        (memq article gnus-newsgroup-processable))
4056       (when length
4057         (gnus-data-update-list
4058          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
4059
4060 (defun gnus-summary-update-article (article &optional iheader)
4061   "Update ARTICLE in the summary buffer."
4062   (set-buffer gnus-summary-buffer)
4063   (let* ((header (gnus-summary-article-header article))
4064          (id (mail-header-id header))
4065          (data (gnus-data-find article))
4066          (thread (gnus-id-to-thread id))
4067          (references (mail-header-references header))
4068          (parent
4069           (gnus-id-to-thread
4070            (or (gnus-parent-id
4071                 (when (and references
4072                            (not (equal "" references)))
4073                   references))
4074                "none")))
4075          (buffer-read-only nil)
4076          (old (car thread)))
4077     (when thread
4078       (unless iheader
4079         (setcar thread nil)
4080         (when parent
4081           (delq thread parent)))
4082       (if (gnus-summary-insert-subject id header)
4083           ;; Set the (possibly) new article number in the data structure.
4084           (gnus-data-set-number data (gnus-id-to-article id))
4085         (setcar thread old)
4086         nil))))
4087
4088 (defun gnus-rebuild-thread (id &optional line)
4089   "Rebuild the thread containing ID.
4090 If LINE, insert the rebuilt thread starting on line LINE."
4091   (let ((buffer-read-only nil)
4092         old-pos current thread data)
4093     (if (not gnus-show-threads)
4094         (setq thread (list (car (gnus-id-to-thread id))))
4095       ;; Get the thread this article is part of.
4096       (setq thread (gnus-remove-thread id)))
4097     (setq old-pos (gnus-point-at-bol))
4098     (setq current (save-excursion
4099                     (and (re-search-backward "[\r\n]" nil t)
4100                          (gnus-summary-article-number))))
4101     ;; If this is a gathered thread, we have to go some re-gathering.
4102     (when (stringp (car thread))
4103       (let ((subject (car thread))
4104             roots thr)
4105         (setq thread (cdr thread))
4106         (while thread
4107           (unless (memq (setq thr (gnus-id-to-thread
4108                                    (gnus-root-id
4109                                     (mail-header-id (caar thread)))))
4110                         roots)
4111             (push thr roots))
4112           (setq thread (cdr thread)))
4113         ;; We now have all (unique) roots.
4114         (if (= (length roots) 1)
4115             ;; All the loose roots are now one solid root.
4116             (setq thread (car roots))
4117           (setq thread (cons subject (gnus-sort-threads roots))))))
4118     (let (threads)
4119       ;; We then insert this thread into the summary buffer.
4120       (when line
4121         (goto-char (point-min))
4122         (forward-line (1- line)))
4123       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4124         (if gnus-show-threads
4125             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4126           (gnus-summary-prepare-unthreaded thread))
4127         (setq data (nreverse gnus-newsgroup-data))
4128         (setq threads gnus-newsgroup-threads))
4129       ;; We splice the new data into the data structure.
4130       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4131       ;;!!! then we want to insert at the beginning of the buffer.
4132       ;;!!! That happens to be true with Gnus now, but that may
4133       ;;!!! change in the future.  Perhaps.
4134       (gnus-data-enter-list
4135        (if line nil current) data (- (point) old-pos))
4136       (setq gnus-newsgroup-threads
4137             (nconc threads gnus-newsgroup-threads))
4138       (gnus-data-compute-positions))))
4139
4140 (defun gnus-number-to-header (number)
4141   "Return the header for article NUMBER."
4142   (let ((headers gnus-newsgroup-headers))
4143     (while (and headers
4144                 (not (= number (mail-header-number (car headers)))))
4145       (pop headers))
4146     (when headers
4147       (car headers))))
4148
4149 (defun gnus-parent-headers (in-headers &optional generation)
4150   "Return the headers of the GENERATIONeth parent of HEADERS."
4151   (unless generation
4152     (setq generation 1))
4153   (let ((parent t)
4154         (headers in-headers)
4155         references)
4156     (while (and parent
4157                 (not (zerop generation))
4158                 (setq references (mail-header-references headers)))
4159       (setq headers (if (and references
4160                              (setq parent (gnus-parent-id references)))
4161                         (car (gnus-id-to-thread parent))
4162                       nil))
4163       (decf generation))
4164     (and (not (eq headers in-headers))
4165          headers)))
4166
4167 (defun gnus-id-to-thread (id)
4168   "Return the (sub-)thread where ID appears."
4169   (gnus-gethash id gnus-newsgroup-dependencies))
4170
4171 (defun gnus-id-to-article (id)
4172   "Return the article number of ID."
4173   (let ((thread (gnus-id-to-thread id)))
4174     (when (and thread
4175                (car thread))
4176       (mail-header-number (car thread)))))
4177
4178 (defun gnus-id-to-header (id)
4179   "Return the article headers of ID."
4180   (car (gnus-id-to-thread id)))
4181
4182 (defun gnus-article-displayed-root-p (article)
4183   "Say whether ARTICLE is a root(ish) article."
4184   (let ((level (gnus-summary-thread-level article))
4185         (refs (mail-header-references  (gnus-summary-article-header article)))
4186         particle)
4187     (cond
4188      ((null level) nil)
4189      ((zerop level) t)
4190      ((null refs) t)
4191      ((null (gnus-parent-id refs)) t)
4192      ((and (= 1 level)
4193            (null (setq particle (gnus-id-to-article
4194                                  (gnus-parent-id refs))))
4195            (null (gnus-summary-thread-level particle)))))))
4196
4197 (defun gnus-root-id (id)
4198   "Return the id of the root of the thread where ID appears."
4199   (let (last-id prev)
4200     (while (and id (setq prev (car (gnus-id-to-thread id))))
4201       (setq last-id id
4202             id (gnus-parent-id (mail-header-references prev))))
4203     last-id))
4204
4205 (defun gnus-articles-in-thread (thread)
4206   "Return the list of articles in THREAD."
4207   (cons (mail-header-number (car thread))
4208         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4209
4210 (defun gnus-remove-thread (id &optional dont-remove)
4211   "Remove the thread that has ID in it."
4212   (let (headers thread last-id)
4213     ;; First go up in this thread until we find the root.
4214     (setq last-id (gnus-root-id id)
4215           headers (message-flatten-list (gnus-id-to-thread last-id)))
4216     ;; We have now found the real root of this thread.  It might have
4217     ;; been gathered into some loose thread, so we have to search
4218     ;; through the threads to find the thread we wanted.
4219     (let ((threads gnus-newsgroup-threads)
4220           sub)
4221       (while threads
4222         (setq sub (car threads))
4223         (if (stringp (car sub))
4224             ;; This is a gathered thread, so we look at the roots
4225             ;; below it to find whether this article is in this
4226             ;; gathered root.
4227             (progn
4228               (setq sub (cdr sub))
4229               (while sub
4230                 (when (member (caar sub) headers)
4231                   (setq thread (car threads)
4232                         threads nil
4233                         sub nil))
4234                 (setq sub (cdr sub))))
4235           ;; It's an ordinary thread, so we check it.
4236           (when (eq (car sub) (car headers))
4237             (setq thread sub
4238                   threads nil)))
4239         (setq threads (cdr threads)))
4240       ;; If this article is in no thread, then it's a root.
4241       (if thread
4242           (unless dont-remove
4243             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4244         (setq thread (gnus-id-to-thread last-id)))
4245       (when thread
4246         (prog1
4247             thread                      ; We return this thread.
4248           (unless dont-remove
4249             (if (stringp (car thread))
4250                 (progn
4251                   ;; If we use dummy roots, then we have to remove the
4252                   ;; dummy root as well.
4253                   (when (eq gnus-summary-make-false-root 'dummy)
4254                     ;; We go to the dummy root by going to
4255                     ;; the first sub-"thread", and then one line up.
4256                     (gnus-summary-goto-article
4257                      (mail-header-number (caadr thread)))
4258                     (forward-line -1)
4259                     (gnus-delete-line)
4260                     (gnus-data-compute-positions))
4261                   (setq thread (cdr thread))
4262                   (while thread
4263                     (gnus-remove-thread-1 (car thread))
4264                     (setq thread (cdr thread))))
4265               (gnus-remove-thread-1 thread))))))))
4266
4267 (defun gnus-remove-thread-1 (thread)
4268   "Remove the thread THREAD recursively."
4269   (let ((number (mail-header-number (pop thread)))
4270         d)
4271     (setq thread (reverse thread))
4272     (while thread
4273       (gnus-remove-thread-1 (pop thread)))
4274     (when (setq d (gnus-data-find number))
4275       (goto-char (gnus-data-pos d))
4276       (gnus-summary-show-thread)
4277       (gnus-data-remove
4278        number
4279        (- (gnus-point-at-bol)
4280           (prog1
4281               (1+ (gnus-point-at-eol))
4282             (gnus-delete-line)))))))
4283
4284 (defun gnus-sort-threads-1 (threads func)
4285   (sort (mapcar (lambda (thread)
4286                   (cons (car thread)
4287                         (and (cdr thread)
4288                              (gnus-sort-threads-1 (cdr thread) func))))
4289                 threads) func))
4290
4291 (defun gnus-sort-threads (threads)
4292   "Sort THREADS."
4293   (if (not gnus-thread-sort-functions)
4294       threads
4295     (gnus-message 8 "Sorting threads...")
4296     (let ((max-lisp-eval-depth 5000))
4297       (prog1 (gnus-sort-threads-1
4298          threads
4299          (gnus-make-sort-function gnus-thread-sort-functions))
4300         (gnus-message 8 "Sorting threads...done")))))
4301
4302 (defun gnus-sort-articles (articles)
4303   "Sort ARTICLES."
4304   (when gnus-article-sort-functions
4305     (gnus-message 7 "Sorting articles...")
4306     (prog1
4307         (setq gnus-newsgroup-headers
4308               (sort articles (gnus-make-sort-function
4309                               gnus-article-sort-functions)))
4310       (gnus-message 7 "Sorting articles...done"))))
4311
4312 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4313 (defmacro gnus-thread-header (thread)
4314   "Return header of first article in THREAD.
4315 Note that THREAD must never, ever be anything else than a variable -
4316 using some other form will lead to serious barfage."
4317   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4318   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4319   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4320         (vector thread) 2))
4321
4322 (defsubst gnus-article-sort-by-number (h1 h2)
4323   "Sort articles by article number."
4324   (< (mail-header-number h1)
4325      (mail-header-number h2)))
4326
4327 (defun gnus-thread-sort-by-number (h1 h2)
4328   "Sort threads by root article number."
4329   (gnus-article-sort-by-number
4330    (gnus-thread-header h1) (gnus-thread-header h2)))
4331
4332 (defsubst gnus-article-sort-by-random (h1 h2)
4333   "Sort articles by article number."
4334   (zerop (random 2)))
4335
4336 (defun gnus-thread-sort-by-random (h1 h2)
4337   "Sort threads by root article number."
4338   (gnus-article-sort-by-random
4339    (gnus-thread-header h1) (gnus-thread-header h2)))
4340
4341 (defsubst gnus-article-sort-by-lines (h1 h2)
4342   "Sort articles by article Lines header."
4343   (< (mail-header-lines h1)
4344      (mail-header-lines h2)))
4345
4346 (defun gnus-thread-sort-by-lines (h1 h2)
4347   "Sort threads by root article Lines header."
4348   (gnus-article-sort-by-lines
4349    (gnus-thread-header h1) (gnus-thread-header h2)))
4350
4351 (defsubst gnus-article-sort-by-chars (h1 h2)
4352   "Sort articles by octet length."
4353   (< (mail-header-chars h1)
4354      (mail-header-chars h2)))
4355
4356 (defun gnus-thread-sort-by-chars (h1 h2)
4357   "Sort threads by root article octet length."
4358   (gnus-article-sort-by-chars
4359    (gnus-thread-header h1) (gnus-thread-header h2)))
4360
4361 (defsubst gnus-article-sort-by-author (h1 h2)
4362   "Sort articles by root author."
4363   (string-lessp
4364    (let ((extract (funcall
4365                    gnus-extract-address-components
4366                    (mail-header-from h1))))
4367      (or (car extract) (cadr extract) ""))
4368    (let ((extract (funcall
4369                    gnus-extract-address-components
4370                    (mail-header-from h2))))
4371      (or (car extract) (cadr extract) ""))))
4372
4373 (defun gnus-thread-sort-by-author (h1 h2)
4374   "Sort threads by root author."
4375   (gnus-article-sort-by-author
4376    (gnus-thread-header h1)  (gnus-thread-header h2)))
4377
4378 (defsubst gnus-article-sort-by-subject (h1 h2)
4379   "Sort articles by root subject."
4380   (string-lessp
4381    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4382    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4383
4384 (defun gnus-thread-sort-by-subject (h1 h2)
4385   "Sort threads by root subject."
4386   (gnus-article-sort-by-subject
4387    (gnus-thread-header h1) (gnus-thread-header h2)))
4388
4389 (defsubst gnus-article-sort-by-date (h1 h2)
4390   "Sort articles by root article date."
4391   (time-less-p
4392    (gnus-date-get-time (mail-header-date h1))
4393    (gnus-date-get-time (mail-header-date h2))))
4394
4395 (defun gnus-thread-sort-by-date (h1 h2)
4396   "Sort threads by root article date."
4397   (gnus-article-sort-by-date
4398    (gnus-thread-header h1) (gnus-thread-header h2)))
4399
4400 (defsubst gnus-article-sort-by-score (h1 h2)
4401   "Sort articles by root article score.
4402 Unscored articles will be counted as having a score of zero."
4403   (> (or (cdr (assq (mail-header-number h1)
4404                     gnus-newsgroup-scored))
4405          gnus-summary-default-score 0)
4406      (or (cdr (assq (mail-header-number h2)
4407                     gnus-newsgroup-scored))
4408          gnus-summary-default-score 0)))
4409
4410 (defun gnus-thread-sort-by-score (h1 h2)
4411   "Sort threads by root article score."
4412   (gnus-article-sort-by-score
4413    (gnus-thread-header h1) (gnus-thread-header h2)))
4414
4415 (defun gnus-thread-sort-by-total-score (h1 h2)
4416   "Sort threads by the sum of all scores in the thread.
4417 Unscored articles will be counted as having a score of zero."
4418   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4419
4420 (defun gnus-thread-total-score (thread)
4421   ;; This function find the total score of THREAD.
4422   (cond
4423    ((null thread)
4424     0)
4425    ((consp thread)
4426     (if (stringp (car thread))
4427         (apply gnus-thread-score-function 0
4428                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4429       (gnus-thread-total-score-1 thread)))
4430    (t
4431     (gnus-thread-total-score-1 (list thread)))))
4432
4433 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4434   "Sort threads such that the thread with the most recently arrived article comes first."
4435   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4436
4437 (defun gnus-thread-highest-number (thread)
4438   "Return the highest article number in THREAD."
4439   (apply 'max (mapcar (lambda (header)
4440                         (mail-header-number header))
4441                       (message-flatten-list thread))))
4442
4443 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4444   "Sort threads such that the thread with the most recently dated article comes first."
4445   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4446
4447 (defun gnus-thread-latest-date (thread)
4448   "Return the highest article date in THREAD."
4449   (let ((previous-time 0))
4450     (apply 'max
4451            (mapcar
4452             (lambda (header)
4453               (setq previous-time
4454                     (time-to-seconds
4455                      (condition-case ()
4456                          (mail-header-parse-date (mail-header-date header))
4457                        (error previous-time)))))
4458             (sort
4459              (message-flatten-list thread)
4460              (lambda (h1 h2)
4461                (< (mail-header-number h1)
4462                   (mail-header-number h2))))))))
4463
4464 (defun gnus-thread-total-score-1 (root)
4465   ;; This function find the total score of the thread below ROOT.
4466   (setq root (car root))
4467   (apply gnus-thread-score-function
4468          (or (append
4469               (mapcar 'gnus-thread-total-score
4470                       (cdr (gnus-id-to-thread (mail-header-id root))))
4471               (when (> (mail-header-number root) 0)
4472                 (list (or (cdr (assq (mail-header-number root)
4473                                      gnus-newsgroup-scored))
4474                           gnus-summary-default-score 0))))
4475              (list gnus-summary-default-score)
4476              '(0))))
4477
4478 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4479 (defvar gnus-tmp-prev-subject nil)
4480 (defvar gnus-tmp-false-parent nil)
4481 (defvar gnus-tmp-root-expunged nil)
4482 (defvar gnus-tmp-dummy-line nil)
4483
4484 (eval-when-compile (defvar gnus-tmp-header))
4485 (defun gnus-extra-header (type &optional header)
4486   "Return the extra header of TYPE."
4487   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4488       ""))
4489
4490 (defvar gnus-tmp-thread-tree-header-string "")
4491
4492 (defcustom gnus-sum-thread-tree-root "> "
4493   "With %B spec, used for the root of a thread.
4494 If nil, use subject instead."
4495   :type 'string
4496   :group 'gnus-thread)
4497 (defcustom gnus-sum-thread-tree-single-indent ""
4498   "With %B spec, used for a thread with just one message.
4499 If nil, use subject instead."
4500   :type 'string
4501   :group 'gnus-thread)
4502 (defcustom gnus-sum-thread-tree-vertical "| "
4503   "With %B spec, used for drawing a vertical line."
4504   :type 'string
4505   :group 'gnus-thread)
4506 (defcustom gnus-sum-thread-tree-indent "  "
4507   "With %B spec, used for indenting."
4508   :type 'string
4509   :group 'gnus-thread)
4510 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4511   "With %B spec, used for a leaf with brothers."
4512   :type 'string
4513   :group 'gnus-thread)
4514 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4515   "With %B spec, used for a leaf without brothers."
4516   :type 'string
4517   :group 'gnus-thread)
4518
4519 (defun gnus-summary-prepare-threads (threads)
4520   "Prepare summary buffer from THREADS and indentation LEVEL.
4521 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4522 or a straight list of headers."
4523   (gnus-message 7 "Generating summary...")
4524
4525   (setq gnus-newsgroup-threads threads)
4526   (beginning-of-line)
4527
4528   (let ((gnus-tmp-level 0)
4529         (default-score (or gnus-summary-default-score 0))
4530         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4531         (building-line-count gnus-summary-display-while-building)
4532         (building-count (integerp gnus-summary-display-while-building))
4533         thread number subject stack state gnus-tmp-gathered beg-match
4534         new-roots gnus-tmp-new-adopts thread-end simp-subject
4535         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4536         gnus-tmp-replied gnus-tmp-subject-or-nil
4537         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4538         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4539         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4540         tree-stack)
4541
4542     (setq gnus-tmp-prev-subject nil)
4543
4544     (if (vectorp (car threads))
4545         ;; If this is a straight (sic) list of headers, then a
4546         ;; threaded summary display isn't required, so we just create
4547         ;; an unthreaded one.
4548         (gnus-summary-prepare-unthreaded threads)
4549
4550       ;; Do the threaded display.
4551
4552       (if gnus-summary-display-while-building
4553           (switch-to-buffer (buffer-name)))
4554       (while (or threads stack gnus-tmp-new-adopts new-roots)
4555
4556         (if (and (= gnus-tmp-level 0)
4557                  (or (not stack)
4558                      (= (caar stack) 0))
4559                  (not gnus-tmp-false-parent)
4560                  (or gnus-tmp-new-adopts new-roots))
4561             (if gnus-tmp-new-adopts
4562                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4563                       thread (list (car gnus-tmp-new-adopts))
4564                       gnus-tmp-header (caar thread)
4565                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4566               (when new-roots
4567                 (setq thread (list (car new-roots))
4568                       gnus-tmp-header (caar thread)
4569                       new-roots (cdr new-roots))))
4570
4571           (if threads
4572               ;; If there are some threads, we do them before the
4573               ;; threads on the stack.
4574               (setq thread threads
4575                     gnus-tmp-header (caar thread))
4576             ;; There were no current threads, so we pop something off
4577             ;; the stack.
4578             (setq state (car stack)
4579                   gnus-tmp-level (car state)
4580                   tree-stack (cadr state)
4581                   thread (caddr state)
4582                   stack (cdr stack)
4583                   gnus-tmp-header (caar thread))))
4584
4585         (setq gnus-tmp-false-parent nil)
4586         (setq gnus-tmp-root-expunged nil)
4587         (setq thread-end nil)
4588
4589         (if (stringp gnus-tmp-header)
4590             ;; The header is a dummy root.
4591             (cond
4592              ((eq gnus-summary-make-false-root 'adopt)
4593               ;; We let the first article adopt the rest.
4594               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4595                                                (cddar thread)))
4596               (setq gnus-tmp-gathered
4597                     (nconc (mapcar
4598                             (lambda (h) (mail-header-number (car h)))
4599                             (cddar thread))
4600                            gnus-tmp-gathered))
4601               (setq thread (cons (list (caar thread)
4602                                        (cadar thread))
4603                                  (cdr thread)))
4604               (setq gnus-tmp-level -1
4605                     gnus-tmp-false-parent t))
4606              ((eq gnus-summary-make-false-root 'empty)
4607               ;; We print adopted articles with empty subject fields.
4608               (setq gnus-tmp-gathered
4609                     (nconc (mapcar
4610                             (lambda (h) (mail-header-number (car h)))
4611                             (cddar thread))
4612                            gnus-tmp-gathered))
4613               (setq gnus-tmp-level -1))
4614              ((eq gnus-summary-make-false-root 'dummy)
4615               ;; We remember that we probably want to output a dummy
4616               ;; root.
4617               (setq gnus-tmp-dummy-line gnus-tmp-header)
4618               (setq gnus-tmp-prev-subject gnus-tmp-header))
4619              (t
4620               ;; We do not make a root for the gathered
4621               ;; sub-threads at all.
4622               (setq gnus-tmp-level -1)))
4623
4624           (setq number (mail-header-number gnus-tmp-header)
4625                 subject (mail-header-subject gnus-tmp-header)
4626                 simp-subject (gnus-simplify-subject-fully subject))
4627
4628           (cond
4629            ;; If the thread has changed subject, we might want to make
4630            ;; this subthread into a root.
4631            ((and (null gnus-thread-ignore-subject)
4632                  (not (zerop gnus-tmp-level))
4633                  gnus-tmp-prev-subject
4634                  (not (string= gnus-tmp-prev-subject simp-subject)))
4635             (setq new-roots (nconc new-roots (list (car thread)))
4636                   thread-end t
4637                   gnus-tmp-header nil))
4638            ;; If the article lies outside the current limit,
4639            ;; then we do not display it.
4640            ((not (memq number gnus-newsgroup-limit))
4641             (setq gnus-tmp-gathered
4642                   (nconc (mapcar
4643                           (lambda (h) (mail-header-number (car h)))
4644                           (cdar thread))
4645                          gnus-tmp-gathered))
4646             (setq gnus-tmp-new-adopts (if (cdar thread)
4647                                           (append gnus-tmp-new-adopts
4648                                                   (cdar thread))
4649                                         gnus-tmp-new-adopts)
4650                   thread-end t
4651                   gnus-tmp-header nil)
4652             (when (zerop gnus-tmp-level)
4653               (setq gnus-tmp-root-expunged t)))
4654            ;; Perhaps this article is to be marked as read?
4655            ((and gnus-summary-mark-below
4656                  (< (or (cdr (assq number gnus-newsgroup-scored))
4657                         default-score)
4658                     gnus-summary-mark-below)
4659                  ;; Don't touch sparse articles.
4660                  (not (gnus-summary-article-sparse-p number))
4661                  (not (gnus-summary-article-ancient-p number)))
4662             (setq gnus-newsgroup-unreads
4663                   (delq number gnus-newsgroup-unreads))
4664             (if gnus-newsgroup-auto-expire
4665                 (setq gnus-newsgroup-expirable
4666                       (gnus-add-to-sorted-list
4667                        gnus-newsgroup-expirable number))
4668               (push (cons number gnus-low-score-mark)
4669                     gnus-newsgroup-reads))))
4670
4671           (when gnus-tmp-header
4672             ;; We may have an old dummy line to output before this
4673             ;; article.
4674             (when (and gnus-tmp-dummy-line
4675                        (gnus-subject-equal
4676                         gnus-tmp-dummy-line
4677                         (mail-header-subject gnus-tmp-header)))
4678               (gnus-summary-insert-dummy-line
4679                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4680               (setq gnus-tmp-dummy-line nil))
4681
4682             ;; Compute the mark.
4683             (setq gnus-tmp-unread (gnus-article-mark number))
4684
4685             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4686                                   gnus-tmp-header gnus-tmp-level)
4687                   gnus-newsgroup-data)
4688
4689             ;; Actually insert the line.
4690             (setq
4691              gnus-tmp-subject-or-nil
4692              (cond
4693               ((and gnus-thread-ignore-subject
4694                     gnus-tmp-prev-subject
4695                     (not (string= gnus-tmp-prev-subject simp-subject)))
4696                subject)
4697               ((zerop gnus-tmp-level)
4698                (if (and (eq gnus-summary-make-false-root 'empty)
4699                         (memq number gnus-tmp-gathered)
4700                         gnus-tmp-prev-subject
4701                         (string= gnus-tmp-prev-subject simp-subject))
4702                    gnus-summary-same-subject
4703                  subject))
4704               (t gnus-summary-same-subject)))
4705             (if (and (eq gnus-summary-make-false-root 'adopt)
4706                      (= gnus-tmp-level 1)
4707                      (memq number gnus-tmp-gathered))
4708                 (setq gnus-tmp-opening-bracket ?\<
4709                       gnus-tmp-closing-bracket ?\>)
4710               (setq gnus-tmp-opening-bracket ?\[
4711                     gnus-tmp-closing-bracket ?\]))
4712             (setq
4713              gnus-tmp-indentation
4714              (aref gnus-thread-indent-array gnus-tmp-level)
4715              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4716              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4717                                 gnus-summary-default-score 0)
4718              gnus-tmp-score-char
4719              (if (or (null gnus-summary-default-score)
4720                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4721                          gnus-summary-zcore-fuzz))
4722                  ?                      ;Whitespace
4723                (if (< gnus-tmp-score gnus-summary-default-score)
4724                    gnus-score-below-mark gnus-score-over-mark))
4725              gnus-tmp-replied
4726              (cond ((memq number gnus-newsgroup-processable)
4727                     gnus-process-mark)
4728                    ((memq number gnus-newsgroup-cached)
4729                     gnus-cached-mark)
4730                    ((memq number gnus-newsgroup-replied)
4731                     gnus-replied-mark)
4732                    ((memq number gnus-newsgroup-forwarded)
4733                     gnus-forwarded-mark)
4734                    ((memq number gnus-newsgroup-saved)
4735                     gnus-saved-mark)
4736                    ((memq number gnus-newsgroup-recent)
4737                     gnus-recent-mark)
4738                    ((memq number gnus-newsgroup-unseen)
4739                     gnus-unseen-mark)
4740                    (t gnus-no-mark))
4741              gnus-tmp-downloaded
4742              (cond ((memq number gnus-newsgroup-undownloaded) 
4743                     gnus-undownloaded-mark)
4744                    (gnus-newsgroup-agentized
4745                     gnus-downloaded-mark)
4746                    (t
4747                     gnus-no-mark))
4748              gnus-tmp-from (mail-header-from gnus-tmp-header)
4749              gnus-tmp-name
4750              (cond
4751               ((string-match "<[^>]+> *$" gnus-tmp-from)
4752                (setq beg-match (match-beginning 0))
4753                (or (and (string-match "^\".+\"" gnus-tmp-from)
4754                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4755                    (substring gnus-tmp-from 0 beg-match)))
4756               ((string-match "(.+)" gnus-tmp-from)
4757                (substring gnus-tmp-from
4758                           (1+ (match-beginning 0)) (1- (match-end 0))))
4759               (t gnus-tmp-from))
4760              gnus-tmp-thread-tree-header-string
4761              (cond
4762               ((not gnus-show-threads) "")
4763               ((zerop gnus-tmp-level)
4764                (if (cdar thread)
4765                    (or gnus-sum-thread-tree-root subject)
4766                  (or gnus-sum-thread-tree-single-indent subject)))
4767               (t
4768                (concat (apply 'concat
4769                               (mapcar (lambda (item)
4770                                         (if (= item 1)
4771                                             gnus-sum-thread-tree-vertical
4772                                           gnus-sum-thread-tree-indent))
4773                                       (cdr (reverse tree-stack))))
4774                        (if (nth 1 thread)
4775                            gnus-sum-thread-tree-leaf-with-other
4776                          gnus-sum-thread-tree-single-leaf)))))
4777             (when (string= gnus-tmp-name "")
4778               (setq gnus-tmp-name gnus-tmp-from))
4779             (unless (numberp gnus-tmp-lines)
4780               (setq gnus-tmp-lines -1))
4781             (if (= gnus-tmp-lines -1)
4782                 (setq gnus-tmp-lines "?")
4783               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4784             (gnus-put-text-property
4785              (point)
4786              (progn (eval gnus-summary-line-format-spec) (point))
4787              'gnus-number number)
4788             (when gnus-visual-p
4789               (forward-line -1)
4790               (gnus-run-hooks 'gnus-summary-update-hook)
4791               (forward-line 1))
4792
4793             (setq gnus-tmp-prev-subject simp-subject)))
4794
4795         (when (nth 1 thread)
4796           (push (list (max 0 gnus-tmp-level)
4797                       (copy-list tree-stack)
4798                       (nthcdr 1 thread))
4799                 stack))
4800         (push (if (nth 1 thread) 1 0) tree-stack)
4801         (incf gnus-tmp-level)
4802         (setq threads (if thread-end nil (cdar thread)))
4803         (if gnus-summary-display-while-building
4804             (if building-count
4805                 (progn
4806                   ;; use a set frequency
4807                   (setq building-line-count (1- building-line-count))
4808                   (when (= building-line-count 0)
4809                     (sit-for 0)
4810                     (setq building-line-count
4811                           gnus-summary-display-while-building)))
4812               ;; always
4813               (sit-for 0)))
4814         (unless threads
4815           (setq gnus-tmp-level 0)))))
4816   (gnus-message 7 "Generating summary...done"))
4817
4818 (defun gnus-summary-prepare-unthreaded (headers)
4819   "Generate an unthreaded summary buffer based on HEADERS."
4820   (let (header number mark)
4821
4822     (beginning-of-line)
4823
4824     (while headers
4825       ;; We may have to root out some bad articles...
4826       (when (memq (setq number (mail-header-number
4827                                 (setq header (pop headers))))
4828                   gnus-newsgroup-limit)
4829         ;; Mark article as read when it has a low score.
4830         (when (and gnus-summary-mark-below
4831                    (< (or (cdr (assq number gnus-newsgroup-scored))
4832                           gnus-summary-default-score 0)
4833                       gnus-summary-mark-below)
4834                    (not (gnus-summary-article-ancient-p number)))
4835           (setq gnus-newsgroup-unreads
4836                 (delq number gnus-newsgroup-unreads))
4837           (if gnus-newsgroup-auto-expire
4838               (push number gnus-newsgroup-expirable)
4839             (push (cons number gnus-low-score-mark)
4840                   gnus-newsgroup-reads)))
4841
4842         (setq mark (gnus-article-mark number))
4843         (push (gnus-data-make number mark (1+ (point)) header 0)
4844               gnus-newsgroup-data)
4845         (gnus-summary-insert-line
4846          header 0 number
4847          (memq number gnus-newsgroup-undownloaded)
4848          mark (memq number gnus-newsgroup-replied)
4849          (memq number gnus-newsgroup-expirable)
4850          (mail-header-subject header) nil
4851          (cdr (assq number gnus-newsgroup-scored))
4852          (memq number gnus-newsgroup-processable))))))
4853
4854 (defun gnus-summary-remove-list-identifiers ()
4855   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4856   (let ((regexp (if (consp gnus-list-identifiers)
4857                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4858                   gnus-list-identifiers))
4859         changed subject)
4860     (when regexp
4861       (dolist (header gnus-newsgroup-headers)
4862         (setq subject (mail-header-subject header)
4863               changed nil)
4864         (while (string-match
4865                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4866                 subject)
4867           (setq subject
4868                 (concat (substring subject 0 (match-beginning 2))
4869                         (substring subject (match-end 0)))
4870                 changed t))
4871         (when (and changed
4872                    (string-match
4873                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4874           (setq subject
4875                 (concat (substring subject 0 (match-beginning 1))
4876                         (substring subject (match-end 1)))))
4877         (when changed
4878           (mail-header-set-subject header subject))))))
4879
4880 (defun gnus-fetch-headers (articles)
4881   "Fetch headers of ARTICLES."
4882   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4883     (gnus-message 5 "Fetching headers for %s..." name)
4884     (prog1
4885         (if (eq 'nov
4886                 (setq gnus-headers-retrieved-by
4887                       (gnus-retrieve-headers
4888                        articles gnus-newsgroup-name
4889                        ;; We might want to fetch old headers, but
4890                        ;; not if there is only 1 article.
4891                        (and (or (and
4892                                  (not (eq gnus-fetch-old-headers 'some))
4893                                  (not (numberp gnus-fetch-old-headers)))
4894                                 (> (length articles) 1))
4895                             gnus-fetch-old-headers))))
4896             (gnus-get-newsgroup-headers-xover
4897              articles nil nil gnus-newsgroup-name t)
4898           (gnus-get-newsgroup-headers))
4899       (gnus-message 5 "Fetching headers for %s...done" name))))
4900
4901 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4902   "Select newsgroup GROUP.
4903 If READ-ALL is non-nil, all articles in the group are selected.
4904 If SELECT-ARTICLES, only select those articles from GROUP."
4905   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4906          ;;!!! Dirty hack; should be removed.
4907          (gnus-summary-ignore-duplicates
4908           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4909               t
4910             gnus-summary-ignore-duplicates))
4911          (info (nth 2 entry))
4912          articles fetched-articles cached)
4913
4914     (unless (gnus-check-server
4915              (set (make-local-variable 'gnus-current-select-method)
4916                   (gnus-find-method-for-group group)))
4917       (error "Couldn't open server"))
4918
4919     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4920         (gnus-activate-group group)     ; Or we can activate it...
4921         (progn                          ; Or we bug out.
4922           (when (equal major-mode 'gnus-summary-mode)
4923             (kill-buffer (current-buffer)))
4924           (error "Couldn't activate group %s: %s"
4925                  group (gnus-status-message group))))
4926
4927     (unless (gnus-request-group group t)
4928       (when (equal major-mode 'gnus-summary-mode)
4929         (kill-buffer (current-buffer)))
4930       (error "Couldn't request group %s: %s"
4931              group (gnus-status-message group)))
4932
4933     (setq gnus-newsgroup-name group
4934           gnus-newsgroup-unselected nil
4935           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4936
4937     (let ((display (gnus-group-find-parameter group 'display)))
4938       (setq gnus-newsgroup-display
4939             (cond
4940              ((not (zerop (or (car-safe read-all) 0)))
4941               ;; The user entered the group with C-u SPC/RET, let's show
4942               ;; all articles.
4943               'gnus-not-ignore)
4944              ((eq display 'all)
4945               'gnus-not-ignore)
4946              ((arrayp display)
4947               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4948              ((numberp display)
4949               ;; The following is probably the "correct" solution, but
4950               ;; it makes Gnus fetch all headers and then limit the
4951               ;; articles (which is slow), so instead we hack the
4952               ;; select-articles parameter instead. -- Simon Josefsson
4953               ;; <jas@kth.se>
4954               ;;
4955               ;; (gnus-byte-compile
4956               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4957               ;;                         display)))))
4958               (setq select-articles
4959                     (gnus-uncompress-range
4960                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4961                              (if (> tmp 0)
4962                                  tmp
4963                                1))
4964                            (cdr (gnus-active group)))))
4965               nil)
4966              (t
4967               nil))))
4968
4969     (gnus-summary-setup-default-charset)
4970
4971     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4972     (when (gnus-virtual-group-p group)
4973       (setq cached gnus-newsgroup-cached))
4974
4975     (setq gnus-newsgroup-unreads
4976           (gnus-sorted-ndifference
4977            (gnus-sorted-ndifference gnus-newsgroup-unreads
4978                                     gnus-newsgroup-marked)
4979            gnus-newsgroup-dormant))
4980
4981     (setq gnus-newsgroup-processable nil)
4982
4983     (gnus-update-read-articles group gnus-newsgroup-unreads)
4984
4985     ;; Adjust and set lists of article marks.
4986     (when info
4987       (gnus-adjust-marked-articles info))
4988     (if (setq articles select-articles)
4989         (setq gnus-newsgroup-unselected
4990               (gnus-sorted-difference gnus-newsgroup-unreads articles))
4991       (setq articles (gnus-articles-to-read group read-all)))
4992
4993     (cond
4994      ((null articles)
4995       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4996       'quit)
4997      ((eq articles 0) nil)
4998      (t
4999       ;; Init the dependencies hash table.
5000       (setq gnus-newsgroup-dependencies
5001             (gnus-make-hashtable (length articles)))
5002       (gnus-set-global-variables)
5003       ;; Retrieve the headers and read them in.
5004
5005       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5006
5007       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5008       (when cached
5009         (setq gnus-newsgroup-cached cached))
5010
5011       ;; Suppress duplicates?
5012       (when gnus-suppress-duplicates
5013         (gnus-dup-suppress-articles))
5014
5015       ;; Set the initial limit.
5016       (setq gnus-newsgroup-limit (copy-sequence articles))
5017       ;; Remove canceled articles from the list of unread articles.
5018       (setq fetched-articles
5019             (mapcar (lambda (headers) (mail-header-number headers))
5020                     gnus-newsgroup-headers))
5021       (setq gnus-newsgroup-articles fetched-articles)
5022       (setq gnus-newsgroup-unreads
5023             (gnus-sorted-nintersection
5024              gnus-newsgroup-unreads fetched-articles))
5025       (gnus-compute-unseen-list)
5026
5027       ;; Removed marked articles that do not exist.
5028       (gnus-update-missing-marks
5029        (gnus-sorted-difference articles fetched-articles))
5030       ;; We might want to build some more threads first.
5031       (when (and gnus-fetch-old-headers
5032                  (eq gnus-headers-retrieved-by 'nov))
5033         (if (eq gnus-fetch-old-headers 'invisible)
5034             (gnus-build-all-threads)
5035           (gnus-build-old-threads)))
5036       ;; Let the Gnus agent mark articles as read.
5037       (when gnus-agent
5038         (gnus-agent-get-undownloaded-list))
5039       ;; Remove list identifiers from subject
5040       (when gnus-list-identifiers
5041         (gnus-summary-remove-list-identifiers))
5042       ;; Check whether auto-expire is to be done in this group.
5043       (setq gnus-newsgroup-auto-expire
5044             (gnus-group-auto-expirable-p group))
5045       ;; Set up the article buffer now, if necessary.
5046       (unless gnus-single-article-buffer
5047         (gnus-article-setup-buffer))
5048       ;; First and last article in this newsgroup.
5049       (when gnus-newsgroup-headers
5050         (setq gnus-newsgroup-begin
5051               (mail-header-number (car gnus-newsgroup-headers))
5052               gnus-newsgroup-end
5053               (mail-header-number
5054                (gnus-last-element gnus-newsgroup-headers))))
5055       ;; GROUP is successfully selected.
5056       (or gnus-newsgroup-headers t)))))
5057
5058 (defun gnus-compute-unseen-list ()
5059   ;; The `seen' marks are treated specially.
5060   (if (not gnus-newsgroup-seen)
5061       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5062     (setq gnus-newsgroup-unseen
5063           (gnus-inverse-list-range-intersection
5064            gnus-newsgroup-articles gnus-newsgroup-seen))))
5065
5066 (defun gnus-summary-display-make-predicate (display)
5067   (require 'gnus-agent)
5068   (when (= (length display) 1)
5069     (setq display (car display)))
5070   (unless gnus-summary-display-cache
5071     (dolist (elem (append '((unread . unread)
5072                             (read . read)
5073                             (unseen . unseen))
5074                           gnus-article-mark-lists))
5075       (push (cons (cdr elem)
5076                   (gnus-byte-compile
5077                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5078             gnus-summary-display-cache)))
5079   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5080         (gnus-category-predicate-cache gnus-summary-display-cache))
5081     (gnus-get-predicate display)))
5082
5083 ;; Uses the dynamically bound `number' variable.
5084 (eval-when-compile
5085   (defvar number))
5086 (defun gnus-article-marked-p (type &optional article)
5087   (let ((article (or article number)))
5088     (cond
5089      ((eq type 'tick)
5090       (memq article gnus-newsgroup-marked))
5091      ((eq type 'spam)
5092       (memq article gnus-newsgroup-spam-marked))
5093      ((eq type 'unsend)
5094       (memq article gnus-newsgroup-unsendable))
5095      ((eq type 'undownload)
5096       (memq article gnus-newsgroup-undownloaded))
5097      ((eq type 'download)
5098       (memq article gnus-newsgroup-downloadable))
5099      ((eq type 'unread)
5100       (memq article gnus-newsgroup-unreads))
5101      ((eq type 'read)
5102       (memq article gnus-newsgroup-reads))
5103      ((eq type 'dormant)
5104       (memq article gnus-newsgroup-dormant) )
5105      ((eq type 'expire)
5106       (memq article gnus-newsgroup-expirable))
5107      ((eq type 'reply)
5108       (memq article gnus-newsgroup-replied))
5109      ((eq type 'killed)
5110       (memq article gnus-newsgroup-killed))
5111      ((eq type 'bookmark)
5112       (assq article gnus-newsgroup-bookmarks))
5113      ((eq type 'score)
5114       (assq article gnus-newsgroup-scored))
5115      ((eq type 'save)
5116       (memq article gnus-newsgroup-saved))
5117      ((eq type 'cache)
5118       (memq article gnus-newsgroup-cached))
5119      ((eq type 'forward)
5120       (memq article gnus-newsgroup-forwarded))
5121      ((eq type 'seen)
5122       (not (memq article gnus-newsgroup-unseen)))
5123      ((eq type 'recent)
5124       (memq article gnus-newsgroup-recent))
5125      (t t))))
5126
5127 (defun gnus-articles-to-read (group &optional read-all)
5128   "Find out what articles the user wants to read."
5129   (let* ((display (gnus-group-find-parameter group 'display))
5130          (articles
5131           ;; Select all articles if `read-all' is non-nil, or if there
5132           ;; are no unread articles.
5133           (if (or read-all
5134                   (and (zerop (length gnus-newsgroup-marked))
5135                        (zerop (length gnus-newsgroup-unreads)))
5136                   ;; Fetch all if the predicate is non-nil.
5137                   gnus-newsgroup-display)
5138               ;; We want to select the headers for all the articles in
5139               ;; the group, so we select either all the active
5140               ;; articles in the group, or (if that's nil), the
5141               ;; articles in the cache.
5142               (or
5143                (gnus-uncompress-range (gnus-active group))
5144                (gnus-cache-articles-in-group group))
5145             ;; Select only the "normal" subset of articles.
5146             (gnus-sorted-nunion
5147              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5148              gnus-newsgroup-unreads)))
5149          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5150          (scored (length scored-list))
5151          (number (length articles))
5152          (marked (+ (length gnus-newsgroup-marked)
5153                     (length gnus-newsgroup-dormant)))
5154          (select
5155           (cond
5156            ((numberp read-all)
5157             read-all)
5158            ((numberp gnus-newsgroup-display)
5159             gnus-newsgroup-display)
5160            (t
5161             (condition-case ()
5162                 (cond
5163                  ((and (or (<= scored marked) (= scored number))
5164                        (numberp gnus-large-newsgroup)
5165                        (> number gnus-large-newsgroup))
5166                   (let* ((cursor-in-echo-area nil)
5167                          (initial (gnus-parameter-large-newsgroup-initial
5168                                    gnus-newsgroup-name))
5169                          (input
5170                           (read-string
5171                            (format
5172                             "How many articles from %s (%s %d): "
5173                             (gnus-limit-string
5174                              (gnus-group-decoded-name gnus-newsgroup-name)
5175                              35)
5176                             (if initial "max" "default")
5177                             number)
5178                            (if initial
5179                                (cons (number-to-string initial)
5180                                      0)))))
5181                     (if (string-match "^[ \t]*$" input) number input)))
5182                  ((and (> scored marked) (< scored number)
5183                        (> (- scored number) 20))
5184                   (let ((input
5185                          (read-string
5186                           (format "%s %s (%d scored, %d total): "
5187                                   "How many articles from"
5188                                   (gnus-group-decoded-name group)
5189                                   scored number))))
5190                     (if (string-match "^[ \t]*$" input)
5191                         number input)))
5192                  (t number))
5193               (quit
5194                (message "Quit getting the articles to read")
5195                nil))))))
5196     (setq select (if (stringp select) (string-to-number select) select))
5197     (if (or (null select) (zerop select))
5198         select
5199       (if (and (not (zerop scored)) (<= (abs select) scored))
5200           (progn
5201             (setq articles (sort scored-list '<))
5202             (setq number (length articles)))
5203         (setq articles (copy-sequence articles)))
5204
5205       (when (< (abs select) number)
5206         (if (< select 0)
5207             ;; Select the N oldest articles.
5208             (setcdr (nthcdr (1- (abs select)) articles) nil)
5209           ;; Select the N most recent articles.
5210           (setq articles (nthcdr (- number select) articles))))
5211       (setq gnus-newsgroup-unselected
5212             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5213       (when gnus-alter-articles-to-read-function
5214         (setq gnus-newsgroup-unreads
5215               (sort
5216                (funcall gnus-alter-articles-to-read-function
5217                         gnus-newsgroup-name gnus-newsgroup-unreads)
5218                '<)))
5219       articles)))
5220
5221 (defun gnus-killed-articles (killed articles)
5222   (let (out)
5223     (while articles
5224       (when (inline (gnus-member-of-range (car articles) killed))
5225         (push (car articles) out))
5226       (setq articles (cdr articles)))
5227     out))
5228
5229 (defun gnus-uncompress-marks (marks)
5230   "Uncompress the mark ranges in MARKS."
5231   (let ((uncompressed '(score bookmark))
5232         out)
5233     (while marks
5234       (if (memq (caar marks) uncompressed)
5235           (push (car marks) out)
5236         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5237       (setq marks (cdr marks)))
5238     out))
5239
5240 (defun gnus-article-mark-to-type (mark)
5241   "Return the type of MARK."
5242   (or (cadr (assq mark gnus-article-special-mark-lists))
5243       'list))
5244
5245 (defun gnus-article-unpropagatable-p (mark)
5246   "Return whether MARK should be propagated to backend."
5247   (memq mark gnus-article-unpropagated-mark-lists))
5248
5249 (defun gnus-adjust-marked-articles (info)
5250   "Set all article lists and remove all marks that are no longer valid."
5251   (let* ((marked-lists (gnus-info-marks info))
5252          (active (gnus-active (gnus-info-group info)))
5253          (min (car active))
5254          (max (cdr active))
5255          (types gnus-article-mark-lists)
5256          marks var articles article mark mark-type)
5257
5258     (dolist (marks marked-lists)
5259       (setq mark (car marks)
5260             mark-type (gnus-article-mark-to-type mark)
5261             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5262
5263       ;; We set the variable according to the type of the marks list,
5264       ;; and then adjust the marks to a subset of the active articles.
5265       (cond
5266        ;; Adjust "simple" lists.
5267        ((eq mark-type 'list)
5268         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5269         (when (memq mark '(tick dormant expire reply save))
5270           (while articles
5271             (when (or (< (setq article (pop articles)) min) (> article max))
5272               (set var (delq article (symbol-value var)))))))
5273        ;; Adjust assocs.
5274        ((eq mark-type 'tuple)
5275         (set var (setq articles (cdr marks)))
5276         (when (not (listp (cdr (symbol-value var))))
5277           (set var (list (symbol-value var))))
5278         (when (not (listp (cdr articles)))
5279           (setq articles (list articles)))
5280         (while articles
5281           (when (or (not (consp (setq article (pop articles))))
5282                     (< (car article) min)
5283                     (> (car article) max))
5284             (set var (delq article (symbol-value var))))))
5285        ;; Adjust ranges (sloppily).
5286        ((eq mark-type 'range)
5287         (cond
5288          ((eq mark 'seen)
5289           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5290           ;; It should be (seen (NUM1 . NUM2)).
5291           (when (numberp (cddr marks))
5292             (setcdr marks (list (cdr marks))))
5293           (setq articles (cdr marks))
5294           (while (and articles
5295                       (or (and (consp (car articles))
5296                                (> min (cdar articles)))
5297                           (and (numberp (car articles))
5298                                (> min (car articles)))))
5299             (pop articles))
5300           (set var articles))))))))
5301
5302 (defun gnus-update-missing-marks (missing)
5303   "Go through the list of MISSING articles and remove them from the mark lists."
5304   (when missing
5305     (let (var m)
5306       ;; Go through all types.
5307       (dolist (elem gnus-article-mark-lists)
5308         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5309           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5310           (when (symbol-value var)
5311             ;; This list has articles.  So we delete all missing
5312             ;; articles from it.
5313             (setq m missing)
5314             (while m
5315               (set var (delq (pop m) (symbol-value var))))))))))
5316
5317 (defun gnus-update-marks ()
5318   "Enter the various lists of marked articles into the newsgroup info list."
5319   (let ((types gnus-article-mark-lists)
5320         (info (gnus-get-info gnus-newsgroup-name))
5321         type list newmarked symbol delta-marks)
5322     (when info
5323       ;; Add all marks lists to the list of marks lists.
5324       (while (setq type (pop types))
5325         (setq list (symbol-value
5326                     (setq symbol
5327                           (intern (format "gnus-newsgroup-%s" (car type))))))
5328
5329         (when list
5330           ;; Get rid of the entries of the articles that have the
5331           ;; default score.
5332           (when (and (eq (cdr type) 'score)
5333                      gnus-save-score
5334                      list)
5335             (let* ((arts list)
5336                    (prev (cons nil list))
5337                    (all prev))
5338               (while arts
5339                 (if (or (not (consp (car arts)))
5340                         (= (cdar arts) gnus-summary-default-score))
5341                     (setcdr prev (cdr arts))
5342                   (setq prev arts))
5343                 (setq arts (cdr arts)))
5344               (setq list (cdr all)))))
5345
5346         (when (eq (cdr type) 'seen)
5347           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5348
5349         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5350           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5351
5352         (when (and (gnus-check-backend-function
5353                     'request-set-mark gnus-newsgroup-name)
5354                    (not (gnus-article-unpropagatable-p (cdr type))))
5355           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5356                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5357                  (add (gnus-remove-from-range
5358                        (gnus-copy-sequence list) old)))
5359             (when add
5360               (push (list add 'add (list (cdr type))) delta-marks))
5361             (when del
5362               (push (list del 'del (list (cdr type))) delta-marks))))
5363
5364         (when list
5365           (push (cons (cdr type) list) newmarked)))
5366
5367       (when delta-marks
5368         (unless (gnus-check-group gnus-newsgroup-name)
5369           (error "Can't open server for %s" gnus-newsgroup-name))
5370         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5371
5372       ;; Enter these new marks into the info of the group.
5373       (if (nthcdr 3 info)
5374           (setcar (nthcdr 3 info) newmarked)
5375         ;; Add the marks lists to the end of the info.
5376         (when newmarked
5377           (setcdr (nthcdr 2 info) (list newmarked))))
5378
5379       ;; Cut off the end of the info if there's nothing else there.
5380       (let ((i 5))
5381         (while (and (> i 2)
5382                     (not (nth i info)))
5383           (when (nthcdr (decf i) info)
5384             (setcdr (nthcdr i info) nil)))))))
5385
5386 (defun gnus-set-mode-line (where)
5387   "Set the mode line of the article or summary buffers.
5388 If WHERE is `summary', the summary mode line format will be used."
5389   ;; Is this mode line one we keep updated?
5390   (when (and (memq where gnus-updated-mode-lines)
5391              (symbol-value
5392               (intern (format "gnus-%s-mode-line-format-spec" where))))
5393     (let (mode-string)
5394       (save-excursion
5395         ;; We evaluate this in the summary buffer since these
5396         ;; variables are buffer-local to that buffer.
5397         (set-buffer gnus-summary-buffer)
5398        ;; We bind all these variables that are used in the `eval' form
5399         ;; below.
5400         (let* ((mformat (symbol-value
5401                          (intern
5402                           (format "gnus-%s-mode-line-format-spec" where))))
5403                (gnus-tmp-group-name (gnus-group-decoded-name
5404                                      gnus-newsgroup-name))
5405                (gnus-tmp-article-number (or gnus-current-article 0))
5406                (gnus-tmp-unread gnus-newsgroup-unreads)
5407                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5408                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5409                (gnus-tmp-unread-and-unselected
5410                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5411                             (zerop gnus-tmp-unselected))
5412                        "")
5413                       ((zerop gnus-tmp-unselected)
5414                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5415                       (t (format "{%d(+%d) more}"
5416                                  gnus-tmp-unread-and-unticked
5417                                  gnus-tmp-unselected))))
5418                (gnus-tmp-subject
5419                 (if (and gnus-current-headers
5420                          (vectorp gnus-current-headers))
5421                     (gnus-mode-string-quote
5422                      (mail-header-subject gnus-current-headers))
5423                   ""))
5424                bufname-length max-len
5425                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5426           (setq mode-string (eval mformat))
5427           (setq bufname-length (if (string-match "%b" mode-string)
5428                                    (- (length
5429                                        (buffer-name
5430                                         (if (eq where 'summary)
5431                                             nil
5432                                           (get-buffer gnus-article-buffer))))
5433                                       2)
5434                                  0))
5435           (setq max-len (max 4 (if gnus-mode-non-string-length
5436                                    (- (window-width)
5437                                       gnus-mode-non-string-length
5438                                       bufname-length)
5439                                  (length mode-string))))
5440           ;; We might have to chop a bit of the string off...
5441           (when (> (length mode-string) max-len)
5442             (setq mode-string
5443                   (concat (truncate-string-to-width mode-string (- max-len 3))
5444                           "...")))
5445           ;; Pad the mode string a bit.
5446           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5447       ;; Update the mode line.
5448       (setq mode-line-buffer-identification
5449             (gnus-mode-line-buffer-identification (list mode-string)))
5450       (set-buffer-modified-p t))))
5451
5452 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5453   "Go through the HEADERS list and add all Xrefs to a hash table.
5454 The resulting hash table is returned, or nil if no Xrefs were found."
5455   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5456          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5457          (xref-hashtb (gnus-make-hashtable))
5458          start group entry number xrefs header)
5459     (while headers
5460       (setq header (pop headers))
5461       (when (and (setq xrefs (mail-header-xref header))
5462                  (not (memq (setq number (mail-header-number header))
5463                             unreads)))
5464         (setq start 0)
5465         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5466           (setq start (match-end 0))
5467           (setq group (if prefix
5468                           (concat prefix (substring xrefs (match-beginning 1)
5469                                                     (match-end 1)))
5470                         (substring xrefs (match-beginning 1) (match-end 1))))
5471           (setq number
5472                 (string-to-int (substring xrefs (match-beginning 2)
5473                                           (match-end 2))))
5474           (if (setq entry (gnus-gethash group xref-hashtb))
5475               (setcdr entry (cons number (cdr entry)))
5476             (gnus-sethash group (cons number nil) xref-hashtb)))))
5477     (and start xref-hashtb)))
5478
5479 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5480   "Look through all the headers and mark the Xrefs as read."
5481   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5482         name entry info xref-hashtb idlist method nth4)
5483     (save-excursion
5484       (set-buffer gnus-group-buffer)
5485       (when (setq xref-hashtb
5486                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5487         (mapatoms
5488          (lambda (group)
5489            (unless (string= from-newsgroup (setq name (symbol-name group)))
5490              (setq idlist (symbol-value group))
5491              ;; Dead groups are not updated.
5492              (and (prog1
5493                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5494                             info (nth 2 entry))
5495                     (when (stringp (setq nth4 (gnus-info-method info)))
5496                       (setq nth4 (gnus-server-to-method nth4))))
5497                   ;; Only do the xrefs if the group has the same
5498                   ;; select method as the group we have just read.
5499                   (or (gnus-methods-equal-p
5500                        nth4 (gnus-find-method-for-group from-newsgroup))
5501                       virtual
5502                       (equal nth4 (setq method (gnus-find-method-for-group
5503                                                 from-newsgroup)))
5504                       (and (equal (car nth4) (car method))
5505                            (equal (nth 1 nth4) (nth 1 method))))
5506                   gnus-use-cross-reference
5507                   (or (not (eq gnus-use-cross-reference t))
5508                       virtual
5509                       ;; Only do cross-references on subscribed
5510                       ;; groups, if that is what is wanted.
5511                       (<= (gnus-info-level info) gnus-level-subscribed))
5512                   (gnus-group-make-articles-read name idlist))))
5513          xref-hashtb)))))
5514
5515 (defun gnus-compute-read-articles (group articles)
5516   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5517          (info (nth 2 entry))
5518          (active (gnus-active group))
5519          ninfo)
5520     (when entry
5521       ;; First peel off all invalid article numbers.
5522       (when active
5523         (let ((ids articles)
5524               id first)
5525           (while (setq id (pop ids))
5526             (when (and first (> id (cdr active)))
5527               ;; We'll end up in this situation in one particular
5528               ;; obscure situation.  If you re-scan a group and get
5529               ;; a new article that is cross-posted to a different
5530               ;; group that has not been re-scanned, you might get
5531               ;; crossposted article that has a higher number than
5532               ;; Gnus believes possible.  So we re-activate this
5533               ;; group as well.  This might mean doing the
5534               ;; crossposting thingy will *increase* the number
5535               ;; of articles in some groups.  Tsk, tsk.
5536               (setq active (or (gnus-activate-group group) active)))
5537             (when (or (> id (cdr active))
5538                       (< id (car active)))
5539               (setq articles (delq id articles))))))
5540       ;; If the read list is nil, we init it.
5541       (if (and active
5542                (null (gnus-info-read info))
5543                (> (car active) 1))
5544           (setq ninfo (cons 1 (1- (car active))))
5545         (setq ninfo (gnus-info-read info)))
5546       ;; Then we add the read articles to the range.
5547       (gnus-add-to-range
5548        ninfo (setq articles (sort articles '<))))))
5549
5550 (defun gnus-group-make-articles-read (group articles)
5551   "Update the info of GROUP to say that ARTICLES are read."
5552   (let* ((num 0)
5553          (entry (gnus-gethash group gnus-newsrc-hashtb))
5554          (info (nth 2 entry))
5555          (active (gnus-active group))
5556          range)
5557     (when entry
5558       (setq range (gnus-compute-read-articles group articles))
5559       (save-excursion
5560         (set-buffer gnus-group-buffer)
5561         (gnus-undo-register
5562           `(progn
5563              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5564              (gnus-info-set-read ',info ',(gnus-info-read info))
5565              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5566              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5567              (gnus-group-update-group ,group t))))
5568       ;; Add the read articles to the range.
5569       (gnus-info-set-read info range)
5570       (gnus-request-set-mark group (list (list range 'add '(read))))
5571       ;; Then we have to re-compute how many unread
5572       ;; articles there are in this group.
5573       (when active
5574         (cond
5575          ((not range)
5576           (setq num (- (1+ (cdr active)) (car active))))
5577          ((not (listp (cdr range)))
5578           (setq num (- (cdr active) (- (1+ (cdr range))
5579                                        (car range)))))
5580          (t
5581           (while range
5582             (if (numberp (car range))
5583                 (setq num (1+ num))
5584               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5585             (setq range (cdr range)))
5586           (setq num (- (cdr active) num))))
5587         ;; Update the number of unread articles.
5588         (setcar entry num)
5589         ;; Update the group buffer.
5590         (unless (gnus-ephemeral-group-p group)
5591           (gnus-group-update-group group t))))))
5592
5593 (defvar gnus-newsgroup-none-id 0)
5594
5595 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5596   (let ((cur nntp-server-buffer)
5597         (dependencies
5598          (or dependencies
5599              (save-excursion (set-buffer gnus-summary-buffer)
5600                              gnus-newsgroup-dependencies)))
5601         headers id end ref
5602         (mail-parse-charset gnus-newsgroup-charset)
5603         (mail-parse-ignored-charsets
5604          (save-excursion (condition-case nil
5605                              (set-buffer gnus-summary-buffer)
5606                            (error))
5607                          gnus-newsgroup-ignored-charsets)))
5608     (save-excursion
5609       (set-buffer nntp-server-buffer)
5610       ;; Translate all TAB characters into SPACE characters.
5611       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5612       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5613       (gnus-run-hooks 'gnus-parse-headers-hook)
5614       (let ((case-fold-search t)
5615             in-reply-to header p lines chars)
5616         (goto-char (point-min))
5617         ;; Search to the beginning of the next header.  Error messages
5618         ;; do not begin with 2 or 3.
5619         (while (re-search-forward "^[23][0-9]+ " nil t)
5620           (setq id nil
5621                 ref nil)
5622           ;; This implementation of this function, with nine
5623           ;; search-forwards instead of the one re-search-forward and
5624           ;; a case (which basically was the old function) is actually
5625           ;; about twice as fast, even though it looks messier.  You
5626           ;; can't have everything, I guess.  Speed and elegance
5627           ;; doesn't always go hand in hand.
5628           (setq
5629            header
5630            (vector
5631             ;; Number.
5632             (prog1
5633                 (read cur)
5634               (end-of-line)
5635               (setq p (point))
5636               (narrow-to-region (point)
5637                                 (or (and (search-forward "\n.\n" nil t)
5638                                          (- (point) 2))
5639                                     (point))))
5640             ;; Subject.
5641             (progn
5642               (goto-char p)
5643               (if (search-forward "\nsubject:" nil t)
5644                   (funcall gnus-decode-encoded-word-function
5645                            (nnheader-header-value))
5646                 "(none)"))
5647             ;; From.
5648             (progn
5649               (goto-char p)
5650               (if (search-forward "\nfrom:" nil t)
5651                   (funcall gnus-decode-encoded-word-function
5652                            (nnheader-header-value))
5653                 "(nobody)"))
5654             ;; Date.
5655             (progn
5656               (goto-char p)
5657               (if (search-forward "\ndate:" nil t)
5658                   (nnheader-header-value) ""))
5659             ;; Message-ID.
5660             (progn
5661               (goto-char p)
5662               (setq id (if (re-search-forward
5663                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5664                            ;; We do it this way to make sure the Message-ID
5665                            ;; is (somewhat) syntactically valid.
5666                            (buffer-substring (match-beginning 1)
5667                                              (match-end 1))
5668                          ;; If there was no message-id, we just fake one
5669                          ;; to make subsequent routines simpler.
5670                          (nnheader-generate-fake-message-id))))
5671             ;; References.
5672             (progn
5673               (goto-char p)
5674               (if (search-forward "\nreferences:" nil t)
5675                   (progn
5676                     (setq end (point))
5677                     (prog1
5678                         (nnheader-header-value)
5679                       (setq ref
5680                             (buffer-substring
5681                              (progn
5682                                (end-of-line)
5683                                (search-backward ">" end t)
5684                                (1+ (point)))
5685                              (progn
5686                                (search-backward "<" end t)
5687                                (point))))))
5688                 ;; Get the references from the in-reply-to header if there
5689                 ;; were no references and the in-reply-to header looks
5690                 ;; promising.
5691                 (if (and (search-forward "\nin-reply-to:" nil t)
5692                          (setq in-reply-to (nnheader-header-value))
5693                          (string-match "<[^>]+>" in-reply-to))
5694                     (let (ref2)
5695                       (setq ref (substring in-reply-to (match-beginning 0)
5696                                            (match-end 0)))
5697                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5698                         (setq ref2 (substring in-reply-to (match-beginning 0)
5699                                               (match-end 0)))
5700                         (when (> (length ref2) (length ref))
5701                           (setq ref ref2)))
5702                       ref)
5703                   (setq ref nil))))
5704             ;; Chars.
5705             (progn
5706               (goto-char p)
5707               (if (search-forward "\nchars: " nil t)
5708                   (if (numberp (setq chars (ignore-errors (read cur))))
5709                       chars -1)
5710                 -1))
5711             ;; Lines.
5712             (progn
5713               (goto-char p)
5714               (if (search-forward "\nlines: " nil t)
5715                   (if (numberp (setq lines (ignore-errors (read cur))))
5716                       lines -1)
5717                 -1))
5718             ;; Xref.
5719             (progn
5720               (goto-char p)
5721               (and (search-forward "\nxref:" nil t)
5722                    (nnheader-header-value)))
5723             ;; Extra.
5724             (when gnus-extra-headers
5725               (let ((extra gnus-extra-headers)
5726                     out)
5727                 (while extra
5728                   (goto-char p)
5729                   (when (search-forward
5730                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5731                     (push (cons (car extra) (nnheader-header-value))
5732                           out))
5733                   (pop extra))
5734                 out))))
5735           (when (equal id ref)
5736             (setq ref nil))
5737
5738           (when gnus-alter-header-function
5739             (funcall gnus-alter-header-function header)
5740             (setq id (mail-header-id header)
5741                   ref (gnus-parent-id (mail-header-references header))))
5742
5743           (when (setq header
5744                       (gnus-dependencies-add-header
5745                        header dependencies force-new))
5746             (push header headers))
5747           (goto-char (point-max))
5748           (widen))
5749         (nreverse headers)))))
5750
5751 ;; Goes through the xover lines and returns a list of vectors
5752 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5753                                                   force-new dependencies
5754                                                   group also-fetch-heads)
5755   "Parse the news overview data in the server buffer.
5756 Return a list of headers that match SEQUENCE (see
5757 `nntp-retrieve-headers')."
5758   ;; Get the Xref when the users reads the articles since most/some
5759   ;; NNTP servers do not include Xrefs when using XOVER.
5760   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5761   (let ((mail-parse-charset gnus-newsgroup-charset)
5762         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5763         (cur nntp-server-buffer)
5764         (dependencies (or dependencies gnus-newsgroup-dependencies))
5765         (allp (cond
5766                ((eq gnus-read-all-available-headers t)
5767                 t)
5768                ((stringp gnus-read-all-available-headers)
5769                 (string-match gnus-read-all-available-headers group))
5770                (t
5771                 nil)))
5772         number headers header)
5773     (save-excursion
5774       (set-buffer nntp-server-buffer)
5775       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5776       ;; Allow the user to mangle the headers before parsing them.
5777       (gnus-run-hooks 'gnus-parse-headers-hook)
5778       (goto-char (point-min))
5779       (gnus-parse-without-error
5780         (while (and (or sequence allp)
5781                     (not (eobp)))
5782           (setq number (read cur))
5783           (when (not allp)
5784             (while (and sequence
5785                         (< (car sequence) number))
5786               (setq sequence (cdr sequence))))
5787           (when (and (or allp
5788                          (and sequence
5789                               (eq number (car sequence))))
5790                      (progn
5791                        (setq sequence (cdr sequence))
5792                        (setq header (inline
5793                                       (gnus-nov-parse-line
5794                                        number dependencies force-new)))))
5795             (push header headers))
5796           (forward-line 1)))
5797       ;; A common bug in inn is that if you have posted an article and
5798       ;; then retrieves the active file, it will answer correctly --
5799       ;; the new article is included.  However, a NOV entry for the
5800       ;; article may not have been generated yet, so this may fail.
5801       ;; We work around this problem by retrieving the last few
5802       ;; headers using HEAD.
5803       (if (or (not also-fetch-heads)
5804               (not sequence))
5805           ;; We (probably) got all the headers.
5806           (nreverse headers)
5807         (let ((gnus-nov-is-evil t))
5808           (nconc
5809            (nreverse headers)
5810            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5811              (gnus-get-newsgroup-headers))))))))
5812
5813 (defun gnus-article-get-xrefs ()
5814   "Fill in the Xref value in `gnus-current-headers', if necessary.
5815 This is meant to be called in `gnus-article-internal-prepare-hook'."
5816   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5817                                  gnus-current-headers)))
5818     (or (not gnus-use-cross-reference)
5819         (not headers)
5820         (and (mail-header-xref headers)
5821              (not (string= (mail-header-xref headers) "")))
5822         (let ((case-fold-search t)
5823               xref)
5824           (save-restriction
5825             (nnheader-narrow-to-headers)
5826             (goto-char (point-min))
5827             (when (or (and (not (eobp))
5828                            (eq (downcase (char-after)) ?x)
5829                            (looking-at "Xref:"))
5830                       (search-forward "\nXref:" nil t))
5831               (goto-char (1+ (match-end 0)))
5832               (setq xref (buffer-substring (point)
5833                                            (progn (end-of-line) (point))))
5834               (mail-header-set-xref headers xref)))))))
5835
5836 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5837   "Find article ID and insert the summary line for that article.
5838 OLD-HEADER can either be a header or a line number to insert
5839 the subject line on."
5840   (let* ((line (and (numberp old-header) old-header))
5841          (old-header (and (vectorp old-header) old-header))
5842          (header (cond ((and old-header use-old-header)
5843                         old-header)
5844                        ((and (numberp id)
5845                              (gnus-number-to-header id))
5846                         (gnus-number-to-header id))
5847                        (t
5848                         (gnus-read-header id))))
5849          (number (and (numberp id) id))
5850          d)
5851     (when header
5852       ;; Rebuild the thread that this article is part of and go to the
5853       ;; article we have fetched.
5854       (when (and (not gnus-show-threads)
5855                  old-header)
5856         (when (and number
5857                    (setq d (gnus-data-find (mail-header-number old-header))))
5858           (goto-char (gnus-data-pos d))
5859           (gnus-data-remove
5860            number
5861            (- (gnus-point-at-bol)
5862               (prog1
5863                   (1+ (gnus-point-at-eol))
5864                 (gnus-delete-line))))))
5865       (when old-header
5866         (mail-header-set-number header (mail-header-number old-header)))
5867       (setq gnus-newsgroup-sparse
5868             (delq (setq number (mail-header-number header))
5869                   gnus-newsgroup-sparse))
5870       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5871       (push number gnus-newsgroup-limit)
5872       (gnus-rebuild-thread (mail-header-id header) line)
5873       (gnus-summary-goto-subject number nil t))
5874     (when (and (numberp number)
5875                (> number 0))
5876       ;; We have to update the boundaries even if we can't fetch the
5877       ;; article if ID is a number -- so that the next `P' or `N'
5878       ;; command will fetch the previous (or next) article even
5879       ;; if the one we tried to fetch this time has been canceled.
5880       (when (> number gnus-newsgroup-end)
5881         (setq gnus-newsgroup-end number))
5882       (when (< number gnus-newsgroup-begin)
5883         (setq gnus-newsgroup-begin number))
5884       (setq gnus-newsgroup-unselected
5885             (delq number gnus-newsgroup-unselected)))
5886     ;; Report back a success?
5887     (and header (mail-header-number header))))
5888
5889 ;;; Process/prefix in the summary buffer
5890
5891 (defun gnus-summary-work-articles (n)
5892   "Return a list of articles to be worked upon.
5893 The prefix argument, the list of process marked articles, and the
5894 current article will be taken into consideration."
5895   (save-excursion
5896     (set-buffer gnus-summary-buffer)
5897     (cond
5898      (n
5899       ;; A numerical prefix has been given.
5900       (setq n (prefix-numeric-value n))
5901       (let ((backward (< n 0))
5902             (n (abs (prefix-numeric-value n)))
5903             articles article)
5904         (save-excursion
5905           (while
5906               (and (> n 0)
5907                    (push (setq article (gnus-summary-article-number))
5908                          articles)
5909                    (if backward
5910                        (gnus-summary-find-prev nil article)
5911                      (gnus-summary-find-next nil article)))
5912             (decf n)))
5913         (nreverse articles)))
5914      ((and (gnus-region-active-p) (mark))
5915       (message "region active")
5916       ;; Work on the region between point and mark.
5917       (let ((max (max (point) (mark)))
5918             articles article)
5919         (save-excursion
5920           (goto-char (min (min (point) (mark))))
5921           (while
5922               (and
5923                (push (setq article (gnus-summary-article-number)) articles)
5924                (gnus-summary-find-next nil article)
5925                (< (point) max)))
5926           (nreverse articles))))
5927      (gnus-newsgroup-processable
5928       ;; There are process-marked articles present.
5929       ;; Save current state.
5930       (gnus-summary-save-process-mark)
5931       ;; Return the list.
5932       (reverse gnus-newsgroup-processable))
5933      (t
5934       ;; Just return the current article.
5935       (list (gnus-summary-article-number))))))
5936
5937 (defmacro gnus-summary-iterate (arg &rest forms)
5938   "Iterate over the process/prefixed articles and do FORMS.
5939 ARG is the interactive prefix given to the command.  FORMS will be
5940 executed with point over the summary line of the articles."
5941   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5942     `(let ((,articles (gnus-summary-work-articles ,arg)))
5943        (while ,articles
5944          (gnus-summary-goto-subject (car ,articles))
5945          ,@forms
5946          (pop ,articles)))))
5947
5948 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5949 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5950
5951 (defun gnus-summary-save-process-mark ()
5952   "Push the current set of process marked articles on the stack."
5953   (interactive)
5954   (push (copy-sequence gnus-newsgroup-processable)
5955         gnus-newsgroup-process-stack))
5956
5957 (defun gnus-summary-kill-process-mark ()
5958   "Push the current set of process marked articles on the stack and unmark."
5959   (interactive)
5960   (gnus-summary-save-process-mark)
5961   (gnus-summary-unmark-all-processable))
5962
5963 (defun gnus-summary-yank-process-mark ()
5964   "Pop the last process mark state off the stack and restore it."
5965   (interactive)
5966   (unless gnus-newsgroup-process-stack
5967     (error "Empty mark stack"))
5968   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5969
5970 (defun gnus-summary-process-mark-set (set)
5971   "Make SET into the current process marked articles."
5972   (gnus-summary-unmark-all-processable)
5973   (while set
5974     (gnus-summary-set-process-mark (pop set))))
5975
5976 ;;; Searching and stuff
5977
5978 (defun gnus-summary-search-group (&optional backward use-level)
5979   "Search for next unread newsgroup.
5980 If optional argument BACKWARD is non-nil, search backward instead."
5981   (save-excursion
5982     (set-buffer gnus-group-buffer)
5983     (when (gnus-group-search-forward
5984            backward nil (if use-level (gnus-group-group-level) nil))
5985       (gnus-group-group-name))))
5986
5987 (defun gnus-summary-best-group (&optional exclude-group)
5988   "Find the name of the best unread group.
5989 If EXCLUDE-GROUP, do not go to this group."
5990   (save-excursion
5991     (set-buffer gnus-group-buffer)
5992     (save-excursion
5993       (gnus-group-best-unread-group exclude-group))))
5994
5995 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5996   (if backward (gnus-summary-find-prev)
5997     (let* ((dummy (gnus-summary-article-intangible-p))
5998            (article (or article (gnus-summary-article-number)))
5999            (arts (gnus-data-find-list article))
6000            result)
6001       (when (and (not dummy)
6002                  (or (not gnus-summary-check-current)
6003                      (not unread)
6004                      (not (gnus-data-unread-p (car arts)))))
6005         (setq arts (cdr arts)))
6006       (when (setq result
6007                   (if unread
6008                       (progn
6009                         (while arts
6010                           (when (or (and undownloaded
6011                                          (memq (car arts) gnus-newsgroup-undownloaded))
6012                                     (gnus-data-unread-p (car arts)))
6013                             (setq result (car arts)
6014                                   arts nil))
6015                           (setq arts (cdr arts)))
6016                         result)
6017                     (car arts)))
6018         (goto-char (gnus-data-pos result))
6019         (gnus-data-number result)))))
6020
6021 (defun gnus-summary-find-prev (&optional unread article)
6022   (let* ((eobp (eobp))
6023          (article (or article (gnus-summary-article-number)))
6024          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
6025          result)
6026     (when (and (not eobp)
6027                (or (not gnus-summary-check-current)
6028                    (not unread)
6029                    (not (gnus-data-unread-p (car arts)))))
6030       (setq arts (cdr arts)))
6031     (when (setq result
6032                 (if unread
6033                     (progn
6034                       (while arts
6035                         (when (gnus-data-unread-p (car arts))
6036                           (setq result (car arts)
6037                                 arts nil))
6038                         (setq arts (cdr arts)))
6039                       result)
6040                   (car arts)))
6041       (goto-char (gnus-data-pos result))
6042       (gnus-data-number result))))
6043
6044 (defun gnus-summary-find-subject (subject &optional unread backward article)
6045   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6046          (article (or article (gnus-summary-article-number)))
6047          (articles (gnus-data-list backward))
6048          (arts (gnus-data-find-list article articles))
6049          result)
6050     (when (or (not gnus-summary-check-current)
6051               (not unread)
6052               (not (gnus-data-unread-p (car arts))))
6053       (setq arts (cdr arts)))
6054     (while arts
6055       (and (or (not unread)
6056                (gnus-data-unread-p (car arts)))
6057            (vectorp (gnus-data-header (car arts)))
6058            (gnus-subject-equal
6059             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6060            (setq result (car arts)
6061                  arts nil))
6062       (setq arts (cdr arts)))
6063     (and result
6064          (goto-char (gnus-data-pos result))
6065          (gnus-data-number result))))
6066
6067 (defun gnus-summary-search-forward (&optional unread subject backward)
6068   "Search forward for an article.
6069 If UNREAD, look for unread articles.  If SUBJECT, look for
6070 articles with that subject.  If BACKWARD, search backward instead."
6071   (cond (subject (gnus-summary-find-subject subject unread backward))
6072         (backward (gnus-summary-find-prev unread))
6073         (t (gnus-summary-find-next unread))))
6074
6075 (defun gnus-recenter (&optional n)
6076   "Center point in window and redisplay frame.
6077 Also do horizontal recentering."
6078   (interactive "P")
6079   (when (and gnus-auto-center-summary
6080              (not (eq gnus-auto-center-summary 'vertical)))
6081     (gnus-horizontal-recenter))
6082   (recenter n))
6083
6084 (defun gnus-summary-recenter ()
6085   "Center point in the summary window.
6086 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6087 displayed, no centering will be performed."
6088   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6089 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6090   (interactive)
6091   (let* ((top (cond ((< (window-height) 4) 0)
6092                     ((< (window-height) 7) 1)
6093                     (t (if (numberp gnus-auto-center-summary)
6094                            gnus-auto-center-summary
6095                          2))))
6096          (height (1- (window-height)))
6097          (bottom (save-excursion (goto-char (point-max))
6098                                  (forward-line (- height))
6099                                  (point)))
6100          (window (get-buffer-window (current-buffer))))
6101     ;; The user has to want it.
6102     (when gnus-auto-center-summary
6103       (when (get-buffer-window gnus-article-buffer)
6104         ;; Only do recentering when the article buffer is displayed,
6105       ;; Set the window start to either `bottom', which is the biggest
6106         ;; possible valid number, or the second line from the top,
6107         ;; whichever is the least.
6108         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6109           (if (> bottom top-pos)
6110               ;; Keep the second line from the top visible
6111               (set-window-start window top-pos t)
6112             ;; Try to keep the bottom line visible; if it's partially
6113             ;; obscured, either scroll one more line to make it fully
6114             ;; visible, or revert to using TOP-POS.
6115             (save-excursion
6116               (goto-char (point-max))
6117               (forward-line -1)
6118               (let ((last-line-start (point)))
6119                 (goto-char bottom)
6120                 (set-window-start window (point) t)
6121                 (when (not (pos-visible-in-window-p last-line-start window))
6122                   (forward-line 1)
6123                   (set-window-start window (min (point) top-pos) t)))))))
6124       ;; Do horizontal recentering while we're at it.
6125       (when (and (get-buffer-window (current-buffer) t)
6126                  (not (eq gnus-auto-center-summary 'vertical)))
6127         (let ((selected (selected-window)))
6128           (select-window (get-buffer-window (current-buffer) t))
6129           (gnus-summary-position-point)
6130           (gnus-horizontal-recenter)
6131           (select-window selected))))))
6132
6133 (defun gnus-summary-jump-to-group (newsgroup)
6134   "Move point to NEWSGROUP in group mode buffer."
6135   ;; Keep update point of group mode buffer if visible.
6136   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6137       (save-window-excursion
6138         ;; Take care of tree window mode.
6139         (when (get-buffer-window gnus-group-buffer)
6140           (pop-to-buffer gnus-group-buffer))
6141         (gnus-group-jump-to-group newsgroup))
6142     (save-excursion
6143       ;; Take care of tree window mode.
6144       (if (get-buffer-window gnus-group-buffer)
6145           (pop-to-buffer gnus-group-buffer)
6146         (set-buffer gnus-group-buffer))
6147       (gnus-group-jump-to-group newsgroup))))
6148
6149 ;; This function returns a list of article numbers based on the
6150 ;; difference between the ranges of read articles in this group and
6151 ;; the range of active articles.
6152 (defun gnus-list-of-unread-articles (group)
6153   (let* ((read (gnus-info-read (gnus-get-info group)))
6154          (active (or (gnus-active group) (gnus-activate-group group)))
6155          (last (cdr active))
6156          first nlast unread)
6157     ;; If none are read, then all are unread.
6158     (if (not read)
6159         (setq first (car active))
6160       ;; If the range of read articles is a single range, then the
6161       ;; first unread article is the article after the last read
6162       ;; article.  Sounds logical, doesn't it?
6163       (if (and (not (listp (cdr read)))
6164                (or (< (car read) (car active))
6165                    (progn (setq read (list read))
6166                           nil)))
6167           (setq first (max (car active) (1+ (cdr read))))
6168         ;; `read' is a list of ranges.
6169         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6170                                   (caar read)))
6171                   1)
6172           (setq first (car active)))
6173         (while read
6174           (when first
6175             (while (< first nlast)
6176               (push first unread)
6177               (setq first (1+ first))))
6178           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6179           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6180           (setq read (cdr read)))))
6181     ;; And add the last unread articles.
6182     (while (<= first last)
6183       (push first unread)
6184       (setq first (1+ first)))
6185     ;; Return the list of unread articles.
6186     (delq 0 (nreverse unread))))
6187
6188 (defun gnus-list-of-read-articles (group)
6189   "Return a list of unread, unticked and non-dormant articles."
6190   (let* ((info (gnus-get-info group))
6191          (marked (gnus-info-marks info))
6192          (active (gnus-active group)))
6193     (and info active
6194          (gnus-list-range-difference
6195           (gnus-list-range-difference
6196            (gnus-sorted-complement
6197             (gnus-uncompress-range active)
6198             (gnus-list-of-unread-articles group))
6199            (cdr (assq 'dormant marked)))
6200           (cdr (assq 'tick marked))))))
6201
6202 ;; Various summary commands
6203
6204 (defun gnus-summary-select-article-buffer ()
6205   "Reconfigure windows to show article buffer."
6206   (interactive)
6207   (if (not (gnus-buffer-live-p gnus-article-buffer))
6208       (error "There is no article buffer for this summary buffer")
6209     (gnus-configure-windows 'article)
6210     (select-window (get-buffer-window gnus-article-buffer))))
6211
6212 (defun gnus-summary-universal-argument (arg)
6213   "Perform any operation on all articles that are process/prefixed."
6214   (interactive "P")
6215   (let ((articles (gnus-summary-work-articles arg))
6216         func article)
6217     (if (eq
6218          (setq
6219           func
6220           (key-binding
6221            (read-key-sequence
6222             (substitute-command-keys
6223              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6224          'undefined)
6225         (gnus-error 1 "Undefined key")
6226       (save-excursion
6227         (while articles
6228           (gnus-summary-goto-subject (setq article (pop articles)))
6229           (let (gnus-newsgroup-processable)
6230             (command-execute func))
6231           (gnus-summary-remove-process-mark article)))))
6232   (gnus-summary-position-point))
6233
6234 (defun gnus-summary-toggle-truncation (&optional arg)
6235   "Toggle truncation of summary lines.
6236 With arg, turn line truncation on if arg is positive."
6237   (interactive "P")
6238   (setq truncate-lines
6239         (if (null arg) (not truncate-lines)
6240           (> (prefix-numeric-value arg) 0)))
6241   (redraw-display))
6242
6243 (defun gnus-summary-find-uncancelled ()
6244   "Return the number of an uncancelled article.
6245 The current article is considered, then following articles, then previous
6246 articles.  If all articles are cancelled then return a dummy 0."
6247   (let (found)
6248     (dolist (rev '(nil t))
6249       (unless found      ; don't demand the reverse list if we don't need it
6250         (let ((data (gnus-data-find-list
6251                      (gnus-summary-article-number) (gnus-data-list rev))))
6252           (while (and data (not found))
6253             (if (not (eq gnus-canceled-mark (gnus-data-mark (car data))))
6254                 (setq found (gnus-data-number (car data))))
6255             (setq data (cdr data))))))
6256     (or found 0)))
6257
6258 (defun gnus-summary-reselect-current-group (&optional all rescan)
6259   "Exit and then reselect the current newsgroup.
6260 The prefix argument ALL means to select all articles."
6261   (interactive "P")
6262   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6263     (error "Ephemeral groups can't be reselected"))
6264   (let ((current-subject (gnus-summary-find-uncancelled))
6265         (group gnus-newsgroup-name))
6266     (setq gnus-newsgroup-begin nil)
6267     (gnus-summary-exit)
6268     ;; We have to adjust the point of group mode buffer because
6269     ;; point was moved to the next unread newsgroup by exiting.
6270     (gnus-summary-jump-to-group group)
6271     (when rescan
6272       (save-excursion
6273         (gnus-group-get-new-news-this-group 1)))
6274     (gnus-group-read-group all t)
6275     (gnus-summary-goto-subject current-subject nil t)))
6276
6277 (defun gnus-summary-rescan-group (&optional all)
6278   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6279   (interactive "P")
6280   (gnus-summary-reselect-current-group all t))
6281
6282 (defun gnus-summary-update-info (&optional non-destructive)
6283   (save-excursion
6284     (let ((group gnus-newsgroup-name))
6285       (when group
6286         (when gnus-newsgroup-kill-headers
6287           (setq gnus-newsgroup-killed
6288                 (gnus-compress-sequence
6289                  (gnus-sorted-union
6290                   (gnus-list-range-intersection
6291                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6292                   gnus-newsgroup-unreads)
6293                  t)))
6294         (unless (listp (cdr gnus-newsgroup-killed))
6295           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6296         (let ((headers gnus-newsgroup-headers))
6297           ;; Set the new ranges of read articles.
6298           (save-excursion
6299             (set-buffer gnus-group-buffer)
6300             (gnus-undo-force-boundary))
6301           (gnus-update-read-articles
6302            group (gnus-sorted-union
6303                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6304           ;; Set the current article marks.
6305           (let ((gnus-newsgroup-scored
6306                  (if (and (not gnus-save-score)
6307                           (not non-destructive))
6308                      nil
6309                    gnus-newsgroup-scored)))
6310             (save-excursion
6311               (gnus-update-marks)))
6312           ;; Do the cross-ref thing.
6313           (when gnus-use-cross-reference
6314             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6315           ;; Do not switch windows but change the buffer to work.
6316           (set-buffer gnus-group-buffer)
6317           (unless (gnus-ephemeral-group-p group)
6318             (gnus-group-update-group group)))))))
6319
6320 (defun gnus-summary-save-newsrc (&optional force)
6321   "Save the current number of read/marked articles in the dribble buffer.
6322 The dribble buffer will then be saved.
6323 If FORCE (the prefix), also save the .newsrc file(s)."
6324   (interactive "P")
6325   (gnus-summary-update-info t)
6326   (if force
6327       (gnus-save-newsrc-file)
6328     (gnus-dribble-save)))
6329
6330 (defun gnus-summary-exit (&optional temporary)
6331   "Exit reading current newsgroup, and then return to group selection mode.
6332 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6333   (interactive)
6334   (gnus-set-global-variables)
6335   (when (gnus-buffer-live-p gnus-article-buffer)
6336     (save-excursion
6337       (set-buffer gnus-article-buffer)
6338       (mm-destroy-parts gnus-article-mime-handles)
6339       ;; Set it to nil for safety reason.
6340       (setq gnus-article-mime-handle-alist nil)
6341       (setq gnus-article-mime-handles nil)))
6342   (gnus-kill-save-kill-buffer)
6343   (gnus-async-halt-prefetch)
6344   (let* ((group gnus-newsgroup-name)
6345          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6346          (gnus-group-is-exiting-p t)
6347          (mode major-mode)
6348          (group-point nil)
6349          (buf (current-buffer)))
6350     (unless quit-config
6351       ;; Do adaptive scoring, and possibly save score files.
6352       (when gnus-newsgroup-adaptive
6353         (gnus-score-adaptive))
6354       (when gnus-use-scoring
6355         (gnus-score-save)))
6356     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6357     ;; If we have several article buffers, we kill them at exit.
6358     (unless gnus-single-article-buffer
6359       (gnus-kill-buffer gnus-original-article-buffer)
6360       (setq gnus-article-current nil))
6361     (when gnus-use-cache
6362       (gnus-cache-possibly-remove-articles)
6363       (gnus-cache-save-buffers))
6364     (gnus-async-prefetch-remove-group group)
6365     (when gnus-suppress-duplicates
6366       (gnus-dup-enter-articles))
6367     (when gnus-use-trees
6368       (gnus-tree-close group))
6369     (when gnus-use-cache
6370       (gnus-cache-write-active))
6371     ;; Remove entries for this group.
6372     (nnmail-purge-split-history (gnus-group-real-name group))
6373     ;; Make all changes in this group permanent.
6374     (unless quit-config
6375       (gnus-run-hooks 'gnus-exit-group-hook)
6376       (gnus-summary-update-info))
6377     (gnus-close-group group)
6378     ;; Make sure where we were, and go to next newsgroup.
6379     (set-buffer gnus-group-buffer)
6380     (unless quit-config
6381       (gnus-group-jump-to-group group))
6382     (gnus-run-hooks 'gnus-summary-exit-hook)
6383     (unless (or quit-config
6384                 ;; If this group has disappeared from the summary
6385                 ;; buffer, don't skip forwards.
6386                 (not (string= group (gnus-group-group-name))))
6387       (gnus-group-next-unread-group 1))
6388     (setq group-point (point))
6389     (if temporary
6390         nil                             ;Nothing to do.
6391       ;; If we have several article buffers, we kill them at exit.
6392       (unless gnus-single-article-buffer
6393         (gnus-kill-buffer gnus-article-buffer)
6394         (gnus-kill-buffer gnus-original-article-buffer)
6395         (setq gnus-article-current nil))
6396       (set-buffer buf)
6397       (if (not gnus-kill-summary-on-exit)
6398           (progn
6399             (gnus-deaden-summary)
6400             (setq mode nil))
6401         ;; We set all buffer-local variables to nil.  It is unclear why
6402         ;; this is needed, but if we don't, buffer-local variables are
6403         ;; not garbage-collected, it seems.  This would the lead to en
6404         ;; ever-growing Emacs.
6405         (gnus-summary-clear-local-variables)
6406         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6407           (gnus-summary-clear-local-variables))
6408         (when (get-buffer gnus-article-buffer)
6409           (bury-buffer gnus-article-buffer))
6410         ;; We clear the global counterparts of the buffer-local
6411         ;; variables as well, just to be on the safe side.
6412         (set-buffer gnus-group-buffer)
6413         (gnus-summary-clear-local-variables)
6414         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6415           (gnus-summary-clear-local-variables)))
6416       (setq gnus-current-select-method gnus-select-method)
6417       (pop-to-buffer gnus-group-buffer)
6418       (if (not quit-config)
6419           (progn
6420             (goto-char group-point)
6421             (gnus-configure-windows 'group 'force))
6422         (gnus-handle-ephemeral-exit quit-config))
6423       ;; Return to group mode buffer.
6424       (when (eq mode 'gnus-summary-mode)
6425         (gnus-kill-buffer buf))
6426       ;; Clear the current group name.
6427       (unless quit-config
6428         (setq gnus-newsgroup-name nil)))))
6429
6430 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6431 (defun gnus-summary-exit-no-update (&optional no-questions)
6432   "Quit reading current newsgroup without updating read article info."
6433   (interactive)
6434   (let* ((group gnus-newsgroup-name)
6435          (gnus-group-is-exiting-p t)
6436          (quit-config (gnus-group-quit-config group)))
6437     (when (or no-questions
6438               gnus-expert-user
6439               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6440       (gnus-async-halt-prefetch)
6441       (mapcar 'funcall
6442               (delq 'gnus-summary-expire-articles
6443                     (copy-sequence gnus-summary-prepare-exit-hook)))
6444       (when (gnus-buffer-live-p gnus-article-buffer)
6445         (save-excursion
6446           (set-buffer gnus-article-buffer)
6447           (mm-destroy-parts gnus-article-mime-handles)
6448           ;; Set it to nil for safety reason.
6449           (setq gnus-article-mime-handle-alist nil)
6450           (setq gnus-article-mime-handles nil)))
6451       ;; If we have several article buffers, we kill them at exit.
6452       (unless gnus-single-article-buffer
6453         (gnus-kill-buffer gnus-article-buffer)
6454         (gnus-kill-buffer gnus-original-article-buffer)
6455         (setq gnus-article-current nil))
6456       (if (not gnus-kill-summary-on-exit)
6457           (gnus-deaden-summary)
6458         (gnus-close-group group)
6459         (gnus-summary-clear-local-variables)
6460         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6461           (gnus-summary-clear-local-variables))
6462         (set-buffer gnus-group-buffer)
6463         (gnus-summary-clear-local-variables)
6464         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6465           (gnus-summary-clear-local-variables))
6466         (when (get-buffer gnus-summary-buffer)
6467           (kill-buffer gnus-summary-buffer)))
6468       (unless gnus-single-article-buffer
6469         (setq gnus-article-current nil))
6470       (when gnus-use-trees
6471         (gnus-tree-close group))
6472       (gnus-async-prefetch-remove-group group)
6473       (when (get-buffer gnus-article-buffer)
6474         (bury-buffer gnus-article-buffer))
6475       ;; Return to the group buffer.
6476       (gnus-configure-windows 'group 'force)
6477       ;; Clear the current group name.
6478       (setq gnus-newsgroup-name nil)
6479       (unless (gnus-ephemeral-group-p group)
6480         (gnus-group-update-group group))
6481       (when (equal (gnus-group-group-name) group)
6482         (gnus-group-next-unread-group 1))
6483       (when quit-config
6484         (gnus-handle-ephemeral-exit quit-config)))))
6485
6486 (defun gnus-handle-ephemeral-exit (quit-config)
6487   "Handle movement when leaving an ephemeral group.
6488 The state which existed when entering the ephemeral is reset."
6489   (if (not (buffer-name (car quit-config)))
6490       (gnus-configure-windows 'group 'force)
6491     (set-buffer (car quit-config))
6492     (cond ((eq major-mode 'gnus-summary-mode)
6493            (gnus-set-global-variables))
6494           ((eq major-mode 'gnus-article-mode)
6495            (save-excursion
6496              ;; The `gnus-summary-buffer' variable may point
6497              ;; to the old summary buffer when using a single
6498              ;; article buffer.
6499              (unless (gnus-buffer-live-p gnus-summary-buffer)
6500                (set-buffer gnus-group-buffer))
6501              (set-buffer gnus-summary-buffer)
6502              (gnus-set-global-variables))))
6503     (if (or (eq (cdr quit-config) 'article)
6504             (eq (cdr quit-config) 'pick))
6505         (progn
6506           ;; The current article may be from the ephemeral group
6507           ;; thus it is best that we reload this article
6508           (gnus-summary-show-article)
6509           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6510               (gnus-configure-windows 'pick 'force)
6511             (gnus-configure-windows (cdr quit-config) 'force)))
6512       (gnus-configure-windows (cdr quit-config) 'force))
6513     (when (eq major-mode 'gnus-summary-mode)
6514       (gnus-summary-next-subject 1 nil t)
6515       (gnus-summary-recenter)
6516       (gnus-summary-position-point))))
6517
6518 ;;; Dead summaries.
6519
6520 (defvar gnus-dead-summary-mode-map nil)
6521
6522 (unless gnus-dead-summary-mode-map
6523   (setq gnus-dead-summary-mode-map (make-keymap))
6524   (suppress-keymap gnus-dead-summary-mode-map)
6525   (substitute-key-definition
6526    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6527   (dolist (key '("\C-d" "\r" "\177" [delete]))
6528     (define-key gnus-dead-summary-mode-map
6529       key 'gnus-summary-wake-up-the-dead))
6530   (dolist (key '("q" "Q"))
6531     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6532
6533 (defvar gnus-dead-summary-mode nil
6534   "Minor mode for Gnus summary buffers.")
6535
6536 (defun gnus-dead-summary-mode (&optional arg)
6537   "Minor mode for Gnus summary buffers."
6538   (interactive "P")
6539   (when (eq major-mode 'gnus-summary-mode)
6540     (make-local-variable 'gnus-dead-summary-mode)
6541     (setq gnus-dead-summary-mode
6542           (if (null arg) (not gnus-dead-summary-mode)
6543             (> (prefix-numeric-value arg) 0)))
6544     (when gnus-dead-summary-mode
6545       (gnus-add-minor-mode
6546        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6547
6548 (defun gnus-deaden-summary ()
6549   "Make the current summary buffer into a dead summary buffer."
6550   ;; Kill any previous dead summary buffer.
6551   (when (and gnus-dead-summary
6552              (buffer-name gnus-dead-summary))
6553     (save-excursion
6554       (set-buffer gnus-dead-summary)
6555       (when gnus-dead-summary-mode
6556         (kill-buffer (current-buffer)))))
6557   ;; Make this the current dead summary.
6558   (setq gnus-dead-summary (current-buffer))
6559   (gnus-dead-summary-mode 1)
6560   (let ((name (buffer-name)))
6561     (when (string-match "Summary" name)
6562       (rename-buffer
6563        (concat (substring name 0 (match-beginning 0)) "Dead "
6564                (substring name (match-beginning 0)))
6565        t)
6566       (bury-buffer))))
6567
6568 (defun gnus-kill-or-deaden-summary (buffer)
6569   "Kill or deaden the summary BUFFER."
6570   (save-excursion
6571     (when (and (buffer-name buffer)
6572                (not gnus-single-article-buffer))
6573       (save-excursion
6574         (set-buffer buffer)
6575         (gnus-kill-buffer gnus-article-buffer)
6576         (gnus-kill-buffer gnus-original-article-buffer)))
6577     (cond
6578      ;; Kill the buffer.
6579      (gnus-kill-summary-on-exit
6580       (when (and gnus-use-trees
6581                  (gnus-buffer-exists-p buffer))
6582         (save-excursion
6583           (set-buffer buffer)
6584           (gnus-tree-close gnus-newsgroup-name)))
6585       (gnus-kill-buffer buffer))
6586      ;; Deaden the buffer.
6587      ((gnus-buffer-exists-p buffer)
6588       (save-excursion
6589         (set-buffer buffer)
6590         (gnus-deaden-summary))))))
6591
6592 (defun gnus-summary-wake-up-the-dead (&rest args)
6593   "Wake up the dead summary buffer."
6594   (interactive)
6595   (gnus-dead-summary-mode -1)
6596   (let ((name (buffer-name)))
6597     (when (string-match "Dead " name)
6598       (rename-buffer
6599        (concat (substring name 0 (match-beginning 0))
6600                (substring name (match-end 0)))
6601        t)))
6602   (gnus-message 3 "This dead summary is now alive again"))
6603
6604 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6605 (defun gnus-summary-fetch-faq (&optional faq-dir)
6606   "Fetch the FAQ for the current group.
6607 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6608 in."
6609   (interactive
6610    (list
6611     (when current-prefix-arg
6612       (completing-read
6613        "FAQ dir: " (and (listp gnus-group-faq-directory)
6614                         (mapcar (lambda (file) (list file))
6615                                 gnus-group-faq-directory))))))
6616   (let (gnus-faq-buffer)
6617     (when (setq gnus-faq-buffer
6618                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6619       (gnus-configure-windows 'summary-faq))))
6620
6621 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6622 (defun gnus-summary-describe-group (&optional force)
6623   "Describe the current newsgroup."
6624   (interactive "P")
6625   (gnus-group-describe-group force gnus-newsgroup-name))
6626
6627 (defun gnus-summary-describe-briefly ()
6628   "Describe summary mode commands briefly."
6629   (interactive)
6630   (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")))
6631
6632 ;; Walking around group mode buffer from summary mode.
6633
6634 (defun gnus-summary-next-group (&optional no-article target-group backward)
6635   "Exit current newsgroup and then select next unread newsgroup.
6636 If prefix argument NO-ARTICLE is non-nil, no article is selected
6637 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6638 previous group instead."
6639   (interactive "P")
6640   ;; Stop pre-fetching.
6641   (gnus-async-halt-prefetch)
6642   (let ((current-group gnus-newsgroup-name)
6643         (current-buffer (current-buffer))
6644         entered)
6645     (while (not entered)
6646       ;; Then we find what group we are supposed to enter.
6647       (set-buffer gnus-group-buffer)
6648       (gnus-group-jump-to-group current-group)
6649       (setq target-group
6650             (or target-group
6651                 (if (eq gnus-keep-same-level 'best)
6652                     (gnus-summary-best-group gnus-newsgroup-name)
6653                   (gnus-summary-search-group backward gnus-keep-same-level))))
6654       (if (not target-group)
6655           ;; There are no further groups, so we return to the group
6656           ;; buffer.
6657           (progn
6658             (gnus-message 5 "Returning to the group buffer")
6659             (setq entered t)
6660             (when (gnus-buffer-live-p current-buffer)
6661               (set-buffer current-buffer)
6662               (gnus-summary-exit))
6663             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6664         ;; We try to enter the target group.
6665         (gnus-group-jump-to-group target-group)
6666         (let ((unreads (gnus-group-group-unread)))
6667           (if (and (or (eq t unreads)
6668                        (and unreads (not (zerop unreads))))
6669                    (progn
6670                      ;; Now we semi-exit this group to update Xrefs
6671                      ;; and all variables.  We can't do a real exit,
6672                      ;; because the window conf must remain the same
6673                      ;; in case the user is prompted for info, and we
6674                      ;; don't want the window conf to change before
6675                      ;; that...
6676                      (when (gnus-buffer-live-p current-buffer)
6677                        (set-buffer current-buffer)
6678                        (gnus-summary-exit t))
6679                      (gnus-summary-read-group
6680                       target-group nil no-article
6681                       (and (buffer-name current-buffer) current-buffer)
6682                       nil backward)))
6683               (setq entered t)
6684             (setq current-group target-group
6685                   target-group nil)))))))
6686
6687 (defun gnus-summary-prev-group (&optional no-article)
6688   "Exit current newsgroup and then select previous unread newsgroup.
6689 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6690   (interactive "P")
6691   (gnus-summary-next-group no-article nil t))
6692
6693 ;; Walking around summary lines.
6694
6695 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6696   "Go to the first unread subject.
6697 If UNREAD is non-nil, go to the first unread article.
6698 Returns the article selected or nil if there are no unread articles."
6699   (interactive "P")
6700   (prog1
6701       (cond
6702        ;; Empty summary.
6703        ((null gnus-newsgroup-data)
6704         (gnus-message 3 "No articles in the group")
6705         nil)
6706        ;; Pick the first article.
6707        ((not unread)
6708         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6709         (gnus-data-number (car gnus-newsgroup-data)))
6710        ;; No unread articles.
6711        ((null gnus-newsgroup-unreads)
6712         (gnus-message 3 "No more unread articles")
6713         nil)
6714        ;; Find the first unread article.
6715        (t
6716         (let ((data gnus-newsgroup-data))
6717           (while (and data
6718                       (and (not (and undownloaded
6719                                      (memq (car data)
6720                                            gnus-newsgroup-undownloaded)))
6721                            (if unseen
6722                                (or (not (memq
6723                                          (gnus-data-number (car data))
6724                                          gnus-newsgroup-unseen))
6725                                    (not (gnus-data-unread-p (car data))))
6726                              (not (gnus-data-unread-p (car data))))))
6727             (setq data (cdr data)))
6728           (when data
6729             (goto-char (gnus-data-pos (car data)))
6730             (gnus-data-number (car data))))))
6731     (gnus-summary-position-point)))
6732
6733 (defun gnus-summary-next-subject (n &optional unread dont-display)
6734   "Go to next N'th summary line.
6735 If N is negative, go to the previous N'th subject line.
6736 If UNREAD is non-nil, only unread articles are selected.
6737 The difference between N and the actual number of steps taken is
6738 returned."
6739   (interactive "p")
6740   (let ((backward (< n 0))
6741         (n (abs n)))
6742     (while (and (> n 0)
6743                 (if backward
6744                     (gnus-summary-find-prev unread)
6745                   (gnus-summary-find-next unread)))
6746       (unless (zerop (setq n (1- n)))
6747         (gnus-summary-show-thread)))
6748     (when (/= 0 n)
6749       (gnus-message 7 "No more%s articles"
6750                     (if unread " unread" "")))
6751     (unless dont-display
6752       (gnus-summary-recenter)
6753       (gnus-summary-position-point))
6754     n))
6755
6756 (defun gnus-summary-next-unread-subject (n)
6757   "Go to next N'th unread summary line."
6758   (interactive "p")
6759   (gnus-summary-next-subject n t))
6760
6761 (defun gnus-summary-prev-subject (n &optional unread)
6762   "Go to previous N'th summary line.
6763 If optional argument UNREAD is non-nil, only unread article is selected."
6764   (interactive "p")
6765   (gnus-summary-next-subject (- n) unread))
6766
6767 (defun gnus-summary-prev-unread-subject (n)
6768   "Go to previous N'th unread summary line."
6769   (interactive "p")
6770   (gnus-summary-next-subject (- n) t))
6771
6772 (defun gnus-summary-goto-subjects (articles)
6773   "Insert the subject header for ARTICLES in the current buffer."
6774   (save-excursion
6775     (dolist (article articles)
6776       (gnus-summary-goto-subject article t)))
6777   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6778   (gnus-summary-position-point))
6779   
6780 (defun gnus-summary-goto-subject (article &optional force silent)
6781   "Go the subject line of ARTICLE.
6782 If FORCE, also allow jumping to articles not currently shown."
6783   (interactive "nArticle number: ")
6784   (unless (numberp article)
6785     (error "Article %s is not a number" article))
6786   (let ((b (point))
6787         (data (gnus-data-find article)))
6788     ;; We read in the article if we have to.
6789     (and (not data)
6790          force
6791          (gnus-summary-insert-subject
6792           article
6793           (if (or (numberp force) (vectorp force)) force)
6794           t)
6795          (setq data (gnus-data-find article)))
6796     (goto-char b)
6797     (if (not data)
6798         (progn
6799           (unless silent
6800             (gnus-message 3 "Can't find article %d" article))
6801           nil)
6802       (let ((pt (gnus-data-pos data)))
6803         (goto-char pt)
6804         (gnus-summary-set-article-display-arrow pt))
6805       (gnus-summary-position-point)
6806       article)))
6807
6808 ;; Walking around summary lines with displaying articles.
6809
6810 (defun gnus-summary-expand-window (&optional arg)
6811   "Make the summary buffer take up the entire Emacs frame.
6812 Given a prefix, will force an `article' buffer configuration."
6813   (interactive "P")
6814   (if arg
6815       (gnus-configure-windows 'article 'force)
6816     (gnus-configure-windows 'summary 'force)))
6817
6818 (defun gnus-summary-display-article (article &optional all-header)
6819   "Display ARTICLE in article buffer."
6820   (when (gnus-buffer-live-p gnus-article-buffer)
6821     (with-current-buffer gnus-article-buffer
6822       (mm-enable-multibyte)))
6823   (gnus-set-global-variables)
6824   (when (gnus-buffer-live-p gnus-article-buffer)
6825     (with-current-buffer gnus-article-buffer
6826       (setq gnus-article-charset gnus-newsgroup-charset)
6827       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6828       (mm-enable-multibyte)))
6829   (if (null article)
6830       nil
6831     (prog1
6832         (if gnus-summary-display-article-function
6833             (funcall gnus-summary-display-article-function article all-header)
6834           (gnus-article-prepare article all-header))
6835       (gnus-run-hooks 'gnus-select-article-hook)
6836       (when (and gnus-current-article
6837                  (not (zerop gnus-current-article)))
6838         (gnus-summary-goto-subject gnus-current-article))
6839       (gnus-summary-recenter)
6840       (when (and gnus-use-trees gnus-show-threads)
6841         (gnus-possibly-generate-tree article)
6842         (gnus-highlight-selected-tree article))
6843       ;; Successfully display article.
6844       (gnus-article-set-window-start
6845        (cdr (assq article gnus-newsgroup-bookmarks))))))
6846
6847 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6848   "Select the current article.
6849 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6850 non-nil, the article will be re-fetched even if it already present in
6851 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6852 be displayed."
6853   ;; Make sure we are in the summary buffer to work around bbdb bug.
6854   (unless (eq major-mode 'gnus-summary-mode)
6855     (set-buffer gnus-summary-buffer))
6856   (let ((article (or article (gnus-summary-article-number)))
6857         (all-headers (not (not all-headers))) ;Must be t or nil.
6858         gnus-summary-display-article-function)
6859     (and (not pseudo)
6860          (gnus-summary-article-pseudo-p article)
6861          (error "This is a pseudo-article"))
6862     (save-excursion
6863       (set-buffer gnus-summary-buffer)
6864       (if (or (and gnus-single-article-buffer
6865                    (or (null gnus-current-article)
6866                        (null gnus-article-current)
6867                        (null (get-buffer gnus-article-buffer))
6868                        (not (eq article (cdr gnus-article-current)))
6869                        (not (equal (car gnus-article-current)
6870                                    gnus-newsgroup-name))))
6871               (and (not gnus-single-article-buffer)
6872                    (or (null gnus-current-article)
6873                        (not (eq gnus-current-article article))))
6874               force)
6875           ;; The requested article is different from the current article.
6876           (progn
6877             (gnus-summary-display-article article all-headers)
6878             (when (gnus-buffer-live-p gnus-article-buffer)
6879               (with-current-buffer gnus-article-buffer
6880                 (if (not gnus-article-decoded-p) ;; a local variable
6881                     (mm-disable-multibyte))))
6882             (gnus-article-set-window-start
6883              (cdr (assq article gnus-newsgroup-bookmarks)))
6884             article)
6885         'old))))
6886
6887 (defun gnus-summary-force-verify-and-decrypt ()
6888   (interactive)
6889   (let ((mm-verify-option 'known)
6890         (mm-decrypt-option 'known)
6891         (gnus-buttonized-mime-types (append (list "multipart/signed"
6892                                                   "multipart/encrypted")
6893                                             gnus-buttonized-mime-types)))
6894     (gnus-summary-select-article nil 'force)))
6895
6896 (defun gnus-summary-set-current-mark (&optional current-mark)
6897   "Obsolete function."
6898   nil)
6899
6900 (defun gnus-summary-next-article (&optional unread subject backward push)
6901   "Select the next article.
6902 If UNREAD, only unread articles are selected.
6903 If SUBJECT, only articles with SUBJECT are selected.
6904 If BACKWARD, the previous article is selected instead of the next."
6905   (interactive "P")
6906   (cond
6907    ;; Is there such an article?
6908    ((and (gnus-summary-search-forward unread subject backward)
6909          (or (gnus-summary-display-article (gnus-summary-article-number))
6910              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6911     (gnus-summary-position-point))
6912    ;; If not, we try the first unread, if that is wanted.
6913    ((and subject
6914          gnus-auto-select-same
6915          (gnus-summary-first-unread-article))
6916     (gnus-summary-position-point)
6917     (gnus-message 6 "Wrapped"))
6918    ;; Try to get next/previous article not displayed in this group.
6919    ((and gnus-auto-extend-newsgroup
6920          (not unread) (not subject))
6921     (gnus-summary-goto-article
6922      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6923      nil (count-lines (point-min) (point))))
6924    ;; Go to next/previous group.
6925    (t
6926     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6927       (gnus-summary-jump-to-group gnus-newsgroup-name))
6928     (let ((cmd last-command-char)
6929           (point
6930            (save-excursion
6931              (set-buffer gnus-group-buffer)
6932              (point)))
6933           (group
6934            (if (eq gnus-keep-same-level 'best)
6935                (gnus-summary-best-group gnus-newsgroup-name)
6936              (gnus-summary-search-group backward gnus-keep-same-level))))
6937       ;; For some reason, the group window gets selected.  We change
6938       ;; it back.
6939       (select-window (get-buffer-window (current-buffer)))
6940       ;; Select next unread newsgroup automagically.
6941       (cond
6942        ((or (not gnus-auto-select-next)
6943             (not cmd))
6944         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6945        ((or (eq gnus-auto-select-next 'quietly)
6946             (and (eq gnus-auto-select-next 'slightly-quietly)
6947                  push)
6948             (and (eq gnus-auto-select-next 'almost-quietly)
6949                  (gnus-summary-last-article-p)))
6950         ;; Select quietly.
6951         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6952             (gnus-summary-exit)
6953           (gnus-message 7 "No more%s articles (%s)..."
6954                         (if unread " unread" "")
6955                         (if group (concat "selecting " group)
6956                           "exiting"))
6957           (gnus-summary-next-group nil group backward)))
6958        (t
6959         (when (gnus-key-press-event-p last-input-event)
6960           (gnus-summary-walk-group-buffer
6961            gnus-newsgroup-name cmd unread backward point))))))))
6962
6963 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6964   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6965                       (?\C-p (gnus-group-prev-unread-group 1))))
6966         (cursor-in-echo-area t)
6967         keve key group ended prompt)
6968     (save-excursion
6969       (set-buffer gnus-group-buffer)
6970       (goto-char start)
6971       (setq group
6972             (if (eq gnus-keep-same-level 'best)
6973                 (gnus-summary-best-group gnus-newsgroup-name)
6974               (gnus-summary-search-group backward gnus-keep-same-level))))
6975     (while (not ended)
6976       (setq prompt
6977             (format
6978              "No more%s articles%s " (if unread " unread" "")
6979              (if (and group
6980                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6981                  (format " (Type %s for %s [%s])"
6982                          (single-key-description cmd) group
6983                          (car (gnus-gethash group gnus-newsrc-hashtb)))
6984                (format " (Type %s to exit %s)"
6985                        (single-key-description cmd)
6986                        gnus-newsgroup-name))))
6987       ;; Confirm auto selection.
6988       (setq key (car (setq keve (gnus-read-event-char prompt)))
6989             ended t)
6990       (cond
6991        ((assq key keystrokes)
6992         (let ((obuf (current-buffer)))
6993           (switch-to-buffer gnus-group-buffer)
6994           (when group
6995             (gnus-group-jump-to-group group))
6996           (eval (cadr (assq key keystrokes)))
6997           (setq group (gnus-group-group-name))
6998           (switch-to-buffer obuf))
6999         (setq ended nil))
7000        ((equal key cmd)
7001         (if (or (not group)
7002                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7003             (gnus-summary-exit)
7004           (gnus-summary-next-group nil group backward)))
7005        (t
7006         (push (cdr keve) unread-command-events))))))
7007
7008 (defun gnus-summary-next-unread-article ()
7009   "Select unread article after current one."
7010   (interactive)
7011   (gnus-summary-next-article
7012    (or (not (eq gnus-summary-goto-unread 'never))
7013        (gnus-summary-last-article-p (gnus-summary-article-number)))
7014    (and gnus-auto-select-same
7015         (gnus-summary-article-subject))))
7016
7017 (defun gnus-summary-prev-article (&optional unread subject)
7018   "Select the article after the current one.
7019 If UNREAD is non-nil, only unread articles are selected."
7020   (interactive "P")
7021   (gnus-summary-next-article unread subject t))
7022
7023 (defun gnus-summary-prev-unread-article ()
7024   "Select unread article before current one."
7025   (interactive)
7026   (gnus-summary-prev-article
7027    (or (not (eq gnus-summary-goto-unread 'never))
7028        (gnus-summary-first-article-p (gnus-summary-article-number)))
7029    (and gnus-auto-select-same
7030         (gnus-summary-article-subject))))
7031
7032 (defun gnus-summary-next-page (&optional lines circular stop)
7033   "Show next page of the selected article.
7034 If at the end of the current article, select the next article.
7035 LINES says how many lines should be scrolled up.
7036
7037 If CIRCULAR is non-nil, go to the start of the article instead of
7038 selecting the next article when reaching the end of the current
7039 article.
7040
7041 If STOP is non-nil, just stop when reaching the end of the message."
7042   (interactive "P")
7043   (setq gnus-summary-buffer (current-buffer))
7044   (gnus-set-global-variables)
7045   (let ((article (gnus-summary-article-number))
7046         (article-window (get-buffer-window gnus-article-buffer t))
7047         endp)
7048     ;; If the buffer is empty, we have no article.
7049     (unless article
7050       (error "No article to select"))
7051     (gnus-configure-windows 'article)
7052     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7053         (if (and (eq gnus-summary-goto-unread 'never)
7054                  (not (gnus-summary-last-article-p article)))
7055             (gnus-summary-next-article)
7056           (gnus-summary-next-unread-article))
7057       (if (or (null gnus-current-article)
7058               (null gnus-article-current)
7059               (/= article (cdr gnus-article-current))
7060               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7061           ;; Selected subject is different from current article's.
7062           (gnus-summary-display-article article)
7063         (when article-window
7064           (gnus-eval-in-buffer-window gnus-article-buffer
7065             (setq endp (gnus-article-next-page lines)))
7066           (when endp
7067             (cond (stop
7068                    (gnus-message 3 "End of message"))
7069                   (circular
7070                    (gnus-summary-beginning-of-article))
7071                   (lines
7072                    (gnus-message 3 "End of message"))
7073                   ((null lines)
7074                    (if (and (eq gnus-summary-goto-unread 'never)
7075                             (not (gnus-summary-last-article-p article)))
7076                        (gnus-summary-next-article)
7077                      (gnus-summary-next-unread-article))))))))
7078     (gnus-summary-recenter)
7079     (gnus-summary-position-point)))
7080
7081 (defun gnus-summary-prev-page (&optional lines move)
7082   "Show previous page of selected article.
7083 Argument LINES specifies lines to be scrolled down.
7084 If MOVE, move to the previous unread article if point is at
7085 the beginning of the buffer."
7086   (interactive "P")
7087   (let ((article (gnus-summary-article-number))
7088         (article-window (get-buffer-window gnus-article-buffer t))
7089         endp)
7090     (gnus-configure-windows 'article)
7091     (if (or (null gnus-current-article)
7092             (null gnus-article-current)
7093             (/= article (cdr gnus-article-current))
7094             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7095         ;; Selected subject is different from current article's.
7096         (gnus-summary-display-article article)
7097       (gnus-summary-recenter)
7098       (when article-window
7099         (gnus-eval-in-buffer-window gnus-article-buffer
7100           (setq endp (gnus-article-prev-page lines)))
7101         (when (and move endp)
7102           (cond (lines
7103                  (gnus-message 3 "Beginning of message"))
7104                 ((null lines)
7105                  (if (and (eq gnus-summary-goto-unread 'never)
7106                           (not (gnus-summary-first-article-p article)))
7107                      (gnus-summary-prev-article)
7108                    (gnus-summary-prev-unread-article))))))))
7109   (gnus-summary-position-point))
7110
7111 (defun gnus-summary-prev-page-or-article (&optional lines)
7112   "Show previous page of selected article.
7113 Argument LINES specifies lines to be scrolled down.
7114 If at the beginning of the article, go to the next article."
7115   (interactive "P")
7116   (gnus-summary-prev-page lines t))
7117
7118 (defun gnus-summary-scroll-up (lines)
7119   "Scroll up (or down) one line current article.
7120 Argument LINES specifies lines to be scrolled up (or down if negative)."
7121   (interactive "p")
7122   (gnus-configure-windows 'article)
7123   (gnus-summary-show-thread)
7124   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7125     (gnus-eval-in-buffer-window gnus-article-buffer
7126       (cond ((> lines 0)
7127              (when (gnus-article-next-page lines)
7128                (gnus-message 3 "End of message")))
7129             ((< lines 0)
7130              (gnus-article-prev-page (- lines))))))
7131   (gnus-summary-recenter)
7132   (gnus-summary-position-point))
7133
7134 (defun gnus-summary-scroll-down (lines)
7135   "Scroll down (or up) one line current article.
7136 Argument LINES specifies lines to be scrolled down (or up if negative)."
7137   (interactive "p")
7138   (gnus-summary-scroll-up (- lines)))
7139
7140 (defun gnus-summary-next-same-subject ()
7141   "Select next article which has the same subject as current one."
7142   (interactive)
7143   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7144
7145 (defun gnus-summary-prev-same-subject ()
7146   "Select previous article which has the same subject as current one."
7147   (interactive)
7148   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7149
7150 (defun gnus-summary-next-unread-same-subject ()
7151   "Select next unread article which has the same subject as current one."
7152   (interactive)
7153   (gnus-summary-next-article t (gnus-summary-article-subject)))
7154
7155 (defun gnus-summary-prev-unread-same-subject ()
7156   "Select previous unread article which has the same subject as current one."
7157   (interactive)
7158   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7159
7160 (defun gnus-summary-first-unread-article ()
7161   "Select the first unread article.
7162 Return nil if there are no unread articles."
7163   (interactive)
7164   (prog1
7165       (when (gnus-summary-first-subject t)
7166         (gnus-summary-show-thread)
7167         (gnus-summary-first-subject t)
7168         (gnus-summary-display-article (gnus-summary-article-number)))
7169     (gnus-summary-position-point)))
7170
7171 (defun gnus-summary-first-unread-subject ()
7172   "Place the point on the subject line of the first unread article.
7173 Return nil if there are no unread articles."
7174   (interactive)
7175   (prog1
7176       (when (gnus-summary-first-subject t)
7177         (gnus-summary-show-thread)
7178         (gnus-summary-first-subject t))
7179     (gnus-summary-position-point)))
7180
7181 (defun gnus-summary-first-unseen-subject ()
7182   "Place the point on the subject line of the first unseen article.
7183 Return nil if there are no unseen articles."
7184   (interactive)
7185   (prog1
7186       (when (gnus-summary-first-subject t t t)
7187         (gnus-summary-show-thread)
7188         (gnus-summary-first-subject t t t))
7189     (gnus-summary-position-point)))
7190
7191 (defun gnus-summary-first-unseen-or-unread-subject ()
7192   "Place the point on the subject line of the first unseen article.
7193 Return nil if there are no unseen articles."
7194   (interactive)
7195   (prog1
7196       (unless (when (gnus-summary-first-subject t t t)
7197                 (gnus-summary-show-thread)
7198                 (gnus-summary-first-subject t t t))
7199         (when (gnus-summary-first-subject t)
7200           (gnus-summary-show-thread)
7201           (gnus-summary-first-subject t)))
7202     (gnus-summary-position-point)))
7203
7204 (defun gnus-summary-first-article ()
7205   "Select the first article.
7206 Return nil if there are no articles."
7207   (interactive)
7208   (prog1
7209       (when (gnus-summary-first-subject)
7210         (gnus-summary-show-thread)
7211         (gnus-summary-first-subject)
7212         (gnus-summary-display-article (gnus-summary-article-number)))
7213     (gnus-summary-position-point)))
7214
7215 (defun gnus-summary-best-unread-article (&optional arg)
7216   "Select the unread article with the highest score.
7217 If given a prefix argument, select the next unread article that has a
7218 score higher than the default score."
7219   (interactive "P")
7220   (let ((article (if arg
7221                      (gnus-summary-better-unread-subject)
7222                    (gnus-summary-best-unread-subject))))
7223     (if article
7224         (gnus-summary-goto-article article)
7225       (error "No unread articles"))))
7226
7227 (defun gnus-summary-best-unread-subject ()
7228   "Select the unread subject with the highest score."
7229   (interactive)
7230   (let ((best -1000000)
7231         (data gnus-newsgroup-data)
7232         article score)
7233     (while data
7234       (and (gnus-data-unread-p (car data))
7235            (> (setq score
7236                     (gnus-summary-article-score (gnus-data-number (car data))))
7237               best)
7238            (setq best score
7239                  article (gnus-data-number (car data))))
7240       (setq data (cdr data)))
7241     (when article
7242       (gnus-summary-goto-subject article))
7243     (gnus-summary-position-point)
7244     article))
7245
7246 (defun gnus-summary-better-unread-subject ()
7247   "Select the first unread subject that has a score over the default score."
7248   (interactive)
7249   (let ((data gnus-newsgroup-data)
7250         article score)
7251     (while (and (setq article (gnus-data-number (car data)))
7252                 (or (gnus-data-read-p (car data))
7253                     (not (> (gnus-summary-article-score article)
7254                             gnus-summary-default-score))))
7255       (setq data (cdr data)))
7256     (when article
7257       (gnus-summary-goto-subject article))
7258     (gnus-summary-position-point)
7259     article))
7260
7261 (defun gnus-summary-last-subject ()
7262   "Go to the last displayed subject line in the group."
7263   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7264     (when article
7265       (gnus-summary-goto-subject article))))
7266
7267 (defun gnus-summary-goto-article (article &optional all-headers force)
7268   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7269 If ALL-HEADERS is non-nil, no header lines are hidden.
7270 If FORCE, go to the article even if it isn't displayed.  If FORCE
7271 is a number, it is the line the article is to be displayed on."
7272   (interactive
7273    (list
7274     (completing-read
7275      "Article number or Message-ID: "
7276      (mapcar (lambda (number) (list (int-to-string number)))
7277              gnus-newsgroup-limit))
7278     current-prefix-arg
7279     t))
7280   (prog1
7281       (if (and (stringp article)
7282                (string-match "@" article))
7283           (gnus-summary-refer-article article)
7284         (when (stringp article)
7285           (setq article (string-to-number article)))
7286         (if (gnus-summary-goto-subject article force)
7287             (gnus-summary-display-article article all-headers)
7288           (gnus-message 4 "Couldn't go to article %s" article) nil))
7289     (gnus-summary-position-point)))
7290
7291 (defun gnus-summary-goto-last-article ()
7292   "Go to the previously read article."
7293   (interactive)
7294   (prog1
7295       (when gnus-last-article
7296         (gnus-summary-goto-article gnus-last-article nil t))
7297     (gnus-summary-position-point)))
7298
7299 (defun gnus-summary-pop-article (number)
7300   "Pop one article off the history and go to the previous.
7301 NUMBER articles will be popped off."
7302   (interactive "p")
7303   (let (to)
7304     (setq gnus-newsgroup-history
7305           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7306     (if to
7307         (gnus-summary-goto-article (car to) nil t)
7308       (error "Article history empty")))
7309   (gnus-summary-position-point))
7310
7311 ;; Summary commands and functions for limiting the summary buffer.
7312
7313 (defun gnus-summary-limit-to-articles (n)
7314   "Limit the summary buffer to the next N articles.
7315 If not given a prefix, use the process marked articles instead."
7316   (interactive "P")
7317   (prog1
7318       (let ((articles (gnus-summary-work-articles n)))
7319         (setq gnus-newsgroup-processable nil)
7320         (gnus-summary-limit articles))
7321     (gnus-summary-position-point)))
7322
7323 (defun gnus-summary-pop-limit (&optional total)
7324   "Restore the previous limit.
7325 If given a prefix, remove all limits."
7326   (interactive "P")
7327   (when total
7328     (setq gnus-newsgroup-limits
7329           (list (mapcar (lambda (h) (mail-header-number h))
7330                         gnus-newsgroup-headers))))
7331   (unless gnus-newsgroup-limits
7332     (error "No limit to pop"))
7333   (prog1
7334       (gnus-summary-limit nil 'pop)
7335     (gnus-summary-position-point)))
7336
7337 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7338   "Limit the summary buffer to articles that have subjects that match a regexp.
7339 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7340   (interactive
7341    (list (read-string (if current-prefix-arg
7342                           "Exclude subject (regexp): "
7343                         "Limit to subject (regexp): "))
7344          nil current-prefix-arg))
7345   (unless header
7346     (setq header "subject"))
7347   (when (not (equal "" subject))
7348     (prog1
7349         (let ((articles (gnus-summary-find-matching
7350                          (or header "subject") subject 'all nil nil
7351                          not-matching)))
7352           (unless articles
7353             (error "Found no matches for \"%s\"" subject))
7354           (gnus-summary-limit articles))
7355       (gnus-summary-position-point))))
7356
7357 (defun gnus-summary-limit-to-author (from &optional not-matching)
7358   "Limit the summary buffer to articles that have authors that match a regexp.
7359 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7360   (interactive
7361    (list (read-string (if current-prefix-arg
7362                           "Exclude author (regexp): "
7363                         "Limit to author (regexp): "))
7364          current-prefix-arg))
7365   (gnus-summary-limit-to-subject from "from" not-matching))
7366
7367 (defun gnus-summary-limit-to-age (age &optional younger-p)
7368   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7369 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7370 articles that are younger than AGE days."
7371   (interactive
7372    (let ((younger current-prefix-arg)
7373          (days-got nil)
7374          days)
7375      (while (not days-got)
7376        (setq days (if younger
7377                       (read-string "Limit to articles younger than (in days, older when negative): ")
7378                     (read-string
7379                      "Limit to articles older than (in days, younger when negative): ")))
7380        (when (> (length days) 0)
7381          (setq days (read days)))
7382        (if (numberp days)
7383            (progn
7384              (setq days-got t)
7385              (if (< days 0)
7386                  (progn
7387                    (setq younger (not younger))
7388                    (setq days (* days -1)))))
7389          (message "Please enter a number.")
7390          (sleep-for 1)))
7391      (list days younger)))
7392   (prog1
7393       (let ((data gnus-newsgroup-data)
7394             (cutoff (days-to-time age))
7395             articles d date is-younger)
7396         (while (setq d (pop data))
7397           (when (and (vectorp (gnus-data-header d))
7398                      (setq date (mail-header-date (gnus-data-header d))))
7399             (setq is-younger (time-less-p
7400                               (time-since (condition-case ()
7401                                               (date-to-time date)
7402                                             (error '(0 0))))
7403                               cutoff))
7404             (when (if younger-p
7405                       is-younger
7406                     (not is-younger))
7407               (push (gnus-data-number d) articles))))
7408         (gnus-summary-limit (nreverse articles)))
7409     (gnus-summary-position-point)))
7410
7411 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7412   "Limit the summary buffer to articles that match an 'extra' header."
7413   (interactive
7414    (let ((header
7415           (intern
7416            (gnus-completing-read-with-default
7417             (symbol-name (car gnus-extra-headers))
7418             (if current-prefix-arg
7419                 "Exclude extra header:"
7420               "Limit extra header:")
7421             (mapcar (lambda (x)
7422                       (cons (symbol-name x) x))
7423                     gnus-extra-headers)
7424             nil
7425             t))))
7426      (list header
7427            (read-string (format "%s header %s (regexp): "
7428                                 (if current-prefix-arg "Exclude" "Limit to")
7429                                 header))
7430            current-prefix-arg)))
7431   (when (not (equal "" regexp))
7432     (prog1
7433         (let ((articles (gnus-summary-find-matching
7434                          (cons 'extra header) regexp 'all nil nil
7435                          not-matching)))
7436           (unless articles
7437             (error "Found no matches for \"%s\"" regexp))
7438           (gnus-summary-limit articles))
7439       (gnus-summary-position-point))))
7440
7441 (defun gnus-summary-limit-to-display-predicate ()
7442   "Limit the summary buffer to the predicated in the `display' group parameter."
7443   (interactive)
7444   (unless gnus-newsgroup-display
7445     (error "There is no `display' group parameter"))
7446   (let (articles)
7447     (dolist (number gnus-newsgroup-articles)
7448       (when (funcall gnus-newsgroup-display)
7449         (push number articles)))
7450     (gnus-summary-limit articles))
7451   (gnus-summary-position-point))
7452
7453 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7454 (make-obsolete
7455  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7456
7457 (defun gnus-summary-limit-to-unread (&optional all)
7458   "Limit the summary buffer to articles that are not marked as read.
7459 If ALL is non-nil, limit strictly to unread articles."
7460   (interactive "P")
7461   (if all
7462       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7463     (gnus-summary-limit-to-marks
7464      ;; Concat all the marks that say that an article is read and have
7465      ;; those removed.
7466      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7467            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7468            gnus-low-score-mark gnus-expirable-mark
7469            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7470            gnus-duplicate-mark gnus-souped-mark)
7471      'reverse)))
7472
7473 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7474 (make-obsolete 'gnus-summary-delete-marked-with
7475                'gnus-summary-limit-exclude-marks)
7476
7477 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7478   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7479 If REVERSE, limit the summary buffer to articles that are marked
7480 with MARKS.  MARKS can either be a string of marks or a list of marks.
7481 Returns how many articles were removed."
7482   (interactive "sMarks: ")
7483   (gnus-summary-limit-to-marks marks t))
7484
7485 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7486   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7487 If REVERSE (the prefix), limit the summary buffer to articles that are
7488 not marked with MARKS.  MARKS can either be a string of marks or a
7489 list of marks.
7490 Returns how many articles were removed."
7491   (interactive "sMarks: \nP")
7492   (prog1
7493       (let ((data gnus-newsgroup-data)
7494             (marks (if (listp marks) marks
7495                      (append marks nil))) ; Transform to list.
7496             articles)
7497         (while data
7498           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7499                   (memq (gnus-data-mark (car data)) marks))
7500             (push (gnus-data-number (car data)) articles))
7501           (setq data (cdr data)))
7502         (gnus-summary-limit articles))
7503     (gnus-summary-position-point)))
7504
7505 (defun gnus-summary-limit-to-score (score)
7506   "Limit to articles with score at or above SCORE."
7507   (interactive "NLimit to articles with score of at least: ")
7508   (let ((data gnus-newsgroup-data)
7509         articles)
7510     (while data
7511       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7512                 score)
7513         (push (gnus-data-number (car data)) articles))
7514       (setq data (cdr data)))
7515     (prog1
7516         (gnus-summary-limit articles)
7517       (gnus-summary-position-point))))
7518
7519 (defun gnus-summary-limit-to-unseen ()
7520   "Limit to unseen articles."
7521   (interactive)
7522   (prog1
7523       (gnus-summary-limit gnus-newsgroup-unseen)
7524     (gnus-summary-position-point)))
7525
7526 (defun gnus-summary-limit-include-thread (id)
7527   "Display all the hidden articles that is in the thread with ID in it.
7528 When called interactively, ID is the Message-ID of the current
7529 article."
7530   (interactive (list (mail-header-id (gnus-summary-article-header))))
7531   (let ((articles (gnus-articles-in-thread
7532                    (gnus-id-to-thread (gnus-root-id id)))))
7533     (prog1
7534         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7535       (gnus-summary-limit-include-matching-articles
7536        "subject"
7537        (regexp-quote (gnus-simplify-subject-re
7538                       (mail-header-subject (gnus-id-to-header id)))))
7539       (gnus-summary-position-point))))
7540
7541 (defun gnus-summary-limit-include-matching-articles (header regexp)
7542   "Display all the hidden articles that have HEADERs that match REGEXP."
7543   (interactive (list (read-string "Match on header: ")
7544                      (read-string "Regexp: ")))
7545   (let ((articles (gnus-find-matching-articles header regexp)))
7546     (prog1
7547         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7548       (gnus-summary-position-point))))
7549
7550 (defun gnus-summary-insert-dormant-articles ()
7551   "Insert all the dormat articles for this group into the current buffer."
7552   (interactive)
7553   (let ((gnus-verbose (max 6 gnus-verbose)))
7554     (if (not gnus-newsgroup-dormant)
7555         (gnus-message 3 "No cached articles for this group")
7556       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7557
7558 (defun gnus-summary-limit-include-dormant ()
7559   "Display all the hidden articles that are marked as dormant.
7560 Note that this command only works on a subset of the articles currently
7561 fetched for this group."
7562   (interactive)
7563   (unless gnus-newsgroup-dormant
7564     (error "There are no dormant articles in this group"))
7565   (prog1
7566       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7567     (gnus-summary-position-point)))
7568
7569 (defun gnus-summary-limit-exclude-dormant ()
7570   "Hide all dormant articles."
7571   (interactive)
7572   (prog1
7573       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7574     (gnus-summary-position-point)))
7575
7576 (defun gnus-summary-limit-exclude-childless-dormant ()
7577   "Hide all dormant articles that have no children."
7578   (interactive)
7579   (let ((data (gnus-data-list t))
7580         articles d children)
7581     ;; Find all articles that are either not dormant or have
7582     ;; children.
7583     (while (setq d (pop data))
7584       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7585                 (and (setq children
7586                            (gnus-article-children (gnus-data-number d)))
7587                      (let (found)
7588                        (while children
7589                          (when (memq (car children) articles)
7590                            (setq children nil
7591                                  found t))
7592                          (pop children))
7593                        found)))
7594         (push (gnus-data-number d) articles)))
7595     ;; Do the limiting.
7596     (prog1
7597         (gnus-summary-limit articles)
7598       (gnus-summary-position-point))))
7599
7600 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7601   "Mark all unread excluded articles as read.
7602 If ALL, mark even excluded ticked and dormants as read."
7603   (interactive "P")
7604   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7605   (let ((articles (gnus-sorted-ndifference
7606                    (sort
7607                     (mapcar (lambda (h) (mail-header-number h))
7608                             gnus-newsgroup-headers)
7609                     '<)
7610                    gnus-newsgroup-limit))
7611         article)
7612     (setq gnus-newsgroup-unreads
7613           (gnus-sorted-intersection gnus-newsgroup-unreads
7614                                     gnus-newsgroup-limit))
7615     (if all
7616         (setq gnus-newsgroup-dormant nil
7617               gnus-newsgroup-marked nil
7618               gnus-newsgroup-reads
7619               (nconc
7620                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7621                gnus-newsgroup-reads))
7622       (while (setq article (pop articles))
7623         (unless (or (memq article gnus-newsgroup-dormant)
7624                     (memq article gnus-newsgroup-marked))
7625           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7626
7627 (defun gnus-summary-limit (articles &optional pop)
7628   (if pop
7629       ;; We pop the previous limit off the stack and use that.
7630       (setq articles (car gnus-newsgroup-limits)
7631             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7632     ;; We use the new limit, so we push the old limit on the stack.
7633     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7634   ;; Set the limit.
7635   (setq gnus-newsgroup-limit articles)
7636   (let ((total (length gnus-newsgroup-data))
7637         (data (gnus-data-find-list (gnus-summary-article-number)))
7638         (gnus-summary-mark-below nil)   ; Inhibit this.
7639         found)
7640     ;; This will do all the work of generating the new summary buffer
7641     ;; according to the new limit.
7642     (gnus-summary-prepare)
7643     ;; Hide any threads, possibly.
7644     (gnus-summary-maybe-hide-threads)
7645     ;; Try to return to the article you were at, or one in the
7646     ;; neighborhood.
7647     (when data
7648       ;; We try to find some article after the current one.
7649       (while data
7650         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7651           (setq data nil
7652                 found t))
7653         (setq data (cdr data))))
7654     (unless found
7655       ;; If there is no data, that means that we were after the last
7656       ;; article.  The same goes when we can't find any articles
7657       ;; after the current one.
7658       (goto-char (point-max))
7659       (gnus-summary-find-prev))
7660     (gnus-set-mode-line 'summary)
7661     ;; We return how many articles were removed from the summary
7662     ;; buffer as a result of the new limit.
7663     (- total (length gnus-newsgroup-data))))
7664
7665 (defsubst gnus-invisible-cut-children (threads)
7666   (let ((num 0))
7667     (while threads
7668       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7669         (incf num))
7670       (pop threads))
7671     (< num 2)))
7672
7673 (defsubst gnus-cut-thread (thread)
7674   "Go forwards in the thread until we find an article that we want to display."
7675   (when (or (eq gnus-fetch-old-headers 'some)
7676             (eq gnus-fetch-old-headers 'invisible)
7677             (numberp gnus-fetch-old-headers)
7678             (eq gnus-build-sparse-threads 'some)
7679             (eq gnus-build-sparse-threads 'more))
7680     ;; Deal with old-fetched headers and sparse threads.
7681     (while (and
7682             thread
7683             (or
7684              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7685              (gnus-summary-article-ancient-p
7686               (mail-header-number (car thread))))
7687             (if (or (<= (length (cdr thread)) 1)
7688                     (eq gnus-fetch-old-headers 'invisible))
7689                 (setq gnus-newsgroup-limit
7690                       (delq (mail-header-number (car thread))
7691                             gnus-newsgroup-limit)
7692                       thread (cadr thread))
7693               (when (gnus-invisible-cut-children (cdr thread))
7694                 (let ((th (cdr thread)))
7695                   (while th
7696                     (if (memq (mail-header-number (caar th))
7697                               gnus-newsgroup-limit)
7698                         (setq thread (car th)
7699                               th nil)
7700                       (setq th (cdr th))))))))))
7701   thread)
7702
7703 (defun gnus-cut-threads (threads)
7704   "Cut off all uninteresting articles from the beginning of threads."
7705   (when (or (eq gnus-fetch-old-headers 'some)
7706             (eq gnus-fetch-old-headers 'invisible)
7707             (numberp gnus-fetch-old-headers)
7708             (eq gnus-build-sparse-threads 'some)
7709             (eq gnus-build-sparse-threads 'more))
7710     (let ((th threads))
7711       (while th
7712         (setcar th (gnus-cut-thread (car th)))
7713         (setq th (cdr th)))))
7714   ;; Remove nixed out threads.
7715   (delq nil threads))
7716
7717 (defun gnus-summary-initial-limit (&optional show-if-empty)
7718   "Figure out what the initial limit is supposed to be on group entry.
7719 This entails weeding out unwanted dormants, low-scored articles,
7720 fetch-old-headers verbiage, and so on."
7721   ;; Most groups have nothing to remove.
7722   (if (or gnus-inhibit-limiting
7723           (and (null gnus-newsgroup-dormant)
7724                (eq gnus-newsgroup-display 'gnus-not-ignore)
7725                (not (eq gnus-fetch-old-headers 'some))
7726                (not (numberp gnus-fetch-old-headers))
7727                (not (eq gnus-fetch-old-headers 'invisible))
7728                (null gnus-summary-expunge-below)
7729                (not (eq gnus-build-sparse-threads 'some))
7730                (not (eq gnus-build-sparse-threads 'more))
7731                (null gnus-thread-expunge-below)
7732                (not gnus-use-nocem)))
7733       ()                                ; Do nothing.
7734     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7735     (setq gnus-newsgroup-limit nil)
7736     (mapatoms
7737      (lambda (node)
7738        (unless (car (symbol-value node))
7739          ;; These threads have no parents -- they are roots.
7740          (let ((nodes (cdr (symbol-value node)))
7741                thread)
7742            (while nodes
7743              (if (and gnus-thread-expunge-below
7744                       (< (gnus-thread-total-score (car nodes))
7745                          gnus-thread-expunge-below))
7746                  (gnus-expunge-thread (pop nodes))
7747                (setq thread (pop nodes))
7748                (gnus-summary-limit-children thread))))))
7749      gnus-newsgroup-dependencies)
7750     ;; If this limitation resulted in an empty group, we might
7751     ;; pop the previous limit and use it instead.
7752     (when (and (not gnus-newsgroup-limit)
7753                show-if-empty)
7754       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7755     gnus-newsgroup-limit))
7756
7757 (defun gnus-summary-limit-children (thread)
7758   "Return 1 if this subthread is visible and 0 if it is not."
7759   ;; First we get the number of visible children to this thread.  This
7760   ;; is done by recursing down the thread using this function, so this
7761   ;; will really go down to a leaf article first, before slowly
7762   ;; working its way up towards the root.
7763   (when thread
7764     (let* ((max-lisp-eval-depth 5000)
7765            (children
7766            (if (cdr thread)
7767                (apply '+ (mapcar 'gnus-summary-limit-children
7768                                  (cdr thread)))
7769              0))
7770           (number (mail-header-number (car thread)))
7771           score)
7772       (if (and
7773            (not (memq number gnus-newsgroup-marked))
7774            (or
7775             ;; If this article is dormant and has absolutely no visible
7776             ;; children, then this article isn't visible.
7777             (and (memq number gnus-newsgroup-dormant)
7778                  (zerop children))
7779             ;; If this is "fetch-old-headered" and there is no
7780             ;; visible children, then we don't want this article.
7781             (and (or (eq gnus-fetch-old-headers 'some)
7782                      (numberp gnus-fetch-old-headers))
7783                  (gnus-summary-article-ancient-p number)
7784                  (zerop children))
7785             ;; If this is "fetch-old-headered" and `invisible', then
7786             ;; we don't want this article.
7787             (and (eq gnus-fetch-old-headers 'invisible)
7788                  (gnus-summary-article-ancient-p number))
7789             ;; If this is a sparsely inserted article with no children,
7790             ;; we don't want it.
7791             (and (eq gnus-build-sparse-threads 'some)
7792                  (gnus-summary-article-sparse-p number)
7793                  (zerop children))
7794             ;; If we use expunging, and this article is really
7795             ;; low-scored, then we don't want this article.
7796             (when (and gnus-summary-expunge-below
7797                        (< (setq score
7798                                 (or (cdr (assq number gnus-newsgroup-scored))
7799                                     gnus-summary-default-score))
7800                           gnus-summary-expunge-below))
7801               ;; We increase the expunge-tally here, but that has
7802               ;; nothing to do with the limits, really.
7803               (incf gnus-newsgroup-expunged-tally)
7804               ;; We also mark as read here, if that's wanted.
7805               (when (and gnus-summary-mark-below
7806                          (< score gnus-summary-mark-below))
7807                 (setq gnus-newsgroup-unreads
7808                       (delq number gnus-newsgroup-unreads))
7809                 (if gnus-newsgroup-auto-expire
7810                     (push number gnus-newsgroup-expirable)
7811                   (push (cons number gnus-low-score-mark)
7812                         gnus-newsgroup-reads)))
7813               t)
7814             ;; Do the `display' group parameter.
7815             (and gnus-newsgroup-display
7816                  (not (funcall gnus-newsgroup-display)))
7817             ;; Check NoCeM things.
7818             (if (and gnus-use-nocem
7819                      (gnus-nocem-unwanted-article-p
7820                       (mail-header-id (car thread))))
7821                 (progn
7822                   (setq gnus-newsgroup-unreads
7823                         (delq number gnus-newsgroup-unreads))
7824                   t))))
7825           ;; Nope, invisible article.
7826           0
7827         ;; Ok, this article is to be visible, so we add it to the limit
7828         ;; and return 1.
7829         (push number gnus-newsgroup-limit)
7830         1))))
7831
7832 (defun gnus-expunge-thread (thread)
7833   "Mark all articles in THREAD as read."
7834   (let* ((number (mail-header-number (car thread))))
7835     (incf gnus-newsgroup-expunged-tally)
7836     ;; We also mark as read here, if that's wanted.
7837     (setq gnus-newsgroup-unreads
7838           (delq number gnus-newsgroup-unreads))
7839     (if gnus-newsgroup-auto-expire
7840         (push number gnus-newsgroup-expirable)
7841       (push (cons number gnus-low-score-mark)
7842             gnus-newsgroup-reads)))
7843   ;; Go recursively through all subthreads.
7844   (mapcar 'gnus-expunge-thread (cdr thread)))
7845
7846 ;; Summary article oriented commands
7847
7848 (defun gnus-summary-refer-parent-article (n)
7849   "Refer parent article N times.
7850 If N is negative, go to ancestor -N instead.
7851 The difference between N and the number of articles fetched is returned."
7852   (interactive "p")
7853   (let ((skip 1)
7854         error header ref)
7855     (when (not (natnump n))
7856       (setq skip (abs n)
7857             n 1))
7858     (while (and (> n 0)
7859                 (not error))
7860       (setq header (gnus-summary-article-header))
7861       (if (and (eq (mail-header-number header)
7862                    (cdr gnus-article-current))
7863                (equal gnus-newsgroup-name
7864                       (car gnus-article-current)))
7865           ;; If we try to find the parent of the currently
7866           ;; displayed article, then we take a look at the actual
7867           ;; References header, since this is slightly more
7868           ;; reliable than the References field we got from the
7869           ;; server.
7870           (save-excursion
7871             (set-buffer gnus-original-article-buffer)
7872             (nnheader-narrow-to-headers)
7873             (unless (setq ref (message-fetch-field "references"))
7874               (setq ref (message-fetch-field "in-reply-to")))
7875             (widen))
7876         (setq ref
7877               ;; It's not the current article, so we take a bet on
7878               ;; the value we got from the server.
7879               (mail-header-references header)))
7880       (if (and ref
7881                (not (equal ref "")))
7882           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7883             (gnus-message 1 "Couldn't find parent"))
7884         (gnus-message 1 "No references in article %d"
7885                       (gnus-summary-article-number))
7886         (setq error t))
7887       (decf n))
7888     (gnus-summary-position-point)
7889     n))
7890
7891 (defun gnus-summary-refer-references ()
7892   "Fetch all articles mentioned in the References header.
7893 Return the number of articles fetched."
7894   (interactive)
7895   (let ((ref (mail-header-references (gnus-summary-article-header)))
7896         (current (gnus-summary-article-number))
7897         (n 0))
7898     (if (or (not ref)
7899             (equal ref ""))
7900         (error "No References in the current article")
7901       ;; For each Message-ID in the References header...
7902       (while (string-match "<[^>]*>" ref)
7903         (incf n)
7904         ;; ... fetch that article.
7905         (gnus-summary-refer-article
7906          (prog1 (match-string 0 ref)
7907            (setq ref (substring ref (match-end 0))))))
7908       (gnus-summary-goto-subject current)
7909       (gnus-summary-position-point)
7910       n)))
7911
7912 (defun gnus-summary-refer-thread (&optional limit)
7913   "Fetch all articles in the current thread.
7914 If LIMIT (the numerical prefix), fetch that many old headers instead
7915 of what's specified by the `gnus-refer-thread-limit' variable."
7916   (interactive "P")
7917   (let ((id (mail-header-id (gnus-summary-article-header)))
7918         (limit (if limit (prefix-numeric-value limit)
7919                  gnus-refer-thread-limit)))
7920     ;; We want to fetch LIMIT *old* headers, but we also have to
7921     ;; re-fetch all the headers in the current buffer, because many of
7922     ;; them may be undisplayed.  So we adjust LIMIT.
7923     (when (numberp limit)
7924       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7925     (unless (eq gnus-fetch-old-headers 'invisible)
7926       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7927       ;; Retrieve the headers and read them in.
7928       (if (eq (gnus-retrieve-headers
7929                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7930               'nov)
7931           (gnus-build-all-threads)
7932         (error "Can't fetch thread from backends that don't support NOV"))
7933       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7934     (gnus-summary-limit-include-thread id)))
7935
7936 (defun gnus-summary-refer-article (message-id)
7937   "Fetch an article specified by MESSAGE-ID."
7938   (interactive "sMessage-ID: ")
7939   (when (and (stringp message-id)
7940              (not (zerop (length message-id))))
7941     ;; Construct the correct Message-ID if necessary.
7942     ;; Suggested by tale@pawl.rpi.edu.
7943     (unless (string-match "^<" message-id)
7944       (setq message-id (concat "<" message-id)))
7945     (unless (string-match ">$" message-id)
7946       (setq message-id (concat message-id ">")))
7947     (let* ((header (gnus-id-to-header message-id))
7948            (sparse (and header
7949                         (gnus-summary-article-sparse-p
7950                          (mail-header-number header))
7951                         (memq (mail-header-number header)
7952                               gnus-newsgroup-limit)))
7953            number)
7954       (cond
7955        ;; If the article is present in the buffer we just go to it.
7956        ((and header
7957              (or (not (gnus-summary-article-sparse-p
7958                        (mail-header-number header)))
7959                  sparse))
7960         (prog1
7961             (gnus-summary-goto-article
7962              (mail-header-number header) nil t)
7963           (when sparse
7964             (gnus-summary-update-article (mail-header-number header)))))
7965        (t
7966         ;; We fetch the article.
7967         (catch 'found
7968           (dolist (gnus-override-method (gnus-refer-article-methods))
7969             (gnus-check-server gnus-override-method)
7970             ;; Fetch the header, and display the article.
7971             (when (setq number (gnus-summary-insert-subject message-id))
7972               (gnus-summary-select-article nil nil nil number)
7973               (throw 'found t)))
7974           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7975
7976 (defun gnus-refer-article-methods ()
7977   "Return a list of referable methods."
7978   (cond
7979    ;; No method, so we default to current and native.
7980    ((null gnus-refer-article-method)
7981     (list gnus-current-select-method gnus-select-method))
7982    ;; Current.
7983    ((eq 'current gnus-refer-article-method)
7984     (list gnus-current-select-method))
7985    ;; List of select methods.
7986    ((not (and (symbolp (car gnus-refer-article-method))
7987               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7988     (let (out)
7989       (dolist (method gnus-refer-article-method)
7990         (push (if (eq 'current method)
7991                   gnus-current-select-method
7992                 method)
7993               out))
7994       (nreverse out)))
7995    ;; One single select method.
7996    (t
7997     (list gnus-refer-article-method))))
7998
7999 (defun gnus-summary-edit-parameters ()
8000   "Edit the group parameters of the current group."
8001   (interactive)
8002   (gnus-group-edit-group gnus-newsgroup-name 'params))
8003
8004 (defun gnus-summary-customize-parameters ()
8005   "Customize the group parameters of the current group."
8006   (interactive)
8007   (gnus-group-customize gnus-newsgroup-name))
8008
8009 (defun gnus-summary-enter-digest-group (&optional force)
8010   "Enter an nndoc group based on the current article.
8011 If FORCE, force a digest interpretation.  If not, try
8012 to guess what the document format is."
8013   (interactive "P")
8014   (let ((conf gnus-current-window-configuration))
8015     (save-excursion
8016       (gnus-summary-select-article))
8017     (setq gnus-current-window-configuration conf)
8018     (let* ((name (format "%s-%d"
8019                          (gnus-group-prefixed-name
8020                           gnus-newsgroup-name (list 'nndoc ""))
8021                          (save-excursion
8022                            (set-buffer gnus-summary-buffer)
8023                            gnus-current-article)))
8024            (ogroup gnus-newsgroup-name)
8025            (params (append (gnus-info-params (gnus-get-info ogroup))
8026                            (list (cons 'to-group ogroup))
8027                            (list (cons 'save-article-group ogroup))))
8028            (case-fold-search t)
8029            (buf (current-buffer))
8030            dig to-address)
8031       (save-excursion
8032         (set-buffer gnus-original-article-buffer)
8033         ;; Have the digest group inherit the main mail address of
8034         ;; the parent article.
8035         (when (setq to-address (or (gnus-fetch-field "reply-to")
8036                                    (gnus-fetch-field "from")))
8037           (setq params (append
8038                         (list (cons 'to-address
8039                                     (funcall gnus-decode-encoded-word-function
8040                                              to-address))))))
8041         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8042         (insert-buffer-substring gnus-original-article-buffer)
8043         ;; Remove lines that may lead nndoc to misinterpret the
8044         ;; document type.
8045         (narrow-to-region
8046          (goto-char (point-min))
8047          (or (search-forward "\n\n" nil t) (point)))
8048         (goto-char (point-min))
8049         (delete-matching-lines "^Path:\\|^From ")
8050         (widen))
8051       (unwind-protect
8052           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8053                     (gnus-newsgroup-ephemeral-ignored-charsets
8054                      gnus-newsgroup-ignored-charsets))
8055                 (gnus-group-read-ephemeral-group
8056                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8057                               (nndoc-article-type
8058                                ,(if force 'mbox 'guess)))
8059                  t nil nil nil
8060                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8061                                                         "ADAPT")))))
8062               ;; Make all postings to this group go to the parent group.
8063               (nconc (gnus-info-params (gnus-get-info name))
8064                      params)
8065             ;; Couldn't select this doc group.
8066             (switch-to-buffer buf)
8067             (gnus-set-global-variables)
8068             (gnus-configure-windows 'summary)
8069             (gnus-message 3 "Article couldn't be entered?"))
8070         (kill-buffer dig)))))
8071
8072 (defun gnus-summary-read-document (n)
8073   "Open a new group based on the current article(s).
8074 This will allow you to read digests and other similar
8075 documents as newsgroups.
8076 Obeys the standard process/prefix convention."
8077   (interactive "P")
8078   (let* ((articles (gnus-summary-work-articles n))
8079          (ogroup gnus-newsgroup-name)
8080          (params (append (gnus-info-params (gnus-get-info ogroup))
8081                          (list (cons 'to-group ogroup))))
8082          article group egroup groups vgroup)
8083     (while (setq article (pop articles))
8084       (setq group (format "%s-%d" gnus-newsgroup-name article))
8085       (gnus-summary-remove-process-mark article)
8086       (when (gnus-summary-display-article article)
8087         (save-excursion
8088           (with-temp-buffer
8089             (insert-buffer-substring gnus-original-article-buffer)
8090             ;; Remove some headers that may lead nndoc to make
8091             ;; the wrong guess.
8092             (message-narrow-to-head)
8093             (goto-char (point-min))
8094             (delete-matching-lines "^\\(Path\\):\\|^From ")
8095             (widen)
8096             (if (setq egroup
8097                       (gnus-group-read-ephemeral-group
8098                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8099                                      (nndoc-article-type guess))
8100                        t nil t))
8101                 (progn
8102             ;; Make all postings to this group go to the parent group.
8103                   (nconc (gnus-info-params (gnus-get-info egroup))
8104                          params)
8105                   (push egroup groups))
8106               ;; Couldn't select this doc group.
8107               (gnus-error 3 "Article couldn't be entered"))))))
8108     ;; Now we have selected all the documents.
8109     (cond
8110      ((not groups)
8111       (error "None of the articles could be interpreted as documents"))
8112      ((gnus-group-read-ephemeral-group
8113        (setq vgroup (format
8114                      "nnvirtual:%s-%s" gnus-newsgroup-name
8115                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8116        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8117        t
8118        (cons (current-buffer) 'summary)))
8119      (t
8120       (error "Couldn't select virtual nndoc group")))))
8121
8122 (defun gnus-summary-isearch-article (&optional regexp-p)
8123   "Do incremental search forward on the current article.
8124 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8125   (interactive "P")
8126   (gnus-summary-select-article)
8127   (gnus-configure-windows 'article)
8128   (gnus-eval-in-buffer-window gnus-article-buffer
8129     (save-restriction
8130       (widen)
8131       (isearch-forward regexp-p))))
8132
8133 (defun gnus-summary-search-article-forward (regexp &optional backward)
8134   "Search for an article containing REGEXP forward.
8135 If BACKWARD, search backward instead."
8136   (interactive
8137    (list (read-string
8138           (format "Search article %s (regexp%s): "
8139                   (if current-prefix-arg "backward" "forward")
8140                   (if gnus-last-search-regexp
8141                       (concat ", default " gnus-last-search-regexp)
8142                     "")))
8143          current-prefix-arg))
8144   (if (string-equal regexp "")
8145       (setq regexp (or gnus-last-search-regexp ""))
8146     (setq gnus-last-search-regexp regexp)
8147     (setq gnus-article-before-search gnus-current-article))
8148   ;; Intentionally set gnus-last-article.
8149   (setq gnus-last-article gnus-article-before-search)
8150   (let ((gnus-last-article gnus-last-article))
8151     (if (gnus-summary-search-article regexp backward)
8152         (gnus-summary-show-thread)
8153       (error "Search failed: \"%s\"" regexp))))
8154
8155 (defun gnus-summary-search-article-backward (regexp)
8156   "Search for an article containing REGEXP backward."
8157   (interactive
8158    (list (read-string
8159           (format "Search article backward (regexp%s): "
8160                   (if gnus-last-search-regexp
8161                       (concat ", default " gnus-last-search-regexp)
8162                     "")))))
8163   (gnus-summary-search-article-forward regexp 'backward))
8164
8165 (defun gnus-summary-search-article (regexp &optional backward)
8166   "Search for an article containing REGEXP.
8167 Optional argument BACKWARD means do search for backward.
8168 `gnus-select-article-hook' is not called during the search."
8169   ;; We have to require this here to make sure that the following
8170   ;; dynamic binding isn't shadowed by autoloading.
8171   (require 'gnus-async)
8172   (require 'gnus-art)
8173   (let ((gnus-select-article-hook nil)  ;Disable hook.
8174         (gnus-article-prepare-hook nil)
8175         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8176         (gnus-use-article-prefetch nil)
8177         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8178         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8179         (sum (current-buffer))
8180         (gnus-display-mime-function nil)
8181         (found nil)
8182         point)
8183     (gnus-save-hidden-threads
8184       (gnus-summary-select-article)
8185       (set-buffer gnus-article-buffer)
8186       (goto-char (window-point (get-buffer-window (current-buffer))))
8187       (when backward
8188         (forward-line -1))
8189       (while (not found)
8190         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8191         (if (if backward
8192                 (re-search-backward regexp nil t)
8193               (re-search-forward regexp nil t))
8194             ;; We found the regexp.
8195             (progn
8196               (setq found 'found)
8197               (beginning-of-line)
8198               (set-window-start
8199                (get-buffer-window (current-buffer))
8200                (point))
8201               (forward-line 1)
8202               (set-window-point
8203                (get-buffer-window (current-buffer))
8204                (point))
8205               (set-buffer sum)
8206               (setq point (point)))
8207           ;; We didn't find it, so we go to the next article.
8208           (set-buffer sum)
8209           (setq found 'not)
8210           (while (eq found 'not)
8211             (if (not (if backward (gnus-summary-find-prev)
8212                        (gnus-summary-find-next)))
8213                 ;; No more articles.
8214                 (setq found t)
8215               ;; Select the next article and adjust point.
8216               (unless (gnus-summary-article-sparse-p
8217                        (gnus-summary-article-number))
8218                 (setq found nil)
8219                 (gnus-summary-select-article)
8220                 (set-buffer gnus-article-buffer)
8221                 (widen)
8222                 (goto-char (if backward (point-max) (point-min))))))))
8223       (gnus-message 7 ""))
8224     ;; Return whether we found the regexp.
8225     (when (eq found 'found)
8226       (goto-char point)
8227       (gnus-summary-show-thread)
8228       (gnus-summary-goto-subject gnus-current-article)
8229       (gnus-summary-position-point)
8230       t)))
8231
8232 (defun gnus-find-matching-articles (header regexp)
8233   "Return a list of all articles that match REGEXP on HEADER.
8234 This search includes all articles in the current group that Gnus has
8235 fetched headers for, whether they are displayed or not."
8236   (let ((articles nil)
8237         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8238         (case-fold-search t))
8239     (dolist (header gnus-newsgroup-headers)
8240       (when (string-match regexp (funcall func header))
8241         (push (mail-header-number header) articles)))
8242     (nreverse articles)))
8243
8244 (defun gnus-summary-find-matching (header regexp &optional backward unread
8245                                           not-case-fold not-matching)
8246   "Return a list of all articles that match REGEXP on HEADER.
8247 The search stars on the current article and goes forwards unless
8248 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8249 If UNREAD is non-nil, only unread articles will
8250 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8251 in the comparisons. If NOT-MATCHING, return a list of all articles that
8252 not match REGEXP on HEADER."
8253   (let ((case-fold-search (not not-case-fold))
8254         articles d func)
8255     (if (consp header)
8256         (if (eq (car header) 'extra)
8257             (setq func
8258                   `(lambda (h)
8259                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8260                          "")))
8261           (error "%s is an invalid header" header))
8262       (unless (fboundp (intern (concat "mail-header-" header)))
8263         (error "%s is not a valid header" header))
8264       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8265     (dolist (d (if (eq backward 'all)
8266                    gnus-newsgroup-data
8267                  (gnus-data-find-list
8268                   (gnus-summary-article-number)
8269                   (gnus-data-list backward))))
8270       (when (and (or (not unread)       ; We want all articles...
8271                      (gnus-data-unread-p d)) ; Or just unreads.
8272                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8273                  (if not-matching
8274                      (not (string-match
8275                            regexp
8276                            (funcall func (gnus-data-header d))))
8277                    (string-match regexp
8278                                  (funcall func (gnus-data-header d)))))
8279         (push (gnus-data-number d) articles))) ; Success!
8280     (nreverse articles)))
8281
8282 (defun gnus-summary-execute-command (header regexp command &optional backward)
8283   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8284 If HEADER is an empty string (or nil), the match is done on the entire
8285 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8286   (interactive
8287    (list (let ((completion-ignore-case t))
8288            (completing-read
8289             "Header name: "
8290             (mapcar (lambda (header) (list (format "%s" header)))
8291                     (append
8292                      '("Number" "Subject" "From" "Lines" "Date"
8293                        "Message-ID" "Xref" "References" "Body")
8294                      gnus-extra-headers))
8295             nil 'require-match))
8296          (read-string "Regexp: ")
8297          (read-key-sequence "Command: ")
8298          current-prefix-arg))
8299   (when (equal header "Body")
8300     (setq header ""))
8301   ;; Hidden thread subtrees must be searched as well.
8302   (gnus-summary-show-all-threads)
8303   ;; We don't want to change current point nor window configuration.
8304   (save-excursion
8305     (save-window-excursion
8306       (let (gnus-visual
8307             gnus-treat-strip-trailing-blank-lines
8308             gnus-treat-strip-leading-blank-lines
8309             gnus-treat-strip-multiple-blank-lines
8310             gnus-treat-hide-boring-headers
8311             gnus-treat-fold-newsgroups
8312             gnus-article-prepare-hook)
8313         (gnus-message 6 "Executing %s..." (key-description command))
8314         ;; We'd like to execute COMMAND interactively so as to give arguments.
8315         (gnus-execute header regexp
8316                       `(call-interactively ',(key-binding command))
8317                       backward)
8318         (gnus-message 6 "Executing %s...done" (key-description command))))))
8319
8320 (defun gnus-summary-beginning-of-article ()
8321   "Scroll the article back to the beginning."
8322   (interactive)
8323   (gnus-summary-select-article)
8324   (gnus-configure-windows 'article)
8325   (gnus-eval-in-buffer-window gnus-article-buffer
8326     (widen)
8327     (goto-char (point-min))
8328     (when gnus-page-broken
8329       (gnus-narrow-to-page))))
8330
8331 (defun gnus-summary-end-of-article ()
8332   "Scroll to the end of the article."
8333   (interactive)
8334   (gnus-summary-select-article)
8335   (gnus-configure-windows 'article)
8336   (gnus-eval-in-buffer-window gnus-article-buffer
8337     (widen)
8338     (goto-char (point-max))
8339     (recenter -3)
8340     (when gnus-page-broken
8341       (gnus-narrow-to-page))))
8342
8343 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8344   "Truncate to LEN and quote all \"(\"'s in STRING."
8345   (gnus-replace-in-string (if (and len (> (length string) len))
8346                               (substring string 0 len)
8347                             string)
8348                           "[()]" "\\\\\\&"))
8349
8350 (defun gnus-summary-print-article (&optional filename n)
8351   "Generate and print a PostScript image of the N next (mail) articles.
8352
8353 If N is negative, print the N previous articles.  If N is nil and articles
8354 have been marked with the process mark, print these instead.
8355
8356 If the optional first argument FILENAME is nil, send the image to the
8357 printer.  If FILENAME is a string, save the PostScript image in a file with
8358 that name.  If FILENAME is a number, prompt the user for the name of the file
8359 to save in."
8360   (interactive (list (ps-print-preprint current-prefix-arg)))
8361   (dolist (article (gnus-summary-work-articles n))
8362     (gnus-summary-select-article nil nil 'pseudo article)
8363     (gnus-eval-in-buffer-window gnus-article-buffer
8364       (gnus-print-buffer))
8365     (gnus-summary-remove-process-mark article))
8366   (ps-despool filename))
8367
8368 (defun gnus-print-buffer ()
8369   (let ((buffer (generate-new-buffer " *print*")))
8370     (unwind-protect
8371         (progn
8372           (copy-to-buffer buffer (point-min) (point-max))
8373           (set-buffer buffer)
8374           (gnus-article-delete-invisible-text)
8375           (gnus-remove-text-with-property 'gnus-decoration)
8376           (when (gnus-visual-p 'article-highlight 'highlight)
8377             ;; Copy-to-buffer doesn't copy overlay.  So redo
8378             ;; highlight.
8379             (let ((gnus-article-buffer buffer))
8380               (gnus-article-highlight-citation t)
8381               (gnus-article-highlight-signature)))
8382           (let ((ps-left-header
8383                  (list
8384                   (concat "("
8385                           (gnus-summary-print-truncate-and-quote
8386                            (mail-header-subject gnus-current-headers)
8387                            66) ")")
8388                   (concat "("
8389                           (gnus-summary-print-truncate-and-quote
8390                            (mail-header-from gnus-current-headers)
8391                            45) ")")))
8392                 (ps-right-header
8393                  (list
8394                   "/pagenumberstring load"
8395                   (concat "("
8396                           (mail-header-date gnus-current-headers) ")"))))
8397             (gnus-run-hooks 'gnus-ps-print-hook)
8398             (save-excursion
8399               (if window-system
8400                   (ps-spool-buffer-with-faces)
8401                 (ps-spool-buffer)))))
8402       (kill-buffer buffer))))
8403
8404 (defun gnus-summary-show-article (&optional arg)
8405   "Force redisplaying of the current article.
8406 If ARG (the prefix) is a number, show the article with the charset
8407 defined in `gnus-summary-show-article-charset-alist', or the charset
8408 input.
8409 If ARG (the prefix) is non-nil and not a number, show the raw article
8410 without any article massaging functions being run.  Normally, the key strokes
8411 are `C-u g'."
8412   (interactive "P")
8413   (cond
8414    ((numberp arg)
8415     (gnus-summary-show-article t)
8416     (let ((gnus-newsgroup-charset
8417            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8418                (mm-read-coding-system
8419                 "View as charset: " ;; actually it is coding system.
8420                 (save-excursion
8421                   (set-buffer gnus-article-buffer)
8422                   (mm-detect-coding-region (point) (point-max))))))
8423           (gnus-newsgroup-ignored-charsets 'gnus-all))
8424       (gnus-summary-select-article nil 'force)
8425       (let ((deps gnus-newsgroup-dependencies)
8426             head header lines)
8427         (save-excursion
8428           (set-buffer gnus-original-article-buffer)
8429           (save-restriction
8430             (message-narrow-to-head)
8431             (setq head (buffer-string))
8432             (goto-char (point-min))
8433             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8434               (goto-char (point-max))
8435               (widen)
8436               (setq lines (1- (count-lines (point) (point-max))))))
8437           (with-temp-buffer
8438             (insert (format "211 %d Article retrieved.\n"
8439                             (cdr gnus-article-current)))
8440             (insert head)
8441             (if lines (insert (format "Lines: %d\n" lines)))
8442             (insert ".\n")
8443             (let ((nntp-server-buffer (current-buffer)))
8444               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8445         (gnus-data-set-header
8446          (gnus-data-find (cdr gnus-article-current))
8447          header)
8448         (gnus-summary-update-article-line
8449          (cdr gnus-article-current) header)
8450         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8451           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8452    ((not arg)
8453     ;; Select the article the normal way.
8454     (gnus-summary-select-article nil 'force))
8455    (t
8456     ;; We have to require this here to make sure that the following
8457     ;; dynamic binding isn't shadowed by autoloading.
8458     (require 'gnus-async)
8459     (require 'gnus-art)
8460     ;; Bind the article treatment functions to nil.
8461     (let ((gnus-have-all-headers t)
8462           gnus-article-prepare-hook
8463           gnus-article-decode-hook
8464           gnus-display-mime-function
8465           gnus-break-pages)
8466       ;; Destroy any MIME parts.
8467       (when (gnus-buffer-live-p gnus-article-buffer)
8468         (save-excursion
8469           (set-buffer gnus-article-buffer)
8470           (mm-destroy-parts gnus-article-mime-handles)
8471           ;; Set it to nil for safety reason.
8472           (setq gnus-article-mime-handle-alist nil)
8473           (setq gnus-article-mime-handles nil)))
8474       (gnus-summary-select-article nil 'force))))
8475   (gnus-summary-goto-subject gnus-current-article)
8476   (gnus-summary-position-point))
8477
8478 (defun gnus-summary-show-raw-article ()
8479   "Show the raw article without any article massaging functions being run."
8480   (interactive)
8481   (gnus-summary-show-article t))
8482
8483 (defun gnus-summary-verbose-headers (&optional arg)
8484   "Toggle permanent full header display.
8485 If ARG is a positive number, turn header display on.
8486 If ARG is a negative number, turn header display off."
8487   (interactive "P")
8488   (setq gnus-show-all-headers
8489         (cond ((or (not (numberp arg))
8490                    (zerop arg))
8491                (not gnus-show-all-headers))
8492               ((natnump arg)
8493                t)))
8494   (gnus-summary-show-article))
8495
8496 (defun gnus-summary-toggle-header (&optional arg)
8497   "Show the headers if they are hidden, or hide them if they are shown.
8498 If ARG is a positive number, show the entire header.
8499 If ARG is a negative number, hide the unwanted header lines."
8500   (interactive "P")
8501   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8502                      (get-buffer-window gnus-article-buffer t))))
8503     (with-current-buffer gnus-article-buffer
8504       (widen)
8505       (article-narrow-to-head)
8506       (let* ((buffer-read-only nil)
8507              (inhibit-point-motion-hooks t)
8508              (hidden (if (numberp arg)
8509                          (>= arg 0)
8510                        (gnus-article-hidden-text-p 'headers)))
8511              s e)
8512         (delete-region (point-min) (point-max))
8513         (with-current-buffer gnus-original-article-buffer
8514           (goto-char (setq s (point-min)))
8515           (setq e (if (search-forward "\n\n" nil t)
8516                       (1- (point))
8517                     (point-max))))
8518         (insert-buffer-substring gnus-original-article-buffer s e)
8519         (article-decode-encoded-words)
8520         (if hidden
8521             (let ((gnus-treat-hide-headers nil)
8522                   (gnus-treat-hide-boring-headers nil))
8523               (gnus-delete-wash-type 'headers)
8524               (gnus-treat-article 'head))
8525           (gnus-treat-article 'head))
8526         (widen)
8527         (if window
8528             (set-window-start window (goto-char (point-min))))
8529         (setq gnus-page-broken
8530               (when gnus-break-pages
8531                 (gnus-narrow-to-page)
8532                 t))
8533         (gnus-set-mode-line 'article)))))
8534
8535 (defun gnus-summary-show-all-headers ()
8536   "Make all header lines visible."
8537   (interactive)
8538   (gnus-summary-toggle-header 1))
8539
8540 (defun gnus-summary-caesar-message (&optional arg)
8541   "Caesar rotate the current article by 13.
8542 The numerical prefix specifies how many places to rotate each letter
8543 forward."
8544   (interactive "P")
8545   (gnus-summary-select-article)
8546   (let ((mail-header-separator ""))
8547     (gnus-eval-in-buffer-window gnus-article-buffer
8548       (save-restriction
8549         (widen)
8550         (let ((start (window-start))
8551               buffer-read-only)
8552           (message-caesar-buffer-body arg)
8553           (set-window-start (get-buffer-window (current-buffer)) start))))))
8554
8555 (autoload 'unmorse-region "morse"
8556   "Convert morse coded text in region to ordinary ASCII text."
8557   t)
8558
8559 (defun gnus-summary-morse-message (&optional arg)
8560   "Morse decode the current article."
8561   (interactive "P")
8562   (gnus-summary-select-article)
8563   (let ((mail-header-separator ""))
8564     (gnus-eval-in-buffer-window gnus-article-buffer
8565       (save-excursion
8566         (save-restriction
8567           (widen)
8568           (let ((pos (window-start))
8569                 buffer-read-only)
8570             (goto-char (point-min))
8571             (when (message-goto-body)
8572               (gnus-narrow-to-body))
8573             (goto-char (point-min))
8574             (while (re-search-forward "·" (point-max) t)
8575               (replace-match "."))
8576             (unmorse-region (point-min) (point-max))
8577             (widen)
8578             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8579
8580 (defun gnus-summary-stop-page-breaking ()
8581   "Stop page breaking in the current article."
8582   (interactive)
8583   (gnus-summary-select-article)
8584   (gnus-eval-in-buffer-window gnus-article-buffer
8585     (widen)
8586     (when (gnus-visual-p 'page-marker)
8587       (let ((buffer-read-only nil))
8588         (gnus-remove-text-with-property 'gnus-prev)
8589         (gnus-remove-text-with-property 'gnus-next))
8590       (setq gnus-page-broken nil))))
8591
8592 (defun gnus-summary-move-article (&optional n to-newsgroup
8593                                             select-method action)
8594   "Move the current article to a different newsgroup.
8595 If N is a positive number, move the N next articles.
8596 If N is a negative number, move the N previous articles.
8597 If N is nil and any articles have been marked with the process mark,
8598 move those articles instead.
8599 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8600 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8601 re-spool using this method.
8602
8603 When called interactively with TO-NEWSGROUP being nil, the value of
8604 the variable `gnus-move-split-methods' is used for finding a default
8605 for the target newsgroup.
8606
8607 For this function to work, both the current newsgroup and the
8608 newsgroup that you want to move to have to support the `request-move'
8609 and `request-accept' functions.
8610
8611 ACTION can be either `move' (the default), `crosspost' or `copy'."
8612   (interactive "P")
8613   (unless action
8614     (setq action 'move))
8615   ;; Check whether the source group supports the required functions.
8616   (cond ((and (eq action 'move)
8617               (not (gnus-check-backend-function
8618                     'request-move-article gnus-newsgroup-name)))
8619          (error "The current group does not support article moving"))
8620         ((and (eq action 'crosspost)
8621               (not (gnus-check-backend-function
8622                     'request-replace-article gnus-newsgroup-name)))
8623          (error "The current group does not support article editing")))
8624   (let ((articles (gnus-summary-work-articles n))
8625         (prefix (if (gnus-check-backend-function
8626                      'request-move-article gnus-newsgroup-name)
8627                     (gnus-group-real-prefix gnus-newsgroup-name)
8628                   ""))
8629         (names '((move "Move" "Moving")
8630                  (copy "Copy" "Copying")
8631                  (crosspost "Crosspost" "Crossposting")))
8632         (copy-buf (save-excursion
8633                     (nnheader-set-temp-buffer " *copy article*")))
8634         art-group to-method new-xref article to-groups)
8635     (unless (assq action names)
8636       (error "Unknown action %s" action))
8637     ;; Read the newsgroup name.
8638     (when (and (not to-newsgroup)
8639                (not select-method))
8640       (if (and gnus-move-split-methods
8641                (not
8642                 (and (memq gnus-current-article articles)
8643                      (gnus-buffer-live-p gnus-original-article-buffer))))
8644           ;; When `gnus-move-split-methods' is non-nil, we have to
8645           ;; select an article to give `gnus-read-move-group-name' an
8646           ;; opportunity to suggest an appropriate default.  However,
8647           ;; we needn't render or mark the article.
8648           (let ((gnus-display-mime-function nil)
8649                 (gnus-article-prepare-hook nil)
8650                 (gnus-mark-article-hook nil))
8651             (gnus-summary-select-article nil nil nil (car articles))))
8652       (setq to-newsgroup
8653             (gnus-read-move-group-name
8654              (cadr (assq action names))
8655              (symbol-value (intern (format "gnus-current-%s-group" action)))
8656              articles prefix))
8657       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8658     (setq to-method (or select-method
8659                         (gnus-server-to-method
8660                          (gnus-group-method to-newsgroup))))
8661     ;; Check the method we are to move this article to...
8662     (unless (gnus-check-backend-function
8663              'request-accept-article (car to-method))
8664       (error "%s does not support article copying" (car to-method)))
8665     (unless (gnus-check-server to-method)
8666       (error "Can't open server %s" (car to-method)))
8667     (gnus-message 6 "%s to %s: %s..."
8668                   (caddr (assq action names))
8669                   (or (car select-method) to-newsgroup) articles)
8670     (while articles
8671       (setq article (pop articles))
8672       (setq
8673        art-group
8674        (cond
8675         ;; Move the article.
8676         ((eq action 'move)
8677          ;; Remove this article from future suppression.
8678          (gnus-dup-unsuppress-article article)
8679          (gnus-request-move-article
8680           article                       ; Article to move
8681           gnus-newsgroup-name           ; From newsgroup
8682           (nth 1 (gnus-find-method-for-group
8683                   gnus-newsgroup-name)) ; Server
8684           (list 'gnus-request-accept-article
8685                 to-newsgroup (list 'quote select-method)
8686                 (not articles) t)       ; Accept form
8687           (not articles)))              ; Only save nov last time
8688         ;; Copy the article.
8689         ((eq action 'copy)
8690          (save-excursion
8691            (set-buffer copy-buf)
8692            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8693              (gnus-request-accept-article
8694               to-newsgroup select-method (not articles) t))))
8695         ;; Crosspost the article.
8696         ((eq action 'crosspost)
8697          (let ((xref (message-tokenize-header
8698                       (mail-header-xref (gnus-summary-article-header article))
8699                       " ")))
8700            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8701                                   ":" (number-to-string article)))
8702            (unless xref
8703              (setq xref (list (system-name))))
8704            (setq new-xref
8705                  (concat
8706                   (mapconcat 'identity
8707                              (delete "Xref:" (delete new-xref xref))
8708                              " ")
8709                   " " new-xref))
8710            (save-excursion
8711              (set-buffer copy-buf)
8712              ;; First put the article in the destination group.
8713              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8714              (when (consp (setq art-group
8715                                 (gnus-request-accept-article
8716                                  to-newsgroup select-method (not articles))))
8717                (setq new-xref (concat new-xref " " (car art-group)
8718                                       ":"
8719                                       (number-to-string (cdr art-group))))
8720                ;; Now we have the new Xrefs header, so we insert
8721                ;; it and replace the new article.
8722                (nnheader-replace-header "Xref" new-xref)
8723                (gnus-request-replace-article
8724                 (cdr art-group) to-newsgroup (current-buffer))
8725                art-group))))))
8726       (cond
8727        ((not art-group)
8728         (gnus-message 1 "Couldn't %s article %s: %s"
8729                       (cadr (assq action names)) article
8730                       (nnheader-get-report (car to-method))))
8731        ((eq art-group 'junk)
8732         (when (eq action 'move)
8733           (gnus-summary-mark-article article gnus-canceled-mark)
8734           (gnus-message 4 "Deleted article %s" article)))
8735        (t
8736         (let* ((pto-group (gnus-group-prefixed-name
8737                            (car art-group) to-method))
8738                (entry
8739                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8740                (info (nth 2 entry))
8741                (to-group (gnus-info-group info))
8742                to-marks)
8743           ;; Update the group that has been moved to.
8744           (when (and info
8745                      (memq action '(move copy)))
8746             (unless (member to-group to-groups)
8747               (push to-group to-groups))
8748
8749             (unless (memq article gnus-newsgroup-unreads)
8750               (push 'read to-marks)
8751               (gnus-info-set-read
8752                info (gnus-add-to-range (gnus-info-read info)
8753                                        (list (cdr art-group)))))
8754
8755             ;; See whether the article is to be put in the cache.
8756             (let ((marks gnus-article-mark-lists)
8757                   (to-article (cdr art-group)))
8758
8759               ;; Enter the article into the cache in the new group,
8760               ;; if that is required.
8761               (when gnus-use-cache
8762                 (gnus-cache-possibly-enter-article
8763                  to-group to-article
8764                  (memq article gnus-newsgroup-marked)
8765                  (memq article gnus-newsgroup-dormant)
8766                  (memq article gnus-newsgroup-unreads)))
8767
8768               (when gnus-preserve-marks
8769                 ;; Copy any marks over to the new group.
8770                 (when (and (equal to-group gnus-newsgroup-name)
8771                            (not (memq article gnus-newsgroup-unreads)))
8772                   ;; Mark this article as read in this group.
8773                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8774                   (setcdr (gnus-active to-group) to-article)
8775                   (setcdr gnus-newsgroup-active to-article))
8776
8777                 (while marks
8778                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8779                     (when (memq article (symbol-value
8780                                          (intern (format "gnus-newsgroup-%s"
8781                                                          (caar marks)))))
8782                       (push (cdar marks) to-marks)
8783                       ;; If the other group is the same as this group,
8784                       ;; then we have to add the mark to the list.
8785                       (when (equal to-group gnus-newsgroup-name)
8786                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8787                              (cons to-article
8788                                    (symbol-value
8789                                     (intern (format "gnus-newsgroup-%s"
8790                                                     (caar marks)))))))
8791                       ;; Copy the marks to other group.
8792                       (gnus-add-marked-articles
8793                        to-group (cdar marks) (list to-article) info)))
8794                   (setq marks (cdr marks)))
8795
8796                 (gnus-request-set-mark
8797                  to-group (list (list (list to-article) 'add to-marks))))
8798
8799               (gnus-dribble-enter
8800                (concat "(gnus-group-set-info '"
8801                        (gnus-prin1-to-string (gnus-get-info to-group))
8802                        ")"))))
8803
8804           ;; Update the Xref header in this article to point to
8805           ;; the new crossposted article we have just created.
8806           (when (eq action 'crosspost)
8807             (save-excursion
8808               (set-buffer copy-buf)
8809               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8810               (nnheader-replace-header "Xref" new-xref)
8811               (gnus-request-replace-article
8812                article gnus-newsgroup-name (current-buffer)))))
8813
8814         ;;;!!!Why is this necessary?
8815         (set-buffer gnus-summary-buffer)
8816
8817         (gnus-summary-goto-subject article)
8818         (when (eq action 'move)
8819           (gnus-summary-mark-article article gnus-canceled-mark))))
8820       (gnus-summary-remove-process-mark article))
8821     ;; Re-activate all groups that have been moved to.
8822     (save-excursion
8823       (set-buffer gnus-group-buffer)
8824       (let ((gnus-group-marked to-groups))
8825         (gnus-group-get-new-news-this-group nil t)))
8826
8827     (gnus-kill-buffer copy-buf)
8828     (gnus-summary-position-point)
8829     (gnus-set-mode-line 'summary)))
8830
8831 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8832   "Move the current article to a different newsgroup.
8833 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8834 When called interactively, if TO-NEWSGROUP is nil, use the value of
8835 the variable `gnus-move-split-methods' for finding a default target
8836 newsgroup.
8837 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8838 re-spool using this method."
8839   (interactive "P")
8840   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8841
8842 (defun gnus-summary-crosspost-article (&optional n)
8843   "Crosspost the current article to some other group."
8844   (interactive "P")
8845   (gnus-summary-move-article n nil nil 'crosspost))
8846
8847 (defcustom gnus-summary-respool-default-method nil
8848   "Default method type for respooling an article.
8849 If nil, use to the current newsgroup method."
8850   :type 'symbol
8851   :group 'gnus-summary-mail)
8852
8853 (defcustom gnus-summary-display-while-building nil
8854   "If not-nil, show and update the summary buffer as it's being built.
8855 If the value is t, update the buffer after every line is inserted.  If
8856 the value is an integer (N), update the display every N lines."
8857   :group 'gnus-thread
8858   :type '(choice (const :tag "off" nil)
8859                  number
8860                  (const :tag "frequently" t)))
8861
8862 (defun gnus-summary-respool-article (&optional n method)
8863   "Respool the current article.
8864 The article will be squeezed through the mail spooling process again,
8865 which means that it will be put in some mail newsgroup or other
8866 depending on `nnmail-split-methods'.
8867 If N is a positive number, respool the N next articles.
8868 If N is a negative number, respool the N previous articles.
8869 If N is nil and any articles have been marked with the process mark,
8870 respool those articles instead.
8871
8872 Respooling can be done both from mail groups and \"real\" newsgroups.
8873 In the former case, the articles in question will be moved from the
8874 current group into whatever groups they are destined to.  In the
8875 latter case, they will be copied into the relevant groups."
8876   (interactive
8877    (list current-prefix-arg
8878          (let* ((methods (gnus-methods-using 'respool))
8879                 (methname
8880                  (symbol-name (or gnus-summary-respool-default-method
8881                                   (car (gnus-find-method-for-group
8882                                         gnus-newsgroup-name)))))
8883                 (method
8884                  (gnus-completing-read-with-default
8885                   methname "What backend do you want to use when respooling?"
8886                   methods nil t nil 'gnus-mail-method-history))
8887                 ms)
8888            (cond
8889             ((zerop (length (setq ms (gnus-servers-using-backend
8890                                       (intern method)))))
8891              (list (intern method) ""))
8892             ((= 1 (length ms))
8893              (car ms))
8894             (t
8895              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8896                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8897                            ms-alist))))))))
8898   (unless method
8899     (error "No method given for respooling"))
8900   (if (assoc (symbol-name
8901               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8902              (gnus-methods-using 'respool))
8903       (gnus-summary-move-article n nil method)
8904     (gnus-summary-copy-article n nil method)))
8905
8906 (defun gnus-summary-import-article (file &optional edit)
8907   "Import an arbitrary file into a mail newsgroup."
8908   (interactive "fImport file: \nP")
8909   (let ((group gnus-newsgroup-name)
8910         (now (current-time))
8911         atts lines group-art)
8912     (unless (gnus-check-backend-function 'request-accept-article group)
8913       (error "%s does not support article importing" group))
8914     (or (file-readable-p file)
8915         (not (file-regular-p file))
8916         (error "Can't read %s" file))
8917     (save-excursion
8918       (set-buffer (gnus-get-buffer-create " *import file*"))
8919       (erase-buffer)
8920       (nnheader-insert-file-contents file)
8921       (goto-char (point-min))
8922       (if (nnheader-article-p)
8923           (save-restriction
8924             (goto-char (point-min))
8925             (search-forward "\n\n" nil t)
8926             (narrow-to-region (point-min) (1- (point)))
8927             (goto-char (point-min))
8928             (unless (re-search-forward "^date:" nil t)
8929               (goto-char (point-max))
8930               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8931        ;; This doesn't look like an article, so we fudge some headers.
8932         (setq atts (file-attributes file)
8933               lines (count-lines (point-min) (point-max)))
8934         (insert "From: " (read-string "From: ") "\n"
8935                 "Subject: " (read-string "Subject: ") "\n"
8936                 "Date: " (message-make-date (nth 5 atts)) "\n"
8937                 "Message-ID: " (message-make-message-id) "\n"
8938                 "Lines: " (int-to-string lines) "\n"
8939                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8940       (setq group-art (gnus-request-accept-article group nil t))
8941       (kill-buffer (current-buffer)))
8942     (setq gnus-newsgroup-active (gnus-activate-group group))
8943     (forward-line 1)
8944     (gnus-summary-goto-article (cdr group-art) nil t)
8945     (when edit
8946       (gnus-summary-edit-article))))
8947
8948 (defun gnus-summary-create-article ()
8949   "Create an article in a mail newsgroup."
8950   (interactive)
8951   (let ((group gnus-newsgroup-name)
8952         (now (current-time))
8953         group-art)
8954     (unless (gnus-check-backend-function 'request-accept-article group)
8955       (error "%s does not support article importing" group))
8956     (save-excursion
8957       (set-buffer (gnus-get-buffer-create " *import file*"))
8958       (erase-buffer)
8959       (goto-char (point-min))
8960       ;; This doesn't look like an article, so we fudge some headers.
8961       (insert "From: " (read-string "From: ") "\n"
8962               "Subject: " (read-string "Subject: ") "\n"
8963               "Date: " (message-make-date now) "\n"
8964               "Message-ID: " (message-make-message-id) "\n")
8965       (setq group-art (gnus-request-accept-article group nil t))
8966       (kill-buffer (current-buffer)))
8967     (setq gnus-newsgroup-active (gnus-activate-group group))
8968     (forward-line 1)
8969     (gnus-summary-goto-article (cdr group-art) nil t)
8970     (gnus-summary-edit-article)))
8971
8972 (defun gnus-summary-article-posted-p ()
8973   "Say whether the current (mail) article is available from news as well.
8974 This will be the case if the article has both been mailed and posted."
8975   (interactive)
8976   (let ((id (mail-header-references (gnus-summary-article-header)))
8977         (gnus-override-method (car (gnus-refer-article-methods))))
8978     (if (gnus-request-head id "")
8979         (gnus-message 2 "The current message was found on %s"
8980                       gnus-override-method)
8981       (gnus-message 2 "The current message couldn't be found on %s"
8982                     gnus-override-method)
8983       nil)))
8984
8985 (defun gnus-summary-expire-articles (&optional now)
8986   "Expire all articles that are marked as expirable in the current group."
8987   (interactive)
8988   (when (gnus-check-backend-function
8989          'request-expire-articles gnus-newsgroup-name)
8990     ;; This backend supports expiry.
8991     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8992            (expirable (if total
8993                           (progn
8994                             ;; We need to update the info for
8995                             ;; this group for `gnus-list-of-read-articles'
8996                             ;; to give us the right answer.
8997                             (gnus-run-hooks 'gnus-exit-group-hook)
8998                             (gnus-summary-update-info)
8999                             (gnus-list-of-read-articles gnus-newsgroup-name))
9000                         (setq gnus-newsgroup-expirable
9001                               (sort gnus-newsgroup-expirable '<))))
9002            (expiry-wait (if now 'immediate
9003                           (gnus-group-find-parameter
9004                            gnus-newsgroup-name 'expiry-wait)))
9005            (nnmail-expiry-target
9006             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9007                 nnmail-expiry-target))
9008            es)
9009       (when expirable
9010         ;; There are expirable articles in this group, so we run them
9011         ;; through the expiry process.
9012         (gnus-message 6 "Expiring articles...")
9013         (unless (gnus-check-group gnus-newsgroup-name)
9014           (error "Can't open server for %s" gnus-newsgroup-name))
9015         ;; The list of articles that weren't expired is returned.
9016         (save-excursion
9017           (if expiry-wait
9018               (let ((nnmail-expiry-wait-function nil)
9019                     (nnmail-expiry-wait expiry-wait))
9020                 (setq es (gnus-request-expire-articles
9021                           expirable gnus-newsgroup-name)))
9022             (setq es (gnus-request-expire-articles
9023                       expirable gnus-newsgroup-name)))
9024           (unless total
9025             (setq gnus-newsgroup-expirable es))
9026           ;; We go through the old list of expirable, and mark all
9027           ;; really expired articles as nonexistent.
9028           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9029             (let ((gnus-use-cache nil))
9030               (dolist (article expirable)
9031                 (when (and (not (memq article es))
9032                            (gnus-data-find article))
9033                   (gnus-summary-mark-article article gnus-canceled-mark))))))
9034         (gnus-message 6 "Expiring articles...done")))))
9035
9036 (defun gnus-summary-expire-articles-now ()
9037   "Expunge all expirable articles in the current group.
9038 This means that *all* articles that are marked as expirable will be
9039 deleted forever, right now."
9040   (interactive)
9041   (or gnus-expert-user
9042       (gnus-yes-or-no-p
9043        "Are you really, really, really sure you want to delete all these messages? ")
9044       (error "Phew!"))
9045   (gnus-summary-expire-articles t))
9046
9047 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9048 (defun gnus-summary-delete-article (&optional n)
9049   "Delete the N next (mail) articles.
9050 This command actually deletes articles.  This is not a marking
9051 command.  The article will disappear forever from your life, never to
9052 return.
9053 If N is negative, delete backwards.
9054 If N is nil and articles have been marked with the process mark,
9055 delete these instead."
9056   (interactive "P")
9057   (unless (gnus-check-backend-function 'request-expire-articles
9058                                        gnus-newsgroup-name)
9059     (error "The current newsgroup does not support article deletion"))
9060   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9061     (error "Couldn't open server"))
9062   ;; Compute the list of articles to delete.
9063   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9064         (nnmail-expiry-target 'delete)
9065         not-deleted)
9066     (if (and gnus-novice-user
9067              (not (gnus-yes-or-no-p
9068                    (format "Do you really want to delete %s forever? "
9069                            (if (> (length articles) 1)
9070                                (format "these %s articles" (length articles))
9071                              "this article")))))
9072         ()
9073       ;; Delete the articles.
9074       (setq not-deleted (gnus-request-expire-articles
9075                          articles gnus-newsgroup-name 'force))
9076       (while articles
9077         (gnus-summary-remove-process-mark (car articles))
9078         ;; The backend might not have been able to delete the article
9079         ;; after all.
9080         (unless (memq (car articles) not-deleted)
9081           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9082         (setq articles (cdr articles)))
9083       (when not-deleted
9084         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9085     (gnus-summary-position-point)
9086     (gnus-set-mode-line 'summary)
9087     not-deleted))
9088
9089 (defun gnus-summary-edit-article (&optional arg)
9090   "Edit the current article.
9091 This will have permanent effect only in mail groups.
9092 If ARG is nil, edit the decoded articles.
9093 If ARG is 1, edit the raw articles.
9094 If ARG is 2, edit the raw articles even in read-only groups.
9095 If ARG is 3, edit the articles with the current handles.
9096 Otherwise, allow editing of articles even in read-only
9097 groups."
9098   (interactive "P")
9099   (let (force raw current-handles)
9100     (cond
9101      ((null arg))
9102      ((eq arg 1)
9103       (setq raw t))
9104      ((eq arg 2)
9105       (setq raw t
9106             force t))
9107      ((eq arg 3)
9108       (setq current-handles
9109             (and (gnus-buffer-live-p gnus-article-buffer)
9110                  (with-current-buffer gnus-article-buffer
9111                    (prog1
9112                        gnus-article-mime-handles
9113                      (setq gnus-article-mime-handles nil))))))
9114      (t
9115       (setq force t)))
9116     (when (and raw (not force)
9117                (member gnus-newsgroup-name '("nndraft:delayed"
9118                                              "nndraft:drafts"
9119                                              "nndraft:queue")))
9120       (error "Can't edit the raw article in group %s"
9121              gnus-newsgroup-name))
9122     (save-excursion
9123       (set-buffer gnus-summary-buffer)
9124       (let ((mail-parse-charset gnus-newsgroup-charset)
9125             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9126         (gnus-set-global-variables)
9127         (when (and (not force)
9128                    (gnus-group-read-only-p))
9129           (error "The current newsgroup does not support article editing"))
9130         (gnus-summary-show-article t)
9131         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9132           (with-current-buffer gnus-article-buffer
9133             (mm-enable-multibyte)))
9134         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9135             (setq raw t))
9136         (gnus-article-edit-article
9137          (if raw 'ignore
9138            `(lambda ()
9139               (let ((mbl mml-buffer-list))
9140                 (setq mml-buffer-list nil)
9141                 (mime-to-mml ,'current-handles)
9142                 (let ((mbl1 mml-buffer-list))
9143                   (setq mml-buffer-list mbl)
9144                   (set (make-local-variable 'mml-buffer-list) mbl1))
9145                 (make-local-hook 'kill-buffer-hook)
9146                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9147          `(lambda (no-highlight)
9148             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9149                   (message-options message-options)
9150                   (message-options-set-recipient)
9151                   (mail-parse-ignored-charsets
9152                    ',gnus-newsgroup-ignored-charsets))
9153               ,(if (not raw) '(progn
9154                                 (mml-to-mime)
9155                                 (mml-destroy-buffers)
9156                                 (remove-hook 'kill-buffer-hook
9157                                              'mml-destroy-buffers t)
9158                                 (kill-local-variable 'mml-buffer-list)))
9159               (gnus-summary-edit-article-done
9160                ,(or (mail-header-references gnus-current-headers) "")
9161                ,(gnus-group-read-only-p)
9162                ,gnus-summary-buffer no-highlight))))))))
9163
9164 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9165
9166 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9167                                                  no-highlight)
9168   "Make edits to the current article permanent."
9169   (interactive)
9170   (save-excursion
9171    ;; The buffer restriction contains the entire article if it exists.
9172     (when (article-goto-body)
9173       (let ((lines (count-lines (point) (point-max)))
9174             (length (- (point-max) (point)))
9175             (case-fold-search t)
9176             (body (copy-marker (point))))
9177         (goto-char (point-min))
9178         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9179           (delete-region (match-beginning 1) (match-end 1))
9180           (insert (number-to-string length)))
9181         (goto-char (point-min))
9182         (when (re-search-forward
9183                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9184           (delete-region (match-beginning 1) (match-end 1))
9185           (insert (number-to-string length)))
9186         (goto-char (point-min))
9187         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9188           (delete-region (match-beginning 1) (match-end 1))
9189           (insert (number-to-string lines))))))
9190   ;; Replace the article.
9191   (let ((buf (current-buffer)))
9192     (with-temp-buffer
9193       (insert-buffer-substring buf)
9194
9195       (if (and (not read-only)
9196                (not (gnus-request-replace-article
9197                      (cdr gnus-article-current) (car gnus-article-current)
9198                      (current-buffer) t)))
9199           (error "Couldn't replace article")
9200         ;; Update the summary buffer.
9201         (if (and references
9202                  (equal (message-tokenize-header references " ")
9203                         (message-tokenize-header
9204                          (or (message-fetch-field "references") "") " ")))
9205             ;; We only have to update this line.
9206             (save-excursion
9207               (save-restriction
9208                 (message-narrow-to-head)
9209                 (let ((head (buffer-string))
9210                       header)
9211                   (with-temp-buffer
9212                     (insert (format "211 %d Article retrieved.\n"
9213                                     (cdr gnus-article-current)))
9214                     (insert head)
9215                     (insert ".\n")
9216                     (let ((nntp-server-buffer (current-buffer)))
9217                       (setq header (car (gnus-get-newsgroup-headers
9218                                          nil t))))
9219                     (save-excursion
9220                       (set-buffer gnus-summary-buffer)
9221                       (gnus-data-set-header
9222                        (gnus-data-find (cdr gnus-article-current))
9223                        header)
9224                       (gnus-summary-update-article-line
9225                        (cdr gnus-article-current) header)
9226                       (if (gnus-summary-goto-subject
9227                            (cdr gnus-article-current) nil t)
9228                           (gnus-summary-update-secondary-mark
9229                            (cdr gnus-article-current))))))))
9230           ;; Update threads.
9231           (set-buffer (or buffer gnus-summary-buffer))
9232           (gnus-summary-update-article (cdr gnus-article-current))
9233           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9234               (gnus-summary-update-secondary-mark
9235                (cdr gnus-article-current))))
9236         ;; Prettify the article buffer again.
9237         (unless no-highlight
9238           (save-excursion
9239             (set-buffer gnus-article-buffer)
9240             ;;;!!! Fix this -- article should be rehighlighted.
9241             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9242             (set-buffer gnus-original-article-buffer)
9243             (gnus-request-article
9244              (cdr gnus-article-current)
9245              (car gnus-article-current) (current-buffer))))
9246         ;; Prettify the summary buffer line.
9247         (when (gnus-visual-p 'summary-highlight 'highlight)
9248           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9249
9250 (defun gnus-summary-edit-wash (key)
9251   "Perform editing command KEY in the article buffer."
9252   (interactive
9253    (list
9254     (progn
9255       (message "%s" (concat (this-command-keys) "- "))
9256       (read-char))))
9257   (message "")
9258   (gnus-summary-edit-article)
9259   (execute-kbd-macro (concat (this-command-keys) key))
9260   (gnus-article-edit-done))
9261
9262 ;;; Respooling
9263
9264 (defun gnus-summary-respool-query (&optional silent trace)
9265   "Query where the respool algorithm would put this article."
9266   (interactive)
9267   (let (gnus-mark-article-hook)
9268     (gnus-summary-select-article)
9269     (save-excursion
9270       (set-buffer gnus-original-article-buffer)
9271       (save-restriction
9272         (message-narrow-to-head)
9273         (let ((groups (nnmail-article-group 'identity trace)))
9274           (unless silent
9275             (if groups
9276                 (message "This message would go to %s"
9277                          (mapconcat 'car groups ", "))
9278               (message "This message would go to no groups"))
9279             groups))))))
9280
9281 (defun gnus-summary-respool-trace ()
9282   "Trace where the respool algorithm would put this article.
9283 Display a buffer showing all fancy splitting patterns which matched."
9284   (interactive)
9285   (gnus-summary-respool-query nil t))
9286
9287 ;; Summary marking commands.
9288
9289 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9290   "Mark articles which has the same subject as read, and then select the next.
9291 If UNMARK is positive, remove any kind of mark.
9292 If UNMARK is negative, tick articles."
9293   (interactive "P")
9294   (when unmark
9295     (setq unmark (prefix-numeric-value unmark)))
9296   (let ((count
9297          (gnus-summary-mark-same-subject
9298           (gnus-summary-article-subject) unmark)))
9299     ;; Select next unread article.  If auto-select-same mode, should
9300     ;; select the first unread article.
9301     (gnus-summary-next-article t (and gnus-auto-select-same
9302                                       (gnus-summary-article-subject)))
9303     (gnus-message 7 "%d article%s marked as %s"
9304                   count (if (= count 1) " is" "s are")
9305                   (if unmark "unread" "read"))))
9306
9307 (defun gnus-summary-kill-same-subject (&optional unmark)
9308   "Mark articles which has the same subject as read.
9309 If UNMARK is positive, remove any kind of mark.
9310 If UNMARK is negative, tick articles."
9311   (interactive "P")
9312   (when unmark
9313     (setq unmark (prefix-numeric-value unmark)))
9314   (let ((count
9315          (gnus-summary-mark-same-subject
9316           (gnus-summary-article-subject) unmark)))
9317     ;; If marked as read, go to next unread subject.
9318     (when (null unmark)
9319       ;; Go to next unread subject.
9320       (gnus-summary-next-subject 1 t))
9321     (gnus-message 7 "%d articles are marked as %s"
9322                   count (if unmark "unread" "read"))))
9323
9324 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9325   "Mark articles with same SUBJECT as read, and return marked number.
9326 If optional argument UNMARK is positive, remove any kinds of marks.
9327 If optional argument UNMARK is negative, mark articles as unread instead."
9328   (let ((count 1))
9329     (save-excursion
9330       (cond
9331        ((null unmark)                   ; Mark as read.
9332         (while (and
9333                 (progn
9334                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9335                   (gnus-summary-show-thread) t)
9336                 (gnus-summary-find-subject subject))
9337           (setq count (1+ count))))
9338        ((> unmark 0)                    ; Tick.
9339         (while (and
9340                 (progn
9341                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9342                   (gnus-summary-show-thread) t)
9343                 (gnus-summary-find-subject subject))
9344           (setq count (1+ count))))
9345        (t                               ; Mark as unread.
9346         (while (and
9347                 (progn
9348                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9349                   (gnus-summary-show-thread) t)
9350                 (gnus-summary-find-subject subject))
9351           (setq count (1+ count)))))
9352       (gnus-set-mode-line 'summary)
9353       ;; Return the number of marked articles.
9354       count)))
9355
9356 (defun gnus-summary-mark-as-processable (n &optional unmark)
9357   "Set the process mark on the next N articles.
9358 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9359 the process mark instead.  The difference between N and the actual
9360 number of articles marked is returned."
9361   (interactive "P")
9362   (if (and (null n) (gnus-region-active-p))
9363       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9364     (setq n (prefix-numeric-value n))
9365     (let ((backward (< n 0))
9366           (n (abs n)))
9367       (while (and
9368               (> n 0)
9369               (if unmark
9370                   (gnus-summary-remove-process-mark
9371                    (gnus-summary-article-number))
9372                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9373               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9374         (setq n (1- n)))
9375       (when (/= 0 n)
9376         (gnus-message 7 "No more articles"))
9377       (gnus-summary-recenter)
9378       (gnus-summary-position-point)
9379       n)))
9380
9381 (defun gnus-summary-unmark-as-processable (n)
9382   "Remove the process mark from the next N articles.
9383 If N is negative, unmark backward instead.  The difference between N and
9384 the actual number of articles unmarked is returned."
9385   (interactive "P")
9386   (gnus-summary-mark-as-processable n t))
9387
9388 (defun gnus-summary-unmark-all-processable ()
9389   "Remove the process mark from all articles."
9390   (interactive)
9391   (save-excursion
9392     (while gnus-newsgroup-processable
9393       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9394   (gnus-summary-position-point))
9395
9396 (defun gnus-summary-add-mark (article type)
9397   "Mark ARTICLE with a mark of TYPE."
9398   (let ((vtype (car (assq type gnus-article-mark-lists)))
9399         var)
9400     (if (not vtype)
9401         (error "No such mark type: %s" type)
9402       (setq var (intern (format "gnus-newsgroup-%s" type)))
9403       (set var (cons article (symbol-value var)))
9404       (if (memq type '(processable cached replied forwarded recent saved))
9405           (gnus-summary-update-secondary-mark article)
9406         ;;; !!! This is bogus.  We should find out what primary
9407         ;;; !!! mark we want to set.
9408         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9409
9410 (defun gnus-summary-mark-as-expirable (n)
9411   "Mark N articles forward as expirable.
9412 If N is negative, mark backward instead.  The difference between N and
9413 the actual number of articles marked is returned."
9414   (interactive "p")
9415   (gnus-summary-mark-forward n gnus-expirable-mark))
9416
9417 (defun gnus-summary-mark-as-spam (n)
9418   "Mark N articles forward as spam.
9419 If N is negative, mark backward instead.  The difference between N and
9420 the actual number of articles marked is returned."
9421   (interactive "p")
9422   (gnus-summary-mark-forward n gnus-spam-mark))
9423
9424 (defun gnus-summary-mark-article-as-replied (article)
9425   "Mark ARTICLE as replied to and update the summary line.
9426 ARTICLE can also be a list of articles."
9427   (interactive (list (gnus-summary-article-number)))
9428   (let ((articles (if (listp article) article (list article))))
9429     (dolist (article articles)
9430       (push article gnus-newsgroup-replied)
9431       (let ((buffer-read-only nil))
9432         (when (gnus-summary-goto-subject article nil t)
9433           (gnus-summary-update-secondary-mark article))))))
9434
9435 (defun gnus-summary-mark-article-as-forwarded (article)
9436   "Mark ARTICLE as forwarded and update the summary line.
9437 ARTICLE can also be a list of articles."
9438   (let ((articles (if (listp article) article (list article))))
9439     (dolist (article articles)
9440       (push article gnus-newsgroup-forwarded)
9441       (let ((buffer-read-only nil))
9442         (when (gnus-summary-goto-subject article nil t)
9443           (gnus-summary-update-secondary-mark article))))))
9444
9445 (defun gnus-summary-set-bookmark (article)
9446   "Set a bookmark in current article."
9447   (interactive (list (gnus-summary-article-number)))
9448   (when (or (not (get-buffer gnus-article-buffer))
9449             (not gnus-current-article)
9450             (not gnus-article-current)
9451             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9452     (error "No current article selected"))
9453   ;; Remove old bookmark, if one exists.
9454   (gnus-pull article gnus-newsgroup-bookmarks)
9455   ;; Set the new bookmark, which is on the form
9456   ;; (article-number . line-number-in-body).
9457   (push
9458    (cons article
9459          (save-excursion
9460            (set-buffer gnus-article-buffer)
9461            (count-lines
9462             (min (point)
9463                  (save-excursion
9464                    (article-goto-body)
9465                    (point)))
9466             (point))))
9467    gnus-newsgroup-bookmarks)
9468   (gnus-message 6 "A bookmark has been added to the current article."))
9469
9470 (defun gnus-summary-remove-bookmark (article)
9471   "Remove the bookmark from the current article."
9472   (interactive (list (gnus-summary-article-number)))
9473   ;; Remove old bookmark, if one exists.
9474   (if (not (assq article gnus-newsgroup-bookmarks))
9475       (gnus-message 6 "No bookmark in current article.")
9476     (gnus-pull article gnus-newsgroup-bookmarks)
9477     (gnus-message 6 "Removed bookmark.")))
9478
9479 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9480 (defun gnus-summary-mark-as-dormant (n)
9481   "Mark N articles forward as dormant.
9482 If N is negative, mark backward instead.  The difference between N and
9483 the actual number of articles marked is returned."
9484   (interactive "p")
9485   (gnus-summary-mark-forward n gnus-dormant-mark))
9486
9487 (defun gnus-summary-set-process-mark (article)
9488   "Set the process mark on ARTICLE and update the summary line."
9489   (setq gnus-newsgroup-processable
9490         (cons article
9491               (delq article gnus-newsgroup-processable)))
9492   (when (gnus-summary-goto-subject article)
9493     (gnus-summary-show-thread)
9494     (gnus-summary-goto-subject article)
9495     (gnus-summary-update-secondary-mark article)))
9496
9497 (defun gnus-summary-remove-process-mark (article)
9498   "Remove the process mark from ARTICLE and update the summary line."
9499   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9500   (when (gnus-summary-goto-subject article)
9501     (gnus-summary-show-thread)
9502     (gnus-summary-goto-subject article)
9503     (gnus-summary-update-secondary-mark article)))
9504
9505 (defun gnus-summary-set-saved-mark (article)
9506   "Set the process mark on ARTICLE and update the summary line."
9507   (push article gnus-newsgroup-saved)
9508   (when (gnus-summary-goto-subject article)
9509     (gnus-summary-update-secondary-mark article)))
9510
9511 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9512   "Mark N articles as read forwards.
9513 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9514 The difference between N and the actual number of articles marked is
9515 returned.
9516 If NO-EXPIRE, auto-expiry will be inhibited."
9517   (interactive "p")
9518   (gnus-summary-show-thread)
9519   (let ((backward (< n 0))
9520         (gnus-summary-goto-unread
9521          (and gnus-summary-goto-unread
9522               (not (eq gnus-summary-goto-unread 'never))
9523               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9524                                     gnus-ticked-mark gnus-dormant-mark)))))
9525         (n (abs n))
9526         (mark (or mark gnus-del-mark)))
9527     (while (and (> n 0)
9528                 (gnus-summary-mark-article nil mark no-expire)
9529                 (zerop (gnus-summary-next-subject
9530                         (if backward -1 1)
9531                         (and gnus-summary-goto-unread
9532                              (not (eq gnus-summary-goto-unread 'never)))
9533                         t)))
9534       (setq n (1- n)))
9535     (when (/= 0 n)
9536       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9537     (gnus-summary-recenter)
9538     (gnus-summary-position-point)
9539     (gnus-set-mode-line 'summary)
9540     n))
9541
9542 (defun gnus-summary-mark-article-as-read (mark)
9543   "Mark the current article quickly as read with MARK."
9544   (let ((article (gnus-summary-article-number)))
9545     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9546     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9547     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9548     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9549     (push (cons article mark) gnus-newsgroup-reads)
9550     ;; Possibly remove from cache, if that is used.
9551     (when gnus-use-cache
9552       (gnus-cache-enter-remove-article article))
9553     ;; Allow the backend to change the mark.
9554     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9555     ;; Check for auto-expiry.
9556     (when (and gnus-newsgroup-auto-expire
9557                (memq mark gnus-auto-expirable-marks))
9558       (setq mark gnus-expirable-mark)
9559       ;; Let the backend know about the mark change.
9560       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9561       (push article gnus-newsgroup-expirable))
9562     ;; Set the mark in the buffer.
9563     (gnus-summary-update-mark mark 'unread)
9564     t))
9565
9566 (defun gnus-summary-mark-article-as-unread (mark)
9567   "Mark the current article quickly as unread with MARK."
9568   (let* ((article (gnus-summary-article-number))
9569          (old-mark (gnus-summary-article-mark article)))
9570     ;; Allow the backend to change the mark.
9571     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9572     (if (eq mark old-mark)
9573         t
9574       (if (<= article 0)
9575           (progn
9576             (gnus-error 1 "Can't mark negative article numbers")
9577             nil)
9578         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9579         (setq gnus-newsgroup-spam-marked
9580               (delq article gnus-newsgroup-spam-marked))
9581         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9582         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9583         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9584         (cond ((= mark gnus-ticked-mark)
9585                (setq gnus-newsgroup-marked
9586                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9587                                               article)))
9588               ((= mark gnus-spam-mark)
9589                (setq gnus-newsgroup-spam-marked
9590                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9591                                               article)))
9592               ((= mark gnus-dormant-mark)
9593                (setq gnus-newsgroup-dormant
9594                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9595                                               article)))
9596               (t
9597                (setq gnus-newsgroup-unreads
9598                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9599                                               article))))
9600         (gnus-pull article gnus-newsgroup-reads)
9601
9602         ;; See whether the article is to be put in the cache.
9603         (and gnus-use-cache
9604              (vectorp (gnus-summary-article-header article))
9605              (save-excursion
9606                (gnus-cache-possibly-enter-article
9607                 gnus-newsgroup-name article
9608                 (= mark gnus-ticked-mark)
9609                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9610
9611         ;; Fix the mark.
9612         (gnus-summary-update-mark mark 'unread)
9613         t))))
9614
9615 (defun gnus-summary-mark-article (&optional article mark no-expire)
9616   "Mark ARTICLE with MARK.  MARK can be any character.
9617 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9618 `??' (dormant) and `?E' (expirable).
9619 If MARK is nil, then the default character `?r' is used.
9620 If ARTICLE is nil, then the article on the current line will be
9621 marked.
9622 If NO-EXPIRE, auto-expiry will be inhibited."
9623   ;; The mark might be a string.
9624   (when (stringp mark)
9625     (setq mark (aref mark 0)))
9626   ;; If no mark is given, then we check auto-expiring.
9627   (when (null mark)
9628     (setq mark gnus-del-mark))
9629   (when (and (not no-expire)
9630              gnus-newsgroup-auto-expire
9631              (memq mark gnus-auto-expirable-marks))
9632     (setq mark gnus-expirable-mark))
9633   (let ((article (or article (gnus-summary-article-number)))
9634         (old-mark (gnus-summary-article-mark article)))
9635     ;; Allow the backend to change the mark.
9636     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9637     (if (eq mark old-mark)
9638         t
9639       (unless article
9640         (error "No article on current line"))
9641       (if (not (if (or (= mark gnus-unread-mark)
9642                        (= mark gnus-ticked-mark)
9643                        (= mark gnus-spam-mark)
9644                        (= mark gnus-dormant-mark))
9645                    (gnus-mark-article-as-unread article mark)
9646                  (gnus-mark-article-as-read article mark)))
9647           t
9648         ;; See whether the article is to be put in the cache.
9649         (and gnus-use-cache
9650              (not (= mark gnus-canceled-mark))
9651              (vectorp (gnus-summary-article-header article))
9652              (save-excursion
9653                (gnus-cache-possibly-enter-article
9654                 gnus-newsgroup-name article
9655                 (= mark gnus-ticked-mark)
9656                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9657
9658         (when (gnus-summary-goto-subject article nil t)
9659           (let ((buffer-read-only nil))
9660             (gnus-summary-show-thread)
9661             ;; Fix the mark.
9662             (gnus-summary-update-mark mark 'unread)
9663             t))))))
9664
9665 (defun gnus-summary-update-secondary-mark (article)
9666   "Update the secondary (read, process, cache) mark."
9667   (gnus-summary-update-mark
9668    (cond ((memq article gnus-newsgroup-processable)
9669           gnus-process-mark)
9670          ((memq article gnus-newsgroup-cached)
9671           gnus-cached-mark)
9672          ((memq article gnus-newsgroup-replied)
9673           gnus-replied-mark)
9674          ((memq article gnus-newsgroup-forwarded)
9675           gnus-forwarded-mark)
9676          ((memq article gnus-newsgroup-saved)
9677           gnus-saved-mark)
9678          ((memq article gnus-newsgroup-recent)
9679           gnus-recent-mark)
9680          ((memq article gnus-newsgroup-unseen)
9681           gnus-unseen-mark)
9682          (t gnus-no-mark))
9683    'replied)
9684   (when (gnus-visual-p 'summary-highlight 'highlight)
9685     (gnus-run-hooks 'gnus-summary-update-hook))
9686   t)
9687
9688 (defun gnus-summary-update-download-mark (article)
9689   "Update the secondary (read, process, cache) mark."
9690   (gnus-summary-update-mark
9691    (cond ((memq article gnus-newsgroup-undownloaded) 
9692           gnus-undownloaded-mark)
9693          (gnus-newsgroup-agentized
9694           gnus-downloaded-mark)
9695          (t
9696           gnus-no-mark))
9697    'download)
9698   (gnus-summary-update-line t)
9699   t)
9700
9701 (defun gnus-summary-update-mark (mark type)
9702   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9703         (buffer-read-only nil))
9704     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9705     (when forward
9706       (when (looking-at "\r")
9707         (incf forward))
9708       (when (<= (+ forward (point)) (point-max))
9709         ;; Go to the right position on the line.
9710         (goto-char (+ forward (point)))
9711         ;; Replace the old mark with the new mark.
9712         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9713         ;; Optionally update the marks by some user rule.
9714         (when (eq type 'unread)
9715           (gnus-data-set-mark
9716            (gnus-data-find (gnus-summary-article-number)) mark)
9717           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9718
9719 (defun gnus-mark-article-as-read (article &optional mark)
9720   "Enter ARTICLE in the pertinent lists and remove it from others."
9721   ;; Make the article expirable.
9722   (let ((mark (or mark gnus-del-mark)))
9723     (setq gnus-newsgroup-expirable
9724           (if (= mark gnus-expirable-mark)
9725               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9726             (delq article gnus-newsgroup-expirable)))
9727     ;; Remove from unread and marked lists.
9728     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9729     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9730     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9731     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9732     (push (cons article mark) gnus-newsgroup-reads)
9733     ;; Possibly remove from cache, if that is used.
9734     (when gnus-use-cache
9735       (gnus-cache-enter-remove-article article))
9736     t))
9737
9738 (defun gnus-mark-article-as-unread (article &optional mark)
9739   "Enter ARTICLE in the pertinent lists and remove it from others."
9740   (let ((mark (or mark gnus-ticked-mark)))
9741     (if (<= article 0)
9742         (progn
9743           (gnus-error 1 "Can't mark negative article numbers")
9744           nil)
9745       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9746             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9747             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9748             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9749             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9750
9751       ;; Unsuppress duplicates?
9752       (when gnus-suppress-duplicates
9753         (gnus-dup-unsuppress-article article))
9754
9755       (cond ((= mark gnus-ticked-mark)
9756              (setq gnus-newsgroup-marked
9757                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9758             ((= mark gnus-spam-mark)
9759              (setq gnus-newsgroup-spam-marked
9760                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9761                                             article)))
9762             ((= mark gnus-dormant-mark)
9763              (setq gnus-newsgroup-dormant
9764                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9765             (t
9766              (setq gnus-newsgroup-unreads
9767                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9768       (gnus-pull article gnus-newsgroup-reads)
9769       t)))
9770
9771 (defalias 'gnus-summary-mark-as-unread-forward
9772   'gnus-summary-tick-article-forward)
9773 (make-obsolete 'gnus-summary-mark-as-unread-forward
9774                'gnus-summary-tick-article-forward)
9775 (defun gnus-summary-tick-article-forward (n)
9776   "Tick N articles forwards.
9777 If N is negative, tick backwards instead.
9778 The difference between N and the number of articles ticked is returned."
9779   (interactive "p")
9780   (gnus-summary-mark-forward n gnus-ticked-mark))
9781
9782 (defalias 'gnus-summary-mark-as-unread-backward
9783   'gnus-summary-tick-article-backward)
9784 (make-obsolete 'gnus-summary-mark-as-unread-backward
9785                'gnus-summary-tick-article-backward)
9786 (defun gnus-summary-tick-article-backward (n)
9787   "Tick N articles backwards.
9788 The difference between N and the number of articles ticked is returned."
9789   (interactive "p")
9790   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9791
9792 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9793 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9794 (defun gnus-summary-tick-article (&optional article clear-mark)
9795   "Mark current article as unread.
9796 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9797 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9798   (interactive)
9799   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9800                                        gnus-ticked-mark)))
9801
9802 (defun gnus-summary-mark-as-read-forward (n)
9803   "Mark N articles as read forwards.
9804 If N is negative, mark backwards instead.
9805 The difference between N and the actual number of articles marked is
9806 returned."
9807   (interactive "p")
9808   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9809
9810 (defun gnus-summary-mark-as-read-backward (n)
9811   "Mark the N articles as read backwards.
9812 The difference between N and the actual number of articles marked is
9813 returned."
9814   (interactive "p")
9815   (gnus-summary-mark-forward
9816    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9817
9818 (defun gnus-summary-mark-as-read (&optional article mark)
9819   "Mark current article as read.
9820 ARTICLE specifies the article to be marked as read.
9821 MARK specifies a string to be inserted at the beginning of the line."
9822   (gnus-summary-mark-article article mark))
9823
9824 (defun gnus-summary-clear-mark-forward (n)
9825   "Clear marks from N articles forward.
9826 If N is negative, clear backward instead.
9827 The difference between N and the number of marks cleared is returned."
9828   (interactive "p")
9829   (gnus-summary-mark-forward n gnus-unread-mark))
9830
9831 (defun gnus-summary-clear-mark-backward (n)
9832   "Clear marks from N articles backward.
9833 The difference between N and the number of marks cleared is returned."
9834   (interactive "p")
9835   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9836
9837 (defun gnus-summary-mark-unread-as-read ()
9838   "Intended to be used by `gnus-summary-mark-article-hook'."
9839   (when (memq gnus-current-article gnus-newsgroup-unreads)
9840     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9841
9842 (defun gnus-summary-mark-read-and-unread-as-read ()
9843   "Intended to be used by `gnus-summary-mark-article-hook'."
9844   (let ((mark (gnus-summary-article-mark)))
9845     (when (or (gnus-unread-mark-p mark)
9846               (gnus-read-mark-p mark))
9847       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9848
9849 (defun gnus-summary-mark-unread-as-ticked ()
9850   "Intended to be used by `gnus-summary-mark-article-hook'."
9851   (when (memq gnus-current-article gnus-newsgroup-unreads)
9852     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9853
9854 (defun gnus-summary-mark-region-as-read (point mark all)
9855   "Mark all unread articles between point and mark as read.
9856 If given a prefix, mark all articles between point and mark as read,
9857 even ticked and dormant ones."
9858   (interactive "r\nP")
9859   (save-excursion
9860     (let (article)
9861       (goto-char point)
9862       (beginning-of-line)
9863       (while (and
9864               (< (point) mark)
9865               (progn
9866                 (when (or all
9867                           (memq (setq article (gnus-summary-article-number))
9868                                 gnus-newsgroup-unreads))
9869                   (gnus-summary-mark-article article gnus-del-mark))
9870                 t)
9871               (gnus-summary-find-next))))))
9872
9873 (defun gnus-summary-mark-below (score mark)
9874   "Mark articles with score less than SCORE with MARK."
9875   (interactive "P\ncMark: ")
9876   (setq score (if score
9877                   (prefix-numeric-value score)
9878                 (or gnus-summary-default-score 0)))
9879   (save-excursion
9880     (set-buffer gnus-summary-buffer)
9881     (goto-char (point-min))
9882     (while
9883         (progn
9884           (and (< (gnus-summary-article-score) score)
9885                (gnus-summary-mark-article nil mark))
9886           (gnus-summary-find-next)))))
9887
9888 (defun gnus-summary-kill-below (&optional score)
9889   "Mark articles with score below SCORE as read."
9890   (interactive "P")
9891   (gnus-summary-mark-below score gnus-killed-mark))
9892
9893 (defun gnus-summary-clear-above (&optional score)
9894   "Clear all marks from articles with score above SCORE."
9895   (interactive "P")
9896   (gnus-summary-mark-above score gnus-unread-mark))
9897
9898 (defun gnus-summary-tick-above (&optional score)
9899   "Tick all articles with score above SCORE."
9900   (interactive "P")
9901   (gnus-summary-mark-above score gnus-ticked-mark))
9902
9903 (defun gnus-summary-mark-above (score mark)
9904   "Mark articles with score over SCORE with MARK."
9905   (interactive "P\ncMark: ")
9906   (setq score (if score
9907                   (prefix-numeric-value score)
9908                 (or gnus-summary-default-score 0)))
9909   (save-excursion
9910     (set-buffer gnus-summary-buffer)
9911     (goto-char (point-min))
9912     (while (and (progn
9913                   (when (> (gnus-summary-article-score) score)
9914                     (gnus-summary-mark-article nil mark))
9915                   t)
9916                 (gnus-summary-find-next)))))
9917
9918 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9919 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9920 (defun gnus-summary-limit-include-expunged (&optional no-error)
9921   "Display all the hidden articles that were expunged for low scores."
9922   (interactive)
9923   (let ((buffer-read-only nil))
9924     (let ((scored gnus-newsgroup-scored)
9925           headers h)
9926       (while scored
9927         (unless (gnus-summary-article-header (caar scored))
9928           (and (setq h (gnus-number-to-header (caar scored)))
9929                (< (cdar scored) gnus-summary-expunge-below)
9930                (push h headers)))
9931         (setq scored (cdr scored)))
9932       (if (not headers)
9933           (when (not no-error)
9934             (error "No expunged articles hidden"))
9935         (goto-char (point-min))
9936         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9937         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9938         (mapcar (lambda (x) (push (mail-header-number x)
9939                                   gnus-newsgroup-limit))
9940                 headers)
9941         (gnus-summary-prepare-unthreaded (nreverse headers))
9942         (goto-char (point-min))
9943         (gnus-summary-position-point)
9944         t))))
9945
9946 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9947   "Mark all unread articles in this newsgroup as read.
9948 If prefix argument ALL is non-nil, ticked and dormant articles will
9949 also be marked as read.
9950 If QUIETLY is non-nil, no questions will be asked.
9951 If TO-HERE is non-nil, it should be a point in the buffer.  All
9952 articles before (after, if REVERSE is set) this point will be marked as read.
9953 Note that this function will only catch up the unread article
9954 in the current summary buffer limitation.
9955 The number of articles marked as read is returned."
9956   (interactive "P")
9957   (prog1
9958       (save-excursion
9959         (when (or quietly
9960                   (not gnus-interactive-catchup) ;Without confirmation?
9961                   gnus-expert-user
9962                   (gnus-y-or-n-p
9963                    (if all
9964                        "Mark absolutely all articles as read? "
9965                      "Mark all unread articles as read? ")))
9966           (if (and not-mark
9967                    (not gnus-newsgroup-adaptive)
9968                    (not gnus-newsgroup-auto-expire)
9969                    (not gnus-suppress-duplicates)
9970                    (or (not gnus-use-cache)
9971                        (eq gnus-use-cache 'passive)))
9972               (progn
9973                 (when all
9974                   (setq gnus-newsgroup-marked nil
9975                         gnus-newsgroup-spam-marked nil
9976                         gnus-newsgroup-dormant nil))
9977                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9978             ;; We actually mark all articles as canceled, which we
9979             ;; have to do when using auto-expiry or adaptive scoring.
9980             (gnus-summary-show-all-threads)
9981             (if (and to-here reverse)
9982                 (progn
9983                   (goto-char to-here)
9984                   (while (and
9985                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9986                           (gnus-summary-find-next (not all) nil nil t))))
9987               (when (gnus-summary-first-subject (not all) t)
9988                 (while (and
9989                         (if to-here (< (point) to-here) t)
9990                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9991                         (gnus-summary-find-next (not all) nil nil t)))))
9992             (gnus-set-mode-line 'summary))
9993           t))
9994     (gnus-summary-position-point)))
9995
9996 (defun gnus-summary-catchup-to-here (&optional all)
9997   "Mark all unticked articles before the current one as read.
9998 If ALL is non-nil, also mark ticked and dormant articles as read."
9999   (interactive "P")
10000   (save-excursion
10001     (gnus-save-hidden-threads
10002       (let ((beg (point)))
10003         ;; We check that there are unread articles.
10004         (when (or all (gnus-summary-find-prev))
10005           (gnus-summary-catchup all t beg)))))
10006   (gnus-summary-position-point))
10007
10008 (defun gnus-summary-catchup-from-here (&optional all)
10009   "Mark all unticked articles after the current one as read.
10010 If ALL is non-nil, also mark ticked and dormant articles as read."
10011   (interactive "P")
10012   (save-excursion
10013     (gnus-save-hidden-threads
10014       (let ((beg (point)))
10015         ;; We check that there are unread articles.
10016         (when (or all (gnus-summary-find-next))
10017           (gnus-summary-catchup all t beg nil t)))))
10018
10019   (gnus-summary-position-point))
10020 (defun gnus-summary-catchup-all (&optional quietly)
10021   "Mark all articles in this newsgroup as read.
10022 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10023 instead, which marks only unread articles as read."
10024   (interactive "P")
10025   (gnus-summary-catchup t quietly))
10026
10027 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10028   "Mark all unread articles in this group as read, then exit.
10029 If prefix argument ALL is non-nil, all articles are marked as read.
10030 If QUIETLY is non-nil, no questions will be asked."
10031   (interactive "P")
10032   (when (gnus-summary-catchup all quietly nil 'fast)
10033     ;; Select next newsgroup or exit.
10034     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10035              (eq gnus-auto-select-next 'quietly))
10036         (gnus-summary-next-group nil)
10037       (gnus-summary-exit))))
10038
10039 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10040   "Mark all articles in this newsgroup as read, and then exit.
10041 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10042 instead, which marks only unread articles as read."
10043   (interactive "P")
10044   (gnus-summary-catchup-and-exit t quietly))
10045
10046 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10047   "Mark all articles in this group as read and select the next group.
10048 If given a prefix, mark all articles, unread as well as ticked, as
10049 read."
10050   (interactive "P")
10051   (save-excursion
10052     (gnus-summary-catchup all))
10053   (gnus-summary-next-group))
10054
10055 ;;;
10056 ;;; with article
10057 ;;;
10058
10059 (defmacro gnus-with-article (article &rest forms)
10060   "Select ARTICLE and perform FORMS in the original article buffer.
10061 Then replace the article with the result."
10062   `(progn
10063      ;; We don't want the article to be marked as read.
10064      (let (gnus-mark-article-hook)
10065        (gnus-summary-select-article t t nil ,article))
10066      (set-buffer gnus-original-article-buffer)
10067      ,@forms
10068      (if (not (gnus-check-backend-function
10069                'request-replace-article (car gnus-article-current)))
10070          (gnus-message 5 "Read-only group; not replacing")
10071        (unless (gnus-request-replace-article
10072                 ,article (car gnus-article-current)
10073                 (current-buffer) t)
10074          (error "Couldn't replace article")))
10075      ;; The cache and backlog have to be flushed somewhat.
10076      (when gnus-keep-backlog
10077        (gnus-backlog-remove-article
10078         (car gnus-article-current) (cdr gnus-article-current)))
10079      (when gnus-use-cache
10080        (gnus-cache-update-article
10081         (car gnus-article-current) (cdr gnus-article-current)))))
10082
10083 (put 'gnus-with-article 'lisp-indent-function 1)
10084 (put 'gnus-with-article 'edebug-form-spec '(form body))
10085
10086 ;; Thread-based commands.
10087
10088 (defun gnus-summary-articles-in-thread (&optional article)
10089   "Return a list of all articles in the current thread.
10090 If ARTICLE is non-nil, return all articles in the thread that starts
10091 with that article."
10092   (let* ((article (or article (gnus-summary-article-number)))
10093          (data (gnus-data-find-list article))
10094          (top-level (gnus-data-level (car data)))
10095          (top-subject
10096           (cond ((null gnus-thread-operation-ignore-subject)
10097                  (gnus-simplify-subject-re
10098                   (mail-header-subject (gnus-data-header (car data)))))
10099                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10100                  (gnus-simplify-subject-fuzzy
10101                   (mail-header-subject (gnus-data-header (car data)))))
10102                 (t nil)))
10103          (end-point (save-excursion
10104                       (if (gnus-summary-go-to-next-thread)
10105                           (point) (point-max))))
10106          articles)
10107     (while (and data
10108                 (< (gnus-data-pos (car data)) end-point))
10109       (when (or (not top-subject)
10110                 (string= top-subject
10111                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10112                              (gnus-simplify-subject-fuzzy
10113                               (mail-header-subject
10114                                (gnus-data-header (car data))))
10115                            (gnus-simplify-subject-re
10116                             (mail-header-subject
10117                              (gnus-data-header (car data)))))))
10118         (push (gnus-data-number (car data)) articles))
10119       (unless (and (setq data (cdr data))
10120                    (> (gnus-data-level (car data)) top-level))
10121         (setq data nil)))
10122     ;; Return the list of articles.
10123     (nreverse articles)))
10124
10125 (defun gnus-summary-rethread-current ()
10126   "Rethread the thread the current article is part of."
10127   (interactive)
10128   (let* ((gnus-show-threads t)
10129          (article (gnus-summary-article-number))
10130          (id (mail-header-id (gnus-summary-article-header)))
10131          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10132     (unless id
10133       (error "No article on the current line"))
10134     (gnus-rebuild-thread id)
10135     (gnus-summary-goto-subject article)))
10136
10137 (defun gnus-summary-reparent-thread ()
10138   "Make the current article child of the marked (or previous) article.
10139
10140 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10141 is non-nil or the Subject: of both articles are the same."
10142   (interactive)
10143   (unless (not (gnus-group-read-only-p))
10144     (error "The current newsgroup does not support article editing"))
10145   (unless (<= (length gnus-newsgroup-processable) 1)
10146     (error "No more than one article may be marked"))
10147   (save-window-excursion
10148     (let ((gnus-article-buffer " *reparent*")
10149           (current-article (gnus-summary-article-number))
10150           ;; First grab the marked article, otherwise one line up.
10151           (parent-article (if (not (null gnus-newsgroup-processable))
10152                               (car gnus-newsgroup-processable)
10153                             (save-excursion
10154                               (if (eq (forward-line -1) 0)
10155                                   (gnus-summary-article-number)
10156                                 (error "Beginning of summary buffer"))))))
10157       (unless (not (eq current-article parent-article))
10158         (error "An article may not be self-referential"))
10159       (let ((message-id (mail-header-id
10160                          (gnus-summary-article-header parent-article))))
10161         (unless (and message-id (not (equal message-id "")))
10162           (error "No message-id in desired parent"))
10163         (gnus-with-article current-article
10164           (save-restriction
10165             (goto-char (point-min))
10166             (message-narrow-to-head)
10167             (if (re-search-forward "^References: " nil t)
10168                 (progn
10169                   (re-search-forward "^[^ \t]" nil t)
10170                   (forward-line -1)
10171                   (end-of-line)
10172                   (insert " " message-id))
10173               (insert "References: " message-id "\n"))))
10174         (set-buffer gnus-summary-buffer)
10175         (gnus-summary-unmark-all-processable)
10176         (gnus-summary-update-article current-article)
10177         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10178             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10179         (gnus-summary-rethread-current)
10180         (gnus-message 3 "Article %d is now the child of article %d"
10181                       current-article parent-article)))))
10182
10183 (defun gnus-summary-toggle-threads (&optional arg)
10184   "Toggle showing conversation threads.
10185 If ARG is positive number, turn showing conversation threads on."
10186   (interactive "P")
10187   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10188     (setq gnus-show-threads
10189           (if (null arg) (not gnus-show-threads)
10190             (> (prefix-numeric-value arg) 0)))
10191     (gnus-summary-prepare)
10192     (gnus-summary-goto-subject current)
10193     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10194     (gnus-summary-position-point)))
10195
10196 (defun gnus-summary-show-all-threads ()
10197   "Show all threads."
10198   (interactive)
10199   (save-excursion
10200     (let ((buffer-read-only nil))
10201       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10202   (gnus-summary-position-point))
10203
10204 (defun gnus-summary-show-thread ()
10205   "Show thread subtrees.
10206 Returns nil if no thread was there to be shown."
10207   (interactive)
10208   (let ((buffer-read-only nil)
10209         (orig (point))
10210         ;; first goto end then to beg, to have point at beg after let
10211         (end (progn (end-of-line) (point)))
10212         (beg (progn (beginning-of-line) (point))))
10213     (prog1
10214         ;; Any hidden lines here?
10215         (search-forward "\r" end t)
10216       (subst-char-in-region beg end ?\^M ?\n t)
10217       (goto-char orig)
10218       (gnus-summary-position-point))))
10219
10220 (defun gnus-summary-maybe-hide-threads ()
10221   "If requested, hide the threads that should be hidden."
10222   (when (and gnus-show-threads
10223              gnus-thread-hide-subtree)
10224     (gnus-summary-hide-all-threads
10225      (if (or (consp gnus-thread-hide-subtree)
10226              (gnus-functionp gnus-thread-hide-subtree))
10227          (gnus-make-predicate gnus-thread-hide-subtree)
10228        nil))))
10229
10230 ;;; Hiding predicates.
10231
10232 (defun gnus-article-unread-p (header)
10233   (memq (mail-header-number header) gnus-newsgroup-unreads))
10234
10235 (defun gnus-article-unseen-p (header)
10236   (memq (mail-header-number header) gnus-newsgroup-unseen))
10237
10238 (defun gnus-map-articles (predicate articles)
10239   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10240   (apply 'gnus-or (mapcar predicate
10241                           (mapcar 'gnus-summary-article-header articles))))
10242
10243 (defun gnus-summary-hide-all-threads (&optional predicate)
10244   "Hide all thread subtrees.
10245 If PREDICATE is supplied, threads that satisfy this predicate
10246 will not be hidden."
10247   (interactive)
10248   (save-excursion
10249     (goto-char (point-min))
10250     (let ((end nil))
10251       (while (not end)
10252         (when (or (not predicate)
10253                   (gnus-map-articles
10254                    predicate (gnus-summary-article-children)))
10255             (gnus-summary-hide-thread))
10256         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10257   (gnus-summary-position-point))
10258
10259 (defun gnus-summary-hide-thread ()
10260   "Hide thread subtrees.
10261 If PREDICATE is supplied, threads that satisfy this predicate
10262 will not be hidden.
10263 Returns nil if no threads were there to be hidden."
10264   (interactive)
10265   (let ((buffer-read-only nil)
10266         (start (point))
10267         (article (gnus-summary-article-number)))
10268     (goto-char start)
10269     ;; Go forward until either the buffer ends or the subthread
10270     ;; ends.
10271     (when (and (not (eobp))
10272                (or (zerop (gnus-summary-next-thread 1 t))
10273                    (goto-char (point-max))))
10274       (prog1
10275           (if (and (> (point) start)
10276                    (search-backward "\n" start t))
10277               (progn
10278                 (subst-char-in-region start (point) ?\n ?\^M)
10279                 (gnus-summary-goto-subject article))
10280             (goto-char start)
10281             nil)))))
10282
10283 (defun gnus-summary-go-to-next-thread (&optional previous)
10284   "Go to the same level (or less) next thread.
10285 If PREVIOUS is non-nil, go to previous thread instead.
10286 Return the article number moved to, or nil if moving was impossible."
10287   (let ((level (gnus-summary-thread-level))
10288         (way (if previous -1 1))
10289         (beg (point)))
10290     (forward-line way)
10291     (while (and (not (eobp))
10292                 (< level (gnus-summary-thread-level)))
10293       (forward-line way))
10294     (if (eobp)
10295         (progn
10296           (goto-char beg)
10297           nil)
10298       (setq beg (point))
10299       (prog1
10300           (gnus-summary-article-number)
10301         (goto-char beg)))))
10302
10303 (defun gnus-summary-next-thread (n &optional silent)
10304   "Go to the same level next N'th thread.
10305 If N is negative, search backward instead.
10306 Returns the difference between N and the number of skips actually
10307 done.
10308
10309 If SILENT, don't output messages."
10310   (interactive "p")
10311   (let ((backward (< n 0))
10312         (n (abs n)))
10313     (while (and (> n 0)
10314                 (gnus-summary-go-to-next-thread backward))
10315       (decf n))
10316     (unless silent
10317       (gnus-summary-position-point))
10318     (when (and (not silent) (/= 0 n))
10319       (gnus-message 7 "No more threads"))
10320     n))
10321
10322 (defun gnus-summary-prev-thread (n)
10323   "Go to the same level previous N'th thread.
10324 Returns the difference between N and the number of skips actually
10325 done."
10326   (interactive "p")
10327   (gnus-summary-next-thread (- n)))
10328
10329 (defun gnus-summary-go-down-thread ()
10330   "Go down one level in the current thread."
10331   (let ((children (gnus-summary-article-children)))
10332     (when children
10333       (gnus-summary-goto-subject (car children)))))
10334
10335 (defun gnus-summary-go-up-thread ()
10336   "Go up one level in the current thread."
10337   (let ((parent (gnus-summary-article-parent)))
10338     (when parent
10339       (gnus-summary-goto-subject parent))))
10340
10341 (defun gnus-summary-down-thread (n)
10342   "Go down thread N steps.
10343 If N is negative, go up instead.
10344 Returns the difference between N and how many steps down that were
10345 taken."
10346   (interactive "p")
10347   (let ((up (< n 0))
10348         (n (abs n)))
10349     (while (and (> n 0)
10350                 (if up (gnus-summary-go-up-thread)
10351                   (gnus-summary-go-down-thread)))
10352       (setq n (1- n)))
10353     (gnus-summary-position-point)
10354     (when (/= 0 n)
10355       (gnus-message 7 "Can't go further"))
10356     n))
10357
10358 (defun gnus-summary-up-thread (n)
10359   "Go up thread N steps.
10360 If N is negative, go down instead.
10361 Returns the difference between N and how many steps down that were
10362 taken."
10363   (interactive "p")
10364   (gnus-summary-down-thread (- n)))
10365
10366 (defun gnus-summary-top-thread ()
10367   "Go to the top of the thread."
10368   (interactive)
10369   (while (gnus-summary-go-up-thread))
10370   (gnus-summary-article-number))
10371
10372 (defun gnus-summary-kill-thread (&optional unmark)
10373   "Mark articles under current thread as read.
10374 If the prefix argument is positive, remove any kinds of marks.
10375 If the prefix argument is negative, tick articles instead."
10376   (interactive "P")
10377   (when unmark
10378     (setq unmark (prefix-numeric-value unmark)))
10379   (let ((articles (gnus-summary-articles-in-thread)))
10380     (save-excursion
10381       ;; Expand the thread.
10382       (gnus-summary-show-thread)
10383       ;; Mark all the articles.
10384       (while articles
10385         (gnus-summary-goto-subject (car articles))
10386         (cond ((null unmark)
10387                (gnus-summary-mark-article-as-read gnus-killed-mark))
10388               ((> unmark 0)
10389                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10390               (t
10391                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10392         (setq articles (cdr articles))))
10393     ;; Hide killed subtrees.
10394     (and (null unmark)
10395          gnus-thread-hide-killed
10396          (gnus-summary-hide-thread))
10397     ;; If marked as read, go to next unread subject.
10398     (when (null unmark)
10399       ;; Go to next unread subject.
10400       (gnus-summary-next-subject 1 t)))
10401   (gnus-set-mode-line 'summary))
10402
10403 ;; Summary sorting commands
10404
10405 (defun gnus-summary-sort-by-number (&optional reverse)
10406   "Sort the summary buffer by article number.
10407 Argument REVERSE means reverse order."
10408   (interactive "P")
10409   (gnus-summary-sort 'number reverse))
10410
10411 (defun gnus-summary-sort-by-random (&optional reverse)
10412   "Randomize the order in the summary buffer.
10413 Argument REVERSE means to randomize in reverse order."
10414   (interactive "P")
10415   (gnus-summary-sort 'random reverse))
10416
10417 (defun gnus-summary-sort-by-author (&optional reverse)
10418   "Sort the summary buffer by author name alphabetically.
10419 If `case-fold-search' is non-nil, case of letters is ignored.
10420 Argument REVERSE means reverse order."
10421   (interactive "P")
10422   (gnus-summary-sort 'author reverse))
10423
10424 (defun gnus-summary-sort-by-subject (&optional reverse)
10425   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10426 If `case-fold-search' is non-nil, case of letters is ignored.
10427 Argument REVERSE means reverse order."
10428   (interactive "P")
10429   (gnus-summary-sort 'subject reverse))
10430
10431 (defun gnus-summary-sort-by-date (&optional reverse)
10432   "Sort the summary buffer by date.
10433 Argument REVERSE means reverse order."
10434   (interactive "P")
10435   (gnus-summary-sort 'date reverse))
10436
10437 (defun gnus-summary-sort-by-score (&optional reverse)
10438   "Sort the summary buffer by score.
10439 Argument REVERSE means reverse order."
10440   (interactive "P")
10441   (gnus-summary-sort 'score reverse))
10442
10443 (defun gnus-summary-sort-by-lines (&optional reverse)
10444   "Sort the summary buffer by the number of lines.
10445 Argument REVERSE means reverse order."
10446   (interactive "P")
10447   (gnus-summary-sort 'lines reverse))
10448
10449 (defun gnus-summary-sort-by-chars (&optional reverse)
10450   "Sort the summary buffer by article length.
10451 Argument REVERSE means reverse order."
10452   (interactive "P")
10453   (gnus-summary-sort 'chars reverse))
10454
10455 (defun gnus-summary-sort-by-original (&optional reverse)
10456   "Sort the summary buffer using the default sorting method.
10457 Argument REVERSE means reverse order."
10458   (interactive "P")
10459   (let* ((buffer-read-only)
10460          (gnus-summary-prepare-hook nil))
10461     ;; We do the sorting by regenerating the threads.
10462     (gnus-summary-prepare)
10463     ;; Hide subthreads if needed.
10464     (gnus-summary-maybe-hide-threads)))
10465
10466 (defun gnus-summary-sort (predicate reverse)
10467   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10468   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10469          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10470          (gnus-thread-sort-functions
10471           (if (not reverse)
10472               thread
10473             `(lambda (t1 t2)
10474                (,thread t2 t1))))
10475          (gnus-sort-gathered-threads-function
10476           gnus-thread-sort-functions)
10477          (gnus-article-sort-functions
10478           (if (not reverse)
10479               article
10480             `(lambda (t1 t2)
10481                (,article t2 t1))))
10482          (buffer-read-only)
10483          (gnus-summary-prepare-hook nil))
10484     ;; We do the sorting by regenerating the threads.
10485     (gnus-summary-prepare)
10486     ;; Hide subthreads if needed.
10487     (gnus-summary-maybe-hide-threads)))
10488
10489 ;; Summary saving commands.
10490
10491 (defun gnus-summary-save-article (&optional n not-saved)
10492   "Save the current article using the default saver function.
10493 If N is a positive number, save the N next articles.
10494 If N is a negative number, save the N previous articles.
10495 If N is nil and any articles have been marked with the process mark,
10496 save those articles instead.
10497 The variable `gnus-default-article-saver' specifies the saver function."
10498   (interactive "P")
10499   (let* ((articles (gnus-summary-work-articles n))
10500          (save-buffer (save-excursion
10501                         (nnheader-set-temp-buffer " *Gnus Save*")))
10502          (num (length articles))
10503          header file)
10504     (dolist (article articles)
10505       (setq header (gnus-summary-article-header article))
10506       (if (not (vectorp header))
10507           ;; This is a pseudo-article.
10508           (if (assq 'name header)
10509               (gnus-copy-file (cdr (assq 'name header)))
10510             (gnus-message 1 "Article %d is unsaveable" article))
10511         ;; This is a real article.
10512         (save-window-excursion
10513           (let ((gnus-display-mime-function nil)
10514                 (gnus-article-prepare-hook nil))
10515             (gnus-summary-select-article t nil nil article)))
10516         (save-excursion
10517           (set-buffer save-buffer)
10518           (erase-buffer)
10519           (insert-buffer-substring gnus-original-article-buffer))
10520         (setq file (gnus-article-save save-buffer file num))
10521         (gnus-summary-remove-process-mark article)
10522         (unless not-saved
10523           (gnus-summary-set-saved-mark article))))
10524     (gnus-kill-buffer save-buffer)
10525     (gnus-summary-position-point)
10526     (gnus-set-mode-line 'summary)
10527     n))
10528
10529 (defun gnus-summary-pipe-output (&optional arg headers)
10530   "Pipe the current article to a subprocess.
10531 If N is a positive number, pipe the N next articles.
10532 If N is a negative number, pipe the N previous articles.
10533 If N is nil and any articles have been marked with the process mark,
10534 pipe those articles instead.
10535 If HEADERS (the symbolic prefix), include the headers, too."
10536   (interactive (gnus-interactive "P\ny"))
10537   (require 'gnus-art)
10538   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10539         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10540     (gnus-summary-save-article arg t))
10541   (let ((buffer (get-buffer "*Shell Command Output*")))
10542     (when (and buffer
10543                (not (zerop (buffer-size buffer))))
10544       (gnus-configure-windows 'pipe))))
10545
10546 (defun gnus-summary-save-article-mail (&optional arg)
10547   "Append the current article to an mail file.
10548 If N is a positive number, save the N next articles.
10549 If N is a negative number, save the N previous articles.
10550 If N is nil and any articles have been marked with the process mark,
10551 save those articles instead."
10552   (interactive "P")
10553   (require 'gnus-art)
10554   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10555     (gnus-summary-save-article arg)))
10556
10557 (defun gnus-summary-save-article-rmail (&optional arg)
10558   "Append the current article to an rmail file.
10559 If N is a positive number, save the N next articles.
10560 If N is a negative number, save the N previous articles.
10561 If N is nil and any articles have been marked with the process mark,
10562 save those articles instead."
10563   (interactive "P")
10564   (require 'gnus-art)
10565   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10566     (gnus-summary-save-article arg)))
10567
10568 (defun gnus-summary-save-article-file (&optional arg)
10569   "Append the current article to a file.
10570 If N is a positive number, save the N next articles.
10571 If N is a negative number, save the N previous articles.
10572 If N is nil and any articles have been marked with the process mark,
10573 save those articles instead."
10574   (interactive "P")
10575   (require 'gnus-art)
10576   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10577     (gnus-summary-save-article arg)))
10578
10579 (defun gnus-summary-write-article-file (&optional arg)
10580   "Write the current article to a file, deleting the previous file.
10581 If N is a positive number, save the N next articles.
10582 If N is a negative number, save the N previous articles.
10583 If N is nil and any articles have been marked with the process mark,
10584 save those articles instead."
10585   (interactive "P")
10586   (require 'gnus-art)
10587   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10588     (gnus-summary-save-article arg)))
10589
10590 (defun gnus-summary-save-article-body-file (&optional arg)
10591   "Append the current article body to a file.
10592 If N is a positive number, save the N next articles.
10593 If N is a negative number, save the N previous articles.
10594 If N is nil and any articles have been marked with the process mark,
10595 save those articles instead."
10596   (interactive "P")
10597   (require 'gnus-art)
10598   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10599     (gnus-summary-save-article arg)))
10600
10601 (defun gnus-summary-muttprint (&optional arg)
10602   "Print the current article using Muttprint.
10603 If N is a positive number, save the N next articles.
10604 If N is a negative number, save the N previous articles.
10605 If N is nil and any articles have been marked with the process mark,
10606 save those articles instead."
10607   (interactive "P")
10608   (require 'gnus-art)
10609   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10610     (gnus-summary-save-article arg t)))
10611
10612 (defun gnus-summary-pipe-message (program)
10613   "Pipe the current article through PROGRAM."
10614   (interactive "sProgram: ")
10615   (gnus-summary-select-article)
10616   (let ((mail-header-separator ""))
10617     (gnus-eval-in-buffer-window gnus-article-buffer
10618       (save-restriction
10619         (widen)
10620         (let ((start (window-start))
10621               buffer-read-only)
10622           (message-pipe-buffer-body program)
10623           (set-window-start (get-buffer-window (current-buffer)) start))))))
10624
10625 (defun gnus-get-split-value (methods)
10626   "Return a value based on the split METHODS."
10627   (let (split-name method result match)
10628     (when methods
10629       (save-excursion
10630         (set-buffer gnus-original-article-buffer)
10631         (save-restriction
10632           (nnheader-narrow-to-headers)
10633           (while (and methods (not split-name))
10634             (goto-char (point-min))
10635             (setq method (pop methods))
10636             (setq match (car method))
10637             (when (cond
10638                    ((stringp match)
10639                     ;; Regular expression.
10640                     (ignore-errors
10641                       (re-search-forward match nil t)))
10642                    ((gnus-functionp match)
10643                     ;; Function.
10644                     (save-restriction
10645                       (widen)
10646                       (setq result (funcall match gnus-newsgroup-name))))
10647                    ((consp match)
10648                     ;; Form.
10649                     (save-restriction
10650                       (widen)
10651                       (setq result (eval match)))))
10652               (setq split-name (cdr method))
10653               (cond ((stringp result)
10654                      (push (expand-file-name
10655                             result gnus-article-save-directory)
10656                            split-name))
10657                     ((consp result)
10658                      (setq split-name (append result split-name)))))))))
10659     (nreverse split-name)))
10660
10661 (defun gnus-valid-move-group-p (group)
10662   (and (boundp group)
10663        (symbol-name group)
10664        (symbol-value group)
10665        (gnus-get-function (gnus-find-method-for-group
10666                            (symbol-name group)) 'request-accept-article t)))
10667
10668 (defun gnus-read-move-group-name (prompt default articles prefix)
10669   "Read a group name."
10670   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10671          (minibuffer-confirm-incomplete nil) ; XEmacs
10672          (prom
10673           (format "%s %s to:"
10674                   prompt
10675                   (if (> (length articles) 1)
10676                       (format "these %d articles" (length articles))
10677                     "this article")))
10678          (to-newsgroup
10679           (cond
10680            ((null split-name)
10681             (gnus-completing-read-with-default
10682              default prom
10683              gnus-active-hashtb
10684              'gnus-valid-move-group-p
10685              nil prefix
10686              'gnus-group-history))
10687            ((= 1 (length split-name))
10688             (gnus-completing-read-with-default
10689              (car split-name) prom
10690              gnus-active-hashtb
10691              'gnus-valid-move-group-p
10692              nil nil
10693              'gnus-group-history))
10694            (t
10695             (gnus-completing-read-with-default
10696              nil prom
10697              (mapcar (lambda (el) (list el))
10698                      (nreverse split-name))
10699              nil nil nil
10700              'gnus-group-history))))
10701          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10702     (when to-newsgroup
10703       (if (or (string= to-newsgroup "")
10704               (string= to-newsgroup prefix))
10705           (setq to-newsgroup default))
10706       (unless to-newsgroup
10707         (error "No group name entered"))
10708       (or (gnus-active to-newsgroup)
10709           (gnus-activate-group to-newsgroup nil nil to-method)
10710           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10711                                      to-newsgroup))
10712               (or (and (gnus-request-create-group to-newsgroup to-method)
10713                        (gnus-activate-group
10714                         to-newsgroup nil nil to-method)
10715                        (gnus-subscribe-group to-newsgroup))
10716                   (error "Couldn't create group %s" to-newsgroup)))
10717           (error "No such group: %s" to-newsgroup)))
10718     to-newsgroup))
10719
10720 (defun gnus-summary-save-parts (type dir n &optional reverse)
10721   "Save parts matching TYPE to DIR.
10722 If REVERSE, save parts that do not match TYPE."
10723   (interactive
10724    (list (read-string "Save parts of type: "
10725                       (or (car gnus-summary-save-parts-type-history)
10726                           gnus-summary-save-parts-default-mime)
10727                       'gnus-summary-save-parts-type-history)
10728          (setq gnus-summary-save-parts-last-directory
10729                (read-file-name "Save to directory: "
10730                                gnus-summary-save-parts-last-directory
10731                                nil t))
10732          current-prefix-arg))
10733   (gnus-summary-iterate n
10734     (let ((gnus-display-mime-function nil)
10735           (gnus-inhibit-treatment t))
10736       (gnus-summary-select-article))
10737     (save-excursion
10738       (set-buffer gnus-article-buffer)
10739       (let ((handles (or gnus-article-mime-handles
10740                          (mm-dissect-buffer nil gnus-article-loose-mime)
10741                          (and gnus-article-emulate-mime
10742                               (mm-uu-dissect)))))
10743         (when handles
10744           (gnus-summary-save-parts-1 type dir handles reverse)
10745           (unless gnus-article-mime-handles ;; Don't destroy this case.
10746             (mm-destroy-parts handles)))))))
10747
10748 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10749   (if (stringp (car handle))
10750       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10751               (cdr handle))
10752     (when (if reverse
10753               (not (string-match type (mm-handle-media-type handle)))
10754             (string-match type (mm-handle-media-type handle)))
10755       (let ((file (expand-file-name
10756                    (file-name-nondirectory
10757                     (or
10758                      (mail-content-type-get
10759                       (mm-handle-disposition handle) 'filename)
10760                      (concat gnus-newsgroup-name
10761                              "." (number-to-string
10762                                   (cdr gnus-article-current)))))
10763                    dir)))
10764         (unless (file-exists-p file)
10765           (mm-save-part-to-file handle file))))))
10766
10767 ;; Summary extract commands
10768
10769 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10770   (let ((buffer-read-only nil)
10771         (article (gnus-summary-article-number))
10772         after-article b e)
10773     (unless (gnus-summary-goto-subject article)
10774       (error "No such article: %d" article))
10775     (gnus-summary-position-point)
10776     ;; If all commands are to be bunched up on one line, we collect
10777     ;; them here.
10778     (unless gnus-view-pseudos-separately
10779       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10780             files action)
10781         (while ps
10782           (setq action (cdr (assq 'action (car ps))))
10783           (setq files (list (cdr (assq 'name (car ps)))))
10784           (while (and ps (cdr ps)
10785                       (string= (or action "1")
10786                                (or (cdr (assq 'action (cadr ps))) "2")))
10787             (push (cdr (assq 'name (cadr ps))) files)
10788             (setcdr ps (cddr ps)))
10789           (when files
10790             (when (not (string-match "%s" action))
10791               (push " " files))
10792             (push " " files)
10793             (when (assq 'execute (car ps))
10794               (setcdr (assq 'execute (car ps))
10795                       (funcall (if (string-match "%s" action)
10796                                    'format 'concat)
10797                                action
10798                                (mapconcat
10799                                 (lambda (f)
10800                                   (if (equal f " ")
10801                                       f
10802                                     (mm-quote-arg f)))
10803                                 files " ")))))
10804           (setq ps (cdr ps)))))
10805     (if (and gnus-view-pseudos (not not-view))
10806         (while pslist
10807           (when (assq 'execute (car pslist))
10808             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10809                                   (eq gnus-view-pseudos 'not-confirm)))
10810           (setq pslist (cdr pslist)))
10811       (save-excursion
10812         (while pslist
10813           (setq after-article (or (cdr (assq 'article (car pslist)))
10814                                   (gnus-summary-article-number)))
10815           (gnus-summary-goto-subject after-article)
10816           (forward-line 1)
10817           (setq b (point))
10818           (insert "    " (file-name-nondirectory
10819                           (cdr (assq 'name (car pslist))))
10820                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10821           (setq e (point))
10822           (forward-line -1)             ; back to `b'
10823           (gnus-add-text-properties
10824            b (1- e) (list 'gnus-number gnus-reffed-article-number
10825                           gnus-mouse-face-prop gnus-mouse-face))
10826           (gnus-data-enter
10827            after-article gnus-reffed-article-number
10828            gnus-unread-mark b (car pslist) 0 (- e b))
10829           (setq gnus-newsgroup-unreads
10830                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10831                                          gnus-reffed-article-number))
10832           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10833           (setq pslist (cdr pslist)))))))
10834
10835 (defun gnus-pseudos< (p1 p2)
10836   (let ((c1 (cdr (assq 'action p1)))
10837         (c2 (cdr (assq 'action p2))))
10838     (and c1 c2 (string< c1 c2))))
10839
10840 (defun gnus-request-pseudo-article (props)
10841   (cond ((assq 'execute props)
10842          (gnus-execute-command (cdr (assq 'execute props)))))
10843   (let ((gnus-current-article (gnus-summary-article-number)))
10844     (gnus-run-hooks 'gnus-mark-article-hook)))
10845
10846 (defun gnus-execute-command (command &optional automatic)
10847   (save-excursion
10848     (gnus-article-setup-buffer)
10849     (set-buffer gnus-article-buffer)
10850     (setq buffer-read-only nil)
10851     (let ((command (if automatic command
10852                      (read-string "Command: " (cons command 0)))))
10853       (erase-buffer)
10854       (insert "$ " command "\n\n")
10855       (if gnus-view-pseudo-asynchronously
10856           (start-process "gnus-execute" (current-buffer) shell-file-name
10857                          shell-command-switch command)
10858         (call-process shell-file-name nil t nil
10859                       shell-command-switch command)))))
10860
10861 ;; Summary kill commands.
10862
10863 (defun gnus-summary-edit-global-kill (article)
10864   "Edit the \"global\" kill file."
10865   (interactive (list (gnus-summary-article-number)))
10866   (gnus-group-edit-global-kill article))
10867
10868 (defun gnus-summary-edit-local-kill ()
10869   "Edit a local kill file applied to the current newsgroup."
10870   (interactive)
10871   (setq gnus-current-headers (gnus-summary-article-header))
10872   (gnus-group-edit-local-kill
10873    (gnus-summary-article-number) gnus-newsgroup-name))
10874
10875 ;;; Header reading.
10876
10877 (defun gnus-read-header (id &optional header)
10878   "Read the headers of article ID and enter them into the Gnus system."
10879   (let ((group gnus-newsgroup-name)
10880         (gnus-override-method
10881          (or
10882           gnus-override-method
10883           (and (gnus-news-group-p gnus-newsgroup-name)
10884                (car (gnus-refer-article-methods)))))
10885         where)
10886     ;; First we check to see whether the header in question is already
10887     ;; fetched.
10888     (if (stringp id)
10889         ;; This is a Message-ID.
10890         (setq header (or header (gnus-id-to-header id)))
10891       ;; This is an article number.
10892       (setq header (or header (gnus-summary-article-header id))))
10893     (if (and header
10894              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10895         ;; We have found the header.
10896         header
10897       ;; If this is a sparse article, we have to nix out its
10898       ;; previous entry in the thread hashtb.
10899       (when (and header
10900                  (gnus-summary-article-sparse-p (mail-header-number header)))
10901         (let* ((parent (gnus-parent-id (mail-header-references header)))
10902                (thread (and parent (gnus-id-to-thread parent))))
10903           (when thread
10904             (delq (assq header thread) thread))))
10905       ;; We have to really fetch the header to this article.
10906       (save-excursion
10907         (set-buffer nntp-server-buffer)
10908         (when (setq where (gnus-request-head id group))
10909           (nnheader-fold-continuation-lines)
10910           (goto-char (point-max))
10911           (insert ".\n")
10912           (goto-char (point-min))
10913           (insert "211 ")
10914           (princ (cond
10915                   ((numberp id) id)
10916                   ((cdr where) (cdr where))
10917                   (header (mail-header-number header))
10918                   (t gnus-reffed-article-number))
10919                  (current-buffer))
10920           (insert " Article retrieved.\n"))
10921         (if (or (not where)
10922                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10923             ()                          ; Malformed head.
10924           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10925             (when (and (stringp id)
10926                        (not (string= (gnus-group-real-name group)
10927                                      (car where))))
10928               ;; If we fetched by Message-ID and the article came
10929               ;; from a different group, we fudge some bogus article
10930               ;; numbers for this article.
10931               (mail-header-set-number header gnus-reffed-article-number))
10932             (save-excursion
10933               (set-buffer gnus-summary-buffer)
10934               (decf gnus-reffed-article-number)
10935               (gnus-remove-header (mail-header-number header))
10936               (push header gnus-newsgroup-headers)
10937               (setq gnus-current-headers header)
10938               (push (mail-header-number header) gnus-newsgroup-limit)))
10939           header)))))
10940
10941 (defun gnus-remove-header (number)
10942   "Remove header NUMBER from `gnus-newsgroup-headers'."
10943   (if (and gnus-newsgroup-headers
10944            (= number (mail-header-number (car gnus-newsgroup-headers))))
10945       (pop gnus-newsgroup-headers)
10946     (let ((headers gnus-newsgroup-headers))
10947       (while (and (cdr headers)
10948                   (not (= number (mail-header-number (cadr headers)))))
10949         (pop headers))
10950       (when (cdr headers)
10951         (setcdr headers (cddr headers))))))
10952
10953 ;;;
10954 ;;; summary highlights
10955 ;;;
10956
10957 (defun gnus-highlight-selected-summary ()
10958   "Highlight selected article in summary buffer."
10959   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10960   (when gnus-summary-selected-face
10961     (save-excursion
10962       (let* ((beg (progn (beginning-of-line) (point)))
10963              (end (progn (end-of-line) (point)))
10964              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10965              (from (if (get-text-property beg gnus-mouse-face-prop)
10966                        beg
10967                      (or (next-single-property-change
10968                           beg gnus-mouse-face-prop nil end)
10969                          beg)))
10970              (to
10971               (if (= from end)
10972                   (- from 2)
10973                 (or (next-single-property-change
10974                      from gnus-mouse-face-prop nil end)
10975                     end))))
10976         ;; If no mouse-face prop on line we will have to = from = end,
10977         ;; so we highlight the entire line instead.
10978         (when (= (+ to 2) from)
10979           (setq from beg)
10980           (setq to end))
10981         (if gnus-newsgroup-selected-overlay
10982             ;; Move old overlay.
10983             (gnus-move-overlay
10984              gnus-newsgroup-selected-overlay from to (current-buffer))
10985           ;; Create new overlay.
10986           (gnus-overlay-put
10987            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10988            'face gnus-summary-selected-face))))))
10989
10990 (defvar gnus-summary-highlight-line-cached nil)
10991 (defvar gnus-summary-highlight-line-trigger nil)
10992
10993 (defun gnus-summary-highlight-line-0 ()
10994   (if (and (eq gnus-summary-highlight-line-trigger 
10995                gnus-summary-highlight)
10996            gnus-summary-highlight-line-cached)
10997       gnus-summary-highlight-line-cached
10998     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
10999           gnus-summary-highlight-line-cached
11000           (let* ((cond (list 'cond))
11001                  (c cond)
11002                  (list gnus-summary-highlight))
11003             (while list
11004               (setcdr c (cons (list (caar list) (list 'quote (cdar list))) nil))
11005               (setq c (cdr c)
11006                     list (cdr list)))
11007             (gnus-byte-compile (list 'lambda nil cond))))))
11008
11009 (defun gnus-summary-highlight-line ()
11010   "Highlight current line according to `gnus-summary-highlight'."
11011   (let* ((beg (gnus-point-at-bol))
11012          (article (or (gnus-summary-article-number) gnus-current-article))
11013          (score (or (cdr (assq article
11014                                gnus-newsgroup-scored))
11015                     gnus-summary-default-score 0))
11016          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11017          (inhibit-read-only t)
11018          (default gnus-summary-default-score)
11019          (default-high gnus-summary-default-high-score)
11020          (default-low gnus-summary-default-low-score)
11021          (uncached (memq article gnus-newsgroup-undownloaded))
11022          (downloaded (not uncached)))
11023     (let ((face (funcall (gnus-summary-highlight-line-0))))
11024       (unless (eq face (get-text-property beg 'face))
11025         (gnus-put-text-property-excluding-characters-with-faces
11026          beg (gnus-point-at-eol) 'face
11027          (setq face (if (boundp face) (symbol-value face) face)))
11028         (when gnus-summary-highlight-line-function
11029           (funcall gnus-summary-highlight-line-function article face))))))
11030
11031 (defun gnus-update-read-articles (group unread &optional compute)
11032   "Update the list of read articles in GROUP.
11033 UNREAD is a sorted list."
11034   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11035          (entry (gnus-gethash group gnus-newsrc-hashtb))
11036          (info (nth 2 entry))
11037          (prev 1)
11038          read)
11039     (if (or (not info) (not active))
11040         ;; There is no info on this group if it was, in fact,
11041         ;; killed.  Gnus stores no information on killed groups, so
11042         ;; there's nothing to be done.
11043         ;; One could store the information somewhere temporarily,
11044         ;; perhaps...  Hmmm...
11045         ()
11046       ;; Remove any negative articles numbers.
11047       (while (and unread (< (car unread) 0))
11048         (setq unread (cdr unread)))
11049       ;; Remove any expired article numbers
11050       (while (and unread (< (car unread) (car active)))
11051         (setq unread (cdr unread)))
11052       ;; Compute the ranges of read articles by looking at the list of
11053       ;; unread articles.
11054       (while unread
11055         (when (/= (car unread) prev)
11056           (push (if (= prev (1- (car unread))) prev
11057                   (cons prev (1- (car unread))))
11058                 read))
11059         (setq prev (1+ (car unread)))
11060         (setq unread (cdr unread)))
11061       (when (<= prev (cdr active))
11062         (push (cons prev (cdr active)) read))
11063       (setq read (if (> (length read) 1) (nreverse read) read))
11064       (if compute
11065           read
11066         (save-excursion
11067           (let (setmarkundo)
11068             ;; Propagate the read marks to the backend.
11069             (when (gnus-check-backend-function 'request-set-mark group)
11070               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11071                     (add (gnus-remove-from-range read (gnus-info-read info))))
11072                 (when (or add del)
11073                   (unless (gnus-check-group group)
11074                     (error "Can't open server for %s" group))
11075                   (gnus-request-set-mark
11076                    group (delq nil (list (if add (list add 'add '(read)))
11077                                          (if del (list del 'del '(read))))))
11078                   (setq setmarkundo
11079                         `(gnus-request-set-mark
11080                           ,group
11081                           ',(delq nil (list
11082                                        (if del (list del 'add '(read)))
11083                                        (if add (list add 'del '(read))))))))))
11084             (set-buffer gnus-group-buffer)
11085             (gnus-undo-register
11086               `(progn
11087                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11088                  (gnus-info-set-read ',info ',(gnus-info-read info))
11089                  (gnus-get-unread-articles-in-group ',info
11090                                                     (gnus-active ,group))
11091                  (gnus-group-update-group ,group t)
11092                  ,setmarkundo))))
11093         ;; Enter this list into the group info.
11094         (gnus-info-set-read info read)
11095         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11096         (gnus-get-unread-articles-in-group info (gnus-active group))
11097         t))))
11098
11099 (defun gnus-offer-save-summaries ()
11100   "Offer to save all active summary buffers."
11101   (let (buffers)
11102     ;; Go through all buffers and find all summaries.
11103     (dolist (buffer (buffer-list))
11104       (when (and (setq buffer (buffer-name buffer))
11105                  (string-match "Summary" buffer)
11106                  (save-excursion
11107                    (set-buffer buffer)
11108                    ;; We check that this is, indeed, a summary buffer.
11109                    (and (eq major-mode 'gnus-summary-mode)
11110                         ;; Also make sure this isn't bogus.
11111                         gnus-newsgroup-prepared
11112                         ;; Also make sure that this isn't a
11113                         ;; dead summary buffer.
11114                         (not gnus-dead-summary-mode))))
11115         (push buffer buffers)))
11116     ;; Go through all these summary buffers and offer to save them.
11117     (when buffers
11118       (save-excursion
11119         (map-y-or-n-p
11120          "Update summary buffer %s? "
11121          (lambda (buf)
11122            (switch-to-buffer buf)
11123            (gnus-summary-exit))
11124          buffers)))))
11125
11126 (defun gnus-summary-setup-default-charset ()
11127   "Setup newsgroup default charset."
11128   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11129       (setq gnus-newsgroup-charset nil)
11130     (let* ((ignored-charsets
11131             (or gnus-newsgroup-ephemeral-ignored-charsets
11132                 (append
11133                  (and gnus-newsgroup-name
11134                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11135                  gnus-newsgroup-ignored-charsets))))
11136       (setq gnus-newsgroup-charset
11137             (or gnus-newsgroup-ephemeral-charset
11138                 (and gnus-newsgroup-name
11139                      (gnus-parameter-charset gnus-newsgroup-name))
11140                 gnus-default-charset))
11141       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11142            ignored-charsets))))
11143
11144 ;;;
11145 ;;; Mime Commands
11146 ;;;
11147
11148 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11149   "Display the current article buffer fully MIME-buttonized.
11150 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11151 treated as multipart/mixed."
11152   (interactive "P")
11153   (require 'gnus-art)
11154   (let ((gnus-unbuttonized-mime-types nil)
11155         (gnus-mime-display-multipart-as-mixed show-all-parts))
11156     (gnus-summary-show-article)))
11157
11158 (defun gnus-summary-repair-multipart (article)
11159   "Add a Content-Type header to a multipart article without one."
11160   (interactive (list (gnus-summary-article-number)))
11161   (gnus-with-article article
11162     (message-narrow-to-head)
11163     (message-remove-header "Mime-Version")
11164     (goto-char (point-max))
11165     (insert "Mime-Version: 1.0\n")
11166     (widen)
11167     (when (search-forward "\n--" nil t)
11168       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11169         (message-narrow-to-head)
11170         (message-remove-header "Content-Type")
11171         (goto-char (point-max))
11172         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11173                         separator))
11174         (widen))))
11175   (let (gnus-mark-article-hook)
11176     (gnus-summary-select-article t t nil article)))
11177
11178 (defun gnus-summary-toggle-display-buttonized ()
11179   "Toggle the buttonizing of the article buffer."
11180   (interactive)
11181   (require 'gnus-art)
11182   (if (setq gnus-inhibit-mime-unbuttonizing
11183             (not gnus-inhibit-mime-unbuttonizing))
11184       (let ((gnus-unbuttonized-mime-types nil))
11185         (gnus-summary-show-article))
11186     (gnus-summary-show-article)))
11187
11188 ;;;
11189 ;;; Generic summary marking commands
11190 ;;;
11191
11192 (defvar gnus-summary-marking-alist
11193   '((read gnus-del-mark "d")
11194     (unread gnus-unread-mark "u")
11195     (ticked gnus-ticked-mark "!")
11196     (dormant gnus-dormant-mark "?")
11197     (expirable gnus-expirable-mark "e"))
11198   "An alist of names/marks/keystrokes.")
11199
11200 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11201 (defvar gnus-summary-mark-map)
11202
11203 (defun gnus-summary-make-all-marking-commands ()
11204   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11205   (dolist (elem gnus-summary-marking-alist)
11206     (apply 'gnus-summary-make-marking-command elem)))
11207
11208 (defun gnus-summary-make-marking-command (name mark keystroke)
11209   (let ((map (make-sparse-keymap)))
11210     (define-key gnus-summary-generic-mark-map keystroke map)
11211     (dolist (lway `((next "next" next nil "n")
11212                     (next-unread "next unread" next t "N")
11213                     (prev "previous" prev nil "p")
11214                     (prev-unread "previous unread" prev t "P")
11215                     (nomove "" nil nil ,keystroke)))
11216       (let ((func (gnus-summary-make-marking-command-1
11217                    mark (car lway) lway name)))
11218         (setq func (eval func))
11219         (define-key map (nth 4 lway) func)))))
11220
11221 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11222   `(defun ,(intern
11223             (format "gnus-summary-put-mark-as-%s%s"
11224                     name (if (eq way 'nomove)
11225                              ""
11226                            (concat "-" (symbol-name way)))))
11227      (n)
11228      ,(format
11229        "Mark the current article as %s%s.
11230 If N, the prefix, then repeat N times.
11231 If N is negative, move in reverse order.
11232 The difference between N and the actual number of articles marked is
11233 returned."
11234        name (cadr lway))
11235      (interactive "p")
11236      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11237
11238 (defun gnus-summary-generic-mark (n mark move unread)
11239   "Mark N articles with MARK."
11240   (unless (eq major-mode 'gnus-summary-mode)
11241     (error "This command can only be used in the summary buffer"))
11242   (gnus-summary-show-thread)
11243   (let ((nummove
11244          (cond
11245           ((eq move 'next) 1)
11246           ((eq move 'prev) -1)
11247           (t 0))))
11248     (if (zerop nummove)
11249         (setq n 1)
11250       (when (< n 0)
11251         (setq n (abs n)
11252               nummove (* -1 nummove))))
11253     (while (and (> n 0)
11254                 (gnus-summary-mark-article nil mark)
11255                 (zerop (gnus-summary-next-subject nummove unread t)))
11256       (setq n (1- n)))
11257     (when (/= 0 n)
11258       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11259     (gnus-summary-recenter)
11260     (gnus-summary-position-point)
11261     (gnus-set-mode-line 'summary)
11262     n))
11263
11264 (defun gnus-summary-insert-articles (articles)
11265   (when (setq articles
11266               (gnus-sorted-difference articles
11267                                       (mapcar (lambda (h)
11268                                                 (mail-header-number h))
11269                                               gnus-newsgroup-headers)))
11270     (setq gnus-newsgroup-headers
11271           (merge 'list
11272                  gnus-newsgroup-headers
11273                  (gnus-fetch-headers articles)
11274                  'gnus-article-sort-by-number))
11275     ;; Suppress duplicates?
11276     (when gnus-suppress-duplicates
11277       (gnus-dup-suppress-articles))
11278
11279     ;; We might want to build some more threads first.
11280     (when (and gnus-fetch-old-headers
11281                (eq gnus-headers-retrieved-by 'nov))
11282       (if (eq gnus-fetch-old-headers 'invisible)
11283           (gnus-build-all-threads)
11284         (gnus-build-old-threads)))
11285     ;; Let the Gnus agent mark articles as read.
11286     (when gnus-agent
11287       (gnus-agent-get-undownloaded-list))
11288     ;; Remove list identifiers from subject
11289     (when gnus-list-identifiers
11290       (gnus-summary-remove-list-identifiers))
11291     ;; First and last article in this newsgroup.
11292     (when gnus-newsgroup-headers
11293       (setq gnus-newsgroup-begin
11294             (mail-header-number (car gnus-newsgroup-headers))
11295             gnus-newsgroup-end
11296             (mail-header-number
11297              (gnus-last-element gnus-newsgroup-headers))))
11298     (when gnus-use-scoring
11299       (gnus-possibly-score-headers))))
11300
11301 (defun gnus-summary-insert-old-articles (&optional all)
11302   "Insert all old articles in this group.
11303 If ALL is non-nil, already read articles become readable.
11304 If ALL is a number, fetch this number of articles."
11305   (interactive "P")
11306   (prog1
11307       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11308             older len)
11309         (setq older
11310               ;; Some nntp servers lie about their active range.  When
11311               ;; this happens, the active range can be in the millions.
11312               ;; Use a compressed range to avoid creating a huge list.
11313               (gnus-range-difference (list gnus-newsgroup-active) old))
11314         (setq len (gnus-range-length older))
11315         (cond
11316          ((null older) nil)
11317          ((numberp all)
11318           (if (< all len)
11319               (let ((older-range (nreverse older)))
11320                 (setq older nil)
11321
11322                 (while (> all 0)
11323                   (let* ((r (pop older-range))
11324                          (min (if (numberp r) r (car r)))
11325                          (max (if (numberp r) r (cdr r))))
11326                     (while (and (<= min max)
11327                                 (> all 0))
11328                       (push max older)
11329                       (setq all (1- all)
11330                             max (1- max))))))
11331             (setq older (gnus-uncompress-range older))))
11332          (all
11333           (setq older (gnus-uncompress-range older)))
11334          (t
11335           (when (and (numberp gnus-large-newsgroup)
11336                    (> len gnus-large-newsgroup))
11337               (let* ((cursor-in-echo-area nil)
11338                      (initial (gnus-parameter-large-newsgroup-initial
11339                                gnus-newsgroup-name))
11340                      (input
11341                       (read-string
11342                        (format
11343                         "How many articles from %s (%s %d): "
11344                         (gnus-limit-string
11345                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11346                         (if initial "max" "default")
11347                         len)
11348                        (if initial
11349                            (cons (number-to-string initial)
11350                                  0)))))
11351                 (unless (string-match "^[ \t]*$" input)
11352                   (setq all (string-to-number input))
11353                   (if (< all len)
11354                       (let ((older-range (nreverse older)))
11355                         (setq older nil)
11356
11357                         (while (> all 0)
11358                           (let* ((r (pop older-range))
11359                                  (min (if (numberp r) r (car r)))
11360                                  (max (if (numberp r) r (cdr r))))
11361                             (while (and (<= min max)
11362                                         (> all 0))
11363                               (push max older)
11364                               (setq all (1- all)
11365                                     max (1- max))))))))))
11366           (setq older (gnus-uncompress-range older))))
11367         (if (not older)
11368             (message "No old news.")
11369           (gnus-summary-insert-articles older)
11370           (gnus-summary-limit (gnus-sorted-nunion old older))))
11371     (gnus-summary-position-point)))
11372
11373 (defun gnus-summary-insert-new-articles ()
11374   "Insert all new articles in this group."
11375   (interactive)
11376   (prog1
11377       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11378             (old-active gnus-newsgroup-active)
11379             (nnmail-fetched-sources (list t))
11380             i new)
11381         (setq gnus-newsgroup-active
11382               (gnus-activate-group gnus-newsgroup-name 'scan))
11383         (setq i (cdr gnus-newsgroup-active))
11384         (while (> i (cdr old-active))
11385           (push i new)
11386           (decf i))
11387         (if (not new)
11388             (message "No gnus is bad news.")
11389           (gnus-summary-insert-articles new)
11390           (setq gnus-newsgroup-unreads
11391                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11392           (gnus-summary-limit (gnus-sorted-nunion old new))))
11393     (gnus-summary-position-point)))
11394
11395 (gnus-summary-make-all-marking-commands)
11396
11397 (gnus-ems-redefine)
11398
11399 (provide 'gnus-sum)
11400
11401 (run-hooks 'gnus-sum-load-hook)
11402
11403 ;;; gnus-sum.el ends here