Import Oort Gnus v0.15.
[elisp/gnus.git-] / 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 ?$
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-article-move-hook nil
851   "*A hook called after an article is moved, copied, respooled, or crossposted."
852   :group 'gnus-summary
853   :type 'hook)
854
855 (defcustom gnus-summary-article-delete-hook nil
856   "*A hook called after an article is deleted."
857   :group 'gnus-summary
858   :type 'hook)
859
860 (defcustom gnus-summary-article-expire-hook nil
861   "*A hook called after an article is expired."
862   :group 'gnus-summary
863   :type 'hook)
864
865 (defcustom gnus-summary-display-arrow
866   (and (fboundp 'display-graphic-p)
867        (display-graphic-p))
868   "*If non-nil, display an arrow highlighting the current article."
869   :version "21.1"
870   :group 'gnus-summary
871   :type 'boolean)
872
873 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
874   "Face used for highlighting the current article in the summary buffer."
875   :group 'gnus-summary-visual
876   :type 'face)
877
878 (defvar gnus-tmp-downloaded nil)
879
880 (defcustom gnus-summary-highlight
881   '(((eq mark gnus-canceled-mark)
882      . gnus-summary-cancelled-face)
883     ((and uncached (> score default-high))
884      . gnus-summary-high-undownloaded-face)
885     ((and uncached (< score default-low))
886      . gnus-summary-low-undownloaded-face)
887     (uncached
888      . gnus-summary-normal-undownloaded-face)
889     ((and (> score default-high)
890           (or (eq mark gnus-dormant-mark)
891               (eq mark gnus-ticked-mark)))
892      . gnus-summary-high-ticked-face)
893     ((and (< score default-low)
894           (or (eq mark gnus-dormant-mark)
895               (eq mark gnus-ticked-mark)))
896      . gnus-summary-low-ticked-face)
897     ((or (eq mark gnus-dormant-mark)
898          (eq mark gnus-ticked-mark))
899      . gnus-summary-normal-ticked-face)
900     ((and (> score default-high) (eq mark gnus-ancient-mark))
901      . gnus-summary-high-ancient-face)
902     ((and (< score default-low) (eq mark gnus-ancient-mark))
903      . gnus-summary-low-ancient-face)
904     ((eq mark gnus-ancient-mark)
905      . gnus-summary-normal-ancient-face)
906     ((and (> score default-high) (eq mark gnus-unread-mark))
907      . gnus-summary-high-unread-face)
908     ((and (< score default-low) (eq mark gnus-unread-mark))
909      . gnus-summary-low-unread-face)
910     ((eq mark gnus-unread-mark)
911      . gnus-summary-normal-unread-face)
912     ((> score default-high)
913      . gnus-summary-high-read-face)
914     ((< score default-low)
915      . gnus-summary-low-read-face)
916     (t
917      . gnus-summary-normal-read-face))
918   "*Controls the highlighting of summary buffer lines.
919
920 A list of (FORM . FACE) pairs.  When deciding how a a particular
921 summary line should be displayed, each form is evaluated.  The content
922 of the face field after the first true form is used.  You can change
923 how those summary lines are displayed, by editing the face field.
924
925 You can use the following variables in the FORM field.
926
927 score:        The article's score
928 default:      The default article score.
929 default-high: The default score for high scored articles.
930 default-low:  The default score for low scored articles.
931 below:        The score below which articles are automatically marked as read.
932 mark:         The articles mark."
933   :group 'gnus-summary-visual
934   :type '(repeat (cons (sexp :tag "Form" nil)
935                        face)))
936
937 (defcustom gnus-alter-header-function nil
938   "Function called to allow alteration of article header structures.
939 The function is called with one parameter, the article header vector,
940 which it may alter in any way."
941   :type '(choice (const :tag "None" nil)
942                  function)
943   :group 'gnus-summary)
944
945 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
946   "Variable that says which function should be used to decode a string with encoded words.")
947
948 (defcustom gnus-extra-headers '(To Newsgroups)
949   "*Extra headers to parse."
950   :version "21.1"
951   :group 'gnus-summary
952   :type '(repeat symbol))
953
954 (defcustom gnus-ignored-from-addresses
955   (and user-mail-address (regexp-quote user-mail-address))
956   "*Regexp of From headers that may be suppressed in favor of To headers."
957   :version "21.1"
958   :group 'gnus-summary
959   :type 'regexp)
960
961 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
962   "List of charsets that should be ignored.
963 When these charsets are used in the \"charset\" parameter, the
964 default charset will be used instead."
965   :version "21.1"
966   :type '(repeat symbol)
967   :group 'gnus-charset)
968
969 (gnus-define-group-parameter
970  ignored-charsets
971  :type list
972  :function-document
973  "Return the ignored charsets of GROUP."
974  :variable gnus-group-ignored-charsets-alist
975  :variable-default
976  '(("alt\\.chinese\\.text" iso-8859-1))
977  :variable-document
978  "Alist of regexps (to match group names) and charsets that should be ignored.
979 When these charsets are used in the \"charset\" parameter, the
980 default charset will be used instead."
981  :variable-group gnus-charset
982  :variable-type '(repeat (cons (regexp :tag "Group")
983                                (repeat symbol)))
984  :parameter-type '(choice :tag "Ignored charsets"
985                           :value nil
986                           (repeat (symbol)))
987  :parameter-document       "\
988 List of charsets that should be ignored.
989
990 When these charsets are used in the \"charset\" parameter, the
991 default charset will be used instead.")
992
993 (defcustom gnus-group-highlight-words-alist nil
994   "Alist of group regexps and highlight regexps.
995 This variable uses the same syntax as `gnus-emphasis-alist'."
996   :version "21.1"
997   :type '(repeat (cons (regexp :tag "Group")
998                        (repeat (list (regexp :tag "Highlight regexp")
999                                      (number :tag "Group for entire word" 0)
1000                                      (number :tag "Group for displayed part" 0)
1001                                      (symbol :tag "Face"
1002                                              gnus-emphasis-highlight-words)))))
1003   :group 'gnus-summary-visual)
1004
1005 (defcustom gnus-summary-show-article-charset-alist
1006   nil
1007   "Alist of number and charset.
1008 The article will be shown with the charset corresponding to the
1009 numbered argument.
1010 For example: ((1 . cn-gb-2312) (2 . big5))."
1011   :version "21.1"
1012   :type '(repeat (cons (number :tag "Argument" 1)
1013                        (symbol :tag "Charset")))
1014   :group 'gnus-charset)
1015
1016 (defcustom gnus-preserve-marks t
1017   "Whether marks are preserved when moving, copying and respooling messages."
1018   :version "21.1"
1019   :type 'boolean
1020   :group 'gnus-summary-marks)
1021
1022 (defcustom gnus-alter-articles-to-read-function nil
1023   "Function to be called to alter the list of articles to be selected."
1024   :type '(choice (const nil) function)
1025   :group 'gnus-summary)
1026
1027 (defcustom gnus-orphan-score nil
1028   "*All orphans get this score added.  Set in the score file."
1029   :group 'gnus-score-default
1030   :type '(choice (const nil)
1031                  integer))
1032
1033 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1034   "*A regexp to match MIME parts when saving multiple parts of a message
1035 with gnus-summary-save-parts (X m). This regexp will be used by default
1036 when prompting the user for which type of files to save."
1037   :group 'gnus-summary
1038   :type 'regexp)
1039
1040 (defcustom gnus-read-all-available-headers nil
1041   "Whether Gnus should parse all headers made available to it.
1042 This is mostly relevant for slow backends where the user may
1043 wish to widen the summary buffer to include all headers
1044 that were fetched.  Say, for nnultimate groups."
1045   :group 'gnus-summary
1046   :type '(choice boolean regexp))
1047
1048 (defcustom gnus-summary-muttprint-program "muttprint"
1049   "Command (and optional arguments) used to run Muttprint."
1050   :version "21.3"
1051   :group 'gnus-summary
1052   :type 'string)
1053
1054 (defcustom gnus-article-loose-mime nil
1055   "If non-nil, don't require MIME-Version header.
1056 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1057 supply the MIME-Version header or deliberately strip it From the mail.
1058 Set it to non-nil, Gnus will treat some articles as MIME even if
1059 the MIME-Version header is missed."
1060   :version "21.3"
1061   :type 'boolean
1062   :group 'gnus-article)
1063
1064 (defcustom gnus-article-emulate-mime t
1065   "If non-nil, use MIME emulation for uuencode and the like.
1066 This means that Gnus will search message bodies for text that look
1067 like uuencoded bits, yEncoded bits, and so on, and present that using
1068 the normal Gnus MIME machinery."
1069   :type 'boolean
1070   :group 'gnus-article)
1071
1072 ;;; Internal variables
1073
1074 (defvar gnus-summary-display-cache nil)
1075 (defvar gnus-article-mime-handles nil)
1076 (defvar gnus-article-decoded-p nil)
1077 (defvar gnus-article-charset nil)
1078 (defvar gnus-article-ignored-charsets nil)
1079 (defvar gnus-scores-exclude-files nil)
1080 (defvar gnus-page-broken nil)
1081
1082 (defvar gnus-original-article nil)
1083 (defvar gnus-article-internal-prepare-hook nil)
1084 (defvar gnus-newsgroup-process-stack nil)
1085
1086 (defvar gnus-thread-indent-array nil)
1087 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1088 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1089   "Function called to sort the articles within a thread after it has been gathered together.")
1090
1091 (defvar gnus-summary-save-parts-type-history nil)
1092 (defvar gnus-summary-save-parts-last-directory nil)
1093
1094 ;; Avoid highlighting in kill files.
1095 (defvar gnus-summary-inhibit-highlight nil)
1096 (defvar gnus-newsgroup-selected-overlay nil)
1097 (defvar gnus-inhibit-limiting nil)
1098 (defvar gnus-newsgroup-adaptive-score-file nil)
1099 (defvar gnus-current-score-file nil)
1100 (defvar gnus-current-move-group nil)
1101 (defvar gnus-current-copy-group nil)
1102 (defvar gnus-current-crosspost-group nil)
1103 (defvar gnus-newsgroup-display nil)
1104
1105 (defvar gnus-newsgroup-dependencies nil)
1106 (defvar gnus-newsgroup-adaptive nil)
1107 (defvar gnus-summary-display-article-function nil)
1108 (defvar gnus-summary-highlight-line-function nil
1109   "Function called after highlighting a summary line.")
1110
1111 (defvar gnus-summary-line-format-alist
1112   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1113     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1114     (?s gnus-tmp-subject-or-nil ?s)
1115     (?n gnus-tmp-name ?s)
1116     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1117         ?s)
1118     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1119             gnus-tmp-from) ?s)
1120     (?F gnus-tmp-from ?s)
1121     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1122     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1123     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1124     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1125     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1126     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1127     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1128     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1129     (?L gnus-tmp-lines ?s)
1130     (?O gnus-tmp-downloaded ?c)
1131     (?I gnus-tmp-indentation ?s)
1132     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1133     (?R gnus-tmp-replied ?c)
1134     (?\[ gnus-tmp-opening-bracket ?c)
1135     (?\] gnus-tmp-closing-bracket ?c)
1136     (?\> (make-string gnus-tmp-level ? ) ?s)
1137     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1138     (?i gnus-tmp-score ?d)
1139     (?z gnus-tmp-score-char ?c)
1140     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1141     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1142     (?U gnus-tmp-unread ?c)
1143     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1144         ?s)
1145     (?t (gnus-summary-number-of-articles-in-thread
1146          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1147         ?d)
1148     (?e (gnus-summary-number-of-articles-in-thread
1149          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1150         ?c)
1151     (?u gnus-tmp-user-defined ?s)
1152     (?P (gnus-pick-line-number) ?d)
1153     (?B gnus-tmp-thread-tree-header-string ?s)
1154     (user-date (gnus-user-date
1155                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1156   "An alist of format specifications that can appear in summary lines.
1157 These are paired with what variables they correspond with, along with
1158 the type of the variable (string, integer, character, etc).")
1159
1160 (defvar gnus-summary-dummy-line-format-alist
1161   `((?S gnus-tmp-subject ?s)
1162     (?N gnus-tmp-number ?d)
1163     (?u gnus-tmp-user-defined ?s)))
1164
1165 (defvar gnus-summary-mode-line-format-alist
1166   `((?G gnus-tmp-group-name ?s)
1167     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1168     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1169     (?A gnus-tmp-article-number ?d)
1170     (?Z gnus-tmp-unread-and-unselected ?s)
1171     (?V gnus-version ?s)
1172     (?U gnus-tmp-unread-and-unticked ?d)
1173     (?S gnus-tmp-subject ?s)
1174     (?e gnus-tmp-unselected ?d)
1175     (?u gnus-tmp-user-defined ?s)
1176     (?d (length gnus-newsgroup-dormant) ?d)
1177     (?t (length gnus-newsgroup-marked) ?d)
1178     (?h (length gnus-newsgroup-spam-marked) ?d)
1179     (?r (length gnus-newsgroup-reads) ?d)
1180     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1181     (?E gnus-newsgroup-expunged-tally ?d)
1182     (?s (gnus-current-score-file-nondirectory) ?s)))
1183
1184 (defvar gnus-last-search-regexp nil
1185   "Default regexp for article search command.")
1186
1187 (defvar gnus-last-shell-command nil
1188   "Default shell command on article.")
1189
1190 (defvar gnus-newsgroup-agentized nil
1191   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1192 (defvar gnus-newsgroup-begin nil)
1193 (defvar gnus-newsgroup-end nil)
1194 (defvar gnus-newsgroup-last-rmail nil)
1195 (defvar gnus-newsgroup-last-mail nil)
1196 (defvar gnus-newsgroup-last-folder nil)
1197 (defvar gnus-newsgroup-last-file nil)
1198 (defvar gnus-newsgroup-auto-expire nil)
1199 (defvar gnus-newsgroup-active nil)
1200
1201 (defvar gnus-newsgroup-data nil)
1202 (defvar gnus-newsgroup-data-reverse nil)
1203 (defvar gnus-newsgroup-limit nil)
1204 (defvar gnus-newsgroup-limits nil)
1205
1206 (defvar gnus-newsgroup-unreads nil
1207   "Sorted list of unread articles in the current newsgroup.")
1208
1209 (defvar gnus-newsgroup-unselected nil
1210   "Sorted list of unselected unread articles in the current newsgroup.")
1211
1212 (defvar gnus-newsgroup-reads nil
1213   "Alist of read articles and article marks in the current newsgroup.")
1214
1215 (defvar gnus-newsgroup-expunged-tally nil)
1216
1217 (defvar gnus-newsgroup-marked nil
1218   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1219
1220 (defvar gnus-newsgroup-spam-marked nil
1221   "List of ranges of articles that have been marked as spam.")
1222
1223 (defvar gnus-newsgroup-killed nil
1224   "List of ranges of articles that have been through the scoring process.")
1225
1226 (defvar gnus-newsgroup-cached nil
1227   "Sorted list of articles that come from the article cache.")
1228
1229 (defvar gnus-newsgroup-saved nil
1230   "List of articles that have been saved.")
1231
1232 (defvar gnus-newsgroup-kill-headers nil)
1233
1234 (defvar gnus-newsgroup-replied nil
1235   "List of articles that have been replied to in the current newsgroup.")
1236
1237 (defvar gnus-newsgroup-forwarded nil
1238   "List of articles that have been forwarded in the current newsgroup.")
1239
1240 (defvar gnus-newsgroup-recent nil
1241   "List of articles that have are recent in the current newsgroup.")
1242
1243 (defvar gnus-newsgroup-expirable nil
1244   "Sorted list of articles in the current newsgroup that can be expired.")
1245
1246 (defvar gnus-newsgroup-processable nil
1247   "List of articles in the current newsgroup that can be processed.")
1248
1249 (defvar gnus-newsgroup-downloadable nil
1250   "Sorted list of articles in the current newsgroup that can be processed.")
1251
1252 (defvar gnus-newsgroup-unfetched nil
1253   "Sorted list of articles in the current newsgroup whose headers have not been fetched into the agent.")
1254
1255 (defvar gnus-newsgroup-undownloaded nil
1256   "List of articles in the current newsgroup that haven't been downloaded..")
1257
1258 (defvar gnus-newsgroup-unsendable nil
1259   "List of articles in the current newsgroup that won't be sent.")
1260
1261 (defvar gnus-newsgroup-bookmarks nil
1262   "List of articles in the current newsgroup that have bookmarks.")
1263
1264 (defvar gnus-newsgroup-dormant nil
1265   "Sorted list of dormant articles in the current newsgroup.")
1266
1267 (defvar gnus-newsgroup-unseen nil
1268   "List of unseen articles in the current newsgroup.")
1269
1270 (defvar gnus-newsgroup-seen nil
1271   "Range of seen articles in the current newsgroup.")
1272
1273 (defvar gnus-newsgroup-articles nil
1274   "List of articles in the current newsgroup.")
1275
1276 (defvar gnus-newsgroup-scored nil
1277   "List of scored articles in the current newsgroup.")
1278
1279 (defvar gnus-newsgroup-headers nil
1280   "List of article headers in the current newsgroup.")
1281
1282 (defvar gnus-newsgroup-threads nil)
1283
1284 (defvar gnus-newsgroup-prepared nil
1285   "Whether the current group has been prepared properly.")
1286
1287 (defvar gnus-newsgroup-ancient nil
1288   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1289
1290 (defvar gnus-newsgroup-sparse nil)
1291
1292 (defvar gnus-current-article nil)
1293 (defvar gnus-article-current nil)
1294 (defvar gnus-current-headers nil)
1295 (defvar gnus-have-all-headers nil)
1296 (defvar gnus-last-article nil)
1297 (defvar gnus-newsgroup-history nil)
1298 (defvar gnus-newsgroup-charset nil)
1299 (defvar gnus-newsgroup-ephemeral-charset nil)
1300 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1301
1302 (defvar gnus-article-before-search nil)
1303
1304 (defconst gnus-summary-local-variables
1305   '(gnus-newsgroup-name
1306     gnus-newsgroup-begin gnus-newsgroup-end
1307     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1308     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1309     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1310     gnus-newsgroup-unselected gnus-newsgroup-marked
1311     gnus-newsgroup-spam-marked
1312     gnus-newsgroup-reads gnus-newsgroup-saved
1313     gnus-newsgroup-replied gnus-newsgroup-forwarded
1314     gnus-newsgroup-recent
1315     gnus-newsgroup-expirable
1316     gnus-newsgroup-processable gnus-newsgroup-killed
1317     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1318     gnus-newsgroup-unfetched
1319     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1320     gnus-newsgroup-seen gnus-newsgroup-articles
1321     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1322     gnus-newsgroup-headers gnus-newsgroup-threads
1323     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1324     gnus-current-article gnus-current-headers gnus-have-all-headers
1325     gnus-last-article gnus-article-internal-prepare-hook
1326     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1327     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1328     gnus-thread-expunge-below
1329     gnus-score-alist gnus-current-score-file
1330     (gnus-summary-expunge-below . global)
1331     (gnus-summary-mark-below . global)
1332     (gnus-orphan-score . global)
1333     gnus-newsgroup-active gnus-scores-exclude-files
1334     gnus-newsgroup-history gnus-newsgroup-ancient
1335     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1336     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1337     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1338     (gnus-newsgroup-expunged-tally . 0)
1339     gnus-cache-removable-articles gnus-newsgroup-cached
1340     gnus-newsgroup-data gnus-newsgroup-data-reverse
1341     gnus-newsgroup-limit gnus-newsgroup-limits
1342     gnus-newsgroup-charset gnus-newsgroup-display)
1343   "Variables that are buffer-local to the summary buffers.")
1344
1345 (defvar gnus-newsgroup-variables nil
1346   "A list of variables that have separate values in different newsgroups.
1347 A list of newsgroup (summary buffer) local variables, or cons of
1348 variables and their default values (when the default values are not
1349 nil), that should be made global while the summary buffer is active.
1350 These variables can be used to set variables in the group parameters
1351 while still allowing them to affect operations done in other
1352 buffers. For example:
1353
1354 \(setq gnus-newsgroup-variables
1355      '(message-use-followup-to
1356        (gnus-visible-headers .
1357          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1358 ")
1359
1360 ;; Byte-compiler warning.
1361 ;(eval-when-compile (defvar gnus-article-mode-map))
1362 (eval-when-compile
1363   (let ((features (cons 'gnus-sum features)))
1364     (require 'gnus)
1365     (require 'gnus-agent)
1366     (require 'gnus-art)))
1367
1368 ;; MIME stuff.
1369
1370 (defvar gnus-decode-encoded-word-methods
1371   '(mail-decode-encoded-word-string)
1372   "List of methods used to decode encoded words.
1373
1374 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1375 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1376 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1377 whose names match REGEXP.
1378
1379 For example:
1380 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1381  mail-decode-encoded-word-string
1382  (\"chinese\" . rfc1843-decode-string))")
1383
1384 (defvar gnus-decode-encoded-word-methods-cache nil)
1385
1386 (defun gnus-multi-decode-encoded-word-string (string)
1387   "Apply the functions from `gnus-encoded-word-methods' that match."
1388   (unless (and gnus-decode-encoded-word-methods-cache
1389                (eq gnus-newsgroup-name
1390                    (car gnus-decode-encoded-word-methods-cache)))
1391     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1392     (mapcar (lambda (x)
1393               (if (symbolp x)
1394                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1395                 (if (and gnus-newsgroup-name
1396                          (string-match (car x) gnus-newsgroup-name))
1397                     (nconc gnus-decode-encoded-word-methods-cache
1398                            (list (cdr x))))))
1399             gnus-decode-encoded-word-methods))
1400   (let ((xlist gnus-decode-encoded-word-methods-cache))
1401     (pop xlist)
1402     (while xlist
1403       (setq string (funcall (pop xlist) string))))
1404   string)
1405
1406 ;; Subject simplification.
1407
1408 (defun gnus-simplify-whitespace (str)
1409   "Remove excessive whitespace from STR."
1410   ;; Multiple spaces.
1411   (while (string-match "[ \t][ \t]+" str)
1412     (setq str (concat (substring str 0 (match-beginning 0))
1413                         " "
1414                         (substring str (match-end 0)))))
1415   ;; Leading spaces.
1416   (when (string-match "^[ \t]+" str)
1417     (setq str (substring str (match-end 0))))
1418   ;; Trailing spaces.
1419   (when (string-match "[ \t]+$" str)
1420     (setq str (substring str 0 (match-beginning 0))))
1421   str)
1422
1423 (defun gnus-simplify-all-whitespace (str)
1424   "Remove all whitespace from STR."
1425   (while (string-match "[ \t\n]+" str)
1426     (setq str (replace-match "" nil nil str)))
1427   str)
1428
1429 (defsubst gnus-simplify-subject-re (subject)
1430   "Remove \"Re:\" from subject lines."
1431   (if (string-match message-subject-re-regexp subject)
1432       (substring subject (match-end 0))
1433     subject))
1434
1435 (defun gnus-simplify-subject (subject &optional re-only)
1436   "Remove `Re:' and words in parentheses.
1437 If RE-ONLY is non-nil, strip leading `Re:'s only."
1438   (let ((case-fold-search t))           ;Ignore case.
1439     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1440     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1441       (setq subject (substring subject (match-end 0))))
1442     ;; Remove uninteresting prefixes.
1443     (when (and (not re-only)
1444                gnus-simplify-ignored-prefixes
1445                (string-match gnus-simplify-ignored-prefixes subject))
1446       (setq subject (substring subject (match-end 0))))
1447     ;; Remove words in parentheses from end.
1448     (unless re-only
1449       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1450         (setq subject (substring subject 0 (match-beginning 0)))))
1451     ;; Return subject string.
1452     subject))
1453
1454 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1455 ;; all whitespace.
1456 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1457   (goto-char (point-min))
1458   (while (re-search-forward regexp nil t)
1459     (replace-match (or newtext ""))))
1460
1461 (defun gnus-simplify-buffer-fuzzy ()
1462   "Simplify string in the buffer fuzzily.
1463 The string in the accessible portion of the current buffer is simplified.
1464 It is assumed to be a single-line subject.
1465 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1466 matter is removed.  Additional things can be deleted by setting
1467 `gnus-simplify-subject-fuzzy-regexp'."
1468   (let ((case-fold-search t)
1469         (modified-tick))
1470     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1471
1472     (while (not (eq modified-tick (buffer-modified-tick)))
1473       (setq modified-tick (buffer-modified-tick))
1474       (cond
1475        ((listp gnus-simplify-subject-fuzzy-regexp)
1476         (mapcar 'gnus-simplify-buffer-fuzzy-step
1477                 gnus-simplify-subject-fuzzy-regexp))
1478        (gnus-simplify-subject-fuzzy-regexp
1479         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1480       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1481       (gnus-simplify-buffer-fuzzy-step
1482        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1483       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1484
1485     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1486     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1487     (gnus-simplify-buffer-fuzzy-step " $")
1488     (gnus-simplify-buffer-fuzzy-step "^ +")))
1489
1490 (defun gnus-simplify-subject-fuzzy (subject)
1491   "Simplify a subject string fuzzily.
1492 See `gnus-simplify-buffer-fuzzy' for details."
1493   (save-excursion
1494     (gnus-set-work-buffer)
1495     (let ((case-fold-search t))
1496       ;; Remove uninteresting prefixes.
1497       (when (and gnus-simplify-ignored-prefixes
1498                  (string-match gnus-simplify-ignored-prefixes subject))
1499         (setq subject (substring subject (match-end 0))))
1500       (insert subject)
1501       (inline (gnus-simplify-buffer-fuzzy))
1502       (buffer-string))))
1503
1504 (defsubst gnus-simplify-subject-fully (subject)
1505   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1506   (cond
1507    (gnus-simplify-subject-functions
1508     (gnus-map-function gnus-simplify-subject-functions subject))
1509    ((null gnus-summary-gather-subject-limit)
1510     (gnus-simplify-subject-re subject))
1511    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1512     (gnus-simplify-subject-fuzzy subject))
1513    ((numberp gnus-summary-gather-subject-limit)
1514     (gnus-limit-string (gnus-simplify-subject-re subject)
1515                        gnus-summary-gather-subject-limit))
1516    (t
1517     subject)))
1518
1519 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1520   "Check whether two subjects are equal.
1521 If optional argument simple-first is t, first argument is already
1522 simplified."
1523   (cond
1524    ((null simple-first)
1525     (equal (gnus-simplify-subject-fully s1)
1526            (gnus-simplify-subject-fully s2)))
1527    (t
1528     (equal s1
1529            (gnus-simplify-subject-fully s2)))))
1530
1531 (defun gnus-summary-bubble-group ()
1532   "Increase the score of the current group.
1533 This is a handy function to add to `gnus-summary-exit-hook' to
1534 increase the score of each group you read."
1535   (gnus-group-add-score gnus-newsgroup-name))
1536
1537 \f
1538 ;;;
1539 ;;; Gnus summary mode
1540 ;;;
1541
1542 (put 'gnus-summary-mode 'mode-class 'special)
1543
1544 (defvar gnus-article-commands-menu)
1545
1546 ;; Non-orthogonal keys
1547
1548 (gnus-define-keys gnus-summary-mode-map
1549   " " gnus-summary-next-page
1550   "\177" gnus-summary-prev-page
1551   [delete] gnus-summary-prev-page
1552   [backspace] gnus-summary-prev-page
1553   "\r" gnus-summary-scroll-up
1554   "\M-\r" gnus-summary-scroll-down
1555   "n" gnus-summary-next-unread-article
1556   "p" gnus-summary-prev-unread-article
1557   "N" gnus-summary-next-article
1558   "P" gnus-summary-prev-article
1559   "\M-\C-n" gnus-summary-next-same-subject
1560   "\M-\C-p" gnus-summary-prev-same-subject
1561   "\M-n" gnus-summary-next-unread-subject
1562   "\M-p" gnus-summary-prev-unread-subject
1563   "." gnus-summary-first-unread-article
1564   "," gnus-summary-best-unread-article
1565   "\M-s" gnus-summary-search-article-forward
1566   "\M-r" gnus-summary-search-article-backward
1567   "<" gnus-summary-beginning-of-article
1568   ">" gnus-summary-end-of-article
1569   "j" gnus-summary-goto-article
1570   "^" gnus-summary-refer-parent-article
1571   "\M-^" gnus-summary-refer-article
1572   "u" gnus-summary-tick-article-forward
1573   "!" gnus-summary-tick-article-forward
1574   "U" gnus-summary-tick-article-backward
1575   "d" gnus-summary-mark-as-read-forward
1576   "D" gnus-summary-mark-as-read-backward
1577   "E" gnus-summary-mark-as-expirable
1578   "\M-u" gnus-summary-clear-mark-forward
1579   "\M-U" gnus-summary-clear-mark-backward
1580   "k" gnus-summary-kill-same-subject-and-select
1581   "\C-k" gnus-summary-kill-same-subject
1582   "\M-\C-k" gnus-summary-kill-thread
1583   "\M-\C-l" gnus-summary-lower-thread
1584   "e" gnus-summary-edit-article
1585   "#" gnus-summary-mark-as-processable
1586   "\M-#" gnus-summary-unmark-as-processable
1587   "\M-\C-t" gnus-summary-toggle-threads
1588   "\M-\C-s" gnus-summary-show-thread
1589   "\M-\C-h" gnus-summary-hide-thread
1590   "\M-\C-f" gnus-summary-next-thread
1591   "\M-\C-b" gnus-summary-prev-thread
1592   [(meta down)] gnus-summary-next-thread
1593   [(meta up)] gnus-summary-prev-thread
1594   "\M-\C-u" gnus-summary-up-thread
1595   "\M-\C-d" gnus-summary-down-thread
1596   "&" gnus-summary-execute-command
1597   "c" gnus-summary-catchup-and-exit
1598   "\C-w" gnus-summary-mark-region-as-read
1599   "\C-t" gnus-summary-toggle-truncation
1600   "?" gnus-summary-mark-as-dormant
1601   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1602   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1603   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1604   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1605   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1606   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1607   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1608   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1609   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1610   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1611   "=" gnus-summary-expand-window
1612   "\C-x\C-s" gnus-summary-reselect-current-group
1613   "\M-g" gnus-summary-rescan-group
1614   "w" gnus-summary-stop-page-breaking
1615   "\C-c\C-r" gnus-summary-caesar-message
1616   "f" gnus-summary-followup
1617   "F" gnus-summary-followup-with-original
1618   "C" gnus-summary-cancel-article
1619   "r" gnus-summary-reply
1620   "R" gnus-summary-reply-with-original
1621   "\C-c\C-f" gnus-summary-mail-forward
1622   "o" gnus-summary-save-article
1623   "\C-o" gnus-summary-save-article-mail
1624   "|" gnus-summary-pipe-output
1625   "\M-k" gnus-summary-edit-local-kill
1626   "\M-K" gnus-summary-edit-global-kill
1627   ;; "V" gnus-version
1628   "\C-c\C-d" gnus-summary-describe-group
1629   "q" gnus-summary-exit
1630   "Q" gnus-summary-exit-no-update
1631   "\C-c\C-i" gnus-info-find-node
1632   gnus-mouse-2 gnus-mouse-pick-article
1633   "m" gnus-summary-mail-other-window
1634   "a" gnus-summary-post-news
1635   "i" gnus-summary-news-other-window
1636   "x" gnus-summary-limit-to-unread
1637   "s" gnus-summary-isearch-article
1638   "t" gnus-summary-toggle-header
1639   "g" gnus-summary-show-article
1640   "l" gnus-summary-goto-last-article
1641   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1642   "\C-d" gnus-summary-enter-digest-group
1643   "\M-\C-d" gnus-summary-read-document
1644   "\M-\C-e" gnus-summary-edit-parameters
1645   "\M-\C-a" gnus-summary-customize-parameters
1646   "\C-c\C-b" gnus-bug
1647   "*" gnus-cache-enter-article
1648   "\M-*" gnus-cache-remove-article
1649   "\M-&" gnus-summary-universal-argument
1650   "\C-l" gnus-recenter
1651   "I" gnus-summary-increase-score
1652   "L" gnus-summary-lower-score
1653   "\M-i" gnus-symbolic-argument
1654   "h" gnus-summary-select-article-buffer
1655
1656   "b" gnus-article-view-part
1657   "\M-t" gnus-summary-toggle-display-buttonized
1658
1659   "V" gnus-summary-score-map
1660   "X" gnus-uu-extract-map
1661   "S" gnus-summary-send-map)
1662
1663 ;; Sort of orthogonal keymap
1664 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1665   "t" gnus-summary-tick-article-forward
1666   "!" gnus-summary-tick-article-forward
1667   "d" gnus-summary-mark-as-read-forward
1668   "r" gnus-summary-mark-as-read-forward
1669   "c" gnus-summary-clear-mark-forward
1670   " " gnus-summary-clear-mark-forward
1671   "e" gnus-summary-mark-as-expirable
1672   "x" gnus-summary-mark-as-expirable
1673   "?" gnus-summary-mark-as-dormant
1674   "b" gnus-summary-set-bookmark
1675   "B" gnus-summary-remove-bookmark
1676   "#" gnus-summary-mark-as-processable
1677   "\M-#" gnus-summary-unmark-as-processable
1678   "S" gnus-summary-limit-include-expunged
1679   "C" gnus-summary-catchup
1680   "H" gnus-summary-catchup-to-here
1681   "h" gnus-summary-catchup-from-here
1682   "\C-c" gnus-summary-catchup-all
1683   "k" gnus-summary-kill-same-subject-and-select
1684   "K" gnus-summary-kill-same-subject
1685   "P" gnus-uu-mark-map)
1686
1687 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1688   "c" gnus-summary-clear-above
1689   "u" gnus-summary-tick-above
1690   "m" gnus-summary-mark-above
1691   "k" gnus-summary-kill-below)
1692
1693 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1694   "/" gnus-summary-limit-to-subject
1695   "n" gnus-summary-limit-to-articles
1696   "w" gnus-summary-pop-limit
1697   "s" gnus-summary-limit-to-subject
1698   "a" gnus-summary-limit-to-author
1699   "u" gnus-summary-limit-to-unread
1700   "m" gnus-summary-limit-to-marks
1701   "M" gnus-summary-limit-exclude-marks
1702   "v" gnus-summary-limit-to-score
1703   "*" gnus-summary-limit-include-cached
1704   "D" gnus-summary-limit-include-dormant
1705   "T" gnus-summary-limit-include-thread
1706   "d" gnus-summary-limit-exclude-dormant
1707   "t" gnus-summary-limit-to-age
1708   "." gnus-summary-limit-to-unseen
1709   "x" gnus-summary-limit-to-extra
1710   "p" gnus-summary-limit-to-display-predicate
1711   "E" gnus-summary-limit-include-expunged
1712   "c" gnus-summary-limit-exclude-childless-dormant
1713   "C" gnus-summary-limit-mark-excluded-as-read
1714   "o" gnus-summary-insert-old-articles
1715   "N" gnus-summary-insert-new-articles)
1716
1717 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1718   "n" gnus-summary-next-unread-article
1719   "p" gnus-summary-prev-unread-article
1720   "N" gnus-summary-next-article
1721   "P" gnus-summary-prev-article
1722   "\C-n" gnus-summary-next-same-subject
1723   "\C-p" gnus-summary-prev-same-subject
1724   "\M-n" gnus-summary-next-unread-subject
1725   "\M-p" gnus-summary-prev-unread-subject
1726   "f" gnus-summary-first-unread-article
1727   "b" gnus-summary-best-unread-article
1728   "j" gnus-summary-goto-article
1729   "g" gnus-summary-goto-subject
1730   "l" gnus-summary-goto-last-article
1731   "o" gnus-summary-pop-article)
1732
1733 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1734   "k" gnus-summary-kill-thread
1735   "l" gnus-summary-lower-thread
1736   "i" gnus-summary-raise-thread
1737   "T" gnus-summary-toggle-threads
1738   "t" gnus-summary-rethread-current
1739   "^" gnus-summary-reparent-thread
1740   "s" gnus-summary-show-thread
1741   "S" gnus-summary-show-all-threads
1742   "h" gnus-summary-hide-thread
1743   "H" gnus-summary-hide-all-threads
1744   "n" gnus-summary-next-thread
1745   "p" gnus-summary-prev-thread
1746   "u" gnus-summary-up-thread
1747   "o" gnus-summary-top-thread
1748   "d" gnus-summary-down-thread
1749   "#" gnus-uu-mark-thread
1750   "\M-#" gnus-uu-unmark-thread)
1751
1752 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1753   "g" gnus-summary-prepare
1754   "c" gnus-summary-insert-cached-articles
1755   "d" gnus-summary-insert-dormant-articles)
1756
1757 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1758   "c" gnus-summary-catchup-and-exit
1759   "C" gnus-summary-catchup-all-and-exit
1760   "E" gnus-summary-exit-no-update
1761   "Q" gnus-summary-exit
1762   "Z" gnus-summary-exit
1763   "n" gnus-summary-catchup-and-goto-next-group
1764   "R" gnus-summary-reselect-current-group
1765   "G" gnus-summary-rescan-group
1766   "N" gnus-summary-next-group
1767   "s" gnus-summary-save-newsrc
1768   "P" gnus-summary-prev-group)
1769
1770 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1771   " " gnus-summary-next-page
1772   "n" gnus-summary-next-page
1773   "\177" gnus-summary-prev-page
1774   [delete] gnus-summary-prev-page
1775   "p" gnus-summary-prev-page
1776   "\r" gnus-summary-scroll-up
1777   "\M-\r" gnus-summary-scroll-down
1778   "<" gnus-summary-beginning-of-article
1779   ">" gnus-summary-end-of-article
1780   "b" gnus-summary-beginning-of-article
1781   "e" gnus-summary-end-of-article
1782   "^" gnus-summary-refer-parent-article
1783   "r" gnus-summary-refer-parent-article
1784   "D" gnus-summary-enter-digest-group
1785   "R" gnus-summary-refer-references
1786   "T" gnus-summary-refer-thread
1787   "g" gnus-summary-show-article
1788   "s" gnus-summary-isearch-article
1789   "P" gnus-summary-print-article
1790   "M" gnus-mailing-list-insinuate
1791   "t" gnus-article-babel)
1792
1793 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1794   "b" gnus-article-add-buttons
1795   "B" gnus-article-add-buttons-to-head
1796   "o" gnus-article-treat-overstrike
1797   "e" gnus-article-emphasize
1798   "w" gnus-article-fill-cited-article
1799   "Q" gnus-article-fill-long-lines
1800   "C" gnus-article-capitalize-sentences
1801   "c" gnus-article-remove-cr
1802   "q" gnus-article-de-quoted-unreadable
1803   "6" gnus-article-de-base64-unreadable
1804   "Z" gnus-article-decode-HZ
1805   "h" gnus-article-wash-html
1806   "u" gnus-article-unsplit-urls
1807   "s" gnus-summary-force-verify-and-decrypt
1808   "f" gnus-article-display-x-face
1809   "l" gnus-summary-stop-page-breaking
1810   "r" gnus-summary-caesar-message
1811   "m" gnus-summary-morse-message
1812   "t" gnus-summary-toggle-header
1813   "g" gnus-treat-smiley
1814   "v" gnus-summary-verbose-headers
1815   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1816   "p" gnus-article-verify-x-pgp-sig
1817   "d" gnus-article-treat-dumbquotes)
1818
1819 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1820   ;; mnemonic: deuglif*Y*
1821   "u" gnus-article-outlook-unwrap-lines
1822   "a" gnus-article-outlook-repair-attribution
1823   "c" gnus-article-outlook-rearrange-citation
1824   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1825
1826 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1827   "a" gnus-article-hide
1828   "h" gnus-article-hide-headers
1829   "b" gnus-article-hide-boring-headers
1830   "s" gnus-article-hide-signature
1831   "c" gnus-article-hide-citation
1832   "C" gnus-article-hide-citation-in-followups
1833   "l" gnus-article-hide-list-identifiers
1834   "B" gnus-article-strip-banner
1835   "P" gnus-article-hide-pem
1836   "\C-c" gnus-article-hide-citation-maybe)
1837
1838 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1839   "a" gnus-article-highlight
1840   "h" gnus-article-highlight-headers
1841   "c" gnus-article-highlight-citation
1842   "s" gnus-article-highlight-signature)
1843
1844 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1845   "f" gnus-article-treat-fold-headers
1846   "u" gnus-article-treat-unfold-headers
1847   "n" gnus-article-treat-fold-newsgroups)
1848
1849 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1850   "x" gnus-article-display-x-face
1851   "d" gnus-article-display-face
1852   "s" gnus-treat-smiley
1853   "D" gnus-article-remove-images
1854   "f" gnus-treat-from-picon
1855   "m" gnus-treat-mail-picon
1856   "n" gnus-treat-newsgroups-picon)
1857
1858 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1859   "w" gnus-article-decode-mime-words
1860   "c" gnus-article-decode-charset
1861   "v" gnus-mime-view-all-parts
1862   "b" gnus-article-view-part)
1863
1864 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1865   "z" gnus-article-date-ut
1866   "u" gnus-article-date-ut
1867   "l" gnus-article-date-local
1868   "p" gnus-article-date-english
1869   "e" gnus-article-date-lapsed
1870   "o" gnus-article-date-original
1871   "i" gnus-article-date-iso8601
1872   "s" gnus-article-date-user)
1873
1874 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1875   "t" gnus-article-remove-trailing-blank-lines
1876   "l" gnus-article-strip-leading-blank-lines
1877   "m" gnus-article-strip-multiple-blank-lines
1878   "a" gnus-article-strip-blank-lines
1879   "A" gnus-article-strip-all-blank-lines
1880   "s" gnus-article-strip-leading-space
1881   "e" gnus-article-strip-trailing-space
1882   "w" gnus-article-remove-leading-whitespace)
1883
1884 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1885   "v" gnus-version
1886   "f" gnus-summary-fetch-faq
1887   "d" gnus-summary-describe-group
1888   "h" gnus-summary-describe-briefly
1889   "i" gnus-info-find-node
1890   "c" gnus-group-fetch-charter
1891   "C" gnus-group-fetch-control)
1892
1893 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1894   "e" gnus-summary-expire-articles
1895   "\M-\C-e" gnus-summary-expire-articles-now
1896   "\177" gnus-summary-delete-article
1897   [delete] gnus-summary-delete-article
1898   [backspace] gnus-summary-delete-article
1899   "m" gnus-summary-move-article
1900   "r" gnus-summary-respool-article
1901   "w" gnus-summary-edit-article
1902   "c" gnus-summary-copy-article
1903   "B" gnus-summary-crosspost-article
1904   "q" gnus-summary-respool-query
1905   "t" gnus-summary-respool-trace
1906   "i" gnus-summary-import-article
1907   "I" gnus-summary-create-article
1908   "p" gnus-summary-article-posted-p)
1909
1910 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1911   "o" gnus-summary-save-article
1912   "m" gnus-summary-save-article-mail
1913   "F" gnus-summary-write-article-file
1914   "r" gnus-summary-save-article-rmail
1915   "f" gnus-summary-save-article-file
1916   "b" gnus-summary-save-article-body-file
1917   "h" gnus-summary-save-article-folder
1918   "v" gnus-summary-save-article-vm
1919   "p" gnus-summary-pipe-output
1920   "P" gnus-summary-muttprint
1921   "s" gnus-soup-add-article)
1922
1923 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1924   "b" gnus-summary-display-buttonized
1925   "m" gnus-summary-repair-multipart
1926   "v" gnus-article-view-part
1927   "o" gnus-article-save-part
1928   "c" gnus-article-copy-part
1929   "C" gnus-article-view-part-as-charset
1930   "e" gnus-article-view-part-externally
1931   "E" gnus-article-encrypt-body
1932   "i" gnus-article-inline-part
1933   "|" gnus-article-pipe-part)
1934
1935 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1936   "p" gnus-summary-mark-as-processable
1937   "u" gnus-summary-unmark-as-processable
1938   "U" gnus-summary-unmark-all-processable
1939   "v" gnus-uu-mark-over
1940   "s" gnus-uu-mark-series
1941   "r" gnus-uu-mark-region
1942   "g" gnus-uu-unmark-region
1943   "R" gnus-uu-mark-by-regexp
1944   "G" gnus-uu-unmark-by-regexp
1945   "t" gnus-uu-mark-thread
1946   "T" gnus-uu-unmark-thread
1947   "a" gnus-uu-mark-all
1948   "b" gnus-uu-mark-buffer
1949   "S" gnus-uu-mark-sparse
1950   "k" gnus-summary-kill-process-mark
1951   "y" gnus-summary-yank-process-mark
1952   "w" gnus-summary-save-process-mark
1953   "i" gnus-uu-invert-processable)
1954
1955 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1956   ;;"x" gnus-uu-extract-any
1957   "m" gnus-summary-save-parts
1958   "u" gnus-uu-decode-uu
1959   "U" gnus-uu-decode-uu-and-save
1960   "s" gnus-uu-decode-unshar
1961   "S" gnus-uu-decode-unshar-and-save
1962   "o" gnus-uu-decode-save
1963   "O" gnus-uu-decode-save
1964   "b" gnus-uu-decode-binhex
1965   "B" gnus-uu-decode-binhex
1966   "p" gnus-uu-decode-postscript
1967   "P" gnus-uu-decode-postscript-and-save)
1968
1969 (gnus-define-keys
1970     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
1971   "u" gnus-uu-decode-uu-view
1972   "U" gnus-uu-decode-uu-and-save-view
1973   "s" gnus-uu-decode-unshar-view
1974   "S" gnus-uu-decode-unshar-and-save-view
1975   "o" gnus-uu-decode-save-view
1976   "O" gnus-uu-decode-save-view
1977   "b" gnus-uu-decode-binhex-view
1978   "B" gnus-uu-decode-binhex-view
1979   "p" gnus-uu-decode-postscript-view
1980   "P" gnus-uu-decode-postscript-and-save-view)
1981
1982 (defvar gnus-article-post-menu nil)
1983
1984 (defconst gnus-summary-menu-maxlen 20)
1985
1986 (defun gnus-summary-menu-split (menu)
1987   ;; If we have lots of elements, divide them into groups of 20
1988   ;; and make a pane (or submenu) for each one.
1989   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
1990       (let ((menu menu) sublists next
1991             (i 1))
1992         (while menu
1993           ;; Pull off the next gnus-summary-menu-maxlen elements
1994           ;; and make them the next element of sublist.
1995           (setq next (nthcdr gnus-summary-menu-maxlen menu))
1996           (if next
1997               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
1998                       nil))
1999           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2000                                              (aref (car (last menu)) 0)) menu)
2001                                sublists))
2002           (setq i (1+ i))
2003           (setq menu next))
2004         (nreverse sublists))
2005     ;; Few elements--put them all in one pane.
2006     menu))
2007
2008 (defun gnus-summary-make-menu-bar ()
2009   (gnus-turn-off-edit-menu 'summary)
2010
2011   (unless (boundp 'gnus-summary-misc-menu)
2012
2013     (easy-menu-define
2014       gnus-summary-kill-menu gnus-summary-mode-map ""
2015       (cons
2016        "Score"
2017        (nconc
2018         (list
2019          ["Customize" gnus-score-customize t])
2020         (gnus-make-score-map 'increase)
2021         (gnus-make-score-map 'lower)
2022         '(("Mark"
2023            ["Kill below" gnus-summary-kill-below t]
2024            ["Mark above" gnus-summary-mark-above t]
2025            ["Tick above" gnus-summary-tick-above t]
2026            ["Clear above" gnus-summary-clear-above t])
2027           ["Current score" gnus-summary-current-score t]
2028           ["Set score" gnus-summary-set-score t]
2029           ["Switch current score file..." gnus-score-change-score-file t]
2030           ["Set mark below..." gnus-score-set-mark-below t]
2031           ["Set expunge below..." gnus-score-set-expunge-below t]
2032           ["Edit current score file" gnus-score-edit-current-scores t]
2033           ["Edit score file" gnus-score-edit-file t]
2034           ["Trace score" gnus-score-find-trace t]
2035           ["Find words" gnus-score-find-favourite-words t]
2036           ["Rescore buffer" gnus-summary-rescore t]
2037           ["Increase score..." gnus-summary-increase-score t]
2038           ["Lower score..." gnus-summary-lower-score t]))))
2039
2040     ;; Define both the Article menu in the summary buffer and the
2041     ;; equivalent Commands menu in the article buffer here for
2042     ;; consistency.
2043     (let ((innards
2044            `(("Hide"
2045               ["All" gnus-article-hide t]
2046               ["Headers" gnus-article-hide-headers t]
2047               ["Signature" gnus-article-hide-signature t]
2048               ["Citation" gnus-article-hide-citation t]
2049               ["List identifiers" gnus-article-hide-list-identifiers t]
2050               ["Banner" gnus-article-strip-banner t]
2051               ["Boring headers" gnus-article-hide-boring-headers t])
2052              ("Highlight"
2053               ["All" gnus-article-highlight t]
2054               ["Headers" gnus-article-highlight-headers t]
2055               ["Signature" gnus-article-highlight-signature t]
2056               ["Citation" gnus-article-highlight-citation t])
2057              ("MIME"
2058               ["Words" gnus-article-decode-mime-words t]
2059               ["Charset" gnus-article-decode-charset t]
2060               ["QP" gnus-article-de-quoted-unreadable t]
2061               ["Base64" gnus-article-de-base64-unreadable t]
2062               ["View MIME buttons" gnus-summary-display-buttonized t]
2063               ["View all" gnus-mime-view-all-parts t]
2064               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2065               ["Encrypt body" gnus-article-encrypt-body t]
2066               ["Extract all parts" gnus-summary-save-parts t])
2067              ("Date"
2068               ["Local" gnus-article-date-local t]
2069               ["ISO8601" gnus-article-date-iso8601 t]
2070               ["UT" gnus-article-date-ut t]
2071               ["Original" gnus-article-date-original t]
2072               ["Lapsed" gnus-article-date-lapsed t]
2073               ["User-defined" gnus-article-date-user t])
2074              ("Display"
2075               ["Remove images" gnus-article-remove-images t]
2076               ["Toggle smiley" gnus-treat-smiley t]
2077               ["Show X-Face" gnus-article-display-x-face t]
2078               ["Show picons in From" gnus-treat-from-picon t]
2079               ["Show picons in mail headers" gnus-treat-mail-picon t]
2080               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2081               ("View as different encoding"
2082                ,@(gnus-summary-menu-split
2083                   (mapcar
2084                    (lambda (cs)
2085                      ;; Since easymenu under FSF Emacs doesn't allow lambda
2086                      ;; forms for menu commands, we should provide intern'ed
2087                      ;; function symbols.
2088                      (let ((command (intern (format "\
2089 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2090                        (fset command
2091                              `(lambda ()
2092                                 (interactive)
2093                                 (let ((gnus-summary-show-article-charset-alist
2094                                        '((1 . ,cs))))
2095                                   (gnus-summary-show-article 1))))
2096                        `[,(symbol-name cs) ,command t]))
2097                    (sort (if (fboundp 'coding-system-list)
2098                              (coding-system-list)
2099                            (mapcar 'car mm-mime-mule-charset-alist))
2100                          'string<)))))
2101              ("Washing"
2102               ("Remove Blanks"
2103                ["Leading" gnus-article-strip-leading-blank-lines t]
2104                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2105                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2106                ["All of the above" gnus-article-strip-blank-lines t]
2107                ["All" gnus-article-strip-all-blank-lines t]
2108                ["Leading space" gnus-article-strip-leading-space t]
2109                ["Trailing space" gnus-article-strip-trailing-space t]
2110                ["Leading space in headers"
2111                 gnus-article-remove-leading-whitespace t])
2112               ["Overstrike" gnus-article-treat-overstrike t]
2113               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2114               ["Emphasis" gnus-article-emphasize t]
2115               ["Word wrap" gnus-article-fill-cited-article t]
2116               ["Fill long lines" gnus-article-fill-long-lines t]
2117               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2118               ["CR" gnus-article-remove-cr t]
2119               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2120               ["Base64" gnus-article-de-base64-unreadable t]
2121               ["Rot 13" gnus-summary-caesar-message
2122                ,@(if (featurep 'xemacs) '(t)
2123                    '(:help "\"Caesar rotate\" article by 13"))]
2124               ["Morse decode" gnus-summary-morse-message t]
2125               ["Unix pipe..." gnus-summary-pipe-message t]
2126               ["Add buttons" gnus-article-add-buttons t]
2127               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2128               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2129               ["Verbose header" gnus-summary-verbose-headers t]
2130               ["Toggle header" gnus-summary-toggle-header t]
2131               ["Unfold headers" gnus-article-treat-unfold-headers t]
2132               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2133               ["Html" gnus-article-wash-html t]
2134               ["URLs" gnus-article-unsplit-urls t]
2135               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2136               ["HZ" gnus-article-decode-HZ t]
2137               ("(Outlook) Deuglify"
2138                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2139                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2140                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2141                ["Full (Outlook) deuglify"
2142                 gnus-article-outlook-deuglify-article t])
2143               )
2144              ("Output"
2145               ["Save in default format" gnus-summary-save-article
2146                ,@(if (featurep 'xemacs) '(t)
2147                    '(:help "Save article using default method"))]
2148               ["Save in file" gnus-summary-save-article-file
2149                ,@(if (featurep 'xemacs) '(t)
2150                    '(:help "Save article in file"))]
2151               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2152               ["Save in MH folder" gnus-summary-save-article-folder t]
2153               ["Save in VM folder" gnus-summary-save-article-vm t]
2154               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2155               ["Save body in file" gnus-summary-save-article-body-file t]
2156               ["Pipe through a filter" gnus-summary-pipe-output t]
2157               ["Add to SOUP packet" gnus-soup-add-article t]
2158               ["Print with Muttprint" gnus-summary-muttprint t]
2159               ["Print" gnus-summary-print-article t])
2160              ("Backend"
2161               ["Respool article..." gnus-summary-respool-article t]
2162               ["Move article..." gnus-summary-move-article
2163                (gnus-check-backend-function
2164                 'request-move-article gnus-newsgroup-name)]
2165               ["Copy article..." gnus-summary-copy-article t]
2166               ["Crosspost article..." gnus-summary-crosspost-article
2167                (gnus-check-backend-function
2168                 'request-replace-article gnus-newsgroup-name)]
2169               ["Import file..." gnus-summary-import-article t]
2170               ["Create article..." gnus-summary-create-article t]
2171               ["Check if posted" gnus-summary-article-posted-p t]
2172               ["Edit article" gnus-summary-edit-article
2173                (not (gnus-group-read-only-p))]
2174               ["Delete article" gnus-summary-delete-article
2175                (gnus-check-backend-function
2176                 'request-expire-articles gnus-newsgroup-name)]
2177               ["Query respool" gnus-summary-respool-query t]
2178               ["Trace respool" gnus-summary-respool-trace t]
2179               ["Delete expirable articles" gnus-summary-expire-articles-now
2180                (gnus-check-backend-function
2181                 'request-expire-articles gnus-newsgroup-name)])
2182              ("Extract"
2183               ["Uudecode" gnus-uu-decode-uu
2184                ,@(if (featurep 'xemacs) '(t)
2185                    '(:help "Decode uuencoded article(s)"))]
2186               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2187               ["Unshar" gnus-uu-decode-unshar t]
2188               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2189               ["Save" gnus-uu-decode-save t]
2190               ["Binhex" gnus-uu-decode-binhex t]
2191               ["Postscript" gnus-uu-decode-postscript t]
2192               ["All MIME parts" gnus-summary-save-parts t])
2193              ("Cache"
2194               ["Enter article" gnus-cache-enter-article t]
2195               ["Remove article" gnus-cache-remove-article t])
2196              ["Translate" gnus-article-babel t]
2197              ["Select article buffer" gnus-summary-select-article-buffer t]
2198              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2199              ["Isearch article..." gnus-summary-isearch-article t]
2200              ["Beginning of the article" gnus-summary-beginning-of-article t]
2201              ["End of the article" gnus-summary-end-of-article t]
2202              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2203              ["Fetch referenced articles" gnus-summary-refer-references t]
2204              ["Fetch current thread" gnus-summary-refer-thread t]
2205              ["Fetch article with id..." gnus-summary-refer-article t]
2206              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2207              ["Redisplay" gnus-summary-show-article t]
2208              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2209       (easy-menu-define
2210         gnus-summary-article-menu gnus-summary-mode-map ""
2211         (cons "Article" innards))
2212
2213       (if (not (keymapp gnus-summary-article-menu))
2214           (easy-menu-define
2215             gnus-article-commands-menu gnus-article-mode-map ""
2216             (cons "Commands" innards))
2217         ;; in Emacs, don't share menu.
2218         (setq gnus-article-commands-menu
2219               (copy-keymap gnus-summary-article-menu))
2220         (define-key gnus-article-mode-map [menu-bar commands]
2221           (cons "Commands" gnus-article-commands-menu))))
2222
2223     (easy-menu-define
2224       gnus-summary-thread-menu gnus-summary-mode-map ""
2225       '("Threads"
2226         ["Find all messages in thread" gnus-summary-refer-thread t]
2227         ["Toggle threading" gnus-summary-toggle-threads t]
2228         ["Hide threads" gnus-summary-hide-all-threads t]
2229         ["Show threads" gnus-summary-show-all-threads t]
2230         ["Hide thread" gnus-summary-hide-thread t]
2231         ["Show thread" gnus-summary-show-thread t]
2232         ["Go to next thread" gnus-summary-next-thread t]
2233         ["Go to previous thread" gnus-summary-prev-thread t]
2234         ["Go down thread" gnus-summary-down-thread t]
2235         ["Go up thread" gnus-summary-up-thread t]
2236         ["Top of thread" gnus-summary-top-thread t]
2237         ["Mark thread as read" gnus-summary-kill-thread t]
2238         ["Lower thread score" gnus-summary-lower-thread t]
2239         ["Raise thread score" gnus-summary-raise-thread t]
2240         ["Rethread current" gnus-summary-rethread-current t]))
2241
2242     (easy-menu-define
2243       gnus-summary-post-menu gnus-summary-mode-map ""
2244       `("Post"
2245         ["Send a message (mail or news)" gnus-summary-post-news
2246          ,@(if (featurep 'xemacs) '(t)
2247              '(:help "Post an article"))]
2248         ["Followup" gnus-summary-followup
2249          ,@(if (featurep 'xemacs) '(t)
2250              '(:help "Post followup to this article"))]
2251         ["Followup and yank" gnus-summary-followup-with-original
2252          ,@(if (featurep 'xemacs) '(t)
2253              '(:help "Post followup to this article, quoting its contents"))]
2254         ["Supersede article" gnus-summary-supersede-article t]
2255         ["Cancel article" gnus-summary-cancel-article
2256          ,@(if (featurep 'xemacs) '(t)
2257              '(:help "Cancel an article you posted"))]
2258         ["Reply" gnus-summary-reply t]
2259         ["Reply and yank" gnus-summary-reply-with-original t]
2260         ["Wide reply" gnus-summary-wide-reply t]
2261         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2262          ,@(if (featurep 'xemacs) '(t)
2263              '(:help "Mail a reply, quoting this article"))]
2264         ["Very wide reply" gnus-summary-very-wide-reply t]
2265         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2266          ,@(if (featurep 'xemacs) '(t)
2267              '(:help "Mail a very wide reply, quoting this article"))]
2268         ["Mail forward" gnus-summary-mail-forward t]
2269         ["Post forward" gnus-summary-post-forward t]
2270         ["Digest and mail" gnus-uu-digest-mail-forward t]
2271         ["Digest and post" gnus-uu-digest-post-forward t]
2272         ["Resend message" gnus-summary-resend-message t]
2273         ["Resend message edit" gnus-summary-resend-message-edit t]
2274         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2275         ["Send a mail" gnus-summary-mail-other-window t]
2276         ["Create a local message" gnus-summary-news-other-window t]
2277         ["Uuencode and post" gnus-uu-post-news
2278          ,@(if (featurep 'xemacs) '(t)
2279              '(:help "Post a uuencoded article"))]
2280         ["Followup via news" gnus-summary-followup-to-mail t]
2281         ["Followup via news and yank"
2282          gnus-summary-followup-to-mail-with-original t]
2283         ;;("Draft"
2284         ;;["Send" gnus-summary-send-draft t]
2285         ;;["Send bounced" gnus-resend-bounced-mail t])
2286         ))
2287
2288     (cond
2289      ((not (keymapp gnus-summary-post-menu))
2290       (setq gnus-article-post-menu gnus-summary-post-menu))
2291      ((not gnus-article-post-menu)
2292       ;; Don't share post menu.
2293       (setq gnus-article-post-menu
2294             (copy-keymap gnus-summary-post-menu))))
2295     (define-key gnus-article-mode-map [menu-bar post]
2296       (cons "Post" gnus-article-post-menu))
2297
2298     (easy-menu-define
2299       gnus-summary-misc-menu gnus-summary-mode-map ""
2300       `("Gnus"
2301         ("Mark Read"
2302          ["Mark as read" gnus-summary-mark-as-read-forward t]
2303          ["Mark same subject and select"
2304           gnus-summary-kill-same-subject-and-select t]
2305          ["Mark same subject" gnus-summary-kill-same-subject t]
2306          ["Catchup" gnus-summary-catchup
2307           ,@(if (featurep 'xemacs) '(t)
2308               '(:help "Mark unread articles in this group as read"))]
2309          ["Catchup all" gnus-summary-catchup-all t]
2310          ["Catchup to here" gnus-summary-catchup-to-here t]
2311          ["Catchup from here" gnus-summary-catchup-from-here t]
2312          ["Catchup region" gnus-summary-mark-region-as-read 
2313           (gnus-mark-active-p)]
2314          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2315         ("Mark Various"
2316          ["Tick" gnus-summary-tick-article-forward t]
2317          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2318          ["Remove marks" gnus-summary-clear-mark-forward t]
2319          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2320          ["Set bookmark" gnus-summary-set-bookmark t]
2321          ["Remove bookmark" gnus-summary-remove-bookmark t])
2322         ("Limit to"
2323          ["Marks..." gnus-summary-limit-to-marks t]
2324          ["Subject..." gnus-summary-limit-to-subject t]
2325          ["Author..." gnus-summary-limit-to-author t]
2326          ["Age..." gnus-summary-limit-to-age t]
2327          ["Extra..." gnus-summary-limit-to-extra t]
2328          ["Score..." gnus-summary-limit-to-score t]
2329          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2330          ["Unread" gnus-summary-limit-to-unread t]
2331          ["Unseen" gnus-summary-limit-to-unseen t]
2332          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2333          ["Next articles" gnus-summary-limit-to-articles t]
2334          ["Pop limit" gnus-summary-pop-limit t]
2335          ["Show dormant" gnus-summary-limit-include-dormant t]
2336          ["Hide childless dormant"
2337           gnus-summary-limit-exclude-childless-dormant t]
2338          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2339          ["Hide marked" gnus-summary-limit-exclude-marks t]
2340          ["Show expunged" gnus-summary-limit-include-expunged t])
2341         ("Process Mark"
2342          ["Set mark" gnus-summary-mark-as-processable t]
2343          ["Remove mark" gnus-summary-unmark-as-processable t]
2344          ["Remove all marks" gnus-summary-unmark-all-processable t]
2345          ["Mark above" gnus-uu-mark-over t]
2346          ["Mark series" gnus-uu-mark-series t]
2347          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2348          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2349          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2350          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2351          ["Mark all" gnus-uu-mark-all t]
2352          ["Mark buffer" gnus-uu-mark-buffer t]
2353          ["Mark sparse" gnus-uu-mark-sparse t]
2354          ["Mark thread" gnus-uu-mark-thread t]
2355          ["Unmark thread" gnus-uu-unmark-thread t]
2356          ("Process Mark Sets"
2357           ["Kill" gnus-summary-kill-process-mark t]
2358           ["Yank" gnus-summary-yank-process-mark
2359            gnus-newsgroup-process-stack]
2360           ["Save" gnus-summary-save-process-mark t]
2361           ["Run command on marked..." gnus-summary-universal-argument t]))
2362         ("Scroll article"
2363          ["Page forward" gnus-summary-next-page
2364           ,@(if (featurep 'xemacs) '(t)
2365               '(:help "Show next page of article"))]
2366          ["Page backward" gnus-summary-prev-page
2367           ,@(if (featurep 'xemacs) '(t)
2368               '(:help "Show previous page of article"))]
2369          ["Line forward" gnus-summary-scroll-up t])
2370         ("Move"
2371          ["Next unread article" gnus-summary-next-unread-article t]
2372          ["Previous unread article" gnus-summary-prev-unread-article t]
2373          ["Next article" gnus-summary-next-article t]
2374          ["Previous article" gnus-summary-prev-article t]
2375          ["Next unread subject" gnus-summary-next-unread-subject t]
2376          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2377          ["Next article same subject" gnus-summary-next-same-subject t]
2378          ["Previous article same subject" gnus-summary-prev-same-subject t]
2379          ["First unread article" gnus-summary-first-unread-article t]
2380          ["Best unread article" gnus-summary-best-unread-article t]
2381          ["Go to subject number..." gnus-summary-goto-subject t]
2382          ["Go to article number..." gnus-summary-goto-article t]
2383          ["Go to the last article" gnus-summary-goto-last-article t]
2384          ["Pop article off history" gnus-summary-pop-article t])
2385         ("Sort"
2386          ["Sort by number" gnus-summary-sort-by-number t]
2387          ["Sort by author" gnus-summary-sort-by-author t]
2388          ["Sort by subject" gnus-summary-sort-by-subject t]
2389          ["Sort by date" gnus-summary-sort-by-date t]
2390          ["Sort by score" gnus-summary-sort-by-score t]
2391          ["Sort by lines" gnus-summary-sort-by-lines t]
2392          ["Sort by characters" gnus-summary-sort-by-chars t]
2393          ["Randomize" gnus-summary-sort-by-random t]
2394          ["Original sort" gnus-summary-sort-by-original t])
2395         ("Help"
2396          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2397          ["Describe group" gnus-summary-describe-group t]
2398          ["Fetch charter" gnus-group-fetch-charter
2399           ,@(if (featurep 'xemacs) nil
2400               '(:help "Display the charter of the current group"))]
2401          ["Fetch control message" gnus-group-fetch-control
2402           ,@(if (featurep 'xemacs) nil
2403               '(:help "Display the archived control message for the current group"))]
2404          ["Read manual" gnus-info-find-node t])
2405         ("Modes"
2406          ["Pick and read" gnus-pick-mode t]
2407          ["Binary" gnus-binary-mode t])
2408         ("Regeneration"
2409          ["Regenerate" gnus-summary-prepare t]
2410          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2411          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2412          ["Toggle threading" gnus-summary-toggle-threads t])
2413         ["See old articles" gnus-summary-insert-old-articles t]
2414         ["See new articles" gnus-summary-insert-new-articles t]
2415         ["Filter articles..." gnus-summary-execute-command t]
2416         ["Run command on articles..." gnus-summary-universal-argument t]
2417         ["Search articles forward..." gnus-summary-search-article-forward t]
2418         ["Search articles backward..." gnus-summary-search-article-backward t]
2419         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2420         ["Expand window" gnus-summary-expand-window t]
2421         ["Expire expirable articles" gnus-summary-expire-articles
2422          (gnus-check-backend-function
2423           'request-expire-articles gnus-newsgroup-name)]
2424         ["Edit local kill file" gnus-summary-edit-local-kill t]
2425         ["Edit main kill file" gnus-summary-edit-global-kill t]
2426         ["Edit group parameters" gnus-summary-edit-parameters t]
2427         ["Customize group parameters" gnus-summary-customize-parameters t]
2428         ["Send a bug report" gnus-bug t]
2429         ("Exit"
2430          ["Catchup and exit" gnus-summary-catchup-and-exit
2431           ,@(if (featurep 'xemacs) '(t)
2432               '(:help "Mark unread articles in this group as read, then exit"))]
2433          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2434          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2435          ["Exit group" gnus-summary-exit
2436           ,@(if (featurep 'xemacs) '(t)
2437               '(:help "Exit current group, return to group selection mode"))]
2438          ["Exit group without updating" gnus-summary-exit-no-update t]
2439          ["Exit and goto next group" gnus-summary-next-group t]
2440          ["Exit and goto prev group" gnus-summary-prev-group t]
2441          ["Reselect group" gnus-summary-reselect-current-group t]
2442          ["Rescan group" gnus-summary-rescan-group t]
2443          ["Update dribble" gnus-summary-save-newsrc t])))
2444
2445     (gnus-run-hooks 'gnus-summary-menu-hook)))
2446
2447 (defvar gnus-summary-tool-bar-map nil)
2448
2449 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2450 (defun gnus-summary-make-tool-bar ()
2451   (if (and (fboundp 'tool-bar-add-item-from-menu)
2452            (default-value 'tool-bar-mode)
2453            (not gnus-summary-tool-bar-map))
2454       (setq gnus-summary-tool-bar-map
2455             (let ((tool-bar-map (make-sparse-keymap))
2456                   (load-path (mm-image-load-path)))
2457               (tool-bar-add-item-from-menu
2458                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2459               (tool-bar-add-item-from-menu
2460                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2461               (tool-bar-add-item-from-menu
2462                'gnus-summary-post-news "post" gnus-summary-mode-map)
2463               (tool-bar-add-item-from-menu
2464                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2465               (tool-bar-add-item-from-menu
2466                'gnus-summary-followup "followup" gnus-summary-mode-map)
2467               (tool-bar-add-item-from-menu
2468                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2469               (tool-bar-add-item-from-menu
2470                'gnus-summary-reply "reply" gnus-summary-mode-map)
2471               (tool-bar-add-item-from-menu
2472                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2473               (tool-bar-add-item-from-menu
2474                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2475               (tool-bar-add-item-from-menu
2476                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2477               (tool-bar-add-item-from-menu
2478                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2479               (tool-bar-add-item-from-menu
2480                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2481               (tool-bar-add-item-from-menu
2482                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2483               (tool-bar-add-item-from-menu
2484                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2485               (tool-bar-add-item-from-menu
2486                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2487               tool-bar-map)))
2488   (if gnus-summary-tool-bar-map
2489       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2490
2491 (defun gnus-score-set-default (var value)
2492   "A version of set that updates the GNU Emacs menu-bar."
2493   (set var value)
2494   ;; It is the message that forces the active status to be updated.
2495   (message ""))
2496
2497 (defun gnus-make-score-map (type)
2498   "Make a summary score map of type TYPE."
2499   (if t
2500       nil
2501     (let ((headers '(("author" "from" string)
2502                      ("subject" "subject" string)
2503                      ("article body" "body" string)
2504                      ("article head" "head" string)
2505                      ("xref" "xref" string)
2506                      ("extra header" "extra" string)
2507                      ("lines" "lines" number)
2508                      ("followups to author" "followup" string)))
2509           (types '((number ("less than" <)
2510                            ("greater than" >)
2511                            ("equal" =))
2512                    (string ("substring" s)
2513                            ("exact string" e)
2514                            ("fuzzy string" f)
2515                            ("regexp" r))))
2516           (perms '(("temporary" (current-time-string))
2517                    ("permanent" nil)
2518                    ("immediate" now)))
2519           header)
2520       (list
2521        (apply
2522         'nconc
2523         (list
2524          (if (eq type 'lower)
2525              "Lower score"
2526            "Increase score"))
2527         (let (outh)
2528           (while headers
2529             (setq header (car headers))
2530             (setq outh
2531                   (cons
2532                    (apply
2533                     'nconc
2534                     (list (car header))
2535                     (let ((ts (cdr (assoc (nth 2 header) types)))
2536                           outt)
2537                       (while ts
2538                         (setq outt
2539                               (cons
2540                                (apply
2541                                 'nconc
2542                                 (list (caar ts))
2543                                 (let ((ps perms)
2544                                       outp)
2545                                   (while ps
2546                                     (setq outp
2547                                           (cons
2548                                            (vector
2549                                             (caar ps)
2550                                             (list
2551                                              'gnus-summary-score-entry
2552                                              (nth 1 header)
2553                                              (if (or (string= (nth 1 header)
2554                                                               "head")
2555                                                      (string= (nth 1 header)
2556                                                               "body"))
2557                                                  ""
2558                                                (list 'gnus-summary-header
2559                                                      (nth 1 header)))
2560                                              (list 'quote (nth 1 (car ts)))
2561                                              (list 'gnus-score-delta-default
2562                                                    nil)
2563                                              (nth 1 (car ps))
2564                                              t)
2565                                             t)
2566                                            outp))
2567                                     (setq ps (cdr ps)))
2568                                   (list (nreverse outp))))
2569                                outt))
2570                         (setq ts (cdr ts)))
2571                       (list (nreverse outt))))
2572                    outh))
2573             (setq headers (cdr headers)))
2574           (list (nreverse outh))))))))
2575
2576 \f
2577
2578 (defun gnus-summary-mode (&optional group)
2579   "Major mode for reading articles.
2580
2581 All normal editing commands are switched off.
2582 \\<gnus-summary-mode-map>
2583 Each line in this buffer represents one article.  To read an
2584 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2585 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2586 respectively.
2587
2588 You can also post articles and send mail from this buffer.  To
2589 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2590 of an article, type `\\[gnus-summary-reply]'.
2591
2592 There are approx. one gazillion commands you can execute in this
2593 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2594
2595 The following commands are available:
2596
2597 \\{gnus-summary-mode-map}"
2598   (interactive)
2599   (kill-all-local-variables)
2600   (when (gnus-visual-p 'summary-menu 'menu)
2601     (gnus-summary-make-menu-bar)
2602     (gnus-summary-make-tool-bar))
2603   (gnus-summary-make-local-variables)
2604   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2605     (gnus-summary-make-local-variables))
2606   (gnus-make-thread-indent-array)
2607   (gnus-simplify-mode-line)
2608   (setq major-mode 'gnus-summary-mode)
2609   (setq mode-name "Summary")
2610   (make-local-variable 'minor-mode-alist)
2611   (use-local-map gnus-summary-mode-map)
2612   (buffer-disable-undo)
2613   (setq buffer-read-only t)             ;Disable modification
2614   (setq truncate-lines t)
2615   (setq selective-display t)
2616   (setq selective-display-ellipses t)   ;Display `...'
2617   (gnus-summary-set-display-table)
2618   (gnus-set-default-directory)
2619   (setq gnus-newsgroup-name group)
2620   (make-local-variable 'gnus-summary-line-format)
2621   (make-local-variable 'gnus-summary-line-format-spec)
2622   (make-local-variable 'gnus-summary-dummy-line-format)
2623   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2624   (make-local-variable 'gnus-summary-mark-positions)
2625   (make-local-hook 'pre-command-hook)
2626   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2627   (gnus-run-hooks 'gnus-summary-mode-hook)
2628   (turn-on-gnus-mailing-list-mode)
2629   (mm-enable-multibyte)
2630   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2631   (gnus-update-summary-mark-positions))
2632
2633 (defun gnus-summary-make-local-variables ()
2634   "Make all the local summary buffer variables."
2635   (let (global)
2636     (dolist (local gnus-summary-local-variables)
2637       (if (consp local)
2638           (progn
2639             (if (eq (cdr local) 'global)
2640                 ;; Copy the global value of the variable.
2641                 (setq global (symbol-value (car local)))
2642               ;; Use the value from the list.
2643               (setq global (eval (cdr local))))
2644             (set (make-local-variable (car local)) global))
2645         ;; Simple nil-valued local variable.
2646         (set (make-local-variable local) nil)))))
2647
2648 (defun gnus-summary-clear-local-variables ()
2649   (let ((locals gnus-summary-local-variables))
2650     (while locals
2651       (if (consp (car locals))
2652           (and (vectorp (caar locals))
2653                (set (caar locals) nil))
2654         (and (vectorp (car locals))
2655              (set (car locals) nil)))
2656       (setq locals (cdr locals)))))
2657
2658 ;; Summary data functions.
2659
2660 (defmacro gnus-data-number (data)
2661   `(car ,data))
2662
2663 (defmacro gnus-data-set-number (data number)
2664   `(setcar ,data ,number))
2665
2666 (defmacro gnus-data-mark (data)
2667   `(nth 1 ,data))
2668
2669 (defmacro gnus-data-set-mark (data mark)
2670   `(setcar (nthcdr 1 ,data) ,mark))
2671
2672 (defmacro gnus-data-pos (data)
2673   `(nth 2 ,data))
2674
2675 (defmacro gnus-data-set-pos (data pos)
2676   `(setcar (nthcdr 2 ,data) ,pos))
2677
2678 (defmacro gnus-data-header (data)
2679   `(nth 3 ,data))
2680
2681 (defmacro gnus-data-set-header (data header)
2682   `(setf (nth 3 ,data) ,header))
2683
2684 (defmacro gnus-data-level (data)
2685   `(nth 4 ,data))
2686
2687 (defmacro gnus-data-unread-p (data)
2688   `(= (nth 1 ,data) gnus-unread-mark))
2689
2690 (defmacro gnus-data-read-p (data)
2691   `(/= (nth 1 ,data) gnus-unread-mark))
2692
2693 (defmacro gnus-data-pseudo-p (data)
2694   `(consp (nth 3 ,data)))
2695
2696 (defmacro gnus-data-find (number)
2697   `(assq ,number gnus-newsgroup-data))
2698
2699 (defmacro gnus-data-find-list (number &optional data)
2700   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2701      (memq (assq ,number bdata)
2702            bdata)))
2703
2704 (defmacro gnus-data-make (number mark pos header level)
2705   `(list ,number ,mark ,pos ,header ,level))
2706
2707 (defun gnus-data-enter (after-article number mark pos header level offset)
2708   (let ((data (gnus-data-find-list after-article)))
2709     (unless data
2710       (error "No such article: %d" after-article))
2711     (setcdr data (cons (gnus-data-make number mark pos header level)
2712                        (cdr data)))
2713     (setq gnus-newsgroup-data-reverse nil)
2714     (gnus-data-update-list (cddr data) offset)))
2715
2716 (defun gnus-data-enter-list (after-article list &optional offset)
2717   (when list
2718     (let ((data (and after-article (gnus-data-find-list after-article)))
2719           (ilist list))
2720       (if (not (or data
2721                    after-article))
2722           (let ((odata gnus-newsgroup-data))
2723             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2724             (when offset
2725               (gnus-data-update-list odata offset)))
2726       ;; Find the last element in the list to be spliced into the main
2727         ;; list.
2728         (while (cdr list)
2729           (setq list (cdr list)))
2730         (if (not data)
2731             (progn
2732               (setcdr list gnus-newsgroup-data)
2733               (setq gnus-newsgroup-data ilist)
2734               (when offset
2735                 (gnus-data-update-list (cdr list) offset)))
2736           (setcdr list (cdr data))
2737           (setcdr data ilist)
2738           (when offset
2739             (gnus-data-update-list (cdr list) offset))))
2740       (setq gnus-newsgroup-data-reverse nil))))
2741
2742 (defun gnus-data-remove (article &optional offset)
2743   (let ((data gnus-newsgroup-data))
2744     (if (= (gnus-data-number (car data)) article)
2745         (progn
2746           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2747                 gnus-newsgroup-data-reverse nil)
2748           (when offset
2749             (gnus-data-update-list gnus-newsgroup-data offset)))
2750       (while (cdr data)
2751         (when (= (gnus-data-number (cadr data)) article)
2752           (setcdr data (cddr data))
2753           (when offset
2754             (gnus-data-update-list (cdr data) offset))
2755           (setq data nil
2756                 gnus-newsgroup-data-reverse nil))
2757         (setq data (cdr data))))))
2758
2759 (defmacro gnus-data-list (backward)
2760   `(if ,backward
2761        (or gnus-newsgroup-data-reverse
2762            (setq gnus-newsgroup-data-reverse
2763                  (reverse gnus-newsgroup-data)))
2764      gnus-newsgroup-data))
2765
2766 (defun gnus-data-update-list (data offset)
2767   "Add OFFSET to the POS of all data entries in DATA."
2768   (setq gnus-newsgroup-data-reverse nil)
2769   (while data
2770     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2771     (setq data (cdr data))))
2772
2773 (defun gnus-summary-article-pseudo-p (article)
2774   "Say whether this article is a pseudo article or not."
2775   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2776
2777 (defmacro gnus-summary-article-sparse-p (article)
2778   "Say whether this article is a sparse article or not."
2779   `(memq ,article gnus-newsgroup-sparse))
2780
2781 (defmacro gnus-summary-article-ancient-p (article)
2782   "Say whether this article is a sparse article or not."
2783   `(memq ,article gnus-newsgroup-ancient))
2784
2785 (defun gnus-article-parent-p (number)
2786   "Say whether this article is a parent or not."
2787   (let ((data (gnus-data-find-list number)))
2788     (and (cdr data)              ; There has to be an article after...
2789          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2790             (gnus-data-level (nth 1 data))))))
2791
2792 (defun gnus-article-children (number)
2793   "Return a list of all children to NUMBER."
2794   (let* ((data (gnus-data-find-list number))
2795          (level (gnus-data-level (car data)))
2796          children)
2797     (setq data (cdr data))
2798     (while (and data
2799                 (= (gnus-data-level (car data)) (1+ level)))
2800       (push (gnus-data-number (car data)) children)
2801       (setq data (cdr data)))
2802     children))
2803
2804 (defmacro gnus-summary-skip-intangible ()
2805   "If the current article is intangible, then jump to a different article."
2806   '(let ((to (get-text-property (point) 'gnus-intangible)))
2807      (and to (gnus-summary-goto-subject to))))
2808
2809 (defmacro gnus-summary-article-intangible-p ()
2810   "Say whether this article is intangible or not."
2811   '(get-text-property (point) 'gnus-intangible))
2812
2813 (defun gnus-article-read-p (article)
2814   "Say whether ARTICLE is read or not."
2815   (not (or (memq article gnus-newsgroup-marked)
2816            (memq article gnus-newsgroup-spam-marked)
2817            (memq article gnus-newsgroup-unreads)
2818            (memq article gnus-newsgroup-unselected)
2819            (memq article gnus-newsgroup-dormant))))
2820
2821 ;; Some summary mode macros.
2822
2823 (defmacro gnus-summary-article-number ()
2824   "The article number of the article on the current line.
2825 If there isn't an article number here, then we return the current
2826 article number."
2827   '(progn
2828      (gnus-summary-skip-intangible)
2829      (or (get-text-property (point) 'gnus-number)
2830          (gnus-summary-last-subject))))
2831
2832 (defmacro gnus-summary-article-header (&optional number)
2833   "Return the header of article NUMBER."
2834   `(gnus-data-header (gnus-data-find
2835                       ,(or number '(gnus-summary-article-number)))))
2836
2837 (defmacro gnus-summary-thread-level (&optional number)
2838   "Return the level of thread that starts with article NUMBER."
2839   `(if (and (eq gnus-summary-make-false-root 'dummy)
2840             (get-text-property (point) 'gnus-intangible))
2841        0
2842      (gnus-data-level (gnus-data-find
2843                        ,(or number '(gnus-summary-article-number))))))
2844
2845 (defmacro gnus-summary-article-mark (&optional number)
2846   "Return the mark of article NUMBER."
2847   `(gnus-data-mark (gnus-data-find
2848                     ,(or number '(gnus-summary-article-number)))))
2849
2850 (defmacro gnus-summary-article-pos (&optional number)
2851   "Return the position of the line of article NUMBER."
2852   `(gnus-data-pos (gnus-data-find
2853                    ,(or number '(gnus-summary-article-number)))))
2854
2855 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2856 (defmacro gnus-summary-article-subject (&optional number)
2857   "Return current subject string or nil if nothing."
2858   `(let ((headers
2859           ,(if number
2860                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2861              '(gnus-data-header (assq (gnus-summary-article-number)
2862                                       gnus-newsgroup-data)))))
2863      (and headers
2864           (vectorp headers)
2865           (mail-header-subject headers))))
2866
2867 (defmacro gnus-summary-article-score (&optional number)
2868   "Return current article score."
2869   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2870                   gnus-newsgroup-scored))
2871        gnus-summary-default-score 0))
2872
2873 (defun gnus-summary-article-children (&optional number)
2874   "Return a list of article numbers that are children of article NUMBER."
2875   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2876          (level (gnus-data-level (car data)))
2877          l children)
2878     (while (and (setq data (cdr data))
2879                 (> (setq l (gnus-data-level (car data))) level))
2880       (and (= (1+ level) l)
2881            (push (gnus-data-number (car data))
2882                  children)))
2883     (nreverse children)))
2884
2885 (defun gnus-summary-article-parent (&optional number)
2886   "Return the article number of the parent of article NUMBER."
2887   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2888                                     (gnus-data-list t)))
2889          (level (gnus-data-level (car data))))
2890     (if (zerop level)
2891         ()                              ; This is a root.
2892       ;; We search until we find an article with a level less than
2893       ;; this one.  That function has to be the parent.
2894       (while (and (setq data (cdr data))
2895                   (not (< (gnus-data-level (car data)) level))))
2896       (and data (gnus-data-number (car data))))))
2897
2898 (defun gnus-unread-mark-p (mark)
2899   "Say whether MARK is the unread mark."
2900   (= mark gnus-unread-mark))
2901
2902 (defun gnus-read-mark-p (mark)
2903   "Say whether MARK is one of the marks that mark as read.
2904 This is all marks except unread, ticked, dormant, and expirable."
2905   (not (or (= mark gnus-unread-mark)
2906            (= mark gnus-ticked-mark)
2907            (= mark gnus-spam-mark)
2908            (= mark gnus-dormant-mark)
2909            (= mark gnus-expirable-mark))))
2910
2911 (defmacro gnus-article-mark (number)
2912   "Return the MARK of article NUMBER.
2913 This macro should only be used when computing the mark the \"first\"
2914 time; i.e., when generating the summary lines.  After that,
2915 `gnus-summary-article-mark' should be used to examine the
2916 marks of articles."
2917   `(cond
2918     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2919     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2920     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2921     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2922     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2923     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2924     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2925     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2926            gnus-ancient-mark))))
2927
2928 ;; Saving hidden threads.
2929
2930 (defmacro gnus-save-hidden-threads (&rest forms)
2931   "Save hidden threads, eval FORMS, and restore the hidden threads."
2932   (let ((config (make-symbol "config")))
2933     `(let ((,config (gnus-hidden-threads-configuration)))
2934        (unwind-protect
2935            (save-excursion
2936              ,@forms)
2937          (gnus-restore-hidden-threads-configuration ,config)))))
2938 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2939 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2940
2941 (defun gnus-data-compute-positions ()
2942   "Compute the positions of all articles."
2943   (setq gnus-newsgroup-data-reverse nil)
2944   (let ((data gnus-newsgroup-data))
2945     (save-excursion
2946       (gnus-save-hidden-threads
2947         (gnus-summary-show-all-threads)
2948         (goto-char (point-min))
2949         (while data
2950           (while (get-text-property (point) 'gnus-intangible)
2951             (forward-line 1))
2952           (gnus-data-set-pos (car data) (+ (point) 3))
2953           (setq data (cdr data))
2954           (forward-line 1))))))
2955
2956 (defun gnus-hidden-threads-configuration ()
2957   "Return the current hidden threads configuration."
2958   (save-excursion
2959     (let (config)
2960       (goto-char (point-min))
2961       (while (search-forward "\r" nil t)
2962         (push (1- (point)) config))
2963       config)))
2964
2965 (defun gnus-restore-hidden-threads-configuration (config)
2966   "Restore hidden threads configuration from CONFIG."
2967   (save-excursion
2968     (let (point buffer-read-only)
2969       (while (setq point (pop config))
2970         (when (and (< point (point-max))
2971                    (goto-char point)
2972                    (eq (char-after) ?\n))
2973           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2974
2975 ;; Various summary mode internalish functions.
2976
2977 (defun gnus-mouse-pick-article (e)
2978   (interactive "e")
2979   (mouse-set-point e)
2980   (gnus-summary-next-page nil t))
2981
2982 (defun gnus-summary-set-display-table ()
2983   "Change the display table.
2984 Odd characters have a tendency to mess
2985 up nicely formatted displays - we make all possible glyphs
2986 display only a single character."
2987
2988   ;; We start from the standard display table, if any.
2989   (let ((table (or (copy-sequence standard-display-table)
2990                    (make-display-table)))
2991         (i 32))
2992     ;; Nix out all the control chars...
2993     (while (>= (setq i (1- i)) 0)
2994       (aset table i [??]))
2995    ;; ... but not newline and cr, of course.  (cr is necessary for the
2996     ;; selective display).
2997     (aset table ?\n nil)
2998     (aset table ?\r nil)
2999     ;; We keep TAB as well.
3000     (aset table ?\t nil)
3001     ;; We nix out any glyphs over 126 that are not set already.
3002     (let ((i 256))
3003       (while (>= (setq i (1- i)) 127)
3004         ;; Only modify if the entry is nil.
3005         (unless (aref table i)
3006           (aset table i [??]))))
3007     (setq buffer-display-table table)))
3008
3009 (defun gnus-summary-set-article-display-arrow (pos)
3010   "Update the overlay arrow to point to line at position POS."
3011   (when (and gnus-summary-display-arrow
3012              (boundp 'overlay-arrow-position)
3013              (boundp 'overlay-arrow-string))
3014     (save-excursion
3015       (goto-char pos)
3016       (beginning-of-line)
3017       (unless overlay-arrow-position
3018         (setq overlay-arrow-position (make-marker)))
3019       (setq overlay-arrow-string "=>"
3020             overlay-arrow-position (set-marker overlay-arrow-position
3021                                                (point)
3022                                                (current-buffer))))))
3023
3024 (defun gnus-summary-buffer-name (group)
3025   "Return the summary buffer name of GROUP."
3026   (concat "*Summary " (gnus-group-decoded-name group) "*"))
3027
3028 (defun gnus-summary-setup-buffer (group)
3029   "Initialize summary buffer."
3030   (let ((buffer (gnus-summary-buffer-name group))
3031         (dead-name (concat "*Dead Summary "
3032                            (gnus-group-decoded-name group) "*")))
3033     ;; If a dead summary buffer exists, we kill it.
3034     (when (gnus-buffer-live-p dead-name)
3035       (gnus-kill-buffer dead-name))
3036     (if (get-buffer buffer)
3037         (progn
3038           (set-buffer buffer)
3039           (setq gnus-summary-buffer (current-buffer))
3040           (not gnus-newsgroup-prepared))
3041       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3042       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3043       (gnus-summary-mode group)
3044       (when gnus-carpal
3045         (gnus-carpal-setup-buffer 'summary))
3046       (unless gnus-single-article-buffer
3047         (make-local-variable 'gnus-article-buffer)
3048         (make-local-variable 'gnus-article-current)
3049         (make-local-variable 'gnus-original-article-buffer))
3050       (setq gnus-newsgroup-name group)
3051       ;; Set any local variables in the group parameters.
3052       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3053       t)))
3054
3055 (defun gnus-set-global-variables ()
3056   "Set the global equivalents of the buffer-local variables.
3057 They are set to the latest values they had.  These reflect the summary
3058 buffer that was in action when the last article was fetched."
3059   (when (eq major-mode 'gnus-summary-mode)
3060     (setq gnus-summary-buffer (current-buffer))
3061     (let ((name gnus-newsgroup-name)
3062           (marked gnus-newsgroup-marked)
3063           (spam gnus-newsgroup-spam-marked)
3064           (unread gnus-newsgroup-unreads)
3065           (headers gnus-current-headers)
3066           (data gnus-newsgroup-data)
3067           (summary gnus-summary-buffer)
3068           (article-buffer gnus-article-buffer)
3069           (original gnus-original-article-buffer)
3070           (gac gnus-article-current)
3071           (reffed gnus-reffed-article-number)
3072           (score-file gnus-current-score-file)
3073           (default-charset gnus-newsgroup-charset)
3074           vlist)
3075       (let ((locals gnus-newsgroup-variables))
3076         (while locals
3077           (if (consp (car locals))
3078               (push (eval (caar locals)) vlist)
3079             (push (eval (car locals)) vlist))
3080           (setq locals (cdr locals)))
3081         (setq vlist (nreverse vlist)))
3082       (save-excursion
3083         (set-buffer gnus-group-buffer)
3084         (setq gnus-newsgroup-name name
3085               gnus-newsgroup-marked marked
3086               gnus-newsgroup-spam-marked spam
3087               gnus-newsgroup-unreads unread
3088               gnus-current-headers headers
3089               gnus-newsgroup-data data
3090               gnus-article-current gac
3091               gnus-summary-buffer summary
3092               gnus-article-buffer article-buffer
3093               gnus-original-article-buffer original
3094               gnus-reffed-article-number reffed
3095               gnus-current-score-file score-file
3096               gnus-newsgroup-charset default-charset)
3097         (let ((locals gnus-newsgroup-variables))
3098           (while locals
3099             (if (consp (car locals))
3100                 (set (caar locals) (pop vlist))
3101               (set (car locals) (pop vlist)))
3102             (setq locals (cdr locals))))
3103         ;; The article buffer also has local variables.
3104         (when (gnus-buffer-live-p gnus-article-buffer)
3105           (set-buffer gnus-article-buffer)
3106           (setq gnus-summary-buffer summary))))))
3107
3108 (defun gnus-summary-article-unread-p (article)
3109   "Say whether ARTICLE is unread or not."
3110   (memq article gnus-newsgroup-unreads))
3111
3112 (defun gnus-summary-first-article-p (&optional article)
3113   "Return whether ARTICLE is the first article in the buffer."
3114   (if (not (setq article (or article (gnus-summary-article-number))))
3115       nil
3116     (eq article (caar gnus-newsgroup-data))))
3117
3118 (defun gnus-summary-last-article-p (&optional article)
3119   "Return whether ARTICLE is the last article in the buffer."
3120   (if (not (setq article (or article (gnus-summary-article-number))))
3121       ;; All non-existent numbers are the last article.  :-)
3122       t
3123     (not (cdr (gnus-data-find-list article)))))
3124
3125 (defun gnus-make-thread-indent-array ()
3126   (let ((n 200))
3127     (unless (and gnus-thread-indent-array
3128                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3129       (setq gnus-thread-indent-array (make-vector 201 "")
3130             gnus-thread-indent-array-level gnus-thread-indent-level)
3131       (while (>= n 0)
3132         (aset gnus-thread-indent-array n
3133               (make-string (* n gnus-thread-indent-level) ? ))
3134         (setq n (1- n))))))
3135
3136 (defun gnus-update-summary-mark-positions ()
3137   "Compute where the summary marks are to go."
3138   (save-excursion
3139     (when (gnus-buffer-exists-p gnus-summary-buffer)
3140       (set-buffer gnus-summary-buffer))
3141     (let ((gnus-replied-mark 129)
3142           (gnus-score-below-mark 130)
3143           (gnus-score-over-mark 130)
3144           (gnus-undownloaded-mark 131)
3145           (spec gnus-summary-line-format-spec)
3146           gnus-visual pos)
3147       (save-excursion
3148         (gnus-set-work-buffer)
3149         (let ((gnus-summary-line-format-spec spec)
3150               (gnus-newsgroup-downloadable '(0)))
3151           (gnus-summary-insert-line
3152            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3153            0 nil t 128 t nil "" nil 1)
3154           (goto-char (point-min))
3155           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3156                                              (- (point) (point-min) 1)))))
3157           (goto-char (point-min))
3158           (push (cons 'replied (and (search-forward "\201" nil t)
3159                                     (- (point) (point-min) 1)))
3160                 pos)
3161           (goto-char (point-min))
3162           (push (cons 'score (and (search-forward "\202" nil t)
3163                                   (- (point) (point-min) 1)))
3164                 pos)
3165           (goto-char (point-min))
3166           (push (cons 'download
3167                       (and (search-forward "\203" nil t)
3168                            (- (point) (point-min) 1)))
3169                 pos)))
3170       (setq gnus-summary-mark-positions pos))))
3171
3172 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3173   "Insert a dummy root in the summary buffer."
3174   (beginning-of-line)
3175   (gnus-add-text-properties
3176    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3177    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3178
3179 (defun gnus-summary-extract-address-component (from)
3180   (or (car (funcall gnus-extract-address-components from))
3181       from))
3182
3183 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3184   (let ((mail-parse-charset gnus-newsgroup-charset)
3185         ; Is it really necessary to do this next part for each summary line?
3186         ; Luckily, doesn't seem to slow things down much.
3187         (mail-parse-ignored-charsets
3188          (save-excursion (set-buffer gnus-summary-buffer)
3189                          gnus-newsgroup-ignored-charsets)))
3190     (or
3191      (and gnus-ignored-from-addresses
3192           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3193           (let ((extra-headers (mail-header-extra header))
3194                 to
3195                 newsgroups)
3196             (cond
3197              ((setq to (cdr (assq 'To extra-headers)))
3198               (concat "-> "
3199                       (inline
3200                         (gnus-summary-extract-address-component
3201                          (funcall gnus-decode-encoded-word-function to)))))
3202              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3203               (concat "=> " newsgroups)))))
3204      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3205
3206 (defun gnus-summary-insert-line (gnus-tmp-header
3207                                  gnus-tmp-level gnus-tmp-current
3208                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3209                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3210                                  &optional gnus-tmp-dummy gnus-tmp-score
3211                                  gnus-tmp-process)
3212   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3213          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3214          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3215          (gnus-tmp-score-char
3216           (if (or (null gnus-summary-default-score)
3217                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3218                       gnus-summary-zcore-fuzz))
3219               ?                         ;Whitespace
3220             (if (< gnus-tmp-score gnus-summary-default-score)
3221                 gnus-score-below-mark gnus-score-over-mark)))
3222          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3223          (gnus-tmp-replied
3224           (cond (gnus-tmp-process gnus-process-mark)
3225                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3226                  gnus-cached-mark)
3227                 (gnus-tmp-replied gnus-replied-mark)
3228                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3229                  gnus-forwarded-mark)
3230                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3231                  gnus-saved-mark)
3232                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3233                  gnus-recent-mark)
3234                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3235                  gnus-unseen-mark)
3236                 (t gnus-no-mark)))
3237          (gnus-tmp-downloaded
3238           (cond (undownloaded 
3239                  gnus-undownloaded-mark)
3240                 (gnus-newsgroup-agentized
3241                  gnus-downloaded-mark)
3242                 (t
3243                  gnus-no-mark)))
3244          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3245          (gnus-tmp-name
3246           (cond
3247            ((string-match "<[^>]+> *$" gnus-tmp-from)
3248             (let ((beg (match-beginning 0)))
3249               (or (and (string-match "^\".+\"" gnus-tmp-from)
3250                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3251                   (substring gnus-tmp-from 0 beg))))
3252            ((string-match "(.+)" gnus-tmp-from)
3253             (substring gnus-tmp-from
3254                        (1+ (match-beginning 0)) (1- (match-end 0))))
3255            (t gnus-tmp-from)))
3256          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3257          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3258          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3259          (buffer-read-only nil))
3260     (when (string= gnus-tmp-name "")
3261       (setq gnus-tmp-name gnus-tmp-from))
3262     (unless (numberp gnus-tmp-lines)
3263       (setq gnus-tmp-lines -1))
3264     (if (= gnus-tmp-lines -1)
3265         (setq gnus-tmp-lines "?")
3266       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines))) 
3267       (gnus-put-text-property
3268      (point)
3269      (progn (eval gnus-summary-line-format-spec) (point))
3270        'gnus-number gnus-tmp-number)
3271     (when (gnus-visual-p 'summary-highlight 'highlight)
3272       (forward-line -1)
3273       (gnus-run-hooks 'gnus-summary-update-hook)
3274       (forward-line 1))))
3275
3276 (defun gnus-summary-update-line (&optional dont-update)
3277   "Update summary line after change."
3278   (when (and gnus-summary-default-score
3279              (not gnus-summary-inhibit-highlight))
3280     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3281            (article (gnus-summary-article-number))
3282            (score (gnus-summary-article-score article)))
3283       (unless dont-update
3284         (if (and gnus-summary-mark-below
3285                  (< (gnus-summary-article-score)
3286                     gnus-summary-mark-below))
3287             ;; This article has a low score, so we mark it as read.
3288             (when (memq article gnus-newsgroup-unreads)
3289               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3290           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3291             ;; This article was previously marked as read on account
3292             ;; of a low score, but now it has risen, so we mark it as
3293             ;; unread.
3294             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3295         (gnus-summary-update-mark
3296          (if (or (null gnus-summary-default-score)
3297                  (<= (abs (- score gnus-summary-default-score))
3298                      gnus-summary-zcore-fuzz))
3299              ?                          ;Whitespace
3300            (if (< score gnus-summary-default-score)
3301                gnus-score-below-mark gnus-score-over-mark))
3302          'score))
3303       ;; Do visual highlighting.
3304       (when (gnus-visual-p 'summary-highlight 'highlight)
3305         (gnus-run-hooks 'gnus-summary-update-hook)))))
3306
3307 (defvar gnus-tmp-new-adopts nil)
3308
3309 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3310   "Return the number of articles in THREAD.
3311 This may be 0 in some cases -- if none of the articles in
3312 the thread are to be displayed."
3313   (let* ((number
3314          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3315           (cond
3316            ((not (listp thread))
3317             1)
3318            ((and (consp thread) (cdr thread))
3319             (apply
3320              '+ 1 (mapcar
3321                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3322            ((null thread)
3323             1)
3324            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3325             1)
3326            (t 0))))
3327     (when (and level (zerop level) gnus-tmp-new-adopts)
3328       (incf number
3329             (apply '+ (mapcar
3330                        'gnus-summary-number-of-articles-in-thread
3331                        gnus-tmp-new-adopts))))
3332     (if char
3333         (if (> number 1) gnus-not-empty-thread-mark
3334           gnus-empty-thread-mark)
3335       number)))
3336
3337 (defsubst gnus-summary-line-message-size (head)
3338   "Return pretty-printed version of message size.
3339 This function is intended to be used in
3340 `gnus-summary-line-format-alist', which see."
3341   (let ((c (or (mail-header-chars head) -1)))
3342     (cond ((< c 0) "n/a")               ; chars not available
3343           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3344           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3345           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3346           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3347
3348
3349 (defun gnus-summary-set-local-parameters (group)
3350   "Go through the local params of GROUP and set all variable specs in that list."
3351   (let ((params (gnus-group-find-parameter group))
3352         (vars '(quit-config))           ; Ignore quit-config.
3353         elem)
3354     (while params
3355       (setq elem (car params)
3356             params (cdr params))
3357       (and (consp elem)                 ; Has to be a cons.
3358            (consp (cdr elem))           ; The cdr has to be a list.
3359            (symbolp (car elem))         ; Has to be a symbol in there.
3360            (not (memq (car elem) vars))
3361            (ignore-errors               ; So we set it.
3362              (push (car elem) vars)
3363              (make-local-variable (car elem))
3364              (set (car elem) (eval (nth 1 elem))))))))
3365
3366 (defun gnus-summary-read-group (group &optional show-all no-article
3367                                       kill-buffer no-display backward
3368                                       select-articles)
3369   "Start reading news in newsgroup GROUP.
3370 If SHOW-ALL is non-nil, already read articles are also listed.
3371 If NO-ARTICLE is non-nil, no article is selected initially.
3372 If NO-DISPLAY, don't generate a summary buffer."
3373   (let (result)
3374     (while (and group
3375                 (null (setq result
3376                             (let ((gnus-auto-select-next nil))
3377                               (or (gnus-summary-read-group-1
3378                                    group show-all no-article
3379                                    kill-buffer no-display
3380                                    select-articles)
3381                                   (setq show-all nil
3382                                         select-articles nil)))))
3383                 (eq gnus-auto-select-next 'quietly))
3384       (set-buffer gnus-group-buffer)
3385       ;; The entry function called above goes to the next
3386       ;; group automatically, so we go two groups back
3387       ;; if we are searching for the previous group.
3388       (when backward
3389         (gnus-group-prev-unread-group 2))
3390       (if (not (equal group (gnus-group-group-name)))
3391           (setq group (gnus-group-group-name))
3392         (setq group nil)))
3393     result))
3394
3395 (defun gnus-summary-read-group-1 (group show-all no-article
3396                                         kill-buffer no-display
3397                                         &optional select-articles)
3398   ;; Killed foreign groups can't be entered.
3399   ;;  (when (and (not (gnus-group-native-p group))
3400   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3401   ;;    (error "Dead non-native groups can't be entered"))
3402   (gnus-message 5 "Retrieving newsgroup: %s..."
3403                 (gnus-group-decoded-name group))
3404   (let* ((new-group (gnus-summary-setup-buffer group))
3405          (quit-config (gnus-group-quit-config group))
3406          (did-select (and new-group (gnus-select-newsgroup
3407                                      group show-all select-articles))))
3408     (cond
3409      ;; This summary buffer exists already, so we just select it.
3410      ((not new-group)
3411       (gnus-set-global-variables)
3412       (when kill-buffer
3413         (gnus-kill-or-deaden-summary kill-buffer))
3414       (gnus-configure-windows 'summary 'force)
3415       (gnus-set-mode-line 'summary)
3416       (gnus-summary-position-point)
3417       (message "")
3418       t)
3419      ;; We couldn't select this group.
3420      ((null did-select)
3421       (when (and (eq major-mode 'gnus-summary-mode)
3422                  (not (equal (current-buffer) kill-buffer)))
3423         (kill-buffer (current-buffer))
3424         (if (not quit-config)
3425             (progn
3426               ;; Update the info -- marks might need to be removed,
3427               ;; for instance.
3428               (gnus-summary-update-info)
3429               (set-buffer gnus-group-buffer)
3430               (gnus-group-jump-to-group group)
3431               (gnus-group-next-unread-group 1))
3432           (gnus-handle-ephemeral-exit quit-config)))
3433       (let ((grpinfo (gnus-get-info group)))
3434         (if (null (gnus-info-read grpinfo))
3435             (gnus-message 3 "Group %s contains no messages"
3436                           (gnus-group-decoded-name group))
3437           (gnus-message 3 "Can't select group")))
3438       nil)
3439      ;; The user did a `C-g' while prompting for number of articles,
3440      ;; so we exit this group.
3441      ((eq did-select 'quit)
3442       (and (eq major-mode 'gnus-summary-mode)
3443            (not (equal (current-buffer) kill-buffer))
3444            (kill-buffer (current-buffer)))
3445       (when kill-buffer
3446         (gnus-kill-or-deaden-summary kill-buffer))
3447       (if (not quit-config)
3448           (progn
3449             (set-buffer gnus-group-buffer)
3450             (gnus-group-jump-to-group group)
3451             (gnus-group-next-unread-group 1)
3452             (gnus-configure-windows 'group 'force))
3453         (gnus-handle-ephemeral-exit quit-config))
3454       ;; Finally signal the quit.
3455       (signal 'quit nil))
3456      ;; The group was successfully selected.
3457      (t
3458       (gnus-set-global-variables)
3459       ;; Save the active value in effect when the group was entered.
3460       (setq gnus-newsgroup-active
3461             (gnus-copy-sequence
3462              (gnus-active gnus-newsgroup-name)))
3463       ;; You can change the summary buffer in some way with this hook.
3464       (gnus-run-hooks 'gnus-select-group-hook)
3465       (gnus-update-format-specifications
3466        nil 'summary 'summary-mode 'summary-dummy)
3467       (gnus-update-summary-mark-positions)
3468       ;; Do score processing.
3469       (when gnus-use-scoring
3470         (gnus-possibly-score-headers))
3471       ;; Check whether to fill in the gaps in the threads.
3472       (when gnus-build-sparse-threads
3473         (gnus-build-sparse-threads))
3474       ;; Find the initial limit.
3475       (if gnus-show-threads
3476           (if show-all
3477               (let ((gnus-newsgroup-dormant nil))
3478                 (gnus-summary-initial-limit show-all))
3479             (gnus-summary-initial-limit show-all))
3480         ;; When unthreaded, all articles are always shown.
3481         (setq gnus-newsgroup-limit
3482               (mapcar
3483                (lambda (header) (mail-header-number header))
3484                gnus-newsgroup-headers)))
3485       ;; Generate the summary buffer.
3486       (unless no-display
3487         (gnus-summary-prepare))
3488       (when gnus-use-trees
3489         (gnus-tree-open group)
3490         (setq gnus-summary-highlight-line-function
3491               'gnus-tree-highlight-article))
3492       ;; If the summary buffer is empty, but there are some low-scored
3493       ;; articles or some excluded dormants, we include these in the
3494       ;; buffer.
3495       (when (and (zerop (buffer-size))
3496                  (not no-display))
3497         (cond (gnus-newsgroup-dormant
3498                (gnus-summary-limit-include-dormant))
3499               ((and gnus-newsgroup-scored show-all)
3500                (gnus-summary-limit-include-expunged t))))
3501       ;; Function `gnus-apply-kill-file' must be called in this hook.
3502       (gnus-run-hooks 'gnus-apply-kill-hook)
3503       (if (and (zerop (buffer-size))
3504                (not no-display))
3505           (progn
3506             ;; This newsgroup is empty.
3507             (gnus-summary-catchup-and-exit nil t)
3508             (gnus-message 6 "No unread news")
3509             (when kill-buffer
3510               (gnus-kill-or-deaden-summary kill-buffer))
3511             ;; Return nil from this function.
3512             nil)
3513         ;; Hide conversation thread subtrees.  We cannot do this in
3514         ;; gnus-summary-prepare-hook since kill processing may not
3515         ;; work with hidden articles.
3516         (gnus-summary-maybe-hide-threads)
3517         (when kill-buffer
3518           (gnus-kill-or-deaden-summary kill-buffer))
3519         (gnus-summary-auto-select-subject)
3520         ;; Show first unread article if requested.
3521         (if (and (not no-article)
3522                  (not no-display)
3523                  gnus-newsgroup-unreads
3524                  gnus-auto-select-first)
3525             (progn
3526               (gnus-configure-windows 'summary)
3527               (let ((art (gnus-summary-article-number)))
3528                 (unless (and (not gnus-plugged)
3529                              (or (memq art gnus-newsgroup-undownloaded)
3530                                  (memq art gnus-newsgroup-downloadable)))
3531                   (gnus-summary-goto-article art))))
3532           ;; Don't select any articles.
3533           (gnus-summary-position-point)
3534           (gnus-configure-windows 'summary 'force)
3535           (gnus-set-mode-line 'summary))
3536         (when (get-buffer-window gnus-group-buffer t)
3537           ;; Gotta use windows, because recenter does weird stuff if
3538           ;; the current buffer ain't the displayed window.
3539           (let ((owin (selected-window)))
3540             (select-window (get-buffer-window gnus-group-buffer t))
3541             (when (gnus-group-goto-group group)
3542               (recenter))
3543             (select-window owin)))
3544         ;; Mark this buffer as "prepared".
3545         (setq gnus-newsgroup-prepared t)
3546         (gnus-run-hooks 'gnus-summary-prepared-hook)
3547         (unless (gnus-ephemeral-group-p group)
3548           (gnus-group-update-group group))
3549         t)))))
3550
3551 (defun gnus-summary-auto-select-subject ()
3552   "Select the subject line on initial group entry."
3553   (goto-char (point-min))
3554   (cond
3555    ((eq gnus-auto-select-subject 'best)
3556     (gnus-summary-best-unread-subject))
3557    ((eq gnus-auto-select-subject 'unread)
3558     (gnus-summary-first-unread-subject))
3559    ((eq gnus-auto-select-subject 'unseen)
3560     (gnus-summary-first-unseen-subject))
3561    ((eq gnus-auto-select-subject 'unseen-or-unread)
3562     (gnus-summary-first-unseen-or-unread-subject))
3563    ((eq gnus-auto-select-subject 'first)
3564     ;; Do nothing.
3565     )
3566    ((gnus-functionp gnus-auto-select-subject)
3567     (funcall gnus-auto-select-subject))))
3568
3569 (defun gnus-summary-prepare ()
3570   "Generate the summary buffer."
3571   (interactive)
3572   (let ((buffer-read-only nil))
3573     (erase-buffer)
3574     (setq gnus-newsgroup-data nil
3575           gnus-newsgroup-data-reverse nil)
3576     (gnus-run-hooks 'gnus-summary-generate-hook)
3577     ;; Generate the buffer, either with threads or without.
3578     (when gnus-newsgroup-headers
3579       (gnus-summary-prepare-threads
3580        (if gnus-show-threads
3581            (gnus-sort-gathered-threads
3582             (funcall gnus-summary-thread-gathering-function
3583                      (gnus-sort-threads
3584                       (gnus-cut-threads (gnus-make-threads)))))
3585          ;; Unthreaded display.
3586          (gnus-sort-articles gnus-newsgroup-headers))))
3587     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3588     ;; Call hooks for modifying summary buffer.
3589     (goto-char (point-min))
3590     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3591
3592 (defsubst gnus-general-simplify-subject (subject)
3593   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3594   (setq subject
3595         (cond
3596          ;; Truncate the subject.
3597          (gnus-simplify-subject-functions
3598           (gnus-map-function gnus-simplify-subject-functions subject))
3599          ((numberp gnus-summary-gather-subject-limit)
3600           (setq subject (gnus-simplify-subject-re subject))
3601           (if (> (length subject) gnus-summary-gather-subject-limit)
3602               (substring subject 0 gnus-summary-gather-subject-limit)
3603             subject))
3604          ;; Fuzzily simplify it.
3605          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3606           (gnus-simplify-subject-fuzzy subject))
3607          ;; Just remove the leading "Re:".
3608          (t
3609           (gnus-simplify-subject-re subject))))
3610
3611   (if (and gnus-summary-gather-exclude-subject
3612            (string-match gnus-summary-gather-exclude-subject subject))
3613       nil                         ; This article shouldn't be gathered
3614     subject))
3615
3616 (defun gnus-summary-simplify-subject-query ()
3617   "Query where the respool algorithm would put this article."
3618   (interactive)
3619   (gnus-summary-select-article)
3620   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3621
3622 (defun gnus-gather-threads-by-subject (threads)
3623   "Gather threads by looking at Subject headers."
3624   (if (not gnus-summary-make-false-root)
3625       threads
3626     (let ((hashtb (gnus-make-hashtable 1024))
3627           (prev threads)
3628           (result threads)
3629           subject hthread whole-subject)
3630       (while threads
3631         (setq subject (gnus-general-simplify-subject
3632                        (setq whole-subject (mail-header-subject
3633                                             (caar threads)))))
3634         (when subject
3635           (if (setq hthread (gnus-gethash subject hashtb))
3636               (progn
3637                 ;; We enter a dummy root into the thread, if we
3638                 ;; haven't done that already.
3639                 (unless (stringp (caar hthread))
3640                   (setcar hthread (list whole-subject (car hthread))))
3641                 ;; We add this new gathered thread to this gathered
3642                 ;; thread.
3643                 (setcdr (car hthread)
3644                         (nconc (cdar hthread) (list (car threads))))
3645                 ;; Remove it from the list of threads.
3646                 (setcdr prev (cdr threads))
3647                 (setq threads prev))
3648             ;; Enter this thread into the hash table.
3649             (gnus-sethash subject
3650                           (if gnus-summary-make-false-root-always
3651                               (progn
3652                                 ;; If you want a dummy root above all
3653                                 ;; threads...
3654                                 (setcar threads (list whole-subject
3655                                                       (car threads)))
3656                                 threads)
3657                             threads)
3658                           hashtb)))
3659         (setq prev threads)
3660         (setq threads (cdr threads)))
3661       result)))
3662
3663 (defun gnus-gather-threads-by-references (threads)
3664   "Gather threads by looking at References headers."
3665   (let ((idhashtb (gnus-make-hashtable 1024))
3666         (thhashtb (gnus-make-hashtable 1024))
3667         (prev threads)
3668         (result threads)
3669         ids references id gthread gid entered ref)
3670     (while threads
3671       (when (setq references (mail-header-references (caar threads)))
3672         (setq id (mail-header-id (caar threads))
3673               ids (inline (gnus-split-references references))
3674               entered nil)
3675         (while (setq ref (pop ids))
3676           (setq ids (delete ref ids))
3677           (if (not (setq gid (gnus-gethash ref idhashtb)))
3678               (progn
3679                 (gnus-sethash ref id idhashtb)
3680                 (gnus-sethash id threads thhashtb))
3681             (setq gthread (gnus-gethash gid thhashtb))
3682             (unless entered
3683               ;; We enter a dummy root into the thread, if we
3684               ;; haven't done that already.
3685               (unless (stringp (caar gthread))
3686                 (setcar gthread (list (mail-header-subject (caar gthread))
3687                                       (car gthread))))
3688               ;; We add this new gathered thread to this gathered
3689               ;; thread.
3690               (setcdr (car gthread)
3691                       (nconc (cdar gthread) (list (car threads)))))
3692             ;; Add it into the thread hash table.
3693             (gnus-sethash id gthread thhashtb)
3694             (setq entered t)
3695             ;; Remove it from the list of threads.
3696             (setcdr prev (cdr threads))
3697             (setq threads prev))))
3698       (setq prev threads)
3699       (setq threads (cdr threads)))
3700     result))
3701
3702 (defun gnus-sort-gathered-threads (threads)
3703   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3704   (let ((result threads))
3705     (while threads
3706       (when (stringp (caar threads))
3707         (setcdr (car threads)
3708                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3709       (setq threads (cdr threads)))
3710     result))
3711
3712 (defun gnus-thread-loop-p (root thread)
3713   "Say whether ROOT is in THREAD."
3714   (let ((stack (list thread))
3715         (infloop 0)
3716         th)
3717     (while (setq thread (pop stack))
3718       (setq th (cdr thread))
3719       (while (and th
3720                   (not (eq (caar th) root)))
3721         (pop th))
3722       (if th
3723           ;; We have found a loop.
3724           (let (ref-dep)
3725             (setcdr thread (delq (car th) (cdr thread)))
3726             (if (boundp (setq ref-dep (intern "none"
3727                                               gnus-newsgroup-dependencies)))
3728                 (setcdr (symbol-value ref-dep)
3729                         (nconc (cdr (symbol-value ref-dep))
3730                                (list (car th))))
3731               (set ref-dep (list nil (car th))))
3732             (setq infloop 1
3733                   stack nil))
3734         ;; Push all the subthreads onto the stack.
3735         (push (cdr thread) stack)))
3736     infloop))
3737
3738 (defun gnus-make-threads ()
3739   "Go through the dependency hashtb and find the roots.  Return all threads."
3740   (let (threads)
3741     (while (catch 'infloop
3742              (mapatoms
3743               (lambda (refs)
3744                 ;; Deal with self-referencing References loops.
3745                 (when (and (car (symbol-value refs))
3746                            (not (zerop
3747                                  (apply
3748                                   '+
3749                                   (mapcar
3750                                    (lambda (thread)
3751                                      (gnus-thread-loop-p
3752                                       (car (symbol-value refs)) thread))
3753                                    (cdr (symbol-value refs)))))))
3754                   (setq threads nil)
3755                   (throw 'infloop t))
3756                 (unless (car (symbol-value refs))
3757                   ;; These threads do not refer back to any other
3758                   ;; articles, so they're roots.
3759                   (setq threads (append (cdr (symbol-value refs)) threads))))
3760               gnus-newsgroup-dependencies)))
3761     threads))
3762
3763 ;; Build the thread tree.
3764 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3765   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3766
3767 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3768 if it was already present.
3769
3770 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3771 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3772 Message-IDs will be renamed to a unique Message-ID before being
3773 entered.
3774
3775 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3776   (let* ((id (mail-header-id header))
3777          (id-dep (and id (intern id dependencies)))
3778          parent-id ref ref-dep ref-header replaced)
3779     ;; Enter this `header' in the `dependencies' table.
3780     (cond
3781      ((not id-dep)
3782       (setq header nil))
3783      ;; The first two cases do the normal part: enter a new `header'
3784      ;; in the `dependencies' table.
3785      ((not (boundp id-dep))
3786       (set id-dep (list header)))
3787      ((null (car (symbol-value id-dep)))
3788       (setcar (symbol-value id-dep) header))
3789
3790      ;; From here the `header' was already present in the
3791      ;; `dependencies' table.
3792      (force-new
3793       ;; Overrides an existing entry;
3794       ;; just set the header part of the entry.
3795       (setcar (symbol-value id-dep) header)
3796       (setq replaced t))
3797
3798      ;; Renames the existing `header' to a unique Message-ID.
3799      ((not gnus-summary-ignore-duplicates)
3800       ;; An article with this Message-ID has already been seen.
3801       ;; We rename the Message-ID.
3802       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3803            (list header))
3804       (mail-header-set-id header id))
3805
3806      ;; The last case ignores an existing entry, except it adds any
3807      ;; additional Xrefs (in case the two articles came from different
3808      ;; servers.
3809      ;; Also sets `header' to `nil' meaning that the `dependencies'
3810      ;; table was *not* modified.
3811      (t
3812       (mail-header-set-xref
3813        (car (symbol-value id-dep))
3814        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3815                    "")
3816                (or (mail-header-xref header) "")))
3817       (setq header nil)))
3818
3819     (when (and header (not replaced))
3820       ;; First check that we are not creating a References loop.
3821       (setq parent-id (gnus-parent-id (mail-header-references header)))
3822       (setq ref parent-id)
3823       (while (and ref
3824                   (setq ref-dep (intern-soft ref dependencies))
3825                   (boundp ref-dep)
3826                   (setq ref-header (car (symbol-value ref-dep))))
3827         (if (string= id ref)
3828             ;; Yuk!  This is a reference loop.  Make the article be a
3829             ;; root article.
3830             (progn
3831               (mail-header-set-references (car (symbol-value id-dep)) "none")
3832               (setq ref nil)
3833               (setq parent-id nil))
3834           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3835       (setq ref-dep (intern (or parent-id "none") dependencies))
3836       (if (boundp ref-dep)
3837           (setcdr (symbol-value ref-dep)
3838                   (nconc (cdr (symbol-value ref-dep))
3839                          (list (symbol-value id-dep))))
3840         (set ref-dep (list nil (symbol-value id-dep)))))
3841     header))
3842
3843 (defun gnus-extract-message-id-from-in-reply-to (string)
3844   (if (string-match "<[^>]+>" string)
3845       (substring string (match-beginning 0) (match-end 0))
3846     nil))
3847
3848 (defun gnus-build-sparse-threads ()
3849   (let ((headers gnus-newsgroup-headers)
3850         (mail-parse-charset gnus-newsgroup-charset)
3851         (gnus-summary-ignore-duplicates t)
3852         header references generation relations
3853         subject child end new-child date)
3854     ;; First we create an alist of generations/relations, where
3855     ;; generations is how much we trust the relation, and the relation
3856     ;; is parent/child.
3857     (gnus-message 7 "Making sparse threads...")
3858     (save-excursion
3859       (nnheader-set-temp-buffer " *gnus sparse threads*")
3860       (while (setq header (pop headers))
3861         (when (and (setq references (mail-header-references header))
3862                    (not (string= references "")))
3863           (insert references)
3864           (setq child (mail-header-id header)
3865                 subject (mail-header-subject header)
3866                 date (mail-header-date header)
3867                 generation 0)
3868           (while (search-backward ">" nil t)
3869             (setq end (1+ (point)))
3870             (when (search-backward "<" nil t)
3871               (setq new-child (buffer-substring (point) end))
3872               (push (list (incf generation)
3873                           child (setq child new-child)
3874                           subject date)
3875                     relations)))
3876           (when child
3877             (push (list (1+ generation) child nil subject) relations))
3878           (erase-buffer)))
3879       (kill-buffer (current-buffer)))
3880     ;; Sort over trustworthiness.
3881     (mapcar
3882      (lambda (relation)
3883        (when (gnus-dependencies-add-header
3884               (make-full-mail-header
3885                gnus-reffed-article-number
3886                (nth 3 relation) "" (or (nth 4 relation) "")
3887                (nth 1 relation)
3888                (or (nth 2 relation) "") 0 0 "")
3889               gnus-newsgroup-dependencies nil)
3890          (push gnus-reffed-article-number gnus-newsgroup-limit)
3891          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3892          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3893                gnus-newsgroup-reads)
3894          (decf gnus-reffed-article-number)))
3895      (sort relations 'car-less-than-car))
3896     (gnus-message 7 "Making sparse threads...done")))
3897
3898 (defun gnus-build-old-threads ()
3899   ;; Look at all the articles that refer back to old articles, and
3900   ;; fetch the headers for the articles that aren't there.  This will
3901   ;; build complete threads - if the roots haven't been expired by the
3902   ;; server, that is.
3903   (let ((mail-parse-charset gnus-newsgroup-charset)
3904         id heads)
3905     (mapatoms
3906      (lambda (refs)
3907        (when (not (car (symbol-value refs)))
3908          (setq heads (cdr (symbol-value refs)))
3909          (while heads
3910            (if (memq (mail-header-number (caar heads))
3911                      gnus-newsgroup-dormant)
3912                (setq heads (cdr heads))
3913              (setq id (symbol-name refs))
3914              (while (and (setq id (gnus-build-get-header id))
3915                          (not (car (gnus-id-to-thread id)))))
3916              (setq heads nil)))))
3917      gnus-newsgroup-dependencies)))
3918
3919 ;; This function has to be called with point after the article number
3920 ;; on the beginning of the line.
3921 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3922   (let ((eol (gnus-point-at-eol))
3923         (buffer (current-buffer))
3924         header references in-reply-to)
3925
3926     ;; overview: [num subject from date id refs chars lines misc]
3927     (unwind-protect
3928         (let (x)
3929           (narrow-to-region (point) eol)
3930           (unless (eobp)
3931             (forward-char))
3932
3933           (setq header
3934                 (make-full-mail-header
3935                  number                 ; number
3936                  (condition-case ()     ; subject
3937                      (funcall gnus-decode-encoded-word-function
3938                               (setq x (nnheader-nov-field)))
3939                    (error x))
3940                  (condition-case ()     ; from
3941                      (funcall gnus-decode-encoded-word-function
3942                               (setq x (nnheader-nov-field)))
3943                    (error x))
3944                  (nnheader-nov-field)   ; date
3945                  (nnheader-nov-read-message-id) ; id
3946                  (setq references (nnheader-nov-field)) ; refs
3947                  (nnheader-nov-read-integer) ; chars
3948                  (nnheader-nov-read-integer) ; lines
3949                  (unless (eobp)
3950                    (if (looking-at "Xref: ")
3951                        (goto-char (match-end 0)))
3952                    (nnheader-nov-field)) ; Xref
3953                  (nnheader-nov-parse-extra)))) ; extra
3954
3955       (widen))
3956
3957     (when (and (string= references "")
3958                (setq in-reply-to (mail-header-extra header))
3959                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3960       (mail-header-set-references
3961        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3962
3963     (when gnus-alter-header-function
3964       (funcall gnus-alter-header-function header))
3965     (gnus-dependencies-add-header header dependencies force-new)))
3966
3967 (defun gnus-build-get-header (id)
3968   "Look through the buffer of NOV lines and find the header to ID.
3969 Enter this line into the dependencies hash table, and return
3970 the id of the parent article (if any)."
3971   (let ((deps gnus-newsgroup-dependencies)
3972         found header)
3973     (prog1
3974         (save-excursion
3975           (set-buffer nntp-server-buffer)
3976           (let ((case-fold-search nil))
3977             (goto-char (point-min))
3978             (while (and (not found)
3979                         (search-forward id nil t))
3980               (beginning-of-line)
3981               (setq found (looking-at
3982                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3983                                    (regexp-quote id))))
3984               (or found (beginning-of-line 2)))
3985             (when found
3986               (beginning-of-line)
3987               (and
3988                (setq header (gnus-nov-parse-line
3989                              (read (current-buffer)) deps))
3990                (gnus-parent-id (mail-header-references header))))))
3991       (when header
3992         (let ((number (mail-header-number header)))
3993           (push number gnus-newsgroup-limit)
3994           (push header gnus-newsgroup-headers)
3995           (if (memq number gnus-newsgroup-unselected)
3996               (progn
3997                 (setq gnus-newsgroup-unreads
3998                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
3999                                                number))
4000                 (setq gnus-newsgroup-unselected
4001                       (delq number gnus-newsgroup-unselected)))
4002             (push number gnus-newsgroup-ancient)))))))
4003
4004 (defun gnus-build-all-threads ()
4005   "Read all the headers."
4006   (let ((gnus-summary-ignore-duplicates t)
4007         (mail-parse-charset gnus-newsgroup-charset)
4008         (dependencies gnus-newsgroup-dependencies)
4009         header article)
4010     (save-excursion
4011       (set-buffer nntp-server-buffer)
4012       (let ((case-fold-search nil))
4013         (goto-char (point-min))
4014         (while (not (eobp))
4015           (ignore-errors
4016             (setq article (read (current-buffer))
4017                   header (gnus-nov-parse-line article dependencies)))
4018           (when header
4019             (save-excursion
4020               (set-buffer gnus-summary-buffer)
4021               (push header gnus-newsgroup-headers)
4022               (if (memq (setq article (mail-header-number header))
4023                         gnus-newsgroup-unselected)
4024                   (progn
4025                     (setq gnus-newsgroup-unreads
4026                           (gnus-add-to-sorted-list
4027                            gnus-newsgroup-unreads article))
4028                     (setq gnus-newsgroup-unselected
4029                           (delq article gnus-newsgroup-unselected)))
4030                 (push article gnus-newsgroup-ancient)))
4031             (forward-line 1)))))))
4032
4033 (defun gnus-summary-update-article-line (article header)
4034   "Update the line for ARTICLE using HEADERS."
4035   (let* ((id (mail-header-id header))
4036          (thread (gnus-id-to-thread id)))
4037     (unless thread
4038       (error "Article in no thread"))
4039     ;; Update the thread.
4040     (setcar thread header)
4041     (gnus-summary-goto-subject article)
4042     (let* ((datal (gnus-data-find-list article))
4043            (data (car datal))
4044            (buffer-read-only nil)
4045            (level (gnus-summary-thread-level)))
4046       (gnus-delete-line)
4047       (let ((inserted (- (point)
4048                          (progn
4049                            (gnus-summary-insert-line
4050                             header level nil 
4051                             (memq article gnus-newsgroup-undownloaded)
4052                             (gnus-article-mark article)
4053                             (memq article gnus-newsgroup-replied)
4054                             (memq article gnus-newsgroup-expirable)
4055                             ;; Only insert the Subject string when it's different
4056                             ;; from the previous Subject string.
4057                             (if (and
4058                                  gnus-show-threads
4059                                  (gnus-subject-equal
4060                                   (condition-case ()
4061                                       (mail-header-subject
4062                                        (gnus-data-header
4063                                         (cadr
4064                                          (gnus-data-find-list
4065                                           article
4066                                           (gnus-data-list t)))))
4067                                     ;; Error on the side of excessive subjects.
4068                                     (error ""))
4069                                   (mail-header-subject header)))
4070                                 ""
4071                               (mail-header-subject header))
4072                             nil (cdr (assq article gnus-newsgroup-scored))
4073                             (memq article gnus-newsgroup-processable))
4074                            (point)))))
4075         (when (cdr datal)
4076           (gnus-data-update-list
4077            (cdr datal) 
4078            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4079
4080 (defun gnus-summary-update-article (article &optional iheader)
4081   "Update ARTICLE in the summary buffer."
4082   (set-buffer gnus-summary-buffer)
4083   (let* ((header (gnus-summary-article-header article))
4084          (id (mail-header-id header))
4085          (data (gnus-data-find article))
4086          (thread (gnus-id-to-thread id))
4087          (references (mail-header-references header))
4088          (parent
4089           (gnus-id-to-thread
4090            (or (gnus-parent-id
4091                 (when (and references
4092                            (not (equal "" references)))
4093                   references))
4094                "none")))
4095          (buffer-read-only nil)
4096          (old (car thread)))
4097     (when thread
4098       (unless iheader
4099         (setcar thread nil)
4100         (when parent
4101           (delq thread parent)))
4102       (if (gnus-summary-insert-subject id header)
4103           ;; Set the (possibly) new article number in the data structure.
4104           (gnus-data-set-number data (gnus-id-to-article id))
4105         (setcar thread old)
4106         nil))))
4107
4108 (defun gnus-rebuild-thread (id &optional line)
4109   "Rebuild the thread containing ID.
4110 If LINE, insert the rebuilt thread starting on line LINE."
4111   (let ((buffer-read-only nil)
4112         old-pos current thread data)
4113     (if (not gnus-show-threads)
4114         (setq thread (list (car (gnus-id-to-thread id))))
4115       ;; Get the thread this article is part of.
4116       (setq thread (gnus-remove-thread id)))
4117     (setq old-pos (gnus-point-at-bol))
4118     (setq current (save-excursion
4119                     (and (re-search-backward "[\r\n]" nil t)
4120                          (gnus-summary-article-number))))
4121     ;; If this is a gathered thread, we have to go some re-gathering.
4122     (when (stringp (car thread))
4123       (let ((subject (car thread))
4124             roots thr)
4125         (setq thread (cdr thread))
4126         (while thread
4127           (unless (memq (setq thr (gnus-id-to-thread
4128                                    (gnus-root-id
4129                                     (mail-header-id (caar thread)))))
4130                         roots)
4131             (push thr roots))
4132           (setq thread (cdr thread)))
4133         ;; We now have all (unique) roots.
4134         (if (= (length roots) 1)
4135             ;; All the loose roots are now one solid root.
4136             (setq thread (car roots))
4137           (setq thread (cons subject (gnus-sort-threads roots))))))
4138     (let (threads)
4139       ;; We then insert this thread into the summary buffer.
4140       (when line
4141         (goto-char (point-min))
4142         (forward-line (1- line)))
4143       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4144         (if gnus-show-threads
4145             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4146           (gnus-summary-prepare-unthreaded thread))
4147         (setq data (nreverse gnus-newsgroup-data))
4148         (setq threads gnus-newsgroup-threads))
4149       ;; We splice the new data into the data structure.
4150       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4151       ;;!!! then we want to insert at the beginning of the buffer.
4152       ;;!!! That happens to be true with Gnus now, but that may
4153       ;;!!! change in the future.  Perhaps.
4154       (gnus-data-enter-list
4155        (if line nil current) data (- (point) old-pos))
4156       (setq gnus-newsgroup-threads
4157             (nconc threads gnus-newsgroup-threads))
4158       (gnus-data-compute-positions))))
4159
4160 (defun gnus-number-to-header (number)
4161   "Return the header for article NUMBER."
4162   (let ((headers gnus-newsgroup-headers))
4163     (while (and headers
4164                 (not (= number (mail-header-number (car headers)))))
4165       (pop headers))
4166     (when headers
4167       (car headers))))
4168
4169 (defun gnus-parent-headers (in-headers &optional generation)
4170   "Return the headers of the GENERATIONeth parent of HEADERS."
4171   (unless generation
4172     (setq generation 1))
4173   (let ((parent t)
4174         (headers in-headers)
4175         references)
4176     (while (and parent
4177                 (not (zerop generation))
4178                 (setq references (mail-header-references headers)))
4179       (setq headers (if (and references
4180                              (setq parent (gnus-parent-id references)))
4181                         (car (gnus-id-to-thread parent))
4182                       nil))
4183       (decf generation))
4184     (and (not (eq headers in-headers))
4185          headers)))
4186
4187 (defun gnus-id-to-thread (id)
4188   "Return the (sub-)thread where ID appears."
4189   (gnus-gethash id gnus-newsgroup-dependencies))
4190
4191 (defun gnus-id-to-article (id)
4192   "Return the article number of ID."
4193   (let ((thread (gnus-id-to-thread id)))
4194     (when (and thread
4195                (car thread))
4196       (mail-header-number (car thread)))))
4197
4198 (defun gnus-id-to-header (id)
4199   "Return the article headers of ID."
4200   (car (gnus-id-to-thread id)))
4201
4202 (defun gnus-article-displayed-root-p (article)
4203   "Say whether ARTICLE is a root(ish) article."
4204   (let ((level (gnus-summary-thread-level article))
4205         (refs (mail-header-references  (gnus-summary-article-header article)))
4206         particle)
4207     (cond
4208      ((null level) nil)
4209      ((zerop level) t)
4210      ((null refs) t)
4211      ((null (gnus-parent-id refs)) t)
4212      ((and (= 1 level)
4213            (null (setq particle (gnus-id-to-article
4214                                  (gnus-parent-id refs))))
4215            (null (gnus-summary-thread-level particle)))))))
4216
4217 (defun gnus-root-id (id)
4218   "Return the id of the root of the thread where ID appears."
4219   (let (last-id prev)
4220     (while (and id (setq prev (car (gnus-id-to-thread id))))
4221       (setq last-id id
4222             id (gnus-parent-id (mail-header-references prev))))
4223     last-id))
4224
4225 (defun gnus-articles-in-thread (thread)
4226   "Return the list of articles in THREAD."
4227   (cons (mail-header-number (car thread))
4228         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4229
4230 (defun gnus-remove-thread (id &optional dont-remove)
4231   "Remove the thread that has ID in it."
4232   (let (headers thread last-id)
4233     ;; First go up in this thread until we find the root.
4234     (setq last-id (gnus-root-id id)
4235           headers (message-flatten-list (gnus-id-to-thread last-id)))
4236     ;; We have now found the real root of this thread.  It might have
4237     ;; been gathered into some loose thread, so we have to search
4238     ;; through the threads to find the thread we wanted.
4239     (let ((threads gnus-newsgroup-threads)
4240           sub)
4241       (while threads
4242         (setq sub (car threads))
4243         (if (stringp (car sub))
4244             ;; This is a gathered thread, so we look at the roots
4245             ;; below it to find whether this article is in this
4246             ;; gathered root.
4247             (progn
4248               (setq sub (cdr sub))
4249               (while sub
4250                 (when (member (caar sub) headers)
4251                   (setq thread (car threads)
4252                         threads nil
4253                         sub nil))
4254                 (setq sub (cdr sub))))
4255           ;; It's an ordinary thread, so we check it.
4256           (when (eq (car sub) (car headers))
4257             (setq thread sub
4258                   threads nil)))
4259         (setq threads (cdr threads)))
4260       ;; If this article is in no thread, then it's a root.
4261       (if thread
4262           (unless dont-remove
4263             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4264         (setq thread (gnus-id-to-thread last-id)))
4265       (when thread
4266         (prog1
4267             thread                      ; We return this thread.
4268           (unless dont-remove
4269             (if (stringp (car thread))
4270                 (progn
4271                   ;; If we use dummy roots, then we have to remove the
4272                   ;; dummy root as well.
4273                   (when (eq gnus-summary-make-false-root 'dummy)
4274                     ;; We go to the dummy root by going to
4275                     ;; the first sub-"thread", and then one line up.
4276                     (gnus-summary-goto-article
4277                      (mail-header-number (caadr thread)))
4278                     (forward-line -1)
4279                     (gnus-delete-line)
4280                     (gnus-data-compute-positions))
4281                   (setq thread (cdr thread))
4282                   (while thread
4283                     (gnus-remove-thread-1 (car thread))
4284                     (setq thread (cdr thread))))
4285               (gnus-remove-thread-1 thread))))))))
4286
4287 (defun gnus-remove-thread-1 (thread)
4288   "Remove the thread THREAD recursively."
4289   (let ((number (mail-header-number (pop thread)))
4290         d)
4291     (setq thread (reverse thread))
4292     (while thread
4293       (gnus-remove-thread-1 (pop thread)))
4294     (when (setq d (gnus-data-find number))
4295       (goto-char (gnus-data-pos d))
4296       (gnus-summary-show-thread)
4297       (gnus-data-remove
4298        number
4299        (- (gnus-point-at-bol)
4300           (prog1
4301               (1+ (gnus-point-at-eol))
4302             (gnus-delete-line)))))))
4303
4304 (defun gnus-sort-threads-1 (threads func)
4305   (sort (mapcar (lambda (thread)
4306                   (cons (car thread)
4307                         (and (cdr thread)
4308                              (gnus-sort-threads-1 (cdr thread) func))))
4309                 threads) func))
4310
4311 (defun gnus-sort-threads (threads)
4312   "Sort THREADS."
4313   (if (not gnus-thread-sort-functions)
4314       threads
4315     (gnus-message 8 "Sorting threads...")
4316     (let ((max-lisp-eval-depth 5000))
4317       (prog1 (gnus-sort-threads-1
4318          threads
4319          (gnus-make-sort-function gnus-thread-sort-functions))
4320         (gnus-message 8 "Sorting threads...done")))))
4321
4322 (defun gnus-sort-articles (articles)
4323   "Sort ARTICLES."
4324   (when gnus-article-sort-functions
4325     (gnus-message 7 "Sorting articles...")
4326     (prog1
4327         (setq gnus-newsgroup-headers
4328               (sort articles (gnus-make-sort-function
4329                               gnus-article-sort-functions)))
4330       (gnus-message 7 "Sorting articles...done"))))
4331
4332 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4333 (defmacro gnus-thread-header (thread)
4334   "Return header of first article in THREAD.
4335 Note that THREAD must never, ever be anything else than a variable -
4336 using some other form will lead to serious barfage."
4337   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4338   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4339   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4340         (vector thread) 2))
4341
4342 (defsubst gnus-article-sort-by-number (h1 h2)
4343   "Sort articles by article number."
4344   (< (mail-header-number h1)
4345      (mail-header-number h2)))
4346
4347 (defun gnus-thread-sort-by-number (h1 h2)
4348   "Sort threads by root article number."
4349   (gnus-article-sort-by-number
4350    (gnus-thread-header h1) (gnus-thread-header h2)))
4351
4352 (defsubst gnus-article-sort-by-random (h1 h2)
4353   "Sort articles by article number."
4354   (zerop (random 2)))
4355
4356 (defun gnus-thread-sort-by-random (h1 h2)
4357   "Sort threads by root article number."
4358   (gnus-article-sort-by-random
4359    (gnus-thread-header h1) (gnus-thread-header h2)))
4360
4361 (defsubst gnus-article-sort-by-lines (h1 h2)
4362   "Sort articles by article Lines header."
4363   (< (mail-header-lines h1)
4364      (mail-header-lines h2)))
4365
4366 (defun gnus-thread-sort-by-lines (h1 h2)
4367   "Sort threads by root article Lines header."
4368   (gnus-article-sort-by-lines
4369    (gnus-thread-header h1) (gnus-thread-header h2)))
4370
4371 (defsubst gnus-article-sort-by-chars (h1 h2)
4372   "Sort articles by octet length."
4373   (< (mail-header-chars h1)
4374      (mail-header-chars h2)))
4375
4376 (defun gnus-thread-sort-by-chars (h1 h2)
4377   "Sort threads by root article octet length."
4378   (gnus-article-sort-by-chars
4379    (gnus-thread-header h1) (gnus-thread-header h2)))
4380
4381 (defsubst gnus-article-sort-by-author (h1 h2)
4382   "Sort articles by root author."
4383   (string-lessp
4384    (let ((extract (funcall
4385                    gnus-extract-address-components
4386                    (mail-header-from h1))))
4387      (or (car extract) (cadr extract) ""))
4388    (let ((extract (funcall
4389                    gnus-extract-address-components
4390                    (mail-header-from h2))))
4391      (or (car extract) (cadr extract) ""))))
4392
4393 (defun gnus-thread-sort-by-author (h1 h2)
4394   "Sort threads by root author."
4395   (gnus-article-sort-by-author
4396    (gnus-thread-header h1)  (gnus-thread-header h2)))
4397
4398 (defsubst gnus-article-sort-by-subject (h1 h2)
4399   "Sort articles by root subject."
4400   (string-lessp
4401    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4402    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4403
4404 (defun gnus-thread-sort-by-subject (h1 h2)
4405   "Sort threads by root subject."
4406   (gnus-article-sort-by-subject
4407    (gnus-thread-header h1) (gnus-thread-header h2)))
4408
4409 (defsubst gnus-article-sort-by-date (h1 h2)
4410   "Sort articles by root article date."
4411   (time-less-p
4412    (gnus-date-get-time (mail-header-date h1))
4413    (gnus-date-get-time (mail-header-date h2))))
4414
4415 (defun gnus-thread-sort-by-date (h1 h2)
4416   "Sort threads by root article date."
4417   (gnus-article-sort-by-date
4418    (gnus-thread-header h1) (gnus-thread-header h2)))
4419
4420 (defsubst gnus-article-sort-by-score (h1 h2)
4421   "Sort articles by root article score.
4422 Unscored articles will be counted as having a score of zero."
4423   (> (or (cdr (assq (mail-header-number h1)
4424                     gnus-newsgroup-scored))
4425          gnus-summary-default-score 0)
4426      (or (cdr (assq (mail-header-number h2)
4427                     gnus-newsgroup-scored))
4428          gnus-summary-default-score 0)))
4429
4430 (defun gnus-thread-sort-by-score (h1 h2)
4431   "Sort threads by root article score."
4432   (gnus-article-sort-by-score
4433    (gnus-thread-header h1) (gnus-thread-header h2)))
4434
4435 (defun gnus-thread-sort-by-total-score (h1 h2)
4436   "Sort threads by the sum of all scores in the thread.
4437 Unscored articles will be counted as having a score of zero."
4438   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4439
4440 (defun gnus-thread-total-score (thread)
4441   ;; This function find the total score of THREAD.
4442   (cond
4443    ((null thread)
4444     0)
4445    ((consp thread)
4446     (if (stringp (car thread))
4447         (apply gnus-thread-score-function 0
4448                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4449       (gnus-thread-total-score-1 thread)))
4450    (t
4451     (gnus-thread-total-score-1 (list thread)))))
4452
4453 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4454   "Sort threads such that the thread with the most recently arrived article comes first."
4455   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4456
4457 (defun gnus-thread-highest-number (thread)
4458   "Return the highest article number in THREAD."
4459   (apply 'max (mapcar (lambda (header)
4460                         (mail-header-number header))
4461                       (message-flatten-list thread))))
4462
4463 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4464   "Sort threads such that the thread with the most recently dated article comes first."
4465   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4466
4467 (defun gnus-thread-latest-date (thread)
4468   "Return the highest article date in THREAD."
4469   (let ((previous-time 0))
4470     (apply 'max
4471            (mapcar
4472             (lambda (header)
4473               (setq previous-time
4474                     (time-to-seconds
4475                      (condition-case ()
4476                          (mail-header-parse-date (mail-header-date header))
4477                        (error previous-time)))))
4478             (sort
4479              (message-flatten-list thread)
4480              (lambda (h1 h2)
4481                (< (mail-header-number h1)
4482                   (mail-header-number h2))))))))
4483
4484 (defun gnus-thread-total-score-1 (root)
4485   ;; This function find the total score of the thread below ROOT.
4486   (setq root (car root))
4487   (apply gnus-thread-score-function
4488          (or (append
4489               (mapcar 'gnus-thread-total-score
4490                       (cdr (gnus-id-to-thread (mail-header-id root))))
4491               (when (> (mail-header-number root) 0)
4492                 (list (or (cdr (assq (mail-header-number root)
4493                                      gnus-newsgroup-scored))
4494                           gnus-summary-default-score 0))))
4495              (list gnus-summary-default-score)
4496              '(0))))
4497
4498 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4499 (defvar gnus-tmp-prev-subject nil)
4500 (defvar gnus-tmp-false-parent nil)
4501 (defvar gnus-tmp-root-expunged nil)
4502 (defvar gnus-tmp-dummy-line nil)
4503
4504 (eval-when-compile (defvar gnus-tmp-header))
4505 (defun gnus-extra-header (type &optional header)
4506   "Return the extra header of TYPE."
4507   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4508       ""))
4509
4510 (defvar gnus-tmp-thread-tree-header-string "")
4511
4512 (defcustom gnus-sum-thread-tree-root "> "
4513   "With %B spec, used for the root of a thread.
4514 If nil, use subject instead."
4515   :type 'string
4516   :group 'gnus-thread)
4517 (defcustom gnus-sum-thread-tree-single-indent ""
4518   "With %B spec, used for a thread with just one message.
4519 If nil, use subject instead."
4520   :type 'string
4521   :group 'gnus-thread)
4522 (defcustom gnus-sum-thread-tree-vertical "| "
4523   "With %B spec, used for drawing a vertical line."
4524   :type 'string
4525   :group 'gnus-thread)
4526 (defcustom gnus-sum-thread-tree-indent "  "
4527   "With %B spec, used for indenting."
4528   :type 'string
4529   :group 'gnus-thread)
4530 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4531   "With %B spec, used for a leaf with brothers."
4532   :type 'string
4533   :group 'gnus-thread)
4534 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4535   "With %B spec, used for a leaf without brothers."
4536   :type 'string
4537   :group 'gnus-thread)
4538
4539 (defun gnus-summary-prepare-threads (threads)
4540   "Prepare summary buffer from THREADS and indentation LEVEL.
4541 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4542 or a straight list of headers."
4543   (gnus-message 7 "Generating summary...")
4544
4545   (setq gnus-newsgroup-threads threads)
4546   (beginning-of-line)
4547
4548   (let ((gnus-tmp-level 0)
4549         (default-score (or gnus-summary-default-score 0))
4550         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4551         (building-line-count gnus-summary-display-while-building)
4552         (building-count (integerp gnus-summary-display-while-building))
4553         thread number subject stack state gnus-tmp-gathered beg-match
4554         new-roots gnus-tmp-new-adopts thread-end simp-subject
4555         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4556         gnus-tmp-replied gnus-tmp-subject-or-nil
4557         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4558         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4559         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4560         tree-stack)
4561
4562     (setq gnus-tmp-prev-subject nil
4563           gnus-tmp-thread-tree-header-string "")
4564
4565     (if (vectorp (car threads))
4566         ;; If this is a straight (sic) list of headers, then a
4567         ;; threaded summary display isn't required, so we just create
4568         ;; an unthreaded one.
4569         (gnus-summary-prepare-unthreaded threads)
4570
4571       ;; Do the threaded display.
4572
4573       (if gnus-summary-display-while-building
4574           (switch-to-buffer (buffer-name)))
4575       (while (or threads stack gnus-tmp-new-adopts new-roots)
4576
4577         (if (and (= gnus-tmp-level 0)
4578                  (or (not stack)
4579                      (= (caar stack) 0))
4580                  (not gnus-tmp-false-parent)
4581                  (or gnus-tmp-new-adopts new-roots))
4582             (if gnus-tmp-new-adopts
4583                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4584                       thread (list (car gnus-tmp-new-adopts))
4585                       gnus-tmp-header (caar thread)
4586                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4587               (when new-roots
4588                 (setq thread (list (car new-roots))
4589                       gnus-tmp-header (caar thread)
4590                       new-roots (cdr new-roots))))
4591
4592           (if threads
4593               ;; If there are some threads, we do them before the
4594               ;; threads on the stack.
4595               (setq thread threads
4596                     gnus-tmp-header (caar thread))
4597             ;; There were no current threads, so we pop something off
4598             ;; the stack.
4599             (setq state (car stack)
4600                   gnus-tmp-level (car state)
4601                   tree-stack (cadr state)
4602                   thread (caddr state)
4603                   stack (cdr stack)
4604                   gnus-tmp-header (caar thread))))
4605
4606         (setq gnus-tmp-false-parent nil)
4607         (setq gnus-tmp-root-expunged nil)
4608         (setq thread-end nil)
4609
4610         (if (stringp gnus-tmp-header)
4611             ;; The header is a dummy root.
4612             (cond
4613              ((eq gnus-summary-make-false-root 'adopt)
4614               ;; We let the first article adopt the rest.
4615               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4616                                                (cddar thread)))
4617               (setq gnus-tmp-gathered
4618                     (nconc (mapcar
4619                             (lambda (h) (mail-header-number (car h)))
4620                             (cddar thread))
4621                            gnus-tmp-gathered))
4622               (setq thread (cons (list (caar thread)
4623                                        (cadar thread))
4624                                  (cdr thread)))
4625               (setq gnus-tmp-level -1
4626                     gnus-tmp-false-parent t))
4627              ((eq gnus-summary-make-false-root 'empty)
4628               ;; We print adopted articles with empty subject fields.
4629               (setq gnus-tmp-gathered
4630                     (nconc (mapcar
4631                             (lambda (h) (mail-header-number (car h)))
4632                             (cddar thread))
4633                            gnus-tmp-gathered))
4634               (setq gnus-tmp-level -1))
4635              ((eq gnus-summary-make-false-root 'dummy)
4636               ;; We remember that we probably want to output a dummy
4637               ;; root.
4638               (setq gnus-tmp-dummy-line gnus-tmp-header)
4639               (setq gnus-tmp-prev-subject gnus-tmp-header))
4640              (t
4641               ;; We do not make a root for the gathered
4642               ;; sub-threads at all.
4643               (setq gnus-tmp-level -1)))
4644
4645           (setq number (mail-header-number gnus-tmp-header)
4646                 subject (mail-header-subject gnus-tmp-header)
4647                 simp-subject (gnus-simplify-subject-fully subject))
4648
4649           (cond
4650            ;; If the thread has changed subject, we might want to make
4651            ;; this subthread into a root.
4652            ((and (null gnus-thread-ignore-subject)
4653                  (not (zerop gnus-tmp-level))
4654                  gnus-tmp-prev-subject
4655                  (not (string= gnus-tmp-prev-subject simp-subject)))
4656             (setq new-roots (nconc new-roots (list (car thread)))
4657                   thread-end t
4658                   gnus-tmp-header nil))
4659            ;; If the article lies outside the current limit,
4660            ;; then we do not display it.
4661            ((not (memq number gnus-newsgroup-limit))
4662             (setq gnus-tmp-gathered
4663                   (nconc (mapcar
4664                           (lambda (h) (mail-header-number (car h)))
4665                           (cdar thread))
4666                          gnus-tmp-gathered))
4667             (setq gnus-tmp-new-adopts (if (cdar thread)
4668                                           (append gnus-tmp-new-adopts
4669                                                   (cdar thread))
4670                                         gnus-tmp-new-adopts)
4671                   thread-end t
4672                   gnus-tmp-header nil)
4673             (when (zerop gnus-tmp-level)
4674               (setq gnus-tmp-root-expunged t)))
4675            ;; Perhaps this article is to be marked as read?
4676            ((and gnus-summary-mark-below
4677                  (< (or (cdr (assq number gnus-newsgroup-scored))
4678                         default-score)
4679                     gnus-summary-mark-below)
4680                  ;; Don't touch sparse articles.
4681                  (not (gnus-summary-article-sparse-p number))
4682                  (not (gnus-summary-article-ancient-p number)))
4683             (setq gnus-newsgroup-unreads
4684                   (delq number gnus-newsgroup-unreads))
4685             (if gnus-newsgroup-auto-expire
4686                 (setq gnus-newsgroup-expirable
4687                       (gnus-add-to-sorted-list
4688                        gnus-newsgroup-expirable number))
4689               (push (cons number gnus-low-score-mark)
4690                     gnus-newsgroup-reads))))
4691
4692           (when gnus-tmp-header
4693             ;; We may have an old dummy line to output before this
4694             ;; article.
4695             (when (and gnus-tmp-dummy-line
4696                        (gnus-subject-equal
4697                         gnus-tmp-dummy-line
4698                         (mail-header-subject gnus-tmp-header)))
4699               (gnus-summary-insert-dummy-line
4700                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4701               (setq gnus-tmp-dummy-line nil))
4702
4703             ;; Compute the mark.
4704             (setq gnus-tmp-unread (gnus-article-mark number))
4705
4706             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4707                                   gnus-tmp-header gnus-tmp-level)
4708                   gnus-newsgroup-data)
4709
4710             ;; Actually insert the line.
4711             (setq
4712              gnus-tmp-subject-or-nil
4713              (cond
4714               ((and gnus-thread-ignore-subject
4715                     gnus-tmp-prev-subject
4716                     (not (string= gnus-tmp-prev-subject simp-subject)))
4717                subject)
4718               ((zerop gnus-tmp-level)
4719                (if (and (eq gnus-summary-make-false-root 'empty)
4720                         (memq number gnus-tmp-gathered)
4721                         gnus-tmp-prev-subject
4722                         (string= gnus-tmp-prev-subject simp-subject))
4723                    gnus-summary-same-subject
4724                  subject))
4725               (t gnus-summary-same-subject)))
4726             (if (and (eq gnus-summary-make-false-root 'adopt)
4727                      (= gnus-tmp-level 1)
4728                      (memq number gnus-tmp-gathered))
4729                 (setq gnus-tmp-opening-bracket ?\<
4730                       gnus-tmp-closing-bracket ?\>)
4731               (setq gnus-tmp-opening-bracket ?\[
4732                     gnus-tmp-closing-bracket ?\]))
4733             (setq
4734              gnus-tmp-indentation
4735              (aref gnus-thread-indent-array gnus-tmp-level)
4736              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4737              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4738                                 gnus-summary-default-score 0)
4739              gnus-tmp-score-char
4740              (if (or (null gnus-summary-default-score)
4741                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4742                          gnus-summary-zcore-fuzz))
4743                  ?                      ;Whitespace
4744                (if (< gnus-tmp-score gnus-summary-default-score)
4745                    gnus-score-below-mark gnus-score-over-mark))
4746              gnus-tmp-replied
4747              (cond ((memq number gnus-newsgroup-processable)
4748                     gnus-process-mark)
4749                    ((memq number gnus-newsgroup-cached)
4750                     gnus-cached-mark)
4751                    ((memq number gnus-newsgroup-replied)
4752                     gnus-replied-mark)
4753                    ((memq number gnus-newsgroup-forwarded)
4754                     gnus-forwarded-mark)
4755                    ((memq number gnus-newsgroup-saved)
4756                     gnus-saved-mark)
4757                    ((memq number gnus-newsgroup-recent)
4758                     gnus-recent-mark)
4759                    ((memq number gnus-newsgroup-unseen)
4760                     gnus-unseen-mark)
4761                    (t gnus-no-mark))
4762              gnus-tmp-downloaded
4763              (cond ((memq number gnus-newsgroup-undownloaded) 
4764                     gnus-undownloaded-mark)
4765                    (gnus-newsgroup-agentized
4766                     gnus-downloaded-mark)
4767                    (t
4768                     gnus-no-mark))
4769              gnus-tmp-from (mail-header-from gnus-tmp-header)
4770              gnus-tmp-name
4771              (cond
4772               ((string-match "<[^>]+> *$" gnus-tmp-from)
4773                (setq beg-match (match-beginning 0))
4774                (or (and (string-match "^\".+\"" gnus-tmp-from)
4775                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4776                    (substring gnus-tmp-from 0 beg-match)))
4777               ((string-match "(.+)" gnus-tmp-from)
4778                (substring gnus-tmp-from
4779                           (1+ (match-beginning 0)) (1- (match-end 0))))
4780               (t gnus-tmp-from))
4781              gnus-tmp-thread-tree-header-string
4782              (cond
4783               ((not gnus-show-threads) "")
4784               ((zerop gnus-tmp-level)
4785                (if (cdar thread)
4786                    (or gnus-sum-thread-tree-root subject)
4787                  (or gnus-sum-thread-tree-single-indent subject)))
4788               (t
4789                (concat (apply 'concat
4790                               (mapcar (lambda (item)
4791                                         (if (= item 1)
4792                                             gnus-sum-thread-tree-vertical
4793                                           gnus-sum-thread-tree-indent))
4794                                       (cdr (reverse tree-stack))))
4795                        (if (nth 1 thread)
4796                            gnus-sum-thread-tree-leaf-with-other
4797                          gnus-sum-thread-tree-single-leaf)))))
4798             (when (string= gnus-tmp-name "")
4799               (setq gnus-tmp-name gnus-tmp-from))
4800             (unless (numberp gnus-tmp-lines)
4801               (setq gnus-tmp-lines -1))
4802             (if (= gnus-tmp-lines -1)
4803                 (setq gnus-tmp-lines "?")
4804               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4805               (gnus-put-text-property
4806              (point)
4807              (progn (eval gnus-summary-line-format-spec) (point))
4808                'gnus-number number)
4809             (when gnus-visual-p
4810               (forward-line -1)
4811               (gnus-run-hooks 'gnus-summary-update-hook)
4812               (forward-line 1))
4813
4814             (setq gnus-tmp-prev-subject simp-subject)))
4815
4816         (when (nth 1 thread)
4817           (push (list (max 0 gnus-tmp-level)
4818                       (copy-list tree-stack)
4819                       (nthcdr 1 thread))
4820                 stack))
4821         (push (if (nth 1 thread) 1 0) tree-stack)
4822         (incf gnus-tmp-level)
4823         (setq threads (if thread-end nil (cdar thread)))
4824         (if gnus-summary-display-while-building
4825             (if building-count
4826                 (progn
4827                   ;; use a set frequency
4828                   (setq building-line-count (1- building-line-count))
4829                   (when (= building-line-count 0)
4830                     (sit-for 0)
4831                     (setq building-line-count
4832                           gnus-summary-display-while-building)))
4833               ;; always
4834               (sit-for 0)))
4835         (unless threads
4836           (setq gnus-tmp-level 0)))))
4837   (gnus-message 7 "Generating summary...done"))
4838
4839 (defun gnus-summary-prepare-unthreaded (headers)
4840   "Generate an unthreaded summary buffer based on HEADERS."
4841   (let (header number mark)
4842
4843     (beginning-of-line)
4844
4845     (while headers
4846       ;; We may have to root out some bad articles...
4847       (when (memq (setq number (mail-header-number
4848                                 (setq header (pop headers))))
4849                   gnus-newsgroup-limit)
4850         ;; Mark article as read when it has a low score.
4851         (when (and gnus-summary-mark-below
4852                    (< (or (cdr (assq number gnus-newsgroup-scored))
4853                           gnus-summary-default-score 0)
4854                       gnus-summary-mark-below)
4855                    (not (gnus-summary-article-ancient-p number)))
4856           (setq gnus-newsgroup-unreads
4857                 (delq number gnus-newsgroup-unreads))
4858           (if gnus-newsgroup-auto-expire
4859               (push number gnus-newsgroup-expirable)
4860             (push (cons number gnus-low-score-mark)
4861                   gnus-newsgroup-reads)))
4862
4863         (setq mark (gnus-article-mark number))
4864         (push (gnus-data-make number mark (1+ (point)) header 0)
4865               gnus-newsgroup-data)
4866         (gnus-summary-insert-line
4867          header 0 number
4868          (memq number gnus-newsgroup-undownloaded)
4869          mark (memq number gnus-newsgroup-replied)
4870          (memq number gnus-newsgroup-expirable)
4871          (mail-header-subject header) nil
4872          (cdr (assq number gnus-newsgroup-scored))
4873          (memq number gnus-newsgroup-processable))))))
4874
4875 (defun gnus-summary-remove-list-identifiers ()
4876   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4877   (let ((regexp (if (consp gnus-list-identifiers)
4878                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4879                   gnus-list-identifiers))
4880         changed subject)
4881     (when regexp
4882       (dolist (header gnus-newsgroup-headers)
4883         (setq subject (mail-header-subject header)
4884               changed nil)
4885         (while (string-match
4886                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4887                 subject)
4888           (setq subject
4889                 (concat (substring subject 0 (match-beginning 2))
4890                         (substring subject (match-end 0)))
4891                 changed t))
4892         (when (and changed
4893                    (string-match
4894                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4895           (setq subject
4896                 (concat (substring subject 0 (match-beginning 1))
4897                         (substring subject (match-end 1)))))
4898         (when changed
4899           (mail-header-set-subject header subject))))))
4900
4901 (defun gnus-fetch-headers (articles)
4902   "Fetch headers of ARTICLES."
4903   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4904     (gnus-message 5 "Fetching headers for %s..." name)
4905     (prog1
4906         (if (eq 'nov
4907                 (setq gnus-headers-retrieved-by
4908                       (gnus-retrieve-headers
4909                        articles gnus-newsgroup-name
4910                        ;; We might want to fetch old headers, but
4911                        ;; not if there is only 1 article.
4912                        (and (or (and
4913                                  (not (eq gnus-fetch-old-headers 'some))
4914                                  (not (numberp gnus-fetch-old-headers)))
4915                                 (> (length articles) 1))
4916                             gnus-fetch-old-headers))))
4917             (gnus-get-newsgroup-headers-xover
4918              articles nil nil gnus-newsgroup-name t)
4919           (gnus-get-newsgroup-headers))
4920       (gnus-message 5 "Fetching headers for %s...done" name))))
4921
4922 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4923   "Select newsgroup GROUP.
4924 If READ-ALL is non-nil, all articles in the group are selected.
4925 If SELECT-ARTICLES, only select those articles from GROUP."
4926   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4927          ;;!!! Dirty hack; should be removed.
4928          (gnus-summary-ignore-duplicates
4929           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4930               t
4931             gnus-summary-ignore-duplicates))
4932          (info (nth 2 entry))
4933          articles fetched-articles cached)
4934
4935     (unless (gnus-check-server
4936              (set (make-local-variable 'gnus-current-select-method)
4937                   (gnus-find-method-for-group group)))
4938       (error "Couldn't open server"))
4939
4940     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4941         (gnus-activate-group group)     ; Or we can activate it...
4942         (progn                          ; Or we bug out.
4943           (when (equal major-mode 'gnus-summary-mode)
4944             (gnus-kill-buffer (current-buffer)))
4945           (error "Couldn't activate group %s: %s"
4946                  group (gnus-status-message group))))
4947
4948     (unless (gnus-request-group group t)
4949       (when (equal major-mode 'gnus-summary-mode)
4950         (gnus-kill-buffer (current-buffer)))
4951       (error "Couldn't request group %s: %s"
4952              group (gnus-status-message group)))
4953
4954     (setq gnus-newsgroup-name group
4955           gnus-newsgroup-unselected nil
4956           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4957
4958     (let ((display (gnus-group-find-parameter group 'display)))
4959       (setq gnus-newsgroup-display
4960             (cond
4961              ((not (zerop (or (car-safe read-all) 0)))
4962               ;; The user entered the group with C-u SPC/RET, let's show
4963               ;; all articles.
4964               'gnus-not-ignore)
4965              ((eq display 'all)
4966               'gnus-not-ignore)
4967              ((arrayp display)
4968               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4969              ((numberp display)
4970               ;; The following is probably the "correct" solution, but
4971               ;; it makes Gnus fetch all headers and then limit the
4972               ;; articles (which is slow), so instead we hack the
4973               ;; select-articles parameter instead. -- Simon Josefsson
4974               ;; <jas@kth.se>
4975               ;;
4976               ;; (gnus-byte-compile
4977               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4978               ;;                         display)))))
4979               (setq select-articles
4980                     (gnus-uncompress-range
4981                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4982                              (if (> tmp 0)
4983                                  tmp
4984                                1))
4985                            (cdr (gnus-active group)))))
4986               nil)
4987              (t
4988               nil))))
4989
4990     (gnus-summary-setup-default-charset)
4991
4992     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4993     (when (gnus-virtual-group-p group)
4994       (setq cached gnus-newsgroup-cached))
4995
4996     (setq gnus-newsgroup-unreads
4997           (gnus-sorted-ndifference
4998            (gnus-sorted-ndifference gnus-newsgroup-unreads
4999                                     gnus-newsgroup-marked)
5000            gnus-newsgroup-dormant))
5001
5002     (setq gnus-newsgroup-processable nil)
5003
5004     (gnus-update-read-articles group gnus-newsgroup-unreads)
5005
5006     ;; Adjust and set lists of article marks.
5007     (when info
5008       (gnus-adjust-marked-articles info))
5009     (if (setq articles select-articles)
5010         (setq gnus-newsgroup-unselected
5011               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5012       (setq articles (gnus-articles-to-read group read-all)))
5013
5014     (cond
5015      ((null articles)
5016       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5017       'quit)
5018      ((eq articles 0) nil)
5019      (t
5020       ;; Init the dependencies hash table.
5021       (setq gnus-newsgroup-dependencies
5022             (gnus-make-hashtable (length articles)))
5023       (gnus-set-global-variables)
5024       ;; Retrieve the headers and read them in.
5025
5026       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5027
5028       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5029       (when cached
5030         (setq gnus-newsgroup-cached cached))
5031
5032       ;; Suppress duplicates?
5033       (when gnus-suppress-duplicates
5034         (gnus-dup-suppress-articles))
5035
5036       ;; Set the initial limit.
5037       (setq gnus-newsgroup-limit (copy-sequence articles))
5038       ;; Remove canceled articles from the list of unread articles.
5039       (setq fetched-articles
5040             (mapcar (lambda (headers) (mail-header-number headers))
5041                     gnus-newsgroup-headers))
5042       (setq gnus-newsgroup-articles fetched-articles)
5043       (setq gnus-newsgroup-unreads
5044             (gnus-sorted-nintersection
5045              gnus-newsgroup-unreads fetched-articles))
5046       (gnus-compute-unseen-list)
5047
5048       ;; Removed marked articles that do not exist.
5049       (gnus-update-missing-marks
5050        (gnus-sorted-difference articles fetched-articles))
5051       ;; We might want to build some more threads first.
5052       (when (and gnus-fetch-old-headers
5053                  (eq gnus-headers-retrieved-by 'nov))
5054         (if (eq gnus-fetch-old-headers 'invisible)
5055             (gnus-build-all-threads)
5056           (gnus-build-old-threads)))
5057       ;; Let the Gnus agent mark articles as read.
5058       (when gnus-agent
5059         (gnus-agent-get-undownloaded-list))
5060       ;; Remove list identifiers from subject
5061       (when gnus-list-identifiers
5062         (gnus-summary-remove-list-identifiers))
5063       ;; Check whether auto-expire is to be done in this group.
5064       (setq gnus-newsgroup-auto-expire
5065             (gnus-group-auto-expirable-p group))
5066       ;; Set up the article buffer now, if necessary.
5067       (unless gnus-single-article-buffer
5068         (gnus-article-setup-buffer))
5069       ;; First and last article in this newsgroup.
5070       (when gnus-newsgroup-headers
5071         (setq gnus-newsgroup-begin
5072               (mail-header-number (car gnus-newsgroup-headers))
5073               gnus-newsgroup-end
5074               (mail-header-number
5075                (gnus-last-element gnus-newsgroup-headers))))
5076       ;; GROUP is successfully selected.
5077       (or gnus-newsgroup-headers t)))))
5078
5079 (defun gnus-compute-unseen-list ()
5080   ;; The `seen' marks are treated specially.
5081   (if (not gnus-newsgroup-seen)
5082       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5083     (setq gnus-newsgroup-unseen
5084           (gnus-inverse-list-range-intersection
5085            gnus-newsgroup-articles gnus-newsgroup-seen))))
5086
5087 (defun gnus-summary-display-make-predicate (display)
5088   (require 'gnus-agent)
5089   (when (= (length display) 1)
5090     (setq display (car display)))
5091   (unless gnus-summary-display-cache
5092     (dolist (elem (append '((unread . unread)
5093                             (read . read)
5094                             (unseen . unseen))
5095                           gnus-article-mark-lists))
5096       (push (cons (cdr elem)
5097                   (gnus-byte-compile
5098                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5099             gnus-summary-display-cache)))
5100   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5101         (gnus-category-predicate-cache gnus-summary-display-cache))
5102     (gnus-get-predicate display)))
5103
5104 ;; Uses the dynamically bound `number' variable.
5105 (eval-when-compile
5106   (defvar number))
5107 (defun gnus-article-marked-p (type &optional article)
5108   (let ((article (or article number)))
5109     (cond
5110      ((eq type 'tick)
5111       (memq article gnus-newsgroup-marked))
5112      ((eq type 'spam)
5113       (memq article gnus-newsgroup-spam-marked))
5114      ((eq type 'unsend)
5115       (memq article gnus-newsgroup-unsendable))
5116      ((eq type 'undownload)
5117       (memq article gnus-newsgroup-undownloaded))
5118      ((eq type 'download)
5119       (memq article gnus-newsgroup-downloadable))
5120      ((eq type 'unread)
5121       (memq article gnus-newsgroup-unreads))
5122      ((eq type 'read)
5123       (memq article gnus-newsgroup-reads))
5124      ((eq type 'dormant)
5125       (memq article gnus-newsgroup-dormant) )
5126      ((eq type 'expire)
5127       (memq article gnus-newsgroup-expirable))
5128      ((eq type 'reply)
5129       (memq article gnus-newsgroup-replied))
5130      ((eq type 'killed)
5131       (memq article gnus-newsgroup-killed))
5132      ((eq type 'bookmark)
5133       (assq article gnus-newsgroup-bookmarks))
5134      ((eq type 'score)
5135       (assq article gnus-newsgroup-scored))
5136      ((eq type 'save)
5137       (memq article gnus-newsgroup-saved))
5138      ((eq type 'cache)
5139       (memq article gnus-newsgroup-cached))
5140      ((eq type 'forward)
5141       (memq article gnus-newsgroup-forwarded))
5142      ((eq type 'seen)
5143       (not (memq article gnus-newsgroup-unseen)))
5144      ((eq type 'recent)
5145       (memq article gnus-newsgroup-recent))
5146      (t t))))
5147
5148 (defun gnus-articles-to-read (group &optional read-all)
5149   "Find out what articles the user wants to read."
5150   (let* ((display (gnus-group-find-parameter group 'display))
5151          (articles
5152           ;; Select all articles if `read-all' is non-nil, or if there
5153           ;; are no unread articles.
5154           (if (or read-all
5155                   (and (zerop (length gnus-newsgroup-marked))
5156                        (zerop (length gnus-newsgroup-unreads)))
5157                   ;; Fetch all if the predicate is non-nil.
5158                   gnus-newsgroup-display)
5159               ;; We want to select the headers for all the articles in
5160               ;; the group, so we select either all the active
5161               ;; articles in the group, or (if that's nil), the
5162               ;; articles in the cache.
5163               (or
5164                (gnus-uncompress-range (gnus-active group))
5165                (gnus-cache-articles-in-group group))
5166             ;; Select only the "normal" subset of articles.
5167             (gnus-sorted-nunion
5168              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5169              gnus-newsgroup-unreads)))
5170          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5171          (scored (length scored-list))
5172          (number (length articles))
5173          (marked (+ (length gnus-newsgroup-marked)
5174                     (length gnus-newsgroup-dormant)))
5175          (select
5176           (cond
5177            ((numberp read-all)
5178             read-all)
5179            ((numberp gnus-newsgroup-display)
5180             gnus-newsgroup-display)
5181            (t
5182             (condition-case ()
5183                 (cond
5184                  ((and (or (<= scored marked) (= scored number))
5185                        (numberp gnus-large-newsgroup)
5186                        (> number gnus-large-newsgroup))
5187                   (let* ((cursor-in-echo-area nil)
5188                          (initial (gnus-parameter-large-newsgroup-initial
5189                                    gnus-newsgroup-name))
5190                          (input
5191                           (read-string
5192                            (format
5193                             "How many articles from %s (%s %d): "
5194                             (gnus-limit-string
5195                              (gnus-group-decoded-name gnus-newsgroup-name)
5196                              35)
5197                             (if initial "max" "default")
5198                             number)
5199                            (if initial
5200                                (cons (number-to-string initial)
5201                                      0)))))
5202                     (if (string-match "^[ \t]*$" input) number input)))
5203                  ((and (> scored marked) (< scored number)
5204                        (> (- scored number) 20))
5205                   (let ((input
5206                          (read-string
5207                           (format "%s %s (%d scored, %d total): "
5208                                   "How many articles from"
5209                                   (gnus-group-decoded-name group)
5210                                   scored number))))
5211                     (if (string-match "^[ \t]*$" input)
5212                         number input)))
5213                  (t number))
5214               (quit
5215                (message "Quit getting the articles to read")
5216                nil))))))
5217     (setq select (if (stringp select) (string-to-number select) select))
5218     (if (or (null select) (zerop select))
5219         select
5220       (if (and (not (zerop scored)) (<= (abs select) scored))
5221           (progn
5222             (setq articles (sort scored-list '<))
5223             (setq number (length articles)))
5224         (setq articles (copy-sequence articles)))
5225
5226       (when (< (abs select) number)
5227         (if (< select 0)
5228             ;; Select the N oldest articles.
5229             (setcdr (nthcdr (1- (abs select)) articles) nil)
5230           ;; Select the N most recent articles.
5231           (setq articles (nthcdr (- number select) articles))))
5232       (setq gnus-newsgroup-unselected
5233             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5234       (when gnus-alter-articles-to-read-function
5235         (setq articles
5236               (sort
5237                (funcall gnus-alter-articles-to-read-function
5238                         gnus-newsgroup-name articles)
5239                '<)))
5240       articles)))
5241
5242 (defun gnus-killed-articles (killed articles)
5243   (let (out)
5244     (while articles
5245       (when (inline (gnus-member-of-range (car articles) killed))
5246         (push (car articles) out))
5247       (setq articles (cdr articles)))
5248     out))
5249
5250 (defun gnus-uncompress-marks (marks)
5251   "Uncompress the mark ranges in MARKS."
5252   (let ((uncompressed '(score bookmark))
5253         out)
5254     (while marks
5255       (if (memq (caar marks) uncompressed)
5256           (push (car marks) out)
5257         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5258       (setq marks (cdr marks)))
5259     out))
5260
5261 (defun gnus-article-mark-to-type (mark)
5262   "Return the type of MARK."
5263   (or (cadr (assq mark gnus-article-special-mark-lists))
5264       'list))
5265
5266 (defun gnus-article-unpropagatable-p (mark)
5267   "Return whether MARK should be propagated to backend."
5268   (memq mark gnus-article-unpropagated-mark-lists))
5269
5270 (defun gnus-adjust-marked-articles (info)
5271   "Set all article lists and remove all marks that are no longer valid."
5272   (let* ((marked-lists (gnus-info-marks info))
5273          (active (gnus-active (gnus-info-group info)))
5274          (min (car active))
5275          (max (cdr active))
5276          (types gnus-article-mark-lists)
5277          marks var articles article mark mark-type)
5278
5279     (dolist (marks marked-lists)
5280       (setq mark (car marks)
5281             mark-type (gnus-article-mark-to-type mark)
5282             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5283
5284       ;; We set the variable according to the type of the marks list,
5285       ;; and then adjust the marks to a subset of the active articles.
5286       (cond
5287        ;; Adjust "simple" lists.
5288        ((eq mark-type 'list)
5289         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5290         (when (memq mark '(tick dormant expire reply save))
5291           (while articles
5292             (when (or (< (setq article (pop articles)) min) (> article max))
5293               (set var (delq article (symbol-value var)))))))
5294        ;; Adjust assocs.
5295        ((eq mark-type 'tuple)
5296         (set var (setq articles (cdr marks)))
5297         (when (not (listp (cdr (symbol-value var))))
5298           (set var (list (symbol-value var))))
5299         (when (not (listp (cdr articles)))
5300           (setq articles (list articles)))
5301         (while articles
5302           (when (or (not (consp (setq article (pop articles))))
5303                     (< (car article) min)
5304                     (> (car article) max))
5305             (set var (delq article (symbol-value var))))))
5306        ;; Adjust ranges (sloppily).
5307        ((eq mark-type 'range)
5308         (cond
5309          ((eq mark 'seen)
5310           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5311           ;; It should be (seen (NUM1 . NUM2)).
5312           (when (numberp (cddr marks))
5313             (setcdr marks (list (cdr marks))))
5314           (setq articles (cdr marks))
5315           (while (and articles
5316                       (or (and (consp (car articles))
5317                                (> min (cdar articles)))
5318                           (and (numberp (car articles))
5319                                (> min (car articles)))))
5320             (pop articles))
5321           (set var articles))))))))
5322
5323 (defun gnus-update-missing-marks (missing)
5324   "Go through the list of MISSING articles and remove them from the mark lists."
5325   (when missing
5326     (let (var m)
5327       ;; Go through all types.
5328       (dolist (elem gnus-article-mark-lists)
5329         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5330           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5331           (when (symbol-value var)
5332             ;; This list has articles.  So we delete all missing
5333             ;; articles from it.
5334             (setq m missing)
5335             (while m
5336               (set var (delq (pop m) (symbol-value var))))))))))
5337
5338 (defun gnus-update-marks ()
5339   "Enter the various lists of marked articles into the newsgroup info list."
5340   (let ((types gnus-article-mark-lists)
5341         (info (gnus-get-info gnus-newsgroup-name))
5342         type list newmarked symbol delta-marks)
5343     (when info
5344       ;; Add all marks lists to the list of marks lists.
5345       (while (setq type (pop types))
5346         (setq list (symbol-value
5347                     (setq symbol
5348                           (intern (format "gnus-newsgroup-%s" (car type))))))
5349
5350         (when list
5351           ;; Get rid of the entries of the articles that have the
5352           ;; default score.
5353           (when (and (eq (cdr type) 'score)
5354                      gnus-save-score
5355                      list)
5356             (let* ((arts list)
5357                    (prev (cons nil list))
5358                    (all prev))
5359               (while arts
5360                 (if (or (not (consp (car arts)))
5361                         (= (cdar arts) gnus-summary-default-score))
5362                     (setcdr prev (cdr arts))
5363                   (setq prev arts))
5364                 (setq arts (cdr arts)))
5365               (setq list (cdr all)))))
5366
5367         (when (eq (cdr type) 'seen)
5368           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5369
5370         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5371           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5372
5373         (when (and (gnus-check-backend-function
5374                     'request-set-mark gnus-newsgroup-name)
5375                    (not (gnus-article-unpropagatable-p (cdr type))))
5376           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5377                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5378                  (add (gnus-remove-from-range
5379                        (gnus-copy-sequence list) old)))
5380             (when add
5381               (push (list add 'add (list (cdr type))) delta-marks))
5382             (when del
5383               (push (list del 'del (list (cdr type))) delta-marks))))
5384
5385         (when list
5386           (push (cons (cdr type) list) newmarked)))
5387
5388       (when delta-marks
5389         (unless (gnus-check-group gnus-newsgroup-name)
5390           (error "Can't open server for %s" gnus-newsgroup-name))
5391         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5392
5393       ;; Enter these new marks into the info of the group.
5394       (if (nthcdr 3 info)
5395           (setcar (nthcdr 3 info) newmarked)
5396         ;; Add the marks lists to the end of the info.
5397         (when newmarked
5398           (setcdr (nthcdr 2 info) (list newmarked))))
5399
5400       ;; Cut off the end of the info if there's nothing else there.
5401       (let ((i 5))
5402         (while (and (> i 2)
5403                     (not (nth i info)))
5404           (when (nthcdr (decf i) info)
5405             (setcdr (nthcdr i info) nil)))))))
5406
5407 (defun gnus-set-mode-line (where)
5408   "Set the mode line of the article or summary buffers.
5409 If WHERE is `summary', the summary mode line format will be used."
5410   ;; Is this mode line one we keep updated?
5411   (when (and (memq where gnus-updated-mode-lines)
5412              (symbol-value
5413               (intern (format "gnus-%s-mode-line-format-spec" where))))
5414     (let (mode-string)
5415       (save-excursion
5416         ;; We evaluate this in the summary buffer since these
5417         ;; variables are buffer-local to that buffer.
5418         (set-buffer gnus-summary-buffer)
5419        ;; We bind all these variables that are used in the `eval' form
5420         ;; below.
5421         (let* ((mformat (symbol-value
5422                          (intern
5423                           (format "gnus-%s-mode-line-format-spec" where))))
5424                (gnus-tmp-group-name (gnus-group-decoded-name
5425                                      gnus-newsgroup-name))
5426                (gnus-tmp-article-number (or gnus-current-article 0))
5427                (gnus-tmp-unread gnus-newsgroup-unreads)
5428                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5429                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5430                (gnus-tmp-unread-and-unselected
5431                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5432                             (zerop gnus-tmp-unselected))
5433                        "")
5434                       ((zerop gnus-tmp-unselected)
5435                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5436                       (t (format "{%d(+%d) more}"
5437                                  gnus-tmp-unread-and-unticked
5438                                  gnus-tmp-unselected))))
5439                (gnus-tmp-subject
5440                 (if (and gnus-current-headers
5441                          (vectorp gnus-current-headers))
5442                     (gnus-mode-string-quote
5443                      (mail-header-subject gnus-current-headers))
5444                   ""))
5445                bufname-length max-len
5446                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5447           (setq mode-string (eval mformat))
5448           (setq bufname-length (if (string-match "%b" mode-string)
5449                                    (- (length
5450                                        (buffer-name
5451                                         (if (eq where 'summary)
5452                                             nil
5453                                           (get-buffer gnus-article-buffer))))
5454                                       2)
5455                                  0))
5456           (setq max-len (max 4 (if gnus-mode-non-string-length
5457                                    (- (window-width)
5458                                       gnus-mode-non-string-length
5459                                       bufname-length)
5460                                  (length mode-string))))
5461           ;; We might have to chop a bit of the string off...
5462           (when (> (length mode-string) max-len)
5463             (setq mode-string
5464                   (concat (truncate-string-to-width mode-string (- max-len 3))
5465                           "...")))
5466           ;; Pad the mode string a bit.
5467           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5468       ;; Update the mode line.
5469       (setq mode-line-buffer-identification
5470             (gnus-mode-line-buffer-identification (list mode-string)))
5471       (set-buffer-modified-p t))))
5472
5473 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5474   "Go through the HEADERS list and add all Xrefs to a hash table.
5475 The resulting hash table is returned, or nil if no Xrefs were found."
5476   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5477          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5478          (xref-hashtb (gnus-make-hashtable))
5479          start group entry number xrefs header)
5480     (while headers
5481       (setq header (pop headers))
5482       (when (and (setq xrefs (mail-header-xref header))
5483                  (not (memq (setq number (mail-header-number header))
5484                             unreads)))
5485         (setq start 0)
5486         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5487           (setq start (match-end 0))
5488           (setq group (if prefix
5489                           (concat prefix (substring xrefs (match-beginning 1)
5490                                                     (match-end 1)))
5491                         (substring xrefs (match-beginning 1) (match-end 1))))
5492           (setq number
5493                 (string-to-int (substring xrefs (match-beginning 2)
5494                                           (match-end 2))))
5495           (if (setq entry (gnus-gethash group xref-hashtb))
5496               (setcdr entry (cons number (cdr entry)))
5497             (gnus-sethash group (cons number nil) xref-hashtb)))))
5498     (and start xref-hashtb)))
5499
5500 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5501   "Look through all the headers and mark the Xrefs as read."
5502   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5503         name entry info xref-hashtb idlist method nth4)
5504     (save-excursion
5505       (set-buffer gnus-group-buffer)
5506       (when (setq xref-hashtb
5507                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5508         (mapatoms
5509          (lambda (group)
5510            (unless (string= from-newsgroup (setq name (symbol-name group)))
5511              (setq idlist (symbol-value group))
5512              ;; Dead groups are not updated.
5513              (and (prog1
5514                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5515                             info (nth 2 entry))
5516                     (when (stringp (setq nth4 (gnus-info-method info)))
5517                       (setq nth4 (gnus-server-to-method nth4))))
5518                   ;; Only do the xrefs if the group has the same
5519                   ;; select method as the group we have just read.
5520                   (or (gnus-methods-equal-p
5521                        nth4 (gnus-find-method-for-group from-newsgroup))
5522                       virtual
5523                       (equal nth4 (setq method (gnus-find-method-for-group
5524                                                 from-newsgroup)))
5525                       (and (equal (car nth4) (car method))
5526                            (equal (nth 1 nth4) (nth 1 method))))
5527                   gnus-use-cross-reference
5528                   (or (not (eq gnus-use-cross-reference t))
5529                       virtual
5530                       ;; Only do cross-references on subscribed
5531                       ;; groups, if that is what is wanted.
5532                       (<= (gnus-info-level info) gnus-level-subscribed))
5533                   (gnus-group-make-articles-read name idlist))))
5534          xref-hashtb)))))
5535
5536 (defun gnus-compute-read-articles (group articles)
5537   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5538          (info (nth 2 entry))
5539          (active (gnus-active group))
5540          ninfo)
5541     (when entry
5542       ;; First peel off all invalid article numbers.
5543       (when active
5544         (let ((ids articles)
5545               id first)
5546           (while (setq id (pop ids))
5547             (when (and first (> id (cdr active)))
5548               ;; We'll end up in this situation in one particular
5549               ;; obscure situation.  If you re-scan a group and get
5550               ;; a new article that is cross-posted to a different
5551               ;; group that has not been re-scanned, you might get
5552               ;; crossposted article that has a higher number than
5553               ;; Gnus believes possible.  So we re-activate this
5554               ;; group as well.  This might mean doing the
5555               ;; crossposting thingy will *increase* the number
5556               ;; of articles in some groups.  Tsk, tsk.
5557               (setq active (or (gnus-activate-group group) active)))
5558             (when (or (> id (cdr active))
5559                       (< id (car active)))
5560               (setq articles (delq id articles))))))
5561       ;; If the read list is nil, we init it.
5562       (if (and active
5563                (null (gnus-info-read info))
5564                (> (car active) 1))
5565           (setq ninfo (cons 1 (1- (car active))))
5566         (setq ninfo (gnus-info-read info)))
5567       ;; Then we add the read articles to the range.
5568       (gnus-add-to-range
5569        ninfo (setq articles (sort articles '<))))))
5570
5571 (defun gnus-group-make-articles-read (group articles)
5572   "Update the info of GROUP to say that ARTICLES are read."
5573   (let* ((num 0)
5574          (entry (gnus-gethash group gnus-newsrc-hashtb))
5575          (info (nth 2 entry))
5576          (active (gnus-active group))
5577          range)
5578     (when entry
5579       (setq range (gnus-compute-read-articles group articles))
5580       (save-excursion
5581         (set-buffer gnus-group-buffer)
5582         (gnus-undo-register
5583           `(progn
5584              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5585              (gnus-info-set-read ',info ',(gnus-info-read info))
5586              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5587              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5588              (gnus-group-update-group ,group t))))
5589       ;; Add the read articles to the range.
5590       (gnus-info-set-read info range)
5591       (gnus-request-set-mark group (list (list range 'add '(read))))
5592       ;; Then we have to re-compute how many unread
5593       ;; articles there are in this group.
5594       (when active
5595         (cond
5596          ((not range)
5597           (setq num (- (1+ (cdr active)) (car active))))
5598          ((not (listp (cdr range)))
5599           (setq num (- (cdr active) (- (1+ (cdr range))
5600                                        (car range)))))
5601          (t
5602           (while range
5603             (if (numberp (car range))
5604                 (setq num (1+ num))
5605               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5606             (setq range (cdr range)))
5607           (setq num (- (cdr active) num))))
5608         ;; Update the number of unread articles.
5609         (setcar entry num)
5610         ;; Update the group buffer.
5611         (unless (gnus-ephemeral-group-p group)
5612           (gnus-group-update-group group t))))))
5613
5614 (defvar gnus-newsgroup-none-id 0)
5615
5616 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5617   (let ((cur nntp-server-buffer)
5618         (dependencies
5619          (or dependencies
5620              (save-excursion (set-buffer gnus-summary-buffer)
5621                              gnus-newsgroup-dependencies)))
5622         headers id end ref
5623         (mail-parse-charset gnus-newsgroup-charset)
5624         (mail-parse-ignored-charsets
5625          (save-excursion (condition-case nil
5626                              (set-buffer gnus-summary-buffer)
5627                            (error))
5628                          gnus-newsgroup-ignored-charsets)))
5629     (save-excursion
5630       (set-buffer nntp-server-buffer)
5631       ;; Translate all TAB characters into SPACE characters.
5632       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5633       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5634       (gnus-run-hooks 'gnus-parse-headers-hook)
5635       (let ((case-fold-search t)
5636             in-reply-to header p lines chars)
5637         (goto-char (point-min))
5638         ;; Search to the beginning of the next header.  Error messages
5639         ;; do not begin with 2 or 3.
5640         (while (re-search-forward "^[23][0-9]+ " nil t)
5641           (setq id nil
5642                 ref nil)
5643           ;; This implementation of this function, with nine
5644           ;; search-forwards instead of the one re-search-forward and
5645           ;; a case (which basically was the old function) is actually
5646           ;; about twice as fast, even though it looks messier.  You
5647           ;; can't have everything, I guess.  Speed and elegance
5648           ;; doesn't always go hand in hand.
5649           (setq
5650            header
5651            (vector
5652             ;; Number.
5653             (prog1
5654                 (read cur)
5655               (end-of-line)
5656               (setq p (point))
5657               (narrow-to-region (point)
5658                                 (or (and (search-forward "\n.\n" nil t)
5659                                          (- (point) 2))
5660                                     (point))))
5661             ;; Subject.
5662             (progn
5663               (goto-char p)
5664               (if (search-forward "\nsubject:" nil t)
5665                   (funcall gnus-decode-encoded-word-function
5666                            (nnheader-header-value))
5667                 "(none)"))
5668             ;; From.
5669             (progn
5670               (goto-char p)
5671               (if (search-forward "\nfrom:" nil t)
5672                   (funcall gnus-decode-encoded-word-function
5673                            (nnheader-header-value))
5674                 "(nobody)"))
5675             ;; Date.
5676             (progn
5677               (goto-char p)
5678               (if (search-forward "\ndate:" nil t)
5679                   (nnheader-header-value) ""))
5680             ;; Message-ID.
5681             (progn
5682               (goto-char p)
5683               (setq id (if (re-search-forward
5684                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5685                            ;; We do it this way to make sure the Message-ID
5686                            ;; is (somewhat) syntactically valid.
5687                            (buffer-substring (match-beginning 1)
5688                                              (match-end 1))
5689                          ;; If there was no message-id, we just fake one
5690                          ;; to make subsequent routines simpler.
5691                          (nnheader-generate-fake-message-id))))
5692             ;; References.
5693             (progn
5694               (goto-char p)
5695               (if (search-forward "\nreferences:" nil t)
5696                   (progn
5697                     (setq end (point))
5698                     (prog1
5699                         (nnheader-header-value)
5700                       (setq ref
5701                             (buffer-substring
5702                              (progn
5703                                (end-of-line)
5704                                (search-backward ">" end t)
5705                                (1+ (point)))
5706                              (progn
5707                                (search-backward "<" end t)
5708                                (point))))))
5709                 ;; Get the references from the in-reply-to header if there
5710                 ;; were no references and the in-reply-to header looks
5711                 ;; promising.
5712                 (if (and (search-forward "\nin-reply-to:" nil t)
5713                          (setq in-reply-to (nnheader-header-value))
5714                          (string-match "<[^>]+>" in-reply-to))
5715                     (let (ref2)
5716                       (setq ref (substring in-reply-to (match-beginning 0)
5717                                            (match-end 0)))
5718                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5719                         (setq ref2 (substring in-reply-to (match-beginning 0)
5720                                               (match-end 0)))
5721                         (when (> (length ref2) (length ref))
5722                           (setq ref ref2)))
5723                       ref)
5724                   (setq ref nil))))
5725             ;; Chars.
5726             (progn
5727               (goto-char p)
5728               (if (search-forward "\nchars: " nil t)
5729                   (if (numberp (setq chars (ignore-errors (read cur))))
5730                       chars -1)
5731                 -1))
5732             ;; Lines.
5733             (progn
5734               (goto-char p)
5735               (if (search-forward "\nlines: " nil t)
5736                   (if (numberp (setq lines (ignore-errors (read cur))))
5737                       lines -1)
5738                 -1))
5739             ;; Xref.
5740             (progn
5741               (goto-char p)
5742               (and (search-forward "\nxref:" nil t)
5743                    (nnheader-header-value)))
5744             ;; Extra.
5745             (when gnus-extra-headers
5746               (let ((extra gnus-extra-headers)
5747                     out)
5748                 (while extra
5749                   (goto-char p)
5750                   (when (search-forward
5751                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5752                     (push (cons (car extra) (nnheader-header-value))
5753                           out))
5754                   (pop extra))
5755                 out))))
5756           (when (equal id ref)
5757             (setq ref nil))
5758
5759           (when gnus-alter-header-function
5760             (funcall gnus-alter-header-function header)
5761             (setq id (mail-header-id header)
5762                   ref (gnus-parent-id (mail-header-references header))))
5763
5764           (when (setq header
5765                       (gnus-dependencies-add-header
5766                        header dependencies force-new))
5767             (push header headers))
5768           (goto-char (point-max))
5769           (widen))
5770         (nreverse headers)))))
5771
5772 ;; Goes through the xover lines and returns a list of vectors
5773 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5774                                                   force-new dependencies
5775                                                   group also-fetch-heads)
5776   "Parse the news overview data in the server buffer.
5777 Return a list of headers that match SEQUENCE (see
5778 `nntp-retrieve-headers')."
5779   ;; Get the Xref when the users reads the articles since most/some
5780   ;; NNTP servers do not include Xrefs when using XOVER.
5781   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5782   (let ((mail-parse-charset gnus-newsgroup-charset)
5783         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5784         (cur nntp-server-buffer)
5785         (dependencies (or dependencies gnus-newsgroup-dependencies))
5786         (allp (cond
5787                ((eq gnus-read-all-available-headers t)
5788                 t)
5789                ((stringp gnus-read-all-available-headers)
5790                 (string-match gnus-read-all-available-headers group))
5791                (t
5792                 nil)))
5793         number headers header)
5794     (save-excursion
5795       (set-buffer nntp-server-buffer)
5796       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5797       ;; Allow the user to mangle the headers before parsing them.
5798       (gnus-run-hooks 'gnus-parse-headers-hook)
5799       (goto-char (point-min))
5800       (gnus-parse-without-error
5801         (while (and (or sequence allp)
5802                     (not (eobp)))
5803           (setq number (read cur))
5804           (when (not allp)
5805             (while (and sequence
5806                         (< (car sequence) number))
5807               (setq sequence (cdr sequence))))
5808           (when (and (or allp
5809                          (and sequence
5810                               (eq number (car sequence))))
5811                      (progn
5812                        (setq sequence (cdr sequence))
5813                        (setq header (inline
5814                                       (gnus-nov-parse-line
5815                                        number dependencies force-new)))))
5816             (push header headers))
5817           (forward-line 1)))
5818       ;; A common bug in inn is that if you have posted an article and
5819       ;; then retrieves the active file, it will answer correctly --
5820       ;; the new article is included.  However, a NOV entry for the
5821       ;; article may not have been generated yet, so this may fail.
5822       ;; We work around this problem by retrieving the last few
5823       ;; headers using HEAD.
5824       (if (or (not also-fetch-heads)
5825               (not sequence))
5826           ;; We (probably) got all the headers.
5827           (nreverse headers)
5828         (let ((gnus-nov-is-evil t))
5829           (nconc
5830            (nreverse headers)
5831            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5832              (gnus-get-newsgroup-headers))))))))
5833
5834 (defun gnus-article-get-xrefs ()
5835   "Fill in the Xref value in `gnus-current-headers', if necessary.
5836 This is meant to be called in `gnus-article-internal-prepare-hook'."
5837   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5838                                  gnus-current-headers)))
5839     (or (not gnus-use-cross-reference)
5840         (not headers)
5841         (and (mail-header-xref headers)
5842              (not (string= (mail-header-xref headers) "")))
5843         (let ((case-fold-search t)
5844               xref)
5845           (save-restriction
5846             (nnheader-narrow-to-headers)
5847             (goto-char (point-min))
5848             (when (or (and (not (eobp))
5849                            (eq (downcase (char-after)) ?x)
5850                            (looking-at "Xref:"))
5851                       (search-forward "\nXref:" nil t))
5852               (goto-char (1+ (match-end 0)))
5853               (setq xref (buffer-substring (point)
5854                                            (progn (end-of-line) (point))))
5855               (mail-header-set-xref headers xref)))))))
5856
5857 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5858   "Find article ID and insert the summary line for that article.
5859 OLD-HEADER can either be a header or a line number to insert
5860 the subject line on."
5861   (let* ((line (and (numberp old-header) old-header))
5862          (old-header (and (vectorp old-header) old-header))
5863          (header (cond ((and old-header use-old-header)
5864                         old-header)
5865                        ((and (numberp id)
5866                              (gnus-number-to-header id))
5867                         (gnus-number-to-header id))
5868                        (t
5869                         (gnus-read-header id))))
5870          (number (and (numberp id) id))
5871          d)
5872     (when header
5873       ;; Rebuild the thread that this article is part of and go to the
5874       ;; article we have fetched.
5875       (when (and (not gnus-show-threads)
5876                  old-header)
5877         (when (and number
5878                    (setq d (gnus-data-find (mail-header-number old-header))))
5879           (goto-char (gnus-data-pos d))
5880           (gnus-data-remove
5881            number
5882            (- (gnus-point-at-bol)
5883               (prog1
5884                   (1+ (gnus-point-at-eol))
5885                 (gnus-delete-line))))))
5886       (when old-header
5887         (mail-header-set-number header (mail-header-number old-header)))
5888       (setq gnus-newsgroup-sparse
5889             (delq (setq number (mail-header-number header))
5890                   gnus-newsgroup-sparse))
5891       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5892       (push number gnus-newsgroup-limit)
5893       (gnus-rebuild-thread (mail-header-id header) line)
5894       (gnus-summary-goto-subject number nil t))
5895     (when (and (numberp number)
5896                (> number 0))
5897       ;; We have to update the boundaries even if we can't fetch the
5898       ;; article if ID is a number -- so that the next `P' or `N'
5899       ;; command will fetch the previous (or next) article even
5900       ;; if the one we tried to fetch this time has been canceled.
5901       (when (> number gnus-newsgroup-end)
5902         (setq gnus-newsgroup-end number))
5903       (when (< number gnus-newsgroup-begin)
5904         (setq gnus-newsgroup-begin number))
5905       (setq gnus-newsgroup-unselected
5906             (delq number gnus-newsgroup-unselected)))
5907     ;; Report back a success?
5908     (and header (mail-header-number header))))
5909
5910 ;;; Process/prefix in the summary buffer
5911
5912 (defun gnus-summary-work-articles (n)
5913   "Return a list of articles to be worked upon.
5914 The prefix argument, the list of process marked articles, and the
5915 current article will be taken into consideration."
5916   (save-excursion
5917     (set-buffer gnus-summary-buffer)
5918     (cond
5919      (n
5920       ;; A numerical prefix has been given.
5921       (setq n (prefix-numeric-value n))
5922       (let ((backward (< n 0))
5923             (n (abs (prefix-numeric-value n)))
5924             articles article)
5925         (save-excursion
5926           (while
5927               (and (> n 0)
5928                    (push (setq article (gnus-summary-article-number))
5929                          articles)
5930                    (if backward
5931                        (gnus-summary-find-prev nil article)
5932                      (gnus-summary-find-next nil article)))
5933             (decf n)))
5934         (nreverse articles)))
5935      ((and (gnus-region-active-p) (mark))
5936       (message "region active")
5937       ;; Work on the region between point and mark.
5938       (let ((max (max (point) (mark)))
5939             articles article)
5940         (save-excursion
5941           (goto-char (min (min (point) (mark))))
5942           (while
5943               (and
5944                (push (setq article (gnus-summary-article-number)) articles)
5945                (gnus-summary-find-next nil article)
5946                (< (point) max)))
5947           (nreverse articles))))
5948      (gnus-newsgroup-processable
5949       ;; There are process-marked articles present.
5950       ;; Save current state.
5951       (gnus-summary-save-process-mark)
5952       ;; Return the list.
5953       (reverse gnus-newsgroup-processable))
5954      (t
5955       ;; Just return the current article.
5956       (list (gnus-summary-article-number))))))
5957
5958 (defmacro gnus-summary-iterate (arg &rest forms)
5959   "Iterate over the process/prefixed articles and do FORMS.
5960 ARG is the interactive prefix given to the command.  FORMS will be
5961 executed with point over the summary line of the articles."
5962   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5963     `(let ((,articles (gnus-summary-work-articles ,arg)))
5964        (while ,articles
5965          (gnus-summary-goto-subject (car ,articles))
5966          ,@forms
5967          (pop ,articles)))))
5968
5969 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5970 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5971
5972 (defun gnus-summary-save-process-mark ()
5973   "Push the current set of process marked articles on the stack."
5974   (interactive)
5975   (push (copy-sequence gnus-newsgroup-processable)
5976         gnus-newsgroup-process-stack))
5977
5978 (defun gnus-summary-kill-process-mark ()
5979   "Push the current set of process marked articles on the stack and unmark."
5980   (interactive)
5981   (gnus-summary-save-process-mark)
5982   (gnus-summary-unmark-all-processable))
5983
5984 (defun gnus-summary-yank-process-mark ()
5985   "Pop the last process mark state off the stack and restore it."
5986   (interactive)
5987   (unless gnus-newsgroup-process-stack
5988     (error "Empty mark stack"))
5989   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5990
5991 (defun gnus-summary-process-mark-set (set)
5992   "Make SET into the current process marked articles."
5993   (gnus-summary-unmark-all-processable)
5994   (while set
5995     (gnus-summary-set-process-mark (pop set))))
5996
5997 ;;; Searching and stuff
5998
5999 (defun gnus-summary-search-group (&optional backward use-level)
6000   "Search for next unread newsgroup.
6001 If optional argument BACKWARD is non-nil, search backward instead."
6002   (save-excursion
6003     (set-buffer gnus-group-buffer)
6004     (when (gnus-group-search-forward
6005            backward nil (if use-level (gnus-group-group-level) nil))
6006       (gnus-group-group-name))))
6007
6008 (defun gnus-summary-best-group (&optional exclude-group)
6009   "Find the name of the best unread group.
6010 If EXCLUDE-GROUP, do not go to this group."
6011   (save-excursion
6012     (set-buffer gnus-group-buffer)
6013     (save-excursion
6014       (gnus-group-best-unread-group exclude-group))))
6015
6016 (defun gnus-summary-find-next (&optional unread article backward)
6017   (if backward (gnus-summary-find-prev)
6018     (let* ((dummy (gnus-summary-article-intangible-p))
6019            (article (or article (gnus-summary-article-number)))
6020            (data (gnus-data-find-list article))
6021            result)
6022       (when (and (not dummy)
6023                  (or (not gnus-summary-check-current)
6024                      (not unread)
6025                      (not (gnus-data-unread-p (car data)))))
6026         (setq data (cdr data)))
6027       (when (setq result
6028                   (if unread
6029                       (progn
6030                         (while data
6031                           (unless (memq (gnus-data-number (car data)) 
6032                                         gnus-newsgroup-unfetched)
6033                             (when (gnus-data-unread-p (car data))
6034                               (setq result (car data)
6035                                     data nil)))
6036                           (setq data (cdr data)))
6037                         result)
6038                     (car data)))
6039         (goto-char (gnus-data-pos result))
6040         (gnus-data-number result)))))
6041
6042 (defun gnus-summary-find-prev (&optional unread article)
6043   (let* ((eobp (eobp))
6044          (article (or article (gnus-summary-article-number)))
6045          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6046          result)
6047     (when (and (not eobp)
6048                (or (not gnus-summary-check-current)
6049                    (not unread)
6050                    (not (gnus-data-unread-p (car data)))))
6051       (setq data (cdr data)))
6052     (when (setq result
6053                 (if unread
6054                     (progn
6055                       (while data
6056                         (unless (memq (gnus-data-number (car data)) gnus-newsgroup-unfetched)
6057                           (when (gnus-data-unread-p (car data))
6058                             (setq result (car data)
6059                                   data nil)))
6060                         (setq data (cdr data)))
6061                       result)
6062                   (car data)))
6063       (goto-char (gnus-data-pos result))
6064       (gnus-data-number result))))
6065
6066 (defun gnus-summary-find-subject (subject &optional unread backward article)
6067   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6068          (article (or article (gnus-summary-article-number)))
6069          (articles (gnus-data-list backward))
6070          (arts (gnus-data-find-list article articles))
6071          result)
6072     (when (or (not gnus-summary-check-current)
6073               (not unread)
6074               (not (gnus-data-unread-p (car arts))))
6075       (setq arts (cdr arts)))
6076     (while arts
6077       (and (or (not unread)
6078                (gnus-data-unread-p (car arts)))
6079            (vectorp (gnus-data-header (car arts)))
6080            (gnus-subject-equal
6081             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6082            (setq result (car arts)
6083                  arts nil))
6084       (setq arts (cdr arts)))
6085     (and result
6086          (goto-char (gnus-data-pos result))
6087          (gnus-data-number result))))
6088
6089 (defun gnus-summary-search-forward (&optional unread subject backward)
6090   "Search forward for an article.
6091 If UNREAD, look for unread articles.  If SUBJECT, look for
6092 articles with that subject.  If BACKWARD, search backward instead."
6093   (cond (subject (gnus-summary-find-subject subject unread backward))
6094         (backward (gnus-summary-find-prev unread))
6095         (t (gnus-summary-find-next unread))))
6096
6097 (defun gnus-recenter (&optional n)
6098   "Center point in window and redisplay frame.
6099 Also do horizontal recentering."
6100   (interactive "P")
6101   (when (and gnus-auto-center-summary
6102              (not (eq gnus-auto-center-summary 'vertical)))
6103     (gnus-horizontal-recenter))
6104   (recenter n))
6105
6106 (defun gnus-summary-recenter ()
6107   "Center point in the summary window.
6108 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6109 displayed, no centering will be performed."
6110   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6111   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6112   (interactive)
6113   ;; The user has to want it.
6114   (when gnus-auto-center-summary
6115     (let* ((top (cond ((< (window-height) 4) 0)
6116                       ((< (window-height) 7) 1)
6117                       (t (if (numberp gnus-auto-center-summary)
6118                              gnus-auto-center-summary
6119                            2))))
6120            (height (1- (window-height)))
6121            (bottom (save-excursion (goto-char (point-max))
6122                                    (forward-line (- height))
6123                                    (point)))
6124            (window (get-buffer-window (current-buffer))))
6125       (when (get-buffer-window gnus-article-buffer)
6126         ;; Only do recentering when the article buffer is displayed,
6127         ;; Set the window start to either `bottom', which is the biggest
6128         ;; possible valid number, or the second line from the top,
6129         ;; whichever is the least.
6130         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6131           (if (> bottom top-pos)
6132               ;; Keep the second line from the top visible
6133               (set-window-start window top-pos t)
6134             ;; Try to keep the bottom line visible; if it's partially
6135             ;; obscured, either scroll one more line to make it fully
6136             ;; visible, or revert to using TOP-POS.
6137             (save-excursion
6138               (goto-char (point-max))
6139               (forward-line -1)
6140               (let ((last-line-start (point)))
6141                 (goto-char bottom)
6142                 (set-window-start window (point) t)
6143                 (when (not (pos-visible-in-window-p last-line-start window))
6144                   (forward-line 1)
6145                   (set-window-start window (min (point) top-pos) t)))))))
6146       ;; Do horizontal recentering while we're at it.
6147       (when (and (get-buffer-window (current-buffer) t)
6148                  (not (eq gnus-auto-center-summary 'vertical)))
6149         (let ((selected (selected-window)))
6150           (select-window (get-buffer-window (current-buffer) t))
6151           (gnus-summary-position-point)
6152           (gnus-horizontal-recenter)
6153           (select-window selected))))))
6154
6155 (defun gnus-summary-jump-to-group (newsgroup)
6156   "Move point to NEWSGROUP in group mode buffer."
6157   ;; Keep update point of group mode buffer if visible.
6158   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6159       (save-window-excursion
6160         ;; Take care of tree window mode.
6161         (when (get-buffer-window gnus-group-buffer)
6162           (pop-to-buffer gnus-group-buffer))
6163         (gnus-group-jump-to-group newsgroup))
6164     (save-excursion
6165       ;; Take care of tree window mode.
6166       (if (get-buffer-window gnus-group-buffer)
6167           (pop-to-buffer gnus-group-buffer)
6168         (set-buffer gnus-group-buffer))
6169       (gnus-group-jump-to-group newsgroup))))
6170
6171 ;; This function returns a list of article numbers based on the
6172 ;; difference between the ranges of read articles in this group and
6173 ;; the range of active articles.
6174 (defun gnus-list-of-unread-articles (group)
6175   (let* ((read (gnus-info-read (gnus-get-info group)))
6176          (active (or (gnus-active group) (gnus-activate-group group)))
6177          (last (cdr active))
6178          first nlast unread)
6179     ;; If none are read, then all are unread.
6180     (if (not read)
6181         (setq first (car active))
6182       ;; If the range of read articles is a single range, then the
6183       ;; first unread article is the article after the last read
6184       ;; article.  Sounds logical, doesn't it?
6185       (if (and (not (listp (cdr read)))
6186                (or (< (car read) (car active))
6187                    (progn (setq read (list read))
6188                           nil)))
6189           (setq first (max (car active) (1+ (cdr read))))
6190         ;; `read' is a list of ranges.
6191         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6192                                   (caar read)))
6193                   1)
6194           (setq first (car active)))
6195         (while read
6196           (when first
6197             (while (< first nlast)
6198               (push first unread)
6199               (setq first (1+ first))))
6200           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6201           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6202           (setq read (cdr read)))))
6203     ;; And add the last unread articles.
6204     (while (<= first last)
6205       (push first unread)
6206       (setq first (1+ first)))
6207     ;; Return the list of unread articles.
6208     (delq 0 (nreverse unread))))
6209
6210 (defun gnus-list-of-read-articles (group)
6211   "Return a list of unread, unticked and non-dormant articles."
6212   (let* ((info (gnus-get-info group))
6213          (marked (gnus-info-marks info))
6214          (active (gnus-active group)))
6215     (and info active
6216          (gnus-list-range-difference
6217           (gnus-list-range-difference
6218            (gnus-sorted-complement
6219             (gnus-uncompress-range active)
6220             (gnus-list-of-unread-articles group))
6221            (cdr (assq 'dormant marked)))
6222           (cdr (assq 'tick marked))))))
6223
6224 ;; Various summary commands
6225
6226 (defun gnus-summary-select-article-buffer ()
6227   "Reconfigure windows to show article buffer."
6228   (interactive)
6229   (if (not (gnus-buffer-live-p gnus-article-buffer))
6230       (error "There is no article buffer for this summary buffer")
6231     (gnus-configure-windows 'article)
6232     (select-window (get-buffer-window gnus-article-buffer))))
6233
6234 (defun gnus-summary-universal-argument (arg)
6235   "Perform any operation on all articles that are process/prefixed."
6236   (interactive "P")
6237   (let ((articles (gnus-summary-work-articles arg))
6238         func article)
6239     (if (eq
6240          (setq
6241           func
6242           (key-binding
6243            (read-key-sequence
6244             (substitute-command-keys
6245              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6246          'undefined)
6247         (gnus-error 1 "Undefined key")
6248       (save-excursion
6249         (while articles
6250           (gnus-summary-goto-subject (setq article (pop articles)))
6251           (let (gnus-newsgroup-processable)
6252             (command-execute func))
6253           (gnus-summary-remove-process-mark article)))))
6254   (gnus-summary-position-point))
6255
6256 (defun gnus-summary-toggle-truncation (&optional arg)
6257   "Toggle truncation of summary lines.
6258 With arg, turn line truncation on if arg is positive."
6259   (interactive "P")
6260   (setq truncate-lines
6261         (if (null arg) (not truncate-lines)
6262           (> (prefix-numeric-value arg) 0)))
6263   (redraw-display))
6264
6265 (defun gnus-summary-find-uncancelled ()
6266   "Return the number of an uncancelled article.
6267 The current article is considered, then following articles, then previous
6268 articles.  If all articles are cancelled then return a dummy 0."
6269   (let (found)
6270     (dolist (rev '(nil t))
6271       (unless found      ; don't demand the reverse list if we don't need it
6272         (let ((data (gnus-data-find-list
6273                      (gnus-summary-article-number) (gnus-data-list rev))))
6274           (while (and data (not found))
6275             (if (not (eq gnus-canceled-mark (gnus-data-mark (car data))))
6276                 (setq found (gnus-data-number (car data))))
6277             (setq data (cdr data))))))
6278     (or found 0)))
6279
6280 (defun gnus-summary-reselect-current-group (&optional all rescan)
6281   "Exit and then reselect the current newsgroup.
6282 The prefix argument ALL means to select all articles."
6283   (interactive "P")
6284   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6285     (error "Ephemeral groups can't be reselected"))
6286   (let ((current-subject (gnus-summary-find-uncancelled))
6287         (group gnus-newsgroup-name))
6288     (setq gnus-newsgroup-begin nil)
6289     (gnus-summary-exit)
6290     ;; We have to adjust the point of group mode buffer because
6291     ;; point was moved to the next unread newsgroup by exiting.
6292     (gnus-summary-jump-to-group group)
6293     (when rescan
6294       (save-excursion
6295         (gnus-group-get-new-news-this-group 1)))
6296     (gnus-group-read-group all t)
6297     (gnus-summary-goto-subject current-subject nil t)))
6298
6299 (defun gnus-summary-rescan-group (&optional all)
6300   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6301   (interactive "P")
6302   (gnus-summary-reselect-current-group all t))
6303
6304 (defun gnus-summary-update-info (&optional non-destructive)
6305   (save-excursion
6306     (let ((group gnus-newsgroup-name))
6307       (when group
6308         (when gnus-newsgroup-kill-headers
6309           (setq gnus-newsgroup-killed
6310                 (gnus-compress-sequence
6311                  (gnus-sorted-union
6312                   (gnus-list-range-intersection
6313                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6314                   gnus-newsgroup-unreads)
6315                  t)))
6316         (unless (listp (cdr gnus-newsgroup-killed))
6317           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6318         (let ((headers gnus-newsgroup-headers))
6319           ;; Set the new ranges of read articles.
6320           (save-excursion
6321             (set-buffer gnus-group-buffer)
6322             (gnus-undo-force-boundary))
6323           (gnus-update-read-articles
6324            group (gnus-sorted-union
6325                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6326           ;; Set the current article marks.
6327           (let ((gnus-newsgroup-scored
6328                  (if (and (not gnus-save-score)
6329                           (not non-destructive))
6330                      nil
6331                    gnus-newsgroup-scored)))
6332             (save-excursion
6333               (gnus-update-marks)))
6334           ;; Do the cross-ref thing.
6335           (when gnus-use-cross-reference
6336             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6337           ;; Do not switch windows but change the buffer to work.
6338           (set-buffer gnus-group-buffer)
6339           (unless (gnus-ephemeral-group-p group)
6340             (gnus-group-update-group group)))))))
6341
6342 (defun gnus-summary-save-newsrc (&optional force)
6343   "Save the current number of read/marked articles in the dribble buffer.
6344 The dribble buffer will then be saved.
6345 If FORCE (the prefix), also save the .newsrc file(s)."
6346   (interactive "P")
6347   (gnus-summary-update-info t)
6348   (if force
6349       (gnus-save-newsrc-file)
6350     (gnus-dribble-save)))
6351
6352 (defun gnus-summary-exit (&optional temporary)
6353   "Exit reading current newsgroup, and then return to group selection mode.
6354 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6355   (interactive)
6356   (gnus-set-global-variables)
6357   (when (gnus-buffer-live-p gnus-article-buffer)
6358     (save-excursion
6359       (set-buffer gnus-article-buffer)
6360       (mm-destroy-parts gnus-article-mime-handles)
6361       ;; Set it to nil for safety reason.
6362       (setq gnus-article-mime-handle-alist nil)
6363       (setq gnus-article-mime-handles nil)))
6364   (gnus-kill-save-kill-buffer)
6365   (gnus-async-halt-prefetch)
6366   (let* ((group gnus-newsgroup-name)
6367          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6368          (gnus-group-is-exiting-p t)
6369          (mode major-mode)
6370          (group-point nil)
6371          (buf (current-buffer)))
6372     (unless quit-config
6373       ;; Do adaptive scoring, and possibly save score files.
6374       (when gnus-newsgroup-adaptive
6375         (gnus-score-adaptive))
6376       (when gnus-use-scoring
6377         (gnus-score-save)))
6378     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6379     ;; If we have several article buffers, we kill them at exit.
6380     (unless gnus-single-article-buffer
6381       (gnus-kill-buffer gnus-original-article-buffer)
6382       (setq gnus-article-current nil))
6383     (when gnus-use-cache
6384       (gnus-cache-possibly-remove-articles)
6385       (gnus-cache-save-buffers))
6386     (gnus-async-prefetch-remove-group group)
6387     (when gnus-suppress-duplicates
6388       (gnus-dup-enter-articles))
6389     (when gnus-use-trees
6390       (gnus-tree-close group))
6391     (when gnus-use-cache
6392       (gnus-cache-write-active))
6393     ;; Remove entries for this group.
6394     (nnmail-purge-split-history (gnus-group-real-name group))
6395     ;; Make all changes in this group permanent.
6396     (unless quit-config
6397       (gnus-run-hooks 'gnus-exit-group-hook)
6398       (gnus-summary-update-info))
6399     (gnus-close-group group)
6400     ;; Make sure where we were, and go to next newsgroup.
6401     (set-buffer gnus-group-buffer)
6402     (unless quit-config
6403       (gnus-group-jump-to-group group))
6404     (gnus-run-hooks 'gnus-summary-exit-hook)
6405     (unless (or quit-config
6406                 ;; If this group has disappeared from the summary
6407                 ;; buffer, don't skip forwards.
6408                 (not (string= group (gnus-group-group-name))))
6409       (gnus-group-next-unread-group 1))
6410     (setq group-point (point))
6411     (if temporary
6412         nil                             ;Nothing to do.
6413       ;; If we have several article buffers, we kill them at exit.
6414       (unless gnus-single-article-buffer
6415         (gnus-kill-buffer gnus-article-buffer)
6416         (gnus-kill-buffer gnus-original-article-buffer)
6417         (setq gnus-article-current nil))
6418       (set-buffer buf)
6419       (if (not gnus-kill-summary-on-exit)
6420           (progn
6421             (gnus-deaden-summary)
6422             (setq mode nil))
6423         ;; We set all buffer-local variables to nil.  It is unclear why
6424         ;; this is needed, but if we don't, buffer-local variables are
6425         ;; not garbage-collected, it seems.  This would the lead to en
6426         ;; ever-growing Emacs.
6427         (gnus-summary-clear-local-variables)
6428         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6429           (gnus-summary-clear-local-variables))
6430         (when (get-buffer gnus-article-buffer)
6431           (bury-buffer gnus-article-buffer))
6432         ;; We clear the global counterparts of the buffer-local
6433         ;; variables as well, just to be on the safe side.
6434         (set-buffer gnus-group-buffer)
6435         (gnus-summary-clear-local-variables)
6436         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6437           (gnus-summary-clear-local-variables))
6438         ;; Return to group mode buffer.
6439         (when (eq mode 'gnus-summary-mode)
6440           (gnus-kill-buffer buf)))
6441       (setq gnus-current-select-method gnus-select-method)
6442       (pop-to-buffer gnus-group-buffer)
6443       (if (not quit-config)
6444           (progn
6445             (goto-char group-point)
6446             (gnus-configure-windows 'group 'force))
6447         (gnus-handle-ephemeral-exit quit-config))
6448       ;; Clear the current group name.
6449       (unless quit-config
6450         (setq gnus-newsgroup-name nil)))))
6451
6452 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6453 (defun gnus-summary-exit-no-update (&optional no-questions)
6454   "Quit reading current newsgroup without updating read article info."
6455   (interactive)
6456   (let* ((group gnus-newsgroup-name)
6457          (gnus-group-is-exiting-p t)
6458          (quit-config (gnus-group-quit-config group)))
6459     (when (or no-questions
6460               gnus-expert-user
6461               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6462       (gnus-async-halt-prefetch)
6463       (mapcar 'funcall
6464               (delq 'gnus-summary-expire-articles
6465                     (copy-sequence gnus-summary-prepare-exit-hook)))
6466       (when (gnus-buffer-live-p gnus-article-buffer)
6467         (save-excursion
6468           (set-buffer gnus-article-buffer)
6469           (mm-destroy-parts gnus-article-mime-handles)
6470           ;; Set it to nil for safety reason.
6471           (setq gnus-article-mime-handle-alist nil)
6472           (setq gnus-article-mime-handles nil)))
6473       ;; If we have several article buffers, we kill them at exit.
6474       (unless gnus-single-article-buffer
6475         (gnus-kill-buffer gnus-article-buffer)
6476         (gnus-kill-buffer gnus-original-article-buffer)
6477         (setq gnus-article-current nil))
6478       (if (not gnus-kill-summary-on-exit)
6479           (gnus-deaden-summary)
6480         (gnus-close-group group)
6481         (gnus-summary-clear-local-variables)
6482         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6483           (gnus-summary-clear-local-variables))
6484         (set-buffer gnus-group-buffer)
6485         (gnus-summary-clear-local-variables)
6486         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6487           (gnus-summary-clear-local-variables))
6488         (when (get-buffer gnus-summary-buffer)
6489           (kill-buffer gnus-summary-buffer)))
6490       (unless gnus-single-article-buffer
6491         (setq gnus-article-current nil))
6492       (when gnus-use-trees
6493         (gnus-tree-close group))
6494       (gnus-async-prefetch-remove-group group)
6495       (when (get-buffer gnus-article-buffer)
6496         (bury-buffer gnus-article-buffer))
6497       ;; Return to the group buffer.
6498       (gnus-configure-windows 'group 'force)
6499       ;; Clear the current group name.
6500       (setq gnus-newsgroup-name nil)
6501       (unless (gnus-ephemeral-group-p group)
6502         (gnus-group-update-group group))
6503       (when (equal (gnus-group-group-name) group)
6504         (gnus-group-next-unread-group 1))
6505       (when quit-config
6506         (gnus-handle-ephemeral-exit quit-config)))))
6507
6508 (defun gnus-handle-ephemeral-exit (quit-config)
6509   "Handle movement when leaving an ephemeral group.
6510 The state which existed when entering the ephemeral is reset."
6511   (if (not (buffer-name (car quit-config)))
6512       (gnus-configure-windows 'group 'force)
6513     (set-buffer (car quit-config))
6514     (cond ((eq major-mode 'gnus-summary-mode)
6515            (gnus-set-global-variables))
6516           ((eq major-mode 'gnus-article-mode)
6517            (save-excursion
6518              ;; The `gnus-summary-buffer' variable may point
6519              ;; to the old summary buffer when using a single
6520              ;; article buffer.
6521              (unless (gnus-buffer-live-p gnus-summary-buffer)
6522                (set-buffer gnus-group-buffer))
6523              (set-buffer gnus-summary-buffer)
6524              (gnus-set-global-variables))))
6525     (if (or (eq (cdr quit-config) 'article)
6526             (eq (cdr quit-config) 'pick))
6527         (progn
6528           ;; The current article may be from the ephemeral group
6529           ;; thus it is best that we reload this article
6530           (gnus-summary-show-article)
6531           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6532               (gnus-configure-windows 'pick 'force)
6533             (gnus-configure-windows (cdr quit-config) 'force)))
6534       (gnus-configure-windows (cdr quit-config) 'force))
6535     (when (eq major-mode 'gnus-summary-mode)
6536       (gnus-summary-next-subject 1 nil t)
6537       (gnus-summary-recenter)
6538       (gnus-summary-position-point))))
6539
6540 ;;; Dead summaries.
6541
6542 (defvar gnus-dead-summary-mode-map nil)
6543
6544 (unless gnus-dead-summary-mode-map
6545   (setq gnus-dead-summary-mode-map (make-keymap))
6546   (suppress-keymap gnus-dead-summary-mode-map)
6547   (substitute-key-definition
6548    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6549   (dolist (key '("\C-d" "\r" "\177" [delete]))
6550     (define-key gnus-dead-summary-mode-map
6551       key 'gnus-summary-wake-up-the-dead))
6552   (dolist (key '("q" "Q"))
6553     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6554
6555 (defvar gnus-dead-summary-mode nil
6556   "Minor mode for Gnus summary buffers.")
6557
6558 (defun gnus-dead-summary-mode (&optional arg)
6559   "Minor mode for Gnus summary buffers."
6560   (interactive "P")
6561   (when (eq major-mode 'gnus-summary-mode)
6562     (make-local-variable 'gnus-dead-summary-mode)
6563     (setq gnus-dead-summary-mode
6564           (if (null arg) (not gnus-dead-summary-mode)
6565             (> (prefix-numeric-value arg) 0)))
6566     (when gnus-dead-summary-mode
6567       (gnus-add-minor-mode
6568        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6569
6570 (defun gnus-deaden-summary ()
6571   "Make the current summary buffer into a dead summary buffer."
6572   ;; Kill any previous dead summary buffer.
6573   (when (and gnus-dead-summary
6574              (buffer-name gnus-dead-summary))
6575     (save-excursion
6576       (set-buffer gnus-dead-summary)
6577       (when gnus-dead-summary-mode
6578         (kill-buffer (current-buffer)))))
6579   ;; Make this the current dead summary.
6580   (setq gnus-dead-summary (current-buffer))
6581   (gnus-dead-summary-mode 1)
6582   (let ((name (buffer-name)))
6583     (when (string-match "Summary" name)
6584       (rename-buffer
6585        (concat (substring name 0 (match-beginning 0)) "Dead "
6586                (substring name (match-beginning 0)))
6587        t)
6588       (bury-buffer))))
6589
6590 (defun gnus-kill-or-deaden-summary (buffer)
6591   "Kill or deaden the summary BUFFER."
6592   (save-excursion
6593     (when (and (buffer-name buffer)
6594                (not gnus-single-article-buffer))
6595       (save-excursion
6596         (set-buffer buffer)
6597         (gnus-kill-buffer gnus-article-buffer)
6598         (gnus-kill-buffer gnus-original-article-buffer)))
6599     (cond
6600      ;; Kill the buffer.
6601      (gnus-kill-summary-on-exit
6602       (when (and gnus-use-trees
6603                  (gnus-buffer-exists-p buffer))
6604         (save-excursion
6605           (set-buffer buffer)
6606           (gnus-tree-close gnus-newsgroup-name)))
6607       (gnus-kill-buffer buffer))
6608      ;; Deaden the buffer.
6609      ((gnus-buffer-exists-p buffer)
6610       (save-excursion
6611         (set-buffer buffer)
6612         (gnus-deaden-summary))))))
6613
6614 (defun gnus-summary-wake-up-the-dead (&rest args)
6615   "Wake up the dead summary buffer."
6616   (interactive)
6617   (gnus-dead-summary-mode -1)
6618   (let ((name (buffer-name)))
6619     (when (string-match "Dead " name)
6620       (rename-buffer
6621        (concat (substring name 0 (match-beginning 0))
6622                (substring name (match-end 0)))
6623        t)))
6624   (gnus-message 3 "This dead summary is now alive again"))
6625
6626 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6627 (defun gnus-summary-fetch-faq (&optional faq-dir)
6628   "Fetch the FAQ for the current group.
6629 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6630 in."
6631   (interactive
6632    (list
6633     (when current-prefix-arg
6634       (completing-read
6635        "FAQ dir: " (and (listp gnus-group-faq-directory)
6636                         (mapcar (lambda (file) (list file))
6637                                 gnus-group-faq-directory))))))
6638   (let (gnus-faq-buffer)
6639     (when (setq gnus-faq-buffer
6640                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6641       (gnus-configure-windows 'summary-faq))))
6642
6643 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6644 (defun gnus-summary-describe-group (&optional force)
6645   "Describe the current newsgroup."
6646   (interactive "P")
6647   (gnus-group-describe-group force gnus-newsgroup-name))
6648
6649 (defun gnus-summary-describe-briefly ()
6650   "Describe summary mode commands briefly."
6651   (interactive)
6652   (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")))
6653
6654 ;; Walking around group mode buffer from summary mode.
6655
6656 (defun gnus-summary-next-group (&optional no-article target-group backward)
6657   "Exit current newsgroup and then select next unread newsgroup.
6658 If prefix argument NO-ARTICLE is non-nil, no article is selected
6659 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6660 previous group instead."
6661   (interactive "P")
6662   ;; Stop pre-fetching.
6663   (gnus-async-halt-prefetch)
6664   (let ((current-group gnus-newsgroup-name)
6665         (current-buffer (current-buffer))
6666         entered)
6667     (while (not entered)
6668       ;; Then we find what group we are supposed to enter.
6669       (set-buffer gnus-group-buffer)
6670       (gnus-group-jump-to-group current-group)
6671       (setq target-group
6672             (or target-group
6673                 (if (eq gnus-keep-same-level 'best)
6674                     (gnus-summary-best-group gnus-newsgroup-name)
6675                   (gnus-summary-search-group backward gnus-keep-same-level))))
6676       (if (not target-group)
6677           ;; There are no further groups, so we return to the group
6678           ;; buffer.
6679           (progn
6680             (gnus-message 5 "Returning to the group buffer")
6681             (setq entered t)
6682             (when (gnus-buffer-live-p current-buffer)
6683               (set-buffer current-buffer)
6684               (gnus-summary-exit))
6685             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6686         ;; We try to enter the target group.
6687         (gnus-group-jump-to-group target-group)
6688         (let ((unreads (gnus-group-group-unread)))
6689           (if (and (or (eq t unreads)
6690                        (and unreads (not (zerop unreads))))
6691                    (progn
6692                      ;; Now we semi-exit this group to update Xrefs
6693                      ;; and all variables.  We can't do a real exit,
6694                      ;; because the window conf must remain the same
6695                      ;; in case the user is prompted for info, and we
6696                      ;; don't want the window conf to change before
6697                      ;; that...
6698                      (when (gnus-buffer-live-p current-buffer)
6699                        (set-buffer current-buffer)
6700                        (gnus-summary-exit t))
6701                      (gnus-summary-read-group
6702                       target-group nil no-article
6703                       (and (buffer-name current-buffer) current-buffer)
6704                       nil backward)))
6705               (setq entered t)
6706             (setq current-group target-group
6707                   target-group nil)))))))
6708
6709 (defun gnus-summary-prev-group (&optional no-article)
6710   "Exit current newsgroup and then select previous unread newsgroup.
6711 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6712   (interactive "P")
6713   (gnus-summary-next-group no-article nil t))
6714
6715 ;; Walking around summary lines.
6716
6717 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6718   "Go to the first subject satisfying any non-nil constraint.
6719 If UNREAD is non-nil, the article should be unread.
6720 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6721 If UNSEED is non-nil, the article should be unseen.
6722 Returns the article selected or nil if there are no matching articles."
6723   (interactive "P")
6724   (cond
6725    ;; Empty summary.
6726    ((null gnus-newsgroup-data)
6727     (gnus-message 3 "No articles in the group")
6728     nil)
6729    ;; Pick the first article.
6730    ((not (or unread undownloaded unseen))
6731     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6732     (gnus-data-number (car gnus-newsgroup-data)))
6733    ;; Find the first unread article.
6734    (t
6735     (let ((data gnus-newsgroup-data))
6736       (while (and data
6737                   (let ((num (gnus-data-number (car data))))
6738                     (or (memq num gnus-newsgroup-unfetched)
6739                         (not (or (and unread
6740                                       (memq num gnus-newsgroup-unreads))
6741                                  (and undownloaded
6742                                       (memq num gnus-newsgroup-undownloaded))
6743                                  (and unseen
6744                                       (memq num gnus-newsgroup-unseen)))))))
6745         (setq data (cdr data)))
6746       (prog1 
6747           (if data
6748               (progn
6749                 (goto-char (gnus-data-pos (car data)))
6750                 (gnus-data-number (car data)))
6751             (gnus-message 3 "No more%s articles"
6752                           (let* ((r (when unread " unread"))
6753                                  (d (when undownloaded " undownloaded"))
6754                                  (s (when unseen " unseen"))
6755                                  (l (delq nil (list r d s))))
6756                             (cond ((= 3 (length l))
6757                                    (concat r "," d ", or" s))
6758                                   ((= 2 (length l))
6759                                    (concat (car l) ", or" (cadr l)))
6760                                   ((= 1 (length l))
6761                                    (car l))
6762                                   (t
6763                                    ""))))
6764             nil
6765             )
6766         (gnus-summary-position-point))))))
6767
6768 (defun gnus-summary-next-subject (n &optional unread dont-display)
6769   "Go to next N'th summary line.
6770 If N is negative, go to the previous N'th subject line.
6771 If UNREAD is non-nil, only unread articles are selected.
6772 The difference between N and the actual number of steps taken is
6773 returned."
6774   (interactive "p")
6775   (let ((backward (< n 0))
6776         (n (abs n)))
6777     (while (and (> n 0)
6778                 (if backward
6779                     (gnus-summary-find-prev unread)
6780                   (gnus-summary-find-next unread)))
6781       (unless (zerop (setq n (1- n)))
6782         (gnus-summary-show-thread)))
6783     (when (/= 0 n)
6784       (gnus-message 7 "No more%s articles"
6785                     (if unread " unread" "")))
6786     (unless dont-display
6787       (gnus-summary-recenter)
6788       (gnus-summary-position-point))
6789     n))
6790
6791 (defun gnus-summary-next-unread-subject (n)
6792   "Go to next N'th unread summary line."
6793   (interactive "p")
6794   (gnus-summary-next-subject n t))
6795
6796 (defun gnus-summary-prev-subject (n &optional unread)
6797   "Go to previous N'th summary line.
6798 If optional argument UNREAD is non-nil, only unread article is selected."
6799   (interactive "p")
6800   (gnus-summary-next-subject (- n) unread))
6801
6802 (defun gnus-summary-prev-unread-subject (n)
6803   "Go to previous N'th unread summary line."
6804   (interactive "p")
6805   (gnus-summary-next-subject (- n) t))
6806
6807 (defun gnus-summary-goto-subjects (articles)
6808   "Insert the subject header for ARTICLES in the current buffer."
6809   (save-excursion
6810     (dolist (article articles)
6811       (gnus-summary-goto-subject article t)))
6812   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6813   (gnus-summary-position-point))
6814  
6815 (defun gnus-summary-goto-subject (article &optional force silent)
6816   "Go the subject line of ARTICLE.
6817 If FORCE, also allow jumping to articles not currently shown."
6818   (interactive "nArticle number: ")
6819   (unless (numberp article)
6820     (error "Article %s is not a number" article))
6821   (let ((b (point))
6822         (data (gnus-data-find article)))
6823     ;; We read in the article if we have to.
6824     (and (not data)
6825          force
6826          (gnus-summary-insert-subject
6827           article
6828           (if (or (numberp force) (vectorp force)) force)
6829           t)
6830          (setq data (gnus-data-find article)))
6831     (goto-char b)
6832     (if (not data)
6833         (progn
6834           (unless silent
6835             (gnus-message 3 "Can't find article %d" article))
6836           nil)
6837       (let ((pt (gnus-data-pos data)))
6838         (goto-char pt)
6839         (gnus-summary-set-article-display-arrow pt))
6840       (gnus-summary-position-point)
6841       article)))
6842
6843 ;; Walking around summary lines with displaying articles.
6844
6845 (defun gnus-summary-expand-window (&optional arg)
6846   "Make the summary buffer take up the entire Emacs frame.
6847 Given a prefix, will force an `article' buffer configuration."
6848   (interactive "P")
6849   (if arg
6850       (gnus-configure-windows 'article 'force)
6851     (gnus-configure-windows 'summary 'force)))
6852
6853 (defun gnus-summary-display-article (article &optional all-header)
6854   "Display ARTICLE in article buffer."
6855   (when (gnus-buffer-live-p gnus-article-buffer)
6856     (with-current-buffer gnus-article-buffer
6857       (mm-enable-multibyte)))
6858   (gnus-set-global-variables)
6859   (when (gnus-buffer-live-p gnus-article-buffer)
6860     (with-current-buffer gnus-article-buffer
6861       (setq gnus-article-charset gnus-newsgroup-charset)
6862       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6863       (mm-enable-multibyte)))
6864   (if (null article)
6865       nil
6866     (prog1
6867         (if gnus-summary-display-article-function
6868             (funcall gnus-summary-display-article-function article all-header)
6869           (gnus-article-prepare article all-header))
6870       (gnus-run-hooks 'gnus-select-article-hook)
6871       (when (and gnus-current-article
6872                  (not (zerop gnus-current-article)))
6873         (gnus-summary-goto-subject gnus-current-article))
6874       (gnus-summary-recenter)
6875       (when (and gnus-use-trees gnus-show-threads)
6876         (gnus-possibly-generate-tree article)
6877         (gnus-highlight-selected-tree article))
6878       ;; Successfully display article.
6879       (gnus-article-set-window-start
6880        (cdr (assq article gnus-newsgroup-bookmarks))))))
6881
6882 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6883   "Select the current article.
6884 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6885 non-nil, the article will be re-fetched even if it already present in
6886 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6887 be displayed."
6888   ;; Make sure we are in the summary buffer to work around bbdb bug.
6889   (unless (eq major-mode 'gnus-summary-mode)
6890     (set-buffer gnus-summary-buffer))
6891   (let ((article (or article (gnus-summary-article-number)))
6892         (all-headers (not (not all-headers))) ;Must be t or nil.
6893         gnus-summary-display-article-function)
6894     (and (not pseudo)
6895          (gnus-summary-article-pseudo-p article)
6896          (error "This is a pseudo-article"))
6897     (save-excursion
6898       (set-buffer gnus-summary-buffer)
6899       (if (or (and gnus-single-article-buffer
6900                    (or (null gnus-current-article)
6901                        (null gnus-article-current)
6902                        (null (get-buffer gnus-article-buffer))
6903                        (not (eq article (cdr gnus-article-current)))
6904                        (not (equal (car gnus-article-current)
6905                                    gnus-newsgroup-name))))
6906               (and (not gnus-single-article-buffer)
6907                    (or (null gnus-current-article)
6908                        (not (eq gnus-current-article article))))
6909               force)
6910           ;; The requested article is different from the current article.
6911           (progn
6912             (gnus-summary-display-article article all-headers)
6913             (when (gnus-buffer-live-p gnus-article-buffer)
6914               (with-current-buffer gnus-article-buffer
6915                 (if (not gnus-article-decoded-p) ;; a local variable
6916                     (mm-disable-multibyte))))
6917             (gnus-article-set-window-start
6918              (cdr (assq article gnus-newsgroup-bookmarks)))
6919             article)
6920         'old))))
6921
6922 (defun gnus-summary-force-verify-and-decrypt ()
6923   "Display buttons for signed/encrypted parts and verify/decrypt them."
6924   (interactive)
6925   (let ((mm-verify-option 'known)
6926         (mm-decrypt-option 'known)
6927         (gnus-buttonized-mime-types (append (list "multipart/signed"
6928                                                   "multipart/encrypted")
6929                                             gnus-buttonized-mime-types)))
6930     (gnus-summary-select-article nil 'force)))
6931
6932 (defun gnus-summary-set-current-mark (&optional current-mark)
6933   "Obsolete function."
6934   nil)
6935
6936 (defun gnus-summary-next-article (&optional unread subject backward push)
6937   "Select the next article.
6938 If UNREAD, only unread articles are selected.
6939 If SUBJECT, only articles with SUBJECT are selected.
6940 If BACKWARD, the previous article is selected instead of the next."
6941   (interactive "P")
6942   (cond
6943    ;; Is there such an article?
6944    ((and (gnus-summary-search-forward unread subject backward)
6945          (or (gnus-summary-display-article (gnus-summary-article-number))
6946              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6947     (gnus-summary-position-point))
6948    ;; If not, we try the first unread, if that is wanted.
6949    ((and subject
6950          gnus-auto-select-same
6951          (gnus-summary-first-unread-article))
6952     (gnus-summary-position-point)
6953     (gnus-message 6 "Wrapped"))
6954    ;; Try to get next/previous article not displayed in this group.
6955    ((and gnus-auto-extend-newsgroup
6956          (not unread) (not subject))
6957     (gnus-summary-goto-article
6958      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6959      nil (count-lines (point-min) (point))))
6960    ;; Go to next/previous group.
6961    (t
6962     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6963       (gnus-summary-jump-to-group gnus-newsgroup-name))
6964     (let ((cmd last-command-char)
6965           (point
6966            (save-excursion
6967              (set-buffer gnus-group-buffer)
6968              (point)))
6969           (group
6970            (if (eq gnus-keep-same-level 'best)
6971                (gnus-summary-best-group gnus-newsgroup-name)
6972              (gnus-summary-search-group backward gnus-keep-same-level))))
6973       ;; For some reason, the group window gets selected.  We change
6974       ;; it back.
6975       (select-window (get-buffer-window (current-buffer)))
6976       ;; Select next unread newsgroup automagically.
6977       (cond
6978        ((or (not gnus-auto-select-next)
6979             (not cmd))
6980         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6981        ((or (eq gnus-auto-select-next 'quietly)
6982             (and (eq gnus-auto-select-next 'slightly-quietly)
6983                  push)
6984             (and (eq gnus-auto-select-next 'almost-quietly)
6985                  (gnus-summary-last-article-p)))
6986         ;; Select quietly.
6987         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6988             (gnus-summary-exit)
6989           (gnus-message 7 "No more%s articles (%s)..."
6990                         (if unread " unread" "")
6991                         (if group (concat "selecting " group)
6992                           "exiting"))
6993           (gnus-summary-next-group nil group backward)))
6994        (t
6995         (when (gnus-key-press-event-p last-input-event)
6996           (gnus-summary-walk-group-buffer
6997            gnus-newsgroup-name cmd unread backward point))))))))
6998
6999 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7000   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7001                       (?\C-p (gnus-group-prev-unread-group 1))))
7002         (cursor-in-echo-area t)
7003         keve key group ended prompt)
7004     (save-excursion
7005       (set-buffer gnus-group-buffer)
7006       (goto-char start)
7007       (setq group
7008             (if (eq gnus-keep-same-level 'best)
7009                 (gnus-summary-best-group gnus-newsgroup-name)
7010               (gnus-summary-search-group backward gnus-keep-same-level))))
7011     (while (not ended)
7012       (setq prompt
7013             (format
7014              "No more%s articles%s " (if unread " unread" "")
7015              (if (and group
7016                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7017                  (format " (Type %s for %s [%s])"
7018                          (single-key-description cmd) group
7019                          (car (gnus-gethash group gnus-newsrc-hashtb)))
7020                (format " (Type %s to exit %s)"
7021                        (single-key-description cmd)
7022                        gnus-newsgroup-name))))
7023       ;; Confirm auto selection.
7024       (setq key (car (setq keve (gnus-read-event-char prompt)))
7025             ended t)
7026       (cond
7027        ((assq key keystrokes)
7028         (let ((obuf (current-buffer)))
7029           (switch-to-buffer gnus-group-buffer)
7030           (when group
7031             (gnus-group-jump-to-group group))
7032           (eval (cadr (assq key keystrokes)))
7033           (setq group (gnus-group-group-name))
7034           (switch-to-buffer obuf))
7035         (setq ended nil))
7036        ((equal key cmd)
7037         (if (or (not group)
7038                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7039             (gnus-summary-exit)
7040           (gnus-summary-next-group nil group backward)))
7041        (t
7042         (push (cdr keve) unread-command-events))))))
7043
7044 (defun gnus-summary-next-unread-article ()
7045   "Select unread article after current one."
7046   (interactive)
7047   (gnus-summary-next-article
7048    (or (not (eq gnus-summary-goto-unread 'never))
7049        (gnus-summary-last-article-p (gnus-summary-article-number)))
7050    (and gnus-auto-select-same
7051         (gnus-summary-article-subject))))
7052
7053 (defun gnus-summary-prev-article (&optional unread subject)
7054   "Select the article after the current one.
7055 If UNREAD is non-nil, only unread articles are selected."
7056   (interactive "P")
7057   (gnus-summary-next-article unread subject t))
7058
7059 (defun gnus-summary-prev-unread-article ()
7060   "Select unread article before current one."
7061   (interactive)
7062   (gnus-summary-prev-article
7063    (or (not (eq gnus-summary-goto-unread 'never))
7064        (gnus-summary-first-article-p (gnus-summary-article-number)))
7065    (and gnus-auto-select-same
7066         (gnus-summary-article-subject))))
7067
7068 (defun gnus-summary-next-page (&optional lines circular stop)
7069   "Show next page of the selected article.
7070 If at the end of the current article, select the next article.
7071 LINES says how many lines should be scrolled up.
7072
7073 If CIRCULAR is non-nil, go to the start of the article instead of
7074 selecting the next article when reaching the end of the current
7075 article.
7076
7077 If STOP is non-nil, just stop when reaching the end of the message."
7078   (interactive "P")
7079   (setq gnus-summary-buffer (current-buffer))
7080   (gnus-set-global-variables)
7081   (let ((article (gnus-summary-article-number))
7082         (article-window (get-buffer-window gnus-article-buffer t))
7083         endp)
7084     ;; If the buffer is empty, we have no article.
7085     (unless article
7086       (error "No article to select"))
7087     (gnus-configure-windows 'article)
7088     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7089         (if (and (eq gnus-summary-goto-unread 'never)
7090                  (not (gnus-summary-last-article-p article)))
7091             (gnus-summary-next-article)
7092           (gnus-summary-next-unread-article))
7093       (if (or (null gnus-current-article)
7094               (null gnus-article-current)
7095               (/= article (cdr gnus-article-current))
7096               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7097           ;; Selected subject is different from current article's.
7098           (gnus-summary-display-article article)
7099         (when article-window
7100           (gnus-eval-in-buffer-window gnus-article-buffer
7101             (setq endp (gnus-article-next-page lines)))
7102           (when endp
7103             (cond (stop
7104                    (gnus-message 3 "End of message"))
7105                   (circular
7106                    (gnus-summary-beginning-of-article))
7107                   (lines
7108                    (gnus-message 3 "End of message"))
7109                   ((null lines)
7110                    (if (and (eq gnus-summary-goto-unread 'never)
7111                             (not (gnus-summary-last-article-p article)))
7112                        (gnus-summary-next-article)
7113                      (gnus-summary-next-unread-article))))))))
7114     (gnus-summary-recenter)
7115     (gnus-summary-position-point)))
7116
7117 (defun gnus-summary-prev-page (&optional lines move)
7118   "Show previous page of selected article.
7119 Argument LINES specifies lines to be scrolled down.
7120 If MOVE, move to the previous unread article if point is at
7121 the beginning of the buffer."
7122   (interactive "P")
7123   (let ((article (gnus-summary-article-number))
7124         (article-window (get-buffer-window gnus-article-buffer t))
7125         endp)
7126     (gnus-configure-windows 'article)
7127     (if (or (null gnus-current-article)
7128             (null gnus-article-current)
7129             (/= article (cdr gnus-article-current))
7130             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7131         ;; Selected subject is different from current article's.
7132         (gnus-summary-display-article article)
7133       (gnus-summary-recenter)
7134       (when article-window
7135         (gnus-eval-in-buffer-window gnus-article-buffer
7136           (setq endp (gnus-article-prev-page lines)))
7137         (when (and move endp)
7138           (cond (lines
7139                  (gnus-message 3 "Beginning of message"))
7140                 ((null lines)
7141                  (if (and (eq gnus-summary-goto-unread 'never)
7142                           (not (gnus-summary-first-article-p article)))
7143                      (gnus-summary-prev-article)
7144                    (gnus-summary-prev-unread-article))))))))
7145   (gnus-summary-position-point))
7146
7147 (defun gnus-summary-prev-page-or-article (&optional lines)
7148   "Show previous page of selected article.
7149 Argument LINES specifies lines to be scrolled down.
7150 If at the beginning of the article, go to the next article."
7151   (interactive "P")
7152   (gnus-summary-prev-page lines t))
7153
7154 (defun gnus-summary-scroll-up (lines)
7155   "Scroll up (or down) one line current article.
7156 Argument LINES specifies lines to be scrolled up (or down if negative)."
7157   (interactive "p")
7158   (gnus-configure-windows 'article)
7159   (gnus-summary-show-thread)
7160   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7161     (gnus-eval-in-buffer-window gnus-article-buffer
7162       (cond ((> lines 0)
7163              (when (gnus-article-next-page lines)
7164                (gnus-message 3 "End of message")))
7165             ((< lines 0)
7166              (gnus-article-prev-page (- lines))))))
7167   (gnus-summary-recenter)
7168   (gnus-summary-position-point))
7169
7170 (defun gnus-summary-scroll-down (lines)
7171   "Scroll down (or up) one line current article.
7172 Argument LINES specifies lines to be scrolled down (or up if negative)."
7173   (interactive "p")
7174   (gnus-summary-scroll-up (- lines)))
7175
7176 (defun gnus-summary-next-same-subject ()
7177   "Select next article which has the same subject as current one."
7178   (interactive)
7179   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7180
7181 (defun gnus-summary-prev-same-subject ()
7182   "Select previous article which has the same subject as current one."
7183   (interactive)
7184   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7185
7186 (defun gnus-summary-next-unread-same-subject ()
7187   "Select next unread article which has the same subject as current one."
7188   (interactive)
7189   (gnus-summary-next-article t (gnus-summary-article-subject)))
7190
7191 (defun gnus-summary-prev-unread-same-subject ()
7192   "Select previous unread article which has the same subject as current one."
7193   (interactive)
7194   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7195
7196 (defun gnus-summary-first-unread-article ()
7197   "Select the first unread article.
7198 Return nil if there are no unread articles."
7199   (interactive)
7200   (prog1
7201       (when (gnus-summary-first-subject t)
7202         (gnus-summary-show-thread)
7203         (gnus-summary-first-subject t)
7204         (gnus-summary-display-article (gnus-summary-article-number)))
7205     (gnus-summary-position-point)))
7206
7207 (defun gnus-summary-first-unread-subject ()
7208   "Place the point on the subject line of the first unread article.
7209 Return nil if there are no unread articles."
7210   (interactive)
7211   (prog1
7212       (when (gnus-summary-first-subject t)
7213         (gnus-summary-show-thread)
7214         (gnus-summary-first-subject t))
7215     (gnus-summary-position-point)))
7216
7217 (defun gnus-summary-first-unseen-subject ()
7218   "Place the point on the subject line of the first unseen article.
7219 Return nil if there are no unseen articles."
7220   (interactive)
7221   (prog1
7222       (when (gnus-summary-first-subject nil nil t)
7223         (gnus-summary-show-thread)
7224         (gnus-summary-first-subject nil nil t))
7225     (gnus-summary-position-point)))
7226
7227 (defun gnus-summary-first-unseen-or-unread-subject ()
7228   "Place the point on the subject line of the first unseen article or,
7229 if all article have been seen, on the subject line of the first unread
7230 article."
7231   (interactive)
7232   (prog1
7233       (unless (when (gnus-summary-first-subject nil nil t)
7234                 (gnus-summary-show-thread)
7235                 (gnus-summary-first-subject nil nil t))
7236         (when (gnus-summary-first-subject t)
7237           (gnus-summary-show-thread)
7238           (gnus-summary-first-subject t)))
7239     (gnus-summary-position-point)))
7240
7241 (defun gnus-summary-first-article ()
7242   "Select the first article.
7243 Return nil if there are no articles."
7244   (interactive)
7245   (prog1
7246       (when (gnus-summary-first-subject)
7247         (gnus-summary-show-thread)
7248         (gnus-summary-first-subject)
7249         (gnus-summary-display-article (gnus-summary-article-number)))
7250     (gnus-summary-position-point)))
7251
7252 (defun gnus-summary-best-unread-article (&optional arg)
7253   "Select the unread article with the highest score.
7254 If given a prefix argument, select the next unread article that has a
7255 score higher than the default score."
7256   (interactive "P")
7257   (let ((article (if arg
7258                      (gnus-summary-better-unread-subject)
7259                    (gnus-summary-best-unread-subject))))
7260     (if article
7261         (gnus-summary-goto-article article)
7262       (error "No unread articles"))))
7263
7264 (defun gnus-summary-best-unread-subject ()
7265   "Select the unread subject with the highest score."
7266   (interactive)
7267   (let ((best -1000000)
7268         (data gnus-newsgroup-data)
7269         article score)
7270     (while data
7271       (and (gnus-data-unread-p (car data))
7272            (> (setq score
7273                     (gnus-summary-article-score (gnus-data-number (car data))))
7274               best)
7275            (setq best score
7276                  article (gnus-data-number (car data))))
7277       (setq data (cdr data)))
7278     (when article
7279       (gnus-summary-goto-subject article))
7280     (gnus-summary-position-point)
7281     article))
7282
7283 (defun gnus-summary-better-unread-subject ()
7284   "Select the first unread subject that has a score over the default score."
7285   (interactive)
7286   (let ((data gnus-newsgroup-data)
7287         article score)
7288     (while (and (setq article (gnus-data-number (car data)))
7289                 (or (gnus-data-read-p (car data))
7290                     (not (> (gnus-summary-article-score article)
7291                             gnus-summary-default-score))))
7292       (setq data (cdr data)))
7293     (when article
7294       (gnus-summary-goto-subject article))
7295     (gnus-summary-position-point)
7296     article))
7297
7298 (defun gnus-summary-last-subject ()
7299   "Go to the last displayed subject line in the group."
7300   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7301     (when article
7302       (gnus-summary-goto-subject article))))
7303
7304 (defun gnus-summary-goto-article (article &optional all-headers force)
7305   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7306 If ALL-HEADERS is non-nil, no header lines are hidden.
7307 If FORCE, go to the article even if it isn't displayed.  If FORCE
7308 is a number, it is the line the article is to be displayed on."
7309   (interactive
7310    (list
7311     (completing-read
7312      "Article number or Message-ID: "
7313      (mapcar (lambda (number) (list (int-to-string number)))
7314              gnus-newsgroup-limit))
7315     current-prefix-arg
7316     t))
7317   (prog1
7318       (if (and (stringp article)
7319                (string-match "@" article))
7320           (gnus-summary-refer-article article)
7321         (when (stringp article)
7322           (setq article (string-to-number article)))
7323         (if (gnus-summary-goto-subject article force)
7324             (gnus-summary-display-article article all-headers)
7325           (gnus-message 4 "Couldn't go to article %s" article) nil))
7326     (gnus-summary-position-point)))
7327
7328 (defun gnus-summary-goto-last-article ()
7329   "Go to the previously read article."
7330   (interactive)
7331   (prog1
7332       (when gnus-last-article
7333         (gnus-summary-goto-article gnus-last-article nil t))
7334     (gnus-summary-position-point)))
7335
7336 (defun gnus-summary-pop-article (number)
7337   "Pop one article off the history and go to the previous.
7338 NUMBER articles will be popped off."
7339   (interactive "p")
7340   (let (to)
7341     (setq gnus-newsgroup-history
7342           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7343     (if to
7344         (gnus-summary-goto-article (car to) nil t)
7345       (error "Article history empty")))
7346   (gnus-summary-position-point))
7347
7348 ;; Summary commands and functions for limiting the summary buffer.
7349
7350 (defun gnus-summary-limit-to-articles (n)
7351   "Limit the summary buffer to the next N articles.
7352 If not given a prefix, use the process marked articles instead."
7353   (interactive "P")
7354   (prog1
7355       (let ((articles (gnus-summary-work-articles n)))
7356         (setq gnus-newsgroup-processable nil)
7357         (gnus-summary-limit articles))
7358     (gnus-summary-position-point)))
7359
7360 (defun gnus-summary-pop-limit (&optional total)
7361   "Restore the previous limit.
7362 If given a prefix, remove all limits."
7363   (interactive "P")
7364   (when total
7365     (setq gnus-newsgroup-limits
7366           (list (mapcar (lambda (h) (mail-header-number h))
7367                         gnus-newsgroup-headers))))
7368   (unless gnus-newsgroup-limits
7369     (error "No limit to pop"))
7370   (prog1
7371       (gnus-summary-limit nil 'pop)
7372     (gnus-summary-position-point)))
7373
7374 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7375   "Limit the summary buffer to articles that have subjects that match a regexp.
7376 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7377   (interactive
7378    (list (read-string (if current-prefix-arg
7379                           "Exclude subject (regexp): "
7380                         "Limit to subject (regexp): "))
7381          nil current-prefix-arg))
7382   (unless header
7383     (setq header "subject"))
7384   (when (not (equal "" subject))
7385     (prog1
7386         (let ((articles (gnus-summary-find-matching
7387                          (or header "subject") subject 'all nil nil
7388                          not-matching)))
7389           (unless articles
7390             (error "Found no matches for \"%s\"" subject))
7391           (gnus-summary-limit articles))
7392       (gnus-summary-position-point))))
7393
7394 (defun gnus-summary-limit-to-author (from &optional not-matching)
7395   "Limit the summary buffer to articles that have authors that match a regexp.
7396 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7397   (interactive
7398    (list (read-string (if current-prefix-arg
7399                           "Exclude author (regexp): "
7400                         "Limit to author (regexp): "))
7401          current-prefix-arg))
7402   (gnus-summary-limit-to-subject from "from" not-matching))
7403
7404 (defun gnus-summary-limit-to-age (age &optional younger-p)
7405   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7406 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7407 articles that are younger than AGE days."
7408   (interactive
7409    (let ((younger current-prefix-arg)
7410          (days-got nil)
7411          days)
7412      (while (not days-got)
7413        (setq days (if younger
7414                       (read-string "Limit to articles younger than (in days, older when negative): ")
7415                     (read-string
7416                      "Limit to articles older than (in days, younger when negative): ")))
7417        (when (> (length days) 0)
7418          (setq days (read days)))
7419        (if (numberp days)
7420            (progn
7421              (setq days-got t)
7422              (if (< days 0)
7423                  (progn
7424                    (setq younger (not younger))
7425                    (setq days (* days -1)))))
7426          (message "Please enter a number.")
7427          (sleep-for 1)))
7428      (list days younger)))
7429   (prog1
7430       (let ((data gnus-newsgroup-data)
7431             (cutoff (days-to-time age))
7432             articles d date is-younger)
7433         (while (setq d (pop data))
7434           (when (and (vectorp (gnus-data-header d))
7435                      (setq date (mail-header-date (gnus-data-header d))))
7436             (setq is-younger (time-less-p
7437                               (time-since (condition-case ()
7438                                               (date-to-time date)
7439                                             (error '(0 0))))
7440                               cutoff))
7441             (when (if younger-p
7442                       is-younger
7443                     (not is-younger))
7444               (push (gnus-data-number d) articles))))
7445         (gnus-summary-limit (nreverse articles)))
7446     (gnus-summary-position-point)))
7447
7448 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7449   "Limit the summary buffer to articles that match an 'extra' header."
7450   (interactive
7451    (let ((header
7452           (intern
7453            (gnus-completing-read-with-default
7454             (symbol-name (car gnus-extra-headers))
7455             (if current-prefix-arg
7456                 "Exclude extra header:"
7457               "Limit extra header:")
7458             (mapcar (lambda (x)
7459                       (cons (symbol-name x) x))
7460                     gnus-extra-headers)
7461             nil
7462             t))))
7463      (list header
7464            (read-string (format "%s header %s (regexp): "
7465                                 (if current-prefix-arg "Exclude" "Limit to")
7466                                 header))
7467            current-prefix-arg)))
7468   (when (not (equal "" regexp))
7469     (prog1
7470         (let ((articles (gnus-summary-find-matching
7471                          (cons 'extra header) regexp 'all nil nil
7472                          not-matching)))
7473           (unless articles
7474             (error "Found no matches for \"%s\"" regexp))
7475           (gnus-summary-limit articles))
7476       (gnus-summary-position-point))))
7477
7478 (defun gnus-summary-limit-to-display-predicate ()
7479   "Limit the summary buffer to the predicated in the `display' group parameter."
7480   (interactive)
7481   (unless gnus-newsgroup-display
7482     (error "There is no `display' group parameter"))
7483   (let (articles)
7484     (dolist (number gnus-newsgroup-articles)
7485       (when (funcall gnus-newsgroup-display)
7486         (push number articles)))
7487     (gnus-summary-limit articles))
7488   (gnus-summary-position-point))
7489
7490 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7491 (make-obsolete
7492  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7493
7494 (defun gnus-summary-limit-to-unread (&optional all)
7495   "Limit the summary buffer to articles that are not marked as read.
7496 If ALL is non-nil, limit strictly to unread articles."
7497   (interactive "P")
7498   (if all
7499       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7500     (gnus-summary-limit-to-marks
7501      ;; Concat all the marks that say that an article is read and have
7502      ;; those removed.
7503      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7504            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7505            gnus-low-score-mark gnus-expirable-mark
7506            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7507            gnus-duplicate-mark gnus-souped-mark)
7508      'reverse)))
7509
7510 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7511 (make-obsolete 'gnus-summary-delete-marked-with
7512                'gnus-summary-limit-exclude-marks)
7513
7514 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7515   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7516 If REVERSE, limit the summary buffer to articles that are marked
7517 with MARKS.  MARKS can either be a string of marks or a list of marks.
7518 Returns how many articles were removed."
7519   (interactive "sMarks: ")
7520   (gnus-summary-limit-to-marks marks t))
7521
7522 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7523   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7524 If REVERSE (the prefix), limit the summary buffer to articles that are
7525 not marked with MARKS.  MARKS can either be a string of marks or a
7526 list of marks.
7527 Returns how many articles were removed."
7528   (interactive "sMarks: \nP")
7529   (prog1
7530       (let ((data gnus-newsgroup-data)
7531             (marks (if (listp marks) marks
7532                      (append marks nil))) ; Transform to list.
7533             articles)
7534         (while data
7535           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7536                   (memq (gnus-data-mark (car data)) marks))
7537             (push (gnus-data-number (car data)) articles))
7538           (setq data (cdr data)))
7539         (gnus-summary-limit articles))
7540     (gnus-summary-position-point)))
7541
7542 (defun gnus-summary-limit-to-score (score)
7543   "Limit to articles with score at or above SCORE."
7544   (interactive "NLimit to articles with score of at least: ")
7545   (let ((data gnus-newsgroup-data)
7546         articles)
7547     (while data
7548       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7549                 score)
7550         (push (gnus-data-number (car data)) articles))
7551       (setq data (cdr data)))
7552     (prog1
7553         (gnus-summary-limit articles)
7554       (gnus-summary-position-point))))
7555
7556 (defun gnus-summary-limit-to-unseen ()
7557   "Limit to unseen articles."
7558   (interactive)
7559   (prog1
7560       (gnus-summary-limit gnus-newsgroup-unseen)
7561     (gnus-summary-position-point)))
7562
7563 (defun gnus-summary-limit-include-thread (id)
7564   "Display all the hidden articles that is in the thread with ID in it.
7565 When called interactively, ID is the Message-ID of the current
7566 article."
7567   (interactive (list (mail-header-id (gnus-summary-article-header))))
7568   (let ((articles (gnus-articles-in-thread
7569                    (gnus-id-to-thread (gnus-root-id id)))))
7570     (prog1
7571         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7572       (gnus-summary-limit-include-matching-articles
7573        "subject"
7574        (regexp-quote (gnus-simplify-subject-re
7575                       (mail-header-subject (gnus-id-to-header id)))))
7576       (gnus-summary-position-point))))
7577
7578 (defun gnus-summary-limit-include-matching-articles (header regexp)
7579   "Display all the hidden articles that have HEADERs that match REGEXP."
7580   (interactive (list (read-string "Match on header: ")
7581                      (read-string "Regexp: ")))
7582   (let ((articles (gnus-find-matching-articles header regexp)))
7583     (prog1
7584         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7585       (gnus-summary-position-point))))
7586
7587 (defun gnus-summary-insert-dormant-articles ()
7588   "Insert all the dormat articles for this group into the current buffer."
7589   (interactive)
7590   (let ((gnus-verbose (max 6 gnus-verbose)))
7591     (if (not gnus-newsgroup-dormant)
7592         (gnus-message 3 "No cached articles for this group")
7593       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7594
7595 (defun gnus-summary-limit-include-dormant ()
7596   "Display all the hidden articles that are marked as dormant.
7597 Note that this command only works on a subset of the articles currently
7598 fetched for this group."
7599   (interactive)
7600   (unless gnus-newsgroup-dormant
7601     (error "There are no dormant articles in this group"))
7602   (prog1
7603       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7604     (gnus-summary-position-point)))
7605
7606 (defun gnus-summary-limit-exclude-dormant ()
7607   "Hide all dormant articles."
7608   (interactive)
7609   (prog1
7610       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7611     (gnus-summary-position-point)))
7612
7613 (defun gnus-summary-limit-exclude-childless-dormant ()
7614   "Hide all dormant articles that have no children."
7615   (interactive)
7616   (let ((data (gnus-data-list t))
7617         articles d children)
7618     ;; Find all articles that are either not dormant or have
7619     ;; children.
7620     (while (setq d (pop data))
7621       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7622                 (and (setq children
7623                            (gnus-article-children (gnus-data-number d)))
7624                      (let (found)
7625                        (while children
7626                          (when (memq (car children) articles)
7627                            (setq children nil
7628                                  found t))
7629                          (pop children))
7630                        found)))
7631         (push (gnus-data-number d) articles)))
7632     ;; Do the limiting.
7633     (prog1
7634         (gnus-summary-limit articles)
7635       (gnus-summary-position-point))))
7636
7637 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7638   "Mark all unread excluded articles as read.
7639 If ALL, mark even excluded ticked and dormants as read."
7640   (interactive "P")
7641   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7642   (let ((articles (gnus-sorted-ndifference
7643                    (sort
7644                     (mapcar (lambda (h) (mail-header-number h))
7645                             gnus-newsgroup-headers)
7646                     '<)
7647                    gnus-newsgroup-limit))
7648         article)
7649     (setq gnus-newsgroup-unreads
7650           (gnus-sorted-intersection gnus-newsgroup-unreads
7651                                     gnus-newsgroup-limit))
7652     (if all
7653         (setq gnus-newsgroup-dormant nil
7654               gnus-newsgroup-marked nil
7655               gnus-newsgroup-reads
7656               (nconc
7657                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7658                gnus-newsgroup-reads))
7659       (while (setq article (pop articles))
7660         (unless (or (memq article gnus-newsgroup-dormant)
7661                     (memq article gnus-newsgroup-marked))
7662           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7663
7664 (defun gnus-summary-limit (articles &optional pop)
7665   (if pop
7666       ;; We pop the previous limit off the stack and use that.
7667       (setq articles (car gnus-newsgroup-limits)
7668             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7669     ;; We use the new limit, so we push the old limit on the stack.
7670     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7671   ;; Set the limit.
7672   (setq gnus-newsgroup-limit articles)
7673   (let ((total (length gnus-newsgroup-data))
7674         (data (gnus-data-find-list (gnus-summary-article-number)))
7675         (gnus-summary-mark-below nil)   ; Inhibit this.
7676         found)
7677     ;; This will do all the work of generating the new summary buffer
7678     ;; according to the new limit.
7679     (gnus-summary-prepare)
7680     ;; Hide any threads, possibly.
7681     (gnus-summary-maybe-hide-threads)
7682     ;; Try to return to the article you were at, or one in the
7683     ;; neighborhood.
7684     (when data
7685       ;; We try to find some article after the current one.
7686       (while data
7687         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7688           (setq data nil
7689                 found t))
7690         (setq data (cdr data))))
7691     (unless found
7692       ;; If there is no data, that means that we were after the last
7693       ;; article.  The same goes when we can't find any articles
7694       ;; after the current one.
7695       (goto-char (point-max))
7696       (gnus-summary-find-prev))
7697     (gnus-set-mode-line 'summary)
7698     ;; We return how many articles were removed from the summary
7699     ;; buffer as a result of the new limit.
7700     (- total (length gnus-newsgroup-data))))
7701
7702 (defsubst gnus-invisible-cut-children (threads)
7703   (let ((num 0))
7704     (while threads
7705       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7706         (incf num))
7707       (pop threads))
7708     (< num 2)))
7709
7710 (defsubst gnus-cut-thread (thread)
7711   "Go forwards in the thread until we find an article that we want to display."
7712   (when (or (eq gnus-fetch-old-headers 'some)
7713             (eq gnus-fetch-old-headers 'invisible)
7714             (numberp gnus-fetch-old-headers)
7715             (eq gnus-build-sparse-threads 'some)
7716             (eq gnus-build-sparse-threads 'more))
7717     ;; Deal with old-fetched headers and sparse threads.
7718     (while (and
7719             thread
7720             (or
7721              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7722              (gnus-summary-article-ancient-p
7723               (mail-header-number (car thread))))
7724             (if (or (<= (length (cdr thread)) 1)
7725                     (eq gnus-fetch-old-headers 'invisible))
7726                 (setq gnus-newsgroup-limit
7727                       (delq (mail-header-number (car thread))
7728                             gnus-newsgroup-limit)
7729                       thread (cadr thread))
7730               (when (gnus-invisible-cut-children (cdr thread))
7731                 (let ((th (cdr thread)))
7732                   (while th
7733                     (if (memq (mail-header-number (caar th))
7734                               gnus-newsgroup-limit)
7735                         (setq thread (car th)
7736                               th nil)
7737                       (setq th (cdr th))))))))))
7738   thread)
7739
7740 (defun gnus-cut-threads (threads)
7741   "Cut off all uninteresting articles from the beginning of threads."
7742   (when (or (eq gnus-fetch-old-headers 'some)
7743             (eq gnus-fetch-old-headers 'invisible)
7744             (numberp gnus-fetch-old-headers)
7745             (eq gnus-build-sparse-threads 'some)
7746             (eq gnus-build-sparse-threads 'more))
7747     (let ((th threads))
7748       (while th
7749         (setcar th (gnus-cut-thread (car th)))
7750         (setq th (cdr th)))))
7751   ;; Remove nixed out threads.
7752   (delq nil threads))
7753
7754 (defun gnus-summary-initial-limit (&optional show-if-empty)
7755   "Figure out what the initial limit is supposed to be on group entry.
7756 This entails weeding out unwanted dormants, low-scored articles,
7757 fetch-old-headers verbiage, and so on."
7758   ;; Most groups have nothing to remove.
7759   (if (or gnus-inhibit-limiting
7760           (and (null gnus-newsgroup-dormant)
7761                (eq gnus-newsgroup-display 'gnus-not-ignore)
7762                (not (eq gnus-fetch-old-headers 'some))
7763                (not (numberp gnus-fetch-old-headers))
7764                (not (eq gnus-fetch-old-headers 'invisible))
7765                (null gnus-summary-expunge-below)
7766                (not (eq gnus-build-sparse-threads 'some))
7767                (not (eq gnus-build-sparse-threads 'more))
7768                (null gnus-thread-expunge-below)
7769                (not gnus-use-nocem)))
7770       ()                                ; Do nothing.
7771     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7772     (setq gnus-newsgroup-limit nil)
7773     (mapatoms
7774      (lambda (node)
7775        (unless (car (symbol-value node))
7776          ;; These threads have no parents -- they are roots.
7777          (let ((nodes (cdr (symbol-value node)))
7778                thread)
7779            (while nodes
7780              (if (and gnus-thread-expunge-below
7781                       (< (gnus-thread-total-score (car nodes))
7782                          gnus-thread-expunge-below))
7783                  (gnus-expunge-thread (pop nodes))
7784                (setq thread (pop nodes))
7785                (gnus-summary-limit-children thread))))))
7786      gnus-newsgroup-dependencies)
7787     ;; If this limitation resulted in an empty group, we might
7788     ;; pop the previous limit and use it instead.
7789     (when (and (not gnus-newsgroup-limit)
7790                show-if-empty)
7791       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7792     gnus-newsgroup-limit))
7793
7794 (defun gnus-summary-limit-children (thread)
7795   "Return 1 if this subthread is visible and 0 if it is not."
7796   ;; First we get the number of visible children to this thread.  This
7797   ;; is done by recursing down the thread using this function, so this
7798   ;; will really go down to a leaf article first, before slowly
7799   ;; working its way up towards the root.
7800   (when thread
7801     (let* ((max-lisp-eval-depth 5000)
7802            (children
7803            (if (cdr thread)
7804                (apply '+ (mapcar 'gnus-summary-limit-children
7805                                  (cdr thread)))
7806              0))
7807           (number (mail-header-number (car thread)))
7808           score)
7809       (if (and
7810            (not (memq number gnus-newsgroup-marked))
7811            (or
7812             ;; If this article is dormant and has absolutely no visible
7813             ;; children, then this article isn't visible.
7814             (and (memq number gnus-newsgroup-dormant)
7815                  (zerop children))
7816             ;; If this is "fetch-old-headered" and there is no
7817             ;; visible children, then we don't want this article.
7818             (and (or (eq gnus-fetch-old-headers 'some)
7819                      (numberp gnus-fetch-old-headers))
7820                  (gnus-summary-article-ancient-p number)
7821                  (zerop children))
7822             ;; If this is "fetch-old-headered" and `invisible', then
7823             ;; we don't want this article.
7824             (and (eq gnus-fetch-old-headers 'invisible)
7825                  (gnus-summary-article-ancient-p number))
7826             ;; If this is a sparsely inserted article with no children,
7827             ;; we don't want it.
7828             (and (eq gnus-build-sparse-threads 'some)
7829                  (gnus-summary-article-sparse-p number)
7830                  (zerop children))
7831             ;; If we use expunging, and this article is really
7832             ;; low-scored, then we don't want this article.
7833             (when (and gnus-summary-expunge-below
7834                        (< (setq score
7835                                 (or (cdr (assq number gnus-newsgroup-scored))
7836                                     gnus-summary-default-score))
7837                           gnus-summary-expunge-below))
7838               ;; We increase the expunge-tally here, but that has
7839               ;; nothing to do with the limits, really.
7840               (incf gnus-newsgroup-expunged-tally)
7841               ;; We also mark as read here, if that's wanted.
7842               (when (and gnus-summary-mark-below
7843                          (< score gnus-summary-mark-below))
7844                 (setq gnus-newsgroup-unreads
7845                       (delq number gnus-newsgroup-unreads))
7846                 (if gnus-newsgroup-auto-expire
7847                     (push number gnus-newsgroup-expirable)
7848                   (push (cons number gnus-low-score-mark)
7849                         gnus-newsgroup-reads)))
7850               t)
7851             ;; Do the `display' group parameter.
7852             (and gnus-newsgroup-display
7853                  (not (funcall gnus-newsgroup-display)))
7854             ;; Check NoCeM things.
7855             (if (and gnus-use-nocem
7856                      (gnus-nocem-unwanted-article-p
7857                       (mail-header-id (car thread))))
7858                 (progn
7859                   (setq gnus-newsgroup-unreads
7860                         (delq number gnus-newsgroup-unreads))
7861                   t))))
7862           ;; Nope, invisible article.
7863           0
7864         ;; Ok, this article is to be visible, so we add it to the limit
7865         ;; and return 1.
7866         (push number gnus-newsgroup-limit)
7867         1))))
7868
7869 (defun gnus-expunge-thread (thread)
7870   "Mark all articles in THREAD as read."
7871   (let* ((number (mail-header-number (car thread))))
7872     (incf gnus-newsgroup-expunged-tally)
7873     ;; We also mark as read here, if that's wanted.
7874     (setq gnus-newsgroup-unreads
7875           (delq number gnus-newsgroup-unreads))
7876     (if gnus-newsgroup-auto-expire
7877         (push number gnus-newsgroup-expirable)
7878       (push (cons number gnus-low-score-mark)
7879             gnus-newsgroup-reads)))
7880   ;; Go recursively through all subthreads.
7881   (mapcar 'gnus-expunge-thread (cdr thread)))
7882
7883 ;; Summary article oriented commands
7884
7885 (defun gnus-summary-refer-parent-article (n)
7886   "Refer parent article N times.
7887 If N is negative, go to ancestor -N instead.
7888 The difference between N and the number of articles fetched is returned."
7889   (interactive "p")
7890   (let ((skip 1)
7891         error header ref)
7892     (when (not (natnump n))
7893       (setq skip (abs n)
7894             n 1))
7895     (while (and (> n 0)
7896                 (not error))
7897       (setq header (gnus-summary-article-header))
7898       (if (and (eq (mail-header-number header)
7899                    (cdr gnus-article-current))
7900                (equal gnus-newsgroup-name
7901                       (car gnus-article-current)))
7902           ;; If we try to find the parent of the currently
7903           ;; displayed article, then we take a look at the actual
7904           ;; References header, since this is slightly more
7905           ;; reliable than the References field we got from the
7906           ;; server.
7907           (save-excursion
7908             (set-buffer gnus-original-article-buffer)
7909             (nnheader-narrow-to-headers)
7910             (unless (setq ref (message-fetch-field "references"))
7911               (setq ref (message-fetch-field "in-reply-to")))
7912             (widen))
7913         (setq ref
7914               ;; It's not the current article, so we take a bet on
7915               ;; the value we got from the server.
7916               (mail-header-references header)))
7917       (if (and ref
7918                (not (equal ref "")))
7919           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7920             (gnus-message 1 "Couldn't find parent"))
7921         (gnus-message 1 "No references in article %d"
7922                       (gnus-summary-article-number))
7923         (setq error t))
7924       (decf n))
7925     (gnus-summary-position-point)
7926     n))
7927
7928 (defun gnus-summary-refer-references ()
7929   "Fetch all articles mentioned in the References header.
7930 Return the number of articles fetched."
7931   (interactive)
7932   (let ((ref (mail-header-references (gnus-summary-article-header)))
7933         (current (gnus-summary-article-number))
7934         (n 0))
7935     (if (or (not ref)
7936             (equal ref ""))
7937         (error "No References in the current article")
7938       ;; For each Message-ID in the References header...
7939       (while (string-match "<[^>]*>" ref)
7940         (incf n)
7941         ;; ... fetch that article.
7942         (gnus-summary-refer-article
7943          (prog1 (match-string 0 ref)
7944            (setq ref (substring ref (match-end 0))))))
7945       (gnus-summary-goto-subject current)
7946       (gnus-summary-position-point)
7947       n)))
7948
7949 (defun gnus-summary-refer-thread (&optional limit)
7950   "Fetch all articles in the current thread.
7951 If LIMIT (the numerical prefix), fetch that many old headers instead
7952 of what's specified by the `gnus-refer-thread-limit' variable."
7953   (interactive "P")
7954   (let ((id (mail-header-id (gnus-summary-article-header)))
7955         (limit (if limit (prefix-numeric-value limit)
7956                  gnus-refer-thread-limit)))
7957     (unless (eq gnus-fetch-old-headers 'invisible)
7958       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7959       ;; Retrieve the headers and read them in.
7960       (if (eq (gnus-retrieve-headers
7961                (list (min
7962                       (+ (mail-header-number
7963                           (gnus-summary-article-header))
7964                          limit)
7965                       gnus-newsgroup-end))
7966                gnus-newsgroup-name (* limit 2))
7967               'nov)
7968           (gnus-build-all-threads)
7969         (error "Can't fetch thread from backends that don't support NOV"))
7970       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7971     (gnus-summary-limit-include-thread id)))
7972
7973 (defun gnus-summary-refer-article (message-id)
7974   "Fetch an article specified by MESSAGE-ID."
7975   (interactive "sMessage-ID: ")
7976   (when (and (stringp message-id)
7977              (not (zerop (length message-id))))
7978     ;; Construct the correct Message-ID if necessary.
7979     ;; Suggested by tale@pawl.rpi.edu.
7980     (unless (string-match "^<" message-id)
7981       (setq message-id (concat "<" message-id)))
7982     (unless (string-match ">$" message-id)
7983       (setq message-id (concat message-id ">")))
7984     (let* ((header (gnus-id-to-header message-id))
7985            (sparse (and header
7986                         (gnus-summary-article-sparse-p
7987                          (mail-header-number header))
7988                         (memq (mail-header-number header)
7989                               gnus-newsgroup-limit)))
7990            number)
7991       (cond
7992        ;; If the article is present in the buffer we just go to it.
7993        ((and header
7994              (or (not (gnus-summary-article-sparse-p
7995                        (mail-header-number header)))
7996                  sparse))
7997         (prog1
7998             (gnus-summary-goto-article
7999              (mail-header-number header) nil t)
8000           (when sparse
8001             (gnus-summary-update-article (mail-header-number header)))))
8002        (t
8003         ;; We fetch the article.
8004         (catch 'found
8005           (dolist (gnus-override-method (gnus-refer-article-methods))
8006             (gnus-check-server gnus-override-method)
8007             ;; Fetch the header, and display the article.
8008             (when (setq number (gnus-summary-insert-subject message-id))
8009               (gnus-summary-select-article nil nil nil number)
8010               (throw 'found t)))
8011           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8012
8013 (defun gnus-refer-article-methods ()
8014   "Return a list of referable methods."
8015   (cond
8016    ;; No method, so we default to current and native.
8017    ((null gnus-refer-article-method)
8018     (list gnus-current-select-method gnus-select-method))
8019    ;; Current.
8020    ((eq 'current gnus-refer-article-method)
8021     (list gnus-current-select-method))
8022    ;; List of select methods.
8023    ((not (and (symbolp (car gnus-refer-article-method))
8024               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8025     (let (out)
8026       (dolist (method gnus-refer-article-method)
8027         (push (if (eq 'current method)
8028                   gnus-current-select-method
8029                 method)
8030               out))
8031       (nreverse out)))
8032    ;; One single select method.
8033    (t
8034     (list gnus-refer-article-method))))
8035
8036 (defun gnus-summary-edit-parameters ()
8037   "Edit the group parameters of the current group."
8038   (interactive)
8039   (gnus-group-edit-group gnus-newsgroup-name 'params))
8040
8041 (defun gnus-summary-customize-parameters ()
8042   "Customize the group parameters of the current group."
8043   (interactive)
8044   (gnus-group-customize gnus-newsgroup-name))
8045
8046 (defun gnus-summary-enter-digest-group (&optional force)
8047   "Enter an nndoc group based on the current article.
8048 If FORCE, force a digest interpretation.  If not, try
8049 to guess what the document format is."
8050   (interactive "P")
8051   (let ((conf gnus-current-window-configuration))
8052     (save-excursion
8053       (gnus-summary-select-article))
8054     (setq gnus-current-window-configuration conf)
8055     (let* ((name (format "%s-%d"
8056                          (gnus-group-prefixed-name
8057                           gnus-newsgroup-name (list 'nndoc ""))
8058                          (save-excursion
8059                            (set-buffer gnus-summary-buffer)
8060                            gnus-current-article)))
8061            (ogroup gnus-newsgroup-name)
8062            (params (append (gnus-info-params (gnus-get-info ogroup))
8063                            (list (cons 'to-group ogroup))
8064                            (list (cons 'save-article-group ogroup))))
8065            (case-fold-search t)
8066            (buf (current-buffer))
8067            dig to-address)
8068       (save-excursion
8069         (set-buffer gnus-original-article-buffer)
8070         ;; Have the digest group inherit the main mail address of
8071         ;; the parent article.
8072         (when (setq to-address (or (gnus-fetch-field "reply-to")
8073                                    (gnus-fetch-field "from")))
8074           (setq params (append
8075                         (list (cons 'to-address
8076                                     (funcall gnus-decode-encoded-word-function
8077                                              to-address))))))
8078         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8079         (insert-buffer-substring gnus-original-article-buffer)
8080         ;; Remove lines that may lead nndoc to misinterpret the
8081         ;; document type.
8082         (narrow-to-region
8083          (goto-char (point-min))
8084          (or (search-forward "\n\n" nil t) (point)))
8085         (goto-char (point-min))
8086         (delete-matching-lines "^Path:\\|^From ")
8087         (widen))
8088       (unwind-protect
8089           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8090                     (gnus-newsgroup-ephemeral-ignored-charsets
8091                      gnus-newsgroup-ignored-charsets))
8092                 (gnus-group-read-ephemeral-group
8093                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8094                               (nndoc-article-type
8095                                ,(if force 'mbox 'guess)))
8096                  t nil nil nil
8097                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8098                                                         "ADAPT")))))
8099               ;; Make all postings to this group go to the parent group.
8100               (nconc (gnus-info-params (gnus-get-info name))
8101                      params)
8102             ;; Couldn't select this doc group.
8103             (switch-to-buffer buf)
8104             (gnus-set-global-variables)
8105             (gnus-configure-windows 'summary)
8106             (gnus-message 3 "Article couldn't be entered?"))
8107         (kill-buffer dig)))))
8108
8109 (defun gnus-summary-read-document (n)
8110   "Open a new group based on the current article(s).
8111 This will allow you to read digests and other similar
8112 documents as newsgroups.
8113 Obeys the standard process/prefix convention."
8114   (interactive "P")
8115   (let* ((articles (gnus-summary-work-articles n))
8116          (ogroup gnus-newsgroup-name)
8117          (params (append (gnus-info-params (gnus-get-info ogroup))
8118                          (list (cons 'to-group ogroup))))
8119          article group egroup groups vgroup)
8120     (while (setq article (pop articles))
8121       (setq group (format "%s-%d" gnus-newsgroup-name article))
8122       (gnus-summary-remove-process-mark article)
8123       (when (gnus-summary-display-article article)
8124         (save-excursion
8125           (with-temp-buffer
8126             (insert-buffer-substring gnus-original-article-buffer)
8127             ;; Remove some headers that may lead nndoc to make
8128             ;; the wrong guess.
8129             (message-narrow-to-head)
8130             (goto-char (point-min))
8131             (delete-matching-lines "^\\(Path\\):\\|^From ")
8132             (widen)
8133             (if (setq egroup
8134                       (gnus-group-read-ephemeral-group
8135                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8136                                      (nndoc-article-type guess))
8137                        t nil t))
8138                 (progn
8139             ;; Make all postings to this group go to the parent group.
8140                   (nconc (gnus-info-params (gnus-get-info egroup))
8141                          params)
8142                   (push egroup groups))
8143               ;; Couldn't select this doc group.
8144               (gnus-error 3 "Article couldn't be entered"))))))
8145     ;; Now we have selected all the documents.
8146     (cond
8147      ((not groups)
8148       (error "None of the articles could be interpreted as documents"))
8149      ((gnus-group-read-ephemeral-group
8150        (setq vgroup (format
8151                      "nnvirtual:%s-%s" gnus-newsgroup-name
8152                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8153        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8154        t
8155        (cons (current-buffer) 'summary)))
8156      (t
8157       (error "Couldn't select virtual nndoc group")))))
8158
8159 (defun gnus-summary-isearch-article (&optional regexp-p)
8160   "Do incremental search forward on the current article.
8161 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8162   (interactive "P")
8163   (gnus-summary-select-article)
8164   (gnus-configure-windows 'article)
8165   (gnus-eval-in-buffer-window gnus-article-buffer
8166     (save-restriction
8167       (widen)
8168       (isearch-forward regexp-p))))
8169
8170 (defun gnus-summary-search-article-forward (regexp &optional backward)
8171   "Search for an article containing REGEXP forward.
8172 If BACKWARD, search backward instead."
8173   (interactive
8174    (list (read-string
8175           (format "Search article %s (regexp%s): "
8176                   (if current-prefix-arg "backward" "forward")
8177                   (if gnus-last-search-regexp
8178                       (concat ", default " gnus-last-search-regexp)
8179                     "")))
8180          current-prefix-arg))
8181   (if (string-equal regexp "")
8182       (setq regexp (or gnus-last-search-regexp ""))
8183     (setq gnus-last-search-regexp regexp)
8184     (setq gnus-article-before-search gnus-current-article))
8185   ;; Intentionally set gnus-last-article.
8186   (setq gnus-last-article gnus-article-before-search)
8187   (let ((gnus-last-article gnus-last-article))
8188     (if (gnus-summary-search-article regexp backward)
8189         (gnus-summary-show-thread)
8190       (error "Search failed: \"%s\"" regexp))))
8191
8192 (defun gnus-summary-search-article-backward (regexp)
8193   "Search for an article containing REGEXP backward."
8194   (interactive
8195    (list (read-string
8196           (format "Search article backward (regexp%s): "
8197                   (if gnus-last-search-regexp
8198                       (concat ", default " gnus-last-search-regexp)
8199                     "")))))
8200   (gnus-summary-search-article-forward regexp 'backward))
8201
8202 (defun gnus-summary-search-article (regexp &optional backward)
8203   "Search for an article containing REGEXP.
8204 Optional argument BACKWARD means do search for backward.
8205 `gnus-select-article-hook' is not called during the search."
8206   ;; We have to require this here to make sure that the following
8207   ;; dynamic binding isn't shadowed by autoloading.
8208   (require 'gnus-async)
8209   (require 'gnus-art)
8210   (let ((gnus-select-article-hook nil)  ;Disable hook.
8211         (gnus-article-prepare-hook nil)
8212         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8213         (gnus-use-article-prefetch nil)
8214         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8215         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8216         (gnus-visual nil)
8217         (gnus-keep-backlog nil)
8218         (gnus-break-pages nil)
8219         (gnus-summary-display-arrow nil)
8220         (gnus-updated-mode-lines nil)
8221         (gnus-auto-center-summary nil)
8222         (sum (current-buffer))
8223         (gnus-display-mime-function nil)
8224         (found nil)
8225         point)
8226     (gnus-save-hidden-threads
8227       (gnus-summary-select-article)
8228       (set-buffer gnus-article-buffer)
8229       (goto-char (window-point (get-buffer-window (current-buffer))))
8230       (when backward
8231         (forward-line -1))
8232       (while (not found)
8233         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8234         (if (if backward
8235                 (re-search-backward regexp nil t)
8236               (re-search-forward regexp nil t))
8237             ;; We found the regexp.
8238             (progn
8239               (setq found 'found)
8240               (beginning-of-line)
8241               (set-window-start
8242                (get-buffer-window (current-buffer))
8243                (point))
8244               (forward-line 1)
8245               (set-window-point
8246                (get-buffer-window (current-buffer))
8247                (point))
8248               (set-buffer sum)
8249               (setq point (point)))
8250           ;; We didn't find it, so we go to the next article.
8251           (set-buffer sum)
8252           (setq found 'not)
8253           (while (eq found 'not)
8254             (if (not (if backward (gnus-summary-find-prev)
8255                        (gnus-summary-find-next)))
8256                 ;; No more articles.
8257                 (setq found t)
8258               ;; Select the next article and adjust point.
8259               (unless (gnus-summary-article-sparse-p
8260                        (gnus-summary-article-number))
8261                 (setq found nil)
8262                 (gnus-summary-select-article)
8263                 (set-buffer gnus-article-buffer)
8264                 (widen)
8265                 (goto-char (if backward (point-max) (point-min))))))))
8266       (gnus-message 7 ""))
8267     ;; Return whether we found the regexp.
8268     (when (eq found 'found)
8269       (goto-char point)
8270       (gnus-summary-show-thread)
8271       (gnus-summary-goto-subject gnus-current-article)
8272       (gnus-summary-position-point)
8273       t)))
8274
8275 (defun gnus-find-matching-articles (header regexp)
8276   "Return a list of all articles that match REGEXP on HEADER.
8277 This search includes all articles in the current group that Gnus has
8278 fetched headers for, whether they are displayed or not."
8279   (let ((articles nil)
8280         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8281         (case-fold-search t))
8282     (dolist (header gnus-newsgroup-headers)
8283       (when (string-match regexp (funcall func header))
8284         (push (mail-header-number header) articles)))
8285     (nreverse articles)))
8286
8287 (defun gnus-summary-find-matching (header regexp &optional backward unread
8288                                           not-case-fold not-matching)
8289   "Return a list of all articles that match REGEXP on HEADER.
8290 The search stars on the current article and goes forwards unless
8291 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8292 If UNREAD is non-nil, only unread articles will
8293 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8294 in the comparisons. If NOT-MATCHING, return a list of all articles that
8295 not match REGEXP on HEADER."
8296   (let ((case-fold-search (not not-case-fold))
8297         articles d func)
8298     (if (consp header)
8299         (if (eq (car header) 'extra)
8300             (setq func
8301                   `(lambda (h)
8302                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8303                          "")))
8304           (error "%s is an invalid header" header))
8305       (unless (fboundp (intern (concat "mail-header-" header)))
8306         (error "%s is not a valid header" header))
8307       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8308     (dolist (d (if (eq backward 'all)
8309                    gnus-newsgroup-data
8310                  (gnus-data-find-list
8311                   (gnus-summary-article-number)
8312                   (gnus-data-list backward))))
8313       (when (and (or (not unread)       ; We want all articles...
8314                      (gnus-data-unread-p d)) ; Or just unreads.
8315                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8316                  (if not-matching
8317                      (not (string-match
8318                            regexp
8319                            (funcall func (gnus-data-header d))))
8320                    (string-match regexp
8321                                  (funcall func (gnus-data-header d)))))
8322         (push (gnus-data-number d) articles))) ; Success!
8323     (nreverse articles)))
8324
8325 (defun gnus-summary-execute-command (header regexp command &optional backward)
8326   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8327 If HEADER is an empty string (or nil), the match is done on the entire
8328 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8329   (interactive
8330    (list (let ((completion-ignore-case t))
8331            (completing-read
8332             "Header name: "
8333             (mapcar (lambda (header) (list (format "%s" header)))
8334                     (append
8335                      '("Number" "Subject" "From" "Lines" "Date"
8336                        "Message-ID" "Xref" "References" "Body")
8337                      gnus-extra-headers))
8338             nil 'require-match))
8339          (read-string "Regexp: ")
8340          (read-key-sequence "Command: ")
8341          current-prefix-arg))
8342   (when (equal header "Body")
8343     (setq header ""))
8344   ;; Hidden thread subtrees must be searched as well.
8345   (gnus-summary-show-all-threads)
8346   ;; We don't want to change current point nor window configuration.
8347   (save-excursion
8348     (save-window-excursion
8349       (let (gnus-visual
8350             gnus-treat-strip-trailing-blank-lines
8351             gnus-treat-strip-leading-blank-lines
8352             gnus-treat-strip-multiple-blank-lines
8353             gnus-treat-hide-boring-headers
8354             gnus-treat-fold-newsgroups
8355             gnus-article-prepare-hook)
8356         (gnus-message 6 "Executing %s..." (key-description command))
8357         ;; We'd like to execute COMMAND interactively so as to give arguments.
8358         (gnus-execute header regexp
8359                       `(call-interactively ',(key-binding command))
8360                       backward)
8361         (gnus-message 6 "Executing %s...done" (key-description command))))))
8362
8363 (defun gnus-summary-beginning-of-article ()
8364   "Scroll the article back to the beginning."
8365   (interactive)
8366   (gnus-summary-select-article)
8367   (gnus-configure-windows 'article)
8368   (gnus-eval-in-buffer-window gnus-article-buffer
8369     (widen)
8370     (goto-char (point-min))
8371     (when gnus-page-broken
8372       (gnus-narrow-to-page))))
8373
8374 (defun gnus-summary-end-of-article ()
8375   "Scroll to the end of the article."
8376   (interactive)
8377   (gnus-summary-select-article)
8378   (gnus-configure-windows 'article)
8379   (gnus-eval-in-buffer-window gnus-article-buffer
8380     (widen)
8381     (goto-char (point-max))
8382     (recenter -3)
8383     (when gnus-page-broken
8384       (gnus-narrow-to-page))))
8385
8386 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8387   "Truncate to LEN and quote all \"(\"'s in STRING."
8388   (gnus-replace-in-string (if (and len (> (length string) len))
8389                               (substring string 0 len)
8390                             string)
8391                           "[()]" "\\\\\\&"))
8392
8393 (defun gnus-summary-print-article (&optional filename n)
8394   "Generate and print a PostScript image of the N next (mail) articles.
8395
8396 If N is negative, print the N previous articles.  If N is nil and articles
8397 have been marked with the process mark, print these instead.
8398
8399 If the optional first argument FILENAME is nil, send the image to the
8400 printer.  If FILENAME is a string, save the PostScript image in a file with
8401 that name.  If FILENAME is a number, prompt the user for the name of the file
8402 to save in."
8403   (interactive (list (ps-print-preprint current-prefix-arg)))
8404   (dolist (article (gnus-summary-work-articles n))
8405     (gnus-summary-select-article nil nil 'pseudo article)
8406     (gnus-eval-in-buffer-window gnus-article-buffer
8407       (gnus-print-buffer))
8408     (gnus-summary-remove-process-mark article))
8409   (ps-despool filename))
8410
8411 (defun gnus-print-buffer ()
8412   (let ((buffer (generate-new-buffer " *print*")))
8413     (unwind-protect
8414         (progn
8415           (copy-to-buffer buffer (point-min) (point-max))
8416           (set-buffer buffer)
8417           (gnus-article-delete-invisible-text)
8418           (gnus-remove-text-with-property 'gnus-decoration)
8419           (when (gnus-visual-p 'article-highlight 'highlight)
8420             ;; Copy-to-buffer doesn't copy overlay.  So redo
8421             ;; highlight.
8422             (let ((gnus-article-buffer buffer))
8423               (gnus-article-highlight-citation t)
8424               (gnus-article-highlight-signature)))
8425           (let ((ps-left-header
8426                  (list
8427                   (concat "("
8428                           (gnus-summary-print-truncate-and-quote
8429                            (mail-header-subject gnus-current-headers)
8430                            66) ")")
8431                   (concat "("
8432                           (gnus-summary-print-truncate-and-quote
8433                            (mail-header-from gnus-current-headers)
8434                            45) ")")))
8435                 (ps-right-header
8436                  (list
8437                   "/pagenumberstring load"
8438                   (concat "("
8439                           (mail-header-date gnus-current-headers) ")"))))
8440             (gnus-run-hooks 'gnus-ps-print-hook)
8441             (save-excursion
8442               (if window-system
8443                   (ps-spool-buffer-with-faces)
8444                 (ps-spool-buffer)))))
8445       (kill-buffer buffer))))
8446
8447 (defun gnus-summary-show-article (&optional arg)
8448   "Force redisplaying of the current article.
8449 If ARG (the prefix) is a number, show the article with the charset
8450 defined in `gnus-summary-show-article-charset-alist', or the charset
8451 input.
8452 If ARG (the prefix) is non-nil and not a number, show the raw article
8453 without any article massaging functions being run.  Normally, the key strokes
8454 are `C-u g'."
8455   (interactive "P")
8456   (cond
8457    ((numberp arg)
8458     (gnus-summary-show-article t)
8459     (let ((gnus-newsgroup-charset
8460            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8461                (mm-read-coding-system
8462                 "View as charset: " ;; actually it is coding system.
8463                 (save-excursion
8464                   (set-buffer gnus-article-buffer)
8465                   (mm-detect-coding-region (point) (point-max))))))
8466           (gnus-newsgroup-ignored-charsets 'gnus-all))
8467       (gnus-summary-select-article nil 'force)
8468       (let ((deps gnus-newsgroup-dependencies)
8469             head header lines)
8470         (save-excursion
8471           (set-buffer gnus-original-article-buffer)
8472           (save-restriction
8473             (message-narrow-to-head)
8474             (setq head (buffer-string))
8475             (goto-char (point-min))
8476             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8477               (goto-char (point-max))
8478               (widen)
8479               (setq lines (1- (count-lines (point) (point-max))))))
8480           (with-temp-buffer
8481             (insert (format "211 %d Article retrieved.\n"
8482                             (cdr gnus-article-current)))
8483             (insert head)
8484             (if lines (insert (format "Lines: %d\n" lines)))
8485             (insert ".\n")
8486             (let ((nntp-server-buffer (current-buffer)))
8487               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8488         (gnus-data-set-header
8489          (gnus-data-find (cdr gnus-article-current))
8490          header)
8491         (gnus-summary-update-article-line
8492          (cdr gnus-article-current) header)
8493         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8494           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8495    ((not arg)
8496     ;; Select the article the normal way.
8497     (gnus-summary-select-article nil 'force))
8498    (t
8499     ;; We have to require this here to make sure that the following
8500     ;; dynamic binding isn't shadowed by autoloading.
8501     (require 'gnus-async)
8502     (require 'gnus-art)
8503     ;; Bind the article treatment functions to nil.
8504     (let ((gnus-have-all-headers t)
8505           gnus-article-prepare-hook
8506           gnus-article-decode-hook
8507           gnus-display-mime-function
8508           gnus-break-pages)
8509       ;; Destroy any MIME parts.
8510       (when (gnus-buffer-live-p gnus-article-buffer)
8511         (save-excursion
8512           (set-buffer gnus-article-buffer)
8513           (mm-destroy-parts gnus-article-mime-handles)
8514           ;; Set it to nil for safety reason.
8515           (setq gnus-article-mime-handle-alist nil)
8516           (setq gnus-article-mime-handles nil)))
8517       (gnus-summary-select-article nil 'force))))
8518   (gnus-summary-goto-subject gnus-current-article)
8519   (gnus-summary-position-point))
8520
8521 (defun gnus-summary-show-raw-article ()
8522   "Show the raw article without any article massaging functions being run."
8523   (interactive)
8524   (gnus-summary-show-article t))
8525
8526 (defun gnus-summary-verbose-headers (&optional arg)
8527   "Toggle permanent full header display.
8528 If ARG is a positive number, turn header display on.
8529 If ARG is a negative number, turn header display off."
8530   (interactive "P")
8531   (setq gnus-show-all-headers
8532         (cond ((or (not (numberp arg))
8533                    (zerop arg))
8534                (not gnus-show-all-headers))
8535               ((natnump arg)
8536                t)))
8537   (gnus-summary-show-article))
8538
8539 (defun gnus-summary-toggle-header (&optional arg)
8540   "Show the headers if they are hidden, or hide them if they are shown.
8541 If ARG is a positive number, show the entire header.
8542 If ARG is a negative number, hide the unwanted header lines."
8543   (interactive "P")
8544   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8545                      (get-buffer-window gnus-article-buffer t))))
8546     (with-current-buffer gnus-article-buffer
8547       (widen)
8548       (article-narrow-to-head)
8549       (let* ((buffer-read-only nil)
8550              (inhibit-point-motion-hooks t)
8551              (hidden (if (numberp arg)
8552                          (>= arg 0)
8553                        (gnus-article-hidden-text-p 'headers)))
8554              s e)
8555         (delete-region (point-min) (point-max))
8556         (with-current-buffer gnus-original-article-buffer
8557           (goto-char (setq s (point-min)))
8558           (setq e (if (search-forward "\n\n" nil t)
8559                       (1- (point))
8560                     (point-max))))
8561         (insert-buffer-substring gnus-original-article-buffer s e)
8562         (article-decode-encoded-words)
8563         (if hidden
8564             (let ((gnus-treat-hide-headers nil)
8565                   (gnus-treat-hide-boring-headers nil))
8566               (gnus-delete-wash-type 'headers)
8567               (gnus-treat-article 'head))
8568           (gnus-treat-article 'head))
8569         (widen)
8570         (if window
8571             (set-window-start window (goto-char (point-min))))
8572         (setq gnus-page-broken
8573               (when gnus-break-pages
8574                 (gnus-narrow-to-page)
8575                 t))
8576         (gnus-set-mode-line 'article)))))
8577
8578 (defun gnus-summary-show-all-headers ()
8579   "Make all header lines visible."
8580   (interactive)
8581   (gnus-summary-toggle-header 1))
8582
8583 (defun gnus-summary-caesar-message (&optional arg)
8584   "Caesar rotate the current article by 13.
8585 The numerical prefix specifies how many places to rotate each letter
8586 forward."
8587   (interactive "P")
8588   (gnus-summary-select-article)
8589   (let ((mail-header-separator ""))
8590     (gnus-eval-in-buffer-window gnus-article-buffer
8591       (save-restriction
8592         (widen)
8593         (let ((start (window-start))
8594               buffer-read-only)
8595           (message-caesar-buffer-body arg)
8596           (set-window-start (get-buffer-window (current-buffer)) start))))))
8597
8598 (autoload 'unmorse-region "morse"
8599   "Convert morse coded text in region to ordinary ASCII text."
8600   t)
8601
8602 (defun gnus-summary-morse-message (&optional arg)
8603   "Morse decode the current article."
8604   (interactive "P")
8605   (gnus-summary-select-article)
8606   (let ((mail-header-separator ""))
8607     (gnus-eval-in-buffer-window gnus-article-buffer
8608       (save-excursion
8609         (save-restriction
8610           (widen)
8611           (let ((pos (window-start))
8612                 buffer-read-only)
8613             (goto-char (point-min))
8614             (when (message-goto-body)
8615               (gnus-narrow-to-body))
8616             (goto-char (point-min))
8617             (while (re-search-forward "·" (point-max) t)
8618               (replace-match "."))
8619             (unmorse-region (point-min) (point-max))
8620             (widen)
8621             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8622
8623 (defun gnus-summary-stop-page-breaking ()
8624   "Stop page breaking in the current article."
8625   (interactive)
8626   (gnus-summary-select-article)
8627   (gnus-eval-in-buffer-window gnus-article-buffer
8628     (widen)
8629     (when (gnus-visual-p 'page-marker)
8630       (let ((buffer-read-only nil))
8631         (gnus-remove-text-with-property 'gnus-prev)
8632         (gnus-remove-text-with-property 'gnus-next))
8633       (setq gnus-page-broken nil))))
8634
8635 (defun gnus-summary-move-article (&optional n to-newsgroup
8636                                             select-method action)
8637   "Move the current article to a different newsgroup.
8638 If N is a positive number, move the N next articles.
8639 If N is a negative number, move the N previous articles.
8640 If N is nil and any articles have been marked with the process mark,
8641 move those articles instead.
8642 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8643 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8644 re-spool using this method.
8645
8646 When called interactively with TO-NEWSGROUP being nil, the value of
8647 the variable `gnus-move-split-methods' is used for finding a default
8648 for the target newsgroup.
8649
8650 For this function to work, both the current newsgroup and the
8651 newsgroup that you want to move to have to support the `request-move'
8652 and `request-accept' functions.
8653
8654 ACTION can be either `move' (the default), `crosspost' or `copy'."
8655   (interactive "P")
8656   (unless action
8657     (setq action 'move))
8658   ;; Check whether the source group supports the required functions.
8659   (cond ((and (eq action 'move)
8660               (not (gnus-check-backend-function
8661                     'request-move-article gnus-newsgroup-name)))
8662          (error "The current group does not support article moving"))
8663         ((and (eq action 'crosspost)
8664               (not (gnus-check-backend-function
8665                     'request-replace-article gnus-newsgroup-name)))
8666          (error "The current group does not support article editing")))
8667   (let ((articles (gnus-summary-work-articles n))
8668         (prefix (if (gnus-check-backend-function
8669                      'request-move-article gnus-newsgroup-name)
8670                     (gnus-group-real-prefix gnus-newsgroup-name)
8671                   ""))
8672         (names '((move "Move" "Moving")
8673                  (copy "Copy" "Copying")
8674                  (crosspost "Crosspost" "Crossposting")))
8675         (copy-buf (save-excursion
8676                     (nnheader-set-temp-buffer " *copy article*")))
8677         art-group to-method new-xref article to-groups)
8678     (unless (assq action names)
8679       (error "Unknown action %s" action))
8680     ;; Read the newsgroup name.
8681     (when (and (not to-newsgroup)
8682                (not select-method))
8683       (if (and gnus-move-split-methods
8684                (not
8685                 (and (memq gnus-current-article articles)
8686                      (gnus-buffer-live-p gnus-original-article-buffer))))
8687           ;; When `gnus-move-split-methods' is non-nil, we have to
8688           ;; select an article to give `gnus-read-move-group-name' an
8689           ;; opportunity to suggest an appropriate default.  However,
8690           ;; we needn't render or mark the article.
8691           (let ((gnus-display-mime-function nil)
8692                 (gnus-article-prepare-hook nil)
8693                 (gnus-mark-article-hook nil))
8694             (gnus-summary-select-article nil nil nil (car articles))))
8695       (setq to-newsgroup
8696             (gnus-read-move-group-name
8697              (cadr (assq action names))
8698              (symbol-value (intern (format "gnus-current-%s-group" action)))
8699              articles prefix))
8700       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8701     (setq to-method (or select-method
8702                         (gnus-server-to-method
8703                          (gnus-group-method to-newsgroup))))
8704     ;; Check the method we are to move this article to...
8705     (unless (gnus-check-backend-function
8706              'request-accept-article (car to-method))
8707       (error "%s does not support article copying" (car to-method)))
8708     (unless (gnus-check-server to-method)
8709       (error "Can't open server %s" (car to-method)))
8710     (gnus-message 6 "%s to %s: %s..."
8711                   (caddr (assq action names))
8712                   (or (car select-method) to-newsgroup) articles)
8713     (while articles
8714       (setq article (pop articles))
8715       (setq
8716        art-group
8717        (cond
8718         ;; Move the article.
8719         ((eq action 'move)
8720          ;; Remove this article from future suppression.
8721          (gnus-dup-unsuppress-article article)
8722          (gnus-request-move-article
8723           article                       ; Article to move
8724           gnus-newsgroup-name           ; From newsgroup
8725           (nth 1 (gnus-find-method-for-group
8726                   gnus-newsgroup-name)) ; Server
8727           (list 'gnus-request-accept-article
8728                 to-newsgroup (list 'quote select-method)
8729                 (not articles) t)       ; Accept form
8730           (not articles)))              ; Only save nov last time
8731         ;; Copy the article.
8732         ((eq action 'copy)
8733          (save-excursion
8734            (set-buffer copy-buf)
8735            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8736              (gnus-request-accept-article
8737               to-newsgroup select-method (not articles) t))))
8738         ;; Crosspost the article.
8739         ((eq action 'crosspost)
8740          (let ((xref (message-tokenize-header
8741                       (mail-header-xref (gnus-summary-article-header article))
8742                       " ")))
8743            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8744                                   ":" (number-to-string article)))
8745            (unless xref
8746              (setq xref (list (system-name))))
8747            (setq new-xref
8748                  (concat
8749                   (mapconcat 'identity
8750                              (delete "Xref:" (delete new-xref xref))
8751                              " ")
8752                   " " new-xref))
8753            (save-excursion
8754              (set-buffer copy-buf)
8755              ;; First put the article in the destination group.
8756              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8757              (when (consp (setq art-group
8758                                 (gnus-request-accept-article
8759                                  to-newsgroup select-method (not articles))))
8760                (setq new-xref (concat new-xref " " (car art-group)
8761                                       ":"
8762                                       (number-to-string (cdr art-group))))
8763                ;; Now we have the new Xrefs header, so we insert
8764                ;; it and replace the new article.
8765                (nnheader-replace-header "Xref" new-xref)
8766                (gnus-request-replace-article
8767                 (cdr art-group) to-newsgroup (current-buffer))
8768                art-group))))))
8769       (cond
8770        ((not art-group)
8771         (gnus-message 1 "Couldn't %s article %s: %s"
8772                       (cadr (assq action names)) article
8773                       (nnheader-get-report (car to-method))))
8774        ((eq art-group 'junk)
8775         (when (eq action 'move)
8776           (let ((id (mail-header-id (gnus-data-header 
8777                                      (assoc article (gnus-data-list nil))))))
8778             (gnus-summary-mark-article article gnus-canceled-mark)
8779             (gnus-message 4 "Deleted article %s" article)
8780             ;; run the move/copy/crosspost/respool hook
8781             (run-hook-with-args 'gnus-summary-article-delete-hook 
8782                                 action id gnus-newsgroup-name nil
8783                                 select-method))))
8784        (t
8785         (let* ((pto-group (gnus-group-prefixed-name
8786                            (car art-group) to-method))
8787                (entry
8788                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8789                (info (nth 2 entry))
8790                (to-group (gnus-info-group info))
8791                to-marks)
8792           ;; Update the group that has been moved to.
8793           (when (and info
8794                      (memq action '(move copy)))
8795             (unless (member to-group to-groups)
8796               (push to-group to-groups))
8797
8798             (unless (memq article gnus-newsgroup-unreads)
8799               (push 'read to-marks)
8800               (gnus-info-set-read
8801                info (gnus-add-to-range (gnus-info-read info)
8802                                        (list (cdr art-group)))))
8803
8804             ;; See whether the article is to be put in the cache.
8805             (let ((marks gnus-article-mark-lists)
8806                   (to-article (cdr art-group)))
8807
8808               ;; Enter the article into the cache in the new group,
8809               ;; if that is required.
8810               (when gnus-use-cache
8811                 (gnus-cache-possibly-enter-article
8812                  to-group to-article
8813                  (memq article gnus-newsgroup-marked)
8814                  (memq article gnus-newsgroup-dormant)
8815                  (memq article gnus-newsgroup-unreads)))
8816
8817               (when gnus-preserve-marks
8818                 ;; Copy any marks over to the new group.
8819                 (when (and (equal to-group gnus-newsgroup-name)
8820                            (not (memq article gnus-newsgroup-unreads)))
8821                   ;; Mark this article as read in this group.
8822                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8823                   (setcdr (gnus-active to-group) to-article)
8824                   (setcdr gnus-newsgroup-active to-article))
8825
8826                 (while marks
8827                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8828                     (when (memq article (symbol-value
8829                                          (intern (format "gnus-newsgroup-%s"
8830                                                          (caar marks)))))
8831                       (push (cdar marks) to-marks)
8832                       ;; If the other group is the same as this group,
8833                       ;; then we have to add the mark to the list.
8834                       (when (equal to-group gnus-newsgroup-name)
8835                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8836                              (cons to-article
8837                                    (symbol-value
8838                                     (intern (format "gnus-newsgroup-%s"
8839                                                     (caar marks)))))))
8840                       ;; Copy the marks to other group.
8841                       (gnus-add-marked-articles
8842                        to-group (cdar marks) (list to-article) info)))
8843                   (setq marks (cdr marks)))
8844
8845                 (gnus-request-set-mark
8846                  to-group (list (list (list to-article) 'add to-marks))))
8847
8848               (gnus-dribble-enter
8849                (concat "(gnus-group-set-info '"
8850                        (gnus-prin1-to-string (gnus-get-info to-group))
8851                        ")"))))
8852
8853           ;; Update the Xref header in this article to point to
8854           ;; the new crossposted article we have just created.
8855           (when (eq action 'crosspost)
8856             (save-excursion
8857               (set-buffer copy-buf)
8858               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8859               (nnheader-replace-header "Xref" new-xref)
8860               (gnus-request-replace-article
8861                article gnus-newsgroup-name (current-buffer))))
8862
8863           ;; run the move/copy/crosspost/respool hook
8864           (let ((id (mail-header-id (gnus-data-header 
8865                                    (assoc article (gnus-data-list nil))))))
8866           (run-hook-with-args 'gnus-summary-article-move-hook 
8867                               action id gnus-newsgroup-name to-newsgroup
8868                               select-method)))
8869
8870         ;;;!!!Why is this necessary?
8871         (set-buffer gnus-summary-buffer)
8872
8873         (gnus-summary-goto-subject article)
8874         (when (eq action 'move)
8875           (gnus-summary-mark-article article gnus-canceled-mark))))
8876       (gnus-summary-remove-process-mark article))
8877     ;; Re-activate all groups that have been moved to.
8878     (save-excursion
8879       (set-buffer gnus-group-buffer)
8880       (let ((gnus-group-marked to-groups))
8881         (gnus-group-get-new-news-this-group nil t)))
8882
8883     (gnus-kill-buffer copy-buf)
8884     (gnus-summary-position-point)
8885     (gnus-set-mode-line 'summary)))
8886
8887 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8888   "Move the current article to a different newsgroup.
8889 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8890 When called interactively, if TO-NEWSGROUP is nil, use the value of
8891 the variable `gnus-move-split-methods' for finding a default target
8892 newsgroup.
8893 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8894 re-spool using this method."
8895   (interactive "P")
8896   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8897
8898 (defun gnus-summary-crosspost-article (&optional n)
8899   "Crosspost the current article to some other group."
8900   (interactive "P")
8901   (gnus-summary-move-article n nil nil 'crosspost))
8902
8903 (defcustom gnus-summary-respool-default-method nil
8904   "Default method type for respooling an article.
8905 If nil, use to the current newsgroup method."
8906   :type 'symbol
8907   :group 'gnus-summary-mail)
8908
8909 (defcustom gnus-summary-display-while-building nil
8910   "If not-nil, show and update the summary buffer as it's being built.
8911 If the value is t, update the buffer after every line is inserted.  If
8912 the value is an integer (N), update the display every N lines."
8913   :group 'gnus-thread
8914   :type '(choice (const :tag "off" nil)
8915                  number
8916                  (const :tag "frequently" t)))
8917
8918 (defun gnus-summary-respool-article (&optional n method)
8919   "Respool the current article.
8920 The article will be squeezed through the mail spooling process again,
8921 which means that it will be put in some mail newsgroup or other
8922 depending on `nnmail-split-methods'.
8923 If N is a positive number, respool the N next articles.
8924 If N is a negative number, respool the N previous articles.
8925 If N is nil and any articles have been marked with the process mark,
8926 respool those articles instead.
8927
8928 Respooling can be done both from mail groups and \"real\" newsgroups.
8929 In the former case, the articles in question will be moved from the
8930 current group into whatever groups they are destined to.  In the
8931 latter case, they will be copied into the relevant groups."
8932   (interactive
8933    (list current-prefix-arg
8934          (let* ((methods (gnus-methods-using 'respool))
8935                 (methname
8936                  (symbol-name (or gnus-summary-respool-default-method
8937                                   (car (gnus-find-method-for-group
8938                                         gnus-newsgroup-name)))))
8939                 (method
8940                  (gnus-completing-read-with-default
8941                   methname "What backend do you want to use when respooling?"
8942                   methods nil t nil 'gnus-mail-method-history))
8943                 ms)
8944            (cond
8945             ((zerop (length (setq ms (gnus-servers-using-backend
8946                                       (intern method)))))
8947              (list (intern method) ""))
8948             ((= 1 (length ms))
8949              (car ms))
8950             (t
8951              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8952                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8953                            ms-alist))))))))
8954   (unless method
8955     (error "No method given for respooling"))
8956   (if (assoc (symbol-name
8957               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8958              (gnus-methods-using 'respool))
8959       (gnus-summary-move-article n nil method)
8960     (gnus-summary-copy-article n nil method)))
8961
8962 (defun gnus-summary-import-article (file &optional edit)
8963   "Import an arbitrary file into a mail newsgroup."
8964   (interactive "fImport file: \nP")
8965   (let ((group gnus-newsgroup-name)
8966         (now (current-time))
8967         atts lines group-art)
8968     (unless (gnus-check-backend-function 'request-accept-article group)
8969       (error "%s does not support article importing" group))
8970     (or (file-readable-p file)
8971         (not (file-regular-p file))
8972         (error "Can't read %s" file))
8973     (save-excursion
8974       (set-buffer (gnus-get-buffer-create " *import file*"))
8975       (erase-buffer)
8976       (nnheader-insert-file-contents file)
8977       (goto-char (point-min))
8978       (if (nnheader-article-p)
8979           (save-restriction
8980             (goto-char (point-min))
8981             (search-forward "\n\n" nil t)
8982             (narrow-to-region (point-min) (1- (point)))
8983             (goto-char (point-min))
8984             (unless (re-search-forward "^date:" nil t)
8985               (goto-char (point-max))
8986               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8987        ;; This doesn't look like an article, so we fudge some headers.
8988         (setq atts (file-attributes file)
8989               lines (count-lines (point-min) (point-max)))
8990         (insert "From: " (read-string "From: ") "\n"
8991                 "Subject: " (read-string "Subject: ") "\n"
8992                 "Date: " (message-make-date (nth 5 atts)) "\n"
8993                 "Message-ID: " (message-make-message-id) "\n"
8994                 "Lines: " (int-to-string lines) "\n"
8995                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8996       (setq group-art (gnus-request-accept-article group nil t))
8997       (kill-buffer (current-buffer)))
8998     (setq gnus-newsgroup-active (gnus-activate-group group))
8999     (forward-line 1)
9000     (gnus-summary-goto-article (cdr group-art) nil t)
9001     (when edit
9002       (gnus-summary-edit-article))))
9003
9004 (defun gnus-summary-create-article ()
9005   "Create an article in a mail newsgroup."
9006   (interactive)
9007   (let ((group gnus-newsgroup-name)
9008         (now (current-time))
9009         group-art)
9010     (unless (gnus-check-backend-function 'request-accept-article group)
9011       (error "%s does not support article importing" group))
9012     (save-excursion
9013       (set-buffer (gnus-get-buffer-create " *import file*"))
9014       (erase-buffer)
9015       (goto-char (point-min))
9016       ;; This doesn't look like an article, so we fudge some headers.
9017       (insert "From: " (read-string "From: ") "\n"
9018               "Subject: " (read-string "Subject: ") "\n"
9019               "Date: " (message-make-date now) "\n"
9020               "Message-ID: " (message-make-message-id) "\n")
9021       (setq group-art (gnus-request-accept-article group nil t))
9022       (kill-buffer (current-buffer)))
9023     (setq gnus-newsgroup-active (gnus-activate-group group))
9024     (forward-line 1)
9025     (gnus-summary-goto-article (cdr group-art) nil t)
9026     (gnus-summary-edit-article)))
9027
9028 (defun gnus-summary-article-posted-p ()
9029   "Say whether the current (mail) article is available from news as well.
9030 This will be the case if the article has both been mailed and posted."
9031   (interactive)
9032   (let ((id (mail-header-references (gnus-summary-article-header)))
9033         (gnus-override-method (car (gnus-refer-article-methods))))
9034     (if (gnus-request-head id "")
9035         (gnus-message 2 "The current message was found on %s"
9036                       gnus-override-method)
9037       (gnus-message 2 "The current message couldn't be found on %s"
9038                     gnus-override-method)
9039       nil)))
9040
9041 (defun gnus-summary-expire-articles (&optional now)
9042   "Expire all articles that are marked as expirable in the current group."
9043   (interactive)
9044   (when (gnus-check-backend-function
9045          'request-expire-articles gnus-newsgroup-name)
9046     ;; This backend supports expiry.
9047     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9048            (expirable (if total
9049                           (progn
9050                             ;; We need to update the info for
9051                             ;; this group for `gnus-list-of-read-articles'
9052                             ;; to give us the right answer.
9053                             (gnus-run-hooks 'gnus-exit-group-hook)
9054                             (gnus-summary-update-info)
9055                             (gnus-list-of-read-articles gnus-newsgroup-name))
9056                         (setq gnus-newsgroup-expirable
9057                               (sort gnus-newsgroup-expirable '<))))
9058            (expiry-wait (if now 'immediate
9059                           (gnus-group-find-parameter
9060                            gnus-newsgroup-name 'expiry-wait)))
9061            (nnmail-expiry-target
9062             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9063                 nnmail-expiry-target))
9064            es)
9065       (when expirable
9066         ;; There are expirable articles in this group, so we run them
9067         ;; through the expiry process.
9068         (gnus-message 6 "Expiring articles...")
9069         (unless (gnus-check-group gnus-newsgroup-name)
9070           (error "Can't open server for %s" gnus-newsgroup-name))
9071         ;; The list of articles that weren't expired is returned.
9072         (save-excursion
9073           (if expiry-wait
9074               (let ((nnmail-expiry-wait-function nil)
9075                     (nnmail-expiry-wait expiry-wait))
9076                 (setq es (gnus-request-expire-articles
9077                           expirable gnus-newsgroup-name)))
9078             (setq es (gnus-request-expire-articles
9079                       expirable gnus-newsgroup-name)))
9080           (unless total
9081             (setq gnus-newsgroup-expirable es))
9082           ;; We go through the old list of expirable, and mark all
9083           ;; really expired articles as nonexistent.
9084           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9085             (let ((gnus-use-cache nil))
9086               (dolist (article expirable)
9087                 (when (and (not (memq article es))
9088                            (gnus-data-find article))
9089                   (gnus-summary-mark-article article gnus-canceled-mark)
9090                   (let ((id (mail-header-id (gnus-data-header 
9091                                              (assoc article 
9092                                                     (gnus-data-list nil))))))
9093                     (run-hook-with-args 'gnus-summary-article-expire-hook
9094                                         'delete id gnus-newsgroup-name nil
9095                                         nil)))))))
9096         (gnus-message 6 "Expiring articles...done")))))
9097
9098 (defun gnus-summary-expire-articles-now ()
9099   "Expunge all expirable articles in the current group.
9100 This means that *all* articles that are marked as expirable will be
9101 deleted forever, right now."
9102   (interactive)
9103   (or gnus-expert-user
9104       (gnus-yes-or-no-p
9105        "Are you really, really, really sure you want to delete all these messages? ")
9106       (error "Phew!"))
9107   (gnus-summary-expire-articles t))
9108
9109 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9110 (defun gnus-summary-delete-article (&optional n)
9111   "Delete the N next (mail) articles.
9112 This command actually deletes articles.  This is not a marking
9113 command.  The article will disappear forever from your life, never to
9114 return.
9115 If N is negative, delete backwards.
9116 If N is nil and articles have been marked with the process mark,
9117 delete these instead."
9118   (interactive "P")
9119   (unless (gnus-check-backend-function 'request-expire-articles
9120                                        gnus-newsgroup-name)
9121     (error "The current newsgroup does not support article deletion"))
9122   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9123     (error "Couldn't open server"))
9124   ;; Compute the list of articles to delete.
9125   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9126         (nnmail-expiry-target 'delete)
9127         not-deleted)
9128     (if (and gnus-novice-user
9129              (not (gnus-yes-or-no-p
9130                    (format "Do you really want to delete %s forever? "
9131                            (if (> (length articles) 1)
9132                                (format "these %s articles" (length articles))
9133                              "this article")))))
9134         ()
9135       ;; Delete the articles.
9136       (setq not-deleted (gnus-request-expire-articles
9137                          articles gnus-newsgroup-name 'force))
9138       (while articles
9139         (gnus-summary-remove-process-mark (car articles))
9140         ;; The backend might not have been able to delete the article
9141         ;; after all.
9142         (unless (memq (car articles) not-deleted)
9143           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9144         (let* ((article (car articles))
9145                (id (mail-header-id (gnus-data-header 
9146                                     (assoc article (gnus-data-list nil))))))
9147           (run-hook-with-args 'gnus-summary-article-delete-hook
9148                               'delete id gnus-newsgroup-name nil
9149                               nil))
9150         (setq articles (cdr articles)))
9151       (when not-deleted
9152         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9153     (gnus-summary-position-point)
9154     (gnus-set-mode-line 'summary)
9155     not-deleted))
9156
9157 (defun gnus-summary-edit-article (&optional arg)
9158   "Edit the current article.
9159 This will have permanent effect only in mail groups.
9160 If ARG is nil, edit the decoded articles.
9161 If ARG is 1, edit the raw articles.
9162 If ARG is 2, edit the raw articles even in read-only groups.
9163 If ARG is 3, edit the articles with the current handles.
9164 Otherwise, allow editing of articles even in read-only
9165 groups."
9166   (interactive "P")
9167   (let (force raw current-handles)
9168     (cond
9169      ((null arg))
9170      ((eq arg 1)
9171       (setq raw t))
9172      ((eq arg 2)
9173       (setq raw t
9174             force t))
9175      ((eq arg 3)
9176       (setq current-handles
9177             (and (gnus-buffer-live-p gnus-article-buffer)
9178                  (with-current-buffer gnus-article-buffer
9179                    (prog1
9180                        gnus-article-mime-handles
9181                      (setq gnus-article-mime-handles nil))))))
9182      (t
9183       (setq force t)))
9184     (when (and raw (not force)
9185                (member gnus-newsgroup-name '("nndraft:delayed"
9186                                              "nndraft:drafts"
9187                                              "nndraft:queue")))
9188       (error "Can't edit the raw article in group %s"
9189              gnus-newsgroup-name))
9190     (save-excursion
9191       (set-buffer gnus-summary-buffer)
9192       (let ((mail-parse-charset gnus-newsgroup-charset)
9193             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9194         (gnus-set-global-variables)
9195         (when (and (not force)
9196                    (gnus-group-read-only-p))
9197           (error "The current newsgroup does not support article editing"))
9198         (gnus-summary-show-article t)
9199         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9200           (with-current-buffer gnus-article-buffer
9201             (mm-enable-multibyte)))
9202         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9203             (setq raw t))
9204         (gnus-article-edit-article
9205          (if raw 'ignore
9206            `(lambda ()
9207               (let ((mbl mml-buffer-list))
9208                 (setq mml-buffer-list nil)
9209                 (mime-to-mml ,'current-handles)
9210                 (let ((mbl1 mml-buffer-list))
9211                   (setq mml-buffer-list mbl)
9212                   (set (make-local-variable 'mml-buffer-list) mbl1))
9213                 (make-local-hook 'kill-buffer-hook)
9214                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9215          `(lambda (no-highlight)
9216             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9217                   (message-options message-options)
9218                   (message-options-set-recipient)
9219                   (mail-parse-ignored-charsets
9220                    ',gnus-newsgroup-ignored-charsets))
9221               ,(if (not raw) '(progn
9222                                 (mml-to-mime)
9223                                 (mml-destroy-buffers)
9224                                 (remove-hook 'kill-buffer-hook
9225                                              'mml-destroy-buffers t)
9226                                 (kill-local-variable 'mml-buffer-list)))
9227               (gnus-summary-edit-article-done
9228                ,(or (mail-header-references gnus-current-headers) "")
9229                ,(gnus-group-read-only-p)
9230                ,gnus-summary-buffer no-highlight))))))))
9231
9232 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9233
9234 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9235                                                  no-highlight)
9236   "Make edits to the current article permanent."
9237   (interactive)
9238   (save-excursion
9239    ;; The buffer restriction contains the entire article if it exists.
9240     (when (article-goto-body)
9241       (let ((lines (count-lines (point) (point-max)))
9242             (length (- (point-max) (point)))
9243             (case-fold-search t)
9244             (body (copy-marker (point))))
9245         (goto-char (point-min))
9246         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9247           (delete-region (match-beginning 1) (match-end 1))
9248           (insert (number-to-string length)))
9249         (goto-char (point-min))
9250         (when (re-search-forward
9251                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9252           (delete-region (match-beginning 1) (match-end 1))
9253           (insert (number-to-string length)))
9254         (goto-char (point-min))
9255         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9256           (delete-region (match-beginning 1) (match-end 1))
9257           (insert (number-to-string lines))))))
9258   ;; Replace the article.
9259   (let ((buf (current-buffer)))
9260     (with-temp-buffer
9261       (insert-buffer-substring buf)
9262
9263       (if (and (not read-only)
9264                (not (gnus-request-replace-article
9265                      (cdr gnus-article-current) (car gnus-article-current)
9266                      (current-buffer) t)))
9267           (error "Couldn't replace article")
9268         ;; Update the summary buffer.
9269         (if (and references
9270                  (equal (message-tokenize-header references " ")
9271                         (message-tokenize-header
9272                          (or (message-fetch-field "references") "") " ")))
9273             ;; We only have to update this line.
9274             (save-excursion
9275               (save-restriction
9276                 (message-narrow-to-head)
9277                 (let ((head (buffer-string))
9278                       header)
9279                   (with-temp-buffer
9280                     (insert (format "211 %d Article retrieved.\n"
9281                                     (cdr gnus-article-current)))
9282                     (insert head)
9283                     (insert ".\n")
9284                     (let ((nntp-server-buffer (current-buffer)))
9285                       (setq header (car (gnus-get-newsgroup-headers
9286                                          nil t))))
9287                     (save-excursion
9288                       (set-buffer gnus-summary-buffer)
9289                       (gnus-data-set-header
9290                        (gnus-data-find (cdr gnus-article-current))
9291                        header)
9292                       (gnus-summary-update-article-line
9293                        (cdr gnus-article-current) header)
9294                       (if (gnus-summary-goto-subject
9295                            (cdr gnus-article-current) nil t)
9296                           (gnus-summary-update-secondary-mark
9297                            (cdr gnus-article-current))))))))
9298           ;; Update threads.
9299           (set-buffer (or buffer gnus-summary-buffer))
9300           (gnus-summary-update-article (cdr gnus-article-current))
9301           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9302               (gnus-summary-update-secondary-mark
9303                (cdr gnus-article-current))))
9304         ;; Prettify the article buffer again.
9305         (unless no-highlight
9306           (save-excursion
9307             (set-buffer gnus-article-buffer)
9308             ;;;!!! Fix this -- article should be rehighlighted.
9309             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9310             (set-buffer gnus-original-article-buffer)
9311             (gnus-request-article
9312              (cdr gnus-article-current)
9313              (car gnus-article-current) (current-buffer))))
9314         ;; Prettify the summary buffer line.
9315         (when (gnus-visual-p 'summary-highlight 'highlight)
9316           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9317
9318 (defun gnus-summary-edit-wash (key)
9319   "Perform editing command KEY in the article buffer."
9320   (interactive
9321    (list
9322     (progn
9323       (message "%s" (concat (this-command-keys) "- "))
9324       (read-char))))
9325   (message "")
9326   (gnus-summary-edit-article)
9327   (execute-kbd-macro (concat (this-command-keys) key))
9328   (gnus-article-edit-done))
9329
9330 ;;; Respooling
9331
9332 (defun gnus-summary-respool-query (&optional silent trace)
9333   "Query where the respool algorithm would put this article."
9334   (interactive)
9335   (let (gnus-mark-article-hook)
9336     (gnus-summary-select-article)
9337     (save-excursion
9338       (set-buffer gnus-original-article-buffer)
9339       (save-restriction
9340         (message-narrow-to-head)
9341         (let ((groups (nnmail-article-group 'identity trace)))
9342           (unless silent
9343             (if groups
9344                 (message "This message would go to %s"
9345                          (mapconcat 'car groups ", "))
9346               (message "This message would go to no groups"))
9347             groups))))))
9348
9349 (defun gnus-summary-respool-trace ()
9350   "Trace where the respool algorithm would put this article.
9351 Display a buffer showing all fancy splitting patterns which matched."
9352   (interactive)
9353   (gnus-summary-respool-query nil t))
9354
9355 ;; Summary marking commands.
9356
9357 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9358   "Mark articles which has the same subject as read, and then select the next.
9359 If UNMARK is positive, remove any kind of mark.
9360 If UNMARK is negative, tick articles."
9361   (interactive "P")
9362   (when unmark
9363     (setq unmark (prefix-numeric-value unmark)))
9364   (let ((count
9365          (gnus-summary-mark-same-subject
9366           (gnus-summary-article-subject) unmark)))
9367     ;; Select next unread article.  If auto-select-same mode, should
9368     ;; select the first unread article.
9369     (gnus-summary-next-article t (and gnus-auto-select-same
9370                                       (gnus-summary-article-subject)))
9371     (gnus-message 7 "%d article%s marked as %s"
9372                   count (if (= count 1) " is" "s are")
9373                   (if unmark "unread" "read"))))
9374
9375 (defun gnus-summary-kill-same-subject (&optional unmark)
9376   "Mark articles which has the same subject as read.
9377 If UNMARK is positive, remove any kind of mark.
9378 If UNMARK is negative, tick articles."
9379   (interactive "P")
9380   (when unmark
9381     (setq unmark (prefix-numeric-value unmark)))
9382   (let ((count
9383          (gnus-summary-mark-same-subject
9384           (gnus-summary-article-subject) unmark)))
9385     ;; If marked as read, go to next unread subject.
9386     (when (null unmark)
9387       ;; Go to next unread subject.
9388       (gnus-summary-next-subject 1 t))
9389     (gnus-message 7 "%d articles are marked as %s"
9390                   count (if unmark "unread" "read"))))
9391
9392 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9393   "Mark articles with same SUBJECT as read, and return marked number.
9394 If optional argument UNMARK is positive, remove any kinds of marks.
9395 If optional argument UNMARK is negative, mark articles as unread instead."
9396   (let ((count 1))
9397     (save-excursion
9398       (cond
9399        ((null unmark)                   ; Mark as read.
9400         (while (and
9401                 (progn
9402                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9403                   (gnus-summary-show-thread) t)
9404                 (gnus-summary-find-subject subject))
9405           (setq count (1+ count))))
9406        ((> unmark 0)                    ; Tick.
9407         (while (and
9408                 (progn
9409                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9410                   (gnus-summary-show-thread) t)
9411                 (gnus-summary-find-subject subject))
9412           (setq count (1+ count))))
9413        (t                               ; Mark as unread.
9414         (while (and
9415                 (progn
9416                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9417                   (gnus-summary-show-thread) t)
9418                 (gnus-summary-find-subject subject))
9419           (setq count (1+ count)))))
9420       (gnus-set-mode-line 'summary)
9421       ;; Return the number of marked articles.
9422       count)))
9423
9424 (defun gnus-summary-mark-as-processable (n &optional unmark)
9425   "Set the process mark on the next N articles.
9426 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9427 the process mark instead.  The difference between N and the actual
9428 number of articles marked is returned."
9429   (interactive "P")
9430   (if (and (null n) (gnus-region-active-p))
9431       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9432     (setq n (prefix-numeric-value n))
9433     (let ((backward (< n 0))
9434           (n (abs n)))
9435       (while (and
9436               (> n 0)
9437               (if unmark
9438                   (gnus-summary-remove-process-mark
9439                    (gnus-summary-article-number))
9440                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9441               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9442         (setq n (1- n)))
9443       (when (/= 0 n)
9444         (gnus-message 7 "No more articles"))
9445       (gnus-summary-recenter)
9446       (gnus-summary-position-point)
9447       n)))
9448
9449 (defun gnus-summary-unmark-as-processable (n)
9450   "Remove the process mark from the next N articles.
9451 If N is negative, unmark backward instead.  The difference between N and
9452 the actual number of articles unmarked is returned."
9453   (interactive "P")
9454   (gnus-summary-mark-as-processable n t))
9455
9456 (defun gnus-summary-unmark-all-processable ()
9457   "Remove the process mark from all articles."
9458   (interactive)
9459   (save-excursion
9460     (while gnus-newsgroup-processable
9461       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9462   (gnus-summary-position-point))
9463
9464 (defun gnus-summary-add-mark (article type)
9465   "Mark ARTICLE with a mark of TYPE."
9466   (let ((vtype (car (assq type gnus-article-mark-lists)))
9467         var)
9468     (if (not vtype)
9469         (error "No such mark type: %s" type)
9470       (setq var (intern (format "gnus-newsgroup-%s" type)))
9471       (set var (cons article (symbol-value var)))
9472       (if (memq type '(processable cached replied forwarded recent saved))
9473           (gnus-summary-update-secondary-mark article)
9474         ;;; !!! This is bogus.  We should find out what primary
9475         ;;; !!! mark we want to set.
9476         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9477
9478 (defun gnus-summary-mark-as-expirable (n)
9479   "Mark N articles forward as expirable.
9480 If N is negative, mark backward instead.  The difference between N and
9481 the actual number of articles marked is returned."
9482   (interactive "p")
9483   (gnus-summary-mark-forward n gnus-expirable-mark))
9484
9485 (defun gnus-summary-mark-as-spam (n)
9486   "Mark N articles forward as spam.
9487 If N is negative, mark backward instead.  The difference between N and
9488 the actual number of articles marked is returned."
9489   (interactive "p")
9490   (gnus-summary-mark-forward n gnus-spam-mark))
9491
9492 (defun gnus-summary-mark-article-as-replied (article)
9493   "Mark ARTICLE as replied to and update the summary line.
9494 ARTICLE can also be a list of articles."
9495   (interactive (list (gnus-summary-article-number)))
9496   (let ((articles (if (listp article) article (list article))))
9497     (dolist (article articles)
9498       (unless (numberp article)
9499         (error "%s is not a number" article))
9500       (push article gnus-newsgroup-replied)
9501       (let ((buffer-read-only nil))
9502         (when (gnus-summary-goto-subject article nil t)
9503           (gnus-summary-update-secondary-mark article))))))
9504
9505 (defun gnus-summary-mark-article-as-forwarded (article)
9506   "Mark ARTICLE as forwarded and update the summary line.
9507 ARTICLE can also be a list of articles."
9508   (let ((articles (if (listp article) article (list article))))
9509     (dolist (article articles)
9510       (push article gnus-newsgroup-forwarded)
9511       (let ((buffer-read-only nil))
9512         (when (gnus-summary-goto-subject article nil t)
9513           (gnus-summary-update-secondary-mark article))))))
9514
9515 (defun gnus-summary-set-bookmark (article)
9516   "Set a bookmark in current article."
9517   (interactive (list (gnus-summary-article-number)))
9518   (when (or (not (get-buffer gnus-article-buffer))
9519             (not gnus-current-article)
9520             (not gnus-article-current)
9521             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9522     (error "No current article selected"))
9523   ;; Remove old bookmark, if one exists.
9524   (gnus-pull article gnus-newsgroup-bookmarks)
9525   ;; Set the new bookmark, which is on the form
9526   ;; (article-number . line-number-in-body).
9527   (push
9528    (cons article
9529          (save-excursion
9530            (set-buffer gnus-article-buffer)
9531            (count-lines
9532             (min (point)
9533                  (save-excursion
9534                    (article-goto-body)
9535                    (point)))
9536             (point))))
9537    gnus-newsgroup-bookmarks)
9538   (gnus-message 6 "A bookmark has been added to the current article."))
9539
9540 (defun gnus-summary-remove-bookmark (article)
9541   "Remove the bookmark from the current article."
9542   (interactive (list (gnus-summary-article-number)))
9543   ;; Remove old bookmark, if one exists.
9544   (if (not (assq article gnus-newsgroup-bookmarks))
9545       (gnus-message 6 "No bookmark in current article.")
9546     (gnus-pull article gnus-newsgroup-bookmarks)
9547     (gnus-message 6 "Removed bookmark.")))
9548
9549 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9550 (defun gnus-summary-mark-as-dormant (n)
9551   "Mark N articles forward as dormant.
9552 If N is negative, mark backward instead.  The difference between N and
9553 the actual number of articles marked is returned."
9554   (interactive "p")
9555   (gnus-summary-mark-forward n gnus-dormant-mark))
9556
9557 (defun gnus-summary-set-process-mark (article)
9558   "Set the process mark on ARTICLE and update the summary line."
9559   (setq gnus-newsgroup-processable
9560         (cons article
9561               (delq article gnus-newsgroup-processable)))
9562   (when (gnus-summary-goto-subject article)
9563     (gnus-summary-show-thread)
9564     (gnus-summary-goto-subject article)
9565     (gnus-summary-update-secondary-mark article)))
9566
9567 (defun gnus-summary-remove-process-mark (article)
9568   "Remove the process mark from ARTICLE and update the summary line."
9569   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9570   (when (gnus-summary-goto-subject article)
9571     (gnus-summary-show-thread)
9572     (gnus-summary-goto-subject article)
9573     (gnus-summary-update-secondary-mark article)))
9574
9575 (defun gnus-summary-set-saved-mark (article)
9576   "Set the process mark on ARTICLE and update the summary line."
9577   (push article gnus-newsgroup-saved)
9578   (when (gnus-summary-goto-subject article)
9579     (gnus-summary-update-secondary-mark article)))
9580
9581 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9582   "Mark N articles as read forwards.
9583 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9584 The difference between N and the actual number of articles marked is
9585 returned.
9586 If NO-EXPIRE, auto-expiry will be inhibited."
9587   (interactive "p")
9588   (gnus-summary-show-thread)
9589   (let ((backward (< n 0))
9590         (gnus-summary-goto-unread
9591          (and gnus-summary-goto-unread
9592               (not (eq gnus-summary-goto-unread 'never))
9593               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9594                                     gnus-ticked-mark gnus-dormant-mark)))))
9595         (n (abs n))
9596         (mark (or mark gnus-del-mark)))
9597     (while (and (> n 0)
9598                 (gnus-summary-mark-article nil mark no-expire)
9599                 (zerop (gnus-summary-next-subject
9600                         (if backward -1 1)
9601                         (and gnus-summary-goto-unread
9602                              (not (eq gnus-summary-goto-unread 'never)))
9603                         t)))
9604       (setq n (1- n)))
9605     (when (/= 0 n)
9606       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9607     (gnus-summary-recenter)
9608     (gnus-summary-position-point)
9609     (gnus-set-mode-line 'summary)
9610     n))
9611
9612 (defun gnus-summary-mark-article-as-read (mark)
9613   "Mark the current article quickly as read with MARK."
9614   (let ((article (gnus-summary-article-number)))
9615     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9616     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9617     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9618     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9619     (push (cons article mark) gnus-newsgroup-reads)
9620     ;; Possibly remove from cache, if that is used.
9621     (when gnus-use-cache
9622       (gnus-cache-enter-remove-article article))
9623     ;; Allow the backend to change the mark.
9624     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9625     ;; Check for auto-expiry.
9626     (when (and gnus-newsgroup-auto-expire
9627                (memq mark gnus-auto-expirable-marks))
9628       (setq mark gnus-expirable-mark)
9629       ;; Let the backend know about the mark change.
9630       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9631       (push article gnus-newsgroup-expirable))
9632     ;; Set the mark in the buffer.
9633     (gnus-summary-update-mark mark 'unread)
9634     t))
9635
9636 (defun gnus-summary-mark-article-as-unread (mark)
9637   "Mark the current article quickly as unread with MARK."
9638   (let* ((article (gnus-summary-article-number))
9639          (old-mark (gnus-summary-article-mark article)))
9640     ;; Allow the backend to change the mark.
9641     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9642     (if (eq mark old-mark)
9643         t
9644       (if (<= article 0)
9645           (progn
9646             (gnus-error 1 "Can't mark negative article numbers")
9647             nil)
9648         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9649         (setq gnus-newsgroup-spam-marked
9650               (delq article gnus-newsgroup-spam-marked))
9651         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9652         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9653         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9654         (cond ((= mark gnus-ticked-mark)
9655                (setq gnus-newsgroup-marked
9656                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9657                                               article)))
9658               ((= mark gnus-spam-mark)
9659                (setq gnus-newsgroup-spam-marked
9660                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9661                                               article)))
9662               ((= mark gnus-dormant-mark)
9663                (setq gnus-newsgroup-dormant
9664                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9665                                               article)))
9666               (t
9667                (setq gnus-newsgroup-unreads
9668                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9669                                               article))))
9670         (gnus-pull article gnus-newsgroup-reads)
9671
9672         ;; See whether the article is to be put in the cache.
9673         (and gnus-use-cache
9674              (vectorp (gnus-summary-article-header article))
9675              (save-excursion
9676                (gnus-cache-possibly-enter-article
9677                 gnus-newsgroup-name article
9678                 (= mark gnus-ticked-mark)
9679                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9680
9681         ;; Fix the mark.
9682         (gnus-summary-update-mark mark 'unread)
9683         t))))
9684
9685 (defun gnus-summary-mark-article (&optional article mark no-expire)
9686   "Mark ARTICLE with MARK.  MARK can be any character.
9687 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9688 `??' (dormant) and `?E' (expirable).
9689 If MARK is nil, then the default character `?r' is used.
9690 If ARTICLE is nil, then the article on the current line will be
9691 marked.
9692 If NO-EXPIRE, auto-expiry will be inhibited."
9693   ;; The mark might be a string.
9694   (when (stringp mark)
9695     (setq mark (aref mark 0)))
9696   ;; If no mark is given, then we check auto-expiring.
9697   (when (null mark)
9698     (setq mark gnus-del-mark))
9699   (when (and (not no-expire)
9700              gnus-newsgroup-auto-expire
9701              (memq mark gnus-auto-expirable-marks))
9702     (setq mark gnus-expirable-mark))
9703   (let ((article (or article (gnus-summary-article-number)))
9704         (old-mark (gnus-summary-article-mark article)))
9705     ;; Allow the backend to change the mark.
9706     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9707     (if (eq mark old-mark)
9708         t
9709       (unless article
9710         (error "No article on current line"))
9711       (if (not (if (or (= mark gnus-unread-mark)
9712                        (= mark gnus-ticked-mark)
9713                        (= mark gnus-spam-mark)
9714                        (= mark gnus-dormant-mark))
9715                    (gnus-mark-article-as-unread article mark)
9716                  (gnus-mark-article-as-read article mark)))
9717           t
9718         ;; See whether the article is to be put in the cache.
9719         (and gnus-use-cache
9720              (not (= mark gnus-canceled-mark))
9721              (vectorp (gnus-summary-article-header article))
9722              (save-excursion
9723                (gnus-cache-possibly-enter-article
9724                 gnus-newsgroup-name article
9725                 (= mark gnus-ticked-mark)
9726                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9727
9728         (when (gnus-summary-goto-subject article nil t)
9729           (let ((buffer-read-only nil))
9730             (gnus-summary-show-thread)
9731             ;; Fix the mark.
9732             (gnus-summary-update-mark mark 'unread)
9733             t))))))
9734
9735 (defun gnus-summary-update-secondary-mark (article)
9736   "Update the secondary (read, process, cache) mark."
9737   (gnus-summary-update-mark
9738    (cond ((memq article gnus-newsgroup-processable)
9739           gnus-process-mark)
9740          ((memq article gnus-newsgroup-cached)
9741           gnus-cached-mark)
9742          ((memq article gnus-newsgroup-replied)
9743           gnus-replied-mark)
9744          ((memq article gnus-newsgroup-forwarded)
9745           gnus-forwarded-mark)
9746          ((memq article gnus-newsgroup-saved)
9747           gnus-saved-mark)
9748          ((memq article gnus-newsgroup-recent)
9749           gnus-recent-mark)
9750          ((memq article gnus-newsgroup-unseen)
9751           gnus-unseen-mark)
9752          (t gnus-no-mark))
9753    'replied)
9754   (when (gnus-visual-p 'summary-highlight 'highlight)
9755     (gnus-run-hooks 'gnus-summary-update-hook))
9756   t)
9757
9758 (defun gnus-summary-update-download-mark (article)
9759   "Update the secondary (read, process, cache) mark."
9760   (gnus-summary-update-mark
9761    (cond ((memq article gnus-newsgroup-undownloaded) 
9762           gnus-undownloaded-mark)
9763          (gnus-newsgroup-agentized
9764           gnus-downloaded-mark)
9765          (t
9766           gnus-no-mark))
9767    'download)
9768   (gnus-summary-update-line t)
9769   t)
9770
9771 (defun gnus-summary-update-mark (mark type)
9772   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9773         (buffer-read-only nil))
9774     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9775     (when forward
9776       (when (looking-at "\r")
9777         (incf forward))
9778       (when (<= (+ forward (point)) (point-max))
9779         ;; Go to the right position on the line.
9780         (goto-char (+ forward (point)))
9781         ;; Replace the old mark with the new mark.
9782         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9783         ;; Optionally update the marks by some user rule.
9784         (when (eq type 'unread)
9785           (gnus-data-set-mark
9786            (gnus-data-find (gnus-summary-article-number)) mark)
9787           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9788
9789 (defun gnus-mark-article-as-read (article &optional mark)
9790   "Enter ARTICLE in the pertinent lists and remove it from others."
9791   ;; Make the article expirable.
9792   (let ((mark (or mark gnus-del-mark)))
9793     (setq gnus-newsgroup-expirable
9794           (if (= mark gnus-expirable-mark)
9795               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9796             (delq article gnus-newsgroup-expirable)))
9797     ;; Remove from unread and marked lists.
9798     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9799     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9800     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9801     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9802     (push (cons article mark) gnus-newsgroup-reads)
9803     ;; Possibly remove from cache, if that is used.
9804     (when gnus-use-cache
9805       (gnus-cache-enter-remove-article article))
9806     t))
9807
9808 (defun gnus-mark-article-as-unread (article &optional mark)
9809   "Enter ARTICLE in the pertinent lists and remove it from others."
9810   (let ((mark (or mark gnus-ticked-mark)))
9811     (if (<= article 0)
9812         (progn
9813           (gnus-error 1 "Can't mark negative article numbers")
9814           nil)
9815       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9816             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9817             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9818             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9819             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9820
9821       ;; Unsuppress duplicates?
9822       (when gnus-suppress-duplicates
9823         (gnus-dup-unsuppress-article article))
9824
9825       (cond ((= mark gnus-ticked-mark)
9826              (setq gnus-newsgroup-marked
9827                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9828             ((= mark gnus-spam-mark)
9829              (setq gnus-newsgroup-spam-marked
9830                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9831                                             article)))
9832             ((= mark gnus-dormant-mark)
9833              (setq gnus-newsgroup-dormant
9834                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9835             (t
9836              (setq gnus-newsgroup-unreads
9837                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9838       (gnus-pull article gnus-newsgroup-reads)
9839       t)))
9840
9841 (defalias 'gnus-summary-mark-as-unread-forward
9842   'gnus-summary-tick-article-forward)
9843 (make-obsolete 'gnus-summary-mark-as-unread-forward
9844                'gnus-summary-tick-article-forward)
9845 (defun gnus-summary-tick-article-forward (n)
9846   "Tick N articles forwards.
9847 If N is negative, tick backwards instead.
9848 The difference between N and the number of articles ticked is returned."
9849   (interactive "p")
9850   (gnus-summary-mark-forward n gnus-ticked-mark))
9851
9852 (defalias 'gnus-summary-mark-as-unread-backward
9853   'gnus-summary-tick-article-backward)
9854 (make-obsolete 'gnus-summary-mark-as-unread-backward
9855                'gnus-summary-tick-article-backward)
9856 (defun gnus-summary-tick-article-backward (n)
9857   "Tick N articles backwards.
9858 The difference between N and the number of articles ticked is returned."
9859   (interactive "p")
9860   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9861
9862 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9863 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9864 (defun gnus-summary-tick-article (&optional article clear-mark)
9865   "Mark current article as unread.
9866 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9867 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9868   (interactive)
9869   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9870                                        gnus-ticked-mark)))
9871
9872 (defun gnus-summary-mark-as-read-forward (n)
9873   "Mark N articles as read forwards.
9874 If N is negative, mark backwards instead.
9875 The difference between N and the actual number of articles marked is
9876 returned."
9877   (interactive "p")
9878   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9879
9880 (defun gnus-summary-mark-as-read-backward (n)
9881   "Mark the N articles as read backwards.
9882 The difference between N and the actual number of articles marked is
9883 returned."
9884   (interactive "p")
9885   (gnus-summary-mark-forward
9886    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9887
9888 (defun gnus-summary-mark-as-read (&optional article mark)
9889   "Mark current article as read.
9890 ARTICLE specifies the article to be marked as read.
9891 MARK specifies a string to be inserted at the beginning of the line."
9892   (gnus-summary-mark-article article mark))
9893
9894 (defun gnus-summary-clear-mark-forward (n)
9895   "Clear marks from N articles forward.
9896 If N is negative, clear backward instead.
9897 The difference between N and the number of marks cleared is returned."
9898   (interactive "p")
9899   (gnus-summary-mark-forward n gnus-unread-mark))
9900
9901 (defun gnus-summary-clear-mark-backward (n)
9902   "Clear marks from N articles backward.
9903 The difference between N and the number of marks cleared is returned."
9904   (interactive "p")
9905   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9906
9907 (defun gnus-summary-mark-unread-as-read ()
9908   "Intended to be used by `gnus-summary-mark-article-hook'."
9909   (when (memq gnus-current-article gnus-newsgroup-unreads)
9910     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9911
9912 (defun gnus-summary-mark-read-and-unread-as-read ()
9913   "Intended to be used by `gnus-summary-mark-article-hook'."
9914   (let ((mark (gnus-summary-article-mark)))
9915     (when (or (gnus-unread-mark-p mark)
9916               (gnus-read-mark-p mark))
9917       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9918
9919 (defun gnus-summary-mark-unread-as-ticked ()
9920   "Intended to be used by `gnus-summary-mark-article-hook'."
9921   (when (memq gnus-current-article gnus-newsgroup-unreads)
9922     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9923
9924 (defun gnus-summary-mark-region-as-read (point mark all)
9925   "Mark all unread articles between point and mark as read.
9926 If given a prefix, mark all articles between point and mark as read,
9927 even ticked and dormant ones."
9928   (interactive "r\nP")
9929   (save-excursion
9930     (let (article)
9931       (goto-char point)
9932       (beginning-of-line)
9933       (while (and
9934               (< (point) mark)
9935               (progn
9936                 (when (or all
9937                           (memq (setq article (gnus-summary-article-number))
9938                                 gnus-newsgroup-unreads))
9939                   (gnus-summary-mark-article article gnus-del-mark))
9940                 t)
9941               (gnus-summary-find-next))))))
9942
9943 (defun gnus-summary-mark-below (score mark)
9944   "Mark articles with score less than SCORE with MARK."
9945   (interactive "P\ncMark: ")
9946   (setq score (if score
9947                   (prefix-numeric-value score)
9948                 (or gnus-summary-default-score 0)))
9949   (save-excursion
9950     (set-buffer gnus-summary-buffer)
9951     (goto-char (point-min))
9952     (while
9953         (progn
9954           (and (< (gnus-summary-article-score) score)
9955                (gnus-summary-mark-article nil mark))
9956           (gnus-summary-find-next)))))
9957
9958 (defun gnus-summary-kill-below (&optional score)
9959   "Mark articles with score below SCORE as read."
9960   (interactive "P")
9961   (gnus-summary-mark-below score gnus-killed-mark))
9962
9963 (defun gnus-summary-clear-above (&optional score)
9964   "Clear all marks from articles with score above SCORE."
9965   (interactive "P")
9966   (gnus-summary-mark-above score gnus-unread-mark))
9967
9968 (defun gnus-summary-tick-above (&optional score)
9969   "Tick all articles with score above SCORE."
9970   (interactive "P")
9971   (gnus-summary-mark-above score gnus-ticked-mark))
9972
9973 (defun gnus-summary-mark-above (score mark)
9974   "Mark articles with score over SCORE with MARK."
9975   (interactive "P\ncMark: ")
9976   (setq score (if score
9977                   (prefix-numeric-value score)
9978                 (or gnus-summary-default-score 0)))
9979   (save-excursion
9980     (set-buffer gnus-summary-buffer)
9981     (goto-char (point-min))
9982     (while (and (progn
9983                   (when (> (gnus-summary-article-score) score)
9984                     (gnus-summary-mark-article nil mark))
9985                   t)
9986                 (gnus-summary-find-next)))))
9987
9988 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9989 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9990 (defun gnus-summary-limit-include-expunged (&optional no-error)
9991   "Display all the hidden articles that were expunged for low scores."
9992   (interactive)
9993   (let ((buffer-read-only nil))
9994     (let ((scored gnus-newsgroup-scored)
9995           headers h)
9996       (while scored
9997         (unless (gnus-summary-article-header (caar scored))
9998           (and (setq h (gnus-number-to-header (caar scored)))
9999                (< (cdar scored) gnus-summary-expunge-below)
10000                (push h headers)))
10001         (setq scored (cdr scored)))
10002       (if (not headers)
10003           (when (not no-error)
10004             (error "No expunged articles hidden"))
10005         (goto-char (point-min))
10006         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10007         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10008         (mapcar (lambda (x) (push (mail-header-number x)
10009                                   gnus-newsgroup-limit))
10010                 headers)
10011         (gnus-summary-prepare-unthreaded (nreverse headers))
10012         (goto-char (point-min))
10013         (gnus-summary-position-point)
10014         t))))
10015
10016 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10017   "Mark all unread articles in this newsgroup as read.
10018 If prefix argument ALL is non-nil, ticked and dormant articles will
10019 also be marked as read.
10020 If QUIETLY is non-nil, no questions will be asked.
10021 If TO-HERE is non-nil, it should be a point in the buffer.  All
10022 articles before (after, if REVERSE is set) this point will be marked as read.
10023 Note that this function will only catch up the unread article
10024 in the current summary buffer limitation.
10025 The number of articles marked as read is returned."
10026   (interactive "P")
10027   (prog1
10028       (save-excursion
10029         (when (or quietly
10030                   (not gnus-interactive-catchup) ;Without confirmation?
10031                   gnus-expert-user
10032                   (gnus-y-or-n-p
10033                    (if all
10034                        "Mark absolutely all articles as read? "
10035                      "Mark all unread articles as read? ")))
10036           (if (and not-mark
10037                    (not gnus-newsgroup-adaptive)
10038                    (not gnus-newsgroup-auto-expire)
10039                    (not gnus-suppress-duplicates)
10040                    (or (not gnus-use-cache)
10041                        (eq gnus-use-cache 'passive)))
10042               (progn
10043                 (when all
10044                   (setq gnus-newsgroup-marked nil
10045                         gnus-newsgroup-spam-marked nil
10046                         gnus-newsgroup-dormant nil))
10047                 (setq gnus-newsgroup-unreads
10048                       (gnus-sorted-nunion
10049                        (gnus-intersection gnus-newsgroup-unreads
10050                                           gnus-newsgroup-downloadable)
10051                        gnus-newsgroup-unfetched)))
10052             ;; We actually mark all articles as canceled, which we
10053             ;; have to do when using auto-expiry or adaptive scoring.
10054             (gnus-summary-show-all-threads)
10055             (if (and to-here reverse)
10056                 (progn
10057                   (goto-char to-here)
10058                   (while (and
10059                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
10060                           (gnus-summary-find-next (not all)))))
10061               (when (gnus-summary-first-subject (not all))
10062                 (while (and
10063                         (if to-here (< (point) to-here) t)
10064                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10065                         (gnus-summary-find-next (not all))))))
10066             (gnus-set-mode-line 'summary))
10067           t))
10068     (gnus-summary-position-point)))
10069
10070 (defun gnus-summary-catchup-to-here (&optional all)
10071   "Mark all unticked articles before the current one as read.
10072 If ALL is non-nil, also mark ticked and dormant articles as read."
10073   (interactive "P")
10074   (save-excursion
10075     (gnus-save-hidden-threads
10076       (let ((beg (point)))
10077         ;; We check that there are unread articles.
10078         (when (or all (gnus-summary-find-prev))
10079           (gnus-summary-catchup all t beg)))))
10080   (gnus-summary-position-point))
10081
10082 (defun gnus-summary-catchup-from-here (&optional all)
10083   "Mark all unticked articles after the current one as read.
10084 If ALL is non-nil, also mark ticked and dormant articles as read."
10085   (interactive "P")
10086   (save-excursion
10087     (gnus-save-hidden-threads
10088       (let ((beg (point)))
10089         ;; We check that there are unread articles.
10090         (when (or all (gnus-summary-find-next))
10091           (gnus-summary-catchup all t beg nil t)))))
10092
10093   (gnus-summary-position-point))
10094 (defun gnus-summary-catchup-all (&optional quietly)
10095   "Mark all articles in this newsgroup as read.
10096 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10097 instead, which marks only unread articles as read."
10098   (interactive "P")
10099   (gnus-summary-catchup t quietly))
10100
10101 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10102   "Mark all unread articles in this group as read, then exit.
10103 If prefix argument ALL is non-nil, all articles are marked as read.
10104 If QUIETLY is non-nil, no questions will be asked."
10105   (interactive "P")
10106   (when (gnus-summary-catchup all quietly nil 'fast)
10107     ;; Select next newsgroup or exit.
10108     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10109              (eq gnus-auto-select-next 'quietly))
10110         (gnus-summary-next-group nil)
10111       (gnus-summary-exit))))
10112
10113 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10114   "Mark all articles in this newsgroup as read, and then exit.
10115 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10116 instead, which marks only unread articles as read."
10117   (interactive "P")
10118   (gnus-summary-catchup-and-exit t quietly))
10119
10120 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10121   "Mark all articles in this group as read and select the next group.
10122 If given a prefix, mark all articles, unread as well as ticked, as
10123 read."
10124   (interactive "P")
10125   (save-excursion
10126     (gnus-summary-catchup all))
10127   (gnus-summary-next-group))
10128
10129 ;;;
10130 ;;; with article
10131 ;;;
10132
10133 (defmacro gnus-with-article (article &rest forms)
10134   "Select ARTICLE and perform FORMS in the original article buffer.
10135 Then replace the article with the result."
10136   `(progn
10137      ;; We don't want the article to be marked as read.
10138      (let (gnus-mark-article-hook)
10139        (gnus-summary-select-article t t nil ,article))
10140      (set-buffer gnus-original-article-buffer)
10141      ,@forms
10142      (if (not (gnus-check-backend-function
10143                'request-replace-article (car gnus-article-current)))
10144          (gnus-message 5 "Read-only group; not replacing")
10145        (unless (gnus-request-replace-article
10146                 ,article (car gnus-article-current)
10147                 (current-buffer) t)
10148          (error "Couldn't replace article")))
10149      ;; The cache and backlog have to be flushed somewhat.
10150      (when gnus-keep-backlog
10151        (gnus-backlog-remove-article
10152         (car gnus-article-current) (cdr gnus-article-current)))
10153      (when gnus-use-cache
10154        (gnus-cache-update-article
10155         (car gnus-article-current) (cdr gnus-article-current)))))
10156
10157 (put 'gnus-with-article 'lisp-indent-function 1)
10158 (put 'gnus-with-article 'edebug-form-spec '(form body))
10159
10160 ;; Thread-based commands.
10161
10162 (defun gnus-summary-articles-in-thread (&optional article)
10163   "Return a list of all articles in the current thread.
10164 If ARTICLE is non-nil, return all articles in the thread that starts
10165 with that article."
10166   (let* ((article (or article (gnus-summary-article-number)))
10167          (data (gnus-data-find-list article))
10168          (top-level (gnus-data-level (car data)))
10169          (top-subject
10170           (cond ((null gnus-thread-operation-ignore-subject)
10171                  (gnus-simplify-subject-re
10172                   (mail-header-subject (gnus-data-header (car data)))))
10173                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10174                  (gnus-simplify-subject-fuzzy
10175                   (mail-header-subject (gnus-data-header (car data)))))
10176                 (t nil)))
10177          (end-point (save-excursion
10178                       (if (gnus-summary-go-to-next-thread)
10179                           (point) (point-max))))
10180          articles)
10181     (while (and data
10182                 (< (gnus-data-pos (car data)) end-point))
10183       (when (or (not top-subject)
10184                 (string= top-subject
10185                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10186                              (gnus-simplify-subject-fuzzy
10187                               (mail-header-subject
10188                                (gnus-data-header (car data))))
10189                            (gnus-simplify-subject-re
10190                             (mail-header-subject
10191                              (gnus-data-header (car data)))))))
10192         (push (gnus-data-number (car data)) articles))
10193       (unless (and (setq data (cdr data))
10194                    (> (gnus-data-level (car data)) top-level))
10195         (setq data nil)))
10196     ;; Return the list of articles.
10197     (nreverse articles)))
10198
10199 (defun gnus-summary-rethread-current ()
10200   "Rethread the thread the current article is part of."
10201   (interactive)
10202   (let* ((gnus-show-threads t)
10203          (article (gnus-summary-article-number))
10204          (id (mail-header-id (gnus-summary-article-header)))
10205          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10206     (unless id
10207       (error "No article on the current line"))
10208     (gnus-rebuild-thread id)
10209     (gnus-summary-goto-subject article)))
10210
10211 (defun gnus-summary-reparent-thread ()
10212   "Make the current article child of the marked (or previous) article.
10213
10214 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10215 is non-nil or the Subject: of both articles are the same."
10216   (interactive)
10217   (unless (not (gnus-group-read-only-p))
10218     (error "The current newsgroup does not support article editing"))
10219   (unless (<= (length gnus-newsgroup-processable) 1)
10220     (error "No more than one article may be marked"))
10221   (save-window-excursion
10222     (let ((gnus-article-buffer " *reparent*")
10223           (current-article (gnus-summary-article-number))
10224           ;; First grab the marked article, otherwise one line up.
10225           (parent-article (if (not (null gnus-newsgroup-processable))
10226                               (car gnus-newsgroup-processable)
10227                             (save-excursion
10228                               (if (eq (forward-line -1) 0)
10229                                   (gnus-summary-article-number)
10230                                 (error "Beginning of summary buffer"))))))
10231       (unless (not (eq current-article parent-article))
10232         (error "An article may not be self-referential"))
10233       (let ((message-id (mail-header-id
10234                          (gnus-summary-article-header parent-article))))
10235         (unless (and message-id (not (equal message-id "")))
10236           (error "No message-id in desired parent"))
10237         (gnus-with-article current-article
10238           (save-restriction
10239             (goto-char (point-min))
10240             (message-narrow-to-head)
10241             (if (re-search-forward "^References: " nil t)
10242                 (progn
10243                   (re-search-forward "^[^ \t]" nil t)
10244                   (forward-line -1)
10245                   (end-of-line)
10246                   (insert " " message-id))
10247               (insert "References: " message-id "\n"))))
10248         (set-buffer gnus-summary-buffer)
10249         (gnus-summary-unmark-all-processable)
10250         (gnus-summary-update-article current-article)
10251         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10252             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10253         (gnus-summary-rethread-current)
10254         (gnus-message 3 "Article %d is now the child of article %d"
10255                       current-article parent-article)))))
10256
10257 (defun gnus-summary-toggle-threads (&optional arg)
10258   "Toggle showing conversation threads.
10259 If ARG is positive number, turn showing conversation threads on."
10260   (interactive "P")
10261   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10262     (setq gnus-show-threads
10263           (if (null arg) (not gnus-show-threads)
10264             (> (prefix-numeric-value arg) 0)))
10265     (gnus-summary-prepare)
10266     (gnus-summary-goto-subject current)
10267     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10268     (gnus-summary-position-point)))
10269
10270 (defun gnus-summary-show-all-threads ()
10271   "Show all threads."
10272   (interactive)
10273   (save-excursion
10274     (let ((buffer-read-only nil))
10275       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10276   (gnus-summary-position-point))
10277
10278 (defun gnus-summary-show-thread ()
10279   "Show thread subtrees.
10280 Returns nil if no thread was there to be shown."
10281   (interactive)
10282   (let ((buffer-read-only nil)
10283         (orig (point))
10284         ;; first goto end then to beg, to have point at beg after let
10285         (end (progn (end-of-line) (point)))
10286         (beg (progn (beginning-of-line) (point))))
10287     (prog1
10288         ;; Any hidden lines here?
10289         (search-forward "\r" end t)
10290       (subst-char-in-region beg end ?\^M ?\n t)
10291       (goto-char orig)
10292       (gnus-summary-position-point))))
10293
10294 (defun gnus-summary-maybe-hide-threads ()
10295   "If requested, hide the threads that should be hidden."
10296   (when (and gnus-show-threads
10297              gnus-thread-hide-subtree)
10298     (gnus-summary-hide-all-threads
10299      (if (or (consp gnus-thread-hide-subtree)
10300              (gnus-functionp gnus-thread-hide-subtree))
10301          (gnus-make-predicate gnus-thread-hide-subtree)
10302        nil))))
10303
10304 ;;; Hiding predicates.
10305
10306 (defun gnus-article-unread-p (header)
10307   (memq (mail-header-number header) gnus-newsgroup-unreads))
10308
10309 (defun gnus-article-unseen-p (header)
10310   (memq (mail-header-number header) gnus-newsgroup-unseen))
10311
10312 (defun gnus-map-articles (predicate articles)
10313   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10314   (apply 'gnus-or (mapcar predicate
10315                           (mapcar 'gnus-summary-article-header articles))))
10316
10317 (defun gnus-summary-hide-all-threads (&optional predicate)
10318   "Hide all thread subtrees.
10319 If PREDICATE is supplied, threads that satisfy this predicate
10320 will not be hidden."
10321   (interactive)
10322   (save-excursion
10323     (goto-char (point-min))
10324     (let ((end nil))
10325       (while (not end)
10326         (when (or (not predicate)
10327                   (gnus-map-articles
10328                    predicate (gnus-summary-article-children)))
10329             (gnus-summary-hide-thread))
10330         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10331   (gnus-summary-position-point))
10332
10333 (defun gnus-summary-hide-thread ()
10334   "Hide thread subtrees.
10335 If PREDICATE is supplied, threads that satisfy this predicate
10336 will not be hidden.
10337 Returns nil if no threads were there to be hidden."
10338   (interactive)
10339   (let ((buffer-read-only nil)
10340         (start (point))
10341         (article (gnus-summary-article-number)))
10342     (goto-char start)
10343     ;; Go forward until either the buffer ends or the subthread
10344     ;; ends.
10345     (when (and (not (eobp))
10346                (or (zerop (gnus-summary-next-thread 1 t))
10347                    (goto-char (point-max))))
10348       (prog1
10349           (if (and (> (point) start)
10350                    (search-backward "\n" start t))
10351               (progn
10352                 (subst-char-in-region start (point) ?\n ?\^M)
10353                 (gnus-summary-goto-subject article))
10354             (goto-char start)
10355             nil)))))
10356
10357 (defun gnus-summary-go-to-next-thread (&optional previous)
10358   "Go to the same level (or less) next thread.
10359 If PREVIOUS is non-nil, go to previous thread instead.
10360 Return the article number moved to, or nil if moving was impossible."
10361   (let ((level (gnus-summary-thread-level))
10362         (way (if previous -1 1))
10363         (beg (point)))
10364     (forward-line way)
10365     (while (and (not (eobp))
10366                 (< level (gnus-summary-thread-level)))
10367       (forward-line way))
10368     (if (eobp)
10369         (progn
10370           (goto-char beg)
10371           nil)
10372       (setq beg (point))
10373       (prog1
10374           (gnus-summary-article-number)
10375         (goto-char beg)))))
10376
10377 (defun gnus-summary-next-thread (n &optional silent)
10378   "Go to the same level next N'th thread.
10379 If N is negative, search backward instead.
10380 Returns the difference between N and the number of skips actually
10381 done.
10382
10383 If SILENT, don't output messages."
10384   (interactive "p")
10385   (let ((backward (< n 0))
10386         (n (abs n)))
10387     (while (and (> n 0)
10388                 (gnus-summary-go-to-next-thread backward))
10389       (decf n))
10390     (unless silent
10391       (gnus-summary-position-point))
10392     (when (and (not silent) (/= 0 n))
10393       (gnus-message 7 "No more threads"))
10394     n))
10395
10396 (defun gnus-summary-prev-thread (n)
10397   "Go to the same level previous N'th thread.
10398 Returns the difference between N and the number of skips actually
10399 done."
10400   (interactive "p")
10401   (gnus-summary-next-thread (- n)))
10402
10403 (defun gnus-summary-go-down-thread ()
10404   "Go down one level in the current thread."
10405   (let ((children (gnus-summary-article-children)))
10406     (when children
10407       (gnus-summary-goto-subject (car children)))))
10408
10409 (defun gnus-summary-go-up-thread ()
10410   "Go up one level in the current thread."
10411   (let ((parent (gnus-summary-article-parent)))
10412     (when parent
10413       (gnus-summary-goto-subject parent))))
10414
10415 (defun gnus-summary-down-thread (n)
10416   "Go down thread N steps.
10417 If N is negative, go up instead.
10418 Returns the difference between N and how many steps down that were
10419 taken."
10420   (interactive "p")
10421   (let ((up (< n 0))
10422         (n (abs n)))
10423     (while (and (> n 0)
10424                 (if up (gnus-summary-go-up-thread)
10425                   (gnus-summary-go-down-thread)))
10426       (setq n (1- n)))
10427     (gnus-summary-position-point)
10428     (when (/= 0 n)
10429       (gnus-message 7 "Can't go further"))
10430     n))
10431
10432 (defun gnus-summary-up-thread (n)
10433   "Go up thread N steps.
10434 If N is negative, go down instead.
10435 Returns the difference between N and how many steps down that were
10436 taken."
10437   (interactive "p")
10438   (gnus-summary-down-thread (- n)))
10439
10440 (defun gnus-summary-top-thread ()
10441   "Go to the top of the thread."
10442   (interactive)
10443   (while (gnus-summary-go-up-thread))
10444   (gnus-summary-article-number))
10445
10446 (defun gnus-summary-kill-thread (&optional unmark)
10447   "Mark articles under current thread as read.
10448 If the prefix argument is positive, remove any kinds of marks.
10449 If the prefix argument is negative, tick articles instead."
10450   (interactive "P")
10451   (when unmark
10452     (setq unmark (prefix-numeric-value unmark)))
10453   (let ((articles (gnus-summary-articles-in-thread)))
10454     (save-excursion
10455       ;; Expand the thread.
10456       (gnus-summary-show-thread)
10457       ;; Mark all the articles.
10458       (while articles
10459         (gnus-summary-goto-subject (car articles))
10460         (cond ((null unmark)
10461                (gnus-summary-mark-article-as-read gnus-killed-mark))
10462               ((> unmark 0)
10463                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10464               (t
10465                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10466         (setq articles (cdr articles))))
10467     ;; Hide killed subtrees.
10468     (and (null unmark)
10469          gnus-thread-hide-killed
10470          (gnus-summary-hide-thread))
10471     ;; If marked as read, go to next unread subject.
10472     (when (null unmark)
10473       ;; Go to next unread subject.
10474       (gnus-summary-next-subject 1 t)))
10475   (gnus-set-mode-line 'summary))
10476
10477 ;; Summary sorting commands
10478
10479 (defun gnus-summary-sort-by-number (&optional reverse)
10480   "Sort the summary buffer by article number.
10481 Argument REVERSE means reverse order."
10482   (interactive "P")
10483   (gnus-summary-sort 'number reverse))
10484
10485 (defun gnus-summary-sort-by-random (&optional reverse)
10486   "Randomize the order in the summary buffer.
10487 Argument REVERSE means to randomize in reverse order."
10488   (interactive "P")
10489   (gnus-summary-sort 'random reverse))
10490
10491 (defun gnus-summary-sort-by-author (&optional reverse)
10492   "Sort the summary buffer by author name alphabetically.
10493 If `case-fold-search' is non-nil, case of letters is ignored.
10494 Argument REVERSE means reverse order."
10495   (interactive "P")
10496   (gnus-summary-sort 'author reverse))
10497
10498 (defun gnus-summary-sort-by-subject (&optional reverse)
10499   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10500 If `case-fold-search' is non-nil, case of letters is ignored.
10501 Argument REVERSE means reverse order."
10502   (interactive "P")
10503   (gnus-summary-sort 'subject reverse))
10504
10505 (defun gnus-summary-sort-by-date (&optional reverse)
10506   "Sort the summary buffer by date.
10507 Argument REVERSE means reverse order."
10508   (interactive "P")
10509   (gnus-summary-sort 'date reverse))
10510
10511 (defun gnus-summary-sort-by-score (&optional reverse)
10512   "Sort the summary buffer by score.
10513 Argument REVERSE means reverse order."
10514   (interactive "P")
10515   (gnus-summary-sort 'score reverse))
10516
10517 (defun gnus-summary-sort-by-lines (&optional reverse)
10518   "Sort the summary buffer by the number of lines.
10519 Argument REVERSE means reverse order."
10520   (interactive "P")
10521   (gnus-summary-sort 'lines reverse))
10522
10523 (defun gnus-summary-sort-by-chars (&optional reverse)
10524   "Sort the summary buffer by article length.
10525 Argument REVERSE means reverse order."
10526   (interactive "P")
10527   (gnus-summary-sort 'chars reverse))
10528
10529 (defun gnus-summary-sort-by-original (&optional reverse)
10530   "Sort the summary buffer using the default sorting method.
10531 Argument REVERSE means reverse order."
10532   (interactive "P")
10533   (let* ((buffer-read-only)
10534          (gnus-summary-prepare-hook nil))
10535     ;; We do the sorting by regenerating the threads.
10536     (gnus-summary-prepare)
10537     ;; Hide subthreads if needed.
10538     (gnus-summary-maybe-hide-threads)))
10539
10540 (defun gnus-summary-sort (predicate reverse)
10541   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10542   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10543          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10544          (gnus-thread-sort-functions
10545           (if (not reverse)
10546               thread
10547             `(lambda (t1 t2)
10548                (,thread t2 t1))))
10549          (gnus-sort-gathered-threads-function
10550           gnus-thread-sort-functions)
10551          (gnus-article-sort-functions
10552           (if (not reverse)
10553               article
10554             `(lambda (t1 t2)
10555                (,article t2 t1))))
10556          (buffer-read-only)
10557          (gnus-summary-prepare-hook nil))
10558     ;; We do the sorting by regenerating the threads.
10559     (gnus-summary-prepare)
10560     ;; Hide subthreads if needed.
10561     (gnus-summary-maybe-hide-threads)))
10562
10563 ;; Summary saving commands.
10564
10565 (defun gnus-summary-save-article (&optional n not-saved)
10566   "Save the current article using the default saver function.
10567 If N is a positive number, save the N next articles.
10568 If N is a negative number, save the N previous articles.
10569 If N is nil and any articles have been marked with the process mark,
10570 save those articles instead.
10571 The variable `gnus-default-article-saver' specifies the saver function."
10572   (interactive "P")
10573   (let* ((articles (gnus-summary-work-articles n))
10574          (save-buffer (save-excursion
10575                         (nnheader-set-temp-buffer " *Gnus Save*")))
10576          (num (length articles))
10577          header file)
10578     (dolist (article articles)
10579       (setq header (gnus-summary-article-header article))
10580       (if (not (vectorp header))
10581           ;; This is a pseudo-article.
10582           (if (assq 'name header)
10583               (gnus-copy-file (cdr (assq 'name header)))
10584             (gnus-message 1 "Article %d is unsaveable" article))
10585         ;; This is a real article.
10586         (save-window-excursion
10587           (let ((gnus-display-mime-function nil)
10588                 (gnus-article-prepare-hook nil))
10589             (gnus-summary-select-article t nil nil article)))
10590         (save-excursion
10591           (set-buffer save-buffer)
10592           (erase-buffer)
10593           (insert-buffer-substring gnus-original-article-buffer))
10594         (setq file (gnus-article-save save-buffer file num))
10595         (gnus-summary-remove-process-mark article)
10596         (unless not-saved
10597           (gnus-summary-set-saved-mark article))))
10598     (gnus-kill-buffer save-buffer)
10599     (gnus-summary-position-point)
10600     (gnus-set-mode-line 'summary)
10601     n))
10602
10603 (defun gnus-summary-pipe-output (&optional arg headers)
10604   "Pipe the current article to a subprocess.
10605 If N is a positive number, pipe the N next articles.
10606 If N is a negative number, pipe the N previous articles.
10607 If N is nil and any articles have been marked with the process mark,
10608 pipe those articles instead.
10609 If HEADERS (the symbolic prefix), include the headers, too."
10610   (interactive (gnus-interactive "P\ny"))
10611   (require 'gnus-art)
10612   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10613         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10614     (gnus-summary-save-article arg t))
10615   (let ((buffer (get-buffer "*Shell Command Output*")))
10616     (when (and buffer
10617                (not (zerop (buffer-size buffer))))
10618       (gnus-configure-windows 'pipe))))
10619
10620 (defun gnus-summary-save-article-mail (&optional arg)
10621   "Append the current article to an mail file.
10622 If N is a positive number, save the N next articles.
10623 If N is a negative number, save the N previous articles.
10624 If N is nil and any articles have been marked with the process mark,
10625 save those articles instead."
10626   (interactive "P")
10627   (require 'gnus-art)
10628   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10629     (gnus-summary-save-article arg)))
10630
10631 (defun gnus-summary-save-article-rmail (&optional arg)
10632   "Append the current article to an rmail file.
10633 If N is a positive number, save the N next articles.
10634 If N is a negative number, save the N previous articles.
10635 If N is nil and any articles have been marked with the process mark,
10636 save those articles instead."
10637   (interactive "P")
10638   (require 'gnus-art)
10639   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10640     (gnus-summary-save-article arg)))
10641
10642 (defun gnus-summary-save-article-file (&optional arg)
10643   "Append the current article to a file.
10644 If N is a positive number, save the N next articles.
10645 If N is a negative number, save the N previous articles.
10646 If N is nil and any articles have been marked with the process mark,
10647 save those articles instead."
10648   (interactive "P")
10649   (require 'gnus-art)
10650   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10651     (gnus-summary-save-article arg)))
10652
10653 (defun gnus-summary-write-article-file (&optional arg)
10654   "Write the current article to a file, deleting the previous file.
10655 If N is a positive number, save the N next articles.
10656 If N is a negative number, save the N previous articles.
10657 If N is nil and any articles have been marked with the process mark,
10658 save those articles instead."
10659   (interactive "P")
10660   (require 'gnus-art)
10661   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10662     (gnus-summary-save-article arg)))
10663
10664 (defun gnus-summary-save-article-body-file (&optional arg)
10665   "Append the current article body to a file.
10666 If N is a positive number, save the N next articles.
10667 If N is a negative number, save the N previous articles.
10668 If N is nil and any articles have been marked with the process mark,
10669 save those articles instead."
10670   (interactive "P")
10671   (require 'gnus-art)
10672   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10673     (gnus-summary-save-article arg)))
10674
10675 (defun gnus-summary-muttprint (&optional arg)
10676   "Print the current article using Muttprint.
10677 If N is a positive number, save the N next articles.
10678 If N is a negative number, save the N previous articles.
10679 If N is nil and any articles have been marked with the process mark,
10680 save those articles instead."
10681   (interactive "P")
10682   (require 'gnus-art)
10683   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10684     (gnus-summary-save-article arg t)))
10685
10686 (defun gnus-summary-pipe-message (program)
10687   "Pipe the current article through PROGRAM."
10688   (interactive "sProgram: ")
10689   (gnus-summary-select-article)
10690   (let ((mail-header-separator ""))
10691     (gnus-eval-in-buffer-window gnus-article-buffer
10692       (save-restriction
10693         (widen)
10694         (let ((start (window-start))
10695               buffer-read-only)
10696           (message-pipe-buffer-body program)
10697           (set-window-start (get-buffer-window (current-buffer)) start))))))
10698
10699 (defun gnus-get-split-value (methods)
10700   "Return a value based on the split METHODS."
10701   (let (split-name method result match)
10702     (when methods
10703       (save-excursion
10704         (set-buffer gnus-original-article-buffer)
10705         (save-restriction
10706           (nnheader-narrow-to-headers)
10707           (while (and methods (not split-name))
10708             (goto-char (point-min))
10709             (setq method (pop methods))
10710             (setq match (car method))
10711             (when (cond
10712                    ((stringp match)
10713                     ;; Regular expression.
10714                     (ignore-errors
10715                       (re-search-forward match nil t)))
10716                    ((gnus-functionp match)
10717                     ;; Function.
10718                     (save-restriction
10719                       (widen)
10720                       (setq result (funcall match gnus-newsgroup-name))))
10721                    ((consp match)
10722                     ;; Form.
10723                     (save-restriction
10724                       (widen)
10725                       (setq result (eval match)))))
10726               (setq split-name (cdr method))
10727               (cond ((stringp result)
10728                      (push (expand-file-name
10729                             result gnus-article-save-directory)
10730                            split-name))
10731                     ((consp result)
10732                      (setq split-name (append result split-name)))))))))
10733     (nreverse split-name)))
10734
10735 (defun gnus-valid-move-group-p (group)
10736   (and (boundp group)
10737        (symbol-name group)
10738        (symbol-value group)
10739        (gnus-get-function (gnus-find-method-for-group
10740                            (symbol-name group)) 'request-accept-article t)))
10741
10742 (defun gnus-read-move-group-name (prompt default articles prefix)
10743   "Read a group name."
10744   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10745          (minibuffer-confirm-incomplete nil) ; XEmacs
10746          (prom
10747           (format "%s %s to:"
10748                   prompt
10749                   (if (> (length articles) 1)
10750                       (format "these %d articles" (length articles))
10751                     "this article")))
10752          (to-newsgroup
10753           (cond
10754            ((null split-name)
10755             (gnus-completing-read-with-default
10756              default prom
10757              gnus-active-hashtb
10758              'gnus-valid-move-group-p
10759              nil prefix
10760              'gnus-group-history))
10761            ((= 1 (length split-name))
10762             (gnus-completing-read-with-default
10763              (car split-name) prom
10764              gnus-active-hashtb
10765              'gnus-valid-move-group-p
10766              nil nil
10767              'gnus-group-history))
10768            (t
10769             (gnus-completing-read-with-default
10770              nil prom
10771              (mapcar (lambda (el) (list el))
10772                      (nreverse split-name))
10773              nil nil nil
10774              'gnus-group-history))))
10775          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10776     (when to-newsgroup
10777       (if (or (string= to-newsgroup "")
10778               (string= to-newsgroup prefix))
10779           (setq to-newsgroup default))
10780       (unless to-newsgroup
10781         (error "No group name entered"))
10782       (or (gnus-active to-newsgroup)
10783           (gnus-activate-group to-newsgroup nil nil to-method)
10784           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10785                                      to-newsgroup))
10786               (or (and (gnus-request-create-group to-newsgroup to-method)
10787                        (gnus-activate-group
10788                         to-newsgroup nil nil to-method)
10789                        (gnus-subscribe-group to-newsgroup))
10790                   (error "Couldn't create group %s" to-newsgroup)))
10791           (error "No such group: %s" to-newsgroup)))
10792     to-newsgroup))
10793
10794 (defun gnus-summary-save-parts (type dir n &optional reverse)
10795   "Save parts matching TYPE to DIR.
10796 If REVERSE, save parts that do not match TYPE."
10797   (interactive
10798    (list (read-string "Save parts of type: "
10799                       (or (car gnus-summary-save-parts-type-history)
10800                           gnus-summary-save-parts-default-mime)
10801                       'gnus-summary-save-parts-type-history)
10802          (setq gnus-summary-save-parts-last-directory
10803                (read-file-name "Save to directory: "
10804                                gnus-summary-save-parts-last-directory
10805                                nil t))
10806          current-prefix-arg))
10807   (gnus-summary-iterate n
10808     (let ((gnus-display-mime-function nil)
10809           (gnus-inhibit-treatment t))
10810       (gnus-summary-select-article))
10811     (save-excursion
10812       (set-buffer gnus-article-buffer)
10813       (let ((handles (or gnus-article-mime-handles
10814                          (mm-dissect-buffer nil gnus-article-loose-mime)
10815                          (and gnus-article-emulate-mime
10816                               (mm-uu-dissect)))))
10817         (when handles
10818           (gnus-summary-save-parts-1 type dir handles reverse)
10819           (unless gnus-article-mime-handles ;; Don't destroy this case.
10820             (mm-destroy-parts handles)))))))
10821
10822 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10823   (if (stringp (car handle))
10824       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10825               (cdr handle))
10826     (when (if reverse
10827               (not (string-match type (mm-handle-media-type handle)))
10828             (string-match type (mm-handle-media-type handle)))
10829       (let ((file (expand-file-name
10830                    (file-name-nondirectory
10831                     (or
10832                      (mail-content-type-get
10833                       (mm-handle-disposition handle) 'filename)
10834                      (concat gnus-newsgroup-name
10835                              "." (number-to-string
10836                                   (cdr gnus-article-current)))))
10837                    dir)))
10838         (unless (file-exists-p file)
10839           (mm-save-part-to-file handle file))))))
10840
10841 ;; Summary extract commands
10842
10843 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10844   (let ((buffer-read-only nil)
10845         (article (gnus-summary-article-number))
10846         after-article b e)
10847     (unless (gnus-summary-goto-subject article)
10848       (error "No such article: %d" article))
10849     (gnus-summary-position-point)
10850     ;; If all commands are to be bunched up on one line, we collect
10851     ;; them here.
10852     (unless gnus-view-pseudos-separately
10853       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10854             files action)
10855         (while ps
10856           (setq action (cdr (assq 'action (car ps))))
10857           (setq files (list (cdr (assq 'name (car ps)))))
10858           (while (and ps (cdr ps)
10859                       (string= (or action "1")
10860                                (or (cdr (assq 'action (cadr ps))) "2")))
10861             (push (cdr (assq 'name (cadr ps))) files)
10862             (setcdr ps (cddr ps)))
10863           (when files
10864             (when (not (string-match "%s" action))
10865               (push " " files))
10866             (push " " files)
10867             (when (assq 'execute (car ps))
10868               (setcdr (assq 'execute (car ps))
10869                       (funcall (if (string-match "%s" action)
10870                                    'format 'concat)
10871                                action
10872                                (mapconcat
10873                                 (lambda (f)
10874                                   (if (equal f " ")
10875                                       f
10876                                     (mm-quote-arg f)))
10877                                 files " ")))))
10878           (setq ps (cdr ps)))))
10879     (if (and gnus-view-pseudos (not not-view))
10880         (while pslist
10881           (when (assq 'execute (car pslist))
10882             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10883                                   (eq gnus-view-pseudos 'not-confirm)))
10884           (setq pslist (cdr pslist)))
10885       (save-excursion
10886         (while pslist
10887           (setq after-article (or (cdr (assq 'article (car pslist)))
10888                                   (gnus-summary-article-number)))
10889           (gnus-summary-goto-subject after-article)
10890           (forward-line 1)
10891           (setq b (point))
10892           (insert "    " (file-name-nondirectory
10893                           (cdr (assq 'name (car pslist))))
10894                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10895           (setq e (point))
10896           (forward-line -1)             ; back to `b'
10897           (gnus-add-text-properties
10898            b (1- e) (list 'gnus-number gnus-reffed-article-number
10899                           gnus-mouse-face-prop gnus-mouse-face))
10900           (gnus-data-enter
10901            after-article gnus-reffed-article-number
10902            gnus-unread-mark b (car pslist) 0 (- e b))
10903           (setq gnus-newsgroup-unreads
10904                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10905                                          gnus-reffed-article-number))
10906           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10907           (setq pslist (cdr pslist)))))))
10908
10909 (defun gnus-pseudos< (p1 p2)
10910   (let ((c1 (cdr (assq 'action p1)))
10911         (c2 (cdr (assq 'action p2))))
10912     (and c1 c2 (string< c1 c2))))
10913
10914 (defun gnus-request-pseudo-article (props)
10915   (cond ((assq 'execute props)
10916          (gnus-execute-command (cdr (assq 'execute props)))))
10917   (let ((gnus-current-article (gnus-summary-article-number)))
10918     (gnus-run-hooks 'gnus-mark-article-hook)))
10919
10920 (defun gnus-execute-command (command &optional automatic)
10921   (save-excursion
10922     (gnus-article-setup-buffer)
10923     (set-buffer gnus-article-buffer)
10924     (setq buffer-read-only nil)
10925     (let ((command (if automatic command
10926                      (read-string "Command: " (cons command 0)))))
10927       (erase-buffer)
10928       (insert "$ " command "\n\n")
10929       (if gnus-view-pseudo-asynchronously
10930           (start-process "gnus-execute" (current-buffer) shell-file-name
10931                          shell-command-switch command)
10932         (call-process shell-file-name nil t nil
10933                       shell-command-switch command)))))
10934
10935 ;; Summary kill commands.
10936
10937 (defun gnus-summary-edit-global-kill (article)
10938   "Edit the \"global\" kill file."
10939   (interactive (list (gnus-summary-article-number)))
10940   (gnus-group-edit-global-kill article))
10941
10942 (defun gnus-summary-edit-local-kill ()
10943   "Edit a local kill file applied to the current newsgroup."
10944   (interactive)
10945   (setq gnus-current-headers (gnus-summary-article-header))
10946   (gnus-group-edit-local-kill
10947    (gnus-summary-article-number) gnus-newsgroup-name))
10948
10949 ;;; Header reading.
10950
10951 (defun gnus-read-header (id &optional header)
10952   "Read the headers of article ID and enter them into the Gnus system."
10953   (let ((group gnus-newsgroup-name)
10954         (gnus-override-method
10955          (or
10956           gnus-override-method
10957           (and (gnus-news-group-p gnus-newsgroup-name)
10958                (car (gnus-refer-article-methods)))))
10959         where)
10960     ;; First we check to see whether the header in question is already
10961     ;; fetched.
10962     (if (stringp id)
10963         ;; This is a Message-ID.
10964         (setq header (or header (gnus-id-to-header id)))
10965       ;; This is an article number.
10966       (setq header (or header (gnus-summary-article-header id))))
10967     (if (and header
10968              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10969         ;; We have found the header.
10970         header
10971       ;; If this is a sparse article, we have to nix out its
10972       ;; previous entry in the thread hashtb.
10973       (when (and header
10974                  (gnus-summary-article-sparse-p (mail-header-number header)))
10975         (let* ((parent (gnus-parent-id (mail-header-references header)))
10976                (thread (and parent (gnus-id-to-thread parent))))
10977           (when thread
10978             (delq (assq header thread) thread))))
10979       ;; We have to really fetch the header to this article.
10980       (save-excursion
10981         (set-buffer nntp-server-buffer)
10982         (when (setq where (gnus-request-head id group))
10983           (nnheader-fold-continuation-lines)
10984           (goto-char (point-max))
10985           (insert ".\n")
10986           (goto-char (point-min))
10987           (insert "211 ")
10988           (princ (cond
10989                   ((numberp id) id)
10990                   ((cdr where) (cdr where))
10991                   (header (mail-header-number header))
10992                   (t gnus-reffed-article-number))
10993                  (current-buffer))
10994           (insert " Article retrieved.\n"))
10995         (if (or (not where)
10996                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10997             ()                          ; Malformed head.
10998           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10999             (when (and (stringp id)
11000                        (not (string= (gnus-group-real-name group)
11001                                      (car where))))
11002               ;; If we fetched by Message-ID and the article came
11003               ;; from a different group, we fudge some bogus article
11004               ;; numbers for this article.
11005               (mail-header-set-number header gnus-reffed-article-number))
11006             (save-excursion
11007               (set-buffer gnus-summary-buffer)
11008               (decf gnus-reffed-article-number)
11009               (gnus-remove-header (mail-header-number header))
11010               (push header gnus-newsgroup-headers)
11011               (setq gnus-current-headers header)
11012               (push (mail-header-number header) gnus-newsgroup-limit)))
11013           header)))))
11014
11015 (defun gnus-remove-header (number)
11016   "Remove header NUMBER from `gnus-newsgroup-headers'."
11017   (if (and gnus-newsgroup-headers
11018            (= number (mail-header-number (car gnus-newsgroup-headers))))
11019       (pop gnus-newsgroup-headers)
11020     (let ((headers gnus-newsgroup-headers))
11021       (while (and (cdr headers)
11022                   (not (= number (mail-header-number (cadr headers)))))
11023         (pop headers))
11024       (when (cdr headers)
11025         (setcdr headers (cddr headers))))))
11026
11027 ;;;
11028 ;;; summary highlights
11029 ;;;
11030
11031 (defun gnus-highlight-selected-summary ()
11032   "Highlight selected article in summary buffer."
11033   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11034   (when gnus-summary-selected-face
11035     (save-excursion
11036       (let* ((beg (progn (beginning-of-line) (point)))
11037              (end (progn (end-of-line) (point)))
11038              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11039              (from (if (get-text-property beg gnus-mouse-face-prop)
11040                        beg
11041                      (or (next-single-property-change
11042                           beg gnus-mouse-face-prop nil end)
11043                          beg)))
11044              (to
11045               (if (= from end)
11046                   (- from 2)
11047                 (or (next-single-property-change
11048                      from gnus-mouse-face-prop nil end)
11049                     end))))
11050         ;; If no mouse-face prop on line we will have to = from = end,
11051         ;; so we highlight the entire line instead.
11052         (when (= (+ to 2) from)
11053           (setq from beg)
11054           (setq to end))
11055         (if gnus-newsgroup-selected-overlay
11056             ;; Move old overlay.
11057             (gnus-move-overlay
11058              gnus-newsgroup-selected-overlay from to (current-buffer))
11059           ;; Create new overlay.
11060           (gnus-overlay-put
11061            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11062            'face gnus-summary-selected-face))))))
11063
11064 (defvar gnus-summary-highlight-line-cached nil)
11065 (defvar gnus-summary-highlight-line-trigger nil)
11066
11067 (defun gnus-summary-highlight-line-0 ()
11068   (if (and (eq gnus-summary-highlight-line-trigger 
11069                gnus-summary-highlight)
11070            gnus-summary-highlight-line-cached)
11071       gnus-summary-highlight-line-cached
11072     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11073           gnus-summary-highlight-line-cached
11074           (let* ((cond (list 'cond))
11075                  (c cond)
11076                  (list gnus-summary-highlight))
11077             (while list
11078               (setcdr c (cons (list (caar list) (list 'quote (cdar list))) nil))
11079               (setq c (cdr c)
11080                     list (cdr list)))
11081             (gnus-byte-compile (list 'lambda nil cond))))))
11082
11083 (defun gnus-summary-highlight-line ()
11084   "Highlight current line according to `gnus-summary-highlight'."
11085   (let* ((beg (gnus-point-at-bol))
11086          (article (or (gnus-summary-article-number) gnus-current-article))
11087          (score (or (cdr (assq article
11088                                gnus-newsgroup-scored))
11089                     gnus-summary-default-score 0))
11090          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11091          (inhibit-read-only t)
11092          (default gnus-summary-default-score)
11093          (default-high gnus-summary-default-high-score)
11094          (default-low gnus-summary-default-low-score)
11095          (uncached (memq article gnus-newsgroup-undownloaded))
11096          (downloaded (not uncached)))
11097     (let ((face (funcall (gnus-summary-highlight-line-0))))
11098       (unless (eq face (get-text-property beg 'face))
11099         (gnus-put-text-property-excluding-characters-with-faces
11100          beg (gnus-point-at-eol) 'face
11101          (setq face (if (boundp face) (symbol-value face) face)))
11102         (when gnus-summary-highlight-line-function
11103           (funcall gnus-summary-highlight-line-function article face))))))
11104
11105 (defun gnus-update-read-articles (group unread &optional compute)
11106   "Update the list of read articles in GROUP.
11107 UNREAD is a sorted list."
11108   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11109          (entry (gnus-gethash group gnus-newsrc-hashtb))
11110          (info (nth 2 entry))
11111          (prev 1)
11112          read)
11113     (if (or (not info) (not active))
11114         ;; There is no info on this group if it was, in fact,
11115         ;; killed.  Gnus stores no information on killed groups, so
11116         ;; there's nothing to be done.
11117         ;; One could store the information somewhere temporarily,
11118         ;; perhaps...  Hmmm...
11119         ()
11120       ;; Remove any negative articles numbers.
11121       (while (and unread (< (car unread) 0))
11122         (setq unread (cdr unread)))
11123       ;; Remove any expired article numbers
11124       (while (and unread (< (car unread) (car active)))
11125         (setq unread (cdr unread)))
11126       ;; Compute the ranges of read articles by looking at the list of
11127       ;; unread articles.
11128       (while unread
11129         (when (/= (car unread) prev)
11130           (push (if (= prev (1- (car unread))) prev
11131                   (cons prev (1- (car unread))))
11132                 read))
11133         (setq prev (1+ (car unread)))
11134         (setq unread (cdr unread)))
11135       (when (<= prev (cdr active))
11136         (push (cons prev (cdr active)) read))
11137       (setq read (if (> (length read) 1) (nreverse read) read))
11138       (if compute
11139           read
11140         (save-excursion
11141           (let (setmarkundo)
11142             ;; Propagate the read marks to the backend.
11143             (when (gnus-check-backend-function 'request-set-mark group)
11144               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11145                     (add (gnus-remove-from-range read (gnus-info-read info))))
11146                 (when (or add del)
11147                   (unless (gnus-check-group group)
11148                     (error "Can't open server for %s" group))
11149                   (gnus-request-set-mark
11150                    group (delq nil (list (if add (list add 'add '(read)))
11151                                          (if del (list del 'del '(read))))))
11152                   (setq setmarkundo
11153                         `(gnus-request-set-mark
11154                           ,group
11155                           ',(delq nil (list
11156                                        (if del (list del 'add '(read)))
11157                                        (if add (list add 'del '(read))))))))))
11158             (set-buffer gnus-group-buffer)
11159             (gnus-undo-register
11160               `(progn
11161                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11162                  (gnus-info-set-read ',info ',(gnus-info-read info))
11163                  (gnus-get-unread-articles-in-group ',info
11164                                                     (gnus-active ,group))
11165                  (gnus-group-update-group ,group t)
11166                  ,setmarkundo))))
11167         ;; Enter this list into the group info.
11168         (gnus-info-set-read info read)
11169         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11170         (gnus-get-unread-articles-in-group info (gnus-active group))
11171         t))))
11172
11173 (defun gnus-offer-save-summaries ()
11174   "Offer to save all active summary buffers."
11175   (let (buffers)
11176     ;; Go through all buffers and find all summaries.
11177     (dolist (buffer (buffer-list))
11178       (when (and (setq buffer (buffer-name buffer))
11179                  (string-match "Summary" buffer)
11180                  (save-excursion
11181                    (set-buffer buffer)
11182                    ;; We check that this is, indeed, a summary buffer.
11183                    (and (eq major-mode 'gnus-summary-mode)
11184                         ;; Also make sure this isn't bogus.
11185                         gnus-newsgroup-prepared
11186                         ;; Also make sure that this isn't a
11187                         ;; dead summary buffer.
11188                         (not gnus-dead-summary-mode))))
11189         (push buffer buffers)))
11190     ;; Go through all these summary buffers and offer to save them.
11191     (when buffers
11192       (save-excursion
11193         (map-y-or-n-p
11194          "Update summary buffer %s? "
11195          (lambda (buf)
11196            (switch-to-buffer buf)
11197            (gnus-summary-exit))
11198          buffers)))))
11199
11200 (defun gnus-summary-setup-default-charset ()
11201   "Setup newsgroup default charset."
11202   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11203       (setq gnus-newsgroup-charset nil)
11204     (let* ((ignored-charsets
11205             (or gnus-newsgroup-ephemeral-ignored-charsets
11206                 (append
11207                  (and gnus-newsgroup-name
11208                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11209                  gnus-newsgroup-ignored-charsets))))
11210       (setq gnus-newsgroup-charset
11211             (or gnus-newsgroup-ephemeral-charset
11212                 (and gnus-newsgroup-name
11213                      (gnus-parameter-charset gnus-newsgroup-name))
11214                 gnus-default-charset))
11215       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11216            ignored-charsets))))
11217
11218 ;;;
11219 ;;; Mime Commands
11220 ;;;
11221
11222 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11223   "Display the current article buffer fully MIME-buttonized.
11224 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11225 treated as multipart/mixed."
11226   (interactive "P")
11227   (require 'gnus-art)
11228   (let ((gnus-unbuttonized-mime-types nil)
11229         (gnus-mime-display-multipart-as-mixed show-all-parts))
11230     (gnus-summary-show-article)))
11231
11232 (defun gnus-summary-repair-multipart (article)
11233   "Add a Content-Type header to a multipart article without one."
11234   (interactive (list (gnus-summary-article-number)))
11235   (gnus-with-article article
11236     (message-narrow-to-head)
11237     (message-remove-header "Mime-Version")
11238     (goto-char (point-max))
11239     (insert "Mime-Version: 1.0\n")
11240     (widen)
11241     (when (search-forward "\n--" nil t)
11242       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11243         (message-narrow-to-head)
11244         (message-remove-header "Content-Type")
11245         (goto-char (point-max))
11246         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11247                         separator))
11248         (widen))))
11249   (let (gnus-mark-article-hook)
11250     (gnus-summary-select-article t t nil article)))
11251
11252 (defun gnus-summary-toggle-display-buttonized ()
11253   "Toggle the buttonizing of the article buffer."
11254   (interactive)
11255   (require 'gnus-art)
11256   (if (setq gnus-inhibit-mime-unbuttonizing
11257             (not gnus-inhibit-mime-unbuttonizing))
11258       (let ((gnus-unbuttonized-mime-types nil))
11259         (gnus-summary-show-article))
11260     (gnus-summary-show-article)))
11261
11262 ;;;
11263 ;;; Generic summary marking commands
11264 ;;;
11265
11266 (defvar gnus-summary-marking-alist
11267   '((read gnus-del-mark "d")
11268     (unread gnus-unread-mark "u")
11269     (ticked gnus-ticked-mark "!")
11270     (dormant gnus-dormant-mark "?")
11271     (expirable gnus-expirable-mark "e"))
11272   "An alist of names/marks/keystrokes.")
11273
11274 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11275 (defvar gnus-summary-mark-map)
11276
11277 (defun gnus-summary-make-all-marking-commands ()
11278   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11279   (dolist (elem gnus-summary-marking-alist)
11280     (apply 'gnus-summary-make-marking-command elem)))
11281
11282 (defun gnus-summary-make-marking-command (name mark keystroke)
11283   (let ((map (make-sparse-keymap)))
11284     (define-key gnus-summary-generic-mark-map keystroke map)
11285     (dolist (lway `((next "next" next nil "n")
11286                     (next-unread "next unread" next t "N")
11287                     (prev "previous" prev nil "p")
11288                     (prev-unread "previous unread" prev t "P")
11289                     (nomove "" nil nil ,keystroke)))
11290       (let ((func (gnus-summary-make-marking-command-1
11291                    mark (car lway) lway name)))
11292         (setq func (eval func))
11293         (define-key map (nth 4 lway) func)))))
11294
11295 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11296   `(defun ,(intern
11297             (format "gnus-summary-put-mark-as-%s%s"
11298                     name (if (eq way 'nomove)
11299                              ""
11300                            (concat "-" (symbol-name way)))))
11301      (n)
11302      ,(format
11303        "Mark the current article as %s%s.
11304 If N, the prefix, then repeat N times.
11305 If N is negative, move in reverse order.
11306 The difference between N and the actual number of articles marked is
11307 returned."
11308        name (cadr lway))
11309      (interactive "p")
11310      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11311
11312 (defun gnus-summary-generic-mark (n mark move unread)
11313   "Mark N articles with MARK."
11314   (unless (eq major-mode 'gnus-summary-mode)
11315     (error "This command can only be used in the summary buffer"))
11316   (gnus-summary-show-thread)
11317   (let ((nummove
11318          (cond
11319           ((eq move 'next) 1)
11320           ((eq move 'prev) -1)
11321           (t 0))))
11322     (if (zerop nummove)
11323         (setq n 1)
11324       (when (< n 0)
11325         (setq n (abs n)
11326               nummove (* -1 nummove))))
11327     (while (and (> n 0)
11328                 (gnus-summary-mark-article nil mark)
11329                 (zerop (gnus-summary-next-subject nummove unread t)))
11330       (setq n (1- n)))
11331     (when (/= 0 n)
11332       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11333     (gnus-summary-recenter)
11334     (gnus-summary-position-point)
11335     (gnus-set-mode-line 'summary)
11336     n))
11337
11338 (defun gnus-summary-insert-articles (articles)
11339   (when (setq articles
11340               (gnus-sorted-difference articles
11341                                       (mapcar (lambda (h)
11342                                                 (mail-header-number h))
11343                                               gnus-newsgroup-headers)))
11344     (setq gnus-newsgroup-headers
11345           (merge 'list
11346                  gnus-newsgroup-headers
11347                  (gnus-fetch-headers articles)
11348                  'gnus-article-sort-by-number))
11349     ;; Suppress duplicates?
11350     (when gnus-suppress-duplicates
11351       (gnus-dup-suppress-articles))
11352
11353     ;; We might want to build some more threads first.
11354     (when (and gnus-fetch-old-headers
11355                (eq gnus-headers-retrieved-by 'nov))
11356       (if (eq gnus-fetch-old-headers 'invisible)
11357           (gnus-build-all-threads)
11358         (gnus-build-old-threads)))
11359     ;; Let the Gnus agent mark articles as read.
11360     (when gnus-agent
11361       (gnus-agent-get-undownloaded-list))
11362     ;; Remove list identifiers from subject
11363     (when gnus-list-identifiers
11364       (gnus-summary-remove-list-identifiers))
11365     ;; First and last article in this newsgroup.
11366     (when gnus-newsgroup-headers
11367       (setq gnus-newsgroup-begin
11368             (mail-header-number (car gnus-newsgroup-headers))
11369             gnus-newsgroup-end
11370             (mail-header-number
11371              (gnus-last-element gnus-newsgroup-headers))))
11372     (when gnus-use-scoring
11373       (gnus-possibly-score-headers))))
11374
11375 (defun gnus-summary-insert-old-articles (&optional all)
11376   "Insert all old articles in this group.
11377 If ALL is non-nil, already read articles become readable.
11378 If ALL is a number, fetch this number of articles."
11379   (interactive "P")
11380   (prog1
11381       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11382             older len)
11383         (setq older
11384               ;; Some nntp servers lie about their active range.  When
11385               ;; this happens, the active range can be in the millions.
11386               ;; Use a compressed range to avoid creating a huge list.
11387               (gnus-range-difference (list gnus-newsgroup-active) old))
11388         (setq len (gnus-range-length older))
11389         (cond
11390          ((null older) nil)
11391          ((numberp all)
11392           (if (< all len)
11393               (let ((older-range (nreverse older)))
11394                 (setq older nil)
11395
11396                 (while (> all 0)
11397                   (let* ((r (pop older-range))
11398                          (min (if (numberp r) r (car r)))
11399                          (max (if (numberp r) r (cdr r))))
11400                     (while (and (<= min max)
11401                                 (> all 0))
11402                       (push max older)
11403                       (setq all (1- all)
11404                             max (1- max))))))
11405             (setq older (gnus-uncompress-range older))))
11406          (all
11407           (setq older (gnus-uncompress-range older)))
11408          (t
11409           (when (and (numberp gnus-large-newsgroup)
11410                    (> len gnus-large-newsgroup))
11411               (let* ((cursor-in-echo-area nil)
11412                      (initial (gnus-parameter-large-newsgroup-initial
11413                                gnus-newsgroup-name))
11414                      (input
11415                       (read-string
11416                        (format
11417                         "How many articles from %s (%s %d): "
11418                         (gnus-limit-string
11419                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11420                         (if initial "max" "default")
11421                         len)
11422                        (if initial
11423                            (cons (number-to-string initial)
11424                                  0)))))
11425                 (unless (string-match "^[ \t]*$" input)
11426                   (setq all (string-to-number input))
11427                   (if (< all len)
11428                       (let ((older-range (nreverse older)))
11429                         (setq older nil)
11430
11431                         (while (> all 0)
11432                           (let* ((r (pop older-range))
11433                                  (min (if (numberp r) r (car r)))
11434                                  (max (if (numberp r) r (cdr r))))
11435                             (while (and (<= min max)
11436                                         (> all 0))
11437                               (push max older)
11438                               (setq all (1- all)
11439                                     max (1- max))))))))))
11440           (setq older (gnus-uncompress-range older))))
11441         (if (not older)
11442             (message "No old news.")
11443           (gnus-summary-insert-articles older)
11444           (gnus-summary-limit (gnus-sorted-nunion old older))))
11445     (gnus-summary-position-point)))
11446
11447 (defun gnus-summary-insert-new-articles ()
11448   "Insert all new articles in this group."
11449   (interactive)
11450   (prog1
11451       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11452             (old-active gnus-newsgroup-active)
11453             (nnmail-fetched-sources (list t))
11454             i new)
11455         (setq gnus-newsgroup-active
11456               (gnus-activate-group gnus-newsgroup-name 'scan))
11457         (setq i (cdr gnus-newsgroup-active))
11458         (while (> i (cdr old-active))
11459           (push i new)
11460           (decf i))
11461         (if (not new)
11462             (message "No gnus is bad news.")
11463           (gnus-summary-insert-articles new)
11464           (setq gnus-newsgroup-unreads
11465                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11466           (gnus-summary-limit (gnus-sorted-nunion old new))))
11467     (gnus-summary-position-point)))
11468
11469 (gnus-summary-make-all-marking-commands)
11470
11471 (gnus-ems-redefine)
11472
11473 (provide 'gnus-sum)
11474
11475 (run-hooks 'gnus-sum-load-hook)
11476
11477 ;;; gnus-sum.el ends here