T-gnus 6.15.15 revision 00.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-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 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32 (eval-when-compile (require 'gnus-clfns))
33
34 (require 'gnus)
35 (require 'gnus-group)
36 (require 'gnus-spec)
37 (require 'gnus-range)
38 (require 'gnus-int)
39 (require 'gnus-undo)
40 (require 'gnus-util)
41 (require 'nnoo)
42 (require 'mime-view)
43
44 (eval-when-compile
45   (require 'mime-play)
46   (require 'static))
47
48 (eval-and-compile
49   (autoload 'pgg-decrypt-region "pgg" nil t)
50   (autoload 'pgg-verify-region "pgg" nil t))
51
52 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
53 (autoload 'gnus-cache-write-active "gnus-cache")
54 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
55 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
56 (autoload 'mm-uu-dissect "mm-uu")
57 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
58   "Deuglify broken Outlook (Express) articles and redisplay."
59   t)
60 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
61 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
62 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
63
64 (defcustom gnus-kill-summary-on-exit t
65   "*If non-nil, kill the summary buffer when you exit from it.
66 If nil, the summary will become a \"*Dead Summary*\" buffer, and
67 it will be killed sometime later."
68   :group 'gnus-summary-exit
69   :type 'boolean)
70
71 (defcustom gnus-fetch-old-headers nil
72   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
73 If an unread article in the group refers to an older, already read (or
74 just marked as read) article, the old article will not normally be
75 displayed in the Summary buffer.  If this variable is non-nil, Gnus
76 will attempt to grab the headers to the old articles, and thereby
77 build complete threads.  If it has the value `some', only enough
78 headers to connect otherwise loose threads will be displayed.  This
79 variable can also be a number.  In that case, no more than that number
80 of old headers will be fetched.  If it has the value `invisible', all
81 old headers will be fetched, but none will be displayed.
82
83 The server has to support NOV for any of this to work."
84   :group 'gnus-thread
85   :type '(choice (const :tag "off" nil)
86                  (const some)
87                  number
88                  (sexp :menu-tag "other" t)))
89
90 (defcustom gnus-refer-thread-limit 200
91   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
92 If t, fetch all the available old headers."
93   :group 'gnus-thread
94   :type '(choice number
95                  (sexp :menu-tag "other" t)))
96
97 (defcustom gnus-summary-make-false-root 'adopt
98   "*nil means that Gnus won't gather loose threads.
99 If the root of a thread has expired or been read in a previous
100 session, the information necessary to build a complete thread has been
101 lost.  Instead of having many small sub-threads from this original thread
102 scattered all over the summary buffer, Gnus can gather them.
103
104 If non-nil, Gnus will try to gather all loose sub-threads from an
105 original thread into one large thread.
106
107 If this variable is non-nil, it should be one of `none', `adopt',
108 `dummy' or `empty'.
109
110 If this variable is `none', Gnus will not make a false root, but just
111 present the sub-threads after another.
112 If this variable is `dummy', Gnus will create a dummy root that will
113 have all the sub-threads as children.
114 If this variable is `adopt', Gnus will make one of the \"children\"
115 the parent and mark all the step-children as such.
116 If this variable is `empty', the \"children\" are printed with empty
117 subject fields.  (Or rather, they will be printed with a string
118 given by the `gnus-summary-same-subject' variable.)"
119   :group 'gnus-thread
120   :type '(choice (const :tag "off" nil)
121                  (const none)
122                  (const dummy)
123                  (const adopt)
124                  (const empty)))
125
126 (defcustom gnus-summary-make-false-root-always nil
127   "Always make a false dummy root."
128   :group 'gnus-thread
129   :type 'boolean)
130
131 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
132   "*A regexp to match subjects to be excluded from loose thread gathering.
133 As loose thread gathering is done on subjects only, that means that
134 there can be many false gatherings performed.  By rooting out certain
135 common subjects, gathering might become saner."
136   :group 'gnus-thread
137   :type 'regexp)
138
139 (defcustom gnus-summary-gather-subject-limit nil
140   "*Maximum length of subject comparisons when gathering loose threads.
141 Use nil to compare full subjects.  Setting this variable to a low
142 number will help gather threads that have been corrupted by
143 newsreaders chopping off subject lines, but it might also mean that
144 unrelated articles that have subject that happen to begin with the
145 same few characters will be incorrectly gathered.
146
147 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
148 comparing subjects."
149   :group 'gnus-thread
150   :type '(choice (const :tag "off" nil)
151                  (const fuzzy)
152                  (sexp :menu-tag "on" t)))
153
154 (defcustom gnus-simplify-subject-functions nil
155   "List of functions taking a string argument that simplify subjects.
156 The functions are applied recursively.
157
158 Useful functions to put in this list include:
159 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
160 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
161   :group 'gnus-thread
162   :type '(repeat function))
163
164 (defcustom gnus-simplify-ignored-prefixes nil
165   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
166   :group 'gnus-thread
167   :type '(choice (const :tag "off" nil)
168                  regexp))
169
170 (defcustom gnus-build-sparse-threads nil
171   "*If non-nil, fill in the gaps in threads.
172 If `some', only fill in the gaps that are needed to tie loose threads
173 together.  If `more', fill in all leaf nodes that Gnus can find.  If
174 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
175   :group 'gnus-thread
176   :type '(choice (const :tag "off" nil)
177                  (const some)
178                  (const more)
179                  (sexp :menu-tag "all" t)))
180
181 (defcustom gnus-summary-thread-gathering-function
182   'gnus-gather-threads-by-subject
183   "*Function used for gathering loose threads.
184 There are two pre-defined functions: `gnus-gather-threads-by-subject',
185 which only takes Subjects into consideration; and
186 `gnus-gather-threads-by-references', which compared the References
187 headers of the articles to find matches."
188   :group 'gnus-thread
189   :type '(radio (function-item gnus-gather-threads-by-subject)
190                 (function-item gnus-gather-threads-by-references)
191                 (function :tag "other")))
192
193 (defcustom gnus-summary-same-subject ""
194   "*String indicating that the current article has the same subject as the previous.
195 This variable will only be used if the value of
196 `gnus-summary-make-false-root' is `empty'."
197   :group 'gnus-summary-format
198   :type 'string)
199
200 (defcustom gnus-summary-goto-unread t
201   "*If t, many commands will go to the next unread article.
202 This applies to marking commands as well as other commands that
203 \"naturally\" select the next article, like, for instance, `SPC' at
204 the end of an article.
205
206 If nil, the marking commands do NOT go to the next unread article
207 \(they go to the next article instead).  If `never', commands that
208 usually go to the next unread article, will go to the next article,
209 whether it is read or not."
210   :group 'gnus-summary-marks
211   :link '(custom-manual "(gnus)Setting Marks")
212   :type '(choice (const :tag "off" nil)
213                  (const never)
214                  (sexp :menu-tag "on" t)))
215
216 (defcustom gnus-summary-default-score 0
217   "*Default article score level.
218 All scores generated by the score files will be added to this score.
219 If this variable is nil, scoring will be disabled."
220   :group 'gnus-score-default
221   :type '(choice (const :tag "disable")
222                  integer))
223
224 (defcustom gnus-summary-default-high-score 0
225   "*Default threshold for a high scored article.
226 An article will be highlighted as high scored if its score is greater
227 than this score."
228   :group 'gnus-score-default
229   :type 'integer)
230
231 (defcustom gnus-summary-default-low-score 0
232   "*Default threshold for a low scored article.
233 An article will be highlighted as low scored if its score is smaller
234 than this score."
235   :group 'gnus-score-default
236   :type 'integer)
237
238 (defcustom gnus-summary-zcore-fuzz 0
239   "*Fuzziness factor for the zcore in the summary buffer.
240 Articles with scores closer than this to `gnus-summary-default-score'
241 will not be marked."
242   :group 'gnus-summary-format
243   :type 'integer)
244
245 (defcustom gnus-simplify-subject-fuzzy-regexp nil
246   "*Strings to be removed when doing fuzzy matches.
247 This can either be a regular expression or list of regular expressions
248 that will be removed from subject strings if fuzzy subject
249 simplification is selected."
250   :group 'gnus-thread
251   :type '(repeat regexp))
252
253 (defcustom gnus-show-threads t
254   "*If non-nil, display threads in summary mode."
255   :group 'gnus-thread
256   :type 'boolean)
257
258 (defcustom gnus-thread-hide-subtree nil
259   "*If non-nil, hide all threads initially.
260 This can be a predicate specifier which says which threads to hide.
261 If threads are hidden, you have to run the command
262 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
263 to expose hidden threads."
264   :group 'gnus-thread
265   :type 'boolean)
266
267 (defcustom gnus-thread-hide-killed t
268   "*If non-nil, hide killed threads automatically."
269   :group 'gnus-thread
270   :type 'boolean)
271
272 (defcustom gnus-thread-ignore-subject t
273   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
274 If nil, articles that have different subjects from their parents will
275 start separate threads."
276   :group 'gnus-thread
277   :type 'boolean)
278
279 (defcustom gnus-thread-operation-ignore-subject t
280   "*If non-nil, subjects will be ignored when doing thread commands.
281 This affects commands like `gnus-summary-kill-thread' and
282 `gnus-summary-lower-thread'.
283
284 If this variable is nil, articles in the same thread with different
285 subjects will not be included in the operation in question.  If this
286 variable is `fuzzy', only articles that have subjects that are fuzzily
287 equal will be included."
288   :group 'gnus-thread
289   :type '(choice (const :tag "off" nil)
290                  (const fuzzy)
291                  (sexp :tag "on" t)))
292
293 (defcustom gnus-thread-indent-level 4
294   "*Number that says how much each sub-thread should be indented."
295   :group 'gnus-thread
296   :type 'integer)
297
298 (defcustom gnus-auto-extend-newsgroup t
299   "*If non-nil, extend newsgroup forward and backward when requested."
300   :group 'gnus-summary-choose
301   :type 'boolean)
302
303 (defcustom gnus-auto-select-first t
304   "*If non-nil, select the article under point.
305 Which article this is is controlled by the `gnus-auto-select-subject'
306 variable.
307
308 If you want to prevent automatic selection of articles in some
309 newsgroups, set the variable to nil in `gnus-select-group-hook'."
310   :group 'gnus-group-select
311   :type '(choice (const :tag "none" nil)
312                  (sexp :menu-tag "first" t)))
313
314 (defcustom gnus-auto-select-subject 'unread
315   "*Says what subject to place under point when entering a group.
316
317 This variable can either be the symbols `first' (place point on the
318 first subject), `unread' (place point on the subject line of the first
319 unread article), `best' (place point on the subject line of the
320 higest-scored article), `unseen' (place point on the subject line of
321 the first unseen article), 'unseen-or-unread' (place point on the subject
322 line of the first unseen article or, if all article have been seen, on the
323 subject line of the first unread article), or a function to be called to
324 place point on some subject line."
325   :group 'gnus-group-select
326   :type '(choice (const best)
327                  (const unread)
328                  (const first)
329                  (const unseen)
330                  (const unseen-or-unread)))
331
332 (defcustom gnus-dont-select-after-jump-to-other-group nil
333   "If non-nil, don't select the first unread article after entering the
334 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
335 it is depend on the value of `gnus-auto-select-first' whether to select
336 or not."
337   :group 'gnus-group-select
338   :type 'boolean)
339
340 (defcustom gnus-auto-select-next t
341   "*If non-nil, offer to go to the next group from the end of the previous.
342 If the value is t and the next newsgroup is empty, Gnus will exit
343 summary mode and go back to group mode.  If the value is neither nil
344 nor t, Gnus will select the following unread newsgroup.  In
345 particular, if the value is the symbol `quietly', the next unread
346 newsgroup will be selected without any confirmation, and if it is
347 `almost-quietly', the next group will be selected without any
348 confirmation if you are located on the last article in the group.
349 Finally, if this variable is `slightly-quietly', the `Z n' command
350 will go to the next group without confirmation."
351   :group 'gnus-summary-maneuvering
352   :type '(choice (const :tag "off" nil)
353                  (const quietly)
354                  (const almost-quietly)
355                  (const slightly-quietly)
356                  (sexp :menu-tag "on" t)))
357
358 (defcustom gnus-auto-select-same nil
359   "*If non-nil, select the next article with the same subject.
360 If there are no more articles with the same subject, go to
361 the first unread article."
362   :group 'gnus-summary-maneuvering
363   :type 'boolean)
364
365 (defcustom gnus-summary-check-current nil
366   "*If non-nil, consider the current article when moving.
367 The \"unread\" movement commands will stay on the same line if the
368 current article is unread."
369   :group 'gnus-summary-maneuvering
370   :type 'boolean)
371
372 (defcustom gnus-auto-center-summary t
373   "*If non-nil, always center the current summary buffer.
374 In particular, if `vertical' do only vertical recentering.  If non-nil
375 and non-`vertical', do both horizontal and vertical recentering."
376   :group 'gnus-summary-maneuvering
377   :type '(choice (const :tag "none" nil)
378                  (const vertical)
379                  (integer :tag "height")
380                  (sexp :menu-tag "both" t)))
381
382 (defcustom gnus-show-all-headers nil
383   "*If non-nil, don't hide any headers."
384   :group 'gnus-article-hiding
385   :group 'gnus-article-headers
386   :type 'boolean)
387
388 (defcustom gnus-summary-ignore-duplicates nil
389   "*If non-nil, ignore articles with identical Message-ID headers."
390   :group 'gnus-summary
391   :type 'boolean)
392
393 (defcustom gnus-single-article-buffer t
394   "*If non-nil, display all articles in the same buffer.
395 If nil, each group will get its own article buffer."
396   :group 'gnus-article-various
397   :type 'boolean)
398
399 (defcustom gnus-break-pages t
400   "*If non-nil, do page breaking on articles.
401 The page delimiter is specified by the `gnus-page-delimiter'
402 variable."
403   :group 'gnus-article-various
404   :type 'boolean)
405
406 (defcustom gnus-show-mime t
407   "*If non-nil, do mime processing of articles.
408 The articles will simply be fed to the function given by
409 `gnus-article-display-method-for-mime'."
410   :group 'gnus-article-mime
411   :type 'boolean)
412
413 (defcustom gnus-move-split-methods nil
414   "*Variable used to suggest where articles are to be moved to.
415 It uses the same syntax as the `gnus-split-methods' variable.
416 However, whereas `gnus-split-methods' specifies file names as targets,
417 this variable specifies group names."
418   :group 'gnus-summary-mail
419   :type '(repeat (choice (list :value (fun) function)
420                          (cons :value ("" "") regexp (repeat string))
421                          (sexp :value nil))))
422
423 (defcustom gnus-unread-mark ?\ ;;;Whitespace
424   "*Mark used for unread articles."
425   :group 'gnus-summary-marks
426   :type 'character)
427
428 (defcustom gnus-ticked-mark ?!
429   "*Mark used for ticked articles."
430   :group 'gnus-summary-marks
431   :type 'character)
432
433 (defcustom gnus-dormant-mark ??
434   "*Mark used for dormant articles."
435   :group 'gnus-summary-marks
436   :type 'character)
437
438 (defcustom gnus-del-mark ?r
439   "*Mark used for del'd articles."
440   :group 'gnus-summary-marks
441   :type 'character)
442
443 (defcustom gnus-read-mark ?R
444   "*Mark used for read articles."
445   :group 'gnus-summary-marks
446   :type 'character)
447
448 (defcustom gnus-expirable-mark ?E
449   "*Mark used for expirable articles."
450   :group 'gnus-summary-marks
451   :type 'character)
452
453 (defcustom gnus-killed-mark ?K
454   "*Mark used for killed articles."
455   :group 'gnus-summary-marks
456   :type 'character)
457
458 (defcustom gnus-spam-mark ?$
459   "*Mark used for spam articles."
460   :group 'gnus-summary-marks
461   :type 'character)
462
463 (defcustom gnus-souped-mark ?F
464   "*Mark used for souped articles."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-kill-file-mark ?X
469   "*Mark used for articles killed by kill files."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-low-score-mark ?Y
474   "*Mark used for articles with a low score."
475   :group 'gnus-summary-marks
476   :type 'character)
477
478 (defcustom gnus-catchup-mark ?C
479   "*Mark used for articles that are caught up."
480   :group 'gnus-summary-marks
481   :type 'character)
482
483 (defcustom gnus-replied-mark ?A
484   "*Mark used for articles that have been replied to."
485   :group 'gnus-summary-marks
486   :type 'character)
487
488 (defcustom gnus-forwarded-mark ?F
489   "*Mark used for articles that have been forwarded."
490   :group 'gnus-summary-marks
491   :type 'character)
492
493 (defcustom gnus-recent-mark ?N
494   "*Mark used for articles that are recent."
495   :group 'gnus-summary-marks
496   :type 'character)
497
498 (defcustom gnus-cached-mark ?*
499   "*Mark used for articles that are in the cache."
500   :group 'gnus-summary-marks
501   :type 'character)
502
503 (defcustom gnus-saved-mark ?S
504   "*Mark used for articles that have been saved."
505   :group 'gnus-summary-marks
506   :type 'character)
507
508 (defcustom gnus-unseen-mark ?.
509   "*Mark used for articles that haven't been seen."
510   :group 'gnus-summary-marks
511   :type 'character)
512
513 (defcustom gnus-no-mark ?\ ;;;Whitespace
514   "*Mark used for articles that have no other secondary mark."
515   :group 'gnus-summary-marks
516   :type 'character)
517
518 (defcustom gnus-ancient-mark ?O
519   "*Mark used for ancient articles."
520   :group 'gnus-summary-marks
521   :type 'character)
522
523 (defcustom gnus-sparse-mark ?Q
524   "*Mark used for sparsely reffed articles."
525   :group 'gnus-summary-marks
526   :type 'character)
527
528 (defcustom gnus-canceled-mark ?G
529   "*Mark used for canceled articles."
530   :group 'gnus-summary-marks
531   :type 'character)
532
533 (defcustom gnus-duplicate-mark ?M
534   "*Mark used for duplicate articles."
535   :group 'gnus-summary-marks
536   :type 'character)
537
538 (defcustom gnus-undownloaded-mark ?-
539   "*Mark used for articles that weren't downloaded."
540   :group 'gnus-summary-marks
541   :type 'character)
542
543 (defcustom gnus-downloaded-mark ?+
544   "*Mark used for articles that were downloaded."
545   :group 'gnus-summary-marks
546   :type 'character)
547
548 (defcustom gnus-downloadable-mark ?%
549   "*Mark used for articles that are to be downloaded."
550   :group 'gnus-summary-marks
551   :type 'character)
552
553 (defcustom gnus-unsendable-mark ?=
554   "*Mark used for articles that won't be sent."
555   :group 'gnus-summary-marks
556   :type 'character)
557
558 (defcustom gnus-score-over-mark ?+
559   "*Score mark used for articles with high scores."
560   :group 'gnus-summary-marks
561   :type 'character)
562
563 (defcustom gnus-score-below-mark ?-
564   "*Score mark used for articles with low scores."
565   :group 'gnus-summary-marks
566   :type 'character)
567
568 (defcustom gnus-empty-thread-mark ?\ ;;;Whitespace
569   "*There is no thread under the article."
570   :group 'gnus-summary-marks
571   :type 'character)
572
573 (defcustom gnus-not-empty-thread-mark ?=
574   "*There is a thread under the article."
575   :group 'gnus-summary-marks
576   :type 'character)
577
578 (defcustom gnus-view-pseudo-asynchronously nil
579   "*If non-nil, Gnus will view pseudo-articles asynchronously."
580   :group 'gnus-extract-view
581   :type 'boolean)
582
583 (defcustom gnus-auto-expirable-marks
584   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
585         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
586         gnus-souped-mark gnus-duplicate-mark)
587   "*The list of marks converted into expiration if a group is auto-expirable."
588   :version "21.1"
589   :group 'gnus-summary
590   :type '(repeat character))
591
592 (defcustom gnus-inhibit-user-auto-expire t
593   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
594   :version "21.1"
595   :group 'gnus-summary
596   :type 'boolean)
597
598 (defcustom gnus-view-pseudos nil
599   "*If `automatic', pseudo-articles will be viewed automatically.
600 If `not-confirm', pseudos will be viewed automatically, and the user
601 will not be asked to confirm the command."
602   :group 'gnus-extract-view
603   :type '(choice (const :tag "off" nil)
604                  (const automatic)
605                  (const not-confirm)))
606
607 (defcustom gnus-view-pseudos-separately t
608   "*If non-nil, one pseudo-article will be created for each file to be viewed.
609 If nil, all files that use the same viewing command will be given as a
610 list of parameters to that command."
611   :group 'gnus-extract-view
612   :type 'boolean)
613
614 (defcustom gnus-insert-pseudo-articles t
615   "*If non-nil, insert pseudo-articles when decoding articles."
616   :group 'gnus-extract-view
617   :type 'boolean)
618
619 (defcustom gnus-summary-dummy-line-format
620   "   %(:                             :%) %S\n"
621   "*The format specification for the dummy roots in the summary buffer.
622 It works along the same lines as a normal formatting string,
623 with some simple extensions.
624
625 %S  The subject
626
627 General format specifiers can also be used.
628 See `(gnus)Formatting Variables'."
629   :link '(custom-manual "(gnus)Formatting Variables")
630   :group 'gnus-threading
631   :type 'string)
632
633 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
634   "*The format specification for the summary mode line.
635 It works along the same lines as a normal formatting string,
636 with some simple extensions:
637
638 %G  Group name
639 %p  Unprefixed group name
640 %A  Current article number
641 %z  Current article score
642 %V  Gnus version
643 %U  Number of unread articles in the group
644 %e  Number of unselected articles in the group
645 %Z  A string with unread/unselected article counts
646 %g  Shortish group name
647 %S  Subject of the current article
648 %u  User-defined spec
649 %s  Current score file name
650 %d  Number of dormant articles
651 %r  Number of articles that have been marked as read in this session
652 %E  Number of articles expunged by the score files"
653   :group 'gnus-summary-format
654   :type 'string)
655
656 (defcustom gnus-list-identifiers nil
657   "Regexp that matches list identifiers to be removed from subject.
658 This can also be a list of regexps."
659   :version "21.1"
660   :group 'gnus-summary-format
661   :group 'gnus-article-hiding
662   :type '(choice (const :tag "none" nil)
663                  (regexp :value ".*")
664                  (repeat :value (".*") regexp)))
665
666 (defcustom gnus-summary-mark-below 0
667   "*Mark all articles with a score below this variable as read.
668 This variable is local to each summary buffer and usually set by the
669 score file."
670   :group 'gnus-score-default
671   :type 'integer)
672
673 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
674   "*List of functions used for sorting articles in the summary buffer.
675
676 Each function takes two articles and returns non-nil if the first
677 article should be sorted before the other.  If you use more than one
678 function, the primary sort function should be the last.  You should
679 probably always include `gnus-article-sort-by-number' in the list of
680 sorting functions -- preferably first.  Also note that sorting by date
681 is often much slower than sorting by number, and the sorting order is
682 very similar.  (Sorting by date means sorting by the time the message
683 was sent, sorting by number means sorting by arrival time.)
684
685 Ready-made functions include `gnus-article-sort-by-number',
686 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
687 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
688 and `gnus-article-sort-by-score'.
689
690 When threading is turned on, the variable `gnus-thread-sort-functions'
691 controls how articles are sorted."
692   :group 'gnus-summary-sort
693   :type '(repeat (choice (function-item gnus-article-sort-by-number)
694                          (function-item gnus-article-sort-by-author)
695                          (function-item gnus-article-sort-by-subject)
696                          (function-item gnus-article-sort-by-date)
697                          (function-item gnus-article-sort-by-score)
698                          (function-item gnus-article-sort-by-random)
699                          (function :tag "other"))))
700
701 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
702   "*List of functions used for sorting threads in the summary buffer.
703 By default, threads are sorted by article number.
704
705 Each function takes two threads and returns non-nil if the first
706 thread should be sorted before the other.  If you use more than one
707 function, the primary sort function should be the last.  You should
708 probably always include `gnus-thread-sort-by-number' in the list of
709 sorting functions -- preferably first.  Also note that sorting by date
710 is often much slower than sorting by number, and the sorting order is
711 very similar.  (Sorting by date means sorting by the time the message
712 was sent, sorting by number means sorting by arrival time.)
713
714 Ready-made functions include `gnus-thread-sort-by-number',
715 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
716 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
717 `gnus-thread-sort-by-most-recent-number',
718 `gnus-thread-sort-by-most-recent-date',
719 `gnus-thread-sort-by-random', and
720 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
721
722 When threading is turned off, the variable
723 `gnus-article-sort-functions' controls how articles are sorted."
724   :group 'gnus-summary-sort
725   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
726                          (function-item gnus-thread-sort-by-author)
727                          (function-item gnus-thread-sort-by-subject)
728                          (function-item gnus-thread-sort-by-date)
729                          (function-item gnus-thread-sort-by-score)
730                          (function-item gnus-thread-sort-by-total-score)
731                          (function-item gnus-thread-sort-by-random)
732                          (function :tag "other"))))
733
734 (defcustom gnus-thread-score-function '+
735   "*Function used for calculating the total score of a thread.
736
737 The function is called with the scores of the article and each
738 subthread and should then return the score of the thread.
739
740 Some functions you can use are `+', `max', or `min'."
741   :group 'gnus-summary-sort
742   :type 'function)
743
744 (defcustom gnus-summary-expunge-below nil
745   "All articles that have a score less than this variable will be expunged.
746 This variable is local to the summary buffers."
747   :group 'gnus-score-default
748   :type '(choice (const :tag "off" nil)
749                  integer))
750
751 (defcustom gnus-thread-expunge-below nil
752   "All threads that have a total score less than this variable will be expunged.
753 See `gnus-thread-score-function' for en explanation of what a
754 \"thread score\" is.
755
756 This variable is local to the summary buffers."
757   :group 'gnus-threading
758   :group 'gnus-score-default
759   :type '(choice (const :tag "off" nil)
760                  integer))
761
762 (defcustom gnus-summary-mode-hook nil
763   "*A hook for Gnus summary mode.
764 This hook is run before any variables are set in the summary buffer."
765   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
766   :group 'gnus-summary-various
767   :type 'hook)
768
769 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
770 (when (featurep 'xemacs)
771   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
772   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
773   (add-hook 'gnus-summary-mode-hook
774             'gnus-xmas-switch-horizontal-scrollbar-off))
775
776 (defcustom gnus-summary-menu-hook nil
777   "*Hook run after the creation of the summary mode menu."
778   :group 'gnus-summary-visual
779   :type 'hook)
780
781 (defcustom gnus-summary-exit-hook nil
782   "*A hook called on exit from the summary buffer.
783 It will be called with point in the group buffer."
784   :group 'gnus-summary-exit
785   :type 'hook)
786
787 (defcustom gnus-summary-prepare-hook nil
788   "*A hook called after the summary buffer has been generated.
789 If you want to modify the summary buffer, you can use this hook."
790   :group 'gnus-summary-various
791   :type 'hook)
792
793 (defcustom gnus-summary-prepared-hook nil
794   "*A hook called as the last thing after the summary buffer has been generated."
795   :group 'gnus-summary-various
796   :type 'hook)
797
798 (defcustom gnus-summary-generate-hook nil
799   "*A hook run just before generating the summary buffer.
800 This hook is commonly used to customize threading variables and the
801 like."
802   :group 'gnus-summary-various
803   :type 'hook)
804
805 (defcustom gnus-select-group-hook nil
806   "*A hook called when a newsgroup is selected.
807
808 If you'd like to simplify subjects like the
809 `gnus-summary-next-same-subject' command does, you can use the
810 following hook:
811
812  (add-hook gnus-select-group-hook
813            (lambda ()
814              (mapcar (lambda (header)
815                        (mail-header-set-subject
816                         header
817                         (gnus-simplify-subject
818                          (mail-header-subject header) 're-only)))
819                      gnus-newsgroup-headers)))"
820   :group 'gnus-group-select
821   :type 'hook)
822
823 (defcustom gnus-select-article-hook nil
824   "*A hook called when an article is selected."
825   :group 'gnus-summary-choose
826   :options '(gnus-agent-fetch-selected-article)
827   :type 'hook)
828
829 (defcustom gnus-visual-mark-article-hook
830   (list 'gnus-highlight-selected-summary)
831   "*Hook run after selecting an article in the summary buffer.
832 It is meant to be used for highlighting the article in some way.  It
833 is not run if `gnus-visual' is nil."
834   :group 'gnus-summary-visual
835   :type 'hook)
836
837 (defcustom gnus-parse-headers-hook '(gnus-summary-inherit-default-charset)
838   "*A hook called before parsing the headers."
839   :group 'gnus-various
840   :type 'hook)
841
842 (defcustom gnus-exit-group-hook nil
843   "*A hook called when exiting summary mode.
844 This hook is not called from the non-updating exit commands like `Q'."
845   :group 'gnus-various
846   :type 'hook)
847
848 (defcustom gnus-summary-update-hook
849   (list 'gnus-summary-highlight-line)
850   "*A hook called when a summary line is changed.
851 The hook will not be called if `gnus-visual' is nil.
852
853 The default function `gnus-summary-highlight-line' will
854 highlight the line according to the `gnus-summary-highlight'
855 variable."
856   :group 'gnus-summary-visual
857   :type 'hook)
858
859 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
860   "*A hook called when an article is selected for the first time.
861 The hook is intended to mark an article as read (or unread)
862 automatically when it is selected."
863   :group 'gnus-summary-choose
864   :type 'hook)
865
866 (defcustom gnus-group-no-more-groups-hook nil
867   "*A hook run when returning to group mode having no more (unread) groups."
868   :group 'gnus-group-select
869   :type 'hook)
870
871 (defcustom gnus-ps-print-hook nil
872   "*A hook run before ps-printing something from Gnus."
873   :group 'gnus-summary
874   :type 'hook)
875
876 (defcustom gnus-summary-article-move-hook nil
877   "*A hook called after an article is moved, copied, respooled, or crossposted."
878   :group 'gnus-summary
879   :type 'hook)
880
881 (defcustom gnus-summary-article-delete-hook nil
882   "*A hook called after an article is deleted."
883   :group 'gnus-summary
884   :type 'hook)
885
886 (defcustom gnus-summary-article-expire-hook nil
887   "*A hook called after an article is expired."
888   :group 'gnus-summary
889   :type 'hook)
890
891 (defcustom gnus-summary-display-arrow
892   (and (fboundp 'display-graphic-p)
893        (display-graphic-p))
894   "*If non-nil, display an arrow highlighting the current article."
895   :version "21.1"
896   :group 'gnus-summary
897   :type 'boolean)
898
899 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
900   "Face used for highlighting the current article in the summary buffer."
901   :group 'gnus-summary-visual
902   :type 'face)
903
904 (defvar gnus-tmp-downloaded nil)
905
906 (defcustom gnus-summary-highlight
907   '(((eq mark gnus-canceled-mark)
908      . gnus-summary-cancelled-face)
909     ((and uncached (> score default-high))
910      . gnus-summary-high-undownloaded-face)
911     ((and uncached (< score default-low))
912      . gnus-summary-low-undownloaded-face)
913     (uncached
914      . gnus-summary-normal-undownloaded-face)
915     ((and (> score default-high)
916           (or (eq mark gnus-dormant-mark)
917               (eq mark gnus-ticked-mark)))
918      . gnus-summary-high-ticked-face)
919     ((and (< score default-low)
920           (or (eq mark gnus-dormant-mark)
921               (eq mark gnus-ticked-mark)))
922      . gnus-summary-low-ticked-face)
923     ((or (eq mark gnus-dormant-mark)
924          (eq mark gnus-ticked-mark))
925      . gnus-summary-normal-ticked-face)
926     ((and (> score default-high) (eq mark gnus-ancient-mark))
927      . gnus-summary-high-ancient-face)
928     ((and (< score default-low) (eq mark gnus-ancient-mark))
929      . gnus-summary-low-ancient-face)
930     ((eq mark gnus-ancient-mark)
931      . gnus-summary-normal-ancient-face)
932     ((and (> score default-high) (eq mark gnus-unread-mark))
933      . gnus-summary-high-unread-face)
934     ((and (< score default-low) (eq mark gnus-unread-mark))
935      . gnus-summary-low-unread-face)
936     ((eq mark gnus-unread-mark)
937      . gnus-summary-normal-unread-face)
938     ((> score default-high)
939      . gnus-summary-high-read-face)
940     ((< score default-low)
941      . gnus-summary-low-read-face)
942     (t
943      . gnus-summary-normal-read-face))
944   "*Controls the highlighting of summary buffer lines.
945
946 A list of (FORM . FACE) pairs.  When deciding how a a particular
947 summary line should be displayed, each form is evaluated.  The content
948 of the face field after the first true form is used.  You can change
949 how those summary lines are displayed, by editing the face field.
950
951 You can use the following variables in the FORM field.
952
953 score:        The article's score
954 default:      The default article score.
955 default-high: The default score for high scored articles.
956 default-low:  The default score for low scored articles.
957 below:        The score below which articles are automatically marked as read.
958 mark:         The articles mark."
959   :group 'gnus-summary-visual
960   :type '(repeat (cons (sexp :tag "Form" nil)
961                        face)))
962
963 (defcustom gnus-alter-header-function nil
964   "Function called to allow alteration of article header structures.
965 The function is called with one parameter, the article header vector,
966 which it may alter in any way."
967   :type '(choice (const :tag "None" nil)
968                  function)
969   :group 'gnus-summary)
970
971 (defvar gnus-decode-encoded-word-function
972   (mime-find-field-decoder 'From 'nov)
973   "Variable that says which function should be used to decode a string with encoded words.")
974
975 (defcustom gnus-extra-headers '(To Newsgroups)
976   "*Extra headers to parse."
977   :version "21.1"
978   :group 'gnus-summary
979   :type '(repeat symbol))
980
981 (defcustom gnus-ignored-from-addresses
982   (and user-mail-address (regexp-quote user-mail-address))
983   "*Regexp of From headers that may be suppressed in favor of To headers."
984   :version "21.1"
985   :group 'gnus-summary
986   :type 'regexp)
987
988 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
989   "List of charsets that should be ignored.
990 When these charsets are used in the \"charset\" parameter, the
991 default charset will be used instead."
992   :version "21.1"
993   :type '(repeat symbol)
994   :group 'gnus-charset)
995
996 (gnus-define-group-parameter
997  ignored-charsets
998  :type list
999  :function-document
1000  "Return the ignored charsets of GROUP."
1001  :variable gnus-group-ignored-charsets-alist
1002  :variable-default
1003  '(("alt\\.chinese\\.text" iso-8859-1))
1004  :variable-document
1005  "Alist of regexps (to match group names) and charsets that should be ignored.
1006 When these charsets are used in the \"charset\" parameter, the
1007 default charset will be used instead."
1008  :variable-group gnus-charset
1009  :variable-type '(repeat (cons (regexp :tag "Group")
1010                                (repeat symbol)))
1011  :parameter-type '(choice :tag "Ignored charsets"
1012                           :value nil
1013                           (repeat (symbol)))
1014  :parameter-document       "\
1015 List of charsets that should be ignored.
1016
1017 When these charsets are used in the \"charset\" parameter, the
1018 default charset will be used instead.")
1019
1020 (defcustom gnus-group-highlight-words-alist nil
1021   "Alist of group regexps and highlight regexps.
1022 This variable uses the same syntax as `gnus-emphasis-alist'."
1023   :version "21.1"
1024   :type '(repeat (cons (regexp :tag "Group")
1025                        (repeat (list (regexp :tag "Highlight regexp")
1026                                      (number :tag "Group for entire word" 0)
1027                                      (number :tag "Group for displayed part" 0)
1028                                      (symbol :tag "Face"
1029                                              gnus-emphasis-highlight-words)))))
1030   :group 'gnus-summary-visual)
1031
1032 (defcustom gnus-use-wheel nil
1033   "Use Intelli-mouse on summary movement"
1034   :type 'boolean
1035   :group 'gnus-summary-maneuvering)
1036
1037 (defcustom gnus-wheel-scroll-amount '(5 . 1)
1038   "Amount to scroll messages by spinning the mouse wheel.
1039 This is actually a cons cell, where the first item is the amount to scroll
1040 on a normal wheel event, and the second is the amount to scroll when the
1041 wheel is moved with the shift key depressed."
1042   :type '(cons (integer :tag "Shift") integer)
1043   :group 'gnus-summary-maneuvering)
1044
1045 (defcustom gnus-wheel-edge-resistance 2
1046   "How hard it should be to change the current article
1047 by moving the mouse over the edge of the article window."
1048   :type 'integer
1049   :group 'gnus-summary-maneuvering)
1050
1051 (defcustom gnus-summary-show-article-charset-alist
1052   nil
1053   "Alist of number and charset.
1054 The article will be shown with the charset corresponding to the
1055 numbered argument.
1056 For example: ((1 . cn-gb-2312) (2 . big5))."
1057   :version "21.1"
1058   :type '(repeat (cons (number :tag "Argument" 1)
1059                        (symbol :tag "Charset")))
1060   :group 'gnus-charset)
1061
1062 (defcustom gnus-preserve-marks t
1063   "Whether marks are preserved when moving, copying and respooling messages."
1064   :version "21.1"
1065   :type 'boolean
1066   :group 'gnus-summary-marks)
1067
1068 (defcustom gnus-alter-articles-to-read-function nil
1069   "Function to be called to alter the list of articles to be selected."
1070   :type '(choice (const nil) function)
1071   :group 'gnus-summary)
1072
1073 (defcustom gnus-orphan-score nil
1074   "*All orphans get this score added.  Set in the score file."
1075   :group 'gnus-score-default
1076   :type '(choice (const nil)
1077                  integer))
1078
1079 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1080   "*A regexp to match MIME parts when saving multiple parts of a message
1081 with gnus-summary-save-parts (X m). This regexp will be used by default
1082 when prompting the user for which type of files to save."
1083   :group 'gnus-summary
1084   :type 'regexp)
1085
1086 (defcustom gnus-read-all-available-headers nil
1087   "Whether Gnus should parse all headers made available to it.
1088 This is mostly relevant for slow backends where the user may
1089 wish to widen the summary buffer to include all headers
1090 that were fetched.  Say, for nnultimate groups."
1091   :group 'gnus-summary
1092   :type '(choice boolean regexp))
1093
1094 (defcustom gnus-summary-muttprint-program "muttprint"
1095   "Command (and optional arguments) used to run Muttprint."
1096   :version "21.3"
1097   :group 'gnus-summary
1098   :type 'string)
1099
1100 (defcustom gnus-article-loose-mime nil
1101   "If non-nil, don't require MIME-Version header.
1102 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1103 supply the MIME-Version header or deliberately strip it From the mail.
1104 Set it to non-nil, Gnus will treat some articles as MIME even if
1105 the MIME-Version header is missed."
1106   :version "21.3"
1107   :type 'boolean
1108   :group 'gnus-article)
1109
1110 (defcustom gnus-article-emulate-mime t
1111   "If non-nil, use MIME emulation for uuencode and the like.
1112 This means that Gnus will search message bodies for text that look
1113 like uuencoded bits, yEncoded bits, and so on, and present that using
1114 the normal Gnus MIME machinery."
1115   :type 'boolean
1116   :group 'gnus-article)
1117
1118 ;;; Internal variables
1119
1120 (defvar gnus-summary-display-cache nil)
1121 (defvar gnus-article-mime-handles nil)
1122 (defvar gnus-article-decoded-p nil)
1123 (defvar gnus-article-charset nil)
1124 (defvar gnus-article-ignored-charsets nil)
1125 (defvar gnus-scores-exclude-files nil)
1126 (defvar gnus-page-broken nil)
1127
1128 (defvar gnus-original-article nil)
1129 (defvar gnus-article-internal-prepare-hook nil)
1130 (defvar gnus-newsgroup-process-stack nil)
1131
1132 (defvar gnus-thread-indent-array nil)
1133 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1134 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1135   "Function called to sort the articles within a thread after it has been gathered together.")
1136
1137 (defvar gnus-summary-save-parts-type-history nil)
1138 (defvar gnus-summary-save-parts-last-directory nil)
1139
1140 ;; Avoid highlighting in kill files.
1141 (defvar gnus-summary-inhibit-highlight nil)
1142 (defvar gnus-newsgroup-selected-overlay nil)
1143 (defvar gnus-inhibit-limiting nil)
1144 (defvar gnus-newsgroup-adaptive-score-file nil)
1145 (defvar gnus-current-score-file nil)
1146 (defvar gnus-current-move-group nil)
1147 (defvar gnus-current-copy-group nil)
1148 (defvar gnus-current-crosspost-group nil)
1149 (defvar gnus-newsgroup-display nil)
1150
1151 (defvar gnus-newsgroup-dependencies nil)
1152 (defvar gnus-newsgroup-adaptive nil)
1153 (defvar gnus-summary-display-article-function nil)
1154 (defvar gnus-summary-highlight-line-function nil
1155   "Function called after highlighting a summary line.")
1156
1157 (defvar gnus-summary-line-format-alist
1158   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1159     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1160     (?s gnus-tmp-subject-or-nil ?s)
1161     (?n gnus-tmp-name ?s)
1162     (?A (std11-address-string
1163          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1164     (?a (or (std11-full-name-string
1165              (car (mime-entity-read-field gnus-tmp-header 'From)))
1166             gnus-tmp-from) ?s)
1167     (?F gnus-tmp-from ?s)
1168     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1169     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1170     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1171     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1172     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1173     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1174     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1175     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1176     (?L gnus-tmp-lines ?s)
1177     (?O gnus-tmp-downloaded ?c)
1178     (?I gnus-tmp-indentation ?s)
1179     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1180     (?R gnus-tmp-replied ?c)
1181     (?\[ gnus-tmp-opening-bracket ?c)
1182     (?\] gnus-tmp-closing-bracket ?c)
1183     (?\> (make-string gnus-tmp-level ? ) ?s)
1184     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1185     (?i gnus-tmp-score ?d)
1186     (?z gnus-tmp-score-char ?c)
1187     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1188     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1189     (?U gnus-tmp-unread ?c)
1190     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1191         ?s)
1192     (?t (gnus-summary-number-of-articles-in-thread
1193          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1194         ?d)
1195     (?e (gnus-summary-number-of-articles-in-thread
1196          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1197         ?c)
1198     (?u gnus-tmp-user-defined ?s)
1199     (?P (gnus-pick-line-number) ?d)
1200     (?B gnus-tmp-thread-tree-header-string ?s)
1201     (user-date (gnus-user-date
1202                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1203   "An alist of format specifications that can appear in summary lines.
1204 These are paired with what variables they correspond with, along with
1205 the type of the variable (string, integer, character, etc).")
1206
1207 (defvar gnus-summary-dummy-line-format-alist
1208   `((?S gnus-tmp-subject ?s)
1209     (?N gnus-tmp-number ?d)
1210     (?u gnus-tmp-user-defined ?s)))
1211
1212 (defvar gnus-summary-mode-line-format-alist
1213   `((?G gnus-tmp-group-name ?s)
1214     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1215     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1216     (?A gnus-tmp-article-number ?d)
1217     (?Z gnus-tmp-unread-and-unselected ?s)
1218     (?V gnus-version ?s)
1219     (?U gnus-tmp-unread-and-unticked ?d)
1220     (?S gnus-tmp-subject ?s)
1221     (?e gnus-tmp-unselected ?d)
1222     (?u gnus-tmp-user-defined ?s)
1223     (?d (length gnus-newsgroup-dormant) ?d)
1224     (?t (length gnus-newsgroup-marked) ?d)
1225     (?h (length gnus-newsgroup-spam-marked) ?d)
1226     (?r (length gnus-newsgroup-reads) ?d)
1227     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1228     (?E gnus-newsgroup-expunged-tally ?d)
1229     (?s (gnus-current-score-file-nondirectory) ?s)))
1230
1231 (defvar gnus-last-search-regexp nil
1232   "Default regexp for article search command.")
1233
1234 (defvar gnus-summary-search-article-matched-data nil
1235   "Last matched data of article search command.  It is the local variable
1236 in `gnus-article-buffer' which consists of the list of start position,
1237 end position and text.")
1238
1239 (defvar gnus-last-shell-command nil
1240   "Default shell command on article.")
1241
1242 (defvar gnus-newsgroup-agentized nil
1243   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1244 (defvar gnus-newsgroup-begin nil)
1245 (defvar gnus-newsgroup-end nil)
1246 (defvar gnus-newsgroup-last-rmail nil)
1247 (defvar gnus-newsgroup-last-mail nil)
1248 (defvar gnus-newsgroup-last-folder nil)
1249 (defvar gnus-newsgroup-last-file nil)
1250 (defvar gnus-newsgroup-auto-expire nil)
1251 (defvar gnus-newsgroup-active nil)
1252
1253 (defvar gnus-newsgroup-data nil)
1254 (defvar gnus-newsgroup-data-reverse nil)
1255 (defvar gnus-newsgroup-limit nil)
1256 (defvar gnus-newsgroup-limits nil)
1257
1258 (defvar gnus-newsgroup-unreads nil
1259   "Sorted list of unread articles in the current newsgroup.")
1260
1261 (defvar gnus-newsgroup-unselected nil
1262   "Sorted list of unselected unread articles in the current newsgroup.")
1263
1264 (defvar gnus-newsgroup-reads nil
1265   "Alist of read articles and article marks in the current newsgroup.")
1266
1267 (defvar gnus-newsgroup-expunged-tally nil)
1268
1269 (defvar gnus-newsgroup-marked nil
1270   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1271
1272 (defvar gnus-newsgroup-spam-marked nil
1273   "List of ranges of articles that have been marked as spam.")
1274
1275 (defvar gnus-newsgroup-killed nil
1276   "List of ranges of articles that have been through the scoring process.")
1277
1278 (defvar gnus-newsgroup-cached nil
1279   "Sorted list of articles that come from the article cache.")
1280
1281 (defvar gnus-newsgroup-saved nil
1282   "List of articles that have been saved.")
1283
1284 (defvar gnus-newsgroup-kill-headers nil)
1285
1286 (defvar gnus-newsgroup-replied nil
1287   "List of articles that have been replied to in the current newsgroup.")
1288
1289 (defvar gnus-newsgroup-forwarded nil
1290   "List of articles that have been forwarded in the current newsgroup.")
1291
1292 (defvar gnus-newsgroup-recent nil
1293   "List of articles that have are recent in the current newsgroup.")
1294
1295 (defvar gnus-newsgroup-expirable nil
1296   "Sorted list of articles in the current newsgroup that can be expired.")
1297
1298 (defvar gnus-newsgroup-processable nil
1299   "List of articles in the current newsgroup that can be processed.")
1300
1301 (defvar gnus-newsgroup-downloadable nil
1302   "Sorted list of articles in the current newsgroup that can be processed.")
1303
1304 (defvar gnus-newsgroup-unfetched nil
1305   "Sorted list of articles in the current newsgroup whose headers have not been fetched into the agent.")
1306
1307 (defvar gnus-newsgroup-undownloaded nil
1308   "List of articles in the current newsgroup that haven't been downloaded..")
1309
1310 (defvar gnus-newsgroup-unsendable nil
1311   "List of articles in the current newsgroup that won't be sent.")
1312
1313 (defvar gnus-newsgroup-bookmarks nil
1314   "List of articles in the current newsgroup that have bookmarks.")
1315
1316 (defvar gnus-newsgroup-dormant nil
1317   "Sorted list of dormant articles in the current newsgroup.")
1318
1319 (defvar gnus-newsgroup-unseen nil
1320   "List of unseen articles in the current newsgroup.")
1321
1322 (defvar gnus-newsgroup-seen nil
1323   "Range of seen articles in the current newsgroup.")
1324
1325 (defvar gnus-newsgroup-articles nil
1326   "List of articles in the current newsgroup.")
1327
1328 (defvar gnus-newsgroup-scored nil
1329   "List of scored articles in the current newsgroup.")
1330
1331 (defvar gnus-newsgroup-incorporated nil
1332   "List of incorporated articles in the current newsgroup.")
1333
1334 (defvar gnus-newsgroup-headers nil
1335   "List of article headers in the current newsgroup.")
1336
1337 (defvar gnus-newsgroup-threads nil)
1338
1339 (defvar gnus-newsgroup-prepared nil
1340   "Whether the current group has been prepared properly.")
1341
1342 (defvar gnus-newsgroup-ancient nil
1343   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1344
1345 (defvar gnus-newsgroup-sparse nil)
1346
1347 (defvar gnus-current-article nil)
1348 (defvar gnus-article-current nil)
1349 (defvar gnus-current-headers nil)
1350 (defvar gnus-have-all-headers nil)
1351 (defvar gnus-last-article nil)
1352 (defvar gnus-newsgroup-history nil)
1353 (defvar gnus-newsgroup-charset nil)
1354 (defvar gnus-newsgroup-ephemeral-charset nil)
1355 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1356
1357 (defvar gnus-article-before-search nil)
1358
1359 (defconst gnus-summary-local-variables
1360   '(gnus-newsgroup-name
1361     gnus-newsgroup-begin gnus-newsgroup-end
1362     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1363     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1364     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1365     gnus-newsgroup-unselected gnus-newsgroup-marked
1366     gnus-newsgroup-spam-marked
1367     gnus-newsgroup-reads gnus-newsgroup-saved
1368     gnus-newsgroup-replied gnus-newsgroup-forwarded
1369     gnus-newsgroup-recent
1370     gnus-newsgroup-expirable
1371     gnus-newsgroup-processable gnus-newsgroup-killed
1372     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1373     gnus-newsgroup-unfetched
1374     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1375     gnus-newsgroup-seen gnus-newsgroup-articles
1376     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1377     gnus-newsgroup-headers gnus-newsgroup-threads
1378     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1379     gnus-current-article gnus-current-headers gnus-have-all-headers
1380     gnus-last-article gnus-article-internal-prepare-hook
1381     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1382     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1383     gnus-thread-expunge-below
1384     gnus-score-alist gnus-current-score-file
1385     (gnus-summary-expunge-below . global)
1386     (gnus-summary-mark-below . global)
1387     (gnus-orphan-score . global)
1388     gnus-newsgroup-active gnus-scores-exclude-files
1389     gnus-newsgroup-history gnus-newsgroup-ancient
1390     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1391     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1392     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1393     (gnus-newsgroup-expunged-tally . 0)
1394     gnus-cache-removable-articles gnus-newsgroup-cached
1395     gnus-newsgroup-data gnus-newsgroup-data-reverse
1396     gnus-newsgroup-limit gnus-newsgroup-limits
1397     gnus-newsgroup-charset gnus-newsgroup-display
1398     gnus-newsgroup-incorporated)
1399   "Variables that are buffer-local to the summary buffers.")
1400
1401 (defvar gnus-newsgroup-variables nil
1402   "A list of variables that have separate values in different newsgroups.
1403 A list of newsgroup (summary buffer) local variables, or cons of
1404 variables and their default values (when the default values are not
1405 nil), that should be made global while the summary buffer is active.
1406 These variables can be used to set variables in the group parameters
1407 while still allowing them to affect operations done in other
1408 buffers. For example:
1409
1410 \(setq gnus-newsgroup-variables
1411      '(message-use-followup-to
1412        (gnus-visible-headers .
1413          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1414 ")
1415
1416 ;; Byte-compiler warning.
1417 ;(eval-when-compile (defvar gnus-article-mode-map))
1418 (eval-when-compile
1419   (let ((features (cons 'gnus-sum features)))
1420     (require 'gnus)
1421     (require 'gnus-agent)
1422     (require 'gnus-art)))
1423
1424 ;; Subject simplification.
1425
1426 (defun gnus-simplify-whitespace (str)
1427   "Remove excessive whitespace from STR."
1428   ;; Multiple spaces.
1429   (while (string-match "[ \t][ \t]+" str)
1430     (setq str (concat (substring str 0 (match-beginning 0))
1431                         " "
1432                         (substring str (match-end 0)))))
1433   ;; Leading spaces.
1434   (when (string-match "^[ \t]+" str)
1435     (setq str (substring str (match-end 0))))
1436   ;; Trailing spaces.
1437   (when (string-match "[ \t]+$" str)
1438     (setq str (substring str 0 (match-beginning 0))))
1439   str)
1440
1441 (defun gnus-simplify-all-whitespace (str)
1442   "Remove all whitespace from STR."
1443   (while (string-match "[ \t\n]+" str)
1444     (setq str (replace-match "" nil nil str)))
1445   str)
1446
1447 (defsubst gnus-simplify-subject-re (subject)
1448   "Remove \"Re:\" from subject lines."
1449   (if (string-match message-subject-re-regexp subject)
1450       (substring subject (match-end 0))
1451     subject))
1452
1453 (defun gnus-simplify-subject (subject &optional re-only)
1454   "Remove `Re:' and words in parentheses.
1455 If RE-ONLY is non-nil, strip leading `Re:'s only."
1456   (let ((case-fold-search t))           ;Ignore case.
1457     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1458     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1459       (setq subject (substring subject (match-end 0))))
1460     ;; Remove uninteresting prefixes.
1461     (when (and (not re-only)
1462                gnus-simplify-ignored-prefixes
1463                (string-match gnus-simplify-ignored-prefixes subject))
1464       (setq subject (substring subject (match-end 0))))
1465     ;; Remove words in parentheses from end.
1466     (unless re-only
1467       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1468         (setq subject (substring subject 0 (match-beginning 0)))))
1469     ;; Return subject string.
1470     subject))
1471
1472 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1473 ;; all whitespace.
1474 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1475   (goto-char (point-min))
1476   (while (re-search-forward regexp nil t)
1477     (replace-match (or newtext ""))))
1478
1479 (defun gnus-simplify-buffer-fuzzy ()
1480   "Simplify string in the buffer fuzzily.
1481 The string in the accessible portion of the current buffer is simplified.
1482 It is assumed to be a single-line subject.
1483 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1484 matter is removed.  Additional things can be deleted by setting
1485 `gnus-simplify-subject-fuzzy-regexp'."
1486   (let ((case-fold-search t)
1487         (modified-tick))
1488     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1489
1490     (while (not (eq modified-tick (buffer-modified-tick)))
1491       (setq modified-tick (buffer-modified-tick))
1492       (cond
1493        ((listp gnus-simplify-subject-fuzzy-regexp)
1494         (mapcar 'gnus-simplify-buffer-fuzzy-step
1495                 gnus-simplify-subject-fuzzy-regexp))
1496        (gnus-simplify-subject-fuzzy-regexp
1497         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1498       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1499       (gnus-simplify-buffer-fuzzy-step
1500        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1501       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1502
1503     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1504     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1505     (gnus-simplify-buffer-fuzzy-step " $")
1506     (gnus-simplify-buffer-fuzzy-step "^ +")))
1507
1508 (defun gnus-simplify-subject-fuzzy (subject)
1509   "Simplify a subject string fuzzily.
1510 See `gnus-simplify-buffer-fuzzy' for details."
1511   (save-excursion
1512     (gnus-set-work-buffer)
1513     (let ((case-fold-search t))
1514       ;; Remove uninteresting prefixes.
1515       (when (and gnus-simplify-ignored-prefixes
1516                  (string-match gnus-simplify-ignored-prefixes subject))
1517         (setq subject (substring subject (match-end 0))))
1518       (insert subject)
1519       (inline (gnus-simplify-buffer-fuzzy))
1520       (buffer-string))))
1521
1522 (defsubst gnus-simplify-subject-fully (subject)
1523   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1524   (cond
1525    (gnus-simplify-subject-functions
1526     (gnus-map-function gnus-simplify-subject-functions subject))
1527    ((null gnus-summary-gather-subject-limit)
1528     (gnus-simplify-subject-re subject))
1529    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1530     (gnus-simplify-subject-fuzzy subject))
1531    ((numberp gnus-summary-gather-subject-limit)
1532     (gnus-limit-string (gnus-simplify-subject-re subject)
1533                        gnus-summary-gather-subject-limit))
1534    (t
1535     subject)))
1536
1537 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1538   "Check whether two subjects are equal.
1539 If optional argument simple-first is t, first argument is already
1540 simplified."
1541   (cond
1542    ((null simple-first)
1543     (equal (gnus-simplify-subject-fully s1)
1544            (gnus-simplify-subject-fully s2)))
1545    (t
1546     (equal s1
1547            (gnus-simplify-subject-fully s2)))))
1548
1549 (defun gnus-summary-bubble-group ()
1550   "Increase the score of the current group.
1551 This is a handy function to add to `gnus-summary-exit-hook' to
1552 increase the score of each group you read."
1553   (gnus-group-add-score gnus-newsgroup-name))
1554
1555 \f
1556 ;;;
1557 ;;; Gnus summary mode
1558 ;;;
1559
1560 (put 'gnus-summary-mode 'mode-class 'special)
1561
1562 (defvar gnus-article-commands-menu)
1563
1564 ;; Non-orthogonal keys
1565
1566 (gnus-define-keys gnus-summary-mode-map
1567   " " gnus-summary-next-page
1568   "\177" gnus-summary-prev-page
1569   [delete] gnus-summary-prev-page
1570   [backspace] gnus-summary-prev-page
1571   "\r" gnus-summary-scroll-up
1572   "\M-\r" gnus-summary-scroll-down
1573   "n" gnus-summary-next-unread-article
1574   "p" gnus-summary-prev-unread-article
1575   "N" gnus-summary-next-article
1576   "P" gnus-summary-prev-article
1577   "\M-\C-n" gnus-summary-next-same-subject
1578   "\M-\C-p" gnus-summary-prev-same-subject
1579   "\M-n" gnus-summary-next-unread-subject
1580   "\M-p" gnus-summary-prev-unread-subject
1581   "." gnus-summary-first-unread-article
1582   "," gnus-summary-best-unread-article
1583   "\M-s" gnus-summary-search-article-forward
1584   "\M-r" gnus-summary-search-article-backward
1585   "<" gnus-summary-beginning-of-article
1586   ">" gnus-summary-end-of-article
1587   "j" gnus-summary-goto-article
1588   "^" gnus-summary-refer-parent-article
1589   "\M-^" gnus-summary-refer-article
1590   "u" gnus-summary-tick-article-forward
1591   "!" gnus-summary-tick-article-forward
1592   "U" gnus-summary-tick-article-backward
1593   "d" gnus-summary-mark-as-read-forward
1594   "D" gnus-summary-mark-as-read-backward
1595   "E" gnus-summary-mark-as-expirable
1596   "\M-u" gnus-summary-clear-mark-forward
1597   "\M-U" gnus-summary-clear-mark-backward
1598   "k" gnus-summary-kill-same-subject-and-select
1599   "\C-k" gnus-summary-kill-same-subject
1600   "\M-\C-k" gnus-summary-kill-thread
1601   "\M-\C-l" gnus-summary-lower-thread
1602   "e" gnus-summary-edit-article
1603   "#" gnus-summary-mark-as-processable
1604   "\M-#" gnus-summary-unmark-as-processable
1605   "\M-\C-t" gnus-summary-toggle-threads
1606   "\M-\C-s" gnus-summary-show-thread
1607   "\M-\C-h" gnus-summary-hide-thread
1608   "\M-\C-f" gnus-summary-next-thread
1609   "\M-\C-b" gnus-summary-prev-thread
1610   [(meta down)] gnus-summary-next-thread
1611   [(meta up)] gnus-summary-prev-thread
1612   "\M-\C-u" gnus-summary-up-thread
1613   "\M-\C-d" gnus-summary-down-thread
1614   "&" gnus-summary-execute-command
1615   "c" gnus-summary-catchup-and-exit
1616   "\C-w" gnus-summary-mark-region-as-read
1617   "\C-t" gnus-summary-toggle-truncation
1618   "?" gnus-summary-mark-as-dormant
1619   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1620   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1621   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1622   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1623   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1624   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1625   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1626   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1627   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1628   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1629   "=" gnus-summary-expand-window
1630   "\C-x\C-s" gnus-summary-reselect-current-group
1631   "\M-g" gnus-summary-rescan-group
1632   "w" gnus-summary-stop-page-breaking
1633   "\C-c\C-r" gnus-summary-caesar-message
1634   "\M-t" gnus-summary-toggle-mime
1635   "f" gnus-summary-followup
1636   "F" gnus-summary-followup-with-original
1637   "C" gnus-summary-cancel-article
1638   "r" gnus-summary-reply
1639   "R" gnus-summary-reply-with-original
1640   "\C-c\C-f" gnus-summary-mail-forward
1641   "o" gnus-summary-save-article
1642   "\C-o" gnus-summary-save-article-mail
1643   "|" gnus-summary-pipe-output
1644   "\M-k" gnus-summary-edit-local-kill
1645   "\M-K" gnus-summary-edit-global-kill
1646   ;; "V" gnus-version
1647   "\C-c\C-d" gnus-summary-describe-group
1648   "q" gnus-summary-exit
1649   "Q" gnus-summary-exit-no-update
1650   "\C-c\C-i" gnus-info-find-node
1651   gnus-mouse-2 gnus-mouse-pick-article
1652   "m" gnus-summary-mail-other-window
1653   "a" gnus-summary-post-news
1654   "i" gnus-summary-news-other-window
1655   "x" gnus-summary-limit-to-unread
1656   "s" gnus-summary-isearch-article
1657   "t" gnus-summary-toggle-header
1658   "g" gnus-summary-show-article
1659   "l" gnus-summary-goto-last-article
1660   "v" gnus-summary-preview-mime-message
1661   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1662   "\C-d" gnus-summary-enter-digest-group
1663   "\M-\C-d" gnus-summary-read-document
1664   "\M-\C-e" gnus-summary-edit-parameters
1665   "\M-\C-a" gnus-summary-customize-parameters
1666   "\C-c\C-b" gnus-bug
1667   "\C-c\C-n" gnus-namazu-search
1668   "*" gnus-cache-enter-article
1669   "\M-*" gnus-cache-remove-article
1670   "\M-&" gnus-summary-universal-argument
1671   "\C-l" gnus-recenter
1672   "I" gnus-summary-increase-score
1673   "L" gnus-summary-lower-score
1674   "\M-i" gnus-symbolic-argument
1675   "h" gnus-summary-select-article-buffer
1676
1677   "V" gnus-summary-score-map
1678   "X" gnus-uu-extract-map
1679   "S" gnus-summary-send-map)
1680
1681   ;; Sort of orthogonal keymap
1682 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1683   "t" gnus-summary-tick-article-forward
1684   "!" gnus-summary-tick-article-forward
1685   "d" gnus-summary-mark-as-read-forward
1686   "r" gnus-summary-mark-as-read-forward
1687   "c" gnus-summary-clear-mark-forward
1688   " " gnus-summary-clear-mark-forward
1689   "e" gnus-summary-mark-as-expirable
1690   "x" gnus-summary-mark-as-expirable
1691   "?" gnus-summary-mark-as-dormant
1692   "b" gnus-summary-set-bookmark
1693   "B" gnus-summary-remove-bookmark
1694   "#" gnus-summary-mark-as-processable
1695   "\M-#" gnus-summary-unmark-as-processable
1696   "S" gnus-summary-limit-include-expunged
1697   "C" gnus-summary-catchup
1698   "H" gnus-summary-catchup-to-here
1699   "h" gnus-summary-catchup-from-here
1700   "\C-c" gnus-summary-catchup-all
1701   "k" gnus-summary-kill-same-subject-and-select
1702   "K" gnus-summary-kill-same-subject
1703   "P" gnus-uu-mark-map)
1704
1705 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1706   "c" gnus-summary-clear-above
1707   "u" gnus-summary-tick-above
1708   "m" gnus-summary-mark-above
1709   "k" gnus-summary-kill-below)
1710
1711 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1712   "/" gnus-summary-limit-to-subject
1713   "n" gnus-summary-limit-to-articles
1714   "w" gnus-summary-pop-limit
1715   "s" gnus-summary-limit-to-subject
1716   "a" gnus-summary-limit-to-author
1717   "u" gnus-summary-limit-to-unread
1718   "m" gnus-summary-limit-to-marks
1719   "M" gnus-summary-limit-exclude-marks
1720   "v" gnus-summary-limit-to-score
1721   "*" gnus-summary-limit-include-cached
1722   "D" gnus-summary-limit-include-dormant
1723   "T" gnus-summary-limit-include-thread
1724   "d" gnus-summary-limit-exclude-dormant
1725   "t" gnus-summary-limit-to-age
1726   "." gnus-summary-limit-to-unseen
1727   "x" gnus-summary-limit-to-extra
1728   "p" gnus-summary-limit-to-display-predicate
1729   "E" gnus-summary-limit-include-expunged
1730   "c" gnus-summary-limit-exclude-childless-dormant
1731   "C" gnus-summary-limit-mark-excluded-as-read
1732   "o" gnus-summary-insert-old-articles
1733   "N" gnus-summary-insert-new-articles)
1734
1735 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1736   "n" gnus-summary-next-unread-article
1737   "p" gnus-summary-prev-unread-article
1738   "N" gnus-summary-next-article
1739   "P" gnus-summary-prev-article
1740   "\C-n" gnus-summary-next-same-subject
1741   "\C-p" gnus-summary-prev-same-subject
1742   "\M-n" gnus-summary-next-unread-subject
1743   "\M-p" gnus-summary-prev-unread-subject
1744   "f" gnus-summary-first-unread-article
1745   "b" gnus-summary-best-unread-article
1746   "j" gnus-summary-goto-article
1747   "g" gnus-summary-goto-subject
1748   "l" gnus-summary-goto-last-article
1749   "o" gnus-summary-pop-article)
1750
1751 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1752   "k" gnus-summary-kill-thread
1753   "l" gnus-summary-lower-thread
1754   "i" gnus-summary-raise-thread
1755   "T" gnus-summary-toggle-threads
1756   "t" gnus-summary-rethread-current
1757   "^" gnus-summary-reparent-thread
1758   "s" gnus-summary-show-thread
1759   "S" gnus-summary-show-all-threads
1760   "h" gnus-summary-hide-thread
1761   "H" gnus-summary-hide-all-threads
1762   "n" gnus-summary-next-thread
1763   "p" gnus-summary-prev-thread
1764   "u" gnus-summary-up-thread
1765   "o" gnus-summary-top-thread
1766   "d" gnus-summary-down-thread
1767   "#" gnus-uu-mark-thread
1768   "\M-#" gnus-uu-unmark-thread)
1769
1770 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1771   "g" gnus-summary-prepare
1772   "c" gnus-summary-insert-cached-articles
1773   "d" gnus-summary-insert-dormant-articles)
1774
1775 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1776   "c" gnus-summary-catchup-and-exit
1777   "C" gnus-summary-catchup-all-and-exit
1778   "E" gnus-summary-exit-no-update
1779   "J" gnus-summary-jump-to-other-group
1780   "Q" gnus-summary-exit
1781   "Z" gnus-summary-exit
1782   "n" gnus-summary-catchup-and-goto-next-group
1783   "R" gnus-summary-reselect-current-group
1784   "G" gnus-summary-rescan-group
1785   "N" gnus-summary-next-group
1786   "s" gnus-summary-save-newsrc
1787   "P" gnus-summary-prev-group)
1788
1789 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1790   " " gnus-summary-next-page
1791   "n" gnus-summary-next-page
1792   "\177" gnus-summary-prev-page
1793   [delete] gnus-summary-prev-page
1794   "p" gnus-summary-prev-page
1795   "\r" gnus-summary-scroll-up
1796   "\M-\r" gnus-summary-scroll-down
1797   "<" gnus-summary-beginning-of-article
1798   ">" gnus-summary-end-of-article
1799   "b" gnus-summary-beginning-of-article
1800   "e" gnus-summary-end-of-article
1801   "^" gnus-summary-refer-parent-article
1802   "r" gnus-summary-refer-parent-article
1803   "D" gnus-summary-enter-digest-group
1804   "R" gnus-summary-refer-references
1805   "T" gnus-summary-refer-thread
1806   "g" gnus-summary-show-article
1807   "s" gnus-summary-isearch-article
1808   "P" gnus-summary-print-article
1809   "M" gnus-mailing-list-insinuate
1810   "t" gnus-article-babel)
1811
1812 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1813   "b" gnus-article-add-buttons
1814   "B" gnus-article-add-buttons-to-head
1815   "o" gnus-article-treat-overstrike
1816   "e" gnus-article-emphasize
1817   "w" gnus-article-fill-cited-article
1818   "Q" gnus-article-fill-long-lines
1819   "C" gnus-article-capitalize-sentences
1820   "c" gnus-article-remove-cr
1821   "Z" gnus-article-decode-HZ
1822   "h" gnus-article-wash-html
1823   "u" gnus-article-unsplit-urls
1824   "f" gnus-article-display-x-face
1825   "l" gnus-summary-stop-page-breaking
1826   "r" gnus-summary-caesar-message
1827   "m" gnus-summary-morse-message
1828   "t" gnus-summary-toggle-header
1829   "g" gnus-treat-smiley
1830   "v" gnus-summary-verbose-headers
1831   "m" gnus-summary-toggle-mime
1832   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1833   "p" gnus-article-verify-x-pgp-sig
1834   "d" gnus-article-treat-dumbquotes)
1835
1836 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1837   ;; mnemonic: deuglif*Y*
1838   "u" gnus-article-outlook-unwrap-lines
1839   "a" gnus-article-outlook-repair-attribution
1840   "c" gnus-article-outlook-rearrange-citation
1841   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1842
1843 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1844   "a" gnus-article-hide
1845   "h" gnus-article-hide-headers
1846   "b" gnus-article-hide-boring-headers
1847   "s" gnus-article-hide-signature
1848   "c" gnus-article-hide-citation
1849   "C" gnus-article-hide-citation-in-followups
1850   "l" gnus-article-hide-list-identifiers
1851   "B" gnus-article-strip-banner
1852   "P" gnus-article-hide-pem
1853   "\C-c" gnus-article-hide-citation-maybe)
1854
1855 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1856   "a" gnus-article-highlight
1857   "h" gnus-article-highlight-headers
1858   "c" gnus-article-highlight-citation
1859   "s" gnus-article-highlight-signature)
1860
1861 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1862   "f" gnus-article-treat-fold-headers
1863   "u" gnus-article-treat-unfold-headers
1864   "n" gnus-article-treat-fold-newsgroups)
1865
1866 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1867   "x" gnus-article-display-x-face
1868   "d" gnus-article-display-face
1869   "s" gnus-treat-smiley
1870   "D" gnus-article-remove-images
1871   "f" gnus-treat-from-picon
1872   "m" gnus-treat-mail-picon
1873   "n" gnus-treat-newsgroups-picon)
1874
1875 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1876   "z" gnus-article-date-ut
1877   "u" gnus-article-date-ut
1878   "l" gnus-article-date-local
1879   "p" gnus-article-date-english
1880   "e" gnus-article-date-lapsed
1881   "o" gnus-article-date-original
1882   "i" gnus-article-date-iso8601
1883   "s" gnus-article-date-user)
1884
1885 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1886   "t" gnus-article-remove-trailing-blank-lines
1887   "l" gnus-article-strip-leading-blank-lines
1888   "m" gnus-article-strip-multiple-blank-lines
1889   "a" gnus-article-strip-blank-lines
1890   "A" gnus-article-strip-all-blank-lines
1891   "s" gnus-article-strip-leading-space
1892   "e" gnus-article-strip-trailing-space
1893   "w" gnus-article-remove-leading-whitespace)
1894
1895 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1896   "v" gnus-version
1897   "f" gnus-summary-fetch-faq
1898   "d" gnus-summary-describe-group
1899   "h" gnus-summary-describe-briefly
1900   "i" gnus-info-find-node
1901   "c" gnus-group-fetch-charter
1902   "C" gnus-group-fetch-control)
1903
1904 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1905   "e" gnus-summary-expire-articles
1906   "\M-\C-e" gnus-summary-expire-articles-now
1907   "\177" gnus-summary-delete-article
1908   [delete] gnus-summary-delete-article
1909   [backspace] gnus-summary-delete-article
1910   "m" gnus-summary-move-article
1911   "r" gnus-summary-respool-article
1912   "w" gnus-summary-edit-article
1913   "c" gnus-summary-copy-article
1914   "B" gnus-summary-crosspost-article
1915   "q" gnus-summary-respool-query
1916   "t" gnus-summary-respool-trace
1917   "i" gnus-summary-import-article
1918   "I" gnus-summary-create-article
1919   "p" gnus-summary-article-posted-p)
1920
1921 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1922   "o" gnus-summary-save-article
1923   "m" gnus-summary-save-article-mail
1924   "F" gnus-summary-write-article-file
1925   "r" gnus-summary-save-article-rmail
1926   "f" gnus-summary-save-article-file
1927   "b" gnus-summary-save-article-body-file
1928   "h" gnus-summary-save-article-folder
1929   "v" gnus-summary-save-article-vm
1930   "p" gnus-summary-pipe-output
1931   "P" gnus-summary-muttprint
1932   "s" gnus-soup-add-article)
1933
1934 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1935   "b" gnus-summary-display-buttonized
1936   "m" gnus-summary-repair-multipart
1937   "v" gnus-article-view-part
1938   "o" gnus-article-save-part
1939   "c" gnus-article-copy-part
1940   "C" gnus-article-view-part-as-charset
1941   "e" gnus-article-view-part-externally
1942   "E" gnus-article-encrypt-body
1943   "i" gnus-article-inline-part
1944   "|" gnus-article-pipe-part)
1945
1946 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1947   "p" gnus-summary-mark-as-processable
1948   "u" gnus-summary-unmark-as-processable
1949   "U" gnus-summary-unmark-all-processable
1950   "v" gnus-uu-mark-over
1951   "s" gnus-uu-mark-series
1952   "r" gnus-uu-mark-region
1953   "g" gnus-uu-unmark-region
1954   "R" gnus-uu-mark-by-regexp
1955   "G" gnus-uu-unmark-by-regexp
1956   "t" gnus-uu-mark-thread
1957   "T" gnus-uu-unmark-thread
1958   "a" gnus-uu-mark-all
1959   "b" gnus-uu-mark-buffer
1960   "S" gnus-uu-mark-sparse
1961   "k" gnus-summary-kill-process-mark
1962   "y" gnus-summary-yank-process-mark
1963   "w" gnus-summary-save-process-mark
1964   "i" gnus-uu-invert-processable)
1965
1966 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1967   ;;"x" gnus-uu-extract-any
1968   "m" gnus-summary-save-parts
1969   "u" gnus-uu-decode-uu
1970   "U" gnus-uu-decode-uu-and-save
1971   "s" gnus-uu-decode-unshar
1972   "S" gnus-uu-decode-unshar-and-save
1973   "o" gnus-uu-decode-save
1974   "O" gnus-uu-decode-save
1975   "b" gnus-uu-decode-binhex
1976   "B" gnus-uu-decode-binhex
1977   "p" gnus-uu-decode-postscript
1978   "P" gnus-uu-decode-postscript-and-save)
1979
1980 (gnus-define-keys
1981     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
1982   "u" gnus-uu-decode-uu-view
1983   "U" gnus-uu-decode-uu-and-save-view
1984   "s" gnus-uu-decode-unshar-view
1985   "S" gnus-uu-decode-unshar-and-save-view
1986   "o" gnus-uu-decode-save-view
1987   "O" gnus-uu-decode-save-view
1988   "b" gnus-uu-decode-binhex-view
1989   "B" gnus-uu-decode-binhex-view
1990   "p" gnus-uu-decode-postscript-view
1991   "P" gnus-uu-decode-postscript-and-save-view)
1992
1993 (defvar gnus-article-post-menu nil)
1994
1995 (defconst gnus-summary-menu-maxlen 20)
1996
1997 (defun gnus-summary-menu-split (menu)
1998   ;; If we have lots of elements, divide them into groups of 20
1999   ;; and make a pane (or submenu) for each one.
2000   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2001       (let ((menu menu) sublists next
2002             (i 1))
2003         (while menu
2004           ;; Pull off the next gnus-summary-menu-maxlen elements
2005           ;; and make them the next element of sublist.
2006           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2007           (if next
2008               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2009                       nil))
2010           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2011                                              (aref (car (last menu)) 0)) menu)
2012                                sublists))
2013           (setq i (1+ i))
2014           (setq menu next))
2015         (nreverse sublists))
2016     ;; Few elements--put them all in one pane.
2017     menu))
2018
2019 (defun gnus-summary-make-menu-bar ()
2020   (gnus-turn-off-edit-menu 'summary)
2021
2022   (unless (boundp 'gnus-summary-misc-menu)
2023
2024     (easy-menu-define
2025      gnus-summary-kill-menu gnus-summary-mode-map ""
2026      (cons
2027       "Score"
2028       (nconc
2029        (list
2030         ["Customize" gnus-score-customize t])
2031        (gnus-make-score-map 'increase)
2032        (gnus-make-score-map 'lower)
2033        '(("Mark"
2034           ["Kill below" gnus-summary-kill-below t]
2035           ["Mark above" gnus-summary-mark-above t]
2036           ["Tick above" gnus-summary-tick-above t]
2037           ["Clear above" gnus-summary-clear-above t])
2038          ["Current score" gnus-summary-current-score t]
2039          ["Set score" gnus-summary-set-score t]
2040          ["Switch current score file..." gnus-score-change-score-file t]
2041          ["Set mark below..." gnus-score-set-mark-below t]
2042          ["Set expunge below..." gnus-score-set-expunge-below t]
2043          ["Edit current score file" gnus-score-edit-current-scores t]
2044          ["Edit score file" gnus-score-edit-file t]
2045          ["Trace score" gnus-score-find-trace t]
2046          ["Find words" gnus-score-find-favourite-words t]
2047          ["Rescore buffer" gnus-summary-rescore t]
2048          ["Increase score..." gnus-summary-increase-score t]
2049          ["Lower score..." gnus-summary-lower-score t]))))
2050
2051     ;; Define both the Article menu in the summary buffer and the
2052     ;; equivalent Commands menu in the article buffer here for
2053     ;; consistency.
2054     (let ((innards
2055            `(("Hide"
2056               ["All" gnus-article-hide t]
2057               ["Headers" gnus-article-hide-headers t]
2058               ["Signature" gnus-article-hide-signature t]
2059               ["Citation" gnus-article-hide-citation t]
2060               ["List identifiers" gnus-article-hide-list-identifiers t]
2061               ["Banner" gnus-article-strip-banner t]
2062               ["Boring headers" gnus-article-hide-boring-headers t])
2063              ("Highlight"
2064               ["All" gnus-article-highlight t]
2065               ["Headers" gnus-article-highlight-headers t]
2066               ["Signature" gnus-article-highlight-signature t]
2067               ["Citation" gnus-article-highlight-citation t])
2068              ("Date"
2069               ["Local" gnus-article-date-local t]
2070               ["ISO8601" gnus-article-date-iso8601 t]
2071               ["UT" gnus-article-date-ut t]
2072               ["Original" gnus-article-date-original t]
2073               ["Lapsed" gnus-article-date-lapsed t]
2074               ["User-defined" gnus-article-date-user t])
2075              ("Display"
2076               ["Remove images" gnus-article-remove-images t]
2077               ["Toggle smiley" gnus-treat-smiley t]
2078               ["Show X-Face" gnus-article-display-x-face t]
2079               ["Show picons in From" gnus-treat-from-picon t]
2080               ["Show picons in mail headers" gnus-treat-mail-picon t]
2081               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2082               ("View as different encoding"
2083                ,@(gnus-summary-menu-split
2084                   (mapcar
2085                    (lambda (cs)
2086                      ;; Since easymenu under FSF Emacs doesn't allow lambda
2087                      ;; forms for menu commands, we should provide intern'ed
2088                      ;; function symbols.
2089                      (let ((command (intern (format "\
2090 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2091                        (fset command
2092                              `(lambda ()
2093                                 (interactive)
2094                                 (let ((gnus-summary-show-article-charset-alist
2095                                        '((1 . ,cs))))
2096                                   (gnus-summary-show-article 1))))
2097                        `[,(symbol-name cs) ,command t]))
2098                    (sort (if (fboundp 'coding-system-list)
2099                              (coding-system-list)
2100                            ;;(mapcar 'car mm-mime-mule-charset-alist)
2101                            )
2102                          'string<)))))
2103              ("Washing"
2104               ("Remove Blanks"
2105                ["Leading" gnus-article-strip-leading-blank-lines t]
2106                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2107                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2108                ["All of the above" gnus-article-strip-blank-lines t]
2109                ["All" gnus-article-strip-all-blank-lines t]
2110                ["Leading space" gnus-article-strip-leading-space t]
2111                ["Trailing space" gnus-article-strip-trailing-space t]
2112                ["Leading space in headers"
2113                 gnus-article-remove-leading-whitespace t])
2114               ["Overstrike" gnus-article-treat-overstrike t]
2115               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2116               ["Emphasis" gnus-article-emphasize t]
2117               ["Word wrap" gnus-article-fill-cited-article t]
2118               ["Fill long lines" gnus-article-fill-long-lines t]
2119               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2120               ["CR" gnus-article-remove-cr 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               ["Toggle MIME" gnus-summary-toggle-mime t]
2130               ["Verbose header" gnus-summary-verbose-headers t]
2131               ["Toggle header" gnus-summary-toggle-header t]
2132               ["Unfold headers" gnus-article-treat-unfold-headers t]
2133               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2134               ["Html" gnus-article-wash-html t]
2135               ["URLs" gnus-article-unsplit-urls t]
2136               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2137               ["HZ" gnus-article-decode-HZ t]
2138               ("(Outlook) Deuglify"
2139                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2140                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2141                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2142                ["Full (Outlook) deuglify"
2143                 gnus-article-outlook-deuglify-article t])
2144               )
2145              ("Output"
2146               ["Save in default format" gnus-summary-save-article
2147                ,@(if (featurep 'xemacs) '(t)
2148                    '(:help "Save article using default method"))]
2149               ["Save in file" gnus-summary-save-article-file
2150                ,@(if (featurep 'xemacs) '(t)
2151                    '(:help "Save article in file"))]
2152               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2153               ["Save in MH folder" gnus-summary-save-article-folder t]
2154               ["Save in VM folder" gnus-summary-save-article-vm t]
2155               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2156               ["Save body in file" gnus-summary-save-article-body-file t]
2157               ["Pipe through a filter" gnus-summary-pipe-output t]
2158               ["Add to SOUP packet" gnus-soup-add-article t]
2159               ["Print with Muttprint" gnus-summary-muttprint t]
2160               ["Print" gnus-summary-print-article t])
2161              ("Backend"
2162               ["Respool article..." gnus-summary-respool-article t]
2163               ["Move article..." gnus-summary-move-article
2164                (gnus-check-backend-function
2165                 'request-move-article gnus-newsgroup-name)]
2166               ["Copy article..." gnus-summary-copy-article t]
2167               ["Crosspost article..." gnus-summary-crosspost-article
2168                (gnus-check-backend-function
2169                 'request-replace-article gnus-newsgroup-name)]
2170               ["Import file..." gnus-summary-import-article t]
2171               ["Create article..." gnus-summary-create-article t]
2172               ["Check if posted" gnus-summary-article-posted-p t]
2173               ["Edit article" gnus-summary-edit-article
2174                (not (gnus-group-read-only-p))]
2175               ["Delete article" gnus-summary-delete-article
2176                (gnus-check-backend-function
2177                 'request-expire-articles gnus-newsgroup-name)]
2178               ["Query respool" gnus-summary-respool-query t]
2179               ["Trace respool" gnus-summary-respool-trace t]
2180               ["Delete expirable articles" gnus-summary-expire-articles-now
2181                (gnus-check-backend-function
2182                 'request-expire-articles gnus-newsgroup-name)])
2183              ("Extract"
2184               ["Uudecode" gnus-uu-decode-uu
2185                ,@(if (featurep 'xemacs) '(t)
2186                    '(:help "Decode uuencoded article(s)"))]
2187               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2188               ["Unshar" gnus-uu-decode-unshar t]
2189               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2190               ["Save" gnus-uu-decode-save t]
2191               ["Binhex" gnus-uu-decode-binhex t]
2192               ["Postscript" gnus-uu-decode-postscript 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-summary-digest-mail-forward t]
2271        ["Digest and post" gnus-summary-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   (unless (gnus-news-group-p group)
2621     (setq gnus-newsgroup-incorporated
2622           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2623   (make-local-variable 'gnus-summary-line-format)
2624   (make-local-variable 'gnus-summary-line-format-spec)
2625   (make-local-variable 'gnus-summary-dummy-line-format)
2626   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2627   (make-local-variable 'gnus-summary-mark-positions)
2628   (make-local-hook 'pre-command-hook)
2629   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2630   (gnus-run-hooks 'gnus-summary-mode-hook)
2631   (turn-on-gnus-mailing-list-mode)
2632   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2633   (gnus-update-summary-mark-positions))
2634
2635 (defun gnus-summary-make-local-variables ()
2636   "Make all the local summary buffer variables."
2637   (let (global)
2638     (dolist (local gnus-summary-local-variables)
2639       (if (consp local)
2640           (progn
2641             (if (eq (cdr local) 'global)
2642                 ;; Copy the global value of the variable.
2643                 (setq global (symbol-value (car local)))
2644               ;; Use the value from the list.
2645               (setq global (eval (cdr local))))
2646             (set (make-local-variable (car local)) global))
2647         ;; Simple nil-valued local variable.
2648         (set (make-local-variable local) nil)))))
2649
2650 (defun gnus-summary-clear-local-variables ()
2651   (let ((locals gnus-summary-local-variables))
2652     (while locals
2653       (if (consp (car locals))
2654           (and (vectorp (caar locals))
2655                (set (caar locals) nil))
2656         (and (vectorp (car locals))
2657              (set (car locals) nil)))
2658       (setq locals (cdr locals)))))
2659
2660 ;; Summary data functions.
2661
2662 (defmacro gnus-data-number (data)
2663   `(car ,data))
2664
2665 (defmacro gnus-data-set-number (data number)
2666   `(setcar ,data ,number))
2667
2668 (defmacro gnus-data-mark (data)
2669   `(nth 1 ,data))
2670
2671 (defmacro gnus-data-set-mark (data mark)
2672   `(setcar (nthcdr 1 ,data) ,mark))
2673
2674 (defmacro gnus-data-pos (data)
2675   `(nth 2 ,data))
2676
2677 (defmacro gnus-data-set-pos (data pos)
2678   `(setcar (nthcdr 2 ,data) ,pos))
2679
2680 (defmacro gnus-data-header (data)
2681   `(nth 3 ,data))
2682
2683 (defmacro gnus-data-set-header (data header)
2684   `(setcar (nthcdr 3 ,data) ,header))
2685
2686 (defmacro gnus-data-level (data)
2687   `(nth 4 ,data))
2688
2689 (defmacro gnus-data-unread-p (data)
2690   `(= (nth 1 ,data) gnus-unread-mark))
2691
2692 (defmacro gnus-data-read-p (data)
2693   `(/= (nth 1 ,data) gnus-unread-mark))
2694
2695 (defmacro gnus-data-pseudo-p (data)
2696   `(consp (nth 3 ,data)))
2697
2698 (defmacro gnus-data-find (number)
2699   `(assq ,number gnus-newsgroup-data))
2700
2701 (defmacro gnus-data-find-list (number &optional data)
2702   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2703      (memq (assq ,number bdata)
2704            bdata)))
2705
2706 (defmacro gnus-data-make (number mark pos header level)
2707   `(list ,number ,mark ,pos ,header ,level))
2708
2709 (defun gnus-data-enter (after-article number mark pos header level offset)
2710   (let ((data (gnus-data-find-list after-article)))
2711     (unless data
2712       (error "No such article: %d" after-article))
2713     (setcdr data (cons (gnus-data-make number mark pos header level)
2714                        (cdr data)))
2715     (setq gnus-newsgroup-data-reverse nil)
2716     (gnus-data-update-list (cddr data) offset)))
2717
2718 (defun gnus-data-enter-list (after-article list &optional offset)
2719   (when list
2720     (let ((data (and after-article (gnus-data-find-list after-article)))
2721           (ilist list))
2722       (if (not (or data
2723                    after-article))
2724           (let ((odata gnus-newsgroup-data))
2725             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2726             (when offset
2727               (gnus-data-update-list odata offset)))
2728         ;; Find the last element in the list to be spliced into the main
2729         ;; list.
2730         (while (cdr list)
2731           (setq list (cdr list)))
2732         (if (not data)
2733             (progn
2734               (setcdr list gnus-newsgroup-data)
2735               (setq gnus-newsgroup-data ilist)
2736               (when offset
2737                 (gnus-data-update-list (cdr list) offset)))
2738           (setcdr list (cdr data))
2739           (setcdr data ilist)
2740           (when offset
2741             (gnus-data-update-list (cdr list) offset))))
2742       (setq gnus-newsgroup-data-reverse nil))))
2743
2744 (defun gnus-data-remove (article &optional offset)
2745   (let ((data gnus-newsgroup-data))
2746     (if (= (gnus-data-number (car data)) article)
2747         (progn
2748           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2749                 gnus-newsgroup-data-reverse nil)
2750           (when offset
2751             (gnus-data-update-list gnus-newsgroup-data offset)))
2752       (while (cdr data)
2753         (when (= (gnus-data-number (cadr data)) article)
2754           (setcdr data (cddr data))
2755           (when offset
2756             (gnus-data-update-list (cdr data) offset))
2757           (setq data nil
2758                 gnus-newsgroup-data-reverse nil))
2759         (setq data (cdr data))))))
2760
2761 (defmacro gnus-data-list (backward)
2762   `(if ,backward
2763        (or gnus-newsgroup-data-reverse
2764            (setq gnus-newsgroup-data-reverse
2765                  (reverse gnus-newsgroup-data)))
2766      gnus-newsgroup-data))
2767
2768 (defun gnus-data-update-list (data offset)
2769   "Add OFFSET to the POS of all data entries in DATA."
2770   (setq gnus-newsgroup-data-reverse nil)
2771   (while data
2772     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2773     (setq data (cdr data))))
2774
2775 (defun gnus-summary-article-pseudo-p (article)
2776   "Say whether this article is a pseudo article or not."
2777   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2778
2779 (defmacro gnus-summary-article-sparse-p (article)
2780   "Say whether this article is a sparse article or not."
2781   `(memq ,article gnus-newsgroup-sparse))
2782
2783 (defmacro gnus-summary-article-ancient-p (article)
2784   "Say whether this article is a sparse article or not."
2785   `(memq ,article gnus-newsgroup-ancient))
2786
2787 (defun gnus-article-parent-p (number)
2788   "Say whether this article is a parent or not."
2789   (let ((data (gnus-data-find-list number)))
2790     (and (cdr data)                     ; There has to be an article after...
2791          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2792             (gnus-data-level (nth 1 data))))))
2793
2794 (defun gnus-article-children (number)
2795   "Return a list of all children to NUMBER."
2796   (let* ((data (gnus-data-find-list number))
2797          (level (gnus-data-level (car data)))
2798          children)
2799     (setq data (cdr data))
2800     (while (and data
2801                 (= (gnus-data-level (car data)) (1+ level)))
2802       (push (gnus-data-number (car data)) children)
2803       (setq data (cdr data)))
2804     children))
2805
2806 (defmacro gnus-summary-skip-intangible ()
2807   "If the current article is intangible, then jump to a different article."
2808   '(let ((to (get-text-property (point) 'gnus-intangible)))
2809      (and to (gnus-summary-goto-subject to))))
2810
2811 (defmacro gnus-summary-article-intangible-p ()
2812   "Say whether this article is intangible or not."
2813   '(get-text-property (point) 'gnus-intangible))
2814
2815 (defun gnus-article-read-p (article)
2816   "Say whether ARTICLE is read or not."
2817   (not (or (memq article gnus-newsgroup-marked)
2818            (memq article gnus-newsgroup-spam-marked)
2819            (memq article gnus-newsgroup-unreads)
2820            (memq article gnus-newsgroup-unselected)
2821            (memq article gnus-newsgroup-dormant))))
2822
2823 ;; Some summary mode macros.
2824
2825 (defmacro gnus-summary-article-number ()
2826   "The article number of the article on the current line.
2827 If there isn't an article number here, then we return the current
2828 article number."
2829   '(progn
2830      (gnus-summary-skip-intangible)
2831      (or (get-text-property (point) 'gnus-number)
2832          (gnus-summary-last-subject))))
2833
2834 (defmacro gnus-summary-article-header (&optional number)
2835   "Return the header of article NUMBER."
2836   `(gnus-data-header (gnus-data-find
2837                       ,(or number '(gnus-summary-article-number)))))
2838
2839 (defmacro gnus-summary-thread-level (&optional number)
2840   "Return the level of thread that starts with article NUMBER."
2841   `(if (and (eq gnus-summary-make-false-root 'dummy)
2842             (get-text-property (point) 'gnus-intangible))
2843        0
2844      (gnus-data-level (gnus-data-find
2845                        ,(or number '(gnus-summary-article-number))))))
2846
2847 (defmacro gnus-summary-article-mark (&optional number)
2848   "Return the mark of article NUMBER."
2849   `(gnus-data-mark (gnus-data-find
2850                     ,(or number '(gnus-summary-article-number)))))
2851
2852 (defmacro gnus-summary-article-pos (&optional number)
2853   "Return the position of the line of article NUMBER."
2854   `(gnus-data-pos (gnus-data-find
2855                    ,(or number '(gnus-summary-article-number)))))
2856
2857 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2858 (defmacro gnus-summary-article-subject (&optional number)
2859   "Return current subject string or nil if nothing."
2860   `(let ((headers
2861           ,(if number
2862                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2863              '(gnus-data-header (assq (gnus-summary-article-number)
2864                                       gnus-newsgroup-data)))))
2865      (and headers
2866           (vectorp headers)
2867           (mail-header-subject headers))))
2868
2869 (defmacro gnus-summary-article-score (&optional number)
2870   "Return current article score."
2871   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2872                   gnus-newsgroup-scored))
2873        gnus-summary-default-score 0))
2874
2875 (defun gnus-summary-article-children (&optional number)
2876   "Return a list of article numbers that are children of article NUMBER."
2877   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2878          (level (gnus-data-level (car data)))
2879          l children)
2880     (while (and (setq data (cdr data))
2881                 (> (setq l (gnus-data-level (car data))) level))
2882       (and (= (1+ level) l)
2883            (push (gnus-data-number (car data))
2884                  children)))
2885     (nreverse children)))
2886
2887 (defun gnus-summary-article-parent (&optional number)
2888   "Return the article number of the parent of article NUMBER."
2889   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2890                                     (gnus-data-list t)))
2891          (level (gnus-data-level (car data))))
2892     (if (zerop level)
2893         ()                              ; This is a root.
2894       ;; We search until we find an article with a level less than
2895       ;; this one.  That function has to be the parent.
2896       (while (and (setq data (cdr data))
2897                   (not (< (gnus-data-level (car data)) level))))
2898       (and data (gnus-data-number (car data))))))
2899
2900 (defun gnus-unread-mark-p (mark)
2901   "Say whether MARK is the unread mark."
2902   (= mark gnus-unread-mark))
2903
2904 (defun gnus-read-mark-p (mark)
2905   "Say whether MARK is one of the marks that mark as read.
2906 This is all marks except unread, ticked, dormant, and expirable."
2907   (not (or (= mark gnus-unread-mark)
2908            (= mark gnus-ticked-mark)
2909            (= mark gnus-spam-mark)
2910            (= mark gnus-dormant-mark)
2911            (= mark gnus-expirable-mark))))
2912
2913 (defmacro gnus-article-mark (number)
2914   "Return the MARK of article NUMBER.
2915 This macro should only be used when computing the mark the \"first\"
2916 time; i.e., when generating the summary lines.  After that,
2917 `gnus-summary-article-mark' should be used to examine the
2918 marks of articles."
2919   `(cond
2920     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2921     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2922     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2923     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2924     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2925     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2926     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2927     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2928            gnus-ancient-mark))))
2929
2930 ;; Saving hidden threads.
2931
2932 (defmacro gnus-save-hidden-threads (&rest forms)
2933   "Save hidden threads, eval FORMS, and restore the hidden threads."
2934   (let ((config (make-symbol "config")))
2935     `(let ((,config (gnus-hidden-threads-configuration)))
2936        (unwind-protect
2937            (save-excursion
2938              ,@forms)
2939          (gnus-restore-hidden-threads-configuration ,config)))))
2940 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2941 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2942
2943 (defun gnus-data-compute-positions ()
2944   "Compute the positions of all articles."
2945   (setq gnus-newsgroup-data-reverse nil)
2946   (let ((data gnus-newsgroup-data))
2947     (save-excursion
2948       (gnus-save-hidden-threads
2949         (gnus-summary-show-all-threads)
2950         (goto-char (point-min))
2951         (while data
2952           (while (get-text-property (point) 'gnus-intangible)
2953             (forward-line 1))
2954           (gnus-data-set-pos (car data) (+ (point) 3))
2955           (setq data (cdr data))
2956           (forward-line 1))))))
2957
2958 (defun gnus-hidden-threads-configuration ()
2959   "Return the current hidden threads configuration."
2960   (save-excursion
2961     (let (config)
2962       (goto-char (point-min))
2963       (while (search-forward "\r" nil t)
2964         (push (1- (point)) config))
2965       config)))
2966
2967 (defun gnus-restore-hidden-threads-configuration (config)
2968   "Restore hidden threads configuration from CONFIG."
2969   (save-excursion
2970     (let (point buffer-read-only)
2971       (while (setq point (pop config))
2972         (when (and (< point (point-max))
2973                    (goto-char point)
2974                    (eq (char-after) ?\n))
2975           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2976
2977 ;; Various summary mode internalish functions.
2978
2979 (defun gnus-mouse-pick-article (e)
2980   (interactive "e")
2981   (mouse-set-point e)
2982   (gnus-summary-next-page nil t))
2983
2984 (defun gnus-summary-set-display-table ()
2985   "Change the display table.
2986 Odd characters have a tendency to mess
2987 up nicely formatted displays - we make all possible glyphs
2988 display only a single character."
2989
2990   ;; We start from the standard display table, if any.
2991   (let ((table (or (copy-sequence standard-display-table)
2992                    (make-display-table)))
2993         (i 32))
2994     ;; Nix out all the control chars...
2995     (while (>= (setq i (1- i)) 0)
2996       (aset table i [??]))
2997     ;; ... but not newline and cr, of course.  (cr is necessary for the
2998     ;; selective display).
2999     (aset table ?\n nil)
3000     (aset table ?\r nil)
3001     ;; We keep TAB as well.
3002     (aset table ?\t nil)
3003     ;; We nix out any glyphs over 126 that are not set already.
3004     (let ((i 256))
3005       (while (>= (setq i (1- i)) 127)
3006         ;; Only modify if the entry is nil.
3007         (unless (aref table i)
3008           (aset table i [??]))))
3009     (setq buffer-display-table table)))
3010
3011 (defun gnus-summary-set-article-display-arrow (pos)
3012   "Update the overlay arrow to point to line at position POS."
3013   (when (and gnus-summary-display-arrow
3014              (boundp 'overlay-arrow-position)
3015              (boundp 'overlay-arrow-string))
3016     (save-excursion
3017       (goto-char pos)
3018       (beginning-of-line)
3019       (unless overlay-arrow-position
3020         (setq overlay-arrow-position (make-marker)))
3021       (setq overlay-arrow-string "=>"
3022             overlay-arrow-position (set-marker overlay-arrow-position
3023                                                (point)
3024                                                (current-buffer))))))
3025
3026 (defun gnus-summary-buffer-name (group)
3027   "Return the summary buffer name of GROUP."
3028   (concat "*Summary " (gnus-group-decoded-name group) "*"))
3029
3030 (defun gnus-summary-setup-buffer (group)
3031   "Initialize summary buffer."
3032   (let ((buffer (gnus-summary-buffer-name group))
3033         (dead-name (concat "*Dead Summary "
3034                            (gnus-group-decoded-name group) "*")))
3035     ;; If a dead summary buffer exists, we kill it.
3036     (when (gnus-buffer-live-p dead-name)
3037       (gnus-kill-buffer dead-name))
3038     (if (get-buffer buffer)
3039         (progn
3040           (set-buffer buffer)
3041           (setq gnus-summary-buffer (current-buffer))
3042           (not gnus-newsgroup-prepared))
3043       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3044       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3045       (gnus-summary-mode group)
3046       (when gnus-carpal
3047         (gnus-carpal-setup-buffer 'summary))
3048       (unless gnus-single-article-buffer
3049         (make-local-variable 'gnus-article-buffer)
3050         (make-local-variable 'gnus-article-current)
3051         (make-local-variable 'gnus-original-article-buffer))
3052       (setq gnus-newsgroup-name group)
3053       ;; Set any local variables in the group parameters.
3054       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3055       t)))
3056
3057 (defun gnus-set-global-variables ()
3058   "Set the global equivalents of the buffer-local variables.
3059 They are set to the latest values they had.  These reflect the summary
3060 buffer that was in action when the last article was fetched."
3061   (when (eq major-mode 'gnus-summary-mode)
3062     (setq gnus-summary-buffer (current-buffer))
3063     (let ((name gnus-newsgroup-name)
3064           (marked gnus-newsgroup-marked)
3065           (spam gnus-newsgroup-spam-marked)
3066           (unread gnus-newsgroup-unreads)
3067           (headers gnus-current-headers)
3068           (data gnus-newsgroup-data)
3069           (summary gnus-summary-buffer)
3070           (article-buffer gnus-article-buffer)
3071           (original gnus-original-article-buffer)
3072           (gac gnus-article-current)
3073           (reffed gnus-reffed-article-number)
3074           (score-file gnus-current-score-file)
3075           (default-charset gnus-newsgroup-charset)
3076           vlist)
3077       (let ((locals gnus-newsgroup-variables))
3078         (while locals
3079           (if (consp (car locals))
3080               (push (eval (caar locals)) vlist)
3081             (push (eval (car locals)) vlist))
3082           (setq locals (cdr locals)))
3083         (setq vlist (nreverse vlist)))
3084       (save-excursion
3085         (set-buffer gnus-group-buffer)
3086         (setq gnus-newsgroup-name name
3087               gnus-newsgroup-marked marked
3088               gnus-newsgroup-spam-marked spam
3089               gnus-newsgroup-unreads unread
3090               gnus-current-headers headers
3091               gnus-newsgroup-data data
3092               gnus-article-current gac
3093               gnus-summary-buffer summary
3094               gnus-article-buffer article-buffer
3095               gnus-original-article-buffer original
3096               gnus-reffed-article-number reffed
3097               gnus-current-score-file score-file
3098               gnus-newsgroup-charset default-charset)
3099         (let ((locals gnus-newsgroup-variables))
3100           (while locals
3101             (if (consp (car locals))
3102                 (set (caar locals) (pop vlist))
3103               (set (car locals) (pop vlist)))
3104             (setq locals (cdr locals))))
3105         ;; The article buffer also has local variables.
3106         (when (gnus-buffer-live-p gnus-article-buffer)
3107           (set-buffer gnus-article-buffer)
3108           (setq gnus-summary-buffer summary))))))
3109
3110 (defun gnus-summary-article-unread-p (article)
3111   "Say whether ARTICLE is unread or not."
3112   (memq article gnus-newsgroup-unreads))
3113
3114 (defun gnus-summary-first-article-p (&optional article)
3115   "Return whether ARTICLE is the first article in the buffer."
3116   (if (not (setq article (or article (gnus-summary-article-number))))
3117       nil
3118     (eq article (caar gnus-newsgroup-data))))
3119
3120 (defun gnus-summary-last-article-p (&optional article)
3121   "Return whether ARTICLE is the last article in the buffer."
3122   (if (not (setq article (or article (gnus-summary-article-number))))
3123       ;; All non-existent numbers are the last article.  :-)
3124       t
3125     (not (cdr (gnus-data-find-list article)))))
3126
3127 (defun gnus-make-thread-indent-array ()
3128   (let ((n 200))
3129     (unless (and gnus-thread-indent-array
3130                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3131       (setq gnus-thread-indent-array (make-vector 201 "")
3132             gnus-thread-indent-array-level gnus-thread-indent-level)
3133       (while (>= n 0)
3134         (aset gnus-thread-indent-array n
3135               (make-string (* n gnus-thread-indent-level) ? ))
3136         (setq n (1- n))))))
3137
3138 (defun gnus-update-summary-mark-positions ()
3139   "Compute where the summary marks are to go."
3140   (save-excursion
3141     (when (gnus-buffer-exists-p gnus-summary-buffer)
3142       (set-buffer gnus-summary-buffer))
3143     (let ((gnus-replied-mark 129)
3144           (gnus-score-below-mark 130)
3145           (gnus-score-over-mark 130)
3146           (gnus-undownloaded-mark 131)
3147           (spec gnus-summary-line-format-spec)
3148           gnus-visual pos)
3149       (save-excursion
3150         (gnus-set-work-buffer)
3151         (let ((gnus-summary-line-format-spec spec)
3152               (gnus-newsgroup-downloadable '(0)))
3153           (gnus-summary-insert-line
3154            (make-full-mail-header 0 "" "nobody"
3155                                   "05 Apr 2001 23:33:09 +0400"
3156                                   "" "" 0 0 "" nil)
3157            0 nil t 128 t nil "" nil 1)
3158           (goto-char (point-min))
3159           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3160                                              (- (point) (point-min) 1)))))
3161           (goto-char (point-min))
3162           (push (cons 'replied (and (search-forward "\201" nil t)
3163                                     (- (point) (point-min) 1)))
3164                 pos)
3165           (goto-char (point-min))
3166           (push (cons 'score (and (search-forward "\202" nil t)
3167                                   (- (point) (point-min) 1)))
3168                 pos)
3169           (goto-char (point-min))
3170           (push (cons 'download
3171                       (and (search-forward "\203" nil t)
3172                            (- (point) (point-min) 1)))
3173                 pos)))
3174       (setq gnus-summary-mark-positions pos))))
3175
3176 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3177   "Insert a dummy root in the summary buffer."
3178   (beginning-of-line)
3179   (gnus-add-text-properties
3180    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3181    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3182
3183 (defun gnus-summary-extract-address-component (from)
3184   (or (car (funcall gnus-extract-address-components from))
3185       from))
3186
3187 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3188   (let ((default-mime-charset (with-current-buffer gnus-summary-buffer
3189                                 default-mime-charset)))
3190     ;; Is it really necessary to do this next part for each summary line?
3191     ;; Luckily, doesn't seem to slow things down much.
3192     (or
3193      (and gnus-ignored-from-addresses
3194           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3195           (let ((extra-headers (mail-header-extra header))
3196                 to
3197                 newsgroups)
3198             (cond
3199              ((setq to (cdr (assq 'To extra-headers)))
3200               (concat "-> "
3201                       (inline
3202                         (gnus-summary-extract-address-component
3203                          (funcall gnus-decode-encoded-word-function to)))))
3204              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3205               (concat "=> " newsgroups)))))
3206      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3207
3208 (defun gnus-summary-insert-line (gnus-tmp-header
3209                                  gnus-tmp-level gnus-tmp-current
3210                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3211                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3212                                  &optional gnus-tmp-dummy gnus-tmp-score
3213                                  gnus-tmp-process)
3214   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3215          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3216          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3217          (gnus-tmp-score-char
3218           (if (or (null gnus-summary-default-score)
3219                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3220                       gnus-summary-zcore-fuzz))
3221               ?\ ;;;Whitespace
3222             (if (< gnus-tmp-score gnus-summary-default-score)
3223                 gnus-score-below-mark gnus-score-over-mark)))
3224          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3225          (gnus-tmp-replied
3226           (cond (gnus-tmp-process gnus-process-mark)
3227                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3228                  gnus-cached-mark)
3229                 (gnus-tmp-replied gnus-replied-mark)
3230                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3231                  gnus-forwarded-mark)
3232                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3233                  gnus-saved-mark)
3234                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3235                  gnus-recent-mark)
3236                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3237                  gnus-unseen-mark)
3238                 (t gnus-no-mark)))
3239          (gnus-tmp-downloaded
3240           (cond (undownloaded 
3241                  gnus-undownloaded-mark)
3242                 (gnus-newsgroup-agentized
3243                  gnus-downloaded-mark)
3244                 (t
3245                  gnus-no-mark)))
3246          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3247          (gnus-tmp-name
3248           (cond
3249            ((string-match "<[^>]+> *$" gnus-tmp-from)
3250             (let ((beg (match-beginning 0)))
3251               (or (and (string-match "^\".+\"" gnus-tmp-from)
3252                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3253                   (substring gnus-tmp-from 0 beg))))
3254            ((string-match "(.+)" gnus-tmp-from)
3255             (substring gnus-tmp-from
3256                        (1+ (match-beginning 0)) (1- (match-end 0))))
3257            (t gnus-tmp-from)))
3258          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3259          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3260          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3261          (buffer-read-only nil))
3262     (when (string= gnus-tmp-name "")
3263       (setq gnus-tmp-name gnus-tmp-from))
3264     (unless (numberp gnus-tmp-lines)
3265       (setq gnus-tmp-lines -1))
3266     (if (= gnus-tmp-lines -1)
3267         (setq gnus-tmp-lines "?")
3268       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3269     (gnus-put-text-property-excluding-characters-with-faces
3270      (point)
3271      (progn (eval gnus-summary-line-format-spec) (point))
3272      'gnus-number gnus-tmp-number)
3273     (when (gnus-visual-p 'summary-highlight 'highlight)
3274       (forward-line -1)
3275       (gnus-run-hooks 'gnus-summary-update-hook)
3276       (forward-line 1))))
3277
3278 (defun gnus-summary-update-line (&optional dont-update)
3279   "Update summary line after change."
3280   (when (and gnus-summary-default-score
3281              (not gnus-summary-inhibit-highlight))
3282     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3283            (article (gnus-summary-article-number))
3284            (score (gnus-summary-article-score article)))
3285       (unless dont-update
3286         (if (and gnus-summary-mark-below
3287                  (< (gnus-summary-article-score)
3288                     gnus-summary-mark-below))
3289             ;; This article has a low score, so we mark it as read.
3290             (when (memq article gnus-newsgroup-unreads)
3291               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3292           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3293             ;; This article was previously marked as read on account
3294             ;; of a low score, but now it has risen, so we mark it as
3295             ;; unread.
3296             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3297         (gnus-summary-update-mark
3298          (if (or (null gnus-summary-default-score)
3299                  (<= (abs (- score gnus-summary-default-score))
3300                      gnus-summary-zcore-fuzz))
3301              ?\ ;;;Whitespace
3302            (if (< score gnus-summary-default-score)
3303                gnus-score-below-mark gnus-score-over-mark))
3304          'score))
3305       ;; Do visual highlighting.
3306       (when (gnus-visual-p 'summary-highlight 'highlight)
3307         (gnus-run-hooks 'gnus-summary-update-hook)))))
3308
3309 (defvar gnus-tmp-new-adopts nil)
3310
3311 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3312   "Return the number of articles in THREAD.
3313 This may be 0 in some cases -- if none of the articles in
3314 the thread are to be displayed."
3315   (let* ((number
3316           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3317           (cond
3318            ((not (listp thread))
3319             1)
3320            ((and (consp thread) (cdr thread))
3321             (apply
3322              '+ 1 (mapcar
3323                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3324            ((null thread)
3325             1)
3326            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3327             1)
3328            (t 0))))
3329     (when (and level (zerop level) gnus-tmp-new-adopts)
3330       (incf number
3331             (apply '+ (mapcar
3332                        'gnus-summary-number-of-articles-in-thread
3333                        gnus-tmp-new-adopts))))
3334     (if char
3335         (if (> number 1) gnus-not-empty-thread-mark
3336           gnus-empty-thread-mark)
3337       number)))
3338
3339 (defsubst gnus-summary-line-message-size (head)
3340   "Return pretty-printed version of message size.
3341 This function is intended to be used in
3342 `gnus-summary-line-format-alist', which see."
3343   (let ((c (or (mail-header-chars head) -1)))
3344     (cond ((< c 0) "n/a")               ; chars not available
3345           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3346           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3347           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3348           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3349
3350
3351 (defun gnus-summary-set-local-parameters (group)
3352   "Go through the local params of GROUP and set all variable specs in that list."
3353   (let ((params (gnus-group-find-parameter group))
3354         (vars '(quit-config))           ; Ignore quit-config.
3355         elem)
3356     (while params
3357       (setq elem (car params)
3358             params (cdr params))
3359       (and (consp elem)                 ; Has to be a cons.
3360            (consp (cdr elem))           ; The cdr has to be a list.
3361            (symbolp (car elem))         ; Has to be a symbol in there.
3362            (not (memq (car elem) vars))
3363            (ignore-errors               ; So we set it.
3364              (push (car elem) vars)
3365              (make-local-variable (car elem))
3366              (set (car elem) (eval (nth 1 elem))))))))
3367
3368 (defun gnus-summary-read-group (group &optional show-all no-article
3369                                       kill-buffer no-display backward
3370                                       select-articles)
3371   "Start reading news in newsgroup GROUP.
3372 If SHOW-ALL is non-nil, already read articles are also listed.
3373 If NO-ARTICLE is non-nil, no article is selected initially.
3374 If NO-DISPLAY, don't generate a summary buffer."
3375   (let (result)
3376     (while (and group
3377                 (null (setq result
3378                             (let ((gnus-auto-select-next nil))
3379                               (or (gnus-summary-read-group-1
3380                                    group show-all no-article
3381                                    kill-buffer no-display
3382                                    select-articles)
3383                                   (setq show-all nil
3384                                         select-articles nil)))))
3385                 (eq gnus-auto-select-next 'quietly))
3386       (set-buffer gnus-group-buffer)
3387       ;; The entry function called above goes to the next
3388       ;; group automatically, so we go two groups back
3389       ;; if we are searching for the previous group.
3390       (when backward
3391         (gnus-group-prev-unread-group 2))
3392       (if (not (equal group (gnus-group-group-name)))
3393           (setq group (gnus-group-group-name))
3394         (setq group nil)))
3395     result))
3396
3397 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3398   "Directly jump to the other GROUP from summary buffer.
3399 If SHOW-ALL is non-nil, already read articles are also listed."
3400   (interactive
3401    (if (eq gnus-summary-buffer (current-buffer))
3402        (list (completing-read
3403               "Group: " gnus-active-hashtb nil t
3404               (when (and gnus-newsgroup-name
3405                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3406                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3407               'gnus-group-history)
3408              current-prefix-arg)
3409      (error "%s must be invoked from a gnus summary buffer." this-command)))
3410   (unless (or (zerop (length group))
3411               (and gnus-newsgroup-name
3412                    (string-equal gnus-newsgroup-name group)))
3413     (gnus-summary-exit)
3414     (gnus-summary-read-group group show-all
3415                              gnus-dont-select-after-jump-to-other-group)))
3416
3417 (defun gnus-summary-read-group-1 (group show-all no-article
3418                                         kill-buffer no-display
3419                                         &optional select-articles)
3420   ;; Killed foreign groups can't be entered.
3421   ;;  (when (and (not (gnus-group-native-p group))
3422   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3423   ;;    (error "Dead non-native groups can't be entered"))
3424   (gnus-message 5 "Retrieving newsgroup: %s..."
3425                 (gnus-group-decoded-name group))
3426   (let* ((new-group (gnus-summary-setup-buffer group))
3427          (quit-config (gnus-group-quit-config group))
3428          (did-select (and new-group (gnus-select-newsgroup
3429                                      group show-all select-articles))))
3430     (cond
3431      ;; This summary buffer exists already, so we just select it.
3432      ((not new-group)
3433       (gnus-set-global-variables)
3434       (when kill-buffer
3435         (gnus-kill-or-deaden-summary kill-buffer))
3436       (gnus-configure-windows 'summary 'force)
3437       (gnus-set-mode-line 'summary)
3438       (gnus-summary-position-point)
3439       (message "")
3440       t)
3441      ;; We couldn't select this group.
3442      ((null did-select)
3443       (when (and (eq major-mode 'gnus-summary-mode)
3444                  (not (equal (current-buffer) kill-buffer)))
3445         (kill-buffer (current-buffer))
3446         (if (not quit-config)
3447             (progn
3448               ;; Update the info -- marks might need to be removed,
3449               ;; for instance.
3450               (gnus-summary-update-info)
3451               (set-buffer gnus-group-buffer)
3452               (gnus-group-jump-to-group group)
3453               (gnus-group-next-unread-group 1))
3454           (gnus-handle-ephemeral-exit quit-config)))
3455       (let ((grpinfo (gnus-get-info group)))
3456         (if (null (gnus-info-read grpinfo))
3457             (gnus-message 3 "Group %s contains no messages"
3458                           (gnus-group-decoded-name group))
3459           (gnus-message 3 "Can't select group")))
3460       nil)
3461      ;; The user did a `C-g' while prompting for number of articles,
3462      ;; so we exit this group.
3463      ((eq did-select 'quit)
3464       (and (eq major-mode 'gnus-summary-mode)
3465            (not (equal (current-buffer) kill-buffer))
3466            (kill-buffer (current-buffer)))
3467       (when kill-buffer
3468         (gnus-kill-or-deaden-summary kill-buffer))
3469       (if (not quit-config)
3470           (progn
3471             (set-buffer gnus-group-buffer)
3472             (gnus-group-jump-to-group group)
3473             (gnus-group-next-unread-group 1)
3474             (gnus-configure-windows 'group 'force))
3475         (gnus-handle-ephemeral-exit quit-config))
3476       ;; Finally signal the quit.
3477       (signal 'quit nil))
3478      ;; The group was successfully selected.
3479      (t
3480       (gnus-set-global-variables)
3481       ;; Save the active value in effect when the group was entered.
3482       (setq gnus-newsgroup-active
3483             (gnus-copy-sequence
3484              (gnus-active gnus-newsgroup-name)))
3485       ;; You can change the summary buffer in some way with this hook.
3486       (gnus-run-hooks 'gnus-select-group-hook)
3487       (gnus-update-format-specifications
3488        nil 'summary 'summary-mode 'summary-dummy)
3489       (gnus-update-summary-mark-positions)
3490       ;; Do score processing.
3491       (when gnus-use-scoring
3492         (gnus-possibly-score-headers))
3493       ;; Check whether to fill in the gaps in the threads.
3494       (when gnus-build-sparse-threads
3495         (gnus-build-sparse-threads))
3496       ;; Find the initial limit.
3497       (if gnus-show-threads
3498           (if show-all
3499               (let ((gnus-newsgroup-dormant nil))
3500                 (gnus-summary-initial-limit show-all))
3501             (gnus-summary-initial-limit show-all))
3502         ;; When unthreaded, all articles are always shown.
3503         (setq gnus-newsgroup-limit
3504               (mapcar
3505                (lambda (header) (mail-header-number header))
3506                gnus-newsgroup-headers)))
3507       ;; Generate the summary buffer.
3508       (unless no-display
3509         (gnus-summary-prepare))
3510       (when gnus-use-trees
3511         (gnus-tree-open group)
3512         (setq gnus-summary-highlight-line-function
3513               'gnus-tree-highlight-article))
3514       ;; If the summary buffer is empty, but there are some low-scored
3515       ;; articles or some excluded dormants, we include these in the
3516       ;; buffer.
3517       (when (and (zerop (buffer-size))
3518                  (not no-display))
3519         (cond (gnus-newsgroup-dormant
3520                (gnus-summary-limit-include-dormant))
3521               ((and gnus-newsgroup-scored show-all)
3522                (gnus-summary-limit-include-expunged t))))
3523       ;; Function `gnus-apply-kill-file' must be called in this hook.
3524       (gnus-run-hooks 'gnus-apply-kill-hook)
3525       (if (and (zerop (buffer-size))
3526                (not no-display))
3527           (progn
3528             ;; This newsgroup is empty.
3529             (gnus-summary-catchup-and-exit nil t)
3530             (gnus-message 6 "No unread news")
3531             (when kill-buffer
3532               (gnus-kill-or-deaden-summary kill-buffer))
3533             ;; Return nil from this function.
3534             nil)
3535         ;; Hide conversation thread subtrees.  We cannot do this in
3536         ;; gnus-summary-prepare-hook since kill processing may not
3537         ;; work with hidden articles.
3538         (gnus-summary-maybe-hide-threads)
3539         (when kill-buffer
3540           (gnus-kill-or-deaden-summary kill-buffer))
3541         (gnus-summary-auto-select-subject)
3542         ;; Show first unread article if requested.
3543         (if (and (not no-article)
3544                  (not no-display)
3545                  gnus-newsgroup-unreads
3546                  gnus-auto-select-first)
3547             (progn
3548               (gnus-configure-windows 'summary)
3549               (let ((art (gnus-summary-article-number)))
3550                 (unless (and (not gnus-plugged)
3551                              (or (memq art gnus-newsgroup-undownloaded)
3552                                  (memq art gnus-newsgroup-downloadable)))
3553                   (gnus-summary-goto-article art))))
3554           ;; Don't select any articles.
3555           (gnus-summary-position-point)
3556           (gnus-configure-windows 'summary 'force)
3557           (gnus-set-mode-line 'summary))
3558         (when (get-buffer-window gnus-group-buffer t)
3559           ;; Gotta use windows, because recenter does weird stuff if
3560           ;; the current buffer ain't the displayed window.
3561           (let ((owin (selected-window)))
3562             (select-window (get-buffer-window gnus-group-buffer t))
3563             (when (gnus-group-goto-group group)
3564               (recenter))
3565             (select-window owin)))
3566         ;; Mark this buffer as "prepared".
3567         (setq gnus-newsgroup-prepared t)
3568         (gnus-run-hooks 'gnus-summary-prepared-hook)
3569         (unless (gnus-ephemeral-group-p group)
3570           (gnus-group-update-group group))
3571         t)))))
3572
3573 (defun gnus-summary-auto-select-subject ()
3574   "Select the subject line on initial group entry."
3575   (goto-char (point-min))
3576   (cond
3577    ((eq gnus-auto-select-subject 'best)
3578     (gnus-summary-best-unread-subject))
3579    ((eq gnus-auto-select-subject 'unread)
3580     (gnus-summary-first-unread-subject))
3581    ((eq gnus-auto-select-subject 'unseen)
3582     (gnus-summary-first-unseen-subject))
3583    ((eq gnus-auto-select-subject 'unseen-or-unread)
3584     (gnus-summary-first-unseen-or-unread-subject))
3585    ((eq gnus-auto-select-subject 'first)
3586     ;; Do nothing.
3587     )
3588    ((gnus-functionp gnus-auto-select-subject)
3589     (funcall gnus-auto-select-subject))))
3590
3591 (defun gnus-summary-prepare ()
3592   "Generate the summary buffer."
3593   (interactive)
3594   (let ((buffer-read-only nil))
3595     (erase-buffer)
3596     (setq gnus-newsgroup-data nil
3597           gnus-newsgroup-data-reverse nil)
3598     (gnus-run-hooks 'gnus-summary-generate-hook)
3599     ;; Generate the buffer, either with threads or without.
3600     (when gnus-newsgroup-headers
3601       (gnus-summary-prepare-threads
3602        (if gnus-show-threads
3603            (gnus-sort-gathered-threads
3604             (funcall gnus-summary-thread-gathering-function
3605                      (gnus-sort-threads
3606                       (gnus-cut-threads (gnus-make-threads)))))
3607          ;; Unthreaded display.
3608          (gnus-sort-articles gnus-newsgroup-headers))))
3609     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3610     ;; Call hooks for modifying summary buffer.
3611     (goto-char (point-min))
3612     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3613
3614 (defsubst gnus-general-simplify-subject (subject)
3615   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3616   (setq subject
3617         (cond
3618          ;; Truncate the subject.
3619          (gnus-simplify-subject-functions
3620           (gnus-map-function gnus-simplify-subject-functions subject))
3621          ((numberp gnus-summary-gather-subject-limit)
3622           (setq subject (gnus-simplify-subject-re subject))
3623           (if (> (length subject) gnus-summary-gather-subject-limit)
3624               (substring subject 0 gnus-summary-gather-subject-limit)
3625             subject))
3626          ;; Fuzzily simplify it.
3627          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3628           (gnus-simplify-subject-fuzzy subject))
3629          ;; Just remove the leading "Re:".
3630          (t
3631           (gnus-simplify-subject-re subject))))
3632
3633   (if (and gnus-summary-gather-exclude-subject
3634            (string-match gnus-summary-gather-exclude-subject subject))
3635       nil                               ; This article shouldn't be gathered
3636     subject))
3637
3638 (defun gnus-summary-simplify-subject-query ()
3639   "Query where the respool algorithm would put this article."
3640   (interactive)
3641   (gnus-summary-select-article)
3642   (message "%s"
3643            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3644
3645 (defun gnus-gather-threads-by-subject (threads)
3646   "Gather threads by looking at Subject headers."
3647   (if (not gnus-summary-make-false-root)
3648       threads
3649     (let ((hashtb (gnus-make-hashtable 1024))
3650           (prev threads)
3651           (result threads)
3652           subject hthread whole-subject)
3653       (while threads
3654         (setq subject (gnus-general-simplify-subject
3655                        (setq whole-subject (mail-header-subject
3656                                             (caar threads)))))
3657         (when subject
3658           (if (setq hthread (gnus-gethash subject hashtb))
3659               (progn
3660                 ;; We enter a dummy root into the thread, if we
3661                 ;; haven't done that already.
3662                 (unless (stringp (caar hthread))
3663                   (setcar hthread (list whole-subject (car hthread))))
3664                 ;; We add this new gathered thread to this gathered
3665                 ;; thread.
3666                 (setcdr (car hthread)
3667                         (nconc (cdar hthread) (list (car threads))))
3668                 ;; Remove it from the list of threads.
3669                 (setcdr prev (cdr threads))
3670                 (setq threads prev))
3671             ;; Enter this thread into the hash table.
3672             (gnus-sethash subject
3673                           (if gnus-summary-make-false-root-always
3674                               (progn
3675                                 ;; If you want a dummy root above all
3676                                 ;; threads...
3677                                 (setcar threads (list whole-subject
3678                                                       (car threads)))
3679                                 threads)
3680                             threads)
3681                           hashtb)))
3682         (setq prev threads)
3683         (setq threads (cdr threads)))
3684       result)))
3685
3686 (defun gnus-gather-threads-by-references (threads)
3687   "Gather threads by looking at References headers."
3688   (let ((idhashtb (gnus-make-hashtable 1024))
3689         (thhashtb (gnus-make-hashtable 1024))
3690         (prev threads)
3691         (result threads)
3692         ids references id gthread gid entered ref)
3693     (while threads
3694       (when (setq references (mail-header-references (caar threads)))
3695         (setq id (mail-header-id (caar threads))
3696               ids (inline (gnus-split-references references))
3697               entered nil)
3698         (while (setq ref (pop ids))
3699           (setq ids (delete ref ids))
3700           (if (not (setq gid (gnus-gethash ref idhashtb)))
3701               (progn
3702                 (gnus-sethash ref id idhashtb)
3703                 (gnus-sethash id threads thhashtb))
3704             (setq gthread (gnus-gethash gid thhashtb))
3705             (unless entered
3706               ;; We enter a dummy root into the thread, if we
3707               ;; haven't done that already.
3708               (unless (stringp (caar gthread))
3709                 (setcar gthread (list (mail-header-subject (caar gthread))
3710                                       (car gthread))))
3711               ;; We add this new gathered thread to this gathered
3712               ;; thread.
3713               (setcdr (car gthread)
3714                       (nconc (cdar gthread) (list (car threads)))))
3715             ;; Add it into the thread hash table.
3716             (gnus-sethash id gthread thhashtb)
3717             (setq entered t)
3718             ;; Remove it from the list of threads.
3719             (setcdr prev (cdr threads))
3720             (setq threads prev))))
3721       (setq prev threads)
3722       (setq threads (cdr threads)))
3723     result))
3724
3725 (defun gnus-sort-gathered-threads (threads)
3726   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3727   (let ((result threads))
3728     (while threads
3729       (when (stringp (caar threads))
3730         (setcdr (car threads)
3731                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3732       (setq threads (cdr threads)))
3733     result))
3734
3735 (defun gnus-thread-loop-p (root thread)
3736   "Say whether ROOT is in THREAD."
3737   (let ((stack (list thread))
3738         (infloop 0)
3739         th)
3740     (while (setq thread (pop stack))
3741       (setq th (cdr thread))
3742       (while (and th
3743                   (not (eq (caar th) root)))
3744         (pop th))
3745       (if th
3746           ;; We have found a loop.
3747           (let (ref-dep)
3748             (setcdr thread (delq (car th) (cdr thread)))
3749             (if (boundp (setq ref-dep (intern "none"
3750                                               gnus-newsgroup-dependencies)))
3751                 (setcdr (symbol-value ref-dep)
3752                         (nconc (cdr (symbol-value ref-dep))
3753                                (list (car th))))
3754               (set ref-dep (list nil (car th))))
3755             (setq infloop 1
3756                   stack nil))
3757         ;; Push all the subthreads onto the stack.
3758         (push (cdr thread) stack)))
3759     infloop))
3760
3761 (defun gnus-make-threads ()
3762   "Go through the dependency hashtb and find the roots.  Return all threads."
3763   (let (threads)
3764     (while (catch 'infloop
3765              (mapatoms
3766               (lambda (refs)
3767                 ;; Deal with self-referencing References loops.
3768                 (when (and (car (symbol-value refs))
3769                            (not (zerop
3770                                  (apply
3771                                   '+
3772                                   (mapcar
3773                                    (lambda (thread)
3774                                      (gnus-thread-loop-p
3775                                       (car (symbol-value refs)) thread))
3776                                    (cdr (symbol-value refs)))))))
3777                   (setq threads nil)
3778                   (throw 'infloop t))
3779                 (unless (car (symbol-value refs))
3780                   ;; These threads do not refer back to any other articles,
3781                   ;; so they're roots.
3782                   (setq threads (append (cdr (symbol-value refs)) threads))))
3783               gnus-newsgroup-dependencies)))
3784     threads))
3785
3786 ;; Build the thread tree.
3787 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3788   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3789
3790 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3791 if it was already present.
3792
3793 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3794 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3795 Message-IDs will be renamed to a unique Message-ID before being
3796 entered.
3797
3798 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3799   (let* ((id (mail-header-id header))
3800          (id-dep (and id (intern id dependencies)))
3801          parent-id ref ref-dep ref-header replaced)
3802     ;; Enter this `header' in the `dependencies' table.
3803     (cond
3804      ((not id-dep)
3805       (setq header nil))
3806      ;; The first two cases do the normal part: enter a new `header'
3807      ;; in the `dependencies' table.
3808      ((not (boundp id-dep))
3809       (set id-dep (list header)))
3810      ((null (car (symbol-value id-dep)))
3811       (setcar (symbol-value id-dep) header))
3812
3813      ;; From here the `header' was already present in the
3814      ;; `dependencies' table.
3815      (force-new
3816       ;; Overrides an existing entry;
3817       ;; just set the header part of the entry.
3818       (setcar (symbol-value id-dep) header)
3819       (setq replaced t))
3820
3821      ;; Renames the existing `header' to a unique Message-ID.
3822      ((not gnus-summary-ignore-duplicates)
3823       ;; An article with this Message-ID has already been seen.
3824       ;; We rename the Message-ID.
3825       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3826            (list header))
3827       (mail-header-set-id header id))
3828
3829      ;; The last case ignores an existing entry, except it adds any
3830      ;; additional Xrefs (in case the two articles came from different
3831      ;; servers.
3832      ;; Also sets `header' to `nil' meaning that the `dependencies'
3833      ;; table was *not* modified.
3834      (t
3835       (mail-header-set-xref
3836        (car (symbol-value id-dep))
3837        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3838                    "")
3839                (or (mail-header-xref header) "")))
3840       (setq header nil)))
3841
3842     (when (and header (not replaced))
3843       ;; First check that we are not creating a References loop.
3844       (setq parent-id (gnus-parent-id (mail-header-references header)))
3845       (setq ref parent-id)
3846       (while (and ref
3847                   (setq ref-dep (intern-soft ref dependencies))
3848                   (boundp ref-dep)
3849                   (setq ref-header (car (symbol-value ref-dep))))
3850         (if (string= id ref)
3851             ;; Yuk!  This is a reference loop.  Make the article be a
3852             ;; root article.
3853             (progn
3854               (mail-header-set-references (car (symbol-value id-dep)) "none")
3855               (setq ref nil)
3856               (setq parent-id nil))
3857           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3858       (setq ref-dep (intern (or parent-id "none") dependencies))
3859       (if (boundp ref-dep)
3860           (setcdr (symbol-value ref-dep)
3861                   (nconc (cdr (symbol-value ref-dep))
3862                          (list (symbol-value id-dep))))
3863         (set ref-dep (list nil (symbol-value id-dep)))))
3864     header))
3865
3866 (defun gnus-extract-message-id-from-in-reply-to (string)
3867   (if (string-match "<[^>]+>" string)
3868       (substring string (match-beginning 0) (match-end 0))
3869     nil))
3870
3871 (defun gnus-build-sparse-threads ()
3872   (let ((headers gnus-newsgroup-headers)
3873         (mail-parse-charset gnus-newsgroup-charset)
3874         (gnus-summary-ignore-duplicates t)
3875         header references generation relations
3876         subject child end new-child date)
3877     ;; First we create an alist of generations/relations, where
3878     ;; generations is how much we trust the relation, and the relation
3879     ;; is parent/child.
3880     (gnus-message 7 "Making sparse threads...")
3881     (save-excursion
3882       (nnheader-set-temp-buffer " *gnus sparse threads*")
3883       (while (setq header (pop headers))
3884         (when (and (setq references (mail-header-references header))
3885                    (not (string= references "")))
3886           (insert references)
3887           (setq child (mail-header-id header)
3888                 subject (mail-header-subject header)
3889                 date (mail-header-date header)
3890                 generation 0)
3891           (while (search-backward ">" nil t)
3892             (setq end (1+ (point)))
3893             (when (search-backward "<" nil t)
3894               (setq new-child (buffer-substring (point) end))
3895               (push (list (incf generation)
3896                           child (setq child new-child)
3897                           subject date)
3898                     relations)))
3899           (when child
3900             (push (list (1+ generation) child nil subject) relations))
3901           (erase-buffer)))
3902       (kill-buffer (current-buffer)))
3903     ;; Sort over trustworthiness.
3904     (mapcar
3905      (lambda (relation)
3906        (when (gnus-dependencies-add-header
3907               (make-full-mail-header-from-decoded-header
3908                gnus-reffed-article-number
3909                (nth 3 relation) "" (or (nth 4 relation) "")
3910                (nth 1 relation)
3911                (or (nth 2 relation) "") 0 0 "")
3912               gnus-newsgroup-dependencies nil)
3913          (push gnus-reffed-article-number gnus-newsgroup-limit)
3914          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3915          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3916                gnus-newsgroup-reads)
3917          (decf gnus-reffed-article-number)))
3918      (sort relations 'car-less-than-car))
3919     (gnus-message 7 "Making sparse threads...done")))
3920
3921 (defun gnus-build-old-threads ()
3922   ;; Look at all the articles that refer back to old articles, and
3923   ;; fetch the headers for the articles that aren't there.  This will
3924   ;; build complete threads - if the roots haven't been expired by the
3925   ;; server, that is.
3926   (let ((mail-parse-charset gnus-newsgroup-charset)
3927         id heads)
3928     (mapatoms
3929      (lambda (refs)
3930        (when (not (car (symbol-value refs)))
3931          (setq heads (cdr (symbol-value refs)))
3932          (while heads
3933            (if (memq (mail-header-number (caar heads))
3934                      gnus-newsgroup-dormant)
3935                (setq heads (cdr heads))
3936              (setq id (symbol-name refs))
3937              (while (and (setq id (gnus-build-get-header id))
3938                          (not (car (gnus-id-to-thread id)))))
3939              (setq heads nil)))))
3940      gnus-newsgroup-dependencies)))
3941
3942 ;; This function has to be called with point after the article number
3943 ;; on the beginning of the line.
3944 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3945   (let ((eol (gnus-point-at-eol))
3946         (buffer (current-buffer))
3947         header references in-reply-to)
3948
3949     ;; overview: [num subject from date id refs chars lines misc]
3950     (unwind-protect
3951         (progn
3952           (narrow-to-region (point) eol)
3953           (unless (eobp)
3954             (forward-char))
3955
3956           (setq header
3957                 (make-full-mail-header
3958                  number                         ; number
3959                  (nnheader-nov-field)           ; subject
3960                  (nnheader-nov-field)           ; from
3961                  (nnheader-nov-field)           ; date
3962                  (nnheader-nov-read-message-id) ; id
3963                  (nnheader-nov-field)           ; refs
3964                  (nnheader-nov-read-integer)    ; chars
3965                  (nnheader-nov-read-integer)    ; lines
3966                  (unless (eobp)
3967                    (if (looking-at "Xref: ")
3968                        (goto-char (match-end 0)))
3969                    (nnheader-nov-field))        ; Xref
3970                  (nnheader-nov-parse-extra))))  ; extra
3971
3972       (widen))
3973
3974     (when (and (string= references "")
3975                (setq in-reply-to (mail-header-extra header))
3976                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3977       (mail-header-set-references
3978        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3979
3980     (when gnus-alter-header-function
3981       (funcall gnus-alter-header-function header))
3982     (gnus-dependencies-add-header header dependencies force-new)))
3983
3984 (defun gnus-build-get-header (id)
3985   "Look through the buffer of NOV lines and find the header to ID.
3986 Enter this line into the dependencies hash table, and return
3987 the id of the parent article (if any)."
3988   (let ((deps gnus-newsgroup-dependencies)
3989         found header)
3990     (prog1
3991         (save-excursion
3992           (set-buffer nntp-server-buffer)
3993           (let ((case-fold-search nil))
3994             (goto-char (point-min))
3995             (while (and (not found)
3996                         (search-forward id nil t))
3997               (beginning-of-line)
3998               (setq found (looking-at
3999                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4000                                    (regexp-quote id))))
4001               (or found (beginning-of-line 2)))
4002             (when found
4003               (beginning-of-line)
4004               (and
4005                (setq header (gnus-nov-parse-line
4006                              (read (current-buffer)) deps))
4007                (gnus-parent-id (mail-header-references header))))))
4008       (when header
4009         (let ((number (mail-header-number header)))
4010           (push number gnus-newsgroup-limit)
4011           (push header gnus-newsgroup-headers)
4012           (if (memq number gnus-newsgroup-unselected)
4013               (progn
4014                 (setq gnus-newsgroup-unreads
4015                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4016                                                number))
4017                 (setq gnus-newsgroup-unselected
4018                       (delq number gnus-newsgroup-unselected)))
4019             (push number gnus-newsgroup-ancient)))))))
4020
4021 (defun gnus-build-all-threads ()
4022   "Read all the headers."
4023   (let ((gnus-summary-ignore-duplicates t)
4024         (mail-parse-charset gnus-newsgroup-charset)
4025         (dependencies gnus-newsgroup-dependencies)
4026         header article)
4027     (save-excursion
4028       (set-buffer nntp-server-buffer)
4029       (let ((case-fold-search nil))
4030         (goto-char (point-min))
4031         (while (not (eobp))
4032           (ignore-errors
4033             (setq article (read (current-buffer))
4034                   header (gnus-nov-parse-line article dependencies)))
4035           (when header
4036             (save-excursion
4037               (set-buffer gnus-summary-buffer)
4038               (push header gnus-newsgroup-headers)
4039               (if (memq (setq article (mail-header-number header))
4040                         gnus-newsgroup-unselected)
4041                   (progn
4042                     (setq gnus-newsgroup-unreads
4043                           (gnus-add-to-sorted-list
4044                            gnus-newsgroup-unreads article))
4045                     (setq gnus-newsgroup-unselected
4046                           (delq article gnus-newsgroup-unselected)))
4047                 (push article gnus-newsgroup-ancient)))
4048             (forward-line 1)))))))
4049
4050 (defun gnus-summary-update-article-line (article header)
4051   "Update the line for ARTICLE using HEADERS."
4052   (let* ((id (mail-header-id header))
4053          (thread (gnus-id-to-thread id)))
4054     (unless thread
4055       (error "Article in no thread"))
4056     ;; Update the thread.
4057     (setcar thread header)
4058     (gnus-summary-goto-subject article)
4059     (let* ((datal (gnus-data-find-list article))
4060            (data (car datal))
4061            (buffer-read-only nil)
4062            (level (gnus-summary-thread-level)))
4063       (gnus-delete-line)
4064       (let ((inserted (- (point)
4065                          (progn
4066                            (gnus-summary-insert-line
4067                             header level nil 
4068                             (memq article gnus-newsgroup-undownloaded)
4069                             (gnus-article-mark article)
4070                             (memq article gnus-newsgroup-replied)
4071                             (memq article gnus-newsgroup-expirable)
4072                             ;; Only insert the Subject string when it's different
4073                             ;; from the previous Subject string.
4074                             (if (and
4075                                  gnus-show-threads
4076                                  (gnus-subject-equal
4077                                   (condition-case ()
4078                                       (mail-header-subject
4079                                        (gnus-data-header
4080                                         (cadr
4081                                          (gnus-data-find-list
4082                                           article
4083                                           (gnus-data-list t)))))
4084                                     ;; Error on the side of excessive subjects.
4085                                     (error ""))
4086                                   (mail-header-subject header)))
4087                                 ""
4088                               (mail-header-subject header))
4089                             nil (cdr (assq article gnus-newsgroup-scored))
4090                             (memq article gnus-newsgroup-processable))
4091                            (point)))))
4092         (when (cdr datal)
4093           (gnus-data-update-list
4094            (cdr datal) 
4095            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4096
4097 (defun gnus-summary-update-article (article &optional iheader)
4098   "Update ARTICLE in the summary buffer."
4099   (set-buffer gnus-summary-buffer)
4100   (let* ((header (gnus-summary-article-header article))
4101          (id (mail-header-id header))
4102          (data (gnus-data-find article))
4103          (thread (gnus-id-to-thread id))
4104          (references (mail-header-references header))
4105          (parent
4106           (gnus-id-to-thread
4107            (or (gnus-parent-id
4108                 (when (and references
4109                            (not (equal "" references)))
4110                   references))
4111                "none")))
4112          (buffer-read-only nil)
4113          (old (car thread)))
4114     (when thread
4115       (unless iheader
4116         (setcar thread nil)
4117         (when parent
4118           (delq thread parent)))
4119       (if (gnus-summary-insert-subject id header)
4120           ;; Set the (possibly) new article number in the data structure.
4121           (gnus-data-set-number data (gnus-id-to-article id))
4122         (setcar thread old)
4123         nil))))
4124
4125 (defun gnus-rebuild-thread (id &optional line)
4126   "Rebuild the thread containing ID.
4127 If LINE, insert the rebuilt thread starting on line LINE."
4128   (let ((buffer-read-only nil)
4129         old-pos current thread data)
4130     (if (not gnus-show-threads)
4131         (setq thread (list (car (gnus-id-to-thread id))))
4132       ;; Get the thread this article is part of.
4133       (setq thread (gnus-remove-thread id)))
4134     (setq old-pos (gnus-point-at-bol))
4135     (setq current (save-excursion
4136                     (and (re-search-backward "[\r\n]" nil t)
4137                          (gnus-summary-article-number))))
4138     ;; If this is a gathered thread, we have to go some re-gathering.
4139     (when (stringp (car thread))
4140       (let ((subject (car thread))
4141             roots thr)
4142         (setq thread (cdr thread))
4143         (while thread
4144           (unless (memq (setq thr (gnus-id-to-thread
4145                                    (gnus-root-id
4146                                     (mail-header-id (caar thread)))))
4147                         roots)
4148             (push thr roots))
4149           (setq thread (cdr thread)))
4150         ;; We now have all (unique) roots.
4151         (if (= (length roots) 1)
4152             ;; All the loose roots are now one solid root.
4153             (setq thread (car roots))
4154           (setq thread (cons subject (gnus-sort-threads roots))))))
4155     (let (threads)
4156       ;; We then insert this thread into the summary buffer.
4157       (when line
4158         (goto-char (point-min))
4159         (forward-line (1- line)))
4160       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4161         (if gnus-show-threads
4162             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4163           (gnus-summary-prepare-unthreaded thread))
4164         (setq data (nreverse gnus-newsgroup-data))
4165         (setq threads gnus-newsgroup-threads))
4166       ;; We splice the new data into the data structure.
4167       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4168       ;;!!! then we want to insert at the beginning of the buffer.
4169       ;;!!! That happens to be true with Gnus now, but that may
4170       ;;!!! change in the future.  Perhaps.
4171       (gnus-data-enter-list
4172        (if line nil current) data (- (point) old-pos))
4173       (setq gnus-newsgroup-threads
4174             (nconc threads gnus-newsgroup-threads))
4175       (gnus-data-compute-positions))))
4176
4177 (defun gnus-number-to-header (number)
4178   "Return the header for article NUMBER."
4179   (let ((headers gnus-newsgroup-headers))
4180     (while (and headers
4181                 (not (= number (mail-header-number (car headers)))))
4182       (pop headers))
4183     (when headers
4184       (car headers))))
4185
4186 (defun gnus-parent-headers (in-headers &optional generation)
4187   "Return the headers of the GENERATIONeth parent of HEADERS."
4188   (unless generation
4189     (setq generation 1))
4190   (let ((parent t)
4191         (headers in-headers)
4192         references)
4193     (while (and parent
4194                 (not (zerop generation))
4195                 (setq references (mail-header-references headers)))
4196       (setq headers (if (and references
4197                              (setq parent (gnus-parent-id references)))
4198                         (car (gnus-id-to-thread parent))
4199                       nil))
4200       (decf generation))
4201     (and (not (eq headers in-headers))
4202          headers)))
4203
4204 (defun gnus-id-to-thread (id)
4205   "Return the (sub-)thread where ID appears."
4206   (gnus-gethash id gnus-newsgroup-dependencies))
4207
4208 (defun gnus-id-to-article (id)
4209   "Return the article number of ID."
4210   (let ((thread (gnus-id-to-thread id)))
4211     (when (and thread
4212                (car thread))
4213       (mail-header-number (car thread)))))
4214
4215 (defun gnus-id-to-header (id)
4216   "Return the article headers of ID."
4217   (car (gnus-id-to-thread id)))
4218
4219 (defun gnus-article-displayed-root-p (article)
4220   "Say whether ARTICLE is a root(ish) article."
4221   (let ((level (gnus-summary-thread-level article))
4222         (refs (mail-header-references  (gnus-summary-article-header article)))
4223         particle)
4224     (cond
4225      ((null level) nil)
4226      ((zerop level) t)
4227      ((null refs) t)
4228      ((null (gnus-parent-id refs)) t)
4229      ((and (= 1 level)
4230            (null (setq particle (gnus-id-to-article
4231                                  (gnus-parent-id refs))))
4232            (null (gnus-summary-thread-level particle)))))))
4233
4234 (defun gnus-root-id (id)
4235   "Return the id of the root of the thread where ID appears."
4236   (let (last-id prev)
4237     (while (and id (setq prev (car (gnus-id-to-thread id))))
4238       (setq last-id id
4239             id (gnus-parent-id (mail-header-references prev))))
4240     last-id))
4241
4242 (defun gnus-articles-in-thread (thread)
4243   "Return the list of articles in THREAD."
4244   (cons (mail-header-number (car thread))
4245         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4246
4247 (defun gnus-remove-thread (id &optional dont-remove)
4248   "Remove the thread that has ID in it."
4249   (let (headers thread last-id)
4250     ;; First go up in this thread until we find the root.
4251     (setq last-id (gnus-root-id id)
4252           headers (message-flatten-list (gnus-id-to-thread last-id)))
4253     ;; We have now found the real root of this thread.  It might have
4254     ;; been gathered into some loose thread, so we have to search
4255     ;; through the threads to find the thread we wanted.
4256     (let ((threads gnus-newsgroup-threads)
4257           sub)
4258       (while threads
4259         (setq sub (car threads))
4260         (if (stringp (car sub))
4261             ;; This is a gathered thread, so we look at the roots
4262             ;; below it to find whether this article is in this
4263             ;; gathered root.
4264             (progn
4265               (setq sub (cdr sub))
4266               (while sub
4267                 (when (member (caar sub) headers)
4268                   (setq thread (car threads)
4269                         threads nil
4270                         sub nil))
4271                 (setq sub (cdr sub))))
4272           ;; It's an ordinary thread, so we check it.
4273           (when (eq (car sub) (car headers))
4274             (setq thread sub
4275                   threads nil)))
4276         (setq threads (cdr threads)))
4277       ;; If this article is in no thread, then it's a root.
4278       (if thread
4279           (unless dont-remove
4280             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4281         (setq thread (gnus-id-to-thread last-id)))
4282       (when thread
4283         (prog1
4284             thread                      ; We return this thread.
4285           (unless dont-remove
4286             (if (stringp (car thread))
4287                 (progn
4288                   ;; If we use dummy roots, then we have to remove the
4289                   ;; dummy root as well.
4290                   (when (eq gnus-summary-make-false-root 'dummy)
4291                     ;; We go to the dummy root by going to
4292                     ;; the first sub-"thread", and then one line up.
4293                     (gnus-summary-goto-article
4294                      (mail-header-number (caadr thread)))
4295                     (forward-line -1)
4296                     (gnus-delete-line)
4297                     (gnus-data-compute-positions))
4298                   (setq thread (cdr thread))
4299                   (while thread
4300                     (gnus-remove-thread-1 (car thread))
4301                     (setq thread (cdr thread))))
4302               (gnus-remove-thread-1 thread))))))))
4303
4304 (defun gnus-remove-thread-1 (thread)
4305   "Remove the thread THREAD recursively."
4306   (let ((number (mail-header-number (pop thread)))
4307         d)
4308     (setq thread (reverse thread))
4309     (while thread
4310       (gnus-remove-thread-1 (pop thread)))
4311     (when (setq d (gnus-data-find number))
4312       (goto-char (gnus-data-pos d))
4313       (gnus-summary-show-thread)
4314       (gnus-data-remove
4315        number
4316        (- (gnus-point-at-bol)
4317           (prog1
4318               (1+ (gnus-point-at-eol))
4319             (gnus-delete-line)))))))
4320
4321 (defun gnus-sort-threads-1 (threads func)
4322   (sort (mapcar (lambda (thread)
4323                   (cons (car thread)
4324                         (and (cdr thread)
4325                              (gnus-sort-threads-1 (cdr thread) func))))
4326                 threads) func))
4327
4328 (defun gnus-sort-threads (threads)
4329   "Sort THREADS."
4330   (if (not gnus-thread-sort-functions)
4331       threads
4332     (gnus-message 8 "Sorting threads...")
4333     (let ((max-lisp-eval-depth 5000))
4334       (prog1 (gnus-sort-threads-1
4335          threads
4336          (gnus-make-sort-function gnus-thread-sort-functions))
4337         (gnus-message 8 "Sorting threads...done")))))
4338
4339 (defun gnus-sort-articles (articles)
4340   "Sort ARTICLES."
4341   (when gnus-article-sort-functions
4342     (gnus-message 7 "Sorting articles...")
4343     (prog1
4344         (setq gnus-newsgroup-headers
4345               (sort articles (gnus-make-sort-function
4346                               gnus-article-sort-functions)))
4347       (gnus-message 7 "Sorting articles...done"))))
4348
4349 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4350 (defmacro gnus-thread-header (thread)
4351   "Return header of first article in THREAD.
4352 Note that THREAD must never, ever be anything else than a variable -
4353 using some other form will lead to serious barfage."
4354   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4355   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4356   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4357         (vector thread) 2))
4358
4359 (defsubst gnus-article-sort-by-number (h1 h2)
4360   "Sort articles by article number."
4361   (< (mail-header-number h1)
4362      (mail-header-number h2)))
4363
4364 (defun gnus-thread-sort-by-number (h1 h2)
4365   "Sort threads by root article number."
4366   (gnus-article-sort-by-number
4367    (gnus-thread-header h1) (gnus-thread-header h2)))
4368
4369 (defsubst gnus-article-sort-by-random (h1 h2)
4370   "Sort articles by article number."
4371   (zerop (random 2)))
4372
4373 (defun gnus-thread-sort-by-random (h1 h2)
4374   "Sort threads by root article number."
4375   (gnus-article-sort-by-random
4376    (gnus-thread-header h1) (gnus-thread-header h2)))
4377
4378 (defsubst gnus-article-sort-by-lines (h1 h2)
4379   "Sort articles by article Lines header."
4380   (< (mail-header-lines h1)
4381      (mail-header-lines h2)))
4382
4383 (defun gnus-thread-sort-by-lines (h1 h2)
4384   "Sort threads by root article Lines header."
4385   (gnus-article-sort-by-lines
4386    (gnus-thread-header h1) (gnus-thread-header h2)))
4387
4388 (defsubst gnus-article-sort-by-chars (h1 h2)
4389   "Sort articles by octet length."
4390   (< (mail-header-chars h1)
4391      (mail-header-chars h2)))
4392
4393 (defun gnus-thread-sort-by-chars (h1 h2)
4394   "Sort threads by root article octet length."
4395   (gnus-article-sort-by-chars
4396    (gnus-thread-header h1) (gnus-thread-header h2)))
4397
4398 (defsubst gnus-article-sort-by-author (h1 h2)
4399   "Sort articles by root author."
4400   (string-lessp
4401    (let ((addr (car (mime-entity-read-field h1 'From))))
4402      (or (std11-full-name-string addr)
4403          (std11-address-string addr)
4404          ""))
4405    (let ((addr (car (mime-entity-read-field h2 'From))))
4406      (or (std11-full-name-string addr)
4407          (std11-address-string addr)
4408          ""))
4409    ))
4410
4411 (defun gnus-thread-sort-by-author (h1 h2)
4412   "Sort threads by root author."
4413   (gnus-article-sort-by-author
4414    (gnus-thread-header h1)  (gnus-thread-header h2)))
4415
4416 (defsubst gnus-article-sort-by-subject (h1 h2)
4417   "Sort articles by root subject."
4418   (string-lessp
4419    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4420    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4421
4422 (defun gnus-thread-sort-by-subject (h1 h2)
4423   "Sort threads by root subject."
4424   (gnus-article-sort-by-subject
4425    (gnus-thread-header h1) (gnus-thread-header h2)))
4426
4427 (defsubst gnus-article-sort-by-date (h1 h2)
4428   "Sort articles by root article date."
4429   (time-less-p
4430    (gnus-date-get-time (mail-header-date h1))
4431    (gnus-date-get-time (mail-header-date h2))))
4432
4433 (defun gnus-thread-sort-by-date (h1 h2)
4434   "Sort threads by root article date."
4435   (gnus-article-sort-by-date
4436    (gnus-thread-header h1) (gnus-thread-header h2)))
4437
4438 (defsubst gnus-article-sort-by-score (h1 h2)
4439   "Sort articles by root article score.
4440 Unscored articles will be counted as having a score of zero."
4441   (> (or (cdr (assq (mail-header-number h1)
4442                     gnus-newsgroup-scored))
4443          gnus-summary-default-score 0)
4444      (or (cdr (assq (mail-header-number h2)
4445                     gnus-newsgroup-scored))
4446          gnus-summary-default-score 0)))
4447
4448 (defun gnus-thread-sort-by-score (h1 h2)
4449   "Sort threads by root article score."
4450   (gnus-article-sort-by-score
4451    (gnus-thread-header h1) (gnus-thread-header h2)))
4452
4453 (defun gnus-thread-sort-by-total-score (h1 h2)
4454   "Sort threads by the sum of all scores in the thread.
4455 Unscored articles will be counted as having a score of zero."
4456   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4457
4458 (defun gnus-thread-total-score (thread)
4459   ;; This function find the total score of THREAD.
4460   (cond
4461    ((null thread)
4462     0)
4463    ((consp thread)
4464     (if (stringp (car thread))
4465         (apply gnus-thread-score-function 0
4466                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4467       (gnus-thread-total-score-1 thread)))
4468    (t
4469     (gnus-thread-total-score-1 (list thread)))))
4470
4471 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4472   "Sort threads such that the thread with the most recently arrived article comes first."
4473   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4474
4475 (defun gnus-thread-highest-number (thread)
4476   "Return the highest article number in THREAD."
4477   (apply 'max (mapcar (lambda (header)
4478                         (mail-header-number header))
4479                       (message-flatten-list thread))))
4480
4481 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4482   "Sort threads such that the thread with the most recently dated article comes first."
4483   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4484
4485 (defun gnus-thread-latest-date (thread)
4486   "Return the highest article date in THREAD."
4487   (let ((previous-time 0))
4488     (apply 'max
4489            (mapcar
4490             (lambda (header)
4491               (setq previous-time
4492                     (time-to-seconds
4493                      (condition-case ()
4494                          (mail-header-parse-date (mail-header-date header))
4495                        (error previous-time)))))
4496             (sort
4497              (message-flatten-list thread)
4498              (lambda (h1 h2)
4499                (< (mail-header-number h1)
4500                   (mail-header-number h2))))))))
4501
4502 (defun gnus-thread-total-score-1 (root)
4503   ;; This function find the total score of the thread below ROOT.
4504   (setq root (car root))
4505   (apply gnus-thread-score-function
4506          (or (append
4507               (mapcar 'gnus-thread-total-score
4508                       (cdr (gnus-id-to-thread (mail-header-id root))))
4509               (when (> (mail-header-number root) 0)
4510                 (list (or (cdr (assq (mail-header-number root)
4511                                      gnus-newsgroup-scored))
4512                           gnus-summary-default-score 0))))
4513              (list gnus-summary-default-score)
4514              '(0))))
4515
4516 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4517 (defvar gnus-tmp-prev-subject nil)
4518 (defvar gnus-tmp-false-parent nil)
4519 (defvar gnus-tmp-root-expunged nil)
4520 (defvar gnus-tmp-dummy-line nil)
4521
4522 (eval-when-compile (defvar gnus-tmp-header))
4523 (defun gnus-extra-header (type &optional header)
4524   "Return the extra header of TYPE."
4525   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4526       ""))
4527
4528 (defvar gnus-tmp-thread-tree-header-string "")
4529
4530 (defcustom gnus-sum-thread-tree-root "> "
4531   "With %B spec, used for the root of a thread.
4532 If nil, use subject instead."
4533   :type 'string
4534   :group 'gnus-thread)
4535 (defcustom gnus-sum-thread-tree-single-indent ""
4536   "With %B spec, used for a thread with just one message.
4537 If nil, use subject instead."
4538   :type 'string
4539   :group 'gnus-thread)
4540 (defcustom gnus-sum-thread-tree-vertical "| "
4541   "With %B spec, used for drawing a vertical line."
4542   :type 'string
4543   :group 'gnus-thread)
4544 (defcustom gnus-sum-thread-tree-indent "  "
4545   "With %B spec, used for indenting."
4546   :type 'string
4547   :group 'gnus-thread)
4548 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4549   "With %B spec, used for a leaf with brothers."
4550   :type 'string
4551   :group 'gnus-thread)
4552 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4553   "With %B spec, used for a leaf without brothers."
4554   :type 'string
4555   :group 'gnus-thread)
4556
4557 (defun gnus-summary-prepare-threads (threads)
4558   "Prepare summary buffer from THREADS and indentation LEVEL.
4559 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4560 or a straight list of headers."
4561   (gnus-message 7 "Generating summary...")
4562
4563   (setq gnus-newsgroup-threads threads)
4564   (beginning-of-line)
4565
4566   (let ((gnus-tmp-level 0)
4567         (default-score (or gnus-summary-default-score 0))
4568         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4569         (building-line-count gnus-summary-display-while-building)
4570         (building-count (integerp gnus-summary-display-while-building))
4571         thread number subject stack state gnus-tmp-gathered beg-match
4572         new-roots gnus-tmp-new-adopts thread-end simp-subject
4573         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4574         gnus-tmp-replied gnus-tmp-subject-or-nil
4575         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4576         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4577         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4578         tree-stack)
4579
4580     (setq gnus-tmp-prev-subject nil
4581           gnus-tmp-thread-tree-header-string "")
4582
4583     (if (vectorp (car threads))
4584         ;; If this is a straight (sic) list of headers, then a
4585         ;; threaded summary display isn't required, so we just create
4586         ;; an unthreaded one.
4587         (gnus-summary-prepare-unthreaded threads)
4588
4589       ;; Do the threaded display.
4590
4591       (if gnus-summary-display-while-building
4592           (switch-to-buffer (buffer-name)))
4593       (while (or threads stack gnus-tmp-new-adopts new-roots)
4594
4595         (if (and (= gnus-tmp-level 0)
4596                  (or (not stack)
4597                      (= (caar stack) 0))
4598                  (not gnus-tmp-false-parent)
4599                  (or gnus-tmp-new-adopts new-roots))
4600             (if gnus-tmp-new-adopts
4601                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4602                       thread (list (car gnus-tmp-new-adopts))
4603                       gnus-tmp-header (caar thread)
4604                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4605               (when new-roots
4606                 (setq thread (list (car new-roots))
4607                       gnus-tmp-header (caar thread)
4608                       new-roots (cdr new-roots))))
4609
4610           (if threads
4611               ;; If there are some threads, we do them before the
4612               ;; threads on the stack.
4613               (setq thread threads
4614                     gnus-tmp-header (caar thread))
4615             ;; There were no current threads, so we pop something off
4616             ;; the stack.
4617             (setq state (car stack)
4618                   gnus-tmp-level (car state)
4619                   tree-stack (cadr state)
4620                   thread (caddr state)
4621                   stack (cdr stack)
4622                   gnus-tmp-header (caar thread))))
4623
4624         (setq gnus-tmp-false-parent nil)
4625         (setq gnus-tmp-root-expunged nil)
4626         (setq thread-end nil)
4627
4628         (if (stringp gnus-tmp-header)
4629             ;; The header is a dummy root.
4630             (cond
4631              ((eq gnus-summary-make-false-root 'adopt)
4632               ;; We let the first article adopt the rest.
4633               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4634                                                (cddar thread)))
4635               (setq gnus-tmp-gathered
4636                     (nconc (mapcar
4637                             (lambda (h) (mail-header-number (car h)))
4638                             (cddar thread))
4639                            gnus-tmp-gathered))
4640               (setq thread (cons (list (caar thread)
4641                                        (cadar thread))
4642                                  (cdr thread)))
4643               (setq gnus-tmp-level -1
4644                     gnus-tmp-false-parent t))
4645              ((eq gnus-summary-make-false-root 'empty)
4646               ;; We print adopted articles with empty subject fields.
4647               (setq gnus-tmp-gathered
4648                     (nconc (mapcar
4649                             (lambda (h) (mail-header-number (car h)))
4650                             (cddar thread))
4651                            gnus-tmp-gathered))
4652               (setq gnus-tmp-level -1))
4653              ((eq gnus-summary-make-false-root 'dummy)
4654               ;; We remember that we probably want to output a dummy
4655               ;; root.
4656               (setq gnus-tmp-dummy-line gnus-tmp-header)
4657               (setq gnus-tmp-prev-subject gnus-tmp-header))
4658              (t
4659               ;; We do not make a root for the gathered
4660               ;; sub-threads at all.
4661               (setq gnus-tmp-level -1)))
4662
4663           (setq number (mail-header-number gnus-tmp-header)
4664                 subject (mail-header-subject gnus-tmp-header)
4665                 simp-subject (gnus-simplify-subject-fully subject))
4666
4667           (cond
4668            ;; If the thread has changed subject, we might want to make
4669            ;; this subthread into a root.
4670            ((and (null gnus-thread-ignore-subject)
4671                  (not (zerop gnus-tmp-level))
4672                  gnus-tmp-prev-subject
4673                  (not (string= gnus-tmp-prev-subject simp-subject)))
4674             (setq new-roots (nconc new-roots (list (car thread)))
4675                   thread-end t
4676                   gnus-tmp-header nil))
4677            ;; If the article lies outside the current limit,
4678            ;; then we do not display it.
4679            ((not (memq number gnus-newsgroup-limit))
4680             (setq gnus-tmp-gathered
4681                   (nconc (mapcar
4682                           (lambda (h) (mail-header-number (car h)))
4683                           (cdar thread))
4684                          gnus-tmp-gathered))
4685             (setq gnus-tmp-new-adopts (if (cdar thread)
4686                                           (append gnus-tmp-new-adopts
4687                                                   (cdar thread))
4688                                         gnus-tmp-new-adopts)
4689                   thread-end t
4690                   gnus-tmp-header nil)
4691             (when (zerop gnus-tmp-level)
4692               (setq gnus-tmp-root-expunged t)))
4693            ;; Perhaps this article is to be marked as read?
4694            ((and gnus-summary-mark-below
4695                  (< (or (cdr (assq number gnus-newsgroup-scored))
4696                         default-score)
4697                     gnus-summary-mark-below)
4698                  ;; Don't touch sparse articles.
4699                  (not (gnus-summary-article-sparse-p number))
4700                  (not (gnus-summary-article-ancient-p number)))
4701             (setq gnus-newsgroup-unreads
4702                   (delq number gnus-newsgroup-unreads))
4703             (if gnus-newsgroup-auto-expire
4704                 (setq gnus-newsgroup-expirable
4705                       (gnus-add-to-sorted-list
4706                        gnus-newsgroup-expirable number))
4707               (push (cons number gnus-low-score-mark)
4708                     gnus-newsgroup-reads))))
4709
4710           (when gnus-tmp-header
4711             ;; We may have an old dummy line to output before this
4712             ;; article.
4713             (when (and gnus-tmp-dummy-line
4714                        (gnus-subject-equal
4715                         gnus-tmp-dummy-line
4716                         (mail-header-subject gnus-tmp-header)))
4717               (gnus-summary-insert-dummy-line
4718                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4719               (setq gnus-tmp-dummy-line nil))
4720
4721             ;; Compute the mark.
4722             (setq gnus-tmp-unread (gnus-article-mark number))
4723
4724             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4725                                   gnus-tmp-header gnus-tmp-level)
4726                   gnus-newsgroup-data)
4727
4728             ;; Actually insert the line.
4729             (setq
4730              gnus-tmp-subject-or-nil
4731              (cond
4732               ((and gnus-thread-ignore-subject
4733                     gnus-tmp-prev-subject
4734                     (not (string= gnus-tmp-prev-subject simp-subject)))
4735                subject)
4736               ((zerop gnus-tmp-level)
4737                (if (and (eq gnus-summary-make-false-root 'empty)
4738                         (memq number gnus-tmp-gathered)
4739                         gnus-tmp-prev-subject
4740                         (string= gnus-tmp-prev-subject simp-subject))
4741                    gnus-summary-same-subject
4742                  subject))
4743               (t gnus-summary-same-subject)))
4744             (if (and (eq gnus-summary-make-false-root 'adopt)
4745                      (= gnus-tmp-level 1)
4746                      (memq number gnus-tmp-gathered))
4747                 (setq gnus-tmp-opening-bracket ?\<
4748                       gnus-tmp-closing-bracket ?\>)
4749               (setq gnus-tmp-opening-bracket ?\[
4750                     gnus-tmp-closing-bracket ?\]))
4751             (setq
4752              gnus-tmp-indentation
4753              (aref gnus-thread-indent-array gnus-tmp-level)
4754              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4755              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4756                                 gnus-summary-default-score 0)
4757              gnus-tmp-score-char
4758              (if (or (null gnus-summary-default-score)
4759                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4760                          gnus-summary-zcore-fuzz))
4761                  ?\ ;;;Whitespace
4762                (if (< gnus-tmp-score gnus-summary-default-score)
4763                    gnus-score-below-mark gnus-score-over-mark))
4764              gnus-tmp-replied
4765              (cond ((memq number gnus-newsgroup-processable)
4766                     gnus-process-mark)
4767                    ((memq number gnus-newsgroup-cached)
4768                     gnus-cached-mark)
4769                    ((memq number gnus-newsgroup-replied)
4770                     gnus-replied-mark)
4771                    ((memq number gnus-newsgroup-forwarded)
4772                     gnus-forwarded-mark)
4773                    ((memq number gnus-newsgroup-saved)
4774                     gnus-saved-mark)
4775                    ((memq number gnus-newsgroup-recent)
4776                     gnus-recent-mark)
4777                    ((memq number gnus-newsgroup-unseen)
4778                     gnus-unseen-mark)
4779                    (t gnus-no-mark))
4780              gnus-tmp-downloaded
4781              (cond ((memq number gnus-newsgroup-undownloaded) 
4782                     gnus-undownloaded-mark)
4783                    (gnus-newsgroup-agentized
4784                     gnus-downloaded-mark)
4785                    (t
4786                     gnus-no-mark))
4787              gnus-tmp-from (mail-header-from gnus-tmp-header)
4788              gnus-tmp-name
4789              (cond
4790               ((string-match "<[^>]+> *$" gnus-tmp-from)
4791                (setq beg-match (match-beginning 0))
4792                (or (and (string-match "^\".+\"" gnus-tmp-from)
4793                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4794                    (substring gnus-tmp-from 0 beg-match)))
4795               ((string-match "(.+)" gnus-tmp-from)
4796                (substring gnus-tmp-from
4797                           (1+ (match-beginning 0)) (1- (match-end 0))))
4798               (t gnus-tmp-from))
4799              gnus-tmp-thread-tree-header-string
4800              (cond
4801               ((not gnus-show-threads) "")
4802               ((zerop gnus-tmp-level)
4803                (if (cdar thread)
4804                    (or gnus-sum-thread-tree-root subject)
4805                  (or gnus-sum-thread-tree-single-indent subject)))
4806               (t
4807                (concat (apply 'concat
4808                               (mapcar (lambda (item)
4809                                         (if (= item 1)
4810                                             gnus-sum-thread-tree-vertical
4811                                           gnus-sum-thread-tree-indent))
4812                                       (cdr (reverse tree-stack))))
4813                        (if (nth 1 thread)
4814                            gnus-sum-thread-tree-leaf-with-other
4815                          gnus-sum-thread-tree-single-leaf)))))
4816             (when (string= gnus-tmp-name "")
4817               (setq gnus-tmp-name gnus-tmp-from))
4818             (unless (numberp gnus-tmp-lines)
4819               (setq gnus-tmp-lines -1))
4820             (if (= gnus-tmp-lines -1)
4821                 (setq gnus-tmp-lines "?")
4822               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4823               (gnus-put-text-property
4824              (point)
4825              (progn (eval gnus-summary-line-format-spec) (point))
4826                'gnus-number number)
4827             (when gnus-visual-p
4828               (forward-line -1)
4829               (gnus-run-hooks 'gnus-summary-update-hook)
4830               (forward-line 1))
4831
4832             (setq gnus-tmp-prev-subject simp-subject)))
4833
4834         (when (nth 1 thread)
4835           (push (list (max 0 gnus-tmp-level)
4836                       (copy-list tree-stack)
4837                       (nthcdr 1 thread))
4838                 stack))
4839         (push (if (nth 1 thread) 1 0) tree-stack)
4840         (incf gnus-tmp-level)
4841         (setq threads (if thread-end nil (cdar thread)))
4842         (if gnus-summary-display-while-building
4843             (if building-count
4844                 (progn
4845                   ;; use a set frequency
4846                   (setq building-line-count (1- building-line-count))
4847                   (when (= building-line-count 0)
4848                     (sit-for 0)
4849                     (setq building-line-count
4850                           gnus-summary-display-while-building)))
4851               ;; always
4852               (sit-for 0)))
4853         (unless threads
4854           (setq gnus-tmp-level 0)))))
4855   (gnus-message 7 "Generating summary...done"))
4856
4857 (defun gnus-summary-prepare-unthreaded (headers)
4858   "Generate an unthreaded summary buffer based on HEADERS."
4859   (let (header number mark)
4860
4861     (beginning-of-line)
4862
4863     (while headers
4864       ;; We may have to root out some bad articles...
4865       (when (memq (setq number (mail-header-number
4866                                 (setq header (pop headers))))
4867                   gnus-newsgroup-limit)
4868         ;; Mark article as read when it has a low score.
4869         (when (and gnus-summary-mark-below
4870                    (< (or (cdr (assq number gnus-newsgroup-scored))
4871                           gnus-summary-default-score 0)
4872                       gnus-summary-mark-below)
4873                    (not (gnus-summary-article-ancient-p number)))
4874           (setq gnus-newsgroup-unreads
4875                 (delq number gnus-newsgroup-unreads))
4876           (if gnus-newsgroup-auto-expire
4877               (push number gnus-newsgroup-expirable)
4878             (push (cons number gnus-low-score-mark)
4879                   gnus-newsgroup-reads)))
4880
4881         (setq mark (gnus-article-mark number))
4882         (push (gnus-data-make number mark (1+ (point)) header 0)
4883               gnus-newsgroup-data)
4884         (gnus-summary-insert-line
4885          header 0 number
4886          (memq number gnus-newsgroup-undownloaded)
4887          mark (memq number gnus-newsgroup-replied)
4888          (memq number gnus-newsgroup-expirable)
4889          (mail-header-subject header) nil
4890          (cdr (assq number gnus-newsgroup-scored))
4891          (memq number gnus-newsgroup-processable))))))
4892
4893 (defun gnus-summary-remove-list-identifiers ()
4894   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4895   (let ((regexp (if (consp gnus-list-identifiers)
4896                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4897                   gnus-list-identifiers))
4898         changed subject)
4899     (when regexp
4900       (dolist (header gnus-newsgroup-headers)
4901         (setq subject (mail-header-subject header)
4902               changed nil)
4903         (while (string-match
4904                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4905                 subject)
4906           (setq subject
4907                 (concat (substring subject 0 (match-beginning 2))
4908                         (substring subject (match-end 0)))
4909                 changed t))
4910         (when (and changed
4911                    (string-match
4912                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4913           (setq subject
4914                 (concat (substring subject 0 (match-beginning 1))
4915                         (substring subject (match-end 1)))))
4916         (when changed
4917           (mail-header-set-subject header subject))))))
4918
4919 (defun gnus-fetch-headers (articles)
4920   "Fetch headers of ARTICLES."
4921   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4922     (gnus-message 5 "Fetching headers for %s..." name)
4923     (prog1
4924         (if (eq 'nov
4925                 (setq gnus-headers-retrieved-by
4926                       (gnus-retrieve-headers
4927                        articles gnus-newsgroup-name
4928                        ;; We might want to fetch old headers, but
4929                        ;; not if there is only 1 article.
4930                        (and (or (and
4931                                  (not (eq gnus-fetch-old-headers 'some))
4932                                  (not (numberp gnus-fetch-old-headers)))
4933                                 (> (length articles) 1))
4934                             gnus-fetch-old-headers))))
4935             (gnus-get-newsgroup-headers-xover
4936              articles nil nil gnus-newsgroup-name t)
4937           (gnus-get-newsgroup-headers))
4938       (gnus-message 5 "Fetching headers for %s...done" name))))
4939
4940 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4941   "Select newsgroup GROUP.
4942 If READ-ALL is non-nil, all articles in the group are selected.
4943 If SELECT-ARTICLES, only select those articles from GROUP."
4944   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4945          ;;!!! Dirty hack; should be removed.
4946          (gnus-summary-ignore-duplicates
4947           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4948               t
4949             gnus-summary-ignore-duplicates))
4950          (info (nth 2 entry))
4951          articles fetched-articles cached)
4952
4953     (unless (gnus-check-server
4954              (set (make-local-variable 'gnus-current-select-method)
4955                   (gnus-find-method-for-group group)))
4956       (error "Couldn't open server"))
4957
4958     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4959         (gnus-activate-group group)     ; Or we can activate it...
4960         (progn                          ; Or we bug out.
4961           (when (equal major-mode 'gnus-summary-mode)
4962             (gnus-kill-buffer (current-buffer)))
4963           (error "Couldn't activate group %s: %s"
4964                  group (gnus-status-message group))))
4965
4966     (unless (gnus-request-group group t)
4967       (when (equal major-mode 'gnus-summary-mode)
4968         (gnus-kill-buffer (current-buffer)))
4969       (error "Couldn't request group %s: %s"
4970              group (gnus-status-message group)))
4971
4972     (setq gnus-newsgroup-name group
4973           gnus-newsgroup-unselected nil
4974           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4975
4976     (let ((display (gnus-group-find-parameter group 'display)))
4977       (setq gnus-newsgroup-display
4978             (cond
4979              ((not (zerop (or (car-safe read-all) 0)))
4980               ;; The user entered the group with C-u SPC/RET, let's show
4981               ;; all articles.
4982               'gnus-not-ignore)
4983              ((eq display 'all)
4984               'gnus-not-ignore)
4985              ((arrayp display)
4986               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4987              ((numberp display)
4988               ;; The following is probably the "correct" solution, but
4989               ;; it makes Gnus fetch all headers and then limit the
4990               ;; articles (which is slow), so instead we hack the
4991               ;; select-articles parameter instead. -- Simon Josefsson
4992               ;; <jas@kth.se>
4993               ;;
4994               ;; (gnus-byte-compile
4995               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4996               ;;                         display)))))
4997               (setq select-articles
4998                     (gnus-uncompress-range
4999                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5000                              (if (> tmp 0)
5001                                  tmp
5002                                1))
5003                            (cdr (gnus-active group)))))
5004               nil)
5005              (t
5006               nil))))
5007
5008     (gnus-summary-setup-default-charset)
5009
5010     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5011     (when (gnus-virtual-group-p group)
5012       (setq cached gnus-newsgroup-cached))
5013
5014     (setq gnus-newsgroup-unreads
5015           (gnus-sorted-ndifference
5016            (gnus-sorted-ndifference gnus-newsgroup-unreads
5017                                     gnus-newsgroup-marked)
5018            gnus-newsgroup-dormant))
5019
5020     (setq gnus-newsgroup-processable nil)
5021
5022     (gnus-update-read-articles group gnus-newsgroup-unreads)
5023
5024     ;; Adjust and set lists of article marks.
5025     (when info
5026       (gnus-adjust-marked-articles info))
5027     (if (setq articles select-articles)
5028         (setq gnus-newsgroup-unselected
5029               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5030       (setq articles (gnus-articles-to-read group read-all)))
5031
5032     (cond
5033      ((null articles)
5034       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5035       'quit)
5036      ((eq articles 0) nil)
5037      (t
5038       ;; Init the dependencies hash table.
5039       (setq gnus-newsgroup-dependencies
5040             (gnus-make-hashtable (length articles)))
5041       (gnus-set-global-variables)
5042       ;; Retrieve the headers and read them in.
5043
5044       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5045
5046       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5047       (when cached
5048         (setq gnus-newsgroup-cached cached))
5049
5050       ;; Suppress duplicates?
5051       (when gnus-suppress-duplicates
5052         (gnus-dup-suppress-articles))
5053
5054       ;; Set the initial limit.
5055       (setq gnus-newsgroup-limit (copy-sequence articles))
5056       ;; Remove canceled articles from the list of unread articles.
5057       (setq fetched-articles
5058             (mapcar (lambda (headers) (mail-header-number headers))
5059                     gnus-newsgroup-headers))
5060       (setq gnus-newsgroup-articles fetched-articles)
5061       (setq gnus-newsgroup-unreads
5062             (gnus-sorted-nintersection
5063              gnus-newsgroup-unreads fetched-articles))
5064       (gnus-compute-unseen-list)
5065
5066       ;; Removed marked articles that do not exist.
5067       (gnus-update-missing-marks
5068        (gnus-sorted-difference articles fetched-articles))
5069       ;; We might want to build some more threads first.
5070       (when (and gnus-fetch-old-headers
5071                  (eq gnus-headers-retrieved-by 'nov))
5072         (if (eq gnus-fetch-old-headers 'invisible)
5073             (gnus-build-all-threads)
5074           (gnus-build-old-threads)))
5075       ;; Let the Gnus agent mark articles as read.
5076       (when gnus-agent
5077         (gnus-agent-get-undownloaded-list))
5078       ;; Remove list identifiers from subject
5079       (when gnus-list-identifiers
5080         (gnus-summary-remove-list-identifiers))
5081       ;; Check whether auto-expire is to be done in this group.
5082       (setq gnus-newsgroup-auto-expire
5083             (gnus-group-auto-expirable-p group))
5084       ;; Set up the article buffer now, if necessary.
5085       (unless gnus-single-article-buffer
5086         (gnus-article-setup-buffer))
5087       ;; First and last article in this newsgroup.
5088       (when gnus-newsgroup-headers
5089         (setq gnus-newsgroup-begin
5090               (mail-header-number (car gnus-newsgroup-headers))
5091               gnus-newsgroup-end
5092               (mail-header-number
5093                (gnus-last-element gnus-newsgroup-headers))))
5094       ;; GROUP is successfully selected.
5095       (or gnus-newsgroup-headers t)))))
5096
5097 (defun gnus-compute-unseen-list ()
5098   ;; The `seen' marks are treated specially.
5099   (if (not gnus-newsgroup-seen)
5100       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5101     (setq gnus-newsgroup-unseen
5102           (gnus-inverse-list-range-intersection
5103            gnus-newsgroup-articles gnus-newsgroup-seen))))
5104
5105 (defun gnus-summary-display-make-predicate (display)
5106   (require 'gnus-agent)
5107   (when (= (length display) 1)
5108     (setq display (car display)))
5109   (unless gnus-summary-display-cache
5110     (dolist (elem (append '((unread . unread)
5111                             (read . read)
5112                             (unseen . unseen))
5113                           gnus-article-mark-lists))
5114       (push (cons (cdr elem)
5115                   (gnus-byte-compile
5116                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5117             gnus-summary-display-cache)))
5118   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5119         (gnus-category-predicate-cache gnus-summary-display-cache))
5120     (gnus-get-predicate display)))
5121
5122 ;; Uses the dynamically bound `number' variable.
5123 (eval-when-compile
5124   (defvar number))
5125 (defun gnus-article-marked-p (type &optional article)
5126   (let ((article (or article number)))
5127     (cond
5128      ((eq type 'tick)
5129       (memq article gnus-newsgroup-marked))
5130      ((eq type 'spam)
5131       (memq article gnus-newsgroup-spam-marked))
5132      ((eq type 'unsend)
5133       (memq article gnus-newsgroup-unsendable))
5134      ((eq type 'undownload)
5135       (memq article gnus-newsgroup-undownloaded))
5136      ((eq type 'download)
5137       (memq article gnus-newsgroup-downloadable))
5138      ((eq type 'unread)
5139       (memq article gnus-newsgroup-unreads))
5140      ((eq type 'read)
5141       (memq article gnus-newsgroup-reads))
5142      ((eq type 'dormant)
5143       (memq article gnus-newsgroup-dormant) )
5144      ((eq type 'expire)
5145       (memq article gnus-newsgroup-expirable))
5146      ((eq type 'reply)
5147       (memq article gnus-newsgroup-replied))
5148      ((eq type 'killed)
5149       (memq article gnus-newsgroup-killed))
5150      ((eq type 'bookmark)
5151       (assq article gnus-newsgroup-bookmarks))
5152      ((eq type 'score)
5153       (assq article gnus-newsgroup-scored))
5154      ((eq type 'save)
5155       (memq article gnus-newsgroup-saved))
5156      ((eq type 'cache)
5157       (memq article gnus-newsgroup-cached))
5158      ((eq type 'forward)
5159       (memq article gnus-newsgroup-forwarded))
5160      ((eq type 'seen)
5161       (not (memq article gnus-newsgroup-unseen)))
5162      ((eq type 'recent)
5163       (memq article gnus-newsgroup-recent))
5164      (t t))))
5165
5166 (defun gnus-articles-to-read (group &optional read-all)
5167   "Find out what articles the user wants to read."
5168   (let* ((display (gnus-group-find-parameter group 'display))
5169          (articles
5170           ;; Select all articles if `read-all' is non-nil, or if there
5171           ;; are no unread articles.
5172           (if (or read-all
5173                   (and (zerop (length gnus-newsgroup-marked))
5174                        (zerop (length gnus-newsgroup-unreads)))
5175                   ;; Fetch all if the predicate is non-nil.
5176                   gnus-newsgroup-display)
5177               ;; We want to select the headers for all the articles in
5178               ;; the group, so we select either all the active
5179               ;; articles in the group, or (if that's nil), the
5180               ;; articles in the cache.
5181               (or
5182                (gnus-uncompress-range (gnus-active group))
5183                (gnus-cache-articles-in-group group))
5184             ;; Select only the "normal" subset of articles.
5185             (gnus-sorted-nunion
5186              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5187              gnus-newsgroup-unreads)))
5188          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5189          (scored (length scored-list))
5190          (number (length articles))
5191          (marked (+ (length gnus-newsgroup-marked)
5192                     (length gnus-newsgroup-dormant)))
5193          (select
5194           (cond
5195            ((numberp read-all)
5196             read-all)
5197            ((numberp gnus-newsgroup-display)
5198             gnus-newsgroup-display)
5199            (t
5200             (condition-case ()
5201                 (cond
5202                  ((and (or (<= scored marked) (= scored number))
5203                        (numberp gnus-large-newsgroup)
5204                        (> number gnus-large-newsgroup))
5205                   (let* ((cursor-in-echo-area nil)
5206                          (initial (gnus-parameter-large-newsgroup-initial
5207                                    gnus-newsgroup-name))
5208                          (input
5209                           (read-string
5210                            (format
5211                             "How many articles from %s (%s %d): "
5212                             (gnus-limit-string
5213                              (gnus-group-decoded-name gnus-newsgroup-name)
5214                              35)
5215                             (if initial "max" "default")
5216                             number)
5217                            (if initial
5218                                (cons (number-to-string initial)
5219                                      0)))))
5220                     (if (string-match "^[ \t]*$" input) number input)))
5221                  ((and (> scored marked) (< scored number)
5222                        (> (- scored number) 20))
5223                   (let ((input
5224                          (read-string
5225                           (format "%s %s (%d scored, %d total): "
5226                                   "How many articles from"
5227                                   (gnus-group-decoded-name group)
5228                                   scored number))))
5229                     (if (string-match "^[ \t]*$" input)
5230                         number input)))
5231                  (t number))
5232               (quit
5233                (message "Quit getting the articles to read")
5234                nil))))))
5235     (setq select (if (stringp select) (string-to-number select) select))
5236     (if (or (null select) (zerop select))
5237         select
5238       (if (and (not (zerop scored)) (<= (abs select) scored))
5239           (progn
5240             (setq articles (sort scored-list '<))
5241             (setq number (length articles)))
5242         (setq articles (copy-sequence articles)))
5243
5244       (when (< (abs select) number)
5245         (if (< select 0)
5246             ;; Select the N oldest articles.
5247             (setcdr (nthcdr (1- (abs select)) articles) nil)
5248           ;; Select the N most recent articles.
5249           (setq articles (nthcdr (- number select) articles))))
5250       (setq gnus-newsgroup-unselected
5251             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5252       (when gnus-alter-articles-to-read-function
5253         (setq articles
5254               (sort
5255                (funcall gnus-alter-articles-to-read-function
5256                         gnus-newsgroup-name articles)
5257                '<)))
5258       articles)))
5259
5260 (defun gnus-killed-articles (killed articles)
5261   (let (out)
5262     (while articles
5263       (when (inline (gnus-member-of-range (car articles) killed))
5264         (push (car articles) out))
5265       (setq articles (cdr articles)))
5266     out))
5267
5268 (defun gnus-uncompress-marks (marks)
5269   "Uncompress the mark ranges in MARKS."
5270   (let ((uncompressed '(score bookmark))
5271         out)
5272     (while marks
5273       (if (memq (caar marks) uncompressed)
5274           (push (car marks) out)
5275         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5276       (setq marks (cdr marks)))
5277     out))
5278
5279 (defun gnus-article-mark-to-type (mark)
5280   "Return the type of MARK."
5281   (or (cadr (assq mark gnus-article-special-mark-lists))
5282       'list))
5283
5284 (defun gnus-article-unpropagatable-p (mark)
5285   "Return whether MARK should be propagated to backend."
5286   (memq mark gnus-article-unpropagated-mark-lists))
5287
5288 (defun gnus-adjust-marked-articles (info)
5289   "Set all article lists and remove all marks that are no longer valid."
5290   (let* ((marked-lists (gnus-info-marks info))
5291          (active (gnus-active (gnus-info-group info)))
5292          (min (car active))
5293          (max (cdr active))
5294          (types gnus-article-mark-lists)
5295          marks var articles article mark mark-type)
5296
5297     (dolist (marks marked-lists)
5298       (setq mark (car marks)
5299             mark-type (gnus-article-mark-to-type mark)
5300             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5301
5302       ;; We set the variable according to the type of the marks list,
5303       ;; and then adjust the marks to a subset of the active articles.
5304       (cond
5305        ;; Adjust "simple" lists.
5306        ((eq mark-type 'list)
5307         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5308         (when (memq mark '(tick dormant expire reply save))
5309           (while articles
5310             (when (or (< (setq article (pop articles)) min) (> article max))
5311               (set var (delq article (symbol-value var)))))))
5312        ;; Adjust assocs.
5313        ((eq mark-type 'tuple)
5314         (set var (setq articles (cdr marks)))
5315         (when (not (listp (cdr (symbol-value var))))
5316           (set var (list (symbol-value var))))
5317         (when (not (listp (cdr articles)))
5318           (setq articles (list articles)))
5319         (while articles
5320           (when (or (not (consp (setq article (pop articles))))
5321                     (< (car article) min)
5322                     (> (car article) max))
5323             (set var (delq article (symbol-value var))))))
5324        ;; Adjust ranges (sloppily).
5325        ((eq mark-type 'range)
5326         (cond
5327          ((eq mark 'seen)
5328           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5329           ;; It should be (seen (NUM1 . NUM2)).
5330           (when (numberp (cddr marks))
5331             (setcdr marks (list (cdr marks))))
5332           (setq articles (cdr marks))
5333           (while (and articles
5334                       (or (and (consp (car articles))
5335                                (> min (cdar articles)))
5336                           (and (numberp (car articles))
5337                                (> min (car articles)))))
5338             (pop articles))
5339           (set var articles))))))))
5340
5341 (defun gnus-update-missing-marks (missing)
5342   "Go through the list of MISSING articles and remove them from the mark lists."
5343   (when missing
5344     (let (var m)
5345       ;; Go through all types.
5346       (dolist (elem gnus-article-mark-lists)
5347         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5348           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5349           (when (symbol-value var)
5350             ;; This list has articles.  So we delete all missing
5351             ;; articles from it.
5352             (setq m missing)
5353             (while m
5354               (set var (delq (pop m) (symbol-value var))))))))))
5355
5356 (defun gnus-update-marks ()
5357   "Enter the various lists of marked articles into the newsgroup info list."
5358   (let ((types gnus-article-mark-lists)
5359         (info (gnus-get-info gnus-newsgroup-name))
5360         type list newmarked symbol delta-marks)
5361     (when info
5362       ;; Add all marks lists to the list of marks lists.
5363       (while (setq type (pop types))
5364         (setq list (symbol-value
5365                     (setq symbol
5366                           (intern (format "gnus-newsgroup-%s" (car type))))))
5367
5368         (when list
5369           ;; Get rid of the entries of the articles that have the
5370           ;; default score.
5371           (when (and (eq (cdr type) 'score)
5372                      gnus-save-score
5373                      list)
5374             (let* ((arts list)
5375                    (prev (cons nil list))
5376                    (all prev))
5377               (while arts
5378                 (if (or (not (consp (car arts)))
5379                         (= (cdar arts) gnus-summary-default-score))
5380                     (setcdr prev (cdr arts))
5381                   (setq prev arts))
5382                 (setq arts (cdr arts)))
5383               (setq list (cdr all)))))
5384
5385         (when (eq (cdr type) 'seen)
5386           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5387
5388         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5389           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5390
5391         (when (and (gnus-check-backend-function
5392                     'request-set-mark gnus-newsgroup-name)
5393                    (not (gnus-article-unpropagatable-p (cdr type))))
5394           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5395                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5396                  (add (gnus-remove-from-range
5397                        (gnus-copy-sequence list) old)))
5398             (when add
5399               (push (list add 'add (list (cdr type))) delta-marks))
5400             (when del
5401               (push (list del 'del (list (cdr type))) delta-marks))))
5402
5403         (when list
5404           (push (cons (cdr type) list) newmarked)))
5405
5406       (when delta-marks
5407         (unless (gnus-check-group gnus-newsgroup-name)
5408           (error "Can't open server for %s" gnus-newsgroup-name))
5409         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5410
5411       ;; Enter these new marks into the info of the group.
5412       (if (nthcdr 3 info)
5413           (setcar (nthcdr 3 info) newmarked)
5414         ;; Add the marks lists to the end of the info.
5415         (when newmarked
5416           (setcdr (nthcdr 2 info) (list newmarked))))
5417
5418       ;; Cut off the end of the info if there's nothing else there.
5419       (let ((i 5))
5420         (while (and (> i 2)
5421                     (not (nth i info)))
5422           (when (nthcdr (decf i) info)
5423             (setcdr (nthcdr i info) nil)))))))
5424
5425 (defun gnus-set-mode-line (where)
5426   "Set the mode line of the article or summary buffers.
5427 If WHERE is `summary', the summary mode line format will be used."
5428   ;; Is this mode line one we keep updated?
5429   (when (and (memq where gnus-updated-mode-lines)
5430              (symbol-value
5431               (intern (format "gnus-%s-mode-line-format-spec" where))))
5432     (let (mode-string)
5433       (save-excursion
5434         ;; We evaluate this in the summary buffer since these
5435         ;; variables are buffer-local to that buffer.
5436         (set-buffer gnus-summary-buffer)
5437         ;; We bind all these variables that are used in the `eval' form
5438         ;; below.
5439         (let* ((mformat (symbol-value
5440                          (intern
5441                           (format "gnus-%s-mode-line-format-spec" where))))
5442                (gnus-tmp-group-name (gnus-group-decoded-name
5443                                      gnus-newsgroup-name))
5444                (gnus-tmp-article-number (or gnus-current-article 0))
5445                (gnus-tmp-unread gnus-newsgroup-unreads)
5446                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5447                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5448                (gnus-tmp-unread-and-unselected
5449                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5450                             (zerop gnus-tmp-unselected))
5451                        "")
5452                       ((zerop gnus-tmp-unselected)
5453                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5454                       (t (format "{%d(+%d) more}"
5455                                  gnus-tmp-unread-and-unticked
5456                                  gnus-tmp-unselected))))
5457                (gnus-tmp-subject
5458                 (if (and gnus-current-headers
5459                          (vectorp gnus-current-headers))
5460                     (gnus-mode-string-quote
5461                      (mail-header-subject gnus-current-headers))
5462                   ""))
5463                bufname-length max-len
5464                gnus-tmp-header);; passed as argument to any user-format-funcs
5465           (setq mode-string (eval mformat))
5466           (setq bufname-length (if (string-match "%b" mode-string)
5467                                    (- (length
5468                                        (buffer-name
5469                                         (if (eq where 'summary)
5470                                             nil
5471                                           (get-buffer gnus-article-buffer))))
5472                                       2)
5473                                  0))
5474           (setq max-len (max 4 (if gnus-mode-non-string-length
5475                                    (- (window-width)
5476                                       gnus-mode-non-string-length
5477                                       bufname-length)
5478                                  (length mode-string))))
5479           ;; We might have to chop a bit of the string off...
5480           (when (> (length mode-string) max-len)
5481             (setq mode-string
5482                   (concat (gnus-truncate-string mode-string (- max-len 3))
5483                           "...")))
5484           ;; Pad the mode string a bit.
5485           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5486       ;; Update the mode line.
5487       (setq mode-line-buffer-identification
5488             (gnus-mode-line-buffer-identification (list mode-string)))
5489       (set-buffer-modified-p t))))
5490
5491 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5492   "Go through the HEADERS list and add all Xrefs to a hash table.
5493 The resulting hash table is returned, or nil if no Xrefs were found."
5494   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5495          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5496          (xref-hashtb (gnus-make-hashtable))
5497          start group entry number xrefs header)
5498     (while headers
5499       (setq header (pop headers))
5500       (when (and (setq xrefs (mail-header-xref header))
5501                  (not (memq (setq number (mail-header-number header))
5502                             unreads)))
5503         (setq start 0)
5504         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5505           (setq start (match-end 0))
5506           (setq group (if prefix
5507                           (concat prefix (substring xrefs (match-beginning 1)
5508                                                     (match-end 1)))
5509                         (substring xrefs (match-beginning 1) (match-end 1))))
5510           (setq number
5511                 (string-to-int (substring xrefs (match-beginning 2)
5512                                           (match-end 2))))
5513           (if (setq entry (gnus-gethash group xref-hashtb))
5514               (setcdr entry (cons number (cdr entry)))
5515             (gnus-sethash group (cons number nil) xref-hashtb)))))
5516     (and start xref-hashtb)))
5517
5518 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5519   "Look through all the headers and mark the Xrefs as read."
5520   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5521         name entry info xref-hashtb idlist method nth4)
5522     (save-excursion
5523       (set-buffer gnus-group-buffer)
5524       (when (setq xref-hashtb
5525                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5526         (mapatoms
5527          (lambda (group)
5528            (unless (string= from-newsgroup (setq name (symbol-name group)))
5529              (setq idlist (symbol-value group))
5530              ;; Dead groups are not updated.
5531              (and (prog1
5532                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5533                             info (nth 2 entry))
5534                     (when (stringp (setq nth4 (gnus-info-method info)))
5535                       (setq nth4 (gnus-server-to-method nth4))))
5536                   ;; Only do the xrefs if the group has the same
5537                   ;; select method as the group we have just read.
5538                   (or (gnus-methods-equal-p
5539                        nth4 (gnus-find-method-for-group from-newsgroup))
5540                       virtual
5541                       (equal nth4 (setq method (gnus-find-method-for-group
5542                                                 from-newsgroup)))
5543                       (and (equal (car nth4) (car method))
5544                            (equal (nth 1 nth4) (nth 1 method))))
5545                   gnus-use-cross-reference
5546                   (or (not (eq gnus-use-cross-reference t))
5547                       virtual
5548                       ;; Only do cross-references on subscribed
5549                       ;; groups, if that is what is wanted.
5550                       (<= (gnus-info-level info) gnus-level-subscribed))
5551                   (gnus-group-make-articles-read name idlist))))
5552          xref-hashtb)))))
5553
5554 (defun gnus-compute-read-articles (group articles)
5555   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5556          (info (nth 2 entry))
5557          (active (gnus-active group))
5558          ninfo)
5559     (when entry
5560       ;; First peel off all invalid article numbers.
5561       (when active
5562         (let ((ids articles)
5563               id first)
5564           (while (setq id (pop ids))
5565             (when (and first (> id (cdr active)))
5566               ;; We'll end up in this situation in one particular
5567               ;; obscure situation.  If you re-scan a group and get
5568               ;; a new article that is cross-posted to a different
5569               ;; group that has not been re-scanned, you might get
5570               ;; crossposted article that has a higher number than
5571               ;; Gnus believes possible.  So we re-activate this
5572               ;; group as well.  This might mean doing the
5573               ;; crossposting thingy will *increase* the number
5574               ;; of articles in some groups.  Tsk, tsk.
5575               (setq active (or (gnus-activate-group group) active)))
5576             (when (or (> id (cdr active))
5577                       (< id (car active)))
5578               (setq articles (delq id articles))))))
5579       ;; If the read list is nil, we init it.
5580       (if (and active
5581                (null (gnus-info-read info))
5582                (> (car active) 1))
5583           (setq ninfo (cons 1 (1- (car active))))
5584         (setq ninfo (gnus-info-read info)))
5585       ;; Then we add the read articles to the range.
5586       (gnus-add-to-range
5587        ninfo (setq articles (sort articles '<))))))
5588
5589 (defun gnus-group-make-articles-read (group articles)
5590   "Update the info of GROUP to say that ARTICLES are read."
5591   (let* ((num 0)
5592          (entry (gnus-gethash group gnus-newsrc-hashtb))
5593          (info (nth 2 entry))
5594          (active (gnus-active group))
5595          range)
5596     (when entry
5597       (setq range (gnus-compute-read-articles group articles))
5598       (save-excursion
5599         (set-buffer gnus-group-buffer)
5600         (gnus-undo-register
5601           `(progn
5602              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5603              (gnus-info-set-read ',info ',(gnus-info-read info))
5604              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5605              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5606              (gnus-group-update-group ,group t))))
5607       ;; Add the read articles to the range.
5608       (gnus-info-set-read info range)
5609       (gnus-request-set-mark group (list (list range 'add '(read))))
5610       ;; Then we have to re-compute how many unread
5611       ;; articles there are in this group.
5612       (when active
5613         (cond
5614          ((not range)
5615           (setq num (- (1+ (cdr active)) (car active))))
5616          ((not (listp (cdr range)))
5617           (setq num (- (cdr active) (- (1+ (cdr range))
5618                                        (car range)))))
5619          (t
5620           (while range
5621             (if (numberp (car range))
5622                 (setq num (1+ num))
5623               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5624             (setq range (cdr range)))
5625           (setq num (- (cdr active) num))))
5626         ;; Update the number of unread articles.
5627         (setcar entry num)
5628         ;; Update the group buffer.
5629         (unless (gnus-ephemeral-group-p group)
5630           (gnus-group-update-group group t))))))
5631
5632 (defvar gnus-newsgroup-none-id 0)
5633
5634 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5635   (let ((cur nntp-server-buffer)
5636         (dependencies
5637          (or dependencies
5638              (save-excursion (set-buffer gnus-summary-buffer)
5639                              gnus-newsgroup-dependencies)))
5640         headers id end ref
5641         (mail-parse-charset gnus-newsgroup-charset)
5642         (mail-parse-ignored-charsets
5643          (save-excursion (condition-case nil
5644                              (set-buffer gnus-summary-buffer)
5645                            (error))
5646                          gnus-newsgroup-ignored-charsets)))
5647     (save-excursion
5648       (set-buffer nntp-server-buffer)
5649       ;; Translate all TAB characters into SPACE characters.
5650       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5651       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5652       (gnus-run-hooks 'gnus-parse-headers-hook)
5653       (let ((case-fold-search t)
5654             in-reply-to header p lines chars ctype)
5655         (goto-char (point-min))
5656         ;; Search to the beginning of the next header.  Error messages
5657         ;; do not begin with 2 or 3.
5658         (while (re-search-forward "^[23][0-9]+ " nil t)
5659           (setq id nil
5660                 ref nil)
5661           ;; This implementation of this function, with nine
5662           ;; search-forwards instead of the one re-search-forward and
5663           ;; a case (which basically was the old function) is actually
5664           ;; about twice as fast, even though it looks messier.  You
5665           ;; can't have everything, I guess.  Speed and elegance
5666           ;; doesn't always go hand in hand.
5667           (setq
5668            header
5669            (make-full-mail-header
5670             ;; Number.
5671             (prog1
5672                 (read cur)
5673               (end-of-line)
5674               (setq p (point))
5675               (narrow-to-region (point)
5676                                 (or (and (search-forward "\n.\n" nil t)
5677                                          (- (point) 2))
5678                                     (point))))
5679             ;; Subject.
5680             (progn
5681               (goto-char p)
5682               (if (search-forward "\nsubject:" nil t)
5683                   (nnheader-header-value)
5684                 "(none)"))
5685             ;; From.
5686             (progn
5687               (goto-char p)
5688               (if (search-forward "\nfrom:" nil t)
5689                   (nnheader-header-value)
5690                 "(nobody)"))
5691             ;; Date.
5692             (progn
5693               (goto-char p)
5694               (if (search-forward "\ndate:" nil t)
5695                   (nnheader-header-value) ""))
5696             ;; Message-ID.
5697             (progn
5698               (goto-char p)
5699               (setq id (if (re-search-forward
5700                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5701                            ;; We do it this way to make sure the Message-ID
5702                            ;; is (somewhat) syntactically valid.
5703                            (buffer-substring (match-beginning 1)
5704                                              (match-end 1))
5705                          ;; If there was no message-id, we just fake one
5706                          ;; to make subsequent routines simpler.
5707                          (nnheader-generate-fake-message-id))))
5708             ;; References.
5709             (progn
5710               (goto-char p)
5711               (if (search-forward "\nreferences:" nil t)
5712                   (progn
5713                     (setq end (point))
5714                     (prog1
5715                         (nnheader-header-value)
5716                       (setq ref
5717                             (buffer-substring
5718                              (progn
5719                                ;; (end-of-line)
5720                                (search-backward ">" end t)
5721                                (1+ (point)))
5722                              (progn
5723                                (search-backward "<" end t)
5724                                (point))))))
5725                 ;; Get the references from the in-reply-to header if there
5726                 ;; were no references and the in-reply-to header looks
5727                 ;; promising.
5728                 (if (and (search-forward "\nin-reply-to:" nil t)
5729                          (setq in-reply-to (nnheader-header-value))
5730                          (string-match "<[^>]+>" in-reply-to))
5731                     (let (ref2)
5732                       (setq ref (substring in-reply-to (match-beginning 0)
5733                                            (match-end 0)))
5734                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5735                         (setq ref2 (substring in-reply-to (match-beginning 0)
5736                                               (match-end 0)))
5737                         (when (> (length ref2) (length ref))
5738                           (setq ref ref2)))
5739                       ref)
5740                   (setq ref nil))))
5741             ;; Chars.
5742             (progn
5743               (goto-char p)
5744               (if (search-forward "\nchars: " nil t)
5745                   (if (numberp (setq chars (ignore-errors (read cur))))
5746                       chars -1)
5747                 -1))
5748             ;; Lines.
5749             (progn
5750               (goto-char p)
5751               (if (search-forward "\nlines: " nil t)
5752                   (if (numberp (setq lines (ignore-errors (read cur))))
5753                       lines -1)
5754                 -1))
5755             ;; Xref.
5756             (progn
5757               (goto-char p)
5758               (and (search-forward "\nxref:" nil t)
5759                    (nnheader-header-value)))
5760             ;; Extra.
5761             (when gnus-extra-headers
5762               (let ((extra gnus-extra-headers)
5763                     out)
5764                 (while extra
5765                   (goto-char p)
5766                   (when (search-forward
5767                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5768                     (push (cons (car extra) (nnheader-header-value)) out))
5769                   (pop extra))
5770                 out))))
5771           (goto-char p)
5772           (if (and (search-forward "\ncontent-type: " nil t)
5773                    (setq ctype (nnheader-header-value)))
5774               (mime-entity-set-content-type-internal
5775                header (mime-parse-Content-Type ctype)))
5776           (when (equal id ref)
5777             (setq ref nil))
5778
5779           (when gnus-alter-header-function
5780             (funcall gnus-alter-header-function header)
5781             (setq id (mail-header-id header)
5782                   ref (gnus-parent-id (mail-header-references header))))
5783
5784           (when (setq header
5785                       (gnus-dependencies-add-header
5786                        header dependencies force-new))
5787             (push header headers))
5788           (goto-char (point-max))
5789           (widen))
5790         (nreverse headers)))))
5791
5792 ;; Goes through the xover lines and returns a list of vectors
5793 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5794                                                   force-new dependencies
5795                                                   group also-fetch-heads)
5796   "Parse the news overview data in the server buffer.
5797 Return a list of headers that match SEQUENCE (see
5798 `nntp-retrieve-headers')."
5799   ;; Get the Xref when the users reads the articles since most/some
5800   ;; NNTP servers do not include Xrefs when using XOVER.
5801   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5802   (let ((mail-parse-charset gnus-newsgroup-charset)
5803         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5804         (cur nntp-server-buffer)
5805         (dependencies (or dependencies gnus-newsgroup-dependencies))
5806         (allp (cond
5807                ((eq gnus-read-all-available-headers t)
5808                 t)
5809                ((stringp gnus-read-all-available-headers)
5810                 (string-match gnus-read-all-available-headers group))
5811                (t
5812                 nil)))
5813         number headers header)
5814     (save-excursion
5815       (set-buffer nntp-server-buffer)
5816       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5817       ;; Allow the user to mangle the headers before parsing them.
5818       (gnus-run-hooks 'gnus-parse-headers-hook)
5819       (goto-char (point-min))
5820       (gnus-parse-without-error
5821         (while (and (or sequence allp)
5822                     (not (eobp)))
5823           (setq number (read cur))
5824           (when (not allp)
5825             (while (and sequence
5826                         (< (car sequence) number))
5827               (setq sequence (cdr sequence))))
5828           (when (and (or allp
5829                          (and sequence
5830                               (eq number (car sequence))))
5831                      (progn
5832                        (setq sequence (cdr sequence))
5833                        (setq header (inline
5834                                       (gnus-nov-parse-line
5835                                        number dependencies force-new)))))
5836             (push header headers))
5837           (forward-line 1)))
5838       ;; A common bug in inn is that if you have posted an article and
5839       ;; then retrieves the active file, it will answer correctly --
5840       ;; the new article is included.  However, a NOV entry for the
5841       ;; article may not have been generated yet, so this may fail.
5842       ;; We work around this problem by retrieving the last few
5843       ;; headers using HEAD.
5844       (if (or (not also-fetch-heads)
5845               (not sequence))
5846           ;; We (probably) got all the headers.
5847           (nreverse headers)
5848         (let ((gnus-nov-is-evil t))
5849           (nconc
5850            (nreverse headers)
5851            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5852              (gnus-get-newsgroup-headers))))))))
5853
5854 (defun gnus-article-get-xrefs ()
5855   "Fill in the Xref value in `gnus-current-headers', if necessary.
5856 This is meant to be called in `gnus-article-internal-prepare-hook'."
5857   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5858                                  gnus-current-headers)))
5859     (or (not gnus-use-cross-reference)
5860         (not headers)
5861         (and (mail-header-xref headers)
5862              (not (string= (mail-header-xref headers) "")))
5863         (let ((case-fold-search t)
5864               xref)
5865           (save-restriction
5866             (nnheader-narrow-to-headers)
5867             (goto-char (point-min))
5868             (when (or (and (not (eobp))
5869                            (eq (downcase (char-after)) ?x)
5870                            (looking-at "Xref:"))
5871                       (search-forward "\nXref:" nil t))
5872               (goto-char (1+ (match-end 0)))
5873               (setq xref (buffer-substring (point)
5874                                            (progn (end-of-line) (point))))
5875               (mail-header-set-xref headers xref)))))))
5876
5877 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5878   "Find article ID and insert the summary line for that article.
5879 OLD-HEADER can either be a header or a line number to insert
5880 the subject line on."
5881   (let* ((line (and (numberp old-header) old-header))
5882          (old-header (and (vectorp old-header) old-header))
5883          (header (cond ((and old-header use-old-header)
5884                         old-header)
5885                        ((and (numberp id)
5886                              (gnus-number-to-header id))
5887                         (gnus-number-to-header id))
5888                        (t
5889                         (gnus-read-header id))))
5890          (number (and (numberp id) id))
5891          d)
5892     (when header
5893       ;; Rebuild the thread that this article is part of and go to the
5894       ;; article we have fetched.
5895       (when (and (not gnus-show-threads)
5896                  old-header)
5897         (when (and number
5898                    (setq d (gnus-data-find (mail-header-number old-header))))
5899           (goto-char (gnus-data-pos d))
5900           (gnus-data-remove
5901            number
5902            (- (gnus-point-at-bol)
5903               (prog1
5904                   (1+ (gnus-point-at-eol))
5905                 (gnus-delete-line))))))
5906       (when old-header
5907         (mail-header-set-number header (mail-header-number old-header)))
5908       (setq gnus-newsgroup-sparse
5909             (delq (setq number (mail-header-number header))
5910                   gnus-newsgroup-sparse))
5911       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5912       (push number gnus-newsgroup-limit)
5913       (gnus-rebuild-thread (mail-header-id header) line)
5914       (gnus-summary-goto-subject number nil t))
5915     (when (and (numberp number)
5916                (> number 0))
5917       ;; We have to update the boundaries even if we can't fetch the
5918       ;; article if ID is a number -- so that the next `P' or `N'
5919       ;; command will fetch the previous (or next) article even
5920       ;; if the one we tried to fetch this time has been canceled.
5921       (when (> number gnus-newsgroup-end)
5922         (setq gnus-newsgroup-end number))
5923       (when (< number gnus-newsgroup-begin)
5924         (setq gnus-newsgroup-begin number))
5925       (setq gnus-newsgroup-unselected
5926             (delq number gnus-newsgroup-unselected)))
5927     ;; Report back a success?
5928     (and header (mail-header-number header))))
5929
5930 ;;; Process/prefix in the summary buffer
5931
5932 (defun gnus-summary-work-articles (n)
5933   "Return a list of articles to be worked upon.
5934 The prefix argument, the list of process marked articles, and the
5935 current article will be taken into consideration."
5936   (save-excursion
5937     (set-buffer gnus-summary-buffer)
5938     (cond
5939      (n
5940       ;; A numerical prefix has been given.
5941       (setq n (prefix-numeric-value n))
5942       (let ((backward (< n 0))
5943             (n (abs (prefix-numeric-value n)))
5944             articles article)
5945         (save-excursion
5946           (while
5947               (and (> n 0)
5948                    (push (setq article (gnus-summary-article-number))
5949                          articles)
5950                    (if backward
5951                        (gnus-summary-find-prev nil article)
5952                      (gnus-summary-find-next nil article)))
5953             (decf n)))
5954         (nreverse articles)))
5955      ((and (gnus-region-active-p) (mark))
5956       (message "region active")
5957       ;; Work on the region between point and mark.
5958       (let ((max (max (point) (mark)))
5959             articles article)
5960         (save-excursion
5961           (goto-char (min (point) (mark)))
5962           (while
5963               (and
5964                (push (setq article (gnus-summary-article-number)) articles)
5965                (gnus-summary-find-next nil article)
5966                (< (point) max)))
5967           (nreverse articles))))
5968      (gnus-newsgroup-processable
5969       ;; There are process-marked articles present.
5970       ;; Save current state.
5971       (gnus-summary-save-process-mark)
5972       ;; Return the list.
5973       (reverse gnus-newsgroup-processable))
5974      (t
5975       ;; Just return the current article.
5976       (list (gnus-summary-article-number))))))
5977
5978 (defmacro gnus-summary-iterate (arg &rest forms)
5979   "Iterate over the process/prefixed articles and do FORMS.
5980 ARG is the interactive prefix given to the command.  FORMS will be
5981 executed with point over the summary line of the articles."
5982   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5983     `(let ((,articles (gnus-summary-work-articles ,arg)))
5984        (while ,articles
5985          (gnus-summary-goto-subject (car ,articles))
5986          ,@forms
5987          (pop ,articles)))))
5988
5989 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5990 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5991
5992 (defun gnus-summary-save-process-mark ()
5993   "Push the current set of process marked articles on the stack."
5994   (interactive)
5995   (push (copy-sequence gnus-newsgroup-processable)
5996         gnus-newsgroup-process-stack))
5997
5998 (defun gnus-summary-kill-process-mark ()
5999   "Push the current set of process marked articles on the stack and unmark."
6000   (interactive)
6001   (gnus-summary-save-process-mark)
6002   (gnus-summary-unmark-all-processable))
6003
6004 (defun gnus-summary-yank-process-mark ()
6005   "Pop the last process mark state off the stack and restore it."
6006   (interactive)
6007   (unless gnus-newsgroup-process-stack
6008     (error "Empty mark stack"))
6009   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6010
6011 (defun gnus-summary-process-mark-set (set)
6012   "Make SET into the current process marked articles."
6013   (gnus-summary-unmark-all-processable)
6014   (while set
6015     (gnus-summary-set-process-mark (pop set))))
6016
6017 ;;; Searching and stuff
6018
6019 (defun gnus-summary-search-group (&optional backward use-level)
6020   "Search for next unread newsgroup.
6021 If optional argument BACKWARD is non-nil, search backward instead."
6022   (save-excursion
6023     (set-buffer gnus-group-buffer)
6024     (when (gnus-group-search-forward
6025            backward nil (if use-level (gnus-group-group-level) nil))
6026       (gnus-group-group-name))))
6027
6028 (defun gnus-summary-best-group (&optional exclude-group)
6029   "Find the name of the best unread group.
6030 If EXCLUDE-GROUP, do not go to this group."
6031   (save-excursion
6032     (set-buffer gnus-group-buffer)
6033     (save-excursion
6034       (gnus-group-best-unread-group exclude-group))))
6035
6036 (defun gnus-summary-find-next (&optional unread article backward)
6037   (if backward (gnus-summary-find-prev)
6038     (let* ((dummy (gnus-summary-article-intangible-p))
6039            (article (or article (gnus-summary-article-number)))
6040            (data (gnus-data-find-list article))
6041            result)
6042       (when (and (not dummy)
6043                  (or (not gnus-summary-check-current)
6044                      (not unread)
6045                      (not (gnus-data-unread-p (car data)))))
6046         (setq data (cdr data)))
6047       (when (setq result
6048                   (if unread
6049                       (progn
6050                         (while data
6051                           (unless (memq (gnus-data-number (car data)) 
6052                                         gnus-newsgroup-unfetched)
6053                             (when (gnus-data-unread-p (car data))
6054                               (setq result (car data)
6055                                     data nil)))
6056                           (setq data (cdr data)))
6057                         result)
6058                     (car data)))
6059         (goto-char (gnus-data-pos result))
6060         (gnus-data-number result)))))
6061
6062 (defun gnus-summary-find-prev (&optional unread article)
6063   (let* ((eobp (eobp))
6064          (article (or article (gnus-summary-article-number)))
6065          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6066          result)
6067     (when (and (not eobp)
6068                (or (not gnus-summary-check-current)
6069                    (not unread)
6070                    (not (gnus-data-unread-p (car data)))))
6071       (setq data (cdr data)))
6072     (when (setq result
6073                 (if unread
6074                     (progn
6075                       (while data
6076                         (unless (memq (gnus-data-number (car data)) gnus-newsgroup-unfetched)
6077                           (when (gnus-data-unread-p (car data))
6078                             (setq result (car data)
6079                                   data nil)))
6080                         (setq data (cdr data)))
6081                       result)
6082                   (car data)))
6083       (goto-char (gnus-data-pos result))
6084       (gnus-data-number result))))
6085
6086 (defun gnus-summary-find-subject (subject &optional unread backward article)
6087   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6088          (article (or article (gnus-summary-article-number)))
6089          (articles (gnus-data-list backward))
6090          (arts (gnus-data-find-list article articles))
6091          result)
6092     (when (or (not gnus-summary-check-current)
6093               (not unread)
6094               (not (gnus-data-unread-p (car arts))))
6095       (setq arts (cdr arts)))
6096     (while arts
6097       (and (or (not unread)
6098                (gnus-data-unread-p (car arts)))
6099            (vectorp (gnus-data-header (car arts)))
6100            (gnus-subject-equal
6101             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6102            (setq result (car arts)
6103                  arts nil))
6104       (setq arts (cdr arts)))
6105     (and result
6106          (goto-char (gnus-data-pos result))
6107          (gnus-data-number result))))
6108
6109 (defun gnus-summary-search-forward (&optional unread subject backward)
6110   "Search forward for an article.
6111 If UNREAD, look for unread articles.  If SUBJECT, look for
6112 articles with that subject.  If BACKWARD, search backward instead."
6113   (cond (subject (gnus-summary-find-subject subject unread backward))
6114         (backward (gnus-summary-find-prev unread))
6115         (t (gnus-summary-find-next unread))))
6116
6117 (defun gnus-recenter (&optional n)
6118   "Center point in window and redisplay frame.
6119 Also do horizontal recentering."
6120   (interactive "P")
6121   (when (and gnus-auto-center-summary
6122              (not (eq gnus-auto-center-summary 'vertical)))
6123     (gnus-horizontal-recenter))
6124   (recenter n))
6125
6126 (defun gnus-summary-recenter ()
6127   "Center point in the summary window.
6128 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6129 displayed, no centering will be performed."
6130   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6131   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6132   (interactive)
6133   ;; The user has to want it.
6134   (when gnus-auto-center-summary
6135     (let* ((top (cond ((< (window-height) 4) 0)
6136                       ((< (window-height) 7) 1)
6137                       (t (if (numberp gnus-auto-center-summary)
6138                              gnus-auto-center-summary
6139                            2))))
6140            (height (1- (window-height)))
6141            (bottom (save-excursion (goto-char (point-max))
6142                                    (forward-line (- height))
6143                                    (point)))
6144            (window (get-buffer-window (current-buffer))))
6145       (when (get-buffer-window gnus-article-buffer)
6146         ;; Only do recentering when the article buffer is displayed,
6147         ;; Set the window start to either `bottom', which is the biggest
6148         ;; possible valid number, or the second line from the top,
6149         ;; whichever is the least.
6150         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6151           (if (> bottom top-pos)
6152               ;; Keep the second line from the top visible
6153               (set-window-start window top-pos t)
6154             ;; Try to keep the bottom line visible; if it's partially
6155             ;; obscured, either scroll one more line to make it fully
6156             ;; visible, or revert to using TOP-POS.
6157             (save-excursion
6158               (goto-char (point-max))
6159               (forward-line -1)
6160               (let ((last-line-start (point)))
6161                 (goto-char bottom)
6162                 (set-window-start window (point) t)
6163                 (when (not (pos-visible-in-window-p last-line-start window))
6164                   (forward-line 1)
6165                   (set-window-start window (min (point) top-pos) t)))))))
6166       ;; Do horizontal recentering while we're at it.
6167       (when (and (get-buffer-window (current-buffer) t)
6168                  (not (eq gnus-auto-center-summary 'vertical)))
6169         (let ((selected (selected-window)))
6170           (select-window (get-buffer-window (current-buffer) t))
6171           (gnus-summary-position-point)
6172           (gnus-horizontal-recenter)
6173           (select-window selected))))))
6174
6175 (defun gnus-summary-jump-to-group (newsgroup)
6176   "Move point to NEWSGROUP in group mode buffer."
6177   ;; Keep update point of group mode buffer if visible.
6178   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6179       (save-window-excursion
6180         ;; Take care of tree window mode.
6181         (when (get-buffer-window gnus-group-buffer)
6182           (pop-to-buffer gnus-group-buffer))
6183         (gnus-group-jump-to-group newsgroup))
6184     (save-excursion
6185       ;; Take care of tree window mode.
6186       (if (get-buffer-window gnus-group-buffer)
6187           (pop-to-buffer gnus-group-buffer)
6188         (set-buffer gnus-group-buffer))
6189       (gnus-group-jump-to-group newsgroup))))
6190
6191 ;; This function returns a list of article numbers based on the
6192 ;; difference between the ranges of read articles in this group and
6193 ;; the range of active articles.
6194 (defun gnus-list-of-unread-articles (group)
6195   (let* ((read (gnus-info-read (gnus-get-info group)))
6196          (active (or (gnus-active group) (gnus-activate-group group)))
6197          (last (cdr active))
6198          first nlast unread)
6199     ;; If none are read, then all are unread.
6200     (if (not read)
6201         (setq first (car active))
6202       ;; If the range of read articles is a single range, then the
6203       ;; first unread article is the article after the last read
6204       ;; article.  Sounds logical, doesn't it?
6205       (if (and (not (listp (cdr read)))
6206                (or (< (car read) (car active))
6207                    (progn (setq read (list read))
6208                           nil)))
6209           (setq first (max (car active) (1+ (cdr read))))
6210         ;; `read' is a list of ranges.
6211         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6212                                   (caar read)))
6213                   1)
6214           (setq first (car active)))
6215         (while read
6216           (when first
6217             (while (< first nlast)
6218               (push first unread)
6219               (setq first (1+ first))))
6220           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6221           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6222           (setq read (cdr read)))))
6223     ;; And add the last unread articles.
6224     (while (<= first last)
6225       (push first unread)
6226       (setq first (1+ first)))
6227     ;; Return the list of unread articles.
6228     (delq 0 (nreverse unread))))
6229
6230 (defun gnus-list-of-read-articles (group)
6231   "Return a list of unread, unticked and non-dormant articles."
6232   (let* ((info (gnus-get-info group))
6233          (marked (gnus-info-marks info))
6234          (active (gnus-active group)))
6235     (and info active
6236          (gnus-list-range-difference
6237           (gnus-list-range-difference
6238            (gnus-sorted-complement
6239             (gnus-uncompress-range active)
6240             (gnus-list-of-unread-articles group))
6241            (cdr (assq 'dormant marked)))
6242           (cdr (assq 'tick marked))))))
6243
6244 ;; Various summary commands
6245
6246 (defun gnus-summary-select-article-buffer ()
6247   "Reconfigure windows to show article buffer."
6248   (interactive)
6249   (if (not (gnus-buffer-live-p gnus-article-buffer))
6250       (error "There is no article buffer for this summary buffer")
6251     (gnus-configure-windows 'article)
6252     (select-window (get-buffer-window gnus-article-buffer))))
6253
6254 (defun gnus-summary-universal-argument (arg)
6255   "Perform any operation on all articles that are process/prefixed."
6256   (interactive "P")
6257   (let ((articles (gnus-summary-work-articles arg))
6258         func article)
6259     (if (eq
6260          (setq
6261           func
6262           (key-binding
6263            (read-key-sequence
6264             (substitute-command-keys
6265              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6266          'undefined)
6267         (gnus-error 1 "Undefined key")
6268       (save-excursion
6269         (while articles
6270           (gnus-summary-goto-subject (setq article (pop articles)))
6271           (let (gnus-newsgroup-processable)
6272             (command-execute func))
6273           (gnus-summary-remove-process-mark article)))))
6274   (gnus-summary-position-point))
6275
6276 (defun gnus-summary-toggle-truncation (&optional arg)
6277   "Toggle truncation of summary lines.
6278 With arg, turn line truncation on if arg is positive."
6279   (interactive "P")
6280   (setq truncate-lines
6281         (if (null arg) (not truncate-lines)
6282           (> (prefix-numeric-value arg) 0)))
6283   (redraw-display))
6284
6285 (defun gnus-summary-find-uncancelled ()
6286   "Return the number of an uncancelled article.
6287 The current article is considered, then following articles, then previous
6288 articles.  If all articles are cancelled then return a dummy 0."
6289   (let (found)
6290     (dolist (rev '(nil t))
6291       (unless found      ; don't demand the reverse list if we don't need it
6292         (let ((data (gnus-data-find-list
6293                      (gnus-summary-article-number) (gnus-data-list rev))))
6294           (while (and data (not found))
6295             (if (not (eq gnus-canceled-mark (gnus-data-mark (car data))))
6296                 (setq found (gnus-data-number (car data))))
6297             (setq data (cdr data))))))
6298     (or found 0)))
6299
6300 (defun gnus-summary-reselect-current-group (&optional all rescan)
6301   "Exit and then reselect the current newsgroup.
6302 The prefix argument ALL means to select all articles."
6303   (interactive "P")
6304   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6305     (error "Ephemeral groups can't be reselected"))
6306   (let ((current-subject (gnus-summary-find-uncancelled))
6307         (group gnus-newsgroup-name))
6308     (setq gnus-newsgroup-begin nil)
6309     (gnus-summary-exit)
6310     ;; We have to adjust the point of group mode buffer because
6311     ;; point was moved to the next unread newsgroup by exiting.
6312     (gnus-summary-jump-to-group group)
6313     (when rescan
6314       (save-excursion
6315         (save-window-excursion
6316           ;; Don't show group contents.
6317           (set-window-start (selected-window) (point-max))
6318           (gnus-group-get-new-news-this-group 1))))
6319     (gnus-group-read-group all t)
6320     (gnus-summary-goto-subject current-subject nil t)))
6321
6322 (defun gnus-summary-rescan-group (&optional all)
6323   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6324   (interactive "P")
6325   (gnus-summary-reselect-current-group all t))
6326
6327 (defun gnus-summary-update-info (&optional non-destructive)
6328   (save-excursion
6329     (let ((group gnus-newsgroup-name))
6330       (when group
6331         (when gnus-newsgroup-kill-headers
6332           (setq gnus-newsgroup-killed
6333                 (gnus-compress-sequence
6334                  (gnus-sorted-union
6335                   (gnus-list-range-intersection
6336                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6337                   gnus-newsgroup-unreads)
6338                  t)))
6339         (unless (listp (cdr gnus-newsgroup-killed))
6340           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6341         (let ((headers gnus-newsgroup-headers))
6342           ;; Set the new ranges of read articles.
6343           (save-excursion
6344             (set-buffer gnus-group-buffer)
6345             (gnus-undo-force-boundary))
6346           (gnus-update-read-articles
6347            group (gnus-sorted-union
6348                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6349           ;; Set the current article marks.
6350           (let ((gnus-newsgroup-scored
6351                  (if (and (not gnus-save-score)
6352                           (not non-destructive))
6353                      nil
6354                    gnus-newsgroup-scored)))
6355             (save-excursion
6356               (gnus-update-marks)))
6357           ;; Do the cross-ref thing.
6358           (when gnus-use-cross-reference
6359             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6360           ;; Do not switch windows but change the buffer to work.
6361           (set-buffer gnus-group-buffer)
6362           (unless (gnus-ephemeral-group-p group)
6363             (gnus-group-update-group group)))))))
6364
6365 (defun gnus-summary-save-newsrc (&optional force)
6366   "Save the current number of read/marked articles in the dribble buffer.
6367 The dribble buffer will then be saved.
6368 If FORCE (the prefix), also save the .newsrc file(s)."
6369   (interactive "P")
6370   (gnus-summary-update-info t)
6371   (if force
6372       (gnus-save-newsrc-file)
6373     (gnus-dribble-save)))
6374
6375 (defun gnus-summary-exit (&optional temporary)
6376   "Exit reading current newsgroup, and then return to group selection mode.
6377 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6378   (interactive)
6379   (gnus-set-global-variables)
6380   (gnus-kill-save-kill-buffer)
6381   (gnus-async-halt-prefetch)
6382   (let* ((group gnus-newsgroup-name)
6383          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6384          (gnus-group-is-exiting-p t)
6385          (mode major-mode)
6386          (group-point nil)
6387          (buf (current-buffer)))
6388     (unless quit-config
6389       ;; Do adaptive scoring, and possibly save score files.
6390       (when gnus-newsgroup-adaptive
6391         (gnus-score-adaptive))
6392       (when gnus-use-scoring
6393         (gnus-score-save)))
6394     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6395     ;; If we have several article buffers, we kill them at exit.
6396     (unless gnus-single-article-buffer
6397       (gnus-kill-buffer gnus-original-article-buffer)
6398       (setq gnus-article-current nil))
6399     (when gnus-use-cache
6400       (gnus-cache-possibly-remove-articles)
6401       (gnus-cache-save-buffers))
6402     (gnus-async-prefetch-remove-group group)
6403     (when gnus-suppress-duplicates
6404       (gnus-dup-enter-articles))
6405     (when gnus-use-trees
6406       (gnus-tree-close group))
6407     (when gnus-use-cache
6408       (gnus-cache-write-active))
6409     ;; Remove entries for this group.
6410     (nnmail-purge-split-history (gnus-group-real-name group))
6411     ;; Make all changes in this group permanent.
6412     (unless quit-config
6413       (gnus-run-hooks 'gnus-exit-group-hook)
6414       (gnus-summary-update-info))
6415     (gnus-close-group group)
6416     ;; Make sure where we were, and go to next newsgroup.
6417     (set-buffer gnus-group-buffer)
6418     (unless quit-config
6419       (gnus-group-jump-to-group group))
6420     (gnus-run-hooks 'gnus-summary-exit-hook)
6421     (unless (or quit-config
6422                 ;; If this group has disappeared from the summary
6423                 ;; buffer, don't skip forwards.
6424                 (not (string= group (gnus-group-group-name))))
6425       (gnus-group-next-unread-group 1))
6426     (setq group-point (point))
6427     (if temporary
6428         nil                             ;Nothing to do.
6429       ;; If we have several article buffers, we kill them at exit.
6430       (unless gnus-single-article-buffer
6431         (gnus-kill-buffer gnus-article-buffer)
6432         (gnus-kill-buffer gnus-original-article-buffer)
6433         (setq gnus-article-current nil))
6434       (set-buffer buf)
6435       (if (not gnus-kill-summary-on-exit)
6436           (progn
6437             (gnus-deaden-summary)
6438             (setq mode nil))
6439         ;; We set all buffer-local variables to nil.  It is unclear why
6440         ;; this is needed, but if we don't, buffer-local variables are
6441         ;; not garbage-collected, it seems.  This would the lead to en
6442         ;; ever-growing Emacs.
6443         (gnus-summary-clear-local-variables)
6444         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6445           (gnus-summary-clear-local-variables))
6446         (when (get-buffer gnus-article-buffer)
6447           (bury-buffer gnus-article-buffer))
6448         ;; We clear the global counterparts of the buffer-local
6449         ;; variables as well, just to be on the safe side.
6450         (set-buffer gnus-group-buffer)
6451         (gnus-summary-clear-local-variables)
6452         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6453           (gnus-summary-clear-local-variables))
6454         ;; Return to group mode buffer.
6455         (when (eq mode 'gnus-summary-mode)
6456           (gnus-kill-buffer buf)))
6457       (setq gnus-current-select-method gnus-select-method)
6458       (pop-to-buffer gnus-group-buffer)
6459       (if (not quit-config)
6460           (progn
6461             (goto-char group-point)
6462             (gnus-configure-windows 'group 'force)
6463             (unless (pos-visible-in-window-p)
6464               (forward-line (/ (static-if (featurep 'xemacs)
6465                                    (window-displayed-height)
6466                                  (1- (window-height)))
6467                                -2))
6468               (set-window-start (selected-window) (point))
6469               (goto-char group-point)))
6470         (gnus-handle-ephemeral-exit quit-config))
6471       ;; Clear the current group name.
6472       (unless quit-config
6473         (setq gnus-newsgroup-name nil)))))
6474
6475 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6476 (defun gnus-summary-exit-no-update (&optional no-questions)
6477   "Quit reading current newsgroup without updating read article info."
6478   (interactive)
6479   (let* ((group gnus-newsgroup-name)
6480          (gnus-group-is-exiting-p t)
6481          (quit-config (gnus-group-quit-config group)))
6482     (when (or no-questions
6483               gnus-expert-user
6484               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6485       (gnus-async-halt-prefetch)
6486       (mapcar 'funcall
6487               (delq 'gnus-summary-expire-articles
6488                     (copy-sequence gnus-summary-prepare-exit-hook)))
6489       ;; If we have several article buffers, we kill them at exit.
6490       (unless gnus-single-article-buffer
6491         (gnus-kill-buffer gnus-article-buffer)
6492         (gnus-kill-buffer gnus-original-article-buffer)
6493         (setq gnus-article-current nil))
6494       (if (not gnus-kill-summary-on-exit)
6495           (gnus-deaden-summary)
6496         (gnus-close-group group)
6497         (gnus-summary-clear-local-variables)
6498         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6499           (gnus-summary-clear-local-variables))
6500         (set-buffer gnus-group-buffer)
6501         (gnus-summary-clear-local-variables)
6502         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6503           (gnus-summary-clear-local-variables))
6504         (when (get-buffer gnus-summary-buffer)
6505           (kill-buffer gnus-summary-buffer)))
6506       (unless gnus-single-article-buffer
6507         (setq gnus-article-current nil))
6508       (when gnus-use-trees
6509         (gnus-tree-close group))
6510       (gnus-async-prefetch-remove-group group)
6511       (when (get-buffer gnus-article-buffer)
6512         (bury-buffer gnus-article-buffer))
6513       ;; Return to the group buffer.
6514       (gnus-configure-windows 'group 'force)
6515       ;; Clear the current group name.
6516       (setq gnus-newsgroup-name nil)
6517       (unless (gnus-ephemeral-group-p group)
6518         (gnus-group-update-group group))
6519       (when (equal (gnus-group-group-name) group)
6520         (gnus-group-next-unread-group 1))
6521       (when quit-config
6522         (gnus-handle-ephemeral-exit quit-config)))))
6523
6524 (defun gnus-handle-ephemeral-exit (quit-config)
6525   "Handle movement when leaving an ephemeral group.
6526 The state which existed when entering the ephemeral is reset."
6527   (if (not (buffer-name (car quit-config)))
6528       (gnus-configure-windows 'group 'force)
6529     (set-buffer (car quit-config))
6530     (cond ((eq major-mode 'gnus-summary-mode)
6531            (gnus-set-global-variables))
6532           ((eq major-mode 'gnus-article-mode)
6533            (save-excursion
6534              ;; The `gnus-summary-buffer' variable may point
6535              ;; to the old summary buffer when using a single
6536              ;; article buffer.
6537              (unless (gnus-buffer-live-p gnus-summary-buffer)
6538                (set-buffer gnus-group-buffer))
6539              (set-buffer gnus-summary-buffer)
6540              (gnus-set-global-variables))))
6541     (if (or (eq (cdr quit-config) 'article)
6542             (eq (cdr quit-config) 'pick))
6543         (progn
6544           ;; The current article may be from the ephemeral group
6545           ;; thus it is best that we reload this article
6546           (gnus-summary-show-article)
6547           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6548               (gnus-configure-windows 'pick 'force)
6549             (gnus-configure-windows (cdr quit-config) 'force)))
6550       (gnus-configure-windows (cdr quit-config) 'force))
6551     (when (eq major-mode 'gnus-summary-mode)
6552       (gnus-summary-next-subject 1 nil t)
6553       (gnus-summary-recenter)
6554       (gnus-summary-position-point))))
6555
6556 (defun gnus-summary-preview-mime-message ()
6557   "MIME decode and play this message."
6558   (interactive)
6559   (let ((gnus-break-pages nil)
6560         (gnus-show-mime t))
6561     (gnus-summary-select-article gnus-show-all-headers t))
6562   (let ((w (get-buffer-window gnus-article-buffer)))
6563     (when w
6564       (select-window (get-buffer-window gnus-article-buffer)))))
6565
6566 ;;; Dead summaries.
6567
6568 (defvar gnus-dead-summary-mode-map nil)
6569
6570 (unless gnus-dead-summary-mode-map
6571   (setq gnus-dead-summary-mode-map (make-keymap))
6572   (suppress-keymap gnus-dead-summary-mode-map)
6573   (substitute-key-definition
6574    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6575   (dolist (key '("\C-d" "\r" "\177" [delete]))
6576     (define-key gnus-dead-summary-mode-map
6577       key 'gnus-summary-wake-up-the-dead))
6578   (dolist (key '("q" "Q"))
6579     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6580
6581 (defvar gnus-dead-summary-mode nil
6582   "Minor mode for Gnus summary buffers.")
6583
6584 (defun gnus-dead-summary-mode (&optional arg)
6585   "Minor mode for Gnus summary buffers."
6586   (interactive "P")
6587   (when (eq major-mode 'gnus-summary-mode)
6588     (make-local-variable 'gnus-dead-summary-mode)
6589     (setq gnus-dead-summary-mode
6590           (if (null arg) (not gnus-dead-summary-mode)
6591             (> (prefix-numeric-value arg) 0)))
6592     (when gnus-dead-summary-mode
6593       (gnus-add-minor-mode
6594        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6595
6596 (defun gnus-deaden-summary ()
6597   "Make the current summary buffer into a dead summary buffer."
6598   ;; Kill any previous dead summary buffer.
6599   (when (and gnus-dead-summary
6600              (buffer-name gnus-dead-summary))
6601     (save-excursion
6602       (set-buffer gnus-dead-summary)
6603       (when gnus-dead-summary-mode
6604         (kill-buffer (current-buffer)))))
6605   ;; Make this the current dead summary.
6606   (setq gnus-dead-summary (current-buffer))
6607   (gnus-dead-summary-mode 1)
6608   (let ((name (buffer-name)))
6609     (when (string-match "Summary" name)
6610       (rename-buffer
6611        (concat (substring name 0 (match-beginning 0)) "Dead "
6612                (substring name (match-beginning 0)))
6613        t)
6614       (bury-buffer))))
6615
6616 (defun gnus-kill-or-deaden-summary (buffer)
6617   "Kill or deaden the summary BUFFER."
6618   (save-excursion
6619     (when (and (buffer-name buffer)
6620                (not gnus-single-article-buffer))
6621       (save-excursion
6622         (set-buffer buffer)
6623         (gnus-kill-buffer gnus-article-buffer)
6624         (gnus-kill-buffer gnus-original-article-buffer)))
6625     (cond
6626      ;; Kill the buffer.
6627      (gnus-kill-summary-on-exit
6628       (when (and gnus-use-trees
6629                  (gnus-buffer-exists-p buffer))
6630         (save-excursion
6631           (set-buffer buffer)
6632           (gnus-tree-close gnus-newsgroup-name)))
6633       (gnus-kill-buffer buffer))
6634      ;; Deaden the buffer.
6635      ((gnus-buffer-exists-p buffer)
6636       (save-excursion
6637         (set-buffer buffer)
6638         (gnus-deaden-summary))))))
6639
6640 (defun gnus-summary-wake-up-the-dead (&rest args)
6641   "Wake up the dead summary buffer."
6642   (interactive)
6643   (gnus-dead-summary-mode -1)
6644   (let ((name (buffer-name)))
6645     (when (string-match "Dead " name)
6646       (rename-buffer
6647        (concat (substring name 0 (match-beginning 0))
6648                (substring name (match-end 0)))
6649        t)))
6650   (gnus-message 3 "This dead summary is now alive again"))
6651
6652 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6653 (defun gnus-summary-fetch-faq (&optional faq-dir)
6654   "Fetch the FAQ for the current group.
6655 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6656 in."
6657   (interactive
6658    (list
6659     (when current-prefix-arg
6660       (completing-read
6661        "FAQ dir: " (and (listp gnus-group-faq-directory)
6662                         (mapcar (lambda (file) (list file))
6663                                 gnus-group-faq-directory))))))
6664   (let (gnus-faq-buffer)
6665     (when (setq gnus-faq-buffer
6666                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6667       (gnus-configure-windows 'summary-faq))))
6668
6669 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6670 (defun gnus-summary-describe-group (&optional force)
6671   "Describe the current newsgroup."
6672   (interactive "P")
6673   (gnus-group-describe-group force gnus-newsgroup-name))
6674
6675 (defun gnus-summary-describe-briefly ()
6676   "Describe summary mode commands briefly."
6677   (interactive)
6678   (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")))
6679
6680 ;; Walking around group mode buffer from summary mode.
6681
6682 (defun gnus-summary-next-group (&optional no-article target-group backward)
6683   "Exit current newsgroup and then select next unread newsgroup.
6684 If prefix argument NO-ARTICLE is non-nil, no article is selected
6685 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6686 previous group instead."
6687   (interactive "P")
6688   ;; Stop pre-fetching.
6689   (gnus-async-halt-prefetch)
6690   (let ((current-group gnus-newsgroup-name)
6691         (current-buffer (current-buffer))
6692         entered)
6693     (gnus-summary-exit t)
6694     (while (not entered)
6695       ;; Then we find what group we are supposed to enter.
6696       (set-buffer gnus-group-buffer)
6697       (gnus-group-jump-to-group current-group)
6698       (setq target-group
6699             (or target-group
6700                 (if (eq gnus-keep-same-level 'best)
6701                     (gnus-summary-best-group gnus-newsgroup-name)
6702                   (gnus-summary-search-group backward gnus-keep-same-level))))
6703       (if (not target-group)
6704           ;; There are no further groups, so we return to the group
6705           ;; buffer.
6706           (progn
6707             (gnus-message 5 "Returning to the group buffer")
6708             (setq entered t)
6709             (when (gnus-buffer-live-p current-buffer)
6710               (set-buffer current-buffer)
6711               (gnus-summary-exit))
6712             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6713         ;; We try to enter the target group.
6714         (gnus-group-jump-to-group target-group)
6715         (let ((unreads (gnus-group-group-unread)))
6716           (if (and (or (eq t unreads)
6717                        (and unreads (not (zerop unreads))))
6718                    (progn
6719                      ;; Now we semi-exit this group to update Xrefs
6720                      ;; and all variables.  We can't do a real exit,
6721                      ;; because the window conf must remain the same
6722                      ;; in case the user is prompted for info, and we
6723                      ;; don't want the window conf to change before
6724                      ;; that...
6725                      (when (gnus-buffer-live-p current-buffer)
6726                        (set-buffer current-buffer)
6727                        (gnus-summary-exit t))
6728                      (gnus-summary-read-group
6729                       target-group nil no-article
6730                       (and (buffer-name current-buffer) current-buffer)
6731                       nil backward)))
6732               (setq entered t)
6733             (setq current-group target-group
6734                   target-group nil)))))))
6735
6736 (defun gnus-summary-prev-group (&optional no-article)
6737   "Exit current newsgroup and then select previous unread newsgroup.
6738 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6739   (interactive "P")
6740   (gnus-summary-next-group no-article nil t))
6741
6742 ;; Walking around summary lines.
6743
6744 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6745   "Go to the first subject satisfying any non-nil constraint.
6746 If UNREAD is non-nil, the article should be unread.
6747 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6748 If UNSEED is non-nil, the article should be unseen.
6749 Returns the article selected or nil if there are no matching articles."
6750   (interactive "P")
6751   (cond
6752    ;; Empty summary.
6753    ((null gnus-newsgroup-data)
6754     (gnus-message 3 "No articles in the group")
6755     nil)
6756    ;; Pick the first article.
6757    ((not (or unread undownloaded unseen))
6758     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6759     (gnus-data-number (car gnus-newsgroup-data)))
6760    ;; Find the first unread article.
6761    (t
6762     (let ((data gnus-newsgroup-data))
6763       (while (and data
6764                   (let ((num (gnus-data-number (car data))))
6765                     (or (memq num gnus-newsgroup-unfetched)
6766                         (not (or (and unread
6767                                       (memq num gnus-newsgroup-unreads))
6768                                  (and undownloaded
6769                                       (memq num gnus-newsgroup-undownloaded))
6770                                  (and unseen
6771                                       (memq num gnus-newsgroup-unseen)))))))
6772         (setq data (cdr data)))
6773       (prog1 
6774           (if data
6775               (progn
6776                 (goto-char (gnus-data-pos (car data)))
6777                 (gnus-data-number (car data)))
6778             (gnus-message 3 "No more%s articles"
6779                           (let* ((r (when unread " unread"))
6780                                  (d (when undownloaded " undownloaded"))
6781                                  (s (when unseen " unseen"))
6782                                  (l (delq nil (list r d s))))
6783                             (cond ((= 3 (length l))
6784                                    (concat r "," d ", or" s))
6785                                   ((= 2 (length l))
6786                                    (concat (car l) ", or" (cadr l)))
6787                                   ((= 1 (length l))
6788                                    (car l))
6789                                   (t
6790                                    ""))))
6791             nil
6792             )
6793         (gnus-summary-position-point))))))
6794
6795 (defun gnus-summary-next-subject (n &optional unread dont-display)
6796   "Go to next N'th summary line.
6797 If N is negative, go to the previous N'th subject line.
6798 If UNREAD is non-nil, only unread articles are selected.
6799 The difference between N and the actual number of steps taken is
6800 returned."
6801   (interactive "p")
6802   (let ((backward (< n 0))
6803         (n (abs n)))
6804     (while (and (> n 0)
6805                 (if backward
6806                     (gnus-summary-find-prev unread)
6807                   (gnus-summary-find-next unread)))
6808       (unless (zerop (setq n (1- n)))
6809         (gnus-summary-show-thread)))
6810     (when (/= 0 n)
6811       (gnus-message 7 "No more%s articles"
6812                     (if unread " unread" "")))
6813     (unless dont-display
6814       (gnus-summary-recenter)
6815       (gnus-summary-position-point))
6816     n))
6817
6818 (defun gnus-summary-next-unread-subject (n)
6819   "Go to next N'th unread summary line."
6820   (interactive "p")
6821   (gnus-summary-next-subject n t))
6822
6823 (defun gnus-summary-prev-subject (n &optional unread)
6824   "Go to previous N'th summary line.
6825 If optional argument UNREAD is non-nil, only unread article is selected."
6826   (interactive "p")
6827   (gnus-summary-next-subject (- n) unread))
6828
6829 (defun gnus-summary-prev-unread-subject (n)
6830   "Go to previous N'th unread summary line."
6831   (interactive "p")
6832   (gnus-summary-next-subject (- n) t))
6833
6834 (defun gnus-summary-goto-subjects (articles)
6835   "Insert the subject header for ARTICLES in the current buffer."
6836   (save-excursion
6837     (dolist (article articles)
6838       (gnus-summary-goto-subject article t)))
6839   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6840   (gnus-summary-position-point))
6841  
6842 (defun gnus-summary-goto-subject (article &optional force silent)
6843   "Go the subject line of ARTICLE.
6844 If FORCE, also allow jumping to articles not currently shown."
6845   (interactive "nArticle number: ")
6846   (unless (numberp article)
6847     (error "Article %s is not a number" article))
6848   (let ((b (point))
6849         (data (gnus-data-find article)))
6850     ;; We read in the article if we have to.
6851     (and (not data)
6852          force
6853          (gnus-summary-insert-subject
6854           article
6855           (if (or (numberp force) (vectorp force)) force)
6856           t)
6857          (setq data (gnus-data-find article)))
6858     (goto-char b)
6859     (if (not data)
6860         (progn
6861           (unless silent
6862             (gnus-message 3 "Can't find article %d" article))
6863           nil)
6864       (let ((pt (gnus-data-pos data)))
6865         (goto-char pt)
6866         (gnus-summary-set-article-display-arrow pt))
6867       (gnus-summary-position-point)
6868       article)))
6869
6870 ;; Walking around summary lines with displaying articles.
6871
6872 (defun gnus-summary-expand-window (&optional arg)
6873   "Make the summary buffer take up the entire Emacs frame.
6874 Given a prefix, will force an `article' buffer configuration."
6875   (interactive "P")
6876   (if arg
6877       (gnus-configure-windows 'article 'force)
6878     (gnus-configure-windows 'summary 'force)))
6879
6880 (defun gnus-summary-display-article (article &optional all-header)
6881   "Display ARTICLE in article buffer."
6882   (when (gnus-buffer-live-p gnus-article-buffer)
6883     (with-current-buffer gnus-article-buffer
6884       (set-buffer-multibyte t)))
6885   (gnus-set-global-variables)
6886   (when (gnus-buffer-live-p gnus-article-buffer)
6887     (with-current-buffer gnus-article-buffer
6888       (setq gnus-article-charset gnus-newsgroup-charset)
6889       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6890       (set-buffer-multibyte t)))
6891   (if (null article)
6892       nil
6893     (prog1
6894         (if gnus-summary-display-article-function
6895             (funcall gnus-summary-display-article-function article all-header)
6896           (gnus-article-prepare article all-header))
6897       (with-current-buffer gnus-article-buffer
6898         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6899              nil))
6900       (gnus-run-hooks 'gnus-select-article-hook)
6901       (when (and gnus-current-article
6902                  (not (zerop gnus-current-article)))
6903         (gnus-summary-goto-subject gnus-current-article))
6904       (gnus-summary-recenter)
6905       (when (and gnus-use-trees gnus-show-threads)
6906         (gnus-possibly-generate-tree article)
6907         (gnus-highlight-selected-tree article))
6908       ;; Successfully display article.
6909       (gnus-article-set-window-start
6910        (cdr (assq article gnus-newsgroup-bookmarks))))))
6911
6912 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6913   "Select the current article.
6914 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6915 non-nil, the article will be re-fetched even if it already present in
6916 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6917 be displayed."
6918   ;; Make sure we are in the summary buffer to work around bbdb bug.
6919   (unless (eq major-mode 'gnus-summary-mode)
6920     (set-buffer gnus-summary-buffer))
6921   (let ((article (or article (gnus-summary-article-number)))
6922         (all-headers (not (not all-headers))) ;Must be T or NIL.
6923         gnus-summary-display-article-function)
6924     (and (not pseudo)
6925          (gnus-summary-article-pseudo-p article)
6926          (error "This is a pseudo-article"))
6927     (save-excursion
6928       (set-buffer gnus-summary-buffer)
6929       (if (or (and gnus-single-article-buffer
6930                    (or (null gnus-current-article)
6931                        (null gnus-article-current)
6932                        (null (get-buffer gnus-article-buffer))
6933                        (not (eq article (cdr gnus-article-current)))
6934                        (not (equal (car gnus-article-current)
6935                                    gnus-newsgroup-name))))
6936               (and (not gnus-single-article-buffer)
6937                    (or (null gnus-current-article)
6938                        (not (eq gnus-current-article article))))
6939               force)
6940           ;; The requested article is different from the current article.
6941           (progn
6942             (gnus-summary-display-article article all-headers)
6943             (gnus-article-set-window-start
6944              (cdr (assq article gnus-newsgroup-bookmarks)))
6945             article)
6946         'old))))
6947
6948 (defun gnus-summary-force-verify-and-decrypt ()
6949   "Display buttons for signed/encrypted parts and verify/decrypt them."
6950   (interactive)
6951   (let ((mm-verify-option 'known)
6952         (mm-decrypt-option 'known)
6953         (gnus-buttonized-mime-types (append (list "multipart/signed"
6954                                                   "multipart/encrypted")
6955                                             gnus-buttonized-mime-types)))
6956     (gnus-summary-select-article nil 'force)))
6957
6958 (defun gnus-summary-set-current-mark (&optional current-mark)
6959   "Obsolete function."
6960   nil)
6961
6962 (defun gnus-summary-next-article (&optional unread subject backward push)
6963   "Select the next article.
6964 If UNREAD, only unread articles are selected.
6965 If SUBJECT, only articles with SUBJECT are selected.
6966 If BACKWARD, the previous article is selected instead of the next."
6967   (interactive "P")
6968   (cond
6969    ;; Is there such an article?
6970    ((and (gnus-summary-search-forward unread subject backward)
6971          (or (gnus-summary-display-article (gnus-summary-article-number))
6972              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6973     (gnus-summary-position-point))
6974    ;; If not, we try the first unread, if that is wanted.
6975    ((and subject
6976          gnus-auto-select-same
6977          (gnus-summary-first-unread-article))
6978     (gnus-summary-position-point)
6979     (gnus-message 6 "Wrapped"))
6980    ;; Try to get next/previous article not displayed in this group.
6981    ((and gnus-auto-extend-newsgroup
6982          (not unread) (not subject))
6983     (gnus-summary-goto-article
6984      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6985      nil (count-lines (point-min) (point))))
6986    ;; Go to next/previous group.
6987    (t
6988     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6989       (gnus-summary-jump-to-group gnus-newsgroup-name))
6990     (let ((cmd last-command-char)
6991           (point
6992            (save-excursion
6993              (set-buffer gnus-group-buffer)
6994              (point)))
6995           (group
6996            (if (eq gnus-keep-same-level 'best)
6997                (gnus-summary-best-group gnus-newsgroup-name)
6998              (gnus-summary-search-group backward gnus-keep-same-level))))
6999       ;; For some reason, the group window gets selected.  We change
7000       ;; it back.
7001       (select-window (get-buffer-window (current-buffer)))
7002       ;; Select next unread newsgroup automagically.
7003       (cond
7004        ((or (not gnus-auto-select-next)
7005             (not cmd))
7006         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7007        ((or (eq gnus-auto-select-next 'quietly)
7008             (and (eq gnus-auto-select-next 'slightly-quietly)
7009                  push)
7010             (and (eq gnus-auto-select-next 'almost-quietly)
7011                  (gnus-summary-last-article-p)))
7012         ;; Select quietly.
7013         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7014             (gnus-summary-exit)
7015           (gnus-message 7 "No more%s articles (%s)..."
7016                         (if unread " unread" "")
7017                         (if group (concat "selecting " group)
7018                           "exiting"))
7019           (gnus-summary-next-group nil group backward)))
7020        (t
7021         (when (gnus-key-press-event-p last-input-event)
7022           (gnus-summary-walk-group-buffer
7023            gnus-newsgroup-name cmd unread backward point))))))))
7024
7025 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7026   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7027                       (?\C-p (gnus-group-prev-unread-group 1))))
7028         (cursor-in-echo-area t)
7029         keve key group ended prompt)
7030     (save-excursion
7031       (set-buffer gnus-group-buffer)
7032       (goto-char start)
7033       (setq group
7034             (if (eq gnus-keep-same-level 'best)
7035                 (gnus-summary-best-group gnus-newsgroup-name)
7036               (gnus-summary-search-group backward gnus-keep-same-level))))
7037     (while (not ended)
7038       (setq prompt
7039             (format
7040              "No more%s articles%s " (if unread " unread" "")
7041              (if (and group
7042                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7043                  (format " (Type %s for %s [%s])"
7044                          (single-key-description cmd) group
7045                          (car (gnus-gethash group gnus-newsrc-hashtb)))
7046                (format " (Type %s to exit %s)"
7047                        (single-key-description cmd)
7048                        gnus-newsgroup-name))))
7049       ;; Confirm auto selection.
7050       (setq key (car (setq keve (gnus-read-event-char prompt)))
7051             ended t)
7052       (cond
7053        ((assq key keystrokes)
7054         (let ((obuf (current-buffer)))
7055           (switch-to-buffer gnus-group-buffer)
7056           (when group
7057             (gnus-group-jump-to-group group))
7058           (eval (cadr (assq key keystrokes)))
7059           (setq group (gnus-group-group-name))
7060           (switch-to-buffer obuf))
7061         (setq ended nil))
7062        ((equal key cmd)
7063         (if (or (not group)
7064                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7065             (gnus-summary-exit)
7066           (gnus-summary-next-group nil group backward)))
7067        (t
7068         (push (cdr keve) unread-command-events))))))
7069
7070 (defun gnus-summary-next-unread-article ()
7071   "Select unread article after current one."
7072   (interactive)
7073   (gnus-summary-next-article
7074    (or (not (eq gnus-summary-goto-unread 'never))
7075        (gnus-summary-last-article-p (gnus-summary-article-number)))
7076    (and gnus-auto-select-same
7077         (gnus-summary-article-subject))))
7078
7079 (defun gnus-summary-prev-article (&optional unread subject)
7080   "Select the article after the current one.
7081 If UNREAD is non-nil, only unread articles are selected."
7082   (interactive "P")
7083   (gnus-summary-next-article unread subject t))
7084
7085 (defun gnus-summary-prev-unread-article ()
7086   "Select unread article before current one."
7087   (interactive)
7088   (gnus-summary-prev-article
7089    (or (not (eq gnus-summary-goto-unread 'never))
7090        (gnus-summary-first-article-p (gnus-summary-article-number)))
7091    (and gnus-auto-select-same
7092         (gnus-summary-article-subject))))
7093
7094 (defun gnus-summary-next-page (&optional lines circular stop)
7095   "Show next page of the selected article.
7096 If at the end of the current article, select the next article.
7097 LINES says how many lines should be scrolled up.
7098
7099 If CIRCULAR is non-nil, go to the start of the article instead of
7100 selecting the next article when reaching the end of the current
7101 article.
7102
7103 If STOP is non-nil, just stop when reaching the end of the message."
7104   (interactive "P")
7105   (setq gnus-summary-buffer (current-buffer))
7106   (gnus-set-global-variables)
7107   (let ((article (gnus-summary-article-number))
7108         (article-window (get-buffer-window gnus-article-buffer t))
7109         endp)
7110     ;; If the buffer is empty, we have no article.
7111     (unless article
7112       (error "No article to select"))
7113     (gnus-configure-windows 'article)
7114     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7115         (if (and (eq gnus-summary-goto-unread 'never)
7116                  (not (gnus-summary-last-article-p article)))
7117             (gnus-summary-next-article)
7118           (gnus-summary-next-unread-article))
7119       (if (or (null gnus-current-article)
7120               (null gnus-article-current)
7121               (/= article (cdr gnus-article-current))
7122               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7123           ;; Selected subject is different from current article's.
7124           (gnus-summary-display-article article)
7125         (when article-window
7126           (gnus-eval-in-buffer-window gnus-article-buffer
7127             (setq endp (gnus-article-next-page lines)))
7128           (when endp
7129             (cond (stop
7130                    (gnus-message 3 "End of message"))
7131                   (circular
7132                    (gnus-summary-beginning-of-article))
7133                   (lines
7134                    (gnus-message 3 "End of message"))
7135                   ((null lines)
7136                    (if (and (eq gnus-summary-goto-unread 'never)
7137                             (not (gnus-summary-last-article-p article)))
7138                        (gnus-summary-next-article)
7139                      (gnus-summary-next-unread-article))))))))
7140     (gnus-summary-recenter)
7141     (gnus-summary-position-point)))
7142
7143 (defun gnus-summary-prev-page (&optional lines move)
7144   "Show previous page of selected article.
7145 Argument LINES specifies lines to be scrolled down.
7146 If MOVE, move to the previous unread article if point is at
7147 the beginning of the buffer."
7148   (interactive "P")
7149   (let ((article (gnus-summary-article-number))
7150         (article-window (get-buffer-window gnus-article-buffer t))
7151         endp)
7152     (gnus-configure-windows 'article)
7153     (if (or (null gnus-current-article)
7154             (null gnus-article-current)
7155             (/= article (cdr gnus-article-current))
7156             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7157         ;; Selected subject is different from current article's.
7158         (gnus-summary-display-article article)
7159       (gnus-summary-recenter)
7160       (when article-window
7161         (gnus-eval-in-buffer-window gnus-article-buffer
7162           (setq endp (gnus-article-prev-page lines)))
7163         (when (and move endp)
7164           (cond (lines
7165                  (gnus-message 3 "Beginning of message"))
7166                 ((null lines)
7167                  (if (and (eq gnus-summary-goto-unread 'never)
7168                           (not (gnus-summary-first-article-p article)))
7169                      (gnus-summary-prev-article)
7170                    (gnus-summary-prev-unread-article))))))))
7171   (gnus-summary-position-point))
7172
7173 (defun gnus-summary-prev-page-or-article (&optional lines)
7174   "Show previous page of selected article.
7175 Argument LINES specifies lines to be scrolled down.
7176 If at the beginning of the article, go to the next article."
7177   (interactive "P")
7178   (gnus-summary-prev-page lines t))
7179
7180 (defun gnus-summary-scroll-up (lines)
7181   "Scroll up (or down) one line current article.
7182 Argument LINES specifies lines to be scrolled up (or down if negative)."
7183   (interactive "p")
7184   (gnus-configure-windows 'article)
7185   (gnus-summary-show-thread)
7186   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7187     (gnus-eval-in-buffer-window gnus-article-buffer
7188       (cond ((> lines 0)
7189              (when (gnus-article-next-page lines)
7190                (gnus-message 3 "End of message")))
7191             ((< lines 0)
7192              (gnus-article-prev-page (- lines))))))
7193   (gnus-summary-recenter)
7194   (gnus-summary-position-point))
7195
7196 (defun gnus-summary-scroll-down (lines)
7197   "Scroll down (or up) one line current article.
7198 Argument LINES specifies lines to be scrolled down (or up if negative)."
7199   (interactive "p")
7200   (gnus-summary-scroll-up (- lines)))
7201
7202 (defun gnus-summary-next-same-subject ()
7203   "Select next article which has the same subject as current one."
7204   (interactive)
7205   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7206
7207 (defun gnus-summary-prev-same-subject ()
7208   "Select previous article which has the same subject as current one."
7209   (interactive)
7210   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7211
7212 (defun gnus-summary-next-unread-same-subject ()
7213   "Select next unread article which has the same subject as current one."
7214   (interactive)
7215   (gnus-summary-next-article t (gnus-summary-article-subject)))
7216
7217 (defun gnus-summary-prev-unread-same-subject ()
7218   "Select previous unread article which has the same subject as current one."
7219   (interactive)
7220   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7221
7222 (defun gnus-summary-first-unread-article ()
7223   "Select the first unread article.
7224 Return nil if there are no unread articles."
7225   (interactive)
7226   (prog1
7227       (when (gnus-summary-first-subject t)
7228         (gnus-summary-show-thread)
7229         (gnus-summary-first-subject t)
7230         (gnus-summary-display-article (gnus-summary-article-number)))
7231     (gnus-summary-position-point)))
7232
7233 (defun gnus-summary-first-unread-subject ()
7234   "Place the point on the subject line of the first unread article.
7235 Return nil if there are no unread articles."
7236   (interactive)
7237   (prog1
7238       (when (gnus-summary-first-subject t)
7239         (gnus-summary-show-thread)
7240         (gnus-summary-first-subject t))
7241     (gnus-summary-position-point)))
7242
7243 (defun gnus-summary-first-unseen-subject ()
7244   "Place the point on the subject line of the first unseen article.
7245 Return nil if there are no unseen articles."
7246   (interactive)
7247   (prog1
7248       (when (gnus-summary-first-subject nil nil t)
7249         (gnus-summary-show-thread)
7250         (gnus-summary-first-subject nil nil t))
7251     (gnus-summary-position-point)))
7252
7253 (defun gnus-summary-first-unseen-or-unread-subject ()
7254   "Place the point on the subject line of the first unseen article or,
7255 if all article have been seen, on the subject line of the first unread
7256 article."
7257   (interactive)
7258   (prog1
7259       (unless (when (gnus-summary-first-subject nil nil t)
7260                 (gnus-summary-show-thread)
7261                 (gnus-summary-first-subject nil nil t))
7262         (when (gnus-summary-first-subject t)
7263           (gnus-summary-show-thread)
7264           (gnus-summary-first-subject t)))
7265     (gnus-summary-position-point)))
7266
7267 (defun gnus-summary-first-article ()
7268   "Select the first article.
7269 Return nil if there are no articles."
7270   (interactive)
7271   (prog1
7272       (when (gnus-summary-first-subject)
7273         (gnus-summary-show-thread)
7274         (gnus-summary-first-subject)
7275         (gnus-summary-display-article (gnus-summary-article-number)))
7276     (gnus-summary-position-point)))
7277
7278 (defun gnus-summary-best-unread-article (&optional arg)
7279   "Select the unread article with the highest score.
7280 If given a prefix argument, select the next unread article that has a
7281 score higher than the default score."
7282   (interactive "P")
7283   (let ((article (if arg
7284                      (gnus-summary-better-unread-subject)
7285                    (gnus-summary-best-unread-subject))))
7286     (if article
7287         (gnus-summary-goto-article article)
7288       (error "No unread articles"))))
7289
7290 (defun gnus-summary-best-unread-subject ()
7291   "Select the unread subject with the highest score."
7292   (interactive)
7293   (let ((best -1000000)
7294         (data gnus-newsgroup-data)
7295         article score)
7296     (while data
7297       (and (gnus-data-unread-p (car data))
7298            (> (setq score
7299                     (gnus-summary-article-score (gnus-data-number (car data))))
7300               best)
7301            (setq best score
7302                  article (gnus-data-number (car data))))
7303       (setq data (cdr data)))
7304     (when article
7305       (gnus-summary-goto-subject article))
7306     (gnus-summary-position-point)
7307     article))
7308
7309 (defun gnus-summary-better-unread-subject ()
7310   "Select the first unread subject that has a score over the default score."
7311   (interactive)
7312   (let ((data gnus-newsgroup-data)
7313         article score)
7314     (while (and (setq article (gnus-data-number (car data)))
7315                 (or (gnus-data-read-p (car data))
7316                     (not (> (gnus-summary-article-score article)
7317                             gnus-summary-default-score))))
7318       (setq data (cdr data)))
7319     (when article
7320       (gnus-summary-goto-subject article))
7321     (gnus-summary-position-point)
7322     article))
7323
7324 (defun gnus-summary-last-subject ()
7325   "Go to the last displayed subject line in the group."
7326   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7327     (when article
7328       (gnus-summary-goto-subject article))))
7329
7330 (defun gnus-summary-goto-article (article &optional all-headers force)
7331   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7332 If ALL-HEADERS is non-nil, no header lines are hidden.
7333 If FORCE, go to the article even if it isn't displayed.  If FORCE
7334 is a number, it is the line the article is to be displayed on."
7335   (interactive
7336    (list
7337     (completing-read
7338      "Article number or Message-ID: "
7339      (mapcar (lambda (number) (list (int-to-string number)))
7340              gnus-newsgroup-limit))
7341     current-prefix-arg
7342     t))
7343   (prog1
7344       (if (and (stringp article)
7345                (string-match "@" article))
7346           (gnus-summary-refer-article article)
7347         (when (stringp article)
7348           (setq article (string-to-number article)))
7349         (if (gnus-summary-goto-subject article force)
7350             (gnus-summary-display-article article all-headers)
7351           (gnus-message 4 "Couldn't go to article %s" article) nil))
7352     (gnus-summary-position-point)))
7353
7354 (defun gnus-summary-goto-last-article ()
7355   "Go to the previously read article."
7356   (interactive)
7357   (prog1
7358       (when gnus-last-article
7359         (gnus-summary-goto-article gnus-last-article nil t))
7360     (gnus-summary-position-point)))
7361
7362 (defun gnus-summary-pop-article (number)
7363   "Pop one article off the history and go to the previous.
7364 NUMBER articles will be popped off."
7365   (interactive "p")
7366   (let (to)
7367     (setq gnus-newsgroup-history
7368           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7369     (if to
7370         (gnus-summary-goto-article (car to) nil t)
7371       (error "Article history empty")))
7372   (gnus-summary-position-point))
7373
7374 ;; Summary commands and functions for limiting the summary buffer.
7375
7376 (defun gnus-summary-limit-to-articles (n)
7377   "Limit the summary buffer to the next N articles.
7378 If not given a prefix, use the process marked articles instead."
7379   (interactive "P")
7380   (prog1
7381       (let ((articles (gnus-summary-work-articles n)))
7382         (setq gnus-newsgroup-processable nil)
7383         (gnus-summary-limit articles))
7384     (gnus-summary-position-point)))
7385
7386 (defun gnus-summary-pop-limit (&optional total)
7387   "Restore the previous limit.
7388 If given a prefix, remove all limits."
7389   (interactive "P")
7390   (when total
7391     (setq gnus-newsgroup-limits
7392           (list (mapcar (lambda (h) (mail-header-number h))
7393                         gnus-newsgroup-headers))))
7394   (unless gnus-newsgroup-limits
7395     (error "No limit to pop"))
7396   (prog1
7397       (gnus-summary-limit nil 'pop)
7398     (gnus-summary-position-point)))
7399
7400 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7401   "Limit the summary buffer to articles that have subjects that match a regexp.
7402 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7403   (interactive
7404    (list (read-string (if current-prefix-arg
7405                           "Exclude subject (regexp): "
7406                         "Limit to subject (regexp): "))
7407          nil current-prefix-arg))
7408   (unless header
7409     (setq header "subject"))
7410   (when (not (equal "" subject))
7411     (prog1
7412         (let ((articles (gnus-summary-find-matching
7413                          (or header "subject") subject 'all nil nil
7414                          not-matching)))
7415           (unless articles
7416             (error "Found no matches for \"%s\"" subject))
7417           (gnus-summary-limit articles))
7418       (gnus-summary-position-point))))
7419
7420 (defun gnus-summary-limit-to-author (from &optional not-matching)
7421   "Limit the summary buffer to articles that have authors that match a regexp.
7422 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7423   (interactive
7424    (list (read-string (if current-prefix-arg
7425                           "Exclude author (regexp): "
7426                         "Limit to author (regexp): "))
7427          current-prefix-arg))
7428   (gnus-summary-limit-to-subject from "from" not-matching))
7429
7430 (defun gnus-summary-limit-to-age (age &optional younger-p)
7431   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7432 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7433 articles that are younger than AGE days."
7434   (interactive
7435    (let ((younger current-prefix-arg)
7436          (days-got nil)
7437          days)
7438      (while (not days-got)
7439        (setq days (if younger
7440                       (read-string "Limit to articles younger than (in days, older when negative): ")
7441                     (read-string
7442                      "Limit to articles older than (in days, younger when negative): ")))
7443        (when (> (length days) 0)
7444          (setq days (read days)))
7445        (if (numberp days)
7446            (progn
7447              (setq days-got t)
7448              (if (< days 0)
7449                  (progn
7450                    (setq younger (not younger))
7451                    (setq days (* days -1)))))
7452          (message "Please enter a number.")
7453          (sleep-for 1)))
7454      (list days younger)))
7455   (prog1
7456       (let ((data gnus-newsgroup-data)
7457             (cutoff (days-to-time age))
7458             articles d date is-younger)
7459         (while (setq d (pop data))
7460           (when (and (vectorp (gnus-data-header d))
7461                      (setq date (mail-header-date (gnus-data-header d))))
7462             (setq is-younger (time-less-p
7463                               (time-since (condition-case ()
7464                                               (date-to-time date)
7465                                             (error '(0 0))))
7466                               cutoff))
7467             (when (if younger-p
7468                       is-younger
7469                     (not is-younger))
7470               (push (gnus-data-number d) articles))))
7471         (gnus-summary-limit (nreverse articles)))
7472     (gnus-summary-position-point)))
7473
7474 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7475   "Limit the summary buffer to articles that match an 'extra' header."
7476   (interactive
7477    (let ((header
7478           (intern
7479            (gnus-completing-read-with-default
7480             (symbol-name (car gnus-extra-headers))
7481             (if current-prefix-arg
7482                 "Exclude extra header:"
7483               "Limit extra header:")
7484             (mapcar (lambda (x)
7485                       (cons (symbol-name x) x))
7486                     gnus-extra-headers)
7487             nil
7488             t))))
7489      (list header
7490            (read-string (format "%s header %s (regexp): "
7491                                 (if current-prefix-arg "Exclude" "Limit to")
7492                                 header))
7493            current-prefix-arg)))
7494   (when (not (equal "" regexp))
7495     (prog1
7496         (let ((articles (gnus-summary-find-matching
7497                          (cons 'extra header) regexp 'all nil nil
7498                          not-matching)))
7499           (unless articles
7500             (error "Found no matches for \"%s\"" regexp))
7501           (gnus-summary-limit articles))
7502       (gnus-summary-position-point))))
7503
7504 (defun gnus-summary-limit-to-display-predicate ()
7505   "Limit the summary buffer to the predicated in the `display' group parameter."
7506   (interactive)
7507   (unless gnus-newsgroup-display
7508     (error "There is no `display' group parameter"))
7509   (let (articles)
7510     (dolist (number gnus-newsgroup-articles)
7511       (when (funcall gnus-newsgroup-display)
7512         (push number articles)))
7513     (gnus-summary-limit articles))
7514   (gnus-summary-position-point))
7515
7516 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7517 (make-obsolete
7518  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7519
7520 (defun gnus-summary-limit-to-unread (&optional all)
7521   "Limit the summary buffer to articles that are not marked as read.
7522 If ALL is non-nil, limit strictly to unread articles."
7523   (interactive "P")
7524   (if all
7525       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7526     (gnus-summary-limit-to-marks
7527      ;; Concat all the marks that say that an article is read and have
7528      ;; those removed.
7529      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7530            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7531            gnus-low-score-mark gnus-expirable-mark
7532            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7533            gnus-duplicate-mark gnus-souped-mark)
7534      'reverse)))
7535
7536 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7537 (make-obsolete 'gnus-summary-delete-marked-with
7538                'gnus-summary-limit-exclude-marks)
7539
7540 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7541   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7542 If REVERSE, limit the summary buffer to articles that are marked
7543 with MARKS.  MARKS can either be a string of marks or a list of marks.
7544 Returns how many articles were removed."
7545   (interactive "sMarks: ")
7546   (gnus-summary-limit-to-marks marks t))
7547
7548 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7549   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7550 If REVERSE (the prefix), limit the summary buffer to articles that are
7551 not marked with MARKS.  MARKS can either be a string of marks or a
7552 list of marks.
7553 Returns how many articles were removed."
7554   (interactive "sMarks: \nP")
7555   (prog1
7556       (let ((data gnus-newsgroup-data)
7557             (marks (if (listp marks) marks
7558                      (append marks nil))) ; Transform to list.
7559             articles)
7560         (while data
7561           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7562                   (memq (gnus-data-mark (car data)) marks))
7563             (push (gnus-data-number (car data)) articles))
7564           (setq data (cdr data)))
7565         (gnus-summary-limit articles))
7566     (gnus-summary-position-point)))
7567
7568 (defun gnus-summary-limit-to-score (score)
7569   "Limit to articles with score at or above SCORE."
7570   (interactive "NLimit to articles with score of at least: ")
7571   (let ((data gnus-newsgroup-data)
7572         articles)
7573     (while data
7574       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7575                 score)
7576         (push (gnus-data-number (car data)) articles))
7577       (setq data (cdr data)))
7578     (prog1
7579         (gnus-summary-limit articles)
7580       (gnus-summary-position-point))))
7581
7582 (defun gnus-summary-limit-to-unseen ()
7583   "Limit to unseen articles."
7584   (interactive)
7585   (prog1
7586       (gnus-summary-limit gnus-newsgroup-unseen)
7587     (gnus-summary-position-point)))
7588
7589 (defun gnus-summary-limit-include-thread (id)
7590   "Display all the hidden articles that is in the thread with ID in it.
7591 When called interactively, ID is the Message-ID of the current
7592 article."
7593   (interactive (list (mail-header-id (gnus-summary-article-header))))
7594   (let ((articles (gnus-articles-in-thread
7595                    (gnus-id-to-thread (gnus-root-id id)))))
7596     (prog1
7597         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7598       (gnus-summary-limit-include-matching-articles
7599        "subject"
7600        (regexp-quote (gnus-simplify-subject-re
7601                       (mail-header-subject (gnus-id-to-header id)))))
7602       (gnus-summary-position-point))))
7603
7604 (defun gnus-summary-limit-include-matching-articles (header regexp)
7605   "Display all the hidden articles that have HEADERs that match REGEXP."
7606   (interactive (list (read-string "Match on header: ")
7607                      (read-string "Regexp: ")))
7608   (let ((articles (gnus-find-matching-articles header regexp)))
7609     (prog1
7610         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7611       (gnus-summary-position-point))))
7612
7613 (defun gnus-summary-insert-dormant-articles ()
7614   "Insert all the dormat articles for this group into the current buffer."
7615   (interactive)
7616   (let ((gnus-verbose (max 6 gnus-verbose)))
7617     (if (not gnus-newsgroup-dormant)
7618         (gnus-message 3 "No cached articles for this group")
7619       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7620
7621 (defun gnus-summary-limit-include-dormant ()
7622   "Display all the hidden articles that are marked as dormant.
7623 Note that this command only works on a subset of the articles currently
7624 fetched for this group."
7625   (interactive)
7626   (unless gnus-newsgroup-dormant
7627     (error "There are no dormant articles in this group"))
7628   (prog1
7629       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7630     (gnus-summary-position-point)))
7631
7632 (defun gnus-summary-limit-exclude-dormant ()
7633   "Hide all dormant articles."
7634   (interactive)
7635   (prog1
7636       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7637     (gnus-summary-position-point)))
7638
7639 (defun gnus-summary-limit-exclude-childless-dormant ()
7640   "Hide all dormant articles that have no children."
7641   (interactive)
7642   (let ((data (gnus-data-list t))
7643         articles d children)
7644     ;; Find all articles that are either not dormant or have
7645     ;; children.
7646     (while (setq d (pop data))
7647       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7648                 (and (setq children
7649                            (gnus-article-children (gnus-data-number d)))
7650                      (let (found)
7651                        (while children
7652                          (when (memq (car children) articles)
7653                            (setq children nil
7654                                  found t))
7655                          (pop children))
7656                        found)))
7657         (push (gnus-data-number d) articles)))
7658     ;; Do the limiting.
7659     (prog1
7660         (gnus-summary-limit articles)
7661       (gnus-summary-position-point))))
7662
7663 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7664   "Mark all unread excluded articles as read.
7665 If ALL, mark even excluded ticked and dormants as read."
7666   (interactive "P")
7667   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7668   (let ((articles (gnus-sorted-ndifference
7669                    (sort
7670                     (mapcar (lambda (h) (mail-header-number h))
7671                             gnus-newsgroup-headers)
7672                     '<)
7673                    gnus-newsgroup-limit))
7674         article)
7675     (setq gnus-newsgroup-unreads
7676           (gnus-sorted-intersection gnus-newsgroup-unreads
7677                                     gnus-newsgroup-limit))
7678     (if all
7679         (setq gnus-newsgroup-dormant nil
7680               gnus-newsgroup-marked nil
7681               gnus-newsgroup-reads
7682               (nconc
7683                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7684                gnus-newsgroup-reads))
7685       (while (setq article (pop articles))
7686         (unless (or (memq article gnus-newsgroup-dormant)
7687                     (memq article gnus-newsgroup-marked))
7688           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7689
7690 (defun gnus-summary-limit (articles &optional pop)
7691   (if pop
7692       ;; We pop the previous limit off the stack and use that.
7693       (setq articles (car gnus-newsgroup-limits)
7694             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7695     ;; We use the new limit, so we push the old limit on the stack.
7696     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7697   ;; Set the limit.
7698   (setq gnus-newsgroup-limit articles)
7699   (let ((total (length gnus-newsgroup-data))
7700         (data (gnus-data-find-list (gnus-summary-article-number)))
7701         (gnus-summary-mark-below nil)   ; Inhibit this.
7702         found)
7703     ;; This will do all the work of generating the new summary buffer
7704     ;; according to the new limit.
7705     (gnus-summary-prepare)
7706     ;; Hide any threads, possibly.
7707     (gnus-summary-maybe-hide-threads)
7708     ;; Try to return to the article you were at, or one in the
7709     ;; neighborhood.
7710     (when data
7711       ;; We try to find some article after the current one.
7712       (while data
7713         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7714           (setq data nil
7715                 found t))
7716         (setq data (cdr data))))
7717     (unless found
7718       ;; If there is no data, that means that we were after the last
7719       ;; article.  The same goes when we can't find any articles
7720       ;; after the current one.
7721       (goto-char (point-max))
7722       (gnus-summary-find-prev))
7723     (gnus-set-mode-line 'summary)
7724     ;; We return how many articles were removed from the summary
7725     ;; buffer as a result of the new limit.
7726     (- total (length gnus-newsgroup-data))))
7727
7728 (defsubst gnus-invisible-cut-children (threads)
7729   (let ((num 0))
7730     (while threads
7731       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7732         (incf num))
7733       (pop threads))
7734     (< num 2)))
7735
7736 (defsubst gnus-cut-thread (thread)
7737   "Go forwards in the thread until we find an article that we want to display."
7738   (when (or (eq gnus-fetch-old-headers 'some)
7739             (eq gnus-fetch-old-headers 'invisible)
7740             (numberp gnus-fetch-old-headers)
7741             (eq gnus-build-sparse-threads 'some)
7742             (eq gnus-build-sparse-threads 'more))
7743     ;; Deal with old-fetched headers and sparse threads.
7744     (while (and
7745             thread
7746             (or
7747              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7748              (gnus-summary-article-ancient-p
7749               (mail-header-number (car thread))))
7750             (if (or (<= (length (cdr thread)) 1)
7751                     (eq gnus-fetch-old-headers 'invisible))
7752                 (setq gnus-newsgroup-limit
7753                       (delq (mail-header-number (car thread))
7754                             gnus-newsgroup-limit)
7755                       thread (cadr thread))
7756               (when (gnus-invisible-cut-children (cdr thread))
7757                 (let ((th (cdr thread)))
7758                   (while th
7759                     (if (memq (mail-header-number (caar th))
7760                               gnus-newsgroup-limit)
7761                         (setq thread (car th)
7762                               th nil)
7763                       (setq th (cdr th))))))))))
7764   thread)
7765
7766 (defun gnus-cut-threads (threads)
7767   "Cut off all uninteresting articles from the beginning of threads."
7768   (when (or (eq gnus-fetch-old-headers 'some)
7769             (eq gnus-fetch-old-headers 'invisible)
7770             (numberp gnus-fetch-old-headers)
7771             (eq gnus-build-sparse-threads 'some)
7772             (eq gnus-build-sparse-threads 'more))
7773     (let ((th threads))
7774       (while th
7775         (setcar th (gnus-cut-thread (car th)))
7776         (setq th (cdr th)))))
7777   ;; Remove nixed out threads.
7778   (delq nil threads))
7779
7780 (defun gnus-summary-initial-limit (&optional show-if-empty)
7781   "Figure out what the initial limit is supposed to be on group entry.
7782 This entails weeding out unwanted dormants, low-scored articles,
7783 fetch-old-headers verbiage, and so on."
7784   ;; Most groups have nothing to remove.
7785   (if (or gnus-inhibit-limiting
7786           (and (null gnus-newsgroup-dormant)
7787                (eq gnus-newsgroup-display 'gnus-not-ignore)
7788                (not (eq gnus-fetch-old-headers 'some))
7789                (not (numberp gnus-fetch-old-headers))
7790                (not (eq gnus-fetch-old-headers 'invisible))
7791                (null gnus-summary-expunge-below)
7792                (not (eq gnus-build-sparse-threads 'some))
7793                (not (eq gnus-build-sparse-threads 'more))
7794                (null gnus-thread-expunge-below)
7795                (not gnus-use-nocem)))
7796       ()                                ; Do nothing.
7797     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7798     (setq gnus-newsgroup-limit nil)
7799     (mapatoms
7800      (lambda (node)
7801        (unless (car (symbol-value node))
7802          ;; These threads have no parents -- they are roots.
7803          (let ((nodes (cdr (symbol-value node)))
7804                thread)
7805            (while nodes
7806              (if (and gnus-thread-expunge-below
7807                       (< (gnus-thread-total-score (car nodes))
7808                          gnus-thread-expunge-below))
7809                  (gnus-expunge-thread (pop nodes))
7810                (setq thread (pop nodes))
7811                (gnus-summary-limit-children thread))))))
7812      gnus-newsgroup-dependencies)
7813     ;; If this limitation resulted in an empty group, we might
7814     ;; pop the previous limit and use it instead.
7815     (when (and (not gnus-newsgroup-limit)
7816                show-if-empty)
7817       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7818     gnus-newsgroup-limit))
7819
7820 (defun gnus-summary-limit-children (thread)
7821   "Return 1 if this subthread is visible and 0 if it is not."
7822   ;; First we get the number of visible children to this thread.  This
7823   ;; is done by recursing down the thread using this function, so this
7824   ;; will really go down to a leaf article first, before slowly
7825   ;; working its way up towards the root.
7826   (when thread
7827     (let* ((max-lisp-eval-depth 5000)
7828            (children
7829            (if (cdr thread)
7830                (apply '+ (mapcar 'gnus-summary-limit-children
7831                                  (cdr thread)))
7832              0))
7833           (number (mail-header-number (car thread)))
7834           score)
7835       (if (and
7836            (not (memq number gnus-newsgroup-marked))
7837            (or
7838             ;; If this article is dormant and has absolutely no visible
7839             ;; children, then this article isn't visible.
7840             (and (memq number gnus-newsgroup-dormant)
7841                  (zerop children))
7842             ;; If this is "fetch-old-headered" and there is no
7843             ;; visible children, then we don't want this article.
7844             (and (or (eq gnus-fetch-old-headers 'some)
7845                      (numberp gnus-fetch-old-headers))
7846                  (gnus-summary-article-ancient-p number)
7847                  (zerop children))
7848             ;; If this is "fetch-old-headered" and `invisible', then
7849             ;; we don't want this article.
7850             (and (eq gnus-fetch-old-headers 'invisible)
7851                  (gnus-summary-article-ancient-p number))
7852             ;; If this is a sparsely inserted article with no children,
7853             ;; we don't want it.
7854             (and (eq gnus-build-sparse-threads 'some)
7855                  (gnus-summary-article-sparse-p number)
7856                  (zerop children))
7857             ;; If we use expunging, and this article is really
7858             ;; low-scored, then we don't want this article.
7859             (when (and gnus-summary-expunge-below
7860                        (< (setq score
7861                                 (or (cdr (assq number gnus-newsgroup-scored))
7862                                     gnus-summary-default-score))
7863                           gnus-summary-expunge-below))
7864               ;; We increase the expunge-tally here, but that has
7865               ;; nothing to do with the limits, really.
7866               (incf gnus-newsgroup-expunged-tally)
7867               ;; We also mark as read here, if that's wanted.
7868               (when (and gnus-summary-mark-below
7869                          (< score gnus-summary-mark-below))
7870                 (setq gnus-newsgroup-unreads
7871                       (delq number gnus-newsgroup-unreads))
7872                 (if gnus-newsgroup-auto-expire
7873                     (push number gnus-newsgroup-expirable)
7874                   (push (cons number gnus-low-score-mark)
7875                         gnus-newsgroup-reads)))
7876               t)
7877             ;; Do the `display' group parameter.
7878             (and gnus-newsgroup-display
7879                  (not (funcall gnus-newsgroup-display)))
7880             ;; Check NoCeM things.
7881             (if (and gnus-use-nocem
7882                      (gnus-nocem-unwanted-article-p
7883                       (mail-header-id (car thread))))
7884                 (progn
7885                   (setq gnus-newsgroup-unreads
7886                         (delq number gnus-newsgroup-unreads))
7887                   t))))
7888           ;; Nope, invisible article.
7889           0
7890         ;; Ok, this article is to be visible, so we add it to the limit
7891         ;; and return 1.
7892         (push number gnus-newsgroup-limit)
7893         1))))
7894
7895 (defun gnus-expunge-thread (thread)
7896   "Mark all articles in THREAD as read."
7897   (let* ((number (mail-header-number (car thread))))
7898     (incf gnus-newsgroup-expunged-tally)
7899     ;; We also mark as read here, if that's wanted.
7900     (setq gnus-newsgroup-unreads
7901           (delq number gnus-newsgroup-unreads))
7902     (if gnus-newsgroup-auto-expire
7903         (push number gnus-newsgroup-expirable)
7904       (push (cons number gnus-low-score-mark)
7905             gnus-newsgroup-reads)))
7906   ;; Go recursively through all subthreads.
7907   (mapcar 'gnus-expunge-thread (cdr thread)))
7908
7909 ;; Summary article oriented commands
7910
7911 (defun gnus-summary-refer-parent-article (n)
7912   "Refer parent article N times.
7913 If N is negative, go to ancestor -N instead.
7914 The difference between N and the number of articles fetched is returned."
7915   (interactive "p")
7916   (let ((skip 1)
7917         error header ref)
7918     (when (not (natnump n))
7919       (setq skip (abs n)
7920             n 1))
7921     (while (and (> n 0)
7922                 (not error))
7923       (setq header (gnus-summary-article-header))
7924       (if (and (eq (mail-header-number header)
7925                    (cdr gnus-article-current))
7926                (equal gnus-newsgroup-name
7927                       (car gnus-article-current)))
7928           ;; If we try to find the parent of the currently
7929           ;; displayed article, then we take a look at the actual
7930           ;; References header, since this is slightly more
7931           ;; reliable than the References field we got from the
7932           ;; server.
7933           (save-excursion
7934             (set-buffer gnus-original-article-buffer)
7935             (nnheader-narrow-to-headers)
7936             (unless (setq ref (message-fetch-field "references"))
7937               (setq ref (message-fetch-field "in-reply-to")))
7938             (widen))
7939         (setq ref
7940               ;; It's not the current article, so we take a bet on
7941               ;; the value we got from the server.
7942               (mail-header-references header)))
7943       (if (and ref
7944                (not (equal ref "")))
7945           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7946             (gnus-message 1 "Couldn't find parent"))
7947         (gnus-message 1 "No references in article %d"
7948                       (gnus-summary-article-number))
7949         (setq error t))
7950       (decf n))
7951     (gnus-summary-position-point)
7952     n))
7953
7954 (defun gnus-summary-refer-references ()
7955   "Fetch all articles mentioned in the References header.
7956 Return the number of articles fetched."
7957   (interactive)
7958   (let ((ref (mail-header-references (gnus-summary-article-header)))
7959         (current (gnus-summary-article-number))
7960         (n 0))
7961     (if (or (not ref)
7962             (equal ref ""))
7963         (error "No References in the current article")
7964       ;; For each Message-ID in the References header...
7965       (while (string-match "<[^>]*>" ref)
7966         (incf n)
7967         ;; ... fetch that article.
7968         (gnus-summary-refer-article
7969          (prog1 (match-string 0 ref)
7970            (setq ref (substring ref (match-end 0))))))
7971       (gnus-summary-goto-subject current)
7972       (gnus-summary-position-point)
7973       n)))
7974
7975 (defun gnus-summary-refer-thread (&optional limit)
7976   "Fetch all articles in the current thread.
7977 If LIMIT (the numerical prefix), fetch that many old headers instead
7978 of what's specified by the `gnus-refer-thread-limit' variable."
7979   (interactive "P")
7980   (let ((id (mail-header-id (gnus-summary-article-header)))
7981         (limit (if limit (prefix-numeric-value limit)
7982                  gnus-refer-thread-limit)))
7983     (unless (eq gnus-fetch-old-headers 'invisible)
7984       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7985       ;; Retrieve the headers and read them in.
7986       (if (eq (gnus-retrieve-headers
7987                (list (min
7988                       (+ (mail-header-number
7989                           (gnus-summary-article-header))
7990                          limit)
7991                       gnus-newsgroup-end))
7992                gnus-newsgroup-name (* limit 2))
7993               'nov)
7994           (gnus-build-all-threads)
7995         (error "Can't fetch thread from backends that don't support NOV"))
7996       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7997     (gnus-summary-limit-include-thread id)))
7998
7999 (defun gnus-summary-refer-article (message-id)
8000   "Fetch an article specified by MESSAGE-ID."
8001   (interactive "sMessage-ID: ")
8002   (when (and (stringp message-id)
8003              (not (zerop (length message-id))))
8004     ;; Construct the correct Message-ID if necessary.
8005     ;; Suggested by tale@pawl.rpi.edu.
8006     (unless (string-match "^<" message-id)
8007       (setq message-id (concat "<" message-id)))
8008     (unless (string-match ">$" message-id)
8009       (setq message-id (concat message-id ">")))
8010     (let* ((header (gnus-id-to-header message-id))
8011            (sparse (and header
8012                         (gnus-summary-article-sparse-p
8013                          (mail-header-number header))
8014                         (memq (mail-header-number header)
8015                               gnus-newsgroup-limit)))
8016            number)
8017       (cond
8018        ;; If the article is present in the buffer we just go to it.
8019        ((and header
8020              (or (not (gnus-summary-article-sparse-p
8021                        (mail-header-number header)))
8022                  sparse))
8023         (prog1
8024             (gnus-summary-goto-article
8025              (mail-header-number header) nil t)
8026           (when sparse
8027             (gnus-summary-update-article (mail-header-number header)))))
8028        (t
8029         ;; We fetch the article.
8030         (catch 'found
8031           (dolist (gnus-override-method (gnus-refer-article-methods))
8032             (gnus-check-server gnus-override-method)
8033             ;; Fetch the header, and display the article.
8034             (when (setq number (gnus-summary-insert-subject message-id))
8035               (gnus-summary-select-article nil nil nil number)
8036               (throw 'found t)))
8037           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8038
8039 (defun gnus-refer-article-methods ()
8040   "Return a list of referable methods."
8041   (cond
8042    ;; No method, so we default to current and native.
8043    ((null gnus-refer-article-method)
8044     (list gnus-current-select-method gnus-select-method))
8045    ;; Current.
8046    ((eq 'current gnus-refer-article-method)
8047     (list gnus-current-select-method))
8048    ;; List of select methods.
8049    ((not (and (symbolp (car gnus-refer-article-method))
8050               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8051     (let (out)
8052       (dolist (method gnus-refer-article-method)
8053         (push (if (eq 'current method)
8054                   gnus-current-select-method
8055                 method)
8056               out))
8057       (nreverse out)))
8058    ;; One single select method.
8059    (t
8060     (list gnus-refer-article-method))))
8061
8062 (defun gnus-summary-edit-parameters ()
8063   "Edit the group parameters of the current group."
8064   (interactive)
8065   (gnus-group-edit-group gnus-newsgroup-name 'params))
8066
8067 (defun gnus-summary-customize-parameters ()
8068   "Customize the group parameters of the current group."
8069   (interactive)
8070   (gnus-group-customize gnus-newsgroup-name))
8071
8072 (defun gnus-summary-enter-digest-group (&optional force)
8073   "Enter an nndoc group based on the current article.
8074 If FORCE, force a digest interpretation.  If not, try
8075 to guess what the document format is."
8076   (interactive "P")
8077   (let ((conf gnus-current-window-configuration))
8078     (save-excursion
8079       (gnus-summary-select-article))
8080     (setq gnus-current-window-configuration conf)
8081     (let* ((name (format "%s-%d"
8082                          (gnus-group-prefixed-name
8083                           gnus-newsgroup-name (list 'nndoc ""))
8084                          (save-excursion
8085                            (set-buffer gnus-summary-buffer)
8086                            gnus-current-article)))
8087            (ogroup gnus-newsgroup-name)
8088            (params (append (gnus-info-params (gnus-get-info ogroup))
8089                            (list (cons 'to-group ogroup))
8090                            (list (cons 'save-article-group ogroup))))
8091            (case-fold-search t)
8092            (buf (current-buffer))
8093            dig to-address)
8094       (save-excursion
8095         (set-buffer gnus-original-article-buffer)
8096         ;; Have the digest group inherit the main mail address of
8097         ;; the parent article.
8098         (when (setq to-address (or (gnus-fetch-field "reply-to")
8099                                    (gnus-fetch-field "from")))
8100           (setq params (append
8101                         (list (cons 'to-address
8102                                     (funcall gnus-decode-encoded-word-function
8103                                              to-address))))))
8104         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8105         (insert-buffer-substring gnus-original-article-buffer)
8106         ;; Remove lines that may lead nndoc to misinterpret the
8107         ;; document type.
8108         (narrow-to-region
8109          (goto-char (point-min))
8110          (or (search-forward "\n\n" nil t) (point)))
8111         (goto-char (point-min))
8112         (delete-matching-lines "^Path:\\|^From ")
8113         (widen))
8114       (unwind-protect
8115           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8116                     (gnus-newsgroup-ephemeral-ignored-charsets
8117                      gnus-newsgroup-ignored-charsets))
8118                 (gnus-group-read-ephemeral-group
8119                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8120                               (nndoc-article-type
8121                                ,(if force 'mbox 'guess)))
8122                  t nil nil nil
8123                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8124                                                         "ADAPT")))))
8125               ;; Make all postings to this group go to the parent group.
8126               (nconc (gnus-info-params (gnus-get-info name))
8127                      params)
8128             ;; Couldn't select this doc group.
8129             (switch-to-buffer buf)
8130             (gnus-set-global-variables)
8131             (gnus-configure-windows 'summary)
8132             (gnus-message 3 "Article couldn't be entered?"))
8133         (kill-buffer dig)))))
8134
8135 (defun gnus-summary-read-document (n)
8136   "Open a new group based on the current article(s).
8137 This will allow you to read digests and other similar
8138 documents as newsgroups.
8139 Obeys the standard process/prefix convention."
8140   (interactive "P")
8141   (let* ((articles (gnus-summary-work-articles n))
8142          (ogroup gnus-newsgroup-name)
8143          (params (append (gnus-info-params (gnus-get-info ogroup))
8144                          (list (cons 'to-group ogroup))))
8145          article group egroup groups vgroup)
8146     (while (setq article (pop articles))
8147       (setq group (format "%s-%d" gnus-newsgroup-name article))
8148       (gnus-summary-remove-process-mark article)
8149       (when (gnus-summary-display-article article)
8150         (save-excursion
8151           (with-temp-buffer
8152             (insert-buffer-substring gnus-original-article-buffer)
8153             ;; Remove some headers that may lead nndoc to make
8154             ;; the wrong guess.
8155             (message-narrow-to-head)
8156             (goto-char (point-min))
8157             (delete-matching-lines "^\\(Path\\):\\|^From ")
8158             (widen)
8159             (if (setq egroup
8160                       (gnus-group-read-ephemeral-group
8161                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8162                                      (nndoc-article-type guess))
8163                        t nil t))
8164                 (progn
8165                   ;; Make all postings to this group go to the parent group.
8166                   (nconc (gnus-info-params (gnus-get-info egroup))
8167                          params)
8168                   (push egroup groups))
8169               ;; Couldn't select this doc group.
8170               (gnus-error 3 "Article couldn't be entered"))))))
8171     ;; Now we have selected all the documents.
8172     (cond
8173      ((not groups)
8174       (error "None of the articles could be interpreted as documents"))
8175      ((gnus-group-read-ephemeral-group
8176        (setq vgroup (format
8177                      "nnvirtual:%s-%s" gnus-newsgroup-name
8178                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8179        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8180        t
8181        (cons (current-buffer) 'summary)))
8182      (t
8183       (error "Couldn't select virtual nndoc group")))))
8184
8185 (defun gnus-summary-isearch-article (&optional regexp-p)
8186   "Do incremental search forward on the current article.
8187 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8188   (interactive "P")
8189   (let* ((gnus-inhibit-treatment t)
8190          (old (gnus-summary-select-article)))
8191     (gnus-configure-windows 'article)
8192     (gnus-eval-in-buffer-window gnus-article-buffer
8193       (save-restriction
8194         (widen)
8195         (when (eq 'old old)
8196           (gnus-article-show-all-headers))
8197         (goto-char (point-min))
8198         (isearch-forward regexp-p)))))
8199
8200 (defun gnus-summary-search-article-forward (regexp &optional backward)
8201   "Search for an article containing REGEXP forward.
8202 If BACKWARD, search backward instead."
8203   (interactive
8204    (list (read-string
8205           (format "Search article %s (regexp%s): "
8206                   (if current-prefix-arg "backward" "forward")
8207                   (if gnus-last-search-regexp
8208                       (concat ", default " gnus-last-search-regexp)
8209                     "")))
8210          current-prefix-arg))
8211   (if (string-equal regexp "")
8212       (setq regexp (or gnus-last-search-regexp ""))
8213     (setq gnus-last-search-regexp regexp)
8214     (setq gnus-article-before-search gnus-current-article))
8215   ;; Intentionally set gnus-last-article.
8216   (setq gnus-last-article gnus-article-before-search)
8217   (let ((gnus-last-article gnus-last-article))
8218     (if (gnus-summary-search-article regexp backward)
8219         (gnus-summary-show-thread)
8220       (error "Search failed: \"%s\"" regexp))))
8221
8222 (defun gnus-summary-search-article-backward (regexp)
8223   "Search for an article containing REGEXP backward."
8224   (interactive
8225    (list (read-string
8226           (format "Search article backward (regexp%s): "
8227                   (if gnus-last-search-regexp
8228                       (concat ", default " gnus-last-search-regexp)
8229                     "")))))
8230   (gnus-summary-search-article-forward regexp 'backward))
8231
8232 (eval-when-compile
8233   (defmacro gnus-summary-search-article-position-point (regexp backward)
8234     "Dehighlight the last matched text and goto the beginning position."
8235     (` (if (and gnus-summary-search-article-matched-data
8236                 (let ((text (caddr gnus-summary-search-article-matched-data))
8237                       (inhibit-read-only t)
8238                       buffer-read-only)
8239                   (delete-region
8240                    (goto-char (car gnus-summary-search-article-matched-data))
8241                    (cadr gnus-summary-search-article-matched-data))
8242                   (insert text)
8243                   (string-match (, regexp) text)))
8244            (if (, backward) (beginning-of-line) (end-of-line))
8245          (goto-char (if (, backward) (point-max) (point-min))))))
8246
8247   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
8248     "Place point where X-Face image is displayed."
8249     (if (featurep 'xemacs)
8250         (` (let ((end (if (search-forward "\n\n" nil t)
8251                           (goto-char (1- (point)))
8252                         (point-min)))
8253                  extent)
8254              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
8255              (unless (and (re-search-forward "^From:" end t)
8256                           (setq extent (extent-at (point)))
8257                           (extent-begin-glyph extent))
8258                (goto-char (, opoint)))))
8259       (` (let ((end (if (search-forward "\n\n" nil t)
8260                         (goto-char (1- (point)))
8261                       (point-min)))
8262                (start (or (search-backward "\n\n" nil t) (point-min))))
8263            (goto-char
8264             (or (text-property-any start end 'x-face-image t);; x-face-e21
8265                 (text-property-any start end 'x-face-mule-bitmap-image t)
8266                 (, opoint)))))))
8267
8268   (defmacro gnus-summary-search-article-highlight-matched-text
8269     (backward treated x-face)
8270     "Highlight matched text in the function `gnus-summary-search-article'."
8271     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
8272              (end (set-marker (make-marker) (match-end 0)))
8273              (inhibit-read-only t)
8274              buffer-read-only)
8275          (unless treated
8276            (let ((,@
8277                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
8278                     (mapcar
8279                      (lambda (item) (setq items (delq item items)))
8280                      '(gnus-treat-buttonize
8281                        gnus-treat-fill-article
8282                        gnus-treat-fill-long-lines
8283                        gnus-treat-emphasize
8284                        gnus-treat-highlight-headers
8285                        gnus-treat-highlight-citation
8286                        gnus-treat-highlight-signature
8287                        gnus-treat-overstrike
8288                        gnus-treat-display-xface
8289                        gnus-treat-buttonize-head
8290                        gnus-treat-decode-article-as-default-mime-charset))
8291                     (static-if (featurep 'xemacs)
8292                         items
8293                       (cons '(x-face-mule-delete-x-face-field
8294                               (quote never))
8295                             items))))
8296                  (gnus-treat-display-xface
8297                   (when (, x-face) gnus-treat-display-xface)))
8298              (gnus-article-prepare-mime-display)))
8299          (goto-char (if (, backward) start end))
8300          (when (, x-face)
8301            (gnus-summary-search-article-highlight-goto-x-face (point)))
8302          (setq gnus-summary-search-article-matched-data
8303                (list start end (buffer-substring start end)))
8304          (unless (eq start end);; matched text has been deleted. :-<
8305            (put-text-property start end 'face
8306                               (or (find-face 'isearch)
8307                                   'secondary-selection))))))
8308   )
8309
8310 (defun gnus-summary-search-article (regexp &optional backward)
8311   "Search for an article containing REGEXP.
8312 Optional argument BACKWARD means do search for backward.
8313 `gnus-select-article-hook' is not called during the search."
8314   ;; We have to require this here to make sure that the following
8315   ;; dynamic binding isn't shadowed by autoloading.
8316   (require 'gnus-async)
8317   (require 'gnus-art)
8318   (let ((gnus-select-article-hook nil)  ;Disable hook.
8319         (gnus-article-prepare-hook nil)
8320         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8321         (gnus-use-article-prefetch nil)
8322         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8323         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8324         (gnus-visual nil)
8325         (gnus-keep-backlog nil)
8326         (gnus-break-pages nil)
8327         (gnus-summary-display-arrow nil)
8328         (gnus-updated-mode-lines nil)
8329         (gnus-auto-center-summary nil)
8330         (sum (current-buffer))
8331         (found nil)
8332         point treated)
8333     (gnus-save-hidden-threads
8334       (static-if (featurep 'xemacs)
8335           (let ((gnus-inhibit-treatment t))
8336             (setq treated (eq 'old (gnus-summary-select-article)))
8337             (when (and treated
8338                        (not (and (gnus-buffer-live-p gnus-article-buffer)
8339                                  (window-live-p (get-buffer-window
8340                                                  gnus-article-buffer t)))))
8341               (gnus-summary-select-article nil t)
8342               (setq treated nil)))
8343         (let ((gnus-inhibit-treatment t)
8344               (x-face-mule-delete-x-face-field 'never))
8345           (setq treated (eq 'old (gnus-summary-select-article)))
8346           (when (and treated
8347                      (not
8348                       (and (gnus-buffer-live-p gnus-article-buffer)
8349                            (window-live-p (get-buffer-window
8350                                            gnus-article-buffer t))
8351                            (or (not (string-match "^\\^X-Face:" regexp))
8352                                (with-current-buffer gnus-article-buffer
8353                                  gnus-summary-search-article-matched-data)))))
8354             (gnus-summary-select-article nil t)
8355             (setq treated nil))))
8356       (set-buffer gnus-article-buffer)
8357       (widen)
8358       (if treated
8359           (progn
8360             (gnus-article-show-all-headers)
8361             (gnus-summary-search-article-position-point regexp backward))
8362         (goto-char (if backward (point-max) (point-min))))
8363       (while (not found)
8364         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8365         (if (if backward
8366                 (re-search-backward regexp nil t)
8367               (re-search-forward regexp nil t))
8368             ;; We found the regexp.
8369             (progn
8370               (gnus-summary-search-article-highlight-matched-text
8371                backward treated (string-match "^\\^X-Face:" regexp))
8372               (setq found 'found)
8373               (forward-line
8374                (/ (- 2 (window-height
8375                         (get-buffer-window gnus-article-buffer t)))
8376                   2))
8377               (set-window-start
8378                (get-buffer-window (current-buffer))
8379                (point))
8380               (set-buffer sum)
8381               (setq point (point)))
8382           ;; We didn't find it, so we go to the next article.
8383           (set-buffer sum)
8384           (setq found 'not)
8385           (while (eq found 'not)
8386             (if (not (if backward (gnus-summary-find-prev)
8387                        (gnus-summary-find-next)))
8388                 ;; No more articles.
8389                 (setq found t)
8390               ;; Select the next article and adjust point.
8391               (unless (gnus-summary-article-sparse-p
8392                        (gnus-summary-article-number))
8393                 (setq found nil)
8394                 (let ((gnus-inhibit-treatment t))
8395                   (gnus-summary-select-article))
8396                 (setq treated nil)
8397                 (set-buffer gnus-article-buffer)
8398                 (widen)
8399                 (goto-char (if backward (point-max) (point-min))))))))
8400       (gnus-message 7 ""))
8401     ;; Return whether we found the regexp.
8402     (when (eq found 'found)
8403       (goto-char point)
8404       (gnus-summary-show-thread)
8405       (gnus-summary-goto-subject gnus-current-article)
8406       (gnus-summary-position-point)
8407       t)))
8408
8409 (defun gnus-find-matching-articles (header regexp)
8410   "Return a list of all articles that match REGEXP on HEADER.
8411 This search includes all articles in the current group that Gnus has
8412 fetched headers for, whether they are displayed or not."
8413   (let ((articles nil)
8414         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8415         (case-fold-search t))
8416     (dolist (header gnus-newsgroup-headers)
8417       (when (string-match regexp (funcall func header))
8418         (push (mail-header-number header) articles)))
8419     (nreverse articles)))
8420
8421 (defun gnus-summary-find-matching (header regexp &optional backward unread
8422                                           not-case-fold not-matching)
8423   "Return a list of all articles that match REGEXP on HEADER.
8424 The search stars on the current article and goes forwards unless
8425 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8426 If UNREAD is non-nil, only unread articles will
8427 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8428 in the comparisons. If NOT-MATCHING, return a list of all articles that
8429 not match REGEXP on HEADER."
8430   (let ((case-fold-search (not not-case-fold))
8431         articles d func)
8432     (if (consp header)
8433         (if (eq (car header) 'extra)
8434             (setq func
8435                   `(lambda (h)
8436                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8437                          "")))
8438           (error "%s is an invalid header" header))
8439       (unless (fboundp (intern (concat "mail-header-" header)))
8440         (error "%s is not a valid header" header))
8441       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8442     (dolist (d (if (eq backward 'all)
8443                    gnus-newsgroup-data
8444                  (gnus-data-find-list
8445                   (gnus-summary-article-number)
8446                   (gnus-data-list backward))))
8447       (when (and (or (not unread)       ; We want all articles...
8448                      (gnus-data-unread-p d)) ; Or just unreads.
8449                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8450                  (if not-matching
8451                      (not (string-match
8452                            regexp
8453                            (funcall func (gnus-data-header d))))
8454                    (string-match regexp
8455                                  (funcall func (gnus-data-header d)))))
8456         (push (gnus-data-number d) articles))) ; Success!
8457     (nreverse articles)))
8458
8459 (defun gnus-summary-execute-command (header regexp command &optional backward)
8460   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8461 If HEADER is an empty string (or nil), the match is done on the entire
8462 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8463   (interactive
8464    (list (let ((completion-ignore-case t))
8465            (completing-read
8466             "Header name: "
8467             (mapcar (lambda (header) (list (format "%s" header)))
8468                     (append
8469                      '("Number" "Subject" "From" "Lines" "Date"
8470                        "Message-ID" "Xref" "References" "Body")
8471                      gnus-extra-headers))
8472             nil 'require-match))
8473          (read-string "Regexp: ")
8474          (read-key-sequence "Command: ")
8475          current-prefix-arg))
8476   (when (equal header "Body")
8477     (setq header ""))
8478   ;; Hidden thread subtrees must be searched as well.
8479   (gnus-summary-show-all-threads)
8480   ;; We don't want to change current point nor window configuration.
8481   (save-excursion
8482     (save-window-excursion
8483       (let (gnus-visual
8484             gnus-treat-strip-trailing-blank-lines
8485             gnus-treat-strip-leading-blank-lines
8486             gnus-treat-strip-multiple-blank-lines
8487             gnus-treat-hide-boring-headers
8488             gnus-treat-fold-newsgroups
8489             gnus-article-prepare-hook)
8490         (gnus-message 6 "Executing %s..." (key-description command))
8491         ;; We'd like to execute COMMAND interactively so as to give arguments.
8492         (gnus-execute header regexp
8493                       `(call-interactively ',(key-binding command))
8494                       backward)
8495         (gnus-message 6 "Executing %s...done" (key-description command))))))
8496
8497 (defun gnus-summary-beginning-of-article ()
8498   "Scroll the article back to the beginning."
8499   (interactive)
8500   (gnus-summary-select-article)
8501   (gnus-configure-windows 'article)
8502   (gnus-eval-in-buffer-window gnus-article-buffer
8503     (widen)
8504     (goto-char (point-min))
8505     (when gnus-page-broken
8506       (gnus-narrow-to-page))))
8507
8508 (defun gnus-summary-end-of-article ()
8509   "Scroll to the end of the article."
8510   (interactive)
8511   (gnus-summary-select-article)
8512   (gnus-configure-windows 'article)
8513   (gnus-eval-in-buffer-window gnus-article-buffer
8514     (widen)
8515     (goto-char (point-max))
8516     (recenter -3)
8517     (when gnus-page-broken
8518       (gnus-narrow-to-page))))
8519
8520 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8521   "Truncate to LEN and quote all \"(\"'s in STRING."
8522   (gnus-replace-in-string (if (and len (> (length string) len))
8523                               (substring string 0 len)
8524                             string)
8525                           "[()]" "\\\\\\&"))
8526
8527 (defun gnus-summary-print-article (&optional filename n)
8528   "Generate and print a PostScript image of the N next (mail) articles.
8529
8530 If N is negative, print the N previous articles.  If N is nil and articles
8531 have been marked with the process mark, print these instead.
8532
8533 If the optional first argument FILENAME is nil, send the image to the
8534 printer.  If FILENAME is a string, save the PostScript image in a file with
8535 that name.  If FILENAME is a number, prompt the user for the name of the file
8536 to save in."
8537   (interactive (list (ps-print-preprint current-prefix-arg)))
8538   (dolist (article (gnus-summary-work-articles n))
8539     (gnus-summary-select-article nil nil 'pseudo article)
8540     (gnus-eval-in-buffer-window gnus-article-buffer
8541       (gnus-print-buffer))
8542     (gnus-summary-remove-process-mark article))
8543   (ps-despool filename))
8544
8545 (defun gnus-print-buffer ()
8546   (let ((buffer (generate-new-buffer " *print*")))
8547     (unwind-protect
8548         (progn
8549           (copy-to-buffer buffer (point-min) (point-max))
8550           (set-buffer buffer)
8551           (gnus-article-delete-invisible-text)
8552           (gnus-remove-text-with-property 'gnus-decoration)
8553           (when (gnus-visual-p 'article-highlight 'highlight)
8554             ;; Copy-to-buffer doesn't copy overlay.  So redo
8555             ;; highlight.
8556             (let ((gnus-article-buffer buffer))
8557               (gnus-article-highlight-citation t)
8558               (gnus-article-highlight-signature)))
8559           (let ((ps-left-header
8560                  (list
8561                   (concat "("
8562                           (gnus-summary-print-truncate-and-quote
8563                            (mail-header-subject gnus-current-headers)
8564                            66) ")")
8565                   (concat "("
8566                           (gnus-summary-print-truncate-and-quote
8567                            (mail-header-from gnus-current-headers)
8568                            45) ")")))
8569                 (ps-right-header
8570                  (list
8571                   "/pagenumberstring load"
8572                   (concat "("
8573                           (mail-header-date gnus-current-headers) ")"))))
8574             (gnus-run-hooks 'gnus-ps-print-hook)
8575             (save-excursion
8576               (if window-system
8577                   (ps-spool-buffer-with-faces)
8578                 (ps-spool-buffer)))))
8579       (kill-buffer buffer))))
8580
8581 (defun gnus-summary-show-article (&optional arg)
8582   "Force redisplaying of the current article.
8583 If ARG (the prefix) is a number, show the article with the charset
8584 defined in `gnus-summary-show-article-charset-alist', or the charset
8585 input.
8586 If ARG (the prefix) is non-nil and not a number, show the raw article
8587 without any article massaging functions being run.  Normally, the key strokes
8588 are `C-u g'."
8589   (interactive "P")
8590   (cond
8591    ((numberp arg)
8592     (gnus-summary-show-article t)
8593     (let* ((gnus-newsgroup-charset
8594             (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8595                 (mm-read-coding-system
8596                  "View as charset: " ;; actually it is coding system.
8597                  (save-excursion
8598                    (set-buffer gnus-article-buffer)
8599                    (mm-detect-coding-region (point) (point-max))))))
8600            (default-mime-charset gnus-newsgroup-charset)
8601            (gnus-newsgroup-ignored-charsets 'gnus-all))
8602       (gnus-summary-select-article nil 'force)
8603       (let ((deps gnus-newsgroup-dependencies)
8604             head header lines)
8605         (save-excursion
8606           (set-buffer gnus-original-article-buffer)
8607           (save-restriction
8608             (message-narrow-to-head)
8609             (setq head (buffer-string))
8610             (goto-char (point-min))
8611             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8612               (goto-char (point-max))
8613               (widen)
8614               (setq lines (1- (count-lines (point) (point-max))))))
8615           (with-temp-buffer
8616             (insert (format "211 %d Article retrieved.\n"
8617                             (cdr gnus-article-current)))
8618             (insert head)
8619             (if lines (insert (format "Lines: %d\n" lines)))
8620             (insert ".\n")
8621             (let ((nntp-server-buffer (current-buffer)))
8622               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8623         (gnus-data-set-header
8624          (gnus-data-find (cdr gnus-article-current))
8625          header)
8626         (gnus-summary-update-article-line
8627          (cdr gnus-article-current) header)
8628         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8629           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8630    ((not arg)
8631     ;; Select the article the normal way.
8632     (gnus-summary-select-article nil 'force))
8633    (t
8634     ;; We have to require this here to make sure that the following
8635     ;; dynamic binding isn't shadowed by autoloading.
8636     (require 'gnus-async)
8637     (require 'gnus-art)
8638     ;; Bind the article treatment functions to nil.
8639     (let ((gnus-have-all-headers t)
8640           gnus-article-prepare-hook
8641           gnus-article-decode-hook
8642           gnus-break-pages
8643           gnus-show-mime
8644           (gnus-inhibit-treatment t))
8645       (gnus-summary-select-article nil 'force))))
8646   (gnus-summary-goto-subject gnus-current-article)
8647   (gnus-summary-position-point))
8648
8649 (defun gnus-summary-show-raw-article ()
8650   "Show the raw article without any article massaging functions being run."
8651   (interactive)
8652   (gnus-summary-show-article t))
8653
8654 (defun gnus-summary-verbose-headers (&optional arg)
8655   "Toggle permanent full header display.
8656 If ARG is a positive number, turn header display on.
8657 If ARG is a negative number, turn header display off."
8658   (interactive "P")
8659   (setq gnus-show-all-headers
8660         (cond ((or (not (numberp arg))
8661                    (zerop arg))
8662                (not gnus-show-all-headers))
8663               ((natnump arg)
8664                t)))
8665   (gnus-summary-show-article))
8666
8667 (defun gnus-summary-toggle-header (&optional arg)
8668   "Show the headers if they are hidden, or hide them if they are shown.
8669 If ARG is a positive number, show the entire header.
8670 If ARG is a negative number, hide the unwanted header lines."
8671   (interactive "P")
8672   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8673                      (get-buffer-window gnus-article-buffer t))))
8674     (with-current-buffer gnus-article-buffer
8675       (widen)
8676       (article-narrow-to-head)
8677       (let* ((buffer-read-only nil)
8678              (inhibit-point-motion-hooks t)
8679              (hidden (if (numberp arg)
8680                          (>= arg 0)
8681                        (gnus-article-hidden-text-p 'headers)))
8682              s e)
8683         (delete-region (point-min) (point-max))
8684         (with-current-buffer gnus-original-article-buffer
8685           (goto-char (setq s (point-min)))
8686           (setq e (if (search-forward "\n\n" nil t)
8687                       (1- (point))
8688                     (point-max))))
8689         (insert-buffer-substring gnus-original-article-buffer s e)
8690         (article-decode-encoded-words)
8691         (if hidden
8692             (let ((gnus-treat-hide-headers nil)
8693                   (gnus-treat-hide-boring-headers nil))
8694               (gnus-delete-wash-type 'headers)
8695               (gnus-treat-article 'head))
8696           (gnus-treat-article 'head))
8697         (widen)
8698         (if window
8699             (set-window-start window (goto-char (point-min))))
8700         (setq gnus-page-broken
8701               (when gnus-break-pages
8702                 (gnus-narrow-to-page)
8703                 t))
8704         (gnus-set-mode-line 'article)))))
8705
8706 (defun gnus-summary-show-all-headers ()
8707   "Make all header lines visible."
8708   (interactive)
8709   (gnus-summary-toggle-header 1))
8710
8711 (defun gnus-summary-toggle-mime (&optional arg)
8712   "Toggle MIME processing.
8713 If ARG is a positive number, turn MIME processing on."
8714   (interactive "P")
8715   (setq gnus-show-mime
8716         (if (null arg)
8717             (not gnus-show-mime)
8718           (> (prefix-numeric-value arg) 0)))
8719   (gnus-summary-select-article t 'force))
8720
8721 (defun gnus-summary-caesar-message (&optional arg)
8722   "Caesar rotate the current article by 13.
8723 The numerical prefix specifies how many places to rotate each letter
8724 forward."
8725   (interactive "P")
8726   (gnus-summary-select-article)
8727   (let ((mail-header-separator ""))
8728     (gnus-eval-in-buffer-window gnus-article-buffer
8729       (save-restriction
8730         (widen)
8731         (let ((start (window-start))
8732               buffer-read-only)
8733           (message-caesar-buffer-body arg)
8734           (set-window-start (get-buffer-window (current-buffer)) start))))))
8735
8736 (autoload 'unmorse-region "morse"
8737   "Convert morse coded text in region to ordinary ASCII text."
8738   t)
8739
8740 (defun gnus-summary-morse-message (&optional arg)
8741   "Morse decode the current article."
8742   (interactive "P")
8743   (gnus-summary-select-article)
8744   (let ((mail-header-separator ""))
8745     (gnus-eval-in-buffer-window gnus-article-buffer
8746       (save-excursion
8747         (save-restriction
8748           (widen)
8749           (let ((pos (window-start))
8750                 buffer-read-only)
8751             (goto-char (point-min))
8752             (when (message-goto-body)
8753               (gnus-narrow-to-body))
8754             (goto-char (point-min))
8755             (while (re-search-forward "·" (point-max) t)
8756               (replace-match "."))
8757             (unmorse-region (point-min) (point-max))
8758             (widen)
8759             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8760
8761 (defun gnus-summary-stop-page-breaking ()
8762   "Stop page breaking in the current article."
8763   (interactive)
8764   (gnus-summary-select-article)
8765   (gnus-eval-in-buffer-window gnus-article-buffer
8766     (widen)
8767     (when (gnus-visual-p 'page-marker)
8768       (let ((buffer-read-only nil))
8769         (gnus-remove-text-with-property 'gnus-prev)
8770         (gnus-remove-text-with-property 'gnus-next))
8771       (setq gnus-page-broken nil))))
8772
8773 (defun gnus-summary-move-article (&optional n to-newsgroup
8774                                             select-method action)
8775   "Move the current article to a different newsgroup.
8776 If N is a positive number, move the N next articles.
8777 If N is a negative number, move the N previous articles.
8778 If N is nil and any articles have been marked with the process mark,
8779 move those articles instead.
8780 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8781 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8782 re-spool using this method.
8783
8784 When called interactively with TO-NEWSGROUP being nil, the value of
8785 the variable `gnus-move-split-methods' is used for finding a default
8786 for the target newsgroup.
8787
8788 For this function to work, both the current newsgroup and the
8789 newsgroup that you want to move to have to support the `request-move'
8790 and `request-accept' functions.
8791
8792 ACTION can be either `move' (the default), `crosspost' or `copy'."
8793   (interactive "P")
8794   (unless action
8795     (setq action 'move))
8796   ;; Check whether the source group supports the required functions.
8797   (cond ((and (eq action 'move)
8798               (not (gnus-check-backend-function
8799                     'request-move-article gnus-newsgroup-name)))
8800          (error "The current group does not support article moving"))
8801         ((and (eq action 'crosspost)
8802               (not (gnus-check-backend-function
8803                     'request-replace-article gnus-newsgroup-name)))
8804          (error "The current group does not support article editing")))
8805   (let ((articles (gnus-summary-work-articles n))
8806         (prefix (if (gnus-check-backend-function
8807                      'request-move-article gnus-newsgroup-name)
8808                     (gnus-group-real-prefix gnus-newsgroup-name)
8809                   ""))
8810         (names '((move "Move" "Moving")
8811                  (copy "Copy" "Copying")
8812                  (crosspost "Crosspost" "Crossposting")))
8813         (copy-buf (save-excursion
8814                     (nnheader-set-temp-buffer " *copy article*")))
8815         (default-marks gnus-article-mark-lists)
8816         (no-expire-marks (delete '(expirable . expire)
8817                                  (copy-sequence gnus-article-mark-lists)))
8818         art-group to-method new-xref article to-groups)
8819     (unless (assq action names)
8820       (error "Unknown action %s" action))
8821     ;; Read the newsgroup name.
8822     (when (and (not to-newsgroup)
8823                (not select-method))
8824       (if (and gnus-move-split-methods
8825                (not
8826                 (and (memq gnus-current-article articles)
8827                      (gnus-buffer-live-p gnus-original-article-buffer))))
8828           ;; When `gnus-move-split-methods' is non-nil, we have to
8829           ;; select an article to give `gnus-read-move-group-name' an
8830           ;; opportunity to suggest an appropriate default.  However,
8831           ;; we needn't render or mark the article.
8832           (let ((gnus-display-mime-function nil)
8833                 (gnus-article-prepare-hook nil)
8834                 (gnus-mark-article-hook nil))
8835             (gnus-summary-select-article nil nil nil (car articles))))
8836       (setq to-newsgroup
8837             (gnus-read-move-group-name
8838              (cadr (assq action names))
8839              (symbol-value (intern (format "gnus-current-%s-group" action)))
8840              articles prefix))
8841       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8842     (setq to-method (or select-method
8843                         (gnus-server-to-method
8844                          (gnus-group-method to-newsgroup))))
8845     ;; Check the method we are to move this article to...
8846     (unless (gnus-check-backend-function
8847              'request-accept-article (car to-method))
8848       (error "%s does not support article copying" (car to-method)))
8849     (unless (gnus-check-server to-method)
8850       (error "Can't open server %s" (car to-method)))
8851     (gnus-message 6 "%s to %s: %s..."
8852                   (caddr (assq action names))
8853                   (or (car select-method) to-newsgroup) articles)
8854     (while articles
8855       (setq article (pop articles))
8856       (setq
8857        art-group
8858        (cond
8859         ;; Move the article.
8860         ((eq action 'move)
8861          ;; Remove this article from future suppression.
8862          (gnus-dup-unsuppress-article article)
8863          (gnus-request-move-article
8864           article                       ; Article to move
8865           gnus-newsgroup-name           ; From newsgroup
8866           (nth 1 (gnus-find-method-for-group
8867                   gnus-newsgroup-name)) ; Server
8868           (list 'gnus-request-accept-article
8869                 to-newsgroup (list 'quote select-method)
8870                 (not articles) t)       ; Accept form
8871           (not articles)))              ; Only save nov last time
8872         ;; Copy the article.
8873         ((eq action 'copy)
8874          (save-excursion
8875            (set-buffer copy-buf)
8876            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8877              (gnus-request-accept-article
8878               to-newsgroup select-method (not articles) t))))
8879         ;; Crosspost the article.
8880         ((eq action 'crosspost)
8881          (let ((xref (message-tokenize-header
8882                       (mail-header-xref (gnus-summary-article-header article))
8883                       " ")))
8884            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8885                                   ":" (number-to-string article)))
8886            (unless xref
8887              (setq xref (list (system-name))))
8888            (setq new-xref
8889                  (concat
8890                   (mapconcat 'identity
8891                              (delete "Xref:" (delete new-xref xref))
8892                              " ")
8893                   " " new-xref))
8894            (save-excursion
8895              (set-buffer copy-buf)
8896              ;; First put the article in the destination group.
8897              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8898              (when (consp (setq art-group
8899                                 (gnus-request-accept-article
8900                                  to-newsgroup select-method (not articles))))
8901                (setq new-xref (concat new-xref " " (car art-group)
8902                                       ":"
8903                                       (number-to-string (cdr art-group))))
8904                ;; Now we have the new Xrefs header, so we insert
8905                ;; it and replace the new article.
8906                (nnheader-replace-header "Xref" new-xref)
8907                (gnus-request-replace-article
8908                 (cdr art-group) to-newsgroup (current-buffer))
8909                art-group))))))
8910       (cond
8911        ((not art-group)
8912         (gnus-message 1 "Couldn't %s article %s: %s"
8913                       (cadr (assq action names)) article
8914                       (nnheader-get-report (car to-method))))
8915        ((eq art-group 'junk)
8916         (when (eq action 'move)
8917           (let ((id (mail-header-id (gnus-data-header 
8918                                      (assoc article (gnus-data-list nil))))))
8919             (gnus-summary-mark-article article gnus-canceled-mark)
8920             (gnus-message 4 "Deleted article %s" article)
8921             ;; run the move/copy/crosspost/respool hook
8922             (run-hook-with-args 'gnus-summary-article-delete-hook 
8923                                 action id gnus-newsgroup-name nil
8924                                 select-method))))
8925        (t
8926         (let* ((pto-group (gnus-group-prefixed-name
8927                            (car art-group) to-method))
8928                (entry
8929                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8930                (info (nth 2 entry))
8931                (to-group (gnus-info-group info))
8932                to-marks)
8933           ;; Update the group that has been moved to.
8934           (when (and info
8935                      (memq action '(move copy)))
8936             (unless (member to-group to-groups)
8937               (push to-group to-groups))
8938
8939             (unless (memq article gnus-newsgroup-unreads)
8940               (push 'read to-marks)
8941               (gnus-info-set-read
8942                info (gnus-add-to-range (gnus-info-read info)
8943                                        (list (cdr art-group)))))
8944
8945             ;; See whether the article is to be put in the cache.
8946             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8947                              default-marks
8948                            no-expire-marks))
8949                   (to-article (cdr art-group)))
8950
8951               ;; Enter the article into the cache in the new group,
8952               ;; if that is required.
8953               (when gnus-use-cache
8954                 (gnus-cache-possibly-enter-article
8955                  to-group to-article
8956                  (let ((header (copy-sequence
8957                                 (gnus-summary-article-header article))))
8958                    (mail-header-set-number header to-article)
8959                    header)
8960                  (memq article gnus-newsgroup-marked)
8961                  (memq article gnus-newsgroup-dormant)
8962                  (memq article gnus-newsgroup-unreads)))
8963
8964               (when gnus-preserve-marks
8965                 ;; Copy any marks over to the new group.
8966                 (when (and (equal to-group gnus-newsgroup-name)
8967                            (not (memq article gnus-newsgroup-unreads)))
8968                   ;; Mark this article as read in this group.
8969                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8970                   (setcdr (gnus-active to-group) to-article)
8971                   (setcdr gnus-newsgroup-active to-article))
8972
8973                 (while marks
8974                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8975                     (when (memq article (symbol-value
8976                                          (intern (format "gnus-newsgroup-%s"
8977                                                          (caar marks)))))
8978                       (push (cdar marks) to-marks)
8979                       ;; If the other group is the same as this group,
8980                       ;; then we have to add the mark to the list.
8981                       (when (equal to-group gnus-newsgroup-name)
8982                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8983                              (cons to-article
8984                                    (symbol-value
8985                                     (intern (format "gnus-newsgroup-%s"
8986                                                     (caar marks)))))))
8987                       ;; Copy the marks to other group.
8988                       (gnus-add-marked-articles
8989                        to-group (cdar marks) (list to-article) info)))
8990                   (setq marks (cdr marks)))
8991
8992                 (gnus-request-set-mark
8993                  to-group (list (list (list to-article) 'add to-marks))))
8994
8995               (gnus-dribble-enter
8996                (concat "(gnus-group-set-info '"
8997                        (gnus-prin1-to-string (gnus-get-info to-group))
8998                        ")"))))
8999
9000           ;; Update the Xref header in this article to point to
9001           ;; the new crossposted article we have just created.
9002           (when (eq action 'crosspost)
9003             (save-excursion
9004               (set-buffer copy-buf)
9005               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9006               (nnheader-replace-header "Xref" new-xref)
9007               (gnus-request-replace-article
9008                article gnus-newsgroup-name (current-buffer))))
9009
9010           ;; run the move/copy/crosspost/respool hook
9011           (let ((id (mail-header-id (gnus-data-header 
9012                                    (assoc article (gnus-data-list nil))))))
9013           (run-hook-with-args 'gnus-summary-article-move-hook 
9014                               action id gnus-newsgroup-name to-newsgroup
9015                               select-method)))
9016
9017         ;;;!!!Why is this necessary?
9018         (set-buffer gnus-summary-buffer)
9019
9020         (gnus-summary-goto-subject article)
9021         (when (eq action 'move)
9022           (gnus-summary-mark-article article gnus-canceled-mark))))
9023       (gnus-summary-remove-process-mark article))
9024     ;; Re-activate all groups that have been moved to.
9025     (save-excursion
9026       (set-buffer gnus-group-buffer)
9027       (let ((gnus-group-marked to-groups))
9028         (gnus-group-get-new-news-this-group nil t)))
9029
9030     (gnus-kill-buffer copy-buf)
9031     (gnus-summary-position-point)
9032     (gnus-set-mode-line 'summary)))
9033
9034 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9035   "Move the current article to a different newsgroup.
9036 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9037 When called interactively, if TO-NEWSGROUP is nil, use the value of
9038 the variable `gnus-move-split-methods' for finding a default target
9039 newsgroup.
9040 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9041 re-spool using this method."
9042   (interactive "P")
9043   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9044
9045 (defun gnus-summary-crosspost-article (&optional n)
9046   "Crosspost the current article to some other group."
9047   (interactive "P")
9048   (gnus-summary-move-article n nil nil 'crosspost))
9049
9050 (defcustom gnus-summary-respool-default-method nil
9051   "Default method type for respooling an article.
9052 If nil, use to the current newsgroup method."
9053   :type 'symbol
9054   :group 'gnus-summary-mail)
9055
9056 (defcustom gnus-summary-display-while-building nil
9057   "If not-nil, show and update the summary buffer as it's being built.
9058 If the value is t, update the buffer after every line is inserted.  If
9059 the value is an integer (N), update the display every N lines."
9060   :group 'gnus-thread
9061   :type '(choice (const :tag "off" nil)
9062                  number
9063                  (const :tag "frequently" t)))
9064
9065 (defun gnus-summary-respool-article (&optional n method)
9066   "Respool the current article.
9067 The article will be squeezed through the mail spooling process again,
9068 which means that it will be put in some mail newsgroup or other
9069 depending on `nnmail-split-methods'.
9070 If N is a positive number, respool the N next articles.
9071 If N is a negative number, respool the N previous articles.
9072 If N is nil and any articles have been marked with the process mark,
9073 respool those articles instead.
9074
9075 Respooling can be done both from mail groups and \"real\" newsgroups.
9076 In the former case, the articles in question will be moved from the
9077 current group into whatever groups they are destined to.  In the
9078 latter case, they will be copied into the relevant groups."
9079   (interactive
9080    (list current-prefix-arg
9081          (let* ((methods (gnus-methods-using 'respool))
9082                 (methname
9083                  (symbol-name (or gnus-summary-respool-default-method
9084                                   (car (gnus-find-method-for-group
9085                                         gnus-newsgroup-name)))))
9086                 (method
9087                  (gnus-completing-read-with-default
9088                   methname "What backend do you want to use when respooling?"
9089                   methods nil t nil 'gnus-mail-method-history))
9090                 ms)
9091            (cond
9092             ((zerop (length (setq ms (gnus-servers-using-backend
9093                                       (intern method)))))
9094              (list (intern method) ""))
9095             ((= 1 (length ms))
9096              (car ms))
9097             (t
9098              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9099                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9100                            ms-alist))))))))
9101   (unless method
9102     (error "No method given for respooling"))
9103   (if (assoc (symbol-name
9104               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9105              (gnus-methods-using 'respool))
9106       (gnus-summary-move-article n nil method)
9107     (gnus-summary-copy-article n nil method)))
9108
9109 (defun gnus-summary-import-article (file &optional edit)
9110   "Import an arbitrary file into a mail newsgroup."
9111   (interactive "fImport file: \nP")
9112   (let ((group gnus-newsgroup-name)
9113         (now (current-time))
9114         atts lines group-art)
9115     (unless (gnus-check-backend-function 'request-accept-article group)
9116       (error "%s does not support article importing" group))
9117     (or (file-readable-p file)
9118         (not (file-regular-p file))
9119         (error "Can't read %s" file))
9120     (save-excursion
9121       (set-buffer (gnus-get-buffer-create " *import file*"))
9122       (erase-buffer)
9123       (nnheader-insert-file-contents file)
9124       (goto-char (point-min))
9125       (if (nnheader-article-p)
9126           (save-restriction
9127             (goto-char (point-min))
9128             (search-forward "\n\n" nil t)
9129             (narrow-to-region (point-min) (1- (point)))
9130             (goto-char (point-min))
9131             (unless (re-search-forward "^date:" nil t)
9132               (goto-char (point-max))
9133               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9134         ;; This doesn't look like an article, so we fudge some headers.
9135         (setq atts (file-attributes file)
9136               lines (count-lines (point-min) (point-max)))
9137         (insert "From: " (read-string "From: ") "\n"
9138                 "Subject: " (read-string "Subject: ") "\n"
9139                 "Date: " (message-make-date (nth 5 atts)) "\n"
9140                 "Message-ID: " (message-make-message-id) "\n"
9141                 "Lines: " (int-to-string lines) "\n"
9142                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9143       (setq group-art (gnus-request-accept-article group nil t))
9144       (kill-buffer (current-buffer)))
9145     (setq gnus-newsgroup-active (gnus-activate-group group))
9146     (forward-line 1)
9147     (gnus-summary-goto-article (cdr group-art) nil t)
9148     (when edit
9149       (gnus-summary-edit-article))))
9150
9151 (defun gnus-summary-create-article ()
9152   "Create an article in a mail newsgroup."
9153   (interactive)
9154   (let ((group gnus-newsgroup-name)
9155         (now (current-time))
9156         group-art)
9157     (unless (gnus-check-backend-function 'request-accept-article group)
9158       (error "%s does not support article importing" group))
9159     (save-excursion
9160       (set-buffer (gnus-get-buffer-create " *import file*"))
9161       (erase-buffer)
9162       (goto-char (point-min))
9163       ;; This doesn't look like an article, so we fudge some headers.
9164       (insert "From: " (read-string "From: ") "\n"
9165               "Subject: " (read-string "Subject: ") "\n"
9166               "Date: " (message-make-date now) "\n"
9167               "Message-ID: " (message-make-message-id) "\n")
9168       (setq group-art (gnus-request-accept-article group nil t))
9169       (kill-buffer (current-buffer)))
9170     (setq gnus-newsgroup-active (gnus-activate-group group))
9171     (forward-line 1)
9172     (gnus-summary-goto-article (cdr group-art) nil t)
9173     (gnus-summary-edit-article)))
9174
9175 (defun gnus-summary-article-posted-p ()
9176   "Say whether the current (mail) article is available from news as well.
9177 This will be the case if the article has both been mailed and posted."
9178   (interactive)
9179   (let ((id (mail-header-references (gnus-summary-article-header)))
9180         (gnus-override-method (car (gnus-refer-article-methods))))
9181     (if (gnus-request-head id "")
9182         (gnus-message 2 "The current message was found on %s"
9183                       gnus-override-method)
9184       (gnus-message 2 "The current message couldn't be found on %s"
9185                     gnus-override-method)
9186       nil)))
9187
9188 (defun gnus-summary-expire-articles (&optional now)
9189   "Expire all articles that are marked as expirable in the current group."
9190   (interactive)
9191   (when (gnus-check-backend-function
9192          'request-expire-articles gnus-newsgroup-name)
9193     ;; This backend supports expiry.
9194     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9195            (expirable (if total
9196                           (progn
9197                             ;; We need to update the info for
9198                             ;; this group for `gnus-list-of-read-articles'
9199                             ;; to give us the right answer.
9200                             (gnus-run-hooks 'gnus-exit-group-hook)
9201                             (gnus-summary-update-info)
9202                             (gnus-list-of-read-articles gnus-newsgroup-name))
9203                         (setq gnus-newsgroup-expirable
9204                               (sort gnus-newsgroup-expirable '<))))
9205            (expiry-wait (if now 'immediate
9206                           (gnus-group-find-parameter
9207                            gnus-newsgroup-name 'expiry-wait)))
9208            (nnmail-expiry-target
9209             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9210                 nnmail-expiry-target))
9211            es)
9212       (when expirable
9213         ;; There are expirable articles in this group, so we run them
9214         ;; through the expiry process.
9215         (gnus-message 6 "Expiring articles...")
9216         (unless (gnus-check-group gnus-newsgroup-name)
9217           (error "Can't open server for %s" gnus-newsgroup-name))
9218         ;; The list of articles that weren't expired is returned.
9219         (save-excursion
9220           (if expiry-wait
9221               (let ((nnmail-expiry-wait-function nil)
9222                     (nnmail-expiry-wait expiry-wait))
9223                 (setq es (gnus-request-expire-articles
9224                           expirable gnus-newsgroup-name)))
9225             (setq es (gnus-request-expire-articles
9226                       expirable gnus-newsgroup-name)))
9227           (unless total
9228             (setq gnus-newsgroup-expirable es))
9229           ;; We go through the old list of expirable, and mark all
9230           ;; really expired articles as nonexistent.
9231           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
9232             (let ((gnus-use-cache nil))
9233               (dolist (article expirable)
9234                 (when (and (not (memq article es))
9235                            (gnus-data-find article))
9236                   (gnus-summary-mark-article article gnus-canceled-mark)
9237                   (let ((id (mail-header-id (gnus-data-header 
9238                                              (assoc article 
9239                                                     (gnus-data-list nil))))))
9240                     (run-hook-with-args 'gnus-summary-article-expire-hook
9241                                         'delete id gnus-newsgroup-name nil
9242                                         nil)))))))
9243         (gnus-message 6 "Expiring articles...done")))))
9244
9245 (defun gnus-summary-expire-articles-now ()
9246   "Expunge all expirable articles in the current group.
9247 This means that *all* articles that are marked as expirable will be
9248 deleted forever, right now."
9249   (interactive)
9250   (or gnus-expert-user
9251       (gnus-yes-or-no-p
9252        "Are you really, really, really sure you want to delete all these messages? ")
9253       (error "Phew!"))
9254   (gnus-summary-expire-articles t))
9255
9256 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9257 (defun gnus-summary-delete-article (&optional n)
9258   "Delete the N next (mail) articles.
9259 This command actually deletes articles.  This is not a marking
9260 command.  The article will disappear forever from your life, never to
9261 return.
9262 If N is negative, delete backwards.
9263 If N is nil and articles have been marked with the process mark,
9264 delete these instead."
9265   (interactive "P")
9266   (unless (gnus-check-backend-function 'request-expire-articles
9267                                        gnus-newsgroup-name)
9268     (error "The current newsgroup does not support article deletion"))
9269   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9270     (error "Couldn't open server"))
9271   ;; Compute the list of articles to delete.
9272   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9273         (nnmail-expiry-target 'delete)
9274         not-deleted)
9275     (if (and gnus-novice-user
9276              (not (gnus-yes-or-no-p
9277                    (format "Do you really want to delete %s forever? "
9278                            (if (> (length articles) 1)
9279                                (format "these %s articles" (length articles))
9280                              "this article")))))
9281         ()
9282       ;; Delete the articles.
9283       (setq not-deleted (gnus-request-expire-articles
9284                          articles gnus-newsgroup-name 'force))
9285       (while articles
9286         (gnus-summary-remove-process-mark (car articles))
9287         ;; The backend might not have been able to delete the article
9288         ;; after all.
9289         (unless (memq (car articles) not-deleted)
9290           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9291         (let* ((article (car articles))
9292                (id (mail-header-id (gnus-data-header 
9293                                     (assoc article (gnus-data-list nil))))))
9294           (run-hook-with-args 'gnus-summary-article-delete-hook
9295                               'delete id gnus-newsgroup-name nil
9296                               nil))
9297         (setq articles (cdr articles)))
9298       (when not-deleted
9299         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9300     (gnus-summary-position-point)
9301     (gnus-set-mode-line 'summary)
9302     not-deleted))
9303
9304 (defun gnus-summary-edit-article (&optional force)
9305   "Edit the current article.
9306 This will have permanent effect only in mail groups.
9307 If FORCE is non-nil, allow editing of articles even in read-only
9308 groups."
9309   (interactive "P")
9310   (save-excursion
9311     (set-buffer gnus-summary-buffer)
9312     (let ((mail-parse-charset gnus-newsgroup-charset)
9313           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9314       (gnus-set-global-variables)
9315       (when (and (not force)
9316                  (gnus-group-read-only-p))
9317         (error "The current newsgroup does not support article editing"))
9318       (gnus-summary-show-article t)
9319       (gnus-article-edit-article
9320        'ignore
9321        `(lambda (no-highlight)
9322           (let ((mail-parse-charset ',gnus-newsgroup-charset)
9323                 (message-options message-options)
9324                 (message-options-set-recipient)
9325                 (mail-parse-ignored-charsets
9326                  ',gnus-newsgroup-ignored-charsets))
9327             (gnus-summary-edit-article-done
9328              ,(or (mail-header-references gnus-current-headers) "")
9329              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
9330
9331 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9332
9333 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9334                                                  no-highlight)
9335   "Make edits to the current article permanent."
9336   (interactive)
9337   (save-excursion
9338     ;; The buffer restriction contains the entire article if it exists.
9339     (when (article-goto-body)
9340       (let ((lines (count-lines (point) (point-max)))
9341             (length (- (point-max) (point)))
9342             (case-fold-search t)
9343             (body (copy-marker (point))))
9344         (goto-char (point-min))
9345         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9346           (delete-region (match-beginning 1) (match-end 1))
9347           (insert (number-to-string length)))
9348         (goto-char (point-min))
9349         (when (re-search-forward
9350                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9351           (delete-region (match-beginning 1) (match-end 1))
9352           (insert (number-to-string length)))
9353         (goto-char (point-min))
9354         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9355           (delete-region (match-beginning 1) (match-end 1))
9356           (insert (number-to-string lines))))))
9357   ;; Replace the article.
9358   (let ((buf (current-buffer)))
9359     (with-temp-buffer
9360       (insert-buffer-substring buf)
9361
9362       (if (and (not read-only)
9363                (not (gnus-request-replace-article
9364                      (cdr gnus-article-current) (car gnus-article-current)
9365                      (current-buffer) t)))
9366           (error "Couldn't replace article")
9367         ;; Update the summary buffer.
9368         (if (and references
9369                  (equal (message-tokenize-header references " ")
9370                         (message-tokenize-header
9371                          (or (message-fetch-field "references") "") " ")))
9372             ;; We only have to update this line.
9373             (save-excursion
9374               (save-restriction
9375                 (message-narrow-to-head)
9376                 (let ((head (buffer-string))
9377                       header)
9378                   (with-temp-buffer
9379                     (insert (format "211 %d Article retrieved.\n"
9380                                     (cdr gnus-article-current)))
9381                     (insert head)
9382                     (insert ".\n")
9383                     (let ((nntp-server-buffer (current-buffer)))
9384                       (setq header (car (gnus-get-newsgroup-headers
9385                                          nil t))))
9386                     (save-excursion
9387                       (set-buffer gnus-summary-buffer)
9388                       (gnus-data-set-header
9389                        (gnus-data-find (cdr gnus-article-current))
9390                        header)
9391                       (gnus-summary-update-article-line
9392                        (cdr gnus-article-current) header)
9393                       (if (gnus-summary-goto-subject
9394                            (cdr gnus-article-current) nil t)
9395                           (gnus-summary-update-secondary-mark
9396                            (cdr gnus-article-current))))))))
9397           ;; Update threads.
9398           (set-buffer (or buffer gnus-summary-buffer))
9399           (gnus-summary-update-article (cdr gnus-article-current))
9400           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9401               (gnus-summary-update-secondary-mark
9402                (cdr gnus-article-current))))
9403         ;; Prettify the article buffer again.
9404         (unless no-highlight
9405           (save-excursion
9406             (set-buffer gnus-article-buffer)
9407             ;;;!!! Fix this -- article should be rehighlighted.
9408             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9409             (set-buffer gnus-original-article-buffer)
9410             (gnus-request-article
9411              (cdr gnus-article-current)
9412              (car gnus-article-current) (current-buffer))))
9413         ;; Prettify the summary buffer line.
9414         (when (gnus-visual-p 'summary-highlight 'highlight)
9415           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9416
9417 (defun gnus-summary-edit-wash (key)
9418   "Perform editing command KEY in the article buffer."
9419   (interactive
9420    (list
9421     (progn
9422       (message "%s" (concat (this-command-keys) "- "))
9423       (read-char))))
9424   (message "")
9425   (gnus-summary-edit-article)
9426   (execute-kbd-macro (concat (this-command-keys) key))
9427   (gnus-article-edit-done))
9428
9429 ;;; Respooling
9430
9431 (defun gnus-summary-respool-query (&optional silent trace)
9432   "Query where the respool algorithm would put this article."
9433   (interactive)
9434   (let (gnus-mark-article-hook)
9435     (gnus-summary-select-article)
9436     (save-excursion
9437       (set-buffer gnus-original-article-buffer)
9438       (save-restriction
9439         (message-narrow-to-head)
9440         (let ((groups (nnmail-article-group 'identity trace)))
9441           (unless silent
9442             (if groups
9443                 (message "This message would go to %s"
9444                          (mapconcat 'car groups ", "))
9445               (message "This message would go to no groups"))
9446             groups))))))
9447
9448 (defun gnus-summary-respool-trace ()
9449   "Trace where the respool algorithm would put this article.
9450 Display a buffer showing all fancy splitting patterns which matched."
9451   (interactive)
9452   (gnus-summary-respool-query nil t))
9453
9454 ;; Summary marking commands.
9455
9456 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9457   "Mark articles which has the same subject as read, and then select the next.
9458 If UNMARK is positive, remove any kind of mark.
9459 If UNMARK is negative, tick articles."
9460   (interactive "P")
9461   (when unmark
9462     (setq unmark (prefix-numeric-value unmark)))
9463   (let ((count
9464          (gnus-summary-mark-same-subject
9465           (gnus-summary-article-subject) unmark)))
9466     ;; Select next unread article.  If auto-select-same mode, should
9467     ;; select the first unread article.
9468     (gnus-summary-next-article t (and gnus-auto-select-same
9469                                       (gnus-summary-article-subject)))
9470     (gnus-message 7 "%d article%s marked as %s"
9471                   count (if (= count 1) " is" "s are")
9472                   (if unmark "unread" "read"))))
9473
9474 (defun gnus-summary-kill-same-subject (&optional unmark)
9475   "Mark articles which has the same subject as read.
9476 If UNMARK is positive, remove any kind of mark.
9477 If UNMARK is negative, tick articles."
9478   (interactive "P")
9479   (when unmark
9480     (setq unmark (prefix-numeric-value unmark)))
9481   (let ((count
9482          (gnus-summary-mark-same-subject
9483           (gnus-summary-article-subject) unmark)))
9484     ;; If marked as read, go to next unread subject.
9485     (when (null unmark)
9486       ;; Go to next unread subject.
9487       (gnus-summary-next-subject 1 t))
9488     (gnus-message 7 "%d articles are marked as %s"
9489                   count (if unmark "unread" "read"))))
9490
9491 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9492   "Mark articles with same SUBJECT as read, and return marked number.
9493 If optional argument UNMARK is positive, remove any kinds of marks.
9494 If optional argument UNMARK is negative, mark articles as unread instead."
9495   (let ((count 1))
9496     (save-excursion
9497       (cond
9498        ((null unmark)                   ; Mark as read.
9499         (while (and
9500                 (progn
9501                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9502                   (gnus-summary-show-thread) t)
9503                 (gnus-summary-find-subject subject))
9504           (setq count (1+ count))))
9505        ((> unmark 0)                    ; Tick.
9506         (while (and
9507                 (progn
9508                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9509                   (gnus-summary-show-thread) t)
9510                 (gnus-summary-find-subject subject))
9511           (setq count (1+ count))))
9512        (t                               ; Mark as unread.
9513         (while (and
9514                 (progn
9515                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9516                   (gnus-summary-show-thread) t)
9517                 (gnus-summary-find-subject subject))
9518           (setq count (1+ count)))))
9519       (gnus-set-mode-line 'summary)
9520       ;; Return the number of marked articles.
9521       count)))
9522
9523 (defun gnus-summary-mark-as-processable (n &optional unmark)
9524   "Set the process mark on the next N articles.
9525 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9526 the process mark instead.  The difference between N and the actual
9527 number of articles marked is returned."
9528   (interactive "P")
9529   (if (and (null n) (gnus-region-active-p))
9530       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9531     (setq n (prefix-numeric-value n))
9532     (let ((backward (< n 0))
9533           (n (abs n)))
9534       (while (and
9535               (> n 0)
9536               (if unmark
9537                   (gnus-summary-remove-process-mark
9538                    (gnus-summary-article-number))
9539                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9540               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9541         (setq n (1- n)))
9542       (when (/= 0 n)
9543         (gnus-message 7 "No more articles"))
9544       (gnus-summary-recenter)
9545       (gnus-summary-position-point)
9546       n)))
9547
9548 (defun gnus-summary-unmark-as-processable (n)
9549   "Remove the process mark from the next N articles.
9550 If N is negative, unmark backward instead.  The difference between N and
9551 the actual number of articles unmarked is returned."
9552   (interactive "P")
9553   (gnus-summary-mark-as-processable n t))
9554
9555 (defun gnus-summary-unmark-all-processable ()
9556   "Remove the process mark from all articles."
9557   (interactive)
9558   (save-excursion
9559     (while gnus-newsgroup-processable
9560       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9561   (gnus-summary-position-point))
9562
9563 (defun gnus-summary-add-mark (article type)
9564   "Mark ARTICLE with a mark of TYPE."
9565   (let ((vtype (car (assq type gnus-article-mark-lists)))
9566         var)
9567     (if (not vtype)
9568         (error "No such mark type: %s" type)
9569       (setq var (intern (format "gnus-newsgroup-%s" type)))
9570       (set var (cons article (symbol-value var)))
9571       (if (memq type '(processable cached replied forwarded recent saved))
9572           (gnus-summary-update-secondary-mark article)
9573         ;;; !!! This is bogus.  We should find out what primary
9574         ;;; !!! mark we want to set.
9575         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9576
9577 (defun gnus-summary-mark-as-expirable (n)
9578   "Mark N articles forward as expirable.
9579 If N is negative, mark backward instead.  The difference between N and
9580 the actual number of articles marked is returned."
9581   (interactive "p")
9582   (gnus-summary-mark-forward n gnus-expirable-mark))
9583
9584 (defun gnus-summary-mark-as-spam (n)
9585   "Mark N articles forward as spam.
9586 If N is negative, mark backward instead.  The difference between N and
9587 the actual number of articles marked is returned."
9588   (interactive "p")
9589   (gnus-summary-mark-forward n gnus-spam-mark))
9590
9591 (defun gnus-summary-mark-article-as-replied (article)
9592   "Mark ARTICLE as replied to and update the summary line.
9593 ARTICLE can also be a list of articles."
9594   (interactive (list (gnus-summary-article-number)))
9595   (let ((articles (if (listp article) article (list article))))
9596     (dolist (article articles)
9597       (unless (numberp article)
9598         (error "%s is not a number" article))
9599       (push article gnus-newsgroup-replied)
9600       (let ((buffer-read-only nil))
9601         (when (gnus-summary-goto-subject article nil t)
9602           (gnus-summary-update-secondary-mark article))))))
9603
9604 (defun gnus-summary-mark-article-as-forwarded (article)
9605   "Mark ARTICLE as forwarded and update the summary line.
9606 ARTICLE can also be a list of articles."
9607   (let ((articles (if (listp article) article (list article))))
9608     (dolist (article articles)
9609       (push article gnus-newsgroup-forwarded)
9610       (let ((buffer-read-only nil))
9611         (when (gnus-summary-goto-subject article nil t)
9612           (gnus-summary-update-secondary-mark article))))))
9613
9614 (defun gnus-summary-set-bookmark (article)
9615   "Set a bookmark in current article."
9616   (interactive (list (gnus-summary-article-number)))
9617   (when (or (not (get-buffer gnus-article-buffer))
9618             (not gnus-current-article)
9619             (not gnus-article-current)
9620             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9621     (error "No current article selected"))
9622   ;; Remove old bookmark, if one exists.
9623   (gnus-pull article gnus-newsgroup-bookmarks)
9624   ;; Set the new bookmark, which is on the form
9625   ;; (article-number . line-number-in-body).
9626   (push
9627    (cons article
9628          (save-excursion
9629            (set-buffer gnus-article-buffer)
9630            (count-lines
9631             (min (point)
9632                  (save-excursion
9633                    (article-goto-body)
9634                    (point)))
9635             (point))))
9636    gnus-newsgroup-bookmarks)
9637   (gnus-message 6 "A bookmark has been added to the current article."))
9638
9639 (defun gnus-summary-remove-bookmark (article)
9640   "Remove the bookmark from the current article."
9641   (interactive (list (gnus-summary-article-number)))
9642   ;; Remove old bookmark, if one exists.
9643   (if (not (assq article gnus-newsgroup-bookmarks))
9644       (gnus-message 6 "No bookmark in current article.")
9645     (gnus-pull article gnus-newsgroup-bookmarks)
9646     (gnus-message 6 "Removed bookmark.")))
9647
9648 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9649 (defun gnus-summary-mark-as-dormant (n)
9650   "Mark N articles forward as dormant.
9651 If N is negative, mark backward instead.  The difference between N and
9652 the actual number of articles marked is returned."
9653   (interactive "p")
9654   (gnus-summary-mark-forward n gnus-dormant-mark))
9655
9656 (defun gnus-summary-set-process-mark (article)
9657   "Set the process mark on ARTICLE and update the summary line."
9658   (setq gnus-newsgroup-processable
9659         (cons article
9660               (delq article gnus-newsgroup-processable)))
9661   (when (gnus-summary-goto-subject article)
9662     (gnus-summary-show-thread)
9663     (gnus-summary-goto-subject article)
9664     (gnus-summary-update-secondary-mark article)))
9665
9666 (defun gnus-summary-remove-process-mark (article)
9667   "Remove the process mark from ARTICLE and update the summary line."
9668   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9669   (when (gnus-summary-goto-subject article)
9670     (gnus-summary-show-thread)
9671     (gnus-summary-goto-subject article)
9672     (gnus-summary-update-secondary-mark article)))
9673
9674 (defun gnus-summary-set-saved-mark (article)
9675   "Set the process mark on ARTICLE and update the summary line."
9676   (push article gnus-newsgroup-saved)
9677   (when (gnus-summary-goto-subject article)
9678     (gnus-summary-update-secondary-mark article)))
9679
9680 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9681   "Mark N articles as read forwards.
9682 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9683 The difference between N and the actual number of articles marked is
9684 returned.
9685 If NO-EXPIRE, auto-expiry will be inhibited."
9686   (interactive "p")
9687   (gnus-summary-show-thread)
9688   (let ((backward (< n 0))
9689         (gnus-summary-goto-unread
9690          (and gnus-summary-goto-unread
9691               (not (eq gnus-summary-goto-unread 'never))
9692               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9693                                     gnus-ticked-mark gnus-dormant-mark)))))
9694         (n (abs n))
9695         (mark (or mark gnus-del-mark)))
9696     (while (and (> n 0)
9697                 (gnus-summary-mark-article nil mark no-expire)
9698                 (zerop (gnus-summary-next-subject
9699                         (if backward -1 1)
9700                         (and gnus-summary-goto-unread
9701                              (not (eq gnus-summary-goto-unread 'never)))
9702                         t)))
9703       (setq n (1- n)))
9704     (when (/= 0 n)
9705       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9706     (gnus-summary-recenter)
9707     (gnus-summary-position-point)
9708     (gnus-set-mode-line 'summary)
9709     n))
9710
9711 (defun gnus-summary-mark-article-as-read (mark)
9712   "Mark the current article quickly as read with MARK."
9713   (let ((article (gnus-summary-article-number)))
9714     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9715     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9716     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9717     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9718     (push (cons article mark) gnus-newsgroup-reads)
9719     ;; Possibly remove from cache, if that is used.
9720     (when gnus-use-cache
9721       (gnus-cache-enter-remove-article article))
9722     ;; Allow the backend to change the mark.
9723     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9724     ;; Check for auto-expiry.
9725     (when (and gnus-newsgroup-auto-expire
9726                (memq mark gnus-auto-expirable-marks))
9727       (setq mark gnus-expirable-mark)
9728       ;; Let the backend know about the mark change.
9729       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9730       (push article gnus-newsgroup-expirable))
9731     ;; Set the mark in the buffer.
9732     (gnus-summary-update-mark mark 'unread)
9733     t))
9734
9735 (defun gnus-summary-mark-article-as-unread (mark)
9736   "Mark the current article quickly as unread with MARK."
9737   (let* ((article (gnus-summary-article-number))
9738          (old-mark (gnus-summary-article-mark article)))
9739     ;; Allow the backend to change the mark.
9740     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9741     (if (eq mark old-mark)
9742         t
9743       (if (<= article 0)
9744           (progn
9745             (gnus-error 1 "Can't mark negative article numbers")
9746             nil)
9747         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9748         (setq gnus-newsgroup-spam-marked
9749               (delq article gnus-newsgroup-spam-marked))
9750         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9751         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9752         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9753         (cond ((= mark gnus-ticked-mark)
9754                (setq gnus-newsgroup-marked
9755                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9756                                               article)))
9757               ((= mark gnus-spam-mark)
9758                (setq gnus-newsgroup-spam-marked
9759                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9760                                               article)))
9761               ((= mark gnus-dormant-mark)
9762                (setq gnus-newsgroup-dormant
9763                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9764                                               article)))
9765               (t
9766                (setq gnus-newsgroup-unreads
9767                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9768                                               article))))
9769         (gnus-pull article gnus-newsgroup-reads)
9770
9771         ;; See whether the article is to be put in the cache.
9772         (and gnus-use-cache
9773              (vectorp (gnus-summary-article-header article))
9774              (save-excursion
9775                (gnus-cache-possibly-enter-article
9776                 gnus-newsgroup-name article
9777                 (gnus-summary-article-header article)
9778                 (= mark gnus-ticked-mark)
9779                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9780
9781         ;; Fix the mark.
9782         (gnus-summary-update-mark mark 'unread)
9783         t))))
9784
9785 (defun gnus-summary-mark-article (&optional article mark no-expire)
9786   "Mark ARTICLE with MARK.  MARK can be any character.
9787 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9788 `??' (dormant) and `?E' (expirable).
9789 If MARK is nil, then the default character `?r' is used.
9790 If ARTICLE is nil, then the article on the current line will be
9791 marked.
9792 If NO-EXPIRE, auto-expiry will be inhibited."
9793   ;; The mark might be a string.
9794   (when (stringp mark)
9795     (setq mark (aref mark 0)))
9796   ;; If no mark is given, then we check auto-expiring.
9797   (when (null mark)
9798     (setq mark gnus-del-mark))
9799   (when (and (not no-expire)
9800              gnus-newsgroup-auto-expire
9801              (memq mark gnus-auto-expirable-marks))
9802     (setq mark gnus-expirable-mark))
9803   (let ((article (or article (gnus-summary-article-number)))
9804         (old-mark (gnus-summary-article-mark article)))
9805     ;; Allow the backend to change the mark.
9806     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9807     (if (eq mark old-mark)
9808         t
9809       (unless article
9810         (error "No article on current line"))
9811       (if (not (if (or (= mark gnus-unread-mark)
9812                        (= mark gnus-ticked-mark)
9813                        (= mark gnus-spam-mark)
9814                        (= mark gnus-dormant-mark))
9815                    (gnus-mark-article-as-unread article mark)
9816                  (gnus-mark-article-as-read article mark)))
9817           t
9818         ;; See whether the article is to be put in the cache.
9819         (and gnus-use-cache
9820              (not (= mark gnus-canceled-mark))
9821              (vectorp (gnus-summary-article-header article))
9822              (save-excursion
9823                (gnus-cache-possibly-enter-article
9824                 gnus-newsgroup-name article
9825                 (gnus-summary-article-header article)
9826                 (= mark gnus-ticked-mark)
9827                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9828
9829         (when (gnus-summary-goto-subject article nil t)
9830           (let ((buffer-read-only nil))
9831             (gnus-summary-show-thread)
9832             ;; Fix the mark.
9833             (gnus-summary-update-mark mark 'unread)
9834             t))))))
9835
9836 (defun gnus-summary-update-secondary-mark (article)
9837   "Update the secondary (read, process, cache) mark."
9838   (gnus-summary-update-mark
9839    (cond ((memq article gnus-newsgroup-processable)
9840           gnus-process-mark)
9841          ((memq article gnus-newsgroup-cached)
9842           gnus-cached-mark)
9843          ((memq article gnus-newsgroup-replied)
9844           gnus-replied-mark)
9845          ((memq article gnus-newsgroup-forwarded)
9846           gnus-forwarded-mark)
9847          ((memq article gnus-newsgroup-saved)
9848           gnus-saved-mark)
9849          ((memq article gnus-newsgroup-recent)
9850           gnus-recent-mark)
9851          ((memq article gnus-newsgroup-unseen)
9852           gnus-unseen-mark)
9853          (t gnus-no-mark))
9854    'replied)
9855   (when (gnus-visual-p 'summary-highlight 'highlight)
9856     (gnus-run-hooks 'gnus-summary-update-hook))
9857   t)
9858
9859 (defun gnus-summary-update-download-mark (article)
9860   "Update the secondary (read, process, cache) mark."
9861   (gnus-summary-update-mark
9862    (cond ((memq article gnus-newsgroup-undownloaded) 
9863           gnus-undownloaded-mark)
9864          (gnus-newsgroup-agentized
9865           gnus-downloaded-mark)
9866          (t
9867           gnus-no-mark))
9868    'download)
9869   (gnus-summary-update-line t)
9870   t)
9871
9872 (defun gnus-summary-update-mark (mark type)
9873   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9874         (buffer-read-only nil))
9875     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9876     (when forward
9877       (when (looking-at "\r")
9878         (incf forward))
9879       (when (<= (+ forward (point)) (point-max))
9880         ;; Go to the right position on the line.
9881         (goto-char (+ forward (point)))
9882         ;; Replace the old mark with the new mark.
9883         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9884         ;; Optionally update the marks by some user rule.
9885         (when (eq type 'unread)
9886           (gnus-data-set-mark
9887            (gnus-data-find (gnus-summary-article-number)) mark)
9888           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9889
9890 (defun gnus-mark-article-as-read (article &optional mark)
9891   "Enter ARTICLE in the pertinent lists and remove it from others."
9892   ;; Make the article expirable.
9893   (let ((mark (or mark gnus-del-mark)))
9894     (setq gnus-newsgroup-expirable
9895           (if (= mark gnus-expirable-mark)
9896               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9897             (delq article gnus-newsgroup-expirable)))
9898     ;; Remove from unread and marked lists.
9899     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9900     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9901     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9902     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9903     (push (cons article mark) gnus-newsgroup-reads)
9904     ;; Possibly remove from cache, if that is used.
9905     (when gnus-use-cache
9906       (gnus-cache-enter-remove-article article))
9907     t))
9908
9909 (defun gnus-mark-article-as-unread (article &optional mark)
9910   "Enter ARTICLE in the pertinent lists and remove it from others."
9911   (let ((mark (or mark gnus-ticked-mark)))
9912     (if (<= article 0)
9913         (progn
9914           (gnus-error 1 "Can't mark negative article numbers")
9915           nil)
9916       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9917             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9918             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9919             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9920             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9921
9922       ;; Unsuppress duplicates?
9923       (when gnus-suppress-duplicates
9924         (gnus-dup-unsuppress-article article))
9925
9926       (cond ((= mark gnus-ticked-mark)
9927              (setq gnus-newsgroup-marked
9928                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9929             ((= mark gnus-spam-mark)
9930              (setq gnus-newsgroup-spam-marked
9931                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9932                                             article)))
9933             ((= mark gnus-dormant-mark)
9934              (setq gnus-newsgroup-dormant
9935                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9936             (t
9937              (setq gnus-newsgroup-unreads
9938                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9939       (gnus-pull article gnus-newsgroup-reads)
9940       t)))
9941
9942 (defalias 'gnus-summary-mark-as-unread-forward
9943   'gnus-summary-tick-article-forward)
9944 (make-obsolete 'gnus-summary-mark-as-unread-forward
9945                'gnus-summary-tick-article-forward)
9946 (defun gnus-summary-tick-article-forward (n)
9947   "Tick N articles forwards.
9948 If N is negative, tick backwards instead.
9949 The difference between N and the number of articles ticked is returned."
9950   (interactive "p")
9951   (gnus-summary-mark-forward n gnus-ticked-mark))
9952
9953 (defalias 'gnus-summary-mark-as-unread-backward
9954   'gnus-summary-tick-article-backward)
9955 (make-obsolete 'gnus-summary-mark-as-unread-backward
9956                'gnus-summary-tick-article-backward)
9957 (defun gnus-summary-tick-article-backward (n)
9958   "Tick N articles backwards.
9959 The difference between N and the number of articles ticked is returned."
9960   (interactive "p")
9961   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9962
9963 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9964 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9965 (defun gnus-summary-tick-article (&optional article clear-mark)
9966   "Mark current article as unread.
9967 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9968 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9969   (interactive)
9970   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9971                                        gnus-ticked-mark)))
9972
9973 (defun gnus-summary-mark-as-read-forward (n)
9974   "Mark N articles as read forwards.
9975 If N is negative, mark backwards instead.
9976 The difference between N and the actual number of articles marked is
9977 returned."
9978   (interactive "p")
9979   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9980
9981 (defun gnus-summary-mark-as-read-backward (n)
9982   "Mark the N articles as read backwards.
9983 The difference between N and the actual number of articles marked is
9984 returned."
9985   (interactive "p")
9986   (gnus-summary-mark-forward
9987    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9988
9989 (defun gnus-summary-mark-as-read (&optional article mark)
9990   "Mark current article as read.
9991 ARTICLE specifies the article to be marked as read.
9992 MARK specifies a string to be inserted at the beginning of the line."
9993   (gnus-summary-mark-article article mark))
9994
9995 (defun gnus-summary-clear-mark-forward (n)
9996   "Clear marks from N articles forward.
9997 If N is negative, clear backward instead.
9998 The difference between N and the number of marks cleared is returned."
9999   (interactive "p")
10000   (gnus-summary-mark-forward n gnus-unread-mark))
10001
10002 (defun gnus-summary-clear-mark-backward (n)
10003   "Clear marks from N articles backward.
10004 The difference between N and the number of marks cleared is returned."
10005   (interactive "p")
10006   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10007
10008 (defun gnus-summary-mark-unread-as-read ()
10009   "Intended to be used by `gnus-summary-mark-article-hook'."
10010   (when (memq gnus-current-article gnus-newsgroup-unreads)
10011     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10012
10013 (defun gnus-summary-mark-read-and-unread-as-read ()
10014   "Intended to be used by `gnus-summary-mark-article-hook'."
10015   (let ((mark (gnus-summary-article-mark)))
10016     (when (or (gnus-unread-mark-p mark)
10017               (gnus-read-mark-p mark))
10018       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
10019
10020 (defun gnus-summary-mark-unread-as-ticked ()
10021   "Intended to be used by `gnus-summary-mark-article-hook'."
10022   (when (memq gnus-current-article gnus-newsgroup-unreads)
10023     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10024
10025 (defun gnus-summary-mark-region-as-read (point mark all)
10026   "Mark all unread articles between point and mark as read.
10027 If given a prefix, mark all articles between point and mark as read,
10028 even ticked and dormant ones."
10029   (interactive "r\nP")
10030   (save-excursion
10031     (let (article)
10032       (goto-char point)
10033       (beginning-of-line)
10034       (while (and
10035               (< (point) mark)
10036               (progn
10037                 (when (or all
10038                           (memq (setq article (gnus-summary-article-number))
10039                                 gnus-newsgroup-unreads))
10040                   (gnus-summary-mark-article article gnus-del-mark))
10041                 t)
10042               (gnus-summary-find-next))))))
10043
10044 (defun gnus-summary-mark-below (score mark)
10045   "Mark articles with score less than SCORE with MARK."
10046   (interactive "P\ncMark: ")
10047   (setq score (if score
10048                   (prefix-numeric-value score)
10049                 (or gnus-summary-default-score 0)))
10050   (save-excursion
10051     (set-buffer gnus-summary-buffer)
10052     (goto-char (point-min))
10053     (while
10054         (progn
10055           (and (< (gnus-summary-article-score) score)
10056                (gnus-summary-mark-article nil mark))
10057           (gnus-summary-find-next)))))
10058
10059 (defun gnus-summary-kill-below (&optional score)
10060   "Mark articles with score below SCORE as read."
10061   (interactive "P")
10062   (gnus-summary-mark-below score gnus-killed-mark))
10063
10064 (defun gnus-summary-clear-above (&optional score)
10065   "Clear all marks from articles with score above SCORE."
10066   (interactive "P")
10067   (gnus-summary-mark-above score gnus-unread-mark))
10068
10069 (defun gnus-summary-tick-above (&optional score)
10070   "Tick all articles with score above SCORE."
10071   (interactive "P")
10072   (gnus-summary-mark-above score gnus-ticked-mark))
10073
10074 (defun gnus-summary-mark-above (score mark)
10075   "Mark articles with score over SCORE with MARK."
10076   (interactive "P\ncMark: ")
10077   (setq score (if score
10078                   (prefix-numeric-value score)
10079                 (or gnus-summary-default-score 0)))
10080   (save-excursion
10081     (set-buffer gnus-summary-buffer)
10082     (goto-char (point-min))
10083     (while (and (progn
10084                   (when (> (gnus-summary-article-score) score)
10085                     (gnus-summary-mark-article nil mark))
10086                   t)
10087                 (gnus-summary-find-next)))))
10088
10089 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10090 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10091 (defun gnus-summary-limit-include-expunged (&optional no-error)
10092   "Display all the hidden articles that were expunged for low scores."
10093   (interactive)
10094   (let ((buffer-read-only nil))
10095     (let ((scored gnus-newsgroup-scored)
10096           headers h)
10097       (while scored
10098         (unless (gnus-summary-article-header (caar scored))
10099           (and (setq h (gnus-number-to-header (caar scored)))
10100                (< (cdar scored) gnus-summary-expunge-below)
10101                (push h headers)))
10102         (setq scored (cdr scored)))
10103       (if (not headers)
10104           (when (not no-error)
10105             (error "No expunged articles hidden"))
10106         (goto-char (point-min))
10107         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10108         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10109         (mapcar (lambda (x) (push (mail-header-number x)
10110                                   gnus-newsgroup-limit))
10111                 headers)
10112         (gnus-summary-prepare-unthreaded (nreverse headers))
10113         (goto-char (point-min))
10114         (gnus-summary-position-point)
10115         t))))
10116
10117 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10118   "Mark all unread articles in this newsgroup as read.
10119 If prefix argument ALL is non-nil, ticked and dormant articles will
10120 also be marked as read.
10121 If QUIETLY is non-nil, no questions will be asked.
10122 If TO-HERE is non-nil, it should be a point in the buffer.  All
10123 articles before (after, if REVERSE is set) this point will be marked as read.
10124 Note that this function will only catch up the unread article
10125 in the current summary buffer limitation.
10126 The number of articles marked as read is returned."
10127   (interactive "P")
10128   (prog1
10129       (save-excursion
10130         (when (or quietly
10131                   (not gnus-interactive-catchup) ;Without confirmation?
10132                   gnus-expert-user
10133                   (gnus-y-or-n-p
10134                    (if all
10135                        "Mark absolutely all articles as read? "
10136                      "Mark all unread articles as read? ")))
10137           (if (and not-mark
10138                    (not gnus-newsgroup-adaptive)
10139                    (not gnus-newsgroup-auto-expire)
10140                    (not gnus-suppress-duplicates)
10141                    (or (not gnus-use-cache)
10142                        (eq gnus-use-cache 'passive)))
10143               (progn
10144                 (when all
10145                   (setq gnus-newsgroup-marked nil
10146                         gnus-newsgroup-spam-marked nil
10147                         gnus-newsgroup-dormant nil))
10148                 (setq gnus-newsgroup-unreads
10149                       (gnus-sorted-nunion
10150                        (gnus-intersection gnus-newsgroup-unreads
10151                                           gnus-newsgroup-downloadable)
10152                        gnus-newsgroup-unfetched)))
10153             ;; We actually mark all articles as canceled, which we
10154             ;; have to do when using auto-expiry or adaptive scoring.
10155             (gnus-summary-show-all-threads)
10156             (if (and to-here reverse)
10157                 (progn
10158                   (goto-char to-here)
10159                   (while (and
10160                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
10161                           (gnus-summary-find-next (not all)))))
10162               (when (gnus-summary-first-subject (not all))
10163                 (while (and
10164                         (if to-here (< (point) to-here) t)
10165                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10166                         (gnus-summary-find-next (not all))))))
10167             (gnus-set-mode-line 'summary))
10168           t))
10169     (gnus-summary-position-point)))
10170
10171 (defun gnus-summary-catchup-to-here (&optional all)
10172   "Mark all unticked articles before the current one as read.
10173 If ALL is non-nil, also mark ticked and dormant articles as read."
10174   (interactive "P")
10175   (save-excursion
10176     (gnus-save-hidden-threads
10177       (let ((beg (point)))
10178         ;; We check that there are unread articles.
10179         (when (or all (gnus-summary-find-prev))
10180           (gnus-summary-catchup all t beg)))))
10181   (gnus-summary-position-point))
10182
10183 (defun gnus-summary-catchup-from-here (&optional all)
10184   "Mark all unticked articles after the current one as read.
10185 If ALL is non-nil, also mark ticked and dormant articles as read."
10186   (interactive "P")
10187   (save-excursion
10188     (gnus-save-hidden-threads
10189       (let ((beg (point)))
10190         ;; We check that there are unread articles.
10191         (when (or all (gnus-summary-find-next))
10192           (gnus-summary-catchup all t beg nil t)))))
10193   (gnus-summary-position-point))
10194
10195 (defun gnus-summary-catchup-all (&optional quietly)
10196   "Mark all articles in this newsgroup as read.
10197 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10198 instead, which marks only unread articles as read."
10199   (interactive "P")
10200   (gnus-summary-catchup t quietly))
10201
10202 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10203   "Mark all unread articles in this group as read, then exit.
10204 If prefix argument ALL is non-nil, all articles are marked as read.
10205 If QUIETLY is non-nil, no questions will be asked."
10206   (interactive "P")
10207   (when (gnus-summary-catchup all quietly nil 'fast)
10208     ;; Select next newsgroup or exit.
10209     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10210              (eq gnus-auto-select-next 'quietly))
10211         (gnus-summary-next-group nil)
10212       (gnus-summary-exit))))
10213
10214 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10215   "Mark all articles in this newsgroup as read, and then exit.
10216 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10217 instead, which marks only unread articles as read."
10218   (interactive "P")
10219   (gnus-summary-catchup-and-exit t quietly))
10220
10221 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10222   "Mark all articles in this group as read and select the next group.
10223 If given a prefix, mark all articles, unread as well as ticked, as
10224 read."
10225   (interactive "P")
10226   (save-excursion
10227     (gnus-summary-catchup all))
10228   (gnus-summary-next-group))
10229
10230 ;;;
10231 ;;; with article
10232 ;;;
10233
10234 (defmacro gnus-with-article (article &rest forms)
10235   "Select ARTICLE and perform FORMS in the original article buffer.
10236 Then replace the article with the result."
10237   `(progn
10238      ;; We don't want the article to be marked as read.
10239      (let (gnus-mark-article-hook)
10240        (gnus-summary-select-article t t nil ,article))
10241      (set-buffer gnus-original-article-buffer)
10242      ,@forms
10243      (if (not (gnus-check-backend-function
10244                'request-replace-article (car gnus-article-current)))
10245          (gnus-message 5 "Read-only group; not replacing")
10246        (unless (gnus-request-replace-article
10247                 ,article (car gnus-article-current)
10248                 (current-buffer) t)
10249          (error "Couldn't replace article")))
10250      ;; The cache and backlog have to be flushed somewhat.
10251      (when gnus-keep-backlog
10252        (gnus-backlog-remove-article
10253         (car gnus-article-current) (cdr gnus-article-current)))
10254      (when gnus-use-cache
10255        (gnus-cache-update-article
10256         (car gnus-article-current) (cdr gnus-article-current)))))
10257
10258 (put 'gnus-with-article 'lisp-indent-function 1)
10259 (put 'gnus-with-article 'edebug-form-spec '(form body))
10260
10261 ;; Thread-based commands.
10262
10263 (defun gnus-summary-articles-in-thread (&optional article)
10264   "Return a list of all articles in the current thread.
10265 If ARTICLE is non-nil, return all articles in the thread that starts
10266 with that article."
10267   (let* ((article (or article (gnus-summary-article-number)))
10268          (data (gnus-data-find-list article))
10269          (top-level (gnus-data-level (car data)))
10270          (top-subject
10271           (cond ((null gnus-thread-operation-ignore-subject)
10272                  (gnus-simplify-subject-re
10273                   (mail-header-subject (gnus-data-header (car data)))))
10274                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10275                  (gnus-simplify-subject-fuzzy
10276                   (mail-header-subject (gnus-data-header (car data)))))
10277                 (t nil)))
10278          (end-point (save-excursion
10279                       (if (gnus-summary-go-to-next-thread)
10280                           (point) (point-max))))
10281          articles)
10282     (while (and data
10283                 (< (gnus-data-pos (car data)) end-point))
10284       (when (or (not top-subject)
10285                 (string= top-subject
10286                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10287                              (gnus-simplify-subject-fuzzy
10288                               (mail-header-subject
10289                                (gnus-data-header (car data))))
10290                            (gnus-simplify-subject-re
10291                             (mail-header-subject
10292                              (gnus-data-header (car data)))))))
10293         (push (gnus-data-number (car data)) articles))
10294       (unless (and (setq data (cdr data))
10295                    (> (gnus-data-level (car data)) top-level))
10296         (setq data nil)))
10297     ;; Return the list of articles.
10298     (nreverse articles)))
10299
10300 (defun gnus-summary-rethread-current ()
10301   "Rethread the thread the current article is part of."
10302   (interactive)
10303   (let* ((gnus-show-threads t)
10304          (article (gnus-summary-article-number))
10305          (id (mail-header-id (gnus-summary-article-header)))
10306          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10307     (unless id
10308       (error "No article on the current line"))
10309     (gnus-rebuild-thread id)
10310     (gnus-summary-goto-subject article)))
10311
10312 (defun gnus-summary-reparent-thread ()
10313   "Make the current article child of the marked (or previous) article.
10314
10315 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10316 is non-nil or the Subject: of both articles are the same."
10317   (interactive)
10318   (unless (not (gnus-group-read-only-p))
10319     (error "The current newsgroup does not support article editing"))
10320   (unless (<= (length gnus-newsgroup-processable) 1)
10321     (error "No more than one article may be marked"))
10322   (save-window-excursion
10323     (let ((gnus-article-buffer " *reparent*")
10324           (current-article (gnus-summary-article-number))
10325           ;; First grab the marked article, otherwise one line up.
10326           (parent-article (if (not (null gnus-newsgroup-processable))
10327                               (car gnus-newsgroup-processable)
10328                             (save-excursion
10329                               (if (eq (forward-line -1) 0)
10330                                   (gnus-summary-article-number)
10331                                 (error "Beginning of summary buffer"))))))
10332       (unless (not (eq current-article parent-article))
10333         (error "An article may not be self-referential"))
10334       (let ((message-id (mail-header-id
10335                          (gnus-summary-article-header parent-article))))
10336         (unless (and message-id (not (equal message-id "")))
10337           (error "No message-id in desired parent"))
10338         (gnus-with-article current-article
10339           (save-restriction
10340             (goto-char (point-min))
10341             (message-narrow-to-head)
10342             (if (re-search-forward "^References: " nil t)
10343                 (progn
10344                   (re-search-forward "^[^ \t]" nil t)
10345                   (forward-line -1)
10346                   (end-of-line)
10347                   (insert " " message-id))
10348               (insert "References: " message-id "\n"))))
10349         (set-buffer gnus-summary-buffer)
10350         (gnus-summary-unmark-all-processable)
10351         (gnus-summary-update-article current-article)
10352         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10353             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10354         (gnus-summary-rethread-current)
10355         (gnus-message 3 "Article %d is now the child of article %d"
10356                       current-article parent-article)))))
10357
10358 (defun gnus-summary-toggle-threads (&optional arg)
10359   "Toggle showing conversation threads.
10360 If ARG is positive number, turn showing conversation threads on."
10361   (interactive "P")
10362   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10363     (setq gnus-show-threads
10364           (if (null arg) (not gnus-show-threads)
10365             (> (prefix-numeric-value arg) 0)))
10366     (gnus-summary-prepare)
10367     (gnus-summary-goto-subject current)
10368     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10369     (gnus-summary-position-point)))
10370
10371 (defun gnus-summary-show-all-threads ()
10372   "Show all threads."
10373   (interactive)
10374   (save-excursion
10375     (let ((buffer-read-only nil))
10376       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10377   (gnus-summary-position-point))
10378
10379 (defun gnus-summary-show-thread ()
10380   "Show thread subtrees.
10381 Returns nil if no thread was there to be shown."
10382   (interactive)
10383   (let ((buffer-read-only nil)
10384         (orig (point))
10385         ;; first goto end then to beg, to have point at beg after let
10386         (end (progn (end-of-line) (point)))
10387         (beg (progn (beginning-of-line) (point))))
10388     (prog1
10389         ;; Any hidden lines here?
10390         (search-forward "\r" end t)
10391       (subst-char-in-region beg end ?\^M ?\n t)
10392       (goto-char orig)
10393       (gnus-summary-position-point))))
10394
10395 (defun gnus-summary-maybe-hide-threads ()
10396   "If requested, hide the threads that should be hidden."
10397   (when (and gnus-show-threads
10398              gnus-thread-hide-subtree)
10399     (gnus-summary-hide-all-threads
10400      (if (or (consp gnus-thread-hide-subtree)
10401              (gnus-functionp gnus-thread-hide-subtree))
10402          (gnus-make-predicate gnus-thread-hide-subtree)
10403        nil))))
10404
10405 ;;; Hiding predicates.
10406
10407 (defun gnus-article-unread-p (header)
10408   (memq (mail-header-number header) gnus-newsgroup-unreads))
10409
10410 (defun gnus-article-unseen-p (header)
10411   (memq (mail-header-number header) gnus-newsgroup-unseen))
10412
10413 (defun gnus-map-articles (predicate articles)
10414   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10415   (apply 'gnus-or (mapcar predicate
10416                           (mapcar 'gnus-summary-article-header articles))))
10417
10418 (defun gnus-summary-hide-all-threads (&optional predicate)
10419   "Hide all thread subtrees.
10420 If PREDICATE is supplied, threads that satisfy this predicate
10421 will not be hidden."
10422   (interactive)
10423   (save-excursion
10424     (goto-char (point-min))
10425     (let ((end nil))
10426       (while (not end)
10427         (when (or (not predicate)
10428                   (gnus-map-articles
10429                    predicate (gnus-summary-article-children)))
10430             (gnus-summary-hide-thread))
10431         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10432   (gnus-summary-position-point))
10433
10434 (defun gnus-summary-hide-thread ()
10435   "Hide thread subtrees.
10436 If PREDICATE is supplied, threads that satisfy this predicate
10437 will not be hidden.
10438 Returns nil if no threads were there to be hidden."
10439   (interactive)
10440   (let ((buffer-read-only nil)
10441         (start (point))
10442         (article (gnus-summary-article-number)))
10443     (goto-char start)
10444     ;; Go forward until either the buffer ends or the subthread
10445     ;; ends.
10446     (when (and (not (eobp))
10447                (or (zerop (gnus-summary-next-thread 1 t))
10448                    (goto-char (point-max))))
10449       (prog1
10450           (if (and (> (point) start)
10451                    (search-backward "\n" start t))
10452               (progn
10453                 (subst-char-in-region start (point) ?\n ?\^M)
10454                 (gnus-summary-goto-subject article))
10455             (goto-char start)
10456             nil)))))
10457
10458 (defun gnus-summary-go-to-next-thread (&optional previous)
10459   "Go to the same level (or less) next thread.
10460 If PREVIOUS is non-nil, go to previous thread instead.
10461 Return the article number moved to, or nil if moving was impossible."
10462   (let ((level (gnus-summary-thread-level))
10463         (way (if previous -1 1))
10464         (beg (point)))
10465     (forward-line way)
10466     (while (and (not (eobp))
10467                 (< level (gnus-summary-thread-level)))
10468       (forward-line way))
10469     (if (eobp)
10470         (progn
10471           (goto-char beg)
10472           nil)
10473       (setq beg (point))
10474       (prog1
10475           (gnus-summary-article-number)
10476         (goto-char beg)))))
10477
10478 (defun gnus-summary-next-thread (n &optional silent)
10479   "Go to the same level next N'th thread.
10480 If N is negative, search backward instead.
10481 Returns the difference between N and the number of skips actually
10482 done.
10483
10484 If SILENT, don't output messages."
10485   (interactive "p")
10486   (let ((backward (< n 0))
10487         (n (abs n)))
10488     (while (and (> n 0)
10489                 (gnus-summary-go-to-next-thread backward))
10490       (decf n))
10491     (unless silent
10492       (gnus-summary-position-point))
10493     (when (and (not silent) (/= 0 n))
10494       (gnus-message 7 "No more threads"))
10495     n))
10496
10497 (defun gnus-summary-prev-thread (n)
10498   "Go to the same level previous N'th thread.
10499 Returns the difference between N and the number of skips actually
10500 done."
10501   (interactive "p")
10502   (gnus-summary-next-thread (- n)))
10503
10504 (defun gnus-summary-go-down-thread ()
10505   "Go down one level in the current thread."
10506   (let ((children (gnus-summary-article-children)))
10507     (when children
10508       (gnus-summary-goto-subject (car children)))))
10509
10510 (defun gnus-summary-go-up-thread ()
10511   "Go up one level in the current thread."
10512   (let ((parent (gnus-summary-article-parent)))
10513     (when parent
10514       (gnus-summary-goto-subject parent))))
10515
10516 (defun gnus-summary-down-thread (n)
10517   "Go down thread N steps.
10518 If N is negative, go up instead.
10519 Returns the difference between N and how many steps down that were
10520 taken."
10521   (interactive "p")
10522   (let ((up (< n 0))
10523         (n (abs n)))
10524     (while (and (> n 0)
10525                 (if up (gnus-summary-go-up-thread)
10526                   (gnus-summary-go-down-thread)))
10527       (setq n (1- n)))
10528     (gnus-summary-position-point)
10529     (when (/= 0 n)
10530       (gnus-message 7 "Can't go further"))
10531     n))
10532
10533 (defun gnus-summary-up-thread (n)
10534   "Go up thread N steps.
10535 If N is negative, go down instead.
10536 Returns the difference between N and how many steps down that were
10537 taken."
10538   (interactive "p")
10539   (gnus-summary-down-thread (- n)))
10540
10541 (defun gnus-summary-top-thread ()
10542   "Go to the top of the thread."
10543   (interactive)
10544   (while (gnus-summary-go-up-thread))
10545   (gnus-summary-article-number))
10546
10547 (defun gnus-summary-kill-thread (&optional unmark)
10548   "Mark articles under current thread as read.
10549 If the prefix argument is positive, remove any kinds of marks.
10550 If the prefix argument is negative, tick articles instead."
10551   (interactive "P")
10552   (when unmark
10553     (setq unmark (prefix-numeric-value unmark)))
10554   (let ((articles (gnus-summary-articles-in-thread)))
10555     (save-excursion
10556       ;; Expand the thread.
10557       (gnus-summary-show-thread)
10558       ;; Mark all the articles.
10559       (while articles
10560         (gnus-summary-goto-subject (car articles))
10561         (cond ((null unmark)
10562                (gnus-summary-mark-article-as-read gnus-killed-mark))
10563               ((> unmark 0)
10564                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10565               (t
10566                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10567         (setq articles (cdr articles))))
10568     ;; Hide killed subtrees.
10569     (and (null unmark)
10570          gnus-thread-hide-killed
10571          (gnus-summary-hide-thread))
10572     ;; If marked as read, go to next unread subject.
10573     (when (null unmark)
10574       ;; Go to next unread subject.
10575       (gnus-summary-next-subject 1 t)))
10576   (gnus-set-mode-line 'summary))
10577
10578 ;; Summary sorting commands
10579
10580 (defun gnus-summary-sort-by-number (&optional reverse)
10581   "Sort the summary buffer by article number.
10582 Argument REVERSE means reverse order."
10583   (interactive "P")
10584   (gnus-summary-sort 'number reverse))
10585
10586 (defun gnus-summary-sort-by-random (&optional reverse)
10587   "Randomize the order in the summary buffer.
10588 Argument REVERSE means to randomize in reverse order."
10589   (interactive "P")
10590   (gnus-summary-sort 'random reverse))
10591
10592 (defun gnus-summary-sort-by-author (&optional reverse)
10593   "Sort the summary buffer by author name alphabetically.
10594 If `case-fold-search' is non-nil, case of letters is ignored.
10595 Argument REVERSE means reverse order."
10596   (interactive "P")
10597   (gnus-summary-sort 'author reverse))
10598
10599 (defun gnus-summary-sort-by-subject (&optional reverse)
10600   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10601 If `case-fold-search' is non-nil, case of letters is ignored.
10602 Argument REVERSE means reverse order."
10603   (interactive "P")
10604   (gnus-summary-sort 'subject reverse))
10605
10606 (defun gnus-summary-sort-by-date (&optional reverse)
10607   "Sort the summary buffer by date.
10608 Argument REVERSE means reverse order."
10609   (interactive "P")
10610   (gnus-summary-sort 'date reverse))
10611
10612 (defun gnus-summary-sort-by-score (&optional reverse)
10613   "Sort the summary buffer by score.
10614 Argument REVERSE means reverse order."
10615   (interactive "P")
10616   (gnus-summary-sort 'score reverse))
10617
10618 (defun gnus-summary-sort-by-lines (&optional reverse)
10619   "Sort the summary buffer by the number of lines.
10620 Argument REVERSE means reverse order."
10621   (interactive "P")
10622   (gnus-summary-sort 'lines reverse))
10623
10624 (defun gnus-summary-sort-by-chars (&optional reverse)
10625   "Sort the summary buffer by article length.
10626 Argument REVERSE means reverse order."
10627   (interactive "P")
10628   (gnus-summary-sort 'chars reverse))
10629
10630 (defun gnus-summary-sort-by-original (&optional reverse)
10631   "Sort the summary buffer using the default sorting method.
10632 Argument REVERSE means reverse order."
10633   (interactive "P")
10634   (let* ((buffer-read-only)
10635          (gnus-summary-prepare-hook nil))
10636     ;; We do the sorting by regenerating the threads.
10637     (gnus-summary-prepare)
10638     ;; Hide subthreads if needed.
10639     (gnus-summary-maybe-hide-threads)))
10640
10641 (defun gnus-summary-sort (predicate reverse)
10642   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10643   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10644          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10645          (gnus-thread-sort-functions
10646           (if (not reverse)
10647               thread
10648             `(lambda (t1 t2)
10649                (,thread t2 t1))))
10650          (gnus-sort-gathered-threads-function
10651           gnus-thread-sort-functions)
10652          (gnus-article-sort-functions
10653           (if (not reverse)
10654               article
10655             `(lambda (t1 t2)
10656                (,article t2 t1))))
10657          (buffer-read-only)
10658          (gnus-summary-prepare-hook nil))
10659     ;; We do the sorting by regenerating the threads.
10660     (gnus-summary-prepare)
10661     ;; Hide subthreads if needed.
10662     (gnus-summary-maybe-hide-threads)))
10663
10664 ;; Summary saving commands.
10665
10666 (defun gnus-summary-save-article (&optional n not-saved)
10667   "Save the current article using the default saver function.
10668 If N is a positive number, save the N next articles.
10669 If N is a negative number, save the N previous articles.
10670 If N is nil and any articles have been marked with the process mark,
10671 save those articles instead.
10672 The variable `gnus-default-article-saver' specifies the saver function."
10673   (interactive "P")
10674   (let* ((articles (gnus-summary-work-articles n))
10675          (save-buffer (save-excursion
10676                         (nnheader-set-temp-buffer " *Gnus Save*")))
10677          (num (length articles))
10678          header file)
10679     (dolist (article articles)
10680       (setq header (gnus-summary-article-header article))
10681       (if (not (vectorp header))
10682           ;; This is a pseudo-article.
10683           (if (assq 'name header)
10684               (gnus-copy-file (cdr (assq 'name header)))
10685             (gnus-message 1 "Article %d is unsaveable" article))
10686         ;; This is a real article.
10687         (save-window-excursion
10688           (let ((gnus-display-mime-function nil)
10689                 (gnus-article-prepare-hook nil))
10690             (gnus-summary-select-article t nil nil article)))
10691         (save-excursion
10692           (set-buffer save-buffer)
10693           (erase-buffer)
10694           (insert-buffer-substring gnus-original-article-buffer))
10695         (setq file (gnus-article-save save-buffer file num))
10696         (gnus-summary-remove-process-mark article)
10697         (unless not-saved
10698           (gnus-summary-set-saved-mark article))))
10699     (gnus-kill-buffer save-buffer)
10700     (gnus-summary-position-point)
10701     (gnus-set-mode-line 'summary)
10702     n))
10703
10704 (defun gnus-summary-pipe-output (&optional arg headers)
10705   "Pipe the current article to a subprocess.
10706 If N is a positive number, pipe the N next articles.
10707 If N is a negative number, pipe the N previous articles.
10708 If N is nil and any articles have been marked with the process mark,
10709 pipe those articles instead.
10710 If HEADERS (the symbolic prefix), include the headers, too."
10711   (interactive (gnus-interactive "P\ny"))
10712   (require 'gnus-art)
10713   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10714         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10715     (gnus-summary-save-article arg t))
10716   (let ((buffer (get-buffer "*Shell Command Output*")))
10717     (when (and buffer
10718                (not (zerop (buffer-size buffer))))
10719       (gnus-configure-windows 'pipe))))
10720
10721 (defun gnus-summary-save-article-mail (&optional arg)
10722   "Append the current article to an mail file.
10723 If N is a positive number, save the N next articles.
10724 If N is a negative number, save the N previous articles.
10725 If N is nil and any articles have been marked with the process mark,
10726 save those articles instead."
10727   (interactive "P")
10728   (require 'gnus-art)
10729   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10730     (gnus-summary-save-article arg)))
10731
10732 (defun gnus-summary-save-article-rmail (&optional arg)
10733   "Append the current article to an rmail file.
10734 If N is a positive number, save the N next articles.
10735 If N is a negative number, save the N previous articles.
10736 If N is nil and any articles have been marked with the process mark,
10737 save those articles instead."
10738   (interactive "P")
10739   (require 'gnus-art)
10740   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10741     (gnus-summary-save-article arg)))
10742
10743 (defun gnus-summary-save-article-file (&optional arg)
10744   "Append the current article to a file.
10745 If N is a positive number, save the N next articles.
10746 If N is a negative number, save the N previous articles.
10747 If N is nil and any articles have been marked with the process mark,
10748 save those articles instead."
10749   (interactive "P")
10750   (require 'gnus-art)
10751   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10752     (gnus-summary-save-article arg)))
10753
10754 (defun gnus-summary-write-article-file (&optional arg)
10755   "Write the current article to a file, deleting the previous file.
10756 If N is a positive number, save the N next articles.
10757 If N is a negative number, save the N previous articles.
10758 If N is nil and any articles have been marked with the process mark,
10759 save those articles instead."
10760   (interactive "P")
10761   (require 'gnus-art)
10762   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10763     (gnus-summary-save-article arg)))
10764
10765 (defun gnus-summary-save-article-body-file (&optional arg)
10766   "Append the current article body to a file.
10767 If N is a positive number, save the N next articles.
10768 If N is a negative number, save the N previous articles.
10769 If N is nil and any articles have been marked with the process mark,
10770 save those articles instead."
10771   (interactive "P")
10772   (require 'gnus-art)
10773   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10774     (gnus-summary-save-article arg)))
10775
10776 (defun gnus-summary-muttprint (&optional arg)
10777   "Print the current article using Muttprint.
10778 If N is a positive number, save the N next articles.
10779 If N is a negative number, save the N previous articles.
10780 If N is nil and any articles have been marked with the process mark,
10781 save those articles instead."
10782   (interactive "P")
10783   (require 'gnus-art)
10784   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10785     (gnus-summary-save-article arg t)))
10786
10787 (defun gnus-summary-pipe-message (program)
10788   "Pipe the current article through PROGRAM."
10789   (interactive "sProgram: ")
10790   (gnus-summary-select-article)
10791   (let ((mail-header-separator ""))
10792     (gnus-eval-in-buffer-window gnus-article-buffer
10793       (save-restriction
10794         (widen)
10795         (let ((start (window-start))
10796               buffer-read-only)
10797           (message-pipe-buffer-body program)
10798           (set-window-start (get-buffer-window (current-buffer)) start))))))
10799
10800 (defun gnus-get-split-value (methods)
10801   "Return a value based on the split METHODS."
10802   (let (split-name method result match)
10803     (when methods
10804       (save-excursion
10805         (set-buffer gnus-original-article-buffer)
10806         (save-restriction
10807           (nnheader-narrow-to-headers)
10808           (while (and methods (not split-name))
10809             (goto-char (point-min))
10810             (setq method (pop methods))
10811             (setq match (car method))
10812             (when (cond
10813                    ((stringp match)
10814                     ;; Regular expression.
10815                     (ignore-errors
10816                       (re-search-forward match nil t)))
10817                    ((gnus-functionp match)
10818                     ;; Function.
10819                     (save-restriction
10820                       (widen)
10821                       (setq result (funcall match gnus-newsgroup-name))))
10822                    ((consp match)
10823                     ;; Form.
10824                     (save-restriction
10825                       (widen)
10826                       (setq result (eval match)))))
10827               (setq split-name (cdr method))
10828               (cond ((stringp result)
10829                      (push (expand-file-name
10830                             result gnus-article-save-directory)
10831                            split-name))
10832                     ((consp result)
10833                      (setq split-name (append result split-name)))))))))
10834     (nreverse split-name)))
10835
10836 (defun gnus-valid-move-group-p (group)
10837   (and (boundp group)
10838        (symbol-name group)
10839        (symbol-value group)
10840        (gnus-get-function (gnus-find-method-for-group
10841                            (symbol-name group)) 'request-accept-article t)))
10842
10843 (defun gnus-read-move-group-name (prompt default articles prefix)
10844   "Read a group name."
10845   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10846          (minibuffer-confirm-incomplete nil) ; XEmacs
10847          (prom
10848           (format "%s %s to:"
10849                   prompt
10850                   (if (> (length articles) 1)
10851                       (format "these %d articles" (length articles))
10852                     "this article")))
10853          (to-newsgroup
10854           (cond
10855            ((null split-name)
10856             (gnus-completing-read-with-default
10857              default prom
10858              gnus-active-hashtb
10859              'gnus-valid-move-group-p
10860              nil prefix
10861              'gnus-group-history))
10862            ((= 1 (length split-name))
10863             (gnus-completing-read-with-default
10864              (car split-name) prom
10865              gnus-active-hashtb
10866              'gnus-valid-move-group-p
10867              nil nil
10868              'gnus-group-history))
10869            (t
10870             (gnus-completing-read-with-default
10871              nil prom
10872              (mapcar (lambda (el) (list el))
10873                      (nreverse split-name))
10874              nil nil nil
10875              'gnus-group-history))))
10876          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10877     (when to-newsgroup
10878       (if (or (string= to-newsgroup "")
10879               (string= to-newsgroup prefix))
10880           (setq to-newsgroup default))
10881       (unless to-newsgroup
10882         (error "No group name entered"))
10883       (or (gnus-active to-newsgroup)
10884           (gnus-activate-group to-newsgroup nil nil to-method)
10885           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10886                                      to-newsgroup))
10887               (or (and (gnus-request-create-group to-newsgroup to-method)
10888                        (gnus-activate-group
10889                         to-newsgroup nil nil to-method)
10890                        (gnus-subscribe-group to-newsgroup))
10891                   (error "Couldn't create group %s" to-newsgroup)))
10892           (error "No such group: %s" to-newsgroup)))
10893     to-newsgroup))
10894
10895 (defun gnus-summary-save-parts (type dir n &optional reverse)
10896   "Save parts matching TYPE to DIR.
10897 If REVERSE, save parts that do not match TYPE."
10898   (interactive
10899    (list (read-string "Save parts of type: "
10900                       (or (car gnus-summary-save-parts-type-history)
10901                           gnus-summary-save-parts-default-mime)
10902                       'gnus-summary-save-parts-type-history)
10903          (setq gnus-summary-save-parts-last-directory
10904                (read-file-name "Save to directory: "
10905                                gnus-summary-save-parts-last-directory
10906                                nil t))
10907          current-prefix-arg))
10908   (gnus-summary-iterate n
10909     (let ((gnus-display-mime-function nil)
10910           (gnus-inhibit-treatment t))
10911       (gnus-summary-select-article))
10912     (save-excursion
10913       (set-buffer gnus-article-buffer)
10914       (let ((handles (or gnus-article-mime-handles
10915                          (mm-dissect-buffer nil gnus-article-loose-mime)
10916                          (and gnus-article-emulate-mime
10917                               (mm-uu-dissect)))))
10918         (when handles
10919           (gnus-summary-save-parts-1 type dir handles reverse)
10920           (unless gnus-article-mime-handles ;; Don't destroy this case.
10921             (mm-destroy-parts handles)))))))
10922
10923 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10924   (if (stringp (car handle))
10925       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10926               (cdr handle))
10927     (when (if reverse
10928               (not (string-match type (mm-handle-media-type handle)))
10929             (string-match type (mm-handle-media-type handle)))
10930       (let ((file (expand-file-name
10931                    (file-name-nondirectory
10932                     (or
10933                      (mail-content-type-get
10934                       (mm-handle-disposition handle) 'filename)
10935                      (concat gnus-newsgroup-name
10936                              "." (number-to-string
10937                                   (cdr gnus-article-current)))))
10938                    dir)))
10939         (unless (file-exists-p file)
10940           (mm-save-part-to-file handle file))))))
10941
10942 ;; Summary extract commands
10943
10944 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10945   (let ((buffer-read-only nil)
10946         (article (gnus-summary-article-number))
10947         after-article b e)
10948     (unless (gnus-summary-goto-subject article)
10949       (error "No such article: %d" article))
10950     (gnus-summary-position-point)
10951     ;; If all commands are to be bunched up on one line, we collect
10952     ;; them here.
10953     (unless gnus-view-pseudos-separately
10954       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10955             files action)
10956         (while ps
10957           (setq action (cdr (assq 'action (car ps))))
10958           (setq files (list (cdr (assq 'name (car ps)))))
10959           (while (and ps (cdr ps)
10960                       (string= (or action "1")
10961                                (or (cdr (assq 'action (cadr ps))) "2")))
10962             (push (cdr (assq 'name (cadr ps))) files)
10963             (setcdr ps (cddr ps)))
10964           (when files
10965             (when (not (string-match "%s" action))
10966               (push " " files))
10967             (push " " files)
10968             (when (assq 'execute (car ps))
10969               (setcdr (assq 'execute (car ps))
10970                       (funcall (if (string-match "%s" action)
10971                                    'format 'concat)
10972                                action
10973                                (mapconcat
10974                                 (lambda (f)
10975                                   (if (equal f " ")
10976                                       f
10977                                     (gnus-quote-arg-for-sh-or-csh f)))
10978                                 files " ")))))
10979           (setq ps (cdr ps)))))
10980     (if (and gnus-view-pseudos (not not-view))
10981         (while pslist
10982           (when (assq 'execute (car pslist))
10983             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10984                                   (eq gnus-view-pseudos 'not-confirm)))
10985           (setq pslist (cdr pslist)))
10986       (save-excursion
10987         (while pslist
10988           (setq after-article (or (cdr (assq 'article (car pslist)))
10989                                   (gnus-summary-article-number)))
10990           (gnus-summary-goto-subject after-article)
10991           (forward-line 1)
10992           (setq b (point))
10993           (insert "    " (file-name-nondirectory
10994                           (cdr (assq 'name (car pslist))))
10995                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10996           (setq e (point))
10997           (forward-line -1)             ; back to `b'
10998           (gnus-add-text-properties
10999            b (1- e) (list 'gnus-number gnus-reffed-article-number
11000                           gnus-mouse-face-prop gnus-mouse-face))
11001           (gnus-data-enter
11002            after-article gnus-reffed-article-number
11003            gnus-unread-mark b (car pslist) 0 (- e b))
11004           (setq gnus-newsgroup-unreads
11005                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11006                                          gnus-reffed-article-number))
11007           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11008           (setq pslist (cdr pslist)))))))
11009
11010 (defun gnus-pseudos< (p1 p2)
11011   (let ((c1 (cdr (assq 'action p1)))
11012         (c2 (cdr (assq 'action p2))))
11013     (and c1 c2 (string< c1 c2))))
11014
11015 (defun gnus-request-pseudo-article (props)
11016   (cond ((assq 'execute props)
11017          (gnus-execute-command (cdr (assq 'execute props)))))
11018   (let ((gnus-current-article (gnus-summary-article-number)))
11019     (gnus-run-hooks 'gnus-mark-article-hook)))
11020
11021 (defun gnus-execute-command (command &optional automatic)
11022   (save-excursion
11023     (gnus-article-setup-buffer)
11024     (set-buffer gnus-article-buffer)
11025     (setq buffer-read-only nil)
11026     (let ((command (if automatic command
11027                      (read-string "Command: " (cons command 0)))))
11028       (erase-buffer)
11029       (insert "$ " command "\n\n")
11030       (if gnus-view-pseudo-asynchronously
11031           (start-process "gnus-execute" (current-buffer) shell-file-name
11032                          shell-command-switch command)
11033         (call-process shell-file-name nil t nil
11034                       shell-command-switch command)))))
11035
11036 ;; Summary kill commands.
11037
11038 (defun gnus-summary-edit-global-kill (article)
11039   "Edit the \"global\" kill file."
11040   (interactive (list (gnus-summary-article-number)))
11041   (gnus-group-edit-global-kill article))
11042
11043 (defun gnus-summary-edit-local-kill ()
11044   "Edit a local kill file applied to the current newsgroup."
11045   (interactive)
11046   (setq gnus-current-headers (gnus-summary-article-header))
11047   (gnus-group-edit-local-kill
11048    (gnus-summary-article-number) gnus-newsgroup-name))
11049
11050 ;;; Header reading.
11051
11052 (defun gnus-read-header (id &optional header)
11053   "Read the headers of article ID and enter them into the Gnus system."
11054   (let ((group gnus-newsgroup-name)
11055         (gnus-override-method
11056          (or
11057           gnus-override-method
11058           (and (gnus-news-group-p gnus-newsgroup-name)
11059                (car (gnus-refer-article-methods)))))
11060         where)
11061     ;; First we check to see whether the header in question is already
11062     ;; fetched.
11063     (if (stringp id)
11064         ;; This is a Message-ID.
11065         (setq header (or header (gnus-id-to-header id)))
11066       ;; This is an article number.
11067       (setq header (or header (gnus-summary-article-header id))))
11068     (if (and header
11069              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11070         ;; We have found the header.
11071         header
11072       ;; If this is a sparse article, we have to nix out its
11073       ;; previous entry in the thread hashtb.
11074       (when (and header
11075                  (gnus-summary-article-sparse-p (mail-header-number header)))
11076         (let* ((parent (gnus-parent-id (mail-header-references header)))
11077                (thread (and parent (gnus-id-to-thread parent))))
11078           (when thread
11079             (delq (assq header thread) thread))))
11080       ;; We have to really fetch the header to this article.
11081       (save-excursion
11082         (set-buffer nntp-server-buffer)
11083         (when (setq where (gnus-request-head id group))
11084           (nnheader-fold-continuation-lines)
11085           (goto-char (point-max))
11086           (insert ".\n")
11087           (goto-char (point-min))
11088           (insert "211 ")
11089           (princ (cond
11090                   ((numberp id) id)
11091                   ((cdr where) (cdr where))
11092                   (header (mail-header-number header))
11093                   (t gnus-reffed-article-number))
11094                  (current-buffer))
11095           (insert " Article retrieved.\n"))
11096         (if (or (not where)
11097                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11098             ()                          ; Malformed head.
11099           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11100             (when (and (stringp id)
11101                        (not (string= (gnus-group-real-name group)
11102                                      (car where))))
11103               ;; If we fetched by Message-ID and the article came
11104               ;; from a different group, we fudge some bogus article
11105               ;; numbers for this article.
11106               (mail-header-set-number header gnus-reffed-article-number))
11107             (save-excursion
11108               (set-buffer gnus-summary-buffer)
11109               (decf gnus-reffed-article-number)
11110               (gnus-remove-header (mail-header-number header))
11111               (push header gnus-newsgroup-headers)
11112               (setq gnus-current-headers header)
11113               (push (mail-header-number header) gnus-newsgroup-limit)))
11114           header)))))
11115
11116 (defun gnus-remove-header (number)
11117   "Remove header NUMBER from `gnus-newsgroup-headers'."
11118   (if (and gnus-newsgroup-headers
11119            (= number (mail-header-number (car gnus-newsgroup-headers))))
11120       (pop gnus-newsgroup-headers)
11121     (let ((headers gnus-newsgroup-headers))
11122       (while (and (cdr headers)
11123                   (not (= number (mail-header-number (cadr headers)))))
11124         (pop headers))
11125       (when (cdr headers)
11126         (setcdr headers (cddr headers))))))
11127
11128 ;;;
11129 ;;; summary highlights
11130 ;;;
11131
11132 (defun gnus-highlight-selected-summary ()
11133   "Highlight selected article in summary buffer."
11134   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11135   (when gnus-summary-selected-face
11136     (save-excursion
11137       (let* ((beg (progn (beginning-of-line) (point)))
11138              (end (progn (end-of-line) (point)))
11139              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11140              (from (if (get-text-property beg gnus-mouse-face-prop)
11141                        beg
11142                      (or (next-single-property-change
11143                           beg gnus-mouse-face-prop nil end)
11144                          beg)))
11145              (to
11146               (if (= from end)
11147                   (- from 2)
11148                 (or (next-single-property-change
11149                      from gnus-mouse-face-prop nil end)
11150                     end))))
11151         ;; If no mouse-face prop on line we will have to = from = end,
11152         ;; so we highlight the entire line instead.
11153         (when (= (+ to 2) from)
11154           (setq from beg)
11155           (setq to end))
11156         (if gnus-newsgroup-selected-overlay
11157             ;; Move old overlay.
11158             (gnus-move-overlay
11159              gnus-newsgroup-selected-overlay from to (current-buffer))
11160           ;; Create new overlay.
11161           (gnus-overlay-put
11162            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11163            'face gnus-summary-selected-face))))))
11164
11165 (defvar gnus-summary-highlight-line-cached nil)
11166 (defvar gnus-summary-highlight-line-trigger nil)
11167
11168 (defun gnus-summary-highlight-line-0 ()
11169   (if (and (eq gnus-summary-highlight-line-trigger 
11170                gnus-summary-highlight)
11171            gnus-summary-highlight-line-cached)
11172       gnus-summary-highlight-line-cached
11173     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11174           gnus-summary-highlight-line-cached
11175           (let* ((cond (list 'cond))
11176                  (c cond)
11177                  (list gnus-summary-highlight))
11178             (while list
11179               (setcdr c (cons (list (caar list) (list 'quote (cdar list))) nil))
11180               (setq c (cdr c)
11181                     list (cdr list)))
11182             (gnus-byte-compile (list 'lambda nil cond))))))
11183
11184 (defun gnus-summary-highlight-line ()
11185   "Highlight current line according to `gnus-summary-highlight'."
11186   (let* ((beg (gnus-point-at-bol))
11187          (article (or (gnus-summary-article-number) gnus-current-article))
11188          (score (or (cdr (assq article
11189                                gnus-newsgroup-scored))
11190                     gnus-summary-default-score 0))
11191          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11192          (inhibit-read-only t)
11193          (default gnus-summary-default-score)
11194          (default-high gnus-summary-default-high-score)
11195          (default-low gnus-summary-default-low-score)
11196          (uncached (memq article gnus-newsgroup-undownloaded))
11197          (downloaded (not uncached)))
11198     (let ((face (funcall (gnus-summary-highlight-line-0))))
11199       (unless (eq face (get-text-property beg 'face))
11200         (gnus-put-text-property-excluding-characters-with-faces
11201          beg (gnus-point-at-eol) 'face
11202          (setq face (if (boundp face) (symbol-value face) face)))
11203         (when gnus-summary-highlight-line-function
11204           (funcall gnus-summary-highlight-line-function article face))))))
11205
11206 (defun gnus-update-read-articles (group unread &optional compute)
11207   "Update the list of read articles in GROUP.
11208 UNREAD is a sorted list."
11209   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11210          (entry (gnus-gethash group gnus-newsrc-hashtb))
11211          (info (nth 2 entry))
11212          (prev 1)
11213          read)
11214     (if (or (not info) (not active))
11215         ;; There is no info on this group if it was, in fact,
11216         ;; killed.  Gnus stores no information on killed groups, so
11217         ;; there's nothing to be done.
11218         ;; One could store the information somewhere temporarily,
11219         ;; perhaps...  Hmmm...
11220         ()
11221       ;; Remove any negative articles numbers.
11222       (while (and unread (< (car unread) 0))
11223         (setq unread (cdr unread)))
11224       ;; Remove any expired article numbers
11225       (while (and unread (< (car unread) (car active)))
11226         (setq unread (cdr unread)))
11227       ;; Compute the ranges of read articles by looking at the list of
11228       ;; unread articles.
11229       (while unread
11230         (when (/= (car unread) prev)
11231           (push (if (= prev (1- (car unread))) prev
11232                   (cons prev (1- (car unread))))
11233                 read))
11234         (setq prev (1+ (car unread)))
11235         (setq unread (cdr unread)))
11236       (when (<= prev (cdr active))
11237         (push (cons prev (cdr active)) read))
11238       (setq read (if (> (length read) 1) (nreverse read) read))
11239       (if compute
11240           read
11241         (save-excursion
11242           (let (setmarkundo)
11243             ;; Propagate the read marks to the backend.
11244             (when (gnus-check-backend-function 'request-set-mark group)
11245               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11246                     (add (gnus-remove-from-range read (gnus-info-read info))))
11247                 (when (or add del)
11248                   (unless (gnus-check-group group)
11249                     (error "Can't open server for %s" group))
11250                   (gnus-request-set-mark
11251                    group (delq nil (list (if add (list add 'add '(read)))
11252                                          (if del (list del 'del '(read))))))
11253                   (setq setmarkundo
11254                         `(gnus-request-set-mark
11255                           ,group
11256                           ',(delq nil (list
11257                                        (if del (list del 'add '(read)))
11258                                        (if add (list add 'del '(read))))))))))
11259             (set-buffer gnus-group-buffer)
11260             (gnus-undo-register
11261               `(progn
11262                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11263                  (gnus-info-set-read ',info ',(gnus-info-read info))
11264                  (gnus-get-unread-articles-in-group ',info
11265                                                     (gnus-active ,group))
11266                  (gnus-group-update-group ,group t)
11267                  ,setmarkundo))))
11268         ;; Enter this list into the group info.
11269         (gnus-info-set-read info read)
11270         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11271         (gnus-get-unread-articles-in-group info (gnus-active group))
11272         t))))
11273
11274 (defun gnus-offer-save-summaries ()
11275   "Offer to save all active summary buffers."
11276   (let (buffers)
11277     ;; Go through all buffers and find all summaries.
11278     (dolist (buffer (buffer-list))
11279       (when (and (setq buffer (buffer-name buffer))
11280                  (string-match "Summary" buffer)
11281                  (save-excursion
11282                    (set-buffer buffer)
11283                    ;; We check that this is, indeed, a summary buffer.
11284                    (and (eq major-mode 'gnus-summary-mode)
11285                         ;; Also make sure this isn't bogus.
11286                         gnus-newsgroup-prepared
11287                         ;; Also make sure that this isn't a
11288                         ;; dead summary buffer.
11289                         (not gnus-dead-summary-mode))))
11290         (push buffer buffers)))
11291     ;; Go through all these summary buffers and offer to save them.
11292     (when buffers
11293       (save-excursion
11294         (map-y-or-n-p
11295          "Update summary buffer %s? "
11296          (lambda (buf)
11297            (switch-to-buffer buf)
11298            (gnus-summary-exit))
11299          buffers)))))
11300
11301
11302 ;;; @ for mime-partial
11303 ;;;
11304
11305 (defun gnus-request-partial-message ()
11306   (save-excursion
11307     (let ((number (gnus-summary-article-number))
11308           (group gnus-newsgroup-name)
11309           (mother gnus-article-buffer))
11310       (set-buffer (get-buffer-create " *Partial Article*"))
11311       (erase-buffer)
11312       (setq mime-preview-buffer mother)
11313       (gnus-request-article-this-buffer number group)
11314       (mime-parse-buffer)
11315       )))
11316
11317 (autoload 'mime-combine-message/partial-pieces-automatically
11318   "mime-partial"
11319   "Internal method to combine message/partial messages automatically.")
11320
11321 (mime-add-condition
11322  'action '((type . message)(subtype . partial)
11323            (major-mode . gnus-original-article-mode)
11324            (method . mime-combine-message/partial-pieces-automatically)
11325            (summary-buffer-exp . gnus-summary-buffer)
11326            (request-partial-message-method . gnus-request-partial-message)
11327            ))
11328
11329
11330 ;;; @ for message/rfc822
11331 ;;;
11332
11333 (defun gnus-mime-extract-message/rfc822 (entity situation)
11334   "Burst a forwarded article."
11335   (save-excursion
11336     (set-buffer gnus-summary-buffer)
11337     (let* ((group (completing-read "Group: " gnus-active-hashtb nil t
11338                                    gnus-newsgroup-name 'gnus-group-history))
11339            (gnus-group-marked (list group))
11340            article info)
11341       (with-temp-buffer
11342         (mime-insert-entity-content entity)
11343         (setq article (gnus-request-accept-article group)))
11344       (when (and (consp article)
11345                  (numberp (setq article (cdr article))))
11346         (setq info (gnus-get-info group))
11347         (gnus-info-set-read info
11348                             (gnus-remove-from-range (gnus-info-read info)
11349                                                     (list article)))
11350         (when (string-equal group gnus-newsgroup-name)
11351           (forward-line 1)
11352           (let (gnus-show-threads)
11353             (gnus-summary-goto-subject article t))
11354           (gnus-summary-clear-mark-forward 1))
11355         (set-buffer gnus-group-buffer)
11356         (gnus-group-get-new-news-this-group nil t)))))
11357
11358 (mime-add-condition
11359  'action '((type . message)(subtype . rfc822)
11360            (major-mode . gnus-original-article-mode)
11361            (method . gnus-mime-extract-message/rfc822)
11362            (mode . "extract")
11363            ))
11364
11365 (mime-add-condition
11366  'action '((type . message)(subtype . news)
11367            (major-mode . gnus-original-article-mode)
11368            (method . gnus-mime-extract-message/rfc822)
11369            (mode . "extract")
11370            ))
11371
11372 (defun gnus-mime-extract-multipart (entity situation)
11373   (let ((children (mime-entity-children entity))
11374         mime-acting-situation-to-override
11375         f)
11376     (while children
11377       (mime-play-entity (car children)
11378                         (cons (assq 'mode situation)
11379                               mime-acting-situation-to-override))
11380       (setq children (cdr children)))
11381     (if (setq f (cdr (assq 'after-method
11382                            mime-acting-situation-to-override)))
11383         (eval f)
11384       )))
11385
11386 (mime-add-condition
11387  'action '((type . multipart)
11388            (method . gnus-mime-extract-multipart)
11389            (mode . "extract")
11390            )
11391  'with-default)
11392
11393
11394 ;;; @ end
11395 ;;;
11396
11397 (defun gnus-summary-inherit-default-charset ()
11398   "Import `default-mime-charset' from summary buffer.
11399 Also take care of `default-mime-charset-unlimited' if the LIMIT version
11400 of FLIM is used."
11401   (if (buffer-live-p gnus-summary-buffer)
11402       (let (d-m-c d-m-c-u)
11403         (with-current-buffer gnus-summary-buffer
11404           (setq d-m-c (if (local-variable-p 'default-mime-charset
11405                                             gnus-summary-buffer)
11406                           default-mime-charset
11407                         t)
11408                 ;; LIMIT
11409                 d-m-c-u (if (local-variable-p 'default-mime-charset-unlimited
11410                                               gnus-summary-buffer)
11411                             (symbol-value 'default-mime-charset-unlimited)
11412                           t)))
11413         (if (eq t d-m-c)
11414             (kill-local-variable 'default-mime-charset)
11415           (set (make-local-variable 'default-mime-charset) d-m-c))
11416         (if (eq t d-m-c-u)
11417             (kill-local-variable 'default-mime-charset-unlimited)
11418           (set (make-local-variable 'default-mime-charset-unlimited)
11419                d-m-c-u)))))
11420
11421 (defun gnus-summary-setup-default-charset ()
11422   "Setup newsgroup default charset."
11423   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11424       (progn
11425         (setq gnus-newsgroup-charset nil)
11426         (set (make-local-variable 'default-mime-charset) nil)
11427         (when (boundp 'default-mime-charset-unlimited);; LIMIT
11428           (set (make-local-variable 'default-mime-charset-unlimited) nil)))
11429     (let ((ignored-charsets
11430            (or gnus-newsgroup-ephemeral-ignored-charsets
11431                (append
11432                 (and gnus-newsgroup-name
11433                      (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11434                 gnus-newsgroup-ignored-charsets)))
11435           charset)
11436       (setq gnus-newsgroup-charset
11437             (or gnus-newsgroup-ephemeral-charset
11438                 (when (and gnus-newsgroup-name
11439                            (setq charset (gnus-parameter-charset
11440                                           gnus-newsgroup-name)))
11441                   (make-local-variable 'default-mime-charset)
11442                   (setq default-mime-charset charset))
11443                 gnus-default-charset))
11444       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11445            ignored-charsets))))
11446
11447 ;;;
11448 ;;; Mime Commands
11449 ;;;
11450
11451 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11452   "Display the current article buffer fully MIME-buttonized.
11453 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11454 treated as multipart/mixed."
11455   (interactive "P")
11456   (require 'gnus-art)
11457   (let ((gnus-unbuttonized-mime-types nil)
11458         (gnus-mime-display-multipart-as-mixed show-all-parts))
11459     (gnus-summary-show-article)))
11460
11461 (defun gnus-summary-repair-multipart (article)
11462   "Add a Content-Type header to a multipart article without one."
11463   (interactive (list (gnus-summary-article-number)))
11464   (gnus-with-article article
11465     (message-narrow-to-head)
11466     (message-remove-header "Mime-Version")
11467     (goto-char (point-max))
11468     (insert "Mime-Version: 1.0\n")
11469     (widen)
11470     (when (search-forward "\n--" nil t)
11471       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11472         (message-narrow-to-head)
11473         (message-remove-header "Content-Type")
11474         (goto-char (point-max))
11475         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11476                         separator))
11477         (widen))))
11478   (let (gnus-mark-article-hook)
11479     (gnus-summary-select-article t t nil article)))
11480
11481 (defun gnus-summary-toggle-display-buttonized ()
11482   "Toggle the buttonizing of the article buffer."
11483   (interactive)
11484   (require 'gnus-art)
11485   (if (setq gnus-inhibit-mime-unbuttonizing
11486             (not gnus-inhibit-mime-unbuttonizing))
11487       (let ((gnus-unbuttonized-mime-types nil))
11488         (gnus-summary-show-article))
11489     (gnus-summary-show-article)))
11490
11491 ;;;
11492 ;;; Intelli-mouse commmands
11493 ;;;
11494
11495 (defun gnus-wheel-summary-scroll (event)
11496   (interactive "e")
11497   (let ((amount (if (memq 'shift (event-modifiers event))
11498                     (car gnus-wheel-scroll-amount)
11499                   (cdr gnus-wheel-scroll-amount)))
11500         (direction (- (* (static-if (featurep 'xemacs)
11501                              (event-button event)
11502                            (cond ((eq 'mouse-4 (event-basic-type event))
11503                                   4)
11504                                  ((eq 'mouse-5 (event-basic-type event))
11505                                   5)))
11506                          2) 9))
11507         edge)
11508     (gnus-summary-scroll-up (* amount direction))
11509     (when (gnus-eval-in-buffer-window gnus-article-buffer
11510             (save-restriction
11511               (widen)
11512               (and (if (< 0 direction)
11513                        (gnus-article-next-page 0)
11514                      (gnus-article-prev-page 0)
11515                      (bobp))
11516                    (if (setq edge (get-text-property
11517                                    (point-min) 'gnus-wheel-edge))
11518                        (setq edge (* edge direction))
11519                      (setq edge -1))
11520                    (or (plusp edge)
11521                        (let ((buffer-read-only nil)
11522                              (inhibit-read-only t))
11523                          (put-text-property (point-min) (point-max)
11524                                             'gnus-wheel-edge direction)
11525                          nil))
11526                    (or (> edge gnus-wheel-edge-resistance)
11527                        (let ((buffer-read-only nil)
11528                              (inhibit-read-only t))
11529                          (put-text-property (point-min) (point-max)
11530                                             'gnus-wheel-edge
11531                                             (* (1+ edge) direction))
11532                          nil))
11533                    (eq last-command 'gnus-wheel-summary-scroll))))
11534       (gnus-summary-next-article nil nil (minusp direction)))))
11535
11536 (defun gnus-wheel-install ()
11537   "Enable mouse wheel support on summary window."
11538   (when gnus-use-wheel
11539     (let ((keys
11540            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
11541       (dolist (key keys)
11542         (define-key gnus-summary-mode-map key
11543           'gnus-wheel-summary-scroll)))))
11544
11545 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
11546
11547 ;;;
11548 ;;; Traditional PGP commmands
11549 ;;;
11550
11551 (defun gnus-summary-decrypt-article (&optional force)
11552   "Decrypt the current article in traditional PGP way.
11553 This will have permanent effect only in mail groups.
11554 If FORCE is non-nil, allow editing of articles even in read-only
11555 groups."
11556   (interactive "P")
11557   (gnus-summary-select-article t)
11558   (gnus-eval-in-buffer-window gnus-article-buffer
11559     (save-excursion
11560       (save-restriction
11561         (widen)
11562         (goto-char (point-min))
11563         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
11564           (error "Not a traditional PGP message!"))
11565         (let ((armor-start (match-beginning 0)))
11566           (if (and (pgg-decrypt-region armor-start (point-max))
11567                    (or force (not (gnus-group-read-only-p))))
11568               (let ((inhibit-read-only t)
11569                     buffer-read-only)
11570                 (delete-region armor-start
11571                                (progn
11572                                  (re-search-forward "^-+END PGP" nil t)
11573                                  (beginning-of-line 2)
11574                                  (point)))
11575                 (insert-buffer-substring pgg-output-buffer))))))))
11576
11577 (defun gnus-summary-verify-article ()
11578   "Verify the current article in traditional PGP way."
11579   (interactive)
11580   (save-excursion
11581     (set-buffer gnus-original-article-buffer)
11582     (goto-char (point-min))
11583     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
11584       (error "Not a traditional PGP message!"))
11585     (re-search-forward "^-+END PGP" nil t)
11586     (beginning-of-line 2)
11587     (call-interactively (function pgg-verify-region))))
11588
11589 ;;;
11590 ;;; Generic summary marking commands
11591 ;;;
11592
11593 (defvar gnus-summary-marking-alist
11594   '((read gnus-del-mark "d")
11595     (unread gnus-unread-mark "u")
11596     (ticked gnus-ticked-mark "!")
11597     (dormant gnus-dormant-mark "?")
11598     (expirable gnus-expirable-mark "e"))
11599   "An alist of names/marks/keystrokes.")
11600
11601 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11602 (defvar gnus-summary-mark-map)
11603
11604 (defun gnus-summary-make-all-marking-commands ()
11605   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11606   (dolist (elem gnus-summary-marking-alist)
11607     (apply 'gnus-summary-make-marking-command elem)))
11608
11609 (defun gnus-summary-make-marking-command (name mark keystroke)
11610   (let ((map (make-sparse-keymap)))
11611     (define-key gnus-summary-generic-mark-map keystroke map)
11612     (dolist (lway `((next "next" next nil "n")
11613                     (next-unread "next unread" next t "N")
11614                     (prev "previous" prev nil "p")
11615                     (prev-unread "previous unread" prev t "P")
11616                     (nomove "" nil nil ,keystroke)))
11617       (let ((func (gnus-summary-make-marking-command-1
11618                    mark (car lway) lway name)))
11619         (setq func (eval func))
11620         (define-key map (nth 4 lway) func)))))
11621
11622 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11623   `(defun ,(intern
11624             (format "gnus-summary-put-mark-as-%s%s"
11625                     name (if (eq way 'nomove)
11626                              ""
11627                            (concat "-" (symbol-name way)))))
11628      (n)
11629      ,(format
11630        "Mark the current article as %s%s.
11631 If N, the prefix, then repeat N times.
11632 If N is negative, move in reverse order.
11633 The difference between N and the actual number of articles marked is
11634 returned."
11635        name (car (cdr lway)))
11636      (interactive "p")
11637      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11638
11639 (defun gnus-summary-generic-mark (n mark move unread)
11640   "Mark N articles with MARK."
11641   (unless (eq major-mode 'gnus-summary-mode)
11642     (error "This command can only be used in the summary buffer"))
11643   (gnus-summary-show-thread)
11644   (let ((nummove
11645          (cond
11646           ((eq move 'next) 1)
11647           ((eq move 'prev) -1)
11648           (t 0))))
11649     (if (zerop nummove)
11650         (setq n 1)
11651       (when (< n 0)
11652         (setq n (abs n)
11653               nummove (* -1 nummove))))
11654     (while (and (> n 0)
11655                 (gnus-summary-mark-article nil mark)
11656                 (zerop (gnus-summary-next-subject nummove unread t)))
11657       (setq n (1- n)))
11658     (when (/= 0 n)
11659       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11660     (gnus-summary-recenter)
11661     (gnus-summary-position-point)
11662     (gnus-set-mode-line 'summary)
11663     n))
11664
11665 (defun gnus-summary-insert-articles (articles)
11666   (when (setq articles
11667               (gnus-sorted-difference articles
11668                                       (mapcar (lambda (h)
11669                                                 (mail-header-number h))
11670                                               gnus-newsgroup-headers)))
11671     (setq gnus-newsgroup-headers
11672           (merge 'list
11673                  gnus-newsgroup-headers
11674                  (gnus-fetch-headers articles)
11675                  'gnus-article-sort-by-number))
11676     ;; Suppress duplicates?
11677     (when gnus-suppress-duplicates
11678       (gnus-dup-suppress-articles))
11679
11680     ;; We might want to build some more threads first.
11681     (when (and gnus-fetch-old-headers
11682                (eq gnus-headers-retrieved-by 'nov))
11683       (if (eq gnus-fetch-old-headers 'invisible)
11684           (gnus-build-all-threads)
11685         (gnus-build-old-threads)))
11686     ;; Let the Gnus agent mark articles as read.
11687     (when gnus-agent
11688       (gnus-agent-get-undownloaded-list))
11689     ;; Remove list identifiers from subject
11690     (when gnus-list-identifiers
11691       (gnus-summary-remove-list-identifiers))
11692     ;; First and last article in this newsgroup.
11693     (when gnus-newsgroup-headers
11694       (setq gnus-newsgroup-begin
11695             (mail-header-number (car gnus-newsgroup-headers))
11696             gnus-newsgroup-end
11697             (mail-header-number
11698              (gnus-last-element gnus-newsgroup-headers))))
11699     (when gnus-use-scoring
11700       (gnus-possibly-score-headers))))
11701
11702 (defun gnus-summary-insert-old-articles (&optional all)
11703   "Insert all old articles in this group.
11704 If ALL is non-nil, already read articles become readable.
11705 If ALL is a number, fetch this number of articles."
11706   (interactive "P")
11707   (prog1
11708       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11709             older len)
11710         (setq older
11711               ;; Some nntp servers lie about their active range.  When
11712               ;; this happens, the active range can be in the millions.
11713               ;; Use a compressed range to avoid creating a huge list.
11714               (gnus-range-difference (list gnus-newsgroup-active) old))
11715         (setq len (gnus-range-length older))
11716         (cond
11717          ((null older) nil)
11718          ((numberp all)
11719           (if (< all len)
11720               (let ((older-range (nreverse older)))
11721                 (setq older nil)
11722
11723                 (while (> all 0)
11724                   (let* ((r (pop older-range))
11725                          (min (if (numberp r) r (car r)))
11726                          (max (if (numberp r) r (cdr r))))
11727                     (while (and (<= min max)
11728                                 (> all 0))
11729                       (push max older)
11730                       (setq all (1- all)
11731                             max (1- max))))))
11732             (setq older (gnus-uncompress-range older))))
11733          (all
11734           (setq older (gnus-uncompress-range older)))
11735          (t
11736           (when (and (numberp gnus-large-newsgroup)
11737                    (> len gnus-large-newsgroup))
11738               (let* ((cursor-in-echo-area nil)
11739                      (initial (gnus-parameter-large-newsgroup-initial
11740                                gnus-newsgroup-name))
11741                      (input
11742                       (read-string
11743                        (format
11744                         "How many articles from %s (%s %d): "
11745                         (gnus-limit-string
11746                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11747                         (if initial "max" "default")
11748                         len)
11749                        (if initial
11750                            (cons (number-to-string initial)
11751                                  0)))))
11752                 (unless (string-match "^[ \t]*$" input)
11753                   (setq all (string-to-number input))
11754                   (if (< all len)
11755                       (let ((older-range (nreverse older)))
11756                         (setq older nil)
11757
11758                         (while (> all 0)
11759                           (let* ((r (pop older-range))
11760                                  (min (if (numberp r) r (car r)))
11761                                  (max (if (numberp r) r (cdr r))))
11762                             (while (and (<= min max)
11763                                         (> all 0))
11764                               (push max older)
11765                               (setq all (1- all)
11766                                     max (1- max))))))))))
11767           (setq older (gnus-uncompress-range older))))
11768         (if (not older)
11769             (message "No old news.")
11770           (gnus-summary-insert-articles older)
11771           (gnus-summary-limit (gnus-sorted-nunion old older))))
11772     (gnus-summary-position-point)))
11773
11774 (defun gnus-summary-insert-new-articles ()
11775   "Insert all new articles in this group."
11776   (interactive)
11777   (prog1
11778       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11779             (old-active gnus-newsgroup-active)
11780             (nnmail-fetched-sources (list t))
11781             i new)
11782         (setq gnus-newsgroup-active
11783               (gnus-activate-group gnus-newsgroup-name 'scan))
11784         (setq i (cdr gnus-newsgroup-active))
11785         (while (> i (cdr old-active))
11786           (push i new)
11787           (decf i))
11788         (if (not new)
11789             (message "No gnus is bad news.")
11790           (gnus-summary-insert-articles new)
11791           (setq gnus-newsgroup-unreads
11792                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11793           (gnus-summary-limit (gnus-sorted-nunion old new))))
11794     (gnus-summary-position-point)))
11795
11796 (gnus-summary-make-all-marking-commands)
11797
11798 (gnus-ems-redefine)
11799
11800 (provide 'gnus-sum)
11801
11802 (run-hooks 'gnus-sum-load-hook)
11803
11804 ;;; gnus-sum.el ends here