Indent.
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 (require 'nnoo)
40
41 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
42 (autoload 'gnus-cache-write-active "gnus-cache")
43 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
44 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
45 (autoload 'mm-uu-dissect "mm-uu")
46 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
47   "Deuglify broken Outlook (Express) articles and redisplay."
48   t)
49 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
50 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
51 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
52
53 (defcustom gnus-kill-summary-on-exit t
54   "*If non-nil, kill the summary buffer when you exit from it.
55 If nil, the summary will become a \"*Dead Summary*\" buffer, and
56 it will be killed sometime later."
57   :group 'gnus-summary-exit
58   :type 'boolean)
59
60 (defcustom gnus-fetch-old-headers nil
61   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
62 If an unread article in the group refers to an older, already read (or
63 just marked as read) article, the old article will not normally be
64 displayed in the Summary buffer.  If this variable is non-nil, Gnus
65 will attempt to grab the headers to the old articles, and thereby
66 build complete threads.  If it has the value `some', only enough
67 headers to connect otherwise loose threads will be displayed.  This
68 variable can also be a number.  In that case, no more than that number
69 of old headers will be fetched.  If it has the value `invisible', all
70 old headers will be fetched, but none will be displayed.
71
72 The server has to support NOV for any of this to work."
73   :group 'gnus-thread
74   :type '(choice (const :tag "off" nil)
75                  (const some)
76                  number
77                  (sexp :menu-tag "other" t)))
78
79 (defcustom gnus-refer-thread-limit 200
80   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
81 If t, fetch all the available old headers."
82   :group 'gnus-thread
83   :type '(choice number
84                  (sexp :menu-tag "other" t)))
85
86 (defcustom gnus-summary-make-false-root 'adopt
87   "*nil means that Gnus won't gather loose threads.
88 If the root of a thread has expired or been read in a previous
89 session, the information necessary to build a complete thread has been
90 lost.  Instead of having many small sub-threads from this original thread
91 scattered all over the summary buffer, Gnus can gather them.
92
93 If non-nil, Gnus will try to gather all loose sub-threads from an
94 original thread into one large thread.
95
96 If this variable is non-nil, it should be one of `none', `adopt',
97 `dummy' or `empty'.
98
99 If this variable is `none', Gnus will not make a false root, but just
100 present the sub-threads after another.
101 If this variable is `dummy', Gnus will create a dummy root that will
102 have all the sub-threads as children.
103 If this variable is `adopt', Gnus will make one of the \"children\"
104 the parent and mark all the step-children as such.
105 If this variable is `empty', the \"children\" are printed with empty
106 subject fields.  (Or rather, they will be printed with a string
107 given by the `gnus-summary-same-subject' variable.)"
108   :group 'gnus-thread
109   :type '(choice (const :tag "off" nil)
110                  (const none)
111                  (const dummy)
112                  (const adopt)
113                  (const empty)))
114
115 (defcustom gnus-summary-make-false-root-always nil
116   "Always make a false dummy root."
117   :group 'gnus-thread
118   :type 'boolean)
119
120 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
121   "*A regexp to match subjects to be excluded from loose thread gathering.
122 As loose thread gathering is done on subjects only, that means that
123 there can be many false gatherings performed.  By rooting out certain
124 common subjects, gathering might become saner."
125   :group 'gnus-thread
126   :type 'regexp)
127
128 (defcustom gnus-summary-gather-subject-limit nil
129   "*Maximum length of subject comparisons when gathering loose threads.
130 Use nil to compare full subjects.  Setting this variable to a low
131 number will help gather threads that have been corrupted by
132 newsreaders chopping off subject lines, but it might also mean that
133 unrelated articles that have subject that happen to begin with the
134 same few characters will be incorrectly gathered.
135
136 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
137 comparing subjects."
138   :group 'gnus-thread
139   :type '(choice (const :tag "off" nil)
140                  (const fuzzy)
141                  (sexp :menu-tag "on" t)))
142
143 (defcustom gnus-simplify-subject-functions nil
144   "List of functions taking a string argument that simplify subjects.
145 The functions are applied recursively.
146
147 Useful functions to put in this list include:
148 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
149 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
150   :group 'gnus-thread
151   :type '(repeat function))
152
153 (defcustom gnus-simplify-ignored-prefixes nil
154   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
155   :group 'gnus-thread
156   :type '(choice (const :tag "off" nil)
157                  regexp))
158
159 (defcustom gnus-build-sparse-threads nil
160   "*If non-nil, fill in the gaps in threads.
161 If `some', only fill in the gaps that are needed to tie loose threads
162 together.  If `more', fill in all leaf nodes that Gnus can find.  If
163 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
164   :group 'gnus-thread
165   :type '(choice (const :tag "off" nil)
166                  (const some)
167                  (const more)
168                  (sexp :menu-tag "all" t)))
169
170 (defcustom gnus-summary-thread-gathering-function
171   'gnus-gather-threads-by-subject
172   "*Function used for gathering loose threads.
173 There are two pre-defined functions: `gnus-gather-threads-by-subject',
174 which only takes Subjects into consideration; and
175 `gnus-gather-threads-by-references', which compared the References
176 headers of the articles to find matches."
177   :group 'gnus-thread
178   :type '(radio (function-item gnus-gather-threads-by-subject)
179                 (function-item gnus-gather-threads-by-references)
180                 (function :tag "other")))
181
182 (defcustom gnus-summary-same-subject ""
183   "*String indicating that the current article has the same subject as the previous.
184 This variable will only be used if the value of
185 `gnus-summary-make-false-root' is `empty'."
186   :group 'gnus-summary-format
187   :type 'string)
188
189 (defcustom gnus-summary-goto-unread t
190   "*If t, many commands will go to the next unread article.
191 This applies to marking commands as well as other commands that
192 \"naturally\" select the next article, like, for instance, `SPC' at
193 the end of an article.
194
195 If nil, the marking commands do NOT go to the next unread article
196 \(they go to the next article instead).  If `never', commands that
197 usually go to the next unread article, will go to the next article,
198 whether it is read or not."
199   :group 'gnus-summary-marks
200   :link '(custom-manual "(gnus)Setting Marks")
201   :type '(choice (const :tag "off" nil)
202                  (const never)
203                  (sexp :menu-tag "on" t)))
204
205 (defcustom gnus-summary-default-score 0
206   "*Default article score level.
207 All scores generated by the score files will be added to this score.
208 If this variable is nil, scoring will be disabled."
209   :group 'gnus-score-default
210   :type '(choice (const :tag "disable")
211                  integer))
212
213 (defcustom gnus-summary-default-high-score 0
214   "*Default threshold for a high scored article.
215 An article will be highlighted as high scored if its score is greater
216 than this score."
217   :group 'gnus-score-default
218   :type 'integer)
219
220 (defcustom gnus-summary-default-low-score 0
221   "*Default threshold for a low scored article.
222 An article will be highlighted as low scored if its score is smaller
223 than this score."
224   :group 'gnus-score-default
225   :type 'integer)
226
227 (defcustom gnus-summary-zcore-fuzz 0
228   "*Fuzziness factor for the zcore in the summary buffer.
229 Articles with scores closer than this to `gnus-summary-default-score'
230 will not be marked."
231   :group 'gnus-summary-format
232   :type 'integer)
233
234 (defcustom gnus-simplify-subject-fuzzy-regexp nil
235   "*Strings to be removed when doing fuzzy matches.
236 This can either be a regular expression or list of regular expressions
237 that will be removed from subject strings if fuzzy subject
238 simplification is selected."
239   :group 'gnus-thread
240   :type '(repeat regexp))
241
242 (defcustom gnus-show-threads t
243   "*If non-nil, display threads in summary mode."
244   :group 'gnus-thread
245   :type 'boolean)
246
247 (defcustom gnus-thread-hide-subtree nil
248   "*If non-nil, hide all threads initially.
249 This can be a predicate specifier which says which threads to hide.
250 If threads are hidden, you have to run the command
251 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
252 to expose hidden threads."
253   :group 'gnus-thread
254   :type 'boolean)
255
256 (defcustom gnus-thread-hide-killed t
257   "*If non-nil, hide killed threads automatically."
258   :group 'gnus-thread
259   :type 'boolean)
260
261 (defcustom gnus-thread-ignore-subject t
262   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
263 If nil, articles that have different subjects from their parents will
264 start separate threads."
265   :group 'gnus-thread
266   :type 'boolean)
267
268 (defcustom gnus-thread-operation-ignore-subject t
269   "*If non-nil, subjects will be ignored when doing thread commands.
270 This affects commands like `gnus-summary-kill-thread' and
271 `gnus-summary-lower-thread'.
272
273 If this variable is nil, articles in the same thread with different
274 subjects will not be included in the operation in question.  If this
275 variable is `fuzzy', only articles that have subjects that are fuzzily
276 equal will be included."
277   :group 'gnus-thread
278   :type '(choice (const :tag "off" nil)
279                  (const fuzzy)
280                  (sexp :tag "on" t)))
281
282 (defcustom gnus-thread-indent-level 4
283   "*Number that says how much each sub-thread should be indented."
284   :group 'gnus-thread
285   :type 'integer)
286
287 (defcustom gnus-auto-extend-newsgroup t
288   "*If non-nil, extend newsgroup forward and backward when requested."
289   :group 'gnus-summary-choose
290   :type 'boolean)
291
292 (defcustom gnus-auto-select-first t
293   "*If non-nil, select the article under point.
294 Which article this is is controlled by the `gnus-auto-select-subject'
295 variable.
296
297 If you want to prevent automatic selection of articles in some
298 newsgroups, set the variable to nil in `gnus-select-group-hook'."
299   :group 'gnus-group-select
300   :type '(choice (const :tag "none" nil)
301                  (sexp :menu-tag "first" t)))
302
303 (defcustom gnus-auto-select-subject 'unread
304   "*Says what subject to place under point when entering a group.
305
306 This variable can either be the symbols `first' (place point on the
307 first subject), `unread' (place point on the subject line of the first
308 unread article), `best' (place point on the subject line of the
309 higest-scored article), `unseen' (place point on the subject line of
310 the first unseen article), 'unseen-or-unread' (place point on the subject
311 line of the first unseen article or, if all article have been seen, on the
312 subject line of the first unread article), or a function to be called to
313 place point on some subject line."
314   :group 'gnus-group-select
315   :type '(choice (const best)
316                  (const unread)
317                  (const first)
318                  (const unseen)
319                  (const unseen-or-unread)))
320
321 (defcustom gnus-auto-select-next t
322   "*If non-nil, offer to go to the next group from the end of the previous.
323 If the value is t and the next newsgroup is empty, Gnus will exit
324 summary mode and go back to group mode.  If the value is neither nil
325 nor t, Gnus will select the following unread newsgroup.  In
326 particular, if the value is the symbol `quietly', the next unread
327 newsgroup will be selected without any confirmation, and if it is
328 `almost-quietly', the next group will be selected without any
329 confirmation if you are located on the last article in the group.
330 Finally, if this variable is `slightly-quietly', the `Z n' command
331 will go to the next group without confirmation."
332   :group 'gnus-summary-maneuvering
333   :type '(choice (const :tag "off" nil)
334                  (const quietly)
335                  (const almost-quietly)
336                  (const slightly-quietly)
337                  (sexp :menu-tag "on" t)))
338
339 (defcustom gnus-auto-select-same nil
340   "*If non-nil, select the next article with the same subject.
341 If there are no more articles with the same subject, go to
342 the first unread article."
343   :group 'gnus-summary-maneuvering
344   :type 'boolean)
345
346 (defcustom gnus-summary-check-current nil
347   "*If non-nil, consider the current article when moving.
348 The \"unread\" movement commands will stay on the same line if the
349 current article is unread."
350   :group 'gnus-summary-maneuvering
351   :type 'boolean)
352
353 (defcustom gnus-auto-center-summary t
354   "*If non-nil, always center the current summary buffer.
355 In particular, if `vertical' do only vertical recentering.  If non-nil
356 and non-`vertical', do both horizontal and vertical recentering."
357   :group 'gnus-summary-maneuvering
358   :type '(choice (const :tag "none" nil)
359                  (const vertical)
360                  (integer :tag "height")
361                  (sexp :menu-tag "both" t)))
362
363 (defcustom gnus-show-all-headers nil
364   "*If non-nil, don't hide any headers."
365   :group 'gnus-article-hiding
366   :group 'gnus-article-headers
367   :type 'boolean)
368
369 (defcustom gnus-summary-ignore-duplicates nil
370   "*If non-nil, ignore articles with identical Message-ID headers."
371   :group 'gnus-summary
372   :type 'boolean)
373
374 (defcustom gnus-single-article-buffer t
375   "*If non-nil, display all articles in the same buffer.
376 If nil, each group will get its own article buffer."
377   :group 'gnus-article-various
378   :type 'boolean)
379
380 (defcustom gnus-break-pages t
381   "*If non-nil, do page breaking on articles.
382 The page delimiter is specified by the `gnus-page-delimiter'
383 variable."
384   :group 'gnus-article-various
385   :type 'boolean)
386
387 (defcustom gnus-move-split-methods nil
388   "*Variable used to suggest where articles are to be moved to.
389 It uses the same syntax as the `gnus-split-methods' variable.
390 However, whereas `gnus-split-methods' specifies file names as targets,
391 this variable specifies group names."
392   :group 'gnus-summary-mail
393   :type '(repeat (choice (list :value (fun) function)
394                          (cons :value ("" "") regexp (repeat string))
395                          (sexp :value nil))))
396
397 (defcustom gnus-unread-mark ?           ;Whitespace
398   "*Mark used for unread articles."
399   :group 'gnus-summary-marks
400   :type 'character)
401
402 (defcustom gnus-ticked-mark ?!
403   "*Mark used for ticked articles."
404   :group 'gnus-summary-marks
405   :type 'character)
406
407 (defcustom gnus-dormant-mark ??
408   "*Mark used for dormant articles."
409   :group 'gnus-summary-marks
410   :type 'character)
411
412 (defcustom gnus-del-mark ?r
413   "*Mark used for del'd articles."
414   :group 'gnus-summary-marks
415   :type 'character)
416
417 (defcustom gnus-read-mark ?R
418   "*Mark used for read articles."
419   :group 'gnus-summary-marks
420   :type 'character)
421
422 (defcustom gnus-expirable-mark ?E
423   "*Mark used for expirable articles."
424   :group 'gnus-summary-marks
425   :type 'character)
426
427 (defcustom gnus-killed-mark ?K
428   "*Mark used for killed articles."
429   :group 'gnus-summary-marks
430   :type 'character)
431
432 (defcustom gnus-spam-mark ?H
433   "*Mark used for spam articles."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-souped-mark ?F
438   "*Mark used for souped articles."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-kill-file-mark ?X
443   "*Mark used for articles killed by kill files."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-low-score-mark ?Y
448   "*Mark used for articles with a low score."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-catchup-mark ?C
453   "*Mark used for articles that are caught up."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-replied-mark ?A
458   "*Mark used for articles that have been replied to."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-forwarded-mark ?F
463   "*Mark used for articles that have been forwarded."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-recent-mark ?N
468   "*Mark used for articles that are recent."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-cached-mark ?*
473   "*Mark used for articles that are in the cache."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-saved-mark ?S
478   "*Mark used for articles that have been saved."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-unseen-mark ?.
483   "*Mark used for articles that haven't been seen."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-no-mark ?               ;Whitespace
488   "*Mark used for articles that have no other secondary mark."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-ancient-mark ?O
493   "*Mark used for ancient articles."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-sparse-mark ?Q
498   "*Mark used for sparsely reffed articles."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-canceled-mark ?G
503   "*Mark used for canceled articles."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-duplicate-mark ?M
508   "*Mark used for duplicate articles."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-undownloaded-mark ?-
513   "*Mark used for articles that weren't downloaded."
514   :group 'gnus-summary-marks
515   :type 'character)
516
517 (defcustom gnus-downloaded-mark ?+
518   "*Mark used for articles that were downloaded."
519   :group 'gnus-summary-marks
520   :type 'character)
521
522 (defcustom gnus-downloadable-mark ?%
523   "*Mark used for articles that are to be downloaded."
524   :group 'gnus-summary-marks
525   :type 'character)
526
527 (defcustom gnus-unsendable-mark ?=
528   "*Mark used for articles that won't be sent."
529   :group 'gnus-summary-marks
530   :type 'character)
531
532 (defcustom gnus-score-over-mark ?+
533   "*Score mark used for articles with high scores."
534   :group 'gnus-summary-marks
535   :type 'character)
536
537 (defcustom gnus-score-below-mark ?-
538   "*Score mark used for articles with low scores."
539   :group 'gnus-summary-marks
540   :type 'character)
541
542 (defcustom gnus-empty-thread-mark ?     ;Whitespace
543   "*There is no thread under the article."
544   :group 'gnus-summary-marks
545   :type 'character)
546
547 (defcustom gnus-not-empty-thread-mark ?=
548   "*There is a thread under the article."
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-view-pseudo-asynchronously nil
553   "*If non-nil, Gnus will view pseudo-articles asynchronously."
554   :group 'gnus-extract-view
555   :type 'boolean)
556
557 (defcustom gnus-auto-expirable-marks
558   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
559         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
560         gnus-souped-mark gnus-duplicate-mark)
561   "*The list of marks converted into expiration if a group is auto-expirable."
562   :version "21.1"
563   :group 'gnus-summary
564   :type '(repeat character))
565
566 (defcustom gnus-inhibit-user-auto-expire t
567   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
568   :version "21.1"
569   :group 'gnus-summary
570   :type 'boolean)
571
572 (defcustom gnus-view-pseudos nil
573   "*If `automatic', pseudo-articles will be viewed automatically.
574 If `not-confirm', pseudos will be viewed automatically, and the user
575 will not be asked to confirm the command."
576   :group 'gnus-extract-view
577   :type '(choice (const :tag "off" nil)
578                  (const automatic)
579                  (const not-confirm)))
580
581 (defcustom gnus-view-pseudos-separately t
582   "*If non-nil, one pseudo-article will be created for each file to be viewed.
583 If nil, all files that use the same viewing command will be given as a
584 list of parameters to that command."
585   :group 'gnus-extract-view
586   :type 'boolean)
587
588 (defcustom gnus-insert-pseudo-articles t
589   "*If non-nil, insert pseudo-articles when decoding articles."
590   :group 'gnus-extract-view
591   :type 'boolean)
592
593 (defcustom gnus-summary-dummy-line-format
594   "   %(:                             :%) %S\n"
595   "*The format specification for the dummy roots in the summary buffer.
596 It works along the same lines as a normal formatting string,
597 with some simple extensions.
598
599 %S  The subject
600
601 General format specifiers can also be used.
602 See `(gnus)Formatting Variables'."
603   :link '(custom-manual "(gnus)Formatting Variables")
604   :group 'gnus-threading
605   :type 'string)
606
607 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
608   "*The format specification for the summary mode line.
609 It works along the same lines as a normal formatting string,
610 with some simple extensions:
611
612 %G  Group name
613 %p  Unprefixed group name
614 %A  Current article number
615 %z  Current article score
616 %V  Gnus version
617 %U  Number of unread articles in the group
618 %e  Number of unselected articles in the group
619 %Z  A string with unread/unselected article counts
620 %g  Shortish group name
621 %S  Subject of the current article
622 %u  User-defined spec
623 %s  Current score file name
624 %d  Number of dormant articles
625 %r  Number of articles that have been marked as read in this session
626 %E  Number of articles expunged by the score files"
627   :group 'gnus-summary-format
628   :type 'string)
629
630 (defcustom gnus-list-identifiers nil
631   "Regexp that matches list identifiers to be removed from subject.
632 This can also be a list of regexps."
633   :version "21.1"
634   :group 'gnus-summary-format
635   :group 'gnus-article-hiding
636   :type '(choice (const :tag "none" nil)
637                  (regexp :value ".*")
638                  (repeat :value (".*") regexp)))
639
640 (defcustom gnus-summary-mark-below 0
641   "*Mark all articles with a score below this variable as read.
642 This variable is local to each summary buffer and usually set by the
643 score file."
644   :group 'gnus-score-default
645   :type 'integer)
646
647 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
648   "*List of functions used for sorting articles in the summary buffer.
649
650 Each function takes two articles and returns non-nil if the first
651 article should be sorted before the other.  If you use more than one
652 function, the primary sort function should be the last.  You should
653 probably always include `gnus-article-sort-by-number' in the list of
654 sorting functions -- preferably first.  Also note that sorting by date
655 is often much slower than sorting by number, and the sorting order is
656 very similar.  (Sorting by date means sorting by the time the message
657 was sent, sorting by number means sorting by arrival time.)
658
659 Ready-made functions include `gnus-article-sort-by-number',
660 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
661 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
662 and `gnus-article-sort-by-score'.
663
664 When threading is turned on, the variable `gnus-thread-sort-functions'
665 controls how articles are sorted."
666   :group 'gnus-summary-sort
667   :type '(repeat (choice (function-item gnus-article-sort-by-number)
668                          (function-item gnus-article-sort-by-author)
669                          (function-item gnus-article-sort-by-subject)
670                          (function-item gnus-article-sort-by-date)
671                          (function-item gnus-article-sort-by-score)
672                          (function-item gnus-article-sort-by-random)
673                          (function :tag "other"))))
674
675 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
676   "*List of functions used for sorting threads in the summary buffer.
677 By default, threads are sorted by article number.
678
679 Each function takes two threads and returns non-nil if the first
680 thread should be sorted before the other.  If you use more than one
681 function, the primary sort function should be the last.  You should
682 probably always include `gnus-thread-sort-by-number' in the list of
683 sorting functions -- preferably first.  Also note that sorting by date
684 is often much slower than sorting by number, and the sorting order is
685 very similar.  (Sorting by date means sorting by the time the message
686 was sent, sorting by number means sorting by arrival time.)
687
688 Ready-made functions include `gnus-thread-sort-by-number',
689 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
690 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
691 `gnus-thread-sort-by-most-recent-number',
692 `gnus-thread-sort-by-most-recent-date',
693 `gnus-thread-sort-by-random', and
694 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
695
696 When threading is turned off, the variable
697 `gnus-article-sort-functions' controls how articles are sorted."
698   :group 'gnus-summary-sort
699   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
700                          (function-item gnus-thread-sort-by-author)
701                          (function-item gnus-thread-sort-by-subject)
702                          (function-item gnus-thread-sort-by-date)
703                          (function-item gnus-thread-sort-by-score)
704                          (function-item gnus-thread-sort-by-total-score)
705                          (function-item gnus-thread-sort-by-random)
706                          (function :tag "other"))))
707
708 (defcustom gnus-thread-score-function '+
709   "*Function used for calculating the total score of a thread.
710
711 The function is called with the scores of the article and each
712 subthread and should then return the score of the thread.
713
714 Some functions you can use are `+', `max', or `min'."
715   :group 'gnus-summary-sort
716   :type 'function)
717
718 (defcustom gnus-summary-expunge-below nil
719   "All articles that have a score less than this variable will be expunged.
720 This variable is local to the summary buffers."
721   :group 'gnus-score-default
722   :type '(choice (const :tag "off" nil)
723                  integer))
724
725 (defcustom gnus-thread-expunge-below nil
726   "All threads that have a total score less than this variable will be expunged.
727 See `gnus-thread-score-function' for en explanation of what a
728 \"thread score\" is.
729
730 This variable is local to the summary buffers."
731   :group 'gnus-threading
732   :group 'gnus-score-default
733   :type '(choice (const :tag "off" nil)
734                  integer))
735
736 (defcustom gnus-summary-mode-hook nil
737   "*A hook for Gnus summary mode.
738 This hook is run before any variables are set in the summary buffer."
739   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
740   :group 'gnus-summary-various
741   :type 'hook)
742
743 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
744 (when (featurep 'xemacs)
745   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
746   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
747   (add-hook 'gnus-summary-mode-hook
748             'gnus-xmas-switch-horizontal-scrollbar-off))
749
750 (defcustom gnus-summary-menu-hook nil
751   "*Hook run after the creation of the summary mode menu."
752   :group 'gnus-summary-visual
753   :type 'hook)
754
755 (defcustom gnus-summary-exit-hook nil
756   "*A hook called on exit from the summary buffer.
757 It will be called with point in the group buffer."
758   :group 'gnus-summary-exit
759   :type 'hook)
760
761 (defcustom gnus-summary-prepare-hook nil
762   "*A hook called after the summary buffer has been generated.
763 If you want to modify the summary buffer, you can use this hook."
764   :group 'gnus-summary-various
765   :type 'hook)
766
767 (defcustom gnus-summary-prepared-hook nil
768   "*A hook called as the last thing after the summary buffer has been generated."
769   :group 'gnus-summary-various
770   :type 'hook)
771
772 (defcustom gnus-summary-generate-hook nil
773   "*A hook run just before generating the summary buffer.
774 This hook is commonly used to customize threading variables and the
775 like."
776   :group 'gnus-summary-various
777   :type 'hook)
778
779 (defcustom gnus-select-group-hook nil
780   "*A hook called when a newsgroup is selected.
781
782 If you'd like to simplify subjects like the
783 `gnus-summary-next-same-subject' command does, you can use the
784 following hook:
785
786  (add-hook gnus-select-group-hook
787            (lambda ()
788              (mapcar (lambda (header)
789                        (mail-header-set-subject
790                         header
791                         (gnus-simplify-subject
792                          (mail-header-subject header) 're-only)))
793                      gnus-newsgroup-headers)))"
794   :group 'gnus-group-select
795   :type 'hook)
796
797 (defcustom gnus-select-article-hook nil
798   "*A hook called when an article is selected."
799   :group 'gnus-summary-choose
800   :options '(gnus-agent-fetch-selected-article)
801   :type 'hook)
802
803 (defcustom gnus-visual-mark-article-hook
804   (list 'gnus-highlight-selected-summary)
805   "*Hook run after selecting an article in the summary buffer.
806 It is meant to be used for highlighting the article in some way.  It
807 is not run if `gnus-visual' is nil."
808   :group 'gnus-summary-visual
809   :type 'hook)
810
811 (defcustom gnus-parse-headers-hook nil
812   "*A hook called before parsing the headers."
813   :group 'gnus-various
814   :type 'hook)
815
816 (defcustom gnus-exit-group-hook nil
817   "*A hook called when exiting summary mode.
818 This hook is not called from the non-updating exit commands like `Q'."
819   :group 'gnus-various
820   :type 'hook)
821
822 (defcustom gnus-summary-update-hook
823   (list 'gnus-summary-highlight-line)
824   "*A hook called when a summary line is changed.
825 The hook will not be called if `gnus-visual' is nil.
826
827 The default function `gnus-summary-highlight-line' will
828 highlight the line according to the `gnus-summary-highlight'
829 variable."
830   :group 'gnus-summary-visual
831   :type 'hook)
832
833 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
834   "*A hook called when an article is selected for the first time.
835 The hook is intended to mark an article as read (or unread)
836 automatically when it is selected."
837   :group 'gnus-summary-choose
838   :type 'hook)
839
840 (defcustom gnus-group-no-more-groups-hook nil
841   "*A hook run when returning to group mode having no more (unread) groups."
842   :group 'gnus-group-select
843   :type 'hook)
844
845 (defcustom gnus-ps-print-hook nil
846   "*A hook run before ps-printing something from Gnus."
847   :group 'gnus-summary
848   :type 'hook)
849
850 (defcustom gnus-summary-display-arrow
851   (and (fboundp 'display-graphic-p)
852        (display-graphic-p))
853   "*If non-nil, display an arrow highlighting the current article."
854   :version "21.1"
855   :group 'gnus-summary
856   :type 'boolean)
857
858 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
859   "Face used for highlighting the current article in the summary buffer."
860   :group 'gnus-summary-visual
861   :type 'face)
862
863 (defvar gnus-tmp-downloaded nil)
864
865 (defcustom gnus-summary-highlight
866   '(((eq mark gnus-canceled-mark)
867      . gnus-summary-cancelled-face)
868     ((and uncached (> score default-high))
869      . gnus-summary-high-undownloaded-face)
870     ((and uncached (< score default-low))
871      . gnus-summary-low-undownloaded-face)
872     (uncached
873      . gnus-summary-normal-undownloaded-face)
874     ((and (> score default-high)
875           (or (eq mark gnus-dormant-mark)
876               (eq mark gnus-ticked-mark)))
877      . gnus-summary-high-ticked-face)
878     ((and (< score default-low)
879           (or (eq mark gnus-dormant-mark)
880               (eq mark gnus-ticked-mark)))
881      . gnus-summary-low-ticked-face)
882     ((or (eq mark gnus-dormant-mark)
883          (eq mark gnus-ticked-mark))
884      . gnus-summary-normal-ticked-face)
885     ((and (> score default-high) (eq mark gnus-ancient-mark))
886      . gnus-summary-high-ancient-face)
887     ((and (< score default-low) (eq mark gnus-ancient-mark))
888      . gnus-summary-low-ancient-face)
889     ((eq mark gnus-ancient-mark)
890      . gnus-summary-normal-ancient-face)
891     ((and (> score default-high) (eq mark gnus-unread-mark))
892      . gnus-summary-high-unread-face)
893     ((and (< score default-low) (eq mark gnus-unread-mark))
894      . gnus-summary-low-unread-face)
895     ((eq mark gnus-unread-mark)
896      . gnus-summary-normal-unread-face)
897     ((> score default-high)
898      . gnus-summary-high-read-face)
899     ((< score default-low)
900      . gnus-summary-low-read-face)
901     (t
902      . gnus-summary-normal-read-face))
903   "*Controls the highlighting of summary buffer lines.
904
905 A list of (FORM . FACE) pairs.  When deciding how a a particular
906 summary line should be displayed, each form is evaluated.  The content
907 of the face field after the first true form is used.  You can change
908 how those summary lines are displayed, by editing the face field.
909
910 You can use the following variables in the FORM field.
911
912 score:        The article's score
913 default:      The default article score.
914 default-high: The default score for high scored articles.
915 default-low:  The default score for low scored articles.
916 below:        The score below which articles are automatically marked as read.
917 mark:         The articles mark."
918   :group 'gnus-summary-visual
919   :type '(repeat (cons (sexp :tag "Form" nil)
920                        face)))
921
922 (defcustom gnus-alter-header-function nil
923   "Function called to allow alteration of article header structures.
924 The function is called with one parameter, the article header vector,
925 which it may alter in any way."
926   :type '(choice (const :tag "None" nil)
927                  function)
928   :group 'gnus-summary)
929
930 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
931   "Variable that says which function should be used to decode a string with encoded words.")
932
933 (defcustom gnus-extra-headers '(To Newsgroups)
934   "*Extra headers to parse."
935   :version "21.1"
936   :group 'gnus-summary
937   :type '(repeat symbol))
938
939 (defcustom gnus-ignored-from-addresses
940   (and user-mail-address (regexp-quote user-mail-address))
941   "*Regexp of From headers that may be suppressed in favor of To headers."
942   :version "21.1"
943   :group 'gnus-summary
944   :type 'regexp)
945
946 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
947   "List of charsets that should be ignored.
948 When these charsets are used in the \"charset\" parameter, the
949 default charset will be used instead."
950   :version "21.1"
951   :type '(repeat symbol)
952   :group 'gnus-charset)
953
954 (gnus-define-group-parameter
955  ignored-charsets
956  :type list
957  :function-document
958  "Return the ignored charsets of GROUP."
959  :variable gnus-group-ignored-charsets-alist
960  :variable-default
961  '(("alt\\.chinese\\.text" iso-8859-1))
962  :variable-document
963  "Alist of regexps (to match group names) and charsets that should be ignored.
964 When these charsets are used in the \"charset\" parameter, the
965 default charset will be used instead."
966  :variable-group gnus-charset
967  :variable-type '(repeat (cons (regexp :tag "Group")
968                                (repeat symbol)))
969  :parameter-type '(choice :tag "Ignored charsets"
970                           :value nil
971                           (repeat (symbol)))
972  :parameter-document       "\
973 List of charsets that should be ignored.
974
975 When these charsets are used in the \"charset\" parameter, the
976 default charset will be used instead.")
977
978 (defcustom gnus-group-highlight-words-alist nil
979   "Alist of group regexps and highlight regexps.
980 This variable uses the same syntax as `gnus-emphasis-alist'."
981   :version "21.1"
982   :type '(repeat (cons (regexp :tag "Group")
983                        (repeat (list (regexp :tag "Highlight regexp")
984                                      (number :tag "Group for entire word" 0)
985                                      (number :tag "Group for displayed part" 0)
986                                      (symbol :tag "Face"
987                                              gnus-emphasis-highlight-words)))))
988   :group 'gnus-summary-visual)
989
990 (defcustom gnus-summary-show-article-charset-alist
991   nil
992   "Alist of number and charset.
993 The article will be shown with the charset corresponding to the
994 numbered argument.
995 For example: ((1 . cn-gb-2312) (2 . big5))."
996   :version "21.1"
997   :type '(repeat (cons (number :tag "Argument" 1)
998                        (symbol :tag "Charset")))
999   :group 'gnus-charset)
1000
1001 (defcustom gnus-preserve-marks t
1002   "Whether marks are preserved when moving, copying and respooling messages."
1003   :version "21.1"
1004   :type 'boolean
1005   :group 'gnus-summary-marks)
1006
1007 (defcustom gnus-alter-articles-to-read-function nil
1008   "Function to be called to alter the list of articles to be selected."
1009   :type '(choice (const nil) function)
1010   :group 'gnus-summary)
1011
1012 (defcustom gnus-orphan-score nil
1013   "*All orphans get this score added.  Set in the score file."
1014   :group 'gnus-score-default
1015   :type '(choice (const nil)
1016                  integer))
1017
1018 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1019   "*A regexp to match MIME parts when saving multiple parts of a message
1020 with gnus-summary-save-parts (X m). This regexp will be used by default
1021 when prompting the user for which type of files to save."
1022   :group 'gnus-summary
1023   :type 'regexp)
1024
1025 (defcustom gnus-read-all-available-headers nil
1026   "Whether Gnus should parse all headers made available to it.
1027 This is mostly relevant for slow backends where the user may
1028 wish to widen the summary buffer to include all headers
1029 that were fetched.  Say, for nnultimate groups."
1030   :group 'gnus-summary
1031   :type '(choice boolean regexp))
1032
1033 (defcustom gnus-summary-muttprint-program "muttprint"
1034   "Command (and optional arguments) used to run Muttprint."
1035   :version "21.3"
1036   :group 'gnus-summary
1037   :type 'string)
1038
1039 (defcustom gnus-article-loose-mime nil
1040   "If non-nil, don't require MIME-Version header.
1041 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1042 supply the MIME-Version header or deliberately strip it From the mail.
1043 Set it to non-nil, Gnus will treat some articles as MIME even if
1044 the MIME-Version header is missed."
1045   :version "21.3"
1046   :type 'boolean
1047   :group 'gnus-article)
1048
1049 (defcustom gnus-article-emulate-mime t
1050   "If non-nil, use MIME emulation for uuencode and the like.
1051 This means that Gnus will search message bodies for text that look
1052 like uuencoded bits, yEncoded bits, and so on, and present that using
1053 the normal Gnus MIME machinery."
1054   :type 'boolean
1055   :group 'gnus-article)
1056
1057 ;;; Internal variables
1058
1059 (defvar gnus-summary-display-cache nil)
1060 (defvar gnus-article-mime-handles nil)
1061 (defvar gnus-article-decoded-p nil)
1062 (defvar gnus-article-charset nil)
1063 (defvar gnus-article-ignored-charsets nil)
1064 (defvar gnus-scores-exclude-files nil)
1065 (defvar gnus-page-broken nil)
1066
1067 (defvar gnus-original-article nil)
1068 (defvar gnus-article-internal-prepare-hook nil)
1069 (defvar gnus-newsgroup-process-stack nil)
1070
1071 (defvar gnus-thread-indent-array nil)
1072 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1073 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1074   "Function called to sort the articles within a thread after it has been gathered together.")
1075
1076 (defvar gnus-summary-save-parts-type-history nil)
1077 (defvar gnus-summary-save-parts-last-directory nil)
1078
1079 ;; Avoid highlighting in kill files.
1080 (defvar gnus-summary-inhibit-highlight nil)
1081 (defvar gnus-newsgroup-selected-overlay nil)
1082 (defvar gnus-inhibit-limiting nil)
1083 (defvar gnus-newsgroup-adaptive-score-file nil)
1084 (defvar gnus-current-score-file nil)
1085 (defvar gnus-current-move-group nil)
1086 (defvar gnus-current-copy-group nil)
1087 (defvar gnus-current-crosspost-group nil)
1088 (defvar gnus-newsgroup-display nil)
1089
1090 (defvar gnus-newsgroup-dependencies nil)
1091 (defvar gnus-newsgroup-adaptive nil)
1092 (defvar gnus-summary-display-article-function nil)
1093 (defvar gnus-summary-highlight-line-function nil
1094   "Function called after highlighting a summary line.")
1095
1096 (defvar gnus-summary-line-format-alist
1097   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1098     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1099     (?s gnus-tmp-subject-or-nil ?s)
1100     (?n gnus-tmp-name ?s)
1101     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1102         ?s)
1103     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1104             gnus-tmp-from) ?s)
1105     (?F gnus-tmp-from ?s)
1106     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1107     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1108     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1109     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1110     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1111     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1112     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1113     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1114     (?L gnus-tmp-lines ?s)
1115     (?O gnus-tmp-downloaded ?c)
1116     (?I gnus-tmp-indentation ?s)
1117     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1118     (?R gnus-tmp-replied ?c)
1119     (?\[ gnus-tmp-opening-bracket ?c)
1120     (?\] gnus-tmp-closing-bracket ?c)
1121     (?\> (make-string gnus-tmp-level ? ) ?s)
1122     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1123     (?i gnus-tmp-score ?d)
1124     (?z gnus-tmp-score-char ?c)
1125     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1126     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1127     (?U gnus-tmp-unread ?c)
1128     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1129         ?s)
1130     (?t (gnus-summary-number-of-articles-in-thread
1131          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1132         ?d)
1133     (?e (gnus-summary-number-of-articles-in-thread
1134          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1135         ?c)
1136     (?u gnus-tmp-user-defined ?s)
1137     (?P (gnus-pick-line-number) ?d)
1138     (?B gnus-tmp-thread-tree-header-string ?s)
1139     (user-date (gnus-user-date
1140                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1141   "An alist of format specifications that can appear in summary lines.
1142 These are paired with what variables they correspond with, along with
1143 the type of the variable (string, integer, character, etc).")
1144
1145 (defvar gnus-summary-dummy-line-format-alist
1146   `((?S gnus-tmp-subject ?s)
1147     (?N gnus-tmp-number ?d)
1148     (?u gnus-tmp-user-defined ?s)))
1149
1150 (defvar gnus-summary-mode-line-format-alist
1151   `((?G gnus-tmp-group-name ?s)
1152     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1153     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1154     (?A gnus-tmp-article-number ?d)
1155     (?Z gnus-tmp-unread-and-unselected ?s)
1156     (?V gnus-version ?s)
1157     (?U gnus-tmp-unread-and-unticked ?d)
1158     (?S gnus-tmp-subject ?s)
1159     (?e gnus-tmp-unselected ?d)
1160     (?u gnus-tmp-user-defined ?s)
1161     (?d (length gnus-newsgroup-dormant) ?d)
1162     (?t (length gnus-newsgroup-marked) ?d)
1163     (?h (length gnus-newsgroup-spam-marked) ?d)
1164     (?r (length gnus-newsgroup-reads) ?d)
1165     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1166     (?E gnus-newsgroup-expunged-tally ?d)
1167     (?s (gnus-current-score-file-nondirectory) ?s)))
1168
1169 (defvar gnus-last-search-regexp nil
1170   "Default regexp for article search command.")
1171
1172 (defvar gnus-last-shell-command nil
1173   "Default shell command on article.")
1174
1175 (defvar gnus-newsgroup-agentized nil
1176   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1177 (defvar gnus-newsgroup-begin nil)
1178 (defvar gnus-newsgroup-end nil)
1179 (defvar gnus-newsgroup-last-rmail nil)
1180 (defvar gnus-newsgroup-last-mail nil)
1181 (defvar gnus-newsgroup-last-folder nil)
1182 (defvar gnus-newsgroup-last-file nil)
1183 (defvar gnus-newsgroup-auto-expire nil)
1184 (defvar gnus-newsgroup-active nil)
1185
1186 (defvar gnus-newsgroup-data nil)
1187 (defvar gnus-newsgroup-data-reverse nil)
1188 (defvar gnus-newsgroup-limit nil)
1189 (defvar gnus-newsgroup-limits nil)
1190
1191 (defvar gnus-newsgroup-unreads nil
1192   "Sorted list of unread articles in the current newsgroup.")
1193
1194 (defvar gnus-newsgroup-unselected nil
1195   "Sorted list of unselected unread articles in the current newsgroup.")
1196
1197 (defvar gnus-newsgroup-reads nil
1198   "Alist of read articles and article marks in the current newsgroup.")
1199
1200 (defvar gnus-newsgroup-expunged-tally nil)
1201
1202 (defvar gnus-newsgroup-marked nil
1203   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1204
1205 (defvar gnus-newsgroup-spam-marked nil
1206   "List of ranges of articles that have been marked as spam.")
1207
1208 (defvar gnus-newsgroup-killed nil
1209   "List of ranges of articles that have been through the scoring process.")
1210
1211 (defvar gnus-newsgroup-cached nil
1212   "Sorted list of articles that come from the article cache.")
1213
1214 (defvar gnus-newsgroup-saved nil
1215   "List of articles that have been saved.")
1216
1217 (defvar gnus-newsgroup-kill-headers nil)
1218
1219 (defvar gnus-newsgroup-replied nil
1220   "List of articles that have been replied to in the current newsgroup.")
1221
1222 (defvar gnus-newsgroup-forwarded nil
1223   "List of articles that have been forwarded in the current newsgroup.")
1224
1225 (defvar gnus-newsgroup-recent nil
1226   "List of articles that have are recent in the current newsgroup.")
1227
1228 (defvar gnus-newsgroup-expirable nil
1229   "Sorted list of articles in the current newsgroup that can be expired.")
1230
1231 (defvar gnus-newsgroup-processable nil
1232   "List of articles in the current newsgroup that can be processed.")
1233
1234 (defvar gnus-newsgroup-downloadable nil
1235   "Sorted list of articles in the current newsgroup that can be processed.")
1236
1237 (defvar gnus-newsgroup-undownloaded nil
1238   "List of articles in the current newsgroup that haven't been downloaded..")
1239
1240 (defvar gnus-newsgroup-unsendable nil
1241   "List of articles in the current newsgroup that won't be sent.")
1242
1243 (defvar gnus-newsgroup-bookmarks nil
1244   "List of articles in the current newsgroup that have bookmarks.")
1245
1246 (defvar gnus-newsgroup-dormant nil
1247   "Sorted list of dormant articles in the current newsgroup.")
1248
1249 (defvar gnus-newsgroup-unseen nil
1250   "List of unseen articles in the current newsgroup.")
1251
1252 (defvar gnus-newsgroup-seen nil
1253   "Range of seen articles in the current newsgroup.")
1254
1255 (defvar gnus-newsgroup-articles nil
1256   "List of articles in the current newsgroup.")
1257
1258 (defvar gnus-newsgroup-scored nil
1259   "List of scored articles in the current newsgroup.")
1260
1261 (defvar gnus-newsgroup-headers nil
1262   "List of article headers in the current newsgroup.")
1263
1264 (defvar gnus-newsgroup-threads nil)
1265
1266 (defvar gnus-newsgroup-prepared nil
1267   "Whether the current group has been prepared properly.")
1268
1269 (defvar gnus-newsgroup-ancient nil
1270   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1271
1272 (defvar gnus-newsgroup-sparse nil)
1273
1274 (defvar gnus-current-article nil)
1275 (defvar gnus-article-current nil)
1276 (defvar gnus-current-headers nil)
1277 (defvar gnus-have-all-headers nil)
1278 (defvar gnus-last-article nil)
1279 (defvar gnus-newsgroup-history nil)
1280 (defvar gnus-newsgroup-charset nil)
1281 (defvar gnus-newsgroup-ephemeral-charset nil)
1282 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1283
1284 (defvar gnus-article-before-search nil)
1285
1286 (defconst gnus-summary-local-variables
1287   '(gnus-newsgroup-name
1288     gnus-newsgroup-begin gnus-newsgroup-end
1289     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1290     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1291     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1292     gnus-newsgroup-unselected gnus-newsgroup-marked
1293     gnus-newsgroup-spam-marked
1294     gnus-newsgroup-reads gnus-newsgroup-saved
1295     gnus-newsgroup-replied gnus-newsgroup-forwarded
1296     gnus-newsgroup-recent
1297     gnus-newsgroup-expirable
1298     gnus-newsgroup-processable gnus-newsgroup-killed
1299     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1300     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1301     gnus-newsgroup-seen gnus-newsgroup-articles
1302     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1303     gnus-newsgroup-headers gnus-newsgroup-threads
1304     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1305     gnus-current-article gnus-current-headers gnus-have-all-headers
1306     gnus-last-article gnus-article-internal-prepare-hook
1307     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1308     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1309     gnus-thread-expunge-below
1310     gnus-score-alist gnus-current-score-file
1311     (gnus-summary-expunge-below . global)
1312     (gnus-summary-mark-below . global)
1313     (gnus-orphan-score . global)
1314     gnus-newsgroup-active gnus-scores-exclude-files
1315     gnus-newsgroup-history gnus-newsgroup-ancient
1316     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1317     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1318     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1319     (gnus-newsgroup-expunged-tally . 0)
1320     gnus-cache-removable-articles gnus-newsgroup-cached
1321     gnus-newsgroup-data gnus-newsgroup-data-reverse
1322     gnus-newsgroup-limit gnus-newsgroup-limits
1323     gnus-newsgroup-charset gnus-newsgroup-display)
1324   "Variables that are buffer-local to the summary buffers.")
1325
1326 (defvar gnus-newsgroup-variables nil
1327   "A list of variables that have separate values in different newsgroups.
1328 A list of newsgroup (summary buffer) local variables, or cons of
1329 variables and their default values (when the default values are not
1330 nil), that should be made global while the summary buffer is active.
1331 These variables can be used to set variables in the group parameters
1332 while still allowing them to affect operations done in other
1333 buffers. For example:
1334
1335 \(setq gnus-newsgroup-variables
1336      '(message-use-followup-to
1337        (gnus-visible-headers .
1338          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1339 ")
1340
1341 ;; Byte-compiler warning.
1342 ;(eval-when-compile (defvar gnus-article-mode-map))
1343 (eval-when-compile
1344   (let ((features (cons 'gnus-sum features)))
1345     (require 'gnus)
1346     (require 'gnus-agent)
1347     (require 'gnus-art)))
1348
1349 ;; MIME stuff.
1350
1351 (defvar gnus-decode-encoded-word-methods
1352   '(mail-decode-encoded-word-string)
1353   "List of methods used to decode encoded words.
1354
1355 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1356 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1357 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1358 whose names match REGEXP.
1359
1360 For example:
1361 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1362  mail-decode-encoded-word-string
1363  (\"chinese\" . rfc1843-decode-string))")
1364
1365 (defvar gnus-decode-encoded-word-methods-cache nil)
1366
1367 (defun gnus-multi-decode-encoded-word-string (string)
1368   "Apply the functions from `gnus-encoded-word-methods' that match."
1369   (unless (and gnus-decode-encoded-word-methods-cache
1370                (eq gnus-newsgroup-name
1371                    (car gnus-decode-encoded-word-methods-cache)))
1372     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1373     (mapcar (lambda (x)
1374               (if (symbolp x)
1375                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1376                 (if (and gnus-newsgroup-name
1377                          (string-match (car x) gnus-newsgroup-name))
1378                     (nconc gnus-decode-encoded-word-methods-cache
1379                            (list (cdr x))))))
1380             gnus-decode-encoded-word-methods))
1381   (let ((xlist gnus-decode-encoded-word-methods-cache))
1382     (pop xlist)
1383     (while xlist
1384       (setq string (funcall (pop xlist) string))))
1385   string)
1386
1387 ;; Subject simplification.
1388
1389 (defun gnus-simplify-whitespace (str)
1390   "Remove excessive whitespace from STR."
1391   (let ((mystr str))
1392     ;; Multiple spaces.
1393     (while (string-match "[ \t][ \t]+" mystr)
1394       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1395                           " "
1396                           (substring mystr (match-end 0)))))
1397     ;; Leading spaces.
1398     (when (string-match "^[ \t]+" mystr)
1399       (setq mystr (substring mystr (match-end 0))))
1400     ;; Trailing spaces.
1401     (when (string-match "[ \t]+$" mystr)
1402       (setq mystr (substring mystr 0 (match-beginning 0))))
1403     mystr))
1404
1405 (defun gnus-simplify-all-whitespace (str)
1406   "Remove all whitespace from STR."
1407   (let ((mystr str))
1408     (while (string-match "[ \t\n]+" mystr)
1409       (setq mystr (replace-match "" nil nil mystr)))
1410     mystr))
1411
1412 (defsubst gnus-simplify-subject-re (subject)
1413   "Remove \"Re:\" from subject lines."
1414   (if (string-match message-subject-re-regexp subject)
1415       (substring subject (match-end 0))
1416     subject))
1417
1418 (defun gnus-simplify-subject (subject &optional re-only)
1419   "Remove `Re:' and words in parentheses.
1420 If RE-ONLY is non-nil, strip leading `Re:'s only."
1421   (let ((case-fold-search t))           ;Ignore case.
1422     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1423     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1424       (setq subject (substring subject (match-end 0))))
1425     ;; Remove uninteresting prefixes.
1426     (when (and (not re-only)
1427                gnus-simplify-ignored-prefixes
1428                (string-match gnus-simplify-ignored-prefixes subject))
1429       (setq subject (substring subject (match-end 0))))
1430     ;; Remove words in parentheses from end.
1431     (unless re-only
1432       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1433         (setq subject (substring subject 0 (match-beginning 0)))))
1434     ;; Return subject string.
1435     subject))
1436
1437 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1438 ;; all whitespace.
1439 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1440   (goto-char (point-min))
1441   (while (re-search-forward regexp nil t)
1442     (replace-match (or newtext ""))))
1443
1444 (defun gnus-simplify-buffer-fuzzy ()
1445   "Simplify string in the buffer fuzzily.
1446 The string in the accessible portion of the current buffer is simplified.
1447 It is assumed to be a single-line subject.
1448 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1449 matter is removed.  Additional things can be deleted by setting
1450 `gnus-simplify-subject-fuzzy-regexp'."
1451   (let ((case-fold-search t)
1452         (modified-tick))
1453     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1454
1455     (while (not (eq modified-tick (buffer-modified-tick)))
1456       (setq modified-tick (buffer-modified-tick))
1457       (cond
1458        ((listp gnus-simplify-subject-fuzzy-regexp)
1459         (mapcar 'gnus-simplify-buffer-fuzzy-step
1460                 gnus-simplify-subject-fuzzy-regexp))
1461        (gnus-simplify-subject-fuzzy-regexp
1462         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1463       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1464       (gnus-simplify-buffer-fuzzy-step
1465        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1466       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1467
1468     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1469     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1470     (gnus-simplify-buffer-fuzzy-step " $")
1471     (gnus-simplify-buffer-fuzzy-step "^ +")))
1472
1473 (defun gnus-simplify-subject-fuzzy (subject)
1474   "Simplify a subject string fuzzily.
1475 See `gnus-simplify-buffer-fuzzy' for details."
1476   (save-excursion
1477     (gnus-set-work-buffer)
1478     (let ((case-fold-search t))
1479       ;; Remove uninteresting prefixes.
1480       (when (and gnus-simplify-ignored-prefixes
1481                  (string-match gnus-simplify-ignored-prefixes subject))
1482         (setq subject (substring subject (match-end 0))))
1483       (insert subject)
1484       (inline (gnus-simplify-buffer-fuzzy))
1485       (buffer-string))))
1486
1487 (defsubst gnus-simplify-subject-fully (subject)
1488   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1489   (cond
1490    (gnus-simplify-subject-functions
1491     (gnus-map-function gnus-simplify-subject-functions subject))
1492    ((null gnus-summary-gather-subject-limit)
1493     (gnus-simplify-subject-re subject))
1494    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1495     (gnus-simplify-subject-fuzzy subject))
1496    ((numberp gnus-summary-gather-subject-limit)
1497     (gnus-limit-string (gnus-simplify-subject-re subject)
1498                        gnus-summary-gather-subject-limit))
1499    (t
1500     subject)))
1501
1502 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1503   "Check whether two subjects are equal.
1504 If optional argument simple-first is t, first argument is already
1505 simplified."
1506   (cond
1507    ((null simple-first)
1508     (equal (gnus-simplify-subject-fully s1)
1509            (gnus-simplify-subject-fully s2)))
1510    (t
1511     (equal s1
1512            (gnus-simplify-subject-fully s2)))))
1513
1514 (defun gnus-summary-bubble-group ()
1515   "Increase the score of the current group.
1516 This is a handy function to add to `gnus-summary-exit-hook' to
1517 increase the score of each group you read."
1518   (gnus-group-add-score gnus-newsgroup-name))
1519
1520 \f
1521 ;;;
1522 ;;; Gnus summary mode
1523 ;;;
1524
1525 (put 'gnus-summary-mode 'mode-class 'special)
1526
1527 (defvar gnus-article-commands-menu)
1528
1529 ;; Non-orthogonal keys
1530
1531 (gnus-define-keys gnus-summary-mode-map
1532   " " gnus-summary-next-page
1533   "\177" gnus-summary-prev-page
1534   [delete] gnus-summary-prev-page
1535   [backspace] gnus-summary-prev-page
1536   "\r" gnus-summary-scroll-up
1537   "\M-\r" gnus-summary-scroll-down
1538   "n" gnus-summary-next-unread-article
1539   "p" gnus-summary-prev-unread-article
1540   "N" gnus-summary-next-article
1541   "P" gnus-summary-prev-article
1542   "\M-\C-n" gnus-summary-next-same-subject
1543   "\M-\C-p" gnus-summary-prev-same-subject
1544   "\M-n" gnus-summary-next-unread-subject
1545   "\M-p" gnus-summary-prev-unread-subject
1546   "." gnus-summary-first-unread-article
1547   "," gnus-summary-best-unread-article
1548   "\M-s" gnus-summary-search-article-forward
1549   "\M-r" gnus-summary-search-article-backward
1550   "<" gnus-summary-beginning-of-article
1551   ">" gnus-summary-end-of-article
1552   "j" gnus-summary-goto-article
1553   "^" gnus-summary-refer-parent-article
1554   "\M-^" gnus-summary-refer-article
1555   "u" gnus-summary-tick-article-forward
1556   "!" gnus-summary-tick-article-forward
1557   "U" gnus-summary-tick-article-backward
1558   "d" gnus-summary-mark-as-read-forward
1559   "D" gnus-summary-mark-as-read-backward
1560   "E" gnus-summary-mark-as-expirable
1561   "\M-u" gnus-summary-clear-mark-forward
1562   "\M-U" gnus-summary-clear-mark-backward
1563   "k" gnus-summary-kill-same-subject-and-select
1564   "\C-k" gnus-summary-kill-same-subject
1565   "\M-\C-k" gnus-summary-kill-thread
1566   "\M-\C-l" gnus-summary-lower-thread
1567   "e" gnus-summary-edit-article
1568   "#" gnus-summary-mark-as-processable
1569   "\M-#" gnus-summary-unmark-as-processable
1570   "\M-\C-t" gnus-summary-toggle-threads
1571   "\M-\C-s" gnus-summary-show-thread
1572   "\M-\C-h" gnus-summary-hide-thread
1573   "\M-\C-f" gnus-summary-next-thread
1574   "\M-\C-b" gnus-summary-prev-thread
1575   [(meta down)] gnus-summary-next-thread
1576   [(meta up)] gnus-summary-prev-thread
1577   "\M-\C-u" gnus-summary-up-thread
1578   "\M-\C-d" gnus-summary-down-thread
1579   "&" gnus-summary-execute-command
1580   "c" gnus-summary-catchup-and-exit
1581   "\C-w" gnus-summary-mark-region-as-read
1582   "\C-t" gnus-summary-toggle-truncation
1583   "?" gnus-summary-mark-as-dormant
1584   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1585   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1586   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1587   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1588   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1589   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1590   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1591   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1592   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1593   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1594   "=" gnus-summary-expand-window
1595   "\C-x\C-s" gnus-summary-reselect-current-group
1596   "\M-g" gnus-summary-rescan-group
1597   "w" gnus-summary-stop-page-breaking
1598   "\C-c\C-r" gnus-summary-caesar-message
1599   "f" gnus-summary-followup
1600   "F" gnus-summary-followup-with-original
1601   "C" gnus-summary-cancel-article
1602   "r" gnus-summary-reply
1603   "R" gnus-summary-reply-with-original
1604   "\C-c\C-f" gnus-summary-mail-forward
1605   "o" gnus-summary-save-article
1606   "\C-o" gnus-summary-save-article-mail
1607   "|" gnus-summary-pipe-output
1608   "\M-k" gnus-summary-edit-local-kill
1609   "\M-K" gnus-summary-edit-global-kill
1610   ;; "V" gnus-version
1611   "\C-c\C-d" gnus-summary-describe-group
1612   "q" gnus-summary-exit
1613   "Q" gnus-summary-exit-no-update
1614   "\C-c\C-i" gnus-info-find-node
1615   gnus-mouse-2 gnus-mouse-pick-article
1616   "m" gnus-summary-mail-other-window
1617   "a" gnus-summary-post-news
1618   "i" gnus-summary-news-other-window
1619   "x" gnus-summary-limit-to-unread
1620   "s" gnus-summary-isearch-article
1621   "t" gnus-summary-toggle-header
1622   "g" gnus-summary-show-article
1623   "l" gnus-summary-goto-last-article
1624   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1625   "\C-d" gnus-summary-enter-digest-group
1626   "\M-\C-d" gnus-summary-read-document
1627   "\M-\C-e" gnus-summary-edit-parameters
1628   "\M-\C-a" gnus-summary-customize-parameters
1629   "\C-c\C-b" gnus-bug
1630   "*" gnus-cache-enter-article
1631   "\M-*" gnus-cache-remove-article
1632   "\M-&" gnus-summary-universal-argument
1633   "\C-l" gnus-recenter
1634   "I" gnus-summary-increase-score
1635   "L" gnus-summary-lower-score
1636   "\M-i" gnus-symbolic-argument
1637   "h" gnus-summary-select-article-buffer
1638
1639   "b" gnus-article-view-part
1640   "\M-t" gnus-summary-toggle-display-buttonized
1641
1642   "V" gnus-summary-score-map
1643   "X" gnus-uu-extract-map
1644   "S" gnus-summary-send-map)
1645
1646 ;; Sort of orthogonal keymap
1647 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1648   "t" gnus-summary-tick-article-forward
1649   "!" gnus-summary-tick-article-forward
1650   "d" gnus-summary-mark-as-read-forward
1651   "r" gnus-summary-mark-as-read-forward
1652   "c" gnus-summary-clear-mark-forward
1653   " " gnus-summary-clear-mark-forward
1654   "e" gnus-summary-mark-as-expirable
1655   "x" gnus-summary-mark-as-expirable
1656   "?" gnus-summary-mark-as-dormant
1657   "b" gnus-summary-set-bookmark
1658   "B" gnus-summary-remove-bookmark
1659   "#" gnus-summary-mark-as-processable
1660   "\M-#" gnus-summary-unmark-as-processable
1661   "S" gnus-summary-limit-include-expunged
1662   "C" gnus-summary-catchup
1663   "H" gnus-summary-catchup-to-here
1664   "h" gnus-summary-catchup-from-here
1665   "\C-c" gnus-summary-catchup-all
1666   "k" gnus-summary-kill-same-subject-and-select
1667   "K" gnus-summary-kill-same-subject
1668   "P" gnus-uu-mark-map)
1669
1670 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1671   "c" gnus-summary-clear-above
1672   "u" gnus-summary-tick-above
1673   "m" gnus-summary-mark-above
1674   "k" gnus-summary-kill-below)
1675
1676 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1677   "/" gnus-summary-limit-to-subject
1678   "n" gnus-summary-limit-to-articles
1679   "w" gnus-summary-pop-limit
1680   "s" gnus-summary-limit-to-subject
1681   "a" gnus-summary-limit-to-author
1682   "u" gnus-summary-limit-to-unread
1683   "m" gnus-summary-limit-to-marks
1684   "M" gnus-summary-limit-exclude-marks
1685   "v" gnus-summary-limit-to-score
1686   "*" gnus-summary-limit-include-cached
1687   "D" gnus-summary-limit-include-dormant
1688   "T" gnus-summary-limit-include-thread
1689   "d" gnus-summary-limit-exclude-dormant
1690   "t" gnus-summary-limit-to-age
1691   "." gnus-summary-limit-to-unseen
1692   "x" gnus-summary-limit-to-extra
1693   "p" gnus-summary-limit-to-display-predicate
1694   "E" gnus-summary-limit-include-expunged
1695   "c" gnus-summary-limit-exclude-childless-dormant
1696   "C" gnus-summary-limit-mark-excluded-as-read
1697   "o" gnus-summary-insert-old-articles
1698   "N" gnus-summary-insert-new-articles)
1699
1700 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1701   "n" gnus-summary-next-unread-article
1702   "p" gnus-summary-prev-unread-article
1703   "N" gnus-summary-next-article
1704   "P" gnus-summary-prev-article
1705   "\C-n" gnus-summary-next-same-subject
1706   "\C-p" gnus-summary-prev-same-subject
1707   "\M-n" gnus-summary-next-unread-subject
1708   "\M-p" gnus-summary-prev-unread-subject
1709   "f" gnus-summary-first-unread-article
1710   "b" gnus-summary-best-unread-article
1711   "j" gnus-summary-goto-article
1712   "g" gnus-summary-goto-subject
1713   "l" gnus-summary-goto-last-article
1714   "o" gnus-summary-pop-article)
1715
1716 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1717   "k" gnus-summary-kill-thread
1718   "l" gnus-summary-lower-thread
1719   "i" gnus-summary-raise-thread
1720   "T" gnus-summary-toggle-threads
1721   "t" gnus-summary-rethread-current
1722   "^" gnus-summary-reparent-thread
1723   "s" gnus-summary-show-thread
1724   "S" gnus-summary-show-all-threads
1725   "h" gnus-summary-hide-thread
1726   "H" gnus-summary-hide-all-threads
1727   "n" gnus-summary-next-thread
1728   "p" gnus-summary-prev-thread
1729   "u" gnus-summary-up-thread
1730   "o" gnus-summary-top-thread
1731   "d" gnus-summary-down-thread
1732   "#" gnus-uu-mark-thread
1733   "\M-#" gnus-uu-unmark-thread)
1734
1735 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1736   "g" gnus-summary-prepare
1737   "c" gnus-summary-insert-cached-articles
1738   "d" gnus-summary-insert-dormant-articles)
1739
1740 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1741   "c" gnus-summary-catchup-and-exit
1742   "C" gnus-summary-catchup-all-and-exit
1743   "E" gnus-summary-exit-no-update
1744   "Q" gnus-summary-exit
1745   "Z" gnus-summary-exit
1746   "n" gnus-summary-catchup-and-goto-next-group
1747   "R" gnus-summary-reselect-current-group
1748   "G" gnus-summary-rescan-group
1749   "N" gnus-summary-next-group
1750   "s" gnus-summary-save-newsrc
1751   "P" gnus-summary-prev-group)
1752
1753 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1754   " " gnus-summary-next-page
1755   "n" gnus-summary-next-page
1756   "\177" gnus-summary-prev-page
1757   [delete] gnus-summary-prev-page
1758   "p" gnus-summary-prev-page
1759   "\r" gnus-summary-scroll-up
1760   "\M-\r" gnus-summary-scroll-down
1761   "<" gnus-summary-beginning-of-article
1762   ">" gnus-summary-end-of-article
1763   "b" gnus-summary-beginning-of-article
1764   "e" gnus-summary-end-of-article
1765   "^" gnus-summary-refer-parent-article
1766   "r" gnus-summary-refer-parent-article
1767   "D" gnus-summary-enter-digest-group
1768   "R" gnus-summary-refer-references
1769   "T" gnus-summary-refer-thread
1770   "g" gnus-summary-show-article
1771   "s" gnus-summary-isearch-article
1772   "P" gnus-summary-print-article
1773   "M" gnus-mailing-list-insinuate
1774   "t" gnus-article-babel)
1775
1776 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1777   "b" gnus-article-add-buttons
1778   "B" gnus-article-add-buttons-to-head
1779   "o" gnus-article-treat-overstrike
1780   "e" gnus-article-emphasize
1781   "w" gnus-article-fill-cited-article
1782   "Q" gnus-article-fill-long-lines
1783   "C" gnus-article-capitalize-sentences
1784   "c" gnus-article-remove-cr
1785   "q" gnus-article-de-quoted-unreadable
1786   "6" gnus-article-de-base64-unreadable
1787   "Z" gnus-article-decode-HZ
1788   "h" gnus-article-wash-html
1789   "u" gnus-article-unsplit-urls
1790   "s" gnus-summary-force-verify-and-decrypt
1791   "f" gnus-article-display-x-face
1792   "l" gnus-summary-stop-page-breaking
1793   "r" gnus-summary-caesar-message
1794   "m" gnus-summary-morse-message
1795   "t" gnus-summary-toggle-header
1796   "g" gnus-treat-smiley
1797   "v" gnus-summary-verbose-headers
1798   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1799   "p" gnus-article-verify-x-pgp-sig
1800   "d" gnus-article-treat-dumbquotes)
1801
1802 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1803   ;; mnemonic: deuglif*Y*
1804   "u" gnus-article-outlook-unwrap-lines
1805   "a" gnus-article-outlook-repair-attribution
1806   "c" gnus-article-outlook-rearrange-citation
1807   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1808
1809 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1810   "a" gnus-article-hide
1811   "h" gnus-article-hide-headers
1812   "b" gnus-article-hide-boring-headers
1813   "s" gnus-article-hide-signature
1814   "c" gnus-article-hide-citation
1815   "C" gnus-article-hide-citation-in-followups
1816   "l" gnus-article-hide-list-identifiers
1817   "p" gnus-article-hide-pgp
1818   "B" gnus-article-strip-banner
1819   "P" gnus-article-hide-pem
1820   "\C-c" gnus-article-hide-citation-maybe)
1821
1822 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1823   "a" gnus-article-highlight
1824   "h" gnus-article-highlight-headers
1825   "c" gnus-article-highlight-citation
1826   "s" gnus-article-highlight-signature)
1827
1828 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1829   "f" gnus-article-treat-fold-headers
1830   "u" gnus-article-treat-unfold-headers
1831   "n" gnus-article-treat-fold-newsgroups)
1832
1833 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1834   "x" gnus-article-display-x-face
1835   "d" gnus-article-display-face
1836   "s" gnus-treat-smiley
1837   "D" gnus-article-remove-images
1838   "f" gnus-treat-from-picon
1839   "m" gnus-treat-mail-picon
1840   "n" gnus-treat-newsgroups-picon)
1841
1842 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1843   "w" gnus-article-decode-mime-words
1844   "c" gnus-article-decode-charset
1845   "v" gnus-mime-view-all-parts
1846   "b" gnus-article-view-part)
1847
1848 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1849   "z" gnus-article-date-ut
1850   "u" gnus-article-date-ut
1851   "l" gnus-article-date-local
1852   "p" gnus-article-date-english
1853   "e" gnus-article-date-lapsed
1854   "o" gnus-article-date-original
1855   "i" gnus-article-date-iso8601
1856   "s" gnus-article-date-user)
1857
1858 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1859   "t" gnus-article-remove-trailing-blank-lines
1860   "l" gnus-article-strip-leading-blank-lines
1861   "m" gnus-article-strip-multiple-blank-lines
1862   "a" gnus-article-strip-blank-lines
1863   "A" gnus-article-strip-all-blank-lines
1864   "s" gnus-article-strip-leading-space
1865   "e" gnus-article-strip-trailing-space
1866   "w" gnus-article-remove-leading-whitespace)
1867
1868 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1869   "v" gnus-version
1870   "f" gnus-summary-fetch-faq
1871   "d" gnus-summary-describe-group
1872   "h" gnus-summary-describe-briefly
1873   "i" gnus-info-find-node
1874   "c" gnus-group-fetch-charter
1875   "C" gnus-group-fetch-control)
1876
1877 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1878   "e" gnus-summary-expire-articles
1879   "\M-\C-e" gnus-summary-expire-articles-now
1880   "\177" gnus-summary-delete-article
1881   [delete] gnus-summary-delete-article
1882   [backspace] gnus-summary-delete-article
1883   "m" gnus-summary-move-article
1884   "r" gnus-summary-respool-article
1885   "w" gnus-summary-edit-article
1886   "c" gnus-summary-copy-article
1887   "B" gnus-summary-crosspost-article
1888   "q" gnus-summary-respool-query
1889   "t" gnus-summary-respool-trace
1890   "i" gnus-summary-import-article
1891   "I" gnus-summary-create-article
1892   "p" gnus-summary-article-posted-p)
1893
1894 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1895   "o" gnus-summary-save-article
1896   "m" gnus-summary-save-article-mail
1897   "F" gnus-summary-write-article-file
1898   "r" gnus-summary-save-article-rmail
1899   "f" gnus-summary-save-article-file
1900   "b" gnus-summary-save-article-body-file
1901   "h" gnus-summary-save-article-folder
1902   "v" gnus-summary-save-article-vm
1903   "p" gnus-summary-pipe-output
1904   "P" gnus-summary-muttprint
1905   "s" gnus-soup-add-article)
1906
1907 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1908   "b" gnus-summary-display-buttonized
1909   "m" gnus-summary-repair-multipart
1910   "v" gnus-article-view-part
1911   "o" gnus-article-save-part
1912   "c" gnus-article-copy-part
1913   "C" gnus-article-view-part-as-charset
1914   "e" gnus-article-view-part-externally
1915   "E" gnus-article-encrypt-body
1916   "i" gnus-article-inline-part
1917   "|" gnus-article-pipe-part)
1918
1919 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1920   "p" gnus-summary-mark-as-processable
1921   "u" gnus-summary-unmark-as-processable
1922   "U" gnus-summary-unmark-all-processable
1923   "v" gnus-uu-mark-over
1924   "s" gnus-uu-mark-series
1925   "r" gnus-uu-mark-region
1926   "g" gnus-uu-unmark-region
1927   "R" gnus-uu-mark-by-regexp
1928   "G" gnus-uu-unmark-by-regexp
1929   "t" gnus-uu-mark-thread
1930   "T" gnus-uu-unmark-thread
1931   "a" gnus-uu-mark-all
1932   "b" gnus-uu-mark-buffer
1933   "S" gnus-uu-mark-sparse
1934   "k" gnus-summary-kill-process-mark
1935   "y" gnus-summary-yank-process-mark
1936   "w" gnus-summary-save-process-mark
1937   "i" gnus-uu-invert-processable)
1938
1939 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1940   ;;"x" gnus-uu-extract-any
1941   "m" gnus-summary-save-parts
1942   "u" gnus-uu-decode-uu
1943   "U" gnus-uu-decode-uu-and-save
1944   "s" gnus-uu-decode-unshar
1945   "S" gnus-uu-decode-unshar-and-save
1946   "o" gnus-uu-decode-save
1947   "O" gnus-uu-decode-save
1948   "b" gnus-uu-decode-binhex
1949   "B" gnus-uu-decode-binhex
1950   "p" gnus-uu-decode-postscript
1951   "P" gnus-uu-decode-postscript-and-save)
1952
1953 (gnus-define-keys
1954     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
1955   "u" gnus-uu-decode-uu-view
1956   "U" gnus-uu-decode-uu-and-save-view
1957   "s" gnus-uu-decode-unshar-view
1958   "S" gnus-uu-decode-unshar-and-save-view
1959   "o" gnus-uu-decode-save-view
1960   "O" gnus-uu-decode-save-view
1961   "b" gnus-uu-decode-binhex-view
1962   "B" gnus-uu-decode-binhex-view
1963   "p" gnus-uu-decode-postscript-view
1964   "P" gnus-uu-decode-postscript-and-save-view)
1965
1966 (defvar gnus-article-post-menu nil)
1967
1968 (defconst gnus-summary-menu-maxlen 20)
1969
1970 (defun gnus-summary-menu-split (menu)
1971   ;; If we have lots of elements, divide them into groups of 20
1972   ;; and make a pane (or submenu) for each one.
1973   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
1974       (let ((menu menu) sublists next
1975             (i 1))
1976         (while menu
1977           ;; Pull off the next gnus-summary-menu-maxlen elements
1978           ;; and make them the next element of sublist.
1979           (setq next (nthcdr gnus-summary-menu-maxlen menu))
1980           (if next
1981               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
1982                       nil))
1983           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
1984                                              (aref (car (last menu)) 0)) menu)
1985                                sublists))
1986           (setq i (1+ i))
1987           (setq menu next))
1988         (nreverse sublists))
1989     ;; Few elements--put them all in one pane.
1990     menu))
1991
1992 (defun gnus-summary-make-menu-bar ()
1993   (gnus-turn-off-edit-menu 'summary)
1994
1995   (unless (boundp 'gnus-summary-misc-menu)
1996
1997     (easy-menu-define
1998       gnus-summary-kill-menu gnus-summary-mode-map ""
1999       (cons
2000        "Score"
2001        (nconc
2002         (list
2003          ["Customize" gnus-score-customize t])
2004         (gnus-make-score-map 'increase)
2005         (gnus-make-score-map 'lower)
2006         '(("Mark"
2007            ["Kill below" gnus-summary-kill-below t]
2008            ["Mark above" gnus-summary-mark-above t]
2009            ["Tick above" gnus-summary-tick-above t]
2010            ["Clear above" gnus-summary-clear-above t])
2011           ["Current score" gnus-summary-current-score t]
2012           ["Set score" gnus-summary-set-score t]
2013           ["Switch current score file..." gnus-score-change-score-file t]
2014           ["Set mark below..." gnus-score-set-mark-below t]
2015           ["Set expunge below..." gnus-score-set-expunge-below t]
2016           ["Edit current score file" gnus-score-edit-current-scores t]
2017           ["Edit score file" gnus-score-edit-file t]
2018           ["Trace score" gnus-score-find-trace t]
2019           ["Find words" gnus-score-find-favourite-words t]
2020           ["Rescore buffer" gnus-summary-rescore t]
2021           ["Increase score..." gnus-summary-increase-score t]
2022           ["Lower score..." gnus-summary-lower-score t]))))
2023
2024     ;; Define both the Article menu in the summary buffer and the
2025     ;; equivalent Commands menu in the article buffer here for
2026     ;; consistency.
2027     (let ((innards
2028            `(("Hide"
2029               ["All" gnus-article-hide t]
2030               ["Headers" gnus-article-hide-headers t]
2031               ["Signature" gnus-article-hide-signature t]
2032               ["Citation" gnus-article-hide-citation t]
2033               ["List identifiers" gnus-article-hide-list-identifiers t]
2034               ["PGP" gnus-article-hide-pgp t]
2035               ["Banner" gnus-article-strip-banner t]
2036               ["Boring headers" gnus-article-hide-boring-headers t])
2037              ("Highlight"
2038               ["All" gnus-article-highlight t]
2039               ["Headers" gnus-article-highlight-headers t]
2040               ["Signature" gnus-article-highlight-signature t]
2041               ["Citation" gnus-article-highlight-citation t])
2042              ("MIME"
2043               ["Words" gnus-article-decode-mime-words t]
2044               ["Charset" gnus-article-decode-charset t]
2045               ["QP" gnus-article-de-quoted-unreadable t]
2046               ["Base64" gnus-article-de-base64-unreadable t]
2047               ["View MIME buttons" gnus-summary-display-buttonized t]
2048               ["View all" gnus-mime-view-all-parts t]
2049               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2050               ["Encrypt body" gnus-article-encrypt-body t]
2051               ["Extract all parts" gnus-summary-save-parts t])
2052              ("Date"
2053               ["Local" gnus-article-date-local t]
2054               ["ISO8601" gnus-article-date-iso8601 t]
2055               ["UT" gnus-article-date-ut t]
2056               ["Original" gnus-article-date-original t]
2057               ["Lapsed" gnus-article-date-lapsed t]
2058               ["User-defined" gnus-article-date-user t])
2059              ("Display"
2060               ["Remove images" gnus-article-remove-images t]
2061               ["Toggle smiley" gnus-treat-smiley t]
2062               ["Show X-Face" gnus-article-display-x-face t]
2063               ["Show picons in From" gnus-treat-from-picon t]
2064               ["Show picons in mail headers" gnus-treat-mail-picon t]
2065               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2066               ("View as different encoding"
2067                ,@(gnus-summary-menu-split
2068                   (mapcar
2069                    (lambda (cs)
2070                      ;; Since easymenu under FSF Emacs doesn't allow lambda
2071                      ;; forms for menu commands, we should provide intern'ed
2072                      ;; function symbols.
2073                      (let ((command (intern (format "\
2074 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2075                        (fset command
2076                              `(lambda ()
2077                                 (interactive)
2078                                 (let ((gnus-summary-show-article-charset-alist
2079                                        '((1 . ,cs))))
2080                                   (gnus-summary-show-article 1))))
2081                        `[,(symbol-name cs) ,command t]))
2082                    (sort (if (fboundp 'coding-system-list)
2083                              (coding-system-list)
2084                            (mapcar 'car mm-mime-mule-charset-alist))
2085                          'string<)))))
2086              ("Washing"
2087               ("Remove Blanks"
2088                ["Leading" gnus-article-strip-leading-blank-lines t]
2089                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2090                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2091                ["All of the above" gnus-article-strip-blank-lines t]
2092                ["All" gnus-article-strip-all-blank-lines t]
2093                ["Leading space" gnus-article-strip-leading-space t]
2094                ["Trailing space" gnus-article-strip-trailing-space t]
2095                ["Leading space in headers"
2096                 gnus-article-remove-leading-whitespace t])
2097               ["Overstrike" gnus-article-treat-overstrike t]
2098               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2099               ["Emphasis" gnus-article-emphasize t]
2100               ["Word wrap" gnus-article-fill-cited-article t]
2101               ["Fill long lines" gnus-article-fill-long-lines t]
2102               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2103               ["CR" gnus-article-remove-cr t]
2104               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2105               ["Base64" gnus-article-de-base64-unreadable t]
2106               ["Rot 13" gnus-summary-caesar-message
2107                ,@(if (featurep 'xemacs) '(t)
2108                    '(:help "\"Caesar rotate\" article by 13"))]
2109               ["Morse decode" gnus-summary-morse-message t]
2110               ["Unix pipe..." gnus-summary-pipe-message t]
2111               ["Add buttons" gnus-article-add-buttons t]
2112               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2113               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2114               ["Verbose header" gnus-summary-verbose-headers t]
2115               ["Toggle header" gnus-summary-toggle-header t]
2116               ["Unfold headers" gnus-article-treat-unfold-headers t]
2117               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2118               ["Html" gnus-article-wash-html t]
2119               ["URLs" gnus-article-unsplit-urls t]
2120               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2121               ["HZ" gnus-article-decode-HZ t]
2122               ("(Outlook) Deuglify"
2123                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2124                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2125                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2126                ["Full (Outlook) deuglify"
2127                 gnus-article-outlook-deuglify-article t])
2128               )
2129              ("Output"
2130               ["Save in default format" gnus-summary-save-article
2131                ,@(if (featurep 'xemacs) '(t)
2132                    '(:help "Save article using default method"))]
2133               ["Save in file" gnus-summary-save-article-file
2134                ,@(if (featurep 'xemacs) '(t)
2135                    '(:help "Save article in file"))]
2136               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2137               ["Save in MH folder" gnus-summary-save-article-folder t]
2138               ["Save in VM folder" gnus-summary-save-article-vm t]
2139               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2140               ["Save body in file" gnus-summary-save-article-body-file t]
2141               ["Pipe through a filter" gnus-summary-pipe-output t]
2142               ["Add to SOUP packet" gnus-soup-add-article t]
2143               ["Print with Muttprint" gnus-summary-muttprint t]
2144               ["Print" gnus-summary-print-article t])
2145              ("Backend"
2146               ["Respool article..." gnus-summary-respool-article t]
2147               ["Move article..." gnus-summary-move-article
2148                (gnus-check-backend-function
2149                 'request-move-article gnus-newsgroup-name)]
2150               ["Copy article..." gnus-summary-copy-article t]
2151               ["Crosspost article..." gnus-summary-crosspost-article
2152                (gnus-check-backend-function
2153                 'request-replace-article gnus-newsgroup-name)]
2154               ["Import file..." gnus-summary-import-article t]
2155               ["Create article..." gnus-summary-create-article t]
2156               ["Check if posted" gnus-summary-article-posted-p t]
2157               ["Edit article" gnus-summary-edit-article
2158                (not (gnus-group-read-only-p))]
2159               ["Delete article" gnus-summary-delete-article
2160                (gnus-check-backend-function
2161                 'request-expire-articles gnus-newsgroup-name)]
2162               ["Query respool" gnus-summary-respool-query t]
2163               ["Trace respool" gnus-summary-respool-trace t]
2164               ["Delete expirable articles" gnus-summary-expire-articles-now
2165                (gnus-check-backend-function
2166                 'request-expire-articles gnus-newsgroup-name)])
2167              ("Extract"
2168               ["Uudecode" gnus-uu-decode-uu
2169                ,@(if (featurep 'xemacs) '(t)
2170                    '(:help "Decode uuencoded article(s)"))]
2171               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2172               ["Unshar" gnus-uu-decode-unshar t]
2173               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2174               ["Save" gnus-uu-decode-save t]
2175               ["Binhex" gnus-uu-decode-binhex t]
2176               ["Postscript" gnus-uu-decode-postscript t]
2177               ["All MIME parts" gnus-summary-save-parts t])
2178              ("Cache"
2179               ["Enter article" gnus-cache-enter-article t]
2180               ["Remove article" gnus-cache-remove-article t])
2181              ["Translate" gnus-article-babel t]
2182              ["Select article buffer" gnus-summary-select-article-buffer t]
2183              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2184              ["Isearch article..." gnus-summary-isearch-article t]
2185              ["Beginning of the article" gnus-summary-beginning-of-article t]
2186              ["End of the article" gnus-summary-end-of-article t]
2187              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2188              ["Fetch referenced articles" gnus-summary-refer-references t]
2189              ["Fetch current thread" gnus-summary-refer-thread t]
2190              ["Fetch article with id..." gnus-summary-refer-article t]
2191              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2192              ["Redisplay" gnus-summary-show-article t]
2193              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2194       (easy-menu-define
2195         gnus-summary-article-menu gnus-summary-mode-map ""
2196         (cons "Article" innards))
2197
2198       (if (not (keymapp gnus-summary-article-menu))
2199           (easy-menu-define
2200             gnus-article-commands-menu gnus-article-mode-map ""
2201             (cons "Commands" innards))
2202         ;; in Emacs, don't share menu.
2203         (setq gnus-article-commands-menu
2204               (copy-keymap gnus-summary-article-menu))
2205         (define-key gnus-article-mode-map [menu-bar commands]
2206           (cons "Commands" gnus-article-commands-menu))))
2207
2208     (easy-menu-define
2209       gnus-summary-thread-menu gnus-summary-mode-map ""
2210       '("Threads"
2211         ["Find all messages in thread" gnus-summary-refer-thread t]
2212         ["Toggle threading" gnus-summary-toggle-threads t]
2213         ["Hide threads" gnus-summary-hide-all-threads t]
2214         ["Show threads" gnus-summary-show-all-threads t]
2215         ["Hide thread" gnus-summary-hide-thread t]
2216         ["Show thread" gnus-summary-show-thread t]
2217         ["Go to next thread" gnus-summary-next-thread t]
2218         ["Go to previous thread" gnus-summary-prev-thread t]
2219         ["Go down thread" gnus-summary-down-thread t]
2220         ["Go up thread" gnus-summary-up-thread t]
2221         ["Top of thread" gnus-summary-top-thread t]
2222         ["Mark thread as read" gnus-summary-kill-thread t]
2223         ["Lower thread score" gnus-summary-lower-thread t]
2224         ["Raise thread score" gnus-summary-raise-thread t]
2225         ["Rethread current" gnus-summary-rethread-current t]))
2226
2227     (easy-menu-define
2228       gnus-summary-post-menu gnus-summary-mode-map ""
2229       `("Post"
2230         ["Send a message (mail or news)" gnus-summary-post-news
2231          ,@(if (featurep 'xemacs) '(t)
2232              '(:help "Post an article"))]
2233         ["Followup" gnus-summary-followup
2234          ,@(if (featurep 'xemacs) '(t)
2235              '(:help "Post followup to this article"))]
2236         ["Followup and yank" gnus-summary-followup-with-original
2237          ,@(if (featurep 'xemacs) '(t)
2238              '(:help "Post followup to this article, quoting its contents"))]
2239         ["Supersede article" gnus-summary-supersede-article t]
2240         ["Cancel article" gnus-summary-cancel-article
2241          ,@(if (featurep 'xemacs) '(t)
2242              '(:help "Cancel an article you posted"))]
2243         ["Reply" gnus-summary-reply t]
2244         ["Reply and yank" gnus-summary-reply-with-original t]
2245         ["Wide reply" gnus-summary-wide-reply t]
2246         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2247          ,@(if (featurep 'xemacs) '(t)
2248              '(:help "Mail a reply, quoting this article"))]
2249         ["Very wide reply" gnus-summary-very-wide-reply t]
2250         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2251          ,@(if (featurep 'xemacs) '(t)
2252              '(:help "Mail a very wide reply, quoting this article"))]
2253         ["Mail forward" gnus-summary-mail-forward t]
2254         ["Post forward" gnus-summary-post-forward t]
2255         ["Digest and mail" gnus-uu-digest-mail-forward t]
2256         ["Digest and post" gnus-uu-digest-post-forward t]
2257         ["Resend message" gnus-summary-resend-message t]
2258         ["Resend message edit" gnus-summary-resend-message-edit t]
2259         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2260         ["Send a mail" gnus-summary-mail-other-window t]
2261         ["Create a local message" gnus-summary-news-other-window t]
2262         ["Uuencode and post" gnus-uu-post-news
2263          ,@(if (featurep 'xemacs) '(t)
2264              '(:help "Post a uuencoded article"))]
2265         ["Followup via news" gnus-summary-followup-to-mail t]
2266         ["Followup via news and yank"
2267          gnus-summary-followup-to-mail-with-original t]
2268         ;;("Draft"
2269         ;;["Send" gnus-summary-send-draft t]
2270         ;;["Send bounced" gnus-resend-bounced-mail t])
2271         ))
2272
2273     (cond
2274      ((not (keymapp gnus-summary-post-menu))
2275       (setq gnus-article-post-menu gnus-summary-post-menu))
2276      ((not gnus-article-post-menu)
2277       ;; Don't share post menu.
2278       (setq gnus-article-post-menu
2279             (copy-keymap gnus-summary-post-menu))))
2280     (define-key gnus-article-mode-map [menu-bar post]
2281       (cons "Post" gnus-article-post-menu))
2282
2283     (easy-menu-define
2284       gnus-summary-misc-menu gnus-summary-mode-map ""
2285       `("Gnus"
2286         ("Mark Read"
2287          ["Mark as read" gnus-summary-mark-as-read-forward t]
2288          ["Mark same subject and select"
2289           gnus-summary-kill-same-subject-and-select t]
2290          ["Mark same subject" gnus-summary-kill-same-subject t]
2291          ["Catchup" gnus-summary-catchup
2292           ,@(if (featurep 'xemacs) '(t)
2293               '(:help "Mark unread articles in this group as read"))]
2294          ["Catchup all" gnus-summary-catchup-all t]
2295          ["Catchup to here" gnus-summary-catchup-to-here t]
2296          ["Catchup from here" gnus-summary-catchup-from-here t]
2297          ["Catchup region" gnus-summary-mark-region-as-read 
2298           (gnus-mark-active-p)]
2299          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2300         ("Mark Various"
2301          ["Tick" gnus-summary-tick-article-forward t]
2302          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2303          ["Remove marks" gnus-summary-clear-mark-forward t]
2304          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2305          ["Set bookmark" gnus-summary-set-bookmark t]
2306          ["Remove bookmark" gnus-summary-remove-bookmark t])
2307         ("Limit to"
2308          ["Marks..." gnus-summary-limit-to-marks t]
2309          ["Subject..." gnus-summary-limit-to-subject t]
2310          ["Author..." gnus-summary-limit-to-author t]
2311          ["Age..." gnus-summary-limit-to-age t]
2312          ["Extra..." gnus-summary-limit-to-extra t]
2313          ["Score..." gnus-summary-limit-to-score t]
2314          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2315          ["Unread" gnus-summary-limit-to-unread t]
2316          ["Unseen" gnus-summary-limit-to-unseen t]
2317          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2318          ["Next articles" gnus-summary-limit-to-articles t]
2319          ["Pop limit" gnus-summary-pop-limit t]
2320          ["Show dormant" gnus-summary-limit-include-dormant t]
2321          ["Hide childless dormant"
2322           gnus-summary-limit-exclude-childless-dormant t]
2323          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2324          ["Hide marked" gnus-summary-limit-exclude-marks t]
2325          ["Show expunged" gnus-summary-limit-include-expunged t])
2326         ("Process Mark"
2327          ["Set mark" gnus-summary-mark-as-processable t]
2328          ["Remove mark" gnus-summary-unmark-as-processable t]
2329          ["Remove all marks" gnus-summary-unmark-all-processable t]
2330          ["Mark above" gnus-uu-mark-over t]
2331          ["Mark series" gnus-uu-mark-series t]
2332          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2333          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2334          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2335          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2336          ["Mark all" gnus-uu-mark-all t]
2337          ["Mark buffer" gnus-uu-mark-buffer t]
2338          ["Mark sparse" gnus-uu-mark-sparse t]
2339          ["Mark thread" gnus-uu-mark-thread t]
2340          ["Unmark thread" gnus-uu-unmark-thread t]
2341          ("Process Mark Sets"
2342           ["Kill" gnus-summary-kill-process-mark t]
2343           ["Yank" gnus-summary-yank-process-mark
2344            gnus-newsgroup-process-stack]
2345           ["Save" gnus-summary-save-process-mark t]))
2346         ("Scroll article"
2347          ["Page forward" gnus-summary-next-page
2348           ,@(if (featurep 'xemacs) '(t)
2349               '(:help "Show next page of article"))]
2350          ["Page backward" gnus-summary-prev-page
2351           ,@(if (featurep 'xemacs) '(t)
2352               '(:help "Show previous page of article"))]
2353          ["Line forward" gnus-summary-scroll-up t])
2354         ("Move"
2355          ["Next unread article" gnus-summary-next-unread-article t]
2356          ["Previous unread article" gnus-summary-prev-unread-article t]
2357          ["Next article" gnus-summary-next-article t]
2358          ["Previous article" gnus-summary-prev-article t]
2359          ["Next unread subject" gnus-summary-next-unread-subject t]
2360          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2361          ["Next article same subject" gnus-summary-next-same-subject t]
2362          ["Previous article same subject" gnus-summary-prev-same-subject t]
2363          ["First unread article" gnus-summary-first-unread-article t]
2364          ["Best unread article" gnus-summary-best-unread-article t]
2365          ["Go to subject number..." gnus-summary-goto-subject t]
2366          ["Go to article number..." gnus-summary-goto-article t]
2367          ["Go to the last article" gnus-summary-goto-last-article t]
2368          ["Pop article off history" gnus-summary-pop-article t])
2369         ("Sort"
2370          ["Sort by number" gnus-summary-sort-by-number t]
2371          ["Sort by author" gnus-summary-sort-by-author t]
2372          ["Sort by subject" gnus-summary-sort-by-subject t]
2373          ["Sort by date" gnus-summary-sort-by-date t]
2374          ["Sort by score" gnus-summary-sort-by-score t]
2375          ["Sort by lines" gnus-summary-sort-by-lines t]
2376          ["Sort by characters" gnus-summary-sort-by-chars t]
2377          ["Randomize" gnus-summary-sort-by-random t]
2378          ["Original sort" gnus-summary-sort-by-original t])
2379         ("Help"
2380          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2381          ["Describe group" gnus-summary-describe-group t]
2382          ["Fetch charter" gnus-group-fetch-charter
2383           ,@(if (featurep 'xemacs) nil
2384               '(:help "Display the charter of the current group"))]
2385          ["Fetch control message" gnus-group-fetch-control
2386           ,@(if (featurep 'xemacs) nil
2387               '(:help "Display the archived control message for the current group"))]
2388          ["Read manual" gnus-info-find-node t])
2389         ("Modes"
2390          ["Pick and read" gnus-pick-mode t]
2391          ["Binary" gnus-binary-mode t])
2392         ("Regeneration"
2393          ["Regenerate" gnus-summary-prepare t]
2394          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2395          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2396          ["Toggle threading" gnus-summary-toggle-threads t])
2397         ["See old articles" gnus-summary-insert-old-articles t]
2398         ["See new articles" gnus-summary-insert-new-articles t]
2399         ["Filter articles..." gnus-summary-execute-command t]
2400         ["Run command on subjects..." gnus-summary-universal-argument t]
2401         ["Search articles forward..." gnus-summary-search-article-forward t]
2402         ["Search articles backward..." gnus-summary-search-article-backward t]
2403         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2404         ["Expand window" gnus-summary-expand-window t]
2405         ["Expire expirable articles" gnus-summary-expire-articles
2406          (gnus-check-backend-function
2407           'request-expire-articles gnus-newsgroup-name)]
2408         ["Edit local kill file" gnus-summary-edit-local-kill t]
2409         ["Edit main kill file" gnus-summary-edit-global-kill t]
2410         ["Edit group parameters" gnus-summary-edit-parameters t]
2411         ["Customize group parameters" gnus-summary-customize-parameters t]
2412         ["Send a bug report" gnus-bug t]
2413         ("Exit"
2414          ["Catchup and exit" gnus-summary-catchup-and-exit
2415           ,@(if (featurep 'xemacs) '(t)
2416               '(:help "Mark unread articles in this group as read, then exit"))]
2417          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2418          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2419          ["Exit group" gnus-summary-exit
2420           ,@(if (featurep 'xemacs) '(t)
2421               '(:help "Exit current group, return to group selection mode"))]
2422          ["Exit group without updating" gnus-summary-exit-no-update t]
2423          ["Exit and goto next group" gnus-summary-next-group t]
2424          ["Exit and goto prev group" gnus-summary-prev-group t]
2425          ["Reselect group" gnus-summary-reselect-current-group t]
2426          ["Rescan group" gnus-summary-rescan-group t]
2427          ["Update dribble" gnus-summary-save-newsrc t])))
2428
2429     (gnus-run-hooks 'gnus-summary-menu-hook)))
2430
2431 (defvar gnus-summary-tool-bar-map nil)
2432
2433 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2434 (defun gnus-summary-make-tool-bar ()
2435   (if (and (fboundp 'tool-bar-add-item-from-menu)
2436            (default-value 'tool-bar-mode)
2437            (not gnus-summary-tool-bar-map))
2438       (setq gnus-summary-tool-bar-map
2439             (let ((tool-bar-map (make-sparse-keymap))
2440                   (load-path (mm-image-load-path)))
2441               (tool-bar-add-item-from-menu
2442                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2443               (tool-bar-add-item-from-menu
2444                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2445               (tool-bar-add-item-from-menu
2446                'gnus-summary-post-news "post" gnus-summary-mode-map)
2447               (tool-bar-add-item-from-menu
2448                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2449               (tool-bar-add-item-from-menu
2450                'gnus-summary-followup "followup" gnus-summary-mode-map)
2451               (tool-bar-add-item-from-menu
2452                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2453               (tool-bar-add-item-from-menu
2454                'gnus-summary-reply "reply" gnus-summary-mode-map)
2455               (tool-bar-add-item-from-menu
2456                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2457               (tool-bar-add-item-from-menu
2458                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2459               (tool-bar-add-item-from-menu
2460                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2461               (tool-bar-add-item-from-menu
2462                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2463               (tool-bar-add-item-from-menu
2464                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2465               (tool-bar-add-item-from-menu
2466                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2467               (tool-bar-add-item-from-menu
2468                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2469               (tool-bar-add-item-from-menu
2470                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2471               tool-bar-map)))
2472   (if gnus-summary-tool-bar-map
2473       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2474
2475 (defun gnus-score-set-default (var value)
2476   "A version of set that updates the GNU Emacs menu-bar."
2477   (set var value)
2478   ;; It is the message that forces the active status to be updated.
2479   (message ""))
2480
2481 (defun gnus-make-score-map (type)
2482   "Make a summary score map of type TYPE."
2483   (if t
2484       nil
2485     (let ((headers '(("author" "from" string)
2486                      ("subject" "subject" string)
2487                      ("article body" "body" string)
2488                      ("article head" "head" string)
2489                      ("xref" "xref" string)
2490                      ("extra header" "extra" string)
2491                      ("lines" "lines" number)
2492                      ("followups to author" "followup" string)))
2493           (types '((number ("less than" <)
2494                            ("greater than" >)
2495                            ("equal" =))
2496                    (string ("substring" s)
2497                            ("exact string" e)
2498                            ("fuzzy string" f)
2499                            ("regexp" r))))
2500           (perms '(("temporary" (current-time-string))
2501                    ("permanent" nil)
2502                    ("immediate" now)))
2503           header)
2504       (list
2505        (apply
2506         'nconc
2507         (list
2508          (if (eq type 'lower)
2509              "Lower score"
2510            "Increase score"))
2511         (let (outh)
2512           (while headers
2513             (setq header (car headers))
2514             (setq outh
2515                   (cons
2516                    (apply
2517                     'nconc
2518                     (list (car header))
2519                     (let ((ts (cdr (assoc (nth 2 header) types)))
2520                           outt)
2521                       (while ts
2522                         (setq outt
2523                               (cons
2524                                (apply
2525                                 'nconc
2526                                 (list (caar ts))
2527                                 (let ((ps perms)
2528                                       outp)
2529                                   (while ps
2530                                     (setq outp
2531                                           (cons
2532                                            (vector
2533                                             (caar ps)
2534                                             (list
2535                                              'gnus-summary-score-entry
2536                                              (nth 1 header)
2537                                              (if (or (string= (nth 1 header)
2538                                                               "head")
2539                                                      (string= (nth 1 header)
2540                                                               "body"))
2541                                                  ""
2542                                                (list 'gnus-summary-header
2543                                                      (nth 1 header)))
2544                                              (list 'quote (nth 1 (car ts)))
2545                                              (list 'gnus-score-delta-default
2546                                                    nil)
2547                                              (nth 1 (car ps))
2548                                              t)
2549                                             t)
2550                                            outp))
2551                                     (setq ps (cdr ps)))
2552                                   (list (nreverse outp))))
2553                                outt))
2554                         (setq ts (cdr ts)))
2555                       (list (nreverse outt))))
2556                    outh))
2557             (setq headers (cdr headers)))
2558           (list (nreverse outh))))))))
2559
2560 \f
2561
2562 (defun gnus-summary-mode (&optional group)
2563   "Major mode for reading articles.
2564
2565 All normal editing commands are switched off.
2566 \\<gnus-summary-mode-map>
2567 Each line in this buffer represents one article.  To read an
2568 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2569 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2570 respectively.
2571
2572 You can also post articles and send mail from this buffer.  To
2573 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2574 of an article, type `\\[gnus-summary-reply]'.
2575
2576 There are approx. one gazillion commands you can execute in this
2577 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2578
2579 The following commands are available:
2580
2581 \\{gnus-summary-mode-map}"
2582   (interactive)
2583   (kill-all-local-variables)
2584   (when (gnus-visual-p 'summary-menu 'menu)
2585     (gnus-summary-make-menu-bar)
2586     (gnus-summary-make-tool-bar))
2587   (gnus-summary-make-local-variables)
2588   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2589     (gnus-summary-make-local-variables))
2590   (gnus-make-thread-indent-array)
2591   (gnus-simplify-mode-line)
2592   (setq major-mode 'gnus-summary-mode)
2593   (setq mode-name "Summary")
2594   (make-local-variable 'minor-mode-alist)
2595   (use-local-map gnus-summary-mode-map)
2596   (buffer-disable-undo)
2597   (setq buffer-read-only t)             ;Disable modification
2598   (setq truncate-lines t)
2599   (setq selective-display t)
2600   (setq selective-display-ellipses t)   ;Display `...'
2601   (gnus-summary-set-display-table)
2602   (gnus-set-default-directory)
2603   (setq gnus-newsgroup-name group)
2604   (make-local-variable 'gnus-summary-line-format)
2605   (make-local-variable 'gnus-summary-line-format-spec)
2606   (make-local-variable 'gnus-summary-dummy-line-format)
2607   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2608   (make-local-variable 'gnus-summary-mark-positions)
2609   (make-local-hook 'pre-command-hook)
2610   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2611   (gnus-run-hooks 'gnus-summary-mode-hook)
2612   (turn-on-gnus-mailing-list-mode)
2613   (mm-enable-multibyte)
2614   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2615   (gnus-update-summary-mark-positions))
2616
2617 (defun gnus-summary-make-local-variables ()
2618   "Make all the local summary buffer variables."
2619   (let (global)
2620     (dolist (local gnus-summary-local-variables)
2621       (if (consp local)
2622           (progn
2623             (if (eq (cdr local) 'global)
2624                 ;; Copy the global value of the variable.
2625                 (setq global (symbol-value (car local)))
2626               ;; Use the value from the list.
2627               (setq global (eval (cdr local))))
2628             (set (make-local-variable (car local)) global))
2629         ;; Simple nil-valued local variable.
2630         (set (make-local-variable local) nil)))))
2631
2632 (defun gnus-summary-clear-local-variables ()
2633   (let ((locals gnus-summary-local-variables))
2634     (while locals
2635       (if (consp (car locals))
2636           (and (vectorp (caar locals))
2637                (set (caar locals) nil))
2638         (and (vectorp (car locals))
2639              (set (car locals) nil)))
2640       (setq locals (cdr locals)))))
2641
2642 ;; Summary data functions.
2643
2644 (defmacro gnus-data-number (data)
2645   `(car ,data))
2646
2647 (defmacro gnus-data-set-number (data number)
2648   `(setcar ,data ,number))
2649
2650 (defmacro gnus-data-mark (data)
2651   `(nth 1 ,data))
2652
2653 (defmacro gnus-data-set-mark (data mark)
2654   `(setcar (nthcdr 1 ,data) ,mark))
2655
2656 (defmacro gnus-data-pos (data)
2657   `(nth 2 ,data))
2658
2659 (defmacro gnus-data-set-pos (data pos)
2660   `(setcar (nthcdr 2 ,data) ,pos))
2661
2662 (defmacro gnus-data-header (data)
2663   `(nth 3 ,data))
2664
2665 (defmacro gnus-data-set-header (data header)
2666   `(setf (nth 3 ,data) ,header))
2667
2668 (defmacro gnus-data-level (data)
2669   `(nth 4 ,data))
2670
2671 (defmacro gnus-data-unread-p (data)
2672   `(= (nth 1 ,data) gnus-unread-mark))
2673
2674 (defmacro gnus-data-read-p (data)
2675   `(/= (nth 1 ,data) gnus-unread-mark))
2676
2677 (defmacro gnus-data-pseudo-p (data)
2678   `(consp (nth 3 ,data)))
2679
2680 (defmacro gnus-data-find (number)
2681   `(assq ,number gnus-newsgroup-data))
2682
2683 (defmacro gnus-data-find-list (number &optional data)
2684   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2685      (memq (assq ,number bdata)
2686            bdata)))
2687
2688 (defmacro gnus-data-make (number mark pos header level)
2689   `(list ,number ,mark ,pos ,header ,level))
2690
2691 (defun gnus-data-enter (after-article number mark pos header level offset)
2692   (let ((data (gnus-data-find-list after-article)))
2693     (unless data
2694       (error "No such article: %d" after-article))
2695     (setcdr data (cons (gnus-data-make number mark pos header level)
2696                        (cdr data)))
2697     (setq gnus-newsgroup-data-reverse nil)
2698     (gnus-data-update-list (cddr data) offset)))
2699
2700 (defun gnus-data-enter-list (after-article list &optional offset)
2701   (when list
2702     (let ((data (and after-article (gnus-data-find-list after-article)))
2703           (ilist list))
2704       (if (not (or data
2705                    after-article))
2706           (let ((odata gnus-newsgroup-data))
2707             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2708             (when offset
2709               (gnus-data-update-list odata offset)))
2710       ;; Find the last element in the list to be spliced into the main
2711         ;; list.
2712         (while (cdr list)
2713           (setq list (cdr list)))
2714         (if (not data)
2715             (progn
2716               (setcdr list gnus-newsgroup-data)
2717               (setq gnus-newsgroup-data ilist)
2718               (when offset
2719                 (gnus-data-update-list (cdr list) offset)))
2720           (setcdr list (cdr data))
2721           (setcdr data ilist)
2722           (when offset
2723             (gnus-data-update-list (cdr list) offset))))
2724       (setq gnus-newsgroup-data-reverse nil))))
2725
2726 (defun gnus-data-remove (article &optional offset)
2727   (let ((data gnus-newsgroup-data))
2728     (if (= (gnus-data-number (car data)) article)
2729         (progn
2730           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2731                 gnus-newsgroup-data-reverse nil)
2732           (when offset
2733             (gnus-data-update-list gnus-newsgroup-data offset)))
2734       (while (cdr data)
2735         (when (= (gnus-data-number (cadr data)) article)
2736           (setcdr data (cddr data))
2737           (when offset
2738             (gnus-data-update-list (cdr data) offset))
2739           (setq data nil
2740                 gnus-newsgroup-data-reverse nil))
2741         (setq data (cdr data))))))
2742
2743 (defmacro gnus-data-list (backward)
2744   `(if ,backward
2745        (or gnus-newsgroup-data-reverse
2746            (setq gnus-newsgroup-data-reverse
2747                  (reverse gnus-newsgroup-data)))
2748      gnus-newsgroup-data))
2749
2750 (defun gnus-data-update-list (data offset)
2751   "Add OFFSET to the POS of all data entries in DATA."
2752   (setq gnus-newsgroup-data-reverse nil)
2753   (while data
2754     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2755     (setq data (cdr data))))
2756
2757 (defun gnus-summary-article-pseudo-p (article)
2758   "Say whether this article is a pseudo article or not."
2759   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2760
2761 (defmacro gnus-summary-article-sparse-p (article)
2762   "Say whether this article is a sparse article or not."
2763   `(memq ,article gnus-newsgroup-sparse))
2764
2765 (defmacro gnus-summary-article-ancient-p (article)
2766   "Say whether this article is a sparse article or not."
2767   `(memq ,article gnus-newsgroup-ancient))
2768
2769 (defun gnus-article-parent-p (number)
2770   "Say whether this article is a parent or not."
2771   (let ((data (gnus-data-find-list number)))
2772     (and (cdr data)              ; There has to be an article after...
2773          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2774             (gnus-data-level (nth 1 data))))))
2775
2776 (defun gnus-article-children (number)
2777   "Return a list of all children to NUMBER."
2778   (let* ((data (gnus-data-find-list number))
2779          (level (gnus-data-level (car data)))
2780          children)
2781     (setq data (cdr data))
2782     (while (and data
2783                 (= (gnus-data-level (car data)) (1+ level)))
2784       (push (gnus-data-number (car data)) children)
2785       (setq data (cdr data)))
2786     children))
2787
2788 (defmacro gnus-summary-skip-intangible ()
2789   "If the current article is intangible, then jump to a different article."
2790   '(let ((to (get-text-property (point) 'gnus-intangible)))
2791      (and to (gnus-summary-goto-subject to))))
2792
2793 (defmacro gnus-summary-article-intangible-p ()
2794   "Say whether this article is intangible or not."
2795   '(get-text-property (point) 'gnus-intangible))
2796
2797 (defun gnus-article-read-p (article)
2798   "Say whether ARTICLE is read or not."
2799   (not (or (memq article gnus-newsgroup-marked)
2800            (memq article gnus-newsgroup-spam-marked)
2801            (memq article gnus-newsgroup-unreads)
2802            (memq article gnus-newsgroup-unselected)
2803            (memq article gnus-newsgroup-dormant))))
2804
2805 ;; Some summary mode macros.
2806
2807 (defmacro gnus-summary-article-number ()
2808   "The article number of the article on the current line.
2809 If there isn't an article number here, then we return the current
2810 article number."
2811   '(progn
2812      (gnus-summary-skip-intangible)
2813      (or (get-text-property (point) 'gnus-number)
2814          (gnus-summary-last-subject))))
2815
2816 (defmacro gnus-summary-article-header (&optional number)
2817   "Return the header of article NUMBER."
2818   `(gnus-data-header (gnus-data-find
2819                       ,(or number '(gnus-summary-article-number)))))
2820
2821 (defmacro gnus-summary-thread-level (&optional number)
2822   "Return the level of thread that starts with article NUMBER."
2823   `(if (and (eq gnus-summary-make-false-root 'dummy)
2824             (get-text-property (point) 'gnus-intangible))
2825        0
2826      (gnus-data-level (gnus-data-find
2827                        ,(or number '(gnus-summary-article-number))))))
2828
2829 (defmacro gnus-summary-article-mark (&optional number)
2830   "Return the mark of article NUMBER."
2831   `(gnus-data-mark (gnus-data-find
2832                     ,(or number '(gnus-summary-article-number)))))
2833
2834 (defmacro gnus-summary-article-pos (&optional number)
2835   "Return the position of the line of article NUMBER."
2836   `(gnus-data-pos (gnus-data-find
2837                    ,(or number '(gnus-summary-article-number)))))
2838
2839 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2840 (defmacro gnus-summary-article-subject (&optional number)
2841   "Return current subject string or nil if nothing."
2842   `(let ((headers
2843           ,(if number
2844                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2845              '(gnus-data-header (assq (gnus-summary-article-number)
2846                                       gnus-newsgroup-data)))))
2847      (and headers
2848           (vectorp headers)
2849           (mail-header-subject headers))))
2850
2851 (defmacro gnus-summary-article-score (&optional number)
2852   "Return current article score."
2853   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2854                   gnus-newsgroup-scored))
2855        gnus-summary-default-score 0))
2856
2857 (defun gnus-summary-article-children (&optional number)
2858   "Return a list of article numbers that are children of article NUMBER."
2859   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2860          (level (gnus-data-level (car data)))
2861          l children)
2862     (while (and (setq data (cdr data))
2863                 (> (setq l (gnus-data-level (car data))) level))
2864       (and (= (1+ level) l)
2865            (push (gnus-data-number (car data))
2866                  children)))
2867     (nreverse children)))
2868
2869 (defun gnus-summary-article-parent (&optional number)
2870   "Return the article number of the parent of article NUMBER."
2871   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2872                                     (gnus-data-list t)))
2873          (level (gnus-data-level (car data))))
2874     (if (zerop level)
2875         ()                              ; This is a root.
2876       ;; We search until we find an article with a level less than
2877       ;; this one.  That function has to be the parent.
2878       (while (and (setq data (cdr data))
2879                   (not (< (gnus-data-level (car data)) level))))
2880       (and data (gnus-data-number (car data))))))
2881
2882 (defun gnus-unread-mark-p (mark)
2883   "Say whether MARK is the unread mark."
2884   (= mark gnus-unread-mark))
2885
2886 (defun gnus-read-mark-p (mark)
2887   "Say whether MARK is one of the marks that mark as read.
2888 This is all marks except unread, ticked, dormant, and expirable."
2889   (not (or (= mark gnus-unread-mark)
2890            (= mark gnus-ticked-mark)
2891            (= mark gnus-spam-mark)
2892            (= mark gnus-dormant-mark)
2893            (= mark gnus-expirable-mark))))
2894
2895 (defmacro gnus-article-mark (number)
2896   "Return the MARK of article NUMBER.
2897 This macro should only be used when computing the mark the \"first\"
2898 time; i.e., when generating the summary lines.  After that,
2899 `gnus-summary-article-mark' should be used to examine the
2900 marks of articles."
2901   `(cond
2902     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2903     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2904     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2905     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2906     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2907     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2908     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2909     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2910            gnus-ancient-mark))))
2911
2912 ;; Saving hidden threads.
2913
2914 (defmacro gnus-save-hidden-threads (&rest forms)
2915   "Save hidden threads, eval FORMS, and restore the hidden threads."
2916   (let ((config (make-symbol "config")))
2917     `(let ((,config (gnus-hidden-threads-configuration)))
2918        (unwind-protect
2919            (save-excursion
2920              ,@forms)
2921          (gnus-restore-hidden-threads-configuration ,config)))))
2922 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2923 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2924
2925 (defun gnus-data-compute-positions ()
2926   "Compute the positions of all articles."
2927   (setq gnus-newsgroup-data-reverse nil)
2928   (let ((data gnus-newsgroup-data))
2929     (save-excursion
2930       (gnus-save-hidden-threads
2931         (gnus-summary-show-all-threads)
2932         (goto-char (point-min))
2933         (while data
2934           (while (get-text-property (point) 'gnus-intangible)
2935             (forward-line 1))
2936           (gnus-data-set-pos (car data) (+ (point) 3))
2937           (setq data (cdr data))
2938           (forward-line 1))))))
2939
2940 (defun gnus-hidden-threads-configuration ()
2941   "Return the current hidden threads configuration."
2942   (save-excursion
2943     (let (config)
2944       (goto-char (point-min))
2945       (while (search-forward "\r" nil t)
2946         (push (1- (point)) config))
2947       config)))
2948
2949 (defun gnus-restore-hidden-threads-configuration (config)
2950   "Restore hidden threads configuration from CONFIG."
2951   (save-excursion
2952     (let (point buffer-read-only)
2953       (while (setq point (pop config))
2954         (when (and (< point (point-max))
2955                    (goto-char point)
2956                    (eq (char-after) ?\n))
2957           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2958
2959 ;; Various summary mode internalish functions.
2960
2961 (defun gnus-mouse-pick-article (e)
2962   (interactive "e")
2963   (mouse-set-point e)
2964   (gnus-summary-next-page nil t))
2965
2966 (defun gnus-summary-set-display-table ()
2967   "Change the display table.
2968 Odd characters have a tendency to mess
2969 up nicely formatted displays - we make all possible glyphs
2970 display only a single character."
2971
2972   ;; We start from the standard display table, if any.
2973   (let ((table (or (copy-sequence standard-display-table)
2974                    (make-display-table)))
2975         (i 32))
2976     ;; Nix out all the control chars...
2977     (while (>= (setq i (1- i)) 0)
2978       (aset table i [??]))
2979    ;; ... but not newline and cr, of course.  (cr is necessary for the
2980     ;; selective display).
2981     (aset table ?\n nil)
2982     (aset table ?\r nil)
2983     ;; We keep TAB as well.
2984     (aset table ?\t nil)
2985     ;; We nix out any glyphs over 126 that are not set already.
2986     (let ((i 256))
2987       (while (>= (setq i (1- i)) 127)
2988         ;; Only modify if the entry is nil.
2989         (unless (aref table i)
2990           (aset table i [??]))))
2991     (setq buffer-display-table table)))
2992
2993 (defun gnus-summary-set-article-display-arrow (pos)
2994   "Update the overlay arrow to point to line at position POS."
2995   (when (and gnus-summary-display-arrow
2996              (boundp 'overlay-arrow-position)
2997              (boundp 'overlay-arrow-string))
2998     (save-excursion
2999       (goto-char pos)
3000       (beginning-of-line)
3001       (unless overlay-arrow-position
3002         (setq overlay-arrow-position (make-marker)))
3003       (setq overlay-arrow-string "=>"
3004             overlay-arrow-position (set-marker overlay-arrow-position
3005                                                (point)
3006                                                (current-buffer))))))
3007
3008 (defun gnus-summary-buffer-name (group)
3009   "Return the summary buffer name of GROUP."
3010   (concat "*Summary " (gnus-group-decoded-name group) "*"))
3011
3012 (defun gnus-summary-setup-buffer (group)
3013   "Initialize summary buffer."
3014   (let ((buffer (gnus-summary-buffer-name group))
3015         (dead-name (concat "*Dead Summary "
3016                            (gnus-group-decoded-name group) "*")))
3017     ;; If a dead summary buffer exists, we kill it.
3018     (when (gnus-buffer-live-p dead-name)
3019       (gnus-kill-buffer dead-name))
3020     (if (get-buffer buffer)
3021         (progn
3022           (set-buffer buffer)
3023           (setq gnus-summary-buffer (current-buffer))
3024           (not gnus-newsgroup-prepared))
3025       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3026       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3027       (gnus-summary-mode group)
3028       (when gnus-carpal
3029         (gnus-carpal-setup-buffer 'summary))
3030       (unless gnus-single-article-buffer
3031         (make-local-variable 'gnus-article-buffer)
3032         (make-local-variable 'gnus-article-current)
3033         (make-local-variable 'gnus-original-article-buffer))
3034       (setq gnus-newsgroup-name group)
3035       ;; Set any local variables in the group parameters.
3036       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3037       t)))
3038
3039 (defun gnus-set-global-variables ()
3040   "Set the global equivalents of the buffer-local variables.
3041 They are set to the latest values they had.  These reflect the summary
3042 buffer that was in action when the last article was fetched."
3043   (when (eq major-mode 'gnus-summary-mode)
3044     (setq gnus-summary-buffer (current-buffer))
3045     (let ((name gnus-newsgroup-name)
3046           (marked gnus-newsgroup-marked)
3047           (spam gnus-newsgroup-spam-marked)
3048           (unread gnus-newsgroup-unreads)
3049           (headers gnus-current-headers)
3050           (data gnus-newsgroup-data)
3051           (summary gnus-summary-buffer)
3052           (article-buffer gnus-article-buffer)
3053           (original gnus-original-article-buffer)
3054           (gac gnus-article-current)
3055           (reffed gnus-reffed-article-number)
3056           (score-file gnus-current-score-file)
3057           (default-charset gnus-newsgroup-charset)
3058           vlist)
3059       (let ((locals gnus-newsgroup-variables))
3060         (while locals
3061           (if (consp (car locals))
3062               (push (eval (caar locals)) vlist)
3063             (push (eval (car locals)) vlist))
3064           (setq locals (cdr locals)))
3065         (setq vlist (nreverse vlist)))
3066       (save-excursion
3067         (set-buffer gnus-group-buffer)
3068         (setq gnus-newsgroup-name name
3069               gnus-newsgroup-marked marked
3070               gnus-newsgroup-spam-marked spam
3071               gnus-newsgroup-unreads unread
3072               gnus-current-headers headers
3073               gnus-newsgroup-data data
3074               gnus-article-current gac
3075               gnus-summary-buffer summary
3076               gnus-article-buffer article-buffer
3077               gnus-original-article-buffer original
3078               gnus-reffed-article-number reffed
3079               gnus-current-score-file score-file
3080               gnus-newsgroup-charset default-charset)
3081         (let ((locals gnus-newsgroup-variables))
3082           (while locals
3083             (if (consp (car locals))
3084                 (set (caar locals) (pop vlist))
3085               (set (car locals) (pop vlist)))
3086             (setq locals (cdr locals))))
3087         ;; The article buffer also has local variables.
3088         (when (gnus-buffer-live-p gnus-article-buffer)
3089           (set-buffer gnus-article-buffer)
3090           (setq gnus-summary-buffer summary))))))
3091
3092 (defun gnus-summary-article-unread-p (article)
3093   "Say whether ARTICLE is unread or not."
3094   (memq article gnus-newsgroup-unreads))
3095
3096 (defun gnus-summary-first-article-p (&optional article)
3097   "Return whether ARTICLE is the first article in the buffer."
3098   (if (not (setq article (or article (gnus-summary-article-number))))
3099       nil
3100     (eq article (caar gnus-newsgroup-data))))
3101
3102 (defun gnus-summary-last-article-p (&optional article)
3103   "Return whether ARTICLE is the last article in the buffer."
3104   (if (not (setq article (or article (gnus-summary-article-number))))
3105       ;; All non-existent numbers are the last article.  :-)
3106       t
3107     (not (cdr (gnus-data-find-list article)))))
3108
3109 (defun gnus-make-thread-indent-array ()
3110   (let ((n 200))
3111     (unless (and gnus-thread-indent-array
3112                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3113       (setq gnus-thread-indent-array (make-vector 201 "")
3114             gnus-thread-indent-array-level gnus-thread-indent-level)
3115       (while (>= n 0)
3116         (aset gnus-thread-indent-array n
3117               (make-string (* n gnus-thread-indent-level) ? ))
3118         (setq n (1- n))))))
3119
3120 (defun gnus-update-summary-mark-positions ()
3121   "Compute where the summary marks are to go."
3122   (save-excursion
3123     (when (gnus-buffer-exists-p gnus-summary-buffer)
3124       (set-buffer gnus-summary-buffer))
3125     (let ((gnus-replied-mark 129)
3126           (gnus-score-below-mark 130)
3127           (gnus-score-over-mark 130)
3128           (gnus-undownloaded-mark 131)
3129           (spec gnus-summary-line-format-spec)
3130           gnus-visual pos)
3131       (save-excursion
3132         (gnus-set-work-buffer)
3133         (let ((gnus-summary-line-format-spec spec)
3134               (gnus-newsgroup-downloadable '(0)))
3135           (gnus-summary-insert-line
3136            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3137            0 nil t 128 t nil "" nil 1)
3138           (goto-char (point-min))
3139           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3140                                              (- (point) (point-min) 1)))))
3141           (goto-char (point-min))
3142           (push (cons 'replied (and (search-forward "\201" nil t)
3143                                     (- (point) (point-min) 1)))
3144                 pos)
3145           (goto-char (point-min))
3146           (push (cons 'score (and (search-forward "\202" nil t)
3147                                   (- (point) (point-min) 1)))
3148                 pos)
3149           (goto-char (point-min))
3150           (push (cons 'download
3151                       (and (search-forward "\203" nil t)
3152                            (- (point) (point-min) 1)))
3153                 pos)))
3154       (setq gnus-summary-mark-positions pos))))
3155
3156 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3157   "Insert a dummy root in the summary buffer."
3158   (beginning-of-line)
3159   (gnus-add-text-properties
3160    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3161    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3162
3163 (defun gnus-summary-extract-address-component (from)
3164   (or (car (funcall gnus-extract-address-components from))
3165       from))
3166
3167 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3168   (let ((mail-parse-charset gnus-newsgroup-charset)
3169         ; Is it really necessary to do this next part for each summary line?
3170         ; Luckily, doesn't seem to slow things down much.
3171         (mail-parse-ignored-charsets
3172          (save-excursion (set-buffer gnus-summary-buffer)
3173                          gnus-newsgroup-ignored-charsets)))
3174     (or
3175      (and gnus-ignored-from-addresses
3176           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3177           (let ((extra-headers (mail-header-extra header))
3178                 to
3179                 newsgroups)
3180             (cond
3181              ((setq to (cdr (assq 'To extra-headers)))
3182               (concat "-> "
3183                       (inline
3184                         (gnus-summary-extract-address-component
3185                          (funcall gnus-decode-encoded-word-function to)))))
3186              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3187               (concat "=> " newsgroups)))))
3188      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3189
3190 (defun gnus-summary-insert-line (gnus-tmp-header
3191                                  gnus-tmp-level gnus-tmp-current
3192                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3193                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3194                                  &optional gnus-tmp-dummy gnus-tmp-score
3195                                  gnus-tmp-process)
3196   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3197          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3198          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3199          (gnus-tmp-score-char
3200           (if (or (null gnus-summary-default-score)
3201                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3202                       gnus-summary-zcore-fuzz))
3203               ?                         ;Whitespace
3204             (if (< gnus-tmp-score gnus-summary-default-score)
3205                 gnus-score-below-mark gnus-score-over-mark)))
3206          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3207          (gnus-tmp-replied
3208           (cond (gnus-tmp-process gnus-process-mark)
3209                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3210                  gnus-cached-mark)
3211                 (gnus-tmp-replied gnus-replied-mark)
3212                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3213                  gnus-forwarded-mark)
3214                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3215                  gnus-saved-mark)
3216                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3217                  gnus-recent-mark)
3218                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3219                  gnus-unseen-mark)
3220                 (t gnus-no-mark)))
3221          (gnus-tmp-downloaded
3222           (cond (undownloaded 
3223                  gnus-undownloaded-mark)
3224                 (gnus-newsgroup-agentized
3225                  gnus-downloaded-mark)
3226                 (t
3227                  gnus-no-mark)))
3228          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3229          (gnus-tmp-name
3230           (cond
3231            ((string-match "<[^>]+> *$" gnus-tmp-from)
3232             (let ((beg (match-beginning 0)))
3233               (or (and (string-match "^\".+\"" gnus-tmp-from)
3234                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3235                   (substring gnus-tmp-from 0 beg))))
3236            ((string-match "(.+)" gnus-tmp-from)
3237             (substring gnus-tmp-from
3238                        (1+ (match-beginning 0)) (1- (match-end 0))))
3239            (t gnus-tmp-from)))
3240          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3241          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3242          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3243          (buffer-read-only nil))
3244     (when (string= gnus-tmp-name "")
3245       (setq gnus-tmp-name gnus-tmp-from))
3246     (unless (numberp gnus-tmp-lines)
3247       (setq gnus-tmp-lines -1))
3248     (if (= gnus-tmp-lines -1)
3249         (setq gnus-tmp-lines "?")
3250       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3251     (gnus-put-text-property
3252      (point)
3253      (progn (eval gnus-summary-line-format-spec) (point))
3254      'gnus-number gnus-tmp-number)
3255     (when (gnus-visual-p 'summary-highlight 'highlight)
3256       (forward-line -1)
3257       (gnus-run-hooks 'gnus-summary-update-hook)
3258       (forward-line 1))))
3259
3260 (defun gnus-summary-update-line (&optional dont-update)
3261   "Update summary line after change."
3262   (when (and gnus-summary-default-score
3263              (not gnus-summary-inhibit-highlight))
3264     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3265            (article (gnus-summary-article-number))
3266            (score (gnus-summary-article-score article)))
3267       (unless dont-update
3268         (if (and gnus-summary-mark-below
3269                  (< (gnus-summary-article-score)
3270                     gnus-summary-mark-below))
3271             ;; This article has a low score, so we mark it as read.
3272             (when (memq article gnus-newsgroup-unreads)
3273               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3274           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3275             ;; This article was previously marked as read on account
3276             ;; of a low score, but now it has risen, so we mark it as
3277             ;; unread.
3278             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3279         (gnus-summary-update-mark
3280          (if (or (null gnus-summary-default-score)
3281                  (<= (abs (- score gnus-summary-default-score))
3282                      gnus-summary-zcore-fuzz))
3283              ?                          ;Whitespace
3284            (if (< score gnus-summary-default-score)
3285                gnus-score-below-mark gnus-score-over-mark))
3286          'score))
3287       ;; Do visual highlighting.
3288       (when (gnus-visual-p 'summary-highlight 'highlight)
3289         (gnus-run-hooks 'gnus-summary-update-hook)))))
3290
3291 (defvar gnus-tmp-new-adopts nil)
3292
3293 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3294   "Return the number of articles in THREAD.
3295 This may be 0 in some cases -- if none of the articles in
3296 the thread are to be displayed."
3297   (let* ((number
3298          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3299           (cond
3300            ((not (listp thread))
3301             1)
3302            ((and (consp thread) (cdr thread))
3303             (apply
3304              '+ 1 (mapcar
3305                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3306            ((null thread)
3307             1)
3308            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3309             1)
3310            (t 0))))
3311     (when (and level (zerop level) gnus-tmp-new-adopts)
3312       (incf number
3313             (apply '+ (mapcar
3314                        'gnus-summary-number-of-articles-in-thread
3315                        gnus-tmp-new-adopts))))
3316     (if char
3317         (if (> number 1) gnus-not-empty-thread-mark
3318           gnus-empty-thread-mark)
3319       number)))
3320
3321 (defsubst gnus-summary-line-message-size (head)
3322   "Return pretty-printed version of message size.
3323 This function is intended to be used in
3324 `gnus-summary-line-format-alist', which see."
3325   (let ((c (or (mail-header-chars head) -1)))
3326     (cond ((< c 0) "n/a")               ; chars not available
3327           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3328           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3329           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3330           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3331
3332
3333 (defun gnus-summary-set-local-parameters (group)
3334   "Go through the local params of GROUP and set all variable specs in that list."
3335   (let ((params (gnus-group-find-parameter group))
3336         (vars '(quit-config))           ; Ignore quit-config.
3337         elem)
3338     (while params
3339       (setq elem (car params)
3340             params (cdr params))
3341       (and (consp elem)                 ; Has to be a cons.
3342            (consp (cdr elem))           ; The cdr has to be a list.
3343            (symbolp (car elem))         ; Has to be a symbol in there.
3344            (not (memq (car elem) vars))
3345            (ignore-errors               ; So we set it.
3346              (push (car elem) vars)
3347              (make-local-variable (car elem))
3348              (set (car elem) (eval (nth 1 elem))))))))
3349
3350 (defun gnus-summary-read-group (group &optional show-all no-article
3351                                       kill-buffer no-display backward
3352                                       select-articles)
3353   "Start reading news in newsgroup GROUP.
3354 If SHOW-ALL is non-nil, already read articles are also listed.
3355 If NO-ARTICLE is non-nil, no article is selected initially.
3356 If NO-DISPLAY, don't generate a summary buffer."
3357   (let (result)
3358     (while (and group
3359                 (null (setq result
3360                             (let ((gnus-auto-select-next nil))
3361                               (or (gnus-summary-read-group-1
3362                                    group show-all no-article
3363                                    kill-buffer no-display
3364                                    select-articles)
3365                                   (setq show-all nil
3366                                         select-articles nil)))))
3367                 (eq gnus-auto-select-next 'quietly))
3368       (set-buffer gnus-group-buffer)
3369       ;; The entry function called above goes to the next
3370       ;; group automatically, so we go two groups back
3371       ;; if we are searching for the previous group.
3372       (when backward
3373         (gnus-group-prev-unread-group 2))
3374       (if (not (equal group (gnus-group-group-name)))
3375           (setq group (gnus-group-group-name))
3376         (setq group nil)))
3377     result))
3378
3379 (defun gnus-summary-read-group-1 (group show-all no-article
3380                                         kill-buffer no-display
3381                                         &optional select-articles)
3382   ;; Killed foreign groups can't be entered.
3383   ;;  (when (and (not (gnus-group-native-p group))
3384   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3385   ;;    (error "Dead non-native groups can't be entered"))
3386   (gnus-message 5 "Retrieving newsgroup: %s..."
3387                 (gnus-group-decoded-name group))
3388   (let* ((new-group (gnus-summary-setup-buffer group))
3389          (quit-config (gnus-group-quit-config group))
3390          (did-select (and new-group (gnus-select-newsgroup
3391                                      group show-all select-articles))))
3392     (cond
3393      ;; This summary buffer exists already, so we just select it.
3394      ((not new-group)
3395       (gnus-set-global-variables)
3396       (when kill-buffer
3397         (gnus-kill-or-deaden-summary kill-buffer))
3398       (gnus-configure-windows 'summary 'force)
3399       (gnus-set-mode-line 'summary)
3400       (gnus-summary-position-point)
3401       (message "")
3402       t)
3403      ;; We couldn't select this group.
3404      ((null did-select)
3405       (when (and (eq major-mode 'gnus-summary-mode)
3406                  (not (equal (current-buffer) kill-buffer)))
3407         (kill-buffer (current-buffer))
3408         (if (not quit-config)
3409             (progn
3410               ;; Update the info -- marks might need to be removed,
3411               ;; for instance.
3412               (gnus-summary-update-info)
3413               (set-buffer gnus-group-buffer)
3414               (gnus-group-jump-to-group group)
3415               (gnus-group-next-unread-group 1))
3416           (gnus-handle-ephemeral-exit quit-config)))
3417       (let ((grpinfo (gnus-get-info group)))
3418         (if (null (gnus-info-read grpinfo))
3419             (gnus-message 3 "Group %s contains no messages"
3420                           (gnus-group-decoded-name group))
3421           (gnus-message 3 "Can't select group")))
3422       nil)
3423      ;; The user did a `C-g' while prompting for number of articles,
3424      ;; so we exit this group.
3425      ((eq did-select 'quit)
3426       (and (eq major-mode 'gnus-summary-mode)
3427            (not (equal (current-buffer) kill-buffer))
3428            (kill-buffer (current-buffer)))
3429       (when kill-buffer
3430         (gnus-kill-or-deaden-summary kill-buffer))
3431       (if (not quit-config)
3432           (progn
3433             (set-buffer gnus-group-buffer)
3434             (gnus-group-jump-to-group group)
3435             (gnus-group-next-unread-group 1)
3436             (gnus-configure-windows 'group 'force))
3437         (gnus-handle-ephemeral-exit quit-config))
3438       ;; Finally signal the quit.
3439       (signal 'quit nil))
3440      ;; The group was successfully selected.
3441      (t
3442       (gnus-set-global-variables)
3443       ;; Save the active value in effect when the group was entered.
3444       (setq gnus-newsgroup-active
3445             (gnus-copy-sequence
3446              (gnus-active gnus-newsgroup-name)))
3447       ;; You can change the summary buffer in some way with this hook.
3448       (gnus-run-hooks 'gnus-select-group-hook)
3449       (gnus-update-format-specifications
3450        nil 'summary 'summary-mode 'summary-dummy)
3451       (gnus-update-summary-mark-positions)
3452       ;; Do score processing.
3453       (when gnus-use-scoring
3454         (gnus-possibly-score-headers))
3455       ;; Check whether to fill in the gaps in the threads.
3456       (when gnus-build-sparse-threads
3457         (gnus-build-sparse-threads))
3458       ;; Find the initial limit.
3459       (if gnus-show-threads
3460           (if show-all
3461               (let ((gnus-newsgroup-dormant nil))
3462                 (gnus-summary-initial-limit show-all))
3463             (gnus-summary-initial-limit show-all))
3464         ;; When unthreaded, all articles are always shown.
3465         (setq gnus-newsgroup-limit
3466               (mapcar
3467                (lambda (header) (mail-header-number header))
3468                gnus-newsgroup-headers)))
3469       ;; Generate the summary buffer.
3470       (unless no-display
3471         (gnus-summary-prepare))
3472       (when gnus-use-trees
3473         (gnus-tree-open group)
3474         (setq gnus-summary-highlight-line-function
3475               'gnus-tree-highlight-article))
3476       ;; If the summary buffer is empty, but there are some low-scored
3477       ;; articles or some excluded dormants, we include these in the
3478       ;; buffer.
3479       (when (and (zerop (buffer-size))
3480                  (not no-display))
3481         (cond (gnus-newsgroup-dormant
3482                (gnus-summary-limit-include-dormant))
3483               ((and gnus-newsgroup-scored show-all)
3484                (gnus-summary-limit-include-expunged t))))
3485       ;; Function `gnus-apply-kill-file' must be called in this hook.
3486       (gnus-run-hooks 'gnus-apply-kill-hook)
3487       (if (and (zerop (buffer-size))
3488                (not no-display))
3489           (progn
3490             ;; This newsgroup is empty.
3491             (gnus-summary-catchup-and-exit nil t)
3492             (gnus-message 6 "No unread news")
3493             (when kill-buffer
3494               (gnus-kill-or-deaden-summary kill-buffer))
3495             ;; Return nil from this function.
3496             nil)
3497         ;; Hide conversation thread subtrees.  We cannot do this in
3498         ;; gnus-summary-prepare-hook since kill processing may not
3499         ;; work with hidden articles.
3500         (gnus-summary-maybe-hide-threads)
3501         (when kill-buffer
3502           (gnus-kill-or-deaden-summary kill-buffer))
3503         (gnus-summary-auto-select-subject)
3504         ;; Show first unread article if requested.
3505         (if (and (not no-article)
3506                  (not no-display)
3507                  gnus-newsgroup-unreads
3508                  gnus-auto-select-first)
3509             (progn
3510               (gnus-configure-windows 'summary)
3511               (let ((art (gnus-summary-article-number)))
3512                 (unless (and (not gnus-plugged)
3513                              (or (memq art gnus-newsgroup-undownloaded)
3514                                  (memq art gnus-newsgroup-downloadable)))
3515                   (gnus-summary-goto-article art))))
3516           ;; Don't select any articles.
3517           (gnus-summary-position-point)
3518           (gnus-configure-windows 'summary 'force)
3519           (gnus-set-mode-line 'summary))
3520         (when (get-buffer-window gnus-group-buffer t)
3521           ;; Gotta use windows, because recenter does weird stuff if
3522           ;; the current buffer ain't the displayed window.
3523           (let ((owin (selected-window)))
3524             (select-window (get-buffer-window gnus-group-buffer t))
3525             (when (gnus-group-goto-group group)
3526               (recenter))
3527             (select-window owin)))
3528         ;; Mark this buffer as "prepared".
3529         (setq gnus-newsgroup-prepared t)
3530         (gnus-run-hooks 'gnus-summary-prepared-hook)
3531         (unless (gnus-ephemeral-group-p group)
3532           (gnus-group-update-group group))
3533         t)))))
3534
3535 (defun gnus-summary-auto-select-subject ()
3536   "Select the subject line on initial group entry."
3537   (goto-char (point-min))
3538   (cond
3539    ((eq gnus-auto-select-subject 'best)
3540     (gnus-summary-best-unread-subject))
3541    ((eq gnus-auto-select-subject 'unread)
3542     (gnus-summary-first-unread-subject))
3543    ((eq gnus-auto-select-subject 'unseen)
3544     (gnus-summary-first-unseen-subject))
3545    ((eq gnus-auto-select-subject 'unseen-or-unread)
3546     (gnus-summary-first-unseen-or-unread-subject))
3547    ((eq gnus-auto-select-subject 'first)
3548     ;; Do nothing.
3549     )
3550    ((gnus-functionp gnus-auto-select-subject)
3551     (funcall gnus-auto-select-subject))))
3552
3553 (defun gnus-summary-prepare ()
3554   "Generate the summary buffer."
3555   (interactive)
3556   (let ((buffer-read-only nil))
3557     (erase-buffer)
3558     (setq gnus-newsgroup-data nil
3559           gnus-newsgroup-data-reverse nil)
3560     (gnus-run-hooks 'gnus-summary-generate-hook)
3561     ;; Generate the buffer, either with threads or without.
3562     (when gnus-newsgroup-headers
3563       (gnus-summary-prepare-threads
3564        (if gnus-show-threads
3565            (gnus-sort-gathered-threads
3566             (funcall gnus-summary-thread-gathering-function
3567                      (gnus-sort-threads
3568                       (gnus-cut-threads (gnus-make-threads)))))
3569          ;; Unthreaded display.
3570          (gnus-sort-articles gnus-newsgroup-headers))))
3571     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3572     ;; Call hooks for modifying summary buffer.
3573     (goto-char (point-min))
3574     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3575
3576 (defsubst gnus-general-simplify-subject (subject)
3577   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3578   (setq subject
3579         (cond
3580          ;; Truncate the subject.
3581          (gnus-simplify-subject-functions
3582           (gnus-map-function gnus-simplify-subject-functions subject))
3583          ((numberp gnus-summary-gather-subject-limit)
3584           (setq subject (gnus-simplify-subject-re subject))
3585           (if (> (length subject) gnus-summary-gather-subject-limit)
3586               (substring subject 0 gnus-summary-gather-subject-limit)
3587             subject))
3588          ;; Fuzzily simplify it.
3589          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3590           (gnus-simplify-subject-fuzzy subject))
3591          ;; Just remove the leading "Re:".
3592          (t
3593           (gnus-simplify-subject-re subject))))
3594
3595   (if (and gnus-summary-gather-exclude-subject
3596            (string-match gnus-summary-gather-exclude-subject subject))
3597       nil                         ; This article shouldn't be gathered
3598     subject))
3599
3600 (defun gnus-summary-simplify-subject-query ()
3601   "Query where the respool algorithm would put this article."
3602   (interactive)
3603   (gnus-summary-select-article)
3604   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3605
3606 (defun gnus-gather-threads-by-subject (threads)
3607   "Gather threads by looking at Subject headers."
3608   (if (not gnus-summary-make-false-root)
3609       threads
3610     (let ((hashtb (gnus-make-hashtable 1024))
3611           (prev threads)
3612           (result threads)
3613           subject hthread whole-subject)
3614       (while threads
3615         (setq subject (gnus-general-simplify-subject
3616                        (setq whole-subject (mail-header-subject
3617                                             (caar threads)))))
3618         (when subject
3619           (if (setq hthread (gnus-gethash subject hashtb))
3620               (progn
3621                 ;; We enter a dummy root into the thread, if we
3622                 ;; haven't done that already.
3623                 (unless (stringp (caar hthread))
3624                   (setcar hthread (list whole-subject (car hthread))))
3625                 ;; We add this new gathered thread to this gathered
3626                 ;; thread.
3627                 (setcdr (car hthread)
3628                         (nconc (cdar hthread) (list (car threads))))
3629                 ;; Remove it from the list of threads.
3630                 (setcdr prev (cdr threads))
3631                 (setq threads prev))
3632             ;; Enter this thread into the hash table.
3633             (gnus-sethash subject
3634                           (if gnus-summary-make-false-root-always
3635                               (progn
3636                                 ;; If you want a dummy root above all
3637                                 ;; threads...
3638                                 (setcar threads (list whole-subject
3639                                                       (car threads)))
3640                                 threads)
3641                             threads)
3642                           hashtb)))
3643         (setq prev threads)
3644         (setq threads (cdr threads)))
3645       result)))
3646
3647 (defun gnus-gather-threads-by-references (threads)
3648   "Gather threads by looking at References headers."
3649   (let ((idhashtb (gnus-make-hashtable 1024))
3650         (thhashtb (gnus-make-hashtable 1024))
3651         (prev threads)
3652         (result threads)
3653         ids references id gthread gid entered ref)
3654     (while threads
3655       (when (setq references (mail-header-references (caar threads)))
3656         (setq id (mail-header-id (caar threads))
3657               ids (inline (gnus-split-references references))
3658               entered nil)
3659         (while (setq ref (pop ids))
3660           (setq ids (delete ref ids))
3661           (if (not (setq gid (gnus-gethash ref idhashtb)))
3662               (progn
3663                 (gnus-sethash ref id idhashtb)
3664                 (gnus-sethash id threads thhashtb))
3665             (setq gthread (gnus-gethash gid thhashtb))
3666             (unless entered
3667               ;; We enter a dummy root into the thread, if we
3668               ;; haven't done that already.
3669               (unless (stringp (caar gthread))
3670                 (setcar gthread (list (mail-header-subject (caar gthread))
3671                                       (car gthread))))
3672               ;; We add this new gathered thread to this gathered
3673               ;; thread.
3674               (setcdr (car gthread)
3675                       (nconc (cdar gthread) (list (car threads)))))
3676             ;; Add it into the thread hash table.
3677             (gnus-sethash id gthread thhashtb)
3678             (setq entered t)
3679             ;; Remove it from the list of threads.
3680             (setcdr prev (cdr threads))
3681             (setq threads prev))))
3682       (setq prev threads)
3683       (setq threads (cdr threads)))
3684     result))
3685
3686 (defun gnus-sort-gathered-threads (threads)
3687   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3688   (let ((result threads))
3689     (while threads
3690       (when (stringp (caar threads))
3691         (setcdr (car threads)
3692                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3693       (setq threads (cdr threads)))
3694     result))
3695
3696 (defun gnus-thread-loop-p (root thread)
3697   "Say whether ROOT is in THREAD."
3698   (let ((stack (list thread))
3699         (infloop 0)
3700         th)
3701     (while (setq thread (pop stack))
3702       (setq th (cdr thread))
3703       (while (and th
3704                   (not (eq (caar th) root)))
3705         (pop th))
3706       (if th
3707           ;; We have found a loop.
3708           (let (ref-dep)
3709             (setcdr thread (delq (car th) (cdr thread)))
3710             (if (boundp (setq ref-dep (intern "none"
3711                                               gnus-newsgroup-dependencies)))
3712                 (setcdr (symbol-value ref-dep)
3713                         (nconc (cdr (symbol-value ref-dep))
3714                                (list (car th))))
3715               (set ref-dep (list nil (car th))))
3716             (setq infloop 1
3717                   stack nil))
3718         ;; Push all the subthreads onto the stack.
3719         (push (cdr thread) stack)))
3720     infloop))
3721
3722 (defun gnus-make-threads ()
3723   "Go through the dependency hashtb and find the roots.  Return all threads."
3724   (let (threads)
3725     (while (catch 'infloop
3726              (mapatoms
3727               (lambda (refs)
3728                 ;; Deal with self-referencing References loops.
3729                 (when (and (car (symbol-value refs))
3730                            (not (zerop
3731                                  (apply
3732                                   '+
3733                                   (mapcar
3734                                    (lambda (thread)
3735                                      (gnus-thread-loop-p
3736                                       (car (symbol-value refs)) thread))
3737                                    (cdr (symbol-value refs)))))))
3738                   (setq threads nil)
3739                   (throw 'infloop t))
3740                 (unless (car (symbol-value refs))
3741                   ;; These threads do not refer back to any other
3742                   ;; articles, so they're roots.
3743                   (setq threads (append (cdr (symbol-value refs)) threads))))
3744               gnus-newsgroup-dependencies)))
3745     threads))
3746
3747 ;; Build the thread tree.
3748 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3749   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3750
3751 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3752 if it was already present.
3753
3754 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3755 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3756 Message-IDs will be renamed to a unique Message-ID before being
3757 entered.
3758
3759 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3760   (let* ((id (mail-header-id header))
3761          (id-dep (and id (intern id dependencies)))
3762          parent-id ref ref-dep ref-header replaced)
3763     ;; Enter this `header' in the `dependencies' table.
3764     (cond
3765      ((not id-dep)
3766       (setq header nil))
3767      ;; The first two cases do the normal part: enter a new `header'
3768      ;; in the `dependencies' table.
3769      ((not (boundp id-dep))
3770       (set id-dep (list header)))
3771      ((null (car (symbol-value id-dep)))
3772       (setcar (symbol-value id-dep) header))
3773
3774      ;; From here the `header' was already present in the
3775      ;; `dependencies' table.
3776      (force-new
3777       ;; Overrides an existing entry;
3778       ;; just set the header part of the entry.
3779       (setcar (symbol-value id-dep) header)
3780       (setq replaced t))
3781
3782      ;; Renames the existing `header' to a unique Message-ID.
3783      ((not gnus-summary-ignore-duplicates)
3784       ;; An article with this Message-ID has already been seen.
3785       ;; We rename the Message-ID.
3786       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3787            (list header))
3788       (mail-header-set-id header id))
3789
3790      ;; The last case ignores an existing entry, except it adds any
3791      ;; additional Xrefs (in case the two articles came from different
3792      ;; servers.
3793      ;; Also sets `header' to `nil' meaning that the `dependencies'
3794      ;; table was *not* modified.
3795      (t
3796       (mail-header-set-xref
3797        (car (symbol-value id-dep))
3798        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3799                    "")
3800                (or (mail-header-xref header) "")))
3801       (setq header nil)))
3802
3803     (when (and header (not replaced))
3804       ;; First check that we are not creating a References loop.
3805       (setq parent-id (gnus-parent-id (mail-header-references header)))
3806       (setq ref parent-id)
3807       (while (and ref
3808                   (setq ref-dep (intern-soft ref dependencies))
3809                   (boundp ref-dep)
3810                   (setq ref-header (car (symbol-value ref-dep))))
3811         (if (string= id ref)
3812             ;; Yuk!  This is a reference loop.  Make the article be a
3813             ;; root article.
3814             (progn
3815               (mail-header-set-references (car (symbol-value id-dep)) "none")
3816               (setq ref nil)
3817               (setq parent-id nil))
3818           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3819       (setq ref-dep (intern (or parent-id "none") dependencies))
3820       (if (boundp ref-dep)
3821           (setcdr (symbol-value ref-dep)
3822                   (nconc (cdr (symbol-value ref-dep))
3823                          (list (symbol-value id-dep))))
3824         (set ref-dep (list nil (symbol-value id-dep)))))
3825     header))
3826
3827 (defun gnus-extract-message-id-from-in-reply-to (string)
3828   (if (string-match "<[^>]+>" string)
3829       (substring string (match-beginning 0) (match-end 0))
3830     nil))
3831
3832 (defun gnus-build-sparse-threads ()
3833   (let ((headers gnus-newsgroup-headers)
3834         (mail-parse-charset gnus-newsgroup-charset)
3835         (gnus-summary-ignore-duplicates t)
3836         header references generation relations
3837         subject child end new-child date)
3838     ;; First we create an alist of generations/relations, where
3839     ;; generations is how much we trust the relation, and the relation
3840     ;; is parent/child.
3841     (gnus-message 7 "Making sparse threads...")
3842     (save-excursion
3843       (nnheader-set-temp-buffer " *gnus sparse threads*")
3844       (while (setq header (pop headers))
3845         (when (and (setq references (mail-header-references header))
3846                    (not (string= references "")))
3847           (insert references)
3848           (setq child (mail-header-id header)
3849                 subject (mail-header-subject header)
3850                 date (mail-header-date header)
3851                 generation 0)
3852           (while (search-backward ">" nil t)
3853             (setq end (1+ (point)))
3854             (when (search-backward "<" nil t)
3855               (setq new-child (buffer-substring (point) end))
3856               (push (list (incf generation)
3857                           child (setq child new-child)
3858                           subject date)
3859                     relations)))
3860           (when child
3861             (push (list (1+ generation) child nil subject) relations))
3862           (erase-buffer)))
3863       (kill-buffer (current-buffer)))
3864     ;; Sort over trustworthiness.
3865     (mapcar
3866      (lambda (relation)
3867        (when (gnus-dependencies-add-header
3868               (make-full-mail-header
3869                gnus-reffed-article-number
3870                (nth 3 relation) "" (or (nth 4 relation) "")
3871                (nth 1 relation)
3872                (or (nth 2 relation) "") 0 0 "")
3873               gnus-newsgroup-dependencies nil)
3874          (push gnus-reffed-article-number gnus-newsgroup-limit)
3875          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3876          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3877                gnus-newsgroup-reads)
3878          (decf gnus-reffed-article-number)))
3879      (sort relations 'car-less-than-car))
3880     (gnus-message 7 "Making sparse threads...done")))
3881
3882 (defun gnus-build-old-threads ()
3883   ;; Look at all the articles that refer back to old articles, and
3884   ;; fetch the headers for the articles that aren't there.  This will
3885   ;; build complete threads - if the roots haven't been expired by the
3886   ;; server, that is.
3887   (let ((mail-parse-charset gnus-newsgroup-charset)
3888         id heads)
3889     (mapatoms
3890      (lambda (refs)
3891        (when (not (car (symbol-value refs)))
3892          (setq heads (cdr (symbol-value refs)))
3893          (while heads
3894            (if (memq (mail-header-number (caar heads))
3895                      gnus-newsgroup-dormant)
3896                (setq heads (cdr heads))
3897              (setq id (symbol-name refs))
3898              (while (and (setq id (gnus-build-get-header id))
3899                          (not (car (gnus-id-to-thread id)))))
3900              (setq heads nil)))))
3901      gnus-newsgroup-dependencies)))
3902
3903 ;; This function has to be called with point after the article number
3904 ;; on the beginning of the line.
3905 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3906   (let ((eol (gnus-point-at-eol))
3907         (buffer (current-buffer))
3908         header references in-reply-to)
3909
3910     ;; overview: [num subject from date id refs chars lines misc]
3911     (unwind-protect
3912         (let (x)
3913           (narrow-to-region (point) eol)
3914           (unless (eobp)
3915             (forward-char))
3916
3917           (setq header
3918                 (make-full-mail-header
3919                  number                 ; number
3920                  (condition-case ()     ; subject
3921                      (funcall gnus-decode-encoded-word-function
3922                               (setq x (nnheader-nov-field)))
3923                    (error x))
3924                  (condition-case ()     ; from
3925                      (funcall gnus-decode-encoded-word-function
3926                               (setq x (nnheader-nov-field)))
3927                    (error x))
3928                  (nnheader-nov-field)   ; date
3929                  (nnheader-nov-read-message-id) ; id
3930                  (setq references (nnheader-nov-field)) ; refs
3931                  (nnheader-nov-read-integer) ; chars
3932                  (nnheader-nov-read-integer) ; lines
3933                  (unless (eobp)
3934                    (if (looking-at "Xref: ")
3935                        (goto-char (match-end 0)))
3936                    (nnheader-nov-field)) ; Xref
3937                  (nnheader-nov-parse-extra)))) ; extra
3938
3939       (widen))
3940
3941     (when (and (string= references "")
3942                (setq in-reply-to (mail-header-extra header))
3943                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3944       (mail-header-set-references
3945        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3946
3947     (when gnus-alter-header-function
3948       (funcall gnus-alter-header-function header))
3949     (gnus-dependencies-add-header header dependencies force-new)))
3950
3951 (defun gnus-build-get-header (id)
3952   "Look through the buffer of NOV lines and find the header to ID.
3953 Enter this line into the dependencies hash table, and return
3954 the id of the parent article (if any)."
3955   (let ((deps gnus-newsgroup-dependencies)
3956         found header)
3957     (prog1
3958         (save-excursion
3959           (set-buffer nntp-server-buffer)
3960           (let ((case-fold-search nil))
3961             (goto-char (point-min))
3962             (while (and (not found)
3963                         (search-forward id nil t))
3964               (beginning-of-line)
3965               (setq found (looking-at
3966                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3967                                    (regexp-quote id))))
3968               (or found (beginning-of-line 2)))
3969             (when found
3970               (beginning-of-line)
3971               (and
3972                (setq header (gnus-nov-parse-line
3973                              (read (current-buffer)) deps))
3974                (gnus-parent-id (mail-header-references header))))))
3975       (when header
3976         (let ((number (mail-header-number header)))
3977           (push number gnus-newsgroup-limit)
3978           (push header gnus-newsgroup-headers)
3979           (if (memq number gnus-newsgroup-unselected)
3980               (progn
3981                 (setq gnus-newsgroup-unreads
3982                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
3983                                                number))
3984                 (setq gnus-newsgroup-unselected
3985                       (delq number gnus-newsgroup-unselected)))
3986             (push number gnus-newsgroup-ancient)))))))
3987
3988 (defun gnus-build-all-threads ()
3989   "Read all the headers."
3990   (let ((gnus-summary-ignore-duplicates t)
3991         (mail-parse-charset gnus-newsgroup-charset)
3992         (dependencies gnus-newsgroup-dependencies)
3993         header article)
3994     (save-excursion
3995       (set-buffer nntp-server-buffer)
3996       (let ((case-fold-search nil))
3997         (goto-char (point-min))
3998         (while (not (eobp))
3999           (ignore-errors
4000             (setq article (read (current-buffer))
4001                   header (gnus-nov-parse-line article dependencies)))
4002           (when header
4003             (save-excursion
4004               (set-buffer gnus-summary-buffer)
4005               (push header gnus-newsgroup-headers)
4006               (if (memq (setq article (mail-header-number header))
4007                         gnus-newsgroup-unselected)
4008                   (progn
4009                     (setq gnus-newsgroup-unreads
4010                           (gnus-add-to-sorted-list
4011                            gnus-newsgroup-unreads article))
4012                     (setq gnus-newsgroup-unselected
4013                           (delq article gnus-newsgroup-unselected)))
4014                 (push article gnus-newsgroup-ancient)))
4015             (forward-line 1)))))))
4016
4017 (defun gnus-summary-update-article-line (article header)
4018   "Update the line for ARTICLE using HEADERS."
4019   (let* ((id (mail-header-id header))
4020          (thread (gnus-id-to-thread id)))
4021     (unless thread
4022       (error "Article in no thread"))
4023     ;; Update the thread.
4024     (setcar thread header)
4025     (gnus-summary-goto-subject article)
4026     (let* ((datal (gnus-data-find-list article))
4027            (data (car datal))
4028            (buffer-read-only nil)
4029            (level (gnus-summary-thread-level)))
4030       (gnus-delete-line)
4031       (let ((inserted (- (point)
4032                          (progn
4033                            (gnus-summary-insert-line
4034                             header level nil 
4035                             (memq article gnus-newsgroup-undownloaded)
4036                             (gnus-article-mark article)
4037                             (memq article gnus-newsgroup-replied)
4038                             (memq article gnus-newsgroup-expirable)
4039                             ;; Only insert the Subject string when it's different
4040                             ;; from the previous Subject string.
4041                             (if (and
4042                                  gnus-show-threads
4043                                  (gnus-subject-equal
4044                                   (condition-case ()
4045                                       (mail-header-subject
4046                                        (gnus-data-header
4047                                         (cadr
4048                                          (gnus-data-find-list
4049                                           article
4050                                           (gnus-data-list t)))))
4051                                     ;; Error on the side of excessive subjects.
4052                                     (error ""))
4053                                   (mail-header-subject header)))
4054                                 ""
4055                               (mail-header-subject header))
4056                             nil (cdr (assq article gnus-newsgroup-scored))
4057                             (memq article gnus-newsgroup-processable))
4058                            (point)))))
4059         (when (cdr datal)
4060           (gnus-data-update-list
4061            (cdr datal) 
4062            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4063
4064 (defun gnus-summary-update-article (article &optional iheader)
4065   "Update ARTICLE in the summary buffer."
4066   (set-buffer gnus-summary-buffer)
4067   (let* ((header (gnus-summary-article-header article))
4068          (id (mail-header-id header))
4069          (data (gnus-data-find article))
4070          (thread (gnus-id-to-thread id))
4071          (references (mail-header-references header))
4072          (parent
4073           (gnus-id-to-thread
4074            (or (gnus-parent-id
4075                 (when (and references
4076                            (not (equal "" references)))
4077                   references))
4078                "none")))
4079          (buffer-read-only nil)
4080          (old (car thread)))
4081     (when thread
4082       (unless iheader
4083         (setcar thread nil)
4084         (when parent
4085           (delq thread parent)))
4086       (if (gnus-summary-insert-subject id header)
4087           ;; Set the (possibly) new article number in the data structure.
4088           (gnus-data-set-number data (gnus-id-to-article id))
4089         (setcar thread old)
4090         nil))))
4091
4092 (defun gnus-rebuild-thread (id &optional line)
4093   "Rebuild the thread containing ID.
4094 If LINE, insert the rebuilt thread starting on line LINE."
4095   (let ((buffer-read-only nil)
4096         old-pos current thread data)
4097     (if (not gnus-show-threads)
4098         (setq thread (list (car (gnus-id-to-thread id))))
4099       ;; Get the thread this article is part of.
4100       (setq thread (gnus-remove-thread id)))
4101     (setq old-pos (gnus-point-at-bol))
4102     (setq current (save-excursion
4103                     (and (re-search-backward "[\r\n]" nil t)
4104                          (gnus-summary-article-number))))
4105     ;; If this is a gathered thread, we have to go some re-gathering.
4106     (when (stringp (car thread))
4107       (let ((subject (car thread))
4108             roots thr)
4109         (setq thread (cdr thread))
4110         (while thread
4111           (unless (memq (setq thr (gnus-id-to-thread
4112                                    (gnus-root-id
4113                                     (mail-header-id (caar thread)))))
4114                         roots)
4115             (push thr roots))
4116           (setq thread (cdr thread)))
4117         ;; We now have all (unique) roots.
4118         (if (= (length roots) 1)
4119             ;; All the loose roots are now one solid root.
4120             (setq thread (car roots))
4121           (setq thread (cons subject (gnus-sort-threads roots))))))
4122     (let (threads)
4123       ;; We then insert this thread into the summary buffer.
4124       (when line
4125         (goto-char (point-min))
4126         (forward-line (1- line)))
4127       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4128         (if gnus-show-threads
4129             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4130           (gnus-summary-prepare-unthreaded thread))
4131         (setq data (nreverse gnus-newsgroup-data))
4132         (setq threads gnus-newsgroup-threads))
4133       ;; We splice the new data into the data structure.
4134       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4135       ;;!!! then we want to insert at the beginning of the buffer.
4136       ;;!!! That happens to be true with Gnus now, but that may
4137       ;;!!! change in the future.  Perhaps.
4138       (gnus-data-enter-list
4139        (if line nil current) data (- (point) old-pos))
4140       (setq gnus-newsgroup-threads
4141             (nconc threads gnus-newsgroup-threads))
4142       (gnus-data-compute-positions))))
4143
4144 (defun gnus-number-to-header (number)
4145   "Return the header for article NUMBER."
4146   (let ((headers gnus-newsgroup-headers))
4147     (while (and headers
4148                 (not (= number (mail-header-number (car headers)))))
4149       (pop headers))
4150     (when headers
4151       (car headers))))
4152
4153 (defun gnus-parent-headers (in-headers &optional generation)
4154   "Return the headers of the GENERATIONeth parent of HEADERS."
4155   (unless generation
4156     (setq generation 1))
4157   (let ((parent t)
4158         (headers in-headers)
4159         references)
4160     (while (and parent
4161                 (not (zerop generation))
4162                 (setq references (mail-header-references headers)))
4163       (setq headers (if (and references
4164                              (setq parent (gnus-parent-id references)))
4165                         (car (gnus-id-to-thread parent))
4166                       nil))
4167       (decf generation))
4168     (and (not (eq headers in-headers))
4169          headers)))
4170
4171 (defun gnus-id-to-thread (id)
4172   "Return the (sub-)thread where ID appears."
4173   (gnus-gethash id gnus-newsgroup-dependencies))
4174
4175 (defun gnus-id-to-article (id)
4176   "Return the article number of ID."
4177   (let ((thread (gnus-id-to-thread id)))
4178     (when (and thread
4179                (car thread))
4180       (mail-header-number (car thread)))))
4181
4182 (defun gnus-id-to-header (id)
4183   "Return the article headers of ID."
4184   (car (gnus-id-to-thread id)))
4185
4186 (defun gnus-article-displayed-root-p (article)
4187   "Say whether ARTICLE is a root(ish) article."
4188   (let ((level (gnus-summary-thread-level article))
4189         (refs (mail-header-references  (gnus-summary-article-header article)))
4190         particle)
4191     (cond
4192      ((null level) nil)
4193      ((zerop level) t)
4194      ((null refs) t)
4195      ((null (gnus-parent-id refs)) t)
4196      ((and (= 1 level)
4197            (null (setq particle (gnus-id-to-article
4198                                  (gnus-parent-id refs))))
4199            (null (gnus-summary-thread-level particle)))))))
4200
4201 (defun gnus-root-id (id)
4202   "Return the id of the root of the thread where ID appears."
4203   (let (last-id prev)
4204     (while (and id (setq prev (car (gnus-id-to-thread id))))
4205       (setq last-id id
4206             id (gnus-parent-id (mail-header-references prev))))
4207     last-id))
4208
4209 (defun gnus-articles-in-thread (thread)
4210   "Return the list of articles in THREAD."
4211   (cons (mail-header-number (car thread))
4212         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4213
4214 (defun gnus-remove-thread (id &optional dont-remove)
4215   "Remove the thread that has ID in it."
4216   (let (headers thread last-id)
4217     ;; First go up in this thread until we find the root.
4218     (setq last-id (gnus-root-id id)
4219           headers (message-flatten-list (gnus-id-to-thread last-id)))
4220     ;; We have now found the real root of this thread.  It might have
4221     ;; been gathered into some loose thread, so we have to search
4222     ;; through the threads to find the thread we wanted.
4223     (let ((threads gnus-newsgroup-threads)
4224           sub)
4225       (while threads
4226         (setq sub (car threads))
4227         (if (stringp (car sub))
4228             ;; This is a gathered thread, so we look at the roots
4229             ;; below it to find whether this article is in this
4230             ;; gathered root.
4231             (progn
4232               (setq sub (cdr sub))
4233               (while sub
4234                 (when (member (caar sub) headers)
4235                   (setq thread (car threads)
4236                         threads nil
4237                         sub nil))
4238                 (setq sub (cdr sub))))
4239           ;; It's an ordinary thread, so we check it.
4240           (when (eq (car sub) (car headers))
4241             (setq thread sub
4242                   threads nil)))
4243         (setq threads (cdr threads)))
4244       ;; If this article is in no thread, then it's a root.
4245       (if thread
4246           (unless dont-remove
4247             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4248         (setq thread (gnus-id-to-thread last-id)))
4249       (when thread
4250         (prog1
4251             thread                      ; We return this thread.
4252           (unless dont-remove
4253             (if (stringp (car thread))
4254                 (progn
4255                   ;; If we use dummy roots, then we have to remove the
4256                   ;; dummy root as well.
4257                   (when (eq gnus-summary-make-false-root 'dummy)
4258                     ;; We go to the dummy root by going to
4259                     ;; the first sub-"thread", and then one line up.
4260                     (gnus-summary-goto-article
4261                      (mail-header-number (caadr thread)))
4262                     (forward-line -1)
4263                     (gnus-delete-line)
4264                     (gnus-data-compute-positions))
4265                   (setq thread (cdr thread))
4266                   (while thread
4267                     (gnus-remove-thread-1 (car thread))
4268                     (setq thread (cdr thread))))
4269               (gnus-remove-thread-1 thread))))))))
4270
4271 (defun gnus-remove-thread-1 (thread)
4272   "Remove the thread THREAD recursively."
4273   (let ((number (mail-header-number (pop thread)))
4274         d)
4275     (setq thread (reverse thread))
4276     (while thread
4277       (gnus-remove-thread-1 (pop thread)))
4278     (when (setq d (gnus-data-find number))
4279       (goto-char (gnus-data-pos d))
4280       (gnus-summary-show-thread)
4281       (gnus-data-remove
4282        number
4283        (- (gnus-point-at-bol)
4284           (prog1
4285               (1+ (gnus-point-at-eol))
4286             (gnus-delete-line)))))))
4287
4288 (defun gnus-sort-threads-1 (threads func)
4289   (sort (mapcar (lambda (thread)
4290                   (cons (car thread)
4291                         (and (cdr thread)
4292                              (gnus-sort-threads-1 (cdr thread) func))))
4293                 threads) func))
4294
4295 (defun gnus-sort-threads (threads)
4296   "Sort THREADS."
4297   (if (not gnus-thread-sort-functions)
4298       threads
4299     (gnus-message 8 "Sorting threads...")
4300     (let ((max-lisp-eval-depth 5000))
4301       (prog1 (gnus-sort-threads-1
4302          threads
4303          (gnus-make-sort-function gnus-thread-sort-functions))
4304         (gnus-message 8 "Sorting threads...done")))))
4305
4306 (defun gnus-sort-articles (articles)
4307   "Sort ARTICLES."
4308   (when gnus-article-sort-functions
4309     (gnus-message 7 "Sorting articles...")
4310     (prog1
4311         (setq gnus-newsgroup-headers
4312               (sort articles (gnus-make-sort-function
4313                               gnus-article-sort-functions)))
4314       (gnus-message 7 "Sorting articles...done"))))
4315
4316 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4317 (defmacro gnus-thread-header (thread)
4318   "Return header of first article in THREAD.
4319 Note that THREAD must never, ever be anything else than a variable -
4320 using some other form will lead to serious barfage."
4321   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4322   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4323   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4324         (vector thread) 2))
4325
4326 (defsubst gnus-article-sort-by-number (h1 h2)
4327   "Sort articles by article number."
4328   (< (mail-header-number h1)
4329      (mail-header-number h2)))
4330
4331 (defun gnus-thread-sort-by-number (h1 h2)
4332   "Sort threads by root article number."
4333   (gnus-article-sort-by-number
4334    (gnus-thread-header h1) (gnus-thread-header h2)))
4335
4336 (defsubst gnus-article-sort-by-random (h1 h2)
4337   "Sort articles by article number."
4338   (zerop (random 2)))
4339
4340 (defun gnus-thread-sort-by-random (h1 h2)
4341   "Sort threads by root article number."
4342   (gnus-article-sort-by-random
4343    (gnus-thread-header h1) (gnus-thread-header h2)))
4344
4345 (defsubst gnus-article-sort-by-lines (h1 h2)
4346   "Sort articles by article Lines header."
4347   (< (mail-header-lines h1)
4348      (mail-header-lines h2)))
4349
4350 (defun gnus-thread-sort-by-lines (h1 h2)
4351   "Sort threads by root article Lines header."
4352   (gnus-article-sort-by-lines
4353    (gnus-thread-header h1) (gnus-thread-header h2)))
4354
4355 (defsubst gnus-article-sort-by-chars (h1 h2)
4356   "Sort articles by octet length."
4357   (< (mail-header-chars h1)
4358      (mail-header-chars h2)))
4359
4360 (defun gnus-thread-sort-by-chars (h1 h2)
4361   "Sort threads by root article octet length."
4362   (gnus-article-sort-by-chars
4363    (gnus-thread-header h1) (gnus-thread-header h2)))
4364
4365 (defsubst gnus-article-sort-by-author (h1 h2)
4366   "Sort articles by root author."
4367   (string-lessp
4368    (let ((extract (funcall
4369                    gnus-extract-address-components
4370                    (mail-header-from h1))))
4371      (or (car extract) (cadr extract) ""))
4372    (let ((extract (funcall
4373                    gnus-extract-address-components
4374                    (mail-header-from h2))))
4375      (or (car extract) (cadr extract) ""))))
4376
4377 (defun gnus-thread-sort-by-author (h1 h2)
4378   "Sort threads by root author."
4379   (gnus-article-sort-by-author
4380    (gnus-thread-header h1)  (gnus-thread-header h2)))
4381
4382 (defsubst gnus-article-sort-by-subject (h1 h2)
4383   "Sort articles by root subject."
4384   (string-lessp
4385    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4386    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4387
4388 (defun gnus-thread-sort-by-subject (h1 h2)
4389   "Sort threads by root subject."
4390   (gnus-article-sort-by-subject
4391    (gnus-thread-header h1) (gnus-thread-header h2)))
4392
4393 (defsubst gnus-article-sort-by-date (h1 h2)
4394   "Sort articles by root article date."
4395   (time-less-p
4396    (gnus-date-get-time (mail-header-date h1))
4397    (gnus-date-get-time (mail-header-date h2))))
4398
4399 (defun gnus-thread-sort-by-date (h1 h2)
4400   "Sort threads by root article date."
4401   (gnus-article-sort-by-date
4402    (gnus-thread-header h1) (gnus-thread-header h2)))
4403
4404 (defsubst gnus-article-sort-by-score (h1 h2)
4405   "Sort articles by root article score.
4406 Unscored articles will be counted as having a score of zero."
4407   (> (or (cdr (assq (mail-header-number h1)
4408                     gnus-newsgroup-scored))
4409          gnus-summary-default-score 0)
4410      (or (cdr (assq (mail-header-number h2)
4411                     gnus-newsgroup-scored))
4412          gnus-summary-default-score 0)))
4413
4414 (defun gnus-thread-sort-by-score (h1 h2)
4415   "Sort threads by root article score."
4416   (gnus-article-sort-by-score
4417    (gnus-thread-header h1) (gnus-thread-header h2)))
4418
4419 (defun gnus-thread-sort-by-total-score (h1 h2)
4420   "Sort threads by the sum of all scores in the thread.
4421 Unscored articles will be counted as having a score of zero."
4422   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4423
4424 (defun gnus-thread-total-score (thread)
4425   ;; This function find the total score of THREAD.
4426   (cond
4427    ((null thread)
4428     0)
4429    ((consp thread)
4430     (if (stringp (car thread))
4431         (apply gnus-thread-score-function 0
4432                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4433       (gnus-thread-total-score-1 thread)))
4434    (t
4435     (gnus-thread-total-score-1 (list thread)))))
4436
4437 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4438   "Sort threads such that the thread with the most recently arrived article comes first."
4439   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4440
4441 (defun gnus-thread-highest-number (thread)
4442   "Return the highest article number in THREAD."
4443   (apply 'max (mapcar (lambda (header)
4444                         (mail-header-number header))
4445                       (message-flatten-list thread))))
4446
4447 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4448   "Sort threads such that the thread with the most recently dated article comes first."
4449   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4450
4451 (defun gnus-thread-latest-date (thread)
4452   "Return the highest article date in THREAD."
4453   (let ((previous-time 0))
4454     (apply 'max
4455            (mapcar
4456             (lambda (header)
4457               (setq previous-time
4458                     (time-to-seconds
4459                      (condition-case ()
4460                          (mail-header-parse-date (mail-header-date header))
4461                        (error previous-time)))))
4462             (sort
4463              (message-flatten-list thread)
4464              (lambda (h1 h2)
4465                (< (mail-header-number h1)
4466                   (mail-header-number h2))))))))
4467
4468 (defun gnus-thread-total-score-1 (root)
4469   ;; This function find the total score of the thread below ROOT.
4470   (setq root (car root))
4471   (apply gnus-thread-score-function
4472          (or (append
4473               (mapcar 'gnus-thread-total-score
4474                       (cdr (gnus-id-to-thread (mail-header-id root))))
4475               (when (> (mail-header-number root) 0)
4476                 (list (or (cdr (assq (mail-header-number root)
4477                                      gnus-newsgroup-scored))
4478                           gnus-summary-default-score 0))))
4479              (list gnus-summary-default-score)
4480              '(0))))
4481
4482 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4483 (defvar gnus-tmp-prev-subject nil)
4484 (defvar gnus-tmp-false-parent nil)
4485 (defvar gnus-tmp-root-expunged nil)
4486 (defvar gnus-tmp-dummy-line nil)
4487
4488 (eval-when-compile (defvar gnus-tmp-header))
4489 (defun gnus-extra-header (type &optional header)
4490   "Return the extra header of TYPE."
4491   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4492       ""))
4493
4494 (defvar gnus-tmp-thread-tree-header-string "")
4495
4496 (defcustom gnus-sum-thread-tree-root "> "
4497   "With %B spec, used for the root of a thread.
4498 If nil, use subject instead."
4499   :type 'string
4500   :group 'gnus-thread)
4501 (defcustom gnus-sum-thread-tree-single-indent ""
4502   "With %B spec, used for a thread with just one message.
4503 If nil, use subject instead."
4504   :type 'string
4505   :group 'gnus-thread)
4506 (defcustom gnus-sum-thread-tree-vertical "| "
4507   "With %B spec, used for drawing a vertical line."
4508   :type 'string
4509   :group 'gnus-thread)
4510 (defcustom gnus-sum-thread-tree-indent "  "
4511   "With %B spec, used for indenting."
4512   :type 'string
4513   :group 'gnus-thread)
4514 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4515   "With %B spec, used for a leaf with brothers."
4516   :type 'string
4517   :group 'gnus-thread)
4518 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4519   "With %B spec, used for a leaf without brothers."
4520   :type 'string
4521   :group 'gnus-thread)
4522
4523 (defun gnus-summary-prepare-threads (threads)
4524   "Prepare summary buffer from THREADS and indentation LEVEL.
4525 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4526 or a straight list of headers."
4527   (gnus-message 7 "Generating summary...")
4528
4529   (setq gnus-newsgroup-threads threads)
4530   (beginning-of-line)
4531
4532   (let ((gnus-tmp-level 0)
4533         (default-score (or gnus-summary-default-score 0))
4534         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4535         (building-line-count gnus-summary-display-while-building)
4536         (building-count (integerp gnus-summary-display-while-building))
4537         thread number subject stack state gnus-tmp-gathered beg-match
4538         new-roots gnus-tmp-new-adopts thread-end simp-subject
4539         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4540         gnus-tmp-replied gnus-tmp-subject-or-nil
4541         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4542         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4543         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4544         tree-stack)
4545
4546     (setq gnus-tmp-prev-subject nil)
4547
4548     (if (vectorp (car threads))
4549         ;; If this is a straight (sic) list of headers, then a
4550         ;; threaded summary display isn't required, so we just create
4551         ;; an unthreaded one.
4552         (gnus-summary-prepare-unthreaded threads)
4553
4554       ;; Do the threaded display.
4555
4556       (if gnus-summary-display-while-building
4557           (switch-to-buffer (buffer-name)))
4558       (while (or threads stack gnus-tmp-new-adopts new-roots)
4559
4560         (if (and (= gnus-tmp-level 0)
4561                  (or (not stack)
4562                      (= (caar stack) 0))
4563                  (not gnus-tmp-false-parent)
4564                  (or gnus-tmp-new-adopts new-roots))
4565             (if gnus-tmp-new-adopts
4566                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4567                       thread (list (car gnus-tmp-new-adopts))
4568                       gnus-tmp-header (caar thread)
4569                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4570               (when new-roots
4571                 (setq thread (list (car new-roots))
4572                       gnus-tmp-header (caar thread)
4573                       new-roots (cdr new-roots))))
4574
4575           (if threads
4576               ;; If there are some threads, we do them before the
4577               ;; threads on the stack.
4578               (setq thread threads
4579                     gnus-tmp-header (caar thread))
4580             ;; There were no current threads, so we pop something off
4581             ;; the stack.
4582             (setq state (car stack)
4583                   gnus-tmp-level (car state)
4584                   tree-stack (cadr state)
4585                   thread (caddr state)
4586                   stack (cdr stack)
4587                   gnus-tmp-header (caar thread))))
4588
4589         (setq gnus-tmp-false-parent nil)
4590         (setq gnus-tmp-root-expunged nil)
4591         (setq thread-end nil)
4592
4593         (if (stringp gnus-tmp-header)
4594             ;; The header is a dummy root.
4595             (cond
4596              ((eq gnus-summary-make-false-root 'adopt)
4597               ;; We let the first article adopt the rest.
4598               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4599                                                (cddar thread)))
4600               (setq gnus-tmp-gathered
4601                     (nconc (mapcar
4602                             (lambda (h) (mail-header-number (car h)))
4603                             (cddar thread))
4604                            gnus-tmp-gathered))
4605               (setq thread (cons (list (caar thread)
4606                                        (cadar thread))
4607                                  (cdr thread)))
4608               (setq gnus-tmp-level -1
4609                     gnus-tmp-false-parent t))
4610              ((eq gnus-summary-make-false-root 'empty)
4611               ;; We print adopted articles with empty subject fields.
4612               (setq gnus-tmp-gathered
4613                     (nconc (mapcar
4614                             (lambda (h) (mail-header-number (car h)))
4615                             (cddar thread))
4616                            gnus-tmp-gathered))
4617               (setq gnus-tmp-level -1))
4618              ((eq gnus-summary-make-false-root 'dummy)
4619               ;; We remember that we probably want to output a dummy
4620               ;; root.
4621               (setq gnus-tmp-dummy-line gnus-tmp-header)
4622               (setq gnus-tmp-prev-subject gnus-tmp-header))
4623              (t
4624               ;; We do not make a root for the gathered
4625               ;; sub-threads at all.
4626               (setq gnus-tmp-level -1)))
4627
4628           (setq number (mail-header-number gnus-tmp-header)
4629                 subject (mail-header-subject gnus-tmp-header)
4630                 simp-subject (gnus-simplify-subject-fully subject))
4631
4632           (cond
4633            ;; If the thread has changed subject, we might want to make
4634            ;; this subthread into a root.
4635            ((and (null gnus-thread-ignore-subject)
4636                  (not (zerop gnus-tmp-level))
4637                  gnus-tmp-prev-subject
4638                  (not (string= gnus-tmp-prev-subject simp-subject)))
4639             (setq new-roots (nconc new-roots (list (car thread)))
4640                   thread-end t
4641                   gnus-tmp-header nil))
4642            ;; If the article lies outside the current limit,
4643            ;; then we do not display it.
4644            ((not (memq number gnus-newsgroup-limit))
4645             (setq gnus-tmp-gathered
4646                   (nconc (mapcar
4647                           (lambda (h) (mail-header-number (car h)))
4648                           (cdar thread))
4649                          gnus-tmp-gathered))
4650             (setq gnus-tmp-new-adopts (if (cdar thread)
4651                                           (append gnus-tmp-new-adopts
4652                                                   (cdar thread))
4653                                         gnus-tmp-new-adopts)
4654                   thread-end t
4655                   gnus-tmp-header nil)
4656             (when (zerop gnus-tmp-level)
4657               (setq gnus-tmp-root-expunged t)))
4658            ;; Perhaps this article is to be marked as read?
4659            ((and gnus-summary-mark-below
4660                  (< (or (cdr (assq number gnus-newsgroup-scored))
4661                         default-score)
4662                     gnus-summary-mark-below)
4663                  ;; Don't touch sparse articles.
4664                  (not (gnus-summary-article-sparse-p number))
4665                  (not (gnus-summary-article-ancient-p number)))
4666             (setq gnus-newsgroup-unreads
4667                   (delq number gnus-newsgroup-unreads))
4668             (if gnus-newsgroup-auto-expire
4669                 (setq gnus-newsgroup-expirable
4670                       (gnus-add-to-sorted-list
4671                        gnus-newsgroup-expirable number))
4672               (push (cons number gnus-low-score-mark)
4673                     gnus-newsgroup-reads))))
4674
4675           (when gnus-tmp-header
4676             ;; We may have an old dummy line to output before this
4677             ;; article.
4678             (when (and gnus-tmp-dummy-line
4679                        (gnus-subject-equal
4680                         gnus-tmp-dummy-line
4681                         (mail-header-subject gnus-tmp-header)))
4682               (gnus-summary-insert-dummy-line
4683                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4684               (setq gnus-tmp-dummy-line nil))
4685
4686             ;; Compute the mark.
4687             (setq gnus-tmp-unread (gnus-article-mark number))
4688
4689             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4690                                   gnus-tmp-header gnus-tmp-level)
4691                   gnus-newsgroup-data)
4692
4693             ;; Actually insert the line.
4694             (setq
4695              gnus-tmp-subject-or-nil
4696              (cond
4697               ((and gnus-thread-ignore-subject
4698                     gnus-tmp-prev-subject
4699                     (not (string= gnus-tmp-prev-subject simp-subject)))
4700                subject)
4701               ((zerop gnus-tmp-level)
4702                (if (and (eq gnus-summary-make-false-root 'empty)
4703                         (memq number gnus-tmp-gathered)
4704                         gnus-tmp-prev-subject
4705                         (string= gnus-tmp-prev-subject simp-subject))
4706                    gnus-summary-same-subject
4707                  subject))
4708               (t gnus-summary-same-subject)))
4709             (if (and (eq gnus-summary-make-false-root 'adopt)
4710                      (= gnus-tmp-level 1)
4711                      (memq number gnus-tmp-gathered))
4712                 (setq gnus-tmp-opening-bracket ?\<
4713                       gnus-tmp-closing-bracket ?\>)
4714               (setq gnus-tmp-opening-bracket ?\[
4715                     gnus-tmp-closing-bracket ?\]))
4716             (setq
4717              gnus-tmp-indentation
4718              (aref gnus-thread-indent-array gnus-tmp-level)
4719              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4720              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4721                                 gnus-summary-default-score 0)
4722              gnus-tmp-score-char
4723              (if (or (null gnus-summary-default-score)
4724                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4725                          gnus-summary-zcore-fuzz))
4726                  ?                      ;Whitespace
4727                (if (< gnus-tmp-score gnus-summary-default-score)
4728                    gnus-score-below-mark gnus-score-over-mark))
4729              gnus-tmp-replied
4730              (cond ((memq number gnus-newsgroup-processable)
4731                     gnus-process-mark)
4732                    ((memq number gnus-newsgroup-cached)
4733                     gnus-cached-mark)
4734                    ((memq number gnus-newsgroup-replied)
4735                     gnus-replied-mark)
4736                    ((memq number gnus-newsgroup-forwarded)
4737                     gnus-forwarded-mark)
4738                    ((memq number gnus-newsgroup-saved)
4739                     gnus-saved-mark)
4740                    ((memq number gnus-newsgroup-recent)
4741                     gnus-recent-mark)
4742                    ((memq number gnus-newsgroup-unseen)
4743                     gnus-unseen-mark)
4744                    (t gnus-no-mark))
4745              gnus-tmp-downloaded
4746              (cond ((memq number gnus-newsgroup-undownloaded) 
4747                     gnus-undownloaded-mark)
4748                    (gnus-newsgroup-agentized
4749                     gnus-downloaded-mark)
4750                    (t
4751                     gnus-no-mark))
4752              gnus-tmp-from (mail-header-from gnus-tmp-header)
4753              gnus-tmp-name
4754              (cond
4755               ((string-match "<[^>]+> *$" gnus-tmp-from)
4756                (setq beg-match (match-beginning 0))
4757                (or (and (string-match "^\".+\"" gnus-tmp-from)
4758                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4759                    (substring gnus-tmp-from 0 beg-match)))
4760               ((string-match "(.+)" gnus-tmp-from)
4761                (substring gnus-tmp-from
4762                           (1+ (match-beginning 0)) (1- (match-end 0))))
4763               (t gnus-tmp-from))
4764              gnus-tmp-thread-tree-header-string
4765              (cond
4766               ((not gnus-show-threads) "")
4767               ((zerop gnus-tmp-level)
4768                (if (cdar thread)
4769                    (or gnus-sum-thread-tree-root subject)
4770                  (or gnus-sum-thread-tree-single-indent subject)))
4771               (t
4772                (concat (apply 'concat
4773                               (mapcar (lambda (item)
4774                                         (if (= item 1)
4775                                             gnus-sum-thread-tree-vertical
4776                                           gnus-sum-thread-tree-indent))
4777                                       (cdr (reverse tree-stack))))
4778                        (if (nth 1 thread)
4779                            gnus-sum-thread-tree-leaf-with-other
4780                          gnus-sum-thread-tree-single-leaf)))))
4781             (when (string= gnus-tmp-name "")
4782               (setq gnus-tmp-name gnus-tmp-from))
4783             (unless (numberp gnus-tmp-lines)
4784               (setq gnus-tmp-lines -1))
4785             (if (= gnus-tmp-lines -1)
4786                 (setq gnus-tmp-lines "?")
4787               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4788             (gnus-put-text-property
4789              (point)
4790              (progn (eval gnus-summary-line-format-spec) (point))
4791              'gnus-number number)
4792             (when gnus-visual-p
4793               (forward-line -1)
4794               (gnus-run-hooks 'gnus-summary-update-hook)
4795               (forward-line 1))
4796
4797             (setq gnus-tmp-prev-subject simp-subject)))
4798
4799         (when (nth 1 thread)
4800           (push (list (max 0 gnus-tmp-level)
4801                       (copy-list tree-stack)
4802                       (nthcdr 1 thread))
4803                 stack))
4804         (push (if (nth 1 thread) 1 0) tree-stack)
4805         (incf gnus-tmp-level)
4806         (setq threads (if thread-end nil (cdar thread)))
4807         (if gnus-summary-display-while-building
4808             (if building-count
4809                 (progn
4810                   ;; use a set frequency
4811                   (setq building-line-count (1- building-line-count))
4812                   (when (= building-line-count 0)
4813                     (sit-for 0)
4814                     (setq building-line-count
4815                           gnus-summary-display-while-building)))
4816               ;; always
4817               (sit-for 0)))
4818         (unless threads
4819           (setq gnus-tmp-level 0)))))
4820   (gnus-message 7 "Generating summary...done"))
4821
4822 (defun gnus-summary-prepare-unthreaded (headers)
4823   "Generate an unthreaded summary buffer based on HEADERS."
4824   (let (header number mark)
4825
4826     (beginning-of-line)
4827
4828     (while headers
4829       ;; We may have to root out some bad articles...
4830       (when (memq (setq number (mail-header-number
4831                                 (setq header (pop headers))))
4832                   gnus-newsgroup-limit)
4833         ;; Mark article as read when it has a low score.
4834         (when (and gnus-summary-mark-below
4835                    (< (or (cdr (assq number gnus-newsgroup-scored))
4836                           gnus-summary-default-score 0)
4837                       gnus-summary-mark-below)
4838                    (not (gnus-summary-article-ancient-p number)))
4839           (setq gnus-newsgroup-unreads
4840                 (delq number gnus-newsgroup-unreads))
4841           (if gnus-newsgroup-auto-expire
4842               (push number gnus-newsgroup-expirable)
4843             (push (cons number gnus-low-score-mark)
4844                   gnus-newsgroup-reads)))
4845
4846         (setq mark (gnus-article-mark number))
4847         (push (gnus-data-make number mark (1+ (point)) header 0)
4848               gnus-newsgroup-data)
4849         (gnus-summary-insert-line
4850          header 0 number
4851          (memq number gnus-newsgroup-undownloaded)
4852          mark (memq number gnus-newsgroup-replied)
4853          (memq number gnus-newsgroup-expirable)
4854          (mail-header-subject header) nil
4855          (cdr (assq number gnus-newsgroup-scored))
4856          (memq number gnus-newsgroup-processable))))))
4857
4858 (defun gnus-summary-remove-list-identifiers ()
4859   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4860   (let ((regexp (if (consp gnus-list-identifiers)
4861                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4862                   gnus-list-identifiers))
4863         changed subject)
4864     (when regexp
4865       (dolist (header gnus-newsgroup-headers)
4866         (setq subject (mail-header-subject header)
4867               changed nil)
4868         (while (string-match
4869                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4870                 subject)
4871           (setq subject
4872                 (concat (substring subject 0 (match-beginning 2))
4873                         (substring subject (match-end 0)))
4874                 changed t))
4875         (when (and changed
4876                    (string-match
4877                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4878           (setq subject
4879                 (concat (substring subject 0 (match-beginning 1))
4880                         (substring subject (match-end 1)))))
4881         (when changed
4882           (mail-header-set-subject header subject))))))
4883
4884 (defun gnus-fetch-headers (articles)
4885   "Fetch headers of ARTICLES."
4886   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4887     (gnus-message 5 "Fetching headers for %s..." name)
4888     (prog1
4889         (if (eq 'nov
4890                 (setq gnus-headers-retrieved-by
4891                       (gnus-retrieve-headers
4892                        articles gnus-newsgroup-name
4893                        ;; We might want to fetch old headers, but
4894                        ;; not if there is only 1 article.
4895                        (and (or (and
4896                                  (not (eq gnus-fetch-old-headers 'some))
4897                                  (not (numberp gnus-fetch-old-headers)))
4898                                 (> (length articles) 1))
4899                             gnus-fetch-old-headers))))
4900             (gnus-get-newsgroup-headers-xover
4901              articles nil nil gnus-newsgroup-name t)
4902           (gnus-get-newsgroup-headers))
4903       (gnus-message 5 "Fetching headers for %s...done" name))))
4904
4905 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4906   "Select newsgroup GROUP.
4907 If READ-ALL is non-nil, all articles in the group are selected.
4908 If SELECT-ARTICLES, only select those articles from GROUP."
4909   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4910          ;;!!! Dirty hack; should be removed.
4911          (gnus-summary-ignore-duplicates
4912           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4913               t
4914             gnus-summary-ignore-duplicates))
4915          (info (nth 2 entry))
4916          articles fetched-articles cached)
4917
4918     (unless (gnus-check-server
4919              (set (make-local-variable 'gnus-current-select-method)
4920                   (gnus-find-method-for-group group)))
4921       (error "Couldn't open server"))
4922
4923     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4924         (gnus-activate-group group)     ; Or we can activate it...
4925         (progn                          ; Or we bug out.
4926           (when (equal major-mode 'gnus-summary-mode)
4927             (kill-buffer (current-buffer)))
4928           (error "Couldn't activate group %s: %s"
4929                  group (gnus-status-message group))))
4930
4931     (unless (gnus-request-group group t)
4932       (when (equal major-mode 'gnus-summary-mode)
4933         (kill-buffer (current-buffer)))
4934       (error "Couldn't request group %s: %s"
4935              group (gnus-status-message group)))
4936
4937     (setq gnus-newsgroup-name group
4938           gnus-newsgroup-unselected nil
4939           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4940
4941     (let ((display (gnus-group-find-parameter group 'display)))
4942       (setq gnus-newsgroup-display
4943             (cond
4944              ((not (zerop (or (car-safe read-all) 0)))
4945               ;; The user entered the group with C-u SPC/RET, let's show
4946               ;; all articles.
4947               'gnus-not-ignore)
4948              ((eq display 'all)
4949               'gnus-not-ignore)
4950              ((arrayp display)
4951               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4952              ((numberp display)
4953               ;; The following is probably the "correct" solution, but
4954               ;; it makes Gnus fetch all headers and then limit the
4955               ;; articles (which is slow), so instead we hack the
4956               ;; select-articles parameter instead. -- Simon Josefsson
4957               ;; <jas@kth.se>
4958               ;;
4959               ;; (gnus-byte-compile
4960               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4961               ;;                         display)))))
4962               (setq select-articles
4963                     (gnus-uncompress-range
4964                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4965                              (if (> tmp 0)
4966                                  tmp
4967                                1))
4968                            (cdr (gnus-active group)))))
4969               nil)
4970              (t
4971               nil))))
4972
4973     (gnus-summary-setup-default-charset)
4974
4975     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4976     (when (gnus-virtual-group-p group)
4977       (setq cached gnus-newsgroup-cached))
4978
4979     (setq gnus-newsgroup-unreads
4980           (gnus-sorted-ndifference
4981            (gnus-sorted-ndifference gnus-newsgroup-unreads
4982                                     gnus-newsgroup-marked)
4983            gnus-newsgroup-dormant))
4984
4985     (setq gnus-newsgroup-processable nil)
4986
4987     (gnus-update-read-articles group gnus-newsgroup-unreads)
4988
4989     ;; Adjust and set lists of article marks.
4990     (when info
4991       (gnus-adjust-marked-articles info))
4992     (if (setq articles select-articles)
4993         (setq gnus-newsgroup-unselected
4994               (gnus-sorted-difference gnus-newsgroup-unreads articles))
4995       (setq articles (gnus-articles-to-read group read-all)))
4996
4997     (cond
4998      ((null articles)
4999       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5000       'quit)
5001      ((eq articles 0) nil)
5002      (t
5003       ;; Init the dependencies hash table.
5004       (setq gnus-newsgroup-dependencies
5005             (gnus-make-hashtable (length articles)))
5006       (gnus-set-global-variables)
5007       ;; Retrieve the headers and read them in.
5008
5009       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5010
5011       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5012       (when cached
5013         (setq gnus-newsgroup-cached cached))
5014
5015       ;; Suppress duplicates?
5016       (when gnus-suppress-duplicates
5017         (gnus-dup-suppress-articles))
5018
5019       ;; Set the initial limit.
5020       (setq gnus-newsgroup-limit (copy-sequence articles))
5021       ;; Remove canceled articles from the list of unread articles.
5022       (setq fetched-articles
5023             (mapcar (lambda (headers) (mail-header-number headers))
5024                     gnus-newsgroup-headers))
5025       (setq gnus-newsgroup-articles fetched-articles)
5026       (setq gnus-newsgroup-unreads
5027             (gnus-sorted-nintersection
5028              gnus-newsgroup-unreads fetched-articles))
5029       (gnus-compute-unseen-list)
5030
5031       ;; Removed marked articles that do not exist.
5032       (gnus-update-missing-marks
5033        (gnus-sorted-difference articles fetched-articles))
5034       ;; We might want to build some more threads first.
5035       (when (and gnus-fetch-old-headers
5036                  (eq gnus-headers-retrieved-by 'nov))
5037         (if (eq gnus-fetch-old-headers 'invisible)
5038             (gnus-build-all-threads)
5039           (gnus-build-old-threads)))
5040       ;; Let the Gnus agent mark articles as read.
5041       (when gnus-agent
5042         (gnus-agent-get-undownloaded-list))
5043       ;; Remove list identifiers from subject
5044       (when gnus-list-identifiers
5045         (gnus-summary-remove-list-identifiers))
5046       ;; Check whether auto-expire is to be done in this group.
5047       (setq gnus-newsgroup-auto-expire
5048             (gnus-group-auto-expirable-p group))
5049       ;; Set up the article buffer now, if necessary.
5050       (unless gnus-single-article-buffer
5051         (gnus-article-setup-buffer))
5052       ;; First and last article in this newsgroup.
5053       (when gnus-newsgroup-headers
5054         (setq gnus-newsgroup-begin
5055               (mail-header-number (car gnus-newsgroup-headers))
5056               gnus-newsgroup-end
5057               (mail-header-number
5058                (gnus-last-element gnus-newsgroup-headers))))
5059       ;; GROUP is successfully selected.
5060       (or gnus-newsgroup-headers t)))))
5061
5062 (defun gnus-compute-unseen-list ()
5063   ;; The `seen' marks are treated specially.
5064   (if (not gnus-newsgroup-seen)
5065       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5066     (setq gnus-newsgroup-unseen
5067           (gnus-inverse-list-range-intersection
5068            gnus-newsgroup-articles gnus-newsgroup-seen))))
5069
5070 (defun gnus-summary-display-make-predicate (display)
5071   (require 'gnus-agent)
5072   (when (= (length display) 1)
5073     (setq display (car display)))
5074   (unless gnus-summary-display-cache
5075     (dolist (elem (append '((unread . unread)
5076                             (read . read)
5077                             (unseen . unseen))
5078                           gnus-article-mark-lists))
5079       (push (cons (cdr elem)
5080                   (gnus-byte-compile
5081                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5082             gnus-summary-display-cache)))
5083   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5084         (gnus-category-predicate-cache gnus-summary-display-cache))
5085     (gnus-get-predicate display)))
5086
5087 ;; Uses the dynamically bound `number' variable.
5088 (eval-when-compile
5089   (defvar number))
5090 (defun gnus-article-marked-p (type &optional article)
5091   (let ((article (or article number)))
5092     (cond
5093      ((eq type 'tick)
5094       (memq article gnus-newsgroup-marked))
5095      ((eq type 'spam)
5096       (memq article gnus-newsgroup-spam-marked))
5097      ((eq type 'unsend)
5098       (memq article gnus-newsgroup-unsendable))
5099      ((eq type 'undownload)
5100       (memq article gnus-newsgroup-undownloaded))
5101      ((eq type 'download)
5102       (memq article gnus-newsgroup-downloadable))
5103      ((eq type 'unread)
5104       (memq article gnus-newsgroup-unreads))
5105      ((eq type 'read)
5106       (memq article gnus-newsgroup-reads))
5107      ((eq type 'dormant)
5108       (memq article gnus-newsgroup-dormant) )
5109      ((eq type 'expire)
5110       (memq article gnus-newsgroup-expirable))
5111      ((eq type 'reply)
5112       (memq article gnus-newsgroup-replied))
5113      ((eq type 'killed)
5114       (memq article gnus-newsgroup-killed))
5115      ((eq type 'bookmark)
5116       (assq article gnus-newsgroup-bookmarks))
5117      ((eq type 'score)
5118       (assq article gnus-newsgroup-scored))
5119      ((eq type 'save)
5120       (memq article gnus-newsgroup-saved))
5121      ((eq type 'cache)
5122       (memq article gnus-newsgroup-cached))
5123      ((eq type 'forward)
5124       (memq article gnus-newsgroup-forwarded))
5125      ((eq type 'seen)
5126       (not (memq article gnus-newsgroup-unseen)))
5127      ((eq type 'recent)
5128       (memq article gnus-newsgroup-recent))
5129      (t t))))
5130
5131 (defun gnus-articles-to-read (group &optional read-all)
5132   "Find out what articles the user wants to read."
5133   (let* ((display (gnus-group-find-parameter group 'display))
5134          (articles
5135           ;; Select all articles if `read-all' is non-nil, or if there
5136           ;; are no unread articles.
5137           (if (or read-all
5138                   (and (zerop (length gnus-newsgroup-marked))
5139                        (zerop (length gnus-newsgroup-unreads)))
5140                   ;; Fetch all if the predicate is non-nil.
5141                   gnus-newsgroup-display)
5142               ;; We want to select the headers for all the articles in
5143               ;; the group, so we select either all the active
5144               ;; articles in the group, or (if that's nil), the
5145               ;; articles in the cache.
5146               (or
5147                (gnus-uncompress-range (gnus-active group))
5148                (gnus-cache-articles-in-group group))
5149             ;; Select only the "normal" subset of articles.
5150             (gnus-sorted-nunion
5151              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5152              gnus-newsgroup-unreads)))
5153          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5154          (scored (length scored-list))
5155          (number (length articles))
5156          (marked (+ (length gnus-newsgroup-marked)
5157                     (length gnus-newsgroup-dormant)))
5158          (select
5159           (cond
5160            ((numberp read-all)
5161             read-all)
5162            ((numberp gnus-newsgroup-display)
5163             gnus-newsgroup-display)
5164            (t
5165             (condition-case ()
5166                 (cond
5167                  ((and (or (<= scored marked) (= scored number))
5168                        (numberp gnus-large-newsgroup)
5169                        (> number gnus-large-newsgroup))
5170                   (let* ((cursor-in-echo-area nil)
5171                          (initial (gnus-parameter-large-newsgroup-initial
5172                                    gnus-newsgroup-name))
5173                          (input
5174                           (read-string
5175                            (format
5176                             "How many articles from %s (%s %d): "
5177                             (gnus-limit-string
5178                              (gnus-group-decoded-name gnus-newsgroup-name)
5179                              35)
5180                             (if initial "max" "default")
5181                             number)
5182                            (if initial
5183                                (cons (number-to-string initial)
5184                                      0)))))
5185                     (if (string-match "^[ \t]*$" input) number input)))
5186                  ((and (> scored marked) (< scored number)
5187                        (> (- scored number) 20))
5188                   (let ((input
5189                          (read-string
5190                           (format "%s %s (%d scored, %d total): "
5191                                   "How many articles from"
5192                                   (gnus-group-decoded-name group)
5193                                   scored number))))
5194                     (if (string-match "^[ \t]*$" input)
5195                         number input)))
5196                  (t number))
5197               (quit
5198                (message "Quit getting the articles to read")
5199                nil))))))
5200     (setq select (if (stringp select) (string-to-number select) select))
5201     (if (or (null select) (zerop select))
5202         select
5203       (if (and (not (zerop scored)) (<= (abs select) scored))
5204           (progn
5205             (setq articles (sort scored-list '<))
5206             (setq number (length articles)))
5207         (setq articles (copy-sequence articles)))
5208
5209       (when (< (abs select) number)
5210         (if (< select 0)
5211             ;; Select the N oldest articles.
5212             (setcdr (nthcdr (1- (abs select)) articles) nil)
5213           ;; Select the N most recent articles.
5214           (setq articles (nthcdr (- number select) articles))))
5215       (setq gnus-newsgroup-unselected
5216             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5217       (when gnus-alter-articles-to-read-function
5218         (setq articles
5219               (sort
5220                (funcall gnus-alter-articles-to-read-function
5221                         gnus-newsgroup-name articles)
5222                '<)))
5223       articles)))
5224
5225 (defun gnus-killed-articles (killed articles)
5226   (let (out)
5227     (while articles
5228       (when (inline (gnus-member-of-range (car articles) killed))
5229         (push (car articles) out))
5230       (setq articles (cdr articles)))
5231     out))
5232
5233 (defun gnus-uncompress-marks (marks)
5234   "Uncompress the mark ranges in MARKS."
5235   (let ((uncompressed '(score bookmark))
5236         out)
5237     (while marks
5238       (if (memq (caar marks) uncompressed)
5239           (push (car marks) out)
5240         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5241       (setq marks (cdr marks)))
5242     out))
5243
5244 (defun gnus-article-mark-to-type (mark)
5245   "Return the type of MARK."
5246   (or (cadr (assq mark gnus-article-special-mark-lists))
5247       'list))
5248
5249 (defun gnus-article-unpropagatable-p (mark)
5250   "Return whether MARK should be propagated to backend."
5251   (memq mark gnus-article-unpropagated-mark-lists))
5252
5253 (defun gnus-adjust-marked-articles (info)
5254   "Set all article lists and remove all marks that are no longer valid."
5255   (let* ((marked-lists (gnus-info-marks info))
5256          (active (gnus-active (gnus-info-group info)))
5257          (min (car active))
5258          (max (cdr active))
5259          (types gnus-article-mark-lists)
5260          marks var articles article mark mark-type)
5261
5262     (dolist (marks marked-lists)
5263       (setq mark (car marks)
5264             mark-type (gnus-article-mark-to-type mark)
5265             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5266
5267       ;; We set the variable according to the type of the marks list,
5268       ;; and then adjust the marks to a subset of the active articles.
5269       (cond
5270        ;; Adjust "simple" lists.
5271        ((eq mark-type 'list)
5272         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5273         (when (memq mark '(tick dormant expire reply save))
5274           (while articles
5275             (when (or (< (setq article (pop articles)) min) (> article max))
5276               (set var (delq article (symbol-value var)))))))
5277        ;; Adjust assocs.
5278        ((eq mark-type 'tuple)
5279         (set var (setq articles (cdr marks)))
5280         (when (not (listp (cdr (symbol-value var))))
5281           (set var (list (symbol-value var))))
5282         (when (not (listp (cdr articles)))
5283           (setq articles (list articles)))
5284         (while articles
5285           (when (or (not (consp (setq article (pop articles))))
5286                     (< (car article) min)
5287                     (> (car article) max))
5288             (set var (delq article (symbol-value var))))))
5289        ;; Adjust ranges (sloppily).
5290        ((eq mark-type 'range)
5291         (cond
5292          ((eq mark 'seen)
5293           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5294           ;; It should be (seen (NUM1 . NUM2)).
5295           (when (numberp (cddr marks))
5296             (setcdr marks (list (cdr marks))))
5297           (setq articles (cdr marks))
5298           (while (and articles
5299                       (or (and (consp (car articles))
5300                                (> min (cdar articles)))
5301                           (and (numberp (car articles))
5302                                (> min (car articles)))))
5303             (pop articles))
5304           (set var articles))))))))
5305
5306 (defun gnus-update-missing-marks (missing)
5307   "Go through the list of MISSING articles and remove them from the mark lists."
5308   (when missing
5309     (let (var m)
5310       ;; Go through all types.
5311       (dolist (elem gnus-article-mark-lists)
5312         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5313           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5314           (when (symbol-value var)
5315             ;; This list has articles.  So we delete all missing
5316             ;; articles from it.
5317             (setq m missing)
5318             (while m
5319               (set var (delq (pop m) (symbol-value var))))))))))
5320
5321 (defun gnus-update-marks ()
5322   "Enter the various lists of marked articles into the newsgroup info list."
5323   (let ((types gnus-article-mark-lists)
5324         (info (gnus-get-info gnus-newsgroup-name))
5325         type list newmarked symbol delta-marks)
5326     (when info
5327       ;; Add all marks lists to the list of marks lists.
5328       (while (setq type (pop types))
5329         (setq list (symbol-value
5330                     (setq symbol
5331                           (intern (format "gnus-newsgroup-%s" (car type))))))
5332
5333         (when list
5334           ;; Get rid of the entries of the articles that have the
5335           ;; default score.
5336           (when (and (eq (cdr type) 'score)
5337                      gnus-save-score
5338                      list)
5339             (let* ((arts list)
5340                    (prev (cons nil list))
5341                    (all prev))
5342               (while arts
5343                 (if (or (not (consp (car arts)))
5344                         (= (cdar arts) gnus-summary-default-score))
5345                     (setcdr prev (cdr arts))
5346                   (setq prev arts))
5347                 (setq arts (cdr arts)))
5348               (setq list (cdr all)))))
5349
5350         (when (eq (cdr type) 'seen)
5351           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5352
5353         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5354           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5355
5356         (when (and (gnus-check-backend-function
5357                     'request-set-mark gnus-newsgroup-name)
5358                    (not (gnus-article-unpropagatable-p (cdr type))))
5359           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5360                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5361                  (add (gnus-remove-from-range
5362                        (gnus-copy-sequence list) old)))
5363             (when add
5364               (push (list add 'add (list (cdr type))) delta-marks))
5365             (when del
5366               (push (list del 'del (list (cdr type))) delta-marks))))
5367
5368         (when list
5369           (push (cons (cdr type) list) newmarked)))
5370
5371       (when delta-marks
5372         (unless (gnus-check-group gnus-newsgroup-name)
5373           (error "Can't open server for %s" gnus-newsgroup-name))
5374         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5375
5376       ;; Enter these new marks into the info of the group.
5377       (if (nthcdr 3 info)
5378           (setcar (nthcdr 3 info) newmarked)
5379         ;; Add the marks lists to the end of the info.
5380         (when newmarked
5381           (setcdr (nthcdr 2 info) (list newmarked))))
5382
5383       ;; Cut off the end of the info if there's nothing else there.
5384       (let ((i 5))
5385         (while (and (> i 2)
5386                     (not (nth i info)))
5387           (when (nthcdr (decf i) info)
5388             (setcdr (nthcdr i info) nil)))))))
5389
5390 (defun gnus-set-mode-line (where)
5391   "Set the mode line of the article or summary buffers.
5392 If WHERE is `summary', the summary mode line format will be used."
5393   ;; Is this mode line one we keep updated?
5394   (when (and (memq where gnus-updated-mode-lines)
5395              (symbol-value
5396               (intern (format "gnus-%s-mode-line-format-spec" where))))
5397     (let (mode-string)
5398       (save-excursion
5399         ;; We evaluate this in the summary buffer since these
5400         ;; variables are buffer-local to that buffer.
5401         (set-buffer gnus-summary-buffer)
5402        ;; We bind all these variables that are used in the `eval' form
5403         ;; below.
5404         (let* ((mformat (symbol-value
5405                          (intern
5406                           (format "gnus-%s-mode-line-format-spec" where))))
5407                (gnus-tmp-group-name (gnus-group-decoded-name
5408                                      gnus-newsgroup-name))
5409                (gnus-tmp-article-number (or gnus-current-article 0))
5410                (gnus-tmp-unread gnus-newsgroup-unreads)
5411                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5412                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5413                (gnus-tmp-unread-and-unselected
5414                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5415                             (zerop gnus-tmp-unselected))
5416                        "")
5417                       ((zerop gnus-tmp-unselected)
5418                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5419                       (t (format "{%d(+%d) more}"
5420                                  gnus-tmp-unread-and-unticked
5421                                  gnus-tmp-unselected))))
5422                (gnus-tmp-subject
5423                 (if (and gnus-current-headers
5424                          (vectorp gnus-current-headers))
5425                     (gnus-mode-string-quote
5426                      (mail-header-subject gnus-current-headers))
5427                   ""))
5428                bufname-length max-len
5429                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5430           (setq mode-string (eval mformat))
5431           (setq bufname-length (if (string-match "%b" mode-string)
5432                                    (- (length
5433                                        (buffer-name
5434                                         (if (eq where 'summary)
5435                                             nil
5436                                           (get-buffer gnus-article-buffer))))
5437                                       2)
5438                                  0))
5439           (setq max-len (max 4 (if gnus-mode-non-string-length
5440                                    (- (window-width)
5441                                       gnus-mode-non-string-length
5442                                       bufname-length)
5443                                  (length mode-string))))
5444           ;; We might have to chop a bit of the string off...
5445           (when (> (length mode-string) max-len)
5446             (setq mode-string
5447                   (concat (truncate-string-to-width mode-string (- max-len 3))
5448                           "...")))
5449           ;; Pad the mode string a bit.
5450           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5451       ;; Update the mode line.
5452       (setq mode-line-buffer-identification
5453             (gnus-mode-line-buffer-identification (list mode-string)))
5454       (set-buffer-modified-p t))))
5455
5456 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5457   "Go through the HEADERS list and add all Xrefs to a hash table.
5458 The resulting hash table is returned, or nil if no Xrefs were found."
5459   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5460          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5461          (xref-hashtb (gnus-make-hashtable))
5462          start group entry number xrefs header)
5463     (while headers
5464       (setq header (pop headers))
5465       (when (and (setq xrefs (mail-header-xref header))
5466                  (not (memq (setq number (mail-header-number header))
5467                             unreads)))
5468         (setq start 0)
5469         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5470           (setq start (match-end 0))
5471           (setq group (if prefix
5472                           (concat prefix (substring xrefs (match-beginning 1)
5473                                                     (match-end 1)))
5474                         (substring xrefs (match-beginning 1) (match-end 1))))
5475           (setq number
5476                 (string-to-int (substring xrefs (match-beginning 2)
5477                                           (match-end 2))))
5478           (if (setq entry (gnus-gethash group xref-hashtb))
5479               (setcdr entry (cons number (cdr entry)))
5480             (gnus-sethash group (cons number nil) xref-hashtb)))))
5481     (and start xref-hashtb)))
5482
5483 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5484   "Look through all the headers and mark the Xrefs as read."
5485   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5486         name entry info xref-hashtb idlist method nth4)
5487     (save-excursion
5488       (set-buffer gnus-group-buffer)
5489       (when (setq xref-hashtb
5490                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5491         (mapatoms
5492          (lambda (group)
5493            (unless (string= from-newsgroup (setq name (symbol-name group)))
5494              (setq idlist (symbol-value group))
5495              ;; Dead groups are not updated.
5496              (and (prog1
5497                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5498                             info (nth 2 entry))
5499                     (when (stringp (setq nth4 (gnus-info-method info)))
5500                       (setq nth4 (gnus-server-to-method nth4))))
5501                   ;; Only do the xrefs if the group has the same
5502                   ;; select method as the group we have just read.
5503                   (or (gnus-methods-equal-p
5504                        nth4 (gnus-find-method-for-group from-newsgroup))
5505                       virtual
5506                       (equal nth4 (setq method (gnus-find-method-for-group
5507                                                 from-newsgroup)))
5508                       (and (equal (car nth4) (car method))
5509                            (equal (nth 1 nth4) (nth 1 method))))
5510                   gnus-use-cross-reference
5511                   (or (not (eq gnus-use-cross-reference t))
5512                       virtual
5513                       ;; Only do cross-references on subscribed
5514                       ;; groups, if that is what is wanted.
5515                       (<= (gnus-info-level info) gnus-level-subscribed))
5516                   (gnus-group-make-articles-read name idlist))))
5517          xref-hashtb)))))
5518
5519 (defun gnus-compute-read-articles (group articles)
5520   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5521          (info (nth 2 entry))
5522          (active (gnus-active group))
5523          ninfo)
5524     (when entry
5525       ;; First peel off all invalid article numbers.
5526       (when active
5527         (let ((ids articles)
5528               id first)
5529           (while (setq id (pop ids))
5530             (when (and first (> id (cdr active)))
5531               ;; We'll end up in this situation in one particular
5532               ;; obscure situation.  If you re-scan a group and get
5533               ;; a new article that is cross-posted to a different
5534               ;; group that has not been re-scanned, you might get
5535               ;; crossposted article that has a higher number than
5536               ;; Gnus believes possible.  So we re-activate this
5537               ;; group as well.  This might mean doing the
5538               ;; crossposting thingy will *increase* the number
5539               ;; of articles in some groups.  Tsk, tsk.
5540               (setq active (or (gnus-activate-group group) active)))
5541             (when (or (> id (cdr active))
5542                       (< id (car active)))
5543               (setq articles (delq id articles))))))
5544       ;; If the read list is nil, we init it.
5545       (if (and active
5546                (null (gnus-info-read info))
5547                (> (car active) 1))
5548           (setq ninfo (cons 1 (1- (car active))))
5549         (setq ninfo (gnus-info-read info)))
5550       ;; Then we add the read articles to the range.
5551       (gnus-add-to-range
5552        ninfo (setq articles (sort articles '<))))))
5553
5554 (defun gnus-group-make-articles-read (group articles)
5555   "Update the info of GROUP to say that ARTICLES are read."
5556   (let* ((num 0)
5557          (entry (gnus-gethash group gnus-newsrc-hashtb))
5558          (info (nth 2 entry))
5559          (active (gnus-active group))
5560          range)
5561     (when entry
5562       (setq range (gnus-compute-read-articles group articles))
5563       (save-excursion
5564         (set-buffer gnus-group-buffer)
5565         (gnus-undo-register
5566           `(progn
5567              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5568              (gnus-info-set-read ',info ',(gnus-info-read info))
5569              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5570              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5571              (gnus-group-update-group ,group t))))
5572       ;; Add the read articles to the range.
5573       (gnus-info-set-read info range)
5574       (gnus-request-set-mark group (list (list range 'add '(read))))
5575       ;; Then we have to re-compute how many unread
5576       ;; articles there are in this group.
5577       (when active
5578         (cond
5579          ((not range)
5580           (setq num (- (1+ (cdr active)) (car active))))
5581          ((not (listp (cdr range)))
5582           (setq num (- (cdr active) (- (1+ (cdr range))
5583                                        (car range)))))
5584          (t
5585           (while range
5586             (if (numberp (car range))
5587                 (setq num (1+ num))
5588               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5589             (setq range (cdr range)))
5590           (setq num (- (cdr active) num))))
5591         ;; Update the number of unread articles.
5592         (setcar entry num)
5593         ;; Update the group buffer.
5594         (unless (gnus-ephemeral-group-p group)
5595           (gnus-group-update-group group t))))))
5596
5597 (defvar gnus-newsgroup-none-id 0)
5598
5599 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5600   (let ((cur nntp-server-buffer)
5601         (dependencies
5602          (or dependencies
5603              (save-excursion (set-buffer gnus-summary-buffer)
5604                              gnus-newsgroup-dependencies)))
5605         headers id end ref
5606         (mail-parse-charset gnus-newsgroup-charset)
5607         (mail-parse-ignored-charsets
5608          (save-excursion (condition-case nil
5609                              (set-buffer gnus-summary-buffer)
5610                            (error))
5611                          gnus-newsgroup-ignored-charsets)))
5612     (save-excursion
5613       (set-buffer nntp-server-buffer)
5614       ;; Translate all TAB characters into SPACE characters.
5615       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5616       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5617       (gnus-run-hooks 'gnus-parse-headers-hook)
5618       (let ((case-fold-search t)
5619             in-reply-to header p lines chars)
5620         (goto-char (point-min))
5621         ;; Search to the beginning of the next header.  Error messages
5622         ;; do not begin with 2 or 3.
5623         (while (re-search-forward "^[23][0-9]+ " nil t)
5624           (setq id nil
5625                 ref nil)
5626           ;; This implementation of this function, with nine
5627           ;; search-forwards instead of the one re-search-forward and
5628           ;; a case (which basically was the old function) is actually
5629           ;; about twice as fast, even though it looks messier.  You
5630           ;; can't have everything, I guess.  Speed and elegance
5631           ;; doesn't always go hand in hand.
5632           (setq
5633            header
5634            (vector
5635             ;; Number.
5636             (prog1
5637                 (read cur)
5638               (end-of-line)
5639               (setq p (point))
5640               (narrow-to-region (point)
5641                                 (or (and (search-forward "\n.\n" nil t)
5642                                          (- (point) 2))
5643                                     (point))))
5644             ;; Subject.
5645             (progn
5646               (goto-char p)
5647               (if (search-forward "\nsubject:" nil t)
5648                   (funcall gnus-decode-encoded-word-function
5649                            (nnheader-header-value))
5650                 "(none)"))
5651             ;; From.
5652             (progn
5653               (goto-char p)
5654               (if (search-forward "\nfrom:" nil t)
5655                   (funcall gnus-decode-encoded-word-function
5656                            (nnheader-header-value))
5657                 "(nobody)"))
5658             ;; Date.
5659             (progn
5660               (goto-char p)
5661               (if (search-forward "\ndate:" nil t)
5662                   (nnheader-header-value) ""))
5663             ;; Message-ID.
5664             (progn
5665               (goto-char p)
5666               (setq id (if (re-search-forward
5667                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5668                            ;; We do it this way to make sure the Message-ID
5669                            ;; is (somewhat) syntactically valid.
5670                            (buffer-substring (match-beginning 1)
5671                                              (match-end 1))
5672                          ;; If there was no message-id, we just fake one
5673                          ;; to make subsequent routines simpler.
5674                          (nnheader-generate-fake-message-id))))
5675             ;; References.
5676             (progn
5677               (goto-char p)
5678               (if (search-forward "\nreferences:" nil t)
5679                   (progn
5680                     (setq end (point))
5681                     (prog1
5682                         (nnheader-header-value)
5683                       (setq ref
5684                             (buffer-substring
5685                              (progn
5686                                (end-of-line)
5687                                (search-backward ">" end t)
5688                                (1+ (point)))
5689                              (progn
5690                                (search-backward "<" end t)
5691                                (point))))))
5692                 ;; Get the references from the in-reply-to header if there
5693                 ;; were no references and the in-reply-to header looks
5694                 ;; promising.
5695                 (if (and (search-forward "\nin-reply-to:" nil t)
5696                          (setq in-reply-to (nnheader-header-value))
5697                          (string-match "<[^>]+>" in-reply-to))
5698                     (let (ref2)
5699                       (setq ref (substring in-reply-to (match-beginning 0)
5700                                            (match-end 0)))
5701                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5702                         (setq ref2 (substring in-reply-to (match-beginning 0)
5703                                               (match-end 0)))
5704                         (when (> (length ref2) (length ref))
5705                           (setq ref ref2)))
5706                       ref)
5707                   (setq ref nil))))
5708             ;; Chars.
5709             (progn
5710               (goto-char p)
5711               (if (search-forward "\nchars: " nil t)
5712                   (if (numberp (setq chars (ignore-errors (read cur))))
5713                       chars -1)
5714                 -1))
5715             ;; Lines.
5716             (progn
5717               (goto-char p)
5718               (if (search-forward "\nlines: " nil t)
5719                   (if (numberp (setq lines (ignore-errors (read cur))))
5720                       lines -1)
5721                 -1))
5722             ;; Xref.
5723             (progn
5724               (goto-char p)
5725               (and (search-forward "\nxref:" nil t)
5726                    (nnheader-header-value)))
5727             ;; Extra.
5728             (when gnus-extra-headers
5729               (let ((extra gnus-extra-headers)
5730                     out)
5731                 (while extra
5732                   (goto-char p)
5733                   (when (search-forward
5734                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5735                     (push (cons (car extra) (nnheader-header-value))
5736                           out))
5737                   (pop extra))
5738                 out))))
5739           (when (equal id ref)
5740             (setq ref nil))
5741
5742           (when gnus-alter-header-function
5743             (funcall gnus-alter-header-function header)
5744             (setq id (mail-header-id header)
5745                   ref (gnus-parent-id (mail-header-references header))))
5746
5747           (when (setq header
5748                       (gnus-dependencies-add-header
5749                        header dependencies force-new))
5750             (push header headers))
5751           (goto-char (point-max))
5752           (widen))
5753         (nreverse headers)))))
5754
5755 ;; Goes through the xover lines and returns a list of vectors
5756 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5757                                                   force-new dependencies
5758                                                   group also-fetch-heads)
5759   "Parse the news overview data in the server buffer.
5760 Return a list of headers that match SEQUENCE (see
5761 `nntp-retrieve-headers')."
5762   ;; Get the Xref when the users reads the articles since most/some
5763   ;; NNTP servers do not include Xrefs when using XOVER.
5764   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5765   (let ((mail-parse-charset gnus-newsgroup-charset)
5766         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5767         (cur nntp-server-buffer)
5768         (dependencies (or dependencies gnus-newsgroup-dependencies))
5769         (allp (cond
5770                ((eq gnus-read-all-available-headers t)
5771                 t)
5772                ((stringp gnus-read-all-available-headers)
5773                 (string-match gnus-read-all-available-headers group))
5774                (t
5775                 nil)))
5776         number headers header)
5777     (save-excursion
5778       (set-buffer nntp-server-buffer)
5779       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5780       ;; Allow the user to mangle the headers before parsing them.
5781       (gnus-run-hooks 'gnus-parse-headers-hook)
5782       (goto-char (point-min))
5783       (gnus-parse-without-error
5784         (while (and (or sequence allp)
5785                     (not (eobp)))
5786           (setq number (read cur))
5787           (when (not allp)
5788             (while (and sequence
5789                         (< (car sequence) number))
5790               (setq sequence (cdr sequence))))
5791           (when (and (or allp
5792                          (and sequence
5793                               (eq number (car sequence))))
5794                      (progn
5795                        (setq sequence (cdr sequence))
5796                        (setq header (inline
5797                                       (gnus-nov-parse-line
5798                                        number dependencies force-new)))))
5799             (push header headers))
5800           (forward-line 1)))
5801       ;; A common bug in inn is that if you have posted an article and
5802       ;; then retrieves the active file, it will answer correctly --
5803       ;; the new article is included.  However, a NOV entry for the
5804       ;; article may not have been generated yet, so this may fail.
5805       ;; We work around this problem by retrieving the last few
5806       ;; headers using HEAD.
5807       (if (or (not also-fetch-heads)
5808               (not sequence))
5809           ;; We (probably) got all the headers.
5810           (nreverse headers)
5811         (let ((gnus-nov-is-evil t))
5812           (nconc
5813            (nreverse headers)
5814            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5815              (gnus-get-newsgroup-headers))))))))
5816
5817 (defun gnus-article-get-xrefs ()
5818   "Fill in the Xref value in `gnus-current-headers', if necessary.
5819 This is meant to be called in `gnus-article-internal-prepare-hook'."
5820   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5821                                  gnus-current-headers)))
5822     (or (not gnus-use-cross-reference)
5823         (not headers)
5824         (and (mail-header-xref headers)
5825              (not (string= (mail-header-xref headers) "")))
5826         (let ((case-fold-search t)
5827               xref)
5828           (save-restriction
5829             (nnheader-narrow-to-headers)
5830             (goto-char (point-min))
5831             (when (or (and (not (eobp))
5832                            (eq (downcase (char-after)) ?x)
5833                            (looking-at "Xref:"))
5834                       (search-forward "\nXref:" nil t))
5835               (goto-char (1+ (match-end 0)))
5836               (setq xref (buffer-substring (point)
5837                                            (progn (end-of-line) (point))))
5838               (mail-header-set-xref headers xref)))))))
5839
5840 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5841   "Find article ID and insert the summary line for that article.
5842 OLD-HEADER can either be a header or a line number to insert
5843 the subject line on."
5844   (let* ((line (and (numberp old-header) old-header))
5845          (old-header (and (vectorp old-header) old-header))
5846          (header (cond ((and old-header use-old-header)
5847                         old-header)
5848                        ((and (numberp id)
5849                              (gnus-number-to-header id))
5850                         (gnus-number-to-header id))
5851                        (t
5852                         (gnus-read-header id))))
5853          (number (and (numberp id) id))
5854          d)
5855     (when header
5856       ;; Rebuild the thread that this article is part of and go to the
5857       ;; article we have fetched.
5858       (when (and (not gnus-show-threads)
5859                  old-header)
5860         (when (and number
5861                    (setq d (gnus-data-find (mail-header-number old-header))))
5862           (goto-char (gnus-data-pos d))
5863           (gnus-data-remove
5864            number
5865            (- (gnus-point-at-bol)
5866               (prog1
5867                   (1+ (gnus-point-at-eol))
5868                 (gnus-delete-line))))))
5869       (when old-header
5870         (mail-header-set-number header (mail-header-number old-header)))
5871       (setq gnus-newsgroup-sparse
5872             (delq (setq number (mail-header-number header))
5873                   gnus-newsgroup-sparse))
5874       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5875       (push number gnus-newsgroup-limit)
5876       (gnus-rebuild-thread (mail-header-id header) line)
5877       (gnus-summary-goto-subject number nil t))
5878     (when (and (numberp number)
5879                (> number 0))
5880       ;; We have to update the boundaries even if we can't fetch the
5881       ;; article if ID is a number -- so that the next `P' or `N'
5882       ;; command will fetch the previous (or next) article even
5883       ;; if the one we tried to fetch this time has been canceled.
5884       (when (> number gnus-newsgroup-end)
5885         (setq gnus-newsgroup-end number))
5886       (when (< number gnus-newsgroup-begin)
5887         (setq gnus-newsgroup-begin number))
5888       (setq gnus-newsgroup-unselected
5889             (delq number gnus-newsgroup-unselected)))
5890     ;; Report back a success?
5891     (and header (mail-header-number header))))
5892
5893 ;;; Process/prefix in the summary buffer
5894
5895 (defun gnus-summary-work-articles (n)
5896   "Return a list of articles to be worked upon.
5897 The prefix argument, the list of process marked articles, and the
5898 current article will be taken into consideration."
5899   (save-excursion
5900     (set-buffer gnus-summary-buffer)
5901     (cond
5902      (n
5903       ;; A numerical prefix has been given.
5904       (setq n (prefix-numeric-value n))
5905       (let ((backward (< n 0))
5906             (n (abs (prefix-numeric-value n)))
5907             articles article)
5908         (save-excursion
5909           (while
5910               (and (> n 0)
5911                    (push (setq article (gnus-summary-article-number))
5912                          articles)
5913                    (if backward
5914                        (gnus-summary-find-prev nil article)
5915                      (gnus-summary-find-next nil article)))
5916             (decf n)))
5917         (nreverse articles)))
5918      ((and (gnus-region-active-p) (mark))
5919       (message "region active")
5920       ;; Work on the region between point and mark.
5921       (let ((max (max (point) (mark)))
5922             articles article)
5923         (save-excursion
5924           (goto-char (min (min (point) (mark))))
5925           (while
5926               (and
5927                (push (setq article (gnus-summary-article-number)) articles)
5928                (gnus-summary-find-next nil article)
5929                (< (point) max)))
5930           (nreverse articles))))
5931      (gnus-newsgroup-processable
5932       ;; There are process-marked articles present.
5933       ;; Save current state.
5934       (gnus-summary-save-process-mark)
5935       ;; Return the list.
5936       (reverse gnus-newsgroup-processable))
5937      (t
5938       ;; Just return the current article.
5939       (list (gnus-summary-article-number))))))
5940
5941 (defmacro gnus-summary-iterate (arg &rest forms)
5942   "Iterate over the process/prefixed articles and do FORMS.
5943 ARG is the interactive prefix given to the command.  FORMS will be
5944 executed with point over the summary line of the articles."
5945   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5946     `(let ((,articles (gnus-summary-work-articles ,arg)))
5947        (while ,articles
5948          (gnus-summary-goto-subject (car ,articles))
5949          ,@forms
5950          (pop ,articles)))))
5951
5952 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5953 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5954
5955 (defun gnus-summary-save-process-mark ()
5956   "Push the current set of process marked articles on the stack."
5957   (interactive)
5958   (push (copy-sequence gnus-newsgroup-processable)
5959         gnus-newsgroup-process-stack))
5960
5961 (defun gnus-summary-kill-process-mark ()
5962   "Push the current set of process marked articles on the stack and unmark."
5963   (interactive)
5964   (gnus-summary-save-process-mark)
5965   (gnus-summary-unmark-all-processable))
5966
5967 (defun gnus-summary-yank-process-mark ()
5968   "Pop the last process mark state off the stack and restore it."
5969   (interactive)
5970   (unless gnus-newsgroup-process-stack
5971     (error "Empty mark stack"))
5972   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5973
5974 (defun gnus-summary-process-mark-set (set)
5975   "Make SET into the current process marked articles."
5976   (gnus-summary-unmark-all-processable)
5977   (while set
5978     (gnus-summary-set-process-mark (pop set))))
5979
5980 ;;; Searching and stuff
5981
5982 (defun gnus-summary-search-group (&optional backward use-level)
5983   "Search for next unread newsgroup.
5984 If optional argument BACKWARD is non-nil, search backward instead."
5985   (save-excursion
5986     (set-buffer gnus-group-buffer)
5987     (when (gnus-group-search-forward
5988            backward nil (if use-level (gnus-group-group-level) nil))
5989       (gnus-group-group-name))))
5990
5991 (defun gnus-summary-best-group (&optional exclude-group)
5992   "Find the name of the best unread group.
5993 If EXCLUDE-GROUP, do not go to this group."
5994   (save-excursion
5995     (set-buffer gnus-group-buffer)
5996     (save-excursion
5997       (gnus-group-best-unread-group exclude-group))))
5998
5999 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
6000   (if backward (gnus-summary-find-prev)
6001     (let* ((dummy (gnus-summary-article-intangible-p))
6002            (article (or article (gnus-summary-article-number)))
6003            (arts (gnus-data-find-list article))
6004            result)
6005       (when (and (not dummy)
6006                  (or (not gnus-summary-check-current)
6007                      (not unread)
6008                      (not (gnus-data-unread-p (car arts)))))
6009         (setq arts (cdr arts)))
6010       (when (setq result
6011                   (if unread
6012                       (progn
6013                         (while arts
6014                           (when (or (and undownloaded
6015                                          (memq (car arts)
6016                                                gnus-newsgroup-undownloaded))
6017                                     (gnus-data-unread-p (car arts)))
6018                             (setq result (car arts)
6019                                   arts nil))
6020                           (setq arts (cdr arts)))
6021                         result)
6022                     (car arts)))
6023         (goto-char (gnus-data-pos result))
6024         (gnus-data-number result)))))
6025
6026 (defun gnus-summary-find-prev (&optional unread article)
6027   (let* ((eobp (eobp))
6028          (article (or article (gnus-summary-article-number)))
6029          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
6030          result)
6031     (when (and (not eobp)
6032                (or (not gnus-summary-check-current)
6033                    (not unread)
6034                    (not (gnus-data-unread-p (car arts)))))
6035       (setq arts (cdr arts)))
6036     (when (setq result
6037                 (if unread
6038                     (progn
6039                       (while arts
6040                         (when (gnus-data-unread-p (car arts))
6041                           (setq result (car arts)
6042                                 arts nil))
6043                         (setq arts (cdr arts)))
6044                       result)
6045                   (car arts)))
6046       (goto-char (gnus-data-pos result))
6047       (gnus-data-number result))))
6048
6049 (defun gnus-summary-find-subject (subject &optional unread backward article)
6050   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6051          (article (or article (gnus-summary-article-number)))
6052          (articles (gnus-data-list backward))
6053          (arts (gnus-data-find-list article articles))
6054          result)
6055     (when (or (not gnus-summary-check-current)
6056               (not unread)
6057               (not (gnus-data-unread-p (car arts))))
6058       (setq arts (cdr arts)))
6059     (while arts
6060       (and (or (not unread)
6061                (gnus-data-unread-p (car arts)))
6062            (vectorp (gnus-data-header (car arts)))
6063            (gnus-subject-equal
6064             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6065            (setq result (car arts)
6066                  arts nil))
6067       (setq arts (cdr arts)))
6068     (and result
6069          (goto-char (gnus-data-pos result))
6070          (gnus-data-number result))))
6071
6072 (defun gnus-summary-search-forward (&optional unread subject backward)
6073   "Search forward for an article.
6074 If UNREAD, look for unread articles.  If SUBJECT, look for
6075 articles with that subject.  If BACKWARD, search backward instead."
6076   (cond (subject (gnus-summary-find-subject subject unread backward))
6077         (backward (gnus-summary-find-prev unread))
6078         (t (gnus-summary-find-next unread))))
6079
6080 (defun gnus-recenter (&optional n)
6081   "Center point in window and redisplay frame.
6082 Also do horizontal recentering."
6083   (interactive "P")
6084   (when (and gnus-auto-center-summary
6085              (not (eq gnus-auto-center-summary 'vertical)))
6086     (gnus-horizontal-recenter))
6087   (recenter n))
6088
6089 (defun gnus-summary-recenter ()
6090   "Center point in the summary window.
6091 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6092 displayed, no centering will be performed."
6093   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6094 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6095   (interactive)
6096   (let* ((top (cond ((< (window-height) 4) 0)
6097                     ((< (window-height) 7) 1)
6098                     (t (if (numberp gnus-auto-center-summary)
6099                            gnus-auto-center-summary
6100                          2))))
6101          (height (1- (window-height)))
6102          (bottom (save-excursion (goto-char (point-max))
6103                                  (forward-line (- height))
6104                                  (point)))
6105          (window (get-buffer-window (current-buffer))))
6106     ;; The user has to want it.
6107     (when gnus-auto-center-summary
6108       (when (get-buffer-window gnus-article-buffer)
6109         ;; Only do recentering when the article buffer is displayed,
6110       ;; Set the window start to either `bottom', which is the biggest
6111         ;; possible valid number, or the second line from the top,
6112         ;; whichever is the least.
6113         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6114           (if (> bottom top-pos)
6115               ;; Keep the second line from the top visible
6116               (set-window-start window top-pos t)
6117             ;; Try to keep the bottom line visible; if it's partially
6118             ;; obscured, either scroll one more line to make it fully
6119             ;; visible, or revert to using TOP-POS.
6120             (save-excursion
6121               (goto-char (point-max))
6122               (forward-line -1)
6123               (let ((last-line-start (point)))
6124                 (goto-char bottom)
6125                 (set-window-start window (point) t)
6126                 (when (not (pos-visible-in-window-p last-line-start window))
6127                   (forward-line 1)
6128                   (set-window-start window (min (point) top-pos) t)))))))
6129       ;; Do horizontal recentering while we're at it.
6130       (when (and (get-buffer-window (current-buffer) t)
6131                  (not (eq gnus-auto-center-summary 'vertical)))
6132         (let ((selected (selected-window)))
6133           (select-window (get-buffer-window (current-buffer) t))
6134           (gnus-summary-position-point)
6135           (gnus-horizontal-recenter)
6136           (select-window selected))))))
6137
6138 (defun gnus-summary-jump-to-group (newsgroup)
6139   "Move point to NEWSGROUP in group mode buffer."
6140   ;; Keep update point of group mode buffer if visible.
6141   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6142       (save-window-excursion
6143         ;; Take care of tree window mode.
6144         (when (get-buffer-window gnus-group-buffer)
6145           (pop-to-buffer gnus-group-buffer))
6146         (gnus-group-jump-to-group newsgroup))
6147     (save-excursion
6148       ;; Take care of tree window mode.
6149       (if (get-buffer-window gnus-group-buffer)
6150           (pop-to-buffer gnus-group-buffer)
6151         (set-buffer gnus-group-buffer))
6152       (gnus-group-jump-to-group newsgroup))))
6153
6154 ;; This function returns a list of article numbers based on the
6155 ;; difference between the ranges of read articles in this group and
6156 ;; the range of active articles.
6157 (defun gnus-list-of-unread-articles (group)
6158   (let* ((read (gnus-info-read (gnus-get-info group)))
6159          (active (or (gnus-active group) (gnus-activate-group group)))
6160          (last (cdr active))
6161          first nlast unread)
6162     ;; If none are read, then all are unread.
6163     (if (not read)
6164         (setq first (car active))
6165       ;; If the range of read articles is a single range, then the
6166       ;; first unread article is the article after the last read
6167       ;; article.  Sounds logical, doesn't it?
6168       (if (and (not (listp (cdr read)))
6169                (or (< (car read) (car active))
6170                    (progn (setq read (list read))
6171                           nil)))
6172           (setq first (max (car active) (1+ (cdr read))))
6173         ;; `read' is a list of ranges.
6174         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6175                                   (caar read)))
6176                   1)
6177           (setq first (car active)))
6178         (while read
6179           (when first
6180             (while (< first nlast)
6181               (push first unread)
6182               (setq first (1+ first))))
6183           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6184           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6185           (setq read (cdr read)))))
6186     ;; And add the last unread articles.
6187     (while (<= first last)
6188       (push first unread)
6189       (setq first (1+ first)))
6190     ;; Return the list of unread articles.
6191     (delq 0 (nreverse unread))))
6192
6193 (defun gnus-list-of-read-articles (group)
6194   "Return a list of unread, unticked and non-dormant articles."
6195   (let* ((info (gnus-get-info group))
6196          (marked (gnus-info-marks info))
6197          (active (gnus-active group)))
6198     (and info active
6199          (gnus-list-range-difference
6200           (gnus-list-range-difference
6201            (gnus-sorted-complement
6202             (gnus-uncompress-range active)
6203             (gnus-list-of-unread-articles group))
6204            (cdr (assq 'dormant marked)))
6205           (cdr (assq 'tick marked))))))
6206
6207 ;; Various summary commands
6208
6209 (defun gnus-summary-select-article-buffer ()
6210   "Reconfigure windows to show article buffer."
6211   (interactive)
6212   (if (not (gnus-buffer-live-p gnus-article-buffer))
6213       (error "There is no article buffer for this summary buffer")
6214     (gnus-configure-windows 'article)
6215     (select-window (get-buffer-window gnus-article-buffer))))
6216
6217 (defun gnus-summary-universal-argument (arg)
6218   "Perform any operation on all articles that are process/prefixed."
6219   (interactive "P")
6220   (let ((articles (gnus-summary-work-articles arg))
6221         func article)
6222     (if (eq
6223          (setq
6224           func
6225           (key-binding
6226            (read-key-sequence
6227             (substitute-command-keys
6228              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6229          'undefined)
6230         (gnus-error 1 "Undefined key")
6231       (save-excursion
6232         (while articles
6233           (gnus-summary-goto-subject (setq article (pop articles)))
6234           (let (gnus-newsgroup-processable)
6235             (command-execute func))
6236           (gnus-summary-remove-process-mark article)))))
6237   (gnus-summary-position-point))
6238
6239 (defun gnus-summary-toggle-truncation (&optional arg)
6240   "Toggle truncation of summary lines.
6241 With arg, turn line truncation on if arg is positive."
6242   (interactive "P")
6243   (setq truncate-lines
6244         (if (null arg) (not truncate-lines)
6245           (> (prefix-numeric-value arg) 0)))
6246   (redraw-display))
6247
6248 (defun gnus-summary-find-uncancelled ()
6249   "Return the number of an uncancelled article.
6250 The current article is considered, then following articles, then previous
6251 articles.  If all articles are cancelled then return a dummy 0."
6252   (let (found)
6253     (dolist (rev '(nil t))
6254       (unless found      ; don't demand the reverse list if we don't need it
6255         (let ((data (gnus-data-find-list
6256                      (gnus-summary-article-number) (gnus-data-list rev))))
6257           (while (and data (not found))
6258             (if (not (eq gnus-canceled-mark (gnus-data-mark (car data))))
6259                 (setq found (gnus-data-number (car data))))
6260             (setq data (cdr data))))))
6261     (or found 0)))
6262
6263 (defun gnus-summary-reselect-current-group (&optional all rescan)
6264   "Exit and then reselect the current newsgroup.
6265 The prefix argument ALL means to select all articles."
6266   (interactive "P")
6267   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6268     (error "Ephemeral groups can't be reselected"))
6269   (let ((current-subject (gnus-summary-find-uncancelled))
6270         (group gnus-newsgroup-name))
6271     (setq gnus-newsgroup-begin nil)
6272     (gnus-summary-exit)
6273     ;; We have to adjust the point of group mode buffer because
6274     ;; point was moved to the next unread newsgroup by exiting.
6275     (gnus-summary-jump-to-group group)
6276     (when rescan
6277       (save-excursion
6278         (gnus-group-get-new-news-this-group 1)))
6279     (gnus-group-read-group all t)
6280     (gnus-summary-goto-subject current-subject nil t)))
6281
6282 (defun gnus-summary-rescan-group (&optional all)
6283   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6284   (interactive "P")
6285   (gnus-summary-reselect-current-group all t))
6286
6287 (defun gnus-summary-update-info (&optional non-destructive)
6288   (save-excursion
6289     (let ((group gnus-newsgroup-name))
6290       (when group
6291         (when gnus-newsgroup-kill-headers
6292           (setq gnus-newsgroup-killed
6293                 (gnus-compress-sequence
6294                  (gnus-sorted-union
6295                   (gnus-list-range-intersection
6296                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6297                   gnus-newsgroup-unreads)
6298                  t)))
6299         (unless (listp (cdr gnus-newsgroup-killed))
6300           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6301         (let ((headers gnus-newsgroup-headers))
6302           ;; Set the new ranges of read articles.
6303           (save-excursion
6304             (set-buffer gnus-group-buffer)
6305             (gnus-undo-force-boundary))
6306           (gnus-update-read-articles
6307            group (gnus-sorted-union
6308                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6309           ;; Set the current article marks.
6310           (let ((gnus-newsgroup-scored
6311                  (if (and (not gnus-save-score)
6312                           (not non-destructive))
6313                      nil
6314                    gnus-newsgroup-scored)))
6315             (save-excursion
6316               (gnus-update-marks)))
6317           ;; Do the cross-ref thing.
6318           (when gnus-use-cross-reference
6319             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6320           ;; Do not switch windows but change the buffer to work.
6321           (set-buffer gnus-group-buffer)
6322           (unless (gnus-ephemeral-group-p group)
6323             (gnus-group-update-group group)))))))
6324
6325 (defun gnus-summary-save-newsrc (&optional force)
6326   "Save the current number of read/marked articles in the dribble buffer.
6327 The dribble buffer will then be saved.
6328 If FORCE (the prefix), also save the .newsrc file(s)."
6329   (interactive "P")
6330   (gnus-summary-update-info t)
6331   (if force
6332       (gnus-save-newsrc-file)
6333     (gnus-dribble-save)))
6334
6335 (defun gnus-summary-exit (&optional temporary)
6336   "Exit reading current newsgroup, and then return to group selection mode.
6337 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6338   (interactive)
6339   (gnus-set-global-variables)
6340   (when (gnus-buffer-live-p gnus-article-buffer)
6341     (save-excursion
6342       (set-buffer gnus-article-buffer)
6343       (mm-destroy-parts gnus-article-mime-handles)
6344       ;; Set it to nil for safety reason.
6345       (setq gnus-article-mime-handle-alist nil)
6346       (setq gnus-article-mime-handles nil)))
6347   (gnus-kill-save-kill-buffer)
6348   (gnus-async-halt-prefetch)
6349   (let* ((group gnus-newsgroup-name)
6350          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6351          (gnus-group-is-exiting-p t)
6352          (mode major-mode)
6353          (group-point nil)
6354          (buf (current-buffer)))
6355     (unless quit-config
6356       ;; Do adaptive scoring, and possibly save score files.
6357       (when gnus-newsgroup-adaptive
6358         (gnus-score-adaptive))
6359       (when gnus-use-scoring
6360         (gnus-score-save)))
6361     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6362     ;; If we have several article buffers, we kill them at exit.
6363     (unless gnus-single-article-buffer
6364       (gnus-kill-buffer gnus-original-article-buffer)
6365       (setq gnus-article-current nil))
6366     (when gnus-use-cache
6367       (gnus-cache-possibly-remove-articles)
6368       (gnus-cache-save-buffers))
6369     (gnus-async-prefetch-remove-group group)
6370     (when gnus-suppress-duplicates
6371       (gnus-dup-enter-articles))
6372     (when gnus-use-trees
6373       (gnus-tree-close group))
6374     (when gnus-use-cache
6375       (gnus-cache-write-active))
6376     ;; Remove entries for this group.
6377     (nnmail-purge-split-history (gnus-group-real-name group))
6378     ;; Make all changes in this group permanent.
6379     (unless quit-config
6380       (gnus-run-hooks 'gnus-exit-group-hook)
6381       (gnus-summary-update-info))
6382     (gnus-close-group group)
6383     ;; Make sure where we were, and go to next newsgroup.
6384     (set-buffer gnus-group-buffer)
6385     (unless quit-config
6386       (gnus-group-jump-to-group group))
6387     (gnus-run-hooks 'gnus-summary-exit-hook)
6388     (unless (or quit-config
6389                 ;; If this group has disappeared from the summary
6390                 ;; buffer, don't skip forwards.
6391                 (not (string= group (gnus-group-group-name))))
6392       (gnus-group-next-unread-group 1))
6393     (setq group-point (point))
6394     (if temporary
6395         nil                             ;Nothing to do.
6396       ;; If we have several article buffers, we kill them at exit.
6397       (unless gnus-single-article-buffer
6398         (gnus-kill-buffer gnus-article-buffer)
6399         (gnus-kill-buffer gnus-original-article-buffer)
6400         (setq gnus-article-current nil))
6401       (set-buffer buf)
6402       (if (not gnus-kill-summary-on-exit)
6403           (progn
6404             (gnus-deaden-summary)
6405             (setq mode nil))
6406         ;; We set all buffer-local variables to nil.  It is unclear why
6407         ;; this is needed, but if we don't, buffer-local variables are
6408         ;; not garbage-collected, it seems.  This would the lead to en
6409         ;; ever-growing Emacs.
6410         (gnus-summary-clear-local-variables)
6411         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6412           (gnus-summary-clear-local-variables))
6413         (when (get-buffer gnus-article-buffer)
6414           (bury-buffer gnus-article-buffer))
6415         ;; We clear the global counterparts of the buffer-local
6416         ;; variables as well, just to be on the safe side.
6417         (set-buffer gnus-group-buffer)
6418         (gnus-summary-clear-local-variables)
6419         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6420           (gnus-summary-clear-local-variables))
6421         ;; Return to group mode buffer.
6422         (when (eq mode 'gnus-summary-mode)
6423           (gnus-kill-buffer buf)))
6424       (setq gnus-current-select-method gnus-select-method)
6425       (pop-to-buffer gnus-group-buffer)
6426       (if (not quit-config)
6427           (progn
6428             (goto-char group-point)
6429             (gnus-configure-windows 'group 'force))
6430         (gnus-handle-ephemeral-exit quit-config))
6431       ;; Clear the current group name.
6432       (unless quit-config
6433         (setq gnus-newsgroup-name nil)))))
6434
6435 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6436 (defun gnus-summary-exit-no-update (&optional no-questions)
6437   "Quit reading current newsgroup without updating read article info."
6438   (interactive)
6439   (let* ((group gnus-newsgroup-name)
6440          (gnus-group-is-exiting-p t)
6441          (quit-config (gnus-group-quit-config group)))
6442     (when (or no-questions
6443               gnus-expert-user
6444               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6445       (gnus-async-halt-prefetch)
6446       (mapcar 'funcall
6447               (delq 'gnus-summary-expire-articles
6448                     (copy-sequence gnus-summary-prepare-exit-hook)))
6449       (when (gnus-buffer-live-p gnus-article-buffer)
6450         (save-excursion
6451           (set-buffer gnus-article-buffer)
6452           (mm-destroy-parts gnus-article-mime-handles)
6453           ;; Set it to nil for safety reason.
6454           (setq gnus-article-mime-handle-alist nil)
6455           (setq gnus-article-mime-handles nil)))
6456       ;; If we have several article buffers, we kill them at exit.
6457       (unless gnus-single-article-buffer
6458         (gnus-kill-buffer gnus-article-buffer)
6459         (gnus-kill-buffer gnus-original-article-buffer)
6460         (setq gnus-article-current nil))
6461       (if (not gnus-kill-summary-on-exit)
6462           (gnus-deaden-summary)
6463         (gnus-close-group group)
6464         (gnus-summary-clear-local-variables)
6465         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6466           (gnus-summary-clear-local-variables))
6467         (set-buffer gnus-group-buffer)
6468         (gnus-summary-clear-local-variables)
6469         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6470           (gnus-summary-clear-local-variables))
6471         (when (get-buffer gnus-summary-buffer)
6472           (kill-buffer gnus-summary-buffer)))
6473       (unless gnus-single-article-buffer
6474         (setq gnus-article-current nil))
6475       (when gnus-use-trees
6476         (gnus-tree-close group))
6477       (gnus-async-prefetch-remove-group group)
6478       (when (get-buffer gnus-article-buffer)
6479         (bury-buffer gnus-article-buffer))
6480       ;; Return to the group buffer.
6481       (gnus-configure-windows 'group 'force)
6482       ;; Clear the current group name.
6483       (setq gnus-newsgroup-name nil)
6484       (unless (gnus-ephemeral-group-p group)
6485         (gnus-group-update-group group))
6486       (when (equal (gnus-group-group-name) group)
6487         (gnus-group-next-unread-group 1))
6488       (when quit-config
6489         (gnus-handle-ephemeral-exit quit-config)))))
6490
6491 (defun gnus-handle-ephemeral-exit (quit-config)
6492   "Handle movement when leaving an ephemeral group.
6493 The state which existed when entering the ephemeral is reset."
6494   (if (not (buffer-name (car quit-config)))
6495       (gnus-configure-windows 'group 'force)
6496     (set-buffer (car quit-config))
6497     (cond ((eq major-mode 'gnus-summary-mode)
6498            (gnus-set-global-variables))
6499           ((eq major-mode 'gnus-article-mode)
6500            (save-excursion
6501              ;; The `gnus-summary-buffer' variable may point
6502              ;; to the old summary buffer when using a single
6503              ;; article buffer.
6504              (unless (gnus-buffer-live-p gnus-summary-buffer)
6505                (set-buffer gnus-group-buffer))
6506              (set-buffer gnus-summary-buffer)
6507              (gnus-set-global-variables))))
6508     (if (or (eq (cdr quit-config) 'article)
6509             (eq (cdr quit-config) 'pick))
6510         (progn
6511           ;; The current article may be from the ephemeral group
6512           ;; thus it is best that we reload this article
6513           (gnus-summary-show-article)
6514           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6515               (gnus-configure-windows 'pick 'force)
6516             (gnus-configure-windows (cdr quit-config) 'force)))
6517       (gnus-configure-windows (cdr quit-config) 'force))
6518     (when (eq major-mode 'gnus-summary-mode)
6519       (gnus-summary-next-subject 1 nil t)
6520       (gnus-summary-recenter)
6521       (gnus-summary-position-point))))
6522
6523 ;;; Dead summaries.
6524
6525 (defvar gnus-dead-summary-mode-map nil)
6526
6527 (unless gnus-dead-summary-mode-map
6528   (setq gnus-dead-summary-mode-map (make-keymap))
6529   (suppress-keymap gnus-dead-summary-mode-map)
6530   (substitute-key-definition
6531    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6532   (dolist (key '("\C-d" "\r" "\177" [delete]))
6533     (define-key gnus-dead-summary-mode-map
6534       key 'gnus-summary-wake-up-the-dead))
6535   (dolist (key '("q" "Q"))
6536     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6537
6538 (defvar gnus-dead-summary-mode nil
6539   "Minor mode for Gnus summary buffers.")
6540
6541 (defun gnus-dead-summary-mode (&optional arg)
6542   "Minor mode for Gnus summary buffers."
6543   (interactive "P")
6544   (when (eq major-mode 'gnus-summary-mode)
6545     (make-local-variable 'gnus-dead-summary-mode)
6546     (setq gnus-dead-summary-mode
6547           (if (null arg) (not gnus-dead-summary-mode)
6548             (> (prefix-numeric-value arg) 0)))
6549     (when gnus-dead-summary-mode
6550       (gnus-add-minor-mode
6551        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6552
6553 (defun gnus-deaden-summary ()
6554   "Make the current summary buffer into a dead summary buffer."
6555   ;; Kill any previous dead summary buffer.
6556   (when (and gnus-dead-summary
6557              (buffer-name gnus-dead-summary))
6558     (save-excursion
6559       (set-buffer gnus-dead-summary)
6560       (when gnus-dead-summary-mode
6561         (kill-buffer (current-buffer)))))
6562   ;; Make this the current dead summary.
6563   (setq gnus-dead-summary (current-buffer))
6564   (gnus-dead-summary-mode 1)
6565   (let ((name (buffer-name)))
6566     (when (string-match "Summary" name)
6567       (rename-buffer
6568        (concat (substring name 0 (match-beginning 0)) "Dead "
6569                (substring name (match-beginning 0)))
6570        t)
6571       (bury-buffer))))
6572
6573 (defun gnus-kill-or-deaden-summary (buffer)
6574   "Kill or deaden the summary BUFFER."
6575   (save-excursion
6576     (when (and (buffer-name buffer)
6577                (not gnus-single-article-buffer))
6578       (save-excursion
6579         (set-buffer buffer)
6580         (gnus-kill-buffer gnus-article-buffer)
6581         (gnus-kill-buffer gnus-original-article-buffer)))
6582     (cond
6583      ;; Kill the buffer.
6584      (gnus-kill-summary-on-exit
6585       (when (and gnus-use-trees
6586                  (gnus-buffer-exists-p buffer))
6587         (save-excursion
6588           (set-buffer buffer)
6589           (gnus-tree-close gnus-newsgroup-name)))
6590       (gnus-kill-buffer buffer))
6591      ;; Deaden the buffer.
6592      ((gnus-buffer-exists-p buffer)
6593       (save-excursion
6594         (set-buffer buffer)
6595         (gnus-deaden-summary))))))
6596
6597 (defun gnus-summary-wake-up-the-dead (&rest args)
6598   "Wake up the dead summary buffer."
6599   (interactive)
6600   (gnus-dead-summary-mode -1)
6601   (let ((name (buffer-name)))
6602     (when (string-match "Dead " name)
6603       (rename-buffer
6604        (concat (substring name 0 (match-beginning 0))
6605                (substring name (match-end 0)))
6606        t)))
6607   (gnus-message 3 "This dead summary is now alive again"))
6608
6609 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6610 (defun gnus-summary-fetch-faq (&optional faq-dir)
6611   "Fetch the FAQ for the current group.
6612 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6613 in."
6614   (interactive
6615    (list
6616     (when current-prefix-arg
6617       (completing-read
6618        "FAQ dir: " (and (listp gnus-group-faq-directory)
6619                         (mapcar (lambda (file) (list file))
6620                                 gnus-group-faq-directory))))))
6621   (let (gnus-faq-buffer)
6622     (when (setq gnus-faq-buffer
6623                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6624       (gnus-configure-windows 'summary-faq))))
6625
6626 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6627 (defun gnus-summary-describe-group (&optional force)
6628   "Describe the current newsgroup."
6629   (interactive "P")
6630   (gnus-group-describe-group force gnus-newsgroup-name))
6631
6632 (defun gnus-summary-describe-briefly ()
6633   "Describe summary mode commands briefly."
6634   (interactive)
6635   (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")))
6636
6637 ;; Walking around group mode buffer from summary mode.
6638
6639 (defun gnus-summary-next-group (&optional no-article target-group backward)
6640   "Exit current newsgroup and then select next unread newsgroup.
6641 If prefix argument NO-ARTICLE is non-nil, no article is selected
6642 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6643 previous group instead."
6644   (interactive "P")
6645   ;; Stop pre-fetching.
6646   (gnus-async-halt-prefetch)
6647   (let ((current-group gnus-newsgroup-name)
6648         (current-buffer (current-buffer))
6649         entered)
6650     (while (not entered)
6651       ;; Then we find what group we are supposed to enter.
6652       (set-buffer gnus-group-buffer)
6653       (gnus-group-jump-to-group current-group)
6654       (setq target-group
6655             (or target-group
6656                 (if (eq gnus-keep-same-level 'best)
6657                     (gnus-summary-best-group gnus-newsgroup-name)
6658                   (gnus-summary-search-group backward gnus-keep-same-level))))
6659       (if (not target-group)
6660           ;; There are no further groups, so we return to the group
6661           ;; buffer.
6662           (progn
6663             (gnus-message 5 "Returning to the group buffer")
6664             (setq entered t)
6665             (when (gnus-buffer-live-p current-buffer)
6666               (set-buffer current-buffer)
6667               (gnus-summary-exit))
6668             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6669         ;; We try to enter the target group.
6670         (gnus-group-jump-to-group target-group)
6671         (let ((unreads (gnus-group-group-unread)))
6672           (if (and (or (eq t unreads)
6673                        (and unreads (not (zerop unreads))))
6674                    (progn
6675                      ;; Now we semi-exit this group to update Xrefs
6676                      ;; and all variables.  We can't do a real exit,
6677                      ;; because the window conf must remain the same
6678                      ;; in case the user is prompted for info, and we
6679                      ;; don't want the window conf to change before
6680                      ;; that...
6681                      (when (gnus-buffer-live-p current-buffer)
6682                        (set-buffer current-buffer)
6683                        (gnus-summary-exit t))
6684                      (gnus-summary-read-group
6685                       target-group nil no-article
6686                       (and (buffer-name current-buffer) current-buffer)
6687                       nil backward)))
6688               (setq entered t)
6689             (setq current-group target-group
6690                   target-group nil)))))))
6691
6692 (defun gnus-summary-prev-group (&optional no-article)
6693   "Exit current newsgroup and then select previous unread newsgroup.
6694 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6695   (interactive "P")
6696   (gnus-summary-next-group no-article nil t))
6697
6698 ;; Walking around summary lines.
6699
6700 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6701   "Go to the first unread subject.
6702 If UNREAD is non-nil, go to the first unread article.
6703 Returns the article selected or nil if there are no unread articles."
6704   (interactive "P")
6705   (prog1
6706       (cond
6707        ;; Empty summary.
6708        ((null gnus-newsgroup-data)
6709         (gnus-message 3 "No articles in the group")
6710         nil)
6711        ;; Pick the first article.
6712        ((not unread)
6713         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6714         (gnus-data-number (car gnus-newsgroup-data)))
6715        ;; No unread articles.
6716        ((null gnus-newsgroup-unreads)
6717         (gnus-message 3 "No more unread articles")
6718         nil)
6719        ;; Find the first unread article.
6720        (t
6721         (let ((data gnus-newsgroup-data))
6722           (while (and data
6723                       (and (not (and undownloaded
6724                                      (memq (car data)
6725                                            gnus-newsgroup-undownloaded)))
6726                            (if unseen
6727                                (or (not (memq
6728                                          (gnus-data-number (car data))
6729                                          gnus-newsgroup-unseen))
6730                                    (not (gnus-data-unread-p (car data))))
6731                              (not (gnus-data-unread-p (car data))))))
6732             (setq data (cdr data)))
6733           (when data
6734             (goto-char (gnus-data-pos (car data)))
6735             (gnus-data-number (car data))))))
6736     (gnus-summary-position-point)))
6737
6738 (defun gnus-summary-next-subject (n &optional unread dont-display)
6739   "Go to next N'th summary line.
6740 If N is negative, go to the previous N'th subject line.
6741 If UNREAD is non-nil, only unread articles are selected.
6742 The difference between N and the actual number of steps taken is
6743 returned."
6744   (interactive "p")
6745   (let ((backward (< n 0))
6746         (n (abs n)))
6747     (while (and (> n 0)
6748                 (if backward
6749                     (gnus-summary-find-prev unread)
6750                   (gnus-summary-find-next unread)))
6751       (unless (zerop (setq n (1- n)))
6752         (gnus-summary-show-thread)))
6753     (when (/= 0 n)
6754       (gnus-message 7 "No more%s articles"
6755                     (if unread " unread" "")))
6756     (unless dont-display
6757       (gnus-summary-recenter)
6758       (gnus-summary-position-point))
6759     n))
6760
6761 (defun gnus-summary-next-unread-subject (n)
6762   "Go to next N'th unread summary line."
6763   (interactive "p")
6764   (gnus-summary-next-subject n t))
6765
6766 (defun gnus-summary-prev-subject (n &optional unread)
6767   "Go to previous N'th summary line.
6768 If optional argument UNREAD is non-nil, only unread article is selected."
6769   (interactive "p")
6770   (gnus-summary-next-subject (- n) unread))
6771
6772 (defun gnus-summary-prev-unread-subject (n)
6773   "Go to previous N'th unread summary line."
6774   (interactive "p")
6775   (gnus-summary-next-subject (- n) t))
6776
6777 (defun gnus-summary-goto-subjects (articles)
6778   "Insert the subject header for ARTICLES in the current buffer."
6779   (save-excursion
6780     (dolist (article articles)
6781       (gnus-summary-goto-subject article t)))
6782   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6783   (gnus-summary-position-point))
6784  
6785 (defun gnus-summary-goto-subject (article &optional force silent)
6786   "Go the subject line of ARTICLE.
6787 If FORCE, also allow jumping to articles not currently shown."
6788   (interactive "nArticle number: ")
6789   (unless (numberp article)
6790     (error "Article %s is not a number" article))
6791   (let ((b (point))
6792         (data (gnus-data-find article)))
6793     ;; We read in the article if we have to.
6794     (and (not data)
6795          force
6796          (gnus-summary-insert-subject
6797           article
6798           (if (or (numberp force) (vectorp force)) force)
6799           t)
6800          (setq data (gnus-data-find article)))
6801     (goto-char b)
6802     (if (not data)
6803         (progn
6804           (unless silent
6805             (gnus-message 3 "Can't find article %d" article))
6806           nil)
6807       (let ((pt (gnus-data-pos data)))
6808         (goto-char pt)
6809         (gnus-summary-set-article-display-arrow pt))
6810       (gnus-summary-position-point)
6811       article)))
6812
6813 ;; Walking around summary lines with displaying articles.
6814
6815 (defun gnus-summary-expand-window (&optional arg)
6816   "Make the summary buffer take up the entire Emacs frame.
6817 Given a prefix, will force an `article' buffer configuration."
6818   (interactive "P")
6819   (if arg
6820       (gnus-configure-windows 'article 'force)
6821     (gnus-configure-windows 'summary 'force)))
6822
6823 (defun gnus-summary-display-article (article &optional all-header)
6824   "Display ARTICLE in article buffer."
6825   (when (gnus-buffer-live-p gnus-article-buffer)
6826     (with-current-buffer gnus-article-buffer
6827       (mm-enable-multibyte)))
6828   (gnus-set-global-variables)
6829   (when (gnus-buffer-live-p gnus-article-buffer)
6830     (with-current-buffer gnus-article-buffer
6831       (setq gnus-article-charset gnus-newsgroup-charset)
6832       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6833       (mm-enable-multibyte)))
6834   (if (null article)
6835       nil
6836     (prog1
6837         (if gnus-summary-display-article-function
6838             (funcall gnus-summary-display-article-function article all-header)
6839           (gnus-article-prepare article all-header))
6840       (gnus-run-hooks 'gnus-select-article-hook)
6841       (when (and gnus-current-article
6842                  (not (zerop gnus-current-article)))
6843         (gnus-summary-goto-subject gnus-current-article))
6844       (gnus-summary-recenter)
6845       (when (and gnus-use-trees gnus-show-threads)
6846         (gnus-possibly-generate-tree article)
6847         (gnus-highlight-selected-tree article))
6848       ;; Successfully display article.
6849       (gnus-article-set-window-start
6850        (cdr (assq article gnus-newsgroup-bookmarks))))))
6851
6852 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6853   "Select the current article.
6854 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6855 non-nil, the article will be re-fetched even if it already present in
6856 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6857 be displayed."
6858   ;; Make sure we are in the summary buffer to work around bbdb bug.
6859   (unless (eq major-mode 'gnus-summary-mode)
6860     (set-buffer gnus-summary-buffer))
6861   (let ((article (or article (gnus-summary-article-number)))
6862         (all-headers (not (not all-headers))) ;Must be t or nil.
6863         gnus-summary-display-article-function)
6864     (and (not pseudo)
6865          (gnus-summary-article-pseudo-p article)
6866          (error "This is a pseudo-article"))
6867     (save-excursion
6868       (set-buffer gnus-summary-buffer)
6869       (if (or (and gnus-single-article-buffer
6870                    (or (null gnus-current-article)
6871                        (null gnus-article-current)
6872                        (null (get-buffer gnus-article-buffer))
6873                        (not (eq article (cdr gnus-article-current)))
6874                        (not (equal (car gnus-article-current)
6875                                    gnus-newsgroup-name))))
6876               (and (not gnus-single-article-buffer)
6877                    (or (null gnus-current-article)
6878                        (not (eq gnus-current-article article))))
6879               force)
6880           ;; The requested article is different from the current article.
6881           (progn
6882             (gnus-summary-display-article article all-headers)
6883             (when (gnus-buffer-live-p gnus-article-buffer)
6884               (with-current-buffer gnus-article-buffer
6885                 (if (not gnus-article-decoded-p) ;; a local variable
6886                     (mm-disable-multibyte))))
6887             (gnus-article-set-window-start
6888              (cdr (assq article gnus-newsgroup-bookmarks)))
6889             article)
6890         'old))))
6891
6892 (defun gnus-summary-force-verify-and-decrypt ()
6893   (interactive)
6894   (let ((mm-verify-option 'known)
6895         (mm-decrypt-option 'known)
6896         (gnus-buttonized-mime-types (append (list "multipart/signed"
6897                                                   "multipart/encrypted")
6898                                             gnus-buttonized-mime-types)))
6899     (gnus-summary-select-article nil 'force)))
6900
6901 (defun gnus-summary-set-current-mark (&optional current-mark)
6902   "Obsolete function."
6903   nil)
6904
6905 (defun gnus-summary-next-article (&optional unread subject backward push)
6906   "Select the next article.
6907 If UNREAD, only unread articles are selected.
6908 If SUBJECT, only articles with SUBJECT are selected.
6909 If BACKWARD, the previous article is selected instead of the next."
6910   (interactive "P")
6911   (cond
6912    ;; Is there such an article?
6913    ((and (gnus-summary-search-forward unread subject backward)
6914          (or (gnus-summary-display-article (gnus-summary-article-number))
6915              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6916     (gnus-summary-position-point))
6917    ;; If not, we try the first unread, if that is wanted.
6918    ((and subject
6919          gnus-auto-select-same
6920          (gnus-summary-first-unread-article))
6921     (gnus-summary-position-point)
6922     (gnus-message 6 "Wrapped"))
6923    ;; Try to get next/previous article not displayed in this group.
6924    ((and gnus-auto-extend-newsgroup
6925          (not unread) (not subject))
6926     (gnus-summary-goto-article
6927      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6928      nil (count-lines (point-min) (point))))
6929    ;; Go to next/previous group.
6930    (t
6931     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6932       (gnus-summary-jump-to-group gnus-newsgroup-name))
6933     (let ((cmd last-command-char)
6934           (point
6935            (save-excursion
6936              (set-buffer gnus-group-buffer)
6937              (point)))
6938           (group
6939            (if (eq gnus-keep-same-level 'best)
6940                (gnus-summary-best-group gnus-newsgroup-name)
6941              (gnus-summary-search-group backward gnus-keep-same-level))))
6942       ;; For some reason, the group window gets selected.  We change
6943       ;; it back.
6944       (select-window (get-buffer-window (current-buffer)))
6945       ;; Select next unread newsgroup automagically.
6946       (cond
6947        ((or (not gnus-auto-select-next)
6948             (not cmd))
6949         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6950        ((or (eq gnus-auto-select-next 'quietly)
6951             (and (eq gnus-auto-select-next 'slightly-quietly)
6952                  push)
6953             (and (eq gnus-auto-select-next 'almost-quietly)
6954                  (gnus-summary-last-article-p)))
6955         ;; Select quietly.
6956         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6957             (gnus-summary-exit)
6958           (gnus-message 7 "No more%s articles (%s)..."
6959                         (if unread " unread" "")
6960                         (if group (concat "selecting " group)
6961                           "exiting"))
6962           (gnus-summary-next-group nil group backward)))
6963        (t
6964         (when (gnus-key-press-event-p last-input-event)
6965           (gnus-summary-walk-group-buffer
6966            gnus-newsgroup-name cmd unread backward point))))))))
6967
6968 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6969   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6970                       (?\C-p (gnus-group-prev-unread-group 1))))
6971         (cursor-in-echo-area t)
6972         keve key group ended prompt)
6973     (save-excursion
6974       (set-buffer gnus-group-buffer)
6975       (goto-char start)
6976       (setq group
6977             (if (eq gnus-keep-same-level 'best)
6978                 (gnus-summary-best-group gnus-newsgroup-name)
6979               (gnus-summary-search-group backward gnus-keep-same-level))))
6980     (while (not ended)
6981       (setq prompt
6982             (format
6983              "No more%s articles%s " (if unread " unread" "")
6984              (if (and group
6985                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6986                  (format " (Type %s for %s [%s])"
6987                          (single-key-description cmd) group
6988                          (car (gnus-gethash group gnus-newsrc-hashtb)))
6989                (format " (Type %s to exit %s)"
6990                        (single-key-description cmd)
6991                        gnus-newsgroup-name))))
6992       ;; Confirm auto selection.
6993       (setq key (car (setq keve (gnus-read-event-char prompt)))
6994             ended t)
6995       (cond
6996        ((assq key keystrokes)
6997         (let ((obuf (current-buffer)))
6998           (switch-to-buffer gnus-group-buffer)
6999           (when group
7000             (gnus-group-jump-to-group group))
7001           (eval (cadr (assq key keystrokes)))
7002           (setq group (gnus-group-group-name))
7003           (switch-to-buffer obuf))
7004         (setq ended nil))
7005        ((equal key cmd)
7006         (if (or (not group)
7007                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7008             (gnus-summary-exit)
7009           (gnus-summary-next-group nil group backward)))
7010        (t
7011         (push (cdr keve) unread-command-events))))))
7012
7013 (defun gnus-summary-next-unread-article ()
7014   "Select unread article after current one."
7015   (interactive)
7016   (gnus-summary-next-article
7017    (or (not (eq gnus-summary-goto-unread 'never))
7018        (gnus-summary-last-article-p (gnus-summary-article-number)))
7019    (and gnus-auto-select-same
7020         (gnus-summary-article-subject))))
7021
7022 (defun gnus-summary-prev-article (&optional unread subject)
7023   "Select the article after the current one.
7024 If UNREAD is non-nil, only unread articles are selected."
7025   (interactive "P")
7026   (gnus-summary-next-article unread subject t))
7027
7028 (defun gnus-summary-prev-unread-article ()
7029   "Select unread article before current one."
7030   (interactive)
7031   (gnus-summary-prev-article
7032    (or (not (eq gnus-summary-goto-unread 'never))
7033        (gnus-summary-first-article-p (gnus-summary-article-number)))
7034    (and gnus-auto-select-same
7035         (gnus-summary-article-subject))))
7036
7037 (defun gnus-summary-next-page (&optional lines circular stop)
7038   "Show next page of the selected article.
7039 If at the end of the current article, select the next article.
7040 LINES says how many lines should be scrolled up.
7041
7042 If CIRCULAR is non-nil, go to the start of the article instead of
7043 selecting the next article when reaching the end of the current
7044 article.
7045
7046 If STOP is non-nil, just stop when reaching the end of the message."
7047   (interactive "P")
7048   (setq gnus-summary-buffer (current-buffer))
7049   (gnus-set-global-variables)
7050   (let ((article (gnus-summary-article-number))
7051         (article-window (get-buffer-window gnus-article-buffer t))
7052         endp)
7053     ;; If the buffer is empty, we have no article.
7054     (unless article
7055       (error "No article to select"))
7056     (gnus-configure-windows 'article)
7057     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7058         (if (and (eq gnus-summary-goto-unread 'never)
7059                  (not (gnus-summary-last-article-p article)))
7060             (gnus-summary-next-article)
7061           (gnus-summary-next-unread-article))
7062       (if (or (null gnus-current-article)
7063               (null gnus-article-current)
7064               (/= article (cdr gnus-article-current))
7065               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7066           ;; Selected subject is different from current article's.
7067           (gnus-summary-display-article article)
7068         (when article-window
7069           (gnus-eval-in-buffer-window gnus-article-buffer
7070             (setq endp (gnus-article-next-page lines)))
7071           (when endp
7072             (cond (stop
7073                    (gnus-message 3 "End of message"))
7074                   (circular
7075                    (gnus-summary-beginning-of-article))
7076                   (lines
7077                    (gnus-message 3 "End of message"))
7078                   ((null lines)
7079                    (if (and (eq gnus-summary-goto-unread 'never)
7080                             (not (gnus-summary-last-article-p article)))
7081                        (gnus-summary-next-article)
7082                      (gnus-summary-next-unread-article))))))))
7083     (gnus-summary-recenter)
7084     (gnus-summary-position-point)))
7085
7086 (defun gnus-summary-prev-page (&optional lines move)
7087   "Show previous page of selected article.
7088 Argument LINES specifies lines to be scrolled down.
7089 If MOVE, move to the previous unread article if point is at
7090 the beginning of the buffer."
7091   (interactive "P")
7092   (let ((article (gnus-summary-article-number))
7093         (article-window (get-buffer-window gnus-article-buffer t))
7094         endp)
7095     (gnus-configure-windows 'article)
7096     (if (or (null gnus-current-article)
7097             (null gnus-article-current)
7098             (/= article (cdr gnus-article-current))
7099             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7100         ;; Selected subject is different from current article's.
7101         (gnus-summary-display-article article)
7102       (gnus-summary-recenter)
7103       (when article-window
7104         (gnus-eval-in-buffer-window gnus-article-buffer
7105           (setq endp (gnus-article-prev-page lines)))
7106         (when (and move endp)
7107           (cond (lines
7108                  (gnus-message 3 "Beginning of message"))
7109                 ((null lines)
7110                  (if (and (eq gnus-summary-goto-unread 'never)
7111                           (not (gnus-summary-first-article-p article)))
7112                      (gnus-summary-prev-article)
7113                    (gnus-summary-prev-unread-article))))))))
7114   (gnus-summary-position-point))
7115
7116 (defun gnus-summary-prev-page-or-article (&optional lines)
7117   "Show previous page of selected article.
7118 Argument LINES specifies lines to be scrolled down.
7119 If at the beginning of the article, go to the next article."
7120   (interactive "P")
7121   (gnus-summary-prev-page lines t))
7122
7123 (defun gnus-summary-scroll-up (lines)
7124   "Scroll up (or down) one line current article.
7125 Argument LINES specifies lines to be scrolled up (or down if negative)."
7126   (interactive "p")
7127   (gnus-configure-windows 'article)
7128   (gnus-summary-show-thread)
7129   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7130     (gnus-eval-in-buffer-window gnus-article-buffer
7131       (cond ((> lines 0)
7132              (when (gnus-article-next-page lines)
7133                (gnus-message 3 "End of message")))
7134             ((< lines 0)
7135              (gnus-article-prev-page (- lines))))))
7136   (gnus-summary-recenter)
7137   (gnus-summary-position-point))
7138
7139 (defun gnus-summary-scroll-down (lines)
7140   "Scroll down (or up) one line current article.
7141 Argument LINES specifies lines to be scrolled down (or up if negative)."
7142   (interactive "p")
7143   (gnus-summary-scroll-up (- lines)))
7144
7145 (defun gnus-summary-next-same-subject ()
7146   "Select next article which has the same subject as current one."
7147   (interactive)
7148   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7149
7150 (defun gnus-summary-prev-same-subject ()
7151   "Select previous article which has the same subject as current one."
7152   (interactive)
7153   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7154
7155 (defun gnus-summary-next-unread-same-subject ()
7156   "Select next unread article which has the same subject as current one."
7157   (interactive)
7158   (gnus-summary-next-article t (gnus-summary-article-subject)))
7159
7160 (defun gnus-summary-prev-unread-same-subject ()
7161   "Select previous unread article which has the same subject as current one."
7162   (interactive)
7163   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7164
7165 (defun gnus-summary-first-unread-article ()
7166   "Select the first unread article.
7167 Return nil if there are no unread articles."
7168   (interactive)
7169   (prog1
7170       (when (gnus-summary-first-subject t)
7171         (gnus-summary-show-thread)
7172         (gnus-summary-first-subject t)
7173         (gnus-summary-display-article (gnus-summary-article-number)))
7174     (gnus-summary-position-point)))
7175
7176 (defun gnus-summary-first-unread-subject ()
7177   "Place the point on the subject line of the first unread article.
7178 Return nil if there are no unread articles."
7179   (interactive)
7180   (prog1
7181       (when (gnus-summary-first-subject t)
7182         (gnus-summary-show-thread)
7183         (gnus-summary-first-subject t))
7184     (gnus-summary-position-point)))
7185
7186 (defun gnus-summary-first-unseen-subject ()
7187   "Place the point on the subject line of the first unseen article.
7188 Return nil if there are no unseen articles."
7189   (interactive)
7190   (prog1
7191       (when (gnus-summary-first-subject t t t)
7192         (gnus-summary-show-thread)
7193         (gnus-summary-first-subject t t t))
7194     (gnus-summary-position-point)))
7195
7196 (defun gnus-summary-first-unseen-or-unread-subject ()
7197   "Place the point on the subject line of the first unseen article.
7198 Return nil if there are no unseen articles."
7199   (interactive)
7200   (prog1
7201       (unless (when (gnus-summary-first-subject t t t)
7202                 (gnus-summary-show-thread)
7203                 (gnus-summary-first-subject t t t))
7204         (when (gnus-summary-first-subject t)
7205           (gnus-summary-show-thread)
7206           (gnus-summary-first-subject t)))
7207     (gnus-summary-position-point)))
7208
7209 (defun gnus-summary-first-article ()
7210   "Select the first article.
7211 Return nil if there are no articles."
7212   (interactive)
7213   (prog1
7214       (when (gnus-summary-first-subject)
7215         (gnus-summary-show-thread)
7216         (gnus-summary-first-subject)
7217         (gnus-summary-display-article (gnus-summary-article-number)))
7218     (gnus-summary-position-point)))
7219
7220 (defun gnus-summary-best-unread-article (&optional arg)
7221   "Select the unread article with the highest score.
7222 If given a prefix argument, select the next unread article that has a
7223 score higher than the default score."
7224   (interactive "P")
7225   (let ((article (if arg
7226                      (gnus-summary-better-unread-subject)
7227                    (gnus-summary-best-unread-subject))))
7228     (if article
7229         (gnus-summary-goto-article article)
7230       (error "No unread articles"))))
7231
7232 (defun gnus-summary-best-unread-subject ()
7233   "Select the unread subject with the highest score."
7234   (interactive)
7235   (let ((best -1000000)
7236         (data gnus-newsgroup-data)
7237         article score)
7238     (while data
7239       (and (gnus-data-unread-p (car data))
7240            (> (setq score
7241                     (gnus-summary-article-score (gnus-data-number (car data))))
7242               best)
7243            (setq best score
7244                  article (gnus-data-number (car data))))
7245       (setq data (cdr data)))
7246     (when article
7247       (gnus-summary-goto-subject article))
7248     (gnus-summary-position-point)
7249     article))
7250
7251 (defun gnus-summary-better-unread-subject ()
7252   "Select the first unread subject that has a score over the default score."
7253   (interactive)
7254   (let ((data gnus-newsgroup-data)
7255         article score)
7256     (while (and (setq article (gnus-data-number (car data)))
7257                 (or (gnus-data-read-p (car data))
7258                     (not (> (gnus-summary-article-score article)
7259                             gnus-summary-default-score))))
7260       (setq data (cdr data)))
7261     (when article
7262       (gnus-summary-goto-subject article))
7263     (gnus-summary-position-point)
7264     article))
7265
7266 (defun gnus-summary-last-subject ()
7267   "Go to the last displayed subject line in the group."
7268   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7269     (when article
7270       (gnus-summary-goto-subject article))))
7271
7272 (defun gnus-summary-goto-article (article &optional all-headers force)
7273   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7274 If ALL-HEADERS is non-nil, no header lines are hidden.
7275 If FORCE, go to the article even if it isn't displayed.  If FORCE
7276 is a number, it is the line the article is to be displayed on."
7277   (interactive
7278    (list
7279     (completing-read
7280      "Article number or Message-ID: "
7281      (mapcar (lambda (number) (list (int-to-string number)))
7282              gnus-newsgroup-limit))
7283     current-prefix-arg
7284     t))
7285   (prog1
7286       (if (and (stringp article)
7287                (string-match "@" article))
7288           (gnus-summary-refer-article article)
7289         (when (stringp article)
7290           (setq article (string-to-number article)))
7291         (if (gnus-summary-goto-subject article force)
7292             (gnus-summary-display-article article all-headers)
7293           (gnus-message 4 "Couldn't go to article %s" article) nil))
7294     (gnus-summary-position-point)))
7295
7296 (defun gnus-summary-goto-last-article ()
7297   "Go to the previously read article."
7298   (interactive)
7299   (prog1
7300       (when gnus-last-article
7301         (gnus-summary-goto-article gnus-last-article nil t))
7302     (gnus-summary-position-point)))
7303
7304 (defun gnus-summary-pop-article (number)
7305   "Pop one article off the history and go to the previous.
7306 NUMBER articles will be popped off."
7307   (interactive "p")
7308   (let (to)
7309     (setq gnus-newsgroup-history
7310           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7311     (if to
7312         (gnus-summary-goto-article (car to) nil t)
7313       (error "Article history empty")))
7314   (gnus-summary-position-point))
7315
7316 ;; Summary commands and functions for limiting the summary buffer.
7317
7318 (defun gnus-summary-limit-to-articles (n)
7319   "Limit the summary buffer to the next N articles.
7320 If not given a prefix, use the process marked articles instead."
7321   (interactive "P")
7322   (prog1
7323       (let ((articles (gnus-summary-work-articles n)))
7324         (setq gnus-newsgroup-processable nil)
7325         (gnus-summary-limit articles))
7326     (gnus-summary-position-point)))
7327
7328 (defun gnus-summary-pop-limit (&optional total)
7329   "Restore the previous limit.
7330 If given a prefix, remove all limits."
7331   (interactive "P")
7332   (when total
7333     (setq gnus-newsgroup-limits
7334           (list (mapcar (lambda (h) (mail-header-number h))
7335                         gnus-newsgroup-headers))))
7336   (unless gnus-newsgroup-limits
7337     (error "No limit to pop"))
7338   (prog1
7339       (gnus-summary-limit nil 'pop)
7340     (gnus-summary-position-point)))
7341
7342 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7343   "Limit the summary buffer to articles that have subjects that match a regexp.
7344 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7345   (interactive
7346    (list (read-string (if current-prefix-arg
7347                           "Exclude subject (regexp): "
7348                         "Limit to subject (regexp): "))
7349          nil current-prefix-arg))
7350   (unless header
7351     (setq header "subject"))
7352   (when (not (equal "" subject))
7353     (prog1
7354         (let ((articles (gnus-summary-find-matching
7355                          (or header "subject") subject 'all nil nil
7356                          not-matching)))
7357           (unless articles
7358             (error "Found no matches for \"%s\"" subject))
7359           (gnus-summary-limit articles))
7360       (gnus-summary-position-point))))
7361
7362 (defun gnus-summary-limit-to-author (from &optional not-matching)
7363   "Limit the summary buffer to articles that have authors that match a regexp.
7364 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7365   (interactive
7366    (list (read-string (if current-prefix-arg
7367                           "Exclude author (regexp): "
7368                         "Limit to author (regexp): "))
7369          current-prefix-arg))
7370   (gnus-summary-limit-to-subject from "from" not-matching))
7371
7372 (defun gnus-summary-limit-to-age (age &optional younger-p)
7373   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7374 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7375 articles that are younger than AGE days."
7376   (interactive
7377    (let ((younger current-prefix-arg)
7378          (days-got nil)
7379          days)
7380      (while (not days-got)
7381        (setq days (if younger
7382                       (read-string "Limit to articles younger than (in days, older when negative): ")
7383                     (read-string
7384                      "Limit to articles older than (in days, younger when negative): ")))
7385        (when (> (length days) 0)
7386          (setq days (read days)))
7387        (if (numberp days)
7388            (progn
7389              (setq days-got t)
7390              (if (< days 0)
7391                  (progn
7392                    (setq younger (not younger))
7393                    (setq days (* days -1)))))
7394          (message "Please enter a number.")
7395          (sleep-for 1)))
7396      (list days younger)))
7397   (prog1
7398       (let ((data gnus-newsgroup-data)
7399             (cutoff (days-to-time age))
7400             articles d date is-younger)
7401         (while (setq d (pop data))
7402           (when (and (vectorp (gnus-data-header d))
7403                      (setq date (mail-header-date (gnus-data-header d))))
7404             (setq is-younger (time-less-p
7405                               (time-since (condition-case ()
7406                                               (date-to-time date)
7407                                             (error '(0 0))))
7408                               cutoff))
7409             (when (if younger-p
7410                       is-younger
7411                     (not is-younger))
7412               (push (gnus-data-number d) articles))))
7413         (gnus-summary-limit (nreverse articles)))
7414     (gnus-summary-position-point)))
7415
7416 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7417   "Limit the summary buffer to articles that match an 'extra' header."
7418   (interactive
7419    (let ((header
7420           (intern
7421            (gnus-completing-read-with-default
7422             (symbol-name (car gnus-extra-headers))
7423             (if current-prefix-arg
7424                 "Exclude extra header:"
7425               "Limit extra header:")
7426             (mapcar (lambda (x)
7427                       (cons (symbol-name x) x))
7428                     gnus-extra-headers)
7429             nil
7430             t))))
7431      (list header
7432            (read-string (format "%s header %s (regexp): "
7433                                 (if current-prefix-arg "Exclude" "Limit to")
7434                                 header))
7435            current-prefix-arg)))
7436   (when (not (equal "" regexp))
7437     (prog1
7438         (let ((articles (gnus-summary-find-matching
7439                          (cons 'extra header) regexp 'all nil nil
7440                          not-matching)))
7441           (unless articles
7442             (error "Found no matches for \"%s\"" regexp))
7443           (gnus-summary-limit articles))
7444       (gnus-summary-position-point))))
7445
7446 (defun gnus-summary-limit-to-display-predicate ()
7447   "Limit the summary buffer to the predicated in the `display' group parameter."
7448   (interactive)
7449   (unless gnus-newsgroup-display
7450     (error "There is no `display' group parameter"))
7451   (let (articles)
7452     (dolist (number gnus-newsgroup-articles)
7453       (when (funcall gnus-newsgroup-display)
7454         (push number articles)))
7455     (gnus-summary-limit articles))
7456   (gnus-summary-position-point))
7457
7458 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7459 (make-obsolete
7460  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7461
7462 (defun gnus-summary-limit-to-unread (&optional all)
7463   "Limit the summary buffer to articles that are not marked as read.
7464 If ALL is non-nil, limit strictly to unread articles."
7465   (interactive "P")
7466   (if all
7467       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7468     (gnus-summary-limit-to-marks
7469      ;; Concat all the marks that say that an article is read and have
7470      ;; those removed.
7471      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7472            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7473            gnus-low-score-mark gnus-expirable-mark
7474            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7475            gnus-duplicate-mark gnus-souped-mark)
7476      'reverse)))
7477
7478 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7479 (make-obsolete 'gnus-summary-delete-marked-with
7480                'gnus-summary-limit-exclude-marks)
7481
7482 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7483   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7484 If REVERSE, limit the summary buffer to articles that are marked
7485 with MARKS.  MARKS can either be a string of marks or a list of marks.
7486 Returns how many articles were removed."
7487   (interactive "sMarks: ")
7488   (gnus-summary-limit-to-marks marks t))
7489
7490 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7491   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7492 If REVERSE (the prefix), limit the summary buffer to articles that are
7493 not marked with MARKS.  MARKS can either be a string of marks or a
7494 list of marks.
7495 Returns how many articles were removed."
7496   (interactive "sMarks: \nP")
7497   (prog1
7498       (let ((data gnus-newsgroup-data)
7499             (marks (if (listp marks) marks
7500                      (append marks nil))) ; Transform to list.
7501             articles)
7502         (while data
7503           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7504                   (memq (gnus-data-mark (car data)) marks))
7505             (push (gnus-data-number (car data)) articles))
7506           (setq data (cdr data)))
7507         (gnus-summary-limit articles))
7508     (gnus-summary-position-point)))
7509
7510 (defun gnus-summary-limit-to-score (score)
7511   "Limit to articles with score at or above SCORE."
7512   (interactive "NLimit to articles with score of at least: ")
7513   (let ((data gnus-newsgroup-data)
7514         articles)
7515     (while data
7516       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7517                 score)
7518         (push (gnus-data-number (car data)) articles))
7519       (setq data (cdr data)))
7520     (prog1
7521         (gnus-summary-limit articles)
7522       (gnus-summary-position-point))))
7523
7524 (defun gnus-summary-limit-to-unseen ()
7525   "Limit to unseen articles."
7526   (interactive)
7527   (prog1
7528       (gnus-summary-limit gnus-newsgroup-unseen)
7529     (gnus-summary-position-point)))
7530
7531 (defun gnus-summary-limit-include-thread (id)
7532   "Display all the hidden articles that is in the thread with ID in it.
7533 When called interactively, ID is the Message-ID of the current
7534 article."
7535   (interactive (list (mail-header-id (gnus-summary-article-header))))
7536   (let ((articles (gnus-articles-in-thread
7537                    (gnus-id-to-thread (gnus-root-id id)))))
7538     (prog1
7539         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7540       (gnus-summary-limit-include-matching-articles
7541        "subject"
7542        (regexp-quote (gnus-simplify-subject-re
7543                       (mail-header-subject (gnus-id-to-header id)))))
7544       (gnus-summary-position-point))))
7545
7546 (defun gnus-summary-limit-include-matching-articles (header regexp)
7547   "Display all the hidden articles that have HEADERs that match REGEXP."
7548   (interactive (list (read-string "Match on header: ")
7549                      (read-string "Regexp: ")))
7550   (let ((articles (gnus-find-matching-articles header regexp)))
7551     (prog1
7552         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7553       (gnus-summary-position-point))))
7554
7555 (defun gnus-summary-insert-dormant-articles ()
7556   "Insert all the dormat articles for this group into the current buffer."
7557   (interactive)
7558   (let ((gnus-verbose (max 6 gnus-verbose)))
7559     (if (not gnus-newsgroup-dormant)
7560         (gnus-message 3 "No cached articles for this group")
7561       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7562
7563 (defun gnus-summary-limit-include-dormant ()
7564   "Display all the hidden articles that are marked as dormant.
7565 Note that this command only works on a subset of the articles currently
7566 fetched for this group."
7567   (interactive)
7568   (unless gnus-newsgroup-dormant
7569     (error "There are no dormant articles in this group"))
7570   (prog1
7571       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7572     (gnus-summary-position-point)))
7573
7574 (defun gnus-summary-limit-exclude-dormant ()
7575   "Hide all dormant articles."
7576   (interactive)
7577   (prog1
7578       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7579     (gnus-summary-position-point)))
7580
7581 (defun gnus-summary-limit-exclude-childless-dormant ()
7582   "Hide all dormant articles that have no children."
7583   (interactive)
7584   (let ((data (gnus-data-list t))
7585         articles d children)
7586     ;; Find all articles that are either not dormant or have
7587     ;; children.
7588     (while (setq d (pop data))
7589       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7590                 (and (setq children
7591                            (gnus-article-children (gnus-data-number d)))
7592                      (let (found)
7593                        (while children
7594                          (when (memq (car children) articles)
7595                            (setq children nil
7596                                  found t))
7597                          (pop children))
7598                        found)))
7599         (push (gnus-data-number d) articles)))
7600     ;; Do the limiting.
7601     (prog1
7602         (gnus-summary-limit articles)
7603       (gnus-summary-position-point))))
7604
7605 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7606   "Mark all unread excluded articles as read.
7607 If ALL, mark even excluded ticked and dormants as read."
7608   (interactive "P")
7609   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7610   (let ((articles (gnus-sorted-ndifference
7611                    (sort
7612                     (mapcar (lambda (h) (mail-header-number h))
7613                             gnus-newsgroup-headers)
7614                     '<)
7615                    gnus-newsgroup-limit))
7616         article)
7617     (setq gnus-newsgroup-unreads
7618           (gnus-sorted-intersection gnus-newsgroup-unreads
7619                                     gnus-newsgroup-limit))
7620     (if all
7621         (setq gnus-newsgroup-dormant nil
7622               gnus-newsgroup-marked nil
7623               gnus-newsgroup-reads
7624               (nconc
7625                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7626                gnus-newsgroup-reads))
7627       (while (setq article (pop articles))
7628         (unless (or (memq article gnus-newsgroup-dormant)
7629                     (memq article gnus-newsgroup-marked))
7630           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7631
7632 (defun gnus-summary-limit (articles &optional pop)
7633   (if pop
7634       ;; We pop the previous limit off the stack and use that.
7635       (setq articles (car gnus-newsgroup-limits)
7636             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7637     ;; We use the new limit, so we push the old limit on the stack.
7638     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7639   ;; Set the limit.
7640   (setq gnus-newsgroup-limit articles)
7641   (let ((total (length gnus-newsgroup-data))
7642         (data (gnus-data-find-list (gnus-summary-article-number)))
7643         (gnus-summary-mark-below nil)   ; Inhibit this.
7644         found)
7645     ;; This will do all the work of generating the new summary buffer
7646     ;; according to the new limit.
7647     (gnus-summary-prepare)
7648     ;; Hide any threads, possibly.
7649     (gnus-summary-maybe-hide-threads)
7650     ;; Try to return to the article you were at, or one in the
7651     ;; neighborhood.
7652     (when data
7653       ;; We try to find some article after the current one.
7654       (while data
7655         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7656           (setq data nil
7657                 found t))
7658         (setq data (cdr data))))
7659     (unless found
7660       ;; If there is no data, that means that we were after the last
7661       ;; article.  The same goes when we can't find any articles
7662       ;; after the current one.
7663       (goto-char (point-max))
7664       (gnus-summary-find-prev))
7665     (gnus-set-mode-line 'summary)
7666     ;; We return how many articles were removed from the summary
7667     ;; buffer as a result of the new limit.
7668     (- total (length gnus-newsgroup-data))))
7669
7670 (defsubst gnus-invisible-cut-children (threads)
7671   (let ((num 0))
7672     (while threads
7673       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7674         (incf num))
7675       (pop threads))
7676     (< num 2)))
7677
7678 (defsubst gnus-cut-thread (thread)
7679   "Go forwards in the thread until we find an article that we want to display."
7680   (when (or (eq gnus-fetch-old-headers 'some)
7681             (eq gnus-fetch-old-headers 'invisible)
7682             (numberp gnus-fetch-old-headers)
7683             (eq gnus-build-sparse-threads 'some)
7684             (eq gnus-build-sparse-threads 'more))
7685     ;; Deal with old-fetched headers and sparse threads.
7686     (while (and
7687             thread
7688             (or
7689              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7690              (gnus-summary-article-ancient-p
7691               (mail-header-number (car thread))))
7692             (if (or (<= (length (cdr thread)) 1)
7693                     (eq gnus-fetch-old-headers 'invisible))
7694                 (setq gnus-newsgroup-limit
7695                       (delq (mail-header-number (car thread))
7696                             gnus-newsgroup-limit)
7697                       thread (cadr thread))
7698               (when (gnus-invisible-cut-children (cdr thread))
7699                 (let ((th (cdr thread)))
7700                   (while th
7701                     (if (memq (mail-header-number (caar th))
7702                               gnus-newsgroup-limit)
7703                         (setq thread (car th)
7704                               th nil)
7705                       (setq th (cdr th))))))))))
7706   thread)
7707
7708 (defun gnus-cut-threads (threads)
7709   "Cut off all uninteresting articles from the beginning of threads."
7710   (when (or (eq gnus-fetch-old-headers 'some)
7711             (eq gnus-fetch-old-headers 'invisible)
7712             (numberp gnus-fetch-old-headers)
7713             (eq gnus-build-sparse-threads 'some)
7714             (eq gnus-build-sparse-threads 'more))
7715     (let ((th threads))
7716       (while th
7717         (setcar th (gnus-cut-thread (car th)))
7718         (setq th (cdr th)))))
7719   ;; Remove nixed out threads.
7720   (delq nil threads))
7721
7722 (defun gnus-summary-initial-limit (&optional show-if-empty)
7723   "Figure out what the initial limit is supposed to be on group entry.
7724 This entails weeding out unwanted dormants, low-scored articles,
7725 fetch-old-headers verbiage, and so on."
7726   ;; Most groups have nothing to remove.
7727   (if (or gnus-inhibit-limiting
7728           (and (null gnus-newsgroup-dormant)
7729                (eq gnus-newsgroup-display 'gnus-not-ignore)
7730                (not (eq gnus-fetch-old-headers 'some))
7731                (not (numberp gnus-fetch-old-headers))
7732                (not (eq gnus-fetch-old-headers 'invisible))
7733                (null gnus-summary-expunge-below)
7734                (not (eq gnus-build-sparse-threads 'some))
7735                (not (eq gnus-build-sparse-threads 'more))
7736                (null gnus-thread-expunge-below)
7737                (not gnus-use-nocem)))
7738       ()                                ; Do nothing.
7739     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7740     (setq gnus-newsgroup-limit nil)
7741     (mapatoms
7742      (lambda (node)
7743        (unless (car (symbol-value node))
7744          ;; These threads have no parents -- they are roots.
7745          (let ((nodes (cdr (symbol-value node)))
7746                thread)
7747            (while nodes
7748              (if (and gnus-thread-expunge-below
7749                       (< (gnus-thread-total-score (car nodes))
7750                          gnus-thread-expunge-below))
7751                  (gnus-expunge-thread (pop nodes))
7752                (setq thread (pop nodes))
7753                (gnus-summary-limit-children thread))))))
7754      gnus-newsgroup-dependencies)
7755     ;; If this limitation resulted in an empty group, we might
7756     ;; pop the previous limit and use it instead.
7757     (when (and (not gnus-newsgroup-limit)
7758                show-if-empty)
7759       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7760     gnus-newsgroup-limit))
7761
7762 (defun gnus-summary-limit-children (thread)
7763   "Return 1 if this subthread is visible and 0 if it is not."
7764   ;; First we get the number of visible children to this thread.  This
7765   ;; is done by recursing down the thread using this function, so this
7766   ;; will really go down to a leaf article first, before slowly
7767   ;; working its way up towards the root.
7768   (when thread
7769     (let* ((max-lisp-eval-depth 5000)
7770            (children
7771            (if (cdr thread)
7772                (apply '+ (mapcar 'gnus-summary-limit-children
7773                                  (cdr thread)))
7774              0))
7775           (number (mail-header-number (car thread)))
7776           score)
7777       (if (and
7778            (not (memq number gnus-newsgroup-marked))
7779            (or
7780             ;; If this article is dormant and has absolutely no visible
7781             ;; children, then this article isn't visible.
7782             (and (memq number gnus-newsgroup-dormant)
7783                  (zerop children))
7784             ;; If this is "fetch-old-headered" and there is no
7785             ;; visible children, then we don't want this article.
7786             (and (or (eq gnus-fetch-old-headers 'some)
7787                      (numberp gnus-fetch-old-headers))
7788                  (gnus-summary-article-ancient-p number)
7789                  (zerop children))
7790             ;; If this is "fetch-old-headered" and `invisible', then
7791             ;; we don't want this article.
7792             (and (eq gnus-fetch-old-headers 'invisible)
7793                  (gnus-summary-article-ancient-p number))
7794             ;; If this is a sparsely inserted article with no children,
7795             ;; we don't want it.
7796             (and (eq gnus-build-sparse-threads 'some)
7797                  (gnus-summary-article-sparse-p number)
7798                  (zerop children))
7799             ;; If we use expunging, and this article is really
7800             ;; low-scored, then we don't want this article.
7801             (when (and gnus-summary-expunge-below
7802                        (< (setq score
7803                                 (or (cdr (assq number gnus-newsgroup-scored))
7804                                     gnus-summary-default-score))
7805                           gnus-summary-expunge-below))
7806               ;; We increase the expunge-tally here, but that has
7807               ;; nothing to do with the limits, really.
7808               (incf gnus-newsgroup-expunged-tally)
7809               ;; We also mark as read here, if that's wanted.
7810               (when (and gnus-summary-mark-below
7811                          (< score gnus-summary-mark-below))
7812                 (setq gnus-newsgroup-unreads
7813                       (delq number gnus-newsgroup-unreads))
7814                 (if gnus-newsgroup-auto-expire
7815                     (push number gnus-newsgroup-expirable)
7816                   (push (cons number gnus-low-score-mark)
7817                         gnus-newsgroup-reads)))
7818               t)
7819             ;; Do the `display' group parameter.
7820             (and gnus-newsgroup-display
7821                  (not (funcall gnus-newsgroup-display)))
7822             ;; Check NoCeM things.
7823             (if (and gnus-use-nocem
7824                      (gnus-nocem-unwanted-article-p
7825                       (mail-header-id (car thread))))
7826                 (progn
7827                   (setq gnus-newsgroup-unreads
7828                         (delq number gnus-newsgroup-unreads))
7829                   t))))
7830           ;; Nope, invisible article.
7831           0
7832         ;; Ok, this article is to be visible, so we add it to the limit
7833         ;; and return 1.
7834         (push number gnus-newsgroup-limit)
7835         1))))
7836
7837 (defun gnus-expunge-thread (thread)
7838   "Mark all articles in THREAD as read."
7839   (let* ((number (mail-header-number (car thread))))
7840     (incf gnus-newsgroup-expunged-tally)
7841     ;; We also mark as read here, if that's wanted.
7842     (setq gnus-newsgroup-unreads
7843           (delq number gnus-newsgroup-unreads))
7844     (if gnus-newsgroup-auto-expire
7845         (push number gnus-newsgroup-expirable)
7846       (push (cons number gnus-low-score-mark)
7847             gnus-newsgroup-reads)))
7848   ;; Go recursively through all subthreads.
7849   (mapcar 'gnus-expunge-thread (cdr thread)))
7850
7851 ;; Summary article oriented commands
7852
7853 (defun gnus-summary-refer-parent-article (n)
7854   "Refer parent article N times.
7855 If N is negative, go to ancestor -N instead.
7856 The difference between N and the number of articles fetched is returned."
7857   (interactive "p")
7858   (let ((skip 1)
7859         error header ref)
7860     (when (not (natnump n))
7861       (setq skip (abs n)
7862             n 1))
7863     (while (and (> n 0)
7864                 (not error))
7865       (setq header (gnus-summary-article-header))
7866       (if (and (eq (mail-header-number header)
7867                    (cdr gnus-article-current))
7868                (equal gnus-newsgroup-name
7869                       (car gnus-article-current)))
7870           ;; If we try to find the parent of the currently
7871           ;; displayed article, then we take a look at the actual
7872           ;; References header, since this is slightly more
7873           ;; reliable than the References field we got from the
7874           ;; server.
7875           (save-excursion
7876             (set-buffer gnus-original-article-buffer)
7877             (nnheader-narrow-to-headers)
7878             (unless (setq ref (message-fetch-field "references"))
7879               (setq ref (message-fetch-field "in-reply-to")))
7880             (widen))
7881         (setq ref
7882               ;; It's not the current article, so we take a bet on
7883               ;; the value we got from the server.
7884               (mail-header-references header)))
7885       (if (and ref
7886                (not (equal ref "")))
7887           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7888             (gnus-message 1 "Couldn't find parent"))
7889         (gnus-message 1 "No references in article %d"
7890                       (gnus-summary-article-number))
7891         (setq error t))
7892       (decf n))
7893     (gnus-summary-position-point)
7894     n))
7895
7896 (defun gnus-summary-refer-references ()
7897   "Fetch all articles mentioned in the References header.
7898 Return the number of articles fetched."
7899   (interactive)
7900   (let ((ref (mail-header-references (gnus-summary-article-header)))
7901         (current (gnus-summary-article-number))
7902         (n 0))
7903     (if (or (not ref)
7904             (equal ref ""))
7905         (error "No References in the current article")
7906       ;; For each Message-ID in the References header...
7907       (while (string-match "<[^>]*>" ref)
7908         (incf n)
7909         ;; ... fetch that article.
7910         (gnus-summary-refer-article
7911          (prog1 (match-string 0 ref)
7912            (setq ref (substring ref (match-end 0))))))
7913       (gnus-summary-goto-subject current)
7914       (gnus-summary-position-point)
7915       n)))
7916
7917 (defun gnus-summary-refer-thread (&optional limit)
7918   "Fetch all articles in the current thread.
7919 If LIMIT (the numerical prefix), fetch that many old headers instead
7920 of what's specified by the `gnus-refer-thread-limit' variable."
7921   (interactive "P")
7922   (let ((id (mail-header-id (gnus-summary-article-header)))
7923         (limit (if limit (prefix-numeric-value limit)
7924                  gnus-refer-thread-limit)))
7925     ;; We want to fetch LIMIT *old* headers, but we also have to
7926     ;; re-fetch all the headers in the current buffer, because many of
7927     ;; them may be undisplayed.  So we adjust LIMIT.
7928     (when (numberp limit)
7929       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7930     (unless (eq gnus-fetch-old-headers 'invisible)
7931       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7932       ;; Retrieve the headers and read them in.
7933       (if (eq (gnus-retrieve-headers
7934                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7935               'nov)
7936           (gnus-build-all-threads)
7937         (error "Can't fetch thread from backends that don't support NOV"))
7938       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7939     (gnus-summary-limit-include-thread id)))
7940
7941 (defun gnus-summary-refer-article (message-id)
7942   "Fetch an article specified by MESSAGE-ID."
7943   (interactive "sMessage-ID: ")
7944   (when (and (stringp message-id)
7945              (not (zerop (length message-id))))
7946     ;; Construct the correct Message-ID if necessary.
7947     ;; Suggested by tale@pawl.rpi.edu.
7948     (unless (string-match "^<" message-id)
7949       (setq message-id (concat "<" message-id)))
7950     (unless (string-match ">$" message-id)
7951       (setq message-id (concat message-id ">")))
7952     (let* ((header (gnus-id-to-header message-id))
7953            (sparse (and header
7954                         (gnus-summary-article-sparse-p
7955                          (mail-header-number header))
7956                         (memq (mail-header-number header)
7957                               gnus-newsgroup-limit)))
7958            number)
7959       (cond
7960        ;; If the article is present in the buffer we just go to it.
7961        ((and header
7962              (or (not (gnus-summary-article-sparse-p
7963                        (mail-header-number header)))
7964                  sparse))
7965         (prog1
7966             (gnus-summary-goto-article
7967              (mail-header-number header) nil t)
7968           (when sparse
7969             (gnus-summary-update-article (mail-header-number header)))))
7970        (t
7971         ;; We fetch the article.
7972         (catch 'found
7973           (dolist (gnus-override-method (gnus-refer-article-methods))
7974             (gnus-check-server gnus-override-method)
7975             ;; Fetch the header, and display the article.
7976             (when (setq number (gnus-summary-insert-subject message-id))
7977               (gnus-summary-select-article nil nil nil number)
7978               (throw 'found t)))
7979           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7980
7981 (defun gnus-refer-article-methods ()
7982   "Return a list of referable methods."
7983   (cond
7984    ;; No method, so we default to current and native.
7985    ((null gnus-refer-article-method)
7986     (list gnus-current-select-method gnus-select-method))
7987    ;; Current.
7988    ((eq 'current gnus-refer-article-method)
7989     (list gnus-current-select-method))
7990    ;; List of select methods.
7991    ((not (and (symbolp (car gnus-refer-article-method))
7992               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7993     (let (out)
7994       (dolist (method gnus-refer-article-method)
7995         (push (if (eq 'current method)
7996                   gnus-current-select-method
7997                 method)
7998               out))
7999       (nreverse out)))
8000    ;; One single select method.
8001    (t
8002     (list gnus-refer-article-method))))
8003
8004 (defun gnus-summary-edit-parameters ()
8005   "Edit the group parameters of the current group."
8006   (interactive)
8007   (gnus-group-edit-group gnus-newsgroup-name 'params))
8008
8009 (defun gnus-summary-customize-parameters ()
8010   "Customize the group parameters of the current group."
8011   (interactive)
8012   (gnus-group-customize gnus-newsgroup-name))
8013
8014 (defun gnus-summary-enter-digest-group (&optional force)
8015   "Enter an nndoc group based on the current article.
8016 If FORCE, force a digest interpretation.  If not, try
8017 to guess what the document format is."
8018   (interactive "P")
8019   (let ((conf gnus-current-window-configuration))
8020     (save-excursion
8021       (gnus-summary-select-article))
8022     (setq gnus-current-window-configuration conf)
8023     (let* ((name (format "%s-%d"
8024                          (gnus-group-prefixed-name
8025                           gnus-newsgroup-name (list 'nndoc ""))
8026                          (save-excursion
8027                            (set-buffer gnus-summary-buffer)
8028                            gnus-current-article)))
8029            (ogroup gnus-newsgroup-name)
8030            (params (append (gnus-info-params (gnus-get-info ogroup))
8031                            (list (cons 'to-group ogroup))
8032                            (list (cons 'save-article-group ogroup))))
8033            (case-fold-search t)
8034            (buf (current-buffer))
8035            dig to-address)
8036       (save-excursion
8037         (set-buffer gnus-original-article-buffer)
8038         ;; Have the digest group inherit the main mail address of
8039         ;; the parent article.
8040         (when (setq to-address (or (gnus-fetch-field "reply-to")
8041                                    (gnus-fetch-field "from")))
8042           (setq params (append
8043                         (list (cons 'to-address
8044                                     (funcall gnus-decode-encoded-word-function
8045                                              to-address))))))
8046         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8047         (insert-buffer-substring gnus-original-article-buffer)
8048         ;; Remove lines that may lead nndoc to misinterpret the
8049         ;; document type.
8050         (narrow-to-region
8051          (goto-char (point-min))
8052          (or (search-forward "\n\n" nil t) (point)))
8053         (goto-char (point-min))
8054         (delete-matching-lines "^Path:\\|^From ")
8055         (widen))
8056       (unwind-protect
8057           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8058                     (gnus-newsgroup-ephemeral-ignored-charsets
8059                      gnus-newsgroup-ignored-charsets))
8060                 (gnus-group-read-ephemeral-group
8061                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8062                               (nndoc-article-type
8063                                ,(if force 'mbox 'guess)))
8064                  t nil nil nil
8065                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8066                                                         "ADAPT")))))
8067               ;; Make all postings to this group go to the parent group.
8068               (nconc (gnus-info-params (gnus-get-info name))
8069                      params)
8070             ;; Couldn't select this doc group.
8071             (switch-to-buffer buf)
8072             (gnus-set-global-variables)
8073             (gnus-configure-windows 'summary)
8074             (gnus-message 3 "Article couldn't be entered?"))
8075         (kill-buffer dig)))))
8076
8077 (defun gnus-summary-read-document (n)
8078   "Open a new group based on the current article(s).
8079 This will allow you to read digests and other similar
8080 documents as newsgroups.
8081 Obeys the standard process/prefix convention."
8082   (interactive "P")
8083   (let* ((articles (gnus-summary-work-articles n))
8084          (ogroup gnus-newsgroup-name)
8085          (params (append (gnus-info-params (gnus-get-info ogroup))
8086                          (list (cons 'to-group ogroup))))
8087          article group egroup groups vgroup)
8088     (while (setq article (pop articles))
8089       (setq group (format "%s-%d" gnus-newsgroup-name article))
8090       (gnus-summary-remove-process-mark article)
8091       (when (gnus-summary-display-article article)
8092         (save-excursion
8093           (with-temp-buffer
8094             (insert-buffer-substring gnus-original-article-buffer)
8095             ;; Remove some headers that may lead nndoc to make
8096             ;; the wrong guess.
8097             (message-narrow-to-head)
8098             (goto-char (point-min))
8099             (delete-matching-lines "^\\(Path\\):\\|^From ")
8100             (widen)
8101             (if (setq egroup
8102                       (gnus-group-read-ephemeral-group
8103                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8104                                      (nndoc-article-type guess))
8105                        t nil t))
8106                 (progn
8107             ;; Make all postings to this group go to the parent group.
8108                   (nconc (gnus-info-params (gnus-get-info egroup))
8109                          params)
8110                   (push egroup groups))
8111               ;; Couldn't select this doc group.
8112               (gnus-error 3 "Article couldn't be entered"))))))
8113     ;; Now we have selected all the documents.
8114     (cond
8115      ((not groups)
8116       (error "None of the articles could be interpreted as documents"))
8117      ((gnus-group-read-ephemeral-group
8118        (setq vgroup (format
8119                      "nnvirtual:%s-%s" gnus-newsgroup-name
8120                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8121        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8122        t
8123        (cons (current-buffer) 'summary)))
8124      (t
8125       (error "Couldn't select virtual nndoc group")))))
8126
8127 (defun gnus-summary-isearch-article (&optional regexp-p)
8128   "Do incremental search forward on the current article.
8129 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8130   (interactive "P")
8131   (gnus-summary-select-article)
8132   (gnus-configure-windows 'article)
8133   (gnus-eval-in-buffer-window gnus-article-buffer
8134     (save-restriction
8135       (widen)
8136       (isearch-forward regexp-p))))
8137
8138 (defun gnus-summary-search-article-forward (regexp &optional backward)
8139   "Search for an article containing REGEXP forward.
8140 If BACKWARD, search backward instead."
8141   (interactive
8142    (list (read-string
8143           (format "Search article %s (regexp%s): "
8144                   (if current-prefix-arg "backward" "forward")
8145                   (if gnus-last-search-regexp
8146                       (concat ", default " gnus-last-search-regexp)
8147                     "")))
8148          current-prefix-arg))
8149   (if (string-equal regexp "")
8150       (setq regexp (or gnus-last-search-regexp ""))
8151     (setq gnus-last-search-regexp regexp)
8152     (setq gnus-article-before-search gnus-current-article))
8153   ;; Intentionally set gnus-last-article.
8154   (setq gnus-last-article gnus-article-before-search)
8155   (let ((gnus-last-article gnus-last-article))
8156     (if (gnus-summary-search-article regexp backward)
8157         (gnus-summary-show-thread)
8158       (error "Search failed: \"%s\"" regexp))))
8159
8160 (defun gnus-summary-search-article-backward (regexp)
8161   "Search for an article containing REGEXP backward."
8162   (interactive
8163    (list (read-string
8164           (format "Search article backward (regexp%s): "
8165                   (if gnus-last-search-regexp
8166                       (concat ", default " gnus-last-search-regexp)
8167                     "")))))
8168   (gnus-summary-search-article-forward regexp 'backward))
8169
8170 (defun gnus-summary-search-article (regexp &optional backward)
8171   "Search for an article containing REGEXP.
8172 Optional argument BACKWARD means do search for backward.
8173 `gnus-select-article-hook' is not called during the search."
8174   ;; We have to require this here to make sure that the following
8175   ;; dynamic binding isn't shadowed by autoloading.
8176   (require 'gnus-async)
8177   (require 'gnus-art)
8178   (let ((gnus-select-article-hook nil)  ;Disable hook.
8179         (gnus-article-prepare-hook nil)
8180         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8181         (gnus-use-article-prefetch nil)
8182         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8183         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8184         (sum (current-buffer))
8185         (gnus-display-mime-function nil)
8186         (found nil)
8187         point)
8188     (gnus-save-hidden-threads
8189       (gnus-summary-select-article)
8190       (set-buffer gnus-article-buffer)
8191       (goto-char (window-point (get-buffer-window (current-buffer))))
8192       (when backward
8193         (forward-line -1))
8194       (while (not found)
8195         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8196         (if (if backward
8197                 (re-search-backward regexp nil t)
8198               (re-search-forward regexp nil t))
8199             ;; We found the regexp.
8200             (progn
8201               (setq found 'found)
8202               (beginning-of-line)
8203               (set-window-start
8204                (get-buffer-window (current-buffer))
8205                (point))
8206               (forward-line 1)
8207               (set-window-point
8208                (get-buffer-window (current-buffer))
8209                (point))
8210               (set-buffer sum)
8211               (setq point (point)))
8212           ;; We didn't find it, so we go to the next article.
8213           (set-buffer sum)
8214           (setq found 'not)
8215           (while (eq found 'not)
8216             (if (not (if backward (gnus-summary-find-prev)
8217                        (gnus-summary-find-next)))
8218                 ;; No more articles.
8219                 (setq found t)
8220               ;; Select the next article and adjust point.
8221               (unless (gnus-summary-article-sparse-p
8222                        (gnus-summary-article-number))
8223                 (setq found nil)
8224                 (gnus-summary-select-article)
8225                 (set-buffer gnus-article-buffer)
8226                 (widen)
8227                 (goto-char (if backward (point-max) (point-min))))))))
8228       (gnus-message 7 ""))
8229     ;; Return whether we found the regexp.
8230     (when (eq found 'found)
8231       (goto-char point)
8232       (gnus-summary-show-thread)
8233       (gnus-summary-goto-subject gnus-current-article)
8234       (gnus-summary-position-point)
8235       t)))
8236
8237 (defun gnus-find-matching-articles (header regexp)
8238   "Return a list of all articles that match REGEXP on HEADER.
8239 This search includes all articles in the current group that Gnus has
8240 fetched headers for, whether they are displayed or not."
8241   (let ((articles nil)
8242         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8243         (case-fold-search t))
8244     (dolist (header gnus-newsgroup-headers)
8245       (when (string-match regexp (funcall func header))
8246         (push (mail-header-number header) articles)))
8247     (nreverse articles)))
8248
8249 (defun gnus-summary-find-matching (header regexp &optional backward unread
8250                                           not-case-fold not-matching)
8251   "Return a list of all articles that match REGEXP on HEADER.
8252 The search stars on the current article and goes forwards unless
8253 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8254 If UNREAD is non-nil, only unread articles will
8255 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8256 in the comparisons. If NOT-MATCHING, return a list of all articles that
8257 not match REGEXP on HEADER."
8258   (let ((case-fold-search (not not-case-fold))
8259         articles d func)
8260     (if (consp header)
8261         (if (eq (car header) 'extra)
8262             (setq func
8263                   `(lambda (h)
8264                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8265                          "")))
8266           (error "%s is an invalid header" header))
8267       (unless (fboundp (intern (concat "mail-header-" header)))
8268         (error "%s is not a valid header" header))
8269       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8270     (dolist (d (if (eq backward 'all)
8271                    gnus-newsgroup-data
8272                  (gnus-data-find-list
8273                   (gnus-summary-article-number)
8274                   (gnus-data-list backward))))
8275       (when (and (or (not unread)       ; We want all articles...
8276                      (gnus-data-unread-p d)) ; Or just unreads.
8277                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8278                  (if not-matching
8279                      (not (string-match
8280                            regexp
8281                            (funcall func (gnus-data-header d))))
8282                    (string-match regexp
8283                                  (funcall func (gnus-data-header d)))))
8284         (push (gnus-data-number d) articles))) ; Success!
8285     (nreverse articles)))
8286
8287 (defun gnus-summary-execute-command (header regexp command &optional backward)
8288   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8289 If HEADER is an empty string (or nil), the match is done on the entire
8290 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8291   (interactive
8292    (list (let ((completion-ignore-case t))
8293            (completing-read
8294             "Header name: "
8295             (mapcar (lambda (header) (list (format "%s" header)))
8296                     (append
8297                      '("Number" "Subject" "From" "Lines" "Date"
8298                        "Message-ID" "Xref" "References" "Body")
8299                      gnus-extra-headers))
8300             nil 'require-match))
8301          (read-string "Regexp: ")
8302          (read-key-sequence "Command: ")
8303          current-prefix-arg))
8304   (when (equal header "Body")
8305     (setq header ""))
8306   ;; Hidden thread subtrees must be searched as well.
8307   (gnus-summary-show-all-threads)
8308   ;; We don't want to change current point nor window configuration.
8309   (save-excursion
8310     (save-window-excursion
8311       (let (gnus-visual
8312             gnus-treat-strip-trailing-blank-lines
8313             gnus-treat-strip-leading-blank-lines
8314             gnus-treat-strip-multiple-blank-lines
8315             gnus-treat-hide-boring-headers
8316             gnus-treat-fold-newsgroups
8317             gnus-article-prepare-hook)
8318         (gnus-message 6 "Executing %s..." (key-description command))
8319         ;; We'd like to execute COMMAND interactively so as to give arguments.
8320         (gnus-execute header regexp
8321                       `(call-interactively ',(key-binding command))
8322                       backward)
8323         (gnus-message 6 "Executing %s...done" (key-description command))))))
8324
8325 (defun gnus-summary-beginning-of-article ()
8326   "Scroll the article back to the beginning."
8327   (interactive)
8328   (gnus-summary-select-article)
8329   (gnus-configure-windows 'article)
8330   (gnus-eval-in-buffer-window gnus-article-buffer
8331     (widen)
8332     (goto-char (point-min))
8333     (when gnus-page-broken
8334       (gnus-narrow-to-page))))
8335
8336 (defun gnus-summary-end-of-article ()
8337   "Scroll to the end of the article."
8338   (interactive)
8339   (gnus-summary-select-article)
8340   (gnus-configure-windows 'article)
8341   (gnus-eval-in-buffer-window gnus-article-buffer
8342     (widen)
8343     (goto-char (point-max))
8344     (recenter -3)
8345     (when gnus-page-broken
8346       (gnus-narrow-to-page))))
8347
8348 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8349   "Truncate to LEN and quote all \"(\"'s in STRING."
8350   (gnus-replace-in-string (if (and len (> (length string) len))
8351                               (substring string 0 len)
8352                             string)
8353                           "[()]" "\\\\\\&"))
8354
8355 (defun gnus-summary-print-article (&optional filename n)
8356   "Generate and print a PostScript image of the N next (mail) articles.
8357
8358 If N is negative, print the N previous articles.  If N is nil and articles
8359 have been marked with the process mark, print these instead.
8360
8361 If the optional first argument FILENAME is nil, send the image to the
8362 printer.  If FILENAME is a string, save the PostScript image in a file with
8363 that name.  If FILENAME is a number, prompt the user for the name of the file
8364 to save in."
8365   (interactive (list (ps-print-preprint current-prefix-arg)))
8366   (dolist (article (gnus-summary-work-articles n))
8367     (gnus-summary-select-article nil nil 'pseudo article)
8368     (gnus-eval-in-buffer-window gnus-article-buffer
8369       (gnus-print-buffer))
8370     (gnus-summary-remove-process-mark article))
8371   (ps-despool filename))
8372
8373 (defun gnus-print-buffer ()
8374   (let ((buffer (generate-new-buffer " *print*")))
8375     (unwind-protect
8376         (progn
8377           (copy-to-buffer buffer (point-min) (point-max))
8378           (set-buffer buffer)
8379           (gnus-article-delete-invisible-text)
8380           (gnus-remove-text-with-property 'gnus-decoration)
8381           (when (gnus-visual-p 'article-highlight 'highlight)
8382             ;; Copy-to-buffer doesn't copy overlay.  So redo
8383             ;; highlight.
8384             (let ((gnus-article-buffer buffer))
8385               (gnus-article-highlight-citation t)
8386               (gnus-article-highlight-signature)))
8387           (let ((ps-left-header
8388                  (list
8389                   (concat "("
8390                           (gnus-summary-print-truncate-and-quote
8391                            (mail-header-subject gnus-current-headers)
8392                            66) ")")
8393                   (concat "("
8394                           (gnus-summary-print-truncate-and-quote
8395                            (mail-header-from gnus-current-headers)
8396                            45) ")")))
8397                 (ps-right-header
8398                  (list
8399                   "/pagenumberstring load"
8400                   (concat "("
8401                           (mail-header-date gnus-current-headers) ")"))))
8402             (gnus-run-hooks 'gnus-ps-print-hook)
8403             (save-excursion
8404               (if window-system
8405                   (ps-spool-buffer-with-faces)
8406                 (ps-spool-buffer)))))
8407       (kill-buffer buffer))))
8408
8409 (defun gnus-summary-show-article (&optional arg)
8410   "Force redisplaying of the current article.
8411 If ARG (the prefix) is a number, show the article with the charset
8412 defined in `gnus-summary-show-article-charset-alist', or the charset
8413 input.
8414 If ARG (the prefix) is non-nil and not a number, show the raw article
8415 without any article massaging functions being run.  Normally, the key strokes
8416 are `C-u g'."
8417   (interactive "P")
8418   (cond
8419    ((numberp arg)
8420     (gnus-summary-show-article t)
8421     (let ((gnus-newsgroup-charset
8422            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8423                (mm-read-coding-system
8424                 "View as charset: " ;; actually it is coding system.
8425                 (save-excursion
8426                   (set-buffer gnus-article-buffer)
8427                   (mm-detect-coding-region (point) (point-max))))))
8428           (gnus-newsgroup-ignored-charsets 'gnus-all))
8429       (gnus-summary-select-article nil 'force)
8430       (let ((deps gnus-newsgroup-dependencies)
8431             head header lines)
8432         (save-excursion
8433           (set-buffer gnus-original-article-buffer)
8434           (save-restriction
8435             (message-narrow-to-head)
8436             (setq head (buffer-string))
8437             (goto-char (point-min))
8438             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8439               (goto-char (point-max))
8440               (widen)
8441               (setq lines (1- (count-lines (point) (point-max))))))
8442           (with-temp-buffer
8443             (insert (format "211 %d Article retrieved.\n"
8444                             (cdr gnus-article-current)))
8445             (insert head)
8446             (if lines (insert (format "Lines: %d\n" lines)))
8447             (insert ".\n")
8448             (let ((nntp-server-buffer (current-buffer)))
8449               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8450         (gnus-data-set-header
8451          (gnus-data-find (cdr gnus-article-current))
8452          header)
8453         (gnus-summary-update-article-line
8454          (cdr gnus-article-current) header)
8455         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8456           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8457    ((not arg)
8458     ;; Select the article the normal way.
8459     (gnus-summary-select-article nil 'force))
8460    (t
8461     ;; We have to require this here to make sure that the following
8462     ;; dynamic binding isn't shadowed by autoloading.
8463     (require 'gnus-async)
8464     (require 'gnus-art)
8465     ;; Bind the article treatment functions to nil.
8466     (let ((gnus-have-all-headers t)
8467           gnus-article-prepare-hook
8468           gnus-article-decode-hook
8469           gnus-display-mime-function
8470           gnus-break-pages)
8471       ;; Destroy any MIME parts.
8472       (when (gnus-buffer-live-p gnus-article-buffer)
8473         (save-excursion
8474           (set-buffer gnus-article-buffer)
8475           (mm-destroy-parts gnus-article-mime-handles)
8476           ;; Set it to nil for safety reason.
8477           (setq gnus-article-mime-handle-alist nil)
8478           (setq gnus-article-mime-handles nil)))
8479       (gnus-summary-select-article nil 'force))))
8480   (gnus-summary-goto-subject gnus-current-article)
8481   (gnus-summary-position-point))
8482
8483 (defun gnus-summary-show-raw-article ()
8484   "Show the raw article without any article massaging functions being run."
8485   (interactive)
8486   (gnus-summary-show-article t))
8487
8488 (defun gnus-summary-verbose-headers (&optional arg)
8489   "Toggle permanent full header display.
8490 If ARG is a positive number, turn header display on.
8491 If ARG is a negative number, turn header display off."
8492   (interactive "P")
8493   (setq gnus-show-all-headers
8494         (cond ((or (not (numberp arg))
8495                    (zerop arg))
8496                (not gnus-show-all-headers))
8497               ((natnump arg)
8498                t)))
8499   (gnus-summary-show-article))
8500
8501 (defun gnus-summary-toggle-header (&optional arg)
8502   "Show the headers if they are hidden, or hide them if they are shown.
8503 If ARG is a positive number, show the entire header.
8504 If ARG is a negative number, hide the unwanted header lines."
8505   (interactive "P")
8506   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8507                      (get-buffer-window gnus-article-buffer t))))
8508     (with-current-buffer gnus-article-buffer
8509       (widen)
8510       (article-narrow-to-head)
8511       (let* ((buffer-read-only nil)
8512              (inhibit-point-motion-hooks t)
8513              (hidden (if (numberp arg)
8514                          (>= arg 0)
8515                        (gnus-article-hidden-text-p 'headers)))
8516              s e)
8517         (delete-region (point-min) (point-max))
8518         (with-current-buffer gnus-original-article-buffer
8519           (goto-char (setq s (point-min)))
8520           (setq e (if (search-forward "\n\n" nil t)
8521                       (1- (point))
8522                     (point-max))))
8523         (insert-buffer-substring gnus-original-article-buffer s e)
8524         (article-decode-encoded-words)
8525         (if hidden
8526             (let ((gnus-treat-hide-headers nil)
8527                   (gnus-treat-hide-boring-headers nil))
8528               (gnus-delete-wash-type 'headers)
8529               (gnus-treat-article 'head))
8530           (gnus-treat-article 'head))
8531         (widen)
8532         (if window
8533             (set-window-start window (goto-char (point-min))))
8534         (setq gnus-page-broken
8535               (when gnus-break-pages
8536                 (gnus-narrow-to-page)
8537                 t))
8538         (gnus-set-mode-line 'article)))))
8539
8540 (defun gnus-summary-show-all-headers ()
8541   "Make all header lines visible."
8542   (interactive)
8543   (gnus-summary-toggle-header 1))
8544
8545 (defun gnus-summary-caesar-message (&optional arg)
8546   "Caesar rotate the current article by 13.
8547 The numerical prefix specifies how many places to rotate each letter
8548 forward."
8549   (interactive "P")
8550   (gnus-summary-select-article)
8551   (let ((mail-header-separator ""))
8552     (gnus-eval-in-buffer-window gnus-article-buffer
8553       (save-restriction
8554         (widen)
8555         (let ((start (window-start))
8556               buffer-read-only)
8557           (message-caesar-buffer-body arg)
8558           (set-window-start (get-buffer-window (current-buffer)) start))))))
8559
8560 (autoload 'unmorse-region "morse"
8561   "Convert morse coded text in region to ordinary ASCII text."
8562   t)
8563
8564 (defun gnus-summary-morse-message (&optional arg)
8565   "Morse decode the current article."
8566   (interactive "P")
8567   (gnus-summary-select-article)
8568   (let ((mail-header-separator ""))
8569     (gnus-eval-in-buffer-window gnus-article-buffer
8570       (save-excursion
8571         (save-restriction
8572           (widen)
8573           (let ((pos (window-start))
8574                 buffer-read-only)
8575             (goto-char (point-min))
8576             (when (message-goto-body)
8577               (gnus-narrow-to-body))
8578             (goto-char (point-min))
8579             (while (re-search-forward "·" (point-max) t)
8580               (replace-match "."))
8581             (unmorse-region (point-min) (point-max))
8582             (widen)
8583             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8584
8585 (defun gnus-summary-stop-page-breaking ()
8586   "Stop page breaking in the current article."
8587   (interactive)
8588   (gnus-summary-select-article)
8589   (gnus-eval-in-buffer-window gnus-article-buffer
8590     (widen)
8591     (when (gnus-visual-p 'page-marker)
8592       (let ((buffer-read-only nil))
8593         (gnus-remove-text-with-property 'gnus-prev)
8594         (gnus-remove-text-with-property 'gnus-next))
8595       (setq gnus-page-broken nil))))
8596
8597 (defun gnus-summary-move-article (&optional n to-newsgroup
8598                                             select-method action)
8599   "Move the current article to a different newsgroup.
8600 If N is a positive number, move the N next articles.
8601 If N is a negative number, move the N previous articles.
8602 If N is nil and any articles have been marked with the process mark,
8603 move those articles instead.
8604 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8605 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8606 re-spool using this method.
8607
8608 When called interactively with TO-NEWSGROUP being nil, the value of
8609 the variable `gnus-move-split-methods' is used for finding a default
8610 for the target newsgroup.
8611
8612 For this function to work, both the current newsgroup and the
8613 newsgroup that you want to move to have to support the `request-move'
8614 and `request-accept' functions.
8615
8616 ACTION can be either `move' (the default), `crosspost' or `copy'."
8617   (interactive "P")
8618   (unless action
8619     (setq action 'move))
8620   ;; Check whether the source group supports the required functions.
8621   (cond ((and (eq action 'move)
8622               (not (gnus-check-backend-function
8623                     'request-move-article gnus-newsgroup-name)))
8624          (error "The current group does not support article moving"))
8625         ((and (eq action 'crosspost)
8626               (not (gnus-check-backend-function
8627                     'request-replace-article gnus-newsgroup-name)))
8628          (error "The current group does not support article editing")))
8629   (let ((articles (gnus-summary-work-articles n))
8630         (prefix (if (gnus-check-backend-function
8631                      'request-move-article gnus-newsgroup-name)
8632                     (gnus-group-real-prefix gnus-newsgroup-name)
8633                   ""))
8634         (names '((move "Move" "Moving")
8635                  (copy "Copy" "Copying")
8636                  (crosspost "Crosspost" "Crossposting")))
8637         (copy-buf (save-excursion
8638                     (nnheader-set-temp-buffer " *copy article*")))
8639         art-group to-method new-xref article to-groups)
8640     (unless (assq action names)
8641       (error "Unknown action %s" action))
8642     ;; Read the newsgroup name.
8643     (when (and (not to-newsgroup)
8644                (not select-method))
8645       (if (and gnus-move-split-methods
8646                (not
8647                 (and (memq gnus-current-article articles)
8648                      (gnus-buffer-live-p gnus-original-article-buffer))))
8649           ;; When `gnus-move-split-methods' is non-nil, we have to
8650           ;; select an article to give `gnus-read-move-group-name' an
8651           ;; opportunity to suggest an appropriate default.  However,
8652           ;; we needn't render or mark the article.
8653           (let ((gnus-display-mime-function nil)
8654                 (gnus-article-prepare-hook nil)
8655                 (gnus-mark-article-hook nil))
8656             (gnus-summary-select-article nil nil nil (car articles))))
8657       (setq to-newsgroup
8658             (gnus-read-move-group-name
8659              (cadr (assq action names))
8660              (symbol-value (intern (format "gnus-current-%s-group" action)))
8661              articles prefix))
8662       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8663     (setq to-method (or select-method
8664                         (gnus-server-to-method
8665                          (gnus-group-method to-newsgroup))))
8666     ;; Check the method we are to move this article to...
8667     (unless (gnus-check-backend-function
8668              'request-accept-article (car to-method))
8669       (error "%s does not support article copying" (car to-method)))
8670     (unless (gnus-check-server to-method)
8671       (error "Can't open server %s" (car to-method)))
8672     (gnus-message 6 "%s to %s: %s..."
8673                   (caddr (assq action names))
8674                   (or (car select-method) to-newsgroup) articles)
8675     (while articles
8676       (setq article (pop articles))
8677       (setq
8678        art-group
8679        (cond
8680         ;; Move the article.
8681         ((eq action 'move)
8682          ;; Remove this article from future suppression.
8683          (gnus-dup-unsuppress-article article)
8684          (gnus-request-move-article
8685           article                       ; Article to move
8686           gnus-newsgroup-name           ; From newsgroup
8687           (nth 1 (gnus-find-method-for-group
8688                   gnus-newsgroup-name)) ; Server
8689           (list 'gnus-request-accept-article
8690                 to-newsgroup (list 'quote select-method)
8691                 (not articles) t)       ; Accept form
8692           (not articles)))              ; Only save nov last time
8693         ;; Copy the article.
8694         ((eq action 'copy)
8695          (save-excursion
8696            (set-buffer copy-buf)
8697            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8698              (gnus-request-accept-article
8699               to-newsgroup select-method (not articles) t))))
8700         ;; Crosspost the article.
8701         ((eq action 'crosspost)
8702          (let ((xref (message-tokenize-header
8703                       (mail-header-xref (gnus-summary-article-header article))
8704                       " ")))
8705            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8706                                   ":" (number-to-string article)))
8707            (unless xref
8708              (setq xref (list (system-name))))
8709            (setq new-xref
8710                  (concat
8711                   (mapconcat 'identity
8712                              (delete "Xref:" (delete new-xref xref))
8713                              " ")
8714                   " " new-xref))
8715            (save-excursion
8716              (set-buffer copy-buf)
8717              ;; First put the article in the destination group.
8718              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8719              (when (consp (setq art-group
8720                                 (gnus-request-accept-article
8721                                  to-newsgroup select-method (not articles))))
8722                (setq new-xref (concat new-xref " " (car art-group)
8723                                       ":"
8724                                       (number-to-string (cdr art-group))))
8725                ;; Now we have the new Xrefs header, so we insert
8726                ;; it and replace the new article.
8727                (nnheader-replace-header "Xref" new-xref)
8728                (gnus-request-replace-article
8729                 (cdr art-group) to-newsgroup (current-buffer))
8730                art-group))))))
8731       (cond
8732        ((not art-group)
8733         (gnus-message 1 "Couldn't %s article %s: %s"
8734                       (cadr (assq action names)) article
8735                       (nnheader-get-report (car to-method))))
8736        ((eq art-group 'junk)
8737         (when (eq action 'move)
8738           (gnus-summary-mark-article article gnus-canceled-mark)
8739           (gnus-message 4 "Deleted article %s" article)))
8740        (t
8741         (let* ((pto-group (gnus-group-prefixed-name
8742                            (car art-group) to-method))
8743                (entry
8744                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8745                (info (nth 2 entry))
8746                (to-group (gnus-info-group info))
8747                to-marks)
8748           ;; Update the group that has been moved to.
8749           (when (and info
8750                      (memq action '(move copy)))
8751             (unless (member to-group to-groups)
8752               (push to-group to-groups))
8753
8754             (unless (memq article gnus-newsgroup-unreads)
8755               (push 'read to-marks)
8756               (gnus-info-set-read
8757                info (gnus-add-to-range (gnus-info-read info)
8758                                        (list (cdr art-group)))))
8759
8760             ;; See whether the article is to be put in the cache.
8761             (let ((marks gnus-article-mark-lists)
8762                   (to-article (cdr art-group)))
8763
8764               ;; Enter the article into the cache in the new group,
8765               ;; if that is required.
8766               (when gnus-use-cache
8767                 (gnus-cache-possibly-enter-article
8768                  to-group to-article
8769                  (memq article gnus-newsgroup-marked)
8770                  (memq article gnus-newsgroup-dormant)
8771                  (memq article gnus-newsgroup-unreads)))
8772
8773               (when gnus-preserve-marks
8774                 ;; Copy any marks over to the new group.
8775                 (when (and (equal to-group gnus-newsgroup-name)
8776                            (not (memq article gnus-newsgroup-unreads)))
8777                   ;; Mark this article as read in this group.
8778                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8779                   (setcdr (gnus-active to-group) to-article)
8780                   (setcdr gnus-newsgroup-active to-article))
8781
8782                 (while marks
8783                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8784                     (when (memq article (symbol-value
8785                                          (intern (format "gnus-newsgroup-%s"
8786                                                          (caar marks)))))
8787                       (push (cdar marks) to-marks)
8788                       ;; If the other group is the same as this group,
8789                       ;; then we have to add the mark to the list.
8790                       (when (equal to-group gnus-newsgroup-name)
8791                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8792                              (cons to-article
8793                                    (symbol-value
8794                                     (intern (format "gnus-newsgroup-%s"
8795                                                     (caar marks)))))))
8796                       ;; Copy the marks to other group.
8797                       (gnus-add-marked-articles
8798                        to-group (cdar marks) (list to-article) info)))
8799                   (setq marks (cdr marks)))
8800
8801                 (gnus-request-set-mark
8802                  to-group (list (list (list to-article) 'add to-marks))))
8803
8804               (gnus-dribble-enter
8805                (concat "(gnus-group-set-info '"
8806                        (gnus-prin1-to-string (gnus-get-info to-group))
8807                        ")"))))
8808
8809           ;; Update the Xref header in this article to point to
8810           ;; the new crossposted article we have just created.
8811           (when (eq action 'crosspost)
8812             (save-excursion
8813               (set-buffer copy-buf)
8814               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8815               (nnheader-replace-header "Xref" new-xref)
8816               (gnus-request-replace-article
8817                article gnus-newsgroup-name (current-buffer)))))
8818
8819         ;;;!!!Why is this necessary?
8820         (set-buffer gnus-summary-buffer)
8821
8822         (gnus-summary-goto-subject article)
8823         (when (eq action 'move)
8824           (gnus-summary-mark-article article gnus-canceled-mark))))
8825       (gnus-summary-remove-process-mark article))
8826     ;; Re-activate all groups that have been moved to.
8827     (save-excursion
8828       (set-buffer gnus-group-buffer)
8829       (let ((gnus-group-marked to-groups))
8830         (gnus-group-get-new-news-this-group nil t)))
8831
8832     (gnus-kill-buffer copy-buf)
8833     (gnus-summary-position-point)
8834     (gnus-set-mode-line 'summary)))
8835
8836 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8837   "Move the current article to a different newsgroup.
8838 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8839 When called interactively, if TO-NEWSGROUP is nil, use the value of
8840 the variable `gnus-move-split-methods' for finding a default target
8841 newsgroup.
8842 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8843 re-spool using this method."
8844   (interactive "P")
8845   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8846
8847 (defun gnus-summary-crosspost-article (&optional n)
8848   "Crosspost the current article to some other group."
8849   (interactive "P")
8850   (gnus-summary-move-article n nil nil 'crosspost))
8851
8852 (defcustom gnus-summary-respool-default-method nil
8853   "Default method type for respooling an article.
8854 If nil, use to the current newsgroup method."
8855   :type 'symbol
8856   :group 'gnus-summary-mail)
8857
8858 (defcustom gnus-summary-display-while-building nil
8859   "If not-nil, show and update the summary buffer as it's being built.
8860 If the value is t, update the buffer after every line is inserted.  If
8861 the value is an integer (N), update the display every N lines."
8862   :group 'gnus-thread
8863   :type '(choice (const :tag "off" nil)
8864                  number
8865                  (const :tag "frequently" t)))
8866
8867 (defun gnus-summary-respool-article (&optional n method)
8868   "Respool the current article.
8869 The article will be squeezed through the mail spooling process again,
8870 which means that it will be put in some mail newsgroup or other
8871 depending on `nnmail-split-methods'.
8872 If N is a positive number, respool the N next articles.
8873 If N is a negative number, respool the N previous articles.
8874 If N is nil and any articles have been marked with the process mark,
8875 respool those articles instead.
8876
8877 Respooling can be done both from mail groups and \"real\" newsgroups.
8878 In the former case, the articles in question will be moved from the
8879 current group into whatever groups they are destined to.  In the
8880 latter case, they will be copied into the relevant groups."
8881   (interactive
8882    (list current-prefix-arg
8883          (let* ((methods (gnus-methods-using 'respool))
8884                 (methname
8885                  (symbol-name (or gnus-summary-respool-default-method
8886                                   (car (gnus-find-method-for-group
8887                                         gnus-newsgroup-name)))))
8888                 (method
8889                  (gnus-completing-read-with-default
8890                   methname "What backend do you want to use when respooling?"
8891                   methods nil t nil 'gnus-mail-method-history))
8892                 ms)
8893            (cond
8894             ((zerop (length (setq ms (gnus-servers-using-backend
8895                                       (intern method)))))
8896              (list (intern method) ""))
8897             ((= 1 (length ms))
8898              (car ms))
8899             (t
8900              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8901                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8902                            ms-alist))))))))
8903   (unless method
8904     (error "No method given for respooling"))
8905   (if (assoc (symbol-name
8906               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8907              (gnus-methods-using 'respool))
8908       (gnus-summary-move-article n nil method)
8909     (gnus-summary-copy-article n nil method)))
8910
8911 (defun gnus-summary-import-article (file &optional edit)
8912   "Import an arbitrary file into a mail newsgroup."
8913   (interactive "fImport file: \nP")
8914   (let ((group gnus-newsgroup-name)
8915         (now (current-time))
8916         atts lines group-art)
8917     (unless (gnus-check-backend-function 'request-accept-article group)
8918       (error "%s does not support article importing" group))
8919     (or (file-readable-p file)
8920         (not (file-regular-p file))
8921         (error "Can't read %s" file))
8922     (save-excursion
8923       (set-buffer (gnus-get-buffer-create " *import file*"))
8924       (erase-buffer)
8925       (nnheader-insert-file-contents file)
8926       (goto-char (point-min))
8927       (if (nnheader-article-p)
8928           (save-restriction
8929             (goto-char (point-min))
8930             (search-forward "\n\n" nil t)
8931             (narrow-to-region (point-min) (1- (point)))
8932             (goto-char (point-min))
8933             (unless (re-search-forward "^date:" nil t)
8934               (goto-char (point-max))
8935               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8936        ;; This doesn't look like an article, so we fudge some headers.
8937         (setq atts (file-attributes file)
8938               lines (count-lines (point-min) (point-max)))
8939         (insert "From: " (read-string "From: ") "\n"
8940                 "Subject: " (read-string "Subject: ") "\n"
8941                 "Date: " (message-make-date (nth 5 atts)) "\n"
8942                 "Message-ID: " (message-make-message-id) "\n"
8943                 "Lines: " (int-to-string lines) "\n"
8944                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8945       (setq group-art (gnus-request-accept-article group nil t))
8946       (kill-buffer (current-buffer)))
8947     (setq gnus-newsgroup-active (gnus-activate-group group))
8948     (forward-line 1)
8949     (gnus-summary-goto-article (cdr group-art) nil t)
8950     (when edit
8951       (gnus-summary-edit-article))))
8952
8953 (defun gnus-summary-create-article ()
8954   "Create an article in a mail newsgroup."
8955   (interactive)
8956   (let ((group gnus-newsgroup-name)
8957         (now (current-time))
8958         group-art)
8959     (unless (gnus-check-backend-function 'request-accept-article group)
8960       (error "%s does not support article importing" group))
8961     (save-excursion
8962       (set-buffer (gnus-get-buffer-create " *import file*"))
8963       (erase-buffer)
8964       (goto-char (point-min))
8965       ;; This doesn't look like an article, so we fudge some headers.
8966       (insert "From: " (read-string "From: ") "\n"
8967               "Subject: " (read-string "Subject: ") "\n"
8968               "Date: " (message-make-date now) "\n"
8969               "Message-ID: " (message-make-message-id) "\n")
8970       (setq group-art (gnus-request-accept-article group nil t))
8971       (kill-buffer (current-buffer)))
8972     (setq gnus-newsgroup-active (gnus-activate-group group))
8973     (forward-line 1)
8974     (gnus-summary-goto-article (cdr group-art) nil t)
8975     (gnus-summary-edit-article)))
8976
8977 (defun gnus-summary-article-posted-p ()
8978   "Say whether the current (mail) article is available from news as well.
8979 This will be the case if the article has both been mailed and posted."
8980   (interactive)
8981   (let ((id (mail-header-references (gnus-summary-article-header)))
8982         (gnus-override-method (car (gnus-refer-article-methods))))
8983     (if (gnus-request-head id "")
8984         (gnus-message 2 "The current message was found on %s"
8985                       gnus-override-method)
8986       (gnus-message 2 "The current message couldn't be found on %s"
8987                     gnus-override-method)
8988       nil)))
8989
8990 (defun gnus-summary-expire-articles (&optional now)
8991   "Expire all articles that are marked as expirable in the current group."
8992   (interactive)
8993   (when (gnus-check-backend-function
8994          'request-expire-articles gnus-newsgroup-name)
8995     ;; This backend supports expiry.
8996     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8997            (expirable (if total
8998                           (progn
8999                             ;; We need to update the info for
9000                             ;; this group for `gnus-list-of-read-articles'
9001                             ;; to give us the right answer.
9002                             (gnus-run-hooks 'gnus-exit-group-hook)
9003                             (gnus-summary-update-info)
9004                             (gnus-list-of-read-articles gnus-newsgroup-name))
9005                         (setq gnus-newsgroup-expirable
9006                               (sort gnus-newsgroup-expirable '<))))
9007            (expiry-wait (if now 'immediate
9008                           (gnus-group-find-parameter
9009                            gnus-newsgroup-name 'expiry-wait)))
9010            (nnmail-expiry-target
9011             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9012                 nnmail-expiry-target))
9013            es)
9014       (when expirable
9015         ;; There are expirable articles in this group, so we run them
9016         ;; through the expiry process.
9017         (gnus-message 6 "Expiring articles...")
9018         (unless (gnus-check-group gnus-newsgroup-name)
9019           (error "Can't open server for %s" gnus-newsgroup-name))
9020         ;; The list of articles that weren't expired is returned.
9021         (save-excursion
9022           (if expiry-wait
9023               (let ((nnmail-expiry-wait-function nil)
9024                     (nnmail-expiry-wait expiry-wait))
9025                 (setq es (gnus-request-expire-articles
9026                           expirable gnus-newsgroup-name)))
9027             (setq es (gnus-request-expire-articles
9028                       expirable gnus-newsgroup-name)))
9029           (unless total
9030             (setq gnus-newsgroup-expirable es))
9031           ;; We go through the old list of expirable, and mark all
9032           ;; really expired articles as nonexistent.
9033           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9034             (let ((gnus-use-cache nil))
9035               (dolist (article expirable)
9036                 (when (and (not (memq article es))
9037                            (gnus-data-find article))
9038                   (gnus-summary-mark-article article gnus-canceled-mark))))))
9039         (gnus-message 6 "Expiring articles...done")))))
9040
9041 (defun gnus-summary-expire-articles-now ()
9042   "Expunge all expirable articles in the current group.
9043 This means that *all* articles that are marked as expirable will be
9044 deleted forever, right now."
9045   (interactive)
9046   (or gnus-expert-user
9047       (gnus-yes-or-no-p
9048        "Are you really, really, really sure you want to delete all these messages? ")
9049       (error "Phew!"))
9050   (gnus-summary-expire-articles t))
9051
9052 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9053 (defun gnus-summary-delete-article (&optional n)
9054   "Delete the N next (mail) articles.
9055 This command actually deletes articles.  This is not a marking
9056 command.  The article will disappear forever from your life, never to
9057 return.
9058 If N is negative, delete backwards.
9059 If N is nil and articles have been marked with the process mark,
9060 delete these instead."
9061   (interactive "P")
9062   (unless (gnus-check-backend-function 'request-expire-articles
9063                                        gnus-newsgroup-name)
9064     (error "The current newsgroup does not support article deletion"))
9065   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9066     (error "Couldn't open server"))
9067   ;; Compute the list of articles to delete.
9068   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9069         (nnmail-expiry-target 'delete)
9070         not-deleted)
9071     (if (and gnus-novice-user
9072              (not (gnus-yes-or-no-p
9073                    (format "Do you really want to delete %s forever? "
9074                            (if (> (length articles) 1)
9075                                (format "these %s articles" (length articles))
9076                              "this article")))))
9077         ()
9078       ;; Delete the articles.
9079       (setq not-deleted (gnus-request-expire-articles
9080                          articles gnus-newsgroup-name 'force))
9081       (while articles
9082         (gnus-summary-remove-process-mark (car articles))
9083         ;; The backend might not have been able to delete the article
9084         ;; after all.
9085         (unless (memq (car articles) not-deleted)
9086           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9087         (setq articles (cdr articles)))
9088       (when not-deleted
9089         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9090     (gnus-summary-position-point)
9091     (gnus-set-mode-line 'summary)
9092     not-deleted))
9093
9094 (defun gnus-summary-edit-article (&optional arg)
9095   "Edit the current article.
9096 This will have permanent effect only in mail groups.
9097 If ARG is nil, edit the decoded articles.
9098 If ARG is 1, edit the raw articles.
9099 If ARG is 2, edit the raw articles even in read-only groups.
9100 If ARG is 3, edit the articles with the current handles.
9101 Otherwise, allow editing of articles even in read-only
9102 groups."
9103   (interactive "P")
9104   (let (force raw current-handles)
9105     (cond
9106      ((null arg))
9107      ((eq arg 1)
9108       (setq raw t))
9109      ((eq arg 2)
9110       (setq raw t
9111             force t))
9112      ((eq arg 3)
9113       (setq current-handles
9114             (and (gnus-buffer-live-p gnus-article-buffer)
9115                  (with-current-buffer gnus-article-buffer
9116                    (prog1
9117                        gnus-article-mime-handles
9118                      (setq gnus-article-mime-handles nil))))))
9119      (t
9120       (setq force t)))
9121     (when (and raw (not force)
9122                (member gnus-newsgroup-name '("nndraft:delayed"
9123                                              "nndraft:drafts"
9124                                              "nndraft:queue")))
9125       (error "Can't edit the raw article in group %s"
9126              gnus-newsgroup-name))
9127     (save-excursion
9128       (set-buffer gnus-summary-buffer)
9129       (let ((mail-parse-charset gnus-newsgroup-charset)
9130             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9131         (gnus-set-global-variables)
9132         (when (and (not force)
9133                    (gnus-group-read-only-p))
9134           (error "The current newsgroup does not support article editing"))
9135         (gnus-summary-show-article t)
9136         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9137           (with-current-buffer gnus-article-buffer
9138             (mm-enable-multibyte)))
9139         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9140             (setq raw t))
9141         (gnus-article-edit-article
9142          (if raw 'ignore
9143            `(lambda ()
9144               (let ((mbl mml-buffer-list))
9145                 (setq mml-buffer-list nil)
9146                 (mime-to-mml ,'current-handles)
9147                 (let ((mbl1 mml-buffer-list))
9148                   (setq mml-buffer-list mbl)
9149                   (set (make-local-variable 'mml-buffer-list) mbl1))
9150                 (make-local-hook 'kill-buffer-hook)
9151                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9152          `(lambda (no-highlight)
9153             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9154                   (message-options message-options)
9155                   (message-options-set-recipient)
9156                   (mail-parse-ignored-charsets
9157                    ',gnus-newsgroup-ignored-charsets))
9158               ,(if (not raw) '(progn
9159                                 (mml-to-mime)
9160                                 (mml-destroy-buffers)
9161                                 (remove-hook 'kill-buffer-hook
9162                                              'mml-destroy-buffers t)
9163                                 (kill-local-variable 'mml-buffer-list)))
9164               (gnus-summary-edit-article-done
9165                ,(or (mail-header-references gnus-current-headers) "")
9166                ,(gnus-group-read-only-p)
9167                ,gnus-summary-buffer no-highlight))))))))
9168
9169 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9170
9171 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9172                                                  no-highlight)
9173   "Make edits to the current article permanent."
9174   (interactive)
9175   (save-excursion
9176    ;; The buffer restriction contains the entire article if it exists.
9177     (when (article-goto-body)
9178       (let ((lines (count-lines (point) (point-max)))
9179             (length (- (point-max) (point)))
9180             (case-fold-search t)
9181             (body (copy-marker (point))))
9182         (goto-char (point-min))
9183         (when (re-search-forward "^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
9188                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9189           (delete-region (match-beginning 1) (match-end 1))
9190           (insert (number-to-string length)))
9191         (goto-char (point-min))
9192         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9193           (delete-region (match-beginning 1) (match-end 1))
9194           (insert (number-to-string lines))))))
9195   ;; Replace the article.
9196   (let ((buf (current-buffer)))
9197     (with-temp-buffer
9198       (insert-buffer-substring buf)
9199
9200       (if (and (not read-only)
9201                (not (gnus-request-replace-article
9202                      (cdr gnus-article-current) (car gnus-article-current)
9203                      (current-buffer) t)))
9204           (error "Couldn't replace article")
9205         ;; Update the summary buffer.
9206         (if (and references
9207                  (equal (message-tokenize-header references " ")
9208                         (message-tokenize-header
9209                          (or (message-fetch-field "references") "") " ")))
9210             ;; We only have to update this line.
9211             (save-excursion
9212               (save-restriction
9213                 (message-narrow-to-head)
9214                 (let ((head (buffer-string))
9215                       header)
9216                   (with-temp-buffer
9217                     (insert (format "211 %d Article retrieved.\n"
9218                                     (cdr gnus-article-current)))
9219                     (insert head)
9220                     (insert ".\n")
9221                     (let ((nntp-server-buffer (current-buffer)))
9222                       (setq header (car (gnus-get-newsgroup-headers
9223                                          nil t))))
9224                     (save-excursion
9225                       (set-buffer gnus-summary-buffer)
9226                       (gnus-data-set-header
9227                        (gnus-data-find (cdr gnus-article-current))
9228                        header)
9229                       (gnus-summary-update-article-line
9230                        (cdr gnus-article-current) header)
9231                       (if (gnus-summary-goto-subject
9232                            (cdr gnus-article-current) nil t)
9233                           (gnus-summary-update-secondary-mark
9234                            (cdr gnus-article-current))))))))
9235           ;; Update threads.
9236           (set-buffer (or buffer gnus-summary-buffer))
9237           (gnus-summary-update-article (cdr gnus-article-current))
9238           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9239               (gnus-summary-update-secondary-mark
9240                (cdr gnus-article-current))))
9241         ;; Prettify the article buffer again.
9242         (unless no-highlight
9243           (save-excursion
9244             (set-buffer gnus-article-buffer)
9245             ;;;!!! Fix this -- article should be rehighlighted.
9246             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9247             (set-buffer gnus-original-article-buffer)
9248             (gnus-request-article
9249              (cdr gnus-article-current)
9250              (car gnus-article-current) (current-buffer))))
9251         ;; Prettify the summary buffer line.
9252         (when (gnus-visual-p 'summary-highlight 'highlight)
9253           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9254
9255 (defun gnus-summary-edit-wash (key)
9256   "Perform editing command KEY in the article buffer."
9257   (interactive
9258    (list
9259     (progn
9260       (message "%s" (concat (this-command-keys) "- "))
9261       (read-char))))
9262   (message "")
9263   (gnus-summary-edit-article)
9264   (execute-kbd-macro (concat (this-command-keys) key))
9265   (gnus-article-edit-done))
9266
9267 ;;; Respooling
9268
9269 (defun gnus-summary-respool-query (&optional silent trace)
9270   "Query where the respool algorithm would put this article."
9271   (interactive)
9272   (let (gnus-mark-article-hook)
9273     (gnus-summary-select-article)
9274     (save-excursion
9275       (set-buffer gnus-original-article-buffer)
9276       (save-restriction
9277         (message-narrow-to-head)
9278         (let ((groups (nnmail-article-group 'identity trace)))
9279           (unless silent
9280             (if groups
9281                 (message "This message would go to %s"
9282                          (mapconcat 'car groups ", "))
9283               (message "This message would go to no groups"))
9284             groups))))))
9285
9286 (defun gnus-summary-respool-trace ()
9287   "Trace where the respool algorithm would put this article.
9288 Display a buffer showing all fancy splitting patterns which matched."
9289   (interactive)
9290   (gnus-summary-respool-query nil t))
9291
9292 ;; Summary marking commands.
9293
9294 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9295   "Mark articles which has the same subject as read, and then select the next.
9296 If UNMARK is positive, remove any kind of mark.
9297 If UNMARK is negative, tick articles."
9298   (interactive "P")
9299   (when unmark
9300     (setq unmark (prefix-numeric-value unmark)))
9301   (let ((count
9302          (gnus-summary-mark-same-subject
9303           (gnus-summary-article-subject) unmark)))
9304     ;; Select next unread article.  If auto-select-same mode, should
9305     ;; select the first unread article.
9306     (gnus-summary-next-article t (and gnus-auto-select-same
9307                                       (gnus-summary-article-subject)))
9308     (gnus-message 7 "%d article%s marked as %s"
9309                   count (if (= count 1) " is" "s are")
9310                   (if unmark "unread" "read"))))
9311
9312 (defun gnus-summary-kill-same-subject (&optional unmark)
9313   "Mark articles which has the same subject as read.
9314 If UNMARK is positive, remove any kind of mark.
9315 If UNMARK is negative, tick articles."
9316   (interactive "P")
9317   (when unmark
9318     (setq unmark (prefix-numeric-value unmark)))
9319   (let ((count
9320          (gnus-summary-mark-same-subject
9321           (gnus-summary-article-subject) unmark)))
9322     ;; If marked as read, go to next unread subject.
9323     (when (null unmark)
9324       ;; Go to next unread subject.
9325       (gnus-summary-next-subject 1 t))
9326     (gnus-message 7 "%d articles are marked as %s"
9327                   count (if unmark "unread" "read"))))
9328
9329 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9330   "Mark articles with same SUBJECT as read, and return marked number.
9331 If optional argument UNMARK is positive, remove any kinds of marks.
9332 If optional argument UNMARK is negative, mark articles as unread instead."
9333   (let ((count 1))
9334     (save-excursion
9335       (cond
9336        ((null unmark)                   ; Mark as read.
9337         (while (and
9338                 (progn
9339                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9340                   (gnus-summary-show-thread) t)
9341                 (gnus-summary-find-subject subject))
9342           (setq count (1+ count))))
9343        ((> unmark 0)                    ; Tick.
9344         (while (and
9345                 (progn
9346                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9347                   (gnus-summary-show-thread) t)
9348                 (gnus-summary-find-subject subject))
9349           (setq count (1+ count))))
9350        (t                               ; Mark as unread.
9351         (while (and
9352                 (progn
9353                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9354                   (gnus-summary-show-thread) t)
9355                 (gnus-summary-find-subject subject))
9356           (setq count (1+ count)))))
9357       (gnus-set-mode-line 'summary)
9358       ;; Return the number of marked articles.
9359       count)))
9360
9361 (defun gnus-summary-mark-as-processable (n &optional unmark)
9362   "Set the process mark on the next N articles.
9363 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9364 the process mark instead.  The difference between N and the actual
9365 number of articles marked is returned."
9366   (interactive "P")
9367   (if (and (null n) (gnus-region-active-p))
9368       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9369     (setq n (prefix-numeric-value n))
9370     (let ((backward (< n 0))
9371           (n (abs n)))
9372       (while (and
9373               (> n 0)
9374               (if unmark
9375                   (gnus-summary-remove-process-mark
9376                    (gnus-summary-article-number))
9377                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9378               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9379         (setq n (1- n)))
9380       (when (/= 0 n)
9381         (gnus-message 7 "No more articles"))
9382       (gnus-summary-recenter)
9383       (gnus-summary-position-point)
9384       n)))
9385
9386 (defun gnus-summary-unmark-as-processable (n)
9387   "Remove the process mark from the next N articles.
9388 If N is negative, unmark backward instead.  The difference between N and
9389 the actual number of articles unmarked is returned."
9390   (interactive "P")
9391   (gnus-summary-mark-as-processable n t))
9392
9393 (defun gnus-summary-unmark-all-processable ()
9394   "Remove the process mark from all articles."
9395   (interactive)
9396   (save-excursion
9397     (while gnus-newsgroup-processable
9398       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9399   (gnus-summary-position-point))
9400
9401 (defun gnus-summary-add-mark (article type)
9402   "Mark ARTICLE with a mark of TYPE."
9403   (let ((vtype (car (assq type gnus-article-mark-lists)))
9404         var)
9405     (if (not vtype)
9406         (error "No such mark type: %s" type)
9407       (setq var (intern (format "gnus-newsgroup-%s" type)))
9408       (set var (cons article (symbol-value var)))
9409       (if (memq type '(processable cached replied forwarded recent saved))
9410           (gnus-summary-update-secondary-mark article)
9411         ;;; !!! This is bogus.  We should find out what primary
9412         ;;; !!! mark we want to set.
9413         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9414
9415 (defun gnus-summary-mark-as-expirable (n)
9416   "Mark N articles forward as expirable.
9417 If N is negative, mark backward instead.  The difference between N and
9418 the actual number of articles marked is returned."
9419   (interactive "p")
9420   (gnus-summary-mark-forward n gnus-expirable-mark))
9421
9422 (defun gnus-summary-mark-as-spam (n)
9423   "Mark N articles forward as spam.
9424 If N is negative, mark backward instead.  The difference between N and
9425 the actual number of articles marked is returned."
9426   (interactive "p")
9427   (gnus-summary-mark-forward n gnus-spam-mark))
9428
9429 (defun gnus-summary-mark-article-as-replied (article)
9430   "Mark ARTICLE as replied to and update the summary line.
9431 ARTICLE can also be a list of articles."
9432   (interactive (list (gnus-summary-article-number)))
9433   (let ((articles (if (listp article) article (list article))))
9434     (dolist (article articles)
9435       (unless (numberp article)
9436         (error "%s is not a number" article))
9437       (push article gnus-newsgroup-replied)
9438       (let ((buffer-read-only nil))
9439         (when (gnus-summary-goto-subject article nil t)
9440           (gnus-summary-update-secondary-mark article))))))
9441
9442 (defun gnus-summary-mark-article-as-forwarded (article)
9443   "Mark ARTICLE as forwarded and update the summary line.
9444 ARTICLE can also be a list of articles."
9445   (let ((articles (if (listp article) article (list article))))
9446     (dolist (article articles)
9447       (push article gnus-newsgroup-forwarded)
9448       (let ((buffer-read-only nil))
9449         (when (gnus-summary-goto-subject article nil t)
9450           (gnus-summary-update-secondary-mark article))))))
9451
9452 (defun gnus-summary-set-bookmark (article)
9453   "Set a bookmark in current article."
9454   (interactive (list (gnus-summary-article-number)))
9455   (when (or (not (get-buffer gnus-article-buffer))
9456             (not gnus-current-article)
9457             (not gnus-article-current)
9458             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9459     (error "No current article selected"))
9460   ;; Remove old bookmark, if one exists.
9461   (gnus-pull article gnus-newsgroup-bookmarks)
9462   ;; Set the new bookmark, which is on the form
9463   ;; (article-number . line-number-in-body).
9464   (push
9465    (cons article
9466          (save-excursion
9467            (set-buffer gnus-article-buffer)
9468            (count-lines
9469             (min (point)
9470                  (save-excursion
9471                    (article-goto-body)
9472                    (point)))
9473             (point))))
9474    gnus-newsgroup-bookmarks)
9475   (gnus-message 6 "A bookmark has been added to the current article."))
9476
9477 (defun gnus-summary-remove-bookmark (article)
9478   "Remove the bookmark from the current article."
9479   (interactive (list (gnus-summary-article-number)))
9480   ;; Remove old bookmark, if one exists.
9481   (if (not (assq article gnus-newsgroup-bookmarks))
9482       (gnus-message 6 "No bookmark in current article.")
9483     (gnus-pull article gnus-newsgroup-bookmarks)
9484     (gnus-message 6 "Removed bookmark.")))
9485
9486 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9487 (defun gnus-summary-mark-as-dormant (n)
9488   "Mark N articles forward as dormant.
9489 If N is negative, mark backward instead.  The difference between N and
9490 the actual number of articles marked is returned."
9491   (interactive "p")
9492   (gnus-summary-mark-forward n gnus-dormant-mark))
9493
9494 (defun gnus-summary-set-process-mark (article)
9495   "Set the process mark on ARTICLE and update the summary line."
9496   (setq gnus-newsgroup-processable
9497         (cons article
9498               (delq article gnus-newsgroup-processable)))
9499   (when (gnus-summary-goto-subject article)
9500     (gnus-summary-show-thread)
9501     (gnus-summary-goto-subject article)
9502     (gnus-summary-update-secondary-mark article)))
9503
9504 (defun gnus-summary-remove-process-mark (article)
9505   "Remove the process mark from ARTICLE and update the summary line."
9506   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9507   (when (gnus-summary-goto-subject article)
9508     (gnus-summary-show-thread)
9509     (gnus-summary-goto-subject article)
9510     (gnus-summary-update-secondary-mark article)))
9511
9512 (defun gnus-summary-set-saved-mark (article)
9513   "Set the process mark on ARTICLE and update the summary line."
9514   (push article gnus-newsgroup-saved)
9515   (when (gnus-summary-goto-subject article)
9516     (gnus-summary-update-secondary-mark article)))
9517
9518 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9519   "Mark N articles as read forwards.
9520 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9521 The difference between N and the actual number of articles marked is
9522 returned.
9523 If NO-EXPIRE, auto-expiry will be inhibited."
9524   (interactive "p")
9525   (gnus-summary-show-thread)
9526   (let ((backward (< n 0))
9527         (gnus-summary-goto-unread
9528          (and gnus-summary-goto-unread
9529               (not (eq gnus-summary-goto-unread 'never))
9530               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9531                                     gnus-ticked-mark gnus-dormant-mark)))))
9532         (n (abs n))
9533         (mark (or mark gnus-del-mark)))
9534     (while (and (> n 0)
9535                 (gnus-summary-mark-article nil mark no-expire)
9536                 (zerop (gnus-summary-next-subject
9537                         (if backward -1 1)
9538                         (and gnus-summary-goto-unread
9539                              (not (eq gnus-summary-goto-unread 'never)))
9540                         t)))
9541       (setq n (1- n)))
9542     (when (/= 0 n)
9543       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9544     (gnus-summary-recenter)
9545     (gnus-summary-position-point)
9546     (gnus-set-mode-line 'summary)
9547     n))
9548
9549 (defun gnus-summary-mark-article-as-read (mark)
9550   "Mark the current article quickly as read with MARK."
9551   (let ((article (gnus-summary-article-number)))
9552     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9553     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9554     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9555     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9556     (push (cons article mark) gnus-newsgroup-reads)
9557     ;; Possibly remove from cache, if that is used.
9558     (when gnus-use-cache
9559       (gnus-cache-enter-remove-article article))
9560     ;; Allow the backend to change the mark.
9561     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9562     ;; Check for auto-expiry.
9563     (when (and gnus-newsgroup-auto-expire
9564                (memq mark gnus-auto-expirable-marks))
9565       (setq mark gnus-expirable-mark)
9566       ;; Let the backend know about the mark change.
9567       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9568       (push article gnus-newsgroup-expirable))
9569     ;; Set the mark in the buffer.
9570     (gnus-summary-update-mark mark 'unread)
9571     t))
9572
9573 (defun gnus-summary-mark-article-as-unread (mark)
9574   "Mark the current article quickly as unread with MARK."
9575   (let* ((article (gnus-summary-article-number))
9576          (old-mark (gnus-summary-article-mark article)))
9577     ;; Allow the backend to change the mark.
9578     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9579     (if (eq mark old-mark)
9580         t
9581       (if (<= article 0)
9582           (progn
9583             (gnus-error 1 "Can't mark negative article numbers")
9584             nil)
9585         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9586         (setq gnus-newsgroup-spam-marked
9587               (delq article gnus-newsgroup-spam-marked))
9588         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9589         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9590         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9591         (cond ((= mark gnus-ticked-mark)
9592                (setq gnus-newsgroup-marked
9593                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9594                                               article)))
9595               ((= mark gnus-spam-mark)
9596                (setq gnus-newsgroup-spam-marked
9597                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9598                                               article)))
9599               ((= mark gnus-dormant-mark)
9600                (setq gnus-newsgroup-dormant
9601                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9602                                               article)))
9603               (t
9604                (setq gnus-newsgroup-unreads
9605                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9606                                               article))))
9607         (gnus-pull article gnus-newsgroup-reads)
9608
9609         ;; See whether the article is to be put in the cache.
9610         (and gnus-use-cache
9611              (vectorp (gnus-summary-article-header article))
9612              (save-excursion
9613                (gnus-cache-possibly-enter-article
9614                 gnus-newsgroup-name article
9615                 (= mark gnus-ticked-mark)
9616                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9617
9618         ;; Fix the mark.
9619         (gnus-summary-update-mark mark 'unread)
9620         t))))
9621
9622 (defun gnus-summary-mark-article (&optional article mark no-expire)
9623   "Mark ARTICLE with MARK.  MARK can be any character.
9624 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9625 `??' (dormant) and `?E' (expirable).
9626 If MARK is nil, then the default character `?r' is used.
9627 If ARTICLE is nil, then the article on the current line will be
9628 marked.
9629 If NO-EXPIRE, auto-expiry will be inhibited."
9630   ;; The mark might be a string.
9631   (when (stringp mark)
9632     (setq mark (aref mark 0)))
9633   ;; If no mark is given, then we check auto-expiring.
9634   (when (null mark)
9635     (setq mark gnus-del-mark))
9636   (when (and (not no-expire)
9637              gnus-newsgroup-auto-expire
9638              (memq mark gnus-auto-expirable-marks))
9639     (setq mark gnus-expirable-mark))
9640   (let ((article (or article (gnus-summary-article-number)))
9641         (old-mark (gnus-summary-article-mark article)))
9642     ;; Allow the backend to change the mark.
9643     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9644     (if (eq mark old-mark)
9645         t
9646       (unless article
9647         (error "No article on current line"))
9648       (if (not (if (or (= mark gnus-unread-mark)
9649                        (= mark gnus-ticked-mark)
9650                        (= mark gnus-spam-mark)
9651                        (= mark gnus-dormant-mark))
9652                    (gnus-mark-article-as-unread article mark)
9653                  (gnus-mark-article-as-read article mark)))
9654           t
9655         ;; See whether the article is to be put in the cache.
9656         (and gnus-use-cache
9657              (not (= mark gnus-canceled-mark))
9658              (vectorp (gnus-summary-article-header article))
9659              (save-excursion
9660                (gnus-cache-possibly-enter-article
9661                 gnus-newsgroup-name article
9662                 (= mark gnus-ticked-mark)
9663                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9664
9665         (when (gnus-summary-goto-subject article nil t)
9666           (let ((buffer-read-only nil))
9667             (gnus-summary-show-thread)
9668             ;; Fix the mark.
9669             (gnus-summary-update-mark mark 'unread)
9670             t))))))
9671
9672 (defun gnus-summary-update-secondary-mark (article)
9673   "Update the secondary (read, process, cache) mark."
9674   (gnus-summary-update-mark
9675    (cond ((memq article gnus-newsgroup-processable)
9676           gnus-process-mark)
9677          ((memq article gnus-newsgroup-cached)
9678           gnus-cached-mark)
9679          ((memq article gnus-newsgroup-replied)
9680           gnus-replied-mark)
9681          ((memq article gnus-newsgroup-forwarded)
9682           gnus-forwarded-mark)
9683          ((memq article gnus-newsgroup-saved)
9684           gnus-saved-mark)
9685          ((memq article gnus-newsgroup-recent)
9686           gnus-recent-mark)
9687          ((memq article gnus-newsgroup-unseen)
9688           gnus-unseen-mark)
9689          (t gnus-no-mark))
9690    'replied)
9691   (when (gnus-visual-p 'summary-highlight 'highlight)
9692     (gnus-run-hooks 'gnus-summary-update-hook))
9693   t)
9694
9695 (defun gnus-summary-update-download-mark (article)
9696   "Update the secondary (read, process, cache) mark."
9697   (gnus-summary-update-mark
9698    (cond ((memq article gnus-newsgroup-undownloaded) 
9699           gnus-undownloaded-mark)
9700          (gnus-newsgroup-agentized
9701           gnus-downloaded-mark)
9702          (t
9703           gnus-no-mark))
9704    'download)
9705   (gnus-summary-update-line t)
9706   t)
9707
9708 (defun gnus-summary-update-mark (mark type)
9709   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9710         (buffer-read-only nil))
9711     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9712     (when forward
9713       (when (looking-at "\r")
9714         (incf forward))
9715       (when (<= (+ forward (point)) (point-max))
9716         ;; Go to the right position on the line.
9717         (goto-char (+ forward (point)))
9718         ;; Replace the old mark with the new mark.
9719         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9720         ;; Optionally update the marks by some user rule.
9721         (when (eq type 'unread)
9722           (gnus-data-set-mark
9723            (gnus-data-find (gnus-summary-article-number)) mark)
9724           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9725
9726 (defun gnus-mark-article-as-read (article &optional mark)
9727   "Enter ARTICLE in the pertinent lists and remove it from others."
9728   ;; Make the article expirable.
9729   (let ((mark (or mark gnus-del-mark)))
9730     (setq gnus-newsgroup-expirable
9731           (if (= mark gnus-expirable-mark)
9732               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9733             (delq article gnus-newsgroup-expirable)))
9734     ;; Remove from unread and marked lists.
9735     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9736     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9737     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9738     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9739     (push (cons article mark) gnus-newsgroup-reads)
9740     ;; Possibly remove from cache, if that is used.
9741     (when gnus-use-cache
9742       (gnus-cache-enter-remove-article article))
9743     t))
9744
9745 (defun gnus-mark-article-as-unread (article &optional mark)
9746   "Enter ARTICLE in the pertinent lists and remove it from others."
9747   (let ((mark (or mark gnus-ticked-mark)))
9748     (if (<= article 0)
9749         (progn
9750           (gnus-error 1 "Can't mark negative article numbers")
9751           nil)
9752       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9753             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9754             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9755             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9756             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9757
9758       ;; Unsuppress duplicates?
9759       (when gnus-suppress-duplicates
9760         (gnus-dup-unsuppress-article article))
9761
9762       (cond ((= mark gnus-ticked-mark)
9763              (setq gnus-newsgroup-marked
9764                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9765             ((= mark gnus-spam-mark)
9766              (setq gnus-newsgroup-spam-marked
9767                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9768                                             article)))
9769             ((= mark gnus-dormant-mark)
9770              (setq gnus-newsgroup-dormant
9771                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9772             (t
9773              (setq gnus-newsgroup-unreads
9774                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9775       (gnus-pull article gnus-newsgroup-reads)
9776       t)))
9777
9778 (defalias 'gnus-summary-mark-as-unread-forward
9779   'gnus-summary-tick-article-forward)
9780 (make-obsolete 'gnus-summary-mark-as-unread-forward
9781                'gnus-summary-tick-article-forward)
9782 (defun gnus-summary-tick-article-forward (n)
9783   "Tick N articles forwards.
9784 If N is negative, tick backwards instead.
9785 The difference between N and the number of articles ticked is returned."
9786   (interactive "p")
9787   (gnus-summary-mark-forward n gnus-ticked-mark))
9788
9789 (defalias 'gnus-summary-mark-as-unread-backward
9790   'gnus-summary-tick-article-backward)
9791 (make-obsolete 'gnus-summary-mark-as-unread-backward
9792                'gnus-summary-tick-article-backward)
9793 (defun gnus-summary-tick-article-backward (n)
9794   "Tick N articles backwards.
9795 The difference between N and the number of articles ticked is returned."
9796   (interactive "p")
9797   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9798
9799 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9800 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9801 (defun gnus-summary-tick-article (&optional article clear-mark)
9802   "Mark current article as unread.
9803 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9804 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9805   (interactive)
9806   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9807                                        gnus-ticked-mark)))
9808
9809 (defun gnus-summary-mark-as-read-forward (n)
9810   "Mark N articles as read forwards.
9811 If N is negative, mark backwards instead.
9812 The difference between N and the actual number of articles marked is
9813 returned."
9814   (interactive "p")
9815   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9816
9817 (defun gnus-summary-mark-as-read-backward (n)
9818   "Mark the N articles as read backwards.
9819 The difference between N and the actual number of articles marked is
9820 returned."
9821   (interactive "p")
9822   (gnus-summary-mark-forward
9823    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9824
9825 (defun gnus-summary-mark-as-read (&optional article mark)
9826   "Mark current article as read.
9827 ARTICLE specifies the article to be marked as read.
9828 MARK specifies a string to be inserted at the beginning of the line."
9829   (gnus-summary-mark-article article mark))
9830
9831 (defun gnus-summary-clear-mark-forward (n)
9832   "Clear marks from N articles forward.
9833 If N is negative, clear backward instead.
9834 The difference between N and the number of marks cleared is returned."
9835   (interactive "p")
9836   (gnus-summary-mark-forward n gnus-unread-mark))
9837
9838 (defun gnus-summary-clear-mark-backward (n)
9839   "Clear marks from N articles backward.
9840 The difference between N and the number of marks cleared is returned."
9841   (interactive "p")
9842   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9843
9844 (defun gnus-summary-mark-unread-as-read ()
9845   "Intended to be used by `gnus-summary-mark-article-hook'."
9846   (when (memq gnus-current-article gnus-newsgroup-unreads)
9847     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9848
9849 (defun gnus-summary-mark-read-and-unread-as-read ()
9850   "Intended to be used by `gnus-summary-mark-article-hook'."
9851   (let ((mark (gnus-summary-article-mark)))
9852     (when (or (gnus-unread-mark-p mark)
9853               (gnus-read-mark-p mark))
9854       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9855
9856 (defun gnus-summary-mark-unread-as-ticked ()
9857   "Intended to be used by `gnus-summary-mark-article-hook'."
9858   (when (memq gnus-current-article gnus-newsgroup-unreads)
9859     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9860
9861 (defun gnus-summary-mark-region-as-read (point mark all)
9862   "Mark all unread articles between point and mark as read.
9863 If given a prefix, mark all articles between point and mark as read,
9864 even ticked and dormant ones."
9865   (interactive "r\nP")
9866   (save-excursion
9867     (let (article)
9868       (goto-char point)
9869       (beginning-of-line)
9870       (while (and
9871               (< (point) mark)
9872               (progn
9873                 (when (or all
9874                           (memq (setq article (gnus-summary-article-number))
9875                                 gnus-newsgroup-unreads))
9876                   (gnus-summary-mark-article article gnus-del-mark))
9877                 t)
9878               (gnus-summary-find-next))))))
9879
9880 (defun gnus-summary-mark-below (score mark)
9881   "Mark articles with score less than SCORE with MARK."
9882   (interactive "P\ncMark: ")
9883   (setq score (if score
9884                   (prefix-numeric-value score)
9885                 (or gnus-summary-default-score 0)))
9886   (save-excursion
9887     (set-buffer gnus-summary-buffer)
9888     (goto-char (point-min))
9889     (while
9890         (progn
9891           (and (< (gnus-summary-article-score) score)
9892                (gnus-summary-mark-article nil mark))
9893           (gnus-summary-find-next)))))
9894
9895 (defun gnus-summary-kill-below (&optional score)
9896   "Mark articles with score below SCORE as read."
9897   (interactive "P")
9898   (gnus-summary-mark-below score gnus-killed-mark))
9899
9900 (defun gnus-summary-clear-above (&optional score)
9901   "Clear all marks from articles with score above SCORE."
9902   (interactive "P")
9903   (gnus-summary-mark-above score gnus-unread-mark))
9904
9905 (defun gnus-summary-tick-above (&optional score)
9906   "Tick all articles with score above SCORE."
9907   (interactive "P")
9908   (gnus-summary-mark-above score gnus-ticked-mark))
9909
9910 (defun gnus-summary-mark-above (score mark)
9911   "Mark articles with score over SCORE with MARK."
9912   (interactive "P\ncMark: ")
9913   (setq score (if score
9914                   (prefix-numeric-value score)
9915                 (or gnus-summary-default-score 0)))
9916   (save-excursion
9917     (set-buffer gnus-summary-buffer)
9918     (goto-char (point-min))
9919     (while (and (progn
9920                   (when (> (gnus-summary-article-score) score)
9921                     (gnus-summary-mark-article nil mark))
9922                   t)
9923                 (gnus-summary-find-next)))))
9924
9925 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9926 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9927 (defun gnus-summary-limit-include-expunged (&optional no-error)
9928   "Display all the hidden articles that were expunged for low scores."
9929   (interactive)
9930   (let ((buffer-read-only nil))
9931     (let ((scored gnus-newsgroup-scored)
9932           headers h)
9933       (while scored
9934         (unless (gnus-summary-article-header (caar scored))
9935           (and (setq h (gnus-number-to-header (caar scored)))
9936                (< (cdar scored) gnus-summary-expunge-below)
9937                (push h headers)))
9938         (setq scored (cdr scored)))
9939       (if (not headers)
9940           (when (not no-error)
9941             (error "No expunged articles hidden"))
9942         (goto-char (point-min))
9943         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9944         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9945         (mapcar (lambda (x) (push (mail-header-number x)
9946                                   gnus-newsgroup-limit))
9947                 headers)
9948         (gnus-summary-prepare-unthreaded (nreverse headers))
9949         (goto-char (point-min))
9950         (gnus-summary-position-point)
9951         t))))
9952
9953 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9954   "Mark all unread articles in this newsgroup as read.
9955 If prefix argument ALL is non-nil, ticked and dormant articles will
9956 also be marked as read.
9957 If QUIETLY is non-nil, no questions will be asked.
9958 If TO-HERE is non-nil, it should be a point in the buffer.  All
9959 articles before (after, if REVERSE is set) this point will be marked as read.
9960 Note that this function will only catch up the unread article
9961 in the current summary buffer limitation.
9962 The number of articles marked as read is returned."
9963   (interactive "P")
9964   (prog1
9965       (save-excursion
9966         (when (or quietly
9967                   (not gnus-interactive-catchup) ;Without confirmation?
9968                   gnus-expert-user
9969                   (gnus-y-or-n-p
9970                    (if all
9971                        "Mark absolutely all articles as read? "
9972                      "Mark all unread articles as read? ")))
9973           (if (and not-mark
9974                    (not gnus-newsgroup-adaptive)
9975                    (not gnus-newsgroup-auto-expire)
9976                    (not gnus-suppress-duplicates)
9977                    (or (not gnus-use-cache)
9978                        (eq gnus-use-cache 'passive)))
9979               (progn
9980                 (when all
9981                   (setq gnus-newsgroup-marked nil
9982                         gnus-newsgroup-spam-marked nil
9983                         gnus-newsgroup-dormant nil))
9984                 (setq gnus-newsgroup-unreads
9985                       (gnus-intersection gnus-newsgroup-unreads
9986                                          gnus-newsgroup-downloadable)))
9987             ;; We actually mark all articles as canceled, which we
9988             ;; have to do when using auto-expiry or adaptive scoring.
9989             (gnus-summary-show-all-threads)
9990             (if (and to-here reverse)
9991                 (progn
9992                   (goto-char to-here)
9993                   (while (and
9994                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9995                           (gnus-summary-find-next (not all) nil nil t))))
9996               (when (gnus-summary-first-subject (not all) t)
9997                 (while (and
9998                         (if to-here (< (point) to-here) t)
9999                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10000                         (gnus-summary-find-next (not all) nil nil t)))))
10001             (gnus-set-mode-line 'summary))
10002           t))
10003     (gnus-summary-position-point)))
10004
10005 (defun gnus-summary-catchup-to-here (&optional all)
10006   "Mark all unticked articles before the current one as read.
10007 If ALL is non-nil, also mark ticked and dormant articles as read."
10008   (interactive "P")
10009   (save-excursion
10010     (gnus-save-hidden-threads
10011       (let ((beg (point)))
10012         ;; We check that there are unread articles.
10013         (when (or all (gnus-summary-find-prev))
10014           (gnus-summary-catchup all t beg)))))
10015   (gnus-summary-position-point))
10016
10017 (defun gnus-summary-catchup-from-here (&optional all)
10018   "Mark all unticked articles after the current one as read.
10019 If ALL is non-nil, also mark ticked and dormant articles as read."
10020   (interactive "P")
10021   (save-excursion
10022     (gnus-save-hidden-threads
10023       (let ((beg (point)))
10024         ;; We check that there are unread articles.
10025         (when (or all (gnus-summary-find-next))
10026           (gnus-summary-catchup all t beg nil t)))))
10027
10028   (gnus-summary-position-point))
10029 (defun gnus-summary-catchup-all (&optional quietly)
10030   "Mark all articles in this newsgroup as read.
10031 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10032 instead, which marks only unread articles as read."
10033   (interactive "P")
10034   (gnus-summary-catchup t quietly))
10035
10036 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10037   "Mark all unread articles in this group as read, then exit.
10038 If prefix argument ALL is non-nil, all articles are marked as read.
10039 If QUIETLY is non-nil, no questions will be asked."
10040   (interactive "P")
10041   (when (gnus-summary-catchup all quietly nil 'fast)
10042     ;; Select next newsgroup or exit.
10043     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10044              (eq gnus-auto-select-next 'quietly))
10045         (gnus-summary-next-group nil)
10046       (gnus-summary-exit))))
10047
10048 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10049   "Mark all articles in this newsgroup as read, and then exit.
10050 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10051 instead, which marks only unread articles as read."
10052   (interactive "P")
10053   (gnus-summary-catchup-and-exit t quietly))
10054
10055 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10056   "Mark all articles in this group as read and select the next group.
10057 If given a prefix, mark all articles, unread as well as ticked, as
10058 read."
10059   (interactive "P")
10060   (save-excursion
10061     (gnus-summary-catchup all))
10062   (gnus-summary-next-group))
10063
10064 ;;;
10065 ;;; with article
10066 ;;;
10067
10068 (defmacro gnus-with-article (article &rest forms)
10069   "Select ARTICLE and perform FORMS in the original article buffer.
10070 Then replace the article with the result."
10071   `(progn
10072      ;; We don't want the article to be marked as read.
10073      (let (gnus-mark-article-hook)
10074        (gnus-summary-select-article t t nil ,article))
10075      (set-buffer gnus-original-article-buffer)
10076      ,@forms
10077      (if (not (gnus-check-backend-function
10078                'request-replace-article (car gnus-article-current)))
10079          (gnus-message 5 "Read-only group; not replacing")
10080        (unless (gnus-request-replace-article
10081                 ,article (car gnus-article-current)
10082                 (current-buffer) t)
10083          (error "Couldn't replace article")))
10084      ;; The cache and backlog have to be flushed somewhat.
10085      (when gnus-keep-backlog
10086        (gnus-backlog-remove-article
10087         (car gnus-article-current) (cdr gnus-article-current)))
10088      (when gnus-use-cache
10089        (gnus-cache-update-article
10090         (car gnus-article-current) (cdr gnus-article-current)))))
10091
10092 (put 'gnus-with-article 'lisp-indent-function 1)
10093 (put 'gnus-with-article 'edebug-form-spec '(form body))
10094
10095 ;; Thread-based commands.
10096
10097 (defun gnus-summary-articles-in-thread (&optional article)
10098   "Return a list of all articles in the current thread.
10099 If ARTICLE is non-nil, return all articles in the thread that starts
10100 with that article."
10101   (let* ((article (or article (gnus-summary-article-number)))
10102          (data (gnus-data-find-list article))
10103          (top-level (gnus-data-level (car data)))
10104          (top-subject
10105           (cond ((null gnus-thread-operation-ignore-subject)
10106                  (gnus-simplify-subject-re
10107                   (mail-header-subject (gnus-data-header (car data)))))
10108                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10109                  (gnus-simplify-subject-fuzzy
10110                   (mail-header-subject (gnus-data-header (car data)))))
10111                 (t nil)))
10112          (end-point (save-excursion
10113                       (if (gnus-summary-go-to-next-thread)
10114                           (point) (point-max))))
10115          articles)
10116     (while (and data
10117                 (< (gnus-data-pos (car data)) end-point))
10118       (when (or (not top-subject)
10119                 (string= top-subject
10120                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10121                              (gnus-simplify-subject-fuzzy
10122                               (mail-header-subject
10123                                (gnus-data-header (car data))))
10124                            (gnus-simplify-subject-re
10125                             (mail-header-subject
10126                              (gnus-data-header (car data)))))))
10127         (push (gnus-data-number (car data)) articles))
10128       (unless (and (setq data (cdr data))
10129                    (> (gnus-data-level (car data)) top-level))
10130         (setq data nil)))
10131     ;; Return the list of articles.
10132     (nreverse articles)))
10133
10134 (defun gnus-summary-rethread-current ()
10135   "Rethread the thread the current article is part of."
10136   (interactive)
10137   (let* ((gnus-show-threads t)
10138          (article (gnus-summary-article-number))
10139          (id (mail-header-id (gnus-summary-article-header)))
10140          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10141     (unless id
10142       (error "No article on the current line"))
10143     (gnus-rebuild-thread id)
10144     (gnus-summary-goto-subject article)))
10145
10146 (defun gnus-summary-reparent-thread ()
10147   "Make the current article child of the marked (or previous) article.
10148
10149 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10150 is non-nil or the Subject: of both articles are the same."
10151   (interactive)
10152   (unless (not (gnus-group-read-only-p))
10153     (error "The current newsgroup does not support article editing"))
10154   (unless (<= (length gnus-newsgroup-processable) 1)
10155     (error "No more than one article may be marked"))
10156   (save-window-excursion
10157     (let ((gnus-article-buffer " *reparent*")
10158           (current-article (gnus-summary-article-number))
10159           ;; First grab the marked article, otherwise one line up.
10160           (parent-article (if (not (null gnus-newsgroup-processable))
10161                               (car gnus-newsgroup-processable)
10162                             (save-excursion
10163                               (if (eq (forward-line -1) 0)
10164                                   (gnus-summary-article-number)
10165                                 (error "Beginning of summary buffer"))))))
10166       (unless (not (eq current-article parent-article))
10167         (error "An article may not be self-referential"))
10168       (let ((message-id (mail-header-id
10169                          (gnus-summary-article-header parent-article))))
10170         (unless (and message-id (not (equal message-id "")))
10171           (error "No message-id in desired parent"))
10172         (gnus-with-article current-article
10173           (save-restriction
10174             (goto-char (point-min))
10175             (message-narrow-to-head)
10176             (if (re-search-forward "^References: " nil t)
10177                 (progn
10178                   (re-search-forward "^[^ \t]" nil t)
10179                   (forward-line -1)
10180                   (end-of-line)
10181                   (insert " " message-id))
10182               (insert "References: " message-id "\n"))))
10183         (set-buffer gnus-summary-buffer)
10184         (gnus-summary-unmark-all-processable)
10185         (gnus-summary-update-article current-article)
10186         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10187             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10188         (gnus-summary-rethread-current)
10189         (gnus-message 3 "Article %d is now the child of article %d"
10190                       current-article parent-article)))))
10191
10192 (defun gnus-summary-toggle-threads (&optional arg)
10193   "Toggle showing conversation threads.
10194 If ARG is positive number, turn showing conversation threads on."
10195   (interactive "P")
10196   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10197     (setq gnus-show-threads
10198           (if (null arg) (not gnus-show-threads)
10199             (> (prefix-numeric-value arg) 0)))
10200     (gnus-summary-prepare)
10201     (gnus-summary-goto-subject current)
10202     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10203     (gnus-summary-position-point)))
10204
10205 (defun gnus-summary-show-all-threads ()
10206   "Show all threads."
10207   (interactive)
10208   (save-excursion
10209     (let ((buffer-read-only nil))
10210       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10211   (gnus-summary-position-point))
10212
10213 (defun gnus-summary-show-thread ()
10214   "Show thread subtrees.
10215 Returns nil if no thread was there to be shown."
10216   (interactive)
10217   (let ((buffer-read-only nil)
10218         (orig (point))
10219         ;; first goto end then to beg, to have point at beg after let
10220         (end (progn (end-of-line) (point)))
10221         (beg (progn (beginning-of-line) (point))))
10222     (prog1
10223         ;; Any hidden lines here?
10224         (search-forward "\r" end t)
10225       (subst-char-in-region beg end ?\^M ?\n t)
10226       (goto-char orig)
10227       (gnus-summary-position-point))))
10228
10229 (defun gnus-summary-maybe-hide-threads ()
10230   "If requested, hide the threads that should be hidden."
10231   (when (and gnus-show-threads
10232              gnus-thread-hide-subtree)
10233     (gnus-summary-hide-all-threads
10234      (if (or (consp gnus-thread-hide-subtree)
10235              (gnus-functionp gnus-thread-hide-subtree))
10236          (gnus-make-predicate gnus-thread-hide-subtree)
10237        nil))))
10238
10239 ;;; Hiding predicates.
10240
10241 (defun gnus-article-unread-p (header)
10242   (memq (mail-header-number header) gnus-newsgroup-unreads))
10243
10244 (defun gnus-article-unseen-p (header)
10245   (memq (mail-header-number header) gnus-newsgroup-unseen))
10246
10247 (defun gnus-map-articles (predicate articles)
10248   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10249   (apply 'gnus-or (mapcar predicate
10250                           (mapcar 'gnus-summary-article-header articles))))
10251
10252 (defun gnus-summary-hide-all-threads (&optional predicate)
10253   "Hide all thread subtrees.
10254 If PREDICATE is supplied, threads that satisfy this predicate
10255 will not be hidden."
10256   (interactive)
10257   (save-excursion
10258     (goto-char (point-min))
10259     (let ((end nil))
10260       (while (not end)
10261         (when (or (not predicate)
10262                   (gnus-map-articles
10263                    predicate (gnus-summary-article-children)))
10264             (gnus-summary-hide-thread))
10265         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10266   (gnus-summary-position-point))
10267
10268 (defun gnus-summary-hide-thread ()
10269   "Hide thread subtrees.
10270 If PREDICATE is supplied, threads that satisfy this predicate
10271 will not be hidden.
10272 Returns nil if no threads were there to be hidden."
10273   (interactive)
10274   (let ((buffer-read-only nil)
10275         (start (point))
10276         (article (gnus-summary-article-number)))
10277     (goto-char start)
10278     ;; Go forward until either the buffer ends or the subthread
10279     ;; ends.
10280     (when (and (not (eobp))
10281                (or (zerop (gnus-summary-next-thread 1 t))
10282                    (goto-char (point-max))))
10283       (prog1
10284           (if (and (> (point) start)
10285                    (search-backward "\n" start t))
10286               (progn
10287                 (subst-char-in-region start (point) ?\n ?\^M)
10288                 (gnus-summary-goto-subject article))
10289             (goto-char start)
10290             nil)))))
10291
10292 (defun gnus-summary-go-to-next-thread (&optional previous)
10293   "Go to the same level (or less) next thread.
10294 If PREVIOUS is non-nil, go to previous thread instead.
10295 Return the article number moved to, or nil if moving was impossible."
10296   (let ((level (gnus-summary-thread-level))
10297         (way (if previous -1 1))
10298         (beg (point)))
10299     (forward-line way)
10300     (while (and (not (eobp))
10301                 (< level (gnus-summary-thread-level)))
10302       (forward-line way))
10303     (if (eobp)
10304         (progn
10305           (goto-char beg)
10306           nil)
10307       (setq beg (point))
10308       (prog1
10309           (gnus-summary-article-number)
10310         (goto-char beg)))))
10311
10312 (defun gnus-summary-next-thread (n &optional silent)
10313   "Go to the same level next N'th thread.
10314 If N is negative, search backward instead.
10315 Returns the difference between N and the number of skips actually
10316 done.
10317
10318 If SILENT, don't output messages."
10319   (interactive "p")
10320   (let ((backward (< n 0))
10321         (n (abs n)))
10322     (while (and (> n 0)
10323                 (gnus-summary-go-to-next-thread backward))
10324       (decf n))
10325     (unless silent
10326       (gnus-summary-position-point))
10327     (when (and (not silent) (/= 0 n))
10328       (gnus-message 7 "No more threads"))
10329     n))
10330
10331 (defun gnus-summary-prev-thread (n)
10332   "Go to the same level previous N'th thread.
10333 Returns the difference between N and the number of skips actually
10334 done."
10335   (interactive "p")
10336   (gnus-summary-next-thread (- n)))
10337
10338 (defun gnus-summary-go-down-thread ()
10339   "Go down one level in the current thread."
10340   (let ((children (gnus-summary-article-children)))
10341     (when children
10342       (gnus-summary-goto-subject (car children)))))
10343
10344 (defun gnus-summary-go-up-thread ()
10345   "Go up one level in the current thread."
10346   (let ((parent (gnus-summary-article-parent)))
10347     (when parent
10348       (gnus-summary-goto-subject parent))))
10349
10350 (defun gnus-summary-down-thread (n)
10351   "Go down thread N steps.
10352 If N is negative, go up instead.
10353 Returns the difference between N and how many steps down that were
10354 taken."
10355   (interactive "p")
10356   (let ((up (< n 0))
10357         (n (abs n)))
10358     (while (and (> n 0)
10359                 (if up (gnus-summary-go-up-thread)
10360                   (gnus-summary-go-down-thread)))
10361       (setq n (1- n)))
10362     (gnus-summary-position-point)
10363     (when (/= 0 n)
10364       (gnus-message 7 "Can't go further"))
10365     n))
10366
10367 (defun gnus-summary-up-thread (n)
10368   "Go up thread N steps.
10369 If N is negative, go down instead.
10370 Returns the difference between N and how many steps down that were
10371 taken."
10372   (interactive "p")
10373   (gnus-summary-down-thread (- n)))
10374
10375 (defun gnus-summary-top-thread ()
10376   "Go to the top of the thread."
10377   (interactive)
10378   (while (gnus-summary-go-up-thread))
10379   (gnus-summary-article-number))
10380
10381 (defun gnus-summary-kill-thread (&optional unmark)
10382   "Mark articles under current thread as read.
10383 If the prefix argument is positive, remove any kinds of marks.
10384 If the prefix argument is negative, tick articles instead."
10385   (interactive "P")
10386   (when unmark
10387     (setq unmark (prefix-numeric-value unmark)))
10388   (let ((articles (gnus-summary-articles-in-thread)))
10389     (save-excursion
10390       ;; Expand the thread.
10391       (gnus-summary-show-thread)
10392       ;; Mark all the articles.
10393       (while articles
10394         (gnus-summary-goto-subject (car articles))
10395         (cond ((null unmark)
10396                (gnus-summary-mark-article-as-read gnus-killed-mark))
10397               ((> unmark 0)
10398                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10399               (t
10400                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10401         (setq articles (cdr articles))))
10402     ;; Hide killed subtrees.
10403     (and (null unmark)
10404          gnus-thread-hide-killed
10405          (gnus-summary-hide-thread))
10406     ;; If marked as read, go to next unread subject.
10407     (when (null unmark)
10408       ;; Go to next unread subject.
10409       (gnus-summary-next-subject 1 t)))
10410   (gnus-set-mode-line 'summary))
10411
10412 ;; Summary sorting commands
10413
10414 (defun gnus-summary-sort-by-number (&optional reverse)
10415   "Sort the summary buffer by article number.
10416 Argument REVERSE means reverse order."
10417   (interactive "P")
10418   (gnus-summary-sort 'number reverse))
10419
10420 (defun gnus-summary-sort-by-random (&optional reverse)
10421   "Randomize the order in the summary buffer.
10422 Argument REVERSE means to randomize in reverse order."
10423   (interactive "P")
10424   (gnus-summary-sort 'random reverse))
10425
10426 (defun gnus-summary-sort-by-author (&optional reverse)
10427   "Sort the summary buffer by author name alphabetically.
10428 If `case-fold-search' is non-nil, case of letters is ignored.
10429 Argument REVERSE means reverse order."
10430   (interactive "P")
10431   (gnus-summary-sort 'author reverse))
10432
10433 (defun gnus-summary-sort-by-subject (&optional reverse)
10434   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10435 If `case-fold-search' is non-nil, case of letters is ignored.
10436 Argument REVERSE means reverse order."
10437   (interactive "P")
10438   (gnus-summary-sort 'subject reverse))
10439
10440 (defun gnus-summary-sort-by-date (&optional reverse)
10441   "Sort the summary buffer by date.
10442 Argument REVERSE means reverse order."
10443   (interactive "P")
10444   (gnus-summary-sort 'date reverse))
10445
10446 (defun gnus-summary-sort-by-score (&optional reverse)
10447   "Sort the summary buffer by score.
10448 Argument REVERSE means reverse order."
10449   (interactive "P")
10450   (gnus-summary-sort 'score reverse))
10451
10452 (defun gnus-summary-sort-by-lines (&optional reverse)
10453   "Sort the summary buffer by the number of lines.
10454 Argument REVERSE means reverse order."
10455   (interactive "P")
10456   (gnus-summary-sort 'lines reverse))
10457
10458 (defun gnus-summary-sort-by-chars (&optional reverse)
10459   "Sort the summary buffer by article length.
10460 Argument REVERSE means reverse order."
10461   (interactive "P")
10462   (gnus-summary-sort 'chars reverse))
10463
10464 (defun gnus-summary-sort-by-original (&optional reverse)
10465   "Sort the summary buffer using the default sorting method.
10466 Argument REVERSE means reverse order."
10467   (interactive "P")
10468   (let* ((buffer-read-only)
10469          (gnus-summary-prepare-hook nil))
10470     ;; We do the sorting by regenerating the threads.
10471     (gnus-summary-prepare)
10472     ;; Hide subthreads if needed.
10473     (gnus-summary-maybe-hide-threads)))
10474
10475 (defun gnus-summary-sort (predicate reverse)
10476   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10477   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10478          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10479          (gnus-thread-sort-functions
10480           (if (not reverse)
10481               thread
10482             `(lambda (t1 t2)
10483                (,thread t2 t1))))
10484          (gnus-sort-gathered-threads-function
10485           gnus-thread-sort-functions)
10486          (gnus-article-sort-functions
10487           (if (not reverse)
10488               article
10489             `(lambda (t1 t2)
10490                (,article t2 t1))))
10491          (buffer-read-only)
10492          (gnus-summary-prepare-hook nil))
10493     ;; We do the sorting by regenerating the threads.
10494     (gnus-summary-prepare)
10495     ;; Hide subthreads if needed.
10496     (gnus-summary-maybe-hide-threads)))
10497
10498 ;; Summary saving commands.
10499
10500 (defun gnus-summary-save-article (&optional n not-saved)
10501   "Save the current article using the default saver function.
10502 If N is a positive number, save the N next articles.
10503 If N is a negative number, save the N previous articles.
10504 If N is nil and any articles have been marked with the process mark,
10505 save those articles instead.
10506 The variable `gnus-default-article-saver' specifies the saver function."
10507   (interactive "P")
10508   (let* ((articles (gnus-summary-work-articles n))
10509          (save-buffer (save-excursion
10510                         (nnheader-set-temp-buffer " *Gnus Save*")))
10511          (num (length articles))
10512          header file)
10513     (dolist (article articles)
10514       (setq header (gnus-summary-article-header article))
10515       (if (not (vectorp header))
10516           ;; This is a pseudo-article.
10517           (if (assq 'name header)
10518               (gnus-copy-file (cdr (assq 'name header)))
10519             (gnus-message 1 "Article %d is unsaveable" article))
10520         ;; This is a real article.
10521         (save-window-excursion
10522           (let ((gnus-display-mime-function nil)
10523                 (gnus-article-prepare-hook nil))
10524             (gnus-summary-select-article t nil nil article)))
10525         (save-excursion
10526           (set-buffer save-buffer)
10527           (erase-buffer)
10528           (insert-buffer-substring gnus-original-article-buffer))
10529         (setq file (gnus-article-save save-buffer file num))
10530         (gnus-summary-remove-process-mark article)
10531         (unless not-saved
10532           (gnus-summary-set-saved-mark article))))
10533     (gnus-kill-buffer save-buffer)
10534     (gnus-summary-position-point)
10535     (gnus-set-mode-line 'summary)
10536     n))
10537
10538 (defun gnus-summary-pipe-output (&optional arg headers)
10539   "Pipe the current article to a subprocess.
10540 If N is a positive number, pipe the N next articles.
10541 If N is a negative number, pipe the N previous articles.
10542 If N is nil and any articles have been marked with the process mark,
10543 pipe those articles instead.
10544 If HEADERS (the symbolic prefix), include the headers, too."
10545   (interactive (gnus-interactive "P\ny"))
10546   (require 'gnus-art)
10547   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10548         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10549     (gnus-summary-save-article arg t))
10550   (let ((buffer (get-buffer "*Shell Command Output*")))
10551     (when (and buffer
10552                (not (zerop (buffer-size buffer))))
10553       (gnus-configure-windows 'pipe))))
10554
10555 (defun gnus-summary-save-article-mail (&optional arg)
10556   "Append the current article to an mail file.
10557 If N is a positive number, save the N next articles.
10558 If N is a negative number, save the N previous articles.
10559 If N is nil and any articles have been marked with the process mark,
10560 save those articles instead."
10561   (interactive "P")
10562   (require 'gnus-art)
10563   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10564     (gnus-summary-save-article arg)))
10565
10566 (defun gnus-summary-save-article-rmail (&optional arg)
10567   "Append the current article to an rmail file.
10568 If N is a positive number, save the N next articles.
10569 If N is a negative number, save the N previous articles.
10570 If N is nil and any articles have been marked with the process mark,
10571 save those articles instead."
10572   (interactive "P")
10573   (require 'gnus-art)
10574   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10575     (gnus-summary-save-article arg)))
10576
10577 (defun gnus-summary-save-article-file (&optional arg)
10578   "Append the current article to a file.
10579 If N is a positive number, save the N next articles.
10580 If N is a negative number, save the N previous articles.
10581 If N is nil and any articles have been marked with the process mark,
10582 save those articles instead."
10583   (interactive "P")
10584   (require 'gnus-art)
10585   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10586     (gnus-summary-save-article arg)))
10587
10588 (defun gnus-summary-write-article-file (&optional arg)
10589   "Write the current article to a file, deleting the previous file.
10590 If N is a positive number, save the N next articles.
10591 If N is a negative number, save the N previous articles.
10592 If N is nil and any articles have been marked with the process mark,
10593 save those articles instead."
10594   (interactive "P")
10595   (require 'gnus-art)
10596   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10597     (gnus-summary-save-article arg)))
10598
10599 (defun gnus-summary-save-article-body-file (&optional arg)
10600   "Append the current article body to a file.
10601 If N is a positive number, save the N next articles.
10602 If N is a negative number, save the N previous articles.
10603 If N is nil and any articles have been marked with the process mark,
10604 save those articles instead."
10605   (interactive "P")
10606   (require 'gnus-art)
10607   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10608     (gnus-summary-save-article arg)))
10609
10610 (defun gnus-summary-muttprint (&optional arg)
10611   "Print the current article using Muttprint.
10612 If N is a positive number, save the N next articles.
10613 If N is a negative number, save the N previous articles.
10614 If N is nil and any articles have been marked with the process mark,
10615 save those articles instead."
10616   (interactive "P")
10617   (require 'gnus-art)
10618   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10619     (gnus-summary-save-article arg t)))
10620
10621 (defun gnus-summary-pipe-message (program)
10622   "Pipe the current article through PROGRAM."
10623   (interactive "sProgram: ")
10624   (gnus-summary-select-article)
10625   (let ((mail-header-separator ""))
10626     (gnus-eval-in-buffer-window gnus-article-buffer
10627       (save-restriction
10628         (widen)
10629         (let ((start (window-start))
10630               buffer-read-only)
10631           (message-pipe-buffer-body program)
10632           (set-window-start (get-buffer-window (current-buffer)) start))))))
10633
10634 (defun gnus-get-split-value (methods)
10635   "Return a value based on the split METHODS."
10636   (let (split-name method result match)
10637     (when methods
10638       (save-excursion
10639         (set-buffer gnus-original-article-buffer)
10640         (save-restriction
10641           (nnheader-narrow-to-headers)
10642           (while (and methods (not split-name))
10643             (goto-char (point-min))
10644             (setq method (pop methods))
10645             (setq match (car method))
10646             (when (cond
10647                    ((stringp match)
10648                     ;; Regular expression.
10649                     (ignore-errors
10650                       (re-search-forward match nil t)))
10651                    ((gnus-functionp match)
10652                     ;; Function.
10653                     (save-restriction
10654                       (widen)
10655                       (setq result (funcall match gnus-newsgroup-name))))
10656                    ((consp match)
10657                     ;; Form.
10658                     (save-restriction
10659                       (widen)
10660                       (setq result (eval match)))))
10661               (setq split-name (cdr method))
10662               (cond ((stringp result)
10663                      (push (expand-file-name
10664                             result gnus-article-save-directory)
10665                            split-name))
10666                     ((consp result)
10667                      (setq split-name (append result split-name)))))))))
10668     (nreverse split-name)))
10669
10670 (defun gnus-valid-move-group-p (group)
10671   (and (boundp group)
10672        (symbol-name group)
10673        (symbol-value group)
10674        (gnus-get-function (gnus-find-method-for-group
10675                            (symbol-name group)) 'request-accept-article t)))
10676
10677 (defun gnus-read-move-group-name (prompt default articles prefix)
10678   "Read a group name."
10679   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10680          (minibuffer-confirm-incomplete nil) ; XEmacs
10681          (prom
10682           (format "%s %s to:"
10683                   prompt
10684                   (if (> (length articles) 1)
10685                       (format "these %d articles" (length articles))
10686                     "this article")))
10687          (to-newsgroup
10688           (cond
10689            ((null split-name)
10690             (gnus-completing-read-with-default
10691              default prom
10692              gnus-active-hashtb
10693              'gnus-valid-move-group-p
10694              nil prefix
10695              'gnus-group-history))
10696            ((= 1 (length split-name))
10697             (gnus-completing-read-with-default
10698              (car split-name) prom
10699              gnus-active-hashtb
10700              'gnus-valid-move-group-p
10701              nil nil
10702              'gnus-group-history))
10703            (t
10704             (gnus-completing-read-with-default
10705              nil prom
10706              (mapcar (lambda (el) (list el))
10707                      (nreverse split-name))
10708              nil nil nil
10709              'gnus-group-history))))
10710          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10711     (when to-newsgroup
10712       (if (or (string= to-newsgroup "")
10713               (string= to-newsgroup prefix))
10714           (setq to-newsgroup default))
10715       (unless to-newsgroup
10716         (error "No group name entered"))
10717       (or (gnus-active to-newsgroup)
10718           (gnus-activate-group to-newsgroup nil nil to-method)
10719           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10720                                      to-newsgroup))
10721               (or (and (gnus-request-create-group to-newsgroup to-method)
10722                        (gnus-activate-group
10723                         to-newsgroup nil nil to-method)
10724                        (gnus-subscribe-group to-newsgroup))
10725                   (error "Couldn't create group %s" to-newsgroup)))
10726           (error "No such group: %s" to-newsgroup)))
10727     to-newsgroup))
10728
10729 (defun gnus-summary-save-parts (type dir n &optional reverse)
10730   "Save parts matching TYPE to DIR.
10731 If REVERSE, save parts that do not match TYPE."
10732   (interactive
10733    (list (read-string "Save parts of type: "
10734                       (or (car gnus-summary-save-parts-type-history)
10735                           gnus-summary-save-parts-default-mime)
10736                       'gnus-summary-save-parts-type-history)
10737          (setq gnus-summary-save-parts-last-directory
10738                (read-file-name "Save to directory: "
10739                                gnus-summary-save-parts-last-directory
10740                                nil t))
10741          current-prefix-arg))
10742   (gnus-summary-iterate n
10743     (let ((gnus-display-mime-function nil)
10744           (gnus-inhibit-treatment t))
10745       (gnus-summary-select-article))
10746     (save-excursion
10747       (set-buffer gnus-article-buffer)
10748       (let ((handles (or gnus-article-mime-handles
10749                          (mm-dissect-buffer nil gnus-article-loose-mime)
10750                          (and gnus-article-emulate-mime
10751                               (mm-uu-dissect)))))
10752         (when handles
10753           (gnus-summary-save-parts-1 type dir handles reverse)
10754           (unless gnus-article-mime-handles ;; Don't destroy this case.
10755             (mm-destroy-parts handles)))))))
10756
10757 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10758   (if (stringp (car handle))
10759       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10760               (cdr handle))
10761     (when (if reverse
10762               (not (string-match type (mm-handle-media-type handle)))
10763             (string-match type (mm-handle-media-type handle)))
10764       (let ((file (expand-file-name
10765                    (file-name-nondirectory
10766                     (or
10767                      (mail-content-type-get
10768                       (mm-handle-disposition handle) 'filename)
10769                      (concat gnus-newsgroup-name
10770                              "." (number-to-string
10771                                   (cdr gnus-article-current)))))
10772                    dir)))
10773         (unless (file-exists-p file)
10774           (mm-save-part-to-file handle file))))))
10775
10776 ;; Summary extract commands
10777
10778 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10779   (let ((buffer-read-only nil)
10780         (article (gnus-summary-article-number))
10781         after-article b e)
10782     (unless (gnus-summary-goto-subject article)
10783       (error "No such article: %d" article))
10784     (gnus-summary-position-point)
10785     ;; If all commands are to be bunched up on one line, we collect
10786     ;; them here.
10787     (unless gnus-view-pseudos-separately
10788       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10789             files action)
10790         (while ps
10791           (setq action (cdr (assq 'action (car ps))))
10792           (setq files (list (cdr (assq 'name (car ps)))))
10793           (while (and ps (cdr ps)
10794                       (string= (or action "1")
10795                                (or (cdr (assq 'action (cadr ps))) "2")))
10796             (push (cdr (assq 'name (cadr ps))) files)
10797             (setcdr ps (cddr ps)))
10798           (when files
10799             (when (not (string-match "%s" action))
10800               (push " " files))
10801             (push " " files)
10802             (when (assq 'execute (car ps))
10803               (setcdr (assq 'execute (car ps))
10804                       (funcall (if (string-match "%s" action)
10805                                    'format 'concat)
10806                                action
10807                                (mapconcat
10808                                 (lambda (f)
10809                                   (if (equal f " ")
10810                                       f
10811                                     (mm-quote-arg f)))
10812                                 files " ")))))
10813           (setq ps (cdr ps)))))
10814     (if (and gnus-view-pseudos (not not-view))
10815         (while pslist
10816           (when (assq 'execute (car pslist))
10817             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10818                                   (eq gnus-view-pseudos 'not-confirm)))
10819           (setq pslist (cdr pslist)))
10820       (save-excursion
10821         (while pslist
10822           (setq after-article (or (cdr (assq 'article (car pslist)))
10823                                   (gnus-summary-article-number)))
10824           (gnus-summary-goto-subject after-article)
10825           (forward-line 1)
10826           (setq b (point))
10827           (insert "    " (file-name-nondirectory
10828                           (cdr (assq 'name (car pslist))))
10829                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10830           (setq e (point))
10831           (forward-line -1)             ; back to `b'
10832           (gnus-add-text-properties
10833            b (1- e) (list 'gnus-number gnus-reffed-article-number
10834                           gnus-mouse-face-prop gnus-mouse-face))
10835           (gnus-data-enter
10836            after-article gnus-reffed-article-number
10837            gnus-unread-mark b (car pslist) 0 (- e b))
10838           (setq gnus-newsgroup-unreads
10839                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10840                                          gnus-reffed-article-number))
10841           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10842           (setq pslist (cdr pslist)))))))
10843
10844 (defun gnus-pseudos< (p1 p2)
10845   (let ((c1 (cdr (assq 'action p1)))
10846         (c2 (cdr (assq 'action p2))))
10847     (and c1 c2 (string< c1 c2))))
10848
10849 (defun gnus-request-pseudo-article (props)
10850   (cond ((assq 'execute props)
10851          (gnus-execute-command (cdr (assq 'execute props)))))
10852   (let ((gnus-current-article (gnus-summary-article-number)))
10853     (gnus-run-hooks 'gnus-mark-article-hook)))
10854
10855 (defun gnus-execute-command (command &optional automatic)
10856   (save-excursion
10857     (gnus-article-setup-buffer)
10858     (set-buffer gnus-article-buffer)
10859     (setq buffer-read-only nil)
10860     (let ((command (if automatic command
10861                      (read-string "Command: " (cons command 0)))))
10862       (erase-buffer)
10863       (insert "$ " command "\n\n")
10864       (if gnus-view-pseudo-asynchronously
10865           (start-process "gnus-execute" (current-buffer) shell-file-name
10866                          shell-command-switch command)
10867         (call-process shell-file-name nil t nil
10868                       shell-command-switch command)))))
10869
10870 ;; Summary kill commands.
10871
10872 (defun gnus-summary-edit-global-kill (article)
10873   "Edit the \"global\" kill file."
10874   (interactive (list (gnus-summary-article-number)))
10875   (gnus-group-edit-global-kill article))
10876
10877 (defun gnus-summary-edit-local-kill ()
10878   "Edit a local kill file applied to the current newsgroup."
10879   (interactive)
10880   (setq gnus-current-headers (gnus-summary-article-header))
10881   (gnus-group-edit-local-kill
10882    (gnus-summary-article-number) gnus-newsgroup-name))
10883
10884 ;;; Header reading.
10885
10886 (defun gnus-read-header (id &optional header)
10887   "Read the headers of article ID and enter them into the Gnus system."
10888   (let ((group gnus-newsgroup-name)
10889         (gnus-override-method
10890          (or
10891           gnus-override-method
10892           (and (gnus-news-group-p gnus-newsgroup-name)
10893                (car (gnus-refer-article-methods)))))
10894         where)
10895     ;; First we check to see whether the header in question is already
10896     ;; fetched.
10897     (if (stringp id)
10898         ;; This is a Message-ID.
10899         (setq header (or header (gnus-id-to-header id)))
10900       ;; This is an article number.
10901       (setq header (or header (gnus-summary-article-header id))))
10902     (if (and header
10903              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10904         ;; We have found the header.
10905         header
10906       ;; If this is a sparse article, we have to nix out its
10907       ;; previous entry in the thread hashtb.
10908       (when (and header
10909                  (gnus-summary-article-sparse-p (mail-header-number header)))
10910         (let* ((parent (gnus-parent-id (mail-header-references header)))
10911                (thread (and parent (gnus-id-to-thread parent))))
10912           (when thread
10913             (delq (assq header thread) thread))))
10914       ;; We have to really fetch the header to this article.
10915       (save-excursion
10916         (set-buffer nntp-server-buffer)
10917         (when (setq where (gnus-request-head id group))
10918           (nnheader-fold-continuation-lines)
10919           (goto-char (point-max))
10920           (insert ".\n")
10921           (goto-char (point-min))
10922           (insert "211 ")
10923           (princ (cond
10924                   ((numberp id) id)
10925                   ((cdr where) (cdr where))
10926                   (header (mail-header-number header))
10927                   (t gnus-reffed-article-number))
10928                  (current-buffer))
10929           (insert " Article retrieved.\n"))
10930         (if (or (not where)
10931                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10932             ()                          ; Malformed head.
10933           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10934             (when (and (stringp id)
10935                        (not (string= (gnus-group-real-name group)
10936                                      (car where))))
10937               ;; If we fetched by Message-ID and the article came
10938               ;; from a different group, we fudge some bogus article
10939               ;; numbers for this article.
10940               (mail-header-set-number header gnus-reffed-article-number))
10941             (save-excursion
10942               (set-buffer gnus-summary-buffer)
10943               (decf gnus-reffed-article-number)
10944               (gnus-remove-header (mail-header-number header))
10945               (push header gnus-newsgroup-headers)
10946               (setq gnus-current-headers header)
10947               (push (mail-header-number header) gnus-newsgroup-limit)))
10948           header)))))
10949
10950 (defun gnus-remove-header (number)
10951   "Remove header NUMBER from `gnus-newsgroup-headers'."
10952   (if (and gnus-newsgroup-headers
10953            (= number (mail-header-number (car gnus-newsgroup-headers))))
10954       (pop gnus-newsgroup-headers)
10955     (let ((headers gnus-newsgroup-headers))
10956       (while (and (cdr headers)
10957                   (not (= number (mail-header-number (cadr headers)))))
10958         (pop headers))
10959       (when (cdr headers)
10960         (setcdr headers (cddr headers))))))
10961
10962 ;;;
10963 ;;; summary highlights
10964 ;;;
10965
10966 (defun gnus-highlight-selected-summary ()
10967   "Highlight selected article in summary buffer."
10968   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10969   (when gnus-summary-selected-face
10970     (save-excursion
10971       (let* ((beg (progn (beginning-of-line) (point)))
10972              (end (progn (end-of-line) (point)))
10973              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10974              (from (if (get-text-property beg gnus-mouse-face-prop)
10975                        beg
10976                      (or (next-single-property-change
10977                           beg gnus-mouse-face-prop nil end)
10978                          beg)))
10979              (to
10980               (if (= from end)
10981                   (- from 2)
10982                 (or (next-single-property-change
10983                      from gnus-mouse-face-prop nil end)
10984                     end))))
10985         ;; If no mouse-face prop on line we will have to = from = end,
10986         ;; so we highlight the entire line instead.
10987         (when (= (+ to 2) from)
10988           (setq from beg)
10989           (setq to end))
10990         (if gnus-newsgroup-selected-overlay
10991             ;; Move old overlay.
10992             (gnus-move-overlay
10993              gnus-newsgroup-selected-overlay from to (current-buffer))
10994           ;; Create new overlay.
10995           (gnus-overlay-put
10996            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10997            'face gnus-summary-selected-face))))))
10998
10999 (defvar gnus-summary-highlight-line-cached nil)
11000 (defvar gnus-summary-highlight-line-trigger nil)
11001
11002 (defun gnus-summary-highlight-line-0 ()
11003   (if (and (eq gnus-summary-highlight-line-trigger 
11004                gnus-summary-highlight)
11005            gnus-summary-highlight-line-cached)
11006       gnus-summary-highlight-line-cached
11007     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11008           gnus-summary-highlight-line-cached
11009           (let* ((cond (list 'cond))
11010                  (c cond)
11011                  (list gnus-summary-highlight))
11012             (while list
11013               (setcdr c (cons (list (caar list) (list 'quote (cdar list))) nil))
11014               (setq c (cdr c)
11015                     list (cdr list)))
11016             (gnus-byte-compile (list 'lambda nil cond))))))
11017
11018 (defun gnus-summary-highlight-line ()
11019   "Highlight current line according to `gnus-summary-highlight'."
11020   (let* ((beg (gnus-point-at-bol))
11021          (article (or (gnus-summary-article-number) gnus-current-article))
11022          (score (or (cdr (assq article
11023                                gnus-newsgroup-scored))
11024                     gnus-summary-default-score 0))
11025          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11026          (inhibit-read-only t)
11027          (default gnus-summary-default-score)
11028          (default-high gnus-summary-default-high-score)
11029          (default-low gnus-summary-default-low-score)
11030          (uncached (memq article gnus-newsgroup-undownloaded))
11031          (downloaded (not uncached)))
11032     (let ((face (funcall (gnus-summary-highlight-line-0))))
11033       (unless (eq face (get-text-property beg 'face))
11034         (gnus-put-text-property-excluding-characters-with-faces
11035          beg (gnus-point-at-eol) 'face
11036          (setq face (if (boundp face) (symbol-value face) face)))
11037         (when gnus-summary-highlight-line-function
11038           (funcall gnus-summary-highlight-line-function article face))))))
11039
11040 (defun gnus-update-read-articles (group unread &optional compute)
11041   "Update the list of read articles in GROUP.
11042 UNREAD is a sorted list."
11043   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11044          (entry (gnus-gethash group gnus-newsrc-hashtb))
11045          (info (nth 2 entry))
11046          (prev 1)
11047          read)
11048     (if (or (not info) (not active))
11049         ;; There is no info on this group if it was, in fact,
11050         ;; killed.  Gnus stores no information on killed groups, so
11051         ;; there's nothing to be done.
11052         ;; One could store the information somewhere temporarily,
11053         ;; perhaps...  Hmmm...
11054         ()
11055       ;; Remove any negative articles numbers.
11056       (while (and unread (< (car unread) 0))
11057         (setq unread (cdr unread)))
11058       ;; Remove any expired article numbers
11059       (while (and unread (< (car unread) (car active)))
11060         (setq unread (cdr unread)))
11061       ;; Compute the ranges of read articles by looking at the list of
11062       ;; unread articles.
11063       (while unread
11064         (when (/= (car unread) prev)
11065           (push (if (= prev (1- (car unread))) prev
11066                   (cons prev (1- (car unread))))
11067                 read))
11068         (setq prev (1+ (car unread)))
11069         (setq unread (cdr unread)))
11070       (when (<= prev (cdr active))
11071         (push (cons prev (cdr active)) read))
11072       (setq read (if (> (length read) 1) (nreverse read) read))
11073       (if compute
11074           read
11075         (save-excursion
11076           (let (setmarkundo)
11077             ;; Propagate the read marks to the backend.
11078             (when (gnus-check-backend-function 'request-set-mark group)
11079               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11080                     (add (gnus-remove-from-range read (gnus-info-read info))))
11081                 (when (or add del)
11082                   (unless (gnus-check-group group)
11083                     (error "Can't open server for %s" group))
11084                   (gnus-request-set-mark
11085                    group (delq nil (list (if add (list add 'add '(read)))
11086                                          (if del (list del 'del '(read))))))
11087                   (setq setmarkundo
11088                         `(gnus-request-set-mark
11089                           ,group
11090                           ',(delq nil (list
11091                                        (if del (list del 'add '(read)))
11092                                        (if add (list add 'del '(read))))))))))
11093             (set-buffer gnus-group-buffer)
11094             (gnus-undo-register
11095               `(progn
11096                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11097                  (gnus-info-set-read ',info ',(gnus-info-read info))
11098                  (gnus-get-unread-articles-in-group ',info
11099                                                     (gnus-active ,group))
11100                  (gnus-group-update-group ,group t)
11101                  ,setmarkundo))))
11102         ;; Enter this list into the group info.
11103         (gnus-info-set-read info read)
11104         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11105         (gnus-get-unread-articles-in-group info (gnus-active group))
11106         t))))
11107
11108 (defun gnus-offer-save-summaries ()
11109   "Offer to save all active summary buffers."
11110   (let (buffers)
11111     ;; Go through all buffers and find all summaries.
11112     (dolist (buffer (buffer-list))
11113       (when (and (setq buffer (buffer-name buffer))
11114                  (string-match "Summary" buffer)
11115                  (save-excursion
11116                    (set-buffer buffer)
11117                    ;; We check that this is, indeed, a summary buffer.
11118                    (and (eq major-mode 'gnus-summary-mode)
11119                         ;; Also make sure this isn't bogus.
11120                         gnus-newsgroup-prepared
11121                         ;; Also make sure that this isn't a
11122                         ;; dead summary buffer.
11123                         (not gnus-dead-summary-mode))))
11124         (push buffer buffers)))
11125     ;; Go through all these summary buffers and offer to save them.
11126     (when buffers
11127       (save-excursion
11128         (map-y-or-n-p
11129          "Update summary buffer %s? "
11130          (lambda (buf)
11131            (switch-to-buffer buf)
11132            (gnus-summary-exit))
11133          buffers)))))
11134
11135 (defun gnus-summary-setup-default-charset ()
11136   "Setup newsgroup default charset."
11137   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11138       (setq gnus-newsgroup-charset nil)
11139     (let* ((ignored-charsets
11140             (or gnus-newsgroup-ephemeral-ignored-charsets
11141                 (append
11142                  (and gnus-newsgroup-name
11143                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11144                  gnus-newsgroup-ignored-charsets))))
11145       (setq gnus-newsgroup-charset
11146             (or gnus-newsgroup-ephemeral-charset
11147                 (and gnus-newsgroup-name
11148                      (gnus-parameter-charset gnus-newsgroup-name))
11149                 gnus-default-charset))
11150       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11151            ignored-charsets))))
11152
11153 ;;;
11154 ;;; Mime Commands
11155 ;;;
11156
11157 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11158   "Display the current article buffer fully MIME-buttonized.
11159 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11160 treated as multipart/mixed."
11161   (interactive "P")
11162   (require 'gnus-art)
11163   (let ((gnus-unbuttonized-mime-types nil)
11164         (gnus-mime-display-multipart-as-mixed show-all-parts))
11165     (gnus-summary-show-article)))
11166
11167 (defun gnus-summary-repair-multipart (article)
11168   "Add a Content-Type header to a multipart article without one."
11169   (interactive (list (gnus-summary-article-number)))
11170   (gnus-with-article article
11171     (message-narrow-to-head)
11172     (message-remove-header "Mime-Version")
11173     (goto-char (point-max))
11174     (insert "Mime-Version: 1.0\n")
11175     (widen)
11176     (when (search-forward "\n--" nil t)
11177       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11178         (message-narrow-to-head)
11179         (message-remove-header "Content-Type")
11180         (goto-char (point-max))
11181         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11182                         separator))
11183         (widen))))
11184   (let (gnus-mark-article-hook)
11185     (gnus-summary-select-article t t nil article)))
11186
11187 (defun gnus-summary-toggle-display-buttonized ()
11188   "Toggle the buttonizing of the article buffer."
11189   (interactive)
11190   (require 'gnus-art)
11191   (if (setq gnus-inhibit-mime-unbuttonizing
11192             (not gnus-inhibit-mime-unbuttonizing))
11193       (let ((gnus-unbuttonized-mime-types nil))
11194         (gnus-summary-show-article))
11195     (gnus-summary-show-article)))
11196
11197 ;;;
11198 ;;; Generic summary marking commands
11199 ;;;
11200
11201 (defvar gnus-summary-marking-alist
11202   '((read gnus-del-mark "d")
11203     (unread gnus-unread-mark "u")
11204     (ticked gnus-ticked-mark "!")
11205     (dormant gnus-dormant-mark "?")
11206     (expirable gnus-expirable-mark "e"))
11207   "An alist of names/marks/keystrokes.")
11208
11209 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11210 (defvar gnus-summary-mark-map)
11211
11212 (defun gnus-summary-make-all-marking-commands ()
11213   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11214   (dolist (elem gnus-summary-marking-alist)
11215     (apply 'gnus-summary-make-marking-command elem)))
11216
11217 (defun gnus-summary-make-marking-command (name mark keystroke)
11218   (let ((map (make-sparse-keymap)))
11219     (define-key gnus-summary-generic-mark-map keystroke map)
11220     (dolist (lway `((next "next" next nil "n")
11221                     (next-unread "next unread" next t "N")
11222                     (prev "previous" prev nil "p")
11223                     (prev-unread "previous unread" prev t "P")
11224                     (nomove "" nil nil ,keystroke)))
11225       (let ((func (gnus-summary-make-marking-command-1
11226                    mark (car lway) lway name)))
11227         (setq func (eval func))
11228         (define-key map (nth 4 lway) func)))))
11229
11230 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11231   `(defun ,(intern
11232             (format "gnus-summary-put-mark-as-%s%s"
11233                     name (if (eq way 'nomove)
11234                              ""
11235                            (concat "-" (symbol-name way)))))
11236      (n)
11237      ,(format
11238        "Mark the current article as %s%s.
11239 If N, the prefix, then repeat N times.
11240 If N is negative, move in reverse order.
11241 The difference between N and the actual number of articles marked is
11242 returned."
11243        name (cadr lway))
11244      (interactive "p")
11245      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11246
11247 (defun gnus-summary-generic-mark (n mark move unread)
11248   "Mark N articles with MARK."
11249   (unless (eq major-mode 'gnus-summary-mode)
11250     (error "This command can only be used in the summary buffer"))
11251   (gnus-summary-show-thread)
11252   (let ((nummove
11253          (cond
11254           ((eq move 'next) 1)
11255           ((eq move 'prev) -1)
11256           (t 0))))
11257     (if (zerop nummove)
11258         (setq n 1)
11259       (when (< n 0)
11260         (setq n (abs n)
11261               nummove (* -1 nummove))))
11262     (while (and (> n 0)
11263                 (gnus-summary-mark-article nil mark)
11264                 (zerop (gnus-summary-next-subject nummove unread t)))
11265       (setq n (1- n)))
11266     (when (/= 0 n)
11267       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11268     (gnus-summary-recenter)
11269     (gnus-summary-position-point)
11270     (gnus-set-mode-line 'summary)
11271     n))
11272
11273 (defun gnus-summary-insert-articles (articles)
11274   (when (setq articles
11275               (gnus-sorted-difference articles
11276                                       (mapcar (lambda (h)
11277                                                 (mail-header-number h))
11278                                               gnus-newsgroup-headers)))
11279     (setq gnus-newsgroup-headers
11280           (merge 'list
11281                  gnus-newsgroup-headers
11282                  (gnus-fetch-headers articles)
11283                  'gnus-article-sort-by-number))
11284     ;; Suppress duplicates?
11285     (when gnus-suppress-duplicates
11286       (gnus-dup-suppress-articles))
11287
11288     ;; We might want to build some more threads first.
11289     (when (and gnus-fetch-old-headers
11290                (eq gnus-headers-retrieved-by 'nov))
11291       (if (eq gnus-fetch-old-headers 'invisible)
11292           (gnus-build-all-threads)
11293         (gnus-build-old-threads)))
11294     ;; Let the Gnus agent mark articles as read.
11295     (when gnus-agent
11296       (gnus-agent-get-undownloaded-list))
11297     ;; Remove list identifiers from subject
11298     (when gnus-list-identifiers
11299       (gnus-summary-remove-list-identifiers))
11300     ;; First and last article in this newsgroup.
11301     (when gnus-newsgroup-headers
11302       (setq gnus-newsgroup-begin
11303             (mail-header-number (car gnus-newsgroup-headers))
11304             gnus-newsgroup-end
11305             (mail-header-number
11306              (gnus-last-element gnus-newsgroup-headers))))
11307     (when gnus-use-scoring
11308       (gnus-possibly-score-headers))))
11309
11310 (defun gnus-summary-insert-old-articles (&optional all)
11311   "Insert all old articles in this group.
11312 If ALL is non-nil, already read articles become readable.
11313 If ALL is a number, fetch this number of articles."
11314   (interactive "P")
11315   (prog1
11316       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11317             older len)
11318         (setq older
11319               ;; Some nntp servers lie about their active range.  When
11320               ;; this happens, the active range can be in the millions.
11321               ;; Use a compressed range to avoid creating a huge list.
11322               (gnus-range-difference (list gnus-newsgroup-active) old))
11323         (setq len (gnus-range-length older))
11324         (cond
11325          ((null older) nil)
11326          ((numberp all)
11327           (if (< all len)
11328               (let ((older-range (nreverse older)))
11329                 (setq older nil)
11330
11331                 (while (> all 0)
11332                   (let* ((r (pop older-range))
11333                          (min (if (numberp r) r (car r)))
11334                          (max (if (numberp r) r (cdr r))))
11335                     (while (and (<= min max)
11336                                 (> all 0))
11337                       (push max older)
11338                       (setq all (1- all)
11339                             max (1- max))))))
11340             (setq older (gnus-uncompress-range older))))
11341          (all
11342           (setq older (gnus-uncompress-range older)))
11343          (t
11344           (when (and (numberp gnus-large-newsgroup)
11345                    (> len gnus-large-newsgroup))
11346               (let* ((cursor-in-echo-area nil)
11347                      (initial (gnus-parameter-large-newsgroup-initial
11348                                gnus-newsgroup-name))
11349                      (input
11350                       (read-string
11351                        (format
11352                         "How many articles from %s (%s %d): "
11353                         (gnus-limit-string
11354                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11355                         (if initial "max" "default")
11356                         len)
11357                        (if initial
11358                            (cons (number-to-string initial)
11359                                  0)))))
11360                 (unless (string-match "^[ \t]*$" input)
11361                   (setq all (string-to-number input))
11362                   (if (< all len)
11363                       (let ((older-range (nreverse older)))
11364                         (setq older nil)
11365
11366                         (while (> all 0)
11367                           (let* ((r (pop older-range))
11368                                  (min (if (numberp r) r (car r)))
11369                                  (max (if (numberp r) r (cdr r))))
11370                             (while (and (<= min max)
11371                                         (> all 0))
11372                               (push max older)
11373                               (setq all (1- all)
11374                                     max (1- max))))))))))
11375           (setq older (gnus-uncompress-range older))))
11376         (if (not older)
11377             (message "No old news.")
11378           (gnus-summary-insert-articles older)
11379           (gnus-summary-limit (gnus-sorted-nunion old older))))
11380     (gnus-summary-position-point)))
11381
11382 (defun gnus-summary-insert-new-articles ()
11383   "Insert all new articles in this group."
11384   (interactive)
11385   (prog1
11386       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11387             (old-active gnus-newsgroup-active)
11388             (nnmail-fetched-sources (list t))
11389             i new)
11390         (setq gnus-newsgroup-active
11391               (gnus-activate-group gnus-newsgroup-name 'scan))
11392         (setq i (cdr gnus-newsgroup-active))
11393         (while (> i (cdr old-active))
11394           (push i new)
11395           (decf i))
11396         (if (not new)
11397             (message "No gnus is bad news.")
11398           (gnus-summary-insert-articles new)
11399           (setq gnus-newsgroup-unreads
11400                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11401           (gnus-summary-limit (gnus-sorted-nunion old new))))
11402     (gnus-summary-position-point)))
11403
11404 (gnus-summary-make-all-marking-commands)
11405
11406 (gnus-ems-redefine)
11407
11408 (provide 'gnus-sum)
11409
11410 (run-hooks 'gnus-sum-load-hook)
11411
11412 ;;; gnus-sum.el ends here