f8594daa2eb5ada6bba8c1c6b25939438fdb2712
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 (require 'nnoo)
40
41 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
42 (autoload 'gnus-cache-write-active "gnus-cache")
43 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
44 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
45 (autoload 'mm-uu-dissect "mm-uu")
46 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
47   "Deuglify broken Outlook (Express) articles and redisplay."
48   t)
49 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
50 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
51 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
52
53 (defcustom gnus-kill-summary-on-exit t
54   "*If non-nil, kill the summary buffer when you exit from it.
55 If nil, the summary will become a \"*Dead Summary*\" buffer, and
56 it will be killed sometime later."
57   :group 'gnus-summary-exit
58   :type 'boolean)
59
60 (defcustom gnus-fetch-old-headers nil
61   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
62 If an unread article in the group refers to an older, already read (or
63 just marked as read) article, the old article will not normally be
64 displayed in the Summary buffer.  If this variable is non-nil, Gnus
65 will attempt to grab the headers to the old articles, and thereby
66 build complete threads.  If it has the value `some', only enough
67 headers to connect otherwise loose threads will be displayed.  This
68 variable can also be a number.  In that case, no more than that number
69 of old headers will be fetched.  If it has the value `invisible', all
70 old headers will be fetched, but none will be displayed.
71
72 The server has to support NOV for any of this to work."
73   :group 'gnus-thread
74   :type '(choice (const :tag "off" nil)
75                  (const some)
76                  number
77                  (sexp :menu-tag "other" t)))
78
79 (defcustom gnus-refer-thread-limit 200
80   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
81 If t, fetch all the available old headers."
82   :group 'gnus-thread
83   :type '(choice number
84                  (sexp :menu-tag "other" t)))
85
86 (defcustom gnus-summary-make-false-root 'adopt
87   "*nil means that Gnus won't gather loose threads.
88 If the root of a thread has expired or been read in a previous
89 session, the information necessary to build a complete thread has been
90 lost.  Instead of having many small sub-threads from this original thread
91 scattered all over the summary buffer, Gnus can gather them.
92
93 If non-nil, Gnus will try to gather all loose sub-threads from an
94 original thread into one large thread.
95
96 If this variable is non-nil, it should be one of `none', `adopt',
97 `dummy' or `empty'.
98
99 If this variable is `none', Gnus will not make a false root, but just
100 present the sub-threads after another.
101 If this variable is `dummy', Gnus will create a dummy root that will
102 have all the sub-threads as children.
103 If this variable is `adopt', Gnus will make one of the \"children\"
104 the parent and mark all the step-children as such.
105 If this variable is `empty', the \"children\" are printed with empty
106 subject fields.  (Or rather, they will be printed with a string
107 given by the `gnus-summary-same-subject' variable.)"
108   :group 'gnus-thread
109   :type '(choice (const :tag "off" nil)
110                  (const none)
111                  (const dummy)
112                  (const adopt)
113                  (const empty)))
114
115 (defcustom gnus-summary-make-false-root-always nil
116   "Always make a false dummy root."
117   :group 'gnus-thread
118   :type 'boolean)
119
120 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
121   "*A regexp to match subjects to be excluded from loose thread gathering.
122 As loose thread gathering is done on subjects only, that means that
123 there can be many false gatherings performed.  By rooting out certain
124 common subjects, gathering might become saner."
125   :group 'gnus-thread
126   :type 'regexp)
127
128 (defcustom gnus-summary-gather-subject-limit nil
129   "*Maximum length of subject comparisons when gathering loose threads.
130 Use nil to compare full subjects.  Setting this variable to a low
131 number will help gather threads that have been corrupted by
132 newsreaders chopping off subject lines, but it might also mean that
133 unrelated articles that have subject that happen to begin with the
134 same few characters will be incorrectly gathered.
135
136 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
137 comparing subjects."
138   :group 'gnus-thread
139   :type '(choice (const :tag "off" nil)
140                  (const fuzzy)
141                  (sexp :menu-tag "on" t)))
142
143 (defcustom gnus-simplify-subject-functions nil
144   "List of functions taking a string argument that simplify subjects.
145 The functions are applied recursively.
146
147 Useful functions to put in this list include:
148 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
149 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
150   :group 'gnus-thread
151   :type '(repeat function))
152
153 (defcustom gnus-simplify-ignored-prefixes nil
154   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
155   :group 'gnus-thread
156   :type '(choice (const :tag "off" nil)
157                  regexp))
158
159 (defcustom gnus-build-sparse-threads nil
160   "*If non-nil, fill in the gaps in threads.
161 If `some', only fill in the gaps that are needed to tie loose threads
162 together.  If `more', fill in all leaf nodes that Gnus can find.  If
163 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
164   :group 'gnus-thread
165   :type '(choice (const :tag "off" nil)
166                  (const some)
167                  (const more)
168                  (sexp :menu-tag "all" t)))
169
170 (defcustom gnus-summary-thread-gathering-function
171   'gnus-gather-threads-by-subject
172   "*Function used for gathering loose threads.
173 There are two pre-defined functions: `gnus-gather-threads-by-subject',
174 which only takes Subjects into consideration; and
175 `gnus-gather-threads-by-references', which compared the References
176 headers of the articles to find matches."
177   :group 'gnus-thread
178   :type '(radio (function-item gnus-gather-threads-by-subject)
179                 (function-item gnus-gather-threads-by-references)
180                 (function :tag "other")))
181
182 (defcustom gnus-summary-same-subject ""
183   "*String indicating that the current article has the same subject as the previous.
184 This variable will only be used if the value of
185 `gnus-summary-make-false-root' is `empty'."
186   :group 'gnus-summary-format
187   :type 'string)
188
189 (defcustom gnus-summary-goto-unread t
190   "*If t, many commands will go to the next unread article.
191 This applies to marking commands as well as other commands that
192 \"naturally\" select the next article, like, for instance, `SPC' at
193 the end of an article.
194
195 If nil, the marking commands do NOT go to the next unread article
196 \(they go to the next article instead).  If `never', commands that
197 usually go to the next unread article, will go to the next article,
198 whether it is read or not."
199   :group 'gnus-summary-marks
200   :link '(custom-manual "(gnus)Setting Marks")
201   :type '(choice (const :tag "off" nil)
202                  (const never)
203                  (sexp :menu-tag "on" t)))
204
205 (defcustom gnus-summary-default-score 0
206   "*Default article score level.
207 All scores generated by the score files will be added to this score.
208 If this variable is nil, scoring will be disabled."
209   :group 'gnus-score-default
210   :type '(choice (const :tag "disable")
211                  integer))
212
213 (defcustom gnus-summary-default-high-score 0
214   "*Default threshold for a high scored article.
215 An article will be highlighted as high scored if its score is greater
216 than this score."
217   :group 'gnus-score-default
218   :type 'integer)
219
220 (defcustom gnus-summary-default-low-score 0
221   "*Default threshold for a low scored article.
222 An article will be highlighted as low scored if its score is smaller
223 than this score."
224   :group 'gnus-score-default
225   :type 'integer)
226
227 (defcustom gnus-summary-zcore-fuzz 0
228   "*Fuzziness factor for the zcore in the summary buffer.
229 Articles with scores closer than this to `gnus-summary-default-score'
230 will not be marked."
231   :group 'gnus-summary-format
232   :type 'integer)
233
234 (defcustom gnus-simplify-subject-fuzzy-regexp nil
235   "*Strings to be removed when doing fuzzy matches.
236 This can either be a regular expression or list of regular expressions
237 that will be removed from subject strings if fuzzy subject
238 simplification is selected."
239   :group 'gnus-thread
240   :type '(repeat regexp))
241
242 (defcustom gnus-show-threads t
243   "*If non-nil, display threads in summary mode."
244   :group 'gnus-thread
245   :type 'boolean)
246
247 (defcustom gnus-thread-hide-subtree nil
248   "*If non-nil, hide all threads initially.
249 This can be a predicate specifier which says which threads to hide.
250 If threads are hidden, you have to run the command
251 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
252 to expose hidden threads."
253   :group 'gnus-thread
254   :type 'boolean)
255
256 (defcustom gnus-thread-hide-killed t
257   "*If non-nil, hide killed threads automatically."
258   :group 'gnus-thread
259   :type 'boolean)
260
261 (defcustom gnus-thread-ignore-subject t
262   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
263 If nil, articles that have different subjects from their parents will
264 start separate threads."
265   :group 'gnus-thread
266   :type 'boolean)
267
268 (defcustom gnus-thread-operation-ignore-subject t
269   "*If non-nil, subjects will be ignored when doing thread commands.
270 This affects commands like `gnus-summary-kill-thread' and
271 `gnus-summary-lower-thread'.
272
273 If this variable is nil, articles in the same thread with different
274 subjects will not be included in the operation in question.  If this
275 variable is `fuzzy', only articles that have subjects that are fuzzily
276 equal will be included."
277   :group 'gnus-thread
278   :type '(choice (const :tag "off" nil)
279                  (const fuzzy)
280                  (sexp :tag "on" t)))
281
282 (defcustom gnus-thread-indent-level 4
283   "*Number that says how much each sub-thread should be indented."
284   :group 'gnus-thread
285   :type 'integer)
286
287 (defcustom gnus-auto-extend-newsgroup t
288   "*If non-nil, extend newsgroup forward and backward when requested."
289   :group 'gnus-summary-choose
290   :type 'boolean)
291
292 (defcustom gnus-auto-select-first t
293   "*If non-nil, select the article under point.
294 Which article this is is controlled by the `gnus-auto-select-subject'
295 variable.
296
297 If you want to prevent automatic selection of articles in some
298 newsgroups, set the variable to nil in `gnus-select-group-hook'."
299   :group 'gnus-group-select
300   :type '(choice (const :tag "none" nil)
301                  (sexp :menu-tag "first" t)))
302
303 (defcustom gnus-auto-select-subject 'unread
304   "*Says what subject to place under point when entering a group.
305
306 This variable can either be the symbols `first' (place point on the
307 first subject), `unread' (place point on the subject line of the first
308 unread article), `best' (place point on the subject line of the
309 higest-scored article), `unseen' (place point on the subject line of
310 the first unseen article), 'unseen-or-unread' (place point on the subject
311 line of the first unseen article or, if all article have been seen, on the
312 subject line of the first unread article), or a function to be called to
313 place point on some subject line."
314   :group 'gnus-group-select
315   :type '(choice (const best)
316                  (const unread)
317                  (const first)
318                  (const unseen)
319                  (const unseen-or-unread)))
320
321 (defcustom gnus-auto-select-next t
322   "*If non-nil, offer to go to the next group from the end of the previous.
323 If the value is t and the next newsgroup is empty, Gnus will exit
324 summary mode and go back to group mode.  If the value is neither nil
325 nor t, Gnus will select the following unread newsgroup.  In
326 particular, if the value is the symbol `quietly', the next unread
327 newsgroup will be selected without any confirmation, and if it is
328 `almost-quietly', the next group will be selected without any
329 confirmation if you are located on the last article in the group.
330 Finally, if this variable is `slightly-quietly', the `Z n' command
331 will go to the next group without confirmation."
332   :group 'gnus-summary-maneuvering
333   :type '(choice (const :tag "off" nil)
334                  (const quietly)
335                  (const almost-quietly)
336                  (const slightly-quietly)
337                  (sexp :menu-tag "on" t)))
338
339 (defcustom gnus-auto-select-same nil
340   "*If non-nil, select the next article with the same subject.
341 If there are no more articles with the same subject, go to
342 the first unread article."
343   :group 'gnus-summary-maneuvering
344   :type 'boolean)
345
346 (defcustom gnus-summary-check-current nil
347   "*If non-nil, consider the current article when moving.
348 The \"unread\" movement commands will stay on the same line if the
349 current article is unread."
350   :group 'gnus-summary-maneuvering
351   :type 'boolean)
352
353 (defcustom gnus-auto-center-summary t
354   "*If non-nil, always center the current summary buffer.
355 In particular, if `vertical' do only vertical recentering.  If non-nil
356 and non-`vertical', do both horizontal and vertical recentering."
357   :group 'gnus-summary-maneuvering
358   :type '(choice (const :tag "none" nil)
359                  (const vertical)
360                  (integer :tag "height")
361                  (sexp :menu-tag "both" t)))
362
363 (defcustom gnus-show-all-headers nil
364   "*If non-nil, don't hide any headers."
365   :group 'gnus-article-hiding
366   :group 'gnus-article-headers
367   :type 'boolean)
368
369 (defcustom gnus-summary-ignore-duplicates nil
370   "*If non-nil, ignore articles with identical Message-ID headers."
371   :group 'gnus-summary
372   :type 'boolean)
373
374 (defcustom gnus-single-article-buffer t
375   "*If non-nil, display all articles in the same buffer.
376 If nil, each group will get its own article buffer."
377   :group 'gnus-article-various
378   :type 'boolean)
379
380 (defcustom gnus-break-pages t
381   "*If non-nil, do page breaking on articles.
382 The page delimiter is specified by the `gnus-page-delimiter'
383 variable."
384   :group 'gnus-article-various
385   :type 'boolean)
386
387 (defcustom gnus-move-split-methods nil
388   "*Variable used to suggest where articles are to be moved to.
389 It uses the same syntax as the `gnus-split-methods' variable.
390 However, whereas `gnus-split-methods' specifies file names as targets,
391 this variable specifies group names."
392   :group 'gnus-summary-mail
393   :type '(repeat (choice (list :value (fun) function)
394                          (cons :value ("" "") regexp (repeat string))
395                          (sexp :value nil))))
396
397 (defcustom gnus-unread-mark ?           ;Whitespace
398   "*Mark used for unread articles."
399   :group 'gnus-summary-marks
400   :type 'character)
401
402 (defcustom gnus-ticked-mark ?!
403   "*Mark used for ticked articles."
404   :group 'gnus-summary-marks
405   :type 'character)
406
407 (defcustom gnus-dormant-mark ??
408   "*Mark used for dormant articles."
409   :group 'gnus-summary-marks
410   :type 'character)
411
412 (defcustom gnus-del-mark ?r
413   "*Mark used for del'd articles."
414   :group 'gnus-summary-marks
415   :type 'character)
416
417 (defcustom gnus-read-mark ?R
418   "*Mark used for read articles."
419   :group 'gnus-summary-marks
420   :type 'character)
421
422 (defcustom gnus-expirable-mark ?E
423   "*Mark used for expirable articles."
424   :group 'gnus-summary-marks
425   :type 'character)
426
427 (defcustom gnus-killed-mark ?K
428   "*Mark used for killed articles."
429   :group 'gnus-summary-marks
430   :type 'character)
431
432 (defcustom gnus-spam-mark ?H
433   "*Mark used for spam articles."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-souped-mark ?F
438   "*Mark used for souped articles."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-kill-file-mark ?X
443   "*Mark used for articles killed by kill files."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-low-score-mark ?Y
448   "*Mark used for articles with a low score."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-catchup-mark ?C
453   "*Mark used for articles that are caught up."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-replied-mark ?A
458   "*Mark used for articles that have been replied to."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-forwarded-mark ?F
463   "*Mark used for articles that have been forwarded."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-recent-mark ?N
468   "*Mark used for articles that are recent."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-cached-mark ?*
473   "*Mark used for articles that are in the cache."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-saved-mark ?S
478   "*Mark used for articles that have been saved."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-unseen-mark ?.
483   "*Mark used for articles that haven't been seen."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-no-mark ?               ;Whitespace
488   "*Mark used for articles that have no other secondary mark."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-ancient-mark ?O
493   "*Mark used for ancient articles."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-sparse-mark ?Q
498   "*Mark used for sparsely reffed articles."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-canceled-mark ?G
503   "*Mark used for canceled articles."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-duplicate-mark ?M
508   "*Mark used for duplicate articles."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-undownloaded-mark ?-
513   "*Mark used for articles that weren't downloaded."
514   :group 'gnus-summary-marks
515   :type 'character)
516
517 (defcustom gnus-downloaded-mark ?+
518   "*Mark used for articles that were downloaded."
519   :group 'gnus-summary-marks
520   :type 'character)
521
522 (defcustom gnus-downloadable-mark ?%
523   "*Mark used for articles that are to be downloaded."
524   :group 'gnus-summary-marks
525   :type 'character)
526
527 (defcustom gnus-unsendable-mark ?=
528   "*Mark used for articles that won't be sent."
529   :group 'gnus-summary-marks
530   :type 'character)
531
532 (defcustom gnus-score-over-mark ?+
533   "*Score mark used for articles with high scores."
534   :group 'gnus-summary-marks
535   :type 'character)
536
537 (defcustom gnus-score-below-mark ?-
538   "*Score mark used for articles with low scores."
539   :group 'gnus-summary-marks
540   :type 'character)
541
542 (defcustom gnus-empty-thread-mark ?     ;Whitespace
543   "*There is no thread under the article."
544   :group 'gnus-summary-marks
545   :type 'character)
546
547 (defcustom gnus-not-empty-thread-mark ?=
548   "*There is a thread under the article."
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-view-pseudo-asynchronously nil
553   "*If non-nil, Gnus will view pseudo-articles asynchronously."
554   :group 'gnus-extract-view
555   :type 'boolean)
556
557 (defcustom gnus-auto-expirable-marks
558   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
559         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
560         gnus-souped-mark gnus-duplicate-mark)
561   "*The list of marks converted into expiration if a group is auto-expirable."
562   :version "21.1"
563   :group 'gnus-summary
564   :type '(repeat character))
565
566 (defcustom gnus-inhibit-user-auto-expire t
567   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
568   :version "21.1"
569   :group 'gnus-summary
570   :type 'boolean)
571
572 (defcustom gnus-view-pseudos nil
573   "*If `automatic', pseudo-articles will be viewed automatically.
574 If `not-confirm', pseudos will be viewed automatically, and the user
575 will not be asked to confirm the command."
576   :group 'gnus-extract-view
577   :type '(choice (const :tag "off" nil)
578                  (const automatic)
579                  (const not-confirm)))
580
581 (defcustom gnus-view-pseudos-separately t
582   "*If non-nil, one pseudo-article will be created for each file to be viewed.
583 If nil, all files that use the same viewing command will be given as a
584 list of parameters to that command."
585   :group 'gnus-extract-view
586   :type 'boolean)
587
588 (defcustom gnus-insert-pseudo-articles t
589   "*If non-nil, insert pseudo-articles when decoding articles."
590   :group 'gnus-extract-view
591   :type 'boolean)
592
593 (defcustom gnus-summary-dummy-line-format
594   "   %(:                             :%) %S\n"
595   "*The format specification for the dummy roots in the summary buffer.
596 It works along the same lines as a normal formatting string,
597 with some simple extensions.
598
599 %S  The subject
600
601 General format specifiers can also be used.
602 See `(gnus)Formatting Variables'."
603   :link '(custom-manual "(gnus)Formatting Variables")
604   :group 'gnus-threading
605   :type 'string)
606
607 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
608   "*The format specification for the summary mode line.
609 It works along the same lines as a normal formatting string,
610 with some simple extensions:
611
612 %G  Group name
613 %p  Unprefixed group name
614 %A  Current article number
615 %z  Current article score
616 %V  Gnus version
617 %U  Number of unread articles in the group
618 %e  Number of unselected articles in the group
619 %Z  A string with unread/unselected article counts
620 %g  Shortish group name
621 %S  Subject of the current article
622 %u  User-defined spec
623 %s  Current score file name
624 %d  Number of dormant articles
625 %r  Number of articles that have been marked as read in this session
626 %E  Number of articles expunged by the score files"
627   :group 'gnus-summary-format
628   :type 'string)
629
630 (defcustom gnus-list-identifiers nil
631   "Regexp that matches list identifiers to be removed from subject.
632 This can also be a list of regexps."
633   :version "21.1"
634   :group 'gnus-summary-format
635   :group 'gnus-article-hiding
636   :type '(choice (const :tag "none" nil)
637                  (regexp :value ".*")
638                  (repeat :value (".*") regexp)))
639
640 (defcustom gnus-summary-mark-below 0
641   "*Mark all articles with a score below this variable as read.
642 This variable is local to each summary buffer and usually set by the
643 score file."
644   :group 'gnus-score-default
645   :type 'integer)
646
647 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
648   "*List of functions used for sorting articles in the summary buffer.
649
650 Each function takes two articles and returns non-nil if the first
651 article should be sorted before the other.  If you use more than one
652 function, the primary sort function should be the last.  You should
653 probably always include `gnus-article-sort-by-number' in the list of
654 sorting functions -- preferably first.  Also note that sorting by date
655 is often much slower than sorting by number, and the sorting order is
656 very similar.  (Sorting by date means sorting by the time the message
657 was sent, sorting by number means sorting by arrival time.)
658
659 Ready-made functions include `gnus-article-sort-by-number',
660 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
661 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
662 and `gnus-article-sort-by-score'.
663
664 When threading is turned on, the variable `gnus-thread-sort-functions'
665 controls how articles are sorted."
666   :group 'gnus-summary-sort
667   :type '(repeat (choice (function-item gnus-article-sort-by-number)
668                          (function-item gnus-article-sort-by-author)
669                          (function-item gnus-article-sort-by-subject)
670                          (function-item gnus-article-sort-by-date)
671                          (function-item gnus-article-sort-by-score)
672                          (function-item gnus-article-sort-by-random)
673                          (function :tag "other"))))
674
675 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
676   "*List of functions used for sorting threads in the summary buffer.
677 By default, threads are sorted by article number.
678
679 Each function takes two threads and returns non-nil if the first
680 thread should be sorted before the other.  If you use more than one
681 function, the primary sort function should be the last.  You should
682 probably always include `gnus-thread-sort-by-number' in the list of
683 sorting functions -- preferably first.  Also note that sorting by date
684 is often much slower than sorting by number, and the sorting order is
685 very similar.  (Sorting by date means sorting by the time the message
686 was sent, sorting by number means sorting by arrival time.)
687
688 Ready-made functions include `gnus-thread-sort-by-number',
689 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
690 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
691 `gnus-thread-sort-by-most-recent-number',
692 `gnus-thread-sort-by-most-recent-date',
693 `gnus-thread-sort-by-random', and
694 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
695
696 When threading is turned off, the variable
697 `gnus-article-sort-functions' controls how articles are sorted."
698   :group 'gnus-summary-sort
699   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
700                          (function-item gnus-thread-sort-by-author)
701                          (function-item gnus-thread-sort-by-subject)
702                          (function-item gnus-thread-sort-by-date)
703                          (function-item gnus-thread-sort-by-score)
704                          (function-item gnus-thread-sort-by-total-score)
705                          (function-item gnus-thread-sort-by-random)
706                          (function :tag "other"))))
707
708 (defcustom gnus-thread-score-function '+
709   "*Function used for calculating the total score of a thread.
710
711 The function is called with the scores of the article and each
712 subthread and should then return the score of the thread.
713
714 Some functions you can use are `+', `max', or `min'."
715   :group 'gnus-summary-sort
716   :type 'function)
717
718 (defcustom gnus-summary-expunge-below nil
719   "All articles that have a score less than this variable will be expunged.
720 This variable is local to the summary buffers."
721   :group 'gnus-score-default
722   :type '(choice (const :tag "off" nil)
723                  integer))
724
725 (defcustom gnus-thread-expunge-below nil
726   "All threads that have a total score less than this variable will be expunged.
727 See `gnus-thread-score-function' for en explanation of what a
728 \"thread score\" is.
729
730 This variable is local to the summary buffers."
731   :group 'gnus-threading
732   :group 'gnus-score-default
733   :type '(choice (const :tag "off" nil)
734                  integer))
735
736 (defcustom gnus-summary-mode-hook nil
737   "*A hook for Gnus summary mode.
738 This hook is run before any variables are set in the summary buffer."
739   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
740   :group 'gnus-summary-various
741   :type 'hook)
742
743 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
744 (when (featurep 'xemacs)
745   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
746   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
747   (add-hook 'gnus-summary-mode-hook
748             'gnus-xmas-switch-horizontal-scrollbar-off))
749
750 (defcustom gnus-summary-menu-hook nil
751   "*Hook run after the creation of the summary mode menu."
752   :group 'gnus-summary-visual
753   :type 'hook)
754
755 (defcustom gnus-summary-exit-hook nil
756   "*A hook called on exit from the summary buffer.
757 It will be called with point in the group buffer."
758   :group 'gnus-summary-exit
759   :type 'hook)
760
761 (defcustom gnus-summary-prepare-hook nil
762   "*A hook called after the summary buffer has been generated.
763 If you want to modify the summary buffer, you can use this hook."
764   :group 'gnus-summary-various
765   :type 'hook)
766
767 (defcustom gnus-summary-prepared-hook nil
768   "*A hook called as the last thing after the summary buffer has been generated."
769   :group 'gnus-summary-various
770   :type 'hook)
771
772 (defcustom gnus-summary-generate-hook nil
773   "*A hook run just before generating the summary buffer.
774 This hook is commonly used to customize threading variables and the
775 like."
776   :group 'gnus-summary-various
777   :type 'hook)
778
779 (defcustom gnus-select-group-hook nil
780   "*A hook called when a newsgroup is selected.
781
782 If you'd like to simplify subjects like the
783 `gnus-summary-next-same-subject' command does, you can use the
784 following hook:
785
786  (add-hook gnus-select-group-hook
787            (lambda ()
788              (mapcar (lambda (header)
789                        (mail-header-set-subject
790                         header
791                         (gnus-simplify-subject
792                          (mail-header-subject header) 're-only)))
793                      gnus-newsgroup-headers)))"
794   :group 'gnus-group-select
795   :type 'hook)
796
797 (defcustom gnus-select-article-hook nil
798   "*A hook called when an article is selected."
799   :group 'gnus-summary-choose
800   :options '(gnus-agent-fetch-selected-article)
801   :type 'hook)
802
803 (defcustom gnus-visual-mark-article-hook
804   (list 'gnus-highlight-selected-summary)
805   "*Hook run after selecting an article in the summary buffer.
806 It is meant to be used for highlighting the article in some way.  It
807 is not run if `gnus-visual' is nil."
808   :group 'gnus-summary-visual
809   :type 'hook)
810
811 (defcustom gnus-parse-headers-hook nil
812   "*A hook called before parsing the headers."
813   :group 'gnus-various
814   :type 'hook)
815
816 (defcustom gnus-exit-group-hook nil
817   "*A hook called when exiting summary mode.
818 This hook is not called from the non-updating exit commands like `Q'."
819   :group 'gnus-various
820   :type 'hook)
821
822 (defcustom gnus-summary-update-hook
823   (list 'gnus-summary-highlight-line)
824   "*A hook called when a summary line is changed.
825 The hook will not be called if `gnus-visual' is nil.
826
827 The default function `gnus-summary-highlight-line' will
828 highlight the line according to the `gnus-summary-highlight'
829 variable."
830   :group 'gnus-summary-visual
831   :type 'hook)
832
833 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
834   "*A hook called when an article is selected for the first time.
835 The hook is intended to mark an article as read (or unread)
836 automatically when it is selected."
837   :group 'gnus-summary-choose
838   :type 'hook)
839
840 (defcustom gnus-group-no-more-groups-hook nil
841   "*A hook run when returning to group mode having no more (unread) groups."
842   :group 'gnus-group-select
843   :type 'hook)
844
845 (defcustom gnus-ps-print-hook nil
846   "*A hook run before ps-printing something from Gnus."
847   :group 'gnus-summary
848   :type 'hook)
849
850 (defcustom gnus-summary-display-arrow
851   (and (fboundp 'display-graphic-p)
852        (display-graphic-p))
853   "*If non-nil, display an arrow highlighting the current article."
854   :version "21.1"
855   :group 'gnus-summary
856   :type 'boolean)
857
858 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
859   "Face used for highlighting the current article in the summary buffer."
860   :group 'gnus-summary-visual
861   :type 'face)
862
863 (defvar gnus-tmp-downloaded nil)
864
865 (defcustom gnus-summary-highlight
866   '(((eq mark gnus-canceled-mark)
867      . gnus-summary-cancelled-face)
868     ((and uncached (> score default-high))
869      . gnus-summary-high-undownloaded-face)
870     ((and uncached (< score default-low))
871      . gnus-summary-low-undownloaded-face)
872     (uncached
873      . gnus-summary-normal-undownloaded-face)
874     ((and (> score default-high)
875           (or (eq mark gnus-dormant-mark)
876               (eq mark gnus-ticked-mark)))
877      . gnus-summary-high-ticked-face)
878     ((and (< score default-low)
879           (or (eq mark gnus-dormant-mark)
880               (eq mark gnus-ticked-mark)))
881      . gnus-summary-low-ticked-face)
882     ((or (eq mark gnus-dormant-mark)
883          (eq mark gnus-ticked-mark))
884      . gnus-summary-normal-ticked-face)
885     ((and (> score default-high) (eq mark gnus-ancient-mark))
886      . gnus-summary-high-ancient-face)
887     ((and (< score default-low) (eq mark gnus-ancient-mark))
888      . gnus-summary-low-ancient-face)
889     ((eq mark gnus-ancient-mark)
890      . gnus-summary-normal-ancient-face)
891     ((and (> score default-high) (eq mark gnus-unread-mark))
892      . gnus-summary-high-unread-face)
893     ((and (< score default-low) (eq mark gnus-unread-mark))
894      . gnus-summary-low-unread-face)
895     ((eq mark gnus-unread-mark)
896      . gnus-summary-normal-unread-face)
897     ((> score default-high)
898      . gnus-summary-high-read-face)
899     ((< score default-low)
900      . gnus-summary-low-read-face)
901     (t
902      . gnus-summary-normal-read-face))
903   "*Controls the highlighting of summary buffer lines.
904
905 A list of (FORM . FACE) pairs.  When deciding how a a particular
906 summary line should be displayed, each form is evaluated.  The content
907 of the face field after the first true form is used.  You can change
908 how those summary lines are displayed, by editing the face field.
909
910 You can use the following variables in the FORM field.
911
912 score:        The article's score
913 default:      The default article score.
914 default-high: The default score for high scored articles.
915 default-low:  The default score for low scored articles.
916 below:        The score below which articles are automatically marked as read.
917 mark:         The articles mark."
918   :group 'gnus-summary-visual
919   :type '(repeat (cons (sexp :tag "Form" nil)
920                        face)))
921
922 (defcustom gnus-alter-header-function nil
923   "Function called to allow alteration of article header structures.
924 The function is called with one parameter, the article header vector,
925 which it may alter in any way."
926   :type '(choice (const :tag "None" nil)
927                  function)
928   :group 'gnus-summary)
929
930 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
931   "Variable that says which function should be used to decode a string with encoded words.")
932
933 (defcustom gnus-extra-headers '(To Newsgroups)
934   "*Extra headers to parse."
935   :version "21.1"
936   :group 'gnus-summary
937   :type '(repeat symbol))
938
939 (defcustom gnus-ignored-from-addresses
940   (and user-mail-address (regexp-quote user-mail-address))
941   "*Regexp of From headers that may be suppressed in favor of To headers."
942   :version "21.1"
943   :group 'gnus-summary
944   :type 'regexp)
945
946 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
947   "List of charsets that should be ignored.
948 When these charsets are used in the \"charset\" parameter, the
949 default charset will be used instead."
950   :version "21.1"
951   :type '(repeat symbol)
952   :group 'gnus-charset)
953
954 (gnus-define-group-parameter
955  ignored-charsets
956  :type list
957  :function-document
958  "Return the ignored charsets of GROUP."
959  :variable gnus-group-ignored-charsets-alist
960  :variable-default
961  '(("alt\\.chinese\\.text" iso-8859-1))
962  :variable-document
963  "Alist of regexps (to match group names) and charsets that should be ignored.
964 When these charsets are used in the \"charset\" parameter, the
965 default charset will be used instead."
966  :variable-group gnus-charset
967  :variable-type '(repeat (cons (regexp :tag "Group")
968                                (repeat symbol)))
969  :parameter-type '(choice :tag "Ignored charsets"
970                           :value nil
971                           (repeat (symbol)))
972  :parameter-document       "\
973 List of charsets that should be ignored.
974
975 When these charsets are used in the \"charset\" parameter, the
976 default charset will be used instead.")
977
978 (defcustom gnus-group-highlight-words-alist nil
979   "Alist of group regexps and highlight regexps.
980 This variable uses the same syntax as `gnus-emphasis-alist'."
981   :version "21.1"
982   :type '(repeat (cons (regexp :tag "Group")
983                        (repeat (list (regexp :tag "Highlight regexp")
984                                      (number :tag "Group for entire word" 0)
985                                      (number :tag "Group for displayed part" 0)
986                                      (symbol :tag "Face"
987                                              gnus-emphasis-highlight-words)))))
988   :group 'gnus-summary-visual)
989
990 (defcustom gnus-summary-show-article-charset-alist
991   nil
992   "Alist of number and charset.
993 The article will be shown with the charset corresponding to the
994 numbered argument.
995 For example: ((1 . cn-gb-2312) (2 . big5))."
996   :version "21.1"
997   :type '(repeat (cons (number :tag "Argument" 1)
998                        (symbol :tag "Charset")))
999   :group 'gnus-charset)
1000
1001 (defcustom gnus-preserve-marks t
1002   "Whether marks are preserved when moving, copying and respooling messages."
1003   :version "21.1"
1004   :type 'boolean
1005   :group 'gnus-summary-marks)
1006
1007 (defcustom gnus-alter-articles-to-read-function nil
1008   "Function to be called to alter the list of articles to be selected."
1009   :type '(choice (const nil) function)
1010   :group 'gnus-summary)
1011
1012 (defcustom gnus-orphan-score nil
1013   "*All orphans get this score added.  Set in the score file."
1014   :group 'gnus-score-default
1015   :type '(choice (const nil)
1016                  integer))
1017
1018 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1019   "*A regexp to match MIME parts when saving multiple parts of a message
1020 with gnus-summary-save-parts (X m). This regexp will be used by default
1021 when prompting the user for which type of files to save."
1022   :group 'gnus-summary
1023   :type 'regexp)
1024
1025 (defcustom gnus-read-all-available-headers nil
1026   "Whether Gnus should parse all headers made available to it.
1027 This is mostly relevant for slow backends where the user may
1028 wish to widen the summary buffer to include all headers
1029 that were fetched.  Say, for nnultimate groups."
1030   :group 'gnus-summary
1031   :type '(choice boolean regexp))
1032
1033 (defcustom gnus-summary-muttprint-program "muttprint"
1034   "Command (and optional arguments) used to run Muttprint."
1035   :version "21.3"
1036   :group 'gnus-summary
1037   :type 'string)
1038
1039 (defcustom gnus-article-loose-mime nil
1040   "If non-nil, don't require MIME-Version header.
1041 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1042 supply the MIME-Version header or deliberately strip it From the mail.
1043 Set it to non-nil, Gnus will treat some articles as MIME even if
1044 the MIME-Version header is missed."
1045   :version "21.3"
1046   :type 'boolean
1047   :group 'gnus-article)
1048
1049 (defcustom gnus-article-emulate-mime t
1050   "If non-nil, use MIME emulation for uuencode and the like.
1051 This means that Gnus will search message bodies for text that look
1052 like uuencoded bits, yEncoded bits, and so on, and present that using
1053 the normal Gnus MIME machinery."
1054   :type 'boolean
1055   :group 'gnus-article)
1056
1057 ;;; Internal variables
1058
1059 (defvar gnus-summary-display-cache nil)
1060 (defvar gnus-article-mime-handles nil)
1061 (defvar gnus-article-decoded-p nil)
1062 (defvar gnus-article-charset nil)
1063 (defvar gnus-article-ignored-charsets nil)
1064 (defvar gnus-scores-exclude-files nil)
1065 (defvar gnus-page-broken nil)
1066
1067 (defvar gnus-original-article nil)
1068 (defvar gnus-article-internal-prepare-hook nil)
1069 (defvar gnus-newsgroup-process-stack nil)
1070
1071 (defvar gnus-thread-indent-array nil)
1072 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1073 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1074   "Function called to sort the articles within a thread after it has been gathered together.")
1075
1076 (defvar gnus-summary-save-parts-type-history nil)
1077 (defvar gnus-summary-save-parts-last-directory nil)
1078
1079 ;; Avoid highlighting in kill files.
1080 (defvar gnus-summary-inhibit-highlight nil)
1081 (defvar gnus-newsgroup-selected-overlay nil)
1082 (defvar gnus-inhibit-limiting nil)
1083 (defvar gnus-newsgroup-adaptive-score-file nil)
1084 (defvar gnus-current-score-file nil)
1085 (defvar gnus-current-move-group nil)
1086 (defvar gnus-current-copy-group nil)
1087 (defvar gnus-current-crosspost-group nil)
1088 (defvar gnus-newsgroup-display nil)
1089
1090 (defvar gnus-newsgroup-dependencies nil)
1091 (defvar gnus-newsgroup-adaptive nil)
1092 (defvar gnus-summary-display-article-function nil)
1093 (defvar gnus-summary-highlight-line-function nil
1094   "Function called after highlighting a summary line.")
1095
1096 (defvar gnus-summary-line-format-alist
1097   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1098     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1099     (?s gnus-tmp-subject-or-nil ?s)
1100     (?n gnus-tmp-name ?s)
1101     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1102         ?s)
1103     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1104             gnus-tmp-from) ?s)
1105     (?F gnus-tmp-from ?s)
1106     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1107     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1108     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1109     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1110     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1111     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1112     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1113     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1114     (?L gnus-tmp-lines ?s)
1115     (?O gnus-tmp-downloaded ?c)
1116     (?I gnus-tmp-indentation ?s)
1117     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1118     (?R gnus-tmp-replied ?c)
1119     (?\[ gnus-tmp-opening-bracket ?c)
1120     (?\] gnus-tmp-closing-bracket ?c)
1121     (?\> (make-string gnus-tmp-level ? ) ?s)
1122     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1123     (?i gnus-tmp-score ?d)
1124     (?z gnus-tmp-score-char ?c)
1125     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1126     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1127     (?U gnus-tmp-unread ?c)
1128     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1129         ?s)
1130     (?t (gnus-summary-number-of-articles-in-thread
1131          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1132         ?d)
1133     (?e (gnus-summary-number-of-articles-in-thread
1134          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1135         ?c)
1136     (?u gnus-tmp-user-defined ?s)
1137     (?P (gnus-pick-line-number) ?d)
1138     (?B gnus-tmp-thread-tree-header-string ?s)
1139     (user-date (gnus-user-date
1140                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1141   "An alist of format specifications that can appear in summary lines.
1142 These are paired with what variables they correspond with, along with
1143 the type of the variable (string, integer, character, etc).")
1144
1145 (defvar gnus-summary-dummy-line-format-alist
1146   `((?S gnus-tmp-subject ?s)
1147     (?N gnus-tmp-number ?d)
1148     (?u gnus-tmp-user-defined ?s)))
1149
1150 (defvar gnus-summary-mode-line-format-alist
1151   `((?G gnus-tmp-group-name ?s)
1152     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1153     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1154     (?A gnus-tmp-article-number ?d)
1155     (?Z gnus-tmp-unread-and-unselected ?s)
1156     (?V gnus-version ?s)
1157     (?U gnus-tmp-unread-and-unticked ?d)
1158     (?S gnus-tmp-subject ?s)
1159     (?e gnus-tmp-unselected ?d)
1160     (?u gnus-tmp-user-defined ?s)
1161     (?d (length gnus-newsgroup-dormant) ?d)
1162     (?t (length gnus-newsgroup-marked) ?d)
1163     (?h (length gnus-newsgroup-spam-marked) ?d)
1164     (?r (length gnus-newsgroup-reads) ?d)
1165     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1166     (?E gnus-newsgroup-expunged-tally ?d)
1167     (?s (gnus-current-score-file-nondirectory) ?s)))
1168
1169 (defvar gnus-last-search-regexp nil
1170   "Default regexp for article search command.")
1171
1172 (defvar gnus-last-shell-command nil
1173   "Default shell command on article.")
1174
1175 (defvar gnus-newsgroup-agentized nil
1176   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1177 (defvar gnus-newsgroup-begin nil)
1178 (defvar gnus-newsgroup-end nil)
1179 (defvar gnus-newsgroup-last-rmail nil)
1180 (defvar gnus-newsgroup-last-mail nil)
1181 (defvar gnus-newsgroup-last-folder nil)
1182 (defvar gnus-newsgroup-last-file nil)
1183 (defvar gnus-newsgroup-auto-expire nil)
1184 (defvar gnus-newsgroup-active nil)
1185
1186 (defvar gnus-newsgroup-data nil)
1187 (defvar gnus-newsgroup-data-reverse nil)
1188 (defvar gnus-newsgroup-limit nil)
1189 (defvar gnus-newsgroup-limits nil)
1190
1191 (defvar gnus-newsgroup-unreads nil
1192   "Sorted list of unread articles in the current newsgroup.")
1193
1194 (defvar gnus-newsgroup-unselected nil
1195   "Sorted list of unselected unread articles in the current newsgroup.")
1196
1197 (defvar gnus-newsgroup-reads nil
1198   "Alist of read articles and article marks in the current newsgroup.")
1199
1200 (defvar gnus-newsgroup-expunged-tally nil)
1201
1202 (defvar gnus-newsgroup-marked nil
1203   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1204
1205 (defvar gnus-newsgroup-spam-marked nil
1206   "List of ranges of articles that have been marked as spam.")
1207
1208 (defvar gnus-newsgroup-killed nil
1209   "List of ranges of articles that have been through the scoring process.")
1210
1211 (defvar gnus-newsgroup-cached nil
1212   "Sorted list of articles that come from the article cache.")
1213
1214 (defvar gnus-newsgroup-saved nil
1215   "List of articles that have been saved.")
1216
1217 (defvar gnus-newsgroup-kill-headers nil)
1218
1219 (defvar gnus-newsgroup-replied nil
1220   "List of articles that have been replied to in the current newsgroup.")
1221
1222 (defvar gnus-newsgroup-forwarded nil
1223   "List of articles that have been forwarded in the current newsgroup.")
1224
1225 (defvar gnus-newsgroup-recent nil
1226   "List of articles that have are recent in the current newsgroup.")
1227
1228 (defvar gnus-newsgroup-expirable nil
1229   "Sorted list of articles in the current newsgroup that can be expired.")
1230
1231 (defvar gnus-newsgroup-processable nil
1232   "List of articles in the current newsgroup that can be processed.")
1233
1234 (defvar gnus-newsgroup-downloadable nil
1235   "Sorted list of articles in the current newsgroup that can be processed.")
1236
1237 (defvar gnus-newsgroup-undownloaded nil
1238   "List of articles in the current newsgroup that haven't been downloaded..")
1239
1240 (defvar gnus-newsgroup-unsendable nil
1241   "List of articles in the current newsgroup that won't be sent.")
1242
1243 (defvar gnus-newsgroup-bookmarks nil
1244   "List of articles in the current newsgroup that have bookmarks.")
1245
1246 (defvar gnus-newsgroup-dormant nil
1247   "Sorted list of dormant articles in the current newsgroup.")
1248
1249 (defvar gnus-newsgroup-unseen nil
1250   "List of unseen articles in the current newsgroup.")
1251
1252 (defvar gnus-newsgroup-seen nil
1253   "Range of seen articles in the current newsgroup.")
1254
1255 (defvar gnus-newsgroup-articles nil
1256   "List of articles in the current newsgroup.")
1257
1258 (defvar gnus-newsgroup-scored nil
1259   "List of scored articles in the current newsgroup.")
1260
1261 (defvar gnus-newsgroup-headers nil
1262   "List of article headers in the current newsgroup.")
1263
1264 (defvar gnus-newsgroup-threads nil)
1265
1266 (defvar gnus-newsgroup-prepared nil
1267   "Whether the current group has been prepared properly.")
1268
1269 (defvar gnus-newsgroup-ancient nil
1270   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1271
1272 (defvar gnus-newsgroup-sparse nil)
1273
1274 (defvar gnus-current-article nil)
1275 (defvar gnus-article-current nil)
1276 (defvar gnus-current-headers nil)
1277 (defvar gnus-have-all-headers nil)
1278 (defvar gnus-last-article nil)
1279 (defvar gnus-newsgroup-history nil)
1280 (defvar gnus-newsgroup-charset nil)
1281 (defvar gnus-newsgroup-ephemeral-charset nil)
1282 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1283
1284 (defvar gnus-article-before-search nil)
1285
1286 (defconst gnus-summary-local-variables
1287   '(gnus-newsgroup-name
1288     gnus-newsgroup-begin gnus-newsgroup-end
1289     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1290     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1291     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1292     gnus-newsgroup-unselected gnus-newsgroup-marked
1293     gnus-newsgroup-spam-marked
1294     gnus-newsgroup-reads gnus-newsgroup-saved
1295     gnus-newsgroup-replied gnus-newsgroup-forwarded
1296     gnus-newsgroup-recent
1297     gnus-newsgroup-expirable
1298     gnus-newsgroup-processable gnus-newsgroup-killed
1299     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1300     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1301     gnus-newsgroup-seen gnus-newsgroup-articles
1302     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1303     gnus-newsgroup-headers gnus-newsgroup-threads
1304     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1305     gnus-current-article gnus-current-headers gnus-have-all-headers
1306     gnus-last-article gnus-article-internal-prepare-hook
1307     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1308     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1309     gnus-thread-expunge-below
1310     gnus-score-alist gnus-current-score-file
1311     (gnus-summary-expunge-below . global)
1312     (gnus-summary-mark-below . global)
1313     (gnus-orphan-score . global)
1314     gnus-newsgroup-active gnus-scores-exclude-files
1315     gnus-newsgroup-history gnus-newsgroup-ancient
1316     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1317     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1318     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1319     (gnus-newsgroup-expunged-tally . 0)
1320     gnus-cache-removable-articles gnus-newsgroup-cached
1321     gnus-newsgroup-data gnus-newsgroup-data-reverse
1322     gnus-newsgroup-limit gnus-newsgroup-limits
1323     gnus-newsgroup-charset gnus-newsgroup-display)
1324   "Variables that are buffer-local to the summary buffers.")
1325
1326 (defvar gnus-newsgroup-variables nil
1327   "A list of variables that have separate values in different newsgroups.
1328 A list of newsgroup (summary buffer) local variables, or cons of
1329 variables and their default values (when the default values are not
1330 nil), that should be made global while the summary buffer is active.
1331 These variables can be used to set variables in the group parameters
1332 while still allowing them to affect operations done in other
1333 buffers. For example:
1334
1335 \(setq gnus-newsgroup-variables
1336      '(message-use-followup-to
1337        (gnus-visible-headers .
1338          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1339 ")
1340
1341 ;; Byte-compiler warning.
1342 ;(eval-when-compile (defvar gnus-article-mode-map))
1343 (eval-when-compile
1344   (let ((features (cons 'gnus-sum features)))
1345     (require 'gnus)
1346     (require 'gnus-agent)
1347     (require 'gnus-art)))
1348
1349 ;; MIME stuff.
1350
1351 (defvar gnus-decode-encoded-word-methods
1352   '(mail-decode-encoded-word-string)
1353   "List of methods used to decode encoded words.
1354
1355 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1356 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1357 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1358 whose names match REGEXP.
1359
1360 For example:
1361 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1362  mail-decode-encoded-word-string
1363  (\"chinese\" . rfc1843-decode-string))")
1364
1365 (defvar gnus-decode-encoded-word-methods-cache nil)
1366
1367 (defun gnus-multi-decode-encoded-word-string (string)
1368   "Apply the functions from `gnus-encoded-word-methods' that match."
1369   (unless (and gnus-decode-encoded-word-methods-cache
1370                (eq gnus-newsgroup-name
1371                    (car gnus-decode-encoded-word-methods-cache)))
1372     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1373     (mapcar (lambda (x)
1374               (if (symbolp x)
1375                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1376                 (if (and gnus-newsgroup-name
1377                          (string-match (car x) gnus-newsgroup-name))
1378                     (nconc gnus-decode-encoded-word-methods-cache
1379                            (list (cdr x))))))
1380             gnus-decode-encoded-word-methods))
1381   (let ((xlist gnus-decode-encoded-word-methods-cache))
1382     (pop xlist)
1383     (while xlist
1384       (setq string (funcall (pop xlist) string))))
1385   string)
1386
1387 ;; Subject simplification.
1388
1389 (defun gnus-simplify-whitespace (str)
1390   "Remove excessive whitespace from STR."
1391   (let ((mystr str))
1392     ;; Multiple spaces.
1393     (while (string-match "[ \t][ \t]+" mystr)
1394       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1395                           " "
1396                           (substring mystr (match-end 0)))))
1397     ;; Leading spaces.
1398     (when (string-match "^[ \t]+" mystr)
1399       (setq mystr (substring mystr (match-end 0))))
1400     ;; Trailing spaces.
1401     (when (string-match "[ \t]+$" mystr)
1402       (setq mystr (substring mystr 0 (match-beginning 0))))
1403     mystr))
1404
1405 (defun gnus-simplify-all-whitespace (str)
1406   "Remove all whitespace from STR."
1407   (let ((mystr str))
1408     (while (string-match "[ \t\n]+" mystr)
1409       (setq mystr (replace-match "" nil nil mystr)))
1410     mystr))
1411
1412 (defsubst gnus-simplify-subject-re (subject)
1413   "Remove \"Re:\" from subject lines."
1414   (if (string-match message-subject-re-regexp subject)
1415       (substring subject (match-end 0))
1416     subject))
1417
1418 (defun gnus-simplify-subject (subject &optional re-only)
1419   "Remove `Re:' and words in parentheses.
1420 If RE-ONLY is non-nil, strip leading `Re:'s only."
1421   (let ((case-fold-search t))           ;Ignore case.
1422     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1423     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1424       (setq subject (substring subject (match-end 0))))
1425     ;; Remove uninteresting prefixes.
1426     (when (and (not re-only)
1427                gnus-simplify-ignored-prefixes
1428                (string-match gnus-simplify-ignored-prefixes subject))
1429       (setq subject (substring subject (match-end 0))))
1430     ;; Remove words in parentheses from end.
1431     (unless re-only
1432       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1433         (setq subject (substring subject 0 (match-beginning 0)))))
1434     ;; Return subject string.
1435     subject))
1436
1437 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1438 ;; all whitespace.
1439 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1440   (goto-char (point-min))
1441   (while (re-search-forward regexp nil t)
1442     (replace-match (or newtext ""))))
1443
1444 (defun gnus-simplify-buffer-fuzzy ()
1445   "Simplify string in the buffer fuzzily.
1446 The string in the accessible portion of the current buffer is simplified.
1447 It is assumed to be a single-line subject.
1448 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1449 matter is removed.  Additional things can be deleted by setting
1450 `gnus-simplify-subject-fuzzy-regexp'."
1451   (let ((case-fold-search t)
1452         (modified-tick))
1453     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1454
1455     (while (not (eq modified-tick (buffer-modified-tick)))
1456       (setq modified-tick (buffer-modified-tick))
1457       (cond
1458        ((listp gnus-simplify-subject-fuzzy-regexp)
1459         (mapcar 'gnus-simplify-buffer-fuzzy-step
1460                 gnus-simplify-subject-fuzzy-regexp))
1461        (gnus-simplify-subject-fuzzy-regexp
1462         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1463       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1464       (gnus-simplify-buffer-fuzzy-step
1465        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1466       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1467
1468     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1469     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1470     (gnus-simplify-buffer-fuzzy-step " $")
1471     (gnus-simplify-buffer-fuzzy-step "^ +")))
1472
1473 (defun gnus-simplify-subject-fuzzy (subject)
1474   "Simplify a subject string fuzzily.
1475 See `gnus-simplify-buffer-fuzzy' for details."
1476   (save-excursion
1477     (gnus-set-work-buffer)
1478     (let ((case-fold-search t))
1479       ;; Remove uninteresting prefixes.
1480       (when (and gnus-simplify-ignored-prefixes
1481                  (string-match gnus-simplify-ignored-prefixes subject))
1482         (setq subject (substring subject (match-end 0))))
1483       (insert subject)
1484       (inline (gnus-simplify-buffer-fuzzy))
1485       (buffer-string))))
1486
1487 (defsubst gnus-simplify-subject-fully (subject)
1488   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1489   (cond
1490    (gnus-simplify-subject-functions
1491     (gnus-map-function gnus-simplify-subject-functions subject))
1492    ((null gnus-summary-gather-subject-limit)
1493     (gnus-simplify-subject-re subject))
1494    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1495     (gnus-simplify-subject-fuzzy subject))
1496    ((numberp gnus-summary-gather-subject-limit)
1497     (gnus-limit-string (gnus-simplify-subject-re subject)
1498                        gnus-summary-gather-subject-limit))
1499    (t
1500     subject)))
1501
1502 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1503   "Check whether two subjects are equal.
1504 If optional argument simple-first is t, first argument is already
1505 simplified."
1506   (cond
1507    ((null simple-first)
1508     (equal (gnus-simplify-subject-fully s1)
1509            (gnus-simplify-subject-fully s2)))
1510    (t
1511     (equal s1
1512            (gnus-simplify-subject-fully s2)))))
1513
1514 (defun gnus-summary-bubble-group ()
1515   "Increase the score of the current group.
1516 This is a handy function to add to `gnus-summary-exit-hook' to
1517 increase the score of each group you read."
1518   (gnus-group-add-score gnus-newsgroup-name))
1519
1520 \f
1521 ;;;
1522 ;;; Gnus summary mode
1523 ;;;
1524
1525 (put 'gnus-summary-mode 'mode-class 'special)
1526
1527 (defvar gnus-article-commands-menu)
1528
1529 ;; Non-orthogonal keys
1530
1531 (gnus-define-keys gnus-summary-mode-map
1532   " " gnus-summary-next-page
1533   "\177" gnus-summary-prev-page
1534   [delete] gnus-summary-prev-page
1535   [backspace] gnus-summary-prev-page
1536   "\r" gnus-summary-scroll-up
1537   "\M-\r" gnus-summary-scroll-down
1538   "n" gnus-summary-next-unread-article
1539   "p" gnus-summary-prev-unread-article
1540   "N" gnus-summary-next-article
1541   "P" gnus-summary-prev-article
1542   "\M-\C-n" gnus-summary-next-same-subject
1543   "\M-\C-p" gnus-summary-prev-same-subject
1544   "\M-n" gnus-summary-next-unread-subject
1545   "\M-p" gnus-summary-prev-unread-subject
1546   "." gnus-summary-first-unread-article
1547   "," gnus-summary-best-unread-article
1548   "\M-s" gnus-summary-search-article-forward
1549   "\M-r" gnus-summary-search-article-backward
1550   "<" gnus-summary-beginning-of-article
1551   ">" gnus-summary-end-of-article
1552   "j" gnus-summary-goto-article
1553   "^" gnus-summary-refer-parent-article
1554   "\M-^" gnus-summary-refer-article
1555   "u" gnus-summary-tick-article-forward
1556   "!" gnus-summary-tick-article-forward
1557   "U" gnus-summary-tick-article-backward
1558   "d" gnus-summary-mark-as-read-forward
1559   "D" gnus-summary-mark-as-read-backward
1560   "E" gnus-summary-mark-as-expirable
1561   "\M-u" gnus-summary-clear-mark-forward
1562   "\M-U" gnus-summary-clear-mark-backward
1563   "k" gnus-summary-kill-same-subject-and-select
1564   "\C-k" gnus-summary-kill-same-subject
1565   "\M-\C-k" gnus-summary-kill-thread
1566   "\M-\C-l" gnus-summary-lower-thread
1567   "e" gnus-summary-edit-article
1568   "#" gnus-summary-mark-as-processable
1569   "\M-#" gnus-summary-unmark-as-processable
1570   "\M-\C-t" gnus-summary-toggle-threads
1571   "\M-\C-s" gnus-summary-show-thread
1572   "\M-\C-h" gnus-summary-hide-thread
1573   "\M-\C-f" gnus-summary-next-thread
1574   "\M-\C-b" gnus-summary-prev-thread
1575   [(meta down)] gnus-summary-next-thread
1576   [(meta up)] gnus-summary-prev-thread
1577   "\M-\C-u" gnus-summary-up-thread
1578   "\M-\C-d" gnus-summary-down-thread
1579   "&" gnus-summary-execute-command
1580   "c" gnus-summary-catchup-and-exit
1581   "\C-w" gnus-summary-mark-region-as-read
1582   "\C-t" gnus-summary-toggle-truncation
1583   "?" gnus-summary-mark-as-dormant
1584   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1585   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1586   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1587   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1588   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1589   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1590   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1591   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1592   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1593   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1594   "=" gnus-summary-expand-window
1595   "\C-x\C-s" gnus-summary-reselect-current-group
1596   "\M-g" gnus-summary-rescan-group
1597   "w" gnus-summary-stop-page-breaking
1598   "\C-c\C-r" gnus-summary-caesar-message
1599   "f" gnus-summary-followup
1600   "F" gnus-summary-followup-with-original
1601   "C" gnus-summary-cancel-article
1602   "r" gnus-summary-reply
1603   "R" gnus-summary-reply-with-original
1604   "\C-c\C-f" gnus-summary-mail-forward
1605   "o" gnus-summary-save-article
1606   "\C-o" gnus-summary-save-article-mail
1607   "|" gnus-summary-pipe-output
1608   "\M-k" gnus-summary-edit-local-kill
1609   "\M-K" gnus-summary-edit-global-kill
1610   ;; "V" gnus-version
1611   "\C-c\C-d" gnus-summary-describe-group
1612   "q" gnus-summary-exit
1613   "Q" gnus-summary-exit-no-update
1614   "\C-c\C-i" gnus-info-find-node
1615   gnus-mouse-2 gnus-mouse-pick-article
1616   "m" gnus-summary-mail-other-window
1617   "a" gnus-summary-post-news
1618   "i" gnus-summary-news-other-window
1619   "x" gnus-summary-limit-to-unread
1620   "s" gnus-summary-isearch-article
1621   "t" gnus-summary-toggle-header
1622   "g" gnus-summary-show-article
1623   "l" gnus-summary-goto-last-article
1624   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1625   "\C-d" gnus-summary-enter-digest-group
1626   "\M-\C-d" gnus-summary-read-document
1627   "\M-\C-e" gnus-summary-edit-parameters
1628   "\M-\C-a" gnus-summary-customize-parameters
1629   "\C-c\C-b" gnus-bug
1630   "*" gnus-cache-enter-article
1631   "\M-*" gnus-cache-remove-article
1632   "\M-&" gnus-summary-universal-argument
1633   "\C-l" gnus-recenter
1634   "I" gnus-summary-increase-score
1635   "L" gnus-summary-lower-score
1636   "\M-i" gnus-symbolic-argument
1637   "h" gnus-summary-select-article-buffer
1638
1639   "b" gnus-article-view-part
1640   "\M-t" gnus-summary-toggle-display-buttonized
1641
1642   "V" gnus-summary-score-map
1643   "X" gnus-uu-extract-map
1644   "S" gnus-summary-send-map)
1645
1646 ;; Sort of orthogonal keymap
1647 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1648   "t" gnus-summary-tick-article-forward
1649   "!" gnus-summary-tick-article-forward
1650   "d" gnus-summary-mark-as-read-forward
1651   "r" gnus-summary-mark-as-read-forward
1652   "c" gnus-summary-clear-mark-forward
1653   " " gnus-summary-clear-mark-forward
1654   "e" gnus-summary-mark-as-expirable
1655   "x" gnus-summary-mark-as-expirable
1656   "?" gnus-summary-mark-as-dormant
1657   "b" gnus-summary-set-bookmark
1658   "B" gnus-summary-remove-bookmark
1659   "#" gnus-summary-mark-as-processable
1660   "\M-#" gnus-summary-unmark-as-processable
1661   "S" gnus-summary-limit-include-expunged
1662   "C" gnus-summary-catchup
1663   "H" gnus-summary-catchup-to-here
1664   "h" gnus-summary-catchup-from-here
1665   "\C-c" gnus-summary-catchup-all
1666   "k" gnus-summary-kill-same-subject-and-select
1667   "K" gnus-summary-kill-same-subject
1668   "P" gnus-uu-mark-map)
1669
1670 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1671   "c" gnus-summary-clear-above
1672   "u" gnus-summary-tick-above
1673   "m" gnus-summary-mark-above
1674   "k" gnus-summary-kill-below)
1675
1676 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1677   "/" gnus-summary-limit-to-subject
1678   "n" gnus-summary-limit-to-articles
1679   "w" gnus-summary-pop-limit
1680   "s" gnus-summary-limit-to-subject
1681   "a" gnus-summary-limit-to-author
1682   "u" gnus-summary-limit-to-unread
1683   "m" gnus-summary-limit-to-marks
1684   "M" gnus-summary-limit-exclude-marks
1685   "v" gnus-summary-limit-to-score
1686   "*" gnus-summary-limit-include-cached
1687   "D" gnus-summary-limit-include-dormant
1688   "T" gnus-summary-limit-include-thread
1689   "d" gnus-summary-limit-exclude-dormant
1690   "t" gnus-summary-limit-to-age
1691   "." gnus-summary-limit-to-unseen
1692   "x" gnus-summary-limit-to-extra
1693   "p" gnus-summary-limit-to-display-predicate
1694   "E" gnus-summary-limit-include-expunged
1695   "c" gnus-summary-limit-exclude-childless-dormant
1696   "C" gnus-summary-limit-mark-excluded-as-read
1697   "o" gnus-summary-insert-old-articles
1698   "N" gnus-summary-insert-new-articles)
1699
1700 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1701   "n" gnus-summary-next-unread-article
1702   "p" gnus-summary-prev-unread-article
1703   "N" gnus-summary-next-article
1704   "P" gnus-summary-prev-article
1705   "\C-n" gnus-summary-next-same-subject
1706   "\C-p" gnus-summary-prev-same-subject
1707   "\M-n" gnus-summary-next-unread-subject
1708   "\M-p" gnus-summary-prev-unread-subject
1709   "f" gnus-summary-first-unread-article
1710   "b" gnus-summary-best-unread-article
1711   "j" gnus-summary-goto-article
1712   "g" gnus-summary-goto-subject
1713   "l" gnus-summary-goto-last-article
1714   "o" gnus-summary-pop-article)
1715
1716 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1717   "k" gnus-summary-kill-thread
1718   "l" gnus-summary-lower-thread
1719   "i" gnus-summary-raise-thread
1720   "T" gnus-summary-toggle-threads
1721   "t" gnus-summary-rethread-current
1722   "^" gnus-summary-reparent-thread
1723   "s" gnus-summary-show-thread
1724   "S" gnus-summary-show-all-threads
1725   "h" gnus-summary-hide-thread
1726   "H" gnus-summary-hide-all-threads
1727   "n" gnus-summary-next-thread
1728   "p" gnus-summary-prev-thread
1729   "u" gnus-summary-up-thread
1730   "o" gnus-summary-top-thread
1731   "d" gnus-summary-down-thread
1732   "#" gnus-uu-mark-thread
1733   "\M-#" gnus-uu-unmark-thread)
1734
1735 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1736   "g" gnus-summary-prepare
1737   "c" gnus-summary-insert-cached-articles
1738   "d" gnus-summary-insert-dormant-articles)
1739
1740 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1741   "c" gnus-summary-catchup-and-exit
1742   "C" gnus-summary-catchup-all-and-exit
1743   "E" gnus-summary-exit-no-update
1744   "Q" gnus-summary-exit
1745   "Z" gnus-summary-exit
1746   "n" gnus-summary-catchup-and-goto-next-group
1747   "R" gnus-summary-reselect-current-group
1748   "G" gnus-summary-rescan-group
1749   "N" gnus-summary-next-group
1750   "s" gnus-summary-save-newsrc
1751   "P" gnus-summary-prev-group)
1752
1753 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1754   " " gnus-summary-next-page
1755   "n" gnus-summary-next-page
1756   "\177" gnus-summary-prev-page
1757   [delete] gnus-summary-prev-page
1758   "p" gnus-summary-prev-page
1759   "\r" gnus-summary-scroll-up
1760   "\M-\r" gnus-summary-scroll-down
1761   "<" gnus-summary-beginning-of-article
1762   ">" gnus-summary-end-of-article
1763   "b" gnus-summary-beginning-of-article
1764   "e" gnus-summary-end-of-article
1765   "^" gnus-summary-refer-parent-article
1766   "r" gnus-summary-refer-parent-article
1767   "D" gnus-summary-enter-digest-group
1768   "R" gnus-summary-refer-references
1769   "T" gnus-summary-refer-thread
1770   "g" gnus-summary-show-article
1771   "s" gnus-summary-isearch-article
1772   "P" gnus-summary-print-article
1773   "M" gnus-mailing-list-insinuate
1774   "t" gnus-article-babel)
1775
1776 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1777   "b" gnus-article-add-buttons
1778   "B" gnus-article-add-buttons-to-head
1779   "o" gnus-article-treat-overstrike
1780   "e" gnus-article-emphasize
1781   "w" gnus-article-fill-cited-article
1782   "Q" gnus-article-fill-long-lines
1783   "C" gnus-article-capitalize-sentences
1784   "c" gnus-article-remove-cr
1785   "q" gnus-article-de-quoted-unreadable
1786   "6" gnus-article-de-base64-unreadable
1787   "Z" gnus-article-decode-HZ
1788   "h" gnus-article-wash-html
1789   "u" gnus-article-unsplit-urls
1790   "s" gnus-summary-force-verify-and-decrypt
1791   "f" gnus-article-display-x-face
1792   "l" gnus-summary-stop-page-breaking
1793   "r" gnus-summary-caesar-message
1794   "m" gnus-summary-morse-message
1795   "t" gnus-summary-toggle-header
1796   "g" gnus-treat-smiley
1797   "v" gnus-summary-verbose-headers
1798   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1799   "p" gnus-article-verify-x-pgp-sig
1800   "d" gnus-article-treat-dumbquotes)
1801
1802 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1803   ;; mnemonic: deuglif*Y*
1804   "u" gnus-article-outlook-unwrap-lines
1805   "a" gnus-article-outlook-repair-attribution
1806   "c" gnus-article-outlook-rearrange-citation
1807   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1808
1809 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1810   "a" gnus-article-hide
1811   "h" gnus-article-hide-headers
1812   "b" gnus-article-hide-boring-headers
1813   "s" gnus-article-hide-signature
1814   "c" gnus-article-hide-citation
1815   "C" gnus-article-hide-citation-in-followups
1816   "l" gnus-article-hide-list-identifiers
1817   "p" gnus-article-hide-pgp
1818   "B" gnus-article-strip-banner
1819   "P" gnus-article-hide-pem
1820   "\C-c" gnus-article-hide-citation-maybe)
1821
1822 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1823   "a" gnus-article-highlight
1824   "h" gnus-article-highlight-headers
1825   "c" gnus-article-highlight-citation
1826   "s" gnus-article-highlight-signature)
1827
1828 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1829   "f" gnus-article-treat-fold-headers
1830   "u" gnus-article-treat-unfold-headers
1831   "n" gnus-article-treat-fold-newsgroups)
1832
1833 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1834   "x" gnus-article-display-x-face
1835   "d" gnus-article-display-face
1836   "s" gnus-treat-smiley
1837   "D" gnus-article-remove-images
1838   "f" gnus-treat-from-picon
1839   "m" gnus-treat-mail-picon
1840   "n" gnus-treat-newsgroups-picon)
1841
1842 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1843   "w" gnus-article-decode-mime-words
1844   "c" gnus-article-decode-charset
1845   "v" gnus-mime-view-all-parts
1846   "b" gnus-article-view-part)
1847
1848 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1849   "z" gnus-article-date-ut
1850   "u" gnus-article-date-ut
1851   "l" gnus-article-date-local
1852   "p" gnus-article-date-english
1853   "e" gnus-article-date-lapsed
1854   "o" gnus-article-date-original
1855   "i" gnus-article-date-iso8601
1856   "s" gnus-article-date-user)
1857
1858 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1859   "t" gnus-article-remove-trailing-blank-lines
1860   "l" gnus-article-strip-leading-blank-lines
1861   "m" gnus-article-strip-multiple-blank-lines
1862   "a" gnus-article-strip-blank-lines
1863   "A" gnus-article-strip-all-blank-lines
1864   "s" gnus-article-strip-leading-space
1865   "e" gnus-article-strip-trailing-space
1866   "w" gnus-article-remove-leading-whitespace)
1867
1868 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1869   "v" gnus-version
1870   "f" gnus-summary-fetch-faq
1871   "d" gnus-summary-describe-group
1872   "h" gnus-summary-describe-briefly
1873   "i" gnus-info-find-node
1874   "c" gnus-group-fetch-charter
1875   "C" gnus-group-fetch-control)
1876
1877 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1878   "e" gnus-summary-expire-articles
1879   "\M-\C-e" gnus-summary-expire-articles-now
1880   "\177" gnus-summary-delete-article
1881   [delete] gnus-summary-delete-article
1882   [backspace] gnus-summary-delete-article
1883   "m" gnus-summary-move-article
1884   "r" gnus-summary-respool-article
1885   "w" gnus-summary-edit-article
1886   "c" gnus-summary-copy-article
1887   "B" gnus-summary-crosspost-article
1888   "q" gnus-summary-respool-query
1889   "t" gnus-summary-respool-trace
1890   "i" gnus-summary-import-article
1891   "I" gnus-summary-create-article
1892   "p" gnus-summary-article-posted-p)
1893
1894 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1895   "o" gnus-summary-save-article
1896   "m" gnus-summary-save-article-mail
1897   "F" gnus-summary-write-article-file
1898   "r" gnus-summary-save-article-rmail
1899   "f" gnus-summary-save-article-file
1900   "b" gnus-summary-save-article-body-file
1901   "h" gnus-summary-save-article-folder
1902   "v" gnus-summary-save-article-vm
1903   "p" gnus-summary-pipe-output
1904   "P" gnus-summary-muttprint
1905   "s" gnus-soup-add-article)
1906
1907 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1908   "b" gnus-summary-display-buttonized
1909   "m" gnus-summary-repair-multipart
1910   "v" gnus-article-view-part
1911   "o" gnus-article-save-part
1912   "c" gnus-article-copy-part
1913   "C" gnus-article-view-part-as-charset
1914   "e" gnus-article-view-part-externally
1915   "E" gnus-article-encrypt-body
1916   "i" gnus-article-inline-part
1917   "|" gnus-article-pipe-part)
1918
1919 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1920   "p" gnus-summary-mark-as-processable
1921   "u" gnus-summary-unmark-as-processable
1922   "U" gnus-summary-unmark-all-processable
1923   "v" gnus-uu-mark-over
1924   "s" gnus-uu-mark-series
1925   "r" gnus-uu-mark-region
1926   "g" gnus-uu-unmark-region
1927   "R" gnus-uu-mark-by-regexp
1928   "G" gnus-uu-unmark-by-regexp
1929   "t" gnus-uu-mark-thread
1930   "T" gnus-uu-unmark-thread
1931   "a" gnus-uu-mark-all
1932   "b" gnus-uu-mark-buffer
1933   "S" gnus-uu-mark-sparse
1934   "k" gnus-summary-kill-process-mark
1935   "y" gnus-summary-yank-process-mark
1936   "w" gnus-summary-save-process-mark
1937   "i" gnus-uu-invert-processable)
1938
1939 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1940   ;;"x" gnus-uu-extract-any
1941   "m" gnus-summary-save-parts
1942   "u" gnus-uu-decode-uu
1943   "U" gnus-uu-decode-uu-and-save
1944   "s" gnus-uu-decode-unshar
1945   "S" gnus-uu-decode-unshar-and-save
1946   "o" gnus-uu-decode-save
1947   "O" gnus-uu-decode-save
1948   "b" gnus-uu-decode-binhex
1949   "B" gnus-uu-decode-binhex
1950   "p" gnus-uu-decode-postscript
1951   "P" gnus-uu-decode-postscript-and-save)
1952
1953 (gnus-define-keys
1954     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
1955   "u" gnus-uu-decode-uu-view
1956   "U" gnus-uu-decode-uu-and-save-view
1957   "s" gnus-uu-decode-unshar-view
1958   "S" gnus-uu-decode-unshar-and-save-view
1959   "o" gnus-uu-decode-save-view
1960   "O" gnus-uu-decode-save-view
1961   "b" gnus-uu-decode-binhex-view
1962   "B" gnus-uu-decode-binhex-view
1963   "p" gnus-uu-decode-postscript-view
1964   "P" gnus-uu-decode-postscript-and-save-view)
1965
1966 (defvar gnus-article-post-menu nil)
1967
1968 (defconst gnus-summary-menu-maxlen 20)
1969
1970 (defun gnus-summary-menu-split (menu)
1971   ;; If we have lots of elements, divide them into groups of 20
1972   ;; and make a pane (or submenu) for each one.
1973   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
1974       (let ((menu menu) sublists next
1975             (i 1))
1976         (while menu
1977           ;; Pull off the next gnus-summary-menu-maxlen elements
1978           ;; and make them the next element of sublist.
1979           (setq next (nthcdr gnus-summary-menu-maxlen menu))
1980           (if next
1981               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
1982                       nil))
1983           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
1984                                              (aref (car (last menu)) 0)) menu)
1985                                sublists))
1986           (setq i (1+ i))
1987           (setq menu next))
1988         (nreverse sublists))
1989     ;; Few elements--put them all in one pane.
1990     menu))
1991
1992 (defun gnus-summary-make-menu-bar ()
1993   (gnus-turn-off-edit-menu 'summary)
1994
1995   (unless (boundp 'gnus-summary-misc-menu)
1996
1997     (easy-menu-define
1998       gnus-summary-kill-menu gnus-summary-mode-map ""
1999       (cons
2000        "Score"
2001        (nconc
2002         (list
2003          ["Customize" gnus-score-customize t])
2004         (gnus-make-score-map 'increase)
2005         (gnus-make-score-map 'lower)
2006         '(("Mark"
2007            ["Kill below" gnus-summary-kill-below t]
2008            ["Mark above" gnus-summary-mark-above t]
2009            ["Tick above" gnus-summary-tick-above t]
2010            ["Clear above" gnus-summary-clear-above t])
2011           ["Current score" gnus-summary-current-score t]
2012           ["Set score" gnus-summary-set-score t]
2013           ["Switch current score file..." gnus-score-change-score-file t]
2014           ["Set mark below..." gnus-score-set-mark-below t]
2015           ["Set expunge below..." gnus-score-set-expunge-below t]
2016           ["Edit current score file" gnus-score-edit-current-scores t]
2017           ["Edit score file" gnus-score-edit-file t]
2018           ["Trace score" gnus-score-find-trace t]
2019           ["Find words" gnus-score-find-favourite-words t]
2020           ["Rescore buffer" gnus-summary-rescore t]
2021           ["Increase score..." gnus-summary-increase-score t]
2022           ["Lower score..." gnus-summary-lower-score t]))))
2023
2024     ;; Define both the Article menu in the summary buffer and the
2025     ;; equivalent Commands menu in the article buffer here for
2026     ;; consistency.
2027     (let ((innards
2028            `(("Hide"
2029               ["All" gnus-article-hide t]
2030               ["Headers" gnus-article-hide-headers t]
2031               ["Signature" gnus-article-hide-signature t]
2032               ["Citation" gnus-article-hide-citation t]
2033               ["List identifiers" gnus-article-hide-list-identifiers t]
2034               ["PGP" gnus-article-hide-pgp t]
2035               ["Banner" gnus-article-strip-banner t]
2036               ["Boring headers" gnus-article-hide-boring-headers t])
2037              ("Highlight"
2038               ["All" gnus-article-highlight t]
2039               ["Headers" gnus-article-highlight-headers t]
2040               ["Signature" gnus-article-highlight-signature t]
2041               ["Citation" gnus-article-highlight-citation t])
2042              ("MIME"
2043               ["Words" gnus-article-decode-mime-words t]
2044               ["Charset" gnus-article-decode-charset t]
2045               ["QP" gnus-article-de-quoted-unreadable t]
2046               ["Base64" gnus-article-de-base64-unreadable t]
2047               ["View MIME buttons" gnus-summary-display-buttonized t]
2048               ["View all" gnus-mime-view-all-parts t]
2049               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2050               ["Encrypt body" gnus-article-encrypt-body t]
2051               ["Extract all parts" gnus-summary-save-parts t])
2052              ("Date"
2053               ["Local" gnus-article-date-local t]
2054               ["ISO8601" gnus-article-date-iso8601 t]
2055               ["UT" gnus-article-date-ut t]
2056               ["Original" gnus-article-date-original t]
2057               ["Lapsed" gnus-article-date-lapsed t]
2058               ["User-defined" gnus-article-date-user t])
2059              ("Display"
2060               ["Remove images" gnus-article-remove-images t]
2061               ["Toggle smiley" gnus-treat-smiley t]
2062               ["Show X-Face" gnus-article-display-x-face t]
2063               ["Show picons in From" gnus-treat-from-picon t]
2064               ["Show picons in mail headers" gnus-treat-mail-picon t]
2065               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2066               ("View as different encoding"
2067                ,@(gnus-summary-menu-split
2068                   (mapcar
2069                    (lambda (cs)
2070                      ;; Since easymenu under FSF Emacs doesn't allow lambda
2071                      ;; forms for menu commands, we should provide intern'ed
2072                      ;; function symbols.
2073                      (let ((command (intern (format "\
2074 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2075                        (fset command
2076                              `(lambda ()
2077                                 (interactive)
2078                                 (let ((gnus-summary-show-article-charset-alist
2079                                        '((1 . ,cs))))
2080                                   (gnus-summary-show-article 1))))
2081                        `[,(symbol-name cs) ,command t]))
2082                    (sort (if (fboundp 'coding-system-list)
2083                              (coding-system-list)
2084                            (mapcar 'car mm-mime-mule-charset-alist))
2085                          'string<)))))
2086              ("Washing"
2087               ("Remove Blanks"
2088                ["Leading" gnus-article-strip-leading-blank-lines t]
2089                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2090                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2091                ["All of the above" gnus-article-strip-blank-lines t]
2092                ["All" gnus-article-strip-all-blank-lines t]
2093                ["Leading space" gnus-article-strip-leading-space t]
2094                ["Trailing space" gnus-article-strip-trailing-space t]
2095                ["Leading space in headers"
2096                 gnus-article-remove-leading-whitespace t])
2097               ["Overstrike" gnus-article-treat-overstrike t]
2098               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2099               ["Emphasis" gnus-article-emphasize t]
2100               ["Word wrap" gnus-article-fill-cited-article t]
2101               ["Fill long lines" gnus-article-fill-long-lines t]
2102               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2103               ["CR" gnus-article-remove-cr t]
2104               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2105               ["Base64" gnus-article-de-base64-unreadable t]
2106               ["Rot 13" gnus-summary-caesar-message
2107                ,@(if (featurep 'xemacs) '(t)
2108                    '(:help "\"Caesar rotate\" article by 13"))]
2109               ["Morse decode" gnus-summary-morse-message t]
2110               ["Unix pipe..." gnus-summary-pipe-message t]
2111               ["Add buttons" gnus-article-add-buttons t]
2112               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2113               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2114               ["Verbose header" gnus-summary-verbose-headers t]
2115               ["Toggle header" gnus-summary-toggle-header t]
2116               ["Unfold headers" gnus-article-treat-unfold-headers t]
2117               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2118               ["Html" gnus-article-wash-html t]
2119               ["URLs" gnus-article-unsplit-urls t]
2120               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2121               ["HZ" gnus-article-decode-HZ t]
2122               ("(Outlook) Deuglify"
2123                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2124                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2125                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2126                ["Full (Outlook) deuglify"
2127                 gnus-article-outlook-deuglify-article t])
2128               )
2129              ("Output"
2130               ["Save in default format" gnus-summary-save-article
2131                ,@(if (featurep 'xemacs) '(t)
2132                    '(:help "Save article using default method"))]
2133               ["Save in file" gnus-summary-save-article-file
2134                ,@(if (featurep 'xemacs) '(t)
2135                    '(:help "Save article in file"))]
2136               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2137               ["Save in MH folder" gnus-summary-save-article-folder t]
2138               ["Save in VM folder" gnus-summary-save-article-vm t]
2139               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2140               ["Save body in file" gnus-summary-save-article-body-file t]
2141               ["Pipe through a filter" gnus-summary-pipe-output t]
2142               ["Add to SOUP packet" gnus-soup-add-article t]
2143               ["Print with Muttprint" gnus-summary-muttprint t]
2144               ["Print" gnus-summary-print-article t])
2145              ("Backend"
2146               ["Respool article..." gnus-summary-respool-article t]
2147               ["Move article..." gnus-summary-move-article
2148                (gnus-check-backend-function
2149                 'request-move-article gnus-newsgroup-name)]
2150               ["Copy article..." gnus-summary-copy-article t]
2151               ["Crosspost article..." gnus-summary-crosspost-article
2152                (gnus-check-backend-function
2153                 'request-replace-article gnus-newsgroup-name)]
2154               ["Import file..." gnus-summary-import-article t]
2155               ["Create article..." gnus-summary-create-article t]
2156               ["Check if posted" gnus-summary-article-posted-p t]
2157               ["Edit article" gnus-summary-edit-article
2158                (not (gnus-group-read-only-p))]
2159               ["Delete article" gnus-summary-delete-article
2160                (gnus-check-backend-function
2161                 'request-expire-articles gnus-newsgroup-name)]
2162               ["Query respool" gnus-summary-respool-query t]
2163               ["Trace respool" gnus-summary-respool-trace t]
2164               ["Delete expirable articles" gnus-summary-expire-articles-now
2165                (gnus-check-backend-function
2166                 'request-expire-articles gnus-newsgroup-name)])
2167              ("Extract"
2168               ["Uudecode" gnus-uu-decode-uu
2169                ,@(if (featurep 'xemacs) '(t)
2170                    '(:help "Decode uuencoded article(s)"))]
2171               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2172               ["Unshar" gnus-uu-decode-unshar t]
2173               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2174               ["Save" gnus-uu-decode-save t]
2175               ["Binhex" gnus-uu-decode-binhex t]
2176               ["Postscript" gnus-uu-decode-postscript t]
2177               ["All MIME parts" gnus-summary-save-parts t])
2178              ("Cache"
2179               ["Enter article" gnus-cache-enter-article t]
2180               ["Remove article" gnus-cache-remove-article t])
2181              ["Translate" gnus-article-babel t]
2182              ["Select article buffer" gnus-summary-select-article-buffer t]
2183              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2184              ["Isearch article..." gnus-summary-isearch-article t]
2185              ["Beginning of the article" gnus-summary-beginning-of-article t]
2186              ["End of the article" gnus-summary-end-of-article t]
2187              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2188              ["Fetch referenced articles" gnus-summary-refer-references t]
2189              ["Fetch current thread" gnus-summary-refer-thread t]
2190              ["Fetch article with id..." gnus-summary-refer-article t]
2191              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2192              ["Redisplay" gnus-summary-show-article t]
2193              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2194       (easy-menu-define
2195         gnus-summary-article-menu gnus-summary-mode-map ""
2196         (cons "Article" innards))
2197
2198       (if (not (keymapp gnus-summary-article-menu))
2199           (easy-menu-define
2200             gnus-article-commands-menu gnus-article-mode-map ""
2201             (cons "Commands" innards))
2202         ;; in Emacs, don't share menu.
2203         (setq gnus-article-commands-menu
2204               (copy-keymap gnus-summary-article-menu))
2205         (define-key gnus-article-mode-map [menu-bar commands]
2206           (cons "Commands" gnus-article-commands-menu))))
2207
2208     (easy-menu-define
2209       gnus-summary-thread-menu gnus-summary-mode-map ""
2210       '("Threads"
2211         ["Find all messages in thread" gnus-summary-refer-thread t]
2212         ["Toggle threading" gnus-summary-toggle-threads t]
2213         ["Hide threads" gnus-summary-hide-all-threads t]
2214         ["Show threads" gnus-summary-show-all-threads t]
2215         ["Hide thread" gnus-summary-hide-thread t]
2216         ["Show thread" gnus-summary-show-thread t]
2217         ["Go to next thread" gnus-summary-next-thread t]
2218         ["Go to previous thread" gnus-summary-prev-thread t]
2219         ["Go down thread" gnus-summary-down-thread t]
2220         ["Go up thread" gnus-summary-up-thread t]
2221         ["Top of thread" gnus-summary-top-thread t]
2222         ["Mark thread as read" gnus-summary-kill-thread t]
2223         ["Lower thread score" gnus-summary-lower-thread t]
2224         ["Raise thread score" gnus-summary-raise-thread t]
2225         ["Rethread current" gnus-summary-rethread-current t]))
2226
2227     (easy-menu-define
2228       gnus-summary-post-menu gnus-summary-mode-map ""
2229       `("Post"
2230         ["Send a message (mail or news)" gnus-summary-post-news
2231          ,@(if (featurep 'xemacs) '(t)
2232              '(:help "Post an article"))]
2233         ["Followup" gnus-summary-followup
2234          ,@(if (featurep 'xemacs) '(t)
2235              '(:help "Post followup to this article"))]
2236         ["Followup and yank" gnus-summary-followup-with-original
2237          ,@(if (featurep 'xemacs) '(t)
2238              '(:help "Post followup to this article, quoting its contents"))]
2239         ["Supersede article" gnus-summary-supersede-article t]
2240         ["Cancel article" gnus-summary-cancel-article
2241          ,@(if (featurep 'xemacs) '(t)
2242              '(:help "Cancel an article you posted"))]
2243         ["Reply" gnus-summary-reply t]
2244         ["Reply and yank" gnus-summary-reply-with-original t]
2245         ["Wide reply" gnus-summary-wide-reply t]
2246         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2247          ,@(if (featurep 'xemacs) '(t)
2248              '(:help "Mail a reply, quoting this article"))]
2249         ["Very wide reply" gnus-summary-very-wide-reply t]
2250         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2251          ,@(if (featurep 'xemacs) '(t)
2252              '(:help "Mail a very wide reply, quoting this article"))]
2253         ["Mail forward" gnus-summary-mail-forward t]
2254         ["Post forward" gnus-summary-post-forward t]
2255         ["Digest and mail" gnus-uu-digest-mail-forward t]
2256         ["Digest and post" gnus-uu-digest-post-forward t]
2257         ["Resend message" gnus-summary-resend-message t]
2258         ["Resend message edit" gnus-summary-resend-message-edit t]
2259         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2260         ["Send a mail" gnus-summary-mail-other-window t]
2261         ["Create a local message" gnus-summary-news-other-window t]
2262         ["Uuencode and post" gnus-uu-post-news
2263          ,@(if (featurep 'xemacs) '(t)
2264              '(:help "Post a uuencoded article"))]
2265         ["Followup via news" gnus-summary-followup-to-mail t]
2266         ["Followup via news and yank"
2267          gnus-summary-followup-to-mail-with-original t]
2268         ;;("Draft"
2269         ;;["Send" gnus-summary-send-draft t]
2270         ;;["Send bounced" gnus-resend-bounced-mail t])
2271         ))
2272
2273     (cond
2274      ((not (keymapp gnus-summary-post-menu))
2275       (setq gnus-article-post-menu gnus-summary-post-menu))
2276      ((not gnus-article-post-menu)
2277       ;; Don't share post menu.
2278       (setq gnus-article-post-menu
2279             (copy-keymap gnus-summary-post-menu))))
2280     (define-key gnus-article-mode-map [menu-bar post]
2281       (cons "Post" gnus-article-post-menu))
2282
2283     (easy-menu-define
2284       gnus-summary-misc-menu gnus-summary-mode-map ""
2285       `("Gnus"
2286         ("Mark Read"
2287          ["Mark as read" gnus-summary-mark-as-read-forward t]
2288          ["Mark same subject and select"
2289           gnus-summary-kill-same-subject-and-select t]
2290          ["Mark same subject" gnus-summary-kill-same-subject t]
2291          ["Catchup" gnus-summary-catchup
2292           ,@(if (featurep 'xemacs) '(t)
2293               '(:help "Mark unread articles in this group as read"))]
2294          ["Catchup all" gnus-summary-catchup-all t]
2295          ["Catchup to here" gnus-summary-catchup-to-here t]
2296          ["Catchup from here" gnus-summary-catchup-from-here t]
2297          ["Catchup region" gnus-summary-mark-region-as-read 
2298           (gnus-mark-active-p)]
2299          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2300         ("Mark Various"
2301          ["Tick" gnus-summary-tick-article-forward t]
2302          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2303          ["Remove marks" gnus-summary-clear-mark-forward t]
2304          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2305          ["Set bookmark" gnus-summary-set-bookmark t]
2306          ["Remove bookmark" gnus-summary-remove-bookmark t])
2307         ("Limit to"
2308          ["Marks..." gnus-summary-limit-to-marks t]
2309          ["Subject..." gnus-summary-limit-to-subject t]
2310          ["Author..." gnus-summary-limit-to-author t]
2311          ["Age..." gnus-summary-limit-to-age t]
2312          ["Extra..." gnus-summary-limit-to-extra t]
2313          ["Score..." gnus-summary-limit-to-score t]
2314          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2315          ["Unread" gnus-summary-limit-to-unread t]
2316          ["Unseen" gnus-summary-limit-to-unseen t]
2317          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2318          ["Next articles" gnus-summary-limit-to-articles t]
2319          ["Pop limit" gnus-summary-pop-limit t]
2320          ["Show dormant" gnus-summary-limit-include-dormant t]
2321          ["Hide childless dormant"
2322           gnus-summary-limit-exclude-childless-dormant t]
2323          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2324          ["Hide marked" gnus-summary-limit-exclude-marks t]
2325          ["Show expunged" gnus-summary-limit-include-expunged t])
2326         ("Process Mark"
2327          ["Set mark" gnus-summary-mark-as-processable t]
2328          ["Remove mark" gnus-summary-unmark-as-processable t]
2329          ["Remove all marks" gnus-summary-unmark-all-processable t]
2330          ["Mark above" gnus-uu-mark-over t]
2331          ["Mark series" gnus-uu-mark-series t]
2332          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2333          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2334          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2335          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2336          ["Mark all" gnus-uu-mark-all t]
2337          ["Mark buffer" gnus-uu-mark-buffer t]
2338          ["Mark sparse" gnus-uu-mark-sparse t]
2339          ["Mark thread" gnus-uu-mark-thread t]
2340          ["Unmark thread" gnus-uu-unmark-thread t]
2341          ("Process Mark Sets"
2342           ["Kill" gnus-summary-kill-process-mark t]
2343           ["Yank" gnus-summary-yank-process-mark
2344            gnus-newsgroup-process-stack]
2345           ["Save" gnus-summary-save-process-mark t]))
2346         ("Scroll article"
2347          ["Page forward" gnus-summary-next-page
2348           ,@(if (featurep 'xemacs) '(t)
2349               '(:help "Show next page of article"))]
2350          ["Page backward" gnus-summary-prev-page
2351           ,@(if (featurep 'xemacs) '(t)
2352               '(:help "Show previous page of article"))]
2353          ["Line forward" gnus-summary-scroll-up t])
2354         ("Move"
2355          ["Next unread article" gnus-summary-next-unread-article t]
2356          ["Previous unread article" gnus-summary-prev-unread-article t]
2357          ["Next article" gnus-summary-next-article t]
2358          ["Previous article" gnus-summary-prev-article t]
2359          ["Next unread subject" gnus-summary-next-unread-subject t]
2360          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2361          ["Next article same subject" gnus-summary-next-same-subject t]
2362          ["Previous article same subject" gnus-summary-prev-same-subject t]
2363          ["First unread article" gnus-summary-first-unread-article t]
2364          ["Best unread article" gnus-summary-best-unread-article t]
2365          ["Go to subject number..." gnus-summary-goto-subject t]
2366          ["Go to article number..." gnus-summary-goto-article t]
2367          ["Go to the last article" gnus-summary-goto-last-article t]
2368          ["Pop article off history" gnus-summary-pop-article t])
2369         ("Sort"
2370          ["Sort by number" gnus-summary-sort-by-number t]
2371          ["Sort by author" gnus-summary-sort-by-author t]
2372          ["Sort by subject" gnus-summary-sort-by-subject t]
2373          ["Sort by date" gnus-summary-sort-by-date t]
2374          ["Sort by score" gnus-summary-sort-by-score t]
2375          ["Sort by lines" gnus-summary-sort-by-lines t]
2376          ["Sort by characters" gnus-summary-sort-by-chars t]
2377          ["Randomize" gnus-summary-sort-by-random t]
2378          ["Original sort" gnus-summary-sort-by-original t])
2379         ("Help"
2380          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2381          ["Describe group" gnus-summary-describe-group t]
2382          ["Fetch charter" gnus-group-fetch-charter
2383           ,@(if (featurep 'xemacs) nil
2384               '(:help "Display the charter of the current group"))]
2385          ["Fetch control message" gnus-group-fetch-control
2386           ,@(if (featurep 'xemacs) nil
2387               '(:help "Display the archived control message for the current group"))]
2388          ["Read manual" gnus-info-find-node t])
2389         ("Modes"
2390          ["Pick and read" gnus-pick-mode t]
2391          ["Binary" gnus-binary-mode t])
2392         ("Regeneration"
2393          ["Regenerate" gnus-summary-prepare t]
2394          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2395          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2396          ["Toggle threading" gnus-summary-toggle-threads t])
2397         ["See old articles" gnus-summary-insert-old-articles t]
2398         ["See new articles" gnus-summary-insert-new-articles t]
2399         ["Filter articles..." gnus-summary-execute-command t]
2400         ["Run command on subjects..." gnus-summary-universal-argument t]
2401         ["Search articles forward..." gnus-summary-search-article-forward t]
2402         ["Search articles backward..." gnus-summary-search-article-backward t]
2403         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2404         ["Expand window" gnus-summary-expand-window t]
2405         ["Expire expirable articles" gnus-summary-expire-articles
2406          (gnus-check-backend-function
2407           'request-expire-articles gnus-newsgroup-name)]
2408         ["Edit local kill file" gnus-summary-edit-local-kill t]
2409         ["Edit main kill file" gnus-summary-edit-global-kill t]
2410         ["Edit group parameters" gnus-summary-edit-parameters t]
2411         ["Customize group parameters" gnus-summary-customize-parameters t]
2412         ["Send a bug report" gnus-bug t]
2413         ("Exit"
2414          ["Catchup and exit" gnus-summary-catchup-and-exit
2415           ,@(if (featurep 'xemacs) '(t)
2416               '(:help "Mark unread articles in this group as read, then exit"))]
2417          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2418          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2419          ["Exit group" gnus-summary-exit
2420           ,@(if (featurep 'xemacs) '(t)
2421               '(:help "Exit current group, return to group selection mode"))]
2422          ["Exit group without updating" gnus-summary-exit-no-update t]
2423          ["Exit and goto next group" gnus-summary-next-group t]
2424          ["Exit and goto prev group" gnus-summary-prev-group t]
2425          ["Reselect group" gnus-summary-reselect-current-group t]
2426          ["Rescan group" gnus-summary-rescan-group t]
2427          ["Update dribble" gnus-summary-save-newsrc t])))
2428
2429     (gnus-run-hooks 'gnus-summary-menu-hook)))
2430
2431 (defvar gnus-summary-tool-bar-map nil)
2432
2433 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2434 (defun gnus-summary-make-tool-bar ()
2435   (if (and (fboundp 'tool-bar-add-item-from-menu)
2436            (default-value 'tool-bar-mode)
2437            (not gnus-summary-tool-bar-map))
2438       (setq gnus-summary-tool-bar-map
2439             (let ((tool-bar-map (make-sparse-keymap))
2440                   (load-path (mm-image-load-path)))
2441               (tool-bar-add-item-from-menu
2442                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2443               (tool-bar-add-item-from-menu
2444                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2445               (tool-bar-add-item-from-menu
2446                'gnus-summary-post-news "post" gnus-summary-mode-map)
2447               (tool-bar-add-item-from-menu
2448                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2449               (tool-bar-add-item-from-menu
2450                'gnus-summary-followup "followup" gnus-summary-mode-map)
2451               (tool-bar-add-item-from-menu
2452                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2453               (tool-bar-add-item-from-menu
2454                'gnus-summary-reply "reply" gnus-summary-mode-map)
2455               (tool-bar-add-item-from-menu
2456                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2457               (tool-bar-add-item-from-menu
2458                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2459               (tool-bar-add-item-from-menu
2460                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2461               (tool-bar-add-item-from-menu
2462                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2463               (tool-bar-add-item-from-menu
2464                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2465               (tool-bar-add-item-from-menu
2466                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2467               (tool-bar-add-item-from-menu
2468                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2469               (tool-bar-add-item-from-menu
2470                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2471               tool-bar-map)))
2472   (if gnus-summary-tool-bar-map
2473       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2474
2475 (defun gnus-score-set-default (var value)
2476   "A version of set that updates the GNU Emacs menu-bar."
2477   (set var value)
2478   ;; It is the message that forces the active status to be updated.
2479   (message ""))
2480
2481 (defun gnus-make-score-map (type)
2482   "Make a summary score map of type TYPE."
2483   (if t
2484       nil
2485     (let ((headers '(("author" "from" string)
2486                      ("subject" "subject" string)
2487                      ("article body" "body" string)
2488                      ("article head" "head" string)
2489                      ("xref" "xref" string)
2490                      ("extra header" "extra" string)
2491                      ("lines" "lines" number)
2492                      ("followups to author" "followup" string)))
2493           (types '((number ("less than" <)
2494                            ("greater than" >)
2495                            ("equal" =))
2496                    (string ("substring" s)
2497                            ("exact string" e)
2498                            ("fuzzy string" f)
2499                            ("regexp" r))))
2500           (perms '(("temporary" (current-time-string))
2501                    ("permanent" nil)
2502                    ("immediate" now)))
2503           header)
2504       (list
2505        (apply
2506         'nconc
2507         (list
2508          (if (eq type 'lower)
2509              "Lower score"
2510            "Increase score"))
2511         (let (outh)
2512           (while headers
2513             (setq header (car headers))
2514             (setq outh
2515                   (cons
2516                    (apply
2517                     'nconc
2518                     (list (car header))
2519                     (let ((ts (cdr (assoc (nth 2 header) types)))
2520                           outt)
2521                       (while ts
2522                         (setq outt
2523                               (cons
2524                                (apply
2525                                 'nconc
2526                                 (list (caar ts))
2527                                 (let ((ps perms)
2528                                       outp)
2529                                   (while ps
2530                                     (setq outp
2531                                           (cons
2532                                            (vector
2533                                             (caar ps)
2534                                             (list
2535                                              'gnus-summary-score-entry
2536                                              (nth 1 header)
2537                                              (if (or (string= (nth 1 header)
2538                                                               "head")
2539                                                      (string= (nth 1 header)
2540                                                               "body"))
2541                                                  ""
2542                                                (list 'gnus-summary-header
2543                                                      (nth 1 header)))
2544                                              (list 'quote (nth 1 (car ts)))
2545                                              (list 'gnus-score-delta-default
2546                                                    nil)
2547                                              (nth 1 (car ps))
2548                                              t)
2549                                             t)
2550                                            outp))
2551                                     (setq ps (cdr ps)))
2552                                   (list (nreverse outp))))
2553                                outt))
2554                         (setq ts (cdr ts)))
2555                       (list (nreverse outt))))
2556                    outh))
2557             (setq headers (cdr headers)))
2558           (list (nreverse outh))))))))
2559
2560 \f
2561
2562 (defun gnus-summary-mode (&optional group)
2563   "Major mode for reading articles.
2564
2565 All normal editing commands are switched off.
2566 \\<gnus-summary-mode-map>
2567 Each line in this buffer represents one article.  To read an
2568 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2569 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2570 respectively.
2571
2572 You can also post articles and send mail from this buffer.  To
2573 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2574 of an article, type `\\[gnus-summary-reply]'.
2575
2576 There are approx. one gazillion commands you can execute in this
2577 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2578
2579 The following commands are available:
2580
2581 \\{gnus-summary-mode-map}"
2582   (interactive)
2583   (kill-all-local-variables)
2584   (when (gnus-visual-p 'summary-menu 'menu)
2585     (gnus-summary-make-menu-bar)
2586     (gnus-summary-make-tool-bar))
2587   (gnus-summary-make-local-variables)
2588   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2589     (gnus-summary-make-local-variables))
2590   (gnus-make-thread-indent-array)
2591   (gnus-simplify-mode-line)
2592   (setq major-mode 'gnus-summary-mode)
2593   (setq mode-name "Summary")
2594   (make-local-variable 'minor-mode-alist)
2595   (use-local-map gnus-summary-mode-map)
2596   (buffer-disable-undo)
2597   (setq buffer-read-only t)             ;Disable modification
2598   (setq truncate-lines t)
2599   (setq selective-display t)
2600   (setq selective-display-ellipses t)   ;Display `...'
2601   (gnus-summary-set-display-table)
2602   (gnus-set-default-directory)
2603   (setq gnus-newsgroup-name group)
2604   (make-local-variable 'gnus-summary-line-format)
2605   (make-local-variable 'gnus-summary-line-format-spec)
2606   (make-local-variable 'gnus-summary-dummy-line-format)
2607   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2608   (make-local-variable 'gnus-summary-mark-positions)
2609   (make-local-hook 'pre-command-hook)
2610   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2611   (gnus-run-hooks 'gnus-summary-mode-hook)
2612   (turn-on-gnus-mailing-list-mode)
2613   (mm-enable-multibyte)
2614   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2615   (gnus-update-summary-mark-positions))
2616
2617 (defun gnus-summary-make-local-variables ()
2618   "Make all the local summary buffer variables."
2619   (let (global)
2620     (dolist (local gnus-summary-local-variables)
2621       (if (consp local)
2622           (progn
2623             (if (eq (cdr local) 'global)
2624                 ;; Copy the global value of the variable.
2625                 (setq global (symbol-value (car local)))
2626               ;; Use the value from the list.
2627               (setq global (eval (cdr local))))
2628             (set (make-local-variable (car local)) global))
2629         ;; Simple nil-valued local variable.
2630         (set (make-local-variable local) nil)))))
2631
2632 (defun gnus-summary-clear-local-variables ()
2633   (let ((locals gnus-summary-local-variables))
2634     (while locals
2635       (if (consp (car locals))
2636           (and (vectorp (caar locals))
2637                (set (caar locals) nil))
2638         (and (vectorp (car locals))
2639              (set (car locals) nil)))
2640       (setq locals (cdr locals)))))
2641
2642 ;; Summary data functions.
2643
2644 (defmacro gnus-data-number (data)
2645   `(car ,data))
2646
2647 (defmacro gnus-data-set-number (data number)
2648   `(setcar ,data ,number))
2649
2650 (defmacro gnus-data-mark (data)
2651   `(nth 1 ,data))
2652
2653 (defmacro gnus-data-set-mark (data mark)
2654   `(setcar (nthcdr 1 ,data) ,mark))
2655
2656 (defmacro gnus-data-pos (data)
2657   `(nth 2 ,data))
2658
2659 (defmacro gnus-data-set-pos (data pos)
2660   `(setcar (nthcdr 2 ,data) ,pos))
2661
2662 (defmacro gnus-data-header (data)
2663   `(nth 3 ,data))
2664
2665 (defmacro gnus-data-set-header (data header)
2666   `(setf (nth 3 ,data) ,header))
2667
2668 (defmacro gnus-data-level (data)
2669   `(nth 4 ,data))
2670
2671 (defmacro gnus-data-unread-p (data)
2672   `(= (nth 1 ,data) gnus-unread-mark))
2673
2674 (defmacro gnus-data-read-p (data)
2675   `(/= (nth 1 ,data) gnus-unread-mark))
2676
2677 (defmacro gnus-data-pseudo-p (data)
2678   `(consp (nth 3 ,data)))
2679
2680 (defmacro gnus-data-find (number)
2681   `(assq ,number gnus-newsgroup-data))
2682
2683 (defmacro gnus-data-find-list (number &optional data)
2684   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2685      (memq (assq ,number bdata)
2686            bdata)))
2687
2688 (defmacro gnus-data-make (number mark pos header level)
2689   `(list ,number ,mark ,pos ,header ,level))
2690
2691 (defun gnus-data-enter (after-article number mark pos header level offset)
2692   (let ((data (gnus-data-find-list after-article)))
2693     (unless data
2694       (error "No such article: %d" after-article))
2695     (setcdr data (cons (gnus-data-make number mark pos header level)
2696                        (cdr data)))
2697     (setq gnus-newsgroup-data-reverse nil)
2698     (gnus-data-update-list (cddr data) offset)))
2699
2700 (defun gnus-data-enter-list (after-article list &optional offset)
2701   (when list
2702     (let ((data (and after-article (gnus-data-find-list after-article)))
2703           (ilist list))
2704       (if (not (or data
2705                    after-article))
2706           (let ((odata gnus-newsgroup-data))
2707             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2708             (when offset
2709               (gnus-data-update-list odata offset)))
2710       ;; Find the last element in the list to be spliced into the main
2711         ;; list.
2712         (while (cdr list)
2713           (setq list (cdr list)))
2714         (if (not data)
2715             (progn
2716               (setcdr list gnus-newsgroup-data)
2717               (setq gnus-newsgroup-data ilist)
2718               (when offset
2719                 (gnus-data-update-list (cdr list) offset)))
2720           (setcdr list (cdr data))
2721           (setcdr data ilist)
2722           (when offset
2723             (gnus-data-update-list (cdr list) offset))))
2724       (setq gnus-newsgroup-data-reverse nil))))
2725
2726 (defun gnus-data-remove (article &optional offset)
2727   (let ((data gnus-newsgroup-data))
2728     (if (= (gnus-data-number (car data)) article)
2729         (progn
2730           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2731                 gnus-newsgroup-data-reverse nil)
2732           (when offset
2733             (gnus-data-update-list gnus-newsgroup-data offset)))
2734       (while (cdr data)
2735         (when (= (gnus-data-number (cadr data)) article)
2736           (setcdr data (cddr data))
2737           (when offset
2738             (gnus-data-update-list (cdr data) offset))
2739           (setq data nil
2740                 gnus-newsgroup-data-reverse nil))
2741         (setq data (cdr data))))))
2742
2743 (defmacro gnus-data-list (backward)
2744   `(if ,backward
2745        (or gnus-newsgroup-data-reverse
2746            (setq gnus-newsgroup-data-reverse
2747                  (reverse gnus-newsgroup-data)))
2748      gnus-newsgroup-data))
2749
2750 (defun gnus-data-update-list (data offset)
2751   "Add OFFSET to the POS of all data entries in DATA."
2752   (setq gnus-newsgroup-data-reverse nil)
2753   (while data
2754     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2755     (setq data (cdr data))))
2756
2757 (defun gnus-summary-article-pseudo-p (article)
2758   "Say whether this article is a pseudo article or not."
2759   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2760
2761 (defmacro gnus-summary-article-sparse-p (article)
2762   "Say whether this article is a sparse article or not."
2763   `(memq ,article gnus-newsgroup-sparse))
2764
2765 (defmacro gnus-summary-article-ancient-p (article)
2766   "Say whether this article is a sparse article or not."
2767   `(memq ,article gnus-newsgroup-ancient))
2768
2769 (defun gnus-article-parent-p (number)
2770   "Say whether this article is a parent or not."
2771   (let ((data (gnus-data-find-list number)))
2772     (and (cdr data)              ; There has to be an article after...
2773          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2774             (gnus-data-level (nth 1 data))))))
2775
2776 (defun gnus-article-children (number)
2777   "Return a list of all children to NUMBER."
2778   (let* ((data (gnus-data-find-list number))
2779          (level (gnus-data-level (car data)))
2780          children)
2781     (setq data (cdr data))
2782     (while (and data
2783                 (= (gnus-data-level (car data)) (1+ level)))
2784       (push (gnus-data-number (car data)) children)
2785       (setq data (cdr data)))
2786     children))
2787
2788 (defmacro gnus-summary-skip-intangible ()
2789   "If the current article is intangible, then jump to a different article."
2790   '(let ((to (get-text-property (point) 'gnus-intangible)))
2791      (and to (gnus-summary-goto-subject to))))
2792
2793 (defmacro gnus-summary-article-intangible-p ()
2794   "Say whether this article is intangible or not."
2795   '(get-text-property (point) 'gnus-intangible))
2796
2797 (defun gnus-article-read-p (article)
2798   "Say whether ARTICLE is read or not."
2799   (not (or (memq article gnus-newsgroup-marked)
2800            (memq article gnus-newsgroup-spam-marked)
2801            (memq article gnus-newsgroup-unreads)
2802            (memq article gnus-newsgroup-unselected)
2803            (memq article gnus-newsgroup-dormant))))
2804
2805 ;; Some summary mode macros.
2806
2807 (defmacro gnus-summary-article-number ()
2808   "The article number of the article on the current line.
2809 If there isn't an article number here, then we return the current
2810 article number."
2811   '(progn
2812      (gnus-summary-skip-intangible)
2813      (or (get-text-property (point) 'gnus-number)
2814          (gnus-summary-last-subject))))
2815
2816 (defmacro gnus-summary-article-header (&optional number)
2817   "Return the header of article NUMBER."
2818   `(gnus-data-header (gnus-data-find
2819                       ,(or number '(gnus-summary-article-number)))))
2820
2821 (defmacro gnus-summary-thread-level (&optional number)
2822   "Return the level of thread that starts with article NUMBER."
2823   `(if (and (eq gnus-summary-make-false-root 'dummy)
2824             (get-text-property (point) 'gnus-intangible))
2825        0
2826      (gnus-data-level (gnus-data-find
2827                        ,(or number '(gnus-summary-article-number))))))
2828
2829 (defmacro gnus-summary-article-mark (&optional number)
2830   "Return the mark of article NUMBER."
2831   `(gnus-data-mark (gnus-data-find
2832                     ,(or number '(gnus-summary-article-number)))))
2833
2834 (defmacro gnus-summary-article-pos (&optional number)
2835   "Return the position of the line of article NUMBER."
2836   `(gnus-data-pos (gnus-data-find
2837                    ,(or number '(gnus-summary-article-number)))))
2838
2839 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2840 (defmacro gnus-summary-article-subject (&optional number)
2841   "Return current subject string or nil if nothing."
2842   `(let ((headers
2843           ,(if number
2844                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2845              '(gnus-data-header (assq (gnus-summary-article-number)
2846                                       gnus-newsgroup-data)))))
2847      (and headers
2848           (vectorp headers)
2849           (mail-header-subject headers))))
2850
2851 (defmacro gnus-summary-article-score (&optional number)
2852   "Return current article score."
2853   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2854                   gnus-newsgroup-scored))
2855        gnus-summary-default-score 0))
2856
2857 (defun gnus-summary-article-children (&optional number)
2858   "Return a list of article numbers that are children of article NUMBER."
2859   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2860          (level (gnus-data-level (car data)))
2861          l children)
2862     (while (and (setq data (cdr data))
2863                 (> (setq l (gnus-data-level (car data))) level))
2864       (and (= (1+ level) l)
2865            (push (gnus-data-number (car data))
2866                  children)))
2867     (nreverse children)))
2868
2869 (defun gnus-summary-article-parent (&optional number)
2870   "Return the article number of the parent of article NUMBER."
2871   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2872                                     (gnus-data-list t)))
2873          (level (gnus-data-level (car data))))
2874     (if (zerop level)
2875         ()                              ; This is a root.
2876       ;; We search until we find an article with a level less than
2877       ;; this one.  That function has to be the parent.
2878       (while (and (setq data (cdr data))
2879                   (not (< (gnus-data-level (car data)) level))))
2880       (and data (gnus-data-number (car data))))))
2881
2882 (defun gnus-unread-mark-p (mark)
2883   "Say whether MARK is the unread mark."
2884   (= mark gnus-unread-mark))
2885
2886 (defun gnus-read-mark-p (mark)
2887   "Say whether MARK is one of the marks that mark as read.
2888 This is all marks except unread, ticked, dormant, and expirable."
2889   (not (or (= mark gnus-unread-mark)
2890            (= mark gnus-ticked-mark)
2891            (= mark gnus-spam-mark)
2892            (= mark gnus-dormant-mark)
2893            (= mark gnus-expirable-mark))))
2894
2895 (defmacro gnus-article-mark (number)
2896   "Return the MARK of article NUMBER.
2897 This macro should only be used when computing the mark the \"first\"
2898 time; i.e., when generating the summary lines.  After that,
2899 `gnus-summary-article-mark' should be used to examine the
2900 marks of articles."
2901   `(cond
2902     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2903     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2904     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2905     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2906     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2907     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2908     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2909     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2910            gnus-ancient-mark))))
2911
2912 ;; Saving hidden threads.
2913
2914 (defmacro gnus-save-hidden-threads (&rest forms)
2915   "Save hidden threads, eval FORMS, and restore the hidden threads."
2916   (let ((config (make-symbol "config")))
2917     `(let ((,config (gnus-hidden-threads-configuration)))
2918        (unwind-protect
2919            (save-excursion
2920              ,@forms)
2921          (gnus-restore-hidden-threads-configuration ,config)))))
2922 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2923 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2924
2925 (defun gnus-data-compute-positions ()
2926   "Compute the positions of all articles."
2927   (setq gnus-newsgroup-data-reverse nil)
2928   (let ((data gnus-newsgroup-data))
2929     (save-excursion
2930       (gnus-save-hidden-threads
2931         (gnus-summary-show-all-threads)
2932         (goto-char (point-min))
2933         (while data
2934           (while (get-text-property (point) 'gnus-intangible)
2935             (forward-line 1))
2936           (gnus-data-set-pos (car data) (+ (point) 3))
2937           (setq data (cdr data))
2938           (forward-line 1))))))
2939
2940 (defun gnus-hidden-threads-configuration ()
2941   "Return the current hidden threads configuration."
2942   (save-excursion
2943     (let (config)
2944       (goto-char (point-min))
2945       (while (search-forward "\r" nil t)
2946         (push (1- (point)) config))
2947       config)))
2948
2949 (defun gnus-restore-hidden-threads-configuration (config)
2950   "Restore hidden threads configuration from CONFIG."
2951   (save-excursion
2952     (let (point buffer-read-only)
2953       (while (setq point (pop config))
2954         (when (and (< point (point-max))
2955                    (goto-char point)
2956                    (eq (char-after) ?\n))
2957           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2958
2959 ;; Various summary mode internalish functions.
2960
2961 (defun gnus-mouse-pick-article (e)
2962   (interactive "e")
2963   (mouse-set-point e)
2964   (gnus-summary-next-page nil t))
2965
2966 (defun gnus-summary-set-display-table ()
2967   "Change the display table.
2968 Odd characters have a tendency to mess
2969 up nicely formatted displays - we make all possible glyphs
2970 display only a single character."
2971
2972   ;; We start from the standard display table, if any.
2973   (let ((table (or (copy-sequence standard-display-table)
2974                    (make-display-table)))
2975         (i 32))
2976     ;; Nix out all the control chars...
2977     (while (>= (setq i (1- i)) 0)
2978       (aset table i [??]))
2979    ;; ... but not newline and cr, of course.  (cr is necessary for the
2980     ;; selective display).
2981     (aset table ?\n nil)
2982     (aset table ?\r nil)
2983     ;; We keep TAB as well.
2984     (aset table ?\t nil)
2985     ;; We nix out any glyphs over 126 that are not set already.
2986     (let ((i 256))
2987       (while (>= (setq i (1- i)) 127)
2988         ;; Only modify if the entry is nil.
2989         (unless (aref table i)
2990           (aset table i [??]))))
2991     (setq buffer-display-table table)))
2992
2993 (defun gnus-summary-set-article-display-arrow (pos)
2994   "Update the overlay arrow to point to line at position POS."
2995   (when (and gnus-summary-display-arrow
2996              (boundp 'overlay-arrow-position)
2997              (boundp 'overlay-arrow-string))
2998     (save-excursion
2999       (goto-char pos)
3000       (beginning-of-line)
3001       (unless overlay-arrow-position
3002         (setq overlay-arrow-position (make-marker)))
3003       (setq overlay-arrow-string "=>"
3004             overlay-arrow-position (set-marker overlay-arrow-position
3005                                                (point)
3006                                                (current-buffer))))))
3007
3008 (defun gnus-summary-buffer-name (group)
3009   "Return the summary buffer name of GROUP."
3010   (concat "*Summary " (gnus-group-decoded-name group) "*"))
3011
3012 (defun gnus-summary-setup-buffer (group)
3013   "Initialize summary buffer."
3014   (let ((buffer (gnus-summary-buffer-name group))
3015         (dead-name (concat "*Dead Summary "
3016                            (gnus-group-decoded-name group) "*")))
3017     ;; If a dead summary buffer exists, we kill it.
3018     (when (gnus-buffer-live-p dead-name)
3019       (gnus-kill-buffer dead-name))
3020     (if (get-buffer buffer)
3021         (progn
3022           (set-buffer buffer)
3023           (setq gnus-summary-buffer (current-buffer))
3024           (not gnus-newsgroup-prepared))
3025       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3026       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3027       (gnus-summary-mode group)
3028       (when gnus-carpal
3029         (gnus-carpal-setup-buffer 'summary))
3030       (unless gnus-single-article-buffer
3031         (make-local-variable 'gnus-article-buffer)
3032         (make-local-variable 'gnus-article-current)
3033         (make-local-variable 'gnus-original-article-buffer))
3034       (setq gnus-newsgroup-name group)
3035       ;; Set any local variables in the group parameters.
3036       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3037       t)))
3038
3039 (defun gnus-set-global-variables ()
3040   "Set the global equivalents of the buffer-local variables.
3041 They are set to the latest values they had.  These reflect the summary
3042 buffer that was in action when the last article was fetched."
3043   (when (eq major-mode 'gnus-summary-mode)
3044     (setq gnus-summary-buffer (current-buffer))
3045     (let ((name gnus-newsgroup-name)
3046           (marked gnus-newsgroup-marked)
3047           (spam gnus-newsgroup-spam-marked)
3048           (unread gnus-newsgroup-unreads)
3049           (headers gnus-current-headers)
3050           (data gnus-newsgroup-data)
3051           (summary gnus-summary-buffer)
3052           (article-buffer gnus-article-buffer)
3053           (original gnus-original-article-buffer)
3054           (gac gnus-article-current)
3055           (reffed gnus-reffed-article-number)
3056           (score-file gnus-current-score-file)
3057           (default-charset gnus-newsgroup-charset)
3058           vlist)
3059       (let ((locals gnus-newsgroup-variables))
3060         (while locals
3061           (if (consp (car locals))
3062               (push (eval (caar locals)) vlist)
3063             (push (eval (car locals)) vlist))
3064           (setq locals (cdr locals)))
3065         (setq vlist (nreverse vlist)))
3066       (save-excursion
3067         (set-buffer gnus-group-buffer)
3068         (setq gnus-newsgroup-name name
3069               gnus-newsgroup-marked marked
3070               gnus-newsgroup-spam-marked spam
3071               gnus-newsgroup-unreads unread
3072               gnus-current-headers headers
3073               gnus-newsgroup-data data
3074               gnus-article-current gac
3075               gnus-summary-buffer summary
3076               gnus-article-buffer article-buffer
3077               gnus-original-article-buffer original
3078               gnus-reffed-article-number reffed
3079               gnus-current-score-file score-file
3080               gnus-newsgroup-charset default-charset)
3081         (let ((locals gnus-newsgroup-variables))
3082           (while locals
3083             (if (consp (car locals))
3084                 (set (caar locals) (pop vlist))
3085               (set (car locals) (pop vlist)))
3086             (setq locals (cdr locals))))
3087         ;; The article buffer also has local variables.
3088         (when (gnus-buffer-live-p gnus-article-buffer)
3089           (set-buffer gnus-article-buffer)
3090           (setq gnus-summary-buffer summary))))))
3091
3092 (defun gnus-summary-article-unread-p (article)
3093   "Say whether ARTICLE is unread or not."
3094   (memq article gnus-newsgroup-unreads))
3095
3096 (defun gnus-summary-first-article-p (&optional article)
3097   "Return whether ARTICLE is the first article in the buffer."
3098   (if (not (setq article (or article (gnus-summary-article-number))))
3099       nil
3100     (eq article (caar gnus-newsgroup-data))))
3101
3102 (defun gnus-summary-last-article-p (&optional article)
3103   "Return whether ARTICLE is the last article in the buffer."
3104   (if (not (setq article (or article (gnus-summary-article-number))))
3105       ;; All non-existent numbers are the last article.  :-)
3106       t
3107     (not (cdr (gnus-data-find-list article)))))
3108
3109 (defun gnus-make-thread-indent-array ()
3110   (let ((n 200))
3111     (unless (and gnus-thread-indent-array
3112                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3113       (setq gnus-thread-indent-array (make-vector 201 "")
3114             gnus-thread-indent-array-level gnus-thread-indent-level)
3115       (while (>= n 0)
3116         (aset gnus-thread-indent-array n
3117               (make-string (* n gnus-thread-indent-level) ? ))
3118         (setq n (1- n))))))
3119
3120 (defun gnus-update-summary-mark-positions ()
3121   "Compute where the summary marks are to go."
3122   (save-excursion
3123     (when (gnus-buffer-exists-p gnus-summary-buffer)
3124       (set-buffer gnus-summary-buffer))
3125     (let ((gnus-replied-mark 129)
3126           (gnus-score-below-mark 130)
3127           (gnus-score-over-mark 130)
3128           (gnus-undownloaded-mark 131)
3129           (spec gnus-summary-line-format-spec)
3130           gnus-visual pos)
3131       (save-excursion
3132         (gnus-set-work-buffer)
3133         (let ((gnus-summary-line-format-spec spec)
3134               (gnus-newsgroup-downloadable '(0)))
3135           (gnus-summary-insert-line
3136            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3137            0 nil t 128 t nil "" nil 1)
3138           (goto-char (point-min))
3139           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3140                                              (- (point) (point-min) 1)))))
3141           (goto-char (point-min))
3142           (push (cons 'replied (and (search-forward "\201" nil t)
3143                                     (- (point) (point-min) 1)))
3144                 pos)
3145           (goto-char (point-min))
3146           (push (cons 'score (and (search-forward "\202" nil t)
3147                                   (- (point) (point-min) 1)))
3148                 pos)
3149           (goto-char (point-min))
3150           (push (cons 'download
3151                       (and (search-forward "\203" nil t)
3152                            (- (point) (point-min) 1)))
3153                 pos)))
3154       (setq gnus-summary-mark-positions pos))))
3155
3156 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3157   "Insert a dummy root in the summary buffer."
3158   (beginning-of-line)
3159   (gnus-add-text-properties
3160    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3161    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3162
3163 (defun gnus-summary-extract-address-component (from)
3164   (or (car (funcall gnus-extract-address-components from))
3165       from))
3166
3167 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3168   (let ((mail-parse-charset gnus-newsgroup-charset)
3169         ; Is it really necessary to do this next part for each summary line?
3170         ; Luckily, doesn't seem to slow things down much.
3171         (mail-parse-ignored-charsets
3172          (save-excursion (set-buffer gnus-summary-buffer)
3173                          gnus-newsgroup-ignored-charsets)))
3174     (or
3175      (and gnus-ignored-from-addresses
3176           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3177           (let ((extra-headers (mail-header-extra header))
3178                 to
3179                 newsgroups)
3180             (cond
3181              ((setq to (cdr (assq 'To extra-headers)))
3182               (concat "-> "
3183                       (inline
3184                         (gnus-summary-extract-address-component
3185                          (funcall gnus-decode-encoded-word-function to)))))
3186              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3187               (concat "=> " newsgroups)))))
3188      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3189
3190 (defun gnus-summary-insert-line (gnus-tmp-header
3191                                  gnus-tmp-level gnus-tmp-current
3192                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3193                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3194                                  &optional gnus-tmp-dummy gnus-tmp-score
3195                                  gnus-tmp-process)
3196   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3197          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3198          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3199          (gnus-tmp-score-char
3200           (if (or (null gnus-summary-default-score)
3201                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3202                       gnus-summary-zcore-fuzz))
3203               ?                         ;Whitespace
3204             (if (< gnus-tmp-score gnus-summary-default-score)
3205                 gnus-score-below-mark gnus-score-over-mark)))
3206          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3207          (gnus-tmp-replied
3208           (cond (gnus-tmp-process gnus-process-mark)
3209                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3210                  gnus-cached-mark)
3211                 (gnus-tmp-replied gnus-replied-mark)
3212                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3213                  gnus-forwarded-mark)
3214                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3215                  gnus-saved-mark)
3216                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3217                  gnus-recent-mark)
3218                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3219                  gnus-unseen-mark)
3220                 (t gnus-no-mark)))
3221          (gnus-tmp-downloaded
3222           (cond (undownloaded 
3223                  gnus-undownloaded-mark)
3224                 (gnus-newsgroup-agentized
3225                  gnus-downloaded-mark)
3226                 (t
3227                  gnus-no-mark)))
3228          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3229          (gnus-tmp-name
3230           (cond
3231            ((string-match "<[^>]+> *$" gnus-tmp-from)
3232             (let ((beg (match-beginning 0)))
3233               (or (and (string-match "^\".+\"" gnus-tmp-from)
3234                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3235                   (substring gnus-tmp-from 0 beg))))
3236            ((string-match "(.+)" gnus-tmp-from)
3237             (substring gnus-tmp-from
3238                        (1+ (match-beginning 0)) (1- (match-end 0))))
3239            (t gnus-tmp-from)))
3240          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3241          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3242          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3243          (buffer-read-only nil))
3244     (when (string= gnus-tmp-name "")
3245       (setq gnus-tmp-name gnus-tmp-from))
3246     (unless (numberp gnus-tmp-lines)
3247       (setq gnus-tmp-lines -1))
3248     (if (= gnus-tmp-lines -1)
3249         (setq gnus-tmp-lines "?")
3250       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3251     (gnus-put-text-property
3252      (point)
3253      (progn (eval gnus-summary-line-format-spec) (point))
3254      'gnus-number gnus-tmp-number)
3255     (when (gnus-visual-p 'summary-highlight 'highlight)
3256       (forward-line -1)
3257       (gnus-run-hooks 'gnus-summary-update-hook)
3258       (forward-line 1))))
3259
3260 (defun gnus-summary-update-line (&optional dont-update)
3261   "Update summary line after change."
3262   (when (and gnus-summary-default-score
3263              (not gnus-summary-inhibit-highlight))
3264     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3265            (article (gnus-summary-article-number))
3266            (score (gnus-summary-article-score article)))
3267       (unless dont-update
3268         (if (and gnus-summary-mark-below
3269                  (< (gnus-summary-article-score)
3270                     gnus-summary-mark-below))
3271             ;; This article has a low score, so we mark it as read.
3272             (when (memq article gnus-newsgroup-unreads)
3273               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3274           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3275             ;; This article was previously marked as read on account
3276             ;; of a low score, but now it has risen, so we mark it as
3277             ;; unread.
3278             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3279         (gnus-summary-update-mark
3280          (if (or (null gnus-summary-default-score)
3281                  (<= (abs (- score gnus-summary-default-score))
3282                      gnus-summary-zcore-fuzz))
3283              ?                          ;Whitespace
3284            (if (< score gnus-summary-default-score)
3285                gnus-score-below-mark gnus-score-over-mark))
3286          'score))
3287       ;; Do visual highlighting.
3288       (when (gnus-visual-p 'summary-highlight 'highlight)
3289         (gnus-run-hooks 'gnus-summary-update-hook)))))
3290
3291 (defvar gnus-tmp-new-adopts nil)
3292
3293 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3294   "Return the number of articles in THREAD.
3295 This may be 0 in some cases -- if none of the articles in
3296 the thread are to be displayed."
3297   (let* ((number
3298          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3299           (cond
3300            ((not (listp thread))
3301             1)
3302            ((and (consp thread) (cdr thread))
3303             (apply
3304              '+ 1 (mapcar
3305                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3306            ((null thread)
3307             1)
3308            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3309             1)
3310            (t 0))))
3311     (when (and level (zerop level) gnus-tmp-new-adopts)
3312       (incf number
3313             (apply '+ (mapcar
3314                        'gnus-summary-number-of-articles-in-thread
3315                        gnus-tmp-new-adopts))))
3316     (if char
3317         (if (> number 1) gnus-not-empty-thread-mark
3318           gnus-empty-thread-mark)
3319       number)))
3320
3321 (defsubst gnus-summary-line-message-size (head)
3322   "Return pretty-printed version of message size.
3323 This function is intended to be used in
3324 `gnus-summary-line-format-alist', which see."
3325   (let ((c (or (mail-header-chars head) -1)))
3326     (cond ((< c 0) "n/a")               ; chars not available
3327           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3328           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3329           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3330           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3331
3332
3333 (defun gnus-summary-set-local-parameters (group)
3334   "Go through the local params of GROUP and set all variable specs in that list."
3335   (let ((params (gnus-group-find-parameter group))
3336         (vars '(quit-config))           ; Ignore quit-config.
3337         elem)
3338     (while params
3339       (setq elem (car params)
3340             params (cdr params))
3341       (and (consp elem)                 ; Has to be a cons.
3342            (consp (cdr elem))           ; The cdr has to be a list.
3343            (symbolp (car elem))         ; Has to be a symbol in there.
3344            (not (memq (car elem) vars))
3345            (ignore-errors               ; So we set it.
3346              (push (car elem) vars)
3347              (make-local-variable (car elem))
3348              (set (car elem) (eval (nth 1 elem))))))))
3349
3350 (defun gnus-summary-read-group (group &optional show-all no-article
3351                                       kill-buffer no-display backward
3352                                       select-articles)
3353   "Start reading news in newsgroup GROUP.
3354 If SHOW-ALL is non-nil, already read articles are also listed.
3355 If NO-ARTICLE is non-nil, no article is selected initially.
3356 If NO-DISPLAY, don't generate a summary buffer."
3357   (let (result)
3358     (while (and group
3359                 (null (setq result
3360                             (let ((gnus-auto-select-next nil))
3361                               (or (gnus-summary-read-group-1
3362                                    group show-all no-article
3363                                    kill-buffer no-display
3364                                    select-articles)
3365                                   (setq show-all nil
3366                                         select-articles nil)))))
3367                 (eq gnus-auto-select-next 'quietly))
3368       (set-buffer gnus-group-buffer)
3369       ;; The entry function called above goes to the next
3370       ;; group automatically, so we go two groups back
3371       ;; if we are searching for the previous group.
3372       (when backward
3373         (gnus-group-prev-unread-group 2))
3374       (if (not (equal group (gnus-group-group-name)))
3375           (setq group (gnus-group-group-name))
3376         (setq group nil)))
3377     result))
3378
3379 (defun gnus-summary-read-group-1 (group show-all no-article
3380                                         kill-buffer no-display
3381                                         &optional select-articles)
3382   ;; Killed foreign groups can't be entered.
3383   ;;  (when (and (not (gnus-group-native-p group))
3384   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3385   ;;    (error "Dead non-native groups can't be entered"))
3386   (gnus-message 5 "Retrieving newsgroup: %s..."
3387                 (gnus-group-decoded-name group))
3388   (let* ((new-group (gnus-summary-setup-buffer group))
3389          (quit-config (gnus-group-quit-config group))
3390          (did-select (and new-group (gnus-select-newsgroup
3391                                      group show-all select-articles))))
3392     (cond
3393      ;; This summary buffer exists already, so we just select it.
3394      ((not new-group)
3395       (gnus-set-global-variables)
3396       (when kill-buffer
3397         (gnus-kill-or-deaden-summary kill-buffer))
3398       (gnus-configure-windows 'summary 'force)
3399       (gnus-set-mode-line 'summary)
3400       (gnus-summary-position-point)
3401       (message "")
3402       t)
3403      ;; We couldn't select this group.
3404      ((null did-select)
3405       (when (and (eq major-mode 'gnus-summary-mode)
3406                  (not (equal (current-buffer) kill-buffer)))
3407         (kill-buffer (current-buffer))
3408         (if (not quit-config)
3409             (progn
3410               ;; Update the info -- marks might need to be removed,
3411               ;; for instance.
3412               (gnus-summary-update-info)
3413               (set-buffer gnus-group-buffer)
3414               (gnus-group-jump-to-group group)
3415               (gnus-group-next-unread-group 1))
3416           (gnus-handle-ephemeral-exit quit-config)))
3417       (let ((grpinfo (gnus-get-info group)))
3418         (if (null (gnus-info-read grpinfo))
3419             (gnus-message 3 "Group %s contains no messages"
3420                           (gnus-group-decoded-name group))
3421           (gnus-message 3 "Can't select group")))
3422       nil)
3423      ;; The user did a `C-g' while prompting for number of articles,
3424      ;; so we exit this group.
3425      ((eq did-select 'quit)
3426       (and (eq major-mode 'gnus-summary-mode)
3427            (not (equal (current-buffer) kill-buffer))
3428            (kill-buffer (current-buffer)))
3429       (when kill-buffer
3430         (gnus-kill-or-deaden-summary kill-buffer))
3431       (if (not quit-config)
3432           (progn
3433             (set-buffer gnus-group-buffer)
3434             (gnus-group-jump-to-group group)
3435             (gnus-group-next-unread-group 1)
3436             (gnus-configure-windows 'group 'force))
3437         (gnus-handle-ephemeral-exit quit-config))
3438       ;; Finally signal the quit.
3439       (signal 'quit nil))
3440      ;; The group was successfully selected.
3441      (t
3442       (gnus-set-global-variables)
3443       ;; Save the active value in effect when the group was entered.
3444       (setq gnus-newsgroup-active
3445             (gnus-copy-sequence
3446              (gnus-active gnus-newsgroup-name)))
3447       ;; You can change the summary buffer in some way with this hook.
3448       (gnus-run-hooks 'gnus-select-group-hook)
3449       (gnus-update-format-specifications
3450        nil 'summary 'summary-mode 'summary-dummy)
3451       (gnus-update-summary-mark-positions)
3452       ;; Do score processing.
3453       (when gnus-use-scoring
3454         (gnus-possibly-score-headers))
3455       ;; Check whether to fill in the gaps in the threads.
3456       (when gnus-build-sparse-threads
3457         (gnus-build-sparse-threads))
3458       ;; Find the initial limit.
3459       (if gnus-show-threads
3460           (if show-all
3461               (let ((gnus-newsgroup-dormant nil))
3462                 (gnus-summary-initial-limit show-all))
3463             (gnus-summary-initial-limit show-all))
3464         ;; When unthreaded, all articles are always shown.
3465         (setq gnus-newsgroup-limit
3466               (mapcar
3467                (lambda (header) (mail-header-number header))
3468                gnus-newsgroup-headers)))
3469       ;; Generate the summary buffer.
3470       (unless no-display
3471         (gnus-summary-prepare))
3472       (when gnus-use-trees
3473         (gnus-tree-open group)
3474         (setq gnus-summary-highlight-line-function
3475               'gnus-tree-highlight-article))
3476       ;; If the summary buffer is empty, but there are some low-scored
3477       ;; articles or some excluded dormants, we include these in the
3478       ;; buffer.
3479       (when (and (zerop (buffer-size))
3480                  (not no-display))
3481         (cond (gnus-newsgroup-dormant
3482                (gnus-summary-limit-include-dormant))
3483               ((and gnus-newsgroup-scored show-all)
3484                (gnus-summary-limit-include-expunged t))))
3485       ;; Function `gnus-apply-kill-file' must be called in this hook.
3486       (gnus-run-hooks 'gnus-apply-kill-hook)
3487       (if (and (zerop (buffer-size))
3488                (not no-display))
3489           (progn
3490             ;; This newsgroup is empty.
3491             (gnus-summary-catchup-and-exit nil t)
3492             (gnus-message 6 "No unread news")
3493             (when kill-buffer
3494               (gnus-kill-or-deaden-summary kill-buffer))
3495             ;; Return nil from this function.
3496             nil)
3497         ;; Hide conversation thread subtrees.  We cannot do this in
3498         ;; gnus-summary-prepare-hook since kill processing may not
3499         ;; work with hidden articles.
3500         (gnus-summary-maybe-hide-threads)
3501         (when kill-buffer
3502           (gnus-kill-or-deaden-summary kill-buffer))
3503         (gnus-summary-auto-select-subject)
3504         ;; Show first unread article if requested.
3505         (if (and (not no-article)
3506                  (not no-display)
3507                  gnus-newsgroup-unreads
3508                  gnus-auto-select-first)
3509             (progn
3510               (gnus-configure-windows 'summary)
3511               (let ((art (gnus-summary-article-number)))
3512                 (unless (and (not gnus-plugged)
3513                              (or (memq art gnus-newsgroup-undownloaded)
3514                                  (memq art gnus-newsgroup-downloadable)))
3515                   (gnus-summary-goto-article art))))
3516           ;; Don't select any articles.
3517           (gnus-summary-position-point)
3518           (gnus-configure-windows 'summary 'force)
3519           (gnus-set-mode-line 'summary))
3520         (when (get-buffer-window gnus-group-buffer t)
3521           ;; Gotta use windows, because recenter does weird stuff if
3522           ;; the current buffer ain't the displayed window.
3523           (let ((owin (selected-window)))
3524             (select-window (get-buffer-window gnus-group-buffer t))
3525             (when (gnus-group-goto-group group)
3526               (recenter))
3527             (select-window owin)))
3528         ;; Mark this buffer as "prepared".
3529         (setq gnus-newsgroup-prepared t)
3530         (gnus-run-hooks 'gnus-summary-prepared-hook)
3531         (unless (gnus-ephemeral-group-p group)
3532           (gnus-group-update-group group))
3533         t)))))
3534
3535 (defun gnus-summary-auto-select-subject ()
3536   "Select the subject line on initial group entry."
3537   (goto-char (point-min))
3538   (cond
3539    ((eq gnus-auto-select-subject 'best)
3540     (gnus-summary-best-unread-subject))
3541    ((eq gnus-auto-select-subject 'unread)
3542     (gnus-summary-first-unread-subject))
3543    ((eq gnus-auto-select-subject 'unseen)
3544     (gnus-summary-first-unseen-subject))
3545    ((eq gnus-auto-select-subject 'unseen-or-unread)
3546     (gnus-summary-first-unseen-or-unread-subject))
3547    ((eq gnus-auto-select-subject 'first)
3548     ;; Do nothing.
3549     )
3550    ((gnus-functionp gnus-auto-select-subject)
3551     (funcall gnus-auto-select-subject))))
3552
3553 (defun gnus-summary-prepare ()
3554   "Generate the summary buffer."
3555   (interactive)
3556   (let ((buffer-read-only nil))
3557     (erase-buffer)
3558     (setq gnus-newsgroup-data nil
3559           gnus-newsgroup-data-reverse nil)
3560     (gnus-run-hooks 'gnus-summary-generate-hook)
3561     ;; Generate the buffer, either with threads or without.
3562     (when gnus-newsgroup-headers
3563       (gnus-summary-prepare-threads
3564        (if gnus-show-threads
3565            (gnus-sort-gathered-threads
3566             (funcall gnus-summary-thread-gathering-function
3567                      (gnus-sort-threads
3568                       (gnus-cut-threads (gnus-make-threads)))))
3569          ;; Unthreaded display.
3570          (gnus-sort-articles gnus-newsgroup-headers))))
3571     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3572     ;; Call hooks for modifying summary buffer.
3573     (goto-char (point-min))
3574     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3575
3576 (defsubst gnus-general-simplify-subject (subject)
3577   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3578   (setq subject
3579         (cond
3580          ;; Truncate the subject.
3581          (gnus-simplify-subject-functions
3582           (gnus-map-function gnus-simplify-subject-functions subject))
3583          ((numberp gnus-summary-gather-subject-limit)
3584           (setq subject (gnus-simplify-subject-re subject))
3585           (if (> (length subject) gnus-summary-gather-subject-limit)
3586               (substring subject 0 gnus-summary-gather-subject-limit)
3587             subject))
3588          ;; Fuzzily simplify it.
3589          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3590           (gnus-simplify-subject-fuzzy subject))
3591          ;; Just remove the leading "Re:".
3592          (t
3593           (gnus-simplify-subject-re subject))))
3594
3595   (if (and gnus-summary-gather-exclude-subject
3596            (string-match gnus-summary-gather-exclude-subject subject))
3597       nil                         ; This article shouldn't be gathered
3598     subject))
3599
3600 (defun gnus-summary-simplify-subject-query ()
3601   "Query where the respool algorithm would put this article."
3602   (interactive)
3603   (gnus-summary-select-article)
3604   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3605
3606 (defun gnus-gather-threads-by-subject (threads)
3607   "Gather threads by looking at Subject headers."
3608   (if (not gnus-summary-make-false-root)
3609       threads
3610     (let ((hashtb (gnus-make-hashtable 1024))
3611           (prev threads)
3612           (result threads)
3613           subject hthread whole-subject)
3614       (while threads
3615         (setq subject (gnus-general-simplify-subject
3616                        (setq whole-subject (mail-header-subject
3617                                             (caar threads)))))
3618         (when subject
3619           (if (setq hthread (gnus-gethash subject hashtb))
3620               (progn
3621                 ;; We enter a dummy root into the thread, if we
3622                 ;; haven't done that already.
3623                 (unless (stringp (caar hthread))
3624                   (setcar hthread (list whole-subject (car hthread))))
3625                 ;; We add this new gathered thread to this gathered
3626                 ;; thread.
3627                 (setcdr (car hthread)
3628                         (nconc (cdar hthread) (list (car threads))))
3629                 ;; Remove it from the list of threads.
3630                 (setcdr prev (cdr threads))
3631                 (setq threads prev))
3632             ;; Enter this thread into the hash table.
3633             (gnus-sethash subject
3634                           (if gnus-summary-make-false-root-always
3635                               (progn
3636                                 ;; If you want a dummy root above all
3637                                 ;; threads...
3638                                 (setcar threads (list whole-subject
3639                                                       (car threads)))
3640                                 threads)
3641                             threads)
3642                           hashtb)))
3643         (setq prev threads)
3644         (setq threads (cdr threads)))
3645       result)))
3646
3647 (defun gnus-gather-threads-by-references (threads)
3648   "Gather threads by looking at References headers."
3649   (let ((idhashtb (gnus-make-hashtable 1024))
3650         (thhashtb (gnus-make-hashtable 1024))
3651         (prev threads)
3652         (result threads)
3653         ids references id gthread gid entered ref)
3654     (while threads
3655       (when (setq references (mail-header-references (caar threads)))
3656         (setq id (mail-header-id (caar threads))
3657               ids (inline (gnus-split-references references))
3658               entered nil)
3659         (while (setq ref (pop ids))
3660           (setq ids (delete ref ids))
3661           (if (not (setq gid (gnus-gethash ref idhashtb)))
3662               (progn
3663                 (gnus-sethash ref id idhashtb)
3664                 (gnus-sethash id threads thhashtb))
3665             (setq gthread (gnus-gethash gid thhashtb))
3666             (unless entered
3667               ;; We enter a dummy root into the thread, if we
3668               ;; haven't done that already.
3669               (unless (stringp (caar gthread))
3670                 (setcar gthread (list (mail-header-subject (caar gthread))
3671                                       (car gthread))))
3672               ;; We add this new gathered thread to this gathered
3673               ;; thread.
3674               (setcdr (car gthread)
3675                       (nconc (cdar gthread) (list (car threads)))))
3676             ;; Add it into the thread hash table.
3677             (gnus-sethash id gthread thhashtb)
3678             (setq entered t)
3679             ;; Remove it from the list of threads.
3680             (setcdr prev (cdr threads))
3681             (setq threads prev))))
3682       (setq prev threads)
3683       (setq threads (cdr threads)))
3684     result))
3685
3686 (defun gnus-sort-gathered-threads (threads)
3687   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3688   (let ((result threads))
3689     (while threads
3690       (when (stringp (caar threads))
3691         (setcdr (car threads)
3692                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3693       (setq threads (cdr threads)))
3694     result))
3695
3696 (defun gnus-thread-loop-p (root thread)
3697   "Say whether ROOT is in THREAD."
3698   (let ((stack (list thread))
3699         (infloop 0)
3700         th)
3701     (while (setq thread (pop stack))
3702       (setq th (cdr thread))
3703       (while (and th
3704                   (not (eq (caar th) root)))
3705         (pop th))
3706       (if th
3707           ;; We have found a loop.
3708           (let (ref-dep)
3709             (setcdr thread (delq (car th) (cdr thread)))
3710             (if (boundp (setq ref-dep (intern "none"
3711                                               gnus-newsgroup-dependencies)))
3712                 (setcdr (symbol-value ref-dep)
3713                         (nconc (cdr (symbol-value ref-dep))
3714                                (list (car th))))
3715               (set ref-dep (list nil (car th))))
3716             (setq infloop 1
3717                   stack nil))
3718         ;; Push all the subthreads onto the stack.
3719         (push (cdr thread) stack)))
3720     infloop))
3721
3722 (defun gnus-make-threads ()
3723   "Go through the dependency hashtb and find the roots.  Return all threads."
3724   (let (threads)
3725     (while (catch 'infloop
3726              (mapatoms
3727               (lambda (refs)
3728                 ;; Deal with self-referencing References loops.
3729                 (when (and (car (symbol-value refs))
3730                            (not (zerop
3731                                  (apply
3732                                   '+
3733                                   (mapcar
3734                                    (lambda (thread)
3735                                      (gnus-thread-loop-p
3736                                       (car (symbol-value refs)) thread))
3737                                    (cdr (symbol-value refs)))))))
3738                   (setq threads nil)
3739                   (throw 'infloop t))
3740                 (unless (car (symbol-value refs))
3741                   ;; These threads do not refer back to any other
3742                   ;; articles, so they're roots.
3743                   (setq threads (append (cdr (symbol-value refs)) threads))))
3744               gnus-newsgroup-dependencies)))
3745     threads))
3746
3747 ;; Build the thread tree.
3748 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3749   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3750
3751 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3752 if it was already present.
3753
3754 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3755 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3756 Message-IDs will be renamed to a unique Message-ID before being
3757 entered.
3758
3759 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3760   (let* ((id (mail-header-id header))
3761          (id-dep (and id (intern id dependencies)))
3762          parent-id ref ref-dep ref-header replaced)
3763     ;; Enter this `header' in the `dependencies' table.
3764     (cond
3765      ((not id-dep)
3766       (setq header nil))
3767      ;; The first two cases do the normal part: enter a new `header'
3768      ;; in the `dependencies' table.
3769      ((not (boundp id-dep))
3770       (set id-dep (list header)))
3771      ((null (car (symbol-value id-dep)))
3772       (setcar (symbol-value id-dep) header))
3773
3774      ;; From here the `header' was already present in the
3775      ;; `dependencies' table.
3776      (force-new
3777       ;; Overrides an existing entry;
3778       ;; just set the header part of the entry.
3779       (setcar (symbol-value id-dep) header)
3780       (setq replaced t))
3781
3782      ;; Renames the existing `header' to a unique Message-ID.
3783      ((not gnus-summary-ignore-duplicates)
3784       ;; An article with this Message-ID has already been seen.
3785       ;; We rename the Message-ID.
3786       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3787            (list header))
3788       (mail-header-set-id header id))
3789
3790      ;; The last case ignores an existing entry, except it adds any
3791      ;; additional Xrefs (in case the two articles came from different
3792      ;; servers.
3793      ;; Also sets `header' to `nil' meaning that the `dependencies'
3794      ;; table was *not* modified.
3795      (t
3796       (mail-header-set-xref
3797        (car (symbol-value id-dep))
3798        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3799                    "")
3800                (or (mail-header-xref header) "")))
3801       (setq header nil)))
3802
3803     (when (and header (not replaced))
3804       ;; First check that we are not creating a References loop.
3805       (setq parent-id (gnus-parent-id (mail-header-references header)))
3806       (setq ref parent-id)
3807       (while (and ref
3808                   (setq ref-dep (intern-soft ref dependencies))
3809                   (boundp ref-dep)
3810                   (setq ref-header (car (symbol-value ref-dep))))
3811         (if (string= id ref)
3812             ;; Yuk!  This is a reference loop.  Make the article be a
3813             ;; root article.
3814             (progn
3815               (mail-header-set-references (car (symbol-value id-dep)) "none")
3816               (setq ref nil)
3817               (setq parent-id nil))
3818           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3819       (setq ref-dep (intern (or parent-id "none") dependencies))
3820       (if (boundp ref-dep)
3821           (setcdr (symbol-value ref-dep)
3822                   (nconc (cdr (symbol-value ref-dep))
3823                          (list (symbol-value id-dep))))
3824         (set ref-dep (list nil (symbol-value id-dep)))))
3825     header))
3826
3827 (defun gnus-extract-message-id-from-in-reply-to (string)
3828   (if (string-match "<[^>]+>" string)
3829       (substring string (match-beginning 0) (match-end 0))
3830     nil))
3831
3832 (defun gnus-build-sparse-threads ()
3833   (let ((headers gnus-newsgroup-headers)
3834         (mail-parse-charset gnus-newsgroup-charset)
3835         (gnus-summary-ignore-duplicates t)
3836         header references generation relations
3837         subject child end new-child date)
3838     ;; First we create an alist of generations/relations, where
3839     ;; generations is how much we trust the relation, and the relation
3840     ;; is parent/child.
3841     (gnus-message 7 "Making sparse threads...")
3842     (save-excursion
3843       (nnheader-set-temp-buffer " *gnus sparse threads*")
3844       (while (setq header (pop headers))
3845         (when (and (setq references (mail-header-references header))
3846                    (not (string= references "")))
3847           (insert references)
3848           (setq child (mail-header-id header)
3849                 subject (mail-header-subject header)
3850                 date (mail-header-date header)
3851                 generation 0)
3852           (while (search-backward ">" nil t)
3853             (setq end (1+ (point)))
3854             (when (search-backward "<" nil t)
3855               (setq new-child (buffer-substring (point) end))
3856               (push (list (incf generation)
3857                           child (setq child new-child)
3858                           subject date)
3859                     relations)))
3860           (when child
3861             (push (list (1+ generation) child nil subject) relations))
3862           (erase-buffer)))
3863       (kill-buffer (current-buffer)))
3864     ;; Sort over trustworthiness.
3865     (mapcar
3866      (lambda (relation)
3867        (when (gnus-dependencies-add-header
3868               (make-full-mail-header
3869                gnus-reffed-article-number
3870                (nth 3 relation) "" (or (nth 4 relation) "")
3871                (nth 1 relation)
3872                (or (nth 2 relation) "") 0 0 "")
3873               gnus-newsgroup-dependencies nil)
3874          (push gnus-reffed-article-number gnus-newsgroup-limit)
3875          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3876          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3877                gnus-newsgroup-reads)
3878          (decf gnus-reffed-article-number)))
3879      (sort relations 'car-less-than-car))
3880     (gnus-message 7 "Making sparse threads...done")))
3881
3882 (defun gnus-build-old-threads ()
3883   ;; Look at all the articles that refer back to old articles, and
3884   ;; fetch the headers for the articles that aren't there.  This will
3885   ;; build complete threads - if the roots haven't been expired by the
3886   ;; server, that is.
3887   (let ((mail-parse-charset gnus-newsgroup-charset)
3888         id heads)
3889     (mapatoms
3890      (lambda (refs)
3891        (when (not (car (symbol-value refs)))
3892          (setq heads (cdr (symbol-value refs)))
3893          (while heads
3894            (if (memq (mail-header-number (caar heads))
3895                      gnus-newsgroup-dormant)
3896                (setq heads (cdr heads))
3897              (setq id (symbol-name refs))
3898              (while (and (setq id (gnus-build-get-header id))
3899                          (not (car (gnus-id-to-thread id)))))
3900              (setq heads nil)))))
3901      gnus-newsgroup-dependencies)))
3902
3903 ;; This function has to be called with point after the article number
3904 ;; on the beginning of the line.
3905 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3906   (let ((eol (gnus-point-at-eol))
3907         (buffer (current-buffer))
3908         header references in-reply-to)
3909
3910     ;; overview: [num subject from date id refs chars lines misc]
3911     (unwind-protect
3912         (let (x)
3913           (narrow-to-region (point) eol)
3914           (unless (eobp)
3915             (forward-char))
3916
3917           (setq header
3918                 (make-full-mail-header
3919                  number                 ; number
3920                  (condition-case ()     ; subject
3921                      (funcall gnus-decode-encoded-word-function
3922                               (setq x (nnheader-nov-field)))
3923                    (error x))
3924                  (condition-case ()     ; from
3925                      (funcall gnus-decode-encoded-word-function
3926                               (setq x (nnheader-nov-field)))
3927                    (error x))
3928                  (nnheader-nov-field)   ; date
3929                  (nnheader-nov-read-message-id) ; id
3930                  (setq references (nnheader-nov-field)) ; refs
3931                  (nnheader-nov-read-integer) ; chars
3932                  (nnheader-nov-read-integer) ; lines
3933                  (unless (eobp)
3934                    (if (looking-at "Xref: ")
3935                        (goto-char (match-end 0)))
3936                    (nnheader-nov-field)) ; Xref
3937                  (nnheader-nov-parse-extra)))) ; extra
3938
3939       (widen))
3940
3941     (when (and (string= references "")
3942                (setq in-reply-to (mail-header-extra header))
3943                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3944       (mail-header-set-references
3945        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3946
3947     (when gnus-alter-header-function
3948       (funcall gnus-alter-header-function header))
3949     (gnus-dependencies-add-header header dependencies force-new)))
3950
3951 (defun gnus-build-get-header (id)
3952   "Look through the buffer of NOV lines and find the header to ID.
3953 Enter this line into the dependencies hash table, and return
3954 the id of the parent article (if any)."
3955   (let ((deps gnus-newsgroup-dependencies)
3956         found header)
3957     (prog1
3958         (save-excursion
3959           (set-buffer nntp-server-buffer)
3960           (let ((case-fold-search nil))
3961             (goto-char (point-min))
3962             (while (and (not found)
3963                         (search-forward id nil t))
3964               (beginning-of-line)
3965               (setq found (looking-at
3966                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3967                                    (regexp-quote id))))
3968               (or found (beginning-of-line 2)))
3969             (when found
3970               (beginning-of-line)
3971               (and
3972                (setq header (gnus-nov-parse-line
3973                              (read (current-buffer)) deps))
3974                (gnus-parent-id (mail-header-references header))))))
3975       (when header
3976         (let ((number (mail-header-number header)))
3977           (push number gnus-newsgroup-limit)
3978           (push header gnus-newsgroup-headers)
3979           (if (memq number gnus-newsgroup-unselected)
3980               (progn
3981                 (setq gnus-newsgroup-unreads
3982                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
3983                                                number))
3984                 (setq gnus-newsgroup-unselected
3985                       (delq number gnus-newsgroup-unselected)))
3986             (push number gnus-newsgroup-ancient)))))))
3987
3988 (defun gnus-build-all-threads ()
3989   "Read all the headers."
3990   (let ((gnus-summary-ignore-duplicates t)
3991         (mail-parse-charset gnus-newsgroup-charset)
3992         (dependencies gnus-newsgroup-dependencies)
3993         header article)
3994     (save-excursion
3995       (set-buffer nntp-server-buffer)
3996       (let ((case-fold-search nil))
3997         (goto-char (point-min))
3998         (while (not (eobp))
3999           (ignore-errors
4000             (setq article (read (current-buffer))
4001                   header (gnus-nov-parse-line article dependencies)))
4002           (when header
4003             (save-excursion
4004               (set-buffer gnus-summary-buffer)
4005               (push header gnus-newsgroup-headers)
4006               (if (memq (setq article (mail-header-number header))
4007                         gnus-newsgroup-unselected)
4008                   (progn
4009                     (setq gnus-newsgroup-unreads
4010                           (gnus-add-to-sorted-list
4011                            gnus-newsgroup-unreads article))
4012                     (setq gnus-newsgroup-unselected
4013                           (delq article gnus-newsgroup-unselected)))
4014                 (push article gnus-newsgroup-ancient)))
4015             (forward-line 1)))))))
4016
4017 (defun gnus-summary-update-article-line (article header)
4018   "Update the line for ARTICLE using HEADERS."
4019   (let* ((id (mail-header-id header))
4020          (thread (gnus-id-to-thread id)))
4021     (unless thread
4022       (error "Article in no thread"))
4023     ;; Update the thread.
4024     (setcar thread header)
4025     (gnus-summary-goto-subject article)
4026     (let* ((datal (gnus-data-find-list article))
4027            (data (car datal))
4028            (length (when (cdr datal)
4029                      (- (gnus-data-pos data)
4030                         (gnus-data-pos (cadr datal)))))
4031            (buffer-read-only nil)
4032            (level (gnus-summary-thread-level)))
4033       (gnus-delete-line)
4034       (gnus-summary-insert-line
4035        header level nil 
4036        (memq article gnus-newsgroup-undownloaded)
4037        (gnus-article-mark article)
4038        (memq article gnus-newsgroup-replied)
4039        (memq article gnus-newsgroup-expirable)
4040        ;; Only insert the Subject string when it's different
4041        ;; from the previous Subject string.
4042        (if (and
4043             gnus-show-threads
4044             (gnus-subject-equal
4045              (condition-case ()
4046                  (mail-header-subject
4047                   (gnus-data-header
4048                    (cadr
4049                     (gnus-data-find-list
4050                      article
4051                      (gnus-data-list t)))))
4052                ;; Error on the side of excessive subjects.
4053                (error ""))
4054              (mail-header-subject header)))
4055            ""
4056          (mail-header-subject header))
4057        nil (cdr (assq article gnus-newsgroup-scored))
4058        (memq article gnus-newsgroup-processable))
4059       (when length
4060         (gnus-data-update-list
4061          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
4062
4063 (defun gnus-summary-update-article (article &optional iheader)
4064   "Update ARTICLE in the summary buffer."
4065   (set-buffer gnus-summary-buffer)
4066   (let* ((header (gnus-summary-article-header article))
4067          (id (mail-header-id header))
4068          (data (gnus-data-find article))
4069          (thread (gnus-id-to-thread id))
4070          (references (mail-header-references header))
4071          (parent
4072           (gnus-id-to-thread
4073            (or (gnus-parent-id
4074                 (when (and references
4075                            (not (equal "" references)))
4076                   references))
4077                "none")))
4078          (buffer-read-only nil)
4079          (old (car thread)))
4080     (when thread
4081       (unless iheader
4082         (setcar thread nil)
4083         (when parent
4084           (delq thread parent)))
4085       (if (gnus-summary-insert-subject id header)
4086           ;; Set the (possibly) new article number in the data structure.
4087           (gnus-data-set-number data (gnus-id-to-article id))
4088         (setcar thread old)
4089         nil))))
4090
4091 (defun gnus-rebuild-thread (id &optional line)
4092   "Rebuild the thread containing ID.
4093 If LINE, insert the rebuilt thread starting on line LINE."
4094   (let ((buffer-read-only nil)
4095         old-pos current thread data)
4096     (if (not gnus-show-threads)
4097         (setq thread (list (car (gnus-id-to-thread id))))
4098       ;; Get the thread this article is part of.
4099       (setq thread (gnus-remove-thread id)))
4100     (setq old-pos (gnus-point-at-bol))
4101     (setq current (save-excursion
4102                     (and (re-search-backward "[\r\n]" nil t)
4103                          (gnus-summary-article-number))))
4104     ;; If this is a gathered thread, we have to go some re-gathering.
4105     (when (stringp (car thread))
4106       (let ((subject (car thread))
4107             roots thr)
4108         (setq thread (cdr thread))
4109         (while thread
4110           (unless (memq (setq thr (gnus-id-to-thread
4111                                    (gnus-root-id
4112                                     (mail-header-id (caar thread)))))
4113                         roots)
4114             (push thr roots))
4115           (setq thread (cdr thread)))
4116         ;; We now have all (unique) roots.
4117         (if (= (length roots) 1)
4118             ;; All the loose roots are now one solid root.
4119             (setq thread (car roots))
4120           (setq thread (cons subject (gnus-sort-threads roots))))))
4121     (let (threads)
4122       ;; We then insert this thread into the summary buffer.
4123       (when line
4124         (goto-char (point-min))
4125         (forward-line (1- line)))
4126       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4127         (if gnus-show-threads
4128             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4129           (gnus-summary-prepare-unthreaded thread))
4130         (setq data (nreverse gnus-newsgroup-data))
4131         (setq threads gnus-newsgroup-threads))
4132       ;; We splice the new data into the data structure.
4133       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4134       ;;!!! then we want to insert at the beginning of the buffer.
4135       ;;!!! That happens to be true with Gnus now, but that may
4136       ;;!!! change in the future.  Perhaps.
4137       (gnus-data-enter-list
4138        (if line nil current) data (- (point) old-pos))
4139       (setq gnus-newsgroup-threads
4140             (nconc threads gnus-newsgroup-threads))
4141       (gnus-data-compute-positions))))
4142
4143 (defun gnus-number-to-header (number)
4144   "Return the header for article NUMBER."
4145   (let ((headers gnus-newsgroup-headers))
4146     (while (and headers
4147                 (not (= number (mail-header-number (car headers)))))
4148       (pop headers))
4149     (when headers
4150       (car headers))))
4151
4152 (defun gnus-parent-headers (in-headers &optional generation)
4153   "Return the headers of the GENERATIONeth parent of HEADERS."
4154   (unless generation
4155     (setq generation 1))
4156   (let ((parent t)
4157         (headers in-headers)
4158         references)
4159     (while (and parent
4160                 (not (zerop generation))
4161                 (setq references (mail-header-references headers)))
4162       (setq headers (if (and references
4163                              (setq parent (gnus-parent-id references)))
4164                         (car (gnus-id-to-thread parent))
4165                       nil))
4166       (decf generation))
4167     (and (not (eq headers in-headers))
4168          headers)))
4169
4170 (defun gnus-id-to-thread (id)
4171   "Return the (sub-)thread where ID appears."
4172   (gnus-gethash id gnus-newsgroup-dependencies))
4173
4174 (defun gnus-id-to-article (id)
4175   "Return the article number of ID."
4176   (let ((thread (gnus-id-to-thread id)))
4177     (when (and thread
4178                (car thread))
4179       (mail-header-number (car thread)))))
4180
4181 (defun gnus-id-to-header (id)
4182   "Return the article headers of ID."
4183   (car (gnus-id-to-thread id)))
4184
4185 (defun gnus-article-displayed-root-p (article)
4186   "Say whether ARTICLE is a root(ish) article."
4187   (let ((level (gnus-summary-thread-level article))
4188         (refs (mail-header-references  (gnus-summary-article-header article)))
4189         particle)
4190     (cond
4191      ((null level) nil)
4192      ((zerop level) t)
4193      ((null refs) t)
4194      ((null (gnus-parent-id refs)) t)
4195      ((and (= 1 level)
4196            (null (setq particle (gnus-id-to-article
4197                                  (gnus-parent-id refs))))
4198            (null (gnus-summary-thread-level particle)))))))
4199
4200 (defun gnus-root-id (id)
4201   "Return the id of the root of the thread where ID appears."
4202   (let (last-id prev)
4203     (while (and id (setq prev (car (gnus-id-to-thread id))))
4204       (setq last-id id
4205             id (gnus-parent-id (mail-header-references prev))))
4206     last-id))
4207
4208 (defun gnus-articles-in-thread (thread)
4209   "Return the list of articles in THREAD."
4210   (cons (mail-header-number (car thread))
4211         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4212
4213 (defun gnus-remove-thread (id &optional dont-remove)
4214   "Remove the thread that has ID in it."
4215   (let (headers thread last-id)
4216     ;; First go up in this thread until we find the root.
4217     (setq last-id (gnus-root-id id)
4218           headers (message-flatten-list (gnus-id-to-thread last-id)))
4219     ;; We have now found the real root of this thread.  It might have
4220     ;; been gathered into some loose thread, so we have to search
4221     ;; through the threads to find the thread we wanted.
4222     (let ((threads gnus-newsgroup-threads)
4223           sub)
4224       (while threads
4225         (setq sub (car threads))
4226         (if (stringp (car sub))
4227             ;; This is a gathered thread, so we look at the roots
4228             ;; below it to find whether this article is in this
4229             ;; gathered root.
4230             (progn
4231               (setq sub (cdr sub))
4232               (while sub
4233                 (when (member (caar sub) headers)
4234                   (setq thread (car threads)
4235                         threads nil
4236                         sub nil))
4237                 (setq sub (cdr sub))))
4238           ;; It's an ordinary thread, so we check it.
4239           (when (eq (car sub) (car headers))
4240             (setq thread sub
4241                   threads nil)))
4242         (setq threads (cdr threads)))
4243       ;; If this article is in no thread, then it's a root.
4244       (if thread
4245           (unless dont-remove
4246             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4247         (setq thread (gnus-id-to-thread last-id)))
4248       (when thread
4249         (prog1
4250             thread                      ; We return this thread.
4251           (unless dont-remove
4252             (if (stringp (car thread))
4253                 (progn
4254                   ;; If we use dummy roots, then we have to remove the
4255                   ;; dummy root as well.
4256                   (when (eq gnus-summary-make-false-root 'dummy)
4257                     ;; We go to the dummy root by going to
4258                     ;; the first sub-"thread", and then one line up.
4259                     (gnus-summary-goto-article
4260                      (mail-header-number (caadr thread)))
4261                     (forward-line -1)
4262                     (gnus-delete-line)
4263                     (gnus-data-compute-positions))
4264                   (setq thread (cdr thread))
4265                   (while thread
4266                     (gnus-remove-thread-1 (car thread))
4267                     (setq thread (cdr thread))))
4268               (gnus-remove-thread-1 thread))))))))
4269
4270 (defun gnus-remove-thread-1 (thread)
4271   "Remove the thread THREAD recursively."
4272   (let ((number (mail-header-number (pop thread)))
4273         d)
4274     (setq thread (reverse thread))
4275     (while thread
4276       (gnus-remove-thread-1 (pop thread)))
4277     (when (setq d (gnus-data-find number))
4278       (goto-char (gnus-data-pos d))
4279       (gnus-summary-show-thread)
4280       (gnus-data-remove
4281        number
4282        (- (gnus-point-at-bol)
4283           (prog1
4284               (1+ (gnus-point-at-eol))
4285             (gnus-delete-line)))))))
4286
4287 (defun gnus-sort-threads-1 (threads func)
4288   (sort (mapcar (lambda (thread)
4289                   (cons (car thread)
4290                         (and (cdr thread)
4291                              (gnus-sort-threads-1 (cdr thread) func))))
4292                 threads) func))
4293
4294 (defun gnus-sort-threads (threads)
4295   "Sort THREADS."
4296   (if (not gnus-thread-sort-functions)
4297       threads
4298     (gnus-message 8 "Sorting threads...")
4299     (let ((max-lisp-eval-depth 5000))
4300       (prog1 (gnus-sort-threads-1
4301          threads
4302          (gnus-make-sort-function gnus-thread-sort-functions))
4303         (gnus-message 8 "Sorting threads...done")))))
4304
4305 (defun gnus-sort-articles (articles)
4306   "Sort ARTICLES."
4307   (when gnus-article-sort-functions
4308     (gnus-message 7 "Sorting articles...")
4309     (prog1
4310         (setq gnus-newsgroup-headers
4311               (sort articles (gnus-make-sort-function
4312                               gnus-article-sort-functions)))
4313       (gnus-message 7 "Sorting articles...done"))))
4314
4315 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4316 (defmacro gnus-thread-header (thread)
4317   "Return header of first article in THREAD.
4318 Note that THREAD must never, ever be anything else than a variable -
4319 using some other form will lead to serious barfage."
4320   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4321   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4322   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4323         (vector thread) 2))
4324
4325 (defsubst gnus-article-sort-by-number (h1 h2)
4326   "Sort articles by article number."
4327   (< (mail-header-number h1)
4328      (mail-header-number h2)))
4329
4330 (defun gnus-thread-sort-by-number (h1 h2)
4331   "Sort threads by root article number."
4332   (gnus-article-sort-by-number
4333    (gnus-thread-header h1) (gnus-thread-header h2)))
4334
4335 (defsubst gnus-article-sort-by-random (h1 h2)
4336   "Sort articles by article number."
4337   (zerop (random 2)))
4338
4339 (defun gnus-thread-sort-by-random (h1 h2)
4340   "Sort threads by root article number."
4341   (gnus-article-sort-by-random
4342    (gnus-thread-header h1) (gnus-thread-header h2)))
4343
4344 (defsubst gnus-article-sort-by-lines (h1 h2)
4345   "Sort articles by article Lines header."
4346   (< (mail-header-lines h1)
4347      (mail-header-lines h2)))
4348
4349 (defun gnus-thread-sort-by-lines (h1 h2)
4350   "Sort threads by root article Lines header."
4351   (gnus-article-sort-by-lines
4352    (gnus-thread-header h1) (gnus-thread-header h2)))
4353
4354 (defsubst gnus-article-sort-by-chars (h1 h2)
4355   "Sort articles by octet length."
4356   (< (mail-header-chars h1)
4357      (mail-header-chars h2)))
4358
4359 (defun gnus-thread-sort-by-chars (h1 h2)
4360   "Sort threads by root article octet length."
4361   (gnus-article-sort-by-chars
4362    (gnus-thread-header h1) (gnus-thread-header h2)))
4363
4364 (defsubst gnus-article-sort-by-author (h1 h2)
4365   "Sort articles by root author."
4366   (string-lessp
4367    (let ((extract (funcall
4368                    gnus-extract-address-components
4369                    (mail-header-from h1))))
4370      (or (car extract) (cadr extract) ""))
4371    (let ((extract (funcall
4372                    gnus-extract-address-components
4373                    (mail-header-from h2))))
4374      (or (car extract) (cadr extract) ""))))
4375
4376 (defun gnus-thread-sort-by-author (h1 h2)
4377   "Sort threads by root author."
4378   (gnus-article-sort-by-author
4379    (gnus-thread-header h1)  (gnus-thread-header h2)))
4380
4381 (defsubst gnus-article-sort-by-subject (h1 h2)
4382   "Sort articles by root subject."
4383   (string-lessp
4384    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4385    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4386
4387 (defun gnus-thread-sort-by-subject (h1 h2)
4388   "Sort threads by root subject."
4389   (gnus-article-sort-by-subject
4390    (gnus-thread-header h1) (gnus-thread-header h2)))
4391
4392 (defsubst gnus-article-sort-by-date (h1 h2)
4393   "Sort articles by root article date."
4394   (time-less-p
4395    (gnus-date-get-time (mail-header-date h1))
4396    (gnus-date-get-time (mail-header-date h2))))
4397
4398 (defun gnus-thread-sort-by-date (h1 h2)
4399   "Sort threads by root article date."
4400   (gnus-article-sort-by-date
4401    (gnus-thread-header h1) (gnus-thread-header h2)))
4402
4403 (defsubst gnus-article-sort-by-score (h1 h2)
4404   "Sort articles by root article score.
4405 Unscored articles will be counted as having a score of zero."
4406   (> (or (cdr (assq (mail-header-number h1)
4407                     gnus-newsgroup-scored))
4408          gnus-summary-default-score 0)
4409      (or (cdr (assq (mail-header-number h2)
4410                     gnus-newsgroup-scored))
4411          gnus-summary-default-score 0)))
4412
4413 (defun gnus-thread-sort-by-score (h1 h2)
4414   "Sort threads by root article score."
4415   (gnus-article-sort-by-score
4416    (gnus-thread-header h1) (gnus-thread-header h2)))
4417
4418 (defun gnus-thread-sort-by-total-score (h1 h2)
4419   "Sort threads by the sum of all scores in the thread.
4420 Unscored articles will be counted as having a score of zero."
4421   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4422
4423 (defun gnus-thread-total-score (thread)
4424   ;; This function find the total score of THREAD.
4425   (cond
4426    ((null thread)
4427     0)
4428    ((consp thread)
4429     (if (stringp (car thread))
4430         (apply gnus-thread-score-function 0
4431                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4432       (gnus-thread-total-score-1 thread)))
4433    (t
4434     (gnus-thread-total-score-1 (list thread)))))
4435
4436 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4437   "Sort threads such that the thread with the most recently arrived article comes first."
4438   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4439
4440 (defun gnus-thread-highest-number (thread)
4441   "Return the highest article number in THREAD."
4442   (apply 'max (mapcar (lambda (header)
4443                         (mail-header-number header))
4444                       (message-flatten-list thread))))
4445
4446 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4447   "Sort threads such that the thread with the most recently dated article comes first."
4448   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4449
4450 (defun gnus-thread-latest-date (thread)
4451   "Return the highest article date in THREAD."
4452   (let ((previous-time 0))
4453     (apply 'max
4454            (mapcar
4455             (lambda (header)
4456               (setq previous-time
4457                     (time-to-seconds
4458                      (condition-case ()
4459                          (mail-header-parse-date (mail-header-date header))
4460                        (error previous-time)))))
4461             (sort
4462              (message-flatten-list thread)
4463              (lambda (h1 h2)
4464                (< (mail-header-number h1)
4465                   (mail-header-number h2))))))))
4466
4467 (defun gnus-thread-total-score-1 (root)
4468   ;; This function find the total score of the thread below ROOT.
4469   (setq root (car root))
4470   (apply gnus-thread-score-function
4471          (or (append
4472               (mapcar 'gnus-thread-total-score
4473                       (cdr (gnus-id-to-thread (mail-header-id root))))
4474               (when (> (mail-header-number root) 0)
4475                 (list (or (cdr (assq (mail-header-number root)
4476                                      gnus-newsgroup-scored))
4477                           gnus-summary-default-score 0))))
4478              (list gnus-summary-default-score)
4479              '(0))))
4480
4481 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4482 (defvar gnus-tmp-prev-subject nil)
4483 (defvar gnus-tmp-false-parent nil)
4484 (defvar gnus-tmp-root-expunged nil)
4485 (defvar gnus-tmp-dummy-line nil)
4486
4487 (eval-when-compile (defvar gnus-tmp-header))
4488 (defun gnus-extra-header (type &optional header)
4489   "Return the extra header of TYPE."
4490   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4491       ""))
4492
4493 (defvar gnus-tmp-thread-tree-header-string "")
4494
4495 (defcustom gnus-sum-thread-tree-root "> "
4496   "With %B spec, used for the root of a thread.
4497 If nil, use subject instead."
4498   :type 'string
4499   :group 'gnus-thread)
4500 (defcustom gnus-sum-thread-tree-single-indent ""
4501   "With %B spec, used for a thread with just one message.
4502 If nil, use subject instead."
4503   :type 'string
4504   :group 'gnus-thread)
4505 (defcustom gnus-sum-thread-tree-vertical "| "
4506   "With %B spec, used for drawing a vertical line."
4507   :type 'string
4508   :group 'gnus-thread)
4509 (defcustom gnus-sum-thread-tree-indent "  "
4510   "With %B spec, used for indenting."
4511   :type 'string
4512   :group 'gnus-thread)
4513 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4514   "With %B spec, used for a leaf with brothers."
4515   :type 'string
4516   :group 'gnus-thread)
4517 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4518   "With %B spec, used for a leaf without brothers."
4519   :type 'string
4520   :group 'gnus-thread)
4521
4522 (defun gnus-summary-prepare-threads (threads)
4523   "Prepare summary buffer from THREADS and indentation LEVEL.
4524 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4525 or a straight list of headers."
4526   (gnus-message 7 "Generating summary...")
4527
4528   (setq gnus-newsgroup-threads threads)
4529   (beginning-of-line)
4530
4531   (let ((gnus-tmp-level 0)
4532         (default-score (or gnus-summary-default-score 0))
4533         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4534         (building-line-count gnus-summary-display-while-building)
4535         (building-count (integerp gnus-summary-display-while-building))
4536         thread number subject stack state gnus-tmp-gathered beg-match
4537         new-roots gnus-tmp-new-adopts thread-end simp-subject
4538         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4539         gnus-tmp-replied gnus-tmp-subject-or-nil
4540         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4541         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4542         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4543         tree-stack)
4544
4545     (setq gnus-tmp-prev-subject nil)
4546
4547     (if (vectorp (car threads))
4548         ;; If this is a straight (sic) list of headers, then a
4549         ;; threaded summary display isn't required, so we just create
4550         ;; an unthreaded one.
4551         (gnus-summary-prepare-unthreaded threads)
4552
4553       ;; Do the threaded display.
4554
4555       (if gnus-summary-display-while-building
4556           (switch-to-buffer (buffer-name)))
4557       (while (or threads stack gnus-tmp-new-adopts new-roots)
4558
4559         (if (and (= gnus-tmp-level 0)
4560                  (or (not stack)
4561                      (= (caar stack) 0))
4562                  (not gnus-tmp-false-parent)
4563                  (or gnus-tmp-new-adopts new-roots))
4564             (if gnus-tmp-new-adopts
4565                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4566                       thread (list (car gnus-tmp-new-adopts))
4567                       gnus-tmp-header (caar thread)
4568                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4569               (when new-roots
4570                 (setq thread (list (car new-roots))
4571                       gnus-tmp-header (caar thread)
4572                       new-roots (cdr new-roots))))
4573
4574           (if threads
4575               ;; If there are some threads, we do them before the
4576               ;; threads on the stack.
4577               (setq thread threads
4578                     gnus-tmp-header (caar thread))
4579             ;; There were no current threads, so we pop something off
4580             ;; the stack.
4581             (setq state (car stack)
4582                   gnus-tmp-level (car state)
4583                   tree-stack (cadr state)
4584                   thread (caddr state)
4585                   stack (cdr stack)
4586                   gnus-tmp-header (caar thread))))
4587
4588         (setq gnus-tmp-false-parent nil)
4589         (setq gnus-tmp-root-expunged nil)
4590         (setq thread-end nil)
4591
4592         (if (stringp gnus-tmp-header)
4593             ;; The header is a dummy root.
4594             (cond
4595              ((eq gnus-summary-make-false-root 'adopt)
4596               ;; We let the first article adopt the rest.
4597               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4598                                                (cddar thread)))
4599               (setq gnus-tmp-gathered
4600                     (nconc (mapcar
4601                             (lambda (h) (mail-header-number (car h)))
4602                             (cddar thread))
4603                            gnus-tmp-gathered))
4604               (setq thread (cons (list (caar thread)
4605                                        (cadar thread))
4606                                  (cdr thread)))
4607               (setq gnus-tmp-level -1
4608                     gnus-tmp-false-parent t))
4609              ((eq gnus-summary-make-false-root 'empty)
4610               ;; We print adopted articles with empty subject fields.
4611               (setq gnus-tmp-gathered
4612                     (nconc (mapcar
4613                             (lambda (h) (mail-header-number (car h)))
4614                             (cddar thread))
4615                            gnus-tmp-gathered))
4616               (setq gnus-tmp-level -1))
4617              ((eq gnus-summary-make-false-root 'dummy)
4618               ;; We remember that we probably want to output a dummy
4619               ;; root.
4620               (setq gnus-tmp-dummy-line gnus-tmp-header)
4621               (setq gnus-tmp-prev-subject gnus-tmp-header))
4622              (t
4623               ;; We do not make a root for the gathered
4624               ;; sub-threads at all.
4625               (setq gnus-tmp-level -1)))
4626
4627           (setq number (mail-header-number gnus-tmp-header)
4628                 subject (mail-header-subject gnus-tmp-header)
4629                 simp-subject (gnus-simplify-subject-fully subject))
4630
4631           (cond
4632            ;; If the thread has changed subject, we might want to make
4633            ;; this subthread into a root.
4634            ((and (null gnus-thread-ignore-subject)
4635                  (not (zerop gnus-tmp-level))
4636                  gnus-tmp-prev-subject
4637                  (not (string= gnus-tmp-prev-subject simp-subject)))
4638             (setq new-roots (nconc new-roots (list (car thread)))
4639                   thread-end t
4640                   gnus-tmp-header nil))
4641            ;; If the article lies outside the current limit,
4642            ;; then we do not display it.
4643            ((not (memq number gnus-newsgroup-limit))
4644             (setq gnus-tmp-gathered
4645                   (nconc (mapcar
4646                           (lambda (h) (mail-header-number (car h)))
4647                           (cdar thread))
4648                          gnus-tmp-gathered))
4649             (setq gnus-tmp-new-adopts (if (cdar thread)
4650                                           (append gnus-tmp-new-adopts
4651                                                   (cdar thread))
4652                                         gnus-tmp-new-adopts)
4653                   thread-end t
4654                   gnus-tmp-header nil)
4655             (when (zerop gnus-tmp-level)
4656               (setq gnus-tmp-root-expunged t)))
4657            ;; Perhaps this article is to be marked as read?
4658            ((and gnus-summary-mark-below
4659                  (< (or (cdr (assq number gnus-newsgroup-scored))
4660                         default-score)
4661                     gnus-summary-mark-below)
4662                  ;; Don't touch sparse articles.
4663                  (not (gnus-summary-article-sparse-p number))
4664                  (not (gnus-summary-article-ancient-p number)))
4665             (setq gnus-newsgroup-unreads
4666                   (delq number gnus-newsgroup-unreads))
4667             (if gnus-newsgroup-auto-expire
4668                 (setq gnus-newsgroup-expirable
4669                       (gnus-add-to-sorted-list
4670                        gnus-newsgroup-expirable number))
4671               (push (cons number gnus-low-score-mark)
4672                     gnus-newsgroup-reads))))
4673
4674           (when gnus-tmp-header
4675             ;; We may have an old dummy line to output before this
4676             ;; article.
4677             (when (and gnus-tmp-dummy-line
4678                        (gnus-subject-equal
4679                         gnus-tmp-dummy-line
4680                         (mail-header-subject gnus-tmp-header)))
4681               (gnus-summary-insert-dummy-line
4682                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4683               (setq gnus-tmp-dummy-line nil))
4684
4685             ;; Compute the mark.
4686             (setq gnus-tmp-unread (gnus-article-mark number))
4687
4688             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4689                                   gnus-tmp-header gnus-tmp-level)
4690                   gnus-newsgroup-data)
4691
4692             ;; Actually insert the line.
4693             (setq
4694              gnus-tmp-subject-or-nil
4695              (cond
4696               ((and gnus-thread-ignore-subject
4697                     gnus-tmp-prev-subject
4698                     (not (string= gnus-tmp-prev-subject simp-subject)))
4699                subject)
4700               ((zerop gnus-tmp-level)
4701                (if (and (eq gnus-summary-make-false-root 'empty)
4702                         (memq number gnus-tmp-gathered)
4703                         gnus-tmp-prev-subject
4704                         (string= gnus-tmp-prev-subject simp-subject))
4705                    gnus-summary-same-subject
4706                  subject))
4707               (t gnus-summary-same-subject)))
4708             (if (and (eq gnus-summary-make-false-root 'adopt)
4709                      (= gnus-tmp-level 1)
4710                      (memq number gnus-tmp-gathered))
4711                 (setq gnus-tmp-opening-bracket ?\<
4712                       gnus-tmp-closing-bracket ?\>)
4713               (setq gnus-tmp-opening-bracket ?\[
4714                     gnus-tmp-closing-bracket ?\]))
4715             (setq
4716              gnus-tmp-indentation
4717              (aref gnus-thread-indent-array gnus-tmp-level)
4718              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4719              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4720                                 gnus-summary-default-score 0)
4721              gnus-tmp-score-char
4722              (if (or (null gnus-summary-default-score)
4723                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4724                          gnus-summary-zcore-fuzz))
4725                  ?                      ;Whitespace
4726                (if (< gnus-tmp-score gnus-summary-default-score)
4727                    gnus-score-below-mark gnus-score-over-mark))
4728              gnus-tmp-replied
4729              (cond ((memq number gnus-newsgroup-processable)
4730                     gnus-process-mark)
4731                    ((memq number gnus-newsgroup-cached)
4732                     gnus-cached-mark)
4733                    ((memq number gnus-newsgroup-replied)
4734                     gnus-replied-mark)
4735                    ((memq number gnus-newsgroup-forwarded)
4736                     gnus-forwarded-mark)
4737                    ((memq number gnus-newsgroup-saved)
4738                     gnus-saved-mark)
4739                    ((memq number gnus-newsgroup-recent)
4740                     gnus-recent-mark)
4741                    ((memq number gnus-newsgroup-unseen)
4742                     gnus-unseen-mark)
4743                    (t gnus-no-mark))
4744              gnus-tmp-downloaded
4745              (cond ((memq number gnus-newsgroup-undownloaded) 
4746                     gnus-undownloaded-mark)
4747                    (gnus-newsgroup-agentized
4748                     gnus-downloaded-mark)
4749                    (t
4750                     gnus-no-mark))
4751              gnus-tmp-from (mail-header-from gnus-tmp-header)
4752              gnus-tmp-name
4753              (cond
4754               ((string-match "<[^>]+> *$" gnus-tmp-from)
4755                (setq beg-match (match-beginning 0))
4756                (or (and (string-match "^\".+\"" gnus-tmp-from)
4757                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4758                    (substring gnus-tmp-from 0 beg-match)))
4759               ((string-match "(.+)" gnus-tmp-from)
4760                (substring gnus-tmp-from
4761                           (1+ (match-beginning 0)) (1- (match-end 0))))
4762               (t gnus-tmp-from))
4763              gnus-tmp-thread-tree-header-string
4764              (cond
4765               ((not gnus-show-threads) "")
4766               ((zerop gnus-tmp-level)
4767                (if (cdar thread)
4768                    (or gnus-sum-thread-tree-root subject)
4769                  (or gnus-sum-thread-tree-single-indent subject)))
4770               (t
4771                (concat (apply 'concat
4772                               (mapcar (lambda (item)
4773                                         (if (= item 1)
4774                                             gnus-sum-thread-tree-vertical
4775                                           gnus-sum-thread-tree-indent))
4776                                       (cdr (reverse tree-stack))))
4777                        (if (nth 1 thread)
4778                            gnus-sum-thread-tree-leaf-with-other
4779                          gnus-sum-thread-tree-single-leaf)))))
4780             (when (string= gnus-tmp-name "")
4781               (setq gnus-tmp-name gnus-tmp-from))
4782             (unless (numberp gnus-tmp-lines)
4783               (setq gnus-tmp-lines -1))
4784             (if (= gnus-tmp-lines -1)
4785                 (setq gnus-tmp-lines "?")
4786               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4787             (gnus-put-text-property
4788              (point)
4789              (progn (eval gnus-summary-line-format-spec) (point))
4790              'gnus-number number)
4791             (when gnus-visual-p
4792               (forward-line -1)
4793               (gnus-run-hooks 'gnus-summary-update-hook)
4794               (forward-line 1))
4795
4796             (setq gnus-tmp-prev-subject simp-subject)))
4797
4798         (when (nth 1 thread)
4799           (push (list (max 0 gnus-tmp-level)
4800                       (copy-list tree-stack)
4801                       (nthcdr 1 thread))
4802                 stack))
4803         (push (if (nth 1 thread) 1 0) tree-stack)
4804         (incf gnus-tmp-level)
4805         (setq threads (if thread-end nil (cdar thread)))
4806         (if gnus-summary-display-while-building
4807             (if building-count
4808                 (progn
4809                   ;; use a set frequency
4810                   (setq building-line-count (1- building-line-count))
4811                   (when (= building-line-count 0)
4812                     (sit-for 0)
4813                     (setq building-line-count
4814                           gnus-summary-display-while-building)))
4815               ;; always
4816               (sit-for 0)))
4817         (unless threads
4818           (setq gnus-tmp-level 0)))))
4819   (gnus-message 7 "Generating summary...done"))
4820
4821 (defun gnus-summary-prepare-unthreaded (headers)
4822   "Generate an unthreaded summary buffer based on HEADERS."
4823   (let (header number mark)
4824
4825     (beginning-of-line)
4826
4827     (while headers
4828       ;; We may have to root out some bad articles...
4829       (when (memq (setq number (mail-header-number
4830                                 (setq header (pop headers))))
4831                   gnus-newsgroup-limit)
4832         ;; Mark article as read when it has a low score.
4833         (when (and gnus-summary-mark-below
4834                    (< (or (cdr (assq number gnus-newsgroup-scored))
4835                           gnus-summary-default-score 0)
4836                       gnus-summary-mark-below)
4837                    (not (gnus-summary-article-ancient-p number)))
4838           (setq gnus-newsgroup-unreads
4839                 (delq number gnus-newsgroup-unreads))
4840           (if gnus-newsgroup-auto-expire
4841               (push number gnus-newsgroup-expirable)
4842             (push (cons number gnus-low-score-mark)
4843                   gnus-newsgroup-reads)))
4844
4845         (setq mark (gnus-article-mark number))
4846         (push (gnus-data-make number mark (1+ (point)) header 0)
4847               gnus-newsgroup-data)
4848         (gnus-summary-insert-line
4849          header 0 number
4850          (memq number gnus-newsgroup-undownloaded)
4851          mark (memq number gnus-newsgroup-replied)
4852          (memq number gnus-newsgroup-expirable)
4853          (mail-header-subject header) nil
4854          (cdr (assq number gnus-newsgroup-scored))
4855          (memq number gnus-newsgroup-processable))))))
4856
4857 (defun gnus-summary-remove-list-identifiers ()
4858   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4859   (let ((regexp (if (consp gnus-list-identifiers)
4860                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4861                   gnus-list-identifiers))
4862         changed subject)
4863     (when regexp
4864       (dolist (header gnus-newsgroup-headers)
4865         (setq subject (mail-header-subject header)
4866               changed nil)
4867         (while (string-match
4868                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4869                 subject)
4870           (setq subject
4871                 (concat (substring subject 0 (match-beginning 2))
4872                         (substring subject (match-end 0)))
4873                 changed t))
4874         (when (and changed
4875                    (string-match
4876                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4877           (setq subject
4878                 (concat (substring subject 0 (match-beginning 1))
4879                         (substring subject (match-end 1)))))
4880         (when changed
4881           (mail-header-set-subject header subject))))))
4882
4883 (defun gnus-fetch-headers (articles)
4884   "Fetch headers of ARTICLES."
4885   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4886     (gnus-message 5 "Fetching headers for %s..." name)
4887     (prog1
4888         (if (eq 'nov
4889                 (setq gnus-headers-retrieved-by
4890                       (gnus-retrieve-headers
4891                        articles gnus-newsgroup-name
4892                        ;; We might want to fetch old headers, but
4893                        ;; not if there is only 1 article.
4894                        (and (or (and
4895                                  (not (eq gnus-fetch-old-headers 'some))
4896                                  (not (numberp gnus-fetch-old-headers)))
4897                                 (> (length articles) 1))
4898                             gnus-fetch-old-headers))))
4899             (gnus-get-newsgroup-headers-xover
4900              articles nil nil gnus-newsgroup-name t)
4901           (gnus-get-newsgroup-headers))
4902       (gnus-message 5 "Fetching headers for %s...done" name))))
4903
4904 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4905   "Select newsgroup GROUP.
4906 If READ-ALL is non-nil, all articles in the group are selected.
4907 If SELECT-ARTICLES, only select those articles from GROUP."
4908   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4909          ;;!!! Dirty hack; should be removed.
4910          (gnus-summary-ignore-duplicates
4911           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4912               t
4913             gnus-summary-ignore-duplicates))
4914          (info (nth 2 entry))
4915          articles fetched-articles cached)
4916
4917     (unless (gnus-check-server
4918              (set (make-local-variable 'gnus-current-select-method)
4919                   (gnus-find-method-for-group group)))
4920       (error "Couldn't open server"))
4921
4922     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4923         (gnus-activate-group group)     ; Or we can activate it...
4924         (progn                          ; Or we bug out.
4925           (when (equal major-mode 'gnus-summary-mode)
4926             (kill-buffer (current-buffer)))
4927           (error "Couldn't activate group %s: %s"
4928                  group (gnus-status-message group))))
4929
4930     (unless (gnus-request-group group t)
4931       (when (equal major-mode 'gnus-summary-mode)
4932         (kill-buffer (current-buffer)))
4933       (error "Couldn't request group %s: %s"
4934              group (gnus-status-message group)))
4935
4936     (setq gnus-newsgroup-name group
4937           gnus-newsgroup-unselected nil
4938           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4939
4940     (let ((display (gnus-group-find-parameter group 'display)))
4941       (setq gnus-newsgroup-display
4942             (cond
4943              ((not (zerop (or (car-safe read-all) 0)))
4944               ;; The user entered the group with C-u SPC/RET, let's show
4945               ;; all articles.
4946               'gnus-not-ignore)
4947              ((eq display 'all)
4948               'gnus-not-ignore)
4949              ((arrayp display)
4950               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4951              ((numberp display)
4952               ;; The following is probably the "correct" solution, but
4953               ;; it makes Gnus fetch all headers and then limit the
4954               ;; articles (which is slow), so instead we hack the
4955               ;; select-articles parameter instead. -- Simon Josefsson
4956               ;; <jas@kth.se>
4957               ;;
4958               ;; (gnus-byte-compile
4959               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4960               ;;                         display)))))
4961               (setq select-articles
4962                     (gnus-uncompress-range
4963                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4964                              (if (> tmp 0)
4965                                  tmp
4966                                1))
4967                            (cdr (gnus-active group)))))
4968               nil)
4969              (t
4970               nil))))
4971
4972     (gnus-summary-setup-default-charset)
4973
4974     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4975     (when (gnus-virtual-group-p group)
4976       (setq cached gnus-newsgroup-cached))
4977
4978     (setq gnus-newsgroup-unreads
4979           (gnus-sorted-ndifference
4980            (gnus-sorted-ndifference gnus-newsgroup-unreads
4981                                     gnus-newsgroup-marked)
4982            gnus-newsgroup-dormant))
4983
4984     (setq gnus-newsgroup-processable nil)
4985
4986     (gnus-update-read-articles group gnus-newsgroup-unreads)
4987
4988     ;; Adjust and set lists of article marks.
4989     (when info
4990       (gnus-adjust-marked-articles info))
4991     (if (setq articles select-articles)
4992         (setq gnus-newsgroup-unselected
4993               (gnus-sorted-difference gnus-newsgroup-unreads articles))
4994       (setq articles (gnus-articles-to-read group read-all)))
4995
4996     (cond
4997      ((null articles)
4998       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4999       'quit)
5000      ((eq articles 0) nil)
5001      (t
5002       ;; Init the dependencies hash table.
5003       (setq gnus-newsgroup-dependencies
5004             (gnus-make-hashtable (length articles)))
5005       (gnus-set-global-variables)
5006       ;; Retrieve the headers and read them in.
5007
5008       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5009
5010       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5011       (when cached
5012         (setq gnus-newsgroup-cached cached))
5013
5014       ;; Suppress duplicates?
5015       (when gnus-suppress-duplicates
5016         (gnus-dup-suppress-articles))
5017
5018       ;; Set the initial limit.
5019       (setq gnus-newsgroup-limit (copy-sequence articles))
5020       ;; Remove canceled articles from the list of unread articles.
5021       (setq fetched-articles
5022             (mapcar (lambda (headers) (mail-header-number headers))
5023                     gnus-newsgroup-headers))
5024       (setq gnus-newsgroup-articles fetched-articles)
5025       (setq gnus-newsgroup-unreads
5026             (gnus-sorted-nintersection
5027              gnus-newsgroup-unreads fetched-articles))
5028       (gnus-compute-unseen-list)
5029
5030       ;; Removed marked articles that do not exist.
5031       (gnus-update-missing-marks
5032        (gnus-sorted-difference articles fetched-articles))
5033       ;; We might want to build some more threads first.
5034       (when (and gnus-fetch-old-headers
5035                  (eq gnus-headers-retrieved-by 'nov))
5036         (if (eq gnus-fetch-old-headers 'invisible)
5037             (gnus-build-all-threads)
5038           (gnus-build-old-threads)))
5039       ;; Let the Gnus agent mark articles as read.
5040       (when gnus-agent
5041         (gnus-agent-get-undownloaded-list))
5042       ;; Remove list identifiers from subject
5043       (when gnus-list-identifiers
5044         (gnus-summary-remove-list-identifiers))
5045       ;; Check whether auto-expire is to be done in this group.
5046       (setq gnus-newsgroup-auto-expire
5047             (gnus-group-auto-expirable-p group))
5048       ;; Set up the article buffer now, if necessary.
5049       (unless gnus-single-article-buffer
5050         (gnus-article-setup-buffer))
5051       ;; First and last article in this newsgroup.
5052       (when gnus-newsgroup-headers
5053         (setq gnus-newsgroup-begin
5054               (mail-header-number (car gnus-newsgroup-headers))
5055               gnus-newsgroup-end
5056               (mail-header-number
5057                (gnus-last-element gnus-newsgroup-headers))))
5058       ;; GROUP is successfully selected.
5059       (or gnus-newsgroup-headers t)))))
5060
5061 (defun gnus-compute-unseen-list ()
5062   ;; The `seen' marks are treated specially.
5063   (if (not gnus-newsgroup-seen)
5064       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5065     (setq gnus-newsgroup-unseen
5066           (gnus-inverse-list-range-intersection
5067            gnus-newsgroup-articles gnus-newsgroup-seen))))
5068
5069 (defun gnus-summary-display-make-predicate (display)
5070   (require 'gnus-agent)
5071   (when (= (length display) 1)
5072     (setq display (car display)))
5073   (unless gnus-summary-display-cache
5074     (dolist (elem (append '((unread . unread)
5075                             (read . read)
5076                             (unseen . unseen))
5077                           gnus-article-mark-lists))
5078       (push (cons (cdr elem)
5079                   (gnus-byte-compile
5080                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5081             gnus-summary-display-cache)))
5082   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5083         (gnus-category-predicate-cache gnus-summary-display-cache))
5084     (gnus-get-predicate display)))
5085
5086 ;; Uses the dynamically bound `number' variable.
5087 (eval-when-compile
5088   (defvar number))
5089 (defun gnus-article-marked-p (type &optional article)
5090   (let ((article (or article number)))
5091     (cond
5092      ((eq type 'tick)
5093       (memq article gnus-newsgroup-marked))
5094      ((eq type 'spam)
5095       (memq article gnus-newsgroup-spam-marked))
5096      ((eq type 'unsend)
5097       (memq article gnus-newsgroup-unsendable))
5098      ((eq type 'undownload)
5099       (memq article gnus-newsgroup-undownloaded))
5100      ((eq type 'download)
5101       (memq article gnus-newsgroup-downloadable))
5102      ((eq type 'unread)
5103       (memq article gnus-newsgroup-unreads))
5104      ((eq type 'read)
5105       (memq article gnus-newsgroup-reads))
5106      ((eq type 'dormant)
5107       (memq article gnus-newsgroup-dormant) )
5108      ((eq type 'expire)
5109       (memq article gnus-newsgroup-expirable))
5110      ((eq type 'reply)
5111       (memq article gnus-newsgroup-replied))
5112      ((eq type 'killed)
5113       (memq article gnus-newsgroup-killed))
5114      ((eq type 'bookmark)
5115       (assq article gnus-newsgroup-bookmarks))
5116      ((eq type 'score)
5117       (assq article gnus-newsgroup-scored))
5118      ((eq type 'save)
5119       (memq article gnus-newsgroup-saved))
5120      ((eq type 'cache)
5121       (memq article gnus-newsgroup-cached))
5122      ((eq type 'forward)
5123       (memq article gnus-newsgroup-forwarded))
5124      ((eq type 'seen)
5125       (not (memq article gnus-newsgroup-unseen)))
5126      ((eq type 'recent)
5127       (memq article gnus-newsgroup-recent))
5128      (t t))))
5129
5130 (defun gnus-articles-to-read (group &optional read-all)
5131   "Find out what articles the user wants to read."
5132   (let* ((display (gnus-group-find-parameter group 'display))
5133          (articles
5134           ;; Select all articles if `read-all' is non-nil, or if there
5135           ;; are no unread articles.
5136           (if (or read-all
5137                   (and (zerop (length gnus-newsgroup-marked))
5138                        (zerop (length gnus-newsgroup-unreads)))
5139                   ;; Fetch all if the predicate is non-nil.
5140                   gnus-newsgroup-display)
5141               ;; We want to select the headers for all the articles in
5142               ;; the group, so we select either all the active
5143               ;; articles in the group, or (if that's nil), the
5144               ;; articles in the cache.
5145               (or
5146                (gnus-uncompress-range (gnus-active group))
5147                (gnus-cache-articles-in-group group))
5148             ;; Select only the "normal" subset of articles.
5149             (gnus-sorted-nunion
5150              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5151              gnus-newsgroup-unreads)))
5152          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5153          (scored (length scored-list))
5154          (number (length articles))
5155          (marked (+ (length gnus-newsgroup-marked)
5156                     (length gnus-newsgroup-dormant)))
5157          (select
5158           (cond
5159            ((numberp read-all)
5160             read-all)
5161            ((numberp gnus-newsgroup-display)
5162             gnus-newsgroup-display)
5163            (t
5164             (condition-case ()
5165                 (cond
5166                  ((and (or (<= scored marked) (= scored number))
5167                        (numberp gnus-large-newsgroup)
5168                        (> number gnus-large-newsgroup))
5169                   (let* ((cursor-in-echo-area nil)
5170                          (initial (gnus-parameter-large-newsgroup-initial
5171                                    gnus-newsgroup-name))
5172                          (input
5173                           (read-string
5174                            (format
5175                             "How many articles from %s (%s %d): "
5176                             (gnus-limit-string
5177                              (gnus-group-decoded-name gnus-newsgroup-name)
5178                              35)
5179                             (if initial "max" "default")
5180                             number)
5181                            (if initial
5182                                (cons (number-to-string initial)
5183                                      0)))))
5184                     (if (string-match "^[ \t]*$" input) number input)))
5185                  ((and (> scored marked) (< scored number)
5186                        (> (- scored number) 20))
5187                   (let ((input
5188                          (read-string
5189                           (format "%s %s (%d scored, %d total): "
5190                                   "How many articles from"
5191                                   (gnus-group-decoded-name group)
5192                                   scored number))))
5193                     (if (string-match "^[ \t]*$" input)
5194                         number input)))
5195                  (t number))
5196               (quit
5197                (message "Quit getting the articles to read")
5198                nil))))))
5199     (setq select (if (stringp select) (string-to-number select) select))
5200     (if (or (null select) (zerop select))
5201         select
5202       (if (and (not (zerop scored)) (<= (abs select) scored))
5203           (progn
5204             (setq articles (sort scored-list '<))
5205             (setq number (length articles)))
5206         (setq articles (copy-sequence articles)))
5207
5208       (when (< (abs select) number)
5209         (if (< select 0)
5210             ;; Select the N oldest articles.
5211             (setcdr (nthcdr (1- (abs select)) articles) nil)
5212           ;; Select the N most recent articles.
5213           (setq articles (nthcdr (- number select) articles))))
5214       (setq gnus-newsgroup-unselected
5215             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5216       (when gnus-alter-articles-to-read-function
5217         (setq articles
5218               (sort
5219                (funcall gnus-alter-articles-to-read-function
5220                         gnus-newsgroup-name articles)
5221                '<)))
5222       articles)))
5223
5224 (defun gnus-killed-articles (killed articles)
5225   (let (out)
5226     (while articles
5227       (when (inline (gnus-member-of-range (car articles) killed))
5228         (push (car articles) out))
5229       (setq articles (cdr articles)))
5230     out))
5231
5232 (defun gnus-uncompress-marks (marks)
5233   "Uncompress the mark ranges in MARKS."
5234   (let ((uncompressed '(score bookmark))
5235         out)
5236     (while marks
5237       (if (memq (caar marks) uncompressed)
5238           (push (car marks) out)
5239         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5240       (setq marks (cdr marks)))
5241     out))
5242
5243 (defun gnus-article-mark-to-type (mark)
5244   "Return the type of MARK."
5245   (or (cadr (assq mark gnus-article-special-mark-lists))
5246       'list))
5247
5248 (defun gnus-article-unpropagatable-p (mark)
5249   "Return whether MARK should be propagated to backend."
5250   (memq mark gnus-article-unpropagated-mark-lists))
5251
5252 (defun gnus-adjust-marked-articles (info)
5253   "Set all article lists and remove all marks that are no longer valid."
5254   (let* ((marked-lists (gnus-info-marks info))
5255          (active (gnus-active (gnus-info-group info)))
5256          (min (car active))
5257          (max (cdr active))
5258          (types gnus-article-mark-lists)
5259          marks var articles article mark mark-type)
5260
5261     (dolist (marks marked-lists)
5262       (setq mark (car marks)
5263             mark-type (gnus-article-mark-to-type mark)
5264             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5265
5266       ;; We set the variable according to the type of the marks list,
5267       ;; and then adjust the marks to a subset of the active articles.
5268       (cond
5269        ;; Adjust "simple" lists.
5270        ((eq mark-type 'list)
5271         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5272         (when (memq mark '(tick dormant expire reply save))
5273           (while articles
5274             (when (or (< (setq article (pop articles)) min) (> article max))
5275               (set var (delq article (symbol-value var)))))))
5276        ;; Adjust assocs.
5277        ((eq mark-type 'tuple)
5278         (set var (setq articles (cdr marks)))
5279         (when (not (listp (cdr (symbol-value var))))
5280           (set var (list (symbol-value var))))
5281         (when (not (listp (cdr articles)))
5282           (setq articles (list articles)))
5283         (while articles
5284           (when (or (not (consp (setq article (pop articles))))
5285                     (< (car article) min)
5286                     (> (car article) max))
5287             (set var (delq article (symbol-value var))))))
5288        ;; Adjust ranges (sloppily).
5289        ((eq mark-type 'range)
5290         (cond
5291          ((eq mark 'seen)
5292           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5293           ;; It should be (seen (NUM1 . NUM2)).
5294           (when (numberp (cddr marks))
5295             (setcdr marks (list (cdr marks))))
5296           (setq articles (cdr marks))
5297           (while (and articles
5298                       (or (and (consp (car articles))
5299                                (> min (cdar articles)))
5300                           (and (numberp (car articles))
5301                                (> min (car articles)))))
5302             (pop articles))
5303           (set var articles))))))))
5304
5305 (defun gnus-update-missing-marks (missing)
5306   "Go through the list of MISSING articles and remove them from the mark lists."
5307   (when missing
5308     (let (var m)
5309       ;; Go through all types.
5310       (dolist (elem gnus-article-mark-lists)
5311         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5312           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5313           (when (symbol-value var)
5314             ;; This list has articles.  So we delete all missing
5315             ;; articles from it.
5316             (setq m missing)
5317             (while m
5318               (set var (delq (pop m) (symbol-value var))))))))))
5319
5320 (defun gnus-update-marks ()
5321   "Enter the various lists of marked articles into the newsgroup info list."
5322   (let ((types gnus-article-mark-lists)
5323         (info (gnus-get-info gnus-newsgroup-name))
5324         type list newmarked symbol delta-marks)
5325     (when info
5326       ;; Add all marks lists to the list of marks lists.
5327       (while (setq type (pop types))
5328         (setq list (symbol-value
5329                     (setq symbol
5330                           (intern (format "gnus-newsgroup-%s" (car type))))))
5331
5332         (when list
5333           ;; Get rid of the entries of the articles that have the
5334           ;; default score.
5335           (when (and (eq (cdr type) 'score)
5336                      gnus-save-score
5337                      list)
5338             (let* ((arts list)
5339                    (prev (cons nil list))
5340                    (all prev))
5341               (while arts
5342                 (if (or (not (consp (car arts)))
5343                         (= (cdar arts) gnus-summary-default-score))
5344                     (setcdr prev (cdr arts))
5345                   (setq prev arts))
5346                 (setq arts (cdr arts)))
5347               (setq list (cdr all)))))
5348
5349         (when (eq (cdr type) 'seen)
5350           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5351
5352         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5353           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5354
5355         (when (and (gnus-check-backend-function
5356                     'request-set-mark gnus-newsgroup-name)
5357                    (not (gnus-article-unpropagatable-p (cdr type))))
5358           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5359                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5360                  (add (gnus-remove-from-range
5361                        (gnus-copy-sequence list) old)))
5362             (when add
5363               (push (list add 'add (list (cdr type))) delta-marks))
5364             (when del
5365               (push (list del 'del (list (cdr type))) delta-marks))))
5366
5367         (when list
5368           (push (cons (cdr type) list) newmarked)))
5369
5370       (when delta-marks
5371         (unless (gnus-check-group gnus-newsgroup-name)
5372           (error "Can't open server for %s" gnus-newsgroup-name))
5373         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5374
5375       ;; Enter these new marks into the info of the group.
5376       (if (nthcdr 3 info)
5377           (setcar (nthcdr 3 info) newmarked)
5378         ;; Add the marks lists to the end of the info.
5379         (when newmarked
5380           (setcdr (nthcdr 2 info) (list newmarked))))
5381
5382       ;; Cut off the end of the info if there's nothing else there.
5383       (let ((i 5))
5384         (while (and (> i 2)
5385                     (not (nth i info)))
5386           (when (nthcdr (decf i) info)
5387             (setcdr (nthcdr i info) nil)))))))
5388
5389 (defun gnus-set-mode-line (where)
5390   "Set the mode line of the article or summary buffers.
5391 If WHERE is `summary', the summary mode line format will be used."
5392   ;; Is this mode line one we keep updated?
5393   (when (and (memq where gnus-updated-mode-lines)
5394              (symbol-value
5395               (intern (format "gnus-%s-mode-line-format-spec" where))))
5396     (let (mode-string)
5397       (save-excursion
5398         ;; We evaluate this in the summary buffer since these
5399         ;; variables are buffer-local to that buffer.
5400         (set-buffer gnus-summary-buffer)
5401        ;; We bind all these variables that are used in the `eval' form
5402         ;; below.
5403         (let* ((mformat (symbol-value
5404                          (intern
5405                           (format "gnus-%s-mode-line-format-spec" where))))
5406                (gnus-tmp-group-name (gnus-group-decoded-name
5407                                      gnus-newsgroup-name))
5408                (gnus-tmp-article-number (or gnus-current-article 0))
5409                (gnus-tmp-unread gnus-newsgroup-unreads)
5410                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5411                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5412                (gnus-tmp-unread-and-unselected
5413                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5414                             (zerop gnus-tmp-unselected))
5415                        "")
5416                       ((zerop gnus-tmp-unselected)
5417                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5418                       (t (format "{%d(+%d) more}"
5419                                  gnus-tmp-unread-and-unticked
5420                                  gnus-tmp-unselected))))
5421                (gnus-tmp-subject
5422                 (if (and gnus-current-headers
5423                          (vectorp gnus-current-headers))
5424                     (gnus-mode-string-quote
5425                      (mail-header-subject gnus-current-headers))
5426                   ""))
5427                bufname-length max-len
5428                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5429           (setq mode-string (eval mformat))
5430           (setq bufname-length (if (string-match "%b" mode-string)
5431                                    (- (length
5432                                        (buffer-name
5433                                         (if (eq where 'summary)
5434                                             nil
5435                                           (get-buffer gnus-article-buffer))))
5436                                       2)
5437                                  0))
5438           (setq max-len (max 4 (if gnus-mode-non-string-length
5439                                    (- (window-width)
5440                                       gnus-mode-non-string-length
5441                                       bufname-length)
5442                                  (length mode-string))))
5443           ;; We might have to chop a bit of the string off...
5444           (when (> (length mode-string) max-len)
5445             (setq mode-string
5446                   (concat (truncate-string-to-width mode-string (- max-len 3))
5447                           "...")))
5448           ;; Pad the mode string a bit.
5449           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5450       ;; Update the mode line.
5451       (setq mode-line-buffer-identification
5452             (gnus-mode-line-buffer-identification (list mode-string)))
5453       (set-buffer-modified-p t))))
5454
5455 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5456   "Go through the HEADERS list and add all Xrefs to a hash table.
5457 The resulting hash table is returned, or nil if no Xrefs were found."
5458   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5459          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5460          (xref-hashtb (gnus-make-hashtable))
5461          start group entry number xrefs header)
5462     (while headers
5463       (setq header (pop headers))
5464       (when (and (setq xrefs (mail-header-xref header))
5465                  (not (memq (setq number (mail-header-number header))
5466                             unreads)))
5467         (setq start 0)
5468         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5469           (setq start (match-end 0))
5470           (setq group (if prefix
5471                           (concat prefix (substring xrefs (match-beginning 1)
5472                                                     (match-end 1)))
5473                         (substring xrefs (match-beginning 1) (match-end 1))))
5474           (setq number
5475                 (string-to-int (substring xrefs (match-beginning 2)
5476                                           (match-end 2))))
5477           (if (setq entry (gnus-gethash group xref-hashtb))
5478               (setcdr entry (cons number (cdr entry)))
5479             (gnus-sethash group (cons number nil) xref-hashtb)))))
5480     (and start xref-hashtb)))
5481
5482 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5483   "Look through all the headers and mark the Xrefs as read."
5484   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5485         name entry info xref-hashtb idlist method nth4)
5486     (save-excursion
5487       (set-buffer gnus-group-buffer)
5488       (when (setq xref-hashtb
5489                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5490         (mapatoms
5491          (lambda (group)
5492            (unless (string= from-newsgroup (setq name (symbol-name group)))
5493              (setq idlist (symbol-value group))
5494              ;; Dead groups are not updated.
5495              (and (prog1
5496                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5497                             info (nth 2 entry))
5498                     (when (stringp (setq nth4 (gnus-info-method info)))
5499                       (setq nth4 (gnus-server-to-method nth4))))
5500                   ;; Only do the xrefs if the group has the same
5501                   ;; select method as the group we have just read.
5502                   (or (gnus-methods-equal-p
5503                        nth4 (gnus-find-method-for-group from-newsgroup))
5504                       virtual
5505                       (equal nth4 (setq method (gnus-find-method-for-group
5506                                                 from-newsgroup)))
5507                       (and (equal (car nth4) (car method))
5508                            (equal (nth 1 nth4) (nth 1 method))))
5509                   gnus-use-cross-reference
5510                   (or (not (eq gnus-use-cross-reference t))
5511                       virtual
5512                       ;; Only do cross-references on subscribed
5513                       ;; groups, if that is what is wanted.
5514                       (<= (gnus-info-level info) gnus-level-subscribed))
5515                   (gnus-group-make-articles-read name idlist))))
5516          xref-hashtb)))))
5517
5518 (defun gnus-compute-read-articles (group articles)
5519   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5520          (info (nth 2 entry))
5521          (active (gnus-active group))
5522          ninfo)
5523     (when entry
5524       ;; First peel off all invalid article numbers.
5525       (when active
5526         (let ((ids articles)
5527               id first)
5528           (while (setq id (pop ids))
5529             (when (and first (> id (cdr active)))
5530               ;; We'll end up in this situation in one particular
5531               ;; obscure situation.  If you re-scan a group and get
5532               ;; a new article that is cross-posted to a different
5533               ;; group that has not been re-scanned, you might get
5534               ;; crossposted article that has a higher number than
5535               ;; Gnus believes possible.  So we re-activate this
5536               ;; group as well.  This might mean doing the
5537               ;; crossposting thingy will *increase* the number
5538               ;; of articles in some groups.  Tsk, tsk.
5539               (setq active (or (gnus-activate-group group) active)))
5540             (when (or (> id (cdr active))
5541                       (< id (car active)))
5542               (setq articles (delq id articles))))))
5543       ;; If the read list is nil, we init it.
5544       (if (and active
5545                (null (gnus-info-read info))
5546                (> (car active) 1))
5547           (setq ninfo (cons 1 (1- (car active))))
5548         (setq ninfo (gnus-info-read info)))
5549       ;; Then we add the read articles to the range.
5550       (gnus-add-to-range
5551        ninfo (setq articles (sort articles '<))))))
5552
5553 (defun gnus-group-make-articles-read (group articles)
5554   "Update the info of GROUP to say that ARTICLES are read."
5555   (let* ((num 0)
5556          (entry (gnus-gethash group gnus-newsrc-hashtb))
5557          (info (nth 2 entry))
5558          (active (gnus-active group))
5559          range)
5560     (when entry
5561       (setq range (gnus-compute-read-articles group articles))
5562       (save-excursion
5563         (set-buffer gnus-group-buffer)
5564         (gnus-undo-register
5565           `(progn
5566              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5567              (gnus-info-set-read ',info ',(gnus-info-read info))
5568              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5569              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5570              (gnus-group-update-group ,group t))))
5571       ;; Add the read articles to the range.
5572       (gnus-info-set-read info range)
5573       (gnus-request-set-mark group (list (list range 'add '(read))))
5574       ;; Then we have to re-compute how many unread
5575       ;; articles there are in this group.
5576       (when active
5577         (cond
5578          ((not range)
5579           (setq num (- (1+ (cdr active)) (car active))))
5580          ((not (listp (cdr range)))
5581           (setq num (- (cdr active) (- (1+ (cdr range))
5582                                        (car range)))))
5583          (t
5584           (while range
5585             (if (numberp (car range))
5586                 (setq num (1+ num))
5587               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5588             (setq range (cdr range)))
5589           (setq num (- (cdr active) num))))
5590         ;; Update the number of unread articles.
5591         (setcar entry num)
5592         ;; Update the group buffer.
5593         (unless (gnus-ephemeral-group-p group)
5594           (gnus-group-update-group group t))))))
5595
5596 (defvar gnus-newsgroup-none-id 0)
5597
5598 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5599   (let ((cur nntp-server-buffer)
5600         (dependencies
5601          (or dependencies
5602              (save-excursion (set-buffer gnus-summary-buffer)
5603                              gnus-newsgroup-dependencies)))
5604         headers id end ref
5605         (mail-parse-charset gnus-newsgroup-charset)
5606         (mail-parse-ignored-charsets
5607          (save-excursion (condition-case nil
5608                              (set-buffer gnus-summary-buffer)
5609                            (error))
5610                          gnus-newsgroup-ignored-charsets)))
5611     (save-excursion
5612       (set-buffer nntp-server-buffer)
5613       ;; Translate all TAB characters into SPACE characters.
5614       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5615       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5616       (gnus-run-hooks 'gnus-parse-headers-hook)
5617       (let ((case-fold-search t)
5618             in-reply-to header p lines chars)
5619         (goto-char (point-min))
5620         ;; Search to the beginning of the next header.  Error messages
5621         ;; do not begin with 2 or 3.
5622         (while (re-search-forward "^[23][0-9]+ " nil t)
5623           (setq id nil
5624                 ref nil)
5625           ;; This implementation of this function, with nine
5626           ;; search-forwards instead of the one re-search-forward and
5627           ;; a case (which basically was the old function) is actually
5628           ;; about twice as fast, even though it looks messier.  You
5629           ;; can't have everything, I guess.  Speed and elegance
5630           ;; doesn't always go hand in hand.
5631           (setq
5632            header
5633            (vector
5634             ;; Number.
5635             (prog1
5636                 (read cur)
5637               (end-of-line)
5638               (setq p (point))
5639               (narrow-to-region (point)
5640                                 (or (and (search-forward "\n.\n" nil t)
5641                                          (- (point) 2))
5642                                     (point))))
5643             ;; Subject.
5644             (progn
5645               (goto-char p)
5646               (if (search-forward "\nsubject:" nil t)
5647                   (funcall gnus-decode-encoded-word-function
5648                            (nnheader-header-value))
5649                 "(none)"))
5650             ;; From.
5651             (progn
5652               (goto-char p)
5653               (if (search-forward "\nfrom:" nil t)
5654                   (funcall gnus-decode-encoded-word-function
5655                            (nnheader-header-value))
5656                 "(nobody)"))
5657             ;; Date.
5658             (progn
5659               (goto-char p)
5660               (if (search-forward "\ndate:" nil t)
5661                   (nnheader-header-value) ""))
5662             ;; Message-ID.
5663             (progn
5664               (goto-char p)
5665               (setq id (if (re-search-forward
5666                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5667                            ;; We do it this way to make sure the Message-ID
5668                            ;; is (somewhat) syntactically valid.
5669                            (buffer-substring (match-beginning 1)
5670                                              (match-end 1))
5671                          ;; If there was no message-id, we just fake one
5672                          ;; to make subsequent routines simpler.
5673                          (nnheader-generate-fake-message-id))))
5674             ;; References.
5675             (progn
5676               (goto-char p)
5677               (if (search-forward "\nreferences:" nil t)
5678                   (progn
5679                     (setq end (point))
5680                     (prog1
5681                         (nnheader-header-value)
5682                       (setq ref
5683                             (buffer-substring
5684                              (progn
5685                                (end-of-line)
5686                                (search-backward ">" end t)
5687                                (1+ (point)))
5688                              (progn
5689                                (search-backward "<" end t)
5690                                (point))))))
5691                 ;; Get the references from the in-reply-to header if there
5692                 ;; were no references and the in-reply-to header looks
5693                 ;; promising.
5694                 (if (and (search-forward "\nin-reply-to:" nil t)
5695                          (setq in-reply-to (nnheader-header-value))
5696                          (string-match "<[^>]+>" in-reply-to))
5697                     (let (ref2)
5698                       (setq ref (substring in-reply-to (match-beginning 0)
5699                                            (match-end 0)))
5700                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5701                         (setq ref2 (substring in-reply-to (match-beginning 0)
5702                                               (match-end 0)))
5703                         (when (> (length ref2) (length ref))
5704                           (setq ref ref2)))
5705                       ref)
5706                   (setq ref nil))))
5707             ;; Chars.
5708             (progn
5709               (goto-char p)
5710               (if (search-forward "\nchars: " nil t)
5711                   (if (numberp (setq chars (ignore-errors (read cur))))
5712                       chars -1)
5713                 -1))
5714             ;; Lines.
5715             (progn
5716               (goto-char p)
5717               (if (search-forward "\nlines: " nil t)
5718                   (if (numberp (setq lines (ignore-errors (read cur))))
5719                       lines -1)
5720                 -1))
5721             ;; Xref.
5722             (progn
5723               (goto-char p)
5724               (and (search-forward "\nxref:" nil t)
5725                    (nnheader-header-value)))
5726             ;; Extra.
5727             (when gnus-extra-headers
5728               (let ((extra gnus-extra-headers)
5729                     out)
5730                 (while extra
5731                   (goto-char p)
5732                   (when (search-forward
5733                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5734                     (push (cons (car extra) (nnheader-header-value))
5735                           out))
5736                   (pop extra))
5737                 out))))
5738           (when (equal id ref)
5739             (setq ref nil))
5740
5741           (when gnus-alter-header-function
5742             (funcall gnus-alter-header-function header)
5743             (setq id (mail-header-id header)
5744                   ref (gnus-parent-id (mail-header-references header))))
5745
5746           (when (setq header
5747                       (gnus-dependencies-add-header
5748                        header dependencies force-new))
5749             (push header headers))
5750           (goto-char (point-max))
5751           (widen))
5752         (nreverse headers)))))
5753
5754 ;; Goes through the xover lines and returns a list of vectors
5755 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5756                                                   force-new dependencies
5757                                                   group also-fetch-heads)
5758   "Parse the news overview data in the server buffer.
5759 Return a list of headers that match SEQUENCE (see
5760 `nntp-retrieve-headers')."
5761   ;; Get the Xref when the users reads the articles since most/some
5762   ;; NNTP servers do not include Xrefs when using XOVER.
5763   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5764   (let ((mail-parse-charset gnus-newsgroup-charset)
5765         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5766         (cur nntp-server-buffer)
5767         (dependencies (or dependencies gnus-newsgroup-dependencies))
5768         (allp (cond
5769                ((eq gnus-read-all-available-headers t)
5770                 t)
5771                ((stringp gnus-read-all-available-headers)
5772                 (string-match gnus-read-all-available-headers group))
5773                (t
5774                 nil)))
5775         number headers header)
5776     (save-excursion
5777       (set-buffer nntp-server-buffer)
5778       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5779       ;; Allow the user to mangle the headers before parsing them.
5780       (gnus-run-hooks 'gnus-parse-headers-hook)
5781       (goto-char (point-min))
5782       (gnus-parse-without-error
5783         (while (and (or sequence allp)
5784                     (not (eobp)))
5785           (setq number (read cur))
5786           (when (not allp)
5787             (while (and sequence
5788                         (< (car sequence) number))
5789               (setq sequence (cdr sequence))))
5790           (when (and (or allp
5791                          (and sequence
5792                               (eq number (car sequence))))
5793                      (progn
5794                        (setq sequence (cdr sequence))
5795                        (setq header (inline
5796                                       (gnus-nov-parse-line
5797                                        number dependencies force-new)))))
5798             (push header headers))
5799           (forward-line 1)))
5800       ;; A common bug in inn is that if you have posted an article and
5801       ;; then retrieves the active file, it will answer correctly --
5802       ;; the new article is included.  However, a NOV entry for the
5803       ;; article may not have been generated yet, so this may fail.
5804       ;; We work around this problem by retrieving the last few
5805       ;; headers using HEAD.
5806       (if (or (not also-fetch-heads)
5807               (not sequence))
5808           ;; We (probably) got all the headers.
5809           (nreverse headers)
5810         (let ((gnus-nov-is-evil t))
5811           (nconc
5812            (nreverse headers)
5813            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5814              (gnus-get-newsgroup-headers))))))))
5815
5816 (defun gnus-article-get-xrefs ()
5817   "Fill in the Xref value in `gnus-current-headers', if necessary.
5818 This is meant to be called in `gnus-article-internal-prepare-hook'."
5819   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5820                                  gnus-current-headers)))
5821     (or (not gnus-use-cross-reference)
5822         (not headers)
5823         (and (mail-header-xref headers)
5824              (not (string= (mail-header-xref headers) "")))
5825         (let ((case-fold-search t)
5826               xref)
5827           (save-restriction
5828             (nnheader-narrow-to-headers)
5829             (goto-char (point-min))
5830             (when (or (and (not (eobp))
5831                            (eq (downcase (char-after)) ?x)
5832                            (looking-at "Xref:"))
5833                       (search-forward "\nXref:" nil t))
5834               (goto-char (1+ (match-end 0)))
5835               (setq xref (buffer-substring (point)
5836                                            (progn (end-of-line) (point))))
5837               (mail-header-set-xref headers xref)))))))
5838
5839 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5840   "Find article ID and insert the summary line for that article.
5841 OLD-HEADER can either be a header or a line number to insert
5842 the subject line on."
5843   (let* ((line (and (numberp old-header) old-header))
5844          (old-header (and (vectorp old-header) old-header))
5845          (header (cond ((and old-header use-old-header)
5846                         old-header)
5847                        ((and (numberp id)
5848                              (gnus-number-to-header id))
5849                         (gnus-number-to-header id))
5850                        (t
5851                         (gnus-read-header id))))
5852          (number (and (numberp id) id))
5853          d)
5854     (when header
5855       ;; Rebuild the thread that this article is part of and go to the
5856       ;; article we have fetched.
5857       (when (and (not gnus-show-threads)
5858                  old-header)
5859         (when (and number
5860                    (setq d (gnus-data-find (mail-header-number old-header))))
5861           (goto-char (gnus-data-pos d))
5862           (gnus-data-remove
5863            number
5864            (- (gnus-point-at-bol)
5865               (prog1
5866                   (1+ (gnus-point-at-eol))
5867                 (gnus-delete-line))))))
5868       (when old-header
5869         (mail-header-set-number header (mail-header-number old-header)))
5870       (setq gnus-newsgroup-sparse
5871             (delq (setq number (mail-header-number header))
5872                   gnus-newsgroup-sparse))
5873       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5874       (push number gnus-newsgroup-limit)
5875       (gnus-rebuild-thread (mail-header-id header) line)
5876       (gnus-summary-goto-subject number nil t))
5877     (when (and (numberp number)
5878                (> number 0))
5879       ;; We have to update the boundaries even if we can't fetch the
5880       ;; article if ID is a number -- so that the next `P' or `N'
5881       ;; command will fetch the previous (or next) article even
5882       ;; if the one we tried to fetch this time has been canceled.
5883       (when (> number gnus-newsgroup-end)
5884         (setq gnus-newsgroup-end number))
5885       (when (< number gnus-newsgroup-begin)
5886         (setq gnus-newsgroup-begin number))
5887       (setq gnus-newsgroup-unselected
5888             (delq number gnus-newsgroup-unselected)))
5889     ;; Report back a success?
5890     (and header (mail-header-number header))))
5891
5892 ;;; Process/prefix in the summary buffer
5893
5894 (defun gnus-summary-work-articles (n)
5895   "Return a list of articles to be worked upon.
5896 The prefix argument, the list of process marked articles, and the
5897 current article will be taken into consideration."
5898   (save-excursion
5899     (set-buffer gnus-summary-buffer)
5900     (cond
5901      (n
5902       ;; A numerical prefix has been given.
5903       (setq n (prefix-numeric-value n))
5904       (let ((backward (< n 0))
5905             (n (abs (prefix-numeric-value n)))
5906             articles article)
5907         (save-excursion
5908           (while
5909               (and (> n 0)
5910                    (push (setq article (gnus-summary-article-number))
5911                          articles)
5912                    (if backward
5913                        (gnus-summary-find-prev nil article)
5914                      (gnus-summary-find-next nil article)))
5915             (decf n)))
5916         (nreverse articles)))
5917      ((and (gnus-region-active-p) (mark))
5918       (message "region active")
5919       ;; Work on the region between point and mark.
5920       (let ((max (max (point) (mark)))
5921             articles article)
5922         (save-excursion
5923           (goto-char (min (min (point) (mark))))
5924           (while
5925               (and
5926                (push (setq article (gnus-summary-article-number)) articles)
5927                (gnus-summary-find-next nil article)
5928                (< (point) max)))
5929           (nreverse articles))))
5930      (gnus-newsgroup-processable
5931       ;; There are process-marked articles present.
5932       ;; Save current state.
5933       (gnus-summary-save-process-mark)
5934       ;; Return the list.
5935       (reverse gnus-newsgroup-processable))
5936      (t
5937       ;; Just return the current article.
5938       (list (gnus-summary-article-number))))))
5939
5940 (defmacro gnus-summary-iterate (arg &rest forms)
5941   "Iterate over the process/prefixed articles and do FORMS.
5942 ARG is the interactive prefix given to the command.  FORMS will be
5943 executed with point over the summary line of the articles."
5944   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5945     `(let ((,articles (gnus-summary-work-articles ,arg)))
5946        (while ,articles
5947          (gnus-summary-goto-subject (car ,articles))
5948          ,@forms
5949          (pop ,articles)))))
5950
5951 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5952 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5953
5954 (defun gnus-summary-save-process-mark ()
5955   "Push the current set of process marked articles on the stack."
5956   (interactive)
5957   (push (copy-sequence gnus-newsgroup-processable)
5958         gnus-newsgroup-process-stack))
5959
5960 (defun gnus-summary-kill-process-mark ()
5961   "Push the current set of process marked articles on the stack and unmark."
5962   (interactive)
5963   (gnus-summary-save-process-mark)
5964   (gnus-summary-unmark-all-processable))
5965
5966 (defun gnus-summary-yank-process-mark ()
5967   "Pop the last process mark state off the stack and restore it."
5968   (interactive)
5969   (unless gnus-newsgroup-process-stack
5970     (error "Empty mark stack"))
5971   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5972
5973 (defun gnus-summary-process-mark-set (set)
5974   "Make SET into the current process marked articles."
5975   (gnus-summary-unmark-all-processable)
5976   (while set
5977     (gnus-summary-set-process-mark (pop set))))
5978
5979 ;;; Searching and stuff
5980
5981 (defun gnus-summary-search-group (&optional backward use-level)
5982   "Search for next unread newsgroup.
5983 If optional argument BACKWARD is non-nil, search backward instead."
5984   (save-excursion
5985     (set-buffer gnus-group-buffer)
5986     (when (gnus-group-search-forward
5987            backward nil (if use-level (gnus-group-group-level) nil))
5988       (gnus-group-group-name))))
5989
5990 (defun gnus-summary-best-group (&optional exclude-group)
5991   "Find the name of the best unread group.
5992 If EXCLUDE-GROUP, do not go to this group."
5993   (save-excursion
5994     (set-buffer gnus-group-buffer)
5995     (save-excursion
5996       (gnus-group-best-unread-group exclude-group))))
5997
5998 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5999   (if backward (gnus-summary-find-prev)
6000     (let* ((dummy (gnus-summary-article-intangible-p))
6001            (article (or article (gnus-summary-article-number)))
6002            (arts (gnus-data-find-list article))
6003            result)
6004       (when (and (not dummy)
6005                  (or (not gnus-summary-check-current)
6006                      (not unread)
6007                      (not (gnus-data-unread-p (car arts)))))
6008         (setq arts (cdr arts)))
6009       (when (setq result
6010                   (if unread
6011                       (progn
6012                         (while arts
6013                           (when (or (and undownloaded
6014                                          (memq (car arts) 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       (push article gnus-newsgroup-replied)
9434       (let ((buffer-read-only nil))
9435         (when (gnus-summary-goto-subject article nil t)
9436           (gnus-summary-update-secondary-mark article))))))
9437
9438 (defun gnus-summary-mark-article-as-forwarded (article)
9439   "Mark ARTICLE as forwarded and update the summary line.
9440 ARTICLE can also be a list of articles."
9441   (let ((articles (if (listp article) article (list article))))
9442     (dolist (article articles)
9443       (push article gnus-newsgroup-forwarded)
9444       (let ((buffer-read-only nil))
9445         (when (gnus-summary-goto-subject article nil t)
9446           (gnus-summary-update-secondary-mark article))))))
9447
9448 (defun gnus-summary-set-bookmark (article)
9449   "Set a bookmark in current article."
9450   (interactive (list (gnus-summary-article-number)))
9451   (when (or (not (get-buffer gnus-article-buffer))
9452             (not gnus-current-article)
9453             (not gnus-article-current)
9454             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9455     (error "No current article selected"))
9456   ;; Remove old bookmark, if one exists.
9457   (gnus-pull article gnus-newsgroup-bookmarks)
9458   ;; Set the new bookmark, which is on the form
9459   ;; (article-number . line-number-in-body).
9460   (push
9461    (cons article
9462          (save-excursion
9463            (set-buffer gnus-article-buffer)
9464            (count-lines
9465             (min (point)
9466                  (save-excursion
9467                    (article-goto-body)
9468                    (point)))
9469             (point))))
9470    gnus-newsgroup-bookmarks)
9471   (gnus-message 6 "A bookmark has been added to the current article."))
9472
9473 (defun gnus-summary-remove-bookmark (article)
9474   "Remove the bookmark from the current article."
9475   (interactive (list (gnus-summary-article-number)))
9476   ;; Remove old bookmark, if one exists.
9477   (if (not (assq article gnus-newsgroup-bookmarks))
9478       (gnus-message 6 "No bookmark in current article.")
9479     (gnus-pull article gnus-newsgroup-bookmarks)
9480     (gnus-message 6 "Removed bookmark.")))
9481
9482 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9483 (defun gnus-summary-mark-as-dormant (n)
9484   "Mark N articles forward as dormant.
9485 If N is negative, mark backward instead.  The difference between N and
9486 the actual number of articles marked is returned."
9487   (interactive "p")
9488   (gnus-summary-mark-forward n gnus-dormant-mark))
9489
9490 (defun gnus-summary-set-process-mark (article)
9491   "Set the process mark on ARTICLE and update the summary line."
9492   (setq gnus-newsgroup-processable
9493         (cons article
9494               (delq article gnus-newsgroup-processable)))
9495   (when (gnus-summary-goto-subject article)
9496     (gnus-summary-show-thread)
9497     (gnus-summary-goto-subject article)
9498     (gnus-summary-update-secondary-mark article)))
9499
9500 (defun gnus-summary-remove-process-mark (article)
9501   "Remove the process mark from ARTICLE and update the summary line."
9502   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9503   (when (gnus-summary-goto-subject article)
9504     (gnus-summary-show-thread)
9505     (gnus-summary-goto-subject article)
9506     (gnus-summary-update-secondary-mark article)))
9507
9508 (defun gnus-summary-set-saved-mark (article)
9509   "Set the process mark on ARTICLE and update the summary line."
9510   (push article gnus-newsgroup-saved)
9511   (when (gnus-summary-goto-subject article)
9512     (gnus-summary-update-secondary-mark article)))
9513
9514 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9515   "Mark N articles as read forwards.
9516 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9517 The difference between N and the actual number of articles marked is
9518 returned.
9519 If NO-EXPIRE, auto-expiry will be inhibited."
9520   (interactive "p")
9521   (gnus-summary-show-thread)
9522   (let ((backward (< n 0))
9523         (gnus-summary-goto-unread
9524          (and gnus-summary-goto-unread
9525               (not (eq gnus-summary-goto-unread 'never))
9526               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9527                                     gnus-ticked-mark gnus-dormant-mark)))))
9528         (n (abs n))
9529         (mark (or mark gnus-del-mark)))
9530     (while (and (> n 0)
9531                 (gnus-summary-mark-article nil mark no-expire)
9532                 (zerop (gnus-summary-next-subject
9533                         (if backward -1 1)
9534                         (and gnus-summary-goto-unread
9535                              (not (eq gnus-summary-goto-unread 'never)))
9536                         t)))
9537       (setq n (1- n)))
9538     (when (/= 0 n)
9539       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9540     (gnus-summary-recenter)
9541     (gnus-summary-position-point)
9542     (gnus-set-mode-line 'summary)
9543     n))
9544
9545 (defun gnus-summary-mark-article-as-read (mark)
9546   "Mark the current article quickly as read with MARK."
9547   (let ((article (gnus-summary-article-number)))
9548     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9549     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9550     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9551     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9552     (push (cons article mark) gnus-newsgroup-reads)
9553     ;; Possibly remove from cache, if that is used.
9554     (when gnus-use-cache
9555       (gnus-cache-enter-remove-article article))
9556     ;; Allow the backend to change the mark.
9557     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9558     ;; Check for auto-expiry.
9559     (when (and gnus-newsgroup-auto-expire
9560                (memq mark gnus-auto-expirable-marks))
9561       (setq mark gnus-expirable-mark)
9562       ;; Let the backend know about the mark change.
9563       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9564       (push article gnus-newsgroup-expirable))
9565     ;; Set the mark in the buffer.
9566     (gnus-summary-update-mark mark 'unread)
9567     t))
9568
9569 (defun gnus-summary-mark-article-as-unread (mark)
9570   "Mark the current article quickly as unread with MARK."
9571   (let* ((article (gnus-summary-article-number))
9572          (old-mark (gnus-summary-article-mark article)))
9573     ;; Allow the backend to change the mark.
9574     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9575     (if (eq mark old-mark)
9576         t
9577       (if (<= article 0)
9578           (progn
9579             (gnus-error 1 "Can't mark negative article numbers")
9580             nil)
9581         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9582         (setq gnus-newsgroup-spam-marked
9583               (delq article gnus-newsgroup-spam-marked))
9584         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9585         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9586         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9587         (cond ((= mark gnus-ticked-mark)
9588                (setq gnus-newsgroup-marked
9589                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9590                                               article)))
9591               ((= mark gnus-spam-mark)
9592                (setq gnus-newsgroup-spam-marked
9593                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9594                                               article)))
9595               ((= mark gnus-dormant-mark)
9596                (setq gnus-newsgroup-dormant
9597                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9598                                               article)))
9599               (t
9600                (setq gnus-newsgroup-unreads
9601                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9602                                               article))))
9603         (gnus-pull article gnus-newsgroup-reads)
9604
9605         ;; See whether the article is to be put in the cache.
9606         (and gnus-use-cache
9607              (vectorp (gnus-summary-article-header article))
9608              (save-excursion
9609                (gnus-cache-possibly-enter-article
9610                 gnus-newsgroup-name article
9611                 (= mark gnus-ticked-mark)
9612                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9613
9614         ;; Fix the mark.
9615         (gnus-summary-update-mark mark 'unread)
9616         t))))
9617
9618 (defun gnus-summary-mark-article (&optional article mark no-expire)
9619   "Mark ARTICLE with MARK.  MARK can be any character.
9620 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9621 `??' (dormant) and `?E' (expirable).
9622 If MARK is nil, then the default character `?r' is used.
9623 If ARTICLE is nil, then the article on the current line will be
9624 marked.
9625 If NO-EXPIRE, auto-expiry will be inhibited."
9626   ;; The mark might be a string.
9627   (when (stringp mark)
9628     (setq mark (aref mark 0)))
9629   ;; If no mark is given, then we check auto-expiring.
9630   (when (null mark)
9631     (setq mark gnus-del-mark))
9632   (when (and (not no-expire)
9633              gnus-newsgroup-auto-expire
9634              (memq mark gnus-auto-expirable-marks))
9635     (setq mark gnus-expirable-mark))
9636   (let ((article (or article (gnus-summary-article-number)))
9637         (old-mark (gnus-summary-article-mark article)))
9638     ;; Allow the backend to change the mark.
9639     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9640     (if (eq mark old-mark)
9641         t
9642       (unless article
9643         (error "No article on current line"))
9644       (if (not (if (or (= mark gnus-unread-mark)
9645                        (= mark gnus-ticked-mark)
9646                        (= mark gnus-spam-mark)
9647                        (= mark gnus-dormant-mark))
9648                    (gnus-mark-article-as-unread article mark)
9649                  (gnus-mark-article-as-read article mark)))
9650           t
9651         ;; See whether the article is to be put in the cache.
9652         (and gnus-use-cache
9653              (not (= mark gnus-canceled-mark))
9654              (vectorp (gnus-summary-article-header article))
9655              (save-excursion
9656                (gnus-cache-possibly-enter-article
9657                 gnus-newsgroup-name article
9658                 (= mark gnus-ticked-mark)
9659                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9660
9661         (when (gnus-summary-goto-subject article nil t)
9662           (let ((buffer-read-only nil))
9663             (gnus-summary-show-thread)
9664             ;; Fix the mark.
9665             (gnus-summary-update-mark mark 'unread)
9666             t))))))
9667
9668 (defun gnus-summary-update-secondary-mark (article)
9669   "Update the secondary (read, process, cache) mark."
9670   (gnus-summary-update-mark
9671    (cond ((memq article gnus-newsgroup-processable)
9672           gnus-process-mark)
9673          ((memq article gnus-newsgroup-cached)
9674           gnus-cached-mark)
9675          ((memq article gnus-newsgroup-replied)
9676           gnus-replied-mark)
9677          ((memq article gnus-newsgroup-forwarded)
9678           gnus-forwarded-mark)
9679          ((memq article gnus-newsgroup-saved)
9680           gnus-saved-mark)
9681          ((memq article gnus-newsgroup-recent)
9682           gnus-recent-mark)
9683          ((memq article gnus-newsgroup-unseen)
9684           gnus-unseen-mark)
9685          (t gnus-no-mark))
9686    'replied)
9687   (when (gnus-visual-p 'summary-highlight 'highlight)
9688     (gnus-run-hooks 'gnus-summary-update-hook))
9689   t)
9690
9691 (defun gnus-summary-update-download-mark (article)
9692   "Update the secondary (read, process, cache) mark."
9693   (gnus-summary-update-mark
9694    (cond ((memq article gnus-newsgroup-undownloaded) 
9695           gnus-undownloaded-mark)
9696          (gnus-newsgroup-agentized
9697           gnus-downloaded-mark)
9698          (t
9699           gnus-no-mark))
9700    'download)
9701   (gnus-summary-update-line t)
9702   t)
9703
9704 (defun gnus-summary-update-mark (mark type)
9705   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9706         (buffer-read-only nil))
9707     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9708     (when forward
9709       (when (looking-at "\r")
9710         (incf forward))
9711       (when (<= (+ forward (point)) (point-max))
9712         ;; Go to the right position on the line.
9713         (goto-char (+ forward (point)))
9714         ;; Replace the old mark with the new mark.
9715         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9716         ;; Optionally update the marks by some user rule.
9717         (when (eq type 'unread)
9718           (gnus-data-set-mark
9719            (gnus-data-find (gnus-summary-article-number)) mark)
9720           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9721
9722 (defun gnus-mark-article-as-read (article &optional mark)
9723   "Enter ARTICLE in the pertinent lists and remove it from others."
9724   ;; Make the article expirable.
9725   (let ((mark (or mark gnus-del-mark)))
9726     (setq gnus-newsgroup-expirable
9727           (if (= mark gnus-expirable-mark)
9728               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9729             (delq article gnus-newsgroup-expirable)))
9730     ;; Remove from unread and marked lists.
9731     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9732     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9733     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9734     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9735     (push (cons article mark) gnus-newsgroup-reads)
9736     ;; Possibly remove from cache, if that is used.
9737     (when gnus-use-cache
9738       (gnus-cache-enter-remove-article article))
9739     t))
9740
9741 (defun gnus-mark-article-as-unread (article &optional mark)
9742   "Enter ARTICLE in the pertinent lists and remove it from others."
9743   (let ((mark (or mark gnus-ticked-mark)))
9744     (if (<= article 0)
9745         (progn
9746           (gnus-error 1 "Can't mark negative article numbers")
9747           nil)
9748       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9749             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9750             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9751             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9752             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9753
9754       ;; Unsuppress duplicates?
9755       (when gnus-suppress-duplicates
9756         (gnus-dup-unsuppress-article article))
9757
9758       (cond ((= mark gnus-ticked-mark)
9759              (setq gnus-newsgroup-marked
9760                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9761             ((= mark gnus-spam-mark)
9762              (setq gnus-newsgroup-spam-marked
9763                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9764                                             article)))
9765             ((= mark gnus-dormant-mark)
9766              (setq gnus-newsgroup-dormant
9767                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9768             (t
9769              (setq gnus-newsgroup-unreads
9770                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9771       (gnus-pull article gnus-newsgroup-reads)
9772       t)))
9773
9774 (defalias 'gnus-summary-mark-as-unread-forward
9775   'gnus-summary-tick-article-forward)
9776 (make-obsolete 'gnus-summary-mark-as-unread-forward
9777                'gnus-summary-tick-article-forward)
9778 (defun gnus-summary-tick-article-forward (n)
9779   "Tick N articles forwards.
9780 If N is negative, tick backwards instead.
9781 The difference between N and the number of articles ticked is returned."
9782   (interactive "p")
9783   (gnus-summary-mark-forward n gnus-ticked-mark))
9784
9785 (defalias 'gnus-summary-mark-as-unread-backward
9786   'gnus-summary-tick-article-backward)
9787 (make-obsolete 'gnus-summary-mark-as-unread-backward
9788                'gnus-summary-tick-article-backward)
9789 (defun gnus-summary-tick-article-backward (n)
9790   "Tick N articles backwards.
9791 The difference between N and the number of articles ticked is returned."
9792   (interactive "p")
9793   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9794
9795 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9796 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9797 (defun gnus-summary-tick-article (&optional article clear-mark)
9798   "Mark current article as unread.
9799 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9800 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9801   (interactive)
9802   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9803                                        gnus-ticked-mark)))
9804
9805 (defun gnus-summary-mark-as-read-forward (n)
9806   "Mark N articles as read forwards.
9807 If N is negative, mark backwards instead.
9808 The difference between N and the actual number of articles marked is
9809 returned."
9810   (interactive "p")
9811   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9812
9813 (defun gnus-summary-mark-as-read-backward (n)
9814   "Mark the N articles as read backwards.
9815 The difference between N and the actual number of articles marked is
9816 returned."
9817   (interactive "p")
9818   (gnus-summary-mark-forward
9819    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9820
9821 (defun gnus-summary-mark-as-read (&optional article mark)
9822   "Mark current article as read.
9823 ARTICLE specifies the article to be marked as read.
9824 MARK specifies a string to be inserted at the beginning of the line."
9825   (gnus-summary-mark-article article mark))
9826
9827 (defun gnus-summary-clear-mark-forward (n)
9828   "Clear marks from N articles forward.
9829 If N is negative, clear backward instead.
9830 The difference between N and the number of marks cleared is returned."
9831   (interactive "p")
9832   (gnus-summary-mark-forward n gnus-unread-mark))
9833
9834 (defun gnus-summary-clear-mark-backward (n)
9835   "Clear marks from N articles backward.
9836 The difference between N and the number of marks cleared is returned."
9837   (interactive "p")
9838   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9839
9840 (defun gnus-summary-mark-unread-as-read ()
9841   "Intended to be used by `gnus-summary-mark-article-hook'."
9842   (when (memq gnus-current-article gnus-newsgroup-unreads)
9843     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9844
9845 (defun gnus-summary-mark-read-and-unread-as-read ()
9846   "Intended to be used by `gnus-summary-mark-article-hook'."
9847   (let ((mark (gnus-summary-article-mark)))
9848     (when (or (gnus-unread-mark-p mark)
9849               (gnus-read-mark-p mark))
9850       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9851
9852 (defun gnus-summary-mark-unread-as-ticked ()
9853   "Intended to be used by `gnus-summary-mark-article-hook'."
9854   (when (memq gnus-current-article gnus-newsgroup-unreads)
9855     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9856
9857 (defun gnus-summary-mark-region-as-read (point mark all)
9858   "Mark all unread articles between point and mark as read.
9859 If given a prefix, mark all articles between point and mark as read,
9860 even ticked and dormant ones."
9861   (interactive "r\nP")
9862   (save-excursion
9863     (let (article)
9864       (goto-char point)
9865       (beginning-of-line)
9866       (while (and
9867               (< (point) mark)
9868               (progn
9869                 (when (or all
9870                           (memq (setq article (gnus-summary-article-number))
9871                                 gnus-newsgroup-unreads))
9872                   (gnus-summary-mark-article article gnus-del-mark))
9873                 t)
9874               (gnus-summary-find-next))))))
9875
9876 (defun gnus-summary-mark-below (score mark)
9877   "Mark articles with score less than SCORE with MARK."
9878   (interactive "P\ncMark: ")
9879   (setq score (if score
9880                   (prefix-numeric-value score)
9881                 (or gnus-summary-default-score 0)))
9882   (save-excursion
9883     (set-buffer gnus-summary-buffer)
9884     (goto-char (point-min))
9885     (while
9886         (progn
9887           (and (< (gnus-summary-article-score) score)
9888                (gnus-summary-mark-article nil mark))
9889           (gnus-summary-find-next)))))
9890
9891 (defun gnus-summary-kill-below (&optional score)
9892   "Mark articles with score below SCORE as read."
9893   (interactive "P")
9894   (gnus-summary-mark-below score gnus-killed-mark))
9895
9896 (defun gnus-summary-clear-above (&optional score)
9897   "Clear all marks from articles with score above SCORE."
9898   (interactive "P")
9899   (gnus-summary-mark-above score gnus-unread-mark))
9900
9901 (defun gnus-summary-tick-above (&optional score)
9902   "Tick all articles with score above SCORE."
9903   (interactive "P")
9904   (gnus-summary-mark-above score gnus-ticked-mark))
9905
9906 (defun gnus-summary-mark-above (score mark)
9907   "Mark articles with score over SCORE with MARK."
9908   (interactive "P\ncMark: ")
9909   (setq score (if score
9910                   (prefix-numeric-value score)
9911                 (or gnus-summary-default-score 0)))
9912   (save-excursion
9913     (set-buffer gnus-summary-buffer)
9914     (goto-char (point-min))
9915     (while (and (progn
9916                   (when (> (gnus-summary-article-score) score)
9917                     (gnus-summary-mark-article nil mark))
9918                   t)
9919                 (gnus-summary-find-next)))))
9920
9921 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9922 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9923 (defun gnus-summary-limit-include-expunged (&optional no-error)
9924   "Display all the hidden articles that were expunged for low scores."
9925   (interactive)
9926   (let ((buffer-read-only nil))
9927     (let ((scored gnus-newsgroup-scored)
9928           headers h)
9929       (while scored
9930         (unless (gnus-summary-article-header (caar scored))
9931           (and (setq h (gnus-number-to-header (caar scored)))
9932                (< (cdar scored) gnus-summary-expunge-below)
9933                (push h headers)))
9934         (setq scored (cdr scored)))
9935       (if (not headers)
9936           (when (not no-error)
9937             (error "No expunged articles hidden"))
9938         (goto-char (point-min))
9939         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9940         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9941         (mapcar (lambda (x) (push (mail-header-number x)
9942                                   gnus-newsgroup-limit))
9943                 headers)
9944         (gnus-summary-prepare-unthreaded (nreverse headers))
9945         (goto-char (point-min))
9946         (gnus-summary-position-point)
9947         t))))
9948
9949 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9950   "Mark all unread articles in this newsgroup as read.
9951 If prefix argument ALL is non-nil, ticked and dormant articles will
9952 also be marked as read.
9953 If QUIETLY is non-nil, no questions will be asked.
9954 If TO-HERE is non-nil, it should be a point in the buffer.  All
9955 articles before (after, if REVERSE is set) this point will be marked as read.
9956 Note that this function will only catch up the unread article
9957 in the current summary buffer limitation.
9958 The number of articles marked as read is returned."
9959   (interactive "P")
9960   (prog1
9961       (save-excursion
9962         (when (or quietly
9963                   (not gnus-interactive-catchup) ;Without confirmation?
9964                   gnus-expert-user
9965                   (gnus-y-or-n-p
9966                    (if all
9967                        "Mark absolutely all articles as read? "
9968                      "Mark all unread articles as read? ")))
9969           (if (and not-mark
9970                    (not gnus-newsgroup-adaptive)
9971                    (not gnus-newsgroup-auto-expire)
9972                    (not gnus-suppress-duplicates)
9973                    (or (not gnus-use-cache)
9974                        (eq gnus-use-cache 'passive)))
9975               (progn
9976                 (when all
9977                   (setq gnus-newsgroup-marked nil
9978                         gnus-newsgroup-spam-marked nil
9979                         gnus-newsgroup-dormant nil))
9980                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9981             ;; We actually mark all articles as canceled, which we
9982             ;; have to do when using auto-expiry or adaptive scoring.
9983             (gnus-summary-show-all-threads)
9984             (if (and to-here reverse)
9985                 (progn
9986                   (goto-char to-here)
9987                   (while (and
9988                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9989                           (gnus-summary-find-next (not all) nil nil t))))
9990               (when (gnus-summary-first-subject (not all) t)
9991                 (while (and
9992                         (if to-here (< (point) to-here) t)
9993                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9994                         (gnus-summary-find-next (not all) nil nil t)))))
9995             (gnus-set-mode-line 'summary))
9996           t))
9997     (gnus-summary-position-point)))
9998
9999 (defun gnus-summary-catchup-to-here (&optional all)
10000   "Mark all unticked articles before the current one as read.
10001 If ALL is non-nil, also mark ticked and dormant articles as read."
10002   (interactive "P")
10003   (save-excursion
10004     (gnus-save-hidden-threads
10005       (let ((beg (point)))
10006         ;; We check that there are unread articles.
10007         (when (or all (gnus-summary-find-prev))
10008           (gnus-summary-catchup all t beg)))))
10009   (gnus-summary-position-point))
10010
10011 (defun gnus-summary-catchup-from-here (&optional all)
10012   "Mark all unticked articles after the current one as read.
10013 If ALL is non-nil, also mark ticked and dormant articles as read."
10014   (interactive "P")
10015   (save-excursion
10016     (gnus-save-hidden-threads
10017       (let ((beg (point)))
10018         ;; We check that there are unread articles.
10019         (when (or all (gnus-summary-find-next))
10020           (gnus-summary-catchup all t beg nil t)))))
10021
10022   (gnus-summary-position-point))
10023 (defun gnus-summary-catchup-all (&optional quietly)
10024   "Mark all articles in this newsgroup as read.
10025 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10026 instead, which marks only unread articles as read."
10027   (interactive "P")
10028   (gnus-summary-catchup t quietly))
10029
10030 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10031   "Mark all unread articles in this group as read, then exit.
10032 If prefix argument ALL is non-nil, all articles are marked as read.
10033 If QUIETLY is non-nil, no questions will be asked."
10034   (interactive "P")
10035   (when (gnus-summary-catchup all quietly nil 'fast)
10036     ;; Select next newsgroup or exit.
10037     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10038              (eq gnus-auto-select-next 'quietly))
10039         (gnus-summary-next-group nil)
10040       (gnus-summary-exit))))
10041
10042 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10043   "Mark all articles in this newsgroup as read, and then exit.
10044 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10045 instead, which marks only unread articles as read."
10046   (interactive "P")
10047   (gnus-summary-catchup-and-exit t quietly))
10048
10049 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10050   "Mark all articles in this group as read and select the next group.
10051 If given a prefix, mark all articles, unread as well as ticked, as
10052 read."
10053   (interactive "P")
10054   (save-excursion
10055     (gnus-summary-catchup all))
10056   (gnus-summary-next-group))
10057
10058 ;;;
10059 ;;; with article
10060 ;;;
10061
10062 (defmacro gnus-with-article (article &rest forms)
10063   "Select ARTICLE and perform FORMS in the original article buffer.
10064 Then replace the article with the result."
10065   `(progn
10066      ;; We don't want the article to be marked as read.
10067      (let (gnus-mark-article-hook)
10068        (gnus-summary-select-article t t nil ,article))
10069      (set-buffer gnus-original-article-buffer)
10070      ,@forms
10071      (if (not (gnus-check-backend-function
10072                'request-replace-article (car gnus-article-current)))
10073          (gnus-message 5 "Read-only group; not replacing")
10074        (unless (gnus-request-replace-article
10075                 ,article (car gnus-article-current)
10076                 (current-buffer) t)
10077          (error "Couldn't replace article")))
10078      ;; The cache and backlog have to be flushed somewhat.
10079      (when gnus-keep-backlog
10080        (gnus-backlog-remove-article
10081         (car gnus-article-current) (cdr gnus-article-current)))
10082      (when gnus-use-cache
10083        (gnus-cache-update-article
10084         (car gnus-article-current) (cdr gnus-article-current)))))
10085
10086 (put 'gnus-with-article 'lisp-indent-function 1)
10087 (put 'gnus-with-article 'edebug-form-spec '(form body))
10088
10089 ;; Thread-based commands.
10090
10091 (defun gnus-summary-articles-in-thread (&optional article)
10092   "Return a list of all articles in the current thread.
10093 If ARTICLE is non-nil, return all articles in the thread that starts
10094 with that article."
10095   (let* ((article (or article (gnus-summary-article-number)))
10096          (data (gnus-data-find-list article))
10097          (top-level (gnus-data-level (car data)))
10098          (top-subject
10099           (cond ((null gnus-thread-operation-ignore-subject)
10100                  (gnus-simplify-subject-re
10101                   (mail-header-subject (gnus-data-header (car data)))))
10102                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10103                  (gnus-simplify-subject-fuzzy
10104                   (mail-header-subject (gnus-data-header (car data)))))
10105                 (t nil)))
10106          (end-point (save-excursion
10107                       (if (gnus-summary-go-to-next-thread)
10108                           (point) (point-max))))
10109          articles)
10110     (while (and data
10111                 (< (gnus-data-pos (car data)) end-point))
10112       (when (or (not top-subject)
10113                 (string= top-subject
10114                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10115                              (gnus-simplify-subject-fuzzy
10116                               (mail-header-subject
10117                                (gnus-data-header (car data))))
10118                            (gnus-simplify-subject-re
10119                             (mail-header-subject
10120                              (gnus-data-header (car data)))))))
10121         (push (gnus-data-number (car data)) articles))
10122       (unless (and (setq data (cdr data))
10123                    (> (gnus-data-level (car data)) top-level))
10124         (setq data nil)))
10125     ;; Return the list of articles.
10126     (nreverse articles)))
10127
10128 (defun gnus-summary-rethread-current ()
10129   "Rethread the thread the current article is part of."
10130   (interactive)
10131   (let* ((gnus-show-threads t)
10132          (article (gnus-summary-article-number))
10133          (id (mail-header-id (gnus-summary-article-header)))
10134          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10135     (unless id
10136       (error "No article on the current line"))
10137     (gnus-rebuild-thread id)
10138     (gnus-summary-goto-subject article)))
10139
10140 (defun gnus-summary-reparent-thread ()
10141   "Make the current article child of the marked (or previous) article.
10142
10143 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10144 is non-nil or the Subject: of both articles are the same."
10145   (interactive)
10146   (unless (not (gnus-group-read-only-p))
10147     (error "The current newsgroup does not support article editing"))
10148   (unless (<= (length gnus-newsgroup-processable) 1)
10149     (error "No more than one article may be marked"))
10150   (save-window-excursion
10151     (let ((gnus-article-buffer " *reparent*")
10152           (current-article (gnus-summary-article-number))
10153           ;; First grab the marked article, otherwise one line up.
10154           (parent-article (if (not (null gnus-newsgroup-processable))
10155                               (car gnus-newsgroup-processable)
10156                             (save-excursion
10157                               (if (eq (forward-line -1) 0)
10158                                   (gnus-summary-article-number)
10159                                 (error "Beginning of summary buffer"))))))
10160       (unless (not (eq current-article parent-article))
10161         (error "An article may not be self-referential"))
10162       (let ((message-id (mail-header-id
10163                          (gnus-summary-article-header parent-article))))
10164         (unless (and message-id (not (equal message-id "")))
10165           (error "No message-id in desired parent"))
10166         (gnus-with-article current-article
10167           (save-restriction
10168             (goto-char (point-min))
10169             (message-narrow-to-head)
10170             (if (re-search-forward "^References: " nil t)
10171                 (progn
10172                   (re-search-forward "^[^ \t]" nil t)
10173                   (forward-line -1)
10174                   (end-of-line)
10175                   (insert " " message-id))
10176               (insert "References: " message-id "\n"))))
10177         (set-buffer gnus-summary-buffer)
10178         (gnus-summary-unmark-all-processable)
10179         (gnus-summary-update-article current-article)
10180         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10181             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10182         (gnus-summary-rethread-current)
10183         (gnus-message 3 "Article %d is now the child of article %d"
10184                       current-article parent-article)))))
10185
10186 (defun gnus-summary-toggle-threads (&optional arg)
10187   "Toggle showing conversation threads.
10188 If ARG is positive number, turn showing conversation threads on."
10189   (interactive "P")
10190   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10191     (setq gnus-show-threads
10192           (if (null arg) (not gnus-show-threads)
10193             (> (prefix-numeric-value arg) 0)))
10194     (gnus-summary-prepare)
10195     (gnus-summary-goto-subject current)
10196     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10197     (gnus-summary-position-point)))
10198
10199 (defun gnus-summary-show-all-threads ()
10200   "Show all threads."
10201   (interactive)
10202   (save-excursion
10203     (let ((buffer-read-only nil))
10204       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10205   (gnus-summary-position-point))
10206
10207 (defun gnus-summary-show-thread ()
10208   "Show thread subtrees.
10209 Returns nil if no thread was there to be shown."
10210   (interactive)
10211   (let ((buffer-read-only nil)
10212         (orig (point))
10213         ;; first goto end then to beg, to have point at beg after let
10214         (end (progn (end-of-line) (point)))
10215         (beg (progn (beginning-of-line) (point))))
10216     (prog1
10217         ;; Any hidden lines here?
10218         (search-forward "\r" end t)
10219       (subst-char-in-region beg end ?\^M ?\n t)
10220       (goto-char orig)
10221       (gnus-summary-position-point))))
10222
10223 (defun gnus-summary-maybe-hide-threads ()
10224   "If requested, hide the threads that should be hidden."
10225   (when (and gnus-show-threads
10226              gnus-thread-hide-subtree)
10227     (gnus-summary-hide-all-threads
10228      (if (or (consp gnus-thread-hide-subtree)
10229              (gnus-functionp gnus-thread-hide-subtree))
10230          (gnus-make-predicate gnus-thread-hide-subtree)
10231        nil))))
10232
10233 ;;; Hiding predicates.
10234
10235 (defun gnus-article-unread-p (header)
10236   (memq (mail-header-number header) gnus-newsgroup-unreads))
10237
10238 (defun gnus-article-unseen-p (header)
10239   (memq (mail-header-number header) gnus-newsgroup-unseen))
10240
10241 (defun gnus-map-articles (predicate articles)
10242   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10243   (apply 'gnus-or (mapcar predicate
10244                           (mapcar 'gnus-summary-article-header articles))))
10245
10246 (defun gnus-summary-hide-all-threads (&optional predicate)
10247   "Hide all thread subtrees.
10248 If PREDICATE is supplied, threads that satisfy this predicate
10249 will not be hidden."
10250   (interactive)
10251   (save-excursion
10252     (goto-char (point-min))
10253     (let ((end nil))
10254       (while (not end)
10255         (when (or (not predicate)
10256                   (gnus-map-articles
10257                    predicate (gnus-summary-article-children)))
10258             (gnus-summary-hide-thread))
10259         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10260   (gnus-summary-position-point))
10261
10262 (defun gnus-summary-hide-thread ()
10263   "Hide thread subtrees.
10264 If PREDICATE is supplied, threads that satisfy this predicate
10265 will not be hidden.
10266 Returns nil if no threads were there to be hidden."
10267   (interactive)
10268   (let ((buffer-read-only nil)
10269         (start (point))
10270         (article (gnus-summary-article-number)))
10271     (goto-char start)
10272     ;; Go forward until either the buffer ends or the subthread
10273     ;; ends.
10274     (when (and (not (eobp))
10275                (or (zerop (gnus-summary-next-thread 1 t))
10276                    (goto-char (point-max))))
10277       (prog1
10278           (if (and (> (point) start)
10279                    (search-backward "\n" start t))
10280               (progn
10281                 (subst-char-in-region start (point) ?\n ?\^M)
10282                 (gnus-summary-goto-subject article))
10283             (goto-char start)
10284             nil)))))
10285
10286 (defun gnus-summary-go-to-next-thread (&optional previous)
10287   "Go to the same level (or less) next thread.
10288 If PREVIOUS is non-nil, go to previous thread instead.
10289 Return the article number moved to, or nil if moving was impossible."
10290   (let ((level (gnus-summary-thread-level))
10291         (way (if previous -1 1))
10292         (beg (point)))
10293     (forward-line way)
10294     (while (and (not (eobp))
10295                 (< level (gnus-summary-thread-level)))
10296       (forward-line way))
10297     (if (eobp)
10298         (progn
10299           (goto-char beg)
10300           nil)
10301       (setq beg (point))
10302       (prog1
10303           (gnus-summary-article-number)
10304         (goto-char beg)))))
10305
10306 (defun gnus-summary-next-thread (n &optional silent)
10307   "Go to the same level next N'th thread.
10308 If N is negative, search backward instead.
10309 Returns the difference between N and the number of skips actually
10310 done.
10311
10312 If SILENT, don't output messages."
10313   (interactive "p")
10314   (let ((backward (< n 0))
10315         (n (abs n)))
10316     (while (and (> n 0)
10317                 (gnus-summary-go-to-next-thread backward))
10318       (decf n))
10319     (unless silent
10320       (gnus-summary-position-point))
10321     (when (and (not silent) (/= 0 n))
10322       (gnus-message 7 "No more threads"))
10323     n))
10324
10325 (defun gnus-summary-prev-thread (n)
10326   "Go to the same level previous N'th thread.
10327 Returns the difference between N and the number of skips actually
10328 done."
10329   (interactive "p")
10330   (gnus-summary-next-thread (- n)))
10331
10332 (defun gnus-summary-go-down-thread ()
10333   "Go down one level in the current thread."
10334   (let ((children (gnus-summary-article-children)))
10335     (when children
10336       (gnus-summary-goto-subject (car children)))))
10337
10338 (defun gnus-summary-go-up-thread ()
10339   "Go up one level in the current thread."
10340   (let ((parent (gnus-summary-article-parent)))
10341     (when parent
10342       (gnus-summary-goto-subject parent))))
10343
10344 (defun gnus-summary-down-thread (n)
10345   "Go down thread N steps.
10346 If N is negative, go up instead.
10347 Returns the difference between N and how many steps down that were
10348 taken."
10349   (interactive "p")
10350   (let ((up (< n 0))
10351         (n (abs n)))
10352     (while (and (> n 0)
10353                 (if up (gnus-summary-go-up-thread)
10354                   (gnus-summary-go-down-thread)))
10355       (setq n (1- n)))
10356     (gnus-summary-position-point)
10357     (when (/= 0 n)
10358       (gnus-message 7 "Can't go further"))
10359     n))
10360
10361 (defun gnus-summary-up-thread (n)
10362   "Go up thread N steps.
10363 If N is negative, go down instead.
10364 Returns the difference between N and how many steps down that were
10365 taken."
10366   (interactive "p")
10367   (gnus-summary-down-thread (- n)))
10368
10369 (defun gnus-summary-top-thread ()
10370   "Go to the top of the thread."
10371   (interactive)
10372   (while (gnus-summary-go-up-thread))
10373   (gnus-summary-article-number))
10374
10375 (defun gnus-summary-kill-thread (&optional unmark)
10376   "Mark articles under current thread as read.
10377 If the prefix argument is positive, remove any kinds of marks.
10378 If the prefix argument is negative, tick articles instead."
10379   (interactive "P")
10380   (when unmark
10381     (setq unmark (prefix-numeric-value unmark)))
10382   (let ((articles (gnus-summary-articles-in-thread)))
10383     (save-excursion
10384       ;; Expand the thread.
10385       (gnus-summary-show-thread)
10386       ;; Mark all the articles.
10387       (while articles
10388         (gnus-summary-goto-subject (car articles))
10389         (cond ((null unmark)
10390                (gnus-summary-mark-article-as-read gnus-killed-mark))
10391               ((> unmark 0)
10392                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10393               (t
10394                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10395         (setq articles (cdr articles))))
10396     ;; Hide killed subtrees.
10397     (and (null unmark)
10398          gnus-thread-hide-killed
10399          (gnus-summary-hide-thread))
10400     ;; If marked as read, go to next unread subject.
10401     (when (null unmark)
10402       ;; Go to next unread subject.
10403       (gnus-summary-next-subject 1 t)))
10404   (gnus-set-mode-line 'summary))
10405
10406 ;; Summary sorting commands
10407
10408 (defun gnus-summary-sort-by-number (&optional reverse)
10409   "Sort the summary buffer by article number.
10410 Argument REVERSE means reverse order."
10411   (interactive "P")
10412   (gnus-summary-sort 'number reverse))
10413
10414 (defun gnus-summary-sort-by-random (&optional reverse)
10415   "Randomize the order in the summary buffer.
10416 Argument REVERSE means to randomize in reverse order."
10417   (interactive "P")
10418   (gnus-summary-sort 'random reverse))
10419
10420 (defun gnus-summary-sort-by-author (&optional reverse)
10421   "Sort the summary buffer by author name alphabetically.
10422 If `case-fold-search' is non-nil, case of letters is ignored.
10423 Argument REVERSE means reverse order."
10424   (interactive "P")
10425   (gnus-summary-sort 'author reverse))
10426
10427 (defun gnus-summary-sort-by-subject (&optional reverse)
10428   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10429 If `case-fold-search' is non-nil, case of letters is ignored.
10430 Argument REVERSE means reverse order."
10431   (interactive "P")
10432   (gnus-summary-sort 'subject reverse))
10433
10434 (defun gnus-summary-sort-by-date (&optional reverse)
10435   "Sort the summary buffer by date.
10436 Argument REVERSE means reverse order."
10437   (interactive "P")
10438   (gnus-summary-sort 'date reverse))
10439
10440 (defun gnus-summary-sort-by-score (&optional reverse)
10441   "Sort the summary buffer by score.
10442 Argument REVERSE means reverse order."
10443   (interactive "P")
10444   (gnus-summary-sort 'score reverse))
10445
10446 (defun gnus-summary-sort-by-lines (&optional reverse)
10447   "Sort the summary buffer by the number of lines.
10448 Argument REVERSE means reverse order."
10449   (interactive "P")
10450   (gnus-summary-sort 'lines reverse))
10451
10452 (defun gnus-summary-sort-by-chars (&optional reverse)
10453   "Sort the summary buffer by article length.
10454 Argument REVERSE means reverse order."
10455   (interactive "P")
10456   (gnus-summary-sort 'chars reverse))
10457
10458 (defun gnus-summary-sort-by-original (&optional reverse)
10459   "Sort the summary buffer using the default sorting method.
10460 Argument REVERSE means reverse order."
10461   (interactive "P")
10462   (let* ((buffer-read-only)
10463          (gnus-summary-prepare-hook nil))
10464     ;; We do the sorting by regenerating the threads.
10465     (gnus-summary-prepare)
10466     ;; Hide subthreads if needed.
10467     (gnus-summary-maybe-hide-threads)))
10468
10469 (defun gnus-summary-sort (predicate reverse)
10470   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10471   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10472          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10473          (gnus-thread-sort-functions
10474           (if (not reverse)
10475               thread
10476             `(lambda (t1 t2)
10477                (,thread t2 t1))))
10478          (gnus-sort-gathered-threads-function
10479           gnus-thread-sort-functions)
10480          (gnus-article-sort-functions
10481           (if (not reverse)
10482               article
10483             `(lambda (t1 t2)
10484                (,article t2 t1))))
10485          (buffer-read-only)
10486          (gnus-summary-prepare-hook nil))
10487     ;; We do the sorting by regenerating the threads.
10488     (gnus-summary-prepare)
10489     ;; Hide subthreads if needed.
10490     (gnus-summary-maybe-hide-threads)))
10491
10492 ;; Summary saving commands.
10493
10494 (defun gnus-summary-save-article (&optional n not-saved)
10495   "Save the current article using the default saver function.
10496 If N is a positive number, save the N next articles.
10497 If N is a negative number, save the N previous articles.
10498 If N is nil and any articles have been marked with the process mark,
10499 save those articles instead.
10500 The variable `gnus-default-article-saver' specifies the saver function."
10501   (interactive "P")
10502   (let* ((articles (gnus-summary-work-articles n))
10503          (save-buffer (save-excursion
10504                         (nnheader-set-temp-buffer " *Gnus Save*")))
10505          (num (length articles))
10506          header file)
10507     (dolist (article articles)
10508       (setq header (gnus-summary-article-header article))
10509       (if (not (vectorp header))
10510           ;; This is a pseudo-article.
10511           (if (assq 'name header)
10512               (gnus-copy-file (cdr (assq 'name header)))
10513             (gnus-message 1 "Article %d is unsaveable" article))
10514         ;; This is a real article.
10515         (save-window-excursion
10516           (let ((gnus-display-mime-function nil)
10517                 (gnus-article-prepare-hook nil))
10518             (gnus-summary-select-article t nil nil article)))
10519         (save-excursion
10520           (set-buffer save-buffer)
10521           (erase-buffer)
10522           (insert-buffer-substring gnus-original-article-buffer))
10523         (setq file (gnus-article-save save-buffer file num))
10524         (gnus-summary-remove-process-mark article)
10525         (unless not-saved
10526           (gnus-summary-set-saved-mark article))))
10527     (gnus-kill-buffer save-buffer)
10528     (gnus-summary-position-point)
10529     (gnus-set-mode-line 'summary)
10530     n))
10531
10532 (defun gnus-summary-pipe-output (&optional arg headers)
10533   "Pipe the current article to a subprocess.
10534 If N is a positive number, pipe the N next articles.
10535 If N is a negative number, pipe the N previous articles.
10536 If N is nil and any articles have been marked with the process mark,
10537 pipe those articles instead.
10538 If HEADERS (the symbolic prefix), include the headers, too."
10539   (interactive (gnus-interactive "P\ny"))
10540   (require 'gnus-art)
10541   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10542         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10543     (gnus-summary-save-article arg t))
10544   (let ((buffer (get-buffer "*Shell Command Output*")))
10545     (when (and buffer
10546                (not (zerop (buffer-size buffer))))
10547       (gnus-configure-windows 'pipe))))
10548
10549 (defun gnus-summary-save-article-mail (&optional arg)
10550   "Append the current article to an mail file.
10551 If N is a positive number, save the N next articles.
10552 If N is a negative number, save the N previous articles.
10553 If N is nil and any articles have been marked with the process mark,
10554 save those articles instead."
10555   (interactive "P")
10556   (require 'gnus-art)
10557   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10558     (gnus-summary-save-article arg)))
10559
10560 (defun gnus-summary-save-article-rmail (&optional arg)
10561   "Append the current article to an rmail file.
10562 If N is a positive number, save the N next articles.
10563 If N is a negative number, save the N previous articles.
10564 If N is nil and any articles have been marked with the process mark,
10565 save those articles instead."
10566   (interactive "P")
10567   (require 'gnus-art)
10568   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10569     (gnus-summary-save-article arg)))
10570
10571 (defun gnus-summary-save-article-file (&optional arg)
10572   "Append the current article to a file.
10573 If N is a positive number, save the N next articles.
10574 If N is a negative number, save the N previous articles.
10575 If N is nil and any articles have been marked with the process mark,
10576 save those articles instead."
10577   (interactive "P")
10578   (require 'gnus-art)
10579   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10580     (gnus-summary-save-article arg)))
10581
10582 (defun gnus-summary-write-article-file (&optional arg)
10583   "Write the current article to a file, deleting the previous file.
10584 If N is a positive number, save the N next articles.
10585 If N is a negative number, save the N previous articles.
10586 If N is nil and any articles have been marked with the process mark,
10587 save those articles instead."
10588   (interactive "P")
10589   (require 'gnus-art)
10590   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10591     (gnus-summary-save-article arg)))
10592
10593 (defun gnus-summary-save-article-body-file (&optional arg)
10594   "Append the current article body to a file.
10595 If N is a positive number, save the N next articles.
10596 If N is a negative number, save the N previous articles.
10597 If N is nil and any articles have been marked with the process mark,
10598 save those articles instead."
10599   (interactive "P")
10600   (require 'gnus-art)
10601   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10602     (gnus-summary-save-article arg)))
10603
10604 (defun gnus-summary-muttprint (&optional arg)
10605   "Print the current article using Muttprint.
10606 If N is a positive number, save the N next articles.
10607 If N is a negative number, save the N previous articles.
10608 If N is nil and any articles have been marked with the process mark,
10609 save those articles instead."
10610   (interactive "P")
10611   (require 'gnus-art)
10612   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10613     (gnus-summary-save-article arg t)))
10614
10615 (defun gnus-summary-pipe-message (program)
10616   "Pipe the current article through PROGRAM."
10617   (interactive "sProgram: ")
10618   (gnus-summary-select-article)
10619   (let ((mail-header-separator ""))
10620     (gnus-eval-in-buffer-window gnus-article-buffer
10621       (save-restriction
10622         (widen)
10623         (let ((start (window-start))
10624               buffer-read-only)
10625           (message-pipe-buffer-body program)
10626           (set-window-start (get-buffer-window (current-buffer)) start))))))
10627
10628 (defun gnus-get-split-value (methods)
10629   "Return a value based on the split METHODS."
10630   (let (split-name method result match)
10631     (when methods
10632       (save-excursion
10633         (set-buffer gnus-original-article-buffer)
10634         (save-restriction
10635           (nnheader-narrow-to-headers)
10636           (while (and methods (not split-name))
10637             (goto-char (point-min))
10638             (setq method (pop methods))
10639             (setq match (car method))
10640             (when (cond
10641                    ((stringp match)
10642                     ;; Regular expression.
10643                     (ignore-errors
10644                       (re-search-forward match nil t)))
10645                    ((gnus-functionp match)
10646                     ;; Function.
10647                     (save-restriction
10648                       (widen)
10649                       (setq result (funcall match gnus-newsgroup-name))))
10650                    ((consp match)
10651                     ;; Form.
10652                     (save-restriction
10653                       (widen)
10654                       (setq result (eval match)))))
10655               (setq split-name (cdr method))
10656               (cond ((stringp result)
10657                      (push (expand-file-name
10658                             result gnus-article-save-directory)
10659                            split-name))
10660                     ((consp result)
10661                      (setq split-name (append result split-name)))))))))
10662     (nreverse split-name)))
10663
10664 (defun gnus-valid-move-group-p (group)
10665   (and (boundp group)
10666        (symbol-name group)
10667        (symbol-value group)
10668        (gnus-get-function (gnus-find-method-for-group
10669                            (symbol-name group)) 'request-accept-article t)))
10670
10671 (defun gnus-read-move-group-name (prompt default articles prefix)
10672   "Read a group name."
10673   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10674          (minibuffer-confirm-incomplete nil) ; XEmacs
10675          (prom
10676           (format "%s %s to:"
10677                   prompt
10678                   (if (> (length articles) 1)
10679                       (format "these %d articles" (length articles))
10680                     "this article")))
10681          (to-newsgroup
10682           (cond
10683            ((null split-name)
10684             (gnus-completing-read-with-default
10685              default prom
10686              gnus-active-hashtb
10687              'gnus-valid-move-group-p
10688              nil prefix
10689              'gnus-group-history))
10690            ((= 1 (length split-name))
10691             (gnus-completing-read-with-default
10692              (car split-name) prom
10693              gnus-active-hashtb
10694              'gnus-valid-move-group-p
10695              nil nil
10696              'gnus-group-history))
10697            (t
10698             (gnus-completing-read-with-default
10699              nil prom
10700              (mapcar (lambda (el) (list el))
10701                      (nreverse split-name))
10702              nil nil nil
10703              'gnus-group-history))))
10704          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10705     (when to-newsgroup
10706       (if (or (string= to-newsgroup "")
10707               (string= to-newsgroup prefix))
10708           (setq to-newsgroup default))
10709       (unless to-newsgroup
10710         (error "No group name entered"))
10711       (or (gnus-active to-newsgroup)
10712           (gnus-activate-group to-newsgroup nil nil to-method)
10713           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10714                                      to-newsgroup))
10715               (or (and (gnus-request-create-group to-newsgroup to-method)
10716                        (gnus-activate-group
10717                         to-newsgroup nil nil to-method)
10718                        (gnus-subscribe-group to-newsgroup))
10719                   (error "Couldn't create group %s" to-newsgroup)))
10720           (error "No such group: %s" to-newsgroup)))
10721     to-newsgroup))
10722
10723 (defun gnus-summary-save-parts (type dir n &optional reverse)
10724   "Save parts matching TYPE to DIR.
10725 If REVERSE, save parts that do not match TYPE."
10726   (interactive
10727    (list (read-string "Save parts of type: "
10728                       (or (car gnus-summary-save-parts-type-history)
10729                           gnus-summary-save-parts-default-mime)
10730                       'gnus-summary-save-parts-type-history)
10731          (setq gnus-summary-save-parts-last-directory
10732                (read-file-name "Save to directory: "
10733                                gnus-summary-save-parts-last-directory
10734                                nil t))
10735          current-prefix-arg))
10736   (gnus-summary-iterate n
10737     (let ((gnus-display-mime-function nil)
10738           (gnus-inhibit-treatment t))
10739       (gnus-summary-select-article))
10740     (save-excursion
10741       (set-buffer gnus-article-buffer)
10742       (let ((handles (or gnus-article-mime-handles
10743                          (mm-dissect-buffer nil gnus-article-loose-mime)
10744                          (and gnus-article-emulate-mime
10745                               (mm-uu-dissect)))))
10746         (when handles
10747           (gnus-summary-save-parts-1 type dir handles reverse)
10748           (unless gnus-article-mime-handles ;; Don't destroy this case.
10749             (mm-destroy-parts handles)))))))
10750
10751 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10752   (if (stringp (car handle))
10753       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10754               (cdr handle))
10755     (when (if reverse
10756               (not (string-match type (mm-handle-media-type handle)))
10757             (string-match type (mm-handle-media-type handle)))
10758       (let ((file (expand-file-name
10759                    (file-name-nondirectory
10760                     (or
10761                      (mail-content-type-get
10762                       (mm-handle-disposition handle) 'filename)
10763                      (concat gnus-newsgroup-name
10764                              "." (number-to-string
10765                                   (cdr gnus-article-current)))))
10766                    dir)))
10767         (unless (file-exists-p file)
10768           (mm-save-part-to-file handle file))))))
10769
10770 ;; Summary extract commands
10771
10772 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10773   (let ((buffer-read-only nil)
10774         (article (gnus-summary-article-number))
10775         after-article b e)
10776     (unless (gnus-summary-goto-subject article)
10777       (error "No such article: %d" article))
10778     (gnus-summary-position-point)
10779     ;; If all commands are to be bunched up on one line, we collect
10780     ;; them here.
10781     (unless gnus-view-pseudos-separately
10782       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10783             files action)
10784         (while ps
10785           (setq action (cdr (assq 'action (car ps))))
10786           (setq files (list (cdr (assq 'name (car ps)))))
10787           (while (and ps (cdr ps)
10788                       (string= (or action "1")
10789                                (or (cdr (assq 'action (cadr ps))) "2")))
10790             (push (cdr (assq 'name (cadr ps))) files)
10791             (setcdr ps (cddr ps)))
10792           (when files
10793             (when (not (string-match "%s" action))
10794               (push " " files))
10795             (push " " files)
10796             (when (assq 'execute (car ps))
10797               (setcdr (assq 'execute (car ps))
10798                       (funcall (if (string-match "%s" action)
10799                                    'format 'concat)
10800                                action
10801                                (mapconcat
10802                                 (lambda (f)
10803                                   (if (equal f " ")
10804                                       f
10805                                     (mm-quote-arg f)))
10806                                 files " ")))))
10807           (setq ps (cdr ps)))))
10808     (if (and gnus-view-pseudos (not not-view))
10809         (while pslist
10810           (when (assq 'execute (car pslist))
10811             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10812                                   (eq gnus-view-pseudos 'not-confirm)))
10813           (setq pslist (cdr pslist)))
10814       (save-excursion
10815         (while pslist
10816           (setq after-article (or (cdr (assq 'article (car pslist)))
10817                                   (gnus-summary-article-number)))
10818           (gnus-summary-goto-subject after-article)
10819           (forward-line 1)
10820           (setq b (point))
10821           (insert "    " (file-name-nondirectory
10822                           (cdr (assq 'name (car pslist))))
10823                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10824           (setq e (point))
10825           (forward-line -1)             ; back to `b'
10826           (gnus-add-text-properties
10827            b (1- e) (list 'gnus-number gnus-reffed-article-number
10828                           gnus-mouse-face-prop gnus-mouse-face))
10829           (gnus-data-enter
10830            after-article gnus-reffed-article-number
10831            gnus-unread-mark b (car pslist) 0 (- e b))
10832           (setq gnus-newsgroup-unreads
10833                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10834                                          gnus-reffed-article-number))
10835           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10836           (setq pslist (cdr pslist)))))))
10837
10838 (defun gnus-pseudos< (p1 p2)
10839   (let ((c1 (cdr (assq 'action p1)))
10840         (c2 (cdr (assq 'action p2))))
10841     (and c1 c2 (string< c1 c2))))
10842
10843 (defun gnus-request-pseudo-article (props)
10844   (cond ((assq 'execute props)
10845          (gnus-execute-command (cdr (assq 'execute props)))))
10846   (let ((gnus-current-article (gnus-summary-article-number)))
10847     (gnus-run-hooks 'gnus-mark-article-hook)))
10848
10849 (defun gnus-execute-command (command &optional automatic)
10850   (save-excursion
10851     (gnus-article-setup-buffer)
10852     (set-buffer gnus-article-buffer)
10853     (setq buffer-read-only nil)
10854     (let ((command (if automatic command
10855                      (read-string "Command: " (cons command 0)))))
10856       (erase-buffer)
10857       (insert "$ " command "\n\n")
10858       (if gnus-view-pseudo-asynchronously
10859           (start-process "gnus-execute" (current-buffer) shell-file-name
10860                          shell-command-switch command)
10861         (call-process shell-file-name nil t nil
10862                       shell-command-switch command)))))
10863
10864 ;; Summary kill commands.
10865
10866 (defun gnus-summary-edit-global-kill (article)
10867   "Edit the \"global\" kill file."
10868   (interactive (list (gnus-summary-article-number)))
10869   (gnus-group-edit-global-kill article))
10870
10871 (defun gnus-summary-edit-local-kill ()
10872   "Edit a local kill file applied to the current newsgroup."
10873   (interactive)
10874   (setq gnus-current-headers (gnus-summary-article-header))
10875   (gnus-group-edit-local-kill
10876    (gnus-summary-article-number) gnus-newsgroup-name))
10877
10878 ;;; Header reading.
10879
10880 (defun gnus-read-header (id &optional header)
10881   "Read the headers of article ID and enter them into the Gnus system."
10882   (let ((group gnus-newsgroup-name)
10883         (gnus-override-method
10884          (or
10885           gnus-override-method
10886           (and (gnus-news-group-p gnus-newsgroup-name)
10887                (car (gnus-refer-article-methods)))))
10888         where)
10889     ;; First we check to see whether the header in question is already
10890     ;; fetched.
10891     (if (stringp id)
10892         ;; This is a Message-ID.
10893         (setq header (or header (gnus-id-to-header id)))
10894       ;; This is an article number.
10895       (setq header (or header (gnus-summary-article-header id))))
10896     (if (and header
10897              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10898         ;; We have found the header.
10899         header
10900       ;; If this is a sparse article, we have to nix out its
10901       ;; previous entry in the thread hashtb.
10902       (when (and header
10903                  (gnus-summary-article-sparse-p (mail-header-number header)))
10904         (let* ((parent (gnus-parent-id (mail-header-references header)))
10905                (thread (and parent (gnus-id-to-thread parent))))
10906           (when thread
10907             (delq (assq header thread) thread))))
10908       ;; We have to really fetch the header to this article.
10909       (save-excursion
10910         (set-buffer nntp-server-buffer)
10911         (when (setq where (gnus-request-head id group))
10912           (nnheader-fold-continuation-lines)
10913           (goto-char (point-max))
10914           (insert ".\n")
10915           (goto-char (point-min))
10916           (insert "211 ")
10917           (princ (cond
10918                   ((numberp id) id)
10919                   ((cdr where) (cdr where))
10920                   (header (mail-header-number header))
10921                   (t gnus-reffed-article-number))
10922                  (current-buffer))
10923           (insert " Article retrieved.\n"))
10924         (if (or (not where)
10925                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10926             ()                          ; Malformed head.
10927           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10928             (when (and (stringp id)
10929                        (not (string= (gnus-group-real-name group)
10930                                      (car where))))
10931               ;; If we fetched by Message-ID and the article came
10932               ;; from a different group, we fudge some bogus article
10933               ;; numbers for this article.
10934               (mail-header-set-number header gnus-reffed-article-number))
10935             (save-excursion
10936               (set-buffer gnus-summary-buffer)
10937               (decf gnus-reffed-article-number)
10938               (gnus-remove-header (mail-header-number header))
10939               (push header gnus-newsgroup-headers)
10940               (setq gnus-current-headers header)
10941               (push (mail-header-number header) gnus-newsgroup-limit)))
10942           header)))))
10943
10944 (defun gnus-remove-header (number)
10945   "Remove header NUMBER from `gnus-newsgroup-headers'."
10946   (if (and gnus-newsgroup-headers
10947            (= number (mail-header-number (car gnus-newsgroup-headers))))
10948       (pop gnus-newsgroup-headers)
10949     (let ((headers gnus-newsgroup-headers))
10950       (while (and (cdr headers)
10951                   (not (= number (mail-header-number (cadr headers)))))
10952         (pop headers))
10953       (when (cdr headers)
10954         (setcdr headers (cddr headers))))))
10955
10956 ;;;
10957 ;;; summary highlights
10958 ;;;
10959
10960 (defun gnus-highlight-selected-summary ()
10961   "Highlight selected article in summary buffer."
10962   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10963   (when gnus-summary-selected-face
10964     (save-excursion
10965       (let* ((beg (progn (beginning-of-line) (point)))
10966              (end (progn (end-of-line) (point)))
10967              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10968              (from (if (get-text-property beg gnus-mouse-face-prop)
10969                        beg
10970                      (or (next-single-property-change
10971                           beg gnus-mouse-face-prop nil end)
10972                          beg)))
10973              (to
10974               (if (= from end)
10975                   (- from 2)
10976                 (or (next-single-property-change
10977                      from gnus-mouse-face-prop nil end)
10978                     end))))
10979         ;; If no mouse-face prop on line we will have to = from = end,
10980         ;; so we highlight the entire line instead.
10981         (when (= (+ to 2) from)
10982           (setq from beg)
10983           (setq to end))
10984         (if gnus-newsgroup-selected-overlay
10985             ;; Move old overlay.
10986             (gnus-move-overlay
10987              gnus-newsgroup-selected-overlay from to (current-buffer))
10988           ;; Create new overlay.
10989           (gnus-overlay-put
10990            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10991            'face gnus-summary-selected-face))))))
10992
10993 (defvar gnus-summary-highlight-line-cached nil)
10994 (defvar gnus-summary-highlight-line-trigger nil)
10995
10996 (defun gnus-summary-highlight-line-0 ()
10997   (if (and (eq gnus-summary-highlight-line-trigger 
10998                gnus-summary-highlight)
10999            gnus-summary-highlight-line-cached)
11000       gnus-summary-highlight-line-cached
11001     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11002           gnus-summary-highlight-line-cached
11003           (let* ((cond (list 'cond))
11004                  (c cond)
11005                  (list gnus-summary-highlight))
11006             (while list
11007               (setcdr c (cons (list (caar list) (list 'quote (cdar list))) nil))
11008               (setq c (cdr c)
11009                     list (cdr list)))
11010             (gnus-byte-compile (list 'lambda nil cond))))))
11011
11012 (defun gnus-summary-highlight-line ()
11013   "Highlight current line according to `gnus-summary-highlight'."
11014   (let* ((beg (gnus-point-at-bol))
11015          (article (or (gnus-summary-article-number) gnus-current-article))
11016          (score (or (cdr (assq article
11017                                gnus-newsgroup-scored))
11018                     gnus-summary-default-score 0))
11019          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11020          (inhibit-read-only t)
11021          (default gnus-summary-default-score)
11022          (default-high gnus-summary-default-high-score)
11023          (default-low gnus-summary-default-low-score)
11024          (uncached (memq article gnus-newsgroup-undownloaded))
11025          (downloaded (not uncached)))
11026     (let ((face (funcall (gnus-summary-highlight-line-0))))
11027       (unless (eq face (get-text-property beg 'face))
11028         (gnus-put-text-property-excluding-characters-with-faces
11029          beg (gnus-point-at-eol) 'face
11030          (setq face (if (boundp face) (symbol-value face) face)))
11031         (when gnus-summary-highlight-line-function
11032           (funcall gnus-summary-highlight-line-function article face))))))
11033
11034 (defun gnus-update-read-articles (group unread &optional compute)
11035   "Update the list of read articles in GROUP.
11036 UNREAD is a sorted list."
11037   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11038          (entry (gnus-gethash group gnus-newsrc-hashtb))
11039          (info (nth 2 entry))
11040          (prev 1)
11041          read)
11042     (if (or (not info) (not active))
11043         ;; There is no info on this group if it was, in fact,
11044         ;; killed.  Gnus stores no information on killed groups, so
11045         ;; there's nothing to be done.
11046         ;; One could store the information somewhere temporarily,
11047         ;; perhaps...  Hmmm...
11048         ()
11049       ;; Remove any negative articles numbers.
11050       (while (and unread (< (car unread) 0))
11051         (setq unread (cdr unread)))
11052       ;; Remove any expired article numbers
11053       (while (and unread (< (car unread) (car active)))
11054         (setq unread (cdr unread)))
11055       ;; Compute the ranges of read articles by looking at the list of
11056       ;; unread articles.
11057       (while unread
11058         (when (/= (car unread) prev)
11059           (push (if (= prev (1- (car unread))) prev
11060                   (cons prev (1- (car unread))))
11061                 read))
11062         (setq prev (1+ (car unread)))
11063         (setq unread (cdr unread)))
11064       (when (<= prev (cdr active))
11065         (push (cons prev (cdr active)) read))
11066       (setq read (if (> (length read) 1) (nreverse read) read))
11067       (if compute
11068           read
11069         (save-excursion
11070           (let (setmarkundo)
11071             ;; Propagate the read marks to the backend.
11072             (when (gnus-check-backend-function 'request-set-mark group)
11073               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11074                     (add (gnus-remove-from-range read (gnus-info-read info))))
11075                 (when (or add del)
11076                   (unless (gnus-check-group group)
11077                     (error "Can't open server for %s" group))
11078                   (gnus-request-set-mark
11079                    group (delq nil (list (if add (list add 'add '(read)))
11080                                          (if del (list del 'del '(read))))))
11081                   (setq setmarkundo
11082                         `(gnus-request-set-mark
11083                           ,group
11084                           ',(delq nil (list
11085                                        (if del (list del 'add '(read)))
11086                                        (if add (list add 'del '(read))))))))))
11087             (set-buffer gnus-group-buffer)
11088             (gnus-undo-register
11089               `(progn
11090                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11091                  (gnus-info-set-read ',info ',(gnus-info-read info))
11092                  (gnus-get-unread-articles-in-group ',info
11093                                                     (gnus-active ,group))
11094                  (gnus-group-update-group ,group t)
11095                  ,setmarkundo))))
11096         ;; Enter this list into the group info.
11097         (gnus-info-set-read info read)
11098         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11099         (gnus-get-unread-articles-in-group info (gnus-active group))
11100         t))))
11101
11102 (defun gnus-offer-save-summaries ()
11103   "Offer to save all active summary buffers."
11104   (let (buffers)
11105     ;; Go through all buffers and find all summaries.
11106     (dolist (buffer (buffer-list))
11107       (when (and (setq buffer (buffer-name buffer))
11108                  (string-match "Summary" buffer)
11109                  (save-excursion
11110                    (set-buffer buffer)
11111                    ;; We check that this is, indeed, a summary buffer.
11112                    (and (eq major-mode 'gnus-summary-mode)
11113                         ;; Also make sure this isn't bogus.
11114                         gnus-newsgroup-prepared
11115                         ;; Also make sure that this isn't a
11116                         ;; dead summary buffer.
11117                         (not gnus-dead-summary-mode))))
11118         (push buffer buffers)))
11119     ;; Go through all these summary buffers and offer to save them.
11120     (when buffers
11121       (save-excursion
11122         (map-y-or-n-p
11123          "Update summary buffer %s? "
11124          (lambda (buf)
11125            (switch-to-buffer buf)
11126            (gnus-summary-exit))
11127          buffers)))))
11128
11129 (defun gnus-summary-setup-default-charset ()
11130   "Setup newsgroup default charset."
11131   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11132       (setq gnus-newsgroup-charset nil)
11133     (let* ((ignored-charsets
11134             (or gnus-newsgroup-ephemeral-ignored-charsets
11135                 (append
11136                  (and gnus-newsgroup-name
11137                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11138                  gnus-newsgroup-ignored-charsets))))
11139       (setq gnus-newsgroup-charset
11140             (or gnus-newsgroup-ephemeral-charset
11141                 (and gnus-newsgroup-name
11142                      (gnus-parameter-charset gnus-newsgroup-name))
11143                 gnus-default-charset))
11144       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11145            ignored-charsets))))
11146
11147 ;;;
11148 ;;; Mime Commands
11149 ;;;
11150
11151 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11152   "Display the current article buffer fully MIME-buttonized.
11153 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11154 treated as multipart/mixed."
11155   (interactive "P")
11156   (require 'gnus-art)
11157   (let ((gnus-unbuttonized-mime-types nil)
11158         (gnus-mime-display-multipart-as-mixed show-all-parts))
11159     (gnus-summary-show-article)))
11160
11161 (defun gnus-summary-repair-multipart (article)
11162   "Add a Content-Type header to a multipart article without one."
11163   (interactive (list (gnus-summary-article-number)))
11164   (gnus-with-article article
11165     (message-narrow-to-head)
11166     (message-remove-header "Mime-Version")
11167     (goto-char (point-max))
11168     (insert "Mime-Version: 1.0\n")
11169     (widen)
11170     (when (search-forward "\n--" nil t)
11171       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11172         (message-narrow-to-head)
11173         (message-remove-header "Content-Type")
11174         (goto-char (point-max))
11175         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11176                         separator))
11177         (widen))))
11178   (let (gnus-mark-article-hook)
11179     (gnus-summary-select-article t t nil article)))
11180
11181 (defun gnus-summary-toggle-display-buttonized ()
11182   "Toggle the buttonizing of the article buffer."
11183   (interactive)
11184   (require 'gnus-art)
11185   (if (setq gnus-inhibit-mime-unbuttonizing
11186             (not gnus-inhibit-mime-unbuttonizing))
11187       (let ((gnus-unbuttonized-mime-types nil))
11188         (gnus-summary-show-article))
11189     (gnus-summary-show-article)))
11190
11191 ;;;
11192 ;;; Generic summary marking commands
11193 ;;;
11194
11195 (defvar gnus-summary-marking-alist
11196   '((read gnus-del-mark "d")
11197     (unread gnus-unread-mark "u")
11198     (ticked gnus-ticked-mark "!")
11199     (dormant gnus-dormant-mark "?")
11200     (expirable gnus-expirable-mark "e"))
11201   "An alist of names/marks/keystrokes.")
11202
11203 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11204 (defvar gnus-summary-mark-map)
11205
11206 (defun gnus-summary-make-all-marking-commands ()
11207   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11208   (dolist (elem gnus-summary-marking-alist)
11209     (apply 'gnus-summary-make-marking-command elem)))
11210
11211 (defun gnus-summary-make-marking-command (name mark keystroke)
11212   (let ((map (make-sparse-keymap)))
11213     (define-key gnus-summary-generic-mark-map keystroke map)
11214     (dolist (lway `((next "next" next nil "n")
11215                     (next-unread "next unread" next t "N")
11216                     (prev "previous" prev nil "p")
11217                     (prev-unread "previous unread" prev t "P")
11218                     (nomove "" nil nil ,keystroke)))
11219       (let ((func (gnus-summary-make-marking-command-1
11220                    mark (car lway) lway name)))
11221         (setq func (eval func))
11222         (define-key map (nth 4 lway) func)))))
11223
11224 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11225   `(defun ,(intern
11226             (format "gnus-summary-put-mark-as-%s%s"
11227                     name (if (eq way 'nomove)
11228                              ""
11229                            (concat "-" (symbol-name way)))))
11230      (n)
11231      ,(format
11232        "Mark the current article as %s%s.
11233 If N, the prefix, then repeat N times.
11234 If N is negative, move in reverse order.
11235 The difference between N and the actual number of articles marked is
11236 returned."
11237        name (cadr lway))
11238      (interactive "p")
11239      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11240
11241 (defun gnus-summary-generic-mark (n mark move unread)
11242   "Mark N articles with MARK."
11243   (unless (eq major-mode 'gnus-summary-mode)
11244     (error "This command can only be used in the summary buffer"))
11245   (gnus-summary-show-thread)
11246   (let ((nummove
11247          (cond
11248           ((eq move 'next) 1)
11249           ((eq move 'prev) -1)
11250           (t 0))))
11251     (if (zerop nummove)
11252         (setq n 1)
11253       (when (< n 0)
11254         (setq n (abs n)
11255               nummove (* -1 nummove))))
11256     (while (and (> n 0)
11257                 (gnus-summary-mark-article nil mark)
11258                 (zerop (gnus-summary-next-subject nummove unread t)))
11259       (setq n (1- n)))
11260     (when (/= 0 n)
11261       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11262     (gnus-summary-recenter)
11263     (gnus-summary-position-point)
11264     (gnus-set-mode-line 'summary)
11265     n))
11266
11267 (defun gnus-summary-insert-articles (articles)
11268   (when (setq articles
11269               (gnus-sorted-difference articles
11270                                       (mapcar (lambda (h)
11271                                                 (mail-header-number h))
11272                                               gnus-newsgroup-headers)))
11273     (setq gnus-newsgroup-headers
11274           (merge 'list
11275                  gnus-newsgroup-headers
11276                  (gnus-fetch-headers articles)
11277                  'gnus-article-sort-by-number))
11278     ;; Suppress duplicates?
11279     (when gnus-suppress-duplicates
11280       (gnus-dup-suppress-articles))
11281
11282     ;; We might want to build some more threads first.
11283     (when (and gnus-fetch-old-headers
11284                (eq gnus-headers-retrieved-by 'nov))
11285       (if (eq gnus-fetch-old-headers 'invisible)
11286           (gnus-build-all-threads)
11287         (gnus-build-old-threads)))
11288     ;; Let the Gnus agent mark articles as read.
11289     (when gnus-agent
11290       (gnus-agent-get-undownloaded-list))
11291     ;; Remove list identifiers from subject
11292     (when gnus-list-identifiers
11293       (gnus-summary-remove-list-identifiers))
11294     ;; First and last article in this newsgroup.
11295     (when gnus-newsgroup-headers
11296       (setq gnus-newsgroup-begin
11297             (mail-header-number (car gnus-newsgroup-headers))
11298             gnus-newsgroup-end
11299             (mail-header-number
11300              (gnus-last-element gnus-newsgroup-headers))))
11301     (when gnus-use-scoring
11302       (gnus-possibly-score-headers))))
11303
11304 (defun gnus-summary-insert-old-articles (&optional all)
11305   "Insert all old articles in this group.
11306 If ALL is non-nil, already read articles become readable.
11307 If ALL is a number, fetch this number of articles."
11308   (interactive "P")
11309   (prog1
11310       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11311             older len)
11312         (setq older
11313               ;; Some nntp servers lie about their active range.  When
11314               ;; this happens, the active range can be in the millions.
11315               ;; Use a compressed range to avoid creating a huge list.
11316               (gnus-range-difference (list gnus-newsgroup-active) old))
11317         (setq len (gnus-range-length older))
11318         (cond
11319          ((null older) nil)
11320          ((numberp all)
11321           (if (< all len)
11322               (let ((older-range (nreverse older)))
11323                 (setq older nil)
11324
11325                 (while (> all 0)
11326                   (let* ((r (pop older-range))
11327                          (min (if (numberp r) r (car r)))
11328                          (max (if (numberp r) r (cdr r))))
11329                     (while (and (<= min max)
11330                                 (> all 0))
11331                       (push max older)
11332                       (setq all (1- all)
11333                             max (1- max))))))
11334             (setq older (gnus-uncompress-range older))))
11335          (all
11336           (setq older (gnus-uncompress-range older)))
11337          (t
11338           (when (and (numberp gnus-large-newsgroup)
11339                    (> len gnus-large-newsgroup))
11340               (let* ((cursor-in-echo-area nil)
11341                      (initial (gnus-parameter-large-newsgroup-initial
11342                                gnus-newsgroup-name))
11343                      (input
11344                       (read-string
11345                        (format
11346                         "How many articles from %s (%s %d): "
11347                         (gnus-limit-string
11348                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11349                         (if initial "max" "default")
11350                         len)
11351                        (if initial
11352                            (cons (number-to-string initial)
11353                                  0)))))
11354                 (unless (string-match "^[ \t]*$" input)
11355                   (setq all (string-to-number input))
11356                   (if (< all len)
11357                       (let ((older-range (nreverse older)))
11358                         (setq older nil)
11359
11360                         (while (> all 0)
11361                           (let* ((r (pop older-range))
11362                                  (min (if (numberp r) r (car r)))
11363                                  (max (if (numberp r) r (cdr r))))
11364                             (while (and (<= min max)
11365                                         (> all 0))
11366                               (push max older)
11367                               (setq all (1- all)
11368                                     max (1- max))))))))))
11369           (setq older (gnus-uncompress-range older))))
11370         (if (not older)
11371             (message "No old news.")
11372           (gnus-summary-insert-articles older)
11373           (gnus-summary-limit (gnus-sorted-nunion old older))))
11374     (gnus-summary-position-point)))
11375
11376 (defun gnus-summary-insert-new-articles ()
11377   "Insert all new articles in this group."
11378   (interactive)
11379   (prog1
11380       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11381             (old-active gnus-newsgroup-active)
11382             (nnmail-fetched-sources (list t))
11383             i new)
11384         (setq gnus-newsgroup-active
11385               (gnus-activate-group gnus-newsgroup-name 'scan))
11386         (setq i (cdr gnus-newsgroup-active))
11387         (while (> i (cdr old-active))
11388           (push i new)
11389           (decf i))
11390         (if (not new)
11391             (message "No gnus is bad news.")
11392           (gnus-summary-insert-articles new)
11393           (setq gnus-newsgroup-unreads
11394                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11395           (gnus-summary-limit (gnus-sorted-nunion old new))))
11396     (gnus-summary-position-point)))
11397
11398 (gnus-summary-make-all-marking-commands)
11399
11400 (gnus-ems-redefine)
11401
11402 (provide 'gnus-sum)
11403
11404 (run-hooks 'gnus-sum-load-hook)
11405
11406 ;;; gnus-sum.el ends here