* gnus-sum.el (gnus-summary-wash-hide-map): Remove
[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   "B" gnus-article-strip-banner
1818   "P" gnus-article-hide-pem
1819   "\C-c" gnus-article-hide-citation-maybe)
1820
1821 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1822   "a" gnus-article-highlight
1823   "h" gnus-article-highlight-headers
1824   "c" gnus-article-highlight-citation
1825   "s" gnus-article-highlight-signature)
1826
1827 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1828   "f" gnus-article-treat-fold-headers
1829   "u" gnus-article-treat-unfold-headers
1830   "n" gnus-article-treat-fold-newsgroups)
1831
1832 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1833   "x" gnus-article-display-x-face
1834   "d" gnus-article-display-face
1835   "s" gnus-treat-smiley
1836   "D" gnus-article-remove-images
1837   "f" gnus-treat-from-picon
1838   "m" gnus-treat-mail-picon
1839   "n" gnus-treat-newsgroups-picon)
1840
1841 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1842   "w" gnus-article-decode-mime-words
1843   "c" gnus-article-decode-charset
1844   "v" gnus-mime-view-all-parts
1845   "b" gnus-article-view-part)
1846
1847 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1848   "z" gnus-article-date-ut
1849   "u" gnus-article-date-ut
1850   "l" gnus-article-date-local
1851   "p" gnus-article-date-english
1852   "e" gnus-article-date-lapsed
1853   "o" gnus-article-date-original
1854   "i" gnus-article-date-iso8601
1855   "s" gnus-article-date-user)
1856
1857 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1858   "t" gnus-article-remove-trailing-blank-lines
1859   "l" gnus-article-strip-leading-blank-lines
1860   "m" gnus-article-strip-multiple-blank-lines
1861   "a" gnus-article-strip-blank-lines
1862   "A" gnus-article-strip-all-blank-lines
1863   "s" gnus-article-strip-leading-space
1864   "e" gnus-article-strip-trailing-space
1865   "w" gnus-article-remove-leading-whitespace)
1866
1867 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1868   "v" gnus-version
1869   "f" gnus-summary-fetch-faq
1870   "d" gnus-summary-describe-group
1871   "h" gnus-summary-describe-briefly
1872   "i" gnus-info-find-node
1873   "c" gnus-group-fetch-charter
1874   "C" gnus-group-fetch-control)
1875
1876 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1877   "e" gnus-summary-expire-articles
1878   "\M-\C-e" gnus-summary-expire-articles-now
1879   "\177" gnus-summary-delete-article
1880   [delete] gnus-summary-delete-article
1881   [backspace] gnus-summary-delete-article
1882   "m" gnus-summary-move-article
1883   "r" gnus-summary-respool-article
1884   "w" gnus-summary-edit-article
1885   "c" gnus-summary-copy-article
1886   "B" gnus-summary-crosspost-article
1887   "q" gnus-summary-respool-query
1888   "t" gnus-summary-respool-trace
1889   "i" gnus-summary-import-article
1890   "I" gnus-summary-create-article
1891   "p" gnus-summary-article-posted-p)
1892
1893 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1894   "o" gnus-summary-save-article
1895   "m" gnus-summary-save-article-mail
1896   "F" gnus-summary-write-article-file
1897   "r" gnus-summary-save-article-rmail
1898   "f" gnus-summary-save-article-file
1899   "b" gnus-summary-save-article-body-file
1900   "h" gnus-summary-save-article-folder
1901   "v" gnus-summary-save-article-vm
1902   "p" gnus-summary-pipe-output
1903   "P" gnus-summary-muttprint
1904   "s" gnus-soup-add-article)
1905
1906 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1907   "b" gnus-summary-display-buttonized
1908   "m" gnus-summary-repair-multipart
1909   "v" gnus-article-view-part
1910   "o" gnus-article-save-part
1911   "c" gnus-article-copy-part
1912   "C" gnus-article-view-part-as-charset
1913   "e" gnus-article-view-part-externally
1914   "E" gnus-article-encrypt-body
1915   "i" gnus-article-inline-part
1916   "|" gnus-article-pipe-part)
1917
1918 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1919   "p" gnus-summary-mark-as-processable
1920   "u" gnus-summary-unmark-as-processable
1921   "U" gnus-summary-unmark-all-processable
1922   "v" gnus-uu-mark-over
1923   "s" gnus-uu-mark-series
1924   "r" gnus-uu-mark-region
1925   "g" gnus-uu-unmark-region
1926   "R" gnus-uu-mark-by-regexp
1927   "G" gnus-uu-unmark-by-regexp
1928   "t" gnus-uu-mark-thread
1929   "T" gnus-uu-unmark-thread
1930   "a" gnus-uu-mark-all
1931   "b" gnus-uu-mark-buffer
1932   "S" gnus-uu-mark-sparse
1933   "k" gnus-summary-kill-process-mark
1934   "y" gnus-summary-yank-process-mark
1935   "w" gnus-summary-save-process-mark
1936   "i" gnus-uu-invert-processable)
1937
1938 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1939   ;;"x" gnus-uu-extract-any
1940   "m" gnus-summary-save-parts
1941   "u" gnus-uu-decode-uu
1942   "U" gnus-uu-decode-uu-and-save
1943   "s" gnus-uu-decode-unshar
1944   "S" gnus-uu-decode-unshar-and-save
1945   "o" gnus-uu-decode-save
1946   "O" gnus-uu-decode-save
1947   "b" gnus-uu-decode-binhex
1948   "B" gnus-uu-decode-binhex
1949   "p" gnus-uu-decode-postscript
1950   "P" gnus-uu-decode-postscript-and-save)
1951
1952 (gnus-define-keys
1953     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
1954   "u" gnus-uu-decode-uu-view
1955   "U" gnus-uu-decode-uu-and-save-view
1956   "s" gnus-uu-decode-unshar-view
1957   "S" gnus-uu-decode-unshar-and-save-view
1958   "o" gnus-uu-decode-save-view
1959   "O" gnus-uu-decode-save-view
1960   "b" gnus-uu-decode-binhex-view
1961   "B" gnus-uu-decode-binhex-view
1962   "p" gnus-uu-decode-postscript-view
1963   "P" gnus-uu-decode-postscript-and-save-view)
1964
1965 (defvar gnus-article-post-menu nil)
1966
1967 (defconst gnus-summary-menu-maxlen 20)
1968
1969 (defun gnus-summary-menu-split (menu)
1970   ;; If we have lots of elements, divide them into groups of 20
1971   ;; and make a pane (or submenu) for each one.
1972   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
1973       (let ((menu menu) sublists next
1974             (i 1))
1975         (while menu
1976           ;; Pull off the next gnus-summary-menu-maxlen elements
1977           ;; and make them the next element of sublist.
1978           (setq next (nthcdr gnus-summary-menu-maxlen menu))
1979           (if next
1980               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
1981                       nil))
1982           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
1983                                              (aref (car (last menu)) 0)) menu)
1984                                sublists))
1985           (setq i (1+ i))
1986           (setq menu next))
1987         (nreverse sublists))
1988     ;; Few elements--put them all in one pane.
1989     menu))
1990
1991 (defun gnus-summary-make-menu-bar ()
1992   (gnus-turn-off-edit-menu 'summary)
1993
1994   (unless (boundp 'gnus-summary-misc-menu)
1995
1996     (easy-menu-define
1997       gnus-summary-kill-menu gnus-summary-mode-map ""
1998       (cons
1999        "Score"
2000        (nconc
2001         (list
2002          ["Customize" gnus-score-customize t])
2003         (gnus-make-score-map 'increase)
2004         (gnus-make-score-map 'lower)
2005         '(("Mark"
2006            ["Kill below" gnus-summary-kill-below t]
2007            ["Mark above" gnus-summary-mark-above t]
2008            ["Tick above" gnus-summary-tick-above t]
2009            ["Clear above" gnus-summary-clear-above t])
2010           ["Current score" gnus-summary-current-score t]
2011           ["Set score" gnus-summary-set-score t]
2012           ["Switch current score file..." gnus-score-change-score-file t]
2013           ["Set mark below..." gnus-score-set-mark-below t]
2014           ["Set expunge below..." gnus-score-set-expunge-below t]
2015           ["Edit current score file" gnus-score-edit-current-scores t]
2016           ["Edit score file" gnus-score-edit-file t]
2017           ["Trace score" gnus-score-find-trace t]
2018           ["Find words" gnus-score-find-favourite-words t]
2019           ["Rescore buffer" gnus-summary-rescore t]
2020           ["Increase score..." gnus-summary-increase-score t]
2021           ["Lower score..." gnus-summary-lower-score t]))))
2022
2023     ;; Define both the Article menu in the summary buffer and the
2024     ;; equivalent Commands menu in the article buffer here for
2025     ;; consistency.
2026     (let ((innards
2027            `(("Hide"
2028               ["All" gnus-article-hide t]
2029               ["Headers" gnus-article-hide-headers t]
2030               ["Signature" gnus-article-hide-signature t]
2031               ["Citation" gnus-article-hide-citation t]
2032               ["List identifiers" gnus-article-hide-list-identifiers t]
2033               ["Banner" gnus-article-strip-banner t]
2034               ["Boring headers" gnus-article-hide-boring-headers t])
2035              ("Highlight"
2036               ["All" gnus-article-highlight t]
2037               ["Headers" gnus-article-highlight-headers t]
2038               ["Signature" gnus-article-highlight-signature t]
2039               ["Citation" gnus-article-highlight-citation t])
2040              ("MIME"
2041               ["Words" gnus-article-decode-mime-words t]
2042               ["Charset" gnus-article-decode-charset t]
2043               ["QP" gnus-article-de-quoted-unreadable t]
2044               ["Base64" gnus-article-de-base64-unreadable t]
2045               ["View MIME buttons" gnus-summary-display-buttonized t]
2046               ["View all" gnus-mime-view-all-parts t]
2047               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2048               ["Encrypt body" gnus-article-encrypt-body t]
2049               ["Extract all parts" gnus-summary-save-parts t])
2050              ("Date"
2051               ["Local" gnus-article-date-local t]
2052               ["ISO8601" gnus-article-date-iso8601 t]
2053               ["UT" gnus-article-date-ut t]
2054               ["Original" gnus-article-date-original t]
2055               ["Lapsed" gnus-article-date-lapsed t]
2056               ["User-defined" gnus-article-date-user t])
2057              ("Display"
2058               ["Remove images" gnus-article-remove-images t]
2059               ["Toggle smiley" gnus-treat-smiley t]
2060               ["Show X-Face" gnus-article-display-x-face t]
2061               ["Show picons in From" gnus-treat-from-picon t]
2062               ["Show picons in mail headers" gnus-treat-mail-picon t]
2063               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2064               ("View as different encoding"
2065                ,@(gnus-summary-menu-split
2066                   (mapcar
2067                    (lambda (cs)
2068                      ;; Since easymenu under FSF Emacs doesn't allow lambda
2069                      ;; forms for menu commands, we should provide intern'ed
2070                      ;; function symbols.
2071                      (let ((command (intern (format "\
2072 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2073                        (fset command
2074                              `(lambda ()
2075                                 (interactive)
2076                                 (let ((gnus-summary-show-article-charset-alist
2077                                        '((1 . ,cs))))
2078                                   (gnus-summary-show-article 1))))
2079                        `[,(symbol-name cs) ,command t]))
2080                    (sort (if (fboundp 'coding-system-list)
2081                              (coding-system-list)
2082                            (mapcar 'car mm-mime-mule-charset-alist))
2083                          'string<)))))
2084              ("Washing"
2085               ("Remove Blanks"
2086                ["Leading" gnus-article-strip-leading-blank-lines t]
2087                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2088                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2089                ["All of the above" gnus-article-strip-blank-lines t]
2090                ["All" gnus-article-strip-all-blank-lines t]
2091                ["Leading space" gnus-article-strip-leading-space t]
2092                ["Trailing space" gnus-article-strip-trailing-space t]
2093                ["Leading space in headers"
2094                 gnus-article-remove-leading-whitespace t])
2095               ["Overstrike" gnus-article-treat-overstrike t]
2096               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2097               ["Emphasis" gnus-article-emphasize t]
2098               ["Word wrap" gnus-article-fill-cited-article t]
2099               ["Fill long lines" gnus-article-fill-long-lines t]
2100               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2101               ["CR" gnus-article-remove-cr t]
2102               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2103               ["Base64" gnus-article-de-base64-unreadable t]
2104               ["Rot 13" gnus-summary-caesar-message
2105                ,@(if (featurep 'xemacs) '(t)
2106                    '(:help "\"Caesar rotate\" article by 13"))]
2107               ["Morse decode" gnus-summary-morse-message t]
2108               ["Unix pipe..." gnus-summary-pipe-message t]
2109               ["Add buttons" gnus-article-add-buttons t]
2110               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2111               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2112               ["Verbose header" gnus-summary-verbose-headers t]
2113               ["Toggle header" gnus-summary-toggle-header t]
2114               ["Unfold headers" gnus-article-treat-unfold-headers t]
2115               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2116               ["Html" gnus-article-wash-html t]
2117               ["URLs" gnus-article-unsplit-urls t]
2118               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2119               ["HZ" gnus-article-decode-HZ t]
2120               ("(Outlook) Deuglify"
2121                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2122                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2123                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2124                ["Full (Outlook) deuglify"
2125                 gnus-article-outlook-deuglify-article t])
2126               )
2127              ("Output"
2128               ["Save in default format" gnus-summary-save-article
2129                ,@(if (featurep 'xemacs) '(t)
2130                    '(:help "Save article using default method"))]
2131               ["Save in file" gnus-summary-save-article-file
2132                ,@(if (featurep 'xemacs) '(t)
2133                    '(:help "Save article in file"))]
2134               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2135               ["Save in MH folder" gnus-summary-save-article-folder t]
2136               ["Save in VM folder" gnus-summary-save-article-vm t]
2137               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2138               ["Save body in file" gnus-summary-save-article-body-file t]
2139               ["Pipe through a filter" gnus-summary-pipe-output t]
2140               ["Add to SOUP packet" gnus-soup-add-article t]
2141               ["Print with Muttprint" gnus-summary-muttprint t]
2142               ["Print" gnus-summary-print-article t])
2143              ("Backend"
2144               ["Respool article..." gnus-summary-respool-article t]
2145               ["Move article..." gnus-summary-move-article
2146                (gnus-check-backend-function
2147                 'request-move-article gnus-newsgroup-name)]
2148               ["Copy article..." gnus-summary-copy-article t]
2149               ["Crosspost article..." gnus-summary-crosspost-article
2150                (gnus-check-backend-function
2151                 'request-replace-article gnus-newsgroup-name)]
2152               ["Import file..." gnus-summary-import-article t]
2153               ["Create article..." gnus-summary-create-article t]
2154               ["Check if posted" gnus-summary-article-posted-p t]
2155               ["Edit article" gnus-summary-edit-article
2156                (not (gnus-group-read-only-p))]
2157               ["Delete article" gnus-summary-delete-article
2158                (gnus-check-backend-function
2159                 'request-expire-articles gnus-newsgroup-name)]
2160               ["Query respool" gnus-summary-respool-query t]
2161               ["Trace respool" gnus-summary-respool-trace t]
2162               ["Delete expirable articles" gnus-summary-expire-articles-now
2163                (gnus-check-backend-function
2164                 'request-expire-articles gnus-newsgroup-name)])
2165              ("Extract"
2166               ["Uudecode" gnus-uu-decode-uu
2167                ,@(if (featurep 'xemacs) '(t)
2168                    '(:help "Decode uuencoded article(s)"))]
2169               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2170               ["Unshar" gnus-uu-decode-unshar t]
2171               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2172               ["Save" gnus-uu-decode-save t]
2173               ["Binhex" gnus-uu-decode-binhex t]
2174               ["Postscript" gnus-uu-decode-postscript t]
2175               ["All MIME parts" gnus-summary-save-parts t])
2176              ("Cache"
2177               ["Enter article" gnus-cache-enter-article t]
2178               ["Remove article" gnus-cache-remove-article t])
2179              ["Translate" gnus-article-babel t]
2180              ["Select article buffer" gnus-summary-select-article-buffer t]
2181              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2182              ["Isearch article..." gnus-summary-isearch-article t]
2183              ["Beginning of the article" gnus-summary-beginning-of-article t]
2184              ["End of the article" gnus-summary-end-of-article t]
2185              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2186              ["Fetch referenced articles" gnus-summary-refer-references t]
2187              ["Fetch current thread" gnus-summary-refer-thread t]
2188              ["Fetch article with id..." gnus-summary-refer-article t]
2189              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2190              ["Redisplay" gnus-summary-show-article t]
2191              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2192       (easy-menu-define
2193         gnus-summary-article-menu gnus-summary-mode-map ""
2194         (cons "Article" innards))
2195
2196       (if (not (keymapp gnus-summary-article-menu))
2197           (easy-menu-define
2198             gnus-article-commands-menu gnus-article-mode-map ""
2199             (cons "Commands" innards))
2200         ;; in Emacs, don't share menu.
2201         (setq gnus-article-commands-menu
2202               (copy-keymap gnus-summary-article-menu))
2203         (define-key gnus-article-mode-map [menu-bar commands]
2204           (cons "Commands" gnus-article-commands-menu))))
2205
2206     (easy-menu-define
2207       gnus-summary-thread-menu gnus-summary-mode-map ""
2208       '("Threads"
2209         ["Find all messages in thread" gnus-summary-refer-thread t]
2210         ["Toggle threading" gnus-summary-toggle-threads t]
2211         ["Hide threads" gnus-summary-hide-all-threads t]
2212         ["Show threads" gnus-summary-show-all-threads t]
2213         ["Hide thread" gnus-summary-hide-thread t]
2214         ["Show thread" gnus-summary-show-thread t]
2215         ["Go to next thread" gnus-summary-next-thread t]
2216         ["Go to previous thread" gnus-summary-prev-thread t]
2217         ["Go down thread" gnus-summary-down-thread t]
2218         ["Go up thread" gnus-summary-up-thread t]
2219         ["Top of thread" gnus-summary-top-thread t]
2220         ["Mark thread as read" gnus-summary-kill-thread t]
2221         ["Lower thread score" gnus-summary-lower-thread t]
2222         ["Raise thread score" gnus-summary-raise-thread t]
2223         ["Rethread current" gnus-summary-rethread-current t]))
2224
2225     (easy-menu-define
2226       gnus-summary-post-menu gnus-summary-mode-map ""
2227       `("Post"
2228         ["Send a message (mail or news)" gnus-summary-post-news
2229          ,@(if (featurep 'xemacs) '(t)
2230              '(:help "Post an article"))]
2231         ["Followup" gnus-summary-followup
2232          ,@(if (featurep 'xemacs) '(t)
2233              '(:help "Post followup to this article"))]
2234         ["Followup and yank" gnus-summary-followup-with-original
2235          ,@(if (featurep 'xemacs) '(t)
2236              '(:help "Post followup to this article, quoting its contents"))]
2237         ["Supersede article" gnus-summary-supersede-article t]
2238         ["Cancel article" gnus-summary-cancel-article
2239          ,@(if (featurep 'xemacs) '(t)
2240              '(:help "Cancel an article you posted"))]
2241         ["Reply" gnus-summary-reply t]
2242         ["Reply and yank" gnus-summary-reply-with-original t]
2243         ["Wide reply" gnus-summary-wide-reply t]
2244         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2245          ,@(if (featurep 'xemacs) '(t)
2246              '(:help "Mail a reply, quoting this article"))]
2247         ["Very wide reply" gnus-summary-very-wide-reply t]
2248         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2249          ,@(if (featurep 'xemacs) '(t)
2250              '(:help "Mail a very wide reply, quoting this article"))]
2251         ["Mail forward" gnus-summary-mail-forward t]
2252         ["Post forward" gnus-summary-post-forward t]
2253         ["Digest and mail" gnus-uu-digest-mail-forward t]
2254         ["Digest and post" gnus-uu-digest-post-forward t]
2255         ["Resend message" gnus-summary-resend-message t]
2256         ["Resend message edit" gnus-summary-resend-message-edit t]
2257         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2258         ["Send a mail" gnus-summary-mail-other-window t]
2259         ["Create a local message" gnus-summary-news-other-window t]
2260         ["Uuencode and post" gnus-uu-post-news
2261          ,@(if (featurep 'xemacs) '(t)
2262              '(:help "Post a uuencoded article"))]
2263         ["Followup via news" gnus-summary-followup-to-mail t]
2264         ["Followup via news and yank"
2265          gnus-summary-followup-to-mail-with-original t]
2266         ;;("Draft"
2267         ;;["Send" gnus-summary-send-draft t]
2268         ;;["Send bounced" gnus-resend-bounced-mail t])
2269         ))
2270
2271     (cond
2272      ((not (keymapp gnus-summary-post-menu))
2273       (setq gnus-article-post-menu gnus-summary-post-menu))
2274      ((not gnus-article-post-menu)
2275       ;; Don't share post menu.
2276       (setq gnus-article-post-menu
2277             (copy-keymap gnus-summary-post-menu))))
2278     (define-key gnus-article-mode-map [menu-bar post]
2279       (cons "Post" gnus-article-post-menu))
2280
2281     (easy-menu-define
2282       gnus-summary-misc-menu gnus-summary-mode-map ""
2283       `("Gnus"
2284         ("Mark Read"
2285          ["Mark as read" gnus-summary-mark-as-read-forward t]
2286          ["Mark same subject and select"
2287           gnus-summary-kill-same-subject-and-select t]
2288          ["Mark same subject" gnus-summary-kill-same-subject t]
2289          ["Catchup" gnus-summary-catchup
2290           ,@(if (featurep 'xemacs) '(t)
2291               '(:help "Mark unread articles in this group as read"))]
2292          ["Catchup all" gnus-summary-catchup-all t]
2293          ["Catchup to here" gnus-summary-catchup-to-here t]
2294          ["Catchup from here" gnus-summary-catchup-from-here t]
2295          ["Catchup region" gnus-summary-mark-region-as-read 
2296           (gnus-mark-active-p)]
2297          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2298         ("Mark Various"
2299          ["Tick" gnus-summary-tick-article-forward t]
2300          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2301          ["Remove marks" gnus-summary-clear-mark-forward t]
2302          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2303          ["Set bookmark" gnus-summary-set-bookmark t]
2304          ["Remove bookmark" gnus-summary-remove-bookmark t])
2305         ("Limit to"
2306          ["Marks..." gnus-summary-limit-to-marks t]
2307          ["Subject..." gnus-summary-limit-to-subject t]
2308          ["Author..." gnus-summary-limit-to-author t]
2309          ["Age..." gnus-summary-limit-to-age t]
2310          ["Extra..." gnus-summary-limit-to-extra t]
2311          ["Score..." gnus-summary-limit-to-score t]
2312          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2313          ["Unread" gnus-summary-limit-to-unread t]
2314          ["Unseen" gnus-summary-limit-to-unseen t]
2315          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2316          ["Next articles" gnus-summary-limit-to-articles t]
2317          ["Pop limit" gnus-summary-pop-limit t]
2318          ["Show dormant" gnus-summary-limit-include-dormant t]
2319          ["Hide childless dormant"
2320           gnus-summary-limit-exclude-childless-dormant t]
2321          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2322          ["Hide marked" gnus-summary-limit-exclude-marks t]
2323          ["Show expunged" gnus-summary-limit-include-expunged t])
2324         ("Process Mark"
2325          ["Set mark" gnus-summary-mark-as-processable t]
2326          ["Remove mark" gnus-summary-unmark-as-processable t]
2327          ["Remove all marks" gnus-summary-unmark-all-processable t]
2328          ["Mark above" gnus-uu-mark-over t]
2329          ["Mark series" gnus-uu-mark-series t]
2330          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2331          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2332          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2333          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2334          ["Mark all" gnus-uu-mark-all t]
2335          ["Mark buffer" gnus-uu-mark-buffer t]
2336          ["Mark sparse" gnus-uu-mark-sparse t]
2337          ["Mark thread" gnus-uu-mark-thread t]
2338          ["Unmark thread" gnus-uu-unmark-thread t]
2339          ("Process Mark Sets"
2340           ["Kill" gnus-summary-kill-process-mark t]
2341           ["Yank" gnus-summary-yank-process-mark
2342            gnus-newsgroup-process-stack]
2343           ["Save" gnus-summary-save-process-mark t]))
2344         ("Scroll article"
2345          ["Page forward" gnus-summary-next-page
2346           ,@(if (featurep 'xemacs) '(t)
2347               '(:help "Show next page of article"))]
2348          ["Page backward" gnus-summary-prev-page
2349           ,@(if (featurep 'xemacs) '(t)
2350               '(:help "Show previous page of article"))]
2351          ["Line forward" gnus-summary-scroll-up t])
2352         ("Move"
2353          ["Next unread article" gnus-summary-next-unread-article t]
2354          ["Previous unread article" gnus-summary-prev-unread-article t]
2355          ["Next article" gnus-summary-next-article t]
2356          ["Previous article" gnus-summary-prev-article t]
2357          ["Next unread subject" gnus-summary-next-unread-subject t]
2358          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2359          ["Next article same subject" gnus-summary-next-same-subject t]
2360          ["Previous article same subject" gnus-summary-prev-same-subject t]
2361          ["First unread article" gnus-summary-first-unread-article t]
2362          ["Best unread article" gnus-summary-best-unread-article t]
2363          ["Go to subject number..." gnus-summary-goto-subject t]
2364          ["Go to article number..." gnus-summary-goto-article t]
2365          ["Go to the last article" gnus-summary-goto-last-article t]
2366          ["Pop article off history" gnus-summary-pop-article t])
2367         ("Sort"
2368          ["Sort by number" gnus-summary-sort-by-number t]
2369          ["Sort by author" gnus-summary-sort-by-author t]
2370          ["Sort by subject" gnus-summary-sort-by-subject t]
2371          ["Sort by date" gnus-summary-sort-by-date t]
2372          ["Sort by score" gnus-summary-sort-by-score t]
2373          ["Sort by lines" gnus-summary-sort-by-lines t]
2374          ["Sort by characters" gnus-summary-sort-by-chars t]
2375          ["Randomize" gnus-summary-sort-by-random t]
2376          ["Original sort" gnus-summary-sort-by-original t])
2377         ("Help"
2378          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2379          ["Describe group" gnus-summary-describe-group t]
2380          ["Fetch charter" gnus-group-fetch-charter
2381           ,@(if (featurep 'xemacs) nil
2382               '(:help "Display the charter of the current group"))]
2383          ["Fetch control message" gnus-group-fetch-control
2384           ,@(if (featurep 'xemacs) nil
2385               '(:help "Display the archived control message for the current group"))]
2386          ["Read manual" gnus-info-find-node t])
2387         ("Modes"
2388          ["Pick and read" gnus-pick-mode t]
2389          ["Binary" gnus-binary-mode t])
2390         ("Regeneration"
2391          ["Regenerate" gnus-summary-prepare t]
2392          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2393          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2394          ["Toggle threading" gnus-summary-toggle-threads t])
2395         ["See old articles" gnus-summary-insert-old-articles t]
2396         ["See new articles" gnus-summary-insert-new-articles t]
2397         ["Filter articles..." gnus-summary-execute-command t]
2398         ["Run command on subjects..." gnus-summary-universal-argument t]
2399         ["Search articles forward..." gnus-summary-search-article-forward t]
2400         ["Search articles backward..." gnus-summary-search-article-backward t]
2401         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2402         ["Expand window" gnus-summary-expand-window t]
2403         ["Expire expirable articles" gnus-summary-expire-articles
2404          (gnus-check-backend-function
2405           'request-expire-articles gnus-newsgroup-name)]
2406         ["Edit local kill file" gnus-summary-edit-local-kill t]
2407         ["Edit main kill file" gnus-summary-edit-global-kill t]
2408         ["Edit group parameters" gnus-summary-edit-parameters t]
2409         ["Customize group parameters" gnus-summary-customize-parameters t]
2410         ["Send a bug report" gnus-bug t]
2411         ("Exit"
2412          ["Catchup and exit" gnus-summary-catchup-and-exit
2413           ,@(if (featurep 'xemacs) '(t)
2414               '(:help "Mark unread articles in this group as read, then exit"))]
2415          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2416          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2417          ["Exit group" gnus-summary-exit
2418           ,@(if (featurep 'xemacs) '(t)
2419               '(:help "Exit current group, return to group selection mode"))]
2420          ["Exit group without updating" gnus-summary-exit-no-update t]
2421          ["Exit and goto next group" gnus-summary-next-group t]
2422          ["Exit and goto prev group" gnus-summary-prev-group t]
2423          ["Reselect group" gnus-summary-reselect-current-group t]
2424          ["Rescan group" gnus-summary-rescan-group t]
2425          ["Update dribble" gnus-summary-save-newsrc t])))
2426
2427     (gnus-run-hooks 'gnus-summary-menu-hook)))
2428
2429 (defvar gnus-summary-tool-bar-map nil)
2430
2431 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2432 (defun gnus-summary-make-tool-bar ()
2433   (if (and (fboundp 'tool-bar-add-item-from-menu)
2434            (default-value 'tool-bar-mode)
2435            (not gnus-summary-tool-bar-map))
2436       (setq gnus-summary-tool-bar-map
2437             (let ((tool-bar-map (make-sparse-keymap))
2438                   (load-path (mm-image-load-path)))
2439               (tool-bar-add-item-from-menu
2440                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2441               (tool-bar-add-item-from-menu
2442                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2443               (tool-bar-add-item-from-menu
2444                'gnus-summary-post-news "post" gnus-summary-mode-map)
2445               (tool-bar-add-item-from-menu
2446                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2447               (tool-bar-add-item-from-menu
2448                'gnus-summary-followup "followup" gnus-summary-mode-map)
2449               (tool-bar-add-item-from-menu
2450                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2451               (tool-bar-add-item-from-menu
2452                'gnus-summary-reply "reply" gnus-summary-mode-map)
2453               (tool-bar-add-item-from-menu
2454                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2455               (tool-bar-add-item-from-menu
2456                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2457               (tool-bar-add-item-from-menu
2458                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2459               (tool-bar-add-item-from-menu
2460                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2461               (tool-bar-add-item-from-menu
2462                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2463               (tool-bar-add-item-from-menu
2464                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2465               (tool-bar-add-item-from-menu
2466                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2467               (tool-bar-add-item-from-menu
2468                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2469               tool-bar-map)))
2470   (if gnus-summary-tool-bar-map
2471       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2472
2473 (defun gnus-score-set-default (var value)
2474   "A version of set that updates the GNU Emacs menu-bar."
2475   (set var value)
2476   ;; It is the message that forces the active status to be updated.
2477   (message ""))
2478
2479 (defun gnus-make-score-map (type)
2480   "Make a summary score map of type TYPE."
2481   (if t
2482       nil
2483     (let ((headers '(("author" "from" string)
2484                      ("subject" "subject" string)
2485                      ("article body" "body" string)
2486                      ("article head" "head" string)
2487                      ("xref" "xref" string)
2488                      ("extra header" "extra" string)
2489                      ("lines" "lines" number)
2490                      ("followups to author" "followup" string)))
2491           (types '((number ("less than" <)
2492                            ("greater than" >)
2493                            ("equal" =))
2494                    (string ("substring" s)
2495                            ("exact string" e)
2496                            ("fuzzy string" f)
2497                            ("regexp" r))))
2498           (perms '(("temporary" (current-time-string))
2499                    ("permanent" nil)
2500                    ("immediate" now)))
2501           header)
2502       (list
2503        (apply
2504         'nconc
2505         (list
2506          (if (eq type 'lower)
2507              "Lower score"
2508            "Increase score"))
2509         (let (outh)
2510           (while headers
2511             (setq header (car headers))
2512             (setq outh
2513                   (cons
2514                    (apply
2515                     'nconc
2516                     (list (car header))
2517                     (let ((ts (cdr (assoc (nth 2 header) types)))
2518                           outt)
2519                       (while ts
2520                         (setq outt
2521                               (cons
2522                                (apply
2523                                 'nconc
2524                                 (list (caar ts))
2525                                 (let ((ps perms)
2526                                       outp)
2527                                   (while ps
2528                                     (setq outp
2529                                           (cons
2530                                            (vector
2531                                             (caar ps)
2532                                             (list
2533                                              'gnus-summary-score-entry
2534                                              (nth 1 header)
2535                                              (if (or (string= (nth 1 header)
2536                                                               "head")
2537                                                      (string= (nth 1 header)
2538                                                               "body"))
2539                                                  ""
2540                                                (list 'gnus-summary-header
2541                                                      (nth 1 header)))
2542                                              (list 'quote (nth 1 (car ts)))
2543                                              (list 'gnus-score-delta-default
2544                                                    nil)
2545                                              (nth 1 (car ps))
2546                                              t)
2547                                             t)
2548                                            outp))
2549                                     (setq ps (cdr ps)))
2550                                   (list (nreverse outp))))
2551                                outt))
2552                         (setq ts (cdr ts)))
2553                       (list (nreverse outt))))
2554                    outh))
2555             (setq headers (cdr headers)))
2556           (list (nreverse outh))))))))
2557
2558 \f
2559
2560 (defun gnus-summary-mode (&optional group)
2561   "Major mode for reading articles.
2562
2563 All normal editing commands are switched off.
2564 \\<gnus-summary-mode-map>
2565 Each line in this buffer represents one article.  To read an
2566 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2567 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2568 respectively.
2569
2570 You can also post articles and send mail from this buffer.  To
2571 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2572 of an article, type `\\[gnus-summary-reply]'.
2573
2574 There are approx. one gazillion commands you can execute in this
2575 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2576
2577 The following commands are available:
2578
2579 \\{gnus-summary-mode-map}"
2580   (interactive)
2581   (kill-all-local-variables)
2582   (when (gnus-visual-p 'summary-menu 'menu)
2583     (gnus-summary-make-menu-bar)
2584     (gnus-summary-make-tool-bar))
2585   (gnus-summary-make-local-variables)
2586   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2587     (gnus-summary-make-local-variables))
2588   (gnus-make-thread-indent-array)
2589   (gnus-simplify-mode-line)
2590   (setq major-mode 'gnus-summary-mode)
2591   (setq mode-name "Summary")
2592   (make-local-variable 'minor-mode-alist)
2593   (use-local-map gnus-summary-mode-map)
2594   (buffer-disable-undo)
2595   (setq buffer-read-only t)             ;Disable modification
2596   (setq truncate-lines t)
2597   (setq selective-display t)
2598   (setq selective-display-ellipses t)   ;Display `...'
2599   (gnus-summary-set-display-table)
2600   (gnus-set-default-directory)
2601   (setq gnus-newsgroup-name group)
2602   (make-local-variable 'gnus-summary-line-format)
2603   (make-local-variable 'gnus-summary-line-format-spec)
2604   (make-local-variable 'gnus-summary-dummy-line-format)
2605   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2606   (make-local-variable 'gnus-summary-mark-positions)
2607   (make-local-hook 'pre-command-hook)
2608   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2609   (gnus-run-hooks 'gnus-summary-mode-hook)
2610   (turn-on-gnus-mailing-list-mode)
2611   (mm-enable-multibyte)
2612   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2613   (gnus-update-summary-mark-positions))
2614
2615 (defun gnus-summary-make-local-variables ()
2616   "Make all the local summary buffer variables."
2617   (let (global)
2618     (dolist (local gnus-summary-local-variables)
2619       (if (consp local)
2620           (progn
2621             (if (eq (cdr local) 'global)
2622                 ;; Copy the global value of the variable.
2623                 (setq global (symbol-value (car local)))
2624               ;; Use the value from the list.
2625               (setq global (eval (cdr local))))
2626             (set (make-local-variable (car local)) global))
2627         ;; Simple nil-valued local variable.
2628         (set (make-local-variable local) nil)))))
2629
2630 (defun gnus-summary-clear-local-variables ()
2631   (let ((locals gnus-summary-local-variables))
2632     (while locals
2633       (if (consp (car locals))
2634           (and (vectorp (caar locals))
2635                (set (caar locals) nil))
2636         (and (vectorp (car locals))
2637              (set (car locals) nil)))
2638       (setq locals (cdr locals)))))
2639
2640 ;; Summary data functions.
2641
2642 (defmacro gnus-data-number (data)
2643   `(car ,data))
2644
2645 (defmacro gnus-data-set-number (data number)
2646   `(setcar ,data ,number))
2647
2648 (defmacro gnus-data-mark (data)
2649   `(nth 1 ,data))
2650
2651 (defmacro gnus-data-set-mark (data mark)
2652   `(setcar (nthcdr 1 ,data) ,mark))
2653
2654 (defmacro gnus-data-pos (data)
2655   `(nth 2 ,data))
2656
2657 (defmacro gnus-data-set-pos (data pos)
2658   `(setcar (nthcdr 2 ,data) ,pos))
2659
2660 (defmacro gnus-data-header (data)
2661   `(nth 3 ,data))
2662
2663 (defmacro gnus-data-set-header (data header)
2664   `(setf (nth 3 ,data) ,header))
2665
2666 (defmacro gnus-data-level (data)
2667   `(nth 4 ,data))
2668
2669 (defmacro gnus-data-unread-p (data)
2670   `(= (nth 1 ,data) gnus-unread-mark))
2671
2672 (defmacro gnus-data-read-p (data)
2673   `(/= (nth 1 ,data) gnus-unread-mark))
2674
2675 (defmacro gnus-data-pseudo-p (data)
2676   `(consp (nth 3 ,data)))
2677
2678 (defmacro gnus-data-find (number)
2679   `(assq ,number gnus-newsgroup-data))
2680
2681 (defmacro gnus-data-find-list (number &optional data)
2682   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2683      (memq (assq ,number bdata)
2684            bdata)))
2685
2686 (defmacro gnus-data-make (number mark pos header level)
2687   `(list ,number ,mark ,pos ,header ,level))
2688
2689 (defun gnus-data-enter (after-article number mark pos header level offset)
2690   (let ((data (gnus-data-find-list after-article)))
2691     (unless data
2692       (error "No such article: %d" after-article))
2693     (setcdr data (cons (gnus-data-make number mark pos header level)
2694                        (cdr data)))
2695     (setq gnus-newsgroup-data-reverse nil)
2696     (gnus-data-update-list (cddr data) offset)))
2697
2698 (defun gnus-data-enter-list (after-article list &optional offset)
2699   (when list
2700     (let ((data (and after-article (gnus-data-find-list after-article)))
2701           (ilist list))
2702       (if (not (or data
2703                    after-article))
2704           (let ((odata gnus-newsgroup-data))
2705             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2706             (when offset
2707               (gnus-data-update-list odata offset)))
2708       ;; Find the last element in the list to be spliced into the main
2709         ;; list.
2710         (while (cdr list)
2711           (setq list (cdr list)))
2712         (if (not data)
2713             (progn
2714               (setcdr list gnus-newsgroup-data)
2715               (setq gnus-newsgroup-data ilist)
2716               (when offset
2717                 (gnus-data-update-list (cdr list) offset)))
2718           (setcdr list (cdr data))
2719           (setcdr data ilist)
2720           (when offset
2721             (gnus-data-update-list (cdr list) offset))))
2722       (setq gnus-newsgroup-data-reverse nil))))
2723
2724 (defun gnus-data-remove (article &optional offset)
2725   (let ((data gnus-newsgroup-data))
2726     (if (= (gnus-data-number (car data)) article)
2727         (progn
2728           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2729                 gnus-newsgroup-data-reverse nil)
2730           (when offset
2731             (gnus-data-update-list gnus-newsgroup-data offset)))
2732       (while (cdr data)
2733         (when (= (gnus-data-number (cadr data)) article)
2734           (setcdr data (cddr data))
2735           (when offset
2736             (gnus-data-update-list (cdr data) offset))
2737           (setq data nil
2738                 gnus-newsgroup-data-reverse nil))
2739         (setq data (cdr data))))))
2740
2741 (defmacro gnus-data-list (backward)
2742   `(if ,backward
2743        (or gnus-newsgroup-data-reverse
2744            (setq gnus-newsgroup-data-reverse
2745                  (reverse gnus-newsgroup-data)))
2746      gnus-newsgroup-data))
2747
2748 (defun gnus-data-update-list (data offset)
2749   "Add OFFSET to the POS of all data entries in DATA."
2750   (setq gnus-newsgroup-data-reverse nil)
2751   (while data
2752     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2753     (setq data (cdr data))))
2754
2755 (defun gnus-summary-article-pseudo-p (article)
2756   "Say whether this article is a pseudo article or not."
2757   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2758
2759 (defmacro gnus-summary-article-sparse-p (article)
2760   "Say whether this article is a sparse article or not."
2761   `(memq ,article gnus-newsgroup-sparse))
2762
2763 (defmacro gnus-summary-article-ancient-p (article)
2764   "Say whether this article is a sparse article or not."
2765   `(memq ,article gnus-newsgroup-ancient))
2766
2767 (defun gnus-article-parent-p (number)
2768   "Say whether this article is a parent or not."
2769   (let ((data (gnus-data-find-list number)))
2770     (and (cdr data)              ; There has to be an article after...
2771          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2772             (gnus-data-level (nth 1 data))))))
2773
2774 (defun gnus-article-children (number)
2775   "Return a list of all children to NUMBER."
2776   (let* ((data (gnus-data-find-list number))
2777          (level (gnus-data-level (car data)))
2778          children)
2779     (setq data (cdr data))
2780     (while (and data
2781                 (= (gnus-data-level (car data)) (1+ level)))
2782       (push (gnus-data-number (car data)) children)
2783       (setq data (cdr data)))
2784     children))
2785
2786 (defmacro gnus-summary-skip-intangible ()
2787   "If the current article is intangible, then jump to a different article."
2788   '(let ((to (get-text-property (point) 'gnus-intangible)))
2789      (and to (gnus-summary-goto-subject to))))
2790
2791 (defmacro gnus-summary-article-intangible-p ()
2792   "Say whether this article is intangible or not."
2793   '(get-text-property (point) 'gnus-intangible))
2794
2795 (defun gnus-article-read-p (article)
2796   "Say whether ARTICLE is read or not."
2797   (not (or (memq article gnus-newsgroup-marked)
2798            (memq article gnus-newsgroup-spam-marked)
2799            (memq article gnus-newsgroup-unreads)
2800            (memq article gnus-newsgroup-unselected)
2801            (memq article gnus-newsgroup-dormant))))
2802
2803 ;; Some summary mode macros.
2804
2805 (defmacro gnus-summary-article-number ()
2806   "The article number of the article on the current line.
2807 If there isn't an article number here, then we return the current
2808 article number."
2809   '(progn
2810      (gnus-summary-skip-intangible)
2811      (or (get-text-property (point) 'gnus-number)
2812          (gnus-summary-last-subject))))
2813
2814 (defmacro gnus-summary-article-header (&optional number)
2815   "Return the header of article NUMBER."
2816   `(gnus-data-header (gnus-data-find
2817                       ,(or number '(gnus-summary-article-number)))))
2818
2819 (defmacro gnus-summary-thread-level (&optional number)
2820   "Return the level of thread that starts with article NUMBER."
2821   `(if (and (eq gnus-summary-make-false-root 'dummy)
2822             (get-text-property (point) 'gnus-intangible))
2823        0
2824      (gnus-data-level (gnus-data-find
2825                        ,(or number '(gnus-summary-article-number))))))
2826
2827 (defmacro gnus-summary-article-mark (&optional number)
2828   "Return the mark of article NUMBER."
2829   `(gnus-data-mark (gnus-data-find
2830                     ,(or number '(gnus-summary-article-number)))))
2831
2832 (defmacro gnus-summary-article-pos (&optional number)
2833   "Return the position of the line of article NUMBER."
2834   `(gnus-data-pos (gnus-data-find
2835                    ,(or number '(gnus-summary-article-number)))))
2836
2837 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2838 (defmacro gnus-summary-article-subject (&optional number)
2839   "Return current subject string or nil if nothing."
2840   `(let ((headers
2841           ,(if number
2842                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2843              '(gnus-data-header (assq (gnus-summary-article-number)
2844                                       gnus-newsgroup-data)))))
2845      (and headers
2846           (vectorp headers)
2847           (mail-header-subject headers))))
2848
2849 (defmacro gnus-summary-article-score (&optional number)
2850   "Return current article score."
2851   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2852                   gnus-newsgroup-scored))
2853        gnus-summary-default-score 0))
2854
2855 (defun gnus-summary-article-children (&optional number)
2856   "Return a list of article numbers that are children of article NUMBER."
2857   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2858          (level (gnus-data-level (car data)))
2859          l children)
2860     (while (and (setq data (cdr data))
2861                 (> (setq l (gnus-data-level (car data))) level))
2862       (and (= (1+ level) l)
2863            (push (gnus-data-number (car data))
2864                  children)))
2865     (nreverse children)))
2866
2867 (defun gnus-summary-article-parent (&optional number)
2868   "Return the article number of the parent of article NUMBER."
2869   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2870                                     (gnus-data-list t)))
2871          (level (gnus-data-level (car data))))
2872     (if (zerop level)
2873         ()                              ; This is a root.
2874       ;; We search until we find an article with a level less than
2875       ;; this one.  That function has to be the parent.
2876       (while (and (setq data (cdr data))
2877                   (not (< (gnus-data-level (car data)) level))))
2878       (and data (gnus-data-number (car data))))))
2879
2880 (defun gnus-unread-mark-p (mark)
2881   "Say whether MARK is the unread mark."
2882   (= mark gnus-unread-mark))
2883
2884 (defun gnus-read-mark-p (mark)
2885   "Say whether MARK is one of the marks that mark as read.
2886 This is all marks except unread, ticked, dormant, and expirable."
2887   (not (or (= mark gnus-unread-mark)
2888            (= mark gnus-ticked-mark)
2889            (= mark gnus-spam-mark)
2890            (= mark gnus-dormant-mark)
2891            (= mark gnus-expirable-mark))))
2892
2893 (defmacro gnus-article-mark (number)
2894   "Return the MARK of article NUMBER.
2895 This macro should only be used when computing the mark the \"first\"
2896 time; i.e., when generating the summary lines.  After that,
2897 `gnus-summary-article-mark' should be used to examine the
2898 marks of articles."
2899   `(cond
2900     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2901     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2902     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2903     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2904     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2905     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2906     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2907     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2908            gnus-ancient-mark))))
2909
2910 ;; Saving hidden threads.
2911
2912 (defmacro gnus-save-hidden-threads (&rest forms)
2913   "Save hidden threads, eval FORMS, and restore the hidden threads."
2914   (let ((config (make-symbol "config")))
2915     `(let ((,config (gnus-hidden-threads-configuration)))
2916        (unwind-protect
2917            (save-excursion
2918              ,@forms)
2919          (gnus-restore-hidden-threads-configuration ,config)))))
2920 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2921 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2922
2923 (defun gnus-data-compute-positions ()
2924   "Compute the positions of all articles."
2925   (setq gnus-newsgroup-data-reverse nil)
2926   (let ((data gnus-newsgroup-data))
2927     (save-excursion
2928       (gnus-save-hidden-threads
2929         (gnus-summary-show-all-threads)
2930         (goto-char (point-min))
2931         (while data
2932           (while (get-text-property (point) 'gnus-intangible)
2933             (forward-line 1))
2934           (gnus-data-set-pos (car data) (+ (point) 3))
2935           (setq data (cdr data))
2936           (forward-line 1))))))
2937
2938 (defun gnus-hidden-threads-configuration ()
2939   "Return the current hidden threads configuration."
2940   (save-excursion
2941     (let (config)
2942       (goto-char (point-min))
2943       (while (search-forward "\r" nil t)
2944         (push (1- (point)) config))
2945       config)))
2946
2947 (defun gnus-restore-hidden-threads-configuration (config)
2948   "Restore hidden threads configuration from CONFIG."
2949   (save-excursion
2950     (let (point buffer-read-only)
2951       (while (setq point (pop config))
2952         (when (and (< point (point-max))
2953                    (goto-char point)
2954                    (eq (char-after) ?\n))
2955           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2956
2957 ;; Various summary mode internalish functions.
2958
2959 (defun gnus-mouse-pick-article (e)
2960   (interactive "e")
2961   (mouse-set-point e)
2962   (gnus-summary-next-page nil t))
2963
2964 (defun gnus-summary-set-display-table ()
2965   "Change the display table.
2966 Odd characters have a tendency to mess
2967 up nicely formatted displays - we make all possible glyphs
2968 display only a single character."
2969
2970   ;; We start from the standard display table, if any.
2971   (let ((table (or (copy-sequence standard-display-table)
2972                    (make-display-table)))
2973         (i 32))
2974     ;; Nix out all the control chars...
2975     (while (>= (setq i (1- i)) 0)
2976       (aset table i [??]))
2977    ;; ... but not newline and cr, of course.  (cr is necessary for the
2978     ;; selective display).
2979     (aset table ?\n nil)
2980     (aset table ?\r nil)
2981     ;; We keep TAB as well.
2982     (aset table ?\t nil)
2983     ;; We nix out any glyphs over 126 that are not set already.
2984     (let ((i 256))
2985       (while (>= (setq i (1- i)) 127)
2986         ;; Only modify if the entry is nil.
2987         (unless (aref table i)
2988           (aset table i [??]))))
2989     (setq buffer-display-table table)))
2990
2991 (defun gnus-summary-set-article-display-arrow (pos)
2992   "Update the overlay arrow to point to line at position POS."
2993   (when (and gnus-summary-display-arrow
2994              (boundp 'overlay-arrow-position)
2995              (boundp 'overlay-arrow-string))
2996     (save-excursion
2997       (goto-char pos)
2998       (beginning-of-line)
2999       (unless overlay-arrow-position
3000         (setq overlay-arrow-position (make-marker)))
3001       (setq overlay-arrow-string "=>"
3002             overlay-arrow-position (set-marker overlay-arrow-position
3003                                                (point)
3004                                                (current-buffer))))))
3005
3006 (defun gnus-summary-buffer-name (group)
3007   "Return the summary buffer name of GROUP."
3008   (concat "*Summary " (gnus-group-decoded-name group) "*"))
3009
3010 (defun gnus-summary-setup-buffer (group)
3011   "Initialize summary buffer."
3012   (let ((buffer (gnus-summary-buffer-name group))
3013         (dead-name (concat "*Dead Summary "
3014                            (gnus-group-decoded-name group) "*")))
3015     ;; If a dead summary buffer exists, we kill it.
3016     (when (gnus-buffer-live-p dead-name)
3017       (gnus-kill-buffer dead-name))
3018     (if (get-buffer buffer)
3019         (progn
3020           (set-buffer buffer)
3021           (setq gnus-summary-buffer (current-buffer))
3022           (not gnus-newsgroup-prepared))
3023       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3024       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3025       (gnus-summary-mode group)
3026       (when gnus-carpal
3027         (gnus-carpal-setup-buffer 'summary))
3028       (unless gnus-single-article-buffer
3029         (make-local-variable 'gnus-article-buffer)
3030         (make-local-variable 'gnus-article-current)
3031         (make-local-variable 'gnus-original-article-buffer))
3032       (setq gnus-newsgroup-name group)
3033       ;; Set any local variables in the group parameters.
3034       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3035       t)))
3036
3037 (defun gnus-set-global-variables ()
3038   "Set the global equivalents of the buffer-local variables.
3039 They are set to the latest values they had.  These reflect the summary
3040 buffer that was in action when the last article was fetched."
3041   (when (eq major-mode 'gnus-summary-mode)
3042     (setq gnus-summary-buffer (current-buffer))
3043     (let ((name gnus-newsgroup-name)
3044           (marked gnus-newsgroup-marked)
3045           (spam gnus-newsgroup-spam-marked)
3046           (unread gnus-newsgroup-unreads)
3047           (headers gnus-current-headers)
3048           (data gnus-newsgroup-data)
3049           (summary gnus-summary-buffer)
3050           (article-buffer gnus-article-buffer)
3051           (original gnus-original-article-buffer)
3052           (gac gnus-article-current)
3053           (reffed gnus-reffed-article-number)
3054           (score-file gnus-current-score-file)
3055           (default-charset gnus-newsgroup-charset)
3056           vlist)
3057       (let ((locals gnus-newsgroup-variables))
3058         (while locals
3059           (if (consp (car locals))
3060               (push (eval (caar locals)) vlist)
3061             (push (eval (car locals)) vlist))
3062           (setq locals (cdr locals)))
3063         (setq vlist (nreverse vlist)))
3064       (save-excursion
3065         (set-buffer gnus-group-buffer)
3066         (setq gnus-newsgroup-name name
3067               gnus-newsgroup-marked marked
3068               gnus-newsgroup-spam-marked spam
3069               gnus-newsgroup-unreads unread
3070               gnus-current-headers headers
3071               gnus-newsgroup-data data
3072               gnus-article-current gac
3073               gnus-summary-buffer summary
3074               gnus-article-buffer article-buffer
3075               gnus-original-article-buffer original
3076               gnus-reffed-article-number reffed
3077               gnus-current-score-file score-file
3078               gnus-newsgroup-charset default-charset)
3079         (let ((locals gnus-newsgroup-variables))
3080           (while locals
3081             (if (consp (car locals))
3082                 (set (caar locals) (pop vlist))
3083               (set (car locals) (pop vlist)))
3084             (setq locals (cdr locals))))
3085         ;; The article buffer also has local variables.
3086         (when (gnus-buffer-live-p gnus-article-buffer)
3087           (set-buffer gnus-article-buffer)
3088           (setq gnus-summary-buffer summary))))))
3089
3090 (defun gnus-summary-article-unread-p (article)
3091   "Say whether ARTICLE is unread or not."
3092   (memq article gnus-newsgroup-unreads))
3093
3094 (defun gnus-summary-first-article-p (&optional article)
3095   "Return whether ARTICLE is the first article in the buffer."
3096   (if (not (setq article (or article (gnus-summary-article-number))))
3097       nil
3098     (eq article (caar gnus-newsgroup-data))))
3099
3100 (defun gnus-summary-last-article-p (&optional article)
3101   "Return whether ARTICLE is the last article in the buffer."
3102   (if (not (setq article (or article (gnus-summary-article-number))))
3103       ;; All non-existent numbers are the last article.  :-)
3104       t
3105     (not (cdr (gnus-data-find-list article)))))
3106
3107 (defun gnus-make-thread-indent-array ()
3108   (let ((n 200))
3109     (unless (and gnus-thread-indent-array
3110                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3111       (setq gnus-thread-indent-array (make-vector 201 "")
3112             gnus-thread-indent-array-level gnus-thread-indent-level)
3113       (while (>= n 0)
3114         (aset gnus-thread-indent-array n
3115               (make-string (* n gnus-thread-indent-level) ? ))
3116         (setq n (1- n))))))
3117
3118 (defun gnus-update-summary-mark-positions ()
3119   "Compute where the summary marks are to go."
3120   (save-excursion
3121     (when (gnus-buffer-exists-p gnus-summary-buffer)
3122       (set-buffer gnus-summary-buffer))
3123     (let ((gnus-replied-mark 129)
3124           (gnus-score-below-mark 130)
3125           (gnus-score-over-mark 130)
3126           (gnus-undownloaded-mark 131)
3127           (spec gnus-summary-line-format-spec)
3128           gnus-visual pos)
3129       (save-excursion
3130         (gnus-set-work-buffer)
3131         (let ((gnus-summary-line-format-spec spec)
3132               (gnus-newsgroup-downloadable '(0)))
3133           (gnus-summary-insert-line
3134            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3135            0 nil t 128 t nil "" nil 1)
3136           (goto-char (point-min))
3137           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3138                                              (- (point) (point-min) 1)))))
3139           (goto-char (point-min))
3140           (push (cons 'replied (and (search-forward "\201" nil t)
3141                                     (- (point) (point-min) 1)))
3142                 pos)
3143           (goto-char (point-min))
3144           (push (cons 'score (and (search-forward "\202" nil t)
3145                                   (- (point) (point-min) 1)))
3146                 pos)
3147           (goto-char (point-min))
3148           (push (cons 'download
3149                       (and (search-forward "\203" nil t)
3150                            (- (point) (point-min) 1)))
3151                 pos)))
3152       (setq gnus-summary-mark-positions pos))))
3153
3154 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3155   "Insert a dummy root in the summary buffer."
3156   (beginning-of-line)
3157   (gnus-add-text-properties
3158    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3159    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3160
3161 (defun gnus-summary-extract-address-component (from)
3162   (or (car (funcall gnus-extract-address-components from))
3163       from))
3164
3165 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3166   (let ((mail-parse-charset gnus-newsgroup-charset)
3167         ; Is it really necessary to do this next part for each summary line?
3168         ; Luckily, doesn't seem to slow things down much.
3169         (mail-parse-ignored-charsets
3170          (save-excursion (set-buffer gnus-summary-buffer)
3171                          gnus-newsgroup-ignored-charsets)))
3172     (or
3173      (and gnus-ignored-from-addresses
3174           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3175           (let ((extra-headers (mail-header-extra header))
3176                 to
3177                 newsgroups)
3178             (cond
3179              ((setq to (cdr (assq 'To extra-headers)))
3180               (concat "-> "
3181                       (inline
3182                         (gnus-summary-extract-address-component
3183                          (funcall gnus-decode-encoded-word-function to)))))
3184              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3185               (concat "=> " newsgroups)))))
3186      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3187
3188 (defun gnus-summary-insert-line (gnus-tmp-header
3189                                  gnus-tmp-level gnus-tmp-current
3190                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3191                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3192                                  &optional gnus-tmp-dummy gnus-tmp-score
3193                                  gnus-tmp-process)
3194   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3195          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3196          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3197          (gnus-tmp-score-char
3198           (if (or (null gnus-summary-default-score)
3199                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3200                       gnus-summary-zcore-fuzz))
3201               ?                         ;Whitespace
3202             (if (< gnus-tmp-score gnus-summary-default-score)
3203                 gnus-score-below-mark gnus-score-over-mark)))
3204          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3205          (gnus-tmp-replied
3206           (cond (gnus-tmp-process gnus-process-mark)
3207                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3208                  gnus-cached-mark)
3209                 (gnus-tmp-replied gnus-replied-mark)
3210                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3211                  gnus-forwarded-mark)
3212                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3213                  gnus-saved-mark)
3214                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3215                  gnus-recent-mark)
3216                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3217                  gnus-unseen-mark)
3218                 (t gnus-no-mark)))
3219          (gnus-tmp-downloaded
3220           (cond (undownloaded 
3221                  gnus-undownloaded-mark)
3222                 (gnus-newsgroup-agentized
3223                  gnus-downloaded-mark)
3224                 (t
3225                  gnus-no-mark)))
3226          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3227          (gnus-tmp-name
3228           (cond
3229            ((string-match "<[^>]+> *$" gnus-tmp-from)
3230             (let ((beg (match-beginning 0)))
3231               (or (and (string-match "^\".+\"" gnus-tmp-from)
3232                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3233                   (substring gnus-tmp-from 0 beg))))
3234            ((string-match "(.+)" gnus-tmp-from)
3235             (substring gnus-tmp-from
3236                        (1+ (match-beginning 0)) (1- (match-end 0))))
3237            (t gnus-tmp-from)))
3238          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3239          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3240          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3241          (buffer-read-only nil))
3242     (when (string= gnus-tmp-name "")
3243       (setq gnus-tmp-name gnus-tmp-from))
3244     (unless (numberp gnus-tmp-lines)
3245       (setq gnus-tmp-lines -1))
3246     (if (= gnus-tmp-lines -1)
3247         (setq gnus-tmp-lines "?")
3248       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3249     (gnus-put-text-property
3250      (point)
3251      (progn (eval gnus-summary-line-format-spec) (point))
3252      'gnus-number gnus-tmp-number)
3253     (when (gnus-visual-p 'summary-highlight 'highlight)
3254       (forward-line -1)
3255       (gnus-run-hooks 'gnus-summary-update-hook)
3256       (forward-line 1))))
3257
3258 (defun gnus-summary-update-line (&optional dont-update)
3259   "Update summary line after change."
3260   (when (and gnus-summary-default-score
3261              (not gnus-summary-inhibit-highlight))
3262     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3263            (article (gnus-summary-article-number))
3264            (score (gnus-summary-article-score article)))
3265       (unless dont-update
3266         (if (and gnus-summary-mark-below
3267                  (< (gnus-summary-article-score)
3268                     gnus-summary-mark-below))
3269             ;; This article has a low score, so we mark it as read.
3270             (when (memq article gnus-newsgroup-unreads)
3271               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3272           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3273             ;; This article was previously marked as read on account
3274             ;; of a low score, but now it has risen, so we mark it as
3275             ;; unread.
3276             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3277         (gnus-summary-update-mark
3278          (if (or (null gnus-summary-default-score)
3279                  (<= (abs (- score gnus-summary-default-score))
3280                      gnus-summary-zcore-fuzz))
3281              ?                          ;Whitespace
3282            (if (< score gnus-summary-default-score)
3283                gnus-score-below-mark gnus-score-over-mark))
3284          'score))
3285       ;; Do visual highlighting.
3286       (when (gnus-visual-p 'summary-highlight 'highlight)
3287         (gnus-run-hooks 'gnus-summary-update-hook)))))
3288
3289 (defvar gnus-tmp-new-adopts nil)
3290
3291 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3292   "Return the number of articles in THREAD.
3293 This may be 0 in some cases -- if none of the articles in
3294 the thread are to be displayed."
3295   (let* ((number
3296          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3297           (cond
3298            ((not (listp thread))
3299             1)
3300            ((and (consp thread) (cdr thread))
3301             (apply
3302              '+ 1 (mapcar
3303                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3304            ((null thread)
3305             1)
3306            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3307             1)
3308            (t 0))))
3309     (when (and level (zerop level) gnus-tmp-new-adopts)
3310       (incf number
3311             (apply '+ (mapcar
3312                        'gnus-summary-number-of-articles-in-thread
3313                        gnus-tmp-new-adopts))))
3314     (if char
3315         (if (> number 1) gnus-not-empty-thread-mark
3316           gnus-empty-thread-mark)
3317       number)))
3318
3319 (defsubst gnus-summary-line-message-size (head)
3320   "Return pretty-printed version of message size.
3321 This function is intended to be used in
3322 `gnus-summary-line-format-alist', which see."
3323   (let ((c (or (mail-header-chars head) -1)))
3324     (cond ((< c 0) "n/a")               ; chars not available
3325           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3326           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3327           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3328           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3329
3330
3331 (defun gnus-summary-set-local-parameters (group)
3332   "Go through the local params of GROUP and set all variable specs in that list."
3333   (let ((params (gnus-group-find-parameter group))
3334         (vars '(quit-config))           ; Ignore quit-config.
3335         elem)
3336     (while params
3337       (setq elem (car params)
3338             params (cdr params))
3339       (and (consp elem)                 ; Has to be a cons.
3340            (consp (cdr elem))           ; The cdr has to be a list.
3341            (symbolp (car elem))         ; Has to be a symbol in there.
3342            (not (memq (car elem) vars))
3343            (ignore-errors               ; So we set it.
3344              (push (car elem) vars)
3345              (make-local-variable (car elem))
3346              (set (car elem) (eval (nth 1 elem))))))))
3347
3348 (defun gnus-summary-read-group (group &optional show-all no-article
3349                                       kill-buffer no-display backward
3350                                       select-articles)
3351   "Start reading news in newsgroup GROUP.
3352 If SHOW-ALL is non-nil, already read articles are also listed.
3353 If NO-ARTICLE is non-nil, no article is selected initially.
3354 If NO-DISPLAY, don't generate a summary buffer."
3355   (let (result)
3356     (while (and group
3357                 (null (setq result
3358                             (let ((gnus-auto-select-next nil))
3359                               (or (gnus-summary-read-group-1
3360                                    group show-all no-article
3361                                    kill-buffer no-display
3362                                    select-articles)
3363                                   (setq show-all nil
3364                                         select-articles nil)))))
3365                 (eq gnus-auto-select-next 'quietly))
3366       (set-buffer gnus-group-buffer)
3367       ;; The entry function called above goes to the next
3368       ;; group automatically, so we go two groups back
3369       ;; if we are searching for the previous group.
3370       (when backward
3371         (gnus-group-prev-unread-group 2))
3372       (if (not (equal group (gnus-group-group-name)))
3373           (setq group (gnus-group-group-name))
3374         (setq group nil)))
3375     result))
3376
3377 (defun gnus-summary-read-group-1 (group show-all no-article
3378                                         kill-buffer no-display
3379                                         &optional select-articles)
3380   ;; Killed foreign groups can't be entered.
3381   ;;  (when (and (not (gnus-group-native-p group))
3382   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3383   ;;    (error "Dead non-native groups can't be entered"))
3384   (gnus-message 5 "Retrieving newsgroup: %s..."
3385                 (gnus-group-decoded-name group))
3386   (let* ((new-group (gnus-summary-setup-buffer group))
3387          (quit-config (gnus-group-quit-config group))
3388          (did-select (and new-group (gnus-select-newsgroup
3389                                      group show-all select-articles))))
3390     (cond
3391      ;; This summary buffer exists already, so we just select it.
3392      ((not new-group)
3393       (gnus-set-global-variables)
3394       (when kill-buffer
3395         (gnus-kill-or-deaden-summary kill-buffer))
3396       (gnus-configure-windows 'summary 'force)
3397       (gnus-set-mode-line 'summary)
3398       (gnus-summary-position-point)
3399       (message "")
3400       t)
3401      ;; We couldn't select this group.
3402      ((null did-select)
3403       (when (and (eq major-mode 'gnus-summary-mode)
3404                  (not (equal (current-buffer) kill-buffer)))
3405         (kill-buffer (current-buffer))
3406         (if (not quit-config)
3407             (progn
3408               ;; Update the info -- marks might need to be removed,
3409               ;; for instance.
3410               (gnus-summary-update-info)
3411               (set-buffer gnus-group-buffer)
3412               (gnus-group-jump-to-group group)
3413               (gnus-group-next-unread-group 1))
3414           (gnus-handle-ephemeral-exit quit-config)))
3415       (let ((grpinfo (gnus-get-info group)))
3416         (if (null (gnus-info-read grpinfo))
3417             (gnus-message 3 "Group %s contains no messages"
3418                           (gnus-group-decoded-name group))
3419           (gnus-message 3 "Can't select group")))
3420       nil)
3421      ;; The user did a `C-g' while prompting for number of articles,
3422      ;; so we exit this group.
3423      ((eq did-select 'quit)
3424       (and (eq major-mode 'gnus-summary-mode)
3425            (not (equal (current-buffer) kill-buffer))
3426            (kill-buffer (current-buffer)))
3427       (when kill-buffer
3428         (gnus-kill-or-deaden-summary kill-buffer))
3429       (if (not quit-config)
3430           (progn
3431             (set-buffer gnus-group-buffer)
3432             (gnus-group-jump-to-group group)
3433             (gnus-group-next-unread-group 1)
3434             (gnus-configure-windows 'group 'force))
3435         (gnus-handle-ephemeral-exit quit-config))
3436       ;; Finally signal the quit.
3437       (signal 'quit nil))
3438      ;; The group was successfully selected.
3439      (t
3440       (gnus-set-global-variables)
3441       ;; Save the active value in effect when the group was entered.
3442       (setq gnus-newsgroup-active
3443             (gnus-copy-sequence
3444              (gnus-active gnus-newsgroup-name)))
3445       ;; You can change the summary buffer in some way with this hook.
3446       (gnus-run-hooks 'gnus-select-group-hook)
3447       (gnus-update-format-specifications
3448        nil 'summary 'summary-mode 'summary-dummy)
3449       (gnus-update-summary-mark-positions)
3450       ;; Do score processing.
3451       (when gnus-use-scoring
3452         (gnus-possibly-score-headers))
3453       ;; Check whether to fill in the gaps in the threads.
3454       (when gnus-build-sparse-threads
3455         (gnus-build-sparse-threads))
3456       ;; Find the initial limit.
3457       (if gnus-show-threads
3458           (if show-all
3459               (let ((gnus-newsgroup-dormant nil))
3460                 (gnus-summary-initial-limit show-all))
3461             (gnus-summary-initial-limit show-all))
3462         ;; When unthreaded, all articles are always shown.
3463         (setq gnus-newsgroup-limit
3464               (mapcar
3465                (lambda (header) (mail-header-number header))
3466                gnus-newsgroup-headers)))
3467       ;; Generate the summary buffer.
3468       (unless no-display
3469         (gnus-summary-prepare))
3470       (when gnus-use-trees
3471         (gnus-tree-open group)
3472         (setq gnus-summary-highlight-line-function
3473               'gnus-tree-highlight-article))
3474       ;; If the summary buffer is empty, but there are some low-scored
3475       ;; articles or some excluded dormants, we include these in the
3476       ;; buffer.
3477       (when (and (zerop (buffer-size))
3478                  (not no-display))
3479         (cond (gnus-newsgroup-dormant
3480                (gnus-summary-limit-include-dormant))
3481               ((and gnus-newsgroup-scored show-all)
3482                (gnus-summary-limit-include-expunged t))))
3483       ;; Function `gnus-apply-kill-file' must be called in this hook.
3484       (gnus-run-hooks 'gnus-apply-kill-hook)
3485       (if (and (zerop (buffer-size))
3486                (not no-display))
3487           (progn
3488             ;; This newsgroup is empty.
3489             (gnus-summary-catchup-and-exit nil t)
3490             (gnus-message 6 "No unread news")
3491             (when kill-buffer
3492               (gnus-kill-or-deaden-summary kill-buffer))
3493             ;; Return nil from this function.
3494             nil)
3495         ;; Hide conversation thread subtrees.  We cannot do this in
3496         ;; gnus-summary-prepare-hook since kill processing may not
3497         ;; work with hidden articles.
3498         (gnus-summary-maybe-hide-threads)
3499         (when kill-buffer
3500           (gnus-kill-or-deaden-summary kill-buffer))
3501         (gnus-summary-auto-select-subject)
3502         ;; Show first unread article if requested.
3503         (if (and (not no-article)
3504                  (not no-display)
3505                  gnus-newsgroup-unreads
3506                  gnus-auto-select-first)
3507             (progn
3508               (gnus-configure-windows 'summary)
3509               (let ((art (gnus-summary-article-number)))
3510                 (unless (and (not gnus-plugged)
3511                              (or (memq art gnus-newsgroup-undownloaded)
3512                                  (memq art gnus-newsgroup-downloadable)))
3513                   (gnus-summary-goto-article art))))
3514           ;; Don't select any articles.
3515           (gnus-summary-position-point)
3516           (gnus-configure-windows 'summary 'force)
3517           (gnus-set-mode-line 'summary))
3518         (when (get-buffer-window gnus-group-buffer t)
3519           ;; Gotta use windows, because recenter does weird stuff if
3520           ;; the current buffer ain't the displayed window.
3521           (let ((owin (selected-window)))
3522             (select-window (get-buffer-window gnus-group-buffer t))
3523             (when (gnus-group-goto-group group)
3524               (recenter))
3525             (select-window owin)))
3526         ;; Mark this buffer as "prepared".
3527         (setq gnus-newsgroup-prepared t)
3528         (gnus-run-hooks 'gnus-summary-prepared-hook)
3529         (unless (gnus-ephemeral-group-p group)
3530           (gnus-group-update-group group))
3531         t)))))
3532
3533 (defun gnus-summary-auto-select-subject ()
3534   "Select the subject line on initial group entry."
3535   (goto-char (point-min))
3536   (cond
3537    ((eq gnus-auto-select-subject 'best)
3538     (gnus-summary-best-unread-subject))
3539    ((eq gnus-auto-select-subject 'unread)
3540     (gnus-summary-first-unread-subject))
3541    ((eq gnus-auto-select-subject 'unseen)
3542     (gnus-summary-first-unseen-subject))
3543    ((eq gnus-auto-select-subject 'unseen-or-unread)
3544     (gnus-summary-first-unseen-or-unread-subject))
3545    ((eq gnus-auto-select-subject 'first)
3546     ;; Do nothing.
3547     )
3548    ((gnus-functionp gnus-auto-select-subject)
3549     (funcall gnus-auto-select-subject))))
3550
3551 (defun gnus-summary-prepare ()
3552   "Generate the summary buffer."
3553   (interactive)
3554   (let ((buffer-read-only nil))
3555     (erase-buffer)
3556     (setq gnus-newsgroup-data nil
3557           gnus-newsgroup-data-reverse nil)
3558     (gnus-run-hooks 'gnus-summary-generate-hook)
3559     ;; Generate the buffer, either with threads or without.
3560     (when gnus-newsgroup-headers
3561       (gnus-summary-prepare-threads
3562        (if gnus-show-threads
3563            (gnus-sort-gathered-threads
3564             (funcall gnus-summary-thread-gathering-function
3565                      (gnus-sort-threads
3566                       (gnus-cut-threads (gnus-make-threads)))))
3567          ;; Unthreaded display.
3568          (gnus-sort-articles gnus-newsgroup-headers))))
3569     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3570     ;; Call hooks for modifying summary buffer.
3571     (goto-char (point-min))
3572     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3573
3574 (defsubst gnus-general-simplify-subject (subject)
3575   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3576   (setq subject
3577         (cond
3578          ;; Truncate the subject.
3579          (gnus-simplify-subject-functions
3580           (gnus-map-function gnus-simplify-subject-functions subject))
3581          ((numberp gnus-summary-gather-subject-limit)
3582           (setq subject (gnus-simplify-subject-re subject))
3583           (if (> (length subject) gnus-summary-gather-subject-limit)
3584               (substring subject 0 gnus-summary-gather-subject-limit)
3585             subject))
3586          ;; Fuzzily simplify it.
3587          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3588           (gnus-simplify-subject-fuzzy subject))
3589          ;; Just remove the leading "Re:".
3590          (t
3591           (gnus-simplify-subject-re subject))))
3592
3593   (if (and gnus-summary-gather-exclude-subject
3594            (string-match gnus-summary-gather-exclude-subject subject))
3595       nil                         ; This article shouldn't be gathered
3596     subject))
3597
3598 (defun gnus-summary-simplify-subject-query ()
3599   "Query where the respool algorithm would put this article."
3600   (interactive)
3601   (gnus-summary-select-article)
3602   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3603
3604 (defun gnus-gather-threads-by-subject (threads)
3605   "Gather threads by looking at Subject headers."
3606   (if (not gnus-summary-make-false-root)
3607       threads
3608     (let ((hashtb (gnus-make-hashtable 1024))
3609           (prev threads)
3610           (result threads)
3611           subject hthread whole-subject)
3612       (while threads
3613         (setq subject (gnus-general-simplify-subject
3614                        (setq whole-subject (mail-header-subject
3615                                             (caar threads)))))
3616         (when subject
3617           (if (setq hthread (gnus-gethash subject hashtb))
3618               (progn
3619                 ;; We enter a dummy root into the thread, if we
3620                 ;; haven't done that already.
3621                 (unless (stringp (caar hthread))
3622                   (setcar hthread (list whole-subject (car hthread))))
3623                 ;; We add this new gathered thread to this gathered
3624                 ;; thread.
3625                 (setcdr (car hthread)
3626                         (nconc (cdar hthread) (list (car threads))))
3627                 ;; Remove it from the list of threads.
3628                 (setcdr prev (cdr threads))
3629                 (setq threads prev))
3630             ;; Enter this thread into the hash table.
3631             (gnus-sethash subject
3632                           (if gnus-summary-make-false-root-always
3633                               (progn
3634                                 ;; If you want a dummy root above all
3635                                 ;; threads...
3636                                 (setcar threads (list whole-subject
3637                                                       (car threads)))
3638                                 threads)
3639                             threads)
3640                           hashtb)))
3641         (setq prev threads)
3642         (setq threads (cdr threads)))
3643       result)))
3644
3645 (defun gnus-gather-threads-by-references (threads)
3646   "Gather threads by looking at References headers."
3647   (let ((idhashtb (gnus-make-hashtable 1024))
3648         (thhashtb (gnus-make-hashtable 1024))
3649         (prev threads)
3650         (result threads)
3651         ids references id gthread gid entered ref)
3652     (while threads
3653       (when (setq references (mail-header-references (caar threads)))
3654         (setq id (mail-header-id (caar threads))
3655               ids (inline (gnus-split-references references))
3656               entered nil)
3657         (while (setq ref (pop ids))
3658           (setq ids (delete ref ids))
3659           (if (not (setq gid (gnus-gethash ref idhashtb)))
3660               (progn
3661                 (gnus-sethash ref id idhashtb)
3662                 (gnus-sethash id threads thhashtb))
3663             (setq gthread (gnus-gethash gid thhashtb))
3664             (unless entered
3665               ;; We enter a dummy root into the thread, if we
3666               ;; haven't done that already.
3667               (unless (stringp (caar gthread))
3668                 (setcar gthread (list (mail-header-subject (caar gthread))
3669                                       (car gthread))))
3670               ;; We add this new gathered thread to this gathered
3671               ;; thread.
3672               (setcdr (car gthread)
3673                       (nconc (cdar gthread) (list (car threads)))))
3674             ;; Add it into the thread hash table.
3675             (gnus-sethash id gthread thhashtb)
3676             (setq entered t)
3677             ;; Remove it from the list of threads.
3678             (setcdr prev (cdr threads))
3679             (setq threads prev))))
3680       (setq prev threads)
3681       (setq threads (cdr threads)))
3682     result))
3683
3684 (defun gnus-sort-gathered-threads (threads)
3685   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3686   (let ((result threads))
3687     (while threads
3688       (when (stringp (caar threads))
3689         (setcdr (car threads)
3690                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3691       (setq threads (cdr threads)))
3692     result))
3693
3694 (defun gnus-thread-loop-p (root thread)
3695   "Say whether ROOT is in THREAD."
3696   (let ((stack (list thread))
3697         (infloop 0)
3698         th)
3699     (while (setq thread (pop stack))
3700       (setq th (cdr thread))
3701       (while (and th
3702                   (not (eq (caar th) root)))
3703         (pop th))
3704       (if th
3705           ;; We have found a loop.
3706           (let (ref-dep)
3707             (setcdr thread (delq (car th) (cdr thread)))
3708             (if (boundp (setq ref-dep (intern "none"
3709                                               gnus-newsgroup-dependencies)))
3710                 (setcdr (symbol-value ref-dep)
3711                         (nconc (cdr (symbol-value ref-dep))
3712                                (list (car th))))
3713               (set ref-dep (list nil (car th))))
3714             (setq infloop 1
3715                   stack nil))
3716         ;; Push all the subthreads onto the stack.
3717         (push (cdr thread) stack)))
3718     infloop))
3719
3720 (defun gnus-make-threads ()
3721   "Go through the dependency hashtb and find the roots.  Return all threads."
3722   (let (threads)
3723     (while (catch 'infloop
3724              (mapatoms
3725               (lambda (refs)
3726                 ;; Deal with self-referencing References loops.
3727                 (when (and (car (symbol-value refs))
3728                            (not (zerop
3729                                  (apply
3730                                   '+
3731                                   (mapcar
3732                                    (lambda (thread)
3733                                      (gnus-thread-loop-p
3734                                       (car (symbol-value refs)) thread))
3735                                    (cdr (symbol-value refs)))))))
3736                   (setq threads nil)
3737                   (throw 'infloop t))
3738                 (unless (car (symbol-value refs))
3739                   ;; These threads do not refer back to any other
3740                   ;; articles, so they're roots.
3741                   (setq threads (append (cdr (symbol-value refs)) threads))))
3742               gnus-newsgroup-dependencies)))
3743     threads))
3744
3745 ;; Build the thread tree.
3746 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3747   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3748
3749 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3750 if it was already present.
3751
3752 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3753 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3754 Message-IDs will be renamed to a unique Message-ID before being
3755 entered.
3756
3757 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3758   (let* ((id (mail-header-id header))
3759          (id-dep (and id (intern id dependencies)))
3760          parent-id ref ref-dep ref-header replaced)
3761     ;; Enter this `header' in the `dependencies' table.
3762     (cond
3763      ((not id-dep)
3764       (setq header nil))
3765      ;; The first two cases do the normal part: enter a new `header'
3766      ;; in the `dependencies' table.
3767      ((not (boundp id-dep))
3768       (set id-dep (list header)))
3769      ((null (car (symbol-value id-dep)))
3770       (setcar (symbol-value id-dep) header))
3771
3772      ;; From here the `header' was already present in the
3773      ;; `dependencies' table.
3774      (force-new
3775       ;; Overrides an existing entry;
3776       ;; just set the header part of the entry.
3777       (setcar (symbol-value id-dep) header)
3778       (setq replaced t))
3779
3780      ;; Renames the existing `header' to a unique Message-ID.
3781      ((not gnus-summary-ignore-duplicates)
3782       ;; An article with this Message-ID has already been seen.
3783       ;; We rename the Message-ID.
3784       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3785            (list header))
3786       (mail-header-set-id header id))
3787
3788      ;; The last case ignores an existing entry, except it adds any
3789      ;; additional Xrefs (in case the two articles came from different
3790      ;; servers.
3791      ;; Also sets `header' to `nil' meaning that the `dependencies'
3792      ;; table was *not* modified.
3793      (t
3794       (mail-header-set-xref
3795        (car (symbol-value id-dep))
3796        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3797                    "")
3798                (or (mail-header-xref header) "")))
3799       (setq header nil)))
3800
3801     (when (and header (not replaced))
3802       ;; First check that we are not creating a References loop.
3803       (setq parent-id (gnus-parent-id (mail-header-references header)))
3804       (setq ref parent-id)
3805       (while (and ref
3806                   (setq ref-dep (intern-soft ref dependencies))
3807                   (boundp ref-dep)
3808                   (setq ref-header (car (symbol-value ref-dep))))
3809         (if (string= id ref)
3810             ;; Yuk!  This is a reference loop.  Make the article be a
3811             ;; root article.
3812             (progn
3813               (mail-header-set-references (car (symbol-value id-dep)) "none")
3814               (setq ref nil)
3815               (setq parent-id nil))
3816           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3817       (setq ref-dep (intern (or parent-id "none") dependencies))
3818       (if (boundp ref-dep)
3819           (setcdr (symbol-value ref-dep)
3820                   (nconc (cdr (symbol-value ref-dep))
3821                          (list (symbol-value id-dep))))
3822         (set ref-dep (list nil (symbol-value id-dep)))))
3823     header))
3824
3825 (defun gnus-extract-message-id-from-in-reply-to (string)
3826   (if (string-match "<[^>]+>" string)
3827       (substring string (match-beginning 0) (match-end 0))
3828     nil))
3829
3830 (defun gnus-build-sparse-threads ()
3831   (let ((headers gnus-newsgroup-headers)
3832         (mail-parse-charset gnus-newsgroup-charset)
3833         (gnus-summary-ignore-duplicates t)
3834         header references generation relations
3835         subject child end new-child date)
3836     ;; First we create an alist of generations/relations, where
3837     ;; generations is how much we trust the relation, and the relation
3838     ;; is parent/child.
3839     (gnus-message 7 "Making sparse threads...")
3840     (save-excursion
3841       (nnheader-set-temp-buffer " *gnus sparse threads*")
3842       (while (setq header (pop headers))
3843         (when (and (setq references (mail-header-references header))
3844                    (not (string= references "")))
3845           (insert references)
3846           (setq child (mail-header-id header)
3847                 subject (mail-header-subject header)
3848                 date (mail-header-date header)
3849                 generation 0)
3850           (while (search-backward ">" nil t)
3851             (setq end (1+ (point)))
3852             (when (search-backward "<" nil t)
3853               (setq new-child (buffer-substring (point) end))
3854               (push (list (incf generation)
3855                           child (setq child new-child)
3856                           subject date)
3857                     relations)))
3858           (when child
3859             (push (list (1+ generation) child nil subject) relations))
3860           (erase-buffer)))
3861       (kill-buffer (current-buffer)))
3862     ;; Sort over trustworthiness.
3863     (mapcar
3864      (lambda (relation)
3865        (when (gnus-dependencies-add-header
3866               (make-full-mail-header
3867                gnus-reffed-article-number
3868                (nth 3 relation) "" (or (nth 4 relation) "")
3869                (nth 1 relation)
3870                (or (nth 2 relation) "") 0 0 "")
3871               gnus-newsgroup-dependencies nil)
3872          (push gnus-reffed-article-number gnus-newsgroup-limit)
3873          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3874          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3875                gnus-newsgroup-reads)
3876          (decf gnus-reffed-article-number)))
3877      (sort relations 'car-less-than-car))
3878     (gnus-message 7 "Making sparse threads...done")))
3879
3880 (defun gnus-build-old-threads ()
3881   ;; Look at all the articles that refer back to old articles, and
3882   ;; fetch the headers for the articles that aren't there.  This will
3883   ;; build complete threads - if the roots haven't been expired by the
3884   ;; server, that is.
3885   (let ((mail-parse-charset gnus-newsgroup-charset)
3886         id heads)
3887     (mapatoms
3888      (lambda (refs)
3889        (when (not (car (symbol-value refs)))
3890          (setq heads (cdr (symbol-value refs)))
3891          (while heads
3892            (if (memq (mail-header-number (caar heads))
3893                      gnus-newsgroup-dormant)
3894                (setq heads (cdr heads))
3895              (setq id (symbol-name refs))
3896              (while (and (setq id (gnus-build-get-header id))
3897                          (not (car (gnus-id-to-thread id)))))
3898              (setq heads nil)))))
3899      gnus-newsgroup-dependencies)))
3900
3901 ;; This function has to be called with point after the article number
3902 ;; on the beginning of the line.
3903 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3904   (let ((eol (gnus-point-at-eol))
3905         (buffer (current-buffer))
3906         header references in-reply-to)
3907
3908     ;; overview: [num subject from date id refs chars lines misc]
3909     (unwind-protect
3910         (let (x)
3911           (narrow-to-region (point) eol)
3912           (unless (eobp)
3913             (forward-char))
3914
3915           (setq header
3916                 (make-full-mail-header
3917                  number                 ; number
3918                  (condition-case ()     ; subject
3919                      (funcall gnus-decode-encoded-word-function
3920                               (setq x (nnheader-nov-field)))
3921                    (error x))
3922                  (condition-case ()     ; from
3923                      (funcall gnus-decode-encoded-word-function
3924                               (setq x (nnheader-nov-field)))
3925                    (error x))
3926                  (nnheader-nov-field)   ; date
3927                  (nnheader-nov-read-message-id) ; id
3928                  (setq references (nnheader-nov-field)) ; refs
3929                  (nnheader-nov-read-integer) ; chars
3930                  (nnheader-nov-read-integer) ; lines
3931                  (unless (eobp)
3932                    (if (looking-at "Xref: ")
3933                        (goto-char (match-end 0)))
3934                    (nnheader-nov-field)) ; Xref
3935                  (nnheader-nov-parse-extra)))) ; extra
3936
3937       (widen))
3938
3939     (when (and (string= references "")
3940                (setq in-reply-to (mail-header-extra header))
3941                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3942       (mail-header-set-references
3943        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3944
3945     (when gnus-alter-header-function
3946       (funcall gnus-alter-header-function header))
3947     (gnus-dependencies-add-header header dependencies force-new)))
3948
3949 (defun gnus-build-get-header (id)
3950   "Look through the buffer of NOV lines and find the header to ID.
3951 Enter this line into the dependencies hash table, and return
3952 the id of the parent article (if any)."
3953   (let ((deps gnus-newsgroup-dependencies)
3954         found header)
3955     (prog1
3956         (save-excursion
3957           (set-buffer nntp-server-buffer)
3958           (let ((case-fold-search nil))
3959             (goto-char (point-min))
3960             (while (and (not found)
3961                         (search-forward id nil t))
3962               (beginning-of-line)
3963               (setq found (looking-at
3964                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3965                                    (regexp-quote id))))
3966               (or found (beginning-of-line 2)))
3967             (when found
3968               (beginning-of-line)
3969               (and
3970                (setq header (gnus-nov-parse-line
3971                              (read (current-buffer)) deps))
3972                (gnus-parent-id (mail-header-references header))))))
3973       (when header
3974         (let ((number (mail-header-number header)))
3975           (push number gnus-newsgroup-limit)
3976           (push header gnus-newsgroup-headers)
3977           (if (memq number gnus-newsgroup-unselected)
3978               (progn
3979                 (setq gnus-newsgroup-unreads
3980                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
3981                                                number))
3982                 (setq gnus-newsgroup-unselected
3983                       (delq number gnus-newsgroup-unselected)))
3984             (push number gnus-newsgroup-ancient)))))))
3985
3986 (defun gnus-build-all-threads ()
3987   "Read all the headers."
3988   (let ((gnus-summary-ignore-duplicates t)
3989         (mail-parse-charset gnus-newsgroup-charset)
3990         (dependencies gnus-newsgroup-dependencies)
3991         header article)
3992     (save-excursion
3993       (set-buffer nntp-server-buffer)
3994       (let ((case-fold-search nil))
3995         (goto-char (point-min))
3996         (while (not (eobp))
3997           (ignore-errors
3998             (setq article (read (current-buffer))
3999                   header (gnus-nov-parse-line article dependencies)))
4000           (when header
4001             (save-excursion
4002               (set-buffer gnus-summary-buffer)
4003               (push header gnus-newsgroup-headers)
4004               (if (memq (setq article (mail-header-number header))
4005                         gnus-newsgroup-unselected)
4006                   (progn
4007                     (setq gnus-newsgroup-unreads
4008                           (gnus-add-to-sorted-list
4009                            gnus-newsgroup-unreads article))
4010                     (setq gnus-newsgroup-unselected
4011                           (delq article gnus-newsgroup-unselected)))
4012                 (push article gnus-newsgroup-ancient)))
4013             (forward-line 1)))))))
4014
4015 (defun gnus-summary-update-article-line (article header)
4016   "Update the line for ARTICLE using HEADERS."
4017   (let* ((id (mail-header-id header))
4018          (thread (gnus-id-to-thread id)))
4019     (unless thread
4020       (error "Article in no thread"))
4021     ;; Update the thread.
4022     (setcar thread header)
4023     (gnus-summary-goto-subject article)
4024     (let* ((datal (gnus-data-find-list article))
4025            (data (car datal))
4026            (buffer-read-only nil)
4027            (level (gnus-summary-thread-level)))
4028       (gnus-delete-line)
4029       (let ((inserted (- (point)
4030                          (progn
4031                            (gnus-summary-insert-line
4032                             header level nil 
4033                             (memq article gnus-newsgroup-undownloaded)
4034                             (gnus-article-mark article)
4035                             (memq article gnus-newsgroup-replied)
4036                             (memq article gnus-newsgroup-expirable)
4037                             ;; Only insert the Subject string when it's different
4038                             ;; from the previous Subject string.
4039                             (if (and
4040                                  gnus-show-threads
4041                                  (gnus-subject-equal
4042                                   (condition-case ()
4043                                       (mail-header-subject
4044                                        (gnus-data-header
4045                                         (cadr
4046                                          (gnus-data-find-list
4047                                           article
4048                                           (gnus-data-list t)))))
4049                                     ;; Error on the side of excessive subjects.
4050                                     (error ""))
4051                                   (mail-header-subject header)))
4052                                 ""
4053                               (mail-header-subject header))
4054                             nil (cdr (assq article gnus-newsgroup-scored))
4055                             (memq article gnus-newsgroup-processable))
4056                            (point)))))
4057         (when (cdr datal)
4058           (gnus-data-update-list
4059            (cdr datal) 
4060            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4061
4062 (defun gnus-summary-update-article (article &optional iheader)
4063   "Update ARTICLE in the summary buffer."
4064   (set-buffer gnus-summary-buffer)
4065   (let* ((header (gnus-summary-article-header article))
4066          (id (mail-header-id header))
4067          (data (gnus-data-find article))
4068          (thread (gnus-id-to-thread id))
4069          (references (mail-header-references header))
4070          (parent
4071           (gnus-id-to-thread
4072            (or (gnus-parent-id
4073                 (when (and references
4074                            (not (equal "" references)))
4075                   references))
4076                "none")))
4077          (buffer-read-only nil)
4078          (old (car thread)))
4079     (when thread
4080       (unless iheader
4081         (setcar thread nil)
4082         (when parent
4083           (delq thread parent)))
4084       (if (gnus-summary-insert-subject id header)
4085           ;; Set the (possibly) new article number in the data structure.
4086           (gnus-data-set-number data (gnus-id-to-article id))
4087         (setcar thread old)
4088         nil))))
4089
4090 (defun gnus-rebuild-thread (id &optional line)
4091   "Rebuild the thread containing ID.
4092 If LINE, insert the rebuilt thread starting on line LINE."
4093   (let ((buffer-read-only nil)
4094         old-pos current thread data)
4095     (if (not gnus-show-threads)
4096         (setq thread (list (car (gnus-id-to-thread id))))
4097       ;; Get the thread this article is part of.
4098       (setq thread (gnus-remove-thread id)))
4099     (setq old-pos (gnus-point-at-bol))
4100     (setq current (save-excursion
4101                     (and (re-search-backward "[\r\n]" nil t)
4102                          (gnus-summary-article-number))))
4103     ;; If this is a gathered thread, we have to go some re-gathering.
4104     (when (stringp (car thread))
4105       (let ((subject (car thread))
4106             roots thr)
4107         (setq thread (cdr thread))
4108         (while thread
4109           (unless (memq (setq thr (gnus-id-to-thread
4110                                    (gnus-root-id
4111                                     (mail-header-id (caar thread)))))
4112                         roots)
4113             (push thr roots))
4114           (setq thread (cdr thread)))
4115         ;; We now have all (unique) roots.
4116         (if (= (length roots) 1)
4117             ;; All the loose roots are now one solid root.
4118             (setq thread (car roots))
4119           (setq thread (cons subject (gnus-sort-threads roots))))))
4120     (let (threads)
4121       ;; We then insert this thread into the summary buffer.
4122       (when line
4123         (goto-char (point-min))
4124         (forward-line (1- line)))
4125       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4126         (if gnus-show-threads
4127             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4128           (gnus-summary-prepare-unthreaded thread))
4129         (setq data (nreverse gnus-newsgroup-data))
4130         (setq threads gnus-newsgroup-threads))
4131       ;; We splice the new data into the data structure.
4132       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4133       ;;!!! then we want to insert at the beginning of the buffer.
4134       ;;!!! That happens to be true with Gnus now, but that may
4135       ;;!!! change in the future.  Perhaps.
4136       (gnus-data-enter-list
4137        (if line nil current) data (- (point) old-pos))
4138       (setq gnus-newsgroup-threads
4139             (nconc threads gnus-newsgroup-threads))
4140       (gnus-data-compute-positions))))
4141
4142 (defun gnus-number-to-header (number)
4143   "Return the header for article NUMBER."
4144   (let ((headers gnus-newsgroup-headers))
4145     (while (and headers
4146                 (not (= number (mail-header-number (car headers)))))
4147       (pop headers))
4148     (when headers
4149       (car headers))))
4150
4151 (defun gnus-parent-headers (in-headers &optional generation)
4152   "Return the headers of the GENERATIONeth parent of HEADERS."
4153   (unless generation
4154     (setq generation 1))
4155   (let ((parent t)
4156         (headers in-headers)
4157         references)
4158     (while (and parent
4159                 (not (zerop generation))
4160                 (setq references (mail-header-references headers)))
4161       (setq headers (if (and references
4162                              (setq parent (gnus-parent-id references)))
4163                         (car (gnus-id-to-thread parent))
4164                       nil))
4165       (decf generation))
4166     (and (not (eq headers in-headers))
4167          headers)))
4168
4169 (defun gnus-id-to-thread (id)
4170   "Return the (sub-)thread where ID appears."
4171   (gnus-gethash id gnus-newsgroup-dependencies))
4172
4173 (defun gnus-id-to-article (id)
4174   "Return the article number of ID."
4175   (let ((thread (gnus-id-to-thread id)))
4176     (when (and thread
4177                (car thread))
4178       (mail-header-number (car thread)))))
4179
4180 (defun gnus-id-to-header (id)
4181   "Return the article headers of ID."
4182   (car (gnus-id-to-thread id)))
4183
4184 (defun gnus-article-displayed-root-p (article)
4185   "Say whether ARTICLE is a root(ish) article."
4186   (let ((level (gnus-summary-thread-level article))
4187         (refs (mail-header-references  (gnus-summary-article-header article)))
4188         particle)
4189     (cond
4190      ((null level) nil)
4191      ((zerop level) t)
4192      ((null refs) t)
4193      ((null (gnus-parent-id refs)) t)
4194      ((and (= 1 level)
4195            (null (setq particle (gnus-id-to-article
4196                                  (gnus-parent-id refs))))
4197            (null (gnus-summary-thread-level particle)))))))
4198
4199 (defun gnus-root-id (id)
4200   "Return the id of the root of the thread where ID appears."
4201   (let (last-id prev)
4202     (while (and id (setq prev (car (gnus-id-to-thread id))))
4203       (setq last-id id
4204             id (gnus-parent-id (mail-header-references prev))))
4205     last-id))
4206
4207 (defun gnus-articles-in-thread (thread)
4208   "Return the list of articles in THREAD."
4209   (cons (mail-header-number (car thread))
4210         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4211
4212 (defun gnus-remove-thread (id &optional dont-remove)
4213   "Remove the thread that has ID in it."
4214   (let (headers thread last-id)
4215     ;; First go up in this thread until we find the root.
4216     (setq last-id (gnus-root-id id)
4217           headers (message-flatten-list (gnus-id-to-thread last-id)))
4218     ;; We have now found the real root of this thread.  It might have
4219     ;; been gathered into some loose thread, so we have to search
4220     ;; through the threads to find the thread we wanted.
4221     (let ((threads gnus-newsgroup-threads)
4222           sub)
4223       (while threads
4224         (setq sub (car threads))
4225         (if (stringp (car sub))
4226             ;; This is a gathered thread, so we look at the roots
4227             ;; below it to find whether this article is in this
4228             ;; gathered root.
4229             (progn
4230               (setq sub (cdr sub))
4231               (while sub
4232                 (when (member (caar sub) headers)
4233                   (setq thread (car threads)
4234                         threads nil
4235                         sub nil))
4236                 (setq sub (cdr sub))))
4237           ;; It's an ordinary thread, so we check it.
4238           (when (eq (car sub) (car headers))
4239             (setq thread sub
4240                   threads nil)))
4241         (setq threads (cdr threads)))
4242       ;; If this article is in no thread, then it's a root.
4243       (if thread
4244           (unless dont-remove
4245             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4246         (setq thread (gnus-id-to-thread last-id)))
4247       (when thread
4248         (prog1
4249             thread                      ; We return this thread.
4250           (unless dont-remove
4251             (if (stringp (car thread))
4252                 (progn
4253                   ;; If we use dummy roots, then we have to remove the
4254                   ;; dummy root as well.
4255                   (when (eq gnus-summary-make-false-root 'dummy)
4256                     ;; We go to the dummy root by going to
4257                     ;; the first sub-"thread", and then one line up.
4258                     (gnus-summary-goto-article
4259                      (mail-header-number (caadr thread)))
4260                     (forward-line -1)
4261                     (gnus-delete-line)
4262                     (gnus-data-compute-positions))
4263                   (setq thread (cdr thread))
4264                   (while thread
4265                     (gnus-remove-thread-1 (car thread))
4266                     (setq thread (cdr thread))))
4267               (gnus-remove-thread-1 thread))))))))
4268
4269 (defun gnus-remove-thread-1 (thread)
4270   "Remove the thread THREAD recursively."
4271   (let ((number (mail-header-number (pop thread)))
4272         d)
4273     (setq thread (reverse thread))
4274     (while thread
4275       (gnus-remove-thread-1 (pop thread)))
4276     (when (setq d (gnus-data-find number))
4277       (goto-char (gnus-data-pos d))
4278       (gnus-summary-show-thread)
4279       (gnus-data-remove
4280        number
4281        (- (gnus-point-at-bol)
4282           (prog1
4283               (1+ (gnus-point-at-eol))
4284             (gnus-delete-line)))))))
4285
4286 (defun gnus-sort-threads-1 (threads func)
4287   (sort (mapcar (lambda (thread)
4288                   (cons (car thread)
4289                         (and (cdr thread)
4290                              (gnus-sort-threads-1 (cdr thread) func))))
4291                 threads) func))
4292
4293 (defun gnus-sort-threads (threads)
4294   "Sort THREADS."
4295   (if (not gnus-thread-sort-functions)
4296       threads
4297     (gnus-message 8 "Sorting threads...")
4298     (let ((max-lisp-eval-depth 5000))
4299       (prog1 (gnus-sort-threads-1
4300          threads
4301          (gnus-make-sort-function gnus-thread-sort-functions))
4302         (gnus-message 8 "Sorting threads...done")))))
4303
4304 (defun gnus-sort-articles (articles)
4305   "Sort ARTICLES."
4306   (when gnus-article-sort-functions
4307     (gnus-message 7 "Sorting articles...")
4308     (prog1
4309         (setq gnus-newsgroup-headers
4310               (sort articles (gnus-make-sort-function
4311                               gnus-article-sort-functions)))
4312       (gnus-message 7 "Sorting articles...done"))))
4313
4314 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4315 (defmacro gnus-thread-header (thread)
4316   "Return header of first article in THREAD.
4317 Note that THREAD must never, ever be anything else than a variable -
4318 using some other form will lead to serious barfage."
4319   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4320   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4321   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4322         (vector thread) 2))
4323
4324 (defsubst gnus-article-sort-by-number (h1 h2)
4325   "Sort articles by article number."
4326   (< (mail-header-number h1)
4327      (mail-header-number h2)))
4328
4329 (defun gnus-thread-sort-by-number (h1 h2)
4330   "Sort threads by root article number."
4331   (gnus-article-sort-by-number
4332    (gnus-thread-header h1) (gnus-thread-header h2)))
4333
4334 (defsubst gnus-article-sort-by-random (h1 h2)
4335   "Sort articles by article number."
4336   (zerop (random 2)))
4337
4338 (defun gnus-thread-sort-by-random (h1 h2)
4339   "Sort threads by root article number."
4340   (gnus-article-sort-by-random
4341    (gnus-thread-header h1) (gnus-thread-header h2)))
4342
4343 (defsubst gnus-article-sort-by-lines (h1 h2)
4344   "Sort articles by article Lines header."
4345   (< (mail-header-lines h1)
4346      (mail-header-lines h2)))
4347
4348 (defun gnus-thread-sort-by-lines (h1 h2)
4349   "Sort threads by root article Lines header."
4350   (gnus-article-sort-by-lines
4351    (gnus-thread-header h1) (gnus-thread-header h2)))
4352
4353 (defsubst gnus-article-sort-by-chars (h1 h2)
4354   "Sort articles by octet length."
4355   (< (mail-header-chars h1)
4356      (mail-header-chars h2)))
4357
4358 (defun gnus-thread-sort-by-chars (h1 h2)
4359   "Sort threads by root article octet length."
4360   (gnus-article-sort-by-chars
4361    (gnus-thread-header h1) (gnus-thread-header h2)))
4362
4363 (defsubst gnus-article-sort-by-author (h1 h2)
4364   "Sort articles by root author."
4365   (string-lessp
4366    (let ((extract (funcall
4367                    gnus-extract-address-components
4368                    (mail-header-from h1))))
4369      (or (car extract) (cadr extract) ""))
4370    (let ((extract (funcall
4371                    gnus-extract-address-components
4372                    (mail-header-from h2))))
4373      (or (car extract) (cadr extract) ""))))
4374
4375 (defun gnus-thread-sort-by-author (h1 h2)
4376   "Sort threads by root author."
4377   (gnus-article-sort-by-author
4378    (gnus-thread-header h1)  (gnus-thread-header h2)))
4379
4380 (defsubst gnus-article-sort-by-subject (h1 h2)
4381   "Sort articles by root subject."
4382   (string-lessp
4383    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4384    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4385
4386 (defun gnus-thread-sort-by-subject (h1 h2)
4387   "Sort threads by root subject."
4388   (gnus-article-sort-by-subject
4389    (gnus-thread-header h1) (gnus-thread-header h2)))
4390
4391 (defsubst gnus-article-sort-by-date (h1 h2)
4392   "Sort articles by root article date."
4393   (time-less-p
4394    (gnus-date-get-time (mail-header-date h1))
4395    (gnus-date-get-time (mail-header-date h2))))
4396
4397 (defun gnus-thread-sort-by-date (h1 h2)
4398   "Sort threads by root article date."
4399   (gnus-article-sort-by-date
4400    (gnus-thread-header h1) (gnus-thread-header h2)))
4401
4402 (defsubst gnus-article-sort-by-score (h1 h2)
4403   "Sort articles by root article score.
4404 Unscored articles will be counted as having a score of zero."
4405   (> (or (cdr (assq (mail-header-number h1)
4406                     gnus-newsgroup-scored))
4407          gnus-summary-default-score 0)
4408      (or (cdr (assq (mail-header-number h2)
4409                     gnus-newsgroup-scored))
4410          gnus-summary-default-score 0)))
4411
4412 (defun gnus-thread-sort-by-score (h1 h2)
4413   "Sort threads by root article score."
4414   (gnus-article-sort-by-score
4415    (gnus-thread-header h1) (gnus-thread-header h2)))
4416
4417 (defun gnus-thread-sort-by-total-score (h1 h2)
4418   "Sort threads by the sum of all scores in the thread.
4419 Unscored articles will be counted as having a score of zero."
4420   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4421
4422 (defun gnus-thread-total-score (thread)
4423   ;; This function find the total score of THREAD.
4424   (cond
4425    ((null thread)
4426     0)
4427    ((consp thread)
4428     (if (stringp (car thread))
4429         (apply gnus-thread-score-function 0
4430                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4431       (gnus-thread-total-score-1 thread)))
4432    (t
4433     (gnus-thread-total-score-1 (list thread)))))
4434
4435 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4436   "Sort threads such that the thread with the most recently arrived article comes first."
4437   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4438
4439 (defun gnus-thread-highest-number (thread)
4440   "Return the highest article number in THREAD."
4441   (apply 'max (mapcar (lambda (header)
4442                         (mail-header-number header))
4443                       (message-flatten-list thread))))
4444
4445 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4446   "Sort threads such that the thread with the most recently dated article comes first."
4447   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4448
4449 (defun gnus-thread-latest-date (thread)
4450   "Return the highest article date in THREAD."
4451   (let ((previous-time 0))
4452     (apply 'max
4453            (mapcar
4454             (lambda (header)
4455               (setq previous-time
4456                     (time-to-seconds
4457                      (condition-case ()
4458                          (mail-header-parse-date (mail-header-date header))
4459                        (error previous-time)))))
4460             (sort
4461              (message-flatten-list thread)
4462              (lambda (h1 h2)
4463                (< (mail-header-number h1)
4464                   (mail-header-number h2))))))))
4465
4466 (defun gnus-thread-total-score-1 (root)
4467   ;; This function find the total score of the thread below ROOT.
4468   (setq root (car root))
4469   (apply gnus-thread-score-function
4470          (or (append
4471               (mapcar 'gnus-thread-total-score
4472                       (cdr (gnus-id-to-thread (mail-header-id root))))
4473               (when (> (mail-header-number root) 0)
4474                 (list (or (cdr (assq (mail-header-number root)
4475                                      gnus-newsgroup-scored))
4476                           gnus-summary-default-score 0))))
4477              (list gnus-summary-default-score)
4478              '(0))))
4479
4480 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4481 (defvar gnus-tmp-prev-subject nil)
4482 (defvar gnus-tmp-false-parent nil)
4483 (defvar gnus-tmp-root-expunged nil)
4484 (defvar gnus-tmp-dummy-line nil)
4485
4486 (eval-when-compile (defvar gnus-tmp-header))
4487 (defun gnus-extra-header (type &optional header)
4488   "Return the extra header of TYPE."
4489   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4490       ""))
4491
4492 (defvar gnus-tmp-thread-tree-header-string "")
4493
4494 (defcustom gnus-sum-thread-tree-root "> "
4495   "With %B spec, used for the root of a thread.
4496 If nil, use subject instead."
4497   :type 'string
4498   :group 'gnus-thread)
4499 (defcustom gnus-sum-thread-tree-single-indent ""
4500   "With %B spec, used for a thread with just one message.
4501 If nil, use subject instead."
4502   :type 'string
4503   :group 'gnus-thread)
4504 (defcustom gnus-sum-thread-tree-vertical "| "
4505   "With %B spec, used for drawing a vertical line."
4506   :type 'string
4507   :group 'gnus-thread)
4508 (defcustom gnus-sum-thread-tree-indent "  "
4509   "With %B spec, used for indenting."
4510   :type 'string
4511   :group 'gnus-thread)
4512 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4513   "With %B spec, used for a leaf with brothers."
4514   :type 'string
4515   :group 'gnus-thread)
4516 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4517   "With %B spec, used for a leaf without brothers."
4518   :type 'string
4519   :group 'gnus-thread)
4520
4521 (defun gnus-summary-prepare-threads (threads)
4522   "Prepare summary buffer from THREADS and indentation LEVEL.
4523 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4524 or a straight list of headers."
4525   (gnus-message 7 "Generating summary...")
4526
4527   (setq gnus-newsgroup-threads threads)
4528   (beginning-of-line)
4529
4530   (let ((gnus-tmp-level 0)
4531         (default-score (or gnus-summary-default-score 0))
4532         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4533         (building-line-count gnus-summary-display-while-building)
4534         (building-count (integerp gnus-summary-display-while-building))
4535         thread number subject stack state gnus-tmp-gathered beg-match
4536         new-roots gnus-tmp-new-adopts thread-end simp-subject
4537         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4538         gnus-tmp-replied gnus-tmp-subject-or-nil
4539         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4540         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4541         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4542         tree-stack)
4543
4544     (setq gnus-tmp-prev-subject nil)
4545
4546     (if (vectorp (car threads))
4547         ;; If this is a straight (sic) list of headers, then a
4548         ;; threaded summary display isn't required, so we just create
4549         ;; an unthreaded one.
4550         (gnus-summary-prepare-unthreaded threads)
4551
4552       ;; Do the threaded display.
4553
4554       (if gnus-summary-display-while-building
4555           (switch-to-buffer (buffer-name)))
4556       (while (or threads stack gnus-tmp-new-adopts new-roots)
4557
4558         (if (and (= gnus-tmp-level 0)
4559                  (or (not stack)
4560                      (= (caar stack) 0))
4561                  (not gnus-tmp-false-parent)
4562                  (or gnus-tmp-new-adopts new-roots))
4563             (if gnus-tmp-new-adopts
4564                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4565                       thread (list (car gnus-tmp-new-adopts))
4566                       gnus-tmp-header (caar thread)
4567                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4568               (when new-roots
4569                 (setq thread (list (car new-roots))
4570                       gnus-tmp-header (caar thread)
4571                       new-roots (cdr new-roots))))
4572
4573           (if threads
4574               ;; If there are some threads, we do them before the
4575               ;; threads on the stack.
4576               (setq thread threads
4577                     gnus-tmp-header (caar thread))
4578             ;; There were no current threads, so we pop something off
4579             ;; the stack.
4580             (setq state (car stack)
4581                   gnus-tmp-level (car state)
4582                   tree-stack (cadr state)
4583                   thread (caddr state)
4584                   stack (cdr stack)
4585                   gnus-tmp-header (caar thread))))
4586
4587         (setq gnus-tmp-false-parent nil)
4588         (setq gnus-tmp-root-expunged nil)
4589         (setq thread-end nil)
4590
4591         (if (stringp gnus-tmp-header)
4592             ;; The header is a dummy root.
4593             (cond
4594              ((eq gnus-summary-make-false-root 'adopt)
4595               ;; We let the first article adopt the rest.
4596               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4597                                                (cddar thread)))
4598               (setq gnus-tmp-gathered
4599                     (nconc (mapcar
4600                             (lambda (h) (mail-header-number (car h)))
4601                             (cddar thread))
4602                            gnus-tmp-gathered))
4603               (setq thread (cons (list (caar thread)
4604                                        (cadar thread))
4605                                  (cdr thread)))
4606               (setq gnus-tmp-level -1
4607                     gnus-tmp-false-parent t))
4608              ((eq gnus-summary-make-false-root 'empty)
4609               ;; We print adopted articles with empty subject fields.
4610               (setq gnus-tmp-gathered
4611                     (nconc (mapcar
4612                             (lambda (h) (mail-header-number (car h)))
4613                             (cddar thread))
4614                            gnus-tmp-gathered))
4615               (setq gnus-tmp-level -1))
4616              ((eq gnus-summary-make-false-root 'dummy)
4617               ;; We remember that we probably want to output a dummy
4618               ;; root.
4619               (setq gnus-tmp-dummy-line gnus-tmp-header)
4620               (setq gnus-tmp-prev-subject gnus-tmp-header))
4621              (t
4622               ;; We do not make a root for the gathered
4623               ;; sub-threads at all.
4624               (setq gnus-tmp-level -1)))
4625
4626           (setq number (mail-header-number gnus-tmp-header)
4627                 subject (mail-header-subject gnus-tmp-header)
4628                 simp-subject (gnus-simplify-subject-fully subject))
4629
4630           (cond
4631            ;; If the thread has changed subject, we might want to make
4632            ;; this subthread into a root.
4633            ((and (null gnus-thread-ignore-subject)
4634                  (not (zerop gnus-tmp-level))
4635                  gnus-tmp-prev-subject
4636                  (not (string= gnus-tmp-prev-subject simp-subject)))
4637             (setq new-roots (nconc new-roots (list (car thread)))
4638                   thread-end t
4639                   gnus-tmp-header nil))
4640            ;; If the article lies outside the current limit,
4641            ;; then we do not display it.
4642            ((not (memq number gnus-newsgroup-limit))
4643             (setq gnus-tmp-gathered
4644                   (nconc (mapcar
4645                           (lambda (h) (mail-header-number (car h)))
4646                           (cdar thread))
4647                          gnus-tmp-gathered))
4648             (setq gnus-tmp-new-adopts (if (cdar thread)
4649                                           (append gnus-tmp-new-adopts
4650                                                   (cdar thread))
4651                                         gnus-tmp-new-adopts)
4652                   thread-end t
4653                   gnus-tmp-header nil)
4654             (when (zerop gnus-tmp-level)
4655               (setq gnus-tmp-root-expunged t)))
4656            ;; Perhaps this article is to be marked as read?
4657            ((and gnus-summary-mark-below
4658                  (< (or (cdr (assq number gnus-newsgroup-scored))
4659                         default-score)
4660                     gnus-summary-mark-below)
4661                  ;; Don't touch sparse articles.
4662                  (not (gnus-summary-article-sparse-p number))
4663                  (not (gnus-summary-article-ancient-p number)))
4664             (setq gnus-newsgroup-unreads
4665                   (delq number gnus-newsgroup-unreads))
4666             (if gnus-newsgroup-auto-expire
4667                 (setq gnus-newsgroup-expirable
4668                       (gnus-add-to-sorted-list
4669                        gnus-newsgroup-expirable number))
4670               (push (cons number gnus-low-score-mark)
4671                     gnus-newsgroup-reads))))
4672
4673           (when gnus-tmp-header
4674             ;; We may have an old dummy line to output before this
4675             ;; article.
4676             (when (and gnus-tmp-dummy-line
4677                        (gnus-subject-equal
4678                         gnus-tmp-dummy-line
4679                         (mail-header-subject gnus-tmp-header)))
4680               (gnus-summary-insert-dummy-line
4681                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4682               (setq gnus-tmp-dummy-line nil))
4683
4684             ;; Compute the mark.
4685             (setq gnus-tmp-unread (gnus-article-mark number))
4686
4687             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4688                                   gnus-tmp-header gnus-tmp-level)
4689                   gnus-newsgroup-data)
4690
4691             ;; Actually insert the line.
4692             (setq
4693              gnus-tmp-subject-or-nil
4694              (cond
4695               ((and gnus-thread-ignore-subject
4696                     gnus-tmp-prev-subject
4697                     (not (string= gnus-tmp-prev-subject simp-subject)))
4698                subject)
4699               ((zerop gnus-tmp-level)
4700                (if (and (eq gnus-summary-make-false-root 'empty)
4701                         (memq number gnus-tmp-gathered)
4702                         gnus-tmp-prev-subject
4703                         (string= gnus-tmp-prev-subject simp-subject))
4704                    gnus-summary-same-subject
4705                  subject))
4706               (t gnus-summary-same-subject)))
4707             (if (and (eq gnus-summary-make-false-root 'adopt)
4708                      (= gnus-tmp-level 1)
4709                      (memq number gnus-tmp-gathered))
4710                 (setq gnus-tmp-opening-bracket ?\<
4711                       gnus-tmp-closing-bracket ?\>)
4712               (setq gnus-tmp-opening-bracket ?\[
4713                     gnus-tmp-closing-bracket ?\]))
4714             (setq
4715              gnus-tmp-indentation
4716              (aref gnus-thread-indent-array gnus-tmp-level)
4717              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4718              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4719                                 gnus-summary-default-score 0)
4720              gnus-tmp-score-char
4721              (if (or (null gnus-summary-default-score)
4722                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4723                          gnus-summary-zcore-fuzz))
4724                  ?                      ;Whitespace
4725                (if (< gnus-tmp-score gnus-summary-default-score)
4726                    gnus-score-below-mark gnus-score-over-mark))
4727              gnus-tmp-replied
4728              (cond ((memq number gnus-newsgroup-processable)
4729                     gnus-process-mark)
4730                    ((memq number gnus-newsgroup-cached)
4731                     gnus-cached-mark)
4732                    ((memq number gnus-newsgroup-replied)
4733                     gnus-replied-mark)
4734                    ((memq number gnus-newsgroup-forwarded)
4735                     gnus-forwarded-mark)
4736                    ((memq number gnus-newsgroup-saved)
4737                     gnus-saved-mark)
4738                    ((memq number gnus-newsgroup-recent)
4739                     gnus-recent-mark)
4740                    ((memq number gnus-newsgroup-unseen)
4741                     gnus-unseen-mark)
4742                    (t gnus-no-mark))
4743              gnus-tmp-downloaded
4744              (cond ((memq number gnus-newsgroup-undownloaded) 
4745                     gnus-undownloaded-mark)
4746                    (gnus-newsgroup-agentized
4747                     gnus-downloaded-mark)
4748                    (t
4749                     gnus-no-mark))
4750              gnus-tmp-from (mail-header-from gnus-tmp-header)
4751              gnus-tmp-name
4752              (cond
4753               ((string-match "<[^>]+> *$" gnus-tmp-from)
4754                (setq beg-match (match-beginning 0))
4755                (or (and (string-match "^\".+\"" gnus-tmp-from)
4756                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4757                    (substring gnus-tmp-from 0 beg-match)))
4758               ((string-match "(.+)" gnus-tmp-from)
4759                (substring gnus-tmp-from
4760                           (1+ (match-beginning 0)) (1- (match-end 0))))
4761               (t gnus-tmp-from))
4762              gnus-tmp-thread-tree-header-string
4763              (cond
4764               ((not gnus-show-threads) "")
4765               ((zerop gnus-tmp-level)
4766                (if (cdar thread)
4767                    (or gnus-sum-thread-tree-root subject)
4768                  (or gnus-sum-thread-tree-single-indent subject)))
4769               (t
4770                (concat (apply 'concat
4771                               (mapcar (lambda (item)
4772                                         (if (= item 1)
4773                                             gnus-sum-thread-tree-vertical
4774                                           gnus-sum-thread-tree-indent))
4775                                       (cdr (reverse tree-stack))))
4776                        (if (nth 1 thread)
4777                            gnus-sum-thread-tree-leaf-with-other
4778                          gnus-sum-thread-tree-single-leaf)))))
4779             (when (string= gnus-tmp-name "")
4780               (setq gnus-tmp-name gnus-tmp-from))
4781             (unless (numberp gnus-tmp-lines)
4782               (setq gnus-tmp-lines -1))
4783             (if (= gnus-tmp-lines -1)
4784                 (setq gnus-tmp-lines "?")
4785               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4786             (gnus-put-text-property
4787              (point)
4788              (progn (eval gnus-summary-line-format-spec) (point))
4789              'gnus-number number)
4790             (when gnus-visual-p
4791               (forward-line -1)
4792               (gnus-run-hooks 'gnus-summary-update-hook)
4793               (forward-line 1))
4794
4795             (setq gnus-tmp-prev-subject simp-subject)))
4796
4797         (when (nth 1 thread)
4798           (push (list (max 0 gnus-tmp-level)
4799                       (copy-list tree-stack)
4800                       (nthcdr 1 thread))
4801                 stack))
4802         (push (if (nth 1 thread) 1 0) tree-stack)
4803         (incf gnus-tmp-level)
4804         (setq threads (if thread-end nil (cdar thread)))
4805         (if gnus-summary-display-while-building
4806             (if building-count
4807                 (progn
4808                   ;; use a set frequency
4809                   (setq building-line-count (1- building-line-count))
4810                   (when (= building-line-count 0)
4811                     (sit-for 0)
4812                     (setq building-line-count
4813                           gnus-summary-display-while-building)))
4814               ;; always
4815               (sit-for 0)))
4816         (unless threads
4817           (setq gnus-tmp-level 0)))))
4818   (gnus-message 7 "Generating summary...done"))
4819
4820 (defun gnus-summary-prepare-unthreaded (headers)
4821   "Generate an unthreaded summary buffer based on HEADERS."
4822   (let (header number mark)
4823
4824     (beginning-of-line)
4825
4826     (while headers
4827       ;; We may have to root out some bad articles...
4828       (when (memq (setq number (mail-header-number
4829                                 (setq header (pop headers))))
4830                   gnus-newsgroup-limit)
4831         ;; Mark article as read when it has a low score.
4832         (when (and gnus-summary-mark-below
4833                    (< (or (cdr (assq number gnus-newsgroup-scored))
4834                           gnus-summary-default-score 0)
4835                       gnus-summary-mark-below)
4836                    (not (gnus-summary-article-ancient-p number)))
4837           (setq gnus-newsgroup-unreads
4838                 (delq number gnus-newsgroup-unreads))
4839           (if gnus-newsgroup-auto-expire
4840               (push number gnus-newsgroup-expirable)
4841             (push (cons number gnus-low-score-mark)
4842                   gnus-newsgroup-reads)))
4843
4844         (setq mark (gnus-article-mark number))
4845         (push (gnus-data-make number mark (1+ (point)) header 0)
4846               gnus-newsgroup-data)
4847         (gnus-summary-insert-line
4848          header 0 number
4849          (memq number gnus-newsgroup-undownloaded)
4850          mark (memq number gnus-newsgroup-replied)
4851          (memq number gnus-newsgroup-expirable)
4852          (mail-header-subject header) nil
4853          (cdr (assq number gnus-newsgroup-scored))
4854          (memq number gnus-newsgroup-processable))))))
4855
4856 (defun gnus-summary-remove-list-identifiers ()
4857   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4858   (let ((regexp (if (consp gnus-list-identifiers)
4859                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4860                   gnus-list-identifiers))
4861         changed subject)
4862     (when regexp
4863       (dolist (header gnus-newsgroup-headers)
4864         (setq subject (mail-header-subject header)
4865               changed nil)
4866         (while (string-match
4867                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4868                 subject)
4869           (setq subject
4870                 (concat (substring subject 0 (match-beginning 2))
4871                         (substring subject (match-end 0)))
4872                 changed t))
4873         (when (and changed
4874                    (string-match
4875                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4876           (setq subject
4877                 (concat (substring subject 0 (match-beginning 1))
4878                         (substring subject (match-end 1)))))
4879         (when changed
4880           (mail-header-set-subject header subject))))))
4881
4882 (defun gnus-fetch-headers (articles)
4883   "Fetch headers of ARTICLES."
4884   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4885     (gnus-message 5 "Fetching headers for %s..." name)
4886     (prog1
4887         (if (eq 'nov
4888                 (setq gnus-headers-retrieved-by
4889                       (gnus-retrieve-headers
4890                        articles gnus-newsgroup-name
4891                        ;; We might want to fetch old headers, but
4892                        ;; not if there is only 1 article.
4893                        (and (or (and
4894                                  (not (eq gnus-fetch-old-headers 'some))
4895                                  (not (numberp gnus-fetch-old-headers)))
4896                                 (> (length articles) 1))
4897                             gnus-fetch-old-headers))))
4898             (gnus-get-newsgroup-headers-xover
4899              articles nil nil gnus-newsgroup-name t)
4900           (gnus-get-newsgroup-headers))
4901       (gnus-message 5 "Fetching headers for %s...done" name))))
4902
4903 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4904   "Select newsgroup GROUP.
4905 If READ-ALL is non-nil, all articles in the group are selected.
4906 If SELECT-ARTICLES, only select those articles from GROUP."
4907   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4908          ;;!!! Dirty hack; should be removed.
4909          (gnus-summary-ignore-duplicates
4910           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4911               t
4912             gnus-summary-ignore-duplicates))
4913          (info (nth 2 entry))
4914          articles fetched-articles cached)
4915
4916     (unless (gnus-check-server
4917              (set (make-local-variable 'gnus-current-select-method)
4918                   (gnus-find-method-for-group group)))
4919       (error "Couldn't open server"))
4920
4921     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4922         (gnus-activate-group group)     ; Or we can activate it...
4923         (progn                          ; Or we bug out.
4924           (when (equal major-mode 'gnus-summary-mode)
4925             (kill-buffer (current-buffer)))
4926           (error "Couldn't activate group %s: %s"
4927                  group (gnus-status-message group))))
4928
4929     (unless (gnus-request-group group t)
4930       (when (equal major-mode 'gnus-summary-mode)
4931         (kill-buffer (current-buffer)))
4932       (error "Couldn't request group %s: %s"
4933              group (gnus-status-message group)))
4934
4935     (setq gnus-newsgroup-name group
4936           gnus-newsgroup-unselected nil
4937           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4938
4939     (let ((display (gnus-group-find-parameter group 'display)))
4940       (setq gnus-newsgroup-display
4941             (cond
4942              ((not (zerop (or (car-safe read-all) 0)))
4943               ;; The user entered the group with C-u SPC/RET, let's show
4944               ;; all articles.
4945               'gnus-not-ignore)
4946              ((eq display 'all)
4947               'gnus-not-ignore)
4948              ((arrayp display)
4949               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4950              ((numberp display)
4951               ;; The following is probably the "correct" solution, but
4952               ;; it makes Gnus fetch all headers and then limit the
4953               ;; articles (which is slow), so instead we hack the
4954               ;; select-articles parameter instead. -- Simon Josefsson
4955               ;; <jas@kth.se>
4956               ;;
4957               ;; (gnus-byte-compile
4958               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4959               ;;                         display)))))
4960               (setq select-articles
4961                     (gnus-uncompress-range
4962                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4963                              (if (> tmp 0)
4964                                  tmp
4965                                1))
4966                            (cdr (gnus-active group)))))
4967               nil)
4968              (t
4969               nil))))
4970
4971     (gnus-summary-setup-default-charset)
4972
4973     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4974     (when (gnus-virtual-group-p group)
4975       (setq cached gnus-newsgroup-cached))
4976
4977     (setq gnus-newsgroup-unreads
4978           (gnus-sorted-ndifference
4979            (gnus-sorted-ndifference gnus-newsgroup-unreads
4980                                     gnus-newsgroup-marked)
4981            gnus-newsgroup-dormant))
4982
4983     (setq gnus-newsgroup-processable nil)
4984
4985     (gnus-update-read-articles group gnus-newsgroup-unreads)
4986
4987     ;; Adjust and set lists of article marks.
4988     (when info
4989       (gnus-adjust-marked-articles info))
4990     (if (setq articles select-articles)
4991         (setq gnus-newsgroup-unselected
4992               (gnus-sorted-difference gnus-newsgroup-unreads articles))
4993       (setq articles (gnus-articles-to-read group read-all)))
4994
4995     (cond
4996      ((null articles)
4997       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4998       'quit)
4999      ((eq articles 0) nil)
5000      (t
5001       ;; Init the dependencies hash table.
5002       (setq gnus-newsgroup-dependencies
5003             (gnus-make-hashtable (length articles)))
5004       (gnus-set-global-variables)
5005       ;; Retrieve the headers and read them in.
5006
5007       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5008
5009       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5010       (when cached
5011         (setq gnus-newsgroup-cached cached))
5012
5013       ;; Suppress duplicates?
5014       (when gnus-suppress-duplicates
5015         (gnus-dup-suppress-articles))
5016
5017       ;; Set the initial limit.
5018       (setq gnus-newsgroup-limit (copy-sequence articles))
5019       ;; Remove canceled articles from the list of unread articles.
5020       (setq fetched-articles
5021             (mapcar (lambda (headers) (mail-header-number headers))
5022                     gnus-newsgroup-headers))
5023       (setq gnus-newsgroup-articles fetched-articles)
5024       (setq gnus-newsgroup-unreads
5025             (gnus-sorted-nintersection
5026              gnus-newsgroup-unreads fetched-articles))
5027       (gnus-compute-unseen-list)
5028
5029       ;; Removed marked articles that do not exist.
5030       (gnus-update-missing-marks
5031        (gnus-sorted-difference articles fetched-articles))
5032       ;; We might want to build some more threads first.
5033       (when (and gnus-fetch-old-headers
5034                  (eq gnus-headers-retrieved-by 'nov))
5035         (if (eq gnus-fetch-old-headers 'invisible)
5036             (gnus-build-all-threads)
5037           (gnus-build-old-threads)))
5038       ;; Let the Gnus agent mark articles as read.
5039       (when gnus-agent
5040         (gnus-agent-get-undownloaded-list))
5041       ;; Remove list identifiers from subject
5042       (when gnus-list-identifiers
5043         (gnus-summary-remove-list-identifiers))
5044       ;; Check whether auto-expire is to be done in this group.
5045       (setq gnus-newsgroup-auto-expire
5046             (gnus-group-auto-expirable-p group))
5047       ;; Set up the article buffer now, if necessary.
5048       (unless gnus-single-article-buffer
5049         (gnus-article-setup-buffer))
5050       ;; First and last article in this newsgroup.
5051       (when gnus-newsgroup-headers
5052         (setq gnus-newsgroup-begin
5053               (mail-header-number (car gnus-newsgroup-headers))
5054               gnus-newsgroup-end
5055               (mail-header-number
5056                (gnus-last-element gnus-newsgroup-headers))))
5057       ;; GROUP is successfully selected.
5058       (or gnus-newsgroup-headers t)))))
5059
5060 (defun gnus-compute-unseen-list ()
5061   ;; The `seen' marks are treated specially.
5062   (if (not gnus-newsgroup-seen)
5063       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5064     (setq gnus-newsgroup-unseen
5065           (gnus-inverse-list-range-intersection
5066            gnus-newsgroup-articles gnus-newsgroup-seen))))
5067
5068 (defun gnus-summary-display-make-predicate (display)
5069   (require 'gnus-agent)
5070   (when (= (length display) 1)
5071     (setq display (car display)))
5072   (unless gnus-summary-display-cache
5073     (dolist (elem (append '((unread . unread)
5074                             (read . read)
5075                             (unseen . unseen))
5076                           gnus-article-mark-lists))
5077       (push (cons (cdr elem)
5078                   (gnus-byte-compile
5079                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5080             gnus-summary-display-cache)))
5081   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5082         (gnus-category-predicate-cache gnus-summary-display-cache))
5083     (gnus-get-predicate display)))
5084
5085 ;; Uses the dynamically bound `number' variable.
5086 (eval-when-compile
5087   (defvar number))
5088 (defun gnus-article-marked-p (type &optional article)
5089   (let ((article (or article number)))
5090     (cond
5091      ((eq type 'tick)
5092       (memq article gnus-newsgroup-marked))
5093      ((eq type 'spam)
5094       (memq article gnus-newsgroup-spam-marked))
5095      ((eq type 'unsend)
5096       (memq article gnus-newsgroup-unsendable))
5097      ((eq type 'undownload)
5098       (memq article gnus-newsgroup-undownloaded))
5099      ((eq type 'download)
5100       (memq article gnus-newsgroup-downloadable))
5101      ((eq type 'unread)
5102       (memq article gnus-newsgroup-unreads))
5103      ((eq type 'read)
5104       (memq article gnus-newsgroup-reads))
5105      ((eq type 'dormant)
5106       (memq article gnus-newsgroup-dormant) )
5107      ((eq type 'expire)
5108       (memq article gnus-newsgroup-expirable))
5109      ((eq type 'reply)
5110       (memq article gnus-newsgroup-replied))
5111      ((eq type 'killed)
5112       (memq article gnus-newsgroup-killed))
5113      ((eq type 'bookmark)
5114       (assq article gnus-newsgroup-bookmarks))
5115      ((eq type 'score)
5116       (assq article gnus-newsgroup-scored))
5117      ((eq type 'save)
5118       (memq article gnus-newsgroup-saved))
5119      ((eq type 'cache)
5120       (memq article gnus-newsgroup-cached))
5121      ((eq type 'forward)
5122       (memq article gnus-newsgroup-forwarded))
5123      ((eq type 'seen)
5124       (not (memq article gnus-newsgroup-unseen)))
5125      ((eq type 'recent)
5126       (memq article gnus-newsgroup-recent))
5127      (t t))))
5128
5129 (defun gnus-articles-to-read (group &optional read-all)
5130   "Find out what articles the user wants to read."
5131   (let* ((display (gnus-group-find-parameter group 'display))
5132          (articles
5133           ;; Select all articles if `read-all' is non-nil, or if there
5134           ;; are no unread articles.
5135           (if (or read-all
5136                   (and (zerop (length gnus-newsgroup-marked))
5137                        (zerop (length gnus-newsgroup-unreads)))
5138                   ;; Fetch all if the predicate is non-nil.
5139                   gnus-newsgroup-display)
5140               ;; We want to select the headers for all the articles in
5141               ;; the group, so we select either all the active
5142               ;; articles in the group, or (if that's nil), the
5143               ;; articles in the cache.
5144               (or
5145                (gnus-uncompress-range (gnus-active group))
5146                (gnus-cache-articles-in-group group))
5147             ;; Select only the "normal" subset of articles.
5148             (gnus-sorted-nunion
5149              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5150              gnus-newsgroup-unreads)))
5151          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5152          (scored (length scored-list))
5153          (number (length articles))
5154          (marked (+ (length gnus-newsgroup-marked)
5155                     (length gnus-newsgroup-dormant)))
5156          (select
5157           (cond
5158            ((numberp read-all)
5159             read-all)
5160            ((numberp gnus-newsgroup-display)
5161             gnus-newsgroup-display)
5162            (t
5163             (condition-case ()
5164                 (cond
5165                  ((and (or (<= scored marked) (= scored number))
5166                        (numberp gnus-large-newsgroup)
5167                        (> number gnus-large-newsgroup))
5168                   (let* ((cursor-in-echo-area nil)
5169                          (initial (gnus-parameter-large-newsgroup-initial
5170                                    gnus-newsgroup-name))
5171                          (input
5172                           (read-string
5173                            (format
5174                             "How many articles from %s (%s %d): "
5175                             (gnus-limit-string
5176                              (gnus-group-decoded-name gnus-newsgroup-name)
5177                              35)
5178                             (if initial "max" "default")
5179                             number)
5180                            (if initial
5181                                (cons (number-to-string initial)
5182                                      0)))))
5183                     (if (string-match "^[ \t]*$" input) number input)))
5184                  ((and (> scored marked) (< scored number)
5185                        (> (- scored number) 20))
5186                   (let ((input
5187                          (read-string
5188                           (format "%s %s (%d scored, %d total): "
5189                                   "How many articles from"
5190                                   (gnus-group-decoded-name group)
5191                                   scored number))))
5192                     (if (string-match "^[ \t]*$" input)
5193                         number input)))
5194                  (t number))
5195               (quit
5196                (message "Quit getting the articles to read")
5197                nil))))))
5198     (setq select (if (stringp select) (string-to-number select) select))
5199     (if (or (null select) (zerop select))
5200         select
5201       (if (and (not (zerop scored)) (<= (abs select) scored))
5202           (progn
5203             (setq articles (sort scored-list '<))
5204             (setq number (length articles)))
5205         (setq articles (copy-sequence articles)))
5206
5207       (when (< (abs select) number)
5208         (if (< select 0)
5209             ;; Select the N oldest articles.
5210             (setcdr (nthcdr (1- (abs select)) articles) nil)
5211           ;; Select the N most recent articles.
5212           (setq articles (nthcdr (- number select) articles))))
5213       (setq gnus-newsgroup-unselected
5214             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5215       (when gnus-alter-articles-to-read-function
5216         (setq articles
5217               (sort
5218                (funcall gnus-alter-articles-to-read-function
5219                         gnus-newsgroup-name articles)
5220                '<)))
5221       articles)))
5222
5223 (defun gnus-killed-articles (killed articles)
5224   (let (out)
5225     (while articles
5226       (when (inline (gnus-member-of-range (car articles) killed))
5227         (push (car articles) out))
5228       (setq articles (cdr articles)))
5229     out))
5230
5231 (defun gnus-uncompress-marks (marks)
5232   "Uncompress the mark ranges in MARKS."
5233   (let ((uncompressed '(score bookmark))
5234         out)
5235     (while marks
5236       (if (memq (caar marks) uncompressed)
5237           (push (car marks) out)
5238         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5239       (setq marks (cdr marks)))
5240     out))
5241
5242 (defun gnus-article-mark-to-type (mark)
5243   "Return the type of MARK."
5244   (or (cadr (assq mark gnus-article-special-mark-lists))
5245       'list))
5246
5247 (defun gnus-article-unpropagatable-p (mark)
5248   "Return whether MARK should be propagated to backend."
5249   (memq mark gnus-article-unpropagated-mark-lists))
5250
5251 (defun gnus-adjust-marked-articles (info)
5252   "Set all article lists and remove all marks that are no longer valid."
5253   (let* ((marked-lists (gnus-info-marks info))
5254          (active (gnus-active (gnus-info-group info)))
5255          (min (car active))
5256          (max (cdr active))
5257          (types gnus-article-mark-lists)
5258          marks var articles article mark mark-type)
5259
5260     (dolist (marks marked-lists)
5261       (setq mark (car marks)
5262             mark-type (gnus-article-mark-to-type mark)
5263             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5264
5265       ;; We set the variable according to the type of the marks list,
5266       ;; and then adjust the marks to a subset of the active articles.
5267       (cond
5268        ;; Adjust "simple" lists.
5269        ((eq mark-type 'list)
5270         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5271         (when (memq mark '(tick dormant expire reply save))
5272           (while articles
5273             (when (or (< (setq article (pop articles)) min) (> article max))
5274               (set var (delq article (symbol-value var)))))))
5275        ;; Adjust assocs.
5276        ((eq mark-type 'tuple)
5277         (set var (setq articles (cdr marks)))
5278         (when (not (listp (cdr (symbol-value var))))
5279           (set var (list (symbol-value var))))
5280         (when (not (listp (cdr articles)))
5281           (setq articles (list articles)))
5282         (while articles
5283           (when (or (not (consp (setq article (pop articles))))
5284                     (< (car article) min)
5285                     (> (car article) max))
5286             (set var (delq article (symbol-value var))))))
5287        ;; Adjust ranges (sloppily).
5288        ((eq mark-type 'range)
5289         (cond
5290          ((eq mark 'seen)
5291           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5292           ;; It should be (seen (NUM1 . NUM2)).
5293           (when (numberp (cddr marks))
5294             (setcdr marks (list (cdr marks))))
5295           (setq articles (cdr marks))
5296           (while (and articles
5297                       (or (and (consp (car articles))
5298                                (> min (cdar articles)))
5299                           (and (numberp (car articles))
5300                                (> min (car articles)))))
5301             (pop articles))
5302           (set var articles))))))))
5303
5304 (defun gnus-update-missing-marks (missing)
5305   "Go through the list of MISSING articles and remove them from the mark lists."
5306   (when missing
5307     (let (var m)
5308       ;; Go through all types.
5309       (dolist (elem gnus-article-mark-lists)
5310         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5311           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5312           (when (symbol-value var)
5313             ;; This list has articles.  So we delete all missing
5314             ;; articles from it.
5315             (setq m missing)
5316             (while m
5317               (set var (delq (pop m) (symbol-value var))))))))))
5318
5319 (defun gnus-update-marks ()
5320   "Enter the various lists of marked articles into the newsgroup info list."
5321   (let ((types gnus-article-mark-lists)
5322         (info (gnus-get-info gnus-newsgroup-name))
5323         type list newmarked symbol delta-marks)
5324     (when info
5325       ;; Add all marks lists to the list of marks lists.
5326       (while (setq type (pop types))
5327         (setq list (symbol-value
5328                     (setq symbol
5329                           (intern (format "gnus-newsgroup-%s" (car type))))))
5330
5331         (when list
5332           ;; Get rid of the entries of the articles that have the
5333           ;; default score.
5334           (when (and (eq (cdr type) 'score)
5335                      gnus-save-score
5336                      list)
5337             (let* ((arts list)
5338                    (prev (cons nil list))
5339                    (all prev))
5340               (while arts
5341                 (if (or (not (consp (car arts)))
5342                         (= (cdar arts) gnus-summary-default-score))
5343                     (setcdr prev (cdr arts))
5344                   (setq prev arts))
5345                 (setq arts (cdr arts)))
5346               (setq list (cdr all)))))
5347
5348         (when (eq (cdr type) 'seen)
5349           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5350
5351         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5352           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5353
5354         (when (and (gnus-check-backend-function
5355                     'request-set-mark gnus-newsgroup-name)
5356                    (not (gnus-article-unpropagatable-p (cdr type))))
5357           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5358                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5359                  (add (gnus-remove-from-range
5360                        (gnus-copy-sequence list) old)))
5361             (when add
5362               (push (list add 'add (list (cdr type))) delta-marks))
5363             (when del
5364               (push (list del 'del (list (cdr type))) delta-marks))))
5365
5366         (when list
5367           (push (cons (cdr type) list) newmarked)))
5368
5369       (when delta-marks
5370         (unless (gnus-check-group gnus-newsgroup-name)
5371           (error "Can't open server for %s" gnus-newsgroup-name))
5372         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5373
5374       ;; Enter these new marks into the info of the group.
5375       (if (nthcdr 3 info)
5376           (setcar (nthcdr 3 info) newmarked)
5377         ;; Add the marks lists to the end of the info.
5378         (when newmarked
5379           (setcdr (nthcdr 2 info) (list newmarked))))
5380
5381       ;; Cut off the end of the info if there's nothing else there.
5382       (let ((i 5))
5383         (while (and (> i 2)
5384                     (not (nth i info)))
5385           (when (nthcdr (decf i) info)
5386             (setcdr (nthcdr i info) nil)))))))
5387
5388 (defun gnus-set-mode-line (where)
5389   "Set the mode line of the article or summary buffers.
5390 If WHERE is `summary', the summary mode line format will be used."
5391   ;; Is this mode line one we keep updated?
5392   (when (and (memq where gnus-updated-mode-lines)
5393              (symbol-value
5394               (intern (format "gnus-%s-mode-line-format-spec" where))))
5395     (let (mode-string)
5396       (save-excursion
5397         ;; We evaluate this in the summary buffer since these
5398         ;; variables are buffer-local to that buffer.
5399         (set-buffer gnus-summary-buffer)
5400        ;; We bind all these variables that are used in the `eval' form
5401         ;; below.
5402         (let* ((mformat (symbol-value
5403                          (intern
5404                           (format "gnus-%s-mode-line-format-spec" where))))
5405                (gnus-tmp-group-name (gnus-group-decoded-name
5406                                      gnus-newsgroup-name))
5407                (gnus-tmp-article-number (or gnus-current-article 0))
5408                (gnus-tmp-unread gnus-newsgroup-unreads)
5409                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5410                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5411                (gnus-tmp-unread-and-unselected
5412                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5413                             (zerop gnus-tmp-unselected))
5414                        "")
5415                       ((zerop gnus-tmp-unselected)
5416                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5417                       (t (format "{%d(+%d) more}"
5418                                  gnus-tmp-unread-and-unticked
5419                                  gnus-tmp-unselected))))
5420                (gnus-tmp-subject
5421                 (if (and gnus-current-headers
5422                          (vectorp gnus-current-headers))
5423                     (gnus-mode-string-quote
5424                      (mail-header-subject gnus-current-headers))
5425                   ""))
5426                bufname-length max-len
5427                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5428           (setq mode-string (eval mformat))
5429           (setq bufname-length (if (string-match "%b" mode-string)
5430                                    (- (length
5431                                        (buffer-name
5432                                         (if (eq where 'summary)
5433                                             nil
5434                                           (get-buffer gnus-article-buffer))))
5435                                       2)
5436                                  0))
5437           (setq max-len (max 4 (if gnus-mode-non-string-length
5438                                    (- (window-width)
5439                                       gnus-mode-non-string-length
5440                                       bufname-length)
5441                                  (length mode-string))))
5442           ;; We might have to chop a bit of the string off...
5443           (when (> (length mode-string) max-len)
5444             (setq mode-string
5445                   (concat (truncate-string-to-width mode-string (- max-len 3))
5446                           "...")))
5447           ;; Pad the mode string a bit.
5448           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5449       ;; Update the mode line.
5450       (setq mode-line-buffer-identification
5451             (gnus-mode-line-buffer-identification (list mode-string)))
5452       (set-buffer-modified-p t))))
5453
5454 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5455   "Go through the HEADERS list and add all Xrefs to a hash table.
5456 The resulting hash table is returned, or nil if no Xrefs were found."
5457   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5458          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5459          (xref-hashtb (gnus-make-hashtable))
5460          start group entry number xrefs header)
5461     (while headers
5462       (setq header (pop headers))
5463       (when (and (setq xrefs (mail-header-xref header))
5464                  (not (memq (setq number (mail-header-number header))
5465                             unreads)))
5466         (setq start 0)
5467         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5468           (setq start (match-end 0))
5469           (setq group (if prefix
5470                           (concat prefix (substring xrefs (match-beginning 1)
5471                                                     (match-end 1)))
5472                         (substring xrefs (match-beginning 1) (match-end 1))))
5473           (setq number
5474                 (string-to-int (substring xrefs (match-beginning 2)
5475                                           (match-end 2))))
5476           (if (setq entry (gnus-gethash group xref-hashtb))
5477               (setcdr entry (cons number (cdr entry)))
5478             (gnus-sethash group (cons number nil) xref-hashtb)))))
5479     (and start xref-hashtb)))
5480
5481 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5482   "Look through all the headers and mark the Xrefs as read."
5483   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5484         name entry info xref-hashtb idlist method nth4)
5485     (save-excursion
5486       (set-buffer gnus-group-buffer)
5487       (when (setq xref-hashtb
5488                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5489         (mapatoms
5490          (lambda (group)
5491            (unless (string= from-newsgroup (setq name (symbol-name group)))
5492              (setq idlist (symbol-value group))
5493              ;; Dead groups are not updated.
5494              (and (prog1
5495                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5496                             info (nth 2 entry))
5497                     (when (stringp (setq nth4 (gnus-info-method info)))
5498                       (setq nth4 (gnus-server-to-method nth4))))
5499                   ;; Only do the xrefs if the group has the same
5500                   ;; select method as the group we have just read.
5501                   (or (gnus-methods-equal-p
5502                        nth4 (gnus-find-method-for-group from-newsgroup))
5503                       virtual
5504                       (equal nth4 (setq method (gnus-find-method-for-group
5505                                                 from-newsgroup)))
5506                       (and (equal (car nth4) (car method))
5507                            (equal (nth 1 nth4) (nth 1 method))))
5508                   gnus-use-cross-reference
5509                   (or (not (eq gnus-use-cross-reference t))
5510                       virtual
5511                       ;; Only do cross-references on subscribed
5512                       ;; groups, if that is what is wanted.
5513                       (<= (gnus-info-level info) gnus-level-subscribed))
5514                   (gnus-group-make-articles-read name idlist))))
5515          xref-hashtb)))))
5516
5517 (defun gnus-compute-read-articles (group articles)
5518   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5519          (info (nth 2 entry))
5520          (active (gnus-active group))
5521          ninfo)
5522     (when entry
5523       ;; First peel off all invalid article numbers.
5524       (when active
5525         (let ((ids articles)
5526               id first)
5527           (while (setq id (pop ids))
5528             (when (and first (> id (cdr active)))
5529               ;; We'll end up in this situation in one particular
5530               ;; obscure situation.  If you re-scan a group and get
5531               ;; a new article that is cross-posted to a different
5532               ;; group that has not been re-scanned, you might get
5533               ;; crossposted article that has a higher number than
5534               ;; Gnus believes possible.  So we re-activate this
5535               ;; group as well.  This might mean doing the
5536               ;; crossposting thingy will *increase* the number
5537               ;; of articles in some groups.  Tsk, tsk.
5538               (setq active (or (gnus-activate-group group) active)))
5539             (when (or (> id (cdr active))
5540                       (< id (car active)))
5541               (setq articles (delq id articles))))))
5542       ;; If the read list is nil, we init it.
5543       (if (and active
5544                (null (gnus-info-read info))
5545                (> (car active) 1))
5546           (setq ninfo (cons 1 (1- (car active))))
5547         (setq ninfo (gnus-info-read info)))
5548       ;; Then we add the read articles to the range.
5549       (gnus-add-to-range
5550        ninfo (setq articles (sort articles '<))))))
5551
5552 (defun gnus-group-make-articles-read (group articles)
5553   "Update the info of GROUP to say that ARTICLES are read."
5554   (let* ((num 0)
5555          (entry (gnus-gethash group gnus-newsrc-hashtb))
5556          (info (nth 2 entry))
5557          (active (gnus-active group))
5558          range)
5559     (when entry
5560       (setq range (gnus-compute-read-articles group articles))
5561       (save-excursion
5562         (set-buffer gnus-group-buffer)
5563         (gnus-undo-register
5564           `(progn
5565              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5566              (gnus-info-set-read ',info ',(gnus-info-read info))
5567              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5568              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5569              (gnus-group-update-group ,group t))))
5570       ;; Add the read articles to the range.
5571       (gnus-info-set-read info range)
5572       (gnus-request-set-mark group (list (list range 'add '(read))))
5573       ;; Then we have to re-compute how many unread
5574       ;; articles there are in this group.
5575       (when active
5576         (cond
5577          ((not range)
5578           (setq num (- (1+ (cdr active)) (car active))))
5579          ((not (listp (cdr range)))
5580           (setq num (- (cdr active) (- (1+ (cdr range))
5581                                        (car range)))))
5582          (t
5583           (while range
5584             (if (numberp (car range))
5585                 (setq num (1+ num))
5586               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5587             (setq range (cdr range)))
5588           (setq num (- (cdr active) num))))
5589         ;; Update the number of unread articles.
5590         (setcar entry num)
5591         ;; Update the group buffer.
5592         (unless (gnus-ephemeral-group-p group)
5593           (gnus-group-update-group group t))))))
5594
5595 (defvar gnus-newsgroup-none-id 0)
5596
5597 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5598   (let ((cur nntp-server-buffer)
5599         (dependencies
5600          (or dependencies
5601              (save-excursion (set-buffer gnus-summary-buffer)
5602                              gnus-newsgroup-dependencies)))
5603         headers id end ref
5604         (mail-parse-charset gnus-newsgroup-charset)
5605         (mail-parse-ignored-charsets
5606          (save-excursion (condition-case nil
5607                              (set-buffer gnus-summary-buffer)
5608                            (error))
5609                          gnus-newsgroup-ignored-charsets)))
5610     (save-excursion
5611       (set-buffer nntp-server-buffer)
5612       ;; Translate all TAB characters into SPACE characters.
5613       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5614       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5615       (gnus-run-hooks 'gnus-parse-headers-hook)
5616       (let ((case-fold-search t)
5617             in-reply-to header p lines chars)
5618         (goto-char (point-min))
5619         ;; Search to the beginning of the next header.  Error messages
5620         ;; do not begin with 2 or 3.
5621         (while (re-search-forward "^[23][0-9]+ " nil t)
5622           (setq id nil
5623                 ref nil)
5624           ;; This implementation of this function, with nine
5625           ;; search-forwards instead of the one re-search-forward and
5626           ;; a case (which basically was the old function) is actually
5627           ;; about twice as fast, even though it looks messier.  You
5628           ;; can't have everything, I guess.  Speed and elegance
5629           ;; doesn't always go hand in hand.
5630           (setq
5631            header
5632            (vector
5633             ;; Number.
5634             (prog1
5635                 (read cur)
5636               (end-of-line)
5637               (setq p (point))
5638               (narrow-to-region (point)
5639                                 (or (and (search-forward "\n.\n" nil t)
5640                                          (- (point) 2))
5641                                     (point))))
5642             ;; Subject.
5643             (progn
5644               (goto-char p)
5645               (if (search-forward "\nsubject:" nil t)
5646                   (funcall gnus-decode-encoded-word-function
5647                            (nnheader-header-value))
5648                 "(none)"))
5649             ;; From.
5650             (progn
5651               (goto-char p)
5652               (if (search-forward "\nfrom:" nil t)
5653                   (funcall gnus-decode-encoded-word-function
5654                            (nnheader-header-value))
5655                 "(nobody)"))
5656             ;; Date.
5657             (progn
5658               (goto-char p)
5659               (if (search-forward "\ndate:" nil t)
5660                   (nnheader-header-value) ""))
5661             ;; Message-ID.
5662             (progn
5663               (goto-char p)
5664               (setq id (if (re-search-forward
5665                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5666                            ;; We do it this way to make sure the Message-ID
5667                            ;; is (somewhat) syntactically valid.
5668                            (buffer-substring (match-beginning 1)
5669                                              (match-end 1))
5670                          ;; If there was no message-id, we just fake one
5671                          ;; to make subsequent routines simpler.
5672                          (nnheader-generate-fake-message-id))))
5673             ;; References.
5674             (progn
5675               (goto-char p)
5676               (if (search-forward "\nreferences:" nil t)
5677                   (progn
5678                     (setq end (point))
5679                     (prog1
5680                         (nnheader-header-value)
5681                       (setq ref
5682                             (buffer-substring
5683                              (progn
5684                                (end-of-line)
5685                                (search-backward ">" end t)
5686                                (1+ (point)))
5687                              (progn
5688                                (search-backward "<" end t)
5689                                (point))))))
5690                 ;; Get the references from the in-reply-to header if there
5691                 ;; were no references and the in-reply-to header looks
5692                 ;; promising.
5693                 (if (and (search-forward "\nin-reply-to:" nil t)
5694                          (setq in-reply-to (nnheader-header-value))
5695                          (string-match "<[^>]+>" in-reply-to))
5696                     (let (ref2)
5697                       (setq ref (substring in-reply-to (match-beginning 0)
5698                                            (match-end 0)))
5699                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5700                         (setq ref2 (substring in-reply-to (match-beginning 0)
5701                                               (match-end 0)))
5702                         (when (> (length ref2) (length ref))
5703                           (setq ref ref2)))
5704                       ref)
5705                   (setq ref nil))))
5706             ;; Chars.
5707             (progn
5708               (goto-char p)
5709               (if (search-forward "\nchars: " nil t)
5710                   (if (numberp (setq chars (ignore-errors (read cur))))
5711                       chars -1)
5712                 -1))
5713             ;; Lines.
5714             (progn
5715               (goto-char p)
5716               (if (search-forward "\nlines: " nil t)
5717                   (if (numberp (setq lines (ignore-errors (read cur))))
5718                       lines -1)
5719                 -1))
5720             ;; Xref.
5721             (progn
5722               (goto-char p)
5723               (and (search-forward "\nxref:" nil t)
5724                    (nnheader-header-value)))
5725             ;; Extra.
5726             (when gnus-extra-headers
5727               (let ((extra gnus-extra-headers)
5728                     out)
5729                 (while extra
5730                   (goto-char p)
5731                   (when (search-forward
5732                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5733                     (push (cons (car extra) (nnheader-header-value))
5734                           out))
5735                   (pop extra))
5736                 out))))
5737           (when (equal id ref)
5738             (setq ref nil))
5739
5740           (when gnus-alter-header-function
5741             (funcall gnus-alter-header-function header)
5742             (setq id (mail-header-id header)
5743                   ref (gnus-parent-id (mail-header-references header))))
5744
5745           (when (setq header
5746                       (gnus-dependencies-add-header
5747                        header dependencies force-new))
5748             (push header headers))
5749           (goto-char (point-max))
5750           (widen))
5751         (nreverse headers)))))
5752
5753 ;; Goes through the xover lines and returns a list of vectors
5754 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5755                                                   force-new dependencies
5756                                                   group also-fetch-heads)
5757   "Parse the news overview data in the server buffer.
5758 Return a list of headers that match SEQUENCE (see
5759 `nntp-retrieve-headers')."
5760   ;; Get the Xref when the users reads the articles since most/some
5761   ;; NNTP servers do not include Xrefs when using XOVER.
5762   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5763   (let ((mail-parse-charset gnus-newsgroup-charset)
5764         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5765         (cur nntp-server-buffer)
5766         (dependencies (or dependencies gnus-newsgroup-dependencies))
5767         (allp (cond
5768                ((eq gnus-read-all-available-headers t)
5769                 t)
5770                ((stringp gnus-read-all-available-headers)
5771                 (string-match gnus-read-all-available-headers group))
5772                (t
5773                 nil)))
5774         number headers header)
5775     (save-excursion
5776       (set-buffer nntp-server-buffer)
5777       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5778       ;; Allow the user to mangle the headers before parsing them.
5779       (gnus-run-hooks 'gnus-parse-headers-hook)
5780       (goto-char (point-min))
5781       (gnus-parse-without-error
5782         (while (and (or sequence allp)
5783                     (not (eobp)))
5784           (setq number (read cur))
5785           (when (not allp)
5786             (while (and sequence
5787                         (< (car sequence) number))
5788               (setq sequence (cdr sequence))))
5789           (when (and (or allp
5790                          (and sequence
5791                               (eq number (car sequence))))
5792                      (progn
5793                        (setq sequence (cdr sequence))
5794                        (setq header (inline
5795                                       (gnus-nov-parse-line
5796                                        number dependencies force-new)))))
5797             (push header headers))
5798           (forward-line 1)))
5799       ;; A common bug in inn is that if you have posted an article and
5800       ;; then retrieves the active file, it will answer correctly --
5801       ;; the new article is included.  However, a NOV entry for the
5802       ;; article may not have been generated yet, so this may fail.
5803       ;; We work around this problem by retrieving the last few
5804       ;; headers using HEAD.
5805       (if (or (not also-fetch-heads)
5806               (not sequence))
5807           ;; We (probably) got all the headers.
5808           (nreverse headers)
5809         (let ((gnus-nov-is-evil t))
5810           (nconc
5811            (nreverse headers)
5812            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5813              (gnus-get-newsgroup-headers))))))))
5814
5815 (defun gnus-article-get-xrefs ()
5816   "Fill in the Xref value in `gnus-current-headers', if necessary.
5817 This is meant to be called in `gnus-article-internal-prepare-hook'."
5818   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5819                                  gnus-current-headers)))
5820     (or (not gnus-use-cross-reference)
5821         (not headers)
5822         (and (mail-header-xref headers)
5823              (not (string= (mail-header-xref headers) "")))
5824         (let ((case-fold-search t)
5825               xref)
5826           (save-restriction
5827             (nnheader-narrow-to-headers)
5828             (goto-char (point-min))
5829             (when (or (and (not (eobp))
5830                            (eq (downcase (char-after)) ?x)
5831                            (looking-at "Xref:"))
5832                       (search-forward "\nXref:" nil t))
5833               (goto-char (1+ (match-end 0)))
5834               (setq xref (buffer-substring (point)
5835                                            (progn (end-of-line) (point))))
5836               (mail-header-set-xref headers xref)))))))
5837
5838 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5839   "Find article ID and insert the summary line for that article.
5840 OLD-HEADER can either be a header or a line number to insert
5841 the subject line on."
5842   (let* ((line (and (numberp old-header) old-header))
5843          (old-header (and (vectorp old-header) old-header))
5844          (header (cond ((and old-header use-old-header)
5845                         old-header)
5846                        ((and (numberp id)
5847                              (gnus-number-to-header id))
5848                         (gnus-number-to-header id))
5849                        (t
5850                         (gnus-read-header id))))
5851          (number (and (numberp id) id))
5852          d)
5853     (when header
5854       ;; Rebuild the thread that this article is part of and go to the
5855       ;; article we have fetched.
5856       (when (and (not gnus-show-threads)
5857                  old-header)
5858         (when (and number
5859                    (setq d (gnus-data-find (mail-header-number old-header))))
5860           (goto-char (gnus-data-pos d))
5861           (gnus-data-remove
5862            number
5863            (- (gnus-point-at-bol)
5864               (prog1
5865                   (1+ (gnus-point-at-eol))
5866                 (gnus-delete-line))))))
5867       (when old-header
5868         (mail-header-set-number header (mail-header-number old-header)))
5869       (setq gnus-newsgroup-sparse
5870             (delq (setq number (mail-header-number header))
5871                   gnus-newsgroup-sparse))
5872       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5873       (push number gnus-newsgroup-limit)
5874       (gnus-rebuild-thread (mail-header-id header) line)
5875       (gnus-summary-goto-subject number nil t))
5876     (when (and (numberp number)
5877                (> number 0))
5878       ;; We have to update the boundaries even if we can't fetch the
5879       ;; article if ID is a number -- so that the next `P' or `N'
5880       ;; command will fetch the previous (or next) article even
5881       ;; if the one we tried to fetch this time has been canceled.
5882       (when (> number gnus-newsgroup-end)
5883         (setq gnus-newsgroup-end number))
5884       (when (< number gnus-newsgroup-begin)
5885         (setq gnus-newsgroup-begin number))
5886       (setq gnus-newsgroup-unselected
5887             (delq number gnus-newsgroup-unselected)))
5888     ;; Report back a success?
5889     (and header (mail-header-number header))))
5890
5891 ;;; Process/prefix in the summary buffer
5892
5893 (defun gnus-summary-work-articles (n)
5894   "Return a list of articles to be worked upon.
5895 The prefix argument, the list of process marked articles, and the
5896 current article will be taken into consideration."
5897   (save-excursion
5898     (set-buffer gnus-summary-buffer)
5899     (cond
5900      (n
5901       ;; A numerical prefix has been given.
5902       (setq n (prefix-numeric-value n))
5903       (let ((backward (< n 0))
5904             (n (abs (prefix-numeric-value n)))
5905             articles article)
5906         (save-excursion
5907           (while
5908               (and (> n 0)
5909                    (push (setq article (gnus-summary-article-number))
5910                          articles)
5911                    (if backward
5912                        (gnus-summary-find-prev nil article)
5913                      (gnus-summary-find-next nil article)))
5914             (decf n)))
5915         (nreverse articles)))
5916      ((and (gnus-region-active-p) (mark))
5917       (message "region active")
5918       ;; Work on the region between point and mark.
5919       (let ((max (max (point) (mark)))
5920             articles article)
5921         (save-excursion
5922           (goto-char (min (min (point) (mark))))
5923           (while
5924               (and
5925                (push (setq article (gnus-summary-article-number)) articles)
5926                (gnus-summary-find-next nil article)
5927                (< (point) max)))
5928           (nreverse articles))))
5929      (gnus-newsgroup-processable
5930       ;; There are process-marked articles present.
5931       ;; Save current state.
5932       (gnus-summary-save-process-mark)
5933       ;; Return the list.
5934       (reverse gnus-newsgroup-processable))
5935      (t
5936       ;; Just return the current article.
5937       (list (gnus-summary-article-number))))))
5938
5939 (defmacro gnus-summary-iterate (arg &rest forms)
5940   "Iterate over the process/prefixed articles and do FORMS.
5941 ARG is the interactive prefix given to the command.  FORMS will be
5942 executed with point over the summary line of the articles."
5943   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5944     `(let ((,articles (gnus-summary-work-articles ,arg)))
5945        (while ,articles
5946          (gnus-summary-goto-subject (car ,articles))
5947          ,@forms
5948          (pop ,articles)))))
5949
5950 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5951 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5952
5953 (defun gnus-summary-save-process-mark ()
5954   "Push the current set of process marked articles on the stack."
5955   (interactive)
5956   (push (copy-sequence gnus-newsgroup-processable)
5957         gnus-newsgroup-process-stack))
5958
5959 (defun gnus-summary-kill-process-mark ()
5960   "Push the current set of process marked articles on the stack and unmark."
5961   (interactive)
5962   (gnus-summary-save-process-mark)
5963   (gnus-summary-unmark-all-processable))
5964
5965 (defun gnus-summary-yank-process-mark ()
5966   "Pop the last process mark state off the stack and restore it."
5967   (interactive)
5968   (unless gnus-newsgroup-process-stack
5969     (error "Empty mark stack"))
5970   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5971
5972 (defun gnus-summary-process-mark-set (set)
5973   "Make SET into the current process marked articles."
5974   (gnus-summary-unmark-all-processable)
5975   (while set
5976     (gnus-summary-set-process-mark (pop set))))
5977
5978 ;;; Searching and stuff
5979
5980 (defun gnus-summary-search-group (&optional backward use-level)
5981   "Search for next unread newsgroup.
5982 If optional argument BACKWARD is non-nil, search backward instead."
5983   (save-excursion
5984     (set-buffer gnus-group-buffer)
5985     (when (gnus-group-search-forward
5986            backward nil (if use-level (gnus-group-group-level) nil))
5987       (gnus-group-group-name))))
5988
5989 (defun gnus-summary-best-group (&optional exclude-group)
5990   "Find the name of the best unread group.
5991 If EXCLUDE-GROUP, do not go to this group."
5992   (save-excursion
5993     (set-buffer gnus-group-buffer)
5994     (save-excursion
5995       (gnus-group-best-unread-group exclude-group))))
5996
5997 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5998   (if backward (gnus-summary-find-prev)
5999     (let* ((dummy (gnus-summary-article-intangible-p))
6000            (article (or article (gnus-summary-article-number)))
6001            (arts (gnus-data-find-list article))
6002            result)
6003       (when (and (not dummy)
6004                  (or (not gnus-summary-check-current)
6005                      (not unread)
6006                      (not (gnus-data-unread-p (car arts)))))
6007         (setq arts (cdr arts)))
6008       (when (setq result
6009                   (if unread
6010                       (progn
6011                         (while arts
6012                           (when (or (and undownloaded
6013                                          (memq (car arts)
6014                                                gnus-newsgroup-undownloaded))
6015                                     (gnus-data-unread-p (car arts)))
6016                             (setq result (car arts)
6017                                   arts nil))
6018                           (setq arts (cdr arts)))
6019                         result)
6020                     (car arts)))
6021         (goto-char (gnus-data-pos result))
6022         (gnus-data-number result)))))
6023
6024 (defun gnus-summary-find-prev (&optional unread article)
6025   (let* ((eobp (eobp))
6026          (article (or article (gnus-summary-article-number)))
6027          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
6028          result)
6029     (when (and (not eobp)
6030                (or (not gnus-summary-check-current)
6031                    (not unread)
6032                    (not (gnus-data-unread-p (car arts)))))
6033       (setq arts (cdr arts)))
6034     (when (setq result
6035                 (if unread
6036                     (progn
6037                       (while arts
6038                         (when (gnus-data-unread-p (car arts))
6039                           (setq result (car arts)
6040                                 arts nil))
6041                         (setq arts (cdr arts)))
6042                       result)
6043                   (car arts)))
6044       (goto-char (gnus-data-pos result))
6045       (gnus-data-number result))))
6046
6047 (defun gnus-summary-find-subject (subject &optional unread backward article)
6048   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6049          (article (or article (gnus-summary-article-number)))
6050          (articles (gnus-data-list backward))
6051          (arts (gnus-data-find-list article articles))
6052          result)
6053     (when (or (not gnus-summary-check-current)
6054               (not unread)
6055               (not (gnus-data-unread-p (car arts))))
6056       (setq arts (cdr arts)))
6057     (while arts
6058       (and (or (not unread)
6059                (gnus-data-unread-p (car arts)))
6060            (vectorp (gnus-data-header (car arts)))
6061            (gnus-subject-equal
6062             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6063            (setq result (car arts)
6064                  arts nil))
6065       (setq arts (cdr arts)))
6066     (and result
6067          (goto-char (gnus-data-pos result))
6068          (gnus-data-number result))))
6069
6070 (defun gnus-summary-search-forward (&optional unread subject backward)
6071   "Search forward for an article.
6072 If UNREAD, look for unread articles.  If SUBJECT, look for
6073 articles with that subject.  If BACKWARD, search backward instead."
6074   (cond (subject (gnus-summary-find-subject subject unread backward))
6075         (backward (gnus-summary-find-prev unread))
6076         (t (gnus-summary-find-next unread))))
6077
6078 (defun gnus-recenter (&optional n)
6079   "Center point in window and redisplay frame.
6080 Also do horizontal recentering."
6081   (interactive "P")
6082   (when (and gnus-auto-center-summary
6083              (not (eq gnus-auto-center-summary 'vertical)))
6084     (gnus-horizontal-recenter))
6085   (recenter n))
6086
6087 (defun gnus-summary-recenter ()
6088   "Center point in the summary window.
6089 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6090 displayed, no centering will be performed."
6091   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6092 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6093   (interactive)
6094   (let* ((top (cond ((< (window-height) 4) 0)
6095                     ((< (window-height) 7) 1)
6096                     (t (if (numberp gnus-auto-center-summary)
6097                            gnus-auto-center-summary
6098                          2))))
6099          (height (1- (window-height)))
6100          (bottom (save-excursion (goto-char (point-max))
6101                                  (forward-line (- height))
6102                                  (point)))
6103          (window (get-buffer-window (current-buffer))))
6104     ;; The user has to want it.
6105     (when gnus-auto-center-summary
6106       (when (get-buffer-window gnus-article-buffer)
6107         ;; Only do recentering when the article buffer is displayed,
6108       ;; Set the window start to either `bottom', which is the biggest
6109         ;; possible valid number, or the second line from the top,
6110         ;; whichever is the least.
6111         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6112           (if (> bottom top-pos)
6113               ;; Keep the second line from the top visible
6114               (set-window-start window top-pos t)
6115             ;; Try to keep the bottom line visible; if it's partially
6116             ;; obscured, either scroll one more line to make it fully
6117             ;; visible, or revert to using TOP-POS.
6118             (save-excursion
6119               (goto-char (point-max))
6120               (forward-line -1)
6121               (let ((last-line-start (point)))
6122                 (goto-char bottom)
6123                 (set-window-start window (point) t)
6124                 (when (not (pos-visible-in-window-p last-line-start window))
6125                   (forward-line 1)
6126                   (set-window-start window (min (point) top-pos) t)))))))
6127       ;; Do horizontal recentering while we're at it.
6128       (when (and (get-buffer-window (current-buffer) t)
6129                  (not (eq gnus-auto-center-summary 'vertical)))
6130         (let ((selected (selected-window)))
6131           (select-window (get-buffer-window (current-buffer) t))
6132           (gnus-summary-position-point)
6133           (gnus-horizontal-recenter)
6134           (select-window selected))))))
6135
6136 (defun gnus-summary-jump-to-group (newsgroup)
6137   "Move point to NEWSGROUP in group mode buffer."
6138   ;; Keep update point of group mode buffer if visible.
6139   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6140       (save-window-excursion
6141         ;; Take care of tree window mode.
6142         (when (get-buffer-window gnus-group-buffer)
6143           (pop-to-buffer gnus-group-buffer))
6144         (gnus-group-jump-to-group newsgroup))
6145     (save-excursion
6146       ;; Take care of tree window mode.
6147       (if (get-buffer-window gnus-group-buffer)
6148           (pop-to-buffer gnus-group-buffer)
6149         (set-buffer gnus-group-buffer))
6150       (gnus-group-jump-to-group newsgroup))))
6151
6152 ;; This function returns a list of article numbers based on the
6153 ;; difference between the ranges of read articles in this group and
6154 ;; the range of active articles.
6155 (defun gnus-list-of-unread-articles (group)
6156   (let* ((read (gnus-info-read (gnus-get-info group)))
6157          (active (or (gnus-active group) (gnus-activate-group group)))
6158          (last (cdr active))
6159          first nlast unread)
6160     ;; If none are read, then all are unread.
6161     (if (not read)
6162         (setq first (car active))
6163       ;; If the range of read articles is a single range, then the
6164       ;; first unread article is the article after the last read
6165       ;; article.  Sounds logical, doesn't it?
6166       (if (and (not (listp (cdr read)))
6167                (or (< (car read) (car active))
6168                    (progn (setq read (list read))
6169                           nil)))
6170           (setq first (max (car active) (1+ (cdr read))))
6171         ;; `read' is a list of ranges.
6172         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6173                                   (caar read)))
6174                   1)
6175           (setq first (car active)))
6176         (while read
6177           (when first
6178             (while (< first nlast)
6179               (push first unread)
6180               (setq first (1+ first))))
6181           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6182           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6183           (setq read (cdr read)))))
6184     ;; And add the last unread articles.
6185     (while (<= first last)
6186       (push first unread)
6187       (setq first (1+ first)))
6188     ;; Return the list of unread articles.
6189     (delq 0 (nreverse unread))))
6190
6191 (defun gnus-list-of-read-articles (group)
6192   "Return a list of unread, unticked and non-dormant articles."
6193   (let* ((info (gnus-get-info group))
6194          (marked (gnus-info-marks info))
6195          (active (gnus-active group)))
6196     (and info active
6197          (gnus-list-range-difference
6198           (gnus-list-range-difference
6199            (gnus-sorted-complement
6200             (gnus-uncompress-range active)
6201             (gnus-list-of-unread-articles group))
6202            (cdr (assq 'dormant marked)))
6203           (cdr (assq 'tick marked))))))
6204
6205 ;; Various summary commands
6206
6207 (defun gnus-summary-select-article-buffer ()
6208   "Reconfigure windows to show article buffer."
6209   (interactive)
6210   (if (not (gnus-buffer-live-p gnus-article-buffer))
6211       (error "There is no article buffer for this summary buffer")
6212     (gnus-configure-windows 'article)
6213     (select-window (get-buffer-window gnus-article-buffer))))
6214
6215 (defun gnus-summary-universal-argument (arg)
6216   "Perform any operation on all articles that are process/prefixed."
6217   (interactive "P")
6218   (let ((articles (gnus-summary-work-articles arg))
6219         func article)
6220     (if (eq
6221          (setq
6222           func
6223           (key-binding
6224            (read-key-sequence
6225             (substitute-command-keys
6226              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6227          'undefined)
6228         (gnus-error 1 "Undefined key")
6229       (save-excursion
6230         (while articles
6231           (gnus-summary-goto-subject (setq article (pop articles)))
6232           (let (gnus-newsgroup-processable)
6233             (command-execute func))
6234           (gnus-summary-remove-process-mark article)))))
6235   (gnus-summary-position-point))
6236
6237 (defun gnus-summary-toggle-truncation (&optional arg)
6238   "Toggle truncation of summary lines.
6239 With arg, turn line truncation on if arg is positive."
6240   (interactive "P")
6241   (setq truncate-lines
6242         (if (null arg) (not truncate-lines)
6243           (> (prefix-numeric-value arg) 0)))
6244   (redraw-display))
6245
6246 (defun gnus-summary-find-uncancelled ()
6247   "Return the number of an uncancelled article.
6248 The current article is considered, then following articles, then previous
6249 articles.  If all articles are cancelled then return a dummy 0."
6250   (let (found)
6251     (dolist (rev '(nil t))
6252       (unless found      ; don't demand the reverse list if we don't need it
6253         (let ((data (gnus-data-find-list
6254                      (gnus-summary-article-number) (gnus-data-list rev))))
6255           (while (and data (not found))
6256             (if (not (eq gnus-canceled-mark (gnus-data-mark (car data))))
6257                 (setq found (gnus-data-number (car data))))
6258             (setq data (cdr data))))))
6259     (or found 0)))
6260
6261 (defun gnus-summary-reselect-current-group (&optional all rescan)
6262   "Exit and then reselect the current newsgroup.
6263 The prefix argument ALL means to select all articles."
6264   (interactive "P")
6265   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6266     (error "Ephemeral groups can't be reselected"))
6267   (let ((current-subject (gnus-summary-find-uncancelled))
6268         (group gnus-newsgroup-name))
6269     (setq gnus-newsgroup-begin nil)
6270     (gnus-summary-exit)
6271     ;; We have to adjust the point of group mode buffer because
6272     ;; point was moved to the next unread newsgroup by exiting.
6273     (gnus-summary-jump-to-group group)
6274     (when rescan
6275       (save-excursion
6276         (gnus-group-get-new-news-this-group 1)))
6277     (gnus-group-read-group all t)
6278     (gnus-summary-goto-subject current-subject nil t)))
6279
6280 (defun gnus-summary-rescan-group (&optional all)
6281   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6282   (interactive "P")
6283   (gnus-summary-reselect-current-group all t))
6284
6285 (defun gnus-summary-update-info (&optional non-destructive)
6286   (save-excursion
6287     (let ((group gnus-newsgroup-name))
6288       (when group
6289         (when gnus-newsgroup-kill-headers
6290           (setq gnus-newsgroup-killed
6291                 (gnus-compress-sequence
6292                  (gnus-sorted-union
6293                   (gnus-list-range-intersection
6294                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6295                   gnus-newsgroup-unreads)
6296                  t)))
6297         (unless (listp (cdr gnus-newsgroup-killed))
6298           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6299         (let ((headers gnus-newsgroup-headers))
6300           ;; Set the new ranges of read articles.
6301           (save-excursion
6302             (set-buffer gnus-group-buffer)
6303             (gnus-undo-force-boundary))
6304           (gnus-update-read-articles
6305            group (gnus-sorted-union
6306                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6307           ;; Set the current article marks.
6308           (let ((gnus-newsgroup-scored
6309                  (if (and (not gnus-save-score)
6310                           (not non-destructive))
6311                      nil
6312                    gnus-newsgroup-scored)))
6313             (save-excursion
6314               (gnus-update-marks)))
6315           ;; Do the cross-ref thing.
6316           (when gnus-use-cross-reference
6317             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6318           ;; Do not switch windows but change the buffer to work.
6319           (set-buffer gnus-group-buffer)
6320           (unless (gnus-ephemeral-group-p group)
6321             (gnus-group-update-group group)))))))
6322
6323 (defun gnus-summary-save-newsrc (&optional force)
6324   "Save the current number of read/marked articles in the dribble buffer.
6325 The dribble buffer will then be saved.
6326 If FORCE (the prefix), also save the .newsrc file(s)."
6327   (interactive "P")
6328   (gnus-summary-update-info t)
6329   (if force
6330       (gnus-save-newsrc-file)
6331     (gnus-dribble-save)))
6332
6333 (defun gnus-summary-exit (&optional temporary)
6334   "Exit reading current newsgroup, and then return to group selection mode.
6335 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6336   (interactive)
6337   (gnus-set-global-variables)
6338   (when (gnus-buffer-live-p gnus-article-buffer)
6339     (save-excursion
6340       (set-buffer gnus-article-buffer)
6341       (mm-destroy-parts gnus-article-mime-handles)
6342       ;; Set it to nil for safety reason.
6343       (setq gnus-article-mime-handle-alist nil)
6344       (setq gnus-article-mime-handles nil)))
6345   (gnus-kill-save-kill-buffer)
6346   (gnus-async-halt-prefetch)
6347   (let* ((group gnus-newsgroup-name)
6348          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6349          (gnus-group-is-exiting-p t)
6350          (mode major-mode)
6351          (group-point nil)
6352          (buf (current-buffer)))
6353     (unless quit-config
6354       ;; Do adaptive scoring, and possibly save score files.
6355       (when gnus-newsgroup-adaptive
6356         (gnus-score-adaptive))
6357       (when gnus-use-scoring
6358         (gnus-score-save)))
6359     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6360     ;; If we have several article buffers, we kill them at exit.
6361     (unless gnus-single-article-buffer
6362       (gnus-kill-buffer gnus-original-article-buffer)
6363       (setq gnus-article-current nil))
6364     (when gnus-use-cache
6365       (gnus-cache-possibly-remove-articles)
6366       (gnus-cache-save-buffers))
6367     (gnus-async-prefetch-remove-group group)
6368     (when gnus-suppress-duplicates
6369       (gnus-dup-enter-articles))
6370     (when gnus-use-trees
6371       (gnus-tree-close group))
6372     (when gnus-use-cache
6373       (gnus-cache-write-active))
6374     ;; Remove entries for this group.
6375     (nnmail-purge-split-history (gnus-group-real-name group))
6376     ;; Make all changes in this group permanent.
6377     (unless quit-config
6378       (gnus-run-hooks 'gnus-exit-group-hook)
6379       (gnus-summary-update-info))
6380     (gnus-close-group group)
6381     ;; Make sure where we were, and go to next newsgroup.
6382     (set-buffer gnus-group-buffer)
6383     (unless quit-config
6384       (gnus-group-jump-to-group group))
6385     (gnus-run-hooks 'gnus-summary-exit-hook)
6386     (unless (or quit-config
6387                 ;; If this group has disappeared from the summary
6388                 ;; buffer, don't skip forwards.
6389                 (not (string= group (gnus-group-group-name))))
6390       (gnus-group-next-unread-group 1))
6391     (setq group-point (point))
6392     (if temporary
6393         nil                             ;Nothing to do.
6394       ;; If we have several article buffers, we kill them at exit.
6395       (unless gnus-single-article-buffer
6396         (gnus-kill-buffer gnus-article-buffer)
6397         (gnus-kill-buffer gnus-original-article-buffer)
6398         (setq gnus-article-current nil))
6399       (set-buffer buf)
6400       (if (not gnus-kill-summary-on-exit)
6401           (progn
6402             (gnus-deaden-summary)
6403             (setq mode nil))
6404         ;; We set all buffer-local variables to nil.  It is unclear why
6405         ;; this is needed, but if we don't, buffer-local variables are
6406         ;; not garbage-collected, it seems.  This would the lead to en
6407         ;; ever-growing Emacs.
6408         (gnus-summary-clear-local-variables)
6409         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6410           (gnus-summary-clear-local-variables))
6411         (when (get-buffer gnus-article-buffer)
6412           (bury-buffer gnus-article-buffer))
6413         ;; We clear the global counterparts of the buffer-local
6414         ;; variables as well, just to be on the safe side.
6415         (set-buffer gnus-group-buffer)
6416         (gnus-summary-clear-local-variables)
6417         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6418           (gnus-summary-clear-local-variables))
6419         ;; Return to group mode buffer.
6420         (when (eq mode 'gnus-summary-mode)
6421           (gnus-kill-buffer buf)))
6422       (setq gnus-current-select-method gnus-select-method)
6423       (pop-to-buffer gnus-group-buffer)
6424       (if (not quit-config)
6425           (progn
6426             (goto-char group-point)
6427             (gnus-configure-windows 'group 'force))
6428         (gnus-handle-ephemeral-exit quit-config))
6429       ;; Clear the current group name.
6430       (unless quit-config
6431         (setq gnus-newsgroup-name nil)))))
6432
6433 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6434 (defun gnus-summary-exit-no-update (&optional no-questions)
6435   "Quit reading current newsgroup without updating read article info."
6436   (interactive)
6437   (let* ((group gnus-newsgroup-name)
6438          (gnus-group-is-exiting-p t)
6439          (quit-config (gnus-group-quit-config group)))
6440     (when (or no-questions
6441               gnus-expert-user
6442               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6443       (gnus-async-halt-prefetch)
6444       (mapcar 'funcall
6445               (delq 'gnus-summary-expire-articles
6446                     (copy-sequence gnus-summary-prepare-exit-hook)))
6447       (when (gnus-buffer-live-p gnus-article-buffer)
6448         (save-excursion
6449           (set-buffer gnus-article-buffer)
6450           (mm-destroy-parts gnus-article-mime-handles)
6451           ;; Set it to nil for safety reason.
6452           (setq gnus-article-mime-handle-alist nil)
6453           (setq gnus-article-mime-handles nil)))
6454       ;; If we have several article buffers, we kill them at exit.
6455       (unless gnus-single-article-buffer
6456         (gnus-kill-buffer gnus-article-buffer)
6457         (gnus-kill-buffer gnus-original-article-buffer)
6458         (setq gnus-article-current nil))
6459       (if (not gnus-kill-summary-on-exit)
6460           (gnus-deaden-summary)
6461         (gnus-close-group group)
6462         (gnus-summary-clear-local-variables)
6463         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6464           (gnus-summary-clear-local-variables))
6465         (set-buffer gnus-group-buffer)
6466         (gnus-summary-clear-local-variables)
6467         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6468           (gnus-summary-clear-local-variables))
6469         (when (get-buffer gnus-summary-buffer)
6470           (kill-buffer gnus-summary-buffer)))
6471       (unless gnus-single-article-buffer
6472         (setq gnus-article-current nil))
6473       (when gnus-use-trees
6474         (gnus-tree-close group))
6475       (gnus-async-prefetch-remove-group group)
6476       (when (get-buffer gnus-article-buffer)
6477         (bury-buffer gnus-article-buffer))
6478       ;; Return to the group buffer.
6479       (gnus-configure-windows 'group 'force)
6480       ;; Clear the current group name.
6481       (setq gnus-newsgroup-name nil)
6482       (unless (gnus-ephemeral-group-p group)
6483         (gnus-group-update-group group))
6484       (when (equal (gnus-group-group-name) group)
6485         (gnus-group-next-unread-group 1))
6486       (when quit-config
6487         (gnus-handle-ephemeral-exit quit-config)))))
6488
6489 (defun gnus-handle-ephemeral-exit (quit-config)
6490   "Handle movement when leaving an ephemeral group.
6491 The state which existed when entering the ephemeral is reset."
6492   (if (not (buffer-name (car quit-config)))
6493       (gnus-configure-windows 'group 'force)
6494     (set-buffer (car quit-config))
6495     (cond ((eq major-mode 'gnus-summary-mode)
6496            (gnus-set-global-variables))
6497           ((eq major-mode 'gnus-article-mode)
6498            (save-excursion
6499              ;; The `gnus-summary-buffer' variable may point
6500              ;; to the old summary buffer when using a single
6501              ;; article buffer.
6502              (unless (gnus-buffer-live-p gnus-summary-buffer)
6503                (set-buffer gnus-group-buffer))
6504              (set-buffer gnus-summary-buffer)
6505              (gnus-set-global-variables))))
6506     (if (or (eq (cdr quit-config) 'article)
6507             (eq (cdr quit-config) 'pick))
6508         (progn
6509           ;; The current article may be from the ephemeral group
6510           ;; thus it is best that we reload this article
6511           (gnus-summary-show-article)
6512           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6513               (gnus-configure-windows 'pick 'force)
6514             (gnus-configure-windows (cdr quit-config) 'force)))
6515       (gnus-configure-windows (cdr quit-config) 'force))
6516     (when (eq major-mode 'gnus-summary-mode)
6517       (gnus-summary-next-subject 1 nil t)
6518       (gnus-summary-recenter)
6519       (gnus-summary-position-point))))
6520
6521 ;;; Dead summaries.
6522
6523 (defvar gnus-dead-summary-mode-map nil)
6524
6525 (unless gnus-dead-summary-mode-map
6526   (setq gnus-dead-summary-mode-map (make-keymap))
6527   (suppress-keymap gnus-dead-summary-mode-map)
6528   (substitute-key-definition
6529    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6530   (dolist (key '("\C-d" "\r" "\177" [delete]))
6531     (define-key gnus-dead-summary-mode-map
6532       key 'gnus-summary-wake-up-the-dead))
6533   (dolist (key '("q" "Q"))
6534     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6535
6536 (defvar gnus-dead-summary-mode nil
6537   "Minor mode for Gnus summary buffers.")
6538
6539 (defun gnus-dead-summary-mode (&optional arg)
6540   "Minor mode for Gnus summary buffers."
6541   (interactive "P")
6542   (when (eq major-mode 'gnus-summary-mode)
6543     (make-local-variable 'gnus-dead-summary-mode)
6544     (setq gnus-dead-summary-mode
6545           (if (null arg) (not gnus-dead-summary-mode)
6546             (> (prefix-numeric-value arg) 0)))
6547     (when gnus-dead-summary-mode
6548       (gnus-add-minor-mode
6549        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6550
6551 (defun gnus-deaden-summary ()
6552   "Make the current summary buffer into a dead summary buffer."
6553   ;; Kill any previous dead summary buffer.
6554   (when (and gnus-dead-summary
6555              (buffer-name gnus-dead-summary))
6556     (save-excursion
6557       (set-buffer gnus-dead-summary)
6558       (when gnus-dead-summary-mode
6559         (kill-buffer (current-buffer)))))
6560   ;; Make this the current dead summary.
6561   (setq gnus-dead-summary (current-buffer))
6562   (gnus-dead-summary-mode 1)
6563   (let ((name (buffer-name)))
6564     (when (string-match "Summary" name)
6565       (rename-buffer
6566        (concat (substring name 0 (match-beginning 0)) "Dead "
6567                (substring name (match-beginning 0)))
6568        t)
6569       (bury-buffer))))
6570
6571 (defun gnus-kill-or-deaden-summary (buffer)
6572   "Kill or deaden the summary BUFFER."
6573   (save-excursion
6574     (when (and (buffer-name buffer)
6575                (not gnus-single-article-buffer))
6576       (save-excursion
6577         (set-buffer buffer)
6578         (gnus-kill-buffer gnus-article-buffer)
6579         (gnus-kill-buffer gnus-original-article-buffer)))
6580     (cond
6581      ;; Kill the buffer.
6582      (gnus-kill-summary-on-exit
6583       (when (and gnus-use-trees
6584                  (gnus-buffer-exists-p buffer))
6585         (save-excursion
6586           (set-buffer buffer)
6587           (gnus-tree-close gnus-newsgroup-name)))
6588       (gnus-kill-buffer buffer))
6589      ;; Deaden the buffer.
6590      ((gnus-buffer-exists-p buffer)
6591       (save-excursion
6592         (set-buffer buffer)
6593         (gnus-deaden-summary))))))
6594
6595 (defun gnus-summary-wake-up-the-dead (&rest args)
6596   "Wake up the dead summary buffer."
6597   (interactive)
6598   (gnus-dead-summary-mode -1)
6599   (let ((name (buffer-name)))
6600     (when (string-match "Dead " name)
6601       (rename-buffer
6602        (concat (substring name 0 (match-beginning 0))
6603                (substring name (match-end 0)))
6604        t)))
6605   (gnus-message 3 "This dead summary is now alive again"))
6606
6607 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6608 (defun gnus-summary-fetch-faq (&optional faq-dir)
6609   "Fetch the FAQ for the current group.
6610 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6611 in."
6612   (interactive
6613    (list
6614     (when current-prefix-arg
6615       (completing-read
6616        "FAQ dir: " (and (listp gnus-group-faq-directory)
6617                         (mapcar (lambda (file) (list file))
6618                                 gnus-group-faq-directory))))))
6619   (let (gnus-faq-buffer)
6620     (when (setq gnus-faq-buffer
6621                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6622       (gnus-configure-windows 'summary-faq))))
6623
6624 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6625 (defun gnus-summary-describe-group (&optional force)
6626   "Describe the current newsgroup."
6627   (interactive "P")
6628   (gnus-group-describe-group force gnus-newsgroup-name))
6629
6630 (defun gnus-summary-describe-briefly ()
6631   "Describe summary mode commands briefly."
6632   (interactive)
6633   (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")))
6634
6635 ;; Walking around group mode buffer from summary mode.
6636
6637 (defun gnus-summary-next-group (&optional no-article target-group backward)
6638   "Exit current newsgroup and then select next unread newsgroup.
6639 If prefix argument NO-ARTICLE is non-nil, no article is selected
6640 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6641 previous group instead."
6642   (interactive "P")
6643   ;; Stop pre-fetching.
6644   (gnus-async-halt-prefetch)
6645   (let ((current-group gnus-newsgroup-name)
6646         (current-buffer (current-buffer))
6647         entered)
6648     (while (not entered)
6649       ;; Then we find what group we are supposed to enter.
6650       (set-buffer gnus-group-buffer)
6651       (gnus-group-jump-to-group current-group)
6652       (setq target-group
6653             (or target-group
6654                 (if (eq gnus-keep-same-level 'best)
6655                     (gnus-summary-best-group gnus-newsgroup-name)
6656                   (gnus-summary-search-group backward gnus-keep-same-level))))
6657       (if (not target-group)
6658           ;; There are no further groups, so we return to the group
6659           ;; buffer.
6660           (progn
6661             (gnus-message 5 "Returning to the group buffer")
6662             (setq entered t)
6663             (when (gnus-buffer-live-p current-buffer)
6664               (set-buffer current-buffer)
6665               (gnus-summary-exit))
6666             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6667         ;; We try to enter the target group.
6668         (gnus-group-jump-to-group target-group)
6669         (let ((unreads (gnus-group-group-unread)))
6670           (if (and (or (eq t unreads)
6671                        (and unreads (not (zerop unreads))))
6672                    (progn
6673                      ;; Now we semi-exit this group to update Xrefs
6674                      ;; and all variables.  We can't do a real exit,
6675                      ;; because the window conf must remain the same
6676                      ;; in case the user is prompted for info, and we
6677                      ;; don't want the window conf to change before
6678                      ;; that...
6679                      (when (gnus-buffer-live-p current-buffer)
6680                        (set-buffer current-buffer)
6681                        (gnus-summary-exit t))
6682                      (gnus-summary-read-group
6683                       target-group nil no-article
6684                       (and (buffer-name current-buffer) current-buffer)
6685                       nil backward)))
6686               (setq entered t)
6687             (setq current-group target-group
6688                   target-group nil)))))))
6689
6690 (defun gnus-summary-prev-group (&optional no-article)
6691   "Exit current newsgroup and then select previous unread newsgroup.
6692 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6693   (interactive "P")
6694   (gnus-summary-next-group no-article nil t))
6695
6696 ;; Walking around summary lines.
6697
6698 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6699   "Go to the first unread subject.
6700 If UNREAD is non-nil, go to the first unread article.
6701 Returns the article selected or nil if there are no unread articles."
6702   (interactive "P")
6703   (prog1
6704       (cond
6705        ;; Empty summary.
6706        ((null gnus-newsgroup-data)
6707         (gnus-message 3 "No articles in the group")
6708         nil)
6709        ;; Pick the first article.
6710        ((not unread)
6711         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6712         (gnus-data-number (car gnus-newsgroup-data)))
6713        ;; No unread articles.
6714        ((null gnus-newsgroup-unreads)
6715         (gnus-message 3 "No more unread articles")
6716         nil)
6717        ;; Find the first unread article.
6718        (t
6719         (let ((data gnus-newsgroup-data))
6720           (while (and data
6721                       (and (not (and undownloaded
6722                                      (memq (car data)
6723                                            gnus-newsgroup-undownloaded)))
6724                            (if unseen
6725                                (or (not (memq
6726                                          (gnus-data-number (car data))
6727                                          gnus-newsgroup-unseen))
6728                                    (not (gnus-data-unread-p (car data))))
6729                              (not (gnus-data-unread-p (car data))))))
6730             (setq data (cdr data)))
6731           (when data
6732             (goto-char (gnus-data-pos (car data)))
6733             (gnus-data-number (car data))))))
6734     (gnus-summary-position-point)))
6735
6736 (defun gnus-summary-next-subject (n &optional unread dont-display)
6737   "Go to next N'th summary line.
6738 If N is negative, go to the previous N'th subject line.
6739 If UNREAD is non-nil, only unread articles are selected.
6740 The difference between N and the actual number of steps taken is
6741 returned."
6742   (interactive "p")
6743   (let ((backward (< n 0))
6744         (n (abs n)))
6745     (while (and (> n 0)
6746                 (if backward
6747                     (gnus-summary-find-prev unread)
6748                   (gnus-summary-find-next unread)))
6749       (unless (zerop (setq n (1- n)))
6750         (gnus-summary-show-thread)))
6751     (when (/= 0 n)
6752       (gnus-message 7 "No more%s articles"
6753                     (if unread " unread" "")))
6754     (unless dont-display
6755       (gnus-summary-recenter)
6756       (gnus-summary-position-point))
6757     n))
6758
6759 (defun gnus-summary-next-unread-subject (n)
6760   "Go to next N'th unread summary line."
6761   (interactive "p")
6762   (gnus-summary-next-subject n t))
6763
6764 (defun gnus-summary-prev-subject (n &optional unread)
6765   "Go to previous N'th summary line.
6766 If optional argument UNREAD is non-nil, only unread article is selected."
6767   (interactive "p")
6768   (gnus-summary-next-subject (- n) unread))
6769
6770 (defun gnus-summary-prev-unread-subject (n)
6771   "Go to previous N'th unread summary line."
6772   (interactive "p")
6773   (gnus-summary-next-subject (- n) t))
6774
6775 (defun gnus-summary-goto-subjects (articles)
6776   "Insert the subject header for ARTICLES in the current buffer."
6777   (save-excursion
6778     (dolist (article articles)
6779       (gnus-summary-goto-subject article t)))
6780   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6781   (gnus-summary-position-point))
6782  
6783 (defun gnus-summary-goto-subject (article &optional force silent)
6784   "Go the subject line of ARTICLE.
6785 If FORCE, also allow jumping to articles not currently shown."
6786   (interactive "nArticle number: ")
6787   (unless (numberp article)
6788     (error "Article %s is not a number" article))
6789   (let ((b (point))
6790         (data (gnus-data-find article)))
6791     ;; We read in the article if we have to.
6792     (and (not data)
6793          force
6794          (gnus-summary-insert-subject
6795           article
6796           (if (or (numberp force) (vectorp force)) force)
6797           t)
6798          (setq data (gnus-data-find article)))
6799     (goto-char b)
6800     (if (not data)
6801         (progn
6802           (unless silent
6803             (gnus-message 3 "Can't find article %d" article))
6804           nil)
6805       (let ((pt (gnus-data-pos data)))
6806         (goto-char pt)
6807         (gnus-summary-set-article-display-arrow pt))
6808       (gnus-summary-position-point)
6809       article)))
6810
6811 ;; Walking around summary lines with displaying articles.
6812
6813 (defun gnus-summary-expand-window (&optional arg)
6814   "Make the summary buffer take up the entire Emacs frame.
6815 Given a prefix, will force an `article' buffer configuration."
6816   (interactive "P")
6817   (if arg
6818       (gnus-configure-windows 'article 'force)
6819     (gnus-configure-windows 'summary 'force)))
6820
6821 (defun gnus-summary-display-article (article &optional all-header)
6822   "Display ARTICLE in article buffer."
6823   (when (gnus-buffer-live-p gnus-article-buffer)
6824     (with-current-buffer gnus-article-buffer
6825       (mm-enable-multibyte)))
6826   (gnus-set-global-variables)
6827   (when (gnus-buffer-live-p gnus-article-buffer)
6828     (with-current-buffer gnus-article-buffer
6829       (setq gnus-article-charset gnus-newsgroup-charset)
6830       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6831       (mm-enable-multibyte)))
6832   (if (null article)
6833       nil
6834     (prog1
6835         (if gnus-summary-display-article-function
6836             (funcall gnus-summary-display-article-function article all-header)
6837           (gnus-article-prepare article all-header))
6838       (gnus-run-hooks 'gnus-select-article-hook)
6839       (when (and gnus-current-article
6840                  (not (zerop gnus-current-article)))
6841         (gnus-summary-goto-subject gnus-current-article))
6842       (gnus-summary-recenter)
6843       (when (and gnus-use-trees gnus-show-threads)
6844         (gnus-possibly-generate-tree article)
6845         (gnus-highlight-selected-tree article))
6846       ;; Successfully display article.
6847       (gnus-article-set-window-start
6848        (cdr (assq article gnus-newsgroup-bookmarks))))))
6849
6850 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6851   "Select the current article.
6852 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6853 non-nil, the article will be re-fetched even if it already present in
6854 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6855 be displayed."
6856   ;; Make sure we are in the summary buffer to work around bbdb bug.
6857   (unless (eq major-mode 'gnus-summary-mode)
6858     (set-buffer gnus-summary-buffer))
6859   (let ((article (or article (gnus-summary-article-number)))
6860         (all-headers (not (not all-headers))) ;Must be t or nil.
6861         gnus-summary-display-article-function)
6862     (and (not pseudo)
6863          (gnus-summary-article-pseudo-p article)
6864          (error "This is a pseudo-article"))
6865     (save-excursion
6866       (set-buffer gnus-summary-buffer)
6867       (if (or (and gnus-single-article-buffer
6868                    (or (null gnus-current-article)
6869                        (null gnus-article-current)
6870                        (null (get-buffer gnus-article-buffer))
6871                        (not (eq article (cdr gnus-article-current)))
6872                        (not (equal (car gnus-article-current)
6873                                    gnus-newsgroup-name))))
6874               (and (not gnus-single-article-buffer)
6875                    (or (null gnus-current-article)
6876                        (not (eq gnus-current-article article))))
6877               force)
6878           ;; The requested article is different from the current article.
6879           (progn
6880             (gnus-summary-display-article article all-headers)
6881             (when (gnus-buffer-live-p gnus-article-buffer)
6882               (with-current-buffer gnus-article-buffer
6883                 (if (not gnus-article-decoded-p) ;; a local variable
6884                     (mm-disable-multibyte))))
6885             (gnus-article-set-window-start
6886              (cdr (assq article gnus-newsgroup-bookmarks)))
6887             article)
6888         'old))))
6889
6890 (defun gnus-summary-force-verify-and-decrypt ()
6891   (interactive)
6892   (let ((mm-verify-option 'known)
6893         (mm-decrypt-option 'known)
6894         (gnus-buttonized-mime-types (append (list "multipart/signed"
6895                                                   "multipart/encrypted")
6896                                             gnus-buttonized-mime-types)))
6897     (gnus-summary-select-article nil 'force)))
6898
6899 (defun gnus-summary-set-current-mark (&optional current-mark)
6900   "Obsolete function."
6901   nil)
6902
6903 (defun gnus-summary-next-article (&optional unread subject backward push)
6904   "Select the next article.
6905 If UNREAD, only unread articles are selected.
6906 If SUBJECT, only articles with SUBJECT are selected.
6907 If BACKWARD, the previous article is selected instead of the next."
6908   (interactive "P")
6909   (cond
6910    ;; Is there such an article?
6911    ((and (gnus-summary-search-forward unread subject backward)
6912          (or (gnus-summary-display-article (gnus-summary-article-number))
6913              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6914     (gnus-summary-position-point))
6915    ;; If not, we try the first unread, if that is wanted.
6916    ((and subject
6917          gnus-auto-select-same
6918          (gnus-summary-first-unread-article))
6919     (gnus-summary-position-point)
6920     (gnus-message 6 "Wrapped"))
6921    ;; Try to get next/previous article not displayed in this group.
6922    ((and gnus-auto-extend-newsgroup
6923          (not unread) (not subject))
6924     (gnus-summary-goto-article
6925      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6926      nil (count-lines (point-min) (point))))
6927    ;; Go to next/previous group.
6928    (t
6929     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6930       (gnus-summary-jump-to-group gnus-newsgroup-name))
6931     (let ((cmd last-command-char)
6932           (point
6933            (save-excursion
6934              (set-buffer gnus-group-buffer)
6935              (point)))
6936           (group
6937            (if (eq gnus-keep-same-level 'best)
6938                (gnus-summary-best-group gnus-newsgroup-name)
6939              (gnus-summary-search-group backward gnus-keep-same-level))))
6940       ;; For some reason, the group window gets selected.  We change
6941       ;; it back.
6942       (select-window (get-buffer-window (current-buffer)))
6943       ;; Select next unread newsgroup automagically.
6944       (cond
6945        ((or (not gnus-auto-select-next)
6946             (not cmd))
6947         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6948        ((or (eq gnus-auto-select-next 'quietly)
6949             (and (eq gnus-auto-select-next 'slightly-quietly)
6950                  push)
6951             (and (eq gnus-auto-select-next 'almost-quietly)
6952                  (gnus-summary-last-article-p)))
6953         ;; Select quietly.
6954         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6955             (gnus-summary-exit)
6956           (gnus-message 7 "No more%s articles (%s)..."
6957                         (if unread " unread" "")
6958                         (if group (concat "selecting " group)
6959                           "exiting"))
6960           (gnus-summary-next-group nil group backward)))
6961        (t
6962         (when (gnus-key-press-event-p last-input-event)
6963           (gnus-summary-walk-group-buffer
6964            gnus-newsgroup-name cmd unread backward point))))))))
6965
6966 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6967   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6968                       (?\C-p (gnus-group-prev-unread-group 1))))
6969         (cursor-in-echo-area t)
6970         keve key group ended prompt)
6971     (save-excursion
6972       (set-buffer gnus-group-buffer)
6973       (goto-char start)
6974       (setq group
6975             (if (eq gnus-keep-same-level 'best)
6976                 (gnus-summary-best-group gnus-newsgroup-name)
6977               (gnus-summary-search-group backward gnus-keep-same-level))))
6978     (while (not ended)
6979       (setq prompt
6980             (format
6981              "No more%s articles%s " (if unread " unread" "")
6982              (if (and group
6983                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6984                  (format " (Type %s for %s [%s])"
6985                          (single-key-description cmd) group
6986                          (car (gnus-gethash group gnus-newsrc-hashtb)))
6987                (format " (Type %s to exit %s)"
6988                        (single-key-description cmd)
6989                        gnus-newsgroup-name))))
6990       ;; Confirm auto selection.
6991       (setq key (car (setq keve (gnus-read-event-char prompt)))
6992             ended t)
6993       (cond
6994        ((assq key keystrokes)
6995         (let ((obuf (current-buffer)))
6996           (switch-to-buffer gnus-group-buffer)
6997           (when group
6998             (gnus-group-jump-to-group group))
6999           (eval (cadr (assq key keystrokes)))
7000           (setq group (gnus-group-group-name))
7001           (switch-to-buffer obuf))
7002         (setq ended nil))
7003        ((equal key cmd)
7004         (if (or (not group)
7005                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7006             (gnus-summary-exit)
7007           (gnus-summary-next-group nil group backward)))
7008        (t
7009         (push (cdr keve) unread-command-events))))))
7010
7011 (defun gnus-summary-next-unread-article ()
7012   "Select unread article after current one."
7013   (interactive)
7014   (gnus-summary-next-article
7015    (or (not (eq gnus-summary-goto-unread 'never))
7016        (gnus-summary-last-article-p (gnus-summary-article-number)))
7017    (and gnus-auto-select-same
7018         (gnus-summary-article-subject))))
7019
7020 (defun gnus-summary-prev-article (&optional unread subject)
7021   "Select the article after the current one.
7022 If UNREAD is non-nil, only unread articles are selected."
7023   (interactive "P")
7024   (gnus-summary-next-article unread subject t))
7025
7026 (defun gnus-summary-prev-unread-article ()
7027   "Select unread article before current one."
7028   (interactive)
7029   (gnus-summary-prev-article
7030    (or (not (eq gnus-summary-goto-unread 'never))
7031        (gnus-summary-first-article-p (gnus-summary-article-number)))
7032    (and gnus-auto-select-same
7033         (gnus-summary-article-subject))))
7034
7035 (defun gnus-summary-next-page (&optional lines circular stop)
7036   "Show next page of the selected article.
7037 If at the end of the current article, select the next article.
7038 LINES says how many lines should be scrolled up.
7039
7040 If CIRCULAR is non-nil, go to the start of the article instead of
7041 selecting the next article when reaching the end of the current
7042 article.
7043
7044 If STOP is non-nil, just stop when reaching the end of the message."
7045   (interactive "P")
7046   (setq gnus-summary-buffer (current-buffer))
7047   (gnus-set-global-variables)
7048   (let ((article (gnus-summary-article-number))
7049         (article-window (get-buffer-window gnus-article-buffer t))
7050         endp)
7051     ;; If the buffer is empty, we have no article.
7052     (unless article
7053       (error "No article to select"))
7054     (gnus-configure-windows 'article)
7055     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7056         (if (and (eq gnus-summary-goto-unread 'never)
7057                  (not (gnus-summary-last-article-p article)))
7058             (gnus-summary-next-article)
7059           (gnus-summary-next-unread-article))
7060       (if (or (null gnus-current-article)
7061               (null gnus-article-current)
7062               (/= article (cdr gnus-article-current))
7063               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7064           ;; Selected subject is different from current article's.
7065           (gnus-summary-display-article article)
7066         (when article-window
7067           (gnus-eval-in-buffer-window gnus-article-buffer
7068             (setq endp (gnus-article-next-page lines)))
7069           (when endp
7070             (cond (stop
7071                    (gnus-message 3 "End of message"))
7072                   (circular
7073                    (gnus-summary-beginning-of-article))
7074                   (lines
7075                    (gnus-message 3 "End of message"))
7076                   ((null lines)
7077                    (if (and (eq gnus-summary-goto-unread 'never)
7078                             (not (gnus-summary-last-article-p article)))
7079                        (gnus-summary-next-article)
7080                      (gnus-summary-next-unread-article))))))))
7081     (gnus-summary-recenter)
7082     (gnus-summary-position-point)))
7083
7084 (defun gnus-summary-prev-page (&optional lines move)
7085   "Show previous page of selected article.
7086 Argument LINES specifies lines to be scrolled down.
7087 If MOVE, move to the previous unread article if point is at
7088 the beginning of the buffer."
7089   (interactive "P")
7090   (let ((article (gnus-summary-article-number))
7091         (article-window (get-buffer-window gnus-article-buffer t))
7092         endp)
7093     (gnus-configure-windows 'article)
7094     (if (or (null gnus-current-article)
7095             (null gnus-article-current)
7096             (/= article (cdr gnus-article-current))
7097             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7098         ;; Selected subject is different from current article's.
7099         (gnus-summary-display-article article)
7100       (gnus-summary-recenter)
7101       (when article-window
7102         (gnus-eval-in-buffer-window gnus-article-buffer
7103           (setq endp (gnus-article-prev-page lines)))
7104         (when (and move endp)
7105           (cond (lines
7106                  (gnus-message 3 "Beginning of message"))
7107                 ((null lines)
7108                  (if (and (eq gnus-summary-goto-unread 'never)
7109                           (not (gnus-summary-first-article-p article)))
7110                      (gnus-summary-prev-article)
7111                    (gnus-summary-prev-unread-article))))))))
7112   (gnus-summary-position-point))
7113
7114 (defun gnus-summary-prev-page-or-article (&optional lines)
7115   "Show previous page of selected article.
7116 Argument LINES specifies lines to be scrolled down.
7117 If at the beginning of the article, go to the next article."
7118   (interactive "P")
7119   (gnus-summary-prev-page lines t))
7120
7121 (defun gnus-summary-scroll-up (lines)
7122   "Scroll up (or down) one line current article.
7123 Argument LINES specifies lines to be scrolled up (or down if negative)."
7124   (interactive "p")
7125   (gnus-configure-windows 'article)
7126   (gnus-summary-show-thread)
7127   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7128     (gnus-eval-in-buffer-window gnus-article-buffer
7129       (cond ((> lines 0)
7130              (when (gnus-article-next-page lines)
7131                (gnus-message 3 "End of message")))
7132             ((< lines 0)
7133              (gnus-article-prev-page (- lines))))))
7134   (gnus-summary-recenter)
7135   (gnus-summary-position-point))
7136
7137 (defun gnus-summary-scroll-down (lines)
7138   "Scroll down (or up) one line current article.
7139 Argument LINES specifies lines to be scrolled down (or up if negative)."
7140   (interactive "p")
7141   (gnus-summary-scroll-up (- lines)))
7142
7143 (defun gnus-summary-next-same-subject ()
7144   "Select next article which has the same subject as current one."
7145   (interactive)
7146   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7147
7148 (defun gnus-summary-prev-same-subject ()
7149   "Select previous article which has the same subject as current one."
7150   (interactive)
7151   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7152
7153 (defun gnus-summary-next-unread-same-subject ()
7154   "Select next unread article which has the same subject as current one."
7155   (interactive)
7156   (gnus-summary-next-article t (gnus-summary-article-subject)))
7157
7158 (defun gnus-summary-prev-unread-same-subject ()
7159   "Select previous unread article which has the same subject as current one."
7160   (interactive)
7161   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7162
7163 (defun gnus-summary-first-unread-article ()
7164   "Select the first unread article.
7165 Return nil if there are no unread articles."
7166   (interactive)
7167   (prog1
7168       (when (gnus-summary-first-subject t)
7169         (gnus-summary-show-thread)
7170         (gnus-summary-first-subject t)
7171         (gnus-summary-display-article (gnus-summary-article-number)))
7172     (gnus-summary-position-point)))
7173
7174 (defun gnus-summary-first-unread-subject ()
7175   "Place the point on the subject line of the first unread article.
7176 Return nil if there are no unread articles."
7177   (interactive)
7178   (prog1
7179       (when (gnus-summary-first-subject t)
7180         (gnus-summary-show-thread)
7181         (gnus-summary-first-subject t))
7182     (gnus-summary-position-point)))
7183
7184 (defun gnus-summary-first-unseen-subject ()
7185   "Place the point on the subject line of the first unseen article.
7186 Return nil if there are no unseen articles."
7187   (interactive)
7188   (prog1
7189       (when (gnus-summary-first-subject t t t)
7190         (gnus-summary-show-thread)
7191         (gnus-summary-first-subject t t t))
7192     (gnus-summary-position-point)))
7193
7194 (defun gnus-summary-first-unseen-or-unread-subject ()
7195   "Place the point on the subject line of the first unseen article.
7196 Return nil if there are no unseen articles."
7197   (interactive)
7198   (prog1
7199       (unless (when (gnus-summary-first-subject t t t)
7200                 (gnus-summary-show-thread)
7201                 (gnus-summary-first-subject t t t))
7202         (when (gnus-summary-first-subject t)
7203           (gnus-summary-show-thread)
7204           (gnus-summary-first-subject t)))
7205     (gnus-summary-position-point)))
7206
7207 (defun gnus-summary-first-article ()
7208   "Select the first article.
7209 Return nil if there are no articles."
7210   (interactive)
7211   (prog1
7212       (when (gnus-summary-first-subject)
7213         (gnus-summary-show-thread)
7214         (gnus-summary-first-subject)
7215         (gnus-summary-display-article (gnus-summary-article-number)))
7216     (gnus-summary-position-point)))
7217
7218 (defun gnus-summary-best-unread-article (&optional arg)
7219   "Select the unread article with the highest score.
7220 If given a prefix argument, select the next unread article that has a
7221 score higher than the default score."
7222   (interactive "P")
7223   (let ((article (if arg
7224                      (gnus-summary-better-unread-subject)
7225                    (gnus-summary-best-unread-subject))))
7226     (if article
7227         (gnus-summary-goto-article article)
7228       (error "No unread articles"))))
7229
7230 (defun gnus-summary-best-unread-subject ()
7231   "Select the unread subject with the highest score."
7232   (interactive)
7233   (let ((best -1000000)
7234         (data gnus-newsgroup-data)
7235         article score)
7236     (while data
7237       (and (gnus-data-unread-p (car data))
7238            (> (setq score
7239                     (gnus-summary-article-score (gnus-data-number (car data))))
7240               best)
7241            (setq best score
7242                  article (gnus-data-number (car data))))
7243       (setq data (cdr data)))
7244     (when article
7245       (gnus-summary-goto-subject article))
7246     (gnus-summary-position-point)
7247     article))
7248
7249 (defun gnus-summary-better-unread-subject ()
7250   "Select the first unread subject that has a score over the default score."
7251   (interactive)
7252   (let ((data gnus-newsgroup-data)
7253         article score)
7254     (while (and (setq article (gnus-data-number (car data)))
7255                 (or (gnus-data-read-p (car data))
7256                     (not (> (gnus-summary-article-score article)
7257                             gnus-summary-default-score))))
7258       (setq data (cdr data)))
7259     (when article
7260       (gnus-summary-goto-subject article))
7261     (gnus-summary-position-point)
7262     article))
7263
7264 (defun gnus-summary-last-subject ()
7265   "Go to the last displayed subject line in the group."
7266   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7267     (when article
7268       (gnus-summary-goto-subject article))))
7269
7270 (defun gnus-summary-goto-article (article &optional all-headers force)
7271   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7272 If ALL-HEADERS is non-nil, no header lines are hidden.
7273 If FORCE, go to the article even if it isn't displayed.  If FORCE
7274 is a number, it is the line the article is to be displayed on."
7275   (interactive
7276    (list
7277     (completing-read
7278      "Article number or Message-ID: "
7279      (mapcar (lambda (number) (list (int-to-string number)))
7280              gnus-newsgroup-limit))
7281     current-prefix-arg
7282     t))
7283   (prog1
7284       (if (and (stringp article)
7285                (string-match "@" article))
7286           (gnus-summary-refer-article article)
7287         (when (stringp article)
7288           (setq article (string-to-number article)))
7289         (if (gnus-summary-goto-subject article force)
7290             (gnus-summary-display-article article all-headers)
7291           (gnus-message 4 "Couldn't go to article %s" article) nil))
7292     (gnus-summary-position-point)))
7293
7294 (defun gnus-summary-goto-last-article ()
7295   "Go to the previously read article."
7296   (interactive)
7297   (prog1
7298       (when gnus-last-article
7299         (gnus-summary-goto-article gnus-last-article nil t))
7300     (gnus-summary-position-point)))
7301
7302 (defun gnus-summary-pop-article (number)
7303   "Pop one article off the history and go to the previous.
7304 NUMBER articles will be popped off."
7305   (interactive "p")
7306   (let (to)
7307     (setq gnus-newsgroup-history
7308           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7309     (if to
7310         (gnus-summary-goto-article (car to) nil t)
7311       (error "Article history empty")))
7312   (gnus-summary-position-point))
7313
7314 ;; Summary commands and functions for limiting the summary buffer.
7315
7316 (defun gnus-summary-limit-to-articles (n)
7317   "Limit the summary buffer to the next N articles.
7318 If not given a prefix, use the process marked articles instead."
7319   (interactive "P")
7320   (prog1
7321       (let ((articles (gnus-summary-work-articles n)))
7322         (setq gnus-newsgroup-processable nil)
7323         (gnus-summary-limit articles))
7324     (gnus-summary-position-point)))
7325
7326 (defun gnus-summary-pop-limit (&optional total)
7327   "Restore the previous limit.
7328 If given a prefix, remove all limits."
7329   (interactive "P")
7330   (when total
7331     (setq gnus-newsgroup-limits
7332           (list (mapcar (lambda (h) (mail-header-number h))
7333                         gnus-newsgroup-headers))))
7334   (unless gnus-newsgroup-limits
7335     (error "No limit to pop"))
7336   (prog1
7337       (gnus-summary-limit nil 'pop)
7338     (gnus-summary-position-point)))
7339
7340 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7341   "Limit the summary buffer to articles that have subjects that match a regexp.
7342 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7343   (interactive
7344    (list (read-string (if current-prefix-arg
7345                           "Exclude subject (regexp): "
7346                         "Limit to subject (regexp): "))
7347          nil current-prefix-arg))
7348   (unless header
7349     (setq header "subject"))
7350   (when (not (equal "" subject))
7351     (prog1
7352         (let ((articles (gnus-summary-find-matching
7353                          (or header "subject") subject 'all nil nil
7354                          not-matching)))
7355           (unless articles
7356             (error "Found no matches for \"%s\"" subject))
7357           (gnus-summary-limit articles))
7358       (gnus-summary-position-point))))
7359
7360 (defun gnus-summary-limit-to-author (from &optional not-matching)
7361   "Limit the summary buffer to articles that have authors that match a regexp.
7362 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7363   (interactive
7364    (list (read-string (if current-prefix-arg
7365                           "Exclude author (regexp): "
7366                         "Limit to author (regexp): "))
7367          current-prefix-arg))
7368   (gnus-summary-limit-to-subject from "from" not-matching))
7369
7370 (defun gnus-summary-limit-to-age (age &optional younger-p)
7371   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7372 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7373 articles that are younger than AGE days."
7374   (interactive
7375    (let ((younger current-prefix-arg)
7376          (days-got nil)
7377          days)
7378      (while (not days-got)
7379        (setq days (if younger
7380                       (read-string "Limit to articles younger than (in days, older when negative): ")
7381                     (read-string
7382                      "Limit to articles older than (in days, younger when negative): ")))
7383        (when (> (length days) 0)
7384          (setq days (read days)))
7385        (if (numberp days)
7386            (progn
7387              (setq days-got t)
7388              (if (< days 0)
7389                  (progn
7390                    (setq younger (not younger))
7391                    (setq days (* days -1)))))
7392          (message "Please enter a number.")
7393          (sleep-for 1)))
7394      (list days younger)))
7395   (prog1
7396       (let ((data gnus-newsgroup-data)
7397             (cutoff (days-to-time age))
7398             articles d date is-younger)
7399         (while (setq d (pop data))
7400           (when (and (vectorp (gnus-data-header d))
7401                      (setq date (mail-header-date (gnus-data-header d))))
7402             (setq is-younger (time-less-p
7403                               (time-since (condition-case ()
7404                                               (date-to-time date)
7405                                             (error '(0 0))))
7406                               cutoff))
7407             (when (if younger-p
7408                       is-younger
7409                     (not is-younger))
7410               (push (gnus-data-number d) articles))))
7411         (gnus-summary-limit (nreverse articles)))
7412     (gnus-summary-position-point)))
7413
7414 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7415   "Limit the summary buffer to articles that match an 'extra' header."
7416   (interactive
7417    (let ((header
7418           (intern
7419            (gnus-completing-read-with-default
7420             (symbol-name (car gnus-extra-headers))
7421             (if current-prefix-arg
7422                 "Exclude extra header:"
7423               "Limit extra header:")
7424             (mapcar (lambda (x)
7425                       (cons (symbol-name x) x))
7426                     gnus-extra-headers)
7427             nil
7428             t))))
7429      (list header
7430            (read-string (format "%s header %s (regexp): "
7431                                 (if current-prefix-arg "Exclude" "Limit to")
7432                                 header))
7433            current-prefix-arg)))
7434   (when (not (equal "" regexp))
7435     (prog1
7436         (let ((articles (gnus-summary-find-matching
7437                          (cons 'extra header) regexp 'all nil nil
7438                          not-matching)))
7439           (unless articles
7440             (error "Found no matches for \"%s\"" regexp))
7441           (gnus-summary-limit articles))
7442       (gnus-summary-position-point))))
7443
7444 (defun gnus-summary-limit-to-display-predicate ()
7445   "Limit the summary buffer to the predicated in the `display' group parameter."
7446   (interactive)
7447   (unless gnus-newsgroup-display
7448     (error "There is no `display' group parameter"))
7449   (let (articles)
7450     (dolist (number gnus-newsgroup-articles)
7451       (when (funcall gnus-newsgroup-display)
7452         (push number articles)))
7453     (gnus-summary-limit articles))
7454   (gnus-summary-position-point))
7455
7456 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7457 (make-obsolete
7458  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7459
7460 (defun gnus-summary-limit-to-unread (&optional all)
7461   "Limit the summary buffer to articles that are not marked as read.
7462 If ALL is non-nil, limit strictly to unread articles."
7463   (interactive "P")
7464   (if all
7465       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7466     (gnus-summary-limit-to-marks
7467      ;; Concat all the marks that say that an article is read and have
7468      ;; those removed.
7469      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7470            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7471            gnus-low-score-mark gnus-expirable-mark
7472            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7473            gnus-duplicate-mark gnus-souped-mark)
7474      'reverse)))
7475
7476 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7477 (make-obsolete 'gnus-summary-delete-marked-with
7478                'gnus-summary-limit-exclude-marks)
7479
7480 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7481   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7482 If REVERSE, limit the summary buffer to articles that are marked
7483 with MARKS.  MARKS can either be a string of marks or a list of marks.
7484 Returns how many articles were removed."
7485   (interactive "sMarks: ")
7486   (gnus-summary-limit-to-marks marks t))
7487
7488 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7489   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7490 If REVERSE (the prefix), limit the summary buffer to articles that are
7491 not marked with MARKS.  MARKS can either be a string of marks or a
7492 list of marks.
7493 Returns how many articles were removed."
7494   (interactive "sMarks: \nP")
7495   (prog1
7496       (let ((data gnus-newsgroup-data)
7497             (marks (if (listp marks) marks
7498                      (append marks nil))) ; Transform to list.
7499             articles)
7500         (while data
7501           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7502                   (memq (gnus-data-mark (car data)) marks))
7503             (push (gnus-data-number (car data)) articles))
7504           (setq data (cdr data)))
7505         (gnus-summary-limit articles))
7506     (gnus-summary-position-point)))
7507
7508 (defun gnus-summary-limit-to-score (score)
7509   "Limit to articles with score at or above SCORE."
7510   (interactive "NLimit to articles with score of at least: ")
7511   (let ((data gnus-newsgroup-data)
7512         articles)
7513     (while data
7514       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7515                 score)
7516         (push (gnus-data-number (car data)) articles))
7517       (setq data (cdr data)))
7518     (prog1
7519         (gnus-summary-limit articles)
7520       (gnus-summary-position-point))))
7521
7522 (defun gnus-summary-limit-to-unseen ()
7523   "Limit to unseen articles."
7524   (interactive)
7525   (prog1
7526       (gnus-summary-limit gnus-newsgroup-unseen)
7527     (gnus-summary-position-point)))
7528
7529 (defun gnus-summary-limit-include-thread (id)
7530   "Display all the hidden articles that is in the thread with ID in it.
7531 When called interactively, ID is the Message-ID of the current
7532 article."
7533   (interactive (list (mail-header-id (gnus-summary-article-header))))
7534   (let ((articles (gnus-articles-in-thread
7535                    (gnus-id-to-thread (gnus-root-id id)))))
7536     (prog1
7537         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7538       (gnus-summary-limit-include-matching-articles
7539        "subject"
7540        (regexp-quote (gnus-simplify-subject-re
7541                       (mail-header-subject (gnus-id-to-header id)))))
7542       (gnus-summary-position-point))))
7543
7544 (defun gnus-summary-limit-include-matching-articles (header regexp)
7545   "Display all the hidden articles that have HEADERs that match REGEXP."
7546   (interactive (list (read-string "Match on header: ")
7547                      (read-string "Regexp: ")))
7548   (let ((articles (gnus-find-matching-articles header regexp)))
7549     (prog1
7550         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7551       (gnus-summary-position-point))))
7552
7553 (defun gnus-summary-insert-dormant-articles ()
7554   "Insert all the dormat articles for this group into the current buffer."
7555   (interactive)
7556   (let ((gnus-verbose (max 6 gnus-verbose)))
7557     (if (not gnus-newsgroup-dormant)
7558         (gnus-message 3 "No cached articles for this group")
7559       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7560
7561 (defun gnus-summary-limit-include-dormant ()
7562   "Display all the hidden articles that are marked as dormant.
7563 Note that this command only works on a subset of the articles currently
7564 fetched for this group."
7565   (interactive)
7566   (unless gnus-newsgroup-dormant
7567     (error "There are no dormant articles in this group"))
7568   (prog1
7569       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7570     (gnus-summary-position-point)))
7571
7572 (defun gnus-summary-limit-exclude-dormant ()
7573   "Hide all dormant articles."
7574   (interactive)
7575   (prog1
7576       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7577     (gnus-summary-position-point)))
7578
7579 (defun gnus-summary-limit-exclude-childless-dormant ()
7580   "Hide all dormant articles that have no children."
7581   (interactive)
7582   (let ((data (gnus-data-list t))
7583         articles d children)
7584     ;; Find all articles that are either not dormant or have
7585     ;; children.
7586     (while (setq d (pop data))
7587       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7588                 (and (setq children
7589                            (gnus-article-children (gnus-data-number d)))
7590                      (let (found)
7591                        (while children
7592                          (when (memq (car children) articles)
7593                            (setq children nil
7594                                  found t))
7595                          (pop children))
7596                        found)))
7597         (push (gnus-data-number d) articles)))
7598     ;; Do the limiting.
7599     (prog1
7600         (gnus-summary-limit articles)
7601       (gnus-summary-position-point))))
7602
7603 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7604   "Mark all unread excluded articles as read.
7605 If ALL, mark even excluded ticked and dormants as read."
7606   (interactive "P")
7607   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7608   (let ((articles (gnus-sorted-ndifference
7609                    (sort
7610                     (mapcar (lambda (h) (mail-header-number h))
7611                             gnus-newsgroup-headers)
7612                     '<)
7613                    gnus-newsgroup-limit))
7614         article)
7615     (setq gnus-newsgroup-unreads
7616           (gnus-sorted-intersection gnus-newsgroup-unreads
7617                                     gnus-newsgroup-limit))
7618     (if all
7619         (setq gnus-newsgroup-dormant nil
7620               gnus-newsgroup-marked nil
7621               gnus-newsgroup-reads
7622               (nconc
7623                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7624                gnus-newsgroup-reads))
7625       (while (setq article (pop articles))
7626         (unless (or (memq article gnus-newsgroup-dormant)
7627                     (memq article gnus-newsgroup-marked))
7628           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7629
7630 (defun gnus-summary-limit (articles &optional pop)
7631   (if pop
7632       ;; We pop the previous limit off the stack and use that.
7633       (setq articles (car gnus-newsgroup-limits)
7634             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7635     ;; We use the new limit, so we push the old limit on the stack.
7636     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7637   ;; Set the limit.
7638   (setq gnus-newsgroup-limit articles)
7639   (let ((total (length gnus-newsgroup-data))
7640         (data (gnus-data-find-list (gnus-summary-article-number)))
7641         (gnus-summary-mark-below nil)   ; Inhibit this.
7642         found)
7643     ;; This will do all the work of generating the new summary buffer
7644     ;; according to the new limit.
7645     (gnus-summary-prepare)
7646     ;; Hide any threads, possibly.
7647     (gnus-summary-maybe-hide-threads)
7648     ;; Try to return to the article you were at, or one in the
7649     ;; neighborhood.
7650     (when data
7651       ;; We try to find some article after the current one.
7652       (while data
7653         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7654           (setq data nil
7655                 found t))
7656         (setq data (cdr data))))
7657     (unless found
7658       ;; If there is no data, that means that we were after the last
7659       ;; article.  The same goes when we can't find any articles
7660       ;; after the current one.
7661       (goto-char (point-max))
7662       (gnus-summary-find-prev))
7663     (gnus-set-mode-line 'summary)
7664     ;; We return how many articles were removed from the summary
7665     ;; buffer as a result of the new limit.
7666     (- total (length gnus-newsgroup-data))))
7667
7668 (defsubst gnus-invisible-cut-children (threads)
7669   (let ((num 0))
7670     (while threads
7671       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7672         (incf num))
7673       (pop threads))
7674     (< num 2)))
7675
7676 (defsubst gnus-cut-thread (thread)
7677   "Go forwards in the thread until we find an article that we want to display."
7678   (when (or (eq gnus-fetch-old-headers 'some)
7679             (eq gnus-fetch-old-headers 'invisible)
7680             (numberp gnus-fetch-old-headers)
7681             (eq gnus-build-sparse-threads 'some)
7682             (eq gnus-build-sparse-threads 'more))
7683     ;; Deal with old-fetched headers and sparse threads.
7684     (while (and
7685             thread
7686             (or
7687              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7688              (gnus-summary-article-ancient-p
7689               (mail-header-number (car thread))))
7690             (if (or (<= (length (cdr thread)) 1)
7691                     (eq gnus-fetch-old-headers 'invisible))
7692                 (setq gnus-newsgroup-limit
7693                       (delq (mail-header-number (car thread))
7694                             gnus-newsgroup-limit)
7695                       thread (cadr thread))
7696               (when (gnus-invisible-cut-children (cdr thread))
7697                 (let ((th (cdr thread)))
7698                   (while th
7699                     (if (memq (mail-header-number (caar th))
7700                               gnus-newsgroup-limit)
7701                         (setq thread (car th)
7702                               th nil)
7703                       (setq th (cdr th))))))))))
7704   thread)
7705
7706 (defun gnus-cut-threads (threads)
7707   "Cut off all uninteresting articles from the beginning of threads."
7708   (when (or (eq gnus-fetch-old-headers 'some)
7709             (eq gnus-fetch-old-headers 'invisible)
7710             (numberp gnus-fetch-old-headers)
7711             (eq gnus-build-sparse-threads 'some)
7712             (eq gnus-build-sparse-threads 'more))
7713     (let ((th threads))
7714       (while th
7715         (setcar th (gnus-cut-thread (car th)))
7716         (setq th (cdr th)))))
7717   ;; Remove nixed out threads.
7718   (delq nil threads))
7719
7720 (defun gnus-summary-initial-limit (&optional show-if-empty)
7721   "Figure out what the initial limit is supposed to be on group entry.
7722 This entails weeding out unwanted dormants, low-scored articles,
7723 fetch-old-headers verbiage, and so on."
7724   ;; Most groups have nothing to remove.
7725   (if (or gnus-inhibit-limiting
7726           (and (null gnus-newsgroup-dormant)
7727                (eq gnus-newsgroup-display 'gnus-not-ignore)
7728                (not (eq gnus-fetch-old-headers 'some))
7729                (not (numberp gnus-fetch-old-headers))
7730                (not (eq gnus-fetch-old-headers 'invisible))
7731                (null gnus-summary-expunge-below)
7732                (not (eq gnus-build-sparse-threads 'some))
7733                (not (eq gnus-build-sparse-threads 'more))
7734                (null gnus-thread-expunge-below)
7735                (not gnus-use-nocem)))
7736       ()                                ; Do nothing.
7737     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7738     (setq gnus-newsgroup-limit nil)
7739     (mapatoms
7740      (lambda (node)
7741        (unless (car (symbol-value node))
7742          ;; These threads have no parents -- they are roots.
7743          (let ((nodes (cdr (symbol-value node)))
7744                thread)
7745            (while nodes
7746              (if (and gnus-thread-expunge-below
7747                       (< (gnus-thread-total-score (car nodes))
7748                          gnus-thread-expunge-below))
7749                  (gnus-expunge-thread (pop nodes))
7750                (setq thread (pop nodes))
7751                (gnus-summary-limit-children thread))))))
7752      gnus-newsgroup-dependencies)
7753     ;; If this limitation resulted in an empty group, we might
7754     ;; pop the previous limit and use it instead.
7755     (when (and (not gnus-newsgroup-limit)
7756                show-if-empty)
7757       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7758     gnus-newsgroup-limit))
7759
7760 (defun gnus-summary-limit-children (thread)
7761   "Return 1 if this subthread is visible and 0 if it is not."
7762   ;; First we get the number of visible children to this thread.  This
7763   ;; is done by recursing down the thread using this function, so this
7764   ;; will really go down to a leaf article first, before slowly
7765   ;; working its way up towards the root.
7766   (when thread
7767     (let* ((max-lisp-eval-depth 5000)
7768            (children
7769            (if (cdr thread)
7770                (apply '+ (mapcar 'gnus-summary-limit-children
7771                                  (cdr thread)))
7772              0))
7773           (number (mail-header-number (car thread)))
7774           score)
7775       (if (and
7776            (not (memq number gnus-newsgroup-marked))
7777            (or
7778             ;; If this article is dormant and has absolutely no visible
7779             ;; children, then this article isn't visible.
7780             (and (memq number gnus-newsgroup-dormant)
7781                  (zerop children))
7782             ;; If this is "fetch-old-headered" and there is no
7783             ;; visible children, then we don't want this article.
7784             (and (or (eq gnus-fetch-old-headers 'some)
7785                      (numberp gnus-fetch-old-headers))
7786                  (gnus-summary-article-ancient-p number)
7787                  (zerop children))
7788             ;; If this is "fetch-old-headered" and `invisible', then
7789             ;; we don't want this article.
7790             (and (eq gnus-fetch-old-headers 'invisible)
7791                  (gnus-summary-article-ancient-p number))
7792             ;; If this is a sparsely inserted article with no children,
7793             ;; we don't want it.
7794             (and (eq gnus-build-sparse-threads 'some)
7795                  (gnus-summary-article-sparse-p number)
7796                  (zerop children))
7797             ;; If we use expunging, and this article is really
7798             ;; low-scored, then we don't want this article.
7799             (when (and gnus-summary-expunge-below
7800                        (< (setq score
7801                                 (or (cdr (assq number gnus-newsgroup-scored))
7802                                     gnus-summary-default-score))
7803                           gnus-summary-expunge-below))
7804               ;; We increase the expunge-tally here, but that has
7805               ;; nothing to do with the limits, really.
7806               (incf gnus-newsgroup-expunged-tally)
7807               ;; We also mark as read here, if that's wanted.
7808               (when (and gnus-summary-mark-below
7809                          (< score gnus-summary-mark-below))
7810                 (setq gnus-newsgroup-unreads
7811                       (delq number gnus-newsgroup-unreads))
7812                 (if gnus-newsgroup-auto-expire
7813                     (push number gnus-newsgroup-expirable)
7814                   (push (cons number gnus-low-score-mark)
7815                         gnus-newsgroup-reads)))
7816               t)
7817             ;; Do the `display' group parameter.
7818             (and gnus-newsgroup-display
7819                  (not (funcall gnus-newsgroup-display)))
7820             ;; Check NoCeM things.
7821             (if (and gnus-use-nocem
7822                      (gnus-nocem-unwanted-article-p
7823                       (mail-header-id (car thread))))
7824                 (progn
7825                   (setq gnus-newsgroup-unreads
7826                         (delq number gnus-newsgroup-unreads))
7827                   t))))
7828           ;; Nope, invisible article.
7829           0
7830         ;; Ok, this article is to be visible, so we add it to the limit
7831         ;; and return 1.
7832         (push number gnus-newsgroup-limit)
7833         1))))
7834
7835 (defun gnus-expunge-thread (thread)
7836   "Mark all articles in THREAD as read."
7837   (let* ((number (mail-header-number (car thread))))
7838     (incf gnus-newsgroup-expunged-tally)
7839     ;; We also mark as read here, if that's wanted.
7840     (setq gnus-newsgroup-unreads
7841           (delq number gnus-newsgroup-unreads))
7842     (if gnus-newsgroup-auto-expire
7843         (push number gnus-newsgroup-expirable)
7844       (push (cons number gnus-low-score-mark)
7845             gnus-newsgroup-reads)))
7846   ;; Go recursively through all subthreads.
7847   (mapcar 'gnus-expunge-thread (cdr thread)))
7848
7849 ;; Summary article oriented commands
7850
7851 (defun gnus-summary-refer-parent-article (n)
7852   "Refer parent article N times.
7853 If N is negative, go to ancestor -N instead.
7854 The difference between N and the number of articles fetched is returned."
7855   (interactive "p")
7856   (let ((skip 1)
7857         error header ref)
7858     (when (not (natnump n))
7859       (setq skip (abs n)
7860             n 1))
7861     (while (and (> n 0)
7862                 (not error))
7863       (setq header (gnus-summary-article-header))
7864       (if (and (eq (mail-header-number header)
7865                    (cdr gnus-article-current))
7866                (equal gnus-newsgroup-name
7867                       (car gnus-article-current)))
7868           ;; If we try to find the parent of the currently
7869           ;; displayed article, then we take a look at the actual
7870           ;; References header, since this is slightly more
7871           ;; reliable than the References field we got from the
7872           ;; server.
7873           (save-excursion
7874             (set-buffer gnus-original-article-buffer)
7875             (nnheader-narrow-to-headers)
7876             (unless (setq ref (message-fetch-field "references"))
7877               (setq ref (message-fetch-field "in-reply-to")))
7878             (widen))
7879         (setq ref
7880               ;; It's not the current article, so we take a bet on
7881               ;; the value we got from the server.
7882               (mail-header-references header)))
7883       (if (and ref
7884                (not (equal ref "")))
7885           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7886             (gnus-message 1 "Couldn't find parent"))
7887         (gnus-message 1 "No references in article %d"
7888                       (gnus-summary-article-number))
7889         (setq error t))
7890       (decf n))
7891     (gnus-summary-position-point)
7892     n))
7893
7894 (defun gnus-summary-refer-references ()
7895   "Fetch all articles mentioned in the References header.
7896 Return the number of articles fetched."
7897   (interactive)
7898   (let ((ref (mail-header-references (gnus-summary-article-header)))
7899         (current (gnus-summary-article-number))
7900         (n 0))
7901     (if (or (not ref)
7902             (equal ref ""))
7903         (error "No References in the current article")
7904       ;; For each Message-ID in the References header...
7905       (while (string-match "<[^>]*>" ref)
7906         (incf n)
7907         ;; ... fetch that article.
7908         (gnus-summary-refer-article
7909          (prog1 (match-string 0 ref)
7910            (setq ref (substring ref (match-end 0))))))
7911       (gnus-summary-goto-subject current)
7912       (gnus-summary-position-point)
7913       n)))
7914
7915 (defun gnus-summary-refer-thread (&optional limit)
7916   "Fetch all articles in the current thread.
7917 If LIMIT (the numerical prefix), fetch that many old headers instead
7918 of what's specified by the `gnus-refer-thread-limit' variable."
7919   (interactive "P")
7920   (let ((id (mail-header-id (gnus-summary-article-header)))
7921         (limit (if limit (prefix-numeric-value limit)
7922                  gnus-refer-thread-limit)))
7923     ;; We want to fetch LIMIT *old* headers, but we also have to
7924     ;; re-fetch all the headers in the current buffer, because many of
7925     ;; them may be undisplayed.  So we adjust LIMIT.
7926     (when (numberp limit)
7927       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7928     (unless (eq gnus-fetch-old-headers 'invisible)
7929       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7930       ;; Retrieve the headers and read them in.
7931       (if (eq (gnus-retrieve-headers
7932                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7933               'nov)
7934           (gnus-build-all-threads)
7935         (error "Can't fetch thread from backends that don't support NOV"))
7936       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7937     (gnus-summary-limit-include-thread id)))
7938
7939 (defun gnus-summary-refer-article (message-id)
7940   "Fetch an article specified by MESSAGE-ID."
7941   (interactive "sMessage-ID: ")
7942   (when (and (stringp message-id)
7943              (not (zerop (length message-id))))
7944     ;; Construct the correct Message-ID if necessary.
7945     ;; Suggested by tale@pawl.rpi.edu.
7946     (unless (string-match "^<" message-id)
7947       (setq message-id (concat "<" message-id)))
7948     (unless (string-match ">$" message-id)
7949       (setq message-id (concat message-id ">")))
7950     (let* ((header (gnus-id-to-header message-id))
7951            (sparse (and header
7952                         (gnus-summary-article-sparse-p
7953                          (mail-header-number header))
7954                         (memq (mail-header-number header)
7955                               gnus-newsgroup-limit)))
7956            number)
7957       (cond
7958        ;; If the article is present in the buffer we just go to it.
7959        ((and header
7960              (or (not (gnus-summary-article-sparse-p
7961                        (mail-header-number header)))
7962                  sparse))
7963         (prog1
7964             (gnus-summary-goto-article
7965              (mail-header-number header) nil t)
7966           (when sparse
7967             (gnus-summary-update-article (mail-header-number header)))))
7968        (t
7969         ;; We fetch the article.
7970         (catch 'found
7971           (dolist (gnus-override-method (gnus-refer-article-methods))
7972             (gnus-check-server gnus-override-method)
7973             ;; Fetch the header, and display the article.
7974             (when (setq number (gnus-summary-insert-subject message-id))
7975               (gnus-summary-select-article nil nil nil number)
7976               (throw 'found t)))
7977           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7978
7979 (defun gnus-refer-article-methods ()
7980   "Return a list of referable methods."
7981   (cond
7982    ;; No method, so we default to current and native.
7983    ((null gnus-refer-article-method)
7984     (list gnus-current-select-method gnus-select-method))
7985    ;; Current.
7986    ((eq 'current gnus-refer-article-method)
7987     (list gnus-current-select-method))
7988    ;; List of select methods.
7989    ((not (and (symbolp (car gnus-refer-article-method))
7990               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7991     (let (out)
7992       (dolist (method gnus-refer-article-method)
7993         (push (if (eq 'current method)
7994                   gnus-current-select-method
7995                 method)
7996               out))
7997       (nreverse out)))
7998    ;; One single select method.
7999    (t
8000     (list gnus-refer-article-method))))
8001
8002 (defun gnus-summary-edit-parameters ()
8003   "Edit the group parameters of the current group."
8004   (interactive)
8005   (gnus-group-edit-group gnus-newsgroup-name 'params))
8006
8007 (defun gnus-summary-customize-parameters ()
8008   "Customize the group parameters of the current group."
8009   (interactive)
8010   (gnus-group-customize gnus-newsgroup-name))
8011
8012 (defun gnus-summary-enter-digest-group (&optional force)
8013   "Enter an nndoc group based on the current article.
8014 If FORCE, force a digest interpretation.  If not, try
8015 to guess what the document format is."
8016   (interactive "P")
8017   (let ((conf gnus-current-window-configuration))
8018     (save-excursion
8019       (gnus-summary-select-article))
8020     (setq gnus-current-window-configuration conf)
8021     (let* ((name (format "%s-%d"
8022                          (gnus-group-prefixed-name
8023                           gnus-newsgroup-name (list 'nndoc ""))
8024                          (save-excursion
8025                            (set-buffer gnus-summary-buffer)
8026                            gnus-current-article)))
8027            (ogroup gnus-newsgroup-name)
8028            (params (append (gnus-info-params (gnus-get-info ogroup))
8029                            (list (cons 'to-group ogroup))
8030                            (list (cons 'save-article-group ogroup))))
8031            (case-fold-search t)
8032            (buf (current-buffer))
8033            dig to-address)
8034       (save-excursion
8035         (set-buffer gnus-original-article-buffer)
8036         ;; Have the digest group inherit the main mail address of
8037         ;; the parent article.
8038         (when (setq to-address (or (gnus-fetch-field "reply-to")
8039                                    (gnus-fetch-field "from")))
8040           (setq params (append
8041                         (list (cons 'to-address
8042                                     (funcall gnus-decode-encoded-word-function
8043                                              to-address))))))
8044         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8045         (insert-buffer-substring gnus-original-article-buffer)
8046         ;; Remove lines that may lead nndoc to misinterpret the
8047         ;; document type.
8048         (narrow-to-region
8049          (goto-char (point-min))
8050          (or (search-forward "\n\n" nil t) (point)))
8051         (goto-char (point-min))
8052         (delete-matching-lines "^Path:\\|^From ")
8053         (widen))
8054       (unwind-protect
8055           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8056                     (gnus-newsgroup-ephemeral-ignored-charsets
8057                      gnus-newsgroup-ignored-charsets))
8058                 (gnus-group-read-ephemeral-group
8059                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8060                               (nndoc-article-type
8061                                ,(if force 'mbox 'guess)))
8062                  t nil nil nil
8063                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8064                                                         "ADAPT")))))
8065               ;; Make all postings to this group go to the parent group.
8066               (nconc (gnus-info-params (gnus-get-info name))
8067                      params)
8068             ;; Couldn't select this doc group.
8069             (switch-to-buffer buf)
8070             (gnus-set-global-variables)
8071             (gnus-configure-windows 'summary)
8072             (gnus-message 3 "Article couldn't be entered?"))
8073         (kill-buffer dig)))))
8074
8075 (defun gnus-summary-read-document (n)
8076   "Open a new group based on the current article(s).
8077 This will allow you to read digests and other similar
8078 documents as newsgroups.
8079 Obeys the standard process/prefix convention."
8080   (interactive "P")
8081   (let* ((articles (gnus-summary-work-articles n))
8082          (ogroup gnus-newsgroup-name)
8083          (params (append (gnus-info-params (gnus-get-info ogroup))
8084                          (list (cons 'to-group ogroup))))
8085          article group egroup groups vgroup)
8086     (while (setq article (pop articles))
8087       (setq group (format "%s-%d" gnus-newsgroup-name article))
8088       (gnus-summary-remove-process-mark article)
8089       (when (gnus-summary-display-article article)
8090         (save-excursion
8091           (with-temp-buffer
8092             (insert-buffer-substring gnus-original-article-buffer)
8093             ;; Remove some headers that may lead nndoc to make
8094             ;; the wrong guess.
8095             (message-narrow-to-head)
8096             (goto-char (point-min))
8097             (delete-matching-lines "^\\(Path\\):\\|^From ")
8098             (widen)
8099             (if (setq egroup
8100                       (gnus-group-read-ephemeral-group
8101                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8102                                      (nndoc-article-type guess))
8103                        t nil t))
8104                 (progn
8105             ;; Make all postings to this group go to the parent group.
8106                   (nconc (gnus-info-params (gnus-get-info egroup))
8107                          params)
8108                   (push egroup groups))
8109               ;; Couldn't select this doc group.
8110               (gnus-error 3 "Article couldn't be entered"))))))
8111     ;; Now we have selected all the documents.
8112     (cond
8113      ((not groups)
8114       (error "None of the articles could be interpreted as documents"))
8115      ((gnus-group-read-ephemeral-group
8116        (setq vgroup (format
8117                      "nnvirtual:%s-%s" gnus-newsgroup-name
8118                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8119        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8120        t
8121        (cons (current-buffer) 'summary)))
8122      (t
8123       (error "Couldn't select virtual nndoc group")))))
8124
8125 (defun gnus-summary-isearch-article (&optional regexp-p)
8126   "Do incremental search forward on the current article.
8127 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8128   (interactive "P")
8129   (gnus-summary-select-article)
8130   (gnus-configure-windows 'article)
8131   (gnus-eval-in-buffer-window gnus-article-buffer
8132     (save-restriction
8133       (widen)
8134       (isearch-forward regexp-p))))
8135
8136 (defun gnus-summary-search-article-forward (regexp &optional backward)
8137   "Search for an article containing REGEXP forward.
8138 If BACKWARD, search backward instead."
8139   (interactive
8140    (list (read-string
8141           (format "Search article %s (regexp%s): "
8142                   (if current-prefix-arg "backward" "forward")
8143                   (if gnus-last-search-regexp
8144                       (concat ", default " gnus-last-search-regexp)
8145                     "")))
8146          current-prefix-arg))
8147   (if (string-equal regexp "")
8148       (setq regexp (or gnus-last-search-regexp ""))
8149     (setq gnus-last-search-regexp regexp)
8150     (setq gnus-article-before-search gnus-current-article))
8151   ;; Intentionally set gnus-last-article.
8152   (setq gnus-last-article gnus-article-before-search)
8153   (let ((gnus-last-article gnus-last-article))
8154     (if (gnus-summary-search-article regexp backward)
8155         (gnus-summary-show-thread)
8156       (error "Search failed: \"%s\"" regexp))))
8157
8158 (defun gnus-summary-search-article-backward (regexp)
8159   "Search for an article containing REGEXP backward."
8160   (interactive
8161    (list (read-string
8162           (format "Search article backward (regexp%s): "
8163                   (if gnus-last-search-regexp
8164                       (concat ", default " gnus-last-search-regexp)
8165                     "")))))
8166   (gnus-summary-search-article-forward regexp 'backward))
8167
8168 (defun gnus-summary-search-article (regexp &optional backward)
8169   "Search for an article containing REGEXP.
8170 Optional argument BACKWARD means do search for backward.
8171 `gnus-select-article-hook' is not called during the search."
8172   ;; We have to require this here to make sure that the following
8173   ;; dynamic binding isn't shadowed by autoloading.
8174   (require 'gnus-async)
8175   (require 'gnus-art)
8176   (let ((gnus-select-article-hook nil)  ;Disable hook.
8177         (gnus-article-prepare-hook nil)
8178         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8179         (gnus-use-article-prefetch nil)
8180         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8181         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8182         (sum (current-buffer))
8183         (gnus-display-mime-function nil)
8184         (found nil)
8185         point)
8186     (gnus-save-hidden-threads
8187       (gnus-summary-select-article)
8188       (set-buffer gnus-article-buffer)
8189       (goto-char (window-point (get-buffer-window (current-buffer))))
8190       (when backward
8191         (forward-line -1))
8192       (while (not found)
8193         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8194         (if (if backward
8195                 (re-search-backward regexp nil t)
8196               (re-search-forward regexp nil t))
8197             ;; We found the regexp.
8198             (progn
8199               (setq found 'found)
8200               (beginning-of-line)
8201               (set-window-start
8202                (get-buffer-window (current-buffer))
8203                (point))
8204               (forward-line 1)
8205               (set-window-point
8206                (get-buffer-window (current-buffer))
8207                (point))
8208               (set-buffer sum)
8209               (setq point (point)))
8210           ;; We didn't find it, so we go to the next article.
8211           (set-buffer sum)
8212           (setq found 'not)
8213           (while (eq found 'not)
8214             (if (not (if backward (gnus-summary-find-prev)
8215                        (gnus-summary-find-next)))
8216                 ;; No more articles.
8217                 (setq found t)
8218               ;; Select the next article and adjust point.
8219               (unless (gnus-summary-article-sparse-p
8220                        (gnus-summary-article-number))
8221                 (setq found nil)
8222                 (gnus-summary-select-article)
8223                 (set-buffer gnus-article-buffer)
8224                 (widen)
8225                 (goto-char (if backward (point-max) (point-min))))))))
8226       (gnus-message 7 ""))
8227     ;; Return whether we found the regexp.
8228     (when (eq found 'found)
8229       (goto-char point)
8230       (gnus-summary-show-thread)
8231       (gnus-summary-goto-subject gnus-current-article)
8232       (gnus-summary-position-point)
8233       t)))
8234
8235 (defun gnus-find-matching-articles (header regexp)
8236   "Return a list of all articles that match REGEXP on HEADER.
8237 This search includes all articles in the current group that Gnus has
8238 fetched headers for, whether they are displayed or not."
8239   (let ((articles nil)
8240         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8241         (case-fold-search t))
8242     (dolist (header gnus-newsgroup-headers)
8243       (when (string-match regexp (funcall func header))
8244         (push (mail-header-number header) articles)))
8245     (nreverse articles)))
8246
8247 (defun gnus-summary-find-matching (header regexp &optional backward unread
8248                                           not-case-fold not-matching)
8249   "Return a list of all articles that match REGEXP on HEADER.
8250 The search stars on the current article and goes forwards unless
8251 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8252 If UNREAD is non-nil, only unread articles will
8253 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8254 in the comparisons. If NOT-MATCHING, return a list of all articles that
8255 not match REGEXP on HEADER."
8256   (let ((case-fold-search (not not-case-fold))
8257         articles d func)
8258     (if (consp header)
8259         (if (eq (car header) 'extra)
8260             (setq func
8261                   `(lambda (h)
8262                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8263                          "")))
8264           (error "%s is an invalid header" header))
8265       (unless (fboundp (intern (concat "mail-header-" header)))
8266         (error "%s is not a valid header" header))
8267       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8268     (dolist (d (if (eq backward 'all)
8269                    gnus-newsgroup-data
8270                  (gnus-data-find-list
8271                   (gnus-summary-article-number)
8272                   (gnus-data-list backward))))
8273       (when (and (or (not unread)       ; We want all articles...
8274                      (gnus-data-unread-p d)) ; Or just unreads.
8275                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8276                  (if not-matching
8277                      (not (string-match
8278                            regexp
8279                            (funcall func (gnus-data-header d))))
8280                    (string-match regexp
8281                                  (funcall func (gnus-data-header d)))))
8282         (push (gnus-data-number d) articles))) ; Success!
8283     (nreverse articles)))
8284
8285 (defun gnus-summary-execute-command (header regexp command &optional backward)
8286   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8287 If HEADER is an empty string (or nil), the match is done on the entire
8288 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8289   (interactive
8290    (list (let ((completion-ignore-case t))
8291            (completing-read
8292             "Header name: "
8293             (mapcar (lambda (header) (list (format "%s" header)))
8294                     (append
8295                      '("Number" "Subject" "From" "Lines" "Date"
8296                        "Message-ID" "Xref" "References" "Body")
8297                      gnus-extra-headers))
8298             nil 'require-match))
8299          (read-string "Regexp: ")
8300          (read-key-sequence "Command: ")
8301          current-prefix-arg))
8302   (when (equal header "Body")
8303     (setq header ""))
8304   ;; Hidden thread subtrees must be searched as well.
8305   (gnus-summary-show-all-threads)
8306   ;; We don't want to change current point nor window configuration.
8307   (save-excursion
8308     (save-window-excursion
8309       (let (gnus-visual
8310             gnus-treat-strip-trailing-blank-lines
8311             gnus-treat-strip-leading-blank-lines
8312             gnus-treat-strip-multiple-blank-lines
8313             gnus-treat-hide-boring-headers
8314             gnus-treat-fold-newsgroups
8315             gnus-article-prepare-hook)
8316         (gnus-message 6 "Executing %s..." (key-description command))
8317         ;; We'd like to execute COMMAND interactively so as to give arguments.
8318         (gnus-execute header regexp
8319                       `(call-interactively ',(key-binding command))
8320                       backward)
8321         (gnus-message 6 "Executing %s...done" (key-description command))))))
8322
8323 (defun gnus-summary-beginning-of-article ()
8324   "Scroll the article back to the beginning."
8325   (interactive)
8326   (gnus-summary-select-article)
8327   (gnus-configure-windows 'article)
8328   (gnus-eval-in-buffer-window gnus-article-buffer
8329     (widen)
8330     (goto-char (point-min))
8331     (when gnus-page-broken
8332       (gnus-narrow-to-page))))
8333
8334 (defun gnus-summary-end-of-article ()
8335   "Scroll to the end of the article."
8336   (interactive)
8337   (gnus-summary-select-article)
8338   (gnus-configure-windows 'article)
8339   (gnus-eval-in-buffer-window gnus-article-buffer
8340     (widen)
8341     (goto-char (point-max))
8342     (recenter -3)
8343     (when gnus-page-broken
8344       (gnus-narrow-to-page))))
8345
8346 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8347   "Truncate to LEN and quote all \"(\"'s in STRING."
8348   (gnus-replace-in-string (if (and len (> (length string) len))
8349                               (substring string 0 len)
8350                             string)
8351                           "[()]" "\\\\\\&"))
8352
8353 (defun gnus-summary-print-article (&optional filename n)
8354   "Generate and print a PostScript image of the N next (mail) articles.
8355
8356 If N is negative, print the N previous articles.  If N is nil and articles
8357 have been marked with the process mark, print these instead.
8358
8359 If the optional first argument FILENAME is nil, send the image to the
8360 printer.  If FILENAME is a string, save the PostScript image in a file with
8361 that name.  If FILENAME is a number, prompt the user for the name of the file
8362 to save in."
8363   (interactive (list (ps-print-preprint current-prefix-arg)))
8364   (dolist (article (gnus-summary-work-articles n))
8365     (gnus-summary-select-article nil nil 'pseudo article)
8366     (gnus-eval-in-buffer-window gnus-article-buffer
8367       (gnus-print-buffer))
8368     (gnus-summary-remove-process-mark article))
8369   (ps-despool filename))
8370
8371 (defun gnus-print-buffer ()
8372   (let ((buffer (generate-new-buffer " *print*")))
8373     (unwind-protect
8374         (progn
8375           (copy-to-buffer buffer (point-min) (point-max))
8376           (set-buffer buffer)
8377           (gnus-article-delete-invisible-text)
8378           (gnus-remove-text-with-property 'gnus-decoration)
8379           (when (gnus-visual-p 'article-highlight 'highlight)
8380             ;; Copy-to-buffer doesn't copy overlay.  So redo
8381             ;; highlight.
8382             (let ((gnus-article-buffer buffer))
8383               (gnus-article-highlight-citation t)
8384               (gnus-article-highlight-signature)))
8385           (let ((ps-left-header
8386                  (list
8387                   (concat "("
8388                           (gnus-summary-print-truncate-and-quote
8389                            (mail-header-subject gnus-current-headers)
8390                            66) ")")
8391                   (concat "("
8392                           (gnus-summary-print-truncate-and-quote
8393                            (mail-header-from gnus-current-headers)
8394                            45) ")")))
8395                 (ps-right-header
8396                  (list
8397                   "/pagenumberstring load"
8398                   (concat "("
8399                           (mail-header-date gnus-current-headers) ")"))))
8400             (gnus-run-hooks 'gnus-ps-print-hook)
8401             (save-excursion
8402               (if window-system
8403                   (ps-spool-buffer-with-faces)
8404                 (ps-spool-buffer)))))
8405       (kill-buffer buffer))))
8406
8407 (defun gnus-summary-show-article (&optional arg)
8408   "Force redisplaying of the current article.
8409 If ARG (the prefix) is a number, show the article with the charset
8410 defined in `gnus-summary-show-article-charset-alist', or the charset
8411 input.
8412 If ARG (the prefix) is non-nil and not a number, show the raw article
8413 without any article massaging functions being run.  Normally, the key strokes
8414 are `C-u g'."
8415   (interactive "P")
8416   (cond
8417    ((numberp arg)
8418     (gnus-summary-show-article t)
8419     (let ((gnus-newsgroup-charset
8420            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8421                (mm-read-coding-system
8422                 "View as charset: " ;; actually it is coding system.
8423                 (save-excursion
8424                   (set-buffer gnus-article-buffer)
8425                   (mm-detect-coding-region (point) (point-max))))))
8426           (gnus-newsgroup-ignored-charsets 'gnus-all))
8427       (gnus-summary-select-article nil 'force)
8428       (let ((deps gnus-newsgroup-dependencies)
8429             head header lines)
8430         (save-excursion
8431           (set-buffer gnus-original-article-buffer)
8432           (save-restriction
8433             (message-narrow-to-head)
8434             (setq head (buffer-string))
8435             (goto-char (point-min))
8436             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8437               (goto-char (point-max))
8438               (widen)
8439               (setq lines (1- (count-lines (point) (point-max))))))
8440           (with-temp-buffer
8441             (insert (format "211 %d Article retrieved.\n"
8442                             (cdr gnus-article-current)))
8443             (insert head)
8444             (if lines (insert (format "Lines: %d\n" lines)))
8445             (insert ".\n")
8446             (let ((nntp-server-buffer (current-buffer)))
8447               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8448         (gnus-data-set-header
8449          (gnus-data-find (cdr gnus-article-current))
8450          header)
8451         (gnus-summary-update-article-line
8452          (cdr gnus-article-current) header)
8453         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8454           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8455    ((not arg)
8456     ;; Select the article the normal way.
8457     (gnus-summary-select-article nil 'force))
8458    (t
8459     ;; We have to require this here to make sure that the following
8460     ;; dynamic binding isn't shadowed by autoloading.
8461     (require 'gnus-async)
8462     (require 'gnus-art)
8463     ;; Bind the article treatment functions to nil.
8464     (let ((gnus-have-all-headers t)
8465           gnus-article-prepare-hook
8466           gnus-article-decode-hook
8467           gnus-display-mime-function
8468           gnus-break-pages)
8469       ;; Destroy any MIME parts.
8470       (when (gnus-buffer-live-p gnus-article-buffer)
8471         (save-excursion
8472           (set-buffer gnus-article-buffer)
8473           (mm-destroy-parts gnus-article-mime-handles)
8474           ;; Set it to nil for safety reason.
8475           (setq gnus-article-mime-handle-alist nil)
8476           (setq gnus-article-mime-handles nil)))
8477       (gnus-summary-select-article nil 'force))))
8478   (gnus-summary-goto-subject gnus-current-article)
8479   (gnus-summary-position-point))
8480
8481 (defun gnus-summary-show-raw-article ()
8482   "Show the raw article without any article massaging functions being run."
8483   (interactive)
8484   (gnus-summary-show-article t))
8485
8486 (defun gnus-summary-verbose-headers (&optional arg)
8487   "Toggle permanent full header display.
8488 If ARG is a positive number, turn header display on.
8489 If ARG is a negative number, turn header display off."
8490   (interactive "P")
8491   (setq gnus-show-all-headers
8492         (cond ((or (not (numberp arg))
8493                    (zerop arg))
8494                (not gnus-show-all-headers))
8495               ((natnump arg)
8496                t)))
8497   (gnus-summary-show-article))
8498
8499 (defun gnus-summary-toggle-header (&optional arg)
8500   "Show the headers if they are hidden, or hide them if they are shown.
8501 If ARG is a positive number, show the entire header.
8502 If ARG is a negative number, hide the unwanted header lines."
8503   (interactive "P")
8504   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8505                      (get-buffer-window gnus-article-buffer t))))
8506     (with-current-buffer gnus-article-buffer
8507       (widen)
8508       (article-narrow-to-head)
8509       (let* ((buffer-read-only nil)
8510              (inhibit-point-motion-hooks t)
8511              (hidden (if (numberp arg)
8512                          (>= arg 0)
8513                        (gnus-article-hidden-text-p 'headers)))
8514              s e)
8515         (delete-region (point-min) (point-max))
8516         (with-current-buffer gnus-original-article-buffer
8517           (goto-char (setq s (point-min)))
8518           (setq e (if (search-forward "\n\n" nil t)
8519                       (1- (point))
8520                     (point-max))))
8521         (insert-buffer-substring gnus-original-article-buffer s e)
8522         (article-decode-encoded-words)
8523         (if hidden
8524             (let ((gnus-treat-hide-headers nil)
8525                   (gnus-treat-hide-boring-headers nil))
8526               (gnus-delete-wash-type 'headers)
8527               (gnus-treat-article 'head))
8528           (gnus-treat-article 'head))
8529         (widen)
8530         (if window
8531             (set-window-start window (goto-char (point-min))))
8532         (setq gnus-page-broken
8533               (when gnus-break-pages
8534                 (gnus-narrow-to-page)
8535                 t))
8536         (gnus-set-mode-line 'article)))))
8537
8538 (defun gnus-summary-show-all-headers ()
8539   "Make all header lines visible."
8540   (interactive)
8541   (gnus-summary-toggle-header 1))
8542
8543 (defun gnus-summary-caesar-message (&optional arg)
8544   "Caesar rotate the current article by 13.
8545 The numerical prefix specifies how many places to rotate each letter
8546 forward."
8547   (interactive "P")
8548   (gnus-summary-select-article)
8549   (let ((mail-header-separator ""))
8550     (gnus-eval-in-buffer-window gnus-article-buffer
8551       (save-restriction
8552         (widen)
8553         (let ((start (window-start))
8554               buffer-read-only)
8555           (message-caesar-buffer-body arg)
8556           (set-window-start (get-buffer-window (current-buffer)) start))))))
8557
8558 (autoload 'unmorse-region "morse"
8559   "Convert morse coded text in region to ordinary ASCII text."
8560   t)
8561
8562 (defun gnus-summary-morse-message (&optional arg)
8563   "Morse decode the current article."
8564   (interactive "P")
8565   (gnus-summary-select-article)
8566   (let ((mail-header-separator ""))
8567     (gnus-eval-in-buffer-window gnus-article-buffer
8568       (save-excursion
8569         (save-restriction
8570           (widen)
8571           (let ((pos (window-start))
8572                 buffer-read-only)
8573             (goto-char (point-min))
8574             (when (message-goto-body)
8575               (gnus-narrow-to-body))
8576             (goto-char (point-min))
8577             (while (re-search-forward "·" (point-max) t)
8578               (replace-match "."))
8579             (unmorse-region (point-min) (point-max))
8580             (widen)
8581             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8582
8583 (defun gnus-summary-stop-page-breaking ()
8584   "Stop page breaking in the current article."
8585   (interactive)
8586   (gnus-summary-select-article)
8587   (gnus-eval-in-buffer-window gnus-article-buffer
8588     (widen)
8589     (when (gnus-visual-p 'page-marker)
8590       (let ((buffer-read-only nil))
8591         (gnus-remove-text-with-property 'gnus-prev)
8592         (gnus-remove-text-with-property 'gnus-next))
8593       (setq gnus-page-broken nil))))
8594
8595 (defun gnus-summary-move-article (&optional n to-newsgroup
8596                                             select-method action)
8597   "Move the current article to a different newsgroup.
8598 If N is a positive number, move the N next articles.
8599 If N is a negative number, move the N previous articles.
8600 If N is nil and any articles have been marked with the process mark,
8601 move those articles instead.
8602 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8603 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8604 re-spool using this method.
8605
8606 When called interactively with TO-NEWSGROUP being nil, the value of
8607 the variable `gnus-move-split-methods' is used for finding a default
8608 for the target newsgroup.
8609
8610 For this function to work, both the current newsgroup and the
8611 newsgroup that you want to move to have to support the `request-move'
8612 and `request-accept' functions.
8613
8614 ACTION can be either `move' (the default), `crosspost' or `copy'."
8615   (interactive "P")
8616   (unless action
8617     (setq action 'move))
8618   ;; Check whether the source group supports the required functions.
8619   (cond ((and (eq action 'move)
8620               (not (gnus-check-backend-function
8621                     'request-move-article gnus-newsgroup-name)))
8622          (error "The current group does not support article moving"))
8623         ((and (eq action 'crosspost)
8624               (not (gnus-check-backend-function
8625                     'request-replace-article gnus-newsgroup-name)))
8626          (error "The current group does not support article editing")))
8627   (let ((articles (gnus-summary-work-articles n))
8628         (prefix (if (gnus-check-backend-function
8629                      'request-move-article gnus-newsgroup-name)
8630                     (gnus-group-real-prefix gnus-newsgroup-name)
8631                   ""))
8632         (names '((move "Move" "Moving")
8633                  (copy "Copy" "Copying")
8634                  (crosspost "Crosspost" "Crossposting")))
8635         (copy-buf (save-excursion
8636                     (nnheader-set-temp-buffer " *copy article*")))
8637         art-group to-method new-xref article to-groups)
8638     (unless (assq action names)
8639       (error "Unknown action %s" action))
8640     ;; Read the newsgroup name.
8641     (when (and (not to-newsgroup)
8642                (not select-method))
8643       (if (and gnus-move-split-methods
8644                (not
8645                 (and (memq gnus-current-article articles)
8646                      (gnus-buffer-live-p gnus-original-article-buffer))))
8647           ;; When `gnus-move-split-methods' is non-nil, we have to
8648           ;; select an article to give `gnus-read-move-group-name' an
8649           ;; opportunity to suggest an appropriate default.  However,
8650           ;; we needn't render or mark the article.
8651           (let ((gnus-display-mime-function nil)
8652                 (gnus-article-prepare-hook nil)
8653                 (gnus-mark-article-hook nil))
8654             (gnus-summary-select-article nil nil nil (car articles))))
8655       (setq to-newsgroup
8656             (gnus-read-move-group-name
8657              (cadr (assq action names))
8658              (symbol-value (intern (format "gnus-current-%s-group" action)))
8659              articles prefix))
8660       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8661     (setq to-method (or select-method
8662                         (gnus-server-to-method
8663                          (gnus-group-method to-newsgroup))))
8664     ;; Check the method we are to move this article to...
8665     (unless (gnus-check-backend-function
8666              'request-accept-article (car to-method))
8667       (error "%s does not support article copying" (car to-method)))
8668     (unless (gnus-check-server to-method)
8669       (error "Can't open server %s" (car to-method)))
8670     (gnus-message 6 "%s to %s: %s..."
8671                   (caddr (assq action names))
8672                   (or (car select-method) to-newsgroup) articles)
8673     (while articles
8674       (setq article (pop articles))
8675       (setq
8676        art-group
8677        (cond
8678         ;; Move the article.
8679         ((eq action 'move)
8680          ;; Remove this article from future suppression.
8681          (gnus-dup-unsuppress-article article)
8682          (gnus-request-move-article
8683           article                       ; Article to move
8684           gnus-newsgroup-name           ; From newsgroup
8685           (nth 1 (gnus-find-method-for-group
8686                   gnus-newsgroup-name)) ; Server
8687           (list 'gnus-request-accept-article
8688                 to-newsgroup (list 'quote select-method)
8689                 (not articles) t)       ; Accept form
8690           (not articles)))              ; Only save nov last time
8691         ;; Copy the article.
8692         ((eq action 'copy)
8693          (save-excursion
8694            (set-buffer copy-buf)
8695            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8696              (gnus-request-accept-article
8697               to-newsgroup select-method (not articles) t))))
8698         ;; Crosspost the article.
8699         ((eq action 'crosspost)
8700          (let ((xref (message-tokenize-header
8701                       (mail-header-xref (gnus-summary-article-header article))
8702                       " ")))
8703            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8704                                   ":" (number-to-string article)))
8705            (unless xref
8706              (setq xref (list (system-name))))
8707            (setq new-xref
8708                  (concat
8709                   (mapconcat 'identity
8710                              (delete "Xref:" (delete new-xref xref))
8711                              " ")
8712                   " " new-xref))
8713            (save-excursion
8714              (set-buffer copy-buf)
8715              ;; First put the article in the destination group.
8716              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8717              (when (consp (setq art-group
8718                                 (gnus-request-accept-article
8719                                  to-newsgroup select-method (not articles))))
8720                (setq new-xref (concat new-xref " " (car art-group)
8721                                       ":"
8722                                       (number-to-string (cdr art-group))))
8723                ;; Now we have the new Xrefs header, so we insert
8724                ;; it and replace the new article.
8725                (nnheader-replace-header "Xref" new-xref)
8726                (gnus-request-replace-article
8727                 (cdr art-group) to-newsgroup (current-buffer))
8728                art-group))))))
8729       (cond
8730        ((not art-group)
8731         (gnus-message 1 "Couldn't %s article %s: %s"
8732                       (cadr (assq action names)) article
8733                       (nnheader-get-report (car to-method))))
8734        ((eq art-group 'junk)
8735         (when (eq action 'move)
8736           (gnus-summary-mark-article article gnus-canceled-mark)
8737           (gnus-message 4 "Deleted article %s" article)))
8738        (t
8739         (let* ((pto-group (gnus-group-prefixed-name
8740                            (car art-group) to-method))
8741                (entry
8742                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8743                (info (nth 2 entry))
8744                (to-group (gnus-info-group info))
8745                to-marks)
8746           ;; Update the group that has been moved to.
8747           (when (and info
8748                      (memq action '(move copy)))
8749             (unless (member to-group to-groups)
8750               (push to-group to-groups))
8751
8752             (unless (memq article gnus-newsgroup-unreads)
8753               (push 'read to-marks)
8754               (gnus-info-set-read
8755                info (gnus-add-to-range (gnus-info-read info)
8756                                        (list (cdr art-group)))))
8757
8758             ;; See whether the article is to be put in the cache.
8759             (let ((marks gnus-article-mark-lists)
8760                   (to-article (cdr art-group)))
8761
8762               ;; Enter the article into the cache in the new group,
8763               ;; if that is required.
8764               (when gnus-use-cache
8765                 (gnus-cache-possibly-enter-article
8766                  to-group to-article
8767                  (memq article gnus-newsgroup-marked)
8768                  (memq article gnus-newsgroup-dormant)
8769                  (memq article gnus-newsgroup-unreads)))
8770
8771               (when gnus-preserve-marks
8772                 ;; Copy any marks over to the new group.
8773                 (when (and (equal to-group gnus-newsgroup-name)
8774                            (not (memq article gnus-newsgroup-unreads)))
8775                   ;; Mark this article as read in this group.
8776                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8777                   (setcdr (gnus-active to-group) to-article)
8778                   (setcdr gnus-newsgroup-active to-article))
8779
8780                 (while marks
8781                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8782                     (when (memq article (symbol-value
8783                                          (intern (format "gnus-newsgroup-%s"
8784                                                          (caar marks)))))
8785                       (push (cdar marks) to-marks)
8786                       ;; If the other group is the same as this group,
8787                       ;; then we have to add the mark to the list.
8788                       (when (equal to-group gnus-newsgroup-name)
8789                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8790                              (cons to-article
8791                                    (symbol-value
8792                                     (intern (format "gnus-newsgroup-%s"
8793                                                     (caar marks)))))))
8794                       ;; Copy the marks to other group.
8795                       (gnus-add-marked-articles
8796                        to-group (cdar marks) (list to-article) info)))
8797                   (setq marks (cdr marks)))
8798
8799                 (gnus-request-set-mark
8800                  to-group (list (list (list to-article) 'add to-marks))))
8801
8802               (gnus-dribble-enter
8803                (concat "(gnus-group-set-info '"
8804                        (gnus-prin1-to-string (gnus-get-info to-group))
8805                        ")"))))
8806
8807           ;; Update the Xref header in this article to point to
8808           ;; the new crossposted article we have just created.
8809           (when (eq action 'crosspost)
8810             (save-excursion
8811               (set-buffer copy-buf)
8812               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8813               (nnheader-replace-header "Xref" new-xref)
8814               (gnus-request-replace-article
8815                article gnus-newsgroup-name (current-buffer)))))
8816
8817         ;;;!!!Why is this necessary?
8818         (set-buffer gnus-summary-buffer)
8819
8820         (gnus-summary-goto-subject article)
8821         (when (eq action 'move)
8822           (gnus-summary-mark-article article gnus-canceled-mark))))
8823       (gnus-summary-remove-process-mark article))
8824     ;; Re-activate all groups that have been moved to.
8825     (save-excursion
8826       (set-buffer gnus-group-buffer)
8827       (let ((gnus-group-marked to-groups))
8828         (gnus-group-get-new-news-this-group nil t)))
8829
8830     (gnus-kill-buffer copy-buf)
8831     (gnus-summary-position-point)
8832     (gnus-set-mode-line 'summary)))
8833
8834 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8835   "Move the current article to a different newsgroup.
8836 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8837 When called interactively, if TO-NEWSGROUP is nil, use the value of
8838 the variable `gnus-move-split-methods' for finding a default target
8839 newsgroup.
8840 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8841 re-spool using this method."
8842   (interactive "P")
8843   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8844
8845 (defun gnus-summary-crosspost-article (&optional n)
8846   "Crosspost the current article to some other group."
8847   (interactive "P")
8848   (gnus-summary-move-article n nil nil 'crosspost))
8849
8850 (defcustom gnus-summary-respool-default-method nil
8851   "Default method type for respooling an article.
8852 If nil, use to the current newsgroup method."
8853   :type 'symbol
8854   :group 'gnus-summary-mail)
8855
8856 (defcustom gnus-summary-display-while-building nil
8857   "If not-nil, show and update the summary buffer as it's being built.
8858 If the value is t, update the buffer after every line is inserted.  If
8859 the value is an integer (N), update the display every N lines."
8860   :group 'gnus-thread
8861   :type '(choice (const :tag "off" nil)
8862                  number
8863                  (const :tag "frequently" t)))
8864
8865 (defun gnus-summary-respool-article (&optional n method)
8866   "Respool the current article.
8867 The article will be squeezed through the mail spooling process again,
8868 which means that it will be put in some mail newsgroup or other
8869 depending on `nnmail-split-methods'.
8870 If N is a positive number, respool the N next articles.
8871 If N is a negative number, respool the N previous articles.
8872 If N is nil and any articles have been marked with the process mark,
8873 respool those articles instead.
8874
8875 Respooling can be done both from mail groups and \"real\" newsgroups.
8876 In the former case, the articles in question will be moved from the
8877 current group into whatever groups they are destined to.  In the
8878 latter case, they will be copied into the relevant groups."
8879   (interactive
8880    (list current-prefix-arg
8881          (let* ((methods (gnus-methods-using 'respool))
8882                 (methname
8883                  (symbol-name (or gnus-summary-respool-default-method
8884                                   (car (gnus-find-method-for-group
8885                                         gnus-newsgroup-name)))))
8886                 (method
8887                  (gnus-completing-read-with-default
8888                   methname "What backend do you want to use when respooling?"
8889                   methods nil t nil 'gnus-mail-method-history))
8890                 ms)
8891            (cond
8892             ((zerop (length (setq ms (gnus-servers-using-backend
8893                                       (intern method)))))
8894              (list (intern method) ""))
8895             ((= 1 (length ms))
8896              (car ms))
8897             (t
8898              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8899                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8900                            ms-alist))))))))
8901   (unless method
8902     (error "No method given for respooling"))
8903   (if (assoc (symbol-name
8904               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8905              (gnus-methods-using 'respool))
8906       (gnus-summary-move-article n nil method)
8907     (gnus-summary-copy-article n nil method)))
8908
8909 (defun gnus-summary-import-article (file &optional edit)
8910   "Import an arbitrary file into a mail newsgroup."
8911   (interactive "fImport file: \nP")
8912   (let ((group gnus-newsgroup-name)
8913         (now (current-time))
8914         atts lines group-art)
8915     (unless (gnus-check-backend-function 'request-accept-article group)
8916       (error "%s does not support article importing" group))
8917     (or (file-readable-p file)
8918         (not (file-regular-p file))
8919         (error "Can't read %s" file))
8920     (save-excursion
8921       (set-buffer (gnus-get-buffer-create " *import file*"))
8922       (erase-buffer)
8923       (nnheader-insert-file-contents file)
8924       (goto-char (point-min))
8925       (if (nnheader-article-p)
8926           (save-restriction
8927             (goto-char (point-min))
8928             (search-forward "\n\n" nil t)
8929             (narrow-to-region (point-min) (1- (point)))
8930             (goto-char (point-min))
8931             (unless (re-search-forward "^date:" nil t)
8932               (goto-char (point-max))
8933               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8934        ;; This doesn't look like an article, so we fudge some headers.
8935         (setq atts (file-attributes file)
8936               lines (count-lines (point-min) (point-max)))
8937         (insert "From: " (read-string "From: ") "\n"
8938                 "Subject: " (read-string "Subject: ") "\n"
8939                 "Date: " (message-make-date (nth 5 atts)) "\n"
8940                 "Message-ID: " (message-make-message-id) "\n"
8941                 "Lines: " (int-to-string lines) "\n"
8942                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8943       (setq group-art (gnus-request-accept-article group nil t))
8944       (kill-buffer (current-buffer)))
8945     (setq gnus-newsgroup-active (gnus-activate-group group))
8946     (forward-line 1)
8947     (gnus-summary-goto-article (cdr group-art) nil t)
8948     (when edit
8949       (gnus-summary-edit-article))))
8950
8951 (defun gnus-summary-create-article ()
8952   "Create an article in a mail newsgroup."
8953   (interactive)
8954   (let ((group gnus-newsgroup-name)
8955         (now (current-time))
8956         group-art)
8957     (unless (gnus-check-backend-function 'request-accept-article group)
8958       (error "%s does not support article importing" group))
8959     (save-excursion
8960       (set-buffer (gnus-get-buffer-create " *import file*"))
8961       (erase-buffer)
8962       (goto-char (point-min))
8963       ;; This doesn't look like an article, so we fudge some headers.
8964       (insert "From: " (read-string "From: ") "\n"
8965               "Subject: " (read-string "Subject: ") "\n"
8966               "Date: " (message-make-date now) "\n"
8967               "Message-ID: " (message-make-message-id) "\n")
8968       (setq group-art (gnus-request-accept-article group nil t))
8969       (kill-buffer (current-buffer)))
8970     (setq gnus-newsgroup-active (gnus-activate-group group))
8971     (forward-line 1)
8972     (gnus-summary-goto-article (cdr group-art) nil t)
8973     (gnus-summary-edit-article)))
8974
8975 (defun gnus-summary-article-posted-p ()
8976   "Say whether the current (mail) article is available from news as well.
8977 This will be the case if the article has both been mailed and posted."
8978   (interactive)
8979   (let ((id (mail-header-references (gnus-summary-article-header)))
8980         (gnus-override-method (car (gnus-refer-article-methods))))
8981     (if (gnus-request-head id "")
8982         (gnus-message 2 "The current message was found on %s"
8983                       gnus-override-method)
8984       (gnus-message 2 "The current message couldn't be found on %s"
8985                     gnus-override-method)
8986       nil)))
8987
8988 (defun gnus-summary-expire-articles (&optional now)
8989   "Expire all articles that are marked as expirable in the current group."
8990   (interactive)
8991   (when (gnus-check-backend-function
8992          'request-expire-articles gnus-newsgroup-name)
8993     ;; This backend supports expiry.
8994     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8995            (expirable (if total
8996                           (progn
8997                             ;; We need to update the info for
8998                             ;; this group for `gnus-list-of-read-articles'
8999                             ;; to give us the right answer.
9000                             (gnus-run-hooks 'gnus-exit-group-hook)
9001                             (gnus-summary-update-info)
9002                             (gnus-list-of-read-articles gnus-newsgroup-name))
9003                         (setq gnus-newsgroup-expirable
9004                               (sort gnus-newsgroup-expirable '<))))
9005            (expiry-wait (if now 'immediate
9006                           (gnus-group-find-parameter
9007                            gnus-newsgroup-name 'expiry-wait)))
9008            (nnmail-expiry-target
9009             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9010                 nnmail-expiry-target))
9011            es)
9012       (when expirable
9013         ;; There are expirable articles in this group, so we run them
9014         ;; through the expiry process.
9015         (gnus-message 6 "Expiring articles...")
9016         (unless (gnus-check-group gnus-newsgroup-name)
9017           (error "Can't open server for %s" gnus-newsgroup-name))
9018         ;; The list of articles that weren't expired is returned.
9019         (save-excursion
9020           (if expiry-wait
9021               (let ((nnmail-expiry-wait-function nil)
9022                     (nnmail-expiry-wait expiry-wait))
9023                 (setq es (gnus-request-expire-articles
9024                           expirable gnus-newsgroup-name)))
9025             (setq es (gnus-request-expire-articles
9026                       expirable gnus-newsgroup-name)))
9027           (unless total
9028             (setq gnus-newsgroup-expirable es))
9029           ;; We go through the old list of expirable, and mark all
9030           ;; really expired articles as nonexistent.
9031           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9032             (let ((gnus-use-cache nil))
9033               (dolist (article expirable)
9034                 (when (and (not (memq article es))
9035                            (gnus-data-find article))
9036                   (gnus-summary-mark-article article gnus-canceled-mark))))))
9037         (gnus-message 6 "Expiring articles...done")))))
9038
9039 (defun gnus-summary-expire-articles-now ()
9040   "Expunge all expirable articles in the current group.
9041 This means that *all* articles that are marked as expirable will be
9042 deleted forever, right now."
9043   (interactive)
9044   (or gnus-expert-user
9045       (gnus-yes-or-no-p
9046        "Are you really, really, really sure you want to delete all these messages? ")
9047       (error "Phew!"))
9048   (gnus-summary-expire-articles t))
9049
9050 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9051 (defun gnus-summary-delete-article (&optional n)
9052   "Delete the N next (mail) articles.
9053 This command actually deletes articles.  This is not a marking
9054 command.  The article will disappear forever from your life, never to
9055 return.
9056 If N is negative, delete backwards.
9057 If N is nil and articles have been marked with the process mark,
9058 delete these instead."
9059   (interactive "P")
9060   (unless (gnus-check-backend-function 'request-expire-articles
9061                                        gnus-newsgroup-name)
9062     (error "The current newsgroup does not support article deletion"))
9063   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9064     (error "Couldn't open server"))
9065   ;; Compute the list of articles to delete.
9066   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9067         (nnmail-expiry-target 'delete)
9068         not-deleted)
9069     (if (and gnus-novice-user
9070              (not (gnus-yes-or-no-p
9071                    (format "Do you really want to delete %s forever? "
9072                            (if (> (length articles) 1)
9073                                (format "these %s articles" (length articles))
9074                              "this article")))))
9075         ()
9076       ;; Delete the articles.
9077       (setq not-deleted (gnus-request-expire-articles
9078                          articles gnus-newsgroup-name 'force))
9079       (while articles
9080         (gnus-summary-remove-process-mark (car articles))
9081         ;; The backend might not have been able to delete the article
9082         ;; after all.
9083         (unless (memq (car articles) not-deleted)
9084           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9085         (setq articles (cdr articles)))
9086       (when not-deleted
9087         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9088     (gnus-summary-position-point)
9089     (gnus-set-mode-line 'summary)
9090     not-deleted))
9091
9092 (defun gnus-summary-edit-article (&optional arg)
9093   "Edit the current article.
9094 This will have permanent effect only in mail groups.
9095 If ARG is nil, edit the decoded articles.
9096 If ARG is 1, edit the raw articles.
9097 If ARG is 2, edit the raw articles even in read-only groups.
9098 If ARG is 3, edit the articles with the current handles.
9099 Otherwise, allow editing of articles even in read-only
9100 groups."
9101   (interactive "P")
9102   (let (force raw current-handles)
9103     (cond
9104      ((null arg))
9105      ((eq arg 1)
9106       (setq raw t))
9107      ((eq arg 2)
9108       (setq raw t
9109             force t))
9110      ((eq arg 3)
9111       (setq current-handles
9112             (and (gnus-buffer-live-p gnus-article-buffer)
9113                  (with-current-buffer gnus-article-buffer
9114                    (prog1
9115                        gnus-article-mime-handles
9116                      (setq gnus-article-mime-handles nil))))))
9117      (t
9118       (setq force t)))
9119     (when (and raw (not force)
9120                (member gnus-newsgroup-name '("nndraft:delayed"
9121                                              "nndraft:drafts"
9122                                              "nndraft:queue")))
9123       (error "Can't edit the raw article in group %s"
9124              gnus-newsgroup-name))
9125     (save-excursion
9126       (set-buffer gnus-summary-buffer)
9127       (let ((mail-parse-charset gnus-newsgroup-charset)
9128             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9129         (gnus-set-global-variables)
9130         (when (and (not force)
9131                    (gnus-group-read-only-p))
9132           (error "The current newsgroup does not support article editing"))
9133         (gnus-summary-show-article t)
9134         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9135           (with-current-buffer gnus-article-buffer
9136             (mm-enable-multibyte)))
9137         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9138             (setq raw t))
9139         (gnus-article-edit-article
9140          (if raw 'ignore
9141            `(lambda ()
9142               (let ((mbl mml-buffer-list))
9143                 (setq mml-buffer-list nil)
9144                 (mime-to-mml ,'current-handles)
9145                 (let ((mbl1 mml-buffer-list))
9146                   (setq mml-buffer-list mbl)
9147                   (set (make-local-variable 'mml-buffer-list) mbl1))
9148                 (make-local-hook 'kill-buffer-hook)
9149                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9150          `(lambda (no-highlight)
9151             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9152                   (message-options message-options)
9153                   (message-options-set-recipient)
9154                   (mail-parse-ignored-charsets
9155                    ',gnus-newsgroup-ignored-charsets))
9156               ,(if (not raw) '(progn
9157                                 (mml-to-mime)
9158                                 (mml-destroy-buffers)
9159                                 (remove-hook 'kill-buffer-hook
9160                                              'mml-destroy-buffers t)
9161                                 (kill-local-variable 'mml-buffer-list)))
9162               (gnus-summary-edit-article-done
9163                ,(or (mail-header-references gnus-current-headers) "")
9164                ,(gnus-group-read-only-p)
9165                ,gnus-summary-buffer no-highlight))))))))
9166
9167 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9168
9169 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9170                                                  no-highlight)
9171   "Make edits to the current article permanent."
9172   (interactive)
9173   (save-excursion
9174    ;; The buffer restriction contains the entire article if it exists.
9175     (when (article-goto-body)
9176       (let ((lines (count-lines (point) (point-max)))
9177             (length (- (point-max) (point)))
9178             (case-fold-search t)
9179             (body (copy-marker (point))))
9180         (goto-char (point-min))
9181         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9182           (delete-region (match-beginning 1) (match-end 1))
9183           (insert (number-to-string length)))
9184         (goto-char (point-min))
9185         (when (re-search-forward
9186                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9187           (delete-region (match-beginning 1) (match-end 1))
9188           (insert (number-to-string length)))
9189         (goto-char (point-min))
9190         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9191           (delete-region (match-beginning 1) (match-end 1))
9192           (insert (number-to-string lines))))))
9193   ;; Replace the article.
9194   (let ((buf (current-buffer)))
9195     (with-temp-buffer
9196       (insert-buffer-substring buf)
9197
9198       (if (and (not read-only)
9199                (not (gnus-request-replace-article
9200                      (cdr gnus-article-current) (car gnus-article-current)
9201                      (current-buffer) t)))
9202           (error "Couldn't replace article")
9203         ;; Update the summary buffer.
9204         (if (and references
9205                  (equal (message-tokenize-header references " ")
9206                         (message-tokenize-header
9207                          (or (message-fetch-field "references") "") " ")))
9208             ;; We only have to update this line.
9209             (save-excursion
9210               (save-restriction
9211                 (message-narrow-to-head)
9212                 (let ((head (buffer-string))
9213                       header)
9214                   (with-temp-buffer
9215                     (insert (format "211 %d Article retrieved.\n"
9216                                     (cdr gnus-article-current)))
9217                     (insert head)
9218                     (insert ".\n")
9219                     (let ((nntp-server-buffer (current-buffer)))
9220                       (setq header (car (gnus-get-newsgroup-headers
9221                                          nil t))))
9222                     (save-excursion
9223                       (set-buffer gnus-summary-buffer)
9224                       (gnus-data-set-header
9225                        (gnus-data-find (cdr gnus-article-current))
9226                        header)
9227                       (gnus-summary-update-article-line
9228                        (cdr gnus-article-current) header)
9229                       (if (gnus-summary-goto-subject
9230                            (cdr gnus-article-current) nil t)
9231                           (gnus-summary-update-secondary-mark
9232                            (cdr gnus-article-current))))))))
9233           ;; Update threads.
9234           (set-buffer (or buffer gnus-summary-buffer))
9235           (gnus-summary-update-article (cdr gnus-article-current))
9236           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9237               (gnus-summary-update-secondary-mark
9238                (cdr gnus-article-current))))
9239         ;; Prettify the article buffer again.
9240         (unless no-highlight
9241           (save-excursion
9242             (set-buffer gnus-article-buffer)
9243             ;;;!!! Fix this -- article should be rehighlighted.
9244             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9245             (set-buffer gnus-original-article-buffer)
9246             (gnus-request-article
9247              (cdr gnus-article-current)
9248              (car gnus-article-current) (current-buffer))))
9249         ;; Prettify the summary buffer line.
9250         (when (gnus-visual-p 'summary-highlight 'highlight)
9251           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9252
9253 (defun gnus-summary-edit-wash (key)
9254   "Perform editing command KEY in the article buffer."
9255   (interactive
9256    (list
9257     (progn
9258       (message "%s" (concat (this-command-keys) "- "))
9259       (read-char))))
9260   (message "")
9261   (gnus-summary-edit-article)
9262   (execute-kbd-macro (concat (this-command-keys) key))
9263   (gnus-article-edit-done))
9264
9265 ;;; Respooling
9266
9267 (defun gnus-summary-respool-query (&optional silent trace)
9268   "Query where the respool algorithm would put this article."
9269   (interactive)
9270   (let (gnus-mark-article-hook)
9271     (gnus-summary-select-article)
9272     (save-excursion
9273       (set-buffer gnus-original-article-buffer)
9274       (save-restriction
9275         (message-narrow-to-head)
9276         (let ((groups (nnmail-article-group 'identity trace)))
9277           (unless silent
9278             (if groups
9279                 (message "This message would go to %s"
9280                          (mapconcat 'car groups ", "))
9281               (message "This message would go to no groups"))
9282             groups))))))
9283
9284 (defun gnus-summary-respool-trace ()
9285   "Trace where the respool algorithm would put this article.
9286 Display a buffer showing all fancy splitting patterns which matched."
9287   (interactive)
9288   (gnus-summary-respool-query nil t))
9289
9290 ;; Summary marking commands.
9291
9292 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9293   "Mark articles which has the same subject as read, and then select the next.
9294 If UNMARK is positive, remove any kind of mark.
9295 If UNMARK is negative, tick articles."
9296   (interactive "P")
9297   (when unmark
9298     (setq unmark (prefix-numeric-value unmark)))
9299   (let ((count
9300          (gnus-summary-mark-same-subject
9301           (gnus-summary-article-subject) unmark)))
9302     ;; Select next unread article.  If auto-select-same mode, should
9303     ;; select the first unread article.
9304     (gnus-summary-next-article t (and gnus-auto-select-same
9305                                       (gnus-summary-article-subject)))
9306     (gnus-message 7 "%d article%s marked as %s"
9307                   count (if (= count 1) " is" "s are")
9308                   (if unmark "unread" "read"))))
9309
9310 (defun gnus-summary-kill-same-subject (&optional unmark)
9311   "Mark articles which has the same subject as read.
9312 If UNMARK is positive, remove any kind of mark.
9313 If UNMARK is negative, tick articles."
9314   (interactive "P")
9315   (when unmark
9316     (setq unmark (prefix-numeric-value unmark)))
9317   (let ((count
9318          (gnus-summary-mark-same-subject
9319           (gnus-summary-article-subject) unmark)))
9320     ;; If marked as read, go to next unread subject.
9321     (when (null unmark)
9322       ;; Go to next unread subject.
9323       (gnus-summary-next-subject 1 t))
9324     (gnus-message 7 "%d articles are marked as %s"
9325                   count (if unmark "unread" "read"))))
9326
9327 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9328   "Mark articles with same SUBJECT as read, and return marked number.
9329 If optional argument UNMARK is positive, remove any kinds of marks.
9330 If optional argument UNMARK is negative, mark articles as unread instead."
9331   (let ((count 1))
9332     (save-excursion
9333       (cond
9334        ((null unmark)                   ; Mark as read.
9335         (while (and
9336                 (progn
9337                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9338                   (gnus-summary-show-thread) t)
9339                 (gnus-summary-find-subject subject))
9340           (setq count (1+ count))))
9341        ((> unmark 0)                    ; Tick.
9342         (while (and
9343                 (progn
9344                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9345                   (gnus-summary-show-thread) t)
9346                 (gnus-summary-find-subject subject))
9347           (setq count (1+ count))))
9348        (t                               ; Mark as unread.
9349         (while (and
9350                 (progn
9351                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9352                   (gnus-summary-show-thread) t)
9353                 (gnus-summary-find-subject subject))
9354           (setq count (1+ count)))))
9355       (gnus-set-mode-line 'summary)
9356       ;; Return the number of marked articles.
9357       count)))
9358
9359 (defun gnus-summary-mark-as-processable (n &optional unmark)
9360   "Set the process mark on the next N articles.
9361 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9362 the process mark instead.  The difference between N and the actual
9363 number of articles marked is returned."
9364   (interactive "P")
9365   (if (and (null n) (gnus-region-active-p))
9366       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9367     (setq n (prefix-numeric-value n))
9368     (let ((backward (< n 0))
9369           (n (abs n)))
9370       (while (and
9371               (> n 0)
9372               (if unmark
9373                   (gnus-summary-remove-process-mark
9374                    (gnus-summary-article-number))
9375                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9376               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9377         (setq n (1- n)))
9378       (when (/= 0 n)
9379         (gnus-message 7 "No more articles"))
9380       (gnus-summary-recenter)
9381       (gnus-summary-position-point)
9382       n)))
9383
9384 (defun gnus-summary-unmark-as-processable (n)
9385   "Remove the process mark from the next N articles.
9386 If N is negative, unmark backward instead.  The difference between N and
9387 the actual number of articles unmarked is returned."
9388   (interactive "P")
9389   (gnus-summary-mark-as-processable n t))
9390
9391 (defun gnus-summary-unmark-all-processable ()
9392   "Remove the process mark from all articles."
9393   (interactive)
9394   (save-excursion
9395     (while gnus-newsgroup-processable
9396       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9397   (gnus-summary-position-point))
9398
9399 (defun gnus-summary-add-mark (article type)
9400   "Mark ARTICLE with a mark of TYPE."
9401   (let ((vtype (car (assq type gnus-article-mark-lists)))
9402         var)
9403     (if (not vtype)
9404         (error "No such mark type: %s" type)
9405       (setq var (intern (format "gnus-newsgroup-%s" type)))
9406       (set var (cons article (symbol-value var)))
9407       (if (memq type '(processable cached replied forwarded recent saved))
9408           (gnus-summary-update-secondary-mark article)
9409         ;;; !!! This is bogus.  We should find out what primary
9410         ;;; !!! mark we want to set.
9411         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9412
9413 (defun gnus-summary-mark-as-expirable (n)
9414   "Mark N articles forward as expirable.
9415 If N is negative, mark backward instead.  The difference between N and
9416 the actual number of articles marked is returned."
9417   (interactive "p")
9418   (gnus-summary-mark-forward n gnus-expirable-mark))
9419
9420 (defun gnus-summary-mark-as-spam (n)
9421   "Mark N articles forward as spam.
9422 If N is negative, mark backward instead.  The difference between N and
9423 the actual number of articles marked is returned."
9424   (interactive "p")
9425   (gnus-summary-mark-forward n gnus-spam-mark))
9426
9427 (defun gnus-summary-mark-article-as-replied (article)
9428   "Mark ARTICLE as replied to and update the summary line.
9429 ARTICLE can also be a list of articles."
9430   (interactive (list (gnus-summary-article-number)))
9431   (let ((articles (if (listp article) article (list article))))
9432     (dolist (article articles)
9433       (unless (numberp article)
9434         (error "%s is not a number" article))
9435       (push article gnus-newsgroup-replied)
9436       (let ((buffer-read-only nil))
9437         (when (gnus-summary-goto-subject article nil t)
9438           (gnus-summary-update-secondary-mark article))))))
9439
9440 (defun gnus-summary-mark-article-as-forwarded (article)
9441   "Mark ARTICLE as forwarded and update the summary line.
9442 ARTICLE can also be a list of articles."
9443   (let ((articles (if (listp article) article (list article))))
9444     (dolist (article articles)
9445       (push article gnus-newsgroup-forwarded)
9446       (let ((buffer-read-only nil))
9447         (when (gnus-summary-goto-subject article nil t)
9448           (gnus-summary-update-secondary-mark article))))))
9449
9450 (defun gnus-summary-set-bookmark (article)
9451   "Set a bookmark in current article."
9452   (interactive (list (gnus-summary-article-number)))
9453   (when (or (not (get-buffer gnus-article-buffer))
9454             (not gnus-current-article)
9455             (not gnus-article-current)
9456             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9457     (error "No current article selected"))
9458   ;; Remove old bookmark, if one exists.
9459   (gnus-pull article gnus-newsgroup-bookmarks)
9460   ;; Set the new bookmark, which is on the form
9461   ;; (article-number . line-number-in-body).
9462   (push
9463    (cons article
9464          (save-excursion
9465            (set-buffer gnus-article-buffer)
9466            (count-lines
9467             (min (point)
9468                  (save-excursion
9469                    (article-goto-body)
9470                    (point)))
9471             (point))))
9472    gnus-newsgroup-bookmarks)
9473   (gnus-message 6 "A bookmark has been added to the current article."))
9474
9475 (defun gnus-summary-remove-bookmark (article)
9476   "Remove the bookmark from the current article."
9477   (interactive (list (gnus-summary-article-number)))
9478   ;; Remove old bookmark, if one exists.
9479   (if (not (assq article gnus-newsgroup-bookmarks))
9480       (gnus-message 6 "No bookmark in current article.")
9481     (gnus-pull article gnus-newsgroup-bookmarks)
9482     (gnus-message 6 "Removed bookmark.")))
9483
9484 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9485 (defun gnus-summary-mark-as-dormant (n)
9486   "Mark N articles forward as dormant.
9487 If N is negative, mark backward instead.  The difference between N and
9488 the actual number of articles marked is returned."
9489   (interactive "p")
9490   (gnus-summary-mark-forward n gnus-dormant-mark))
9491
9492 (defun gnus-summary-set-process-mark (article)
9493   "Set the process mark on ARTICLE and update the summary line."
9494   (setq gnus-newsgroup-processable
9495         (cons article
9496               (delq article gnus-newsgroup-processable)))
9497   (when (gnus-summary-goto-subject article)
9498     (gnus-summary-show-thread)
9499     (gnus-summary-goto-subject article)
9500     (gnus-summary-update-secondary-mark article)))
9501
9502 (defun gnus-summary-remove-process-mark (article)
9503   "Remove the process mark from ARTICLE and update the summary line."
9504   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9505   (when (gnus-summary-goto-subject article)
9506     (gnus-summary-show-thread)
9507     (gnus-summary-goto-subject article)
9508     (gnus-summary-update-secondary-mark article)))
9509
9510 (defun gnus-summary-set-saved-mark (article)
9511   "Set the process mark on ARTICLE and update the summary line."
9512   (push article gnus-newsgroup-saved)
9513   (when (gnus-summary-goto-subject article)
9514     (gnus-summary-update-secondary-mark article)))
9515
9516 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9517   "Mark N articles as read forwards.
9518 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9519 The difference between N and the actual number of articles marked is
9520 returned.
9521 If NO-EXPIRE, auto-expiry will be inhibited."
9522   (interactive "p")
9523   (gnus-summary-show-thread)
9524   (let ((backward (< n 0))
9525         (gnus-summary-goto-unread
9526          (and gnus-summary-goto-unread
9527               (not (eq gnus-summary-goto-unread 'never))
9528               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9529                                     gnus-ticked-mark gnus-dormant-mark)))))
9530         (n (abs n))
9531         (mark (or mark gnus-del-mark)))
9532     (while (and (> n 0)
9533                 (gnus-summary-mark-article nil mark no-expire)
9534                 (zerop (gnus-summary-next-subject
9535                         (if backward -1 1)
9536                         (and gnus-summary-goto-unread
9537                              (not (eq gnus-summary-goto-unread 'never)))
9538                         t)))
9539       (setq n (1- n)))
9540     (when (/= 0 n)
9541       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9542     (gnus-summary-recenter)
9543     (gnus-summary-position-point)
9544     (gnus-set-mode-line 'summary)
9545     n))
9546
9547 (defun gnus-summary-mark-article-as-read (mark)
9548   "Mark the current article quickly as read with MARK."
9549   (let ((article (gnus-summary-article-number)))
9550     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9551     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9552     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9553     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9554     (push (cons article mark) gnus-newsgroup-reads)
9555     ;; Possibly remove from cache, if that is used.
9556     (when gnus-use-cache
9557       (gnus-cache-enter-remove-article article))
9558     ;; Allow the backend to change the mark.
9559     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9560     ;; Check for auto-expiry.
9561     (when (and gnus-newsgroup-auto-expire
9562                (memq mark gnus-auto-expirable-marks))
9563       (setq mark gnus-expirable-mark)
9564       ;; Let the backend know about the mark change.
9565       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9566       (push article gnus-newsgroup-expirable))
9567     ;; Set the mark in the buffer.
9568     (gnus-summary-update-mark mark 'unread)
9569     t))
9570
9571 (defun gnus-summary-mark-article-as-unread (mark)
9572   "Mark the current article quickly as unread with MARK."
9573   (let* ((article (gnus-summary-article-number))
9574          (old-mark (gnus-summary-article-mark article)))
9575     ;; Allow the backend to change the mark.
9576     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9577     (if (eq mark old-mark)
9578         t
9579       (if (<= article 0)
9580           (progn
9581             (gnus-error 1 "Can't mark negative article numbers")
9582             nil)
9583         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9584         (setq gnus-newsgroup-spam-marked
9585               (delq article gnus-newsgroup-spam-marked))
9586         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9587         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9588         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9589         (cond ((= mark gnus-ticked-mark)
9590                (setq gnus-newsgroup-marked
9591                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9592                                               article)))
9593               ((= mark gnus-spam-mark)
9594                (setq gnus-newsgroup-spam-marked
9595                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9596                                               article)))
9597               ((= mark gnus-dormant-mark)
9598                (setq gnus-newsgroup-dormant
9599                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9600                                               article)))
9601               (t
9602                (setq gnus-newsgroup-unreads
9603                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9604                                               article))))
9605         (gnus-pull article gnus-newsgroup-reads)
9606
9607         ;; See whether the article is to be put in the cache.
9608         (and gnus-use-cache
9609              (vectorp (gnus-summary-article-header article))
9610              (save-excursion
9611                (gnus-cache-possibly-enter-article
9612                 gnus-newsgroup-name article
9613                 (= mark gnus-ticked-mark)
9614                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9615
9616         ;; Fix the mark.
9617         (gnus-summary-update-mark mark 'unread)
9618         t))))
9619
9620 (defun gnus-summary-mark-article (&optional article mark no-expire)
9621   "Mark ARTICLE with MARK.  MARK can be any character.
9622 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9623 `??' (dormant) and `?E' (expirable).
9624 If MARK is nil, then the default character `?r' is used.
9625 If ARTICLE is nil, then the article on the current line will be
9626 marked.
9627 If NO-EXPIRE, auto-expiry will be inhibited."
9628   ;; The mark might be a string.
9629   (when (stringp mark)
9630     (setq mark (aref mark 0)))
9631   ;; If no mark is given, then we check auto-expiring.
9632   (when (null mark)
9633     (setq mark gnus-del-mark))
9634   (when (and (not no-expire)
9635              gnus-newsgroup-auto-expire
9636              (memq mark gnus-auto-expirable-marks))
9637     (setq mark gnus-expirable-mark))
9638   (let ((article (or article (gnus-summary-article-number)))
9639         (old-mark (gnus-summary-article-mark article)))
9640     ;; Allow the backend to change the mark.
9641     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9642     (if (eq mark old-mark)
9643         t
9644       (unless article
9645         (error "No article on current line"))
9646       (if (not (if (or (= mark gnus-unread-mark)
9647                        (= mark gnus-ticked-mark)
9648                        (= mark gnus-spam-mark)
9649                        (= mark gnus-dormant-mark))
9650                    (gnus-mark-article-as-unread article mark)
9651                  (gnus-mark-article-as-read article mark)))
9652           t
9653         ;; See whether the article is to be put in the cache.
9654         (and gnus-use-cache
9655              (not (= mark gnus-canceled-mark))
9656              (vectorp (gnus-summary-article-header article))
9657              (save-excursion
9658                (gnus-cache-possibly-enter-article
9659                 gnus-newsgroup-name article
9660                 (= mark gnus-ticked-mark)
9661                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9662
9663         (when (gnus-summary-goto-subject article nil t)
9664           (let ((buffer-read-only nil))
9665             (gnus-summary-show-thread)
9666             ;; Fix the mark.
9667             (gnus-summary-update-mark mark 'unread)
9668             t))))))
9669
9670 (defun gnus-summary-update-secondary-mark (article)
9671   "Update the secondary (read, process, cache) mark."
9672   (gnus-summary-update-mark
9673    (cond ((memq article gnus-newsgroup-processable)
9674           gnus-process-mark)
9675          ((memq article gnus-newsgroup-cached)
9676           gnus-cached-mark)
9677          ((memq article gnus-newsgroup-replied)
9678           gnus-replied-mark)
9679          ((memq article gnus-newsgroup-forwarded)
9680           gnus-forwarded-mark)
9681          ((memq article gnus-newsgroup-saved)
9682           gnus-saved-mark)
9683          ((memq article gnus-newsgroup-recent)
9684           gnus-recent-mark)
9685          ((memq article gnus-newsgroup-unseen)
9686           gnus-unseen-mark)
9687          (t gnus-no-mark))
9688    'replied)
9689   (when (gnus-visual-p 'summary-highlight 'highlight)
9690     (gnus-run-hooks 'gnus-summary-update-hook))
9691   t)
9692
9693 (defun gnus-summary-update-download-mark (article)
9694   "Update the secondary (read, process, cache) mark."
9695   (gnus-summary-update-mark
9696    (cond ((memq article gnus-newsgroup-undownloaded) 
9697           gnus-undownloaded-mark)
9698          (gnus-newsgroup-agentized
9699           gnus-downloaded-mark)
9700          (t
9701           gnus-no-mark))
9702    'download)
9703   (gnus-summary-update-line t)
9704   t)
9705
9706 (defun gnus-summary-update-mark (mark type)
9707   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9708         (buffer-read-only nil))
9709     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9710     (when forward
9711       (when (looking-at "\r")
9712         (incf forward))
9713       (when (<= (+ forward (point)) (point-max))
9714         ;; Go to the right position on the line.
9715         (goto-char (+ forward (point)))
9716         ;; Replace the old mark with the new mark.
9717         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9718         ;; Optionally update the marks by some user rule.
9719         (when (eq type 'unread)
9720           (gnus-data-set-mark
9721            (gnus-data-find (gnus-summary-article-number)) mark)
9722           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9723
9724 (defun gnus-mark-article-as-read (article &optional mark)
9725   "Enter ARTICLE in the pertinent lists and remove it from others."
9726   ;; Make the article expirable.
9727   (let ((mark (or mark gnus-del-mark)))
9728     (setq gnus-newsgroup-expirable
9729           (if (= mark gnus-expirable-mark)
9730               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9731             (delq article gnus-newsgroup-expirable)))
9732     ;; Remove from unread and marked lists.
9733     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9734     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9735     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9736     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9737     (push (cons article mark) gnus-newsgroup-reads)
9738     ;; Possibly remove from cache, if that is used.
9739     (when gnus-use-cache
9740       (gnus-cache-enter-remove-article article))
9741     t))
9742
9743 (defun gnus-mark-article-as-unread (article &optional mark)
9744   "Enter ARTICLE in the pertinent lists and remove it from others."
9745   (let ((mark (or mark gnus-ticked-mark)))
9746     (if (<= article 0)
9747         (progn
9748           (gnus-error 1 "Can't mark negative article numbers")
9749           nil)
9750       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9751             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9752             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9753             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9754             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9755
9756       ;; Unsuppress duplicates?
9757       (when gnus-suppress-duplicates
9758         (gnus-dup-unsuppress-article article))
9759
9760       (cond ((= mark gnus-ticked-mark)
9761              (setq gnus-newsgroup-marked
9762                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9763             ((= mark gnus-spam-mark)
9764              (setq gnus-newsgroup-spam-marked
9765                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9766                                             article)))
9767             ((= mark gnus-dormant-mark)
9768              (setq gnus-newsgroup-dormant
9769                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9770             (t
9771              (setq gnus-newsgroup-unreads
9772                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9773       (gnus-pull article gnus-newsgroup-reads)
9774       t)))
9775
9776 (defalias 'gnus-summary-mark-as-unread-forward
9777   'gnus-summary-tick-article-forward)
9778 (make-obsolete 'gnus-summary-mark-as-unread-forward
9779                'gnus-summary-tick-article-forward)
9780 (defun gnus-summary-tick-article-forward (n)
9781   "Tick N articles forwards.
9782 If N is negative, tick backwards instead.
9783 The difference between N and the number of articles ticked is returned."
9784   (interactive "p")
9785   (gnus-summary-mark-forward n gnus-ticked-mark))
9786
9787 (defalias 'gnus-summary-mark-as-unread-backward
9788   'gnus-summary-tick-article-backward)
9789 (make-obsolete 'gnus-summary-mark-as-unread-backward
9790                'gnus-summary-tick-article-backward)
9791 (defun gnus-summary-tick-article-backward (n)
9792   "Tick N articles backwards.
9793 The difference between N and the number of articles ticked is returned."
9794   (interactive "p")
9795   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9796
9797 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9798 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9799 (defun gnus-summary-tick-article (&optional article clear-mark)
9800   "Mark current article as unread.
9801 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9802 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9803   (interactive)
9804   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9805                                        gnus-ticked-mark)))
9806
9807 (defun gnus-summary-mark-as-read-forward (n)
9808   "Mark N articles as read forwards.
9809 If N is negative, mark backwards instead.
9810 The difference between N and the actual number of articles marked is
9811 returned."
9812   (interactive "p")
9813   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9814
9815 (defun gnus-summary-mark-as-read-backward (n)
9816   "Mark the N articles as read backwards.
9817 The difference between N and the actual number of articles marked is
9818 returned."
9819   (interactive "p")
9820   (gnus-summary-mark-forward
9821    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9822
9823 (defun gnus-summary-mark-as-read (&optional article mark)
9824   "Mark current article as read.
9825 ARTICLE specifies the article to be marked as read.
9826 MARK specifies a string to be inserted at the beginning of the line."
9827   (gnus-summary-mark-article article mark))
9828
9829 (defun gnus-summary-clear-mark-forward (n)
9830   "Clear marks from N articles forward.
9831 If N is negative, clear backward instead.
9832 The difference between N and the number of marks cleared is returned."
9833   (interactive "p")
9834   (gnus-summary-mark-forward n gnus-unread-mark))
9835
9836 (defun gnus-summary-clear-mark-backward (n)
9837   "Clear marks from N articles backward.
9838 The difference between N and the number of marks cleared is returned."
9839   (interactive "p")
9840   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9841
9842 (defun gnus-summary-mark-unread-as-read ()
9843   "Intended to be used by `gnus-summary-mark-article-hook'."
9844   (when (memq gnus-current-article gnus-newsgroup-unreads)
9845     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9846
9847 (defun gnus-summary-mark-read-and-unread-as-read ()
9848   "Intended to be used by `gnus-summary-mark-article-hook'."
9849   (let ((mark (gnus-summary-article-mark)))
9850     (when (or (gnus-unread-mark-p mark)
9851               (gnus-read-mark-p mark))
9852       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9853
9854 (defun gnus-summary-mark-unread-as-ticked ()
9855   "Intended to be used by `gnus-summary-mark-article-hook'."
9856   (when (memq gnus-current-article gnus-newsgroup-unreads)
9857     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9858
9859 (defun gnus-summary-mark-region-as-read (point mark all)
9860   "Mark all unread articles between point and mark as read.
9861 If given a prefix, mark all articles between point and mark as read,
9862 even ticked and dormant ones."
9863   (interactive "r\nP")
9864   (save-excursion
9865     (let (article)
9866       (goto-char point)
9867       (beginning-of-line)
9868       (while (and
9869               (< (point) mark)
9870               (progn
9871                 (when (or all
9872                           (memq (setq article (gnus-summary-article-number))
9873                                 gnus-newsgroup-unreads))
9874                   (gnus-summary-mark-article article gnus-del-mark))
9875                 t)
9876               (gnus-summary-find-next))))))
9877
9878 (defun gnus-summary-mark-below (score mark)
9879   "Mark articles with score less than SCORE with MARK."
9880   (interactive "P\ncMark: ")
9881   (setq score (if score
9882                   (prefix-numeric-value score)
9883                 (or gnus-summary-default-score 0)))
9884   (save-excursion
9885     (set-buffer gnus-summary-buffer)
9886     (goto-char (point-min))
9887     (while
9888         (progn
9889           (and (< (gnus-summary-article-score) score)
9890                (gnus-summary-mark-article nil mark))
9891           (gnus-summary-find-next)))))
9892
9893 (defun gnus-summary-kill-below (&optional score)
9894   "Mark articles with score below SCORE as read."
9895   (interactive "P")
9896   (gnus-summary-mark-below score gnus-killed-mark))
9897
9898 (defun gnus-summary-clear-above (&optional score)
9899   "Clear all marks from articles with score above SCORE."
9900   (interactive "P")
9901   (gnus-summary-mark-above score gnus-unread-mark))
9902
9903 (defun gnus-summary-tick-above (&optional score)
9904   "Tick all articles with score above SCORE."
9905   (interactive "P")
9906   (gnus-summary-mark-above score gnus-ticked-mark))
9907
9908 (defun gnus-summary-mark-above (score mark)
9909   "Mark articles with score over SCORE with MARK."
9910   (interactive "P\ncMark: ")
9911   (setq score (if score
9912                   (prefix-numeric-value score)
9913                 (or gnus-summary-default-score 0)))
9914   (save-excursion
9915     (set-buffer gnus-summary-buffer)
9916     (goto-char (point-min))
9917     (while (and (progn
9918                   (when (> (gnus-summary-article-score) score)
9919                     (gnus-summary-mark-article nil mark))
9920                   t)
9921                 (gnus-summary-find-next)))))
9922
9923 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9924 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9925 (defun gnus-summary-limit-include-expunged (&optional no-error)
9926   "Display all the hidden articles that were expunged for low scores."
9927   (interactive)
9928   (let ((buffer-read-only nil))
9929     (let ((scored gnus-newsgroup-scored)
9930           headers h)
9931       (while scored
9932         (unless (gnus-summary-article-header (caar scored))
9933           (and (setq h (gnus-number-to-header (caar scored)))
9934                (< (cdar scored) gnus-summary-expunge-below)
9935                (push h headers)))
9936         (setq scored (cdr scored)))
9937       (if (not headers)
9938           (when (not no-error)
9939             (error "No expunged articles hidden"))
9940         (goto-char (point-min))
9941         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9942         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9943         (mapcar (lambda (x) (push (mail-header-number x)
9944                                   gnus-newsgroup-limit))
9945                 headers)
9946         (gnus-summary-prepare-unthreaded (nreverse headers))
9947         (goto-char (point-min))
9948         (gnus-summary-position-point)
9949         t))))
9950
9951 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9952   "Mark all unread articles in this newsgroup as read.
9953 If prefix argument ALL is non-nil, ticked and dormant articles will
9954 also be marked as read.
9955 If QUIETLY is non-nil, no questions will be asked.
9956 If TO-HERE is non-nil, it should be a point in the buffer.  All
9957 articles before (after, if REVERSE is set) this point will be marked as read.
9958 Note that this function will only catch up the unread article
9959 in the current summary buffer limitation.
9960 The number of articles marked as read is returned."
9961   (interactive "P")
9962   (prog1
9963       (save-excursion
9964         (when (or quietly
9965                   (not gnus-interactive-catchup) ;Without confirmation?
9966                   gnus-expert-user
9967                   (gnus-y-or-n-p
9968                    (if all
9969                        "Mark absolutely all articles as read? "
9970                      "Mark all unread articles as read? ")))
9971           (if (and not-mark
9972                    (not gnus-newsgroup-adaptive)
9973                    (not gnus-newsgroup-auto-expire)
9974                    (not gnus-suppress-duplicates)
9975                    (or (not gnus-use-cache)
9976                        (eq gnus-use-cache 'passive)))
9977               (progn
9978                 (when all
9979                   (setq gnus-newsgroup-marked nil
9980                         gnus-newsgroup-spam-marked nil
9981                         gnus-newsgroup-dormant nil))
9982                 (setq gnus-newsgroup-unreads
9983                       (gnus-intersection gnus-newsgroup-unreads
9984                                          gnus-newsgroup-downloadable)))
9985             ;; We actually mark all articles as canceled, which we
9986             ;; have to do when using auto-expiry or adaptive scoring.
9987             (gnus-summary-show-all-threads)
9988             (if (and to-here reverse)
9989                 (progn
9990                   (goto-char to-here)
9991                   (while (and
9992                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9993                           (gnus-summary-find-next (not all) nil nil t))))
9994               (when (gnus-summary-first-subject (not all) t)
9995                 (while (and
9996                         (if to-here (< (point) to-here) t)
9997                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9998                         (gnus-summary-find-next (not all) nil nil t)))))
9999             (gnus-set-mode-line 'summary))
10000           t))
10001     (gnus-summary-position-point)))
10002
10003 (defun gnus-summary-catchup-to-here (&optional all)
10004   "Mark all unticked articles before the current one as read.
10005 If ALL is non-nil, also mark ticked and dormant articles as read."
10006   (interactive "P")
10007   (save-excursion
10008     (gnus-save-hidden-threads
10009       (let ((beg (point)))
10010         ;; We check that there are unread articles.
10011         (when (or all (gnus-summary-find-prev))
10012           (gnus-summary-catchup all t beg)))))
10013   (gnus-summary-position-point))
10014
10015 (defun gnus-summary-catchup-from-here (&optional all)
10016   "Mark all unticked articles after the current one as read.
10017 If ALL is non-nil, also mark ticked and dormant articles as read."
10018   (interactive "P")
10019   (save-excursion
10020     (gnus-save-hidden-threads
10021       (let ((beg (point)))
10022         ;; We check that there are unread articles.
10023         (when (or all (gnus-summary-find-next))
10024           (gnus-summary-catchup all t beg nil t)))))
10025
10026   (gnus-summary-position-point))
10027 (defun gnus-summary-catchup-all (&optional quietly)
10028   "Mark all articles in this newsgroup as read.
10029 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10030 instead, which marks only unread articles as read."
10031   (interactive "P")
10032   (gnus-summary-catchup t quietly))
10033
10034 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10035   "Mark all unread articles in this group as read, then exit.
10036 If prefix argument ALL is non-nil, all articles are marked as read.
10037 If QUIETLY is non-nil, no questions will be asked."
10038   (interactive "P")
10039   (when (gnus-summary-catchup all quietly nil 'fast)
10040     ;; Select next newsgroup or exit.
10041     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10042              (eq gnus-auto-select-next 'quietly))
10043         (gnus-summary-next-group nil)
10044       (gnus-summary-exit))))
10045
10046 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10047   "Mark all articles in this newsgroup as read, and then exit.
10048 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10049 instead, which marks only unread articles as read."
10050   (interactive "P")
10051   (gnus-summary-catchup-and-exit t quietly))
10052
10053 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10054   "Mark all articles in this group as read and select the next group.
10055 If given a prefix, mark all articles, unread as well as ticked, as
10056 read."
10057   (interactive "P")
10058   (save-excursion
10059     (gnus-summary-catchup all))
10060   (gnus-summary-next-group))
10061
10062 ;;;
10063 ;;; with article
10064 ;;;
10065
10066 (defmacro gnus-with-article (article &rest forms)
10067   "Select ARTICLE and perform FORMS in the original article buffer.
10068 Then replace the article with the result."
10069   `(progn
10070      ;; We don't want the article to be marked as read.
10071      (let (gnus-mark-article-hook)
10072        (gnus-summary-select-article t t nil ,article))
10073      (set-buffer gnus-original-article-buffer)
10074      ,@forms
10075      (if (not (gnus-check-backend-function
10076                'request-replace-article (car gnus-article-current)))
10077          (gnus-message 5 "Read-only group; not replacing")
10078        (unless (gnus-request-replace-article
10079                 ,article (car gnus-article-current)
10080                 (current-buffer) t)
10081          (error "Couldn't replace article")))
10082      ;; The cache and backlog have to be flushed somewhat.
10083      (when gnus-keep-backlog
10084        (gnus-backlog-remove-article
10085         (car gnus-article-current) (cdr gnus-article-current)))
10086      (when gnus-use-cache
10087        (gnus-cache-update-article
10088         (car gnus-article-current) (cdr gnus-article-current)))))
10089
10090 (put 'gnus-with-article 'lisp-indent-function 1)
10091 (put 'gnus-with-article 'edebug-form-spec '(form body))
10092
10093 ;; Thread-based commands.
10094
10095 (defun gnus-summary-articles-in-thread (&optional article)
10096   "Return a list of all articles in the current thread.
10097 If ARTICLE is non-nil, return all articles in the thread that starts
10098 with that article."
10099   (let* ((article (or article (gnus-summary-article-number)))
10100          (data (gnus-data-find-list article))
10101          (top-level (gnus-data-level (car data)))
10102          (top-subject
10103           (cond ((null gnus-thread-operation-ignore-subject)
10104                  (gnus-simplify-subject-re
10105                   (mail-header-subject (gnus-data-header (car data)))))
10106                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10107                  (gnus-simplify-subject-fuzzy
10108                   (mail-header-subject (gnus-data-header (car data)))))
10109                 (t nil)))
10110          (end-point (save-excursion
10111                       (if (gnus-summary-go-to-next-thread)
10112                           (point) (point-max))))
10113          articles)
10114     (while (and data
10115                 (< (gnus-data-pos (car data)) end-point))
10116       (when (or (not top-subject)
10117                 (string= top-subject
10118                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10119                              (gnus-simplify-subject-fuzzy
10120                               (mail-header-subject
10121                                (gnus-data-header (car data))))
10122                            (gnus-simplify-subject-re
10123                             (mail-header-subject
10124                              (gnus-data-header (car data)))))))
10125         (push (gnus-data-number (car data)) articles))
10126       (unless (and (setq data (cdr data))
10127                    (> (gnus-data-level (car data)) top-level))
10128         (setq data nil)))
10129     ;; Return the list of articles.
10130     (nreverse articles)))
10131
10132 (defun gnus-summary-rethread-current ()
10133   "Rethread the thread the current article is part of."
10134   (interactive)
10135   (let* ((gnus-show-threads t)
10136          (article (gnus-summary-article-number))
10137          (id (mail-header-id (gnus-summary-article-header)))
10138          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10139     (unless id
10140       (error "No article on the current line"))
10141     (gnus-rebuild-thread id)
10142     (gnus-summary-goto-subject article)))
10143
10144 (defun gnus-summary-reparent-thread ()
10145   "Make the current article child of the marked (or previous) article.
10146
10147 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10148 is non-nil or the Subject: of both articles are the same."
10149   (interactive)
10150   (unless (not (gnus-group-read-only-p))
10151     (error "The current newsgroup does not support article editing"))
10152   (unless (<= (length gnus-newsgroup-processable) 1)
10153     (error "No more than one article may be marked"))
10154   (save-window-excursion
10155     (let ((gnus-article-buffer " *reparent*")
10156           (current-article (gnus-summary-article-number))
10157           ;; First grab the marked article, otherwise one line up.
10158           (parent-article (if (not (null gnus-newsgroup-processable))
10159                               (car gnus-newsgroup-processable)
10160                             (save-excursion
10161                               (if (eq (forward-line -1) 0)
10162                                   (gnus-summary-article-number)
10163                                 (error "Beginning of summary buffer"))))))
10164       (unless (not (eq current-article parent-article))
10165         (error "An article may not be self-referential"))
10166       (let ((message-id (mail-header-id
10167                          (gnus-summary-article-header parent-article))))
10168         (unless (and message-id (not (equal message-id "")))
10169           (error "No message-id in desired parent"))
10170         (gnus-with-article current-article
10171           (save-restriction
10172             (goto-char (point-min))
10173             (message-narrow-to-head)
10174             (if (re-search-forward "^References: " nil t)
10175                 (progn
10176                   (re-search-forward "^[^ \t]" nil t)
10177                   (forward-line -1)
10178                   (end-of-line)
10179                   (insert " " message-id))
10180               (insert "References: " message-id "\n"))))
10181         (set-buffer gnus-summary-buffer)
10182         (gnus-summary-unmark-all-processable)
10183         (gnus-summary-update-article current-article)
10184         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10185             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10186         (gnus-summary-rethread-current)
10187         (gnus-message 3 "Article %d is now the child of article %d"
10188                       current-article parent-article)))))
10189
10190 (defun gnus-summary-toggle-threads (&optional arg)
10191   "Toggle showing conversation threads.
10192 If ARG is positive number, turn showing conversation threads on."
10193   (interactive "P")
10194   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10195     (setq gnus-show-threads
10196           (if (null arg) (not gnus-show-threads)
10197             (> (prefix-numeric-value arg) 0)))
10198     (gnus-summary-prepare)
10199     (gnus-summary-goto-subject current)
10200     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10201     (gnus-summary-position-point)))
10202
10203 (defun gnus-summary-show-all-threads ()
10204   "Show all threads."
10205   (interactive)
10206   (save-excursion
10207     (let ((buffer-read-only nil))
10208       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10209   (gnus-summary-position-point))
10210
10211 (defun gnus-summary-show-thread ()
10212   "Show thread subtrees.
10213 Returns nil if no thread was there to be shown."
10214   (interactive)
10215   (let ((buffer-read-only nil)
10216         (orig (point))
10217         ;; first goto end then to beg, to have point at beg after let
10218         (end (progn (end-of-line) (point)))
10219         (beg (progn (beginning-of-line) (point))))
10220     (prog1
10221         ;; Any hidden lines here?
10222         (search-forward "\r" end t)
10223       (subst-char-in-region beg end ?\^M ?\n t)
10224       (goto-char orig)
10225       (gnus-summary-position-point))))
10226
10227 (defun gnus-summary-maybe-hide-threads ()
10228   "If requested, hide the threads that should be hidden."
10229   (when (and gnus-show-threads
10230              gnus-thread-hide-subtree)
10231     (gnus-summary-hide-all-threads
10232      (if (or (consp gnus-thread-hide-subtree)
10233              (gnus-functionp gnus-thread-hide-subtree))
10234          (gnus-make-predicate gnus-thread-hide-subtree)
10235        nil))))
10236
10237 ;;; Hiding predicates.
10238
10239 (defun gnus-article-unread-p (header)
10240   (memq (mail-header-number header) gnus-newsgroup-unreads))
10241
10242 (defun gnus-article-unseen-p (header)
10243   (memq (mail-header-number header) gnus-newsgroup-unseen))
10244
10245 (defun gnus-map-articles (predicate articles)
10246   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10247   (apply 'gnus-or (mapcar predicate
10248                           (mapcar 'gnus-summary-article-header articles))))
10249
10250 (defun gnus-summary-hide-all-threads (&optional predicate)
10251   "Hide all thread subtrees.
10252 If PREDICATE is supplied, threads that satisfy this predicate
10253 will not be hidden."
10254   (interactive)
10255   (save-excursion
10256     (goto-char (point-min))
10257     (let ((end nil))
10258       (while (not end)
10259         (when (or (not predicate)
10260                   (gnus-map-articles
10261                    predicate (gnus-summary-article-children)))
10262             (gnus-summary-hide-thread))
10263         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10264   (gnus-summary-position-point))
10265
10266 (defun gnus-summary-hide-thread ()
10267   "Hide thread subtrees.
10268 If PREDICATE is supplied, threads that satisfy this predicate
10269 will not be hidden.
10270 Returns nil if no threads were there to be hidden."
10271   (interactive)
10272   (let ((buffer-read-only nil)
10273         (start (point))
10274         (article (gnus-summary-article-number)))
10275     (goto-char start)
10276     ;; Go forward until either the buffer ends or the subthread
10277     ;; ends.
10278     (when (and (not (eobp))
10279                (or (zerop (gnus-summary-next-thread 1 t))
10280                    (goto-char (point-max))))
10281       (prog1
10282           (if (and (> (point) start)
10283                    (search-backward "\n" start t))
10284               (progn
10285                 (subst-char-in-region start (point) ?\n ?\^M)
10286                 (gnus-summary-goto-subject article))
10287             (goto-char start)
10288             nil)))))
10289
10290 (defun gnus-summary-go-to-next-thread (&optional previous)
10291   "Go to the same level (or less) next thread.
10292 If PREVIOUS is non-nil, go to previous thread instead.
10293 Return the article number moved to, or nil if moving was impossible."
10294   (let ((level (gnus-summary-thread-level))
10295         (way (if previous -1 1))
10296         (beg (point)))
10297     (forward-line way)
10298     (while (and (not (eobp))
10299                 (< level (gnus-summary-thread-level)))
10300       (forward-line way))
10301     (if (eobp)
10302         (progn
10303           (goto-char beg)
10304           nil)
10305       (setq beg (point))
10306       (prog1
10307           (gnus-summary-article-number)
10308         (goto-char beg)))))
10309
10310 (defun gnus-summary-next-thread (n &optional silent)
10311   "Go to the same level next N'th thread.
10312 If N is negative, search backward instead.
10313 Returns the difference between N and the number of skips actually
10314 done.
10315
10316 If SILENT, don't output messages."
10317   (interactive "p")
10318   (let ((backward (< n 0))
10319         (n (abs n)))
10320     (while (and (> n 0)
10321                 (gnus-summary-go-to-next-thread backward))
10322       (decf n))
10323     (unless silent
10324       (gnus-summary-position-point))
10325     (when (and (not silent) (/= 0 n))
10326       (gnus-message 7 "No more threads"))
10327     n))
10328
10329 (defun gnus-summary-prev-thread (n)
10330   "Go to the same level previous N'th thread.
10331 Returns the difference between N and the number of skips actually
10332 done."
10333   (interactive "p")
10334   (gnus-summary-next-thread (- n)))
10335
10336 (defun gnus-summary-go-down-thread ()
10337   "Go down one level in the current thread."
10338   (let ((children (gnus-summary-article-children)))
10339     (when children
10340       (gnus-summary-goto-subject (car children)))))
10341
10342 (defun gnus-summary-go-up-thread ()
10343   "Go up one level in the current thread."
10344   (let ((parent (gnus-summary-article-parent)))
10345     (when parent
10346       (gnus-summary-goto-subject parent))))
10347
10348 (defun gnus-summary-down-thread (n)
10349   "Go down thread N steps.
10350 If N is negative, go up instead.
10351 Returns the difference between N and how many steps down that were
10352 taken."
10353   (interactive "p")
10354   (let ((up (< n 0))
10355         (n (abs n)))
10356     (while (and (> n 0)
10357                 (if up (gnus-summary-go-up-thread)
10358                   (gnus-summary-go-down-thread)))
10359       (setq n (1- n)))
10360     (gnus-summary-position-point)
10361     (when (/= 0 n)
10362       (gnus-message 7 "Can't go further"))
10363     n))
10364
10365 (defun gnus-summary-up-thread (n)
10366   "Go up thread N steps.
10367 If N is negative, go down instead.
10368 Returns the difference between N and how many steps down that were
10369 taken."
10370   (interactive "p")
10371   (gnus-summary-down-thread (- n)))
10372
10373 (defun gnus-summary-top-thread ()
10374   "Go to the top of the thread."
10375   (interactive)
10376   (while (gnus-summary-go-up-thread))
10377   (gnus-summary-article-number))
10378
10379 (defun gnus-summary-kill-thread (&optional unmark)
10380   "Mark articles under current thread as read.
10381 If the prefix argument is positive, remove any kinds of marks.
10382 If the prefix argument is negative, tick articles instead."
10383   (interactive "P")
10384   (when unmark
10385     (setq unmark (prefix-numeric-value unmark)))
10386   (let ((articles (gnus-summary-articles-in-thread)))
10387     (save-excursion
10388       ;; Expand the thread.
10389       (gnus-summary-show-thread)
10390       ;; Mark all the articles.
10391       (while articles
10392         (gnus-summary-goto-subject (car articles))
10393         (cond ((null unmark)
10394                (gnus-summary-mark-article-as-read gnus-killed-mark))
10395               ((> unmark 0)
10396                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10397               (t
10398                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10399         (setq articles (cdr articles))))
10400     ;; Hide killed subtrees.
10401     (and (null unmark)
10402          gnus-thread-hide-killed
10403          (gnus-summary-hide-thread))
10404     ;; If marked as read, go to next unread subject.
10405     (when (null unmark)
10406       ;; Go to next unread subject.
10407       (gnus-summary-next-subject 1 t)))
10408   (gnus-set-mode-line 'summary))
10409
10410 ;; Summary sorting commands
10411
10412 (defun gnus-summary-sort-by-number (&optional reverse)
10413   "Sort the summary buffer by article number.
10414 Argument REVERSE means reverse order."
10415   (interactive "P")
10416   (gnus-summary-sort 'number reverse))
10417
10418 (defun gnus-summary-sort-by-random (&optional reverse)
10419   "Randomize the order in the summary buffer.
10420 Argument REVERSE means to randomize in reverse order."
10421   (interactive "P")
10422   (gnus-summary-sort 'random reverse))
10423
10424 (defun gnus-summary-sort-by-author (&optional reverse)
10425   "Sort the summary buffer by author name alphabetically.
10426 If `case-fold-search' is non-nil, case of letters is ignored.
10427 Argument REVERSE means reverse order."
10428   (interactive "P")
10429   (gnus-summary-sort 'author reverse))
10430
10431 (defun gnus-summary-sort-by-subject (&optional reverse)
10432   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10433 If `case-fold-search' is non-nil, case of letters is ignored.
10434 Argument REVERSE means reverse order."
10435   (interactive "P")
10436   (gnus-summary-sort 'subject reverse))
10437
10438 (defun gnus-summary-sort-by-date (&optional reverse)
10439   "Sort the summary buffer by date.
10440 Argument REVERSE means reverse order."
10441   (interactive "P")
10442   (gnus-summary-sort 'date reverse))
10443
10444 (defun gnus-summary-sort-by-score (&optional reverse)
10445   "Sort the summary buffer by score.
10446 Argument REVERSE means reverse order."
10447   (interactive "P")
10448   (gnus-summary-sort 'score reverse))
10449
10450 (defun gnus-summary-sort-by-lines (&optional reverse)
10451   "Sort the summary buffer by the number of lines.
10452 Argument REVERSE means reverse order."
10453   (interactive "P")
10454   (gnus-summary-sort 'lines reverse))
10455
10456 (defun gnus-summary-sort-by-chars (&optional reverse)
10457   "Sort the summary buffer by article length.
10458 Argument REVERSE means reverse order."
10459   (interactive "P")
10460   (gnus-summary-sort 'chars reverse))
10461
10462 (defun gnus-summary-sort-by-original (&optional reverse)
10463   "Sort the summary buffer using the default sorting method.
10464 Argument REVERSE means reverse order."
10465   (interactive "P")
10466   (let* ((buffer-read-only)
10467          (gnus-summary-prepare-hook nil))
10468     ;; We do the sorting by regenerating the threads.
10469     (gnus-summary-prepare)
10470     ;; Hide subthreads if needed.
10471     (gnus-summary-maybe-hide-threads)))
10472
10473 (defun gnus-summary-sort (predicate reverse)
10474   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10475   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10476          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10477          (gnus-thread-sort-functions
10478           (if (not reverse)
10479               thread
10480             `(lambda (t1 t2)
10481                (,thread t2 t1))))
10482          (gnus-sort-gathered-threads-function
10483           gnus-thread-sort-functions)
10484          (gnus-article-sort-functions
10485           (if (not reverse)
10486               article
10487             `(lambda (t1 t2)
10488                (,article t2 t1))))
10489          (buffer-read-only)
10490          (gnus-summary-prepare-hook nil))
10491     ;; We do the sorting by regenerating the threads.
10492     (gnus-summary-prepare)
10493     ;; Hide subthreads if needed.
10494     (gnus-summary-maybe-hide-threads)))
10495
10496 ;; Summary saving commands.
10497
10498 (defun gnus-summary-save-article (&optional n not-saved)
10499   "Save the current article using the default saver function.
10500 If N is a positive number, save the N next articles.
10501 If N is a negative number, save the N previous articles.
10502 If N is nil and any articles have been marked with the process mark,
10503 save those articles instead.
10504 The variable `gnus-default-article-saver' specifies the saver function."
10505   (interactive "P")
10506   (let* ((articles (gnus-summary-work-articles n))
10507          (save-buffer (save-excursion
10508                         (nnheader-set-temp-buffer " *Gnus Save*")))
10509          (num (length articles))
10510          header file)
10511     (dolist (article articles)
10512       (setq header (gnus-summary-article-header article))
10513       (if (not (vectorp header))
10514           ;; This is a pseudo-article.
10515           (if (assq 'name header)
10516               (gnus-copy-file (cdr (assq 'name header)))
10517             (gnus-message 1 "Article %d is unsaveable" article))
10518         ;; This is a real article.
10519         (save-window-excursion
10520           (let ((gnus-display-mime-function nil)
10521                 (gnus-article-prepare-hook nil))
10522             (gnus-summary-select-article t nil nil article)))
10523         (save-excursion
10524           (set-buffer save-buffer)
10525           (erase-buffer)
10526           (insert-buffer-substring gnus-original-article-buffer))
10527         (setq file (gnus-article-save save-buffer file num))
10528         (gnus-summary-remove-process-mark article)
10529         (unless not-saved
10530           (gnus-summary-set-saved-mark article))))
10531     (gnus-kill-buffer save-buffer)
10532     (gnus-summary-position-point)
10533     (gnus-set-mode-line 'summary)
10534     n))
10535
10536 (defun gnus-summary-pipe-output (&optional arg headers)
10537   "Pipe the current article to a subprocess.
10538 If N is a positive number, pipe the N next articles.
10539 If N is a negative number, pipe the N previous articles.
10540 If N is nil and any articles have been marked with the process mark,
10541 pipe those articles instead.
10542 If HEADERS (the symbolic prefix), include the headers, too."
10543   (interactive (gnus-interactive "P\ny"))
10544   (require 'gnus-art)
10545   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10546         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10547     (gnus-summary-save-article arg t))
10548   (let ((buffer (get-buffer "*Shell Command Output*")))
10549     (when (and buffer
10550                (not (zerop (buffer-size buffer))))
10551       (gnus-configure-windows 'pipe))))
10552
10553 (defun gnus-summary-save-article-mail (&optional arg)
10554   "Append the current article to an mail file.
10555 If N is a positive number, save the N next articles.
10556 If N is a negative number, save the N previous articles.
10557 If N is nil and any articles have been marked with the process mark,
10558 save those articles instead."
10559   (interactive "P")
10560   (require 'gnus-art)
10561   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10562     (gnus-summary-save-article arg)))
10563
10564 (defun gnus-summary-save-article-rmail (&optional arg)
10565   "Append the current article to an rmail file.
10566 If N is a positive number, save the N next articles.
10567 If N is a negative number, save the N previous articles.
10568 If N is nil and any articles have been marked with the process mark,
10569 save those articles instead."
10570   (interactive "P")
10571   (require 'gnus-art)
10572   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10573     (gnus-summary-save-article arg)))
10574
10575 (defun gnus-summary-save-article-file (&optional arg)
10576   "Append the current article to a file.
10577 If N is a positive number, save the N next articles.
10578 If N is a negative number, save the N previous articles.
10579 If N is nil and any articles have been marked with the process mark,
10580 save those articles instead."
10581   (interactive "P")
10582   (require 'gnus-art)
10583   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10584     (gnus-summary-save-article arg)))
10585
10586 (defun gnus-summary-write-article-file (&optional arg)
10587   "Write the current article to a file, deleting the previous file.
10588 If N is a positive number, save the N next articles.
10589 If N is a negative number, save the N previous articles.
10590 If N is nil and any articles have been marked with the process mark,
10591 save those articles instead."
10592   (interactive "P")
10593   (require 'gnus-art)
10594   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10595     (gnus-summary-save-article arg)))
10596
10597 (defun gnus-summary-save-article-body-file (&optional arg)
10598   "Append the current article body to a file.
10599 If N is a positive number, save the N next articles.
10600 If N is a negative number, save the N previous articles.
10601 If N is nil and any articles have been marked with the process mark,
10602 save those articles instead."
10603   (interactive "P")
10604   (require 'gnus-art)
10605   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10606     (gnus-summary-save-article arg)))
10607
10608 (defun gnus-summary-muttprint (&optional arg)
10609   "Print the current article using Muttprint.
10610 If N is a positive number, save the N next articles.
10611 If N is a negative number, save the N previous articles.
10612 If N is nil and any articles have been marked with the process mark,
10613 save those articles instead."
10614   (interactive "P")
10615   (require 'gnus-art)
10616   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10617     (gnus-summary-save-article arg t)))
10618
10619 (defun gnus-summary-pipe-message (program)
10620   "Pipe the current article through PROGRAM."
10621   (interactive "sProgram: ")
10622   (gnus-summary-select-article)
10623   (let ((mail-header-separator ""))
10624     (gnus-eval-in-buffer-window gnus-article-buffer
10625       (save-restriction
10626         (widen)
10627         (let ((start (window-start))
10628               buffer-read-only)
10629           (message-pipe-buffer-body program)
10630           (set-window-start (get-buffer-window (current-buffer)) start))))))
10631
10632 (defun gnus-get-split-value (methods)
10633   "Return a value based on the split METHODS."
10634   (let (split-name method result match)
10635     (when methods
10636       (save-excursion
10637         (set-buffer gnus-original-article-buffer)
10638         (save-restriction
10639           (nnheader-narrow-to-headers)
10640           (while (and methods (not split-name))
10641             (goto-char (point-min))
10642             (setq method (pop methods))
10643             (setq match (car method))
10644             (when (cond
10645                    ((stringp match)
10646                     ;; Regular expression.
10647                     (ignore-errors
10648                       (re-search-forward match nil t)))
10649                    ((gnus-functionp match)
10650                     ;; Function.
10651                     (save-restriction
10652                       (widen)
10653                       (setq result (funcall match gnus-newsgroup-name))))
10654                    ((consp match)
10655                     ;; Form.
10656                     (save-restriction
10657                       (widen)
10658                       (setq result (eval match)))))
10659               (setq split-name (cdr method))
10660               (cond ((stringp result)
10661                      (push (expand-file-name
10662                             result gnus-article-save-directory)
10663                            split-name))
10664                     ((consp result)
10665                      (setq split-name (append result split-name)))))))))
10666     (nreverse split-name)))
10667
10668 (defun gnus-valid-move-group-p (group)
10669   (and (boundp group)
10670        (symbol-name group)
10671        (symbol-value group)
10672        (gnus-get-function (gnus-find-method-for-group
10673                            (symbol-name group)) 'request-accept-article t)))
10674
10675 (defun gnus-read-move-group-name (prompt default articles prefix)
10676   "Read a group name."
10677   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10678          (minibuffer-confirm-incomplete nil) ; XEmacs
10679          (prom
10680           (format "%s %s to:"
10681                   prompt
10682                   (if (> (length articles) 1)
10683                       (format "these %d articles" (length articles))
10684                     "this article")))
10685          (to-newsgroup
10686           (cond
10687            ((null split-name)
10688             (gnus-completing-read-with-default
10689              default prom
10690              gnus-active-hashtb
10691              'gnus-valid-move-group-p
10692              nil prefix
10693              'gnus-group-history))
10694            ((= 1 (length split-name))
10695             (gnus-completing-read-with-default
10696              (car split-name) prom
10697              gnus-active-hashtb
10698              'gnus-valid-move-group-p
10699              nil nil
10700              'gnus-group-history))
10701            (t
10702             (gnus-completing-read-with-default
10703              nil prom
10704              (mapcar (lambda (el) (list el))
10705                      (nreverse split-name))
10706              nil nil nil
10707              'gnus-group-history))))
10708          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10709     (when to-newsgroup
10710       (if (or (string= to-newsgroup "")
10711               (string= to-newsgroup prefix))
10712           (setq to-newsgroup default))
10713       (unless to-newsgroup
10714         (error "No group name entered"))
10715       (or (gnus-active to-newsgroup)
10716           (gnus-activate-group to-newsgroup nil nil to-method)
10717           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10718                                      to-newsgroup))
10719               (or (and (gnus-request-create-group to-newsgroup to-method)
10720                        (gnus-activate-group
10721                         to-newsgroup nil nil to-method)
10722                        (gnus-subscribe-group to-newsgroup))
10723                   (error "Couldn't create group %s" to-newsgroup)))
10724           (error "No such group: %s" to-newsgroup)))
10725     to-newsgroup))
10726
10727 (defun gnus-summary-save-parts (type dir n &optional reverse)
10728   "Save parts matching TYPE to DIR.
10729 If REVERSE, save parts that do not match TYPE."
10730   (interactive
10731    (list (read-string "Save parts of type: "
10732                       (or (car gnus-summary-save-parts-type-history)
10733                           gnus-summary-save-parts-default-mime)
10734                       'gnus-summary-save-parts-type-history)
10735          (setq gnus-summary-save-parts-last-directory
10736                (read-file-name "Save to directory: "
10737                                gnus-summary-save-parts-last-directory
10738                                nil t))
10739          current-prefix-arg))
10740   (gnus-summary-iterate n
10741     (let ((gnus-display-mime-function nil)
10742           (gnus-inhibit-treatment t))
10743       (gnus-summary-select-article))
10744     (save-excursion
10745       (set-buffer gnus-article-buffer)
10746       (let ((handles (or gnus-article-mime-handles
10747                          (mm-dissect-buffer nil gnus-article-loose-mime)
10748                          (and gnus-article-emulate-mime
10749                               (mm-uu-dissect)))))
10750         (when handles
10751           (gnus-summary-save-parts-1 type dir handles reverse)
10752           (unless gnus-article-mime-handles ;; Don't destroy this case.
10753             (mm-destroy-parts handles)))))))
10754
10755 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10756   (if (stringp (car handle))
10757       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10758               (cdr handle))
10759     (when (if reverse
10760               (not (string-match type (mm-handle-media-type handle)))
10761             (string-match type (mm-handle-media-type handle)))
10762       (let ((file (expand-file-name
10763                    (file-name-nondirectory
10764                     (or
10765                      (mail-content-type-get
10766                       (mm-handle-disposition handle) 'filename)
10767                      (concat gnus-newsgroup-name
10768                              "." (number-to-string
10769                                   (cdr gnus-article-current)))))
10770                    dir)))
10771         (unless (file-exists-p file)
10772           (mm-save-part-to-file handle file))))))
10773
10774 ;; Summary extract commands
10775
10776 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10777   (let ((buffer-read-only nil)
10778         (article (gnus-summary-article-number))
10779         after-article b e)
10780     (unless (gnus-summary-goto-subject article)
10781       (error "No such article: %d" article))
10782     (gnus-summary-position-point)
10783     ;; If all commands are to be bunched up on one line, we collect
10784     ;; them here.
10785     (unless gnus-view-pseudos-separately
10786       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10787             files action)
10788         (while ps
10789           (setq action (cdr (assq 'action (car ps))))
10790           (setq files (list (cdr (assq 'name (car ps)))))
10791           (while (and ps (cdr ps)
10792                       (string= (or action "1")
10793                                (or (cdr (assq 'action (cadr ps))) "2")))
10794             (push (cdr (assq 'name (cadr ps))) files)
10795             (setcdr ps (cddr ps)))
10796           (when files
10797             (when (not (string-match "%s" action))
10798               (push " " files))
10799             (push " " files)
10800             (when (assq 'execute (car ps))
10801               (setcdr (assq 'execute (car ps))
10802                       (funcall (if (string-match "%s" action)
10803                                    'format 'concat)
10804                                action
10805                                (mapconcat
10806                                 (lambda (f)
10807                                   (if (equal f " ")
10808                                       f
10809                                     (mm-quote-arg f)))
10810                                 files " ")))))
10811           (setq ps (cdr ps)))))
10812     (if (and gnus-view-pseudos (not not-view))
10813         (while pslist
10814           (when (assq 'execute (car pslist))
10815             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10816                                   (eq gnus-view-pseudos 'not-confirm)))
10817           (setq pslist (cdr pslist)))
10818       (save-excursion
10819         (while pslist
10820           (setq after-article (or (cdr (assq 'article (car pslist)))
10821                                   (gnus-summary-article-number)))
10822           (gnus-summary-goto-subject after-article)
10823           (forward-line 1)
10824           (setq b (point))
10825           (insert "    " (file-name-nondirectory
10826                           (cdr (assq 'name (car pslist))))
10827                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10828           (setq e (point))
10829           (forward-line -1)             ; back to `b'
10830           (gnus-add-text-properties
10831            b (1- e) (list 'gnus-number gnus-reffed-article-number
10832                           gnus-mouse-face-prop gnus-mouse-face))
10833           (gnus-data-enter
10834            after-article gnus-reffed-article-number
10835            gnus-unread-mark b (car pslist) 0 (- e b))
10836           (setq gnus-newsgroup-unreads
10837                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10838                                          gnus-reffed-article-number))
10839           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10840           (setq pslist (cdr pslist)))))))
10841
10842 (defun gnus-pseudos< (p1 p2)
10843   (let ((c1 (cdr (assq 'action p1)))
10844         (c2 (cdr (assq 'action p2))))
10845     (and c1 c2 (string< c1 c2))))
10846
10847 (defun gnus-request-pseudo-article (props)
10848   (cond ((assq 'execute props)
10849          (gnus-execute-command (cdr (assq 'execute props)))))
10850   (let ((gnus-current-article (gnus-summary-article-number)))
10851     (gnus-run-hooks 'gnus-mark-article-hook)))
10852
10853 (defun gnus-execute-command (command &optional automatic)
10854   (save-excursion
10855     (gnus-article-setup-buffer)
10856     (set-buffer gnus-article-buffer)
10857     (setq buffer-read-only nil)
10858     (let ((command (if automatic command
10859                      (read-string "Command: " (cons command 0)))))
10860       (erase-buffer)
10861       (insert "$ " command "\n\n")
10862       (if gnus-view-pseudo-asynchronously
10863           (start-process "gnus-execute" (current-buffer) shell-file-name
10864                          shell-command-switch command)
10865         (call-process shell-file-name nil t nil
10866                       shell-command-switch command)))))
10867
10868 ;; Summary kill commands.
10869
10870 (defun gnus-summary-edit-global-kill (article)
10871   "Edit the \"global\" kill file."
10872   (interactive (list (gnus-summary-article-number)))
10873   (gnus-group-edit-global-kill article))
10874
10875 (defun gnus-summary-edit-local-kill ()
10876   "Edit a local kill file applied to the current newsgroup."
10877   (interactive)
10878   (setq gnus-current-headers (gnus-summary-article-header))
10879   (gnus-group-edit-local-kill
10880    (gnus-summary-article-number) gnus-newsgroup-name))
10881
10882 ;;; Header reading.
10883
10884 (defun gnus-read-header (id &optional header)
10885   "Read the headers of article ID and enter them into the Gnus system."
10886   (let ((group gnus-newsgroup-name)
10887         (gnus-override-method
10888          (or
10889           gnus-override-method
10890           (and (gnus-news-group-p gnus-newsgroup-name)
10891                (car (gnus-refer-article-methods)))))
10892         where)
10893     ;; First we check to see whether the header in question is already
10894     ;; fetched.
10895     (if (stringp id)
10896         ;; This is a Message-ID.
10897         (setq header (or header (gnus-id-to-header id)))
10898       ;; This is an article number.
10899       (setq header (or header (gnus-summary-article-header id))))
10900     (if (and header
10901              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10902         ;; We have found the header.
10903         header
10904       ;; If this is a sparse article, we have to nix out its
10905       ;; previous entry in the thread hashtb.
10906       (when (and header
10907                  (gnus-summary-article-sparse-p (mail-header-number header)))
10908         (let* ((parent (gnus-parent-id (mail-header-references header)))
10909                (thread (and parent (gnus-id-to-thread parent))))
10910           (when thread
10911             (delq (assq header thread) thread))))
10912       ;; We have to really fetch the header to this article.
10913       (save-excursion
10914         (set-buffer nntp-server-buffer)
10915         (when (setq where (gnus-request-head id group))
10916           (nnheader-fold-continuation-lines)
10917           (goto-char (point-max))
10918           (insert ".\n")
10919           (goto-char (point-min))
10920           (insert "211 ")
10921           (princ (cond
10922                   ((numberp id) id)
10923                   ((cdr where) (cdr where))
10924                   (header (mail-header-number header))
10925                   (t gnus-reffed-article-number))
10926                  (current-buffer))
10927           (insert " Article retrieved.\n"))
10928         (if (or (not where)
10929                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10930             ()                          ; Malformed head.
10931           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10932             (when (and (stringp id)
10933                        (not (string= (gnus-group-real-name group)
10934                                      (car where))))
10935               ;; If we fetched by Message-ID and the article came
10936               ;; from a different group, we fudge some bogus article
10937               ;; numbers for this article.
10938               (mail-header-set-number header gnus-reffed-article-number))
10939             (save-excursion
10940               (set-buffer gnus-summary-buffer)
10941               (decf gnus-reffed-article-number)
10942               (gnus-remove-header (mail-header-number header))
10943               (push header gnus-newsgroup-headers)
10944               (setq gnus-current-headers header)
10945               (push (mail-header-number header) gnus-newsgroup-limit)))
10946           header)))))
10947
10948 (defun gnus-remove-header (number)
10949   "Remove header NUMBER from `gnus-newsgroup-headers'."
10950   (if (and gnus-newsgroup-headers
10951            (= number (mail-header-number (car gnus-newsgroup-headers))))
10952       (pop gnus-newsgroup-headers)
10953     (let ((headers gnus-newsgroup-headers))
10954       (while (and (cdr headers)
10955                   (not (= number (mail-header-number (cadr headers)))))
10956         (pop headers))
10957       (when (cdr headers)
10958         (setcdr headers (cddr headers))))))
10959
10960 ;;;
10961 ;;; summary highlights
10962 ;;;
10963
10964 (defun gnus-highlight-selected-summary ()
10965   "Highlight selected article in summary buffer."
10966   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10967   (when gnus-summary-selected-face
10968     (save-excursion
10969       (let* ((beg (progn (beginning-of-line) (point)))
10970              (end (progn (end-of-line) (point)))
10971              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10972              (from (if (get-text-property beg gnus-mouse-face-prop)
10973                        beg
10974                      (or (next-single-property-change
10975                           beg gnus-mouse-face-prop nil end)
10976                          beg)))
10977              (to
10978               (if (= from end)
10979                   (- from 2)
10980                 (or (next-single-property-change
10981                      from gnus-mouse-face-prop nil end)
10982                     end))))
10983         ;; If no mouse-face prop on line we will have to = from = end,
10984         ;; so we highlight the entire line instead.
10985         (when (= (+ to 2) from)
10986           (setq from beg)
10987           (setq to end))
10988         (if gnus-newsgroup-selected-overlay
10989             ;; Move old overlay.
10990             (gnus-move-overlay
10991              gnus-newsgroup-selected-overlay from to (current-buffer))
10992           ;; Create new overlay.
10993           (gnus-overlay-put
10994            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10995            'face gnus-summary-selected-face))))))
10996
10997 (defvar gnus-summary-highlight-line-cached nil)
10998 (defvar gnus-summary-highlight-line-trigger nil)
10999
11000 (defun gnus-summary-highlight-line-0 ()
11001   (if (and (eq gnus-summary-highlight-line-trigger 
11002                gnus-summary-highlight)
11003            gnus-summary-highlight-line-cached)
11004       gnus-summary-highlight-line-cached
11005     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11006           gnus-summary-highlight-line-cached
11007           (let* ((cond (list 'cond))
11008                  (c cond)
11009                  (list gnus-summary-highlight))
11010             (while list
11011               (setcdr c (cons (list (caar list) (list 'quote (cdar list))) nil))
11012               (setq c (cdr c)
11013                     list (cdr list)))
11014             (gnus-byte-compile (list 'lambda nil cond))))))
11015
11016 (defun gnus-summary-highlight-line ()
11017   "Highlight current line according to `gnus-summary-highlight'."
11018   (let* ((beg (gnus-point-at-bol))
11019          (article (or (gnus-summary-article-number) gnus-current-article))
11020          (score (or (cdr (assq article
11021                                gnus-newsgroup-scored))
11022                     gnus-summary-default-score 0))
11023          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11024          (inhibit-read-only t)
11025          (default gnus-summary-default-score)
11026          (default-high gnus-summary-default-high-score)
11027          (default-low gnus-summary-default-low-score)
11028          (uncached (memq article gnus-newsgroup-undownloaded))
11029          (downloaded (not uncached)))
11030     (let ((face (funcall (gnus-summary-highlight-line-0))))
11031       (unless (eq face (get-text-property beg 'face))
11032         (gnus-put-text-property-excluding-characters-with-faces
11033          beg (gnus-point-at-eol) 'face
11034          (setq face (if (boundp face) (symbol-value face) face)))
11035         (when gnus-summary-highlight-line-function
11036           (funcall gnus-summary-highlight-line-function article face))))))
11037
11038 (defun gnus-update-read-articles (group unread &optional compute)
11039   "Update the list of read articles in GROUP.
11040 UNREAD is a sorted list."
11041   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11042          (entry (gnus-gethash group gnus-newsrc-hashtb))
11043          (info (nth 2 entry))
11044          (prev 1)
11045          read)
11046     (if (or (not info) (not active))
11047         ;; There is no info on this group if it was, in fact,
11048         ;; killed.  Gnus stores no information on killed groups, so
11049         ;; there's nothing to be done.
11050         ;; One could store the information somewhere temporarily,
11051         ;; perhaps...  Hmmm...
11052         ()
11053       ;; Remove any negative articles numbers.
11054       (while (and unread (< (car unread) 0))
11055         (setq unread (cdr unread)))
11056       ;; Remove any expired article numbers
11057       (while (and unread (< (car unread) (car active)))
11058         (setq unread (cdr unread)))
11059       ;; Compute the ranges of read articles by looking at the list of
11060       ;; unread articles.
11061       (while unread
11062         (when (/= (car unread) prev)
11063           (push (if (= prev (1- (car unread))) prev
11064                   (cons prev (1- (car unread))))
11065                 read))
11066         (setq prev (1+ (car unread)))
11067         (setq unread (cdr unread)))
11068       (when (<= prev (cdr active))
11069         (push (cons prev (cdr active)) read))
11070       (setq read (if (> (length read) 1) (nreverse read) read))
11071       (if compute
11072           read
11073         (save-excursion
11074           (let (setmarkundo)
11075             ;; Propagate the read marks to the backend.
11076             (when (gnus-check-backend-function 'request-set-mark group)
11077               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11078                     (add (gnus-remove-from-range read (gnus-info-read info))))
11079                 (when (or add del)
11080                   (unless (gnus-check-group group)
11081                     (error "Can't open server for %s" group))
11082                   (gnus-request-set-mark
11083                    group (delq nil (list (if add (list add 'add '(read)))
11084                                          (if del (list del 'del '(read))))))
11085                   (setq setmarkundo
11086                         `(gnus-request-set-mark
11087                           ,group
11088                           ',(delq nil (list
11089                                        (if del (list del 'add '(read)))
11090                                        (if add (list add 'del '(read))))))))))
11091             (set-buffer gnus-group-buffer)
11092             (gnus-undo-register
11093               `(progn
11094                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11095                  (gnus-info-set-read ',info ',(gnus-info-read info))
11096                  (gnus-get-unread-articles-in-group ',info
11097                                                     (gnus-active ,group))
11098                  (gnus-group-update-group ,group t)
11099                  ,setmarkundo))))
11100         ;; Enter this list into the group info.
11101         (gnus-info-set-read info read)
11102         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11103         (gnus-get-unread-articles-in-group info (gnus-active group))
11104         t))))
11105
11106 (defun gnus-offer-save-summaries ()
11107   "Offer to save all active summary buffers."
11108   (let (buffers)
11109     ;; Go through all buffers and find all summaries.
11110     (dolist (buffer (buffer-list))
11111       (when (and (setq buffer (buffer-name buffer))
11112                  (string-match "Summary" buffer)
11113                  (save-excursion
11114                    (set-buffer buffer)
11115                    ;; We check that this is, indeed, a summary buffer.
11116                    (and (eq major-mode 'gnus-summary-mode)
11117                         ;; Also make sure this isn't bogus.
11118                         gnus-newsgroup-prepared
11119                         ;; Also make sure that this isn't a
11120                         ;; dead summary buffer.
11121                         (not gnus-dead-summary-mode))))
11122         (push buffer buffers)))
11123     ;; Go through all these summary buffers and offer to save them.
11124     (when buffers
11125       (save-excursion
11126         (map-y-or-n-p
11127          "Update summary buffer %s? "
11128          (lambda (buf)
11129            (switch-to-buffer buf)
11130            (gnus-summary-exit))
11131          buffers)))))
11132
11133 (defun gnus-summary-setup-default-charset ()
11134   "Setup newsgroup default charset."
11135   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11136       (setq gnus-newsgroup-charset nil)
11137     (let* ((ignored-charsets
11138             (or gnus-newsgroup-ephemeral-ignored-charsets
11139                 (append
11140                  (and gnus-newsgroup-name
11141                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11142                  gnus-newsgroup-ignored-charsets))))
11143       (setq gnus-newsgroup-charset
11144             (or gnus-newsgroup-ephemeral-charset
11145                 (and gnus-newsgroup-name
11146                      (gnus-parameter-charset gnus-newsgroup-name))
11147                 gnus-default-charset))
11148       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11149            ignored-charsets))))
11150
11151 ;;;
11152 ;;; Mime Commands
11153 ;;;
11154
11155 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11156   "Display the current article buffer fully MIME-buttonized.
11157 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11158 treated as multipart/mixed."
11159   (interactive "P")
11160   (require 'gnus-art)
11161   (let ((gnus-unbuttonized-mime-types nil)
11162         (gnus-mime-display-multipart-as-mixed show-all-parts))
11163     (gnus-summary-show-article)))
11164
11165 (defun gnus-summary-repair-multipart (article)
11166   "Add a Content-Type header to a multipart article without one."
11167   (interactive (list (gnus-summary-article-number)))
11168   (gnus-with-article article
11169     (message-narrow-to-head)
11170     (message-remove-header "Mime-Version")
11171     (goto-char (point-max))
11172     (insert "Mime-Version: 1.0\n")
11173     (widen)
11174     (when (search-forward "\n--" nil t)
11175       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11176         (message-narrow-to-head)
11177         (message-remove-header "Content-Type")
11178         (goto-char (point-max))
11179         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11180                         separator))
11181         (widen))))
11182   (let (gnus-mark-article-hook)
11183     (gnus-summary-select-article t t nil article)))
11184
11185 (defun gnus-summary-toggle-display-buttonized ()
11186   "Toggle the buttonizing of the article buffer."
11187   (interactive)
11188   (require 'gnus-art)
11189   (if (setq gnus-inhibit-mime-unbuttonizing
11190             (not gnus-inhibit-mime-unbuttonizing))
11191       (let ((gnus-unbuttonized-mime-types nil))
11192         (gnus-summary-show-article))
11193     (gnus-summary-show-article)))
11194
11195 ;;;
11196 ;;; Generic summary marking commands
11197 ;;;
11198
11199 (defvar gnus-summary-marking-alist
11200   '((read gnus-del-mark "d")
11201     (unread gnus-unread-mark "u")
11202     (ticked gnus-ticked-mark "!")
11203     (dormant gnus-dormant-mark "?")
11204     (expirable gnus-expirable-mark "e"))
11205   "An alist of names/marks/keystrokes.")
11206
11207 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11208 (defvar gnus-summary-mark-map)
11209
11210 (defun gnus-summary-make-all-marking-commands ()
11211   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11212   (dolist (elem gnus-summary-marking-alist)
11213     (apply 'gnus-summary-make-marking-command elem)))
11214
11215 (defun gnus-summary-make-marking-command (name mark keystroke)
11216   (let ((map (make-sparse-keymap)))
11217     (define-key gnus-summary-generic-mark-map keystroke map)
11218     (dolist (lway `((next "next" next nil "n")
11219                     (next-unread "next unread" next t "N")
11220                     (prev "previous" prev nil "p")
11221                     (prev-unread "previous unread" prev t "P")
11222                     (nomove "" nil nil ,keystroke)))
11223       (let ((func (gnus-summary-make-marking-command-1
11224                    mark (car lway) lway name)))
11225         (setq func (eval func))
11226         (define-key map (nth 4 lway) func)))))
11227
11228 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11229   `(defun ,(intern
11230             (format "gnus-summary-put-mark-as-%s%s"
11231                     name (if (eq way 'nomove)
11232                              ""
11233                            (concat "-" (symbol-name way)))))
11234      (n)
11235      ,(format
11236        "Mark the current article as %s%s.
11237 If N, the prefix, then repeat N times.
11238 If N is negative, move in reverse order.
11239 The difference between N and the actual number of articles marked is
11240 returned."
11241        name (cadr lway))
11242      (interactive "p")
11243      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11244
11245 (defun gnus-summary-generic-mark (n mark move unread)
11246   "Mark N articles with MARK."
11247   (unless (eq major-mode 'gnus-summary-mode)
11248     (error "This command can only be used in the summary buffer"))
11249   (gnus-summary-show-thread)
11250   (let ((nummove
11251          (cond
11252           ((eq move 'next) 1)
11253           ((eq move 'prev) -1)
11254           (t 0))))
11255     (if (zerop nummove)
11256         (setq n 1)
11257       (when (< n 0)
11258         (setq n (abs n)
11259               nummove (* -1 nummove))))
11260     (while (and (> n 0)
11261                 (gnus-summary-mark-article nil mark)
11262                 (zerop (gnus-summary-next-subject nummove unread t)))
11263       (setq n (1- n)))
11264     (when (/= 0 n)
11265       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11266     (gnus-summary-recenter)
11267     (gnus-summary-position-point)
11268     (gnus-set-mode-line 'summary)
11269     n))
11270
11271 (defun gnus-summary-insert-articles (articles)
11272   (when (setq articles
11273               (gnus-sorted-difference articles
11274                                       (mapcar (lambda (h)
11275                                                 (mail-header-number h))
11276                                               gnus-newsgroup-headers)))
11277     (setq gnus-newsgroup-headers
11278           (merge 'list
11279                  gnus-newsgroup-headers
11280                  (gnus-fetch-headers articles)
11281                  'gnus-article-sort-by-number))
11282     ;; Suppress duplicates?
11283     (when gnus-suppress-duplicates
11284       (gnus-dup-suppress-articles))
11285
11286     ;; We might want to build some more threads first.
11287     (when (and gnus-fetch-old-headers
11288                (eq gnus-headers-retrieved-by 'nov))
11289       (if (eq gnus-fetch-old-headers 'invisible)
11290           (gnus-build-all-threads)
11291         (gnus-build-old-threads)))
11292     ;; Let the Gnus agent mark articles as read.
11293     (when gnus-agent
11294       (gnus-agent-get-undownloaded-list))
11295     ;; Remove list identifiers from subject
11296     (when gnus-list-identifiers
11297       (gnus-summary-remove-list-identifiers))
11298     ;; First and last article in this newsgroup.
11299     (when gnus-newsgroup-headers
11300       (setq gnus-newsgroup-begin
11301             (mail-header-number (car gnus-newsgroup-headers))
11302             gnus-newsgroup-end
11303             (mail-header-number
11304              (gnus-last-element gnus-newsgroup-headers))))
11305     (when gnus-use-scoring
11306       (gnus-possibly-score-headers))))
11307
11308 (defun gnus-summary-insert-old-articles (&optional all)
11309   "Insert all old articles in this group.
11310 If ALL is non-nil, already read articles become readable.
11311 If ALL is a number, fetch this number of articles."
11312   (interactive "P")
11313   (prog1
11314       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11315             older len)
11316         (setq older
11317               ;; Some nntp servers lie about their active range.  When
11318               ;; this happens, the active range can be in the millions.
11319               ;; Use a compressed range to avoid creating a huge list.
11320               (gnus-range-difference (list gnus-newsgroup-active) old))
11321         (setq len (gnus-range-length older))
11322         (cond
11323          ((null older) nil)
11324          ((numberp all)
11325           (if (< all len)
11326               (let ((older-range (nreverse older)))
11327                 (setq older nil)
11328
11329                 (while (> all 0)
11330                   (let* ((r (pop older-range))
11331                          (min (if (numberp r) r (car r)))
11332                          (max (if (numberp r) r (cdr r))))
11333                     (while (and (<= min max)
11334                                 (> all 0))
11335                       (push max older)
11336                       (setq all (1- all)
11337                             max (1- max))))))
11338             (setq older (gnus-uncompress-range older))))
11339          (all
11340           (setq older (gnus-uncompress-range older)))
11341          (t
11342           (when (and (numberp gnus-large-newsgroup)
11343                    (> len gnus-large-newsgroup))
11344               (let* ((cursor-in-echo-area nil)
11345                      (initial (gnus-parameter-large-newsgroup-initial
11346                                gnus-newsgroup-name))
11347                      (input
11348                       (read-string
11349                        (format
11350                         "How many articles from %s (%s %d): "
11351                         (gnus-limit-string
11352                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11353                         (if initial "max" "default")
11354                         len)
11355                        (if initial
11356                            (cons (number-to-string initial)
11357                                  0)))))
11358                 (unless (string-match "^[ \t]*$" input)
11359                   (setq all (string-to-number input))
11360                   (if (< all len)
11361                       (let ((older-range (nreverse older)))
11362                         (setq older nil)
11363
11364                         (while (> all 0)
11365                           (let* ((r (pop older-range))
11366                                  (min (if (numberp r) r (car r)))
11367                                  (max (if (numberp r) r (cdr r))))
11368                             (while (and (<= min max)
11369                                         (> all 0))
11370                               (push max older)
11371                               (setq all (1- all)
11372                                     max (1- max))))))))))
11373           (setq older (gnus-uncompress-range older))))
11374         (if (not older)
11375             (message "No old news.")
11376           (gnus-summary-insert-articles older)
11377           (gnus-summary-limit (gnus-sorted-nunion old older))))
11378     (gnus-summary-position-point)))
11379
11380 (defun gnus-summary-insert-new-articles ()
11381   "Insert all new articles in this group."
11382   (interactive)
11383   (prog1
11384       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11385             (old-active gnus-newsgroup-active)
11386             (nnmail-fetched-sources (list t))
11387             i new)
11388         (setq gnus-newsgroup-active
11389               (gnus-activate-group gnus-newsgroup-name 'scan))
11390         (setq i (cdr gnus-newsgroup-active))
11391         (while (> i (cdr old-active))
11392           (push i new)
11393           (decf i))
11394         (if (not new)
11395             (message "No gnus is bad news.")
11396           (gnus-summary-insert-articles new)
11397           (setq gnus-newsgroup-unreads
11398                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11399           (gnus-summary-limit (gnus-sorted-nunion old new))))
11400     (gnus-summary-position-point)))
11401
11402 (gnus-summary-make-all-marking-commands)
11403
11404 (gnus-ems-redefine)
11405
11406 (provide 'gnus-sum)
11407
11408 (run-hooks 'gnus-sum-load-hook)
11409
11410 ;;; gnus-sum.el ends here